xref: /linux-6.15/scripts/checkpatch.pl (revision dbf004d7)
10a920b5bSAndy Whitcroft#!/usr/bin/perl -w
2*dbf004d7SDave Jones# (c) 2001, Dave Jones. (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)
5131edb34SAndy Whitcroft# (c) 2008,2009, 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
135e8d8f6fSAndy Whitcroftmy $V = '0.30';
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
1332ceb532bSAndy Whitcroftour $Ident	= qr{
1342ceb532bSAndy Whitcroft			[A-Za-z_][A-Za-z\d_]*
1352ceb532bSAndy Whitcroft			(?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
1362ceb532bSAndy Whitcroft		}x;
1376c72ffaaSAndy Whitcroftour $Storage	= qr{extern|static|asmlinkage};
1386c72ffaaSAndy Whitcroftour $Sparse	= qr{
1396c72ffaaSAndy Whitcroft			__user|
1406c72ffaaSAndy Whitcroft			__kernel|
1416c72ffaaSAndy Whitcroft			__force|
1426c72ffaaSAndy Whitcroft			__iomem|
1436c72ffaaSAndy Whitcroft			__must_check|
1446c72ffaaSAndy Whitcroft			__init_refok|
145417495edSAndy Whitcroft			__kprobes|
146417495edSAndy Whitcroft			__ref
1476c72ffaaSAndy Whitcroft		}x;
1486c72ffaaSAndy Whitcroftour $Attribute	= qr{
1496c72ffaaSAndy Whitcroft			const|
1506c72ffaaSAndy Whitcroft			__read_mostly|
1516c72ffaaSAndy Whitcroft			__kprobes|
15224e1d81aSAndy Whitcroft			__(?:mem|cpu|dev|)(?:initdata|init)|
15324e1d81aSAndy Whitcroft			____cacheline_aligned|
15424e1d81aSAndy Whitcroft			____cacheline_aligned_in_smp|
1555fe3af11SAndy Whitcroft			____cacheline_internodealigned_in_smp|
1565fe3af11SAndy Whitcroft			__weak
1576c72ffaaSAndy Whitcroft		  }x;
158c45dcabdSAndy Whitcroftour $Modifier;
1596c72ffaaSAndy Whitcroftour $Inline	= qr{inline|__always_inline|noinline};
1606c72ffaaSAndy Whitcroftour $Member	= qr{->$Ident|\.$Ident|\[[^]]*\]};
1616c72ffaaSAndy Whitcroftour $Lval	= qr{$Ident(?:$Member)*};
1626c72ffaaSAndy Whitcroft
1636c72ffaaSAndy Whitcroftour $Constant	= qr{(?:[0-9]+|0x[0-9a-fA-F]+)[UL]*};
1646c72ffaaSAndy Whitcroftour $Assignment	= qr{(?:\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=)};
16586f9d059SAndy Whitcroftour $Compare    = qr{<=|>=|==|!=|<|>};
1666c72ffaaSAndy Whitcroftour $Operators	= qr{
1676c72ffaaSAndy Whitcroft			<=|>=|==|!=|
1686c72ffaaSAndy Whitcroft			=>|->|<<|>>|<|>|!|~|
169c2fdda0dSAndy Whitcroft			&&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%
1706c72ffaaSAndy Whitcroft		  }x;
1716c72ffaaSAndy Whitcroft
1728905a67cSAndy Whitcroftour $NonptrType;
1738905a67cSAndy Whitcroftour $Type;
1748905a67cSAndy Whitcroftour $Declare;
1758905a67cSAndy Whitcroft
176171ae1a4SAndy Whitcroftour $UTF8	= qr {
177171ae1a4SAndy Whitcroft	[\x09\x0A\x0D\x20-\x7E]              # ASCII
178171ae1a4SAndy Whitcroft	| [\xC2-\xDF][\x80-\xBF]             # non-overlong 2-byte
179171ae1a4SAndy Whitcroft	|  \xE0[\xA0-\xBF][\x80-\xBF]        # excluding overlongs
180171ae1a4SAndy Whitcroft	| [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}  # straight 3-byte
181171ae1a4SAndy Whitcroft	|  \xED[\x80-\x9F][\x80-\xBF]        # excluding surrogates
182171ae1a4SAndy Whitcroft	|  \xF0[\x90-\xBF][\x80-\xBF]{2}     # planes 1-3
183171ae1a4SAndy Whitcroft	| [\xF1-\xF3][\x80-\xBF]{3}          # planes 4-15
184171ae1a4SAndy Whitcroft	|  \xF4[\x80-\x8F][\x80-\xBF]{2}     # plane 16
185171ae1a4SAndy Whitcroft}x;
186171ae1a4SAndy Whitcroft
1878ed22cadSAndy Whitcroftour $typeTypedefs = qr{(?x:
188fb9e9096SAndy Whitcroft	(?:__)?(?:u|s|be|le)(?:8|16|32|64)|
1898ed22cadSAndy Whitcroft	atomic_t
1908ed22cadSAndy Whitcroft)};
1918ed22cadSAndy Whitcroft
1928905a67cSAndy Whitcroftour @typeList = (
1938905a67cSAndy Whitcroft	qr{void},
194c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?char},
195c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?short},
196c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?int},
197c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?long},
198c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?long\s+int},
199c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?long\s+long},
200c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?long\s+long\s+int},
2018905a67cSAndy Whitcroft	qr{unsigned},
2028905a67cSAndy Whitcroft	qr{float},
2038905a67cSAndy Whitcroft	qr{double},
2048905a67cSAndy Whitcroft	qr{bool},
2058905a67cSAndy Whitcroft	qr{struct\s+$Ident},
2068905a67cSAndy Whitcroft	qr{union\s+$Ident},
2078905a67cSAndy Whitcroft	qr{enum\s+$Ident},
2088905a67cSAndy Whitcroft	qr{${Ident}_t},
2098905a67cSAndy Whitcroft	qr{${Ident}_handler},
2108905a67cSAndy Whitcroft	qr{${Ident}_handler_fn},
2118905a67cSAndy Whitcroft);
212c45dcabdSAndy Whitcroftour @modifierList = (
213c45dcabdSAndy Whitcroft	qr{fastcall},
214c45dcabdSAndy Whitcroft);
2158905a67cSAndy Whitcroft
2168905a67cSAndy Whitcroftsub build_types {
217d2172eb5SAndy Whitcroft	my $mods = "(?x:  \n" . join("|\n  ", @modifierList) . "\n)";
218d2172eb5SAndy Whitcroft	my $all = "(?x:  \n" . join("|\n  ", @typeList) . "\n)";
219c8cb2ca3SAndy Whitcroft	$Modifier	= qr{(?:$Attribute|$Sparse|$mods)};
2208905a67cSAndy Whitcroft	$NonptrType	= qr{
221d2172eb5SAndy Whitcroft			(?:$Modifier\s+|const\s+)*
222cf655043SAndy Whitcroft			(?:
223c45dcabdSAndy Whitcroft				(?:typeof|__typeof__)\s*\(\s*\**\s*$Ident\s*\)|
2248ed22cadSAndy Whitcroft				(?:$typeTypedefs\b)|
225c45dcabdSAndy Whitcroft				(?:${all}\b)
226cf655043SAndy Whitcroft			)
227c8cb2ca3SAndy Whitcroft			(?:\s+$Modifier|\s+const)*
2288905a67cSAndy Whitcroft		  }x;
2298905a67cSAndy Whitcroft	$Type	= qr{
230c45dcabdSAndy Whitcroft			$NonptrType
23165863862SAndy Whitcroft			(?:[\s\*]+\s*const|[\s\*]+|(?:\s*\[\s*\])+)?
232c8cb2ca3SAndy Whitcroft			(?:\s+$Inline|\s+$Modifier)*
2338905a67cSAndy Whitcroft		  }x;
2348905a67cSAndy Whitcroft	$Declare	= qr{(?:$Storage\s+)?$Type};
2358905a67cSAndy Whitcroft}
2368905a67cSAndy Whitcroftbuild_types();
2376c72ffaaSAndy Whitcroft
2386c72ffaaSAndy Whitcroft$chk_signoff = 0 if ($file);
2390a920b5bSAndy Whitcroft
2404a0df2efSAndy Whitcroftmy @dep_includes = ();
2414a0df2efSAndy Whitcroftmy @dep_functions = ();
2426c72ffaaSAndy Whitcroftmy $removal = "Documentation/feature-removal-schedule.txt";
2436c72ffaaSAndy Whitcroftif ($tree && -f "$root/$removal") {
24421caa13cSAndy Whitcroft	open(my $REMOVE, '<', "$root/$removal") ||
2456c72ffaaSAndy Whitcroft				die "$P: $removal: open failed - $!\n";
24621caa13cSAndy Whitcroft	while (<$REMOVE>) {
247f0a594c1SAndy Whitcroft		if (/^Check:\s+(.*\S)/) {
248f0a594c1SAndy Whitcroft			for my $entry (split(/[, ]+/, $1)) {
249f0a594c1SAndy Whitcroft				if ($entry =~ m@include/(.*)@) {
2504a0df2efSAndy Whitcroft					push(@dep_includes, $1);
2514a0df2efSAndy Whitcroft
252f0a594c1SAndy Whitcroft				} elsif ($entry !~ m@/@) {
253f0a594c1SAndy Whitcroft					push(@dep_functions, $entry);
254f0a594c1SAndy Whitcroft				}
2554a0df2efSAndy Whitcroft			}
2560a920b5bSAndy Whitcroft		}
2570a920b5bSAndy Whitcroft	}
25821caa13cSAndy Whitcroft	close($REMOVE);
2590a920b5bSAndy Whitcroft}
2600a920b5bSAndy Whitcroft
26100df344fSAndy Whitcroftmy @rawlines = ();
262c2fdda0dSAndy Whitcroftmy @lines = ();
263c2fdda0dSAndy Whitcroftmy $vname;
2646c72ffaaSAndy Whitcroftfor my $filename (@ARGV) {
26521caa13cSAndy Whitcroft	my $FILE;
2666c72ffaaSAndy Whitcroft	if ($file) {
26721caa13cSAndy Whitcroft		open($FILE, '-|', "diff -u /dev/null $filename") ||
2686c72ffaaSAndy Whitcroft			die "$P: $filename: diff failed - $!\n";
26921caa13cSAndy Whitcroft	} elsif ($filename eq '-') {
27021caa13cSAndy Whitcroft		open($FILE, '<&STDIN');
2716c72ffaaSAndy Whitcroft	} else {
27221caa13cSAndy Whitcroft		open($FILE, '<', "$filename") ||
2736c72ffaaSAndy Whitcroft			die "$P: $filename: open failed - $!\n";
2746c72ffaaSAndy Whitcroft	}
275c2fdda0dSAndy Whitcroft	if ($filename eq '-') {
276c2fdda0dSAndy Whitcroft		$vname = 'Your patch';
277c2fdda0dSAndy Whitcroft	} else {
278c2fdda0dSAndy Whitcroft		$vname = $filename;
279c2fdda0dSAndy Whitcroft	}
28021caa13cSAndy Whitcroft	while (<$FILE>) {
2810a920b5bSAndy Whitcroft		chomp;
28200df344fSAndy Whitcroft		push(@rawlines, $_);
2836c72ffaaSAndy Whitcroft	}
28421caa13cSAndy Whitcroft	close($FILE);
285c2fdda0dSAndy Whitcroft	if (!process($filename)) {
2860a920b5bSAndy Whitcroft		$exit = 1;
2870a920b5bSAndy Whitcroft	}
28800df344fSAndy Whitcroft	@rawlines = ();
28913214adfSAndy Whitcroft	@lines = ();
2900a920b5bSAndy Whitcroft}
2910a920b5bSAndy Whitcroft
2920a920b5bSAndy Whitcroftexit($exit);
2930a920b5bSAndy Whitcroft
2940a920b5bSAndy Whitcroftsub top_of_kernel_tree {
2956c72ffaaSAndy Whitcroft	my ($root) = @_;
2966c72ffaaSAndy Whitcroft
2976c72ffaaSAndy Whitcroft	my @tree_check = (
2986c72ffaaSAndy Whitcroft		"COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
2996c72ffaaSAndy Whitcroft		"README", "Documentation", "arch", "include", "drivers",
3006c72ffaaSAndy Whitcroft		"fs", "init", "ipc", "kernel", "lib", "scripts",
3016c72ffaaSAndy Whitcroft	);
3026c72ffaaSAndy Whitcroft
3036c72ffaaSAndy Whitcroft	foreach my $check (@tree_check) {
3046c72ffaaSAndy Whitcroft		if (! -e $root . '/' . $check) {
3050a920b5bSAndy Whitcroft			return 0;
3060a920b5bSAndy Whitcroft		}
3076c72ffaaSAndy Whitcroft	}
3086c72ffaaSAndy Whitcroft	return 1;
3096c72ffaaSAndy Whitcroft}
3100a920b5bSAndy Whitcroft
3110a920b5bSAndy Whitcroftsub expand_tabs {
3120a920b5bSAndy Whitcroft	my ($str) = @_;
3130a920b5bSAndy Whitcroft
3140a920b5bSAndy Whitcroft	my $res = '';
3150a920b5bSAndy Whitcroft	my $n = 0;
3160a920b5bSAndy Whitcroft	for my $c (split(//, $str)) {
3170a920b5bSAndy Whitcroft		if ($c eq "\t") {
3180a920b5bSAndy Whitcroft			$res .= ' ';
3190a920b5bSAndy Whitcroft			$n++;
3200a920b5bSAndy Whitcroft			for (; ($n % 8) != 0; $n++) {
3210a920b5bSAndy Whitcroft				$res .= ' ';
3220a920b5bSAndy Whitcroft			}
3230a920b5bSAndy Whitcroft			next;
3240a920b5bSAndy Whitcroft		}
3250a920b5bSAndy Whitcroft		$res .= $c;
3260a920b5bSAndy Whitcroft		$n++;
3270a920b5bSAndy Whitcroft	}
3280a920b5bSAndy Whitcroft
3290a920b5bSAndy Whitcroft	return $res;
3300a920b5bSAndy Whitcroft}
3316c72ffaaSAndy Whitcroftsub copy_spacing {
332773647a0SAndy Whitcroft	(my $res = shift) =~ tr/\t/ /c;
3336c72ffaaSAndy Whitcroft	return $res;
3346c72ffaaSAndy Whitcroft}
3350a920b5bSAndy Whitcroft
3364a0df2efSAndy Whitcroftsub line_stats {
3374a0df2efSAndy Whitcroft	my ($line) = @_;
3384a0df2efSAndy Whitcroft
3394a0df2efSAndy Whitcroft	# Drop the diff line leader and expand tabs
3404a0df2efSAndy Whitcroft	$line =~ s/^.//;
3414a0df2efSAndy Whitcroft	$line = expand_tabs($line);
3424a0df2efSAndy Whitcroft
3434a0df2efSAndy Whitcroft	# Pick the indent from the front of the line.
3444a0df2efSAndy Whitcroft	my ($white) = ($line =~ /^(\s*)/);
3454a0df2efSAndy Whitcroft
3464a0df2efSAndy Whitcroft	return (length($line), length($white));
3474a0df2efSAndy Whitcroft}
3484a0df2efSAndy Whitcroft
349773647a0SAndy Whitcroftmy $sanitise_quote = '';
350773647a0SAndy Whitcroft
351773647a0SAndy Whitcroftsub sanitise_line_reset {
352773647a0SAndy Whitcroft	my ($in_comment) = @_;
353773647a0SAndy Whitcroft
354773647a0SAndy Whitcroft	if ($in_comment) {
355773647a0SAndy Whitcroft		$sanitise_quote = '*/';
356773647a0SAndy Whitcroft	} else {
357773647a0SAndy Whitcroft		$sanitise_quote = '';
358773647a0SAndy Whitcroft	}
359773647a0SAndy Whitcroft}
36000df344fSAndy Whitcroftsub sanitise_line {
36100df344fSAndy Whitcroft	my ($line) = @_;
36200df344fSAndy Whitcroft
36300df344fSAndy Whitcroft	my $res = '';
36400df344fSAndy Whitcroft	my $l = '';
36500df344fSAndy Whitcroft
366c2fdda0dSAndy Whitcroft	my $qlen = 0;
367773647a0SAndy Whitcroft	my $off = 0;
368773647a0SAndy Whitcroft	my $c;
36900df344fSAndy Whitcroft
370773647a0SAndy Whitcroft	# Always copy over the diff marker.
371773647a0SAndy Whitcroft	$res = substr($line, 0, 1);
372773647a0SAndy Whitcroft
373773647a0SAndy Whitcroft	for ($off = 1; $off < length($line); $off++) {
374773647a0SAndy Whitcroft		$c = substr($line, $off, 1);
375773647a0SAndy Whitcroft
376773647a0SAndy Whitcroft		# Comments we are wacking completly including the begin
377773647a0SAndy Whitcroft		# and end, all to $;.
378773647a0SAndy Whitcroft		if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
379773647a0SAndy Whitcroft			$sanitise_quote = '*/';
380773647a0SAndy Whitcroft
381773647a0SAndy Whitcroft			substr($res, $off, 2, "$;$;");
382773647a0SAndy Whitcroft			$off++;
38300df344fSAndy Whitcroft			next;
384773647a0SAndy Whitcroft		}
38581bc0e02SAndy Whitcroft		if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
386773647a0SAndy Whitcroft			$sanitise_quote = '';
387773647a0SAndy Whitcroft			substr($res, $off, 2, "$;$;");
388773647a0SAndy Whitcroft			$off++;
389773647a0SAndy Whitcroft			next;
390773647a0SAndy Whitcroft		}
391113f04a8SDaniel Walker		if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
392113f04a8SDaniel Walker			$sanitise_quote = '//';
393113f04a8SDaniel Walker
394113f04a8SDaniel Walker			substr($res, $off, 2, $sanitise_quote);
395113f04a8SDaniel Walker			$off++;
396113f04a8SDaniel Walker			next;
397113f04a8SDaniel Walker		}
398773647a0SAndy Whitcroft
399773647a0SAndy Whitcroft		# A \ in a string means ignore the next character.
400773647a0SAndy Whitcroft		if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
401773647a0SAndy Whitcroft		    $c eq "\\") {
402773647a0SAndy Whitcroft			substr($res, $off, 2, 'XX');
403773647a0SAndy Whitcroft			$off++;
404773647a0SAndy Whitcroft			next;
405773647a0SAndy Whitcroft		}
406773647a0SAndy Whitcroft		# Regular quotes.
407773647a0SAndy Whitcroft		if ($c eq "'" || $c eq '"') {
408773647a0SAndy Whitcroft			if ($sanitise_quote eq '') {
409773647a0SAndy Whitcroft				$sanitise_quote = $c;
410773647a0SAndy Whitcroft
411773647a0SAndy Whitcroft				substr($res, $off, 1, $c);
412773647a0SAndy Whitcroft				next;
413773647a0SAndy Whitcroft			} elsif ($sanitise_quote eq $c) {
414773647a0SAndy Whitcroft				$sanitise_quote = '';
41500df344fSAndy Whitcroft			}
41600df344fSAndy Whitcroft		}
417773647a0SAndy Whitcroft
418fae17daeSAndy Whitcroft		#print "c<$c> SQ<$sanitise_quote>\n";
419773647a0SAndy Whitcroft		if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
420773647a0SAndy Whitcroft			substr($res, $off, 1, $;);
421113f04a8SDaniel Walker		} elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
422113f04a8SDaniel Walker			substr($res, $off, 1, $;);
423773647a0SAndy Whitcroft		} elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
424773647a0SAndy Whitcroft			substr($res, $off, 1, 'X');
42500df344fSAndy Whitcroft		} else {
426773647a0SAndy Whitcroft			substr($res, $off, 1, $c);
42700df344fSAndy Whitcroft		}
428c2fdda0dSAndy Whitcroft	}
429c2fdda0dSAndy Whitcroft
430113f04a8SDaniel Walker	if ($sanitise_quote eq '//') {
431113f04a8SDaniel Walker		$sanitise_quote = '';
432113f04a8SDaniel Walker	}
433113f04a8SDaniel Walker
434c2fdda0dSAndy Whitcroft	# The pathname on a #include may be surrounded by '<' and '>'.
435c45dcabdSAndy Whitcroft	if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
436c2fdda0dSAndy Whitcroft		my $clean = 'X' x length($1);
437c2fdda0dSAndy Whitcroft		$res =~ s@\<.*\>@<$clean>@;
438c2fdda0dSAndy Whitcroft
439c2fdda0dSAndy Whitcroft	# The whole of a #error is a string.
440c45dcabdSAndy Whitcroft	} elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
441c2fdda0dSAndy Whitcroft		my $clean = 'X' x length($1);
442c45dcabdSAndy Whitcroft		$res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
443c2fdda0dSAndy Whitcroft	}
444c2fdda0dSAndy Whitcroft
44500df344fSAndy Whitcroft	return $res;
44600df344fSAndy Whitcroft}
44700df344fSAndy Whitcroft
4488905a67cSAndy Whitcroftsub ctx_statement_block {
4498905a67cSAndy Whitcroft	my ($linenr, $remain, $off) = @_;
4508905a67cSAndy Whitcroft	my $line = $linenr - 1;
4518905a67cSAndy Whitcroft	my $blk = '';
4528905a67cSAndy Whitcroft	my $soff = $off;
4538905a67cSAndy Whitcroft	my $coff = $off - 1;
454773647a0SAndy Whitcroft	my $coff_set = 0;
4558905a67cSAndy Whitcroft
45613214adfSAndy Whitcroft	my $loff = 0;
45713214adfSAndy Whitcroft
4588905a67cSAndy Whitcroft	my $type = '';
4598905a67cSAndy Whitcroft	my $level = 0;
460a2750645SAndy Whitcroft	my @stack = ();
461cf655043SAndy Whitcroft	my $p;
4628905a67cSAndy Whitcroft	my $c;
4638905a67cSAndy Whitcroft	my $len = 0;
46413214adfSAndy Whitcroft
46513214adfSAndy Whitcroft	my $remainder;
4668905a67cSAndy Whitcroft	while (1) {
467a2750645SAndy Whitcroft		@stack = (['', 0]) if ($#stack == -1);
468a2750645SAndy Whitcroft
469773647a0SAndy Whitcroft		#warn "CSB: blk<$blk> remain<$remain>\n";
4708905a67cSAndy Whitcroft		# If we are about to drop off the end, pull in more
4718905a67cSAndy Whitcroft		# context.
4728905a67cSAndy Whitcroft		if ($off >= $len) {
4738905a67cSAndy Whitcroft			for (; $remain > 0; $line++) {
474dea33496SAndy Whitcroft				last if (!defined $lines[$line]);
475c2fdda0dSAndy Whitcroft				next if ($lines[$line] =~ /^-/);
4768905a67cSAndy Whitcroft				$remain--;
47713214adfSAndy Whitcroft				$loff = $len;
478c2fdda0dSAndy Whitcroft				$blk .= $lines[$line] . "\n";
4798905a67cSAndy Whitcroft				$len = length($blk);
4808905a67cSAndy Whitcroft				$line++;
4818905a67cSAndy Whitcroft				last;
4828905a67cSAndy Whitcroft			}
4838905a67cSAndy Whitcroft			# Bail if there is no further context.
4848905a67cSAndy Whitcroft			#warn "CSB: blk<$blk> off<$off> len<$len>\n";
48513214adfSAndy Whitcroft			if ($off >= $len) {
4868905a67cSAndy Whitcroft				last;
4878905a67cSAndy Whitcroft			}
4888905a67cSAndy Whitcroft		}
489cf655043SAndy Whitcroft		$p = $c;
4908905a67cSAndy Whitcroft		$c = substr($blk, $off, 1);
49113214adfSAndy Whitcroft		$remainder = substr($blk, $off);
4928905a67cSAndy Whitcroft
493773647a0SAndy Whitcroft		#warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
4944635f4fbSAndy Whitcroft
4954635f4fbSAndy Whitcroft		# Handle nested #if/#else.
4964635f4fbSAndy Whitcroft		if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
4974635f4fbSAndy Whitcroft			push(@stack, [ $type, $level ]);
4984635f4fbSAndy Whitcroft		} elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
4994635f4fbSAndy Whitcroft			($type, $level) = @{$stack[$#stack - 1]};
5004635f4fbSAndy Whitcroft		} elsif ($remainder =~ /^#\s*endif\b/) {
5014635f4fbSAndy Whitcroft			($type, $level) = @{pop(@stack)};
5024635f4fbSAndy Whitcroft		}
5034635f4fbSAndy Whitcroft
5048905a67cSAndy Whitcroft		# Statement ends at the ';' or a close '}' at the
5058905a67cSAndy Whitcroft		# outermost level.
5068905a67cSAndy Whitcroft		if ($level == 0 && $c eq ';') {
5078905a67cSAndy Whitcroft			last;
5088905a67cSAndy Whitcroft		}
5098905a67cSAndy Whitcroft
51013214adfSAndy Whitcroft		# An else is really a conditional as long as its not else if
511773647a0SAndy Whitcroft		if ($level == 0 && $coff_set == 0 &&
512773647a0SAndy Whitcroft				(!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
513773647a0SAndy Whitcroft				$remainder =~ /^(else)(?:\s|{)/ &&
514773647a0SAndy Whitcroft				$remainder !~ /^else\s+if\b/) {
515773647a0SAndy Whitcroft			$coff = $off + length($1) - 1;
516773647a0SAndy Whitcroft			$coff_set = 1;
517773647a0SAndy Whitcroft			#warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
518773647a0SAndy Whitcroft			#warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
51913214adfSAndy Whitcroft		}
52013214adfSAndy Whitcroft
5218905a67cSAndy Whitcroft		if (($type eq '' || $type eq '(') && $c eq '(') {
5228905a67cSAndy Whitcroft			$level++;
5238905a67cSAndy Whitcroft			$type = '(';
5248905a67cSAndy Whitcroft		}
5258905a67cSAndy Whitcroft		if ($type eq '(' && $c eq ')') {
5268905a67cSAndy Whitcroft			$level--;
5278905a67cSAndy Whitcroft			$type = ($level != 0)? '(' : '';
5288905a67cSAndy Whitcroft
5298905a67cSAndy Whitcroft			if ($level == 0 && $coff < $soff) {
5308905a67cSAndy Whitcroft				$coff = $off;
531773647a0SAndy Whitcroft				$coff_set = 1;
532773647a0SAndy Whitcroft				#warn "CSB: mark coff<$coff>\n";
5338905a67cSAndy Whitcroft			}
5348905a67cSAndy Whitcroft		}
5358905a67cSAndy Whitcroft		if (($type eq '' || $type eq '{') && $c eq '{') {
5368905a67cSAndy Whitcroft			$level++;
5378905a67cSAndy Whitcroft			$type = '{';
5388905a67cSAndy Whitcroft		}
5398905a67cSAndy Whitcroft		if ($type eq '{' && $c eq '}') {
5408905a67cSAndy Whitcroft			$level--;
5418905a67cSAndy Whitcroft			$type = ($level != 0)? '{' : '';
5428905a67cSAndy Whitcroft
5438905a67cSAndy Whitcroft			if ($level == 0) {
5448905a67cSAndy Whitcroft				last;
5458905a67cSAndy Whitcroft			}
5468905a67cSAndy Whitcroft		}
5478905a67cSAndy Whitcroft		$off++;
5488905a67cSAndy Whitcroft	}
549a3bb97a7SAndy Whitcroft	# We are truly at the end, so shuffle to the next line.
55013214adfSAndy Whitcroft	if ($off == $len) {
551a3bb97a7SAndy Whitcroft		$loff = $len + 1;
55213214adfSAndy Whitcroft		$line++;
55313214adfSAndy Whitcroft		$remain--;
55413214adfSAndy Whitcroft	}
5558905a67cSAndy Whitcroft
5568905a67cSAndy Whitcroft	my $statement = substr($blk, $soff, $off - $soff + 1);
5578905a67cSAndy Whitcroft	my $condition = substr($blk, $soff, $coff - $soff + 1);
5588905a67cSAndy Whitcroft
5598905a67cSAndy Whitcroft	#warn "STATEMENT<$statement>\n";
5608905a67cSAndy Whitcroft	#warn "CONDITION<$condition>\n";
5618905a67cSAndy Whitcroft
562773647a0SAndy Whitcroft	#print "coff<$coff> soff<$off> loff<$loff>\n";
56313214adfSAndy Whitcroft
56413214adfSAndy Whitcroft	return ($statement, $condition,
56513214adfSAndy Whitcroft			$line, $remain + 1, $off - $loff + 1, $level);
56613214adfSAndy Whitcroft}
56713214adfSAndy Whitcroft
568cf655043SAndy Whitcroftsub statement_lines {
569cf655043SAndy Whitcroft	my ($stmt) = @_;
570cf655043SAndy Whitcroft
571cf655043SAndy Whitcroft	# Strip the diff line prefixes and rip blank lines at start and end.
572cf655043SAndy Whitcroft	$stmt =~ s/(^|\n)./$1/g;
573cf655043SAndy Whitcroft	$stmt =~ s/^\s*//;
574cf655043SAndy Whitcroft	$stmt =~ s/\s*$//;
575cf655043SAndy Whitcroft
576cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
577cf655043SAndy Whitcroft
578cf655043SAndy Whitcroft	return $#stmt_lines + 2;
579cf655043SAndy Whitcroft}
580cf655043SAndy Whitcroft
581cf655043SAndy Whitcroftsub statement_rawlines {
582cf655043SAndy Whitcroft	my ($stmt) = @_;
583cf655043SAndy Whitcroft
584cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
585cf655043SAndy Whitcroft
586cf655043SAndy Whitcroft	return $#stmt_lines + 2;
587cf655043SAndy Whitcroft}
588cf655043SAndy Whitcroft
589cf655043SAndy Whitcroftsub statement_block_size {
590cf655043SAndy Whitcroft	my ($stmt) = @_;
591cf655043SAndy Whitcroft
592cf655043SAndy Whitcroft	$stmt =~ s/(^|\n)./$1/g;
593cf655043SAndy Whitcroft	$stmt =~ s/^\s*{//;
594cf655043SAndy Whitcroft	$stmt =~ s/}\s*$//;
595cf655043SAndy Whitcroft	$stmt =~ s/^\s*//;
596cf655043SAndy Whitcroft	$stmt =~ s/\s*$//;
597cf655043SAndy Whitcroft
598cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
599cf655043SAndy Whitcroft	my @stmt_statements = ($stmt =~ /;/g);
600cf655043SAndy Whitcroft
601cf655043SAndy Whitcroft	my $stmt_lines = $#stmt_lines + 2;
602cf655043SAndy Whitcroft	my $stmt_statements = $#stmt_statements + 1;
603cf655043SAndy Whitcroft
604cf655043SAndy Whitcroft	if ($stmt_lines > $stmt_statements) {
605cf655043SAndy Whitcroft		return $stmt_lines;
606cf655043SAndy Whitcroft	} else {
607cf655043SAndy Whitcroft		return $stmt_statements;
608cf655043SAndy Whitcroft	}
609cf655043SAndy Whitcroft}
610cf655043SAndy Whitcroft
61113214adfSAndy Whitcroftsub ctx_statement_full {
61213214adfSAndy Whitcroft	my ($linenr, $remain, $off) = @_;
61313214adfSAndy Whitcroft	my ($statement, $condition, $level);
61413214adfSAndy Whitcroft
61513214adfSAndy Whitcroft	my (@chunks);
61613214adfSAndy Whitcroft
617cf655043SAndy Whitcroft	# Grab the first conditional/block pair.
61813214adfSAndy Whitcroft	($statement, $condition, $linenr, $remain, $off, $level) =
61913214adfSAndy Whitcroft				ctx_statement_block($linenr, $remain, $off);
620773647a0SAndy Whitcroft	#print "F: c<$condition> s<$statement> remain<$remain>\n";
62113214adfSAndy Whitcroft	push(@chunks, [ $condition, $statement ]);
622cf655043SAndy Whitcroft	if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
623cf655043SAndy Whitcroft		return ($level, $linenr, @chunks);
624cf655043SAndy Whitcroft	}
625cf655043SAndy Whitcroft
626cf655043SAndy Whitcroft	# Pull in the following conditional/block pairs and see if they
627cf655043SAndy Whitcroft	# could continue the statement.
628cf655043SAndy Whitcroft	for (;;) {
62913214adfSAndy Whitcroft		($statement, $condition, $linenr, $remain, $off, $level) =
63013214adfSAndy Whitcroft				ctx_statement_block($linenr, $remain, $off);
631cf655043SAndy Whitcroft		#print "C: c<$condition> s<$statement> remain<$remain>\n";
632773647a0SAndy Whitcroft		last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
633cf655043SAndy Whitcroft		#print "C: push\n";
634cf655043SAndy Whitcroft		push(@chunks, [ $condition, $statement ]);
63513214adfSAndy Whitcroft	}
63613214adfSAndy Whitcroft
63713214adfSAndy Whitcroft	return ($level, $linenr, @chunks);
6388905a67cSAndy Whitcroft}
6398905a67cSAndy Whitcroft
6404a0df2efSAndy Whitcroftsub ctx_block_get {
641f0a594c1SAndy Whitcroft	my ($linenr, $remain, $outer, $open, $close, $off) = @_;
6424a0df2efSAndy Whitcroft	my $line;
6434a0df2efSAndy Whitcroft	my $start = $linenr - 1;
6444a0df2efSAndy Whitcroft	my $blk = '';
6454a0df2efSAndy Whitcroft	my @o;
6464a0df2efSAndy Whitcroft	my @c;
6474a0df2efSAndy Whitcroft	my @res = ();
6484a0df2efSAndy Whitcroft
649f0a594c1SAndy Whitcroft	my $level = 0;
6504635f4fbSAndy Whitcroft	my @stack = ($level);
65100df344fSAndy Whitcroft	for ($line = $start; $remain > 0; $line++) {
65200df344fSAndy Whitcroft		next if ($rawlines[$line] =~ /^-/);
65300df344fSAndy Whitcroft		$remain--;
65400df344fSAndy Whitcroft
65500df344fSAndy Whitcroft		$blk .= $rawlines[$line];
6564635f4fbSAndy Whitcroft
6574635f4fbSAndy Whitcroft		# Handle nested #if/#else.
6584635f4fbSAndy Whitcroft		if ($rawlines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
6594635f4fbSAndy Whitcroft			push(@stack, $level);
6604635f4fbSAndy Whitcroft		} elsif ($rawlines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
6614635f4fbSAndy Whitcroft			$level = $stack[$#stack - 1];
6624635f4fbSAndy Whitcroft		} elsif ($rawlines[$line] =~ /^.\s*#\s*endif\b/) {
6634635f4fbSAndy Whitcroft			$level = pop(@stack);
6644635f4fbSAndy Whitcroft		}
6654635f4fbSAndy Whitcroft
666f0a594c1SAndy Whitcroft		foreach my $c (split(//, $rawlines[$line])) {
667f0a594c1SAndy Whitcroft			##print "C<$c>L<$level><$open$close>O<$off>\n";
668f0a594c1SAndy Whitcroft			if ($off > 0) {
669f0a594c1SAndy Whitcroft				$off--;
670f0a594c1SAndy Whitcroft				next;
671f0a594c1SAndy Whitcroft			}
6724a0df2efSAndy Whitcroft
673f0a594c1SAndy Whitcroft			if ($c eq $close && $level > 0) {
674f0a594c1SAndy Whitcroft				$level--;
675f0a594c1SAndy Whitcroft				last if ($level == 0);
676f0a594c1SAndy Whitcroft			} elsif ($c eq $open) {
677f0a594c1SAndy Whitcroft				$level++;
678f0a594c1SAndy Whitcroft			}
679f0a594c1SAndy Whitcroft		}
6804a0df2efSAndy Whitcroft
681f0a594c1SAndy Whitcroft		if (!$outer || $level <= 1) {
68200df344fSAndy Whitcroft			push(@res, $rawlines[$line]);
6834a0df2efSAndy Whitcroft		}
6844a0df2efSAndy Whitcroft
685f0a594c1SAndy Whitcroft		last if ($level == 0);
6864a0df2efSAndy Whitcroft	}
6874a0df2efSAndy Whitcroft
688f0a594c1SAndy Whitcroft	return ($level, @res);
6894a0df2efSAndy Whitcroft}
6904a0df2efSAndy Whitcroftsub ctx_block_outer {
6914a0df2efSAndy Whitcroft	my ($linenr, $remain) = @_;
6924a0df2efSAndy Whitcroft
693f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
694f0a594c1SAndy Whitcroft	return @r;
6954a0df2efSAndy Whitcroft}
6964a0df2efSAndy Whitcroftsub ctx_block {
6974a0df2efSAndy Whitcroft	my ($linenr, $remain) = @_;
6984a0df2efSAndy Whitcroft
699f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
700f0a594c1SAndy Whitcroft	return @r;
701653d4876SAndy Whitcroft}
702653d4876SAndy Whitcroftsub ctx_statement {
703f0a594c1SAndy Whitcroft	my ($linenr, $remain, $off) = @_;
704f0a594c1SAndy Whitcroft
705f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
706f0a594c1SAndy Whitcroft	return @r;
707f0a594c1SAndy Whitcroft}
708f0a594c1SAndy Whitcroftsub ctx_block_level {
709653d4876SAndy Whitcroft	my ($linenr, $remain) = @_;
710653d4876SAndy Whitcroft
711f0a594c1SAndy Whitcroft	return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
7124a0df2efSAndy Whitcroft}
7139c0ca6f9SAndy Whitcroftsub ctx_statement_level {
7149c0ca6f9SAndy Whitcroft	my ($linenr, $remain, $off) = @_;
7159c0ca6f9SAndy Whitcroft
7169c0ca6f9SAndy Whitcroft	return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
7179c0ca6f9SAndy Whitcroft}
7184a0df2efSAndy Whitcroft
7194a0df2efSAndy Whitcroftsub ctx_locate_comment {
7204a0df2efSAndy Whitcroft	my ($first_line, $end_line) = @_;
7214a0df2efSAndy Whitcroft
7224a0df2efSAndy Whitcroft	# Catch a comment on the end of the line itself.
723beae6332SAndy Whitcroft	my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
7244a0df2efSAndy Whitcroft	return $current_comment if (defined $current_comment);
7254a0df2efSAndy Whitcroft
7264a0df2efSAndy Whitcroft	# Look through the context and try and figure out if there is a
7274a0df2efSAndy Whitcroft	# comment.
7284a0df2efSAndy Whitcroft	my $in_comment = 0;
7294a0df2efSAndy Whitcroft	$current_comment = '';
7304a0df2efSAndy Whitcroft	for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
73100df344fSAndy Whitcroft		my $line = $rawlines[$linenr - 1];
73200df344fSAndy Whitcroft		#warn "           $line\n";
7334a0df2efSAndy Whitcroft		if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
7344a0df2efSAndy Whitcroft			$in_comment = 1;
7354a0df2efSAndy Whitcroft		}
7364a0df2efSAndy Whitcroft		if ($line =~ m@/\*@) {
7374a0df2efSAndy Whitcroft			$in_comment = 1;
7384a0df2efSAndy Whitcroft		}
7394a0df2efSAndy Whitcroft		if (!$in_comment && $current_comment ne '') {
7404a0df2efSAndy Whitcroft			$current_comment = '';
7414a0df2efSAndy Whitcroft		}
7424a0df2efSAndy Whitcroft		$current_comment .= $line . "\n" if ($in_comment);
7434a0df2efSAndy Whitcroft		if ($line =~ m@\*/@) {
7444a0df2efSAndy Whitcroft			$in_comment = 0;
7454a0df2efSAndy Whitcroft		}
7464a0df2efSAndy Whitcroft	}
7474a0df2efSAndy Whitcroft
7484a0df2efSAndy Whitcroft	chomp($current_comment);
7494a0df2efSAndy Whitcroft	return($current_comment);
7504a0df2efSAndy Whitcroft}
7514a0df2efSAndy Whitcroftsub ctx_has_comment {
7524a0df2efSAndy Whitcroft	my ($first_line, $end_line) = @_;
7534a0df2efSAndy Whitcroft	my $cmt = ctx_locate_comment($first_line, $end_line);
7544a0df2efSAndy Whitcroft
75500df344fSAndy Whitcroft	##print "LINE: $rawlines[$end_line - 1 ]\n";
7564a0df2efSAndy Whitcroft	##print "CMMT: $cmt\n";
7574a0df2efSAndy Whitcroft
7584a0df2efSAndy Whitcroft	return ($cmt ne '');
7594a0df2efSAndy Whitcroft}
7604a0df2efSAndy Whitcroft
7614d001e4dSAndy Whitcroftsub raw_line {
7624d001e4dSAndy Whitcroft	my ($linenr, $cnt) = @_;
7634d001e4dSAndy Whitcroft
7644d001e4dSAndy Whitcroft	my $offset = $linenr - 1;
7654d001e4dSAndy Whitcroft	$cnt++;
7664d001e4dSAndy Whitcroft
7674d001e4dSAndy Whitcroft	my $line;
7684d001e4dSAndy Whitcroft	while ($cnt) {
7694d001e4dSAndy Whitcroft		$line = $rawlines[$offset++];
7704d001e4dSAndy Whitcroft		next if (defined($line) && $line =~ /^-/);
7714d001e4dSAndy Whitcroft		$cnt--;
7724d001e4dSAndy Whitcroft	}
7734d001e4dSAndy Whitcroft
7744d001e4dSAndy Whitcroft	return $line;
7754d001e4dSAndy Whitcroft}
7764d001e4dSAndy Whitcroft
7770a920b5bSAndy Whitcroftsub cat_vet {
7780a920b5bSAndy Whitcroft	my ($vet) = @_;
7799c0ca6f9SAndy Whitcroft	my ($res, $coded);
7800a920b5bSAndy Whitcroft
7819c0ca6f9SAndy Whitcroft	$res = '';
7826c72ffaaSAndy Whitcroft	while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
7836c72ffaaSAndy Whitcroft		$res .= $1;
7846c72ffaaSAndy Whitcroft		if ($2 ne '') {
7859c0ca6f9SAndy Whitcroft			$coded = sprintf("^%c", unpack('C', $2) + 64);
7866c72ffaaSAndy Whitcroft			$res .= $coded;
7876c72ffaaSAndy Whitcroft		}
7889c0ca6f9SAndy Whitcroft	}
7899c0ca6f9SAndy Whitcroft	$res =~ s/$/\$/;
7900a920b5bSAndy Whitcroft
7919c0ca6f9SAndy Whitcroft	return $res;
7920a920b5bSAndy Whitcroft}
7930a920b5bSAndy Whitcroft
794c2fdda0dSAndy Whitcroftmy $av_preprocessor = 0;
795cf655043SAndy Whitcroftmy $av_pending;
796c2fdda0dSAndy Whitcroftmy @av_paren_type;
7971f65f947SAndy Whitcroftmy $av_pend_colon;
798c2fdda0dSAndy Whitcroft
799c2fdda0dSAndy Whitcroftsub annotate_reset {
800c2fdda0dSAndy Whitcroft	$av_preprocessor = 0;
801cf655043SAndy Whitcroft	$av_pending = '_';
802cf655043SAndy Whitcroft	@av_paren_type = ('E');
8031f65f947SAndy Whitcroft	$av_pend_colon = 'O';
804c2fdda0dSAndy Whitcroft}
805c2fdda0dSAndy Whitcroft
8066c72ffaaSAndy Whitcroftsub annotate_values {
8076c72ffaaSAndy Whitcroft	my ($stream, $type) = @_;
8086c72ffaaSAndy Whitcroft
8096c72ffaaSAndy Whitcroft	my $res;
8101f65f947SAndy Whitcroft	my $var = '_' x length($stream);
8116c72ffaaSAndy Whitcroft	my $cur = $stream;
8126c72ffaaSAndy Whitcroft
813c2fdda0dSAndy Whitcroft	print "$stream\n" if ($dbg_values > 1);
8146c72ffaaSAndy Whitcroft
8156c72ffaaSAndy Whitcroft	while (length($cur)) {
816773647a0SAndy Whitcroft		@av_paren_type = ('E') if ($#av_paren_type < 0);
817cf655043SAndy Whitcroft		print " <" . join('', @av_paren_type) .
818171ae1a4SAndy Whitcroft				"> <$type> <$av_pending>" if ($dbg_values > 1);
8196c72ffaaSAndy Whitcroft		if ($cur =~ /^(\s+)/o) {
820c2fdda0dSAndy Whitcroft			print "WS($1)\n" if ($dbg_values > 1);
821c2fdda0dSAndy Whitcroft			if ($1 =~ /\n/ && $av_preprocessor) {
822cf655043SAndy Whitcroft				$type = pop(@av_paren_type);
823c2fdda0dSAndy Whitcroft				$av_preprocessor = 0;
8246c72ffaaSAndy Whitcroft			}
8256c72ffaaSAndy Whitcroft
8268ea3eb9aSAndy Whitcroft		} elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\()/) {
827c2fdda0dSAndy Whitcroft			print "DECLARE($1)\n" if ($dbg_values > 1);
8286c72ffaaSAndy Whitcroft			$type = 'T';
8296c72ffaaSAndy Whitcroft
830389a2fe5SAndy Whitcroft		} elsif ($cur =~ /^($Modifier)\s*/) {
831389a2fe5SAndy Whitcroft			print "MODIFIER($1)\n" if ($dbg_values > 1);
832389a2fe5SAndy Whitcroft			$type = 'T';
833389a2fe5SAndy Whitcroft
834c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
835171ae1a4SAndy Whitcroft			print "DEFINE($1,$2)\n" if ($dbg_values > 1);
836c2fdda0dSAndy Whitcroft			$av_preprocessor = 1;
837171ae1a4SAndy Whitcroft			push(@av_paren_type, $type);
838171ae1a4SAndy Whitcroft			if ($2 ne '') {
839cf655043SAndy Whitcroft				$av_pending = 'N';
840171ae1a4SAndy Whitcroft			}
841171ae1a4SAndy Whitcroft			$type = 'E';
842171ae1a4SAndy Whitcroft
843c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
844171ae1a4SAndy Whitcroft			print "UNDEF($1)\n" if ($dbg_values > 1);
845171ae1a4SAndy Whitcroft			$av_preprocessor = 1;
846171ae1a4SAndy Whitcroft			push(@av_paren_type, $type);
8476c72ffaaSAndy Whitcroft
848c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
849cf655043SAndy Whitcroft			print "PRE_START($1)\n" if ($dbg_values > 1);
850c2fdda0dSAndy Whitcroft			$av_preprocessor = 1;
851cf655043SAndy Whitcroft
852cf655043SAndy Whitcroft			push(@av_paren_type, $type);
853cf655043SAndy Whitcroft			push(@av_paren_type, $type);
854171ae1a4SAndy Whitcroft			$type = 'E';
855cf655043SAndy Whitcroft
856c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
857cf655043SAndy Whitcroft			print "PRE_RESTART($1)\n" if ($dbg_values > 1);
858cf655043SAndy Whitcroft			$av_preprocessor = 1;
859cf655043SAndy Whitcroft
860cf655043SAndy Whitcroft			push(@av_paren_type, $av_paren_type[$#av_paren_type]);
861cf655043SAndy Whitcroft
862171ae1a4SAndy Whitcroft			$type = 'E';
863cf655043SAndy Whitcroft
864c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:endif))/o) {
865cf655043SAndy Whitcroft			print "PRE_END($1)\n" if ($dbg_values > 1);
866cf655043SAndy Whitcroft
867cf655043SAndy Whitcroft			$av_preprocessor = 1;
868cf655043SAndy Whitcroft
869cf655043SAndy Whitcroft			# Assume all arms of the conditional end as this
870cf655043SAndy Whitcroft			# one does, and continue as if the #endif was not here.
871cf655043SAndy Whitcroft			pop(@av_paren_type);
872cf655043SAndy Whitcroft			push(@av_paren_type, $type);
873171ae1a4SAndy Whitcroft			$type = 'E';
8746c72ffaaSAndy Whitcroft
8756c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\\\n)/o) {
876c2fdda0dSAndy Whitcroft			print "PRECONT($1)\n" if ($dbg_values > 1);
8776c72ffaaSAndy Whitcroft
878171ae1a4SAndy Whitcroft		} elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
879171ae1a4SAndy Whitcroft			print "ATTR($1)\n" if ($dbg_values > 1);
880171ae1a4SAndy Whitcroft			$av_pending = $type;
881171ae1a4SAndy Whitcroft			$type = 'N';
882171ae1a4SAndy Whitcroft
8836c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
884c2fdda0dSAndy Whitcroft			print "SIZEOF($1)\n" if ($dbg_values > 1);
8856c72ffaaSAndy Whitcroft			if (defined $2) {
886cf655043SAndy Whitcroft				$av_pending = 'V';
8876c72ffaaSAndy Whitcroft			}
8886c72ffaaSAndy Whitcroft			$type = 'N';
8896c72ffaaSAndy Whitcroft
89014b111c1SAndy Whitcroft		} elsif ($cur =~ /^(if|while|for)\b/o) {
891c2fdda0dSAndy Whitcroft			print "COND($1)\n" if ($dbg_values > 1);
89214b111c1SAndy Whitcroft			$av_pending = 'E';
8936c72ffaaSAndy Whitcroft			$type = 'N';
8946c72ffaaSAndy Whitcroft
8951f65f947SAndy Whitcroft		} elsif ($cur =~/^(case)/o) {
8961f65f947SAndy Whitcroft			print "CASE($1)\n" if ($dbg_values > 1);
8971f65f947SAndy Whitcroft			$av_pend_colon = 'C';
8981f65f947SAndy Whitcroft			$type = 'N';
8991f65f947SAndy Whitcroft
90014b111c1SAndy Whitcroft		} elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
901c2fdda0dSAndy Whitcroft			print "KEYWORD($1)\n" if ($dbg_values > 1);
9026c72ffaaSAndy Whitcroft			$type = 'N';
9036c72ffaaSAndy Whitcroft
9046c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\()/o) {
905c2fdda0dSAndy Whitcroft			print "PAREN('$1')\n" if ($dbg_values > 1);
906cf655043SAndy Whitcroft			push(@av_paren_type, $av_pending);
907cf655043SAndy Whitcroft			$av_pending = '_';
9086c72ffaaSAndy Whitcroft			$type = 'N';
9096c72ffaaSAndy Whitcroft
9106c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\))/o) {
911cf655043SAndy Whitcroft			my $new_type = pop(@av_paren_type);
912cf655043SAndy Whitcroft			if ($new_type ne '_') {
913cf655043SAndy Whitcroft				$type = $new_type;
914c2fdda0dSAndy Whitcroft				print "PAREN('$1') -> $type\n"
915c2fdda0dSAndy Whitcroft							if ($dbg_values > 1);
9166c72ffaaSAndy Whitcroft			} else {
917c2fdda0dSAndy Whitcroft				print "PAREN('$1')\n" if ($dbg_values > 1);
9186c72ffaaSAndy Whitcroft			}
9196c72ffaaSAndy Whitcroft
920c8cb2ca3SAndy Whitcroft		} elsif ($cur =~ /^($Ident)\s*\(/o) {
921c2fdda0dSAndy Whitcroft			print "FUNC($1)\n" if ($dbg_values > 1);
922c8cb2ca3SAndy Whitcroft			$type = 'V';
923cf655043SAndy Whitcroft			$av_pending = 'V';
9246c72ffaaSAndy Whitcroft
9258e761b04SAndy Whitcroft		} elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
9268e761b04SAndy Whitcroft			if (defined $2 && $type eq 'C' || $type eq 'T') {
9271f65f947SAndy Whitcroft				$av_pend_colon = 'B';
9288e761b04SAndy Whitcroft			} elsif ($type eq 'E') {
9298e761b04SAndy Whitcroft				$av_pend_colon = 'L';
9301f65f947SAndy Whitcroft			}
9311f65f947SAndy Whitcroft			print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
9321f65f947SAndy Whitcroft			$type = 'V';
9331f65f947SAndy Whitcroft
9346c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Ident|$Constant)/o) {
935c2fdda0dSAndy Whitcroft			print "IDENT($1)\n" if ($dbg_values > 1);
9366c72ffaaSAndy Whitcroft			$type = 'V';
9376c72ffaaSAndy Whitcroft
9386c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Assignment)/o) {
939c2fdda0dSAndy Whitcroft			print "ASSIGN($1)\n" if ($dbg_values > 1);
9406c72ffaaSAndy Whitcroft			$type = 'N';
9416c72ffaaSAndy Whitcroft
942cf655043SAndy Whitcroft		} elsif ($cur =~/^(;|{|})/) {
943c2fdda0dSAndy Whitcroft			print "END($1)\n" if ($dbg_values > 1);
94413214adfSAndy Whitcroft			$type = 'E';
9451f65f947SAndy Whitcroft			$av_pend_colon = 'O';
94613214adfSAndy Whitcroft
9478e761b04SAndy Whitcroft		} elsif ($cur =~/^(,)/) {
9488e761b04SAndy Whitcroft			print "COMMA($1)\n" if ($dbg_values > 1);
9498e761b04SAndy Whitcroft			$type = 'C';
9508e761b04SAndy Whitcroft
9511f65f947SAndy Whitcroft		} elsif ($cur =~ /^(\?)/o) {
9521f65f947SAndy Whitcroft			print "QUESTION($1)\n" if ($dbg_values > 1);
9531f65f947SAndy Whitcroft			$type = 'N';
9541f65f947SAndy Whitcroft
9551f65f947SAndy Whitcroft		} elsif ($cur =~ /^(:)/o) {
9561f65f947SAndy Whitcroft			print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
9571f65f947SAndy Whitcroft
9581f65f947SAndy Whitcroft			substr($var, length($res), 1, $av_pend_colon);
9591f65f947SAndy Whitcroft			if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
9601f65f947SAndy Whitcroft				$type = 'E';
9611f65f947SAndy Whitcroft			} else {
9621f65f947SAndy Whitcroft				$type = 'N';
9631f65f947SAndy Whitcroft			}
9641f65f947SAndy Whitcroft			$av_pend_colon = 'O';
9651f65f947SAndy Whitcroft
9668e761b04SAndy Whitcroft		} elsif ($cur =~ /^(\[)/o) {
96713214adfSAndy Whitcroft			print "CLOSE($1)\n" if ($dbg_values > 1);
9686c72ffaaSAndy Whitcroft			$type = 'N';
9696c72ffaaSAndy Whitcroft
9700d413866SAndy Whitcroft		} elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
97174048ed8SAndy Whitcroft			my $variant;
97274048ed8SAndy Whitcroft
97374048ed8SAndy Whitcroft			print "OPV($1)\n" if ($dbg_values > 1);
97474048ed8SAndy Whitcroft			if ($type eq 'V') {
97574048ed8SAndy Whitcroft				$variant = 'B';
97674048ed8SAndy Whitcroft			} else {
97774048ed8SAndy Whitcroft				$variant = 'U';
97874048ed8SAndy Whitcroft			}
97974048ed8SAndy Whitcroft
98074048ed8SAndy Whitcroft			substr($var, length($res), 1, $variant);
98174048ed8SAndy Whitcroft			$type = 'N';
98274048ed8SAndy Whitcroft
9836c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Operators)/o) {
984c2fdda0dSAndy Whitcroft			print "OP($1)\n" if ($dbg_values > 1);
9856c72ffaaSAndy Whitcroft			if ($1 ne '++' && $1 ne '--') {
9866c72ffaaSAndy Whitcroft				$type = 'N';
9876c72ffaaSAndy Whitcroft			}
9886c72ffaaSAndy Whitcroft
9896c72ffaaSAndy Whitcroft		} elsif ($cur =~ /(^.)/o) {
990c2fdda0dSAndy Whitcroft			print "C($1)\n" if ($dbg_values > 1);
9916c72ffaaSAndy Whitcroft		}
9926c72ffaaSAndy Whitcroft		if (defined $1) {
9936c72ffaaSAndy Whitcroft			$cur = substr($cur, length($1));
9946c72ffaaSAndy Whitcroft			$res .= $type x length($1);
9956c72ffaaSAndy Whitcroft		}
9966c72ffaaSAndy Whitcroft	}
9976c72ffaaSAndy Whitcroft
9981f65f947SAndy Whitcroft	return ($res, $var);
9996c72ffaaSAndy Whitcroft}
10006c72ffaaSAndy Whitcroft
10018905a67cSAndy Whitcroftsub possible {
100213214adfSAndy Whitcroft	my ($possible, $line) = @_;
10039a974fdbSAndy Whitcroft	my $notPermitted = qr{(?:
10040776e594SAndy Whitcroft		^(?:
10050776e594SAndy Whitcroft			$Modifier|
10060776e594SAndy Whitcroft			$Storage|
10070776e594SAndy Whitcroft			$Type|
10089a974fdbSAndy Whitcroft			DEFINE_\S+
10099a974fdbSAndy Whitcroft		)$|
10109a974fdbSAndy Whitcroft		^(?:
10110776e594SAndy Whitcroft			goto|
10120776e594SAndy Whitcroft			return|
10130776e594SAndy Whitcroft			case|
10140776e594SAndy Whitcroft			else|
10150776e594SAndy Whitcroft			asm|__asm__|
10160776e594SAndy Whitcroft			do
10179a974fdbSAndy Whitcroft		)(?:\s|$)|
10180776e594SAndy Whitcroft		^(?:typedef|struct|enum)\b
10199a974fdbSAndy Whitcroft	    )}x;
10209a974fdbSAndy Whitcroft	warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
10219a974fdbSAndy Whitcroft	if ($possible !~ $notPermitted) {
1022c45dcabdSAndy Whitcroft		# Check for modifiers.
1023c45dcabdSAndy Whitcroft		$possible =~ s/\s*$Storage\s*//g;
1024c45dcabdSAndy Whitcroft		$possible =~ s/\s*$Sparse\s*//g;
1025c45dcabdSAndy Whitcroft		if ($possible =~ /^\s*$/) {
1026c45dcabdSAndy Whitcroft
1027c45dcabdSAndy Whitcroft		} elsif ($possible =~ /\s/) {
1028c45dcabdSAndy Whitcroft			$possible =~ s/\s*$Type\s*//g;
1029d2506586SAndy Whitcroft			for my $modifier (split(' ', $possible)) {
10309a974fdbSAndy Whitcroft				if ($modifier !~ $notPermitted) {
1031d2506586SAndy Whitcroft					warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1032d2506586SAndy Whitcroft					push(@modifierList, $modifier);
1033d2506586SAndy Whitcroft				}
10349a974fdbSAndy Whitcroft			}
1035c45dcabdSAndy Whitcroft
1036c45dcabdSAndy Whitcroft		} else {
103713214adfSAndy Whitcroft			warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
10388905a67cSAndy Whitcroft			push(@typeList, $possible);
1039c45dcabdSAndy Whitcroft		}
10408905a67cSAndy Whitcroft		build_types();
10410776e594SAndy Whitcroft	} else {
10420776e594SAndy Whitcroft		warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
10438905a67cSAndy Whitcroft	}
10448905a67cSAndy Whitcroft}
10458905a67cSAndy Whitcroft
10466c72ffaaSAndy Whitcroftmy $prefix = '';
10476c72ffaaSAndy Whitcroft
1048f0a594c1SAndy Whitcroftsub report {
1049773647a0SAndy Whitcroft	if (defined $tst_only && $_[0] !~ /\Q$tst_only\E/) {
1050773647a0SAndy Whitcroft		return 0;
1051773647a0SAndy Whitcroft	}
10528905a67cSAndy Whitcroft	my $line = $prefix . $_[0];
10538905a67cSAndy Whitcroft
10548905a67cSAndy Whitcroft	$line = (split('\n', $line))[0] . "\n" if ($terse);
10558905a67cSAndy Whitcroft
105613214adfSAndy Whitcroft	push(our @report, $line);
1057773647a0SAndy Whitcroft
1058773647a0SAndy Whitcroft	return 1;
1059f0a594c1SAndy Whitcroft}
1060f0a594c1SAndy Whitcroftsub report_dump {
106113214adfSAndy Whitcroft	our @report;
1062f0a594c1SAndy Whitcroft}
1063de7d4f0eSAndy Whitcroftsub ERROR {
1064773647a0SAndy Whitcroft	if (report("ERROR: $_[0]\n")) {
1065de7d4f0eSAndy Whitcroft		our $clean = 0;
10666c72ffaaSAndy Whitcroft		our $cnt_error++;
1067de7d4f0eSAndy Whitcroft	}
1068773647a0SAndy Whitcroft}
1069de7d4f0eSAndy Whitcroftsub WARN {
1070773647a0SAndy Whitcroft	if (report("WARNING: $_[0]\n")) {
1071de7d4f0eSAndy Whitcroft		our $clean = 0;
10726c72ffaaSAndy Whitcroft		our $cnt_warn++;
1073de7d4f0eSAndy Whitcroft	}
1074773647a0SAndy Whitcroft}
1075de7d4f0eSAndy Whitcroftsub CHK {
1076773647a0SAndy Whitcroft	if ($check && report("CHECK: $_[0]\n")) {
1077de7d4f0eSAndy Whitcroft		our $clean = 0;
10786c72ffaaSAndy Whitcroft		our $cnt_chk++;
10796c72ffaaSAndy Whitcroft	}
1080de7d4f0eSAndy Whitcroft}
1081de7d4f0eSAndy Whitcroft
10826ecd9674SAndy Whitcroftsub check_absolute_file {
10836ecd9674SAndy Whitcroft	my ($absolute, $herecurr) = @_;
10846ecd9674SAndy Whitcroft	my $file = $absolute;
10856ecd9674SAndy Whitcroft
10866ecd9674SAndy Whitcroft	##print "absolute<$absolute>\n";
10876ecd9674SAndy Whitcroft
10886ecd9674SAndy Whitcroft	# See if any suffix of this path is a path within the tree.
10896ecd9674SAndy Whitcroft	while ($file =~ s@^[^/]*/@@) {
10906ecd9674SAndy Whitcroft		if (-f "$root/$file") {
10916ecd9674SAndy Whitcroft			##print "file<$file>\n";
10926ecd9674SAndy Whitcroft			last;
10936ecd9674SAndy Whitcroft		}
10946ecd9674SAndy Whitcroft	}
10956ecd9674SAndy Whitcroft	if (! -f _)  {
10966ecd9674SAndy Whitcroft		return 0;
10976ecd9674SAndy Whitcroft	}
10986ecd9674SAndy Whitcroft
10996ecd9674SAndy Whitcroft	# It is, so see if the prefix is acceptable.
11006ecd9674SAndy Whitcroft	my $prefix = $absolute;
11016ecd9674SAndy Whitcroft	substr($prefix, -length($file)) = '';
11026ecd9674SAndy Whitcroft
11036ecd9674SAndy Whitcroft	##print "prefix<$prefix>\n";
11046ecd9674SAndy Whitcroft	if ($prefix ne ".../") {
11056ecd9674SAndy Whitcroft		WARN("use relative pathname instead of absolute in changelog text\n" . $herecurr);
11066ecd9674SAndy Whitcroft	}
11076ecd9674SAndy Whitcroft}
11086ecd9674SAndy Whitcroft
11090a920b5bSAndy Whitcroftsub process {
11100a920b5bSAndy Whitcroft	my $filename = shift;
11110a920b5bSAndy Whitcroft
11120a920b5bSAndy Whitcroft	my $linenr=0;
11130a920b5bSAndy Whitcroft	my $prevline="";
1114c2fdda0dSAndy Whitcroft	my $prevrawline="";
11150a920b5bSAndy Whitcroft	my $stashline="";
1116c2fdda0dSAndy Whitcroft	my $stashrawline="";
11170a920b5bSAndy Whitcroft
11184a0df2efSAndy Whitcroft	my $length;
11190a920b5bSAndy Whitcroft	my $indent;
11200a920b5bSAndy Whitcroft	my $previndent=0;
11210a920b5bSAndy Whitcroft	my $stashindent=0;
11220a920b5bSAndy Whitcroft
1123de7d4f0eSAndy Whitcroft	our $clean = 1;
11240a920b5bSAndy Whitcroft	my $signoff = 0;
11250a920b5bSAndy Whitcroft	my $is_patch = 0;
11260a920b5bSAndy Whitcroft
112713214adfSAndy Whitcroft	our @report = ();
11286c72ffaaSAndy Whitcroft	our $cnt_lines = 0;
11296c72ffaaSAndy Whitcroft	our $cnt_error = 0;
11306c72ffaaSAndy Whitcroft	our $cnt_warn = 0;
11316c72ffaaSAndy Whitcroft	our $cnt_chk = 0;
11326c72ffaaSAndy Whitcroft
11330a920b5bSAndy Whitcroft	# Trace the real file/line as we go.
11340a920b5bSAndy Whitcroft	my $realfile = '';
11350a920b5bSAndy Whitcroft	my $realline = 0;
11360a920b5bSAndy Whitcroft	my $realcnt = 0;
11370a920b5bSAndy Whitcroft	my $here = '';
11380a920b5bSAndy Whitcroft	my $in_comment = 0;
1139c2fdda0dSAndy Whitcroft	my $comment_edge = 0;
11400a920b5bSAndy Whitcroft	my $first_line = 0;
11411e855726SWolfram Sang	my $p1_prefix = '';
11420a920b5bSAndy Whitcroft
114313214adfSAndy Whitcroft	my $prev_values = 'E';
114413214adfSAndy Whitcroft
114513214adfSAndy Whitcroft	# suppression flags
1146773647a0SAndy Whitcroft	my %suppress_ifbraces;
1147170d3a22SAndy Whitcroft	my %suppress_whiletrailers;
11482b474a1aSAndy Whitcroft	my %suppress_export;
1149653d4876SAndy Whitcroft
1150c2fdda0dSAndy Whitcroft	# Pre-scan the patch sanitizing the lines.
1151de7d4f0eSAndy Whitcroft	# Pre-scan the patch looking for any __setup documentation.
1152c2fdda0dSAndy Whitcroft	#
1153de7d4f0eSAndy Whitcroft	my @setup_docs = ();
1154de7d4f0eSAndy Whitcroft	my $setup_docs = 0;
1155773647a0SAndy Whitcroft
1156773647a0SAndy Whitcroft	sanitise_line_reset();
1157c2fdda0dSAndy Whitcroft	my $line;
1158c2fdda0dSAndy Whitcroft	foreach my $rawline (@rawlines) {
1159773647a0SAndy Whitcroft		$linenr++;
1160773647a0SAndy Whitcroft		$line = $rawline;
1161c2fdda0dSAndy Whitcroft
1162773647a0SAndy Whitcroft		if ($rawline=~/^\+\+\+\s+(\S+)/) {
1163de7d4f0eSAndy Whitcroft			$setup_docs = 0;
1164de7d4f0eSAndy Whitcroft			if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
1165de7d4f0eSAndy Whitcroft				$setup_docs = 1;
1166de7d4f0eSAndy Whitcroft			}
1167773647a0SAndy Whitcroft			#next;
1168de7d4f0eSAndy Whitcroft		}
1169773647a0SAndy Whitcroft		if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1170773647a0SAndy Whitcroft			$realline=$1-1;
1171773647a0SAndy Whitcroft			if (defined $2) {
1172773647a0SAndy Whitcroft				$realcnt=$3+1;
1173773647a0SAndy Whitcroft			} else {
1174773647a0SAndy Whitcroft				$realcnt=1+1;
1175773647a0SAndy Whitcroft			}
1176c45dcabdSAndy Whitcroft			$in_comment = 0;
1177773647a0SAndy Whitcroft
1178773647a0SAndy Whitcroft			# Guestimate if this is a continuing comment.  Run
1179773647a0SAndy Whitcroft			# the context looking for a comment "edge".  If this
1180773647a0SAndy Whitcroft			# edge is a close comment then we must be in a comment
1181773647a0SAndy Whitcroft			# at context start.
1182773647a0SAndy Whitcroft			my $edge;
118301fa9147SAndy Whitcroft			my $cnt = $realcnt;
118401fa9147SAndy Whitcroft			for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
118501fa9147SAndy Whitcroft				next if (defined $rawlines[$ln - 1] &&
118601fa9147SAndy Whitcroft					 $rawlines[$ln - 1] =~ /^-/);
118701fa9147SAndy Whitcroft				$cnt--;
118801fa9147SAndy Whitcroft				#print "RAW<$rawlines[$ln - 1]>\n";
1189721c1cb6SAndy Whitcroft				last if (!defined $rawlines[$ln - 1]);
1190fae17daeSAndy Whitcroft				if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
1191fae17daeSAndy Whitcroft				    $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
1192fae17daeSAndy Whitcroft					($edge) = $1;
1193fae17daeSAndy Whitcroft					last;
1194fae17daeSAndy Whitcroft				}
1195773647a0SAndy Whitcroft			}
1196773647a0SAndy Whitcroft			if (defined $edge && $edge eq '*/') {
1197773647a0SAndy Whitcroft				$in_comment = 1;
1198773647a0SAndy Whitcroft			}
1199773647a0SAndy Whitcroft
1200773647a0SAndy Whitcroft			# Guestimate if this is a continuing comment.  If this
1201773647a0SAndy Whitcroft			# is the start of a diff block and this line starts
1202773647a0SAndy Whitcroft			# ' *' then it is very likely a comment.
1203773647a0SAndy Whitcroft			if (!defined $edge &&
120483242e0cSAndy Whitcroft			    $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
1205773647a0SAndy Whitcroft			{
1206773647a0SAndy Whitcroft				$in_comment = 1;
1207773647a0SAndy Whitcroft			}
1208773647a0SAndy Whitcroft
1209773647a0SAndy Whitcroft			##print "COMMENT:$in_comment edge<$edge> $rawline\n";
1210773647a0SAndy Whitcroft			sanitise_line_reset($in_comment);
1211773647a0SAndy Whitcroft
1212171ae1a4SAndy Whitcroft		} elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
1213773647a0SAndy Whitcroft			# Standardise the strings and chars within the input to
1214171ae1a4SAndy Whitcroft			# simplify matching -- only bother with positive lines.
1215773647a0SAndy Whitcroft			$line = sanitise_line($rawline);
1216773647a0SAndy Whitcroft		}
1217773647a0SAndy Whitcroft		push(@lines, $line);
1218773647a0SAndy Whitcroft
1219773647a0SAndy Whitcroft		if ($realcnt > 1) {
1220773647a0SAndy Whitcroft			$realcnt-- if ($line =~ /^(?:\+| |$)/);
1221773647a0SAndy Whitcroft		} else {
1222773647a0SAndy Whitcroft			$realcnt = 0;
1223773647a0SAndy Whitcroft		}
1224773647a0SAndy Whitcroft
1225773647a0SAndy Whitcroft		#print "==>$rawline\n";
1226773647a0SAndy Whitcroft		#print "-->$line\n";
1227de7d4f0eSAndy Whitcroft
1228de7d4f0eSAndy Whitcroft		if ($setup_docs && $line =~ /^\+/) {
1229de7d4f0eSAndy Whitcroft			push(@setup_docs, $line);
1230de7d4f0eSAndy Whitcroft		}
1231de7d4f0eSAndy Whitcroft	}
1232de7d4f0eSAndy Whitcroft
12336c72ffaaSAndy Whitcroft	$prefix = '';
12346c72ffaaSAndy Whitcroft
1235773647a0SAndy Whitcroft	$realcnt = 0;
1236773647a0SAndy Whitcroft	$linenr = 0;
12370a920b5bSAndy Whitcroft	foreach my $line (@lines) {
12380a920b5bSAndy Whitcroft		$linenr++;
12390a920b5bSAndy Whitcroft
1240c2fdda0dSAndy Whitcroft		my $rawline = $rawlines[$linenr - 1];
12416c72ffaaSAndy Whitcroft
12420a920b5bSAndy Whitcroft#extract the line range in the file after the patch is applied
12436c72ffaaSAndy Whitcroft		if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
12440a920b5bSAndy Whitcroft			$is_patch = 1;
12454a0df2efSAndy Whitcroft			$first_line = $linenr + 1;
12460a920b5bSAndy Whitcroft			$realline=$1-1;
12470a920b5bSAndy Whitcroft			if (defined $2) {
12480a920b5bSAndy Whitcroft				$realcnt=$3+1;
12490a920b5bSAndy Whitcroft			} else {
12500a920b5bSAndy Whitcroft				$realcnt=1+1;
12510a920b5bSAndy Whitcroft			}
1252c2fdda0dSAndy Whitcroft			annotate_reset();
125313214adfSAndy Whitcroft			$prev_values = 'E';
125413214adfSAndy Whitcroft
1255773647a0SAndy Whitcroft			%suppress_ifbraces = ();
1256170d3a22SAndy Whitcroft			%suppress_whiletrailers = ();
12572b474a1aSAndy Whitcroft			%suppress_export = ();
12580a920b5bSAndy Whitcroft			next;
12590a920b5bSAndy Whitcroft
12604a0df2efSAndy Whitcroft# track the line number as we move through the hunk, note that
12614a0df2efSAndy Whitcroft# new versions of GNU diff omit the leading space on completely
12624a0df2efSAndy Whitcroft# blank context lines so we need to count that too.
1263773647a0SAndy Whitcroft		} elsif ($line =~ /^( |\+|$)/) {
12640a920b5bSAndy Whitcroft			$realline++;
1265d8aaf121SAndy Whitcroft			$realcnt-- if ($realcnt != 0);
12660a920b5bSAndy Whitcroft
12674a0df2efSAndy Whitcroft			# Measure the line length and indent.
1268c2fdda0dSAndy Whitcroft			($length, $indent) = line_stats($rawline);
12690a920b5bSAndy Whitcroft
12700a920b5bSAndy Whitcroft			# Track the previous line.
12710a920b5bSAndy Whitcroft			($prevline, $stashline) = ($stashline, $line);
12720a920b5bSAndy Whitcroft			($previndent, $stashindent) = ($stashindent, $indent);
1273c2fdda0dSAndy Whitcroft			($prevrawline, $stashrawline) = ($stashrawline, $rawline);
1274c2fdda0dSAndy Whitcroft
1275773647a0SAndy Whitcroft			#warn "line<$line>\n";
12766c72ffaaSAndy Whitcroft
1277d8aaf121SAndy Whitcroft		} elsif ($realcnt == 1) {
1278d8aaf121SAndy Whitcroft			$realcnt--;
12790a920b5bSAndy Whitcroft		}
12800a920b5bSAndy Whitcroft
1281cc77cdcaSAndy Whitcroft		my $hunk_line = ($realcnt != 0);
1282cc77cdcaSAndy Whitcroft
12830a920b5bSAndy Whitcroft#make up the handle for any error we report on this line
1284773647a0SAndy Whitcroft		$prefix = "$filename:$realline: " if ($emacs && $file);
1285773647a0SAndy Whitcroft		$prefix = "$filename:$linenr: " if ($emacs && !$file);
1286773647a0SAndy Whitcroft
12876c72ffaaSAndy Whitcroft		$here = "#$linenr: " if (!$file);
12886c72ffaaSAndy Whitcroft		$here = "#$realline: " if ($file);
1289773647a0SAndy Whitcroft
1290773647a0SAndy Whitcroft		# extract the filename as it passes
1291773647a0SAndy Whitcroft		if ($line=~/^\+\+\+\s+(\S+)/) {
1292773647a0SAndy Whitcroft			$realfile = $1;
12931e855726SWolfram Sang			$realfile =~ s@^([^/]*)/@@;
12941e855726SWolfram Sang
12951e855726SWolfram Sang			$p1_prefix = $1;
1296e2f7aa4bSAndy Whitcroft			if (!$file && $tree && $p1_prefix ne '' &&
1297e2f7aa4bSAndy Whitcroft			    -e "$root/$p1_prefix") {
12981e855726SWolfram Sang				WARN("patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
12991e855726SWolfram Sang			}
1300773647a0SAndy Whitcroft
1301c1ab3326SAndy Whitcroft			if ($realfile =~ m@^include/asm/@) {
1302773647a0SAndy Whitcroft				ERROR("do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
1303773647a0SAndy Whitcroft			}
1304773647a0SAndy Whitcroft			next;
1305773647a0SAndy Whitcroft		}
1306773647a0SAndy Whitcroft
1307389834b6SRandy Dunlap		$here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
13080a920b5bSAndy Whitcroft
1309c2fdda0dSAndy Whitcroft		my $hereline = "$here\n$rawline\n";
1310c2fdda0dSAndy Whitcroft		my $herecurr = "$here\n$rawline\n";
1311c2fdda0dSAndy Whitcroft		my $hereprev = "$here\n$prevrawline\n$rawline\n";
13120a920b5bSAndy Whitcroft
13136c72ffaaSAndy Whitcroft		$cnt_lines++ if ($realcnt != 0);
13146c72ffaaSAndy Whitcroft
13150a920b5bSAndy Whitcroft#check the patch for a signoff:
1316d8aaf121SAndy Whitcroft		if ($line =~ /^\s*signed-off-by:/i) {
13174a0df2efSAndy Whitcroft			# This is a signoff, if ugly, so do not double report.
13184a0df2efSAndy Whitcroft			$signoff++;
13190a920b5bSAndy Whitcroft			if (!($line =~ /^\s*Signed-off-by:/)) {
1320de7d4f0eSAndy Whitcroft				WARN("Signed-off-by: is the preferred form\n" .
1321de7d4f0eSAndy Whitcroft					$herecurr);
13220a920b5bSAndy Whitcroft			}
13230a920b5bSAndy Whitcroft			if ($line =~ /^\s*signed-off-by:\S/i) {
1324773647a0SAndy Whitcroft				WARN("space required after Signed-off-by:\n" .
1325de7d4f0eSAndy Whitcroft					$herecurr);
13260a920b5bSAndy Whitcroft			}
13270a920b5bSAndy Whitcroft		}
13280a920b5bSAndy Whitcroft
132900df344fSAndy Whitcroft# Check for wrappage within a valid hunk of the file
13308905a67cSAndy Whitcroft		if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
1331de7d4f0eSAndy Whitcroft			ERROR("patch seems to be corrupt (line wrapped?)\n" .
13326c72ffaaSAndy Whitcroft				$herecurr) if (!$emitted_corrupt++);
1333de7d4f0eSAndy Whitcroft		}
1334de7d4f0eSAndy Whitcroft
13356ecd9674SAndy Whitcroft# Check for absolute kernel paths.
13366ecd9674SAndy Whitcroft		if ($tree) {
13376ecd9674SAndy Whitcroft			while ($line =~ m{(?:^|\s)(/\S*)}g) {
13386ecd9674SAndy Whitcroft				my $file = $1;
13396ecd9674SAndy Whitcroft
13406ecd9674SAndy Whitcroft				if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
13416ecd9674SAndy Whitcroft				    check_absolute_file($1, $herecurr)) {
13426ecd9674SAndy Whitcroft					#
13436ecd9674SAndy Whitcroft				} else {
13446ecd9674SAndy Whitcroft					check_absolute_file($file, $herecurr);
13456ecd9674SAndy Whitcroft				}
13466ecd9674SAndy Whitcroft			}
13476ecd9674SAndy Whitcroft		}
13486ecd9674SAndy Whitcroft
1349de7d4f0eSAndy Whitcroft# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
1350de7d4f0eSAndy Whitcroft		if (($realfile =~ /^$/ || $line =~ /^\+/) &&
1351171ae1a4SAndy Whitcroft		    $rawline !~ m/^$UTF8*$/) {
1352171ae1a4SAndy Whitcroft			my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
1353171ae1a4SAndy Whitcroft
1354171ae1a4SAndy Whitcroft			my $blank = copy_spacing($rawline);
1355171ae1a4SAndy Whitcroft			my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
1356171ae1a4SAndy Whitcroft			my $hereptr = "$hereline$ptr\n";
1357171ae1a4SAndy Whitcroft
1358171ae1a4SAndy Whitcroft			ERROR("Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
135900df344fSAndy Whitcroft		}
13600a920b5bSAndy Whitcroft
136130670854SAndy Whitcroft# ignore non-hunk lines and lines being removed
136230670854SAndy Whitcroft		next if (!$hunk_line || $line =~ /^-/);
136300df344fSAndy Whitcroft
13640a920b5bSAndy Whitcroft#trailing whitespace
13659c0ca6f9SAndy Whitcroft		if ($line =~ /^\+.*\015/) {
1366c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
13679c0ca6f9SAndy Whitcroft			ERROR("DOS line endings\n" . $herevet);
13689c0ca6f9SAndy Whitcroft
1369c2fdda0dSAndy Whitcroft		} elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
1370c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1371de7d4f0eSAndy Whitcroft			ERROR("trailing whitespace\n" . $herevet);
13720a920b5bSAndy Whitcroft		}
13735368df20SAndy Whitcroft
13745368df20SAndy Whitcroft# check we are in a valid source file if not then ignore this hunk
13755368df20SAndy Whitcroft		next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
13765368df20SAndy Whitcroft
13770a920b5bSAndy Whitcroft#80 column limit
1378c45dcabdSAndy Whitcroft		if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ &&
1379f4c014c0SAndy Whitcroft		    $rawline !~ /^.\s*\*\s*\@$Ident\s/ &&
1380f4c014c0SAndy Whitcroft		    $line !~ /^\+\s*printk\s*\(\s*(?:KERN_\S+\s*)?"[X\t]*"\s*(?:,|\)\s*;)\s*$/ &&
1381f4c014c0SAndy Whitcroft		    $length > 80)
1382c45dcabdSAndy Whitcroft		{
1383de7d4f0eSAndy Whitcroft			WARN("line over 80 characters\n" . $herecurr);
13840a920b5bSAndy Whitcroft		}
13850a920b5bSAndy Whitcroft
13868905a67cSAndy Whitcroft# check for adding lines without a newline.
13878905a67cSAndy Whitcroft		if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
13888905a67cSAndy Whitcroft			WARN("adding a line without newline at end of file\n" . $herecurr);
13898905a67cSAndy Whitcroft		}
13908905a67cSAndy Whitcroft
139142e41c54SMike Frysinger# Blackfin: use hi/lo macros
139242e41c54SMike Frysinger		if ($realfile =~ m@arch/blackfin/.*\.S$@) {
139342e41c54SMike Frysinger			if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
139442e41c54SMike Frysinger				my $herevet = "$here\n" . cat_vet($line) . "\n";
139542e41c54SMike Frysinger				ERROR("use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
139642e41c54SMike Frysinger			}
139742e41c54SMike Frysinger			if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
139842e41c54SMike Frysinger				my $herevet = "$here\n" . cat_vet($line) . "\n";
139942e41c54SMike Frysinger				ERROR("use the HI() macro, not (... >> 16)\n" . $herevet);
140042e41c54SMike Frysinger			}
140142e41c54SMike Frysinger		}
140242e41c54SMike Frysinger
1403b9ea10d6SAndy Whitcroft# check we are in a valid source file C or perl if not then ignore this hunk
1404b9ea10d6SAndy Whitcroft		next if ($realfile !~ /\.(h|c|pl)$/);
14050a920b5bSAndy Whitcroft
14060a920b5bSAndy Whitcroft# at the beginning of a line any tabs must come first and anything
14070a920b5bSAndy Whitcroft# more than 8 must use tabs.
1408c2fdda0dSAndy Whitcroft		if ($rawline =~ /^\+\s* \t\s*\S/ ||
1409c2fdda0dSAndy Whitcroft		    $rawline =~ /^\+\s*        \s*/) {
1410c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1411171ae1a4SAndy Whitcroft			ERROR("code indent should use tabs where possible\n" . $herevet);
14120a920b5bSAndy Whitcroft		}
14130a920b5bSAndy Whitcroft
1414b9ea10d6SAndy Whitcroft# check we are in a valid C source file if not then ignore this hunk
1415b9ea10d6SAndy Whitcroft		next if ($realfile !~ /\.(h|c)$/);
1416b9ea10d6SAndy Whitcroft
1417c2fdda0dSAndy Whitcroft# check for RCS/CVS revision markers
1418cf655043SAndy Whitcroft		if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
1419c2fdda0dSAndy Whitcroft			WARN("CVS style keyword markers, these will _not_ be updated\n". $herecurr);
1420c2fdda0dSAndy Whitcroft		}
142122f2a2efSAndy Whitcroft
142242e41c54SMike Frysinger# Blackfin: don't use __builtin_bfin_[cs]sync
142342e41c54SMike Frysinger		if ($line =~ /__builtin_bfin_csync/) {
142442e41c54SMike Frysinger			my $herevet = "$here\n" . cat_vet($line) . "\n";
142542e41c54SMike Frysinger			ERROR("use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
142642e41c54SMike Frysinger		}
142742e41c54SMike Frysinger		if ($line =~ /__builtin_bfin_ssync/) {
142842e41c54SMike Frysinger			my $herevet = "$here\n" . cat_vet($line) . "\n";
142942e41c54SMike Frysinger			ERROR("use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
143042e41c54SMike Frysinger		}
143142e41c54SMike Frysinger
14329c0ca6f9SAndy Whitcroft# Check for potential 'bare' types
14332b474a1aSAndy Whitcroft		my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
14342b474a1aSAndy Whitcroft		    $realline_next);
14359c9ba34eSAndy Whitcroft		if ($realcnt && $line =~ /.\s*\S/) {
1436170d3a22SAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
1437f5fe35ddSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0);
1438171ae1a4SAndy Whitcroft			$stat =~ s/\n./\n /g;
1439171ae1a4SAndy Whitcroft			$cond =~ s/\n./\n /g;
1440171ae1a4SAndy Whitcroft
14412b474a1aSAndy Whitcroft			# Find the real next line.
14422b474a1aSAndy Whitcroft			$realline_next = $line_nr_next;
14432b474a1aSAndy Whitcroft			if (defined $realline_next &&
14442b474a1aSAndy Whitcroft			    (!defined $lines[$realline_next - 1] ||
14452b474a1aSAndy Whitcroft			     substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
14462b474a1aSAndy Whitcroft				$realline_next++;
14472b474a1aSAndy Whitcroft			}
14482b474a1aSAndy Whitcroft
1449171ae1a4SAndy Whitcroft			my $s = $stat;
1450171ae1a4SAndy Whitcroft			$s =~ s/{.*$//s;
1451cf655043SAndy Whitcroft
1452c2fdda0dSAndy Whitcroft			# Ignore goto labels.
1453171ae1a4SAndy Whitcroft			if ($s =~ /$Ident:\*$/s) {
1454c2fdda0dSAndy Whitcroft
1455c2fdda0dSAndy Whitcroft			# Ignore functions being called
1456171ae1a4SAndy Whitcroft			} elsif ($s =~ /^.\s*$Ident\s*\(/s) {
1457c2fdda0dSAndy Whitcroft
1458463f2864SAndy Whitcroft			} elsif ($s =~ /^.\s*else\b/s) {
1459463f2864SAndy Whitcroft
1460c45dcabdSAndy Whitcroft			# declarations always start with types
1461d2506586SAndy 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) {
1462c45dcabdSAndy Whitcroft				my $type = $1;
1463c45dcabdSAndy Whitcroft				$type =~ s/\s+/ /g;
1464c45dcabdSAndy Whitcroft				possible($type, "A:" . $s);
1465c45dcabdSAndy Whitcroft
14666c72ffaaSAndy Whitcroft			# definitions in global scope can only start with types
1467a6a84062SAndy Whitcroft			} elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
1468c45dcabdSAndy Whitcroft				possible($1, "B:" . $s);
1469c2fdda0dSAndy Whitcroft			}
14708905a67cSAndy Whitcroft
14716c72ffaaSAndy Whitcroft			# any (foo ... *) is a pointer cast, and foo is a type
147265863862SAndy Whitcroft			while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
1473c45dcabdSAndy Whitcroft				possible($1, "C:" . $s);
14749c0ca6f9SAndy Whitcroft			}
14758905a67cSAndy Whitcroft
14768905a67cSAndy Whitcroft			# Check for any sort of function declaration.
14778905a67cSAndy Whitcroft			# int foo(something bar, other baz);
14788905a67cSAndy Whitcroft			# void (*store_gdt)(x86_descr_ptr *);
1479171ae1a4SAndy Whitcroft			if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
14808905a67cSAndy Whitcroft				my ($name_len) = length($1);
14818905a67cSAndy Whitcroft
1482cf655043SAndy Whitcroft				my $ctx = $s;
1483773647a0SAndy Whitcroft				substr($ctx, 0, $name_len + 1, '');
14848905a67cSAndy Whitcroft				$ctx =~ s/\)[^\)]*$//;
1485cf655043SAndy Whitcroft
14868905a67cSAndy Whitcroft				for my $arg (split(/\s*,\s*/, $ctx)) {
1487c45dcabdSAndy Whitcroft					if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
14888905a67cSAndy Whitcroft
1489c45dcabdSAndy Whitcroft						possible($1, "D:" . $s);
14908905a67cSAndy Whitcroft					}
14918905a67cSAndy Whitcroft				}
14928905a67cSAndy Whitcroft			}
14938905a67cSAndy Whitcroft
14949c0ca6f9SAndy Whitcroft		}
14959c0ca6f9SAndy Whitcroft
149600df344fSAndy Whitcroft#
149700df344fSAndy Whitcroft# Checks which may be anchored in the context.
149800df344fSAndy Whitcroft#
149900df344fSAndy Whitcroft
150000df344fSAndy Whitcroft# Check for switch () and associated case and default
150100df344fSAndy Whitcroft# statements should be at the same indent.
150200df344fSAndy Whitcroft		if ($line=~/\bswitch\s*\(.*\)/) {
150300df344fSAndy Whitcroft			my $err = '';
150400df344fSAndy Whitcroft			my $sep = '';
150500df344fSAndy Whitcroft			my @ctx = ctx_block_outer($linenr, $realcnt);
150600df344fSAndy Whitcroft			shift(@ctx);
150700df344fSAndy Whitcroft			for my $ctx (@ctx) {
150800df344fSAndy Whitcroft				my ($clen, $cindent) = line_stats($ctx);
150900df344fSAndy Whitcroft				if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
151000df344fSAndy Whitcroft							$indent != $cindent) {
151100df344fSAndy Whitcroft					$err .= "$sep$ctx\n";
151200df344fSAndy Whitcroft					$sep = '';
151300df344fSAndy Whitcroft				} else {
151400df344fSAndy Whitcroft					$sep = "[...]\n";
151500df344fSAndy Whitcroft				}
151600df344fSAndy Whitcroft			}
151700df344fSAndy Whitcroft			if ($err ne '') {
15189c0ca6f9SAndy Whitcroft				ERROR("switch and case should be at the same indent\n$hereline$err");
1519de7d4f0eSAndy Whitcroft			}
1520de7d4f0eSAndy Whitcroft		}
1521de7d4f0eSAndy Whitcroft
1522de7d4f0eSAndy Whitcroft# if/while/etc brace do not go on next line, unless defining a do while loop,
1523de7d4f0eSAndy Whitcroft# or if that brace on the next line is for something else
1524c45dcabdSAndy Whitcroft		if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
1525773647a0SAndy Whitcroft			my $pre_ctx = "$1$2";
1526773647a0SAndy Whitcroft
15279c0ca6f9SAndy Whitcroft			my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
1528de7d4f0eSAndy Whitcroft			my $ctx_cnt = $realcnt - $#ctx - 1;
1529de7d4f0eSAndy Whitcroft			my $ctx = join("\n", @ctx);
1530de7d4f0eSAndy Whitcroft
1531548596d5SAndy Whitcroft			my $ctx_ln = $linenr;
1532548596d5SAndy Whitcroft			my $ctx_skip = $realcnt;
1533de7d4f0eSAndy Whitcroft
1534548596d5SAndy Whitcroft			while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
1535548596d5SAndy Whitcroft					defined $lines[$ctx_ln - 1] &&
1536548596d5SAndy Whitcroft					$lines[$ctx_ln - 1] =~ /^-/)) {
1537548596d5SAndy Whitcroft				##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
1538548596d5SAndy Whitcroft				$ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
1539773647a0SAndy Whitcroft				$ctx_ln++;
1540773647a0SAndy Whitcroft			}
1541548596d5SAndy Whitcroft
154253210168SAndy Whitcroft			#print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
154353210168SAndy Whitcroft			#print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
1544773647a0SAndy Whitcroft
1545773647a0SAndy Whitcroft			if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln -1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
1546773647a0SAndy Whitcroft				ERROR("that open brace { should be on the previous line\n" .
1547c45dcabdSAndy Whitcroft					"$here\n$ctx\n$lines[$ctx_ln - 1]\n");
154800df344fSAndy Whitcroft			}
1549773647a0SAndy Whitcroft			if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
1550773647a0SAndy Whitcroft			    $ctx =~ /\)\s*\;\s*$/ &&
1551773647a0SAndy Whitcroft			    defined $lines[$ctx_ln - 1])
1552773647a0SAndy Whitcroft			{
15539c0ca6f9SAndy Whitcroft				my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
15549c0ca6f9SAndy Whitcroft				if ($nindent > $indent) {
1555773647a0SAndy Whitcroft					WARN("trailing semicolon indicates no statements, indent implies otherwise\n" .
1556c45dcabdSAndy Whitcroft						"$here\n$ctx\n$lines[$ctx_ln - 1]\n");
15579c0ca6f9SAndy Whitcroft				}
15589c0ca6f9SAndy Whitcroft			}
155900df344fSAndy Whitcroft		}
156000df344fSAndy Whitcroft
15614d001e4dSAndy Whitcroft# Check relative indent for conditionals and blocks.
15624d001e4dSAndy Whitcroft		if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
15634d001e4dSAndy Whitcroft			my ($s, $c) = ($stat, $cond);
15644d001e4dSAndy Whitcroft
15654d001e4dSAndy Whitcroft			substr($s, 0, length($c), '');
15664d001e4dSAndy Whitcroft
15674d001e4dSAndy Whitcroft			# Make sure we remove the line prefixes as we have
15684d001e4dSAndy Whitcroft			# none on the first line, and are going to readd them
15694d001e4dSAndy Whitcroft			# where necessary.
15704d001e4dSAndy Whitcroft			$s =~ s/\n./\n/gs;
15714d001e4dSAndy Whitcroft
15724d001e4dSAndy Whitcroft			# Find out how long the conditional actually is.
15736f779c18SAndy Whitcroft			my @newlines = ($c =~ /\n/gs);
15746f779c18SAndy Whitcroft			my $cond_lines = 1 + $#newlines;
15754d001e4dSAndy Whitcroft
15764d001e4dSAndy Whitcroft			# We want to check the first line inside the block
15774d001e4dSAndy Whitcroft			# starting at the end of the conditional, so remove:
15784d001e4dSAndy Whitcroft			#  1) any blank line termination
15794d001e4dSAndy Whitcroft			#  2) any opening brace { on end of the line
15804d001e4dSAndy Whitcroft			#  3) any do (...) {
15814d001e4dSAndy Whitcroft			my $continuation = 0;
15824d001e4dSAndy Whitcroft			my $check = 0;
15834d001e4dSAndy Whitcroft			$s =~ s/^.*\bdo\b//;
15844d001e4dSAndy Whitcroft			$s =~ s/^\s*{//;
15854d001e4dSAndy Whitcroft			if ($s =~ s/^\s*\\//) {
15864d001e4dSAndy Whitcroft				$continuation = 1;
15874d001e4dSAndy Whitcroft			}
15889bd49efeSAndy Whitcroft			if ($s =~ s/^\s*?\n//) {
15894d001e4dSAndy Whitcroft				$check = 1;
15904d001e4dSAndy Whitcroft				$cond_lines++;
15914d001e4dSAndy Whitcroft			}
15924d001e4dSAndy Whitcroft
15934d001e4dSAndy Whitcroft			# Also ignore a loop construct at the end of a
15944d001e4dSAndy Whitcroft			# preprocessor statement.
15954d001e4dSAndy Whitcroft			if (($prevline =~ /^.\s*#\s*define\s/ ||
15964d001e4dSAndy Whitcroft			    $prevline =~ /\\\s*$/) && $continuation == 0) {
15974d001e4dSAndy Whitcroft				$check = 0;
15984d001e4dSAndy Whitcroft			}
15994d001e4dSAndy Whitcroft
16009bd49efeSAndy Whitcroft			my $cond_ptr = -1;
1601740504c6SAndy Whitcroft			$continuation = 0;
16029bd49efeSAndy Whitcroft			while ($cond_ptr != $cond_lines) {
16039bd49efeSAndy Whitcroft				$cond_ptr = $cond_lines;
16044d001e4dSAndy Whitcroft
1605f16fa28fSAndy Whitcroft				# If we see an #else/#elif then the code
1606f16fa28fSAndy Whitcroft				# is not linear.
1607f16fa28fSAndy Whitcroft				if ($s =~ /^\s*\#\s*(?:else|elif)/) {
1608f16fa28fSAndy Whitcroft					$check = 0;
1609f16fa28fSAndy Whitcroft				}
1610f16fa28fSAndy Whitcroft
16119bd49efeSAndy Whitcroft				# Ignore:
16129bd49efeSAndy Whitcroft				#  1) blank lines, they should be at 0,
16139bd49efeSAndy Whitcroft				#  2) preprocessor lines, and
16149bd49efeSAndy Whitcroft				#  3) labels.
1615740504c6SAndy Whitcroft				if ($continuation ||
1616740504c6SAndy Whitcroft				    $s =~ /^\s*?\n/ ||
16179bd49efeSAndy Whitcroft				    $s =~ /^\s*#\s*?/ ||
16189bd49efeSAndy Whitcroft				    $s =~ /^\s*$Ident\s*:/) {
1619740504c6SAndy Whitcroft					$continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
162030dad6ebSAndy Whitcroft					if ($s =~ s/^.*?\n//) {
16219bd49efeSAndy Whitcroft						$cond_lines++;
16229bd49efeSAndy Whitcroft					}
16234d001e4dSAndy Whitcroft				}
162430dad6ebSAndy Whitcroft			}
16254d001e4dSAndy Whitcroft
16264d001e4dSAndy Whitcroft			my (undef, $sindent) = line_stats("+" . $s);
16274d001e4dSAndy Whitcroft			my $stat_real = raw_line($linenr, $cond_lines);
16284d001e4dSAndy Whitcroft
16294d001e4dSAndy Whitcroft			# Check if either of these lines are modified, else
16304d001e4dSAndy Whitcroft			# this is not this patch's fault.
16314d001e4dSAndy Whitcroft			if (!defined($stat_real) ||
16324d001e4dSAndy Whitcroft			    $stat !~ /^\+/ && $stat_real !~ /^\+/) {
16334d001e4dSAndy Whitcroft				$check = 0;
16344d001e4dSAndy Whitcroft			}
16354d001e4dSAndy Whitcroft			if (defined($stat_real) && $cond_lines > 1) {
16364d001e4dSAndy Whitcroft				$stat_real = "[...]\n$stat_real";
16374d001e4dSAndy Whitcroft			}
16384d001e4dSAndy Whitcroft
16399bd49efeSAndy 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";
16404d001e4dSAndy Whitcroft
16414d001e4dSAndy Whitcroft			if ($check && (($sindent % 8) != 0 ||
16424d001e4dSAndy Whitcroft			    ($sindent <= $indent && $s ne ''))) {
16434d001e4dSAndy Whitcroft				WARN("suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
16444d001e4dSAndy Whitcroft			}
16454d001e4dSAndy Whitcroft		}
16464d001e4dSAndy Whitcroft
16476c72ffaaSAndy Whitcroft		# Track the 'values' across context and added lines.
16486c72ffaaSAndy Whitcroft		my $opline = $line; $opline =~ s/^./ /;
16491f65f947SAndy Whitcroft		my ($curr_values, $curr_vars) =
16501f65f947SAndy Whitcroft				annotate_values($opline . "\n", $prev_values);
16516c72ffaaSAndy Whitcroft		$curr_values = $prev_values . $curr_values;
1652c2fdda0dSAndy Whitcroft		if ($dbg_values) {
1653c2fdda0dSAndy Whitcroft			my $outline = $opline; $outline =~ s/\t/ /g;
1654cf655043SAndy Whitcroft			print "$linenr > .$outline\n";
1655cf655043SAndy Whitcroft			print "$linenr > $curr_values\n";
16561f65f947SAndy Whitcroft			print "$linenr >  $curr_vars\n";
1657c2fdda0dSAndy Whitcroft		}
16586c72ffaaSAndy Whitcroft		$prev_values = substr($curr_values, -1);
16596c72ffaaSAndy Whitcroft
166000df344fSAndy Whitcroft#ignore lines not being added
166100df344fSAndy Whitcroft		if ($line=~/^[^\+]/) {next;}
166200df344fSAndy Whitcroft
1663653d4876SAndy Whitcroft# TEST: allow direct testing of the type matcher.
16647429c690SAndy Whitcroft		if ($dbg_type) {
16657429c690SAndy Whitcroft			if ($line =~ /^.\s*$Declare\s*$/) {
16667429c690SAndy Whitcroft				ERROR("TEST: is type\n" . $herecurr);
16677429c690SAndy Whitcroft			} elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
16687429c690SAndy Whitcroft				ERROR("TEST: is not type ($1 is)\n". $herecurr);
16697429c690SAndy Whitcroft			}
1670653d4876SAndy Whitcroft			next;
1671653d4876SAndy Whitcroft		}
1672a1ef277eSAndy Whitcroft# TEST: allow direct testing of the attribute matcher.
1673a1ef277eSAndy Whitcroft		if ($dbg_attr) {
16749360b0e5SAndy Whitcroft			if ($line =~ /^.\s*$Modifier\s*$/) {
1675a1ef277eSAndy Whitcroft				ERROR("TEST: is attr\n" . $herecurr);
16769360b0e5SAndy Whitcroft			} elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
1677a1ef277eSAndy Whitcroft				ERROR("TEST: is not attr ($1 is)\n". $herecurr);
1678a1ef277eSAndy Whitcroft			}
1679a1ef277eSAndy Whitcroft			next;
1680a1ef277eSAndy Whitcroft		}
1681653d4876SAndy Whitcroft
1682f0a594c1SAndy Whitcroft# check for initialisation to aggregates open brace on the next line
168399423c20SAndy Whitcroft		if ($line =~ /^.\s*{/ &&
168499423c20SAndy Whitcroft		    $prevline =~ /(?:^|[^=])=\s*$/) {
1685773647a0SAndy Whitcroft			ERROR("that open brace { should be on the previous line\n" . $hereprev);
1686f0a594c1SAndy Whitcroft		}
1687f0a594c1SAndy Whitcroft
168800df344fSAndy Whitcroft#
168900df344fSAndy Whitcroft# Checks which are anchored on the added line.
169000df344fSAndy Whitcroft#
169100df344fSAndy Whitcroft
1692653d4876SAndy Whitcroft# check for malformed paths in #include statements (uses RAW line)
1693c45dcabdSAndy Whitcroft		if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
1694653d4876SAndy Whitcroft			my $path = $1;
1695653d4876SAndy Whitcroft			if ($path =~ m{//}) {
1696de7d4f0eSAndy Whitcroft				ERROR("malformed #include filename\n" .
1697de7d4f0eSAndy Whitcroft					$herecurr);
1698653d4876SAndy Whitcroft			}
1699653d4876SAndy Whitcroft		}
1700653d4876SAndy Whitcroft
170100df344fSAndy Whitcroft# no C99 // comments
170200df344fSAndy Whitcroft		if ($line =~ m{//}) {
1703de7d4f0eSAndy Whitcroft			ERROR("do not use C99 // comments\n" . $herecurr);
170400df344fSAndy Whitcroft		}
170500df344fSAndy Whitcroft		# Remove C99 comments.
17060a920b5bSAndy Whitcroft		$line =~ s@//.*@@;
17076c72ffaaSAndy Whitcroft		$opline =~ s@//.*@@;
17080a920b5bSAndy Whitcroft
17092b474a1aSAndy Whitcroft# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
17102b474a1aSAndy Whitcroft# the whole statement.
17112b474a1aSAndy Whitcroft#print "APW <$lines[$realline_next - 1]>\n";
17122b474a1aSAndy Whitcroft		if (defined $realline_next &&
17132b474a1aSAndy Whitcroft		    exists $lines[$realline_next - 1] &&
17142b474a1aSAndy Whitcroft		    !defined $suppress_export{$realline_next} &&
17152b474a1aSAndy Whitcroft		    ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
17162b474a1aSAndy Whitcroft		     $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
1717653d4876SAndy Whitcroft			my $name = $1;
17182b474a1aSAndy Whitcroft			if ($stat !~ /(?:
17192b474a1aSAndy Whitcroft				\n.}\s*$|
172048012058SAndy Whitcroft				^.DEFINE_$Ident\(\Q$name\E\)|
172148012058SAndy Whitcroft				^.DECLARE_$Ident\(\Q$name\E\)|
172248012058SAndy Whitcroft				^.LIST_HEAD\(\Q$name\E\)|
17232b474a1aSAndy Whitcroft				^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
17242b474a1aSAndy Whitcroft				\b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
172548012058SAndy Whitcroft			    )/x) {
17262b474a1aSAndy Whitcroft#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
17272b474a1aSAndy Whitcroft				$suppress_export{$realline_next} = 2;
17282b474a1aSAndy Whitcroft			} else {
17292b474a1aSAndy Whitcroft				$suppress_export{$realline_next} = 1;
17300a920b5bSAndy Whitcroft			}
17310a920b5bSAndy Whitcroft		}
17322b474a1aSAndy Whitcroft		if (!defined $suppress_export{$linenr} &&
17332b474a1aSAndy Whitcroft		    $prevline =~ /^.\s*$/ &&
17342b474a1aSAndy Whitcroft		    ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
17352b474a1aSAndy Whitcroft		     $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
17362b474a1aSAndy Whitcroft#print "FOO B <$lines[$linenr - 1]>\n";
17372b474a1aSAndy Whitcroft			$suppress_export{$linenr} = 2;
17382b474a1aSAndy Whitcroft		}
17392b474a1aSAndy Whitcroft		if (defined $suppress_export{$linenr} &&
17402b474a1aSAndy Whitcroft		    $suppress_export{$linenr} == 2) {
17412b474a1aSAndy Whitcroft			WARN("EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
17422b474a1aSAndy Whitcroft		}
17430a920b5bSAndy Whitcroft
1744f0a594c1SAndy Whitcroft# check for external initialisers.
1745c45dcabdSAndy Whitcroft		if ($line =~ /^.$Type\s*$Ident\s*(?:\s+$Modifier)*\s*=\s*(0|NULL|false)\s*;/) {
1746f0a594c1SAndy Whitcroft			ERROR("do not initialise externals to 0 or NULL\n" .
1747f0a594c1SAndy Whitcroft				$herecurr);
1748f0a594c1SAndy Whitcroft		}
17490a920b5bSAndy Whitcroft# check for static initialisers.
17502d1bafd7SAndy Whitcroft		if ($line =~ /\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
1751de7d4f0eSAndy Whitcroft			ERROR("do not initialise statics to 0 or NULL\n" .
1752de7d4f0eSAndy Whitcroft				$herecurr);
17530a920b5bSAndy Whitcroft		}
17540a920b5bSAndy Whitcroft
1755653d4876SAndy Whitcroft# check for new typedefs, only function parameters and sparse annotations
1756653d4876SAndy Whitcroft# make sense.
1757653d4876SAndy Whitcroft		if ($line =~ /\btypedef\s/ &&
17588054576dSAndy Whitcroft		    $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
1759c45dcabdSAndy Whitcroft		    $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
17608ed22cadSAndy Whitcroft		    $line !~ /\b$typeTypedefs\b/ &&
1761653d4876SAndy Whitcroft		    $line !~ /\b__bitwise(?:__|)\b/) {
1762de7d4f0eSAndy Whitcroft			WARN("do not add new typedefs\n" . $herecurr);
17630a920b5bSAndy Whitcroft		}
17640a920b5bSAndy Whitcroft
17650a920b5bSAndy Whitcroft# * goes on variable not on type
176665863862SAndy Whitcroft		# (char*[ const])
176700ef4eceSAndy Whitcroft		if ($line =~ m{\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\)}) {
176865863862SAndy Whitcroft			my ($from, $to) = ($1, $1);
1769d8aaf121SAndy Whitcroft
177065863862SAndy Whitcroft			# Should start with a space.
177165863862SAndy Whitcroft			$to =~ s/^(\S)/ $1/;
177265863862SAndy Whitcroft			# Should not end with a space.
177365863862SAndy Whitcroft			$to =~ s/\s+$//;
177465863862SAndy Whitcroft			# '*'s should not have spaces between.
1775f9a0b3d1SAndy Whitcroft			while ($to =~ s/\*\s+\*/\*\*/) {
177665863862SAndy Whitcroft			}
1777d8aaf121SAndy Whitcroft
177865863862SAndy Whitcroft			#print "from<$from> to<$to>\n";
177965863862SAndy Whitcroft			if ($from ne $to) {
178065863862SAndy Whitcroft				ERROR("\"(foo$from)\" should be \"(foo$to)\"\n" .  $herecurr);
178165863862SAndy Whitcroft			}
178200ef4eceSAndy Whitcroft		} elsif ($line =~ m{\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident)}) {
178365863862SAndy Whitcroft			my ($from, $to, $ident) = ($1, $1, $2);
1784d8aaf121SAndy Whitcroft
178565863862SAndy Whitcroft			# Should start with a space.
178665863862SAndy Whitcroft			$to =~ s/^(\S)/ $1/;
178765863862SAndy Whitcroft			# Should not end with a space.
178865863862SAndy Whitcroft			$to =~ s/\s+$//;
178965863862SAndy Whitcroft			# '*'s should not have spaces between.
1790f9a0b3d1SAndy Whitcroft			while ($to =~ s/\*\s+\*/\*\*/) {
179165863862SAndy Whitcroft			}
179265863862SAndy Whitcroft			# Modifiers should have spaces.
179365863862SAndy Whitcroft			$to =~ s/(\b$Modifier$)/$1 /;
179465863862SAndy Whitcroft
1795667026e7SAndy Whitcroft			#print "from<$from> to<$to> ident<$ident>\n";
1796667026e7SAndy Whitcroft			if ($from ne $to && $ident !~ /^$Modifier$/) {
179765863862SAndy Whitcroft				ERROR("\"foo${from}bar\" should be \"foo${to}bar\"\n" .  $herecurr);
179865863862SAndy Whitcroft			}
17990a920b5bSAndy Whitcroft		}
18000a920b5bSAndy Whitcroft
18010a920b5bSAndy Whitcroft# # no BUG() or BUG_ON()
18020a920b5bSAndy Whitcroft# 		if ($line =~ /\b(BUG|BUG_ON)\b/) {
18030a920b5bSAndy Whitcroft# 			print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
18040a920b5bSAndy Whitcroft# 			print "$herecurr";
18050a920b5bSAndy Whitcroft# 			$clean = 0;
18060a920b5bSAndy Whitcroft# 		}
18070a920b5bSAndy Whitcroft
18088905a67cSAndy Whitcroft		if ($line =~ /\bLINUX_VERSION_CODE\b/) {
1809c2fdda0dSAndy Whitcroft			WARN("LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
18108905a67cSAndy Whitcroft		}
18118905a67cSAndy Whitcroft
181200df344fSAndy Whitcroft# printk should use KERN_* levels.  Note that follow on printk's on the
181300df344fSAndy Whitcroft# same line do not need a level, so we use the current block context
181400df344fSAndy Whitcroft# to try and find and validate the current printk.  In summary the current
181500df344fSAndy Whitcroft# printk includes all preceeding printk's which have no newline on the end.
181600df344fSAndy Whitcroft# we assume the first bad printk is the one to report.
1817f0a594c1SAndy Whitcroft		if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
181800df344fSAndy Whitcroft			my $ok = 0;
181900df344fSAndy Whitcroft			for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
182000df344fSAndy Whitcroft				#print "CHECK<$lines[$ln - 1]\n";
182100df344fSAndy Whitcroft				# we have a preceeding printk if it ends
182200df344fSAndy Whitcroft				# with "\n" ignore it, else it is to blame
182300df344fSAndy Whitcroft				if ($lines[$ln - 1] =~ m{\bprintk\(}) {
182400df344fSAndy Whitcroft					if ($rawlines[$ln - 1] !~ m{\\n"}) {
182500df344fSAndy Whitcroft						$ok = 1;
182600df344fSAndy Whitcroft					}
182700df344fSAndy Whitcroft					last;
182800df344fSAndy Whitcroft				}
182900df344fSAndy Whitcroft			}
183000df344fSAndy Whitcroft			if ($ok == 0) {
1831de7d4f0eSAndy Whitcroft				WARN("printk() should include KERN_ facility level\n" . $herecurr);
18320a920b5bSAndy Whitcroft			}
183300df344fSAndy Whitcroft		}
18340a920b5bSAndy Whitcroft
1835653d4876SAndy Whitcroft# function brace can't be on same line, except for #defines of do while,
1836653d4876SAndy Whitcroft# or if closed on same line
1837c45dcabdSAndy Whitcroft		if (($line=~/$Type\s*$Ident\(.*\).*\s{/) and
1838c45dcabdSAndy Whitcroft		    !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) {
1839de7d4f0eSAndy Whitcroft			ERROR("open brace '{' following function declarations go on the next line\n" . $herecurr);
18400a920b5bSAndy Whitcroft		}
1841653d4876SAndy Whitcroft
18428905a67cSAndy Whitcroft# open braces for enum, union and struct go on the same line.
18438905a67cSAndy Whitcroft		if ($line =~ /^.\s*{/ &&
18448905a67cSAndy Whitcroft		    $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
18458905a67cSAndy Whitcroft			ERROR("open brace '{' following $1 go on the same line\n" . $hereprev);
18468905a67cSAndy Whitcroft		}
18478905a67cSAndy Whitcroft
18488d31cfceSAndy Whitcroft# check for spacing round square brackets; allowed:
18498d31cfceSAndy Whitcroft#  1. with a type on the left -- int [] a;
1850fe2a7dbcSAndy Whitcroft#  2. at the beginning of a line for slice initialisers -- [0...10] = 5,
1851fe2a7dbcSAndy Whitcroft#  3. inside a curly brace -- = { [0...10] = 5 }
18528d31cfceSAndy Whitcroft		while ($line =~ /(.*?\s)\[/g) {
18538d31cfceSAndy Whitcroft			my ($where, $prefix) = ($-[1], $1);
18548d31cfceSAndy Whitcroft			if ($prefix !~ /$Type\s+$/ &&
1855fe2a7dbcSAndy Whitcroft			    ($where != 0 || $prefix !~ /^.\s+$/) &&
1856fe2a7dbcSAndy Whitcroft			    $prefix !~ /{\s+$/) {
18578d31cfceSAndy Whitcroft				ERROR("space prohibited before open square bracket '['\n" . $herecurr);
18588d31cfceSAndy Whitcroft			}
18598d31cfceSAndy Whitcroft		}
18608d31cfceSAndy Whitcroft
1861f0a594c1SAndy Whitcroft# check for spaces between functions and their parentheses.
18626c72ffaaSAndy Whitcroft		while ($line =~ /($Ident)\s+\(/g) {
1863c2fdda0dSAndy Whitcroft			my $name = $1;
1864773647a0SAndy Whitcroft			my $ctx_before = substr($line, 0, $-[1]);
1865773647a0SAndy Whitcroft			my $ctx = "$ctx_before$name";
1866c2fdda0dSAndy Whitcroft
1867c2fdda0dSAndy Whitcroft			# Ignore those directives where spaces _are_ permitted.
1868773647a0SAndy Whitcroft			if ($name =~ /^(?:
1869773647a0SAndy Whitcroft				if|for|while|switch|return|case|
1870773647a0SAndy Whitcroft				volatile|__volatile__|
1871773647a0SAndy Whitcroft				__attribute__|format|__extension__|
1872773647a0SAndy Whitcroft				asm|__asm__)$/x)
1873773647a0SAndy Whitcroft			{
1874c2fdda0dSAndy Whitcroft
1875c2fdda0dSAndy Whitcroft			# cpp #define statements have non-optional spaces, ie
1876c2fdda0dSAndy Whitcroft			# if there is a space between the name and the open
1877c2fdda0dSAndy Whitcroft			# parenthesis it is simply not a parameter group.
1878c45dcabdSAndy Whitcroft			} elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
1879773647a0SAndy Whitcroft
1880773647a0SAndy Whitcroft			# cpp #elif statement condition may start with a (
1881c45dcabdSAndy Whitcroft			} elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
1882c2fdda0dSAndy Whitcroft
1883c2fdda0dSAndy Whitcroft			# If this whole things ends with a type its most
1884c2fdda0dSAndy Whitcroft			# likely a typedef for a function.
1885773647a0SAndy Whitcroft			} elsif ($ctx =~ /$Type$/) {
1886c2fdda0dSAndy Whitcroft
1887c2fdda0dSAndy Whitcroft			} else {
1888773647a0SAndy Whitcroft				WARN("space prohibited between function name and open parenthesis '('\n" . $herecurr);
1889f0a594c1SAndy Whitcroft			}
18906c72ffaaSAndy Whitcroft		}
1891653d4876SAndy Whitcroft# Check operator spacing.
18920a920b5bSAndy Whitcroft		if (!($line=~/\#\s*include/)) {
18939c0ca6f9SAndy Whitcroft			my $ops = qr{
18949c0ca6f9SAndy Whitcroft				<<=|>>=|<=|>=|==|!=|
18959c0ca6f9SAndy Whitcroft				\+=|-=|\*=|\/=|%=|\^=|\|=|&=|
18969c0ca6f9SAndy Whitcroft				=>|->|<<|>>|<|>|=|!|~|
18971f65f947SAndy Whitcroft				&&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
18981f65f947SAndy Whitcroft				\?|:
18999c0ca6f9SAndy Whitcroft			}x;
1900cf655043SAndy Whitcroft			my @elements = split(/($ops|;)/, $opline);
190100df344fSAndy Whitcroft			my $off = 0;
19026c72ffaaSAndy Whitcroft
19036c72ffaaSAndy Whitcroft			my $blank = copy_spacing($opline);
19046c72ffaaSAndy Whitcroft
19050a920b5bSAndy Whitcroft			for (my $n = 0; $n < $#elements; $n += 2) {
19064a0df2efSAndy Whitcroft				$off += length($elements[$n]);
19074a0df2efSAndy Whitcroft
1908773647a0SAndy Whitcroft				# Pick up the preceeding and succeeding characters.
1909773647a0SAndy Whitcroft				my $ca = substr($opline, 0, $off);
1910773647a0SAndy Whitcroft				my $cc = '';
1911773647a0SAndy Whitcroft				if (length($opline) >= ($off + length($elements[$n + 1]))) {
1912773647a0SAndy Whitcroft					$cc = substr($opline, $off + length($elements[$n + 1]));
1913773647a0SAndy Whitcroft				}
1914773647a0SAndy Whitcroft				my $cb = "$ca$;$cc";
1915773647a0SAndy Whitcroft
19164a0df2efSAndy Whitcroft				my $a = '';
19174a0df2efSAndy Whitcroft				$a = 'V' if ($elements[$n] ne '');
19184a0df2efSAndy Whitcroft				$a = 'W' if ($elements[$n] =~ /\s$/);
1919cf655043SAndy Whitcroft				$a = 'C' if ($elements[$n] =~ /$;$/);
19204a0df2efSAndy Whitcroft				$a = 'B' if ($elements[$n] =~ /(\[|\()$/);
19214a0df2efSAndy Whitcroft				$a = 'O' if ($elements[$n] eq '');
1922773647a0SAndy Whitcroft				$a = 'E' if ($ca =~ /^\s*$/);
19234a0df2efSAndy Whitcroft
19240a920b5bSAndy Whitcroft				my $op = $elements[$n + 1];
19254a0df2efSAndy Whitcroft
19264a0df2efSAndy Whitcroft				my $c = '';
19270a920b5bSAndy Whitcroft				if (defined $elements[$n + 2]) {
19284a0df2efSAndy Whitcroft					$c = 'V' if ($elements[$n + 2] ne '');
19294a0df2efSAndy Whitcroft					$c = 'W' if ($elements[$n + 2] =~ /^\s/);
1930cf655043SAndy Whitcroft					$c = 'C' if ($elements[$n + 2] =~ /^$;/);
19314a0df2efSAndy Whitcroft					$c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
19324a0df2efSAndy Whitcroft					$c = 'O' if ($elements[$n + 2] eq '');
19338b1b3378SAndy Whitcroft					$c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
19344a0df2efSAndy Whitcroft				} else {
19354a0df2efSAndy Whitcroft					$c = 'E';
19360a920b5bSAndy Whitcroft				}
19370a920b5bSAndy Whitcroft
19384a0df2efSAndy Whitcroft				my $ctx = "${a}x${c}";
19394a0df2efSAndy Whitcroft
19404a0df2efSAndy Whitcroft				my $at = "(ctx:$ctx)";
19414a0df2efSAndy Whitcroft
19426c72ffaaSAndy Whitcroft				my $ptr = substr($blank, 0, $off) . "^";
1943de7d4f0eSAndy Whitcroft				my $hereptr = "$hereline$ptr\n";
19440a920b5bSAndy Whitcroft
194574048ed8SAndy Whitcroft				# Pull out the value of this operator.
19466c72ffaaSAndy Whitcroft				my $op_type = substr($curr_values, $off + 1, 1);
19470a920b5bSAndy Whitcroft
19481f65f947SAndy Whitcroft				# Get the full operator variant.
19491f65f947SAndy Whitcroft				my $opv = $op . substr($curr_vars, $off, 1);
19501f65f947SAndy Whitcroft
195113214adfSAndy Whitcroft				# Ignore operators passed as parameters.
195213214adfSAndy Whitcroft				if ($op_type ne 'V' &&
195313214adfSAndy Whitcroft				    $ca =~ /\s$/ && $cc =~ /^\s*,/) {
195413214adfSAndy Whitcroft
1955cf655043SAndy Whitcroft#				# Ignore comments
1956cf655043SAndy Whitcroft#				} elsif ($op =~ /^$;+$/) {
195713214adfSAndy Whitcroft
1958d8aaf121SAndy Whitcroft				# ; should have either the end of line or a space or \ after it
195913214adfSAndy Whitcroft				} elsif ($op eq ';') {
1960cf655043SAndy Whitcroft					if ($ctx !~ /.x[WEBC]/ &&
1961cf655043SAndy Whitcroft					    $cc !~ /^\\/ && $cc !~ /^;/) {
1962773647a0SAndy Whitcroft						ERROR("space required after that '$op' $at\n" . $hereptr);
1963d8aaf121SAndy Whitcroft					}
1964d8aaf121SAndy Whitcroft
1965d8aaf121SAndy Whitcroft				# // is a comment
1966d8aaf121SAndy Whitcroft				} elsif ($op eq '//') {
19670a920b5bSAndy Whitcroft
19681f65f947SAndy Whitcroft				# No spaces for:
19691f65f947SAndy Whitcroft				#   ->
19701f65f947SAndy Whitcroft				#   :   when part of a bitfield
19711f65f947SAndy Whitcroft				} elsif ($op eq '->' || $opv eq ':B') {
19724a0df2efSAndy Whitcroft					if ($ctx =~ /Wx.|.xW/) {
1973773647a0SAndy Whitcroft						ERROR("spaces prohibited around that '$op' $at\n" . $hereptr);
19740a920b5bSAndy Whitcroft					}
19750a920b5bSAndy Whitcroft
19760a920b5bSAndy Whitcroft				# , must have a space on the right.
19770a920b5bSAndy Whitcroft				} elsif ($op eq ',') {
1978cf655043SAndy Whitcroft					if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
1979773647a0SAndy Whitcroft						ERROR("space required after that '$op' $at\n" . $hereptr);
19800a920b5bSAndy Whitcroft					}
19810a920b5bSAndy Whitcroft
19829c0ca6f9SAndy Whitcroft				# '*' as part of a type definition -- reported already.
198374048ed8SAndy Whitcroft				} elsif ($opv eq '*_') {
19849c0ca6f9SAndy Whitcroft					#warn "'*' is part of type\n";
19859c0ca6f9SAndy Whitcroft
19869c0ca6f9SAndy Whitcroft				# unary operators should have a space before and
19879c0ca6f9SAndy Whitcroft				# none after.  May be left adjacent to another
19889c0ca6f9SAndy Whitcroft				# unary operator, or a cast
19899c0ca6f9SAndy Whitcroft				} elsif ($op eq '!' || $op eq '~' ||
199074048ed8SAndy Whitcroft					 $opv eq '*U' || $opv eq '-U' ||
19910d413866SAndy Whitcroft					 $opv eq '&U' || $opv eq '&&U') {
1992cf655043SAndy Whitcroft					if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
1993773647a0SAndy Whitcroft						ERROR("space required before that '$op' $at\n" . $hereptr);
19940a920b5bSAndy Whitcroft					}
1995a3340b35SAndy Whitcroft					if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
1996171ae1a4SAndy Whitcroft						# A unary '*' may be const
1997171ae1a4SAndy Whitcroft
1998171ae1a4SAndy Whitcroft					} elsif ($ctx =~ /.xW/) {
1999fb9e9096SAndy Whitcroft						ERROR("space prohibited after that '$op' $at\n" . $hereptr);
20000a920b5bSAndy Whitcroft					}
20010a920b5bSAndy Whitcroft
20020a920b5bSAndy Whitcroft				# unary ++ and unary -- are allowed no space on one side.
20030a920b5bSAndy Whitcroft				} elsif ($op eq '++' or $op eq '--') {
2004773647a0SAndy Whitcroft					if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
2005773647a0SAndy Whitcroft						ERROR("space required one side of that '$op' $at\n" . $hereptr);
20060a920b5bSAndy Whitcroft					}
2007773647a0SAndy Whitcroft					if ($ctx =~ /Wx[BE]/ ||
2008773647a0SAndy Whitcroft					    ($ctx =~ /Wx./ && $cc =~ /^;/)) {
2009773647a0SAndy Whitcroft						ERROR("space prohibited before that '$op' $at\n" . $hereptr);
2010653d4876SAndy Whitcroft					}
2011773647a0SAndy Whitcroft					if ($ctx =~ /ExW/) {
2012773647a0SAndy Whitcroft						ERROR("space prohibited after that '$op' $at\n" . $hereptr);
2013773647a0SAndy Whitcroft					}
2014773647a0SAndy Whitcroft
20150a920b5bSAndy Whitcroft
20160a920b5bSAndy Whitcroft				# << and >> may either have or not have spaces both sides
20179c0ca6f9SAndy Whitcroft				} elsif ($op eq '<<' or $op eq '>>' or
20189c0ca6f9SAndy Whitcroft					 $op eq '&' or $op eq '^' or $op eq '|' or
20199c0ca6f9SAndy Whitcroft					 $op eq '+' or $op eq '-' or
2020c2fdda0dSAndy Whitcroft					 $op eq '*' or $op eq '/' or
2021c2fdda0dSAndy Whitcroft					 $op eq '%')
20220a920b5bSAndy Whitcroft				{
2023773647a0SAndy Whitcroft					if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
2024de7d4f0eSAndy Whitcroft						ERROR("need consistent spacing around '$op' $at\n" .
2025de7d4f0eSAndy Whitcroft							$hereptr);
20260a920b5bSAndy Whitcroft					}
20270a920b5bSAndy Whitcroft
20281f65f947SAndy Whitcroft				# A colon needs no spaces before when it is
20291f65f947SAndy Whitcroft				# terminating a case value or a label.
20301f65f947SAndy Whitcroft				} elsif ($opv eq ':C' || $opv eq ':L') {
20311f65f947SAndy Whitcroft					if ($ctx =~ /Wx./) {
20321f65f947SAndy Whitcroft						ERROR("space prohibited before that '$op' $at\n" . $hereptr);
20331f65f947SAndy Whitcroft					}
20341f65f947SAndy Whitcroft
20350a920b5bSAndy Whitcroft				# All the others need spaces both sides.
2036cf655043SAndy Whitcroft				} elsif ($ctx !~ /[EWC]x[CWE]/) {
20371f65f947SAndy Whitcroft					my $ok = 0;
20381f65f947SAndy Whitcroft
203922f2a2efSAndy Whitcroft					# Ignore email addresses <foo@bar>
20401f65f947SAndy Whitcroft					if (($op eq '<' &&
20411f65f947SAndy Whitcroft					     $cc =~ /^\S+\@\S+>/) ||
20421f65f947SAndy Whitcroft					    ($op eq '>' &&
20431f65f947SAndy Whitcroft					     $ca =~ /<\S+\@\S+$/))
20441f65f947SAndy Whitcroft					{
20451f65f947SAndy Whitcroft					    	$ok = 1;
20461f65f947SAndy Whitcroft					}
20471f65f947SAndy Whitcroft
20481f65f947SAndy Whitcroft					# Ignore ?:
20491f65f947SAndy Whitcroft					if (($opv eq ':O' && $ca =~ /\?$/) ||
20501f65f947SAndy Whitcroft					    ($op eq '?' && $cc =~ /^:/)) {
20511f65f947SAndy Whitcroft					    	$ok = 1;
20521f65f947SAndy Whitcroft					}
20531f65f947SAndy Whitcroft
20541f65f947SAndy Whitcroft					if ($ok == 0) {
2055773647a0SAndy Whitcroft						ERROR("spaces required around that '$op' $at\n" . $hereptr);
20560a920b5bSAndy Whitcroft					}
205722f2a2efSAndy Whitcroft				}
20584a0df2efSAndy Whitcroft				$off += length($elements[$n + 1]);
20590a920b5bSAndy Whitcroft			}
20600a920b5bSAndy Whitcroft		}
20610a920b5bSAndy Whitcroft
2062f0a594c1SAndy Whitcroft# check for multiple assignments
2063f0a594c1SAndy Whitcroft		if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
20646c72ffaaSAndy Whitcroft			CHK("multiple assignments should be avoided\n" . $herecurr);
2065f0a594c1SAndy Whitcroft		}
2066f0a594c1SAndy Whitcroft
206722f2a2efSAndy Whitcroft## # check for multiple declarations, allowing for a function declaration
206822f2a2efSAndy Whitcroft## # continuation.
206922f2a2efSAndy Whitcroft## 		if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
207022f2a2efSAndy Whitcroft## 		    $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
207122f2a2efSAndy Whitcroft##
207222f2a2efSAndy Whitcroft## 			# Remove any bracketed sections to ensure we do not
207322f2a2efSAndy Whitcroft## 			# falsly report the parameters of functions.
207422f2a2efSAndy Whitcroft## 			my $ln = $line;
207522f2a2efSAndy Whitcroft## 			while ($ln =~ s/\([^\(\)]*\)//g) {
207622f2a2efSAndy Whitcroft## 			}
207722f2a2efSAndy Whitcroft## 			if ($ln =~ /,/) {
207822f2a2efSAndy Whitcroft## 				WARN("declaring multiple variables together should be avoided\n" . $herecurr);
207922f2a2efSAndy Whitcroft## 			}
208022f2a2efSAndy Whitcroft## 		}
2081f0a594c1SAndy Whitcroft
20820a920b5bSAndy Whitcroft#need space before brace following if, while, etc
208322f2a2efSAndy Whitcroft		if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
208422f2a2efSAndy Whitcroft		    $line =~ /do{/) {
2085773647a0SAndy Whitcroft			ERROR("space required before the open brace '{'\n" . $herecurr);
2086de7d4f0eSAndy Whitcroft		}
2087de7d4f0eSAndy Whitcroft
2088de7d4f0eSAndy Whitcroft# closing brace should have a space following it when it has anything
2089de7d4f0eSAndy Whitcroft# on the line
2090de7d4f0eSAndy Whitcroft		if ($line =~ /}(?!(?:,|;|\)))\S/) {
2091773647a0SAndy Whitcroft			ERROR("space required after that close brace '}'\n" . $herecurr);
20920a920b5bSAndy Whitcroft		}
20930a920b5bSAndy Whitcroft
209422f2a2efSAndy Whitcroft# check spacing on square brackets
209522f2a2efSAndy Whitcroft		if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
2096773647a0SAndy Whitcroft			ERROR("space prohibited after that open square bracket '['\n" . $herecurr);
209722f2a2efSAndy Whitcroft		}
209822f2a2efSAndy Whitcroft		if ($line =~ /\s\]/) {
2099773647a0SAndy Whitcroft			ERROR("space prohibited before that close square bracket ']'\n" . $herecurr);
210022f2a2efSAndy Whitcroft		}
210122f2a2efSAndy Whitcroft
2102c45dcabdSAndy Whitcroft# check spacing on parentheses
21039c0ca6f9SAndy Whitcroft		if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
21049c0ca6f9SAndy Whitcroft		    $line !~ /for\s*\(\s+;/) {
2105773647a0SAndy Whitcroft			ERROR("space prohibited after that open parenthesis '('\n" . $herecurr);
210622f2a2efSAndy Whitcroft		}
210713214adfSAndy Whitcroft		if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
2108c45dcabdSAndy Whitcroft		    $line !~ /for\s*\(.*;\s+\)/ &&
2109c45dcabdSAndy Whitcroft		    $line !~ /:\s+\)/) {
2110773647a0SAndy Whitcroft			ERROR("space prohibited before that close parenthesis ')'\n" . $herecurr);
211122f2a2efSAndy Whitcroft		}
211222f2a2efSAndy Whitcroft
21130a920b5bSAndy Whitcroft#goto labels aren't indented, allow a single space however
21144a0df2efSAndy Whitcroft		if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
21150a920b5bSAndy Whitcroft		   !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
2116de7d4f0eSAndy Whitcroft			WARN("labels should not be indented\n" . $herecurr);
21170a920b5bSAndy Whitcroft		}
21180a920b5bSAndy Whitcroft
2119c45dcabdSAndy Whitcroft# Return is not a function.
2120c45dcabdSAndy Whitcroft		if (defined($stat) && $stat =~ /^.\s*return(\s*)(\(.*);/s) {
2121c45dcabdSAndy Whitcroft			my $spacing = $1;
2122c45dcabdSAndy Whitcroft			my $value = $2;
2123c45dcabdSAndy Whitcroft
212486f9d059SAndy Whitcroft			# Flatten any parentheses
2125fee61c47SAndy Whitcroft			$value =~ s/\)\(/\) \(/g;
212663f17f89SAndy Whitcroft			while ($value =~ s/\[[^\{\}]*\]/1/ ||
212763f17f89SAndy Whitcroft			       $value !~ /(?:$Ident|-?$Constant)\s*
212863f17f89SAndy Whitcroft					     $Compare\s*
212963f17f89SAndy Whitcroft					     (?:$Ident|-?$Constant)/x &&
213063f17f89SAndy Whitcroft			       $value =~ s/\([^\(\)]*\)/1/) {
2131c45dcabdSAndy Whitcroft			}
2132c45dcabdSAndy Whitcroft
2133c45dcabdSAndy Whitcroft			if ($value =~ /^(?:$Ident|-?$Constant)$/) {
2134c45dcabdSAndy Whitcroft				ERROR("return is not a function, parentheses are not required\n" . $herecurr);
2135c45dcabdSAndy Whitcroft
2136c45dcabdSAndy Whitcroft			} elsif ($spacing !~ /\s+/) {
2137c45dcabdSAndy Whitcroft				ERROR("space required before the open parenthesis '('\n" . $herecurr);
2138c45dcabdSAndy Whitcroft			}
2139c45dcabdSAndy Whitcroft		}
2140c45dcabdSAndy Whitcroft
21410a920b5bSAndy Whitcroft# Need a space before open parenthesis after if, while etc
21424a0df2efSAndy Whitcroft		if ($line=~/\b(if|while|for|switch)\(/) {
2143773647a0SAndy Whitcroft			ERROR("space required before the open parenthesis '('\n" . $herecurr);
21440a920b5bSAndy Whitcroft		}
21450a920b5bSAndy Whitcroft
2146f5fe35ddSAndy Whitcroft# Check for illegal assignment in if conditional -- and check for trailing
2147f5fe35ddSAndy Whitcroft# statements after the conditional.
2148170d3a22SAndy Whitcroft		if ($line =~ /do\s*(?!{)/) {
2149170d3a22SAndy Whitcroft			my ($stat_next) = ctx_statement_block($line_nr_next,
2150170d3a22SAndy Whitcroft						$remain_next, $off_next);
2151170d3a22SAndy Whitcroft			$stat_next =~ s/\n./\n /g;
2152170d3a22SAndy Whitcroft			##print "stat<$stat> stat_next<$stat_next>\n";
2153170d3a22SAndy Whitcroft
2154170d3a22SAndy Whitcroft			if ($stat_next =~ /^\s*while\b/) {
2155170d3a22SAndy Whitcroft				# If the statement carries leading newlines,
2156170d3a22SAndy Whitcroft				# then count those as offsets.
2157170d3a22SAndy Whitcroft				my ($whitespace) =
2158170d3a22SAndy Whitcroft					($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
2159170d3a22SAndy Whitcroft				my $offset =
2160170d3a22SAndy Whitcroft					statement_rawlines($whitespace) - 1;
2161170d3a22SAndy Whitcroft
2162170d3a22SAndy Whitcroft				$suppress_whiletrailers{$line_nr_next +
2163170d3a22SAndy Whitcroft								$offset} = 1;
2164170d3a22SAndy Whitcroft			}
2165170d3a22SAndy Whitcroft		}
2166170d3a22SAndy Whitcroft		if (!defined $suppress_whiletrailers{$linenr} &&
2167170d3a22SAndy Whitcroft		    $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
2168171ae1a4SAndy Whitcroft			my ($s, $c) = ($stat, $cond);
21698905a67cSAndy Whitcroft
2170b53c8e10SAndy Whitcroft			if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
2171c2fdda0dSAndy Whitcroft				ERROR("do not use assignment in if condition\n" . $herecurr);
21728905a67cSAndy Whitcroft			}
21738905a67cSAndy Whitcroft
21748905a67cSAndy Whitcroft			# Find out what is on the end of the line after the
21758905a67cSAndy Whitcroft			# conditional.
2176773647a0SAndy Whitcroft			substr($s, 0, length($c), '');
21778905a67cSAndy Whitcroft			$s =~ s/\n.*//g;
217813214adfSAndy Whitcroft			$s =~ s/$;//g; 	# Remove any comments
217953210168SAndy Whitcroft			if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
218053210168SAndy Whitcroft			    $c !~ /}\s*while\s*/)
2181773647a0SAndy Whitcroft			{
2182bb44ad39SAndy Whitcroft				# Find out how long the conditional actually is.
2183bb44ad39SAndy Whitcroft				my @newlines = ($c =~ /\n/gs);
2184bb44ad39SAndy Whitcroft				my $cond_lines = 1 + $#newlines;
2185bb44ad39SAndy Whitcroft
2186bb44ad39SAndy Whitcroft				my $stat_real = raw_line($linenr, $cond_lines);
2187bb44ad39SAndy Whitcroft				if (defined($stat_real) && $cond_lines > 1) {
2188bb44ad39SAndy Whitcroft					$stat_real = "[...]\n$stat_real";
2189bb44ad39SAndy Whitcroft				}
2190bb44ad39SAndy Whitcroft
2191bb44ad39SAndy Whitcroft				ERROR("trailing statements should be on next line\n" . $herecurr . $stat_real);
21928905a67cSAndy Whitcroft			}
21938905a67cSAndy Whitcroft		}
21948905a67cSAndy Whitcroft
219513214adfSAndy Whitcroft# Check for bitwise tests written as boolean
219613214adfSAndy Whitcroft		if ($line =~ /
219713214adfSAndy Whitcroft			(?:
219813214adfSAndy Whitcroft				(?:\[|\(|\&\&|\|\|)
219913214adfSAndy Whitcroft				\s*0[xX][0-9]+\s*
220013214adfSAndy Whitcroft				(?:\&\&|\|\|)
220113214adfSAndy Whitcroft			|
220213214adfSAndy Whitcroft				(?:\&\&|\|\|)
220313214adfSAndy Whitcroft				\s*0[xX][0-9]+\s*
220413214adfSAndy Whitcroft				(?:\&\&|\|\||\)|\])
220513214adfSAndy Whitcroft			)/x)
220613214adfSAndy Whitcroft		{
220713214adfSAndy Whitcroft			WARN("boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
220813214adfSAndy Whitcroft		}
220913214adfSAndy Whitcroft
22108905a67cSAndy Whitcroft# if and else should not have general statements after it
221113214adfSAndy Whitcroft		if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
221213214adfSAndy Whitcroft			my $s = $1;
221313214adfSAndy Whitcroft			$s =~ s/$;//g; 	# Remove any comments
221413214adfSAndy Whitcroft			if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
22158905a67cSAndy Whitcroft				ERROR("trailing statements should be on next line\n" . $herecurr);
22160a920b5bSAndy Whitcroft			}
221713214adfSAndy Whitcroft		}
221839667782SAndy Whitcroft# if should not continue a brace
221939667782SAndy Whitcroft		if ($line =~ /}\s*if\b/) {
222039667782SAndy Whitcroft			ERROR("trailing statements should be on next line\n" .
222139667782SAndy Whitcroft				$herecurr);
222239667782SAndy Whitcroft		}
2223a1080bf8SAndy Whitcroft# case and default should not have general statements after them
2224a1080bf8SAndy Whitcroft		if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
2225a1080bf8SAndy Whitcroft		    $line !~ /\G(?:
22263fef12d6SAndy Whitcroft			(?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
2227a1080bf8SAndy Whitcroft			\s*return\s+
2228a1080bf8SAndy Whitcroft		    )/xg)
2229a1080bf8SAndy Whitcroft		{
2230a1080bf8SAndy Whitcroft			ERROR("trailing statements should be on next line\n" . $herecurr);
2231a1080bf8SAndy Whitcroft		}
22320a920b5bSAndy Whitcroft
22330a920b5bSAndy Whitcroft		# Check for }<nl>else {, these must be at the same
22340a920b5bSAndy Whitcroft		# indent level to be relevant to each other.
22350a920b5bSAndy Whitcroft		if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
22360a920b5bSAndy Whitcroft						$previndent == $indent) {
2237de7d4f0eSAndy Whitcroft			ERROR("else should follow close brace '}'\n" . $hereprev);
22380a920b5bSAndy Whitcroft		}
22390a920b5bSAndy Whitcroft
2240c2fdda0dSAndy Whitcroft		if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and
2241c2fdda0dSAndy Whitcroft						$previndent == $indent) {
2242c2fdda0dSAndy Whitcroft			my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
2243c2fdda0dSAndy Whitcroft
2244c2fdda0dSAndy Whitcroft			# Find out what is on the end of the line after the
2245c2fdda0dSAndy Whitcroft			# conditional.
2246773647a0SAndy Whitcroft			substr($s, 0, length($c), '');
2247c2fdda0dSAndy Whitcroft			$s =~ s/\n.*//g;
2248c2fdda0dSAndy Whitcroft
2249c2fdda0dSAndy Whitcroft			if ($s =~ /^\s*;/) {
2250c2fdda0dSAndy Whitcroft				ERROR("while should follow close brace '}'\n" . $hereprev);
2251c2fdda0dSAndy Whitcroft			}
2252c2fdda0dSAndy Whitcroft		}
2253c2fdda0dSAndy Whitcroft
22540a920b5bSAndy Whitcroft#studly caps, commented out until figure out how to distinguish between use of existing and adding new
22550a920b5bSAndy Whitcroft#		if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) {
22560a920b5bSAndy Whitcroft#		    print "No studly caps, use _\n";
22570a920b5bSAndy Whitcroft#		    print "$herecurr";
22580a920b5bSAndy Whitcroft#		    $clean = 0;
22590a920b5bSAndy Whitcroft#		}
22600a920b5bSAndy Whitcroft
22610a920b5bSAndy Whitcroft#no spaces allowed after \ in define
2262c45dcabdSAndy Whitcroft		if ($line=~/\#\s*define.*\\\s$/) {
2263de7d4f0eSAndy Whitcroft			WARN("Whitepspace after \\ makes next lines useless\n" . $herecurr);
22640a920b5bSAndy Whitcroft		}
22650a920b5bSAndy Whitcroft
2266653d4876SAndy Whitcroft#warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
2267c45dcabdSAndy Whitcroft		if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
2268e09dec48SAndy Whitcroft			my $file = "$1.h";
2269e09dec48SAndy Whitcroft			my $checkfile = "include/linux/$file";
2270e09dec48SAndy Whitcroft			if (-f "$root/$checkfile" &&
2271e09dec48SAndy Whitcroft			    $realfile ne $checkfile &&
2272c45dcabdSAndy Whitcroft			    $1 ne 'irq')
2273c45dcabdSAndy Whitcroft			{
2274e09dec48SAndy Whitcroft				if ($realfile =~ m{^arch/}) {
2275e09dec48SAndy Whitcroft					CHK("Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
2276e09dec48SAndy Whitcroft				} else {
2277e09dec48SAndy Whitcroft					WARN("Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
2278e09dec48SAndy Whitcroft				}
22790a920b5bSAndy Whitcroft			}
22800a920b5bSAndy Whitcroft		}
22810a920b5bSAndy Whitcroft
2282653d4876SAndy Whitcroft# multi-statement macros should be enclosed in a do while loop, grab the
2283653d4876SAndy Whitcroft# first statement and ensure its the whole macro if its not enclosed
2284cf655043SAndy Whitcroft# in a known good container
2285b8f96a31SAndy Whitcroft		if ($realfile !~ m@/vmlinux.lds.h$@ &&
2286b8f96a31SAndy Whitcroft		    $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
2287d8aaf121SAndy Whitcroft			my $ln = $linenr;
2288d8aaf121SAndy Whitcroft			my $cnt = $realcnt;
2289c45dcabdSAndy Whitcroft			my ($off, $dstat, $dcond, $rest);
2290c45dcabdSAndy Whitcroft			my $ctx = '';
2291653d4876SAndy Whitcroft
2292c45dcabdSAndy Whitcroft			my $args = defined($1);
2293de7d4f0eSAndy Whitcroft
2294c45dcabdSAndy Whitcroft			# Find the end of the macro and limit our statement
2295c45dcabdSAndy Whitcroft			# search to that.
2296c45dcabdSAndy Whitcroft			while ($cnt > 0 && defined $lines[$ln - 1] &&
2297c45dcabdSAndy Whitcroft				$lines[$ln - 1] =~ /^(?:-|..*\\$)/)
2298c45dcabdSAndy Whitcroft			{
2299c45dcabdSAndy Whitcroft				$ctx .= $rawlines[$ln - 1] . "\n";
2300a3bb97a7SAndy Whitcroft				$cnt-- if ($lines[$ln - 1] !~ /^-/);
2301c45dcabdSAndy Whitcroft				$ln++;
2302de7d4f0eSAndy Whitcroft			}
2303c45dcabdSAndy Whitcroft			$ctx .= $rawlines[$ln - 1];
2304d8aaf121SAndy Whitcroft
2305c45dcabdSAndy Whitcroft			($dstat, $dcond, $ln, $cnt, $off) =
2306c45dcabdSAndy Whitcroft				ctx_statement_block($linenr, $ln - $linenr + 1, 0);
2307c45dcabdSAndy Whitcroft			#print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
2308a3bb97a7SAndy Whitcroft			#print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
2309c45dcabdSAndy Whitcroft
2310c45dcabdSAndy Whitcroft			# Extract the remainder of the define (if any) and
2311c45dcabdSAndy Whitcroft			# rip off surrounding spaces, and trailing \'s.
2312c45dcabdSAndy Whitcroft			$rest = '';
2313636d140aSAndy Whitcroft			while ($off != 0 || ($cnt > 0 && $rest =~ /\\\s*$/)) {
2314636d140aSAndy Whitcroft				#print "ADDING cnt<$cnt> $off <" . substr($lines[$ln - 1], $off) . "> rest<$rest>\n";
2315a3bb97a7SAndy Whitcroft				if ($off != 0 || $lines[$ln - 1] !~ /^-/) {
2316c45dcabdSAndy Whitcroft					$rest .= substr($lines[$ln - 1], $off) . "\n";
2317c45dcabdSAndy Whitcroft					$cnt--;
2318a3bb97a7SAndy Whitcroft				}
2319a3bb97a7SAndy Whitcroft				$ln++;
2320c45dcabdSAndy Whitcroft				$off = 0;
2321c45dcabdSAndy Whitcroft			}
2322c45dcabdSAndy Whitcroft			$rest =~ s/\\\n.//g;
2323c45dcabdSAndy Whitcroft			$rest =~ s/^\s*//s;
2324c45dcabdSAndy Whitcroft			$rest =~ s/\s*$//s;
2325c45dcabdSAndy Whitcroft
2326c45dcabdSAndy Whitcroft			# Clean up the original statement.
2327c45dcabdSAndy Whitcroft			if ($args) {
2328c45dcabdSAndy Whitcroft				substr($dstat, 0, length($dcond), '');
2329d8aaf121SAndy Whitcroft			} else {
2330c45dcabdSAndy Whitcroft				$dstat =~ s/^.\s*\#\s*define\s+$Ident\s*//;
2331c45dcabdSAndy Whitcroft			}
2332292f1a9bSAndy Whitcroft			$dstat =~ s/$;//g;
2333c45dcabdSAndy Whitcroft			$dstat =~ s/\\\n.//g;
2334c45dcabdSAndy Whitcroft			$dstat =~ s/^\s*//s;
2335c45dcabdSAndy Whitcroft			$dstat =~ s/\s*$//s;
2336c45dcabdSAndy Whitcroft
2337c45dcabdSAndy Whitcroft			# Flatten any parentheses and braces
2338bf30d6edSAndy Whitcroft			while ($dstat =~ s/\([^\(\)]*\)/1/ ||
2339bf30d6edSAndy Whitcroft			       $dstat =~ s/\{[^\{\}]*\}/1/ ||
2340bf30d6edSAndy Whitcroft			       $dstat =~ s/\[[^\{\}]*\]/1/)
2341bf30d6edSAndy Whitcroft			{
2342c45dcabdSAndy Whitcroft			}
2343c45dcabdSAndy Whitcroft
2344c45dcabdSAndy Whitcroft			my $exceptions = qr{
2345c45dcabdSAndy Whitcroft				$Declare|
2346c45dcabdSAndy Whitcroft				module_param_named|
2347c45dcabdSAndy Whitcroft				MODULE_PARAM_DESC|
2348c45dcabdSAndy Whitcroft				DECLARE_PER_CPU|
2349c45dcabdSAndy Whitcroft				DEFINE_PER_CPU|
2350383099fdSAndy Whitcroft				__typeof__\(|
2351ea71a0a0SAndy Whitcroft				\.$Ident\s*=\s*|
2352ea71a0a0SAndy Whitcroft				^\"|\"$
2353c45dcabdSAndy Whitcroft			}x;
2354383099fdSAndy Whitcroft			#print "REST<$rest> dstat<$dstat>\n";
2355c45dcabdSAndy Whitcroft			if ($rest ne '') {
2356c45dcabdSAndy Whitcroft				if ($rest !~ /while\s*\(/ &&
2357c45dcabdSAndy Whitcroft				    $dstat !~ /$exceptions/)
2358c45dcabdSAndy Whitcroft				{
2359c45dcabdSAndy Whitcroft					ERROR("Macros with multiple statements should be enclosed in a do - while loop\n" . "$here\n$ctx\n");
2360c45dcabdSAndy Whitcroft				}
2361c45dcabdSAndy Whitcroft
2362c45dcabdSAndy Whitcroft			} elsif ($ctx !~ /;/) {
2363c45dcabdSAndy Whitcroft				if ($dstat ne '' &&
2364c45dcabdSAndy Whitcroft				    $dstat !~ /^(?:$Ident|-?$Constant)$/ &&
2365c45dcabdSAndy Whitcroft				    $dstat !~ /$exceptions/ &&
2366b132e5d5SAndy Whitcroft				    $dstat !~ /^\.$Ident\s*=/ &&
2367c45dcabdSAndy Whitcroft				    $dstat =~ /$Operators/)
2368c45dcabdSAndy Whitcroft				{
2369de7d4f0eSAndy Whitcroft					ERROR("Macros with complex values should be enclosed in parenthesis\n" . "$here\n$ctx\n");
2370d8aaf121SAndy Whitcroft				}
23710a920b5bSAndy Whitcroft			}
2372653d4876SAndy Whitcroft		}
23730a920b5bSAndy Whitcroft
2374080ba929SMike Frysinger# make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
2375080ba929SMike Frysinger# all assignments may have only one of the following with an assignment:
2376080ba929SMike Frysinger#	.
2377080ba929SMike Frysinger#	ALIGN(...)
2378080ba929SMike Frysinger#	VMLINUX_SYMBOL(...)
2379080ba929SMike Frysinger		if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
2380080ba929SMike Frysinger			WARN("vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
2381080ba929SMike Frysinger		}
2382080ba929SMike Frysinger
2383f0a594c1SAndy Whitcroft# check for redundant bracing round if etc
238413214adfSAndy Whitcroft		if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
238513214adfSAndy Whitcroft			my ($level, $endln, @chunks) =
2386cf655043SAndy Whitcroft				ctx_statement_full($linenr, $realcnt, 1);
238713214adfSAndy Whitcroft			#print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
2388cf655043SAndy Whitcroft			#print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
2389cf655043SAndy Whitcroft			if ($#chunks > 0 && $level == 0) {
239013214adfSAndy Whitcroft				my $allowed = 0;
239113214adfSAndy Whitcroft				my $seen = 0;
2392773647a0SAndy Whitcroft				my $herectx = $here . "\n";
2393cf655043SAndy Whitcroft				my $ln = $linenr - 1;
239413214adfSAndy Whitcroft				for my $chunk (@chunks) {
239513214adfSAndy Whitcroft					my ($cond, $block) = @{$chunk};
239613214adfSAndy Whitcroft
2397773647a0SAndy Whitcroft					# If the condition carries leading newlines, then count those as offsets.
2398773647a0SAndy Whitcroft					my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
2399773647a0SAndy Whitcroft					my $offset = statement_rawlines($whitespace) - 1;
2400773647a0SAndy Whitcroft
2401773647a0SAndy Whitcroft					#print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
2402773647a0SAndy Whitcroft
2403773647a0SAndy Whitcroft					# We have looked at and allowed this specific line.
2404773647a0SAndy Whitcroft					$suppress_ifbraces{$ln + $offset} = 1;
2405773647a0SAndy Whitcroft
2406773647a0SAndy Whitcroft					$herectx .= "$rawlines[$ln + $offset]\n[...]\n";
2407cf655043SAndy Whitcroft					$ln += statement_rawlines($block) - 1;
2408cf655043SAndy Whitcroft
2409773647a0SAndy Whitcroft					substr($block, 0, length($cond), '');
241013214adfSAndy Whitcroft
241113214adfSAndy Whitcroft					$seen++ if ($block =~ /^\s*{/);
241213214adfSAndy Whitcroft
2413cf655043SAndy Whitcroft					#print "cond<$cond> block<$block> allowed<$allowed>\n";
2414cf655043SAndy Whitcroft					if (statement_lines($cond) > 1) {
2415cf655043SAndy Whitcroft						#print "APW: ALLOWED: cond<$cond>\n";
241613214adfSAndy Whitcroft						$allowed = 1;
241713214adfSAndy Whitcroft					}
241813214adfSAndy Whitcroft					if ($block =~/\b(?:if|for|while)\b/) {
2419cf655043SAndy Whitcroft						#print "APW: ALLOWED: block<$block>\n";
242013214adfSAndy Whitcroft						$allowed = 1;
242113214adfSAndy Whitcroft					}
2422cf655043SAndy Whitcroft					if (statement_block_size($block) > 1) {
2423cf655043SAndy Whitcroft						#print "APW: ALLOWED: lines block<$block>\n";
242413214adfSAndy Whitcroft						$allowed = 1;
242513214adfSAndy Whitcroft					}
242613214adfSAndy Whitcroft				}
242713214adfSAndy Whitcroft				if ($seen && !$allowed) {
2428cf655043SAndy Whitcroft					WARN("braces {} are not necessary for any arm of this statement\n" . $herectx);
242913214adfSAndy Whitcroft				}
243013214adfSAndy Whitcroft			}
243113214adfSAndy Whitcroft		}
2432773647a0SAndy Whitcroft		if (!defined $suppress_ifbraces{$linenr - 1} &&
243313214adfSAndy Whitcroft					$line =~ /\b(if|while|for|else)\b/) {
2434cf655043SAndy Whitcroft			my $allowed = 0;
2435f0a594c1SAndy Whitcroft
2436cf655043SAndy Whitcroft			# Check the pre-context.
2437cf655043SAndy Whitcroft			if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
2438cf655043SAndy Whitcroft				#print "APW: ALLOWED: pre<$1>\n";
2439cf655043SAndy Whitcroft				$allowed = 1;
2440f0a594c1SAndy Whitcroft			}
2441773647a0SAndy Whitcroft
2442773647a0SAndy Whitcroft			my ($level, $endln, @chunks) =
2443773647a0SAndy Whitcroft				ctx_statement_full($linenr, $realcnt, $-[0]);
2444773647a0SAndy Whitcroft
2445cf655043SAndy Whitcroft			# Check the condition.
2446cf655043SAndy Whitcroft			my ($cond, $block) = @{$chunks[0]};
2447773647a0SAndy Whitcroft			#print "CHECKING<$linenr> cond<$cond> block<$block>\n";
2448cf655043SAndy Whitcroft			if (defined $cond) {
2449773647a0SAndy Whitcroft				substr($block, 0, length($cond), '');
2450cf655043SAndy Whitcroft			}
2451cf655043SAndy Whitcroft			if (statement_lines($cond) > 1) {
2452cf655043SAndy Whitcroft				#print "APW: ALLOWED: cond<$cond>\n";
2453cf655043SAndy Whitcroft				$allowed = 1;
2454cf655043SAndy Whitcroft			}
2455cf655043SAndy Whitcroft			if ($block =~/\b(?:if|for|while)\b/) {
2456cf655043SAndy Whitcroft				#print "APW: ALLOWED: block<$block>\n";
2457cf655043SAndy Whitcroft				$allowed = 1;
2458cf655043SAndy Whitcroft			}
2459cf655043SAndy Whitcroft			if (statement_block_size($block) > 1) {
2460cf655043SAndy Whitcroft				#print "APW: ALLOWED: lines block<$block>\n";
2461cf655043SAndy Whitcroft				$allowed = 1;
2462cf655043SAndy Whitcroft			}
2463cf655043SAndy Whitcroft			# Check the post-context.
2464cf655043SAndy Whitcroft			if (defined $chunks[1]) {
2465cf655043SAndy Whitcroft				my ($cond, $block) = @{$chunks[1]};
2466cf655043SAndy Whitcroft				if (defined $cond) {
2467773647a0SAndy Whitcroft					substr($block, 0, length($cond), '');
2468cf655043SAndy Whitcroft				}
2469cf655043SAndy Whitcroft				if ($block =~ /^\s*\{/) {
2470cf655043SAndy Whitcroft					#print "APW: ALLOWED: chunk-1 block<$block>\n";
2471cf655043SAndy Whitcroft					$allowed = 1;
2472cf655043SAndy Whitcroft				}
2473cf655043SAndy Whitcroft			}
2474cf655043SAndy Whitcroft			if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
2475cf655043SAndy Whitcroft				my $herectx = $here . "\n";;
2476f055663cSAndy Whitcroft				my $cnt = statement_rawlines($block);
2477cf655043SAndy Whitcroft
2478f055663cSAndy Whitcroft				for (my $n = 0; $n < $cnt; $n++) {
2479f055663cSAndy Whitcroft					$herectx .= raw_line($linenr, $n) . "\n";;
2480cf655043SAndy Whitcroft				}
2481cf655043SAndy Whitcroft
2482cf655043SAndy Whitcroft				WARN("braces {} are not necessary for single statement blocks\n" . $herectx);
2483f0a594c1SAndy Whitcroft			}
2484f0a594c1SAndy Whitcroft		}
2485f0a594c1SAndy Whitcroft
2486653d4876SAndy Whitcroft# don't include deprecated include files (uses RAW line)
24874a0df2efSAndy Whitcroft		for my $inc (@dep_includes) {
2488c45dcabdSAndy Whitcroft			if ($rawline =~ m@^.\s*\#\s*include\s*\<$inc>@) {
2489de7d4f0eSAndy Whitcroft				ERROR("Don't use <$inc>: see Documentation/feature-removal-schedule.txt\n" . $herecurr);
24900a920b5bSAndy Whitcroft			}
24910a920b5bSAndy Whitcroft		}
24920a920b5bSAndy Whitcroft
24934a0df2efSAndy Whitcroft# don't use deprecated functions
24944a0df2efSAndy Whitcroft		for my $func (@dep_functions) {
249500df344fSAndy Whitcroft			if ($line =~ /\b$func\b/) {
2496de7d4f0eSAndy Whitcroft				ERROR("Don't use $func(): see Documentation/feature-removal-schedule.txt\n" . $herecurr);
24974a0df2efSAndy Whitcroft			}
24984a0df2efSAndy Whitcroft		}
24994a0df2efSAndy Whitcroft
25004a0df2efSAndy Whitcroft# no volatiles please
25016c72ffaaSAndy Whitcroft		my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
25026c72ffaaSAndy Whitcroft		if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
2503de7d4f0eSAndy Whitcroft			WARN("Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
25044a0df2efSAndy Whitcroft		}
25054a0df2efSAndy Whitcroft
25069c0ca6f9SAndy Whitcroft# SPIN_LOCK_UNLOCKED & RW_LOCK_UNLOCKED are deprecated
25079c0ca6f9SAndy Whitcroft		if ($line =~ /\b(SPIN_LOCK_UNLOCKED|RW_LOCK_UNLOCKED)/) {
25089c0ca6f9SAndy Whitcroft			ERROR("Use of $1 is deprecated: see Documentation/spinlocks.txt\n" . $herecurr);
25099c0ca6f9SAndy Whitcroft		}
25109c0ca6f9SAndy Whitcroft
251100df344fSAndy Whitcroft# warn about #if 0
2512c45dcabdSAndy Whitcroft		if ($line =~ /^.\s*\#\s*if\s+0\b/) {
2513de7d4f0eSAndy Whitcroft			CHK("if this code is redundant consider removing it\n" .
2514de7d4f0eSAndy Whitcroft				$herecurr);
25154a0df2efSAndy Whitcroft		}
25164a0df2efSAndy Whitcroft
2517f0a594c1SAndy Whitcroft# check for needless kfree() checks
2518f0a594c1SAndy Whitcroft		if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
2519f0a594c1SAndy Whitcroft			my $expr = $1;
2520f0a594c1SAndy Whitcroft			if ($line =~ /\bkfree\(\Q$expr\E\);/) {
25213c232147SWolfram Sang				WARN("kfree(NULL) is safe this check is probably not required\n" . $hereprev);
2522f0a594c1SAndy Whitcroft			}
2523f0a594c1SAndy Whitcroft		}
25244c432a8fSGreg Kroah-Hartman# check for needless usb_free_urb() checks
25254c432a8fSGreg Kroah-Hartman		if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
25264c432a8fSGreg Kroah-Hartman			my $expr = $1;
25274c432a8fSGreg Kroah-Hartman			if ($line =~ /\busb_free_urb\(\Q$expr\E\);/) {
25284c432a8fSGreg Kroah-Hartman				WARN("usb_free_urb(NULL) is safe this check is probably not required\n" . $hereprev);
25294c432a8fSGreg Kroah-Hartman			}
25304c432a8fSGreg Kroah-Hartman		}
2531f0a594c1SAndy Whitcroft
253200df344fSAndy Whitcroft# warn about #ifdefs in C files
2533c45dcabdSAndy Whitcroft#		if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
253400df344fSAndy Whitcroft#			print "#ifdef in C files should be avoided\n";
253500df344fSAndy Whitcroft#			print "$herecurr";
253600df344fSAndy Whitcroft#			$clean = 0;
253700df344fSAndy Whitcroft#		}
253800df344fSAndy Whitcroft
253922f2a2efSAndy Whitcroft# warn about spacing in #ifdefs
2540c45dcabdSAndy Whitcroft		if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
254122f2a2efSAndy Whitcroft			ERROR("exactly one space required after that #$1\n" . $herecurr);
254222f2a2efSAndy Whitcroft		}
254322f2a2efSAndy Whitcroft
25444a0df2efSAndy Whitcroft# check for spinlock_t definitions without a comment.
2545171ae1a4SAndy Whitcroft		if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
2546171ae1a4SAndy Whitcroft		    $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
25474a0df2efSAndy Whitcroft			my $which = $1;
25484a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
2549de7d4f0eSAndy Whitcroft				CHK("$1 definition without comment\n" . $herecurr);
25504a0df2efSAndy Whitcroft			}
25514a0df2efSAndy Whitcroft		}
25524a0df2efSAndy Whitcroft# check for memory barriers without a comment.
25534a0df2efSAndy Whitcroft		if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
25544a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
2555de7d4f0eSAndy Whitcroft				CHK("memory barrier without comment\n" . $herecurr);
25564a0df2efSAndy Whitcroft			}
25574a0df2efSAndy Whitcroft		}
25584a0df2efSAndy Whitcroft# check of hardware specific defines
2559c45dcabdSAndy Whitcroft		if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
2560de7d4f0eSAndy Whitcroft			CHK("architecture specific defines should be avoided\n" .  $herecurr);
25610a920b5bSAndy Whitcroft		}
2562653d4876SAndy Whitcroft
2563de7d4f0eSAndy Whitcroft# check the location of the inline attribute, that it is between
2564de7d4f0eSAndy Whitcroft# storage class and type.
25659c0ca6f9SAndy Whitcroft		if ($line =~ /\b$Type\s+$Inline\b/ ||
25669c0ca6f9SAndy Whitcroft		    $line =~ /\b$Inline\s+$Storage\b/) {
2567de7d4f0eSAndy Whitcroft			ERROR("inline keyword should sit between storage class and type\n" . $herecurr);
2568de7d4f0eSAndy Whitcroft		}
2569de7d4f0eSAndy Whitcroft
25708905a67cSAndy Whitcroft# Check for __inline__ and __inline, prefer inline
25718905a67cSAndy Whitcroft		if ($line =~ /\b(__inline__|__inline)\b/) {
25728905a67cSAndy Whitcroft			WARN("plain inline is preferred over $1\n" . $herecurr);
25738905a67cSAndy Whitcroft		}
25748905a67cSAndy Whitcroft
2575de7d4f0eSAndy Whitcroft# check for new externs in .c files.
2576171ae1a4SAndy Whitcroft		if ($realfile =~ /\.c$/ && defined $stat &&
2577c45dcabdSAndy Whitcroft		    $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
2578171ae1a4SAndy Whitcroft		{
2579c45dcabdSAndy Whitcroft			my $function_name = $1;
2580c45dcabdSAndy Whitcroft			my $paren_space = $2;
2581171ae1a4SAndy Whitcroft
2582171ae1a4SAndy Whitcroft			my $s = $stat;
2583171ae1a4SAndy Whitcroft			if (defined $cond) {
2584171ae1a4SAndy Whitcroft				substr($s, 0, length($cond), '');
2585171ae1a4SAndy Whitcroft			}
2586c45dcabdSAndy Whitcroft			if ($s =~ /^\s*;/ &&
2587c45dcabdSAndy Whitcroft			    $function_name ne 'uninitialized_var')
2588c45dcabdSAndy Whitcroft			{
2589de7d4f0eSAndy Whitcroft				WARN("externs should be avoided in .c files\n" .  $herecurr);
2590de7d4f0eSAndy Whitcroft			}
2591de7d4f0eSAndy Whitcroft
2592171ae1a4SAndy Whitcroft			if ($paren_space =~ /\n/) {
2593171ae1a4SAndy Whitcroft				WARN("arguments for function declarations should follow identifier\n" . $herecurr);
2594171ae1a4SAndy Whitcroft			}
25959c9ba34eSAndy Whitcroft
25969c9ba34eSAndy Whitcroft		} elsif ($realfile =~ /\.c$/ && defined $stat &&
25979c9ba34eSAndy Whitcroft		    $stat =~ /^.\s*extern\s+/)
25989c9ba34eSAndy Whitcroft		{
25999c9ba34eSAndy Whitcroft			WARN("externs should be avoided in .c files\n" .  $herecurr);
2600171ae1a4SAndy Whitcroft		}
2601171ae1a4SAndy Whitcroft
2602de7d4f0eSAndy Whitcroft# checks for new __setup's
2603de7d4f0eSAndy Whitcroft		if ($rawline =~ /\b__setup\("([^"]*)"/) {
2604de7d4f0eSAndy Whitcroft			my $name = $1;
2605de7d4f0eSAndy Whitcroft
2606de7d4f0eSAndy Whitcroft			if (!grep(/$name/, @setup_docs)) {
2607de7d4f0eSAndy Whitcroft				CHK("__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
2608de7d4f0eSAndy Whitcroft			}
2609653d4876SAndy Whitcroft		}
26109c0ca6f9SAndy Whitcroft
26119c0ca6f9SAndy Whitcroft# check for pointless casting of kmalloc return
26129c0ca6f9SAndy Whitcroft		if ($line =~ /\*\s*\)\s*k[czm]alloc\b/) {
26139c0ca6f9SAndy Whitcroft			WARN("unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
26149c0ca6f9SAndy Whitcroft		}
261513214adfSAndy Whitcroft
261613214adfSAndy Whitcroft# check for gcc specific __FUNCTION__
261713214adfSAndy Whitcroft		if ($line =~ /__FUNCTION__/) {
261813214adfSAndy Whitcroft			WARN("__func__ should be used instead of gcc specific __FUNCTION__\n"  . $herecurr);
261913214adfSAndy Whitcroft		}
2620773647a0SAndy Whitcroft
2621773647a0SAndy Whitcroft# check for semaphores used as mutexes
2622171ae1a4SAndy Whitcroft		if ($line =~ /^.\s*(DECLARE_MUTEX|init_MUTEX)\s*\(/) {
2623773647a0SAndy Whitcroft			WARN("mutexes are preferred for single holder semaphores\n" . $herecurr);
2624773647a0SAndy Whitcroft		}
2625773647a0SAndy Whitcroft# check for semaphores used as mutexes
2626171ae1a4SAndy Whitcroft		if ($line =~ /^.\s*init_MUTEX_LOCKED\s*\(/) {
2627773647a0SAndy Whitcroft			WARN("consider using a completion\n" . $herecurr);
2628773647a0SAndy Whitcroft		}
2629773647a0SAndy Whitcroft# recommend strict_strto* over simple_strto*
2630773647a0SAndy Whitcroft		if ($line =~ /\bsimple_(strto.*?)\s*\(/) {
2631773647a0SAndy Whitcroft			WARN("consider using strict_$1 in preference to simple_$1\n" . $herecurr);
2632773647a0SAndy Whitcroft		}
2633f3db6639SMichael Ellerman# check for __initcall(), use device_initcall() explicitly please
2634f3db6639SMichael Ellerman		if ($line =~ /^.\s*__initcall\s*\(/) {
2635f3db6639SMichael Ellerman			WARN("please use device_initcall() instead of __initcall()\n" . $herecurr);
2636f3db6639SMichael Ellerman		}
26372b6db5cbSAndy Whitcroft# check for struct file_operations, ensure they are const.
26386903ffb2SAndy Whitcroft		if ($line !~ /\bconst\b/ &&
26396903ffb2SAndy Whitcroft		    $line =~ /\bstruct\s+(file_operations|seq_operations)\b/) {
26406903ffb2SAndy Whitcroft			WARN("struct $1 should normally be const\n" .
26416903ffb2SAndy Whitcroft				$herecurr);
26422b6db5cbSAndy Whitcroft		}
2643773647a0SAndy Whitcroft
2644773647a0SAndy Whitcroft# use of NR_CPUS is usually wrong
2645773647a0SAndy Whitcroft# ignore definitions of NR_CPUS and usage to define arrays as likely right
2646773647a0SAndy Whitcroft		if ($line =~ /\bNR_CPUS\b/ &&
2647c45dcabdSAndy Whitcroft		    $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
2648c45dcabdSAndy Whitcroft		    $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
2649171ae1a4SAndy Whitcroft		    $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
2650171ae1a4SAndy Whitcroft		    $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
2651171ae1a4SAndy Whitcroft		    $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
2652773647a0SAndy Whitcroft		{
2653773647a0SAndy Whitcroft			WARN("usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
2654773647a0SAndy Whitcroft		}
26559c9ba34eSAndy Whitcroft
26569c9ba34eSAndy Whitcroft# check for %L{u,d,i} in strings
26579c9ba34eSAndy Whitcroft		my $string;
26589c9ba34eSAndy Whitcroft		while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
26599c9ba34eSAndy Whitcroft			$string = substr($rawline, $-[1], $+[1] - $-[1]);
26602a1bc5d5SAndy Whitcroft			$string =~ s/%%/__/g;
26619c9ba34eSAndy Whitcroft			if ($string =~ /(?<!%)%L[udi]/) {
26629c9ba34eSAndy Whitcroft				WARN("\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
26639c9ba34eSAndy Whitcroft				last;
26649c9ba34eSAndy Whitcroft			}
26659c9ba34eSAndy Whitcroft		}
2666691d77b6SAndy Whitcroft
2667691d77b6SAndy Whitcroft# whine mightly about in_atomic
2668691d77b6SAndy Whitcroft		if ($line =~ /\bin_atomic\s*\(/) {
2669691d77b6SAndy Whitcroft			if ($realfile =~ m@^drivers/@) {
2670691d77b6SAndy Whitcroft				ERROR("do not use in_atomic in drivers\n" . $herecurr);
2671f4a87736SAndy Whitcroft			} elsif ($realfile !~ m@^kernel/@) {
2672691d77b6SAndy Whitcroft				WARN("use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
2673691d77b6SAndy Whitcroft			}
2674691d77b6SAndy Whitcroft		}
267513214adfSAndy Whitcroft	}
267613214adfSAndy Whitcroft
267713214adfSAndy Whitcroft	# If we have no input at all, then there is nothing to report on
267813214adfSAndy Whitcroft	# so just keep quiet.
267913214adfSAndy Whitcroft	if ($#rawlines == -1) {
268013214adfSAndy Whitcroft		exit(0);
26810a920b5bSAndy Whitcroft	}
26820a920b5bSAndy Whitcroft
26838905a67cSAndy Whitcroft	# In mailback mode only produce a report in the negative, for
26848905a67cSAndy Whitcroft	# things that appear to be patches.
26858905a67cSAndy Whitcroft	if ($mailback && ($clean == 1 || !$is_patch)) {
26868905a67cSAndy Whitcroft		exit(0);
26878905a67cSAndy Whitcroft	}
26888905a67cSAndy Whitcroft
26898905a67cSAndy Whitcroft	# This is not a patch, and we are are in 'no-patch' mode so
26908905a67cSAndy Whitcroft	# just keep quiet.
26918905a67cSAndy Whitcroft	if (!$chk_patch && !$is_patch) {
26928905a67cSAndy Whitcroft		exit(0);
26938905a67cSAndy Whitcroft	}
26948905a67cSAndy Whitcroft
26958905a67cSAndy Whitcroft	if (!$is_patch) {
2696de7d4f0eSAndy Whitcroft		ERROR("Does not appear to be a unified-diff format patch\n");
26970a920b5bSAndy Whitcroft	}
26980a920b5bSAndy Whitcroft	if ($is_patch && $chk_signoff && $signoff == 0) {
2699de7d4f0eSAndy Whitcroft		ERROR("Missing Signed-off-by: line(s)\n");
27000a920b5bSAndy Whitcroft	}
27010a920b5bSAndy Whitcroft
2702f0a594c1SAndy Whitcroft	print report_dump();
270313214adfSAndy Whitcroft	if ($summary && !($clean == 1 && $quiet == 1)) {
270413214adfSAndy Whitcroft		print "$filename " if ($summary_file);
27056c72ffaaSAndy Whitcroft		print "total: $cnt_error errors, $cnt_warn warnings, " .
27066c72ffaaSAndy Whitcroft			(($check)? "$cnt_chk checks, " : "") .
27076c72ffaaSAndy Whitcroft			"$cnt_lines lines checked\n";
27088905a67cSAndy Whitcroft		print "\n" if ($quiet == 0);
27096c72ffaaSAndy Whitcroft	}
27108905a67cSAndy Whitcroft
27110a920b5bSAndy Whitcroft	if ($clean == 1 && $quiet == 0) {
2712c2fdda0dSAndy Whitcroft		print "$vname has no obvious style problems and is ready for submission.\n"
27130a920b5bSAndy Whitcroft	}
27140a920b5bSAndy Whitcroft	if ($clean == 0 && $quiet == 0) {
2715c2fdda0dSAndy Whitcroft		print "$vname has style problems, please review.  If any of these errors\n";
27160a920b5bSAndy Whitcroft		print "are false positives report them to the maintainer, see\n";
27170a920b5bSAndy Whitcroft		print "CHECKPATCH in MAINTAINERS.\n";
27180a920b5bSAndy Whitcroft	}
271913214adfSAndy Whitcroft
27200a920b5bSAndy Whitcroft	return $clean;
27210a920b5bSAndy Whitcroft}
2722