xref: /linux-6.15/scripts/checkpatch.pl (revision 165e72a6)
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)
5015830beSAndy Whitcroft# (c) 2008-2010 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
13267ad8f4SAndy Whitcroftmy $V = '0.31';
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
106d2c0a235SAndy Whitcroftmy $rpt_cleaners = 0;
107d2c0a235SAndy Whitcroft
1088905a67cSAndy Whitcroftif ($terse) {
1098905a67cSAndy Whitcroft	$emacs = 1;
1108905a67cSAndy Whitcroft	$quiet++;
1118905a67cSAndy Whitcroft}
1128905a67cSAndy Whitcroft
1136c72ffaaSAndy Whitcroftif ($tree) {
1146c72ffaaSAndy Whitcroft	if (defined $root) {
1156c72ffaaSAndy Whitcroft		if (!top_of_kernel_tree($root)) {
1166c72ffaaSAndy Whitcroft			die "$P: $root: --root does not point at a valid tree\n";
1176c72ffaaSAndy Whitcroft		}
1186c72ffaaSAndy Whitcroft	} else {
1196c72ffaaSAndy Whitcroft		if (top_of_kernel_tree('.')) {
1206c72ffaaSAndy Whitcroft			$root = '.';
1216c72ffaaSAndy Whitcroft		} elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
1226c72ffaaSAndy Whitcroft						top_of_kernel_tree($1)) {
1236c72ffaaSAndy Whitcroft			$root = $1;
1246c72ffaaSAndy Whitcroft		}
1256c72ffaaSAndy Whitcroft	}
1266c72ffaaSAndy Whitcroft
1276c72ffaaSAndy Whitcroft	if (!defined $root) {
1280a920b5bSAndy Whitcroft		print "Must be run from the top-level dir. of a kernel tree\n";
1290a920b5bSAndy Whitcroft		exit(2);
1300a920b5bSAndy Whitcroft	}
1316c72ffaaSAndy Whitcroft}
1326c72ffaaSAndy Whitcroft
1336c72ffaaSAndy Whitcroftmy $emitted_corrupt = 0;
1346c72ffaaSAndy Whitcroft
1352ceb532bSAndy Whitcroftour $Ident	= qr{
1362ceb532bSAndy Whitcroft			[A-Za-z_][A-Za-z\d_]*
1372ceb532bSAndy Whitcroft			(?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
1382ceb532bSAndy Whitcroft		}x;
1396c72ffaaSAndy Whitcroftour $Storage	= qr{extern|static|asmlinkage};
1406c72ffaaSAndy Whitcroftour $Sparse	= qr{
1416c72ffaaSAndy Whitcroft			__user|
1426c72ffaaSAndy Whitcroft			__kernel|
1436c72ffaaSAndy Whitcroft			__force|
1446c72ffaaSAndy Whitcroft			__iomem|
1456c72ffaaSAndy Whitcroft			__must_check|
1466c72ffaaSAndy Whitcroft			__init_refok|
147417495edSAndy Whitcroft			__kprobes|
148*165e72a6SSven Eckelmann			__ref|
149*165e72a6SSven Eckelmann			__rcu
1506c72ffaaSAndy Whitcroft		}x;
15152131292SWolfram Sang
15252131292SWolfram Sang# Notes to $Attribute:
15352131292SWolfram Sang# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
1546c72ffaaSAndy Whitcroftour $Attribute	= qr{
1556c72ffaaSAndy Whitcroft			const|
15603f1df7dSJoe Perches			__percpu|
15703f1df7dSJoe Perches			__nocast|
15803f1df7dSJoe Perches			__safe|
15903f1df7dSJoe Perches			__bitwise__|
16003f1df7dSJoe Perches			__packed__|
16103f1df7dSJoe Perches			__packed2__|
16203f1df7dSJoe Perches			__naked|
16303f1df7dSJoe Perches			__maybe_unused|
16403f1df7dSJoe Perches			__always_unused|
16503f1df7dSJoe Perches			__noreturn|
16603f1df7dSJoe Perches			__used|
16703f1df7dSJoe Perches			__cold|
16803f1df7dSJoe Perches			__noclone|
16903f1df7dSJoe Perches			__deprecated|
1706c72ffaaSAndy Whitcroft			__read_mostly|
1716c72ffaaSAndy Whitcroft			__kprobes|
17252131292SWolfram Sang			__(?:mem|cpu|dev|)(?:initdata|initconst|init\b)|
17324e1d81aSAndy Whitcroft			____cacheline_aligned|
17424e1d81aSAndy Whitcroft			____cacheline_aligned_in_smp|
1755fe3af11SAndy Whitcroft			____cacheline_internodealigned_in_smp|
1765fe3af11SAndy Whitcroft			__weak
1776c72ffaaSAndy Whitcroft		  }x;
178c45dcabdSAndy Whitcroftour $Modifier;
1796c72ffaaSAndy Whitcroftour $Inline	= qr{inline|__always_inline|noinline};
1806c72ffaaSAndy Whitcroftour $Member	= qr{->$Ident|\.$Ident|\[[^]]*\]};
1816c72ffaaSAndy Whitcroftour $Lval	= qr{$Ident(?:$Member)*};
1826c72ffaaSAndy Whitcroft
1836c72ffaaSAndy Whitcroftour $Constant	= qr{(?:[0-9]+|0x[0-9a-fA-F]+)[UL]*};
1846c72ffaaSAndy Whitcroftour $Assignment	= qr{(?:\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=)};
18586f9d059SAndy Whitcroftour $Compare    = qr{<=|>=|==|!=|<|>};
1866c72ffaaSAndy Whitcroftour $Operators	= qr{
1876c72ffaaSAndy Whitcroft			<=|>=|==|!=|
1886c72ffaaSAndy Whitcroft			=>|->|<<|>>|<|>|!|~|
189c2fdda0dSAndy Whitcroft			&&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%
1906c72ffaaSAndy Whitcroft		  }x;
1916c72ffaaSAndy Whitcroft
1928905a67cSAndy Whitcroftour $NonptrType;
1938905a67cSAndy Whitcroftour $Type;
1948905a67cSAndy Whitcroftour $Declare;
1958905a67cSAndy Whitcroft
196171ae1a4SAndy Whitcroftour $UTF8	= qr {
197171ae1a4SAndy Whitcroft	[\x09\x0A\x0D\x20-\x7E]              # ASCII
198171ae1a4SAndy Whitcroft	| [\xC2-\xDF][\x80-\xBF]             # non-overlong 2-byte
199171ae1a4SAndy Whitcroft	|  \xE0[\xA0-\xBF][\x80-\xBF]        # excluding overlongs
200171ae1a4SAndy Whitcroft	| [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}  # straight 3-byte
201171ae1a4SAndy Whitcroft	|  \xED[\x80-\x9F][\x80-\xBF]        # excluding surrogates
202171ae1a4SAndy Whitcroft	|  \xF0[\x90-\xBF][\x80-\xBF]{2}     # planes 1-3
203171ae1a4SAndy Whitcroft	| [\xF1-\xF3][\x80-\xBF]{3}          # planes 4-15
204171ae1a4SAndy Whitcroft	|  \xF4[\x80-\x8F][\x80-\xBF]{2}     # plane 16
205171ae1a4SAndy Whitcroft}x;
206171ae1a4SAndy Whitcroft
2078ed22cadSAndy Whitcroftour $typeTypedefs = qr{(?x:
208fb9e9096SAndy Whitcroft	(?:__)?(?:u|s|be|le)(?:8|16|32|64)|
2098ed22cadSAndy Whitcroft	atomic_t
2108ed22cadSAndy Whitcroft)};
2118ed22cadSAndy Whitcroft
212691e669bSJoe Perchesour $logFunctions = qr{(?x:
213691e669bSJoe Perches	printk|
214b0531722SJoe Perches	[a-z]+_(emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)|
215691e669bSJoe Perches	WARN|
216b0531722SJoe Perches	panic|
217b0531722SJoe Perches	MODULE_[A-Z_]+
218691e669bSJoe Perches)};
219691e669bSJoe Perches
2208905a67cSAndy Whitcroftour @typeList = (
2218905a67cSAndy Whitcroft	qr{void},
222c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?char},
223c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?short},
224c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?int},
225c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?long},
226c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?long\s+int},
227c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?long\s+long},
228c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?long\s+long\s+int},
2298905a67cSAndy Whitcroft	qr{unsigned},
2308905a67cSAndy Whitcroft	qr{float},
2318905a67cSAndy Whitcroft	qr{double},
2328905a67cSAndy Whitcroft	qr{bool},
2338905a67cSAndy Whitcroft	qr{struct\s+$Ident},
2348905a67cSAndy Whitcroft	qr{union\s+$Ident},
2358905a67cSAndy Whitcroft	qr{enum\s+$Ident},
2368905a67cSAndy Whitcroft	qr{${Ident}_t},
2378905a67cSAndy Whitcroft	qr{${Ident}_handler},
2388905a67cSAndy Whitcroft	qr{${Ident}_handler_fn},
2398905a67cSAndy Whitcroft);
240c45dcabdSAndy Whitcroftour @modifierList = (
241c45dcabdSAndy Whitcroft	qr{fastcall},
242c45dcabdSAndy Whitcroft);
2438905a67cSAndy Whitcroft
2447840a94cSWolfram Sangour $allowed_asm_includes = qr{(?x:
2457840a94cSWolfram Sang	irq|
2467840a94cSWolfram Sang	memory
2477840a94cSWolfram Sang)};
2487840a94cSWolfram Sang# memory.h: ARM has a custom one
2497840a94cSWolfram Sang
2508905a67cSAndy Whitcroftsub build_types {
251d2172eb5SAndy Whitcroft	my $mods = "(?x:  \n" . join("|\n  ", @modifierList) . "\n)";
252d2172eb5SAndy Whitcroft	my $all = "(?x:  \n" . join("|\n  ", @typeList) . "\n)";
253c8cb2ca3SAndy Whitcroft	$Modifier	= qr{(?:$Attribute|$Sparse|$mods)};
2548905a67cSAndy Whitcroft	$NonptrType	= qr{
255d2172eb5SAndy Whitcroft			(?:$Modifier\s+|const\s+)*
256cf655043SAndy Whitcroft			(?:
257c45dcabdSAndy Whitcroft				(?:typeof|__typeof__)\s*\(\s*\**\s*$Ident\s*\)|
2588ed22cadSAndy Whitcroft				(?:$typeTypedefs\b)|
259c45dcabdSAndy Whitcroft				(?:${all}\b)
260cf655043SAndy Whitcroft			)
261c8cb2ca3SAndy Whitcroft			(?:\s+$Modifier|\s+const)*
2628905a67cSAndy Whitcroft		  }x;
2638905a67cSAndy Whitcroft	$Type	= qr{
264c45dcabdSAndy Whitcroft			$NonptrType
26565863862SAndy Whitcroft			(?:[\s\*]+\s*const|[\s\*]+|(?:\s*\[\s*\])+)?
266c8cb2ca3SAndy Whitcroft			(?:\s+$Inline|\s+$Modifier)*
2678905a67cSAndy Whitcroft		  }x;
2688905a67cSAndy Whitcroft	$Declare	= qr{(?:$Storage\s+)?$Type};
2698905a67cSAndy Whitcroft}
2708905a67cSAndy Whitcroftbuild_types();
2716c72ffaaSAndy Whitcroft
2727d2367afSJoe Perchesour $match_balanced_parentheses = qr/(\((?:[^\(\)]+|(-1))*\))/;
2737d2367afSJoe Perches
2747d2367afSJoe Perchesour $Typecast	= qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
2757d2367afSJoe Perchesour $LvalOrFunc	= qr{($Lval)\s*($match_balanced_parentheses{0,1})\s*};
2767d2367afSJoe Perches
2777d2367afSJoe Perchessub deparenthesize {
2787d2367afSJoe Perches	my ($string) = @_;
2797d2367afSJoe Perches	return "" if (!defined($string));
2807d2367afSJoe Perches	$string =~ s@^\s*\(\s*@@g;
2817d2367afSJoe Perches	$string =~ s@\s*\)\s*$@@g;
2827d2367afSJoe Perches	$string =~ s@\s+@ @g;
2837d2367afSJoe Perches	return $string;
2847d2367afSJoe Perches}
2857d2367afSJoe Perches
2866c72ffaaSAndy Whitcroft$chk_signoff = 0 if ($file);
2870a920b5bSAndy Whitcroft
2884a0df2efSAndy Whitcroftmy @dep_includes = ();
2894a0df2efSAndy Whitcroftmy @dep_functions = ();
2906c72ffaaSAndy Whitcroftmy $removal = "Documentation/feature-removal-schedule.txt";
2916c72ffaaSAndy Whitcroftif ($tree && -f "$root/$removal") {
29221caa13cSAndy Whitcroft	open(my $REMOVE, '<', "$root/$removal") ||
2936c72ffaaSAndy Whitcroft				die "$P: $removal: open failed - $!\n";
29421caa13cSAndy Whitcroft	while (<$REMOVE>) {
295f0a594c1SAndy Whitcroft		if (/^Check:\s+(.*\S)/) {
296f0a594c1SAndy Whitcroft			for my $entry (split(/[, ]+/, $1)) {
297f0a594c1SAndy Whitcroft				if ($entry =~ m@include/(.*)@) {
2984a0df2efSAndy Whitcroft					push(@dep_includes, $1);
2994a0df2efSAndy Whitcroft
300f0a594c1SAndy Whitcroft				} elsif ($entry !~ m@/@) {
301f0a594c1SAndy Whitcroft					push(@dep_functions, $entry);
302f0a594c1SAndy Whitcroft				}
3034a0df2efSAndy Whitcroft			}
3040a920b5bSAndy Whitcroft		}
3050a920b5bSAndy Whitcroft	}
30621caa13cSAndy Whitcroft	close($REMOVE);
3070a920b5bSAndy Whitcroft}
3080a920b5bSAndy Whitcroft
30900df344fSAndy Whitcroftmy @rawlines = ();
310c2fdda0dSAndy Whitcroftmy @lines = ();
311c2fdda0dSAndy Whitcroftmy $vname;
3126c72ffaaSAndy Whitcroftfor my $filename (@ARGV) {
31321caa13cSAndy Whitcroft	my $FILE;
3146c72ffaaSAndy Whitcroft	if ($file) {
31521caa13cSAndy Whitcroft		open($FILE, '-|', "diff -u /dev/null $filename") ||
3166c72ffaaSAndy Whitcroft			die "$P: $filename: diff failed - $!\n";
31721caa13cSAndy Whitcroft	} elsif ($filename eq '-') {
31821caa13cSAndy Whitcroft		open($FILE, '<&STDIN');
3196c72ffaaSAndy Whitcroft	} else {
32021caa13cSAndy Whitcroft		open($FILE, '<', "$filename") ||
3216c72ffaaSAndy Whitcroft			die "$P: $filename: open failed - $!\n";
3226c72ffaaSAndy Whitcroft	}
323c2fdda0dSAndy Whitcroft	if ($filename eq '-') {
324c2fdda0dSAndy Whitcroft		$vname = 'Your patch';
325c2fdda0dSAndy Whitcroft	} else {
326c2fdda0dSAndy Whitcroft		$vname = $filename;
327c2fdda0dSAndy Whitcroft	}
32821caa13cSAndy Whitcroft	while (<$FILE>) {
3290a920b5bSAndy Whitcroft		chomp;
33000df344fSAndy Whitcroft		push(@rawlines, $_);
3316c72ffaaSAndy Whitcroft	}
33221caa13cSAndy Whitcroft	close($FILE);
333c2fdda0dSAndy Whitcroft	if (!process($filename)) {
3340a920b5bSAndy Whitcroft		$exit = 1;
3350a920b5bSAndy Whitcroft	}
33600df344fSAndy Whitcroft	@rawlines = ();
33713214adfSAndy Whitcroft	@lines = ();
3380a920b5bSAndy Whitcroft}
3390a920b5bSAndy Whitcroft
3400a920b5bSAndy Whitcroftexit($exit);
3410a920b5bSAndy Whitcroft
3420a920b5bSAndy Whitcroftsub top_of_kernel_tree {
3436c72ffaaSAndy Whitcroft	my ($root) = @_;
3446c72ffaaSAndy Whitcroft
3456c72ffaaSAndy Whitcroft	my @tree_check = (
3466c72ffaaSAndy Whitcroft		"COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
3476c72ffaaSAndy Whitcroft		"README", "Documentation", "arch", "include", "drivers",
3486c72ffaaSAndy Whitcroft		"fs", "init", "ipc", "kernel", "lib", "scripts",
3496c72ffaaSAndy Whitcroft	);
3506c72ffaaSAndy Whitcroft
3516c72ffaaSAndy Whitcroft	foreach my $check (@tree_check) {
3526c72ffaaSAndy Whitcroft		if (! -e $root . '/' . $check) {
3530a920b5bSAndy Whitcroft			return 0;
3540a920b5bSAndy Whitcroft		}
3556c72ffaaSAndy Whitcroft	}
3566c72ffaaSAndy Whitcroft	return 1;
3576c72ffaaSAndy Whitcroft}
3580a920b5bSAndy Whitcroft
3590a920b5bSAndy Whitcroftsub expand_tabs {
3600a920b5bSAndy Whitcroft	my ($str) = @_;
3610a920b5bSAndy Whitcroft
3620a920b5bSAndy Whitcroft	my $res = '';
3630a920b5bSAndy Whitcroft	my $n = 0;
3640a920b5bSAndy Whitcroft	for my $c (split(//, $str)) {
3650a920b5bSAndy Whitcroft		if ($c eq "\t") {
3660a920b5bSAndy Whitcroft			$res .= ' ';
3670a920b5bSAndy Whitcroft			$n++;
3680a920b5bSAndy Whitcroft			for (; ($n % 8) != 0; $n++) {
3690a920b5bSAndy Whitcroft				$res .= ' ';
3700a920b5bSAndy Whitcroft			}
3710a920b5bSAndy Whitcroft			next;
3720a920b5bSAndy Whitcroft		}
3730a920b5bSAndy Whitcroft		$res .= $c;
3740a920b5bSAndy Whitcroft		$n++;
3750a920b5bSAndy Whitcroft	}
3760a920b5bSAndy Whitcroft
3770a920b5bSAndy Whitcroft	return $res;
3780a920b5bSAndy Whitcroft}
3796c72ffaaSAndy Whitcroftsub copy_spacing {
380773647a0SAndy Whitcroft	(my $res = shift) =~ tr/\t/ /c;
3816c72ffaaSAndy Whitcroft	return $res;
3826c72ffaaSAndy Whitcroft}
3830a920b5bSAndy Whitcroft
3844a0df2efSAndy Whitcroftsub line_stats {
3854a0df2efSAndy Whitcroft	my ($line) = @_;
3864a0df2efSAndy Whitcroft
3874a0df2efSAndy Whitcroft	# Drop the diff line leader and expand tabs
3884a0df2efSAndy Whitcroft	$line =~ s/^.//;
3894a0df2efSAndy Whitcroft	$line = expand_tabs($line);
3904a0df2efSAndy Whitcroft
3914a0df2efSAndy Whitcroft	# Pick the indent from the front of the line.
3924a0df2efSAndy Whitcroft	my ($white) = ($line =~ /^(\s*)/);
3934a0df2efSAndy Whitcroft
3944a0df2efSAndy Whitcroft	return (length($line), length($white));
3954a0df2efSAndy Whitcroft}
3964a0df2efSAndy Whitcroft
397773647a0SAndy Whitcroftmy $sanitise_quote = '';
398773647a0SAndy Whitcroft
399773647a0SAndy Whitcroftsub sanitise_line_reset {
400773647a0SAndy Whitcroft	my ($in_comment) = @_;
401773647a0SAndy Whitcroft
402773647a0SAndy Whitcroft	if ($in_comment) {
403773647a0SAndy Whitcroft		$sanitise_quote = '*/';
404773647a0SAndy Whitcroft	} else {
405773647a0SAndy Whitcroft		$sanitise_quote = '';
406773647a0SAndy Whitcroft	}
407773647a0SAndy Whitcroft}
40800df344fSAndy Whitcroftsub sanitise_line {
40900df344fSAndy Whitcroft	my ($line) = @_;
41000df344fSAndy Whitcroft
41100df344fSAndy Whitcroft	my $res = '';
41200df344fSAndy Whitcroft	my $l = '';
41300df344fSAndy Whitcroft
414c2fdda0dSAndy Whitcroft	my $qlen = 0;
415773647a0SAndy Whitcroft	my $off = 0;
416773647a0SAndy Whitcroft	my $c;
41700df344fSAndy Whitcroft
418773647a0SAndy Whitcroft	# Always copy over the diff marker.
419773647a0SAndy Whitcroft	$res = substr($line, 0, 1);
420773647a0SAndy Whitcroft
421773647a0SAndy Whitcroft	for ($off = 1; $off < length($line); $off++) {
422773647a0SAndy Whitcroft		$c = substr($line, $off, 1);
423773647a0SAndy Whitcroft
424773647a0SAndy Whitcroft		# Comments we are wacking completly including the begin
425773647a0SAndy Whitcroft		# and end, all to $;.
426773647a0SAndy Whitcroft		if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
427773647a0SAndy Whitcroft			$sanitise_quote = '*/';
428773647a0SAndy Whitcroft
429773647a0SAndy Whitcroft			substr($res, $off, 2, "$;$;");
430773647a0SAndy Whitcroft			$off++;
43100df344fSAndy Whitcroft			next;
432773647a0SAndy Whitcroft		}
43381bc0e02SAndy Whitcroft		if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
434773647a0SAndy Whitcroft			$sanitise_quote = '';
435773647a0SAndy Whitcroft			substr($res, $off, 2, "$;$;");
436773647a0SAndy Whitcroft			$off++;
437773647a0SAndy Whitcroft			next;
438773647a0SAndy Whitcroft		}
439113f04a8SDaniel Walker		if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
440113f04a8SDaniel Walker			$sanitise_quote = '//';
441113f04a8SDaniel Walker
442113f04a8SDaniel Walker			substr($res, $off, 2, $sanitise_quote);
443113f04a8SDaniel Walker			$off++;
444113f04a8SDaniel Walker			next;
445113f04a8SDaniel Walker		}
446773647a0SAndy Whitcroft
447773647a0SAndy Whitcroft		# A \ in a string means ignore the next character.
448773647a0SAndy Whitcroft		if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
449773647a0SAndy Whitcroft		    $c eq "\\") {
450773647a0SAndy Whitcroft			substr($res, $off, 2, 'XX');
451773647a0SAndy Whitcroft			$off++;
452773647a0SAndy Whitcroft			next;
453773647a0SAndy Whitcroft		}
454773647a0SAndy Whitcroft		# Regular quotes.
455773647a0SAndy Whitcroft		if ($c eq "'" || $c eq '"') {
456773647a0SAndy Whitcroft			if ($sanitise_quote eq '') {
457773647a0SAndy Whitcroft				$sanitise_quote = $c;
458773647a0SAndy Whitcroft
459773647a0SAndy Whitcroft				substr($res, $off, 1, $c);
460773647a0SAndy Whitcroft				next;
461773647a0SAndy Whitcroft			} elsif ($sanitise_quote eq $c) {
462773647a0SAndy Whitcroft				$sanitise_quote = '';
46300df344fSAndy Whitcroft			}
46400df344fSAndy Whitcroft		}
465773647a0SAndy Whitcroft
466fae17daeSAndy Whitcroft		#print "c<$c> SQ<$sanitise_quote>\n";
467773647a0SAndy Whitcroft		if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
468773647a0SAndy Whitcroft			substr($res, $off, 1, $;);
469113f04a8SDaniel Walker		} elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
470113f04a8SDaniel Walker			substr($res, $off, 1, $;);
471773647a0SAndy Whitcroft		} elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
472773647a0SAndy Whitcroft			substr($res, $off, 1, 'X');
47300df344fSAndy Whitcroft		} else {
474773647a0SAndy Whitcroft			substr($res, $off, 1, $c);
47500df344fSAndy Whitcroft		}
476c2fdda0dSAndy Whitcroft	}
477c2fdda0dSAndy Whitcroft
478113f04a8SDaniel Walker	if ($sanitise_quote eq '//') {
479113f04a8SDaniel Walker		$sanitise_quote = '';
480113f04a8SDaniel Walker	}
481113f04a8SDaniel Walker
482c2fdda0dSAndy Whitcroft	# The pathname on a #include may be surrounded by '<' and '>'.
483c45dcabdSAndy Whitcroft	if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
484c2fdda0dSAndy Whitcroft		my $clean = 'X' x length($1);
485c2fdda0dSAndy Whitcroft		$res =~ s@\<.*\>@<$clean>@;
486c2fdda0dSAndy Whitcroft
487c2fdda0dSAndy Whitcroft	# The whole of a #error is a string.
488c45dcabdSAndy Whitcroft	} elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
489c2fdda0dSAndy Whitcroft		my $clean = 'X' x length($1);
490c45dcabdSAndy Whitcroft		$res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
491c2fdda0dSAndy Whitcroft	}
492c2fdda0dSAndy Whitcroft
49300df344fSAndy Whitcroft	return $res;
49400df344fSAndy Whitcroft}
49500df344fSAndy Whitcroft
4968905a67cSAndy Whitcroftsub ctx_statement_block {
4978905a67cSAndy Whitcroft	my ($linenr, $remain, $off) = @_;
4988905a67cSAndy Whitcroft	my $line = $linenr - 1;
4998905a67cSAndy Whitcroft	my $blk = '';
5008905a67cSAndy Whitcroft	my $soff = $off;
5018905a67cSAndy Whitcroft	my $coff = $off - 1;
502773647a0SAndy Whitcroft	my $coff_set = 0;
5038905a67cSAndy Whitcroft
50413214adfSAndy Whitcroft	my $loff = 0;
50513214adfSAndy Whitcroft
5068905a67cSAndy Whitcroft	my $type = '';
5078905a67cSAndy Whitcroft	my $level = 0;
508a2750645SAndy Whitcroft	my @stack = ();
509cf655043SAndy Whitcroft	my $p;
5108905a67cSAndy Whitcroft	my $c;
5118905a67cSAndy Whitcroft	my $len = 0;
51213214adfSAndy Whitcroft
51313214adfSAndy Whitcroft	my $remainder;
5148905a67cSAndy Whitcroft	while (1) {
515a2750645SAndy Whitcroft		@stack = (['', 0]) if ($#stack == -1);
516a2750645SAndy Whitcroft
517773647a0SAndy Whitcroft		#warn "CSB: blk<$blk> remain<$remain>\n";
5188905a67cSAndy Whitcroft		# If we are about to drop off the end, pull in more
5198905a67cSAndy Whitcroft		# context.
5208905a67cSAndy Whitcroft		if ($off >= $len) {
5218905a67cSAndy Whitcroft			for (; $remain > 0; $line++) {
522dea33496SAndy Whitcroft				last if (!defined $lines[$line]);
523c2fdda0dSAndy Whitcroft				next if ($lines[$line] =~ /^-/);
5248905a67cSAndy Whitcroft				$remain--;
52513214adfSAndy Whitcroft				$loff = $len;
526c2fdda0dSAndy Whitcroft				$blk .= $lines[$line] . "\n";
5278905a67cSAndy Whitcroft				$len = length($blk);
5288905a67cSAndy Whitcroft				$line++;
5298905a67cSAndy Whitcroft				last;
5308905a67cSAndy Whitcroft			}
5318905a67cSAndy Whitcroft			# Bail if there is no further context.
5328905a67cSAndy Whitcroft			#warn "CSB: blk<$blk> off<$off> len<$len>\n";
53313214adfSAndy Whitcroft			if ($off >= $len) {
5348905a67cSAndy Whitcroft				last;
5358905a67cSAndy Whitcroft			}
5368905a67cSAndy Whitcroft		}
537cf655043SAndy Whitcroft		$p = $c;
5388905a67cSAndy Whitcroft		$c = substr($blk, $off, 1);
53913214adfSAndy Whitcroft		$remainder = substr($blk, $off);
5408905a67cSAndy Whitcroft
541773647a0SAndy Whitcroft		#warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
5424635f4fbSAndy Whitcroft
5434635f4fbSAndy Whitcroft		# Handle nested #if/#else.
5444635f4fbSAndy Whitcroft		if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
5454635f4fbSAndy Whitcroft			push(@stack, [ $type, $level ]);
5464635f4fbSAndy Whitcroft		} elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
5474635f4fbSAndy Whitcroft			($type, $level) = @{$stack[$#stack - 1]};
5484635f4fbSAndy Whitcroft		} elsif ($remainder =~ /^#\s*endif\b/) {
5494635f4fbSAndy Whitcroft			($type, $level) = @{pop(@stack)};
5504635f4fbSAndy Whitcroft		}
5514635f4fbSAndy Whitcroft
5528905a67cSAndy Whitcroft		# Statement ends at the ';' or a close '}' at the
5538905a67cSAndy Whitcroft		# outermost level.
5548905a67cSAndy Whitcroft		if ($level == 0 && $c eq ';') {
5558905a67cSAndy Whitcroft			last;
5568905a67cSAndy Whitcroft		}
5578905a67cSAndy Whitcroft
55813214adfSAndy Whitcroft		# An else is really a conditional as long as its not else if
559773647a0SAndy Whitcroft		if ($level == 0 && $coff_set == 0 &&
560773647a0SAndy Whitcroft				(!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
561773647a0SAndy Whitcroft				$remainder =~ /^(else)(?:\s|{)/ &&
562773647a0SAndy Whitcroft				$remainder !~ /^else\s+if\b/) {
563773647a0SAndy Whitcroft			$coff = $off + length($1) - 1;
564773647a0SAndy Whitcroft			$coff_set = 1;
565773647a0SAndy Whitcroft			#warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
566773647a0SAndy Whitcroft			#warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
56713214adfSAndy Whitcroft		}
56813214adfSAndy Whitcroft
5698905a67cSAndy Whitcroft		if (($type eq '' || $type eq '(') && $c eq '(') {
5708905a67cSAndy Whitcroft			$level++;
5718905a67cSAndy Whitcroft			$type = '(';
5728905a67cSAndy Whitcroft		}
5738905a67cSAndy Whitcroft		if ($type eq '(' && $c eq ')') {
5748905a67cSAndy Whitcroft			$level--;
5758905a67cSAndy Whitcroft			$type = ($level != 0)? '(' : '';
5768905a67cSAndy Whitcroft
5778905a67cSAndy Whitcroft			if ($level == 0 && $coff < $soff) {
5788905a67cSAndy Whitcroft				$coff = $off;
579773647a0SAndy Whitcroft				$coff_set = 1;
580773647a0SAndy Whitcroft				#warn "CSB: mark coff<$coff>\n";
5818905a67cSAndy Whitcroft			}
5828905a67cSAndy Whitcroft		}
5838905a67cSAndy Whitcroft		if (($type eq '' || $type eq '{') && $c eq '{') {
5848905a67cSAndy Whitcroft			$level++;
5858905a67cSAndy Whitcroft			$type = '{';
5868905a67cSAndy Whitcroft		}
5878905a67cSAndy Whitcroft		if ($type eq '{' && $c eq '}') {
5888905a67cSAndy Whitcroft			$level--;
5898905a67cSAndy Whitcroft			$type = ($level != 0)? '{' : '';
5908905a67cSAndy Whitcroft
5918905a67cSAndy Whitcroft			if ($level == 0) {
592b998e001SPatrick Pannuto				if (substr($blk, $off + 1, 1) eq ';') {
593b998e001SPatrick Pannuto					$off++;
594b998e001SPatrick Pannuto				}
5958905a67cSAndy Whitcroft				last;
5968905a67cSAndy Whitcroft			}
5978905a67cSAndy Whitcroft		}
5988905a67cSAndy Whitcroft		$off++;
5998905a67cSAndy Whitcroft	}
600a3bb97a7SAndy Whitcroft	# We are truly at the end, so shuffle to the next line.
60113214adfSAndy Whitcroft	if ($off == $len) {
602a3bb97a7SAndy Whitcroft		$loff = $len + 1;
60313214adfSAndy Whitcroft		$line++;
60413214adfSAndy Whitcroft		$remain--;
60513214adfSAndy Whitcroft	}
6068905a67cSAndy Whitcroft
6078905a67cSAndy Whitcroft	my $statement = substr($blk, $soff, $off - $soff + 1);
6088905a67cSAndy Whitcroft	my $condition = substr($blk, $soff, $coff - $soff + 1);
6098905a67cSAndy Whitcroft
6108905a67cSAndy Whitcroft	#warn "STATEMENT<$statement>\n";
6118905a67cSAndy Whitcroft	#warn "CONDITION<$condition>\n";
6128905a67cSAndy Whitcroft
613773647a0SAndy Whitcroft	#print "coff<$coff> soff<$off> loff<$loff>\n";
61413214adfSAndy Whitcroft
61513214adfSAndy Whitcroft	return ($statement, $condition,
61613214adfSAndy Whitcroft			$line, $remain + 1, $off - $loff + 1, $level);
61713214adfSAndy Whitcroft}
61813214adfSAndy Whitcroft
619cf655043SAndy Whitcroftsub statement_lines {
620cf655043SAndy Whitcroft	my ($stmt) = @_;
621cf655043SAndy Whitcroft
622cf655043SAndy Whitcroft	# Strip the diff line prefixes and rip blank lines at start and end.
623cf655043SAndy Whitcroft	$stmt =~ s/(^|\n)./$1/g;
624cf655043SAndy Whitcroft	$stmt =~ s/^\s*//;
625cf655043SAndy Whitcroft	$stmt =~ s/\s*$//;
626cf655043SAndy Whitcroft
627cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
628cf655043SAndy Whitcroft
629cf655043SAndy Whitcroft	return $#stmt_lines + 2;
630cf655043SAndy Whitcroft}
631cf655043SAndy Whitcroft
632cf655043SAndy Whitcroftsub statement_rawlines {
633cf655043SAndy Whitcroft	my ($stmt) = @_;
634cf655043SAndy Whitcroft
635cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
636cf655043SAndy Whitcroft
637cf655043SAndy Whitcroft	return $#stmt_lines + 2;
638cf655043SAndy Whitcroft}
639cf655043SAndy Whitcroft
640cf655043SAndy Whitcroftsub statement_block_size {
641cf655043SAndy Whitcroft	my ($stmt) = @_;
642cf655043SAndy Whitcroft
643cf655043SAndy Whitcroft	$stmt =~ s/(^|\n)./$1/g;
644cf655043SAndy Whitcroft	$stmt =~ s/^\s*{//;
645cf655043SAndy Whitcroft	$stmt =~ s/}\s*$//;
646cf655043SAndy Whitcroft	$stmt =~ s/^\s*//;
647cf655043SAndy Whitcroft	$stmt =~ s/\s*$//;
648cf655043SAndy Whitcroft
649cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
650cf655043SAndy Whitcroft	my @stmt_statements = ($stmt =~ /;/g);
651cf655043SAndy Whitcroft
652cf655043SAndy Whitcroft	my $stmt_lines = $#stmt_lines + 2;
653cf655043SAndy Whitcroft	my $stmt_statements = $#stmt_statements + 1;
654cf655043SAndy Whitcroft
655cf655043SAndy Whitcroft	if ($stmt_lines > $stmt_statements) {
656cf655043SAndy Whitcroft		return $stmt_lines;
657cf655043SAndy Whitcroft	} else {
658cf655043SAndy Whitcroft		return $stmt_statements;
659cf655043SAndy Whitcroft	}
660cf655043SAndy Whitcroft}
661cf655043SAndy Whitcroft
66213214adfSAndy Whitcroftsub ctx_statement_full {
66313214adfSAndy Whitcroft	my ($linenr, $remain, $off) = @_;
66413214adfSAndy Whitcroft	my ($statement, $condition, $level);
66513214adfSAndy Whitcroft
66613214adfSAndy Whitcroft	my (@chunks);
66713214adfSAndy Whitcroft
668cf655043SAndy Whitcroft	# Grab the first conditional/block pair.
66913214adfSAndy Whitcroft	($statement, $condition, $linenr, $remain, $off, $level) =
67013214adfSAndy Whitcroft				ctx_statement_block($linenr, $remain, $off);
671773647a0SAndy Whitcroft	#print "F: c<$condition> s<$statement> remain<$remain>\n";
67213214adfSAndy Whitcroft	push(@chunks, [ $condition, $statement ]);
673cf655043SAndy Whitcroft	if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
674cf655043SAndy Whitcroft		return ($level, $linenr, @chunks);
675cf655043SAndy Whitcroft	}
676cf655043SAndy Whitcroft
677cf655043SAndy Whitcroft	# Pull in the following conditional/block pairs and see if they
678cf655043SAndy Whitcroft	# could continue the statement.
679cf655043SAndy Whitcroft	for (;;) {
68013214adfSAndy Whitcroft		($statement, $condition, $linenr, $remain, $off, $level) =
68113214adfSAndy Whitcroft				ctx_statement_block($linenr, $remain, $off);
682cf655043SAndy Whitcroft		#print "C: c<$condition> s<$statement> remain<$remain>\n";
683773647a0SAndy Whitcroft		last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
684cf655043SAndy Whitcroft		#print "C: push\n";
685cf655043SAndy Whitcroft		push(@chunks, [ $condition, $statement ]);
68613214adfSAndy Whitcroft	}
68713214adfSAndy Whitcroft
68813214adfSAndy Whitcroft	return ($level, $linenr, @chunks);
6898905a67cSAndy Whitcroft}
6908905a67cSAndy Whitcroft
6914a0df2efSAndy Whitcroftsub ctx_block_get {
692f0a594c1SAndy Whitcroft	my ($linenr, $remain, $outer, $open, $close, $off) = @_;
6934a0df2efSAndy Whitcroft	my $line;
6944a0df2efSAndy Whitcroft	my $start = $linenr - 1;
6954a0df2efSAndy Whitcroft	my $blk = '';
6964a0df2efSAndy Whitcroft	my @o;
6974a0df2efSAndy Whitcroft	my @c;
6984a0df2efSAndy Whitcroft	my @res = ();
6994a0df2efSAndy Whitcroft
700f0a594c1SAndy Whitcroft	my $level = 0;
7014635f4fbSAndy Whitcroft	my @stack = ($level);
70200df344fSAndy Whitcroft	for ($line = $start; $remain > 0; $line++) {
70300df344fSAndy Whitcroft		next if ($rawlines[$line] =~ /^-/);
70400df344fSAndy Whitcroft		$remain--;
70500df344fSAndy Whitcroft
70600df344fSAndy Whitcroft		$blk .= $rawlines[$line];
7074635f4fbSAndy Whitcroft
7084635f4fbSAndy Whitcroft		# Handle nested #if/#else.
70901464f30SAndy Whitcroft		if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
7104635f4fbSAndy Whitcroft			push(@stack, $level);
71101464f30SAndy Whitcroft		} elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
7124635f4fbSAndy Whitcroft			$level = $stack[$#stack - 1];
71301464f30SAndy Whitcroft		} elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
7144635f4fbSAndy Whitcroft			$level = pop(@stack);
7154635f4fbSAndy Whitcroft		}
7164635f4fbSAndy Whitcroft
71701464f30SAndy Whitcroft		foreach my $c (split(//, $lines[$line])) {
718f0a594c1SAndy Whitcroft			##print "C<$c>L<$level><$open$close>O<$off>\n";
719f0a594c1SAndy Whitcroft			if ($off > 0) {
720f0a594c1SAndy Whitcroft				$off--;
721f0a594c1SAndy Whitcroft				next;
722f0a594c1SAndy Whitcroft			}
7234a0df2efSAndy Whitcroft
724f0a594c1SAndy Whitcroft			if ($c eq $close && $level > 0) {
725f0a594c1SAndy Whitcroft				$level--;
726f0a594c1SAndy Whitcroft				last if ($level == 0);
727f0a594c1SAndy Whitcroft			} elsif ($c eq $open) {
728f0a594c1SAndy Whitcroft				$level++;
729f0a594c1SAndy Whitcroft			}
730f0a594c1SAndy Whitcroft		}
7314a0df2efSAndy Whitcroft
732f0a594c1SAndy Whitcroft		if (!$outer || $level <= 1) {
73300df344fSAndy Whitcroft			push(@res, $rawlines[$line]);
7344a0df2efSAndy Whitcroft		}
7354a0df2efSAndy Whitcroft
736f0a594c1SAndy Whitcroft		last if ($level == 0);
7374a0df2efSAndy Whitcroft	}
7384a0df2efSAndy Whitcroft
739f0a594c1SAndy Whitcroft	return ($level, @res);
7404a0df2efSAndy Whitcroft}
7414a0df2efSAndy Whitcroftsub ctx_block_outer {
7424a0df2efSAndy Whitcroft	my ($linenr, $remain) = @_;
7434a0df2efSAndy Whitcroft
744f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
745f0a594c1SAndy Whitcroft	return @r;
7464a0df2efSAndy Whitcroft}
7474a0df2efSAndy Whitcroftsub ctx_block {
7484a0df2efSAndy Whitcroft	my ($linenr, $remain) = @_;
7494a0df2efSAndy Whitcroft
750f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
751f0a594c1SAndy Whitcroft	return @r;
752653d4876SAndy Whitcroft}
753653d4876SAndy Whitcroftsub ctx_statement {
754f0a594c1SAndy Whitcroft	my ($linenr, $remain, $off) = @_;
755f0a594c1SAndy Whitcroft
756f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
757f0a594c1SAndy Whitcroft	return @r;
758f0a594c1SAndy Whitcroft}
759f0a594c1SAndy Whitcroftsub ctx_block_level {
760653d4876SAndy Whitcroft	my ($linenr, $remain) = @_;
761653d4876SAndy Whitcroft
762f0a594c1SAndy Whitcroft	return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
7634a0df2efSAndy Whitcroft}
7649c0ca6f9SAndy Whitcroftsub ctx_statement_level {
7659c0ca6f9SAndy Whitcroft	my ($linenr, $remain, $off) = @_;
7669c0ca6f9SAndy Whitcroft
7679c0ca6f9SAndy Whitcroft	return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
7689c0ca6f9SAndy Whitcroft}
7694a0df2efSAndy Whitcroft
7704a0df2efSAndy Whitcroftsub ctx_locate_comment {
7714a0df2efSAndy Whitcroft	my ($first_line, $end_line) = @_;
7724a0df2efSAndy Whitcroft
7734a0df2efSAndy Whitcroft	# Catch a comment on the end of the line itself.
774beae6332SAndy Whitcroft	my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
7754a0df2efSAndy Whitcroft	return $current_comment if (defined $current_comment);
7764a0df2efSAndy Whitcroft
7774a0df2efSAndy Whitcroft	# Look through the context and try and figure out if there is a
7784a0df2efSAndy Whitcroft	# comment.
7794a0df2efSAndy Whitcroft	my $in_comment = 0;
7804a0df2efSAndy Whitcroft	$current_comment = '';
7814a0df2efSAndy Whitcroft	for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
78200df344fSAndy Whitcroft		my $line = $rawlines[$linenr - 1];
78300df344fSAndy Whitcroft		#warn "           $line\n";
7844a0df2efSAndy Whitcroft		if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
7854a0df2efSAndy Whitcroft			$in_comment = 1;
7864a0df2efSAndy Whitcroft		}
7874a0df2efSAndy Whitcroft		if ($line =~ m@/\*@) {
7884a0df2efSAndy Whitcroft			$in_comment = 1;
7894a0df2efSAndy Whitcroft		}
7904a0df2efSAndy Whitcroft		if (!$in_comment && $current_comment ne '') {
7914a0df2efSAndy Whitcroft			$current_comment = '';
7924a0df2efSAndy Whitcroft		}
7934a0df2efSAndy Whitcroft		$current_comment .= $line . "\n" if ($in_comment);
7944a0df2efSAndy Whitcroft		if ($line =~ m@\*/@) {
7954a0df2efSAndy Whitcroft			$in_comment = 0;
7964a0df2efSAndy Whitcroft		}
7974a0df2efSAndy Whitcroft	}
7984a0df2efSAndy Whitcroft
7994a0df2efSAndy Whitcroft	chomp($current_comment);
8004a0df2efSAndy Whitcroft	return($current_comment);
8014a0df2efSAndy Whitcroft}
8024a0df2efSAndy Whitcroftsub ctx_has_comment {
8034a0df2efSAndy Whitcroft	my ($first_line, $end_line) = @_;
8044a0df2efSAndy Whitcroft	my $cmt = ctx_locate_comment($first_line, $end_line);
8054a0df2efSAndy Whitcroft
80600df344fSAndy Whitcroft	##print "LINE: $rawlines[$end_line - 1 ]\n";
8074a0df2efSAndy Whitcroft	##print "CMMT: $cmt\n";
8084a0df2efSAndy Whitcroft
8094a0df2efSAndy Whitcroft	return ($cmt ne '');
8104a0df2efSAndy Whitcroft}
8114a0df2efSAndy Whitcroft
8124d001e4dSAndy Whitcroftsub raw_line {
8134d001e4dSAndy Whitcroft	my ($linenr, $cnt) = @_;
8144d001e4dSAndy Whitcroft
8154d001e4dSAndy Whitcroft	my $offset = $linenr - 1;
8164d001e4dSAndy Whitcroft	$cnt++;
8174d001e4dSAndy Whitcroft
8184d001e4dSAndy Whitcroft	my $line;
8194d001e4dSAndy Whitcroft	while ($cnt) {
8204d001e4dSAndy Whitcroft		$line = $rawlines[$offset++];
8214d001e4dSAndy Whitcroft		next if (defined($line) && $line =~ /^-/);
8224d001e4dSAndy Whitcroft		$cnt--;
8234d001e4dSAndy Whitcroft	}
8244d001e4dSAndy Whitcroft
8254d001e4dSAndy Whitcroft	return $line;
8264d001e4dSAndy Whitcroft}
8274d001e4dSAndy Whitcroft
8280a920b5bSAndy Whitcroftsub cat_vet {
8290a920b5bSAndy Whitcroft	my ($vet) = @_;
8309c0ca6f9SAndy Whitcroft	my ($res, $coded);
8310a920b5bSAndy Whitcroft
8329c0ca6f9SAndy Whitcroft	$res = '';
8336c72ffaaSAndy Whitcroft	while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
8346c72ffaaSAndy Whitcroft		$res .= $1;
8356c72ffaaSAndy Whitcroft		if ($2 ne '') {
8369c0ca6f9SAndy Whitcroft			$coded = sprintf("^%c", unpack('C', $2) + 64);
8376c72ffaaSAndy Whitcroft			$res .= $coded;
8386c72ffaaSAndy Whitcroft		}
8399c0ca6f9SAndy Whitcroft	}
8409c0ca6f9SAndy Whitcroft	$res =~ s/$/\$/;
8410a920b5bSAndy Whitcroft
8429c0ca6f9SAndy Whitcroft	return $res;
8430a920b5bSAndy Whitcroft}
8440a920b5bSAndy Whitcroft
845c2fdda0dSAndy Whitcroftmy $av_preprocessor = 0;
846cf655043SAndy Whitcroftmy $av_pending;
847c2fdda0dSAndy Whitcroftmy @av_paren_type;
8481f65f947SAndy Whitcroftmy $av_pend_colon;
849c2fdda0dSAndy Whitcroft
850c2fdda0dSAndy Whitcroftsub annotate_reset {
851c2fdda0dSAndy Whitcroft	$av_preprocessor = 0;
852cf655043SAndy Whitcroft	$av_pending = '_';
853cf655043SAndy Whitcroft	@av_paren_type = ('E');
8541f65f947SAndy Whitcroft	$av_pend_colon = 'O';
855c2fdda0dSAndy Whitcroft}
856c2fdda0dSAndy Whitcroft
8576c72ffaaSAndy Whitcroftsub annotate_values {
8586c72ffaaSAndy Whitcroft	my ($stream, $type) = @_;
8596c72ffaaSAndy Whitcroft
8606c72ffaaSAndy Whitcroft	my $res;
8611f65f947SAndy Whitcroft	my $var = '_' x length($stream);
8626c72ffaaSAndy Whitcroft	my $cur = $stream;
8636c72ffaaSAndy Whitcroft
864c2fdda0dSAndy Whitcroft	print "$stream\n" if ($dbg_values > 1);
8656c72ffaaSAndy Whitcroft
8666c72ffaaSAndy Whitcroft	while (length($cur)) {
867773647a0SAndy Whitcroft		@av_paren_type = ('E') if ($#av_paren_type < 0);
868cf655043SAndy Whitcroft		print " <" . join('', @av_paren_type) .
869171ae1a4SAndy Whitcroft				"> <$type> <$av_pending>" if ($dbg_values > 1);
8706c72ffaaSAndy Whitcroft		if ($cur =~ /^(\s+)/o) {
871c2fdda0dSAndy Whitcroft			print "WS($1)\n" if ($dbg_values > 1);
872c2fdda0dSAndy Whitcroft			if ($1 =~ /\n/ && $av_preprocessor) {
873cf655043SAndy Whitcroft				$type = pop(@av_paren_type);
874c2fdda0dSAndy Whitcroft				$av_preprocessor = 0;
8756c72ffaaSAndy Whitcroft			}
8766c72ffaaSAndy Whitcroft
877c023e473SFlorian Mickler		} elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
8789446ef56SAndy Whitcroft			print "CAST($1)\n" if ($dbg_values > 1);
8799446ef56SAndy Whitcroft			push(@av_paren_type, $type);
8809446ef56SAndy Whitcroft			$type = 'C';
8819446ef56SAndy Whitcroft
882e91b6e26SAndy Whitcroft		} elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
883c2fdda0dSAndy Whitcroft			print "DECLARE($1)\n" if ($dbg_values > 1);
8846c72ffaaSAndy Whitcroft			$type = 'T';
8856c72ffaaSAndy Whitcroft
886389a2fe5SAndy Whitcroft		} elsif ($cur =~ /^($Modifier)\s*/) {
887389a2fe5SAndy Whitcroft			print "MODIFIER($1)\n" if ($dbg_values > 1);
888389a2fe5SAndy Whitcroft			$type = 'T';
889389a2fe5SAndy Whitcroft
890c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
891171ae1a4SAndy Whitcroft			print "DEFINE($1,$2)\n" if ($dbg_values > 1);
892c2fdda0dSAndy Whitcroft			$av_preprocessor = 1;
893171ae1a4SAndy Whitcroft			push(@av_paren_type, $type);
894171ae1a4SAndy Whitcroft			if ($2 ne '') {
895cf655043SAndy Whitcroft				$av_pending = 'N';
896171ae1a4SAndy Whitcroft			}
897171ae1a4SAndy Whitcroft			$type = 'E';
898171ae1a4SAndy Whitcroft
899c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
900171ae1a4SAndy Whitcroft			print "UNDEF($1)\n" if ($dbg_values > 1);
901171ae1a4SAndy Whitcroft			$av_preprocessor = 1;
902171ae1a4SAndy Whitcroft			push(@av_paren_type, $type);
9036c72ffaaSAndy Whitcroft
904c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
905cf655043SAndy Whitcroft			print "PRE_START($1)\n" if ($dbg_values > 1);
906c2fdda0dSAndy Whitcroft			$av_preprocessor = 1;
907cf655043SAndy Whitcroft
908cf655043SAndy Whitcroft			push(@av_paren_type, $type);
909cf655043SAndy Whitcroft			push(@av_paren_type, $type);
910171ae1a4SAndy Whitcroft			$type = 'E';
911cf655043SAndy Whitcroft
912c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
913cf655043SAndy Whitcroft			print "PRE_RESTART($1)\n" if ($dbg_values > 1);
914cf655043SAndy Whitcroft			$av_preprocessor = 1;
915cf655043SAndy Whitcroft
916cf655043SAndy Whitcroft			push(@av_paren_type, $av_paren_type[$#av_paren_type]);
917cf655043SAndy Whitcroft
918171ae1a4SAndy Whitcroft			$type = 'E';
919cf655043SAndy Whitcroft
920c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:endif))/o) {
921cf655043SAndy Whitcroft			print "PRE_END($1)\n" if ($dbg_values > 1);
922cf655043SAndy Whitcroft
923cf655043SAndy Whitcroft			$av_preprocessor = 1;
924cf655043SAndy Whitcroft
925cf655043SAndy Whitcroft			# Assume all arms of the conditional end as this
926cf655043SAndy Whitcroft			# one does, and continue as if the #endif was not here.
927cf655043SAndy Whitcroft			pop(@av_paren_type);
928cf655043SAndy Whitcroft			push(@av_paren_type, $type);
929171ae1a4SAndy Whitcroft			$type = 'E';
9306c72ffaaSAndy Whitcroft
9316c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\\\n)/o) {
932c2fdda0dSAndy Whitcroft			print "PRECONT($1)\n" if ($dbg_values > 1);
9336c72ffaaSAndy Whitcroft
934171ae1a4SAndy Whitcroft		} elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
935171ae1a4SAndy Whitcroft			print "ATTR($1)\n" if ($dbg_values > 1);
936171ae1a4SAndy Whitcroft			$av_pending = $type;
937171ae1a4SAndy Whitcroft			$type = 'N';
938171ae1a4SAndy Whitcroft
9396c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
940c2fdda0dSAndy Whitcroft			print "SIZEOF($1)\n" if ($dbg_values > 1);
9416c72ffaaSAndy Whitcroft			if (defined $2) {
942cf655043SAndy Whitcroft				$av_pending = 'V';
9436c72ffaaSAndy Whitcroft			}
9446c72ffaaSAndy Whitcroft			$type = 'N';
9456c72ffaaSAndy Whitcroft
94614b111c1SAndy Whitcroft		} elsif ($cur =~ /^(if|while|for)\b/o) {
947c2fdda0dSAndy Whitcroft			print "COND($1)\n" if ($dbg_values > 1);
94814b111c1SAndy Whitcroft			$av_pending = 'E';
9496c72ffaaSAndy Whitcroft			$type = 'N';
9506c72ffaaSAndy Whitcroft
9511f65f947SAndy Whitcroft		} elsif ($cur =~/^(case)/o) {
9521f65f947SAndy Whitcroft			print "CASE($1)\n" if ($dbg_values > 1);
9531f65f947SAndy Whitcroft			$av_pend_colon = 'C';
9541f65f947SAndy Whitcroft			$type = 'N';
9551f65f947SAndy Whitcroft
95614b111c1SAndy Whitcroft		} elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
957c2fdda0dSAndy Whitcroft			print "KEYWORD($1)\n" if ($dbg_values > 1);
9586c72ffaaSAndy Whitcroft			$type = 'N';
9596c72ffaaSAndy Whitcroft
9606c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\()/o) {
961c2fdda0dSAndy Whitcroft			print "PAREN('$1')\n" if ($dbg_values > 1);
962cf655043SAndy Whitcroft			push(@av_paren_type, $av_pending);
963cf655043SAndy Whitcroft			$av_pending = '_';
9646c72ffaaSAndy Whitcroft			$type = 'N';
9656c72ffaaSAndy Whitcroft
9666c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\))/o) {
967cf655043SAndy Whitcroft			my $new_type = pop(@av_paren_type);
968cf655043SAndy Whitcroft			if ($new_type ne '_') {
969cf655043SAndy Whitcroft				$type = $new_type;
970c2fdda0dSAndy Whitcroft				print "PAREN('$1') -> $type\n"
971c2fdda0dSAndy Whitcroft							if ($dbg_values > 1);
9726c72ffaaSAndy Whitcroft			} else {
973c2fdda0dSAndy Whitcroft				print "PAREN('$1')\n" if ($dbg_values > 1);
9746c72ffaaSAndy Whitcroft			}
9756c72ffaaSAndy Whitcroft
976c8cb2ca3SAndy Whitcroft		} elsif ($cur =~ /^($Ident)\s*\(/o) {
977c2fdda0dSAndy Whitcroft			print "FUNC($1)\n" if ($dbg_values > 1);
978c8cb2ca3SAndy Whitcroft			$type = 'V';
979cf655043SAndy Whitcroft			$av_pending = 'V';
9806c72ffaaSAndy Whitcroft
9818e761b04SAndy Whitcroft		} elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
9828e761b04SAndy Whitcroft			if (defined $2 && $type eq 'C' || $type eq 'T') {
9831f65f947SAndy Whitcroft				$av_pend_colon = 'B';
9848e761b04SAndy Whitcroft			} elsif ($type eq 'E') {
9858e761b04SAndy Whitcroft				$av_pend_colon = 'L';
9861f65f947SAndy Whitcroft			}
9871f65f947SAndy Whitcroft			print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
9881f65f947SAndy Whitcroft			$type = 'V';
9891f65f947SAndy Whitcroft
9906c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Ident|$Constant)/o) {
991c2fdda0dSAndy Whitcroft			print "IDENT($1)\n" if ($dbg_values > 1);
9926c72ffaaSAndy Whitcroft			$type = 'V';
9936c72ffaaSAndy Whitcroft
9946c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Assignment)/o) {
995c2fdda0dSAndy Whitcroft			print "ASSIGN($1)\n" if ($dbg_values > 1);
9966c72ffaaSAndy Whitcroft			$type = 'N';
9976c72ffaaSAndy Whitcroft
998cf655043SAndy Whitcroft		} elsif ($cur =~/^(;|{|})/) {
999c2fdda0dSAndy Whitcroft			print "END($1)\n" if ($dbg_values > 1);
100013214adfSAndy Whitcroft			$type = 'E';
10011f65f947SAndy Whitcroft			$av_pend_colon = 'O';
100213214adfSAndy Whitcroft
10038e761b04SAndy Whitcroft		} elsif ($cur =~/^(,)/) {
10048e761b04SAndy Whitcroft			print "COMMA($1)\n" if ($dbg_values > 1);
10058e761b04SAndy Whitcroft			$type = 'C';
10068e761b04SAndy Whitcroft
10071f65f947SAndy Whitcroft		} elsif ($cur =~ /^(\?)/o) {
10081f65f947SAndy Whitcroft			print "QUESTION($1)\n" if ($dbg_values > 1);
10091f65f947SAndy Whitcroft			$type = 'N';
10101f65f947SAndy Whitcroft
10111f65f947SAndy Whitcroft		} elsif ($cur =~ /^(:)/o) {
10121f65f947SAndy Whitcroft			print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
10131f65f947SAndy Whitcroft
10141f65f947SAndy Whitcroft			substr($var, length($res), 1, $av_pend_colon);
10151f65f947SAndy Whitcroft			if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
10161f65f947SAndy Whitcroft				$type = 'E';
10171f65f947SAndy Whitcroft			} else {
10181f65f947SAndy Whitcroft				$type = 'N';
10191f65f947SAndy Whitcroft			}
10201f65f947SAndy Whitcroft			$av_pend_colon = 'O';
10211f65f947SAndy Whitcroft
10228e761b04SAndy Whitcroft		} elsif ($cur =~ /^(\[)/o) {
102313214adfSAndy Whitcroft			print "CLOSE($1)\n" if ($dbg_values > 1);
10246c72ffaaSAndy Whitcroft			$type = 'N';
10256c72ffaaSAndy Whitcroft
10260d413866SAndy Whitcroft		} elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
102774048ed8SAndy Whitcroft			my $variant;
102874048ed8SAndy Whitcroft
102974048ed8SAndy Whitcroft			print "OPV($1)\n" if ($dbg_values > 1);
103074048ed8SAndy Whitcroft			if ($type eq 'V') {
103174048ed8SAndy Whitcroft				$variant = 'B';
103274048ed8SAndy Whitcroft			} else {
103374048ed8SAndy Whitcroft				$variant = 'U';
103474048ed8SAndy Whitcroft			}
103574048ed8SAndy Whitcroft
103674048ed8SAndy Whitcroft			substr($var, length($res), 1, $variant);
103774048ed8SAndy Whitcroft			$type = 'N';
103874048ed8SAndy Whitcroft
10396c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Operators)/o) {
1040c2fdda0dSAndy Whitcroft			print "OP($1)\n" if ($dbg_values > 1);
10416c72ffaaSAndy Whitcroft			if ($1 ne '++' && $1 ne '--') {
10426c72ffaaSAndy Whitcroft				$type = 'N';
10436c72ffaaSAndy Whitcroft			}
10446c72ffaaSAndy Whitcroft
10456c72ffaaSAndy Whitcroft		} elsif ($cur =~ /(^.)/o) {
1046c2fdda0dSAndy Whitcroft			print "C($1)\n" if ($dbg_values > 1);
10476c72ffaaSAndy Whitcroft		}
10486c72ffaaSAndy Whitcroft		if (defined $1) {
10496c72ffaaSAndy Whitcroft			$cur = substr($cur, length($1));
10506c72ffaaSAndy Whitcroft			$res .= $type x length($1);
10516c72ffaaSAndy Whitcroft		}
10526c72ffaaSAndy Whitcroft	}
10536c72ffaaSAndy Whitcroft
10541f65f947SAndy Whitcroft	return ($res, $var);
10556c72ffaaSAndy Whitcroft}
10566c72ffaaSAndy Whitcroft
10578905a67cSAndy Whitcroftsub possible {
105813214adfSAndy Whitcroft	my ($possible, $line) = @_;
10599a974fdbSAndy Whitcroft	my $notPermitted = qr{(?:
10600776e594SAndy Whitcroft		^(?:
10610776e594SAndy Whitcroft			$Modifier|
10620776e594SAndy Whitcroft			$Storage|
10630776e594SAndy Whitcroft			$Type|
10649a974fdbSAndy Whitcroft			DEFINE_\S+
10659a974fdbSAndy Whitcroft		)$|
10669a974fdbSAndy Whitcroft		^(?:
10670776e594SAndy Whitcroft			goto|
10680776e594SAndy Whitcroft			return|
10690776e594SAndy Whitcroft			case|
10700776e594SAndy Whitcroft			else|
10710776e594SAndy Whitcroft			asm|__asm__|
10720776e594SAndy Whitcroft			do
10739a974fdbSAndy Whitcroft		)(?:\s|$)|
10740776e594SAndy Whitcroft		^(?:typedef|struct|enum)\b
10759a974fdbSAndy Whitcroft	    )}x;
10769a974fdbSAndy Whitcroft	warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
10779a974fdbSAndy Whitcroft	if ($possible !~ $notPermitted) {
1078c45dcabdSAndy Whitcroft		# Check for modifiers.
1079c45dcabdSAndy Whitcroft		$possible =~ s/\s*$Storage\s*//g;
1080c45dcabdSAndy Whitcroft		$possible =~ s/\s*$Sparse\s*//g;
1081c45dcabdSAndy Whitcroft		if ($possible =~ /^\s*$/) {
1082c45dcabdSAndy Whitcroft
1083c45dcabdSAndy Whitcroft		} elsif ($possible =~ /\s/) {
1084c45dcabdSAndy Whitcroft			$possible =~ s/\s*$Type\s*//g;
1085d2506586SAndy Whitcroft			for my $modifier (split(' ', $possible)) {
10869a974fdbSAndy Whitcroft				if ($modifier !~ $notPermitted) {
1087d2506586SAndy Whitcroft					warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1088d2506586SAndy Whitcroft					push(@modifierList, $modifier);
1089d2506586SAndy Whitcroft				}
10909a974fdbSAndy Whitcroft			}
1091c45dcabdSAndy Whitcroft
1092c45dcabdSAndy Whitcroft		} else {
109313214adfSAndy Whitcroft			warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
10948905a67cSAndy Whitcroft			push(@typeList, $possible);
1095c45dcabdSAndy Whitcroft		}
10968905a67cSAndy Whitcroft		build_types();
10970776e594SAndy Whitcroft	} else {
10980776e594SAndy Whitcroft		warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
10998905a67cSAndy Whitcroft	}
11008905a67cSAndy Whitcroft}
11018905a67cSAndy Whitcroft
11026c72ffaaSAndy Whitcroftmy $prefix = '';
11036c72ffaaSAndy Whitcroft
1104f0a594c1SAndy Whitcroftsub report {
1105773647a0SAndy Whitcroft	if (defined $tst_only && $_[0] !~ /\Q$tst_only\E/) {
1106773647a0SAndy Whitcroft		return 0;
1107773647a0SAndy Whitcroft	}
11088905a67cSAndy Whitcroft	my $line = $prefix . $_[0];
11098905a67cSAndy Whitcroft
11108905a67cSAndy Whitcroft	$line = (split('\n', $line))[0] . "\n" if ($terse);
11118905a67cSAndy Whitcroft
111213214adfSAndy Whitcroft	push(our @report, $line);
1113773647a0SAndy Whitcroft
1114773647a0SAndy Whitcroft	return 1;
1115f0a594c1SAndy Whitcroft}
1116f0a594c1SAndy Whitcroftsub report_dump {
111713214adfSAndy Whitcroft	our @report;
1118f0a594c1SAndy Whitcroft}
1119de7d4f0eSAndy Whitcroftsub ERROR {
1120773647a0SAndy Whitcroft	if (report("ERROR: $_[0]\n")) {
1121de7d4f0eSAndy Whitcroft		our $clean = 0;
11226c72ffaaSAndy Whitcroft		our $cnt_error++;
1123de7d4f0eSAndy Whitcroft	}
1124773647a0SAndy Whitcroft}
1125de7d4f0eSAndy Whitcroftsub WARN {
1126773647a0SAndy Whitcroft	if (report("WARNING: $_[0]\n")) {
1127de7d4f0eSAndy Whitcroft		our $clean = 0;
11286c72ffaaSAndy Whitcroft		our $cnt_warn++;
1129de7d4f0eSAndy Whitcroft	}
1130773647a0SAndy Whitcroft}
1131de7d4f0eSAndy Whitcroftsub CHK {
1132773647a0SAndy Whitcroft	if ($check && report("CHECK: $_[0]\n")) {
1133de7d4f0eSAndy Whitcroft		our $clean = 0;
11346c72ffaaSAndy Whitcroft		our $cnt_chk++;
11356c72ffaaSAndy Whitcroft	}
1136de7d4f0eSAndy Whitcroft}
1137de7d4f0eSAndy Whitcroft
11386ecd9674SAndy Whitcroftsub check_absolute_file {
11396ecd9674SAndy Whitcroft	my ($absolute, $herecurr) = @_;
11406ecd9674SAndy Whitcroft	my $file = $absolute;
11416ecd9674SAndy Whitcroft
11426ecd9674SAndy Whitcroft	##print "absolute<$absolute>\n";
11436ecd9674SAndy Whitcroft
11446ecd9674SAndy Whitcroft	# See if any suffix of this path is a path within the tree.
11456ecd9674SAndy Whitcroft	while ($file =~ s@^[^/]*/@@) {
11466ecd9674SAndy Whitcroft		if (-f "$root/$file") {
11476ecd9674SAndy Whitcroft			##print "file<$file>\n";
11486ecd9674SAndy Whitcroft			last;
11496ecd9674SAndy Whitcroft		}
11506ecd9674SAndy Whitcroft	}
11516ecd9674SAndy Whitcroft	if (! -f _)  {
11526ecd9674SAndy Whitcroft		return 0;
11536ecd9674SAndy Whitcroft	}
11546ecd9674SAndy Whitcroft
11556ecd9674SAndy Whitcroft	# It is, so see if the prefix is acceptable.
11566ecd9674SAndy Whitcroft	my $prefix = $absolute;
11576ecd9674SAndy Whitcroft	substr($prefix, -length($file)) = '';
11586ecd9674SAndy Whitcroft
11596ecd9674SAndy Whitcroft	##print "prefix<$prefix>\n";
11606ecd9674SAndy Whitcroft	if ($prefix ne ".../") {
11616ecd9674SAndy Whitcroft		WARN("use relative pathname instead of absolute in changelog text\n" . $herecurr);
11626ecd9674SAndy Whitcroft	}
11636ecd9674SAndy Whitcroft}
11646ecd9674SAndy Whitcroft
11650a920b5bSAndy Whitcroftsub process {
11660a920b5bSAndy Whitcroft	my $filename = shift;
11670a920b5bSAndy Whitcroft
11680a920b5bSAndy Whitcroft	my $linenr=0;
11690a920b5bSAndy Whitcroft	my $prevline="";
1170c2fdda0dSAndy Whitcroft	my $prevrawline="";
11710a920b5bSAndy Whitcroft	my $stashline="";
1172c2fdda0dSAndy Whitcroft	my $stashrawline="";
11730a920b5bSAndy Whitcroft
11744a0df2efSAndy Whitcroft	my $length;
11750a920b5bSAndy Whitcroft	my $indent;
11760a920b5bSAndy Whitcroft	my $previndent=0;
11770a920b5bSAndy Whitcroft	my $stashindent=0;
11780a920b5bSAndy Whitcroft
1179de7d4f0eSAndy Whitcroft	our $clean = 1;
11800a920b5bSAndy Whitcroft	my $signoff = 0;
11810a920b5bSAndy Whitcroft	my $is_patch = 0;
11820a920b5bSAndy Whitcroft
118313214adfSAndy Whitcroft	our @report = ();
11846c72ffaaSAndy Whitcroft	our $cnt_lines = 0;
11856c72ffaaSAndy Whitcroft	our $cnt_error = 0;
11866c72ffaaSAndy Whitcroft	our $cnt_warn = 0;
11876c72ffaaSAndy Whitcroft	our $cnt_chk = 0;
11886c72ffaaSAndy Whitcroft
11890a920b5bSAndy Whitcroft	# Trace the real file/line as we go.
11900a920b5bSAndy Whitcroft	my $realfile = '';
11910a920b5bSAndy Whitcroft	my $realline = 0;
11920a920b5bSAndy Whitcroft	my $realcnt = 0;
11930a920b5bSAndy Whitcroft	my $here = '';
11940a920b5bSAndy Whitcroft	my $in_comment = 0;
1195c2fdda0dSAndy Whitcroft	my $comment_edge = 0;
11960a920b5bSAndy Whitcroft	my $first_line = 0;
11971e855726SWolfram Sang	my $p1_prefix = '';
11980a920b5bSAndy Whitcroft
119913214adfSAndy Whitcroft	my $prev_values = 'E';
120013214adfSAndy Whitcroft
120113214adfSAndy Whitcroft	# suppression flags
1202773647a0SAndy Whitcroft	my %suppress_ifbraces;
1203170d3a22SAndy Whitcroft	my %suppress_whiletrailers;
12042b474a1aSAndy Whitcroft	my %suppress_export;
1205653d4876SAndy Whitcroft
1206c2fdda0dSAndy Whitcroft	# Pre-scan the patch sanitizing the lines.
1207de7d4f0eSAndy Whitcroft	# Pre-scan the patch looking for any __setup documentation.
1208c2fdda0dSAndy Whitcroft	#
1209de7d4f0eSAndy Whitcroft	my @setup_docs = ();
1210de7d4f0eSAndy Whitcroft	my $setup_docs = 0;
1211773647a0SAndy Whitcroft
1212773647a0SAndy Whitcroft	sanitise_line_reset();
1213c2fdda0dSAndy Whitcroft	my $line;
1214c2fdda0dSAndy Whitcroft	foreach my $rawline (@rawlines) {
1215773647a0SAndy Whitcroft		$linenr++;
1216773647a0SAndy Whitcroft		$line = $rawline;
1217c2fdda0dSAndy Whitcroft
1218773647a0SAndy Whitcroft		if ($rawline=~/^\+\+\+\s+(\S+)/) {
1219de7d4f0eSAndy Whitcroft			$setup_docs = 0;
1220de7d4f0eSAndy Whitcroft			if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
1221de7d4f0eSAndy Whitcroft				$setup_docs = 1;
1222de7d4f0eSAndy Whitcroft			}
1223773647a0SAndy Whitcroft			#next;
1224de7d4f0eSAndy Whitcroft		}
1225773647a0SAndy Whitcroft		if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1226773647a0SAndy Whitcroft			$realline=$1-1;
1227773647a0SAndy Whitcroft			if (defined $2) {
1228773647a0SAndy Whitcroft				$realcnt=$3+1;
1229773647a0SAndy Whitcroft			} else {
1230773647a0SAndy Whitcroft				$realcnt=1+1;
1231773647a0SAndy Whitcroft			}
1232c45dcabdSAndy Whitcroft			$in_comment = 0;
1233773647a0SAndy Whitcroft
1234773647a0SAndy Whitcroft			# Guestimate if this is a continuing comment.  Run
1235773647a0SAndy Whitcroft			# the context looking for a comment "edge".  If this
1236773647a0SAndy Whitcroft			# edge is a close comment then we must be in a comment
1237773647a0SAndy Whitcroft			# at context start.
1238773647a0SAndy Whitcroft			my $edge;
123901fa9147SAndy Whitcroft			my $cnt = $realcnt;
124001fa9147SAndy Whitcroft			for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
124101fa9147SAndy Whitcroft				next if (defined $rawlines[$ln - 1] &&
124201fa9147SAndy Whitcroft					 $rawlines[$ln - 1] =~ /^-/);
124301fa9147SAndy Whitcroft				$cnt--;
124401fa9147SAndy Whitcroft				#print "RAW<$rawlines[$ln - 1]>\n";
1245721c1cb6SAndy Whitcroft				last if (!defined $rawlines[$ln - 1]);
1246fae17daeSAndy Whitcroft				if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
1247fae17daeSAndy Whitcroft				    $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
1248fae17daeSAndy Whitcroft					($edge) = $1;
1249fae17daeSAndy Whitcroft					last;
1250fae17daeSAndy Whitcroft				}
1251773647a0SAndy Whitcroft			}
1252773647a0SAndy Whitcroft			if (defined $edge && $edge eq '*/') {
1253773647a0SAndy Whitcroft				$in_comment = 1;
1254773647a0SAndy Whitcroft			}
1255773647a0SAndy Whitcroft
1256773647a0SAndy Whitcroft			# Guestimate if this is a continuing comment.  If this
1257773647a0SAndy Whitcroft			# is the start of a diff block and this line starts
1258773647a0SAndy Whitcroft			# ' *' then it is very likely a comment.
1259773647a0SAndy Whitcroft			if (!defined $edge &&
126083242e0cSAndy Whitcroft			    $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
1261773647a0SAndy Whitcroft			{
1262773647a0SAndy Whitcroft				$in_comment = 1;
1263773647a0SAndy Whitcroft			}
1264773647a0SAndy Whitcroft
1265773647a0SAndy Whitcroft			##print "COMMENT:$in_comment edge<$edge> $rawline\n";
1266773647a0SAndy Whitcroft			sanitise_line_reset($in_comment);
1267773647a0SAndy Whitcroft
1268171ae1a4SAndy Whitcroft		} elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
1269773647a0SAndy Whitcroft			# Standardise the strings and chars within the input to
1270171ae1a4SAndy Whitcroft			# simplify matching -- only bother with positive lines.
1271773647a0SAndy Whitcroft			$line = sanitise_line($rawline);
1272773647a0SAndy Whitcroft		}
1273773647a0SAndy Whitcroft		push(@lines, $line);
1274773647a0SAndy Whitcroft
1275773647a0SAndy Whitcroft		if ($realcnt > 1) {
1276773647a0SAndy Whitcroft			$realcnt-- if ($line =~ /^(?:\+| |$)/);
1277773647a0SAndy Whitcroft		} else {
1278773647a0SAndy Whitcroft			$realcnt = 0;
1279773647a0SAndy Whitcroft		}
1280773647a0SAndy Whitcroft
1281773647a0SAndy Whitcroft		#print "==>$rawline\n";
1282773647a0SAndy Whitcroft		#print "-->$line\n";
1283de7d4f0eSAndy Whitcroft
1284de7d4f0eSAndy Whitcroft		if ($setup_docs && $line =~ /^\+/) {
1285de7d4f0eSAndy Whitcroft			push(@setup_docs, $line);
1286de7d4f0eSAndy Whitcroft		}
1287de7d4f0eSAndy Whitcroft	}
1288de7d4f0eSAndy Whitcroft
12896c72ffaaSAndy Whitcroft	$prefix = '';
12906c72ffaaSAndy Whitcroft
1291773647a0SAndy Whitcroft	$realcnt = 0;
1292773647a0SAndy Whitcroft	$linenr = 0;
12930a920b5bSAndy Whitcroft	foreach my $line (@lines) {
12940a920b5bSAndy Whitcroft		$linenr++;
12950a920b5bSAndy Whitcroft
1296c2fdda0dSAndy Whitcroft		my $rawline = $rawlines[$linenr - 1];
12976c72ffaaSAndy Whitcroft
12980a920b5bSAndy Whitcroft#extract the line range in the file after the patch is applied
12996c72ffaaSAndy Whitcroft		if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
13000a920b5bSAndy Whitcroft			$is_patch = 1;
13014a0df2efSAndy Whitcroft			$first_line = $linenr + 1;
13020a920b5bSAndy Whitcroft			$realline=$1-1;
13030a920b5bSAndy Whitcroft			if (defined $2) {
13040a920b5bSAndy Whitcroft				$realcnt=$3+1;
13050a920b5bSAndy Whitcroft			} else {
13060a920b5bSAndy Whitcroft				$realcnt=1+1;
13070a920b5bSAndy Whitcroft			}
1308c2fdda0dSAndy Whitcroft			annotate_reset();
130913214adfSAndy Whitcroft			$prev_values = 'E';
131013214adfSAndy Whitcroft
1311773647a0SAndy Whitcroft			%suppress_ifbraces = ();
1312170d3a22SAndy Whitcroft			%suppress_whiletrailers = ();
13132b474a1aSAndy Whitcroft			%suppress_export = ();
13140a920b5bSAndy Whitcroft			next;
13150a920b5bSAndy Whitcroft
13164a0df2efSAndy Whitcroft# track the line number as we move through the hunk, note that
13174a0df2efSAndy Whitcroft# new versions of GNU diff omit the leading space on completely
13184a0df2efSAndy Whitcroft# blank context lines so we need to count that too.
1319773647a0SAndy Whitcroft		} elsif ($line =~ /^( |\+|$)/) {
13200a920b5bSAndy Whitcroft			$realline++;
1321d8aaf121SAndy Whitcroft			$realcnt-- if ($realcnt != 0);
13220a920b5bSAndy Whitcroft
13234a0df2efSAndy Whitcroft			# Measure the line length and indent.
1324c2fdda0dSAndy Whitcroft			($length, $indent) = line_stats($rawline);
13250a920b5bSAndy Whitcroft
13260a920b5bSAndy Whitcroft			# Track the previous line.
13270a920b5bSAndy Whitcroft			($prevline, $stashline) = ($stashline, $line);
13280a920b5bSAndy Whitcroft			($previndent, $stashindent) = ($stashindent, $indent);
1329c2fdda0dSAndy Whitcroft			($prevrawline, $stashrawline) = ($stashrawline, $rawline);
1330c2fdda0dSAndy Whitcroft
1331773647a0SAndy Whitcroft			#warn "line<$line>\n";
13326c72ffaaSAndy Whitcroft
1333d8aaf121SAndy Whitcroft		} elsif ($realcnt == 1) {
1334d8aaf121SAndy Whitcroft			$realcnt--;
13350a920b5bSAndy Whitcroft		}
13360a920b5bSAndy Whitcroft
1337cc77cdcaSAndy Whitcroft		my $hunk_line = ($realcnt != 0);
1338cc77cdcaSAndy Whitcroft
13390a920b5bSAndy Whitcroft#make up the handle for any error we report on this line
1340773647a0SAndy Whitcroft		$prefix = "$filename:$realline: " if ($emacs && $file);
1341773647a0SAndy Whitcroft		$prefix = "$filename:$linenr: " if ($emacs && !$file);
1342773647a0SAndy Whitcroft
13436c72ffaaSAndy Whitcroft		$here = "#$linenr: " if (!$file);
13446c72ffaaSAndy Whitcroft		$here = "#$realline: " if ($file);
1345773647a0SAndy Whitcroft
1346773647a0SAndy Whitcroft		# extract the filename as it passes
13473bf9a009SRabin Vincent		if ($line =~ /^diff --git.*?(\S+)$/) {
13483bf9a009SRabin Vincent			$realfile = $1;
13493bf9a009SRabin Vincent			$realfile =~ s@^([^/]*)/@@;
13503bf9a009SRabin Vincent
13513bf9a009SRabin Vincent		} elsif ($line =~ /^\+\+\+\s+(\S+)/) {
1352773647a0SAndy Whitcroft			$realfile = $1;
13531e855726SWolfram Sang			$realfile =~ s@^([^/]*)/@@;
13541e855726SWolfram Sang
13551e855726SWolfram Sang			$p1_prefix = $1;
1356e2f7aa4bSAndy Whitcroft			if (!$file && $tree && $p1_prefix ne '' &&
1357e2f7aa4bSAndy Whitcroft			    -e "$root/$p1_prefix") {
13581e855726SWolfram Sang				WARN("patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
13591e855726SWolfram Sang			}
1360773647a0SAndy Whitcroft
1361c1ab3326SAndy Whitcroft			if ($realfile =~ m@^include/asm/@) {
1362773647a0SAndy Whitcroft				ERROR("do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
1363773647a0SAndy Whitcroft			}
1364773647a0SAndy Whitcroft			next;
1365773647a0SAndy Whitcroft		}
1366773647a0SAndy Whitcroft
1367389834b6SRandy Dunlap		$here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
13680a920b5bSAndy Whitcroft
1369c2fdda0dSAndy Whitcroft		my $hereline = "$here\n$rawline\n";
1370c2fdda0dSAndy Whitcroft		my $herecurr = "$here\n$rawline\n";
1371c2fdda0dSAndy Whitcroft		my $hereprev = "$here\n$prevrawline\n$rawline\n";
13720a920b5bSAndy Whitcroft
13736c72ffaaSAndy Whitcroft		$cnt_lines++ if ($realcnt != 0);
13746c72ffaaSAndy Whitcroft
13753bf9a009SRabin Vincent# Check for incorrect file permissions
13763bf9a009SRabin Vincent		if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
13773bf9a009SRabin Vincent			my $permhere = $here . "FILE: $realfile\n";
13783bf9a009SRabin Vincent			if ($realfile =~ /(Makefile|Kconfig|\.c|\.h|\.S|\.tmpl)$/) {
13793bf9a009SRabin Vincent				ERROR("do not set execute permissions for source files\n" . $permhere);
13803bf9a009SRabin Vincent			}
13813bf9a009SRabin Vincent		}
13823bf9a009SRabin Vincent
13830a920b5bSAndy Whitcroft#check the patch for a signoff:
1384d8aaf121SAndy Whitcroft		if ($line =~ /^\s*signed-off-by:/i) {
13854a0df2efSAndy Whitcroft			# This is a signoff, if ugly, so do not double report.
13864a0df2efSAndy Whitcroft			$signoff++;
13870a920b5bSAndy Whitcroft			if (!($line =~ /^\s*Signed-off-by:/)) {
1388de7d4f0eSAndy Whitcroft				WARN("Signed-off-by: is the preferred form\n" .
1389de7d4f0eSAndy Whitcroft					$herecurr);
13900a920b5bSAndy Whitcroft			}
13910a920b5bSAndy Whitcroft			if ($line =~ /^\s*signed-off-by:\S/i) {
1392773647a0SAndy Whitcroft				WARN("space required after Signed-off-by:\n" .
1393de7d4f0eSAndy Whitcroft					$herecurr);
13940a920b5bSAndy Whitcroft			}
13950a920b5bSAndy Whitcroft		}
13960a920b5bSAndy Whitcroft
139700df344fSAndy Whitcroft# Check for wrappage within a valid hunk of the file
13988905a67cSAndy Whitcroft		if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
1399de7d4f0eSAndy Whitcroft			ERROR("patch seems to be corrupt (line wrapped?)\n" .
14006c72ffaaSAndy Whitcroft				$herecurr) if (!$emitted_corrupt++);
1401de7d4f0eSAndy Whitcroft		}
1402de7d4f0eSAndy Whitcroft
14036ecd9674SAndy Whitcroft# Check for absolute kernel paths.
14046ecd9674SAndy Whitcroft		if ($tree) {
14056ecd9674SAndy Whitcroft			while ($line =~ m{(?:^|\s)(/\S*)}g) {
14066ecd9674SAndy Whitcroft				my $file = $1;
14076ecd9674SAndy Whitcroft
14086ecd9674SAndy Whitcroft				if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
14096ecd9674SAndy Whitcroft				    check_absolute_file($1, $herecurr)) {
14106ecd9674SAndy Whitcroft					#
14116ecd9674SAndy Whitcroft				} else {
14126ecd9674SAndy Whitcroft					check_absolute_file($file, $herecurr);
14136ecd9674SAndy Whitcroft				}
14146ecd9674SAndy Whitcroft			}
14156ecd9674SAndy Whitcroft		}
14166ecd9674SAndy Whitcroft
1417de7d4f0eSAndy Whitcroft# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
1418de7d4f0eSAndy Whitcroft		if (($realfile =~ /^$/ || $line =~ /^\+/) &&
1419171ae1a4SAndy Whitcroft		    $rawline !~ m/^$UTF8*$/) {
1420171ae1a4SAndy Whitcroft			my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
1421171ae1a4SAndy Whitcroft
1422171ae1a4SAndy Whitcroft			my $blank = copy_spacing($rawline);
1423171ae1a4SAndy Whitcroft			my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
1424171ae1a4SAndy Whitcroft			my $hereptr = "$hereline$ptr\n";
1425171ae1a4SAndy Whitcroft
1426171ae1a4SAndy Whitcroft			ERROR("Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
142700df344fSAndy Whitcroft		}
14280a920b5bSAndy Whitcroft
142930670854SAndy Whitcroft# ignore non-hunk lines and lines being removed
143030670854SAndy Whitcroft		next if (!$hunk_line || $line =~ /^-/);
143100df344fSAndy Whitcroft
14320a920b5bSAndy Whitcroft#trailing whitespace
14339c0ca6f9SAndy Whitcroft		if ($line =~ /^\+.*\015/) {
1434c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
14359c0ca6f9SAndy Whitcroft			ERROR("DOS line endings\n" . $herevet);
14369c0ca6f9SAndy Whitcroft
1437c2fdda0dSAndy Whitcroft		} elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
1438c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1439de7d4f0eSAndy Whitcroft			ERROR("trailing whitespace\n" . $herevet);
1440d2c0a235SAndy Whitcroft			$rpt_cleaners = 1;
14410a920b5bSAndy Whitcroft		}
14425368df20SAndy Whitcroft
14433354957aSAndi Kleen# check for Kconfig help text having a real description
14449fe287d7SAndy Whitcroft# Only applies when adding the entry originally, after that we do not have
14459fe287d7SAndy Whitcroft# sufficient context to determine whether it is indeed long enough.
14463354957aSAndi Kleen		if ($realfile =~ /Kconfig/ &&
14479fe287d7SAndy Whitcroft		    $line =~ /\+\s*(?:---)?help(?:---)?$/) {
14483354957aSAndi Kleen			my $length = 0;
14499fe287d7SAndy Whitcroft			my $cnt = $realcnt;
14509fe287d7SAndy Whitcroft			my $ln = $linenr + 1;
14519fe287d7SAndy Whitcroft			my $f;
14529fe287d7SAndy Whitcroft			my $is_end = 0;
14539fe287d7SAndy Whitcroft			while ($cnt > 0 && defined $lines[$ln - 1]) {
14549fe287d7SAndy Whitcroft				$f = $lines[$ln - 1];
14559fe287d7SAndy Whitcroft				$cnt-- if ($lines[$ln - 1] !~ /^-/);
14569fe287d7SAndy Whitcroft				$is_end = $lines[$ln - 1] =~ /^\+/;
14579fe287d7SAndy Whitcroft				$ln++;
14589fe287d7SAndy Whitcroft
14599fe287d7SAndy Whitcroft				next if ($f =~ /^-/);
14609fe287d7SAndy Whitcroft				$f =~ s/^.//;
14613354957aSAndi Kleen				$f =~ s/#.*//;
14623354957aSAndi Kleen				$f =~ s/^\s+//;
14633354957aSAndi Kleen				next if ($f =~ /^$/);
14649fe287d7SAndy Whitcroft				if ($f =~ /^\s*config\s/) {
14659fe287d7SAndy Whitcroft					$is_end = 1;
14669fe287d7SAndy Whitcroft					last;
14679fe287d7SAndy Whitcroft				}
14683354957aSAndi Kleen				$length++;
14693354957aSAndi Kleen			}
14709fe287d7SAndy Whitcroft			WARN("please write a paragraph that describes the config symbol fully\n" . $herecurr) if ($is_end && $length < 4);
14719fe287d7SAndy Whitcroft			#print "is_end<$is_end> length<$length>\n";
14723354957aSAndi Kleen		}
14733354957aSAndi Kleen
14745368df20SAndy Whitcroft# check we are in a valid source file if not then ignore this hunk
14755368df20SAndy Whitcroft		next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
14765368df20SAndy Whitcroft
14770a920b5bSAndy Whitcroft#80 column limit
1478c45dcabdSAndy Whitcroft		if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ &&
1479f4c014c0SAndy Whitcroft		    $rawline !~ /^.\s*\*\s*\@$Ident\s/ &&
14800fccc622SJoe Perches		    !($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(KERN_\S+\s*|[^"]*))?"[X\t]*"\s*(?:|,|\)\s*;)\s*$/ ||
14818bbea968SJoe Perches		    $line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) &&
1482f4c014c0SAndy Whitcroft		    $length > 80)
1483c45dcabdSAndy Whitcroft		{
1484de7d4f0eSAndy Whitcroft			WARN("line over 80 characters\n" . $herecurr);
14850a920b5bSAndy Whitcroft		}
14860a920b5bSAndy Whitcroft
14875e79d96eSJoe Perches# check for spaces before a quoted newline
14885e79d96eSJoe Perches		if ($rawline =~ /^.*\".*\s\\n/) {
14895e79d96eSJoe Perches			WARN("unnecessary whitespace before a quoted newline\n" . $herecurr);
14905e79d96eSJoe Perches		}
14915e79d96eSJoe Perches
14928905a67cSAndy Whitcroft# check for adding lines without a newline.
14938905a67cSAndy Whitcroft		if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
14948905a67cSAndy Whitcroft			WARN("adding a line without newline at end of file\n" . $herecurr);
14958905a67cSAndy Whitcroft		}
14968905a67cSAndy Whitcroft
149742e41c54SMike Frysinger# Blackfin: use hi/lo macros
149842e41c54SMike Frysinger		if ($realfile =~ m@arch/blackfin/.*\.S$@) {
149942e41c54SMike Frysinger			if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
150042e41c54SMike Frysinger				my $herevet = "$here\n" . cat_vet($line) . "\n";
150142e41c54SMike Frysinger				ERROR("use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
150242e41c54SMike Frysinger			}
150342e41c54SMike Frysinger			if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
150442e41c54SMike Frysinger				my $herevet = "$here\n" . cat_vet($line) . "\n";
150542e41c54SMike Frysinger				ERROR("use the HI() macro, not (... >> 16)\n" . $herevet);
150642e41c54SMike Frysinger			}
150742e41c54SMike Frysinger		}
150842e41c54SMike Frysinger
1509b9ea10d6SAndy Whitcroft# check we are in a valid source file C or perl if not then ignore this hunk
1510b9ea10d6SAndy Whitcroft		next if ($realfile !~ /\.(h|c|pl)$/);
15110a920b5bSAndy Whitcroft
15120a920b5bSAndy Whitcroft# at the beginning of a line any tabs must come first and anything
15130a920b5bSAndy Whitcroft# more than 8 must use tabs.
1514c2fdda0dSAndy Whitcroft		if ($rawline =~ /^\+\s* \t\s*\S/ ||
1515c2fdda0dSAndy Whitcroft		    $rawline =~ /^\+\s*        \s*/) {
1516c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1517171ae1a4SAndy Whitcroft			ERROR("code indent should use tabs where possible\n" . $herevet);
1518d2c0a235SAndy Whitcroft			$rpt_cleaners = 1;
15190a920b5bSAndy Whitcroft		}
15200a920b5bSAndy Whitcroft
152108e44365SAlberto Panizzo# check for space before tabs.
152208e44365SAlberto Panizzo		if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
152308e44365SAlberto Panizzo			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
152408e44365SAlberto Panizzo			WARN("please, no space before tabs\n" . $herevet);
152508e44365SAlberto Panizzo		}
152608e44365SAlberto Panizzo
15275f7ddae6SRaffaele Recalcati# check for spaces at the beginning of a line.
15286b4c5bebSAndy Whitcroft# Exceptions:
15296b4c5bebSAndy Whitcroft#  1) within comments
15306b4c5bebSAndy Whitcroft#  2) indented preprocessor commands
15316b4c5bebSAndy Whitcroft#  3) hanging labels
15326b4c5bebSAndy Whitcroft		if ($rawline =~ /^\+ / && $line !~ /\+ *(?:$;|#|$Ident:)/)  {
15335f7ddae6SRaffaele Recalcati			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
15346b4c5bebSAndy Whitcroft			WARN("please, no spaces at the start of a line\n" . $herevet);
15355f7ddae6SRaffaele Recalcati		}
15365f7ddae6SRaffaele Recalcati
1537b9ea10d6SAndy Whitcroft# check we are in a valid C source file if not then ignore this hunk
1538b9ea10d6SAndy Whitcroft		next if ($realfile !~ /\.(h|c)$/);
1539b9ea10d6SAndy Whitcroft
1540c2fdda0dSAndy Whitcroft# check for RCS/CVS revision markers
1541cf655043SAndy Whitcroft		if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
1542c2fdda0dSAndy Whitcroft			WARN("CVS style keyword markers, these will _not_ be updated\n". $herecurr);
1543c2fdda0dSAndy Whitcroft		}
154422f2a2efSAndy Whitcroft
154542e41c54SMike Frysinger# Blackfin: don't use __builtin_bfin_[cs]sync
154642e41c54SMike Frysinger		if ($line =~ /__builtin_bfin_csync/) {
154742e41c54SMike Frysinger			my $herevet = "$here\n" . cat_vet($line) . "\n";
154842e41c54SMike Frysinger			ERROR("use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
154942e41c54SMike Frysinger		}
155042e41c54SMike Frysinger		if ($line =~ /__builtin_bfin_ssync/) {
155142e41c54SMike Frysinger			my $herevet = "$here\n" . cat_vet($line) . "\n";
155242e41c54SMike Frysinger			ERROR("use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
155342e41c54SMike Frysinger		}
155442e41c54SMike Frysinger
15559c0ca6f9SAndy Whitcroft# Check for potential 'bare' types
15562b474a1aSAndy Whitcroft		my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
15572b474a1aSAndy Whitcroft		    $realline_next);
15589c9ba34eSAndy Whitcroft		if ($realcnt && $line =~ /.\s*\S/) {
1559170d3a22SAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
1560f5fe35ddSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0);
1561171ae1a4SAndy Whitcroft			$stat =~ s/\n./\n /g;
1562171ae1a4SAndy Whitcroft			$cond =~ s/\n./\n /g;
1563171ae1a4SAndy Whitcroft
15642b474a1aSAndy Whitcroft			# Find the real next line.
15652b474a1aSAndy Whitcroft			$realline_next = $line_nr_next;
15662b474a1aSAndy Whitcroft			if (defined $realline_next &&
15672b474a1aSAndy Whitcroft			    (!defined $lines[$realline_next - 1] ||
15682b474a1aSAndy Whitcroft			     substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
15692b474a1aSAndy Whitcroft				$realline_next++;
15702b474a1aSAndy Whitcroft			}
15712b474a1aSAndy Whitcroft
1572171ae1a4SAndy Whitcroft			my $s = $stat;
1573171ae1a4SAndy Whitcroft			$s =~ s/{.*$//s;
1574cf655043SAndy Whitcroft
1575c2fdda0dSAndy Whitcroft			# Ignore goto labels.
1576171ae1a4SAndy Whitcroft			if ($s =~ /$Ident:\*$/s) {
1577c2fdda0dSAndy Whitcroft
1578c2fdda0dSAndy Whitcroft			# Ignore functions being called
1579171ae1a4SAndy Whitcroft			} elsif ($s =~ /^.\s*$Ident\s*\(/s) {
1580c2fdda0dSAndy Whitcroft
1581463f2864SAndy Whitcroft			} elsif ($s =~ /^.\s*else\b/s) {
1582463f2864SAndy Whitcroft
1583c45dcabdSAndy Whitcroft			# declarations always start with types
1584d2506586SAndy 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) {
1585c45dcabdSAndy Whitcroft				my $type = $1;
1586c45dcabdSAndy Whitcroft				$type =~ s/\s+/ /g;
1587c45dcabdSAndy Whitcroft				possible($type, "A:" . $s);
1588c45dcabdSAndy Whitcroft
15896c72ffaaSAndy Whitcroft			# definitions in global scope can only start with types
1590a6a84062SAndy Whitcroft			} elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
1591c45dcabdSAndy Whitcroft				possible($1, "B:" . $s);
1592c2fdda0dSAndy Whitcroft			}
15938905a67cSAndy Whitcroft
15946c72ffaaSAndy Whitcroft			# any (foo ... *) is a pointer cast, and foo is a type
159565863862SAndy Whitcroft			while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
1596c45dcabdSAndy Whitcroft				possible($1, "C:" . $s);
15979c0ca6f9SAndy Whitcroft			}
15988905a67cSAndy Whitcroft
15998905a67cSAndy Whitcroft			# Check for any sort of function declaration.
16008905a67cSAndy Whitcroft			# int foo(something bar, other baz);
16018905a67cSAndy Whitcroft			# void (*store_gdt)(x86_descr_ptr *);
1602171ae1a4SAndy Whitcroft			if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
16038905a67cSAndy Whitcroft				my ($name_len) = length($1);
16048905a67cSAndy Whitcroft
1605cf655043SAndy Whitcroft				my $ctx = $s;
1606773647a0SAndy Whitcroft				substr($ctx, 0, $name_len + 1, '');
16078905a67cSAndy Whitcroft				$ctx =~ s/\)[^\)]*$//;
1608cf655043SAndy Whitcroft
16098905a67cSAndy Whitcroft				for my $arg (split(/\s*,\s*/, $ctx)) {
1610c45dcabdSAndy Whitcroft					if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
16118905a67cSAndy Whitcroft
1612c45dcabdSAndy Whitcroft						possible($1, "D:" . $s);
16138905a67cSAndy Whitcroft					}
16148905a67cSAndy Whitcroft				}
16158905a67cSAndy Whitcroft			}
16168905a67cSAndy Whitcroft
16179c0ca6f9SAndy Whitcroft		}
16189c0ca6f9SAndy Whitcroft
161900df344fSAndy Whitcroft#
162000df344fSAndy Whitcroft# Checks which may be anchored in the context.
162100df344fSAndy Whitcroft#
162200df344fSAndy Whitcroft
162300df344fSAndy Whitcroft# Check for switch () and associated case and default
162400df344fSAndy Whitcroft# statements should be at the same indent.
162500df344fSAndy Whitcroft		if ($line=~/\bswitch\s*\(.*\)/) {
162600df344fSAndy Whitcroft			my $err = '';
162700df344fSAndy Whitcroft			my $sep = '';
162800df344fSAndy Whitcroft			my @ctx = ctx_block_outer($linenr, $realcnt);
162900df344fSAndy Whitcroft			shift(@ctx);
163000df344fSAndy Whitcroft			for my $ctx (@ctx) {
163100df344fSAndy Whitcroft				my ($clen, $cindent) = line_stats($ctx);
163200df344fSAndy Whitcroft				if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
163300df344fSAndy Whitcroft							$indent != $cindent) {
163400df344fSAndy Whitcroft					$err .= "$sep$ctx\n";
163500df344fSAndy Whitcroft					$sep = '';
163600df344fSAndy Whitcroft				} else {
163700df344fSAndy Whitcroft					$sep = "[...]\n";
163800df344fSAndy Whitcroft				}
163900df344fSAndy Whitcroft			}
164000df344fSAndy Whitcroft			if ($err ne '') {
16419c0ca6f9SAndy Whitcroft				ERROR("switch and case should be at the same indent\n$hereline$err");
1642de7d4f0eSAndy Whitcroft			}
1643de7d4f0eSAndy Whitcroft		}
1644de7d4f0eSAndy Whitcroft
1645de7d4f0eSAndy Whitcroft# if/while/etc brace do not go on next line, unless defining a do while loop,
1646de7d4f0eSAndy Whitcroft# or if that brace on the next line is for something else
1647c45dcabdSAndy Whitcroft		if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
1648773647a0SAndy Whitcroft			my $pre_ctx = "$1$2";
1649773647a0SAndy Whitcroft
16509c0ca6f9SAndy Whitcroft			my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
1651de7d4f0eSAndy Whitcroft			my $ctx_cnt = $realcnt - $#ctx - 1;
1652de7d4f0eSAndy Whitcroft			my $ctx = join("\n", @ctx);
1653de7d4f0eSAndy Whitcroft
1654548596d5SAndy Whitcroft			my $ctx_ln = $linenr;
1655548596d5SAndy Whitcroft			my $ctx_skip = $realcnt;
1656de7d4f0eSAndy Whitcroft
1657548596d5SAndy Whitcroft			while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
1658548596d5SAndy Whitcroft					defined $lines[$ctx_ln - 1] &&
1659548596d5SAndy Whitcroft					$lines[$ctx_ln - 1] =~ /^-/)) {
1660548596d5SAndy Whitcroft				##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
1661548596d5SAndy Whitcroft				$ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
1662773647a0SAndy Whitcroft				$ctx_ln++;
1663773647a0SAndy Whitcroft			}
1664548596d5SAndy Whitcroft
166553210168SAndy Whitcroft			#print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
166653210168SAndy Whitcroft			#print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
1667773647a0SAndy Whitcroft
1668773647a0SAndy Whitcroft			if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln -1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
1669773647a0SAndy Whitcroft				ERROR("that open brace { should be on the previous line\n" .
167001464f30SAndy Whitcroft					"$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
167100df344fSAndy Whitcroft			}
1672773647a0SAndy Whitcroft			if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
1673773647a0SAndy Whitcroft			    $ctx =~ /\)\s*\;\s*$/ &&
1674773647a0SAndy Whitcroft			    defined $lines[$ctx_ln - 1])
1675773647a0SAndy Whitcroft			{
16769c0ca6f9SAndy Whitcroft				my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
16779c0ca6f9SAndy Whitcroft				if ($nindent > $indent) {
1678773647a0SAndy Whitcroft					WARN("trailing semicolon indicates no statements, indent implies otherwise\n" .
167901464f30SAndy Whitcroft						"$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
16809c0ca6f9SAndy Whitcroft				}
16819c0ca6f9SAndy Whitcroft			}
168200df344fSAndy Whitcroft		}
168300df344fSAndy Whitcroft
16844d001e4dSAndy Whitcroft# Check relative indent for conditionals and blocks.
16854d001e4dSAndy Whitcroft		if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
16864d001e4dSAndy Whitcroft			my ($s, $c) = ($stat, $cond);
16874d001e4dSAndy Whitcroft
16884d001e4dSAndy Whitcroft			substr($s, 0, length($c), '');
16894d001e4dSAndy Whitcroft
16904d001e4dSAndy Whitcroft			# Make sure we remove the line prefixes as we have
16914d001e4dSAndy Whitcroft			# none on the first line, and are going to readd them
16924d001e4dSAndy Whitcroft			# where necessary.
16934d001e4dSAndy Whitcroft			$s =~ s/\n./\n/gs;
16944d001e4dSAndy Whitcroft
16954d001e4dSAndy Whitcroft			# Find out how long the conditional actually is.
16966f779c18SAndy Whitcroft			my @newlines = ($c =~ /\n/gs);
16976f779c18SAndy Whitcroft			my $cond_lines = 1 + $#newlines;
16984d001e4dSAndy Whitcroft
16994d001e4dSAndy Whitcroft			# We want to check the first line inside the block
17004d001e4dSAndy Whitcroft			# starting at the end of the conditional, so remove:
17014d001e4dSAndy Whitcroft			#  1) any blank line termination
17024d001e4dSAndy Whitcroft			#  2) any opening brace { on end of the line
17034d001e4dSAndy Whitcroft			#  3) any do (...) {
17044d001e4dSAndy Whitcroft			my $continuation = 0;
17054d001e4dSAndy Whitcroft			my $check = 0;
17064d001e4dSAndy Whitcroft			$s =~ s/^.*\bdo\b//;
17074d001e4dSAndy Whitcroft			$s =~ s/^\s*{//;
17084d001e4dSAndy Whitcroft			if ($s =~ s/^\s*\\//) {
17094d001e4dSAndy Whitcroft				$continuation = 1;
17104d001e4dSAndy Whitcroft			}
17119bd49efeSAndy Whitcroft			if ($s =~ s/^\s*?\n//) {
17124d001e4dSAndy Whitcroft				$check = 1;
17134d001e4dSAndy Whitcroft				$cond_lines++;
17144d001e4dSAndy Whitcroft			}
17154d001e4dSAndy Whitcroft
17164d001e4dSAndy Whitcroft			# Also ignore a loop construct at the end of a
17174d001e4dSAndy Whitcroft			# preprocessor statement.
17184d001e4dSAndy Whitcroft			if (($prevline =~ /^.\s*#\s*define\s/ ||
17194d001e4dSAndy Whitcroft			    $prevline =~ /\\\s*$/) && $continuation == 0) {
17204d001e4dSAndy Whitcroft				$check = 0;
17214d001e4dSAndy Whitcroft			}
17224d001e4dSAndy Whitcroft
17239bd49efeSAndy Whitcroft			my $cond_ptr = -1;
1724740504c6SAndy Whitcroft			$continuation = 0;
17259bd49efeSAndy Whitcroft			while ($cond_ptr != $cond_lines) {
17269bd49efeSAndy Whitcroft				$cond_ptr = $cond_lines;
17274d001e4dSAndy Whitcroft
1728f16fa28fSAndy Whitcroft				# If we see an #else/#elif then the code
1729f16fa28fSAndy Whitcroft				# is not linear.
1730f16fa28fSAndy Whitcroft				if ($s =~ /^\s*\#\s*(?:else|elif)/) {
1731f16fa28fSAndy Whitcroft					$check = 0;
1732f16fa28fSAndy Whitcroft				}
1733f16fa28fSAndy Whitcroft
17349bd49efeSAndy Whitcroft				# Ignore:
17359bd49efeSAndy Whitcroft				#  1) blank lines, they should be at 0,
17369bd49efeSAndy Whitcroft				#  2) preprocessor lines, and
17379bd49efeSAndy Whitcroft				#  3) labels.
1738740504c6SAndy Whitcroft				if ($continuation ||
1739740504c6SAndy Whitcroft				    $s =~ /^\s*?\n/ ||
17409bd49efeSAndy Whitcroft				    $s =~ /^\s*#\s*?/ ||
17419bd49efeSAndy Whitcroft				    $s =~ /^\s*$Ident\s*:/) {
1742740504c6SAndy Whitcroft					$continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
174330dad6ebSAndy Whitcroft					if ($s =~ s/^.*?\n//) {
17449bd49efeSAndy Whitcroft						$cond_lines++;
17459bd49efeSAndy Whitcroft					}
17464d001e4dSAndy Whitcroft				}
174730dad6ebSAndy Whitcroft			}
17484d001e4dSAndy Whitcroft
17494d001e4dSAndy Whitcroft			my (undef, $sindent) = line_stats("+" . $s);
17504d001e4dSAndy Whitcroft			my $stat_real = raw_line($linenr, $cond_lines);
17514d001e4dSAndy Whitcroft
17524d001e4dSAndy Whitcroft			# Check if either of these lines are modified, else
17534d001e4dSAndy Whitcroft			# this is not this patch's fault.
17544d001e4dSAndy Whitcroft			if (!defined($stat_real) ||
17554d001e4dSAndy Whitcroft			    $stat !~ /^\+/ && $stat_real !~ /^\+/) {
17564d001e4dSAndy Whitcroft				$check = 0;
17574d001e4dSAndy Whitcroft			}
17584d001e4dSAndy Whitcroft			if (defined($stat_real) && $cond_lines > 1) {
17594d001e4dSAndy Whitcroft				$stat_real = "[...]\n$stat_real";
17604d001e4dSAndy Whitcroft			}
17614d001e4dSAndy Whitcroft
17629bd49efeSAndy 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";
17634d001e4dSAndy Whitcroft
17644d001e4dSAndy Whitcroft			if ($check && (($sindent % 8) != 0 ||
17654d001e4dSAndy Whitcroft			    ($sindent <= $indent && $s ne ''))) {
17664d001e4dSAndy Whitcroft				WARN("suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
17674d001e4dSAndy Whitcroft			}
17684d001e4dSAndy Whitcroft		}
17694d001e4dSAndy Whitcroft
17706c72ffaaSAndy Whitcroft		# Track the 'values' across context and added lines.
17716c72ffaaSAndy Whitcroft		my $opline = $line; $opline =~ s/^./ /;
17721f65f947SAndy Whitcroft		my ($curr_values, $curr_vars) =
17731f65f947SAndy Whitcroft				annotate_values($opline . "\n", $prev_values);
17746c72ffaaSAndy Whitcroft		$curr_values = $prev_values . $curr_values;
1775c2fdda0dSAndy Whitcroft		if ($dbg_values) {
1776c2fdda0dSAndy Whitcroft			my $outline = $opline; $outline =~ s/\t/ /g;
1777cf655043SAndy Whitcroft			print "$linenr > .$outline\n";
1778cf655043SAndy Whitcroft			print "$linenr > $curr_values\n";
17791f65f947SAndy Whitcroft			print "$linenr >  $curr_vars\n";
1780c2fdda0dSAndy Whitcroft		}
17816c72ffaaSAndy Whitcroft		$prev_values = substr($curr_values, -1);
17826c72ffaaSAndy Whitcroft
178300df344fSAndy Whitcroft#ignore lines not being added
178400df344fSAndy Whitcroft		if ($line=~/^[^\+]/) {next;}
178500df344fSAndy Whitcroft
1786653d4876SAndy Whitcroft# TEST: allow direct testing of the type matcher.
17877429c690SAndy Whitcroft		if ($dbg_type) {
17887429c690SAndy Whitcroft			if ($line =~ /^.\s*$Declare\s*$/) {
17897429c690SAndy Whitcroft				ERROR("TEST: is type\n" . $herecurr);
17907429c690SAndy Whitcroft			} elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
17917429c690SAndy Whitcroft				ERROR("TEST: is not type ($1 is)\n". $herecurr);
17927429c690SAndy Whitcroft			}
1793653d4876SAndy Whitcroft			next;
1794653d4876SAndy Whitcroft		}
1795a1ef277eSAndy Whitcroft# TEST: allow direct testing of the attribute matcher.
1796a1ef277eSAndy Whitcroft		if ($dbg_attr) {
17979360b0e5SAndy Whitcroft			if ($line =~ /^.\s*$Modifier\s*$/) {
1798a1ef277eSAndy Whitcroft				ERROR("TEST: is attr\n" . $herecurr);
17999360b0e5SAndy Whitcroft			} elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
1800a1ef277eSAndy Whitcroft				ERROR("TEST: is not attr ($1 is)\n". $herecurr);
1801a1ef277eSAndy Whitcroft			}
1802a1ef277eSAndy Whitcroft			next;
1803a1ef277eSAndy Whitcroft		}
1804653d4876SAndy Whitcroft
1805f0a594c1SAndy Whitcroft# check for initialisation to aggregates open brace on the next line
180699423c20SAndy Whitcroft		if ($line =~ /^.\s*{/ &&
180799423c20SAndy Whitcroft		    $prevline =~ /(?:^|[^=])=\s*$/) {
1808773647a0SAndy Whitcroft			ERROR("that open brace { should be on the previous line\n" . $hereprev);
1809f0a594c1SAndy Whitcroft		}
1810f0a594c1SAndy Whitcroft
181100df344fSAndy Whitcroft#
181200df344fSAndy Whitcroft# Checks which are anchored on the added line.
181300df344fSAndy Whitcroft#
181400df344fSAndy Whitcroft
1815653d4876SAndy Whitcroft# check for malformed paths in #include statements (uses RAW line)
1816c45dcabdSAndy Whitcroft		if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
1817653d4876SAndy Whitcroft			my $path = $1;
1818653d4876SAndy Whitcroft			if ($path =~ m{//}) {
1819de7d4f0eSAndy Whitcroft				ERROR("malformed #include filename\n" .
1820de7d4f0eSAndy Whitcroft					$herecurr);
1821653d4876SAndy Whitcroft			}
1822653d4876SAndy Whitcroft		}
1823653d4876SAndy Whitcroft
182400df344fSAndy Whitcroft# no C99 // comments
182500df344fSAndy Whitcroft		if ($line =~ m{//}) {
1826de7d4f0eSAndy Whitcroft			ERROR("do not use C99 // comments\n" . $herecurr);
182700df344fSAndy Whitcroft		}
182800df344fSAndy Whitcroft		# Remove C99 comments.
18290a920b5bSAndy Whitcroft		$line =~ s@//.*@@;
18306c72ffaaSAndy Whitcroft		$opline =~ s@//.*@@;
18310a920b5bSAndy Whitcroft
18322b474a1aSAndy Whitcroft# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
18332b474a1aSAndy Whitcroft# the whole statement.
18342b474a1aSAndy Whitcroft#print "APW <$lines[$realline_next - 1]>\n";
18352b474a1aSAndy Whitcroft		if (defined $realline_next &&
18362b474a1aSAndy Whitcroft		    exists $lines[$realline_next - 1] &&
18372b474a1aSAndy Whitcroft		    !defined $suppress_export{$realline_next} &&
18382b474a1aSAndy Whitcroft		    ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
18392b474a1aSAndy Whitcroft		     $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
18403cbf62dfSAndy Whitcroft			# Handle definitions which produce identifiers with
18413cbf62dfSAndy Whitcroft			# a prefix:
18423cbf62dfSAndy Whitcroft			#   XXX(foo);
18433cbf62dfSAndy Whitcroft			#   EXPORT_SYMBOL(something_foo);
1844653d4876SAndy Whitcroft			my $name = $1;
18453cbf62dfSAndy Whitcroft			if ($stat =~ /^.([A-Z_]+)\s*\(\s*($Ident)/ &&
18463cbf62dfSAndy Whitcroft			    $name =~ /^${Ident}_$2/) {
18473cbf62dfSAndy Whitcroft#print "FOO C name<$name>\n";
18483cbf62dfSAndy Whitcroft				$suppress_export{$realline_next} = 1;
18493cbf62dfSAndy Whitcroft
18503cbf62dfSAndy Whitcroft			} elsif ($stat !~ /(?:
18512b474a1aSAndy Whitcroft				\n.}\s*$|
185248012058SAndy Whitcroft				^.DEFINE_$Ident\(\Q$name\E\)|
185348012058SAndy Whitcroft				^.DECLARE_$Ident\(\Q$name\E\)|
185448012058SAndy Whitcroft				^.LIST_HEAD\(\Q$name\E\)|
18552b474a1aSAndy Whitcroft				^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
18562b474a1aSAndy Whitcroft				\b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
185748012058SAndy Whitcroft			    )/x) {
18582b474a1aSAndy Whitcroft#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
18592b474a1aSAndy Whitcroft				$suppress_export{$realline_next} = 2;
18602b474a1aSAndy Whitcroft			} else {
18612b474a1aSAndy Whitcroft				$suppress_export{$realline_next} = 1;
18620a920b5bSAndy Whitcroft			}
18630a920b5bSAndy Whitcroft		}
18642b474a1aSAndy Whitcroft		if (!defined $suppress_export{$linenr} &&
18652b474a1aSAndy Whitcroft		    $prevline =~ /^.\s*$/ &&
18662b474a1aSAndy Whitcroft		    ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
18672b474a1aSAndy Whitcroft		     $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
18682b474a1aSAndy Whitcroft#print "FOO B <$lines[$linenr - 1]>\n";
18692b474a1aSAndy Whitcroft			$suppress_export{$linenr} = 2;
18702b474a1aSAndy Whitcroft		}
18712b474a1aSAndy Whitcroft		if (defined $suppress_export{$linenr} &&
18722b474a1aSAndy Whitcroft		    $suppress_export{$linenr} == 2) {
18732b474a1aSAndy Whitcroft			WARN("EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
18742b474a1aSAndy Whitcroft		}
18750a920b5bSAndy Whitcroft
18765150bda4SJoe Eloff# check for global initialisers.
1877c45dcabdSAndy Whitcroft		if ($line =~ /^.$Type\s*$Ident\s*(?:\s+$Modifier)*\s*=\s*(0|NULL|false)\s*;/) {
18785150bda4SJoe Eloff			ERROR("do not initialise globals to 0 or NULL\n" .
1879f0a594c1SAndy Whitcroft				$herecurr);
1880f0a594c1SAndy Whitcroft		}
18810a920b5bSAndy Whitcroft# check for static initialisers.
18822d1bafd7SAndy Whitcroft		if ($line =~ /\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
1883de7d4f0eSAndy Whitcroft			ERROR("do not initialise statics to 0 or NULL\n" .
1884de7d4f0eSAndy Whitcroft				$herecurr);
18850a920b5bSAndy Whitcroft		}
18860a920b5bSAndy Whitcroft
1887cb710ecaSJoe Perches# check for static const char * arrays.
1888cb710ecaSJoe Perches		if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
1889cb710ecaSJoe Perches			WARN("static const char * array should probably be static const char * const\n" .
1890cb710ecaSJoe Perches				$herecurr);
1891cb710ecaSJoe Perches               }
1892cb710ecaSJoe Perches
1893cb710ecaSJoe Perches# check for static char foo[] = "bar" declarations.
1894cb710ecaSJoe Perches		if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
1895cb710ecaSJoe Perches			WARN("static char array declaration should probably be static const char\n" .
1896cb710ecaSJoe Perches				$herecurr);
1897cb710ecaSJoe Perches               }
1898cb710ecaSJoe Perches
189993ed0e2dSJoe Perches# check for declarations of struct pci_device_id
190093ed0e2dSJoe Perches		if ($line =~ /\bstruct\s+pci_device_id\s+\w+\s*\[\s*\]\s*\=\s*\{/) {
190193ed0e2dSJoe Perches			WARN("Use DEFINE_PCI_DEVICE_TABLE for struct pci_device_id\n" . $herecurr);
190293ed0e2dSJoe Perches		}
190393ed0e2dSJoe Perches
1904653d4876SAndy Whitcroft# check for new typedefs, only function parameters and sparse annotations
1905653d4876SAndy Whitcroft# make sense.
1906653d4876SAndy Whitcroft		if ($line =~ /\btypedef\s/ &&
19078054576dSAndy Whitcroft		    $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
1908c45dcabdSAndy Whitcroft		    $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
19098ed22cadSAndy Whitcroft		    $line !~ /\b$typeTypedefs\b/ &&
1910653d4876SAndy Whitcroft		    $line !~ /\b__bitwise(?:__|)\b/) {
1911de7d4f0eSAndy Whitcroft			WARN("do not add new typedefs\n" . $herecurr);
19120a920b5bSAndy Whitcroft		}
19130a920b5bSAndy Whitcroft
19140a920b5bSAndy Whitcroft# * goes on variable not on type
191565863862SAndy Whitcroft		# (char*[ const])
191600ef4eceSAndy Whitcroft		if ($line =~ m{\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\)}) {
191765863862SAndy Whitcroft			my ($from, $to) = ($1, $1);
1918d8aaf121SAndy Whitcroft
191965863862SAndy Whitcroft			# Should start with a space.
192065863862SAndy Whitcroft			$to =~ s/^(\S)/ $1/;
192165863862SAndy Whitcroft			# Should not end with a space.
192265863862SAndy Whitcroft			$to =~ s/\s+$//;
192365863862SAndy Whitcroft			# '*'s should not have spaces between.
1924f9a0b3d1SAndy Whitcroft			while ($to =~ s/\*\s+\*/\*\*/) {
192565863862SAndy Whitcroft			}
1926d8aaf121SAndy Whitcroft
192765863862SAndy Whitcroft			#print "from<$from> to<$to>\n";
192865863862SAndy Whitcroft			if ($from ne $to) {
192965863862SAndy Whitcroft				ERROR("\"(foo$from)\" should be \"(foo$to)\"\n" .  $herecurr);
193065863862SAndy Whitcroft			}
193100ef4eceSAndy Whitcroft		} elsif ($line =~ m{\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident)}) {
193265863862SAndy Whitcroft			my ($from, $to, $ident) = ($1, $1, $2);
1933d8aaf121SAndy Whitcroft
193465863862SAndy Whitcroft			# Should start with a space.
193565863862SAndy Whitcroft			$to =~ s/^(\S)/ $1/;
193665863862SAndy Whitcroft			# Should not end with a space.
193765863862SAndy Whitcroft			$to =~ s/\s+$//;
193865863862SAndy Whitcroft			# '*'s should not have spaces between.
1939f9a0b3d1SAndy Whitcroft			while ($to =~ s/\*\s+\*/\*\*/) {
194065863862SAndy Whitcroft			}
194165863862SAndy Whitcroft			# Modifiers should have spaces.
194265863862SAndy Whitcroft			$to =~ s/(\b$Modifier$)/$1 /;
194365863862SAndy Whitcroft
1944667026e7SAndy Whitcroft			#print "from<$from> to<$to> ident<$ident>\n";
1945667026e7SAndy Whitcroft			if ($from ne $to && $ident !~ /^$Modifier$/) {
194665863862SAndy Whitcroft				ERROR("\"foo${from}bar\" should be \"foo${to}bar\"\n" .  $herecurr);
194765863862SAndy Whitcroft			}
19480a920b5bSAndy Whitcroft		}
19490a920b5bSAndy Whitcroft
19500a920b5bSAndy Whitcroft# # no BUG() or BUG_ON()
19510a920b5bSAndy Whitcroft# 		if ($line =~ /\b(BUG|BUG_ON)\b/) {
19520a920b5bSAndy Whitcroft# 			print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
19530a920b5bSAndy Whitcroft# 			print "$herecurr";
19540a920b5bSAndy Whitcroft# 			$clean = 0;
19550a920b5bSAndy Whitcroft# 		}
19560a920b5bSAndy Whitcroft
19578905a67cSAndy Whitcroft		if ($line =~ /\bLINUX_VERSION_CODE\b/) {
1958c2fdda0dSAndy Whitcroft			WARN("LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
19598905a67cSAndy Whitcroft		}
19608905a67cSAndy Whitcroft
196117441227SJoe Perches# check for uses of printk_ratelimit
196217441227SJoe Perches		if ($line =~ /\bprintk_ratelimit\s*\(/) {
196317441227SJoe Perches			WARN("Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
196417441227SJoe Perches		}
196517441227SJoe Perches
196600df344fSAndy Whitcroft# printk should use KERN_* levels.  Note that follow on printk's on the
196700df344fSAndy Whitcroft# same line do not need a level, so we use the current block context
196800df344fSAndy Whitcroft# to try and find and validate the current printk.  In summary the current
196925985edcSLucas De Marchi# printk includes all preceding printk's which have no newline on the end.
197000df344fSAndy Whitcroft# we assume the first bad printk is the one to report.
1971f0a594c1SAndy Whitcroft		if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
197200df344fSAndy Whitcroft			my $ok = 0;
197300df344fSAndy Whitcroft			for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
197400df344fSAndy Whitcroft				#print "CHECK<$lines[$ln - 1]\n";
197525985edcSLucas De Marchi				# we have a preceding printk if it ends
197600df344fSAndy Whitcroft				# with "\n" ignore it, else it is to blame
197700df344fSAndy Whitcroft				if ($lines[$ln - 1] =~ m{\bprintk\(}) {
197800df344fSAndy Whitcroft					if ($rawlines[$ln - 1] !~ m{\\n"}) {
197900df344fSAndy Whitcroft						$ok = 1;
198000df344fSAndy Whitcroft					}
198100df344fSAndy Whitcroft					last;
198200df344fSAndy Whitcroft				}
198300df344fSAndy Whitcroft			}
198400df344fSAndy Whitcroft			if ($ok == 0) {
1985de7d4f0eSAndy Whitcroft				WARN("printk() should include KERN_ facility level\n" . $herecurr);
19860a920b5bSAndy Whitcroft			}
198700df344fSAndy Whitcroft		}
19880a920b5bSAndy Whitcroft
1989653d4876SAndy Whitcroft# function brace can't be on same line, except for #defines of do while,
1990653d4876SAndy Whitcroft# or if closed on same line
1991c45dcabdSAndy Whitcroft		if (($line=~/$Type\s*$Ident\(.*\).*\s{/) and
1992c45dcabdSAndy Whitcroft		    !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) {
1993de7d4f0eSAndy Whitcroft			ERROR("open brace '{' following function declarations go on the next line\n" . $herecurr);
19940a920b5bSAndy Whitcroft		}
1995653d4876SAndy Whitcroft
19968905a67cSAndy Whitcroft# open braces for enum, union and struct go on the same line.
19978905a67cSAndy Whitcroft		if ($line =~ /^.\s*{/ &&
19988905a67cSAndy Whitcroft		    $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
19998905a67cSAndy Whitcroft			ERROR("open brace '{' following $1 go on the same line\n" . $hereprev);
20008905a67cSAndy Whitcroft		}
20018905a67cSAndy Whitcroft
20020c73b4ebSAndy Whitcroft# missing space after union, struct or enum definition
20030c73b4ebSAndy Whitcroft		if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?(?:\s+$Ident)?[=\{]/) {
20040c73b4ebSAndy Whitcroft		    WARN("missing space after $1 definition\n" . $herecurr);
20050c73b4ebSAndy Whitcroft		}
20060c73b4ebSAndy Whitcroft
20078d31cfceSAndy Whitcroft# check for spacing round square brackets; allowed:
20088d31cfceSAndy Whitcroft#  1. with a type on the left -- int [] a;
2009fe2a7dbcSAndy Whitcroft#  2. at the beginning of a line for slice initialisers -- [0...10] = 5,
2010fe2a7dbcSAndy Whitcroft#  3. inside a curly brace -- = { [0...10] = 5 }
20118d31cfceSAndy Whitcroft		while ($line =~ /(.*?\s)\[/g) {
20128d31cfceSAndy Whitcroft			my ($where, $prefix) = ($-[1], $1);
20138d31cfceSAndy Whitcroft			if ($prefix !~ /$Type\s+$/ &&
2014fe2a7dbcSAndy Whitcroft			    ($where != 0 || $prefix !~ /^.\s+$/) &&
2015fe2a7dbcSAndy Whitcroft			    $prefix !~ /{\s+$/) {
20168d31cfceSAndy Whitcroft				ERROR("space prohibited before open square bracket '['\n" . $herecurr);
20178d31cfceSAndy Whitcroft			}
20188d31cfceSAndy Whitcroft		}
20198d31cfceSAndy Whitcroft
2020f0a594c1SAndy Whitcroft# check for spaces between functions and their parentheses.
20216c72ffaaSAndy Whitcroft		while ($line =~ /($Ident)\s+\(/g) {
2022c2fdda0dSAndy Whitcroft			my $name = $1;
2023773647a0SAndy Whitcroft			my $ctx_before = substr($line, 0, $-[1]);
2024773647a0SAndy Whitcroft			my $ctx = "$ctx_before$name";
2025c2fdda0dSAndy Whitcroft
2026c2fdda0dSAndy Whitcroft			# Ignore those directives where spaces _are_ permitted.
2027773647a0SAndy Whitcroft			if ($name =~ /^(?:
2028773647a0SAndy Whitcroft				if|for|while|switch|return|case|
2029773647a0SAndy Whitcroft				volatile|__volatile__|
2030773647a0SAndy Whitcroft				__attribute__|format|__extension__|
2031773647a0SAndy Whitcroft				asm|__asm__)$/x)
2032773647a0SAndy Whitcroft			{
2033c2fdda0dSAndy Whitcroft
2034c2fdda0dSAndy Whitcroft			# cpp #define statements have non-optional spaces, ie
2035c2fdda0dSAndy Whitcroft			# if there is a space between the name and the open
2036c2fdda0dSAndy Whitcroft			# parenthesis it is simply not a parameter group.
2037c45dcabdSAndy Whitcroft			} elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
2038773647a0SAndy Whitcroft
2039773647a0SAndy Whitcroft			# cpp #elif statement condition may start with a (
2040c45dcabdSAndy Whitcroft			} elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
2041c2fdda0dSAndy Whitcroft
2042c2fdda0dSAndy Whitcroft			# If this whole things ends with a type its most
2043c2fdda0dSAndy Whitcroft			# likely a typedef for a function.
2044773647a0SAndy Whitcroft			} elsif ($ctx =~ /$Type$/) {
2045c2fdda0dSAndy Whitcroft
2046c2fdda0dSAndy Whitcroft			} else {
2047773647a0SAndy Whitcroft				WARN("space prohibited between function name and open parenthesis '('\n" . $herecurr);
2048f0a594c1SAndy Whitcroft			}
20496c72ffaaSAndy Whitcroft		}
2050653d4876SAndy Whitcroft# Check operator spacing.
20510a920b5bSAndy Whitcroft		if (!($line=~/\#\s*include/)) {
20529c0ca6f9SAndy Whitcroft			my $ops = qr{
20539c0ca6f9SAndy Whitcroft				<<=|>>=|<=|>=|==|!=|
20549c0ca6f9SAndy Whitcroft				\+=|-=|\*=|\/=|%=|\^=|\|=|&=|
20559c0ca6f9SAndy Whitcroft				=>|->|<<|>>|<|>|=|!|~|
20561f65f947SAndy Whitcroft				&&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
20571f65f947SAndy Whitcroft				\?|:
20589c0ca6f9SAndy Whitcroft			}x;
2059cf655043SAndy Whitcroft			my @elements = split(/($ops|;)/, $opline);
206000df344fSAndy Whitcroft			my $off = 0;
20616c72ffaaSAndy Whitcroft
20626c72ffaaSAndy Whitcroft			my $blank = copy_spacing($opline);
20636c72ffaaSAndy Whitcroft
20640a920b5bSAndy Whitcroft			for (my $n = 0; $n < $#elements; $n += 2) {
20654a0df2efSAndy Whitcroft				$off += length($elements[$n]);
20664a0df2efSAndy Whitcroft
206725985edcSLucas De Marchi				# Pick up the preceding and succeeding characters.
2068773647a0SAndy Whitcroft				my $ca = substr($opline, 0, $off);
2069773647a0SAndy Whitcroft				my $cc = '';
2070773647a0SAndy Whitcroft				if (length($opline) >= ($off + length($elements[$n + 1]))) {
2071773647a0SAndy Whitcroft					$cc = substr($opline, $off + length($elements[$n + 1]));
2072773647a0SAndy Whitcroft				}
2073773647a0SAndy Whitcroft				my $cb = "$ca$;$cc";
2074773647a0SAndy Whitcroft
20754a0df2efSAndy Whitcroft				my $a = '';
20764a0df2efSAndy Whitcroft				$a = 'V' if ($elements[$n] ne '');
20774a0df2efSAndy Whitcroft				$a = 'W' if ($elements[$n] =~ /\s$/);
2078cf655043SAndy Whitcroft				$a = 'C' if ($elements[$n] =~ /$;$/);
20794a0df2efSAndy Whitcroft				$a = 'B' if ($elements[$n] =~ /(\[|\()$/);
20804a0df2efSAndy Whitcroft				$a = 'O' if ($elements[$n] eq '');
2081773647a0SAndy Whitcroft				$a = 'E' if ($ca =~ /^\s*$/);
20824a0df2efSAndy Whitcroft
20830a920b5bSAndy Whitcroft				my $op = $elements[$n + 1];
20844a0df2efSAndy Whitcroft
20854a0df2efSAndy Whitcroft				my $c = '';
20860a920b5bSAndy Whitcroft				if (defined $elements[$n + 2]) {
20874a0df2efSAndy Whitcroft					$c = 'V' if ($elements[$n + 2] ne '');
20884a0df2efSAndy Whitcroft					$c = 'W' if ($elements[$n + 2] =~ /^\s/);
2089cf655043SAndy Whitcroft					$c = 'C' if ($elements[$n + 2] =~ /^$;/);
20904a0df2efSAndy Whitcroft					$c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
20914a0df2efSAndy Whitcroft					$c = 'O' if ($elements[$n + 2] eq '');
20928b1b3378SAndy Whitcroft					$c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
20934a0df2efSAndy Whitcroft				} else {
20944a0df2efSAndy Whitcroft					$c = 'E';
20950a920b5bSAndy Whitcroft				}
20960a920b5bSAndy Whitcroft
20974a0df2efSAndy Whitcroft				my $ctx = "${a}x${c}";
20984a0df2efSAndy Whitcroft
20994a0df2efSAndy Whitcroft				my $at = "(ctx:$ctx)";
21004a0df2efSAndy Whitcroft
21016c72ffaaSAndy Whitcroft				my $ptr = substr($blank, 0, $off) . "^";
2102de7d4f0eSAndy Whitcroft				my $hereptr = "$hereline$ptr\n";
21030a920b5bSAndy Whitcroft
210474048ed8SAndy Whitcroft				# Pull out the value of this operator.
21056c72ffaaSAndy Whitcroft				my $op_type = substr($curr_values, $off + 1, 1);
21060a920b5bSAndy Whitcroft
21071f65f947SAndy Whitcroft				# Get the full operator variant.
21081f65f947SAndy Whitcroft				my $opv = $op . substr($curr_vars, $off, 1);
21091f65f947SAndy Whitcroft
211013214adfSAndy Whitcroft				# Ignore operators passed as parameters.
211113214adfSAndy Whitcroft				if ($op_type ne 'V' &&
211213214adfSAndy Whitcroft				    $ca =~ /\s$/ && $cc =~ /^\s*,/) {
211313214adfSAndy Whitcroft
2114cf655043SAndy Whitcroft#				# Ignore comments
2115cf655043SAndy Whitcroft#				} elsif ($op =~ /^$;+$/) {
211613214adfSAndy Whitcroft
2117d8aaf121SAndy Whitcroft				# ; should have either the end of line or a space or \ after it
211813214adfSAndy Whitcroft				} elsif ($op eq ';') {
2119cf655043SAndy Whitcroft					if ($ctx !~ /.x[WEBC]/ &&
2120cf655043SAndy Whitcroft					    $cc !~ /^\\/ && $cc !~ /^;/) {
2121773647a0SAndy Whitcroft						ERROR("space required after that '$op' $at\n" . $hereptr);
2122d8aaf121SAndy Whitcroft					}
2123d8aaf121SAndy Whitcroft
2124d8aaf121SAndy Whitcroft				# // is a comment
2125d8aaf121SAndy Whitcroft				} elsif ($op eq '//') {
21260a920b5bSAndy Whitcroft
21271f65f947SAndy Whitcroft				# No spaces for:
21281f65f947SAndy Whitcroft				#   ->
21291f65f947SAndy Whitcroft				#   :   when part of a bitfield
21301f65f947SAndy Whitcroft				} elsif ($op eq '->' || $opv eq ':B') {
21314a0df2efSAndy Whitcroft					if ($ctx =~ /Wx.|.xW/) {
2132773647a0SAndy Whitcroft						ERROR("spaces prohibited around that '$op' $at\n" . $hereptr);
21330a920b5bSAndy Whitcroft					}
21340a920b5bSAndy Whitcroft
21350a920b5bSAndy Whitcroft				# , must have a space on the right.
21360a920b5bSAndy Whitcroft				} elsif ($op eq ',') {
2137cf655043SAndy Whitcroft					if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
2138773647a0SAndy Whitcroft						ERROR("space required after that '$op' $at\n" . $hereptr);
21390a920b5bSAndy Whitcroft					}
21400a920b5bSAndy Whitcroft
21419c0ca6f9SAndy Whitcroft				# '*' as part of a type definition -- reported already.
214274048ed8SAndy Whitcroft				} elsif ($opv eq '*_') {
21439c0ca6f9SAndy Whitcroft					#warn "'*' is part of type\n";
21449c0ca6f9SAndy Whitcroft
21459c0ca6f9SAndy Whitcroft				# unary operators should have a space before and
21469c0ca6f9SAndy Whitcroft				# none after.  May be left adjacent to another
21479c0ca6f9SAndy Whitcroft				# unary operator, or a cast
21489c0ca6f9SAndy Whitcroft				} elsif ($op eq '!' || $op eq '~' ||
214974048ed8SAndy Whitcroft					 $opv eq '*U' || $opv eq '-U' ||
21500d413866SAndy Whitcroft					 $opv eq '&U' || $opv eq '&&U') {
2151cf655043SAndy Whitcroft					if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
2152773647a0SAndy Whitcroft						ERROR("space required before that '$op' $at\n" . $hereptr);
21530a920b5bSAndy Whitcroft					}
2154a3340b35SAndy Whitcroft					if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
2155171ae1a4SAndy Whitcroft						# A unary '*' may be const
2156171ae1a4SAndy Whitcroft
2157171ae1a4SAndy Whitcroft					} elsif ($ctx =~ /.xW/) {
2158fb9e9096SAndy Whitcroft						ERROR("space prohibited after that '$op' $at\n" . $hereptr);
21590a920b5bSAndy Whitcroft					}
21600a920b5bSAndy Whitcroft
21610a920b5bSAndy Whitcroft				# unary ++ and unary -- are allowed no space on one side.
21620a920b5bSAndy Whitcroft				} elsif ($op eq '++' or $op eq '--') {
2163773647a0SAndy Whitcroft					if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
2164773647a0SAndy Whitcroft						ERROR("space required one side of that '$op' $at\n" . $hereptr);
21650a920b5bSAndy Whitcroft					}
2166773647a0SAndy Whitcroft					if ($ctx =~ /Wx[BE]/ ||
2167773647a0SAndy Whitcroft					    ($ctx =~ /Wx./ && $cc =~ /^;/)) {
2168773647a0SAndy Whitcroft						ERROR("space prohibited before that '$op' $at\n" . $hereptr);
2169653d4876SAndy Whitcroft					}
2170773647a0SAndy Whitcroft					if ($ctx =~ /ExW/) {
2171773647a0SAndy Whitcroft						ERROR("space prohibited after that '$op' $at\n" . $hereptr);
2172773647a0SAndy Whitcroft					}
2173773647a0SAndy Whitcroft
21740a920b5bSAndy Whitcroft
21750a920b5bSAndy Whitcroft				# << and >> may either have or not have spaces both sides
21769c0ca6f9SAndy Whitcroft				} elsif ($op eq '<<' or $op eq '>>' or
21779c0ca6f9SAndy Whitcroft					 $op eq '&' or $op eq '^' or $op eq '|' or
21789c0ca6f9SAndy Whitcroft					 $op eq '+' or $op eq '-' or
2179c2fdda0dSAndy Whitcroft					 $op eq '*' or $op eq '/' or
2180c2fdda0dSAndy Whitcroft					 $op eq '%')
21810a920b5bSAndy Whitcroft				{
2182773647a0SAndy Whitcroft					if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
2183de7d4f0eSAndy Whitcroft						ERROR("need consistent spacing around '$op' $at\n" .
2184de7d4f0eSAndy Whitcroft							$hereptr);
21850a920b5bSAndy Whitcroft					}
21860a920b5bSAndy Whitcroft
21871f65f947SAndy Whitcroft				# A colon needs no spaces before when it is
21881f65f947SAndy Whitcroft				# terminating a case value or a label.
21891f65f947SAndy Whitcroft				} elsif ($opv eq ':C' || $opv eq ':L') {
21901f65f947SAndy Whitcroft					if ($ctx =~ /Wx./) {
21911f65f947SAndy Whitcroft						ERROR("space prohibited before that '$op' $at\n" . $hereptr);
21921f65f947SAndy Whitcroft					}
21931f65f947SAndy Whitcroft
21940a920b5bSAndy Whitcroft				# All the others need spaces both sides.
2195cf655043SAndy Whitcroft				} elsif ($ctx !~ /[EWC]x[CWE]/) {
21961f65f947SAndy Whitcroft					my $ok = 0;
21971f65f947SAndy Whitcroft
219822f2a2efSAndy Whitcroft					# Ignore email addresses <foo@bar>
21991f65f947SAndy Whitcroft					if (($op eq '<' &&
22001f65f947SAndy Whitcroft					     $cc =~ /^\S+\@\S+>/) ||
22011f65f947SAndy Whitcroft					    ($op eq '>' &&
22021f65f947SAndy Whitcroft					     $ca =~ /<\S+\@\S+$/))
22031f65f947SAndy Whitcroft					{
22041f65f947SAndy Whitcroft					    	$ok = 1;
22051f65f947SAndy Whitcroft					}
22061f65f947SAndy Whitcroft
22071f65f947SAndy Whitcroft					# Ignore ?:
22081f65f947SAndy Whitcroft					if (($opv eq ':O' && $ca =~ /\?$/) ||
22091f65f947SAndy Whitcroft					    ($op eq '?' && $cc =~ /^:/)) {
22101f65f947SAndy Whitcroft					    	$ok = 1;
22111f65f947SAndy Whitcroft					}
22121f65f947SAndy Whitcroft
22131f65f947SAndy Whitcroft					if ($ok == 0) {
2214773647a0SAndy Whitcroft						ERROR("spaces required around that '$op' $at\n" . $hereptr);
22150a920b5bSAndy Whitcroft					}
221622f2a2efSAndy Whitcroft				}
22174a0df2efSAndy Whitcroft				$off += length($elements[$n + 1]);
22180a920b5bSAndy Whitcroft			}
22190a920b5bSAndy Whitcroft		}
22200a920b5bSAndy Whitcroft
2221f0a594c1SAndy Whitcroft# check for multiple assignments
2222f0a594c1SAndy Whitcroft		if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
22236c72ffaaSAndy Whitcroft			CHK("multiple assignments should be avoided\n" . $herecurr);
2224f0a594c1SAndy Whitcroft		}
2225f0a594c1SAndy Whitcroft
222622f2a2efSAndy Whitcroft## # check for multiple declarations, allowing for a function declaration
222722f2a2efSAndy Whitcroft## # continuation.
222822f2a2efSAndy Whitcroft## 		if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
222922f2a2efSAndy Whitcroft## 		    $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
223022f2a2efSAndy Whitcroft##
223122f2a2efSAndy Whitcroft## 			# Remove any bracketed sections to ensure we do not
223222f2a2efSAndy Whitcroft## 			# falsly report the parameters of functions.
223322f2a2efSAndy Whitcroft## 			my $ln = $line;
223422f2a2efSAndy Whitcroft## 			while ($ln =~ s/\([^\(\)]*\)//g) {
223522f2a2efSAndy Whitcroft## 			}
223622f2a2efSAndy Whitcroft## 			if ($ln =~ /,/) {
223722f2a2efSAndy Whitcroft## 				WARN("declaring multiple variables together should be avoided\n" . $herecurr);
223822f2a2efSAndy Whitcroft## 			}
223922f2a2efSAndy Whitcroft## 		}
2240f0a594c1SAndy Whitcroft
22410a920b5bSAndy Whitcroft#need space before brace following if, while, etc
224222f2a2efSAndy Whitcroft		if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
224322f2a2efSAndy Whitcroft		    $line =~ /do{/) {
2244773647a0SAndy Whitcroft			ERROR("space required before the open brace '{'\n" . $herecurr);
2245de7d4f0eSAndy Whitcroft		}
2246de7d4f0eSAndy Whitcroft
2247de7d4f0eSAndy Whitcroft# closing brace should have a space following it when it has anything
2248de7d4f0eSAndy Whitcroft# on the line
2249de7d4f0eSAndy Whitcroft		if ($line =~ /}(?!(?:,|;|\)))\S/) {
2250773647a0SAndy Whitcroft			ERROR("space required after that close brace '}'\n" . $herecurr);
22510a920b5bSAndy Whitcroft		}
22520a920b5bSAndy Whitcroft
225322f2a2efSAndy Whitcroft# check spacing on square brackets
225422f2a2efSAndy Whitcroft		if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
2255773647a0SAndy Whitcroft			ERROR("space prohibited after that open square bracket '['\n" . $herecurr);
225622f2a2efSAndy Whitcroft		}
225722f2a2efSAndy Whitcroft		if ($line =~ /\s\]/) {
2258773647a0SAndy Whitcroft			ERROR("space prohibited before that close square bracket ']'\n" . $herecurr);
225922f2a2efSAndy Whitcroft		}
226022f2a2efSAndy Whitcroft
2261c45dcabdSAndy Whitcroft# check spacing on parentheses
22629c0ca6f9SAndy Whitcroft		if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
22639c0ca6f9SAndy Whitcroft		    $line !~ /for\s*\(\s+;/) {
2264773647a0SAndy Whitcroft			ERROR("space prohibited after that open parenthesis '('\n" . $herecurr);
226522f2a2efSAndy Whitcroft		}
226613214adfSAndy Whitcroft		if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
2267c45dcabdSAndy Whitcroft		    $line !~ /for\s*\(.*;\s+\)/ &&
2268c45dcabdSAndy Whitcroft		    $line !~ /:\s+\)/) {
2269773647a0SAndy Whitcroft			ERROR("space prohibited before that close parenthesis ')'\n" . $herecurr);
227022f2a2efSAndy Whitcroft		}
227122f2a2efSAndy Whitcroft
22720a920b5bSAndy Whitcroft#goto labels aren't indented, allow a single space however
22734a0df2efSAndy Whitcroft		if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
22740a920b5bSAndy Whitcroft		   !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
2275de7d4f0eSAndy Whitcroft			WARN("labels should not be indented\n" . $herecurr);
22760a920b5bSAndy Whitcroft		}
22770a920b5bSAndy Whitcroft
2278c45dcabdSAndy Whitcroft# Return is not a function.
2279c45dcabdSAndy Whitcroft		if (defined($stat) && $stat =~ /^.\s*return(\s*)(\(.*);/s) {
2280c45dcabdSAndy Whitcroft			my $spacing = $1;
2281c45dcabdSAndy Whitcroft			my $value = $2;
2282c45dcabdSAndy Whitcroft
228386f9d059SAndy Whitcroft			# Flatten any parentheses
2284fb2d2c1bSAndy Whitcroft			$value =~ s/\(/ \(/g;
2285fb2d2c1bSAndy Whitcroft			$value =~ s/\)/\) /g;
228663f17f89SAndy Whitcroft			while ($value =~ s/\[[^\{\}]*\]/1/ ||
228763f17f89SAndy Whitcroft			       $value !~ /(?:$Ident|-?$Constant)\s*
228863f17f89SAndy Whitcroft					     $Compare\s*
228963f17f89SAndy Whitcroft					     (?:$Ident|-?$Constant)/x &&
229063f17f89SAndy Whitcroft			       $value =~ s/\([^\(\)]*\)/1/) {
2291c45dcabdSAndy Whitcroft			}
2292fb2d2c1bSAndy Whitcroft#print "value<$value>\n";
2293fb2d2c1bSAndy Whitcroft			if ($value =~ /^\s*(?:$Ident|-?$Constant)\s*$/) {
2294c45dcabdSAndy Whitcroft				ERROR("return is not a function, parentheses are not required\n" . $herecurr);
2295c45dcabdSAndy Whitcroft
2296c45dcabdSAndy Whitcroft			} elsif ($spacing !~ /\s+/) {
2297c45dcabdSAndy Whitcroft				ERROR("space required before the open parenthesis '('\n" . $herecurr);
2298c45dcabdSAndy Whitcroft			}
2299c45dcabdSAndy Whitcroft		}
230053a3c448SAndy Whitcroft# Return of what appears to be an errno should normally be -'ve
230153a3c448SAndy Whitcroft		if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) {
230253a3c448SAndy Whitcroft			my $name = $1;
230353a3c448SAndy Whitcroft			if ($name ne 'EOF' && $name ne 'ERROR') {
230453a3c448SAndy Whitcroft				WARN("return of an errno should typically be -ve (return -$1)\n" . $herecurr);
230553a3c448SAndy Whitcroft			}
230653a3c448SAndy Whitcroft		}
2307c45dcabdSAndy Whitcroft
23087d2367afSJoe Perches# typecasts on min/max could be min_t/max_t
23097d2367afSJoe Perches		if ($line =~ /^\+(?:.*?)\b(min|max)\s*\($Typecast{0,1}($LvalOrFunc)\s*,\s*$Typecast{0,1}($LvalOrFunc)\s*\)/) {
23107d2367afSJoe Perches			if (defined $2 || defined $8) {
23117d2367afSJoe Perches				my $call = $1;
23127d2367afSJoe Perches				my $cast1 = deparenthesize($2);
23137d2367afSJoe Perches				my $arg1 = $3;
23147d2367afSJoe Perches				my $cast2 = deparenthesize($8);
23157d2367afSJoe Perches				my $arg2 = $9;
23167d2367afSJoe Perches				my $cast;
23177d2367afSJoe Perches
23187d2367afSJoe Perches				if ($cast1 ne "" && $cast2 ne "") {
23197d2367afSJoe Perches					$cast = "$cast1 or $cast2";
23207d2367afSJoe Perches				} elsif ($cast1 ne "") {
23217d2367afSJoe Perches					$cast = $cast1;
23227d2367afSJoe Perches				} else {
23237d2367afSJoe Perches					$cast = $cast2;
23247d2367afSJoe Perches				}
23257d2367afSJoe Perches				WARN("$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . $herecurr);
23267d2367afSJoe Perches			}
23277d2367afSJoe Perches		}
23287d2367afSJoe Perches
23290a920b5bSAndy Whitcroft# Need a space before open parenthesis after if, while etc
23304a0df2efSAndy Whitcroft		if ($line=~/\b(if|while|for|switch)\(/) {
2331773647a0SAndy Whitcroft			ERROR("space required before the open parenthesis '('\n" . $herecurr);
23320a920b5bSAndy Whitcroft		}
23330a920b5bSAndy Whitcroft
2334f5fe35ddSAndy Whitcroft# Check for illegal assignment in if conditional -- and check for trailing
2335f5fe35ddSAndy Whitcroft# statements after the conditional.
2336170d3a22SAndy Whitcroft		if ($line =~ /do\s*(?!{)/) {
2337170d3a22SAndy Whitcroft			my ($stat_next) = ctx_statement_block($line_nr_next,
2338170d3a22SAndy Whitcroft						$remain_next, $off_next);
2339170d3a22SAndy Whitcroft			$stat_next =~ s/\n./\n /g;
2340170d3a22SAndy Whitcroft			##print "stat<$stat> stat_next<$stat_next>\n";
2341170d3a22SAndy Whitcroft
2342170d3a22SAndy Whitcroft			if ($stat_next =~ /^\s*while\b/) {
2343170d3a22SAndy Whitcroft				# If the statement carries leading newlines,
2344170d3a22SAndy Whitcroft				# then count those as offsets.
2345170d3a22SAndy Whitcroft				my ($whitespace) =
2346170d3a22SAndy Whitcroft					($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
2347170d3a22SAndy Whitcroft				my $offset =
2348170d3a22SAndy Whitcroft					statement_rawlines($whitespace) - 1;
2349170d3a22SAndy Whitcroft
2350170d3a22SAndy Whitcroft				$suppress_whiletrailers{$line_nr_next +
2351170d3a22SAndy Whitcroft								$offset} = 1;
2352170d3a22SAndy Whitcroft			}
2353170d3a22SAndy Whitcroft		}
2354170d3a22SAndy Whitcroft		if (!defined $suppress_whiletrailers{$linenr} &&
2355170d3a22SAndy Whitcroft		    $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
2356171ae1a4SAndy Whitcroft			my ($s, $c) = ($stat, $cond);
23578905a67cSAndy Whitcroft
2358b53c8e10SAndy Whitcroft			if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
2359c2fdda0dSAndy Whitcroft				ERROR("do not use assignment in if condition\n" . $herecurr);
23608905a67cSAndy Whitcroft			}
23618905a67cSAndy Whitcroft
23628905a67cSAndy Whitcroft			# Find out what is on the end of the line after the
23638905a67cSAndy Whitcroft			# conditional.
2364773647a0SAndy Whitcroft			substr($s, 0, length($c), '');
23658905a67cSAndy Whitcroft			$s =~ s/\n.*//g;
236613214adfSAndy Whitcroft			$s =~ s/$;//g; 	# Remove any comments
236753210168SAndy Whitcroft			if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
236853210168SAndy Whitcroft			    $c !~ /}\s*while\s*/)
2369773647a0SAndy Whitcroft			{
2370bb44ad39SAndy Whitcroft				# Find out how long the conditional actually is.
2371bb44ad39SAndy Whitcroft				my @newlines = ($c =~ /\n/gs);
2372bb44ad39SAndy Whitcroft				my $cond_lines = 1 + $#newlines;
237342bdf74cSHidetoshi Seto				my $stat_real = '';
2374bb44ad39SAndy Whitcroft
237542bdf74cSHidetoshi Seto				$stat_real = raw_line($linenr, $cond_lines)
237642bdf74cSHidetoshi Seto							. "\n" if ($cond_lines);
2377bb44ad39SAndy Whitcroft				if (defined($stat_real) && $cond_lines > 1) {
2378bb44ad39SAndy Whitcroft					$stat_real = "[...]\n$stat_real";
2379bb44ad39SAndy Whitcroft				}
2380bb44ad39SAndy Whitcroft
2381bb44ad39SAndy Whitcroft				ERROR("trailing statements should be on next line\n" . $herecurr . $stat_real);
23828905a67cSAndy Whitcroft			}
23838905a67cSAndy Whitcroft		}
23848905a67cSAndy Whitcroft
238513214adfSAndy Whitcroft# Check for bitwise tests written as boolean
238613214adfSAndy Whitcroft		if ($line =~ /
238713214adfSAndy Whitcroft			(?:
238813214adfSAndy Whitcroft				(?:\[|\(|\&\&|\|\|)
238913214adfSAndy Whitcroft				\s*0[xX][0-9]+\s*
239013214adfSAndy Whitcroft				(?:\&\&|\|\|)
239113214adfSAndy Whitcroft			|
239213214adfSAndy Whitcroft				(?:\&\&|\|\|)
239313214adfSAndy Whitcroft				\s*0[xX][0-9]+\s*
239413214adfSAndy Whitcroft				(?:\&\&|\|\||\)|\])
239513214adfSAndy Whitcroft			)/x)
239613214adfSAndy Whitcroft		{
239713214adfSAndy Whitcroft			WARN("boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
239813214adfSAndy Whitcroft		}
239913214adfSAndy Whitcroft
24008905a67cSAndy Whitcroft# if and else should not have general statements after it
240113214adfSAndy Whitcroft		if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
240213214adfSAndy Whitcroft			my $s = $1;
240313214adfSAndy Whitcroft			$s =~ s/$;//g; 	# Remove any comments
240413214adfSAndy Whitcroft			if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
24058905a67cSAndy Whitcroft				ERROR("trailing statements should be on next line\n" . $herecurr);
24060a920b5bSAndy Whitcroft			}
240713214adfSAndy Whitcroft		}
240839667782SAndy Whitcroft# if should not continue a brace
240939667782SAndy Whitcroft		if ($line =~ /}\s*if\b/) {
241039667782SAndy Whitcroft			ERROR("trailing statements should be on next line\n" .
241139667782SAndy Whitcroft				$herecurr);
241239667782SAndy Whitcroft		}
2413a1080bf8SAndy Whitcroft# case and default should not have general statements after them
2414a1080bf8SAndy Whitcroft		if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
2415a1080bf8SAndy Whitcroft		    $line !~ /\G(?:
24163fef12d6SAndy Whitcroft			(?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
2417a1080bf8SAndy Whitcroft			\s*return\s+
2418a1080bf8SAndy Whitcroft		    )/xg)
2419a1080bf8SAndy Whitcroft		{
2420a1080bf8SAndy Whitcroft			ERROR("trailing statements should be on next line\n" . $herecurr);
2421a1080bf8SAndy Whitcroft		}
24220a920b5bSAndy Whitcroft
24230a920b5bSAndy Whitcroft		# Check for }<nl>else {, these must be at the same
24240a920b5bSAndy Whitcroft		# indent level to be relevant to each other.
24250a920b5bSAndy Whitcroft		if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
24260a920b5bSAndy Whitcroft						$previndent == $indent) {
2427de7d4f0eSAndy Whitcroft			ERROR("else should follow close brace '}'\n" . $hereprev);
24280a920b5bSAndy Whitcroft		}
24290a920b5bSAndy Whitcroft
2430c2fdda0dSAndy Whitcroft		if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and
2431c2fdda0dSAndy Whitcroft						$previndent == $indent) {
2432c2fdda0dSAndy Whitcroft			my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
2433c2fdda0dSAndy Whitcroft
2434c2fdda0dSAndy Whitcroft			# Find out what is on the end of the line after the
2435c2fdda0dSAndy Whitcroft			# conditional.
2436773647a0SAndy Whitcroft			substr($s, 0, length($c), '');
2437c2fdda0dSAndy Whitcroft			$s =~ s/\n.*//g;
2438c2fdda0dSAndy Whitcroft
2439c2fdda0dSAndy Whitcroft			if ($s =~ /^\s*;/) {
2440c2fdda0dSAndy Whitcroft				ERROR("while should follow close brace '}'\n" . $hereprev);
2441c2fdda0dSAndy Whitcroft			}
2442c2fdda0dSAndy Whitcroft		}
2443c2fdda0dSAndy Whitcroft
24440a920b5bSAndy Whitcroft#studly caps, commented out until figure out how to distinguish between use of existing and adding new
24450a920b5bSAndy Whitcroft#		if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) {
24460a920b5bSAndy Whitcroft#		    print "No studly caps, use _\n";
24470a920b5bSAndy Whitcroft#		    print "$herecurr";
24480a920b5bSAndy Whitcroft#		    $clean = 0;
24490a920b5bSAndy Whitcroft#		}
24500a920b5bSAndy Whitcroft
24510a920b5bSAndy Whitcroft#no spaces allowed after \ in define
2452c45dcabdSAndy Whitcroft		if ($line=~/\#\s*define.*\\\s$/) {
2453de7d4f0eSAndy Whitcroft			WARN("Whitepspace after \\ makes next lines useless\n" . $herecurr);
24540a920b5bSAndy Whitcroft		}
24550a920b5bSAndy Whitcroft
2456653d4876SAndy Whitcroft#warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
2457c45dcabdSAndy Whitcroft		if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
2458e09dec48SAndy Whitcroft			my $file = "$1.h";
2459e09dec48SAndy Whitcroft			my $checkfile = "include/linux/$file";
2460e09dec48SAndy Whitcroft			if (-f "$root/$checkfile" &&
2461e09dec48SAndy Whitcroft			    $realfile ne $checkfile &&
24627840a94cSWolfram Sang			    $1 !~ /$allowed_asm_includes/)
2463c45dcabdSAndy Whitcroft			{
2464e09dec48SAndy Whitcroft				if ($realfile =~ m{^arch/}) {
2465e09dec48SAndy Whitcroft					CHK("Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
2466e09dec48SAndy Whitcroft				} else {
2467e09dec48SAndy Whitcroft					WARN("Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
2468e09dec48SAndy Whitcroft				}
24690a920b5bSAndy Whitcroft			}
24700a920b5bSAndy Whitcroft		}
24710a920b5bSAndy Whitcroft
2472653d4876SAndy Whitcroft# multi-statement macros should be enclosed in a do while loop, grab the
2473653d4876SAndy Whitcroft# first statement and ensure its the whole macro if its not enclosed
2474cf655043SAndy Whitcroft# in a known good container
2475b8f96a31SAndy Whitcroft		if ($realfile !~ m@/vmlinux.lds.h$@ &&
2476b8f96a31SAndy Whitcroft		    $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
2477d8aaf121SAndy Whitcroft			my $ln = $linenr;
2478d8aaf121SAndy Whitcroft			my $cnt = $realcnt;
2479c45dcabdSAndy Whitcroft			my ($off, $dstat, $dcond, $rest);
2480c45dcabdSAndy Whitcroft			my $ctx = '';
2481653d4876SAndy Whitcroft
2482c45dcabdSAndy Whitcroft			my $args = defined($1);
2483de7d4f0eSAndy Whitcroft
2484c45dcabdSAndy Whitcroft			# Find the end of the macro and limit our statement
2485c45dcabdSAndy Whitcroft			# search to that.
2486c45dcabdSAndy Whitcroft			while ($cnt > 0 && defined $lines[$ln - 1] &&
2487c45dcabdSAndy Whitcroft				$lines[$ln - 1] =~ /^(?:-|..*\\$)/)
2488c45dcabdSAndy Whitcroft			{
2489c45dcabdSAndy Whitcroft				$ctx .= $rawlines[$ln - 1] . "\n";
2490a3bb97a7SAndy Whitcroft				$cnt-- if ($lines[$ln - 1] !~ /^-/);
2491c45dcabdSAndy Whitcroft				$ln++;
2492de7d4f0eSAndy Whitcroft			}
2493c45dcabdSAndy Whitcroft			$ctx .= $rawlines[$ln - 1];
2494d8aaf121SAndy Whitcroft
2495c45dcabdSAndy Whitcroft			($dstat, $dcond, $ln, $cnt, $off) =
2496c45dcabdSAndy Whitcroft				ctx_statement_block($linenr, $ln - $linenr + 1, 0);
2497c45dcabdSAndy Whitcroft			#print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
2498a3bb97a7SAndy Whitcroft			#print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
2499c45dcabdSAndy Whitcroft
2500c45dcabdSAndy Whitcroft			# Extract the remainder of the define (if any) and
2501c45dcabdSAndy Whitcroft			# rip off surrounding spaces, and trailing \'s.
2502c45dcabdSAndy Whitcroft			$rest = '';
2503636d140aSAndy Whitcroft			while ($off != 0 || ($cnt > 0 && $rest =~ /\\\s*$/)) {
2504636d140aSAndy Whitcroft				#print "ADDING cnt<$cnt> $off <" . substr($lines[$ln - 1], $off) . "> rest<$rest>\n";
2505a3bb97a7SAndy Whitcroft				if ($off != 0 || $lines[$ln - 1] !~ /^-/) {
2506c45dcabdSAndy Whitcroft					$rest .= substr($lines[$ln - 1], $off) . "\n";
2507c45dcabdSAndy Whitcroft					$cnt--;
2508a3bb97a7SAndy Whitcroft				}
2509a3bb97a7SAndy Whitcroft				$ln++;
2510c45dcabdSAndy Whitcroft				$off = 0;
2511c45dcabdSAndy Whitcroft			}
2512c45dcabdSAndy Whitcroft			$rest =~ s/\\\n.//g;
2513c45dcabdSAndy Whitcroft			$rest =~ s/^\s*//s;
2514c45dcabdSAndy Whitcroft			$rest =~ s/\s*$//s;
2515c45dcabdSAndy Whitcroft
2516c45dcabdSAndy Whitcroft			# Clean up the original statement.
2517c45dcabdSAndy Whitcroft			if ($args) {
2518c45dcabdSAndy Whitcroft				substr($dstat, 0, length($dcond), '');
2519d8aaf121SAndy Whitcroft			} else {
2520c45dcabdSAndy Whitcroft				$dstat =~ s/^.\s*\#\s*define\s+$Ident\s*//;
2521c45dcabdSAndy Whitcroft			}
2522292f1a9bSAndy Whitcroft			$dstat =~ s/$;//g;
2523c45dcabdSAndy Whitcroft			$dstat =~ s/\\\n.//g;
2524c45dcabdSAndy Whitcroft			$dstat =~ s/^\s*//s;
2525c45dcabdSAndy Whitcroft			$dstat =~ s/\s*$//s;
2526c45dcabdSAndy Whitcroft
2527c45dcabdSAndy Whitcroft			# Flatten any parentheses and braces
2528bf30d6edSAndy Whitcroft			while ($dstat =~ s/\([^\(\)]*\)/1/ ||
2529bf30d6edSAndy Whitcroft			       $dstat =~ s/\{[^\{\}]*\}/1/ ||
2530bf30d6edSAndy Whitcroft			       $dstat =~ s/\[[^\{\}]*\]/1/)
2531bf30d6edSAndy Whitcroft			{
2532c45dcabdSAndy Whitcroft			}
2533c45dcabdSAndy Whitcroft
2534c45dcabdSAndy Whitcroft			my $exceptions = qr{
2535c45dcabdSAndy Whitcroft				$Declare|
2536c45dcabdSAndy Whitcroft				module_param_named|
2537c45dcabdSAndy Whitcroft				MODULE_PARAM_DESC|
2538c45dcabdSAndy Whitcroft				DECLARE_PER_CPU|
2539c45dcabdSAndy Whitcroft				DEFINE_PER_CPU|
2540383099fdSAndy Whitcroft				__typeof__\(|
254122fd2d3eSStefani Seibold				union|
254222fd2d3eSStefani Seibold				struct|
2543ea71a0a0SAndy Whitcroft				\.$Ident\s*=\s*|
2544ea71a0a0SAndy Whitcroft				^\"|\"$
2545c45dcabdSAndy Whitcroft			}x;
25465eaa20b9SAndy Whitcroft			#print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
25475eaa20b9SAndy Whitcroft			if ($rest ne '' && $rest ne ',') {
2548c45dcabdSAndy Whitcroft				if ($rest !~ /while\s*\(/ &&
2549c45dcabdSAndy Whitcroft				    $dstat !~ /$exceptions/)
2550c45dcabdSAndy Whitcroft				{
2551c45dcabdSAndy Whitcroft					ERROR("Macros with multiple statements should be enclosed in a do - while loop\n" . "$here\n$ctx\n");
2552c45dcabdSAndy Whitcroft				}
2553c45dcabdSAndy Whitcroft
2554c45dcabdSAndy Whitcroft			} elsif ($ctx !~ /;/) {
2555c45dcabdSAndy Whitcroft				if ($dstat ne '' &&
2556c45dcabdSAndy Whitcroft				    $dstat !~ /^(?:$Ident|-?$Constant)$/ &&
2557c45dcabdSAndy Whitcroft				    $dstat !~ /$exceptions/ &&
2558b132e5d5SAndy Whitcroft				    $dstat !~ /^\.$Ident\s*=/ &&
2559c45dcabdSAndy Whitcroft				    $dstat =~ /$Operators/)
2560c45dcabdSAndy Whitcroft				{
2561de7d4f0eSAndy Whitcroft					ERROR("Macros with complex values should be enclosed in parenthesis\n" . "$here\n$ctx\n");
2562d8aaf121SAndy Whitcroft				}
25630a920b5bSAndy Whitcroft			}
2564653d4876SAndy Whitcroft		}
25650a920b5bSAndy Whitcroft
2566080ba929SMike Frysinger# make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
2567080ba929SMike Frysinger# all assignments may have only one of the following with an assignment:
2568080ba929SMike Frysinger#	.
2569080ba929SMike Frysinger#	ALIGN(...)
2570080ba929SMike Frysinger#	VMLINUX_SYMBOL(...)
2571080ba929SMike Frysinger		if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
2572080ba929SMike Frysinger			WARN("vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
2573080ba929SMike Frysinger		}
2574080ba929SMike Frysinger
2575f0a594c1SAndy Whitcroft# check for redundant bracing round if etc
257613214adfSAndy Whitcroft		if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
257713214adfSAndy Whitcroft			my ($level, $endln, @chunks) =
2578cf655043SAndy Whitcroft				ctx_statement_full($linenr, $realcnt, 1);
257913214adfSAndy Whitcroft			#print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
2580cf655043SAndy Whitcroft			#print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
2581cf655043SAndy Whitcroft			if ($#chunks > 0 && $level == 0) {
258213214adfSAndy Whitcroft				my $allowed = 0;
258313214adfSAndy Whitcroft				my $seen = 0;
2584773647a0SAndy Whitcroft				my $herectx = $here . "\n";
2585cf655043SAndy Whitcroft				my $ln = $linenr - 1;
258613214adfSAndy Whitcroft				for my $chunk (@chunks) {
258713214adfSAndy Whitcroft					my ($cond, $block) = @{$chunk};
258813214adfSAndy Whitcroft
2589773647a0SAndy Whitcroft					# If the condition carries leading newlines, then count those as offsets.
2590773647a0SAndy Whitcroft					my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
2591773647a0SAndy Whitcroft					my $offset = statement_rawlines($whitespace) - 1;
2592773647a0SAndy Whitcroft
2593773647a0SAndy Whitcroft					#print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
2594773647a0SAndy Whitcroft
2595773647a0SAndy Whitcroft					# We have looked at and allowed this specific line.
2596773647a0SAndy Whitcroft					$suppress_ifbraces{$ln + $offset} = 1;
2597773647a0SAndy Whitcroft
2598773647a0SAndy Whitcroft					$herectx .= "$rawlines[$ln + $offset]\n[...]\n";
2599cf655043SAndy Whitcroft					$ln += statement_rawlines($block) - 1;
2600cf655043SAndy Whitcroft
2601773647a0SAndy Whitcroft					substr($block, 0, length($cond), '');
260213214adfSAndy Whitcroft
260313214adfSAndy Whitcroft					$seen++ if ($block =~ /^\s*{/);
260413214adfSAndy Whitcroft
2605cf655043SAndy Whitcroft					#print "cond<$cond> block<$block> allowed<$allowed>\n";
2606cf655043SAndy Whitcroft					if (statement_lines($cond) > 1) {
2607cf655043SAndy Whitcroft						#print "APW: ALLOWED: cond<$cond>\n";
260813214adfSAndy Whitcroft						$allowed = 1;
260913214adfSAndy Whitcroft					}
261013214adfSAndy Whitcroft					if ($block =~/\b(?:if|for|while)\b/) {
2611cf655043SAndy Whitcroft						#print "APW: ALLOWED: block<$block>\n";
261213214adfSAndy Whitcroft						$allowed = 1;
261313214adfSAndy Whitcroft					}
2614cf655043SAndy Whitcroft					if (statement_block_size($block) > 1) {
2615cf655043SAndy Whitcroft						#print "APW: ALLOWED: lines block<$block>\n";
261613214adfSAndy Whitcroft						$allowed = 1;
261713214adfSAndy Whitcroft					}
261813214adfSAndy Whitcroft				}
261913214adfSAndy Whitcroft				if ($seen && !$allowed) {
2620cf655043SAndy Whitcroft					WARN("braces {} are not necessary for any arm of this statement\n" . $herectx);
262113214adfSAndy Whitcroft				}
262213214adfSAndy Whitcroft			}
262313214adfSAndy Whitcroft		}
2624773647a0SAndy Whitcroft		if (!defined $suppress_ifbraces{$linenr - 1} &&
262513214adfSAndy Whitcroft					$line =~ /\b(if|while|for|else)\b/) {
2626cf655043SAndy Whitcroft			my $allowed = 0;
2627f0a594c1SAndy Whitcroft
2628cf655043SAndy Whitcroft			# Check the pre-context.
2629cf655043SAndy Whitcroft			if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
2630cf655043SAndy Whitcroft				#print "APW: ALLOWED: pre<$1>\n";
2631cf655043SAndy Whitcroft				$allowed = 1;
2632f0a594c1SAndy Whitcroft			}
2633773647a0SAndy Whitcroft
2634773647a0SAndy Whitcroft			my ($level, $endln, @chunks) =
2635773647a0SAndy Whitcroft				ctx_statement_full($linenr, $realcnt, $-[0]);
2636773647a0SAndy Whitcroft
2637cf655043SAndy Whitcroft			# Check the condition.
2638cf655043SAndy Whitcroft			my ($cond, $block) = @{$chunks[0]};
2639773647a0SAndy Whitcroft			#print "CHECKING<$linenr> cond<$cond> block<$block>\n";
2640cf655043SAndy Whitcroft			if (defined $cond) {
2641773647a0SAndy Whitcroft				substr($block, 0, length($cond), '');
2642cf655043SAndy Whitcroft			}
2643cf655043SAndy Whitcroft			if (statement_lines($cond) > 1) {
2644cf655043SAndy Whitcroft				#print "APW: ALLOWED: cond<$cond>\n";
2645cf655043SAndy Whitcroft				$allowed = 1;
2646cf655043SAndy Whitcroft			}
2647cf655043SAndy Whitcroft			if ($block =~/\b(?:if|for|while)\b/) {
2648cf655043SAndy Whitcroft				#print "APW: ALLOWED: block<$block>\n";
2649cf655043SAndy Whitcroft				$allowed = 1;
2650cf655043SAndy Whitcroft			}
2651cf655043SAndy Whitcroft			if (statement_block_size($block) > 1) {
2652cf655043SAndy Whitcroft				#print "APW: ALLOWED: lines block<$block>\n";
2653cf655043SAndy Whitcroft				$allowed = 1;
2654cf655043SAndy Whitcroft			}
2655cf655043SAndy Whitcroft			# Check the post-context.
2656cf655043SAndy Whitcroft			if (defined $chunks[1]) {
2657cf655043SAndy Whitcroft				my ($cond, $block) = @{$chunks[1]};
2658cf655043SAndy Whitcroft				if (defined $cond) {
2659773647a0SAndy Whitcroft					substr($block, 0, length($cond), '');
2660cf655043SAndy Whitcroft				}
2661cf655043SAndy Whitcroft				if ($block =~ /^\s*\{/) {
2662cf655043SAndy Whitcroft					#print "APW: ALLOWED: chunk-1 block<$block>\n";
2663cf655043SAndy Whitcroft					$allowed = 1;
2664cf655043SAndy Whitcroft				}
2665cf655043SAndy Whitcroft			}
2666cf655043SAndy Whitcroft			if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
2667cf655043SAndy Whitcroft				my $herectx = $here . "\n";;
2668f055663cSAndy Whitcroft				my $cnt = statement_rawlines($block);
2669cf655043SAndy Whitcroft
2670f055663cSAndy Whitcroft				for (my $n = 0; $n < $cnt; $n++) {
2671f055663cSAndy Whitcroft					$herectx .= raw_line($linenr, $n) . "\n";;
2672cf655043SAndy Whitcroft				}
2673cf655043SAndy Whitcroft
2674cf655043SAndy Whitcroft				WARN("braces {} are not necessary for single statement blocks\n" . $herectx);
2675f0a594c1SAndy Whitcroft			}
2676f0a594c1SAndy Whitcroft		}
2677f0a594c1SAndy Whitcroft
2678653d4876SAndy Whitcroft# don't include deprecated include files (uses RAW line)
26794a0df2efSAndy Whitcroft		for my $inc (@dep_includes) {
2680c45dcabdSAndy Whitcroft			if ($rawline =~ m@^.\s*\#\s*include\s*\<$inc>@) {
2681de7d4f0eSAndy Whitcroft				ERROR("Don't use <$inc>: see Documentation/feature-removal-schedule.txt\n" . $herecurr);
26820a920b5bSAndy Whitcroft			}
26830a920b5bSAndy Whitcroft		}
26840a920b5bSAndy Whitcroft
26854a0df2efSAndy Whitcroft# don't use deprecated functions
26864a0df2efSAndy Whitcroft		for my $func (@dep_functions) {
268700df344fSAndy Whitcroft			if ($line =~ /\b$func\b/) {
2688de7d4f0eSAndy Whitcroft				ERROR("Don't use $func(): see Documentation/feature-removal-schedule.txt\n" . $herecurr);
26894a0df2efSAndy Whitcroft			}
26904a0df2efSAndy Whitcroft		}
26914a0df2efSAndy Whitcroft
26924a0df2efSAndy Whitcroft# no volatiles please
26936c72ffaaSAndy Whitcroft		my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
26946c72ffaaSAndy Whitcroft		if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
2695de7d4f0eSAndy Whitcroft			WARN("Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
26964a0df2efSAndy Whitcroft		}
26974a0df2efSAndy Whitcroft
269800df344fSAndy Whitcroft# warn about #if 0
2699c45dcabdSAndy Whitcroft		if ($line =~ /^.\s*\#\s*if\s+0\b/) {
2700de7d4f0eSAndy Whitcroft			CHK("if this code is redundant consider removing it\n" .
2701de7d4f0eSAndy Whitcroft				$herecurr);
27024a0df2efSAndy Whitcroft		}
27034a0df2efSAndy Whitcroft
2704f0a594c1SAndy Whitcroft# check for needless kfree() checks
2705f0a594c1SAndy Whitcroft		if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
2706f0a594c1SAndy Whitcroft			my $expr = $1;
2707f0a594c1SAndy Whitcroft			if ($line =~ /\bkfree\(\Q$expr\E\);/) {
27083c232147SWolfram Sang				WARN("kfree(NULL) is safe this check is probably not required\n" . $hereprev);
2709f0a594c1SAndy Whitcroft			}
2710f0a594c1SAndy Whitcroft		}
27114c432a8fSGreg Kroah-Hartman# check for needless usb_free_urb() checks
27124c432a8fSGreg Kroah-Hartman		if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
27134c432a8fSGreg Kroah-Hartman			my $expr = $1;
27144c432a8fSGreg Kroah-Hartman			if ($line =~ /\busb_free_urb\(\Q$expr\E\);/) {
27154c432a8fSGreg Kroah-Hartman				WARN("usb_free_urb(NULL) is safe this check is probably not required\n" . $hereprev);
27164c432a8fSGreg Kroah-Hartman			}
27174c432a8fSGreg Kroah-Hartman		}
2718f0a594c1SAndy Whitcroft
27191a15a250SPatrick Pannuto# prefer usleep_range over udelay
27201a15a250SPatrick Pannuto		if ($line =~ /\budelay\s*\(\s*(\w+)\s*\)/) {
27211a15a250SPatrick Pannuto			# ignore udelay's < 10, however
27221a15a250SPatrick Pannuto			if (! (($1 =~ /(\d+)/) && ($1 < 10)) ) {
27231a15a250SPatrick Pannuto				CHK("usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $line);
27241a15a250SPatrick Pannuto			}
27251a15a250SPatrick Pannuto		}
27261a15a250SPatrick Pannuto
272709ef8725SPatrick Pannuto# warn about unexpectedly long msleep's
272809ef8725SPatrick Pannuto		if ($line =~ /\bmsleep\s*\((\d+)\);/) {
272909ef8725SPatrick Pannuto			if ($1 < 20) {
273009ef8725SPatrick Pannuto				WARN("msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $line);
273109ef8725SPatrick Pannuto			}
273209ef8725SPatrick Pannuto		}
273309ef8725SPatrick Pannuto
273400df344fSAndy Whitcroft# warn about #ifdefs in C files
2735c45dcabdSAndy Whitcroft#		if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
273600df344fSAndy Whitcroft#			print "#ifdef in C files should be avoided\n";
273700df344fSAndy Whitcroft#			print "$herecurr";
273800df344fSAndy Whitcroft#			$clean = 0;
273900df344fSAndy Whitcroft#		}
274000df344fSAndy Whitcroft
274122f2a2efSAndy Whitcroft# warn about spacing in #ifdefs
2742c45dcabdSAndy Whitcroft		if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
274322f2a2efSAndy Whitcroft			ERROR("exactly one space required after that #$1\n" . $herecurr);
274422f2a2efSAndy Whitcroft		}
274522f2a2efSAndy Whitcroft
27464a0df2efSAndy Whitcroft# check for spinlock_t definitions without a comment.
2747171ae1a4SAndy Whitcroft		if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
2748171ae1a4SAndy Whitcroft		    $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
27494a0df2efSAndy Whitcroft			my $which = $1;
27504a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
2751de7d4f0eSAndy Whitcroft				CHK("$1 definition without comment\n" . $herecurr);
27524a0df2efSAndy Whitcroft			}
27534a0df2efSAndy Whitcroft		}
27544a0df2efSAndy Whitcroft# check for memory barriers without a comment.
27554a0df2efSAndy Whitcroft		if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
27564a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
2757de7d4f0eSAndy Whitcroft				CHK("memory barrier without comment\n" . $herecurr);
27584a0df2efSAndy Whitcroft			}
27594a0df2efSAndy Whitcroft		}
27604a0df2efSAndy Whitcroft# check of hardware specific defines
2761c45dcabdSAndy Whitcroft		if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
2762de7d4f0eSAndy Whitcroft			CHK("architecture specific defines should be avoided\n" .  $herecurr);
27630a920b5bSAndy Whitcroft		}
2764653d4876SAndy Whitcroft
2765d4977c78STobias Klauser# Check that the storage class is at the beginning of a declaration
2766d4977c78STobias Klauser		if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
2767d4977c78STobias Klauser			WARN("storage class should be at the beginning of the declaration\n" . $herecurr)
2768d4977c78STobias Klauser		}
2769d4977c78STobias Klauser
2770de7d4f0eSAndy Whitcroft# check the location of the inline attribute, that it is between
2771de7d4f0eSAndy Whitcroft# storage class and type.
27729c0ca6f9SAndy Whitcroft		if ($line =~ /\b$Type\s+$Inline\b/ ||
27739c0ca6f9SAndy Whitcroft		    $line =~ /\b$Inline\s+$Storage\b/) {
2774de7d4f0eSAndy Whitcroft			ERROR("inline keyword should sit between storage class and type\n" . $herecurr);
2775de7d4f0eSAndy Whitcroft		}
2776de7d4f0eSAndy Whitcroft
27778905a67cSAndy Whitcroft# Check for __inline__ and __inline, prefer inline
27788905a67cSAndy Whitcroft		if ($line =~ /\b(__inline__|__inline)\b/) {
27798905a67cSAndy Whitcroft			WARN("plain inline is preferred over $1\n" . $herecurr);
27808905a67cSAndy Whitcroft		}
27818905a67cSAndy Whitcroft
27823d130fd0SJoe Perches# Check for __attribute__ packed, prefer __packed
27833d130fd0SJoe Perches		if ($line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
27843d130fd0SJoe Perches			WARN("__packed is preferred over __attribute__((packed))\n" . $herecurr);
27853d130fd0SJoe Perches		}
27863d130fd0SJoe Perches
27878f53a9b8SJoe Perches# check for sizeof(&)
27888f53a9b8SJoe Perches		if ($line =~ /\bsizeof\s*\(\s*\&/) {
27898f53a9b8SJoe Perches			WARN("sizeof(& should be avoided\n" . $herecurr);
27908f53a9b8SJoe Perches		}
27918f53a9b8SJoe Perches
2792428e2fdcSJoe Perches# check for line continuations in quoted strings with odd counts of "
2793428e2fdcSJoe Perches		if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) {
2794428e2fdcSJoe Perches			WARN("Avoid line continuations in quoted strings\n" . $herecurr);
2795428e2fdcSJoe Perches		}
2796428e2fdcSJoe Perches
2797de7d4f0eSAndy Whitcroft# check for new externs in .c files.
2798171ae1a4SAndy Whitcroft		if ($realfile =~ /\.c$/ && defined $stat &&
2799c45dcabdSAndy Whitcroft		    $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
2800171ae1a4SAndy Whitcroft		{
2801c45dcabdSAndy Whitcroft			my $function_name = $1;
2802c45dcabdSAndy Whitcroft			my $paren_space = $2;
2803171ae1a4SAndy Whitcroft
2804171ae1a4SAndy Whitcroft			my $s = $stat;
2805171ae1a4SAndy Whitcroft			if (defined $cond) {
2806171ae1a4SAndy Whitcroft				substr($s, 0, length($cond), '');
2807171ae1a4SAndy Whitcroft			}
2808c45dcabdSAndy Whitcroft			if ($s =~ /^\s*;/ &&
2809c45dcabdSAndy Whitcroft			    $function_name ne 'uninitialized_var')
2810c45dcabdSAndy Whitcroft			{
2811de7d4f0eSAndy Whitcroft				WARN("externs should be avoided in .c files\n" .  $herecurr);
2812de7d4f0eSAndy Whitcroft			}
2813de7d4f0eSAndy Whitcroft
2814171ae1a4SAndy Whitcroft			if ($paren_space =~ /\n/) {
2815171ae1a4SAndy Whitcroft				WARN("arguments for function declarations should follow identifier\n" . $herecurr);
2816171ae1a4SAndy Whitcroft			}
28179c9ba34eSAndy Whitcroft
28189c9ba34eSAndy Whitcroft		} elsif ($realfile =~ /\.c$/ && defined $stat &&
28199c9ba34eSAndy Whitcroft		    $stat =~ /^.\s*extern\s+/)
28209c9ba34eSAndy Whitcroft		{
28219c9ba34eSAndy Whitcroft			WARN("externs should be avoided in .c files\n" .  $herecurr);
2822171ae1a4SAndy Whitcroft		}
2823171ae1a4SAndy Whitcroft
2824de7d4f0eSAndy Whitcroft# checks for new __setup's
2825de7d4f0eSAndy Whitcroft		if ($rawline =~ /\b__setup\("([^"]*)"/) {
2826de7d4f0eSAndy Whitcroft			my $name = $1;
2827de7d4f0eSAndy Whitcroft
2828de7d4f0eSAndy Whitcroft			if (!grep(/$name/, @setup_docs)) {
2829de7d4f0eSAndy Whitcroft				CHK("__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
2830de7d4f0eSAndy Whitcroft			}
2831653d4876SAndy Whitcroft		}
28329c0ca6f9SAndy Whitcroft
28339c0ca6f9SAndy Whitcroft# check for pointless casting of kmalloc return
2834caf2a54fSJoe Perches		if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) {
28359c0ca6f9SAndy Whitcroft			WARN("unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
28369c0ca6f9SAndy Whitcroft		}
283713214adfSAndy Whitcroft
2838caf2a54fSJoe Perches# check for multiple semicolons
2839caf2a54fSJoe Perches		if ($line =~ /;\s*;\s*$/) {
2840caf2a54fSJoe Perches		    WARN("Statements terminations use 1 semicolon\n" . $herecurr);
2841caf2a54fSJoe Perches		}
2842caf2a54fSJoe Perches
284313214adfSAndy Whitcroft# check for gcc specific __FUNCTION__
284413214adfSAndy Whitcroft		if ($line =~ /__FUNCTION__/) {
284513214adfSAndy Whitcroft			WARN("__func__ should be used instead of gcc specific __FUNCTION__\n"  . $herecurr);
284613214adfSAndy Whitcroft		}
2847773647a0SAndy Whitcroft
28484882720bSThomas Gleixner# check for semaphores initialized locked
28494882720bSThomas Gleixner		if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
2850773647a0SAndy Whitcroft			WARN("consider using a completion\n" . $herecurr);
28511704f47bSPeter Zijlstra
2852773647a0SAndy Whitcroft		}
285333ee3b2eSAlexey Dobriyan# recommend kstrto* over simple_strto*
2854773647a0SAndy Whitcroft		if ($line =~ /\bsimple_(strto.*?)\s*\(/) {
285533ee3b2eSAlexey Dobriyan			WARN("consider using kstrto* in preference to simple_$1\n" . $herecurr);
2856773647a0SAndy Whitcroft		}
2857f3db6639SMichael Ellerman# check for __initcall(), use device_initcall() explicitly please
2858f3db6639SMichael Ellerman		if ($line =~ /^.\s*__initcall\s*\(/) {
2859f3db6639SMichael Ellerman			WARN("please use device_initcall() instead of __initcall()\n" . $herecurr);
2860f3db6639SMichael Ellerman		}
286179404849SEmese Revfy# check for various ops structs, ensure they are const.
286279404849SEmese Revfy		my $struct_ops = qr{acpi_dock_ops|
286379404849SEmese Revfy				address_space_operations|
286479404849SEmese Revfy				backlight_ops|
286579404849SEmese Revfy				block_device_operations|
286679404849SEmese Revfy				dentry_operations|
286779404849SEmese Revfy				dev_pm_ops|
286879404849SEmese Revfy				dma_map_ops|
286979404849SEmese Revfy				extent_io_ops|
287079404849SEmese Revfy				file_lock_operations|
287179404849SEmese Revfy				file_operations|
287279404849SEmese Revfy				hv_ops|
287379404849SEmese Revfy				ide_dma_ops|
287479404849SEmese Revfy				intel_dvo_dev_ops|
287579404849SEmese Revfy				item_operations|
287679404849SEmese Revfy				iwl_ops|
287779404849SEmese Revfy				kgdb_arch|
287879404849SEmese Revfy				kgdb_io|
287979404849SEmese Revfy				kset_uevent_ops|
288079404849SEmese Revfy				lock_manager_operations|
288179404849SEmese Revfy				microcode_ops|
288279404849SEmese Revfy				mtrr_ops|
288379404849SEmese Revfy				neigh_ops|
288479404849SEmese Revfy				nlmsvc_binding|
288579404849SEmese Revfy				pci_raw_ops|
288679404849SEmese Revfy				pipe_buf_operations|
288779404849SEmese Revfy				platform_hibernation_ops|
288879404849SEmese Revfy				platform_suspend_ops|
288979404849SEmese Revfy				proto_ops|
289079404849SEmese Revfy				rpc_pipe_ops|
289179404849SEmese Revfy				seq_operations|
289279404849SEmese Revfy				snd_ac97_build_ops|
289379404849SEmese Revfy				soc_pcmcia_socket_ops|
289479404849SEmese Revfy				stacktrace_ops|
289579404849SEmese Revfy				sysfs_ops|
289679404849SEmese Revfy				tty_operations|
289779404849SEmese Revfy				usb_mon_operations|
289879404849SEmese Revfy				wd_ops}x;
28996903ffb2SAndy Whitcroft		if ($line !~ /\bconst\b/ &&
290079404849SEmese Revfy		    $line =~ /\bstruct\s+($struct_ops)\b/) {
29016903ffb2SAndy Whitcroft			WARN("struct $1 should normally be const\n" .
29026903ffb2SAndy Whitcroft				$herecurr);
29032b6db5cbSAndy Whitcroft		}
2904773647a0SAndy Whitcroft
2905773647a0SAndy Whitcroft# use of NR_CPUS is usually wrong
2906773647a0SAndy Whitcroft# ignore definitions of NR_CPUS and usage to define arrays as likely right
2907773647a0SAndy Whitcroft		if ($line =~ /\bNR_CPUS\b/ &&
2908c45dcabdSAndy Whitcroft		    $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
2909c45dcabdSAndy Whitcroft		    $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
2910171ae1a4SAndy Whitcroft		    $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
2911171ae1a4SAndy Whitcroft		    $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
2912171ae1a4SAndy Whitcroft		    $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
2913773647a0SAndy Whitcroft		{
2914773647a0SAndy Whitcroft			WARN("usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
2915773647a0SAndy Whitcroft		}
29169c9ba34eSAndy Whitcroft
29179c9ba34eSAndy Whitcroft# check for %L{u,d,i} in strings
29189c9ba34eSAndy Whitcroft		my $string;
29199c9ba34eSAndy Whitcroft		while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
29209c9ba34eSAndy Whitcroft			$string = substr($rawline, $-[1], $+[1] - $-[1]);
29212a1bc5d5SAndy Whitcroft			$string =~ s/%%/__/g;
29229c9ba34eSAndy Whitcroft			if ($string =~ /(?<!%)%L[udi]/) {
29239c9ba34eSAndy Whitcroft				WARN("\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
29249c9ba34eSAndy Whitcroft				last;
29259c9ba34eSAndy Whitcroft			}
29269c9ba34eSAndy Whitcroft		}
2927691d77b6SAndy Whitcroft
2928691d77b6SAndy Whitcroft# whine mightly about in_atomic
2929691d77b6SAndy Whitcroft		if ($line =~ /\bin_atomic\s*\(/) {
2930691d77b6SAndy Whitcroft			if ($realfile =~ m@^drivers/@) {
2931691d77b6SAndy Whitcroft				ERROR("do not use in_atomic in drivers\n" . $herecurr);
2932f4a87736SAndy Whitcroft			} elsif ($realfile !~ m@^kernel/@) {
2933691d77b6SAndy Whitcroft				WARN("use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
2934691d77b6SAndy Whitcroft			}
2935691d77b6SAndy Whitcroft		}
29361704f47bSPeter Zijlstra
29371704f47bSPeter Zijlstra# check for lockdep_set_novalidate_class
29381704f47bSPeter Zijlstra		if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
29391704f47bSPeter Zijlstra		    $line =~ /__lockdep_no_validate__\s*\)/ ) {
29401704f47bSPeter Zijlstra			if ($realfile !~ m@^kernel/lockdep@ &&
29411704f47bSPeter Zijlstra			    $realfile !~ m@^include/linux/lockdep@ &&
29421704f47bSPeter Zijlstra			    $realfile !~ m@^drivers/base/core@) {
29431704f47bSPeter Zijlstra				ERROR("lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
29441704f47bSPeter Zijlstra			}
29451704f47bSPeter Zijlstra		}
294688f8831cSDave Jones
294788f8831cSDave Jones		if ($line =~ /debugfs_create_file.*S_IWUGO/ ||
294888f8831cSDave Jones		    $line =~ /DEVICE_ATTR.*S_IWUGO/ ) {
294988f8831cSDave Jones			WARN("Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
295088f8831cSDave Jones		}
2951309c00c7SDave Jones
2952309c00c7SDave Jones		# Check for memset with swapped arguments
2953309c00c7SDave Jones		if ($line =~ /memset.*\,(\ |)(0x|)0(\ |0|)\);/) {
2954309c00c7SDave Jones			ERROR("memset size is 3rd argument, not the second.\n" . $herecurr);
2955309c00c7SDave Jones		}
295613214adfSAndy Whitcroft	}
295713214adfSAndy Whitcroft
295813214adfSAndy Whitcroft	# If we have no input at all, then there is nothing to report on
295913214adfSAndy Whitcroft	# so just keep quiet.
296013214adfSAndy Whitcroft	if ($#rawlines == -1) {
296113214adfSAndy Whitcroft		exit(0);
29620a920b5bSAndy Whitcroft	}
29630a920b5bSAndy Whitcroft
29648905a67cSAndy Whitcroft	# In mailback mode only produce a report in the negative, for
29658905a67cSAndy Whitcroft	# things that appear to be patches.
29668905a67cSAndy Whitcroft	if ($mailback && ($clean == 1 || !$is_patch)) {
29678905a67cSAndy Whitcroft		exit(0);
29688905a67cSAndy Whitcroft	}
29698905a67cSAndy Whitcroft
29708905a67cSAndy Whitcroft	# This is not a patch, and we are are in 'no-patch' mode so
29718905a67cSAndy Whitcroft	# just keep quiet.
29728905a67cSAndy Whitcroft	if (!$chk_patch && !$is_patch) {
29738905a67cSAndy Whitcroft		exit(0);
29748905a67cSAndy Whitcroft	}
29758905a67cSAndy Whitcroft
29768905a67cSAndy Whitcroft	if (!$is_patch) {
2977de7d4f0eSAndy Whitcroft		ERROR("Does not appear to be a unified-diff format patch\n");
29780a920b5bSAndy Whitcroft	}
29790a920b5bSAndy Whitcroft	if ($is_patch && $chk_signoff && $signoff == 0) {
2980de7d4f0eSAndy Whitcroft		ERROR("Missing Signed-off-by: line(s)\n");
29810a920b5bSAndy Whitcroft	}
29820a920b5bSAndy Whitcroft
2983f0a594c1SAndy Whitcroft	print report_dump();
298413214adfSAndy Whitcroft	if ($summary && !($clean == 1 && $quiet == 1)) {
298513214adfSAndy Whitcroft		print "$filename " if ($summary_file);
29866c72ffaaSAndy Whitcroft		print "total: $cnt_error errors, $cnt_warn warnings, " .
29876c72ffaaSAndy Whitcroft			(($check)? "$cnt_chk checks, " : "") .
29886c72ffaaSAndy Whitcroft			"$cnt_lines lines checked\n";
29898905a67cSAndy Whitcroft		print "\n" if ($quiet == 0);
29906c72ffaaSAndy Whitcroft	}
29918905a67cSAndy Whitcroft
2992d2c0a235SAndy Whitcroft	if ($quiet == 0) {
2993d2c0a235SAndy Whitcroft		# If there were whitespace errors which cleanpatch can fix
2994d2c0a235SAndy Whitcroft		# then suggest that.
2995d2c0a235SAndy Whitcroft		if ($rpt_cleaners) {
2996d2c0a235SAndy Whitcroft			print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n";
2997d2c0a235SAndy Whitcroft			print "      scripts/cleanfile\n\n";
2998b0781216SMike Frysinger			$rpt_cleaners = 0;
2999d2c0a235SAndy Whitcroft		}
3000d2c0a235SAndy Whitcroft	}
3001d2c0a235SAndy Whitcroft
30020a920b5bSAndy Whitcroft	if ($clean == 1 && $quiet == 0) {
3003c2fdda0dSAndy Whitcroft		print "$vname has no obvious style problems and is ready for submission.\n"
30040a920b5bSAndy Whitcroft	}
30050a920b5bSAndy Whitcroft	if ($clean == 0 && $quiet == 0) {
3006c2fdda0dSAndy Whitcroft		print "$vname has style problems, please review.  If any of these errors\n";
30070a920b5bSAndy Whitcroft		print "are false positives report them to the maintainer, see\n";
30080a920b5bSAndy Whitcroft		print "CHECKPATCH in MAINTAINERS.\n";
30090a920b5bSAndy Whitcroft	}
301013214adfSAndy Whitcroft
30110a920b5bSAndy Whitcroft	return $clean;
30120a920b5bSAndy Whitcroft}
3013