xref: /linux-6.15/scripts/checkpatch.pl (revision 7d2367af)
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|
148417495edSAndy Whitcroft			__ref
1496c72ffaaSAndy Whitcroft		}x;
15052131292SWolfram Sang
15152131292SWolfram Sang# Notes to $Attribute:
15252131292SWolfram Sang# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
1536c72ffaaSAndy Whitcroftour $Attribute	= qr{
1546c72ffaaSAndy Whitcroft			const|
15503f1df7dSJoe Perches			__percpu|
15603f1df7dSJoe Perches			__nocast|
15703f1df7dSJoe Perches			__safe|
15803f1df7dSJoe Perches			__bitwise__|
15903f1df7dSJoe Perches			__packed__|
16003f1df7dSJoe Perches			__packed2__|
16103f1df7dSJoe Perches			__naked|
16203f1df7dSJoe Perches			__maybe_unused|
16303f1df7dSJoe Perches			__always_unused|
16403f1df7dSJoe Perches			__noreturn|
16503f1df7dSJoe Perches			__used|
16603f1df7dSJoe Perches			__cold|
16703f1df7dSJoe Perches			__noclone|
16803f1df7dSJoe Perches			__deprecated|
1696c72ffaaSAndy Whitcroft			__read_mostly|
1706c72ffaaSAndy Whitcroft			__kprobes|
17152131292SWolfram Sang			__(?:mem|cpu|dev|)(?:initdata|initconst|init\b)|
17224e1d81aSAndy Whitcroft			____cacheline_aligned|
17324e1d81aSAndy Whitcroft			____cacheline_aligned_in_smp|
1745fe3af11SAndy Whitcroft			____cacheline_internodealigned_in_smp|
1755fe3af11SAndy Whitcroft			__weak
1766c72ffaaSAndy Whitcroft		  }x;
177c45dcabdSAndy Whitcroftour $Modifier;
1786c72ffaaSAndy Whitcroftour $Inline	= qr{inline|__always_inline|noinline};
1796c72ffaaSAndy Whitcroftour $Member	= qr{->$Ident|\.$Ident|\[[^]]*\]};
1806c72ffaaSAndy Whitcroftour $Lval	= qr{$Ident(?:$Member)*};
1816c72ffaaSAndy Whitcroft
1826c72ffaaSAndy Whitcroftour $Constant	= qr{(?:[0-9]+|0x[0-9a-fA-F]+)[UL]*};
1836c72ffaaSAndy Whitcroftour $Assignment	= qr{(?:\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=)};
18486f9d059SAndy Whitcroftour $Compare    = qr{<=|>=|==|!=|<|>};
1856c72ffaaSAndy Whitcroftour $Operators	= qr{
1866c72ffaaSAndy Whitcroft			<=|>=|==|!=|
1876c72ffaaSAndy Whitcroft			=>|->|<<|>>|<|>|!|~|
188c2fdda0dSAndy Whitcroft			&&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%
1896c72ffaaSAndy Whitcroft		  }x;
1906c72ffaaSAndy Whitcroft
1918905a67cSAndy Whitcroftour $NonptrType;
1928905a67cSAndy Whitcroftour $Type;
1938905a67cSAndy Whitcroftour $Declare;
1948905a67cSAndy Whitcroft
195171ae1a4SAndy Whitcroftour $UTF8	= qr {
196171ae1a4SAndy Whitcroft	[\x09\x0A\x0D\x20-\x7E]              # ASCII
197171ae1a4SAndy Whitcroft	| [\xC2-\xDF][\x80-\xBF]             # non-overlong 2-byte
198171ae1a4SAndy Whitcroft	|  \xE0[\xA0-\xBF][\x80-\xBF]        # excluding overlongs
199171ae1a4SAndy Whitcroft	| [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}  # straight 3-byte
200171ae1a4SAndy Whitcroft	|  \xED[\x80-\x9F][\x80-\xBF]        # excluding surrogates
201171ae1a4SAndy Whitcroft	|  \xF0[\x90-\xBF][\x80-\xBF]{2}     # planes 1-3
202171ae1a4SAndy Whitcroft	| [\xF1-\xF3][\x80-\xBF]{3}          # planes 4-15
203171ae1a4SAndy Whitcroft	|  \xF4[\x80-\x8F][\x80-\xBF]{2}     # plane 16
204171ae1a4SAndy Whitcroft}x;
205171ae1a4SAndy Whitcroft
2068ed22cadSAndy Whitcroftour $typeTypedefs = qr{(?x:
207fb9e9096SAndy Whitcroft	(?:__)?(?:u|s|be|le)(?:8|16|32|64)|
2088ed22cadSAndy Whitcroft	atomic_t
2098ed22cadSAndy Whitcroft)};
2108ed22cadSAndy Whitcroft
211691e669bSJoe Perchesour $logFunctions = qr{(?x:
212691e669bSJoe Perches	printk|
213b0531722SJoe Perches	[a-z]+_(emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)|
214691e669bSJoe Perches	WARN|
215b0531722SJoe Perches	panic|
216b0531722SJoe Perches	MODULE_[A-Z_]+
217691e669bSJoe Perches)};
218691e669bSJoe Perches
2198905a67cSAndy Whitcroftour @typeList = (
2208905a67cSAndy Whitcroft	qr{void},
221c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?char},
222c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?short},
223c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?int},
224c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?long},
225c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?long\s+int},
226c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?long\s+long},
227c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?long\s+long\s+int},
2288905a67cSAndy Whitcroft	qr{unsigned},
2298905a67cSAndy Whitcroft	qr{float},
2308905a67cSAndy Whitcroft	qr{double},
2318905a67cSAndy Whitcroft	qr{bool},
2328905a67cSAndy Whitcroft	qr{struct\s+$Ident},
2338905a67cSAndy Whitcroft	qr{union\s+$Ident},
2348905a67cSAndy Whitcroft	qr{enum\s+$Ident},
2358905a67cSAndy Whitcroft	qr{${Ident}_t},
2368905a67cSAndy Whitcroft	qr{${Ident}_handler},
2378905a67cSAndy Whitcroft	qr{${Ident}_handler_fn},
2388905a67cSAndy Whitcroft);
239c45dcabdSAndy Whitcroftour @modifierList = (
240c45dcabdSAndy Whitcroft	qr{fastcall},
241c45dcabdSAndy Whitcroft);
2428905a67cSAndy Whitcroft
2437840a94cSWolfram Sangour $allowed_asm_includes = qr{(?x:
2447840a94cSWolfram Sang	irq|
2457840a94cSWolfram Sang	memory
2467840a94cSWolfram Sang)};
2477840a94cSWolfram Sang# memory.h: ARM has a custom one
2487840a94cSWolfram Sang
2498905a67cSAndy Whitcroftsub build_types {
250d2172eb5SAndy Whitcroft	my $mods = "(?x:  \n" . join("|\n  ", @modifierList) . "\n)";
251d2172eb5SAndy Whitcroft	my $all = "(?x:  \n" . join("|\n  ", @typeList) . "\n)";
252c8cb2ca3SAndy Whitcroft	$Modifier	= qr{(?:$Attribute|$Sparse|$mods)};
2538905a67cSAndy Whitcroft	$NonptrType	= qr{
254d2172eb5SAndy Whitcroft			(?:$Modifier\s+|const\s+)*
255cf655043SAndy Whitcroft			(?:
256c45dcabdSAndy Whitcroft				(?:typeof|__typeof__)\s*\(\s*\**\s*$Ident\s*\)|
2578ed22cadSAndy Whitcroft				(?:$typeTypedefs\b)|
258c45dcabdSAndy Whitcroft				(?:${all}\b)
259cf655043SAndy Whitcroft			)
260c8cb2ca3SAndy Whitcroft			(?:\s+$Modifier|\s+const)*
2618905a67cSAndy Whitcroft		  }x;
2628905a67cSAndy Whitcroft	$Type	= qr{
263c45dcabdSAndy Whitcroft			$NonptrType
26465863862SAndy Whitcroft			(?:[\s\*]+\s*const|[\s\*]+|(?:\s*\[\s*\])+)?
265c8cb2ca3SAndy Whitcroft			(?:\s+$Inline|\s+$Modifier)*
2668905a67cSAndy Whitcroft		  }x;
2678905a67cSAndy Whitcroft	$Declare	= qr{(?:$Storage\s+)?$Type};
2688905a67cSAndy Whitcroft}
2698905a67cSAndy Whitcroftbuild_types();
2706c72ffaaSAndy Whitcroft
271*7d2367afSJoe Perchesour $match_balanced_parentheses = qr/(\((?:[^\(\)]+|(-1))*\))/;
272*7d2367afSJoe Perches
273*7d2367afSJoe Perchesour $Typecast	= qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
274*7d2367afSJoe Perchesour $LvalOrFunc	= qr{($Lval)\s*($match_balanced_parentheses{0,1})\s*};
275*7d2367afSJoe Perches
276*7d2367afSJoe Perchessub deparenthesize {
277*7d2367afSJoe Perches	my ($string) = @_;
278*7d2367afSJoe Perches	return "" if (!defined($string));
279*7d2367afSJoe Perches	$string =~ s@^\s*\(\s*@@g;
280*7d2367afSJoe Perches	$string =~ s@\s*\)\s*$@@g;
281*7d2367afSJoe Perches	$string =~ s@\s+@ @g;
282*7d2367afSJoe Perches	return $string;
283*7d2367afSJoe Perches}
284*7d2367afSJoe Perches
2856c72ffaaSAndy Whitcroft$chk_signoff = 0 if ($file);
2860a920b5bSAndy Whitcroft
2874a0df2efSAndy Whitcroftmy @dep_includes = ();
2884a0df2efSAndy Whitcroftmy @dep_functions = ();
2896c72ffaaSAndy Whitcroftmy $removal = "Documentation/feature-removal-schedule.txt";
2906c72ffaaSAndy Whitcroftif ($tree && -f "$root/$removal") {
29121caa13cSAndy Whitcroft	open(my $REMOVE, '<', "$root/$removal") ||
2926c72ffaaSAndy Whitcroft				die "$P: $removal: open failed - $!\n";
29321caa13cSAndy Whitcroft	while (<$REMOVE>) {
294f0a594c1SAndy Whitcroft		if (/^Check:\s+(.*\S)/) {
295f0a594c1SAndy Whitcroft			for my $entry (split(/[, ]+/, $1)) {
296f0a594c1SAndy Whitcroft				if ($entry =~ m@include/(.*)@) {
2974a0df2efSAndy Whitcroft					push(@dep_includes, $1);
2984a0df2efSAndy Whitcroft
299f0a594c1SAndy Whitcroft				} elsif ($entry !~ m@/@) {
300f0a594c1SAndy Whitcroft					push(@dep_functions, $entry);
301f0a594c1SAndy Whitcroft				}
3024a0df2efSAndy Whitcroft			}
3030a920b5bSAndy Whitcroft		}
3040a920b5bSAndy Whitcroft	}
30521caa13cSAndy Whitcroft	close($REMOVE);
3060a920b5bSAndy Whitcroft}
3070a920b5bSAndy Whitcroft
30800df344fSAndy Whitcroftmy @rawlines = ();
309c2fdda0dSAndy Whitcroftmy @lines = ();
310c2fdda0dSAndy Whitcroftmy $vname;
3116c72ffaaSAndy Whitcroftfor my $filename (@ARGV) {
31221caa13cSAndy Whitcroft	my $FILE;
3136c72ffaaSAndy Whitcroft	if ($file) {
31421caa13cSAndy Whitcroft		open($FILE, '-|', "diff -u /dev/null $filename") ||
3156c72ffaaSAndy Whitcroft			die "$P: $filename: diff failed - $!\n";
31621caa13cSAndy Whitcroft	} elsif ($filename eq '-') {
31721caa13cSAndy Whitcroft		open($FILE, '<&STDIN');
3186c72ffaaSAndy Whitcroft	} else {
31921caa13cSAndy Whitcroft		open($FILE, '<', "$filename") ||
3206c72ffaaSAndy Whitcroft			die "$P: $filename: open failed - $!\n";
3216c72ffaaSAndy Whitcroft	}
322c2fdda0dSAndy Whitcroft	if ($filename eq '-') {
323c2fdda0dSAndy Whitcroft		$vname = 'Your patch';
324c2fdda0dSAndy Whitcroft	} else {
325c2fdda0dSAndy Whitcroft		$vname = $filename;
326c2fdda0dSAndy Whitcroft	}
32721caa13cSAndy Whitcroft	while (<$FILE>) {
3280a920b5bSAndy Whitcroft		chomp;
32900df344fSAndy Whitcroft		push(@rawlines, $_);
3306c72ffaaSAndy Whitcroft	}
33121caa13cSAndy Whitcroft	close($FILE);
332c2fdda0dSAndy Whitcroft	if (!process($filename)) {
3330a920b5bSAndy Whitcroft		$exit = 1;
3340a920b5bSAndy Whitcroft	}
33500df344fSAndy Whitcroft	@rawlines = ();
33613214adfSAndy Whitcroft	@lines = ();
3370a920b5bSAndy Whitcroft}
3380a920b5bSAndy Whitcroft
3390a920b5bSAndy Whitcroftexit($exit);
3400a920b5bSAndy Whitcroft
3410a920b5bSAndy Whitcroftsub top_of_kernel_tree {
3426c72ffaaSAndy Whitcroft	my ($root) = @_;
3436c72ffaaSAndy Whitcroft
3446c72ffaaSAndy Whitcroft	my @tree_check = (
3456c72ffaaSAndy Whitcroft		"COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
3466c72ffaaSAndy Whitcroft		"README", "Documentation", "arch", "include", "drivers",
3476c72ffaaSAndy Whitcroft		"fs", "init", "ipc", "kernel", "lib", "scripts",
3486c72ffaaSAndy Whitcroft	);
3496c72ffaaSAndy Whitcroft
3506c72ffaaSAndy Whitcroft	foreach my $check (@tree_check) {
3516c72ffaaSAndy Whitcroft		if (! -e $root . '/' . $check) {
3520a920b5bSAndy Whitcroft			return 0;
3530a920b5bSAndy Whitcroft		}
3546c72ffaaSAndy Whitcroft	}
3556c72ffaaSAndy Whitcroft	return 1;
3566c72ffaaSAndy Whitcroft}
3570a920b5bSAndy Whitcroft
3580a920b5bSAndy Whitcroftsub expand_tabs {
3590a920b5bSAndy Whitcroft	my ($str) = @_;
3600a920b5bSAndy Whitcroft
3610a920b5bSAndy Whitcroft	my $res = '';
3620a920b5bSAndy Whitcroft	my $n = 0;
3630a920b5bSAndy Whitcroft	for my $c (split(//, $str)) {
3640a920b5bSAndy Whitcroft		if ($c eq "\t") {
3650a920b5bSAndy Whitcroft			$res .= ' ';
3660a920b5bSAndy Whitcroft			$n++;
3670a920b5bSAndy Whitcroft			for (; ($n % 8) != 0; $n++) {
3680a920b5bSAndy Whitcroft				$res .= ' ';
3690a920b5bSAndy Whitcroft			}
3700a920b5bSAndy Whitcroft			next;
3710a920b5bSAndy Whitcroft		}
3720a920b5bSAndy Whitcroft		$res .= $c;
3730a920b5bSAndy Whitcroft		$n++;
3740a920b5bSAndy Whitcroft	}
3750a920b5bSAndy Whitcroft
3760a920b5bSAndy Whitcroft	return $res;
3770a920b5bSAndy Whitcroft}
3786c72ffaaSAndy Whitcroftsub copy_spacing {
379773647a0SAndy Whitcroft	(my $res = shift) =~ tr/\t/ /c;
3806c72ffaaSAndy Whitcroft	return $res;
3816c72ffaaSAndy Whitcroft}
3820a920b5bSAndy Whitcroft
3834a0df2efSAndy Whitcroftsub line_stats {
3844a0df2efSAndy Whitcroft	my ($line) = @_;
3854a0df2efSAndy Whitcroft
3864a0df2efSAndy Whitcroft	# Drop the diff line leader and expand tabs
3874a0df2efSAndy Whitcroft	$line =~ s/^.//;
3884a0df2efSAndy Whitcroft	$line = expand_tabs($line);
3894a0df2efSAndy Whitcroft
3904a0df2efSAndy Whitcroft	# Pick the indent from the front of the line.
3914a0df2efSAndy Whitcroft	my ($white) = ($line =~ /^(\s*)/);
3924a0df2efSAndy Whitcroft
3934a0df2efSAndy Whitcroft	return (length($line), length($white));
3944a0df2efSAndy Whitcroft}
3954a0df2efSAndy Whitcroft
396773647a0SAndy Whitcroftmy $sanitise_quote = '';
397773647a0SAndy Whitcroft
398773647a0SAndy Whitcroftsub sanitise_line_reset {
399773647a0SAndy Whitcroft	my ($in_comment) = @_;
400773647a0SAndy Whitcroft
401773647a0SAndy Whitcroft	if ($in_comment) {
402773647a0SAndy Whitcroft		$sanitise_quote = '*/';
403773647a0SAndy Whitcroft	} else {
404773647a0SAndy Whitcroft		$sanitise_quote = '';
405773647a0SAndy Whitcroft	}
406773647a0SAndy Whitcroft}
40700df344fSAndy Whitcroftsub sanitise_line {
40800df344fSAndy Whitcroft	my ($line) = @_;
40900df344fSAndy Whitcroft
41000df344fSAndy Whitcroft	my $res = '';
41100df344fSAndy Whitcroft	my $l = '';
41200df344fSAndy Whitcroft
413c2fdda0dSAndy Whitcroft	my $qlen = 0;
414773647a0SAndy Whitcroft	my $off = 0;
415773647a0SAndy Whitcroft	my $c;
41600df344fSAndy Whitcroft
417773647a0SAndy Whitcroft	# Always copy over the diff marker.
418773647a0SAndy Whitcroft	$res = substr($line, 0, 1);
419773647a0SAndy Whitcroft
420773647a0SAndy Whitcroft	for ($off = 1; $off < length($line); $off++) {
421773647a0SAndy Whitcroft		$c = substr($line, $off, 1);
422773647a0SAndy Whitcroft
423773647a0SAndy Whitcroft		# Comments we are wacking completly including the begin
424773647a0SAndy Whitcroft		# and end, all to $;.
425773647a0SAndy Whitcroft		if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
426773647a0SAndy Whitcroft			$sanitise_quote = '*/';
427773647a0SAndy Whitcroft
428773647a0SAndy Whitcroft			substr($res, $off, 2, "$;$;");
429773647a0SAndy Whitcroft			$off++;
43000df344fSAndy Whitcroft			next;
431773647a0SAndy Whitcroft		}
43281bc0e02SAndy Whitcroft		if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
433773647a0SAndy Whitcroft			$sanitise_quote = '';
434773647a0SAndy Whitcroft			substr($res, $off, 2, "$;$;");
435773647a0SAndy Whitcroft			$off++;
436773647a0SAndy Whitcroft			next;
437773647a0SAndy Whitcroft		}
438113f04a8SDaniel Walker		if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
439113f04a8SDaniel Walker			$sanitise_quote = '//';
440113f04a8SDaniel Walker
441113f04a8SDaniel Walker			substr($res, $off, 2, $sanitise_quote);
442113f04a8SDaniel Walker			$off++;
443113f04a8SDaniel Walker			next;
444113f04a8SDaniel Walker		}
445773647a0SAndy Whitcroft
446773647a0SAndy Whitcroft		# A \ in a string means ignore the next character.
447773647a0SAndy Whitcroft		if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
448773647a0SAndy Whitcroft		    $c eq "\\") {
449773647a0SAndy Whitcroft			substr($res, $off, 2, 'XX');
450773647a0SAndy Whitcroft			$off++;
451773647a0SAndy Whitcroft			next;
452773647a0SAndy Whitcroft		}
453773647a0SAndy Whitcroft		# Regular quotes.
454773647a0SAndy Whitcroft		if ($c eq "'" || $c eq '"') {
455773647a0SAndy Whitcroft			if ($sanitise_quote eq '') {
456773647a0SAndy Whitcroft				$sanitise_quote = $c;
457773647a0SAndy Whitcroft
458773647a0SAndy Whitcroft				substr($res, $off, 1, $c);
459773647a0SAndy Whitcroft				next;
460773647a0SAndy Whitcroft			} elsif ($sanitise_quote eq $c) {
461773647a0SAndy Whitcroft				$sanitise_quote = '';
46200df344fSAndy Whitcroft			}
46300df344fSAndy Whitcroft		}
464773647a0SAndy Whitcroft
465fae17daeSAndy Whitcroft		#print "c<$c> SQ<$sanitise_quote>\n";
466773647a0SAndy Whitcroft		if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
467773647a0SAndy Whitcroft			substr($res, $off, 1, $;);
468113f04a8SDaniel Walker		} elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
469113f04a8SDaniel Walker			substr($res, $off, 1, $;);
470773647a0SAndy Whitcroft		} elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
471773647a0SAndy Whitcroft			substr($res, $off, 1, 'X');
47200df344fSAndy Whitcroft		} else {
473773647a0SAndy Whitcroft			substr($res, $off, 1, $c);
47400df344fSAndy Whitcroft		}
475c2fdda0dSAndy Whitcroft	}
476c2fdda0dSAndy Whitcroft
477113f04a8SDaniel Walker	if ($sanitise_quote eq '//') {
478113f04a8SDaniel Walker		$sanitise_quote = '';
479113f04a8SDaniel Walker	}
480113f04a8SDaniel Walker
481c2fdda0dSAndy Whitcroft	# The pathname on a #include may be surrounded by '<' and '>'.
482c45dcabdSAndy Whitcroft	if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
483c2fdda0dSAndy Whitcroft		my $clean = 'X' x length($1);
484c2fdda0dSAndy Whitcroft		$res =~ s@\<.*\>@<$clean>@;
485c2fdda0dSAndy Whitcroft
486c2fdda0dSAndy Whitcroft	# The whole of a #error is a string.
487c45dcabdSAndy Whitcroft	} elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
488c2fdda0dSAndy Whitcroft		my $clean = 'X' x length($1);
489c45dcabdSAndy Whitcroft		$res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
490c2fdda0dSAndy Whitcroft	}
491c2fdda0dSAndy Whitcroft
49200df344fSAndy Whitcroft	return $res;
49300df344fSAndy Whitcroft}
49400df344fSAndy Whitcroft
4958905a67cSAndy Whitcroftsub ctx_statement_block {
4968905a67cSAndy Whitcroft	my ($linenr, $remain, $off) = @_;
4978905a67cSAndy Whitcroft	my $line = $linenr - 1;
4988905a67cSAndy Whitcroft	my $blk = '';
4998905a67cSAndy Whitcroft	my $soff = $off;
5008905a67cSAndy Whitcroft	my $coff = $off - 1;
501773647a0SAndy Whitcroft	my $coff_set = 0;
5028905a67cSAndy Whitcroft
50313214adfSAndy Whitcroft	my $loff = 0;
50413214adfSAndy Whitcroft
5058905a67cSAndy Whitcroft	my $type = '';
5068905a67cSAndy Whitcroft	my $level = 0;
507a2750645SAndy Whitcroft	my @stack = ();
508cf655043SAndy Whitcroft	my $p;
5098905a67cSAndy Whitcroft	my $c;
5108905a67cSAndy Whitcroft	my $len = 0;
51113214adfSAndy Whitcroft
51213214adfSAndy Whitcroft	my $remainder;
5138905a67cSAndy Whitcroft	while (1) {
514a2750645SAndy Whitcroft		@stack = (['', 0]) if ($#stack == -1);
515a2750645SAndy Whitcroft
516773647a0SAndy Whitcroft		#warn "CSB: blk<$blk> remain<$remain>\n";
5178905a67cSAndy Whitcroft		# If we are about to drop off the end, pull in more
5188905a67cSAndy Whitcroft		# context.
5198905a67cSAndy Whitcroft		if ($off >= $len) {
5208905a67cSAndy Whitcroft			for (; $remain > 0; $line++) {
521dea33496SAndy Whitcroft				last if (!defined $lines[$line]);
522c2fdda0dSAndy Whitcroft				next if ($lines[$line] =~ /^-/);
5238905a67cSAndy Whitcroft				$remain--;
52413214adfSAndy Whitcroft				$loff = $len;
525c2fdda0dSAndy Whitcroft				$blk .= $lines[$line] . "\n";
5268905a67cSAndy Whitcroft				$len = length($blk);
5278905a67cSAndy Whitcroft				$line++;
5288905a67cSAndy Whitcroft				last;
5298905a67cSAndy Whitcroft			}
5308905a67cSAndy Whitcroft			# Bail if there is no further context.
5318905a67cSAndy Whitcroft			#warn "CSB: blk<$blk> off<$off> len<$len>\n";
53213214adfSAndy Whitcroft			if ($off >= $len) {
5338905a67cSAndy Whitcroft				last;
5348905a67cSAndy Whitcroft			}
5358905a67cSAndy Whitcroft		}
536cf655043SAndy Whitcroft		$p = $c;
5378905a67cSAndy Whitcroft		$c = substr($blk, $off, 1);
53813214adfSAndy Whitcroft		$remainder = substr($blk, $off);
5398905a67cSAndy Whitcroft
540773647a0SAndy Whitcroft		#warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
5414635f4fbSAndy Whitcroft
5424635f4fbSAndy Whitcroft		# Handle nested #if/#else.
5434635f4fbSAndy Whitcroft		if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
5444635f4fbSAndy Whitcroft			push(@stack, [ $type, $level ]);
5454635f4fbSAndy Whitcroft		} elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
5464635f4fbSAndy Whitcroft			($type, $level) = @{$stack[$#stack - 1]};
5474635f4fbSAndy Whitcroft		} elsif ($remainder =~ /^#\s*endif\b/) {
5484635f4fbSAndy Whitcroft			($type, $level) = @{pop(@stack)};
5494635f4fbSAndy Whitcroft		}
5504635f4fbSAndy Whitcroft
5518905a67cSAndy Whitcroft		# Statement ends at the ';' or a close '}' at the
5528905a67cSAndy Whitcroft		# outermost level.
5538905a67cSAndy Whitcroft		if ($level == 0 && $c eq ';') {
5548905a67cSAndy Whitcroft			last;
5558905a67cSAndy Whitcroft		}
5568905a67cSAndy Whitcroft
55713214adfSAndy Whitcroft		# An else is really a conditional as long as its not else if
558773647a0SAndy Whitcroft		if ($level == 0 && $coff_set == 0 &&
559773647a0SAndy Whitcroft				(!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
560773647a0SAndy Whitcroft				$remainder =~ /^(else)(?:\s|{)/ &&
561773647a0SAndy Whitcroft				$remainder !~ /^else\s+if\b/) {
562773647a0SAndy Whitcroft			$coff = $off + length($1) - 1;
563773647a0SAndy Whitcroft			$coff_set = 1;
564773647a0SAndy Whitcroft			#warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
565773647a0SAndy Whitcroft			#warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
56613214adfSAndy Whitcroft		}
56713214adfSAndy Whitcroft
5688905a67cSAndy Whitcroft		if (($type eq '' || $type eq '(') && $c eq '(') {
5698905a67cSAndy Whitcroft			$level++;
5708905a67cSAndy Whitcroft			$type = '(';
5718905a67cSAndy Whitcroft		}
5728905a67cSAndy Whitcroft		if ($type eq '(' && $c eq ')') {
5738905a67cSAndy Whitcroft			$level--;
5748905a67cSAndy Whitcroft			$type = ($level != 0)? '(' : '';
5758905a67cSAndy Whitcroft
5768905a67cSAndy Whitcroft			if ($level == 0 && $coff < $soff) {
5778905a67cSAndy Whitcroft				$coff = $off;
578773647a0SAndy Whitcroft				$coff_set = 1;
579773647a0SAndy Whitcroft				#warn "CSB: mark coff<$coff>\n";
5808905a67cSAndy Whitcroft			}
5818905a67cSAndy Whitcroft		}
5828905a67cSAndy Whitcroft		if (($type eq '' || $type eq '{') && $c eq '{') {
5838905a67cSAndy Whitcroft			$level++;
5848905a67cSAndy Whitcroft			$type = '{';
5858905a67cSAndy Whitcroft		}
5868905a67cSAndy Whitcroft		if ($type eq '{' && $c eq '}') {
5878905a67cSAndy Whitcroft			$level--;
5888905a67cSAndy Whitcroft			$type = ($level != 0)? '{' : '';
5898905a67cSAndy Whitcroft
5908905a67cSAndy Whitcroft			if ($level == 0) {
591b998e001SPatrick Pannuto				if (substr($blk, $off + 1, 1) eq ';') {
592b998e001SPatrick Pannuto					$off++;
593b998e001SPatrick Pannuto				}
5948905a67cSAndy Whitcroft				last;
5958905a67cSAndy Whitcroft			}
5968905a67cSAndy Whitcroft		}
5978905a67cSAndy Whitcroft		$off++;
5988905a67cSAndy Whitcroft	}
599a3bb97a7SAndy Whitcroft	# We are truly at the end, so shuffle to the next line.
60013214adfSAndy Whitcroft	if ($off == $len) {
601a3bb97a7SAndy Whitcroft		$loff = $len + 1;
60213214adfSAndy Whitcroft		$line++;
60313214adfSAndy Whitcroft		$remain--;
60413214adfSAndy Whitcroft	}
6058905a67cSAndy Whitcroft
6068905a67cSAndy Whitcroft	my $statement = substr($blk, $soff, $off - $soff + 1);
6078905a67cSAndy Whitcroft	my $condition = substr($blk, $soff, $coff - $soff + 1);
6088905a67cSAndy Whitcroft
6098905a67cSAndy Whitcroft	#warn "STATEMENT<$statement>\n";
6108905a67cSAndy Whitcroft	#warn "CONDITION<$condition>\n";
6118905a67cSAndy Whitcroft
612773647a0SAndy Whitcroft	#print "coff<$coff> soff<$off> loff<$loff>\n";
61313214adfSAndy Whitcroft
61413214adfSAndy Whitcroft	return ($statement, $condition,
61513214adfSAndy Whitcroft			$line, $remain + 1, $off - $loff + 1, $level);
61613214adfSAndy Whitcroft}
61713214adfSAndy Whitcroft
618cf655043SAndy Whitcroftsub statement_lines {
619cf655043SAndy Whitcroft	my ($stmt) = @_;
620cf655043SAndy Whitcroft
621cf655043SAndy Whitcroft	# Strip the diff line prefixes and rip blank lines at start and end.
622cf655043SAndy Whitcroft	$stmt =~ s/(^|\n)./$1/g;
623cf655043SAndy Whitcroft	$stmt =~ s/^\s*//;
624cf655043SAndy Whitcroft	$stmt =~ s/\s*$//;
625cf655043SAndy Whitcroft
626cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
627cf655043SAndy Whitcroft
628cf655043SAndy Whitcroft	return $#stmt_lines + 2;
629cf655043SAndy Whitcroft}
630cf655043SAndy Whitcroft
631cf655043SAndy Whitcroftsub statement_rawlines {
632cf655043SAndy Whitcroft	my ($stmt) = @_;
633cf655043SAndy Whitcroft
634cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
635cf655043SAndy Whitcroft
636cf655043SAndy Whitcroft	return $#stmt_lines + 2;
637cf655043SAndy Whitcroft}
638cf655043SAndy Whitcroft
639cf655043SAndy Whitcroftsub statement_block_size {
640cf655043SAndy Whitcroft	my ($stmt) = @_;
641cf655043SAndy Whitcroft
642cf655043SAndy Whitcroft	$stmt =~ s/(^|\n)./$1/g;
643cf655043SAndy Whitcroft	$stmt =~ s/^\s*{//;
644cf655043SAndy Whitcroft	$stmt =~ s/}\s*$//;
645cf655043SAndy Whitcroft	$stmt =~ s/^\s*//;
646cf655043SAndy Whitcroft	$stmt =~ s/\s*$//;
647cf655043SAndy Whitcroft
648cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
649cf655043SAndy Whitcroft	my @stmt_statements = ($stmt =~ /;/g);
650cf655043SAndy Whitcroft
651cf655043SAndy Whitcroft	my $stmt_lines = $#stmt_lines + 2;
652cf655043SAndy Whitcroft	my $stmt_statements = $#stmt_statements + 1;
653cf655043SAndy Whitcroft
654cf655043SAndy Whitcroft	if ($stmt_lines > $stmt_statements) {
655cf655043SAndy Whitcroft		return $stmt_lines;
656cf655043SAndy Whitcroft	} else {
657cf655043SAndy Whitcroft		return $stmt_statements;
658cf655043SAndy Whitcroft	}
659cf655043SAndy Whitcroft}
660cf655043SAndy Whitcroft
66113214adfSAndy Whitcroftsub ctx_statement_full {
66213214adfSAndy Whitcroft	my ($linenr, $remain, $off) = @_;
66313214adfSAndy Whitcroft	my ($statement, $condition, $level);
66413214adfSAndy Whitcroft
66513214adfSAndy Whitcroft	my (@chunks);
66613214adfSAndy Whitcroft
667cf655043SAndy Whitcroft	# Grab the first conditional/block pair.
66813214adfSAndy Whitcroft	($statement, $condition, $linenr, $remain, $off, $level) =
66913214adfSAndy Whitcroft				ctx_statement_block($linenr, $remain, $off);
670773647a0SAndy Whitcroft	#print "F: c<$condition> s<$statement> remain<$remain>\n";
67113214adfSAndy Whitcroft	push(@chunks, [ $condition, $statement ]);
672cf655043SAndy Whitcroft	if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
673cf655043SAndy Whitcroft		return ($level, $linenr, @chunks);
674cf655043SAndy Whitcroft	}
675cf655043SAndy Whitcroft
676cf655043SAndy Whitcroft	# Pull in the following conditional/block pairs and see if they
677cf655043SAndy Whitcroft	# could continue the statement.
678cf655043SAndy Whitcroft	for (;;) {
67913214adfSAndy Whitcroft		($statement, $condition, $linenr, $remain, $off, $level) =
68013214adfSAndy Whitcroft				ctx_statement_block($linenr, $remain, $off);
681cf655043SAndy Whitcroft		#print "C: c<$condition> s<$statement> remain<$remain>\n";
682773647a0SAndy Whitcroft		last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
683cf655043SAndy Whitcroft		#print "C: push\n";
684cf655043SAndy Whitcroft		push(@chunks, [ $condition, $statement ]);
68513214adfSAndy Whitcroft	}
68613214adfSAndy Whitcroft
68713214adfSAndy Whitcroft	return ($level, $linenr, @chunks);
6888905a67cSAndy Whitcroft}
6898905a67cSAndy Whitcroft
6904a0df2efSAndy Whitcroftsub ctx_block_get {
691f0a594c1SAndy Whitcroft	my ($linenr, $remain, $outer, $open, $close, $off) = @_;
6924a0df2efSAndy Whitcroft	my $line;
6934a0df2efSAndy Whitcroft	my $start = $linenr - 1;
6944a0df2efSAndy Whitcroft	my $blk = '';
6954a0df2efSAndy Whitcroft	my @o;
6964a0df2efSAndy Whitcroft	my @c;
6974a0df2efSAndy Whitcroft	my @res = ();
6984a0df2efSAndy Whitcroft
699f0a594c1SAndy Whitcroft	my $level = 0;
7004635f4fbSAndy Whitcroft	my @stack = ($level);
70100df344fSAndy Whitcroft	for ($line = $start; $remain > 0; $line++) {
70200df344fSAndy Whitcroft		next if ($rawlines[$line] =~ /^-/);
70300df344fSAndy Whitcroft		$remain--;
70400df344fSAndy Whitcroft
70500df344fSAndy Whitcroft		$blk .= $rawlines[$line];
7064635f4fbSAndy Whitcroft
7074635f4fbSAndy Whitcroft		# Handle nested #if/#else.
70801464f30SAndy Whitcroft		if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
7094635f4fbSAndy Whitcroft			push(@stack, $level);
71001464f30SAndy Whitcroft		} elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
7114635f4fbSAndy Whitcroft			$level = $stack[$#stack - 1];
71201464f30SAndy Whitcroft		} elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
7134635f4fbSAndy Whitcroft			$level = pop(@stack);
7144635f4fbSAndy Whitcroft		}
7154635f4fbSAndy Whitcroft
71601464f30SAndy Whitcroft		foreach my $c (split(//, $lines[$line])) {
717f0a594c1SAndy Whitcroft			##print "C<$c>L<$level><$open$close>O<$off>\n";
718f0a594c1SAndy Whitcroft			if ($off > 0) {
719f0a594c1SAndy Whitcroft				$off--;
720f0a594c1SAndy Whitcroft				next;
721f0a594c1SAndy Whitcroft			}
7224a0df2efSAndy Whitcroft
723f0a594c1SAndy Whitcroft			if ($c eq $close && $level > 0) {
724f0a594c1SAndy Whitcroft				$level--;
725f0a594c1SAndy Whitcroft				last if ($level == 0);
726f0a594c1SAndy Whitcroft			} elsif ($c eq $open) {
727f0a594c1SAndy Whitcroft				$level++;
728f0a594c1SAndy Whitcroft			}
729f0a594c1SAndy Whitcroft		}
7304a0df2efSAndy Whitcroft
731f0a594c1SAndy Whitcroft		if (!$outer || $level <= 1) {
73200df344fSAndy Whitcroft			push(@res, $rawlines[$line]);
7334a0df2efSAndy Whitcroft		}
7344a0df2efSAndy Whitcroft
735f0a594c1SAndy Whitcroft		last if ($level == 0);
7364a0df2efSAndy Whitcroft	}
7374a0df2efSAndy Whitcroft
738f0a594c1SAndy Whitcroft	return ($level, @res);
7394a0df2efSAndy Whitcroft}
7404a0df2efSAndy Whitcroftsub ctx_block_outer {
7414a0df2efSAndy Whitcroft	my ($linenr, $remain) = @_;
7424a0df2efSAndy Whitcroft
743f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
744f0a594c1SAndy Whitcroft	return @r;
7454a0df2efSAndy Whitcroft}
7464a0df2efSAndy Whitcroftsub ctx_block {
7474a0df2efSAndy Whitcroft	my ($linenr, $remain) = @_;
7484a0df2efSAndy Whitcroft
749f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
750f0a594c1SAndy Whitcroft	return @r;
751653d4876SAndy Whitcroft}
752653d4876SAndy Whitcroftsub ctx_statement {
753f0a594c1SAndy Whitcroft	my ($linenr, $remain, $off) = @_;
754f0a594c1SAndy Whitcroft
755f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
756f0a594c1SAndy Whitcroft	return @r;
757f0a594c1SAndy Whitcroft}
758f0a594c1SAndy Whitcroftsub ctx_block_level {
759653d4876SAndy Whitcroft	my ($linenr, $remain) = @_;
760653d4876SAndy Whitcroft
761f0a594c1SAndy Whitcroft	return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
7624a0df2efSAndy Whitcroft}
7639c0ca6f9SAndy Whitcroftsub ctx_statement_level {
7649c0ca6f9SAndy Whitcroft	my ($linenr, $remain, $off) = @_;
7659c0ca6f9SAndy Whitcroft
7669c0ca6f9SAndy Whitcroft	return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
7679c0ca6f9SAndy Whitcroft}
7684a0df2efSAndy Whitcroft
7694a0df2efSAndy Whitcroftsub ctx_locate_comment {
7704a0df2efSAndy Whitcroft	my ($first_line, $end_line) = @_;
7714a0df2efSAndy Whitcroft
7724a0df2efSAndy Whitcroft	# Catch a comment on the end of the line itself.
773beae6332SAndy Whitcroft	my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
7744a0df2efSAndy Whitcroft	return $current_comment if (defined $current_comment);
7754a0df2efSAndy Whitcroft
7764a0df2efSAndy Whitcroft	# Look through the context and try and figure out if there is a
7774a0df2efSAndy Whitcroft	# comment.
7784a0df2efSAndy Whitcroft	my $in_comment = 0;
7794a0df2efSAndy Whitcroft	$current_comment = '';
7804a0df2efSAndy Whitcroft	for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
78100df344fSAndy Whitcroft		my $line = $rawlines[$linenr - 1];
78200df344fSAndy Whitcroft		#warn "           $line\n";
7834a0df2efSAndy Whitcroft		if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
7844a0df2efSAndy Whitcroft			$in_comment = 1;
7854a0df2efSAndy Whitcroft		}
7864a0df2efSAndy Whitcroft		if ($line =~ m@/\*@) {
7874a0df2efSAndy Whitcroft			$in_comment = 1;
7884a0df2efSAndy Whitcroft		}
7894a0df2efSAndy Whitcroft		if (!$in_comment && $current_comment ne '') {
7904a0df2efSAndy Whitcroft			$current_comment = '';
7914a0df2efSAndy Whitcroft		}
7924a0df2efSAndy Whitcroft		$current_comment .= $line . "\n" if ($in_comment);
7934a0df2efSAndy Whitcroft		if ($line =~ m@\*/@) {
7944a0df2efSAndy Whitcroft			$in_comment = 0;
7954a0df2efSAndy Whitcroft		}
7964a0df2efSAndy Whitcroft	}
7974a0df2efSAndy Whitcroft
7984a0df2efSAndy Whitcroft	chomp($current_comment);
7994a0df2efSAndy Whitcroft	return($current_comment);
8004a0df2efSAndy Whitcroft}
8014a0df2efSAndy Whitcroftsub ctx_has_comment {
8024a0df2efSAndy Whitcroft	my ($first_line, $end_line) = @_;
8034a0df2efSAndy Whitcroft	my $cmt = ctx_locate_comment($first_line, $end_line);
8044a0df2efSAndy Whitcroft
80500df344fSAndy Whitcroft	##print "LINE: $rawlines[$end_line - 1 ]\n";
8064a0df2efSAndy Whitcroft	##print "CMMT: $cmt\n";
8074a0df2efSAndy Whitcroft
8084a0df2efSAndy Whitcroft	return ($cmt ne '');
8094a0df2efSAndy Whitcroft}
8104a0df2efSAndy Whitcroft
8114d001e4dSAndy Whitcroftsub raw_line {
8124d001e4dSAndy Whitcroft	my ($linenr, $cnt) = @_;
8134d001e4dSAndy Whitcroft
8144d001e4dSAndy Whitcroft	my $offset = $linenr - 1;
8154d001e4dSAndy Whitcroft	$cnt++;
8164d001e4dSAndy Whitcroft
8174d001e4dSAndy Whitcroft	my $line;
8184d001e4dSAndy Whitcroft	while ($cnt) {
8194d001e4dSAndy Whitcroft		$line = $rawlines[$offset++];
8204d001e4dSAndy Whitcroft		next if (defined($line) && $line =~ /^-/);
8214d001e4dSAndy Whitcroft		$cnt--;
8224d001e4dSAndy Whitcroft	}
8234d001e4dSAndy Whitcroft
8244d001e4dSAndy Whitcroft	return $line;
8254d001e4dSAndy Whitcroft}
8264d001e4dSAndy Whitcroft
8270a920b5bSAndy Whitcroftsub cat_vet {
8280a920b5bSAndy Whitcroft	my ($vet) = @_;
8299c0ca6f9SAndy Whitcroft	my ($res, $coded);
8300a920b5bSAndy Whitcroft
8319c0ca6f9SAndy Whitcroft	$res = '';
8326c72ffaaSAndy Whitcroft	while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
8336c72ffaaSAndy Whitcroft		$res .= $1;
8346c72ffaaSAndy Whitcroft		if ($2 ne '') {
8359c0ca6f9SAndy Whitcroft			$coded = sprintf("^%c", unpack('C', $2) + 64);
8366c72ffaaSAndy Whitcroft			$res .= $coded;
8376c72ffaaSAndy Whitcroft		}
8389c0ca6f9SAndy Whitcroft	}
8399c0ca6f9SAndy Whitcroft	$res =~ s/$/\$/;
8400a920b5bSAndy Whitcroft
8419c0ca6f9SAndy Whitcroft	return $res;
8420a920b5bSAndy Whitcroft}
8430a920b5bSAndy Whitcroft
844c2fdda0dSAndy Whitcroftmy $av_preprocessor = 0;
845cf655043SAndy Whitcroftmy $av_pending;
846c2fdda0dSAndy Whitcroftmy @av_paren_type;
8471f65f947SAndy Whitcroftmy $av_pend_colon;
848c2fdda0dSAndy Whitcroft
849c2fdda0dSAndy Whitcroftsub annotate_reset {
850c2fdda0dSAndy Whitcroft	$av_preprocessor = 0;
851cf655043SAndy Whitcroft	$av_pending = '_';
852cf655043SAndy Whitcroft	@av_paren_type = ('E');
8531f65f947SAndy Whitcroft	$av_pend_colon = 'O';
854c2fdda0dSAndy Whitcroft}
855c2fdda0dSAndy Whitcroft
8566c72ffaaSAndy Whitcroftsub annotate_values {
8576c72ffaaSAndy Whitcroft	my ($stream, $type) = @_;
8586c72ffaaSAndy Whitcroft
8596c72ffaaSAndy Whitcroft	my $res;
8601f65f947SAndy Whitcroft	my $var = '_' x length($stream);
8616c72ffaaSAndy Whitcroft	my $cur = $stream;
8626c72ffaaSAndy Whitcroft
863c2fdda0dSAndy Whitcroft	print "$stream\n" if ($dbg_values > 1);
8646c72ffaaSAndy Whitcroft
8656c72ffaaSAndy Whitcroft	while (length($cur)) {
866773647a0SAndy Whitcroft		@av_paren_type = ('E') if ($#av_paren_type < 0);
867cf655043SAndy Whitcroft		print " <" . join('', @av_paren_type) .
868171ae1a4SAndy Whitcroft				"> <$type> <$av_pending>" if ($dbg_values > 1);
8696c72ffaaSAndy Whitcroft		if ($cur =~ /^(\s+)/o) {
870c2fdda0dSAndy Whitcroft			print "WS($1)\n" if ($dbg_values > 1);
871c2fdda0dSAndy Whitcroft			if ($1 =~ /\n/ && $av_preprocessor) {
872cf655043SAndy Whitcroft				$type = pop(@av_paren_type);
873c2fdda0dSAndy Whitcroft				$av_preprocessor = 0;
8746c72ffaaSAndy Whitcroft			}
8756c72ffaaSAndy Whitcroft
876c023e473SFlorian Mickler		} elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
8779446ef56SAndy Whitcroft			print "CAST($1)\n" if ($dbg_values > 1);
8789446ef56SAndy Whitcroft			push(@av_paren_type, $type);
8799446ef56SAndy Whitcroft			$type = 'C';
8809446ef56SAndy Whitcroft
881e91b6e26SAndy Whitcroft		} elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
882c2fdda0dSAndy Whitcroft			print "DECLARE($1)\n" if ($dbg_values > 1);
8836c72ffaaSAndy Whitcroft			$type = 'T';
8846c72ffaaSAndy Whitcroft
885389a2fe5SAndy Whitcroft		} elsif ($cur =~ /^($Modifier)\s*/) {
886389a2fe5SAndy Whitcroft			print "MODIFIER($1)\n" if ($dbg_values > 1);
887389a2fe5SAndy Whitcroft			$type = 'T';
888389a2fe5SAndy Whitcroft
889c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
890171ae1a4SAndy Whitcroft			print "DEFINE($1,$2)\n" if ($dbg_values > 1);
891c2fdda0dSAndy Whitcroft			$av_preprocessor = 1;
892171ae1a4SAndy Whitcroft			push(@av_paren_type, $type);
893171ae1a4SAndy Whitcroft			if ($2 ne '') {
894cf655043SAndy Whitcroft				$av_pending = 'N';
895171ae1a4SAndy Whitcroft			}
896171ae1a4SAndy Whitcroft			$type = 'E';
897171ae1a4SAndy Whitcroft
898c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
899171ae1a4SAndy Whitcroft			print "UNDEF($1)\n" if ($dbg_values > 1);
900171ae1a4SAndy Whitcroft			$av_preprocessor = 1;
901171ae1a4SAndy Whitcroft			push(@av_paren_type, $type);
9026c72ffaaSAndy Whitcroft
903c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
904cf655043SAndy Whitcroft			print "PRE_START($1)\n" if ($dbg_values > 1);
905c2fdda0dSAndy Whitcroft			$av_preprocessor = 1;
906cf655043SAndy Whitcroft
907cf655043SAndy Whitcroft			push(@av_paren_type, $type);
908cf655043SAndy Whitcroft			push(@av_paren_type, $type);
909171ae1a4SAndy Whitcroft			$type = 'E';
910cf655043SAndy Whitcroft
911c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
912cf655043SAndy Whitcroft			print "PRE_RESTART($1)\n" if ($dbg_values > 1);
913cf655043SAndy Whitcroft			$av_preprocessor = 1;
914cf655043SAndy Whitcroft
915cf655043SAndy Whitcroft			push(@av_paren_type, $av_paren_type[$#av_paren_type]);
916cf655043SAndy Whitcroft
917171ae1a4SAndy Whitcroft			$type = 'E';
918cf655043SAndy Whitcroft
919c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:endif))/o) {
920cf655043SAndy Whitcroft			print "PRE_END($1)\n" if ($dbg_values > 1);
921cf655043SAndy Whitcroft
922cf655043SAndy Whitcroft			$av_preprocessor = 1;
923cf655043SAndy Whitcroft
924cf655043SAndy Whitcroft			# Assume all arms of the conditional end as this
925cf655043SAndy Whitcroft			# one does, and continue as if the #endif was not here.
926cf655043SAndy Whitcroft			pop(@av_paren_type);
927cf655043SAndy Whitcroft			push(@av_paren_type, $type);
928171ae1a4SAndy Whitcroft			$type = 'E';
9296c72ffaaSAndy Whitcroft
9306c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\\\n)/o) {
931c2fdda0dSAndy Whitcroft			print "PRECONT($1)\n" if ($dbg_values > 1);
9326c72ffaaSAndy Whitcroft
933171ae1a4SAndy Whitcroft		} elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
934171ae1a4SAndy Whitcroft			print "ATTR($1)\n" if ($dbg_values > 1);
935171ae1a4SAndy Whitcroft			$av_pending = $type;
936171ae1a4SAndy Whitcroft			$type = 'N';
937171ae1a4SAndy Whitcroft
9386c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
939c2fdda0dSAndy Whitcroft			print "SIZEOF($1)\n" if ($dbg_values > 1);
9406c72ffaaSAndy Whitcroft			if (defined $2) {
941cf655043SAndy Whitcroft				$av_pending = 'V';
9426c72ffaaSAndy Whitcroft			}
9436c72ffaaSAndy Whitcroft			$type = 'N';
9446c72ffaaSAndy Whitcroft
94514b111c1SAndy Whitcroft		} elsif ($cur =~ /^(if|while|for)\b/o) {
946c2fdda0dSAndy Whitcroft			print "COND($1)\n" if ($dbg_values > 1);
94714b111c1SAndy Whitcroft			$av_pending = 'E';
9486c72ffaaSAndy Whitcroft			$type = 'N';
9496c72ffaaSAndy Whitcroft
9501f65f947SAndy Whitcroft		} elsif ($cur =~/^(case)/o) {
9511f65f947SAndy Whitcroft			print "CASE($1)\n" if ($dbg_values > 1);
9521f65f947SAndy Whitcroft			$av_pend_colon = 'C';
9531f65f947SAndy Whitcroft			$type = 'N';
9541f65f947SAndy Whitcroft
95514b111c1SAndy Whitcroft		} elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
956c2fdda0dSAndy Whitcroft			print "KEYWORD($1)\n" if ($dbg_values > 1);
9576c72ffaaSAndy Whitcroft			$type = 'N';
9586c72ffaaSAndy Whitcroft
9596c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\()/o) {
960c2fdda0dSAndy Whitcroft			print "PAREN('$1')\n" if ($dbg_values > 1);
961cf655043SAndy Whitcroft			push(@av_paren_type, $av_pending);
962cf655043SAndy Whitcroft			$av_pending = '_';
9636c72ffaaSAndy Whitcroft			$type = 'N';
9646c72ffaaSAndy Whitcroft
9656c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\))/o) {
966cf655043SAndy Whitcroft			my $new_type = pop(@av_paren_type);
967cf655043SAndy Whitcroft			if ($new_type ne '_') {
968cf655043SAndy Whitcroft				$type = $new_type;
969c2fdda0dSAndy Whitcroft				print "PAREN('$1') -> $type\n"
970c2fdda0dSAndy Whitcroft							if ($dbg_values > 1);
9716c72ffaaSAndy Whitcroft			} else {
972c2fdda0dSAndy Whitcroft				print "PAREN('$1')\n" if ($dbg_values > 1);
9736c72ffaaSAndy Whitcroft			}
9746c72ffaaSAndy Whitcroft
975c8cb2ca3SAndy Whitcroft		} elsif ($cur =~ /^($Ident)\s*\(/o) {
976c2fdda0dSAndy Whitcroft			print "FUNC($1)\n" if ($dbg_values > 1);
977c8cb2ca3SAndy Whitcroft			$type = 'V';
978cf655043SAndy Whitcroft			$av_pending = 'V';
9796c72ffaaSAndy Whitcroft
9808e761b04SAndy Whitcroft		} elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
9818e761b04SAndy Whitcroft			if (defined $2 && $type eq 'C' || $type eq 'T') {
9821f65f947SAndy Whitcroft				$av_pend_colon = 'B';
9838e761b04SAndy Whitcroft			} elsif ($type eq 'E') {
9848e761b04SAndy Whitcroft				$av_pend_colon = 'L';
9851f65f947SAndy Whitcroft			}
9861f65f947SAndy Whitcroft			print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
9871f65f947SAndy Whitcroft			$type = 'V';
9881f65f947SAndy Whitcroft
9896c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Ident|$Constant)/o) {
990c2fdda0dSAndy Whitcroft			print "IDENT($1)\n" if ($dbg_values > 1);
9916c72ffaaSAndy Whitcroft			$type = 'V';
9926c72ffaaSAndy Whitcroft
9936c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Assignment)/o) {
994c2fdda0dSAndy Whitcroft			print "ASSIGN($1)\n" if ($dbg_values > 1);
9956c72ffaaSAndy Whitcroft			$type = 'N';
9966c72ffaaSAndy Whitcroft
997cf655043SAndy Whitcroft		} elsif ($cur =~/^(;|{|})/) {
998c2fdda0dSAndy Whitcroft			print "END($1)\n" if ($dbg_values > 1);
99913214adfSAndy Whitcroft			$type = 'E';
10001f65f947SAndy Whitcroft			$av_pend_colon = 'O';
100113214adfSAndy Whitcroft
10028e761b04SAndy Whitcroft		} elsif ($cur =~/^(,)/) {
10038e761b04SAndy Whitcroft			print "COMMA($1)\n" if ($dbg_values > 1);
10048e761b04SAndy Whitcroft			$type = 'C';
10058e761b04SAndy Whitcroft
10061f65f947SAndy Whitcroft		} elsif ($cur =~ /^(\?)/o) {
10071f65f947SAndy Whitcroft			print "QUESTION($1)\n" if ($dbg_values > 1);
10081f65f947SAndy Whitcroft			$type = 'N';
10091f65f947SAndy Whitcroft
10101f65f947SAndy Whitcroft		} elsif ($cur =~ /^(:)/o) {
10111f65f947SAndy Whitcroft			print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
10121f65f947SAndy Whitcroft
10131f65f947SAndy Whitcroft			substr($var, length($res), 1, $av_pend_colon);
10141f65f947SAndy Whitcroft			if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
10151f65f947SAndy Whitcroft				$type = 'E';
10161f65f947SAndy Whitcroft			} else {
10171f65f947SAndy Whitcroft				$type = 'N';
10181f65f947SAndy Whitcroft			}
10191f65f947SAndy Whitcroft			$av_pend_colon = 'O';
10201f65f947SAndy Whitcroft
10218e761b04SAndy Whitcroft		} elsif ($cur =~ /^(\[)/o) {
102213214adfSAndy Whitcroft			print "CLOSE($1)\n" if ($dbg_values > 1);
10236c72ffaaSAndy Whitcroft			$type = 'N';
10246c72ffaaSAndy Whitcroft
10250d413866SAndy Whitcroft		} elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
102674048ed8SAndy Whitcroft			my $variant;
102774048ed8SAndy Whitcroft
102874048ed8SAndy Whitcroft			print "OPV($1)\n" if ($dbg_values > 1);
102974048ed8SAndy Whitcroft			if ($type eq 'V') {
103074048ed8SAndy Whitcroft				$variant = 'B';
103174048ed8SAndy Whitcroft			} else {
103274048ed8SAndy Whitcroft				$variant = 'U';
103374048ed8SAndy Whitcroft			}
103474048ed8SAndy Whitcroft
103574048ed8SAndy Whitcroft			substr($var, length($res), 1, $variant);
103674048ed8SAndy Whitcroft			$type = 'N';
103774048ed8SAndy Whitcroft
10386c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Operators)/o) {
1039c2fdda0dSAndy Whitcroft			print "OP($1)\n" if ($dbg_values > 1);
10406c72ffaaSAndy Whitcroft			if ($1 ne '++' && $1 ne '--') {
10416c72ffaaSAndy Whitcroft				$type = 'N';
10426c72ffaaSAndy Whitcroft			}
10436c72ffaaSAndy Whitcroft
10446c72ffaaSAndy Whitcroft		} elsif ($cur =~ /(^.)/o) {
1045c2fdda0dSAndy Whitcroft			print "C($1)\n" if ($dbg_values > 1);
10466c72ffaaSAndy Whitcroft		}
10476c72ffaaSAndy Whitcroft		if (defined $1) {
10486c72ffaaSAndy Whitcroft			$cur = substr($cur, length($1));
10496c72ffaaSAndy Whitcroft			$res .= $type x length($1);
10506c72ffaaSAndy Whitcroft		}
10516c72ffaaSAndy Whitcroft	}
10526c72ffaaSAndy Whitcroft
10531f65f947SAndy Whitcroft	return ($res, $var);
10546c72ffaaSAndy Whitcroft}
10556c72ffaaSAndy Whitcroft
10568905a67cSAndy Whitcroftsub possible {
105713214adfSAndy Whitcroft	my ($possible, $line) = @_;
10589a974fdbSAndy Whitcroft	my $notPermitted = qr{(?:
10590776e594SAndy Whitcroft		^(?:
10600776e594SAndy Whitcroft			$Modifier|
10610776e594SAndy Whitcroft			$Storage|
10620776e594SAndy Whitcroft			$Type|
10639a974fdbSAndy Whitcroft			DEFINE_\S+
10649a974fdbSAndy Whitcroft		)$|
10659a974fdbSAndy Whitcroft		^(?:
10660776e594SAndy Whitcroft			goto|
10670776e594SAndy Whitcroft			return|
10680776e594SAndy Whitcroft			case|
10690776e594SAndy Whitcroft			else|
10700776e594SAndy Whitcroft			asm|__asm__|
10710776e594SAndy Whitcroft			do
10729a974fdbSAndy Whitcroft		)(?:\s|$)|
10730776e594SAndy Whitcroft		^(?:typedef|struct|enum)\b
10749a974fdbSAndy Whitcroft	    )}x;
10759a974fdbSAndy Whitcroft	warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
10769a974fdbSAndy Whitcroft	if ($possible !~ $notPermitted) {
1077c45dcabdSAndy Whitcroft		# Check for modifiers.
1078c45dcabdSAndy Whitcroft		$possible =~ s/\s*$Storage\s*//g;
1079c45dcabdSAndy Whitcroft		$possible =~ s/\s*$Sparse\s*//g;
1080c45dcabdSAndy Whitcroft		if ($possible =~ /^\s*$/) {
1081c45dcabdSAndy Whitcroft
1082c45dcabdSAndy Whitcroft		} elsif ($possible =~ /\s/) {
1083c45dcabdSAndy Whitcroft			$possible =~ s/\s*$Type\s*//g;
1084d2506586SAndy Whitcroft			for my $modifier (split(' ', $possible)) {
10859a974fdbSAndy Whitcroft				if ($modifier !~ $notPermitted) {
1086d2506586SAndy Whitcroft					warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1087d2506586SAndy Whitcroft					push(@modifierList, $modifier);
1088d2506586SAndy Whitcroft				}
10899a974fdbSAndy Whitcroft			}
1090c45dcabdSAndy Whitcroft
1091c45dcabdSAndy Whitcroft		} else {
109213214adfSAndy Whitcroft			warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
10938905a67cSAndy Whitcroft			push(@typeList, $possible);
1094c45dcabdSAndy Whitcroft		}
10958905a67cSAndy Whitcroft		build_types();
10960776e594SAndy Whitcroft	} else {
10970776e594SAndy Whitcroft		warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
10988905a67cSAndy Whitcroft	}
10998905a67cSAndy Whitcroft}
11008905a67cSAndy Whitcroft
11016c72ffaaSAndy Whitcroftmy $prefix = '';
11026c72ffaaSAndy Whitcroft
1103f0a594c1SAndy Whitcroftsub report {
1104773647a0SAndy Whitcroft	if (defined $tst_only && $_[0] !~ /\Q$tst_only\E/) {
1105773647a0SAndy Whitcroft		return 0;
1106773647a0SAndy Whitcroft	}
11078905a67cSAndy Whitcroft	my $line = $prefix . $_[0];
11088905a67cSAndy Whitcroft
11098905a67cSAndy Whitcroft	$line = (split('\n', $line))[0] . "\n" if ($terse);
11108905a67cSAndy Whitcroft
111113214adfSAndy Whitcroft	push(our @report, $line);
1112773647a0SAndy Whitcroft
1113773647a0SAndy Whitcroft	return 1;
1114f0a594c1SAndy Whitcroft}
1115f0a594c1SAndy Whitcroftsub report_dump {
111613214adfSAndy Whitcroft	our @report;
1117f0a594c1SAndy Whitcroft}
1118de7d4f0eSAndy Whitcroftsub ERROR {
1119773647a0SAndy Whitcroft	if (report("ERROR: $_[0]\n")) {
1120de7d4f0eSAndy Whitcroft		our $clean = 0;
11216c72ffaaSAndy Whitcroft		our $cnt_error++;
1122de7d4f0eSAndy Whitcroft	}
1123773647a0SAndy Whitcroft}
1124de7d4f0eSAndy Whitcroftsub WARN {
1125773647a0SAndy Whitcroft	if (report("WARNING: $_[0]\n")) {
1126de7d4f0eSAndy Whitcroft		our $clean = 0;
11276c72ffaaSAndy Whitcroft		our $cnt_warn++;
1128de7d4f0eSAndy Whitcroft	}
1129773647a0SAndy Whitcroft}
1130de7d4f0eSAndy Whitcroftsub CHK {
1131773647a0SAndy Whitcroft	if ($check && report("CHECK: $_[0]\n")) {
1132de7d4f0eSAndy Whitcroft		our $clean = 0;
11336c72ffaaSAndy Whitcroft		our $cnt_chk++;
11346c72ffaaSAndy Whitcroft	}
1135de7d4f0eSAndy Whitcroft}
1136de7d4f0eSAndy Whitcroft
11376ecd9674SAndy Whitcroftsub check_absolute_file {
11386ecd9674SAndy Whitcroft	my ($absolute, $herecurr) = @_;
11396ecd9674SAndy Whitcroft	my $file = $absolute;
11406ecd9674SAndy Whitcroft
11416ecd9674SAndy Whitcroft	##print "absolute<$absolute>\n";
11426ecd9674SAndy Whitcroft
11436ecd9674SAndy Whitcroft	# See if any suffix of this path is a path within the tree.
11446ecd9674SAndy Whitcroft	while ($file =~ s@^[^/]*/@@) {
11456ecd9674SAndy Whitcroft		if (-f "$root/$file") {
11466ecd9674SAndy Whitcroft			##print "file<$file>\n";
11476ecd9674SAndy Whitcroft			last;
11486ecd9674SAndy Whitcroft		}
11496ecd9674SAndy Whitcroft	}
11506ecd9674SAndy Whitcroft	if (! -f _)  {
11516ecd9674SAndy Whitcroft		return 0;
11526ecd9674SAndy Whitcroft	}
11536ecd9674SAndy Whitcroft
11546ecd9674SAndy Whitcroft	# It is, so see if the prefix is acceptable.
11556ecd9674SAndy Whitcroft	my $prefix = $absolute;
11566ecd9674SAndy Whitcroft	substr($prefix, -length($file)) = '';
11576ecd9674SAndy Whitcroft
11586ecd9674SAndy Whitcroft	##print "prefix<$prefix>\n";
11596ecd9674SAndy Whitcroft	if ($prefix ne ".../") {
11606ecd9674SAndy Whitcroft		WARN("use relative pathname instead of absolute in changelog text\n" . $herecurr);
11616ecd9674SAndy Whitcroft	}
11626ecd9674SAndy Whitcroft}
11636ecd9674SAndy Whitcroft
11640a920b5bSAndy Whitcroftsub process {
11650a920b5bSAndy Whitcroft	my $filename = shift;
11660a920b5bSAndy Whitcroft
11670a920b5bSAndy Whitcroft	my $linenr=0;
11680a920b5bSAndy Whitcroft	my $prevline="";
1169c2fdda0dSAndy Whitcroft	my $prevrawline="";
11700a920b5bSAndy Whitcroft	my $stashline="";
1171c2fdda0dSAndy Whitcroft	my $stashrawline="";
11720a920b5bSAndy Whitcroft
11734a0df2efSAndy Whitcroft	my $length;
11740a920b5bSAndy Whitcroft	my $indent;
11750a920b5bSAndy Whitcroft	my $previndent=0;
11760a920b5bSAndy Whitcroft	my $stashindent=0;
11770a920b5bSAndy Whitcroft
1178de7d4f0eSAndy Whitcroft	our $clean = 1;
11790a920b5bSAndy Whitcroft	my $signoff = 0;
11800a920b5bSAndy Whitcroft	my $is_patch = 0;
11810a920b5bSAndy Whitcroft
118213214adfSAndy Whitcroft	our @report = ();
11836c72ffaaSAndy Whitcroft	our $cnt_lines = 0;
11846c72ffaaSAndy Whitcroft	our $cnt_error = 0;
11856c72ffaaSAndy Whitcroft	our $cnt_warn = 0;
11866c72ffaaSAndy Whitcroft	our $cnt_chk = 0;
11876c72ffaaSAndy Whitcroft
11880a920b5bSAndy Whitcroft	# Trace the real file/line as we go.
11890a920b5bSAndy Whitcroft	my $realfile = '';
11900a920b5bSAndy Whitcroft	my $realline = 0;
11910a920b5bSAndy Whitcroft	my $realcnt = 0;
11920a920b5bSAndy Whitcroft	my $here = '';
11930a920b5bSAndy Whitcroft	my $in_comment = 0;
1194c2fdda0dSAndy Whitcroft	my $comment_edge = 0;
11950a920b5bSAndy Whitcroft	my $first_line = 0;
11961e855726SWolfram Sang	my $p1_prefix = '';
11970a920b5bSAndy Whitcroft
119813214adfSAndy Whitcroft	my $prev_values = 'E';
119913214adfSAndy Whitcroft
120013214adfSAndy Whitcroft	# suppression flags
1201773647a0SAndy Whitcroft	my %suppress_ifbraces;
1202170d3a22SAndy Whitcroft	my %suppress_whiletrailers;
12032b474a1aSAndy Whitcroft	my %suppress_export;
1204653d4876SAndy Whitcroft
1205c2fdda0dSAndy Whitcroft	# Pre-scan the patch sanitizing the lines.
1206de7d4f0eSAndy Whitcroft	# Pre-scan the patch looking for any __setup documentation.
1207c2fdda0dSAndy Whitcroft	#
1208de7d4f0eSAndy Whitcroft	my @setup_docs = ();
1209de7d4f0eSAndy Whitcroft	my $setup_docs = 0;
1210773647a0SAndy Whitcroft
1211773647a0SAndy Whitcroft	sanitise_line_reset();
1212c2fdda0dSAndy Whitcroft	my $line;
1213c2fdda0dSAndy Whitcroft	foreach my $rawline (@rawlines) {
1214773647a0SAndy Whitcroft		$linenr++;
1215773647a0SAndy Whitcroft		$line = $rawline;
1216c2fdda0dSAndy Whitcroft
1217773647a0SAndy Whitcroft		if ($rawline=~/^\+\+\+\s+(\S+)/) {
1218de7d4f0eSAndy Whitcroft			$setup_docs = 0;
1219de7d4f0eSAndy Whitcroft			if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
1220de7d4f0eSAndy Whitcroft				$setup_docs = 1;
1221de7d4f0eSAndy Whitcroft			}
1222773647a0SAndy Whitcroft			#next;
1223de7d4f0eSAndy Whitcroft		}
1224773647a0SAndy Whitcroft		if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1225773647a0SAndy Whitcroft			$realline=$1-1;
1226773647a0SAndy Whitcroft			if (defined $2) {
1227773647a0SAndy Whitcroft				$realcnt=$3+1;
1228773647a0SAndy Whitcroft			} else {
1229773647a0SAndy Whitcroft				$realcnt=1+1;
1230773647a0SAndy Whitcroft			}
1231c45dcabdSAndy Whitcroft			$in_comment = 0;
1232773647a0SAndy Whitcroft
1233773647a0SAndy Whitcroft			# Guestimate if this is a continuing comment.  Run
1234773647a0SAndy Whitcroft			# the context looking for a comment "edge".  If this
1235773647a0SAndy Whitcroft			# edge is a close comment then we must be in a comment
1236773647a0SAndy Whitcroft			# at context start.
1237773647a0SAndy Whitcroft			my $edge;
123801fa9147SAndy Whitcroft			my $cnt = $realcnt;
123901fa9147SAndy Whitcroft			for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
124001fa9147SAndy Whitcroft				next if (defined $rawlines[$ln - 1] &&
124101fa9147SAndy Whitcroft					 $rawlines[$ln - 1] =~ /^-/);
124201fa9147SAndy Whitcroft				$cnt--;
124301fa9147SAndy Whitcroft				#print "RAW<$rawlines[$ln - 1]>\n";
1244721c1cb6SAndy Whitcroft				last if (!defined $rawlines[$ln - 1]);
1245fae17daeSAndy Whitcroft				if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
1246fae17daeSAndy Whitcroft				    $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
1247fae17daeSAndy Whitcroft					($edge) = $1;
1248fae17daeSAndy Whitcroft					last;
1249fae17daeSAndy Whitcroft				}
1250773647a0SAndy Whitcroft			}
1251773647a0SAndy Whitcroft			if (defined $edge && $edge eq '*/') {
1252773647a0SAndy Whitcroft				$in_comment = 1;
1253773647a0SAndy Whitcroft			}
1254773647a0SAndy Whitcroft
1255773647a0SAndy Whitcroft			# Guestimate if this is a continuing comment.  If this
1256773647a0SAndy Whitcroft			# is the start of a diff block and this line starts
1257773647a0SAndy Whitcroft			# ' *' then it is very likely a comment.
1258773647a0SAndy Whitcroft			if (!defined $edge &&
125983242e0cSAndy Whitcroft			    $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
1260773647a0SAndy Whitcroft			{
1261773647a0SAndy Whitcroft				$in_comment = 1;
1262773647a0SAndy Whitcroft			}
1263773647a0SAndy Whitcroft
1264773647a0SAndy Whitcroft			##print "COMMENT:$in_comment edge<$edge> $rawline\n";
1265773647a0SAndy Whitcroft			sanitise_line_reset($in_comment);
1266773647a0SAndy Whitcroft
1267171ae1a4SAndy Whitcroft		} elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
1268773647a0SAndy Whitcroft			# Standardise the strings and chars within the input to
1269171ae1a4SAndy Whitcroft			# simplify matching -- only bother with positive lines.
1270773647a0SAndy Whitcroft			$line = sanitise_line($rawline);
1271773647a0SAndy Whitcroft		}
1272773647a0SAndy Whitcroft		push(@lines, $line);
1273773647a0SAndy Whitcroft
1274773647a0SAndy Whitcroft		if ($realcnt > 1) {
1275773647a0SAndy Whitcroft			$realcnt-- if ($line =~ /^(?:\+| |$)/);
1276773647a0SAndy Whitcroft		} else {
1277773647a0SAndy Whitcroft			$realcnt = 0;
1278773647a0SAndy Whitcroft		}
1279773647a0SAndy Whitcroft
1280773647a0SAndy Whitcroft		#print "==>$rawline\n";
1281773647a0SAndy Whitcroft		#print "-->$line\n";
1282de7d4f0eSAndy Whitcroft
1283de7d4f0eSAndy Whitcroft		if ($setup_docs && $line =~ /^\+/) {
1284de7d4f0eSAndy Whitcroft			push(@setup_docs, $line);
1285de7d4f0eSAndy Whitcroft		}
1286de7d4f0eSAndy Whitcroft	}
1287de7d4f0eSAndy Whitcroft
12886c72ffaaSAndy Whitcroft	$prefix = '';
12896c72ffaaSAndy Whitcroft
1290773647a0SAndy Whitcroft	$realcnt = 0;
1291773647a0SAndy Whitcroft	$linenr = 0;
12920a920b5bSAndy Whitcroft	foreach my $line (@lines) {
12930a920b5bSAndy Whitcroft		$linenr++;
12940a920b5bSAndy Whitcroft
1295c2fdda0dSAndy Whitcroft		my $rawline = $rawlines[$linenr - 1];
12966c72ffaaSAndy Whitcroft
12970a920b5bSAndy Whitcroft#extract the line range in the file after the patch is applied
12986c72ffaaSAndy Whitcroft		if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
12990a920b5bSAndy Whitcroft			$is_patch = 1;
13004a0df2efSAndy Whitcroft			$first_line = $linenr + 1;
13010a920b5bSAndy Whitcroft			$realline=$1-1;
13020a920b5bSAndy Whitcroft			if (defined $2) {
13030a920b5bSAndy Whitcroft				$realcnt=$3+1;
13040a920b5bSAndy Whitcroft			} else {
13050a920b5bSAndy Whitcroft				$realcnt=1+1;
13060a920b5bSAndy Whitcroft			}
1307c2fdda0dSAndy Whitcroft			annotate_reset();
130813214adfSAndy Whitcroft			$prev_values = 'E';
130913214adfSAndy Whitcroft
1310773647a0SAndy Whitcroft			%suppress_ifbraces = ();
1311170d3a22SAndy Whitcroft			%suppress_whiletrailers = ();
13122b474a1aSAndy Whitcroft			%suppress_export = ();
13130a920b5bSAndy Whitcroft			next;
13140a920b5bSAndy Whitcroft
13154a0df2efSAndy Whitcroft# track the line number as we move through the hunk, note that
13164a0df2efSAndy Whitcroft# new versions of GNU diff omit the leading space on completely
13174a0df2efSAndy Whitcroft# blank context lines so we need to count that too.
1318773647a0SAndy Whitcroft		} elsif ($line =~ /^( |\+|$)/) {
13190a920b5bSAndy Whitcroft			$realline++;
1320d8aaf121SAndy Whitcroft			$realcnt-- if ($realcnt != 0);
13210a920b5bSAndy Whitcroft
13224a0df2efSAndy Whitcroft			# Measure the line length and indent.
1323c2fdda0dSAndy Whitcroft			($length, $indent) = line_stats($rawline);
13240a920b5bSAndy Whitcroft
13250a920b5bSAndy Whitcroft			# Track the previous line.
13260a920b5bSAndy Whitcroft			($prevline, $stashline) = ($stashline, $line);
13270a920b5bSAndy Whitcroft			($previndent, $stashindent) = ($stashindent, $indent);
1328c2fdda0dSAndy Whitcroft			($prevrawline, $stashrawline) = ($stashrawline, $rawline);
1329c2fdda0dSAndy Whitcroft
1330773647a0SAndy Whitcroft			#warn "line<$line>\n";
13316c72ffaaSAndy Whitcroft
1332d8aaf121SAndy Whitcroft		} elsif ($realcnt == 1) {
1333d8aaf121SAndy Whitcroft			$realcnt--;
13340a920b5bSAndy Whitcroft		}
13350a920b5bSAndy Whitcroft
1336cc77cdcaSAndy Whitcroft		my $hunk_line = ($realcnt != 0);
1337cc77cdcaSAndy Whitcroft
13380a920b5bSAndy Whitcroft#make up the handle for any error we report on this line
1339773647a0SAndy Whitcroft		$prefix = "$filename:$realline: " if ($emacs && $file);
1340773647a0SAndy Whitcroft		$prefix = "$filename:$linenr: " if ($emacs && !$file);
1341773647a0SAndy Whitcroft
13426c72ffaaSAndy Whitcroft		$here = "#$linenr: " if (!$file);
13436c72ffaaSAndy Whitcroft		$here = "#$realline: " if ($file);
1344773647a0SAndy Whitcroft
1345773647a0SAndy Whitcroft		# extract the filename as it passes
13463bf9a009SRabin Vincent		if ($line =~ /^diff --git.*?(\S+)$/) {
13473bf9a009SRabin Vincent			$realfile = $1;
13483bf9a009SRabin Vincent			$realfile =~ s@^([^/]*)/@@;
13493bf9a009SRabin Vincent
13503bf9a009SRabin Vincent		} elsif ($line =~ /^\+\+\+\s+(\S+)/) {
1351773647a0SAndy Whitcroft			$realfile = $1;
13521e855726SWolfram Sang			$realfile =~ s@^([^/]*)/@@;
13531e855726SWolfram Sang
13541e855726SWolfram Sang			$p1_prefix = $1;
1355e2f7aa4bSAndy Whitcroft			if (!$file && $tree && $p1_prefix ne '' &&
1356e2f7aa4bSAndy Whitcroft			    -e "$root/$p1_prefix") {
13571e855726SWolfram Sang				WARN("patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
13581e855726SWolfram Sang			}
1359773647a0SAndy Whitcroft
1360c1ab3326SAndy Whitcroft			if ($realfile =~ m@^include/asm/@) {
1361773647a0SAndy Whitcroft				ERROR("do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
1362773647a0SAndy Whitcroft			}
1363773647a0SAndy Whitcroft			next;
1364773647a0SAndy Whitcroft		}
1365773647a0SAndy Whitcroft
1366389834b6SRandy Dunlap		$here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
13670a920b5bSAndy Whitcroft
1368c2fdda0dSAndy Whitcroft		my $hereline = "$here\n$rawline\n";
1369c2fdda0dSAndy Whitcroft		my $herecurr = "$here\n$rawline\n";
1370c2fdda0dSAndy Whitcroft		my $hereprev = "$here\n$prevrawline\n$rawline\n";
13710a920b5bSAndy Whitcroft
13726c72ffaaSAndy Whitcroft		$cnt_lines++ if ($realcnt != 0);
13736c72ffaaSAndy Whitcroft
13743bf9a009SRabin Vincent# Check for incorrect file permissions
13753bf9a009SRabin Vincent		if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
13763bf9a009SRabin Vincent			my $permhere = $here . "FILE: $realfile\n";
13773bf9a009SRabin Vincent			if ($realfile =~ /(Makefile|Kconfig|\.c|\.h|\.S|\.tmpl)$/) {
13783bf9a009SRabin Vincent				ERROR("do not set execute permissions for source files\n" . $permhere);
13793bf9a009SRabin Vincent			}
13803bf9a009SRabin Vincent		}
13813bf9a009SRabin Vincent
13820a920b5bSAndy Whitcroft#check the patch for a signoff:
1383d8aaf121SAndy Whitcroft		if ($line =~ /^\s*signed-off-by:/i) {
13844a0df2efSAndy Whitcroft			# This is a signoff, if ugly, so do not double report.
13854a0df2efSAndy Whitcroft			$signoff++;
13860a920b5bSAndy Whitcroft			if (!($line =~ /^\s*Signed-off-by:/)) {
1387de7d4f0eSAndy Whitcroft				WARN("Signed-off-by: is the preferred form\n" .
1388de7d4f0eSAndy Whitcroft					$herecurr);
13890a920b5bSAndy Whitcroft			}
13900a920b5bSAndy Whitcroft			if ($line =~ /^\s*signed-off-by:\S/i) {
1391773647a0SAndy Whitcroft				WARN("space required after Signed-off-by:\n" .
1392de7d4f0eSAndy Whitcroft					$herecurr);
13930a920b5bSAndy Whitcroft			}
13940a920b5bSAndy Whitcroft		}
13950a920b5bSAndy Whitcroft
139600df344fSAndy Whitcroft# Check for wrappage within a valid hunk of the file
13978905a67cSAndy Whitcroft		if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
1398de7d4f0eSAndy Whitcroft			ERROR("patch seems to be corrupt (line wrapped?)\n" .
13996c72ffaaSAndy Whitcroft				$herecurr) if (!$emitted_corrupt++);
1400de7d4f0eSAndy Whitcroft		}
1401de7d4f0eSAndy Whitcroft
14026ecd9674SAndy Whitcroft# Check for absolute kernel paths.
14036ecd9674SAndy Whitcroft		if ($tree) {
14046ecd9674SAndy Whitcroft			while ($line =~ m{(?:^|\s)(/\S*)}g) {
14056ecd9674SAndy Whitcroft				my $file = $1;
14066ecd9674SAndy Whitcroft
14076ecd9674SAndy Whitcroft				if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
14086ecd9674SAndy Whitcroft				    check_absolute_file($1, $herecurr)) {
14096ecd9674SAndy Whitcroft					#
14106ecd9674SAndy Whitcroft				} else {
14116ecd9674SAndy Whitcroft					check_absolute_file($file, $herecurr);
14126ecd9674SAndy Whitcroft				}
14136ecd9674SAndy Whitcroft			}
14146ecd9674SAndy Whitcroft		}
14156ecd9674SAndy Whitcroft
1416de7d4f0eSAndy Whitcroft# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
1417de7d4f0eSAndy Whitcroft		if (($realfile =~ /^$/ || $line =~ /^\+/) &&
1418171ae1a4SAndy Whitcroft		    $rawline !~ m/^$UTF8*$/) {
1419171ae1a4SAndy Whitcroft			my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
1420171ae1a4SAndy Whitcroft
1421171ae1a4SAndy Whitcroft			my $blank = copy_spacing($rawline);
1422171ae1a4SAndy Whitcroft			my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
1423171ae1a4SAndy Whitcroft			my $hereptr = "$hereline$ptr\n";
1424171ae1a4SAndy Whitcroft
1425171ae1a4SAndy Whitcroft			ERROR("Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
142600df344fSAndy Whitcroft		}
14270a920b5bSAndy Whitcroft
142830670854SAndy Whitcroft# ignore non-hunk lines and lines being removed
142930670854SAndy Whitcroft		next if (!$hunk_line || $line =~ /^-/);
143000df344fSAndy Whitcroft
14310a920b5bSAndy Whitcroft#trailing whitespace
14329c0ca6f9SAndy Whitcroft		if ($line =~ /^\+.*\015/) {
1433c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
14349c0ca6f9SAndy Whitcroft			ERROR("DOS line endings\n" . $herevet);
14359c0ca6f9SAndy Whitcroft
1436c2fdda0dSAndy Whitcroft		} elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
1437c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1438de7d4f0eSAndy Whitcroft			ERROR("trailing whitespace\n" . $herevet);
1439d2c0a235SAndy Whitcroft			$rpt_cleaners = 1;
14400a920b5bSAndy Whitcroft		}
14415368df20SAndy Whitcroft
14423354957aSAndi Kleen# check for Kconfig help text having a real description
14439fe287d7SAndy Whitcroft# Only applies when adding the entry originally, after that we do not have
14449fe287d7SAndy Whitcroft# sufficient context to determine whether it is indeed long enough.
14453354957aSAndi Kleen		if ($realfile =~ /Kconfig/ &&
14469fe287d7SAndy Whitcroft		    $line =~ /\+\s*(?:---)?help(?:---)?$/) {
14473354957aSAndi Kleen			my $length = 0;
14489fe287d7SAndy Whitcroft			my $cnt = $realcnt;
14499fe287d7SAndy Whitcroft			my $ln = $linenr + 1;
14509fe287d7SAndy Whitcroft			my $f;
14519fe287d7SAndy Whitcroft			my $is_end = 0;
14529fe287d7SAndy Whitcroft			while ($cnt > 0 && defined $lines[$ln - 1]) {
14539fe287d7SAndy Whitcroft				$f = $lines[$ln - 1];
14549fe287d7SAndy Whitcroft				$cnt-- if ($lines[$ln - 1] !~ /^-/);
14559fe287d7SAndy Whitcroft				$is_end = $lines[$ln - 1] =~ /^\+/;
14569fe287d7SAndy Whitcroft				$ln++;
14579fe287d7SAndy Whitcroft
14589fe287d7SAndy Whitcroft				next if ($f =~ /^-/);
14599fe287d7SAndy Whitcroft				$f =~ s/^.//;
14603354957aSAndi Kleen				$f =~ s/#.*//;
14613354957aSAndi Kleen				$f =~ s/^\s+//;
14623354957aSAndi Kleen				next if ($f =~ /^$/);
14639fe287d7SAndy Whitcroft				if ($f =~ /^\s*config\s/) {
14649fe287d7SAndy Whitcroft					$is_end = 1;
14659fe287d7SAndy Whitcroft					last;
14669fe287d7SAndy Whitcroft				}
14673354957aSAndi Kleen				$length++;
14683354957aSAndi Kleen			}
14699fe287d7SAndy Whitcroft			WARN("please write a paragraph that describes the config symbol fully\n" . $herecurr) if ($is_end && $length < 4);
14709fe287d7SAndy Whitcroft			#print "is_end<$is_end> length<$length>\n";
14713354957aSAndi Kleen		}
14723354957aSAndi Kleen
14735368df20SAndy Whitcroft# check we are in a valid source file if not then ignore this hunk
14745368df20SAndy Whitcroft		next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
14755368df20SAndy Whitcroft
14760a920b5bSAndy Whitcroft#80 column limit
1477c45dcabdSAndy Whitcroft		if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ &&
1478f4c014c0SAndy Whitcroft		    $rawline !~ /^.\s*\*\s*\@$Ident\s/ &&
14790fccc622SJoe Perches		    !($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(KERN_\S+\s*|[^"]*))?"[X\t]*"\s*(?:|,|\)\s*;)\s*$/ ||
14808bbea968SJoe Perches		    $line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) &&
1481f4c014c0SAndy Whitcroft		    $length > 80)
1482c45dcabdSAndy Whitcroft		{
1483de7d4f0eSAndy Whitcroft			WARN("line over 80 characters\n" . $herecurr);
14840a920b5bSAndy Whitcroft		}
14850a920b5bSAndy Whitcroft
14865e79d96eSJoe Perches# check for spaces before a quoted newline
14875e79d96eSJoe Perches		if ($rawline =~ /^.*\".*\s\\n/) {
14885e79d96eSJoe Perches			WARN("unnecessary whitespace before a quoted newline\n" . $herecurr);
14895e79d96eSJoe Perches		}
14905e79d96eSJoe Perches
14918905a67cSAndy Whitcroft# check for adding lines without a newline.
14928905a67cSAndy Whitcroft		if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
14938905a67cSAndy Whitcroft			WARN("adding a line without newline at end of file\n" . $herecurr);
14948905a67cSAndy Whitcroft		}
14958905a67cSAndy Whitcroft
149642e41c54SMike Frysinger# Blackfin: use hi/lo macros
149742e41c54SMike Frysinger		if ($realfile =~ m@arch/blackfin/.*\.S$@) {
149842e41c54SMike Frysinger			if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
149942e41c54SMike Frysinger				my $herevet = "$here\n" . cat_vet($line) . "\n";
150042e41c54SMike Frysinger				ERROR("use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
150142e41c54SMike Frysinger			}
150242e41c54SMike Frysinger			if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
150342e41c54SMike Frysinger				my $herevet = "$here\n" . cat_vet($line) . "\n";
150442e41c54SMike Frysinger				ERROR("use the HI() macro, not (... >> 16)\n" . $herevet);
150542e41c54SMike Frysinger			}
150642e41c54SMike Frysinger		}
150742e41c54SMike Frysinger
1508b9ea10d6SAndy Whitcroft# check we are in a valid source file C or perl if not then ignore this hunk
1509b9ea10d6SAndy Whitcroft		next if ($realfile !~ /\.(h|c|pl)$/);
15100a920b5bSAndy Whitcroft
15110a920b5bSAndy Whitcroft# at the beginning of a line any tabs must come first and anything
15120a920b5bSAndy Whitcroft# more than 8 must use tabs.
1513c2fdda0dSAndy Whitcroft		if ($rawline =~ /^\+\s* \t\s*\S/ ||
1514c2fdda0dSAndy Whitcroft		    $rawline =~ /^\+\s*        \s*/) {
1515c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1516171ae1a4SAndy Whitcroft			ERROR("code indent should use tabs where possible\n" . $herevet);
1517d2c0a235SAndy Whitcroft			$rpt_cleaners = 1;
15180a920b5bSAndy Whitcroft		}
15190a920b5bSAndy Whitcroft
152008e44365SAlberto Panizzo# check for space before tabs.
152108e44365SAlberto Panizzo		if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
152208e44365SAlberto Panizzo			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
152308e44365SAlberto Panizzo			WARN("please, no space before tabs\n" . $herevet);
152408e44365SAlberto Panizzo		}
152508e44365SAlberto Panizzo
15265f7ddae6SRaffaele Recalcati# check for spaces at the beginning of a line.
15276b4c5bebSAndy Whitcroft# Exceptions:
15286b4c5bebSAndy Whitcroft#  1) within comments
15296b4c5bebSAndy Whitcroft#  2) indented preprocessor commands
15306b4c5bebSAndy Whitcroft#  3) hanging labels
15316b4c5bebSAndy Whitcroft		if ($rawline =~ /^\+ / && $line !~ /\+ *(?:$;|#|$Ident:)/)  {
15325f7ddae6SRaffaele Recalcati			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
15336b4c5bebSAndy Whitcroft			WARN("please, no spaces at the start of a line\n" . $herevet);
15345f7ddae6SRaffaele Recalcati		}
15355f7ddae6SRaffaele Recalcati
1536b9ea10d6SAndy Whitcroft# check we are in a valid C source file if not then ignore this hunk
1537b9ea10d6SAndy Whitcroft		next if ($realfile !~ /\.(h|c)$/);
1538b9ea10d6SAndy Whitcroft
1539c2fdda0dSAndy Whitcroft# check for RCS/CVS revision markers
1540cf655043SAndy Whitcroft		if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
1541c2fdda0dSAndy Whitcroft			WARN("CVS style keyword markers, these will _not_ be updated\n". $herecurr);
1542c2fdda0dSAndy Whitcroft		}
154322f2a2efSAndy Whitcroft
154442e41c54SMike Frysinger# Blackfin: don't use __builtin_bfin_[cs]sync
154542e41c54SMike Frysinger		if ($line =~ /__builtin_bfin_csync/) {
154642e41c54SMike Frysinger			my $herevet = "$here\n" . cat_vet($line) . "\n";
154742e41c54SMike Frysinger			ERROR("use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
154842e41c54SMike Frysinger		}
154942e41c54SMike Frysinger		if ($line =~ /__builtin_bfin_ssync/) {
155042e41c54SMike Frysinger			my $herevet = "$here\n" . cat_vet($line) . "\n";
155142e41c54SMike Frysinger			ERROR("use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
155242e41c54SMike Frysinger		}
155342e41c54SMike Frysinger
15549c0ca6f9SAndy Whitcroft# Check for potential 'bare' types
15552b474a1aSAndy Whitcroft		my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
15562b474a1aSAndy Whitcroft		    $realline_next);
15579c9ba34eSAndy Whitcroft		if ($realcnt && $line =~ /.\s*\S/) {
1558170d3a22SAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
1559f5fe35ddSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0);
1560171ae1a4SAndy Whitcroft			$stat =~ s/\n./\n /g;
1561171ae1a4SAndy Whitcroft			$cond =~ s/\n./\n /g;
1562171ae1a4SAndy Whitcroft
15632b474a1aSAndy Whitcroft			# Find the real next line.
15642b474a1aSAndy Whitcroft			$realline_next = $line_nr_next;
15652b474a1aSAndy Whitcroft			if (defined $realline_next &&
15662b474a1aSAndy Whitcroft			    (!defined $lines[$realline_next - 1] ||
15672b474a1aSAndy Whitcroft			     substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
15682b474a1aSAndy Whitcroft				$realline_next++;
15692b474a1aSAndy Whitcroft			}
15702b474a1aSAndy Whitcroft
1571171ae1a4SAndy Whitcroft			my $s = $stat;
1572171ae1a4SAndy Whitcroft			$s =~ s/{.*$//s;
1573cf655043SAndy Whitcroft
1574c2fdda0dSAndy Whitcroft			# Ignore goto labels.
1575171ae1a4SAndy Whitcroft			if ($s =~ /$Ident:\*$/s) {
1576c2fdda0dSAndy Whitcroft
1577c2fdda0dSAndy Whitcroft			# Ignore functions being called
1578171ae1a4SAndy Whitcroft			} elsif ($s =~ /^.\s*$Ident\s*\(/s) {
1579c2fdda0dSAndy Whitcroft
1580463f2864SAndy Whitcroft			} elsif ($s =~ /^.\s*else\b/s) {
1581463f2864SAndy Whitcroft
1582c45dcabdSAndy Whitcroft			# declarations always start with types
1583d2506586SAndy 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) {
1584c45dcabdSAndy Whitcroft				my $type = $1;
1585c45dcabdSAndy Whitcroft				$type =~ s/\s+/ /g;
1586c45dcabdSAndy Whitcroft				possible($type, "A:" . $s);
1587c45dcabdSAndy Whitcroft
15886c72ffaaSAndy Whitcroft			# definitions in global scope can only start with types
1589a6a84062SAndy Whitcroft			} elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
1590c45dcabdSAndy Whitcroft				possible($1, "B:" . $s);
1591c2fdda0dSAndy Whitcroft			}
15928905a67cSAndy Whitcroft
15936c72ffaaSAndy Whitcroft			# any (foo ... *) is a pointer cast, and foo is a type
159465863862SAndy Whitcroft			while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
1595c45dcabdSAndy Whitcroft				possible($1, "C:" . $s);
15969c0ca6f9SAndy Whitcroft			}
15978905a67cSAndy Whitcroft
15988905a67cSAndy Whitcroft			# Check for any sort of function declaration.
15998905a67cSAndy Whitcroft			# int foo(something bar, other baz);
16008905a67cSAndy Whitcroft			# void (*store_gdt)(x86_descr_ptr *);
1601171ae1a4SAndy Whitcroft			if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
16028905a67cSAndy Whitcroft				my ($name_len) = length($1);
16038905a67cSAndy Whitcroft
1604cf655043SAndy Whitcroft				my $ctx = $s;
1605773647a0SAndy Whitcroft				substr($ctx, 0, $name_len + 1, '');
16068905a67cSAndy Whitcroft				$ctx =~ s/\)[^\)]*$//;
1607cf655043SAndy Whitcroft
16088905a67cSAndy Whitcroft				for my $arg (split(/\s*,\s*/, $ctx)) {
1609c45dcabdSAndy Whitcroft					if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
16108905a67cSAndy Whitcroft
1611c45dcabdSAndy Whitcroft						possible($1, "D:" . $s);
16128905a67cSAndy Whitcroft					}
16138905a67cSAndy Whitcroft				}
16148905a67cSAndy Whitcroft			}
16158905a67cSAndy Whitcroft
16169c0ca6f9SAndy Whitcroft		}
16179c0ca6f9SAndy Whitcroft
161800df344fSAndy Whitcroft#
161900df344fSAndy Whitcroft# Checks which may be anchored in the context.
162000df344fSAndy Whitcroft#
162100df344fSAndy Whitcroft
162200df344fSAndy Whitcroft# Check for switch () and associated case and default
162300df344fSAndy Whitcroft# statements should be at the same indent.
162400df344fSAndy Whitcroft		if ($line=~/\bswitch\s*\(.*\)/) {
162500df344fSAndy Whitcroft			my $err = '';
162600df344fSAndy Whitcroft			my $sep = '';
162700df344fSAndy Whitcroft			my @ctx = ctx_block_outer($linenr, $realcnt);
162800df344fSAndy Whitcroft			shift(@ctx);
162900df344fSAndy Whitcroft			for my $ctx (@ctx) {
163000df344fSAndy Whitcroft				my ($clen, $cindent) = line_stats($ctx);
163100df344fSAndy Whitcroft				if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
163200df344fSAndy Whitcroft							$indent != $cindent) {
163300df344fSAndy Whitcroft					$err .= "$sep$ctx\n";
163400df344fSAndy Whitcroft					$sep = '';
163500df344fSAndy Whitcroft				} else {
163600df344fSAndy Whitcroft					$sep = "[...]\n";
163700df344fSAndy Whitcroft				}
163800df344fSAndy Whitcroft			}
163900df344fSAndy Whitcroft			if ($err ne '') {
16409c0ca6f9SAndy Whitcroft				ERROR("switch and case should be at the same indent\n$hereline$err");
1641de7d4f0eSAndy Whitcroft			}
1642de7d4f0eSAndy Whitcroft		}
1643de7d4f0eSAndy Whitcroft
1644de7d4f0eSAndy Whitcroft# if/while/etc brace do not go on next line, unless defining a do while loop,
1645de7d4f0eSAndy Whitcroft# or if that brace on the next line is for something else
1646c45dcabdSAndy Whitcroft		if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
1647773647a0SAndy Whitcroft			my $pre_ctx = "$1$2";
1648773647a0SAndy Whitcroft
16499c0ca6f9SAndy Whitcroft			my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
1650de7d4f0eSAndy Whitcroft			my $ctx_cnt = $realcnt - $#ctx - 1;
1651de7d4f0eSAndy Whitcroft			my $ctx = join("\n", @ctx);
1652de7d4f0eSAndy Whitcroft
1653548596d5SAndy Whitcroft			my $ctx_ln = $linenr;
1654548596d5SAndy Whitcroft			my $ctx_skip = $realcnt;
1655de7d4f0eSAndy Whitcroft
1656548596d5SAndy Whitcroft			while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
1657548596d5SAndy Whitcroft					defined $lines[$ctx_ln - 1] &&
1658548596d5SAndy Whitcroft					$lines[$ctx_ln - 1] =~ /^-/)) {
1659548596d5SAndy Whitcroft				##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
1660548596d5SAndy Whitcroft				$ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
1661773647a0SAndy Whitcroft				$ctx_ln++;
1662773647a0SAndy Whitcroft			}
1663548596d5SAndy Whitcroft
166453210168SAndy Whitcroft			#print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
166553210168SAndy Whitcroft			#print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
1666773647a0SAndy Whitcroft
1667773647a0SAndy Whitcroft			if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln -1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
1668773647a0SAndy Whitcroft				ERROR("that open brace { should be on the previous line\n" .
166901464f30SAndy Whitcroft					"$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
167000df344fSAndy Whitcroft			}
1671773647a0SAndy Whitcroft			if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
1672773647a0SAndy Whitcroft			    $ctx =~ /\)\s*\;\s*$/ &&
1673773647a0SAndy Whitcroft			    defined $lines[$ctx_ln - 1])
1674773647a0SAndy Whitcroft			{
16759c0ca6f9SAndy Whitcroft				my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
16769c0ca6f9SAndy Whitcroft				if ($nindent > $indent) {
1677773647a0SAndy Whitcroft					WARN("trailing semicolon indicates no statements, indent implies otherwise\n" .
167801464f30SAndy Whitcroft						"$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
16799c0ca6f9SAndy Whitcroft				}
16809c0ca6f9SAndy Whitcroft			}
168100df344fSAndy Whitcroft		}
168200df344fSAndy Whitcroft
16834d001e4dSAndy Whitcroft# Check relative indent for conditionals and blocks.
16844d001e4dSAndy Whitcroft		if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
16854d001e4dSAndy Whitcroft			my ($s, $c) = ($stat, $cond);
16864d001e4dSAndy Whitcroft
16874d001e4dSAndy Whitcroft			substr($s, 0, length($c), '');
16884d001e4dSAndy Whitcroft
16894d001e4dSAndy Whitcroft			# Make sure we remove the line prefixes as we have
16904d001e4dSAndy Whitcroft			# none on the first line, and are going to readd them
16914d001e4dSAndy Whitcroft			# where necessary.
16924d001e4dSAndy Whitcroft			$s =~ s/\n./\n/gs;
16934d001e4dSAndy Whitcroft
16944d001e4dSAndy Whitcroft			# Find out how long the conditional actually is.
16956f779c18SAndy Whitcroft			my @newlines = ($c =~ /\n/gs);
16966f779c18SAndy Whitcroft			my $cond_lines = 1 + $#newlines;
16974d001e4dSAndy Whitcroft
16984d001e4dSAndy Whitcroft			# We want to check the first line inside the block
16994d001e4dSAndy Whitcroft			# starting at the end of the conditional, so remove:
17004d001e4dSAndy Whitcroft			#  1) any blank line termination
17014d001e4dSAndy Whitcroft			#  2) any opening brace { on end of the line
17024d001e4dSAndy Whitcroft			#  3) any do (...) {
17034d001e4dSAndy Whitcroft			my $continuation = 0;
17044d001e4dSAndy Whitcroft			my $check = 0;
17054d001e4dSAndy Whitcroft			$s =~ s/^.*\bdo\b//;
17064d001e4dSAndy Whitcroft			$s =~ s/^\s*{//;
17074d001e4dSAndy Whitcroft			if ($s =~ s/^\s*\\//) {
17084d001e4dSAndy Whitcroft				$continuation = 1;
17094d001e4dSAndy Whitcroft			}
17109bd49efeSAndy Whitcroft			if ($s =~ s/^\s*?\n//) {
17114d001e4dSAndy Whitcroft				$check = 1;
17124d001e4dSAndy Whitcroft				$cond_lines++;
17134d001e4dSAndy Whitcroft			}
17144d001e4dSAndy Whitcroft
17154d001e4dSAndy Whitcroft			# Also ignore a loop construct at the end of a
17164d001e4dSAndy Whitcroft			# preprocessor statement.
17174d001e4dSAndy Whitcroft			if (($prevline =~ /^.\s*#\s*define\s/ ||
17184d001e4dSAndy Whitcroft			    $prevline =~ /\\\s*$/) && $continuation == 0) {
17194d001e4dSAndy Whitcroft				$check = 0;
17204d001e4dSAndy Whitcroft			}
17214d001e4dSAndy Whitcroft
17229bd49efeSAndy Whitcroft			my $cond_ptr = -1;
1723740504c6SAndy Whitcroft			$continuation = 0;
17249bd49efeSAndy Whitcroft			while ($cond_ptr != $cond_lines) {
17259bd49efeSAndy Whitcroft				$cond_ptr = $cond_lines;
17264d001e4dSAndy Whitcroft
1727f16fa28fSAndy Whitcroft				# If we see an #else/#elif then the code
1728f16fa28fSAndy Whitcroft				# is not linear.
1729f16fa28fSAndy Whitcroft				if ($s =~ /^\s*\#\s*(?:else|elif)/) {
1730f16fa28fSAndy Whitcroft					$check = 0;
1731f16fa28fSAndy Whitcroft				}
1732f16fa28fSAndy Whitcroft
17339bd49efeSAndy Whitcroft				# Ignore:
17349bd49efeSAndy Whitcroft				#  1) blank lines, they should be at 0,
17359bd49efeSAndy Whitcroft				#  2) preprocessor lines, and
17369bd49efeSAndy Whitcroft				#  3) labels.
1737740504c6SAndy Whitcroft				if ($continuation ||
1738740504c6SAndy Whitcroft				    $s =~ /^\s*?\n/ ||
17399bd49efeSAndy Whitcroft				    $s =~ /^\s*#\s*?/ ||
17409bd49efeSAndy Whitcroft				    $s =~ /^\s*$Ident\s*:/) {
1741740504c6SAndy Whitcroft					$continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
174230dad6ebSAndy Whitcroft					if ($s =~ s/^.*?\n//) {
17439bd49efeSAndy Whitcroft						$cond_lines++;
17449bd49efeSAndy Whitcroft					}
17454d001e4dSAndy Whitcroft				}
174630dad6ebSAndy Whitcroft			}
17474d001e4dSAndy Whitcroft
17484d001e4dSAndy Whitcroft			my (undef, $sindent) = line_stats("+" . $s);
17494d001e4dSAndy Whitcroft			my $stat_real = raw_line($linenr, $cond_lines);
17504d001e4dSAndy Whitcroft
17514d001e4dSAndy Whitcroft			# Check if either of these lines are modified, else
17524d001e4dSAndy Whitcroft			# this is not this patch's fault.
17534d001e4dSAndy Whitcroft			if (!defined($stat_real) ||
17544d001e4dSAndy Whitcroft			    $stat !~ /^\+/ && $stat_real !~ /^\+/) {
17554d001e4dSAndy Whitcroft				$check = 0;
17564d001e4dSAndy Whitcroft			}
17574d001e4dSAndy Whitcroft			if (defined($stat_real) && $cond_lines > 1) {
17584d001e4dSAndy Whitcroft				$stat_real = "[...]\n$stat_real";
17594d001e4dSAndy Whitcroft			}
17604d001e4dSAndy Whitcroft
17619bd49efeSAndy 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";
17624d001e4dSAndy Whitcroft
17634d001e4dSAndy Whitcroft			if ($check && (($sindent % 8) != 0 ||
17644d001e4dSAndy Whitcroft			    ($sindent <= $indent && $s ne ''))) {
17654d001e4dSAndy Whitcroft				WARN("suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
17664d001e4dSAndy Whitcroft			}
17674d001e4dSAndy Whitcroft		}
17684d001e4dSAndy Whitcroft
17696c72ffaaSAndy Whitcroft		# Track the 'values' across context and added lines.
17706c72ffaaSAndy Whitcroft		my $opline = $line; $opline =~ s/^./ /;
17711f65f947SAndy Whitcroft		my ($curr_values, $curr_vars) =
17721f65f947SAndy Whitcroft				annotate_values($opline . "\n", $prev_values);
17736c72ffaaSAndy Whitcroft		$curr_values = $prev_values . $curr_values;
1774c2fdda0dSAndy Whitcroft		if ($dbg_values) {
1775c2fdda0dSAndy Whitcroft			my $outline = $opline; $outline =~ s/\t/ /g;
1776cf655043SAndy Whitcroft			print "$linenr > .$outline\n";
1777cf655043SAndy Whitcroft			print "$linenr > $curr_values\n";
17781f65f947SAndy Whitcroft			print "$linenr >  $curr_vars\n";
1779c2fdda0dSAndy Whitcroft		}
17806c72ffaaSAndy Whitcroft		$prev_values = substr($curr_values, -1);
17816c72ffaaSAndy Whitcroft
178200df344fSAndy Whitcroft#ignore lines not being added
178300df344fSAndy Whitcroft		if ($line=~/^[^\+]/) {next;}
178400df344fSAndy Whitcroft
1785653d4876SAndy Whitcroft# TEST: allow direct testing of the type matcher.
17867429c690SAndy Whitcroft		if ($dbg_type) {
17877429c690SAndy Whitcroft			if ($line =~ /^.\s*$Declare\s*$/) {
17887429c690SAndy Whitcroft				ERROR("TEST: is type\n" . $herecurr);
17897429c690SAndy Whitcroft			} elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
17907429c690SAndy Whitcroft				ERROR("TEST: is not type ($1 is)\n". $herecurr);
17917429c690SAndy Whitcroft			}
1792653d4876SAndy Whitcroft			next;
1793653d4876SAndy Whitcroft		}
1794a1ef277eSAndy Whitcroft# TEST: allow direct testing of the attribute matcher.
1795a1ef277eSAndy Whitcroft		if ($dbg_attr) {
17969360b0e5SAndy Whitcroft			if ($line =~ /^.\s*$Modifier\s*$/) {
1797a1ef277eSAndy Whitcroft				ERROR("TEST: is attr\n" . $herecurr);
17989360b0e5SAndy Whitcroft			} elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
1799a1ef277eSAndy Whitcroft				ERROR("TEST: is not attr ($1 is)\n". $herecurr);
1800a1ef277eSAndy Whitcroft			}
1801a1ef277eSAndy Whitcroft			next;
1802a1ef277eSAndy Whitcroft		}
1803653d4876SAndy Whitcroft
1804f0a594c1SAndy Whitcroft# check for initialisation to aggregates open brace on the next line
180599423c20SAndy Whitcroft		if ($line =~ /^.\s*{/ &&
180699423c20SAndy Whitcroft		    $prevline =~ /(?:^|[^=])=\s*$/) {
1807773647a0SAndy Whitcroft			ERROR("that open brace { should be on the previous line\n" . $hereprev);
1808f0a594c1SAndy Whitcroft		}
1809f0a594c1SAndy Whitcroft
181000df344fSAndy Whitcroft#
181100df344fSAndy Whitcroft# Checks which are anchored on the added line.
181200df344fSAndy Whitcroft#
181300df344fSAndy Whitcroft
1814653d4876SAndy Whitcroft# check for malformed paths in #include statements (uses RAW line)
1815c45dcabdSAndy Whitcroft		if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
1816653d4876SAndy Whitcroft			my $path = $1;
1817653d4876SAndy Whitcroft			if ($path =~ m{//}) {
1818de7d4f0eSAndy Whitcroft				ERROR("malformed #include filename\n" .
1819de7d4f0eSAndy Whitcroft					$herecurr);
1820653d4876SAndy Whitcroft			}
1821653d4876SAndy Whitcroft		}
1822653d4876SAndy Whitcroft
182300df344fSAndy Whitcroft# no C99 // comments
182400df344fSAndy Whitcroft		if ($line =~ m{//}) {
1825de7d4f0eSAndy Whitcroft			ERROR("do not use C99 // comments\n" . $herecurr);
182600df344fSAndy Whitcroft		}
182700df344fSAndy Whitcroft		# Remove C99 comments.
18280a920b5bSAndy Whitcroft		$line =~ s@//.*@@;
18296c72ffaaSAndy Whitcroft		$opline =~ s@//.*@@;
18300a920b5bSAndy Whitcroft
18312b474a1aSAndy Whitcroft# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
18322b474a1aSAndy Whitcroft# the whole statement.
18332b474a1aSAndy Whitcroft#print "APW <$lines[$realline_next - 1]>\n";
18342b474a1aSAndy Whitcroft		if (defined $realline_next &&
18352b474a1aSAndy Whitcroft		    exists $lines[$realline_next - 1] &&
18362b474a1aSAndy Whitcroft		    !defined $suppress_export{$realline_next} &&
18372b474a1aSAndy Whitcroft		    ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
18382b474a1aSAndy Whitcroft		     $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
18393cbf62dfSAndy Whitcroft			# Handle definitions which produce identifiers with
18403cbf62dfSAndy Whitcroft			# a prefix:
18413cbf62dfSAndy Whitcroft			#   XXX(foo);
18423cbf62dfSAndy Whitcroft			#   EXPORT_SYMBOL(something_foo);
1843653d4876SAndy Whitcroft			my $name = $1;
18443cbf62dfSAndy Whitcroft			if ($stat =~ /^.([A-Z_]+)\s*\(\s*($Ident)/ &&
18453cbf62dfSAndy Whitcroft			    $name =~ /^${Ident}_$2/) {
18463cbf62dfSAndy Whitcroft#print "FOO C name<$name>\n";
18473cbf62dfSAndy Whitcroft				$suppress_export{$realline_next} = 1;
18483cbf62dfSAndy Whitcroft
18493cbf62dfSAndy Whitcroft			} elsif ($stat !~ /(?:
18502b474a1aSAndy Whitcroft				\n.}\s*$|
185148012058SAndy Whitcroft				^.DEFINE_$Ident\(\Q$name\E\)|
185248012058SAndy Whitcroft				^.DECLARE_$Ident\(\Q$name\E\)|
185348012058SAndy Whitcroft				^.LIST_HEAD\(\Q$name\E\)|
18542b474a1aSAndy Whitcroft				^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
18552b474a1aSAndy Whitcroft				\b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
185648012058SAndy Whitcroft			    )/x) {
18572b474a1aSAndy Whitcroft#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
18582b474a1aSAndy Whitcroft				$suppress_export{$realline_next} = 2;
18592b474a1aSAndy Whitcroft			} else {
18602b474a1aSAndy Whitcroft				$suppress_export{$realline_next} = 1;
18610a920b5bSAndy Whitcroft			}
18620a920b5bSAndy Whitcroft		}
18632b474a1aSAndy Whitcroft		if (!defined $suppress_export{$linenr} &&
18642b474a1aSAndy Whitcroft		    $prevline =~ /^.\s*$/ &&
18652b474a1aSAndy Whitcroft		    ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
18662b474a1aSAndy Whitcroft		     $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
18672b474a1aSAndy Whitcroft#print "FOO B <$lines[$linenr - 1]>\n";
18682b474a1aSAndy Whitcroft			$suppress_export{$linenr} = 2;
18692b474a1aSAndy Whitcroft		}
18702b474a1aSAndy Whitcroft		if (defined $suppress_export{$linenr} &&
18712b474a1aSAndy Whitcroft		    $suppress_export{$linenr} == 2) {
18722b474a1aSAndy Whitcroft			WARN("EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
18732b474a1aSAndy Whitcroft		}
18740a920b5bSAndy Whitcroft
18755150bda4SJoe Eloff# check for global initialisers.
1876c45dcabdSAndy Whitcroft		if ($line =~ /^.$Type\s*$Ident\s*(?:\s+$Modifier)*\s*=\s*(0|NULL|false)\s*;/) {
18775150bda4SJoe Eloff			ERROR("do not initialise globals to 0 or NULL\n" .
1878f0a594c1SAndy Whitcroft				$herecurr);
1879f0a594c1SAndy Whitcroft		}
18800a920b5bSAndy Whitcroft# check for static initialisers.
18812d1bafd7SAndy Whitcroft		if ($line =~ /\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
1882de7d4f0eSAndy Whitcroft			ERROR("do not initialise statics to 0 or NULL\n" .
1883de7d4f0eSAndy Whitcroft				$herecurr);
18840a920b5bSAndy Whitcroft		}
18850a920b5bSAndy Whitcroft
1886cb710ecaSJoe Perches# check for static const char * arrays.
1887cb710ecaSJoe Perches		if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
1888cb710ecaSJoe Perches			WARN("static const char * array should probably be static const char * const\n" .
1889cb710ecaSJoe Perches				$herecurr);
1890cb710ecaSJoe Perches               }
1891cb710ecaSJoe Perches
1892cb710ecaSJoe Perches# check for static char foo[] = "bar" declarations.
1893cb710ecaSJoe Perches		if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
1894cb710ecaSJoe Perches			WARN("static char array declaration should probably be static const char\n" .
1895cb710ecaSJoe Perches				$herecurr);
1896cb710ecaSJoe Perches               }
1897cb710ecaSJoe Perches
189893ed0e2dSJoe Perches# check for declarations of struct pci_device_id
189993ed0e2dSJoe Perches		if ($line =~ /\bstruct\s+pci_device_id\s+\w+\s*\[\s*\]\s*\=\s*\{/) {
190093ed0e2dSJoe Perches			WARN("Use DEFINE_PCI_DEVICE_TABLE for struct pci_device_id\n" . $herecurr);
190193ed0e2dSJoe Perches		}
190293ed0e2dSJoe Perches
1903653d4876SAndy Whitcroft# check for new typedefs, only function parameters and sparse annotations
1904653d4876SAndy Whitcroft# make sense.
1905653d4876SAndy Whitcroft		if ($line =~ /\btypedef\s/ &&
19068054576dSAndy Whitcroft		    $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
1907c45dcabdSAndy Whitcroft		    $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
19088ed22cadSAndy Whitcroft		    $line !~ /\b$typeTypedefs\b/ &&
1909653d4876SAndy Whitcroft		    $line !~ /\b__bitwise(?:__|)\b/) {
1910de7d4f0eSAndy Whitcroft			WARN("do not add new typedefs\n" . $herecurr);
19110a920b5bSAndy Whitcroft		}
19120a920b5bSAndy Whitcroft
19130a920b5bSAndy Whitcroft# * goes on variable not on type
191465863862SAndy Whitcroft		# (char*[ const])
191500ef4eceSAndy Whitcroft		if ($line =~ m{\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\)}) {
191665863862SAndy Whitcroft			my ($from, $to) = ($1, $1);
1917d8aaf121SAndy Whitcroft
191865863862SAndy Whitcroft			# Should start with a space.
191965863862SAndy Whitcroft			$to =~ s/^(\S)/ $1/;
192065863862SAndy Whitcroft			# Should not end with a space.
192165863862SAndy Whitcroft			$to =~ s/\s+$//;
192265863862SAndy Whitcroft			# '*'s should not have spaces between.
1923f9a0b3d1SAndy Whitcroft			while ($to =~ s/\*\s+\*/\*\*/) {
192465863862SAndy Whitcroft			}
1925d8aaf121SAndy Whitcroft
192665863862SAndy Whitcroft			#print "from<$from> to<$to>\n";
192765863862SAndy Whitcroft			if ($from ne $to) {
192865863862SAndy Whitcroft				ERROR("\"(foo$from)\" should be \"(foo$to)\"\n" .  $herecurr);
192965863862SAndy Whitcroft			}
193000ef4eceSAndy Whitcroft		} elsif ($line =~ m{\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident)}) {
193165863862SAndy Whitcroft			my ($from, $to, $ident) = ($1, $1, $2);
1932d8aaf121SAndy Whitcroft
193365863862SAndy Whitcroft			# Should start with a space.
193465863862SAndy Whitcroft			$to =~ s/^(\S)/ $1/;
193565863862SAndy Whitcroft			# Should not end with a space.
193665863862SAndy Whitcroft			$to =~ s/\s+$//;
193765863862SAndy Whitcroft			# '*'s should not have spaces between.
1938f9a0b3d1SAndy Whitcroft			while ($to =~ s/\*\s+\*/\*\*/) {
193965863862SAndy Whitcroft			}
194065863862SAndy Whitcroft			# Modifiers should have spaces.
194165863862SAndy Whitcroft			$to =~ s/(\b$Modifier$)/$1 /;
194265863862SAndy Whitcroft
1943667026e7SAndy Whitcroft			#print "from<$from> to<$to> ident<$ident>\n";
1944667026e7SAndy Whitcroft			if ($from ne $to && $ident !~ /^$Modifier$/) {
194565863862SAndy Whitcroft				ERROR("\"foo${from}bar\" should be \"foo${to}bar\"\n" .  $herecurr);
194665863862SAndy Whitcroft			}
19470a920b5bSAndy Whitcroft		}
19480a920b5bSAndy Whitcroft
19490a920b5bSAndy Whitcroft# # no BUG() or BUG_ON()
19500a920b5bSAndy Whitcroft# 		if ($line =~ /\b(BUG|BUG_ON)\b/) {
19510a920b5bSAndy Whitcroft# 			print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
19520a920b5bSAndy Whitcroft# 			print "$herecurr";
19530a920b5bSAndy Whitcroft# 			$clean = 0;
19540a920b5bSAndy Whitcroft# 		}
19550a920b5bSAndy Whitcroft
19568905a67cSAndy Whitcroft		if ($line =~ /\bLINUX_VERSION_CODE\b/) {
1957c2fdda0dSAndy Whitcroft			WARN("LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
19588905a67cSAndy Whitcroft		}
19598905a67cSAndy Whitcroft
196017441227SJoe Perches# check for uses of printk_ratelimit
196117441227SJoe Perches		if ($line =~ /\bprintk_ratelimit\s*\(/) {
196217441227SJoe Perches			WARN("Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
196317441227SJoe Perches		}
196417441227SJoe Perches
196500df344fSAndy Whitcroft# printk should use KERN_* levels.  Note that follow on printk's on the
196600df344fSAndy Whitcroft# same line do not need a level, so we use the current block context
196700df344fSAndy Whitcroft# to try and find and validate the current printk.  In summary the current
196825985edcSLucas De Marchi# printk includes all preceding printk's which have no newline on the end.
196900df344fSAndy Whitcroft# we assume the first bad printk is the one to report.
1970f0a594c1SAndy Whitcroft		if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
197100df344fSAndy Whitcroft			my $ok = 0;
197200df344fSAndy Whitcroft			for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
197300df344fSAndy Whitcroft				#print "CHECK<$lines[$ln - 1]\n";
197425985edcSLucas De Marchi				# we have a preceding printk if it ends
197500df344fSAndy Whitcroft				# with "\n" ignore it, else it is to blame
197600df344fSAndy Whitcroft				if ($lines[$ln - 1] =~ m{\bprintk\(}) {
197700df344fSAndy Whitcroft					if ($rawlines[$ln - 1] !~ m{\\n"}) {
197800df344fSAndy Whitcroft						$ok = 1;
197900df344fSAndy Whitcroft					}
198000df344fSAndy Whitcroft					last;
198100df344fSAndy Whitcroft				}
198200df344fSAndy Whitcroft			}
198300df344fSAndy Whitcroft			if ($ok == 0) {
1984de7d4f0eSAndy Whitcroft				WARN("printk() should include KERN_ facility level\n" . $herecurr);
19850a920b5bSAndy Whitcroft			}
198600df344fSAndy Whitcroft		}
19870a920b5bSAndy Whitcroft
1988653d4876SAndy Whitcroft# function brace can't be on same line, except for #defines of do while,
1989653d4876SAndy Whitcroft# or if closed on same line
1990c45dcabdSAndy Whitcroft		if (($line=~/$Type\s*$Ident\(.*\).*\s{/) and
1991c45dcabdSAndy Whitcroft		    !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) {
1992de7d4f0eSAndy Whitcroft			ERROR("open brace '{' following function declarations go on the next line\n" . $herecurr);
19930a920b5bSAndy Whitcroft		}
1994653d4876SAndy Whitcroft
19958905a67cSAndy Whitcroft# open braces for enum, union and struct go on the same line.
19968905a67cSAndy Whitcroft		if ($line =~ /^.\s*{/ &&
19978905a67cSAndy Whitcroft		    $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
19988905a67cSAndy Whitcroft			ERROR("open brace '{' following $1 go on the same line\n" . $hereprev);
19998905a67cSAndy Whitcroft		}
20008905a67cSAndy Whitcroft
20010c73b4ebSAndy Whitcroft# missing space after union, struct or enum definition
20020c73b4ebSAndy Whitcroft		if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?(?:\s+$Ident)?[=\{]/) {
20030c73b4ebSAndy Whitcroft		    WARN("missing space after $1 definition\n" . $herecurr);
20040c73b4ebSAndy Whitcroft		}
20050c73b4ebSAndy Whitcroft
20068d31cfceSAndy Whitcroft# check for spacing round square brackets; allowed:
20078d31cfceSAndy Whitcroft#  1. with a type on the left -- int [] a;
2008fe2a7dbcSAndy Whitcroft#  2. at the beginning of a line for slice initialisers -- [0...10] = 5,
2009fe2a7dbcSAndy Whitcroft#  3. inside a curly brace -- = { [0...10] = 5 }
20108d31cfceSAndy Whitcroft		while ($line =~ /(.*?\s)\[/g) {
20118d31cfceSAndy Whitcroft			my ($where, $prefix) = ($-[1], $1);
20128d31cfceSAndy Whitcroft			if ($prefix !~ /$Type\s+$/ &&
2013fe2a7dbcSAndy Whitcroft			    ($where != 0 || $prefix !~ /^.\s+$/) &&
2014fe2a7dbcSAndy Whitcroft			    $prefix !~ /{\s+$/) {
20158d31cfceSAndy Whitcroft				ERROR("space prohibited before open square bracket '['\n" . $herecurr);
20168d31cfceSAndy Whitcroft			}
20178d31cfceSAndy Whitcroft		}
20188d31cfceSAndy Whitcroft
2019f0a594c1SAndy Whitcroft# check for spaces between functions and their parentheses.
20206c72ffaaSAndy Whitcroft		while ($line =~ /($Ident)\s+\(/g) {
2021c2fdda0dSAndy Whitcroft			my $name = $1;
2022773647a0SAndy Whitcroft			my $ctx_before = substr($line, 0, $-[1]);
2023773647a0SAndy Whitcroft			my $ctx = "$ctx_before$name";
2024c2fdda0dSAndy Whitcroft
2025c2fdda0dSAndy Whitcroft			# Ignore those directives where spaces _are_ permitted.
2026773647a0SAndy Whitcroft			if ($name =~ /^(?:
2027773647a0SAndy Whitcroft				if|for|while|switch|return|case|
2028773647a0SAndy Whitcroft				volatile|__volatile__|
2029773647a0SAndy Whitcroft				__attribute__|format|__extension__|
2030773647a0SAndy Whitcroft				asm|__asm__)$/x)
2031773647a0SAndy Whitcroft			{
2032c2fdda0dSAndy Whitcroft
2033c2fdda0dSAndy Whitcroft			# cpp #define statements have non-optional spaces, ie
2034c2fdda0dSAndy Whitcroft			# if there is a space between the name and the open
2035c2fdda0dSAndy Whitcroft			# parenthesis it is simply not a parameter group.
2036c45dcabdSAndy Whitcroft			} elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
2037773647a0SAndy Whitcroft
2038773647a0SAndy Whitcroft			# cpp #elif statement condition may start with a (
2039c45dcabdSAndy Whitcroft			} elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
2040c2fdda0dSAndy Whitcroft
2041c2fdda0dSAndy Whitcroft			# If this whole things ends with a type its most
2042c2fdda0dSAndy Whitcroft			# likely a typedef for a function.
2043773647a0SAndy Whitcroft			} elsif ($ctx =~ /$Type$/) {
2044c2fdda0dSAndy Whitcroft
2045c2fdda0dSAndy Whitcroft			} else {
2046773647a0SAndy Whitcroft				WARN("space prohibited between function name and open parenthesis '('\n" . $herecurr);
2047f0a594c1SAndy Whitcroft			}
20486c72ffaaSAndy Whitcroft		}
2049653d4876SAndy Whitcroft# Check operator spacing.
20500a920b5bSAndy Whitcroft		if (!($line=~/\#\s*include/)) {
20519c0ca6f9SAndy Whitcroft			my $ops = qr{
20529c0ca6f9SAndy Whitcroft				<<=|>>=|<=|>=|==|!=|
20539c0ca6f9SAndy Whitcroft				\+=|-=|\*=|\/=|%=|\^=|\|=|&=|
20549c0ca6f9SAndy Whitcroft				=>|->|<<|>>|<|>|=|!|~|
20551f65f947SAndy Whitcroft				&&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
20561f65f947SAndy Whitcroft				\?|:
20579c0ca6f9SAndy Whitcroft			}x;
2058cf655043SAndy Whitcroft			my @elements = split(/($ops|;)/, $opline);
205900df344fSAndy Whitcroft			my $off = 0;
20606c72ffaaSAndy Whitcroft
20616c72ffaaSAndy Whitcroft			my $blank = copy_spacing($opline);
20626c72ffaaSAndy Whitcroft
20630a920b5bSAndy Whitcroft			for (my $n = 0; $n < $#elements; $n += 2) {
20644a0df2efSAndy Whitcroft				$off += length($elements[$n]);
20654a0df2efSAndy Whitcroft
206625985edcSLucas De Marchi				# Pick up the preceding and succeeding characters.
2067773647a0SAndy Whitcroft				my $ca = substr($opline, 0, $off);
2068773647a0SAndy Whitcroft				my $cc = '';
2069773647a0SAndy Whitcroft				if (length($opline) >= ($off + length($elements[$n + 1]))) {
2070773647a0SAndy Whitcroft					$cc = substr($opline, $off + length($elements[$n + 1]));
2071773647a0SAndy Whitcroft				}
2072773647a0SAndy Whitcroft				my $cb = "$ca$;$cc";
2073773647a0SAndy Whitcroft
20744a0df2efSAndy Whitcroft				my $a = '';
20754a0df2efSAndy Whitcroft				$a = 'V' if ($elements[$n] ne '');
20764a0df2efSAndy Whitcroft				$a = 'W' if ($elements[$n] =~ /\s$/);
2077cf655043SAndy Whitcroft				$a = 'C' if ($elements[$n] =~ /$;$/);
20784a0df2efSAndy Whitcroft				$a = 'B' if ($elements[$n] =~ /(\[|\()$/);
20794a0df2efSAndy Whitcroft				$a = 'O' if ($elements[$n] eq '');
2080773647a0SAndy Whitcroft				$a = 'E' if ($ca =~ /^\s*$/);
20814a0df2efSAndy Whitcroft
20820a920b5bSAndy Whitcroft				my $op = $elements[$n + 1];
20834a0df2efSAndy Whitcroft
20844a0df2efSAndy Whitcroft				my $c = '';
20850a920b5bSAndy Whitcroft				if (defined $elements[$n + 2]) {
20864a0df2efSAndy Whitcroft					$c = 'V' if ($elements[$n + 2] ne '');
20874a0df2efSAndy Whitcroft					$c = 'W' if ($elements[$n + 2] =~ /^\s/);
2088cf655043SAndy Whitcroft					$c = 'C' if ($elements[$n + 2] =~ /^$;/);
20894a0df2efSAndy Whitcroft					$c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
20904a0df2efSAndy Whitcroft					$c = 'O' if ($elements[$n + 2] eq '');
20918b1b3378SAndy Whitcroft					$c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
20924a0df2efSAndy Whitcroft				} else {
20934a0df2efSAndy Whitcroft					$c = 'E';
20940a920b5bSAndy Whitcroft				}
20950a920b5bSAndy Whitcroft
20964a0df2efSAndy Whitcroft				my $ctx = "${a}x${c}";
20974a0df2efSAndy Whitcroft
20984a0df2efSAndy Whitcroft				my $at = "(ctx:$ctx)";
20994a0df2efSAndy Whitcroft
21006c72ffaaSAndy Whitcroft				my $ptr = substr($blank, 0, $off) . "^";
2101de7d4f0eSAndy Whitcroft				my $hereptr = "$hereline$ptr\n";
21020a920b5bSAndy Whitcroft
210374048ed8SAndy Whitcroft				# Pull out the value of this operator.
21046c72ffaaSAndy Whitcroft				my $op_type = substr($curr_values, $off + 1, 1);
21050a920b5bSAndy Whitcroft
21061f65f947SAndy Whitcroft				# Get the full operator variant.
21071f65f947SAndy Whitcroft				my $opv = $op . substr($curr_vars, $off, 1);
21081f65f947SAndy Whitcroft
210913214adfSAndy Whitcroft				# Ignore operators passed as parameters.
211013214adfSAndy Whitcroft				if ($op_type ne 'V' &&
211113214adfSAndy Whitcroft				    $ca =~ /\s$/ && $cc =~ /^\s*,/) {
211213214adfSAndy Whitcroft
2113cf655043SAndy Whitcroft#				# Ignore comments
2114cf655043SAndy Whitcroft#				} elsif ($op =~ /^$;+$/) {
211513214adfSAndy Whitcroft
2116d8aaf121SAndy Whitcroft				# ; should have either the end of line or a space or \ after it
211713214adfSAndy Whitcroft				} elsif ($op eq ';') {
2118cf655043SAndy Whitcroft					if ($ctx !~ /.x[WEBC]/ &&
2119cf655043SAndy Whitcroft					    $cc !~ /^\\/ && $cc !~ /^;/) {
2120773647a0SAndy Whitcroft						ERROR("space required after that '$op' $at\n" . $hereptr);
2121d8aaf121SAndy Whitcroft					}
2122d8aaf121SAndy Whitcroft
2123d8aaf121SAndy Whitcroft				# // is a comment
2124d8aaf121SAndy Whitcroft				} elsif ($op eq '//') {
21250a920b5bSAndy Whitcroft
21261f65f947SAndy Whitcroft				# No spaces for:
21271f65f947SAndy Whitcroft				#   ->
21281f65f947SAndy Whitcroft				#   :   when part of a bitfield
21291f65f947SAndy Whitcroft				} elsif ($op eq '->' || $opv eq ':B') {
21304a0df2efSAndy Whitcroft					if ($ctx =~ /Wx.|.xW/) {
2131773647a0SAndy Whitcroft						ERROR("spaces prohibited around that '$op' $at\n" . $hereptr);
21320a920b5bSAndy Whitcroft					}
21330a920b5bSAndy Whitcroft
21340a920b5bSAndy Whitcroft				# , must have a space on the right.
21350a920b5bSAndy Whitcroft				} elsif ($op eq ',') {
2136cf655043SAndy Whitcroft					if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
2137773647a0SAndy Whitcroft						ERROR("space required after that '$op' $at\n" . $hereptr);
21380a920b5bSAndy Whitcroft					}
21390a920b5bSAndy Whitcroft
21409c0ca6f9SAndy Whitcroft				# '*' as part of a type definition -- reported already.
214174048ed8SAndy Whitcroft				} elsif ($opv eq '*_') {
21429c0ca6f9SAndy Whitcroft					#warn "'*' is part of type\n";
21439c0ca6f9SAndy Whitcroft
21449c0ca6f9SAndy Whitcroft				# unary operators should have a space before and
21459c0ca6f9SAndy Whitcroft				# none after.  May be left adjacent to another
21469c0ca6f9SAndy Whitcroft				# unary operator, or a cast
21479c0ca6f9SAndy Whitcroft				} elsif ($op eq '!' || $op eq '~' ||
214874048ed8SAndy Whitcroft					 $opv eq '*U' || $opv eq '-U' ||
21490d413866SAndy Whitcroft					 $opv eq '&U' || $opv eq '&&U') {
2150cf655043SAndy Whitcroft					if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
2151773647a0SAndy Whitcroft						ERROR("space required before that '$op' $at\n" . $hereptr);
21520a920b5bSAndy Whitcroft					}
2153a3340b35SAndy Whitcroft					if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
2154171ae1a4SAndy Whitcroft						# A unary '*' may be const
2155171ae1a4SAndy Whitcroft
2156171ae1a4SAndy Whitcroft					} elsif ($ctx =~ /.xW/) {
2157fb9e9096SAndy Whitcroft						ERROR("space prohibited after that '$op' $at\n" . $hereptr);
21580a920b5bSAndy Whitcroft					}
21590a920b5bSAndy Whitcroft
21600a920b5bSAndy Whitcroft				# unary ++ and unary -- are allowed no space on one side.
21610a920b5bSAndy Whitcroft				} elsif ($op eq '++' or $op eq '--') {
2162773647a0SAndy Whitcroft					if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
2163773647a0SAndy Whitcroft						ERROR("space required one side of that '$op' $at\n" . $hereptr);
21640a920b5bSAndy Whitcroft					}
2165773647a0SAndy Whitcroft					if ($ctx =~ /Wx[BE]/ ||
2166773647a0SAndy Whitcroft					    ($ctx =~ /Wx./ && $cc =~ /^;/)) {
2167773647a0SAndy Whitcroft						ERROR("space prohibited before that '$op' $at\n" . $hereptr);
2168653d4876SAndy Whitcroft					}
2169773647a0SAndy Whitcroft					if ($ctx =~ /ExW/) {
2170773647a0SAndy Whitcroft						ERROR("space prohibited after that '$op' $at\n" . $hereptr);
2171773647a0SAndy Whitcroft					}
2172773647a0SAndy Whitcroft
21730a920b5bSAndy Whitcroft
21740a920b5bSAndy Whitcroft				# << and >> may either have or not have spaces both sides
21759c0ca6f9SAndy Whitcroft				} elsif ($op eq '<<' or $op eq '>>' or
21769c0ca6f9SAndy Whitcroft					 $op eq '&' or $op eq '^' or $op eq '|' or
21779c0ca6f9SAndy Whitcroft					 $op eq '+' or $op eq '-' or
2178c2fdda0dSAndy Whitcroft					 $op eq '*' or $op eq '/' or
2179c2fdda0dSAndy Whitcroft					 $op eq '%')
21800a920b5bSAndy Whitcroft				{
2181773647a0SAndy Whitcroft					if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
2182de7d4f0eSAndy Whitcroft						ERROR("need consistent spacing around '$op' $at\n" .
2183de7d4f0eSAndy Whitcroft							$hereptr);
21840a920b5bSAndy Whitcroft					}
21850a920b5bSAndy Whitcroft
21861f65f947SAndy Whitcroft				# A colon needs no spaces before when it is
21871f65f947SAndy Whitcroft				# terminating a case value or a label.
21881f65f947SAndy Whitcroft				} elsif ($opv eq ':C' || $opv eq ':L') {
21891f65f947SAndy Whitcroft					if ($ctx =~ /Wx./) {
21901f65f947SAndy Whitcroft						ERROR("space prohibited before that '$op' $at\n" . $hereptr);
21911f65f947SAndy Whitcroft					}
21921f65f947SAndy Whitcroft
21930a920b5bSAndy Whitcroft				# All the others need spaces both sides.
2194cf655043SAndy Whitcroft				} elsif ($ctx !~ /[EWC]x[CWE]/) {
21951f65f947SAndy Whitcroft					my $ok = 0;
21961f65f947SAndy Whitcroft
219722f2a2efSAndy Whitcroft					# Ignore email addresses <foo@bar>
21981f65f947SAndy Whitcroft					if (($op eq '<' &&
21991f65f947SAndy Whitcroft					     $cc =~ /^\S+\@\S+>/) ||
22001f65f947SAndy Whitcroft					    ($op eq '>' &&
22011f65f947SAndy Whitcroft					     $ca =~ /<\S+\@\S+$/))
22021f65f947SAndy Whitcroft					{
22031f65f947SAndy Whitcroft					    	$ok = 1;
22041f65f947SAndy Whitcroft					}
22051f65f947SAndy Whitcroft
22061f65f947SAndy Whitcroft					# Ignore ?:
22071f65f947SAndy Whitcroft					if (($opv eq ':O' && $ca =~ /\?$/) ||
22081f65f947SAndy Whitcroft					    ($op eq '?' && $cc =~ /^:/)) {
22091f65f947SAndy Whitcroft					    	$ok = 1;
22101f65f947SAndy Whitcroft					}
22111f65f947SAndy Whitcroft
22121f65f947SAndy Whitcroft					if ($ok == 0) {
2213773647a0SAndy Whitcroft						ERROR("spaces required around that '$op' $at\n" . $hereptr);
22140a920b5bSAndy Whitcroft					}
221522f2a2efSAndy Whitcroft				}
22164a0df2efSAndy Whitcroft				$off += length($elements[$n + 1]);
22170a920b5bSAndy Whitcroft			}
22180a920b5bSAndy Whitcroft		}
22190a920b5bSAndy Whitcroft
2220f0a594c1SAndy Whitcroft# check for multiple assignments
2221f0a594c1SAndy Whitcroft		if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
22226c72ffaaSAndy Whitcroft			CHK("multiple assignments should be avoided\n" . $herecurr);
2223f0a594c1SAndy Whitcroft		}
2224f0a594c1SAndy Whitcroft
222522f2a2efSAndy Whitcroft## # check for multiple declarations, allowing for a function declaration
222622f2a2efSAndy Whitcroft## # continuation.
222722f2a2efSAndy Whitcroft## 		if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
222822f2a2efSAndy Whitcroft## 		    $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
222922f2a2efSAndy Whitcroft##
223022f2a2efSAndy Whitcroft## 			# Remove any bracketed sections to ensure we do not
223122f2a2efSAndy Whitcroft## 			# falsly report the parameters of functions.
223222f2a2efSAndy Whitcroft## 			my $ln = $line;
223322f2a2efSAndy Whitcroft## 			while ($ln =~ s/\([^\(\)]*\)//g) {
223422f2a2efSAndy Whitcroft## 			}
223522f2a2efSAndy Whitcroft## 			if ($ln =~ /,/) {
223622f2a2efSAndy Whitcroft## 				WARN("declaring multiple variables together should be avoided\n" . $herecurr);
223722f2a2efSAndy Whitcroft## 			}
223822f2a2efSAndy Whitcroft## 		}
2239f0a594c1SAndy Whitcroft
22400a920b5bSAndy Whitcroft#need space before brace following if, while, etc
224122f2a2efSAndy Whitcroft		if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
224222f2a2efSAndy Whitcroft		    $line =~ /do{/) {
2243773647a0SAndy Whitcroft			ERROR("space required before the open brace '{'\n" . $herecurr);
2244de7d4f0eSAndy Whitcroft		}
2245de7d4f0eSAndy Whitcroft
2246de7d4f0eSAndy Whitcroft# closing brace should have a space following it when it has anything
2247de7d4f0eSAndy Whitcroft# on the line
2248de7d4f0eSAndy Whitcroft		if ($line =~ /}(?!(?:,|;|\)))\S/) {
2249773647a0SAndy Whitcroft			ERROR("space required after that close brace '}'\n" . $herecurr);
22500a920b5bSAndy Whitcroft		}
22510a920b5bSAndy Whitcroft
225222f2a2efSAndy Whitcroft# check spacing on square brackets
225322f2a2efSAndy Whitcroft		if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
2254773647a0SAndy Whitcroft			ERROR("space prohibited after that open square bracket '['\n" . $herecurr);
225522f2a2efSAndy Whitcroft		}
225622f2a2efSAndy Whitcroft		if ($line =~ /\s\]/) {
2257773647a0SAndy Whitcroft			ERROR("space prohibited before that close square bracket ']'\n" . $herecurr);
225822f2a2efSAndy Whitcroft		}
225922f2a2efSAndy Whitcroft
2260c45dcabdSAndy Whitcroft# check spacing on parentheses
22619c0ca6f9SAndy Whitcroft		if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
22629c0ca6f9SAndy Whitcroft		    $line !~ /for\s*\(\s+;/) {
2263773647a0SAndy Whitcroft			ERROR("space prohibited after that open parenthesis '('\n" . $herecurr);
226422f2a2efSAndy Whitcroft		}
226513214adfSAndy Whitcroft		if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
2266c45dcabdSAndy Whitcroft		    $line !~ /for\s*\(.*;\s+\)/ &&
2267c45dcabdSAndy Whitcroft		    $line !~ /:\s+\)/) {
2268773647a0SAndy Whitcroft			ERROR("space prohibited before that close parenthesis ')'\n" . $herecurr);
226922f2a2efSAndy Whitcroft		}
227022f2a2efSAndy Whitcroft
22710a920b5bSAndy Whitcroft#goto labels aren't indented, allow a single space however
22724a0df2efSAndy Whitcroft		if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
22730a920b5bSAndy Whitcroft		   !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
2274de7d4f0eSAndy Whitcroft			WARN("labels should not be indented\n" . $herecurr);
22750a920b5bSAndy Whitcroft		}
22760a920b5bSAndy Whitcroft
2277c45dcabdSAndy Whitcroft# Return is not a function.
2278c45dcabdSAndy Whitcroft		if (defined($stat) && $stat =~ /^.\s*return(\s*)(\(.*);/s) {
2279c45dcabdSAndy Whitcroft			my $spacing = $1;
2280c45dcabdSAndy Whitcroft			my $value = $2;
2281c45dcabdSAndy Whitcroft
228286f9d059SAndy Whitcroft			# Flatten any parentheses
2283fb2d2c1bSAndy Whitcroft			$value =~ s/\(/ \(/g;
2284fb2d2c1bSAndy Whitcroft			$value =~ s/\)/\) /g;
228563f17f89SAndy Whitcroft			while ($value =~ s/\[[^\{\}]*\]/1/ ||
228663f17f89SAndy Whitcroft			       $value !~ /(?:$Ident|-?$Constant)\s*
228763f17f89SAndy Whitcroft					     $Compare\s*
228863f17f89SAndy Whitcroft					     (?:$Ident|-?$Constant)/x &&
228963f17f89SAndy Whitcroft			       $value =~ s/\([^\(\)]*\)/1/) {
2290c45dcabdSAndy Whitcroft			}
2291fb2d2c1bSAndy Whitcroft#print "value<$value>\n";
2292fb2d2c1bSAndy Whitcroft			if ($value =~ /^\s*(?:$Ident|-?$Constant)\s*$/) {
2293c45dcabdSAndy Whitcroft				ERROR("return is not a function, parentheses are not required\n" . $herecurr);
2294c45dcabdSAndy Whitcroft
2295c45dcabdSAndy Whitcroft			} elsif ($spacing !~ /\s+/) {
2296c45dcabdSAndy Whitcroft				ERROR("space required before the open parenthesis '('\n" . $herecurr);
2297c45dcabdSAndy Whitcroft			}
2298c45dcabdSAndy Whitcroft		}
229953a3c448SAndy Whitcroft# Return of what appears to be an errno should normally be -'ve
230053a3c448SAndy Whitcroft		if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) {
230153a3c448SAndy Whitcroft			my $name = $1;
230253a3c448SAndy Whitcroft			if ($name ne 'EOF' && $name ne 'ERROR') {
230353a3c448SAndy Whitcroft				WARN("return of an errno should typically be -ve (return -$1)\n" . $herecurr);
230453a3c448SAndy Whitcroft			}
230553a3c448SAndy Whitcroft		}
2306c45dcabdSAndy Whitcroft
2307*7d2367afSJoe Perches# typecasts on min/max could be min_t/max_t
2308*7d2367afSJoe Perches		if ($line =~ /^\+(?:.*?)\b(min|max)\s*\($Typecast{0,1}($LvalOrFunc)\s*,\s*$Typecast{0,1}($LvalOrFunc)\s*\)/) {
2309*7d2367afSJoe Perches			if (defined $2 || defined $8) {
2310*7d2367afSJoe Perches				my $call = $1;
2311*7d2367afSJoe Perches				my $cast1 = deparenthesize($2);
2312*7d2367afSJoe Perches				my $arg1 = $3;
2313*7d2367afSJoe Perches				my $cast2 = deparenthesize($8);
2314*7d2367afSJoe Perches				my $arg2 = $9;
2315*7d2367afSJoe Perches				my $cast;
2316*7d2367afSJoe Perches
2317*7d2367afSJoe Perches				if ($cast1 ne "" && $cast2 ne "") {
2318*7d2367afSJoe Perches					$cast = "$cast1 or $cast2";
2319*7d2367afSJoe Perches				} elsif ($cast1 ne "") {
2320*7d2367afSJoe Perches					$cast = $cast1;
2321*7d2367afSJoe Perches				} else {
2322*7d2367afSJoe Perches					$cast = $cast2;
2323*7d2367afSJoe Perches				}
2324*7d2367afSJoe Perches				WARN("$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . $herecurr);
2325*7d2367afSJoe Perches			}
2326*7d2367afSJoe Perches		}
2327*7d2367afSJoe Perches
23280a920b5bSAndy Whitcroft# Need a space before open parenthesis after if, while etc
23294a0df2efSAndy Whitcroft		if ($line=~/\b(if|while|for|switch)\(/) {
2330773647a0SAndy Whitcroft			ERROR("space required before the open parenthesis '('\n" . $herecurr);
23310a920b5bSAndy Whitcroft		}
23320a920b5bSAndy Whitcroft
2333f5fe35ddSAndy Whitcroft# Check for illegal assignment in if conditional -- and check for trailing
2334f5fe35ddSAndy Whitcroft# statements after the conditional.
2335170d3a22SAndy Whitcroft		if ($line =~ /do\s*(?!{)/) {
2336170d3a22SAndy Whitcroft			my ($stat_next) = ctx_statement_block($line_nr_next,
2337170d3a22SAndy Whitcroft						$remain_next, $off_next);
2338170d3a22SAndy Whitcroft			$stat_next =~ s/\n./\n /g;
2339170d3a22SAndy Whitcroft			##print "stat<$stat> stat_next<$stat_next>\n";
2340170d3a22SAndy Whitcroft
2341170d3a22SAndy Whitcroft			if ($stat_next =~ /^\s*while\b/) {
2342170d3a22SAndy Whitcroft				# If the statement carries leading newlines,
2343170d3a22SAndy Whitcroft				# then count those as offsets.
2344170d3a22SAndy Whitcroft				my ($whitespace) =
2345170d3a22SAndy Whitcroft					($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
2346170d3a22SAndy Whitcroft				my $offset =
2347170d3a22SAndy Whitcroft					statement_rawlines($whitespace) - 1;
2348170d3a22SAndy Whitcroft
2349170d3a22SAndy Whitcroft				$suppress_whiletrailers{$line_nr_next +
2350170d3a22SAndy Whitcroft								$offset} = 1;
2351170d3a22SAndy Whitcroft			}
2352170d3a22SAndy Whitcroft		}
2353170d3a22SAndy Whitcroft		if (!defined $suppress_whiletrailers{$linenr} &&
2354170d3a22SAndy Whitcroft		    $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
2355171ae1a4SAndy Whitcroft			my ($s, $c) = ($stat, $cond);
23568905a67cSAndy Whitcroft
2357b53c8e10SAndy Whitcroft			if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
2358c2fdda0dSAndy Whitcroft				ERROR("do not use assignment in if condition\n" . $herecurr);
23598905a67cSAndy Whitcroft			}
23608905a67cSAndy Whitcroft
23618905a67cSAndy Whitcroft			# Find out what is on the end of the line after the
23628905a67cSAndy Whitcroft			# conditional.
2363773647a0SAndy Whitcroft			substr($s, 0, length($c), '');
23648905a67cSAndy Whitcroft			$s =~ s/\n.*//g;
236513214adfSAndy Whitcroft			$s =~ s/$;//g; 	# Remove any comments
236653210168SAndy Whitcroft			if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
236753210168SAndy Whitcroft			    $c !~ /}\s*while\s*/)
2368773647a0SAndy Whitcroft			{
2369bb44ad39SAndy Whitcroft				# Find out how long the conditional actually is.
2370bb44ad39SAndy Whitcroft				my @newlines = ($c =~ /\n/gs);
2371bb44ad39SAndy Whitcroft				my $cond_lines = 1 + $#newlines;
237242bdf74cSHidetoshi Seto				my $stat_real = '';
2373bb44ad39SAndy Whitcroft
237442bdf74cSHidetoshi Seto				$stat_real = raw_line($linenr, $cond_lines)
237542bdf74cSHidetoshi Seto							. "\n" if ($cond_lines);
2376bb44ad39SAndy Whitcroft				if (defined($stat_real) && $cond_lines > 1) {
2377bb44ad39SAndy Whitcroft					$stat_real = "[...]\n$stat_real";
2378bb44ad39SAndy Whitcroft				}
2379bb44ad39SAndy Whitcroft
2380bb44ad39SAndy Whitcroft				ERROR("trailing statements should be on next line\n" . $herecurr . $stat_real);
23818905a67cSAndy Whitcroft			}
23828905a67cSAndy Whitcroft		}
23838905a67cSAndy Whitcroft
238413214adfSAndy Whitcroft# Check for bitwise tests written as boolean
238513214adfSAndy Whitcroft		if ($line =~ /
238613214adfSAndy Whitcroft			(?:
238713214adfSAndy Whitcroft				(?:\[|\(|\&\&|\|\|)
238813214adfSAndy Whitcroft				\s*0[xX][0-9]+\s*
238913214adfSAndy Whitcroft				(?:\&\&|\|\|)
239013214adfSAndy Whitcroft			|
239113214adfSAndy Whitcroft				(?:\&\&|\|\|)
239213214adfSAndy Whitcroft				\s*0[xX][0-9]+\s*
239313214adfSAndy Whitcroft				(?:\&\&|\|\||\)|\])
239413214adfSAndy Whitcroft			)/x)
239513214adfSAndy Whitcroft		{
239613214adfSAndy Whitcroft			WARN("boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
239713214adfSAndy Whitcroft		}
239813214adfSAndy Whitcroft
23998905a67cSAndy Whitcroft# if and else should not have general statements after it
240013214adfSAndy Whitcroft		if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
240113214adfSAndy Whitcroft			my $s = $1;
240213214adfSAndy Whitcroft			$s =~ s/$;//g; 	# Remove any comments
240313214adfSAndy Whitcroft			if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
24048905a67cSAndy Whitcroft				ERROR("trailing statements should be on next line\n" . $herecurr);
24050a920b5bSAndy Whitcroft			}
240613214adfSAndy Whitcroft		}
240739667782SAndy Whitcroft# if should not continue a brace
240839667782SAndy Whitcroft		if ($line =~ /}\s*if\b/) {
240939667782SAndy Whitcroft			ERROR("trailing statements should be on next line\n" .
241039667782SAndy Whitcroft				$herecurr);
241139667782SAndy Whitcroft		}
2412a1080bf8SAndy Whitcroft# case and default should not have general statements after them
2413a1080bf8SAndy Whitcroft		if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
2414a1080bf8SAndy Whitcroft		    $line !~ /\G(?:
24153fef12d6SAndy Whitcroft			(?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
2416a1080bf8SAndy Whitcroft			\s*return\s+
2417a1080bf8SAndy Whitcroft		    )/xg)
2418a1080bf8SAndy Whitcroft		{
2419a1080bf8SAndy Whitcroft			ERROR("trailing statements should be on next line\n" . $herecurr);
2420a1080bf8SAndy Whitcroft		}
24210a920b5bSAndy Whitcroft
24220a920b5bSAndy Whitcroft		# Check for }<nl>else {, these must be at the same
24230a920b5bSAndy Whitcroft		# indent level to be relevant to each other.
24240a920b5bSAndy Whitcroft		if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
24250a920b5bSAndy Whitcroft						$previndent == $indent) {
2426de7d4f0eSAndy Whitcroft			ERROR("else should follow close brace '}'\n" . $hereprev);
24270a920b5bSAndy Whitcroft		}
24280a920b5bSAndy Whitcroft
2429c2fdda0dSAndy Whitcroft		if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and
2430c2fdda0dSAndy Whitcroft						$previndent == $indent) {
2431c2fdda0dSAndy Whitcroft			my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
2432c2fdda0dSAndy Whitcroft
2433c2fdda0dSAndy Whitcroft			# Find out what is on the end of the line after the
2434c2fdda0dSAndy Whitcroft			# conditional.
2435773647a0SAndy Whitcroft			substr($s, 0, length($c), '');
2436c2fdda0dSAndy Whitcroft			$s =~ s/\n.*//g;
2437c2fdda0dSAndy Whitcroft
2438c2fdda0dSAndy Whitcroft			if ($s =~ /^\s*;/) {
2439c2fdda0dSAndy Whitcroft				ERROR("while should follow close brace '}'\n" . $hereprev);
2440c2fdda0dSAndy Whitcroft			}
2441c2fdda0dSAndy Whitcroft		}
2442c2fdda0dSAndy Whitcroft
24430a920b5bSAndy Whitcroft#studly caps, commented out until figure out how to distinguish between use of existing and adding new
24440a920b5bSAndy Whitcroft#		if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) {
24450a920b5bSAndy Whitcroft#		    print "No studly caps, use _\n";
24460a920b5bSAndy Whitcroft#		    print "$herecurr";
24470a920b5bSAndy Whitcroft#		    $clean = 0;
24480a920b5bSAndy Whitcroft#		}
24490a920b5bSAndy Whitcroft
24500a920b5bSAndy Whitcroft#no spaces allowed after \ in define
2451c45dcabdSAndy Whitcroft		if ($line=~/\#\s*define.*\\\s$/) {
2452de7d4f0eSAndy Whitcroft			WARN("Whitepspace after \\ makes next lines useless\n" . $herecurr);
24530a920b5bSAndy Whitcroft		}
24540a920b5bSAndy Whitcroft
2455653d4876SAndy Whitcroft#warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
2456c45dcabdSAndy Whitcroft		if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
2457e09dec48SAndy Whitcroft			my $file = "$1.h";
2458e09dec48SAndy Whitcroft			my $checkfile = "include/linux/$file";
2459e09dec48SAndy Whitcroft			if (-f "$root/$checkfile" &&
2460e09dec48SAndy Whitcroft			    $realfile ne $checkfile &&
24617840a94cSWolfram Sang			    $1 !~ /$allowed_asm_includes/)
2462c45dcabdSAndy Whitcroft			{
2463e09dec48SAndy Whitcroft				if ($realfile =~ m{^arch/}) {
2464e09dec48SAndy Whitcroft					CHK("Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
2465e09dec48SAndy Whitcroft				} else {
2466e09dec48SAndy Whitcroft					WARN("Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
2467e09dec48SAndy Whitcroft				}
24680a920b5bSAndy Whitcroft			}
24690a920b5bSAndy Whitcroft		}
24700a920b5bSAndy Whitcroft
2471653d4876SAndy Whitcroft# multi-statement macros should be enclosed in a do while loop, grab the
2472653d4876SAndy Whitcroft# first statement and ensure its the whole macro if its not enclosed
2473cf655043SAndy Whitcroft# in a known good container
2474b8f96a31SAndy Whitcroft		if ($realfile !~ m@/vmlinux.lds.h$@ &&
2475b8f96a31SAndy Whitcroft		    $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
2476d8aaf121SAndy Whitcroft			my $ln = $linenr;
2477d8aaf121SAndy Whitcroft			my $cnt = $realcnt;
2478c45dcabdSAndy Whitcroft			my ($off, $dstat, $dcond, $rest);
2479c45dcabdSAndy Whitcroft			my $ctx = '';
2480653d4876SAndy Whitcroft
2481c45dcabdSAndy Whitcroft			my $args = defined($1);
2482de7d4f0eSAndy Whitcroft
2483c45dcabdSAndy Whitcroft			# Find the end of the macro and limit our statement
2484c45dcabdSAndy Whitcroft			# search to that.
2485c45dcabdSAndy Whitcroft			while ($cnt > 0 && defined $lines[$ln - 1] &&
2486c45dcabdSAndy Whitcroft				$lines[$ln - 1] =~ /^(?:-|..*\\$)/)
2487c45dcabdSAndy Whitcroft			{
2488c45dcabdSAndy Whitcroft				$ctx .= $rawlines[$ln - 1] . "\n";
2489a3bb97a7SAndy Whitcroft				$cnt-- if ($lines[$ln - 1] !~ /^-/);
2490c45dcabdSAndy Whitcroft				$ln++;
2491de7d4f0eSAndy Whitcroft			}
2492c45dcabdSAndy Whitcroft			$ctx .= $rawlines[$ln - 1];
2493d8aaf121SAndy Whitcroft
2494c45dcabdSAndy Whitcroft			($dstat, $dcond, $ln, $cnt, $off) =
2495c45dcabdSAndy Whitcroft				ctx_statement_block($linenr, $ln - $linenr + 1, 0);
2496c45dcabdSAndy Whitcroft			#print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
2497a3bb97a7SAndy Whitcroft			#print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
2498c45dcabdSAndy Whitcroft
2499c45dcabdSAndy Whitcroft			# Extract the remainder of the define (if any) and
2500c45dcabdSAndy Whitcroft			# rip off surrounding spaces, and trailing \'s.
2501c45dcabdSAndy Whitcroft			$rest = '';
2502636d140aSAndy Whitcroft			while ($off != 0 || ($cnt > 0 && $rest =~ /\\\s*$/)) {
2503636d140aSAndy Whitcroft				#print "ADDING cnt<$cnt> $off <" . substr($lines[$ln - 1], $off) . "> rest<$rest>\n";
2504a3bb97a7SAndy Whitcroft				if ($off != 0 || $lines[$ln - 1] !~ /^-/) {
2505c45dcabdSAndy Whitcroft					$rest .= substr($lines[$ln - 1], $off) . "\n";
2506c45dcabdSAndy Whitcroft					$cnt--;
2507a3bb97a7SAndy Whitcroft				}
2508a3bb97a7SAndy Whitcroft				$ln++;
2509c45dcabdSAndy Whitcroft				$off = 0;
2510c45dcabdSAndy Whitcroft			}
2511c45dcabdSAndy Whitcroft			$rest =~ s/\\\n.//g;
2512c45dcabdSAndy Whitcroft			$rest =~ s/^\s*//s;
2513c45dcabdSAndy Whitcroft			$rest =~ s/\s*$//s;
2514c45dcabdSAndy Whitcroft
2515c45dcabdSAndy Whitcroft			# Clean up the original statement.
2516c45dcabdSAndy Whitcroft			if ($args) {
2517c45dcabdSAndy Whitcroft				substr($dstat, 0, length($dcond), '');
2518d8aaf121SAndy Whitcroft			} else {
2519c45dcabdSAndy Whitcroft				$dstat =~ s/^.\s*\#\s*define\s+$Ident\s*//;
2520c45dcabdSAndy Whitcroft			}
2521292f1a9bSAndy Whitcroft			$dstat =~ s/$;//g;
2522c45dcabdSAndy Whitcroft			$dstat =~ s/\\\n.//g;
2523c45dcabdSAndy Whitcroft			$dstat =~ s/^\s*//s;
2524c45dcabdSAndy Whitcroft			$dstat =~ s/\s*$//s;
2525c45dcabdSAndy Whitcroft
2526c45dcabdSAndy Whitcroft			# Flatten any parentheses and braces
2527bf30d6edSAndy Whitcroft			while ($dstat =~ s/\([^\(\)]*\)/1/ ||
2528bf30d6edSAndy Whitcroft			       $dstat =~ s/\{[^\{\}]*\}/1/ ||
2529bf30d6edSAndy Whitcroft			       $dstat =~ s/\[[^\{\}]*\]/1/)
2530bf30d6edSAndy Whitcroft			{
2531c45dcabdSAndy Whitcroft			}
2532c45dcabdSAndy Whitcroft
2533c45dcabdSAndy Whitcroft			my $exceptions = qr{
2534c45dcabdSAndy Whitcroft				$Declare|
2535c45dcabdSAndy Whitcroft				module_param_named|
2536c45dcabdSAndy Whitcroft				MODULE_PARAM_DESC|
2537c45dcabdSAndy Whitcroft				DECLARE_PER_CPU|
2538c45dcabdSAndy Whitcroft				DEFINE_PER_CPU|
2539383099fdSAndy Whitcroft				__typeof__\(|
254022fd2d3eSStefani Seibold				union|
254122fd2d3eSStefani Seibold				struct|
2542ea71a0a0SAndy Whitcroft				\.$Ident\s*=\s*|
2543ea71a0a0SAndy Whitcroft				^\"|\"$
2544c45dcabdSAndy Whitcroft			}x;
25455eaa20b9SAndy Whitcroft			#print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
25465eaa20b9SAndy Whitcroft			if ($rest ne '' && $rest ne ',') {
2547c45dcabdSAndy Whitcroft				if ($rest !~ /while\s*\(/ &&
2548c45dcabdSAndy Whitcroft				    $dstat !~ /$exceptions/)
2549c45dcabdSAndy Whitcroft				{
2550c45dcabdSAndy Whitcroft					ERROR("Macros with multiple statements should be enclosed in a do - while loop\n" . "$here\n$ctx\n");
2551c45dcabdSAndy Whitcroft				}
2552c45dcabdSAndy Whitcroft
2553c45dcabdSAndy Whitcroft			} elsif ($ctx !~ /;/) {
2554c45dcabdSAndy Whitcroft				if ($dstat ne '' &&
2555c45dcabdSAndy Whitcroft				    $dstat !~ /^(?:$Ident|-?$Constant)$/ &&
2556c45dcabdSAndy Whitcroft				    $dstat !~ /$exceptions/ &&
2557b132e5d5SAndy Whitcroft				    $dstat !~ /^\.$Ident\s*=/ &&
2558c45dcabdSAndy Whitcroft				    $dstat =~ /$Operators/)
2559c45dcabdSAndy Whitcroft				{
2560de7d4f0eSAndy Whitcroft					ERROR("Macros with complex values should be enclosed in parenthesis\n" . "$here\n$ctx\n");
2561d8aaf121SAndy Whitcroft				}
25620a920b5bSAndy Whitcroft			}
2563653d4876SAndy Whitcroft		}
25640a920b5bSAndy Whitcroft
2565080ba929SMike Frysinger# make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
2566080ba929SMike Frysinger# all assignments may have only one of the following with an assignment:
2567080ba929SMike Frysinger#	.
2568080ba929SMike Frysinger#	ALIGN(...)
2569080ba929SMike Frysinger#	VMLINUX_SYMBOL(...)
2570080ba929SMike Frysinger		if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
2571080ba929SMike Frysinger			WARN("vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
2572080ba929SMike Frysinger		}
2573080ba929SMike Frysinger
2574f0a594c1SAndy Whitcroft# check for redundant bracing round if etc
257513214adfSAndy Whitcroft		if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
257613214adfSAndy Whitcroft			my ($level, $endln, @chunks) =
2577cf655043SAndy Whitcroft				ctx_statement_full($linenr, $realcnt, 1);
257813214adfSAndy Whitcroft			#print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
2579cf655043SAndy Whitcroft			#print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
2580cf655043SAndy Whitcroft			if ($#chunks > 0 && $level == 0) {
258113214adfSAndy Whitcroft				my $allowed = 0;
258213214adfSAndy Whitcroft				my $seen = 0;
2583773647a0SAndy Whitcroft				my $herectx = $here . "\n";
2584cf655043SAndy Whitcroft				my $ln = $linenr - 1;
258513214adfSAndy Whitcroft				for my $chunk (@chunks) {
258613214adfSAndy Whitcroft					my ($cond, $block) = @{$chunk};
258713214adfSAndy Whitcroft
2588773647a0SAndy Whitcroft					# If the condition carries leading newlines, then count those as offsets.
2589773647a0SAndy Whitcroft					my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
2590773647a0SAndy Whitcroft					my $offset = statement_rawlines($whitespace) - 1;
2591773647a0SAndy Whitcroft
2592773647a0SAndy Whitcroft					#print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
2593773647a0SAndy Whitcroft
2594773647a0SAndy Whitcroft					# We have looked at and allowed this specific line.
2595773647a0SAndy Whitcroft					$suppress_ifbraces{$ln + $offset} = 1;
2596773647a0SAndy Whitcroft
2597773647a0SAndy Whitcroft					$herectx .= "$rawlines[$ln + $offset]\n[...]\n";
2598cf655043SAndy Whitcroft					$ln += statement_rawlines($block) - 1;
2599cf655043SAndy Whitcroft
2600773647a0SAndy Whitcroft					substr($block, 0, length($cond), '');
260113214adfSAndy Whitcroft
260213214adfSAndy Whitcroft					$seen++ if ($block =~ /^\s*{/);
260313214adfSAndy Whitcroft
2604cf655043SAndy Whitcroft					#print "cond<$cond> block<$block> allowed<$allowed>\n";
2605cf655043SAndy Whitcroft					if (statement_lines($cond) > 1) {
2606cf655043SAndy Whitcroft						#print "APW: ALLOWED: cond<$cond>\n";
260713214adfSAndy Whitcroft						$allowed = 1;
260813214adfSAndy Whitcroft					}
260913214adfSAndy Whitcroft					if ($block =~/\b(?:if|for|while)\b/) {
2610cf655043SAndy Whitcroft						#print "APW: ALLOWED: block<$block>\n";
261113214adfSAndy Whitcroft						$allowed = 1;
261213214adfSAndy Whitcroft					}
2613cf655043SAndy Whitcroft					if (statement_block_size($block) > 1) {
2614cf655043SAndy Whitcroft						#print "APW: ALLOWED: lines block<$block>\n";
261513214adfSAndy Whitcroft						$allowed = 1;
261613214adfSAndy Whitcroft					}
261713214adfSAndy Whitcroft				}
261813214adfSAndy Whitcroft				if ($seen && !$allowed) {
2619cf655043SAndy Whitcroft					WARN("braces {} are not necessary for any arm of this statement\n" . $herectx);
262013214adfSAndy Whitcroft				}
262113214adfSAndy Whitcroft			}
262213214adfSAndy Whitcroft		}
2623773647a0SAndy Whitcroft		if (!defined $suppress_ifbraces{$linenr - 1} &&
262413214adfSAndy Whitcroft					$line =~ /\b(if|while|for|else)\b/) {
2625cf655043SAndy Whitcroft			my $allowed = 0;
2626f0a594c1SAndy Whitcroft
2627cf655043SAndy Whitcroft			# Check the pre-context.
2628cf655043SAndy Whitcroft			if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
2629cf655043SAndy Whitcroft				#print "APW: ALLOWED: pre<$1>\n";
2630cf655043SAndy Whitcroft				$allowed = 1;
2631f0a594c1SAndy Whitcroft			}
2632773647a0SAndy Whitcroft
2633773647a0SAndy Whitcroft			my ($level, $endln, @chunks) =
2634773647a0SAndy Whitcroft				ctx_statement_full($linenr, $realcnt, $-[0]);
2635773647a0SAndy Whitcroft
2636cf655043SAndy Whitcroft			# Check the condition.
2637cf655043SAndy Whitcroft			my ($cond, $block) = @{$chunks[0]};
2638773647a0SAndy Whitcroft			#print "CHECKING<$linenr> cond<$cond> block<$block>\n";
2639cf655043SAndy Whitcroft			if (defined $cond) {
2640773647a0SAndy Whitcroft				substr($block, 0, length($cond), '');
2641cf655043SAndy Whitcroft			}
2642cf655043SAndy Whitcroft			if (statement_lines($cond) > 1) {
2643cf655043SAndy Whitcroft				#print "APW: ALLOWED: cond<$cond>\n";
2644cf655043SAndy Whitcroft				$allowed = 1;
2645cf655043SAndy Whitcroft			}
2646cf655043SAndy Whitcroft			if ($block =~/\b(?:if|for|while)\b/) {
2647cf655043SAndy Whitcroft				#print "APW: ALLOWED: block<$block>\n";
2648cf655043SAndy Whitcroft				$allowed = 1;
2649cf655043SAndy Whitcroft			}
2650cf655043SAndy Whitcroft			if (statement_block_size($block) > 1) {
2651cf655043SAndy Whitcroft				#print "APW: ALLOWED: lines block<$block>\n";
2652cf655043SAndy Whitcroft				$allowed = 1;
2653cf655043SAndy Whitcroft			}
2654cf655043SAndy Whitcroft			# Check the post-context.
2655cf655043SAndy Whitcroft			if (defined $chunks[1]) {
2656cf655043SAndy Whitcroft				my ($cond, $block) = @{$chunks[1]};
2657cf655043SAndy Whitcroft				if (defined $cond) {
2658773647a0SAndy Whitcroft					substr($block, 0, length($cond), '');
2659cf655043SAndy Whitcroft				}
2660cf655043SAndy Whitcroft				if ($block =~ /^\s*\{/) {
2661cf655043SAndy Whitcroft					#print "APW: ALLOWED: chunk-1 block<$block>\n";
2662cf655043SAndy Whitcroft					$allowed = 1;
2663cf655043SAndy Whitcroft				}
2664cf655043SAndy Whitcroft			}
2665cf655043SAndy Whitcroft			if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
2666cf655043SAndy Whitcroft				my $herectx = $here . "\n";;
2667f055663cSAndy Whitcroft				my $cnt = statement_rawlines($block);
2668cf655043SAndy Whitcroft
2669f055663cSAndy Whitcroft				for (my $n = 0; $n < $cnt; $n++) {
2670f055663cSAndy Whitcroft					$herectx .= raw_line($linenr, $n) . "\n";;
2671cf655043SAndy Whitcroft				}
2672cf655043SAndy Whitcroft
2673cf655043SAndy Whitcroft				WARN("braces {} are not necessary for single statement blocks\n" . $herectx);
2674f0a594c1SAndy Whitcroft			}
2675f0a594c1SAndy Whitcroft		}
2676f0a594c1SAndy Whitcroft
2677653d4876SAndy Whitcroft# don't include deprecated include files (uses RAW line)
26784a0df2efSAndy Whitcroft		for my $inc (@dep_includes) {
2679c45dcabdSAndy Whitcroft			if ($rawline =~ m@^.\s*\#\s*include\s*\<$inc>@) {
2680de7d4f0eSAndy Whitcroft				ERROR("Don't use <$inc>: see Documentation/feature-removal-schedule.txt\n" . $herecurr);
26810a920b5bSAndy Whitcroft			}
26820a920b5bSAndy Whitcroft		}
26830a920b5bSAndy Whitcroft
26844a0df2efSAndy Whitcroft# don't use deprecated functions
26854a0df2efSAndy Whitcroft		for my $func (@dep_functions) {
268600df344fSAndy Whitcroft			if ($line =~ /\b$func\b/) {
2687de7d4f0eSAndy Whitcroft				ERROR("Don't use $func(): see Documentation/feature-removal-schedule.txt\n" . $herecurr);
26884a0df2efSAndy Whitcroft			}
26894a0df2efSAndy Whitcroft		}
26904a0df2efSAndy Whitcroft
26914a0df2efSAndy Whitcroft# no volatiles please
26926c72ffaaSAndy Whitcroft		my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
26936c72ffaaSAndy Whitcroft		if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
2694de7d4f0eSAndy Whitcroft			WARN("Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
26954a0df2efSAndy Whitcroft		}
26964a0df2efSAndy Whitcroft
269700df344fSAndy Whitcroft# warn about #if 0
2698c45dcabdSAndy Whitcroft		if ($line =~ /^.\s*\#\s*if\s+0\b/) {
2699de7d4f0eSAndy Whitcroft			CHK("if this code is redundant consider removing it\n" .
2700de7d4f0eSAndy Whitcroft				$herecurr);
27014a0df2efSAndy Whitcroft		}
27024a0df2efSAndy Whitcroft
2703f0a594c1SAndy Whitcroft# check for needless kfree() checks
2704f0a594c1SAndy Whitcroft		if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
2705f0a594c1SAndy Whitcroft			my $expr = $1;
2706f0a594c1SAndy Whitcroft			if ($line =~ /\bkfree\(\Q$expr\E\);/) {
27073c232147SWolfram Sang				WARN("kfree(NULL) is safe this check is probably not required\n" . $hereprev);
2708f0a594c1SAndy Whitcroft			}
2709f0a594c1SAndy Whitcroft		}
27104c432a8fSGreg Kroah-Hartman# check for needless usb_free_urb() checks
27114c432a8fSGreg Kroah-Hartman		if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
27124c432a8fSGreg Kroah-Hartman			my $expr = $1;
27134c432a8fSGreg Kroah-Hartman			if ($line =~ /\busb_free_urb\(\Q$expr\E\);/) {
27144c432a8fSGreg Kroah-Hartman				WARN("usb_free_urb(NULL) is safe this check is probably not required\n" . $hereprev);
27154c432a8fSGreg Kroah-Hartman			}
27164c432a8fSGreg Kroah-Hartman		}
2717f0a594c1SAndy Whitcroft
27181a15a250SPatrick Pannuto# prefer usleep_range over udelay
27191a15a250SPatrick Pannuto		if ($line =~ /\budelay\s*\(\s*(\w+)\s*\)/) {
27201a15a250SPatrick Pannuto			# ignore udelay's < 10, however
27211a15a250SPatrick Pannuto			if (! (($1 =~ /(\d+)/) && ($1 < 10)) ) {
27221a15a250SPatrick Pannuto				CHK("usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $line);
27231a15a250SPatrick Pannuto			}
27241a15a250SPatrick Pannuto		}
27251a15a250SPatrick Pannuto
272609ef8725SPatrick Pannuto# warn about unexpectedly long msleep's
272709ef8725SPatrick Pannuto		if ($line =~ /\bmsleep\s*\((\d+)\);/) {
272809ef8725SPatrick Pannuto			if ($1 < 20) {
272909ef8725SPatrick Pannuto				WARN("msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $line);
273009ef8725SPatrick Pannuto			}
273109ef8725SPatrick Pannuto		}
273209ef8725SPatrick Pannuto
273300df344fSAndy Whitcroft# warn about #ifdefs in C files
2734c45dcabdSAndy Whitcroft#		if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
273500df344fSAndy Whitcroft#			print "#ifdef in C files should be avoided\n";
273600df344fSAndy Whitcroft#			print "$herecurr";
273700df344fSAndy Whitcroft#			$clean = 0;
273800df344fSAndy Whitcroft#		}
273900df344fSAndy Whitcroft
274022f2a2efSAndy Whitcroft# warn about spacing in #ifdefs
2741c45dcabdSAndy Whitcroft		if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
274222f2a2efSAndy Whitcroft			ERROR("exactly one space required after that #$1\n" . $herecurr);
274322f2a2efSAndy Whitcroft		}
274422f2a2efSAndy Whitcroft
27454a0df2efSAndy Whitcroft# check for spinlock_t definitions without a comment.
2746171ae1a4SAndy Whitcroft		if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
2747171ae1a4SAndy Whitcroft		    $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
27484a0df2efSAndy Whitcroft			my $which = $1;
27494a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
2750de7d4f0eSAndy Whitcroft				CHK("$1 definition without comment\n" . $herecurr);
27514a0df2efSAndy Whitcroft			}
27524a0df2efSAndy Whitcroft		}
27534a0df2efSAndy Whitcroft# check for memory barriers without a comment.
27544a0df2efSAndy Whitcroft		if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
27554a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
2756de7d4f0eSAndy Whitcroft				CHK("memory barrier without comment\n" . $herecurr);
27574a0df2efSAndy Whitcroft			}
27584a0df2efSAndy Whitcroft		}
27594a0df2efSAndy Whitcroft# check of hardware specific defines
2760c45dcabdSAndy Whitcroft		if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
2761de7d4f0eSAndy Whitcroft			CHK("architecture specific defines should be avoided\n" .  $herecurr);
27620a920b5bSAndy Whitcroft		}
2763653d4876SAndy Whitcroft
2764d4977c78STobias Klauser# Check that the storage class is at the beginning of a declaration
2765d4977c78STobias Klauser		if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
2766d4977c78STobias Klauser			WARN("storage class should be at the beginning of the declaration\n" . $herecurr)
2767d4977c78STobias Klauser		}
2768d4977c78STobias Klauser
2769de7d4f0eSAndy Whitcroft# check the location of the inline attribute, that it is between
2770de7d4f0eSAndy Whitcroft# storage class and type.
27719c0ca6f9SAndy Whitcroft		if ($line =~ /\b$Type\s+$Inline\b/ ||
27729c0ca6f9SAndy Whitcroft		    $line =~ /\b$Inline\s+$Storage\b/) {
2773de7d4f0eSAndy Whitcroft			ERROR("inline keyword should sit between storage class and type\n" . $herecurr);
2774de7d4f0eSAndy Whitcroft		}
2775de7d4f0eSAndy Whitcroft
27768905a67cSAndy Whitcroft# Check for __inline__ and __inline, prefer inline
27778905a67cSAndy Whitcroft		if ($line =~ /\b(__inline__|__inline)\b/) {
27788905a67cSAndy Whitcroft			WARN("plain inline is preferred over $1\n" . $herecurr);
27798905a67cSAndy Whitcroft		}
27808905a67cSAndy Whitcroft
27813d130fd0SJoe Perches# Check for __attribute__ packed, prefer __packed
27823d130fd0SJoe Perches		if ($line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
27833d130fd0SJoe Perches			WARN("__packed is preferred over __attribute__((packed))\n" . $herecurr);
27843d130fd0SJoe Perches		}
27853d130fd0SJoe Perches
27868f53a9b8SJoe Perches# check for sizeof(&)
27878f53a9b8SJoe Perches		if ($line =~ /\bsizeof\s*\(\s*\&/) {
27888f53a9b8SJoe Perches			WARN("sizeof(& should be avoided\n" . $herecurr);
27898f53a9b8SJoe Perches		}
27908f53a9b8SJoe Perches
2791428e2fdcSJoe Perches# check for line continuations in quoted strings with odd counts of "
2792428e2fdcSJoe Perches		if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) {
2793428e2fdcSJoe Perches			WARN("Avoid line continuations in quoted strings\n" . $herecurr);
2794428e2fdcSJoe Perches		}
2795428e2fdcSJoe Perches
2796de7d4f0eSAndy Whitcroft# check for new externs in .c files.
2797171ae1a4SAndy Whitcroft		if ($realfile =~ /\.c$/ && defined $stat &&
2798c45dcabdSAndy Whitcroft		    $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
2799171ae1a4SAndy Whitcroft		{
2800c45dcabdSAndy Whitcroft			my $function_name = $1;
2801c45dcabdSAndy Whitcroft			my $paren_space = $2;
2802171ae1a4SAndy Whitcroft
2803171ae1a4SAndy Whitcroft			my $s = $stat;
2804171ae1a4SAndy Whitcroft			if (defined $cond) {
2805171ae1a4SAndy Whitcroft				substr($s, 0, length($cond), '');
2806171ae1a4SAndy Whitcroft			}
2807c45dcabdSAndy Whitcroft			if ($s =~ /^\s*;/ &&
2808c45dcabdSAndy Whitcroft			    $function_name ne 'uninitialized_var')
2809c45dcabdSAndy Whitcroft			{
2810de7d4f0eSAndy Whitcroft				WARN("externs should be avoided in .c files\n" .  $herecurr);
2811de7d4f0eSAndy Whitcroft			}
2812de7d4f0eSAndy Whitcroft
2813171ae1a4SAndy Whitcroft			if ($paren_space =~ /\n/) {
2814171ae1a4SAndy Whitcroft				WARN("arguments for function declarations should follow identifier\n" . $herecurr);
2815171ae1a4SAndy Whitcroft			}
28169c9ba34eSAndy Whitcroft
28179c9ba34eSAndy Whitcroft		} elsif ($realfile =~ /\.c$/ && defined $stat &&
28189c9ba34eSAndy Whitcroft		    $stat =~ /^.\s*extern\s+/)
28199c9ba34eSAndy Whitcroft		{
28209c9ba34eSAndy Whitcroft			WARN("externs should be avoided in .c files\n" .  $herecurr);
2821171ae1a4SAndy Whitcroft		}
2822171ae1a4SAndy Whitcroft
2823de7d4f0eSAndy Whitcroft# checks for new __setup's
2824de7d4f0eSAndy Whitcroft		if ($rawline =~ /\b__setup\("([^"]*)"/) {
2825de7d4f0eSAndy Whitcroft			my $name = $1;
2826de7d4f0eSAndy Whitcroft
2827de7d4f0eSAndy Whitcroft			if (!grep(/$name/, @setup_docs)) {
2828de7d4f0eSAndy Whitcroft				CHK("__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
2829de7d4f0eSAndy Whitcroft			}
2830653d4876SAndy Whitcroft		}
28319c0ca6f9SAndy Whitcroft
28329c0ca6f9SAndy Whitcroft# check for pointless casting of kmalloc return
2833caf2a54fSJoe Perches		if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) {
28349c0ca6f9SAndy Whitcroft			WARN("unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
28359c0ca6f9SAndy Whitcroft		}
283613214adfSAndy Whitcroft
2837caf2a54fSJoe Perches# check for multiple semicolons
2838caf2a54fSJoe Perches		if ($line =~ /;\s*;\s*$/) {
2839caf2a54fSJoe Perches		    WARN("Statements terminations use 1 semicolon\n" . $herecurr);
2840caf2a54fSJoe Perches		}
2841caf2a54fSJoe Perches
284213214adfSAndy Whitcroft# check for gcc specific __FUNCTION__
284313214adfSAndy Whitcroft		if ($line =~ /__FUNCTION__/) {
284413214adfSAndy Whitcroft			WARN("__func__ should be used instead of gcc specific __FUNCTION__\n"  . $herecurr);
284513214adfSAndy Whitcroft		}
2846773647a0SAndy Whitcroft
28474882720bSThomas Gleixner# check for semaphores initialized locked
28484882720bSThomas Gleixner		if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
2849773647a0SAndy Whitcroft			WARN("consider using a completion\n" . $herecurr);
28501704f47bSPeter Zijlstra
2851773647a0SAndy Whitcroft		}
285233ee3b2eSAlexey Dobriyan# recommend kstrto* over simple_strto*
2853773647a0SAndy Whitcroft		if ($line =~ /\bsimple_(strto.*?)\s*\(/) {
285433ee3b2eSAlexey Dobriyan			WARN("consider using kstrto* in preference to simple_$1\n" . $herecurr);
2855773647a0SAndy Whitcroft		}
2856f3db6639SMichael Ellerman# check for __initcall(), use device_initcall() explicitly please
2857f3db6639SMichael Ellerman		if ($line =~ /^.\s*__initcall\s*\(/) {
2858f3db6639SMichael Ellerman			WARN("please use device_initcall() instead of __initcall()\n" . $herecurr);
2859f3db6639SMichael Ellerman		}
286079404849SEmese Revfy# check for various ops structs, ensure they are const.
286179404849SEmese Revfy		my $struct_ops = qr{acpi_dock_ops|
286279404849SEmese Revfy				address_space_operations|
286379404849SEmese Revfy				backlight_ops|
286479404849SEmese Revfy				block_device_operations|
286579404849SEmese Revfy				dentry_operations|
286679404849SEmese Revfy				dev_pm_ops|
286779404849SEmese Revfy				dma_map_ops|
286879404849SEmese Revfy				extent_io_ops|
286979404849SEmese Revfy				file_lock_operations|
287079404849SEmese Revfy				file_operations|
287179404849SEmese Revfy				hv_ops|
287279404849SEmese Revfy				ide_dma_ops|
287379404849SEmese Revfy				intel_dvo_dev_ops|
287479404849SEmese Revfy				item_operations|
287579404849SEmese Revfy				iwl_ops|
287679404849SEmese Revfy				kgdb_arch|
287779404849SEmese Revfy				kgdb_io|
287879404849SEmese Revfy				kset_uevent_ops|
287979404849SEmese Revfy				lock_manager_operations|
288079404849SEmese Revfy				microcode_ops|
288179404849SEmese Revfy				mtrr_ops|
288279404849SEmese Revfy				neigh_ops|
288379404849SEmese Revfy				nlmsvc_binding|
288479404849SEmese Revfy				pci_raw_ops|
288579404849SEmese Revfy				pipe_buf_operations|
288679404849SEmese Revfy				platform_hibernation_ops|
288779404849SEmese Revfy				platform_suspend_ops|
288879404849SEmese Revfy				proto_ops|
288979404849SEmese Revfy				rpc_pipe_ops|
289079404849SEmese Revfy				seq_operations|
289179404849SEmese Revfy				snd_ac97_build_ops|
289279404849SEmese Revfy				soc_pcmcia_socket_ops|
289379404849SEmese Revfy				stacktrace_ops|
289479404849SEmese Revfy				sysfs_ops|
289579404849SEmese Revfy				tty_operations|
289679404849SEmese Revfy				usb_mon_operations|
289779404849SEmese Revfy				wd_ops}x;
28986903ffb2SAndy Whitcroft		if ($line !~ /\bconst\b/ &&
289979404849SEmese Revfy		    $line =~ /\bstruct\s+($struct_ops)\b/) {
29006903ffb2SAndy Whitcroft			WARN("struct $1 should normally be const\n" .
29016903ffb2SAndy Whitcroft				$herecurr);
29022b6db5cbSAndy Whitcroft		}
2903773647a0SAndy Whitcroft
2904773647a0SAndy Whitcroft# use of NR_CPUS is usually wrong
2905773647a0SAndy Whitcroft# ignore definitions of NR_CPUS and usage to define arrays as likely right
2906773647a0SAndy Whitcroft		if ($line =~ /\bNR_CPUS\b/ &&
2907c45dcabdSAndy Whitcroft		    $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
2908c45dcabdSAndy Whitcroft		    $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
2909171ae1a4SAndy Whitcroft		    $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
2910171ae1a4SAndy Whitcroft		    $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
2911171ae1a4SAndy Whitcroft		    $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
2912773647a0SAndy Whitcroft		{
2913773647a0SAndy Whitcroft			WARN("usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
2914773647a0SAndy Whitcroft		}
29159c9ba34eSAndy Whitcroft
29169c9ba34eSAndy Whitcroft# check for %L{u,d,i} in strings
29179c9ba34eSAndy Whitcroft		my $string;
29189c9ba34eSAndy Whitcroft		while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
29199c9ba34eSAndy Whitcroft			$string = substr($rawline, $-[1], $+[1] - $-[1]);
29202a1bc5d5SAndy Whitcroft			$string =~ s/%%/__/g;
29219c9ba34eSAndy Whitcroft			if ($string =~ /(?<!%)%L[udi]/) {
29229c9ba34eSAndy Whitcroft				WARN("\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
29239c9ba34eSAndy Whitcroft				last;
29249c9ba34eSAndy Whitcroft			}
29259c9ba34eSAndy Whitcroft		}
2926691d77b6SAndy Whitcroft
2927691d77b6SAndy Whitcroft# whine mightly about in_atomic
2928691d77b6SAndy Whitcroft		if ($line =~ /\bin_atomic\s*\(/) {
2929691d77b6SAndy Whitcroft			if ($realfile =~ m@^drivers/@) {
2930691d77b6SAndy Whitcroft				ERROR("do not use in_atomic in drivers\n" . $herecurr);
2931f4a87736SAndy Whitcroft			} elsif ($realfile !~ m@^kernel/@) {
2932691d77b6SAndy Whitcroft				WARN("use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
2933691d77b6SAndy Whitcroft			}
2934691d77b6SAndy Whitcroft		}
29351704f47bSPeter Zijlstra
29361704f47bSPeter Zijlstra# check for lockdep_set_novalidate_class
29371704f47bSPeter Zijlstra		if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
29381704f47bSPeter Zijlstra		    $line =~ /__lockdep_no_validate__\s*\)/ ) {
29391704f47bSPeter Zijlstra			if ($realfile !~ m@^kernel/lockdep@ &&
29401704f47bSPeter Zijlstra			    $realfile !~ m@^include/linux/lockdep@ &&
29411704f47bSPeter Zijlstra			    $realfile !~ m@^drivers/base/core@) {
29421704f47bSPeter Zijlstra				ERROR("lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
29431704f47bSPeter Zijlstra			}
29441704f47bSPeter Zijlstra		}
294588f8831cSDave Jones
294688f8831cSDave Jones		if ($line =~ /debugfs_create_file.*S_IWUGO/ ||
294788f8831cSDave Jones		    $line =~ /DEVICE_ATTR.*S_IWUGO/ ) {
294888f8831cSDave Jones			WARN("Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
294988f8831cSDave Jones		}
2950309c00c7SDave Jones
2951309c00c7SDave Jones		# Check for memset with swapped arguments
2952309c00c7SDave Jones		if ($line =~ /memset.*\,(\ |)(0x|)0(\ |0|)\);/) {
2953309c00c7SDave Jones			ERROR("memset size is 3rd argument, not the second.\n" . $herecurr);
2954309c00c7SDave Jones		}
295513214adfSAndy Whitcroft	}
295613214adfSAndy Whitcroft
295713214adfSAndy Whitcroft	# If we have no input at all, then there is nothing to report on
295813214adfSAndy Whitcroft	# so just keep quiet.
295913214adfSAndy Whitcroft	if ($#rawlines == -1) {
296013214adfSAndy Whitcroft		exit(0);
29610a920b5bSAndy Whitcroft	}
29620a920b5bSAndy Whitcroft
29638905a67cSAndy Whitcroft	# In mailback mode only produce a report in the negative, for
29648905a67cSAndy Whitcroft	# things that appear to be patches.
29658905a67cSAndy Whitcroft	if ($mailback && ($clean == 1 || !$is_patch)) {
29668905a67cSAndy Whitcroft		exit(0);
29678905a67cSAndy Whitcroft	}
29688905a67cSAndy Whitcroft
29698905a67cSAndy Whitcroft	# This is not a patch, and we are are in 'no-patch' mode so
29708905a67cSAndy Whitcroft	# just keep quiet.
29718905a67cSAndy Whitcroft	if (!$chk_patch && !$is_patch) {
29728905a67cSAndy Whitcroft		exit(0);
29738905a67cSAndy Whitcroft	}
29748905a67cSAndy Whitcroft
29758905a67cSAndy Whitcroft	if (!$is_patch) {
2976de7d4f0eSAndy Whitcroft		ERROR("Does not appear to be a unified-diff format patch\n");
29770a920b5bSAndy Whitcroft	}
29780a920b5bSAndy Whitcroft	if ($is_patch && $chk_signoff && $signoff == 0) {
2979de7d4f0eSAndy Whitcroft		ERROR("Missing Signed-off-by: line(s)\n");
29800a920b5bSAndy Whitcroft	}
29810a920b5bSAndy Whitcroft
2982f0a594c1SAndy Whitcroft	print report_dump();
298313214adfSAndy Whitcroft	if ($summary && !($clean == 1 && $quiet == 1)) {
298413214adfSAndy Whitcroft		print "$filename " if ($summary_file);
29856c72ffaaSAndy Whitcroft		print "total: $cnt_error errors, $cnt_warn warnings, " .
29866c72ffaaSAndy Whitcroft			(($check)? "$cnt_chk checks, " : "") .
29876c72ffaaSAndy Whitcroft			"$cnt_lines lines checked\n";
29888905a67cSAndy Whitcroft		print "\n" if ($quiet == 0);
29896c72ffaaSAndy Whitcroft	}
29908905a67cSAndy Whitcroft
2991d2c0a235SAndy Whitcroft	if ($quiet == 0) {
2992d2c0a235SAndy Whitcroft		# If there were whitespace errors which cleanpatch can fix
2993d2c0a235SAndy Whitcroft		# then suggest that.
2994d2c0a235SAndy Whitcroft		if ($rpt_cleaners) {
2995d2c0a235SAndy Whitcroft			print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n";
2996d2c0a235SAndy Whitcroft			print "      scripts/cleanfile\n\n";
2997b0781216SMike Frysinger			$rpt_cleaners = 0;
2998d2c0a235SAndy Whitcroft		}
2999d2c0a235SAndy Whitcroft	}
3000d2c0a235SAndy Whitcroft
30010a920b5bSAndy Whitcroft	if ($clean == 1 && $quiet == 0) {
3002c2fdda0dSAndy Whitcroft		print "$vname has no obvious style problems and is ready for submission.\n"
30030a920b5bSAndy Whitcroft	}
30040a920b5bSAndy Whitcroft	if ($clean == 0 && $quiet == 0) {
3005c2fdda0dSAndy Whitcroft		print "$vname has style problems, please review.  If any of these errors\n";
30060a920b5bSAndy Whitcroft		print "are false positives report them to the maintainer, see\n";
30070a920b5bSAndy Whitcroft		print "CHECKPATCH in MAINTAINERS.\n";
30080a920b5bSAndy Whitcroft	}
300913214adfSAndy Whitcroft
30100a920b5bSAndy Whitcroft	return $clean;
30110a920b5bSAndy Whitcroft}
3012