xref: /linux-6.15/scripts/checkpatch.pl (revision 3bf9a009)
10a920b5bSAndy Whitcroft#!/usr/bin/perl -w
2dbf004d7SDave Jones# (c) 2001, Dave Jones. (the file handling bit)
300df344fSAndy Whitcroft# (c) 2005, Joel Schopp <[email protected]> (the ugly bit)
42a5a2c25SAndy Whitcroft# (c) 2007,2008, Andy Whitcroft <[email protected]> (new conditions, test suite)
5131edb34SAndy Whitcroft# (c) 2008,2009, Andy Whitcroft <[email protected]>
60a920b5bSAndy Whitcroft# Licensed under the terms of the GNU GPL License version 2
70a920b5bSAndy Whitcroft
80a920b5bSAndy Whitcroftuse strict;
90a920b5bSAndy Whitcroft
100a920b5bSAndy Whitcroftmy $P = $0;
1100df344fSAndy Whitcroft$P =~ s@.*/@@g;
120a920b5bSAndy Whitcroft
135e8d8f6fSAndy Whitcroftmy $V = '0.30';
140a920b5bSAndy Whitcroft
150a920b5bSAndy Whitcroftuse Getopt::Long qw(:config no_auto_abbrev);
160a920b5bSAndy Whitcroft
170a920b5bSAndy Whitcroftmy $quiet = 0;
180a920b5bSAndy Whitcroftmy $tree = 1;
190a920b5bSAndy Whitcroftmy $chk_signoff = 1;
200a920b5bSAndy Whitcroftmy $chk_patch = 1;
21773647a0SAndy Whitcroftmy $tst_only;
226c72ffaaSAndy Whitcroftmy $emacs = 0;
238905a67cSAndy Whitcroftmy $terse = 0;
246c72ffaaSAndy Whitcroftmy $file = 0;
256c72ffaaSAndy Whitcroftmy $check = 0;
268905a67cSAndy Whitcroftmy $summary = 1;
278905a67cSAndy Whitcroftmy $mailback = 0;
2813214adfSAndy Whitcroftmy $summary_file = 0;
296c72ffaaSAndy Whitcroftmy $root;
30c2fdda0dSAndy Whitcroftmy %debug;
3177f5b10aSHannes Edermy $help = 0;
3277f5b10aSHannes Eder
3377f5b10aSHannes Edersub help {
3477f5b10aSHannes Eder	my ($exitcode) = @_;
3577f5b10aSHannes Eder
3677f5b10aSHannes Eder	print << "EOM";
3777f5b10aSHannes EderUsage: $P [OPTION]... [FILE]...
3877f5b10aSHannes EderVersion: $V
3977f5b10aSHannes Eder
4077f5b10aSHannes EderOptions:
4177f5b10aSHannes Eder  -q, --quiet                quiet
4277f5b10aSHannes Eder  --no-tree                  run without a kernel tree
4377f5b10aSHannes Eder  --no-signoff               do not check for 'Signed-off-by' line
4477f5b10aSHannes Eder  --patch                    treat FILE as patchfile (default)
4577f5b10aSHannes Eder  --emacs                    emacs compile window format
4677f5b10aSHannes Eder  --terse                    one line per report
4777f5b10aSHannes Eder  -f, --file                 treat FILE as regular source file
4877f5b10aSHannes Eder  --subjective, --strict     enable more subjective tests
4977f5b10aSHannes Eder  --root=PATH                PATH to the kernel tree root
5077f5b10aSHannes Eder  --no-summary               suppress the per-file summary
5177f5b10aSHannes Eder  --mailback                 only produce a report in case of warnings/errors
5277f5b10aSHannes Eder  --summary-file             include the filename in summary
5377f5b10aSHannes Eder  --debug KEY=[0|1]          turn on/off debugging of KEY, where KEY is one of
5477f5b10aSHannes Eder                             'values', 'possible', 'type', and 'attr' (default
5577f5b10aSHannes Eder                             is all off)
5677f5b10aSHannes Eder  --test-only=WORD           report only warnings/errors containing WORD
5777f5b10aSHannes Eder                             literally
5877f5b10aSHannes Eder  -h, --help, --version      display this help and exit
5977f5b10aSHannes Eder
6077f5b10aSHannes EderWhen FILE is - read standard input.
6177f5b10aSHannes EderEOM
6277f5b10aSHannes Eder
6377f5b10aSHannes Eder	exit($exitcode);
6477f5b10aSHannes Eder}
6577f5b10aSHannes Eder
660a920b5bSAndy WhitcroftGetOptions(
676c72ffaaSAndy Whitcroft	'q|quiet+'	=> \$quiet,
680a920b5bSAndy Whitcroft	'tree!'		=> \$tree,
690a920b5bSAndy Whitcroft	'signoff!'	=> \$chk_signoff,
700a920b5bSAndy Whitcroft	'patch!'	=> \$chk_patch,
716c72ffaaSAndy Whitcroft	'emacs!'	=> \$emacs,
728905a67cSAndy Whitcroft	'terse!'	=> \$terse,
7377f5b10aSHannes Eder	'f|file!'	=> \$file,
746c72ffaaSAndy Whitcroft	'subjective!'	=> \$check,
756c72ffaaSAndy Whitcroft	'strict!'	=> \$check,
766c72ffaaSAndy Whitcroft	'root=s'	=> \$root,
778905a67cSAndy Whitcroft	'summary!'	=> \$summary,
788905a67cSAndy Whitcroft	'mailback!'	=> \$mailback,
7913214adfSAndy Whitcroft	'summary-file!'	=> \$summary_file,
8013214adfSAndy Whitcroft
81c2fdda0dSAndy Whitcroft	'debug=s'	=> \%debug,
82773647a0SAndy Whitcroft	'test-only=s'	=> \$tst_only,
8377f5b10aSHannes Eder	'h|help'	=> \$help,
8477f5b10aSHannes Eder	'version'	=> \$help
8577f5b10aSHannes Eder) or help(1);
8677f5b10aSHannes Eder
8777f5b10aSHannes Ederhelp(0) if ($help);
880a920b5bSAndy Whitcroft
890a920b5bSAndy Whitcroftmy $exit = 0;
900a920b5bSAndy Whitcroft
910a920b5bSAndy Whitcroftif ($#ARGV < 0) {
9277f5b10aSHannes Eder	print "$P: no input files\n";
930a920b5bSAndy Whitcroft	exit(1);
940a920b5bSAndy Whitcroft}
950a920b5bSAndy Whitcroft
96c2fdda0dSAndy Whitcroftmy $dbg_values = 0;
97c2fdda0dSAndy Whitcroftmy $dbg_possible = 0;
987429c690SAndy Whitcroftmy $dbg_type = 0;
99a1ef277eSAndy Whitcroftmy $dbg_attr = 0;
100c2fdda0dSAndy Whitcroftfor my $key (keys %debug) {
10121caa13cSAndy Whitcroft	## no critic
10221caa13cSAndy Whitcroft	eval "\${dbg_$key} = '$debug{$key}';";
10321caa13cSAndy Whitcroft	die "$@" if ($@);
104c2fdda0dSAndy Whitcroft}
105c2fdda0dSAndy Whitcroft
106d2c0a235SAndy Whitcroftmy $rpt_cleaners = 0;
107d2c0a235SAndy Whitcroft
1088905a67cSAndy Whitcroftif ($terse) {
1098905a67cSAndy Whitcroft	$emacs = 1;
1108905a67cSAndy Whitcroft	$quiet++;
1118905a67cSAndy Whitcroft}
1128905a67cSAndy Whitcroft
1136c72ffaaSAndy Whitcroftif ($tree) {
1146c72ffaaSAndy Whitcroft	if (defined $root) {
1156c72ffaaSAndy Whitcroft		if (!top_of_kernel_tree($root)) {
1166c72ffaaSAndy Whitcroft			die "$P: $root: --root does not point at a valid tree\n";
1176c72ffaaSAndy Whitcroft		}
1186c72ffaaSAndy Whitcroft	} else {
1196c72ffaaSAndy Whitcroft		if (top_of_kernel_tree('.')) {
1206c72ffaaSAndy Whitcroft			$root = '.';
1216c72ffaaSAndy Whitcroft		} elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
1226c72ffaaSAndy Whitcroft						top_of_kernel_tree($1)) {
1236c72ffaaSAndy Whitcroft			$root = $1;
1246c72ffaaSAndy Whitcroft		}
1256c72ffaaSAndy Whitcroft	}
1266c72ffaaSAndy Whitcroft
1276c72ffaaSAndy Whitcroft	if (!defined $root) {
1280a920b5bSAndy Whitcroft		print "Must be run from the top-level dir. of a kernel tree\n";
1290a920b5bSAndy Whitcroft		exit(2);
1300a920b5bSAndy Whitcroft	}
1316c72ffaaSAndy Whitcroft}
1326c72ffaaSAndy Whitcroft
1336c72ffaaSAndy Whitcroftmy $emitted_corrupt = 0;
1346c72ffaaSAndy Whitcroft
1352ceb532bSAndy Whitcroftour $Ident	= qr{
1362ceb532bSAndy Whitcroft			[A-Za-z_][A-Za-z\d_]*
1372ceb532bSAndy Whitcroft			(?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
1382ceb532bSAndy Whitcroft		}x;
1396c72ffaaSAndy Whitcroftour $Storage	= qr{extern|static|asmlinkage};
1406c72ffaaSAndy Whitcroftour $Sparse	= qr{
1416c72ffaaSAndy Whitcroft			__user|
1426c72ffaaSAndy Whitcroft			__kernel|
1436c72ffaaSAndy Whitcroft			__force|
1446c72ffaaSAndy Whitcroft			__iomem|
1456c72ffaaSAndy Whitcroft			__must_check|
1466c72ffaaSAndy Whitcroft			__init_refok|
147417495edSAndy Whitcroft			__kprobes|
148417495edSAndy Whitcroft			__ref
1496c72ffaaSAndy Whitcroft		}x;
15052131292SWolfram Sang
15152131292SWolfram Sang# Notes to $Attribute:
15252131292SWolfram Sang# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
1536c72ffaaSAndy Whitcroftour $Attribute	= qr{
1546c72ffaaSAndy Whitcroft			const|
1556c72ffaaSAndy Whitcroft			__read_mostly|
1566c72ffaaSAndy Whitcroft			__kprobes|
15752131292SWolfram Sang			__(?:mem|cpu|dev|)(?:initdata|initconst|init\b)|
15824e1d81aSAndy Whitcroft			____cacheline_aligned|
15924e1d81aSAndy Whitcroft			____cacheline_aligned_in_smp|
1605fe3af11SAndy Whitcroft			____cacheline_internodealigned_in_smp|
1615fe3af11SAndy Whitcroft			__weak
1626c72ffaaSAndy Whitcroft		  }x;
163c45dcabdSAndy Whitcroftour $Modifier;
1646c72ffaaSAndy Whitcroftour $Inline	= qr{inline|__always_inline|noinline};
1656c72ffaaSAndy Whitcroftour $Member	= qr{->$Ident|\.$Ident|\[[^]]*\]};
1666c72ffaaSAndy Whitcroftour $Lval	= qr{$Ident(?:$Member)*};
1676c72ffaaSAndy Whitcroft
1686c72ffaaSAndy Whitcroftour $Constant	= qr{(?:[0-9]+|0x[0-9a-fA-F]+)[UL]*};
1696c72ffaaSAndy Whitcroftour $Assignment	= qr{(?:\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=)};
17086f9d059SAndy Whitcroftour $Compare    = qr{<=|>=|==|!=|<|>};
1716c72ffaaSAndy Whitcroftour $Operators	= qr{
1726c72ffaaSAndy Whitcroft			<=|>=|==|!=|
1736c72ffaaSAndy Whitcroft			=>|->|<<|>>|<|>|!|~|
174c2fdda0dSAndy Whitcroft			&&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%
1756c72ffaaSAndy Whitcroft		  }x;
1766c72ffaaSAndy Whitcroft
1778905a67cSAndy Whitcroftour $NonptrType;
1788905a67cSAndy Whitcroftour $Type;
1798905a67cSAndy Whitcroftour $Declare;
1808905a67cSAndy Whitcroft
181171ae1a4SAndy Whitcroftour $UTF8	= qr {
182171ae1a4SAndy Whitcroft	[\x09\x0A\x0D\x20-\x7E]              # ASCII
183171ae1a4SAndy Whitcroft	| [\xC2-\xDF][\x80-\xBF]             # non-overlong 2-byte
184171ae1a4SAndy Whitcroft	|  \xE0[\xA0-\xBF][\x80-\xBF]        # excluding overlongs
185171ae1a4SAndy Whitcroft	| [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}  # straight 3-byte
186171ae1a4SAndy Whitcroft	|  \xED[\x80-\x9F][\x80-\xBF]        # excluding surrogates
187171ae1a4SAndy Whitcroft	|  \xF0[\x90-\xBF][\x80-\xBF]{2}     # planes 1-3
188171ae1a4SAndy Whitcroft	| [\xF1-\xF3][\x80-\xBF]{3}          # planes 4-15
189171ae1a4SAndy Whitcroft	|  \xF4[\x80-\x8F][\x80-\xBF]{2}     # plane 16
190171ae1a4SAndy Whitcroft}x;
191171ae1a4SAndy Whitcroft
1928ed22cadSAndy Whitcroftour $typeTypedefs = qr{(?x:
193fb9e9096SAndy Whitcroft	(?:__)?(?:u|s|be|le)(?:8|16|32|64)|
1948ed22cadSAndy Whitcroft	atomic_t
1958ed22cadSAndy Whitcroft)};
1968ed22cadSAndy Whitcroft
197691e669bSJoe Perchesour $logFunctions = qr{(?x:
198691e669bSJoe Perches	printk|
199691e669bSJoe Perches	pr_(debug|dbg|vdbg|devel|info|warning|err|notice|alert|crit|emerg|cont)|
2008bbea968SJoe Perches	(dev|netdev|netif)_(printk|dbg|vdbg|info|warn|err|notice|alert|crit|emerg|WARN)|
201691e669bSJoe Perches	WARN|
202691e669bSJoe Perches	panic
203691e669bSJoe Perches)};
204691e669bSJoe Perches
2058905a67cSAndy Whitcroftour @typeList = (
2068905a67cSAndy Whitcroft	qr{void},
207c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?char},
208c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?short},
209c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?int},
210c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?long},
211c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?long\s+int},
212c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?long\s+long},
213c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?long\s+long\s+int},
2148905a67cSAndy Whitcroft	qr{unsigned},
2158905a67cSAndy Whitcroft	qr{float},
2168905a67cSAndy Whitcroft	qr{double},
2178905a67cSAndy Whitcroft	qr{bool},
2188905a67cSAndy Whitcroft	qr{struct\s+$Ident},
2198905a67cSAndy Whitcroft	qr{union\s+$Ident},
2208905a67cSAndy Whitcroft	qr{enum\s+$Ident},
2218905a67cSAndy Whitcroft	qr{${Ident}_t},
2228905a67cSAndy Whitcroft	qr{${Ident}_handler},
2238905a67cSAndy Whitcroft	qr{${Ident}_handler_fn},
2248905a67cSAndy Whitcroft);
225c45dcabdSAndy Whitcroftour @modifierList = (
226c45dcabdSAndy Whitcroft	qr{fastcall},
227c45dcabdSAndy Whitcroft);
2288905a67cSAndy Whitcroft
2297840a94cSWolfram Sangour $allowed_asm_includes = qr{(?x:
2307840a94cSWolfram Sang	irq|
2317840a94cSWolfram Sang	memory
2327840a94cSWolfram Sang)};
2337840a94cSWolfram Sang# memory.h: ARM has a custom one
2347840a94cSWolfram Sang
2358905a67cSAndy Whitcroftsub build_types {
236d2172eb5SAndy Whitcroft	my $mods = "(?x:  \n" . join("|\n  ", @modifierList) . "\n)";
237d2172eb5SAndy Whitcroft	my $all = "(?x:  \n" . join("|\n  ", @typeList) . "\n)";
238c8cb2ca3SAndy Whitcroft	$Modifier	= qr{(?:$Attribute|$Sparse|$mods)};
2398905a67cSAndy Whitcroft	$NonptrType	= qr{
240d2172eb5SAndy Whitcroft			(?:$Modifier\s+|const\s+)*
241cf655043SAndy Whitcroft			(?:
242c45dcabdSAndy Whitcroft				(?:typeof|__typeof__)\s*\(\s*\**\s*$Ident\s*\)|
2438ed22cadSAndy Whitcroft				(?:$typeTypedefs\b)|
244c45dcabdSAndy Whitcroft				(?:${all}\b)
245cf655043SAndy Whitcroft			)
246c8cb2ca3SAndy Whitcroft			(?:\s+$Modifier|\s+const)*
2478905a67cSAndy Whitcroft		  }x;
2488905a67cSAndy Whitcroft	$Type	= qr{
249c45dcabdSAndy Whitcroft			$NonptrType
25065863862SAndy Whitcroft			(?:[\s\*]+\s*const|[\s\*]+|(?:\s*\[\s*\])+)?
251c8cb2ca3SAndy Whitcroft			(?:\s+$Inline|\s+$Modifier)*
2528905a67cSAndy Whitcroft		  }x;
2538905a67cSAndy Whitcroft	$Declare	= qr{(?:$Storage\s+)?$Type};
2548905a67cSAndy Whitcroft}
2558905a67cSAndy Whitcroftbuild_types();
2566c72ffaaSAndy Whitcroft
2576c72ffaaSAndy Whitcroft$chk_signoff = 0 if ($file);
2580a920b5bSAndy Whitcroft
2594a0df2efSAndy Whitcroftmy @dep_includes = ();
2604a0df2efSAndy Whitcroftmy @dep_functions = ();
2616c72ffaaSAndy Whitcroftmy $removal = "Documentation/feature-removal-schedule.txt";
2626c72ffaaSAndy Whitcroftif ($tree && -f "$root/$removal") {
26321caa13cSAndy Whitcroft	open(my $REMOVE, '<', "$root/$removal") ||
2646c72ffaaSAndy Whitcroft				die "$P: $removal: open failed - $!\n";
26521caa13cSAndy Whitcroft	while (<$REMOVE>) {
266f0a594c1SAndy Whitcroft		if (/^Check:\s+(.*\S)/) {
267f0a594c1SAndy Whitcroft			for my $entry (split(/[, ]+/, $1)) {
268f0a594c1SAndy Whitcroft				if ($entry =~ m@include/(.*)@) {
2694a0df2efSAndy Whitcroft					push(@dep_includes, $1);
2704a0df2efSAndy Whitcroft
271f0a594c1SAndy Whitcroft				} elsif ($entry !~ m@/@) {
272f0a594c1SAndy Whitcroft					push(@dep_functions, $entry);
273f0a594c1SAndy Whitcroft				}
2744a0df2efSAndy Whitcroft			}
2750a920b5bSAndy Whitcroft		}
2760a920b5bSAndy Whitcroft	}
27721caa13cSAndy Whitcroft	close($REMOVE);
2780a920b5bSAndy Whitcroft}
2790a920b5bSAndy Whitcroft
28000df344fSAndy Whitcroftmy @rawlines = ();
281c2fdda0dSAndy Whitcroftmy @lines = ();
282c2fdda0dSAndy Whitcroftmy $vname;
2836c72ffaaSAndy Whitcroftfor my $filename (@ARGV) {
28421caa13cSAndy Whitcroft	my $FILE;
2856c72ffaaSAndy Whitcroft	if ($file) {
28621caa13cSAndy Whitcroft		open($FILE, '-|', "diff -u /dev/null $filename") ||
2876c72ffaaSAndy Whitcroft			die "$P: $filename: diff failed - $!\n";
28821caa13cSAndy Whitcroft	} elsif ($filename eq '-') {
28921caa13cSAndy Whitcroft		open($FILE, '<&STDIN');
2906c72ffaaSAndy Whitcroft	} else {
29121caa13cSAndy Whitcroft		open($FILE, '<', "$filename") ||
2926c72ffaaSAndy Whitcroft			die "$P: $filename: open failed - $!\n";
2936c72ffaaSAndy Whitcroft	}
294c2fdda0dSAndy Whitcroft	if ($filename eq '-') {
295c2fdda0dSAndy Whitcroft		$vname = 'Your patch';
296c2fdda0dSAndy Whitcroft	} else {
297c2fdda0dSAndy Whitcroft		$vname = $filename;
298c2fdda0dSAndy Whitcroft	}
29921caa13cSAndy Whitcroft	while (<$FILE>) {
3000a920b5bSAndy Whitcroft		chomp;
30100df344fSAndy Whitcroft		push(@rawlines, $_);
3026c72ffaaSAndy Whitcroft	}
30321caa13cSAndy Whitcroft	close($FILE);
304c2fdda0dSAndy Whitcroft	if (!process($filename)) {
3050a920b5bSAndy Whitcroft		$exit = 1;
3060a920b5bSAndy Whitcroft	}
30700df344fSAndy Whitcroft	@rawlines = ();
30813214adfSAndy Whitcroft	@lines = ();
3090a920b5bSAndy Whitcroft}
3100a920b5bSAndy Whitcroft
3110a920b5bSAndy Whitcroftexit($exit);
3120a920b5bSAndy Whitcroft
3130a920b5bSAndy Whitcroftsub top_of_kernel_tree {
3146c72ffaaSAndy Whitcroft	my ($root) = @_;
3156c72ffaaSAndy Whitcroft
3166c72ffaaSAndy Whitcroft	my @tree_check = (
3176c72ffaaSAndy Whitcroft		"COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
3186c72ffaaSAndy Whitcroft		"README", "Documentation", "arch", "include", "drivers",
3196c72ffaaSAndy Whitcroft		"fs", "init", "ipc", "kernel", "lib", "scripts",
3206c72ffaaSAndy Whitcroft	);
3216c72ffaaSAndy Whitcroft
3226c72ffaaSAndy Whitcroft	foreach my $check (@tree_check) {
3236c72ffaaSAndy Whitcroft		if (! -e $root . '/' . $check) {
3240a920b5bSAndy Whitcroft			return 0;
3250a920b5bSAndy Whitcroft		}
3266c72ffaaSAndy Whitcroft	}
3276c72ffaaSAndy Whitcroft	return 1;
3286c72ffaaSAndy Whitcroft}
3290a920b5bSAndy Whitcroft
3300a920b5bSAndy Whitcroftsub expand_tabs {
3310a920b5bSAndy Whitcroft	my ($str) = @_;
3320a920b5bSAndy Whitcroft
3330a920b5bSAndy Whitcroft	my $res = '';
3340a920b5bSAndy Whitcroft	my $n = 0;
3350a920b5bSAndy Whitcroft	for my $c (split(//, $str)) {
3360a920b5bSAndy Whitcroft		if ($c eq "\t") {
3370a920b5bSAndy Whitcroft			$res .= ' ';
3380a920b5bSAndy Whitcroft			$n++;
3390a920b5bSAndy Whitcroft			for (; ($n % 8) != 0; $n++) {
3400a920b5bSAndy Whitcroft				$res .= ' ';
3410a920b5bSAndy Whitcroft			}
3420a920b5bSAndy Whitcroft			next;
3430a920b5bSAndy Whitcroft		}
3440a920b5bSAndy Whitcroft		$res .= $c;
3450a920b5bSAndy Whitcroft		$n++;
3460a920b5bSAndy Whitcroft	}
3470a920b5bSAndy Whitcroft
3480a920b5bSAndy Whitcroft	return $res;
3490a920b5bSAndy Whitcroft}
3506c72ffaaSAndy Whitcroftsub copy_spacing {
351773647a0SAndy Whitcroft	(my $res = shift) =~ tr/\t/ /c;
3526c72ffaaSAndy Whitcroft	return $res;
3536c72ffaaSAndy Whitcroft}
3540a920b5bSAndy Whitcroft
3554a0df2efSAndy Whitcroftsub line_stats {
3564a0df2efSAndy Whitcroft	my ($line) = @_;
3574a0df2efSAndy Whitcroft
3584a0df2efSAndy Whitcroft	# Drop the diff line leader and expand tabs
3594a0df2efSAndy Whitcroft	$line =~ s/^.//;
3604a0df2efSAndy Whitcroft	$line = expand_tabs($line);
3614a0df2efSAndy Whitcroft
3624a0df2efSAndy Whitcroft	# Pick the indent from the front of the line.
3634a0df2efSAndy Whitcroft	my ($white) = ($line =~ /^(\s*)/);
3644a0df2efSAndy Whitcroft
3654a0df2efSAndy Whitcroft	return (length($line), length($white));
3664a0df2efSAndy Whitcroft}
3674a0df2efSAndy Whitcroft
368773647a0SAndy Whitcroftmy $sanitise_quote = '';
369773647a0SAndy Whitcroft
370773647a0SAndy Whitcroftsub sanitise_line_reset {
371773647a0SAndy Whitcroft	my ($in_comment) = @_;
372773647a0SAndy Whitcroft
373773647a0SAndy Whitcroft	if ($in_comment) {
374773647a0SAndy Whitcroft		$sanitise_quote = '*/';
375773647a0SAndy Whitcroft	} else {
376773647a0SAndy Whitcroft		$sanitise_quote = '';
377773647a0SAndy Whitcroft	}
378773647a0SAndy Whitcroft}
37900df344fSAndy Whitcroftsub sanitise_line {
38000df344fSAndy Whitcroft	my ($line) = @_;
38100df344fSAndy Whitcroft
38200df344fSAndy Whitcroft	my $res = '';
38300df344fSAndy Whitcroft	my $l = '';
38400df344fSAndy Whitcroft
385c2fdda0dSAndy Whitcroft	my $qlen = 0;
386773647a0SAndy Whitcroft	my $off = 0;
387773647a0SAndy Whitcroft	my $c;
38800df344fSAndy Whitcroft
389773647a0SAndy Whitcroft	# Always copy over the diff marker.
390773647a0SAndy Whitcroft	$res = substr($line, 0, 1);
391773647a0SAndy Whitcroft
392773647a0SAndy Whitcroft	for ($off = 1; $off < length($line); $off++) {
393773647a0SAndy Whitcroft		$c = substr($line, $off, 1);
394773647a0SAndy Whitcroft
395773647a0SAndy Whitcroft		# Comments we are wacking completly including the begin
396773647a0SAndy Whitcroft		# and end, all to $;.
397773647a0SAndy Whitcroft		if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
398773647a0SAndy Whitcroft			$sanitise_quote = '*/';
399773647a0SAndy Whitcroft
400773647a0SAndy Whitcroft			substr($res, $off, 2, "$;$;");
401773647a0SAndy Whitcroft			$off++;
40200df344fSAndy Whitcroft			next;
403773647a0SAndy Whitcroft		}
40481bc0e02SAndy Whitcroft		if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
405773647a0SAndy Whitcroft			$sanitise_quote = '';
406773647a0SAndy Whitcroft			substr($res, $off, 2, "$;$;");
407773647a0SAndy Whitcroft			$off++;
408773647a0SAndy Whitcroft			next;
409773647a0SAndy Whitcroft		}
410113f04a8SDaniel Walker		if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
411113f04a8SDaniel Walker			$sanitise_quote = '//';
412113f04a8SDaniel Walker
413113f04a8SDaniel Walker			substr($res, $off, 2, $sanitise_quote);
414113f04a8SDaniel Walker			$off++;
415113f04a8SDaniel Walker			next;
416113f04a8SDaniel Walker		}
417773647a0SAndy Whitcroft
418773647a0SAndy Whitcroft		# A \ in a string means ignore the next character.
419773647a0SAndy Whitcroft		if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
420773647a0SAndy Whitcroft		    $c eq "\\") {
421773647a0SAndy Whitcroft			substr($res, $off, 2, 'XX');
422773647a0SAndy Whitcroft			$off++;
423773647a0SAndy Whitcroft			next;
424773647a0SAndy Whitcroft		}
425773647a0SAndy Whitcroft		# Regular quotes.
426773647a0SAndy Whitcroft		if ($c eq "'" || $c eq '"') {
427773647a0SAndy Whitcroft			if ($sanitise_quote eq '') {
428773647a0SAndy Whitcroft				$sanitise_quote = $c;
429773647a0SAndy Whitcroft
430773647a0SAndy Whitcroft				substr($res, $off, 1, $c);
431773647a0SAndy Whitcroft				next;
432773647a0SAndy Whitcroft			} elsif ($sanitise_quote eq $c) {
433773647a0SAndy Whitcroft				$sanitise_quote = '';
43400df344fSAndy Whitcroft			}
43500df344fSAndy Whitcroft		}
436773647a0SAndy Whitcroft
437fae17daeSAndy Whitcroft		#print "c<$c> SQ<$sanitise_quote>\n";
438773647a0SAndy Whitcroft		if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
439773647a0SAndy Whitcroft			substr($res, $off, 1, $;);
440113f04a8SDaniel Walker		} elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
441113f04a8SDaniel Walker			substr($res, $off, 1, $;);
442773647a0SAndy Whitcroft		} elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
443773647a0SAndy Whitcroft			substr($res, $off, 1, 'X');
44400df344fSAndy Whitcroft		} else {
445773647a0SAndy Whitcroft			substr($res, $off, 1, $c);
44600df344fSAndy Whitcroft		}
447c2fdda0dSAndy Whitcroft	}
448c2fdda0dSAndy Whitcroft
449113f04a8SDaniel Walker	if ($sanitise_quote eq '//') {
450113f04a8SDaniel Walker		$sanitise_quote = '';
451113f04a8SDaniel Walker	}
452113f04a8SDaniel Walker
453c2fdda0dSAndy Whitcroft	# The pathname on a #include may be surrounded by '<' and '>'.
454c45dcabdSAndy Whitcroft	if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
455c2fdda0dSAndy Whitcroft		my $clean = 'X' x length($1);
456c2fdda0dSAndy Whitcroft		$res =~ s@\<.*\>@<$clean>@;
457c2fdda0dSAndy Whitcroft
458c2fdda0dSAndy Whitcroft	# The whole of a #error is a string.
459c45dcabdSAndy Whitcroft	} elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
460c2fdda0dSAndy Whitcroft		my $clean = 'X' x length($1);
461c45dcabdSAndy Whitcroft		$res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
462c2fdda0dSAndy Whitcroft	}
463c2fdda0dSAndy Whitcroft
46400df344fSAndy Whitcroft	return $res;
46500df344fSAndy Whitcroft}
46600df344fSAndy Whitcroft
4678905a67cSAndy Whitcroftsub ctx_statement_block {
4688905a67cSAndy Whitcroft	my ($linenr, $remain, $off) = @_;
4698905a67cSAndy Whitcroft	my $line = $linenr - 1;
4708905a67cSAndy Whitcroft	my $blk = '';
4718905a67cSAndy Whitcroft	my $soff = $off;
4728905a67cSAndy Whitcroft	my $coff = $off - 1;
473773647a0SAndy Whitcroft	my $coff_set = 0;
4748905a67cSAndy Whitcroft
47513214adfSAndy Whitcroft	my $loff = 0;
47613214adfSAndy Whitcroft
4778905a67cSAndy Whitcroft	my $type = '';
4788905a67cSAndy Whitcroft	my $level = 0;
479a2750645SAndy Whitcroft	my @stack = ();
480cf655043SAndy Whitcroft	my $p;
4818905a67cSAndy Whitcroft	my $c;
4828905a67cSAndy Whitcroft	my $len = 0;
48313214adfSAndy Whitcroft
48413214adfSAndy Whitcroft	my $remainder;
4858905a67cSAndy Whitcroft	while (1) {
486a2750645SAndy Whitcroft		@stack = (['', 0]) if ($#stack == -1);
487a2750645SAndy Whitcroft
488773647a0SAndy Whitcroft		#warn "CSB: blk<$blk> remain<$remain>\n";
4898905a67cSAndy Whitcroft		# If we are about to drop off the end, pull in more
4908905a67cSAndy Whitcroft		# context.
4918905a67cSAndy Whitcroft		if ($off >= $len) {
4928905a67cSAndy Whitcroft			for (; $remain > 0; $line++) {
493dea33496SAndy Whitcroft				last if (!defined $lines[$line]);
494c2fdda0dSAndy Whitcroft				next if ($lines[$line] =~ /^-/);
4958905a67cSAndy Whitcroft				$remain--;
49613214adfSAndy Whitcroft				$loff = $len;
497c2fdda0dSAndy Whitcroft				$blk .= $lines[$line] . "\n";
4988905a67cSAndy Whitcroft				$len = length($blk);
4998905a67cSAndy Whitcroft				$line++;
5008905a67cSAndy Whitcroft				last;
5018905a67cSAndy Whitcroft			}
5028905a67cSAndy Whitcroft			# Bail if there is no further context.
5038905a67cSAndy Whitcroft			#warn "CSB: blk<$blk> off<$off> len<$len>\n";
50413214adfSAndy Whitcroft			if ($off >= $len) {
5058905a67cSAndy Whitcroft				last;
5068905a67cSAndy Whitcroft			}
5078905a67cSAndy Whitcroft		}
508cf655043SAndy Whitcroft		$p = $c;
5098905a67cSAndy Whitcroft		$c = substr($blk, $off, 1);
51013214adfSAndy Whitcroft		$remainder = substr($blk, $off);
5118905a67cSAndy Whitcroft
512773647a0SAndy Whitcroft		#warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
5134635f4fbSAndy Whitcroft
5144635f4fbSAndy Whitcroft		# Handle nested #if/#else.
5154635f4fbSAndy Whitcroft		if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
5164635f4fbSAndy Whitcroft			push(@stack, [ $type, $level ]);
5174635f4fbSAndy Whitcroft		} elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
5184635f4fbSAndy Whitcroft			($type, $level) = @{$stack[$#stack - 1]};
5194635f4fbSAndy Whitcroft		} elsif ($remainder =~ /^#\s*endif\b/) {
5204635f4fbSAndy Whitcroft			($type, $level) = @{pop(@stack)};
5214635f4fbSAndy Whitcroft		}
5224635f4fbSAndy Whitcroft
5238905a67cSAndy Whitcroft		# Statement ends at the ';' or a close '}' at the
5248905a67cSAndy Whitcroft		# outermost level.
5258905a67cSAndy Whitcroft		if ($level == 0 && $c eq ';') {
5268905a67cSAndy Whitcroft			last;
5278905a67cSAndy Whitcroft		}
5288905a67cSAndy Whitcroft
52913214adfSAndy Whitcroft		# An else is really a conditional as long as its not else if
530773647a0SAndy Whitcroft		if ($level == 0 && $coff_set == 0 &&
531773647a0SAndy Whitcroft				(!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
532773647a0SAndy Whitcroft				$remainder =~ /^(else)(?:\s|{)/ &&
533773647a0SAndy Whitcroft				$remainder !~ /^else\s+if\b/) {
534773647a0SAndy Whitcroft			$coff = $off + length($1) - 1;
535773647a0SAndy Whitcroft			$coff_set = 1;
536773647a0SAndy Whitcroft			#warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
537773647a0SAndy Whitcroft			#warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
53813214adfSAndy Whitcroft		}
53913214adfSAndy Whitcroft
5408905a67cSAndy Whitcroft		if (($type eq '' || $type eq '(') && $c eq '(') {
5418905a67cSAndy Whitcroft			$level++;
5428905a67cSAndy Whitcroft			$type = '(';
5438905a67cSAndy Whitcroft		}
5448905a67cSAndy Whitcroft		if ($type eq '(' && $c eq ')') {
5458905a67cSAndy Whitcroft			$level--;
5468905a67cSAndy Whitcroft			$type = ($level != 0)? '(' : '';
5478905a67cSAndy Whitcroft
5488905a67cSAndy Whitcroft			if ($level == 0 && $coff < $soff) {
5498905a67cSAndy Whitcroft				$coff = $off;
550773647a0SAndy Whitcroft				$coff_set = 1;
551773647a0SAndy Whitcroft				#warn "CSB: mark coff<$coff>\n";
5528905a67cSAndy Whitcroft			}
5538905a67cSAndy Whitcroft		}
5548905a67cSAndy Whitcroft		if (($type eq '' || $type eq '{') && $c eq '{') {
5558905a67cSAndy Whitcroft			$level++;
5568905a67cSAndy Whitcroft			$type = '{';
5578905a67cSAndy Whitcroft		}
5588905a67cSAndy Whitcroft		if ($type eq '{' && $c eq '}') {
5598905a67cSAndy Whitcroft			$level--;
5608905a67cSAndy Whitcroft			$type = ($level != 0)? '{' : '';
5618905a67cSAndy Whitcroft
5628905a67cSAndy Whitcroft			if ($level == 0) {
563b998e001SPatrick Pannuto				if (substr($blk, $off + 1, 1) eq ';') {
564b998e001SPatrick Pannuto					$off++;
565b998e001SPatrick Pannuto				}
5668905a67cSAndy Whitcroft				last;
5678905a67cSAndy Whitcroft			}
5688905a67cSAndy Whitcroft		}
5698905a67cSAndy Whitcroft		$off++;
5708905a67cSAndy Whitcroft	}
571a3bb97a7SAndy Whitcroft	# We are truly at the end, so shuffle to the next line.
57213214adfSAndy Whitcroft	if ($off == $len) {
573a3bb97a7SAndy Whitcroft		$loff = $len + 1;
57413214adfSAndy Whitcroft		$line++;
57513214adfSAndy Whitcroft		$remain--;
57613214adfSAndy Whitcroft	}
5778905a67cSAndy Whitcroft
5788905a67cSAndy Whitcroft	my $statement = substr($blk, $soff, $off - $soff + 1);
5798905a67cSAndy Whitcroft	my $condition = substr($blk, $soff, $coff - $soff + 1);
5808905a67cSAndy Whitcroft
5818905a67cSAndy Whitcroft	#warn "STATEMENT<$statement>\n";
5828905a67cSAndy Whitcroft	#warn "CONDITION<$condition>\n";
5838905a67cSAndy Whitcroft
584773647a0SAndy Whitcroft	#print "coff<$coff> soff<$off> loff<$loff>\n";
58513214adfSAndy Whitcroft
58613214adfSAndy Whitcroft	return ($statement, $condition,
58713214adfSAndy Whitcroft			$line, $remain + 1, $off - $loff + 1, $level);
58813214adfSAndy Whitcroft}
58913214adfSAndy Whitcroft
590cf655043SAndy Whitcroftsub statement_lines {
591cf655043SAndy Whitcroft	my ($stmt) = @_;
592cf655043SAndy Whitcroft
593cf655043SAndy Whitcroft	# Strip the diff line prefixes and rip blank lines at start and end.
594cf655043SAndy Whitcroft	$stmt =~ s/(^|\n)./$1/g;
595cf655043SAndy Whitcroft	$stmt =~ s/^\s*//;
596cf655043SAndy Whitcroft	$stmt =~ s/\s*$//;
597cf655043SAndy Whitcroft
598cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
599cf655043SAndy Whitcroft
600cf655043SAndy Whitcroft	return $#stmt_lines + 2;
601cf655043SAndy Whitcroft}
602cf655043SAndy Whitcroft
603cf655043SAndy Whitcroftsub statement_rawlines {
604cf655043SAndy Whitcroft	my ($stmt) = @_;
605cf655043SAndy Whitcroft
606cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
607cf655043SAndy Whitcroft
608cf655043SAndy Whitcroft	return $#stmt_lines + 2;
609cf655043SAndy Whitcroft}
610cf655043SAndy Whitcroft
611cf655043SAndy Whitcroftsub statement_block_size {
612cf655043SAndy Whitcroft	my ($stmt) = @_;
613cf655043SAndy Whitcroft
614cf655043SAndy Whitcroft	$stmt =~ s/(^|\n)./$1/g;
615cf655043SAndy Whitcroft	$stmt =~ s/^\s*{//;
616cf655043SAndy Whitcroft	$stmt =~ s/}\s*$//;
617cf655043SAndy Whitcroft	$stmt =~ s/^\s*//;
618cf655043SAndy Whitcroft	$stmt =~ s/\s*$//;
619cf655043SAndy Whitcroft
620cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
621cf655043SAndy Whitcroft	my @stmt_statements = ($stmt =~ /;/g);
622cf655043SAndy Whitcroft
623cf655043SAndy Whitcroft	my $stmt_lines = $#stmt_lines + 2;
624cf655043SAndy Whitcroft	my $stmt_statements = $#stmt_statements + 1;
625cf655043SAndy Whitcroft
626cf655043SAndy Whitcroft	if ($stmt_lines > $stmt_statements) {
627cf655043SAndy Whitcroft		return $stmt_lines;
628cf655043SAndy Whitcroft	} else {
629cf655043SAndy Whitcroft		return $stmt_statements;
630cf655043SAndy Whitcroft	}
631cf655043SAndy Whitcroft}
632cf655043SAndy Whitcroft
63313214adfSAndy Whitcroftsub ctx_statement_full {
63413214adfSAndy Whitcroft	my ($linenr, $remain, $off) = @_;
63513214adfSAndy Whitcroft	my ($statement, $condition, $level);
63613214adfSAndy Whitcroft
63713214adfSAndy Whitcroft	my (@chunks);
63813214adfSAndy Whitcroft
639cf655043SAndy Whitcroft	# Grab the first conditional/block pair.
64013214adfSAndy Whitcroft	($statement, $condition, $linenr, $remain, $off, $level) =
64113214adfSAndy Whitcroft				ctx_statement_block($linenr, $remain, $off);
642773647a0SAndy Whitcroft	#print "F: c<$condition> s<$statement> remain<$remain>\n";
64313214adfSAndy Whitcroft	push(@chunks, [ $condition, $statement ]);
644cf655043SAndy Whitcroft	if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
645cf655043SAndy Whitcroft		return ($level, $linenr, @chunks);
646cf655043SAndy Whitcroft	}
647cf655043SAndy Whitcroft
648cf655043SAndy Whitcroft	# Pull in the following conditional/block pairs and see if they
649cf655043SAndy Whitcroft	# could continue the statement.
650cf655043SAndy Whitcroft	for (;;) {
65113214adfSAndy Whitcroft		($statement, $condition, $linenr, $remain, $off, $level) =
65213214adfSAndy Whitcroft				ctx_statement_block($linenr, $remain, $off);
653cf655043SAndy Whitcroft		#print "C: c<$condition> s<$statement> remain<$remain>\n";
654773647a0SAndy Whitcroft		last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
655cf655043SAndy Whitcroft		#print "C: push\n";
656cf655043SAndy Whitcroft		push(@chunks, [ $condition, $statement ]);
65713214adfSAndy Whitcroft	}
65813214adfSAndy Whitcroft
65913214adfSAndy Whitcroft	return ($level, $linenr, @chunks);
6608905a67cSAndy Whitcroft}
6618905a67cSAndy Whitcroft
6624a0df2efSAndy Whitcroftsub ctx_block_get {
663f0a594c1SAndy Whitcroft	my ($linenr, $remain, $outer, $open, $close, $off) = @_;
6644a0df2efSAndy Whitcroft	my $line;
6654a0df2efSAndy Whitcroft	my $start = $linenr - 1;
6664a0df2efSAndy Whitcroft	my $blk = '';
6674a0df2efSAndy Whitcroft	my @o;
6684a0df2efSAndy Whitcroft	my @c;
6694a0df2efSAndy Whitcroft	my @res = ();
6704a0df2efSAndy Whitcroft
671f0a594c1SAndy Whitcroft	my $level = 0;
6724635f4fbSAndy Whitcroft	my @stack = ($level);
67300df344fSAndy Whitcroft	for ($line = $start; $remain > 0; $line++) {
67400df344fSAndy Whitcroft		next if ($rawlines[$line] =~ /^-/);
67500df344fSAndy Whitcroft		$remain--;
67600df344fSAndy Whitcroft
67700df344fSAndy Whitcroft		$blk .= $rawlines[$line];
6784635f4fbSAndy Whitcroft
6794635f4fbSAndy Whitcroft		# Handle nested #if/#else.
6804635f4fbSAndy Whitcroft		if ($rawlines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
6814635f4fbSAndy Whitcroft			push(@stack, $level);
6824635f4fbSAndy Whitcroft		} elsif ($rawlines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
6834635f4fbSAndy Whitcroft			$level = $stack[$#stack - 1];
6844635f4fbSAndy Whitcroft		} elsif ($rawlines[$line] =~ /^.\s*#\s*endif\b/) {
6854635f4fbSAndy Whitcroft			$level = pop(@stack);
6864635f4fbSAndy Whitcroft		}
6874635f4fbSAndy Whitcroft
688f0a594c1SAndy Whitcroft		foreach my $c (split(//, $rawlines[$line])) {
689f0a594c1SAndy Whitcroft			##print "C<$c>L<$level><$open$close>O<$off>\n";
690f0a594c1SAndy Whitcroft			if ($off > 0) {
691f0a594c1SAndy Whitcroft				$off--;
692f0a594c1SAndy Whitcroft				next;
693f0a594c1SAndy Whitcroft			}
6944a0df2efSAndy Whitcroft
695f0a594c1SAndy Whitcroft			if ($c eq $close && $level > 0) {
696f0a594c1SAndy Whitcroft				$level--;
697f0a594c1SAndy Whitcroft				last if ($level == 0);
698f0a594c1SAndy Whitcroft			} elsif ($c eq $open) {
699f0a594c1SAndy Whitcroft				$level++;
700f0a594c1SAndy Whitcroft			}
701f0a594c1SAndy Whitcroft		}
7024a0df2efSAndy Whitcroft
703f0a594c1SAndy Whitcroft		if (!$outer || $level <= 1) {
70400df344fSAndy Whitcroft			push(@res, $rawlines[$line]);
7054a0df2efSAndy Whitcroft		}
7064a0df2efSAndy Whitcroft
707f0a594c1SAndy Whitcroft		last if ($level == 0);
7084a0df2efSAndy Whitcroft	}
7094a0df2efSAndy Whitcroft
710f0a594c1SAndy Whitcroft	return ($level, @res);
7114a0df2efSAndy Whitcroft}
7124a0df2efSAndy Whitcroftsub ctx_block_outer {
7134a0df2efSAndy Whitcroft	my ($linenr, $remain) = @_;
7144a0df2efSAndy Whitcroft
715f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
716f0a594c1SAndy Whitcroft	return @r;
7174a0df2efSAndy Whitcroft}
7184a0df2efSAndy Whitcroftsub ctx_block {
7194a0df2efSAndy Whitcroft	my ($linenr, $remain) = @_;
7204a0df2efSAndy Whitcroft
721f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
722f0a594c1SAndy Whitcroft	return @r;
723653d4876SAndy Whitcroft}
724653d4876SAndy Whitcroftsub ctx_statement {
725f0a594c1SAndy Whitcroft	my ($linenr, $remain, $off) = @_;
726f0a594c1SAndy Whitcroft
727f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
728f0a594c1SAndy Whitcroft	return @r;
729f0a594c1SAndy Whitcroft}
730f0a594c1SAndy Whitcroftsub ctx_block_level {
731653d4876SAndy Whitcroft	my ($linenr, $remain) = @_;
732653d4876SAndy Whitcroft
733f0a594c1SAndy Whitcroft	return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
7344a0df2efSAndy Whitcroft}
7359c0ca6f9SAndy Whitcroftsub ctx_statement_level {
7369c0ca6f9SAndy Whitcroft	my ($linenr, $remain, $off) = @_;
7379c0ca6f9SAndy Whitcroft
7389c0ca6f9SAndy Whitcroft	return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
7399c0ca6f9SAndy Whitcroft}
7404a0df2efSAndy Whitcroft
7414a0df2efSAndy Whitcroftsub ctx_locate_comment {
7424a0df2efSAndy Whitcroft	my ($first_line, $end_line) = @_;
7434a0df2efSAndy Whitcroft
7444a0df2efSAndy Whitcroft	# Catch a comment on the end of the line itself.
745beae6332SAndy Whitcroft	my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
7464a0df2efSAndy Whitcroft	return $current_comment if (defined $current_comment);
7474a0df2efSAndy Whitcroft
7484a0df2efSAndy Whitcroft	# Look through the context and try and figure out if there is a
7494a0df2efSAndy Whitcroft	# comment.
7504a0df2efSAndy Whitcroft	my $in_comment = 0;
7514a0df2efSAndy Whitcroft	$current_comment = '';
7524a0df2efSAndy Whitcroft	for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
75300df344fSAndy Whitcroft		my $line = $rawlines[$linenr - 1];
75400df344fSAndy Whitcroft		#warn "           $line\n";
7554a0df2efSAndy Whitcroft		if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
7564a0df2efSAndy Whitcroft			$in_comment = 1;
7574a0df2efSAndy Whitcroft		}
7584a0df2efSAndy Whitcroft		if ($line =~ m@/\*@) {
7594a0df2efSAndy Whitcroft			$in_comment = 1;
7604a0df2efSAndy Whitcroft		}
7614a0df2efSAndy Whitcroft		if (!$in_comment && $current_comment ne '') {
7624a0df2efSAndy Whitcroft			$current_comment = '';
7634a0df2efSAndy Whitcroft		}
7644a0df2efSAndy Whitcroft		$current_comment .= $line . "\n" if ($in_comment);
7654a0df2efSAndy Whitcroft		if ($line =~ m@\*/@) {
7664a0df2efSAndy Whitcroft			$in_comment = 0;
7674a0df2efSAndy Whitcroft		}
7684a0df2efSAndy Whitcroft	}
7694a0df2efSAndy Whitcroft
7704a0df2efSAndy Whitcroft	chomp($current_comment);
7714a0df2efSAndy Whitcroft	return($current_comment);
7724a0df2efSAndy Whitcroft}
7734a0df2efSAndy Whitcroftsub ctx_has_comment {
7744a0df2efSAndy Whitcroft	my ($first_line, $end_line) = @_;
7754a0df2efSAndy Whitcroft	my $cmt = ctx_locate_comment($first_line, $end_line);
7764a0df2efSAndy Whitcroft
77700df344fSAndy Whitcroft	##print "LINE: $rawlines[$end_line - 1 ]\n";
7784a0df2efSAndy Whitcroft	##print "CMMT: $cmt\n";
7794a0df2efSAndy Whitcroft
7804a0df2efSAndy Whitcroft	return ($cmt ne '');
7814a0df2efSAndy Whitcroft}
7824a0df2efSAndy Whitcroft
7834d001e4dSAndy Whitcroftsub raw_line {
7844d001e4dSAndy Whitcroft	my ($linenr, $cnt) = @_;
7854d001e4dSAndy Whitcroft
7864d001e4dSAndy Whitcroft	my $offset = $linenr - 1;
7874d001e4dSAndy Whitcroft	$cnt++;
7884d001e4dSAndy Whitcroft
7894d001e4dSAndy Whitcroft	my $line;
7904d001e4dSAndy Whitcroft	while ($cnt) {
7914d001e4dSAndy Whitcroft		$line = $rawlines[$offset++];
7924d001e4dSAndy Whitcroft		next if (defined($line) && $line =~ /^-/);
7934d001e4dSAndy Whitcroft		$cnt--;
7944d001e4dSAndy Whitcroft	}
7954d001e4dSAndy Whitcroft
7964d001e4dSAndy Whitcroft	return $line;
7974d001e4dSAndy Whitcroft}
7984d001e4dSAndy Whitcroft
7990a920b5bSAndy Whitcroftsub cat_vet {
8000a920b5bSAndy Whitcroft	my ($vet) = @_;
8019c0ca6f9SAndy Whitcroft	my ($res, $coded);
8020a920b5bSAndy Whitcroft
8039c0ca6f9SAndy Whitcroft	$res = '';
8046c72ffaaSAndy Whitcroft	while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
8056c72ffaaSAndy Whitcroft		$res .= $1;
8066c72ffaaSAndy Whitcroft		if ($2 ne '') {
8079c0ca6f9SAndy Whitcroft			$coded = sprintf("^%c", unpack('C', $2) + 64);
8086c72ffaaSAndy Whitcroft			$res .= $coded;
8096c72ffaaSAndy Whitcroft		}
8109c0ca6f9SAndy Whitcroft	}
8119c0ca6f9SAndy Whitcroft	$res =~ s/$/\$/;
8120a920b5bSAndy Whitcroft
8139c0ca6f9SAndy Whitcroft	return $res;
8140a920b5bSAndy Whitcroft}
8150a920b5bSAndy Whitcroft
816c2fdda0dSAndy Whitcroftmy $av_preprocessor = 0;
817cf655043SAndy Whitcroftmy $av_pending;
818c2fdda0dSAndy Whitcroftmy @av_paren_type;
8191f65f947SAndy Whitcroftmy $av_pend_colon;
820c2fdda0dSAndy Whitcroft
821c2fdda0dSAndy Whitcroftsub annotate_reset {
822c2fdda0dSAndy Whitcroft	$av_preprocessor = 0;
823cf655043SAndy Whitcroft	$av_pending = '_';
824cf655043SAndy Whitcroft	@av_paren_type = ('E');
8251f65f947SAndy Whitcroft	$av_pend_colon = 'O';
826c2fdda0dSAndy Whitcroft}
827c2fdda0dSAndy Whitcroft
8286c72ffaaSAndy Whitcroftsub annotate_values {
8296c72ffaaSAndy Whitcroft	my ($stream, $type) = @_;
8306c72ffaaSAndy Whitcroft
8316c72ffaaSAndy Whitcroft	my $res;
8321f65f947SAndy Whitcroft	my $var = '_' x length($stream);
8336c72ffaaSAndy Whitcroft	my $cur = $stream;
8346c72ffaaSAndy Whitcroft
835c2fdda0dSAndy Whitcroft	print "$stream\n" if ($dbg_values > 1);
8366c72ffaaSAndy Whitcroft
8376c72ffaaSAndy Whitcroft	while (length($cur)) {
838773647a0SAndy Whitcroft		@av_paren_type = ('E') if ($#av_paren_type < 0);
839cf655043SAndy Whitcroft		print " <" . join('', @av_paren_type) .
840171ae1a4SAndy Whitcroft				"> <$type> <$av_pending>" if ($dbg_values > 1);
8416c72ffaaSAndy Whitcroft		if ($cur =~ /^(\s+)/o) {
842c2fdda0dSAndy Whitcroft			print "WS($1)\n" if ($dbg_values > 1);
843c2fdda0dSAndy Whitcroft			if ($1 =~ /\n/ && $av_preprocessor) {
844cf655043SAndy Whitcroft				$type = pop(@av_paren_type);
845c2fdda0dSAndy Whitcroft				$av_preprocessor = 0;
8466c72ffaaSAndy Whitcroft			}
8476c72ffaaSAndy Whitcroft
8489446ef56SAndy Whitcroft		} elsif ($cur =~ /^(\(\s*$Type\s*)\)/) {
8499446ef56SAndy Whitcroft			print "CAST($1)\n" if ($dbg_values > 1);
8509446ef56SAndy Whitcroft			push(@av_paren_type, $type);
8519446ef56SAndy Whitcroft			$type = 'C';
8529446ef56SAndy Whitcroft
853e91b6e26SAndy Whitcroft		} elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
854c2fdda0dSAndy Whitcroft			print "DECLARE($1)\n" if ($dbg_values > 1);
8556c72ffaaSAndy Whitcroft			$type = 'T';
8566c72ffaaSAndy Whitcroft
857389a2fe5SAndy Whitcroft		} elsif ($cur =~ /^($Modifier)\s*/) {
858389a2fe5SAndy Whitcroft			print "MODIFIER($1)\n" if ($dbg_values > 1);
859389a2fe5SAndy Whitcroft			$type = 'T';
860389a2fe5SAndy Whitcroft
861c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
862171ae1a4SAndy Whitcroft			print "DEFINE($1,$2)\n" if ($dbg_values > 1);
863c2fdda0dSAndy Whitcroft			$av_preprocessor = 1;
864171ae1a4SAndy Whitcroft			push(@av_paren_type, $type);
865171ae1a4SAndy Whitcroft			if ($2 ne '') {
866cf655043SAndy Whitcroft				$av_pending = 'N';
867171ae1a4SAndy Whitcroft			}
868171ae1a4SAndy Whitcroft			$type = 'E';
869171ae1a4SAndy Whitcroft
870c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
871171ae1a4SAndy Whitcroft			print "UNDEF($1)\n" if ($dbg_values > 1);
872171ae1a4SAndy Whitcroft			$av_preprocessor = 1;
873171ae1a4SAndy Whitcroft			push(@av_paren_type, $type);
8746c72ffaaSAndy Whitcroft
875c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
876cf655043SAndy Whitcroft			print "PRE_START($1)\n" if ($dbg_values > 1);
877c2fdda0dSAndy Whitcroft			$av_preprocessor = 1;
878cf655043SAndy Whitcroft
879cf655043SAndy Whitcroft			push(@av_paren_type, $type);
880cf655043SAndy Whitcroft			push(@av_paren_type, $type);
881171ae1a4SAndy Whitcroft			$type = 'E';
882cf655043SAndy Whitcroft
883c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
884cf655043SAndy Whitcroft			print "PRE_RESTART($1)\n" if ($dbg_values > 1);
885cf655043SAndy Whitcroft			$av_preprocessor = 1;
886cf655043SAndy Whitcroft
887cf655043SAndy Whitcroft			push(@av_paren_type, $av_paren_type[$#av_paren_type]);
888cf655043SAndy Whitcroft
889171ae1a4SAndy Whitcroft			$type = 'E';
890cf655043SAndy Whitcroft
891c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:endif))/o) {
892cf655043SAndy Whitcroft			print "PRE_END($1)\n" if ($dbg_values > 1);
893cf655043SAndy Whitcroft
894cf655043SAndy Whitcroft			$av_preprocessor = 1;
895cf655043SAndy Whitcroft
896cf655043SAndy Whitcroft			# Assume all arms of the conditional end as this
897cf655043SAndy Whitcroft			# one does, and continue as if the #endif was not here.
898cf655043SAndy Whitcroft			pop(@av_paren_type);
899cf655043SAndy Whitcroft			push(@av_paren_type, $type);
900171ae1a4SAndy Whitcroft			$type = 'E';
9016c72ffaaSAndy Whitcroft
9026c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\\\n)/o) {
903c2fdda0dSAndy Whitcroft			print "PRECONT($1)\n" if ($dbg_values > 1);
9046c72ffaaSAndy Whitcroft
905171ae1a4SAndy Whitcroft		} elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
906171ae1a4SAndy Whitcroft			print "ATTR($1)\n" if ($dbg_values > 1);
907171ae1a4SAndy Whitcroft			$av_pending = $type;
908171ae1a4SAndy Whitcroft			$type = 'N';
909171ae1a4SAndy Whitcroft
9106c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
911c2fdda0dSAndy Whitcroft			print "SIZEOF($1)\n" if ($dbg_values > 1);
9126c72ffaaSAndy Whitcroft			if (defined $2) {
913cf655043SAndy Whitcroft				$av_pending = 'V';
9146c72ffaaSAndy Whitcroft			}
9156c72ffaaSAndy Whitcroft			$type = 'N';
9166c72ffaaSAndy Whitcroft
91714b111c1SAndy Whitcroft		} elsif ($cur =~ /^(if|while|for)\b/o) {
918c2fdda0dSAndy Whitcroft			print "COND($1)\n" if ($dbg_values > 1);
91914b111c1SAndy Whitcroft			$av_pending = 'E';
9206c72ffaaSAndy Whitcroft			$type = 'N';
9216c72ffaaSAndy Whitcroft
9221f65f947SAndy Whitcroft		} elsif ($cur =~/^(case)/o) {
9231f65f947SAndy Whitcroft			print "CASE($1)\n" if ($dbg_values > 1);
9241f65f947SAndy Whitcroft			$av_pend_colon = 'C';
9251f65f947SAndy Whitcroft			$type = 'N';
9261f65f947SAndy Whitcroft
92714b111c1SAndy Whitcroft		} elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
928c2fdda0dSAndy Whitcroft			print "KEYWORD($1)\n" if ($dbg_values > 1);
9296c72ffaaSAndy Whitcroft			$type = 'N';
9306c72ffaaSAndy Whitcroft
9316c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\()/o) {
932c2fdda0dSAndy Whitcroft			print "PAREN('$1')\n" if ($dbg_values > 1);
933cf655043SAndy Whitcroft			push(@av_paren_type, $av_pending);
934cf655043SAndy Whitcroft			$av_pending = '_';
9356c72ffaaSAndy Whitcroft			$type = 'N';
9366c72ffaaSAndy Whitcroft
9376c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\))/o) {
938cf655043SAndy Whitcroft			my $new_type = pop(@av_paren_type);
939cf655043SAndy Whitcroft			if ($new_type ne '_') {
940cf655043SAndy Whitcroft				$type = $new_type;
941c2fdda0dSAndy Whitcroft				print "PAREN('$1') -> $type\n"
942c2fdda0dSAndy Whitcroft							if ($dbg_values > 1);
9436c72ffaaSAndy Whitcroft			} else {
944c2fdda0dSAndy Whitcroft				print "PAREN('$1')\n" if ($dbg_values > 1);
9456c72ffaaSAndy Whitcroft			}
9466c72ffaaSAndy Whitcroft
947c8cb2ca3SAndy Whitcroft		} elsif ($cur =~ /^($Ident)\s*\(/o) {
948c2fdda0dSAndy Whitcroft			print "FUNC($1)\n" if ($dbg_values > 1);
949c8cb2ca3SAndy Whitcroft			$type = 'V';
950cf655043SAndy Whitcroft			$av_pending = 'V';
9516c72ffaaSAndy Whitcroft
9528e761b04SAndy Whitcroft		} elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
9538e761b04SAndy Whitcroft			if (defined $2 && $type eq 'C' || $type eq 'T') {
9541f65f947SAndy Whitcroft				$av_pend_colon = 'B';
9558e761b04SAndy Whitcroft			} elsif ($type eq 'E') {
9568e761b04SAndy Whitcroft				$av_pend_colon = 'L';
9571f65f947SAndy Whitcroft			}
9581f65f947SAndy Whitcroft			print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
9591f65f947SAndy Whitcroft			$type = 'V';
9601f65f947SAndy Whitcroft
9616c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Ident|$Constant)/o) {
962c2fdda0dSAndy Whitcroft			print "IDENT($1)\n" if ($dbg_values > 1);
9636c72ffaaSAndy Whitcroft			$type = 'V';
9646c72ffaaSAndy Whitcroft
9656c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Assignment)/o) {
966c2fdda0dSAndy Whitcroft			print "ASSIGN($1)\n" if ($dbg_values > 1);
9676c72ffaaSAndy Whitcroft			$type = 'N';
9686c72ffaaSAndy Whitcroft
969cf655043SAndy Whitcroft		} elsif ($cur =~/^(;|{|})/) {
970c2fdda0dSAndy Whitcroft			print "END($1)\n" if ($dbg_values > 1);
97113214adfSAndy Whitcroft			$type = 'E';
9721f65f947SAndy Whitcroft			$av_pend_colon = 'O';
97313214adfSAndy Whitcroft
9748e761b04SAndy Whitcroft		} elsif ($cur =~/^(,)/) {
9758e761b04SAndy Whitcroft			print "COMMA($1)\n" if ($dbg_values > 1);
9768e761b04SAndy Whitcroft			$type = 'C';
9778e761b04SAndy Whitcroft
9781f65f947SAndy Whitcroft		} elsif ($cur =~ /^(\?)/o) {
9791f65f947SAndy Whitcroft			print "QUESTION($1)\n" if ($dbg_values > 1);
9801f65f947SAndy Whitcroft			$type = 'N';
9811f65f947SAndy Whitcroft
9821f65f947SAndy Whitcroft		} elsif ($cur =~ /^(:)/o) {
9831f65f947SAndy Whitcroft			print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
9841f65f947SAndy Whitcroft
9851f65f947SAndy Whitcroft			substr($var, length($res), 1, $av_pend_colon);
9861f65f947SAndy Whitcroft			if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
9871f65f947SAndy Whitcroft				$type = 'E';
9881f65f947SAndy Whitcroft			} else {
9891f65f947SAndy Whitcroft				$type = 'N';
9901f65f947SAndy Whitcroft			}
9911f65f947SAndy Whitcroft			$av_pend_colon = 'O';
9921f65f947SAndy Whitcroft
9938e761b04SAndy Whitcroft		} elsif ($cur =~ /^(\[)/o) {
99413214adfSAndy Whitcroft			print "CLOSE($1)\n" if ($dbg_values > 1);
9956c72ffaaSAndy Whitcroft			$type = 'N';
9966c72ffaaSAndy Whitcroft
9970d413866SAndy Whitcroft		} elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
99874048ed8SAndy Whitcroft			my $variant;
99974048ed8SAndy Whitcroft
100074048ed8SAndy Whitcroft			print "OPV($1)\n" if ($dbg_values > 1);
100174048ed8SAndy Whitcroft			if ($type eq 'V') {
100274048ed8SAndy Whitcroft				$variant = 'B';
100374048ed8SAndy Whitcroft			} else {
100474048ed8SAndy Whitcroft				$variant = 'U';
100574048ed8SAndy Whitcroft			}
100674048ed8SAndy Whitcroft
100774048ed8SAndy Whitcroft			substr($var, length($res), 1, $variant);
100874048ed8SAndy Whitcroft			$type = 'N';
100974048ed8SAndy Whitcroft
10106c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Operators)/o) {
1011c2fdda0dSAndy Whitcroft			print "OP($1)\n" if ($dbg_values > 1);
10126c72ffaaSAndy Whitcroft			if ($1 ne '++' && $1 ne '--') {
10136c72ffaaSAndy Whitcroft				$type = 'N';
10146c72ffaaSAndy Whitcroft			}
10156c72ffaaSAndy Whitcroft
10166c72ffaaSAndy Whitcroft		} elsif ($cur =~ /(^.)/o) {
1017c2fdda0dSAndy Whitcroft			print "C($1)\n" if ($dbg_values > 1);
10186c72ffaaSAndy Whitcroft		}
10196c72ffaaSAndy Whitcroft		if (defined $1) {
10206c72ffaaSAndy Whitcroft			$cur = substr($cur, length($1));
10216c72ffaaSAndy Whitcroft			$res .= $type x length($1);
10226c72ffaaSAndy Whitcroft		}
10236c72ffaaSAndy Whitcroft	}
10246c72ffaaSAndy Whitcroft
10251f65f947SAndy Whitcroft	return ($res, $var);
10266c72ffaaSAndy Whitcroft}
10276c72ffaaSAndy Whitcroft
10288905a67cSAndy Whitcroftsub possible {
102913214adfSAndy Whitcroft	my ($possible, $line) = @_;
10309a974fdbSAndy Whitcroft	my $notPermitted = qr{(?:
10310776e594SAndy Whitcroft		^(?:
10320776e594SAndy Whitcroft			$Modifier|
10330776e594SAndy Whitcroft			$Storage|
10340776e594SAndy Whitcroft			$Type|
10359a974fdbSAndy Whitcroft			DEFINE_\S+
10369a974fdbSAndy Whitcroft		)$|
10379a974fdbSAndy Whitcroft		^(?:
10380776e594SAndy Whitcroft			goto|
10390776e594SAndy Whitcroft			return|
10400776e594SAndy Whitcroft			case|
10410776e594SAndy Whitcroft			else|
10420776e594SAndy Whitcroft			asm|__asm__|
10430776e594SAndy Whitcroft			do
10449a974fdbSAndy Whitcroft		)(?:\s|$)|
10450776e594SAndy Whitcroft		^(?:typedef|struct|enum)\b
10469a974fdbSAndy Whitcroft	    )}x;
10479a974fdbSAndy Whitcroft	warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
10489a974fdbSAndy Whitcroft	if ($possible !~ $notPermitted) {
1049c45dcabdSAndy Whitcroft		# Check for modifiers.
1050c45dcabdSAndy Whitcroft		$possible =~ s/\s*$Storage\s*//g;
1051c45dcabdSAndy Whitcroft		$possible =~ s/\s*$Sparse\s*//g;
1052c45dcabdSAndy Whitcroft		if ($possible =~ /^\s*$/) {
1053c45dcabdSAndy Whitcroft
1054c45dcabdSAndy Whitcroft		} elsif ($possible =~ /\s/) {
1055c45dcabdSAndy Whitcroft			$possible =~ s/\s*$Type\s*//g;
1056d2506586SAndy Whitcroft			for my $modifier (split(' ', $possible)) {
10579a974fdbSAndy Whitcroft				if ($modifier !~ $notPermitted) {
1058d2506586SAndy Whitcroft					warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1059d2506586SAndy Whitcroft					push(@modifierList, $modifier);
1060d2506586SAndy Whitcroft				}
10619a974fdbSAndy Whitcroft			}
1062c45dcabdSAndy Whitcroft
1063c45dcabdSAndy Whitcroft		} else {
106413214adfSAndy Whitcroft			warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
10658905a67cSAndy Whitcroft			push(@typeList, $possible);
1066c45dcabdSAndy Whitcroft		}
10678905a67cSAndy Whitcroft		build_types();
10680776e594SAndy Whitcroft	} else {
10690776e594SAndy Whitcroft		warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
10708905a67cSAndy Whitcroft	}
10718905a67cSAndy Whitcroft}
10728905a67cSAndy Whitcroft
10736c72ffaaSAndy Whitcroftmy $prefix = '';
10746c72ffaaSAndy Whitcroft
1075f0a594c1SAndy Whitcroftsub report {
1076773647a0SAndy Whitcroft	if (defined $tst_only && $_[0] !~ /\Q$tst_only\E/) {
1077773647a0SAndy Whitcroft		return 0;
1078773647a0SAndy Whitcroft	}
10798905a67cSAndy Whitcroft	my $line = $prefix . $_[0];
10808905a67cSAndy Whitcroft
10818905a67cSAndy Whitcroft	$line = (split('\n', $line))[0] . "\n" if ($terse);
10828905a67cSAndy Whitcroft
108313214adfSAndy Whitcroft	push(our @report, $line);
1084773647a0SAndy Whitcroft
1085773647a0SAndy Whitcroft	return 1;
1086f0a594c1SAndy Whitcroft}
1087f0a594c1SAndy Whitcroftsub report_dump {
108813214adfSAndy Whitcroft	our @report;
1089f0a594c1SAndy Whitcroft}
1090de7d4f0eSAndy Whitcroftsub ERROR {
1091773647a0SAndy Whitcroft	if (report("ERROR: $_[0]\n")) {
1092de7d4f0eSAndy Whitcroft		our $clean = 0;
10936c72ffaaSAndy Whitcroft		our $cnt_error++;
1094de7d4f0eSAndy Whitcroft	}
1095773647a0SAndy Whitcroft}
1096de7d4f0eSAndy Whitcroftsub WARN {
1097773647a0SAndy Whitcroft	if (report("WARNING: $_[0]\n")) {
1098de7d4f0eSAndy Whitcroft		our $clean = 0;
10996c72ffaaSAndy Whitcroft		our $cnt_warn++;
1100de7d4f0eSAndy Whitcroft	}
1101773647a0SAndy Whitcroft}
1102de7d4f0eSAndy Whitcroftsub CHK {
1103773647a0SAndy Whitcroft	if ($check && report("CHECK: $_[0]\n")) {
1104de7d4f0eSAndy Whitcroft		our $clean = 0;
11056c72ffaaSAndy Whitcroft		our $cnt_chk++;
11066c72ffaaSAndy Whitcroft	}
1107de7d4f0eSAndy Whitcroft}
1108de7d4f0eSAndy Whitcroft
11096ecd9674SAndy Whitcroftsub check_absolute_file {
11106ecd9674SAndy Whitcroft	my ($absolute, $herecurr) = @_;
11116ecd9674SAndy Whitcroft	my $file = $absolute;
11126ecd9674SAndy Whitcroft
11136ecd9674SAndy Whitcroft	##print "absolute<$absolute>\n";
11146ecd9674SAndy Whitcroft
11156ecd9674SAndy Whitcroft	# See if any suffix of this path is a path within the tree.
11166ecd9674SAndy Whitcroft	while ($file =~ s@^[^/]*/@@) {
11176ecd9674SAndy Whitcroft		if (-f "$root/$file") {
11186ecd9674SAndy Whitcroft			##print "file<$file>\n";
11196ecd9674SAndy Whitcroft			last;
11206ecd9674SAndy Whitcroft		}
11216ecd9674SAndy Whitcroft	}
11226ecd9674SAndy Whitcroft	if (! -f _)  {
11236ecd9674SAndy Whitcroft		return 0;
11246ecd9674SAndy Whitcroft	}
11256ecd9674SAndy Whitcroft
11266ecd9674SAndy Whitcroft	# It is, so see if the prefix is acceptable.
11276ecd9674SAndy Whitcroft	my $prefix = $absolute;
11286ecd9674SAndy Whitcroft	substr($prefix, -length($file)) = '';
11296ecd9674SAndy Whitcroft
11306ecd9674SAndy Whitcroft	##print "prefix<$prefix>\n";
11316ecd9674SAndy Whitcroft	if ($prefix ne ".../") {
11326ecd9674SAndy Whitcroft		WARN("use relative pathname instead of absolute in changelog text\n" . $herecurr);
11336ecd9674SAndy Whitcroft	}
11346ecd9674SAndy Whitcroft}
11356ecd9674SAndy Whitcroft
11360a920b5bSAndy Whitcroftsub process {
11370a920b5bSAndy Whitcroft	my $filename = shift;
11380a920b5bSAndy Whitcroft
11390a920b5bSAndy Whitcroft	my $linenr=0;
11400a920b5bSAndy Whitcroft	my $prevline="";
1141c2fdda0dSAndy Whitcroft	my $prevrawline="";
11420a920b5bSAndy Whitcroft	my $stashline="";
1143c2fdda0dSAndy Whitcroft	my $stashrawline="";
11440a920b5bSAndy Whitcroft
11454a0df2efSAndy Whitcroft	my $length;
11460a920b5bSAndy Whitcroft	my $indent;
11470a920b5bSAndy Whitcroft	my $previndent=0;
11480a920b5bSAndy Whitcroft	my $stashindent=0;
11490a920b5bSAndy Whitcroft
1150de7d4f0eSAndy Whitcroft	our $clean = 1;
11510a920b5bSAndy Whitcroft	my $signoff = 0;
11520a920b5bSAndy Whitcroft	my $is_patch = 0;
11530a920b5bSAndy Whitcroft
115413214adfSAndy Whitcroft	our @report = ();
11556c72ffaaSAndy Whitcroft	our $cnt_lines = 0;
11566c72ffaaSAndy Whitcroft	our $cnt_error = 0;
11576c72ffaaSAndy Whitcroft	our $cnt_warn = 0;
11586c72ffaaSAndy Whitcroft	our $cnt_chk = 0;
11596c72ffaaSAndy Whitcroft
11600a920b5bSAndy Whitcroft	# Trace the real file/line as we go.
11610a920b5bSAndy Whitcroft	my $realfile = '';
11620a920b5bSAndy Whitcroft	my $realline = 0;
11630a920b5bSAndy Whitcroft	my $realcnt = 0;
11640a920b5bSAndy Whitcroft	my $here = '';
11650a920b5bSAndy Whitcroft	my $in_comment = 0;
1166c2fdda0dSAndy Whitcroft	my $comment_edge = 0;
11670a920b5bSAndy Whitcroft	my $first_line = 0;
11681e855726SWolfram Sang	my $p1_prefix = '';
11690a920b5bSAndy Whitcroft
117013214adfSAndy Whitcroft	my $prev_values = 'E';
117113214adfSAndy Whitcroft
117213214adfSAndy Whitcroft	# suppression flags
1173773647a0SAndy Whitcroft	my %suppress_ifbraces;
1174170d3a22SAndy Whitcroft	my %suppress_whiletrailers;
11752b474a1aSAndy Whitcroft	my %suppress_export;
1176653d4876SAndy Whitcroft
1177c2fdda0dSAndy Whitcroft	# Pre-scan the patch sanitizing the lines.
1178de7d4f0eSAndy Whitcroft	# Pre-scan the patch looking for any __setup documentation.
1179c2fdda0dSAndy Whitcroft	#
1180de7d4f0eSAndy Whitcroft	my @setup_docs = ();
1181de7d4f0eSAndy Whitcroft	my $setup_docs = 0;
1182773647a0SAndy Whitcroft
1183773647a0SAndy Whitcroft	sanitise_line_reset();
1184c2fdda0dSAndy Whitcroft	my $line;
1185c2fdda0dSAndy Whitcroft	foreach my $rawline (@rawlines) {
1186773647a0SAndy Whitcroft		$linenr++;
1187773647a0SAndy Whitcroft		$line = $rawline;
1188c2fdda0dSAndy Whitcroft
1189773647a0SAndy Whitcroft		if ($rawline=~/^\+\+\+\s+(\S+)/) {
1190de7d4f0eSAndy Whitcroft			$setup_docs = 0;
1191de7d4f0eSAndy Whitcroft			if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
1192de7d4f0eSAndy Whitcroft				$setup_docs = 1;
1193de7d4f0eSAndy Whitcroft			}
1194773647a0SAndy Whitcroft			#next;
1195de7d4f0eSAndy Whitcroft		}
1196773647a0SAndy Whitcroft		if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1197773647a0SAndy Whitcroft			$realline=$1-1;
1198773647a0SAndy Whitcroft			if (defined $2) {
1199773647a0SAndy Whitcroft				$realcnt=$3+1;
1200773647a0SAndy Whitcroft			} else {
1201773647a0SAndy Whitcroft				$realcnt=1+1;
1202773647a0SAndy Whitcroft			}
1203c45dcabdSAndy Whitcroft			$in_comment = 0;
1204773647a0SAndy Whitcroft
1205773647a0SAndy Whitcroft			# Guestimate if this is a continuing comment.  Run
1206773647a0SAndy Whitcroft			# the context looking for a comment "edge".  If this
1207773647a0SAndy Whitcroft			# edge is a close comment then we must be in a comment
1208773647a0SAndy Whitcroft			# at context start.
1209773647a0SAndy Whitcroft			my $edge;
121001fa9147SAndy Whitcroft			my $cnt = $realcnt;
121101fa9147SAndy Whitcroft			for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
121201fa9147SAndy Whitcroft				next if (defined $rawlines[$ln - 1] &&
121301fa9147SAndy Whitcroft					 $rawlines[$ln - 1] =~ /^-/);
121401fa9147SAndy Whitcroft				$cnt--;
121501fa9147SAndy Whitcroft				#print "RAW<$rawlines[$ln - 1]>\n";
1216721c1cb6SAndy Whitcroft				last if (!defined $rawlines[$ln - 1]);
1217fae17daeSAndy Whitcroft				if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
1218fae17daeSAndy Whitcroft				    $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
1219fae17daeSAndy Whitcroft					($edge) = $1;
1220fae17daeSAndy Whitcroft					last;
1221fae17daeSAndy Whitcroft				}
1222773647a0SAndy Whitcroft			}
1223773647a0SAndy Whitcroft			if (defined $edge && $edge eq '*/') {
1224773647a0SAndy Whitcroft				$in_comment = 1;
1225773647a0SAndy Whitcroft			}
1226773647a0SAndy Whitcroft
1227773647a0SAndy Whitcroft			# Guestimate if this is a continuing comment.  If this
1228773647a0SAndy Whitcroft			# is the start of a diff block and this line starts
1229773647a0SAndy Whitcroft			# ' *' then it is very likely a comment.
1230773647a0SAndy Whitcroft			if (!defined $edge &&
123183242e0cSAndy Whitcroft			    $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
1232773647a0SAndy Whitcroft			{
1233773647a0SAndy Whitcroft				$in_comment = 1;
1234773647a0SAndy Whitcroft			}
1235773647a0SAndy Whitcroft
1236773647a0SAndy Whitcroft			##print "COMMENT:$in_comment edge<$edge> $rawline\n";
1237773647a0SAndy Whitcroft			sanitise_line_reset($in_comment);
1238773647a0SAndy Whitcroft
1239171ae1a4SAndy Whitcroft		} elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
1240773647a0SAndy Whitcroft			# Standardise the strings and chars within the input to
1241171ae1a4SAndy Whitcroft			# simplify matching -- only bother with positive lines.
1242773647a0SAndy Whitcroft			$line = sanitise_line($rawline);
1243773647a0SAndy Whitcroft		}
1244773647a0SAndy Whitcroft		push(@lines, $line);
1245773647a0SAndy Whitcroft
1246773647a0SAndy Whitcroft		if ($realcnt > 1) {
1247773647a0SAndy Whitcroft			$realcnt-- if ($line =~ /^(?:\+| |$)/);
1248773647a0SAndy Whitcroft		} else {
1249773647a0SAndy Whitcroft			$realcnt = 0;
1250773647a0SAndy Whitcroft		}
1251773647a0SAndy Whitcroft
1252773647a0SAndy Whitcroft		#print "==>$rawline\n";
1253773647a0SAndy Whitcroft		#print "-->$line\n";
1254de7d4f0eSAndy Whitcroft
1255de7d4f0eSAndy Whitcroft		if ($setup_docs && $line =~ /^\+/) {
1256de7d4f0eSAndy Whitcroft			push(@setup_docs, $line);
1257de7d4f0eSAndy Whitcroft		}
1258de7d4f0eSAndy Whitcroft	}
1259de7d4f0eSAndy Whitcroft
12606c72ffaaSAndy Whitcroft	$prefix = '';
12616c72ffaaSAndy Whitcroft
1262773647a0SAndy Whitcroft	$realcnt = 0;
1263773647a0SAndy Whitcroft	$linenr = 0;
12640a920b5bSAndy Whitcroft	foreach my $line (@lines) {
12650a920b5bSAndy Whitcroft		$linenr++;
12660a920b5bSAndy Whitcroft
1267c2fdda0dSAndy Whitcroft		my $rawline = $rawlines[$linenr - 1];
12686c72ffaaSAndy Whitcroft
12690a920b5bSAndy Whitcroft#extract the line range in the file after the patch is applied
12706c72ffaaSAndy Whitcroft		if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
12710a920b5bSAndy Whitcroft			$is_patch = 1;
12724a0df2efSAndy Whitcroft			$first_line = $linenr + 1;
12730a920b5bSAndy Whitcroft			$realline=$1-1;
12740a920b5bSAndy Whitcroft			if (defined $2) {
12750a920b5bSAndy Whitcroft				$realcnt=$3+1;
12760a920b5bSAndy Whitcroft			} else {
12770a920b5bSAndy Whitcroft				$realcnt=1+1;
12780a920b5bSAndy Whitcroft			}
1279c2fdda0dSAndy Whitcroft			annotate_reset();
128013214adfSAndy Whitcroft			$prev_values = 'E';
128113214adfSAndy Whitcroft
1282773647a0SAndy Whitcroft			%suppress_ifbraces = ();
1283170d3a22SAndy Whitcroft			%suppress_whiletrailers = ();
12842b474a1aSAndy Whitcroft			%suppress_export = ();
12850a920b5bSAndy Whitcroft			next;
12860a920b5bSAndy Whitcroft
12874a0df2efSAndy Whitcroft# track the line number as we move through the hunk, note that
12884a0df2efSAndy Whitcroft# new versions of GNU diff omit the leading space on completely
12894a0df2efSAndy Whitcroft# blank context lines so we need to count that too.
1290773647a0SAndy Whitcroft		} elsif ($line =~ /^( |\+|$)/) {
12910a920b5bSAndy Whitcroft			$realline++;
1292d8aaf121SAndy Whitcroft			$realcnt-- if ($realcnt != 0);
12930a920b5bSAndy Whitcroft
12944a0df2efSAndy Whitcroft			# Measure the line length and indent.
1295c2fdda0dSAndy Whitcroft			($length, $indent) = line_stats($rawline);
12960a920b5bSAndy Whitcroft
12970a920b5bSAndy Whitcroft			# Track the previous line.
12980a920b5bSAndy Whitcroft			($prevline, $stashline) = ($stashline, $line);
12990a920b5bSAndy Whitcroft			($previndent, $stashindent) = ($stashindent, $indent);
1300c2fdda0dSAndy Whitcroft			($prevrawline, $stashrawline) = ($stashrawline, $rawline);
1301c2fdda0dSAndy Whitcroft
1302773647a0SAndy Whitcroft			#warn "line<$line>\n";
13036c72ffaaSAndy Whitcroft
1304d8aaf121SAndy Whitcroft		} elsif ($realcnt == 1) {
1305d8aaf121SAndy Whitcroft			$realcnt--;
13060a920b5bSAndy Whitcroft		}
13070a920b5bSAndy Whitcroft
1308cc77cdcaSAndy Whitcroft		my $hunk_line = ($realcnt != 0);
1309cc77cdcaSAndy Whitcroft
13100a920b5bSAndy Whitcroft#make up the handle for any error we report on this line
1311773647a0SAndy Whitcroft		$prefix = "$filename:$realline: " if ($emacs && $file);
1312773647a0SAndy Whitcroft		$prefix = "$filename:$linenr: " if ($emacs && !$file);
1313773647a0SAndy Whitcroft
13146c72ffaaSAndy Whitcroft		$here = "#$linenr: " if (!$file);
13156c72ffaaSAndy Whitcroft		$here = "#$realline: " if ($file);
1316773647a0SAndy Whitcroft
1317773647a0SAndy Whitcroft		# extract the filename as it passes
1318*3bf9a009SRabin Vincent		if ($line =~ /^diff --git.*?(\S+)$/) {
1319*3bf9a009SRabin Vincent			$realfile = $1;
1320*3bf9a009SRabin Vincent			$realfile =~ s@^([^/]*)/@@;
1321*3bf9a009SRabin Vincent
1322*3bf9a009SRabin Vincent		} elsif ($line =~ /^\+\+\+\s+(\S+)/) {
1323773647a0SAndy Whitcroft			$realfile = $1;
13241e855726SWolfram Sang			$realfile =~ s@^([^/]*)/@@;
13251e855726SWolfram Sang
13261e855726SWolfram Sang			$p1_prefix = $1;
1327e2f7aa4bSAndy Whitcroft			if (!$file && $tree && $p1_prefix ne '' &&
1328e2f7aa4bSAndy Whitcroft			    -e "$root/$p1_prefix") {
13291e855726SWolfram Sang				WARN("patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
13301e855726SWolfram Sang			}
1331773647a0SAndy Whitcroft
1332c1ab3326SAndy Whitcroft			if ($realfile =~ m@^include/asm/@) {
1333773647a0SAndy Whitcroft				ERROR("do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
1334773647a0SAndy Whitcroft			}
1335773647a0SAndy Whitcroft			next;
1336773647a0SAndy Whitcroft		}
1337773647a0SAndy Whitcroft
1338389834b6SRandy Dunlap		$here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
13390a920b5bSAndy Whitcroft
1340c2fdda0dSAndy Whitcroft		my $hereline = "$here\n$rawline\n";
1341c2fdda0dSAndy Whitcroft		my $herecurr = "$here\n$rawline\n";
1342c2fdda0dSAndy Whitcroft		my $hereprev = "$here\n$prevrawline\n$rawline\n";
13430a920b5bSAndy Whitcroft
13446c72ffaaSAndy Whitcroft		$cnt_lines++ if ($realcnt != 0);
13456c72ffaaSAndy Whitcroft
1346*3bf9a009SRabin Vincent# Check for incorrect file permissions
1347*3bf9a009SRabin Vincent		if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
1348*3bf9a009SRabin Vincent			my $permhere = $here . "FILE: $realfile\n";
1349*3bf9a009SRabin Vincent			if ($realfile =~ /(Makefile|Kconfig|\.c|\.h|\.S|\.tmpl)$/) {
1350*3bf9a009SRabin Vincent				ERROR("do not set execute permissions for source files\n" . $permhere);
1351*3bf9a009SRabin Vincent			}
1352*3bf9a009SRabin Vincent		}
1353*3bf9a009SRabin Vincent
13540a920b5bSAndy Whitcroft#check the patch for a signoff:
1355d8aaf121SAndy Whitcroft		if ($line =~ /^\s*signed-off-by:/i) {
13564a0df2efSAndy Whitcroft			# This is a signoff, if ugly, so do not double report.
13574a0df2efSAndy Whitcroft			$signoff++;
13580a920b5bSAndy Whitcroft			if (!($line =~ /^\s*Signed-off-by:/)) {
1359de7d4f0eSAndy Whitcroft				WARN("Signed-off-by: is the preferred form\n" .
1360de7d4f0eSAndy Whitcroft					$herecurr);
13610a920b5bSAndy Whitcroft			}
13620a920b5bSAndy Whitcroft			if ($line =~ /^\s*signed-off-by:\S/i) {
1363773647a0SAndy Whitcroft				WARN("space required after Signed-off-by:\n" .
1364de7d4f0eSAndy Whitcroft					$herecurr);
13650a920b5bSAndy Whitcroft			}
13660a920b5bSAndy Whitcroft		}
13670a920b5bSAndy Whitcroft
136800df344fSAndy Whitcroft# Check for wrappage within a valid hunk of the file
13698905a67cSAndy Whitcroft		if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
1370de7d4f0eSAndy Whitcroft			ERROR("patch seems to be corrupt (line wrapped?)\n" .
13716c72ffaaSAndy Whitcroft				$herecurr) if (!$emitted_corrupt++);
1372de7d4f0eSAndy Whitcroft		}
1373de7d4f0eSAndy Whitcroft
13746ecd9674SAndy Whitcroft# Check for absolute kernel paths.
13756ecd9674SAndy Whitcroft		if ($tree) {
13766ecd9674SAndy Whitcroft			while ($line =~ m{(?:^|\s)(/\S*)}g) {
13776ecd9674SAndy Whitcroft				my $file = $1;
13786ecd9674SAndy Whitcroft
13796ecd9674SAndy Whitcroft				if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
13806ecd9674SAndy Whitcroft				    check_absolute_file($1, $herecurr)) {
13816ecd9674SAndy Whitcroft					#
13826ecd9674SAndy Whitcroft				} else {
13836ecd9674SAndy Whitcroft					check_absolute_file($file, $herecurr);
13846ecd9674SAndy Whitcroft				}
13856ecd9674SAndy Whitcroft			}
13866ecd9674SAndy Whitcroft		}
13876ecd9674SAndy Whitcroft
1388de7d4f0eSAndy Whitcroft# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
1389de7d4f0eSAndy Whitcroft		if (($realfile =~ /^$/ || $line =~ /^\+/) &&
1390171ae1a4SAndy Whitcroft		    $rawline !~ m/^$UTF8*$/) {
1391171ae1a4SAndy Whitcroft			my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
1392171ae1a4SAndy Whitcroft
1393171ae1a4SAndy Whitcroft			my $blank = copy_spacing($rawline);
1394171ae1a4SAndy Whitcroft			my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
1395171ae1a4SAndy Whitcroft			my $hereptr = "$hereline$ptr\n";
1396171ae1a4SAndy Whitcroft
1397171ae1a4SAndy Whitcroft			ERROR("Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
139800df344fSAndy Whitcroft		}
13990a920b5bSAndy Whitcroft
140030670854SAndy Whitcroft# ignore non-hunk lines and lines being removed
140130670854SAndy Whitcroft		next if (!$hunk_line || $line =~ /^-/);
140200df344fSAndy Whitcroft
14030a920b5bSAndy Whitcroft#trailing whitespace
14049c0ca6f9SAndy Whitcroft		if ($line =~ /^\+.*\015/) {
1405c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
14069c0ca6f9SAndy Whitcroft			ERROR("DOS line endings\n" . $herevet);
14079c0ca6f9SAndy Whitcroft
1408c2fdda0dSAndy Whitcroft		} elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
1409c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1410de7d4f0eSAndy Whitcroft			ERROR("trailing whitespace\n" . $herevet);
1411d2c0a235SAndy Whitcroft			$rpt_cleaners = 1;
14120a920b5bSAndy Whitcroft		}
14135368df20SAndy Whitcroft
14143354957aSAndi Kleen# check for Kconfig help text having a real description
14159fe287d7SAndy Whitcroft# Only applies when adding the entry originally, after that we do not have
14169fe287d7SAndy Whitcroft# sufficient context to determine whether it is indeed long enough.
14173354957aSAndi Kleen		if ($realfile =~ /Kconfig/ &&
14189fe287d7SAndy Whitcroft		    $line =~ /\+\s*(?:---)?help(?:---)?$/) {
14193354957aSAndi Kleen			my $length = 0;
14209fe287d7SAndy Whitcroft			my $cnt = $realcnt;
14219fe287d7SAndy Whitcroft			my $ln = $linenr + 1;
14229fe287d7SAndy Whitcroft			my $f;
14239fe287d7SAndy Whitcroft			my $is_end = 0;
14249fe287d7SAndy Whitcroft			while ($cnt > 0 && defined $lines[$ln - 1]) {
14259fe287d7SAndy Whitcroft				$f = $lines[$ln - 1];
14269fe287d7SAndy Whitcroft				$cnt-- if ($lines[$ln - 1] !~ /^-/);
14279fe287d7SAndy Whitcroft				$is_end = $lines[$ln - 1] =~ /^\+/;
14289fe287d7SAndy Whitcroft				$ln++;
14299fe287d7SAndy Whitcroft
14309fe287d7SAndy Whitcroft				next if ($f =~ /^-/);
14319fe287d7SAndy Whitcroft				$f =~ s/^.//;
14323354957aSAndi Kleen				$f =~ s/#.*//;
14333354957aSAndi Kleen				$f =~ s/^\s+//;
14343354957aSAndi Kleen				next if ($f =~ /^$/);
14359fe287d7SAndy Whitcroft				if ($f =~ /^\s*config\s/) {
14369fe287d7SAndy Whitcroft					$is_end = 1;
14379fe287d7SAndy Whitcroft					last;
14389fe287d7SAndy Whitcroft				}
14393354957aSAndi Kleen				$length++;
14403354957aSAndi Kleen			}
14419fe287d7SAndy Whitcroft			WARN("please write a paragraph that describes the config symbol fully\n" . $herecurr) if ($is_end && $length < 4);
14429fe287d7SAndy Whitcroft			#print "is_end<$is_end> length<$length>\n";
14433354957aSAndi Kleen		}
14443354957aSAndi Kleen
14455368df20SAndy Whitcroft# check we are in a valid source file if not then ignore this hunk
14465368df20SAndy Whitcroft		next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
14475368df20SAndy Whitcroft
14480a920b5bSAndy Whitcroft#80 column limit
1449c45dcabdSAndy Whitcroft		if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ &&
1450f4c014c0SAndy Whitcroft		    $rawline !~ /^.\s*\*\s*\@$Ident\s/ &&
14518bbea968SJoe Perches		    !($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(KERN_\S+\s*|[^"]*))?"[X\t]*"\s*(?:,|\)\s*;)\s*$/ ||
14528bbea968SJoe Perches		    $line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) &&
1453f4c014c0SAndy Whitcroft		    $length > 80)
1454c45dcabdSAndy Whitcroft		{
1455de7d4f0eSAndy Whitcroft			WARN("line over 80 characters\n" . $herecurr);
14560a920b5bSAndy Whitcroft		}
14570a920b5bSAndy Whitcroft
14585e79d96eSJoe Perches# check for spaces before a quoted newline
14595e79d96eSJoe Perches		if ($rawline =~ /^.*\".*\s\\n/) {
14605e79d96eSJoe Perches			WARN("unnecessary whitespace before a quoted newline\n" . $herecurr);
14615e79d96eSJoe Perches		}
14625e79d96eSJoe Perches
14638905a67cSAndy Whitcroft# check for adding lines without a newline.
14648905a67cSAndy Whitcroft		if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
14658905a67cSAndy Whitcroft			WARN("adding a line without newline at end of file\n" . $herecurr);
14668905a67cSAndy Whitcroft		}
14678905a67cSAndy Whitcroft
146842e41c54SMike Frysinger# Blackfin: use hi/lo macros
146942e41c54SMike Frysinger		if ($realfile =~ m@arch/blackfin/.*\.S$@) {
147042e41c54SMike Frysinger			if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
147142e41c54SMike Frysinger				my $herevet = "$here\n" . cat_vet($line) . "\n";
147242e41c54SMike Frysinger				ERROR("use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
147342e41c54SMike Frysinger			}
147442e41c54SMike Frysinger			if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
147542e41c54SMike Frysinger				my $herevet = "$here\n" . cat_vet($line) . "\n";
147642e41c54SMike Frysinger				ERROR("use the HI() macro, not (... >> 16)\n" . $herevet);
147742e41c54SMike Frysinger			}
147842e41c54SMike Frysinger		}
147942e41c54SMike Frysinger
1480b9ea10d6SAndy Whitcroft# check we are in a valid source file C or perl if not then ignore this hunk
1481b9ea10d6SAndy Whitcroft		next if ($realfile !~ /\.(h|c|pl)$/);
14820a920b5bSAndy Whitcroft
14830a920b5bSAndy Whitcroft# at the beginning of a line any tabs must come first and anything
14840a920b5bSAndy Whitcroft# more than 8 must use tabs.
1485c2fdda0dSAndy Whitcroft		if ($rawline =~ /^\+\s* \t\s*\S/ ||
1486c2fdda0dSAndy Whitcroft		    $rawline =~ /^\+\s*        \s*/) {
1487c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1488171ae1a4SAndy Whitcroft			ERROR("code indent should use tabs where possible\n" . $herevet);
1489d2c0a235SAndy Whitcroft			$rpt_cleaners = 1;
14900a920b5bSAndy Whitcroft		}
14910a920b5bSAndy Whitcroft
149208e44365SAlberto Panizzo# check for space before tabs.
149308e44365SAlberto Panizzo		if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
149408e44365SAlberto Panizzo			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
149508e44365SAlberto Panizzo			WARN("please, no space before tabs\n" . $herevet);
149608e44365SAlberto Panizzo		}
149708e44365SAlberto Panizzo
14985f7ddae6SRaffaele Recalcati# check for spaces at the beginning of a line.
14996b4c5bebSAndy Whitcroft# Exceptions:
15006b4c5bebSAndy Whitcroft#  1) within comments
15016b4c5bebSAndy Whitcroft#  2) indented preprocessor commands
15026b4c5bebSAndy Whitcroft#  3) hanging labels
15036b4c5bebSAndy Whitcroft		if ($rawline =~ /^\+ / && $line !~ /\+ *(?:$;|#|$Ident:)/)  {
15045f7ddae6SRaffaele Recalcati			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
15056b4c5bebSAndy Whitcroft			WARN("please, no spaces at the start of a line\n" . $herevet);
15065f7ddae6SRaffaele Recalcati		}
15075f7ddae6SRaffaele Recalcati
1508b9ea10d6SAndy Whitcroft# check we are in a valid C source file if not then ignore this hunk
1509b9ea10d6SAndy Whitcroft		next if ($realfile !~ /\.(h|c)$/);
1510b9ea10d6SAndy Whitcroft
1511c2fdda0dSAndy Whitcroft# check for RCS/CVS revision markers
1512cf655043SAndy Whitcroft		if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
1513c2fdda0dSAndy Whitcroft			WARN("CVS style keyword markers, these will _not_ be updated\n". $herecurr);
1514c2fdda0dSAndy Whitcroft		}
151522f2a2efSAndy Whitcroft
151642e41c54SMike Frysinger# Blackfin: don't use __builtin_bfin_[cs]sync
151742e41c54SMike Frysinger		if ($line =~ /__builtin_bfin_csync/) {
151842e41c54SMike Frysinger			my $herevet = "$here\n" . cat_vet($line) . "\n";
151942e41c54SMike Frysinger			ERROR("use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
152042e41c54SMike Frysinger		}
152142e41c54SMike Frysinger		if ($line =~ /__builtin_bfin_ssync/) {
152242e41c54SMike Frysinger			my $herevet = "$here\n" . cat_vet($line) . "\n";
152342e41c54SMike Frysinger			ERROR("use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
152442e41c54SMike Frysinger		}
152542e41c54SMike Frysinger
15269c0ca6f9SAndy Whitcroft# Check for potential 'bare' types
15272b474a1aSAndy Whitcroft		my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
15282b474a1aSAndy Whitcroft		    $realline_next);
15299c9ba34eSAndy Whitcroft		if ($realcnt && $line =~ /.\s*\S/) {
1530170d3a22SAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
1531f5fe35ddSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0);
1532171ae1a4SAndy Whitcroft			$stat =~ s/\n./\n /g;
1533171ae1a4SAndy Whitcroft			$cond =~ s/\n./\n /g;
1534171ae1a4SAndy Whitcroft
15352b474a1aSAndy Whitcroft			# Find the real next line.
15362b474a1aSAndy Whitcroft			$realline_next = $line_nr_next;
15372b474a1aSAndy Whitcroft			if (defined $realline_next &&
15382b474a1aSAndy Whitcroft			    (!defined $lines[$realline_next - 1] ||
15392b474a1aSAndy Whitcroft			     substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
15402b474a1aSAndy Whitcroft				$realline_next++;
15412b474a1aSAndy Whitcroft			}
15422b474a1aSAndy Whitcroft
1543171ae1a4SAndy Whitcroft			my $s = $stat;
1544171ae1a4SAndy Whitcroft			$s =~ s/{.*$//s;
1545cf655043SAndy Whitcroft
1546c2fdda0dSAndy Whitcroft			# Ignore goto labels.
1547171ae1a4SAndy Whitcroft			if ($s =~ /$Ident:\*$/s) {
1548c2fdda0dSAndy Whitcroft
1549c2fdda0dSAndy Whitcroft			# Ignore functions being called
1550171ae1a4SAndy Whitcroft			} elsif ($s =~ /^.\s*$Ident\s*\(/s) {
1551c2fdda0dSAndy Whitcroft
1552463f2864SAndy Whitcroft			} elsif ($s =~ /^.\s*else\b/s) {
1553463f2864SAndy Whitcroft
1554c45dcabdSAndy Whitcroft			# declarations always start with types
1555d2506586SAndy 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) {
1556c45dcabdSAndy Whitcroft				my $type = $1;
1557c45dcabdSAndy Whitcroft				$type =~ s/\s+/ /g;
1558c45dcabdSAndy Whitcroft				possible($type, "A:" . $s);
1559c45dcabdSAndy Whitcroft
15606c72ffaaSAndy Whitcroft			# definitions in global scope can only start with types
1561a6a84062SAndy Whitcroft			} elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
1562c45dcabdSAndy Whitcroft				possible($1, "B:" . $s);
1563c2fdda0dSAndy Whitcroft			}
15648905a67cSAndy Whitcroft
15656c72ffaaSAndy Whitcroft			# any (foo ... *) is a pointer cast, and foo is a type
156665863862SAndy Whitcroft			while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
1567c45dcabdSAndy Whitcroft				possible($1, "C:" . $s);
15689c0ca6f9SAndy Whitcroft			}
15698905a67cSAndy Whitcroft
15708905a67cSAndy Whitcroft			# Check for any sort of function declaration.
15718905a67cSAndy Whitcroft			# int foo(something bar, other baz);
15728905a67cSAndy Whitcroft			# void (*store_gdt)(x86_descr_ptr *);
1573171ae1a4SAndy Whitcroft			if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
15748905a67cSAndy Whitcroft				my ($name_len) = length($1);
15758905a67cSAndy Whitcroft
1576cf655043SAndy Whitcroft				my $ctx = $s;
1577773647a0SAndy Whitcroft				substr($ctx, 0, $name_len + 1, '');
15788905a67cSAndy Whitcroft				$ctx =~ s/\)[^\)]*$//;
1579cf655043SAndy Whitcroft
15808905a67cSAndy Whitcroft				for my $arg (split(/\s*,\s*/, $ctx)) {
1581c45dcabdSAndy Whitcroft					if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
15828905a67cSAndy Whitcroft
1583c45dcabdSAndy Whitcroft						possible($1, "D:" . $s);
15848905a67cSAndy Whitcroft					}
15858905a67cSAndy Whitcroft				}
15868905a67cSAndy Whitcroft			}
15878905a67cSAndy Whitcroft
15889c0ca6f9SAndy Whitcroft		}
15899c0ca6f9SAndy Whitcroft
159000df344fSAndy Whitcroft#
159100df344fSAndy Whitcroft# Checks which may be anchored in the context.
159200df344fSAndy Whitcroft#
159300df344fSAndy Whitcroft
159400df344fSAndy Whitcroft# Check for switch () and associated case and default
159500df344fSAndy Whitcroft# statements should be at the same indent.
159600df344fSAndy Whitcroft		if ($line=~/\bswitch\s*\(.*\)/) {
159700df344fSAndy Whitcroft			my $err = '';
159800df344fSAndy Whitcroft			my $sep = '';
159900df344fSAndy Whitcroft			my @ctx = ctx_block_outer($linenr, $realcnt);
160000df344fSAndy Whitcroft			shift(@ctx);
160100df344fSAndy Whitcroft			for my $ctx (@ctx) {
160200df344fSAndy Whitcroft				my ($clen, $cindent) = line_stats($ctx);
160300df344fSAndy Whitcroft				if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
160400df344fSAndy Whitcroft							$indent != $cindent) {
160500df344fSAndy Whitcroft					$err .= "$sep$ctx\n";
160600df344fSAndy Whitcroft					$sep = '';
160700df344fSAndy Whitcroft				} else {
160800df344fSAndy Whitcroft					$sep = "[...]\n";
160900df344fSAndy Whitcroft				}
161000df344fSAndy Whitcroft			}
161100df344fSAndy Whitcroft			if ($err ne '') {
16129c0ca6f9SAndy Whitcroft				ERROR("switch and case should be at the same indent\n$hereline$err");
1613de7d4f0eSAndy Whitcroft			}
1614de7d4f0eSAndy Whitcroft		}
1615de7d4f0eSAndy Whitcroft
1616de7d4f0eSAndy Whitcroft# if/while/etc brace do not go on next line, unless defining a do while loop,
1617de7d4f0eSAndy Whitcroft# or if that brace on the next line is for something else
1618c45dcabdSAndy Whitcroft		if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
1619773647a0SAndy Whitcroft			my $pre_ctx = "$1$2";
1620773647a0SAndy Whitcroft
16219c0ca6f9SAndy Whitcroft			my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
1622de7d4f0eSAndy Whitcroft			my $ctx_cnt = $realcnt - $#ctx - 1;
1623de7d4f0eSAndy Whitcroft			my $ctx = join("\n", @ctx);
1624de7d4f0eSAndy Whitcroft
1625548596d5SAndy Whitcroft			my $ctx_ln = $linenr;
1626548596d5SAndy Whitcroft			my $ctx_skip = $realcnt;
1627de7d4f0eSAndy Whitcroft
1628548596d5SAndy Whitcroft			while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
1629548596d5SAndy Whitcroft					defined $lines[$ctx_ln - 1] &&
1630548596d5SAndy Whitcroft					$lines[$ctx_ln - 1] =~ /^-/)) {
1631548596d5SAndy Whitcroft				##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
1632548596d5SAndy Whitcroft				$ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
1633773647a0SAndy Whitcroft				$ctx_ln++;
1634773647a0SAndy Whitcroft			}
1635548596d5SAndy Whitcroft
163653210168SAndy Whitcroft			#print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
163753210168SAndy Whitcroft			#print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
1638773647a0SAndy Whitcroft
1639773647a0SAndy Whitcroft			if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln -1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
1640773647a0SAndy Whitcroft				ERROR("that open brace { should be on the previous line\n" .
1641c45dcabdSAndy Whitcroft					"$here\n$ctx\n$lines[$ctx_ln - 1]\n");
164200df344fSAndy Whitcroft			}
1643773647a0SAndy Whitcroft			if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
1644773647a0SAndy Whitcroft			    $ctx =~ /\)\s*\;\s*$/ &&
1645773647a0SAndy Whitcroft			    defined $lines[$ctx_ln - 1])
1646773647a0SAndy Whitcroft			{
16479c0ca6f9SAndy Whitcroft				my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
16489c0ca6f9SAndy Whitcroft				if ($nindent > $indent) {
1649773647a0SAndy Whitcroft					WARN("trailing semicolon indicates no statements, indent implies otherwise\n" .
1650c45dcabdSAndy Whitcroft						"$here\n$ctx\n$lines[$ctx_ln - 1]\n");
16519c0ca6f9SAndy Whitcroft				}
16529c0ca6f9SAndy Whitcroft			}
165300df344fSAndy Whitcroft		}
165400df344fSAndy Whitcroft
16554d001e4dSAndy Whitcroft# Check relative indent for conditionals and blocks.
16564d001e4dSAndy Whitcroft		if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
16574d001e4dSAndy Whitcroft			my ($s, $c) = ($stat, $cond);
16584d001e4dSAndy Whitcroft
16594d001e4dSAndy Whitcroft			substr($s, 0, length($c), '');
16604d001e4dSAndy Whitcroft
16614d001e4dSAndy Whitcroft			# Make sure we remove the line prefixes as we have
16624d001e4dSAndy Whitcroft			# none on the first line, and are going to readd them
16634d001e4dSAndy Whitcroft			# where necessary.
16644d001e4dSAndy Whitcroft			$s =~ s/\n./\n/gs;
16654d001e4dSAndy Whitcroft
16664d001e4dSAndy Whitcroft			# Find out how long the conditional actually is.
16676f779c18SAndy Whitcroft			my @newlines = ($c =~ /\n/gs);
16686f779c18SAndy Whitcroft			my $cond_lines = 1 + $#newlines;
16694d001e4dSAndy Whitcroft
16704d001e4dSAndy Whitcroft			# We want to check the first line inside the block
16714d001e4dSAndy Whitcroft			# starting at the end of the conditional, so remove:
16724d001e4dSAndy Whitcroft			#  1) any blank line termination
16734d001e4dSAndy Whitcroft			#  2) any opening brace { on end of the line
16744d001e4dSAndy Whitcroft			#  3) any do (...) {
16754d001e4dSAndy Whitcroft			my $continuation = 0;
16764d001e4dSAndy Whitcroft			my $check = 0;
16774d001e4dSAndy Whitcroft			$s =~ s/^.*\bdo\b//;
16784d001e4dSAndy Whitcroft			$s =~ s/^\s*{//;
16794d001e4dSAndy Whitcroft			if ($s =~ s/^\s*\\//) {
16804d001e4dSAndy Whitcroft				$continuation = 1;
16814d001e4dSAndy Whitcroft			}
16829bd49efeSAndy Whitcroft			if ($s =~ s/^\s*?\n//) {
16834d001e4dSAndy Whitcroft				$check = 1;
16844d001e4dSAndy Whitcroft				$cond_lines++;
16854d001e4dSAndy Whitcroft			}
16864d001e4dSAndy Whitcroft
16874d001e4dSAndy Whitcroft			# Also ignore a loop construct at the end of a
16884d001e4dSAndy Whitcroft			# preprocessor statement.
16894d001e4dSAndy Whitcroft			if (($prevline =~ /^.\s*#\s*define\s/ ||
16904d001e4dSAndy Whitcroft			    $prevline =~ /\\\s*$/) && $continuation == 0) {
16914d001e4dSAndy Whitcroft				$check = 0;
16924d001e4dSAndy Whitcroft			}
16934d001e4dSAndy Whitcroft
16949bd49efeSAndy Whitcroft			my $cond_ptr = -1;
1695740504c6SAndy Whitcroft			$continuation = 0;
16969bd49efeSAndy Whitcroft			while ($cond_ptr != $cond_lines) {
16979bd49efeSAndy Whitcroft				$cond_ptr = $cond_lines;
16984d001e4dSAndy Whitcroft
1699f16fa28fSAndy Whitcroft				# If we see an #else/#elif then the code
1700f16fa28fSAndy Whitcroft				# is not linear.
1701f16fa28fSAndy Whitcroft				if ($s =~ /^\s*\#\s*(?:else|elif)/) {
1702f16fa28fSAndy Whitcroft					$check = 0;
1703f16fa28fSAndy Whitcroft				}
1704f16fa28fSAndy Whitcroft
17059bd49efeSAndy Whitcroft				# Ignore:
17069bd49efeSAndy Whitcroft				#  1) blank lines, they should be at 0,
17079bd49efeSAndy Whitcroft				#  2) preprocessor lines, and
17089bd49efeSAndy Whitcroft				#  3) labels.
1709740504c6SAndy Whitcroft				if ($continuation ||
1710740504c6SAndy Whitcroft				    $s =~ /^\s*?\n/ ||
17119bd49efeSAndy Whitcroft				    $s =~ /^\s*#\s*?/ ||
17129bd49efeSAndy Whitcroft				    $s =~ /^\s*$Ident\s*:/) {
1713740504c6SAndy Whitcroft					$continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
171430dad6ebSAndy Whitcroft					if ($s =~ s/^.*?\n//) {
17159bd49efeSAndy Whitcroft						$cond_lines++;
17169bd49efeSAndy Whitcroft					}
17174d001e4dSAndy Whitcroft				}
171830dad6ebSAndy Whitcroft			}
17194d001e4dSAndy Whitcroft
17204d001e4dSAndy Whitcroft			my (undef, $sindent) = line_stats("+" . $s);
17214d001e4dSAndy Whitcroft			my $stat_real = raw_line($linenr, $cond_lines);
17224d001e4dSAndy Whitcroft
17234d001e4dSAndy Whitcroft			# Check if either of these lines are modified, else
17244d001e4dSAndy Whitcroft			# this is not this patch's fault.
17254d001e4dSAndy Whitcroft			if (!defined($stat_real) ||
17264d001e4dSAndy Whitcroft			    $stat !~ /^\+/ && $stat_real !~ /^\+/) {
17274d001e4dSAndy Whitcroft				$check = 0;
17284d001e4dSAndy Whitcroft			}
17294d001e4dSAndy Whitcroft			if (defined($stat_real) && $cond_lines > 1) {
17304d001e4dSAndy Whitcroft				$stat_real = "[...]\n$stat_real";
17314d001e4dSAndy Whitcroft			}
17324d001e4dSAndy Whitcroft
17339bd49efeSAndy 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";
17344d001e4dSAndy Whitcroft
17354d001e4dSAndy Whitcroft			if ($check && (($sindent % 8) != 0 ||
17364d001e4dSAndy Whitcroft			    ($sindent <= $indent && $s ne ''))) {
17374d001e4dSAndy Whitcroft				WARN("suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
17384d001e4dSAndy Whitcroft			}
17394d001e4dSAndy Whitcroft		}
17404d001e4dSAndy Whitcroft
17416c72ffaaSAndy Whitcroft		# Track the 'values' across context and added lines.
17426c72ffaaSAndy Whitcroft		my $opline = $line; $opline =~ s/^./ /;
17431f65f947SAndy Whitcroft		my ($curr_values, $curr_vars) =
17441f65f947SAndy Whitcroft				annotate_values($opline . "\n", $prev_values);
17456c72ffaaSAndy Whitcroft		$curr_values = $prev_values . $curr_values;
1746c2fdda0dSAndy Whitcroft		if ($dbg_values) {
1747c2fdda0dSAndy Whitcroft			my $outline = $opline; $outline =~ s/\t/ /g;
1748cf655043SAndy Whitcroft			print "$linenr > .$outline\n";
1749cf655043SAndy Whitcroft			print "$linenr > $curr_values\n";
17501f65f947SAndy Whitcroft			print "$linenr >  $curr_vars\n";
1751c2fdda0dSAndy Whitcroft		}
17526c72ffaaSAndy Whitcroft		$prev_values = substr($curr_values, -1);
17536c72ffaaSAndy Whitcroft
175400df344fSAndy Whitcroft#ignore lines not being added
175500df344fSAndy Whitcroft		if ($line=~/^[^\+]/) {next;}
175600df344fSAndy Whitcroft
1757653d4876SAndy Whitcroft# TEST: allow direct testing of the type matcher.
17587429c690SAndy Whitcroft		if ($dbg_type) {
17597429c690SAndy Whitcroft			if ($line =~ /^.\s*$Declare\s*$/) {
17607429c690SAndy Whitcroft				ERROR("TEST: is type\n" . $herecurr);
17617429c690SAndy Whitcroft			} elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
17627429c690SAndy Whitcroft				ERROR("TEST: is not type ($1 is)\n". $herecurr);
17637429c690SAndy Whitcroft			}
1764653d4876SAndy Whitcroft			next;
1765653d4876SAndy Whitcroft		}
1766a1ef277eSAndy Whitcroft# TEST: allow direct testing of the attribute matcher.
1767a1ef277eSAndy Whitcroft		if ($dbg_attr) {
17689360b0e5SAndy Whitcroft			if ($line =~ /^.\s*$Modifier\s*$/) {
1769a1ef277eSAndy Whitcroft				ERROR("TEST: is attr\n" . $herecurr);
17709360b0e5SAndy Whitcroft			} elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
1771a1ef277eSAndy Whitcroft				ERROR("TEST: is not attr ($1 is)\n". $herecurr);
1772a1ef277eSAndy Whitcroft			}
1773a1ef277eSAndy Whitcroft			next;
1774a1ef277eSAndy Whitcroft		}
1775653d4876SAndy Whitcroft
1776f0a594c1SAndy Whitcroft# check for initialisation to aggregates open brace on the next line
177799423c20SAndy Whitcroft		if ($line =~ /^.\s*{/ &&
177899423c20SAndy Whitcroft		    $prevline =~ /(?:^|[^=])=\s*$/) {
1779773647a0SAndy Whitcroft			ERROR("that open brace { should be on the previous line\n" . $hereprev);
1780f0a594c1SAndy Whitcroft		}
1781f0a594c1SAndy Whitcroft
178200df344fSAndy Whitcroft#
178300df344fSAndy Whitcroft# Checks which are anchored on the added line.
178400df344fSAndy Whitcroft#
178500df344fSAndy Whitcroft
1786653d4876SAndy Whitcroft# check for malformed paths in #include statements (uses RAW line)
1787c45dcabdSAndy Whitcroft		if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
1788653d4876SAndy Whitcroft			my $path = $1;
1789653d4876SAndy Whitcroft			if ($path =~ m{//}) {
1790de7d4f0eSAndy Whitcroft				ERROR("malformed #include filename\n" .
1791de7d4f0eSAndy Whitcroft					$herecurr);
1792653d4876SAndy Whitcroft			}
1793653d4876SAndy Whitcroft		}
1794653d4876SAndy Whitcroft
179500df344fSAndy Whitcroft# no C99 // comments
179600df344fSAndy Whitcroft		if ($line =~ m{//}) {
1797de7d4f0eSAndy Whitcroft			ERROR("do not use C99 // comments\n" . $herecurr);
179800df344fSAndy Whitcroft		}
179900df344fSAndy Whitcroft		# Remove C99 comments.
18000a920b5bSAndy Whitcroft		$line =~ s@//.*@@;
18016c72ffaaSAndy Whitcroft		$opline =~ s@//.*@@;
18020a920b5bSAndy Whitcroft
18032b474a1aSAndy Whitcroft# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
18042b474a1aSAndy Whitcroft# the whole statement.
18052b474a1aSAndy Whitcroft#print "APW <$lines[$realline_next - 1]>\n";
18062b474a1aSAndy Whitcroft		if (defined $realline_next &&
18072b474a1aSAndy Whitcroft		    exists $lines[$realline_next - 1] &&
18082b474a1aSAndy Whitcroft		    !defined $suppress_export{$realline_next} &&
18092b474a1aSAndy Whitcroft		    ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
18102b474a1aSAndy Whitcroft		     $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
1811653d4876SAndy Whitcroft			my $name = $1;
18122b474a1aSAndy Whitcroft			if ($stat !~ /(?:
18132b474a1aSAndy Whitcroft				\n.}\s*$|
181448012058SAndy Whitcroft				^.DEFINE_$Ident\(\Q$name\E\)|
181548012058SAndy Whitcroft				^.DECLARE_$Ident\(\Q$name\E\)|
181648012058SAndy Whitcroft				^.LIST_HEAD\(\Q$name\E\)|
18172b474a1aSAndy Whitcroft				^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
18182b474a1aSAndy Whitcroft				\b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
181948012058SAndy Whitcroft			    )/x) {
18202b474a1aSAndy Whitcroft#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
18212b474a1aSAndy Whitcroft				$suppress_export{$realline_next} = 2;
18222b474a1aSAndy Whitcroft			} else {
18232b474a1aSAndy Whitcroft				$suppress_export{$realline_next} = 1;
18240a920b5bSAndy Whitcroft			}
18250a920b5bSAndy Whitcroft		}
18262b474a1aSAndy Whitcroft		if (!defined $suppress_export{$linenr} &&
18272b474a1aSAndy Whitcroft		    $prevline =~ /^.\s*$/ &&
18282b474a1aSAndy Whitcroft		    ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
18292b474a1aSAndy Whitcroft		     $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
18302b474a1aSAndy Whitcroft#print "FOO B <$lines[$linenr - 1]>\n";
18312b474a1aSAndy Whitcroft			$suppress_export{$linenr} = 2;
18322b474a1aSAndy Whitcroft		}
18332b474a1aSAndy Whitcroft		if (defined $suppress_export{$linenr} &&
18342b474a1aSAndy Whitcroft		    $suppress_export{$linenr} == 2) {
18352b474a1aSAndy Whitcroft			WARN("EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
18362b474a1aSAndy Whitcroft		}
18370a920b5bSAndy Whitcroft
18385150bda4SJoe Eloff# check for global initialisers.
1839c45dcabdSAndy Whitcroft		if ($line =~ /^.$Type\s*$Ident\s*(?:\s+$Modifier)*\s*=\s*(0|NULL|false)\s*;/) {
18405150bda4SJoe Eloff			ERROR("do not initialise globals to 0 or NULL\n" .
1841f0a594c1SAndy Whitcroft				$herecurr);
1842f0a594c1SAndy Whitcroft		}
18430a920b5bSAndy Whitcroft# check for static initialisers.
18442d1bafd7SAndy Whitcroft		if ($line =~ /\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
1845de7d4f0eSAndy Whitcroft			ERROR("do not initialise statics to 0 or NULL\n" .
1846de7d4f0eSAndy Whitcroft				$herecurr);
18470a920b5bSAndy Whitcroft		}
18480a920b5bSAndy Whitcroft
1849653d4876SAndy Whitcroft# check for new typedefs, only function parameters and sparse annotations
1850653d4876SAndy Whitcroft# make sense.
1851653d4876SAndy Whitcroft		if ($line =~ /\btypedef\s/ &&
18528054576dSAndy Whitcroft		    $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
1853c45dcabdSAndy Whitcroft		    $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
18548ed22cadSAndy Whitcroft		    $line !~ /\b$typeTypedefs\b/ &&
1855653d4876SAndy Whitcroft		    $line !~ /\b__bitwise(?:__|)\b/) {
1856de7d4f0eSAndy Whitcroft			WARN("do not add new typedefs\n" . $herecurr);
18570a920b5bSAndy Whitcroft		}
18580a920b5bSAndy Whitcroft
18590a920b5bSAndy Whitcroft# * goes on variable not on type
186065863862SAndy Whitcroft		# (char*[ const])
186100ef4eceSAndy Whitcroft		if ($line =~ m{\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\)}) {
186265863862SAndy Whitcroft			my ($from, $to) = ($1, $1);
1863d8aaf121SAndy Whitcroft
186465863862SAndy Whitcroft			# Should start with a space.
186565863862SAndy Whitcroft			$to =~ s/^(\S)/ $1/;
186665863862SAndy Whitcroft			# Should not end with a space.
186765863862SAndy Whitcroft			$to =~ s/\s+$//;
186865863862SAndy Whitcroft			# '*'s should not have spaces between.
1869f9a0b3d1SAndy Whitcroft			while ($to =~ s/\*\s+\*/\*\*/) {
187065863862SAndy Whitcroft			}
1871d8aaf121SAndy Whitcroft
187265863862SAndy Whitcroft			#print "from<$from> to<$to>\n";
187365863862SAndy Whitcroft			if ($from ne $to) {
187465863862SAndy Whitcroft				ERROR("\"(foo$from)\" should be \"(foo$to)\"\n" .  $herecurr);
187565863862SAndy Whitcroft			}
187600ef4eceSAndy Whitcroft		} elsif ($line =~ m{\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident)}) {
187765863862SAndy Whitcroft			my ($from, $to, $ident) = ($1, $1, $2);
1878d8aaf121SAndy Whitcroft
187965863862SAndy Whitcroft			# Should start with a space.
188065863862SAndy Whitcroft			$to =~ s/^(\S)/ $1/;
188165863862SAndy Whitcroft			# Should not end with a space.
188265863862SAndy Whitcroft			$to =~ s/\s+$//;
188365863862SAndy Whitcroft			# '*'s should not have spaces between.
1884f9a0b3d1SAndy Whitcroft			while ($to =~ s/\*\s+\*/\*\*/) {
188565863862SAndy Whitcroft			}
188665863862SAndy Whitcroft			# Modifiers should have spaces.
188765863862SAndy Whitcroft			$to =~ s/(\b$Modifier$)/$1 /;
188865863862SAndy Whitcroft
1889667026e7SAndy Whitcroft			#print "from<$from> to<$to> ident<$ident>\n";
1890667026e7SAndy Whitcroft			if ($from ne $to && $ident !~ /^$Modifier$/) {
189165863862SAndy Whitcroft				ERROR("\"foo${from}bar\" should be \"foo${to}bar\"\n" .  $herecurr);
189265863862SAndy Whitcroft			}
18930a920b5bSAndy Whitcroft		}
18940a920b5bSAndy Whitcroft
18950a920b5bSAndy Whitcroft# # no BUG() or BUG_ON()
18960a920b5bSAndy Whitcroft# 		if ($line =~ /\b(BUG|BUG_ON)\b/) {
18970a920b5bSAndy Whitcroft# 			print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
18980a920b5bSAndy Whitcroft# 			print "$herecurr";
18990a920b5bSAndy Whitcroft# 			$clean = 0;
19000a920b5bSAndy Whitcroft# 		}
19010a920b5bSAndy Whitcroft
19028905a67cSAndy Whitcroft		if ($line =~ /\bLINUX_VERSION_CODE\b/) {
1903c2fdda0dSAndy Whitcroft			WARN("LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
19048905a67cSAndy Whitcroft		}
19058905a67cSAndy Whitcroft
190600df344fSAndy Whitcroft# printk should use KERN_* levels.  Note that follow on printk's on the
190700df344fSAndy Whitcroft# same line do not need a level, so we use the current block context
190800df344fSAndy Whitcroft# to try and find and validate the current printk.  In summary the current
190900df344fSAndy Whitcroft# printk includes all preceeding printk's which have no newline on the end.
191000df344fSAndy Whitcroft# we assume the first bad printk is the one to report.
1911f0a594c1SAndy Whitcroft		if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
191200df344fSAndy Whitcroft			my $ok = 0;
191300df344fSAndy Whitcroft			for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
191400df344fSAndy Whitcroft				#print "CHECK<$lines[$ln - 1]\n";
191500df344fSAndy Whitcroft				# we have a preceeding printk if it ends
191600df344fSAndy Whitcroft				# with "\n" ignore it, else it is to blame
191700df344fSAndy Whitcroft				if ($lines[$ln - 1] =~ m{\bprintk\(}) {
191800df344fSAndy Whitcroft					if ($rawlines[$ln - 1] !~ m{\\n"}) {
191900df344fSAndy Whitcroft						$ok = 1;
192000df344fSAndy Whitcroft					}
192100df344fSAndy Whitcroft					last;
192200df344fSAndy Whitcroft				}
192300df344fSAndy Whitcroft			}
192400df344fSAndy Whitcroft			if ($ok == 0) {
1925de7d4f0eSAndy Whitcroft				WARN("printk() should include KERN_ facility level\n" . $herecurr);
19260a920b5bSAndy Whitcroft			}
192700df344fSAndy Whitcroft		}
19280a920b5bSAndy Whitcroft
1929653d4876SAndy Whitcroft# function brace can't be on same line, except for #defines of do while,
1930653d4876SAndy Whitcroft# or if closed on same line
1931c45dcabdSAndy Whitcroft		if (($line=~/$Type\s*$Ident\(.*\).*\s{/) and
1932c45dcabdSAndy Whitcroft		    !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) {
1933de7d4f0eSAndy Whitcroft			ERROR("open brace '{' following function declarations go on the next line\n" . $herecurr);
19340a920b5bSAndy Whitcroft		}
1935653d4876SAndy Whitcroft
19368905a67cSAndy Whitcroft# open braces for enum, union and struct go on the same line.
19378905a67cSAndy Whitcroft		if ($line =~ /^.\s*{/ &&
19388905a67cSAndy Whitcroft		    $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
19398905a67cSAndy Whitcroft			ERROR("open brace '{' following $1 go on the same line\n" . $hereprev);
19408905a67cSAndy Whitcroft		}
19418905a67cSAndy Whitcroft
19420c73b4ebSAndy Whitcroft# missing space after union, struct or enum definition
19430c73b4ebSAndy Whitcroft		if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?(?:\s+$Ident)?[=\{]/) {
19440c73b4ebSAndy Whitcroft		    WARN("missing space after $1 definition\n" . $herecurr);
19450c73b4ebSAndy Whitcroft		}
19460c73b4ebSAndy Whitcroft
19478d31cfceSAndy Whitcroft# check for spacing round square brackets; allowed:
19488d31cfceSAndy Whitcroft#  1. with a type on the left -- int [] a;
1949fe2a7dbcSAndy Whitcroft#  2. at the beginning of a line for slice initialisers -- [0...10] = 5,
1950fe2a7dbcSAndy Whitcroft#  3. inside a curly brace -- = { [0...10] = 5 }
19518d31cfceSAndy Whitcroft		while ($line =~ /(.*?\s)\[/g) {
19528d31cfceSAndy Whitcroft			my ($where, $prefix) = ($-[1], $1);
19538d31cfceSAndy Whitcroft			if ($prefix !~ /$Type\s+$/ &&
1954fe2a7dbcSAndy Whitcroft			    ($where != 0 || $prefix !~ /^.\s+$/) &&
1955fe2a7dbcSAndy Whitcroft			    $prefix !~ /{\s+$/) {
19568d31cfceSAndy Whitcroft				ERROR("space prohibited before open square bracket '['\n" . $herecurr);
19578d31cfceSAndy Whitcroft			}
19588d31cfceSAndy Whitcroft		}
19598d31cfceSAndy Whitcroft
1960f0a594c1SAndy Whitcroft# check for spaces between functions and their parentheses.
19616c72ffaaSAndy Whitcroft		while ($line =~ /($Ident)\s+\(/g) {
1962c2fdda0dSAndy Whitcroft			my $name = $1;
1963773647a0SAndy Whitcroft			my $ctx_before = substr($line, 0, $-[1]);
1964773647a0SAndy Whitcroft			my $ctx = "$ctx_before$name";
1965c2fdda0dSAndy Whitcroft
1966c2fdda0dSAndy Whitcroft			# Ignore those directives where spaces _are_ permitted.
1967773647a0SAndy Whitcroft			if ($name =~ /^(?:
1968773647a0SAndy Whitcroft				if|for|while|switch|return|case|
1969773647a0SAndy Whitcroft				volatile|__volatile__|
1970773647a0SAndy Whitcroft				__attribute__|format|__extension__|
1971773647a0SAndy Whitcroft				asm|__asm__)$/x)
1972773647a0SAndy Whitcroft			{
1973c2fdda0dSAndy Whitcroft
1974c2fdda0dSAndy Whitcroft			# cpp #define statements have non-optional spaces, ie
1975c2fdda0dSAndy Whitcroft			# if there is a space between the name and the open
1976c2fdda0dSAndy Whitcroft			# parenthesis it is simply not a parameter group.
1977c45dcabdSAndy Whitcroft			} elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
1978773647a0SAndy Whitcroft
1979773647a0SAndy Whitcroft			# cpp #elif statement condition may start with a (
1980c45dcabdSAndy Whitcroft			} elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
1981c2fdda0dSAndy Whitcroft
1982c2fdda0dSAndy Whitcroft			# If this whole things ends with a type its most
1983c2fdda0dSAndy Whitcroft			# likely a typedef for a function.
1984773647a0SAndy Whitcroft			} elsif ($ctx =~ /$Type$/) {
1985c2fdda0dSAndy Whitcroft
1986c2fdda0dSAndy Whitcroft			} else {
1987773647a0SAndy Whitcroft				WARN("space prohibited between function name and open parenthesis '('\n" . $herecurr);
1988f0a594c1SAndy Whitcroft			}
19896c72ffaaSAndy Whitcroft		}
1990653d4876SAndy Whitcroft# Check operator spacing.
19910a920b5bSAndy Whitcroft		if (!($line=~/\#\s*include/)) {
19929c0ca6f9SAndy Whitcroft			my $ops = qr{
19939c0ca6f9SAndy Whitcroft				<<=|>>=|<=|>=|==|!=|
19949c0ca6f9SAndy Whitcroft				\+=|-=|\*=|\/=|%=|\^=|\|=|&=|
19959c0ca6f9SAndy Whitcroft				=>|->|<<|>>|<|>|=|!|~|
19961f65f947SAndy Whitcroft				&&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
19971f65f947SAndy Whitcroft				\?|:
19989c0ca6f9SAndy Whitcroft			}x;
1999cf655043SAndy Whitcroft			my @elements = split(/($ops|;)/, $opline);
200000df344fSAndy Whitcroft			my $off = 0;
20016c72ffaaSAndy Whitcroft
20026c72ffaaSAndy Whitcroft			my $blank = copy_spacing($opline);
20036c72ffaaSAndy Whitcroft
20040a920b5bSAndy Whitcroft			for (my $n = 0; $n < $#elements; $n += 2) {
20054a0df2efSAndy Whitcroft				$off += length($elements[$n]);
20064a0df2efSAndy Whitcroft
2007773647a0SAndy Whitcroft				# Pick up the preceeding and succeeding characters.
2008773647a0SAndy Whitcroft				my $ca = substr($opline, 0, $off);
2009773647a0SAndy Whitcroft				my $cc = '';
2010773647a0SAndy Whitcroft				if (length($opline) >= ($off + length($elements[$n + 1]))) {
2011773647a0SAndy Whitcroft					$cc = substr($opline, $off + length($elements[$n + 1]));
2012773647a0SAndy Whitcroft				}
2013773647a0SAndy Whitcroft				my $cb = "$ca$;$cc";
2014773647a0SAndy Whitcroft
20154a0df2efSAndy Whitcroft				my $a = '';
20164a0df2efSAndy Whitcroft				$a = 'V' if ($elements[$n] ne '');
20174a0df2efSAndy Whitcroft				$a = 'W' if ($elements[$n] =~ /\s$/);
2018cf655043SAndy Whitcroft				$a = 'C' if ($elements[$n] =~ /$;$/);
20194a0df2efSAndy Whitcroft				$a = 'B' if ($elements[$n] =~ /(\[|\()$/);
20204a0df2efSAndy Whitcroft				$a = 'O' if ($elements[$n] eq '');
2021773647a0SAndy Whitcroft				$a = 'E' if ($ca =~ /^\s*$/);
20224a0df2efSAndy Whitcroft
20230a920b5bSAndy Whitcroft				my $op = $elements[$n + 1];
20244a0df2efSAndy Whitcroft
20254a0df2efSAndy Whitcroft				my $c = '';
20260a920b5bSAndy Whitcroft				if (defined $elements[$n + 2]) {
20274a0df2efSAndy Whitcroft					$c = 'V' if ($elements[$n + 2] ne '');
20284a0df2efSAndy Whitcroft					$c = 'W' if ($elements[$n + 2] =~ /^\s/);
2029cf655043SAndy Whitcroft					$c = 'C' if ($elements[$n + 2] =~ /^$;/);
20304a0df2efSAndy Whitcroft					$c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
20314a0df2efSAndy Whitcroft					$c = 'O' if ($elements[$n + 2] eq '');
20328b1b3378SAndy Whitcroft					$c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
20334a0df2efSAndy Whitcroft				} else {
20344a0df2efSAndy Whitcroft					$c = 'E';
20350a920b5bSAndy Whitcroft				}
20360a920b5bSAndy Whitcroft
20374a0df2efSAndy Whitcroft				my $ctx = "${a}x${c}";
20384a0df2efSAndy Whitcroft
20394a0df2efSAndy Whitcroft				my $at = "(ctx:$ctx)";
20404a0df2efSAndy Whitcroft
20416c72ffaaSAndy Whitcroft				my $ptr = substr($blank, 0, $off) . "^";
2042de7d4f0eSAndy Whitcroft				my $hereptr = "$hereline$ptr\n";
20430a920b5bSAndy Whitcroft
204474048ed8SAndy Whitcroft				# Pull out the value of this operator.
20456c72ffaaSAndy Whitcroft				my $op_type = substr($curr_values, $off + 1, 1);
20460a920b5bSAndy Whitcroft
20471f65f947SAndy Whitcroft				# Get the full operator variant.
20481f65f947SAndy Whitcroft				my $opv = $op . substr($curr_vars, $off, 1);
20491f65f947SAndy Whitcroft
205013214adfSAndy Whitcroft				# Ignore operators passed as parameters.
205113214adfSAndy Whitcroft				if ($op_type ne 'V' &&
205213214adfSAndy Whitcroft				    $ca =~ /\s$/ && $cc =~ /^\s*,/) {
205313214adfSAndy Whitcroft
2054cf655043SAndy Whitcroft#				# Ignore comments
2055cf655043SAndy Whitcroft#				} elsif ($op =~ /^$;+$/) {
205613214adfSAndy Whitcroft
2057d8aaf121SAndy Whitcroft				# ; should have either the end of line or a space or \ after it
205813214adfSAndy Whitcroft				} elsif ($op eq ';') {
2059cf655043SAndy Whitcroft					if ($ctx !~ /.x[WEBC]/ &&
2060cf655043SAndy Whitcroft					    $cc !~ /^\\/ && $cc !~ /^;/) {
2061773647a0SAndy Whitcroft						ERROR("space required after that '$op' $at\n" . $hereptr);
2062d8aaf121SAndy Whitcroft					}
2063d8aaf121SAndy Whitcroft
2064d8aaf121SAndy Whitcroft				# // is a comment
2065d8aaf121SAndy Whitcroft				} elsif ($op eq '//') {
20660a920b5bSAndy Whitcroft
20671f65f947SAndy Whitcroft				# No spaces for:
20681f65f947SAndy Whitcroft				#   ->
20691f65f947SAndy Whitcroft				#   :   when part of a bitfield
20701f65f947SAndy Whitcroft				} elsif ($op eq '->' || $opv eq ':B') {
20714a0df2efSAndy Whitcroft					if ($ctx =~ /Wx.|.xW/) {
2072773647a0SAndy Whitcroft						ERROR("spaces prohibited around that '$op' $at\n" . $hereptr);
20730a920b5bSAndy Whitcroft					}
20740a920b5bSAndy Whitcroft
20750a920b5bSAndy Whitcroft				# , must have a space on the right.
20760a920b5bSAndy Whitcroft				} elsif ($op eq ',') {
2077cf655043SAndy Whitcroft					if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
2078773647a0SAndy Whitcroft						ERROR("space required after that '$op' $at\n" . $hereptr);
20790a920b5bSAndy Whitcroft					}
20800a920b5bSAndy Whitcroft
20819c0ca6f9SAndy Whitcroft				# '*' as part of a type definition -- reported already.
208274048ed8SAndy Whitcroft				} elsif ($opv eq '*_') {
20839c0ca6f9SAndy Whitcroft					#warn "'*' is part of type\n";
20849c0ca6f9SAndy Whitcroft
20859c0ca6f9SAndy Whitcroft				# unary operators should have a space before and
20869c0ca6f9SAndy Whitcroft				# none after.  May be left adjacent to another
20879c0ca6f9SAndy Whitcroft				# unary operator, or a cast
20889c0ca6f9SAndy Whitcroft				} elsif ($op eq '!' || $op eq '~' ||
208974048ed8SAndy Whitcroft					 $opv eq '*U' || $opv eq '-U' ||
20900d413866SAndy Whitcroft					 $opv eq '&U' || $opv eq '&&U') {
2091cf655043SAndy Whitcroft					if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
2092773647a0SAndy Whitcroft						ERROR("space required before that '$op' $at\n" . $hereptr);
20930a920b5bSAndy Whitcroft					}
2094a3340b35SAndy Whitcroft					if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
2095171ae1a4SAndy Whitcroft						# A unary '*' may be const
2096171ae1a4SAndy Whitcroft
2097171ae1a4SAndy Whitcroft					} elsif ($ctx =~ /.xW/) {
2098fb9e9096SAndy Whitcroft						ERROR("space prohibited after that '$op' $at\n" . $hereptr);
20990a920b5bSAndy Whitcroft					}
21000a920b5bSAndy Whitcroft
21010a920b5bSAndy Whitcroft				# unary ++ and unary -- are allowed no space on one side.
21020a920b5bSAndy Whitcroft				} elsif ($op eq '++' or $op eq '--') {
2103773647a0SAndy Whitcroft					if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
2104773647a0SAndy Whitcroft						ERROR("space required one side of that '$op' $at\n" . $hereptr);
21050a920b5bSAndy Whitcroft					}
2106773647a0SAndy Whitcroft					if ($ctx =~ /Wx[BE]/ ||
2107773647a0SAndy Whitcroft					    ($ctx =~ /Wx./ && $cc =~ /^;/)) {
2108773647a0SAndy Whitcroft						ERROR("space prohibited before that '$op' $at\n" . $hereptr);
2109653d4876SAndy Whitcroft					}
2110773647a0SAndy Whitcroft					if ($ctx =~ /ExW/) {
2111773647a0SAndy Whitcroft						ERROR("space prohibited after that '$op' $at\n" . $hereptr);
2112773647a0SAndy Whitcroft					}
2113773647a0SAndy Whitcroft
21140a920b5bSAndy Whitcroft
21150a920b5bSAndy Whitcroft				# << and >> may either have or not have spaces both sides
21169c0ca6f9SAndy Whitcroft				} elsif ($op eq '<<' or $op eq '>>' or
21179c0ca6f9SAndy Whitcroft					 $op eq '&' or $op eq '^' or $op eq '|' or
21189c0ca6f9SAndy Whitcroft					 $op eq '+' or $op eq '-' or
2119c2fdda0dSAndy Whitcroft					 $op eq '*' or $op eq '/' or
2120c2fdda0dSAndy Whitcroft					 $op eq '%')
21210a920b5bSAndy Whitcroft				{
2122773647a0SAndy Whitcroft					if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
2123de7d4f0eSAndy Whitcroft						ERROR("need consistent spacing around '$op' $at\n" .
2124de7d4f0eSAndy Whitcroft							$hereptr);
21250a920b5bSAndy Whitcroft					}
21260a920b5bSAndy Whitcroft
21271f65f947SAndy Whitcroft				# A colon needs no spaces before when it is
21281f65f947SAndy Whitcroft				# terminating a case value or a label.
21291f65f947SAndy Whitcroft				} elsif ($opv eq ':C' || $opv eq ':L') {
21301f65f947SAndy Whitcroft					if ($ctx =~ /Wx./) {
21311f65f947SAndy Whitcroft						ERROR("space prohibited before that '$op' $at\n" . $hereptr);
21321f65f947SAndy Whitcroft					}
21331f65f947SAndy Whitcroft
21340a920b5bSAndy Whitcroft				# All the others need spaces both sides.
2135cf655043SAndy Whitcroft				} elsif ($ctx !~ /[EWC]x[CWE]/) {
21361f65f947SAndy Whitcroft					my $ok = 0;
21371f65f947SAndy Whitcroft
213822f2a2efSAndy Whitcroft					# Ignore email addresses <foo@bar>
21391f65f947SAndy Whitcroft					if (($op eq '<' &&
21401f65f947SAndy Whitcroft					     $cc =~ /^\S+\@\S+>/) ||
21411f65f947SAndy Whitcroft					    ($op eq '>' &&
21421f65f947SAndy Whitcroft					     $ca =~ /<\S+\@\S+$/))
21431f65f947SAndy Whitcroft					{
21441f65f947SAndy Whitcroft					    	$ok = 1;
21451f65f947SAndy Whitcroft					}
21461f65f947SAndy Whitcroft
21471f65f947SAndy Whitcroft					# Ignore ?:
21481f65f947SAndy Whitcroft					if (($opv eq ':O' && $ca =~ /\?$/) ||
21491f65f947SAndy Whitcroft					    ($op eq '?' && $cc =~ /^:/)) {
21501f65f947SAndy Whitcroft					    	$ok = 1;
21511f65f947SAndy Whitcroft					}
21521f65f947SAndy Whitcroft
21531f65f947SAndy Whitcroft					if ($ok == 0) {
2154773647a0SAndy Whitcroft						ERROR("spaces required around that '$op' $at\n" . $hereptr);
21550a920b5bSAndy Whitcroft					}
215622f2a2efSAndy Whitcroft				}
21574a0df2efSAndy Whitcroft				$off += length($elements[$n + 1]);
21580a920b5bSAndy Whitcroft			}
21590a920b5bSAndy Whitcroft		}
21600a920b5bSAndy Whitcroft
2161f0a594c1SAndy Whitcroft# check for multiple assignments
2162f0a594c1SAndy Whitcroft		if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
21636c72ffaaSAndy Whitcroft			CHK("multiple assignments should be avoided\n" . $herecurr);
2164f0a594c1SAndy Whitcroft		}
2165f0a594c1SAndy Whitcroft
216622f2a2efSAndy Whitcroft## # check for multiple declarations, allowing for a function declaration
216722f2a2efSAndy Whitcroft## # continuation.
216822f2a2efSAndy Whitcroft## 		if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
216922f2a2efSAndy Whitcroft## 		    $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
217022f2a2efSAndy Whitcroft##
217122f2a2efSAndy Whitcroft## 			# Remove any bracketed sections to ensure we do not
217222f2a2efSAndy Whitcroft## 			# falsly report the parameters of functions.
217322f2a2efSAndy Whitcroft## 			my $ln = $line;
217422f2a2efSAndy Whitcroft## 			while ($ln =~ s/\([^\(\)]*\)//g) {
217522f2a2efSAndy Whitcroft## 			}
217622f2a2efSAndy Whitcroft## 			if ($ln =~ /,/) {
217722f2a2efSAndy Whitcroft## 				WARN("declaring multiple variables together should be avoided\n" . $herecurr);
217822f2a2efSAndy Whitcroft## 			}
217922f2a2efSAndy Whitcroft## 		}
2180f0a594c1SAndy Whitcroft
21810a920b5bSAndy Whitcroft#need space before brace following if, while, etc
218222f2a2efSAndy Whitcroft		if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
218322f2a2efSAndy Whitcroft		    $line =~ /do{/) {
2184773647a0SAndy Whitcroft			ERROR("space required before the open brace '{'\n" . $herecurr);
2185de7d4f0eSAndy Whitcroft		}
2186de7d4f0eSAndy Whitcroft
2187de7d4f0eSAndy Whitcroft# closing brace should have a space following it when it has anything
2188de7d4f0eSAndy Whitcroft# on the line
2189de7d4f0eSAndy Whitcroft		if ($line =~ /}(?!(?:,|;|\)))\S/) {
2190773647a0SAndy Whitcroft			ERROR("space required after that close brace '}'\n" . $herecurr);
21910a920b5bSAndy Whitcroft		}
21920a920b5bSAndy Whitcroft
219322f2a2efSAndy Whitcroft# check spacing on square brackets
219422f2a2efSAndy Whitcroft		if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
2195773647a0SAndy Whitcroft			ERROR("space prohibited after that open square bracket '['\n" . $herecurr);
219622f2a2efSAndy Whitcroft		}
219722f2a2efSAndy Whitcroft		if ($line =~ /\s\]/) {
2198773647a0SAndy Whitcroft			ERROR("space prohibited before that close square bracket ']'\n" . $herecurr);
219922f2a2efSAndy Whitcroft		}
220022f2a2efSAndy Whitcroft
2201c45dcabdSAndy Whitcroft# check spacing on parentheses
22029c0ca6f9SAndy Whitcroft		if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
22039c0ca6f9SAndy Whitcroft		    $line !~ /for\s*\(\s+;/) {
2204773647a0SAndy Whitcroft			ERROR("space prohibited after that open parenthesis '('\n" . $herecurr);
220522f2a2efSAndy Whitcroft		}
220613214adfSAndy Whitcroft		if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
2207c45dcabdSAndy Whitcroft		    $line !~ /for\s*\(.*;\s+\)/ &&
2208c45dcabdSAndy Whitcroft		    $line !~ /:\s+\)/) {
2209773647a0SAndy Whitcroft			ERROR("space prohibited before that close parenthesis ')'\n" . $herecurr);
221022f2a2efSAndy Whitcroft		}
221122f2a2efSAndy Whitcroft
22120a920b5bSAndy Whitcroft#goto labels aren't indented, allow a single space however
22134a0df2efSAndy Whitcroft		if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
22140a920b5bSAndy Whitcroft		   !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
2215de7d4f0eSAndy Whitcroft			WARN("labels should not be indented\n" . $herecurr);
22160a920b5bSAndy Whitcroft		}
22170a920b5bSAndy Whitcroft
2218c45dcabdSAndy Whitcroft# Return is not a function.
2219c45dcabdSAndy Whitcroft		if (defined($stat) && $stat =~ /^.\s*return(\s*)(\(.*);/s) {
2220c45dcabdSAndy Whitcroft			my $spacing = $1;
2221c45dcabdSAndy Whitcroft			my $value = $2;
2222c45dcabdSAndy Whitcroft
222386f9d059SAndy Whitcroft			# Flatten any parentheses
2224fb2d2c1bSAndy Whitcroft			$value =~ s/\(/ \(/g;
2225fb2d2c1bSAndy Whitcroft			$value =~ s/\)/\) /g;
222663f17f89SAndy Whitcroft			while ($value =~ s/\[[^\{\}]*\]/1/ ||
222763f17f89SAndy Whitcroft			       $value !~ /(?:$Ident|-?$Constant)\s*
222863f17f89SAndy Whitcroft					     $Compare\s*
222963f17f89SAndy Whitcroft					     (?:$Ident|-?$Constant)/x &&
223063f17f89SAndy Whitcroft			       $value =~ s/\([^\(\)]*\)/1/) {
2231c45dcabdSAndy Whitcroft			}
2232fb2d2c1bSAndy Whitcroft#print "value<$value>\n";
2233fb2d2c1bSAndy Whitcroft			if ($value =~ /^\s*(?:$Ident|-?$Constant)\s*$/) {
2234c45dcabdSAndy Whitcroft				ERROR("return is not a function, parentheses are not required\n" . $herecurr);
2235c45dcabdSAndy Whitcroft
2236c45dcabdSAndy Whitcroft			} elsif ($spacing !~ /\s+/) {
2237c45dcabdSAndy Whitcroft				ERROR("space required before the open parenthesis '('\n" . $herecurr);
2238c45dcabdSAndy Whitcroft			}
2239c45dcabdSAndy Whitcroft		}
224053a3c448SAndy Whitcroft# Return of what appears to be an errno should normally be -'ve
224153a3c448SAndy Whitcroft		if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) {
224253a3c448SAndy Whitcroft			my $name = $1;
224353a3c448SAndy Whitcroft			if ($name ne 'EOF' && $name ne 'ERROR') {
224453a3c448SAndy Whitcroft				WARN("return of an errno should typically be -ve (return -$1)\n" . $herecurr);
224553a3c448SAndy Whitcroft			}
224653a3c448SAndy Whitcroft		}
2247c45dcabdSAndy Whitcroft
22480a920b5bSAndy Whitcroft# Need a space before open parenthesis after if, while etc
22494a0df2efSAndy Whitcroft		if ($line=~/\b(if|while|for|switch)\(/) {
2250773647a0SAndy Whitcroft			ERROR("space required before the open parenthesis '('\n" . $herecurr);
22510a920b5bSAndy Whitcroft		}
22520a920b5bSAndy Whitcroft
2253f5fe35ddSAndy Whitcroft# Check for illegal assignment in if conditional -- and check for trailing
2254f5fe35ddSAndy Whitcroft# statements after the conditional.
2255170d3a22SAndy Whitcroft		if ($line =~ /do\s*(?!{)/) {
2256170d3a22SAndy Whitcroft			my ($stat_next) = ctx_statement_block($line_nr_next,
2257170d3a22SAndy Whitcroft						$remain_next, $off_next);
2258170d3a22SAndy Whitcroft			$stat_next =~ s/\n./\n /g;
2259170d3a22SAndy Whitcroft			##print "stat<$stat> stat_next<$stat_next>\n";
2260170d3a22SAndy Whitcroft
2261170d3a22SAndy Whitcroft			if ($stat_next =~ /^\s*while\b/) {
2262170d3a22SAndy Whitcroft				# If the statement carries leading newlines,
2263170d3a22SAndy Whitcroft				# then count those as offsets.
2264170d3a22SAndy Whitcroft				my ($whitespace) =
2265170d3a22SAndy Whitcroft					($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
2266170d3a22SAndy Whitcroft				my $offset =
2267170d3a22SAndy Whitcroft					statement_rawlines($whitespace) - 1;
2268170d3a22SAndy Whitcroft
2269170d3a22SAndy Whitcroft				$suppress_whiletrailers{$line_nr_next +
2270170d3a22SAndy Whitcroft								$offset} = 1;
2271170d3a22SAndy Whitcroft			}
2272170d3a22SAndy Whitcroft		}
2273170d3a22SAndy Whitcroft		if (!defined $suppress_whiletrailers{$linenr} &&
2274170d3a22SAndy Whitcroft		    $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
2275171ae1a4SAndy Whitcroft			my ($s, $c) = ($stat, $cond);
22768905a67cSAndy Whitcroft
2277b53c8e10SAndy Whitcroft			if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
2278c2fdda0dSAndy Whitcroft				ERROR("do not use assignment in if condition\n" . $herecurr);
22798905a67cSAndy Whitcroft			}
22808905a67cSAndy Whitcroft
22818905a67cSAndy Whitcroft			# Find out what is on the end of the line after the
22828905a67cSAndy Whitcroft			# conditional.
2283773647a0SAndy Whitcroft			substr($s, 0, length($c), '');
22848905a67cSAndy Whitcroft			$s =~ s/\n.*//g;
228513214adfSAndy Whitcroft			$s =~ s/$;//g; 	# Remove any comments
228653210168SAndy Whitcroft			if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
228753210168SAndy Whitcroft			    $c !~ /}\s*while\s*/)
2288773647a0SAndy Whitcroft			{
2289bb44ad39SAndy Whitcroft				# Find out how long the conditional actually is.
2290bb44ad39SAndy Whitcroft				my @newlines = ($c =~ /\n/gs);
2291bb44ad39SAndy Whitcroft				my $cond_lines = 1 + $#newlines;
229242bdf74cSHidetoshi Seto				my $stat_real = '';
2293bb44ad39SAndy Whitcroft
229442bdf74cSHidetoshi Seto				$stat_real = raw_line($linenr, $cond_lines)
229542bdf74cSHidetoshi Seto							. "\n" if ($cond_lines);
2296bb44ad39SAndy Whitcroft				if (defined($stat_real) && $cond_lines > 1) {
2297bb44ad39SAndy Whitcroft					$stat_real = "[...]\n$stat_real";
2298bb44ad39SAndy Whitcroft				}
2299bb44ad39SAndy Whitcroft
2300bb44ad39SAndy Whitcroft				ERROR("trailing statements should be on next line\n" . $herecurr . $stat_real);
23018905a67cSAndy Whitcroft			}
23028905a67cSAndy Whitcroft		}
23038905a67cSAndy Whitcroft
230413214adfSAndy Whitcroft# Check for bitwise tests written as boolean
230513214adfSAndy Whitcroft		if ($line =~ /
230613214adfSAndy Whitcroft			(?:
230713214adfSAndy Whitcroft				(?:\[|\(|\&\&|\|\|)
230813214adfSAndy Whitcroft				\s*0[xX][0-9]+\s*
230913214adfSAndy Whitcroft				(?:\&\&|\|\|)
231013214adfSAndy Whitcroft			|
231113214adfSAndy Whitcroft				(?:\&\&|\|\|)
231213214adfSAndy Whitcroft				\s*0[xX][0-9]+\s*
231313214adfSAndy Whitcroft				(?:\&\&|\|\||\)|\])
231413214adfSAndy Whitcroft			)/x)
231513214adfSAndy Whitcroft		{
231613214adfSAndy Whitcroft			WARN("boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
231713214adfSAndy Whitcroft		}
231813214adfSAndy Whitcroft
23198905a67cSAndy Whitcroft# if and else should not have general statements after it
232013214adfSAndy Whitcroft		if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
232113214adfSAndy Whitcroft			my $s = $1;
232213214adfSAndy Whitcroft			$s =~ s/$;//g; 	# Remove any comments
232313214adfSAndy Whitcroft			if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
23248905a67cSAndy Whitcroft				ERROR("trailing statements should be on next line\n" . $herecurr);
23250a920b5bSAndy Whitcroft			}
232613214adfSAndy Whitcroft		}
232739667782SAndy Whitcroft# if should not continue a brace
232839667782SAndy Whitcroft		if ($line =~ /}\s*if\b/) {
232939667782SAndy Whitcroft			ERROR("trailing statements should be on next line\n" .
233039667782SAndy Whitcroft				$herecurr);
233139667782SAndy Whitcroft		}
2332a1080bf8SAndy Whitcroft# case and default should not have general statements after them
2333a1080bf8SAndy Whitcroft		if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
2334a1080bf8SAndy Whitcroft		    $line !~ /\G(?:
23353fef12d6SAndy Whitcroft			(?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
2336a1080bf8SAndy Whitcroft			\s*return\s+
2337a1080bf8SAndy Whitcroft		    )/xg)
2338a1080bf8SAndy Whitcroft		{
2339a1080bf8SAndy Whitcroft			ERROR("trailing statements should be on next line\n" . $herecurr);
2340a1080bf8SAndy Whitcroft		}
23410a920b5bSAndy Whitcroft
23420a920b5bSAndy Whitcroft		# Check for }<nl>else {, these must be at the same
23430a920b5bSAndy Whitcroft		# indent level to be relevant to each other.
23440a920b5bSAndy Whitcroft		if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
23450a920b5bSAndy Whitcroft						$previndent == $indent) {
2346de7d4f0eSAndy Whitcroft			ERROR("else should follow close brace '}'\n" . $hereprev);
23470a920b5bSAndy Whitcroft		}
23480a920b5bSAndy Whitcroft
2349c2fdda0dSAndy Whitcroft		if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and
2350c2fdda0dSAndy Whitcroft						$previndent == $indent) {
2351c2fdda0dSAndy Whitcroft			my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
2352c2fdda0dSAndy Whitcroft
2353c2fdda0dSAndy Whitcroft			# Find out what is on the end of the line after the
2354c2fdda0dSAndy Whitcroft			# conditional.
2355773647a0SAndy Whitcroft			substr($s, 0, length($c), '');
2356c2fdda0dSAndy Whitcroft			$s =~ s/\n.*//g;
2357c2fdda0dSAndy Whitcroft
2358c2fdda0dSAndy Whitcroft			if ($s =~ /^\s*;/) {
2359c2fdda0dSAndy Whitcroft				ERROR("while should follow close brace '}'\n" . $hereprev);
2360c2fdda0dSAndy Whitcroft			}
2361c2fdda0dSAndy Whitcroft		}
2362c2fdda0dSAndy Whitcroft
23630a920b5bSAndy Whitcroft#studly caps, commented out until figure out how to distinguish between use of existing and adding new
23640a920b5bSAndy Whitcroft#		if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) {
23650a920b5bSAndy Whitcroft#		    print "No studly caps, use _\n";
23660a920b5bSAndy Whitcroft#		    print "$herecurr";
23670a920b5bSAndy Whitcroft#		    $clean = 0;
23680a920b5bSAndy Whitcroft#		}
23690a920b5bSAndy Whitcroft
23700a920b5bSAndy Whitcroft#no spaces allowed after \ in define
2371c45dcabdSAndy Whitcroft		if ($line=~/\#\s*define.*\\\s$/) {
2372de7d4f0eSAndy Whitcroft			WARN("Whitepspace after \\ makes next lines useless\n" . $herecurr);
23730a920b5bSAndy Whitcroft		}
23740a920b5bSAndy Whitcroft
2375653d4876SAndy Whitcroft#warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
2376c45dcabdSAndy Whitcroft		if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
2377e09dec48SAndy Whitcroft			my $file = "$1.h";
2378e09dec48SAndy Whitcroft			my $checkfile = "include/linux/$file";
2379e09dec48SAndy Whitcroft			if (-f "$root/$checkfile" &&
2380e09dec48SAndy Whitcroft			    $realfile ne $checkfile &&
23817840a94cSWolfram Sang			    $1 !~ /$allowed_asm_includes/)
2382c45dcabdSAndy Whitcroft			{
2383e09dec48SAndy Whitcroft				if ($realfile =~ m{^arch/}) {
2384e09dec48SAndy Whitcroft					CHK("Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
2385e09dec48SAndy Whitcroft				} else {
2386e09dec48SAndy Whitcroft					WARN("Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
2387e09dec48SAndy Whitcroft				}
23880a920b5bSAndy Whitcroft			}
23890a920b5bSAndy Whitcroft		}
23900a920b5bSAndy Whitcroft
2391653d4876SAndy Whitcroft# multi-statement macros should be enclosed in a do while loop, grab the
2392653d4876SAndy Whitcroft# first statement and ensure its the whole macro if its not enclosed
2393cf655043SAndy Whitcroft# in a known good container
2394b8f96a31SAndy Whitcroft		if ($realfile !~ m@/vmlinux.lds.h$@ &&
2395b8f96a31SAndy Whitcroft		    $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
2396d8aaf121SAndy Whitcroft			my $ln = $linenr;
2397d8aaf121SAndy Whitcroft			my $cnt = $realcnt;
2398c45dcabdSAndy Whitcroft			my ($off, $dstat, $dcond, $rest);
2399c45dcabdSAndy Whitcroft			my $ctx = '';
2400653d4876SAndy Whitcroft
2401c45dcabdSAndy Whitcroft			my $args = defined($1);
2402de7d4f0eSAndy Whitcroft
2403c45dcabdSAndy Whitcroft			# Find the end of the macro and limit our statement
2404c45dcabdSAndy Whitcroft			# search to that.
2405c45dcabdSAndy Whitcroft			while ($cnt > 0 && defined $lines[$ln - 1] &&
2406c45dcabdSAndy Whitcroft				$lines[$ln - 1] =~ /^(?:-|..*\\$)/)
2407c45dcabdSAndy Whitcroft			{
2408c45dcabdSAndy Whitcroft				$ctx .= $rawlines[$ln - 1] . "\n";
2409a3bb97a7SAndy Whitcroft				$cnt-- if ($lines[$ln - 1] !~ /^-/);
2410c45dcabdSAndy Whitcroft				$ln++;
2411de7d4f0eSAndy Whitcroft			}
2412c45dcabdSAndy Whitcroft			$ctx .= $rawlines[$ln - 1];
2413d8aaf121SAndy Whitcroft
2414c45dcabdSAndy Whitcroft			($dstat, $dcond, $ln, $cnt, $off) =
2415c45dcabdSAndy Whitcroft				ctx_statement_block($linenr, $ln - $linenr + 1, 0);
2416c45dcabdSAndy Whitcroft			#print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
2417a3bb97a7SAndy Whitcroft			#print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
2418c45dcabdSAndy Whitcroft
2419c45dcabdSAndy Whitcroft			# Extract the remainder of the define (if any) and
2420c45dcabdSAndy Whitcroft			# rip off surrounding spaces, and trailing \'s.
2421c45dcabdSAndy Whitcroft			$rest = '';
2422636d140aSAndy Whitcroft			while ($off != 0 || ($cnt > 0 && $rest =~ /\\\s*$/)) {
2423636d140aSAndy Whitcroft				#print "ADDING cnt<$cnt> $off <" . substr($lines[$ln - 1], $off) . "> rest<$rest>\n";
2424a3bb97a7SAndy Whitcroft				if ($off != 0 || $lines[$ln - 1] !~ /^-/) {
2425c45dcabdSAndy Whitcroft					$rest .= substr($lines[$ln - 1], $off) . "\n";
2426c45dcabdSAndy Whitcroft					$cnt--;
2427a3bb97a7SAndy Whitcroft				}
2428a3bb97a7SAndy Whitcroft				$ln++;
2429c45dcabdSAndy Whitcroft				$off = 0;
2430c45dcabdSAndy Whitcroft			}
2431c45dcabdSAndy Whitcroft			$rest =~ s/\\\n.//g;
2432c45dcabdSAndy Whitcroft			$rest =~ s/^\s*//s;
2433c45dcabdSAndy Whitcroft			$rest =~ s/\s*$//s;
2434c45dcabdSAndy Whitcroft
2435c45dcabdSAndy Whitcroft			# Clean up the original statement.
2436c45dcabdSAndy Whitcroft			if ($args) {
2437c45dcabdSAndy Whitcroft				substr($dstat, 0, length($dcond), '');
2438d8aaf121SAndy Whitcroft			} else {
2439c45dcabdSAndy Whitcroft				$dstat =~ s/^.\s*\#\s*define\s+$Ident\s*//;
2440c45dcabdSAndy Whitcroft			}
2441292f1a9bSAndy Whitcroft			$dstat =~ s/$;//g;
2442c45dcabdSAndy Whitcroft			$dstat =~ s/\\\n.//g;
2443c45dcabdSAndy Whitcroft			$dstat =~ s/^\s*//s;
2444c45dcabdSAndy Whitcroft			$dstat =~ s/\s*$//s;
2445c45dcabdSAndy Whitcroft
2446c45dcabdSAndy Whitcroft			# Flatten any parentheses and braces
2447bf30d6edSAndy Whitcroft			while ($dstat =~ s/\([^\(\)]*\)/1/ ||
2448bf30d6edSAndy Whitcroft			       $dstat =~ s/\{[^\{\}]*\}/1/ ||
2449bf30d6edSAndy Whitcroft			       $dstat =~ s/\[[^\{\}]*\]/1/)
2450bf30d6edSAndy Whitcroft			{
2451c45dcabdSAndy Whitcroft			}
2452c45dcabdSAndy Whitcroft
2453c45dcabdSAndy Whitcroft			my $exceptions = qr{
2454c45dcabdSAndy Whitcroft				$Declare|
2455c45dcabdSAndy Whitcroft				module_param_named|
2456c45dcabdSAndy Whitcroft				MODULE_PARAM_DESC|
2457c45dcabdSAndy Whitcroft				DECLARE_PER_CPU|
2458c45dcabdSAndy Whitcroft				DEFINE_PER_CPU|
2459383099fdSAndy Whitcroft				__typeof__\(|
246022fd2d3eSStefani Seibold				union|
246122fd2d3eSStefani Seibold				struct|
2462ea71a0a0SAndy Whitcroft				\.$Ident\s*=\s*|
2463ea71a0a0SAndy Whitcroft				^\"|\"$
2464c45dcabdSAndy Whitcroft			}x;
2465383099fdSAndy Whitcroft			#print "REST<$rest> dstat<$dstat>\n";
2466c45dcabdSAndy Whitcroft			if ($rest ne '') {
2467c45dcabdSAndy Whitcroft				if ($rest !~ /while\s*\(/ &&
2468c45dcabdSAndy Whitcroft				    $dstat !~ /$exceptions/)
2469c45dcabdSAndy Whitcroft				{
2470c45dcabdSAndy Whitcroft					ERROR("Macros with multiple statements should be enclosed in a do - while loop\n" . "$here\n$ctx\n");
2471c45dcabdSAndy Whitcroft				}
2472c45dcabdSAndy Whitcroft
2473c45dcabdSAndy Whitcroft			} elsif ($ctx !~ /;/) {
2474c45dcabdSAndy Whitcroft				if ($dstat ne '' &&
2475c45dcabdSAndy Whitcroft				    $dstat !~ /^(?:$Ident|-?$Constant)$/ &&
2476c45dcabdSAndy Whitcroft				    $dstat !~ /$exceptions/ &&
2477b132e5d5SAndy Whitcroft				    $dstat !~ /^\.$Ident\s*=/ &&
2478c45dcabdSAndy Whitcroft				    $dstat =~ /$Operators/)
2479c45dcabdSAndy Whitcroft				{
2480de7d4f0eSAndy Whitcroft					ERROR("Macros with complex values should be enclosed in parenthesis\n" . "$here\n$ctx\n");
2481d8aaf121SAndy Whitcroft				}
24820a920b5bSAndy Whitcroft			}
2483653d4876SAndy Whitcroft		}
24840a920b5bSAndy Whitcroft
2485080ba929SMike Frysinger# make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
2486080ba929SMike Frysinger# all assignments may have only one of the following with an assignment:
2487080ba929SMike Frysinger#	.
2488080ba929SMike Frysinger#	ALIGN(...)
2489080ba929SMike Frysinger#	VMLINUX_SYMBOL(...)
2490080ba929SMike Frysinger		if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
2491080ba929SMike Frysinger			WARN("vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
2492080ba929SMike Frysinger		}
2493080ba929SMike Frysinger
2494f0a594c1SAndy Whitcroft# check for redundant bracing round if etc
249513214adfSAndy Whitcroft		if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
249613214adfSAndy Whitcroft			my ($level, $endln, @chunks) =
2497cf655043SAndy Whitcroft				ctx_statement_full($linenr, $realcnt, 1);
249813214adfSAndy Whitcroft			#print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
2499cf655043SAndy Whitcroft			#print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
2500cf655043SAndy Whitcroft			if ($#chunks > 0 && $level == 0) {
250113214adfSAndy Whitcroft				my $allowed = 0;
250213214adfSAndy Whitcroft				my $seen = 0;
2503773647a0SAndy Whitcroft				my $herectx = $here . "\n";
2504cf655043SAndy Whitcroft				my $ln = $linenr - 1;
250513214adfSAndy Whitcroft				for my $chunk (@chunks) {
250613214adfSAndy Whitcroft					my ($cond, $block) = @{$chunk};
250713214adfSAndy Whitcroft
2508773647a0SAndy Whitcroft					# If the condition carries leading newlines, then count those as offsets.
2509773647a0SAndy Whitcroft					my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
2510773647a0SAndy Whitcroft					my $offset = statement_rawlines($whitespace) - 1;
2511773647a0SAndy Whitcroft
2512773647a0SAndy Whitcroft					#print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
2513773647a0SAndy Whitcroft
2514773647a0SAndy Whitcroft					# We have looked at and allowed this specific line.
2515773647a0SAndy Whitcroft					$suppress_ifbraces{$ln + $offset} = 1;
2516773647a0SAndy Whitcroft
2517773647a0SAndy Whitcroft					$herectx .= "$rawlines[$ln + $offset]\n[...]\n";
2518cf655043SAndy Whitcroft					$ln += statement_rawlines($block) - 1;
2519cf655043SAndy Whitcroft
2520773647a0SAndy Whitcroft					substr($block, 0, length($cond), '');
252113214adfSAndy Whitcroft
252213214adfSAndy Whitcroft					$seen++ if ($block =~ /^\s*{/);
252313214adfSAndy Whitcroft
2524cf655043SAndy Whitcroft					#print "cond<$cond> block<$block> allowed<$allowed>\n";
2525cf655043SAndy Whitcroft					if (statement_lines($cond) > 1) {
2526cf655043SAndy Whitcroft						#print "APW: ALLOWED: cond<$cond>\n";
252713214adfSAndy Whitcroft						$allowed = 1;
252813214adfSAndy Whitcroft					}
252913214adfSAndy Whitcroft					if ($block =~/\b(?:if|for|while)\b/) {
2530cf655043SAndy Whitcroft						#print "APW: ALLOWED: block<$block>\n";
253113214adfSAndy Whitcroft						$allowed = 1;
253213214adfSAndy Whitcroft					}
2533cf655043SAndy Whitcroft					if (statement_block_size($block) > 1) {
2534cf655043SAndy Whitcroft						#print "APW: ALLOWED: lines block<$block>\n";
253513214adfSAndy Whitcroft						$allowed = 1;
253613214adfSAndy Whitcroft					}
253713214adfSAndy Whitcroft				}
253813214adfSAndy Whitcroft				if ($seen && !$allowed) {
2539cf655043SAndy Whitcroft					WARN("braces {} are not necessary for any arm of this statement\n" . $herectx);
254013214adfSAndy Whitcroft				}
254113214adfSAndy Whitcroft			}
254213214adfSAndy Whitcroft		}
2543773647a0SAndy Whitcroft		if (!defined $suppress_ifbraces{$linenr - 1} &&
254413214adfSAndy Whitcroft					$line =~ /\b(if|while|for|else)\b/) {
2545cf655043SAndy Whitcroft			my $allowed = 0;
2546f0a594c1SAndy Whitcroft
2547cf655043SAndy Whitcroft			# Check the pre-context.
2548cf655043SAndy Whitcroft			if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
2549cf655043SAndy Whitcroft				#print "APW: ALLOWED: pre<$1>\n";
2550cf655043SAndy Whitcroft				$allowed = 1;
2551f0a594c1SAndy Whitcroft			}
2552773647a0SAndy Whitcroft
2553773647a0SAndy Whitcroft			my ($level, $endln, @chunks) =
2554773647a0SAndy Whitcroft				ctx_statement_full($linenr, $realcnt, $-[0]);
2555773647a0SAndy Whitcroft
2556cf655043SAndy Whitcroft			# Check the condition.
2557cf655043SAndy Whitcroft			my ($cond, $block) = @{$chunks[0]};
2558773647a0SAndy Whitcroft			#print "CHECKING<$linenr> cond<$cond> block<$block>\n";
2559cf655043SAndy Whitcroft			if (defined $cond) {
2560773647a0SAndy Whitcroft				substr($block, 0, length($cond), '');
2561cf655043SAndy Whitcroft			}
2562cf655043SAndy Whitcroft			if (statement_lines($cond) > 1) {
2563cf655043SAndy Whitcroft				#print "APW: ALLOWED: cond<$cond>\n";
2564cf655043SAndy Whitcroft				$allowed = 1;
2565cf655043SAndy Whitcroft			}
2566cf655043SAndy Whitcroft			if ($block =~/\b(?:if|for|while)\b/) {
2567cf655043SAndy Whitcroft				#print "APW: ALLOWED: block<$block>\n";
2568cf655043SAndy Whitcroft				$allowed = 1;
2569cf655043SAndy Whitcroft			}
2570cf655043SAndy Whitcroft			if (statement_block_size($block) > 1) {
2571cf655043SAndy Whitcroft				#print "APW: ALLOWED: lines block<$block>\n";
2572cf655043SAndy Whitcroft				$allowed = 1;
2573cf655043SAndy Whitcroft			}
2574cf655043SAndy Whitcroft			# Check the post-context.
2575cf655043SAndy Whitcroft			if (defined $chunks[1]) {
2576cf655043SAndy Whitcroft				my ($cond, $block) = @{$chunks[1]};
2577cf655043SAndy Whitcroft				if (defined $cond) {
2578773647a0SAndy Whitcroft					substr($block, 0, length($cond), '');
2579cf655043SAndy Whitcroft				}
2580cf655043SAndy Whitcroft				if ($block =~ /^\s*\{/) {
2581cf655043SAndy Whitcroft					#print "APW: ALLOWED: chunk-1 block<$block>\n";
2582cf655043SAndy Whitcroft					$allowed = 1;
2583cf655043SAndy Whitcroft				}
2584cf655043SAndy Whitcroft			}
2585cf655043SAndy Whitcroft			if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
2586cf655043SAndy Whitcroft				my $herectx = $here . "\n";;
2587f055663cSAndy Whitcroft				my $cnt = statement_rawlines($block);
2588cf655043SAndy Whitcroft
2589f055663cSAndy Whitcroft				for (my $n = 0; $n < $cnt; $n++) {
2590f055663cSAndy Whitcroft					$herectx .= raw_line($linenr, $n) . "\n";;
2591cf655043SAndy Whitcroft				}
2592cf655043SAndy Whitcroft
2593cf655043SAndy Whitcroft				WARN("braces {} are not necessary for single statement blocks\n" . $herectx);
2594f0a594c1SAndy Whitcroft			}
2595f0a594c1SAndy Whitcroft		}
2596f0a594c1SAndy Whitcroft
2597653d4876SAndy Whitcroft# don't include deprecated include files (uses RAW line)
25984a0df2efSAndy Whitcroft		for my $inc (@dep_includes) {
2599c45dcabdSAndy Whitcroft			if ($rawline =~ m@^.\s*\#\s*include\s*\<$inc>@) {
2600de7d4f0eSAndy Whitcroft				ERROR("Don't use <$inc>: see Documentation/feature-removal-schedule.txt\n" . $herecurr);
26010a920b5bSAndy Whitcroft			}
26020a920b5bSAndy Whitcroft		}
26030a920b5bSAndy Whitcroft
26044a0df2efSAndy Whitcroft# don't use deprecated functions
26054a0df2efSAndy Whitcroft		for my $func (@dep_functions) {
260600df344fSAndy Whitcroft			if ($line =~ /\b$func\b/) {
2607de7d4f0eSAndy Whitcroft				ERROR("Don't use $func(): see Documentation/feature-removal-schedule.txt\n" . $herecurr);
26084a0df2efSAndy Whitcroft			}
26094a0df2efSAndy Whitcroft		}
26104a0df2efSAndy Whitcroft
26114a0df2efSAndy Whitcroft# no volatiles please
26126c72ffaaSAndy Whitcroft		my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
26136c72ffaaSAndy Whitcroft		if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
2614de7d4f0eSAndy Whitcroft			WARN("Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
26154a0df2efSAndy Whitcroft		}
26164a0df2efSAndy Whitcroft
26179c0ca6f9SAndy Whitcroft# SPIN_LOCK_UNLOCKED & RW_LOCK_UNLOCKED are deprecated
26189c0ca6f9SAndy Whitcroft		if ($line =~ /\b(SPIN_LOCK_UNLOCKED|RW_LOCK_UNLOCKED)/) {
26199c0ca6f9SAndy Whitcroft			ERROR("Use of $1 is deprecated: see Documentation/spinlocks.txt\n" . $herecurr);
26209c0ca6f9SAndy Whitcroft		}
26219c0ca6f9SAndy Whitcroft
262200df344fSAndy Whitcroft# warn about #if 0
2623c45dcabdSAndy Whitcroft		if ($line =~ /^.\s*\#\s*if\s+0\b/) {
2624de7d4f0eSAndy Whitcroft			CHK("if this code is redundant consider removing it\n" .
2625de7d4f0eSAndy Whitcroft				$herecurr);
26264a0df2efSAndy Whitcroft		}
26274a0df2efSAndy Whitcroft
2628f0a594c1SAndy Whitcroft# check for needless kfree() checks
2629f0a594c1SAndy Whitcroft		if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
2630f0a594c1SAndy Whitcroft			my $expr = $1;
2631f0a594c1SAndy Whitcroft			if ($line =~ /\bkfree\(\Q$expr\E\);/) {
26323c232147SWolfram Sang				WARN("kfree(NULL) is safe this check is probably not required\n" . $hereprev);
2633f0a594c1SAndy Whitcroft			}
2634f0a594c1SAndy Whitcroft		}
26354c432a8fSGreg Kroah-Hartman# check for needless usb_free_urb() checks
26364c432a8fSGreg Kroah-Hartman		if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
26374c432a8fSGreg Kroah-Hartman			my $expr = $1;
26384c432a8fSGreg Kroah-Hartman			if ($line =~ /\busb_free_urb\(\Q$expr\E\);/) {
26394c432a8fSGreg Kroah-Hartman				WARN("usb_free_urb(NULL) is safe this check is probably not required\n" . $hereprev);
26404c432a8fSGreg Kroah-Hartman			}
26414c432a8fSGreg Kroah-Hartman		}
2642f0a594c1SAndy Whitcroft
26431a15a250SPatrick Pannuto# prefer usleep_range over udelay
26441a15a250SPatrick Pannuto		if ($line =~ /\budelay\s*\(\s*(\w+)\s*\)/) {
26451a15a250SPatrick Pannuto			# ignore udelay's < 10, however
26461a15a250SPatrick Pannuto			if (! (($1 =~ /(\d+)/) && ($1 < 10)) ) {
26471a15a250SPatrick Pannuto				CHK("usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $line);
26481a15a250SPatrick Pannuto			}
26491a15a250SPatrick Pannuto		}
26501a15a250SPatrick Pannuto
265109ef8725SPatrick Pannuto# warn about unexpectedly long msleep's
265209ef8725SPatrick Pannuto		if ($line =~ /\bmsleep\s*\((\d+)\);/) {
265309ef8725SPatrick Pannuto			if ($1 < 20) {
265409ef8725SPatrick Pannuto				WARN("msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $line);
265509ef8725SPatrick Pannuto			}
265609ef8725SPatrick Pannuto		}
265709ef8725SPatrick Pannuto
265800df344fSAndy Whitcroft# warn about #ifdefs in C files
2659c45dcabdSAndy Whitcroft#		if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
266000df344fSAndy Whitcroft#			print "#ifdef in C files should be avoided\n";
266100df344fSAndy Whitcroft#			print "$herecurr";
266200df344fSAndy Whitcroft#			$clean = 0;
266300df344fSAndy Whitcroft#		}
266400df344fSAndy Whitcroft
266522f2a2efSAndy Whitcroft# warn about spacing in #ifdefs
2666c45dcabdSAndy Whitcroft		if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
266722f2a2efSAndy Whitcroft			ERROR("exactly one space required after that #$1\n" . $herecurr);
266822f2a2efSAndy Whitcroft		}
266922f2a2efSAndy Whitcroft
26704a0df2efSAndy Whitcroft# check for spinlock_t definitions without a comment.
2671171ae1a4SAndy Whitcroft		if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
2672171ae1a4SAndy Whitcroft		    $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
26734a0df2efSAndy Whitcroft			my $which = $1;
26744a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
2675de7d4f0eSAndy Whitcroft				CHK("$1 definition without comment\n" . $herecurr);
26764a0df2efSAndy Whitcroft			}
26774a0df2efSAndy Whitcroft		}
26784a0df2efSAndy Whitcroft# check for memory barriers without a comment.
26794a0df2efSAndy Whitcroft		if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
26804a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
2681de7d4f0eSAndy Whitcroft				CHK("memory barrier without comment\n" . $herecurr);
26824a0df2efSAndy Whitcroft			}
26834a0df2efSAndy Whitcroft		}
26844a0df2efSAndy Whitcroft# check of hardware specific defines
2685c45dcabdSAndy Whitcroft		if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
2686de7d4f0eSAndy Whitcroft			CHK("architecture specific defines should be avoided\n" .  $herecurr);
26870a920b5bSAndy Whitcroft		}
2688653d4876SAndy Whitcroft
2689d4977c78STobias Klauser# Check that the storage class is at the beginning of a declaration
2690d4977c78STobias Klauser		if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
2691d4977c78STobias Klauser			WARN("storage class should be at the beginning of the declaration\n" . $herecurr)
2692d4977c78STobias Klauser		}
2693d4977c78STobias Klauser
2694de7d4f0eSAndy Whitcroft# check the location of the inline attribute, that it is between
2695de7d4f0eSAndy Whitcroft# storage class and type.
26969c0ca6f9SAndy Whitcroft		if ($line =~ /\b$Type\s+$Inline\b/ ||
26979c0ca6f9SAndy Whitcroft		    $line =~ /\b$Inline\s+$Storage\b/) {
2698de7d4f0eSAndy Whitcroft			ERROR("inline keyword should sit between storage class and type\n" . $herecurr);
2699de7d4f0eSAndy Whitcroft		}
2700de7d4f0eSAndy Whitcroft
27018905a67cSAndy Whitcroft# Check for __inline__ and __inline, prefer inline
27028905a67cSAndy Whitcroft		if ($line =~ /\b(__inline__|__inline)\b/) {
27038905a67cSAndy Whitcroft			WARN("plain inline is preferred over $1\n" . $herecurr);
27048905a67cSAndy Whitcroft		}
27058905a67cSAndy Whitcroft
27068f53a9b8SJoe Perches# check for sizeof(&)
27078f53a9b8SJoe Perches		if ($line =~ /\bsizeof\s*\(\s*\&/) {
27088f53a9b8SJoe Perches			WARN("sizeof(& should be avoided\n" . $herecurr);
27098f53a9b8SJoe Perches		}
27108f53a9b8SJoe Perches
2711de7d4f0eSAndy Whitcroft# check for new externs in .c files.
2712171ae1a4SAndy Whitcroft		if ($realfile =~ /\.c$/ && defined $stat &&
2713c45dcabdSAndy Whitcroft		    $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
2714171ae1a4SAndy Whitcroft		{
2715c45dcabdSAndy Whitcroft			my $function_name = $1;
2716c45dcabdSAndy Whitcroft			my $paren_space = $2;
2717171ae1a4SAndy Whitcroft
2718171ae1a4SAndy Whitcroft			my $s = $stat;
2719171ae1a4SAndy Whitcroft			if (defined $cond) {
2720171ae1a4SAndy Whitcroft				substr($s, 0, length($cond), '');
2721171ae1a4SAndy Whitcroft			}
2722c45dcabdSAndy Whitcroft			if ($s =~ /^\s*;/ &&
2723c45dcabdSAndy Whitcroft			    $function_name ne 'uninitialized_var')
2724c45dcabdSAndy Whitcroft			{
2725de7d4f0eSAndy Whitcroft				WARN("externs should be avoided in .c files\n" .  $herecurr);
2726de7d4f0eSAndy Whitcroft			}
2727de7d4f0eSAndy Whitcroft
2728171ae1a4SAndy Whitcroft			if ($paren_space =~ /\n/) {
2729171ae1a4SAndy Whitcroft				WARN("arguments for function declarations should follow identifier\n" . $herecurr);
2730171ae1a4SAndy Whitcroft			}
27319c9ba34eSAndy Whitcroft
27329c9ba34eSAndy Whitcroft		} elsif ($realfile =~ /\.c$/ && defined $stat &&
27339c9ba34eSAndy Whitcroft		    $stat =~ /^.\s*extern\s+/)
27349c9ba34eSAndy Whitcroft		{
27359c9ba34eSAndy Whitcroft			WARN("externs should be avoided in .c files\n" .  $herecurr);
2736171ae1a4SAndy Whitcroft		}
2737171ae1a4SAndy Whitcroft
2738de7d4f0eSAndy Whitcroft# checks for new __setup's
2739de7d4f0eSAndy Whitcroft		if ($rawline =~ /\b__setup\("([^"]*)"/) {
2740de7d4f0eSAndy Whitcroft			my $name = $1;
2741de7d4f0eSAndy Whitcroft
2742de7d4f0eSAndy Whitcroft			if (!grep(/$name/, @setup_docs)) {
2743de7d4f0eSAndy Whitcroft				CHK("__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
2744de7d4f0eSAndy Whitcroft			}
2745653d4876SAndy Whitcroft		}
27469c0ca6f9SAndy Whitcroft
27479c0ca6f9SAndy Whitcroft# check for pointless casting of kmalloc return
27489c0ca6f9SAndy Whitcroft		if ($line =~ /\*\s*\)\s*k[czm]alloc\b/) {
27499c0ca6f9SAndy Whitcroft			WARN("unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
27509c0ca6f9SAndy Whitcroft		}
275113214adfSAndy Whitcroft
275213214adfSAndy Whitcroft# check for gcc specific __FUNCTION__
275313214adfSAndy Whitcroft		if ($line =~ /__FUNCTION__/) {
275413214adfSAndy Whitcroft			WARN("__func__ should be used instead of gcc specific __FUNCTION__\n"  . $herecurr);
275513214adfSAndy Whitcroft		}
2756773647a0SAndy Whitcroft
2757773647a0SAndy Whitcroft# check for semaphores used as mutexes
2758171ae1a4SAndy Whitcroft		if ($line =~ /^.\s*(DECLARE_MUTEX|init_MUTEX)\s*\(/) {
2759773647a0SAndy Whitcroft			WARN("mutexes are preferred for single holder semaphores\n" . $herecurr);
2760773647a0SAndy Whitcroft		}
2761773647a0SAndy Whitcroft# check for semaphores used as mutexes
2762171ae1a4SAndy Whitcroft		if ($line =~ /^.\s*init_MUTEX_LOCKED\s*\(/) {
2763773647a0SAndy Whitcroft			WARN("consider using a completion\n" . $herecurr);
27641704f47bSPeter Zijlstra
2765773647a0SAndy Whitcroft		}
2766773647a0SAndy Whitcroft# recommend strict_strto* over simple_strto*
2767773647a0SAndy Whitcroft		if ($line =~ /\bsimple_(strto.*?)\s*\(/) {
2768773647a0SAndy Whitcroft			WARN("consider using strict_$1 in preference to simple_$1\n" . $herecurr);
2769773647a0SAndy Whitcroft		}
2770f3db6639SMichael Ellerman# check for __initcall(), use device_initcall() explicitly please
2771f3db6639SMichael Ellerman		if ($line =~ /^.\s*__initcall\s*\(/) {
2772f3db6639SMichael Ellerman			WARN("please use device_initcall() instead of __initcall()\n" . $herecurr);
2773f3db6639SMichael Ellerman		}
277479404849SEmese Revfy# check for various ops structs, ensure they are const.
277579404849SEmese Revfy		my $struct_ops = qr{acpi_dock_ops|
277679404849SEmese Revfy				address_space_operations|
277779404849SEmese Revfy				backlight_ops|
277879404849SEmese Revfy				block_device_operations|
277979404849SEmese Revfy				dentry_operations|
278079404849SEmese Revfy				dev_pm_ops|
278179404849SEmese Revfy				dma_map_ops|
278279404849SEmese Revfy				extent_io_ops|
278379404849SEmese Revfy				file_lock_operations|
278479404849SEmese Revfy				file_operations|
278579404849SEmese Revfy				hv_ops|
278679404849SEmese Revfy				ide_dma_ops|
278779404849SEmese Revfy				intel_dvo_dev_ops|
278879404849SEmese Revfy				item_operations|
278979404849SEmese Revfy				iwl_ops|
279079404849SEmese Revfy				kgdb_arch|
279179404849SEmese Revfy				kgdb_io|
279279404849SEmese Revfy				kset_uevent_ops|
279379404849SEmese Revfy				lock_manager_operations|
279479404849SEmese Revfy				microcode_ops|
279579404849SEmese Revfy				mtrr_ops|
279679404849SEmese Revfy				neigh_ops|
279779404849SEmese Revfy				nlmsvc_binding|
279879404849SEmese Revfy				pci_raw_ops|
279979404849SEmese Revfy				pipe_buf_operations|
280079404849SEmese Revfy				platform_hibernation_ops|
280179404849SEmese Revfy				platform_suspend_ops|
280279404849SEmese Revfy				proto_ops|
280379404849SEmese Revfy				rpc_pipe_ops|
280479404849SEmese Revfy				seq_operations|
280579404849SEmese Revfy				snd_ac97_build_ops|
280679404849SEmese Revfy				soc_pcmcia_socket_ops|
280779404849SEmese Revfy				stacktrace_ops|
280879404849SEmese Revfy				sysfs_ops|
280979404849SEmese Revfy				tty_operations|
281079404849SEmese Revfy				usb_mon_operations|
281179404849SEmese Revfy				wd_ops}x;
28126903ffb2SAndy Whitcroft		if ($line !~ /\bconst\b/ &&
281379404849SEmese Revfy		    $line =~ /\bstruct\s+($struct_ops)\b/) {
28146903ffb2SAndy Whitcroft			WARN("struct $1 should normally be const\n" .
28156903ffb2SAndy Whitcroft				$herecurr);
28162b6db5cbSAndy Whitcroft		}
2817773647a0SAndy Whitcroft
2818773647a0SAndy Whitcroft# use of NR_CPUS is usually wrong
2819773647a0SAndy Whitcroft# ignore definitions of NR_CPUS and usage to define arrays as likely right
2820773647a0SAndy Whitcroft		if ($line =~ /\bNR_CPUS\b/ &&
2821c45dcabdSAndy Whitcroft		    $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
2822c45dcabdSAndy Whitcroft		    $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
2823171ae1a4SAndy Whitcroft		    $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
2824171ae1a4SAndy Whitcroft		    $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
2825171ae1a4SAndy Whitcroft		    $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
2826773647a0SAndy Whitcroft		{
2827773647a0SAndy Whitcroft			WARN("usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
2828773647a0SAndy Whitcroft		}
28299c9ba34eSAndy Whitcroft
28309c9ba34eSAndy Whitcroft# check for %L{u,d,i} in strings
28319c9ba34eSAndy Whitcroft		my $string;
28329c9ba34eSAndy Whitcroft		while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
28339c9ba34eSAndy Whitcroft			$string = substr($rawline, $-[1], $+[1] - $-[1]);
28342a1bc5d5SAndy Whitcroft			$string =~ s/%%/__/g;
28359c9ba34eSAndy Whitcroft			if ($string =~ /(?<!%)%L[udi]/) {
28369c9ba34eSAndy Whitcroft				WARN("\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
28379c9ba34eSAndy Whitcroft				last;
28389c9ba34eSAndy Whitcroft			}
28399c9ba34eSAndy Whitcroft		}
2840691d77b6SAndy Whitcroft
2841691d77b6SAndy Whitcroft# whine mightly about in_atomic
2842691d77b6SAndy Whitcroft		if ($line =~ /\bin_atomic\s*\(/) {
2843691d77b6SAndy Whitcroft			if ($realfile =~ m@^drivers/@) {
2844691d77b6SAndy Whitcroft				ERROR("do not use in_atomic in drivers\n" . $herecurr);
2845f4a87736SAndy Whitcroft			} elsif ($realfile !~ m@^kernel/@) {
2846691d77b6SAndy Whitcroft				WARN("use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
2847691d77b6SAndy Whitcroft			}
2848691d77b6SAndy Whitcroft		}
28491704f47bSPeter Zijlstra
28501704f47bSPeter Zijlstra# check for lockdep_set_novalidate_class
28511704f47bSPeter Zijlstra		if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
28521704f47bSPeter Zijlstra		    $line =~ /__lockdep_no_validate__\s*\)/ ) {
28531704f47bSPeter Zijlstra			if ($realfile !~ m@^kernel/lockdep@ &&
28541704f47bSPeter Zijlstra			    $realfile !~ m@^include/linux/lockdep@ &&
28551704f47bSPeter Zijlstra			    $realfile !~ m@^drivers/base/core@) {
28561704f47bSPeter Zijlstra				ERROR("lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
28571704f47bSPeter Zijlstra			}
28581704f47bSPeter Zijlstra		}
285913214adfSAndy Whitcroft	}
286013214adfSAndy Whitcroft
286113214adfSAndy Whitcroft	# If we have no input at all, then there is nothing to report on
286213214adfSAndy Whitcroft	# so just keep quiet.
286313214adfSAndy Whitcroft	if ($#rawlines == -1) {
286413214adfSAndy Whitcroft		exit(0);
28650a920b5bSAndy Whitcroft	}
28660a920b5bSAndy Whitcroft
28678905a67cSAndy Whitcroft	# In mailback mode only produce a report in the negative, for
28688905a67cSAndy Whitcroft	# things that appear to be patches.
28698905a67cSAndy Whitcroft	if ($mailback && ($clean == 1 || !$is_patch)) {
28708905a67cSAndy Whitcroft		exit(0);
28718905a67cSAndy Whitcroft	}
28728905a67cSAndy Whitcroft
28738905a67cSAndy Whitcroft	# This is not a patch, and we are are in 'no-patch' mode so
28748905a67cSAndy Whitcroft	# just keep quiet.
28758905a67cSAndy Whitcroft	if (!$chk_patch && !$is_patch) {
28768905a67cSAndy Whitcroft		exit(0);
28778905a67cSAndy Whitcroft	}
28788905a67cSAndy Whitcroft
28798905a67cSAndy Whitcroft	if (!$is_patch) {
2880de7d4f0eSAndy Whitcroft		ERROR("Does not appear to be a unified-diff format patch\n");
28810a920b5bSAndy Whitcroft	}
28820a920b5bSAndy Whitcroft	if ($is_patch && $chk_signoff && $signoff == 0) {
2883de7d4f0eSAndy Whitcroft		ERROR("Missing Signed-off-by: line(s)\n");
28840a920b5bSAndy Whitcroft	}
28850a920b5bSAndy Whitcroft
2886f0a594c1SAndy Whitcroft	print report_dump();
288713214adfSAndy Whitcroft	if ($summary && !($clean == 1 && $quiet == 1)) {
288813214adfSAndy Whitcroft		print "$filename " if ($summary_file);
28896c72ffaaSAndy Whitcroft		print "total: $cnt_error errors, $cnt_warn warnings, " .
28906c72ffaaSAndy Whitcroft			(($check)? "$cnt_chk checks, " : "") .
28916c72ffaaSAndy Whitcroft			"$cnt_lines lines checked\n";
28928905a67cSAndy Whitcroft		print "\n" if ($quiet == 0);
28936c72ffaaSAndy Whitcroft	}
28948905a67cSAndy Whitcroft
2895d2c0a235SAndy Whitcroft	if ($quiet == 0) {
2896d2c0a235SAndy Whitcroft		# If there were whitespace errors which cleanpatch can fix
2897d2c0a235SAndy Whitcroft		# then suggest that.
2898d2c0a235SAndy Whitcroft		if ($rpt_cleaners) {
2899d2c0a235SAndy Whitcroft			print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n";
2900d2c0a235SAndy Whitcroft			print "      scripts/cleanfile\n\n";
2901d2c0a235SAndy Whitcroft		}
2902d2c0a235SAndy Whitcroft	}
2903d2c0a235SAndy Whitcroft
29040a920b5bSAndy Whitcroft	if ($clean == 1 && $quiet == 0) {
2905c2fdda0dSAndy Whitcroft		print "$vname has no obvious style problems and is ready for submission.\n"
29060a920b5bSAndy Whitcroft	}
29070a920b5bSAndy Whitcroft	if ($clean == 0 && $quiet == 0) {
2908c2fdda0dSAndy Whitcroft		print "$vname has style problems, please review.  If any of these errors\n";
29090a920b5bSAndy Whitcroft		print "are false positives report them to the maintainer, see\n";
29100a920b5bSAndy Whitcroft		print "CHECKPATCH in MAINTAINERS.\n";
29110a920b5bSAndy Whitcroft	}
291213214adfSAndy Whitcroft
29130a920b5bSAndy Whitcroft	return $clean;
29140a920b5bSAndy Whitcroft}
2915