xref: /linux-6.15/scripts/checkpatch.pl (revision b998e001)
10a920b5bSAndy Whitcroft#!/usr/bin/perl -w
2dbf004d7SDave Jones# (c) 2001, Dave Jones. (the file handling bit)
300df344fSAndy Whitcroft# (c) 2005, Joel Schopp <[email protected]> (the ugly bit)
42a5a2c25SAndy Whitcroft# (c) 2007,2008, Andy Whitcroft <[email protected]> (new conditions, test suite)
5131edb34SAndy Whitcroft# (c) 2008,2009, Andy Whitcroft <[email protected]>
60a920b5bSAndy Whitcroft# Licensed under the terms of the GNU GPL License version 2
70a920b5bSAndy Whitcroft
80a920b5bSAndy Whitcroftuse strict;
90a920b5bSAndy Whitcroft
100a920b5bSAndy Whitcroftmy $P = $0;
1100df344fSAndy Whitcroft$P =~ s@.*/@@g;
120a920b5bSAndy Whitcroft
135e8d8f6fSAndy Whitcroftmy $V = '0.30';
140a920b5bSAndy Whitcroft
150a920b5bSAndy Whitcroftuse Getopt::Long qw(:config no_auto_abbrev);
160a920b5bSAndy Whitcroft
170a920b5bSAndy Whitcroftmy $quiet = 0;
180a920b5bSAndy Whitcroftmy $tree = 1;
190a920b5bSAndy Whitcroftmy $chk_signoff = 1;
200a920b5bSAndy Whitcroftmy $chk_patch = 1;
21773647a0SAndy Whitcroftmy $tst_only;
226c72ffaaSAndy Whitcroftmy $emacs = 0;
238905a67cSAndy Whitcroftmy $terse = 0;
246c72ffaaSAndy Whitcroftmy $file = 0;
256c72ffaaSAndy Whitcroftmy $check = 0;
268905a67cSAndy Whitcroftmy $summary = 1;
278905a67cSAndy Whitcroftmy $mailback = 0;
2813214adfSAndy Whitcroftmy $summary_file = 0;
296c72ffaaSAndy Whitcroftmy $root;
30c2fdda0dSAndy Whitcroftmy %debug;
3177f5b10aSHannes Edermy $help = 0;
3277f5b10aSHannes Eder
3377f5b10aSHannes Edersub help {
3477f5b10aSHannes Eder	my ($exitcode) = @_;
3577f5b10aSHannes Eder
3677f5b10aSHannes Eder	print << "EOM";
3777f5b10aSHannes EderUsage: $P [OPTION]... [FILE]...
3877f5b10aSHannes EderVersion: $V
3977f5b10aSHannes Eder
4077f5b10aSHannes EderOptions:
4177f5b10aSHannes Eder  -q, --quiet                quiet
4277f5b10aSHannes Eder  --no-tree                  run without a kernel tree
4377f5b10aSHannes Eder  --no-signoff               do not check for 'Signed-off-by' line
4477f5b10aSHannes Eder  --patch                    treat FILE as patchfile (default)
4577f5b10aSHannes Eder  --emacs                    emacs compile window format
4677f5b10aSHannes Eder  --terse                    one line per report
4777f5b10aSHannes Eder  -f, --file                 treat FILE as regular source file
4877f5b10aSHannes Eder  --subjective, --strict     enable more subjective tests
4977f5b10aSHannes Eder  --root=PATH                PATH to the kernel tree root
5077f5b10aSHannes Eder  --no-summary               suppress the per-file summary
5177f5b10aSHannes Eder  --mailback                 only produce a report in case of warnings/errors
5277f5b10aSHannes Eder  --summary-file             include the filename in summary
5377f5b10aSHannes Eder  --debug KEY=[0|1]          turn on/off debugging of KEY, where KEY is one of
5477f5b10aSHannes Eder                             'values', 'possible', 'type', and 'attr' (default
5577f5b10aSHannes Eder                             is all off)
5677f5b10aSHannes Eder  --test-only=WORD           report only warnings/errors containing WORD
5777f5b10aSHannes Eder                             literally
5877f5b10aSHannes Eder  -h, --help, --version      display this help and exit
5977f5b10aSHannes Eder
6077f5b10aSHannes EderWhen FILE is - read standard input.
6177f5b10aSHannes EderEOM
6277f5b10aSHannes Eder
6377f5b10aSHannes Eder	exit($exitcode);
6477f5b10aSHannes Eder}
6577f5b10aSHannes Eder
660a920b5bSAndy WhitcroftGetOptions(
676c72ffaaSAndy Whitcroft	'q|quiet+'	=> \$quiet,
680a920b5bSAndy Whitcroft	'tree!'		=> \$tree,
690a920b5bSAndy Whitcroft	'signoff!'	=> \$chk_signoff,
700a920b5bSAndy Whitcroft	'patch!'	=> \$chk_patch,
716c72ffaaSAndy Whitcroft	'emacs!'	=> \$emacs,
728905a67cSAndy Whitcroft	'terse!'	=> \$terse,
7377f5b10aSHannes Eder	'f|file!'	=> \$file,
746c72ffaaSAndy Whitcroft	'subjective!'	=> \$check,
756c72ffaaSAndy Whitcroft	'strict!'	=> \$check,
766c72ffaaSAndy Whitcroft	'root=s'	=> \$root,
778905a67cSAndy Whitcroft	'summary!'	=> \$summary,
788905a67cSAndy Whitcroft	'mailback!'	=> \$mailback,
7913214adfSAndy Whitcroft	'summary-file!'	=> \$summary_file,
8013214adfSAndy Whitcroft
81c2fdda0dSAndy Whitcroft	'debug=s'	=> \%debug,
82773647a0SAndy Whitcroft	'test-only=s'	=> \$tst_only,
8377f5b10aSHannes Eder	'h|help'	=> \$help,
8477f5b10aSHannes Eder	'version'	=> \$help
8577f5b10aSHannes Eder) or help(1);
8677f5b10aSHannes Eder
8777f5b10aSHannes Ederhelp(0) if ($help);
880a920b5bSAndy Whitcroft
890a920b5bSAndy Whitcroftmy $exit = 0;
900a920b5bSAndy Whitcroft
910a920b5bSAndy Whitcroftif ($#ARGV < 0) {
9277f5b10aSHannes Eder	print "$P: no input files\n";
930a920b5bSAndy Whitcroft	exit(1);
940a920b5bSAndy Whitcroft}
950a920b5bSAndy Whitcroft
96c2fdda0dSAndy Whitcroftmy $dbg_values = 0;
97c2fdda0dSAndy Whitcroftmy $dbg_possible = 0;
987429c690SAndy Whitcroftmy $dbg_type = 0;
99a1ef277eSAndy Whitcroftmy $dbg_attr = 0;
100c2fdda0dSAndy Whitcroftfor my $key (keys %debug) {
10121caa13cSAndy Whitcroft	## no critic
10221caa13cSAndy Whitcroft	eval "\${dbg_$key} = '$debug{$key}';";
10321caa13cSAndy Whitcroft	die "$@" if ($@);
104c2fdda0dSAndy Whitcroft}
105c2fdda0dSAndy Whitcroft
1068905a67cSAndy Whitcroftif ($terse) {
1078905a67cSAndy Whitcroft	$emacs = 1;
1088905a67cSAndy Whitcroft	$quiet++;
1098905a67cSAndy Whitcroft}
1108905a67cSAndy Whitcroft
1116c72ffaaSAndy Whitcroftif ($tree) {
1126c72ffaaSAndy Whitcroft	if (defined $root) {
1136c72ffaaSAndy Whitcroft		if (!top_of_kernel_tree($root)) {
1146c72ffaaSAndy Whitcroft			die "$P: $root: --root does not point at a valid tree\n";
1156c72ffaaSAndy Whitcroft		}
1166c72ffaaSAndy Whitcroft	} else {
1176c72ffaaSAndy Whitcroft		if (top_of_kernel_tree('.')) {
1186c72ffaaSAndy Whitcroft			$root = '.';
1196c72ffaaSAndy Whitcroft		} elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
1206c72ffaaSAndy Whitcroft						top_of_kernel_tree($1)) {
1216c72ffaaSAndy Whitcroft			$root = $1;
1226c72ffaaSAndy Whitcroft		}
1236c72ffaaSAndy Whitcroft	}
1246c72ffaaSAndy Whitcroft
1256c72ffaaSAndy Whitcroft	if (!defined $root) {
1260a920b5bSAndy Whitcroft		print "Must be run from the top-level dir. of a kernel tree\n";
1270a920b5bSAndy Whitcroft		exit(2);
1280a920b5bSAndy Whitcroft	}
1296c72ffaaSAndy Whitcroft}
1306c72ffaaSAndy Whitcroft
1316c72ffaaSAndy Whitcroftmy $emitted_corrupt = 0;
1326c72ffaaSAndy Whitcroft
1332ceb532bSAndy Whitcroftour $Ident	= qr{
1342ceb532bSAndy Whitcroft			[A-Za-z_][A-Za-z\d_]*
1352ceb532bSAndy Whitcroft			(?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
1362ceb532bSAndy Whitcroft		}x;
1376c72ffaaSAndy Whitcroftour $Storage	= qr{extern|static|asmlinkage};
1386c72ffaaSAndy Whitcroftour $Sparse	= qr{
1396c72ffaaSAndy Whitcroft			__user|
1406c72ffaaSAndy Whitcroft			__kernel|
1416c72ffaaSAndy Whitcroft			__force|
1426c72ffaaSAndy Whitcroft			__iomem|
1436c72ffaaSAndy Whitcroft			__must_check|
1446c72ffaaSAndy Whitcroft			__init_refok|
145417495edSAndy Whitcroft			__kprobes|
146417495edSAndy Whitcroft			__ref
1476c72ffaaSAndy Whitcroft		}x;
14852131292SWolfram Sang
14952131292SWolfram Sang# Notes to $Attribute:
15052131292SWolfram Sang# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
1516c72ffaaSAndy Whitcroftour $Attribute	= qr{
1526c72ffaaSAndy Whitcroft			const|
1536c72ffaaSAndy Whitcroft			__read_mostly|
1546c72ffaaSAndy Whitcroft			__kprobes|
15552131292SWolfram Sang			__(?:mem|cpu|dev|)(?:initdata|initconst|init\b)|
15624e1d81aSAndy Whitcroft			____cacheline_aligned|
15724e1d81aSAndy Whitcroft			____cacheline_aligned_in_smp|
1585fe3af11SAndy Whitcroft			____cacheline_internodealigned_in_smp|
1595fe3af11SAndy Whitcroft			__weak
1606c72ffaaSAndy Whitcroft		  }x;
161c45dcabdSAndy Whitcroftour $Modifier;
1626c72ffaaSAndy Whitcroftour $Inline	= qr{inline|__always_inline|noinline};
1636c72ffaaSAndy Whitcroftour $Member	= qr{->$Ident|\.$Ident|\[[^]]*\]};
1646c72ffaaSAndy Whitcroftour $Lval	= qr{$Ident(?:$Member)*};
1656c72ffaaSAndy Whitcroft
1666c72ffaaSAndy Whitcroftour $Constant	= qr{(?:[0-9]+|0x[0-9a-fA-F]+)[UL]*};
1676c72ffaaSAndy Whitcroftour $Assignment	= qr{(?:\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=)};
16886f9d059SAndy Whitcroftour $Compare    = qr{<=|>=|==|!=|<|>};
1696c72ffaaSAndy Whitcroftour $Operators	= qr{
1706c72ffaaSAndy Whitcroft			<=|>=|==|!=|
1716c72ffaaSAndy Whitcroft			=>|->|<<|>>|<|>|!|~|
172c2fdda0dSAndy Whitcroft			&&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%
1736c72ffaaSAndy Whitcroft		  }x;
1746c72ffaaSAndy Whitcroft
1758905a67cSAndy Whitcroftour $NonptrType;
1768905a67cSAndy Whitcroftour $Type;
1778905a67cSAndy Whitcroftour $Declare;
1788905a67cSAndy Whitcroft
179171ae1a4SAndy Whitcroftour $UTF8	= qr {
180171ae1a4SAndy Whitcroft	[\x09\x0A\x0D\x20-\x7E]              # ASCII
181171ae1a4SAndy Whitcroft	| [\xC2-\xDF][\x80-\xBF]             # non-overlong 2-byte
182171ae1a4SAndy Whitcroft	|  \xE0[\xA0-\xBF][\x80-\xBF]        # excluding overlongs
183171ae1a4SAndy Whitcroft	| [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}  # straight 3-byte
184171ae1a4SAndy Whitcroft	|  \xED[\x80-\x9F][\x80-\xBF]        # excluding surrogates
185171ae1a4SAndy Whitcroft	|  \xF0[\x90-\xBF][\x80-\xBF]{2}     # planes 1-3
186171ae1a4SAndy Whitcroft	| [\xF1-\xF3][\x80-\xBF]{3}          # planes 4-15
187171ae1a4SAndy Whitcroft	|  \xF4[\x80-\x8F][\x80-\xBF]{2}     # plane 16
188171ae1a4SAndy Whitcroft}x;
189171ae1a4SAndy Whitcroft
1908ed22cadSAndy Whitcroftour $typeTypedefs = qr{(?x:
191fb9e9096SAndy Whitcroft	(?:__)?(?:u|s|be|le)(?:8|16|32|64)|
1928ed22cadSAndy Whitcroft	atomic_t
1938ed22cadSAndy Whitcroft)};
1948ed22cadSAndy Whitcroft
195691e669bSJoe Perchesour $logFunctions = qr{(?x:
196691e669bSJoe Perches	printk|
197691e669bSJoe Perches	pr_(debug|dbg|vdbg|devel|info|warning|err|notice|alert|crit|emerg|cont)|
1988bbea968SJoe Perches	(dev|netdev|netif)_(printk|dbg|vdbg|info|warn|err|notice|alert|crit|emerg|WARN)|
199691e669bSJoe Perches	WARN|
200691e669bSJoe Perches	panic
201691e669bSJoe Perches)};
202691e669bSJoe Perches
2038905a67cSAndy Whitcroftour @typeList = (
2048905a67cSAndy Whitcroft	qr{void},
205c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?char},
206c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?short},
207c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?int},
208c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?long},
209c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?long\s+int},
210c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?long\s+long},
211c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?long\s+long\s+int},
2128905a67cSAndy Whitcroft	qr{unsigned},
2138905a67cSAndy Whitcroft	qr{float},
2148905a67cSAndy Whitcroft	qr{double},
2158905a67cSAndy Whitcroft	qr{bool},
2168905a67cSAndy Whitcroft	qr{struct\s+$Ident},
2178905a67cSAndy Whitcroft	qr{union\s+$Ident},
2188905a67cSAndy Whitcroft	qr{enum\s+$Ident},
2198905a67cSAndy Whitcroft	qr{${Ident}_t},
2208905a67cSAndy Whitcroft	qr{${Ident}_handler},
2218905a67cSAndy Whitcroft	qr{${Ident}_handler_fn},
2228905a67cSAndy Whitcroft);
223c45dcabdSAndy Whitcroftour @modifierList = (
224c45dcabdSAndy Whitcroft	qr{fastcall},
225c45dcabdSAndy Whitcroft);
2268905a67cSAndy Whitcroft
2277840a94cSWolfram Sangour $allowed_asm_includes = qr{(?x:
2287840a94cSWolfram Sang	irq|
2297840a94cSWolfram Sang	memory
2307840a94cSWolfram Sang)};
2317840a94cSWolfram Sang# memory.h: ARM has a custom one
2327840a94cSWolfram Sang
2338905a67cSAndy Whitcroftsub build_types {
234d2172eb5SAndy Whitcroft	my $mods = "(?x:  \n" . join("|\n  ", @modifierList) . "\n)";
235d2172eb5SAndy Whitcroft	my $all = "(?x:  \n" . join("|\n  ", @typeList) . "\n)";
236c8cb2ca3SAndy Whitcroft	$Modifier	= qr{(?:$Attribute|$Sparse|$mods)};
2378905a67cSAndy Whitcroft	$NonptrType	= qr{
238d2172eb5SAndy Whitcroft			(?:$Modifier\s+|const\s+)*
239cf655043SAndy Whitcroft			(?:
240c45dcabdSAndy Whitcroft				(?:typeof|__typeof__)\s*\(\s*\**\s*$Ident\s*\)|
2418ed22cadSAndy Whitcroft				(?:$typeTypedefs\b)|
242c45dcabdSAndy Whitcroft				(?:${all}\b)
243cf655043SAndy Whitcroft			)
244c8cb2ca3SAndy Whitcroft			(?:\s+$Modifier|\s+const)*
2458905a67cSAndy Whitcroft		  }x;
2468905a67cSAndy Whitcroft	$Type	= qr{
247c45dcabdSAndy Whitcroft			$NonptrType
24865863862SAndy Whitcroft			(?:[\s\*]+\s*const|[\s\*]+|(?:\s*\[\s*\])+)?
249c8cb2ca3SAndy Whitcroft			(?:\s+$Inline|\s+$Modifier)*
2508905a67cSAndy Whitcroft		  }x;
2518905a67cSAndy Whitcroft	$Declare	= qr{(?:$Storage\s+)?$Type};
2528905a67cSAndy Whitcroft}
2538905a67cSAndy Whitcroftbuild_types();
2546c72ffaaSAndy Whitcroft
2556c72ffaaSAndy Whitcroft$chk_signoff = 0 if ($file);
2560a920b5bSAndy Whitcroft
2574a0df2efSAndy Whitcroftmy @dep_includes = ();
2584a0df2efSAndy Whitcroftmy @dep_functions = ();
2596c72ffaaSAndy Whitcroftmy $removal = "Documentation/feature-removal-schedule.txt";
2606c72ffaaSAndy Whitcroftif ($tree && -f "$root/$removal") {
26121caa13cSAndy Whitcroft	open(my $REMOVE, '<', "$root/$removal") ||
2626c72ffaaSAndy Whitcroft				die "$P: $removal: open failed - $!\n";
26321caa13cSAndy Whitcroft	while (<$REMOVE>) {
264f0a594c1SAndy Whitcroft		if (/^Check:\s+(.*\S)/) {
265f0a594c1SAndy Whitcroft			for my $entry (split(/[, ]+/, $1)) {
266f0a594c1SAndy Whitcroft				if ($entry =~ m@include/(.*)@) {
2674a0df2efSAndy Whitcroft					push(@dep_includes, $1);
2684a0df2efSAndy Whitcroft
269f0a594c1SAndy Whitcroft				} elsif ($entry !~ m@/@) {
270f0a594c1SAndy Whitcroft					push(@dep_functions, $entry);
271f0a594c1SAndy Whitcroft				}
2724a0df2efSAndy Whitcroft			}
2730a920b5bSAndy Whitcroft		}
2740a920b5bSAndy Whitcroft	}
27521caa13cSAndy Whitcroft	close($REMOVE);
2760a920b5bSAndy Whitcroft}
2770a920b5bSAndy Whitcroft
27800df344fSAndy Whitcroftmy @rawlines = ();
279c2fdda0dSAndy Whitcroftmy @lines = ();
280c2fdda0dSAndy Whitcroftmy $vname;
2816c72ffaaSAndy Whitcroftfor my $filename (@ARGV) {
28221caa13cSAndy Whitcroft	my $FILE;
2836c72ffaaSAndy Whitcroft	if ($file) {
28421caa13cSAndy Whitcroft		open($FILE, '-|', "diff -u /dev/null $filename") ||
2856c72ffaaSAndy Whitcroft			die "$P: $filename: diff failed - $!\n";
28621caa13cSAndy Whitcroft	} elsif ($filename eq '-') {
28721caa13cSAndy Whitcroft		open($FILE, '<&STDIN');
2886c72ffaaSAndy Whitcroft	} else {
28921caa13cSAndy Whitcroft		open($FILE, '<', "$filename") ||
2906c72ffaaSAndy Whitcroft			die "$P: $filename: open failed - $!\n";
2916c72ffaaSAndy Whitcroft	}
292c2fdda0dSAndy Whitcroft	if ($filename eq '-') {
293c2fdda0dSAndy Whitcroft		$vname = 'Your patch';
294c2fdda0dSAndy Whitcroft	} else {
295c2fdda0dSAndy Whitcroft		$vname = $filename;
296c2fdda0dSAndy Whitcroft	}
29721caa13cSAndy Whitcroft	while (<$FILE>) {
2980a920b5bSAndy Whitcroft		chomp;
29900df344fSAndy Whitcroft		push(@rawlines, $_);
3006c72ffaaSAndy Whitcroft	}
30121caa13cSAndy Whitcroft	close($FILE);
302c2fdda0dSAndy Whitcroft	if (!process($filename)) {
3030a920b5bSAndy Whitcroft		$exit = 1;
3040a920b5bSAndy Whitcroft	}
30500df344fSAndy Whitcroft	@rawlines = ();
30613214adfSAndy Whitcroft	@lines = ();
3070a920b5bSAndy Whitcroft}
3080a920b5bSAndy Whitcroft
3090a920b5bSAndy Whitcroftexit($exit);
3100a920b5bSAndy Whitcroft
3110a920b5bSAndy Whitcroftsub top_of_kernel_tree {
3126c72ffaaSAndy Whitcroft	my ($root) = @_;
3136c72ffaaSAndy Whitcroft
3146c72ffaaSAndy Whitcroft	my @tree_check = (
3156c72ffaaSAndy Whitcroft		"COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
3166c72ffaaSAndy Whitcroft		"README", "Documentation", "arch", "include", "drivers",
3176c72ffaaSAndy Whitcroft		"fs", "init", "ipc", "kernel", "lib", "scripts",
3186c72ffaaSAndy Whitcroft	);
3196c72ffaaSAndy Whitcroft
3206c72ffaaSAndy Whitcroft	foreach my $check (@tree_check) {
3216c72ffaaSAndy Whitcroft		if (! -e $root . '/' . $check) {
3220a920b5bSAndy Whitcroft			return 0;
3230a920b5bSAndy Whitcroft		}
3246c72ffaaSAndy Whitcroft	}
3256c72ffaaSAndy Whitcroft	return 1;
3266c72ffaaSAndy Whitcroft}
3270a920b5bSAndy Whitcroft
3280a920b5bSAndy Whitcroftsub expand_tabs {
3290a920b5bSAndy Whitcroft	my ($str) = @_;
3300a920b5bSAndy Whitcroft
3310a920b5bSAndy Whitcroft	my $res = '';
3320a920b5bSAndy Whitcroft	my $n = 0;
3330a920b5bSAndy Whitcroft	for my $c (split(//, $str)) {
3340a920b5bSAndy Whitcroft		if ($c eq "\t") {
3350a920b5bSAndy Whitcroft			$res .= ' ';
3360a920b5bSAndy Whitcroft			$n++;
3370a920b5bSAndy Whitcroft			for (; ($n % 8) != 0; $n++) {
3380a920b5bSAndy Whitcroft				$res .= ' ';
3390a920b5bSAndy Whitcroft			}
3400a920b5bSAndy Whitcroft			next;
3410a920b5bSAndy Whitcroft		}
3420a920b5bSAndy Whitcroft		$res .= $c;
3430a920b5bSAndy Whitcroft		$n++;
3440a920b5bSAndy Whitcroft	}
3450a920b5bSAndy Whitcroft
3460a920b5bSAndy Whitcroft	return $res;
3470a920b5bSAndy Whitcroft}
3486c72ffaaSAndy Whitcroftsub copy_spacing {
349773647a0SAndy Whitcroft	(my $res = shift) =~ tr/\t/ /c;
3506c72ffaaSAndy Whitcroft	return $res;
3516c72ffaaSAndy Whitcroft}
3520a920b5bSAndy Whitcroft
3534a0df2efSAndy Whitcroftsub line_stats {
3544a0df2efSAndy Whitcroft	my ($line) = @_;
3554a0df2efSAndy Whitcroft
3564a0df2efSAndy Whitcroft	# Drop the diff line leader and expand tabs
3574a0df2efSAndy Whitcroft	$line =~ s/^.//;
3584a0df2efSAndy Whitcroft	$line = expand_tabs($line);
3594a0df2efSAndy Whitcroft
3604a0df2efSAndy Whitcroft	# Pick the indent from the front of the line.
3614a0df2efSAndy Whitcroft	my ($white) = ($line =~ /^(\s*)/);
3624a0df2efSAndy Whitcroft
3634a0df2efSAndy Whitcroft	return (length($line), length($white));
3644a0df2efSAndy Whitcroft}
3654a0df2efSAndy Whitcroft
366773647a0SAndy Whitcroftmy $sanitise_quote = '';
367773647a0SAndy Whitcroft
368773647a0SAndy Whitcroftsub sanitise_line_reset {
369773647a0SAndy Whitcroft	my ($in_comment) = @_;
370773647a0SAndy Whitcroft
371773647a0SAndy Whitcroft	if ($in_comment) {
372773647a0SAndy Whitcroft		$sanitise_quote = '*/';
373773647a0SAndy Whitcroft	} else {
374773647a0SAndy Whitcroft		$sanitise_quote = '';
375773647a0SAndy Whitcroft	}
376773647a0SAndy Whitcroft}
37700df344fSAndy Whitcroftsub sanitise_line {
37800df344fSAndy Whitcroft	my ($line) = @_;
37900df344fSAndy Whitcroft
38000df344fSAndy Whitcroft	my $res = '';
38100df344fSAndy Whitcroft	my $l = '';
38200df344fSAndy Whitcroft
383c2fdda0dSAndy Whitcroft	my $qlen = 0;
384773647a0SAndy Whitcroft	my $off = 0;
385773647a0SAndy Whitcroft	my $c;
38600df344fSAndy Whitcroft
387773647a0SAndy Whitcroft	# Always copy over the diff marker.
388773647a0SAndy Whitcroft	$res = substr($line, 0, 1);
389773647a0SAndy Whitcroft
390773647a0SAndy Whitcroft	for ($off = 1; $off < length($line); $off++) {
391773647a0SAndy Whitcroft		$c = substr($line, $off, 1);
392773647a0SAndy Whitcroft
393773647a0SAndy Whitcroft		# Comments we are wacking completly including the begin
394773647a0SAndy Whitcroft		# and end, all to $;.
395773647a0SAndy Whitcroft		if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
396773647a0SAndy Whitcroft			$sanitise_quote = '*/';
397773647a0SAndy Whitcroft
398773647a0SAndy Whitcroft			substr($res, $off, 2, "$;$;");
399773647a0SAndy Whitcroft			$off++;
40000df344fSAndy Whitcroft			next;
401773647a0SAndy Whitcroft		}
40281bc0e02SAndy Whitcroft		if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
403773647a0SAndy Whitcroft			$sanitise_quote = '';
404773647a0SAndy Whitcroft			substr($res, $off, 2, "$;$;");
405773647a0SAndy Whitcroft			$off++;
406773647a0SAndy Whitcroft			next;
407773647a0SAndy Whitcroft		}
408113f04a8SDaniel Walker		if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
409113f04a8SDaniel Walker			$sanitise_quote = '//';
410113f04a8SDaniel Walker
411113f04a8SDaniel Walker			substr($res, $off, 2, $sanitise_quote);
412113f04a8SDaniel Walker			$off++;
413113f04a8SDaniel Walker			next;
414113f04a8SDaniel Walker		}
415773647a0SAndy Whitcroft
416773647a0SAndy Whitcroft		# A \ in a string means ignore the next character.
417773647a0SAndy Whitcroft		if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
418773647a0SAndy Whitcroft		    $c eq "\\") {
419773647a0SAndy Whitcroft			substr($res, $off, 2, 'XX');
420773647a0SAndy Whitcroft			$off++;
421773647a0SAndy Whitcroft			next;
422773647a0SAndy Whitcroft		}
423773647a0SAndy Whitcroft		# Regular quotes.
424773647a0SAndy Whitcroft		if ($c eq "'" || $c eq '"') {
425773647a0SAndy Whitcroft			if ($sanitise_quote eq '') {
426773647a0SAndy Whitcroft				$sanitise_quote = $c;
427773647a0SAndy Whitcroft
428773647a0SAndy Whitcroft				substr($res, $off, 1, $c);
429773647a0SAndy Whitcroft				next;
430773647a0SAndy Whitcroft			} elsif ($sanitise_quote eq $c) {
431773647a0SAndy Whitcroft				$sanitise_quote = '';
43200df344fSAndy Whitcroft			}
43300df344fSAndy Whitcroft		}
434773647a0SAndy Whitcroft
435fae17daeSAndy Whitcroft		#print "c<$c> SQ<$sanitise_quote>\n";
436773647a0SAndy Whitcroft		if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
437773647a0SAndy Whitcroft			substr($res, $off, 1, $;);
438113f04a8SDaniel Walker		} elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
439113f04a8SDaniel Walker			substr($res, $off, 1, $;);
440773647a0SAndy Whitcroft		} elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
441773647a0SAndy Whitcroft			substr($res, $off, 1, 'X');
44200df344fSAndy Whitcroft		} else {
443773647a0SAndy Whitcroft			substr($res, $off, 1, $c);
44400df344fSAndy Whitcroft		}
445c2fdda0dSAndy Whitcroft	}
446c2fdda0dSAndy Whitcroft
447113f04a8SDaniel Walker	if ($sanitise_quote eq '//') {
448113f04a8SDaniel Walker		$sanitise_quote = '';
449113f04a8SDaniel Walker	}
450113f04a8SDaniel Walker
451c2fdda0dSAndy Whitcroft	# The pathname on a #include may be surrounded by '<' and '>'.
452c45dcabdSAndy Whitcroft	if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
453c2fdda0dSAndy Whitcroft		my $clean = 'X' x length($1);
454c2fdda0dSAndy Whitcroft		$res =~ s@\<.*\>@<$clean>@;
455c2fdda0dSAndy Whitcroft
456c2fdda0dSAndy Whitcroft	# The whole of a #error is a string.
457c45dcabdSAndy Whitcroft	} elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
458c2fdda0dSAndy Whitcroft		my $clean = 'X' x length($1);
459c45dcabdSAndy Whitcroft		$res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
460c2fdda0dSAndy Whitcroft	}
461c2fdda0dSAndy Whitcroft
46200df344fSAndy Whitcroft	return $res;
46300df344fSAndy Whitcroft}
46400df344fSAndy Whitcroft
4658905a67cSAndy Whitcroftsub ctx_statement_block {
4668905a67cSAndy Whitcroft	my ($linenr, $remain, $off) = @_;
4678905a67cSAndy Whitcroft	my $line = $linenr - 1;
4688905a67cSAndy Whitcroft	my $blk = '';
4698905a67cSAndy Whitcroft	my $soff = $off;
4708905a67cSAndy Whitcroft	my $coff = $off - 1;
471773647a0SAndy Whitcroft	my $coff_set = 0;
4728905a67cSAndy Whitcroft
47313214adfSAndy Whitcroft	my $loff = 0;
47413214adfSAndy Whitcroft
4758905a67cSAndy Whitcroft	my $type = '';
4768905a67cSAndy Whitcroft	my $level = 0;
477a2750645SAndy Whitcroft	my @stack = ();
478cf655043SAndy Whitcroft	my $p;
4798905a67cSAndy Whitcroft	my $c;
4808905a67cSAndy Whitcroft	my $len = 0;
48113214adfSAndy Whitcroft
48213214adfSAndy Whitcroft	my $remainder;
4838905a67cSAndy Whitcroft	while (1) {
484a2750645SAndy Whitcroft		@stack = (['', 0]) if ($#stack == -1);
485a2750645SAndy Whitcroft
486773647a0SAndy Whitcroft		#warn "CSB: blk<$blk> remain<$remain>\n";
4878905a67cSAndy Whitcroft		# If we are about to drop off the end, pull in more
4888905a67cSAndy Whitcroft		# context.
4898905a67cSAndy Whitcroft		if ($off >= $len) {
4908905a67cSAndy Whitcroft			for (; $remain > 0; $line++) {
491dea33496SAndy Whitcroft				last if (!defined $lines[$line]);
492c2fdda0dSAndy Whitcroft				next if ($lines[$line] =~ /^-/);
4938905a67cSAndy Whitcroft				$remain--;
49413214adfSAndy Whitcroft				$loff = $len;
495c2fdda0dSAndy Whitcroft				$blk .= $lines[$line] . "\n";
4968905a67cSAndy Whitcroft				$len = length($blk);
4978905a67cSAndy Whitcroft				$line++;
4988905a67cSAndy Whitcroft				last;
4998905a67cSAndy Whitcroft			}
5008905a67cSAndy Whitcroft			# Bail if there is no further context.
5018905a67cSAndy Whitcroft			#warn "CSB: blk<$blk> off<$off> len<$len>\n";
50213214adfSAndy Whitcroft			if ($off >= $len) {
5038905a67cSAndy Whitcroft				last;
5048905a67cSAndy Whitcroft			}
5058905a67cSAndy Whitcroft		}
506cf655043SAndy Whitcroft		$p = $c;
5078905a67cSAndy Whitcroft		$c = substr($blk, $off, 1);
50813214adfSAndy Whitcroft		$remainder = substr($blk, $off);
5098905a67cSAndy Whitcroft
510773647a0SAndy Whitcroft		#warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
5114635f4fbSAndy Whitcroft
5124635f4fbSAndy Whitcroft		# Handle nested #if/#else.
5134635f4fbSAndy Whitcroft		if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
5144635f4fbSAndy Whitcroft			push(@stack, [ $type, $level ]);
5154635f4fbSAndy Whitcroft		} elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
5164635f4fbSAndy Whitcroft			($type, $level) = @{$stack[$#stack - 1]};
5174635f4fbSAndy Whitcroft		} elsif ($remainder =~ /^#\s*endif\b/) {
5184635f4fbSAndy Whitcroft			($type, $level) = @{pop(@stack)};
5194635f4fbSAndy Whitcroft		}
5204635f4fbSAndy Whitcroft
5218905a67cSAndy Whitcroft		# Statement ends at the ';' or a close '}' at the
5228905a67cSAndy Whitcroft		# outermost level.
5238905a67cSAndy Whitcroft		if ($level == 0 && $c eq ';') {
5248905a67cSAndy Whitcroft			last;
5258905a67cSAndy Whitcroft		}
5268905a67cSAndy Whitcroft
52713214adfSAndy Whitcroft		# An else is really a conditional as long as its not else if
528773647a0SAndy Whitcroft		if ($level == 0 && $coff_set == 0 &&
529773647a0SAndy Whitcroft				(!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
530773647a0SAndy Whitcroft				$remainder =~ /^(else)(?:\s|{)/ &&
531773647a0SAndy Whitcroft				$remainder !~ /^else\s+if\b/) {
532773647a0SAndy Whitcroft			$coff = $off + length($1) - 1;
533773647a0SAndy Whitcroft			$coff_set = 1;
534773647a0SAndy Whitcroft			#warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
535773647a0SAndy Whitcroft			#warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
53613214adfSAndy Whitcroft		}
53713214adfSAndy Whitcroft
5388905a67cSAndy Whitcroft		if (($type eq '' || $type eq '(') && $c eq '(') {
5398905a67cSAndy Whitcroft			$level++;
5408905a67cSAndy Whitcroft			$type = '(';
5418905a67cSAndy Whitcroft		}
5428905a67cSAndy Whitcroft		if ($type eq '(' && $c eq ')') {
5438905a67cSAndy Whitcroft			$level--;
5448905a67cSAndy Whitcroft			$type = ($level != 0)? '(' : '';
5458905a67cSAndy Whitcroft
5468905a67cSAndy Whitcroft			if ($level == 0 && $coff < $soff) {
5478905a67cSAndy Whitcroft				$coff = $off;
548773647a0SAndy Whitcroft				$coff_set = 1;
549773647a0SAndy Whitcroft				#warn "CSB: mark coff<$coff>\n";
5508905a67cSAndy Whitcroft			}
5518905a67cSAndy Whitcroft		}
5528905a67cSAndy Whitcroft		if (($type eq '' || $type eq '{') && $c eq '{') {
5538905a67cSAndy Whitcroft			$level++;
5548905a67cSAndy Whitcroft			$type = '{';
5558905a67cSAndy Whitcroft		}
5568905a67cSAndy Whitcroft		if ($type eq '{' && $c eq '}') {
5578905a67cSAndy Whitcroft			$level--;
5588905a67cSAndy Whitcroft			$type = ($level != 0)? '{' : '';
5598905a67cSAndy Whitcroft
5608905a67cSAndy Whitcroft			if ($level == 0) {
561*b998e001SPatrick Pannuto				if (substr($blk, $off + 1, 1) eq ';') {
562*b998e001SPatrick Pannuto					$off++;
563*b998e001SPatrick Pannuto				}
5648905a67cSAndy Whitcroft				last;
5658905a67cSAndy Whitcroft			}
5668905a67cSAndy Whitcroft		}
5678905a67cSAndy Whitcroft		$off++;
5688905a67cSAndy Whitcroft	}
569a3bb97a7SAndy Whitcroft	# We are truly at the end, so shuffle to the next line.
57013214adfSAndy Whitcroft	if ($off == $len) {
571a3bb97a7SAndy Whitcroft		$loff = $len + 1;
57213214adfSAndy Whitcroft		$line++;
57313214adfSAndy Whitcroft		$remain--;
57413214adfSAndy Whitcroft	}
5758905a67cSAndy Whitcroft
5768905a67cSAndy Whitcroft	my $statement = substr($blk, $soff, $off - $soff + 1);
5778905a67cSAndy Whitcroft	my $condition = substr($blk, $soff, $coff - $soff + 1);
5788905a67cSAndy Whitcroft
5798905a67cSAndy Whitcroft	#warn "STATEMENT<$statement>\n";
5808905a67cSAndy Whitcroft	#warn "CONDITION<$condition>\n";
5818905a67cSAndy Whitcroft
582773647a0SAndy Whitcroft	#print "coff<$coff> soff<$off> loff<$loff>\n";
58313214adfSAndy Whitcroft
58413214adfSAndy Whitcroft	return ($statement, $condition,
58513214adfSAndy Whitcroft			$line, $remain + 1, $off - $loff + 1, $level);
58613214adfSAndy Whitcroft}
58713214adfSAndy Whitcroft
588cf655043SAndy Whitcroftsub statement_lines {
589cf655043SAndy Whitcroft	my ($stmt) = @_;
590cf655043SAndy Whitcroft
591cf655043SAndy Whitcroft	# Strip the diff line prefixes and rip blank lines at start and end.
592cf655043SAndy Whitcroft	$stmt =~ s/(^|\n)./$1/g;
593cf655043SAndy Whitcroft	$stmt =~ s/^\s*//;
594cf655043SAndy Whitcroft	$stmt =~ s/\s*$//;
595cf655043SAndy Whitcroft
596cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
597cf655043SAndy Whitcroft
598cf655043SAndy Whitcroft	return $#stmt_lines + 2;
599cf655043SAndy Whitcroft}
600cf655043SAndy Whitcroft
601cf655043SAndy Whitcroftsub statement_rawlines {
602cf655043SAndy Whitcroft	my ($stmt) = @_;
603cf655043SAndy Whitcroft
604cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
605cf655043SAndy Whitcroft
606cf655043SAndy Whitcroft	return $#stmt_lines + 2;
607cf655043SAndy Whitcroft}
608cf655043SAndy Whitcroft
609cf655043SAndy Whitcroftsub statement_block_size {
610cf655043SAndy Whitcroft	my ($stmt) = @_;
611cf655043SAndy Whitcroft
612cf655043SAndy Whitcroft	$stmt =~ s/(^|\n)./$1/g;
613cf655043SAndy Whitcroft	$stmt =~ s/^\s*{//;
614cf655043SAndy Whitcroft	$stmt =~ s/}\s*$//;
615cf655043SAndy Whitcroft	$stmt =~ s/^\s*//;
616cf655043SAndy Whitcroft	$stmt =~ s/\s*$//;
617cf655043SAndy Whitcroft
618cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
619cf655043SAndy Whitcroft	my @stmt_statements = ($stmt =~ /;/g);
620cf655043SAndy Whitcroft
621cf655043SAndy Whitcroft	my $stmt_lines = $#stmt_lines + 2;
622cf655043SAndy Whitcroft	my $stmt_statements = $#stmt_statements + 1;
623cf655043SAndy Whitcroft
624cf655043SAndy Whitcroft	if ($stmt_lines > $stmt_statements) {
625cf655043SAndy Whitcroft		return $stmt_lines;
626cf655043SAndy Whitcroft	} else {
627cf655043SAndy Whitcroft		return $stmt_statements;
628cf655043SAndy Whitcroft	}
629cf655043SAndy Whitcroft}
630cf655043SAndy Whitcroft
63113214adfSAndy Whitcroftsub ctx_statement_full {
63213214adfSAndy Whitcroft	my ($linenr, $remain, $off) = @_;
63313214adfSAndy Whitcroft	my ($statement, $condition, $level);
63413214adfSAndy Whitcroft
63513214adfSAndy Whitcroft	my (@chunks);
63613214adfSAndy Whitcroft
637cf655043SAndy Whitcroft	# Grab the first conditional/block pair.
63813214adfSAndy Whitcroft	($statement, $condition, $linenr, $remain, $off, $level) =
63913214adfSAndy Whitcroft				ctx_statement_block($linenr, $remain, $off);
640773647a0SAndy Whitcroft	#print "F: c<$condition> s<$statement> remain<$remain>\n";
64113214adfSAndy Whitcroft	push(@chunks, [ $condition, $statement ]);
642cf655043SAndy Whitcroft	if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
643cf655043SAndy Whitcroft		return ($level, $linenr, @chunks);
644cf655043SAndy Whitcroft	}
645cf655043SAndy Whitcroft
646cf655043SAndy Whitcroft	# Pull in the following conditional/block pairs and see if they
647cf655043SAndy Whitcroft	# could continue the statement.
648cf655043SAndy Whitcroft	for (;;) {
64913214adfSAndy Whitcroft		($statement, $condition, $linenr, $remain, $off, $level) =
65013214adfSAndy Whitcroft				ctx_statement_block($linenr, $remain, $off);
651cf655043SAndy Whitcroft		#print "C: c<$condition> s<$statement> remain<$remain>\n";
652773647a0SAndy Whitcroft		last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
653cf655043SAndy Whitcroft		#print "C: push\n";
654cf655043SAndy Whitcroft		push(@chunks, [ $condition, $statement ]);
65513214adfSAndy Whitcroft	}
65613214adfSAndy Whitcroft
65713214adfSAndy Whitcroft	return ($level, $linenr, @chunks);
6588905a67cSAndy Whitcroft}
6598905a67cSAndy Whitcroft
6604a0df2efSAndy Whitcroftsub ctx_block_get {
661f0a594c1SAndy Whitcroft	my ($linenr, $remain, $outer, $open, $close, $off) = @_;
6624a0df2efSAndy Whitcroft	my $line;
6634a0df2efSAndy Whitcroft	my $start = $linenr - 1;
6644a0df2efSAndy Whitcroft	my $blk = '';
6654a0df2efSAndy Whitcroft	my @o;
6664a0df2efSAndy Whitcroft	my @c;
6674a0df2efSAndy Whitcroft	my @res = ();
6684a0df2efSAndy Whitcroft
669f0a594c1SAndy Whitcroft	my $level = 0;
6704635f4fbSAndy Whitcroft	my @stack = ($level);
67100df344fSAndy Whitcroft	for ($line = $start; $remain > 0; $line++) {
67200df344fSAndy Whitcroft		next if ($rawlines[$line] =~ /^-/);
67300df344fSAndy Whitcroft		$remain--;
67400df344fSAndy Whitcroft
67500df344fSAndy Whitcroft		$blk .= $rawlines[$line];
6764635f4fbSAndy Whitcroft
6774635f4fbSAndy Whitcroft		# Handle nested #if/#else.
6784635f4fbSAndy Whitcroft		if ($rawlines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
6794635f4fbSAndy Whitcroft			push(@stack, $level);
6804635f4fbSAndy Whitcroft		} elsif ($rawlines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
6814635f4fbSAndy Whitcroft			$level = $stack[$#stack - 1];
6824635f4fbSAndy Whitcroft		} elsif ($rawlines[$line] =~ /^.\s*#\s*endif\b/) {
6834635f4fbSAndy Whitcroft			$level = pop(@stack);
6844635f4fbSAndy Whitcroft		}
6854635f4fbSAndy Whitcroft
686f0a594c1SAndy Whitcroft		foreach my $c (split(//, $rawlines[$line])) {
687f0a594c1SAndy Whitcroft			##print "C<$c>L<$level><$open$close>O<$off>\n";
688f0a594c1SAndy Whitcroft			if ($off > 0) {
689f0a594c1SAndy Whitcroft				$off--;
690f0a594c1SAndy Whitcroft				next;
691f0a594c1SAndy Whitcroft			}
6924a0df2efSAndy Whitcroft
693f0a594c1SAndy Whitcroft			if ($c eq $close && $level > 0) {
694f0a594c1SAndy Whitcroft				$level--;
695f0a594c1SAndy Whitcroft				last if ($level == 0);
696f0a594c1SAndy Whitcroft			} elsif ($c eq $open) {
697f0a594c1SAndy Whitcroft				$level++;
698f0a594c1SAndy Whitcroft			}
699f0a594c1SAndy Whitcroft		}
7004a0df2efSAndy Whitcroft
701f0a594c1SAndy Whitcroft		if (!$outer || $level <= 1) {
70200df344fSAndy Whitcroft			push(@res, $rawlines[$line]);
7034a0df2efSAndy Whitcroft		}
7044a0df2efSAndy Whitcroft
705f0a594c1SAndy Whitcroft		last if ($level == 0);
7064a0df2efSAndy Whitcroft	}
7074a0df2efSAndy Whitcroft
708f0a594c1SAndy Whitcroft	return ($level, @res);
7094a0df2efSAndy Whitcroft}
7104a0df2efSAndy Whitcroftsub ctx_block_outer {
7114a0df2efSAndy Whitcroft	my ($linenr, $remain) = @_;
7124a0df2efSAndy Whitcroft
713f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
714f0a594c1SAndy Whitcroft	return @r;
7154a0df2efSAndy Whitcroft}
7164a0df2efSAndy Whitcroftsub ctx_block {
7174a0df2efSAndy Whitcroft	my ($linenr, $remain) = @_;
7184a0df2efSAndy Whitcroft
719f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
720f0a594c1SAndy Whitcroft	return @r;
721653d4876SAndy Whitcroft}
722653d4876SAndy Whitcroftsub ctx_statement {
723f0a594c1SAndy Whitcroft	my ($linenr, $remain, $off) = @_;
724f0a594c1SAndy Whitcroft
725f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
726f0a594c1SAndy Whitcroft	return @r;
727f0a594c1SAndy Whitcroft}
728f0a594c1SAndy Whitcroftsub ctx_block_level {
729653d4876SAndy Whitcroft	my ($linenr, $remain) = @_;
730653d4876SAndy Whitcroft
731f0a594c1SAndy Whitcroft	return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
7324a0df2efSAndy Whitcroft}
7339c0ca6f9SAndy Whitcroftsub ctx_statement_level {
7349c0ca6f9SAndy Whitcroft	my ($linenr, $remain, $off) = @_;
7359c0ca6f9SAndy Whitcroft
7369c0ca6f9SAndy Whitcroft	return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
7379c0ca6f9SAndy Whitcroft}
7384a0df2efSAndy Whitcroft
7394a0df2efSAndy Whitcroftsub ctx_locate_comment {
7404a0df2efSAndy Whitcroft	my ($first_line, $end_line) = @_;
7414a0df2efSAndy Whitcroft
7424a0df2efSAndy Whitcroft	# Catch a comment on the end of the line itself.
743beae6332SAndy Whitcroft	my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
7444a0df2efSAndy Whitcroft	return $current_comment if (defined $current_comment);
7454a0df2efSAndy Whitcroft
7464a0df2efSAndy Whitcroft	# Look through the context and try and figure out if there is a
7474a0df2efSAndy Whitcroft	# comment.
7484a0df2efSAndy Whitcroft	my $in_comment = 0;
7494a0df2efSAndy Whitcroft	$current_comment = '';
7504a0df2efSAndy Whitcroft	for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
75100df344fSAndy Whitcroft		my $line = $rawlines[$linenr - 1];
75200df344fSAndy Whitcroft		#warn "           $line\n";
7534a0df2efSAndy Whitcroft		if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
7544a0df2efSAndy Whitcroft			$in_comment = 1;
7554a0df2efSAndy Whitcroft		}
7564a0df2efSAndy Whitcroft		if ($line =~ m@/\*@) {
7574a0df2efSAndy Whitcroft			$in_comment = 1;
7584a0df2efSAndy Whitcroft		}
7594a0df2efSAndy Whitcroft		if (!$in_comment && $current_comment ne '') {
7604a0df2efSAndy Whitcroft			$current_comment = '';
7614a0df2efSAndy Whitcroft		}
7624a0df2efSAndy Whitcroft		$current_comment .= $line . "\n" if ($in_comment);
7634a0df2efSAndy Whitcroft		if ($line =~ m@\*/@) {
7644a0df2efSAndy Whitcroft			$in_comment = 0;
7654a0df2efSAndy Whitcroft		}
7664a0df2efSAndy Whitcroft	}
7674a0df2efSAndy Whitcroft
7684a0df2efSAndy Whitcroft	chomp($current_comment);
7694a0df2efSAndy Whitcroft	return($current_comment);
7704a0df2efSAndy Whitcroft}
7714a0df2efSAndy Whitcroftsub ctx_has_comment {
7724a0df2efSAndy Whitcroft	my ($first_line, $end_line) = @_;
7734a0df2efSAndy Whitcroft	my $cmt = ctx_locate_comment($first_line, $end_line);
7744a0df2efSAndy Whitcroft
77500df344fSAndy Whitcroft	##print "LINE: $rawlines[$end_line - 1 ]\n";
7764a0df2efSAndy Whitcroft	##print "CMMT: $cmt\n";
7774a0df2efSAndy Whitcroft
7784a0df2efSAndy Whitcroft	return ($cmt ne '');
7794a0df2efSAndy Whitcroft}
7804a0df2efSAndy Whitcroft
7814d001e4dSAndy Whitcroftsub raw_line {
7824d001e4dSAndy Whitcroft	my ($linenr, $cnt) = @_;
7834d001e4dSAndy Whitcroft
7844d001e4dSAndy Whitcroft	my $offset = $linenr - 1;
7854d001e4dSAndy Whitcroft	$cnt++;
7864d001e4dSAndy Whitcroft
7874d001e4dSAndy Whitcroft	my $line;
7884d001e4dSAndy Whitcroft	while ($cnt) {
7894d001e4dSAndy Whitcroft		$line = $rawlines[$offset++];
7904d001e4dSAndy Whitcroft		next if (defined($line) && $line =~ /^-/);
7914d001e4dSAndy Whitcroft		$cnt--;
7924d001e4dSAndy Whitcroft	}
7934d001e4dSAndy Whitcroft
7944d001e4dSAndy Whitcroft	return $line;
7954d001e4dSAndy Whitcroft}
7964d001e4dSAndy Whitcroft
7970a920b5bSAndy Whitcroftsub cat_vet {
7980a920b5bSAndy Whitcroft	my ($vet) = @_;
7999c0ca6f9SAndy Whitcroft	my ($res, $coded);
8000a920b5bSAndy Whitcroft
8019c0ca6f9SAndy Whitcroft	$res = '';
8026c72ffaaSAndy Whitcroft	while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
8036c72ffaaSAndy Whitcroft		$res .= $1;
8046c72ffaaSAndy Whitcroft		if ($2 ne '') {
8059c0ca6f9SAndy Whitcroft			$coded = sprintf("^%c", unpack('C', $2) + 64);
8066c72ffaaSAndy Whitcroft			$res .= $coded;
8076c72ffaaSAndy Whitcroft		}
8089c0ca6f9SAndy Whitcroft	}
8099c0ca6f9SAndy Whitcroft	$res =~ s/$/\$/;
8100a920b5bSAndy Whitcroft
8119c0ca6f9SAndy Whitcroft	return $res;
8120a920b5bSAndy Whitcroft}
8130a920b5bSAndy Whitcroft
814c2fdda0dSAndy Whitcroftmy $av_preprocessor = 0;
815cf655043SAndy Whitcroftmy $av_pending;
816c2fdda0dSAndy Whitcroftmy @av_paren_type;
8171f65f947SAndy Whitcroftmy $av_pend_colon;
818c2fdda0dSAndy Whitcroft
819c2fdda0dSAndy Whitcroftsub annotate_reset {
820c2fdda0dSAndy Whitcroft	$av_preprocessor = 0;
821cf655043SAndy Whitcroft	$av_pending = '_';
822cf655043SAndy Whitcroft	@av_paren_type = ('E');
8231f65f947SAndy Whitcroft	$av_pend_colon = 'O';
824c2fdda0dSAndy Whitcroft}
825c2fdda0dSAndy Whitcroft
8266c72ffaaSAndy Whitcroftsub annotate_values {
8276c72ffaaSAndy Whitcroft	my ($stream, $type) = @_;
8286c72ffaaSAndy Whitcroft
8296c72ffaaSAndy Whitcroft	my $res;
8301f65f947SAndy Whitcroft	my $var = '_' x length($stream);
8316c72ffaaSAndy Whitcroft	my $cur = $stream;
8326c72ffaaSAndy Whitcroft
833c2fdda0dSAndy Whitcroft	print "$stream\n" if ($dbg_values > 1);
8346c72ffaaSAndy Whitcroft
8356c72ffaaSAndy Whitcroft	while (length($cur)) {
836773647a0SAndy Whitcroft		@av_paren_type = ('E') if ($#av_paren_type < 0);
837cf655043SAndy Whitcroft		print " <" . join('', @av_paren_type) .
838171ae1a4SAndy Whitcroft				"> <$type> <$av_pending>" if ($dbg_values > 1);
8396c72ffaaSAndy Whitcroft		if ($cur =~ /^(\s+)/o) {
840c2fdda0dSAndy Whitcroft			print "WS($1)\n" if ($dbg_values > 1);
841c2fdda0dSAndy Whitcroft			if ($1 =~ /\n/ && $av_preprocessor) {
842cf655043SAndy Whitcroft				$type = pop(@av_paren_type);
843c2fdda0dSAndy Whitcroft				$av_preprocessor = 0;
8446c72ffaaSAndy Whitcroft			}
8456c72ffaaSAndy Whitcroft
8468ea3eb9aSAndy Whitcroft		} elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\()/) {
847c2fdda0dSAndy Whitcroft			print "DECLARE($1)\n" if ($dbg_values > 1);
8486c72ffaaSAndy Whitcroft			$type = 'T';
8496c72ffaaSAndy Whitcroft
850389a2fe5SAndy Whitcroft		} elsif ($cur =~ /^($Modifier)\s*/) {
851389a2fe5SAndy Whitcroft			print "MODIFIER($1)\n" if ($dbg_values > 1);
852389a2fe5SAndy Whitcroft			$type = 'T';
853389a2fe5SAndy Whitcroft
854c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
855171ae1a4SAndy Whitcroft			print "DEFINE($1,$2)\n" if ($dbg_values > 1);
856c2fdda0dSAndy Whitcroft			$av_preprocessor = 1;
857171ae1a4SAndy Whitcroft			push(@av_paren_type, $type);
858171ae1a4SAndy Whitcroft			if ($2 ne '') {
859cf655043SAndy Whitcroft				$av_pending = 'N';
860171ae1a4SAndy Whitcroft			}
861171ae1a4SAndy Whitcroft			$type = 'E';
862171ae1a4SAndy Whitcroft
863c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
864171ae1a4SAndy Whitcroft			print "UNDEF($1)\n" if ($dbg_values > 1);
865171ae1a4SAndy Whitcroft			$av_preprocessor = 1;
866171ae1a4SAndy Whitcroft			push(@av_paren_type, $type);
8676c72ffaaSAndy Whitcroft
868c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
869cf655043SAndy Whitcroft			print "PRE_START($1)\n" if ($dbg_values > 1);
870c2fdda0dSAndy Whitcroft			$av_preprocessor = 1;
871cf655043SAndy Whitcroft
872cf655043SAndy Whitcroft			push(@av_paren_type, $type);
873cf655043SAndy Whitcroft			push(@av_paren_type, $type);
874171ae1a4SAndy Whitcroft			$type = 'E';
875cf655043SAndy Whitcroft
876c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
877cf655043SAndy Whitcroft			print "PRE_RESTART($1)\n" if ($dbg_values > 1);
878cf655043SAndy Whitcroft			$av_preprocessor = 1;
879cf655043SAndy Whitcroft
880cf655043SAndy Whitcroft			push(@av_paren_type, $av_paren_type[$#av_paren_type]);
881cf655043SAndy Whitcroft
882171ae1a4SAndy Whitcroft			$type = 'E';
883cf655043SAndy Whitcroft
884c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:endif))/o) {
885cf655043SAndy Whitcroft			print "PRE_END($1)\n" if ($dbg_values > 1);
886cf655043SAndy Whitcroft
887cf655043SAndy Whitcroft			$av_preprocessor = 1;
888cf655043SAndy Whitcroft
889cf655043SAndy Whitcroft			# Assume all arms of the conditional end as this
890cf655043SAndy Whitcroft			# one does, and continue as if the #endif was not here.
891cf655043SAndy Whitcroft			pop(@av_paren_type);
892cf655043SAndy Whitcroft			push(@av_paren_type, $type);
893171ae1a4SAndy Whitcroft			$type = 'E';
8946c72ffaaSAndy Whitcroft
8956c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\\\n)/o) {
896c2fdda0dSAndy Whitcroft			print "PRECONT($1)\n" if ($dbg_values > 1);
8976c72ffaaSAndy Whitcroft
898171ae1a4SAndy Whitcroft		} elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
899171ae1a4SAndy Whitcroft			print "ATTR($1)\n" if ($dbg_values > 1);
900171ae1a4SAndy Whitcroft			$av_pending = $type;
901171ae1a4SAndy Whitcroft			$type = 'N';
902171ae1a4SAndy Whitcroft
9036c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
904c2fdda0dSAndy Whitcroft			print "SIZEOF($1)\n" if ($dbg_values > 1);
9056c72ffaaSAndy Whitcroft			if (defined $2) {
906cf655043SAndy Whitcroft				$av_pending = 'V';
9076c72ffaaSAndy Whitcroft			}
9086c72ffaaSAndy Whitcroft			$type = 'N';
9096c72ffaaSAndy Whitcroft
91014b111c1SAndy Whitcroft		} elsif ($cur =~ /^(if|while|for)\b/o) {
911c2fdda0dSAndy Whitcroft			print "COND($1)\n" if ($dbg_values > 1);
91214b111c1SAndy Whitcroft			$av_pending = 'E';
9136c72ffaaSAndy Whitcroft			$type = 'N';
9146c72ffaaSAndy Whitcroft
9151f65f947SAndy Whitcroft		} elsif ($cur =~/^(case)/o) {
9161f65f947SAndy Whitcroft			print "CASE($1)\n" if ($dbg_values > 1);
9171f65f947SAndy Whitcroft			$av_pend_colon = 'C';
9181f65f947SAndy Whitcroft			$type = 'N';
9191f65f947SAndy Whitcroft
92014b111c1SAndy Whitcroft		} elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
921c2fdda0dSAndy Whitcroft			print "KEYWORD($1)\n" if ($dbg_values > 1);
9226c72ffaaSAndy Whitcroft			$type = 'N';
9236c72ffaaSAndy Whitcroft
9246c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\()/o) {
925c2fdda0dSAndy Whitcroft			print "PAREN('$1')\n" if ($dbg_values > 1);
926cf655043SAndy Whitcroft			push(@av_paren_type, $av_pending);
927cf655043SAndy Whitcroft			$av_pending = '_';
9286c72ffaaSAndy Whitcroft			$type = 'N';
9296c72ffaaSAndy Whitcroft
9306c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\))/o) {
931cf655043SAndy Whitcroft			my $new_type = pop(@av_paren_type);
932cf655043SAndy Whitcroft			if ($new_type ne '_') {
933cf655043SAndy Whitcroft				$type = $new_type;
934c2fdda0dSAndy Whitcroft				print "PAREN('$1') -> $type\n"
935c2fdda0dSAndy Whitcroft							if ($dbg_values > 1);
9366c72ffaaSAndy Whitcroft			} else {
937c2fdda0dSAndy Whitcroft				print "PAREN('$1')\n" if ($dbg_values > 1);
9386c72ffaaSAndy Whitcroft			}
9396c72ffaaSAndy Whitcroft
940c8cb2ca3SAndy Whitcroft		} elsif ($cur =~ /^($Ident)\s*\(/o) {
941c2fdda0dSAndy Whitcroft			print "FUNC($1)\n" if ($dbg_values > 1);
942c8cb2ca3SAndy Whitcroft			$type = 'V';
943cf655043SAndy Whitcroft			$av_pending = 'V';
9446c72ffaaSAndy Whitcroft
9458e761b04SAndy Whitcroft		} elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
9468e761b04SAndy Whitcroft			if (defined $2 && $type eq 'C' || $type eq 'T') {
9471f65f947SAndy Whitcroft				$av_pend_colon = 'B';
9488e761b04SAndy Whitcroft			} elsif ($type eq 'E') {
9498e761b04SAndy Whitcroft				$av_pend_colon = 'L';
9501f65f947SAndy Whitcroft			}
9511f65f947SAndy Whitcroft			print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
9521f65f947SAndy Whitcroft			$type = 'V';
9531f65f947SAndy Whitcroft
9546c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Ident|$Constant)/o) {
955c2fdda0dSAndy Whitcroft			print "IDENT($1)\n" if ($dbg_values > 1);
9566c72ffaaSAndy Whitcroft			$type = 'V';
9576c72ffaaSAndy Whitcroft
9586c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Assignment)/o) {
959c2fdda0dSAndy Whitcroft			print "ASSIGN($1)\n" if ($dbg_values > 1);
9606c72ffaaSAndy Whitcroft			$type = 'N';
9616c72ffaaSAndy Whitcroft
962cf655043SAndy Whitcroft		} elsif ($cur =~/^(;|{|})/) {
963c2fdda0dSAndy Whitcroft			print "END($1)\n" if ($dbg_values > 1);
96413214adfSAndy Whitcroft			$type = 'E';
9651f65f947SAndy Whitcroft			$av_pend_colon = 'O';
96613214adfSAndy Whitcroft
9678e761b04SAndy Whitcroft		} elsif ($cur =~/^(,)/) {
9688e761b04SAndy Whitcroft			print "COMMA($1)\n" if ($dbg_values > 1);
9698e761b04SAndy Whitcroft			$type = 'C';
9708e761b04SAndy Whitcroft
9711f65f947SAndy Whitcroft		} elsif ($cur =~ /^(\?)/o) {
9721f65f947SAndy Whitcroft			print "QUESTION($1)\n" if ($dbg_values > 1);
9731f65f947SAndy Whitcroft			$type = 'N';
9741f65f947SAndy Whitcroft
9751f65f947SAndy Whitcroft		} elsif ($cur =~ /^(:)/o) {
9761f65f947SAndy Whitcroft			print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
9771f65f947SAndy Whitcroft
9781f65f947SAndy Whitcroft			substr($var, length($res), 1, $av_pend_colon);
9791f65f947SAndy Whitcroft			if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
9801f65f947SAndy Whitcroft				$type = 'E';
9811f65f947SAndy Whitcroft			} else {
9821f65f947SAndy Whitcroft				$type = 'N';
9831f65f947SAndy Whitcroft			}
9841f65f947SAndy Whitcroft			$av_pend_colon = 'O';
9851f65f947SAndy Whitcroft
9868e761b04SAndy Whitcroft		} elsif ($cur =~ /^(\[)/o) {
98713214adfSAndy Whitcroft			print "CLOSE($1)\n" if ($dbg_values > 1);
9886c72ffaaSAndy Whitcroft			$type = 'N';
9896c72ffaaSAndy Whitcroft
9900d413866SAndy Whitcroft		} elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
99174048ed8SAndy Whitcroft			my $variant;
99274048ed8SAndy Whitcroft
99374048ed8SAndy Whitcroft			print "OPV($1)\n" if ($dbg_values > 1);
99474048ed8SAndy Whitcroft			if ($type eq 'V') {
99574048ed8SAndy Whitcroft				$variant = 'B';
99674048ed8SAndy Whitcroft			} else {
99774048ed8SAndy Whitcroft				$variant = 'U';
99874048ed8SAndy Whitcroft			}
99974048ed8SAndy Whitcroft
100074048ed8SAndy Whitcroft			substr($var, length($res), 1, $variant);
100174048ed8SAndy Whitcroft			$type = 'N';
100274048ed8SAndy Whitcroft
10036c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Operators)/o) {
1004c2fdda0dSAndy Whitcroft			print "OP($1)\n" if ($dbg_values > 1);
10056c72ffaaSAndy Whitcroft			if ($1 ne '++' && $1 ne '--') {
10066c72ffaaSAndy Whitcroft				$type = 'N';
10076c72ffaaSAndy Whitcroft			}
10086c72ffaaSAndy Whitcroft
10096c72ffaaSAndy Whitcroft		} elsif ($cur =~ /(^.)/o) {
1010c2fdda0dSAndy Whitcroft			print "C($1)\n" if ($dbg_values > 1);
10116c72ffaaSAndy Whitcroft		}
10126c72ffaaSAndy Whitcroft		if (defined $1) {
10136c72ffaaSAndy Whitcroft			$cur = substr($cur, length($1));
10146c72ffaaSAndy Whitcroft			$res .= $type x length($1);
10156c72ffaaSAndy Whitcroft		}
10166c72ffaaSAndy Whitcroft	}
10176c72ffaaSAndy Whitcroft
10181f65f947SAndy Whitcroft	return ($res, $var);
10196c72ffaaSAndy Whitcroft}
10206c72ffaaSAndy Whitcroft
10218905a67cSAndy Whitcroftsub possible {
102213214adfSAndy Whitcroft	my ($possible, $line) = @_;
10239a974fdbSAndy Whitcroft	my $notPermitted = qr{(?:
10240776e594SAndy Whitcroft		^(?:
10250776e594SAndy Whitcroft			$Modifier|
10260776e594SAndy Whitcroft			$Storage|
10270776e594SAndy Whitcroft			$Type|
10289a974fdbSAndy Whitcroft			DEFINE_\S+
10299a974fdbSAndy Whitcroft		)$|
10309a974fdbSAndy Whitcroft		^(?:
10310776e594SAndy Whitcroft			goto|
10320776e594SAndy Whitcroft			return|
10330776e594SAndy Whitcroft			case|
10340776e594SAndy Whitcroft			else|
10350776e594SAndy Whitcroft			asm|__asm__|
10360776e594SAndy Whitcroft			do
10379a974fdbSAndy Whitcroft		)(?:\s|$)|
10380776e594SAndy Whitcroft		^(?:typedef|struct|enum)\b
10399a974fdbSAndy Whitcroft	    )}x;
10409a974fdbSAndy Whitcroft	warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
10419a974fdbSAndy Whitcroft	if ($possible !~ $notPermitted) {
1042c45dcabdSAndy Whitcroft		# Check for modifiers.
1043c45dcabdSAndy Whitcroft		$possible =~ s/\s*$Storage\s*//g;
1044c45dcabdSAndy Whitcroft		$possible =~ s/\s*$Sparse\s*//g;
1045c45dcabdSAndy Whitcroft		if ($possible =~ /^\s*$/) {
1046c45dcabdSAndy Whitcroft
1047c45dcabdSAndy Whitcroft		} elsif ($possible =~ /\s/) {
1048c45dcabdSAndy Whitcroft			$possible =~ s/\s*$Type\s*//g;
1049d2506586SAndy Whitcroft			for my $modifier (split(' ', $possible)) {
10509a974fdbSAndy Whitcroft				if ($modifier !~ $notPermitted) {
1051d2506586SAndy Whitcroft					warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1052d2506586SAndy Whitcroft					push(@modifierList, $modifier);
1053d2506586SAndy Whitcroft				}
10549a974fdbSAndy Whitcroft			}
1055c45dcabdSAndy Whitcroft
1056c45dcabdSAndy Whitcroft		} else {
105713214adfSAndy Whitcroft			warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
10588905a67cSAndy Whitcroft			push(@typeList, $possible);
1059c45dcabdSAndy Whitcroft		}
10608905a67cSAndy Whitcroft		build_types();
10610776e594SAndy Whitcroft	} else {
10620776e594SAndy Whitcroft		warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
10638905a67cSAndy Whitcroft	}
10648905a67cSAndy Whitcroft}
10658905a67cSAndy Whitcroft
10666c72ffaaSAndy Whitcroftmy $prefix = '';
10676c72ffaaSAndy Whitcroft
1068f0a594c1SAndy Whitcroftsub report {
1069773647a0SAndy Whitcroft	if (defined $tst_only && $_[0] !~ /\Q$tst_only\E/) {
1070773647a0SAndy Whitcroft		return 0;
1071773647a0SAndy Whitcroft	}
10728905a67cSAndy Whitcroft	my $line = $prefix . $_[0];
10738905a67cSAndy Whitcroft
10748905a67cSAndy Whitcroft	$line = (split('\n', $line))[0] . "\n" if ($terse);
10758905a67cSAndy Whitcroft
107613214adfSAndy Whitcroft	push(our @report, $line);
1077773647a0SAndy Whitcroft
1078773647a0SAndy Whitcroft	return 1;
1079f0a594c1SAndy Whitcroft}
1080f0a594c1SAndy Whitcroftsub report_dump {
108113214adfSAndy Whitcroft	our @report;
1082f0a594c1SAndy Whitcroft}
1083de7d4f0eSAndy Whitcroftsub ERROR {
1084773647a0SAndy Whitcroft	if (report("ERROR: $_[0]\n")) {
1085de7d4f0eSAndy Whitcroft		our $clean = 0;
10866c72ffaaSAndy Whitcroft		our $cnt_error++;
1087de7d4f0eSAndy Whitcroft	}
1088773647a0SAndy Whitcroft}
1089de7d4f0eSAndy Whitcroftsub WARN {
1090773647a0SAndy Whitcroft	if (report("WARNING: $_[0]\n")) {
1091de7d4f0eSAndy Whitcroft		our $clean = 0;
10926c72ffaaSAndy Whitcroft		our $cnt_warn++;
1093de7d4f0eSAndy Whitcroft	}
1094773647a0SAndy Whitcroft}
1095de7d4f0eSAndy Whitcroftsub CHK {
1096773647a0SAndy Whitcroft	if ($check && report("CHECK: $_[0]\n")) {
1097de7d4f0eSAndy Whitcroft		our $clean = 0;
10986c72ffaaSAndy Whitcroft		our $cnt_chk++;
10996c72ffaaSAndy Whitcroft	}
1100de7d4f0eSAndy Whitcroft}
1101de7d4f0eSAndy Whitcroft
11026ecd9674SAndy Whitcroftsub check_absolute_file {
11036ecd9674SAndy Whitcroft	my ($absolute, $herecurr) = @_;
11046ecd9674SAndy Whitcroft	my $file = $absolute;
11056ecd9674SAndy Whitcroft
11066ecd9674SAndy Whitcroft	##print "absolute<$absolute>\n";
11076ecd9674SAndy Whitcroft
11086ecd9674SAndy Whitcroft	# See if any suffix of this path is a path within the tree.
11096ecd9674SAndy Whitcroft	while ($file =~ s@^[^/]*/@@) {
11106ecd9674SAndy Whitcroft		if (-f "$root/$file") {
11116ecd9674SAndy Whitcroft			##print "file<$file>\n";
11126ecd9674SAndy Whitcroft			last;
11136ecd9674SAndy Whitcroft		}
11146ecd9674SAndy Whitcroft	}
11156ecd9674SAndy Whitcroft	if (! -f _)  {
11166ecd9674SAndy Whitcroft		return 0;
11176ecd9674SAndy Whitcroft	}
11186ecd9674SAndy Whitcroft
11196ecd9674SAndy Whitcroft	# It is, so see if the prefix is acceptable.
11206ecd9674SAndy Whitcroft	my $prefix = $absolute;
11216ecd9674SAndy Whitcroft	substr($prefix, -length($file)) = '';
11226ecd9674SAndy Whitcroft
11236ecd9674SAndy Whitcroft	##print "prefix<$prefix>\n";
11246ecd9674SAndy Whitcroft	if ($prefix ne ".../") {
11256ecd9674SAndy Whitcroft		WARN("use relative pathname instead of absolute in changelog text\n" . $herecurr);
11266ecd9674SAndy Whitcroft	}
11276ecd9674SAndy Whitcroft}
11286ecd9674SAndy Whitcroft
11290a920b5bSAndy Whitcroftsub process {
11300a920b5bSAndy Whitcroft	my $filename = shift;
11310a920b5bSAndy Whitcroft
11320a920b5bSAndy Whitcroft	my $linenr=0;
11330a920b5bSAndy Whitcroft	my $prevline="";
1134c2fdda0dSAndy Whitcroft	my $prevrawline="";
11350a920b5bSAndy Whitcroft	my $stashline="";
1136c2fdda0dSAndy Whitcroft	my $stashrawline="";
11370a920b5bSAndy Whitcroft
11384a0df2efSAndy Whitcroft	my $length;
11390a920b5bSAndy Whitcroft	my $indent;
11400a920b5bSAndy Whitcroft	my $previndent=0;
11410a920b5bSAndy Whitcroft	my $stashindent=0;
11420a920b5bSAndy Whitcroft
1143de7d4f0eSAndy Whitcroft	our $clean = 1;
11440a920b5bSAndy Whitcroft	my $signoff = 0;
11450a920b5bSAndy Whitcroft	my $is_patch = 0;
11460a920b5bSAndy Whitcroft
114713214adfSAndy Whitcroft	our @report = ();
11486c72ffaaSAndy Whitcroft	our $cnt_lines = 0;
11496c72ffaaSAndy Whitcroft	our $cnt_error = 0;
11506c72ffaaSAndy Whitcroft	our $cnt_warn = 0;
11516c72ffaaSAndy Whitcroft	our $cnt_chk = 0;
11526c72ffaaSAndy Whitcroft
11530a920b5bSAndy Whitcroft	# Trace the real file/line as we go.
11540a920b5bSAndy Whitcroft	my $realfile = '';
11550a920b5bSAndy Whitcroft	my $realline = 0;
11560a920b5bSAndy Whitcroft	my $realcnt = 0;
11570a920b5bSAndy Whitcroft	my $here = '';
11580a920b5bSAndy Whitcroft	my $in_comment = 0;
1159c2fdda0dSAndy Whitcroft	my $comment_edge = 0;
11600a920b5bSAndy Whitcroft	my $first_line = 0;
11611e855726SWolfram Sang	my $p1_prefix = '';
11620a920b5bSAndy Whitcroft
116313214adfSAndy Whitcroft	my $prev_values = 'E';
116413214adfSAndy Whitcroft
116513214adfSAndy Whitcroft	# suppression flags
1166773647a0SAndy Whitcroft	my %suppress_ifbraces;
1167170d3a22SAndy Whitcroft	my %suppress_whiletrailers;
11682b474a1aSAndy Whitcroft	my %suppress_export;
1169653d4876SAndy Whitcroft
1170c2fdda0dSAndy Whitcroft	# Pre-scan the patch sanitizing the lines.
1171de7d4f0eSAndy Whitcroft	# Pre-scan the patch looking for any __setup documentation.
1172c2fdda0dSAndy Whitcroft	#
1173de7d4f0eSAndy Whitcroft	my @setup_docs = ();
1174de7d4f0eSAndy Whitcroft	my $setup_docs = 0;
1175773647a0SAndy Whitcroft
1176773647a0SAndy Whitcroft	sanitise_line_reset();
1177c2fdda0dSAndy Whitcroft	my $line;
1178c2fdda0dSAndy Whitcroft	foreach my $rawline (@rawlines) {
1179773647a0SAndy Whitcroft		$linenr++;
1180773647a0SAndy Whitcroft		$line = $rawline;
1181c2fdda0dSAndy Whitcroft
1182773647a0SAndy Whitcroft		if ($rawline=~/^\+\+\+\s+(\S+)/) {
1183de7d4f0eSAndy Whitcroft			$setup_docs = 0;
1184de7d4f0eSAndy Whitcroft			if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
1185de7d4f0eSAndy Whitcroft				$setup_docs = 1;
1186de7d4f0eSAndy Whitcroft			}
1187773647a0SAndy Whitcroft			#next;
1188de7d4f0eSAndy Whitcroft		}
1189773647a0SAndy Whitcroft		if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1190773647a0SAndy Whitcroft			$realline=$1-1;
1191773647a0SAndy Whitcroft			if (defined $2) {
1192773647a0SAndy Whitcroft				$realcnt=$3+1;
1193773647a0SAndy Whitcroft			} else {
1194773647a0SAndy Whitcroft				$realcnt=1+1;
1195773647a0SAndy Whitcroft			}
1196c45dcabdSAndy Whitcroft			$in_comment = 0;
1197773647a0SAndy Whitcroft
1198773647a0SAndy Whitcroft			# Guestimate if this is a continuing comment.  Run
1199773647a0SAndy Whitcroft			# the context looking for a comment "edge".  If this
1200773647a0SAndy Whitcroft			# edge is a close comment then we must be in a comment
1201773647a0SAndy Whitcroft			# at context start.
1202773647a0SAndy Whitcroft			my $edge;
120301fa9147SAndy Whitcroft			my $cnt = $realcnt;
120401fa9147SAndy Whitcroft			for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
120501fa9147SAndy Whitcroft				next if (defined $rawlines[$ln - 1] &&
120601fa9147SAndy Whitcroft					 $rawlines[$ln - 1] =~ /^-/);
120701fa9147SAndy Whitcroft				$cnt--;
120801fa9147SAndy Whitcroft				#print "RAW<$rawlines[$ln - 1]>\n";
1209721c1cb6SAndy Whitcroft				last if (!defined $rawlines[$ln - 1]);
1210fae17daeSAndy Whitcroft				if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
1211fae17daeSAndy Whitcroft				    $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
1212fae17daeSAndy Whitcroft					($edge) = $1;
1213fae17daeSAndy Whitcroft					last;
1214fae17daeSAndy Whitcroft				}
1215773647a0SAndy Whitcroft			}
1216773647a0SAndy Whitcroft			if (defined $edge && $edge eq '*/') {
1217773647a0SAndy Whitcroft				$in_comment = 1;
1218773647a0SAndy Whitcroft			}
1219773647a0SAndy Whitcroft
1220773647a0SAndy Whitcroft			# Guestimate if this is a continuing comment.  If this
1221773647a0SAndy Whitcroft			# is the start of a diff block and this line starts
1222773647a0SAndy Whitcroft			# ' *' then it is very likely a comment.
1223773647a0SAndy Whitcroft			if (!defined $edge &&
122483242e0cSAndy Whitcroft			    $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
1225773647a0SAndy Whitcroft			{
1226773647a0SAndy Whitcroft				$in_comment = 1;
1227773647a0SAndy Whitcroft			}
1228773647a0SAndy Whitcroft
1229773647a0SAndy Whitcroft			##print "COMMENT:$in_comment edge<$edge> $rawline\n";
1230773647a0SAndy Whitcroft			sanitise_line_reset($in_comment);
1231773647a0SAndy Whitcroft
1232171ae1a4SAndy Whitcroft		} elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
1233773647a0SAndy Whitcroft			# Standardise the strings and chars within the input to
1234171ae1a4SAndy Whitcroft			# simplify matching -- only bother with positive lines.
1235773647a0SAndy Whitcroft			$line = sanitise_line($rawline);
1236773647a0SAndy Whitcroft		}
1237773647a0SAndy Whitcroft		push(@lines, $line);
1238773647a0SAndy Whitcroft
1239773647a0SAndy Whitcroft		if ($realcnt > 1) {
1240773647a0SAndy Whitcroft			$realcnt-- if ($line =~ /^(?:\+| |$)/);
1241773647a0SAndy Whitcroft		} else {
1242773647a0SAndy Whitcroft			$realcnt = 0;
1243773647a0SAndy Whitcroft		}
1244773647a0SAndy Whitcroft
1245773647a0SAndy Whitcroft		#print "==>$rawline\n";
1246773647a0SAndy Whitcroft		#print "-->$line\n";
1247de7d4f0eSAndy Whitcroft
1248de7d4f0eSAndy Whitcroft		if ($setup_docs && $line =~ /^\+/) {
1249de7d4f0eSAndy Whitcroft			push(@setup_docs, $line);
1250de7d4f0eSAndy Whitcroft		}
1251de7d4f0eSAndy Whitcroft	}
1252de7d4f0eSAndy Whitcroft
12536c72ffaaSAndy Whitcroft	$prefix = '';
12546c72ffaaSAndy Whitcroft
1255773647a0SAndy Whitcroft	$realcnt = 0;
1256773647a0SAndy Whitcroft	$linenr = 0;
12570a920b5bSAndy Whitcroft	foreach my $line (@lines) {
12580a920b5bSAndy Whitcroft		$linenr++;
12590a920b5bSAndy Whitcroft
1260c2fdda0dSAndy Whitcroft		my $rawline = $rawlines[$linenr - 1];
12616c72ffaaSAndy Whitcroft
12620a920b5bSAndy Whitcroft#extract the line range in the file after the patch is applied
12636c72ffaaSAndy Whitcroft		if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
12640a920b5bSAndy Whitcroft			$is_patch = 1;
12654a0df2efSAndy Whitcroft			$first_line = $linenr + 1;
12660a920b5bSAndy Whitcroft			$realline=$1-1;
12670a920b5bSAndy Whitcroft			if (defined $2) {
12680a920b5bSAndy Whitcroft				$realcnt=$3+1;
12690a920b5bSAndy Whitcroft			} else {
12700a920b5bSAndy Whitcroft				$realcnt=1+1;
12710a920b5bSAndy Whitcroft			}
1272c2fdda0dSAndy Whitcroft			annotate_reset();
127313214adfSAndy Whitcroft			$prev_values = 'E';
127413214adfSAndy Whitcroft
1275773647a0SAndy Whitcroft			%suppress_ifbraces = ();
1276170d3a22SAndy Whitcroft			%suppress_whiletrailers = ();
12772b474a1aSAndy Whitcroft			%suppress_export = ();
12780a920b5bSAndy Whitcroft			next;
12790a920b5bSAndy Whitcroft
12804a0df2efSAndy Whitcroft# track the line number as we move through the hunk, note that
12814a0df2efSAndy Whitcroft# new versions of GNU diff omit the leading space on completely
12824a0df2efSAndy Whitcroft# blank context lines so we need to count that too.
1283773647a0SAndy Whitcroft		} elsif ($line =~ /^( |\+|$)/) {
12840a920b5bSAndy Whitcroft			$realline++;
1285d8aaf121SAndy Whitcroft			$realcnt-- if ($realcnt != 0);
12860a920b5bSAndy Whitcroft
12874a0df2efSAndy Whitcroft			# Measure the line length and indent.
1288c2fdda0dSAndy Whitcroft			($length, $indent) = line_stats($rawline);
12890a920b5bSAndy Whitcroft
12900a920b5bSAndy Whitcroft			# Track the previous line.
12910a920b5bSAndy Whitcroft			($prevline, $stashline) = ($stashline, $line);
12920a920b5bSAndy Whitcroft			($previndent, $stashindent) = ($stashindent, $indent);
1293c2fdda0dSAndy Whitcroft			($prevrawline, $stashrawline) = ($stashrawline, $rawline);
1294c2fdda0dSAndy Whitcroft
1295773647a0SAndy Whitcroft			#warn "line<$line>\n";
12966c72ffaaSAndy Whitcroft
1297d8aaf121SAndy Whitcroft		} elsif ($realcnt == 1) {
1298d8aaf121SAndy Whitcroft			$realcnt--;
12990a920b5bSAndy Whitcroft		}
13000a920b5bSAndy Whitcroft
1301cc77cdcaSAndy Whitcroft		my $hunk_line = ($realcnt != 0);
1302cc77cdcaSAndy Whitcroft
13030a920b5bSAndy Whitcroft#make up the handle for any error we report on this line
1304773647a0SAndy Whitcroft		$prefix = "$filename:$realline: " if ($emacs && $file);
1305773647a0SAndy Whitcroft		$prefix = "$filename:$linenr: " if ($emacs && !$file);
1306773647a0SAndy Whitcroft
13076c72ffaaSAndy Whitcroft		$here = "#$linenr: " if (!$file);
13086c72ffaaSAndy Whitcroft		$here = "#$realline: " if ($file);
1309773647a0SAndy Whitcroft
1310773647a0SAndy Whitcroft		# extract the filename as it passes
1311773647a0SAndy Whitcroft		if ($line=~/^\+\+\+\s+(\S+)/) {
1312773647a0SAndy Whitcroft			$realfile = $1;
13131e855726SWolfram Sang			$realfile =~ s@^([^/]*)/@@;
13141e855726SWolfram Sang
13151e855726SWolfram Sang			$p1_prefix = $1;
1316e2f7aa4bSAndy Whitcroft			if (!$file && $tree && $p1_prefix ne '' &&
1317e2f7aa4bSAndy Whitcroft			    -e "$root/$p1_prefix") {
13181e855726SWolfram Sang				WARN("patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
13191e855726SWolfram Sang			}
1320773647a0SAndy Whitcroft
1321c1ab3326SAndy Whitcroft			if ($realfile =~ m@^include/asm/@) {
1322773647a0SAndy Whitcroft				ERROR("do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
1323773647a0SAndy Whitcroft			}
1324773647a0SAndy Whitcroft			next;
1325773647a0SAndy Whitcroft		}
1326773647a0SAndy Whitcroft
1327389834b6SRandy Dunlap		$here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
13280a920b5bSAndy Whitcroft
1329c2fdda0dSAndy Whitcroft		my $hereline = "$here\n$rawline\n";
1330c2fdda0dSAndy Whitcroft		my $herecurr = "$here\n$rawline\n";
1331c2fdda0dSAndy Whitcroft		my $hereprev = "$here\n$prevrawline\n$rawline\n";
13320a920b5bSAndy Whitcroft
13336c72ffaaSAndy Whitcroft		$cnt_lines++ if ($realcnt != 0);
13346c72ffaaSAndy Whitcroft
13350a920b5bSAndy Whitcroft#check the patch for a signoff:
1336d8aaf121SAndy Whitcroft		if ($line =~ /^\s*signed-off-by:/i) {
13374a0df2efSAndy Whitcroft			# This is a signoff, if ugly, so do not double report.
13384a0df2efSAndy Whitcroft			$signoff++;
13390a920b5bSAndy Whitcroft			if (!($line =~ /^\s*Signed-off-by:/)) {
1340de7d4f0eSAndy Whitcroft				WARN("Signed-off-by: is the preferred form\n" .
1341de7d4f0eSAndy Whitcroft					$herecurr);
13420a920b5bSAndy Whitcroft			}
13430a920b5bSAndy Whitcroft			if ($line =~ /^\s*signed-off-by:\S/i) {
1344773647a0SAndy Whitcroft				WARN("space required after Signed-off-by:\n" .
1345de7d4f0eSAndy Whitcroft					$herecurr);
13460a920b5bSAndy Whitcroft			}
13470a920b5bSAndy Whitcroft		}
13480a920b5bSAndy Whitcroft
134900df344fSAndy Whitcroft# Check for wrappage within a valid hunk of the file
13508905a67cSAndy Whitcroft		if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
1351de7d4f0eSAndy Whitcroft			ERROR("patch seems to be corrupt (line wrapped?)\n" .
13526c72ffaaSAndy Whitcroft				$herecurr) if (!$emitted_corrupt++);
1353de7d4f0eSAndy Whitcroft		}
1354de7d4f0eSAndy Whitcroft
13556ecd9674SAndy Whitcroft# Check for absolute kernel paths.
13566ecd9674SAndy Whitcroft		if ($tree) {
13576ecd9674SAndy Whitcroft			while ($line =~ m{(?:^|\s)(/\S*)}g) {
13586ecd9674SAndy Whitcroft				my $file = $1;
13596ecd9674SAndy Whitcroft
13606ecd9674SAndy Whitcroft				if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
13616ecd9674SAndy Whitcroft				    check_absolute_file($1, $herecurr)) {
13626ecd9674SAndy Whitcroft					#
13636ecd9674SAndy Whitcroft				} else {
13646ecd9674SAndy Whitcroft					check_absolute_file($file, $herecurr);
13656ecd9674SAndy Whitcroft				}
13666ecd9674SAndy Whitcroft			}
13676ecd9674SAndy Whitcroft		}
13686ecd9674SAndy Whitcroft
1369de7d4f0eSAndy Whitcroft# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
1370de7d4f0eSAndy Whitcroft		if (($realfile =~ /^$/ || $line =~ /^\+/) &&
1371171ae1a4SAndy Whitcroft		    $rawline !~ m/^$UTF8*$/) {
1372171ae1a4SAndy Whitcroft			my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
1373171ae1a4SAndy Whitcroft
1374171ae1a4SAndy Whitcroft			my $blank = copy_spacing($rawline);
1375171ae1a4SAndy Whitcroft			my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
1376171ae1a4SAndy Whitcroft			my $hereptr = "$hereline$ptr\n";
1377171ae1a4SAndy Whitcroft
1378171ae1a4SAndy Whitcroft			ERROR("Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
137900df344fSAndy Whitcroft		}
13800a920b5bSAndy Whitcroft
138130670854SAndy Whitcroft# ignore non-hunk lines and lines being removed
138230670854SAndy Whitcroft		next if (!$hunk_line || $line =~ /^-/);
138300df344fSAndy Whitcroft
13840a920b5bSAndy Whitcroft#trailing whitespace
13859c0ca6f9SAndy Whitcroft		if ($line =~ /^\+.*\015/) {
1386c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
13879c0ca6f9SAndy Whitcroft			ERROR("DOS line endings\n" . $herevet);
13889c0ca6f9SAndy Whitcroft
1389c2fdda0dSAndy Whitcroft		} elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
1390c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1391de7d4f0eSAndy Whitcroft			ERROR("trailing whitespace\n" . $herevet);
13920a920b5bSAndy Whitcroft		}
13935368df20SAndy Whitcroft
13943354957aSAndi Kleen# check for Kconfig help text having a real description
13953354957aSAndi Kleen		if ($realfile =~ /Kconfig/ &&
13963354957aSAndi Kleen		    $line =~ /\+?\s*(---)?help(---)?$/) {
13973354957aSAndi Kleen			my $length = 0;
13983354957aSAndi Kleen			for (my $l = $linenr; defined($lines[$l]); $l++) {
13993354957aSAndi Kleen				my $f = $lines[$l];
14003354957aSAndi Kleen				$f =~ s/#.*//;
14013354957aSAndi Kleen				$f =~ s/^\s+//;
14023354957aSAndi Kleen				next if ($f =~ /^$/);
14033354957aSAndi Kleen				last if ($f =~ /^\s*config\s/);
14043354957aSAndi Kleen				$length++;
14053354957aSAndi Kleen			}
14063354957aSAndi Kleen			WARN("please write a paragraph that describes the config symbol fully\n" . $herecurr) if ($length < 4);
14073354957aSAndi Kleen		}
14083354957aSAndi Kleen
14095368df20SAndy Whitcroft# check we are in a valid source file if not then ignore this hunk
14105368df20SAndy Whitcroft		next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
14115368df20SAndy Whitcroft
14120a920b5bSAndy Whitcroft#80 column limit
1413c45dcabdSAndy Whitcroft		if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ &&
1414f4c014c0SAndy Whitcroft		    $rawline !~ /^.\s*\*\s*\@$Ident\s/ &&
14158bbea968SJoe Perches		    !($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(KERN_\S+\s*|[^"]*))?"[X\t]*"\s*(?:,|\)\s*;)\s*$/ ||
14168bbea968SJoe Perches		    $line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) &&
1417f4c014c0SAndy Whitcroft		    $length > 80)
1418c45dcabdSAndy Whitcroft		{
1419de7d4f0eSAndy Whitcroft			WARN("line over 80 characters\n" . $herecurr);
14200a920b5bSAndy Whitcroft		}
14210a920b5bSAndy Whitcroft
14225e79d96eSJoe Perches# check for spaces before a quoted newline
14235e79d96eSJoe Perches		if ($rawline =~ /^.*\".*\s\\n/) {
14245e79d96eSJoe Perches			WARN("unnecessary whitespace before a quoted newline\n" . $herecurr);
14255e79d96eSJoe Perches		}
14265e79d96eSJoe Perches
14278905a67cSAndy Whitcroft# check for adding lines without a newline.
14288905a67cSAndy Whitcroft		if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
14298905a67cSAndy Whitcroft			WARN("adding a line without newline at end of file\n" . $herecurr);
14308905a67cSAndy Whitcroft		}
14318905a67cSAndy Whitcroft
143242e41c54SMike Frysinger# Blackfin: use hi/lo macros
143342e41c54SMike Frysinger		if ($realfile =~ m@arch/blackfin/.*\.S$@) {
143442e41c54SMike Frysinger			if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
143542e41c54SMike Frysinger				my $herevet = "$here\n" . cat_vet($line) . "\n";
143642e41c54SMike Frysinger				ERROR("use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
143742e41c54SMike Frysinger			}
143842e41c54SMike Frysinger			if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
143942e41c54SMike Frysinger				my $herevet = "$here\n" . cat_vet($line) . "\n";
144042e41c54SMike Frysinger				ERROR("use the HI() macro, not (... >> 16)\n" . $herevet);
144142e41c54SMike Frysinger			}
144242e41c54SMike Frysinger		}
144342e41c54SMike Frysinger
1444b9ea10d6SAndy Whitcroft# check we are in a valid source file C or perl if not then ignore this hunk
1445b9ea10d6SAndy Whitcroft		next if ($realfile !~ /\.(h|c|pl)$/);
14460a920b5bSAndy Whitcroft
14470a920b5bSAndy Whitcroft# at the beginning of a line any tabs must come first and anything
14480a920b5bSAndy Whitcroft# more than 8 must use tabs.
1449c2fdda0dSAndy Whitcroft		if ($rawline =~ /^\+\s* \t\s*\S/ ||
1450c2fdda0dSAndy Whitcroft		    $rawline =~ /^\+\s*        \s*/) {
1451c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1452171ae1a4SAndy Whitcroft			ERROR("code indent should use tabs where possible\n" . $herevet);
14530a920b5bSAndy Whitcroft		}
14540a920b5bSAndy Whitcroft
145508e44365SAlberto Panizzo# check for space before tabs.
145608e44365SAlberto Panizzo		if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
145708e44365SAlberto Panizzo			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
145808e44365SAlberto Panizzo			WARN("please, no space before tabs\n" . $herevet);
145908e44365SAlberto Panizzo		}
146008e44365SAlberto Panizzo
14615f7ddae6SRaffaele Recalcati# check for spaces at the beginning of a line.
14625f7ddae6SRaffaele Recalcati		if ($rawline =~ /^\+ / && $rawline !~ /\+ +\*/)  {
14635f7ddae6SRaffaele Recalcati			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
14645f7ddae6SRaffaele Recalcati			WARN("please, no space for starting a line, \
14655f7ddae6SRaffaele Recalcati				excluding comments\n" . $herevet);
14665f7ddae6SRaffaele Recalcati		}
14675f7ddae6SRaffaele Recalcati
1468b9ea10d6SAndy Whitcroft# check we are in a valid C source file if not then ignore this hunk
1469b9ea10d6SAndy Whitcroft		next if ($realfile !~ /\.(h|c)$/);
1470b9ea10d6SAndy Whitcroft
1471c2fdda0dSAndy Whitcroft# check for RCS/CVS revision markers
1472cf655043SAndy Whitcroft		if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
1473c2fdda0dSAndy Whitcroft			WARN("CVS style keyword markers, these will _not_ be updated\n". $herecurr);
1474c2fdda0dSAndy Whitcroft		}
147522f2a2efSAndy Whitcroft
147642e41c54SMike Frysinger# Blackfin: don't use __builtin_bfin_[cs]sync
147742e41c54SMike Frysinger		if ($line =~ /__builtin_bfin_csync/) {
147842e41c54SMike Frysinger			my $herevet = "$here\n" . cat_vet($line) . "\n";
147942e41c54SMike Frysinger			ERROR("use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
148042e41c54SMike Frysinger		}
148142e41c54SMike Frysinger		if ($line =~ /__builtin_bfin_ssync/) {
148242e41c54SMike Frysinger			my $herevet = "$here\n" . cat_vet($line) . "\n";
148342e41c54SMike Frysinger			ERROR("use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
148442e41c54SMike Frysinger		}
148542e41c54SMike Frysinger
14869c0ca6f9SAndy Whitcroft# Check for potential 'bare' types
14872b474a1aSAndy Whitcroft		my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
14882b474a1aSAndy Whitcroft		    $realline_next);
14899c9ba34eSAndy Whitcroft		if ($realcnt && $line =~ /.\s*\S/) {
1490170d3a22SAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
1491f5fe35ddSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0);
1492171ae1a4SAndy Whitcroft			$stat =~ s/\n./\n /g;
1493171ae1a4SAndy Whitcroft			$cond =~ s/\n./\n /g;
1494171ae1a4SAndy Whitcroft
14952b474a1aSAndy Whitcroft			# Find the real next line.
14962b474a1aSAndy Whitcroft			$realline_next = $line_nr_next;
14972b474a1aSAndy Whitcroft			if (defined $realline_next &&
14982b474a1aSAndy Whitcroft			    (!defined $lines[$realline_next - 1] ||
14992b474a1aSAndy Whitcroft			     substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
15002b474a1aSAndy Whitcroft				$realline_next++;
15012b474a1aSAndy Whitcroft			}
15022b474a1aSAndy Whitcroft
1503171ae1a4SAndy Whitcroft			my $s = $stat;
1504171ae1a4SAndy Whitcroft			$s =~ s/{.*$//s;
1505cf655043SAndy Whitcroft
1506c2fdda0dSAndy Whitcroft			# Ignore goto labels.
1507171ae1a4SAndy Whitcroft			if ($s =~ /$Ident:\*$/s) {
1508c2fdda0dSAndy Whitcroft
1509c2fdda0dSAndy Whitcroft			# Ignore functions being called
1510171ae1a4SAndy Whitcroft			} elsif ($s =~ /^.\s*$Ident\s*\(/s) {
1511c2fdda0dSAndy Whitcroft
1512463f2864SAndy Whitcroft			} elsif ($s =~ /^.\s*else\b/s) {
1513463f2864SAndy Whitcroft
1514c45dcabdSAndy Whitcroft			# declarations always start with types
1515d2506586SAndy 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) {
1516c45dcabdSAndy Whitcroft				my $type = $1;
1517c45dcabdSAndy Whitcroft				$type =~ s/\s+/ /g;
1518c45dcabdSAndy Whitcroft				possible($type, "A:" . $s);
1519c45dcabdSAndy Whitcroft
15206c72ffaaSAndy Whitcroft			# definitions in global scope can only start with types
1521a6a84062SAndy Whitcroft			} elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
1522c45dcabdSAndy Whitcroft				possible($1, "B:" . $s);
1523c2fdda0dSAndy Whitcroft			}
15248905a67cSAndy Whitcroft
15256c72ffaaSAndy Whitcroft			# any (foo ... *) is a pointer cast, and foo is a type
152665863862SAndy Whitcroft			while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
1527c45dcabdSAndy Whitcroft				possible($1, "C:" . $s);
15289c0ca6f9SAndy Whitcroft			}
15298905a67cSAndy Whitcroft
15308905a67cSAndy Whitcroft			# Check for any sort of function declaration.
15318905a67cSAndy Whitcroft			# int foo(something bar, other baz);
15328905a67cSAndy Whitcroft			# void (*store_gdt)(x86_descr_ptr *);
1533171ae1a4SAndy Whitcroft			if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
15348905a67cSAndy Whitcroft				my ($name_len) = length($1);
15358905a67cSAndy Whitcroft
1536cf655043SAndy Whitcroft				my $ctx = $s;
1537773647a0SAndy Whitcroft				substr($ctx, 0, $name_len + 1, '');
15388905a67cSAndy Whitcroft				$ctx =~ s/\)[^\)]*$//;
1539cf655043SAndy Whitcroft
15408905a67cSAndy Whitcroft				for my $arg (split(/\s*,\s*/, $ctx)) {
1541c45dcabdSAndy Whitcroft					if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
15428905a67cSAndy Whitcroft
1543c45dcabdSAndy Whitcroft						possible($1, "D:" . $s);
15448905a67cSAndy Whitcroft					}
15458905a67cSAndy Whitcroft				}
15468905a67cSAndy Whitcroft			}
15478905a67cSAndy Whitcroft
15489c0ca6f9SAndy Whitcroft		}
15499c0ca6f9SAndy Whitcroft
155000df344fSAndy Whitcroft#
155100df344fSAndy Whitcroft# Checks which may be anchored in the context.
155200df344fSAndy Whitcroft#
155300df344fSAndy Whitcroft
155400df344fSAndy Whitcroft# Check for switch () and associated case and default
155500df344fSAndy Whitcroft# statements should be at the same indent.
155600df344fSAndy Whitcroft		if ($line=~/\bswitch\s*\(.*\)/) {
155700df344fSAndy Whitcroft			my $err = '';
155800df344fSAndy Whitcroft			my $sep = '';
155900df344fSAndy Whitcroft			my @ctx = ctx_block_outer($linenr, $realcnt);
156000df344fSAndy Whitcroft			shift(@ctx);
156100df344fSAndy Whitcroft			for my $ctx (@ctx) {
156200df344fSAndy Whitcroft				my ($clen, $cindent) = line_stats($ctx);
156300df344fSAndy Whitcroft				if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
156400df344fSAndy Whitcroft							$indent != $cindent) {
156500df344fSAndy Whitcroft					$err .= "$sep$ctx\n";
156600df344fSAndy Whitcroft					$sep = '';
156700df344fSAndy Whitcroft				} else {
156800df344fSAndy Whitcroft					$sep = "[...]\n";
156900df344fSAndy Whitcroft				}
157000df344fSAndy Whitcroft			}
157100df344fSAndy Whitcroft			if ($err ne '') {
15729c0ca6f9SAndy Whitcroft				ERROR("switch and case should be at the same indent\n$hereline$err");
1573de7d4f0eSAndy Whitcroft			}
1574de7d4f0eSAndy Whitcroft		}
1575de7d4f0eSAndy Whitcroft
1576de7d4f0eSAndy Whitcroft# if/while/etc brace do not go on next line, unless defining a do while loop,
1577de7d4f0eSAndy Whitcroft# or if that brace on the next line is for something else
1578c45dcabdSAndy Whitcroft		if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
1579773647a0SAndy Whitcroft			my $pre_ctx = "$1$2";
1580773647a0SAndy Whitcroft
15819c0ca6f9SAndy Whitcroft			my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
1582de7d4f0eSAndy Whitcroft			my $ctx_cnt = $realcnt - $#ctx - 1;
1583de7d4f0eSAndy Whitcroft			my $ctx = join("\n", @ctx);
1584de7d4f0eSAndy Whitcroft
1585548596d5SAndy Whitcroft			my $ctx_ln = $linenr;
1586548596d5SAndy Whitcroft			my $ctx_skip = $realcnt;
1587de7d4f0eSAndy Whitcroft
1588548596d5SAndy Whitcroft			while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
1589548596d5SAndy Whitcroft					defined $lines[$ctx_ln - 1] &&
1590548596d5SAndy Whitcroft					$lines[$ctx_ln - 1] =~ /^-/)) {
1591548596d5SAndy Whitcroft				##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
1592548596d5SAndy Whitcroft				$ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
1593773647a0SAndy Whitcroft				$ctx_ln++;
1594773647a0SAndy Whitcroft			}
1595548596d5SAndy Whitcroft
159653210168SAndy Whitcroft			#print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
159753210168SAndy Whitcroft			#print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
1598773647a0SAndy Whitcroft
1599773647a0SAndy Whitcroft			if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln -1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
1600773647a0SAndy Whitcroft				ERROR("that open brace { should be on the previous line\n" .
1601c45dcabdSAndy Whitcroft					"$here\n$ctx\n$lines[$ctx_ln - 1]\n");
160200df344fSAndy Whitcroft			}
1603773647a0SAndy Whitcroft			if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
1604773647a0SAndy Whitcroft			    $ctx =~ /\)\s*\;\s*$/ &&
1605773647a0SAndy Whitcroft			    defined $lines[$ctx_ln - 1])
1606773647a0SAndy Whitcroft			{
16079c0ca6f9SAndy Whitcroft				my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
16089c0ca6f9SAndy Whitcroft				if ($nindent > $indent) {
1609773647a0SAndy Whitcroft					WARN("trailing semicolon indicates no statements, indent implies otherwise\n" .
1610c45dcabdSAndy Whitcroft						"$here\n$ctx\n$lines[$ctx_ln - 1]\n");
16119c0ca6f9SAndy Whitcroft				}
16129c0ca6f9SAndy Whitcroft			}
161300df344fSAndy Whitcroft		}
161400df344fSAndy Whitcroft
16154d001e4dSAndy Whitcroft# Check relative indent for conditionals and blocks.
16164d001e4dSAndy Whitcroft		if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
16174d001e4dSAndy Whitcroft			my ($s, $c) = ($stat, $cond);
16184d001e4dSAndy Whitcroft
16194d001e4dSAndy Whitcroft			substr($s, 0, length($c), '');
16204d001e4dSAndy Whitcroft
16214d001e4dSAndy Whitcroft			# Make sure we remove the line prefixes as we have
16224d001e4dSAndy Whitcroft			# none on the first line, and are going to readd them
16234d001e4dSAndy Whitcroft			# where necessary.
16244d001e4dSAndy Whitcroft			$s =~ s/\n./\n/gs;
16254d001e4dSAndy Whitcroft
16264d001e4dSAndy Whitcroft			# Find out how long the conditional actually is.
16276f779c18SAndy Whitcroft			my @newlines = ($c =~ /\n/gs);
16286f779c18SAndy Whitcroft			my $cond_lines = 1 + $#newlines;
16294d001e4dSAndy Whitcroft
16304d001e4dSAndy Whitcroft			# We want to check the first line inside the block
16314d001e4dSAndy Whitcroft			# starting at the end of the conditional, so remove:
16324d001e4dSAndy Whitcroft			#  1) any blank line termination
16334d001e4dSAndy Whitcroft			#  2) any opening brace { on end of the line
16344d001e4dSAndy Whitcroft			#  3) any do (...) {
16354d001e4dSAndy Whitcroft			my $continuation = 0;
16364d001e4dSAndy Whitcroft			my $check = 0;
16374d001e4dSAndy Whitcroft			$s =~ s/^.*\bdo\b//;
16384d001e4dSAndy Whitcroft			$s =~ s/^\s*{//;
16394d001e4dSAndy Whitcroft			if ($s =~ s/^\s*\\//) {
16404d001e4dSAndy Whitcroft				$continuation = 1;
16414d001e4dSAndy Whitcroft			}
16429bd49efeSAndy Whitcroft			if ($s =~ s/^\s*?\n//) {
16434d001e4dSAndy Whitcroft				$check = 1;
16444d001e4dSAndy Whitcroft				$cond_lines++;
16454d001e4dSAndy Whitcroft			}
16464d001e4dSAndy Whitcroft
16474d001e4dSAndy Whitcroft			# Also ignore a loop construct at the end of a
16484d001e4dSAndy Whitcroft			# preprocessor statement.
16494d001e4dSAndy Whitcroft			if (($prevline =~ /^.\s*#\s*define\s/ ||
16504d001e4dSAndy Whitcroft			    $prevline =~ /\\\s*$/) && $continuation == 0) {
16514d001e4dSAndy Whitcroft				$check = 0;
16524d001e4dSAndy Whitcroft			}
16534d001e4dSAndy Whitcroft
16549bd49efeSAndy Whitcroft			my $cond_ptr = -1;
1655740504c6SAndy Whitcroft			$continuation = 0;
16569bd49efeSAndy Whitcroft			while ($cond_ptr != $cond_lines) {
16579bd49efeSAndy Whitcroft				$cond_ptr = $cond_lines;
16584d001e4dSAndy Whitcroft
1659f16fa28fSAndy Whitcroft				# If we see an #else/#elif then the code
1660f16fa28fSAndy Whitcroft				# is not linear.
1661f16fa28fSAndy Whitcroft				if ($s =~ /^\s*\#\s*(?:else|elif)/) {
1662f16fa28fSAndy Whitcroft					$check = 0;
1663f16fa28fSAndy Whitcroft				}
1664f16fa28fSAndy Whitcroft
16659bd49efeSAndy Whitcroft				# Ignore:
16669bd49efeSAndy Whitcroft				#  1) blank lines, they should be at 0,
16679bd49efeSAndy Whitcroft				#  2) preprocessor lines, and
16689bd49efeSAndy Whitcroft				#  3) labels.
1669740504c6SAndy Whitcroft				if ($continuation ||
1670740504c6SAndy Whitcroft				    $s =~ /^\s*?\n/ ||
16719bd49efeSAndy Whitcroft				    $s =~ /^\s*#\s*?/ ||
16729bd49efeSAndy Whitcroft				    $s =~ /^\s*$Ident\s*:/) {
1673740504c6SAndy Whitcroft					$continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
167430dad6ebSAndy Whitcroft					if ($s =~ s/^.*?\n//) {
16759bd49efeSAndy Whitcroft						$cond_lines++;
16769bd49efeSAndy Whitcroft					}
16774d001e4dSAndy Whitcroft				}
167830dad6ebSAndy Whitcroft			}
16794d001e4dSAndy Whitcroft
16804d001e4dSAndy Whitcroft			my (undef, $sindent) = line_stats("+" . $s);
16814d001e4dSAndy Whitcroft			my $stat_real = raw_line($linenr, $cond_lines);
16824d001e4dSAndy Whitcroft
16834d001e4dSAndy Whitcroft			# Check if either of these lines are modified, else
16844d001e4dSAndy Whitcroft			# this is not this patch's fault.
16854d001e4dSAndy Whitcroft			if (!defined($stat_real) ||
16864d001e4dSAndy Whitcroft			    $stat !~ /^\+/ && $stat_real !~ /^\+/) {
16874d001e4dSAndy Whitcroft				$check = 0;
16884d001e4dSAndy Whitcroft			}
16894d001e4dSAndy Whitcroft			if (defined($stat_real) && $cond_lines > 1) {
16904d001e4dSAndy Whitcroft				$stat_real = "[...]\n$stat_real";
16914d001e4dSAndy Whitcroft			}
16924d001e4dSAndy Whitcroft
16939bd49efeSAndy 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";
16944d001e4dSAndy Whitcroft
16954d001e4dSAndy Whitcroft			if ($check && (($sindent % 8) != 0 ||
16964d001e4dSAndy Whitcroft			    ($sindent <= $indent && $s ne ''))) {
16974d001e4dSAndy Whitcroft				WARN("suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
16984d001e4dSAndy Whitcroft			}
16994d001e4dSAndy Whitcroft		}
17004d001e4dSAndy Whitcroft
17016c72ffaaSAndy Whitcroft		# Track the 'values' across context and added lines.
17026c72ffaaSAndy Whitcroft		my $opline = $line; $opline =~ s/^./ /;
17031f65f947SAndy Whitcroft		my ($curr_values, $curr_vars) =
17041f65f947SAndy Whitcroft				annotate_values($opline . "\n", $prev_values);
17056c72ffaaSAndy Whitcroft		$curr_values = $prev_values . $curr_values;
1706c2fdda0dSAndy Whitcroft		if ($dbg_values) {
1707c2fdda0dSAndy Whitcroft			my $outline = $opline; $outline =~ s/\t/ /g;
1708cf655043SAndy Whitcroft			print "$linenr > .$outline\n";
1709cf655043SAndy Whitcroft			print "$linenr > $curr_values\n";
17101f65f947SAndy Whitcroft			print "$linenr >  $curr_vars\n";
1711c2fdda0dSAndy Whitcroft		}
17126c72ffaaSAndy Whitcroft		$prev_values = substr($curr_values, -1);
17136c72ffaaSAndy Whitcroft
171400df344fSAndy Whitcroft#ignore lines not being added
171500df344fSAndy Whitcroft		if ($line=~/^[^\+]/) {next;}
171600df344fSAndy Whitcroft
1717653d4876SAndy Whitcroft# TEST: allow direct testing of the type matcher.
17187429c690SAndy Whitcroft		if ($dbg_type) {
17197429c690SAndy Whitcroft			if ($line =~ /^.\s*$Declare\s*$/) {
17207429c690SAndy Whitcroft				ERROR("TEST: is type\n" . $herecurr);
17217429c690SAndy Whitcroft			} elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
17227429c690SAndy Whitcroft				ERROR("TEST: is not type ($1 is)\n". $herecurr);
17237429c690SAndy Whitcroft			}
1724653d4876SAndy Whitcroft			next;
1725653d4876SAndy Whitcroft		}
1726a1ef277eSAndy Whitcroft# TEST: allow direct testing of the attribute matcher.
1727a1ef277eSAndy Whitcroft		if ($dbg_attr) {
17289360b0e5SAndy Whitcroft			if ($line =~ /^.\s*$Modifier\s*$/) {
1729a1ef277eSAndy Whitcroft				ERROR("TEST: is attr\n" . $herecurr);
17309360b0e5SAndy Whitcroft			} elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
1731a1ef277eSAndy Whitcroft				ERROR("TEST: is not attr ($1 is)\n". $herecurr);
1732a1ef277eSAndy Whitcroft			}
1733a1ef277eSAndy Whitcroft			next;
1734a1ef277eSAndy Whitcroft		}
1735653d4876SAndy Whitcroft
1736f0a594c1SAndy Whitcroft# check for initialisation to aggregates open brace on the next line
173799423c20SAndy Whitcroft		if ($line =~ /^.\s*{/ &&
173899423c20SAndy Whitcroft		    $prevline =~ /(?:^|[^=])=\s*$/) {
1739773647a0SAndy Whitcroft			ERROR("that open brace { should be on the previous line\n" . $hereprev);
1740f0a594c1SAndy Whitcroft		}
1741f0a594c1SAndy Whitcroft
174200df344fSAndy Whitcroft#
174300df344fSAndy Whitcroft# Checks which are anchored on the added line.
174400df344fSAndy Whitcroft#
174500df344fSAndy Whitcroft
1746653d4876SAndy Whitcroft# check for malformed paths in #include statements (uses RAW line)
1747c45dcabdSAndy Whitcroft		if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
1748653d4876SAndy Whitcroft			my $path = $1;
1749653d4876SAndy Whitcroft			if ($path =~ m{//}) {
1750de7d4f0eSAndy Whitcroft				ERROR("malformed #include filename\n" .
1751de7d4f0eSAndy Whitcroft					$herecurr);
1752653d4876SAndy Whitcroft			}
1753653d4876SAndy Whitcroft		}
1754653d4876SAndy Whitcroft
175500df344fSAndy Whitcroft# no C99 // comments
175600df344fSAndy Whitcroft		if ($line =~ m{//}) {
1757de7d4f0eSAndy Whitcroft			ERROR("do not use C99 // comments\n" . $herecurr);
175800df344fSAndy Whitcroft		}
175900df344fSAndy Whitcroft		# Remove C99 comments.
17600a920b5bSAndy Whitcroft		$line =~ s@//.*@@;
17616c72ffaaSAndy Whitcroft		$opline =~ s@//.*@@;
17620a920b5bSAndy Whitcroft
17632b474a1aSAndy Whitcroft# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
17642b474a1aSAndy Whitcroft# the whole statement.
17652b474a1aSAndy Whitcroft#print "APW <$lines[$realline_next - 1]>\n";
17662b474a1aSAndy Whitcroft		if (defined $realline_next &&
17672b474a1aSAndy Whitcroft		    exists $lines[$realline_next - 1] &&
17682b474a1aSAndy Whitcroft		    !defined $suppress_export{$realline_next} &&
17692b474a1aSAndy Whitcroft		    ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
17702b474a1aSAndy Whitcroft		     $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
1771653d4876SAndy Whitcroft			my $name = $1;
17722b474a1aSAndy Whitcroft			if ($stat !~ /(?:
17732b474a1aSAndy Whitcroft				\n.}\s*$|
177448012058SAndy Whitcroft				^.DEFINE_$Ident\(\Q$name\E\)|
177548012058SAndy Whitcroft				^.DECLARE_$Ident\(\Q$name\E\)|
177648012058SAndy Whitcroft				^.LIST_HEAD\(\Q$name\E\)|
17772b474a1aSAndy Whitcroft				^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
17782b474a1aSAndy Whitcroft				\b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
177948012058SAndy Whitcroft			    )/x) {
17802b474a1aSAndy Whitcroft#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
17812b474a1aSAndy Whitcroft				$suppress_export{$realline_next} = 2;
17822b474a1aSAndy Whitcroft			} else {
17832b474a1aSAndy Whitcroft				$suppress_export{$realline_next} = 1;
17840a920b5bSAndy Whitcroft			}
17850a920b5bSAndy Whitcroft		}
17862b474a1aSAndy Whitcroft		if (!defined $suppress_export{$linenr} &&
17872b474a1aSAndy Whitcroft		    $prevline =~ /^.\s*$/ &&
17882b474a1aSAndy Whitcroft		    ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
17892b474a1aSAndy Whitcroft		     $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
17902b474a1aSAndy Whitcroft#print "FOO B <$lines[$linenr - 1]>\n";
17912b474a1aSAndy Whitcroft			$suppress_export{$linenr} = 2;
17922b474a1aSAndy Whitcroft		}
17932b474a1aSAndy Whitcroft		if (defined $suppress_export{$linenr} &&
17942b474a1aSAndy Whitcroft		    $suppress_export{$linenr} == 2) {
17952b474a1aSAndy Whitcroft			WARN("EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
17962b474a1aSAndy Whitcroft		}
17970a920b5bSAndy Whitcroft
17985150bda4SJoe Eloff# check for global initialisers.
1799c45dcabdSAndy Whitcroft		if ($line =~ /^.$Type\s*$Ident\s*(?:\s+$Modifier)*\s*=\s*(0|NULL|false)\s*;/) {
18005150bda4SJoe Eloff			ERROR("do not initialise globals to 0 or NULL\n" .
1801f0a594c1SAndy Whitcroft				$herecurr);
1802f0a594c1SAndy Whitcroft		}
18030a920b5bSAndy Whitcroft# check for static initialisers.
18042d1bafd7SAndy Whitcroft		if ($line =~ /\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
1805de7d4f0eSAndy Whitcroft			ERROR("do not initialise statics to 0 or NULL\n" .
1806de7d4f0eSAndy Whitcroft				$herecurr);
18070a920b5bSAndy Whitcroft		}
18080a920b5bSAndy Whitcroft
1809653d4876SAndy Whitcroft# check for new typedefs, only function parameters and sparse annotations
1810653d4876SAndy Whitcroft# make sense.
1811653d4876SAndy Whitcroft		if ($line =~ /\btypedef\s/ &&
18128054576dSAndy Whitcroft		    $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
1813c45dcabdSAndy Whitcroft		    $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
18148ed22cadSAndy Whitcroft		    $line !~ /\b$typeTypedefs\b/ &&
1815653d4876SAndy Whitcroft		    $line !~ /\b__bitwise(?:__|)\b/) {
1816de7d4f0eSAndy Whitcroft			WARN("do not add new typedefs\n" . $herecurr);
18170a920b5bSAndy Whitcroft		}
18180a920b5bSAndy Whitcroft
18190a920b5bSAndy Whitcroft# * goes on variable not on type
182065863862SAndy Whitcroft		# (char*[ const])
182100ef4eceSAndy Whitcroft		if ($line =~ m{\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\)}) {
182265863862SAndy Whitcroft			my ($from, $to) = ($1, $1);
1823d8aaf121SAndy Whitcroft
182465863862SAndy Whitcroft			# Should start with a space.
182565863862SAndy Whitcroft			$to =~ s/^(\S)/ $1/;
182665863862SAndy Whitcroft			# Should not end with a space.
182765863862SAndy Whitcroft			$to =~ s/\s+$//;
182865863862SAndy Whitcroft			# '*'s should not have spaces between.
1829f9a0b3d1SAndy Whitcroft			while ($to =~ s/\*\s+\*/\*\*/) {
183065863862SAndy Whitcroft			}
1831d8aaf121SAndy Whitcroft
183265863862SAndy Whitcroft			#print "from<$from> to<$to>\n";
183365863862SAndy Whitcroft			if ($from ne $to) {
183465863862SAndy Whitcroft				ERROR("\"(foo$from)\" should be \"(foo$to)\"\n" .  $herecurr);
183565863862SAndy Whitcroft			}
183600ef4eceSAndy Whitcroft		} elsif ($line =~ m{\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident)}) {
183765863862SAndy Whitcroft			my ($from, $to, $ident) = ($1, $1, $2);
1838d8aaf121SAndy Whitcroft
183965863862SAndy Whitcroft			# Should start with a space.
184065863862SAndy Whitcroft			$to =~ s/^(\S)/ $1/;
184165863862SAndy Whitcroft			# Should not end with a space.
184265863862SAndy Whitcroft			$to =~ s/\s+$//;
184365863862SAndy Whitcroft			# '*'s should not have spaces between.
1844f9a0b3d1SAndy Whitcroft			while ($to =~ s/\*\s+\*/\*\*/) {
184565863862SAndy Whitcroft			}
184665863862SAndy Whitcroft			# Modifiers should have spaces.
184765863862SAndy Whitcroft			$to =~ s/(\b$Modifier$)/$1 /;
184865863862SAndy Whitcroft
1849667026e7SAndy Whitcroft			#print "from<$from> to<$to> ident<$ident>\n";
1850667026e7SAndy Whitcroft			if ($from ne $to && $ident !~ /^$Modifier$/) {
185165863862SAndy Whitcroft				ERROR("\"foo${from}bar\" should be \"foo${to}bar\"\n" .  $herecurr);
185265863862SAndy Whitcroft			}
18530a920b5bSAndy Whitcroft		}
18540a920b5bSAndy Whitcroft
18550a920b5bSAndy Whitcroft# # no BUG() or BUG_ON()
18560a920b5bSAndy Whitcroft# 		if ($line =~ /\b(BUG|BUG_ON)\b/) {
18570a920b5bSAndy Whitcroft# 			print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
18580a920b5bSAndy Whitcroft# 			print "$herecurr";
18590a920b5bSAndy Whitcroft# 			$clean = 0;
18600a920b5bSAndy Whitcroft# 		}
18610a920b5bSAndy Whitcroft
18628905a67cSAndy Whitcroft		if ($line =~ /\bLINUX_VERSION_CODE\b/) {
1863c2fdda0dSAndy Whitcroft			WARN("LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
18648905a67cSAndy Whitcroft		}
18658905a67cSAndy Whitcroft
186600df344fSAndy Whitcroft# printk should use KERN_* levels.  Note that follow on printk's on the
186700df344fSAndy Whitcroft# same line do not need a level, so we use the current block context
186800df344fSAndy Whitcroft# to try and find and validate the current printk.  In summary the current
186900df344fSAndy Whitcroft# printk includes all preceeding printk's which have no newline on the end.
187000df344fSAndy Whitcroft# we assume the first bad printk is the one to report.
1871f0a594c1SAndy Whitcroft		if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
187200df344fSAndy Whitcroft			my $ok = 0;
187300df344fSAndy Whitcroft			for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
187400df344fSAndy Whitcroft				#print "CHECK<$lines[$ln - 1]\n";
187500df344fSAndy Whitcroft				# we have a preceeding printk if it ends
187600df344fSAndy Whitcroft				# with "\n" ignore it, else it is to blame
187700df344fSAndy Whitcroft				if ($lines[$ln - 1] =~ m{\bprintk\(}) {
187800df344fSAndy Whitcroft					if ($rawlines[$ln - 1] !~ m{\\n"}) {
187900df344fSAndy Whitcroft						$ok = 1;
188000df344fSAndy Whitcroft					}
188100df344fSAndy Whitcroft					last;
188200df344fSAndy Whitcroft				}
188300df344fSAndy Whitcroft			}
188400df344fSAndy Whitcroft			if ($ok == 0) {
1885de7d4f0eSAndy Whitcroft				WARN("printk() should include KERN_ facility level\n" . $herecurr);
18860a920b5bSAndy Whitcroft			}
188700df344fSAndy Whitcroft		}
18880a920b5bSAndy Whitcroft
1889653d4876SAndy Whitcroft# function brace can't be on same line, except for #defines of do while,
1890653d4876SAndy Whitcroft# or if closed on same line
1891c45dcabdSAndy Whitcroft		if (($line=~/$Type\s*$Ident\(.*\).*\s{/) and
1892c45dcabdSAndy Whitcroft		    !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) {
1893de7d4f0eSAndy Whitcroft			ERROR("open brace '{' following function declarations go on the next line\n" . $herecurr);
18940a920b5bSAndy Whitcroft		}
1895653d4876SAndy Whitcroft
18968905a67cSAndy Whitcroft# open braces for enum, union and struct go on the same line.
18978905a67cSAndy Whitcroft		if ($line =~ /^.\s*{/ &&
18988905a67cSAndy Whitcroft		    $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
18998905a67cSAndy Whitcroft			ERROR("open brace '{' following $1 go on the same line\n" . $hereprev);
19008905a67cSAndy Whitcroft		}
19018905a67cSAndy Whitcroft
19028d31cfceSAndy Whitcroft# check for spacing round square brackets; allowed:
19038d31cfceSAndy Whitcroft#  1. with a type on the left -- int [] a;
1904fe2a7dbcSAndy Whitcroft#  2. at the beginning of a line for slice initialisers -- [0...10] = 5,
1905fe2a7dbcSAndy Whitcroft#  3. inside a curly brace -- = { [0...10] = 5 }
19068d31cfceSAndy Whitcroft		while ($line =~ /(.*?\s)\[/g) {
19078d31cfceSAndy Whitcroft			my ($where, $prefix) = ($-[1], $1);
19088d31cfceSAndy Whitcroft			if ($prefix !~ /$Type\s+$/ &&
1909fe2a7dbcSAndy Whitcroft			    ($where != 0 || $prefix !~ /^.\s+$/) &&
1910fe2a7dbcSAndy Whitcroft			    $prefix !~ /{\s+$/) {
19118d31cfceSAndy Whitcroft				ERROR("space prohibited before open square bracket '['\n" . $herecurr);
19128d31cfceSAndy Whitcroft			}
19138d31cfceSAndy Whitcroft		}
19148d31cfceSAndy Whitcroft
1915f0a594c1SAndy Whitcroft# check for spaces between functions and their parentheses.
19166c72ffaaSAndy Whitcroft		while ($line =~ /($Ident)\s+\(/g) {
1917c2fdda0dSAndy Whitcroft			my $name = $1;
1918773647a0SAndy Whitcroft			my $ctx_before = substr($line, 0, $-[1]);
1919773647a0SAndy Whitcroft			my $ctx = "$ctx_before$name";
1920c2fdda0dSAndy Whitcroft
1921c2fdda0dSAndy Whitcroft			# Ignore those directives where spaces _are_ permitted.
1922773647a0SAndy Whitcroft			if ($name =~ /^(?:
1923773647a0SAndy Whitcroft				if|for|while|switch|return|case|
1924773647a0SAndy Whitcroft				volatile|__volatile__|
1925773647a0SAndy Whitcroft				__attribute__|format|__extension__|
1926773647a0SAndy Whitcroft				asm|__asm__)$/x)
1927773647a0SAndy Whitcroft			{
1928c2fdda0dSAndy Whitcroft
1929c2fdda0dSAndy Whitcroft			# cpp #define statements have non-optional spaces, ie
1930c2fdda0dSAndy Whitcroft			# if there is a space between the name and the open
1931c2fdda0dSAndy Whitcroft			# parenthesis it is simply not a parameter group.
1932c45dcabdSAndy Whitcroft			} elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
1933773647a0SAndy Whitcroft
1934773647a0SAndy Whitcroft			# cpp #elif statement condition may start with a (
1935c45dcabdSAndy Whitcroft			} elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
1936c2fdda0dSAndy Whitcroft
1937c2fdda0dSAndy Whitcroft			# If this whole things ends with a type its most
1938c2fdda0dSAndy Whitcroft			# likely a typedef for a function.
1939773647a0SAndy Whitcroft			} elsif ($ctx =~ /$Type$/) {
1940c2fdda0dSAndy Whitcroft
1941c2fdda0dSAndy Whitcroft			} else {
1942773647a0SAndy Whitcroft				WARN("space prohibited between function name and open parenthesis '('\n" . $herecurr);
1943f0a594c1SAndy Whitcroft			}
19446c72ffaaSAndy Whitcroft		}
1945653d4876SAndy Whitcroft# Check operator spacing.
19460a920b5bSAndy Whitcroft		if (!($line=~/\#\s*include/)) {
19479c0ca6f9SAndy Whitcroft			my $ops = qr{
19489c0ca6f9SAndy Whitcroft				<<=|>>=|<=|>=|==|!=|
19499c0ca6f9SAndy Whitcroft				\+=|-=|\*=|\/=|%=|\^=|\|=|&=|
19509c0ca6f9SAndy Whitcroft				=>|->|<<|>>|<|>|=|!|~|
19511f65f947SAndy Whitcroft				&&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
19521f65f947SAndy Whitcroft				\?|:
19539c0ca6f9SAndy Whitcroft			}x;
1954cf655043SAndy Whitcroft			my @elements = split(/($ops|;)/, $opline);
195500df344fSAndy Whitcroft			my $off = 0;
19566c72ffaaSAndy Whitcroft
19576c72ffaaSAndy Whitcroft			my $blank = copy_spacing($opline);
19586c72ffaaSAndy Whitcroft
19590a920b5bSAndy Whitcroft			for (my $n = 0; $n < $#elements; $n += 2) {
19604a0df2efSAndy Whitcroft				$off += length($elements[$n]);
19614a0df2efSAndy Whitcroft
1962773647a0SAndy Whitcroft				# Pick up the preceeding and succeeding characters.
1963773647a0SAndy Whitcroft				my $ca = substr($opline, 0, $off);
1964773647a0SAndy Whitcroft				my $cc = '';
1965773647a0SAndy Whitcroft				if (length($opline) >= ($off + length($elements[$n + 1]))) {
1966773647a0SAndy Whitcroft					$cc = substr($opline, $off + length($elements[$n + 1]));
1967773647a0SAndy Whitcroft				}
1968773647a0SAndy Whitcroft				my $cb = "$ca$;$cc";
1969773647a0SAndy Whitcroft
19704a0df2efSAndy Whitcroft				my $a = '';
19714a0df2efSAndy Whitcroft				$a = 'V' if ($elements[$n] ne '');
19724a0df2efSAndy Whitcroft				$a = 'W' if ($elements[$n] =~ /\s$/);
1973cf655043SAndy Whitcroft				$a = 'C' if ($elements[$n] =~ /$;$/);
19744a0df2efSAndy Whitcroft				$a = 'B' if ($elements[$n] =~ /(\[|\()$/);
19754a0df2efSAndy Whitcroft				$a = 'O' if ($elements[$n] eq '');
1976773647a0SAndy Whitcroft				$a = 'E' if ($ca =~ /^\s*$/);
19774a0df2efSAndy Whitcroft
19780a920b5bSAndy Whitcroft				my $op = $elements[$n + 1];
19794a0df2efSAndy Whitcroft
19804a0df2efSAndy Whitcroft				my $c = '';
19810a920b5bSAndy Whitcroft				if (defined $elements[$n + 2]) {
19824a0df2efSAndy Whitcroft					$c = 'V' if ($elements[$n + 2] ne '');
19834a0df2efSAndy Whitcroft					$c = 'W' if ($elements[$n + 2] =~ /^\s/);
1984cf655043SAndy Whitcroft					$c = 'C' if ($elements[$n + 2] =~ /^$;/);
19854a0df2efSAndy Whitcroft					$c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
19864a0df2efSAndy Whitcroft					$c = 'O' if ($elements[$n + 2] eq '');
19878b1b3378SAndy Whitcroft					$c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
19884a0df2efSAndy Whitcroft				} else {
19894a0df2efSAndy Whitcroft					$c = 'E';
19900a920b5bSAndy Whitcroft				}
19910a920b5bSAndy Whitcroft
19924a0df2efSAndy Whitcroft				my $ctx = "${a}x${c}";
19934a0df2efSAndy Whitcroft
19944a0df2efSAndy Whitcroft				my $at = "(ctx:$ctx)";
19954a0df2efSAndy Whitcroft
19966c72ffaaSAndy Whitcroft				my $ptr = substr($blank, 0, $off) . "^";
1997de7d4f0eSAndy Whitcroft				my $hereptr = "$hereline$ptr\n";
19980a920b5bSAndy Whitcroft
199974048ed8SAndy Whitcroft				# Pull out the value of this operator.
20006c72ffaaSAndy Whitcroft				my $op_type = substr($curr_values, $off + 1, 1);
20010a920b5bSAndy Whitcroft
20021f65f947SAndy Whitcroft				# Get the full operator variant.
20031f65f947SAndy Whitcroft				my $opv = $op . substr($curr_vars, $off, 1);
20041f65f947SAndy Whitcroft
200513214adfSAndy Whitcroft				# Ignore operators passed as parameters.
200613214adfSAndy Whitcroft				if ($op_type ne 'V' &&
200713214adfSAndy Whitcroft				    $ca =~ /\s$/ && $cc =~ /^\s*,/) {
200813214adfSAndy Whitcroft
2009cf655043SAndy Whitcroft#				# Ignore comments
2010cf655043SAndy Whitcroft#				} elsif ($op =~ /^$;+$/) {
201113214adfSAndy Whitcroft
2012d8aaf121SAndy Whitcroft				# ; should have either the end of line or a space or \ after it
201313214adfSAndy Whitcroft				} elsif ($op eq ';') {
2014cf655043SAndy Whitcroft					if ($ctx !~ /.x[WEBC]/ &&
2015cf655043SAndy Whitcroft					    $cc !~ /^\\/ && $cc !~ /^;/) {
2016773647a0SAndy Whitcroft						ERROR("space required after that '$op' $at\n" . $hereptr);
2017d8aaf121SAndy Whitcroft					}
2018d8aaf121SAndy Whitcroft
2019d8aaf121SAndy Whitcroft				# // is a comment
2020d8aaf121SAndy Whitcroft				} elsif ($op eq '//') {
20210a920b5bSAndy Whitcroft
20221f65f947SAndy Whitcroft				# No spaces for:
20231f65f947SAndy Whitcroft				#   ->
20241f65f947SAndy Whitcroft				#   :   when part of a bitfield
20251f65f947SAndy Whitcroft				} elsif ($op eq '->' || $opv eq ':B') {
20264a0df2efSAndy Whitcroft					if ($ctx =~ /Wx.|.xW/) {
2027773647a0SAndy Whitcroft						ERROR("spaces prohibited around that '$op' $at\n" . $hereptr);
20280a920b5bSAndy Whitcroft					}
20290a920b5bSAndy Whitcroft
20300a920b5bSAndy Whitcroft				# , must have a space on the right.
20310a920b5bSAndy Whitcroft				} elsif ($op eq ',') {
2032cf655043SAndy Whitcroft					if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
2033773647a0SAndy Whitcroft						ERROR("space required after that '$op' $at\n" . $hereptr);
20340a920b5bSAndy Whitcroft					}
20350a920b5bSAndy Whitcroft
20369c0ca6f9SAndy Whitcroft				# '*' as part of a type definition -- reported already.
203774048ed8SAndy Whitcroft				} elsif ($opv eq '*_') {
20389c0ca6f9SAndy Whitcroft					#warn "'*' is part of type\n";
20399c0ca6f9SAndy Whitcroft
20409c0ca6f9SAndy Whitcroft				# unary operators should have a space before and
20419c0ca6f9SAndy Whitcroft				# none after.  May be left adjacent to another
20429c0ca6f9SAndy Whitcroft				# unary operator, or a cast
20439c0ca6f9SAndy Whitcroft				} elsif ($op eq '!' || $op eq '~' ||
204474048ed8SAndy Whitcroft					 $opv eq '*U' || $opv eq '-U' ||
20450d413866SAndy Whitcroft					 $opv eq '&U' || $opv eq '&&U') {
2046cf655043SAndy Whitcroft					if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
2047773647a0SAndy Whitcroft						ERROR("space required before that '$op' $at\n" . $hereptr);
20480a920b5bSAndy Whitcroft					}
2049a3340b35SAndy Whitcroft					if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
2050171ae1a4SAndy Whitcroft						# A unary '*' may be const
2051171ae1a4SAndy Whitcroft
2052171ae1a4SAndy Whitcroft					} elsif ($ctx =~ /.xW/) {
2053fb9e9096SAndy Whitcroft						ERROR("space prohibited after that '$op' $at\n" . $hereptr);
20540a920b5bSAndy Whitcroft					}
20550a920b5bSAndy Whitcroft
20560a920b5bSAndy Whitcroft				# unary ++ and unary -- are allowed no space on one side.
20570a920b5bSAndy Whitcroft				} elsif ($op eq '++' or $op eq '--') {
2058773647a0SAndy Whitcroft					if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
2059773647a0SAndy Whitcroft						ERROR("space required one side of that '$op' $at\n" . $hereptr);
20600a920b5bSAndy Whitcroft					}
2061773647a0SAndy Whitcroft					if ($ctx =~ /Wx[BE]/ ||
2062773647a0SAndy Whitcroft					    ($ctx =~ /Wx./ && $cc =~ /^;/)) {
2063773647a0SAndy Whitcroft						ERROR("space prohibited before that '$op' $at\n" . $hereptr);
2064653d4876SAndy Whitcroft					}
2065773647a0SAndy Whitcroft					if ($ctx =~ /ExW/) {
2066773647a0SAndy Whitcroft						ERROR("space prohibited after that '$op' $at\n" . $hereptr);
2067773647a0SAndy Whitcroft					}
2068773647a0SAndy Whitcroft
20690a920b5bSAndy Whitcroft
20700a920b5bSAndy Whitcroft				# << and >> may either have or not have spaces both sides
20719c0ca6f9SAndy Whitcroft				} elsif ($op eq '<<' or $op eq '>>' or
20729c0ca6f9SAndy Whitcroft					 $op eq '&' or $op eq '^' or $op eq '|' or
20739c0ca6f9SAndy Whitcroft					 $op eq '+' or $op eq '-' or
2074c2fdda0dSAndy Whitcroft					 $op eq '*' or $op eq '/' or
2075c2fdda0dSAndy Whitcroft					 $op eq '%')
20760a920b5bSAndy Whitcroft				{
2077773647a0SAndy Whitcroft					if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
2078de7d4f0eSAndy Whitcroft						ERROR("need consistent spacing around '$op' $at\n" .
2079de7d4f0eSAndy Whitcroft							$hereptr);
20800a920b5bSAndy Whitcroft					}
20810a920b5bSAndy Whitcroft
20821f65f947SAndy Whitcroft				# A colon needs no spaces before when it is
20831f65f947SAndy Whitcroft				# terminating a case value or a label.
20841f65f947SAndy Whitcroft				} elsif ($opv eq ':C' || $opv eq ':L') {
20851f65f947SAndy Whitcroft					if ($ctx =~ /Wx./) {
20861f65f947SAndy Whitcroft						ERROR("space prohibited before that '$op' $at\n" . $hereptr);
20871f65f947SAndy Whitcroft					}
20881f65f947SAndy Whitcroft
20890a920b5bSAndy Whitcroft				# All the others need spaces both sides.
2090cf655043SAndy Whitcroft				} elsif ($ctx !~ /[EWC]x[CWE]/) {
20911f65f947SAndy Whitcroft					my $ok = 0;
20921f65f947SAndy Whitcroft
209322f2a2efSAndy Whitcroft					# Ignore email addresses <foo@bar>
20941f65f947SAndy Whitcroft					if (($op eq '<' &&
20951f65f947SAndy Whitcroft					     $cc =~ /^\S+\@\S+>/) ||
20961f65f947SAndy Whitcroft					    ($op eq '>' &&
20971f65f947SAndy Whitcroft					     $ca =~ /<\S+\@\S+$/))
20981f65f947SAndy Whitcroft					{
20991f65f947SAndy Whitcroft					    	$ok = 1;
21001f65f947SAndy Whitcroft					}
21011f65f947SAndy Whitcroft
21021f65f947SAndy Whitcroft					# Ignore ?:
21031f65f947SAndy Whitcroft					if (($opv eq ':O' && $ca =~ /\?$/) ||
21041f65f947SAndy Whitcroft					    ($op eq '?' && $cc =~ /^:/)) {
21051f65f947SAndy Whitcroft					    	$ok = 1;
21061f65f947SAndy Whitcroft					}
21071f65f947SAndy Whitcroft
21081f65f947SAndy Whitcroft					if ($ok == 0) {
2109773647a0SAndy Whitcroft						ERROR("spaces required around that '$op' $at\n" . $hereptr);
21100a920b5bSAndy Whitcroft					}
211122f2a2efSAndy Whitcroft				}
21124a0df2efSAndy Whitcroft				$off += length($elements[$n + 1]);
21130a920b5bSAndy Whitcroft			}
21140a920b5bSAndy Whitcroft		}
21150a920b5bSAndy Whitcroft
2116f0a594c1SAndy Whitcroft# check for multiple assignments
2117f0a594c1SAndy Whitcroft		if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
21186c72ffaaSAndy Whitcroft			CHK("multiple assignments should be avoided\n" . $herecurr);
2119f0a594c1SAndy Whitcroft		}
2120f0a594c1SAndy Whitcroft
212122f2a2efSAndy Whitcroft## # check for multiple declarations, allowing for a function declaration
212222f2a2efSAndy Whitcroft## # continuation.
212322f2a2efSAndy Whitcroft## 		if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
212422f2a2efSAndy Whitcroft## 		    $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
212522f2a2efSAndy Whitcroft##
212622f2a2efSAndy Whitcroft## 			# Remove any bracketed sections to ensure we do not
212722f2a2efSAndy Whitcroft## 			# falsly report the parameters of functions.
212822f2a2efSAndy Whitcroft## 			my $ln = $line;
212922f2a2efSAndy Whitcroft## 			while ($ln =~ s/\([^\(\)]*\)//g) {
213022f2a2efSAndy Whitcroft## 			}
213122f2a2efSAndy Whitcroft## 			if ($ln =~ /,/) {
213222f2a2efSAndy Whitcroft## 				WARN("declaring multiple variables together should be avoided\n" . $herecurr);
213322f2a2efSAndy Whitcroft## 			}
213422f2a2efSAndy Whitcroft## 		}
2135f0a594c1SAndy Whitcroft
21360a920b5bSAndy Whitcroft#need space before brace following if, while, etc
213722f2a2efSAndy Whitcroft		if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
213822f2a2efSAndy Whitcroft		    $line =~ /do{/) {
2139773647a0SAndy Whitcroft			ERROR("space required before the open brace '{'\n" . $herecurr);
2140de7d4f0eSAndy Whitcroft		}
2141de7d4f0eSAndy Whitcroft
2142de7d4f0eSAndy Whitcroft# closing brace should have a space following it when it has anything
2143de7d4f0eSAndy Whitcroft# on the line
2144de7d4f0eSAndy Whitcroft		if ($line =~ /}(?!(?:,|;|\)))\S/) {
2145773647a0SAndy Whitcroft			ERROR("space required after that close brace '}'\n" . $herecurr);
21460a920b5bSAndy Whitcroft		}
21470a920b5bSAndy Whitcroft
214822f2a2efSAndy Whitcroft# check spacing on square brackets
214922f2a2efSAndy Whitcroft		if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
2150773647a0SAndy Whitcroft			ERROR("space prohibited after that open square bracket '['\n" . $herecurr);
215122f2a2efSAndy Whitcroft		}
215222f2a2efSAndy Whitcroft		if ($line =~ /\s\]/) {
2153773647a0SAndy Whitcroft			ERROR("space prohibited before that close square bracket ']'\n" . $herecurr);
215422f2a2efSAndy Whitcroft		}
215522f2a2efSAndy Whitcroft
2156c45dcabdSAndy Whitcroft# check spacing on parentheses
21579c0ca6f9SAndy Whitcroft		if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
21589c0ca6f9SAndy Whitcroft		    $line !~ /for\s*\(\s+;/) {
2159773647a0SAndy Whitcroft			ERROR("space prohibited after that open parenthesis '('\n" . $herecurr);
216022f2a2efSAndy Whitcroft		}
216113214adfSAndy Whitcroft		if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
2162c45dcabdSAndy Whitcroft		    $line !~ /for\s*\(.*;\s+\)/ &&
2163c45dcabdSAndy Whitcroft		    $line !~ /:\s+\)/) {
2164773647a0SAndy Whitcroft			ERROR("space prohibited before that close parenthesis ')'\n" . $herecurr);
216522f2a2efSAndy Whitcroft		}
216622f2a2efSAndy Whitcroft
21670a920b5bSAndy Whitcroft#goto labels aren't indented, allow a single space however
21684a0df2efSAndy Whitcroft		if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
21690a920b5bSAndy Whitcroft		   !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
2170de7d4f0eSAndy Whitcroft			WARN("labels should not be indented\n" . $herecurr);
21710a920b5bSAndy Whitcroft		}
21720a920b5bSAndy Whitcroft
2173c45dcabdSAndy Whitcroft# Return is not a function.
2174c45dcabdSAndy Whitcroft		if (defined($stat) && $stat =~ /^.\s*return(\s*)(\(.*);/s) {
2175c45dcabdSAndy Whitcroft			my $spacing = $1;
2176c45dcabdSAndy Whitcroft			my $value = $2;
2177c45dcabdSAndy Whitcroft
217886f9d059SAndy Whitcroft			# Flatten any parentheses
2179fee61c47SAndy Whitcroft			$value =~ s/\)\(/\) \(/g;
218063f17f89SAndy Whitcroft			while ($value =~ s/\[[^\{\}]*\]/1/ ||
218163f17f89SAndy Whitcroft			       $value !~ /(?:$Ident|-?$Constant)\s*
218263f17f89SAndy Whitcroft					     $Compare\s*
218363f17f89SAndy Whitcroft					     (?:$Ident|-?$Constant)/x &&
218463f17f89SAndy Whitcroft			       $value =~ s/\([^\(\)]*\)/1/) {
2185c45dcabdSAndy Whitcroft			}
2186c45dcabdSAndy Whitcroft
2187c45dcabdSAndy Whitcroft			if ($value =~ /^(?:$Ident|-?$Constant)$/) {
2188c45dcabdSAndy Whitcroft				ERROR("return is not a function, parentheses are not required\n" . $herecurr);
2189c45dcabdSAndy Whitcroft
2190c45dcabdSAndy Whitcroft			} elsif ($spacing !~ /\s+/) {
2191c45dcabdSAndy Whitcroft				ERROR("space required before the open parenthesis '('\n" . $herecurr);
2192c45dcabdSAndy Whitcroft			}
2193c45dcabdSAndy Whitcroft		}
2194c45dcabdSAndy Whitcroft
21950a920b5bSAndy Whitcroft# Need a space before open parenthesis after if, while etc
21964a0df2efSAndy Whitcroft		if ($line=~/\b(if|while|for|switch)\(/) {
2197773647a0SAndy Whitcroft			ERROR("space required before the open parenthesis '('\n" . $herecurr);
21980a920b5bSAndy Whitcroft		}
21990a920b5bSAndy Whitcroft
2200f5fe35ddSAndy Whitcroft# Check for illegal assignment in if conditional -- and check for trailing
2201f5fe35ddSAndy Whitcroft# statements after the conditional.
2202170d3a22SAndy Whitcroft		if ($line =~ /do\s*(?!{)/) {
2203170d3a22SAndy Whitcroft			my ($stat_next) = ctx_statement_block($line_nr_next,
2204170d3a22SAndy Whitcroft						$remain_next, $off_next);
2205170d3a22SAndy Whitcroft			$stat_next =~ s/\n./\n /g;
2206170d3a22SAndy Whitcroft			##print "stat<$stat> stat_next<$stat_next>\n";
2207170d3a22SAndy Whitcroft
2208170d3a22SAndy Whitcroft			if ($stat_next =~ /^\s*while\b/) {
2209170d3a22SAndy Whitcroft				# If the statement carries leading newlines,
2210170d3a22SAndy Whitcroft				# then count those as offsets.
2211170d3a22SAndy Whitcroft				my ($whitespace) =
2212170d3a22SAndy Whitcroft					($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
2213170d3a22SAndy Whitcroft				my $offset =
2214170d3a22SAndy Whitcroft					statement_rawlines($whitespace) - 1;
2215170d3a22SAndy Whitcroft
2216170d3a22SAndy Whitcroft				$suppress_whiletrailers{$line_nr_next +
2217170d3a22SAndy Whitcroft								$offset} = 1;
2218170d3a22SAndy Whitcroft			}
2219170d3a22SAndy Whitcroft		}
2220170d3a22SAndy Whitcroft		if (!defined $suppress_whiletrailers{$linenr} &&
2221170d3a22SAndy Whitcroft		    $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
2222171ae1a4SAndy Whitcroft			my ($s, $c) = ($stat, $cond);
22238905a67cSAndy Whitcroft
2224b53c8e10SAndy Whitcroft			if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
2225c2fdda0dSAndy Whitcroft				ERROR("do not use assignment in if condition\n" . $herecurr);
22268905a67cSAndy Whitcroft			}
22278905a67cSAndy Whitcroft
22288905a67cSAndy Whitcroft			# Find out what is on the end of the line after the
22298905a67cSAndy Whitcroft			# conditional.
2230773647a0SAndy Whitcroft			substr($s, 0, length($c), '');
22318905a67cSAndy Whitcroft			$s =~ s/\n.*//g;
223213214adfSAndy Whitcroft			$s =~ s/$;//g; 	# Remove any comments
223353210168SAndy Whitcroft			if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
223453210168SAndy Whitcroft			    $c !~ /}\s*while\s*/)
2235773647a0SAndy Whitcroft			{
2236bb44ad39SAndy Whitcroft				# Find out how long the conditional actually is.
2237bb44ad39SAndy Whitcroft				my @newlines = ($c =~ /\n/gs);
2238bb44ad39SAndy Whitcroft				my $cond_lines = 1 + $#newlines;
223942bdf74cSHidetoshi Seto				my $stat_real = '';
2240bb44ad39SAndy Whitcroft
224142bdf74cSHidetoshi Seto				$stat_real = raw_line($linenr, $cond_lines)
224242bdf74cSHidetoshi Seto							. "\n" if ($cond_lines);
2243bb44ad39SAndy Whitcroft				if (defined($stat_real) && $cond_lines > 1) {
2244bb44ad39SAndy Whitcroft					$stat_real = "[...]\n$stat_real";
2245bb44ad39SAndy Whitcroft				}
2246bb44ad39SAndy Whitcroft
2247bb44ad39SAndy Whitcroft				ERROR("trailing statements should be on next line\n" . $herecurr . $stat_real);
22488905a67cSAndy Whitcroft			}
22498905a67cSAndy Whitcroft		}
22508905a67cSAndy Whitcroft
225113214adfSAndy Whitcroft# Check for bitwise tests written as boolean
225213214adfSAndy Whitcroft		if ($line =~ /
225313214adfSAndy Whitcroft			(?:
225413214adfSAndy Whitcroft				(?:\[|\(|\&\&|\|\|)
225513214adfSAndy Whitcroft				\s*0[xX][0-9]+\s*
225613214adfSAndy Whitcroft				(?:\&\&|\|\|)
225713214adfSAndy Whitcroft			|
225813214adfSAndy Whitcroft				(?:\&\&|\|\|)
225913214adfSAndy Whitcroft				\s*0[xX][0-9]+\s*
226013214adfSAndy Whitcroft				(?:\&\&|\|\||\)|\])
226113214adfSAndy Whitcroft			)/x)
226213214adfSAndy Whitcroft		{
226313214adfSAndy Whitcroft			WARN("boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
226413214adfSAndy Whitcroft		}
226513214adfSAndy Whitcroft
22668905a67cSAndy Whitcroft# if and else should not have general statements after it
226713214adfSAndy Whitcroft		if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
226813214adfSAndy Whitcroft			my $s = $1;
226913214adfSAndy Whitcroft			$s =~ s/$;//g; 	# Remove any comments
227013214adfSAndy Whitcroft			if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
22718905a67cSAndy Whitcroft				ERROR("trailing statements should be on next line\n" . $herecurr);
22720a920b5bSAndy Whitcroft			}
227313214adfSAndy Whitcroft		}
227439667782SAndy Whitcroft# if should not continue a brace
227539667782SAndy Whitcroft		if ($line =~ /}\s*if\b/) {
227639667782SAndy Whitcroft			ERROR("trailing statements should be on next line\n" .
227739667782SAndy Whitcroft				$herecurr);
227839667782SAndy Whitcroft		}
2279a1080bf8SAndy Whitcroft# case and default should not have general statements after them
2280a1080bf8SAndy Whitcroft		if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
2281a1080bf8SAndy Whitcroft		    $line !~ /\G(?:
22823fef12d6SAndy Whitcroft			(?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
2283a1080bf8SAndy Whitcroft			\s*return\s+
2284a1080bf8SAndy Whitcroft		    )/xg)
2285a1080bf8SAndy Whitcroft		{
2286a1080bf8SAndy Whitcroft			ERROR("trailing statements should be on next line\n" . $herecurr);
2287a1080bf8SAndy Whitcroft		}
22880a920b5bSAndy Whitcroft
22890a920b5bSAndy Whitcroft		# Check for }<nl>else {, these must be at the same
22900a920b5bSAndy Whitcroft		# indent level to be relevant to each other.
22910a920b5bSAndy Whitcroft		if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
22920a920b5bSAndy Whitcroft						$previndent == $indent) {
2293de7d4f0eSAndy Whitcroft			ERROR("else should follow close brace '}'\n" . $hereprev);
22940a920b5bSAndy Whitcroft		}
22950a920b5bSAndy Whitcroft
2296c2fdda0dSAndy Whitcroft		if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and
2297c2fdda0dSAndy Whitcroft						$previndent == $indent) {
2298c2fdda0dSAndy Whitcroft			my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
2299c2fdda0dSAndy Whitcroft
2300c2fdda0dSAndy Whitcroft			# Find out what is on the end of the line after the
2301c2fdda0dSAndy Whitcroft			# conditional.
2302773647a0SAndy Whitcroft			substr($s, 0, length($c), '');
2303c2fdda0dSAndy Whitcroft			$s =~ s/\n.*//g;
2304c2fdda0dSAndy Whitcroft
2305c2fdda0dSAndy Whitcroft			if ($s =~ /^\s*;/) {
2306c2fdda0dSAndy Whitcroft				ERROR("while should follow close brace '}'\n" . $hereprev);
2307c2fdda0dSAndy Whitcroft			}
2308c2fdda0dSAndy Whitcroft		}
2309c2fdda0dSAndy Whitcroft
23100a920b5bSAndy Whitcroft#studly caps, commented out until figure out how to distinguish between use of existing and adding new
23110a920b5bSAndy Whitcroft#		if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) {
23120a920b5bSAndy Whitcroft#		    print "No studly caps, use _\n";
23130a920b5bSAndy Whitcroft#		    print "$herecurr";
23140a920b5bSAndy Whitcroft#		    $clean = 0;
23150a920b5bSAndy Whitcroft#		}
23160a920b5bSAndy Whitcroft
23170a920b5bSAndy Whitcroft#no spaces allowed after \ in define
2318c45dcabdSAndy Whitcroft		if ($line=~/\#\s*define.*\\\s$/) {
2319de7d4f0eSAndy Whitcroft			WARN("Whitepspace after \\ makes next lines useless\n" . $herecurr);
23200a920b5bSAndy Whitcroft		}
23210a920b5bSAndy Whitcroft
2322653d4876SAndy Whitcroft#warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
2323c45dcabdSAndy Whitcroft		if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
2324e09dec48SAndy Whitcroft			my $file = "$1.h";
2325e09dec48SAndy Whitcroft			my $checkfile = "include/linux/$file";
2326e09dec48SAndy Whitcroft			if (-f "$root/$checkfile" &&
2327e09dec48SAndy Whitcroft			    $realfile ne $checkfile &&
23287840a94cSWolfram Sang			    $1 !~ /$allowed_asm_includes/)
2329c45dcabdSAndy Whitcroft			{
2330e09dec48SAndy Whitcroft				if ($realfile =~ m{^arch/}) {
2331e09dec48SAndy Whitcroft					CHK("Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
2332e09dec48SAndy Whitcroft				} else {
2333e09dec48SAndy Whitcroft					WARN("Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
2334e09dec48SAndy Whitcroft				}
23350a920b5bSAndy Whitcroft			}
23360a920b5bSAndy Whitcroft		}
23370a920b5bSAndy Whitcroft
2338653d4876SAndy Whitcroft# multi-statement macros should be enclosed in a do while loop, grab the
2339653d4876SAndy Whitcroft# first statement and ensure its the whole macro if its not enclosed
2340cf655043SAndy Whitcroft# in a known good container
2341b8f96a31SAndy Whitcroft		if ($realfile !~ m@/vmlinux.lds.h$@ &&
2342b8f96a31SAndy Whitcroft		    $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
2343d8aaf121SAndy Whitcroft			my $ln = $linenr;
2344d8aaf121SAndy Whitcroft			my $cnt = $realcnt;
2345c45dcabdSAndy Whitcroft			my ($off, $dstat, $dcond, $rest);
2346c45dcabdSAndy Whitcroft			my $ctx = '';
2347653d4876SAndy Whitcroft
2348c45dcabdSAndy Whitcroft			my $args = defined($1);
2349de7d4f0eSAndy Whitcroft
2350c45dcabdSAndy Whitcroft			# Find the end of the macro and limit our statement
2351c45dcabdSAndy Whitcroft			# search to that.
2352c45dcabdSAndy Whitcroft			while ($cnt > 0 && defined $lines[$ln - 1] &&
2353c45dcabdSAndy Whitcroft				$lines[$ln - 1] =~ /^(?:-|..*\\$)/)
2354c45dcabdSAndy Whitcroft			{
2355c45dcabdSAndy Whitcroft				$ctx .= $rawlines[$ln - 1] . "\n";
2356a3bb97a7SAndy Whitcroft				$cnt-- if ($lines[$ln - 1] !~ /^-/);
2357c45dcabdSAndy Whitcroft				$ln++;
2358de7d4f0eSAndy Whitcroft			}
2359c45dcabdSAndy Whitcroft			$ctx .= $rawlines[$ln - 1];
2360d8aaf121SAndy Whitcroft
2361c45dcabdSAndy Whitcroft			($dstat, $dcond, $ln, $cnt, $off) =
2362c45dcabdSAndy Whitcroft				ctx_statement_block($linenr, $ln - $linenr + 1, 0);
2363c45dcabdSAndy Whitcroft			#print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
2364a3bb97a7SAndy Whitcroft			#print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
2365c45dcabdSAndy Whitcroft
2366c45dcabdSAndy Whitcroft			# Extract the remainder of the define (if any) and
2367c45dcabdSAndy Whitcroft			# rip off surrounding spaces, and trailing \'s.
2368c45dcabdSAndy Whitcroft			$rest = '';
2369636d140aSAndy Whitcroft			while ($off != 0 || ($cnt > 0 && $rest =~ /\\\s*$/)) {
2370636d140aSAndy Whitcroft				#print "ADDING cnt<$cnt> $off <" . substr($lines[$ln - 1], $off) . "> rest<$rest>\n";
2371a3bb97a7SAndy Whitcroft				if ($off != 0 || $lines[$ln - 1] !~ /^-/) {
2372c45dcabdSAndy Whitcroft					$rest .= substr($lines[$ln - 1], $off) . "\n";
2373c45dcabdSAndy Whitcroft					$cnt--;
2374a3bb97a7SAndy Whitcroft				}
2375a3bb97a7SAndy Whitcroft				$ln++;
2376c45dcabdSAndy Whitcroft				$off = 0;
2377c45dcabdSAndy Whitcroft			}
2378c45dcabdSAndy Whitcroft			$rest =~ s/\\\n.//g;
2379c45dcabdSAndy Whitcroft			$rest =~ s/^\s*//s;
2380c45dcabdSAndy Whitcroft			$rest =~ s/\s*$//s;
2381c45dcabdSAndy Whitcroft
2382c45dcabdSAndy Whitcroft			# Clean up the original statement.
2383c45dcabdSAndy Whitcroft			if ($args) {
2384c45dcabdSAndy Whitcroft				substr($dstat, 0, length($dcond), '');
2385d8aaf121SAndy Whitcroft			} else {
2386c45dcabdSAndy Whitcroft				$dstat =~ s/^.\s*\#\s*define\s+$Ident\s*//;
2387c45dcabdSAndy Whitcroft			}
2388292f1a9bSAndy Whitcroft			$dstat =~ s/$;//g;
2389c45dcabdSAndy Whitcroft			$dstat =~ s/\\\n.//g;
2390c45dcabdSAndy Whitcroft			$dstat =~ s/^\s*//s;
2391c45dcabdSAndy Whitcroft			$dstat =~ s/\s*$//s;
2392c45dcabdSAndy Whitcroft
2393c45dcabdSAndy Whitcroft			# Flatten any parentheses and braces
2394bf30d6edSAndy Whitcroft			while ($dstat =~ s/\([^\(\)]*\)/1/ ||
2395bf30d6edSAndy Whitcroft			       $dstat =~ s/\{[^\{\}]*\}/1/ ||
2396bf30d6edSAndy Whitcroft			       $dstat =~ s/\[[^\{\}]*\]/1/)
2397bf30d6edSAndy Whitcroft			{
2398c45dcabdSAndy Whitcroft			}
2399c45dcabdSAndy Whitcroft
2400c45dcabdSAndy Whitcroft			my $exceptions = qr{
2401c45dcabdSAndy Whitcroft				$Declare|
2402c45dcabdSAndy Whitcroft				module_param_named|
2403c45dcabdSAndy Whitcroft				MODULE_PARAM_DESC|
2404c45dcabdSAndy Whitcroft				DECLARE_PER_CPU|
2405c45dcabdSAndy Whitcroft				DEFINE_PER_CPU|
2406383099fdSAndy Whitcroft				__typeof__\(|
240722fd2d3eSStefani Seibold				union|
240822fd2d3eSStefani Seibold				struct|
2409ea71a0a0SAndy Whitcroft				\.$Ident\s*=\s*|
2410ea71a0a0SAndy Whitcroft				^\"|\"$
2411c45dcabdSAndy Whitcroft			}x;
2412383099fdSAndy Whitcroft			#print "REST<$rest> dstat<$dstat>\n";
2413c45dcabdSAndy Whitcroft			if ($rest ne '') {
2414c45dcabdSAndy Whitcroft				if ($rest !~ /while\s*\(/ &&
2415c45dcabdSAndy Whitcroft				    $dstat !~ /$exceptions/)
2416c45dcabdSAndy Whitcroft				{
2417c45dcabdSAndy Whitcroft					ERROR("Macros with multiple statements should be enclosed in a do - while loop\n" . "$here\n$ctx\n");
2418c45dcabdSAndy Whitcroft				}
2419c45dcabdSAndy Whitcroft
2420c45dcabdSAndy Whitcroft			} elsif ($ctx !~ /;/) {
2421c45dcabdSAndy Whitcroft				if ($dstat ne '' &&
2422c45dcabdSAndy Whitcroft				    $dstat !~ /^(?:$Ident|-?$Constant)$/ &&
2423c45dcabdSAndy Whitcroft				    $dstat !~ /$exceptions/ &&
2424b132e5d5SAndy Whitcroft				    $dstat !~ /^\.$Ident\s*=/ &&
2425c45dcabdSAndy Whitcroft				    $dstat =~ /$Operators/)
2426c45dcabdSAndy Whitcroft				{
2427de7d4f0eSAndy Whitcroft					ERROR("Macros with complex values should be enclosed in parenthesis\n" . "$here\n$ctx\n");
2428d8aaf121SAndy Whitcroft				}
24290a920b5bSAndy Whitcroft			}
2430653d4876SAndy Whitcroft		}
24310a920b5bSAndy Whitcroft
2432080ba929SMike Frysinger# make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
2433080ba929SMike Frysinger# all assignments may have only one of the following with an assignment:
2434080ba929SMike Frysinger#	.
2435080ba929SMike Frysinger#	ALIGN(...)
2436080ba929SMike Frysinger#	VMLINUX_SYMBOL(...)
2437080ba929SMike Frysinger		if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
2438080ba929SMike Frysinger			WARN("vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
2439080ba929SMike Frysinger		}
2440080ba929SMike Frysinger
2441f0a594c1SAndy Whitcroft# check for redundant bracing round if etc
244213214adfSAndy Whitcroft		if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
244313214adfSAndy Whitcroft			my ($level, $endln, @chunks) =
2444cf655043SAndy Whitcroft				ctx_statement_full($linenr, $realcnt, 1);
244513214adfSAndy Whitcroft			#print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
2446cf655043SAndy Whitcroft			#print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
2447cf655043SAndy Whitcroft			if ($#chunks > 0 && $level == 0) {
244813214adfSAndy Whitcroft				my $allowed = 0;
244913214adfSAndy Whitcroft				my $seen = 0;
2450773647a0SAndy Whitcroft				my $herectx = $here . "\n";
2451cf655043SAndy Whitcroft				my $ln = $linenr - 1;
245213214adfSAndy Whitcroft				for my $chunk (@chunks) {
245313214adfSAndy Whitcroft					my ($cond, $block) = @{$chunk};
245413214adfSAndy Whitcroft
2455773647a0SAndy Whitcroft					# If the condition carries leading newlines, then count those as offsets.
2456773647a0SAndy Whitcroft					my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
2457773647a0SAndy Whitcroft					my $offset = statement_rawlines($whitespace) - 1;
2458773647a0SAndy Whitcroft
2459773647a0SAndy Whitcroft					#print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
2460773647a0SAndy Whitcroft
2461773647a0SAndy Whitcroft					# We have looked at and allowed this specific line.
2462773647a0SAndy Whitcroft					$suppress_ifbraces{$ln + $offset} = 1;
2463773647a0SAndy Whitcroft
2464773647a0SAndy Whitcroft					$herectx .= "$rawlines[$ln + $offset]\n[...]\n";
2465cf655043SAndy Whitcroft					$ln += statement_rawlines($block) - 1;
2466cf655043SAndy Whitcroft
2467773647a0SAndy Whitcroft					substr($block, 0, length($cond), '');
246813214adfSAndy Whitcroft
246913214adfSAndy Whitcroft					$seen++ if ($block =~ /^\s*{/);
247013214adfSAndy Whitcroft
2471cf655043SAndy Whitcroft					#print "cond<$cond> block<$block> allowed<$allowed>\n";
2472cf655043SAndy Whitcroft					if (statement_lines($cond) > 1) {
2473cf655043SAndy Whitcroft						#print "APW: ALLOWED: cond<$cond>\n";
247413214adfSAndy Whitcroft						$allowed = 1;
247513214adfSAndy Whitcroft					}
247613214adfSAndy Whitcroft					if ($block =~/\b(?:if|for|while)\b/) {
2477cf655043SAndy Whitcroft						#print "APW: ALLOWED: block<$block>\n";
247813214adfSAndy Whitcroft						$allowed = 1;
247913214adfSAndy Whitcroft					}
2480cf655043SAndy Whitcroft					if (statement_block_size($block) > 1) {
2481cf655043SAndy Whitcroft						#print "APW: ALLOWED: lines block<$block>\n";
248213214adfSAndy Whitcroft						$allowed = 1;
248313214adfSAndy Whitcroft					}
248413214adfSAndy Whitcroft				}
248513214adfSAndy Whitcroft				if ($seen && !$allowed) {
2486cf655043SAndy Whitcroft					WARN("braces {} are not necessary for any arm of this statement\n" . $herectx);
248713214adfSAndy Whitcroft				}
248813214adfSAndy Whitcroft			}
248913214adfSAndy Whitcroft		}
2490773647a0SAndy Whitcroft		if (!defined $suppress_ifbraces{$linenr - 1} &&
249113214adfSAndy Whitcroft					$line =~ /\b(if|while|for|else)\b/) {
2492cf655043SAndy Whitcroft			my $allowed = 0;
2493f0a594c1SAndy Whitcroft
2494cf655043SAndy Whitcroft			# Check the pre-context.
2495cf655043SAndy Whitcroft			if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
2496cf655043SAndy Whitcroft				#print "APW: ALLOWED: pre<$1>\n";
2497cf655043SAndy Whitcroft				$allowed = 1;
2498f0a594c1SAndy Whitcroft			}
2499773647a0SAndy Whitcroft
2500773647a0SAndy Whitcroft			my ($level, $endln, @chunks) =
2501773647a0SAndy Whitcroft				ctx_statement_full($linenr, $realcnt, $-[0]);
2502773647a0SAndy Whitcroft
2503cf655043SAndy Whitcroft			# Check the condition.
2504cf655043SAndy Whitcroft			my ($cond, $block) = @{$chunks[0]};
2505773647a0SAndy Whitcroft			#print "CHECKING<$linenr> cond<$cond> block<$block>\n";
2506cf655043SAndy Whitcroft			if (defined $cond) {
2507773647a0SAndy Whitcroft				substr($block, 0, length($cond), '');
2508cf655043SAndy Whitcroft			}
2509cf655043SAndy Whitcroft			if (statement_lines($cond) > 1) {
2510cf655043SAndy Whitcroft				#print "APW: ALLOWED: cond<$cond>\n";
2511cf655043SAndy Whitcroft				$allowed = 1;
2512cf655043SAndy Whitcroft			}
2513cf655043SAndy Whitcroft			if ($block =~/\b(?:if|for|while)\b/) {
2514cf655043SAndy Whitcroft				#print "APW: ALLOWED: block<$block>\n";
2515cf655043SAndy Whitcroft				$allowed = 1;
2516cf655043SAndy Whitcroft			}
2517cf655043SAndy Whitcroft			if (statement_block_size($block) > 1) {
2518cf655043SAndy Whitcroft				#print "APW: ALLOWED: lines block<$block>\n";
2519cf655043SAndy Whitcroft				$allowed = 1;
2520cf655043SAndy Whitcroft			}
2521cf655043SAndy Whitcroft			# Check the post-context.
2522cf655043SAndy Whitcroft			if (defined $chunks[1]) {
2523cf655043SAndy Whitcroft				my ($cond, $block) = @{$chunks[1]};
2524cf655043SAndy Whitcroft				if (defined $cond) {
2525773647a0SAndy Whitcroft					substr($block, 0, length($cond), '');
2526cf655043SAndy Whitcroft				}
2527cf655043SAndy Whitcroft				if ($block =~ /^\s*\{/) {
2528cf655043SAndy Whitcroft					#print "APW: ALLOWED: chunk-1 block<$block>\n";
2529cf655043SAndy Whitcroft					$allowed = 1;
2530cf655043SAndy Whitcroft				}
2531cf655043SAndy Whitcroft			}
2532cf655043SAndy Whitcroft			if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
2533cf655043SAndy Whitcroft				my $herectx = $here . "\n";;
2534f055663cSAndy Whitcroft				my $cnt = statement_rawlines($block);
2535cf655043SAndy Whitcroft
2536f055663cSAndy Whitcroft				for (my $n = 0; $n < $cnt; $n++) {
2537f055663cSAndy Whitcroft					$herectx .= raw_line($linenr, $n) . "\n";;
2538cf655043SAndy Whitcroft				}
2539cf655043SAndy Whitcroft
2540cf655043SAndy Whitcroft				WARN("braces {} are not necessary for single statement blocks\n" . $herectx);
2541f0a594c1SAndy Whitcroft			}
2542f0a594c1SAndy Whitcroft		}
2543f0a594c1SAndy Whitcroft
2544653d4876SAndy Whitcroft# don't include deprecated include files (uses RAW line)
25454a0df2efSAndy Whitcroft		for my $inc (@dep_includes) {
2546c45dcabdSAndy Whitcroft			if ($rawline =~ m@^.\s*\#\s*include\s*\<$inc>@) {
2547de7d4f0eSAndy Whitcroft				ERROR("Don't use <$inc>: see Documentation/feature-removal-schedule.txt\n" . $herecurr);
25480a920b5bSAndy Whitcroft			}
25490a920b5bSAndy Whitcroft		}
25500a920b5bSAndy Whitcroft
25514a0df2efSAndy Whitcroft# don't use deprecated functions
25524a0df2efSAndy Whitcroft		for my $func (@dep_functions) {
255300df344fSAndy Whitcroft			if ($line =~ /\b$func\b/) {
2554de7d4f0eSAndy Whitcroft				ERROR("Don't use $func(): see Documentation/feature-removal-schedule.txt\n" . $herecurr);
25554a0df2efSAndy Whitcroft			}
25564a0df2efSAndy Whitcroft		}
25574a0df2efSAndy Whitcroft
25584a0df2efSAndy Whitcroft# no volatiles please
25596c72ffaaSAndy Whitcroft		my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
25606c72ffaaSAndy Whitcroft		if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
2561de7d4f0eSAndy Whitcroft			WARN("Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
25624a0df2efSAndy Whitcroft		}
25634a0df2efSAndy Whitcroft
25649c0ca6f9SAndy Whitcroft# SPIN_LOCK_UNLOCKED & RW_LOCK_UNLOCKED are deprecated
25659c0ca6f9SAndy Whitcroft		if ($line =~ /\b(SPIN_LOCK_UNLOCKED|RW_LOCK_UNLOCKED)/) {
25669c0ca6f9SAndy Whitcroft			ERROR("Use of $1 is deprecated: see Documentation/spinlocks.txt\n" . $herecurr);
25679c0ca6f9SAndy Whitcroft		}
25689c0ca6f9SAndy Whitcroft
256900df344fSAndy Whitcroft# warn about #if 0
2570c45dcabdSAndy Whitcroft		if ($line =~ /^.\s*\#\s*if\s+0\b/) {
2571de7d4f0eSAndy Whitcroft			CHK("if this code is redundant consider removing it\n" .
2572de7d4f0eSAndy Whitcroft				$herecurr);
25734a0df2efSAndy Whitcroft		}
25744a0df2efSAndy Whitcroft
2575f0a594c1SAndy Whitcroft# check for needless kfree() checks
2576f0a594c1SAndy Whitcroft		if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
2577f0a594c1SAndy Whitcroft			my $expr = $1;
2578f0a594c1SAndy Whitcroft			if ($line =~ /\bkfree\(\Q$expr\E\);/) {
25793c232147SWolfram Sang				WARN("kfree(NULL) is safe this check is probably not required\n" . $hereprev);
2580f0a594c1SAndy Whitcroft			}
2581f0a594c1SAndy Whitcroft		}
25824c432a8fSGreg Kroah-Hartman# check for needless usb_free_urb() checks
25834c432a8fSGreg Kroah-Hartman		if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
25844c432a8fSGreg Kroah-Hartman			my $expr = $1;
25854c432a8fSGreg Kroah-Hartman			if ($line =~ /\busb_free_urb\(\Q$expr\E\);/) {
25864c432a8fSGreg Kroah-Hartman				WARN("usb_free_urb(NULL) is safe this check is probably not required\n" . $hereprev);
25874c432a8fSGreg Kroah-Hartman			}
25884c432a8fSGreg Kroah-Hartman		}
2589f0a594c1SAndy Whitcroft
25901a15a250SPatrick Pannuto# prefer usleep_range over udelay
25911a15a250SPatrick Pannuto		if ($line =~ /\budelay\s*\(\s*(\w+)\s*\)/) {
25921a15a250SPatrick Pannuto			# ignore udelay's < 10, however
25931a15a250SPatrick Pannuto			if (! (($1 =~ /(\d+)/) && ($1 < 10)) ) {
25941a15a250SPatrick Pannuto				CHK("usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $line);
25951a15a250SPatrick Pannuto			}
25961a15a250SPatrick Pannuto		}
25971a15a250SPatrick Pannuto
259809ef8725SPatrick Pannuto# warn about unexpectedly long msleep's
259909ef8725SPatrick Pannuto		if ($line =~ /\bmsleep\s*\((\d+)\);/) {
260009ef8725SPatrick Pannuto			if ($1 < 20) {
260109ef8725SPatrick Pannuto				WARN("msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $line);
260209ef8725SPatrick Pannuto			}
260309ef8725SPatrick Pannuto		}
260409ef8725SPatrick Pannuto
260500df344fSAndy Whitcroft# warn about #ifdefs in C files
2606c45dcabdSAndy Whitcroft#		if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
260700df344fSAndy Whitcroft#			print "#ifdef in C files should be avoided\n";
260800df344fSAndy Whitcroft#			print "$herecurr";
260900df344fSAndy Whitcroft#			$clean = 0;
261000df344fSAndy Whitcroft#		}
261100df344fSAndy Whitcroft
261222f2a2efSAndy Whitcroft# warn about spacing in #ifdefs
2613c45dcabdSAndy Whitcroft		if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
261422f2a2efSAndy Whitcroft			ERROR("exactly one space required after that #$1\n" . $herecurr);
261522f2a2efSAndy Whitcroft		}
261622f2a2efSAndy Whitcroft
26174a0df2efSAndy Whitcroft# check for spinlock_t definitions without a comment.
2618171ae1a4SAndy Whitcroft		if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
2619171ae1a4SAndy Whitcroft		    $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
26204a0df2efSAndy Whitcroft			my $which = $1;
26214a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
2622de7d4f0eSAndy Whitcroft				CHK("$1 definition without comment\n" . $herecurr);
26234a0df2efSAndy Whitcroft			}
26244a0df2efSAndy Whitcroft		}
26254a0df2efSAndy Whitcroft# check for memory barriers without a comment.
26264a0df2efSAndy Whitcroft		if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
26274a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
2628de7d4f0eSAndy Whitcroft				CHK("memory barrier without comment\n" . $herecurr);
26294a0df2efSAndy Whitcroft			}
26304a0df2efSAndy Whitcroft		}
26314a0df2efSAndy Whitcroft# check of hardware specific defines
2632c45dcabdSAndy Whitcroft		if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
2633de7d4f0eSAndy Whitcroft			CHK("architecture specific defines should be avoided\n" .  $herecurr);
26340a920b5bSAndy Whitcroft		}
2635653d4876SAndy Whitcroft
2636d4977c78STobias Klauser# Check that the storage class is at the beginning of a declaration
2637d4977c78STobias Klauser		if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
2638d4977c78STobias Klauser			WARN("storage class should be at the beginning of the declaration\n" . $herecurr)
2639d4977c78STobias Klauser		}
2640d4977c78STobias Klauser
2641de7d4f0eSAndy Whitcroft# check the location of the inline attribute, that it is between
2642de7d4f0eSAndy Whitcroft# storage class and type.
26439c0ca6f9SAndy Whitcroft		if ($line =~ /\b$Type\s+$Inline\b/ ||
26449c0ca6f9SAndy Whitcroft		    $line =~ /\b$Inline\s+$Storage\b/) {
2645de7d4f0eSAndy Whitcroft			ERROR("inline keyword should sit between storage class and type\n" . $herecurr);
2646de7d4f0eSAndy Whitcroft		}
2647de7d4f0eSAndy Whitcroft
26488905a67cSAndy Whitcroft# Check for __inline__ and __inline, prefer inline
26498905a67cSAndy Whitcroft		if ($line =~ /\b(__inline__|__inline)\b/) {
26508905a67cSAndy Whitcroft			WARN("plain inline is preferred over $1\n" . $herecurr);
26518905a67cSAndy Whitcroft		}
26528905a67cSAndy Whitcroft
26538f53a9b8SJoe Perches# check for sizeof(&)
26548f53a9b8SJoe Perches		if ($line =~ /\bsizeof\s*\(\s*\&/) {
26558f53a9b8SJoe Perches			WARN("sizeof(& should be avoided\n" . $herecurr);
26568f53a9b8SJoe Perches		}
26578f53a9b8SJoe Perches
2658de7d4f0eSAndy Whitcroft# check for new externs in .c files.
2659171ae1a4SAndy Whitcroft		if ($realfile =~ /\.c$/ && defined $stat &&
2660c45dcabdSAndy Whitcroft		    $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
2661171ae1a4SAndy Whitcroft		{
2662c45dcabdSAndy Whitcroft			my $function_name = $1;
2663c45dcabdSAndy Whitcroft			my $paren_space = $2;
2664171ae1a4SAndy Whitcroft
2665171ae1a4SAndy Whitcroft			my $s = $stat;
2666171ae1a4SAndy Whitcroft			if (defined $cond) {
2667171ae1a4SAndy Whitcroft				substr($s, 0, length($cond), '');
2668171ae1a4SAndy Whitcroft			}
2669c45dcabdSAndy Whitcroft			if ($s =~ /^\s*;/ &&
2670c45dcabdSAndy Whitcroft			    $function_name ne 'uninitialized_var')
2671c45dcabdSAndy Whitcroft			{
2672de7d4f0eSAndy Whitcroft				WARN("externs should be avoided in .c files\n" .  $herecurr);
2673de7d4f0eSAndy Whitcroft			}
2674de7d4f0eSAndy Whitcroft
2675171ae1a4SAndy Whitcroft			if ($paren_space =~ /\n/) {
2676171ae1a4SAndy Whitcroft				WARN("arguments for function declarations should follow identifier\n" . $herecurr);
2677171ae1a4SAndy Whitcroft			}
26789c9ba34eSAndy Whitcroft
26799c9ba34eSAndy Whitcroft		} elsif ($realfile =~ /\.c$/ && defined $stat &&
26809c9ba34eSAndy Whitcroft		    $stat =~ /^.\s*extern\s+/)
26819c9ba34eSAndy Whitcroft		{
26829c9ba34eSAndy Whitcroft			WARN("externs should be avoided in .c files\n" .  $herecurr);
2683171ae1a4SAndy Whitcroft		}
2684171ae1a4SAndy Whitcroft
2685de7d4f0eSAndy Whitcroft# checks for new __setup's
2686de7d4f0eSAndy Whitcroft		if ($rawline =~ /\b__setup\("([^"]*)"/) {
2687de7d4f0eSAndy Whitcroft			my $name = $1;
2688de7d4f0eSAndy Whitcroft
2689de7d4f0eSAndy Whitcroft			if (!grep(/$name/, @setup_docs)) {
2690de7d4f0eSAndy Whitcroft				CHK("__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
2691de7d4f0eSAndy Whitcroft			}
2692653d4876SAndy Whitcroft		}
26939c0ca6f9SAndy Whitcroft
26949c0ca6f9SAndy Whitcroft# check for pointless casting of kmalloc return
26959c0ca6f9SAndy Whitcroft		if ($line =~ /\*\s*\)\s*k[czm]alloc\b/) {
26969c0ca6f9SAndy Whitcroft			WARN("unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
26979c0ca6f9SAndy Whitcroft		}
269813214adfSAndy Whitcroft
269913214adfSAndy Whitcroft# check for gcc specific __FUNCTION__
270013214adfSAndy Whitcroft		if ($line =~ /__FUNCTION__/) {
270113214adfSAndy Whitcroft			WARN("__func__ should be used instead of gcc specific __FUNCTION__\n"  . $herecurr);
270213214adfSAndy Whitcroft		}
2703773647a0SAndy Whitcroft
2704773647a0SAndy Whitcroft# check for semaphores used as mutexes
2705171ae1a4SAndy Whitcroft		if ($line =~ /^.\s*(DECLARE_MUTEX|init_MUTEX)\s*\(/) {
2706773647a0SAndy Whitcroft			WARN("mutexes are preferred for single holder semaphores\n" . $herecurr);
2707773647a0SAndy Whitcroft		}
2708773647a0SAndy Whitcroft# check for semaphores used as mutexes
2709171ae1a4SAndy Whitcroft		if ($line =~ /^.\s*init_MUTEX_LOCKED\s*\(/) {
2710773647a0SAndy Whitcroft			WARN("consider using a completion\n" . $herecurr);
27111704f47bSPeter Zijlstra
2712773647a0SAndy Whitcroft		}
2713773647a0SAndy Whitcroft# recommend strict_strto* over simple_strto*
2714773647a0SAndy Whitcroft		if ($line =~ /\bsimple_(strto.*?)\s*\(/) {
2715773647a0SAndy Whitcroft			WARN("consider using strict_$1 in preference to simple_$1\n" . $herecurr);
2716773647a0SAndy Whitcroft		}
2717f3db6639SMichael Ellerman# check for __initcall(), use device_initcall() explicitly please
2718f3db6639SMichael Ellerman		if ($line =~ /^.\s*__initcall\s*\(/) {
2719f3db6639SMichael Ellerman			WARN("please use device_initcall() instead of __initcall()\n" . $herecurr);
2720f3db6639SMichael Ellerman		}
272179404849SEmese Revfy# check for various ops structs, ensure they are const.
272279404849SEmese Revfy		my $struct_ops = qr{acpi_dock_ops|
272379404849SEmese Revfy				address_space_operations|
272479404849SEmese Revfy				backlight_ops|
272579404849SEmese Revfy				block_device_operations|
272679404849SEmese Revfy				dentry_operations|
272779404849SEmese Revfy				dev_pm_ops|
272879404849SEmese Revfy				dma_map_ops|
272979404849SEmese Revfy				extent_io_ops|
273079404849SEmese Revfy				file_lock_operations|
273179404849SEmese Revfy				file_operations|
273279404849SEmese Revfy				hv_ops|
273379404849SEmese Revfy				ide_dma_ops|
273479404849SEmese Revfy				intel_dvo_dev_ops|
273579404849SEmese Revfy				item_operations|
273679404849SEmese Revfy				iwl_ops|
273779404849SEmese Revfy				kgdb_arch|
273879404849SEmese Revfy				kgdb_io|
273979404849SEmese Revfy				kset_uevent_ops|
274079404849SEmese Revfy				lock_manager_operations|
274179404849SEmese Revfy				microcode_ops|
274279404849SEmese Revfy				mtrr_ops|
274379404849SEmese Revfy				neigh_ops|
274479404849SEmese Revfy				nlmsvc_binding|
274579404849SEmese Revfy				pci_raw_ops|
274679404849SEmese Revfy				pipe_buf_operations|
274779404849SEmese Revfy				platform_hibernation_ops|
274879404849SEmese Revfy				platform_suspend_ops|
274979404849SEmese Revfy				proto_ops|
275079404849SEmese Revfy				rpc_pipe_ops|
275179404849SEmese Revfy				seq_operations|
275279404849SEmese Revfy				snd_ac97_build_ops|
275379404849SEmese Revfy				soc_pcmcia_socket_ops|
275479404849SEmese Revfy				stacktrace_ops|
275579404849SEmese Revfy				sysfs_ops|
275679404849SEmese Revfy				tty_operations|
275779404849SEmese Revfy				usb_mon_operations|
275879404849SEmese Revfy				wd_ops}x;
27596903ffb2SAndy Whitcroft		if ($line !~ /\bconst\b/ &&
276079404849SEmese Revfy		    $line =~ /\bstruct\s+($struct_ops)\b/) {
27616903ffb2SAndy Whitcroft			WARN("struct $1 should normally be const\n" .
27626903ffb2SAndy Whitcroft				$herecurr);
27632b6db5cbSAndy Whitcroft		}
2764773647a0SAndy Whitcroft
2765773647a0SAndy Whitcroft# use of NR_CPUS is usually wrong
2766773647a0SAndy Whitcroft# ignore definitions of NR_CPUS and usage to define arrays as likely right
2767773647a0SAndy Whitcroft		if ($line =~ /\bNR_CPUS\b/ &&
2768c45dcabdSAndy Whitcroft		    $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
2769c45dcabdSAndy Whitcroft		    $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
2770171ae1a4SAndy Whitcroft		    $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
2771171ae1a4SAndy Whitcroft		    $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
2772171ae1a4SAndy Whitcroft		    $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
2773773647a0SAndy Whitcroft		{
2774773647a0SAndy Whitcroft			WARN("usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
2775773647a0SAndy Whitcroft		}
27769c9ba34eSAndy Whitcroft
27779c9ba34eSAndy Whitcroft# check for %L{u,d,i} in strings
27789c9ba34eSAndy Whitcroft		my $string;
27799c9ba34eSAndy Whitcroft		while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
27809c9ba34eSAndy Whitcroft			$string = substr($rawline, $-[1], $+[1] - $-[1]);
27812a1bc5d5SAndy Whitcroft			$string =~ s/%%/__/g;
27829c9ba34eSAndy Whitcroft			if ($string =~ /(?<!%)%L[udi]/) {
27839c9ba34eSAndy Whitcroft				WARN("\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
27849c9ba34eSAndy Whitcroft				last;
27859c9ba34eSAndy Whitcroft			}
27869c9ba34eSAndy Whitcroft		}
2787691d77b6SAndy Whitcroft
2788691d77b6SAndy Whitcroft# whine mightly about in_atomic
2789691d77b6SAndy Whitcroft		if ($line =~ /\bin_atomic\s*\(/) {
2790691d77b6SAndy Whitcroft			if ($realfile =~ m@^drivers/@) {
2791691d77b6SAndy Whitcroft				ERROR("do not use in_atomic in drivers\n" . $herecurr);
2792f4a87736SAndy Whitcroft			} elsif ($realfile !~ m@^kernel/@) {
2793691d77b6SAndy Whitcroft				WARN("use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
2794691d77b6SAndy Whitcroft			}
2795691d77b6SAndy Whitcroft		}
27961704f47bSPeter Zijlstra
27971704f47bSPeter Zijlstra# check for lockdep_set_novalidate_class
27981704f47bSPeter Zijlstra		if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
27991704f47bSPeter Zijlstra		    $line =~ /__lockdep_no_validate__\s*\)/ ) {
28001704f47bSPeter Zijlstra			if ($realfile !~ m@^kernel/lockdep@ &&
28011704f47bSPeter Zijlstra			    $realfile !~ m@^include/linux/lockdep@ &&
28021704f47bSPeter Zijlstra			    $realfile !~ m@^drivers/base/core@) {
28031704f47bSPeter Zijlstra				ERROR("lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
28041704f47bSPeter Zijlstra			}
28051704f47bSPeter Zijlstra		}
280613214adfSAndy Whitcroft	}
280713214adfSAndy Whitcroft
280813214adfSAndy Whitcroft	# If we have no input at all, then there is nothing to report on
280913214adfSAndy Whitcroft	# so just keep quiet.
281013214adfSAndy Whitcroft	if ($#rawlines == -1) {
281113214adfSAndy Whitcroft		exit(0);
28120a920b5bSAndy Whitcroft	}
28130a920b5bSAndy Whitcroft
28148905a67cSAndy Whitcroft	# In mailback mode only produce a report in the negative, for
28158905a67cSAndy Whitcroft	# things that appear to be patches.
28168905a67cSAndy Whitcroft	if ($mailback && ($clean == 1 || !$is_patch)) {
28178905a67cSAndy Whitcroft		exit(0);
28188905a67cSAndy Whitcroft	}
28198905a67cSAndy Whitcroft
28208905a67cSAndy Whitcroft	# This is not a patch, and we are are in 'no-patch' mode so
28218905a67cSAndy Whitcroft	# just keep quiet.
28228905a67cSAndy Whitcroft	if (!$chk_patch && !$is_patch) {
28238905a67cSAndy Whitcroft		exit(0);
28248905a67cSAndy Whitcroft	}
28258905a67cSAndy Whitcroft
28268905a67cSAndy Whitcroft	if (!$is_patch) {
2827de7d4f0eSAndy Whitcroft		ERROR("Does not appear to be a unified-diff format patch\n");
28280a920b5bSAndy Whitcroft	}
28290a920b5bSAndy Whitcroft	if ($is_patch && $chk_signoff && $signoff == 0) {
2830de7d4f0eSAndy Whitcroft		ERROR("Missing Signed-off-by: line(s)\n");
28310a920b5bSAndy Whitcroft	}
28320a920b5bSAndy Whitcroft
2833f0a594c1SAndy Whitcroft	print report_dump();
283413214adfSAndy Whitcroft	if ($summary && !($clean == 1 && $quiet == 1)) {
283513214adfSAndy Whitcroft		print "$filename " if ($summary_file);
28366c72ffaaSAndy Whitcroft		print "total: $cnt_error errors, $cnt_warn warnings, " .
28376c72ffaaSAndy Whitcroft			(($check)? "$cnt_chk checks, " : "") .
28386c72ffaaSAndy Whitcroft			"$cnt_lines lines checked\n";
28398905a67cSAndy Whitcroft		print "\n" if ($quiet == 0);
28406c72ffaaSAndy Whitcroft	}
28418905a67cSAndy Whitcroft
28420a920b5bSAndy Whitcroft	if ($clean == 1 && $quiet == 0) {
2843c2fdda0dSAndy Whitcroft		print "$vname has no obvious style problems and is ready for submission.\n"
28440a920b5bSAndy Whitcroft	}
28450a920b5bSAndy Whitcroft	if ($clean == 0 && $quiet == 0) {
2846c2fdda0dSAndy Whitcroft		print "$vname has style problems, please review.  If any of these errors\n";
28470a920b5bSAndy Whitcroft		print "are false positives report them to the maintainer, see\n";
28480a920b5bSAndy Whitcroft		print "CHECKPATCH in MAINTAINERS.\n";
28490a920b5bSAndy Whitcroft	}
285013214adfSAndy Whitcroft
28510a920b5bSAndy Whitcroft	return $clean;
28520a920b5bSAndy Whitcroft}
2853