xref: /linux-6.15/scripts/checkpatch.pl (revision cc77cdca)
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
1304876830SAndy Whitcroftmy $V = '0.29';
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:
185fb9e9096SAndy Whitcroft	(?:__)?(?:u|s|be|le)(?:8|16|32|64)|
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) = @_;
10009a974fdbSAndy Whitcroft	my $notPermitted = qr{(?:
10010776e594SAndy Whitcroft		^(?:
10020776e594SAndy Whitcroft			$Modifier|
10030776e594SAndy Whitcroft			$Storage|
10040776e594SAndy Whitcroft			$Type|
10059a974fdbSAndy Whitcroft			DEFINE_\S+
10069a974fdbSAndy Whitcroft		)$|
10079a974fdbSAndy Whitcroft		^(?:
10080776e594SAndy Whitcroft			goto|
10090776e594SAndy Whitcroft			return|
10100776e594SAndy Whitcroft			case|
10110776e594SAndy Whitcroft			else|
10120776e594SAndy Whitcroft			asm|__asm__|
10130776e594SAndy Whitcroft			do
10149a974fdbSAndy Whitcroft		)(?:\s|$)|
10150776e594SAndy Whitcroft		^(?:typedef|struct|enum)\b
10169a974fdbSAndy Whitcroft	    )}x;
10179a974fdbSAndy Whitcroft	warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
10189a974fdbSAndy Whitcroft	if ($possible !~ $notPermitted) {
1019c45dcabdSAndy Whitcroft		# Check for modifiers.
1020c45dcabdSAndy Whitcroft		$possible =~ s/\s*$Storage\s*//g;
1021c45dcabdSAndy Whitcroft		$possible =~ s/\s*$Sparse\s*//g;
1022c45dcabdSAndy Whitcroft		if ($possible =~ /^\s*$/) {
1023c45dcabdSAndy Whitcroft
1024c45dcabdSAndy Whitcroft		} elsif ($possible =~ /\s/) {
1025c45dcabdSAndy Whitcroft			$possible =~ s/\s*$Type\s*//g;
1026d2506586SAndy Whitcroft			for my $modifier (split(' ', $possible)) {
10279a974fdbSAndy Whitcroft				if ($modifier !~ $notPermitted) {
1028d2506586SAndy Whitcroft					warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1029d2506586SAndy Whitcroft					push(@modifierList, $modifier);
1030d2506586SAndy Whitcroft				}
10319a974fdbSAndy Whitcroft			}
1032c45dcabdSAndy Whitcroft
1033c45dcabdSAndy Whitcroft		} else {
103413214adfSAndy Whitcroft			warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
10358905a67cSAndy Whitcroft			push(@typeList, $possible);
1036c45dcabdSAndy Whitcroft		}
10378905a67cSAndy Whitcroft		build_types();
10380776e594SAndy Whitcroft	} else {
10390776e594SAndy Whitcroft		warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
10408905a67cSAndy Whitcroft	}
10418905a67cSAndy Whitcroft}
10428905a67cSAndy Whitcroft
10436c72ffaaSAndy Whitcroftmy $prefix = '';
10446c72ffaaSAndy Whitcroft
1045f0a594c1SAndy Whitcroftsub report {
1046773647a0SAndy Whitcroft	if (defined $tst_only && $_[0] !~ /\Q$tst_only\E/) {
1047773647a0SAndy Whitcroft		return 0;
1048773647a0SAndy Whitcroft	}
10498905a67cSAndy Whitcroft	my $line = $prefix . $_[0];
10508905a67cSAndy Whitcroft
10518905a67cSAndy Whitcroft	$line = (split('\n', $line))[0] . "\n" if ($terse);
10528905a67cSAndy Whitcroft
105313214adfSAndy Whitcroft	push(our @report, $line);
1054773647a0SAndy Whitcroft
1055773647a0SAndy Whitcroft	return 1;
1056f0a594c1SAndy Whitcroft}
1057f0a594c1SAndy Whitcroftsub report_dump {
105813214adfSAndy Whitcroft	our @report;
1059f0a594c1SAndy Whitcroft}
1060de7d4f0eSAndy Whitcroftsub ERROR {
1061773647a0SAndy Whitcroft	if (report("ERROR: $_[0]\n")) {
1062de7d4f0eSAndy Whitcroft		our $clean = 0;
10636c72ffaaSAndy Whitcroft		our $cnt_error++;
1064de7d4f0eSAndy Whitcroft	}
1065773647a0SAndy Whitcroft}
1066de7d4f0eSAndy Whitcroftsub WARN {
1067773647a0SAndy Whitcroft	if (report("WARNING: $_[0]\n")) {
1068de7d4f0eSAndy Whitcroft		our $clean = 0;
10696c72ffaaSAndy Whitcroft		our $cnt_warn++;
1070de7d4f0eSAndy Whitcroft	}
1071773647a0SAndy Whitcroft}
1072de7d4f0eSAndy Whitcroftsub CHK {
1073773647a0SAndy Whitcroft	if ($check && report("CHECK: $_[0]\n")) {
1074de7d4f0eSAndy Whitcroft		our $clean = 0;
10756c72ffaaSAndy Whitcroft		our $cnt_chk++;
10766c72ffaaSAndy Whitcroft	}
1077de7d4f0eSAndy Whitcroft}
1078de7d4f0eSAndy Whitcroft
10796ecd9674SAndy Whitcroftsub check_absolute_file {
10806ecd9674SAndy Whitcroft	my ($absolute, $herecurr) = @_;
10816ecd9674SAndy Whitcroft	my $file = $absolute;
10826ecd9674SAndy Whitcroft
10836ecd9674SAndy Whitcroft	##print "absolute<$absolute>\n";
10846ecd9674SAndy Whitcroft
10856ecd9674SAndy Whitcroft	# See if any suffix of this path is a path within the tree.
10866ecd9674SAndy Whitcroft	while ($file =~ s@^[^/]*/@@) {
10876ecd9674SAndy Whitcroft		if (-f "$root/$file") {
10886ecd9674SAndy Whitcroft			##print "file<$file>\n";
10896ecd9674SAndy Whitcroft			last;
10906ecd9674SAndy Whitcroft		}
10916ecd9674SAndy Whitcroft	}
10926ecd9674SAndy Whitcroft	if (! -f _)  {
10936ecd9674SAndy Whitcroft		return 0;
10946ecd9674SAndy Whitcroft	}
10956ecd9674SAndy Whitcroft
10966ecd9674SAndy Whitcroft	# It is, so see if the prefix is acceptable.
10976ecd9674SAndy Whitcroft	my $prefix = $absolute;
10986ecd9674SAndy Whitcroft	substr($prefix, -length($file)) = '';
10996ecd9674SAndy Whitcroft
11006ecd9674SAndy Whitcroft	##print "prefix<$prefix>\n";
11016ecd9674SAndy Whitcroft	if ($prefix ne ".../") {
11026ecd9674SAndy Whitcroft		WARN("use relative pathname instead of absolute in changelog text\n" . $herecurr);
11036ecd9674SAndy Whitcroft	}
11046ecd9674SAndy Whitcroft}
11056ecd9674SAndy Whitcroft
11060a920b5bSAndy Whitcroftsub process {
11070a920b5bSAndy Whitcroft	my $filename = shift;
11080a920b5bSAndy Whitcroft
11090a920b5bSAndy Whitcroft	my $linenr=0;
11100a920b5bSAndy Whitcroft	my $prevline="";
1111c2fdda0dSAndy Whitcroft	my $prevrawline="";
11120a920b5bSAndy Whitcroft	my $stashline="";
1113c2fdda0dSAndy Whitcroft	my $stashrawline="";
11140a920b5bSAndy Whitcroft
11154a0df2efSAndy Whitcroft	my $length;
11160a920b5bSAndy Whitcroft	my $indent;
11170a920b5bSAndy Whitcroft	my $previndent=0;
11180a920b5bSAndy Whitcroft	my $stashindent=0;
11190a920b5bSAndy Whitcroft
1120de7d4f0eSAndy Whitcroft	our $clean = 1;
11210a920b5bSAndy Whitcroft	my $signoff = 0;
11220a920b5bSAndy Whitcroft	my $is_patch = 0;
11230a920b5bSAndy Whitcroft
112413214adfSAndy Whitcroft	our @report = ();
11256c72ffaaSAndy Whitcroft	our $cnt_lines = 0;
11266c72ffaaSAndy Whitcroft	our $cnt_error = 0;
11276c72ffaaSAndy Whitcroft	our $cnt_warn = 0;
11286c72ffaaSAndy Whitcroft	our $cnt_chk = 0;
11296c72ffaaSAndy Whitcroft
11300a920b5bSAndy Whitcroft	# Trace the real file/line as we go.
11310a920b5bSAndy Whitcroft	my $realfile = '';
11320a920b5bSAndy Whitcroft	my $realline = 0;
11330a920b5bSAndy Whitcroft	my $realcnt = 0;
11340a920b5bSAndy Whitcroft	my $here = '';
11350a920b5bSAndy Whitcroft	my $in_comment = 0;
1136c2fdda0dSAndy Whitcroft	my $comment_edge = 0;
11370a920b5bSAndy Whitcroft	my $first_line = 0;
11381e855726SWolfram Sang	my $p1_prefix = '';
11390a920b5bSAndy Whitcroft
114013214adfSAndy Whitcroft	my $prev_values = 'E';
114113214adfSAndy Whitcroft
114213214adfSAndy Whitcroft	# suppression flags
1143773647a0SAndy Whitcroft	my %suppress_ifbraces;
1144170d3a22SAndy Whitcroft	my %suppress_whiletrailers;
1145653d4876SAndy Whitcroft
1146c2fdda0dSAndy Whitcroft	# Pre-scan the patch sanitizing the lines.
1147de7d4f0eSAndy Whitcroft	# Pre-scan the patch looking for any __setup documentation.
1148c2fdda0dSAndy Whitcroft	#
1149de7d4f0eSAndy Whitcroft	my @setup_docs = ();
1150de7d4f0eSAndy Whitcroft	my $setup_docs = 0;
1151773647a0SAndy Whitcroft
1152773647a0SAndy Whitcroft	sanitise_line_reset();
1153c2fdda0dSAndy Whitcroft	my $line;
1154c2fdda0dSAndy Whitcroft	foreach my $rawline (@rawlines) {
1155773647a0SAndy Whitcroft		$linenr++;
1156773647a0SAndy Whitcroft		$line = $rawline;
1157c2fdda0dSAndy Whitcroft
1158773647a0SAndy Whitcroft		if ($rawline=~/^\+\+\+\s+(\S+)/) {
1159de7d4f0eSAndy Whitcroft			$setup_docs = 0;
1160de7d4f0eSAndy Whitcroft			if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
1161de7d4f0eSAndy Whitcroft				$setup_docs = 1;
1162de7d4f0eSAndy Whitcroft			}
1163773647a0SAndy Whitcroft			#next;
1164de7d4f0eSAndy Whitcroft		}
1165773647a0SAndy Whitcroft		if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1166773647a0SAndy Whitcroft			$realline=$1-1;
1167773647a0SAndy Whitcroft			if (defined $2) {
1168773647a0SAndy Whitcroft				$realcnt=$3+1;
1169773647a0SAndy Whitcroft			} else {
1170773647a0SAndy Whitcroft				$realcnt=1+1;
1171773647a0SAndy Whitcroft			}
1172c45dcabdSAndy Whitcroft			$in_comment = 0;
1173773647a0SAndy Whitcroft
1174773647a0SAndy Whitcroft			# Guestimate if this is a continuing comment.  Run
1175773647a0SAndy Whitcroft			# the context looking for a comment "edge".  If this
1176773647a0SAndy Whitcroft			# edge is a close comment then we must be in a comment
1177773647a0SAndy Whitcroft			# at context start.
1178773647a0SAndy Whitcroft			my $edge;
117901fa9147SAndy Whitcroft			my $cnt = $realcnt;
118001fa9147SAndy Whitcroft			for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
118101fa9147SAndy Whitcroft				next if (defined $rawlines[$ln - 1] &&
118201fa9147SAndy Whitcroft					 $rawlines[$ln - 1] =~ /^-/);
118301fa9147SAndy Whitcroft				$cnt--;
118401fa9147SAndy Whitcroft				#print "RAW<$rawlines[$ln - 1]>\n";
1185721c1cb6SAndy Whitcroft				last if (!defined $rawlines[$ln - 1]);
1186fae17daeSAndy Whitcroft				if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
1187fae17daeSAndy Whitcroft				    $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
1188fae17daeSAndy Whitcroft					($edge) = $1;
1189fae17daeSAndy Whitcroft					last;
1190fae17daeSAndy Whitcroft				}
1191773647a0SAndy Whitcroft			}
1192773647a0SAndy Whitcroft			if (defined $edge && $edge eq '*/') {
1193773647a0SAndy Whitcroft				$in_comment = 1;
1194773647a0SAndy Whitcroft			}
1195773647a0SAndy Whitcroft
1196773647a0SAndy Whitcroft			# Guestimate if this is a continuing comment.  If this
1197773647a0SAndy Whitcroft			# is the start of a diff block and this line starts
1198773647a0SAndy Whitcroft			# ' *' then it is very likely a comment.
1199773647a0SAndy Whitcroft			if (!defined $edge &&
120083242e0cSAndy Whitcroft			    $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
1201773647a0SAndy Whitcroft			{
1202773647a0SAndy Whitcroft				$in_comment = 1;
1203773647a0SAndy Whitcroft			}
1204773647a0SAndy Whitcroft
1205773647a0SAndy Whitcroft			##print "COMMENT:$in_comment edge<$edge> $rawline\n";
1206773647a0SAndy Whitcroft			sanitise_line_reset($in_comment);
1207773647a0SAndy Whitcroft
1208171ae1a4SAndy Whitcroft		} elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
1209773647a0SAndy Whitcroft			# Standardise the strings and chars within the input to
1210171ae1a4SAndy Whitcroft			# simplify matching -- only bother with positive lines.
1211773647a0SAndy Whitcroft			$line = sanitise_line($rawline);
1212773647a0SAndy Whitcroft		}
1213773647a0SAndy Whitcroft		push(@lines, $line);
1214773647a0SAndy Whitcroft
1215773647a0SAndy Whitcroft		if ($realcnt > 1) {
1216773647a0SAndy Whitcroft			$realcnt-- if ($line =~ /^(?:\+| |$)/);
1217773647a0SAndy Whitcroft		} else {
1218773647a0SAndy Whitcroft			$realcnt = 0;
1219773647a0SAndy Whitcroft		}
1220773647a0SAndy Whitcroft
1221773647a0SAndy Whitcroft		#print "==>$rawline\n";
1222773647a0SAndy Whitcroft		#print "-->$line\n";
1223de7d4f0eSAndy Whitcroft
1224de7d4f0eSAndy Whitcroft		if ($setup_docs && $line =~ /^\+/) {
1225de7d4f0eSAndy Whitcroft			push(@setup_docs, $line);
1226de7d4f0eSAndy Whitcroft		}
1227de7d4f0eSAndy Whitcroft	}
1228de7d4f0eSAndy Whitcroft
12296c72ffaaSAndy Whitcroft	$prefix = '';
12306c72ffaaSAndy Whitcroft
1231773647a0SAndy Whitcroft	$realcnt = 0;
1232773647a0SAndy Whitcroft	$linenr = 0;
12330a920b5bSAndy Whitcroft	foreach my $line (@lines) {
12340a920b5bSAndy Whitcroft		$linenr++;
12350a920b5bSAndy Whitcroft
1236c2fdda0dSAndy Whitcroft		my $rawline = $rawlines[$linenr - 1];
12376c72ffaaSAndy Whitcroft
12380a920b5bSAndy Whitcroft#extract the line range in the file after the patch is applied
12396c72ffaaSAndy Whitcroft		if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
12400a920b5bSAndy Whitcroft			$is_patch = 1;
12414a0df2efSAndy Whitcroft			$first_line = $linenr + 1;
12420a920b5bSAndy Whitcroft			$realline=$1-1;
12430a920b5bSAndy Whitcroft			if (defined $2) {
12440a920b5bSAndy Whitcroft				$realcnt=$3+1;
12450a920b5bSAndy Whitcroft			} else {
12460a920b5bSAndy Whitcroft				$realcnt=1+1;
12470a920b5bSAndy Whitcroft			}
1248c2fdda0dSAndy Whitcroft			annotate_reset();
124913214adfSAndy Whitcroft			$prev_values = 'E';
125013214adfSAndy Whitcroft
1251773647a0SAndy Whitcroft			%suppress_ifbraces = ();
1252170d3a22SAndy Whitcroft			%suppress_whiletrailers = ();
12530a920b5bSAndy Whitcroft			next;
12540a920b5bSAndy Whitcroft
12554a0df2efSAndy Whitcroft# track the line number as we move through the hunk, note that
12564a0df2efSAndy Whitcroft# new versions of GNU diff omit the leading space on completely
12574a0df2efSAndy Whitcroft# blank context lines so we need to count that too.
1258773647a0SAndy Whitcroft		} elsif ($line =~ /^( |\+|$)/) {
12590a920b5bSAndy Whitcroft			$realline++;
1260d8aaf121SAndy Whitcroft			$realcnt-- if ($realcnt != 0);
12610a920b5bSAndy Whitcroft
12624a0df2efSAndy Whitcroft			# Measure the line length and indent.
1263c2fdda0dSAndy Whitcroft			($length, $indent) = line_stats($rawline);
12640a920b5bSAndy Whitcroft
12650a920b5bSAndy Whitcroft			# Track the previous line.
12660a920b5bSAndy Whitcroft			($prevline, $stashline) = ($stashline, $line);
12670a920b5bSAndy Whitcroft			($previndent, $stashindent) = ($stashindent, $indent);
1268c2fdda0dSAndy Whitcroft			($prevrawline, $stashrawline) = ($stashrawline, $rawline);
1269c2fdda0dSAndy Whitcroft
1270773647a0SAndy Whitcroft			#warn "line<$line>\n";
12716c72ffaaSAndy Whitcroft
1272d8aaf121SAndy Whitcroft		} elsif ($realcnt == 1) {
1273d8aaf121SAndy Whitcroft			$realcnt--;
12740a920b5bSAndy Whitcroft		}
12750a920b5bSAndy Whitcroft
1276*cc77cdcaSAndy Whitcroft		my $hunk_line = ($realcnt != 0);
1277*cc77cdcaSAndy Whitcroft
12780a920b5bSAndy Whitcroft#make up the handle for any error we report on this line
1279773647a0SAndy Whitcroft		$prefix = "$filename:$realline: " if ($emacs && $file);
1280773647a0SAndy Whitcroft		$prefix = "$filename:$linenr: " if ($emacs && !$file);
1281773647a0SAndy Whitcroft
12826c72ffaaSAndy Whitcroft		$here = "#$linenr: " if (!$file);
12836c72ffaaSAndy Whitcroft		$here = "#$realline: " if ($file);
1284773647a0SAndy Whitcroft
1285773647a0SAndy Whitcroft		# extract the filename as it passes
1286773647a0SAndy Whitcroft		if ($line=~/^\+\+\+\s+(\S+)/) {
1287773647a0SAndy Whitcroft			$realfile = $1;
12881e855726SWolfram Sang			$realfile =~ s@^([^/]*)/@@;
12891e855726SWolfram Sang
12901e855726SWolfram Sang			$p1_prefix = $1;
1291e2f7aa4bSAndy Whitcroft			if (!$file && $tree && $p1_prefix ne '' &&
1292e2f7aa4bSAndy Whitcroft			    -e "$root/$p1_prefix") {
12931e855726SWolfram Sang				WARN("patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
12941e855726SWolfram Sang			}
1295773647a0SAndy Whitcroft
1296c1ab3326SAndy Whitcroft			if ($realfile =~ m@^include/asm/@) {
1297773647a0SAndy Whitcroft				ERROR("do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
1298773647a0SAndy Whitcroft			}
1299773647a0SAndy Whitcroft			next;
1300773647a0SAndy Whitcroft		}
1301773647a0SAndy Whitcroft
1302389834b6SRandy Dunlap		$here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
13030a920b5bSAndy Whitcroft
1304c2fdda0dSAndy Whitcroft		my $hereline = "$here\n$rawline\n";
1305c2fdda0dSAndy Whitcroft		my $herecurr = "$here\n$rawline\n";
1306c2fdda0dSAndy Whitcroft		my $hereprev = "$here\n$prevrawline\n$rawline\n";
13070a920b5bSAndy Whitcroft
13086c72ffaaSAndy Whitcroft		$cnt_lines++ if ($realcnt != 0);
13096c72ffaaSAndy Whitcroft
13100a920b5bSAndy Whitcroft#check the patch for a signoff:
1311d8aaf121SAndy Whitcroft		if ($line =~ /^\s*signed-off-by:/i) {
13124a0df2efSAndy Whitcroft			# This is a signoff, if ugly, so do not double report.
13134a0df2efSAndy Whitcroft			$signoff++;
13140a920b5bSAndy Whitcroft			if (!($line =~ /^\s*Signed-off-by:/)) {
1315de7d4f0eSAndy Whitcroft				WARN("Signed-off-by: is the preferred form\n" .
1316de7d4f0eSAndy Whitcroft					$herecurr);
13170a920b5bSAndy Whitcroft			}
13180a920b5bSAndy Whitcroft			if ($line =~ /^\s*signed-off-by:\S/i) {
1319773647a0SAndy Whitcroft				WARN("space required after Signed-off-by:\n" .
1320de7d4f0eSAndy Whitcroft					$herecurr);
13210a920b5bSAndy Whitcroft			}
13220a920b5bSAndy Whitcroft		}
13230a920b5bSAndy Whitcroft
132400df344fSAndy Whitcroft# Check for wrappage within a valid hunk of the file
13258905a67cSAndy Whitcroft		if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
1326de7d4f0eSAndy Whitcroft			ERROR("patch seems to be corrupt (line wrapped?)\n" .
13276c72ffaaSAndy Whitcroft				$herecurr) if (!$emitted_corrupt++);
1328de7d4f0eSAndy Whitcroft		}
1329de7d4f0eSAndy Whitcroft
13306ecd9674SAndy Whitcroft# Check for absolute kernel paths.
13316ecd9674SAndy Whitcroft		if ($tree) {
13326ecd9674SAndy Whitcroft			while ($line =~ m{(?:^|\s)(/\S*)}g) {
13336ecd9674SAndy Whitcroft				my $file = $1;
13346ecd9674SAndy Whitcroft
13356ecd9674SAndy Whitcroft				if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
13366ecd9674SAndy Whitcroft				    check_absolute_file($1, $herecurr)) {
13376ecd9674SAndy Whitcroft					#
13386ecd9674SAndy Whitcroft				} else {
13396ecd9674SAndy Whitcroft					check_absolute_file($file, $herecurr);
13406ecd9674SAndy Whitcroft				}
13416ecd9674SAndy Whitcroft			}
13426ecd9674SAndy Whitcroft		}
13436ecd9674SAndy Whitcroft
1344de7d4f0eSAndy Whitcroft# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
1345de7d4f0eSAndy Whitcroft		if (($realfile =~ /^$/ || $line =~ /^\+/) &&
1346171ae1a4SAndy Whitcroft		    $rawline !~ m/^$UTF8*$/) {
1347171ae1a4SAndy Whitcroft			my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
1348171ae1a4SAndy Whitcroft
1349171ae1a4SAndy Whitcroft			my $blank = copy_spacing($rawline);
1350171ae1a4SAndy Whitcroft			my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
1351171ae1a4SAndy Whitcroft			my $hereptr = "$hereline$ptr\n";
1352171ae1a4SAndy Whitcroft
1353171ae1a4SAndy Whitcroft			ERROR("Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
135400df344fSAndy Whitcroft		}
13550a920b5bSAndy Whitcroft
135630670854SAndy Whitcroft# ignore non-hunk lines and lines being removed
135730670854SAndy Whitcroft		next if (!$hunk_line || $line =~ /^-/);
135800df344fSAndy Whitcroft
13590a920b5bSAndy Whitcroft#trailing whitespace
13609c0ca6f9SAndy Whitcroft		if ($line =~ /^\+.*\015/) {
1361c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
13629c0ca6f9SAndy Whitcroft			ERROR("DOS line endings\n" . $herevet);
13639c0ca6f9SAndy Whitcroft
1364c2fdda0dSAndy Whitcroft		} elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
1365c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1366de7d4f0eSAndy Whitcroft			ERROR("trailing whitespace\n" . $herevet);
13670a920b5bSAndy Whitcroft		}
13685368df20SAndy Whitcroft
13695368df20SAndy Whitcroft# check we are in a valid source file if not then ignore this hunk
13705368df20SAndy Whitcroft		next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
13715368df20SAndy Whitcroft
13720a920b5bSAndy Whitcroft#80 column limit
1373c45dcabdSAndy Whitcroft		if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ &&
1374f4c014c0SAndy Whitcroft		    $rawline !~ /^.\s*\*\s*\@$Ident\s/ &&
1375f4c014c0SAndy Whitcroft		    $line !~ /^\+\s*printk\s*\(\s*(?:KERN_\S+\s*)?"[X\t]*"\s*(?:,|\)\s*;)\s*$/ &&
1376f4c014c0SAndy Whitcroft		    $length > 80)
1377c45dcabdSAndy Whitcroft		{
1378de7d4f0eSAndy Whitcroft			WARN("line over 80 characters\n" . $herecurr);
13790a920b5bSAndy Whitcroft		}
13800a920b5bSAndy Whitcroft
13818905a67cSAndy Whitcroft# check for adding lines without a newline.
13828905a67cSAndy Whitcroft		if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
13838905a67cSAndy Whitcroft			WARN("adding a line without newline at end of file\n" . $herecurr);
13848905a67cSAndy Whitcroft		}
13858905a67cSAndy Whitcroft
138642e41c54SMike Frysinger# Blackfin: use hi/lo macros
138742e41c54SMike Frysinger		if ($realfile =~ m@arch/blackfin/.*\.S$@) {
138842e41c54SMike Frysinger			if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
138942e41c54SMike Frysinger				my $herevet = "$here\n" . cat_vet($line) . "\n";
139042e41c54SMike Frysinger				ERROR("use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
139142e41c54SMike Frysinger			}
139242e41c54SMike Frysinger			if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
139342e41c54SMike Frysinger				my $herevet = "$here\n" . cat_vet($line) . "\n";
139442e41c54SMike Frysinger				ERROR("use the HI() macro, not (... >> 16)\n" . $herevet);
139542e41c54SMike Frysinger			}
139642e41c54SMike Frysinger		}
139742e41c54SMike Frysinger
1398b9ea10d6SAndy Whitcroft# check we are in a valid source file C or perl if not then ignore this hunk
1399b9ea10d6SAndy Whitcroft		next if ($realfile !~ /\.(h|c|pl)$/);
14000a920b5bSAndy Whitcroft
14010a920b5bSAndy Whitcroft# at the beginning of a line any tabs must come first and anything
14020a920b5bSAndy Whitcroft# more than 8 must use tabs.
1403c2fdda0dSAndy Whitcroft		if ($rawline =~ /^\+\s* \t\s*\S/ ||
1404c2fdda0dSAndy Whitcroft		    $rawline =~ /^\+\s*        \s*/) {
1405c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1406171ae1a4SAndy Whitcroft			ERROR("code indent should use tabs where possible\n" . $herevet);
14070a920b5bSAndy Whitcroft		}
14080a920b5bSAndy Whitcroft
1409b9ea10d6SAndy Whitcroft# check we are in a valid C source file if not then ignore this hunk
1410b9ea10d6SAndy Whitcroft		next if ($realfile !~ /\.(h|c)$/);
1411b9ea10d6SAndy Whitcroft
1412c2fdda0dSAndy Whitcroft# check for RCS/CVS revision markers
1413cf655043SAndy Whitcroft		if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
1414c2fdda0dSAndy Whitcroft			WARN("CVS style keyword markers, these will _not_ be updated\n". $herecurr);
1415c2fdda0dSAndy Whitcroft		}
141622f2a2efSAndy Whitcroft
141742e41c54SMike Frysinger# Blackfin: don't use __builtin_bfin_[cs]sync
141842e41c54SMike Frysinger		if ($line =~ /__builtin_bfin_csync/) {
141942e41c54SMike Frysinger			my $herevet = "$here\n" . cat_vet($line) . "\n";
142042e41c54SMike Frysinger			ERROR("use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
142142e41c54SMike Frysinger		}
142242e41c54SMike Frysinger		if ($line =~ /__builtin_bfin_ssync/) {
142342e41c54SMike Frysinger			my $herevet = "$here\n" . cat_vet($line) . "\n";
142442e41c54SMike Frysinger			ERROR("use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
142542e41c54SMike Frysinger		}
142642e41c54SMike Frysinger
14279c0ca6f9SAndy Whitcroft# Check for potential 'bare' types
1428170d3a22SAndy Whitcroft		my ($stat, $cond, $line_nr_next, $remain_next, $off_next);
14299c9ba34eSAndy Whitcroft		if ($realcnt && $line =~ /.\s*\S/) {
1430170d3a22SAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
1431f5fe35ddSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0);
1432171ae1a4SAndy Whitcroft			$stat =~ s/\n./\n /g;
1433171ae1a4SAndy Whitcroft			$cond =~ s/\n./\n /g;
1434171ae1a4SAndy Whitcroft
1435171ae1a4SAndy Whitcroft			my $s = $stat;
1436171ae1a4SAndy Whitcroft			$s =~ s/{.*$//s;
1437cf655043SAndy Whitcroft
1438c2fdda0dSAndy Whitcroft			# Ignore goto labels.
1439171ae1a4SAndy Whitcroft			if ($s =~ /$Ident:\*$/s) {
1440c2fdda0dSAndy Whitcroft
1441c2fdda0dSAndy Whitcroft			# Ignore functions being called
1442171ae1a4SAndy Whitcroft			} elsif ($s =~ /^.\s*$Ident\s*\(/s) {
1443c2fdda0dSAndy Whitcroft
1444463f2864SAndy Whitcroft			} elsif ($s =~ /^.\s*else\b/s) {
1445463f2864SAndy Whitcroft
1446c45dcabdSAndy Whitcroft			# declarations always start with types
1447d2506586SAndy 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) {
1448c45dcabdSAndy Whitcroft				my $type = $1;
1449c45dcabdSAndy Whitcroft				$type =~ s/\s+/ /g;
1450c45dcabdSAndy Whitcroft				possible($type, "A:" . $s);
1451c45dcabdSAndy Whitcroft
14526c72ffaaSAndy Whitcroft			# definitions in global scope can only start with types
1453a6a84062SAndy Whitcroft			} elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
1454c45dcabdSAndy Whitcroft				possible($1, "B:" . $s);
1455c2fdda0dSAndy Whitcroft			}
14568905a67cSAndy Whitcroft
14576c72ffaaSAndy Whitcroft			# any (foo ... *) is a pointer cast, and foo is a type
145865863862SAndy Whitcroft			while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
1459c45dcabdSAndy Whitcroft				possible($1, "C:" . $s);
14609c0ca6f9SAndy Whitcroft			}
14618905a67cSAndy Whitcroft
14628905a67cSAndy Whitcroft			# Check for any sort of function declaration.
14638905a67cSAndy Whitcroft			# int foo(something bar, other baz);
14648905a67cSAndy Whitcroft			# void (*store_gdt)(x86_descr_ptr *);
1465171ae1a4SAndy Whitcroft			if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
14668905a67cSAndy Whitcroft				my ($name_len) = length($1);
14678905a67cSAndy Whitcroft
1468cf655043SAndy Whitcroft				my $ctx = $s;
1469773647a0SAndy Whitcroft				substr($ctx, 0, $name_len + 1, '');
14708905a67cSAndy Whitcroft				$ctx =~ s/\)[^\)]*$//;
1471cf655043SAndy Whitcroft
14728905a67cSAndy Whitcroft				for my $arg (split(/\s*,\s*/, $ctx)) {
1473c45dcabdSAndy Whitcroft					if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
14748905a67cSAndy Whitcroft
1475c45dcabdSAndy Whitcroft						possible($1, "D:" . $s);
14768905a67cSAndy Whitcroft					}
14778905a67cSAndy Whitcroft				}
14788905a67cSAndy Whitcroft			}
14798905a67cSAndy Whitcroft
14809c0ca6f9SAndy Whitcroft		}
14819c0ca6f9SAndy Whitcroft
148200df344fSAndy Whitcroft#
148300df344fSAndy Whitcroft# Checks which may be anchored in the context.
148400df344fSAndy Whitcroft#
148500df344fSAndy Whitcroft
148600df344fSAndy Whitcroft# Check for switch () and associated case and default
148700df344fSAndy Whitcroft# statements should be at the same indent.
148800df344fSAndy Whitcroft		if ($line=~/\bswitch\s*\(.*\)/) {
148900df344fSAndy Whitcroft			my $err = '';
149000df344fSAndy Whitcroft			my $sep = '';
149100df344fSAndy Whitcroft			my @ctx = ctx_block_outer($linenr, $realcnt);
149200df344fSAndy Whitcroft			shift(@ctx);
149300df344fSAndy Whitcroft			for my $ctx (@ctx) {
149400df344fSAndy Whitcroft				my ($clen, $cindent) = line_stats($ctx);
149500df344fSAndy Whitcroft				if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
149600df344fSAndy Whitcroft							$indent != $cindent) {
149700df344fSAndy Whitcroft					$err .= "$sep$ctx\n";
149800df344fSAndy Whitcroft					$sep = '';
149900df344fSAndy Whitcroft				} else {
150000df344fSAndy Whitcroft					$sep = "[...]\n";
150100df344fSAndy Whitcroft				}
150200df344fSAndy Whitcroft			}
150300df344fSAndy Whitcroft			if ($err ne '') {
15049c0ca6f9SAndy Whitcroft				ERROR("switch and case should be at the same indent\n$hereline$err");
1505de7d4f0eSAndy Whitcroft			}
1506de7d4f0eSAndy Whitcroft		}
1507de7d4f0eSAndy Whitcroft
1508de7d4f0eSAndy Whitcroft# if/while/etc brace do not go on next line, unless defining a do while loop,
1509de7d4f0eSAndy Whitcroft# or if that brace on the next line is for something else
1510c45dcabdSAndy Whitcroft		if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
1511773647a0SAndy Whitcroft			my $pre_ctx = "$1$2";
1512773647a0SAndy Whitcroft
15139c0ca6f9SAndy Whitcroft			my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
1514de7d4f0eSAndy Whitcroft			my $ctx_cnt = $realcnt - $#ctx - 1;
1515de7d4f0eSAndy Whitcroft			my $ctx = join("\n", @ctx);
1516de7d4f0eSAndy Whitcroft
1517548596d5SAndy Whitcroft			my $ctx_ln = $linenr;
1518548596d5SAndy Whitcroft			my $ctx_skip = $realcnt;
1519de7d4f0eSAndy Whitcroft
1520548596d5SAndy Whitcroft			while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
1521548596d5SAndy Whitcroft					defined $lines[$ctx_ln - 1] &&
1522548596d5SAndy Whitcroft					$lines[$ctx_ln - 1] =~ /^-/)) {
1523548596d5SAndy Whitcroft				##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
1524548596d5SAndy Whitcroft				$ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
1525773647a0SAndy Whitcroft				$ctx_ln++;
1526773647a0SAndy Whitcroft			}
1527548596d5SAndy Whitcroft
152853210168SAndy Whitcroft			#print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
152953210168SAndy Whitcroft			#print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
1530773647a0SAndy Whitcroft
1531773647a0SAndy Whitcroft			if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln -1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
1532773647a0SAndy Whitcroft				ERROR("that open brace { should be on the previous line\n" .
1533c45dcabdSAndy Whitcroft					"$here\n$ctx\n$lines[$ctx_ln - 1]\n");
153400df344fSAndy Whitcroft			}
1535773647a0SAndy Whitcroft			if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
1536773647a0SAndy Whitcroft			    $ctx =~ /\)\s*\;\s*$/ &&
1537773647a0SAndy Whitcroft			    defined $lines[$ctx_ln - 1])
1538773647a0SAndy Whitcroft			{
15399c0ca6f9SAndy Whitcroft				my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
15409c0ca6f9SAndy Whitcroft				if ($nindent > $indent) {
1541773647a0SAndy Whitcroft					WARN("trailing semicolon indicates no statements, indent implies otherwise\n" .
1542c45dcabdSAndy Whitcroft						"$here\n$ctx\n$lines[$ctx_ln - 1]\n");
15439c0ca6f9SAndy Whitcroft				}
15449c0ca6f9SAndy Whitcroft			}
154500df344fSAndy Whitcroft		}
154600df344fSAndy Whitcroft
15474d001e4dSAndy Whitcroft# Check relative indent for conditionals and blocks.
15484d001e4dSAndy Whitcroft		if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
15494d001e4dSAndy Whitcroft			my ($s, $c) = ($stat, $cond);
15504d001e4dSAndy Whitcroft
15514d001e4dSAndy Whitcroft			substr($s, 0, length($c), '');
15524d001e4dSAndy Whitcroft
15534d001e4dSAndy Whitcroft			# Make sure we remove the line prefixes as we have
15544d001e4dSAndy Whitcroft			# none on the first line, and are going to readd them
15554d001e4dSAndy Whitcroft			# where necessary.
15564d001e4dSAndy Whitcroft			$s =~ s/\n./\n/gs;
15574d001e4dSAndy Whitcroft
15584d001e4dSAndy Whitcroft			# Find out how long the conditional actually is.
15596f779c18SAndy Whitcroft			my @newlines = ($c =~ /\n/gs);
15606f779c18SAndy Whitcroft			my $cond_lines = 1 + $#newlines;
15614d001e4dSAndy Whitcroft
15624d001e4dSAndy Whitcroft			# We want to check the first line inside the block
15634d001e4dSAndy Whitcroft			# starting at the end of the conditional, so remove:
15644d001e4dSAndy Whitcroft			#  1) any blank line termination
15654d001e4dSAndy Whitcroft			#  2) any opening brace { on end of the line
15664d001e4dSAndy Whitcroft			#  3) any do (...) {
15674d001e4dSAndy Whitcroft			my $continuation = 0;
15684d001e4dSAndy Whitcroft			my $check = 0;
15694d001e4dSAndy Whitcroft			$s =~ s/^.*\bdo\b//;
15704d001e4dSAndy Whitcroft			$s =~ s/^\s*{//;
15714d001e4dSAndy Whitcroft			if ($s =~ s/^\s*\\//) {
15724d001e4dSAndy Whitcroft				$continuation = 1;
15734d001e4dSAndy Whitcroft			}
15749bd49efeSAndy Whitcroft			if ($s =~ s/^\s*?\n//) {
15754d001e4dSAndy Whitcroft				$check = 1;
15764d001e4dSAndy Whitcroft				$cond_lines++;
15774d001e4dSAndy Whitcroft			}
15784d001e4dSAndy Whitcroft
15794d001e4dSAndy Whitcroft			# Also ignore a loop construct at the end of a
15804d001e4dSAndy Whitcroft			# preprocessor statement.
15814d001e4dSAndy Whitcroft			if (($prevline =~ /^.\s*#\s*define\s/ ||
15824d001e4dSAndy Whitcroft			    $prevline =~ /\\\s*$/) && $continuation == 0) {
15834d001e4dSAndy Whitcroft				$check = 0;
15844d001e4dSAndy Whitcroft			}
15854d001e4dSAndy Whitcroft
15869bd49efeSAndy Whitcroft			my $cond_ptr = -1;
1587740504c6SAndy Whitcroft			$continuation = 0;
15889bd49efeSAndy Whitcroft			while ($cond_ptr != $cond_lines) {
15899bd49efeSAndy Whitcroft				$cond_ptr = $cond_lines;
15904d001e4dSAndy Whitcroft
1591f16fa28fSAndy Whitcroft				# If we see an #else/#elif then the code
1592f16fa28fSAndy Whitcroft				# is not linear.
1593f16fa28fSAndy Whitcroft				if ($s =~ /^\s*\#\s*(?:else|elif)/) {
1594f16fa28fSAndy Whitcroft					$check = 0;
1595f16fa28fSAndy Whitcroft				}
1596f16fa28fSAndy Whitcroft
15979bd49efeSAndy Whitcroft				# Ignore:
15989bd49efeSAndy Whitcroft				#  1) blank lines, they should be at 0,
15999bd49efeSAndy Whitcroft				#  2) preprocessor lines, and
16009bd49efeSAndy Whitcroft				#  3) labels.
1601740504c6SAndy Whitcroft				if ($continuation ||
1602740504c6SAndy Whitcroft				    $s =~ /^\s*?\n/ ||
16039bd49efeSAndy Whitcroft				    $s =~ /^\s*#\s*?/ ||
16049bd49efeSAndy Whitcroft				    $s =~ /^\s*$Ident\s*:/) {
1605740504c6SAndy Whitcroft					$continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
160630dad6ebSAndy Whitcroft					if ($s =~ s/^.*?\n//) {
16079bd49efeSAndy Whitcroft						$cond_lines++;
16089bd49efeSAndy Whitcroft					}
16094d001e4dSAndy Whitcroft				}
161030dad6ebSAndy Whitcroft			}
16114d001e4dSAndy Whitcroft
16124d001e4dSAndy Whitcroft			my (undef, $sindent) = line_stats("+" . $s);
16134d001e4dSAndy Whitcroft			my $stat_real = raw_line($linenr, $cond_lines);
16144d001e4dSAndy Whitcroft
16154d001e4dSAndy Whitcroft			# Check if either of these lines are modified, else
16164d001e4dSAndy Whitcroft			# this is not this patch's fault.
16174d001e4dSAndy Whitcroft			if (!defined($stat_real) ||
16184d001e4dSAndy Whitcroft			    $stat !~ /^\+/ && $stat_real !~ /^\+/) {
16194d001e4dSAndy Whitcroft				$check = 0;
16204d001e4dSAndy Whitcroft			}
16214d001e4dSAndy Whitcroft			if (defined($stat_real) && $cond_lines > 1) {
16224d001e4dSAndy Whitcroft				$stat_real = "[...]\n$stat_real";
16234d001e4dSAndy Whitcroft			}
16244d001e4dSAndy Whitcroft
16259bd49efeSAndy 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";
16264d001e4dSAndy Whitcroft
16274d001e4dSAndy Whitcroft			if ($check && (($sindent % 8) != 0 ||
16284d001e4dSAndy Whitcroft			    ($sindent <= $indent && $s ne ''))) {
16294d001e4dSAndy Whitcroft				WARN("suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
16304d001e4dSAndy Whitcroft			}
16314d001e4dSAndy Whitcroft		}
16324d001e4dSAndy Whitcroft
16336c72ffaaSAndy Whitcroft		# Track the 'values' across context and added lines.
16346c72ffaaSAndy Whitcroft		my $opline = $line; $opline =~ s/^./ /;
16351f65f947SAndy Whitcroft		my ($curr_values, $curr_vars) =
16361f65f947SAndy Whitcroft				annotate_values($opline . "\n", $prev_values);
16376c72ffaaSAndy Whitcroft		$curr_values = $prev_values . $curr_values;
1638c2fdda0dSAndy Whitcroft		if ($dbg_values) {
1639c2fdda0dSAndy Whitcroft			my $outline = $opline; $outline =~ s/\t/ /g;
1640cf655043SAndy Whitcroft			print "$linenr > .$outline\n";
1641cf655043SAndy Whitcroft			print "$linenr > $curr_values\n";
16421f65f947SAndy Whitcroft			print "$linenr >  $curr_vars\n";
1643c2fdda0dSAndy Whitcroft		}
16446c72ffaaSAndy Whitcroft		$prev_values = substr($curr_values, -1);
16456c72ffaaSAndy Whitcroft
164600df344fSAndy Whitcroft#ignore lines not being added
164700df344fSAndy Whitcroft		if ($line=~/^[^\+]/) {next;}
164800df344fSAndy Whitcroft
1649653d4876SAndy Whitcroft# TEST: allow direct testing of the type matcher.
16507429c690SAndy Whitcroft		if ($dbg_type) {
16517429c690SAndy Whitcroft			if ($line =~ /^.\s*$Declare\s*$/) {
16527429c690SAndy Whitcroft				ERROR("TEST: is type\n" . $herecurr);
16537429c690SAndy Whitcroft			} elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
16547429c690SAndy Whitcroft				ERROR("TEST: is not type ($1 is)\n". $herecurr);
16557429c690SAndy Whitcroft			}
1656653d4876SAndy Whitcroft			next;
1657653d4876SAndy Whitcroft		}
1658a1ef277eSAndy Whitcroft# TEST: allow direct testing of the attribute matcher.
1659a1ef277eSAndy Whitcroft		if ($dbg_attr) {
16609360b0e5SAndy Whitcroft			if ($line =~ /^.\s*$Modifier\s*$/) {
1661a1ef277eSAndy Whitcroft				ERROR("TEST: is attr\n" . $herecurr);
16629360b0e5SAndy Whitcroft			} elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
1663a1ef277eSAndy Whitcroft				ERROR("TEST: is not attr ($1 is)\n". $herecurr);
1664a1ef277eSAndy Whitcroft			}
1665a1ef277eSAndy Whitcroft			next;
1666a1ef277eSAndy Whitcroft		}
1667653d4876SAndy Whitcroft
1668f0a594c1SAndy Whitcroft# check for initialisation to aggregates open brace on the next line
1669f0a594c1SAndy Whitcroft		if ($prevline =~ /$Declare\s*$Ident\s*=\s*$/ &&
1670f0a594c1SAndy Whitcroft		    $line =~ /^.\s*{/) {
1671773647a0SAndy Whitcroft			ERROR("that open brace { should be on the previous line\n" . $hereprev);
1672f0a594c1SAndy Whitcroft		}
1673f0a594c1SAndy Whitcroft
167400df344fSAndy Whitcroft#
167500df344fSAndy Whitcroft# Checks which are anchored on the added line.
167600df344fSAndy Whitcroft#
167700df344fSAndy Whitcroft
1678653d4876SAndy Whitcroft# check for malformed paths in #include statements (uses RAW line)
1679c45dcabdSAndy Whitcroft		if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
1680653d4876SAndy Whitcroft			my $path = $1;
1681653d4876SAndy Whitcroft			if ($path =~ m{//}) {
1682de7d4f0eSAndy Whitcroft				ERROR("malformed #include filename\n" .
1683de7d4f0eSAndy Whitcroft					$herecurr);
1684653d4876SAndy Whitcroft			}
1685653d4876SAndy Whitcroft		}
1686653d4876SAndy Whitcroft
168700df344fSAndy Whitcroft# no C99 // comments
168800df344fSAndy Whitcroft		if ($line =~ m{//}) {
1689de7d4f0eSAndy Whitcroft			ERROR("do not use C99 // comments\n" . $herecurr);
169000df344fSAndy Whitcroft		}
169100df344fSAndy Whitcroft		# Remove C99 comments.
16920a920b5bSAndy Whitcroft		$line =~ s@//.*@@;
16936c72ffaaSAndy Whitcroft		$opline =~ s@//.*@@;
16940a920b5bSAndy Whitcroft
16950a920b5bSAndy Whitcroft#EXPORT_SYMBOL should immediately follow its function closing }.
1696653d4876SAndy Whitcroft		if (($line =~ /EXPORT_SYMBOL.*\((.*)\)/) ||
1697653d4876SAndy Whitcroft		    ($line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
1698653d4876SAndy Whitcroft			my $name = $1;
169948012058SAndy Whitcroft			if ($prevline !~ /(?:
170048012058SAndy Whitcroft				^.}|
170148012058SAndy Whitcroft				^.DEFINE_$Ident\(\Q$name\E\)|
170248012058SAndy Whitcroft				^.DECLARE_$Ident\(\Q$name\E\)|
170348012058SAndy Whitcroft				^.LIST_HEAD\(\Q$name\E\)|
170448012058SAndy Whitcroft				^.$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
170548012058SAndy Whitcroft				\b\Q$name\E(?:\s+$Attribute)?\s*(?:;|=|\[)
170648012058SAndy Whitcroft			    )/x) {
1707de7d4f0eSAndy Whitcroft				WARN("EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
17080a920b5bSAndy Whitcroft			}
17090a920b5bSAndy Whitcroft		}
17100a920b5bSAndy Whitcroft
1711f0a594c1SAndy Whitcroft# check for external initialisers.
1712c45dcabdSAndy Whitcroft		if ($line =~ /^.$Type\s*$Ident\s*(?:\s+$Modifier)*\s*=\s*(0|NULL|false)\s*;/) {
1713f0a594c1SAndy Whitcroft			ERROR("do not initialise externals to 0 or NULL\n" .
1714f0a594c1SAndy Whitcroft				$herecurr);
1715f0a594c1SAndy Whitcroft		}
17160a920b5bSAndy Whitcroft# check for static initialisers.
17172d1bafd7SAndy Whitcroft		if ($line =~ /\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
1718de7d4f0eSAndy Whitcroft			ERROR("do not initialise statics to 0 or NULL\n" .
1719de7d4f0eSAndy Whitcroft				$herecurr);
17200a920b5bSAndy Whitcroft		}
17210a920b5bSAndy Whitcroft
1722653d4876SAndy Whitcroft# check for new typedefs, only function parameters and sparse annotations
1723653d4876SAndy Whitcroft# make sense.
1724653d4876SAndy Whitcroft		if ($line =~ /\btypedef\s/ &&
17258054576dSAndy Whitcroft		    $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
1726c45dcabdSAndy Whitcroft		    $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
17278ed22cadSAndy Whitcroft		    $line !~ /\b$typeTypedefs\b/ &&
1728653d4876SAndy Whitcroft		    $line !~ /\b__bitwise(?:__|)\b/) {
1729de7d4f0eSAndy Whitcroft			WARN("do not add new typedefs\n" . $herecurr);
17300a920b5bSAndy Whitcroft		}
17310a920b5bSAndy Whitcroft
17320a920b5bSAndy Whitcroft# * goes on variable not on type
173365863862SAndy Whitcroft		# (char*[ const])
173400ef4eceSAndy Whitcroft		if ($line =~ m{\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\)}) {
173565863862SAndy Whitcroft			my ($from, $to) = ($1, $1);
1736d8aaf121SAndy Whitcroft
173765863862SAndy Whitcroft			# Should start with a space.
173865863862SAndy Whitcroft			$to =~ s/^(\S)/ $1/;
173965863862SAndy Whitcroft			# Should not end with a space.
174065863862SAndy Whitcroft			$to =~ s/\s+$//;
174165863862SAndy Whitcroft			# '*'s should not have spaces between.
1742f9a0b3d1SAndy Whitcroft			while ($to =~ s/\*\s+\*/\*\*/) {
174365863862SAndy Whitcroft			}
1744d8aaf121SAndy Whitcroft
174565863862SAndy Whitcroft			#print "from<$from> to<$to>\n";
174665863862SAndy Whitcroft			if ($from ne $to) {
174765863862SAndy Whitcroft				ERROR("\"(foo$from)\" should be \"(foo$to)\"\n" .  $herecurr);
174865863862SAndy Whitcroft			}
174900ef4eceSAndy Whitcroft		} elsif ($line =~ m{\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident)}) {
175065863862SAndy Whitcroft			my ($from, $to, $ident) = ($1, $1, $2);
1751d8aaf121SAndy Whitcroft
175265863862SAndy Whitcroft			# Should start with a space.
175365863862SAndy Whitcroft			$to =~ s/^(\S)/ $1/;
175465863862SAndy Whitcroft			# Should not end with a space.
175565863862SAndy Whitcroft			$to =~ s/\s+$//;
175665863862SAndy Whitcroft			# '*'s should not have spaces between.
1757f9a0b3d1SAndy Whitcroft			while ($to =~ s/\*\s+\*/\*\*/) {
175865863862SAndy Whitcroft			}
175965863862SAndy Whitcroft			# Modifiers should have spaces.
176065863862SAndy Whitcroft			$to =~ s/(\b$Modifier$)/$1 /;
176165863862SAndy Whitcroft
1762667026e7SAndy Whitcroft			#print "from<$from> to<$to> ident<$ident>\n";
1763667026e7SAndy Whitcroft			if ($from ne $to && $ident !~ /^$Modifier$/) {
176465863862SAndy Whitcroft				ERROR("\"foo${from}bar\" should be \"foo${to}bar\"\n" .  $herecurr);
176565863862SAndy Whitcroft			}
17660a920b5bSAndy Whitcroft		}
17670a920b5bSAndy Whitcroft
17680a920b5bSAndy Whitcroft# # no BUG() or BUG_ON()
17690a920b5bSAndy Whitcroft# 		if ($line =~ /\b(BUG|BUG_ON)\b/) {
17700a920b5bSAndy Whitcroft# 			print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
17710a920b5bSAndy Whitcroft# 			print "$herecurr";
17720a920b5bSAndy Whitcroft# 			$clean = 0;
17730a920b5bSAndy Whitcroft# 		}
17740a920b5bSAndy Whitcroft
17758905a67cSAndy Whitcroft		if ($line =~ /\bLINUX_VERSION_CODE\b/) {
1776c2fdda0dSAndy Whitcroft			WARN("LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
17778905a67cSAndy Whitcroft		}
17788905a67cSAndy Whitcroft
177900df344fSAndy Whitcroft# printk should use KERN_* levels.  Note that follow on printk's on the
178000df344fSAndy Whitcroft# same line do not need a level, so we use the current block context
178100df344fSAndy Whitcroft# to try and find and validate the current printk.  In summary the current
178200df344fSAndy Whitcroft# printk includes all preceeding printk's which have no newline on the end.
178300df344fSAndy Whitcroft# we assume the first bad printk is the one to report.
1784f0a594c1SAndy Whitcroft		if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
178500df344fSAndy Whitcroft			my $ok = 0;
178600df344fSAndy Whitcroft			for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
178700df344fSAndy Whitcroft				#print "CHECK<$lines[$ln - 1]\n";
178800df344fSAndy Whitcroft				# we have a preceeding printk if it ends
178900df344fSAndy Whitcroft				# with "\n" ignore it, else it is to blame
179000df344fSAndy Whitcroft				if ($lines[$ln - 1] =~ m{\bprintk\(}) {
179100df344fSAndy Whitcroft					if ($rawlines[$ln - 1] !~ m{\\n"}) {
179200df344fSAndy Whitcroft						$ok = 1;
179300df344fSAndy Whitcroft					}
179400df344fSAndy Whitcroft					last;
179500df344fSAndy Whitcroft				}
179600df344fSAndy Whitcroft			}
179700df344fSAndy Whitcroft			if ($ok == 0) {
1798de7d4f0eSAndy Whitcroft				WARN("printk() should include KERN_ facility level\n" . $herecurr);
17990a920b5bSAndy Whitcroft			}
180000df344fSAndy Whitcroft		}
18010a920b5bSAndy Whitcroft
1802653d4876SAndy Whitcroft# function brace can't be on same line, except for #defines of do while,
1803653d4876SAndy Whitcroft# or if closed on same line
1804c45dcabdSAndy Whitcroft		if (($line=~/$Type\s*$Ident\(.*\).*\s{/) and
1805c45dcabdSAndy Whitcroft		    !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) {
1806de7d4f0eSAndy Whitcroft			ERROR("open brace '{' following function declarations go on the next line\n" . $herecurr);
18070a920b5bSAndy Whitcroft		}
1808653d4876SAndy Whitcroft
18098905a67cSAndy Whitcroft# open braces for enum, union and struct go on the same line.
18108905a67cSAndy Whitcroft		if ($line =~ /^.\s*{/ &&
18118905a67cSAndy Whitcroft		    $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
18128905a67cSAndy Whitcroft			ERROR("open brace '{' following $1 go on the same line\n" . $hereprev);
18138905a67cSAndy Whitcroft		}
18148905a67cSAndy Whitcroft
18158d31cfceSAndy Whitcroft# check for spacing round square brackets; allowed:
18168d31cfceSAndy Whitcroft#  1. with a type on the left -- int [] a;
1817fe2a7dbcSAndy Whitcroft#  2. at the beginning of a line for slice initialisers -- [0...10] = 5,
1818fe2a7dbcSAndy Whitcroft#  3. inside a curly brace -- = { [0...10] = 5 }
18198d31cfceSAndy Whitcroft		while ($line =~ /(.*?\s)\[/g) {
18208d31cfceSAndy Whitcroft			my ($where, $prefix) = ($-[1], $1);
18218d31cfceSAndy Whitcroft			if ($prefix !~ /$Type\s+$/ &&
1822fe2a7dbcSAndy Whitcroft			    ($where != 0 || $prefix !~ /^.\s+$/) &&
1823fe2a7dbcSAndy Whitcroft			    $prefix !~ /{\s+$/) {
18248d31cfceSAndy Whitcroft				ERROR("space prohibited before open square bracket '['\n" . $herecurr);
18258d31cfceSAndy Whitcroft			}
18268d31cfceSAndy Whitcroft		}
18278d31cfceSAndy Whitcroft
1828f0a594c1SAndy Whitcroft# check for spaces between functions and their parentheses.
18296c72ffaaSAndy Whitcroft		while ($line =~ /($Ident)\s+\(/g) {
1830c2fdda0dSAndy Whitcroft			my $name = $1;
1831773647a0SAndy Whitcroft			my $ctx_before = substr($line, 0, $-[1]);
1832773647a0SAndy Whitcroft			my $ctx = "$ctx_before$name";
1833c2fdda0dSAndy Whitcroft
1834c2fdda0dSAndy Whitcroft			# Ignore those directives where spaces _are_ permitted.
1835773647a0SAndy Whitcroft			if ($name =~ /^(?:
1836773647a0SAndy Whitcroft				if|for|while|switch|return|case|
1837773647a0SAndy Whitcroft				volatile|__volatile__|
1838773647a0SAndy Whitcroft				__attribute__|format|__extension__|
1839773647a0SAndy Whitcroft				asm|__asm__)$/x)
1840773647a0SAndy Whitcroft			{
1841c2fdda0dSAndy Whitcroft
1842c2fdda0dSAndy Whitcroft			# cpp #define statements have non-optional spaces, ie
1843c2fdda0dSAndy Whitcroft			# if there is a space between the name and the open
1844c2fdda0dSAndy Whitcroft			# parenthesis it is simply not a parameter group.
1845c45dcabdSAndy Whitcroft			} elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
1846773647a0SAndy Whitcroft
1847773647a0SAndy Whitcroft			# cpp #elif statement condition may start with a (
1848c45dcabdSAndy Whitcroft			} elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
1849c2fdda0dSAndy Whitcroft
1850c2fdda0dSAndy Whitcroft			# If this whole things ends with a type its most
1851c2fdda0dSAndy Whitcroft			# likely a typedef for a function.
1852773647a0SAndy Whitcroft			} elsif ($ctx =~ /$Type$/) {
1853c2fdda0dSAndy Whitcroft
1854c2fdda0dSAndy Whitcroft			} else {
1855773647a0SAndy Whitcroft				WARN("space prohibited between function name and open parenthesis '('\n" . $herecurr);
1856f0a594c1SAndy Whitcroft			}
18576c72ffaaSAndy Whitcroft		}
1858653d4876SAndy Whitcroft# Check operator spacing.
18590a920b5bSAndy Whitcroft		if (!($line=~/\#\s*include/)) {
18609c0ca6f9SAndy Whitcroft			my $ops = qr{
18619c0ca6f9SAndy Whitcroft				<<=|>>=|<=|>=|==|!=|
18629c0ca6f9SAndy Whitcroft				\+=|-=|\*=|\/=|%=|\^=|\|=|&=|
18639c0ca6f9SAndy Whitcroft				=>|->|<<|>>|<|>|=|!|~|
18641f65f947SAndy Whitcroft				&&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
18651f65f947SAndy Whitcroft				\?|:
18669c0ca6f9SAndy Whitcroft			}x;
1867cf655043SAndy Whitcroft			my @elements = split(/($ops|;)/, $opline);
186800df344fSAndy Whitcroft			my $off = 0;
18696c72ffaaSAndy Whitcroft
18706c72ffaaSAndy Whitcroft			my $blank = copy_spacing($opline);
18716c72ffaaSAndy Whitcroft
18720a920b5bSAndy Whitcroft			for (my $n = 0; $n < $#elements; $n += 2) {
18734a0df2efSAndy Whitcroft				$off += length($elements[$n]);
18744a0df2efSAndy Whitcroft
1875773647a0SAndy Whitcroft				# Pick up the preceeding and succeeding characters.
1876773647a0SAndy Whitcroft				my $ca = substr($opline, 0, $off);
1877773647a0SAndy Whitcroft				my $cc = '';
1878773647a0SAndy Whitcroft				if (length($opline) >= ($off + length($elements[$n + 1]))) {
1879773647a0SAndy Whitcroft					$cc = substr($opline, $off + length($elements[$n + 1]));
1880773647a0SAndy Whitcroft				}
1881773647a0SAndy Whitcroft				my $cb = "$ca$;$cc";
1882773647a0SAndy Whitcroft
18834a0df2efSAndy Whitcroft				my $a = '';
18844a0df2efSAndy Whitcroft				$a = 'V' if ($elements[$n] ne '');
18854a0df2efSAndy Whitcroft				$a = 'W' if ($elements[$n] =~ /\s$/);
1886cf655043SAndy Whitcroft				$a = 'C' if ($elements[$n] =~ /$;$/);
18874a0df2efSAndy Whitcroft				$a = 'B' if ($elements[$n] =~ /(\[|\()$/);
18884a0df2efSAndy Whitcroft				$a = 'O' if ($elements[$n] eq '');
1889773647a0SAndy Whitcroft				$a = 'E' if ($ca =~ /^\s*$/);
18904a0df2efSAndy Whitcroft
18910a920b5bSAndy Whitcroft				my $op = $elements[$n + 1];
18924a0df2efSAndy Whitcroft
18934a0df2efSAndy Whitcroft				my $c = '';
18940a920b5bSAndy Whitcroft				if (defined $elements[$n + 2]) {
18954a0df2efSAndy Whitcroft					$c = 'V' if ($elements[$n + 2] ne '');
18964a0df2efSAndy Whitcroft					$c = 'W' if ($elements[$n + 2] =~ /^\s/);
1897cf655043SAndy Whitcroft					$c = 'C' if ($elements[$n + 2] =~ /^$;/);
18984a0df2efSAndy Whitcroft					$c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
18994a0df2efSAndy Whitcroft					$c = 'O' if ($elements[$n + 2] eq '');
19008b1b3378SAndy Whitcroft					$c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
19014a0df2efSAndy Whitcroft				} else {
19024a0df2efSAndy Whitcroft					$c = 'E';
19030a920b5bSAndy Whitcroft				}
19040a920b5bSAndy Whitcroft
19054a0df2efSAndy Whitcroft				my $ctx = "${a}x${c}";
19064a0df2efSAndy Whitcroft
19074a0df2efSAndy Whitcroft				my $at = "(ctx:$ctx)";
19084a0df2efSAndy Whitcroft
19096c72ffaaSAndy Whitcroft				my $ptr = substr($blank, 0, $off) . "^";
1910de7d4f0eSAndy Whitcroft				my $hereptr = "$hereline$ptr\n";
19110a920b5bSAndy Whitcroft
191274048ed8SAndy Whitcroft				# Pull out the value of this operator.
19136c72ffaaSAndy Whitcroft				my $op_type = substr($curr_values, $off + 1, 1);
19140a920b5bSAndy Whitcroft
19151f65f947SAndy Whitcroft				# Get the full operator variant.
19161f65f947SAndy Whitcroft				my $opv = $op . substr($curr_vars, $off, 1);
19171f65f947SAndy Whitcroft
191813214adfSAndy Whitcroft				# Ignore operators passed as parameters.
191913214adfSAndy Whitcroft				if ($op_type ne 'V' &&
192013214adfSAndy Whitcroft				    $ca =~ /\s$/ && $cc =~ /^\s*,/) {
192113214adfSAndy Whitcroft
1922cf655043SAndy Whitcroft#				# Ignore comments
1923cf655043SAndy Whitcroft#				} elsif ($op =~ /^$;+$/) {
192413214adfSAndy Whitcroft
1925d8aaf121SAndy Whitcroft				# ; should have either the end of line or a space or \ after it
192613214adfSAndy Whitcroft				} elsif ($op eq ';') {
1927cf655043SAndy Whitcroft					if ($ctx !~ /.x[WEBC]/ &&
1928cf655043SAndy Whitcroft					    $cc !~ /^\\/ && $cc !~ /^;/) {
1929773647a0SAndy Whitcroft						ERROR("space required after that '$op' $at\n" . $hereptr);
1930d8aaf121SAndy Whitcroft					}
1931d8aaf121SAndy Whitcroft
1932d8aaf121SAndy Whitcroft				# // is a comment
1933d8aaf121SAndy Whitcroft				} elsif ($op eq '//') {
19340a920b5bSAndy Whitcroft
19351f65f947SAndy Whitcroft				# No spaces for:
19361f65f947SAndy Whitcroft				#   ->
19371f65f947SAndy Whitcroft				#   :   when part of a bitfield
19381f65f947SAndy Whitcroft				} elsif ($op eq '->' || $opv eq ':B') {
19394a0df2efSAndy Whitcroft					if ($ctx =~ /Wx.|.xW/) {
1940773647a0SAndy Whitcroft						ERROR("spaces prohibited around that '$op' $at\n" . $hereptr);
19410a920b5bSAndy Whitcroft					}
19420a920b5bSAndy Whitcroft
19430a920b5bSAndy Whitcroft				# , must have a space on the right.
19440a920b5bSAndy Whitcroft				} elsif ($op eq ',') {
1945cf655043SAndy Whitcroft					if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
1946773647a0SAndy Whitcroft						ERROR("space required after that '$op' $at\n" . $hereptr);
19470a920b5bSAndy Whitcroft					}
19480a920b5bSAndy Whitcroft
19499c0ca6f9SAndy Whitcroft				# '*' as part of a type definition -- reported already.
195074048ed8SAndy Whitcroft				} elsif ($opv eq '*_') {
19519c0ca6f9SAndy Whitcroft					#warn "'*' is part of type\n";
19529c0ca6f9SAndy Whitcroft
19539c0ca6f9SAndy Whitcroft				# unary operators should have a space before and
19549c0ca6f9SAndy Whitcroft				# none after.  May be left adjacent to another
19559c0ca6f9SAndy Whitcroft				# unary operator, or a cast
19569c0ca6f9SAndy Whitcroft				} elsif ($op eq '!' || $op eq '~' ||
195774048ed8SAndy Whitcroft					 $opv eq '*U' || $opv eq '-U' ||
19580d413866SAndy Whitcroft					 $opv eq '&U' || $opv eq '&&U') {
1959cf655043SAndy Whitcroft					if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
1960773647a0SAndy Whitcroft						ERROR("space required before that '$op' $at\n" . $hereptr);
19610a920b5bSAndy Whitcroft					}
1962a3340b35SAndy Whitcroft					if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
1963171ae1a4SAndy Whitcroft						# A unary '*' may be const
1964171ae1a4SAndy Whitcroft
1965171ae1a4SAndy Whitcroft					} elsif ($ctx =~ /.xW/) {
1966fb9e9096SAndy Whitcroft						ERROR("space prohibited after that '$op' $at\n" . $hereptr);
19670a920b5bSAndy Whitcroft					}
19680a920b5bSAndy Whitcroft
19690a920b5bSAndy Whitcroft				# unary ++ and unary -- are allowed no space on one side.
19700a920b5bSAndy Whitcroft				} elsif ($op eq '++' or $op eq '--') {
1971773647a0SAndy Whitcroft					if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
1972773647a0SAndy Whitcroft						ERROR("space required one side of that '$op' $at\n" . $hereptr);
19730a920b5bSAndy Whitcroft					}
1974773647a0SAndy Whitcroft					if ($ctx =~ /Wx[BE]/ ||
1975773647a0SAndy Whitcroft					    ($ctx =~ /Wx./ && $cc =~ /^;/)) {
1976773647a0SAndy Whitcroft						ERROR("space prohibited before that '$op' $at\n" . $hereptr);
1977653d4876SAndy Whitcroft					}
1978773647a0SAndy Whitcroft					if ($ctx =~ /ExW/) {
1979773647a0SAndy Whitcroft						ERROR("space prohibited after that '$op' $at\n" . $hereptr);
1980773647a0SAndy Whitcroft					}
1981773647a0SAndy Whitcroft
19820a920b5bSAndy Whitcroft
19830a920b5bSAndy Whitcroft				# << and >> may either have or not have spaces both sides
19849c0ca6f9SAndy Whitcroft				} elsif ($op eq '<<' or $op eq '>>' or
19859c0ca6f9SAndy Whitcroft					 $op eq '&' or $op eq '^' or $op eq '|' or
19869c0ca6f9SAndy Whitcroft					 $op eq '+' or $op eq '-' or
1987c2fdda0dSAndy Whitcroft					 $op eq '*' or $op eq '/' or
1988c2fdda0dSAndy Whitcroft					 $op eq '%')
19890a920b5bSAndy Whitcroft				{
1990773647a0SAndy Whitcroft					if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
1991de7d4f0eSAndy Whitcroft						ERROR("need consistent spacing around '$op' $at\n" .
1992de7d4f0eSAndy Whitcroft							$hereptr);
19930a920b5bSAndy Whitcroft					}
19940a920b5bSAndy Whitcroft
19951f65f947SAndy Whitcroft				# A colon needs no spaces before when it is
19961f65f947SAndy Whitcroft				# terminating a case value or a label.
19971f65f947SAndy Whitcroft				} elsif ($opv eq ':C' || $opv eq ':L') {
19981f65f947SAndy Whitcroft					if ($ctx =~ /Wx./) {
19991f65f947SAndy Whitcroft						ERROR("space prohibited before that '$op' $at\n" . $hereptr);
20001f65f947SAndy Whitcroft					}
20011f65f947SAndy Whitcroft
20020a920b5bSAndy Whitcroft				# All the others need spaces both sides.
2003cf655043SAndy Whitcroft				} elsif ($ctx !~ /[EWC]x[CWE]/) {
20041f65f947SAndy Whitcroft					my $ok = 0;
20051f65f947SAndy Whitcroft
200622f2a2efSAndy Whitcroft					# Ignore email addresses <foo@bar>
20071f65f947SAndy Whitcroft					if (($op eq '<' &&
20081f65f947SAndy Whitcroft					     $cc =~ /^\S+\@\S+>/) ||
20091f65f947SAndy Whitcroft					    ($op eq '>' &&
20101f65f947SAndy Whitcroft					     $ca =~ /<\S+\@\S+$/))
20111f65f947SAndy Whitcroft					{
20121f65f947SAndy Whitcroft					    	$ok = 1;
20131f65f947SAndy Whitcroft					}
20141f65f947SAndy Whitcroft
20151f65f947SAndy Whitcroft					# Ignore ?:
20161f65f947SAndy Whitcroft					if (($opv eq ':O' && $ca =~ /\?$/) ||
20171f65f947SAndy Whitcroft					    ($op eq '?' && $cc =~ /^:/)) {
20181f65f947SAndy Whitcroft					    	$ok = 1;
20191f65f947SAndy Whitcroft					}
20201f65f947SAndy Whitcroft
20211f65f947SAndy Whitcroft					if ($ok == 0) {
2022773647a0SAndy Whitcroft						ERROR("spaces required around that '$op' $at\n" . $hereptr);
20230a920b5bSAndy Whitcroft					}
202422f2a2efSAndy Whitcroft				}
20254a0df2efSAndy Whitcroft				$off += length($elements[$n + 1]);
20260a920b5bSAndy Whitcroft			}
20270a920b5bSAndy Whitcroft		}
20280a920b5bSAndy Whitcroft
2029f0a594c1SAndy Whitcroft# check for multiple assignments
2030f0a594c1SAndy Whitcroft		if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
20316c72ffaaSAndy Whitcroft			CHK("multiple assignments should be avoided\n" . $herecurr);
2032f0a594c1SAndy Whitcroft		}
2033f0a594c1SAndy Whitcroft
203422f2a2efSAndy Whitcroft## # check for multiple declarations, allowing for a function declaration
203522f2a2efSAndy Whitcroft## # continuation.
203622f2a2efSAndy Whitcroft## 		if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
203722f2a2efSAndy Whitcroft## 		    $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
203822f2a2efSAndy Whitcroft##
203922f2a2efSAndy Whitcroft## 			# Remove any bracketed sections to ensure we do not
204022f2a2efSAndy Whitcroft## 			# falsly report the parameters of functions.
204122f2a2efSAndy Whitcroft## 			my $ln = $line;
204222f2a2efSAndy Whitcroft## 			while ($ln =~ s/\([^\(\)]*\)//g) {
204322f2a2efSAndy Whitcroft## 			}
204422f2a2efSAndy Whitcroft## 			if ($ln =~ /,/) {
204522f2a2efSAndy Whitcroft## 				WARN("declaring multiple variables together should be avoided\n" . $herecurr);
204622f2a2efSAndy Whitcroft## 			}
204722f2a2efSAndy Whitcroft## 		}
2048f0a594c1SAndy Whitcroft
20490a920b5bSAndy Whitcroft#need space before brace following if, while, etc
205022f2a2efSAndy Whitcroft		if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
205122f2a2efSAndy Whitcroft		    $line =~ /do{/) {
2052773647a0SAndy Whitcroft			ERROR("space required before the open brace '{'\n" . $herecurr);
2053de7d4f0eSAndy Whitcroft		}
2054de7d4f0eSAndy Whitcroft
2055de7d4f0eSAndy Whitcroft# closing brace should have a space following it when it has anything
2056de7d4f0eSAndy Whitcroft# on the line
2057de7d4f0eSAndy Whitcroft		if ($line =~ /}(?!(?:,|;|\)))\S/) {
2058773647a0SAndy Whitcroft			ERROR("space required after that close brace '}'\n" . $herecurr);
20590a920b5bSAndy Whitcroft		}
20600a920b5bSAndy Whitcroft
206122f2a2efSAndy Whitcroft# check spacing on square brackets
206222f2a2efSAndy Whitcroft		if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
2063773647a0SAndy Whitcroft			ERROR("space prohibited after that open square bracket '['\n" . $herecurr);
206422f2a2efSAndy Whitcroft		}
206522f2a2efSAndy Whitcroft		if ($line =~ /\s\]/) {
2066773647a0SAndy Whitcroft			ERROR("space prohibited before that close square bracket ']'\n" . $herecurr);
206722f2a2efSAndy Whitcroft		}
206822f2a2efSAndy Whitcroft
2069c45dcabdSAndy Whitcroft# check spacing on parentheses
20709c0ca6f9SAndy Whitcroft		if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
20719c0ca6f9SAndy Whitcroft		    $line !~ /for\s*\(\s+;/) {
2072773647a0SAndy Whitcroft			ERROR("space prohibited after that open parenthesis '('\n" . $herecurr);
207322f2a2efSAndy Whitcroft		}
207413214adfSAndy Whitcroft		if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
2075c45dcabdSAndy Whitcroft		    $line !~ /for\s*\(.*;\s+\)/ &&
2076c45dcabdSAndy Whitcroft		    $line !~ /:\s+\)/) {
2077773647a0SAndy Whitcroft			ERROR("space prohibited before that close parenthesis ')'\n" . $herecurr);
207822f2a2efSAndy Whitcroft		}
207922f2a2efSAndy Whitcroft
20800a920b5bSAndy Whitcroft#goto labels aren't indented, allow a single space however
20814a0df2efSAndy Whitcroft		if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
20820a920b5bSAndy Whitcroft		   !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
2083de7d4f0eSAndy Whitcroft			WARN("labels should not be indented\n" . $herecurr);
20840a920b5bSAndy Whitcroft		}
20850a920b5bSAndy Whitcroft
2086c45dcabdSAndy Whitcroft# Return is not a function.
2087c45dcabdSAndy Whitcroft		if (defined($stat) && $stat =~ /^.\s*return(\s*)(\(.*);/s) {
2088c45dcabdSAndy Whitcroft			my $spacing = $1;
2089c45dcabdSAndy Whitcroft			my $value = $2;
2090c45dcabdSAndy Whitcroft
209186f9d059SAndy Whitcroft			# Flatten any parentheses
2092fee61c47SAndy Whitcroft			$value =~ s/\)\(/\) \(/g;
209363f17f89SAndy Whitcroft			while ($value =~ s/\[[^\{\}]*\]/1/ ||
209463f17f89SAndy Whitcroft			       $value !~ /(?:$Ident|-?$Constant)\s*
209563f17f89SAndy Whitcroft					     $Compare\s*
209663f17f89SAndy Whitcroft					     (?:$Ident|-?$Constant)/x &&
209763f17f89SAndy Whitcroft			       $value =~ s/\([^\(\)]*\)/1/) {
2098c45dcabdSAndy Whitcroft			}
2099c45dcabdSAndy Whitcroft
2100c45dcabdSAndy Whitcroft			if ($value =~ /^(?:$Ident|-?$Constant)$/) {
2101c45dcabdSAndy Whitcroft				ERROR("return is not a function, parentheses are not required\n" . $herecurr);
2102c45dcabdSAndy Whitcroft
2103c45dcabdSAndy Whitcroft			} elsif ($spacing !~ /\s+/) {
2104c45dcabdSAndy Whitcroft				ERROR("space required before the open parenthesis '('\n" . $herecurr);
2105c45dcabdSAndy Whitcroft			}
2106c45dcabdSAndy Whitcroft		}
2107c45dcabdSAndy Whitcroft
21080a920b5bSAndy Whitcroft# Need a space before open parenthesis after if, while etc
21094a0df2efSAndy Whitcroft		if ($line=~/\b(if|while|for|switch)\(/) {
2110773647a0SAndy Whitcroft			ERROR("space required before the open parenthesis '('\n" . $herecurr);
21110a920b5bSAndy Whitcroft		}
21120a920b5bSAndy Whitcroft
2113f5fe35ddSAndy Whitcroft# Check for illegal assignment in if conditional -- and check for trailing
2114f5fe35ddSAndy Whitcroft# statements after the conditional.
2115170d3a22SAndy Whitcroft		if ($line =~ /do\s*(?!{)/) {
2116170d3a22SAndy Whitcroft			my ($stat_next) = ctx_statement_block($line_nr_next,
2117170d3a22SAndy Whitcroft						$remain_next, $off_next);
2118170d3a22SAndy Whitcroft			$stat_next =~ s/\n./\n /g;
2119170d3a22SAndy Whitcroft			##print "stat<$stat> stat_next<$stat_next>\n";
2120170d3a22SAndy Whitcroft
2121170d3a22SAndy Whitcroft			if ($stat_next =~ /^\s*while\b/) {
2122170d3a22SAndy Whitcroft				# If the statement carries leading newlines,
2123170d3a22SAndy Whitcroft				# then count those as offsets.
2124170d3a22SAndy Whitcroft				my ($whitespace) =
2125170d3a22SAndy Whitcroft					($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
2126170d3a22SAndy Whitcroft				my $offset =
2127170d3a22SAndy Whitcroft					statement_rawlines($whitespace) - 1;
2128170d3a22SAndy Whitcroft
2129170d3a22SAndy Whitcroft				$suppress_whiletrailers{$line_nr_next +
2130170d3a22SAndy Whitcroft								$offset} = 1;
2131170d3a22SAndy Whitcroft			}
2132170d3a22SAndy Whitcroft		}
2133170d3a22SAndy Whitcroft		if (!defined $suppress_whiletrailers{$linenr} &&
2134170d3a22SAndy Whitcroft		    $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
2135171ae1a4SAndy Whitcroft			my ($s, $c) = ($stat, $cond);
21368905a67cSAndy Whitcroft
2137b53c8e10SAndy Whitcroft			if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
2138c2fdda0dSAndy Whitcroft				ERROR("do not use assignment in if condition\n" . $herecurr);
21398905a67cSAndy Whitcroft			}
21408905a67cSAndy Whitcroft
21418905a67cSAndy Whitcroft			# Find out what is on the end of the line after the
21428905a67cSAndy Whitcroft			# conditional.
2143773647a0SAndy Whitcroft			substr($s, 0, length($c), '');
21448905a67cSAndy Whitcroft			$s =~ s/\n.*//g;
214513214adfSAndy Whitcroft			$s =~ s/$;//g; 	# Remove any comments
214653210168SAndy Whitcroft			if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
214753210168SAndy Whitcroft			    $c !~ /}\s*while\s*/)
2148773647a0SAndy Whitcroft			{
2149bb44ad39SAndy Whitcroft				# Find out how long the conditional actually is.
2150bb44ad39SAndy Whitcroft				my @newlines = ($c =~ /\n/gs);
2151bb44ad39SAndy Whitcroft				my $cond_lines = 1 + $#newlines;
2152bb44ad39SAndy Whitcroft
2153bb44ad39SAndy Whitcroft				my $stat_real = raw_line($linenr, $cond_lines);
2154bb44ad39SAndy Whitcroft				if (defined($stat_real) && $cond_lines > 1) {
2155bb44ad39SAndy Whitcroft					$stat_real = "[...]\n$stat_real";
2156bb44ad39SAndy Whitcroft				}
2157bb44ad39SAndy Whitcroft
2158bb44ad39SAndy Whitcroft				ERROR("trailing statements should be on next line\n" . $herecurr . $stat_real);
21598905a67cSAndy Whitcroft			}
21608905a67cSAndy Whitcroft		}
21618905a67cSAndy Whitcroft
216213214adfSAndy Whitcroft# Check for bitwise tests written as boolean
216313214adfSAndy Whitcroft		if ($line =~ /
216413214adfSAndy Whitcroft			(?:
216513214adfSAndy Whitcroft				(?:\[|\(|\&\&|\|\|)
216613214adfSAndy Whitcroft				\s*0[xX][0-9]+\s*
216713214adfSAndy Whitcroft				(?:\&\&|\|\|)
216813214adfSAndy Whitcroft			|
216913214adfSAndy Whitcroft				(?:\&\&|\|\|)
217013214adfSAndy Whitcroft				\s*0[xX][0-9]+\s*
217113214adfSAndy Whitcroft				(?:\&\&|\|\||\)|\])
217213214adfSAndy Whitcroft			)/x)
217313214adfSAndy Whitcroft		{
217413214adfSAndy Whitcroft			WARN("boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
217513214adfSAndy Whitcroft		}
217613214adfSAndy Whitcroft
21778905a67cSAndy Whitcroft# if and else should not have general statements after it
217813214adfSAndy Whitcroft		if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
217913214adfSAndy Whitcroft			my $s = $1;
218013214adfSAndy Whitcroft			$s =~ s/$;//g; 	# Remove any comments
218113214adfSAndy Whitcroft			if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
21828905a67cSAndy Whitcroft				ERROR("trailing statements should be on next line\n" . $herecurr);
21830a920b5bSAndy Whitcroft			}
218413214adfSAndy Whitcroft		}
218539667782SAndy Whitcroft# if should not continue a brace
218639667782SAndy Whitcroft		if ($line =~ /}\s*if\b/) {
218739667782SAndy Whitcroft			ERROR("trailing statements should be on next line\n" .
218839667782SAndy Whitcroft				$herecurr);
218939667782SAndy Whitcroft		}
2190a1080bf8SAndy Whitcroft# case and default should not have general statements after them
2191a1080bf8SAndy Whitcroft		if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
2192a1080bf8SAndy Whitcroft		    $line !~ /\G(?:
21933fef12d6SAndy Whitcroft			(?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
2194a1080bf8SAndy Whitcroft			\s*return\s+
2195a1080bf8SAndy Whitcroft		    )/xg)
2196a1080bf8SAndy Whitcroft		{
2197a1080bf8SAndy Whitcroft			ERROR("trailing statements should be on next line\n" . $herecurr);
2198a1080bf8SAndy Whitcroft		}
21990a920b5bSAndy Whitcroft
22000a920b5bSAndy Whitcroft		# Check for }<nl>else {, these must be at the same
22010a920b5bSAndy Whitcroft		# indent level to be relevant to each other.
22020a920b5bSAndy Whitcroft		if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
22030a920b5bSAndy Whitcroft						$previndent == $indent) {
2204de7d4f0eSAndy Whitcroft			ERROR("else should follow close brace '}'\n" . $hereprev);
22050a920b5bSAndy Whitcroft		}
22060a920b5bSAndy Whitcroft
2207c2fdda0dSAndy Whitcroft		if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and
2208c2fdda0dSAndy Whitcroft						$previndent == $indent) {
2209c2fdda0dSAndy Whitcroft			my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
2210c2fdda0dSAndy Whitcroft
2211c2fdda0dSAndy Whitcroft			# Find out what is on the end of the line after the
2212c2fdda0dSAndy Whitcroft			# conditional.
2213773647a0SAndy Whitcroft			substr($s, 0, length($c), '');
2214c2fdda0dSAndy Whitcroft			$s =~ s/\n.*//g;
2215c2fdda0dSAndy Whitcroft
2216c2fdda0dSAndy Whitcroft			if ($s =~ /^\s*;/) {
2217c2fdda0dSAndy Whitcroft				ERROR("while should follow close brace '}'\n" . $hereprev);
2218c2fdda0dSAndy Whitcroft			}
2219c2fdda0dSAndy Whitcroft		}
2220c2fdda0dSAndy Whitcroft
22210a920b5bSAndy Whitcroft#studly caps, commented out until figure out how to distinguish between use of existing and adding new
22220a920b5bSAndy Whitcroft#		if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) {
22230a920b5bSAndy Whitcroft#		    print "No studly caps, use _\n";
22240a920b5bSAndy Whitcroft#		    print "$herecurr";
22250a920b5bSAndy Whitcroft#		    $clean = 0;
22260a920b5bSAndy Whitcroft#		}
22270a920b5bSAndy Whitcroft
22280a920b5bSAndy Whitcroft#no spaces allowed after \ in define
2229c45dcabdSAndy Whitcroft		if ($line=~/\#\s*define.*\\\s$/) {
2230de7d4f0eSAndy Whitcroft			WARN("Whitepspace after \\ makes next lines useless\n" . $herecurr);
22310a920b5bSAndy Whitcroft		}
22320a920b5bSAndy Whitcroft
2233653d4876SAndy Whitcroft#warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
2234c45dcabdSAndy Whitcroft		if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
2235e09dec48SAndy Whitcroft			my $file = "$1.h";
2236e09dec48SAndy Whitcroft			my $checkfile = "include/linux/$file";
2237e09dec48SAndy Whitcroft			if (-f "$root/$checkfile" &&
2238e09dec48SAndy Whitcroft			    $realfile ne $checkfile &&
2239c45dcabdSAndy Whitcroft			    $1 ne 'irq')
2240c45dcabdSAndy Whitcroft			{
2241e09dec48SAndy Whitcroft				if ($realfile =~ m{^arch/}) {
2242e09dec48SAndy Whitcroft					CHK("Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
2243e09dec48SAndy Whitcroft				} else {
2244e09dec48SAndy Whitcroft					WARN("Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
2245e09dec48SAndy Whitcroft				}
22460a920b5bSAndy Whitcroft			}
22470a920b5bSAndy Whitcroft		}
22480a920b5bSAndy Whitcroft
2249653d4876SAndy Whitcroft# multi-statement macros should be enclosed in a do while loop, grab the
2250653d4876SAndy Whitcroft# first statement and ensure its the whole macro if its not enclosed
2251cf655043SAndy Whitcroft# in a known good container
2252b8f96a31SAndy Whitcroft		if ($realfile !~ m@/vmlinux.lds.h$@ &&
2253b8f96a31SAndy Whitcroft		    $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
2254d8aaf121SAndy Whitcroft			my $ln = $linenr;
2255d8aaf121SAndy Whitcroft			my $cnt = $realcnt;
2256c45dcabdSAndy Whitcroft			my ($off, $dstat, $dcond, $rest);
2257c45dcabdSAndy Whitcroft			my $ctx = '';
2258653d4876SAndy Whitcroft
2259c45dcabdSAndy Whitcroft			my $args = defined($1);
2260de7d4f0eSAndy Whitcroft
2261c45dcabdSAndy Whitcroft			# Find the end of the macro and limit our statement
2262c45dcabdSAndy Whitcroft			# search to that.
2263c45dcabdSAndy Whitcroft			while ($cnt > 0 && defined $lines[$ln - 1] &&
2264c45dcabdSAndy Whitcroft				$lines[$ln - 1] =~ /^(?:-|..*\\$)/)
2265c45dcabdSAndy Whitcroft			{
2266c45dcabdSAndy Whitcroft				$ctx .= $rawlines[$ln - 1] . "\n";
2267a3bb97a7SAndy Whitcroft				$cnt-- if ($lines[$ln - 1] !~ /^-/);
2268c45dcabdSAndy Whitcroft				$ln++;
2269de7d4f0eSAndy Whitcroft			}
2270c45dcabdSAndy Whitcroft			$ctx .= $rawlines[$ln - 1];
2271d8aaf121SAndy Whitcroft
2272c45dcabdSAndy Whitcroft			($dstat, $dcond, $ln, $cnt, $off) =
2273c45dcabdSAndy Whitcroft				ctx_statement_block($linenr, $ln - $linenr + 1, 0);
2274c45dcabdSAndy Whitcroft			#print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
2275a3bb97a7SAndy Whitcroft			#print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
2276c45dcabdSAndy Whitcroft
2277c45dcabdSAndy Whitcroft			# Extract the remainder of the define (if any) and
2278c45dcabdSAndy Whitcroft			# rip off surrounding spaces, and trailing \'s.
2279c45dcabdSAndy Whitcroft			$rest = '';
2280636d140aSAndy Whitcroft			while ($off != 0 || ($cnt > 0 && $rest =~ /\\\s*$/)) {
2281636d140aSAndy Whitcroft				#print "ADDING cnt<$cnt> $off <" . substr($lines[$ln - 1], $off) . "> rest<$rest>\n";
2282a3bb97a7SAndy Whitcroft				if ($off != 0 || $lines[$ln - 1] !~ /^-/) {
2283c45dcabdSAndy Whitcroft					$rest .= substr($lines[$ln - 1], $off) . "\n";
2284c45dcabdSAndy Whitcroft					$cnt--;
2285a3bb97a7SAndy Whitcroft				}
2286a3bb97a7SAndy Whitcroft				$ln++;
2287c45dcabdSAndy Whitcroft				$off = 0;
2288c45dcabdSAndy Whitcroft			}
2289c45dcabdSAndy Whitcroft			$rest =~ s/\\\n.//g;
2290c45dcabdSAndy Whitcroft			$rest =~ s/^\s*//s;
2291c45dcabdSAndy Whitcroft			$rest =~ s/\s*$//s;
2292c45dcabdSAndy Whitcroft
2293c45dcabdSAndy Whitcroft			# Clean up the original statement.
2294c45dcabdSAndy Whitcroft			if ($args) {
2295c45dcabdSAndy Whitcroft				substr($dstat, 0, length($dcond), '');
2296d8aaf121SAndy Whitcroft			} else {
2297c45dcabdSAndy Whitcroft				$dstat =~ s/^.\s*\#\s*define\s+$Ident\s*//;
2298c45dcabdSAndy Whitcroft			}
2299292f1a9bSAndy Whitcroft			$dstat =~ s/$;//g;
2300c45dcabdSAndy Whitcroft			$dstat =~ s/\\\n.//g;
2301c45dcabdSAndy Whitcroft			$dstat =~ s/^\s*//s;
2302c45dcabdSAndy Whitcroft			$dstat =~ s/\s*$//s;
2303c45dcabdSAndy Whitcroft
2304c45dcabdSAndy Whitcroft			# Flatten any parentheses and braces
2305bf30d6edSAndy Whitcroft			while ($dstat =~ s/\([^\(\)]*\)/1/ ||
2306bf30d6edSAndy Whitcroft			       $dstat =~ s/\{[^\{\}]*\}/1/ ||
2307bf30d6edSAndy Whitcroft			       $dstat =~ s/\[[^\{\}]*\]/1/)
2308bf30d6edSAndy Whitcroft			{
2309c45dcabdSAndy Whitcroft			}
2310c45dcabdSAndy Whitcroft
2311c45dcabdSAndy Whitcroft			my $exceptions = qr{
2312c45dcabdSAndy Whitcroft				$Declare|
2313c45dcabdSAndy Whitcroft				module_param_named|
2314c45dcabdSAndy Whitcroft				MODULE_PARAM_DESC|
2315c45dcabdSAndy Whitcroft				DECLARE_PER_CPU|
2316c45dcabdSAndy Whitcroft				DEFINE_PER_CPU|
2317383099fdSAndy Whitcroft				__typeof__\(|
2318ea71a0a0SAndy Whitcroft				\.$Ident\s*=\s*|
2319ea71a0a0SAndy Whitcroft				^\"|\"$
2320c45dcabdSAndy Whitcroft			}x;
2321383099fdSAndy Whitcroft			#print "REST<$rest> dstat<$dstat>\n";
2322c45dcabdSAndy Whitcroft			if ($rest ne '') {
2323c45dcabdSAndy Whitcroft				if ($rest !~ /while\s*\(/ &&
2324c45dcabdSAndy Whitcroft				    $dstat !~ /$exceptions/)
2325c45dcabdSAndy Whitcroft				{
2326c45dcabdSAndy Whitcroft					ERROR("Macros with multiple statements should be enclosed in a do - while loop\n" . "$here\n$ctx\n");
2327c45dcabdSAndy Whitcroft				}
2328c45dcabdSAndy Whitcroft
2329c45dcabdSAndy Whitcroft			} elsif ($ctx !~ /;/) {
2330c45dcabdSAndy Whitcroft				if ($dstat ne '' &&
2331c45dcabdSAndy Whitcroft				    $dstat !~ /^(?:$Ident|-?$Constant)$/ &&
2332c45dcabdSAndy Whitcroft				    $dstat !~ /$exceptions/ &&
2333b132e5d5SAndy Whitcroft				    $dstat !~ /^\.$Ident\s*=/ &&
2334c45dcabdSAndy Whitcroft				    $dstat =~ /$Operators/)
2335c45dcabdSAndy Whitcroft				{
2336de7d4f0eSAndy Whitcroft					ERROR("Macros with complex values should be enclosed in parenthesis\n" . "$here\n$ctx\n");
2337d8aaf121SAndy Whitcroft				}
23380a920b5bSAndy Whitcroft			}
2339653d4876SAndy Whitcroft		}
23400a920b5bSAndy Whitcroft
2341080ba929SMike Frysinger# make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
2342080ba929SMike Frysinger# all assignments may have only one of the following with an assignment:
2343080ba929SMike Frysinger#	.
2344080ba929SMike Frysinger#	ALIGN(...)
2345080ba929SMike Frysinger#	VMLINUX_SYMBOL(...)
2346080ba929SMike Frysinger		if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
2347080ba929SMike Frysinger			WARN("vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
2348080ba929SMike Frysinger		}
2349080ba929SMike Frysinger
2350f0a594c1SAndy Whitcroft# check for redundant bracing round if etc
235113214adfSAndy Whitcroft		if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
235213214adfSAndy Whitcroft			my ($level, $endln, @chunks) =
2353cf655043SAndy Whitcroft				ctx_statement_full($linenr, $realcnt, 1);
235413214adfSAndy Whitcroft			#print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
2355cf655043SAndy Whitcroft			#print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
2356cf655043SAndy Whitcroft			if ($#chunks > 0 && $level == 0) {
235713214adfSAndy Whitcroft				my $allowed = 0;
235813214adfSAndy Whitcroft				my $seen = 0;
2359773647a0SAndy Whitcroft				my $herectx = $here . "\n";
2360cf655043SAndy Whitcroft				my $ln = $linenr - 1;
236113214adfSAndy Whitcroft				for my $chunk (@chunks) {
236213214adfSAndy Whitcroft					my ($cond, $block) = @{$chunk};
236313214adfSAndy Whitcroft
2364773647a0SAndy Whitcroft					# If the condition carries leading newlines, then count those as offsets.
2365773647a0SAndy Whitcroft					my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
2366773647a0SAndy Whitcroft					my $offset = statement_rawlines($whitespace) - 1;
2367773647a0SAndy Whitcroft
2368773647a0SAndy Whitcroft					#print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
2369773647a0SAndy Whitcroft
2370773647a0SAndy Whitcroft					# We have looked at and allowed this specific line.
2371773647a0SAndy Whitcroft					$suppress_ifbraces{$ln + $offset} = 1;
2372773647a0SAndy Whitcroft
2373773647a0SAndy Whitcroft					$herectx .= "$rawlines[$ln + $offset]\n[...]\n";
2374cf655043SAndy Whitcroft					$ln += statement_rawlines($block) - 1;
2375cf655043SAndy Whitcroft
2376773647a0SAndy Whitcroft					substr($block, 0, length($cond), '');
237713214adfSAndy Whitcroft
237813214adfSAndy Whitcroft					$seen++ if ($block =~ /^\s*{/);
237913214adfSAndy Whitcroft
2380cf655043SAndy Whitcroft					#print "cond<$cond> block<$block> allowed<$allowed>\n";
2381cf655043SAndy Whitcroft					if (statement_lines($cond) > 1) {
2382cf655043SAndy Whitcroft						#print "APW: ALLOWED: cond<$cond>\n";
238313214adfSAndy Whitcroft						$allowed = 1;
238413214adfSAndy Whitcroft					}
238513214adfSAndy Whitcroft					if ($block =~/\b(?:if|for|while)\b/) {
2386cf655043SAndy Whitcroft						#print "APW: ALLOWED: block<$block>\n";
238713214adfSAndy Whitcroft						$allowed = 1;
238813214adfSAndy Whitcroft					}
2389cf655043SAndy Whitcroft					if (statement_block_size($block) > 1) {
2390cf655043SAndy Whitcroft						#print "APW: ALLOWED: lines block<$block>\n";
239113214adfSAndy Whitcroft						$allowed = 1;
239213214adfSAndy Whitcroft					}
239313214adfSAndy Whitcroft				}
239413214adfSAndy Whitcroft				if ($seen && !$allowed) {
2395cf655043SAndy Whitcroft					WARN("braces {} are not necessary for any arm of this statement\n" . $herectx);
239613214adfSAndy Whitcroft				}
239713214adfSAndy Whitcroft			}
239813214adfSAndy Whitcroft		}
2399773647a0SAndy Whitcroft		if (!defined $suppress_ifbraces{$linenr - 1} &&
240013214adfSAndy Whitcroft					$line =~ /\b(if|while|for|else)\b/) {
2401cf655043SAndy Whitcroft			my $allowed = 0;
2402f0a594c1SAndy Whitcroft
2403cf655043SAndy Whitcroft			# Check the pre-context.
2404cf655043SAndy Whitcroft			if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
2405cf655043SAndy Whitcroft				#print "APW: ALLOWED: pre<$1>\n";
2406cf655043SAndy Whitcroft				$allowed = 1;
2407f0a594c1SAndy Whitcroft			}
2408773647a0SAndy Whitcroft
2409773647a0SAndy Whitcroft			my ($level, $endln, @chunks) =
2410773647a0SAndy Whitcroft				ctx_statement_full($linenr, $realcnt, $-[0]);
2411773647a0SAndy Whitcroft
2412cf655043SAndy Whitcroft			# Check the condition.
2413cf655043SAndy Whitcroft			my ($cond, $block) = @{$chunks[0]};
2414773647a0SAndy Whitcroft			#print "CHECKING<$linenr> cond<$cond> block<$block>\n";
2415cf655043SAndy Whitcroft			if (defined $cond) {
2416773647a0SAndy Whitcroft				substr($block, 0, length($cond), '');
2417cf655043SAndy Whitcroft			}
2418cf655043SAndy Whitcroft			if (statement_lines($cond) > 1) {
2419cf655043SAndy Whitcroft				#print "APW: ALLOWED: cond<$cond>\n";
2420cf655043SAndy Whitcroft				$allowed = 1;
2421cf655043SAndy Whitcroft			}
2422cf655043SAndy Whitcroft			if ($block =~/\b(?:if|for|while)\b/) {
2423cf655043SAndy Whitcroft				#print "APW: ALLOWED: block<$block>\n";
2424cf655043SAndy Whitcroft				$allowed = 1;
2425cf655043SAndy Whitcroft			}
2426cf655043SAndy Whitcroft			if (statement_block_size($block) > 1) {
2427cf655043SAndy Whitcroft				#print "APW: ALLOWED: lines block<$block>\n";
2428cf655043SAndy Whitcroft				$allowed = 1;
2429cf655043SAndy Whitcroft			}
2430cf655043SAndy Whitcroft			# Check the post-context.
2431cf655043SAndy Whitcroft			if (defined $chunks[1]) {
2432cf655043SAndy Whitcroft				my ($cond, $block) = @{$chunks[1]};
2433cf655043SAndy Whitcroft				if (defined $cond) {
2434773647a0SAndy Whitcroft					substr($block, 0, length($cond), '');
2435cf655043SAndy Whitcroft				}
2436cf655043SAndy Whitcroft				if ($block =~ /^\s*\{/) {
2437cf655043SAndy Whitcroft					#print "APW: ALLOWED: chunk-1 block<$block>\n";
2438cf655043SAndy Whitcroft					$allowed = 1;
2439cf655043SAndy Whitcroft				}
2440cf655043SAndy Whitcroft			}
2441cf655043SAndy Whitcroft			if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
2442cf655043SAndy Whitcroft				my $herectx = $here . "\n";;
2443f055663cSAndy Whitcroft				my $cnt = statement_rawlines($block);
2444cf655043SAndy Whitcroft
2445f055663cSAndy Whitcroft				for (my $n = 0; $n < $cnt; $n++) {
2446f055663cSAndy Whitcroft					$herectx .= raw_line($linenr, $n) . "\n";;
2447cf655043SAndy Whitcroft				}
2448cf655043SAndy Whitcroft
2449cf655043SAndy Whitcroft				WARN("braces {} are not necessary for single statement blocks\n" . $herectx);
2450f0a594c1SAndy Whitcroft			}
2451f0a594c1SAndy Whitcroft		}
2452f0a594c1SAndy Whitcroft
2453653d4876SAndy Whitcroft# don't include deprecated include files (uses RAW line)
24544a0df2efSAndy Whitcroft		for my $inc (@dep_includes) {
2455c45dcabdSAndy Whitcroft			if ($rawline =~ m@^.\s*\#\s*include\s*\<$inc>@) {
2456de7d4f0eSAndy Whitcroft				ERROR("Don't use <$inc>: see Documentation/feature-removal-schedule.txt\n" . $herecurr);
24570a920b5bSAndy Whitcroft			}
24580a920b5bSAndy Whitcroft		}
24590a920b5bSAndy Whitcroft
24604a0df2efSAndy Whitcroft# don't use deprecated functions
24614a0df2efSAndy Whitcroft		for my $func (@dep_functions) {
246200df344fSAndy Whitcroft			if ($line =~ /\b$func\b/) {
2463de7d4f0eSAndy Whitcroft				ERROR("Don't use $func(): see Documentation/feature-removal-schedule.txt\n" . $herecurr);
24644a0df2efSAndy Whitcroft			}
24654a0df2efSAndy Whitcroft		}
24664a0df2efSAndy Whitcroft
24674a0df2efSAndy Whitcroft# no volatiles please
24686c72ffaaSAndy Whitcroft		my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
24696c72ffaaSAndy Whitcroft		if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
2470de7d4f0eSAndy Whitcroft			WARN("Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
24714a0df2efSAndy Whitcroft		}
24724a0df2efSAndy Whitcroft
24739c0ca6f9SAndy Whitcroft# SPIN_LOCK_UNLOCKED & RW_LOCK_UNLOCKED are deprecated
24749c0ca6f9SAndy Whitcroft		if ($line =~ /\b(SPIN_LOCK_UNLOCKED|RW_LOCK_UNLOCKED)/) {
24759c0ca6f9SAndy Whitcroft			ERROR("Use of $1 is deprecated: see Documentation/spinlocks.txt\n" . $herecurr);
24769c0ca6f9SAndy Whitcroft		}
24779c0ca6f9SAndy Whitcroft
247800df344fSAndy Whitcroft# warn about #if 0
2479c45dcabdSAndy Whitcroft		if ($line =~ /^.\s*\#\s*if\s+0\b/) {
2480de7d4f0eSAndy Whitcroft			CHK("if this code is redundant consider removing it\n" .
2481de7d4f0eSAndy Whitcroft				$herecurr);
24824a0df2efSAndy Whitcroft		}
24834a0df2efSAndy Whitcroft
2484f0a594c1SAndy Whitcroft# check for needless kfree() checks
2485f0a594c1SAndy Whitcroft		if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
2486f0a594c1SAndy Whitcroft			my $expr = $1;
2487f0a594c1SAndy Whitcroft			if ($line =~ /\bkfree\(\Q$expr\E\);/) {
24883c232147SWolfram Sang				WARN("kfree(NULL) is safe this check is probably not required\n" . $hereprev);
2489f0a594c1SAndy Whitcroft			}
2490f0a594c1SAndy Whitcroft		}
24914c432a8fSGreg Kroah-Hartman# check for needless usb_free_urb() checks
24924c432a8fSGreg Kroah-Hartman		if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
24934c432a8fSGreg Kroah-Hartman			my $expr = $1;
24944c432a8fSGreg Kroah-Hartman			if ($line =~ /\busb_free_urb\(\Q$expr\E\);/) {
24954c432a8fSGreg Kroah-Hartman				WARN("usb_free_urb(NULL) is safe this check is probably not required\n" . $hereprev);
24964c432a8fSGreg Kroah-Hartman			}
24974c432a8fSGreg Kroah-Hartman		}
2498f0a594c1SAndy Whitcroft
249900df344fSAndy Whitcroft# warn about #ifdefs in C files
2500c45dcabdSAndy Whitcroft#		if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
250100df344fSAndy Whitcroft#			print "#ifdef in C files should be avoided\n";
250200df344fSAndy Whitcroft#			print "$herecurr";
250300df344fSAndy Whitcroft#			$clean = 0;
250400df344fSAndy Whitcroft#		}
250500df344fSAndy Whitcroft
250622f2a2efSAndy Whitcroft# warn about spacing in #ifdefs
2507c45dcabdSAndy Whitcroft		if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
250822f2a2efSAndy Whitcroft			ERROR("exactly one space required after that #$1\n" . $herecurr);
250922f2a2efSAndy Whitcroft		}
251022f2a2efSAndy Whitcroft
25114a0df2efSAndy Whitcroft# check for spinlock_t definitions without a comment.
2512171ae1a4SAndy Whitcroft		if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
2513171ae1a4SAndy Whitcroft		    $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
25144a0df2efSAndy Whitcroft			my $which = $1;
25154a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
2516de7d4f0eSAndy Whitcroft				CHK("$1 definition without comment\n" . $herecurr);
25174a0df2efSAndy Whitcroft			}
25184a0df2efSAndy Whitcroft		}
25194a0df2efSAndy Whitcroft# check for memory barriers without a comment.
25204a0df2efSAndy Whitcroft		if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
25214a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
2522de7d4f0eSAndy Whitcroft				CHK("memory barrier without comment\n" . $herecurr);
25234a0df2efSAndy Whitcroft			}
25244a0df2efSAndy Whitcroft		}
25254a0df2efSAndy Whitcroft# check of hardware specific defines
2526c45dcabdSAndy Whitcroft		if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
2527de7d4f0eSAndy Whitcroft			CHK("architecture specific defines should be avoided\n" .  $herecurr);
25280a920b5bSAndy Whitcroft		}
2529653d4876SAndy Whitcroft
2530de7d4f0eSAndy Whitcroft# check the location of the inline attribute, that it is between
2531de7d4f0eSAndy Whitcroft# storage class and type.
25329c0ca6f9SAndy Whitcroft		if ($line =~ /\b$Type\s+$Inline\b/ ||
25339c0ca6f9SAndy Whitcroft		    $line =~ /\b$Inline\s+$Storage\b/) {
2534de7d4f0eSAndy Whitcroft			ERROR("inline keyword should sit between storage class and type\n" . $herecurr);
2535de7d4f0eSAndy Whitcroft		}
2536de7d4f0eSAndy Whitcroft
25378905a67cSAndy Whitcroft# Check for __inline__ and __inline, prefer inline
25388905a67cSAndy Whitcroft		if ($line =~ /\b(__inline__|__inline)\b/) {
25398905a67cSAndy Whitcroft			WARN("plain inline is preferred over $1\n" . $herecurr);
25408905a67cSAndy Whitcroft		}
25418905a67cSAndy Whitcroft
2542de7d4f0eSAndy Whitcroft# check for new externs in .c files.
2543171ae1a4SAndy Whitcroft		if ($realfile =~ /\.c$/ && defined $stat &&
2544c45dcabdSAndy Whitcroft		    $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
2545171ae1a4SAndy Whitcroft		{
2546c45dcabdSAndy Whitcroft			my $function_name = $1;
2547c45dcabdSAndy Whitcroft			my $paren_space = $2;
2548171ae1a4SAndy Whitcroft
2549171ae1a4SAndy Whitcroft			my $s = $stat;
2550171ae1a4SAndy Whitcroft			if (defined $cond) {
2551171ae1a4SAndy Whitcroft				substr($s, 0, length($cond), '');
2552171ae1a4SAndy Whitcroft			}
2553c45dcabdSAndy Whitcroft			if ($s =~ /^\s*;/ &&
2554c45dcabdSAndy Whitcroft			    $function_name ne 'uninitialized_var')
2555c45dcabdSAndy Whitcroft			{
2556de7d4f0eSAndy Whitcroft				WARN("externs should be avoided in .c files\n" .  $herecurr);
2557de7d4f0eSAndy Whitcroft			}
2558de7d4f0eSAndy Whitcroft
2559171ae1a4SAndy Whitcroft			if ($paren_space =~ /\n/) {
2560171ae1a4SAndy Whitcroft				WARN("arguments for function declarations should follow identifier\n" . $herecurr);
2561171ae1a4SAndy Whitcroft			}
25629c9ba34eSAndy Whitcroft
25639c9ba34eSAndy Whitcroft		} elsif ($realfile =~ /\.c$/ && defined $stat &&
25649c9ba34eSAndy Whitcroft		    $stat =~ /^.\s*extern\s+/)
25659c9ba34eSAndy Whitcroft		{
25669c9ba34eSAndy Whitcroft			WARN("externs should be avoided in .c files\n" .  $herecurr);
2567171ae1a4SAndy Whitcroft		}
2568171ae1a4SAndy Whitcroft
2569de7d4f0eSAndy Whitcroft# checks for new __setup's
2570de7d4f0eSAndy Whitcroft		if ($rawline =~ /\b__setup\("([^"]*)"/) {
2571de7d4f0eSAndy Whitcroft			my $name = $1;
2572de7d4f0eSAndy Whitcroft
2573de7d4f0eSAndy Whitcroft			if (!grep(/$name/, @setup_docs)) {
2574de7d4f0eSAndy Whitcroft				CHK("__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
2575de7d4f0eSAndy Whitcroft			}
2576653d4876SAndy Whitcroft		}
25779c0ca6f9SAndy Whitcroft
25789c0ca6f9SAndy Whitcroft# check for pointless casting of kmalloc return
25799c0ca6f9SAndy Whitcroft		if ($line =~ /\*\s*\)\s*k[czm]alloc\b/) {
25809c0ca6f9SAndy Whitcroft			WARN("unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
25819c0ca6f9SAndy Whitcroft		}
258213214adfSAndy Whitcroft
258313214adfSAndy Whitcroft# check for gcc specific __FUNCTION__
258413214adfSAndy Whitcroft		if ($line =~ /__FUNCTION__/) {
258513214adfSAndy Whitcroft			WARN("__func__ should be used instead of gcc specific __FUNCTION__\n"  . $herecurr);
258613214adfSAndy Whitcroft		}
2587773647a0SAndy Whitcroft
2588773647a0SAndy Whitcroft# check for semaphores used as mutexes
2589171ae1a4SAndy Whitcroft		if ($line =~ /^.\s*(DECLARE_MUTEX|init_MUTEX)\s*\(/) {
2590773647a0SAndy Whitcroft			WARN("mutexes are preferred for single holder semaphores\n" . $herecurr);
2591773647a0SAndy Whitcroft		}
2592773647a0SAndy Whitcroft# check for semaphores used as mutexes
2593171ae1a4SAndy Whitcroft		if ($line =~ /^.\s*init_MUTEX_LOCKED\s*\(/) {
2594773647a0SAndy Whitcroft			WARN("consider using a completion\n" . $herecurr);
2595773647a0SAndy Whitcroft		}
2596773647a0SAndy Whitcroft# recommend strict_strto* over simple_strto*
2597773647a0SAndy Whitcroft		if ($line =~ /\bsimple_(strto.*?)\s*\(/) {
2598773647a0SAndy Whitcroft			WARN("consider using strict_$1 in preference to simple_$1\n" . $herecurr);
2599773647a0SAndy Whitcroft		}
2600f3db6639SMichael Ellerman# check for __initcall(), use device_initcall() explicitly please
2601f3db6639SMichael Ellerman		if ($line =~ /^.\s*__initcall\s*\(/) {
2602f3db6639SMichael Ellerman			WARN("please use device_initcall() instead of __initcall()\n" . $herecurr);
2603f3db6639SMichael Ellerman		}
26042b6db5cbSAndy Whitcroft# check for struct file_operations, ensure they are const.
26056903ffb2SAndy Whitcroft		if ($line !~ /\bconst\b/ &&
26066903ffb2SAndy Whitcroft		    $line =~ /\bstruct\s+(file_operations|seq_operations)\b/) {
26076903ffb2SAndy Whitcroft			WARN("struct $1 should normally be const\n" .
26086903ffb2SAndy Whitcroft				$herecurr);
26092b6db5cbSAndy Whitcroft		}
2610773647a0SAndy Whitcroft
2611773647a0SAndy Whitcroft# use of NR_CPUS is usually wrong
2612773647a0SAndy Whitcroft# ignore definitions of NR_CPUS and usage to define arrays as likely right
2613773647a0SAndy Whitcroft		if ($line =~ /\bNR_CPUS\b/ &&
2614c45dcabdSAndy Whitcroft		    $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
2615c45dcabdSAndy Whitcroft		    $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
2616171ae1a4SAndy Whitcroft		    $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
2617171ae1a4SAndy Whitcroft		    $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
2618171ae1a4SAndy Whitcroft		    $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
2619773647a0SAndy Whitcroft		{
2620773647a0SAndy Whitcroft			WARN("usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
2621773647a0SAndy Whitcroft		}
26229c9ba34eSAndy Whitcroft
26239c9ba34eSAndy Whitcroft# check for %L{u,d,i} in strings
26249c9ba34eSAndy Whitcroft		my $string;
26259c9ba34eSAndy Whitcroft		while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
26269c9ba34eSAndy Whitcroft			$string = substr($rawline, $-[1], $+[1] - $-[1]);
26272a1bc5d5SAndy Whitcroft			$string =~ s/%%/__/g;
26289c9ba34eSAndy Whitcroft			if ($string =~ /(?<!%)%L[udi]/) {
26299c9ba34eSAndy Whitcroft				WARN("\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
26309c9ba34eSAndy Whitcroft				last;
26319c9ba34eSAndy Whitcroft			}
26329c9ba34eSAndy Whitcroft		}
2633691d77b6SAndy Whitcroft
2634691d77b6SAndy Whitcroft# whine mightly about in_atomic
2635691d77b6SAndy Whitcroft		if ($line =~ /\bin_atomic\s*\(/) {
2636691d77b6SAndy Whitcroft			if ($realfile =~ m@^drivers/@) {
2637691d77b6SAndy Whitcroft				ERROR("do not use in_atomic in drivers\n" . $herecurr);
2638f4a87736SAndy Whitcroft			} elsif ($realfile !~ m@^kernel/@) {
2639691d77b6SAndy Whitcroft				WARN("use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
2640691d77b6SAndy Whitcroft			}
2641691d77b6SAndy Whitcroft		}
264213214adfSAndy Whitcroft	}
264313214adfSAndy Whitcroft
264413214adfSAndy Whitcroft	# If we have no input at all, then there is nothing to report on
264513214adfSAndy Whitcroft	# so just keep quiet.
264613214adfSAndy Whitcroft	if ($#rawlines == -1) {
264713214adfSAndy Whitcroft		exit(0);
26480a920b5bSAndy Whitcroft	}
26490a920b5bSAndy Whitcroft
26508905a67cSAndy Whitcroft	# In mailback mode only produce a report in the negative, for
26518905a67cSAndy Whitcroft	# things that appear to be patches.
26528905a67cSAndy Whitcroft	if ($mailback && ($clean == 1 || !$is_patch)) {
26538905a67cSAndy Whitcroft		exit(0);
26548905a67cSAndy Whitcroft	}
26558905a67cSAndy Whitcroft
26568905a67cSAndy Whitcroft	# This is not a patch, and we are are in 'no-patch' mode so
26578905a67cSAndy Whitcroft	# just keep quiet.
26588905a67cSAndy Whitcroft	if (!$chk_patch && !$is_patch) {
26598905a67cSAndy Whitcroft		exit(0);
26608905a67cSAndy Whitcroft	}
26618905a67cSAndy Whitcroft
26628905a67cSAndy Whitcroft	if (!$is_patch) {
2663de7d4f0eSAndy Whitcroft		ERROR("Does not appear to be a unified-diff format patch\n");
26640a920b5bSAndy Whitcroft	}
26650a920b5bSAndy Whitcroft	if ($is_patch && $chk_signoff && $signoff == 0) {
2666de7d4f0eSAndy Whitcroft		ERROR("Missing Signed-off-by: line(s)\n");
26670a920b5bSAndy Whitcroft	}
26680a920b5bSAndy Whitcroft
2669f0a594c1SAndy Whitcroft	print report_dump();
267013214adfSAndy Whitcroft	if ($summary && !($clean == 1 && $quiet == 1)) {
267113214adfSAndy Whitcroft		print "$filename " if ($summary_file);
26726c72ffaaSAndy Whitcroft		print "total: $cnt_error errors, $cnt_warn warnings, " .
26736c72ffaaSAndy Whitcroft			(($check)? "$cnt_chk checks, " : "") .
26746c72ffaaSAndy Whitcroft			"$cnt_lines lines checked\n";
26758905a67cSAndy Whitcroft		print "\n" if ($quiet == 0);
26766c72ffaaSAndy Whitcroft	}
26778905a67cSAndy Whitcroft
26780a920b5bSAndy Whitcroft	if ($clean == 1 && $quiet == 0) {
2679c2fdda0dSAndy Whitcroft		print "$vname has no obvious style problems and is ready for submission.\n"
26800a920b5bSAndy Whitcroft	}
26810a920b5bSAndy Whitcroft	if ($clean == 0 && $quiet == 0) {
2682c2fdda0dSAndy Whitcroft		print "$vname has style problems, please review.  If any of these errors\n";
26830a920b5bSAndy Whitcroft		print "are false positives report them to the maintainer, see\n";
26840a920b5bSAndy Whitcroft		print "CHECKPATCH in MAINTAINERS.\n";
26850a920b5bSAndy Whitcroft	}
268613214adfSAndy Whitcroft
26870a920b5bSAndy Whitcroft	return $clean;
26880a920b5bSAndy Whitcroft}
2689