xref: /linux-6.15/scripts/checkpatch.pl (revision 20112475)
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|
148165e72a6SSven Eckelmann			__ref|
149165e72a6SSven Eckelmann			__rcu
1506c72ffaaSAndy Whitcroft		}x;
15152131292SWolfram Sang
15252131292SWolfram Sang# Notes to $Attribute:
15352131292SWolfram Sang# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
1546c72ffaaSAndy Whitcroftour $Attribute	= qr{
1556c72ffaaSAndy Whitcroft			const|
15603f1df7dSJoe Perches			__percpu|
15703f1df7dSJoe Perches			__nocast|
15803f1df7dSJoe Perches			__safe|
15903f1df7dSJoe Perches			__bitwise__|
16003f1df7dSJoe Perches			__packed__|
16103f1df7dSJoe Perches			__packed2__|
16203f1df7dSJoe Perches			__naked|
16303f1df7dSJoe Perches			__maybe_unused|
16403f1df7dSJoe Perches			__always_unused|
16503f1df7dSJoe Perches			__noreturn|
16603f1df7dSJoe Perches			__used|
16703f1df7dSJoe Perches			__cold|
16803f1df7dSJoe Perches			__noclone|
16903f1df7dSJoe Perches			__deprecated|
1706c72ffaaSAndy Whitcroft			__read_mostly|
1716c72ffaaSAndy Whitcroft			__kprobes|
17252131292SWolfram Sang			__(?:mem|cpu|dev|)(?:initdata|initconst|init\b)|
17324e1d81aSAndy Whitcroft			____cacheline_aligned|
17424e1d81aSAndy Whitcroft			____cacheline_aligned_in_smp|
1755fe3af11SAndy Whitcroft			____cacheline_internodealigned_in_smp|
1765fe3af11SAndy Whitcroft			__weak
1776c72ffaaSAndy Whitcroft		  }x;
178c45dcabdSAndy Whitcroftour $Modifier;
1796c72ffaaSAndy Whitcroftour $Inline	= qr{inline|__always_inline|noinline};
1806c72ffaaSAndy Whitcroftour $Member	= qr{->$Ident|\.$Ident|\[[^]]*\]};
1816c72ffaaSAndy Whitcroftour $Lval	= qr{$Ident(?:$Member)*};
1826c72ffaaSAndy Whitcroft
1836c72ffaaSAndy Whitcroftour $Constant	= qr{(?:[0-9]+|0x[0-9a-fA-F]+)[UL]*};
1846c72ffaaSAndy Whitcroftour $Assignment	= qr{(?:\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=)};
18586f9d059SAndy Whitcroftour $Compare    = qr{<=|>=|==|!=|<|>};
1866c72ffaaSAndy Whitcroftour $Operators	= qr{
1876c72ffaaSAndy Whitcroft			<=|>=|==|!=|
1886c72ffaaSAndy Whitcroft			=>|->|<<|>>|<|>|!|~|
189c2fdda0dSAndy Whitcroft			&&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%
1906c72ffaaSAndy Whitcroft		  }x;
1916c72ffaaSAndy Whitcroft
1928905a67cSAndy Whitcroftour $NonptrType;
1938905a67cSAndy Whitcroftour $Type;
1948905a67cSAndy Whitcroftour $Declare;
1958905a67cSAndy Whitcroft
196171ae1a4SAndy Whitcroftour $UTF8	= qr {
197171ae1a4SAndy Whitcroft	[\x09\x0A\x0D\x20-\x7E]              # ASCII
198171ae1a4SAndy Whitcroft	| [\xC2-\xDF][\x80-\xBF]             # non-overlong 2-byte
199171ae1a4SAndy Whitcroft	|  \xE0[\xA0-\xBF][\x80-\xBF]        # excluding overlongs
200171ae1a4SAndy Whitcroft	| [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}  # straight 3-byte
201171ae1a4SAndy Whitcroft	|  \xED[\x80-\x9F][\x80-\xBF]        # excluding surrogates
202171ae1a4SAndy Whitcroft	|  \xF0[\x90-\xBF][\x80-\xBF]{2}     # planes 1-3
203171ae1a4SAndy Whitcroft	| [\xF1-\xF3][\x80-\xBF]{3}          # planes 4-15
204171ae1a4SAndy Whitcroft	|  \xF4[\x80-\x8F][\x80-\xBF]{2}     # plane 16
205171ae1a4SAndy Whitcroft}x;
206171ae1a4SAndy Whitcroft
2078ed22cadSAndy Whitcroftour $typeTypedefs = qr{(?x:
208fb9e9096SAndy Whitcroft	(?:__)?(?:u|s|be|le)(?:8|16|32|64)|
2098ed22cadSAndy Whitcroft	atomic_t
2108ed22cadSAndy Whitcroft)};
2118ed22cadSAndy Whitcroft
212691e669bSJoe Perchesour $logFunctions = qr{(?x:
213691e669bSJoe Perches	printk|
214b0531722SJoe Perches	[a-z]+_(emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)|
215691e669bSJoe Perches	WARN|
216b0531722SJoe Perches	panic|
217b0531722SJoe Perches	MODULE_[A-Z_]+
218691e669bSJoe Perches)};
219691e669bSJoe Perches
220*20112475SJoe Perchesour $signature_tags = qr{(?xi:
221*20112475SJoe Perches	Signed-off-by:|
222*20112475SJoe Perches	Acked-by:|
223*20112475SJoe Perches	Tested-by:|
224*20112475SJoe Perches	Reviewed-by:|
225*20112475SJoe Perches	Reported-by:|
226*20112475SJoe Perches	To:|
227*20112475SJoe Perches	Cc:
228*20112475SJoe Perches)};
229*20112475SJoe Perches
2308905a67cSAndy Whitcroftour @typeList = (
2318905a67cSAndy Whitcroft	qr{void},
232c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?char},
233c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?short},
234c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?int},
235c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?long},
236c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?long\s+int},
237c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?long\s+long},
238c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?long\s+long\s+int},
2398905a67cSAndy Whitcroft	qr{unsigned},
2408905a67cSAndy Whitcroft	qr{float},
2418905a67cSAndy Whitcroft	qr{double},
2428905a67cSAndy Whitcroft	qr{bool},
2438905a67cSAndy Whitcroft	qr{struct\s+$Ident},
2448905a67cSAndy Whitcroft	qr{union\s+$Ident},
2458905a67cSAndy Whitcroft	qr{enum\s+$Ident},
2468905a67cSAndy Whitcroft	qr{${Ident}_t},
2478905a67cSAndy Whitcroft	qr{${Ident}_handler},
2488905a67cSAndy Whitcroft	qr{${Ident}_handler_fn},
2498905a67cSAndy Whitcroft);
250c45dcabdSAndy Whitcroftour @modifierList = (
251c45dcabdSAndy Whitcroft	qr{fastcall},
252c45dcabdSAndy Whitcroft);
2538905a67cSAndy Whitcroft
2547840a94cSWolfram Sangour $allowed_asm_includes = qr{(?x:
2557840a94cSWolfram Sang	irq|
2567840a94cSWolfram Sang	memory
2577840a94cSWolfram Sang)};
2587840a94cSWolfram Sang# memory.h: ARM has a custom one
2597840a94cSWolfram Sang
2608905a67cSAndy Whitcroftsub build_types {
261d2172eb5SAndy Whitcroft	my $mods = "(?x:  \n" . join("|\n  ", @modifierList) . "\n)";
262d2172eb5SAndy Whitcroft	my $all = "(?x:  \n" . join("|\n  ", @typeList) . "\n)";
263c8cb2ca3SAndy Whitcroft	$Modifier	= qr{(?:$Attribute|$Sparse|$mods)};
2648905a67cSAndy Whitcroft	$NonptrType	= qr{
265d2172eb5SAndy Whitcroft			(?:$Modifier\s+|const\s+)*
266cf655043SAndy Whitcroft			(?:
267c45dcabdSAndy Whitcroft				(?:typeof|__typeof__)\s*\(\s*\**\s*$Ident\s*\)|
2688ed22cadSAndy Whitcroft				(?:$typeTypedefs\b)|
269c45dcabdSAndy Whitcroft				(?:${all}\b)
270cf655043SAndy Whitcroft			)
271c8cb2ca3SAndy Whitcroft			(?:\s+$Modifier|\s+const)*
2728905a67cSAndy Whitcroft		  }x;
2738905a67cSAndy Whitcroft	$Type	= qr{
274c45dcabdSAndy Whitcroft			$NonptrType
27565863862SAndy Whitcroft			(?:[\s\*]+\s*const|[\s\*]+|(?:\s*\[\s*\])+)?
276c8cb2ca3SAndy Whitcroft			(?:\s+$Inline|\s+$Modifier)*
2778905a67cSAndy Whitcroft		  }x;
2788905a67cSAndy Whitcroft	$Declare	= qr{(?:$Storage\s+)?$Type};
2798905a67cSAndy Whitcroft}
2808905a67cSAndy Whitcroftbuild_types();
2816c72ffaaSAndy Whitcroft
2827d2367afSJoe Perchesour $match_balanced_parentheses = qr/(\((?:[^\(\)]+|(-1))*\))/;
2837d2367afSJoe Perches
2847d2367afSJoe Perchesour $Typecast	= qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
2857d2367afSJoe Perchesour $LvalOrFunc	= qr{($Lval)\s*($match_balanced_parentheses{0,1})\s*};
2867d2367afSJoe Perches
2877d2367afSJoe Perchessub deparenthesize {
2887d2367afSJoe Perches	my ($string) = @_;
2897d2367afSJoe Perches	return "" if (!defined($string));
2907d2367afSJoe Perches	$string =~ s@^\s*\(\s*@@g;
2917d2367afSJoe Perches	$string =~ s@\s*\)\s*$@@g;
2927d2367afSJoe Perches	$string =~ s@\s+@ @g;
2937d2367afSJoe Perches	return $string;
2947d2367afSJoe Perches}
2957d2367afSJoe Perches
2966c72ffaaSAndy Whitcroft$chk_signoff = 0 if ($file);
2970a920b5bSAndy Whitcroft
2984a0df2efSAndy Whitcroftmy @dep_includes = ();
2994a0df2efSAndy Whitcroftmy @dep_functions = ();
3006c72ffaaSAndy Whitcroftmy $removal = "Documentation/feature-removal-schedule.txt";
3016c72ffaaSAndy Whitcroftif ($tree && -f "$root/$removal") {
30221caa13cSAndy Whitcroft	open(my $REMOVE, '<', "$root/$removal") ||
3036c72ffaaSAndy Whitcroft				die "$P: $removal: open failed - $!\n";
30421caa13cSAndy Whitcroft	while (<$REMOVE>) {
305f0a594c1SAndy Whitcroft		if (/^Check:\s+(.*\S)/) {
306f0a594c1SAndy Whitcroft			for my $entry (split(/[, ]+/, $1)) {
307f0a594c1SAndy Whitcroft				if ($entry =~ m@include/(.*)@) {
3084a0df2efSAndy Whitcroft					push(@dep_includes, $1);
3094a0df2efSAndy Whitcroft
310f0a594c1SAndy Whitcroft				} elsif ($entry !~ m@/@) {
311f0a594c1SAndy Whitcroft					push(@dep_functions, $entry);
312f0a594c1SAndy Whitcroft				}
3134a0df2efSAndy Whitcroft			}
3140a920b5bSAndy Whitcroft		}
3150a920b5bSAndy Whitcroft	}
31621caa13cSAndy Whitcroft	close($REMOVE);
3170a920b5bSAndy Whitcroft}
3180a920b5bSAndy Whitcroft
31900df344fSAndy Whitcroftmy @rawlines = ();
320c2fdda0dSAndy Whitcroftmy @lines = ();
321c2fdda0dSAndy Whitcroftmy $vname;
3226c72ffaaSAndy Whitcroftfor my $filename (@ARGV) {
32321caa13cSAndy Whitcroft	my $FILE;
3246c72ffaaSAndy Whitcroft	if ($file) {
32521caa13cSAndy Whitcroft		open($FILE, '-|', "diff -u /dev/null $filename") ||
3266c72ffaaSAndy Whitcroft			die "$P: $filename: diff failed - $!\n";
32721caa13cSAndy Whitcroft	} elsif ($filename eq '-') {
32821caa13cSAndy Whitcroft		open($FILE, '<&STDIN');
3296c72ffaaSAndy Whitcroft	} else {
33021caa13cSAndy Whitcroft		open($FILE, '<', "$filename") ||
3316c72ffaaSAndy Whitcroft			die "$P: $filename: open failed - $!\n";
3326c72ffaaSAndy Whitcroft	}
333c2fdda0dSAndy Whitcroft	if ($filename eq '-') {
334c2fdda0dSAndy Whitcroft		$vname = 'Your patch';
335c2fdda0dSAndy Whitcroft	} else {
336c2fdda0dSAndy Whitcroft		$vname = $filename;
337c2fdda0dSAndy Whitcroft	}
33821caa13cSAndy Whitcroft	while (<$FILE>) {
3390a920b5bSAndy Whitcroft		chomp;
34000df344fSAndy Whitcroft		push(@rawlines, $_);
3416c72ffaaSAndy Whitcroft	}
34221caa13cSAndy Whitcroft	close($FILE);
343c2fdda0dSAndy Whitcroft	if (!process($filename)) {
3440a920b5bSAndy Whitcroft		$exit = 1;
3450a920b5bSAndy Whitcroft	}
34600df344fSAndy Whitcroft	@rawlines = ();
34713214adfSAndy Whitcroft	@lines = ();
3480a920b5bSAndy Whitcroft}
3490a920b5bSAndy Whitcroft
3500a920b5bSAndy Whitcroftexit($exit);
3510a920b5bSAndy Whitcroft
3520a920b5bSAndy Whitcroftsub top_of_kernel_tree {
3536c72ffaaSAndy Whitcroft	my ($root) = @_;
3546c72ffaaSAndy Whitcroft
3556c72ffaaSAndy Whitcroft	my @tree_check = (
3566c72ffaaSAndy Whitcroft		"COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
3576c72ffaaSAndy Whitcroft		"README", "Documentation", "arch", "include", "drivers",
3586c72ffaaSAndy Whitcroft		"fs", "init", "ipc", "kernel", "lib", "scripts",
3596c72ffaaSAndy Whitcroft	);
3606c72ffaaSAndy Whitcroft
3616c72ffaaSAndy Whitcroft	foreach my $check (@tree_check) {
3626c72ffaaSAndy Whitcroft		if (! -e $root . '/' . $check) {
3630a920b5bSAndy Whitcroft			return 0;
3640a920b5bSAndy Whitcroft		}
3656c72ffaaSAndy Whitcroft	}
3666c72ffaaSAndy Whitcroft	return 1;
3676c72ffaaSAndy Whitcroft}
3680a920b5bSAndy Whitcroft
369*20112475SJoe Perchessub parse_email {
370*20112475SJoe Perches	my ($formatted_email) = @_;
371*20112475SJoe Perches
372*20112475SJoe Perches	my $name = "";
373*20112475SJoe Perches	my $address = "";
374*20112475SJoe Perches	my $comment = "";
375*20112475SJoe Perches
376*20112475SJoe Perches	if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
377*20112475SJoe Perches		$name = $1;
378*20112475SJoe Perches		$address = $2;
379*20112475SJoe Perches		$comment = $3 if defined $3;
380*20112475SJoe Perches	} elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
381*20112475SJoe Perches		$address = $1;
382*20112475SJoe Perches		$comment = $2 if defined $2;
383*20112475SJoe Perches	} elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
384*20112475SJoe Perches		$address = $1;
385*20112475SJoe Perches		$comment = $2 if defined $2;
386*20112475SJoe Perches		$formatted_email =~ s/$address.*$//;
387*20112475SJoe Perches		$name = $formatted_email;
388*20112475SJoe Perches		$name =~ s/^\s+|\s+$//g;
389*20112475SJoe Perches		$name =~ s/^\"|\"$//g;
390*20112475SJoe Perches		# If there's a name left after stripping spaces and
391*20112475SJoe Perches		# leading quotes, and the address doesn't have both
392*20112475SJoe Perches		# leading and trailing angle brackets, the address
393*20112475SJoe Perches		# is invalid. ie:
394*20112475SJoe Perches		#   "joe smith [email protected]" bad
395*20112475SJoe Perches		#   "joe smith <[email protected]" bad
396*20112475SJoe Perches		if ($name ne "" && $address !~ /^<[^>]+>$/) {
397*20112475SJoe Perches			$name = "";
398*20112475SJoe Perches			$address = "";
399*20112475SJoe Perches			$comment = "";
400*20112475SJoe Perches		}
401*20112475SJoe Perches	}
402*20112475SJoe Perches
403*20112475SJoe Perches	$name =~ s/^\s+|\s+$//g;
404*20112475SJoe Perches	$name =~ s/^\"|\"$//g;
405*20112475SJoe Perches	$address =~ s/^\s+|\s+$//g;
406*20112475SJoe Perches	$address =~ s/^\<|\>$//g;
407*20112475SJoe Perches
408*20112475SJoe Perches	if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
409*20112475SJoe Perches		$name =~ s/(?<!\\)"/\\"/g; ##escape quotes
410*20112475SJoe Perches		$name = "\"$name\"";
411*20112475SJoe Perches	}
412*20112475SJoe Perches
413*20112475SJoe Perches	return ($name, $address, $comment);
414*20112475SJoe Perches}
415*20112475SJoe Perches
416*20112475SJoe Perchessub format_email {
417*20112475SJoe Perches	my ($name, $address) = @_;
418*20112475SJoe Perches
419*20112475SJoe Perches	my $formatted_email;
420*20112475SJoe Perches
421*20112475SJoe Perches	$name =~ s/^\s+|\s+$//g;
422*20112475SJoe Perches	$name =~ s/^\"|\"$//g;
423*20112475SJoe Perches	$address =~ s/^\s+|\s+$//g;
424*20112475SJoe Perches
425*20112475SJoe Perches	if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
426*20112475SJoe Perches		$name =~ s/(?<!\\)"/\\"/g; ##escape quotes
427*20112475SJoe Perches		$name = "\"$name\"";
428*20112475SJoe Perches	}
429*20112475SJoe Perches
430*20112475SJoe Perches	if ("$name" eq "") {
431*20112475SJoe Perches		$formatted_email = "$address";
432*20112475SJoe Perches	} else {
433*20112475SJoe Perches		$formatted_email = "$name <$address>";
434*20112475SJoe Perches	}
435*20112475SJoe Perches
436*20112475SJoe Perches	return $formatted_email;
437*20112475SJoe Perches}
438*20112475SJoe Perches
4390a920b5bSAndy Whitcroftsub expand_tabs {
4400a920b5bSAndy Whitcroft	my ($str) = @_;
4410a920b5bSAndy Whitcroft
4420a920b5bSAndy Whitcroft	my $res = '';
4430a920b5bSAndy Whitcroft	my $n = 0;
4440a920b5bSAndy Whitcroft	for my $c (split(//, $str)) {
4450a920b5bSAndy Whitcroft		if ($c eq "\t") {
4460a920b5bSAndy Whitcroft			$res .= ' ';
4470a920b5bSAndy Whitcroft			$n++;
4480a920b5bSAndy Whitcroft			for (; ($n % 8) != 0; $n++) {
4490a920b5bSAndy Whitcroft				$res .= ' ';
4500a920b5bSAndy Whitcroft			}
4510a920b5bSAndy Whitcroft			next;
4520a920b5bSAndy Whitcroft		}
4530a920b5bSAndy Whitcroft		$res .= $c;
4540a920b5bSAndy Whitcroft		$n++;
4550a920b5bSAndy Whitcroft	}
4560a920b5bSAndy Whitcroft
4570a920b5bSAndy Whitcroft	return $res;
4580a920b5bSAndy Whitcroft}
4596c72ffaaSAndy Whitcroftsub copy_spacing {
460773647a0SAndy Whitcroft	(my $res = shift) =~ tr/\t/ /c;
4616c72ffaaSAndy Whitcroft	return $res;
4626c72ffaaSAndy Whitcroft}
4630a920b5bSAndy Whitcroft
4644a0df2efSAndy Whitcroftsub line_stats {
4654a0df2efSAndy Whitcroft	my ($line) = @_;
4664a0df2efSAndy Whitcroft
4674a0df2efSAndy Whitcroft	# Drop the diff line leader and expand tabs
4684a0df2efSAndy Whitcroft	$line =~ s/^.//;
4694a0df2efSAndy Whitcroft	$line = expand_tabs($line);
4704a0df2efSAndy Whitcroft
4714a0df2efSAndy Whitcroft	# Pick the indent from the front of the line.
4724a0df2efSAndy Whitcroft	my ($white) = ($line =~ /^(\s*)/);
4734a0df2efSAndy Whitcroft
4744a0df2efSAndy Whitcroft	return (length($line), length($white));
4754a0df2efSAndy Whitcroft}
4764a0df2efSAndy Whitcroft
477773647a0SAndy Whitcroftmy $sanitise_quote = '';
478773647a0SAndy Whitcroft
479773647a0SAndy Whitcroftsub sanitise_line_reset {
480773647a0SAndy Whitcroft	my ($in_comment) = @_;
481773647a0SAndy Whitcroft
482773647a0SAndy Whitcroft	if ($in_comment) {
483773647a0SAndy Whitcroft		$sanitise_quote = '*/';
484773647a0SAndy Whitcroft	} else {
485773647a0SAndy Whitcroft		$sanitise_quote = '';
486773647a0SAndy Whitcroft	}
487773647a0SAndy Whitcroft}
48800df344fSAndy Whitcroftsub sanitise_line {
48900df344fSAndy Whitcroft	my ($line) = @_;
49000df344fSAndy Whitcroft
49100df344fSAndy Whitcroft	my $res = '';
49200df344fSAndy Whitcroft	my $l = '';
49300df344fSAndy Whitcroft
494c2fdda0dSAndy Whitcroft	my $qlen = 0;
495773647a0SAndy Whitcroft	my $off = 0;
496773647a0SAndy Whitcroft	my $c;
49700df344fSAndy Whitcroft
498773647a0SAndy Whitcroft	# Always copy over the diff marker.
499773647a0SAndy Whitcroft	$res = substr($line, 0, 1);
500773647a0SAndy Whitcroft
501773647a0SAndy Whitcroft	for ($off = 1; $off < length($line); $off++) {
502773647a0SAndy Whitcroft		$c = substr($line, $off, 1);
503773647a0SAndy Whitcroft
504773647a0SAndy Whitcroft		# Comments we are wacking completly including the begin
505773647a0SAndy Whitcroft		# and end, all to $;.
506773647a0SAndy Whitcroft		if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
507773647a0SAndy Whitcroft			$sanitise_quote = '*/';
508773647a0SAndy Whitcroft
509773647a0SAndy Whitcroft			substr($res, $off, 2, "$;$;");
510773647a0SAndy Whitcroft			$off++;
51100df344fSAndy Whitcroft			next;
512773647a0SAndy Whitcroft		}
51381bc0e02SAndy Whitcroft		if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
514773647a0SAndy Whitcroft			$sanitise_quote = '';
515773647a0SAndy Whitcroft			substr($res, $off, 2, "$;$;");
516773647a0SAndy Whitcroft			$off++;
517773647a0SAndy Whitcroft			next;
518773647a0SAndy Whitcroft		}
519113f04a8SDaniel Walker		if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
520113f04a8SDaniel Walker			$sanitise_quote = '//';
521113f04a8SDaniel Walker
522113f04a8SDaniel Walker			substr($res, $off, 2, $sanitise_quote);
523113f04a8SDaniel Walker			$off++;
524113f04a8SDaniel Walker			next;
525113f04a8SDaniel Walker		}
526773647a0SAndy Whitcroft
527773647a0SAndy Whitcroft		# A \ in a string means ignore the next character.
528773647a0SAndy Whitcroft		if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
529773647a0SAndy Whitcroft		    $c eq "\\") {
530773647a0SAndy Whitcroft			substr($res, $off, 2, 'XX');
531773647a0SAndy Whitcroft			$off++;
532773647a0SAndy Whitcroft			next;
533773647a0SAndy Whitcroft		}
534773647a0SAndy Whitcroft		# Regular quotes.
535773647a0SAndy Whitcroft		if ($c eq "'" || $c eq '"') {
536773647a0SAndy Whitcroft			if ($sanitise_quote eq '') {
537773647a0SAndy Whitcroft				$sanitise_quote = $c;
538773647a0SAndy Whitcroft
539773647a0SAndy Whitcroft				substr($res, $off, 1, $c);
540773647a0SAndy Whitcroft				next;
541773647a0SAndy Whitcroft			} elsif ($sanitise_quote eq $c) {
542773647a0SAndy Whitcroft				$sanitise_quote = '';
54300df344fSAndy Whitcroft			}
54400df344fSAndy Whitcroft		}
545773647a0SAndy Whitcroft
546fae17daeSAndy Whitcroft		#print "c<$c> SQ<$sanitise_quote>\n";
547773647a0SAndy Whitcroft		if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
548773647a0SAndy Whitcroft			substr($res, $off, 1, $;);
549113f04a8SDaniel Walker		} elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
550113f04a8SDaniel Walker			substr($res, $off, 1, $;);
551773647a0SAndy Whitcroft		} elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
552773647a0SAndy Whitcroft			substr($res, $off, 1, 'X');
55300df344fSAndy Whitcroft		} else {
554773647a0SAndy Whitcroft			substr($res, $off, 1, $c);
55500df344fSAndy Whitcroft		}
556c2fdda0dSAndy Whitcroft	}
557c2fdda0dSAndy Whitcroft
558113f04a8SDaniel Walker	if ($sanitise_quote eq '//') {
559113f04a8SDaniel Walker		$sanitise_quote = '';
560113f04a8SDaniel Walker	}
561113f04a8SDaniel Walker
562c2fdda0dSAndy Whitcroft	# The pathname on a #include may be surrounded by '<' and '>'.
563c45dcabdSAndy Whitcroft	if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
564c2fdda0dSAndy Whitcroft		my $clean = 'X' x length($1);
565c2fdda0dSAndy Whitcroft		$res =~ s@\<.*\>@<$clean>@;
566c2fdda0dSAndy Whitcroft
567c2fdda0dSAndy Whitcroft	# The whole of a #error is a string.
568c45dcabdSAndy Whitcroft	} elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
569c2fdda0dSAndy Whitcroft		my $clean = 'X' x length($1);
570c45dcabdSAndy Whitcroft		$res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
571c2fdda0dSAndy Whitcroft	}
572c2fdda0dSAndy Whitcroft
57300df344fSAndy Whitcroft	return $res;
57400df344fSAndy Whitcroft}
57500df344fSAndy Whitcroft
5768905a67cSAndy Whitcroftsub ctx_statement_block {
5778905a67cSAndy Whitcroft	my ($linenr, $remain, $off) = @_;
5788905a67cSAndy Whitcroft	my $line = $linenr - 1;
5798905a67cSAndy Whitcroft	my $blk = '';
5808905a67cSAndy Whitcroft	my $soff = $off;
5818905a67cSAndy Whitcroft	my $coff = $off - 1;
582773647a0SAndy Whitcroft	my $coff_set = 0;
5838905a67cSAndy Whitcroft
58413214adfSAndy Whitcroft	my $loff = 0;
58513214adfSAndy Whitcroft
5868905a67cSAndy Whitcroft	my $type = '';
5878905a67cSAndy Whitcroft	my $level = 0;
588a2750645SAndy Whitcroft	my @stack = ();
589cf655043SAndy Whitcroft	my $p;
5908905a67cSAndy Whitcroft	my $c;
5918905a67cSAndy Whitcroft	my $len = 0;
59213214adfSAndy Whitcroft
59313214adfSAndy Whitcroft	my $remainder;
5948905a67cSAndy Whitcroft	while (1) {
595a2750645SAndy Whitcroft		@stack = (['', 0]) if ($#stack == -1);
596a2750645SAndy Whitcroft
597773647a0SAndy Whitcroft		#warn "CSB: blk<$blk> remain<$remain>\n";
5988905a67cSAndy Whitcroft		# If we are about to drop off the end, pull in more
5998905a67cSAndy Whitcroft		# context.
6008905a67cSAndy Whitcroft		if ($off >= $len) {
6018905a67cSAndy Whitcroft			for (; $remain > 0; $line++) {
602dea33496SAndy Whitcroft				last if (!defined $lines[$line]);
603c2fdda0dSAndy Whitcroft				next if ($lines[$line] =~ /^-/);
6048905a67cSAndy Whitcroft				$remain--;
60513214adfSAndy Whitcroft				$loff = $len;
606c2fdda0dSAndy Whitcroft				$blk .= $lines[$line] . "\n";
6078905a67cSAndy Whitcroft				$len = length($blk);
6088905a67cSAndy Whitcroft				$line++;
6098905a67cSAndy Whitcroft				last;
6108905a67cSAndy Whitcroft			}
6118905a67cSAndy Whitcroft			# Bail if there is no further context.
6128905a67cSAndy Whitcroft			#warn "CSB: blk<$blk> off<$off> len<$len>\n";
61313214adfSAndy Whitcroft			if ($off >= $len) {
6148905a67cSAndy Whitcroft				last;
6158905a67cSAndy Whitcroft			}
6168905a67cSAndy Whitcroft		}
617cf655043SAndy Whitcroft		$p = $c;
6188905a67cSAndy Whitcroft		$c = substr($blk, $off, 1);
61913214adfSAndy Whitcroft		$remainder = substr($blk, $off);
6208905a67cSAndy Whitcroft
621773647a0SAndy Whitcroft		#warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
6224635f4fbSAndy Whitcroft
6234635f4fbSAndy Whitcroft		# Handle nested #if/#else.
6244635f4fbSAndy Whitcroft		if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
6254635f4fbSAndy Whitcroft			push(@stack, [ $type, $level ]);
6264635f4fbSAndy Whitcroft		} elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
6274635f4fbSAndy Whitcroft			($type, $level) = @{$stack[$#stack - 1]};
6284635f4fbSAndy Whitcroft		} elsif ($remainder =~ /^#\s*endif\b/) {
6294635f4fbSAndy Whitcroft			($type, $level) = @{pop(@stack)};
6304635f4fbSAndy Whitcroft		}
6314635f4fbSAndy Whitcroft
6328905a67cSAndy Whitcroft		# Statement ends at the ';' or a close '}' at the
6338905a67cSAndy Whitcroft		# outermost level.
6348905a67cSAndy Whitcroft		if ($level == 0 && $c eq ';') {
6358905a67cSAndy Whitcroft			last;
6368905a67cSAndy Whitcroft		}
6378905a67cSAndy Whitcroft
63813214adfSAndy Whitcroft		# An else is really a conditional as long as its not else if
639773647a0SAndy Whitcroft		if ($level == 0 && $coff_set == 0 &&
640773647a0SAndy Whitcroft				(!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
641773647a0SAndy Whitcroft				$remainder =~ /^(else)(?:\s|{)/ &&
642773647a0SAndy Whitcroft				$remainder !~ /^else\s+if\b/) {
643773647a0SAndy Whitcroft			$coff = $off + length($1) - 1;
644773647a0SAndy Whitcroft			$coff_set = 1;
645773647a0SAndy Whitcroft			#warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
646773647a0SAndy Whitcroft			#warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
64713214adfSAndy Whitcroft		}
64813214adfSAndy Whitcroft
6498905a67cSAndy Whitcroft		if (($type eq '' || $type eq '(') && $c eq '(') {
6508905a67cSAndy Whitcroft			$level++;
6518905a67cSAndy Whitcroft			$type = '(';
6528905a67cSAndy Whitcroft		}
6538905a67cSAndy Whitcroft		if ($type eq '(' && $c eq ')') {
6548905a67cSAndy Whitcroft			$level--;
6558905a67cSAndy Whitcroft			$type = ($level != 0)? '(' : '';
6568905a67cSAndy Whitcroft
6578905a67cSAndy Whitcroft			if ($level == 0 && $coff < $soff) {
6588905a67cSAndy Whitcroft				$coff = $off;
659773647a0SAndy Whitcroft				$coff_set = 1;
660773647a0SAndy Whitcroft				#warn "CSB: mark coff<$coff>\n";
6618905a67cSAndy Whitcroft			}
6628905a67cSAndy Whitcroft		}
6638905a67cSAndy Whitcroft		if (($type eq '' || $type eq '{') && $c eq '{') {
6648905a67cSAndy Whitcroft			$level++;
6658905a67cSAndy Whitcroft			$type = '{';
6668905a67cSAndy Whitcroft		}
6678905a67cSAndy Whitcroft		if ($type eq '{' && $c eq '}') {
6688905a67cSAndy Whitcroft			$level--;
6698905a67cSAndy Whitcroft			$type = ($level != 0)? '{' : '';
6708905a67cSAndy Whitcroft
6718905a67cSAndy Whitcroft			if ($level == 0) {
672b998e001SPatrick Pannuto				if (substr($blk, $off + 1, 1) eq ';') {
673b998e001SPatrick Pannuto					$off++;
674b998e001SPatrick Pannuto				}
6758905a67cSAndy Whitcroft				last;
6768905a67cSAndy Whitcroft			}
6778905a67cSAndy Whitcroft		}
6788905a67cSAndy Whitcroft		$off++;
6798905a67cSAndy Whitcroft	}
680a3bb97a7SAndy Whitcroft	# We are truly at the end, so shuffle to the next line.
68113214adfSAndy Whitcroft	if ($off == $len) {
682a3bb97a7SAndy Whitcroft		$loff = $len + 1;
68313214adfSAndy Whitcroft		$line++;
68413214adfSAndy Whitcroft		$remain--;
68513214adfSAndy Whitcroft	}
6868905a67cSAndy Whitcroft
6878905a67cSAndy Whitcroft	my $statement = substr($blk, $soff, $off - $soff + 1);
6888905a67cSAndy Whitcroft	my $condition = substr($blk, $soff, $coff - $soff + 1);
6898905a67cSAndy Whitcroft
6908905a67cSAndy Whitcroft	#warn "STATEMENT<$statement>\n";
6918905a67cSAndy Whitcroft	#warn "CONDITION<$condition>\n";
6928905a67cSAndy Whitcroft
693773647a0SAndy Whitcroft	#print "coff<$coff> soff<$off> loff<$loff>\n";
69413214adfSAndy Whitcroft
69513214adfSAndy Whitcroft	return ($statement, $condition,
69613214adfSAndy Whitcroft			$line, $remain + 1, $off - $loff + 1, $level);
69713214adfSAndy Whitcroft}
69813214adfSAndy Whitcroft
699cf655043SAndy Whitcroftsub statement_lines {
700cf655043SAndy Whitcroft	my ($stmt) = @_;
701cf655043SAndy Whitcroft
702cf655043SAndy Whitcroft	# Strip the diff line prefixes and rip blank lines at start and end.
703cf655043SAndy Whitcroft	$stmt =~ s/(^|\n)./$1/g;
704cf655043SAndy Whitcroft	$stmt =~ s/^\s*//;
705cf655043SAndy Whitcroft	$stmt =~ s/\s*$//;
706cf655043SAndy Whitcroft
707cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
708cf655043SAndy Whitcroft
709cf655043SAndy Whitcroft	return $#stmt_lines + 2;
710cf655043SAndy Whitcroft}
711cf655043SAndy Whitcroft
712cf655043SAndy Whitcroftsub statement_rawlines {
713cf655043SAndy Whitcroft	my ($stmt) = @_;
714cf655043SAndy Whitcroft
715cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
716cf655043SAndy Whitcroft
717cf655043SAndy Whitcroft	return $#stmt_lines + 2;
718cf655043SAndy Whitcroft}
719cf655043SAndy Whitcroft
720cf655043SAndy Whitcroftsub statement_block_size {
721cf655043SAndy Whitcroft	my ($stmt) = @_;
722cf655043SAndy Whitcroft
723cf655043SAndy Whitcroft	$stmt =~ s/(^|\n)./$1/g;
724cf655043SAndy Whitcroft	$stmt =~ s/^\s*{//;
725cf655043SAndy Whitcroft	$stmt =~ s/}\s*$//;
726cf655043SAndy Whitcroft	$stmt =~ s/^\s*//;
727cf655043SAndy Whitcroft	$stmt =~ s/\s*$//;
728cf655043SAndy Whitcroft
729cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
730cf655043SAndy Whitcroft	my @stmt_statements = ($stmt =~ /;/g);
731cf655043SAndy Whitcroft
732cf655043SAndy Whitcroft	my $stmt_lines = $#stmt_lines + 2;
733cf655043SAndy Whitcroft	my $stmt_statements = $#stmt_statements + 1;
734cf655043SAndy Whitcroft
735cf655043SAndy Whitcroft	if ($stmt_lines > $stmt_statements) {
736cf655043SAndy Whitcroft		return $stmt_lines;
737cf655043SAndy Whitcroft	} else {
738cf655043SAndy Whitcroft		return $stmt_statements;
739cf655043SAndy Whitcroft	}
740cf655043SAndy Whitcroft}
741cf655043SAndy Whitcroft
74213214adfSAndy Whitcroftsub ctx_statement_full {
74313214adfSAndy Whitcroft	my ($linenr, $remain, $off) = @_;
74413214adfSAndy Whitcroft	my ($statement, $condition, $level);
74513214adfSAndy Whitcroft
74613214adfSAndy Whitcroft	my (@chunks);
74713214adfSAndy Whitcroft
748cf655043SAndy Whitcroft	# Grab the first conditional/block pair.
74913214adfSAndy Whitcroft	($statement, $condition, $linenr, $remain, $off, $level) =
75013214adfSAndy Whitcroft				ctx_statement_block($linenr, $remain, $off);
751773647a0SAndy Whitcroft	#print "F: c<$condition> s<$statement> remain<$remain>\n";
75213214adfSAndy Whitcroft	push(@chunks, [ $condition, $statement ]);
753cf655043SAndy Whitcroft	if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
754cf655043SAndy Whitcroft		return ($level, $linenr, @chunks);
755cf655043SAndy Whitcroft	}
756cf655043SAndy Whitcroft
757cf655043SAndy Whitcroft	# Pull in the following conditional/block pairs and see if they
758cf655043SAndy Whitcroft	# could continue the statement.
759cf655043SAndy Whitcroft	for (;;) {
76013214adfSAndy Whitcroft		($statement, $condition, $linenr, $remain, $off, $level) =
76113214adfSAndy Whitcroft				ctx_statement_block($linenr, $remain, $off);
762cf655043SAndy Whitcroft		#print "C: c<$condition> s<$statement> remain<$remain>\n";
763773647a0SAndy Whitcroft		last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
764cf655043SAndy Whitcroft		#print "C: push\n";
765cf655043SAndy Whitcroft		push(@chunks, [ $condition, $statement ]);
76613214adfSAndy Whitcroft	}
76713214adfSAndy Whitcroft
76813214adfSAndy Whitcroft	return ($level, $linenr, @chunks);
7698905a67cSAndy Whitcroft}
7708905a67cSAndy Whitcroft
7714a0df2efSAndy Whitcroftsub ctx_block_get {
772f0a594c1SAndy Whitcroft	my ($linenr, $remain, $outer, $open, $close, $off) = @_;
7734a0df2efSAndy Whitcroft	my $line;
7744a0df2efSAndy Whitcroft	my $start = $linenr - 1;
7754a0df2efSAndy Whitcroft	my $blk = '';
7764a0df2efSAndy Whitcroft	my @o;
7774a0df2efSAndy Whitcroft	my @c;
7784a0df2efSAndy Whitcroft	my @res = ();
7794a0df2efSAndy Whitcroft
780f0a594c1SAndy Whitcroft	my $level = 0;
7814635f4fbSAndy Whitcroft	my @stack = ($level);
78200df344fSAndy Whitcroft	for ($line = $start; $remain > 0; $line++) {
78300df344fSAndy Whitcroft		next if ($rawlines[$line] =~ /^-/);
78400df344fSAndy Whitcroft		$remain--;
78500df344fSAndy Whitcroft
78600df344fSAndy Whitcroft		$blk .= $rawlines[$line];
7874635f4fbSAndy Whitcroft
7884635f4fbSAndy Whitcroft		# Handle nested #if/#else.
78901464f30SAndy Whitcroft		if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
7904635f4fbSAndy Whitcroft			push(@stack, $level);
79101464f30SAndy Whitcroft		} elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
7924635f4fbSAndy Whitcroft			$level = $stack[$#stack - 1];
79301464f30SAndy Whitcroft		} elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
7944635f4fbSAndy Whitcroft			$level = pop(@stack);
7954635f4fbSAndy Whitcroft		}
7964635f4fbSAndy Whitcroft
79701464f30SAndy Whitcroft		foreach my $c (split(//, $lines[$line])) {
798f0a594c1SAndy Whitcroft			##print "C<$c>L<$level><$open$close>O<$off>\n";
799f0a594c1SAndy Whitcroft			if ($off > 0) {
800f0a594c1SAndy Whitcroft				$off--;
801f0a594c1SAndy Whitcroft				next;
802f0a594c1SAndy Whitcroft			}
8034a0df2efSAndy Whitcroft
804f0a594c1SAndy Whitcroft			if ($c eq $close && $level > 0) {
805f0a594c1SAndy Whitcroft				$level--;
806f0a594c1SAndy Whitcroft				last if ($level == 0);
807f0a594c1SAndy Whitcroft			} elsif ($c eq $open) {
808f0a594c1SAndy Whitcroft				$level++;
809f0a594c1SAndy Whitcroft			}
810f0a594c1SAndy Whitcroft		}
8114a0df2efSAndy Whitcroft
812f0a594c1SAndy Whitcroft		if (!$outer || $level <= 1) {
81300df344fSAndy Whitcroft			push(@res, $rawlines[$line]);
8144a0df2efSAndy Whitcroft		}
8154a0df2efSAndy Whitcroft
816f0a594c1SAndy Whitcroft		last if ($level == 0);
8174a0df2efSAndy Whitcroft	}
8184a0df2efSAndy Whitcroft
819f0a594c1SAndy Whitcroft	return ($level, @res);
8204a0df2efSAndy Whitcroft}
8214a0df2efSAndy Whitcroftsub ctx_block_outer {
8224a0df2efSAndy Whitcroft	my ($linenr, $remain) = @_;
8234a0df2efSAndy Whitcroft
824f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
825f0a594c1SAndy Whitcroft	return @r;
8264a0df2efSAndy Whitcroft}
8274a0df2efSAndy Whitcroftsub ctx_block {
8284a0df2efSAndy Whitcroft	my ($linenr, $remain) = @_;
8294a0df2efSAndy Whitcroft
830f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
831f0a594c1SAndy Whitcroft	return @r;
832653d4876SAndy Whitcroft}
833653d4876SAndy Whitcroftsub ctx_statement {
834f0a594c1SAndy Whitcroft	my ($linenr, $remain, $off) = @_;
835f0a594c1SAndy Whitcroft
836f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
837f0a594c1SAndy Whitcroft	return @r;
838f0a594c1SAndy Whitcroft}
839f0a594c1SAndy Whitcroftsub ctx_block_level {
840653d4876SAndy Whitcroft	my ($linenr, $remain) = @_;
841653d4876SAndy Whitcroft
842f0a594c1SAndy Whitcroft	return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
8434a0df2efSAndy Whitcroft}
8449c0ca6f9SAndy Whitcroftsub ctx_statement_level {
8459c0ca6f9SAndy Whitcroft	my ($linenr, $remain, $off) = @_;
8469c0ca6f9SAndy Whitcroft
8479c0ca6f9SAndy Whitcroft	return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
8489c0ca6f9SAndy Whitcroft}
8494a0df2efSAndy Whitcroft
8504a0df2efSAndy Whitcroftsub ctx_locate_comment {
8514a0df2efSAndy Whitcroft	my ($first_line, $end_line) = @_;
8524a0df2efSAndy Whitcroft
8534a0df2efSAndy Whitcroft	# Catch a comment on the end of the line itself.
854beae6332SAndy Whitcroft	my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
8554a0df2efSAndy Whitcroft	return $current_comment if (defined $current_comment);
8564a0df2efSAndy Whitcroft
8574a0df2efSAndy Whitcroft	# Look through the context and try and figure out if there is a
8584a0df2efSAndy Whitcroft	# comment.
8594a0df2efSAndy Whitcroft	my $in_comment = 0;
8604a0df2efSAndy Whitcroft	$current_comment = '';
8614a0df2efSAndy Whitcroft	for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
86200df344fSAndy Whitcroft		my $line = $rawlines[$linenr - 1];
86300df344fSAndy Whitcroft		#warn "           $line\n";
8644a0df2efSAndy Whitcroft		if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
8654a0df2efSAndy Whitcroft			$in_comment = 1;
8664a0df2efSAndy Whitcroft		}
8674a0df2efSAndy Whitcroft		if ($line =~ m@/\*@) {
8684a0df2efSAndy Whitcroft			$in_comment = 1;
8694a0df2efSAndy Whitcroft		}
8704a0df2efSAndy Whitcroft		if (!$in_comment && $current_comment ne '') {
8714a0df2efSAndy Whitcroft			$current_comment = '';
8724a0df2efSAndy Whitcroft		}
8734a0df2efSAndy Whitcroft		$current_comment .= $line . "\n" if ($in_comment);
8744a0df2efSAndy Whitcroft		if ($line =~ m@\*/@) {
8754a0df2efSAndy Whitcroft			$in_comment = 0;
8764a0df2efSAndy Whitcroft		}
8774a0df2efSAndy Whitcroft	}
8784a0df2efSAndy Whitcroft
8794a0df2efSAndy Whitcroft	chomp($current_comment);
8804a0df2efSAndy Whitcroft	return($current_comment);
8814a0df2efSAndy Whitcroft}
8824a0df2efSAndy Whitcroftsub ctx_has_comment {
8834a0df2efSAndy Whitcroft	my ($first_line, $end_line) = @_;
8844a0df2efSAndy Whitcroft	my $cmt = ctx_locate_comment($first_line, $end_line);
8854a0df2efSAndy Whitcroft
88600df344fSAndy Whitcroft	##print "LINE: $rawlines[$end_line - 1 ]\n";
8874a0df2efSAndy Whitcroft	##print "CMMT: $cmt\n";
8884a0df2efSAndy Whitcroft
8894a0df2efSAndy Whitcroft	return ($cmt ne '');
8904a0df2efSAndy Whitcroft}
8914a0df2efSAndy Whitcroft
8924d001e4dSAndy Whitcroftsub raw_line {
8934d001e4dSAndy Whitcroft	my ($linenr, $cnt) = @_;
8944d001e4dSAndy Whitcroft
8954d001e4dSAndy Whitcroft	my $offset = $linenr - 1;
8964d001e4dSAndy Whitcroft	$cnt++;
8974d001e4dSAndy Whitcroft
8984d001e4dSAndy Whitcroft	my $line;
8994d001e4dSAndy Whitcroft	while ($cnt) {
9004d001e4dSAndy Whitcroft		$line = $rawlines[$offset++];
9014d001e4dSAndy Whitcroft		next if (defined($line) && $line =~ /^-/);
9024d001e4dSAndy Whitcroft		$cnt--;
9034d001e4dSAndy Whitcroft	}
9044d001e4dSAndy Whitcroft
9054d001e4dSAndy Whitcroft	return $line;
9064d001e4dSAndy Whitcroft}
9074d001e4dSAndy Whitcroft
9080a920b5bSAndy Whitcroftsub cat_vet {
9090a920b5bSAndy Whitcroft	my ($vet) = @_;
9109c0ca6f9SAndy Whitcroft	my ($res, $coded);
9110a920b5bSAndy Whitcroft
9129c0ca6f9SAndy Whitcroft	$res = '';
9136c72ffaaSAndy Whitcroft	while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
9146c72ffaaSAndy Whitcroft		$res .= $1;
9156c72ffaaSAndy Whitcroft		if ($2 ne '') {
9169c0ca6f9SAndy Whitcroft			$coded = sprintf("^%c", unpack('C', $2) + 64);
9176c72ffaaSAndy Whitcroft			$res .= $coded;
9186c72ffaaSAndy Whitcroft		}
9199c0ca6f9SAndy Whitcroft	}
9209c0ca6f9SAndy Whitcroft	$res =~ s/$/\$/;
9210a920b5bSAndy Whitcroft
9229c0ca6f9SAndy Whitcroft	return $res;
9230a920b5bSAndy Whitcroft}
9240a920b5bSAndy Whitcroft
925c2fdda0dSAndy Whitcroftmy $av_preprocessor = 0;
926cf655043SAndy Whitcroftmy $av_pending;
927c2fdda0dSAndy Whitcroftmy @av_paren_type;
9281f65f947SAndy Whitcroftmy $av_pend_colon;
929c2fdda0dSAndy Whitcroft
930c2fdda0dSAndy Whitcroftsub annotate_reset {
931c2fdda0dSAndy Whitcroft	$av_preprocessor = 0;
932cf655043SAndy Whitcroft	$av_pending = '_';
933cf655043SAndy Whitcroft	@av_paren_type = ('E');
9341f65f947SAndy Whitcroft	$av_pend_colon = 'O';
935c2fdda0dSAndy Whitcroft}
936c2fdda0dSAndy Whitcroft
9376c72ffaaSAndy Whitcroftsub annotate_values {
9386c72ffaaSAndy Whitcroft	my ($stream, $type) = @_;
9396c72ffaaSAndy Whitcroft
9406c72ffaaSAndy Whitcroft	my $res;
9411f65f947SAndy Whitcroft	my $var = '_' x length($stream);
9426c72ffaaSAndy Whitcroft	my $cur = $stream;
9436c72ffaaSAndy Whitcroft
944c2fdda0dSAndy Whitcroft	print "$stream\n" if ($dbg_values > 1);
9456c72ffaaSAndy Whitcroft
9466c72ffaaSAndy Whitcroft	while (length($cur)) {
947773647a0SAndy Whitcroft		@av_paren_type = ('E') if ($#av_paren_type < 0);
948cf655043SAndy Whitcroft		print " <" . join('', @av_paren_type) .
949171ae1a4SAndy Whitcroft				"> <$type> <$av_pending>" if ($dbg_values > 1);
9506c72ffaaSAndy Whitcroft		if ($cur =~ /^(\s+)/o) {
951c2fdda0dSAndy Whitcroft			print "WS($1)\n" if ($dbg_values > 1);
952c2fdda0dSAndy Whitcroft			if ($1 =~ /\n/ && $av_preprocessor) {
953cf655043SAndy Whitcroft				$type = pop(@av_paren_type);
954c2fdda0dSAndy Whitcroft				$av_preprocessor = 0;
9556c72ffaaSAndy Whitcroft			}
9566c72ffaaSAndy Whitcroft
957c023e473SFlorian Mickler		} elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
9589446ef56SAndy Whitcroft			print "CAST($1)\n" if ($dbg_values > 1);
9599446ef56SAndy Whitcroft			push(@av_paren_type, $type);
9609446ef56SAndy Whitcroft			$type = 'C';
9619446ef56SAndy Whitcroft
962e91b6e26SAndy Whitcroft		} elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
963c2fdda0dSAndy Whitcroft			print "DECLARE($1)\n" if ($dbg_values > 1);
9646c72ffaaSAndy Whitcroft			$type = 'T';
9656c72ffaaSAndy Whitcroft
966389a2fe5SAndy Whitcroft		} elsif ($cur =~ /^($Modifier)\s*/) {
967389a2fe5SAndy Whitcroft			print "MODIFIER($1)\n" if ($dbg_values > 1);
968389a2fe5SAndy Whitcroft			$type = 'T';
969389a2fe5SAndy Whitcroft
970c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
971171ae1a4SAndy Whitcroft			print "DEFINE($1,$2)\n" if ($dbg_values > 1);
972c2fdda0dSAndy Whitcroft			$av_preprocessor = 1;
973171ae1a4SAndy Whitcroft			push(@av_paren_type, $type);
974171ae1a4SAndy Whitcroft			if ($2 ne '') {
975cf655043SAndy Whitcroft				$av_pending = 'N';
976171ae1a4SAndy Whitcroft			}
977171ae1a4SAndy Whitcroft			$type = 'E';
978171ae1a4SAndy Whitcroft
979c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
980171ae1a4SAndy Whitcroft			print "UNDEF($1)\n" if ($dbg_values > 1);
981171ae1a4SAndy Whitcroft			$av_preprocessor = 1;
982171ae1a4SAndy Whitcroft			push(@av_paren_type, $type);
9836c72ffaaSAndy Whitcroft
984c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
985cf655043SAndy Whitcroft			print "PRE_START($1)\n" if ($dbg_values > 1);
986c2fdda0dSAndy Whitcroft			$av_preprocessor = 1;
987cf655043SAndy Whitcroft
988cf655043SAndy Whitcroft			push(@av_paren_type, $type);
989cf655043SAndy Whitcroft			push(@av_paren_type, $type);
990171ae1a4SAndy Whitcroft			$type = 'E';
991cf655043SAndy Whitcroft
992c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
993cf655043SAndy Whitcroft			print "PRE_RESTART($1)\n" if ($dbg_values > 1);
994cf655043SAndy Whitcroft			$av_preprocessor = 1;
995cf655043SAndy Whitcroft
996cf655043SAndy Whitcroft			push(@av_paren_type, $av_paren_type[$#av_paren_type]);
997cf655043SAndy Whitcroft
998171ae1a4SAndy Whitcroft			$type = 'E';
999cf655043SAndy Whitcroft
1000c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:endif))/o) {
1001cf655043SAndy Whitcroft			print "PRE_END($1)\n" if ($dbg_values > 1);
1002cf655043SAndy Whitcroft
1003cf655043SAndy Whitcroft			$av_preprocessor = 1;
1004cf655043SAndy Whitcroft
1005cf655043SAndy Whitcroft			# Assume all arms of the conditional end as this
1006cf655043SAndy Whitcroft			# one does, and continue as if the #endif was not here.
1007cf655043SAndy Whitcroft			pop(@av_paren_type);
1008cf655043SAndy Whitcroft			push(@av_paren_type, $type);
1009171ae1a4SAndy Whitcroft			$type = 'E';
10106c72ffaaSAndy Whitcroft
10116c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\\\n)/o) {
1012c2fdda0dSAndy Whitcroft			print "PRECONT($1)\n" if ($dbg_values > 1);
10136c72ffaaSAndy Whitcroft
1014171ae1a4SAndy Whitcroft		} elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1015171ae1a4SAndy Whitcroft			print "ATTR($1)\n" if ($dbg_values > 1);
1016171ae1a4SAndy Whitcroft			$av_pending = $type;
1017171ae1a4SAndy Whitcroft			$type = 'N';
1018171ae1a4SAndy Whitcroft
10196c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
1020c2fdda0dSAndy Whitcroft			print "SIZEOF($1)\n" if ($dbg_values > 1);
10216c72ffaaSAndy Whitcroft			if (defined $2) {
1022cf655043SAndy Whitcroft				$av_pending = 'V';
10236c72ffaaSAndy Whitcroft			}
10246c72ffaaSAndy Whitcroft			$type = 'N';
10256c72ffaaSAndy Whitcroft
102614b111c1SAndy Whitcroft		} elsif ($cur =~ /^(if|while|for)\b/o) {
1027c2fdda0dSAndy Whitcroft			print "COND($1)\n" if ($dbg_values > 1);
102814b111c1SAndy Whitcroft			$av_pending = 'E';
10296c72ffaaSAndy Whitcroft			$type = 'N';
10306c72ffaaSAndy Whitcroft
10311f65f947SAndy Whitcroft		} elsif ($cur =~/^(case)/o) {
10321f65f947SAndy Whitcroft			print "CASE($1)\n" if ($dbg_values > 1);
10331f65f947SAndy Whitcroft			$av_pend_colon = 'C';
10341f65f947SAndy Whitcroft			$type = 'N';
10351f65f947SAndy Whitcroft
103614b111c1SAndy Whitcroft		} elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
1037c2fdda0dSAndy Whitcroft			print "KEYWORD($1)\n" if ($dbg_values > 1);
10386c72ffaaSAndy Whitcroft			$type = 'N';
10396c72ffaaSAndy Whitcroft
10406c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\()/o) {
1041c2fdda0dSAndy Whitcroft			print "PAREN('$1')\n" if ($dbg_values > 1);
1042cf655043SAndy Whitcroft			push(@av_paren_type, $av_pending);
1043cf655043SAndy Whitcroft			$av_pending = '_';
10446c72ffaaSAndy Whitcroft			$type = 'N';
10456c72ffaaSAndy Whitcroft
10466c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\))/o) {
1047cf655043SAndy Whitcroft			my $new_type = pop(@av_paren_type);
1048cf655043SAndy Whitcroft			if ($new_type ne '_') {
1049cf655043SAndy Whitcroft				$type = $new_type;
1050c2fdda0dSAndy Whitcroft				print "PAREN('$1') -> $type\n"
1051c2fdda0dSAndy Whitcroft							if ($dbg_values > 1);
10526c72ffaaSAndy Whitcroft			} else {
1053c2fdda0dSAndy Whitcroft				print "PAREN('$1')\n" if ($dbg_values > 1);
10546c72ffaaSAndy Whitcroft			}
10556c72ffaaSAndy Whitcroft
1056c8cb2ca3SAndy Whitcroft		} elsif ($cur =~ /^($Ident)\s*\(/o) {
1057c2fdda0dSAndy Whitcroft			print "FUNC($1)\n" if ($dbg_values > 1);
1058c8cb2ca3SAndy Whitcroft			$type = 'V';
1059cf655043SAndy Whitcroft			$av_pending = 'V';
10606c72ffaaSAndy Whitcroft
10618e761b04SAndy Whitcroft		} elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
10628e761b04SAndy Whitcroft			if (defined $2 && $type eq 'C' || $type eq 'T') {
10631f65f947SAndy Whitcroft				$av_pend_colon = 'B';
10648e761b04SAndy Whitcroft			} elsif ($type eq 'E') {
10658e761b04SAndy Whitcroft				$av_pend_colon = 'L';
10661f65f947SAndy Whitcroft			}
10671f65f947SAndy Whitcroft			print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
10681f65f947SAndy Whitcroft			$type = 'V';
10691f65f947SAndy Whitcroft
10706c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Ident|$Constant)/o) {
1071c2fdda0dSAndy Whitcroft			print "IDENT($1)\n" if ($dbg_values > 1);
10726c72ffaaSAndy Whitcroft			$type = 'V';
10736c72ffaaSAndy Whitcroft
10746c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Assignment)/o) {
1075c2fdda0dSAndy Whitcroft			print "ASSIGN($1)\n" if ($dbg_values > 1);
10766c72ffaaSAndy Whitcroft			$type = 'N';
10776c72ffaaSAndy Whitcroft
1078cf655043SAndy Whitcroft		} elsif ($cur =~/^(;|{|})/) {
1079c2fdda0dSAndy Whitcroft			print "END($1)\n" if ($dbg_values > 1);
108013214adfSAndy Whitcroft			$type = 'E';
10811f65f947SAndy Whitcroft			$av_pend_colon = 'O';
108213214adfSAndy Whitcroft
10838e761b04SAndy Whitcroft		} elsif ($cur =~/^(,)/) {
10848e761b04SAndy Whitcroft			print "COMMA($1)\n" if ($dbg_values > 1);
10858e761b04SAndy Whitcroft			$type = 'C';
10868e761b04SAndy Whitcroft
10871f65f947SAndy Whitcroft		} elsif ($cur =~ /^(\?)/o) {
10881f65f947SAndy Whitcroft			print "QUESTION($1)\n" if ($dbg_values > 1);
10891f65f947SAndy Whitcroft			$type = 'N';
10901f65f947SAndy Whitcroft
10911f65f947SAndy Whitcroft		} elsif ($cur =~ /^(:)/o) {
10921f65f947SAndy Whitcroft			print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
10931f65f947SAndy Whitcroft
10941f65f947SAndy Whitcroft			substr($var, length($res), 1, $av_pend_colon);
10951f65f947SAndy Whitcroft			if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
10961f65f947SAndy Whitcroft				$type = 'E';
10971f65f947SAndy Whitcroft			} else {
10981f65f947SAndy Whitcroft				$type = 'N';
10991f65f947SAndy Whitcroft			}
11001f65f947SAndy Whitcroft			$av_pend_colon = 'O';
11011f65f947SAndy Whitcroft
11028e761b04SAndy Whitcroft		} elsif ($cur =~ /^(\[)/o) {
110313214adfSAndy Whitcroft			print "CLOSE($1)\n" if ($dbg_values > 1);
11046c72ffaaSAndy Whitcroft			$type = 'N';
11056c72ffaaSAndy Whitcroft
11060d413866SAndy Whitcroft		} elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
110774048ed8SAndy Whitcroft			my $variant;
110874048ed8SAndy Whitcroft
110974048ed8SAndy Whitcroft			print "OPV($1)\n" if ($dbg_values > 1);
111074048ed8SAndy Whitcroft			if ($type eq 'V') {
111174048ed8SAndy Whitcroft				$variant = 'B';
111274048ed8SAndy Whitcroft			} else {
111374048ed8SAndy Whitcroft				$variant = 'U';
111474048ed8SAndy Whitcroft			}
111574048ed8SAndy Whitcroft
111674048ed8SAndy Whitcroft			substr($var, length($res), 1, $variant);
111774048ed8SAndy Whitcroft			$type = 'N';
111874048ed8SAndy Whitcroft
11196c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Operators)/o) {
1120c2fdda0dSAndy Whitcroft			print "OP($1)\n" if ($dbg_values > 1);
11216c72ffaaSAndy Whitcroft			if ($1 ne '++' && $1 ne '--') {
11226c72ffaaSAndy Whitcroft				$type = 'N';
11236c72ffaaSAndy Whitcroft			}
11246c72ffaaSAndy Whitcroft
11256c72ffaaSAndy Whitcroft		} elsif ($cur =~ /(^.)/o) {
1126c2fdda0dSAndy Whitcroft			print "C($1)\n" if ($dbg_values > 1);
11276c72ffaaSAndy Whitcroft		}
11286c72ffaaSAndy Whitcroft		if (defined $1) {
11296c72ffaaSAndy Whitcroft			$cur = substr($cur, length($1));
11306c72ffaaSAndy Whitcroft			$res .= $type x length($1);
11316c72ffaaSAndy Whitcroft		}
11326c72ffaaSAndy Whitcroft	}
11336c72ffaaSAndy Whitcroft
11341f65f947SAndy Whitcroft	return ($res, $var);
11356c72ffaaSAndy Whitcroft}
11366c72ffaaSAndy Whitcroft
11378905a67cSAndy Whitcroftsub possible {
113813214adfSAndy Whitcroft	my ($possible, $line) = @_;
11399a974fdbSAndy Whitcroft	my $notPermitted = qr{(?:
11400776e594SAndy Whitcroft		^(?:
11410776e594SAndy Whitcroft			$Modifier|
11420776e594SAndy Whitcroft			$Storage|
11430776e594SAndy Whitcroft			$Type|
11449a974fdbSAndy Whitcroft			DEFINE_\S+
11459a974fdbSAndy Whitcroft		)$|
11469a974fdbSAndy Whitcroft		^(?:
11470776e594SAndy Whitcroft			goto|
11480776e594SAndy Whitcroft			return|
11490776e594SAndy Whitcroft			case|
11500776e594SAndy Whitcroft			else|
11510776e594SAndy Whitcroft			asm|__asm__|
11520776e594SAndy Whitcroft			do
11539a974fdbSAndy Whitcroft		)(?:\s|$)|
11540776e594SAndy Whitcroft		^(?:typedef|struct|enum)\b
11559a974fdbSAndy Whitcroft	    )}x;
11569a974fdbSAndy Whitcroft	warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
11579a974fdbSAndy Whitcroft	if ($possible !~ $notPermitted) {
1158c45dcabdSAndy Whitcroft		# Check for modifiers.
1159c45dcabdSAndy Whitcroft		$possible =~ s/\s*$Storage\s*//g;
1160c45dcabdSAndy Whitcroft		$possible =~ s/\s*$Sparse\s*//g;
1161c45dcabdSAndy Whitcroft		if ($possible =~ /^\s*$/) {
1162c45dcabdSAndy Whitcroft
1163c45dcabdSAndy Whitcroft		} elsif ($possible =~ /\s/) {
1164c45dcabdSAndy Whitcroft			$possible =~ s/\s*$Type\s*//g;
1165d2506586SAndy Whitcroft			for my $modifier (split(' ', $possible)) {
11669a974fdbSAndy Whitcroft				if ($modifier !~ $notPermitted) {
1167d2506586SAndy Whitcroft					warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1168d2506586SAndy Whitcroft					push(@modifierList, $modifier);
1169d2506586SAndy Whitcroft				}
11709a974fdbSAndy Whitcroft			}
1171c45dcabdSAndy Whitcroft
1172c45dcabdSAndy Whitcroft		} else {
117313214adfSAndy Whitcroft			warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
11748905a67cSAndy Whitcroft			push(@typeList, $possible);
1175c45dcabdSAndy Whitcroft		}
11768905a67cSAndy Whitcroft		build_types();
11770776e594SAndy Whitcroft	} else {
11780776e594SAndy Whitcroft		warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
11798905a67cSAndy Whitcroft	}
11808905a67cSAndy Whitcroft}
11818905a67cSAndy Whitcroft
11826c72ffaaSAndy Whitcroftmy $prefix = '';
11836c72ffaaSAndy Whitcroft
1184f0a594c1SAndy Whitcroftsub report {
1185773647a0SAndy Whitcroft	if (defined $tst_only && $_[0] !~ /\Q$tst_only\E/) {
1186773647a0SAndy Whitcroft		return 0;
1187773647a0SAndy Whitcroft	}
11888905a67cSAndy Whitcroft	my $line = $prefix . $_[0];
11898905a67cSAndy Whitcroft
11908905a67cSAndy Whitcroft	$line = (split('\n', $line))[0] . "\n" if ($terse);
11918905a67cSAndy Whitcroft
119213214adfSAndy Whitcroft	push(our @report, $line);
1193773647a0SAndy Whitcroft
1194773647a0SAndy Whitcroft	return 1;
1195f0a594c1SAndy Whitcroft}
1196f0a594c1SAndy Whitcroftsub report_dump {
119713214adfSAndy Whitcroft	our @report;
1198f0a594c1SAndy Whitcroft}
1199de7d4f0eSAndy Whitcroftsub ERROR {
1200773647a0SAndy Whitcroft	if (report("ERROR: $_[0]\n")) {
1201de7d4f0eSAndy Whitcroft		our $clean = 0;
12026c72ffaaSAndy Whitcroft		our $cnt_error++;
1203de7d4f0eSAndy Whitcroft	}
1204773647a0SAndy Whitcroft}
1205de7d4f0eSAndy Whitcroftsub WARN {
1206773647a0SAndy Whitcroft	if (report("WARNING: $_[0]\n")) {
1207de7d4f0eSAndy Whitcroft		our $clean = 0;
12086c72ffaaSAndy Whitcroft		our $cnt_warn++;
1209de7d4f0eSAndy Whitcroft	}
1210773647a0SAndy Whitcroft}
1211de7d4f0eSAndy Whitcroftsub CHK {
1212773647a0SAndy Whitcroft	if ($check && report("CHECK: $_[0]\n")) {
1213de7d4f0eSAndy Whitcroft		our $clean = 0;
12146c72ffaaSAndy Whitcroft		our $cnt_chk++;
12156c72ffaaSAndy Whitcroft	}
1216de7d4f0eSAndy Whitcroft}
1217de7d4f0eSAndy Whitcroft
12186ecd9674SAndy Whitcroftsub check_absolute_file {
12196ecd9674SAndy Whitcroft	my ($absolute, $herecurr) = @_;
12206ecd9674SAndy Whitcroft	my $file = $absolute;
12216ecd9674SAndy Whitcroft
12226ecd9674SAndy Whitcroft	##print "absolute<$absolute>\n";
12236ecd9674SAndy Whitcroft
12246ecd9674SAndy Whitcroft	# See if any suffix of this path is a path within the tree.
12256ecd9674SAndy Whitcroft	while ($file =~ s@^[^/]*/@@) {
12266ecd9674SAndy Whitcroft		if (-f "$root/$file") {
12276ecd9674SAndy Whitcroft			##print "file<$file>\n";
12286ecd9674SAndy Whitcroft			last;
12296ecd9674SAndy Whitcroft		}
12306ecd9674SAndy Whitcroft	}
12316ecd9674SAndy Whitcroft	if (! -f _)  {
12326ecd9674SAndy Whitcroft		return 0;
12336ecd9674SAndy Whitcroft	}
12346ecd9674SAndy Whitcroft
12356ecd9674SAndy Whitcroft	# It is, so see if the prefix is acceptable.
12366ecd9674SAndy Whitcroft	my $prefix = $absolute;
12376ecd9674SAndy Whitcroft	substr($prefix, -length($file)) = '';
12386ecd9674SAndy Whitcroft
12396ecd9674SAndy Whitcroft	##print "prefix<$prefix>\n";
12406ecd9674SAndy Whitcroft	if ($prefix ne ".../") {
12416ecd9674SAndy Whitcroft		WARN("use relative pathname instead of absolute in changelog text\n" . $herecurr);
12426ecd9674SAndy Whitcroft	}
12436ecd9674SAndy Whitcroft}
12446ecd9674SAndy Whitcroft
12450a920b5bSAndy Whitcroftsub process {
12460a920b5bSAndy Whitcroft	my $filename = shift;
12470a920b5bSAndy Whitcroft
12480a920b5bSAndy Whitcroft	my $linenr=0;
12490a920b5bSAndy Whitcroft	my $prevline="";
1250c2fdda0dSAndy Whitcroft	my $prevrawline="";
12510a920b5bSAndy Whitcroft	my $stashline="";
1252c2fdda0dSAndy Whitcroft	my $stashrawline="";
12530a920b5bSAndy Whitcroft
12544a0df2efSAndy Whitcroft	my $length;
12550a920b5bSAndy Whitcroft	my $indent;
12560a920b5bSAndy Whitcroft	my $previndent=0;
12570a920b5bSAndy Whitcroft	my $stashindent=0;
12580a920b5bSAndy Whitcroft
1259de7d4f0eSAndy Whitcroft	our $clean = 1;
12600a920b5bSAndy Whitcroft	my $signoff = 0;
12610a920b5bSAndy Whitcroft	my $is_patch = 0;
12620a920b5bSAndy Whitcroft
126313214adfSAndy Whitcroft	our @report = ();
12646c72ffaaSAndy Whitcroft	our $cnt_lines = 0;
12656c72ffaaSAndy Whitcroft	our $cnt_error = 0;
12666c72ffaaSAndy Whitcroft	our $cnt_warn = 0;
12676c72ffaaSAndy Whitcroft	our $cnt_chk = 0;
12686c72ffaaSAndy Whitcroft
12690a920b5bSAndy Whitcroft	# Trace the real file/line as we go.
12700a920b5bSAndy Whitcroft	my $realfile = '';
12710a920b5bSAndy Whitcroft	my $realline = 0;
12720a920b5bSAndy Whitcroft	my $realcnt = 0;
12730a920b5bSAndy Whitcroft	my $here = '';
12740a920b5bSAndy Whitcroft	my $in_comment = 0;
1275c2fdda0dSAndy Whitcroft	my $comment_edge = 0;
12760a920b5bSAndy Whitcroft	my $first_line = 0;
12771e855726SWolfram Sang	my $p1_prefix = '';
12780a920b5bSAndy Whitcroft
127913214adfSAndy Whitcroft	my $prev_values = 'E';
128013214adfSAndy Whitcroft
128113214adfSAndy Whitcroft	# suppression flags
1282773647a0SAndy Whitcroft	my %suppress_ifbraces;
1283170d3a22SAndy Whitcroft	my %suppress_whiletrailers;
12842b474a1aSAndy Whitcroft	my %suppress_export;
1285653d4876SAndy Whitcroft
1286c2fdda0dSAndy Whitcroft	# Pre-scan the patch sanitizing the lines.
1287de7d4f0eSAndy Whitcroft	# Pre-scan the patch looking for any __setup documentation.
1288c2fdda0dSAndy Whitcroft	#
1289de7d4f0eSAndy Whitcroft	my @setup_docs = ();
1290de7d4f0eSAndy Whitcroft	my $setup_docs = 0;
1291773647a0SAndy Whitcroft
1292773647a0SAndy Whitcroft	sanitise_line_reset();
1293c2fdda0dSAndy Whitcroft	my $line;
1294c2fdda0dSAndy Whitcroft	foreach my $rawline (@rawlines) {
1295773647a0SAndy Whitcroft		$linenr++;
1296773647a0SAndy Whitcroft		$line = $rawline;
1297c2fdda0dSAndy Whitcroft
1298773647a0SAndy Whitcroft		if ($rawline=~/^\+\+\+\s+(\S+)/) {
1299de7d4f0eSAndy Whitcroft			$setup_docs = 0;
1300de7d4f0eSAndy Whitcroft			if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
1301de7d4f0eSAndy Whitcroft				$setup_docs = 1;
1302de7d4f0eSAndy Whitcroft			}
1303773647a0SAndy Whitcroft			#next;
1304de7d4f0eSAndy Whitcroft		}
1305773647a0SAndy Whitcroft		if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1306773647a0SAndy Whitcroft			$realline=$1-1;
1307773647a0SAndy Whitcroft			if (defined $2) {
1308773647a0SAndy Whitcroft				$realcnt=$3+1;
1309773647a0SAndy Whitcroft			} else {
1310773647a0SAndy Whitcroft				$realcnt=1+1;
1311773647a0SAndy Whitcroft			}
1312c45dcabdSAndy Whitcroft			$in_comment = 0;
1313773647a0SAndy Whitcroft
1314773647a0SAndy Whitcroft			# Guestimate if this is a continuing comment.  Run
1315773647a0SAndy Whitcroft			# the context looking for a comment "edge".  If this
1316773647a0SAndy Whitcroft			# edge is a close comment then we must be in a comment
1317773647a0SAndy Whitcroft			# at context start.
1318773647a0SAndy Whitcroft			my $edge;
131901fa9147SAndy Whitcroft			my $cnt = $realcnt;
132001fa9147SAndy Whitcroft			for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
132101fa9147SAndy Whitcroft				next if (defined $rawlines[$ln - 1] &&
132201fa9147SAndy Whitcroft					 $rawlines[$ln - 1] =~ /^-/);
132301fa9147SAndy Whitcroft				$cnt--;
132401fa9147SAndy Whitcroft				#print "RAW<$rawlines[$ln - 1]>\n";
1325721c1cb6SAndy Whitcroft				last if (!defined $rawlines[$ln - 1]);
1326fae17daeSAndy Whitcroft				if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
1327fae17daeSAndy Whitcroft				    $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
1328fae17daeSAndy Whitcroft					($edge) = $1;
1329fae17daeSAndy Whitcroft					last;
1330fae17daeSAndy Whitcroft				}
1331773647a0SAndy Whitcroft			}
1332773647a0SAndy Whitcroft			if (defined $edge && $edge eq '*/') {
1333773647a0SAndy Whitcroft				$in_comment = 1;
1334773647a0SAndy Whitcroft			}
1335773647a0SAndy Whitcroft
1336773647a0SAndy Whitcroft			# Guestimate if this is a continuing comment.  If this
1337773647a0SAndy Whitcroft			# is the start of a diff block and this line starts
1338773647a0SAndy Whitcroft			# ' *' then it is very likely a comment.
1339773647a0SAndy Whitcroft			if (!defined $edge &&
134083242e0cSAndy Whitcroft			    $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
1341773647a0SAndy Whitcroft			{
1342773647a0SAndy Whitcroft				$in_comment = 1;
1343773647a0SAndy Whitcroft			}
1344773647a0SAndy Whitcroft
1345773647a0SAndy Whitcroft			##print "COMMENT:$in_comment edge<$edge> $rawline\n";
1346773647a0SAndy Whitcroft			sanitise_line_reset($in_comment);
1347773647a0SAndy Whitcroft
1348171ae1a4SAndy Whitcroft		} elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
1349773647a0SAndy Whitcroft			# Standardise the strings and chars within the input to
1350171ae1a4SAndy Whitcroft			# simplify matching -- only bother with positive lines.
1351773647a0SAndy Whitcroft			$line = sanitise_line($rawline);
1352773647a0SAndy Whitcroft		}
1353773647a0SAndy Whitcroft		push(@lines, $line);
1354773647a0SAndy Whitcroft
1355773647a0SAndy Whitcroft		if ($realcnt > 1) {
1356773647a0SAndy Whitcroft			$realcnt-- if ($line =~ /^(?:\+| |$)/);
1357773647a0SAndy Whitcroft		} else {
1358773647a0SAndy Whitcroft			$realcnt = 0;
1359773647a0SAndy Whitcroft		}
1360773647a0SAndy Whitcroft
1361773647a0SAndy Whitcroft		#print "==>$rawline\n";
1362773647a0SAndy Whitcroft		#print "-->$line\n";
1363de7d4f0eSAndy Whitcroft
1364de7d4f0eSAndy Whitcroft		if ($setup_docs && $line =~ /^\+/) {
1365de7d4f0eSAndy Whitcroft			push(@setup_docs, $line);
1366de7d4f0eSAndy Whitcroft		}
1367de7d4f0eSAndy Whitcroft	}
1368de7d4f0eSAndy Whitcroft
13696c72ffaaSAndy Whitcroft	$prefix = '';
13706c72ffaaSAndy Whitcroft
1371773647a0SAndy Whitcroft	$realcnt = 0;
1372773647a0SAndy Whitcroft	$linenr = 0;
13730a920b5bSAndy Whitcroft	foreach my $line (@lines) {
13740a920b5bSAndy Whitcroft		$linenr++;
13750a920b5bSAndy Whitcroft
1376c2fdda0dSAndy Whitcroft		my $rawline = $rawlines[$linenr - 1];
13776c72ffaaSAndy Whitcroft
13780a920b5bSAndy Whitcroft#extract the line range in the file after the patch is applied
13796c72ffaaSAndy Whitcroft		if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
13800a920b5bSAndy Whitcroft			$is_patch = 1;
13814a0df2efSAndy Whitcroft			$first_line = $linenr + 1;
13820a920b5bSAndy Whitcroft			$realline=$1-1;
13830a920b5bSAndy Whitcroft			if (defined $2) {
13840a920b5bSAndy Whitcroft				$realcnt=$3+1;
13850a920b5bSAndy Whitcroft			} else {
13860a920b5bSAndy Whitcroft				$realcnt=1+1;
13870a920b5bSAndy Whitcroft			}
1388c2fdda0dSAndy Whitcroft			annotate_reset();
138913214adfSAndy Whitcroft			$prev_values = 'E';
139013214adfSAndy Whitcroft
1391773647a0SAndy Whitcroft			%suppress_ifbraces = ();
1392170d3a22SAndy Whitcroft			%suppress_whiletrailers = ();
13932b474a1aSAndy Whitcroft			%suppress_export = ();
13940a920b5bSAndy Whitcroft			next;
13950a920b5bSAndy Whitcroft
13964a0df2efSAndy Whitcroft# track the line number as we move through the hunk, note that
13974a0df2efSAndy Whitcroft# new versions of GNU diff omit the leading space on completely
13984a0df2efSAndy Whitcroft# blank context lines so we need to count that too.
1399773647a0SAndy Whitcroft		} elsif ($line =~ /^( |\+|$)/) {
14000a920b5bSAndy Whitcroft			$realline++;
1401d8aaf121SAndy Whitcroft			$realcnt-- if ($realcnt != 0);
14020a920b5bSAndy Whitcroft
14034a0df2efSAndy Whitcroft			# Measure the line length and indent.
1404c2fdda0dSAndy Whitcroft			($length, $indent) = line_stats($rawline);
14050a920b5bSAndy Whitcroft
14060a920b5bSAndy Whitcroft			# Track the previous line.
14070a920b5bSAndy Whitcroft			($prevline, $stashline) = ($stashline, $line);
14080a920b5bSAndy Whitcroft			($previndent, $stashindent) = ($stashindent, $indent);
1409c2fdda0dSAndy Whitcroft			($prevrawline, $stashrawline) = ($stashrawline, $rawline);
1410c2fdda0dSAndy Whitcroft
1411773647a0SAndy Whitcroft			#warn "line<$line>\n";
14126c72ffaaSAndy Whitcroft
1413d8aaf121SAndy Whitcroft		} elsif ($realcnt == 1) {
1414d8aaf121SAndy Whitcroft			$realcnt--;
14150a920b5bSAndy Whitcroft		}
14160a920b5bSAndy Whitcroft
1417cc77cdcaSAndy Whitcroft		my $hunk_line = ($realcnt != 0);
1418cc77cdcaSAndy Whitcroft
14190a920b5bSAndy Whitcroft#make up the handle for any error we report on this line
1420773647a0SAndy Whitcroft		$prefix = "$filename:$realline: " if ($emacs && $file);
1421773647a0SAndy Whitcroft		$prefix = "$filename:$linenr: " if ($emacs && !$file);
1422773647a0SAndy Whitcroft
14236c72ffaaSAndy Whitcroft		$here = "#$linenr: " if (!$file);
14246c72ffaaSAndy Whitcroft		$here = "#$realline: " if ($file);
1425773647a0SAndy Whitcroft
1426773647a0SAndy Whitcroft		# extract the filename as it passes
14273bf9a009SRabin Vincent		if ($line =~ /^diff --git.*?(\S+)$/) {
14283bf9a009SRabin Vincent			$realfile = $1;
14293bf9a009SRabin Vincent			$realfile =~ s@^([^/]*)/@@;
14303bf9a009SRabin Vincent
14313bf9a009SRabin Vincent		} elsif ($line =~ /^\+\+\+\s+(\S+)/) {
1432773647a0SAndy Whitcroft			$realfile = $1;
14331e855726SWolfram Sang			$realfile =~ s@^([^/]*)/@@;
14341e855726SWolfram Sang
14351e855726SWolfram Sang			$p1_prefix = $1;
1436e2f7aa4bSAndy Whitcroft			if (!$file && $tree && $p1_prefix ne '' &&
1437e2f7aa4bSAndy Whitcroft			    -e "$root/$p1_prefix") {
14381e855726SWolfram Sang				WARN("patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
14391e855726SWolfram Sang			}
1440773647a0SAndy Whitcroft
1441c1ab3326SAndy Whitcroft			if ($realfile =~ m@^include/asm/@) {
1442773647a0SAndy Whitcroft				ERROR("do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
1443773647a0SAndy Whitcroft			}
1444773647a0SAndy Whitcroft			next;
1445773647a0SAndy Whitcroft		}
1446773647a0SAndy Whitcroft
1447389834b6SRandy Dunlap		$here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
14480a920b5bSAndy Whitcroft
1449c2fdda0dSAndy Whitcroft		my $hereline = "$here\n$rawline\n";
1450c2fdda0dSAndy Whitcroft		my $herecurr = "$here\n$rawline\n";
1451c2fdda0dSAndy Whitcroft		my $hereprev = "$here\n$prevrawline\n$rawline\n";
14520a920b5bSAndy Whitcroft
14536c72ffaaSAndy Whitcroft		$cnt_lines++ if ($realcnt != 0);
14546c72ffaaSAndy Whitcroft
14553bf9a009SRabin Vincent# Check for incorrect file permissions
14563bf9a009SRabin Vincent		if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
14573bf9a009SRabin Vincent			my $permhere = $here . "FILE: $realfile\n";
14583bf9a009SRabin Vincent			if ($realfile =~ /(Makefile|Kconfig|\.c|\.h|\.S|\.tmpl)$/) {
14593bf9a009SRabin Vincent				ERROR("do not set execute permissions for source files\n" . $permhere);
14603bf9a009SRabin Vincent			}
14613bf9a009SRabin Vincent		}
14623bf9a009SRabin Vincent
1463*20112475SJoe Perches# Check the patch for a signoff:
1464d8aaf121SAndy Whitcroft		if ($line =~ /^\s*signed-off-by:/i) {
14654a0df2efSAndy Whitcroft			$signoff++;
14660a920b5bSAndy Whitcroft		}
1467*20112475SJoe Perches
1468*20112475SJoe Perches# Check signature styles
1469*20112475SJoe Perches		if ($line =~ /^(\s*)($signature_tags)(\s*)(.*)/) {
1470*20112475SJoe Perches			my $space_before = $1;
1471*20112475SJoe Perches			my $sign_off = $2;
1472*20112475SJoe Perches			my $space_after = $3;
1473*20112475SJoe Perches			my $email = $4;
1474*20112475SJoe Perches			my $ucfirst_sign_off = ucfirst(lc($sign_off));
1475*20112475SJoe Perches
1476*20112475SJoe Perches			if (defined $space_before && $space_before ne "") {
1477*20112475SJoe Perches				WARN("Do not use whitespace before $ucfirst_sign_off\n" . $herecurr);
1478*20112475SJoe Perches			}
1479*20112475SJoe Perches			if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
1480*20112475SJoe Perches				WARN("'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr);
1481*20112475SJoe Perches			}
1482*20112475SJoe Perches			if (!defined $space_after || $space_after ne " ") {
1483*20112475SJoe Perches				WARN("Use a single space after $ucfirst_sign_off\n" . $herecurr);
1484*20112475SJoe Perches			}
1485*20112475SJoe Perches
1486*20112475SJoe Perches			my ($email_name, $email_address, $comment) = parse_email($email);
1487*20112475SJoe Perches			my $suggested_email = format_email(($email_name, $email_address));
1488*20112475SJoe Perches			if ($suggested_email eq "") {
1489*20112475SJoe Perches				ERROR("Unrecognized email address: '$email'\n" . $herecurr);
1490*20112475SJoe Perches			} else {
1491*20112475SJoe Perches				my $dequoted = $suggested_email;
1492*20112475SJoe Perches				$dequoted =~ s/^"//;
1493*20112475SJoe Perches				$dequoted =~ s/" </ </;
1494*20112475SJoe Perches				# Don't force email to have quotes
1495*20112475SJoe Perches				# Allow just an angle bracketed address
1496*20112475SJoe Perches				if ("$dequoted$comment" ne $email &&
1497*20112475SJoe Perches				    "<$email_address>$comment" ne $email &&
1498*20112475SJoe Perches				    "$suggested_email$comment" ne $email) {
1499*20112475SJoe Perches					WARN("email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
1500*20112475SJoe Perches				}
15010a920b5bSAndy Whitcroft			}
15020a920b5bSAndy Whitcroft		}
15030a920b5bSAndy Whitcroft
150400df344fSAndy Whitcroft# Check for wrappage within a valid hunk of the file
15058905a67cSAndy Whitcroft		if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
1506de7d4f0eSAndy Whitcroft			ERROR("patch seems to be corrupt (line wrapped?)\n" .
15076c72ffaaSAndy Whitcroft				$herecurr) if (!$emitted_corrupt++);
1508de7d4f0eSAndy Whitcroft		}
1509de7d4f0eSAndy Whitcroft
15106ecd9674SAndy Whitcroft# Check for absolute kernel paths.
15116ecd9674SAndy Whitcroft		if ($tree) {
15126ecd9674SAndy Whitcroft			while ($line =~ m{(?:^|\s)(/\S*)}g) {
15136ecd9674SAndy Whitcroft				my $file = $1;
15146ecd9674SAndy Whitcroft
15156ecd9674SAndy Whitcroft				if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
15166ecd9674SAndy Whitcroft				    check_absolute_file($1, $herecurr)) {
15176ecd9674SAndy Whitcroft					#
15186ecd9674SAndy Whitcroft				} else {
15196ecd9674SAndy Whitcroft					check_absolute_file($file, $herecurr);
15206ecd9674SAndy Whitcroft				}
15216ecd9674SAndy Whitcroft			}
15226ecd9674SAndy Whitcroft		}
15236ecd9674SAndy Whitcroft
1524de7d4f0eSAndy Whitcroft# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
1525de7d4f0eSAndy Whitcroft		if (($realfile =~ /^$/ || $line =~ /^\+/) &&
1526171ae1a4SAndy Whitcroft		    $rawline !~ m/^$UTF8*$/) {
1527171ae1a4SAndy Whitcroft			my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
1528171ae1a4SAndy Whitcroft
1529171ae1a4SAndy Whitcroft			my $blank = copy_spacing($rawline);
1530171ae1a4SAndy Whitcroft			my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
1531171ae1a4SAndy Whitcroft			my $hereptr = "$hereline$ptr\n";
1532171ae1a4SAndy Whitcroft
1533171ae1a4SAndy Whitcroft			ERROR("Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
153400df344fSAndy Whitcroft		}
15350a920b5bSAndy Whitcroft
153630670854SAndy Whitcroft# ignore non-hunk lines and lines being removed
153730670854SAndy Whitcroft		next if (!$hunk_line || $line =~ /^-/);
153800df344fSAndy Whitcroft
15390a920b5bSAndy Whitcroft#trailing whitespace
15409c0ca6f9SAndy Whitcroft		if ($line =~ /^\+.*\015/) {
1541c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
15429c0ca6f9SAndy Whitcroft			ERROR("DOS line endings\n" . $herevet);
15439c0ca6f9SAndy Whitcroft
1544c2fdda0dSAndy Whitcroft		} elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
1545c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1546de7d4f0eSAndy Whitcroft			ERROR("trailing whitespace\n" . $herevet);
1547d2c0a235SAndy Whitcroft			$rpt_cleaners = 1;
15480a920b5bSAndy Whitcroft		}
15495368df20SAndy Whitcroft
15503354957aSAndi Kleen# check for Kconfig help text having a real description
15519fe287d7SAndy Whitcroft# Only applies when adding the entry originally, after that we do not have
15529fe287d7SAndy Whitcroft# sufficient context to determine whether it is indeed long enough.
15533354957aSAndi Kleen		if ($realfile =~ /Kconfig/ &&
15549fe287d7SAndy Whitcroft		    $line =~ /\+\s*(?:---)?help(?:---)?$/) {
15553354957aSAndi Kleen			my $length = 0;
15569fe287d7SAndy Whitcroft			my $cnt = $realcnt;
15579fe287d7SAndy Whitcroft			my $ln = $linenr + 1;
15589fe287d7SAndy Whitcroft			my $f;
15599fe287d7SAndy Whitcroft			my $is_end = 0;
15609fe287d7SAndy Whitcroft			while ($cnt > 0 && defined $lines[$ln - 1]) {
15619fe287d7SAndy Whitcroft				$f = $lines[$ln - 1];
15629fe287d7SAndy Whitcroft				$cnt-- if ($lines[$ln - 1] !~ /^-/);
15639fe287d7SAndy Whitcroft				$is_end = $lines[$ln - 1] =~ /^\+/;
15649fe287d7SAndy Whitcroft				$ln++;
15659fe287d7SAndy Whitcroft
15669fe287d7SAndy Whitcroft				next if ($f =~ /^-/);
15679fe287d7SAndy Whitcroft				$f =~ s/^.//;
15683354957aSAndi Kleen				$f =~ s/#.*//;
15693354957aSAndi Kleen				$f =~ s/^\s+//;
15703354957aSAndi Kleen				next if ($f =~ /^$/);
15719fe287d7SAndy Whitcroft				if ($f =~ /^\s*config\s/) {
15729fe287d7SAndy Whitcroft					$is_end = 1;
15739fe287d7SAndy Whitcroft					last;
15749fe287d7SAndy Whitcroft				}
15753354957aSAndi Kleen				$length++;
15763354957aSAndi Kleen			}
15779fe287d7SAndy Whitcroft			WARN("please write a paragraph that describes the config symbol fully\n" . $herecurr) if ($is_end && $length < 4);
15789fe287d7SAndy Whitcroft			#print "is_end<$is_end> length<$length>\n";
15793354957aSAndi Kleen		}
15803354957aSAndi Kleen
15815368df20SAndy Whitcroft# check we are in a valid source file if not then ignore this hunk
15825368df20SAndy Whitcroft		next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
15835368df20SAndy Whitcroft
15840a920b5bSAndy Whitcroft#80 column limit
1585c45dcabdSAndy Whitcroft		if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ &&
1586f4c014c0SAndy Whitcroft		    $rawline !~ /^.\s*\*\s*\@$Ident\s/ &&
15870fccc622SJoe Perches		    !($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(KERN_\S+\s*|[^"]*))?"[X\t]*"\s*(?:|,|\)\s*;)\s*$/ ||
15888bbea968SJoe Perches		    $line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) &&
1589f4c014c0SAndy Whitcroft		    $length > 80)
1590c45dcabdSAndy Whitcroft		{
1591de7d4f0eSAndy Whitcroft			WARN("line over 80 characters\n" . $herecurr);
15920a920b5bSAndy Whitcroft		}
15930a920b5bSAndy Whitcroft
15945e79d96eSJoe Perches# check for spaces before a quoted newline
15955e79d96eSJoe Perches		if ($rawline =~ /^.*\".*\s\\n/) {
15965e79d96eSJoe Perches			WARN("unnecessary whitespace before a quoted newline\n" . $herecurr);
15975e79d96eSJoe Perches		}
15985e79d96eSJoe Perches
15998905a67cSAndy Whitcroft# check for adding lines without a newline.
16008905a67cSAndy Whitcroft		if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
16018905a67cSAndy Whitcroft			WARN("adding a line without newline at end of file\n" . $herecurr);
16028905a67cSAndy Whitcroft		}
16038905a67cSAndy Whitcroft
160442e41c54SMike Frysinger# Blackfin: use hi/lo macros
160542e41c54SMike Frysinger		if ($realfile =~ m@arch/blackfin/.*\.S$@) {
160642e41c54SMike Frysinger			if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
160742e41c54SMike Frysinger				my $herevet = "$here\n" . cat_vet($line) . "\n";
160842e41c54SMike Frysinger				ERROR("use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
160942e41c54SMike Frysinger			}
161042e41c54SMike Frysinger			if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
161142e41c54SMike Frysinger				my $herevet = "$here\n" . cat_vet($line) . "\n";
161242e41c54SMike Frysinger				ERROR("use the HI() macro, not (... >> 16)\n" . $herevet);
161342e41c54SMike Frysinger			}
161442e41c54SMike Frysinger		}
161542e41c54SMike Frysinger
1616b9ea10d6SAndy Whitcroft# check we are in a valid source file C or perl if not then ignore this hunk
1617b9ea10d6SAndy Whitcroft		next if ($realfile !~ /\.(h|c|pl)$/);
16180a920b5bSAndy Whitcroft
16190a920b5bSAndy Whitcroft# at the beginning of a line any tabs must come first and anything
16200a920b5bSAndy Whitcroft# more than 8 must use tabs.
1621c2fdda0dSAndy Whitcroft		if ($rawline =~ /^\+\s* \t\s*\S/ ||
1622c2fdda0dSAndy Whitcroft		    $rawline =~ /^\+\s*        \s*/) {
1623c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1624171ae1a4SAndy Whitcroft			ERROR("code indent should use tabs where possible\n" . $herevet);
1625d2c0a235SAndy Whitcroft			$rpt_cleaners = 1;
16260a920b5bSAndy Whitcroft		}
16270a920b5bSAndy Whitcroft
162808e44365SAlberto Panizzo# check for space before tabs.
162908e44365SAlberto Panizzo		if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
163008e44365SAlberto Panizzo			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
163108e44365SAlberto Panizzo			WARN("please, no space before tabs\n" . $herevet);
163208e44365SAlberto Panizzo		}
163308e44365SAlberto Panizzo
16345f7ddae6SRaffaele Recalcati# check for spaces at the beginning of a line.
16356b4c5bebSAndy Whitcroft# Exceptions:
16366b4c5bebSAndy Whitcroft#  1) within comments
16376b4c5bebSAndy Whitcroft#  2) indented preprocessor commands
16386b4c5bebSAndy Whitcroft#  3) hanging labels
16396b4c5bebSAndy Whitcroft		if ($rawline =~ /^\+ / && $line !~ /\+ *(?:$;|#|$Ident:)/)  {
16405f7ddae6SRaffaele Recalcati			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
16416b4c5bebSAndy Whitcroft			WARN("please, no spaces at the start of a line\n" . $herevet);
16425f7ddae6SRaffaele Recalcati		}
16435f7ddae6SRaffaele Recalcati
1644b9ea10d6SAndy Whitcroft# check we are in a valid C source file if not then ignore this hunk
1645b9ea10d6SAndy Whitcroft		next if ($realfile !~ /\.(h|c)$/);
1646b9ea10d6SAndy Whitcroft
1647c2fdda0dSAndy Whitcroft# check for RCS/CVS revision markers
1648cf655043SAndy Whitcroft		if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
1649c2fdda0dSAndy Whitcroft			WARN("CVS style keyword markers, these will _not_ be updated\n". $herecurr);
1650c2fdda0dSAndy Whitcroft		}
165122f2a2efSAndy Whitcroft
165242e41c54SMike Frysinger# Blackfin: don't use __builtin_bfin_[cs]sync
165342e41c54SMike Frysinger		if ($line =~ /__builtin_bfin_csync/) {
165442e41c54SMike Frysinger			my $herevet = "$here\n" . cat_vet($line) . "\n";
165542e41c54SMike Frysinger			ERROR("use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
165642e41c54SMike Frysinger		}
165742e41c54SMike Frysinger		if ($line =~ /__builtin_bfin_ssync/) {
165842e41c54SMike Frysinger			my $herevet = "$here\n" . cat_vet($line) . "\n";
165942e41c54SMike Frysinger			ERROR("use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
166042e41c54SMike Frysinger		}
166142e41c54SMike Frysinger
16629c0ca6f9SAndy Whitcroft# Check for potential 'bare' types
16632b474a1aSAndy Whitcroft		my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
16642b474a1aSAndy Whitcroft		    $realline_next);
16659c9ba34eSAndy Whitcroft		if ($realcnt && $line =~ /.\s*\S/) {
1666170d3a22SAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
1667f5fe35ddSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0);
1668171ae1a4SAndy Whitcroft			$stat =~ s/\n./\n /g;
1669171ae1a4SAndy Whitcroft			$cond =~ s/\n./\n /g;
1670171ae1a4SAndy Whitcroft
16712b474a1aSAndy Whitcroft			# Find the real next line.
16722b474a1aSAndy Whitcroft			$realline_next = $line_nr_next;
16732b474a1aSAndy Whitcroft			if (defined $realline_next &&
16742b474a1aSAndy Whitcroft			    (!defined $lines[$realline_next - 1] ||
16752b474a1aSAndy Whitcroft			     substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
16762b474a1aSAndy Whitcroft				$realline_next++;
16772b474a1aSAndy Whitcroft			}
16782b474a1aSAndy Whitcroft
1679171ae1a4SAndy Whitcroft			my $s = $stat;
1680171ae1a4SAndy Whitcroft			$s =~ s/{.*$//s;
1681cf655043SAndy Whitcroft
1682c2fdda0dSAndy Whitcroft			# Ignore goto labels.
1683171ae1a4SAndy Whitcroft			if ($s =~ /$Ident:\*$/s) {
1684c2fdda0dSAndy Whitcroft
1685c2fdda0dSAndy Whitcroft			# Ignore functions being called
1686171ae1a4SAndy Whitcroft			} elsif ($s =~ /^.\s*$Ident\s*\(/s) {
1687c2fdda0dSAndy Whitcroft
1688463f2864SAndy Whitcroft			} elsif ($s =~ /^.\s*else\b/s) {
1689463f2864SAndy Whitcroft
1690c45dcabdSAndy Whitcroft			# declarations always start with types
1691d2506586SAndy 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) {
1692c45dcabdSAndy Whitcroft				my $type = $1;
1693c45dcabdSAndy Whitcroft				$type =~ s/\s+/ /g;
1694c45dcabdSAndy Whitcroft				possible($type, "A:" . $s);
1695c45dcabdSAndy Whitcroft
16966c72ffaaSAndy Whitcroft			# definitions in global scope can only start with types
1697a6a84062SAndy Whitcroft			} elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
1698c45dcabdSAndy Whitcroft				possible($1, "B:" . $s);
1699c2fdda0dSAndy Whitcroft			}
17008905a67cSAndy Whitcroft
17016c72ffaaSAndy Whitcroft			# any (foo ... *) is a pointer cast, and foo is a type
170265863862SAndy Whitcroft			while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
1703c45dcabdSAndy Whitcroft				possible($1, "C:" . $s);
17049c0ca6f9SAndy Whitcroft			}
17058905a67cSAndy Whitcroft
17068905a67cSAndy Whitcroft			# Check for any sort of function declaration.
17078905a67cSAndy Whitcroft			# int foo(something bar, other baz);
17088905a67cSAndy Whitcroft			# void (*store_gdt)(x86_descr_ptr *);
1709171ae1a4SAndy Whitcroft			if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
17108905a67cSAndy Whitcroft				my ($name_len) = length($1);
17118905a67cSAndy Whitcroft
1712cf655043SAndy Whitcroft				my $ctx = $s;
1713773647a0SAndy Whitcroft				substr($ctx, 0, $name_len + 1, '');
17148905a67cSAndy Whitcroft				$ctx =~ s/\)[^\)]*$//;
1715cf655043SAndy Whitcroft
17168905a67cSAndy Whitcroft				for my $arg (split(/\s*,\s*/, $ctx)) {
1717c45dcabdSAndy Whitcroft					if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
17188905a67cSAndy Whitcroft
1719c45dcabdSAndy Whitcroft						possible($1, "D:" . $s);
17208905a67cSAndy Whitcroft					}
17218905a67cSAndy Whitcroft				}
17228905a67cSAndy Whitcroft			}
17238905a67cSAndy Whitcroft
17249c0ca6f9SAndy Whitcroft		}
17259c0ca6f9SAndy Whitcroft
172600df344fSAndy Whitcroft#
172700df344fSAndy Whitcroft# Checks which may be anchored in the context.
172800df344fSAndy Whitcroft#
172900df344fSAndy Whitcroft
173000df344fSAndy Whitcroft# Check for switch () and associated case and default
173100df344fSAndy Whitcroft# statements should be at the same indent.
173200df344fSAndy Whitcroft		if ($line=~/\bswitch\s*\(.*\)/) {
173300df344fSAndy Whitcroft			my $err = '';
173400df344fSAndy Whitcroft			my $sep = '';
173500df344fSAndy Whitcroft			my @ctx = ctx_block_outer($linenr, $realcnt);
173600df344fSAndy Whitcroft			shift(@ctx);
173700df344fSAndy Whitcroft			for my $ctx (@ctx) {
173800df344fSAndy Whitcroft				my ($clen, $cindent) = line_stats($ctx);
173900df344fSAndy Whitcroft				if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
174000df344fSAndy Whitcroft							$indent != $cindent) {
174100df344fSAndy Whitcroft					$err .= "$sep$ctx\n";
174200df344fSAndy Whitcroft					$sep = '';
174300df344fSAndy Whitcroft				} else {
174400df344fSAndy Whitcroft					$sep = "[...]\n";
174500df344fSAndy Whitcroft				}
174600df344fSAndy Whitcroft			}
174700df344fSAndy Whitcroft			if ($err ne '') {
17489c0ca6f9SAndy Whitcroft				ERROR("switch and case should be at the same indent\n$hereline$err");
1749de7d4f0eSAndy Whitcroft			}
1750de7d4f0eSAndy Whitcroft		}
1751de7d4f0eSAndy Whitcroft
1752de7d4f0eSAndy Whitcroft# if/while/etc brace do not go on next line, unless defining a do while loop,
1753de7d4f0eSAndy Whitcroft# or if that brace on the next line is for something else
1754c45dcabdSAndy Whitcroft		if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
1755773647a0SAndy Whitcroft			my $pre_ctx = "$1$2";
1756773647a0SAndy Whitcroft
17579c0ca6f9SAndy Whitcroft			my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
1758de7d4f0eSAndy Whitcroft			my $ctx_cnt = $realcnt - $#ctx - 1;
1759de7d4f0eSAndy Whitcroft			my $ctx = join("\n", @ctx);
1760de7d4f0eSAndy Whitcroft
1761548596d5SAndy Whitcroft			my $ctx_ln = $linenr;
1762548596d5SAndy Whitcroft			my $ctx_skip = $realcnt;
1763de7d4f0eSAndy Whitcroft
1764548596d5SAndy Whitcroft			while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
1765548596d5SAndy Whitcroft					defined $lines[$ctx_ln - 1] &&
1766548596d5SAndy Whitcroft					$lines[$ctx_ln - 1] =~ /^-/)) {
1767548596d5SAndy Whitcroft				##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
1768548596d5SAndy Whitcroft				$ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
1769773647a0SAndy Whitcroft				$ctx_ln++;
1770773647a0SAndy Whitcroft			}
1771548596d5SAndy Whitcroft
177253210168SAndy Whitcroft			#print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
177353210168SAndy Whitcroft			#print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
1774773647a0SAndy Whitcroft
1775773647a0SAndy Whitcroft			if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln -1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
1776773647a0SAndy Whitcroft				ERROR("that open brace { should be on the previous line\n" .
177701464f30SAndy Whitcroft					"$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
177800df344fSAndy Whitcroft			}
1779773647a0SAndy Whitcroft			if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
1780773647a0SAndy Whitcroft			    $ctx =~ /\)\s*\;\s*$/ &&
1781773647a0SAndy Whitcroft			    defined $lines[$ctx_ln - 1])
1782773647a0SAndy Whitcroft			{
17839c0ca6f9SAndy Whitcroft				my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
17849c0ca6f9SAndy Whitcroft				if ($nindent > $indent) {
1785773647a0SAndy Whitcroft					WARN("trailing semicolon indicates no statements, indent implies otherwise\n" .
178601464f30SAndy Whitcroft						"$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
17879c0ca6f9SAndy Whitcroft				}
17889c0ca6f9SAndy Whitcroft			}
178900df344fSAndy Whitcroft		}
179000df344fSAndy Whitcroft
17914d001e4dSAndy Whitcroft# Check relative indent for conditionals and blocks.
17924d001e4dSAndy Whitcroft		if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
17934d001e4dSAndy Whitcroft			my ($s, $c) = ($stat, $cond);
17944d001e4dSAndy Whitcroft
17954d001e4dSAndy Whitcroft			substr($s, 0, length($c), '');
17964d001e4dSAndy Whitcroft
17974d001e4dSAndy Whitcroft			# Make sure we remove the line prefixes as we have
17984d001e4dSAndy Whitcroft			# none on the first line, and are going to readd them
17994d001e4dSAndy Whitcroft			# where necessary.
18004d001e4dSAndy Whitcroft			$s =~ s/\n./\n/gs;
18014d001e4dSAndy Whitcroft
18024d001e4dSAndy Whitcroft			# Find out how long the conditional actually is.
18036f779c18SAndy Whitcroft			my @newlines = ($c =~ /\n/gs);
18046f779c18SAndy Whitcroft			my $cond_lines = 1 + $#newlines;
18054d001e4dSAndy Whitcroft
18064d001e4dSAndy Whitcroft			# We want to check the first line inside the block
18074d001e4dSAndy Whitcroft			# starting at the end of the conditional, so remove:
18084d001e4dSAndy Whitcroft			#  1) any blank line termination
18094d001e4dSAndy Whitcroft			#  2) any opening brace { on end of the line
18104d001e4dSAndy Whitcroft			#  3) any do (...) {
18114d001e4dSAndy Whitcroft			my $continuation = 0;
18124d001e4dSAndy Whitcroft			my $check = 0;
18134d001e4dSAndy Whitcroft			$s =~ s/^.*\bdo\b//;
18144d001e4dSAndy Whitcroft			$s =~ s/^\s*{//;
18154d001e4dSAndy Whitcroft			if ($s =~ s/^\s*\\//) {
18164d001e4dSAndy Whitcroft				$continuation = 1;
18174d001e4dSAndy Whitcroft			}
18189bd49efeSAndy Whitcroft			if ($s =~ s/^\s*?\n//) {
18194d001e4dSAndy Whitcroft				$check = 1;
18204d001e4dSAndy Whitcroft				$cond_lines++;
18214d001e4dSAndy Whitcroft			}
18224d001e4dSAndy Whitcroft
18234d001e4dSAndy Whitcroft			# Also ignore a loop construct at the end of a
18244d001e4dSAndy Whitcroft			# preprocessor statement.
18254d001e4dSAndy Whitcroft			if (($prevline =~ /^.\s*#\s*define\s/ ||
18264d001e4dSAndy Whitcroft			    $prevline =~ /\\\s*$/) && $continuation == 0) {
18274d001e4dSAndy Whitcroft				$check = 0;
18284d001e4dSAndy Whitcroft			}
18294d001e4dSAndy Whitcroft
18309bd49efeSAndy Whitcroft			my $cond_ptr = -1;
1831740504c6SAndy Whitcroft			$continuation = 0;
18329bd49efeSAndy Whitcroft			while ($cond_ptr != $cond_lines) {
18339bd49efeSAndy Whitcroft				$cond_ptr = $cond_lines;
18344d001e4dSAndy Whitcroft
1835f16fa28fSAndy Whitcroft				# If we see an #else/#elif then the code
1836f16fa28fSAndy Whitcroft				# is not linear.
1837f16fa28fSAndy Whitcroft				if ($s =~ /^\s*\#\s*(?:else|elif)/) {
1838f16fa28fSAndy Whitcroft					$check = 0;
1839f16fa28fSAndy Whitcroft				}
1840f16fa28fSAndy Whitcroft
18419bd49efeSAndy Whitcroft				# Ignore:
18429bd49efeSAndy Whitcroft				#  1) blank lines, they should be at 0,
18439bd49efeSAndy Whitcroft				#  2) preprocessor lines, and
18449bd49efeSAndy Whitcroft				#  3) labels.
1845740504c6SAndy Whitcroft				if ($continuation ||
1846740504c6SAndy Whitcroft				    $s =~ /^\s*?\n/ ||
18479bd49efeSAndy Whitcroft				    $s =~ /^\s*#\s*?/ ||
18489bd49efeSAndy Whitcroft				    $s =~ /^\s*$Ident\s*:/) {
1849740504c6SAndy Whitcroft					$continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
185030dad6ebSAndy Whitcroft					if ($s =~ s/^.*?\n//) {
18519bd49efeSAndy Whitcroft						$cond_lines++;
18529bd49efeSAndy Whitcroft					}
18534d001e4dSAndy Whitcroft				}
185430dad6ebSAndy Whitcroft			}
18554d001e4dSAndy Whitcroft
18564d001e4dSAndy Whitcroft			my (undef, $sindent) = line_stats("+" . $s);
18574d001e4dSAndy Whitcroft			my $stat_real = raw_line($linenr, $cond_lines);
18584d001e4dSAndy Whitcroft
18594d001e4dSAndy Whitcroft			# Check if either of these lines are modified, else
18604d001e4dSAndy Whitcroft			# this is not this patch's fault.
18614d001e4dSAndy Whitcroft			if (!defined($stat_real) ||
18624d001e4dSAndy Whitcroft			    $stat !~ /^\+/ && $stat_real !~ /^\+/) {
18634d001e4dSAndy Whitcroft				$check = 0;
18644d001e4dSAndy Whitcroft			}
18654d001e4dSAndy Whitcroft			if (defined($stat_real) && $cond_lines > 1) {
18664d001e4dSAndy Whitcroft				$stat_real = "[...]\n$stat_real";
18674d001e4dSAndy Whitcroft			}
18684d001e4dSAndy Whitcroft
18699bd49efeSAndy 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";
18704d001e4dSAndy Whitcroft
18714d001e4dSAndy Whitcroft			if ($check && (($sindent % 8) != 0 ||
18724d001e4dSAndy Whitcroft			    ($sindent <= $indent && $s ne ''))) {
18734d001e4dSAndy Whitcroft				WARN("suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
18744d001e4dSAndy Whitcroft			}
18754d001e4dSAndy Whitcroft		}
18764d001e4dSAndy Whitcroft
18776c72ffaaSAndy Whitcroft		# Track the 'values' across context and added lines.
18786c72ffaaSAndy Whitcroft		my $opline = $line; $opline =~ s/^./ /;
18791f65f947SAndy Whitcroft		my ($curr_values, $curr_vars) =
18801f65f947SAndy Whitcroft				annotate_values($opline . "\n", $prev_values);
18816c72ffaaSAndy Whitcroft		$curr_values = $prev_values . $curr_values;
1882c2fdda0dSAndy Whitcroft		if ($dbg_values) {
1883c2fdda0dSAndy Whitcroft			my $outline = $opline; $outline =~ s/\t/ /g;
1884cf655043SAndy Whitcroft			print "$linenr > .$outline\n";
1885cf655043SAndy Whitcroft			print "$linenr > $curr_values\n";
18861f65f947SAndy Whitcroft			print "$linenr >  $curr_vars\n";
1887c2fdda0dSAndy Whitcroft		}
18886c72ffaaSAndy Whitcroft		$prev_values = substr($curr_values, -1);
18896c72ffaaSAndy Whitcroft
189000df344fSAndy Whitcroft#ignore lines not being added
189100df344fSAndy Whitcroft		if ($line=~/^[^\+]/) {next;}
189200df344fSAndy Whitcroft
1893653d4876SAndy Whitcroft# TEST: allow direct testing of the type matcher.
18947429c690SAndy Whitcroft		if ($dbg_type) {
18957429c690SAndy Whitcroft			if ($line =~ /^.\s*$Declare\s*$/) {
18967429c690SAndy Whitcroft				ERROR("TEST: is type\n" . $herecurr);
18977429c690SAndy Whitcroft			} elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
18987429c690SAndy Whitcroft				ERROR("TEST: is not type ($1 is)\n". $herecurr);
18997429c690SAndy Whitcroft			}
1900653d4876SAndy Whitcroft			next;
1901653d4876SAndy Whitcroft		}
1902a1ef277eSAndy Whitcroft# TEST: allow direct testing of the attribute matcher.
1903a1ef277eSAndy Whitcroft		if ($dbg_attr) {
19049360b0e5SAndy Whitcroft			if ($line =~ /^.\s*$Modifier\s*$/) {
1905a1ef277eSAndy Whitcroft				ERROR("TEST: is attr\n" . $herecurr);
19069360b0e5SAndy Whitcroft			} elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
1907a1ef277eSAndy Whitcroft				ERROR("TEST: is not attr ($1 is)\n". $herecurr);
1908a1ef277eSAndy Whitcroft			}
1909a1ef277eSAndy Whitcroft			next;
1910a1ef277eSAndy Whitcroft		}
1911653d4876SAndy Whitcroft
1912f0a594c1SAndy Whitcroft# check for initialisation to aggregates open brace on the next line
191399423c20SAndy Whitcroft		if ($line =~ /^.\s*{/ &&
191499423c20SAndy Whitcroft		    $prevline =~ /(?:^|[^=])=\s*$/) {
1915773647a0SAndy Whitcroft			ERROR("that open brace { should be on the previous line\n" . $hereprev);
1916f0a594c1SAndy Whitcroft		}
1917f0a594c1SAndy Whitcroft
191800df344fSAndy Whitcroft#
191900df344fSAndy Whitcroft# Checks which are anchored on the added line.
192000df344fSAndy Whitcroft#
192100df344fSAndy Whitcroft
1922653d4876SAndy Whitcroft# check for malformed paths in #include statements (uses RAW line)
1923c45dcabdSAndy Whitcroft		if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
1924653d4876SAndy Whitcroft			my $path = $1;
1925653d4876SAndy Whitcroft			if ($path =~ m{//}) {
1926de7d4f0eSAndy Whitcroft				ERROR("malformed #include filename\n" .
1927de7d4f0eSAndy Whitcroft					$herecurr);
1928653d4876SAndy Whitcroft			}
1929653d4876SAndy Whitcroft		}
1930653d4876SAndy Whitcroft
193100df344fSAndy Whitcroft# no C99 // comments
193200df344fSAndy Whitcroft		if ($line =~ m{//}) {
1933de7d4f0eSAndy Whitcroft			ERROR("do not use C99 // comments\n" . $herecurr);
193400df344fSAndy Whitcroft		}
193500df344fSAndy Whitcroft		# Remove C99 comments.
19360a920b5bSAndy Whitcroft		$line =~ s@//.*@@;
19376c72ffaaSAndy Whitcroft		$opline =~ s@//.*@@;
19380a920b5bSAndy Whitcroft
19392b474a1aSAndy Whitcroft# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
19402b474a1aSAndy Whitcroft# the whole statement.
19412b474a1aSAndy Whitcroft#print "APW <$lines[$realline_next - 1]>\n";
19422b474a1aSAndy Whitcroft		if (defined $realline_next &&
19432b474a1aSAndy Whitcroft		    exists $lines[$realline_next - 1] &&
19442b474a1aSAndy Whitcroft		    !defined $suppress_export{$realline_next} &&
19452b474a1aSAndy Whitcroft		    ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
19462b474a1aSAndy Whitcroft		     $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
19473cbf62dfSAndy Whitcroft			# Handle definitions which produce identifiers with
19483cbf62dfSAndy Whitcroft			# a prefix:
19493cbf62dfSAndy Whitcroft			#   XXX(foo);
19503cbf62dfSAndy Whitcroft			#   EXPORT_SYMBOL(something_foo);
1951653d4876SAndy Whitcroft			my $name = $1;
19523cbf62dfSAndy Whitcroft			if ($stat =~ /^.([A-Z_]+)\s*\(\s*($Ident)/ &&
19533cbf62dfSAndy Whitcroft			    $name =~ /^${Ident}_$2/) {
19543cbf62dfSAndy Whitcroft#print "FOO C name<$name>\n";
19553cbf62dfSAndy Whitcroft				$suppress_export{$realline_next} = 1;
19563cbf62dfSAndy Whitcroft
19573cbf62dfSAndy Whitcroft			} elsif ($stat !~ /(?:
19582b474a1aSAndy Whitcroft				\n.}\s*$|
195948012058SAndy Whitcroft				^.DEFINE_$Ident\(\Q$name\E\)|
196048012058SAndy Whitcroft				^.DECLARE_$Ident\(\Q$name\E\)|
196148012058SAndy Whitcroft				^.LIST_HEAD\(\Q$name\E\)|
19622b474a1aSAndy Whitcroft				^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
19632b474a1aSAndy Whitcroft				\b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
196448012058SAndy Whitcroft			    )/x) {
19652b474a1aSAndy Whitcroft#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
19662b474a1aSAndy Whitcroft				$suppress_export{$realline_next} = 2;
19672b474a1aSAndy Whitcroft			} else {
19682b474a1aSAndy Whitcroft				$suppress_export{$realline_next} = 1;
19690a920b5bSAndy Whitcroft			}
19700a920b5bSAndy Whitcroft		}
19712b474a1aSAndy Whitcroft		if (!defined $suppress_export{$linenr} &&
19722b474a1aSAndy Whitcroft		    $prevline =~ /^.\s*$/ &&
19732b474a1aSAndy Whitcroft		    ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
19742b474a1aSAndy Whitcroft		     $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
19752b474a1aSAndy Whitcroft#print "FOO B <$lines[$linenr - 1]>\n";
19762b474a1aSAndy Whitcroft			$suppress_export{$linenr} = 2;
19772b474a1aSAndy Whitcroft		}
19782b474a1aSAndy Whitcroft		if (defined $suppress_export{$linenr} &&
19792b474a1aSAndy Whitcroft		    $suppress_export{$linenr} == 2) {
19802b474a1aSAndy Whitcroft			WARN("EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
19812b474a1aSAndy Whitcroft		}
19820a920b5bSAndy Whitcroft
19835150bda4SJoe Eloff# check for global initialisers.
1984c45dcabdSAndy Whitcroft		if ($line =~ /^.$Type\s*$Ident\s*(?:\s+$Modifier)*\s*=\s*(0|NULL|false)\s*;/) {
19855150bda4SJoe Eloff			ERROR("do not initialise globals to 0 or NULL\n" .
1986f0a594c1SAndy Whitcroft				$herecurr);
1987f0a594c1SAndy Whitcroft		}
19880a920b5bSAndy Whitcroft# check for static initialisers.
19892d1bafd7SAndy Whitcroft		if ($line =~ /\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
1990de7d4f0eSAndy Whitcroft			ERROR("do not initialise statics to 0 or NULL\n" .
1991de7d4f0eSAndy Whitcroft				$herecurr);
19920a920b5bSAndy Whitcroft		}
19930a920b5bSAndy Whitcroft
1994cb710ecaSJoe Perches# check for static const char * arrays.
1995cb710ecaSJoe Perches		if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
1996cb710ecaSJoe Perches			WARN("static const char * array should probably be static const char * const\n" .
1997cb710ecaSJoe Perches				$herecurr);
1998cb710ecaSJoe Perches               }
1999cb710ecaSJoe Perches
2000cb710ecaSJoe Perches# check for static char foo[] = "bar" declarations.
2001cb710ecaSJoe Perches		if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
2002cb710ecaSJoe Perches			WARN("static char array declaration should probably be static const char\n" .
2003cb710ecaSJoe Perches				$herecurr);
2004cb710ecaSJoe Perches               }
2005cb710ecaSJoe Perches
200693ed0e2dSJoe Perches# check for declarations of struct pci_device_id
200793ed0e2dSJoe Perches		if ($line =~ /\bstruct\s+pci_device_id\s+\w+\s*\[\s*\]\s*\=\s*\{/) {
200893ed0e2dSJoe Perches			WARN("Use DEFINE_PCI_DEVICE_TABLE for struct pci_device_id\n" . $herecurr);
200993ed0e2dSJoe Perches		}
201093ed0e2dSJoe Perches
2011653d4876SAndy Whitcroft# check for new typedefs, only function parameters and sparse annotations
2012653d4876SAndy Whitcroft# make sense.
2013653d4876SAndy Whitcroft		if ($line =~ /\btypedef\s/ &&
20148054576dSAndy Whitcroft		    $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
2015c45dcabdSAndy Whitcroft		    $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
20168ed22cadSAndy Whitcroft		    $line !~ /\b$typeTypedefs\b/ &&
2017653d4876SAndy Whitcroft		    $line !~ /\b__bitwise(?:__|)\b/) {
2018de7d4f0eSAndy Whitcroft			WARN("do not add new typedefs\n" . $herecurr);
20190a920b5bSAndy Whitcroft		}
20200a920b5bSAndy Whitcroft
20210a920b5bSAndy Whitcroft# * goes on variable not on type
202265863862SAndy Whitcroft		# (char*[ const])
202300ef4eceSAndy Whitcroft		if ($line =~ m{\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\)}) {
202465863862SAndy Whitcroft			my ($from, $to) = ($1, $1);
2025d8aaf121SAndy Whitcroft
202665863862SAndy Whitcroft			# Should start with a space.
202765863862SAndy Whitcroft			$to =~ s/^(\S)/ $1/;
202865863862SAndy Whitcroft			# Should not end with a space.
202965863862SAndy Whitcroft			$to =~ s/\s+$//;
203065863862SAndy Whitcroft			# '*'s should not have spaces between.
2031f9a0b3d1SAndy Whitcroft			while ($to =~ s/\*\s+\*/\*\*/) {
203265863862SAndy Whitcroft			}
2033d8aaf121SAndy Whitcroft
203465863862SAndy Whitcroft			#print "from<$from> to<$to>\n";
203565863862SAndy Whitcroft			if ($from ne $to) {
203665863862SAndy Whitcroft				ERROR("\"(foo$from)\" should be \"(foo$to)\"\n" .  $herecurr);
203765863862SAndy Whitcroft			}
203800ef4eceSAndy Whitcroft		} elsif ($line =~ m{\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident)}) {
203965863862SAndy Whitcroft			my ($from, $to, $ident) = ($1, $1, $2);
2040d8aaf121SAndy Whitcroft
204165863862SAndy Whitcroft			# Should start with a space.
204265863862SAndy Whitcroft			$to =~ s/^(\S)/ $1/;
204365863862SAndy Whitcroft			# Should not end with a space.
204465863862SAndy Whitcroft			$to =~ s/\s+$//;
204565863862SAndy Whitcroft			# '*'s should not have spaces between.
2046f9a0b3d1SAndy Whitcroft			while ($to =~ s/\*\s+\*/\*\*/) {
204765863862SAndy Whitcroft			}
204865863862SAndy Whitcroft			# Modifiers should have spaces.
204965863862SAndy Whitcroft			$to =~ s/(\b$Modifier$)/$1 /;
205065863862SAndy Whitcroft
2051667026e7SAndy Whitcroft			#print "from<$from> to<$to> ident<$ident>\n";
2052667026e7SAndy Whitcroft			if ($from ne $to && $ident !~ /^$Modifier$/) {
205365863862SAndy Whitcroft				ERROR("\"foo${from}bar\" should be \"foo${to}bar\"\n" .  $herecurr);
205465863862SAndy Whitcroft			}
20550a920b5bSAndy Whitcroft		}
20560a920b5bSAndy Whitcroft
20570a920b5bSAndy Whitcroft# # no BUG() or BUG_ON()
20580a920b5bSAndy Whitcroft# 		if ($line =~ /\b(BUG|BUG_ON)\b/) {
20590a920b5bSAndy Whitcroft# 			print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
20600a920b5bSAndy Whitcroft# 			print "$herecurr";
20610a920b5bSAndy Whitcroft# 			$clean = 0;
20620a920b5bSAndy Whitcroft# 		}
20630a920b5bSAndy Whitcroft
20648905a67cSAndy Whitcroft		if ($line =~ /\bLINUX_VERSION_CODE\b/) {
2065c2fdda0dSAndy Whitcroft			WARN("LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
20668905a67cSAndy Whitcroft		}
20678905a67cSAndy Whitcroft
206817441227SJoe Perches# check for uses of printk_ratelimit
206917441227SJoe Perches		if ($line =~ /\bprintk_ratelimit\s*\(/) {
207017441227SJoe Perches			WARN("Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
207117441227SJoe Perches		}
207217441227SJoe Perches
207300df344fSAndy Whitcroft# printk should use KERN_* levels.  Note that follow on printk's on the
207400df344fSAndy Whitcroft# same line do not need a level, so we use the current block context
207500df344fSAndy Whitcroft# to try and find and validate the current printk.  In summary the current
207625985edcSLucas De Marchi# printk includes all preceding printk's which have no newline on the end.
207700df344fSAndy Whitcroft# we assume the first bad printk is the one to report.
2078f0a594c1SAndy Whitcroft		if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
207900df344fSAndy Whitcroft			my $ok = 0;
208000df344fSAndy Whitcroft			for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
208100df344fSAndy Whitcroft				#print "CHECK<$lines[$ln - 1]\n";
208225985edcSLucas De Marchi				# we have a preceding printk if it ends
208300df344fSAndy Whitcroft				# with "\n" ignore it, else it is to blame
208400df344fSAndy Whitcroft				if ($lines[$ln - 1] =~ m{\bprintk\(}) {
208500df344fSAndy Whitcroft					if ($rawlines[$ln - 1] !~ m{\\n"}) {
208600df344fSAndy Whitcroft						$ok = 1;
208700df344fSAndy Whitcroft					}
208800df344fSAndy Whitcroft					last;
208900df344fSAndy Whitcroft				}
209000df344fSAndy Whitcroft			}
209100df344fSAndy Whitcroft			if ($ok == 0) {
2092de7d4f0eSAndy Whitcroft				WARN("printk() should include KERN_ facility level\n" . $herecurr);
20930a920b5bSAndy Whitcroft			}
209400df344fSAndy Whitcroft		}
20950a920b5bSAndy Whitcroft
2096653d4876SAndy Whitcroft# function brace can't be on same line, except for #defines of do while,
2097653d4876SAndy Whitcroft# or if closed on same line
2098c45dcabdSAndy Whitcroft		if (($line=~/$Type\s*$Ident\(.*\).*\s{/) and
2099c45dcabdSAndy Whitcroft		    !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) {
2100de7d4f0eSAndy Whitcroft			ERROR("open brace '{' following function declarations go on the next line\n" . $herecurr);
21010a920b5bSAndy Whitcroft		}
2102653d4876SAndy Whitcroft
21038905a67cSAndy Whitcroft# open braces for enum, union and struct go on the same line.
21048905a67cSAndy Whitcroft		if ($line =~ /^.\s*{/ &&
21058905a67cSAndy Whitcroft		    $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
21068905a67cSAndy Whitcroft			ERROR("open brace '{' following $1 go on the same line\n" . $hereprev);
21078905a67cSAndy Whitcroft		}
21088905a67cSAndy Whitcroft
21090c73b4ebSAndy Whitcroft# missing space after union, struct or enum definition
21100c73b4ebSAndy Whitcroft		if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?(?:\s+$Ident)?[=\{]/) {
21110c73b4ebSAndy Whitcroft		    WARN("missing space after $1 definition\n" . $herecurr);
21120c73b4ebSAndy Whitcroft		}
21130c73b4ebSAndy Whitcroft
21148d31cfceSAndy Whitcroft# check for spacing round square brackets; allowed:
21158d31cfceSAndy Whitcroft#  1. with a type on the left -- int [] a;
2116fe2a7dbcSAndy Whitcroft#  2. at the beginning of a line for slice initialisers -- [0...10] = 5,
2117fe2a7dbcSAndy Whitcroft#  3. inside a curly brace -- = { [0...10] = 5 }
21188d31cfceSAndy Whitcroft		while ($line =~ /(.*?\s)\[/g) {
21198d31cfceSAndy Whitcroft			my ($where, $prefix) = ($-[1], $1);
21208d31cfceSAndy Whitcroft			if ($prefix !~ /$Type\s+$/ &&
2121fe2a7dbcSAndy Whitcroft			    ($where != 0 || $prefix !~ /^.\s+$/) &&
2122fe2a7dbcSAndy Whitcroft			    $prefix !~ /{\s+$/) {
21238d31cfceSAndy Whitcroft				ERROR("space prohibited before open square bracket '['\n" . $herecurr);
21248d31cfceSAndy Whitcroft			}
21258d31cfceSAndy Whitcroft		}
21268d31cfceSAndy Whitcroft
2127f0a594c1SAndy Whitcroft# check for spaces between functions and their parentheses.
21286c72ffaaSAndy Whitcroft		while ($line =~ /($Ident)\s+\(/g) {
2129c2fdda0dSAndy Whitcroft			my $name = $1;
2130773647a0SAndy Whitcroft			my $ctx_before = substr($line, 0, $-[1]);
2131773647a0SAndy Whitcroft			my $ctx = "$ctx_before$name";
2132c2fdda0dSAndy Whitcroft
2133c2fdda0dSAndy Whitcroft			# Ignore those directives where spaces _are_ permitted.
2134773647a0SAndy Whitcroft			if ($name =~ /^(?:
2135773647a0SAndy Whitcroft				if|for|while|switch|return|case|
2136773647a0SAndy Whitcroft				volatile|__volatile__|
2137773647a0SAndy Whitcroft				__attribute__|format|__extension__|
2138773647a0SAndy Whitcroft				asm|__asm__)$/x)
2139773647a0SAndy Whitcroft			{
2140c2fdda0dSAndy Whitcroft
2141c2fdda0dSAndy Whitcroft			# cpp #define statements have non-optional spaces, ie
2142c2fdda0dSAndy Whitcroft			# if there is a space between the name and the open
2143c2fdda0dSAndy Whitcroft			# parenthesis it is simply not a parameter group.
2144c45dcabdSAndy Whitcroft			} elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
2145773647a0SAndy Whitcroft
2146773647a0SAndy Whitcroft			# cpp #elif statement condition may start with a (
2147c45dcabdSAndy Whitcroft			} elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
2148c2fdda0dSAndy Whitcroft
2149c2fdda0dSAndy Whitcroft			# If this whole things ends with a type its most
2150c2fdda0dSAndy Whitcroft			# likely a typedef for a function.
2151773647a0SAndy Whitcroft			} elsif ($ctx =~ /$Type$/) {
2152c2fdda0dSAndy Whitcroft
2153c2fdda0dSAndy Whitcroft			} else {
2154773647a0SAndy Whitcroft				WARN("space prohibited between function name and open parenthesis '('\n" . $herecurr);
2155f0a594c1SAndy Whitcroft			}
21566c72ffaaSAndy Whitcroft		}
2157653d4876SAndy Whitcroft# Check operator spacing.
21580a920b5bSAndy Whitcroft		if (!($line=~/\#\s*include/)) {
21599c0ca6f9SAndy Whitcroft			my $ops = qr{
21609c0ca6f9SAndy Whitcroft				<<=|>>=|<=|>=|==|!=|
21619c0ca6f9SAndy Whitcroft				\+=|-=|\*=|\/=|%=|\^=|\|=|&=|
21629c0ca6f9SAndy Whitcroft				=>|->|<<|>>|<|>|=|!|~|
21631f65f947SAndy Whitcroft				&&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
21641f65f947SAndy Whitcroft				\?|:
21659c0ca6f9SAndy Whitcroft			}x;
2166cf655043SAndy Whitcroft			my @elements = split(/($ops|;)/, $opline);
216700df344fSAndy Whitcroft			my $off = 0;
21686c72ffaaSAndy Whitcroft
21696c72ffaaSAndy Whitcroft			my $blank = copy_spacing($opline);
21706c72ffaaSAndy Whitcroft
21710a920b5bSAndy Whitcroft			for (my $n = 0; $n < $#elements; $n += 2) {
21724a0df2efSAndy Whitcroft				$off += length($elements[$n]);
21734a0df2efSAndy Whitcroft
217425985edcSLucas De Marchi				# Pick up the preceding and succeeding characters.
2175773647a0SAndy Whitcroft				my $ca = substr($opline, 0, $off);
2176773647a0SAndy Whitcroft				my $cc = '';
2177773647a0SAndy Whitcroft				if (length($opline) >= ($off + length($elements[$n + 1]))) {
2178773647a0SAndy Whitcroft					$cc = substr($opline, $off + length($elements[$n + 1]));
2179773647a0SAndy Whitcroft				}
2180773647a0SAndy Whitcroft				my $cb = "$ca$;$cc";
2181773647a0SAndy Whitcroft
21824a0df2efSAndy Whitcroft				my $a = '';
21834a0df2efSAndy Whitcroft				$a = 'V' if ($elements[$n] ne '');
21844a0df2efSAndy Whitcroft				$a = 'W' if ($elements[$n] =~ /\s$/);
2185cf655043SAndy Whitcroft				$a = 'C' if ($elements[$n] =~ /$;$/);
21864a0df2efSAndy Whitcroft				$a = 'B' if ($elements[$n] =~ /(\[|\()$/);
21874a0df2efSAndy Whitcroft				$a = 'O' if ($elements[$n] eq '');
2188773647a0SAndy Whitcroft				$a = 'E' if ($ca =~ /^\s*$/);
21894a0df2efSAndy Whitcroft
21900a920b5bSAndy Whitcroft				my $op = $elements[$n + 1];
21914a0df2efSAndy Whitcroft
21924a0df2efSAndy Whitcroft				my $c = '';
21930a920b5bSAndy Whitcroft				if (defined $elements[$n + 2]) {
21944a0df2efSAndy Whitcroft					$c = 'V' if ($elements[$n + 2] ne '');
21954a0df2efSAndy Whitcroft					$c = 'W' if ($elements[$n + 2] =~ /^\s/);
2196cf655043SAndy Whitcroft					$c = 'C' if ($elements[$n + 2] =~ /^$;/);
21974a0df2efSAndy Whitcroft					$c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
21984a0df2efSAndy Whitcroft					$c = 'O' if ($elements[$n + 2] eq '');
21998b1b3378SAndy Whitcroft					$c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
22004a0df2efSAndy Whitcroft				} else {
22014a0df2efSAndy Whitcroft					$c = 'E';
22020a920b5bSAndy Whitcroft				}
22030a920b5bSAndy Whitcroft
22044a0df2efSAndy Whitcroft				my $ctx = "${a}x${c}";
22054a0df2efSAndy Whitcroft
22064a0df2efSAndy Whitcroft				my $at = "(ctx:$ctx)";
22074a0df2efSAndy Whitcroft
22086c72ffaaSAndy Whitcroft				my $ptr = substr($blank, 0, $off) . "^";
2209de7d4f0eSAndy Whitcroft				my $hereptr = "$hereline$ptr\n";
22100a920b5bSAndy Whitcroft
221174048ed8SAndy Whitcroft				# Pull out the value of this operator.
22126c72ffaaSAndy Whitcroft				my $op_type = substr($curr_values, $off + 1, 1);
22130a920b5bSAndy Whitcroft
22141f65f947SAndy Whitcroft				# Get the full operator variant.
22151f65f947SAndy Whitcroft				my $opv = $op . substr($curr_vars, $off, 1);
22161f65f947SAndy Whitcroft
221713214adfSAndy Whitcroft				# Ignore operators passed as parameters.
221813214adfSAndy Whitcroft				if ($op_type ne 'V' &&
221913214adfSAndy Whitcroft				    $ca =~ /\s$/ && $cc =~ /^\s*,/) {
222013214adfSAndy Whitcroft
2221cf655043SAndy Whitcroft#				# Ignore comments
2222cf655043SAndy Whitcroft#				} elsif ($op =~ /^$;+$/) {
222313214adfSAndy Whitcroft
2224d8aaf121SAndy Whitcroft				# ; should have either the end of line or a space or \ after it
222513214adfSAndy Whitcroft				} elsif ($op eq ';') {
2226cf655043SAndy Whitcroft					if ($ctx !~ /.x[WEBC]/ &&
2227cf655043SAndy Whitcroft					    $cc !~ /^\\/ && $cc !~ /^;/) {
2228773647a0SAndy Whitcroft						ERROR("space required after that '$op' $at\n" . $hereptr);
2229d8aaf121SAndy Whitcroft					}
2230d8aaf121SAndy Whitcroft
2231d8aaf121SAndy Whitcroft				# // is a comment
2232d8aaf121SAndy Whitcroft				} elsif ($op eq '//') {
22330a920b5bSAndy Whitcroft
22341f65f947SAndy Whitcroft				# No spaces for:
22351f65f947SAndy Whitcroft				#   ->
22361f65f947SAndy Whitcroft				#   :   when part of a bitfield
22371f65f947SAndy Whitcroft				} elsif ($op eq '->' || $opv eq ':B') {
22384a0df2efSAndy Whitcroft					if ($ctx =~ /Wx.|.xW/) {
2239773647a0SAndy Whitcroft						ERROR("spaces prohibited around that '$op' $at\n" . $hereptr);
22400a920b5bSAndy Whitcroft					}
22410a920b5bSAndy Whitcroft
22420a920b5bSAndy Whitcroft				# , must have a space on the right.
22430a920b5bSAndy Whitcroft				} elsif ($op eq ',') {
2244cf655043SAndy Whitcroft					if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
2245773647a0SAndy Whitcroft						ERROR("space required after that '$op' $at\n" . $hereptr);
22460a920b5bSAndy Whitcroft					}
22470a920b5bSAndy Whitcroft
22489c0ca6f9SAndy Whitcroft				# '*' as part of a type definition -- reported already.
224974048ed8SAndy Whitcroft				} elsif ($opv eq '*_') {
22509c0ca6f9SAndy Whitcroft					#warn "'*' is part of type\n";
22519c0ca6f9SAndy Whitcroft
22529c0ca6f9SAndy Whitcroft				# unary operators should have a space before and
22539c0ca6f9SAndy Whitcroft				# none after.  May be left adjacent to another
22549c0ca6f9SAndy Whitcroft				# unary operator, or a cast
22559c0ca6f9SAndy Whitcroft				} elsif ($op eq '!' || $op eq '~' ||
225674048ed8SAndy Whitcroft					 $opv eq '*U' || $opv eq '-U' ||
22570d413866SAndy Whitcroft					 $opv eq '&U' || $opv eq '&&U') {
2258cf655043SAndy Whitcroft					if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
2259773647a0SAndy Whitcroft						ERROR("space required before that '$op' $at\n" . $hereptr);
22600a920b5bSAndy Whitcroft					}
2261a3340b35SAndy Whitcroft					if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
2262171ae1a4SAndy Whitcroft						# A unary '*' may be const
2263171ae1a4SAndy Whitcroft
2264171ae1a4SAndy Whitcroft					} elsif ($ctx =~ /.xW/) {
2265fb9e9096SAndy Whitcroft						ERROR("space prohibited after that '$op' $at\n" . $hereptr);
22660a920b5bSAndy Whitcroft					}
22670a920b5bSAndy Whitcroft
22680a920b5bSAndy Whitcroft				# unary ++ and unary -- are allowed no space on one side.
22690a920b5bSAndy Whitcroft				} elsif ($op eq '++' or $op eq '--') {
2270773647a0SAndy Whitcroft					if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
2271773647a0SAndy Whitcroft						ERROR("space required one side of that '$op' $at\n" . $hereptr);
22720a920b5bSAndy Whitcroft					}
2273773647a0SAndy Whitcroft					if ($ctx =~ /Wx[BE]/ ||
2274773647a0SAndy Whitcroft					    ($ctx =~ /Wx./ && $cc =~ /^;/)) {
2275773647a0SAndy Whitcroft						ERROR("space prohibited before that '$op' $at\n" . $hereptr);
2276653d4876SAndy Whitcroft					}
2277773647a0SAndy Whitcroft					if ($ctx =~ /ExW/) {
2278773647a0SAndy Whitcroft						ERROR("space prohibited after that '$op' $at\n" . $hereptr);
2279773647a0SAndy Whitcroft					}
2280773647a0SAndy Whitcroft
22810a920b5bSAndy Whitcroft
22820a920b5bSAndy Whitcroft				# << and >> may either have or not have spaces both sides
22839c0ca6f9SAndy Whitcroft				} elsif ($op eq '<<' or $op eq '>>' or
22849c0ca6f9SAndy Whitcroft					 $op eq '&' or $op eq '^' or $op eq '|' or
22859c0ca6f9SAndy Whitcroft					 $op eq '+' or $op eq '-' or
2286c2fdda0dSAndy Whitcroft					 $op eq '*' or $op eq '/' or
2287c2fdda0dSAndy Whitcroft					 $op eq '%')
22880a920b5bSAndy Whitcroft				{
2289773647a0SAndy Whitcroft					if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
2290de7d4f0eSAndy Whitcroft						ERROR("need consistent spacing around '$op' $at\n" .
2291de7d4f0eSAndy Whitcroft							$hereptr);
22920a920b5bSAndy Whitcroft					}
22930a920b5bSAndy Whitcroft
22941f65f947SAndy Whitcroft				# A colon needs no spaces before when it is
22951f65f947SAndy Whitcroft				# terminating a case value or a label.
22961f65f947SAndy Whitcroft				} elsif ($opv eq ':C' || $opv eq ':L') {
22971f65f947SAndy Whitcroft					if ($ctx =~ /Wx./) {
22981f65f947SAndy Whitcroft						ERROR("space prohibited before that '$op' $at\n" . $hereptr);
22991f65f947SAndy Whitcroft					}
23001f65f947SAndy Whitcroft
23010a920b5bSAndy Whitcroft				# All the others need spaces both sides.
2302cf655043SAndy Whitcroft				} elsif ($ctx !~ /[EWC]x[CWE]/) {
23031f65f947SAndy Whitcroft					my $ok = 0;
23041f65f947SAndy Whitcroft
230522f2a2efSAndy Whitcroft					# Ignore email addresses <foo@bar>
23061f65f947SAndy Whitcroft					if (($op eq '<' &&
23071f65f947SAndy Whitcroft					     $cc =~ /^\S+\@\S+>/) ||
23081f65f947SAndy Whitcroft					    ($op eq '>' &&
23091f65f947SAndy Whitcroft					     $ca =~ /<\S+\@\S+$/))
23101f65f947SAndy Whitcroft					{
23111f65f947SAndy Whitcroft					    	$ok = 1;
23121f65f947SAndy Whitcroft					}
23131f65f947SAndy Whitcroft
23141f65f947SAndy Whitcroft					# Ignore ?:
23151f65f947SAndy Whitcroft					if (($opv eq ':O' && $ca =~ /\?$/) ||
23161f65f947SAndy Whitcroft					    ($op eq '?' && $cc =~ /^:/)) {
23171f65f947SAndy Whitcroft					    	$ok = 1;
23181f65f947SAndy Whitcroft					}
23191f65f947SAndy Whitcroft
23201f65f947SAndy Whitcroft					if ($ok == 0) {
2321773647a0SAndy Whitcroft						ERROR("spaces required around that '$op' $at\n" . $hereptr);
23220a920b5bSAndy Whitcroft					}
232322f2a2efSAndy Whitcroft				}
23244a0df2efSAndy Whitcroft				$off += length($elements[$n + 1]);
23250a920b5bSAndy Whitcroft			}
23260a920b5bSAndy Whitcroft		}
23270a920b5bSAndy Whitcroft
2328f0a594c1SAndy Whitcroft# check for multiple assignments
2329f0a594c1SAndy Whitcroft		if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
23306c72ffaaSAndy Whitcroft			CHK("multiple assignments should be avoided\n" . $herecurr);
2331f0a594c1SAndy Whitcroft		}
2332f0a594c1SAndy Whitcroft
233322f2a2efSAndy Whitcroft## # check for multiple declarations, allowing for a function declaration
233422f2a2efSAndy Whitcroft## # continuation.
233522f2a2efSAndy Whitcroft## 		if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
233622f2a2efSAndy Whitcroft## 		    $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
233722f2a2efSAndy Whitcroft##
233822f2a2efSAndy Whitcroft## 			# Remove any bracketed sections to ensure we do not
233922f2a2efSAndy Whitcroft## 			# falsly report the parameters of functions.
234022f2a2efSAndy Whitcroft## 			my $ln = $line;
234122f2a2efSAndy Whitcroft## 			while ($ln =~ s/\([^\(\)]*\)//g) {
234222f2a2efSAndy Whitcroft## 			}
234322f2a2efSAndy Whitcroft## 			if ($ln =~ /,/) {
234422f2a2efSAndy Whitcroft## 				WARN("declaring multiple variables together should be avoided\n" . $herecurr);
234522f2a2efSAndy Whitcroft## 			}
234622f2a2efSAndy Whitcroft## 		}
2347f0a594c1SAndy Whitcroft
23480a920b5bSAndy Whitcroft#need space before brace following if, while, etc
234922f2a2efSAndy Whitcroft		if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
235022f2a2efSAndy Whitcroft		    $line =~ /do{/) {
2351773647a0SAndy Whitcroft			ERROR("space required before the open brace '{'\n" . $herecurr);
2352de7d4f0eSAndy Whitcroft		}
2353de7d4f0eSAndy Whitcroft
2354de7d4f0eSAndy Whitcroft# closing brace should have a space following it when it has anything
2355de7d4f0eSAndy Whitcroft# on the line
2356de7d4f0eSAndy Whitcroft		if ($line =~ /}(?!(?:,|;|\)))\S/) {
2357773647a0SAndy Whitcroft			ERROR("space required after that close brace '}'\n" . $herecurr);
23580a920b5bSAndy Whitcroft		}
23590a920b5bSAndy Whitcroft
236022f2a2efSAndy Whitcroft# check spacing on square brackets
236122f2a2efSAndy Whitcroft		if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
2362773647a0SAndy Whitcroft			ERROR("space prohibited after that open square bracket '['\n" . $herecurr);
236322f2a2efSAndy Whitcroft		}
236422f2a2efSAndy Whitcroft		if ($line =~ /\s\]/) {
2365773647a0SAndy Whitcroft			ERROR("space prohibited before that close square bracket ']'\n" . $herecurr);
236622f2a2efSAndy Whitcroft		}
236722f2a2efSAndy Whitcroft
2368c45dcabdSAndy Whitcroft# check spacing on parentheses
23699c0ca6f9SAndy Whitcroft		if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
23709c0ca6f9SAndy Whitcroft		    $line !~ /for\s*\(\s+;/) {
2371773647a0SAndy Whitcroft			ERROR("space prohibited after that open parenthesis '('\n" . $herecurr);
237222f2a2efSAndy Whitcroft		}
237313214adfSAndy Whitcroft		if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
2374c45dcabdSAndy Whitcroft		    $line !~ /for\s*\(.*;\s+\)/ &&
2375c45dcabdSAndy Whitcroft		    $line !~ /:\s+\)/) {
2376773647a0SAndy Whitcroft			ERROR("space prohibited before that close parenthesis ')'\n" . $herecurr);
237722f2a2efSAndy Whitcroft		}
237822f2a2efSAndy Whitcroft
23790a920b5bSAndy Whitcroft#goto labels aren't indented, allow a single space however
23804a0df2efSAndy Whitcroft		if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
23810a920b5bSAndy Whitcroft		   !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
2382de7d4f0eSAndy Whitcroft			WARN("labels should not be indented\n" . $herecurr);
23830a920b5bSAndy Whitcroft		}
23840a920b5bSAndy Whitcroft
2385c45dcabdSAndy Whitcroft# Return is not a function.
2386c45dcabdSAndy Whitcroft		if (defined($stat) && $stat =~ /^.\s*return(\s*)(\(.*);/s) {
2387c45dcabdSAndy Whitcroft			my $spacing = $1;
2388c45dcabdSAndy Whitcroft			my $value = $2;
2389c45dcabdSAndy Whitcroft
239086f9d059SAndy Whitcroft			# Flatten any parentheses
2391fb2d2c1bSAndy Whitcroft			$value =~ s/\(/ \(/g;
2392fb2d2c1bSAndy Whitcroft			$value =~ s/\)/\) /g;
239363f17f89SAndy Whitcroft			while ($value =~ s/\[[^\{\}]*\]/1/ ||
239463f17f89SAndy Whitcroft			       $value !~ /(?:$Ident|-?$Constant)\s*
239563f17f89SAndy Whitcroft					     $Compare\s*
239663f17f89SAndy Whitcroft					     (?:$Ident|-?$Constant)/x &&
239763f17f89SAndy Whitcroft			       $value =~ s/\([^\(\)]*\)/1/) {
2398c45dcabdSAndy Whitcroft			}
2399fb2d2c1bSAndy Whitcroft#print "value<$value>\n";
2400fb2d2c1bSAndy Whitcroft			if ($value =~ /^\s*(?:$Ident|-?$Constant)\s*$/) {
2401c45dcabdSAndy Whitcroft				ERROR("return is not a function, parentheses are not required\n" . $herecurr);
2402c45dcabdSAndy Whitcroft
2403c45dcabdSAndy Whitcroft			} elsif ($spacing !~ /\s+/) {
2404c45dcabdSAndy Whitcroft				ERROR("space required before the open parenthesis '('\n" . $herecurr);
2405c45dcabdSAndy Whitcroft			}
2406c45dcabdSAndy Whitcroft		}
240753a3c448SAndy Whitcroft# Return of what appears to be an errno should normally be -'ve
240853a3c448SAndy Whitcroft		if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) {
240953a3c448SAndy Whitcroft			my $name = $1;
241053a3c448SAndy Whitcroft			if ($name ne 'EOF' && $name ne 'ERROR') {
241153a3c448SAndy Whitcroft				WARN("return of an errno should typically be -ve (return -$1)\n" . $herecurr);
241253a3c448SAndy Whitcroft			}
241353a3c448SAndy Whitcroft		}
2414c45dcabdSAndy Whitcroft
24157d2367afSJoe Perches# typecasts on min/max could be min_t/max_t
24167d2367afSJoe Perches		if ($line =~ /^\+(?:.*?)\b(min|max)\s*\($Typecast{0,1}($LvalOrFunc)\s*,\s*$Typecast{0,1}($LvalOrFunc)\s*\)/) {
24177d2367afSJoe Perches			if (defined $2 || defined $8) {
24187d2367afSJoe Perches				my $call = $1;
24197d2367afSJoe Perches				my $cast1 = deparenthesize($2);
24207d2367afSJoe Perches				my $arg1 = $3;
24217d2367afSJoe Perches				my $cast2 = deparenthesize($8);
24227d2367afSJoe Perches				my $arg2 = $9;
24237d2367afSJoe Perches				my $cast;
24247d2367afSJoe Perches
24257d2367afSJoe Perches				if ($cast1 ne "" && $cast2 ne "") {
24267d2367afSJoe Perches					$cast = "$cast1 or $cast2";
24277d2367afSJoe Perches				} elsif ($cast1 ne "") {
24287d2367afSJoe Perches					$cast = $cast1;
24297d2367afSJoe Perches				} else {
24307d2367afSJoe Perches					$cast = $cast2;
24317d2367afSJoe Perches				}
24327d2367afSJoe Perches				WARN("$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . $herecurr);
24337d2367afSJoe Perches			}
24347d2367afSJoe Perches		}
24357d2367afSJoe Perches
24360a920b5bSAndy Whitcroft# Need a space before open parenthesis after if, while etc
24374a0df2efSAndy Whitcroft		if ($line=~/\b(if|while|for|switch)\(/) {
2438773647a0SAndy Whitcroft			ERROR("space required before the open parenthesis '('\n" . $herecurr);
24390a920b5bSAndy Whitcroft		}
24400a920b5bSAndy Whitcroft
2441f5fe35ddSAndy Whitcroft# Check for illegal assignment in if conditional -- and check for trailing
2442f5fe35ddSAndy Whitcroft# statements after the conditional.
2443170d3a22SAndy Whitcroft		if ($line =~ /do\s*(?!{)/) {
2444170d3a22SAndy Whitcroft			my ($stat_next) = ctx_statement_block($line_nr_next,
2445170d3a22SAndy Whitcroft						$remain_next, $off_next);
2446170d3a22SAndy Whitcroft			$stat_next =~ s/\n./\n /g;
2447170d3a22SAndy Whitcroft			##print "stat<$stat> stat_next<$stat_next>\n";
2448170d3a22SAndy Whitcroft
2449170d3a22SAndy Whitcroft			if ($stat_next =~ /^\s*while\b/) {
2450170d3a22SAndy Whitcroft				# If the statement carries leading newlines,
2451170d3a22SAndy Whitcroft				# then count those as offsets.
2452170d3a22SAndy Whitcroft				my ($whitespace) =
2453170d3a22SAndy Whitcroft					($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
2454170d3a22SAndy Whitcroft				my $offset =
2455170d3a22SAndy Whitcroft					statement_rawlines($whitespace) - 1;
2456170d3a22SAndy Whitcroft
2457170d3a22SAndy Whitcroft				$suppress_whiletrailers{$line_nr_next +
2458170d3a22SAndy Whitcroft								$offset} = 1;
2459170d3a22SAndy Whitcroft			}
2460170d3a22SAndy Whitcroft		}
2461170d3a22SAndy Whitcroft		if (!defined $suppress_whiletrailers{$linenr} &&
2462170d3a22SAndy Whitcroft		    $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
2463171ae1a4SAndy Whitcroft			my ($s, $c) = ($stat, $cond);
24648905a67cSAndy Whitcroft
2465b53c8e10SAndy Whitcroft			if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
2466c2fdda0dSAndy Whitcroft				ERROR("do not use assignment in if condition\n" . $herecurr);
24678905a67cSAndy Whitcroft			}
24688905a67cSAndy Whitcroft
24698905a67cSAndy Whitcroft			# Find out what is on the end of the line after the
24708905a67cSAndy Whitcroft			# conditional.
2471773647a0SAndy Whitcroft			substr($s, 0, length($c), '');
24728905a67cSAndy Whitcroft			$s =~ s/\n.*//g;
247313214adfSAndy Whitcroft			$s =~ s/$;//g; 	# Remove any comments
247453210168SAndy Whitcroft			if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
247553210168SAndy Whitcroft			    $c !~ /}\s*while\s*/)
2476773647a0SAndy Whitcroft			{
2477bb44ad39SAndy Whitcroft				# Find out how long the conditional actually is.
2478bb44ad39SAndy Whitcroft				my @newlines = ($c =~ /\n/gs);
2479bb44ad39SAndy Whitcroft				my $cond_lines = 1 + $#newlines;
248042bdf74cSHidetoshi Seto				my $stat_real = '';
2481bb44ad39SAndy Whitcroft
248242bdf74cSHidetoshi Seto				$stat_real = raw_line($linenr, $cond_lines)
248342bdf74cSHidetoshi Seto							. "\n" if ($cond_lines);
2484bb44ad39SAndy Whitcroft				if (defined($stat_real) && $cond_lines > 1) {
2485bb44ad39SAndy Whitcroft					$stat_real = "[...]\n$stat_real";
2486bb44ad39SAndy Whitcroft				}
2487bb44ad39SAndy Whitcroft
2488bb44ad39SAndy Whitcroft				ERROR("trailing statements should be on next line\n" . $herecurr . $stat_real);
24898905a67cSAndy Whitcroft			}
24908905a67cSAndy Whitcroft		}
24918905a67cSAndy Whitcroft
249213214adfSAndy Whitcroft# Check for bitwise tests written as boolean
249313214adfSAndy Whitcroft		if ($line =~ /
249413214adfSAndy Whitcroft			(?:
249513214adfSAndy Whitcroft				(?:\[|\(|\&\&|\|\|)
249613214adfSAndy Whitcroft				\s*0[xX][0-9]+\s*
249713214adfSAndy Whitcroft				(?:\&\&|\|\|)
249813214adfSAndy Whitcroft			|
249913214adfSAndy Whitcroft				(?:\&\&|\|\|)
250013214adfSAndy Whitcroft				\s*0[xX][0-9]+\s*
250113214adfSAndy Whitcroft				(?:\&\&|\|\||\)|\])
250213214adfSAndy Whitcroft			)/x)
250313214adfSAndy Whitcroft		{
250413214adfSAndy Whitcroft			WARN("boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
250513214adfSAndy Whitcroft		}
250613214adfSAndy Whitcroft
25078905a67cSAndy Whitcroft# if and else should not have general statements after it
250813214adfSAndy Whitcroft		if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
250913214adfSAndy Whitcroft			my $s = $1;
251013214adfSAndy Whitcroft			$s =~ s/$;//g; 	# Remove any comments
251113214adfSAndy Whitcroft			if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
25128905a67cSAndy Whitcroft				ERROR("trailing statements should be on next line\n" . $herecurr);
25130a920b5bSAndy Whitcroft			}
251413214adfSAndy Whitcroft		}
251539667782SAndy Whitcroft# if should not continue a brace
251639667782SAndy Whitcroft		if ($line =~ /}\s*if\b/) {
251739667782SAndy Whitcroft			ERROR("trailing statements should be on next line\n" .
251839667782SAndy Whitcroft				$herecurr);
251939667782SAndy Whitcroft		}
2520a1080bf8SAndy Whitcroft# case and default should not have general statements after them
2521a1080bf8SAndy Whitcroft		if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
2522a1080bf8SAndy Whitcroft		    $line !~ /\G(?:
25233fef12d6SAndy Whitcroft			(?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
2524a1080bf8SAndy Whitcroft			\s*return\s+
2525a1080bf8SAndy Whitcroft		    )/xg)
2526a1080bf8SAndy Whitcroft		{
2527a1080bf8SAndy Whitcroft			ERROR("trailing statements should be on next line\n" . $herecurr);
2528a1080bf8SAndy Whitcroft		}
25290a920b5bSAndy Whitcroft
25300a920b5bSAndy Whitcroft		# Check for }<nl>else {, these must be at the same
25310a920b5bSAndy Whitcroft		# indent level to be relevant to each other.
25320a920b5bSAndy Whitcroft		if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
25330a920b5bSAndy Whitcroft						$previndent == $indent) {
2534de7d4f0eSAndy Whitcroft			ERROR("else should follow close brace '}'\n" . $hereprev);
25350a920b5bSAndy Whitcroft		}
25360a920b5bSAndy Whitcroft
2537c2fdda0dSAndy Whitcroft		if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and
2538c2fdda0dSAndy Whitcroft						$previndent == $indent) {
2539c2fdda0dSAndy Whitcroft			my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
2540c2fdda0dSAndy Whitcroft
2541c2fdda0dSAndy Whitcroft			# Find out what is on the end of the line after the
2542c2fdda0dSAndy Whitcroft			# conditional.
2543773647a0SAndy Whitcroft			substr($s, 0, length($c), '');
2544c2fdda0dSAndy Whitcroft			$s =~ s/\n.*//g;
2545c2fdda0dSAndy Whitcroft
2546c2fdda0dSAndy Whitcroft			if ($s =~ /^\s*;/) {
2547c2fdda0dSAndy Whitcroft				ERROR("while should follow close brace '}'\n" . $hereprev);
2548c2fdda0dSAndy Whitcroft			}
2549c2fdda0dSAndy Whitcroft		}
2550c2fdda0dSAndy Whitcroft
25510a920b5bSAndy Whitcroft#studly caps, commented out until figure out how to distinguish between use of existing and adding new
25520a920b5bSAndy Whitcroft#		if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) {
25530a920b5bSAndy Whitcroft#		    print "No studly caps, use _\n";
25540a920b5bSAndy Whitcroft#		    print "$herecurr";
25550a920b5bSAndy Whitcroft#		    $clean = 0;
25560a920b5bSAndy Whitcroft#		}
25570a920b5bSAndy Whitcroft
25580a920b5bSAndy Whitcroft#no spaces allowed after \ in define
2559c45dcabdSAndy Whitcroft		if ($line=~/\#\s*define.*\\\s$/) {
2560de7d4f0eSAndy Whitcroft			WARN("Whitepspace after \\ makes next lines useless\n" . $herecurr);
25610a920b5bSAndy Whitcroft		}
25620a920b5bSAndy Whitcroft
2563653d4876SAndy Whitcroft#warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
2564c45dcabdSAndy Whitcroft		if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
2565e09dec48SAndy Whitcroft			my $file = "$1.h";
2566e09dec48SAndy Whitcroft			my $checkfile = "include/linux/$file";
2567e09dec48SAndy Whitcroft			if (-f "$root/$checkfile" &&
2568e09dec48SAndy Whitcroft			    $realfile ne $checkfile &&
25697840a94cSWolfram Sang			    $1 !~ /$allowed_asm_includes/)
2570c45dcabdSAndy Whitcroft			{
2571e09dec48SAndy Whitcroft				if ($realfile =~ m{^arch/}) {
2572e09dec48SAndy Whitcroft					CHK("Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
2573e09dec48SAndy Whitcroft				} else {
2574e09dec48SAndy Whitcroft					WARN("Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
2575e09dec48SAndy Whitcroft				}
25760a920b5bSAndy Whitcroft			}
25770a920b5bSAndy Whitcroft		}
25780a920b5bSAndy Whitcroft
2579653d4876SAndy Whitcroft# multi-statement macros should be enclosed in a do while loop, grab the
2580653d4876SAndy Whitcroft# first statement and ensure its the whole macro if its not enclosed
2581cf655043SAndy Whitcroft# in a known good container
2582b8f96a31SAndy Whitcroft		if ($realfile !~ m@/vmlinux.lds.h$@ &&
2583b8f96a31SAndy Whitcroft		    $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
2584d8aaf121SAndy Whitcroft			my $ln = $linenr;
2585d8aaf121SAndy Whitcroft			my $cnt = $realcnt;
2586c45dcabdSAndy Whitcroft			my ($off, $dstat, $dcond, $rest);
2587c45dcabdSAndy Whitcroft			my $ctx = '';
2588653d4876SAndy Whitcroft
2589c45dcabdSAndy Whitcroft			my $args = defined($1);
2590de7d4f0eSAndy Whitcroft
2591c45dcabdSAndy Whitcroft			# Find the end of the macro and limit our statement
2592c45dcabdSAndy Whitcroft			# search to that.
2593c45dcabdSAndy Whitcroft			while ($cnt > 0 && defined $lines[$ln - 1] &&
2594c45dcabdSAndy Whitcroft				$lines[$ln - 1] =~ /^(?:-|..*\\$)/)
2595c45dcabdSAndy Whitcroft			{
2596c45dcabdSAndy Whitcroft				$ctx .= $rawlines[$ln - 1] . "\n";
2597a3bb97a7SAndy Whitcroft				$cnt-- if ($lines[$ln - 1] !~ /^-/);
2598c45dcabdSAndy Whitcroft				$ln++;
2599de7d4f0eSAndy Whitcroft			}
2600c45dcabdSAndy Whitcroft			$ctx .= $rawlines[$ln - 1];
2601d8aaf121SAndy Whitcroft
2602c45dcabdSAndy Whitcroft			($dstat, $dcond, $ln, $cnt, $off) =
2603c45dcabdSAndy Whitcroft				ctx_statement_block($linenr, $ln - $linenr + 1, 0);
2604c45dcabdSAndy Whitcroft			#print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
2605a3bb97a7SAndy Whitcroft			#print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
2606c45dcabdSAndy Whitcroft
2607c45dcabdSAndy Whitcroft			# Extract the remainder of the define (if any) and
2608c45dcabdSAndy Whitcroft			# rip off surrounding spaces, and trailing \'s.
2609c45dcabdSAndy Whitcroft			$rest = '';
2610636d140aSAndy Whitcroft			while ($off != 0 || ($cnt > 0 && $rest =~ /\\\s*$/)) {
2611636d140aSAndy Whitcroft				#print "ADDING cnt<$cnt> $off <" . substr($lines[$ln - 1], $off) . "> rest<$rest>\n";
2612a3bb97a7SAndy Whitcroft				if ($off != 0 || $lines[$ln - 1] !~ /^-/) {
2613c45dcabdSAndy Whitcroft					$rest .= substr($lines[$ln - 1], $off) . "\n";
2614c45dcabdSAndy Whitcroft					$cnt--;
2615a3bb97a7SAndy Whitcroft				}
2616a3bb97a7SAndy Whitcroft				$ln++;
2617c45dcabdSAndy Whitcroft				$off = 0;
2618c45dcabdSAndy Whitcroft			}
2619c45dcabdSAndy Whitcroft			$rest =~ s/\\\n.//g;
2620c45dcabdSAndy Whitcroft			$rest =~ s/^\s*//s;
2621c45dcabdSAndy Whitcroft			$rest =~ s/\s*$//s;
2622c45dcabdSAndy Whitcroft
2623c45dcabdSAndy Whitcroft			# Clean up the original statement.
2624c45dcabdSAndy Whitcroft			if ($args) {
2625c45dcabdSAndy Whitcroft				substr($dstat, 0, length($dcond), '');
2626d8aaf121SAndy Whitcroft			} else {
2627c45dcabdSAndy Whitcroft				$dstat =~ s/^.\s*\#\s*define\s+$Ident\s*//;
2628c45dcabdSAndy Whitcroft			}
2629292f1a9bSAndy Whitcroft			$dstat =~ s/$;//g;
2630c45dcabdSAndy Whitcroft			$dstat =~ s/\\\n.//g;
2631c45dcabdSAndy Whitcroft			$dstat =~ s/^\s*//s;
2632c45dcabdSAndy Whitcroft			$dstat =~ s/\s*$//s;
2633c45dcabdSAndy Whitcroft
2634c45dcabdSAndy Whitcroft			# Flatten any parentheses and braces
2635bf30d6edSAndy Whitcroft			while ($dstat =~ s/\([^\(\)]*\)/1/ ||
2636bf30d6edSAndy Whitcroft			       $dstat =~ s/\{[^\{\}]*\}/1/ ||
2637bf30d6edSAndy Whitcroft			       $dstat =~ s/\[[^\{\}]*\]/1/)
2638bf30d6edSAndy Whitcroft			{
2639c45dcabdSAndy Whitcroft			}
2640c45dcabdSAndy Whitcroft
2641c45dcabdSAndy Whitcroft			my $exceptions = qr{
2642c45dcabdSAndy Whitcroft				$Declare|
2643c45dcabdSAndy Whitcroft				module_param_named|
2644c45dcabdSAndy Whitcroft				MODULE_PARAM_DESC|
2645c45dcabdSAndy Whitcroft				DECLARE_PER_CPU|
2646c45dcabdSAndy Whitcroft				DEFINE_PER_CPU|
2647383099fdSAndy Whitcroft				__typeof__\(|
264822fd2d3eSStefani Seibold				union|
264922fd2d3eSStefani Seibold				struct|
2650ea71a0a0SAndy Whitcroft				\.$Ident\s*=\s*|
2651ea71a0a0SAndy Whitcroft				^\"|\"$
2652c45dcabdSAndy Whitcroft			}x;
26535eaa20b9SAndy Whitcroft			#print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
26545eaa20b9SAndy Whitcroft			if ($rest ne '' && $rest ne ',') {
2655c45dcabdSAndy Whitcroft				if ($rest !~ /while\s*\(/ &&
2656c45dcabdSAndy Whitcroft				    $dstat !~ /$exceptions/)
2657c45dcabdSAndy Whitcroft				{
2658c45dcabdSAndy Whitcroft					ERROR("Macros with multiple statements should be enclosed in a do - while loop\n" . "$here\n$ctx\n");
2659c45dcabdSAndy Whitcroft				}
2660c45dcabdSAndy Whitcroft
2661c45dcabdSAndy Whitcroft			} elsif ($ctx !~ /;/) {
2662c45dcabdSAndy Whitcroft				if ($dstat ne '' &&
2663c45dcabdSAndy Whitcroft				    $dstat !~ /^(?:$Ident|-?$Constant)$/ &&
2664c45dcabdSAndy Whitcroft				    $dstat !~ /$exceptions/ &&
2665b132e5d5SAndy Whitcroft				    $dstat !~ /^\.$Ident\s*=/ &&
2666c45dcabdSAndy Whitcroft				    $dstat =~ /$Operators/)
2667c45dcabdSAndy Whitcroft				{
2668de7d4f0eSAndy Whitcroft					ERROR("Macros with complex values should be enclosed in parenthesis\n" . "$here\n$ctx\n");
2669d8aaf121SAndy Whitcroft				}
26700a920b5bSAndy Whitcroft			}
2671653d4876SAndy Whitcroft		}
26720a920b5bSAndy Whitcroft
2673080ba929SMike Frysinger# make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
2674080ba929SMike Frysinger# all assignments may have only one of the following with an assignment:
2675080ba929SMike Frysinger#	.
2676080ba929SMike Frysinger#	ALIGN(...)
2677080ba929SMike Frysinger#	VMLINUX_SYMBOL(...)
2678080ba929SMike Frysinger		if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
2679080ba929SMike Frysinger			WARN("vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
2680080ba929SMike Frysinger		}
2681080ba929SMike Frysinger
2682f0a594c1SAndy Whitcroft# check for redundant bracing round if etc
268313214adfSAndy Whitcroft		if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
268413214adfSAndy Whitcroft			my ($level, $endln, @chunks) =
2685cf655043SAndy Whitcroft				ctx_statement_full($linenr, $realcnt, 1);
268613214adfSAndy Whitcroft			#print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
2687cf655043SAndy Whitcroft			#print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
2688cf655043SAndy Whitcroft			if ($#chunks > 0 && $level == 0) {
268913214adfSAndy Whitcroft				my $allowed = 0;
269013214adfSAndy Whitcroft				my $seen = 0;
2691773647a0SAndy Whitcroft				my $herectx = $here . "\n";
2692cf655043SAndy Whitcroft				my $ln = $linenr - 1;
269313214adfSAndy Whitcroft				for my $chunk (@chunks) {
269413214adfSAndy Whitcroft					my ($cond, $block) = @{$chunk};
269513214adfSAndy Whitcroft
2696773647a0SAndy Whitcroft					# If the condition carries leading newlines, then count those as offsets.
2697773647a0SAndy Whitcroft					my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
2698773647a0SAndy Whitcroft					my $offset = statement_rawlines($whitespace) - 1;
2699773647a0SAndy Whitcroft
2700773647a0SAndy Whitcroft					#print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
2701773647a0SAndy Whitcroft
2702773647a0SAndy Whitcroft					# We have looked at and allowed this specific line.
2703773647a0SAndy Whitcroft					$suppress_ifbraces{$ln + $offset} = 1;
2704773647a0SAndy Whitcroft
2705773647a0SAndy Whitcroft					$herectx .= "$rawlines[$ln + $offset]\n[...]\n";
2706cf655043SAndy Whitcroft					$ln += statement_rawlines($block) - 1;
2707cf655043SAndy Whitcroft
2708773647a0SAndy Whitcroft					substr($block, 0, length($cond), '');
270913214adfSAndy Whitcroft
271013214adfSAndy Whitcroft					$seen++ if ($block =~ /^\s*{/);
271113214adfSAndy Whitcroft
2712cf655043SAndy Whitcroft					#print "cond<$cond> block<$block> allowed<$allowed>\n";
2713cf655043SAndy Whitcroft					if (statement_lines($cond) > 1) {
2714cf655043SAndy Whitcroft						#print "APW: ALLOWED: cond<$cond>\n";
271513214adfSAndy Whitcroft						$allowed = 1;
271613214adfSAndy Whitcroft					}
271713214adfSAndy Whitcroft					if ($block =~/\b(?:if|for|while)\b/) {
2718cf655043SAndy Whitcroft						#print "APW: ALLOWED: block<$block>\n";
271913214adfSAndy Whitcroft						$allowed = 1;
272013214adfSAndy Whitcroft					}
2721cf655043SAndy Whitcroft					if (statement_block_size($block) > 1) {
2722cf655043SAndy Whitcroft						#print "APW: ALLOWED: lines block<$block>\n";
272313214adfSAndy Whitcroft						$allowed = 1;
272413214adfSAndy Whitcroft					}
272513214adfSAndy Whitcroft				}
272613214adfSAndy Whitcroft				if ($seen && !$allowed) {
2727cf655043SAndy Whitcroft					WARN("braces {} are not necessary for any arm of this statement\n" . $herectx);
272813214adfSAndy Whitcroft				}
272913214adfSAndy Whitcroft			}
273013214adfSAndy Whitcroft		}
2731773647a0SAndy Whitcroft		if (!defined $suppress_ifbraces{$linenr - 1} &&
273213214adfSAndy Whitcroft					$line =~ /\b(if|while|for|else)\b/) {
2733cf655043SAndy Whitcroft			my $allowed = 0;
2734f0a594c1SAndy Whitcroft
2735cf655043SAndy Whitcroft			# Check the pre-context.
2736cf655043SAndy Whitcroft			if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
2737cf655043SAndy Whitcroft				#print "APW: ALLOWED: pre<$1>\n";
2738cf655043SAndy Whitcroft				$allowed = 1;
2739f0a594c1SAndy Whitcroft			}
2740773647a0SAndy Whitcroft
2741773647a0SAndy Whitcroft			my ($level, $endln, @chunks) =
2742773647a0SAndy Whitcroft				ctx_statement_full($linenr, $realcnt, $-[0]);
2743773647a0SAndy Whitcroft
2744cf655043SAndy Whitcroft			# Check the condition.
2745cf655043SAndy Whitcroft			my ($cond, $block) = @{$chunks[0]};
2746773647a0SAndy Whitcroft			#print "CHECKING<$linenr> cond<$cond> block<$block>\n";
2747cf655043SAndy Whitcroft			if (defined $cond) {
2748773647a0SAndy Whitcroft				substr($block, 0, length($cond), '');
2749cf655043SAndy Whitcroft			}
2750cf655043SAndy Whitcroft			if (statement_lines($cond) > 1) {
2751cf655043SAndy Whitcroft				#print "APW: ALLOWED: cond<$cond>\n";
2752cf655043SAndy Whitcroft				$allowed = 1;
2753cf655043SAndy Whitcroft			}
2754cf655043SAndy Whitcroft			if ($block =~/\b(?:if|for|while)\b/) {
2755cf655043SAndy Whitcroft				#print "APW: ALLOWED: block<$block>\n";
2756cf655043SAndy Whitcroft				$allowed = 1;
2757cf655043SAndy Whitcroft			}
2758cf655043SAndy Whitcroft			if (statement_block_size($block) > 1) {
2759cf655043SAndy Whitcroft				#print "APW: ALLOWED: lines block<$block>\n";
2760cf655043SAndy Whitcroft				$allowed = 1;
2761cf655043SAndy Whitcroft			}
2762cf655043SAndy Whitcroft			# Check the post-context.
2763cf655043SAndy Whitcroft			if (defined $chunks[1]) {
2764cf655043SAndy Whitcroft				my ($cond, $block) = @{$chunks[1]};
2765cf655043SAndy Whitcroft				if (defined $cond) {
2766773647a0SAndy Whitcroft					substr($block, 0, length($cond), '');
2767cf655043SAndy Whitcroft				}
2768cf655043SAndy Whitcroft				if ($block =~ /^\s*\{/) {
2769cf655043SAndy Whitcroft					#print "APW: ALLOWED: chunk-1 block<$block>\n";
2770cf655043SAndy Whitcroft					$allowed = 1;
2771cf655043SAndy Whitcroft				}
2772cf655043SAndy Whitcroft			}
2773cf655043SAndy Whitcroft			if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
2774cf655043SAndy Whitcroft				my $herectx = $here . "\n";;
2775f055663cSAndy Whitcroft				my $cnt = statement_rawlines($block);
2776cf655043SAndy Whitcroft
2777f055663cSAndy Whitcroft				for (my $n = 0; $n < $cnt; $n++) {
2778f055663cSAndy Whitcroft					$herectx .= raw_line($linenr, $n) . "\n";;
2779cf655043SAndy Whitcroft				}
2780cf655043SAndy Whitcroft
2781cf655043SAndy Whitcroft				WARN("braces {} are not necessary for single statement blocks\n" . $herectx);
2782f0a594c1SAndy Whitcroft			}
2783f0a594c1SAndy Whitcroft		}
2784f0a594c1SAndy Whitcroft
2785653d4876SAndy Whitcroft# don't include deprecated include files (uses RAW line)
27864a0df2efSAndy Whitcroft		for my $inc (@dep_includes) {
2787c45dcabdSAndy Whitcroft			if ($rawline =~ m@^.\s*\#\s*include\s*\<$inc>@) {
2788de7d4f0eSAndy Whitcroft				ERROR("Don't use <$inc>: see Documentation/feature-removal-schedule.txt\n" . $herecurr);
27890a920b5bSAndy Whitcroft			}
27900a920b5bSAndy Whitcroft		}
27910a920b5bSAndy Whitcroft
27924a0df2efSAndy Whitcroft# don't use deprecated functions
27934a0df2efSAndy Whitcroft		for my $func (@dep_functions) {
279400df344fSAndy Whitcroft			if ($line =~ /\b$func\b/) {
2795de7d4f0eSAndy Whitcroft				ERROR("Don't use $func(): see Documentation/feature-removal-schedule.txt\n" . $herecurr);
27964a0df2efSAndy Whitcroft			}
27974a0df2efSAndy Whitcroft		}
27984a0df2efSAndy Whitcroft
27994a0df2efSAndy Whitcroft# no volatiles please
28006c72ffaaSAndy Whitcroft		my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
28016c72ffaaSAndy Whitcroft		if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
2802de7d4f0eSAndy Whitcroft			WARN("Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
28034a0df2efSAndy Whitcroft		}
28044a0df2efSAndy Whitcroft
280500df344fSAndy Whitcroft# warn about #if 0
2806c45dcabdSAndy Whitcroft		if ($line =~ /^.\s*\#\s*if\s+0\b/) {
2807de7d4f0eSAndy Whitcroft			CHK("if this code is redundant consider removing it\n" .
2808de7d4f0eSAndy Whitcroft				$herecurr);
28094a0df2efSAndy Whitcroft		}
28104a0df2efSAndy Whitcroft
2811f0a594c1SAndy Whitcroft# check for needless kfree() checks
2812f0a594c1SAndy Whitcroft		if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
2813f0a594c1SAndy Whitcroft			my $expr = $1;
2814f0a594c1SAndy Whitcroft			if ($line =~ /\bkfree\(\Q$expr\E\);/) {
28153c232147SWolfram Sang				WARN("kfree(NULL) is safe this check is probably not required\n" . $hereprev);
2816f0a594c1SAndy Whitcroft			}
2817f0a594c1SAndy Whitcroft		}
28184c432a8fSGreg Kroah-Hartman# check for needless usb_free_urb() checks
28194c432a8fSGreg Kroah-Hartman		if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
28204c432a8fSGreg Kroah-Hartman			my $expr = $1;
28214c432a8fSGreg Kroah-Hartman			if ($line =~ /\busb_free_urb\(\Q$expr\E\);/) {
28224c432a8fSGreg Kroah-Hartman				WARN("usb_free_urb(NULL) is safe this check is probably not required\n" . $hereprev);
28234c432a8fSGreg Kroah-Hartman			}
28244c432a8fSGreg Kroah-Hartman		}
2825f0a594c1SAndy Whitcroft
28261a15a250SPatrick Pannuto# prefer usleep_range over udelay
28271a15a250SPatrick Pannuto		if ($line =~ /\budelay\s*\(\s*(\w+)\s*\)/) {
28281a15a250SPatrick Pannuto			# ignore udelay's < 10, however
28291a15a250SPatrick Pannuto			if (! (($1 =~ /(\d+)/) && ($1 < 10)) ) {
28301a15a250SPatrick Pannuto				CHK("usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $line);
28311a15a250SPatrick Pannuto			}
28321a15a250SPatrick Pannuto		}
28331a15a250SPatrick Pannuto
283409ef8725SPatrick Pannuto# warn about unexpectedly long msleep's
283509ef8725SPatrick Pannuto		if ($line =~ /\bmsleep\s*\((\d+)\);/) {
283609ef8725SPatrick Pannuto			if ($1 < 20) {
283709ef8725SPatrick Pannuto				WARN("msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $line);
283809ef8725SPatrick Pannuto			}
283909ef8725SPatrick Pannuto		}
284009ef8725SPatrick Pannuto
284100df344fSAndy Whitcroft# warn about #ifdefs in C files
2842c45dcabdSAndy Whitcroft#		if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
284300df344fSAndy Whitcroft#			print "#ifdef in C files should be avoided\n";
284400df344fSAndy Whitcroft#			print "$herecurr";
284500df344fSAndy Whitcroft#			$clean = 0;
284600df344fSAndy Whitcroft#		}
284700df344fSAndy Whitcroft
284822f2a2efSAndy Whitcroft# warn about spacing in #ifdefs
2849c45dcabdSAndy Whitcroft		if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
285022f2a2efSAndy Whitcroft			ERROR("exactly one space required after that #$1\n" . $herecurr);
285122f2a2efSAndy Whitcroft		}
285222f2a2efSAndy Whitcroft
28534a0df2efSAndy Whitcroft# check for spinlock_t definitions without a comment.
2854171ae1a4SAndy Whitcroft		if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
2855171ae1a4SAndy Whitcroft		    $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
28564a0df2efSAndy Whitcroft			my $which = $1;
28574a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
2858de7d4f0eSAndy Whitcroft				CHK("$1 definition without comment\n" . $herecurr);
28594a0df2efSAndy Whitcroft			}
28604a0df2efSAndy Whitcroft		}
28614a0df2efSAndy Whitcroft# check for memory barriers without a comment.
28624a0df2efSAndy Whitcroft		if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
28634a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
2864de7d4f0eSAndy Whitcroft				CHK("memory barrier without comment\n" . $herecurr);
28654a0df2efSAndy Whitcroft			}
28664a0df2efSAndy Whitcroft		}
28674a0df2efSAndy Whitcroft# check of hardware specific defines
2868c45dcabdSAndy Whitcroft		if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
2869de7d4f0eSAndy Whitcroft			CHK("architecture specific defines should be avoided\n" .  $herecurr);
28700a920b5bSAndy Whitcroft		}
2871653d4876SAndy Whitcroft
2872d4977c78STobias Klauser# Check that the storage class is at the beginning of a declaration
2873d4977c78STobias Klauser		if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
2874d4977c78STobias Klauser			WARN("storage class should be at the beginning of the declaration\n" . $herecurr)
2875d4977c78STobias Klauser		}
2876d4977c78STobias Klauser
2877de7d4f0eSAndy Whitcroft# check the location of the inline attribute, that it is between
2878de7d4f0eSAndy Whitcroft# storage class and type.
28799c0ca6f9SAndy Whitcroft		if ($line =~ /\b$Type\s+$Inline\b/ ||
28809c0ca6f9SAndy Whitcroft		    $line =~ /\b$Inline\s+$Storage\b/) {
2881de7d4f0eSAndy Whitcroft			ERROR("inline keyword should sit between storage class and type\n" . $herecurr);
2882de7d4f0eSAndy Whitcroft		}
2883de7d4f0eSAndy Whitcroft
28848905a67cSAndy Whitcroft# Check for __inline__ and __inline, prefer inline
28858905a67cSAndy Whitcroft		if ($line =~ /\b(__inline__|__inline)\b/) {
28868905a67cSAndy Whitcroft			WARN("plain inline is preferred over $1\n" . $herecurr);
28878905a67cSAndy Whitcroft		}
28888905a67cSAndy Whitcroft
28893d130fd0SJoe Perches# Check for __attribute__ packed, prefer __packed
28903d130fd0SJoe Perches		if ($line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
28913d130fd0SJoe Perches			WARN("__packed is preferred over __attribute__((packed))\n" . $herecurr);
28923d130fd0SJoe Perches		}
28933d130fd0SJoe Perches
28948f53a9b8SJoe Perches# check for sizeof(&)
28958f53a9b8SJoe Perches		if ($line =~ /\bsizeof\s*\(\s*\&/) {
28968f53a9b8SJoe Perches			WARN("sizeof(& should be avoided\n" . $herecurr);
28978f53a9b8SJoe Perches		}
28988f53a9b8SJoe Perches
2899428e2fdcSJoe Perches# check for line continuations in quoted strings with odd counts of "
2900428e2fdcSJoe Perches		if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) {
2901428e2fdcSJoe Perches			WARN("Avoid line continuations in quoted strings\n" . $herecurr);
2902428e2fdcSJoe Perches		}
2903428e2fdcSJoe Perches
2904de7d4f0eSAndy Whitcroft# check for new externs in .c files.
2905171ae1a4SAndy Whitcroft		if ($realfile =~ /\.c$/ && defined $stat &&
2906c45dcabdSAndy Whitcroft		    $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
2907171ae1a4SAndy Whitcroft		{
2908c45dcabdSAndy Whitcroft			my $function_name = $1;
2909c45dcabdSAndy Whitcroft			my $paren_space = $2;
2910171ae1a4SAndy Whitcroft
2911171ae1a4SAndy Whitcroft			my $s = $stat;
2912171ae1a4SAndy Whitcroft			if (defined $cond) {
2913171ae1a4SAndy Whitcroft				substr($s, 0, length($cond), '');
2914171ae1a4SAndy Whitcroft			}
2915c45dcabdSAndy Whitcroft			if ($s =~ /^\s*;/ &&
2916c45dcabdSAndy Whitcroft			    $function_name ne 'uninitialized_var')
2917c45dcabdSAndy Whitcroft			{
2918de7d4f0eSAndy Whitcroft				WARN("externs should be avoided in .c files\n" .  $herecurr);
2919de7d4f0eSAndy Whitcroft			}
2920de7d4f0eSAndy Whitcroft
2921171ae1a4SAndy Whitcroft			if ($paren_space =~ /\n/) {
2922171ae1a4SAndy Whitcroft				WARN("arguments for function declarations should follow identifier\n" . $herecurr);
2923171ae1a4SAndy Whitcroft			}
29249c9ba34eSAndy Whitcroft
29259c9ba34eSAndy Whitcroft		} elsif ($realfile =~ /\.c$/ && defined $stat &&
29269c9ba34eSAndy Whitcroft		    $stat =~ /^.\s*extern\s+/)
29279c9ba34eSAndy Whitcroft		{
29289c9ba34eSAndy Whitcroft			WARN("externs should be avoided in .c files\n" .  $herecurr);
2929171ae1a4SAndy Whitcroft		}
2930171ae1a4SAndy Whitcroft
2931de7d4f0eSAndy Whitcroft# checks for new __setup's
2932de7d4f0eSAndy Whitcroft		if ($rawline =~ /\b__setup\("([^"]*)"/) {
2933de7d4f0eSAndy Whitcroft			my $name = $1;
2934de7d4f0eSAndy Whitcroft
2935de7d4f0eSAndy Whitcroft			if (!grep(/$name/, @setup_docs)) {
2936de7d4f0eSAndy Whitcroft				CHK("__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
2937de7d4f0eSAndy Whitcroft			}
2938653d4876SAndy Whitcroft		}
29399c0ca6f9SAndy Whitcroft
29409c0ca6f9SAndy Whitcroft# check for pointless casting of kmalloc return
2941caf2a54fSJoe Perches		if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) {
29429c0ca6f9SAndy Whitcroft			WARN("unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
29439c0ca6f9SAndy Whitcroft		}
294413214adfSAndy Whitcroft
2945caf2a54fSJoe Perches# check for multiple semicolons
2946caf2a54fSJoe Perches		if ($line =~ /;\s*;\s*$/) {
2947caf2a54fSJoe Perches		    WARN("Statements terminations use 1 semicolon\n" . $herecurr);
2948caf2a54fSJoe Perches		}
2949caf2a54fSJoe Perches
295013214adfSAndy Whitcroft# check for gcc specific __FUNCTION__
295113214adfSAndy Whitcroft		if ($line =~ /__FUNCTION__/) {
295213214adfSAndy Whitcroft			WARN("__func__ should be used instead of gcc specific __FUNCTION__\n"  . $herecurr);
295313214adfSAndy Whitcroft		}
2954773647a0SAndy Whitcroft
29554882720bSThomas Gleixner# check for semaphores initialized locked
29564882720bSThomas Gleixner		if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
2957773647a0SAndy Whitcroft			WARN("consider using a completion\n" . $herecurr);
29581704f47bSPeter Zijlstra
2959773647a0SAndy Whitcroft		}
296033ee3b2eSAlexey Dobriyan# recommend kstrto* over simple_strto*
2961773647a0SAndy Whitcroft		if ($line =~ /\bsimple_(strto.*?)\s*\(/) {
296233ee3b2eSAlexey Dobriyan			WARN("consider using kstrto* in preference to simple_$1\n" . $herecurr);
2963773647a0SAndy Whitcroft		}
2964f3db6639SMichael Ellerman# check for __initcall(), use device_initcall() explicitly please
2965f3db6639SMichael Ellerman		if ($line =~ /^.\s*__initcall\s*\(/) {
2966f3db6639SMichael Ellerman			WARN("please use device_initcall() instead of __initcall()\n" . $herecurr);
2967f3db6639SMichael Ellerman		}
296879404849SEmese Revfy# check for various ops structs, ensure they are const.
296979404849SEmese Revfy		my $struct_ops = qr{acpi_dock_ops|
297079404849SEmese Revfy				address_space_operations|
297179404849SEmese Revfy				backlight_ops|
297279404849SEmese Revfy				block_device_operations|
297379404849SEmese Revfy				dentry_operations|
297479404849SEmese Revfy				dev_pm_ops|
297579404849SEmese Revfy				dma_map_ops|
297679404849SEmese Revfy				extent_io_ops|
297779404849SEmese Revfy				file_lock_operations|
297879404849SEmese Revfy				file_operations|
297979404849SEmese Revfy				hv_ops|
298079404849SEmese Revfy				ide_dma_ops|
298179404849SEmese Revfy				intel_dvo_dev_ops|
298279404849SEmese Revfy				item_operations|
298379404849SEmese Revfy				iwl_ops|
298479404849SEmese Revfy				kgdb_arch|
298579404849SEmese Revfy				kgdb_io|
298679404849SEmese Revfy				kset_uevent_ops|
298779404849SEmese Revfy				lock_manager_operations|
298879404849SEmese Revfy				microcode_ops|
298979404849SEmese Revfy				mtrr_ops|
299079404849SEmese Revfy				neigh_ops|
299179404849SEmese Revfy				nlmsvc_binding|
299279404849SEmese Revfy				pci_raw_ops|
299379404849SEmese Revfy				pipe_buf_operations|
299479404849SEmese Revfy				platform_hibernation_ops|
299579404849SEmese Revfy				platform_suspend_ops|
299679404849SEmese Revfy				proto_ops|
299779404849SEmese Revfy				rpc_pipe_ops|
299879404849SEmese Revfy				seq_operations|
299979404849SEmese Revfy				snd_ac97_build_ops|
300079404849SEmese Revfy				soc_pcmcia_socket_ops|
300179404849SEmese Revfy				stacktrace_ops|
300279404849SEmese Revfy				sysfs_ops|
300379404849SEmese Revfy				tty_operations|
300479404849SEmese Revfy				usb_mon_operations|
300579404849SEmese Revfy				wd_ops}x;
30066903ffb2SAndy Whitcroft		if ($line !~ /\bconst\b/ &&
300779404849SEmese Revfy		    $line =~ /\bstruct\s+($struct_ops)\b/) {
30086903ffb2SAndy Whitcroft			WARN("struct $1 should normally be const\n" .
30096903ffb2SAndy Whitcroft				$herecurr);
30102b6db5cbSAndy Whitcroft		}
3011773647a0SAndy Whitcroft
3012773647a0SAndy Whitcroft# use of NR_CPUS is usually wrong
3013773647a0SAndy Whitcroft# ignore definitions of NR_CPUS and usage to define arrays as likely right
3014773647a0SAndy Whitcroft		if ($line =~ /\bNR_CPUS\b/ &&
3015c45dcabdSAndy Whitcroft		    $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
3016c45dcabdSAndy Whitcroft		    $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
3017171ae1a4SAndy Whitcroft		    $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
3018171ae1a4SAndy Whitcroft		    $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
3019171ae1a4SAndy Whitcroft		    $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
3020773647a0SAndy Whitcroft		{
3021773647a0SAndy Whitcroft			WARN("usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
3022773647a0SAndy Whitcroft		}
30239c9ba34eSAndy Whitcroft
30249c9ba34eSAndy Whitcroft# check for %L{u,d,i} in strings
30259c9ba34eSAndy Whitcroft		my $string;
30269c9ba34eSAndy Whitcroft		while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
30279c9ba34eSAndy Whitcroft			$string = substr($rawline, $-[1], $+[1] - $-[1]);
30282a1bc5d5SAndy Whitcroft			$string =~ s/%%/__/g;
30299c9ba34eSAndy Whitcroft			if ($string =~ /(?<!%)%L[udi]/) {
30309c9ba34eSAndy Whitcroft				WARN("\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
30319c9ba34eSAndy Whitcroft				last;
30329c9ba34eSAndy Whitcroft			}
30339c9ba34eSAndy Whitcroft		}
3034691d77b6SAndy Whitcroft
3035691d77b6SAndy Whitcroft# whine mightly about in_atomic
3036691d77b6SAndy Whitcroft		if ($line =~ /\bin_atomic\s*\(/) {
3037691d77b6SAndy Whitcroft			if ($realfile =~ m@^drivers/@) {
3038691d77b6SAndy Whitcroft				ERROR("do not use in_atomic in drivers\n" . $herecurr);
3039f4a87736SAndy Whitcroft			} elsif ($realfile !~ m@^kernel/@) {
3040691d77b6SAndy Whitcroft				WARN("use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
3041691d77b6SAndy Whitcroft			}
3042691d77b6SAndy Whitcroft		}
30431704f47bSPeter Zijlstra
30441704f47bSPeter Zijlstra# check for lockdep_set_novalidate_class
30451704f47bSPeter Zijlstra		if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
30461704f47bSPeter Zijlstra		    $line =~ /__lockdep_no_validate__\s*\)/ ) {
30471704f47bSPeter Zijlstra			if ($realfile !~ m@^kernel/lockdep@ &&
30481704f47bSPeter Zijlstra			    $realfile !~ m@^include/linux/lockdep@ &&
30491704f47bSPeter Zijlstra			    $realfile !~ m@^drivers/base/core@) {
30501704f47bSPeter Zijlstra				ERROR("lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
30511704f47bSPeter Zijlstra			}
30521704f47bSPeter Zijlstra		}
305388f8831cSDave Jones
305488f8831cSDave Jones		if ($line =~ /debugfs_create_file.*S_IWUGO/ ||
305588f8831cSDave Jones		    $line =~ /DEVICE_ATTR.*S_IWUGO/ ) {
305688f8831cSDave Jones			WARN("Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
305788f8831cSDave Jones		}
3058309c00c7SDave Jones
3059309c00c7SDave Jones		# Check for memset with swapped arguments
3060309c00c7SDave Jones		if ($line =~ /memset.*\,(\ |)(0x|)0(\ |0|)\);/) {
3061309c00c7SDave Jones			ERROR("memset size is 3rd argument, not the second.\n" . $herecurr);
3062309c00c7SDave Jones		}
306313214adfSAndy Whitcroft	}
306413214adfSAndy Whitcroft
306513214adfSAndy Whitcroft	# If we have no input at all, then there is nothing to report on
306613214adfSAndy Whitcroft	# so just keep quiet.
306713214adfSAndy Whitcroft	if ($#rawlines == -1) {
306813214adfSAndy Whitcroft		exit(0);
30690a920b5bSAndy Whitcroft	}
30700a920b5bSAndy Whitcroft
30718905a67cSAndy Whitcroft	# In mailback mode only produce a report in the negative, for
30728905a67cSAndy Whitcroft	# things that appear to be patches.
30738905a67cSAndy Whitcroft	if ($mailback && ($clean == 1 || !$is_patch)) {
30748905a67cSAndy Whitcroft		exit(0);
30758905a67cSAndy Whitcroft	}
30768905a67cSAndy Whitcroft
30778905a67cSAndy Whitcroft	# This is not a patch, and we are are in 'no-patch' mode so
30788905a67cSAndy Whitcroft	# just keep quiet.
30798905a67cSAndy Whitcroft	if (!$chk_patch && !$is_patch) {
30808905a67cSAndy Whitcroft		exit(0);
30818905a67cSAndy Whitcroft	}
30828905a67cSAndy Whitcroft
30838905a67cSAndy Whitcroft	if (!$is_patch) {
3084de7d4f0eSAndy Whitcroft		ERROR("Does not appear to be a unified-diff format patch\n");
30850a920b5bSAndy Whitcroft	}
30860a920b5bSAndy Whitcroft	if ($is_patch && $chk_signoff && $signoff == 0) {
3087de7d4f0eSAndy Whitcroft		ERROR("Missing Signed-off-by: line(s)\n");
30880a920b5bSAndy Whitcroft	}
30890a920b5bSAndy Whitcroft
3090f0a594c1SAndy Whitcroft	print report_dump();
309113214adfSAndy Whitcroft	if ($summary && !($clean == 1 && $quiet == 1)) {
309213214adfSAndy Whitcroft		print "$filename " if ($summary_file);
30936c72ffaaSAndy Whitcroft		print "total: $cnt_error errors, $cnt_warn warnings, " .
30946c72ffaaSAndy Whitcroft			(($check)? "$cnt_chk checks, " : "") .
30956c72ffaaSAndy Whitcroft			"$cnt_lines lines checked\n";
30968905a67cSAndy Whitcroft		print "\n" if ($quiet == 0);
30976c72ffaaSAndy Whitcroft	}
30988905a67cSAndy Whitcroft
3099d2c0a235SAndy Whitcroft	if ($quiet == 0) {
3100d2c0a235SAndy Whitcroft		# If there were whitespace errors which cleanpatch can fix
3101d2c0a235SAndy Whitcroft		# then suggest that.
3102d2c0a235SAndy Whitcroft		if ($rpt_cleaners) {
3103d2c0a235SAndy Whitcroft			print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n";
3104d2c0a235SAndy Whitcroft			print "      scripts/cleanfile\n\n";
3105b0781216SMike Frysinger			$rpt_cleaners = 0;
3106d2c0a235SAndy Whitcroft		}
3107d2c0a235SAndy Whitcroft	}
3108d2c0a235SAndy Whitcroft
31090a920b5bSAndy Whitcroft	if ($clean == 1 && $quiet == 0) {
3110c2fdda0dSAndy Whitcroft		print "$vname has no obvious style problems and is ready for submission.\n"
31110a920b5bSAndy Whitcroft	}
31120a920b5bSAndy Whitcroft	if ($clean == 0 && $quiet == 0) {
3113c2fdda0dSAndy Whitcroft		print "$vname has style problems, please review.  If any of these errors\n";
31140a920b5bSAndy Whitcroft		print "are false positives report them to the maintainer, see\n";
31150a920b5bSAndy Whitcroft		print "CHECKPATCH in MAINTAINERS.\n";
31160a920b5bSAndy Whitcroft	}
311713214adfSAndy Whitcroft
31180a920b5bSAndy Whitcroft	return $clean;
31190a920b5bSAndy Whitcroft}
3120