xref: /linux-6.15/scripts/checkpatch.pl (revision 9a974fdb)
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) = @_;
1000*9a974fdbSAndy Whitcroft	my $notPermitted = qr{(?:
10010776e594SAndy Whitcroft		^(?:
10020776e594SAndy Whitcroft			$Modifier|
10030776e594SAndy Whitcroft			$Storage|
10040776e594SAndy Whitcroft			$Type|
1005*9a974fdbSAndy Whitcroft			DEFINE_\S+
1006*9a974fdbSAndy Whitcroft		)$|
1007*9a974fdbSAndy Whitcroft		^(?:
10080776e594SAndy Whitcroft			goto|
10090776e594SAndy Whitcroft			return|
10100776e594SAndy Whitcroft			case|
10110776e594SAndy Whitcroft			else|
10120776e594SAndy Whitcroft			asm|__asm__|
10130776e594SAndy Whitcroft			do
1014*9a974fdbSAndy Whitcroft		)(?:\s|$)|
10150776e594SAndy Whitcroft		^(?:typedef|struct|enum)\b
1016*9a974fdbSAndy Whitcroft	    )}x;
1017*9a974fdbSAndy Whitcroft	warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
1018*9a974fdbSAndy 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)) {
1027*9a974fdbSAndy Whitcroft				if ($modifier !~ $notPermitted) {
1028d2506586SAndy Whitcroft					warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1029d2506586SAndy Whitcroft					push(@modifierList, $modifier);
1030d2506586SAndy Whitcroft				}
1031*9a974fdbSAndy 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];
123730670854SAndy Whitcroft		my $hunk_line = ($realcnt != 0);
12386c72ffaaSAndy Whitcroft
12390a920b5bSAndy Whitcroft#extract the line range in the file after the patch is applied
12406c72ffaaSAndy Whitcroft		if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
12410a920b5bSAndy Whitcroft			$is_patch = 1;
12424a0df2efSAndy Whitcroft			$first_line = $linenr + 1;
12430a920b5bSAndy Whitcroft			$realline=$1-1;
12440a920b5bSAndy Whitcroft			if (defined $2) {
12450a920b5bSAndy Whitcroft				$realcnt=$3+1;
12460a920b5bSAndy Whitcroft			} else {
12470a920b5bSAndy Whitcroft				$realcnt=1+1;
12480a920b5bSAndy Whitcroft			}
1249c2fdda0dSAndy Whitcroft			annotate_reset();
125013214adfSAndy Whitcroft			$prev_values = 'E';
125113214adfSAndy Whitcroft
1252773647a0SAndy Whitcroft			%suppress_ifbraces = ();
1253170d3a22SAndy Whitcroft			%suppress_whiletrailers = ();
12540a920b5bSAndy Whitcroft			next;
12550a920b5bSAndy Whitcroft
12564a0df2efSAndy Whitcroft# track the line number as we move through the hunk, note that
12574a0df2efSAndy Whitcroft# new versions of GNU diff omit the leading space on completely
12584a0df2efSAndy Whitcroft# blank context lines so we need to count that too.
1259773647a0SAndy Whitcroft		} elsif ($line =~ /^( |\+|$)/) {
12600a920b5bSAndy Whitcroft			$realline++;
1261d8aaf121SAndy Whitcroft			$realcnt-- if ($realcnt != 0);
12620a920b5bSAndy Whitcroft
12634a0df2efSAndy Whitcroft			# Measure the line length and indent.
1264c2fdda0dSAndy Whitcroft			($length, $indent) = line_stats($rawline);
12650a920b5bSAndy Whitcroft
12660a920b5bSAndy Whitcroft			# Track the previous line.
12670a920b5bSAndy Whitcroft			($prevline, $stashline) = ($stashline, $line);
12680a920b5bSAndy Whitcroft			($previndent, $stashindent) = ($stashindent, $indent);
1269c2fdda0dSAndy Whitcroft			($prevrawline, $stashrawline) = ($stashrawline, $rawline);
1270c2fdda0dSAndy Whitcroft
1271773647a0SAndy Whitcroft			#warn "line<$line>\n";
12726c72ffaaSAndy Whitcroft
1273d8aaf121SAndy Whitcroft		} elsif ($realcnt == 1) {
1274d8aaf121SAndy Whitcroft			$realcnt--;
12750a920b5bSAndy Whitcroft		}
12760a920b5bSAndy Whitcroft
12770a920b5bSAndy Whitcroft#make up the handle for any error we report on this line
1278773647a0SAndy Whitcroft		$prefix = "$filename:$realline: " if ($emacs && $file);
1279773647a0SAndy Whitcroft		$prefix = "$filename:$linenr: " if ($emacs && !$file);
1280773647a0SAndy Whitcroft
12816c72ffaaSAndy Whitcroft		$here = "#$linenr: " if (!$file);
12826c72ffaaSAndy Whitcroft		$here = "#$realline: " if ($file);
1283773647a0SAndy Whitcroft
1284773647a0SAndy Whitcroft		# extract the filename as it passes
1285773647a0SAndy Whitcroft		if ($line=~/^\+\+\+\s+(\S+)/) {
1286773647a0SAndy Whitcroft			$realfile = $1;
12871e855726SWolfram Sang			$realfile =~ s@^([^/]*)/@@;
12881e855726SWolfram Sang
12891e855726SWolfram Sang			$p1_prefix = $1;
1290e2f7aa4bSAndy Whitcroft			if (!$file && $tree && $p1_prefix ne '' &&
1291e2f7aa4bSAndy Whitcroft			    -e "$root/$p1_prefix") {
12921e855726SWolfram Sang				WARN("patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
12931e855726SWolfram Sang			}
1294773647a0SAndy Whitcroft
1295c1ab3326SAndy Whitcroft			if ($realfile =~ m@^include/asm/@) {
1296773647a0SAndy Whitcroft				ERROR("do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
1297773647a0SAndy Whitcroft			}
1298773647a0SAndy Whitcroft			next;
1299773647a0SAndy Whitcroft		}
1300773647a0SAndy Whitcroft
1301389834b6SRandy Dunlap		$here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
13020a920b5bSAndy Whitcroft
1303c2fdda0dSAndy Whitcroft		my $hereline = "$here\n$rawline\n";
1304c2fdda0dSAndy Whitcroft		my $herecurr = "$here\n$rawline\n";
1305c2fdda0dSAndy Whitcroft		my $hereprev = "$here\n$prevrawline\n$rawline\n";
13060a920b5bSAndy Whitcroft
13076c72ffaaSAndy Whitcroft		$cnt_lines++ if ($realcnt != 0);
13086c72ffaaSAndy Whitcroft
13090a920b5bSAndy Whitcroft#check the patch for a signoff:
1310d8aaf121SAndy Whitcroft		if ($line =~ /^\s*signed-off-by:/i) {
13114a0df2efSAndy Whitcroft			# This is a signoff, if ugly, so do not double report.
13124a0df2efSAndy Whitcroft			$signoff++;
13130a920b5bSAndy Whitcroft			if (!($line =~ /^\s*Signed-off-by:/)) {
1314de7d4f0eSAndy Whitcroft				WARN("Signed-off-by: is the preferred form\n" .
1315de7d4f0eSAndy Whitcroft					$herecurr);
13160a920b5bSAndy Whitcroft			}
13170a920b5bSAndy Whitcroft			if ($line =~ /^\s*signed-off-by:\S/i) {
1318773647a0SAndy Whitcroft				WARN("space required after Signed-off-by:\n" .
1319de7d4f0eSAndy Whitcroft					$herecurr);
13200a920b5bSAndy Whitcroft			}
13210a920b5bSAndy Whitcroft		}
13220a920b5bSAndy Whitcroft
132300df344fSAndy Whitcroft# Check for wrappage within a valid hunk of the file
13248905a67cSAndy Whitcroft		if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
1325de7d4f0eSAndy Whitcroft			ERROR("patch seems to be corrupt (line wrapped?)\n" .
13266c72ffaaSAndy Whitcroft				$herecurr) if (!$emitted_corrupt++);
1327de7d4f0eSAndy Whitcroft		}
1328de7d4f0eSAndy Whitcroft
13296ecd9674SAndy Whitcroft# Check for absolute kernel paths.
13306ecd9674SAndy Whitcroft		if ($tree) {
13316ecd9674SAndy Whitcroft			while ($line =~ m{(?:^|\s)(/\S*)}g) {
13326ecd9674SAndy Whitcroft				my $file = $1;
13336ecd9674SAndy Whitcroft
13346ecd9674SAndy Whitcroft				if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
13356ecd9674SAndy Whitcroft				    check_absolute_file($1, $herecurr)) {
13366ecd9674SAndy Whitcroft					#
13376ecd9674SAndy Whitcroft				} else {
13386ecd9674SAndy Whitcroft					check_absolute_file($file, $herecurr);
13396ecd9674SAndy Whitcroft				}
13406ecd9674SAndy Whitcroft			}
13416ecd9674SAndy Whitcroft		}
13426ecd9674SAndy Whitcroft
1343de7d4f0eSAndy Whitcroft# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
1344de7d4f0eSAndy Whitcroft		if (($realfile =~ /^$/ || $line =~ /^\+/) &&
1345171ae1a4SAndy Whitcroft		    $rawline !~ m/^$UTF8*$/) {
1346171ae1a4SAndy Whitcroft			my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
1347171ae1a4SAndy Whitcroft
1348171ae1a4SAndy Whitcroft			my $blank = copy_spacing($rawline);
1349171ae1a4SAndy Whitcroft			my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
1350171ae1a4SAndy Whitcroft			my $hereptr = "$hereline$ptr\n";
1351171ae1a4SAndy Whitcroft
1352171ae1a4SAndy Whitcroft			ERROR("Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
135300df344fSAndy Whitcroft		}
13540a920b5bSAndy Whitcroft
135530670854SAndy Whitcroft# ignore non-hunk lines and lines being removed
135630670854SAndy Whitcroft		next if (!$hunk_line || $line =~ /^-/);
135700df344fSAndy Whitcroft
13580a920b5bSAndy Whitcroft#trailing whitespace
13599c0ca6f9SAndy Whitcroft		if ($line =~ /^\+.*\015/) {
1360c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
13619c0ca6f9SAndy Whitcroft			ERROR("DOS line endings\n" . $herevet);
13629c0ca6f9SAndy Whitcroft
1363c2fdda0dSAndy Whitcroft		} elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
1364c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1365de7d4f0eSAndy Whitcroft			ERROR("trailing whitespace\n" . $herevet);
13660a920b5bSAndy Whitcroft		}
13675368df20SAndy Whitcroft
13685368df20SAndy Whitcroft# check we are in a valid source file if not then ignore this hunk
13695368df20SAndy Whitcroft		next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
13705368df20SAndy Whitcroft
13710a920b5bSAndy Whitcroft#80 column limit
1372c45dcabdSAndy Whitcroft		if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ &&
1373f4c014c0SAndy Whitcroft		    $rawline !~ /^.\s*\*\s*\@$Ident\s/ &&
1374f4c014c0SAndy Whitcroft		    $line !~ /^\+\s*printk\s*\(\s*(?:KERN_\S+\s*)?"[X\t]*"\s*(?:,|\)\s*;)\s*$/ &&
1375f4c014c0SAndy Whitcroft		    $length > 80)
1376c45dcabdSAndy Whitcroft		{
1377de7d4f0eSAndy Whitcroft			WARN("line over 80 characters\n" . $herecurr);
13780a920b5bSAndy Whitcroft		}
13790a920b5bSAndy Whitcroft
13808905a67cSAndy Whitcroft# check for adding lines without a newline.
13818905a67cSAndy Whitcroft		if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
13828905a67cSAndy Whitcroft			WARN("adding a line without newline at end of file\n" . $herecurr);
13838905a67cSAndy Whitcroft		}
13848905a67cSAndy Whitcroft
138542e41c54SMike Frysinger# Blackfin: use hi/lo macros
138642e41c54SMike Frysinger		if ($realfile =~ m@arch/blackfin/.*\.S$@) {
138742e41c54SMike Frysinger			if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
138842e41c54SMike Frysinger				my $herevet = "$here\n" . cat_vet($line) . "\n";
138942e41c54SMike Frysinger				ERROR("use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
139042e41c54SMike Frysinger			}
139142e41c54SMike Frysinger			if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
139242e41c54SMike Frysinger				my $herevet = "$here\n" . cat_vet($line) . "\n";
139342e41c54SMike Frysinger				ERROR("use the HI() macro, not (... >> 16)\n" . $herevet);
139442e41c54SMike Frysinger			}
139542e41c54SMike Frysinger		}
139642e41c54SMike Frysinger
1397b9ea10d6SAndy Whitcroft# check we are in a valid source file C or perl if not then ignore this hunk
1398b9ea10d6SAndy Whitcroft		next if ($realfile !~ /\.(h|c|pl)$/);
13990a920b5bSAndy Whitcroft
14000a920b5bSAndy Whitcroft# at the beginning of a line any tabs must come first and anything
14010a920b5bSAndy Whitcroft# more than 8 must use tabs.
1402c2fdda0dSAndy Whitcroft		if ($rawline =~ /^\+\s* \t\s*\S/ ||
1403c2fdda0dSAndy Whitcroft		    $rawline =~ /^\+\s*        \s*/) {
1404c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1405171ae1a4SAndy Whitcroft			ERROR("code indent should use tabs where possible\n" . $herevet);
14060a920b5bSAndy Whitcroft		}
14070a920b5bSAndy Whitcroft
1408b9ea10d6SAndy Whitcroft# check we are in a valid C source file if not then ignore this hunk
1409b9ea10d6SAndy Whitcroft		next if ($realfile !~ /\.(h|c)$/);
1410b9ea10d6SAndy Whitcroft
1411c2fdda0dSAndy Whitcroft# check for RCS/CVS revision markers
1412cf655043SAndy Whitcroft		if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
1413c2fdda0dSAndy Whitcroft			WARN("CVS style keyword markers, these will _not_ be updated\n". $herecurr);
1414c2fdda0dSAndy Whitcroft		}
141522f2a2efSAndy Whitcroft
141642e41c54SMike Frysinger# Blackfin: don't use __builtin_bfin_[cs]sync
141742e41c54SMike Frysinger		if ($line =~ /__builtin_bfin_csync/) {
141842e41c54SMike Frysinger			my $herevet = "$here\n" . cat_vet($line) . "\n";
141942e41c54SMike Frysinger			ERROR("use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
142042e41c54SMike Frysinger		}
142142e41c54SMike Frysinger		if ($line =~ /__builtin_bfin_ssync/) {
142242e41c54SMike Frysinger			my $herevet = "$here\n" . cat_vet($line) . "\n";
142342e41c54SMike Frysinger			ERROR("use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
142442e41c54SMike Frysinger		}
142542e41c54SMike Frysinger
14269c0ca6f9SAndy Whitcroft# Check for potential 'bare' types
1427170d3a22SAndy Whitcroft		my ($stat, $cond, $line_nr_next, $remain_next, $off_next);
14289c9ba34eSAndy Whitcroft		if ($realcnt && $line =~ /.\s*\S/) {
1429170d3a22SAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
1430f5fe35ddSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0);
1431171ae1a4SAndy Whitcroft			$stat =~ s/\n./\n /g;
1432171ae1a4SAndy Whitcroft			$cond =~ s/\n./\n /g;
1433171ae1a4SAndy Whitcroft
1434171ae1a4SAndy Whitcroft			my $s = $stat;
1435171ae1a4SAndy Whitcroft			$s =~ s/{.*$//s;
1436cf655043SAndy Whitcroft
1437c2fdda0dSAndy Whitcroft			# Ignore goto labels.
1438171ae1a4SAndy Whitcroft			if ($s =~ /$Ident:\*$/s) {
1439c2fdda0dSAndy Whitcroft
1440c2fdda0dSAndy Whitcroft			# Ignore functions being called
1441171ae1a4SAndy Whitcroft			} elsif ($s =~ /^.\s*$Ident\s*\(/s) {
1442c2fdda0dSAndy Whitcroft
1443463f2864SAndy Whitcroft			} elsif ($s =~ /^.\s*else\b/s) {
1444463f2864SAndy Whitcroft
1445c45dcabdSAndy Whitcroft			# declarations always start with types
1446d2506586SAndy 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) {
1447c45dcabdSAndy Whitcroft				my $type = $1;
1448c45dcabdSAndy Whitcroft				$type =~ s/\s+/ /g;
1449c45dcabdSAndy Whitcroft				possible($type, "A:" . $s);
1450c45dcabdSAndy Whitcroft
14516c72ffaaSAndy Whitcroft			# definitions in global scope can only start with types
1452a6a84062SAndy Whitcroft			} elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
1453c45dcabdSAndy Whitcroft				possible($1, "B:" . $s);
1454c2fdda0dSAndy Whitcroft			}
14558905a67cSAndy Whitcroft
14566c72ffaaSAndy Whitcroft			# any (foo ... *) is a pointer cast, and foo is a type
145765863862SAndy Whitcroft			while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
1458c45dcabdSAndy Whitcroft				possible($1, "C:" . $s);
14599c0ca6f9SAndy Whitcroft			}
14608905a67cSAndy Whitcroft
14618905a67cSAndy Whitcroft			# Check for any sort of function declaration.
14628905a67cSAndy Whitcroft			# int foo(something bar, other baz);
14638905a67cSAndy Whitcroft			# void (*store_gdt)(x86_descr_ptr *);
1464171ae1a4SAndy Whitcroft			if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
14658905a67cSAndy Whitcroft				my ($name_len) = length($1);
14668905a67cSAndy Whitcroft
1467cf655043SAndy Whitcroft				my $ctx = $s;
1468773647a0SAndy Whitcroft				substr($ctx, 0, $name_len + 1, '');
14698905a67cSAndy Whitcroft				$ctx =~ s/\)[^\)]*$//;
1470cf655043SAndy Whitcroft
14718905a67cSAndy Whitcroft				for my $arg (split(/\s*,\s*/, $ctx)) {
1472c45dcabdSAndy Whitcroft					if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
14738905a67cSAndy Whitcroft
1474c45dcabdSAndy Whitcroft						possible($1, "D:" . $s);
14758905a67cSAndy Whitcroft					}
14768905a67cSAndy Whitcroft				}
14778905a67cSAndy Whitcroft			}
14788905a67cSAndy Whitcroft
14799c0ca6f9SAndy Whitcroft		}
14809c0ca6f9SAndy Whitcroft
148100df344fSAndy Whitcroft#
148200df344fSAndy Whitcroft# Checks which may be anchored in the context.
148300df344fSAndy Whitcroft#
148400df344fSAndy Whitcroft
148500df344fSAndy Whitcroft# Check for switch () and associated case and default
148600df344fSAndy Whitcroft# statements should be at the same indent.
148700df344fSAndy Whitcroft		if ($line=~/\bswitch\s*\(.*\)/) {
148800df344fSAndy Whitcroft			my $err = '';
148900df344fSAndy Whitcroft			my $sep = '';
149000df344fSAndy Whitcroft			my @ctx = ctx_block_outer($linenr, $realcnt);
149100df344fSAndy Whitcroft			shift(@ctx);
149200df344fSAndy Whitcroft			for my $ctx (@ctx) {
149300df344fSAndy Whitcroft				my ($clen, $cindent) = line_stats($ctx);
149400df344fSAndy Whitcroft				if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
149500df344fSAndy Whitcroft							$indent != $cindent) {
149600df344fSAndy Whitcroft					$err .= "$sep$ctx\n";
149700df344fSAndy Whitcroft					$sep = '';
149800df344fSAndy Whitcroft				} else {
149900df344fSAndy Whitcroft					$sep = "[...]\n";
150000df344fSAndy Whitcroft				}
150100df344fSAndy Whitcroft			}
150200df344fSAndy Whitcroft			if ($err ne '') {
15039c0ca6f9SAndy Whitcroft				ERROR("switch and case should be at the same indent\n$hereline$err");
1504de7d4f0eSAndy Whitcroft			}
1505de7d4f0eSAndy Whitcroft		}
1506de7d4f0eSAndy Whitcroft
1507de7d4f0eSAndy Whitcroft# if/while/etc brace do not go on next line, unless defining a do while loop,
1508de7d4f0eSAndy Whitcroft# or if that brace on the next line is for something else
1509c45dcabdSAndy Whitcroft		if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
1510773647a0SAndy Whitcroft			my $pre_ctx = "$1$2";
1511773647a0SAndy Whitcroft
15129c0ca6f9SAndy Whitcroft			my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
1513de7d4f0eSAndy Whitcroft			my $ctx_cnt = $realcnt - $#ctx - 1;
1514de7d4f0eSAndy Whitcroft			my $ctx = join("\n", @ctx);
1515de7d4f0eSAndy Whitcroft
1516548596d5SAndy Whitcroft			my $ctx_ln = $linenr;
1517548596d5SAndy Whitcroft			my $ctx_skip = $realcnt;
1518de7d4f0eSAndy Whitcroft
1519548596d5SAndy Whitcroft			while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
1520548596d5SAndy Whitcroft					defined $lines[$ctx_ln - 1] &&
1521548596d5SAndy Whitcroft					$lines[$ctx_ln - 1] =~ /^-/)) {
1522548596d5SAndy Whitcroft				##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
1523548596d5SAndy Whitcroft				$ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
1524773647a0SAndy Whitcroft				$ctx_ln++;
1525773647a0SAndy Whitcroft			}
1526548596d5SAndy Whitcroft
152753210168SAndy Whitcroft			#print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
152853210168SAndy Whitcroft			#print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
1529773647a0SAndy Whitcroft
1530773647a0SAndy Whitcroft			if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln -1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
1531773647a0SAndy Whitcroft				ERROR("that open brace { should be on the previous line\n" .
1532c45dcabdSAndy Whitcroft					"$here\n$ctx\n$lines[$ctx_ln - 1]\n");
153300df344fSAndy Whitcroft			}
1534773647a0SAndy Whitcroft			if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
1535773647a0SAndy Whitcroft			    $ctx =~ /\)\s*\;\s*$/ &&
1536773647a0SAndy Whitcroft			    defined $lines[$ctx_ln - 1])
1537773647a0SAndy Whitcroft			{
15389c0ca6f9SAndy Whitcroft				my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
15399c0ca6f9SAndy Whitcroft				if ($nindent > $indent) {
1540773647a0SAndy Whitcroft					WARN("trailing semicolon indicates no statements, indent implies otherwise\n" .
1541c45dcabdSAndy Whitcroft						"$here\n$ctx\n$lines[$ctx_ln - 1]\n");
15429c0ca6f9SAndy Whitcroft				}
15439c0ca6f9SAndy Whitcroft			}
154400df344fSAndy Whitcroft		}
154500df344fSAndy Whitcroft
15464d001e4dSAndy Whitcroft# Check relative indent for conditionals and blocks.
15474d001e4dSAndy Whitcroft		if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
15484d001e4dSAndy Whitcroft			my ($s, $c) = ($stat, $cond);
15494d001e4dSAndy Whitcroft
15504d001e4dSAndy Whitcroft			substr($s, 0, length($c), '');
15514d001e4dSAndy Whitcroft
15524d001e4dSAndy Whitcroft			# Make sure we remove the line prefixes as we have
15534d001e4dSAndy Whitcroft			# none on the first line, and are going to readd them
15544d001e4dSAndy Whitcroft			# where necessary.
15554d001e4dSAndy Whitcroft			$s =~ s/\n./\n/gs;
15564d001e4dSAndy Whitcroft
15574d001e4dSAndy Whitcroft			# Find out how long the conditional actually is.
15586f779c18SAndy Whitcroft			my @newlines = ($c =~ /\n/gs);
15596f779c18SAndy Whitcroft			my $cond_lines = 1 + $#newlines;
15604d001e4dSAndy Whitcroft
15614d001e4dSAndy Whitcroft			# We want to check the first line inside the block
15624d001e4dSAndy Whitcroft			# starting at the end of the conditional, so remove:
15634d001e4dSAndy Whitcroft			#  1) any blank line termination
15644d001e4dSAndy Whitcroft			#  2) any opening brace { on end of the line
15654d001e4dSAndy Whitcroft			#  3) any do (...) {
15664d001e4dSAndy Whitcroft			my $continuation = 0;
15674d001e4dSAndy Whitcroft			my $check = 0;
15684d001e4dSAndy Whitcroft			$s =~ s/^.*\bdo\b//;
15694d001e4dSAndy Whitcroft			$s =~ s/^\s*{//;
15704d001e4dSAndy Whitcroft			if ($s =~ s/^\s*\\//) {
15714d001e4dSAndy Whitcroft				$continuation = 1;
15724d001e4dSAndy Whitcroft			}
15739bd49efeSAndy Whitcroft			if ($s =~ s/^\s*?\n//) {
15744d001e4dSAndy Whitcroft				$check = 1;
15754d001e4dSAndy Whitcroft				$cond_lines++;
15764d001e4dSAndy Whitcroft			}
15774d001e4dSAndy Whitcroft
15784d001e4dSAndy Whitcroft			# Also ignore a loop construct at the end of a
15794d001e4dSAndy Whitcroft			# preprocessor statement.
15804d001e4dSAndy Whitcroft			if (($prevline =~ /^.\s*#\s*define\s/ ||
15814d001e4dSAndy Whitcroft			    $prevline =~ /\\\s*$/) && $continuation == 0) {
15824d001e4dSAndy Whitcroft				$check = 0;
15834d001e4dSAndy Whitcroft			}
15844d001e4dSAndy Whitcroft
15859bd49efeSAndy Whitcroft			my $cond_ptr = -1;
1586740504c6SAndy Whitcroft			$continuation = 0;
15879bd49efeSAndy Whitcroft			while ($cond_ptr != $cond_lines) {
15889bd49efeSAndy Whitcroft				$cond_ptr = $cond_lines;
15894d001e4dSAndy Whitcroft
1590f16fa28fSAndy Whitcroft				# If we see an #else/#elif then the code
1591f16fa28fSAndy Whitcroft				# is not linear.
1592f16fa28fSAndy Whitcroft				if ($s =~ /^\s*\#\s*(?:else|elif)/) {
1593f16fa28fSAndy Whitcroft					$check = 0;
1594f16fa28fSAndy Whitcroft				}
1595f16fa28fSAndy Whitcroft
15969bd49efeSAndy Whitcroft				# Ignore:
15979bd49efeSAndy Whitcroft				#  1) blank lines, they should be at 0,
15989bd49efeSAndy Whitcroft				#  2) preprocessor lines, and
15999bd49efeSAndy Whitcroft				#  3) labels.
1600740504c6SAndy Whitcroft				if ($continuation ||
1601740504c6SAndy Whitcroft				    $s =~ /^\s*?\n/ ||
16029bd49efeSAndy Whitcroft				    $s =~ /^\s*#\s*?/ ||
16039bd49efeSAndy Whitcroft				    $s =~ /^\s*$Ident\s*:/) {
1604740504c6SAndy Whitcroft					$continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
160530dad6ebSAndy Whitcroft					if ($s =~ s/^.*?\n//) {
16069bd49efeSAndy Whitcroft						$cond_lines++;
16079bd49efeSAndy Whitcroft					}
16084d001e4dSAndy Whitcroft				}
160930dad6ebSAndy Whitcroft			}
16104d001e4dSAndy Whitcroft
16114d001e4dSAndy Whitcroft			my (undef, $sindent) = line_stats("+" . $s);
16124d001e4dSAndy Whitcroft			my $stat_real = raw_line($linenr, $cond_lines);
16134d001e4dSAndy Whitcroft
16144d001e4dSAndy Whitcroft			# Check if either of these lines are modified, else
16154d001e4dSAndy Whitcroft			# this is not this patch's fault.
16164d001e4dSAndy Whitcroft			if (!defined($stat_real) ||
16174d001e4dSAndy Whitcroft			    $stat !~ /^\+/ && $stat_real !~ /^\+/) {
16184d001e4dSAndy Whitcroft				$check = 0;
16194d001e4dSAndy Whitcroft			}
16204d001e4dSAndy Whitcroft			if (defined($stat_real) && $cond_lines > 1) {
16214d001e4dSAndy Whitcroft				$stat_real = "[...]\n$stat_real";
16224d001e4dSAndy Whitcroft			}
16234d001e4dSAndy Whitcroft
16249bd49efeSAndy 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";
16254d001e4dSAndy Whitcroft
16264d001e4dSAndy Whitcroft			if ($check && (($sindent % 8) != 0 ||
16274d001e4dSAndy Whitcroft			    ($sindent <= $indent && $s ne ''))) {
16284d001e4dSAndy Whitcroft				WARN("suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
16294d001e4dSAndy Whitcroft			}
16304d001e4dSAndy Whitcroft		}
16314d001e4dSAndy Whitcroft
16326c72ffaaSAndy Whitcroft		# Track the 'values' across context and added lines.
16336c72ffaaSAndy Whitcroft		my $opline = $line; $opline =~ s/^./ /;
16341f65f947SAndy Whitcroft		my ($curr_values, $curr_vars) =
16351f65f947SAndy Whitcroft				annotate_values($opline . "\n", $prev_values);
16366c72ffaaSAndy Whitcroft		$curr_values = $prev_values . $curr_values;
1637c2fdda0dSAndy Whitcroft		if ($dbg_values) {
1638c2fdda0dSAndy Whitcroft			my $outline = $opline; $outline =~ s/\t/ /g;
1639cf655043SAndy Whitcroft			print "$linenr > .$outline\n";
1640cf655043SAndy Whitcroft			print "$linenr > $curr_values\n";
16411f65f947SAndy Whitcroft			print "$linenr >  $curr_vars\n";
1642c2fdda0dSAndy Whitcroft		}
16436c72ffaaSAndy Whitcroft		$prev_values = substr($curr_values, -1);
16446c72ffaaSAndy Whitcroft
164500df344fSAndy Whitcroft#ignore lines not being added
164600df344fSAndy Whitcroft		if ($line=~/^[^\+]/) {next;}
164700df344fSAndy Whitcroft
1648653d4876SAndy Whitcroft# TEST: allow direct testing of the type matcher.
16497429c690SAndy Whitcroft		if ($dbg_type) {
16507429c690SAndy Whitcroft			if ($line =~ /^.\s*$Declare\s*$/) {
16517429c690SAndy Whitcroft				ERROR("TEST: is type\n" . $herecurr);
16527429c690SAndy Whitcroft			} elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
16537429c690SAndy Whitcroft				ERROR("TEST: is not type ($1 is)\n". $herecurr);
16547429c690SAndy Whitcroft			}
1655653d4876SAndy Whitcroft			next;
1656653d4876SAndy Whitcroft		}
1657a1ef277eSAndy Whitcroft# TEST: allow direct testing of the attribute matcher.
1658a1ef277eSAndy Whitcroft		if ($dbg_attr) {
16599360b0e5SAndy Whitcroft			if ($line =~ /^.\s*$Modifier\s*$/) {
1660a1ef277eSAndy Whitcroft				ERROR("TEST: is attr\n" . $herecurr);
16619360b0e5SAndy Whitcroft			} elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
1662a1ef277eSAndy Whitcroft				ERROR("TEST: is not attr ($1 is)\n". $herecurr);
1663a1ef277eSAndy Whitcroft			}
1664a1ef277eSAndy Whitcroft			next;
1665a1ef277eSAndy Whitcroft		}
1666653d4876SAndy Whitcroft
1667f0a594c1SAndy Whitcroft# check for initialisation to aggregates open brace on the next line
1668f0a594c1SAndy Whitcroft		if ($prevline =~ /$Declare\s*$Ident\s*=\s*$/ &&
1669f0a594c1SAndy Whitcroft		    $line =~ /^.\s*{/) {
1670773647a0SAndy Whitcroft			ERROR("that open brace { should be on the previous line\n" . $hereprev);
1671f0a594c1SAndy Whitcroft		}
1672f0a594c1SAndy Whitcroft
167300df344fSAndy Whitcroft#
167400df344fSAndy Whitcroft# Checks which are anchored on the added line.
167500df344fSAndy Whitcroft#
167600df344fSAndy Whitcroft
1677653d4876SAndy Whitcroft# check for malformed paths in #include statements (uses RAW line)
1678c45dcabdSAndy Whitcroft		if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
1679653d4876SAndy Whitcroft			my $path = $1;
1680653d4876SAndy Whitcroft			if ($path =~ m{//}) {
1681de7d4f0eSAndy Whitcroft				ERROR("malformed #include filename\n" .
1682de7d4f0eSAndy Whitcroft					$herecurr);
1683653d4876SAndy Whitcroft			}
1684653d4876SAndy Whitcroft		}
1685653d4876SAndy Whitcroft
168600df344fSAndy Whitcroft# no C99 // comments
168700df344fSAndy Whitcroft		if ($line =~ m{//}) {
1688de7d4f0eSAndy Whitcroft			ERROR("do not use C99 // comments\n" . $herecurr);
168900df344fSAndy Whitcroft		}
169000df344fSAndy Whitcroft		# Remove C99 comments.
16910a920b5bSAndy Whitcroft		$line =~ s@//.*@@;
16926c72ffaaSAndy Whitcroft		$opline =~ s@//.*@@;
16930a920b5bSAndy Whitcroft
16940a920b5bSAndy Whitcroft#EXPORT_SYMBOL should immediately follow its function closing }.
1695653d4876SAndy Whitcroft		if (($line =~ /EXPORT_SYMBOL.*\((.*)\)/) ||
1696653d4876SAndy Whitcroft		    ($line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
1697653d4876SAndy Whitcroft			my $name = $1;
169848012058SAndy Whitcroft			if ($prevline !~ /(?:
169948012058SAndy Whitcroft				^.}|
170048012058SAndy Whitcroft				^.DEFINE_$Ident\(\Q$name\E\)|
170148012058SAndy Whitcroft				^.DECLARE_$Ident\(\Q$name\E\)|
170248012058SAndy Whitcroft				^.LIST_HEAD\(\Q$name\E\)|
170348012058SAndy Whitcroft				^.$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
170448012058SAndy Whitcroft				\b\Q$name\E(?:\s+$Attribute)?\s*(?:;|=|\[)
170548012058SAndy Whitcroft			    )/x) {
1706de7d4f0eSAndy Whitcroft				WARN("EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
17070a920b5bSAndy Whitcroft			}
17080a920b5bSAndy Whitcroft		}
17090a920b5bSAndy Whitcroft
1710f0a594c1SAndy Whitcroft# check for external initialisers.
1711c45dcabdSAndy Whitcroft		if ($line =~ /^.$Type\s*$Ident\s*(?:\s+$Modifier)*\s*=\s*(0|NULL|false)\s*;/) {
1712f0a594c1SAndy Whitcroft			ERROR("do not initialise externals to 0 or NULL\n" .
1713f0a594c1SAndy Whitcroft				$herecurr);
1714f0a594c1SAndy Whitcroft		}
17150a920b5bSAndy Whitcroft# check for static initialisers.
17162d1bafd7SAndy Whitcroft		if ($line =~ /\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
1717de7d4f0eSAndy Whitcroft			ERROR("do not initialise statics to 0 or NULL\n" .
1718de7d4f0eSAndy Whitcroft				$herecurr);
17190a920b5bSAndy Whitcroft		}
17200a920b5bSAndy Whitcroft
1721653d4876SAndy Whitcroft# check for new typedefs, only function parameters and sparse annotations
1722653d4876SAndy Whitcroft# make sense.
1723653d4876SAndy Whitcroft		if ($line =~ /\btypedef\s/ &&
17248054576dSAndy Whitcroft		    $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
1725c45dcabdSAndy Whitcroft		    $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
17268ed22cadSAndy Whitcroft		    $line !~ /\b$typeTypedefs\b/ &&
1727653d4876SAndy Whitcroft		    $line !~ /\b__bitwise(?:__|)\b/) {
1728de7d4f0eSAndy Whitcroft			WARN("do not add new typedefs\n" . $herecurr);
17290a920b5bSAndy Whitcroft		}
17300a920b5bSAndy Whitcroft
17310a920b5bSAndy Whitcroft# * goes on variable not on type
173265863862SAndy Whitcroft		# (char*[ const])
173300ef4eceSAndy Whitcroft		if ($line =~ m{\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\)}) {
173465863862SAndy Whitcroft			my ($from, $to) = ($1, $1);
1735d8aaf121SAndy Whitcroft
173665863862SAndy Whitcroft			# Should start with a space.
173765863862SAndy Whitcroft			$to =~ s/^(\S)/ $1/;
173865863862SAndy Whitcroft			# Should not end with a space.
173965863862SAndy Whitcroft			$to =~ s/\s+$//;
174065863862SAndy Whitcroft			# '*'s should not have spaces between.
1741f9a0b3d1SAndy Whitcroft			while ($to =~ s/\*\s+\*/\*\*/) {
174265863862SAndy Whitcroft			}
1743d8aaf121SAndy Whitcroft
174465863862SAndy Whitcroft			#print "from<$from> to<$to>\n";
174565863862SAndy Whitcroft			if ($from ne $to) {
174665863862SAndy Whitcroft				ERROR("\"(foo$from)\" should be \"(foo$to)\"\n" .  $herecurr);
174765863862SAndy Whitcroft			}
174800ef4eceSAndy Whitcroft		} elsif ($line =~ m{\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident)}) {
174965863862SAndy Whitcroft			my ($from, $to, $ident) = ($1, $1, $2);
1750d8aaf121SAndy Whitcroft
175165863862SAndy Whitcroft			# Should start with a space.
175265863862SAndy Whitcroft			$to =~ s/^(\S)/ $1/;
175365863862SAndy Whitcroft			# Should not end with a space.
175465863862SAndy Whitcroft			$to =~ s/\s+$//;
175565863862SAndy Whitcroft			# '*'s should not have spaces between.
1756f9a0b3d1SAndy Whitcroft			while ($to =~ s/\*\s+\*/\*\*/) {
175765863862SAndy Whitcroft			}
175865863862SAndy Whitcroft			# Modifiers should have spaces.
175965863862SAndy Whitcroft			$to =~ s/(\b$Modifier$)/$1 /;
176065863862SAndy Whitcroft
1761667026e7SAndy Whitcroft			#print "from<$from> to<$to> ident<$ident>\n";
1762667026e7SAndy Whitcroft			if ($from ne $to && $ident !~ /^$Modifier$/) {
176365863862SAndy Whitcroft				ERROR("\"foo${from}bar\" should be \"foo${to}bar\"\n" .  $herecurr);
176465863862SAndy Whitcroft			}
17650a920b5bSAndy Whitcroft		}
17660a920b5bSAndy Whitcroft
17670a920b5bSAndy Whitcroft# # no BUG() or BUG_ON()
17680a920b5bSAndy Whitcroft# 		if ($line =~ /\b(BUG|BUG_ON)\b/) {
17690a920b5bSAndy Whitcroft# 			print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
17700a920b5bSAndy Whitcroft# 			print "$herecurr";
17710a920b5bSAndy Whitcroft# 			$clean = 0;
17720a920b5bSAndy Whitcroft# 		}
17730a920b5bSAndy Whitcroft
17748905a67cSAndy Whitcroft		if ($line =~ /\bLINUX_VERSION_CODE\b/) {
1775c2fdda0dSAndy Whitcroft			WARN("LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
17768905a67cSAndy Whitcroft		}
17778905a67cSAndy Whitcroft
177800df344fSAndy Whitcroft# printk should use KERN_* levels.  Note that follow on printk's on the
177900df344fSAndy Whitcroft# same line do not need a level, so we use the current block context
178000df344fSAndy Whitcroft# to try and find and validate the current printk.  In summary the current
178100df344fSAndy Whitcroft# printk includes all preceeding printk's which have no newline on the end.
178200df344fSAndy Whitcroft# we assume the first bad printk is the one to report.
1783f0a594c1SAndy Whitcroft		if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
178400df344fSAndy Whitcroft			my $ok = 0;
178500df344fSAndy Whitcroft			for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
178600df344fSAndy Whitcroft				#print "CHECK<$lines[$ln - 1]\n";
178700df344fSAndy Whitcroft				# we have a preceeding printk if it ends
178800df344fSAndy Whitcroft				# with "\n" ignore it, else it is to blame
178900df344fSAndy Whitcroft				if ($lines[$ln - 1] =~ m{\bprintk\(}) {
179000df344fSAndy Whitcroft					if ($rawlines[$ln - 1] !~ m{\\n"}) {
179100df344fSAndy Whitcroft						$ok = 1;
179200df344fSAndy Whitcroft					}
179300df344fSAndy Whitcroft					last;
179400df344fSAndy Whitcroft				}
179500df344fSAndy Whitcroft			}
179600df344fSAndy Whitcroft			if ($ok == 0) {
1797de7d4f0eSAndy Whitcroft				WARN("printk() should include KERN_ facility level\n" . $herecurr);
17980a920b5bSAndy Whitcroft			}
179900df344fSAndy Whitcroft		}
18000a920b5bSAndy Whitcroft
1801653d4876SAndy Whitcroft# function brace can't be on same line, except for #defines of do while,
1802653d4876SAndy Whitcroft# or if closed on same line
1803c45dcabdSAndy Whitcroft		if (($line=~/$Type\s*$Ident\(.*\).*\s{/) and
1804c45dcabdSAndy Whitcroft		    !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) {
1805de7d4f0eSAndy Whitcroft			ERROR("open brace '{' following function declarations go on the next line\n" . $herecurr);
18060a920b5bSAndy Whitcroft		}
1807653d4876SAndy Whitcroft
18088905a67cSAndy Whitcroft# open braces for enum, union and struct go on the same line.
18098905a67cSAndy Whitcroft		if ($line =~ /^.\s*{/ &&
18108905a67cSAndy Whitcroft		    $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
18118905a67cSAndy Whitcroft			ERROR("open brace '{' following $1 go on the same line\n" . $hereprev);
18128905a67cSAndy Whitcroft		}
18138905a67cSAndy Whitcroft
18148d31cfceSAndy Whitcroft# check for spacing round square brackets; allowed:
18158d31cfceSAndy Whitcroft#  1. with a type on the left -- int [] a;
1816fe2a7dbcSAndy Whitcroft#  2. at the beginning of a line for slice initialisers -- [0...10] = 5,
1817fe2a7dbcSAndy Whitcroft#  3. inside a curly brace -- = { [0...10] = 5 }
18188d31cfceSAndy Whitcroft		while ($line =~ /(.*?\s)\[/g) {
18198d31cfceSAndy Whitcroft			my ($where, $prefix) = ($-[1], $1);
18208d31cfceSAndy Whitcroft			if ($prefix !~ /$Type\s+$/ &&
1821fe2a7dbcSAndy Whitcroft			    ($where != 0 || $prefix !~ /^.\s+$/) &&
1822fe2a7dbcSAndy Whitcroft			    $prefix !~ /{\s+$/) {
18238d31cfceSAndy Whitcroft				ERROR("space prohibited before open square bracket '['\n" . $herecurr);
18248d31cfceSAndy Whitcroft			}
18258d31cfceSAndy Whitcroft		}
18268d31cfceSAndy Whitcroft
1827f0a594c1SAndy Whitcroft# check for spaces between functions and their parentheses.
18286c72ffaaSAndy Whitcroft		while ($line =~ /($Ident)\s+\(/g) {
1829c2fdda0dSAndy Whitcroft			my $name = $1;
1830773647a0SAndy Whitcroft			my $ctx_before = substr($line, 0, $-[1]);
1831773647a0SAndy Whitcroft			my $ctx = "$ctx_before$name";
1832c2fdda0dSAndy Whitcroft
1833c2fdda0dSAndy Whitcroft			# Ignore those directives where spaces _are_ permitted.
1834773647a0SAndy Whitcroft			if ($name =~ /^(?:
1835773647a0SAndy Whitcroft				if|for|while|switch|return|case|
1836773647a0SAndy Whitcroft				volatile|__volatile__|
1837773647a0SAndy Whitcroft				__attribute__|format|__extension__|
1838773647a0SAndy Whitcroft				asm|__asm__)$/x)
1839773647a0SAndy Whitcroft			{
1840c2fdda0dSAndy Whitcroft
1841c2fdda0dSAndy Whitcroft			# cpp #define statements have non-optional spaces, ie
1842c2fdda0dSAndy Whitcroft			# if there is a space between the name and the open
1843c2fdda0dSAndy Whitcroft			# parenthesis it is simply not a parameter group.
1844c45dcabdSAndy Whitcroft			} elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
1845773647a0SAndy Whitcroft
1846773647a0SAndy Whitcroft			# cpp #elif statement condition may start with a (
1847c45dcabdSAndy Whitcroft			} elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
1848c2fdda0dSAndy Whitcroft
1849c2fdda0dSAndy Whitcroft			# If this whole things ends with a type its most
1850c2fdda0dSAndy Whitcroft			# likely a typedef for a function.
1851773647a0SAndy Whitcroft			} elsif ($ctx =~ /$Type$/) {
1852c2fdda0dSAndy Whitcroft
1853c2fdda0dSAndy Whitcroft			} else {
1854773647a0SAndy Whitcroft				WARN("space prohibited between function name and open parenthesis '('\n" . $herecurr);
1855f0a594c1SAndy Whitcroft			}
18566c72ffaaSAndy Whitcroft		}
1857653d4876SAndy Whitcroft# Check operator spacing.
18580a920b5bSAndy Whitcroft		if (!($line=~/\#\s*include/)) {
18599c0ca6f9SAndy Whitcroft			my $ops = qr{
18609c0ca6f9SAndy Whitcroft				<<=|>>=|<=|>=|==|!=|
18619c0ca6f9SAndy Whitcroft				\+=|-=|\*=|\/=|%=|\^=|\|=|&=|
18629c0ca6f9SAndy Whitcroft				=>|->|<<|>>|<|>|=|!|~|
18631f65f947SAndy Whitcroft				&&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
18641f65f947SAndy Whitcroft				\?|:
18659c0ca6f9SAndy Whitcroft			}x;
1866cf655043SAndy Whitcroft			my @elements = split(/($ops|;)/, $opline);
186700df344fSAndy Whitcroft			my $off = 0;
18686c72ffaaSAndy Whitcroft
18696c72ffaaSAndy Whitcroft			my $blank = copy_spacing($opline);
18706c72ffaaSAndy Whitcroft
18710a920b5bSAndy Whitcroft			for (my $n = 0; $n < $#elements; $n += 2) {
18724a0df2efSAndy Whitcroft				$off += length($elements[$n]);
18734a0df2efSAndy Whitcroft
1874773647a0SAndy Whitcroft				# Pick up the preceeding and succeeding characters.
1875773647a0SAndy Whitcroft				my $ca = substr($opline, 0, $off);
1876773647a0SAndy Whitcroft				my $cc = '';
1877773647a0SAndy Whitcroft				if (length($opline) >= ($off + length($elements[$n + 1]))) {
1878773647a0SAndy Whitcroft					$cc = substr($opline, $off + length($elements[$n + 1]));
1879773647a0SAndy Whitcroft				}
1880773647a0SAndy Whitcroft				my $cb = "$ca$;$cc";
1881773647a0SAndy Whitcroft
18824a0df2efSAndy Whitcroft				my $a = '';
18834a0df2efSAndy Whitcroft				$a = 'V' if ($elements[$n] ne '');
18844a0df2efSAndy Whitcroft				$a = 'W' if ($elements[$n] =~ /\s$/);
1885cf655043SAndy Whitcroft				$a = 'C' if ($elements[$n] =~ /$;$/);
18864a0df2efSAndy Whitcroft				$a = 'B' if ($elements[$n] =~ /(\[|\()$/);
18874a0df2efSAndy Whitcroft				$a = 'O' if ($elements[$n] eq '');
1888773647a0SAndy Whitcroft				$a = 'E' if ($ca =~ /^\s*$/);
18894a0df2efSAndy Whitcroft
18900a920b5bSAndy Whitcroft				my $op = $elements[$n + 1];
18914a0df2efSAndy Whitcroft
18924a0df2efSAndy Whitcroft				my $c = '';
18930a920b5bSAndy Whitcroft				if (defined $elements[$n + 2]) {
18944a0df2efSAndy Whitcroft					$c = 'V' if ($elements[$n + 2] ne '');
18954a0df2efSAndy Whitcroft					$c = 'W' if ($elements[$n + 2] =~ /^\s/);
1896cf655043SAndy Whitcroft					$c = 'C' if ($elements[$n + 2] =~ /^$;/);
18974a0df2efSAndy Whitcroft					$c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
18984a0df2efSAndy Whitcroft					$c = 'O' if ($elements[$n + 2] eq '');
18998b1b3378SAndy Whitcroft					$c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
19004a0df2efSAndy Whitcroft				} else {
19014a0df2efSAndy Whitcroft					$c = 'E';
19020a920b5bSAndy Whitcroft				}
19030a920b5bSAndy Whitcroft
19044a0df2efSAndy Whitcroft				my $ctx = "${a}x${c}";
19054a0df2efSAndy Whitcroft
19064a0df2efSAndy Whitcroft				my $at = "(ctx:$ctx)";
19074a0df2efSAndy Whitcroft
19086c72ffaaSAndy Whitcroft				my $ptr = substr($blank, 0, $off) . "^";
1909de7d4f0eSAndy Whitcroft				my $hereptr = "$hereline$ptr\n";
19100a920b5bSAndy Whitcroft
191174048ed8SAndy Whitcroft				# Pull out the value of this operator.
19126c72ffaaSAndy Whitcroft				my $op_type = substr($curr_values, $off + 1, 1);
19130a920b5bSAndy Whitcroft
19141f65f947SAndy Whitcroft				# Get the full operator variant.
19151f65f947SAndy Whitcroft				my $opv = $op . substr($curr_vars, $off, 1);
19161f65f947SAndy Whitcroft
191713214adfSAndy Whitcroft				# Ignore operators passed as parameters.
191813214adfSAndy Whitcroft				if ($op_type ne 'V' &&
191913214adfSAndy Whitcroft				    $ca =~ /\s$/ && $cc =~ /^\s*,/) {
192013214adfSAndy Whitcroft
1921cf655043SAndy Whitcroft#				# Ignore comments
1922cf655043SAndy Whitcroft#				} elsif ($op =~ /^$;+$/) {
192313214adfSAndy Whitcroft
1924d8aaf121SAndy Whitcroft				# ; should have either the end of line or a space or \ after it
192513214adfSAndy Whitcroft				} elsif ($op eq ';') {
1926cf655043SAndy Whitcroft					if ($ctx !~ /.x[WEBC]/ &&
1927cf655043SAndy Whitcroft					    $cc !~ /^\\/ && $cc !~ /^;/) {
1928773647a0SAndy Whitcroft						ERROR("space required after that '$op' $at\n" . $hereptr);
1929d8aaf121SAndy Whitcroft					}
1930d8aaf121SAndy Whitcroft
1931d8aaf121SAndy Whitcroft				# // is a comment
1932d8aaf121SAndy Whitcroft				} elsif ($op eq '//') {
19330a920b5bSAndy Whitcroft
19341f65f947SAndy Whitcroft				# No spaces for:
19351f65f947SAndy Whitcroft				#   ->
19361f65f947SAndy Whitcroft				#   :   when part of a bitfield
19371f65f947SAndy Whitcroft				} elsif ($op eq '->' || $opv eq ':B') {
19384a0df2efSAndy Whitcroft					if ($ctx =~ /Wx.|.xW/) {
1939773647a0SAndy Whitcroft						ERROR("spaces prohibited around that '$op' $at\n" . $hereptr);
19400a920b5bSAndy Whitcroft					}
19410a920b5bSAndy Whitcroft
19420a920b5bSAndy Whitcroft				# , must have a space on the right.
19430a920b5bSAndy Whitcroft				} elsif ($op eq ',') {
1944cf655043SAndy Whitcroft					if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
1945773647a0SAndy Whitcroft						ERROR("space required after that '$op' $at\n" . $hereptr);
19460a920b5bSAndy Whitcroft					}
19470a920b5bSAndy Whitcroft
19489c0ca6f9SAndy Whitcroft				# '*' as part of a type definition -- reported already.
194974048ed8SAndy Whitcroft				} elsif ($opv eq '*_') {
19509c0ca6f9SAndy Whitcroft					#warn "'*' is part of type\n";
19519c0ca6f9SAndy Whitcroft
19529c0ca6f9SAndy Whitcroft				# unary operators should have a space before and
19539c0ca6f9SAndy Whitcroft				# none after.  May be left adjacent to another
19549c0ca6f9SAndy Whitcroft				# unary operator, or a cast
19559c0ca6f9SAndy Whitcroft				} elsif ($op eq '!' || $op eq '~' ||
195674048ed8SAndy Whitcroft					 $opv eq '*U' || $opv eq '-U' ||
19570d413866SAndy Whitcroft					 $opv eq '&U' || $opv eq '&&U') {
1958cf655043SAndy Whitcroft					if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
1959773647a0SAndy Whitcroft						ERROR("space required before that '$op' $at\n" . $hereptr);
19600a920b5bSAndy Whitcroft					}
1961a3340b35SAndy Whitcroft					if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
1962171ae1a4SAndy Whitcroft						# A unary '*' may be const
1963171ae1a4SAndy Whitcroft
1964171ae1a4SAndy Whitcroft					} elsif ($ctx =~ /.xW/) {
1965fb9e9096SAndy Whitcroft						ERROR("space prohibited after that '$op' $at\n" . $hereptr);
19660a920b5bSAndy Whitcroft					}
19670a920b5bSAndy Whitcroft
19680a920b5bSAndy Whitcroft				# unary ++ and unary -- are allowed no space on one side.
19690a920b5bSAndy Whitcroft				} elsif ($op eq '++' or $op eq '--') {
1970773647a0SAndy Whitcroft					if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
1971773647a0SAndy Whitcroft						ERROR("space required one side of that '$op' $at\n" . $hereptr);
19720a920b5bSAndy Whitcroft					}
1973773647a0SAndy Whitcroft					if ($ctx =~ /Wx[BE]/ ||
1974773647a0SAndy Whitcroft					    ($ctx =~ /Wx./ && $cc =~ /^;/)) {
1975773647a0SAndy Whitcroft						ERROR("space prohibited before that '$op' $at\n" . $hereptr);
1976653d4876SAndy Whitcroft					}
1977773647a0SAndy Whitcroft					if ($ctx =~ /ExW/) {
1978773647a0SAndy Whitcroft						ERROR("space prohibited after that '$op' $at\n" . $hereptr);
1979773647a0SAndy Whitcroft					}
1980773647a0SAndy Whitcroft
19810a920b5bSAndy Whitcroft
19820a920b5bSAndy Whitcroft				# << and >> may either have or not have spaces both sides
19839c0ca6f9SAndy Whitcroft				} elsif ($op eq '<<' or $op eq '>>' or
19849c0ca6f9SAndy Whitcroft					 $op eq '&' or $op eq '^' or $op eq '|' or
19859c0ca6f9SAndy Whitcroft					 $op eq '+' or $op eq '-' or
1986c2fdda0dSAndy Whitcroft					 $op eq '*' or $op eq '/' or
1987c2fdda0dSAndy Whitcroft					 $op eq '%')
19880a920b5bSAndy Whitcroft				{
1989773647a0SAndy Whitcroft					if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
1990de7d4f0eSAndy Whitcroft						ERROR("need consistent spacing around '$op' $at\n" .
1991de7d4f0eSAndy Whitcroft							$hereptr);
19920a920b5bSAndy Whitcroft					}
19930a920b5bSAndy Whitcroft
19941f65f947SAndy Whitcroft				# A colon needs no spaces before when it is
19951f65f947SAndy Whitcroft				# terminating a case value or a label.
19961f65f947SAndy Whitcroft				} elsif ($opv eq ':C' || $opv eq ':L') {
19971f65f947SAndy Whitcroft					if ($ctx =~ /Wx./) {
19981f65f947SAndy Whitcroft						ERROR("space prohibited before that '$op' $at\n" . $hereptr);
19991f65f947SAndy Whitcroft					}
20001f65f947SAndy Whitcroft
20010a920b5bSAndy Whitcroft				# All the others need spaces both sides.
2002cf655043SAndy Whitcroft				} elsif ($ctx !~ /[EWC]x[CWE]/) {
20031f65f947SAndy Whitcroft					my $ok = 0;
20041f65f947SAndy Whitcroft
200522f2a2efSAndy Whitcroft					# Ignore email addresses <foo@bar>
20061f65f947SAndy Whitcroft					if (($op eq '<' &&
20071f65f947SAndy Whitcroft					     $cc =~ /^\S+\@\S+>/) ||
20081f65f947SAndy Whitcroft					    ($op eq '>' &&
20091f65f947SAndy Whitcroft					     $ca =~ /<\S+\@\S+$/))
20101f65f947SAndy Whitcroft					{
20111f65f947SAndy Whitcroft					    	$ok = 1;
20121f65f947SAndy Whitcroft					}
20131f65f947SAndy Whitcroft
20141f65f947SAndy Whitcroft					# Ignore ?:
20151f65f947SAndy Whitcroft					if (($opv eq ':O' && $ca =~ /\?$/) ||
20161f65f947SAndy Whitcroft					    ($op eq '?' && $cc =~ /^:/)) {
20171f65f947SAndy Whitcroft					    	$ok = 1;
20181f65f947SAndy Whitcroft					}
20191f65f947SAndy Whitcroft
20201f65f947SAndy Whitcroft					if ($ok == 0) {
2021773647a0SAndy Whitcroft						ERROR("spaces required around that '$op' $at\n" . $hereptr);
20220a920b5bSAndy Whitcroft					}
202322f2a2efSAndy Whitcroft				}
20244a0df2efSAndy Whitcroft				$off += length($elements[$n + 1]);
20250a920b5bSAndy Whitcroft			}
20260a920b5bSAndy Whitcroft		}
20270a920b5bSAndy Whitcroft
2028f0a594c1SAndy Whitcroft# check for multiple assignments
2029f0a594c1SAndy Whitcroft		if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
20306c72ffaaSAndy Whitcroft			CHK("multiple assignments should be avoided\n" . $herecurr);
2031f0a594c1SAndy Whitcroft		}
2032f0a594c1SAndy Whitcroft
203322f2a2efSAndy Whitcroft## # check for multiple declarations, allowing for a function declaration
203422f2a2efSAndy Whitcroft## # continuation.
203522f2a2efSAndy Whitcroft## 		if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
203622f2a2efSAndy Whitcroft## 		    $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
203722f2a2efSAndy Whitcroft##
203822f2a2efSAndy Whitcroft## 			# Remove any bracketed sections to ensure we do not
203922f2a2efSAndy Whitcroft## 			# falsly report the parameters of functions.
204022f2a2efSAndy Whitcroft## 			my $ln = $line;
204122f2a2efSAndy Whitcroft## 			while ($ln =~ s/\([^\(\)]*\)//g) {
204222f2a2efSAndy Whitcroft## 			}
204322f2a2efSAndy Whitcroft## 			if ($ln =~ /,/) {
204422f2a2efSAndy Whitcroft## 				WARN("declaring multiple variables together should be avoided\n" . $herecurr);
204522f2a2efSAndy Whitcroft## 			}
204622f2a2efSAndy Whitcroft## 		}
2047f0a594c1SAndy Whitcroft
20480a920b5bSAndy Whitcroft#need space before brace following if, while, etc
204922f2a2efSAndy Whitcroft		if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
205022f2a2efSAndy Whitcroft		    $line =~ /do{/) {
2051773647a0SAndy Whitcroft			ERROR("space required before the open brace '{'\n" . $herecurr);
2052de7d4f0eSAndy Whitcroft		}
2053de7d4f0eSAndy Whitcroft
2054de7d4f0eSAndy Whitcroft# closing brace should have a space following it when it has anything
2055de7d4f0eSAndy Whitcroft# on the line
2056de7d4f0eSAndy Whitcroft		if ($line =~ /}(?!(?:,|;|\)))\S/) {
2057773647a0SAndy Whitcroft			ERROR("space required after that close brace '}'\n" . $herecurr);
20580a920b5bSAndy Whitcroft		}
20590a920b5bSAndy Whitcroft
206022f2a2efSAndy Whitcroft# check spacing on square brackets
206122f2a2efSAndy Whitcroft		if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
2062773647a0SAndy Whitcroft			ERROR("space prohibited after that open square bracket '['\n" . $herecurr);
206322f2a2efSAndy Whitcroft		}
206422f2a2efSAndy Whitcroft		if ($line =~ /\s\]/) {
2065773647a0SAndy Whitcroft			ERROR("space prohibited before that close square bracket ']'\n" . $herecurr);
206622f2a2efSAndy Whitcroft		}
206722f2a2efSAndy Whitcroft
2068c45dcabdSAndy Whitcroft# check spacing on parentheses
20699c0ca6f9SAndy Whitcroft		if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
20709c0ca6f9SAndy Whitcroft		    $line !~ /for\s*\(\s+;/) {
2071773647a0SAndy Whitcroft			ERROR("space prohibited after that open parenthesis '('\n" . $herecurr);
207222f2a2efSAndy Whitcroft		}
207313214adfSAndy Whitcroft		if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
2074c45dcabdSAndy Whitcroft		    $line !~ /for\s*\(.*;\s+\)/ &&
2075c45dcabdSAndy Whitcroft		    $line !~ /:\s+\)/) {
2076773647a0SAndy Whitcroft			ERROR("space prohibited before that close parenthesis ')'\n" . $herecurr);
207722f2a2efSAndy Whitcroft		}
207822f2a2efSAndy Whitcroft
20790a920b5bSAndy Whitcroft#goto labels aren't indented, allow a single space however
20804a0df2efSAndy Whitcroft		if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
20810a920b5bSAndy Whitcroft		   !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
2082de7d4f0eSAndy Whitcroft			WARN("labels should not be indented\n" . $herecurr);
20830a920b5bSAndy Whitcroft		}
20840a920b5bSAndy Whitcroft
2085c45dcabdSAndy Whitcroft# Return is not a function.
2086c45dcabdSAndy Whitcroft		if (defined($stat) && $stat =~ /^.\s*return(\s*)(\(.*);/s) {
2087c45dcabdSAndy Whitcroft			my $spacing = $1;
2088c45dcabdSAndy Whitcroft			my $value = $2;
2089c45dcabdSAndy Whitcroft
209086f9d059SAndy Whitcroft			# Flatten any parentheses
2091fee61c47SAndy Whitcroft			$value =~ s/\)\(/\) \(/g;
209263f17f89SAndy Whitcroft			while ($value =~ s/\[[^\{\}]*\]/1/ ||
209363f17f89SAndy Whitcroft			       $value !~ /(?:$Ident|-?$Constant)\s*
209463f17f89SAndy Whitcroft					     $Compare\s*
209563f17f89SAndy Whitcroft					     (?:$Ident|-?$Constant)/x &&
209663f17f89SAndy Whitcroft			       $value =~ s/\([^\(\)]*\)/1/) {
2097c45dcabdSAndy Whitcroft			}
2098c45dcabdSAndy Whitcroft
2099c45dcabdSAndy Whitcroft			if ($value =~ /^(?:$Ident|-?$Constant)$/) {
2100c45dcabdSAndy Whitcroft				ERROR("return is not a function, parentheses are not required\n" . $herecurr);
2101c45dcabdSAndy Whitcroft
2102c45dcabdSAndy Whitcroft			} elsif ($spacing !~ /\s+/) {
2103c45dcabdSAndy Whitcroft				ERROR("space required before the open parenthesis '('\n" . $herecurr);
2104c45dcabdSAndy Whitcroft			}
2105c45dcabdSAndy Whitcroft		}
2106c45dcabdSAndy Whitcroft
21070a920b5bSAndy Whitcroft# Need a space before open parenthesis after if, while etc
21084a0df2efSAndy Whitcroft		if ($line=~/\b(if|while|for|switch)\(/) {
2109773647a0SAndy Whitcroft			ERROR("space required before the open parenthesis '('\n" . $herecurr);
21100a920b5bSAndy Whitcroft		}
21110a920b5bSAndy Whitcroft
2112f5fe35ddSAndy Whitcroft# Check for illegal assignment in if conditional -- and check for trailing
2113f5fe35ddSAndy Whitcroft# statements after the conditional.
2114170d3a22SAndy Whitcroft		if ($line =~ /do\s*(?!{)/) {
2115170d3a22SAndy Whitcroft			my ($stat_next) = ctx_statement_block($line_nr_next,
2116170d3a22SAndy Whitcroft						$remain_next, $off_next);
2117170d3a22SAndy Whitcroft			$stat_next =~ s/\n./\n /g;
2118170d3a22SAndy Whitcroft			##print "stat<$stat> stat_next<$stat_next>\n";
2119170d3a22SAndy Whitcroft
2120170d3a22SAndy Whitcroft			if ($stat_next =~ /^\s*while\b/) {
2121170d3a22SAndy Whitcroft				# If the statement carries leading newlines,
2122170d3a22SAndy Whitcroft				# then count those as offsets.
2123170d3a22SAndy Whitcroft				my ($whitespace) =
2124170d3a22SAndy Whitcroft					($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
2125170d3a22SAndy Whitcroft				my $offset =
2126170d3a22SAndy Whitcroft					statement_rawlines($whitespace) - 1;
2127170d3a22SAndy Whitcroft
2128170d3a22SAndy Whitcroft				$suppress_whiletrailers{$line_nr_next +
2129170d3a22SAndy Whitcroft								$offset} = 1;
2130170d3a22SAndy Whitcroft			}
2131170d3a22SAndy Whitcroft		}
2132170d3a22SAndy Whitcroft		if (!defined $suppress_whiletrailers{$linenr} &&
2133170d3a22SAndy Whitcroft		    $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
2134171ae1a4SAndy Whitcroft			my ($s, $c) = ($stat, $cond);
21358905a67cSAndy Whitcroft
2136b53c8e10SAndy Whitcroft			if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
2137c2fdda0dSAndy Whitcroft				ERROR("do not use assignment in if condition\n" . $herecurr);
21388905a67cSAndy Whitcroft			}
21398905a67cSAndy Whitcroft
21408905a67cSAndy Whitcroft			# Find out what is on the end of the line after the
21418905a67cSAndy Whitcroft			# conditional.
2142773647a0SAndy Whitcroft			substr($s, 0, length($c), '');
21438905a67cSAndy Whitcroft			$s =~ s/\n.*//g;
214413214adfSAndy Whitcroft			$s =~ s/$;//g; 	# Remove any comments
214553210168SAndy Whitcroft			if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
214653210168SAndy Whitcroft			    $c !~ /}\s*while\s*/)
2147773647a0SAndy Whitcroft			{
2148bb44ad39SAndy Whitcroft				# Find out how long the conditional actually is.
2149bb44ad39SAndy Whitcroft				my @newlines = ($c =~ /\n/gs);
2150bb44ad39SAndy Whitcroft				my $cond_lines = 1 + $#newlines;
2151bb44ad39SAndy Whitcroft
2152bb44ad39SAndy Whitcroft				my $stat_real = raw_line($linenr, $cond_lines);
2153bb44ad39SAndy Whitcroft				if (defined($stat_real) && $cond_lines > 1) {
2154bb44ad39SAndy Whitcroft					$stat_real = "[...]\n$stat_real";
2155bb44ad39SAndy Whitcroft				}
2156bb44ad39SAndy Whitcroft
2157bb44ad39SAndy Whitcroft				ERROR("trailing statements should be on next line\n" . $herecurr . $stat_real);
21588905a67cSAndy Whitcroft			}
21598905a67cSAndy Whitcroft		}
21608905a67cSAndy Whitcroft
216113214adfSAndy Whitcroft# Check for bitwise tests written as boolean
216213214adfSAndy Whitcroft		if ($line =~ /
216313214adfSAndy Whitcroft			(?:
216413214adfSAndy Whitcroft				(?:\[|\(|\&\&|\|\|)
216513214adfSAndy Whitcroft				\s*0[xX][0-9]+\s*
216613214adfSAndy Whitcroft				(?:\&\&|\|\|)
216713214adfSAndy Whitcroft			|
216813214adfSAndy Whitcroft				(?:\&\&|\|\|)
216913214adfSAndy Whitcroft				\s*0[xX][0-9]+\s*
217013214adfSAndy Whitcroft				(?:\&\&|\|\||\)|\])
217113214adfSAndy Whitcroft			)/x)
217213214adfSAndy Whitcroft		{
217313214adfSAndy Whitcroft			WARN("boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
217413214adfSAndy Whitcroft		}
217513214adfSAndy Whitcroft
21768905a67cSAndy Whitcroft# if and else should not have general statements after it
217713214adfSAndy Whitcroft		if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
217813214adfSAndy Whitcroft			my $s = $1;
217913214adfSAndy Whitcroft			$s =~ s/$;//g; 	# Remove any comments
218013214adfSAndy Whitcroft			if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
21818905a67cSAndy Whitcroft				ERROR("trailing statements should be on next line\n" . $herecurr);
21820a920b5bSAndy Whitcroft			}
218313214adfSAndy Whitcroft		}
218439667782SAndy Whitcroft# if should not continue a brace
218539667782SAndy Whitcroft		if ($line =~ /}\s*if\b/) {
218639667782SAndy Whitcroft			ERROR("trailing statements should be on next line\n" .
218739667782SAndy Whitcroft				$herecurr);
218839667782SAndy Whitcroft		}
2189a1080bf8SAndy Whitcroft# case and default should not have general statements after them
2190a1080bf8SAndy Whitcroft		if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
2191a1080bf8SAndy Whitcroft		    $line !~ /\G(?:
21923fef12d6SAndy Whitcroft			(?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
2193a1080bf8SAndy Whitcroft			\s*return\s+
2194a1080bf8SAndy Whitcroft		    )/xg)
2195a1080bf8SAndy Whitcroft		{
2196a1080bf8SAndy Whitcroft			ERROR("trailing statements should be on next line\n" . $herecurr);
2197a1080bf8SAndy Whitcroft		}
21980a920b5bSAndy Whitcroft
21990a920b5bSAndy Whitcroft		# Check for }<nl>else {, these must be at the same
22000a920b5bSAndy Whitcroft		# indent level to be relevant to each other.
22010a920b5bSAndy Whitcroft		if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
22020a920b5bSAndy Whitcroft						$previndent == $indent) {
2203de7d4f0eSAndy Whitcroft			ERROR("else should follow close brace '}'\n" . $hereprev);
22040a920b5bSAndy Whitcroft		}
22050a920b5bSAndy Whitcroft
2206c2fdda0dSAndy Whitcroft		if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and
2207c2fdda0dSAndy Whitcroft						$previndent == $indent) {
2208c2fdda0dSAndy Whitcroft			my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
2209c2fdda0dSAndy Whitcroft
2210c2fdda0dSAndy Whitcroft			# Find out what is on the end of the line after the
2211c2fdda0dSAndy Whitcroft			# conditional.
2212773647a0SAndy Whitcroft			substr($s, 0, length($c), '');
2213c2fdda0dSAndy Whitcroft			$s =~ s/\n.*//g;
2214c2fdda0dSAndy Whitcroft
2215c2fdda0dSAndy Whitcroft			if ($s =~ /^\s*;/) {
2216c2fdda0dSAndy Whitcroft				ERROR("while should follow close brace '}'\n" . $hereprev);
2217c2fdda0dSAndy Whitcroft			}
2218c2fdda0dSAndy Whitcroft		}
2219c2fdda0dSAndy Whitcroft
22200a920b5bSAndy Whitcroft#studly caps, commented out until figure out how to distinguish between use of existing and adding new
22210a920b5bSAndy Whitcroft#		if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) {
22220a920b5bSAndy Whitcroft#		    print "No studly caps, use _\n";
22230a920b5bSAndy Whitcroft#		    print "$herecurr";
22240a920b5bSAndy Whitcroft#		    $clean = 0;
22250a920b5bSAndy Whitcroft#		}
22260a920b5bSAndy Whitcroft
22270a920b5bSAndy Whitcroft#no spaces allowed after \ in define
2228c45dcabdSAndy Whitcroft		if ($line=~/\#\s*define.*\\\s$/) {
2229de7d4f0eSAndy Whitcroft			WARN("Whitepspace after \\ makes next lines useless\n" . $herecurr);
22300a920b5bSAndy Whitcroft		}
22310a920b5bSAndy Whitcroft
2232653d4876SAndy Whitcroft#warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
2233c45dcabdSAndy Whitcroft		if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
2234e09dec48SAndy Whitcroft			my $file = "$1.h";
2235e09dec48SAndy Whitcroft			my $checkfile = "include/linux/$file";
2236e09dec48SAndy Whitcroft			if (-f "$root/$checkfile" &&
2237e09dec48SAndy Whitcroft			    $realfile ne $checkfile &&
2238c45dcabdSAndy Whitcroft			    $1 ne 'irq')
2239c45dcabdSAndy Whitcroft			{
2240e09dec48SAndy Whitcroft				if ($realfile =~ m{^arch/}) {
2241e09dec48SAndy Whitcroft					CHK("Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
2242e09dec48SAndy Whitcroft				} else {
2243e09dec48SAndy Whitcroft					WARN("Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
2244e09dec48SAndy Whitcroft				}
22450a920b5bSAndy Whitcroft			}
22460a920b5bSAndy Whitcroft		}
22470a920b5bSAndy Whitcroft
2248653d4876SAndy Whitcroft# multi-statement macros should be enclosed in a do while loop, grab the
2249653d4876SAndy Whitcroft# first statement and ensure its the whole macro if its not enclosed
2250cf655043SAndy Whitcroft# in a known good container
2251b8f96a31SAndy Whitcroft		if ($realfile !~ m@/vmlinux.lds.h$@ &&
2252b8f96a31SAndy Whitcroft		    $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
2253d8aaf121SAndy Whitcroft			my $ln = $linenr;
2254d8aaf121SAndy Whitcroft			my $cnt = $realcnt;
2255c45dcabdSAndy Whitcroft			my ($off, $dstat, $dcond, $rest);
2256c45dcabdSAndy Whitcroft			my $ctx = '';
2257653d4876SAndy Whitcroft
2258c45dcabdSAndy Whitcroft			my $args = defined($1);
2259de7d4f0eSAndy Whitcroft
2260c45dcabdSAndy Whitcroft			# Find the end of the macro and limit our statement
2261c45dcabdSAndy Whitcroft			# search to that.
2262c45dcabdSAndy Whitcroft			while ($cnt > 0 && defined $lines[$ln - 1] &&
2263c45dcabdSAndy Whitcroft				$lines[$ln - 1] =~ /^(?:-|..*\\$)/)
2264c45dcabdSAndy Whitcroft			{
2265c45dcabdSAndy Whitcroft				$ctx .= $rawlines[$ln - 1] . "\n";
2266a3bb97a7SAndy Whitcroft				$cnt-- if ($lines[$ln - 1] !~ /^-/);
2267c45dcabdSAndy Whitcroft				$ln++;
2268de7d4f0eSAndy Whitcroft			}
2269c45dcabdSAndy Whitcroft			$ctx .= $rawlines[$ln - 1];
2270d8aaf121SAndy Whitcroft
2271c45dcabdSAndy Whitcroft			($dstat, $dcond, $ln, $cnt, $off) =
2272c45dcabdSAndy Whitcroft				ctx_statement_block($linenr, $ln - $linenr + 1, 0);
2273c45dcabdSAndy Whitcroft			#print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
2274a3bb97a7SAndy Whitcroft			#print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
2275c45dcabdSAndy Whitcroft
2276c45dcabdSAndy Whitcroft			# Extract the remainder of the define (if any) and
2277c45dcabdSAndy Whitcroft			# rip off surrounding spaces, and trailing \'s.
2278c45dcabdSAndy Whitcroft			$rest = '';
2279636d140aSAndy Whitcroft			while ($off != 0 || ($cnt > 0 && $rest =~ /\\\s*$/)) {
2280636d140aSAndy Whitcroft				#print "ADDING cnt<$cnt> $off <" . substr($lines[$ln - 1], $off) . "> rest<$rest>\n";
2281a3bb97a7SAndy Whitcroft				if ($off != 0 || $lines[$ln - 1] !~ /^-/) {
2282c45dcabdSAndy Whitcroft					$rest .= substr($lines[$ln - 1], $off) . "\n";
2283c45dcabdSAndy Whitcroft					$cnt--;
2284a3bb97a7SAndy Whitcroft				}
2285a3bb97a7SAndy Whitcroft				$ln++;
2286c45dcabdSAndy Whitcroft				$off = 0;
2287c45dcabdSAndy Whitcroft			}
2288c45dcabdSAndy Whitcroft			$rest =~ s/\\\n.//g;
2289c45dcabdSAndy Whitcroft			$rest =~ s/^\s*//s;
2290c45dcabdSAndy Whitcroft			$rest =~ s/\s*$//s;
2291c45dcabdSAndy Whitcroft
2292c45dcabdSAndy Whitcroft			# Clean up the original statement.
2293c45dcabdSAndy Whitcroft			if ($args) {
2294c45dcabdSAndy Whitcroft				substr($dstat, 0, length($dcond), '');
2295d8aaf121SAndy Whitcroft			} else {
2296c45dcabdSAndy Whitcroft				$dstat =~ s/^.\s*\#\s*define\s+$Ident\s*//;
2297c45dcabdSAndy Whitcroft			}
2298292f1a9bSAndy Whitcroft			$dstat =~ s/$;//g;
2299c45dcabdSAndy Whitcroft			$dstat =~ s/\\\n.//g;
2300c45dcabdSAndy Whitcroft			$dstat =~ s/^\s*//s;
2301c45dcabdSAndy Whitcroft			$dstat =~ s/\s*$//s;
2302c45dcabdSAndy Whitcroft
2303c45dcabdSAndy Whitcroft			# Flatten any parentheses and braces
2304bf30d6edSAndy Whitcroft			while ($dstat =~ s/\([^\(\)]*\)/1/ ||
2305bf30d6edSAndy Whitcroft			       $dstat =~ s/\{[^\{\}]*\}/1/ ||
2306bf30d6edSAndy Whitcroft			       $dstat =~ s/\[[^\{\}]*\]/1/)
2307bf30d6edSAndy Whitcroft			{
2308c45dcabdSAndy Whitcroft			}
2309c45dcabdSAndy Whitcroft
2310c45dcabdSAndy Whitcroft			my $exceptions = qr{
2311c45dcabdSAndy Whitcroft				$Declare|
2312c45dcabdSAndy Whitcroft				module_param_named|
2313c45dcabdSAndy Whitcroft				MODULE_PARAM_DESC|
2314c45dcabdSAndy Whitcroft				DECLARE_PER_CPU|
2315c45dcabdSAndy Whitcroft				DEFINE_PER_CPU|
2316383099fdSAndy Whitcroft				__typeof__\(|
2317ea71a0a0SAndy Whitcroft				\.$Ident\s*=\s*|
2318ea71a0a0SAndy Whitcroft				^\"|\"$
2319c45dcabdSAndy Whitcroft			}x;
2320383099fdSAndy Whitcroft			#print "REST<$rest> dstat<$dstat>\n";
2321c45dcabdSAndy Whitcroft			if ($rest ne '') {
2322c45dcabdSAndy Whitcroft				if ($rest !~ /while\s*\(/ &&
2323c45dcabdSAndy Whitcroft				    $dstat !~ /$exceptions/)
2324c45dcabdSAndy Whitcroft				{
2325c45dcabdSAndy Whitcroft					ERROR("Macros with multiple statements should be enclosed in a do - while loop\n" . "$here\n$ctx\n");
2326c45dcabdSAndy Whitcroft				}
2327c45dcabdSAndy Whitcroft
2328c45dcabdSAndy Whitcroft			} elsif ($ctx !~ /;/) {
2329c45dcabdSAndy Whitcroft				if ($dstat ne '' &&
2330c45dcabdSAndy Whitcroft				    $dstat !~ /^(?:$Ident|-?$Constant)$/ &&
2331c45dcabdSAndy Whitcroft				    $dstat !~ /$exceptions/ &&
2332b132e5d5SAndy Whitcroft				    $dstat !~ /^\.$Ident\s*=/ &&
2333c45dcabdSAndy Whitcroft				    $dstat =~ /$Operators/)
2334c45dcabdSAndy Whitcroft				{
2335de7d4f0eSAndy Whitcroft					ERROR("Macros with complex values should be enclosed in parenthesis\n" . "$here\n$ctx\n");
2336d8aaf121SAndy Whitcroft				}
23370a920b5bSAndy Whitcroft			}
2338653d4876SAndy Whitcroft		}
23390a920b5bSAndy Whitcroft
2340080ba929SMike Frysinger# make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
2341080ba929SMike Frysinger# all assignments may have only one of the following with an assignment:
2342080ba929SMike Frysinger#	.
2343080ba929SMike Frysinger#	ALIGN(...)
2344080ba929SMike Frysinger#	VMLINUX_SYMBOL(...)
2345080ba929SMike Frysinger		if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
2346080ba929SMike Frysinger			WARN("vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
2347080ba929SMike Frysinger		}
2348080ba929SMike Frysinger
2349f0a594c1SAndy Whitcroft# check for redundant bracing round if etc
235013214adfSAndy Whitcroft		if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
235113214adfSAndy Whitcroft			my ($level, $endln, @chunks) =
2352cf655043SAndy Whitcroft				ctx_statement_full($linenr, $realcnt, 1);
235313214adfSAndy Whitcroft			#print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
2354cf655043SAndy Whitcroft			#print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
2355cf655043SAndy Whitcroft			if ($#chunks > 0 && $level == 0) {
235613214adfSAndy Whitcroft				my $allowed = 0;
235713214adfSAndy Whitcroft				my $seen = 0;
2358773647a0SAndy Whitcroft				my $herectx = $here . "\n";
2359cf655043SAndy Whitcroft				my $ln = $linenr - 1;
236013214adfSAndy Whitcroft				for my $chunk (@chunks) {
236113214adfSAndy Whitcroft					my ($cond, $block) = @{$chunk};
236213214adfSAndy Whitcroft
2363773647a0SAndy Whitcroft					# If the condition carries leading newlines, then count those as offsets.
2364773647a0SAndy Whitcroft					my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
2365773647a0SAndy Whitcroft					my $offset = statement_rawlines($whitespace) - 1;
2366773647a0SAndy Whitcroft
2367773647a0SAndy Whitcroft					#print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
2368773647a0SAndy Whitcroft
2369773647a0SAndy Whitcroft					# We have looked at and allowed this specific line.
2370773647a0SAndy Whitcroft					$suppress_ifbraces{$ln + $offset} = 1;
2371773647a0SAndy Whitcroft
2372773647a0SAndy Whitcroft					$herectx .= "$rawlines[$ln + $offset]\n[...]\n";
2373cf655043SAndy Whitcroft					$ln += statement_rawlines($block) - 1;
2374cf655043SAndy Whitcroft
2375773647a0SAndy Whitcroft					substr($block, 0, length($cond), '');
237613214adfSAndy Whitcroft
237713214adfSAndy Whitcroft					$seen++ if ($block =~ /^\s*{/);
237813214adfSAndy Whitcroft
2379cf655043SAndy Whitcroft					#print "cond<$cond> block<$block> allowed<$allowed>\n";
2380cf655043SAndy Whitcroft					if (statement_lines($cond) > 1) {
2381cf655043SAndy Whitcroft						#print "APW: ALLOWED: cond<$cond>\n";
238213214adfSAndy Whitcroft						$allowed = 1;
238313214adfSAndy Whitcroft					}
238413214adfSAndy Whitcroft					if ($block =~/\b(?:if|for|while)\b/) {
2385cf655043SAndy Whitcroft						#print "APW: ALLOWED: block<$block>\n";
238613214adfSAndy Whitcroft						$allowed = 1;
238713214adfSAndy Whitcroft					}
2388cf655043SAndy Whitcroft					if (statement_block_size($block) > 1) {
2389cf655043SAndy Whitcroft						#print "APW: ALLOWED: lines block<$block>\n";
239013214adfSAndy Whitcroft						$allowed = 1;
239113214adfSAndy Whitcroft					}
239213214adfSAndy Whitcroft				}
239313214adfSAndy Whitcroft				if ($seen && !$allowed) {
2394cf655043SAndy Whitcroft					WARN("braces {} are not necessary for any arm of this statement\n" . $herectx);
239513214adfSAndy Whitcroft				}
239613214adfSAndy Whitcroft			}
239713214adfSAndy Whitcroft		}
2398773647a0SAndy Whitcroft		if (!defined $suppress_ifbraces{$linenr - 1} &&
239913214adfSAndy Whitcroft					$line =~ /\b(if|while|for|else)\b/) {
2400cf655043SAndy Whitcroft			my $allowed = 0;
2401f0a594c1SAndy Whitcroft
2402cf655043SAndy Whitcroft			# Check the pre-context.
2403cf655043SAndy Whitcroft			if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
2404cf655043SAndy Whitcroft				#print "APW: ALLOWED: pre<$1>\n";
2405cf655043SAndy Whitcroft				$allowed = 1;
2406f0a594c1SAndy Whitcroft			}
2407773647a0SAndy Whitcroft
2408773647a0SAndy Whitcroft			my ($level, $endln, @chunks) =
2409773647a0SAndy Whitcroft				ctx_statement_full($linenr, $realcnt, $-[0]);
2410773647a0SAndy Whitcroft
2411cf655043SAndy Whitcroft			# Check the condition.
2412cf655043SAndy Whitcroft			my ($cond, $block) = @{$chunks[0]};
2413773647a0SAndy Whitcroft			#print "CHECKING<$linenr> cond<$cond> block<$block>\n";
2414cf655043SAndy Whitcroft			if (defined $cond) {
2415773647a0SAndy Whitcroft				substr($block, 0, length($cond), '');
2416cf655043SAndy Whitcroft			}
2417cf655043SAndy Whitcroft			if (statement_lines($cond) > 1) {
2418cf655043SAndy Whitcroft				#print "APW: ALLOWED: cond<$cond>\n";
2419cf655043SAndy Whitcroft				$allowed = 1;
2420cf655043SAndy Whitcroft			}
2421cf655043SAndy Whitcroft			if ($block =~/\b(?:if|for|while)\b/) {
2422cf655043SAndy Whitcroft				#print "APW: ALLOWED: block<$block>\n";
2423cf655043SAndy Whitcroft				$allowed = 1;
2424cf655043SAndy Whitcroft			}
2425cf655043SAndy Whitcroft			if (statement_block_size($block) > 1) {
2426cf655043SAndy Whitcroft				#print "APW: ALLOWED: lines block<$block>\n";
2427cf655043SAndy Whitcroft				$allowed = 1;
2428cf655043SAndy Whitcroft			}
2429cf655043SAndy Whitcroft			# Check the post-context.
2430cf655043SAndy Whitcroft			if (defined $chunks[1]) {
2431cf655043SAndy Whitcroft				my ($cond, $block) = @{$chunks[1]};
2432cf655043SAndy Whitcroft				if (defined $cond) {
2433773647a0SAndy Whitcroft					substr($block, 0, length($cond), '');
2434cf655043SAndy Whitcroft				}
2435cf655043SAndy Whitcroft				if ($block =~ /^\s*\{/) {
2436cf655043SAndy Whitcroft					#print "APW: ALLOWED: chunk-1 block<$block>\n";
2437cf655043SAndy Whitcroft					$allowed = 1;
2438cf655043SAndy Whitcroft				}
2439cf655043SAndy Whitcroft			}
2440cf655043SAndy Whitcroft			if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
2441cf655043SAndy Whitcroft				my $herectx = $here . "\n";;
2442f055663cSAndy Whitcroft				my $cnt = statement_rawlines($block);
2443cf655043SAndy Whitcroft
2444f055663cSAndy Whitcroft				for (my $n = 0; $n < $cnt; $n++) {
2445f055663cSAndy Whitcroft					$herectx .= raw_line($linenr, $n) . "\n";;
2446cf655043SAndy Whitcroft				}
2447cf655043SAndy Whitcroft
2448cf655043SAndy Whitcroft				WARN("braces {} are not necessary for single statement blocks\n" . $herectx);
2449f0a594c1SAndy Whitcroft			}
2450f0a594c1SAndy Whitcroft		}
2451f0a594c1SAndy Whitcroft
2452653d4876SAndy Whitcroft# don't include deprecated include files (uses RAW line)
24534a0df2efSAndy Whitcroft		for my $inc (@dep_includes) {
2454c45dcabdSAndy Whitcroft			if ($rawline =~ m@^.\s*\#\s*include\s*\<$inc>@) {
2455de7d4f0eSAndy Whitcroft				ERROR("Don't use <$inc>: see Documentation/feature-removal-schedule.txt\n" . $herecurr);
24560a920b5bSAndy Whitcroft			}
24570a920b5bSAndy Whitcroft		}
24580a920b5bSAndy Whitcroft
24594a0df2efSAndy Whitcroft# don't use deprecated functions
24604a0df2efSAndy Whitcroft		for my $func (@dep_functions) {
246100df344fSAndy Whitcroft			if ($line =~ /\b$func\b/) {
2462de7d4f0eSAndy Whitcroft				ERROR("Don't use $func(): see Documentation/feature-removal-schedule.txt\n" . $herecurr);
24634a0df2efSAndy Whitcroft			}
24644a0df2efSAndy Whitcroft		}
24654a0df2efSAndy Whitcroft
24664a0df2efSAndy Whitcroft# no volatiles please
24676c72ffaaSAndy Whitcroft		my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
24686c72ffaaSAndy Whitcroft		if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
2469de7d4f0eSAndy Whitcroft			WARN("Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
24704a0df2efSAndy Whitcroft		}
24714a0df2efSAndy Whitcroft
24729c0ca6f9SAndy Whitcroft# SPIN_LOCK_UNLOCKED & RW_LOCK_UNLOCKED are deprecated
24739c0ca6f9SAndy Whitcroft		if ($line =~ /\b(SPIN_LOCK_UNLOCKED|RW_LOCK_UNLOCKED)/) {
24749c0ca6f9SAndy Whitcroft			ERROR("Use of $1 is deprecated: see Documentation/spinlocks.txt\n" . $herecurr);
24759c0ca6f9SAndy Whitcroft		}
24769c0ca6f9SAndy Whitcroft
247700df344fSAndy Whitcroft# warn about #if 0
2478c45dcabdSAndy Whitcroft		if ($line =~ /^.\s*\#\s*if\s+0\b/) {
2479de7d4f0eSAndy Whitcroft			CHK("if this code is redundant consider removing it\n" .
2480de7d4f0eSAndy Whitcroft				$herecurr);
24814a0df2efSAndy Whitcroft		}
24824a0df2efSAndy Whitcroft
2483f0a594c1SAndy Whitcroft# check for needless kfree() checks
2484f0a594c1SAndy Whitcroft		if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
2485f0a594c1SAndy Whitcroft			my $expr = $1;
2486f0a594c1SAndy Whitcroft			if ($line =~ /\bkfree\(\Q$expr\E\);/) {
24873c232147SWolfram Sang				WARN("kfree(NULL) is safe this check is probably not required\n" . $hereprev);
2488f0a594c1SAndy Whitcroft			}
2489f0a594c1SAndy Whitcroft		}
24904c432a8fSGreg Kroah-Hartman# check for needless usb_free_urb() checks
24914c432a8fSGreg Kroah-Hartman		if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
24924c432a8fSGreg Kroah-Hartman			my $expr = $1;
24934c432a8fSGreg Kroah-Hartman			if ($line =~ /\busb_free_urb\(\Q$expr\E\);/) {
24944c432a8fSGreg Kroah-Hartman				WARN("usb_free_urb(NULL) is safe this check is probably not required\n" . $hereprev);
24954c432a8fSGreg Kroah-Hartman			}
24964c432a8fSGreg Kroah-Hartman		}
2497f0a594c1SAndy Whitcroft
249800df344fSAndy Whitcroft# warn about #ifdefs in C files
2499c45dcabdSAndy Whitcroft#		if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
250000df344fSAndy Whitcroft#			print "#ifdef in C files should be avoided\n";
250100df344fSAndy Whitcroft#			print "$herecurr";
250200df344fSAndy Whitcroft#			$clean = 0;
250300df344fSAndy Whitcroft#		}
250400df344fSAndy Whitcroft
250522f2a2efSAndy Whitcroft# warn about spacing in #ifdefs
2506c45dcabdSAndy Whitcroft		if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
250722f2a2efSAndy Whitcroft			ERROR("exactly one space required after that #$1\n" . $herecurr);
250822f2a2efSAndy Whitcroft		}
250922f2a2efSAndy Whitcroft
25104a0df2efSAndy Whitcroft# check for spinlock_t definitions without a comment.
2511171ae1a4SAndy Whitcroft		if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
2512171ae1a4SAndy Whitcroft		    $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
25134a0df2efSAndy Whitcroft			my $which = $1;
25144a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
2515de7d4f0eSAndy Whitcroft				CHK("$1 definition without comment\n" . $herecurr);
25164a0df2efSAndy Whitcroft			}
25174a0df2efSAndy Whitcroft		}
25184a0df2efSAndy Whitcroft# check for memory barriers without a comment.
25194a0df2efSAndy Whitcroft		if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
25204a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
2521de7d4f0eSAndy Whitcroft				CHK("memory barrier without comment\n" . $herecurr);
25224a0df2efSAndy Whitcroft			}
25234a0df2efSAndy Whitcroft		}
25244a0df2efSAndy Whitcroft# check of hardware specific defines
2525c45dcabdSAndy Whitcroft		if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
2526de7d4f0eSAndy Whitcroft			CHK("architecture specific defines should be avoided\n" .  $herecurr);
25270a920b5bSAndy Whitcroft		}
2528653d4876SAndy Whitcroft
2529de7d4f0eSAndy Whitcroft# check the location of the inline attribute, that it is between
2530de7d4f0eSAndy Whitcroft# storage class and type.
25319c0ca6f9SAndy Whitcroft		if ($line =~ /\b$Type\s+$Inline\b/ ||
25329c0ca6f9SAndy Whitcroft		    $line =~ /\b$Inline\s+$Storage\b/) {
2533de7d4f0eSAndy Whitcroft			ERROR("inline keyword should sit between storage class and type\n" . $herecurr);
2534de7d4f0eSAndy Whitcroft		}
2535de7d4f0eSAndy Whitcroft
25368905a67cSAndy Whitcroft# Check for __inline__ and __inline, prefer inline
25378905a67cSAndy Whitcroft		if ($line =~ /\b(__inline__|__inline)\b/) {
25388905a67cSAndy Whitcroft			WARN("plain inline is preferred over $1\n" . $herecurr);
25398905a67cSAndy Whitcroft		}
25408905a67cSAndy Whitcroft
2541de7d4f0eSAndy Whitcroft# check for new externs in .c files.
2542171ae1a4SAndy Whitcroft		if ($realfile =~ /\.c$/ && defined $stat &&
2543c45dcabdSAndy Whitcroft		    $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
2544171ae1a4SAndy Whitcroft		{
2545c45dcabdSAndy Whitcroft			my $function_name = $1;
2546c45dcabdSAndy Whitcroft			my $paren_space = $2;
2547171ae1a4SAndy Whitcroft
2548171ae1a4SAndy Whitcroft			my $s = $stat;
2549171ae1a4SAndy Whitcroft			if (defined $cond) {
2550171ae1a4SAndy Whitcroft				substr($s, 0, length($cond), '');
2551171ae1a4SAndy Whitcroft			}
2552c45dcabdSAndy Whitcroft			if ($s =~ /^\s*;/ &&
2553c45dcabdSAndy Whitcroft			    $function_name ne 'uninitialized_var')
2554c45dcabdSAndy Whitcroft			{
2555de7d4f0eSAndy Whitcroft				WARN("externs should be avoided in .c files\n" .  $herecurr);
2556de7d4f0eSAndy Whitcroft			}
2557de7d4f0eSAndy Whitcroft
2558171ae1a4SAndy Whitcroft			if ($paren_space =~ /\n/) {
2559171ae1a4SAndy Whitcroft				WARN("arguments for function declarations should follow identifier\n" . $herecurr);
2560171ae1a4SAndy Whitcroft			}
25619c9ba34eSAndy Whitcroft
25629c9ba34eSAndy Whitcroft		} elsif ($realfile =~ /\.c$/ && defined $stat &&
25639c9ba34eSAndy Whitcroft		    $stat =~ /^.\s*extern\s+/)
25649c9ba34eSAndy Whitcroft		{
25659c9ba34eSAndy Whitcroft			WARN("externs should be avoided in .c files\n" .  $herecurr);
2566171ae1a4SAndy Whitcroft		}
2567171ae1a4SAndy Whitcroft
2568de7d4f0eSAndy Whitcroft# checks for new __setup's
2569de7d4f0eSAndy Whitcroft		if ($rawline =~ /\b__setup\("([^"]*)"/) {
2570de7d4f0eSAndy Whitcroft			my $name = $1;
2571de7d4f0eSAndy Whitcroft
2572de7d4f0eSAndy Whitcroft			if (!grep(/$name/, @setup_docs)) {
2573de7d4f0eSAndy Whitcroft				CHK("__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
2574de7d4f0eSAndy Whitcroft			}
2575653d4876SAndy Whitcroft		}
25769c0ca6f9SAndy Whitcroft
25779c0ca6f9SAndy Whitcroft# check for pointless casting of kmalloc return
25789c0ca6f9SAndy Whitcroft		if ($line =~ /\*\s*\)\s*k[czm]alloc\b/) {
25799c0ca6f9SAndy Whitcroft			WARN("unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
25809c0ca6f9SAndy Whitcroft		}
258113214adfSAndy Whitcroft
258213214adfSAndy Whitcroft# check for gcc specific __FUNCTION__
258313214adfSAndy Whitcroft		if ($line =~ /__FUNCTION__/) {
258413214adfSAndy Whitcroft			WARN("__func__ should be used instead of gcc specific __FUNCTION__\n"  . $herecurr);
258513214adfSAndy Whitcroft		}
2586773647a0SAndy Whitcroft
2587773647a0SAndy Whitcroft# check for semaphores used as mutexes
2588171ae1a4SAndy Whitcroft		if ($line =~ /^.\s*(DECLARE_MUTEX|init_MUTEX)\s*\(/) {
2589773647a0SAndy Whitcroft			WARN("mutexes are preferred for single holder semaphores\n" . $herecurr);
2590773647a0SAndy Whitcroft		}
2591773647a0SAndy Whitcroft# check for semaphores used as mutexes
2592171ae1a4SAndy Whitcroft		if ($line =~ /^.\s*init_MUTEX_LOCKED\s*\(/) {
2593773647a0SAndy Whitcroft			WARN("consider using a completion\n" . $herecurr);
2594773647a0SAndy Whitcroft		}
2595773647a0SAndy Whitcroft# recommend strict_strto* over simple_strto*
2596773647a0SAndy Whitcroft		if ($line =~ /\bsimple_(strto.*?)\s*\(/) {
2597773647a0SAndy Whitcroft			WARN("consider using strict_$1 in preference to simple_$1\n" . $herecurr);
2598773647a0SAndy Whitcroft		}
2599f3db6639SMichael Ellerman# check for __initcall(), use device_initcall() explicitly please
2600f3db6639SMichael Ellerman		if ($line =~ /^.\s*__initcall\s*\(/) {
2601f3db6639SMichael Ellerman			WARN("please use device_initcall() instead of __initcall()\n" . $herecurr);
2602f3db6639SMichael Ellerman		}
26032b6db5cbSAndy Whitcroft# check for struct file_operations, ensure they are const.
26046903ffb2SAndy Whitcroft		if ($line !~ /\bconst\b/ &&
26056903ffb2SAndy Whitcroft		    $line =~ /\bstruct\s+(file_operations|seq_operations)\b/) {
26066903ffb2SAndy Whitcroft			WARN("struct $1 should normally be const\n" .
26076903ffb2SAndy Whitcroft				$herecurr);
26082b6db5cbSAndy Whitcroft		}
2609773647a0SAndy Whitcroft
2610773647a0SAndy Whitcroft# use of NR_CPUS is usually wrong
2611773647a0SAndy Whitcroft# ignore definitions of NR_CPUS and usage to define arrays as likely right
2612773647a0SAndy Whitcroft		if ($line =~ /\bNR_CPUS\b/ &&
2613c45dcabdSAndy Whitcroft		    $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
2614c45dcabdSAndy Whitcroft		    $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
2615171ae1a4SAndy Whitcroft		    $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
2616171ae1a4SAndy Whitcroft		    $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
2617171ae1a4SAndy Whitcroft		    $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
2618773647a0SAndy Whitcroft		{
2619773647a0SAndy Whitcroft			WARN("usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
2620773647a0SAndy Whitcroft		}
26219c9ba34eSAndy Whitcroft
26229c9ba34eSAndy Whitcroft# check for %L{u,d,i} in strings
26239c9ba34eSAndy Whitcroft		my $string;
26249c9ba34eSAndy Whitcroft		while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
26259c9ba34eSAndy Whitcroft			$string = substr($rawline, $-[1], $+[1] - $-[1]);
26262a1bc5d5SAndy Whitcroft			$string =~ s/%%/__/g;
26279c9ba34eSAndy Whitcroft			if ($string =~ /(?<!%)%L[udi]/) {
26289c9ba34eSAndy Whitcroft				WARN("\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
26299c9ba34eSAndy Whitcroft				last;
26309c9ba34eSAndy Whitcroft			}
26319c9ba34eSAndy Whitcroft		}
2632691d77b6SAndy Whitcroft
2633691d77b6SAndy Whitcroft# whine mightly about in_atomic
2634691d77b6SAndy Whitcroft		if ($line =~ /\bin_atomic\s*\(/) {
2635691d77b6SAndy Whitcroft			if ($realfile =~ m@^drivers/@) {
2636691d77b6SAndy Whitcroft				ERROR("do not use in_atomic in drivers\n" . $herecurr);
2637f4a87736SAndy Whitcroft			} elsif ($realfile !~ m@^kernel/@) {
2638691d77b6SAndy Whitcroft				WARN("use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
2639691d77b6SAndy Whitcroft			}
2640691d77b6SAndy Whitcroft		}
264113214adfSAndy Whitcroft	}
264213214adfSAndy Whitcroft
264313214adfSAndy Whitcroft	# If we have no input at all, then there is nothing to report on
264413214adfSAndy Whitcroft	# so just keep quiet.
264513214adfSAndy Whitcroft	if ($#rawlines == -1) {
264613214adfSAndy Whitcroft		exit(0);
26470a920b5bSAndy Whitcroft	}
26480a920b5bSAndy Whitcroft
26498905a67cSAndy Whitcroft	# In mailback mode only produce a report in the negative, for
26508905a67cSAndy Whitcroft	# things that appear to be patches.
26518905a67cSAndy Whitcroft	if ($mailback && ($clean == 1 || !$is_patch)) {
26528905a67cSAndy Whitcroft		exit(0);
26538905a67cSAndy Whitcroft	}
26548905a67cSAndy Whitcroft
26558905a67cSAndy Whitcroft	# This is not a patch, and we are are in 'no-patch' mode so
26568905a67cSAndy Whitcroft	# just keep quiet.
26578905a67cSAndy Whitcroft	if (!$chk_patch && !$is_patch) {
26588905a67cSAndy Whitcroft		exit(0);
26598905a67cSAndy Whitcroft	}
26608905a67cSAndy Whitcroft
26618905a67cSAndy Whitcroft	if (!$is_patch) {
2662de7d4f0eSAndy Whitcroft		ERROR("Does not appear to be a unified-diff format patch\n");
26630a920b5bSAndy Whitcroft	}
26640a920b5bSAndy Whitcroft	if ($is_patch && $chk_signoff && $signoff == 0) {
2665de7d4f0eSAndy Whitcroft		ERROR("Missing Signed-off-by: line(s)\n");
26660a920b5bSAndy Whitcroft	}
26670a920b5bSAndy Whitcroft
2668f0a594c1SAndy Whitcroft	print report_dump();
266913214adfSAndy Whitcroft	if ($summary && !($clean == 1 && $quiet == 1)) {
267013214adfSAndy Whitcroft		print "$filename " if ($summary_file);
26716c72ffaaSAndy Whitcroft		print "total: $cnt_error errors, $cnt_warn warnings, " .
26726c72ffaaSAndy Whitcroft			(($check)? "$cnt_chk checks, " : "") .
26736c72ffaaSAndy Whitcroft			"$cnt_lines lines checked\n";
26748905a67cSAndy Whitcroft		print "\n" if ($quiet == 0);
26756c72ffaaSAndy Whitcroft	}
26768905a67cSAndy Whitcroft
26770a920b5bSAndy Whitcroft	if ($clean == 1 && $quiet == 0) {
2678c2fdda0dSAndy Whitcroft		print "$vname has no obvious style problems and is ready for submission.\n"
26790a920b5bSAndy Whitcroft	}
26800a920b5bSAndy Whitcroft	if ($clean == 0 && $quiet == 0) {
2681c2fdda0dSAndy Whitcroft		print "$vname has style problems, please review.  If any of these errors\n";
26820a920b5bSAndy Whitcroft		print "are false positives report them to the maintainer, see\n";
26830a920b5bSAndy Whitcroft		print "CHECKPATCH in MAINTAINERS.\n";
26840a920b5bSAndy Whitcroft	}
268513214adfSAndy Whitcroft
26860a920b5bSAndy Whitcroft	return $clean;
26870a920b5bSAndy Whitcroft}
2688