xref: /linux-6.15/scripts/checkpatch.pl (revision 691e669b)
10a920b5bSAndy Whitcroft#!/usr/bin/perl -w
2dbf004d7SDave 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
192*691e669bSJoe Perchesour $logFunctions = qr{(?x:
193*691e669bSJoe Perches	printk|
194*691e669bSJoe Perches	pr_(debug|dbg|vdbg|devel|info|warning|err|notice|alert|crit|emerg|cont)|
195*691e669bSJoe Perches	dev_(printk|dbg|vdbg|info|warn|err|notice|alert|crit|emerg|WARN)|
196*691e669bSJoe Perches	WARN|
197*691e669bSJoe Perches	panic
198*691e669bSJoe Perches)};
199*691e669bSJoe Perches
2008905a67cSAndy Whitcroftour @typeList = (
2018905a67cSAndy Whitcroft	qr{void},
202c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?char},
203c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?short},
204c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?int},
205c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?long},
206c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?long\s+int},
207c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?long\s+long},
208c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?long\s+long\s+int},
2098905a67cSAndy Whitcroft	qr{unsigned},
2108905a67cSAndy Whitcroft	qr{float},
2118905a67cSAndy Whitcroft	qr{double},
2128905a67cSAndy Whitcroft	qr{bool},
2138905a67cSAndy Whitcroft	qr{struct\s+$Ident},
2148905a67cSAndy Whitcroft	qr{union\s+$Ident},
2158905a67cSAndy Whitcroft	qr{enum\s+$Ident},
2168905a67cSAndy Whitcroft	qr{${Ident}_t},
2178905a67cSAndy Whitcroft	qr{${Ident}_handler},
2188905a67cSAndy Whitcroft	qr{${Ident}_handler_fn},
2198905a67cSAndy Whitcroft);
220c45dcabdSAndy Whitcroftour @modifierList = (
221c45dcabdSAndy Whitcroft	qr{fastcall},
222c45dcabdSAndy Whitcroft);
2238905a67cSAndy Whitcroft
2248905a67cSAndy Whitcroftsub build_types {
225d2172eb5SAndy Whitcroft	my $mods = "(?x:  \n" . join("|\n  ", @modifierList) . "\n)";
226d2172eb5SAndy Whitcroft	my $all = "(?x:  \n" . join("|\n  ", @typeList) . "\n)";
227c8cb2ca3SAndy Whitcroft	$Modifier	= qr{(?:$Attribute|$Sparse|$mods)};
2288905a67cSAndy Whitcroft	$NonptrType	= qr{
229d2172eb5SAndy Whitcroft			(?:$Modifier\s+|const\s+)*
230cf655043SAndy Whitcroft			(?:
231c45dcabdSAndy Whitcroft				(?:typeof|__typeof__)\s*\(\s*\**\s*$Ident\s*\)|
2328ed22cadSAndy Whitcroft				(?:$typeTypedefs\b)|
233c45dcabdSAndy Whitcroft				(?:${all}\b)
234cf655043SAndy Whitcroft			)
235c8cb2ca3SAndy Whitcroft			(?:\s+$Modifier|\s+const)*
2368905a67cSAndy Whitcroft		  }x;
2378905a67cSAndy Whitcroft	$Type	= qr{
238c45dcabdSAndy Whitcroft			$NonptrType
23965863862SAndy Whitcroft			(?:[\s\*]+\s*const|[\s\*]+|(?:\s*\[\s*\])+)?
240c8cb2ca3SAndy Whitcroft			(?:\s+$Inline|\s+$Modifier)*
2418905a67cSAndy Whitcroft		  }x;
2428905a67cSAndy Whitcroft	$Declare	= qr{(?:$Storage\s+)?$Type};
2438905a67cSAndy Whitcroft}
2448905a67cSAndy Whitcroftbuild_types();
2456c72ffaaSAndy Whitcroft
2466c72ffaaSAndy Whitcroft$chk_signoff = 0 if ($file);
2470a920b5bSAndy Whitcroft
2484a0df2efSAndy Whitcroftmy @dep_includes = ();
2494a0df2efSAndy Whitcroftmy @dep_functions = ();
2506c72ffaaSAndy Whitcroftmy $removal = "Documentation/feature-removal-schedule.txt";
2516c72ffaaSAndy Whitcroftif ($tree && -f "$root/$removal") {
25221caa13cSAndy Whitcroft	open(my $REMOVE, '<', "$root/$removal") ||
2536c72ffaaSAndy Whitcroft				die "$P: $removal: open failed - $!\n";
25421caa13cSAndy Whitcroft	while (<$REMOVE>) {
255f0a594c1SAndy Whitcroft		if (/^Check:\s+(.*\S)/) {
256f0a594c1SAndy Whitcroft			for my $entry (split(/[, ]+/, $1)) {
257f0a594c1SAndy Whitcroft				if ($entry =~ m@include/(.*)@) {
2584a0df2efSAndy Whitcroft					push(@dep_includes, $1);
2594a0df2efSAndy Whitcroft
260f0a594c1SAndy Whitcroft				} elsif ($entry !~ m@/@) {
261f0a594c1SAndy Whitcroft					push(@dep_functions, $entry);
262f0a594c1SAndy Whitcroft				}
2634a0df2efSAndy Whitcroft			}
2640a920b5bSAndy Whitcroft		}
2650a920b5bSAndy Whitcroft	}
26621caa13cSAndy Whitcroft	close($REMOVE);
2670a920b5bSAndy Whitcroft}
2680a920b5bSAndy Whitcroft
26900df344fSAndy Whitcroftmy @rawlines = ();
270c2fdda0dSAndy Whitcroftmy @lines = ();
271c2fdda0dSAndy Whitcroftmy $vname;
2726c72ffaaSAndy Whitcroftfor my $filename (@ARGV) {
27321caa13cSAndy Whitcroft	my $FILE;
2746c72ffaaSAndy Whitcroft	if ($file) {
27521caa13cSAndy Whitcroft		open($FILE, '-|', "diff -u /dev/null $filename") ||
2766c72ffaaSAndy Whitcroft			die "$P: $filename: diff failed - $!\n";
27721caa13cSAndy Whitcroft	} elsif ($filename eq '-') {
27821caa13cSAndy Whitcroft		open($FILE, '<&STDIN');
2796c72ffaaSAndy Whitcroft	} else {
28021caa13cSAndy Whitcroft		open($FILE, '<', "$filename") ||
2816c72ffaaSAndy Whitcroft			die "$P: $filename: open failed - $!\n";
2826c72ffaaSAndy Whitcroft	}
283c2fdda0dSAndy Whitcroft	if ($filename eq '-') {
284c2fdda0dSAndy Whitcroft		$vname = 'Your patch';
285c2fdda0dSAndy Whitcroft	} else {
286c2fdda0dSAndy Whitcroft		$vname = $filename;
287c2fdda0dSAndy Whitcroft	}
28821caa13cSAndy Whitcroft	while (<$FILE>) {
2890a920b5bSAndy Whitcroft		chomp;
29000df344fSAndy Whitcroft		push(@rawlines, $_);
2916c72ffaaSAndy Whitcroft	}
29221caa13cSAndy Whitcroft	close($FILE);
293c2fdda0dSAndy Whitcroft	if (!process($filename)) {
2940a920b5bSAndy Whitcroft		$exit = 1;
2950a920b5bSAndy Whitcroft	}
29600df344fSAndy Whitcroft	@rawlines = ();
29713214adfSAndy Whitcroft	@lines = ();
2980a920b5bSAndy Whitcroft}
2990a920b5bSAndy Whitcroft
3000a920b5bSAndy Whitcroftexit($exit);
3010a920b5bSAndy Whitcroft
3020a920b5bSAndy Whitcroftsub top_of_kernel_tree {
3036c72ffaaSAndy Whitcroft	my ($root) = @_;
3046c72ffaaSAndy Whitcroft
3056c72ffaaSAndy Whitcroft	my @tree_check = (
3066c72ffaaSAndy Whitcroft		"COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
3076c72ffaaSAndy Whitcroft		"README", "Documentation", "arch", "include", "drivers",
3086c72ffaaSAndy Whitcroft		"fs", "init", "ipc", "kernel", "lib", "scripts",
3096c72ffaaSAndy Whitcroft	);
3106c72ffaaSAndy Whitcroft
3116c72ffaaSAndy Whitcroft	foreach my $check (@tree_check) {
3126c72ffaaSAndy Whitcroft		if (! -e $root . '/' . $check) {
3130a920b5bSAndy Whitcroft			return 0;
3140a920b5bSAndy Whitcroft		}
3156c72ffaaSAndy Whitcroft	}
3166c72ffaaSAndy Whitcroft	return 1;
3176c72ffaaSAndy Whitcroft}
3180a920b5bSAndy Whitcroft
3190a920b5bSAndy Whitcroftsub expand_tabs {
3200a920b5bSAndy Whitcroft	my ($str) = @_;
3210a920b5bSAndy Whitcroft
3220a920b5bSAndy Whitcroft	my $res = '';
3230a920b5bSAndy Whitcroft	my $n = 0;
3240a920b5bSAndy Whitcroft	for my $c (split(//, $str)) {
3250a920b5bSAndy Whitcroft		if ($c eq "\t") {
3260a920b5bSAndy Whitcroft			$res .= ' ';
3270a920b5bSAndy Whitcroft			$n++;
3280a920b5bSAndy Whitcroft			for (; ($n % 8) != 0; $n++) {
3290a920b5bSAndy Whitcroft				$res .= ' ';
3300a920b5bSAndy Whitcroft			}
3310a920b5bSAndy Whitcroft			next;
3320a920b5bSAndy Whitcroft		}
3330a920b5bSAndy Whitcroft		$res .= $c;
3340a920b5bSAndy Whitcroft		$n++;
3350a920b5bSAndy Whitcroft	}
3360a920b5bSAndy Whitcroft
3370a920b5bSAndy Whitcroft	return $res;
3380a920b5bSAndy Whitcroft}
3396c72ffaaSAndy Whitcroftsub copy_spacing {
340773647a0SAndy Whitcroft	(my $res = shift) =~ tr/\t/ /c;
3416c72ffaaSAndy Whitcroft	return $res;
3426c72ffaaSAndy Whitcroft}
3430a920b5bSAndy Whitcroft
3444a0df2efSAndy Whitcroftsub line_stats {
3454a0df2efSAndy Whitcroft	my ($line) = @_;
3464a0df2efSAndy Whitcroft
3474a0df2efSAndy Whitcroft	# Drop the diff line leader and expand tabs
3484a0df2efSAndy Whitcroft	$line =~ s/^.//;
3494a0df2efSAndy Whitcroft	$line = expand_tabs($line);
3504a0df2efSAndy Whitcroft
3514a0df2efSAndy Whitcroft	# Pick the indent from the front of the line.
3524a0df2efSAndy Whitcroft	my ($white) = ($line =~ /^(\s*)/);
3534a0df2efSAndy Whitcroft
3544a0df2efSAndy Whitcroft	return (length($line), length($white));
3554a0df2efSAndy Whitcroft}
3564a0df2efSAndy Whitcroft
357773647a0SAndy Whitcroftmy $sanitise_quote = '';
358773647a0SAndy Whitcroft
359773647a0SAndy Whitcroftsub sanitise_line_reset {
360773647a0SAndy Whitcroft	my ($in_comment) = @_;
361773647a0SAndy Whitcroft
362773647a0SAndy Whitcroft	if ($in_comment) {
363773647a0SAndy Whitcroft		$sanitise_quote = '*/';
364773647a0SAndy Whitcroft	} else {
365773647a0SAndy Whitcroft		$sanitise_quote = '';
366773647a0SAndy Whitcroft	}
367773647a0SAndy Whitcroft}
36800df344fSAndy Whitcroftsub sanitise_line {
36900df344fSAndy Whitcroft	my ($line) = @_;
37000df344fSAndy Whitcroft
37100df344fSAndy Whitcroft	my $res = '';
37200df344fSAndy Whitcroft	my $l = '';
37300df344fSAndy Whitcroft
374c2fdda0dSAndy Whitcroft	my $qlen = 0;
375773647a0SAndy Whitcroft	my $off = 0;
376773647a0SAndy Whitcroft	my $c;
37700df344fSAndy Whitcroft
378773647a0SAndy Whitcroft	# Always copy over the diff marker.
379773647a0SAndy Whitcroft	$res = substr($line, 0, 1);
380773647a0SAndy Whitcroft
381773647a0SAndy Whitcroft	for ($off = 1; $off < length($line); $off++) {
382773647a0SAndy Whitcroft		$c = substr($line, $off, 1);
383773647a0SAndy Whitcroft
384773647a0SAndy Whitcroft		# Comments we are wacking completly including the begin
385773647a0SAndy Whitcroft		# and end, all to $;.
386773647a0SAndy Whitcroft		if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
387773647a0SAndy Whitcroft			$sanitise_quote = '*/';
388773647a0SAndy Whitcroft
389773647a0SAndy Whitcroft			substr($res, $off, 2, "$;$;");
390773647a0SAndy Whitcroft			$off++;
39100df344fSAndy Whitcroft			next;
392773647a0SAndy Whitcroft		}
39381bc0e02SAndy Whitcroft		if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
394773647a0SAndy Whitcroft			$sanitise_quote = '';
395773647a0SAndy Whitcroft			substr($res, $off, 2, "$;$;");
396773647a0SAndy Whitcroft			$off++;
397773647a0SAndy Whitcroft			next;
398773647a0SAndy Whitcroft		}
399113f04a8SDaniel Walker		if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
400113f04a8SDaniel Walker			$sanitise_quote = '//';
401113f04a8SDaniel Walker
402113f04a8SDaniel Walker			substr($res, $off, 2, $sanitise_quote);
403113f04a8SDaniel Walker			$off++;
404113f04a8SDaniel Walker			next;
405113f04a8SDaniel Walker		}
406773647a0SAndy Whitcroft
407773647a0SAndy Whitcroft		# A \ in a string means ignore the next character.
408773647a0SAndy Whitcroft		if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
409773647a0SAndy Whitcroft		    $c eq "\\") {
410773647a0SAndy Whitcroft			substr($res, $off, 2, 'XX');
411773647a0SAndy Whitcroft			$off++;
412773647a0SAndy Whitcroft			next;
413773647a0SAndy Whitcroft		}
414773647a0SAndy Whitcroft		# Regular quotes.
415773647a0SAndy Whitcroft		if ($c eq "'" || $c eq '"') {
416773647a0SAndy Whitcroft			if ($sanitise_quote eq '') {
417773647a0SAndy Whitcroft				$sanitise_quote = $c;
418773647a0SAndy Whitcroft
419773647a0SAndy Whitcroft				substr($res, $off, 1, $c);
420773647a0SAndy Whitcroft				next;
421773647a0SAndy Whitcroft			} elsif ($sanitise_quote eq $c) {
422773647a0SAndy Whitcroft				$sanitise_quote = '';
42300df344fSAndy Whitcroft			}
42400df344fSAndy Whitcroft		}
425773647a0SAndy Whitcroft
426fae17daeSAndy Whitcroft		#print "c<$c> SQ<$sanitise_quote>\n";
427773647a0SAndy Whitcroft		if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
428773647a0SAndy Whitcroft			substr($res, $off, 1, $;);
429113f04a8SDaniel Walker		} elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
430113f04a8SDaniel Walker			substr($res, $off, 1, $;);
431773647a0SAndy Whitcroft		} elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
432773647a0SAndy Whitcroft			substr($res, $off, 1, 'X');
43300df344fSAndy Whitcroft		} else {
434773647a0SAndy Whitcroft			substr($res, $off, 1, $c);
43500df344fSAndy Whitcroft		}
436c2fdda0dSAndy Whitcroft	}
437c2fdda0dSAndy Whitcroft
438113f04a8SDaniel Walker	if ($sanitise_quote eq '//') {
439113f04a8SDaniel Walker		$sanitise_quote = '';
440113f04a8SDaniel Walker	}
441113f04a8SDaniel Walker
442c2fdda0dSAndy Whitcroft	# The pathname on a #include may be surrounded by '<' and '>'.
443c45dcabdSAndy Whitcroft	if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
444c2fdda0dSAndy Whitcroft		my $clean = 'X' x length($1);
445c2fdda0dSAndy Whitcroft		$res =~ s@\<.*\>@<$clean>@;
446c2fdda0dSAndy Whitcroft
447c2fdda0dSAndy Whitcroft	# The whole of a #error is a string.
448c45dcabdSAndy Whitcroft	} elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
449c2fdda0dSAndy Whitcroft		my $clean = 'X' x length($1);
450c45dcabdSAndy Whitcroft		$res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
451c2fdda0dSAndy Whitcroft	}
452c2fdda0dSAndy Whitcroft
45300df344fSAndy Whitcroft	return $res;
45400df344fSAndy Whitcroft}
45500df344fSAndy Whitcroft
4568905a67cSAndy Whitcroftsub ctx_statement_block {
4578905a67cSAndy Whitcroft	my ($linenr, $remain, $off) = @_;
4588905a67cSAndy Whitcroft	my $line = $linenr - 1;
4598905a67cSAndy Whitcroft	my $blk = '';
4608905a67cSAndy Whitcroft	my $soff = $off;
4618905a67cSAndy Whitcroft	my $coff = $off - 1;
462773647a0SAndy Whitcroft	my $coff_set = 0;
4638905a67cSAndy Whitcroft
46413214adfSAndy Whitcroft	my $loff = 0;
46513214adfSAndy Whitcroft
4668905a67cSAndy Whitcroft	my $type = '';
4678905a67cSAndy Whitcroft	my $level = 0;
468a2750645SAndy Whitcroft	my @stack = ();
469cf655043SAndy Whitcroft	my $p;
4708905a67cSAndy Whitcroft	my $c;
4718905a67cSAndy Whitcroft	my $len = 0;
47213214adfSAndy Whitcroft
47313214adfSAndy Whitcroft	my $remainder;
4748905a67cSAndy Whitcroft	while (1) {
475a2750645SAndy Whitcroft		@stack = (['', 0]) if ($#stack == -1);
476a2750645SAndy Whitcroft
477773647a0SAndy Whitcroft		#warn "CSB: blk<$blk> remain<$remain>\n";
4788905a67cSAndy Whitcroft		# If we are about to drop off the end, pull in more
4798905a67cSAndy Whitcroft		# context.
4808905a67cSAndy Whitcroft		if ($off >= $len) {
4818905a67cSAndy Whitcroft			for (; $remain > 0; $line++) {
482dea33496SAndy Whitcroft				last if (!defined $lines[$line]);
483c2fdda0dSAndy Whitcroft				next if ($lines[$line] =~ /^-/);
4848905a67cSAndy Whitcroft				$remain--;
48513214adfSAndy Whitcroft				$loff = $len;
486c2fdda0dSAndy Whitcroft				$blk .= $lines[$line] . "\n";
4878905a67cSAndy Whitcroft				$len = length($blk);
4888905a67cSAndy Whitcroft				$line++;
4898905a67cSAndy Whitcroft				last;
4908905a67cSAndy Whitcroft			}
4918905a67cSAndy Whitcroft			# Bail if there is no further context.
4928905a67cSAndy Whitcroft			#warn "CSB: blk<$blk> off<$off> len<$len>\n";
49313214adfSAndy Whitcroft			if ($off >= $len) {
4948905a67cSAndy Whitcroft				last;
4958905a67cSAndy Whitcroft			}
4968905a67cSAndy Whitcroft		}
497cf655043SAndy Whitcroft		$p = $c;
4988905a67cSAndy Whitcroft		$c = substr($blk, $off, 1);
49913214adfSAndy Whitcroft		$remainder = substr($blk, $off);
5008905a67cSAndy Whitcroft
501773647a0SAndy Whitcroft		#warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
5024635f4fbSAndy Whitcroft
5034635f4fbSAndy Whitcroft		# Handle nested #if/#else.
5044635f4fbSAndy Whitcroft		if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
5054635f4fbSAndy Whitcroft			push(@stack, [ $type, $level ]);
5064635f4fbSAndy Whitcroft		} elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
5074635f4fbSAndy Whitcroft			($type, $level) = @{$stack[$#stack - 1]};
5084635f4fbSAndy Whitcroft		} elsif ($remainder =~ /^#\s*endif\b/) {
5094635f4fbSAndy Whitcroft			($type, $level) = @{pop(@stack)};
5104635f4fbSAndy Whitcroft		}
5114635f4fbSAndy Whitcroft
5128905a67cSAndy Whitcroft		# Statement ends at the ';' or a close '}' at the
5138905a67cSAndy Whitcroft		# outermost level.
5148905a67cSAndy Whitcroft		if ($level == 0 && $c eq ';') {
5158905a67cSAndy Whitcroft			last;
5168905a67cSAndy Whitcroft		}
5178905a67cSAndy Whitcroft
51813214adfSAndy Whitcroft		# An else is really a conditional as long as its not else if
519773647a0SAndy Whitcroft		if ($level == 0 && $coff_set == 0 &&
520773647a0SAndy Whitcroft				(!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
521773647a0SAndy Whitcroft				$remainder =~ /^(else)(?:\s|{)/ &&
522773647a0SAndy Whitcroft				$remainder !~ /^else\s+if\b/) {
523773647a0SAndy Whitcroft			$coff = $off + length($1) - 1;
524773647a0SAndy Whitcroft			$coff_set = 1;
525773647a0SAndy Whitcroft			#warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
526773647a0SAndy Whitcroft			#warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
52713214adfSAndy Whitcroft		}
52813214adfSAndy Whitcroft
5298905a67cSAndy Whitcroft		if (($type eq '' || $type eq '(') && $c eq '(') {
5308905a67cSAndy Whitcroft			$level++;
5318905a67cSAndy Whitcroft			$type = '(';
5328905a67cSAndy Whitcroft		}
5338905a67cSAndy Whitcroft		if ($type eq '(' && $c eq ')') {
5348905a67cSAndy Whitcroft			$level--;
5358905a67cSAndy Whitcroft			$type = ($level != 0)? '(' : '';
5368905a67cSAndy Whitcroft
5378905a67cSAndy Whitcroft			if ($level == 0 && $coff < $soff) {
5388905a67cSAndy Whitcroft				$coff = $off;
539773647a0SAndy Whitcroft				$coff_set = 1;
540773647a0SAndy Whitcroft				#warn "CSB: mark coff<$coff>\n";
5418905a67cSAndy Whitcroft			}
5428905a67cSAndy Whitcroft		}
5438905a67cSAndy Whitcroft		if (($type eq '' || $type eq '{') && $c eq '{') {
5448905a67cSAndy Whitcroft			$level++;
5458905a67cSAndy Whitcroft			$type = '{';
5468905a67cSAndy Whitcroft		}
5478905a67cSAndy Whitcroft		if ($type eq '{' && $c eq '}') {
5488905a67cSAndy Whitcroft			$level--;
5498905a67cSAndy Whitcroft			$type = ($level != 0)? '{' : '';
5508905a67cSAndy Whitcroft
5518905a67cSAndy Whitcroft			if ($level == 0) {
5528905a67cSAndy Whitcroft				last;
5538905a67cSAndy Whitcroft			}
5548905a67cSAndy Whitcroft		}
5558905a67cSAndy Whitcroft		$off++;
5568905a67cSAndy Whitcroft	}
557a3bb97a7SAndy Whitcroft	# We are truly at the end, so shuffle to the next line.
55813214adfSAndy Whitcroft	if ($off == $len) {
559a3bb97a7SAndy Whitcroft		$loff = $len + 1;
56013214adfSAndy Whitcroft		$line++;
56113214adfSAndy Whitcroft		$remain--;
56213214adfSAndy Whitcroft	}
5638905a67cSAndy Whitcroft
5648905a67cSAndy Whitcroft	my $statement = substr($blk, $soff, $off - $soff + 1);
5658905a67cSAndy Whitcroft	my $condition = substr($blk, $soff, $coff - $soff + 1);
5668905a67cSAndy Whitcroft
5678905a67cSAndy Whitcroft	#warn "STATEMENT<$statement>\n";
5688905a67cSAndy Whitcroft	#warn "CONDITION<$condition>\n";
5698905a67cSAndy Whitcroft
570773647a0SAndy Whitcroft	#print "coff<$coff> soff<$off> loff<$loff>\n";
57113214adfSAndy Whitcroft
57213214adfSAndy Whitcroft	return ($statement, $condition,
57313214adfSAndy Whitcroft			$line, $remain + 1, $off - $loff + 1, $level);
57413214adfSAndy Whitcroft}
57513214adfSAndy Whitcroft
576cf655043SAndy Whitcroftsub statement_lines {
577cf655043SAndy Whitcroft	my ($stmt) = @_;
578cf655043SAndy Whitcroft
579cf655043SAndy Whitcroft	# Strip the diff line prefixes and rip blank lines at start and end.
580cf655043SAndy Whitcroft	$stmt =~ s/(^|\n)./$1/g;
581cf655043SAndy Whitcroft	$stmt =~ s/^\s*//;
582cf655043SAndy Whitcroft	$stmt =~ s/\s*$//;
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_rawlines {
590cf655043SAndy Whitcroft	my ($stmt) = @_;
591cf655043SAndy Whitcroft
592cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
593cf655043SAndy Whitcroft
594cf655043SAndy Whitcroft	return $#stmt_lines + 2;
595cf655043SAndy Whitcroft}
596cf655043SAndy Whitcroft
597cf655043SAndy Whitcroftsub statement_block_size {
598cf655043SAndy Whitcroft	my ($stmt) = @_;
599cf655043SAndy Whitcroft
600cf655043SAndy Whitcroft	$stmt =~ s/(^|\n)./$1/g;
601cf655043SAndy Whitcroft	$stmt =~ s/^\s*{//;
602cf655043SAndy Whitcroft	$stmt =~ s/}\s*$//;
603cf655043SAndy Whitcroft	$stmt =~ s/^\s*//;
604cf655043SAndy Whitcroft	$stmt =~ s/\s*$//;
605cf655043SAndy Whitcroft
606cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
607cf655043SAndy Whitcroft	my @stmt_statements = ($stmt =~ /;/g);
608cf655043SAndy Whitcroft
609cf655043SAndy Whitcroft	my $stmt_lines = $#stmt_lines + 2;
610cf655043SAndy Whitcroft	my $stmt_statements = $#stmt_statements + 1;
611cf655043SAndy Whitcroft
612cf655043SAndy Whitcroft	if ($stmt_lines > $stmt_statements) {
613cf655043SAndy Whitcroft		return $stmt_lines;
614cf655043SAndy Whitcroft	} else {
615cf655043SAndy Whitcroft		return $stmt_statements;
616cf655043SAndy Whitcroft	}
617cf655043SAndy Whitcroft}
618cf655043SAndy Whitcroft
61913214adfSAndy Whitcroftsub ctx_statement_full {
62013214adfSAndy Whitcroft	my ($linenr, $remain, $off) = @_;
62113214adfSAndy Whitcroft	my ($statement, $condition, $level);
62213214adfSAndy Whitcroft
62313214adfSAndy Whitcroft	my (@chunks);
62413214adfSAndy Whitcroft
625cf655043SAndy Whitcroft	# Grab the first conditional/block pair.
62613214adfSAndy Whitcroft	($statement, $condition, $linenr, $remain, $off, $level) =
62713214adfSAndy Whitcroft				ctx_statement_block($linenr, $remain, $off);
628773647a0SAndy Whitcroft	#print "F: c<$condition> s<$statement> remain<$remain>\n";
62913214adfSAndy Whitcroft	push(@chunks, [ $condition, $statement ]);
630cf655043SAndy Whitcroft	if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
631cf655043SAndy Whitcroft		return ($level, $linenr, @chunks);
632cf655043SAndy Whitcroft	}
633cf655043SAndy Whitcroft
634cf655043SAndy Whitcroft	# Pull in the following conditional/block pairs and see if they
635cf655043SAndy Whitcroft	# could continue the statement.
636cf655043SAndy Whitcroft	for (;;) {
63713214adfSAndy Whitcroft		($statement, $condition, $linenr, $remain, $off, $level) =
63813214adfSAndy Whitcroft				ctx_statement_block($linenr, $remain, $off);
639cf655043SAndy Whitcroft		#print "C: c<$condition> s<$statement> remain<$remain>\n";
640773647a0SAndy Whitcroft		last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
641cf655043SAndy Whitcroft		#print "C: push\n";
642cf655043SAndy Whitcroft		push(@chunks, [ $condition, $statement ]);
64313214adfSAndy Whitcroft	}
64413214adfSAndy Whitcroft
64513214adfSAndy Whitcroft	return ($level, $linenr, @chunks);
6468905a67cSAndy Whitcroft}
6478905a67cSAndy Whitcroft
6484a0df2efSAndy Whitcroftsub ctx_block_get {
649f0a594c1SAndy Whitcroft	my ($linenr, $remain, $outer, $open, $close, $off) = @_;
6504a0df2efSAndy Whitcroft	my $line;
6514a0df2efSAndy Whitcroft	my $start = $linenr - 1;
6524a0df2efSAndy Whitcroft	my $blk = '';
6534a0df2efSAndy Whitcroft	my @o;
6544a0df2efSAndy Whitcroft	my @c;
6554a0df2efSAndy Whitcroft	my @res = ();
6564a0df2efSAndy Whitcroft
657f0a594c1SAndy Whitcroft	my $level = 0;
6584635f4fbSAndy Whitcroft	my @stack = ($level);
65900df344fSAndy Whitcroft	for ($line = $start; $remain > 0; $line++) {
66000df344fSAndy Whitcroft		next if ($rawlines[$line] =~ /^-/);
66100df344fSAndy Whitcroft		$remain--;
66200df344fSAndy Whitcroft
66300df344fSAndy Whitcroft		$blk .= $rawlines[$line];
6644635f4fbSAndy Whitcroft
6654635f4fbSAndy Whitcroft		# Handle nested #if/#else.
6664635f4fbSAndy Whitcroft		if ($rawlines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
6674635f4fbSAndy Whitcroft			push(@stack, $level);
6684635f4fbSAndy Whitcroft		} elsif ($rawlines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
6694635f4fbSAndy Whitcroft			$level = $stack[$#stack - 1];
6704635f4fbSAndy Whitcroft		} elsif ($rawlines[$line] =~ /^.\s*#\s*endif\b/) {
6714635f4fbSAndy Whitcroft			$level = pop(@stack);
6724635f4fbSAndy Whitcroft		}
6734635f4fbSAndy Whitcroft
674f0a594c1SAndy Whitcroft		foreach my $c (split(//, $rawlines[$line])) {
675f0a594c1SAndy Whitcroft			##print "C<$c>L<$level><$open$close>O<$off>\n";
676f0a594c1SAndy Whitcroft			if ($off > 0) {
677f0a594c1SAndy Whitcroft				$off--;
678f0a594c1SAndy Whitcroft				next;
679f0a594c1SAndy Whitcroft			}
6804a0df2efSAndy Whitcroft
681f0a594c1SAndy Whitcroft			if ($c eq $close && $level > 0) {
682f0a594c1SAndy Whitcroft				$level--;
683f0a594c1SAndy Whitcroft				last if ($level == 0);
684f0a594c1SAndy Whitcroft			} elsif ($c eq $open) {
685f0a594c1SAndy Whitcroft				$level++;
686f0a594c1SAndy Whitcroft			}
687f0a594c1SAndy Whitcroft		}
6884a0df2efSAndy Whitcroft
689f0a594c1SAndy Whitcroft		if (!$outer || $level <= 1) {
69000df344fSAndy Whitcroft			push(@res, $rawlines[$line]);
6914a0df2efSAndy Whitcroft		}
6924a0df2efSAndy Whitcroft
693f0a594c1SAndy Whitcroft		last if ($level == 0);
6944a0df2efSAndy Whitcroft	}
6954a0df2efSAndy Whitcroft
696f0a594c1SAndy Whitcroft	return ($level, @res);
6974a0df2efSAndy Whitcroft}
6984a0df2efSAndy Whitcroftsub ctx_block_outer {
6994a0df2efSAndy Whitcroft	my ($linenr, $remain) = @_;
7004a0df2efSAndy Whitcroft
701f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
702f0a594c1SAndy Whitcroft	return @r;
7034a0df2efSAndy Whitcroft}
7044a0df2efSAndy Whitcroftsub ctx_block {
7054a0df2efSAndy Whitcroft	my ($linenr, $remain) = @_;
7064a0df2efSAndy Whitcroft
707f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
708f0a594c1SAndy Whitcroft	return @r;
709653d4876SAndy Whitcroft}
710653d4876SAndy Whitcroftsub ctx_statement {
711f0a594c1SAndy Whitcroft	my ($linenr, $remain, $off) = @_;
712f0a594c1SAndy Whitcroft
713f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
714f0a594c1SAndy Whitcroft	return @r;
715f0a594c1SAndy Whitcroft}
716f0a594c1SAndy Whitcroftsub ctx_block_level {
717653d4876SAndy Whitcroft	my ($linenr, $remain) = @_;
718653d4876SAndy Whitcroft
719f0a594c1SAndy Whitcroft	return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
7204a0df2efSAndy Whitcroft}
7219c0ca6f9SAndy Whitcroftsub ctx_statement_level {
7229c0ca6f9SAndy Whitcroft	my ($linenr, $remain, $off) = @_;
7239c0ca6f9SAndy Whitcroft
7249c0ca6f9SAndy Whitcroft	return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
7259c0ca6f9SAndy Whitcroft}
7264a0df2efSAndy Whitcroft
7274a0df2efSAndy Whitcroftsub ctx_locate_comment {
7284a0df2efSAndy Whitcroft	my ($first_line, $end_line) = @_;
7294a0df2efSAndy Whitcroft
7304a0df2efSAndy Whitcroft	# Catch a comment on the end of the line itself.
731beae6332SAndy Whitcroft	my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
7324a0df2efSAndy Whitcroft	return $current_comment if (defined $current_comment);
7334a0df2efSAndy Whitcroft
7344a0df2efSAndy Whitcroft	# Look through the context and try and figure out if there is a
7354a0df2efSAndy Whitcroft	# comment.
7364a0df2efSAndy Whitcroft	my $in_comment = 0;
7374a0df2efSAndy Whitcroft	$current_comment = '';
7384a0df2efSAndy Whitcroft	for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
73900df344fSAndy Whitcroft		my $line = $rawlines[$linenr - 1];
74000df344fSAndy Whitcroft		#warn "           $line\n";
7414a0df2efSAndy Whitcroft		if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
7424a0df2efSAndy Whitcroft			$in_comment = 1;
7434a0df2efSAndy Whitcroft		}
7444a0df2efSAndy Whitcroft		if ($line =~ m@/\*@) {
7454a0df2efSAndy Whitcroft			$in_comment = 1;
7464a0df2efSAndy Whitcroft		}
7474a0df2efSAndy Whitcroft		if (!$in_comment && $current_comment ne '') {
7484a0df2efSAndy Whitcroft			$current_comment = '';
7494a0df2efSAndy Whitcroft		}
7504a0df2efSAndy Whitcroft		$current_comment .= $line . "\n" if ($in_comment);
7514a0df2efSAndy Whitcroft		if ($line =~ m@\*/@) {
7524a0df2efSAndy Whitcroft			$in_comment = 0;
7534a0df2efSAndy Whitcroft		}
7544a0df2efSAndy Whitcroft	}
7554a0df2efSAndy Whitcroft
7564a0df2efSAndy Whitcroft	chomp($current_comment);
7574a0df2efSAndy Whitcroft	return($current_comment);
7584a0df2efSAndy Whitcroft}
7594a0df2efSAndy Whitcroftsub ctx_has_comment {
7604a0df2efSAndy Whitcroft	my ($first_line, $end_line) = @_;
7614a0df2efSAndy Whitcroft	my $cmt = ctx_locate_comment($first_line, $end_line);
7624a0df2efSAndy Whitcroft
76300df344fSAndy Whitcroft	##print "LINE: $rawlines[$end_line - 1 ]\n";
7644a0df2efSAndy Whitcroft	##print "CMMT: $cmt\n";
7654a0df2efSAndy Whitcroft
7664a0df2efSAndy Whitcroft	return ($cmt ne '');
7674a0df2efSAndy Whitcroft}
7684a0df2efSAndy Whitcroft
7694d001e4dSAndy Whitcroftsub raw_line {
7704d001e4dSAndy Whitcroft	my ($linenr, $cnt) = @_;
7714d001e4dSAndy Whitcroft
7724d001e4dSAndy Whitcroft	my $offset = $linenr - 1;
7734d001e4dSAndy Whitcroft	$cnt++;
7744d001e4dSAndy Whitcroft
7754d001e4dSAndy Whitcroft	my $line;
7764d001e4dSAndy Whitcroft	while ($cnt) {
7774d001e4dSAndy Whitcroft		$line = $rawlines[$offset++];
7784d001e4dSAndy Whitcroft		next if (defined($line) && $line =~ /^-/);
7794d001e4dSAndy Whitcroft		$cnt--;
7804d001e4dSAndy Whitcroft	}
7814d001e4dSAndy Whitcroft
7824d001e4dSAndy Whitcroft	return $line;
7834d001e4dSAndy Whitcroft}
7844d001e4dSAndy Whitcroft
7850a920b5bSAndy Whitcroftsub cat_vet {
7860a920b5bSAndy Whitcroft	my ($vet) = @_;
7879c0ca6f9SAndy Whitcroft	my ($res, $coded);
7880a920b5bSAndy Whitcroft
7899c0ca6f9SAndy Whitcroft	$res = '';
7906c72ffaaSAndy Whitcroft	while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
7916c72ffaaSAndy Whitcroft		$res .= $1;
7926c72ffaaSAndy Whitcroft		if ($2 ne '') {
7939c0ca6f9SAndy Whitcroft			$coded = sprintf("^%c", unpack('C', $2) + 64);
7946c72ffaaSAndy Whitcroft			$res .= $coded;
7956c72ffaaSAndy Whitcroft		}
7969c0ca6f9SAndy Whitcroft	}
7979c0ca6f9SAndy Whitcroft	$res =~ s/$/\$/;
7980a920b5bSAndy Whitcroft
7999c0ca6f9SAndy Whitcroft	return $res;
8000a920b5bSAndy Whitcroft}
8010a920b5bSAndy Whitcroft
802c2fdda0dSAndy Whitcroftmy $av_preprocessor = 0;
803cf655043SAndy Whitcroftmy $av_pending;
804c2fdda0dSAndy Whitcroftmy @av_paren_type;
8051f65f947SAndy Whitcroftmy $av_pend_colon;
806c2fdda0dSAndy Whitcroft
807c2fdda0dSAndy Whitcroftsub annotate_reset {
808c2fdda0dSAndy Whitcroft	$av_preprocessor = 0;
809cf655043SAndy Whitcroft	$av_pending = '_';
810cf655043SAndy Whitcroft	@av_paren_type = ('E');
8111f65f947SAndy Whitcroft	$av_pend_colon = 'O';
812c2fdda0dSAndy Whitcroft}
813c2fdda0dSAndy Whitcroft
8146c72ffaaSAndy Whitcroftsub annotate_values {
8156c72ffaaSAndy Whitcroft	my ($stream, $type) = @_;
8166c72ffaaSAndy Whitcroft
8176c72ffaaSAndy Whitcroft	my $res;
8181f65f947SAndy Whitcroft	my $var = '_' x length($stream);
8196c72ffaaSAndy Whitcroft	my $cur = $stream;
8206c72ffaaSAndy Whitcroft
821c2fdda0dSAndy Whitcroft	print "$stream\n" if ($dbg_values > 1);
8226c72ffaaSAndy Whitcroft
8236c72ffaaSAndy Whitcroft	while (length($cur)) {
824773647a0SAndy Whitcroft		@av_paren_type = ('E') if ($#av_paren_type < 0);
825cf655043SAndy Whitcroft		print " <" . join('', @av_paren_type) .
826171ae1a4SAndy Whitcroft				"> <$type> <$av_pending>" if ($dbg_values > 1);
8276c72ffaaSAndy Whitcroft		if ($cur =~ /^(\s+)/o) {
828c2fdda0dSAndy Whitcroft			print "WS($1)\n" if ($dbg_values > 1);
829c2fdda0dSAndy Whitcroft			if ($1 =~ /\n/ && $av_preprocessor) {
830cf655043SAndy Whitcroft				$type = pop(@av_paren_type);
831c2fdda0dSAndy Whitcroft				$av_preprocessor = 0;
8326c72ffaaSAndy Whitcroft			}
8336c72ffaaSAndy Whitcroft
8348ea3eb9aSAndy Whitcroft		} elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\()/) {
835c2fdda0dSAndy Whitcroft			print "DECLARE($1)\n" if ($dbg_values > 1);
8366c72ffaaSAndy Whitcroft			$type = 'T';
8376c72ffaaSAndy Whitcroft
838389a2fe5SAndy Whitcroft		} elsif ($cur =~ /^($Modifier)\s*/) {
839389a2fe5SAndy Whitcroft			print "MODIFIER($1)\n" if ($dbg_values > 1);
840389a2fe5SAndy Whitcroft			$type = 'T';
841389a2fe5SAndy Whitcroft
842c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
843171ae1a4SAndy Whitcroft			print "DEFINE($1,$2)\n" if ($dbg_values > 1);
844c2fdda0dSAndy Whitcroft			$av_preprocessor = 1;
845171ae1a4SAndy Whitcroft			push(@av_paren_type, $type);
846171ae1a4SAndy Whitcroft			if ($2 ne '') {
847cf655043SAndy Whitcroft				$av_pending = 'N';
848171ae1a4SAndy Whitcroft			}
849171ae1a4SAndy Whitcroft			$type = 'E';
850171ae1a4SAndy Whitcroft
851c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
852171ae1a4SAndy Whitcroft			print "UNDEF($1)\n" if ($dbg_values > 1);
853171ae1a4SAndy Whitcroft			$av_preprocessor = 1;
854171ae1a4SAndy Whitcroft			push(@av_paren_type, $type);
8556c72ffaaSAndy Whitcroft
856c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
857cf655043SAndy Whitcroft			print "PRE_START($1)\n" if ($dbg_values > 1);
858c2fdda0dSAndy Whitcroft			$av_preprocessor = 1;
859cf655043SAndy Whitcroft
860cf655043SAndy Whitcroft			push(@av_paren_type, $type);
861cf655043SAndy Whitcroft			push(@av_paren_type, $type);
862171ae1a4SAndy Whitcroft			$type = 'E';
863cf655043SAndy Whitcroft
864c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
865cf655043SAndy Whitcroft			print "PRE_RESTART($1)\n" if ($dbg_values > 1);
866cf655043SAndy Whitcroft			$av_preprocessor = 1;
867cf655043SAndy Whitcroft
868cf655043SAndy Whitcroft			push(@av_paren_type, $av_paren_type[$#av_paren_type]);
869cf655043SAndy Whitcroft
870171ae1a4SAndy Whitcroft			$type = 'E';
871cf655043SAndy Whitcroft
872c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:endif))/o) {
873cf655043SAndy Whitcroft			print "PRE_END($1)\n" if ($dbg_values > 1);
874cf655043SAndy Whitcroft
875cf655043SAndy Whitcroft			$av_preprocessor = 1;
876cf655043SAndy Whitcroft
877cf655043SAndy Whitcroft			# Assume all arms of the conditional end as this
878cf655043SAndy Whitcroft			# one does, and continue as if the #endif was not here.
879cf655043SAndy Whitcroft			pop(@av_paren_type);
880cf655043SAndy Whitcroft			push(@av_paren_type, $type);
881171ae1a4SAndy Whitcroft			$type = 'E';
8826c72ffaaSAndy Whitcroft
8836c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\\\n)/o) {
884c2fdda0dSAndy Whitcroft			print "PRECONT($1)\n" if ($dbg_values > 1);
8856c72ffaaSAndy Whitcroft
886171ae1a4SAndy Whitcroft		} elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
887171ae1a4SAndy Whitcroft			print "ATTR($1)\n" if ($dbg_values > 1);
888171ae1a4SAndy Whitcroft			$av_pending = $type;
889171ae1a4SAndy Whitcroft			$type = 'N';
890171ae1a4SAndy Whitcroft
8916c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
892c2fdda0dSAndy Whitcroft			print "SIZEOF($1)\n" if ($dbg_values > 1);
8936c72ffaaSAndy Whitcroft			if (defined $2) {
894cf655043SAndy Whitcroft				$av_pending = 'V';
8956c72ffaaSAndy Whitcroft			}
8966c72ffaaSAndy Whitcroft			$type = 'N';
8976c72ffaaSAndy Whitcroft
89814b111c1SAndy Whitcroft		} elsif ($cur =~ /^(if|while|for)\b/o) {
899c2fdda0dSAndy Whitcroft			print "COND($1)\n" if ($dbg_values > 1);
90014b111c1SAndy Whitcroft			$av_pending = 'E';
9016c72ffaaSAndy Whitcroft			$type = 'N';
9026c72ffaaSAndy Whitcroft
9031f65f947SAndy Whitcroft		} elsif ($cur =~/^(case)/o) {
9041f65f947SAndy Whitcroft			print "CASE($1)\n" if ($dbg_values > 1);
9051f65f947SAndy Whitcroft			$av_pend_colon = 'C';
9061f65f947SAndy Whitcroft			$type = 'N';
9071f65f947SAndy Whitcroft
90814b111c1SAndy Whitcroft		} elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
909c2fdda0dSAndy Whitcroft			print "KEYWORD($1)\n" if ($dbg_values > 1);
9106c72ffaaSAndy Whitcroft			$type = 'N';
9116c72ffaaSAndy Whitcroft
9126c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\()/o) {
913c2fdda0dSAndy Whitcroft			print "PAREN('$1')\n" if ($dbg_values > 1);
914cf655043SAndy Whitcroft			push(@av_paren_type, $av_pending);
915cf655043SAndy Whitcroft			$av_pending = '_';
9166c72ffaaSAndy Whitcroft			$type = 'N';
9176c72ffaaSAndy Whitcroft
9186c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\))/o) {
919cf655043SAndy Whitcroft			my $new_type = pop(@av_paren_type);
920cf655043SAndy Whitcroft			if ($new_type ne '_') {
921cf655043SAndy Whitcroft				$type = $new_type;
922c2fdda0dSAndy Whitcroft				print "PAREN('$1') -> $type\n"
923c2fdda0dSAndy Whitcroft							if ($dbg_values > 1);
9246c72ffaaSAndy Whitcroft			} else {
925c2fdda0dSAndy Whitcroft				print "PAREN('$1')\n" if ($dbg_values > 1);
9266c72ffaaSAndy Whitcroft			}
9276c72ffaaSAndy Whitcroft
928c8cb2ca3SAndy Whitcroft		} elsif ($cur =~ /^($Ident)\s*\(/o) {
929c2fdda0dSAndy Whitcroft			print "FUNC($1)\n" if ($dbg_values > 1);
930c8cb2ca3SAndy Whitcroft			$type = 'V';
931cf655043SAndy Whitcroft			$av_pending = 'V';
9326c72ffaaSAndy Whitcroft
9338e761b04SAndy Whitcroft		} elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
9348e761b04SAndy Whitcroft			if (defined $2 && $type eq 'C' || $type eq 'T') {
9351f65f947SAndy Whitcroft				$av_pend_colon = 'B';
9368e761b04SAndy Whitcroft			} elsif ($type eq 'E') {
9378e761b04SAndy Whitcroft				$av_pend_colon = 'L';
9381f65f947SAndy Whitcroft			}
9391f65f947SAndy Whitcroft			print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
9401f65f947SAndy Whitcroft			$type = 'V';
9411f65f947SAndy Whitcroft
9426c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Ident|$Constant)/o) {
943c2fdda0dSAndy Whitcroft			print "IDENT($1)\n" if ($dbg_values > 1);
9446c72ffaaSAndy Whitcroft			$type = 'V';
9456c72ffaaSAndy Whitcroft
9466c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Assignment)/o) {
947c2fdda0dSAndy Whitcroft			print "ASSIGN($1)\n" if ($dbg_values > 1);
9486c72ffaaSAndy Whitcroft			$type = 'N';
9496c72ffaaSAndy Whitcroft
950cf655043SAndy Whitcroft		} elsif ($cur =~/^(;|{|})/) {
951c2fdda0dSAndy Whitcroft			print "END($1)\n" if ($dbg_values > 1);
95213214adfSAndy Whitcroft			$type = 'E';
9531f65f947SAndy Whitcroft			$av_pend_colon = 'O';
95413214adfSAndy Whitcroft
9558e761b04SAndy Whitcroft		} elsif ($cur =~/^(,)/) {
9568e761b04SAndy Whitcroft			print "COMMA($1)\n" if ($dbg_values > 1);
9578e761b04SAndy Whitcroft			$type = 'C';
9588e761b04SAndy Whitcroft
9591f65f947SAndy Whitcroft		} elsif ($cur =~ /^(\?)/o) {
9601f65f947SAndy Whitcroft			print "QUESTION($1)\n" if ($dbg_values > 1);
9611f65f947SAndy Whitcroft			$type = 'N';
9621f65f947SAndy Whitcroft
9631f65f947SAndy Whitcroft		} elsif ($cur =~ /^(:)/o) {
9641f65f947SAndy Whitcroft			print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
9651f65f947SAndy Whitcroft
9661f65f947SAndy Whitcroft			substr($var, length($res), 1, $av_pend_colon);
9671f65f947SAndy Whitcroft			if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
9681f65f947SAndy Whitcroft				$type = 'E';
9691f65f947SAndy Whitcroft			} else {
9701f65f947SAndy Whitcroft				$type = 'N';
9711f65f947SAndy Whitcroft			}
9721f65f947SAndy Whitcroft			$av_pend_colon = 'O';
9731f65f947SAndy Whitcroft
9748e761b04SAndy Whitcroft		} elsif ($cur =~ /^(\[)/o) {
97513214adfSAndy Whitcroft			print "CLOSE($1)\n" if ($dbg_values > 1);
9766c72ffaaSAndy Whitcroft			$type = 'N';
9776c72ffaaSAndy Whitcroft
9780d413866SAndy Whitcroft		} elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
97974048ed8SAndy Whitcroft			my $variant;
98074048ed8SAndy Whitcroft
98174048ed8SAndy Whitcroft			print "OPV($1)\n" if ($dbg_values > 1);
98274048ed8SAndy Whitcroft			if ($type eq 'V') {
98374048ed8SAndy Whitcroft				$variant = 'B';
98474048ed8SAndy Whitcroft			} else {
98574048ed8SAndy Whitcroft				$variant = 'U';
98674048ed8SAndy Whitcroft			}
98774048ed8SAndy Whitcroft
98874048ed8SAndy Whitcroft			substr($var, length($res), 1, $variant);
98974048ed8SAndy Whitcroft			$type = 'N';
99074048ed8SAndy Whitcroft
9916c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Operators)/o) {
992c2fdda0dSAndy Whitcroft			print "OP($1)\n" if ($dbg_values > 1);
9936c72ffaaSAndy Whitcroft			if ($1 ne '++' && $1 ne '--') {
9946c72ffaaSAndy Whitcroft				$type = 'N';
9956c72ffaaSAndy Whitcroft			}
9966c72ffaaSAndy Whitcroft
9976c72ffaaSAndy Whitcroft		} elsif ($cur =~ /(^.)/o) {
998c2fdda0dSAndy Whitcroft			print "C($1)\n" if ($dbg_values > 1);
9996c72ffaaSAndy Whitcroft		}
10006c72ffaaSAndy Whitcroft		if (defined $1) {
10016c72ffaaSAndy Whitcroft			$cur = substr($cur, length($1));
10026c72ffaaSAndy Whitcroft			$res .= $type x length($1);
10036c72ffaaSAndy Whitcroft		}
10046c72ffaaSAndy Whitcroft	}
10056c72ffaaSAndy Whitcroft
10061f65f947SAndy Whitcroft	return ($res, $var);
10076c72ffaaSAndy Whitcroft}
10086c72ffaaSAndy Whitcroft
10098905a67cSAndy Whitcroftsub possible {
101013214adfSAndy Whitcroft	my ($possible, $line) = @_;
10119a974fdbSAndy Whitcroft	my $notPermitted = qr{(?:
10120776e594SAndy Whitcroft		^(?:
10130776e594SAndy Whitcroft			$Modifier|
10140776e594SAndy Whitcroft			$Storage|
10150776e594SAndy Whitcroft			$Type|
10169a974fdbSAndy Whitcroft			DEFINE_\S+
10179a974fdbSAndy Whitcroft		)$|
10189a974fdbSAndy Whitcroft		^(?:
10190776e594SAndy Whitcroft			goto|
10200776e594SAndy Whitcroft			return|
10210776e594SAndy Whitcroft			case|
10220776e594SAndy Whitcroft			else|
10230776e594SAndy Whitcroft			asm|__asm__|
10240776e594SAndy Whitcroft			do
10259a974fdbSAndy Whitcroft		)(?:\s|$)|
10260776e594SAndy Whitcroft		^(?:typedef|struct|enum)\b
10279a974fdbSAndy Whitcroft	    )}x;
10289a974fdbSAndy Whitcroft	warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
10299a974fdbSAndy Whitcroft	if ($possible !~ $notPermitted) {
1030c45dcabdSAndy Whitcroft		# Check for modifiers.
1031c45dcabdSAndy Whitcroft		$possible =~ s/\s*$Storage\s*//g;
1032c45dcabdSAndy Whitcroft		$possible =~ s/\s*$Sparse\s*//g;
1033c45dcabdSAndy Whitcroft		if ($possible =~ /^\s*$/) {
1034c45dcabdSAndy Whitcroft
1035c45dcabdSAndy Whitcroft		} elsif ($possible =~ /\s/) {
1036c45dcabdSAndy Whitcroft			$possible =~ s/\s*$Type\s*//g;
1037d2506586SAndy Whitcroft			for my $modifier (split(' ', $possible)) {
10389a974fdbSAndy Whitcroft				if ($modifier !~ $notPermitted) {
1039d2506586SAndy Whitcroft					warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1040d2506586SAndy Whitcroft					push(@modifierList, $modifier);
1041d2506586SAndy Whitcroft				}
10429a974fdbSAndy Whitcroft			}
1043c45dcabdSAndy Whitcroft
1044c45dcabdSAndy Whitcroft		} else {
104513214adfSAndy Whitcroft			warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
10468905a67cSAndy Whitcroft			push(@typeList, $possible);
1047c45dcabdSAndy Whitcroft		}
10488905a67cSAndy Whitcroft		build_types();
10490776e594SAndy Whitcroft	} else {
10500776e594SAndy Whitcroft		warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
10518905a67cSAndy Whitcroft	}
10528905a67cSAndy Whitcroft}
10538905a67cSAndy Whitcroft
10546c72ffaaSAndy Whitcroftmy $prefix = '';
10556c72ffaaSAndy Whitcroft
1056f0a594c1SAndy Whitcroftsub report {
1057773647a0SAndy Whitcroft	if (defined $tst_only && $_[0] !~ /\Q$tst_only\E/) {
1058773647a0SAndy Whitcroft		return 0;
1059773647a0SAndy Whitcroft	}
10608905a67cSAndy Whitcroft	my $line = $prefix . $_[0];
10618905a67cSAndy Whitcroft
10628905a67cSAndy Whitcroft	$line = (split('\n', $line))[0] . "\n" if ($terse);
10638905a67cSAndy Whitcroft
106413214adfSAndy Whitcroft	push(our @report, $line);
1065773647a0SAndy Whitcroft
1066773647a0SAndy Whitcroft	return 1;
1067f0a594c1SAndy Whitcroft}
1068f0a594c1SAndy Whitcroftsub report_dump {
106913214adfSAndy Whitcroft	our @report;
1070f0a594c1SAndy Whitcroft}
1071de7d4f0eSAndy Whitcroftsub ERROR {
1072773647a0SAndy Whitcroft	if (report("ERROR: $_[0]\n")) {
1073de7d4f0eSAndy Whitcroft		our $clean = 0;
10746c72ffaaSAndy Whitcroft		our $cnt_error++;
1075de7d4f0eSAndy Whitcroft	}
1076773647a0SAndy Whitcroft}
1077de7d4f0eSAndy Whitcroftsub WARN {
1078773647a0SAndy Whitcroft	if (report("WARNING: $_[0]\n")) {
1079de7d4f0eSAndy Whitcroft		our $clean = 0;
10806c72ffaaSAndy Whitcroft		our $cnt_warn++;
1081de7d4f0eSAndy Whitcroft	}
1082773647a0SAndy Whitcroft}
1083de7d4f0eSAndy Whitcroftsub CHK {
1084773647a0SAndy Whitcroft	if ($check && report("CHECK: $_[0]\n")) {
1085de7d4f0eSAndy Whitcroft		our $clean = 0;
10866c72ffaaSAndy Whitcroft		our $cnt_chk++;
10876c72ffaaSAndy Whitcroft	}
1088de7d4f0eSAndy Whitcroft}
1089de7d4f0eSAndy Whitcroft
10906ecd9674SAndy Whitcroftsub check_absolute_file {
10916ecd9674SAndy Whitcroft	my ($absolute, $herecurr) = @_;
10926ecd9674SAndy Whitcroft	my $file = $absolute;
10936ecd9674SAndy Whitcroft
10946ecd9674SAndy Whitcroft	##print "absolute<$absolute>\n";
10956ecd9674SAndy Whitcroft
10966ecd9674SAndy Whitcroft	# See if any suffix of this path is a path within the tree.
10976ecd9674SAndy Whitcroft	while ($file =~ s@^[^/]*/@@) {
10986ecd9674SAndy Whitcroft		if (-f "$root/$file") {
10996ecd9674SAndy Whitcroft			##print "file<$file>\n";
11006ecd9674SAndy Whitcroft			last;
11016ecd9674SAndy Whitcroft		}
11026ecd9674SAndy Whitcroft	}
11036ecd9674SAndy Whitcroft	if (! -f _)  {
11046ecd9674SAndy Whitcroft		return 0;
11056ecd9674SAndy Whitcroft	}
11066ecd9674SAndy Whitcroft
11076ecd9674SAndy Whitcroft	# It is, so see if the prefix is acceptable.
11086ecd9674SAndy Whitcroft	my $prefix = $absolute;
11096ecd9674SAndy Whitcroft	substr($prefix, -length($file)) = '';
11106ecd9674SAndy Whitcroft
11116ecd9674SAndy Whitcroft	##print "prefix<$prefix>\n";
11126ecd9674SAndy Whitcroft	if ($prefix ne ".../") {
11136ecd9674SAndy Whitcroft		WARN("use relative pathname instead of absolute in changelog text\n" . $herecurr);
11146ecd9674SAndy Whitcroft	}
11156ecd9674SAndy Whitcroft}
11166ecd9674SAndy Whitcroft
11170a920b5bSAndy Whitcroftsub process {
11180a920b5bSAndy Whitcroft	my $filename = shift;
11190a920b5bSAndy Whitcroft
11200a920b5bSAndy Whitcroft	my $linenr=0;
11210a920b5bSAndy Whitcroft	my $prevline="";
1122c2fdda0dSAndy Whitcroft	my $prevrawline="";
11230a920b5bSAndy Whitcroft	my $stashline="";
1124c2fdda0dSAndy Whitcroft	my $stashrawline="";
11250a920b5bSAndy Whitcroft
11264a0df2efSAndy Whitcroft	my $length;
11270a920b5bSAndy Whitcroft	my $indent;
11280a920b5bSAndy Whitcroft	my $previndent=0;
11290a920b5bSAndy Whitcroft	my $stashindent=0;
11300a920b5bSAndy Whitcroft
1131de7d4f0eSAndy Whitcroft	our $clean = 1;
11320a920b5bSAndy Whitcroft	my $signoff = 0;
11330a920b5bSAndy Whitcroft	my $is_patch = 0;
11340a920b5bSAndy Whitcroft
113513214adfSAndy Whitcroft	our @report = ();
11366c72ffaaSAndy Whitcroft	our $cnt_lines = 0;
11376c72ffaaSAndy Whitcroft	our $cnt_error = 0;
11386c72ffaaSAndy Whitcroft	our $cnt_warn = 0;
11396c72ffaaSAndy Whitcroft	our $cnt_chk = 0;
11406c72ffaaSAndy Whitcroft
11410a920b5bSAndy Whitcroft	# Trace the real file/line as we go.
11420a920b5bSAndy Whitcroft	my $realfile = '';
11430a920b5bSAndy Whitcroft	my $realline = 0;
11440a920b5bSAndy Whitcroft	my $realcnt = 0;
11450a920b5bSAndy Whitcroft	my $here = '';
11460a920b5bSAndy Whitcroft	my $in_comment = 0;
1147c2fdda0dSAndy Whitcroft	my $comment_edge = 0;
11480a920b5bSAndy Whitcroft	my $first_line = 0;
11491e855726SWolfram Sang	my $p1_prefix = '';
11500a920b5bSAndy Whitcroft
115113214adfSAndy Whitcroft	my $prev_values = 'E';
115213214adfSAndy Whitcroft
115313214adfSAndy Whitcroft	# suppression flags
1154773647a0SAndy Whitcroft	my %suppress_ifbraces;
1155170d3a22SAndy Whitcroft	my %suppress_whiletrailers;
11562b474a1aSAndy Whitcroft	my %suppress_export;
1157653d4876SAndy Whitcroft
1158c2fdda0dSAndy Whitcroft	# Pre-scan the patch sanitizing the lines.
1159de7d4f0eSAndy Whitcroft	# Pre-scan the patch looking for any __setup documentation.
1160c2fdda0dSAndy Whitcroft	#
1161de7d4f0eSAndy Whitcroft	my @setup_docs = ();
1162de7d4f0eSAndy Whitcroft	my $setup_docs = 0;
1163773647a0SAndy Whitcroft
1164773647a0SAndy Whitcroft	sanitise_line_reset();
1165c2fdda0dSAndy Whitcroft	my $line;
1166c2fdda0dSAndy Whitcroft	foreach my $rawline (@rawlines) {
1167773647a0SAndy Whitcroft		$linenr++;
1168773647a0SAndy Whitcroft		$line = $rawline;
1169c2fdda0dSAndy Whitcroft
1170773647a0SAndy Whitcroft		if ($rawline=~/^\+\+\+\s+(\S+)/) {
1171de7d4f0eSAndy Whitcroft			$setup_docs = 0;
1172de7d4f0eSAndy Whitcroft			if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
1173de7d4f0eSAndy Whitcroft				$setup_docs = 1;
1174de7d4f0eSAndy Whitcroft			}
1175773647a0SAndy Whitcroft			#next;
1176de7d4f0eSAndy Whitcroft		}
1177773647a0SAndy Whitcroft		if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1178773647a0SAndy Whitcroft			$realline=$1-1;
1179773647a0SAndy Whitcroft			if (defined $2) {
1180773647a0SAndy Whitcroft				$realcnt=$3+1;
1181773647a0SAndy Whitcroft			} else {
1182773647a0SAndy Whitcroft				$realcnt=1+1;
1183773647a0SAndy Whitcroft			}
1184c45dcabdSAndy Whitcroft			$in_comment = 0;
1185773647a0SAndy Whitcroft
1186773647a0SAndy Whitcroft			# Guestimate if this is a continuing comment.  Run
1187773647a0SAndy Whitcroft			# the context looking for a comment "edge".  If this
1188773647a0SAndy Whitcroft			# edge is a close comment then we must be in a comment
1189773647a0SAndy Whitcroft			# at context start.
1190773647a0SAndy Whitcroft			my $edge;
119101fa9147SAndy Whitcroft			my $cnt = $realcnt;
119201fa9147SAndy Whitcroft			for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
119301fa9147SAndy Whitcroft				next if (defined $rawlines[$ln - 1] &&
119401fa9147SAndy Whitcroft					 $rawlines[$ln - 1] =~ /^-/);
119501fa9147SAndy Whitcroft				$cnt--;
119601fa9147SAndy Whitcroft				#print "RAW<$rawlines[$ln - 1]>\n";
1197721c1cb6SAndy Whitcroft				last if (!defined $rawlines[$ln - 1]);
1198fae17daeSAndy Whitcroft				if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
1199fae17daeSAndy Whitcroft				    $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
1200fae17daeSAndy Whitcroft					($edge) = $1;
1201fae17daeSAndy Whitcroft					last;
1202fae17daeSAndy Whitcroft				}
1203773647a0SAndy Whitcroft			}
1204773647a0SAndy Whitcroft			if (defined $edge && $edge eq '*/') {
1205773647a0SAndy Whitcroft				$in_comment = 1;
1206773647a0SAndy Whitcroft			}
1207773647a0SAndy Whitcroft
1208773647a0SAndy Whitcroft			# Guestimate if this is a continuing comment.  If this
1209773647a0SAndy Whitcroft			# is the start of a diff block and this line starts
1210773647a0SAndy Whitcroft			# ' *' then it is very likely a comment.
1211773647a0SAndy Whitcroft			if (!defined $edge &&
121283242e0cSAndy Whitcroft			    $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
1213773647a0SAndy Whitcroft			{
1214773647a0SAndy Whitcroft				$in_comment = 1;
1215773647a0SAndy Whitcroft			}
1216773647a0SAndy Whitcroft
1217773647a0SAndy Whitcroft			##print "COMMENT:$in_comment edge<$edge> $rawline\n";
1218773647a0SAndy Whitcroft			sanitise_line_reset($in_comment);
1219773647a0SAndy Whitcroft
1220171ae1a4SAndy Whitcroft		} elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
1221773647a0SAndy Whitcroft			# Standardise the strings and chars within the input to
1222171ae1a4SAndy Whitcroft			# simplify matching -- only bother with positive lines.
1223773647a0SAndy Whitcroft			$line = sanitise_line($rawline);
1224773647a0SAndy Whitcroft		}
1225773647a0SAndy Whitcroft		push(@lines, $line);
1226773647a0SAndy Whitcroft
1227773647a0SAndy Whitcroft		if ($realcnt > 1) {
1228773647a0SAndy Whitcroft			$realcnt-- if ($line =~ /^(?:\+| |$)/);
1229773647a0SAndy Whitcroft		} else {
1230773647a0SAndy Whitcroft			$realcnt = 0;
1231773647a0SAndy Whitcroft		}
1232773647a0SAndy Whitcroft
1233773647a0SAndy Whitcroft		#print "==>$rawline\n";
1234773647a0SAndy Whitcroft		#print "-->$line\n";
1235de7d4f0eSAndy Whitcroft
1236de7d4f0eSAndy Whitcroft		if ($setup_docs && $line =~ /^\+/) {
1237de7d4f0eSAndy Whitcroft			push(@setup_docs, $line);
1238de7d4f0eSAndy Whitcroft		}
1239de7d4f0eSAndy Whitcroft	}
1240de7d4f0eSAndy Whitcroft
12416c72ffaaSAndy Whitcroft	$prefix = '';
12426c72ffaaSAndy Whitcroft
1243773647a0SAndy Whitcroft	$realcnt = 0;
1244773647a0SAndy Whitcroft	$linenr = 0;
12450a920b5bSAndy Whitcroft	foreach my $line (@lines) {
12460a920b5bSAndy Whitcroft		$linenr++;
12470a920b5bSAndy Whitcroft
1248c2fdda0dSAndy Whitcroft		my $rawline = $rawlines[$linenr - 1];
12496c72ffaaSAndy Whitcroft
12500a920b5bSAndy Whitcroft#extract the line range in the file after the patch is applied
12516c72ffaaSAndy Whitcroft		if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
12520a920b5bSAndy Whitcroft			$is_patch = 1;
12534a0df2efSAndy Whitcroft			$first_line = $linenr + 1;
12540a920b5bSAndy Whitcroft			$realline=$1-1;
12550a920b5bSAndy Whitcroft			if (defined $2) {
12560a920b5bSAndy Whitcroft				$realcnt=$3+1;
12570a920b5bSAndy Whitcroft			} else {
12580a920b5bSAndy Whitcroft				$realcnt=1+1;
12590a920b5bSAndy Whitcroft			}
1260c2fdda0dSAndy Whitcroft			annotate_reset();
126113214adfSAndy Whitcroft			$prev_values = 'E';
126213214adfSAndy Whitcroft
1263773647a0SAndy Whitcroft			%suppress_ifbraces = ();
1264170d3a22SAndy Whitcroft			%suppress_whiletrailers = ();
12652b474a1aSAndy Whitcroft			%suppress_export = ();
12660a920b5bSAndy Whitcroft			next;
12670a920b5bSAndy Whitcroft
12684a0df2efSAndy Whitcroft# track the line number as we move through the hunk, note that
12694a0df2efSAndy Whitcroft# new versions of GNU diff omit the leading space on completely
12704a0df2efSAndy Whitcroft# blank context lines so we need to count that too.
1271773647a0SAndy Whitcroft		} elsif ($line =~ /^( |\+|$)/) {
12720a920b5bSAndy Whitcroft			$realline++;
1273d8aaf121SAndy Whitcroft			$realcnt-- if ($realcnt != 0);
12740a920b5bSAndy Whitcroft
12754a0df2efSAndy Whitcroft			# Measure the line length and indent.
1276c2fdda0dSAndy Whitcroft			($length, $indent) = line_stats($rawline);
12770a920b5bSAndy Whitcroft
12780a920b5bSAndy Whitcroft			# Track the previous line.
12790a920b5bSAndy Whitcroft			($prevline, $stashline) = ($stashline, $line);
12800a920b5bSAndy Whitcroft			($previndent, $stashindent) = ($stashindent, $indent);
1281c2fdda0dSAndy Whitcroft			($prevrawline, $stashrawline) = ($stashrawline, $rawline);
1282c2fdda0dSAndy Whitcroft
1283773647a0SAndy Whitcroft			#warn "line<$line>\n";
12846c72ffaaSAndy Whitcroft
1285d8aaf121SAndy Whitcroft		} elsif ($realcnt == 1) {
1286d8aaf121SAndy Whitcroft			$realcnt--;
12870a920b5bSAndy Whitcroft		}
12880a920b5bSAndy Whitcroft
1289cc77cdcaSAndy Whitcroft		my $hunk_line = ($realcnt != 0);
1290cc77cdcaSAndy Whitcroft
12910a920b5bSAndy Whitcroft#make up the handle for any error we report on this line
1292773647a0SAndy Whitcroft		$prefix = "$filename:$realline: " if ($emacs && $file);
1293773647a0SAndy Whitcroft		$prefix = "$filename:$linenr: " if ($emacs && !$file);
1294773647a0SAndy Whitcroft
12956c72ffaaSAndy Whitcroft		$here = "#$linenr: " if (!$file);
12966c72ffaaSAndy Whitcroft		$here = "#$realline: " if ($file);
1297773647a0SAndy Whitcroft
1298773647a0SAndy Whitcroft		# extract the filename as it passes
1299773647a0SAndy Whitcroft		if ($line=~/^\+\+\+\s+(\S+)/) {
1300773647a0SAndy Whitcroft			$realfile = $1;
13011e855726SWolfram Sang			$realfile =~ s@^([^/]*)/@@;
13021e855726SWolfram Sang
13031e855726SWolfram Sang			$p1_prefix = $1;
1304e2f7aa4bSAndy Whitcroft			if (!$file && $tree && $p1_prefix ne '' &&
1305e2f7aa4bSAndy Whitcroft			    -e "$root/$p1_prefix") {
13061e855726SWolfram Sang				WARN("patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
13071e855726SWolfram Sang			}
1308773647a0SAndy Whitcroft
1309c1ab3326SAndy Whitcroft			if ($realfile =~ m@^include/asm/@) {
1310773647a0SAndy Whitcroft				ERROR("do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
1311773647a0SAndy Whitcroft			}
1312773647a0SAndy Whitcroft			next;
1313773647a0SAndy Whitcroft		}
1314773647a0SAndy Whitcroft
1315389834b6SRandy Dunlap		$here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
13160a920b5bSAndy Whitcroft
1317c2fdda0dSAndy Whitcroft		my $hereline = "$here\n$rawline\n";
1318c2fdda0dSAndy Whitcroft		my $herecurr = "$here\n$rawline\n";
1319c2fdda0dSAndy Whitcroft		my $hereprev = "$here\n$prevrawline\n$rawline\n";
13200a920b5bSAndy Whitcroft
13216c72ffaaSAndy Whitcroft		$cnt_lines++ if ($realcnt != 0);
13226c72ffaaSAndy Whitcroft
13230a920b5bSAndy Whitcroft#check the patch for a signoff:
1324d8aaf121SAndy Whitcroft		if ($line =~ /^\s*signed-off-by:/i) {
13254a0df2efSAndy Whitcroft			# This is a signoff, if ugly, so do not double report.
13264a0df2efSAndy Whitcroft			$signoff++;
13270a920b5bSAndy Whitcroft			if (!($line =~ /^\s*Signed-off-by:/)) {
1328de7d4f0eSAndy Whitcroft				WARN("Signed-off-by: is the preferred form\n" .
1329de7d4f0eSAndy Whitcroft					$herecurr);
13300a920b5bSAndy Whitcroft			}
13310a920b5bSAndy Whitcroft			if ($line =~ /^\s*signed-off-by:\S/i) {
1332773647a0SAndy Whitcroft				WARN("space required after Signed-off-by:\n" .
1333de7d4f0eSAndy Whitcroft					$herecurr);
13340a920b5bSAndy Whitcroft			}
13350a920b5bSAndy Whitcroft		}
13360a920b5bSAndy Whitcroft
133700df344fSAndy Whitcroft# Check for wrappage within a valid hunk of the file
13388905a67cSAndy Whitcroft		if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
1339de7d4f0eSAndy Whitcroft			ERROR("patch seems to be corrupt (line wrapped?)\n" .
13406c72ffaaSAndy Whitcroft				$herecurr) if (!$emitted_corrupt++);
1341de7d4f0eSAndy Whitcroft		}
1342de7d4f0eSAndy Whitcroft
13436ecd9674SAndy Whitcroft# Check for absolute kernel paths.
13446ecd9674SAndy Whitcroft		if ($tree) {
13456ecd9674SAndy Whitcroft			while ($line =~ m{(?:^|\s)(/\S*)}g) {
13466ecd9674SAndy Whitcroft				my $file = $1;
13476ecd9674SAndy Whitcroft
13486ecd9674SAndy Whitcroft				if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
13496ecd9674SAndy Whitcroft				    check_absolute_file($1, $herecurr)) {
13506ecd9674SAndy Whitcroft					#
13516ecd9674SAndy Whitcroft				} else {
13526ecd9674SAndy Whitcroft					check_absolute_file($file, $herecurr);
13536ecd9674SAndy Whitcroft				}
13546ecd9674SAndy Whitcroft			}
13556ecd9674SAndy Whitcroft		}
13566ecd9674SAndy Whitcroft
1357de7d4f0eSAndy Whitcroft# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
1358de7d4f0eSAndy Whitcroft		if (($realfile =~ /^$/ || $line =~ /^\+/) &&
1359171ae1a4SAndy Whitcroft		    $rawline !~ m/^$UTF8*$/) {
1360171ae1a4SAndy Whitcroft			my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
1361171ae1a4SAndy Whitcroft
1362171ae1a4SAndy Whitcroft			my $blank = copy_spacing($rawline);
1363171ae1a4SAndy Whitcroft			my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
1364171ae1a4SAndy Whitcroft			my $hereptr = "$hereline$ptr\n";
1365171ae1a4SAndy Whitcroft
1366171ae1a4SAndy Whitcroft			ERROR("Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
136700df344fSAndy Whitcroft		}
13680a920b5bSAndy Whitcroft
136930670854SAndy Whitcroft# ignore non-hunk lines and lines being removed
137030670854SAndy Whitcroft		next if (!$hunk_line || $line =~ /^-/);
137100df344fSAndy Whitcroft
13720a920b5bSAndy Whitcroft#trailing whitespace
13739c0ca6f9SAndy Whitcroft		if ($line =~ /^\+.*\015/) {
1374c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
13759c0ca6f9SAndy Whitcroft			ERROR("DOS line endings\n" . $herevet);
13769c0ca6f9SAndy Whitcroft
1377c2fdda0dSAndy Whitcroft		} elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
1378c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1379de7d4f0eSAndy Whitcroft			ERROR("trailing whitespace\n" . $herevet);
13800a920b5bSAndy Whitcroft		}
13815368df20SAndy Whitcroft
13825368df20SAndy Whitcroft# check we are in a valid source file if not then ignore this hunk
13835368df20SAndy Whitcroft		next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
13845368df20SAndy Whitcroft
13850a920b5bSAndy Whitcroft#80 column limit
1386c45dcabdSAndy Whitcroft		if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ &&
1387f4c014c0SAndy Whitcroft		    $rawline !~ /^.\s*\*\s*\@$Ident\s/ &&
1388*691e669bSJoe Perches		    $line !~ /^\+\s*$logFunctions\s*\(\s*(?:KERN_\S+\s*)?"[X\t]*"\s*(?:,|\)\s*;)\s*$/ &&
1389f4c014c0SAndy Whitcroft		    $length > 80)
1390c45dcabdSAndy Whitcroft		{
1391de7d4f0eSAndy Whitcroft			WARN("line over 80 characters\n" . $herecurr);
13920a920b5bSAndy Whitcroft		}
13930a920b5bSAndy Whitcroft
13948905a67cSAndy Whitcroft# check for adding lines without a newline.
13958905a67cSAndy Whitcroft		if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
13968905a67cSAndy Whitcroft			WARN("adding a line without newline at end of file\n" . $herecurr);
13978905a67cSAndy Whitcroft		}
13988905a67cSAndy Whitcroft
139942e41c54SMike Frysinger# Blackfin: use hi/lo macros
140042e41c54SMike Frysinger		if ($realfile =~ m@arch/blackfin/.*\.S$@) {
140142e41c54SMike Frysinger			if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
140242e41c54SMike Frysinger				my $herevet = "$here\n" . cat_vet($line) . "\n";
140342e41c54SMike Frysinger				ERROR("use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
140442e41c54SMike Frysinger			}
140542e41c54SMike Frysinger			if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
140642e41c54SMike Frysinger				my $herevet = "$here\n" . cat_vet($line) . "\n";
140742e41c54SMike Frysinger				ERROR("use the HI() macro, not (... >> 16)\n" . $herevet);
140842e41c54SMike Frysinger			}
140942e41c54SMike Frysinger		}
141042e41c54SMike Frysinger
1411b9ea10d6SAndy Whitcroft# check we are in a valid source file C or perl if not then ignore this hunk
1412b9ea10d6SAndy Whitcroft		next if ($realfile !~ /\.(h|c|pl)$/);
14130a920b5bSAndy Whitcroft
14140a920b5bSAndy Whitcroft# at the beginning of a line any tabs must come first and anything
14150a920b5bSAndy Whitcroft# more than 8 must use tabs.
1416c2fdda0dSAndy Whitcroft		if ($rawline =~ /^\+\s* \t\s*\S/ ||
1417c2fdda0dSAndy Whitcroft		    $rawline =~ /^\+\s*        \s*/) {
1418c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1419171ae1a4SAndy Whitcroft			ERROR("code indent should use tabs where possible\n" . $herevet);
14200a920b5bSAndy Whitcroft		}
14210a920b5bSAndy Whitcroft
1422b9ea10d6SAndy Whitcroft# check we are in a valid C source file if not then ignore this hunk
1423b9ea10d6SAndy Whitcroft		next if ($realfile !~ /\.(h|c)$/);
1424b9ea10d6SAndy Whitcroft
1425c2fdda0dSAndy Whitcroft# check for RCS/CVS revision markers
1426cf655043SAndy Whitcroft		if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
1427c2fdda0dSAndy Whitcroft			WARN("CVS style keyword markers, these will _not_ be updated\n". $herecurr);
1428c2fdda0dSAndy Whitcroft		}
142922f2a2efSAndy Whitcroft
143042e41c54SMike Frysinger# Blackfin: don't use __builtin_bfin_[cs]sync
143142e41c54SMike Frysinger		if ($line =~ /__builtin_bfin_csync/) {
143242e41c54SMike Frysinger			my $herevet = "$here\n" . cat_vet($line) . "\n";
143342e41c54SMike Frysinger			ERROR("use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
143442e41c54SMike Frysinger		}
143542e41c54SMike Frysinger		if ($line =~ /__builtin_bfin_ssync/) {
143642e41c54SMike Frysinger			my $herevet = "$here\n" . cat_vet($line) . "\n";
143742e41c54SMike Frysinger			ERROR("use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
143842e41c54SMike Frysinger		}
143942e41c54SMike Frysinger
14409c0ca6f9SAndy Whitcroft# Check for potential 'bare' types
14412b474a1aSAndy Whitcroft		my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
14422b474a1aSAndy Whitcroft		    $realline_next);
14439c9ba34eSAndy Whitcroft		if ($realcnt && $line =~ /.\s*\S/) {
1444170d3a22SAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
1445f5fe35ddSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0);
1446171ae1a4SAndy Whitcroft			$stat =~ s/\n./\n /g;
1447171ae1a4SAndy Whitcroft			$cond =~ s/\n./\n /g;
1448171ae1a4SAndy Whitcroft
14492b474a1aSAndy Whitcroft			# Find the real next line.
14502b474a1aSAndy Whitcroft			$realline_next = $line_nr_next;
14512b474a1aSAndy Whitcroft			if (defined $realline_next &&
14522b474a1aSAndy Whitcroft			    (!defined $lines[$realline_next - 1] ||
14532b474a1aSAndy Whitcroft			     substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
14542b474a1aSAndy Whitcroft				$realline_next++;
14552b474a1aSAndy Whitcroft			}
14562b474a1aSAndy Whitcroft
1457171ae1a4SAndy Whitcroft			my $s = $stat;
1458171ae1a4SAndy Whitcroft			$s =~ s/{.*$//s;
1459cf655043SAndy Whitcroft
1460c2fdda0dSAndy Whitcroft			# Ignore goto labels.
1461171ae1a4SAndy Whitcroft			if ($s =~ /$Ident:\*$/s) {
1462c2fdda0dSAndy Whitcroft
1463c2fdda0dSAndy Whitcroft			# Ignore functions being called
1464171ae1a4SAndy Whitcroft			} elsif ($s =~ /^.\s*$Ident\s*\(/s) {
1465c2fdda0dSAndy Whitcroft
1466463f2864SAndy Whitcroft			} elsif ($s =~ /^.\s*else\b/s) {
1467463f2864SAndy Whitcroft
1468c45dcabdSAndy Whitcroft			# declarations always start with types
1469d2506586SAndy 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) {
1470c45dcabdSAndy Whitcroft				my $type = $1;
1471c45dcabdSAndy Whitcroft				$type =~ s/\s+/ /g;
1472c45dcabdSAndy Whitcroft				possible($type, "A:" . $s);
1473c45dcabdSAndy Whitcroft
14746c72ffaaSAndy Whitcroft			# definitions in global scope can only start with types
1475a6a84062SAndy Whitcroft			} elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
1476c45dcabdSAndy Whitcroft				possible($1, "B:" . $s);
1477c2fdda0dSAndy Whitcroft			}
14788905a67cSAndy Whitcroft
14796c72ffaaSAndy Whitcroft			# any (foo ... *) is a pointer cast, and foo is a type
148065863862SAndy Whitcroft			while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
1481c45dcabdSAndy Whitcroft				possible($1, "C:" . $s);
14829c0ca6f9SAndy Whitcroft			}
14838905a67cSAndy Whitcroft
14848905a67cSAndy Whitcroft			# Check for any sort of function declaration.
14858905a67cSAndy Whitcroft			# int foo(something bar, other baz);
14868905a67cSAndy Whitcroft			# void (*store_gdt)(x86_descr_ptr *);
1487171ae1a4SAndy Whitcroft			if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
14888905a67cSAndy Whitcroft				my ($name_len) = length($1);
14898905a67cSAndy Whitcroft
1490cf655043SAndy Whitcroft				my $ctx = $s;
1491773647a0SAndy Whitcroft				substr($ctx, 0, $name_len + 1, '');
14928905a67cSAndy Whitcroft				$ctx =~ s/\)[^\)]*$//;
1493cf655043SAndy Whitcroft
14948905a67cSAndy Whitcroft				for my $arg (split(/\s*,\s*/, $ctx)) {
1495c45dcabdSAndy Whitcroft					if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
14968905a67cSAndy Whitcroft
1497c45dcabdSAndy Whitcroft						possible($1, "D:" . $s);
14988905a67cSAndy Whitcroft					}
14998905a67cSAndy Whitcroft				}
15008905a67cSAndy Whitcroft			}
15018905a67cSAndy Whitcroft
15029c0ca6f9SAndy Whitcroft		}
15039c0ca6f9SAndy Whitcroft
150400df344fSAndy Whitcroft#
150500df344fSAndy Whitcroft# Checks which may be anchored in the context.
150600df344fSAndy Whitcroft#
150700df344fSAndy Whitcroft
150800df344fSAndy Whitcroft# Check for switch () and associated case and default
150900df344fSAndy Whitcroft# statements should be at the same indent.
151000df344fSAndy Whitcroft		if ($line=~/\bswitch\s*\(.*\)/) {
151100df344fSAndy Whitcroft			my $err = '';
151200df344fSAndy Whitcroft			my $sep = '';
151300df344fSAndy Whitcroft			my @ctx = ctx_block_outer($linenr, $realcnt);
151400df344fSAndy Whitcroft			shift(@ctx);
151500df344fSAndy Whitcroft			for my $ctx (@ctx) {
151600df344fSAndy Whitcroft				my ($clen, $cindent) = line_stats($ctx);
151700df344fSAndy Whitcroft				if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
151800df344fSAndy Whitcroft							$indent != $cindent) {
151900df344fSAndy Whitcroft					$err .= "$sep$ctx\n";
152000df344fSAndy Whitcroft					$sep = '';
152100df344fSAndy Whitcroft				} else {
152200df344fSAndy Whitcroft					$sep = "[...]\n";
152300df344fSAndy Whitcroft				}
152400df344fSAndy Whitcroft			}
152500df344fSAndy Whitcroft			if ($err ne '') {
15269c0ca6f9SAndy Whitcroft				ERROR("switch and case should be at the same indent\n$hereline$err");
1527de7d4f0eSAndy Whitcroft			}
1528de7d4f0eSAndy Whitcroft		}
1529de7d4f0eSAndy Whitcroft
1530de7d4f0eSAndy Whitcroft# if/while/etc brace do not go on next line, unless defining a do while loop,
1531de7d4f0eSAndy Whitcroft# or if that brace on the next line is for something else
1532c45dcabdSAndy Whitcroft		if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
1533773647a0SAndy Whitcroft			my $pre_ctx = "$1$2";
1534773647a0SAndy Whitcroft
15359c0ca6f9SAndy Whitcroft			my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
1536de7d4f0eSAndy Whitcroft			my $ctx_cnt = $realcnt - $#ctx - 1;
1537de7d4f0eSAndy Whitcroft			my $ctx = join("\n", @ctx);
1538de7d4f0eSAndy Whitcroft
1539548596d5SAndy Whitcroft			my $ctx_ln = $linenr;
1540548596d5SAndy Whitcroft			my $ctx_skip = $realcnt;
1541de7d4f0eSAndy Whitcroft
1542548596d5SAndy Whitcroft			while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
1543548596d5SAndy Whitcroft					defined $lines[$ctx_ln - 1] &&
1544548596d5SAndy Whitcroft					$lines[$ctx_ln - 1] =~ /^-/)) {
1545548596d5SAndy Whitcroft				##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
1546548596d5SAndy Whitcroft				$ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
1547773647a0SAndy Whitcroft				$ctx_ln++;
1548773647a0SAndy Whitcroft			}
1549548596d5SAndy Whitcroft
155053210168SAndy Whitcroft			#print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
155153210168SAndy Whitcroft			#print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
1552773647a0SAndy Whitcroft
1553773647a0SAndy Whitcroft			if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln -1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
1554773647a0SAndy Whitcroft				ERROR("that open brace { should be on the previous line\n" .
1555c45dcabdSAndy Whitcroft					"$here\n$ctx\n$lines[$ctx_ln - 1]\n");
155600df344fSAndy Whitcroft			}
1557773647a0SAndy Whitcroft			if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
1558773647a0SAndy Whitcroft			    $ctx =~ /\)\s*\;\s*$/ &&
1559773647a0SAndy Whitcroft			    defined $lines[$ctx_ln - 1])
1560773647a0SAndy Whitcroft			{
15619c0ca6f9SAndy Whitcroft				my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
15629c0ca6f9SAndy Whitcroft				if ($nindent > $indent) {
1563773647a0SAndy Whitcroft					WARN("trailing semicolon indicates no statements, indent implies otherwise\n" .
1564c45dcabdSAndy Whitcroft						"$here\n$ctx\n$lines[$ctx_ln - 1]\n");
15659c0ca6f9SAndy Whitcroft				}
15669c0ca6f9SAndy Whitcroft			}
156700df344fSAndy Whitcroft		}
156800df344fSAndy Whitcroft
15694d001e4dSAndy Whitcroft# Check relative indent for conditionals and blocks.
15704d001e4dSAndy Whitcroft		if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
15714d001e4dSAndy Whitcroft			my ($s, $c) = ($stat, $cond);
15724d001e4dSAndy Whitcroft
15734d001e4dSAndy Whitcroft			substr($s, 0, length($c), '');
15744d001e4dSAndy Whitcroft
15754d001e4dSAndy Whitcroft			# Make sure we remove the line prefixes as we have
15764d001e4dSAndy Whitcroft			# none on the first line, and are going to readd them
15774d001e4dSAndy Whitcroft			# where necessary.
15784d001e4dSAndy Whitcroft			$s =~ s/\n./\n/gs;
15794d001e4dSAndy Whitcroft
15804d001e4dSAndy Whitcroft			# Find out how long the conditional actually is.
15816f779c18SAndy Whitcroft			my @newlines = ($c =~ /\n/gs);
15826f779c18SAndy Whitcroft			my $cond_lines = 1 + $#newlines;
15834d001e4dSAndy Whitcroft
15844d001e4dSAndy Whitcroft			# We want to check the first line inside the block
15854d001e4dSAndy Whitcroft			# starting at the end of the conditional, so remove:
15864d001e4dSAndy Whitcroft			#  1) any blank line termination
15874d001e4dSAndy Whitcroft			#  2) any opening brace { on end of the line
15884d001e4dSAndy Whitcroft			#  3) any do (...) {
15894d001e4dSAndy Whitcroft			my $continuation = 0;
15904d001e4dSAndy Whitcroft			my $check = 0;
15914d001e4dSAndy Whitcroft			$s =~ s/^.*\bdo\b//;
15924d001e4dSAndy Whitcroft			$s =~ s/^\s*{//;
15934d001e4dSAndy Whitcroft			if ($s =~ s/^\s*\\//) {
15944d001e4dSAndy Whitcroft				$continuation = 1;
15954d001e4dSAndy Whitcroft			}
15969bd49efeSAndy Whitcroft			if ($s =~ s/^\s*?\n//) {
15974d001e4dSAndy Whitcroft				$check = 1;
15984d001e4dSAndy Whitcroft				$cond_lines++;
15994d001e4dSAndy Whitcroft			}
16004d001e4dSAndy Whitcroft
16014d001e4dSAndy Whitcroft			# Also ignore a loop construct at the end of a
16024d001e4dSAndy Whitcroft			# preprocessor statement.
16034d001e4dSAndy Whitcroft			if (($prevline =~ /^.\s*#\s*define\s/ ||
16044d001e4dSAndy Whitcroft			    $prevline =~ /\\\s*$/) && $continuation == 0) {
16054d001e4dSAndy Whitcroft				$check = 0;
16064d001e4dSAndy Whitcroft			}
16074d001e4dSAndy Whitcroft
16089bd49efeSAndy Whitcroft			my $cond_ptr = -1;
1609740504c6SAndy Whitcroft			$continuation = 0;
16109bd49efeSAndy Whitcroft			while ($cond_ptr != $cond_lines) {
16119bd49efeSAndy Whitcroft				$cond_ptr = $cond_lines;
16124d001e4dSAndy Whitcroft
1613f16fa28fSAndy Whitcroft				# If we see an #else/#elif then the code
1614f16fa28fSAndy Whitcroft				# is not linear.
1615f16fa28fSAndy Whitcroft				if ($s =~ /^\s*\#\s*(?:else|elif)/) {
1616f16fa28fSAndy Whitcroft					$check = 0;
1617f16fa28fSAndy Whitcroft				}
1618f16fa28fSAndy Whitcroft
16199bd49efeSAndy Whitcroft				# Ignore:
16209bd49efeSAndy Whitcroft				#  1) blank lines, they should be at 0,
16219bd49efeSAndy Whitcroft				#  2) preprocessor lines, and
16229bd49efeSAndy Whitcroft				#  3) labels.
1623740504c6SAndy Whitcroft				if ($continuation ||
1624740504c6SAndy Whitcroft				    $s =~ /^\s*?\n/ ||
16259bd49efeSAndy Whitcroft				    $s =~ /^\s*#\s*?/ ||
16269bd49efeSAndy Whitcroft				    $s =~ /^\s*$Ident\s*:/) {
1627740504c6SAndy Whitcroft					$continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
162830dad6ebSAndy Whitcroft					if ($s =~ s/^.*?\n//) {
16299bd49efeSAndy Whitcroft						$cond_lines++;
16309bd49efeSAndy Whitcroft					}
16314d001e4dSAndy Whitcroft				}
163230dad6ebSAndy Whitcroft			}
16334d001e4dSAndy Whitcroft
16344d001e4dSAndy Whitcroft			my (undef, $sindent) = line_stats("+" . $s);
16354d001e4dSAndy Whitcroft			my $stat_real = raw_line($linenr, $cond_lines);
16364d001e4dSAndy Whitcroft
16374d001e4dSAndy Whitcroft			# Check if either of these lines are modified, else
16384d001e4dSAndy Whitcroft			# this is not this patch's fault.
16394d001e4dSAndy Whitcroft			if (!defined($stat_real) ||
16404d001e4dSAndy Whitcroft			    $stat !~ /^\+/ && $stat_real !~ /^\+/) {
16414d001e4dSAndy Whitcroft				$check = 0;
16424d001e4dSAndy Whitcroft			}
16434d001e4dSAndy Whitcroft			if (defined($stat_real) && $cond_lines > 1) {
16444d001e4dSAndy Whitcroft				$stat_real = "[...]\n$stat_real";
16454d001e4dSAndy Whitcroft			}
16464d001e4dSAndy Whitcroft
16479bd49efeSAndy 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";
16484d001e4dSAndy Whitcroft
16494d001e4dSAndy Whitcroft			if ($check && (($sindent % 8) != 0 ||
16504d001e4dSAndy Whitcroft			    ($sindent <= $indent && $s ne ''))) {
16514d001e4dSAndy Whitcroft				WARN("suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
16524d001e4dSAndy Whitcroft			}
16534d001e4dSAndy Whitcroft		}
16544d001e4dSAndy Whitcroft
16556c72ffaaSAndy Whitcroft		# Track the 'values' across context and added lines.
16566c72ffaaSAndy Whitcroft		my $opline = $line; $opline =~ s/^./ /;
16571f65f947SAndy Whitcroft		my ($curr_values, $curr_vars) =
16581f65f947SAndy Whitcroft				annotate_values($opline . "\n", $prev_values);
16596c72ffaaSAndy Whitcroft		$curr_values = $prev_values . $curr_values;
1660c2fdda0dSAndy Whitcroft		if ($dbg_values) {
1661c2fdda0dSAndy Whitcroft			my $outline = $opline; $outline =~ s/\t/ /g;
1662cf655043SAndy Whitcroft			print "$linenr > .$outline\n";
1663cf655043SAndy Whitcroft			print "$linenr > $curr_values\n";
16641f65f947SAndy Whitcroft			print "$linenr >  $curr_vars\n";
1665c2fdda0dSAndy Whitcroft		}
16666c72ffaaSAndy Whitcroft		$prev_values = substr($curr_values, -1);
16676c72ffaaSAndy Whitcroft
166800df344fSAndy Whitcroft#ignore lines not being added
166900df344fSAndy Whitcroft		if ($line=~/^[^\+]/) {next;}
167000df344fSAndy Whitcroft
1671653d4876SAndy Whitcroft# TEST: allow direct testing of the type matcher.
16727429c690SAndy Whitcroft		if ($dbg_type) {
16737429c690SAndy Whitcroft			if ($line =~ /^.\s*$Declare\s*$/) {
16747429c690SAndy Whitcroft				ERROR("TEST: is type\n" . $herecurr);
16757429c690SAndy Whitcroft			} elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
16767429c690SAndy Whitcroft				ERROR("TEST: is not type ($1 is)\n". $herecurr);
16777429c690SAndy Whitcroft			}
1678653d4876SAndy Whitcroft			next;
1679653d4876SAndy Whitcroft		}
1680a1ef277eSAndy Whitcroft# TEST: allow direct testing of the attribute matcher.
1681a1ef277eSAndy Whitcroft		if ($dbg_attr) {
16829360b0e5SAndy Whitcroft			if ($line =~ /^.\s*$Modifier\s*$/) {
1683a1ef277eSAndy Whitcroft				ERROR("TEST: is attr\n" . $herecurr);
16849360b0e5SAndy Whitcroft			} elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
1685a1ef277eSAndy Whitcroft				ERROR("TEST: is not attr ($1 is)\n". $herecurr);
1686a1ef277eSAndy Whitcroft			}
1687a1ef277eSAndy Whitcroft			next;
1688a1ef277eSAndy Whitcroft		}
1689653d4876SAndy Whitcroft
1690f0a594c1SAndy Whitcroft# check for initialisation to aggregates open brace on the next line
169199423c20SAndy Whitcroft		if ($line =~ /^.\s*{/ &&
169299423c20SAndy Whitcroft		    $prevline =~ /(?:^|[^=])=\s*$/) {
1693773647a0SAndy Whitcroft			ERROR("that open brace { should be on the previous line\n" . $hereprev);
1694f0a594c1SAndy Whitcroft		}
1695f0a594c1SAndy Whitcroft
169600df344fSAndy Whitcroft#
169700df344fSAndy Whitcroft# Checks which are anchored on the added line.
169800df344fSAndy Whitcroft#
169900df344fSAndy Whitcroft
1700653d4876SAndy Whitcroft# check for malformed paths in #include statements (uses RAW line)
1701c45dcabdSAndy Whitcroft		if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
1702653d4876SAndy Whitcroft			my $path = $1;
1703653d4876SAndy Whitcroft			if ($path =~ m{//}) {
1704de7d4f0eSAndy Whitcroft				ERROR("malformed #include filename\n" .
1705de7d4f0eSAndy Whitcroft					$herecurr);
1706653d4876SAndy Whitcroft			}
1707653d4876SAndy Whitcroft		}
1708653d4876SAndy Whitcroft
170900df344fSAndy Whitcroft# no C99 // comments
171000df344fSAndy Whitcroft		if ($line =~ m{//}) {
1711de7d4f0eSAndy Whitcroft			ERROR("do not use C99 // comments\n" . $herecurr);
171200df344fSAndy Whitcroft		}
171300df344fSAndy Whitcroft		# Remove C99 comments.
17140a920b5bSAndy Whitcroft		$line =~ s@//.*@@;
17156c72ffaaSAndy Whitcroft		$opline =~ s@//.*@@;
17160a920b5bSAndy Whitcroft
17172b474a1aSAndy Whitcroft# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
17182b474a1aSAndy Whitcroft# the whole statement.
17192b474a1aSAndy Whitcroft#print "APW <$lines[$realline_next - 1]>\n";
17202b474a1aSAndy Whitcroft		if (defined $realline_next &&
17212b474a1aSAndy Whitcroft		    exists $lines[$realline_next - 1] &&
17222b474a1aSAndy Whitcroft		    !defined $suppress_export{$realline_next} &&
17232b474a1aSAndy Whitcroft		    ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
17242b474a1aSAndy Whitcroft		     $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
1725653d4876SAndy Whitcroft			my $name = $1;
17262b474a1aSAndy Whitcroft			if ($stat !~ /(?:
17272b474a1aSAndy Whitcroft				\n.}\s*$|
172848012058SAndy Whitcroft				^.DEFINE_$Ident\(\Q$name\E\)|
172948012058SAndy Whitcroft				^.DECLARE_$Ident\(\Q$name\E\)|
173048012058SAndy Whitcroft				^.LIST_HEAD\(\Q$name\E\)|
17312b474a1aSAndy Whitcroft				^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
17322b474a1aSAndy Whitcroft				\b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
173348012058SAndy Whitcroft			    )/x) {
17342b474a1aSAndy Whitcroft#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
17352b474a1aSAndy Whitcroft				$suppress_export{$realline_next} = 2;
17362b474a1aSAndy Whitcroft			} else {
17372b474a1aSAndy Whitcroft				$suppress_export{$realline_next} = 1;
17380a920b5bSAndy Whitcroft			}
17390a920b5bSAndy Whitcroft		}
17402b474a1aSAndy Whitcroft		if (!defined $suppress_export{$linenr} &&
17412b474a1aSAndy Whitcroft		    $prevline =~ /^.\s*$/ &&
17422b474a1aSAndy Whitcroft		    ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
17432b474a1aSAndy Whitcroft		     $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
17442b474a1aSAndy Whitcroft#print "FOO B <$lines[$linenr - 1]>\n";
17452b474a1aSAndy Whitcroft			$suppress_export{$linenr} = 2;
17462b474a1aSAndy Whitcroft		}
17472b474a1aSAndy Whitcroft		if (defined $suppress_export{$linenr} &&
17482b474a1aSAndy Whitcroft		    $suppress_export{$linenr} == 2) {
17492b474a1aSAndy Whitcroft			WARN("EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
17502b474a1aSAndy Whitcroft		}
17510a920b5bSAndy Whitcroft
1752f0a594c1SAndy Whitcroft# check for external initialisers.
1753c45dcabdSAndy Whitcroft		if ($line =~ /^.$Type\s*$Ident\s*(?:\s+$Modifier)*\s*=\s*(0|NULL|false)\s*;/) {
1754f0a594c1SAndy Whitcroft			ERROR("do not initialise externals to 0 or NULL\n" .
1755f0a594c1SAndy Whitcroft				$herecurr);
1756f0a594c1SAndy Whitcroft		}
17570a920b5bSAndy Whitcroft# check for static initialisers.
17582d1bafd7SAndy Whitcroft		if ($line =~ /\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
1759de7d4f0eSAndy Whitcroft			ERROR("do not initialise statics to 0 or NULL\n" .
1760de7d4f0eSAndy Whitcroft				$herecurr);
17610a920b5bSAndy Whitcroft		}
17620a920b5bSAndy Whitcroft
1763653d4876SAndy Whitcroft# check for new typedefs, only function parameters and sparse annotations
1764653d4876SAndy Whitcroft# make sense.
1765653d4876SAndy Whitcroft		if ($line =~ /\btypedef\s/ &&
17668054576dSAndy Whitcroft		    $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
1767c45dcabdSAndy Whitcroft		    $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
17688ed22cadSAndy Whitcroft		    $line !~ /\b$typeTypedefs\b/ &&
1769653d4876SAndy Whitcroft		    $line !~ /\b__bitwise(?:__|)\b/) {
1770de7d4f0eSAndy Whitcroft			WARN("do not add new typedefs\n" . $herecurr);
17710a920b5bSAndy Whitcroft		}
17720a920b5bSAndy Whitcroft
17730a920b5bSAndy Whitcroft# * goes on variable not on type
177465863862SAndy Whitcroft		# (char*[ const])
177500ef4eceSAndy Whitcroft		if ($line =~ m{\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\)}) {
177665863862SAndy Whitcroft			my ($from, $to) = ($1, $1);
1777d8aaf121SAndy Whitcroft
177865863862SAndy Whitcroft			# Should start with a space.
177965863862SAndy Whitcroft			$to =~ s/^(\S)/ $1/;
178065863862SAndy Whitcroft			# Should not end with a space.
178165863862SAndy Whitcroft			$to =~ s/\s+$//;
178265863862SAndy Whitcroft			# '*'s should not have spaces between.
1783f9a0b3d1SAndy Whitcroft			while ($to =~ s/\*\s+\*/\*\*/) {
178465863862SAndy Whitcroft			}
1785d8aaf121SAndy Whitcroft
178665863862SAndy Whitcroft			#print "from<$from> to<$to>\n";
178765863862SAndy Whitcroft			if ($from ne $to) {
178865863862SAndy Whitcroft				ERROR("\"(foo$from)\" should be \"(foo$to)\"\n" .  $herecurr);
178965863862SAndy Whitcroft			}
179000ef4eceSAndy Whitcroft		} elsif ($line =~ m{\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident)}) {
179165863862SAndy Whitcroft			my ($from, $to, $ident) = ($1, $1, $2);
1792d8aaf121SAndy Whitcroft
179365863862SAndy Whitcroft			# Should start with a space.
179465863862SAndy Whitcroft			$to =~ s/^(\S)/ $1/;
179565863862SAndy Whitcroft			# Should not end with a space.
179665863862SAndy Whitcroft			$to =~ s/\s+$//;
179765863862SAndy Whitcroft			# '*'s should not have spaces between.
1798f9a0b3d1SAndy Whitcroft			while ($to =~ s/\*\s+\*/\*\*/) {
179965863862SAndy Whitcroft			}
180065863862SAndy Whitcroft			# Modifiers should have spaces.
180165863862SAndy Whitcroft			$to =~ s/(\b$Modifier$)/$1 /;
180265863862SAndy Whitcroft
1803667026e7SAndy Whitcroft			#print "from<$from> to<$to> ident<$ident>\n";
1804667026e7SAndy Whitcroft			if ($from ne $to && $ident !~ /^$Modifier$/) {
180565863862SAndy Whitcroft				ERROR("\"foo${from}bar\" should be \"foo${to}bar\"\n" .  $herecurr);
180665863862SAndy Whitcroft			}
18070a920b5bSAndy Whitcroft		}
18080a920b5bSAndy Whitcroft
18090a920b5bSAndy Whitcroft# # no BUG() or BUG_ON()
18100a920b5bSAndy Whitcroft# 		if ($line =~ /\b(BUG|BUG_ON)\b/) {
18110a920b5bSAndy Whitcroft# 			print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
18120a920b5bSAndy Whitcroft# 			print "$herecurr";
18130a920b5bSAndy Whitcroft# 			$clean = 0;
18140a920b5bSAndy Whitcroft# 		}
18150a920b5bSAndy Whitcroft
18168905a67cSAndy Whitcroft		if ($line =~ /\bLINUX_VERSION_CODE\b/) {
1817c2fdda0dSAndy Whitcroft			WARN("LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
18188905a67cSAndy Whitcroft		}
18198905a67cSAndy Whitcroft
182000df344fSAndy Whitcroft# printk should use KERN_* levels.  Note that follow on printk's on the
182100df344fSAndy Whitcroft# same line do not need a level, so we use the current block context
182200df344fSAndy Whitcroft# to try and find and validate the current printk.  In summary the current
182300df344fSAndy Whitcroft# printk includes all preceeding printk's which have no newline on the end.
182400df344fSAndy Whitcroft# we assume the first bad printk is the one to report.
1825f0a594c1SAndy Whitcroft		if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
182600df344fSAndy Whitcroft			my $ok = 0;
182700df344fSAndy Whitcroft			for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
182800df344fSAndy Whitcroft				#print "CHECK<$lines[$ln - 1]\n";
182900df344fSAndy Whitcroft				# we have a preceeding printk if it ends
183000df344fSAndy Whitcroft				# with "\n" ignore it, else it is to blame
183100df344fSAndy Whitcroft				if ($lines[$ln - 1] =~ m{\bprintk\(}) {
183200df344fSAndy Whitcroft					if ($rawlines[$ln - 1] !~ m{\\n"}) {
183300df344fSAndy Whitcroft						$ok = 1;
183400df344fSAndy Whitcroft					}
183500df344fSAndy Whitcroft					last;
183600df344fSAndy Whitcroft				}
183700df344fSAndy Whitcroft			}
183800df344fSAndy Whitcroft			if ($ok == 0) {
1839de7d4f0eSAndy Whitcroft				WARN("printk() should include KERN_ facility level\n" . $herecurr);
18400a920b5bSAndy Whitcroft			}
184100df344fSAndy Whitcroft		}
18420a920b5bSAndy Whitcroft
1843653d4876SAndy Whitcroft# function brace can't be on same line, except for #defines of do while,
1844653d4876SAndy Whitcroft# or if closed on same line
1845c45dcabdSAndy Whitcroft		if (($line=~/$Type\s*$Ident\(.*\).*\s{/) and
1846c45dcabdSAndy Whitcroft		    !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) {
1847de7d4f0eSAndy Whitcroft			ERROR("open brace '{' following function declarations go on the next line\n" . $herecurr);
18480a920b5bSAndy Whitcroft		}
1849653d4876SAndy Whitcroft
18508905a67cSAndy Whitcroft# open braces for enum, union and struct go on the same line.
18518905a67cSAndy Whitcroft		if ($line =~ /^.\s*{/ &&
18528905a67cSAndy Whitcroft		    $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
18538905a67cSAndy Whitcroft			ERROR("open brace '{' following $1 go on the same line\n" . $hereprev);
18548905a67cSAndy Whitcroft		}
18558905a67cSAndy Whitcroft
18568d31cfceSAndy Whitcroft# check for spacing round square brackets; allowed:
18578d31cfceSAndy Whitcroft#  1. with a type on the left -- int [] a;
1858fe2a7dbcSAndy Whitcroft#  2. at the beginning of a line for slice initialisers -- [0...10] = 5,
1859fe2a7dbcSAndy Whitcroft#  3. inside a curly brace -- = { [0...10] = 5 }
18608d31cfceSAndy Whitcroft		while ($line =~ /(.*?\s)\[/g) {
18618d31cfceSAndy Whitcroft			my ($where, $prefix) = ($-[1], $1);
18628d31cfceSAndy Whitcroft			if ($prefix !~ /$Type\s+$/ &&
1863fe2a7dbcSAndy Whitcroft			    ($where != 0 || $prefix !~ /^.\s+$/) &&
1864fe2a7dbcSAndy Whitcroft			    $prefix !~ /{\s+$/) {
18658d31cfceSAndy Whitcroft				ERROR("space prohibited before open square bracket '['\n" . $herecurr);
18668d31cfceSAndy Whitcroft			}
18678d31cfceSAndy Whitcroft		}
18688d31cfceSAndy Whitcroft
1869f0a594c1SAndy Whitcroft# check for spaces between functions and their parentheses.
18706c72ffaaSAndy Whitcroft		while ($line =~ /($Ident)\s+\(/g) {
1871c2fdda0dSAndy Whitcroft			my $name = $1;
1872773647a0SAndy Whitcroft			my $ctx_before = substr($line, 0, $-[1]);
1873773647a0SAndy Whitcroft			my $ctx = "$ctx_before$name";
1874c2fdda0dSAndy Whitcroft
1875c2fdda0dSAndy Whitcroft			# Ignore those directives where spaces _are_ permitted.
1876773647a0SAndy Whitcroft			if ($name =~ /^(?:
1877773647a0SAndy Whitcroft				if|for|while|switch|return|case|
1878773647a0SAndy Whitcroft				volatile|__volatile__|
1879773647a0SAndy Whitcroft				__attribute__|format|__extension__|
1880773647a0SAndy Whitcroft				asm|__asm__)$/x)
1881773647a0SAndy Whitcroft			{
1882c2fdda0dSAndy Whitcroft
1883c2fdda0dSAndy Whitcroft			# cpp #define statements have non-optional spaces, ie
1884c2fdda0dSAndy Whitcroft			# if there is a space between the name and the open
1885c2fdda0dSAndy Whitcroft			# parenthesis it is simply not a parameter group.
1886c45dcabdSAndy Whitcroft			} elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
1887773647a0SAndy Whitcroft
1888773647a0SAndy Whitcroft			# cpp #elif statement condition may start with a (
1889c45dcabdSAndy Whitcroft			} elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
1890c2fdda0dSAndy Whitcroft
1891c2fdda0dSAndy Whitcroft			# If this whole things ends with a type its most
1892c2fdda0dSAndy Whitcroft			# likely a typedef for a function.
1893773647a0SAndy Whitcroft			} elsif ($ctx =~ /$Type$/) {
1894c2fdda0dSAndy Whitcroft
1895c2fdda0dSAndy Whitcroft			} else {
1896773647a0SAndy Whitcroft				WARN("space prohibited between function name and open parenthesis '('\n" . $herecurr);
1897f0a594c1SAndy Whitcroft			}
18986c72ffaaSAndy Whitcroft		}
1899653d4876SAndy Whitcroft# Check operator spacing.
19000a920b5bSAndy Whitcroft		if (!($line=~/\#\s*include/)) {
19019c0ca6f9SAndy Whitcroft			my $ops = qr{
19029c0ca6f9SAndy Whitcroft				<<=|>>=|<=|>=|==|!=|
19039c0ca6f9SAndy Whitcroft				\+=|-=|\*=|\/=|%=|\^=|\|=|&=|
19049c0ca6f9SAndy Whitcroft				=>|->|<<|>>|<|>|=|!|~|
19051f65f947SAndy Whitcroft				&&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
19061f65f947SAndy Whitcroft				\?|:
19079c0ca6f9SAndy Whitcroft			}x;
1908cf655043SAndy Whitcroft			my @elements = split(/($ops|;)/, $opline);
190900df344fSAndy Whitcroft			my $off = 0;
19106c72ffaaSAndy Whitcroft
19116c72ffaaSAndy Whitcroft			my $blank = copy_spacing($opline);
19126c72ffaaSAndy Whitcroft
19130a920b5bSAndy Whitcroft			for (my $n = 0; $n < $#elements; $n += 2) {
19144a0df2efSAndy Whitcroft				$off += length($elements[$n]);
19154a0df2efSAndy Whitcroft
1916773647a0SAndy Whitcroft				# Pick up the preceeding and succeeding characters.
1917773647a0SAndy Whitcroft				my $ca = substr($opline, 0, $off);
1918773647a0SAndy Whitcroft				my $cc = '';
1919773647a0SAndy Whitcroft				if (length($opline) >= ($off + length($elements[$n + 1]))) {
1920773647a0SAndy Whitcroft					$cc = substr($opline, $off + length($elements[$n + 1]));
1921773647a0SAndy Whitcroft				}
1922773647a0SAndy Whitcroft				my $cb = "$ca$;$cc";
1923773647a0SAndy Whitcroft
19244a0df2efSAndy Whitcroft				my $a = '';
19254a0df2efSAndy Whitcroft				$a = 'V' if ($elements[$n] ne '');
19264a0df2efSAndy Whitcroft				$a = 'W' if ($elements[$n] =~ /\s$/);
1927cf655043SAndy Whitcroft				$a = 'C' if ($elements[$n] =~ /$;$/);
19284a0df2efSAndy Whitcroft				$a = 'B' if ($elements[$n] =~ /(\[|\()$/);
19294a0df2efSAndy Whitcroft				$a = 'O' if ($elements[$n] eq '');
1930773647a0SAndy Whitcroft				$a = 'E' if ($ca =~ /^\s*$/);
19314a0df2efSAndy Whitcroft
19320a920b5bSAndy Whitcroft				my $op = $elements[$n + 1];
19334a0df2efSAndy Whitcroft
19344a0df2efSAndy Whitcroft				my $c = '';
19350a920b5bSAndy Whitcroft				if (defined $elements[$n + 2]) {
19364a0df2efSAndy Whitcroft					$c = 'V' if ($elements[$n + 2] ne '');
19374a0df2efSAndy Whitcroft					$c = 'W' if ($elements[$n + 2] =~ /^\s/);
1938cf655043SAndy Whitcroft					$c = 'C' if ($elements[$n + 2] =~ /^$;/);
19394a0df2efSAndy Whitcroft					$c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
19404a0df2efSAndy Whitcroft					$c = 'O' if ($elements[$n + 2] eq '');
19418b1b3378SAndy Whitcroft					$c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
19424a0df2efSAndy Whitcroft				} else {
19434a0df2efSAndy Whitcroft					$c = 'E';
19440a920b5bSAndy Whitcroft				}
19450a920b5bSAndy Whitcroft
19464a0df2efSAndy Whitcroft				my $ctx = "${a}x${c}";
19474a0df2efSAndy Whitcroft
19484a0df2efSAndy Whitcroft				my $at = "(ctx:$ctx)";
19494a0df2efSAndy Whitcroft
19506c72ffaaSAndy Whitcroft				my $ptr = substr($blank, 0, $off) . "^";
1951de7d4f0eSAndy Whitcroft				my $hereptr = "$hereline$ptr\n";
19520a920b5bSAndy Whitcroft
195374048ed8SAndy Whitcroft				# Pull out the value of this operator.
19546c72ffaaSAndy Whitcroft				my $op_type = substr($curr_values, $off + 1, 1);
19550a920b5bSAndy Whitcroft
19561f65f947SAndy Whitcroft				# Get the full operator variant.
19571f65f947SAndy Whitcroft				my $opv = $op . substr($curr_vars, $off, 1);
19581f65f947SAndy Whitcroft
195913214adfSAndy Whitcroft				# Ignore operators passed as parameters.
196013214adfSAndy Whitcroft				if ($op_type ne 'V' &&
196113214adfSAndy Whitcroft				    $ca =~ /\s$/ && $cc =~ /^\s*,/) {
196213214adfSAndy Whitcroft
1963cf655043SAndy Whitcroft#				# Ignore comments
1964cf655043SAndy Whitcroft#				} elsif ($op =~ /^$;+$/) {
196513214adfSAndy Whitcroft
1966d8aaf121SAndy Whitcroft				# ; should have either the end of line or a space or \ after it
196713214adfSAndy Whitcroft				} elsif ($op eq ';') {
1968cf655043SAndy Whitcroft					if ($ctx !~ /.x[WEBC]/ &&
1969cf655043SAndy Whitcroft					    $cc !~ /^\\/ && $cc !~ /^;/) {
1970773647a0SAndy Whitcroft						ERROR("space required after that '$op' $at\n" . $hereptr);
1971d8aaf121SAndy Whitcroft					}
1972d8aaf121SAndy Whitcroft
1973d8aaf121SAndy Whitcroft				# // is a comment
1974d8aaf121SAndy Whitcroft				} elsif ($op eq '//') {
19750a920b5bSAndy Whitcroft
19761f65f947SAndy Whitcroft				# No spaces for:
19771f65f947SAndy Whitcroft				#   ->
19781f65f947SAndy Whitcroft				#   :   when part of a bitfield
19791f65f947SAndy Whitcroft				} elsif ($op eq '->' || $opv eq ':B') {
19804a0df2efSAndy Whitcroft					if ($ctx =~ /Wx.|.xW/) {
1981773647a0SAndy Whitcroft						ERROR("spaces prohibited around that '$op' $at\n" . $hereptr);
19820a920b5bSAndy Whitcroft					}
19830a920b5bSAndy Whitcroft
19840a920b5bSAndy Whitcroft				# , must have a space on the right.
19850a920b5bSAndy Whitcroft				} elsif ($op eq ',') {
1986cf655043SAndy Whitcroft					if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
1987773647a0SAndy Whitcroft						ERROR("space required after that '$op' $at\n" . $hereptr);
19880a920b5bSAndy Whitcroft					}
19890a920b5bSAndy Whitcroft
19909c0ca6f9SAndy Whitcroft				# '*' as part of a type definition -- reported already.
199174048ed8SAndy Whitcroft				} elsif ($opv eq '*_') {
19929c0ca6f9SAndy Whitcroft					#warn "'*' is part of type\n";
19939c0ca6f9SAndy Whitcroft
19949c0ca6f9SAndy Whitcroft				# unary operators should have a space before and
19959c0ca6f9SAndy Whitcroft				# none after.  May be left adjacent to another
19969c0ca6f9SAndy Whitcroft				# unary operator, or a cast
19979c0ca6f9SAndy Whitcroft				} elsif ($op eq '!' || $op eq '~' ||
199874048ed8SAndy Whitcroft					 $opv eq '*U' || $opv eq '-U' ||
19990d413866SAndy Whitcroft					 $opv eq '&U' || $opv eq '&&U') {
2000cf655043SAndy Whitcroft					if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
2001773647a0SAndy Whitcroft						ERROR("space required before that '$op' $at\n" . $hereptr);
20020a920b5bSAndy Whitcroft					}
2003a3340b35SAndy Whitcroft					if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
2004171ae1a4SAndy Whitcroft						# A unary '*' may be const
2005171ae1a4SAndy Whitcroft
2006171ae1a4SAndy Whitcroft					} elsif ($ctx =~ /.xW/) {
2007fb9e9096SAndy Whitcroft						ERROR("space prohibited after that '$op' $at\n" . $hereptr);
20080a920b5bSAndy Whitcroft					}
20090a920b5bSAndy Whitcroft
20100a920b5bSAndy Whitcroft				# unary ++ and unary -- are allowed no space on one side.
20110a920b5bSAndy Whitcroft				} elsif ($op eq '++' or $op eq '--') {
2012773647a0SAndy Whitcroft					if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
2013773647a0SAndy Whitcroft						ERROR("space required one side of that '$op' $at\n" . $hereptr);
20140a920b5bSAndy Whitcroft					}
2015773647a0SAndy Whitcroft					if ($ctx =~ /Wx[BE]/ ||
2016773647a0SAndy Whitcroft					    ($ctx =~ /Wx./ && $cc =~ /^;/)) {
2017773647a0SAndy Whitcroft						ERROR("space prohibited before that '$op' $at\n" . $hereptr);
2018653d4876SAndy Whitcroft					}
2019773647a0SAndy Whitcroft					if ($ctx =~ /ExW/) {
2020773647a0SAndy Whitcroft						ERROR("space prohibited after that '$op' $at\n" . $hereptr);
2021773647a0SAndy Whitcroft					}
2022773647a0SAndy Whitcroft
20230a920b5bSAndy Whitcroft
20240a920b5bSAndy Whitcroft				# << and >> may either have or not have spaces both sides
20259c0ca6f9SAndy Whitcroft				} elsif ($op eq '<<' or $op eq '>>' or
20269c0ca6f9SAndy Whitcroft					 $op eq '&' or $op eq '^' or $op eq '|' or
20279c0ca6f9SAndy Whitcroft					 $op eq '+' or $op eq '-' or
2028c2fdda0dSAndy Whitcroft					 $op eq '*' or $op eq '/' or
2029c2fdda0dSAndy Whitcroft					 $op eq '%')
20300a920b5bSAndy Whitcroft				{
2031773647a0SAndy Whitcroft					if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
2032de7d4f0eSAndy Whitcroft						ERROR("need consistent spacing around '$op' $at\n" .
2033de7d4f0eSAndy Whitcroft							$hereptr);
20340a920b5bSAndy Whitcroft					}
20350a920b5bSAndy Whitcroft
20361f65f947SAndy Whitcroft				# A colon needs no spaces before when it is
20371f65f947SAndy Whitcroft				# terminating a case value or a label.
20381f65f947SAndy Whitcroft				} elsif ($opv eq ':C' || $opv eq ':L') {
20391f65f947SAndy Whitcroft					if ($ctx =~ /Wx./) {
20401f65f947SAndy Whitcroft						ERROR("space prohibited before that '$op' $at\n" . $hereptr);
20411f65f947SAndy Whitcroft					}
20421f65f947SAndy Whitcroft
20430a920b5bSAndy Whitcroft				# All the others need spaces both sides.
2044cf655043SAndy Whitcroft				} elsif ($ctx !~ /[EWC]x[CWE]/) {
20451f65f947SAndy Whitcroft					my $ok = 0;
20461f65f947SAndy Whitcroft
204722f2a2efSAndy Whitcroft					# Ignore email addresses <foo@bar>
20481f65f947SAndy Whitcroft					if (($op eq '<' &&
20491f65f947SAndy Whitcroft					     $cc =~ /^\S+\@\S+>/) ||
20501f65f947SAndy Whitcroft					    ($op eq '>' &&
20511f65f947SAndy Whitcroft					     $ca =~ /<\S+\@\S+$/))
20521f65f947SAndy Whitcroft					{
20531f65f947SAndy Whitcroft					    	$ok = 1;
20541f65f947SAndy Whitcroft					}
20551f65f947SAndy Whitcroft
20561f65f947SAndy Whitcroft					# Ignore ?:
20571f65f947SAndy Whitcroft					if (($opv eq ':O' && $ca =~ /\?$/) ||
20581f65f947SAndy Whitcroft					    ($op eq '?' && $cc =~ /^:/)) {
20591f65f947SAndy Whitcroft					    	$ok = 1;
20601f65f947SAndy Whitcroft					}
20611f65f947SAndy Whitcroft
20621f65f947SAndy Whitcroft					if ($ok == 0) {
2063773647a0SAndy Whitcroft						ERROR("spaces required around that '$op' $at\n" . $hereptr);
20640a920b5bSAndy Whitcroft					}
206522f2a2efSAndy Whitcroft				}
20664a0df2efSAndy Whitcroft				$off += length($elements[$n + 1]);
20670a920b5bSAndy Whitcroft			}
20680a920b5bSAndy Whitcroft		}
20690a920b5bSAndy Whitcroft
2070f0a594c1SAndy Whitcroft# check for multiple assignments
2071f0a594c1SAndy Whitcroft		if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
20726c72ffaaSAndy Whitcroft			CHK("multiple assignments should be avoided\n" . $herecurr);
2073f0a594c1SAndy Whitcroft		}
2074f0a594c1SAndy Whitcroft
207522f2a2efSAndy Whitcroft## # check for multiple declarations, allowing for a function declaration
207622f2a2efSAndy Whitcroft## # continuation.
207722f2a2efSAndy Whitcroft## 		if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
207822f2a2efSAndy Whitcroft## 		    $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
207922f2a2efSAndy Whitcroft##
208022f2a2efSAndy Whitcroft## 			# Remove any bracketed sections to ensure we do not
208122f2a2efSAndy Whitcroft## 			# falsly report the parameters of functions.
208222f2a2efSAndy Whitcroft## 			my $ln = $line;
208322f2a2efSAndy Whitcroft## 			while ($ln =~ s/\([^\(\)]*\)//g) {
208422f2a2efSAndy Whitcroft## 			}
208522f2a2efSAndy Whitcroft## 			if ($ln =~ /,/) {
208622f2a2efSAndy Whitcroft## 				WARN("declaring multiple variables together should be avoided\n" . $herecurr);
208722f2a2efSAndy Whitcroft## 			}
208822f2a2efSAndy Whitcroft## 		}
2089f0a594c1SAndy Whitcroft
20900a920b5bSAndy Whitcroft#need space before brace following if, while, etc
209122f2a2efSAndy Whitcroft		if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
209222f2a2efSAndy Whitcroft		    $line =~ /do{/) {
2093773647a0SAndy Whitcroft			ERROR("space required before the open brace '{'\n" . $herecurr);
2094de7d4f0eSAndy Whitcroft		}
2095de7d4f0eSAndy Whitcroft
2096de7d4f0eSAndy Whitcroft# closing brace should have a space following it when it has anything
2097de7d4f0eSAndy Whitcroft# on the line
2098de7d4f0eSAndy Whitcroft		if ($line =~ /}(?!(?:,|;|\)))\S/) {
2099773647a0SAndy Whitcroft			ERROR("space required after that close brace '}'\n" . $herecurr);
21000a920b5bSAndy Whitcroft		}
21010a920b5bSAndy Whitcroft
210222f2a2efSAndy Whitcroft# check spacing on square brackets
210322f2a2efSAndy Whitcroft		if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
2104773647a0SAndy Whitcroft			ERROR("space prohibited after that open square bracket '['\n" . $herecurr);
210522f2a2efSAndy Whitcroft		}
210622f2a2efSAndy Whitcroft		if ($line =~ /\s\]/) {
2107773647a0SAndy Whitcroft			ERROR("space prohibited before that close square bracket ']'\n" . $herecurr);
210822f2a2efSAndy Whitcroft		}
210922f2a2efSAndy Whitcroft
2110c45dcabdSAndy Whitcroft# check spacing on parentheses
21119c0ca6f9SAndy Whitcroft		if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
21129c0ca6f9SAndy Whitcroft		    $line !~ /for\s*\(\s+;/) {
2113773647a0SAndy Whitcroft			ERROR("space prohibited after that open parenthesis '('\n" . $herecurr);
211422f2a2efSAndy Whitcroft		}
211513214adfSAndy Whitcroft		if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
2116c45dcabdSAndy Whitcroft		    $line !~ /for\s*\(.*;\s+\)/ &&
2117c45dcabdSAndy Whitcroft		    $line !~ /:\s+\)/) {
2118773647a0SAndy Whitcroft			ERROR("space prohibited before that close parenthesis ')'\n" . $herecurr);
211922f2a2efSAndy Whitcroft		}
212022f2a2efSAndy Whitcroft
21210a920b5bSAndy Whitcroft#goto labels aren't indented, allow a single space however
21224a0df2efSAndy Whitcroft		if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
21230a920b5bSAndy Whitcroft		   !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
2124de7d4f0eSAndy Whitcroft			WARN("labels should not be indented\n" . $herecurr);
21250a920b5bSAndy Whitcroft		}
21260a920b5bSAndy Whitcroft
2127c45dcabdSAndy Whitcroft# Return is not a function.
2128c45dcabdSAndy Whitcroft		if (defined($stat) && $stat =~ /^.\s*return(\s*)(\(.*);/s) {
2129c45dcabdSAndy Whitcroft			my $spacing = $1;
2130c45dcabdSAndy Whitcroft			my $value = $2;
2131c45dcabdSAndy Whitcroft
213286f9d059SAndy Whitcroft			# Flatten any parentheses
2133fee61c47SAndy Whitcroft			$value =~ s/\)\(/\) \(/g;
213463f17f89SAndy Whitcroft			while ($value =~ s/\[[^\{\}]*\]/1/ ||
213563f17f89SAndy Whitcroft			       $value !~ /(?:$Ident|-?$Constant)\s*
213663f17f89SAndy Whitcroft					     $Compare\s*
213763f17f89SAndy Whitcroft					     (?:$Ident|-?$Constant)/x &&
213863f17f89SAndy Whitcroft			       $value =~ s/\([^\(\)]*\)/1/) {
2139c45dcabdSAndy Whitcroft			}
2140c45dcabdSAndy Whitcroft
2141c45dcabdSAndy Whitcroft			if ($value =~ /^(?:$Ident|-?$Constant)$/) {
2142c45dcabdSAndy Whitcroft				ERROR("return is not a function, parentheses are not required\n" . $herecurr);
2143c45dcabdSAndy Whitcroft
2144c45dcabdSAndy Whitcroft			} elsif ($spacing !~ /\s+/) {
2145c45dcabdSAndy Whitcroft				ERROR("space required before the open parenthesis '('\n" . $herecurr);
2146c45dcabdSAndy Whitcroft			}
2147c45dcabdSAndy Whitcroft		}
2148c45dcabdSAndy Whitcroft
21490a920b5bSAndy Whitcroft# Need a space before open parenthesis after if, while etc
21504a0df2efSAndy Whitcroft		if ($line=~/\b(if|while|for|switch)\(/) {
2151773647a0SAndy Whitcroft			ERROR("space required before the open parenthesis '('\n" . $herecurr);
21520a920b5bSAndy Whitcroft		}
21530a920b5bSAndy Whitcroft
2154f5fe35ddSAndy Whitcroft# Check for illegal assignment in if conditional -- and check for trailing
2155f5fe35ddSAndy Whitcroft# statements after the conditional.
2156170d3a22SAndy Whitcroft		if ($line =~ /do\s*(?!{)/) {
2157170d3a22SAndy Whitcroft			my ($stat_next) = ctx_statement_block($line_nr_next,
2158170d3a22SAndy Whitcroft						$remain_next, $off_next);
2159170d3a22SAndy Whitcroft			$stat_next =~ s/\n./\n /g;
2160170d3a22SAndy Whitcroft			##print "stat<$stat> stat_next<$stat_next>\n";
2161170d3a22SAndy Whitcroft
2162170d3a22SAndy Whitcroft			if ($stat_next =~ /^\s*while\b/) {
2163170d3a22SAndy Whitcroft				# If the statement carries leading newlines,
2164170d3a22SAndy Whitcroft				# then count those as offsets.
2165170d3a22SAndy Whitcroft				my ($whitespace) =
2166170d3a22SAndy Whitcroft					($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
2167170d3a22SAndy Whitcroft				my $offset =
2168170d3a22SAndy Whitcroft					statement_rawlines($whitespace) - 1;
2169170d3a22SAndy Whitcroft
2170170d3a22SAndy Whitcroft				$suppress_whiletrailers{$line_nr_next +
2171170d3a22SAndy Whitcroft								$offset} = 1;
2172170d3a22SAndy Whitcroft			}
2173170d3a22SAndy Whitcroft		}
2174170d3a22SAndy Whitcroft		if (!defined $suppress_whiletrailers{$linenr} &&
2175170d3a22SAndy Whitcroft		    $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
2176171ae1a4SAndy Whitcroft			my ($s, $c) = ($stat, $cond);
21778905a67cSAndy Whitcroft
2178b53c8e10SAndy Whitcroft			if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
2179c2fdda0dSAndy Whitcroft				ERROR("do not use assignment in if condition\n" . $herecurr);
21808905a67cSAndy Whitcroft			}
21818905a67cSAndy Whitcroft
21828905a67cSAndy Whitcroft			# Find out what is on the end of the line after the
21838905a67cSAndy Whitcroft			# conditional.
2184773647a0SAndy Whitcroft			substr($s, 0, length($c), '');
21858905a67cSAndy Whitcroft			$s =~ s/\n.*//g;
218613214adfSAndy Whitcroft			$s =~ s/$;//g; 	# Remove any comments
218753210168SAndy Whitcroft			if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
218853210168SAndy Whitcroft			    $c !~ /}\s*while\s*/)
2189773647a0SAndy Whitcroft			{
2190bb44ad39SAndy Whitcroft				# Find out how long the conditional actually is.
2191bb44ad39SAndy Whitcroft				my @newlines = ($c =~ /\n/gs);
2192bb44ad39SAndy Whitcroft				my $cond_lines = 1 + $#newlines;
219342bdf74cSHidetoshi Seto				my $stat_real = '';
2194bb44ad39SAndy Whitcroft
219542bdf74cSHidetoshi Seto				$stat_real = raw_line($linenr, $cond_lines)
219642bdf74cSHidetoshi Seto							. "\n" if ($cond_lines);
2197bb44ad39SAndy Whitcroft				if (defined($stat_real) && $cond_lines > 1) {
2198bb44ad39SAndy Whitcroft					$stat_real = "[...]\n$stat_real";
2199bb44ad39SAndy Whitcroft				}
2200bb44ad39SAndy Whitcroft
2201bb44ad39SAndy Whitcroft				ERROR("trailing statements should be on next line\n" . $herecurr . $stat_real);
22028905a67cSAndy Whitcroft			}
22038905a67cSAndy Whitcroft		}
22048905a67cSAndy Whitcroft
220513214adfSAndy Whitcroft# Check for bitwise tests written as boolean
220613214adfSAndy Whitcroft		if ($line =~ /
220713214adfSAndy Whitcroft			(?:
220813214adfSAndy Whitcroft				(?:\[|\(|\&\&|\|\|)
220913214adfSAndy Whitcroft				\s*0[xX][0-9]+\s*
221013214adfSAndy Whitcroft				(?:\&\&|\|\|)
221113214adfSAndy Whitcroft			|
221213214adfSAndy Whitcroft				(?:\&\&|\|\|)
221313214adfSAndy Whitcroft				\s*0[xX][0-9]+\s*
221413214adfSAndy Whitcroft				(?:\&\&|\|\||\)|\])
221513214adfSAndy Whitcroft			)/x)
221613214adfSAndy Whitcroft		{
221713214adfSAndy Whitcroft			WARN("boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
221813214adfSAndy Whitcroft		}
221913214adfSAndy Whitcroft
22208905a67cSAndy Whitcroft# if and else should not have general statements after it
222113214adfSAndy Whitcroft		if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
222213214adfSAndy Whitcroft			my $s = $1;
222313214adfSAndy Whitcroft			$s =~ s/$;//g; 	# Remove any comments
222413214adfSAndy Whitcroft			if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
22258905a67cSAndy Whitcroft				ERROR("trailing statements should be on next line\n" . $herecurr);
22260a920b5bSAndy Whitcroft			}
222713214adfSAndy Whitcroft		}
222839667782SAndy Whitcroft# if should not continue a brace
222939667782SAndy Whitcroft		if ($line =~ /}\s*if\b/) {
223039667782SAndy Whitcroft			ERROR("trailing statements should be on next line\n" .
223139667782SAndy Whitcroft				$herecurr);
223239667782SAndy Whitcroft		}
2233a1080bf8SAndy Whitcroft# case and default should not have general statements after them
2234a1080bf8SAndy Whitcroft		if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
2235a1080bf8SAndy Whitcroft		    $line !~ /\G(?:
22363fef12d6SAndy Whitcroft			(?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
2237a1080bf8SAndy Whitcroft			\s*return\s+
2238a1080bf8SAndy Whitcroft		    )/xg)
2239a1080bf8SAndy Whitcroft		{
2240a1080bf8SAndy Whitcroft			ERROR("trailing statements should be on next line\n" . $herecurr);
2241a1080bf8SAndy Whitcroft		}
22420a920b5bSAndy Whitcroft
22430a920b5bSAndy Whitcroft		# Check for }<nl>else {, these must be at the same
22440a920b5bSAndy Whitcroft		# indent level to be relevant to each other.
22450a920b5bSAndy Whitcroft		if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
22460a920b5bSAndy Whitcroft						$previndent == $indent) {
2247de7d4f0eSAndy Whitcroft			ERROR("else should follow close brace '}'\n" . $hereprev);
22480a920b5bSAndy Whitcroft		}
22490a920b5bSAndy Whitcroft
2250c2fdda0dSAndy Whitcroft		if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and
2251c2fdda0dSAndy Whitcroft						$previndent == $indent) {
2252c2fdda0dSAndy Whitcroft			my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
2253c2fdda0dSAndy Whitcroft
2254c2fdda0dSAndy Whitcroft			# Find out what is on the end of the line after the
2255c2fdda0dSAndy Whitcroft			# conditional.
2256773647a0SAndy Whitcroft			substr($s, 0, length($c), '');
2257c2fdda0dSAndy Whitcroft			$s =~ s/\n.*//g;
2258c2fdda0dSAndy Whitcroft
2259c2fdda0dSAndy Whitcroft			if ($s =~ /^\s*;/) {
2260c2fdda0dSAndy Whitcroft				ERROR("while should follow close brace '}'\n" . $hereprev);
2261c2fdda0dSAndy Whitcroft			}
2262c2fdda0dSAndy Whitcroft		}
2263c2fdda0dSAndy Whitcroft
22640a920b5bSAndy Whitcroft#studly caps, commented out until figure out how to distinguish between use of existing and adding new
22650a920b5bSAndy Whitcroft#		if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) {
22660a920b5bSAndy Whitcroft#		    print "No studly caps, use _\n";
22670a920b5bSAndy Whitcroft#		    print "$herecurr";
22680a920b5bSAndy Whitcroft#		    $clean = 0;
22690a920b5bSAndy Whitcroft#		}
22700a920b5bSAndy Whitcroft
22710a920b5bSAndy Whitcroft#no spaces allowed after \ in define
2272c45dcabdSAndy Whitcroft		if ($line=~/\#\s*define.*\\\s$/) {
2273de7d4f0eSAndy Whitcroft			WARN("Whitepspace after \\ makes next lines useless\n" . $herecurr);
22740a920b5bSAndy Whitcroft		}
22750a920b5bSAndy Whitcroft
2276653d4876SAndy Whitcroft#warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
2277c45dcabdSAndy Whitcroft		if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
2278e09dec48SAndy Whitcroft			my $file = "$1.h";
2279e09dec48SAndy Whitcroft			my $checkfile = "include/linux/$file";
2280e09dec48SAndy Whitcroft			if (-f "$root/$checkfile" &&
2281e09dec48SAndy Whitcroft			    $realfile ne $checkfile &&
2282c45dcabdSAndy Whitcroft			    $1 ne 'irq')
2283c45dcabdSAndy Whitcroft			{
2284e09dec48SAndy Whitcroft				if ($realfile =~ m{^arch/}) {
2285e09dec48SAndy Whitcroft					CHK("Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
2286e09dec48SAndy Whitcroft				} else {
2287e09dec48SAndy Whitcroft					WARN("Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
2288e09dec48SAndy Whitcroft				}
22890a920b5bSAndy Whitcroft			}
22900a920b5bSAndy Whitcroft		}
22910a920b5bSAndy Whitcroft
2292653d4876SAndy Whitcroft# multi-statement macros should be enclosed in a do while loop, grab the
2293653d4876SAndy Whitcroft# first statement and ensure its the whole macro if its not enclosed
2294cf655043SAndy Whitcroft# in a known good container
2295b8f96a31SAndy Whitcroft		if ($realfile !~ m@/vmlinux.lds.h$@ &&
2296b8f96a31SAndy Whitcroft		    $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
2297d8aaf121SAndy Whitcroft			my $ln = $linenr;
2298d8aaf121SAndy Whitcroft			my $cnt = $realcnt;
2299c45dcabdSAndy Whitcroft			my ($off, $dstat, $dcond, $rest);
2300c45dcabdSAndy Whitcroft			my $ctx = '';
2301653d4876SAndy Whitcroft
2302c45dcabdSAndy Whitcroft			my $args = defined($1);
2303de7d4f0eSAndy Whitcroft
2304c45dcabdSAndy Whitcroft			# Find the end of the macro and limit our statement
2305c45dcabdSAndy Whitcroft			# search to that.
2306c45dcabdSAndy Whitcroft			while ($cnt > 0 && defined $lines[$ln - 1] &&
2307c45dcabdSAndy Whitcroft				$lines[$ln - 1] =~ /^(?:-|..*\\$)/)
2308c45dcabdSAndy Whitcroft			{
2309c45dcabdSAndy Whitcroft				$ctx .= $rawlines[$ln - 1] . "\n";
2310a3bb97a7SAndy Whitcroft				$cnt-- if ($lines[$ln - 1] !~ /^-/);
2311c45dcabdSAndy Whitcroft				$ln++;
2312de7d4f0eSAndy Whitcroft			}
2313c45dcabdSAndy Whitcroft			$ctx .= $rawlines[$ln - 1];
2314d8aaf121SAndy Whitcroft
2315c45dcabdSAndy Whitcroft			($dstat, $dcond, $ln, $cnt, $off) =
2316c45dcabdSAndy Whitcroft				ctx_statement_block($linenr, $ln - $linenr + 1, 0);
2317c45dcabdSAndy Whitcroft			#print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
2318a3bb97a7SAndy Whitcroft			#print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
2319c45dcabdSAndy Whitcroft
2320c45dcabdSAndy Whitcroft			# Extract the remainder of the define (if any) and
2321c45dcabdSAndy Whitcroft			# rip off surrounding spaces, and trailing \'s.
2322c45dcabdSAndy Whitcroft			$rest = '';
2323636d140aSAndy Whitcroft			while ($off != 0 || ($cnt > 0 && $rest =~ /\\\s*$/)) {
2324636d140aSAndy Whitcroft				#print "ADDING cnt<$cnt> $off <" . substr($lines[$ln - 1], $off) . "> rest<$rest>\n";
2325a3bb97a7SAndy Whitcroft				if ($off != 0 || $lines[$ln - 1] !~ /^-/) {
2326c45dcabdSAndy Whitcroft					$rest .= substr($lines[$ln - 1], $off) . "\n";
2327c45dcabdSAndy Whitcroft					$cnt--;
2328a3bb97a7SAndy Whitcroft				}
2329a3bb97a7SAndy Whitcroft				$ln++;
2330c45dcabdSAndy Whitcroft				$off = 0;
2331c45dcabdSAndy Whitcroft			}
2332c45dcabdSAndy Whitcroft			$rest =~ s/\\\n.//g;
2333c45dcabdSAndy Whitcroft			$rest =~ s/^\s*//s;
2334c45dcabdSAndy Whitcroft			$rest =~ s/\s*$//s;
2335c45dcabdSAndy Whitcroft
2336c45dcabdSAndy Whitcroft			# Clean up the original statement.
2337c45dcabdSAndy Whitcroft			if ($args) {
2338c45dcabdSAndy Whitcroft				substr($dstat, 0, length($dcond), '');
2339d8aaf121SAndy Whitcroft			} else {
2340c45dcabdSAndy Whitcroft				$dstat =~ s/^.\s*\#\s*define\s+$Ident\s*//;
2341c45dcabdSAndy Whitcroft			}
2342292f1a9bSAndy Whitcroft			$dstat =~ s/$;//g;
2343c45dcabdSAndy Whitcroft			$dstat =~ s/\\\n.//g;
2344c45dcabdSAndy Whitcroft			$dstat =~ s/^\s*//s;
2345c45dcabdSAndy Whitcroft			$dstat =~ s/\s*$//s;
2346c45dcabdSAndy Whitcroft
2347c45dcabdSAndy Whitcroft			# Flatten any parentheses and braces
2348bf30d6edSAndy Whitcroft			while ($dstat =~ s/\([^\(\)]*\)/1/ ||
2349bf30d6edSAndy Whitcroft			       $dstat =~ s/\{[^\{\}]*\}/1/ ||
2350bf30d6edSAndy Whitcroft			       $dstat =~ s/\[[^\{\}]*\]/1/)
2351bf30d6edSAndy Whitcroft			{
2352c45dcabdSAndy Whitcroft			}
2353c45dcabdSAndy Whitcroft
2354c45dcabdSAndy Whitcroft			my $exceptions = qr{
2355c45dcabdSAndy Whitcroft				$Declare|
2356c45dcabdSAndy Whitcroft				module_param_named|
2357c45dcabdSAndy Whitcroft				MODULE_PARAM_DESC|
2358c45dcabdSAndy Whitcroft				DECLARE_PER_CPU|
2359c45dcabdSAndy Whitcroft				DEFINE_PER_CPU|
2360383099fdSAndy Whitcroft				__typeof__\(|
2361ea71a0a0SAndy Whitcroft				\.$Ident\s*=\s*|
2362ea71a0a0SAndy Whitcroft				^\"|\"$
2363c45dcabdSAndy Whitcroft			}x;
2364383099fdSAndy Whitcroft			#print "REST<$rest> dstat<$dstat>\n";
2365c45dcabdSAndy Whitcroft			if ($rest ne '') {
2366c45dcabdSAndy Whitcroft				if ($rest !~ /while\s*\(/ &&
2367c45dcabdSAndy Whitcroft				    $dstat !~ /$exceptions/)
2368c45dcabdSAndy Whitcroft				{
2369c45dcabdSAndy Whitcroft					ERROR("Macros with multiple statements should be enclosed in a do - while loop\n" . "$here\n$ctx\n");
2370c45dcabdSAndy Whitcroft				}
2371c45dcabdSAndy Whitcroft
2372c45dcabdSAndy Whitcroft			} elsif ($ctx !~ /;/) {
2373c45dcabdSAndy Whitcroft				if ($dstat ne '' &&
2374c45dcabdSAndy Whitcroft				    $dstat !~ /^(?:$Ident|-?$Constant)$/ &&
2375c45dcabdSAndy Whitcroft				    $dstat !~ /$exceptions/ &&
2376b132e5d5SAndy Whitcroft				    $dstat !~ /^\.$Ident\s*=/ &&
2377c45dcabdSAndy Whitcroft				    $dstat =~ /$Operators/)
2378c45dcabdSAndy Whitcroft				{
2379de7d4f0eSAndy Whitcroft					ERROR("Macros with complex values should be enclosed in parenthesis\n" . "$here\n$ctx\n");
2380d8aaf121SAndy Whitcroft				}
23810a920b5bSAndy Whitcroft			}
2382653d4876SAndy Whitcroft		}
23830a920b5bSAndy Whitcroft
2384080ba929SMike Frysinger# make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
2385080ba929SMike Frysinger# all assignments may have only one of the following with an assignment:
2386080ba929SMike Frysinger#	.
2387080ba929SMike Frysinger#	ALIGN(...)
2388080ba929SMike Frysinger#	VMLINUX_SYMBOL(...)
2389080ba929SMike Frysinger		if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
2390080ba929SMike Frysinger			WARN("vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
2391080ba929SMike Frysinger		}
2392080ba929SMike Frysinger
2393f0a594c1SAndy Whitcroft# check for redundant bracing round if etc
239413214adfSAndy Whitcroft		if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
239513214adfSAndy Whitcroft			my ($level, $endln, @chunks) =
2396cf655043SAndy Whitcroft				ctx_statement_full($linenr, $realcnt, 1);
239713214adfSAndy Whitcroft			#print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
2398cf655043SAndy Whitcroft			#print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
2399cf655043SAndy Whitcroft			if ($#chunks > 0 && $level == 0) {
240013214adfSAndy Whitcroft				my $allowed = 0;
240113214adfSAndy Whitcroft				my $seen = 0;
2402773647a0SAndy Whitcroft				my $herectx = $here . "\n";
2403cf655043SAndy Whitcroft				my $ln = $linenr - 1;
240413214adfSAndy Whitcroft				for my $chunk (@chunks) {
240513214adfSAndy Whitcroft					my ($cond, $block) = @{$chunk};
240613214adfSAndy Whitcroft
2407773647a0SAndy Whitcroft					# If the condition carries leading newlines, then count those as offsets.
2408773647a0SAndy Whitcroft					my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
2409773647a0SAndy Whitcroft					my $offset = statement_rawlines($whitespace) - 1;
2410773647a0SAndy Whitcroft
2411773647a0SAndy Whitcroft					#print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
2412773647a0SAndy Whitcroft
2413773647a0SAndy Whitcroft					# We have looked at and allowed this specific line.
2414773647a0SAndy Whitcroft					$suppress_ifbraces{$ln + $offset} = 1;
2415773647a0SAndy Whitcroft
2416773647a0SAndy Whitcroft					$herectx .= "$rawlines[$ln + $offset]\n[...]\n";
2417cf655043SAndy Whitcroft					$ln += statement_rawlines($block) - 1;
2418cf655043SAndy Whitcroft
2419773647a0SAndy Whitcroft					substr($block, 0, length($cond), '');
242013214adfSAndy Whitcroft
242113214adfSAndy Whitcroft					$seen++ if ($block =~ /^\s*{/);
242213214adfSAndy Whitcroft
2423cf655043SAndy Whitcroft					#print "cond<$cond> block<$block> allowed<$allowed>\n";
2424cf655043SAndy Whitcroft					if (statement_lines($cond) > 1) {
2425cf655043SAndy Whitcroft						#print "APW: ALLOWED: cond<$cond>\n";
242613214adfSAndy Whitcroft						$allowed = 1;
242713214adfSAndy Whitcroft					}
242813214adfSAndy Whitcroft					if ($block =~/\b(?:if|for|while)\b/) {
2429cf655043SAndy Whitcroft						#print "APW: ALLOWED: block<$block>\n";
243013214adfSAndy Whitcroft						$allowed = 1;
243113214adfSAndy Whitcroft					}
2432cf655043SAndy Whitcroft					if (statement_block_size($block) > 1) {
2433cf655043SAndy Whitcroft						#print "APW: ALLOWED: lines block<$block>\n";
243413214adfSAndy Whitcroft						$allowed = 1;
243513214adfSAndy Whitcroft					}
243613214adfSAndy Whitcroft				}
243713214adfSAndy Whitcroft				if ($seen && !$allowed) {
2438cf655043SAndy Whitcroft					WARN("braces {} are not necessary for any arm of this statement\n" . $herectx);
243913214adfSAndy Whitcroft				}
244013214adfSAndy Whitcroft			}
244113214adfSAndy Whitcroft		}
2442773647a0SAndy Whitcroft		if (!defined $suppress_ifbraces{$linenr - 1} &&
244313214adfSAndy Whitcroft					$line =~ /\b(if|while|for|else)\b/) {
2444cf655043SAndy Whitcroft			my $allowed = 0;
2445f0a594c1SAndy Whitcroft
2446cf655043SAndy Whitcroft			# Check the pre-context.
2447cf655043SAndy Whitcroft			if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
2448cf655043SAndy Whitcroft				#print "APW: ALLOWED: pre<$1>\n";
2449cf655043SAndy Whitcroft				$allowed = 1;
2450f0a594c1SAndy Whitcroft			}
2451773647a0SAndy Whitcroft
2452773647a0SAndy Whitcroft			my ($level, $endln, @chunks) =
2453773647a0SAndy Whitcroft				ctx_statement_full($linenr, $realcnt, $-[0]);
2454773647a0SAndy Whitcroft
2455cf655043SAndy Whitcroft			# Check the condition.
2456cf655043SAndy Whitcroft			my ($cond, $block) = @{$chunks[0]};
2457773647a0SAndy Whitcroft			#print "CHECKING<$linenr> cond<$cond> block<$block>\n";
2458cf655043SAndy Whitcroft			if (defined $cond) {
2459773647a0SAndy Whitcroft				substr($block, 0, length($cond), '');
2460cf655043SAndy Whitcroft			}
2461cf655043SAndy Whitcroft			if (statement_lines($cond) > 1) {
2462cf655043SAndy Whitcroft				#print "APW: ALLOWED: cond<$cond>\n";
2463cf655043SAndy Whitcroft				$allowed = 1;
2464cf655043SAndy Whitcroft			}
2465cf655043SAndy Whitcroft			if ($block =~/\b(?:if|for|while)\b/) {
2466cf655043SAndy Whitcroft				#print "APW: ALLOWED: block<$block>\n";
2467cf655043SAndy Whitcroft				$allowed = 1;
2468cf655043SAndy Whitcroft			}
2469cf655043SAndy Whitcroft			if (statement_block_size($block) > 1) {
2470cf655043SAndy Whitcroft				#print "APW: ALLOWED: lines block<$block>\n";
2471cf655043SAndy Whitcroft				$allowed = 1;
2472cf655043SAndy Whitcroft			}
2473cf655043SAndy Whitcroft			# Check the post-context.
2474cf655043SAndy Whitcroft			if (defined $chunks[1]) {
2475cf655043SAndy Whitcroft				my ($cond, $block) = @{$chunks[1]};
2476cf655043SAndy Whitcroft				if (defined $cond) {
2477773647a0SAndy Whitcroft					substr($block, 0, length($cond), '');
2478cf655043SAndy Whitcroft				}
2479cf655043SAndy Whitcroft				if ($block =~ /^\s*\{/) {
2480cf655043SAndy Whitcroft					#print "APW: ALLOWED: chunk-1 block<$block>\n";
2481cf655043SAndy Whitcroft					$allowed = 1;
2482cf655043SAndy Whitcroft				}
2483cf655043SAndy Whitcroft			}
2484cf655043SAndy Whitcroft			if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
2485cf655043SAndy Whitcroft				my $herectx = $here . "\n";;
2486f055663cSAndy Whitcroft				my $cnt = statement_rawlines($block);
2487cf655043SAndy Whitcroft
2488f055663cSAndy Whitcroft				for (my $n = 0; $n < $cnt; $n++) {
2489f055663cSAndy Whitcroft					$herectx .= raw_line($linenr, $n) . "\n";;
2490cf655043SAndy Whitcroft				}
2491cf655043SAndy Whitcroft
2492cf655043SAndy Whitcroft				WARN("braces {} are not necessary for single statement blocks\n" . $herectx);
2493f0a594c1SAndy Whitcroft			}
2494f0a594c1SAndy Whitcroft		}
2495f0a594c1SAndy Whitcroft
2496653d4876SAndy Whitcroft# don't include deprecated include files (uses RAW line)
24974a0df2efSAndy Whitcroft		for my $inc (@dep_includes) {
2498c45dcabdSAndy Whitcroft			if ($rawline =~ m@^.\s*\#\s*include\s*\<$inc>@) {
2499de7d4f0eSAndy Whitcroft				ERROR("Don't use <$inc>: see Documentation/feature-removal-schedule.txt\n" . $herecurr);
25000a920b5bSAndy Whitcroft			}
25010a920b5bSAndy Whitcroft		}
25020a920b5bSAndy Whitcroft
25034a0df2efSAndy Whitcroft# don't use deprecated functions
25044a0df2efSAndy Whitcroft		for my $func (@dep_functions) {
250500df344fSAndy Whitcroft			if ($line =~ /\b$func\b/) {
2506de7d4f0eSAndy Whitcroft				ERROR("Don't use $func(): see Documentation/feature-removal-schedule.txt\n" . $herecurr);
25074a0df2efSAndy Whitcroft			}
25084a0df2efSAndy Whitcroft		}
25094a0df2efSAndy Whitcroft
25104a0df2efSAndy Whitcroft# no volatiles please
25116c72ffaaSAndy Whitcroft		my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
25126c72ffaaSAndy Whitcroft		if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
2513de7d4f0eSAndy Whitcroft			WARN("Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
25144a0df2efSAndy Whitcroft		}
25154a0df2efSAndy Whitcroft
25169c0ca6f9SAndy Whitcroft# SPIN_LOCK_UNLOCKED & RW_LOCK_UNLOCKED are deprecated
25179c0ca6f9SAndy Whitcroft		if ($line =~ /\b(SPIN_LOCK_UNLOCKED|RW_LOCK_UNLOCKED)/) {
25189c0ca6f9SAndy Whitcroft			ERROR("Use of $1 is deprecated: see Documentation/spinlocks.txt\n" . $herecurr);
25199c0ca6f9SAndy Whitcroft		}
25209c0ca6f9SAndy Whitcroft
252100df344fSAndy Whitcroft# warn about #if 0
2522c45dcabdSAndy Whitcroft		if ($line =~ /^.\s*\#\s*if\s+0\b/) {
2523de7d4f0eSAndy Whitcroft			CHK("if this code is redundant consider removing it\n" .
2524de7d4f0eSAndy Whitcroft				$herecurr);
25254a0df2efSAndy Whitcroft		}
25264a0df2efSAndy Whitcroft
2527f0a594c1SAndy Whitcroft# check for needless kfree() checks
2528f0a594c1SAndy Whitcroft		if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
2529f0a594c1SAndy Whitcroft			my $expr = $1;
2530f0a594c1SAndy Whitcroft			if ($line =~ /\bkfree\(\Q$expr\E\);/) {
25313c232147SWolfram Sang				WARN("kfree(NULL) is safe this check is probably not required\n" . $hereprev);
2532f0a594c1SAndy Whitcroft			}
2533f0a594c1SAndy Whitcroft		}
25344c432a8fSGreg Kroah-Hartman# check for needless usb_free_urb() checks
25354c432a8fSGreg Kroah-Hartman		if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
25364c432a8fSGreg Kroah-Hartman			my $expr = $1;
25374c432a8fSGreg Kroah-Hartman			if ($line =~ /\busb_free_urb\(\Q$expr\E\);/) {
25384c432a8fSGreg Kroah-Hartman				WARN("usb_free_urb(NULL) is safe this check is probably not required\n" . $hereprev);
25394c432a8fSGreg Kroah-Hartman			}
25404c432a8fSGreg Kroah-Hartman		}
2541f0a594c1SAndy Whitcroft
254200df344fSAndy Whitcroft# warn about #ifdefs in C files
2543c45dcabdSAndy Whitcroft#		if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
254400df344fSAndy Whitcroft#			print "#ifdef in C files should be avoided\n";
254500df344fSAndy Whitcroft#			print "$herecurr";
254600df344fSAndy Whitcroft#			$clean = 0;
254700df344fSAndy Whitcroft#		}
254800df344fSAndy Whitcroft
254922f2a2efSAndy Whitcroft# warn about spacing in #ifdefs
2550c45dcabdSAndy Whitcroft		if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
255122f2a2efSAndy Whitcroft			ERROR("exactly one space required after that #$1\n" . $herecurr);
255222f2a2efSAndy Whitcroft		}
255322f2a2efSAndy Whitcroft
25544a0df2efSAndy Whitcroft# check for spinlock_t definitions without a comment.
2555171ae1a4SAndy Whitcroft		if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
2556171ae1a4SAndy Whitcroft		    $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
25574a0df2efSAndy Whitcroft			my $which = $1;
25584a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
2559de7d4f0eSAndy Whitcroft				CHK("$1 definition without comment\n" . $herecurr);
25604a0df2efSAndy Whitcroft			}
25614a0df2efSAndy Whitcroft		}
25624a0df2efSAndy Whitcroft# check for memory barriers without a comment.
25634a0df2efSAndy Whitcroft		if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
25644a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
2565de7d4f0eSAndy Whitcroft				CHK("memory barrier without comment\n" . $herecurr);
25664a0df2efSAndy Whitcroft			}
25674a0df2efSAndy Whitcroft		}
25684a0df2efSAndy Whitcroft# check of hardware specific defines
2569c45dcabdSAndy Whitcroft		if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
2570de7d4f0eSAndy Whitcroft			CHK("architecture specific defines should be avoided\n" .  $herecurr);
25710a920b5bSAndy Whitcroft		}
2572653d4876SAndy Whitcroft
2573de7d4f0eSAndy Whitcroft# check the location of the inline attribute, that it is between
2574de7d4f0eSAndy Whitcroft# storage class and type.
25759c0ca6f9SAndy Whitcroft		if ($line =~ /\b$Type\s+$Inline\b/ ||
25769c0ca6f9SAndy Whitcroft		    $line =~ /\b$Inline\s+$Storage\b/) {
2577de7d4f0eSAndy Whitcroft			ERROR("inline keyword should sit between storage class and type\n" . $herecurr);
2578de7d4f0eSAndy Whitcroft		}
2579de7d4f0eSAndy Whitcroft
25808905a67cSAndy Whitcroft# Check for __inline__ and __inline, prefer inline
25818905a67cSAndy Whitcroft		if ($line =~ /\b(__inline__|__inline)\b/) {
25828905a67cSAndy Whitcroft			WARN("plain inline is preferred over $1\n" . $herecurr);
25838905a67cSAndy Whitcroft		}
25848905a67cSAndy Whitcroft
25858f53a9b8SJoe Perches# check for sizeof(&)
25868f53a9b8SJoe Perches		if ($line =~ /\bsizeof\s*\(\s*\&/) {
25878f53a9b8SJoe Perches			WARN("sizeof(& should be avoided\n" . $herecurr);
25888f53a9b8SJoe Perches		}
25898f53a9b8SJoe Perches
2590de7d4f0eSAndy Whitcroft# check for new externs in .c files.
2591171ae1a4SAndy Whitcroft		if ($realfile =~ /\.c$/ && defined $stat &&
2592c45dcabdSAndy Whitcroft		    $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
2593171ae1a4SAndy Whitcroft		{
2594c45dcabdSAndy Whitcroft			my $function_name = $1;
2595c45dcabdSAndy Whitcroft			my $paren_space = $2;
2596171ae1a4SAndy Whitcroft
2597171ae1a4SAndy Whitcroft			my $s = $stat;
2598171ae1a4SAndy Whitcroft			if (defined $cond) {
2599171ae1a4SAndy Whitcroft				substr($s, 0, length($cond), '');
2600171ae1a4SAndy Whitcroft			}
2601c45dcabdSAndy Whitcroft			if ($s =~ /^\s*;/ &&
2602c45dcabdSAndy Whitcroft			    $function_name ne 'uninitialized_var')
2603c45dcabdSAndy Whitcroft			{
2604de7d4f0eSAndy Whitcroft				WARN("externs should be avoided in .c files\n" .  $herecurr);
2605de7d4f0eSAndy Whitcroft			}
2606de7d4f0eSAndy Whitcroft
2607171ae1a4SAndy Whitcroft			if ($paren_space =~ /\n/) {
2608171ae1a4SAndy Whitcroft				WARN("arguments for function declarations should follow identifier\n" . $herecurr);
2609171ae1a4SAndy Whitcroft			}
26109c9ba34eSAndy Whitcroft
26119c9ba34eSAndy Whitcroft		} elsif ($realfile =~ /\.c$/ && defined $stat &&
26129c9ba34eSAndy Whitcroft		    $stat =~ /^.\s*extern\s+/)
26139c9ba34eSAndy Whitcroft		{
26149c9ba34eSAndy Whitcroft			WARN("externs should be avoided in .c files\n" .  $herecurr);
2615171ae1a4SAndy Whitcroft		}
2616171ae1a4SAndy Whitcroft
2617de7d4f0eSAndy Whitcroft# checks for new __setup's
2618de7d4f0eSAndy Whitcroft		if ($rawline =~ /\b__setup\("([^"]*)"/) {
2619de7d4f0eSAndy Whitcroft			my $name = $1;
2620de7d4f0eSAndy Whitcroft
2621de7d4f0eSAndy Whitcroft			if (!grep(/$name/, @setup_docs)) {
2622de7d4f0eSAndy Whitcroft				CHK("__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
2623de7d4f0eSAndy Whitcroft			}
2624653d4876SAndy Whitcroft		}
26259c0ca6f9SAndy Whitcroft
26269c0ca6f9SAndy Whitcroft# check for pointless casting of kmalloc return
26279c0ca6f9SAndy Whitcroft		if ($line =~ /\*\s*\)\s*k[czm]alloc\b/) {
26289c0ca6f9SAndy Whitcroft			WARN("unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
26299c0ca6f9SAndy Whitcroft		}
263013214adfSAndy Whitcroft
263113214adfSAndy Whitcroft# check for gcc specific __FUNCTION__
263213214adfSAndy Whitcroft		if ($line =~ /__FUNCTION__/) {
263313214adfSAndy Whitcroft			WARN("__func__ should be used instead of gcc specific __FUNCTION__\n"  . $herecurr);
263413214adfSAndy Whitcroft		}
2635773647a0SAndy Whitcroft
2636773647a0SAndy Whitcroft# check for semaphores used as mutexes
2637171ae1a4SAndy Whitcroft		if ($line =~ /^.\s*(DECLARE_MUTEX|init_MUTEX)\s*\(/) {
2638773647a0SAndy Whitcroft			WARN("mutexes are preferred for single holder semaphores\n" . $herecurr);
2639773647a0SAndy Whitcroft		}
2640773647a0SAndy Whitcroft# check for semaphores used as mutexes
2641171ae1a4SAndy Whitcroft		if ($line =~ /^.\s*init_MUTEX_LOCKED\s*\(/) {
2642773647a0SAndy Whitcroft			WARN("consider using a completion\n" . $herecurr);
2643773647a0SAndy Whitcroft		}
2644773647a0SAndy Whitcroft# recommend strict_strto* over simple_strto*
2645773647a0SAndy Whitcroft		if ($line =~ /\bsimple_(strto.*?)\s*\(/) {
2646773647a0SAndy Whitcroft			WARN("consider using strict_$1 in preference to simple_$1\n" . $herecurr);
2647773647a0SAndy Whitcroft		}
2648f3db6639SMichael Ellerman# check for __initcall(), use device_initcall() explicitly please
2649f3db6639SMichael Ellerman		if ($line =~ /^.\s*__initcall\s*\(/) {
2650f3db6639SMichael Ellerman			WARN("please use device_initcall() instead of __initcall()\n" . $herecurr);
2651f3db6639SMichael Ellerman		}
26522b6db5cbSAndy Whitcroft# check for struct file_operations, ensure they are const.
26536903ffb2SAndy Whitcroft		if ($line !~ /\bconst\b/ &&
26546903ffb2SAndy Whitcroft		    $line =~ /\bstruct\s+(file_operations|seq_operations)\b/) {
26556903ffb2SAndy Whitcroft			WARN("struct $1 should normally be const\n" .
26566903ffb2SAndy Whitcroft				$herecurr);
26572b6db5cbSAndy Whitcroft		}
2658773647a0SAndy Whitcroft
2659773647a0SAndy Whitcroft# use of NR_CPUS is usually wrong
2660773647a0SAndy Whitcroft# ignore definitions of NR_CPUS and usage to define arrays as likely right
2661773647a0SAndy Whitcroft		if ($line =~ /\bNR_CPUS\b/ &&
2662c45dcabdSAndy Whitcroft		    $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
2663c45dcabdSAndy Whitcroft		    $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
2664171ae1a4SAndy Whitcroft		    $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
2665171ae1a4SAndy Whitcroft		    $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
2666171ae1a4SAndy Whitcroft		    $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
2667773647a0SAndy Whitcroft		{
2668773647a0SAndy Whitcroft			WARN("usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
2669773647a0SAndy Whitcroft		}
26709c9ba34eSAndy Whitcroft
26719c9ba34eSAndy Whitcroft# check for %L{u,d,i} in strings
26729c9ba34eSAndy Whitcroft		my $string;
26739c9ba34eSAndy Whitcroft		while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
26749c9ba34eSAndy Whitcroft			$string = substr($rawline, $-[1], $+[1] - $-[1]);
26752a1bc5d5SAndy Whitcroft			$string =~ s/%%/__/g;
26769c9ba34eSAndy Whitcroft			if ($string =~ /(?<!%)%L[udi]/) {
26779c9ba34eSAndy Whitcroft				WARN("\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
26789c9ba34eSAndy Whitcroft				last;
26799c9ba34eSAndy Whitcroft			}
26809c9ba34eSAndy Whitcroft		}
2681691d77b6SAndy Whitcroft
2682691d77b6SAndy Whitcroft# whine mightly about in_atomic
2683691d77b6SAndy Whitcroft		if ($line =~ /\bin_atomic\s*\(/) {
2684691d77b6SAndy Whitcroft			if ($realfile =~ m@^drivers/@) {
2685691d77b6SAndy Whitcroft				ERROR("do not use in_atomic in drivers\n" . $herecurr);
2686f4a87736SAndy Whitcroft			} elsif ($realfile !~ m@^kernel/@) {
2687691d77b6SAndy Whitcroft				WARN("use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
2688691d77b6SAndy Whitcroft			}
2689691d77b6SAndy Whitcroft		}
269013214adfSAndy Whitcroft	}
269113214adfSAndy Whitcroft
269213214adfSAndy Whitcroft	# If we have no input at all, then there is nothing to report on
269313214adfSAndy Whitcroft	# so just keep quiet.
269413214adfSAndy Whitcroft	if ($#rawlines == -1) {
269513214adfSAndy Whitcroft		exit(0);
26960a920b5bSAndy Whitcroft	}
26970a920b5bSAndy Whitcroft
26988905a67cSAndy Whitcroft	# In mailback mode only produce a report in the negative, for
26998905a67cSAndy Whitcroft	# things that appear to be patches.
27008905a67cSAndy Whitcroft	if ($mailback && ($clean == 1 || !$is_patch)) {
27018905a67cSAndy Whitcroft		exit(0);
27028905a67cSAndy Whitcroft	}
27038905a67cSAndy Whitcroft
27048905a67cSAndy Whitcroft	# This is not a patch, and we are are in 'no-patch' mode so
27058905a67cSAndy Whitcroft	# just keep quiet.
27068905a67cSAndy Whitcroft	if (!$chk_patch && !$is_patch) {
27078905a67cSAndy Whitcroft		exit(0);
27088905a67cSAndy Whitcroft	}
27098905a67cSAndy Whitcroft
27108905a67cSAndy Whitcroft	if (!$is_patch) {
2711de7d4f0eSAndy Whitcroft		ERROR("Does not appear to be a unified-diff format patch\n");
27120a920b5bSAndy Whitcroft	}
27130a920b5bSAndy Whitcroft	if ($is_patch && $chk_signoff && $signoff == 0) {
2714de7d4f0eSAndy Whitcroft		ERROR("Missing Signed-off-by: line(s)\n");
27150a920b5bSAndy Whitcroft	}
27160a920b5bSAndy Whitcroft
2717f0a594c1SAndy Whitcroft	print report_dump();
271813214adfSAndy Whitcroft	if ($summary && !($clean == 1 && $quiet == 1)) {
271913214adfSAndy Whitcroft		print "$filename " if ($summary_file);
27206c72ffaaSAndy Whitcroft		print "total: $cnt_error errors, $cnt_warn warnings, " .
27216c72ffaaSAndy Whitcroft			(($check)? "$cnt_chk checks, " : "") .
27226c72ffaaSAndy Whitcroft			"$cnt_lines lines checked\n";
27238905a67cSAndy Whitcroft		print "\n" if ($quiet == 0);
27246c72ffaaSAndy Whitcroft	}
27258905a67cSAndy Whitcroft
27260a920b5bSAndy Whitcroft	if ($clean == 1 && $quiet == 0) {
2727c2fdda0dSAndy Whitcroft		print "$vname has no obvious style problems and is ready for submission.\n"
27280a920b5bSAndy Whitcroft	}
27290a920b5bSAndy Whitcroft	if ($clean == 0 && $quiet == 0) {
2730c2fdda0dSAndy Whitcroft		print "$vname has style problems, please review.  If any of these errors\n";
27310a920b5bSAndy Whitcroft		print "are false positives report them to the maintainer, see\n";
27320a920b5bSAndy Whitcroft		print "CHECKPATCH in MAINTAINERS.\n";
27330a920b5bSAndy Whitcroft	}
273413214adfSAndy Whitcroft
27350a920b5bSAndy Whitcroft	return $clean;
27360a920b5bSAndy Whitcroft}
2737