xref: /linux-6.15/scripts/checkpatch.pl (revision 5e79d96e)
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)|
198691e669bSJoe Perches	dev_(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
2278905a67cSAndy Whitcroftsub build_types {
228d2172eb5SAndy Whitcroft	my $mods = "(?x:  \n" . join("|\n  ", @modifierList) . "\n)";
229d2172eb5SAndy Whitcroft	my $all = "(?x:  \n" . join("|\n  ", @typeList) . "\n)";
230c8cb2ca3SAndy Whitcroft	$Modifier	= qr{(?:$Attribute|$Sparse|$mods)};
2318905a67cSAndy Whitcroft	$NonptrType	= qr{
232d2172eb5SAndy Whitcroft			(?:$Modifier\s+|const\s+)*
233cf655043SAndy Whitcroft			(?:
234c45dcabdSAndy Whitcroft				(?:typeof|__typeof__)\s*\(\s*\**\s*$Ident\s*\)|
2358ed22cadSAndy Whitcroft				(?:$typeTypedefs\b)|
236c45dcabdSAndy Whitcroft				(?:${all}\b)
237cf655043SAndy Whitcroft			)
238c8cb2ca3SAndy Whitcroft			(?:\s+$Modifier|\s+const)*
2398905a67cSAndy Whitcroft		  }x;
2408905a67cSAndy Whitcroft	$Type	= qr{
241c45dcabdSAndy Whitcroft			$NonptrType
24265863862SAndy Whitcroft			(?:[\s\*]+\s*const|[\s\*]+|(?:\s*\[\s*\])+)?
243c8cb2ca3SAndy Whitcroft			(?:\s+$Inline|\s+$Modifier)*
2448905a67cSAndy Whitcroft		  }x;
2458905a67cSAndy Whitcroft	$Declare	= qr{(?:$Storage\s+)?$Type};
2468905a67cSAndy Whitcroft}
2478905a67cSAndy Whitcroftbuild_types();
2486c72ffaaSAndy Whitcroft
2496c72ffaaSAndy Whitcroft$chk_signoff = 0 if ($file);
2500a920b5bSAndy Whitcroft
2514a0df2efSAndy Whitcroftmy @dep_includes = ();
2524a0df2efSAndy Whitcroftmy @dep_functions = ();
2536c72ffaaSAndy Whitcroftmy $removal = "Documentation/feature-removal-schedule.txt";
2546c72ffaaSAndy Whitcroftif ($tree && -f "$root/$removal") {
25521caa13cSAndy Whitcroft	open(my $REMOVE, '<', "$root/$removal") ||
2566c72ffaaSAndy Whitcroft				die "$P: $removal: open failed - $!\n";
25721caa13cSAndy Whitcroft	while (<$REMOVE>) {
258f0a594c1SAndy Whitcroft		if (/^Check:\s+(.*\S)/) {
259f0a594c1SAndy Whitcroft			for my $entry (split(/[, ]+/, $1)) {
260f0a594c1SAndy Whitcroft				if ($entry =~ m@include/(.*)@) {
2614a0df2efSAndy Whitcroft					push(@dep_includes, $1);
2624a0df2efSAndy Whitcroft
263f0a594c1SAndy Whitcroft				} elsif ($entry !~ m@/@) {
264f0a594c1SAndy Whitcroft					push(@dep_functions, $entry);
265f0a594c1SAndy Whitcroft				}
2664a0df2efSAndy Whitcroft			}
2670a920b5bSAndy Whitcroft		}
2680a920b5bSAndy Whitcroft	}
26921caa13cSAndy Whitcroft	close($REMOVE);
2700a920b5bSAndy Whitcroft}
2710a920b5bSAndy Whitcroft
27200df344fSAndy Whitcroftmy @rawlines = ();
273c2fdda0dSAndy Whitcroftmy @lines = ();
274c2fdda0dSAndy Whitcroftmy $vname;
2756c72ffaaSAndy Whitcroftfor my $filename (@ARGV) {
27621caa13cSAndy Whitcroft	my $FILE;
2776c72ffaaSAndy Whitcroft	if ($file) {
27821caa13cSAndy Whitcroft		open($FILE, '-|', "diff -u /dev/null $filename") ||
2796c72ffaaSAndy Whitcroft			die "$P: $filename: diff failed - $!\n";
28021caa13cSAndy Whitcroft	} elsif ($filename eq '-') {
28121caa13cSAndy Whitcroft		open($FILE, '<&STDIN');
2826c72ffaaSAndy Whitcroft	} else {
28321caa13cSAndy Whitcroft		open($FILE, '<', "$filename") ||
2846c72ffaaSAndy Whitcroft			die "$P: $filename: open failed - $!\n";
2856c72ffaaSAndy Whitcroft	}
286c2fdda0dSAndy Whitcroft	if ($filename eq '-') {
287c2fdda0dSAndy Whitcroft		$vname = 'Your patch';
288c2fdda0dSAndy Whitcroft	} else {
289c2fdda0dSAndy Whitcroft		$vname = $filename;
290c2fdda0dSAndy Whitcroft	}
29121caa13cSAndy Whitcroft	while (<$FILE>) {
2920a920b5bSAndy Whitcroft		chomp;
29300df344fSAndy Whitcroft		push(@rawlines, $_);
2946c72ffaaSAndy Whitcroft	}
29521caa13cSAndy Whitcroft	close($FILE);
296c2fdda0dSAndy Whitcroft	if (!process($filename)) {
2970a920b5bSAndy Whitcroft		$exit = 1;
2980a920b5bSAndy Whitcroft	}
29900df344fSAndy Whitcroft	@rawlines = ();
30013214adfSAndy Whitcroft	@lines = ();
3010a920b5bSAndy Whitcroft}
3020a920b5bSAndy Whitcroft
3030a920b5bSAndy Whitcroftexit($exit);
3040a920b5bSAndy Whitcroft
3050a920b5bSAndy Whitcroftsub top_of_kernel_tree {
3066c72ffaaSAndy Whitcroft	my ($root) = @_;
3076c72ffaaSAndy Whitcroft
3086c72ffaaSAndy Whitcroft	my @tree_check = (
3096c72ffaaSAndy Whitcroft		"COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
3106c72ffaaSAndy Whitcroft		"README", "Documentation", "arch", "include", "drivers",
3116c72ffaaSAndy Whitcroft		"fs", "init", "ipc", "kernel", "lib", "scripts",
3126c72ffaaSAndy Whitcroft	);
3136c72ffaaSAndy Whitcroft
3146c72ffaaSAndy Whitcroft	foreach my $check (@tree_check) {
3156c72ffaaSAndy Whitcroft		if (! -e $root . '/' . $check) {
3160a920b5bSAndy Whitcroft			return 0;
3170a920b5bSAndy Whitcroft		}
3186c72ffaaSAndy Whitcroft	}
3196c72ffaaSAndy Whitcroft	return 1;
3206c72ffaaSAndy Whitcroft}
3210a920b5bSAndy Whitcroft
3220a920b5bSAndy Whitcroftsub expand_tabs {
3230a920b5bSAndy Whitcroft	my ($str) = @_;
3240a920b5bSAndy Whitcroft
3250a920b5bSAndy Whitcroft	my $res = '';
3260a920b5bSAndy Whitcroft	my $n = 0;
3270a920b5bSAndy Whitcroft	for my $c (split(//, $str)) {
3280a920b5bSAndy Whitcroft		if ($c eq "\t") {
3290a920b5bSAndy Whitcroft			$res .= ' ';
3300a920b5bSAndy Whitcroft			$n++;
3310a920b5bSAndy Whitcroft			for (; ($n % 8) != 0; $n++) {
3320a920b5bSAndy Whitcroft				$res .= ' ';
3330a920b5bSAndy Whitcroft			}
3340a920b5bSAndy Whitcroft			next;
3350a920b5bSAndy Whitcroft		}
3360a920b5bSAndy Whitcroft		$res .= $c;
3370a920b5bSAndy Whitcroft		$n++;
3380a920b5bSAndy Whitcroft	}
3390a920b5bSAndy Whitcroft
3400a920b5bSAndy Whitcroft	return $res;
3410a920b5bSAndy Whitcroft}
3426c72ffaaSAndy Whitcroftsub copy_spacing {
343773647a0SAndy Whitcroft	(my $res = shift) =~ tr/\t/ /c;
3446c72ffaaSAndy Whitcroft	return $res;
3456c72ffaaSAndy Whitcroft}
3460a920b5bSAndy Whitcroft
3474a0df2efSAndy Whitcroftsub line_stats {
3484a0df2efSAndy Whitcroft	my ($line) = @_;
3494a0df2efSAndy Whitcroft
3504a0df2efSAndy Whitcroft	# Drop the diff line leader and expand tabs
3514a0df2efSAndy Whitcroft	$line =~ s/^.//;
3524a0df2efSAndy Whitcroft	$line = expand_tabs($line);
3534a0df2efSAndy Whitcroft
3544a0df2efSAndy Whitcroft	# Pick the indent from the front of the line.
3554a0df2efSAndy Whitcroft	my ($white) = ($line =~ /^(\s*)/);
3564a0df2efSAndy Whitcroft
3574a0df2efSAndy Whitcroft	return (length($line), length($white));
3584a0df2efSAndy Whitcroft}
3594a0df2efSAndy Whitcroft
360773647a0SAndy Whitcroftmy $sanitise_quote = '';
361773647a0SAndy Whitcroft
362773647a0SAndy Whitcroftsub sanitise_line_reset {
363773647a0SAndy Whitcroft	my ($in_comment) = @_;
364773647a0SAndy Whitcroft
365773647a0SAndy Whitcroft	if ($in_comment) {
366773647a0SAndy Whitcroft		$sanitise_quote = '*/';
367773647a0SAndy Whitcroft	} else {
368773647a0SAndy Whitcroft		$sanitise_quote = '';
369773647a0SAndy Whitcroft	}
370773647a0SAndy Whitcroft}
37100df344fSAndy Whitcroftsub sanitise_line {
37200df344fSAndy Whitcroft	my ($line) = @_;
37300df344fSAndy Whitcroft
37400df344fSAndy Whitcroft	my $res = '';
37500df344fSAndy Whitcroft	my $l = '';
37600df344fSAndy Whitcroft
377c2fdda0dSAndy Whitcroft	my $qlen = 0;
378773647a0SAndy Whitcroft	my $off = 0;
379773647a0SAndy Whitcroft	my $c;
38000df344fSAndy Whitcroft
381773647a0SAndy Whitcroft	# Always copy over the diff marker.
382773647a0SAndy Whitcroft	$res = substr($line, 0, 1);
383773647a0SAndy Whitcroft
384773647a0SAndy Whitcroft	for ($off = 1; $off < length($line); $off++) {
385773647a0SAndy Whitcroft		$c = substr($line, $off, 1);
386773647a0SAndy Whitcroft
387773647a0SAndy Whitcroft		# Comments we are wacking completly including the begin
388773647a0SAndy Whitcroft		# and end, all to $;.
389773647a0SAndy Whitcroft		if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
390773647a0SAndy Whitcroft			$sanitise_quote = '*/';
391773647a0SAndy Whitcroft
392773647a0SAndy Whitcroft			substr($res, $off, 2, "$;$;");
393773647a0SAndy Whitcroft			$off++;
39400df344fSAndy Whitcroft			next;
395773647a0SAndy Whitcroft		}
39681bc0e02SAndy Whitcroft		if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
397773647a0SAndy Whitcroft			$sanitise_quote = '';
398773647a0SAndy Whitcroft			substr($res, $off, 2, "$;$;");
399773647a0SAndy Whitcroft			$off++;
400773647a0SAndy Whitcroft			next;
401773647a0SAndy Whitcroft		}
402113f04a8SDaniel Walker		if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
403113f04a8SDaniel Walker			$sanitise_quote = '//';
404113f04a8SDaniel Walker
405113f04a8SDaniel Walker			substr($res, $off, 2, $sanitise_quote);
406113f04a8SDaniel Walker			$off++;
407113f04a8SDaniel Walker			next;
408113f04a8SDaniel Walker		}
409773647a0SAndy Whitcroft
410773647a0SAndy Whitcroft		# A \ in a string means ignore the next character.
411773647a0SAndy Whitcroft		if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
412773647a0SAndy Whitcroft		    $c eq "\\") {
413773647a0SAndy Whitcroft			substr($res, $off, 2, 'XX');
414773647a0SAndy Whitcroft			$off++;
415773647a0SAndy Whitcroft			next;
416773647a0SAndy Whitcroft		}
417773647a0SAndy Whitcroft		# Regular quotes.
418773647a0SAndy Whitcroft		if ($c eq "'" || $c eq '"') {
419773647a0SAndy Whitcroft			if ($sanitise_quote eq '') {
420773647a0SAndy Whitcroft				$sanitise_quote = $c;
421773647a0SAndy Whitcroft
422773647a0SAndy Whitcroft				substr($res, $off, 1, $c);
423773647a0SAndy Whitcroft				next;
424773647a0SAndy Whitcroft			} elsif ($sanitise_quote eq $c) {
425773647a0SAndy Whitcroft				$sanitise_quote = '';
42600df344fSAndy Whitcroft			}
42700df344fSAndy Whitcroft		}
428773647a0SAndy Whitcroft
429fae17daeSAndy Whitcroft		#print "c<$c> SQ<$sanitise_quote>\n";
430773647a0SAndy Whitcroft		if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
431773647a0SAndy Whitcroft			substr($res, $off, 1, $;);
432113f04a8SDaniel Walker		} elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
433113f04a8SDaniel Walker			substr($res, $off, 1, $;);
434773647a0SAndy Whitcroft		} elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
435773647a0SAndy Whitcroft			substr($res, $off, 1, 'X');
43600df344fSAndy Whitcroft		} else {
437773647a0SAndy Whitcroft			substr($res, $off, 1, $c);
43800df344fSAndy Whitcroft		}
439c2fdda0dSAndy Whitcroft	}
440c2fdda0dSAndy Whitcroft
441113f04a8SDaniel Walker	if ($sanitise_quote eq '//') {
442113f04a8SDaniel Walker		$sanitise_quote = '';
443113f04a8SDaniel Walker	}
444113f04a8SDaniel Walker
445c2fdda0dSAndy Whitcroft	# The pathname on a #include may be surrounded by '<' and '>'.
446c45dcabdSAndy Whitcroft	if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
447c2fdda0dSAndy Whitcroft		my $clean = 'X' x length($1);
448c2fdda0dSAndy Whitcroft		$res =~ s@\<.*\>@<$clean>@;
449c2fdda0dSAndy Whitcroft
450c2fdda0dSAndy Whitcroft	# The whole of a #error is a string.
451c45dcabdSAndy Whitcroft	} elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
452c2fdda0dSAndy Whitcroft		my $clean = 'X' x length($1);
453c45dcabdSAndy Whitcroft		$res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
454c2fdda0dSAndy Whitcroft	}
455c2fdda0dSAndy Whitcroft
45600df344fSAndy Whitcroft	return $res;
45700df344fSAndy Whitcroft}
45800df344fSAndy Whitcroft
4598905a67cSAndy Whitcroftsub ctx_statement_block {
4608905a67cSAndy Whitcroft	my ($linenr, $remain, $off) = @_;
4618905a67cSAndy Whitcroft	my $line = $linenr - 1;
4628905a67cSAndy Whitcroft	my $blk = '';
4638905a67cSAndy Whitcroft	my $soff = $off;
4648905a67cSAndy Whitcroft	my $coff = $off - 1;
465773647a0SAndy Whitcroft	my $coff_set = 0;
4668905a67cSAndy Whitcroft
46713214adfSAndy Whitcroft	my $loff = 0;
46813214adfSAndy Whitcroft
4698905a67cSAndy Whitcroft	my $type = '';
4708905a67cSAndy Whitcroft	my $level = 0;
471a2750645SAndy Whitcroft	my @stack = ();
472cf655043SAndy Whitcroft	my $p;
4738905a67cSAndy Whitcroft	my $c;
4748905a67cSAndy Whitcroft	my $len = 0;
47513214adfSAndy Whitcroft
47613214adfSAndy Whitcroft	my $remainder;
4778905a67cSAndy Whitcroft	while (1) {
478a2750645SAndy Whitcroft		@stack = (['', 0]) if ($#stack == -1);
479a2750645SAndy Whitcroft
480773647a0SAndy Whitcroft		#warn "CSB: blk<$blk> remain<$remain>\n";
4818905a67cSAndy Whitcroft		# If we are about to drop off the end, pull in more
4828905a67cSAndy Whitcroft		# context.
4838905a67cSAndy Whitcroft		if ($off >= $len) {
4848905a67cSAndy Whitcroft			for (; $remain > 0; $line++) {
485dea33496SAndy Whitcroft				last if (!defined $lines[$line]);
486c2fdda0dSAndy Whitcroft				next if ($lines[$line] =~ /^-/);
4878905a67cSAndy Whitcroft				$remain--;
48813214adfSAndy Whitcroft				$loff = $len;
489c2fdda0dSAndy Whitcroft				$blk .= $lines[$line] . "\n";
4908905a67cSAndy Whitcroft				$len = length($blk);
4918905a67cSAndy Whitcroft				$line++;
4928905a67cSAndy Whitcroft				last;
4938905a67cSAndy Whitcroft			}
4948905a67cSAndy Whitcroft			# Bail if there is no further context.
4958905a67cSAndy Whitcroft			#warn "CSB: blk<$blk> off<$off> len<$len>\n";
49613214adfSAndy Whitcroft			if ($off >= $len) {
4978905a67cSAndy Whitcroft				last;
4988905a67cSAndy Whitcroft			}
4998905a67cSAndy Whitcroft		}
500cf655043SAndy Whitcroft		$p = $c;
5018905a67cSAndy Whitcroft		$c = substr($blk, $off, 1);
50213214adfSAndy Whitcroft		$remainder = substr($blk, $off);
5038905a67cSAndy Whitcroft
504773647a0SAndy Whitcroft		#warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
5054635f4fbSAndy Whitcroft
5064635f4fbSAndy Whitcroft		# Handle nested #if/#else.
5074635f4fbSAndy Whitcroft		if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
5084635f4fbSAndy Whitcroft			push(@stack, [ $type, $level ]);
5094635f4fbSAndy Whitcroft		} elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
5104635f4fbSAndy Whitcroft			($type, $level) = @{$stack[$#stack - 1]};
5114635f4fbSAndy Whitcroft		} elsif ($remainder =~ /^#\s*endif\b/) {
5124635f4fbSAndy Whitcroft			($type, $level) = @{pop(@stack)};
5134635f4fbSAndy Whitcroft		}
5144635f4fbSAndy Whitcroft
5158905a67cSAndy Whitcroft		# Statement ends at the ';' or a close '}' at the
5168905a67cSAndy Whitcroft		# outermost level.
5178905a67cSAndy Whitcroft		if ($level == 0 && $c eq ';') {
5188905a67cSAndy Whitcroft			last;
5198905a67cSAndy Whitcroft		}
5208905a67cSAndy Whitcroft
52113214adfSAndy Whitcroft		# An else is really a conditional as long as its not else if
522773647a0SAndy Whitcroft		if ($level == 0 && $coff_set == 0 &&
523773647a0SAndy Whitcroft				(!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
524773647a0SAndy Whitcroft				$remainder =~ /^(else)(?:\s|{)/ &&
525773647a0SAndy Whitcroft				$remainder !~ /^else\s+if\b/) {
526773647a0SAndy Whitcroft			$coff = $off + length($1) - 1;
527773647a0SAndy Whitcroft			$coff_set = 1;
528773647a0SAndy Whitcroft			#warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
529773647a0SAndy Whitcroft			#warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
53013214adfSAndy Whitcroft		}
53113214adfSAndy Whitcroft
5328905a67cSAndy Whitcroft		if (($type eq '' || $type eq '(') && $c eq '(') {
5338905a67cSAndy Whitcroft			$level++;
5348905a67cSAndy Whitcroft			$type = '(';
5358905a67cSAndy Whitcroft		}
5368905a67cSAndy Whitcroft		if ($type eq '(' && $c eq ')') {
5378905a67cSAndy Whitcroft			$level--;
5388905a67cSAndy Whitcroft			$type = ($level != 0)? '(' : '';
5398905a67cSAndy Whitcroft
5408905a67cSAndy Whitcroft			if ($level == 0 && $coff < $soff) {
5418905a67cSAndy Whitcroft				$coff = $off;
542773647a0SAndy Whitcroft				$coff_set = 1;
543773647a0SAndy Whitcroft				#warn "CSB: mark coff<$coff>\n";
5448905a67cSAndy Whitcroft			}
5458905a67cSAndy Whitcroft		}
5468905a67cSAndy Whitcroft		if (($type eq '' || $type eq '{') && $c eq '{') {
5478905a67cSAndy Whitcroft			$level++;
5488905a67cSAndy Whitcroft			$type = '{';
5498905a67cSAndy Whitcroft		}
5508905a67cSAndy Whitcroft		if ($type eq '{' && $c eq '}') {
5518905a67cSAndy Whitcroft			$level--;
5528905a67cSAndy Whitcroft			$type = ($level != 0)? '{' : '';
5538905a67cSAndy Whitcroft
5548905a67cSAndy Whitcroft			if ($level == 0) {
5558905a67cSAndy Whitcroft				last;
5568905a67cSAndy Whitcroft			}
5578905a67cSAndy Whitcroft		}
5588905a67cSAndy Whitcroft		$off++;
5598905a67cSAndy Whitcroft	}
560a3bb97a7SAndy Whitcroft	# We are truly at the end, so shuffle to the next line.
56113214adfSAndy Whitcroft	if ($off == $len) {
562a3bb97a7SAndy Whitcroft		$loff = $len + 1;
56313214adfSAndy Whitcroft		$line++;
56413214adfSAndy Whitcroft		$remain--;
56513214adfSAndy Whitcroft	}
5668905a67cSAndy Whitcroft
5678905a67cSAndy Whitcroft	my $statement = substr($blk, $soff, $off - $soff + 1);
5688905a67cSAndy Whitcroft	my $condition = substr($blk, $soff, $coff - $soff + 1);
5698905a67cSAndy Whitcroft
5708905a67cSAndy Whitcroft	#warn "STATEMENT<$statement>\n";
5718905a67cSAndy Whitcroft	#warn "CONDITION<$condition>\n";
5728905a67cSAndy Whitcroft
573773647a0SAndy Whitcroft	#print "coff<$coff> soff<$off> loff<$loff>\n";
57413214adfSAndy Whitcroft
57513214adfSAndy Whitcroft	return ($statement, $condition,
57613214adfSAndy Whitcroft			$line, $remain + 1, $off - $loff + 1, $level);
57713214adfSAndy Whitcroft}
57813214adfSAndy Whitcroft
579cf655043SAndy Whitcroftsub statement_lines {
580cf655043SAndy Whitcroft	my ($stmt) = @_;
581cf655043SAndy Whitcroft
582cf655043SAndy Whitcroft	# Strip the diff line prefixes and rip blank lines at start and end.
583cf655043SAndy Whitcroft	$stmt =~ s/(^|\n)./$1/g;
584cf655043SAndy Whitcroft	$stmt =~ s/^\s*//;
585cf655043SAndy Whitcroft	$stmt =~ s/\s*$//;
586cf655043SAndy Whitcroft
587cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
588cf655043SAndy Whitcroft
589cf655043SAndy Whitcroft	return $#stmt_lines + 2;
590cf655043SAndy Whitcroft}
591cf655043SAndy Whitcroft
592cf655043SAndy Whitcroftsub statement_rawlines {
593cf655043SAndy Whitcroft	my ($stmt) = @_;
594cf655043SAndy Whitcroft
595cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
596cf655043SAndy Whitcroft
597cf655043SAndy Whitcroft	return $#stmt_lines + 2;
598cf655043SAndy Whitcroft}
599cf655043SAndy Whitcroft
600cf655043SAndy Whitcroftsub statement_block_size {
601cf655043SAndy Whitcroft	my ($stmt) = @_;
602cf655043SAndy Whitcroft
603cf655043SAndy Whitcroft	$stmt =~ s/(^|\n)./$1/g;
604cf655043SAndy Whitcroft	$stmt =~ s/^\s*{//;
605cf655043SAndy Whitcroft	$stmt =~ s/}\s*$//;
606cf655043SAndy Whitcroft	$stmt =~ s/^\s*//;
607cf655043SAndy Whitcroft	$stmt =~ s/\s*$//;
608cf655043SAndy Whitcroft
609cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
610cf655043SAndy Whitcroft	my @stmt_statements = ($stmt =~ /;/g);
611cf655043SAndy Whitcroft
612cf655043SAndy Whitcroft	my $stmt_lines = $#stmt_lines + 2;
613cf655043SAndy Whitcroft	my $stmt_statements = $#stmt_statements + 1;
614cf655043SAndy Whitcroft
615cf655043SAndy Whitcroft	if ($stmt_lines > $stmt_statements) {
616cf655043SAndy Whitcroft		return $stmt_lines;
617cf655043SAndy Whitcroft	} else {
618cf655043SAndy Whitcroft		return $stmt_statements;
619cf655043SAndy Whitcroft	}
620cf655043SAndy Whitcroft}
621cf655043SAndy Whitcroft
62213214adfSAndy Whitcroftsub ctx_statement_full {
62313214adfSAndy Whitcroft	my ($linenr, $remain, $off) = @_;
62413214adfSAndy Whitcroft	my ($statement, $condition, $level);
62513214adfSAndy Whitcroft
62613214adfSAndy Whitcroft	my (@chunks);
62713214adfSAndy Whitcroft
628cf655043SAndy Whitcroft	# Grab the first conditional/block pair.
62913214adfSAndy Whitcroft	($statement, $condition, $linenr, $remain, $off, $level) =
63013214adfSAndy Whitcroft				ctx_statement_block($linenr, $remain, $off);
631773647a0SAndy Whitcroft	#print "F: c<$condition> s<$statement> remain<$remain>\n";
63213214adfSAndy Whitcroft	push(@chunks, [ $condition, $statement ]);
633cf655043SAndy Whitcroft	if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
634cf655043SAndy Whitcroft		return ($level, $linenr, @chunks);
635cf655043SAndy Whitcroft	}
636cf655043SAndy Whitcroft
637cf655043SAndy Whitcroft	# Pull in the following conditional/block pairs and see if they
638cf655043SAndy Whitcroft	# could continue the statement.
639cf655043SAndy Whitcroft	for (;;) {
64013214adfSAndy Whitcroft		($statement, $condition, $linenr, $remain, $off, $level) =
64113214adfSAndy Whitcroft				ctx_statement_block($linenr, $remain, $off);
642cf655043SAndy Whitcroft		#print "C: c<$condition> s<$statement> remain<$remain>\n";
643773647a0SAndy Whitcroft		last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
644cf655043SAndy Whitcroft		#print "C: push\n";
645cf655043SAndy Whitcroft		push(@chunks, [ $condition, $statement ]);
64613214adfSAndy Whitcroft	}
64713214adfSAndy Whitcroft
64813214adfSAndy Whitcroft	return ($level, $linenr, @chunks);
6498905a67cSAndy Whitcroft}
6508905a67cSAndy Whitcroft
6514a0df2efSAndy Whitcroftsub ctx_block_get {
652f0a594c1SAndy Whitcroft	my ($linenr, $remain, $outer, $open, $close, $off) = @_;
6534a0df2efSAndy Whitcroft	my $line;
6544a0df2efSAndy Whitcroft	my $start = $linenr - 1;
6554a0df2efSAndy Whitcroft	my $blk = '';
6564a0df2efSAndy Whitcroft	my @o;
6574a0df2efSAndy Whitcroft	my @c;
6584a0df2efSAndy Whitcroft	my @res = ();
6594a0df2efSAndy Whitcroft
660f0a594c1SAndy Whitcroft	my $level = 0;
6614635f4fbSAndy Whitcroft	my @stack = ($level);
66200df344fSAndy Whitcroft	for ($line = $start; $remain > 0; $line++) {
66300df344fSAndy Whitcroft		next if ($rawlines[$line] =~ /^-/);
66400df344fSAndy Whitcroft		$remain--;
66500df344fSAndy Whitcroft
66600df344fSAndy Whitcroft		$blk .= $rawlines[$line];
6674635f4fbSAndy Whitcroft
6684635f4fbSAndy Whitcroft		# Handle nested #if/#else.
6694635f4fbSAndy Whitcroft		if ($rawlines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
6704635f4fbSAndy Whitcroft			push(@stack, $level);
6714635f4fbSAndy Whitcroft		} elsif ($rawlines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
6724635f4fbSAndy Whitcroft			$level = $stack[$#stack - 1];
6734635f4fbSAndy Whitcroft		} elsif ($rawlines[$line] =~ /^.\s*#\s*endif\b/) {
6744635f4fbSAndy Whitcroft			$level = pop(@stack);
6754635f4fbSAndy Whitcroft		}
6764635f4fbSAndy Whitcroft
677f0a594c1SAndy Whitcroft		foreach my $c (split(//, $rawlines[$line])) {
678f0a594c1SAndy Whitcroft			##print "C<$c>L<$level><$open$close>O<$off>\n";
679f0a594c1SAndy Whitcroft			if ($off > 0) {
680f0a594c1SAndy Whitcroft				$off--;
681f0a594c1SAndy Whitcroft				next;
682f0a594c1SAndy Whitcroft			}
6834a0df2efSAndy Whitcroft
684f0a594c1SAndy Whitcroft			if ($c eq $close && $level > 0) {
685f0a594c1SAndy Whitcroft				$level--;
686f0a594c1SAndy Whitcroft				last if ($level == 0);
687f0a594c1SAndy Whitcroft			} elsif ($c eq $open) {
688f0a594c1SAndy Whitcroft				$level++;
689f0a594c1SAndy Whitcroft			}
690f0a594c1SAndy Whitcroft		}
6914a0df2efSAndy Whitcroft
692f0a594c1SAndy Whitcroft		if (!$outer || $level <= 1) {
69300df344fSAndy Whitcroft			push(@res, $rawlines[$line]);
6944a0df2efSAndy Whitcroft		}
6954a0df2efSAndy Whitcroft
696f0a594c1SAndy Whitcroft		last if ($level == 0);
6974a0df2efSAndy Whitcroft	}
6984a0df2efSAndy Whitcroft
699f0a594c1SAndy Whitcroft	return ($level, @res);
7004a0df2efSAndy Whitcroft}
7014a0df2efSAndy Whitcroftsub ctx_block_outer {
7024a0df2efSAndy Whitcroft	my ($linenr, $remain) = @_;
7034a0df2efSAndy Whitcroft
704f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
705f0a594c1SAndy Whitcroft	return @r;
7064a0df2efSAndy Whitcroft}
7074a0df2efSAndy Whitcroftsub ctx_block {
7084a0df2efSAndy Whitcroft	my ($linenr, $remain) = @_;
7094a0df2efSAndy Whitcroft
710f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
711f0a594c1SAndy Whitcroft	return @r;
712653d4876SAndy Whitcroft}
713653d4876SAndy Whitcroftsub ctx_statement {
714f0a594c1SAndy Whitcroft	my ($linenr, $remain, $off) = @_;
715f0a594c1SAndy Whitcroft
716f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
717f0a594c1SAndy Whitcroft	return @r;
718f0a594c1SAndy Whitcroft}
719f0a594c1SAndy Whitcroftsub ctx_block_level {
720653d4876SAndy Whitcroft	my ($linenr, $remain) = @_;
721653d4876SAndy Whitcroft
722f0a594c1SAndy Whitcroft	return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
7234a0df2efSAndy Whitcroft}
7249c0ca6f9SAndy Whitcroftsub ctx_statement_level {
7259c0ca6f9SAndy Whitcroft	my ($linenr, $remain, $off) = @_;
7269c0ca6f9SAndy Whitcroft
7279c0ca6f9SAndy Whitcroft	return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
7289c0ca6f9SAndy Whitcroft}
7294a0df2efSAndy Whitcroft
7304a0df2efSAndy Whitcroftsub ctx_locate_comment {
7314a0df2efSAndy Whitcroft	my ($first_line, $end_line) = @_;
7324a0df2efSAndy Whitcroft
7334a0df2efSAndy Whitcroft	# Catch a comment on the end of the line itself.
734beae6332SAndy Whitcroft	my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
7354a0df2efSAndy Whitcroft	return $current_comment if (defined $current_comment);
7364a0df2efSAndy Whitcroft
7374a0df2efSAndy Whitcroft	# Look through the context and try and figure out if there is a
7384a0df2efSAndy Whitcroft	# comment.
7394a0df2efSAndy Whitcroft	my $in_comment = 0;
7404a0df2efSAndy Whitcroft	$current_comment = '';
7414a0df2efSAndy Whitcroft	for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
74200df344fSAndy Whitcroft		my $line = $rawlines[$linenr - 1];
74300df344fSAndy Whitcroft		#warn "           $line\n";
7444a0df2efSAndy Whitcroft		if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
7454a0df2efSAndy Whitcroft			$in_comment = 1;
7464a0df2efSAndy Whitcroft		}
7474a0df2efSAndy Whitcroft		if ($line =~ m@/\*@) {
7484a0df2efSAndy Whitcroft			$in_comment = 1;
7494a0df2efSAndy Whitcroft		}
7504a0df2efSAndy Whitcroft		if (!$in_comment && $current_comment ne '') {
7514a0df2efSAndy Whitcroft			$current_comment = '';
7524a0df2efSAndy Whitcroft		}
7534a0df2efSAndy Whitcroft		$current_comment .= $line . "\n" if ($in_comment);
7544a0df2efSAndy Whitcroft		if ($line =~ m@\*/@) {
7554a0df2efSAndy Whitcroft			$in_comment = 0;
7564a0df2efSAndy Whitcroft		}
7574a0df2efSAndy Whitcroft	}
7584a0df2efSAndy Whitcroft
7594a0df2efSAndy Whitcroft	chomp($current_comment);
7604a0df2efSAndy Whitcroft	return($current_comment);
7614a0df2efSAndy Whitcroft}
7624a0df2efSAndy Whitcroftsub ctx_has_comment {
7634a0df2efSAndy Whitcroft	my ($first_line, $end_line) = @_;
7644a0df2efSAndy Whitcroft	my $cmt = ctx_locate_comment($first_line, $end_line);
7654a0df2efSAndy Whitcroft
76600df344fSAndy Whitcroft	##print "LINE: $rawlines[$end_line - 1 ]\n";
7674a0df2efSAndy Whitcroft	##print "CMMT: $cmt\n";
7684a0df2efSAndy Whitcroft
7694a0df2efSAndy Whitcroft	return ($cmt ne '');
7704a0df2efSAndy Whitcroft}
7714a0df2efSAndy Whitcroft
7724d001e4dSAndy Whitcroftsub raw_line {
7734d001e4dSAndy Whitcroft	my ($linenr, $cnt) = @_;
7744d001e4dSAndy Whitcroft
7754d001e4dSAndy Whitcroft	my $offset = $linenr - 1;
7764d001e4dSAndy Whitcroft	$cnt++;
7774d001e4dSAndy Whitcroft
7784d001e4dSAndy Whitcroft	my $line;
7794d001e4dSAndy Whitcroft	while ($cnt) {
7804d001e4dSAndy Whitcroft		$line = $rawlines[$offset++];
7814d001e4dSAndy Whitcroft		next if (defined($line) && $line =~ /^-/);
7824d001e4dSAndy Whitcroft		$cnt--;
7834d001e4dSAndy Whitcroft	}
7844d001e4dSAndy Whitcroft
7854d001e4dSAndy Whitcroft	return $line;
7864d001e4dSAndy Whitcroft}
7874d001e4dSAndy Whitcroft
7880a920b5bSAndy Whitcroftsub cat_vet {
7890a920b5bSAndy Whitcroft	my ($vet) = @_;
7909c0ca6f9SAndy Whitcroft	my ($res, $coded);
7910a920b5bSAndy Whitcroft
7929c0ca6f9SAndy Whitcroft	$res = '';
7936c72ffaaSAndy Whitcroft	while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
7946c72ffaaSAndy Whitcroft		$res .= $1;
7956c72ffaaSAndy Whitcroft		if ($2 ne '') {
7969c0ca6f9SAndy Whitcroft			$coded = sprintf("^%c", unpack('C', $2) + 64);
7976c72ffaaSAndy Whitcroft			$res .= $coded;
7986c72ffaaSAndy Whitcroft		}
7999c0ca6f9SAndy Whitcroft	}
8009c0ca6f9SAndy Whitcroft	$res =~ s/$/\$/;
8010a920b5bSAndy Whitcroft
8029c0ca6f9SAndy Whitcroft	return $res;
8030a920b5bSAndy Whitcroft}
8040a920b5bSAndy Whitcroft
805c2fdda0dSAndy Whitcroftmy $av_preprocessor = 0;
806cf655043SAndy Whitcroftmy $av_pending;
807c2fdda0dSAndy Whitcroftmy @av_paren_type;
8081f65f947SAndy Whitcroftmy $av_pend_colon;
809c2fdda0dSAndy Whitcroft
810c2fdda0dSAndy Whitcroftsub annotate_reset {
811c2fdda0dSAndy Whitcroft	$av_preprocessor = 0;
812cf655043SAndy Whitcroft	$av_pending = '_';
813cf655043SAndy Whitcroft	@av_paren_type = ('E');
8141f65f947SAndy Whitcroft	$av_pend_colon = 'O';
815c2fdda0dSAndy Whitcroft}
816c2fdda0dSAndy Whitcroft
8176c72ffaaSAndy Whitcroftsub annotate_values {
8186c72ffaaSAndy Whitcroft	my ($stream, $type) = @_;
8196c72ffaaSAndy Whitcroft
8206c72ffaaSAndy Whitcroft	my $res;
8211f65f947SAndy Whitcroft	my $var = '_' x length($stream);
8226c72ffaaSAndy Whitcroft	my $cur = $stream;
8236c72ffaaSAndy Whitcroft
824c2fdda0dSAndy Whitcroft	print "$stream\n" if ($dbg_values > 1);
8256c72ffaaSAndy Whitcroft
8266c72ffaaSAndy Whitcroft	while (length($cur)) {
827773647a0SAndy Whitcroft		@av_paren_type = ('E') if ($#av_paren_type < 0);
828cf655043SAndy Whitcroft		print " <" . join('', @av_paren_type) .
829171ae1a4SAndy Whitcroft				"> <$type> <$av_pending>" if ($dbg_values > 1);
8306c72ffaaSAndy Whitcroft		if ($cur =~ /^(\s+)/o) {
831c2fdda0dSAndy Whitcroft			print "WS($1)\n" if ($dbg_values > 1);
832c2fdda0dSAndy Whitcroft			if ($1 =~ /\n/ && $av_preprocessor) {
833cf655043SAndy Whitcroft				$type = pop(@av_paren_type);
834c2fdda0dSAndy Whitcroft				$av_preprocessor = 0;
8356c72ffaaSAndy Whitcroft			}
8366c72ffaaSAndy Whitcroft
8378ea3eb9aSAndy Whitcroft		} elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\()/) {
838c2fdda0dSAndy Whitcroft			print "DECLARE($1)\n" if ($dbg_values > 1);
8396c72ffaaSAndy Whitcroft			$type = 'T';
8406c72ffaaSAndy Whitcroft
841389a2fe5SAndy Whitcroft		} elsif ($cur =~ /^($Modifier)\s*/) {
842389a2fe5SAndy Whitcroft			print "MODIFIER($1)\n" if ($dbg_values > 1);
843389a2fe5SAndy Whitcroft			$type = 'T';
844389a2fe5SAndy Whitcroft
845c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
846171ae1a4SAndy Whitcroft			print "DEFINE($1,$2)\n" if ($dbg_values > 1);
847c2fdda0dSAndy Whitcroft			$av_preprocessor = 1;
848171ae1a4SAndy Whitcroft			push(@av_paren_type, $type);
849171ae1a4SAndy Whitcroft			if ($2 ne '') {
850cf655043SAndy Whitcroft				$av_pending = 'N';
851171ae1a4SAndy Whitcroft			}
852171ae1a4SAndy Whitcroft			$type = 'E';
853171ae1a4SAndy Whitcroft
854c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
855171ae1a4SAndy Whitcroft			print "UNDEF($1)\n" if ($dbg_values > 1);
856171ae1a4SAndy Whitcroft			$av_preprocessor = 1;
857171ae1a4SAndy Whitcroft			push(@av_paren_type, $type);
8586c72ffaaSAndy Whitcroft
859c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
860cf655043SAndy Whitcroft			print "PRE_START($1)\n" if ($dbg_values > 1);
861c2fdda0dSAndy Whitcroft			$av_preprocessor = 1;
862cf655043SAndy Whitcroft
863cf655043SAndy Whitcroft			push(@av_paren_type, $type);
864cf655043SAndy Whitcroft			push(@av_paren_type, $type);
865171ae1a4SAndy Whitcroft			$type = 'E';
866cf655043SAndy Whitcroft
867c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
868cf655043SAndy Whitcroft			print "PRE_RESTART($1)\n" if ($dbg_values > 1);
869cf655043SAndy Whitcroft			$av_preprocessor = 1;
870cf655043SAndy Whitcroft
871cf655043SAndy Whitcroft			push(@av_paren_type, $av_paren_type[$#av_paren_type]);
872cf655043SAndy Whitcroft
873171ae1a4SAndy Whitcroft			$type = 'E';
874cf655043SAndy Whitcroft
875c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:endif))/o) {
876cf655043SAndy Whitcroft			print "PRE_END($1)\n" if ($dbg_values > 1);
877cf655043SAndy Whitcroft
878cf655043SAndy Whitcroft			$av_preprocessor = 1;
879cf655043SAndy Whitcroft
880cf655043SAndy Whitcroft			# Assume all arms of the conditional end as this
881cf655043SAndy Whitcroft			# one does, and continue as if the #endif was not here.
882cf655043SAndy Whitcroft			pop(@av_paren_type);
883cf655043SAndy Whitcroft			push(@av_paren_type, $type);
884171ae1a4SAndy Whitcroft			$type = 'E';
8856c72ffaaSAndy Whitcroft
8866c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\\\n)/o) {
887c2fdda0dSAndy Whitcroft			print "PRECONT($1)\n" if ($dbg_values > 1);
8886c72ffaaSAndy Whitcroft
889171ae1a4SAndy Whitcroft		} elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
890171ae1a4SAndy Whitcroft			print "ATTR($1)\n" if ($dbg_values > 1);
891171ae1a4SAndy Whitcroft			$av_pending = $type;
892171ae1a4SAndy Whitcroft			$type = 'N';
893171ae1a4SAndy Whitcroft
8946c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
895c2fdda0dSAndy Whitcroft			print "SIZEOF($1)\n" if ($dbg_values > 1);
8966c72ffaaSAndy Whitcroft			if (defined $2) {
897cf655043SAndy Whitcroft				$av_pending = 'V';
8986c72ffaaSAndy Whitcroft			}
8996c72ffaaSAndy Whitcroft			$type = 'N';
9006c72ffaaSAndy Whitcroft
90114b111c1SAndy Whitcroft		} elsif ($cur =~ /^(if|while|for)\b/o) {
902c2fdda0dSAndy Whitcroft			print "COND($1)\n" if ($dbg_values > 1);
90314b111c1SAndy Whitcroft			$av_pending = 'E';
9046c72ffaaSAndy Whitcroft			$type = 'N';
9056c72ffaaSAndy Whitcroft
9061f65f947SAndy Whitcroft		} elsif ($cur =~/^(case)/o) {
9071f65f947SAndy Whitcroft			print "CASE($1)\n" if ($dbg_values > 1);
9081f65f947SAndy Whitcroft			$av_pend_colon = 'C';
9091f65f947SAndy Whitcroft			$type = 'N';
9101f65f947SAndy Whitcroft
91114b111c1SAndy Whitcroft		} elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
912c2fdda0dSAndy Whitcroft			print "KEYWORD($1)\n" if ($dbg_values > 1);
9136c72ffaaSAndy Whitcroft			$type = 'N';
9146c72ffaaSAndy Whitcroft
9156c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\()/o) {
916c2fdda0dSAndy Whitcroft			print "PAREN('$1')\n" if ($dbg_values > 1);
917cf655043SAndy Whitcroft			push(@av_paren_type, $av_pending);
918cf655043SAndy Whitcroft			$av_pending = '_';
9196c72ffaaSAndy Whitcroft			$type = 'N';
9206c72ffaaSAndy Whitcroft
9216c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\))/o) {
922cf655043SAndy Whitcroft			my $new_type = pop(@av_paren_type);
923cf655043SAndy Whitcroft			if ($new_type ne '_') {
924cf655043SAndy Whitcroft				$type = $new_type;
925c2fdda0dSAndy Whitcroft				print "PAREN('$1') -> $type\n"
926c2fdda0dSAndy Whitcroft							if ($dbg_values > 1);
9276c72ffaaSAndy Whitcroft			} else {
928c2fdda0dSAndy Whitcroft				print "PAREN('$1')\n" if ($dbg_values > 1);
9296c72ffaaSAndy Whitcroft			}
9306c72ffaaSAndy Whitcroft
931c8cb2ca3SAndy Whitcroft		} elsif ($cur =~ /^($Ident)\s*\(/o) {
932c2fdda0dSAndy Whitcroft			print "FUNC($1)\n" if ($dbg_values > 1);
933c8cb2ca3SAndy Whitcroft			$type = 'V';
934cf655043SAndy Whitcroft			$av_pending = 'V';
9356c72ffaaSAndy Whitcroft
9368e761b04SAndy Whitcroft		} elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
9378e761b04SAndy Whitcroft			if (defined $2 && $type eq 'C' || $type eq 'T') {
9381f65f947SAndy Whitcroft				$av_pend_colon = 'B';
9398e761b04SAndy Whitcroft			} elsif ($type eq 'E') {
9408e761b04SAndy Whitcroft				$av_pend_colon = 'L';
9411f65f947SAndy Whitcroft			}
9421f65f947SAndy Whitcroft			print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
9431f65f947SAndy Whitcroft			$type = 'V';
9441f65f947SAndy Whitcroft
9456c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Ident|$Constant)/o) {
946c2fdda0dSAndy Whitcroft			print "IDENT($1)\n" if ($dbg_values > 1);
9476c72ffaaSAndy Whitcroft			$type = 'V';
9486c72ffaaSAndy Whitcroft
9496c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Assignment)/o) {
950c2fdda0dSAndy Whitcroft			print "ASSIGN($1)\n" if ($dbg_values > 1);
9516c72ffaaSAndy Whitcroft			$type = 'N';
9526c72ffaaSAndy Whitcroft
953cf655043SAndy Whitcroft		} elsif ($cur =~/^(;|{|})/) {
954c2fdda0dSAndy Whitcroft			print "END($1)\n" if ($dbg_values > 1);
95513214adfSAndy Whitcroft			$type = 'E';
9561f65f947SAndy Whitcroft			$av_pend_colon = 'O';
95713214adfSAndy Whitcroft
9588e761b04SAndy Whitcroft		} elsif ($cur =~/^(,)/) {
9598e761b04SAndy Whitcroft			print "COMMA($1)\n" if ($dbg_values > 1);
9608e761b04SAndy Whitcroft			$type = 'C';
9618e761b04SAndy Whitcroft
9621f65f947SAndy Whitcroft		} elsif ($cur =~ /^(\?)/o) {
9631f65f947SAndy Whitcroft			print "QUESTION($1)\n" if ($dbg_values > 1);
9641f65f947SAndy Whitcroft			$type = 'N';
9651f65f947SAndy Whitcroft
9661f65f947SAndy Whitcroft		} elsif ($cur =~ /^(:)/o) {
9671f65f947SAndy Whitcroft			print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
9681f65f947SAndy Whitcroft
9691f65f947SAndy Whitcroft			substr($var, length($res), 1, $av_pend_colon);
9701f65f947SAndy Whitcroft			if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
9711f65f947SAndy Whitcroft				$type = 'E';
9721f65f947SAndy Whitcroft			} else {
9731f65f947SAndy Whitcroft				$type = 'N';
9741f65f947SAndy Whitcroft			}
9751f65f947SAndy Whitcroft			$av_pend_colon = 'O';
9761f65f947SAndy Whitcroft
9778e761b04SAndy Whitcroft		} elsif ($cur =~ /^(\[)/o) {
97813214adfSAndy Whitcroft			print "CLOSE($1)\n" if ($dbg_values > 1);
9796c72ffaaSAndy Whitcroft			$type = 'N';
9806c72ffaaSAndy Whitcroft
9810d413866SAndy Whitcroft		} elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
98274048ed8SAndy Whitcroft			my $variant;
98374048ed8SAndy Whitcroft
98474048ed8SAndy Whitcroft			print "OPV($1)\n" if ($dbg_values > 1);
98574048ed8SAndy Whitcroft			if ($type eq 'V') {
98674048ed8SAndy Whitcroft				$variant = 'B';
98774048ed8SAndy Whitcroft			} else {
98874048ed8SAndy Whitcroft				$variant = 'U';
98974048ed8SAndy Whitcroft			}
99074048ed8SAndy Whitcroft
99174048ed8SAndy Whitcroft			substr($var, length($res), 1, $variant);
99274048ed8SAndy Whitcroft			$type = 'N';
99374048ed8SAndy Whitcroft
9946c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Operators)/o) {
995c2fdda0dSAndy Whitcroft			print "OP($1)\n" if ($dbg_values > 1);
9966c72ffaaSAndy Whitcroft			if ($1 ne '++' && $1 ne '--') {
9976c72ffaaSAndy Whitcroft				$type = 'N';
9986c72ffaaSAndy Whitcroft			}
9996c72ffaaSAndy Whitcroft
10006c72ffaaSAndy Whitcroft		} elsif ($cur =~ /(^.)/o) {
1001c2fdda0dSAndy Whitcroft			print "C($1)\n" if ($dbg_values > 1);
10026c72ffaaSAndy Whitcroft		}
10036c72ffaaSAndy Whitcroft		if (defined $1) {
10046c72ffaaSAndy Whitcroft			$cur = substr($cur, length($1));
10056c72ffaaSAndy Whitcroft			$res .= $type x length($1);
10066c72ffaaSAndy Whitcroft		}
10076c72ffaaSAndy Whitcroft	}
10086c72ffaaSAndy Whitcroft
10091f65f947SAndy Whitcroft	return ($res, $var);
10106c72ffaaSAndy Whitcroft}
10116c72ffaaSAndy Whitcroft
10128905a67cSAndy Whitcroftsub possible {
101313214adfSAndy Whitcroft	my ($possible, $line) = @_;
10149a974fdbSAndy Whitcroft	my $notPermitted = qr{(?:
10150776e594SAndy Whitcroft		^(?:
10160776e594SAndy Whitcroft			$Modifier|
10170776e594SAndy Whitcroft			$Storage|
10180776e594SAndy Whitcroft			$Type|
10199a974fdbSAndy Whitcroft			DEFINE_\S+
10209a974fdbSAndy Whitcroft		)$|
10219a974fdbSAndy Whitcroft		^(?:
10220776e594SAndy Whitcroft			goto|
10230776e594SAndy Whitcroft			return|
10240776e594SAndy Whitcroft			case|
10250776e594SAndy Whitcroft			else|
10260776e594SAndy Whitcroft			asm|__asm__|
10270776e594SAndy Whitcroft			do
10289a974fdbSAndy Whitcroft		)(?:\s|$)|
10290776e594SAndy Whitcroft		^(?:typedef|struct|enum)\b
10309a974fdbSAndy Whitcroft	    )}x;
10319a974fdbSAndy Whitcroft	warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
10329a974fdbSAndy Whitcroft	if ($possible !~ $notPermitted) {
1033c45dcabdSAndy Whitcroft		# Check for modifiers.
1034c45dcabdSAndy Whitcroft		$possible =~ s/\s*$Storage\s*//g;
1035c45dcabdSAndy Whitcroft		$possible =~ s/\s*$Sparse\s*//g;
1036c45dcabdSAndy Whitcroft		if ($possible =~ /^\s*$/) {
1037c45dcabdSAndy Whitcroft
1038c45dcabdSAndy Whitcroft		} elsif ($possible =~ /\s/) {
1039c45dcabdSAndy Whitcroft			$possible =~ s/\s*$Type\s*//g;
1040d2506586SAndy Whitcroft			for my $modifier (split(' ', $possible)) {
10419a974fdbSAndy Whitcroft				if ($modifier !~ $notPermitted) {
1042d2506586SAndy Whitcroft					warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1043d2506586SAndy Whitcroft					push(@modifierList, $modifier);
1044d2506586SAndy Whitcroft				}
10459a974fdbSAndy Whitcroft			}
1046c45dcabdSAndy Whitcroft
1047c45dcabdSAndy Whitcroft		} else {
104813214adfSAndy Whitcroft			warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
10498905a67cSAndy Whitcroft			push(@typeList, $possible);
1050c45dcabdSAndy Whitcroft		}
10518905a67cSAndy Whitcroft		build_types();
10520776e594SAndy Whitcroft	} else {
10530776e594SAndy Whitcroft		warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
10548905a67cSAndy Whitcroft	}
10558905a67cSAndy Whitcroft}
10568905a67cSAndy Whitcroft
10576c72ffaaSAndy Whitcroftmy $prefix = '';
10586c72ffaaSAndy Whitcroft
1059f0a594c1SAndy Whitcroftsub report {
1060773647a0SAndy Whitcroft	if (defined $tst_only && $_[0] !~ /\Q$tst_only\E/) {
1061773647a0SAndy Whitcroft		return 0;
1062773647a0SAndy Whitcroft	}
10638905a67cSAndy Whitcroft	my $line = $prefix . $_[0];
10648905a67cSAndy Whitcroft
10658905a67cSAndy Whitcroft	$line = (split('\n', $line))[0] . "\n" if ($terse);
10668905a67cSAndy Whitcroft
106713214adfSAndy Whitcroft	push(our @report, $line);
1068773647a0SAndy Whitcroft
1069773647a0SAndy Whitcroft	return 1;
1070f0a594c1SAndy Whitcroft}
1071f0a594c1SAndy Whitcroftsub report_dump {
107213214adfSAndy Whitcroft	our @report;
1073f0a594c1SAndy Whitcroft}
1074de7d4f0eSAndy Whitcroftsub ERROR {
1075773647a0SAndy Whitcroft	if (report("ERROR: $_[0]\n")) {
1076de7d4f0eSAndy Whitcroft		our $clean = 0;
10776c72ffaaSAndy Whitcroft		our $cnt_error++;
1078de7d4f0eSAndy Whitcroft	}
1079773647a0SAndy Whitcroft}
1080de7d4f0eSAndy Whitcroftsub WARN {
1081773647a0SAndy Whitcroft	if (report("WARNING: $_[0]\n")) {
1082de7d4f0eSAndy Whitcroft		our $clean = 0;
10836c72ffaaSAndy Whitcroft		our $cnt_warn++;
1084de7d4f0eSAndy Whitcroft	}
1085773647a0SAndy Whitcroft}
1086de7d4f0eSAndy Whitcroftsub CHK {
1087773647a0SAndy Whitcroft	if ($check && report("CHECK: $_[0]\n")) {
1088de7d4f0eSAndy Whitcroft		our $clean = 0;
10896c72ffaaSAndy Whitcroft		our $cnt_chk++;
10906c72ffaaSAndy Whitcroft	}
1091de7d4f0eSAndy Whitcroft}
1092de7d4f0eSAndy Whitcroft
10936ecd9674SAndy Whitcroftsub check_absolute_file {
10946ecd9674SAndy Whitcroft	my ($absolute, $herecurr) = @_;
10956ecd9674SAndy Whitcroft	my $file = $absolute;
10966ecd9674SAndy Whitcroft
10976ecd9674SAndy Whitcroft	##print "absolute<$absolute>\n";
10986ecd9674SAndy Whitcroft
10996ecd9674SAndy Whitcroft	# See if any suffix of this path is a path within the tree.
11006ecd9674SAndy Whitcroft	while ($file =~ s@^[^/]*/@@) {
11016ecd9674SAndy Whitcroft		if (-f "$root/$file") {
11026ecd9674SAndy Whitcroft			##print "file<$file>\n";
11036ecd9674SAndy Whitcroft			last;
11046ecd9674SAndy Whitcroft		}
11056ecd9674SAndy Whitcroft	}
11066ecd9674SAndy Whitcroft	if (! -f _)  {
11076ecd9674SAndy Whitcroft		return 0;
11086ecd9674SAndy Whitcroft	}
11096ecd9674SAndy Whitcroft
11106ecd9674SAndy Whitcroft	# It is, so see if the prefix is acceptable.
11116ecd9674SAndy Whitcroft	my $prefix = $absolute;
11126ecd9674SAndy Whitcroft	substr($prefix, -length($file)) = '';
11136ecd9674SAndy Whitcroft
11146ecd9674SAndy Whitcroft	##print "prefix<$prefix>\n";
11156ecd9674SAndy Whitcroft	if ($prefix ne ".../") {
11166ecd9674SAndy Whitcroft		WARN("use relative pathname instead of absolute in changelog text\n" . $herecurr);
11176ecd9674SAndy Whitcroft	}
11186ecd9674SAndy Whitcroft}
11196ecd9674SAndy Whitcroft
11200a920b5bSAndy Whitcroftsub process {
11210a920b5bSAndy Whitcroft	my $filename = shift;
11220a920b5bSAndy Whitcroft
11230a920b5bSAndy Whitcroft	my $linenr=0;
11240a920b5bSAndy Whitcroft	my $prevline="";
1125c2fdda0dSAndy Whitcroft	my $prevrawline="";
11260a920b5bSAndy Whitcroft	my $stashline="";
1127c2fdda0dSAndy Whitcroft	my $stashrawline="";
11280a920b5bSAndy Whitcroft
11294a0df2efSAndy Whitcroft	my $length;
11300a920b5bSAndy Whitcroft	my $indent;
11310a920b5bSAndy Whitcroft	my $previndent=0;
11320a920b5bSAndy Whitcroft	my $stashindent=0;
11330a920b5bSAndy Whitcroft
1134de7d4f0eSAndy Whitcroft	our $clean = 1;
11350a920b5bSAndy Whitcroft	my $signoff = 0;
11360a920b5bSAndy Whitcroft	my $is_patch = 0;
11370a920b5bSAndy Whitcroft
113813214adfSAndy Whitcroft	our @report = ();
11396c72ffaaSAndy Whitcroft	our $cnt_lines = 0;
11406c72ffaaSAndy Whitcroft	our $cnt_error = 0;
11416c72ffaaSAndy Whitcroft	our $cnt_warn = 0;
11426c72ffaaSAndy Whitcroft	our $cnt_chk = 0;
11436c72ffaaSAndy Whitcroft
11440a920b5bSAndy Whitcroft	# Trace the real file/line as we go.
11450a920b5bSAndy Whitcroft	my $realfile = '';
11460a920b5bSAndy Whitcroft	my $realline = 0;
11470a920b5bSAndy Whitcroft	my $realcnt = 0;
11480a920b5bSAndy Whitcroft	my $here = '';
11490a920b5bSAndy Whitcroft	my $in_comment = 0;
1150c2fdda0dSAndy Whitcroft	my $comment_edge = 0;
11510a920b5bSAndy Whitcroft	my $first_line = 0;
11521e855726SWolfram Sang	my $p1_prefix = '';
11530a920b5bSAndy Whitcroft
115413214adfSAndy Whitcroft	my $prev_values = 'E';
115513214adfSAndy Whitcroft
115613214adfSAndy Whitcroft	# suppression flags
1157773647a0SAndy Whitcroft	my %suppress_ifbraces;
1158170d3a22SAndy Whitcroft	my %suppress_whiletrailers;
11592b474a1aSAndy Whitcroft	my %suppress_export;
1160653d4876SAndy Whitcroft
1161c2fdda0dSAndy Whitcroft	# Pre-scan the patch sanitizing the lines.
1162de7d4f0eSAndy Whitcroft	# Pre-scan the patch looking for any __setup documentation.
1163c2fdda0dSAndy Whitcroft	#
1164de7d4f0eSAndy Whitcroft	my @setup_docs = ();
1165de7d4f0eSAndy Whitcroft	my $setup_docs = 0;
1166773647a0SAndy Whitcroft
1167773647a0SAndy Whitcroft	sanitise_line_reset();
1168c2fdda0dSAndy Whitcroft	my $line;
1169c2fdda0dSAndy Whitcroft	foreach my $rawline (@rawlines) {
1170773647a0SAndy Whitcroft		$linenr++;
1171773647a0SAndy Whitcroft		$line = $rawline;
1172c2fdda0dSAndy Whitcroft
1173773647a0SAndy Whitcroft		if ($rawline=~/^\+\+\+\s+(\S+)/) {
1174de7d4f0eSAndy Whitcroft			$setup_docs = 0;
1175de7d4f0eSAndy Whitcroft			if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
1176de7d4f0eSAndy Whitcroft				$setup_docs = 1;
1177de7d4f0eSAndy Whitcroft			}
1178773647a0SAndy Whitcroft			#next;
1179de7d4f0eSAndy Whitcroft		}
1180773647a0SAndy Whitcroft		if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1181773647a0SAndy Whitcroft			$realline=$1-1;
1182773647a0SAndy Whitcroft			if (defined $2) {
1183773647a0SAndy Whitcroft				$realcnt=$3+1;
1184773647a0SAndy Whitcroft			} else {
1185773647a0SAndy Whitcroft				$realcnt=1+1;
1186773647a0SAndy Whitcroft			}
1187c45dcabdSAndy Whitcroft			$in_comment = 0;
1188773647a0SAndy Whitcroft
1189773647a0SAndy Whitcroft			# Guestimate if this is a continuing comment.  Run
1190773647a0SAndy Whitcroft			# the context looking for a comment "edge".  If this
1191773647a0SAndy Whitcroft			# edge is a close comment then we must be in a comment
1192773647a0SAndy Whitcroft			# at context start.
1193773647a0SAndy Whitcroft			my $edge;
119401fa9147SAndy Whitcroft			my $cnt = $realcnt;
119501fa9147SAndy Whitcroft			for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
119601fa9147SAndy Whitcroft				next if (defined $rawlines[$ln - 1] &&
119701fa9147SAndy Whitcroft					 $rawlines[$ln - 1] =~ /^-/);
119801fa9147SAndy Whitcroft				$cnt--;
119901fa9147SAndy Whitcroft				#print "RAW<$rawlines[$ln - 1]>\n";
1200721c1cb6SAndy Whitcroft				last if (!defined $rawlines[$ln - 1]);
1201fae17daeSAndy Whitcroft				if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
1202fae17daeSAndy Whitcroft				    $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
1203fae17daeSAndy Whitcroft					($edge) = $1;
1204fae17daeSAndy Whitcroft					last;
1205fae17daeSAndy Whitcroft				}
1206773647a0SAndy Whitcroft			}
1207773647a0SAndy Whitcroft			if (defined $edge && $edge eq '*/') {
1208773647a0SAndy Whitcroft				$in_comment = 1;
1209773647a0SAndy Whitcroft			}
1210773647a0SAndy Whitcroft
1211773647a0SAndy Whitcroft			# Guestimate if this is a continuing comment.  If this
1212773647a0SAndy Whitcroft			# is the start of a diff block and this line starts
1213773647a0SAndy Whitcroft			# ' *' then it is very likely a comment.
1214773647a0SAndy Whitcroft			if (!defined $edge &&
121583242e0cSAndy Whitcroft			    $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
1216773647a0SAndy Whitcroft			{
1217773647a0SAndy Whitcroft				$in_comment = 1;
1218773647a0SAndy Whitcroft			}
1219773647a0SAndy Whitcroft
1220773647a0SAndy Whitcroft			##print "COMMENT:$in_comment edge<$edge> $rawline\n";
1221773647a0SAndy Whitcroft			sanitise_line_reset($in_comment);
1222773647a0SAndy Whitcroft
1223171ae1a4SAndy Whitcroft		} elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
1224773647a0SAndy Whitcroft			# Standardise the strings and chars within the input to
1225171ae1a4SAndy Whitcroft			# simplify matching -- only bother with positive lines.
1226773647a0SAndy Whitcroft			$line = sanitise_line($rawline);
1227773647a0SAndy Whitcroft		}
1228773647a0SAndy Whitcroft		push(@lines, $line);
1229773647a0SAndy Whitcroft
1230773647a0SAndy Whitcroft		if ($realcnt > 1) {
1231773647a0SAndy Whitcroft			$realcnt-- if ($line =~ /^(?:\+| |$)/);
1232773647a0SAndy Whitcroft		} else {
1233773647a0SAndy Whitcroft			$realcnt = 0;
1234773647a0SAndy Whitcroft		}
1235773647a0SAndy Whitcroft
1236773647a0SAndy Whitcroft		#print "==>$rawline\n";
1237773647a0SAndy Whitcroft		#print "-->$line\n";
1238de7d4f0eSAndy Whitcroft
1239de7d4f0eSAndy Whitcroft		if ($setup_docs && $line =~ /^\+/) {
1240de7d4f0eSAndy Whitcroft			push(@setup_docs, $line);
1241de7d4f0eSAndy Whitcroft		}
1242de7d4f0eSAndy Whitcroft	}
1243de7d4f0eSAndy Whitcroft
12446c72ffaaSAndy Whitcroft	$prefix = '';
12456c72ffaaSAndy Whitcroft
1246773647a0SAndy Whitcroft	$realcnt = 0;
1247773647a0SAndy Whitcroft	$linenr = 0;
12480a920b5bSAndy Whitcroft	foreach my $line (@lines) {
12490a920b5bSAndy Whitcroft		$linenr++;
12500a920b5bSAndy Whitcroft
1251c2fdda0dSAndy Whitcroft		my $rawline = $rawlines[$linenr - 1];
12526c72ffaaSAndy Whitcroft
12530a920b5bSAndy Whitcroft#extract the line range in the file after the patch is applied
12546c72ffaaSAndy Whitcroft		if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
12550a920b5bSAndy Whitcroft			$is_patch = 1;
12564a0df2efSAndy Whitcroft			$first_line = $linenr + 1;
12570a920b5bSAndy Whitcroft			$realline=$1-1;
12580a920b5bSAndy Whitcroft			if (defined $2) {
12590a920b5bSAndy Whitcroft				$realcnt=$3+1;
12600a920b5bSAndy Whitcroft			} else {
12610a920b5bSAndy Whitcroft				$realcnt=1+1;
12620a920b5bSAndy Whitcroft			}
1263c2fdda0dSAndy Whitcroft			annotate_reset();
126413214adfSAndy Whitcroft			$prev_values = 'E';
126513214adfSAndy Whitcroft
1266773647a0SAndy Whitcroft			%suppress_ifbraces = ();
1267170d3a22SAndy Whitcroft			%suppress_whiletrailers = ();
12682b474a1aSAndy Whitcroft			%suppress_export = ();
12690a920b5bSAndy Whitcroft			next;
12700a920b5bSAndy Whitcroft
12714a0df2efSAndy Whitcroft# track the line number as we move through the hunk, note that
12724a0df2efSAndy Whitcroft# new versions of GNU diff omit the leading space on completely
12734a0df2efSAndy Whitcroft# blank context lines so we need to count that too.
1274773647a0SAndy Whitcroft		} elsif ($line =~ /^( |\+|$)/) {
12750a920b5bSAndy Whitcroft			$realline++;
1276d8aaf121SAndy Whitcroft			$realcnt-- if ($realcnt != 0);
12770a920b5bSAndy Whitcroft
12784a0df2efSAndy Whitcroft			# Measure the line length and indent.
1279c2fdda0dSAndy Whitcroft			($length, $indent) = line_stats($rawline);
12800a920b5bSAndy Whitcroft
12810a920b5bSAndy Whitcroft			# Track the previous line.
12820a920b5bSAndy Whitcroft			($prevline, $stashline) = ($stashline, $line);
12830a920b5bSAndy Whitcroft			($previndent, $stashindent) = ($stashindent, $indent);
1284c2fdda0dSAndy Whitcroft			($prevrawline, $stashrawline) = ($stashrawline, $rawline);
1285c2fdda0dSAndy Whitcroft
1286773647a0SAndy Whitcroft			#warn "line<$line>\n";
12876c72ffaaSAndy Whitcroft
1288d8aaf121SAndy Whitcroft		} elsif ($realcnt == 1) {
1289d8aaf121SAndy Whitcroft			$realcnt--;
12900a920b5bSAndy Whitcroft		}
12910a920b5bSAndy Whitcroft
1292cc77cdcaSAndy Whitcroft		my $hunk_line = ($realcnt != 0);
1293cc77cdcaSAndy Whitcroft
12940a920b5bSAndy Whitcroft#make up the handle for any error we report on this line
1295773647a0SAndy Whitcroft		$prefix = "$filename:$realline: " if ($emacs && $file);
1296773647a0SAndy Whitcroft		$prefix = "$filename:$linenr: " if ($emacs && !$file);
1297773647a0SAndy Whitcroft
12986c72ffaaSAndy Whitcroft		$here = "#$linenr: " if (!$file);
12996c72ffaaSAndy Whitcroft		$here = "#$realline: " if ($file);
1300773647a0SAndy Whitcroft
1301773647a0SAndy Whitcroft		# extract the filename as it passes
1302773647a0SAndy Whitcroft		if ($line=~/^\+\+\+\s+(\S+)/) {
1303773647a0SAndy Whitcroft			$realfile = $1;
13041e855726SWolfram Sang			$realfile =~ s@^([^/]*)/@@;
13051e855726SWolfram Sang
13061e855726SWolfram Sang			$p1_prefix = $1;
1307e2f7aa4bSAndy Whitcroft			if (!$file && $tree && $p1_prefix ne '' &&
1308e2f7aa4bSAndy Whitcroft			    -e "$root/$p1_prefix") {
13091e855726SWolfram Sang				WARN("patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
13101e855726SWolfram Sang			}
1311773647a0SAndy Whitcroft
1312c1ab3326SAndy Whitcroft			if ($realfile =~ m@^include/asm/@) {
1313773647a0SAndy Whitcroft				ERROR("do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
1314773647a0SAndy Whitcroft			}
1315773647a0SAndy Whitcroft			next;
1316773647a0SAndy Whitcroft		}
1317773647a0SAndy Whitcroft
1318389834b6SRandy Dunlap		$here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
13190a920b5bSAndy Whitcroft
1320c2fdda0dSAndy Whitcroft		my $hereline = "$here\n$rawline\n";
1321c2fdda0dSAndy Whitcroft		my $herecurr = "$here\n$rawline\n";
1322c2fdda0dSAndy Whitcroft		my $hereprev = "$here\n$prevrawline\n$rawline\n";
13230a920b5bSAndy Whitcroft
13246c72ffaaSAndy Whitcroft		$cnt_lines++ if ($realcnt != 0);
13256c72ffaaSAndy Whitcroft
13260a920b5bSAndy Whitcroft#check the patch for a signoff:
1327d8aaf121SAndy Whitcroft		if ($line =~ /^\s*signed-off-by:/i) {
13284a0df2efSAndy Whitcroft			# This is a signoff, if ugly, so do not double report.
13294a0df2efSAndy Whitcroft			$signoff++;
13300a920b5bSAndy Whitcroft			if (!($line =~ /^\s*Signed-off-by:/)) {
1331de7d4f0eSAndy Whitcroft				WARN("Signed-off-by: is the preferred form\n" .
1332de7d4f0eSAndy Whitcroft					$herecurr);
13330a920b5bSAndy Whitcroft			}
13340a920b5bSAndy Whitcroft			if ($line =~ /^\s*signed-off-by:\S/i) {
1335773647a0SAndy Whitcroft				WARN("space required after Signed-off-by:\n" .
1336de7d4f0eSAndy Whitcroft					$herecurr);
13370a920b5bSAndy Whitcroft			}
13380a920b5bSAndy Whitcroft		}
13390a920b5bSAndy Whitcroft
134000df344fSAndy Whitcroft# Check for wrappage within a valid hunk of the file
13418905a67cSAndy Whitcroft		if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
1342de7d4f0eSAndy Whitcroft			ERROR("patch seems to be corrupt (line wrapped?)\n" .
13436c72ffaaSAndy Whitcroft				$herecurr) if (!$emitted_corrupt++);
1344de7d4f0eSAndy Whitcroft		}
1345de7d4f0eSAndy Whitcroft
13466ecd9674SAndy Whitcroft# Check for absolute kernel paths.
13476ecd9674SAndy Whitcroft		if ($tree) {
13486ecd9674SAndy Whitcroft			while ($line =~ m{(?:^|\s)(/\S*)}g) {
13496ecd9674SAndy Whitcroft				my $file = $1;
13506ecd9674SAndy Whitcroft
13516ecd9674SAndy Whitcroft				if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
13526ecd9674SAndy Whitcroft				    check_absolute_file($1, $herecurr)) {
13536ecd9674SAndy Whitcroft					#
13546ecd9674SAndy Whitcroft				} else {
13556ecd9674SAndy Whitcroft					check_absolute_file($file, $herecurr);
13566ecd9674SAndy Whitcroft				}
13576ecd9674SAndy Whitcroft			}
13586ecd9674SAndy Whitcroft		}
13596ecd9674SAndy Whitcroft
1360de7d4f0eSAndy Whitcroft# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
1361de7d4f0eSAndy Whitcroft		if (($realfile =~ /^$/ || $line =~ /^\+/) &&
1362171ae1a4SAndy Whitcroft		    $rawline !~ m/^$UTF8*$/) {
1363171ae1a4SAndy Whitcroft			my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
1364171ae1a4SAndy Whitcroft
1365171ae1a4SAndy Whitcroft			my $blank = copy_spacing($rawline);
1366171ae1a4SAndy Whitcroft			my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
1367171ae1a4SAndy Whitcroft			my $hereptr = "$hereline$ptr\n";
1368171ae1a4SAndy Whitcroft
1369171ae1a4SAndy Whitcroft			ERROR("Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
137000df344fSAndy Whitcroft		}
13710a920b5bSAndy Whitcroft
137230670854SAndy Whitcroft# ignore non-hunk lines and lines being removed
137330670854SAndy Whitcroft		next if (!$hunk_line || $line =~ /^-/);
137400df344fSAndy Whitcroft
13750a920b5bSAndy Whitcroft#trailing whitespace
13769c0ca6f9SAndy Whitcroft		if ($line =~ /^\+.*\015/) {
1377c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
13789c0ca6f9SAndy Whitcroft			ERROR("DOS line endings\n" . $herevet);
13799c0ca6f9SAndy Whitcroft
1380c2fdda0dSAndy Whitcroft		} elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
1381c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1382de7d4f0eSAndy Whitcroft			ERROR("trailing whitespace\n" . $herevet);
13830a920b5bSAndy Whitcroft		}
13845368df20SAndy Whitcroft
13855368df20SAndy Whitcroft# check we are in a valid source file if not then ignore this hunk
13865368df20SAndy Whitcroft		next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
13875368df20SAndy Whitcroft
13880a920b5bSAndy Whitcroft#80 column limit
1389c45dcabdSAndy Whitcroft		if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ &&
1390f4c014c0SAndy Whitcroft		    $rawline !~ /^.\s*\*\s*\@$Ident\s/ &&
1391691e669bSJoe Perches		    $line !~ /^\+\s*$logFunctions\s*\(\s*(?:KERN_\S+\s*)?"[X\t]*"\s*(?:,|\)\s*;)\s*$/ &&
1392f4c014c0SAndy Whitcroft		    $length > 80)
1393c45dcabdSAndy Whitcroft		{
1394de7d4f0eSAndy Whitcroft			WARN("line over 80 characters\n" . $herecurr);
13950a920b5bSAndy Whitcroft		}
13960a920b5bSAndy Whitcroft
1397*5e79d96eSJoe Perches# check for spaces before a quoted newline
1398*5e79d96eSJoe Perches		if ($rawline =~ /^.*\".*\s\\n/) {
1399*5e79d96eSJoe Perches			WARN("unnecessary whitespace before a quoted newline\n" . $herecurr);
1400*5e79d96eSJoe Perches		}
1401*5e79d96eSJoe Perches
14028905a67cSAndy Whitcroft# check for adding lines without a newline.
14038905a67cSAndy Whitcroft		if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
14048905a67cSAndy Whitcroft			WARN("adding a line without newline at end of file\n" . $herecurr);
14058905a67cSAndy Whitcroft		}
14068905a67cSAndy Whitcroft
140742e41c54SMike Frysinger# Blackfin: use hi/lo macros
140842e41c54SMike Frysinger		if ($realfile =~ m@arch/blackfin/.*\.S$@) {
140942e41c54SMike Frysinger			if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
141042e41c54SMike Frysinger				my $herevet = "$here\n" . cat_vet($line) . "\n";
141142e41c54SMike Frysinger				ERROR("use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
141242e41c54SMike Frysinger			}
141342e41c54SMike Frysinger			if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
141442e41c54SMike Frysinger				my $herevet = "$here\n" . cat_vet($line) . "\n";
141542e41c54SMike Frysinger				ERROR("use the HI() macro, not (... >> 16)\n" . $herevet);
141642e41c54SMike Frysinger			}
141742e41c54SMike Frysinger		}
141842e41c54SMike Frysinger
1419b9ea10d6SAndy Whitcroft# check we are in a valid source file C or perl if not then ignore this hunk
1420b9ea10d6SAndy Whitcroft		next if ($realfile !~ /\.(h|c|pl)$/);
14210a920b5bSAndy Whitcroft
14220a920b5bSAndy Whitcroft# at the beginning of a line any tabs must come first and anything
14230a920b5bSAndy Whitcroft# more than 8 must use tabs.
1424c2fdda0dSAndy Whitcroft		if ($rawline =~ /^\+\s* \t\s*\S/ ||
1425c2fdda0dSAndy Whitcroft		    $rawline =~ /^\+\s*        \s*/) {
1426c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1427171ae1a4SAndy Whitcroft			ERROR("code indent should use tabs where possible\n" . $herevet);
14280a920b5bSAndy Whitcroft		}
14290a920b5bSAndy Whitcroft
143008e44365SAlberto Panizzo# check for space before tabs.
143108e44365SAlberto Panizzo		if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
143208e44365SAlberto Panizzo			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
143308e44365SAlberto Panizzo			WARN("please, no space before tabs\n" . $herevet);
143408e44365SAlberto Panizzo		}
143508e44365SAlberto Panizzo
1436b9ea10d6SAndy Whitcroft# check we are in a valid C source file if not then ignore this hunk
1437b9ea10d6SAndy Whitcroft		next if ($realfile !~ /\.(h|c)$/);
1438b9ea10d6SAndy Whitcroft
1439c2fdda0dSAndy Whitcroft# check for RCS/CVS revision markers
1440cf655043SAndy Whitcroft		if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
1441c2fdda0dSAndy Whitcroft			WARN("CVS style keyword markers, these will _not_ be updated\n". $herecurr);
1442c2fdda0dSAndy Whitcroft		}
144322f2a2efSAndy Whitcroft
144442e41c54SMike Frysinger# Blackfin: don't use __builtin_bfin_[cs]sync
144542e41c54SMike Frysinger		if ($line =~ /__builtin_bfin_csync/) {
144642e41c54SMike Frysinger			my $herevet = "$here\n" . cat_vet($line) . "\n";
144742e41c54SMike Frysinger			ERROR("use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
144842e41c54SMike Frysinger		}
144942e41c54SMike Frysinger		if ($line =~ /__builtin_bfin_ssync/) {
145042e41c54SMike Frysinger			my $herevet = "$here\n" . cat_vet($line) . "\n";
145142e41c54SMike Frysinger			ERROR("use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
145242e41c54SMike Frysinger		}
145342e41c54SMike Frysinger
14549c0ca6f9SAndy Whitcroft# Check for potential 'bare' types
14552b474a1aSAndy Whitcroft		my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
14562b474a1aSAndy Whitcroft		    $realline_next);
14579c9ba34eSAndy Whitcroft		if ($realcnt && $line =~ /.\s*\S/) {
1458170d3a22SAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
1459f5fe35ddSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0);
1460171ae1a4SAndy Whitcroft			$stat =~ s/\n./\n /g;
1461171ae1a4SAndy Whitcroft			$cond =~ s/\n./\n /g;
1462171ae1a4SAndy Whitcroft
14632b474a1aSAndy Whitcroft			# Find the real next line.
14642b474a1aSAndy Whitcroft			$realline_next = $line_nr_next;
14652b474a1aSAndy Whitcroft			if (defined $realline_next &&
14662b474a1aSAndy Whitcroft			    (!defined $lines[$realline_next - 1] ||
14672b474a1aSAndy Whitcroft			     substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
14682b474a1aSAndy Whitcroft				$realline_next++;
14692b474a1aSAndy Whitcroft			}
14702b474a1aSAndy Whitcroft
1471171ae1a4SAndy Whitcroft			my $s = $stat;
1472171ae1a4SAndy Whitcroft			$s =~ s/{.*$//s;
1473cf655043SAndy Whitcroft
1474c2fdda0dSAndy Whitcroft			# Ignore goto labels.
1475171ae1a4SAndy Whitcroft			if ($s =~ /$Ident:\*$/s) {
1476c2fdda0dSAndy Whitcroft
1477c2fdda0dSAndy Whitcroft			# Ignore functions being called
1478171ae1a4SAndy Whitcroft			} elsif ($s =~ /^.\s*$Ident\s*\(/s) {
1479c2fdda0dSAndy Whitcroft
1480463f2864SAndy Whitcroft			} elsif ($s =~ /^.\s*else\b/s) {
1481463f2864SAndy Whitcroft
1482c45dcabdSAndy Whitcroft			# declarations always start with types
1483d2506586SAndy 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) {
1484c45dcabdSAndy Whitcroft				my $type = $1;
1485c45dcabdSAndy Whitcroft				$type =~ s/\s+/ /g;
1486c45dcabdSAndy Whitcroft				possible($type, "A:" . $s);
1487c45dcabdSAndy Whitcroft
14886c72ffaaSAndy Whitcroft			# definitions in global scope can only start with types
1489a6a84062SAndy Whitcroft			} elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
1490c45dcabdSAndy Whitcroft				possible($1, "B:" . $s);
1491c2fdda0dSAndy Whitcroft			}
14928905a67cSAndy Whitcroft
14936c72ffaaSAndy Whitcroft			# any (foo ... *) is a pointer cast, and foo is a type
149465863862SAndy Whitcroft			while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
1495c45dcabdSAndy Whitcroft				possible($1, "C:" . $s);
14969c0ca6f9SAndy Whitcroft			}
14978905a67cSAndy Whitcroft
14988905a67cSAndy Whitcroft			# Check for any sort of function declaration.
14998905a67cSAndy Whitcroft			# int foo(something bar, other baz);
15008905a67cSAndy Whitcroft			# void (*store_gdt)(x86_descr_ptr *);
1501171ae1a4SAndy Whitcroft			if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
15028905a67cSAndy Whitcroft				my ($name_len) = length($1);
15038905a67cSAndy Whitcroft
1504cf655043SAndy Whitcroft				my $ctx = $s;
1505773647a0SAndy Whitcroft				substr($ctx, 0, $name_len + 1, '');
15068905a67cSAndy Whitcroft				$ctx =~ s/\)[^\)]*$//;
1507cf655043SAndy Whitcroft
15088905a67cSAndy Whitcroft				for my $arg (split(/\s*,\s*/, $ctx)) {
1509c45dcabdSAndy Whitcroft					if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
15108905a67cSAndy Whitcroft
1511c45dcabdSAndy Whitcroft						possible($1, "D:" . $s);
15128905a67cSAndy Whitcroft					}
15138905a67cSAndy Whitcroft				}
15148905a67cSAndy Whitcroft			}
15158905a67cSAndy Whitcroft
15169c0ca6f9SAndy Whitcroft		}
15179c0ca6f9SAndy Whitcroft
151800df344fSAndy Whitcroft#
151900df344fSAndy Whitcroft# Checks which may be anchored in the context.
152000df344fSAndy Whitcroft#
152100df344fSAndy Whitcroft
152200df344fSAndy Whitcroft# Check for switch () and associated case and default
152300df344fSAndy Whitcroft# statements should be at the same indent.
152400df344fSAndy Whitcroft		if ($line=~/\bswitch\s*\(.*\)/) {
152500df344fSAndy Whitcroft			my $err = '';
152600df344fSAndy Whitcroft			my $sep = '';
152700df344fSAndy Whitcroft			my @ctx = ctx_block_outer($linenr, $realcnt);
152800df344fSAndy Whitcroft			shift(@ctx);
152900df344fSAndy Whitcroft			for my $ctx (@ctx) {
153000df344fSAndy Whitcroft				my ($clen, $cindent) = line_stats($ctx);
153100df344fSAndy Whitcroft				if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
153200df344fSAndy Whitcroft							$indent != $cindent) {
153300df344fSAndy Whitcroft					$err .= "$sep$ctx\n";
153400df344fSAndy Whitcroft					$sep = '';
153500df344fSAndy Whitcroft				} else {
153600df344fSAndy Whitcroft					$sep = "[...]\n";
153700df344fSAndy Whitcroft				}
153800df344fSAndy Whitcroft			}
153900df344fSAndy Whitcroft			if ($err ne '') {
15409c0ca6f9SAndy Whitcroft				ERROR("switch and case should be at the same indent\n$hereline$err");
1541de7d4f0eSAndy Whitcroft			}
1542de7d4f0eSAndy Whitcroft		}
1543de7d4f0eSAndy Whitcroft
1544de7d4f0eSAndy Whitcroft# if/while/etc brace do not go on next line, unless defining a do while loop,
1545de7d4f0eSAndy Whitcroft# or if that brace on the next line is for something else
1546c45dcabdSAndy Whitcroft		if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
1547773647a0SAndy Whitcroft			my $pre_ctx = "$1$2";
1548773647a0SAndy Whitcroft
15499c0ca6f9SAndy Whitcroft			my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
1550de7d4f0eSAndy Whitcroft			my $ctx_cnt = $realcnt - $#ctx - 1;
1551de7d4f0eSAndy Whitcroft			my $ctx = join("\n", @ctx);
1552de7d4f0eSAndy Whitcroft
1553548596d5SAndy Whitcroft			my $ctx_ln = $linenr;
1554548596d5SAndy Whitcroft			my $ctx_skip = $realcnt;
1555de7d4f0eSAndy Whitcroft
1556548596d5SAndy Whitcroft			while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
1557548596d5SAndy Whitcroft					defined $lines[$ctx_ln - 1] &&
1558548596d5SAndy Whitcroft					$lines[$ctx_ln - 1] =~ /^-/)) {
1559548596d5SAndy Whitcroft				##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
1560548596d5SAndy Whitcroft				$ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
1561773647a0SAndy Whitcroft				$ctx_ln++;
1562773647a0SAndy Whitcroft			}
1563548596d5SAndy Whitcroft
156453210168SAndy Whitcroft			#print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
156553210168SAndy Whitcroft			#print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
1566773647a0SAndy Whitcroft
1567773647a0SAndy Whitcroft			if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln -1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
1568773647a0SAndy Whitcroft				ERROR("that open brace { should be on the previous line\n" .
1569c45dcabdSAndy Whitcroft					"$here\n$ctx\n$lines[$ctx_ln - 1]\n");
157000df344fSAndy Whitcroft			}
1571773647a0SAndy Whitcroft			if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
1572773647a0SAndy Whitcroft			    $ctx =~ /\)\s*\;\s*$/ &&
1573773647a0SAndy Whitcroft			    defined $lines[$ctx_ln - 1])
1574773647a0SAndy Whitcroft			{
15759c0ca6f9SAndy Whitcroft				my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
15769c0ca6f9SAndy Whitcroft				if ($nindent > $indent) {
1577773647a0SAndy Whitcroft					WARN("trailing semicolon indicates no statements, indent implies otherwise\n" .
1578c45dcabdSAndy Whitcroft						"$here\n$ctx\n$lines[$ctx_ln - 1]\n");
15799c0ca6f9SAndy Whitcroft				}
15809c0ca6f9SAndy Whitcroft			}
158100df344fSAndy Whitcroft		}
158200df344fSAndy Whitcroft
15834d001e4dSAndy Whitcroft# Check relative indent for conditionals and blocks.
15844d001e4dSAndy Whitcroft		if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
15854d001e4dSAndy Whitcroft			my ($s, $c) = ($stat, $cond);
15864d001e4dSAndy Whitcroft
15874d001e4dSAndy Whitcroft			substr($s, 0, length($c), '');
15884d001e4dSAndy Whitcroft
15894d001e4dSAndy Whitcroft			# Make sure we remove the line prefixes as we have
15904d001e4dSAndy Whitcroft			# none on the first line, and are going to readd them
15914d001e4dSAndy Whitcroft			# where necessary.
15924d001e4dSAndy Whitcroft			$s =~ s/\n./\n/gs;
15934d001e4dSAndy Whitcroft
15944d001e4dSAndy Whitcroft			# Find out how long the conditional actually is.
15956f779c18SAndy Whitcroft			my @newlines = ($c =~ /\n/gs);
15966f779c18SAndy Whitcroft			my $cond_lines = 1 + $#newlines;
15974d001e4dSAndy Whitcroft
15984d001e4dSAndy Whitcroft			# We want to check the first line inside the block
15994d001e4dSAndy Whitcroft			# starting at the end of the conditional, so remove:
16004d001e4dSAndy Whitcroft			#  1) any blank line termination
16014d001e4dSAndy Whitcroft			#  2) any opening brace { on end of the line
16024d001e4dSAndy Whitcroft			#  3) any do (...) {
16034d001e4dSAndy Whitcroft			my $continuation = 0;
16044d001e4dSAndy Whitcroft			my $check = 0;
16054d001e4dSAndy Whitcroft			$s =~ s/^.*\bdo\b//;
16064d001e4dSAndy Whitcroft			$s =~ s/^\s*{//;
16074d001e4dSAndy Whitcroft			if ($s =~ s/^\s*\\//) {
16084d001e4dSAndy Whitcroft				$continuation = 1;
16094d001e4dSAndy Whitcroft			}
16109bd49efeSAndy Whitcroft			if ($s =~ s/^\s*?\n//) {
16114d001e4dSAndy Whitcroft				$check = 1;
16124d001e4dSAndy Whitcroft				$cond_lines++;
16134d001e4dSAndy Whitcroft			}
16144d001e4dSAndy Whitcroft
16154d001e4dSAndy Whitcroft			# Also ignore a loop construct at the end of a
16164d001e4dSAndy Whitcroft			# preprocessor statement.
16174d001e4dSAndy Whitcroft			if (($prevline =~ /^.\s*#\s*define\s/ ||
16184d001e4dSAndy Whitcroft			    $prevline =~ /\\\s*$/) && $continuation == 0) {
16194d001e4dSAndy Whitcroft				$check = 0;
16204d001e4dSAndy Whitcroft			}
16214d001e4dSAndy Whitcroft
16229bd49efeSAndy Whitcroft			my $cond_ptr = -1;
1623740504c6SAndy Whitcroft			$continuation = 0;
16249bd49efeSAndy Whitcroft			while ($cond_ptr != $cond_lines) {
16259bd49efeSAndy Whitcroft				$cond_ptr = $cond_lines;
16264d001e4dSAndy Whitcroft
1627f16fa28fSAndy Whitcroft				# If we see an #else/#elif then the code
1628f16fa28fSAndy Whitcroft				# is not linear.
1629f16fa28fSAndy Whitcroft				if ($s =~ /^\s*\#\s*(?:else|elif)/) {
1630f16fa28fSAndy Whitcroft					$check = 0;
1631f16fa28fSAndy Whitcroft				}
1632f16fa28fSAndy Whitcroft
16339bd49efeSAndy Whitcroft				# Ignore:
16349bd49efeSAndy Whitcroft				#  1) blank lines, they should be at 0,
16359bd49efeSAndy Whitcroft				#  2) preprocessor lines, and
16369bd49efeSAndy Whitcroft				#  3) labels.
1637740504c6SAndy Whitcroft				if ($continuation ||
1638740504c6SAndy Whitcroft				    $s =~ /^\s*?\n/ ||
16399bd49efeSAndy Whitcroft				    $s =~ /^\s*#\s*?/ ||
16409bd49efeSAndy Whitcroft				    $s =~ /^\s*$Ident\s*:/) {
1641740504c6SAndy Whitcroft					$continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
164230dad6ebSAndy Whitcroft					if ($s =~ s/^.*?\n//) {
16439bd49efeSAndy Whitcroft						$cond_lines++;
16449bd49efeSAndy Whitcroft					}
16454d001e4dSAndy Whitcroft				}
164630dad6ebSAndy Whitcroft			}
16474d001e4dSAndy Whitcroft
16484d001e4dSAndy Whitcroft			my (undef, $sindent) = line_stats("+" . $s);
16494d001e4dSAndy Whitcroft			my $stat_real = raw_line($linenr, $cond_lines);
16504d001e4dSAndy Whitcroft
16514d001e4dSAndy Whitcroft			# Check if either of these lines are modified, else
16524d001e4dSAndy Whitcroft			# this is not this patch's fault.
16534d001e4dSAndy Whitcroft			if (!defined($stat_real) ||
16544d001e4dSAndy Whitcroft			    $stat !~ /^\+/ && $stat_real !~ /^\+/) {
16554d001e4dSAndy Whitcroft				$check = 0;
16564d001e4dSAndy Whitcroft			}
16574d001e4dSAndy Whitcroft			if (defined($stat_real) && $cond_lines > 1) {
16584d001e4dSAndy Whitcroft				$stat_real = "[...]\n$stat_real";
16594d001e4dSAndy Whitcroft			}
16604d001e4dSAndy Whitcroft
16619bd49efeSAndy 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";
16624d001e4dSAndy Whitcroft
16634d001e4dSAndy Whitcroft			if ($check && (($sindent % 8) != 0 ||
16644d001e4dSAndy Whitcroft			    ($sindent <= $indent && $s ne ''))) {
16654d001e4dSAndy Whitcroft				WARN("suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
16664d001e4dSAndy Whitcroft			}
16674d001e4dSAndy Whitcroft		}
16684d001e4dSAndy Whitcroft
16696c72ffaaSAndy Whitcroft		# Track the 'values' across context and added lines.
16706c72ffaaSAndy Whitcroft		my $opline = $line; $opline =~ s/^./ /;
16711f65f947SAndy Whitcroft		my ($curr_values, $curr_vars) =
16721f65f947SAndy Whitcroft				annotate_values($opline . "\n", $prev_values);
16736c72ffaaSAndy Whitcroft		$curr_values = $prev_values . $curr_values;
1674c2fdda0dSAndy Whitcroft		if ($dbg_values) {
1675c2fdda0dSAndy Whitcroft			my $outline = $opline; $outline =~ s/\t/ /g;
1676cf655043SAndy Whitcroft			print "$linenr > .$outline\n";
1677cf655043SAndy Whitcroft			print "$linenr > $curr_values\n";
16781f65f947SAndy Whitcroft			print "$linenr >  $curr_vars\n";
1679c2fdda0dSAndy Whitcroft		}
16806c72ffaaSAndy Whitcroft		$prev_values = substr($curr_values, -1);
16816c72ffaaSAndy Whitcroft
168200df344fSAndy Whitcroft#ignore lines not being added
168300df344fSAndy Whitcroft		if ($line=~/^[^\+]/) {next;}
168400df344fSAndy Whitcroft
1685653d4876SAndy Whitcroft# TEST: allow direct testing of the type matcher.
16867429c690SAndy Whitcroft		if ($dbg_type) {
16877429c690SAndy Whitcroft			if ($line =~ /^.\s*$Declare\s*$/) {
16887429c690SAndy Whitcroft				ERROR("TEST: is type\n" . $herecurr);
16897429c690SAndy Whitcroft			} elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
16907429c690SAndy Whitcroft				ERROR("TEST: is not type ($1 is)\n". $herecurr);
16917429c690SAndy Whitcroft			}
1692653d4876SAndy Whitcroft			next;
1693653d4876SAndy Whitcroft		}
1694a1ef277eSAndy Whitcroft# TEST: allow direct testing of the attribute matcher.
1695a1ef277eSAndy Whitcroft		if ($dbg_attr) {
16969360b0e5SAndy Whitcroft			if ($line =~ /^.\s*$Modifier\s*$/) {
1697a1ef277eSAndy Whitcroft				ERROR("TEST: is attr\n" . $herecurr);
16989360b0e5SAndy Whitcroft			} elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
1699a1ef277eSAndy Whitcroft				ERROR("TEST: is not attr ($1 is)\n". $herecurr);
1700a1ef277eSAndy Whitcroft			}
1701a1ef277eSAndy Whitcroft			next;
1702a1ef277eSAndy Whitcroft		}
1703653d4876SAndy Whitcroft
1704f0a594c1SAndy Whitcroft# check for initialisation to aggregates open brace on the next line
170599423c20SAndy Whitcroft		if ($line =~ /^.\s*{/ &&
170699423c20SAndy Whitcroft		    $prevline =~ /(?:^|[^=])=\s*$/) {
1707773647a0SAndy Whitcroft			ERROR("that open brace { should be on the previous line\n" . $hereprev);
1708f0a594c1SAndy Whitcroft		}
1709f0a594c1SAndy Whitcroft
171000df344fSAndy Whitcroft#
171100df344fSAndy Whitcroft# Checks which are anchored on the added line.
171200df344fSAndy Whitcroft#
171300df344fSAndy Whitcroft
1714653d4876SAndy Whitcroft# check for malformed paths in #include statements (uses RAW line)
1715c45dcabdSAndy Whitcroft		if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
1716653d4876SAndy Whitcroft			my $path = $1;
1717653d4876SAndy Whitcroft			if ($path =~ m{//}) {
1718de7d4f0eSAndy Whitcroft				ERROR("malformed #include filename\n" .
1719de7d4f0eSAndy Whitcroft					$herecurr);
1720653d4876SAndy Whitcroft			}
1721653d4876SAndy Whitcroft		}
1722653d4876SAndy Whitcroft
172300df344fSAndy Whitcroft# no C99 // comments
172400df344fSAndy Whitcroft		if ($line =~ m{//}) {
1725de7d4f0eSAndy Whitcroft			ERROR("do not use C99 // comments\n" . $herecurr);
172600df344fSAndy Whitcroft		}
172700df344fSAndy Whitcroft		# Remove C99 comments.
17280a920b5bSAndy Whitcroft		$line =~ s@//.*@@;
17296c72ffaaSAndy Whitcroft		$opline =~ s@//.*@@;
17300a920b5bSAndy Whitcroft
17312b474a1aSAndy Whitcroft# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
17322b474a1aSAndy Whitcroft# the whole statement.
17332b474a1aSAndy Whitcroft#print "APW <$lines[$realline_next - 1]>\n";
17342b474a1aSAndy Whitcroft		if (defined $realline_next &&
17352b474a1aSAndy Whitcroft		    exists $lines[$realline_next - 1] &&
17362b474a1aSAndy Whitcroft		    !defined $suppress_export{$realline_next} &&
17372b474a1aSAndy Whitcroft		    ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
17382b474a1aSAndy Whitcroft		     $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
1739653d4876SAndy Whitcroft			my $name = $1;
17402b474a1aSAndy Whitcroft			if ($stat !~ /(?:
17412b474a1aSAndy Whitcroft				\n.}\s*$|
174248012058SAndy Whitcroft				^.DEFINE_$Ident\(\Q$name\E\)|
174348012058SAndy Whitcroft				^.DECLARE_$Ident\(\Q$name\E\)|
174448012058SAndy Whitcroft				^.LIST_HEAD\(\Q$name\E\)|
17452b474a1aSAndy Whitcroft				^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
17462b474a1aSAndy Whitcroft				\b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
174748012058SAndy Whitcroft			    )/x) {
17482b474a1aSAndy Whitcroft#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
17492b474a1aSAndy Whitcroft				$suppress_export{$realline_next} = 2;
17502b474a1aSAndy Whitcroft			} else {
17512b474a1aSAndy Whitcroft				$suppress_export{$realline_next} = 1;
17520a920b5bSAndy Whitcroft			}
17530a920b5bSAndy Whitcroft		}
17542b474a1aSAndy Whitcroft		if (!defined $suppress_export{$linenr} &&
17552b474a1aSAndy Whitcroft		    $prevline =~ /^.\s*$/ &&
17562b474a1aSAndy Whitcroft		    ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
17572b474a1aSAndy Whitcroft		     $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
17582b474a1aSAndy Whitcroft#print "FOO B <$lines[$linenr - 1]>\n";
17592b474a1aSAndy Whitcroft			$suppress_export{$linenr} = 2;
17602b474a1aSAndy Whitcroft		}
17612b474a1aSAndy Whitcroft		if (defined $suppress_export{$linenr} &&
17622b474a1aSAndy Whitcroft		    $suppress_export{$linenr} == 2) {
17632b474a1aSAndy Whitcroft			WARN("EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
17642b474a1aSAndy Whitcroft		}
17650a920b5bSAndy Whitcroft
1766f0a594c1SAndy Whitcroft# check for external initialisers.
1767c45dcabdSAndy Whitcroft		if ($line =~ /^.$Type\s*$Ident\s*(?:\s+$Modifier)*\s*=\s*(0|NULL|false)\s*;/) {
1768f0a594c1SAndy Whitcroft			ERROR("do not initialise externals to 0 or NULL\n" .
1769f0a594c1SAndy Whitcroft				$herecurr);
1770f0a594c1SAndy Whitcroft		}
17710a920b5bSAndy Whitcroft# check for static initialisers.
17722d1bafd7SAndy Whitcroft		if ($line =~ /\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
1773de7d4f0eSAndy Whitcroft			ERROR("do not initialise statics to 0 or NULL\n" .
1774de7d4f0eSAndy Whitcroft				$herecurr);
17750a920b5bSAndy Whitcroft		}
17760a920b5bSAndy Whitcroft
1777653d4876SAndy Whitcroft# check for new typedefs, only function parameters and sparse annotations
1778653d4876SAndy Whitcroft# make sense.
1779653d4876SAndy Whitcroft		if ($line =~ /\btypedef\s/ &&
17808054576dSAndy Whitcroft		    $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
1781c45dcabdSAndy Whitcroft		    $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
17828ed22cadSAndy Whitcroft		    $line !~ /\b$typeTypedefs\b/ &&
1783653d4876SAndy Whitcroft		    $line !~ /\b__bitwise(?:__|)\b/) {
1784de7d4f0eSAndy Whitcroft			WARN("do not add new typedefs\n" . $herecurr);
17850a920b5bSAndy Whitcroft		}
17860a920b5bSAndy Whitcroft
17870a920b5bSAndy Whitcroft# * goes on variable not on type
178865863862SAndy Whitcroft		# (char*[ const])
178900ef4eceSAndy Whitcroft		if ($line =~ m{\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\)}) {
179065863862SAndy Whitcroft			my ($from, $to) = ($1, $1);
1791d8aaf121SAndy Whitcroft
179265863862SAndy Whitcroft			# Should start with a space.
179365863862SAndy Whitcroft			$to =~ s/^(\S)/ $1/;
179465863862SAndy Whitcroft			# Should not end with a space.
179565863862SAndy Whitcroft			$to =~ s/\s+$//;
179665863862SAndy Whitcroft			# '*'s should not have spaces between.
1797f9a0b3d1SAndy Whitcroft			while ($to =~ s/\*\s+\*/\*\*/) {
179865863862SAndy Whitcroft			}
1799d8aaf121SAndy Whitcroft
180065863862SAndy Whitcroft			#print "from<$from> to<$to>\n";
180165863862SAndy Whitcroft			if ($from ne $to) {
180265863862SAndy Whitcroft				ERROR("\"(foo$from)\" should be \"(foo$to)\"\n" .  $herecurr);
180365863862SAndy Whitcroft			}
180400ef4eceSAndy Whitcroft		} elsif ($line =~ m{\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident)}) {
180565863862SAndy Whitcroft			my ($from, $to, $ident) = ($1, $1, $2);
1806d8aaf121SAndy Whitcroft
180765863862SAndy Whitcroft			# Should start with a space.
180865863862SAndy Whitcroft			$to =~ s/^(\S)/ $1/;
180965863862SAndy Whitcroft			# Should not end with a space.
181065863862SAndy Whitcroft			$to =~ s/\s+$//;
181165863862SAndy Whitcroft			# '*'s should not have spaces between.
1812f9a0b3d1SAndy Whitcroft			while ($to =~ s/\*\s+\*/\*\*/) {
181365863862SAndy Whitcroft			}
181465863862SAndy Whitcroft			# Modifiers should have spaces.
181565863862SAndy Whitcroft			$to =~ s/(\b$Modifier$)/$1 /;
181665863862SAndy Whitcroft
1817667026e7SAndy Whitcroft			#print "from<$from> to<$to> ident<$ident>\n";
1818667026e7SAndy Whitcroft			if ($from ne $to && $ident !~ /^$Modifier$/) {
181965863862SAndy Whitcroft				ERROR("\"foo${from}bar\" should be \"foo${to}bar\"\n" .  $herecurr);
182065863862SAndy Whitcroft			}
18210a920b5bSAndy Whitcroft		}
18220a920b5bSAndy Whitcroft
18230a920b5bSAndy Whitcroft# # no BUG() or BUG_ON()
18240a920b5bSAndy Whitcroft# 		if ($line =~ /\b(BUG|BUG_ON)\b/) {
18250a920b5bSAndy Whitcroft# 			print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
18260a920b5bSAndy Whitcroft# 			print "$herecurr";
18270a920b5bSAndy Whitcroft# 			$clean = 0;
18280a920b5bSAndy Whitcroft# 		}
18290a920b5bSAndy Whitcroft
18308905a67cSAndy Whitcroft		if ($line =~ /\bLINUX_VERSION_CODE\b/) {
1831c2fdda0dSAndy Whitcroft			WARN("LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
18328905a67cSAndy Whitcroft		}
18338905a67cSAndy Whitcroft
183400df344fSAndy Whitcroft# printk should use KERN_* levels.  Note that follow on printk's on the
183500df344fSAndy Whitcroft# same line do not need a level, so we use the current block context
183600df344fSAndy Whitcroft# to try and find and validate the current printk.  In summary the current
183700df344fSAndy Whitcroft# printk includes all preceeding printk's which have no newline on the end.
183800df344fSAndy Whitcroft# we assume the first bad printk is the one to report.
1839f0a594c1SAndy Whitcroft		if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
184000df344fSAndy Whitcroft			my $ok = 0;
184100df344fSAndy Whitcroft			for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
184200df344fSAndy Whitcroft				#print "CHECK<$lines[$ln - 1]\n";
184300df344fSAndy Whitcroft				# we have a preceeding printk if it ends
184400df344fSAndy Whitcroft				# with "\n" ignore it, else it is to blame
184500df344fSAndy Whitcroft				if ($lines[$ln - 1] =~ m{\bprintk\(}) {
184600df344fSAndy Whitcroft					if ($rawlines[$ln - 1] !~ m{\\n"}) {
184700df344fSAndy Whitcroft						$ok = 1;
184800df344fSAndy Whitcroft					}
184900df344fSAndy Whitcroft					last;
185000df344fSAndy Whitcroft				}
185100df344fSAndy Whitcroft			}
185200df344fSAndy Whitcroft			if ($ok == 0) {
1853de7d4f0eSAndy Whitcroft				WARN("printk() should include KERN_ facility level\n" . $herecurr);
18540a920b5bSAndy Whitcroft			}
185500df344fSAndy Whitcroft		}
18560a920b5bSAndy Whitcroft
1857653d4876SAndy Whitcroft# function brace can't be on same line, except for #defines of do while,
1858653d4876SAndy Whitcroft# or if closed on same line
1859c45dcabdSAndy Whitcroft		if (($line=~/$Type\s*$Ident\(.*\).*\s{/) and
1860c45dcabdSAndy Whitcroft		    !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) {
1861de7d4f0eSAndy Whitcroft			ERROR("open brace '{' following function declarations go on the next line\n" . $herecurr);
18620a920b5bSAndy Whitcroft		}
1863653d4876SAndy Whitcroft
18648905a67cSAndy Whitcroft# open braces for enum, union and struct go on the same line.
18658905a67cSAndy Whitcroft		if ($line =~ /^.\s*{/ &&
18668905a67cSAndy Whitcroft		    $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
18678905a67cSAndy Whitcroft			ERROR("open brace '{' following $1 go on the same line\n" . $hereprev);
18688905a67cSAndy Whitcroft		}
18698905a67cSAndy Whitcroft
18708d31cfceSAndy Whitcroft# check for spacing round square brackets; allowed:
18718d31cfceSAndy Whitcroft#  1. with a type on the left -- int [] a;
1872fe2a7dbcSAndy Whitcroft#  2. at the beginning of a line for slice initialisers -- [0...10] = 5,
1873fe2a7dbcSAndy Whitcroft#  3. inside a curly brace -- = { [0...10] = 5 }
18748d31cfceSAndy Whitcroft		while ($line =~ /(.*?\s)\[/g) {
18758d31cfceSAndy Whitcroft			my ($where, $prefix) = ($-[1], $1);
18768d31cfceSAndy Whitcroft			if ($prefix !~ /$Type\s+$/ &&
1877fe2a7dbcSAndy Whitcroft			    ($where != 0 || $prefix !~ /^.\s+$/) &&
1878fe2a7dbcSAndy Whitcroft			    $prefix !~ /{\s+$/) {
18798d31cfceSAndy Whitcroft				ERROR("space prohibited before open square bracket '['\n" . $herecurr);
18808d31cfceSAndy Whitcroft			}
18818d31cfceSAndy Whitcroft		}
18828d31cfceSAndy Whitcroft
1883f0a594c1SAndy Whitcroft# check for spaces between functions and their parentheses.
18846c72ffaaSAndy Whitcroft		while ($line =~ /($Ident)\s+\(/g) {
1885c2fdda0dSAndy Whitcroft			my $name = $1;
1886773647a0SAndy Whitcroft			my $ctx_before = substr($line, 0, $-[1]);
1887773647a0SAndy Whitcroft			my $ctx = "$ctx_before$name";
1888c2fdda0dSAndy Whitcroft
1889c2fdda0dSAndy Whitcroft			# Ignore those directives where spaces _are_ permitted.
1890773647a0SAndy Whitcroft			if ($name =~ /^(?:
1891773647a0SAndy Whitcroft				if|for|while|switch|return|case|
1892773647a0SAndy Whitcroft				volatile|__volatile__|
1893773647a0SAndy Whitcroft				__attribute__|format|__extension__|
1894773647a0SAndy Whitcroft				asm|__asm__)$/x)
1895773647a0SAndy Whitcroft			{
1896c2fdda0dSAndy Whitcroft
1897c2fdda0dSAndy Whitcroft			# cpp #define statements have non-optional spaces, ie
1898c2fdda0dSAndy Whitcroft			# if there is a space between the name and the open
1899c2fdda0dSAndy Whitcroft			# parenthesis it is simply not a parameter group.
1900c45dcabdSAndy Whitcroft			} elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
1901773647a0SAndy Whitcroft
1902773647a0SAndy Whitcroft			# cpp #elif statement condition may start with a (
1903c45dcabdSAndy Whitcroft			} elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
1904c2fdda0dSAndy Whitcroft
1905c2fdda0dSAndy Whitcroft			# If this whole things ends with a type its most
1906c2fdda0dSAndy Whitcroft			# likely a typedef for a function.
1907773647a0SAndy Whitcroft			} elsif ($ctx =~ /$Type$/) {
1908c2fdda0dSAndy Whitcroft
1909c2fdda0dSAndy Whitcroft			} else {
1910773647a0SAndy Whitcroft				WARN("space prohibited between function name and open parenthesis '('\n" . $herecurr);
1911f0a594c1SAndy Whitcroft			}
19126c72ffaaSAndy Whitcroft		}
1913653d4876SAndy Whitcroft# Check operator spacing.
19140a920b5bSAndy Whitcroft		if (!($line=~/\#\s*include/)) {
19159c0ca6f9SAndy Whitcroft			my $ops = qr{
19169c0ca6f9SAndy Whitcroft				<<=|>>=|<=|>=|==|!=|
19179c0ca6f9SAndy Whitcroft				\+=|-=|\*=|\/=|%=|\^=|\|=|&=|
19189c0ca6f9SAndy Whitcroft				=>|->|<<|>>|<|>|=|!|~|
19191f65f947SAndy Whitcroft				&&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
19201f65f947SAndy Whitcroft				\?|:
19219c0ca6f9SAndy Whitcroft			}x;
1922cf655043SAndy Whitcroft			my @elements = split(/($ops|;)/, $opline);
192300df344fSAndy Whitcroft			my $off = 0;
19246c72ffaaSAndy Whitcroft
19256c72ffaaSAndy Whitcroft			my $blank = copy_spacing($opline);
19266c72ffaaSAndy Whitcroft
19270a920b5bSAndy Whitcroft			for (my $n = 0; $n < $#elements; $n += 2) {
19284a0df2efSAndy Whitcroft				$off += length($elements[$n]);
19294a0df2efSAndy Whitcroft
1930773647a0SAndy Whitcroft				# Pick up the preceeding and succeeding characters.
1931773647a0SAndy Whitcroft				my $ca = substr($opline, 0, $off);
1932773647a0SAndy Whitcroft				my $cc = '';
1933773647a0SAndy Whitcroft				if (length($opline) >= ($off + length($elements[$n + 1]))) {
1934773647a0SAndy Whitcroft					$cc = substr($opline, $off + length($elements[$n + 1]));
1935773647a0SAndy Whitcroft				}
1936773647a0SAndy Whitcroft				my $cb = "$ca$;$cc";
1937773647a0SAndy Whitcroft
19384a0df2efSAndy Whitcroft				my $a = '';
19394a0df2efSAndy Whitcroft				$a = 'V' if ($elements[$n] ne '');
19404a0df2efSAndy Whitcroft				$a = 'W' if ($elements[$n] =~ /\s$/);
1941cf655043SAndy Whitcroft				$a = 'C' if ($elements[$n] =~ /$;$/);
19424a0df2efSAndy Whitcroft				$a = 'B' if ($elements[$n] =~ /(\[|\()$/);
19434a0df2efSAndy Whitcroft				$a = 'O' if ($elements[$n] eq '');
1944773647a0SAndy Whitcroft				$a = 'E' if ($ca =~ /^\s*$/);
19454a0df2efSAndy Whitcroft
19460a920b5bSAndy Whitcroft				my $op = $elements[$n + 1];
19474a0df2efSAndy Whitcroft
19484a0df2efSAndy Whitcroft				my $c = '';
19490a920b5bSAndy Whitcroft				if (defined $elements[$n + 2]) {
19504a0df2efSAndy Whitcroft					$c = 'V' if ($elements[$n + 2] ne '');
19514a0df2efSAndy Whitcroft					$c = 'W' if ($elements[$n + 2] =~ /^\s/);
1952cf655043SAndy Whitcroft					$c = 'C' if ($elements[$n + 2] =~ /^$;/);
19534a0df2efSAndy Whitcroft					$c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
19544a0df2efSAndy Whitcroft					$c = 'O' if ($elements[$n + 2] eq '');
19558b1b3378SAndy Whitcroft					$c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
19564a0df2efSAndy Whitcroft				} else {
19574a0df2efSAndy Whitcroft					$c = 'E';
19580a920b5bSAndy Whitcroft				}
19590a920b5bSAndy Whitcroft
19604a0df2efSAndy Whitcroft				my $ctx = "${a}x${c}";
19614a0df2efSAndy Whitcroft
19624a0df2efSAndy Whitcroft				my $at = "(ctx:$ctx)";
19634a0df2efSAndy Whitcroft
19646c72ffaaSAndy Whitcroft				my $ptr = substr($blank, 0, $off) . "^";
1965de7d4f0eSAndy Whitcroft				my $hereptr = "$hereline$ptr\n";
19660a920b5bSAndy Whitcroft
196774048ed8SAndy Whitcroft				# Pull out the value of this operator.
19686c72ffaaSAndy Whitcroft				my $op_type = substr($curr_values, $off + 1, 1);
19690a920b5bSAndy Whitcroft
19701f65f947SAndy Whitcroft				# Get the full operator variant.
19711f65f947SAndy Whitcroft				my $opv = $op . substr($curr_vars, $off, 1);
19721f65f947SAndy Whitcroft
197313214adfSAndy Whitcroft				# Ignore operators passed as parameters.
197413214adfSAndy Whitcroft				if ($op_type ne 'V' &&
197513214adfSAndy Whitcroft				    $ca =~ /\s$/ && $cc =~ /^\s*,/) {
197613214adfSAndy Whitcroft
1977cf655043SAndy Whitcroft#				# Ignore comments
1978cf655043SAndy Whitcroft#				} elsif ($op =~ /^$;+$/) {
197913214adfSAndy Whitcroft
1980d8aaf121SAndy Whitcroft				# ; should have either the end of line or a space or \ after it
198113214adfSAndy Whitcroft				} elsif ($op eq ';') {
1982cf655043SAndy Whitcroft					if ($ctx !~ /.x[WEBC]/ &&
1983cf655043SAndy Whitcroft					    $cc !~ /^\\/ && $cc !~ /^;/) {
1984773647a0SAndy Whitcroft						ERROR("space required after that '$op' $at\n" . $hereptr);
1985d8aaf121SAndy Whitcroft					}
1986d8aaf121SAndy Whitcroft
1987d8aaf121SAndy Whitcroft				# // is a comment
1988d8aaf121SAndy Whitcroft				} elsif ($op eq '//') {
19890a920b5bSAndy Whitcroft
19901f65f947SAndy Whitcroft				# No spaces for:
19911f65f947SAndy Whitcroft				#   ->
19921f65f947SAndy Whitcroft				#   :   when part of a bitfield
19931f65f947SAndy Whitcroft				} elsif ($op eq '->' || $opv eq ':B') {
19944a0df2efSAndy Whitcroft					if ($ctx =~ /Wx.|.xW/) {
1995773647a0SAndy Whitcroft						ERROR("spaces prohibited around that '$op' $at\n" . $hereptr);
19960a920b5bSAndy Whitcroft					}
19970a920b5bSAndy Whitcroft
19980a920b5bSAndy Whitcroft				# , must have a space on the right.
19990a920b5bSAndy Whitcroft				} elsif ($op eq ',') {
2000cf655043SAndy Whitcroft					if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
2001773647a0SAndy Whitcroft						ERROR("space required after that '$op' $at\n" . $hereptr);
20020a920b5bSAndy Whitcroft					}
20030a920b5bSAndy Whitcroft
20049c0ca6f9SAndy Whitcroft				# '*' as part of a type definition -- reported already.
200574048ed8SAndy Whitcroft				} elsif ($opv eq '*_') {
20069c0ca6f9SAndy Whitcroft					#warn "'*' is part of type\n";
20079c0ca6f9SAndy Whitcroft
20089c0ca6f9SAndy Whitcroft				# unary operators should have a space before and
20099c0ca6f9SAndy Whitcroft				# none after.  May be left adjacent to another
20109c0ca6f9SAndy Whitcroft				# unary operator, or a cast
20119c0ca6f9SAndy Whitcroft				} elsif ($op eq '!' || $op eq '~' ||
201274048ed8SAndy Whitcroft					 $opv eq '*U' || $opv eq '-U' ||
20130d413866SAndy Whitcroft					 $opv eq '&U' || $opv eq '&&U') {
2014cf655043SAndy Whitcroft					if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
2015773647a0SAndy Whitcroft						ERROR("space required before that '$op' $at\n" . $hereptr);
20160a920b5bSAndy Whitcroft					}
2017a3340b35SAndy Whitcroft					if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
2018171ae1a4SAndy Whitcroft						# A unary '*' may be const
2019171ae1a4SAndy Whitcroft
2020171ae1a4SAndy Whitcroft					} elsif ($ctx =~ /.xW/) {
2021fb9e9096SAndy Whitcroft						ERROR("space prohibited after that '$op' $at\n" . $hereptr);
20220a920b5bSAndy Whitcroft					}
20230a920b5bSAndy Whitcroft
20240a920b5bSAndy Whitcroft				# unary ++ and unary -- are allowed no space on one side.
20250a920b5bSAndy Whitcroft				} elsif ($op eq '++' or $op eq '--') {
2026773647a0SAndy Whitcroft					if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
2027773647a0SAndy Whitcroft						ERROR("space required one side of that '$op' $at\n" . $hereptr);
20280a920b5bSAndy Whitcroft					}
2029773647a0SAndy Whitcroft					if ($ctx =~ /Wx[BE]/ ||
2030773647a0SAndy Whitcroft					    ($ctx =~ /Wx./ && $cc =~ /^;/)) {
2031773647a0SAndy Whitcroft						ERROR("space prohibited before that '$op' $at\n" . $hereptr);
2032653d4876SAndy Whitcroft					}
2033773647a0SAndy Whitcroft					if ($ctx =~ /ExW/) {
2034773647a0SAndy Whitcroft						ERROR("space prohibited after that '$op' $at\n" . $hereptr);
2035773647a0SAndy Whitcroft					}
2036773647a0SAndy Whitcroft
20370a920b5bSAndy Whitcroft
20380a920b5bSAndy Whitcroft				# << and >> may either have or not have spaces both sides
20399c0ca6f9SAndy Whitcroft				} elsif ($op eq '<<' or $op eq '>>' or
20409c0ca6f9SAndy Whitcroft					 $op eq '&' or $op eq '^' or $op eq '|' or
20419c0ca6f9SAndy Whitcroft					 $op eq '+' or $op eq '-' or
2042c2fdda0dSAndy Whitcroft					 $op eq '*' or $op eq '/' or
2043c2fdda0dSAndy Whitcroft					 $op eq '%')
20440a920b5bSAndy Whitcroft				{
2045773647a0SAndy Whitcroft					if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
2046de7d4f0eSAndy Whitcroft						ERROR("need consistent spacing around '$op' $at\n" .
2047de7d4f0eSAndy Whitcroft							$hereptr);
20480a920b5bSAndy Whitcroft					}
20490a920b5bSAndy Whitcroft
20501f65f947SAndy Whitcroft				# A colon needs no spaces before when it is
20511f65f947SAndy Whitcroft				# terminating a case value or a label.
20521f65f947SAndy Whitcroft				} elsif ($opv eq ':C' || $opv eq ':L') {
20531f65f947SAndy Whitcroft					if ($ctx =~ /Wx./) {
20541f65f947SAndy Whitcroft						ERROR("space prohibited before that '$op' $at\n" . $hereptr);
20551f65f947SAndy Whitcroft					}
20561f65f947SAndy Whitcroft
20570a920b5bSAndy Whitcroft				# All the others need spaces both sides.
2058cf655043SAndy Whitcroft				} elsif ($ctx !~ /[EWC]x[CWE]/) {
20591f65f947SAndy Whitcroft					my $ok = 0;
20601f65f947SAndy Whitcroft
206122f2a2efSAndy Whitcroft					# Ignore email addresses <foo@bar>
20621f65f947SAndy Whitcroft					if (($op eq '<' &&
20631f65f947SAndy Whitcroft					     $cc =~ /^\S+\@\S+>/) ||
20641f65f947SAndy Whitcroft					    ($op eq '>' &&
20651f65f947SAndy Whitcroft					     $ca =~ /<\S+\@\S+$/))
20661f65f947SAndy Whitcroft					{
20671f65f947SAndy Whitcroft					    	$ok = 1;
20681f65f947SAndy Whitcroft					}
20691f65f947SAndy Whitcroft
20701f65f947SAndy Whitcroft					# Ignore ?:
20711f65f947SAndy Whitcroft					if (($opv eq ':O' && $ca =~ /\?$/) ||
20721f65f947SAndy Whitcroft					    ($op eq '?' && $cc =~ /^:/)) {
20731f65f947SAndy Whitcroft					    	$ok = 1;
20741f65f947SAndy Whitcroft					}
20751f65f947SAndy Whitcroft
20761f65f947SAndy Whitcroft					if ($ok == 0) {
2077773647a0SAndy Whitcroft						ERROR("spaces required around that '$op' $at\n" . $hereptr);
20780a920b5bSAndy Whitcroft					}
207922f2a2efSAndy Whitcroft				}
20804a0df2efSAndy Whitcroft				$off += length($elements[$n + 1]);
20810a920b5bSAndy Whitcroft			}
20820a920b5bSAndy Whitcroft		}
20830a920b5bSAndy Whitcroft
2084f0a594c1SAndy Whitcroft# check for multiple assignments
2085f0a594c1SAndy Whitcroft		if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
20866c72ffaaSAndy Whitcroft			CHK("multiple assignments should be avoided\n" . $herecurr);
2087f0a594c1SAndy Whitcroft		}
2088f0a594c1SAndy Whitcroft
208922f2a2efSAndy Whitcroft## # check for multiple declarations, allowing for a function declaration
209022f2a2efSAndy Whitcroft## # continuation.
209122f2a2efSAndy Whitcroft## 		if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
209222f2a2efSAndy Whitcroft## 		    $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
209322f2a2efSAndy Whitcroft##
209422f2a2efSAndy Whitcroft## 			# Remove any bracketed sections to ensure we do not
209522f2a2efSAndy Whitcroft## 			# falsly report the parameters of functions.
209622f2a2efSAndy Whitcroft## 			my $ln = $line;
209722f2a2efSAndy Whitcroft## 			while ($ln =~ s/\([^\(\)]*\)//g) {
209822f2a2efSAndy Whitcroft## 			}
209922f2a2efSAndy Whitcroft## 			if ($ln =~ /,/) {
210022f2a2efSAndy Whitcroft## 				WARN("declaring multiple variables together should be avoided\n" . $herecurr);
210122f2a2efSAndy Whitcroft## 			}
210222f2a2efSAndy Whitcroft## 		}
2103f0a594c1SAndy Whitcroft
21040a920b5bSAndy Whitcroft#need space before brace following if, while, etc
210522f2a2efSAndy Whitcroft		if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
210622f2a2efSAndy Whitcroft		    $line =~ /do{/) {
2107773647a0SAndy Whitcroft			ERROR("space required before the open brace '{'\n" . $herecurr);
2108de7d4f0eSAndy Whitcroft		}
2109de7d4f0eSAndy Whitcroft
2110de7d4f0eSAndy Whitcroft# closing brace should have a space following it when it has anything
2111de7d4f0eSAndy Whitcroft# on the line
2112de7d4f0eSAndy Whitcroft		if ($line =~ /}(?!(?:,|;|\)))\S/) {
2113773647a0SAndy Whitcroft			ERROR("space required after that close brace '}'\n" . $herecurr);
21140a920b5bSAndy Whitcroft		}
21150a920b5bSAndy Whitcroft
211622f2a2efSAndy Whitcroft# check spacing on square brackets
211722f2a2efSAndy Whitcroft		if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
2118773647a0SAndy Whitcroft			ERROR("space prohibited after that open square bracket '['\n" . $herecurr);
211922f2a2efSAndy Whitcroft		}
212022f2a2efSAndy Whitcroft		if ($line =~ /\s\]/) {
2121773647a0SAndy Whitcroft			ERROR("space prohibited before that close square bracket ']'\n" . $herecurr);
212222f2a2efSAndy Whitcroft		}
212322f2a2efSAndy Whitcroft
2124c45dcabdSAndy Whitcroft# check spacing on parentheses
21259c0ca6f9SAndy Whitcroft		if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
21269c0ca6f9SAndy Whitcroft		    $line !~ /for\s*\(\s+;/) {
2127773647a0SAndy Whitcroft			ERROR("space prohibited after that open parenthesis '('\n" . $herecurr);
212822f2a2efSAndy Whitcroft		}
212913214adfSAndy Whitcroft		if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
2130c45dcabdSAndy Whitcroft		    $line !~ /for\s*\(.*;\s+\)/ &&
2131c45dcabdSAndy Whitcroft		    $line !~ /:\s+\)/) {
2132773647a0SAndy Whitcroft			ERROR("space prohibited before that close parenthesis ')'\n" . $herecurr);
213322f2a2efSAndy Whitcroft		}
213422f2a2efSAndy Whitcroft
21350a920b5bSAndy Whitcroft#goto labels aren't indented, allow a single space however
21364a0df2efSAndy Whitcroft		if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
21370a920b5bSAndy Whitcroft		   !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
2138de7d4f0eSAndy Whitcroft			WARN("labels should not be indented\n" . $herecurr);
21390a920b5bSAndy Whitcroft		}
21400a920b5bSAndy Whitcroft
2141c45dcabdSAndy Whitcroft# Return is not a function.
2142c45dcabdSAndy Whitcroft		if (defined($stat) && $stat =~ /^.\s*return(\s*)(\(.*);/s) {
2143c45dcabdSAndy Whitcroft			my $spacing = $1;
2144c45dcabdSAndy Whitcroft			my $value = $2;
2145c45dcabdSAndy Whitcroft
214686f9d059SAndy Whitcroft			# Flatten any parentheses
2147fee61c47SAndy Whitcroft			$value =~ s/\)\(/\) \(/g;
214863f17f89SAndy Whitcroft			while ($value =~ s/\[[^\{\}]*\]/1/ ||
214963f17f89SAndy Whitcroft			       $value !~ /(?:$Ident|-?$Constant)\s*
215063f17f89SAndy Whitcroft					     $Compare\s*
215163f17f89SAndy Whitcroft					     (?:$Ident|-?$Constant)/x &&
215263f17f89SAndy Whitcroft			       $value =~ s/\([^\(\)]*\)/1/) {
2153c45dcabdSAndy Whitcroft			}
2154c45dcabdSAndy Whitcroft
2155c45dcabdSAndy Whitcroft			if ($value =~ /^(?:$Ident|-?$Constant)$/) {
2156c45dcabdSAndy Whitcroft				ERROR("return is not a function, parentheses are not required\n" . $herecurr);
2157c45dcabdSAndy Whitcroft
2158c45dcabdSAndy Whitcroft			} elsif ($spacing !~ /\s+/) {
2159c45dcabdSAndy Whitcroft				ERROR("space required before the open parenthesis '('\n" . $herecurr);
2160c45dcabdSAndy Whitcroft			}
2161c45dcabdSAndy Whitcroft		}
2162c45dcabdSAndy Whitcroft
21630a920b5bSAndy Whitcroft# Need a space before open parenthesis after if, while etc
21644a0df2efSAndy Whitcroft		if ($line=~/\b(if|while|for|switch)\(/) {
2165773647a0SAndy Whitcroft			ERROR("space required before the open parenthesis '('\n" . $herecurr);
21660a920b5bSAndy Whitcroft		}
21670a920b5bSAndy Whitcroft
2168f5fe35ddSAndy Whitcroft# Check for illegal assignment in if conditional -- and check for trailing
2169f5fe35ddSAndy Whitcroft# statements after the conditional.
2170170d3a22SAndy Whitcroft		if ($line =~ /do\s*(?!{)/) {
2171170d3a22SAndy Whitcroft			my ($stat_next) = ctx_statement_block($line_nr_next,
2172170d3a22SAndy Whitcroft						$remain_next, $off_next);
2173170d3a22SAndy Whitcroft			$stat_next =~ s/\n./\n /g;
2174170d3a22SAndy Whitcroft			##print "stat<$stat> stat_next<$stat_next>\n";
2175170d3a22SAndy Whitcroft
2176170d3a22SAndy Whitcroft			if ($stat_next =~ /^\s*while\b/) {
2177170d3a22SAndy Whitcroft				# If the statement carries leading newlines,
2178170d3a22SAndy Whitcroft				# then count those as offsets.
2179170d3a22SAndy Whitcroft				my ($whitespace) =
2180170d3a22SAndy Whitcroft					($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
2181170d3a22SAndy Whitcroft				my $offset =
2182170d3a22SAndy Whitcroft					statement_rawlines($whitespace) - 1;
2183170d3a22SAndy Whitcroft
2184170d3a22SAndy Whitcroft				$suppress_whiletrailers{$line_nr_next +
2185170d3a22SAndy Whitcroft								$offset} = 1;
2186170d3a22SAndy Whitcroft			}
2187170d3a22SAndy Whitcroft		}
2188170d3a22SAndy Whitcroft		if (!defined $suppress_whiletrailers{$linenr} &&
2189170d3a22SAndy Whitcroft		    $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
2190171ae1a4SAndy Whitcroft			my ($s, $c) = ($stat, $cond);
21918905a67cSAndy Whitcroft
2192b53c8e10SAndy Whitcroft			if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
2193c2fdda0dSAndy Whitcroft				ERROR("do not use assignment in if condition\n" . $herecurr);
21948905a67cSAndy Whitcroft			}
21958905a67cSAndy Whitcroft
21968905a67cSAndy Whitcroft			# Find out what is on the end of the line after the
21978905a67cSAndy Whitcroft			# conditional.
2198773647a0SAndy Whitcroft			substr($s, 0, length($c), '');
21998905a67cSAndy Whitcroft			$s =~ s/\n.*//g;
220013214adfSAndy Whitcroft			$s =~ s/$;//g; 	# Remove any comments
220153210168SAndy Whitcroft			if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
220253210168SAndy Whitcroft			    $c !~ /}\s*while\s*/)
2203773647a0SAndy Whitcroft			{
2204bb44ad39SAndy Whitcroft				# Find out how long the conditional actually is.
2205bb44ad39SAndy Whitcroft				my @newlines = ($c =~ /\n/gs);
2206bb44ad39SAndy Whitcroft				my $cond_lines = 1 + $#newlines;
220742bdf74cSHidetoshi Seto				my $stat_real = '';
2208bb44ad39SAndy Whitcroft
220942bdf74cSHidetoshi Seto				$stat_real = raw_line($linenr, $cond_lines)
221042bdf74cSHidetoshi Seto							. "\n" if ($cond_lines);
2211bb44ad39SAndy Whitcroft				if (defined($stat_real) && $cond_lines > 1) {
2212bb44ad39SAndy Whitcroft					$stat_real = "[...]\n$stat_real";
2213bb44ad39SAndy Whitcroft				}
2214bb44ad39SAndy Whitcroft
2215bb44ad39SAndy Whitcroft				ERROR("trailing statements should be on next line\n" . $herecurr . $stat_real);
22168905a67cSAndy Whitcroft			}
22178905a67cSAndy Whitcroft		}
22188905a67cSAndy Whitcroft
221913214adfSAndy Whitcroft# Check for bitwise tests written as boolean
222013214adfSAndy Whitcroft		if ($line =~ /
222113214adfSAndy Whitcroft			(?:
222213214adfSAndy Whitcroft				(?:\[|\(|\&\&|\|\|)
222313214adfSAndy Whitcroft				\s*0[xX][0-9]+\s*
222413214adfSAndy Whitcroft				(?:\&\&|\|\|)
222513214adfSAndy Whitcroft			|
222613214adfSAndy Whitcroft				(?:\&\&|\|\|)
222713214adfSAndy Whitcroft				\s*0[xX][0-9]+\s*
222813214adfSAndy Whitcroft				(?:\&\&|\|\||\)|\])
222913214adfSAndy Whitcroft			)/x)
223013214adfSAndy Whitcroft		{
223113214adfSAndy Whitcroft			WARN("boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
223213214adfSAndy Whitcroft		}
223313214adfSAndy Whitcroft
22348905a67cSAndy Whitcroft# if and else should not have general statements after it
223513214adfSAndy Whitcroft		if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
223613214adfSAndy Whitcroft			my $s = $1;
223713214adfSAndy Whitcroft			$s =~ s/$;//g; 	# Remove any comments
223813214adfSAndy Whitcroft			if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
22398905a67cSAndy Whitcroft				ERROR("trailing statements should be on next line\n" . $herecurr);
22400a920b5bSAndy Whitcroft			}
224113214adfSAndy Whitcroft		}
224239667782SAndy Whitcroft# if should not continue a brace
224339667782SAndy Whitcroft		if ($line =~ /}\s*if\b/) {
224439667782SAndy Whitcroft			ERROR("trailing statements should be on next line\n" .
224539667782SAndy Whitcroft				$herecurr);
224639667782SAndy Whitcroft		}
2247a1080bf8SAndy Whitcroft# case and default should not have general statements after them
2248a1080bf8SAndy Whitcroft		if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
2249a1080bf8SAndy Whitcroft		    $line !~ /\G(?:
22503fef12d6SAndy Whitcroft			(?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
2251a1080bf8SAndy Whitcroft			\s*return\s+
2252a1080bf8SAndy Whitcroft		    )/xg)
2253a1080bf8SAndy Whitcroft		{
2254a1080bf8SAndy Whitcroft			ERROR("trailing statements should be on next line\n" . $herecurr);
2255a1080bf8SAndy Whitcroft		}
22560a920b5bSAndy Whitcroft
22570a920b5bSAndy Whitcroft		# Check for }<nl>else {, these must be at the same
22580a920b5bSAndy Whitcroft		# indent level to be relevant to each other.
22590a920b5bSAndy Whitcroft		if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
22600a920b5bSAndy Whitcroft						$previndent == $indent) {
2261de7d4f0eSAndy Whitcroft			ERROR("else should follow close brace '}'\n" . $hereprev);
22620a920b5bSAndy Whitcroft		}
22630a920b5bSAndy Whitcroft
2264c2fdda0dSAndy Whitcroft		if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and
2265c2fdda0dSAndy Whitcroft						$previndent == $indent) {
2266c2fdda0dSAndy Whitcroft			my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
2267c2fdda0dSAndy Whitcroft
2268c2fdda0dSAndy Whitcroft			# Find out what is on the end of the line after the
2269c2fdda0dSAndy Whitcroft			# conditional.
2270773647a0SAndy Whitcroft			substr($s, 0, length($c), '');
2271c2fdda0dSAndy Whitcroft			$s =~ s/\n.*//g;
2272c2fdda0dSAndy Whitcroft
2273c2fdda0dSAndy Whitcroft			if ($s =~ /^\s*;/) {
2274c2fdda0dSAndy Whitcroft				ERROR("while should follow close brace '}'\n" . $hereprev);
2275c2fdda0dSAndy Whitcroft			}
2276c2fdda0dSAndy Whitcroft		}
2277c2fdda0dSAndy Whitcroft
22780a920b5bSAndy Whitcroft#studly caps, commented out until figure out how to distinguish between use of existing and adding new
22790a920b5bSAndy Whitcroft#		if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) {
22800a920b5bSAndy Whitcroft#		    print "No studly caps, use _\n";
22810a920b5bSAndy Whitcroft#		    print "$herecurr";
22820a920b5bSAndy Whitcroft#		    $clean = 0;
22830a920b5bSAndy Whitcroft#		}
22840a920b5bSAndy Whitcroft
22850a920b5bSAndy Whitcroft#no spaces allowed after \ in define
2286c45dcabdSAndy Whitcroft		if ($line=~/\#\s*define.*\\\s$/) {
2287de7d4f0eSAndy Whitcroft			WARN("Whitepspace after \\ makes next lines useless\n" . $herecurr);
22880a920b5bSAndy Whitcroft		}
22890a920b5bSAndy Whitcroft
2290653d4876SAndy Whitcroft#warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
2291c45dcabdSAndy Whitcroft		if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
2292e09dec48SAndy Whitcroft			my $file = "$1.h";
2293e09dec48SAndy Whitcroft			my $checkfile = "include/linux/$file";
2294e09dec48SAndy Whitcroft			if (-f "$root/$checkfile" &&
2295e09dec48SAndy Whitcroft			    $realfile ne $checkfile &&
2296c45dcabdSAndy Whitcroft			    $1 ne 'irq')
2297c45dcabdSAndy Whitcroft			{
2298e09dec48SAndy Whitcroft				if ($realfile =~ m{^arch/}) {
2299e09dec48SAndy Whitcroft					CHK("Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
2300e09dec48SAndy Whitcroft				} else {
2301e09dec48SAndy Whitcroft					WARN("Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
2302e09dec48SAndy Whitcroft				}
23030a920b5bSAndy Whitcroft			}
23040a920b5bSAndy Whitcroft		}
23050a920b5bSAndy Whitcroft
2306653d4876SAndy Whitcroft# multi-statement macros should be enclosed in a do while loop, grab the
2307653d4876SAndy Whitcroft# first statement and ensure its the whole macro if its not enclosed
2308cf655043SAndy Whitcroft# in a known good container
2309b8f96a31SAndy Whitcroft		if ($realfile !~ m@/vmlinux.lds.h$@ &&
2310b8f96a31SAndy Whitcroft		    $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
2311d8aaf121SAndy Whitcroft			my $ln = $linenr;
2312d8aaf121SAndy Whitcroft			my $cnt = $realcnt;
2313c45dcabdSAndy Whitcroft			my ($off, $dstat, $dcond, $rest);
2314c45dcabdSAndy Whitcroft			my $ctx = '';
2315653d4876SAndy Whitcroft
2316c45dcabdSAndy Whitcroft			my $args = defined($1);
2317de7d4f0eSAndy Whitcroft
2318c45dcabdSAndy Whitcroft			# Find the end of the macro and limit our statement
2319c45dcabdSAndy Whitcroft			# search to that.
2320c45dcabdSAndy Whitcroft			while ($cnt > 0 && defined $lines[$ln - 1] &&
2321c45dcabdSAndy Whitcroft				$lines[$ln - 1] =~ /^(?:-|..*\\$)/)
2322c45dcabdSAndy Whitcroft			{
2323c45dcabdSAndy Whitcroft				$ctx .= $rawlines[$ln - 1] . "\n";
2324a3bb97a7SAndy Whitcroft				$cnt-- if ($lines[$ln - 1] !~ /^-/);
2325c45dcabdSAndy Whitcroft				$ln++;
2326de7d4f0eSAndy Whitcroft			}
2327c45dcabdSAndy Whitcroft			$ctx .= $rawlines[$ln - 1];
2328d8aaf121SAndy Whitcroft
2329c45dcabdSAndy Whitcroft			($dstat, $dcond, $ln, $cnt, $off) =
2330c45dcabdSAndy Whitcroft				ctx_statement_block($linenr, $ln - $linenr + 1, 0);
2331c45dcabdSAndy Whitcroft			#print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
2332a3bb97a7SAndy Whitcroft			#print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
2333c45dcabdSAndy Whitcroft
2334c45dcabdSAndy Whitcroft			# Extract the remainder of the define (if any) and
2335c45dcabdSAndy Whitcroft			# rip off surrounding spaces, and trailing \'s.
2336c45dcabdSAndy Whitcroft			$rest = '';
2337636d140aSAndy Whitcroft			while ($off != 0 || ($cnt > 0 && $rest =~ /\\\s*$/)) {
2338636d140aSAndy Whitcroft				#print "ADDING cnt<$cnt> $off <" . substr($lines[$ln - 1], $off) . "> rest<$rest>\n";
2339a3bb97a7SAndy Whitcroft				if ($off != 0 || $lines[$ln - 1] !~ /^-/) {
2340c45dcabdSAndy Whitcroft					$rest .= substr($lines[$ln - 1], $off) . "\n";
2341c45dcabdSAndy Whitcroft					$cnt--;
2342a3bb97a7SAndy Whitcroft				}
2343a3bb97a7SAndy Whitcroft				$ln++;
2344c45dcabdSAndy Whitcroft				$off = 0;
2345c45dcabdSAndy Whitcroft			}
2346c45dcabdSAndy Whitcroft			$rest =~ s/\\\n.//g;
2347c45dcabdSAndy Whitcroft			$rest =~ s/^\s*//s;
2348c45dcabdSAndy Whitcroft			$rest =~ s/\s*$//s;
2349c45dcabdSAndy Whitcroft
2350c45dcabdSAndy Whitcroft			# Clean up the original statement.
2351c45dcabdSAndy Whitcroft			if ($args) {
2352c45dcabdSAndy Whitcroft				substr($dstat, 0, length($dcond), '');
2353d8aaf121SAndy Whitcroft			} else {
2354c45dcabdSAndy Whitcroft				$dstat =~ s/^.\s*\#\s*define\s+$Ident\s*//;
2355c45dcabdSAndy Whitcroft			}
2356292f1a9bSAndy Whitcroft			$dstat =~ s/$;//g;
2357c45dcabdSAndy Whitcroft			$dstat =~ s/\\\n.//g;
2358c45dcabdSAndy Whitcroft			$dstat =~ s/^\s*//s;
2359c45dcabdSAndy Whitcroft			$dstat =~ s/\s*$//s;
2360c45dcabdSAndy Whitcroft
2361c45dcabdSAndy Whitcroft			# Flatten any parentheses and braces
2362bf30d6edSAndy Whitcroft			while ($dstat =~ s/\([^\(\)]*\)/1/ ||
2363bf30d6edSAndy Whitcroft			       $dstat =~ s/\{[^\{\}]*\}/1/ ||
2364bf30d6edSAndy Whitcroft			       $dstat =~ s/\[[^\{\}]*\]/1/)
2365bf30d6edSAndy Whitcroft			{
2366c45dcabdSAndy Whitcroft			}
2367c45dcabdSAndy Whitcroft
2368c45dcabdSAndy Whitcroft			my $exceptions = qr{
2369c45dcabdSAndy Whitcroft				$Declare|
2370c45dcabdSAndy Whitcroft				module_param_named|
2371c45dcabdSAndy Whitcroft				MODULE_PARAM_DESC|
2372c45dcabdSAndy Whitcroft				DECLARE_PER_CPU|
2373c45dcabdSAndy Whitcroft				DEFINE_PER_CPU|
2374383099fdSAndy Whitcroft				__typeof__\(|
237522fd2d3eSStefani Seibold				union|
237622fd2d3eSStefani Seibold				struct|
2377ea71a0a0SAndy Whitcroft				\.$Ident\s*=\s*|
2378ea71a0a0SAndy Whitcroft				^\"|\"$
2379c45dcabdSAndy Whitcroft			}x;
2380383099fdSAndy Whitcroft			#print "REST<$rest> dstat<$dstat>\n";
2381c45dcabdSAndy Whitcroft			if ($rest ne '') {
2382c45dcabdSAndy Whitcroft				if ($rest !~ /while\s*\(/ &&
2383c45dcabdSAndy Whitcroft				    $dstat !~ /$exceptions/)
2384c45dcabdSAndy Whitcroft				{
2385c45dcabdSAndy Whitcroft					ERROR("Macros with multiple statements should be enclosed in a do - while loop\n" . "$here\n$ctx\n");
2386c45dcabdSAndy Whitcroft				}
2387c45dcabdSAndy Whitcroft
2388c45dcabdSAndy Whitcroft			} elsif ($ctx !~ /;/) {
2389c45dcabdSAndy Whitcroft				if ($dstat ne '' &&
2390c45dcabdSAndy Whitcroft				    $dstat !~ /^(?:$Ident|-?$Constant)$/ &&
2391c45dcabdSAndy Whitcroft				    $dstat !~ /$exceptions/ &&
2392b132e5d5SAndy Whitcroft				    $dstat !~ /^\.$Ident\s*=/ &&
2393c45dcabdSAndy Whitcroft				    $dstat =~ /$Operators/)
2394c45dcabdSAndy Whitcroft				{
2395de7d4f0eSAndy Whitcroft					ERROR("Macros with complex values should be enclosed in parenthesis\n" . "$here\n$ctx\n");
2396d8aaf121SAndy Whitcroft				}
23970a920b5bSAndy Whitcroft			}
2398653d4876SAndy Whitcroft		}
23990a920b5bSAndy Whitcroft
2400080ba929SMike Frysinger# make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
2401080ba929SMike Frysinger# all assignments may have only one of the following with an assignment:
2402080ba929SMike Frysinger#	.
2403080ba929SMike Frysinger#	ALIGN(...)
2404080ba929SMike Frysinger#	VMLINUX_SYMBOL(...)
2405080ba929SMike Frysinger		if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
2406080ba929SMike Frysinger			WARN("vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
2407080ba929SMike Frysinger		}
2408080ba929SMike Frysinger
2409f0a594c1SAndy Whitcroft# check for redundant bracing round if etc
241013214adfSAndy Whitcroft		if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
241113214adfSAndy Whitcroft			my ($level, $endln, @chunks) =
2412cf655043SAndy Whitcroft				ctx_statement_full($linenr, $realcnt, 1);
241313214adfSAndy Whitcroft			#print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
2414cf655043SAndy Whitcroft			#print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
2415cf655043SAndy Whitcroft			if ($#chunks > 0 && $level == 0) {
241613214adfSAndy Whitcroft				my $allowed = 0;
241713214adfSAndy Whitcroft				my $seen = 0;
2418773647a0SAndy Whitcroft				my $herectx = $here . "\n";
2419cf655043SAndy Whitcroft				my $ln = $linenr - 1;
242013214adfSAndy Whitcroft				for my $chunk (@chunks) {
242113214adfSAndy Whitcroft					my ($cond, $block) = @{$chunk};
242213214adfSAndy Whitcroft
2423773647a0SAndy Whitcroft					# If the condition carries leading newlines, then count those as offsets.
2424773647a0SAndy Whitcroft					my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
2425773647a0SAndy Whitcroft					my $offset = statement_rawlines($whitespace) - 1;
2426773647a0SAndy Whitcroft
2427773647a0SAndy Whitcroft					#print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
2428773647a0SAndy Whitcroft
2429773647a0SAndy Whitcroft					# We have looked at and allowed this specific line.
2430773647a0SAndy Whitcroft					$suppress_ifbraces{$ln + $offset} = 1;
2431773647a0SAndy Whitcroft
2432773647a0SAndy Whitcroft					$herectx .= "$rawlines[$ln + $offset]\n[...]\n";
2433cf655043SAndy Whitcroft					$ln += statement_rawlines($block) - 1;
2434cf655043SAndy Whitcroft
2435773647a0SAndy Whitcroft					substr($block, 0, length($cond), '');
243613214adfSAndy Whitcroft
243713214adfSAndy Whitcroft					$seen++ if ($block =~ /^\s*{/);
243813214adfSAndy Whitcroft
2439cf655043SAndy Whitcroft					#print "cond<$cond> block<$block> allowed<$allowed>\n";
2440cf655043SAndy Whitcroft					if (statement_lines($cond) > 1) {
2441cf655043SAndy Whitcroft						#print "APW: ALLOWED: cond<$cond>\n";
244213214adfSAndy Whitcroft						$allowed = 1;
244313214adfSAndy Whitcroft					}
244413214adfSAndy Whitcroft					if ($block =~/\b(?:if|for|while)\b/) {
2445cf655043SAndy Whitcroft						#print "APW: ALLOWED: block<$block>\n";
244613214adfSAndy Whitcroft						$allowed = 1;
244713214adfSAndy Whitcroft					}
2448cf655043SAndy Whitcroft					if (statement_block_size($block) > 1) {
2449cf655043SAndy Whitcroft						#print "APW: ALLOWED: lines block<$block>\n";
245013214adfSAndy Whitcroft						$allowed = 1;
245113214adfSAndy Whitcroft					}
245213214adfSAndy Whitcroft				}
245313214adfSAndy Whitcroft				if ($seen && !$allowed) {
2454cf655043SAndy Whitcroft					WARN("braces {} are not necessary for any arm of this statement\n" . $herectx);
245513214adfSAndy Whitcroft				}
245613214adfSAndy Whitcroft			}
245713214adfSAndy Whitcroft		}
2458773647a0SAndy Whitcroft		if (!defined $suppress_ifbraces{$linenr - 1} &&
245913214adfSAndy Whitcroft					$line =~ /\b(if|while|for|else)\b/) {
2460cf655043SAndy Whitcroft			my $allowed = 0;
2461f0a594c1SAndy Whitcroft
2462cf655043SAndy Whitcroft			# Check the pre-context.
2463cf655043SAndy Whitcroft			if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
2464cf655043SAndy Whitcroft				#print "APW: ALLOWED: pre<$1>\n";
2465cf655043SAndy Whitcroft				$allowed = 1;
2466f0a594c1SAndy Whitcroft			}
2467773647a0SAndy Whitcroft
2468773647a0SAndy Whitcroft			my ($level, $endln, @chunks) =
2469773647a0SAndy Whitcroft				ctx_statement_full($linenr, $realcnt, $-[0]);
2470773647a0SAndy Whitcroft
2471cf655043SAndy Whitcroft			# Check the condition.
2472cf655043SAndy Whitcroft			my ($cond, $block) = @{$chunks[0]};
2473773647a0SAndy Whitcroft			#print "CHECKING<$linenr> cond<$cond> block<$block>\n";
2474cf655043SAndy Whitcroft			if (defined $cond) {
2475773647a0SAndy Whitcroft				substr($block, 0, length($cond), '');
2476cf655043SAndy Whitcroft			}
2477cf655043SAndy Whitcroft			if (statement_lines($cond) > 1) {
2478cf655043SAndy Whitcroft				#print "APW: ALLOWED: cond<$cond>\n";
2479cf655043SAndy Whitcroft				$allowed = 1;
2480cf655043SAndy Whitcroft			}
2481cf655043SAndy Whitcroft			if ($block =~/\b(?:if|for|while)\b/) {
2482cf655043SAndy Whitcroft				#print "APW: ALLOWED: block<$block>\n";
2483cf655043SAndy Whitcroft				$allowed = 1;
2484cf655043SAndy Whitcroft			}
2485cf655043SAndy Whitcroft			if (statement_block_size($block) > 1) {
2486cf655043SAndy Whitcroft				#print "APW: ALLOWED: lines block<$block>\n";
2487cf655043SAndy Whitcroft				$allowed = 1;
2488cf655043SAndy Whitcroft			}
2489cf655043SAndy Whitcroft			# Check the post-context.
2490cf655043SAndy Whitcroft			if (defined $chunks[1]) {
2491cf655043SAndy Whitcroft				my ($cond, $block) = @{$chunks[1]};
2492cf655043SAndy Whitcroft				if (defined $cond) {
2493773647a0SAndy Whitcroft					substr($block, 0, length($cond), '');
2494cf655043SAndy Whitcroft				}
2495cf655043SAndy Whitcroft				if ($block =~ /^\s*\{/) {
2496cf655043SAndy Whitcroft					#print "APW: ALLOWED: chunk-1 block<$block>\n";
2497cf655043SAndy Whitcroft					$allowed = 1;
2498cf655043SAndy Whitcroft				}
2499cf655043SAndy Whitcroft			}
2500cf655043SAndy Whitcroft			if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
2501cf655043SAndy Whitcroft				my $herectx = $here . "\n";;
2502f055663cSAndy Whitcroft				my $cnt = statement_rawlines($block);
2503cf655043SAndy Whitcroft
2504f055663cSAndy Whitcroft				for (my $n = 0; $n < $cnt; $n++) {
2505f055663cSAndy Whitcroft					$herectx .= raw_line($linenr, $n) . "\n";;
2506cf655043SAndy Whitcroft				}
2507cf655043SAndy Whitcroft
2508cf655043SAndy Whitcroft				WARN("braces {} are not necessary for single statement blocks\n" . $herectx);
2509f0a594c1SAndy Whitcroft			}
2510f0a594c1SAndy Whitcroft		}
2511f0a594c1SAndy Whitcroft
2512653d4876SAndy Whitcroft# don't include deprecated include files (uses RAW line)
25134a0df2efSAndy Whitcroft		for my $inc (@dep_includes) {
2514c45dcabdSAndy Whitcroft			if ($rawline =~ m@^.\s*\#\s*include\s*\<$inc>@) {
2515de7d4f0eSAndy Whitcroft				ERROR("Don't use <$inc>: see Documentation/feature-removal-schedule.txt\n" . $herecurr);
25160a920b5bSAndy Whitcroft			}
25170a920b5bSAndy Whitcroft		}
25180a920b5bSAndy Whitcroft
25194a0df2efSAndy Whitcroft# don't use deprecated functions
25204a0df2efSAndy Whitcroft		for my $func (@dep_functions) {
252100df344fSAndy Whitcroft			if ($line =~ /\b$func\b/) {
2522de7d4f0eSAndy Whitcroft				ERROR("Don't use $func(): see Documentation/feature-removal-schedule.txt\n" . $herecurr);
25234a0df2efSAndy Whitcroft			}
25244a0df2efSAndy Whitcroft		}
25254a0df2efSAndy Whitcroft
25264a0df2efSAndy Whitcroft# no volatiles please
25276c72ffaaSAndy Whitcroft		my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
25286c72ffaaSAndy Whitcroft		if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
2529de7d4f0eSAndy Whitcroft			WARN("Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
25304a0df2efSAndy Whitcroft		}
25314a0df2efSAndy Whitcroft
25329c0ca6f9SAndy Whitcroft# SPIN_LOCK_UNLOCKED & RW_LOCK_UNLOCKED are deprecated
25339c0ca6f9SAndy Whitcroft		if ($line =~ /\b(SPIN_LOCK_UNLOCKED|RW_LOCK_UNLOCKED)/) {
25349c0ca6f9SAndy Whitcroft			ERROR("Use of $1 is deprecated: see Documentation/spinlocks.txt\n" . $herecurr);
25359c0ca6f9SAndy Whitcroft		}
25369c0ca6f9SAndy Whitcroft
253700df344fSAndy Whitcroft# warn about #if 0
2538c45dcabdSAndy Whitcroft		if ($line =~ /^.\s*\#\s*if\s+0\b/) {
2539de7d4f0eSAndy Whitcroft			CHK("if this code is redundant consider removing it\n" .
2540de7d4f0eSAndy Whitcroft				$herecurr);
25414a0df2efSAndy Whitcroft		}
25424a0df2efSAndy Whitcroft
2543f0a594c1SAndy Whitcroft# check for needless kfree() checks
2544f0a594c1SAndy Whitcroft		if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
2545f0a594c1SAndy Whitcroft			my $expr = $1;
2546f0a594c1SAndy Whitcroft			if ($line =~ /\bkfree\(\Q$expr\E\);/) {
25473c232147SWolfram Sang				WARN("kfree(NULL) is safe this check is probably not required\n" . $hereprev);
2548f0a594c1SAndy Whitcroft			}
2549f0a594c1SAndy Whitcroft		}
25504c432a8fSGreg Kroah-Hartman# check for needless usb_free_urb() checks
25514c432a8fSGreg Kroah-Hartman		if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
25524c432a8fSGreg Kroah-Hartman			my $expr = $1;
25534c432a8fSGreg Kroah-Hartman			if ($line =~ /\busb_free_urb\(\Q$expr\E\);/) {
25544c432a8fSGreg Kroah-Hartman				WARN("usb_free_urb(NULL) is safe this check is probably not required\n" . $hereprev);
25554c432a8fSGreg Kroah-Hartman			}
25564c432a8fSGreg Kroah-Hartman		}
2557f0a594c1SAndy Whitcroft
255800df344fSAndy Whitcroft# warn about #ifdefs in C files
2559c45dcabdSAndy Whitcroft#		if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
256000df344fSAndy Whitcroft#			print "#ifdef in C files should be avoided\n";
256100df344fSAndy Whitcroft#			print "$herecurr";
256200df344fSAndy Whitcroft#			$clean = 0;
256300df344fSAndy Whitcroft#		}
256400df344fSAndy Whitcroft
256522f2a2efSAndy Whitcroft# warn about spacing in #ifdefs
2566c45dcabdSAndy Whitcroft		if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
256722f2a2efSAndy Whitcroft			ERROR("exactly one space required after that #$1\n" . $herecurr);
256822f2a2efSAndy Whitcroft		}
256922f2a2efSAndy Whitcroft
25704a0df2efSAndy Whitcroft# check for spinlock_t definitions without a comment.
2571171ae1a4SAndy Whitcroft		if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
2572171ae1a4SAndy Whitcroft		    $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
25734a0df2efSAndy Whitcroft			my $which = $1;
25744a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
2575de7d4f0eSAndy Whitcroft				CHK("$1 definition without comment\n" . $herecurr);
25764a0df2efSAndy Whitcroft			}
25774a0df2efSAndy Whitcroft		}
25784a0df2efSAndy Whitcroft# check for memory barriers without a comment.
25794a0df2efSAndy Whitcroft		if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
25804a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
2581de7d4f0eSAndy Whitcroft				CHK("memory barrier without comment\n" . $herecurr);
25824a0df2efSAndy Whitcroft			}
25834a0df2efSAndy Whitcroft		}
25844a0df2efSAndy Whitcroft# check of hardware specific defines
2585c45dcabdSAndy Whitcroft		if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
2586de7d4f0eSAndy Whitcroft			CHK("architecture specific defines should be avoided\n" .  $herecurr);
25870a920b5bSAndy Whitcroft		}
2588653d4876SAndy Whitcroft
2589de7d4f0eSAndy Whitcroft# check the location of the inline attribute, that it is between
2590de7d4f0eSAndy Whitcroft# storage class and type.
25919c0ca6f9SAndy Whitcroft		if ($line =~ /\b$Type\s+$Inline\b/ ||
25929c0ca6f9SAndy Whitcroft		    $line =~ /\b$Inline\s+$Storage\b/) {
2593de7d4f0eSAndy Whitcroft			ERROR("inline keyword should sit between storage class and type\n" . $herecurr);
2594de7d4f0eSAndy Whitcroft		}
2595de7d4f0eSAndy Whitcroft
25968905a67cSAndy Whitcroft# Check for __inline__ and __inline, prefer inline
25978905a67cSAndy Whitcroft		if ($line =~ /\b(__inline__|__inline)\b/) {
25988905a67cSAndy Whitcroft			WARN("plain inline is preferred over $1\n" . $herecurr);
25998905a67cSAndy Whitcroft		}
26008905a67cSAndy Whitcroft
26018f53a9b8SJoe Perches# check for sizeof(&)
26028f53a9b8SJoe Perches		if ($line =~ /\bsizeof\s*\(\s*\&/) {
26038f53a9b8SJoe Perches			WARN("sizeof(& should be avoided\n" . $herecurr);
26048f53a9b8SJoe Perches		}
26058f53a9b8SJoe Perches
2606de7d4f0eSAndy Whitcroft# check for new externs in .c files.
2607171ae1a4SAndy Whitcroft		if ($realfile =~ /\.c$/ && defined $stat &&
2608c45dcabdSAndy Whitcroft		    $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
2609171ae1a4SAndy Whitcroft		{
2610c45dcabdSAndy Whitcroft			my $function_name = $1;
2611c45dcabdSAndy Whitcroft			my $paren_space = $2;
2612171ae1a4SAndy Whitcroft
2613171ae1a4SAndy Whitcroft			my $s = $stat;
2614171ae1a4SAndy Whitcroft			if (defined $cond) {
2615171ae1a4SAndy Whitcroft				substr($s, 0, length($cond), '');
2616171ae1a4SAndy Whitcroft			}
2617c45dcabdSAndy Whitcroft			if ($s =~ /^\s*;/ &&
2618c45dcabdSAndy Whitcroft			    $function_name ne 'uninitialized_var')
2619c45dcabdSAndy Whitcroft			{
2620de7d4f0eSAndy Whitcroft				WARN("externs should be avoided in .c files\n" .  $herecurr);
2621de7d4f0eSAndy Whitcroft			}
2622de7d4f0eSAndy Whitcroft
2623171ae1a4SAndy Whitcroft			if ($paren_space =~ /\n/) {
2624171ae1a4SAndy Whitcroft				WARN("arguments for function declarations should follow identifier\n" . $herecurr);
2625171ae1a4SAndy Whitcroft			}
26269c9ba34eSAndy Whitcroft
26279c9ba34eSAndy Whitcroft		} elsif ($realfile =~ /\.c$/ && defined $stat &&
26289c9ba34eSAndy Whitcroft		    $stat =~ /^.\s*extern\s+/)
26299c9ba34eSAndy Whitcroft		{
26309c9ba34eSAndy Whitcroft			WARN("externs should be avoided in .c files\n" .  $herecurr);
2631171ae1a4SAndy Whitcroft		}
2632171ae1a4SAndy Whitcroft
2633de7d4f0eSAndy Whitcroft# checks for new __setup's
2634de7d4f0eSAndy Whitcroft		if ($rawline =~ /\b__setup\("([^"]*)"/) {
2635de7d4f0eSAndy Whitcroft			my $name = $1;
2636de7d4f0eSAndy Whitcroft
2637de7d4f0eSAndy Whitcroft			if (!grep(/$name/, @setup_docs)) {
2638de7d4f0eSAndy Whitcroft				CHK("__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
2639de7d4f0eSAndy Whitcroft			}
2640653d4876SAndy Whitcroft		}
26419c0ca6f9SAndy Whitcroft
26429c0ca6f9SAndy Whitcroft# check for pointless casting of kmalloc return
26439c0ca6f9SAndy Whitcroft		if ($line =~ /\*\s*\)\s*k[czm]alloc\b/) {
26449c0ca6f9SAndy Whitcroft			WARN("unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
26459c0ca6f9SAndy Whitcroft		}
264613214adfSAndy Whitcroft
264713214adfSAndy Whitcroft# check for gcc specific __FUNCTION__
264813214adfSAndy Whitcroft		if ($line =~ /__FUNCTION__/) {
264913214adfSAndy Whitcroft			WARN("__func__ should be used instead of gcc specific __FUNCTION__\n"  . $herecurr);
265013214adfSAndy Whitcroft		}
2651773647a0SAndy Whitcroft
2652773647a0SAndy Whitcroft# check for semaphores used as mutexes
2653171ae1a4SAndy Whitcroft		if ($line =~ /^.\s*(DECLARE_MUTEX|init_MUTEX)\s*\(/) {
2654773647a0SAndy Whitcroft			WARN("mutexes are preferred for single holder semaphores\n" . $herecurr);
2655773647a0SAndy Whitcroft		}
2656773647a0SAndy Whitcroft# check for semaphores used as mutexes
2657171ae1a4SAndy Whitcroft		if ($line =~ /^.\s*init_MUTEX_LOCKED\s*\(/) {
2658773647a0SAndy Whitcroft			WARN("consider using a completion\n" . $herecurr);
2659773647a0SAndy Whitcroft		}
2660773647a0SAndy Whitcroft# recommend strict_strto* over simple_strto*
2661773647a0SAndy Whitcroft		if ($line =~ /\bsimple_(strto.*?)\s*\(/) {
2662773647a0SAndy Whitcroft			WARN("consider using strict_$1 in preference to simple_$1\n" . $herecurr);
2663773647a0SAndy Whitcroft		}
2664f3db6639SMichael Ellerman# check for __initcall(), use device_initcall() explicitly please
2665f3db6639SMichael Ellerman		if ($line =~ /^.\s*__initcall\s*\(/) {
2666f3db6639SMichael Ellerman			WARN("please use device_initcall() instead of __initcall()\n" . $herecurr);
2667f3db6639SMichael Ellerman		}
266879404849SEmese Revfy# check for various ops structs, ensure they are const.
266979404849SEmese Revfy		my $struct_ops = qr{acpi_dock_ops|
267079404849SEmese Revfy				address_space_operations|
267179404849SEmese Revfy				backlight_ops|
267279404849SEmese Revfy				block_device_operations|
267379404849SEmese Revfy				dentry_operations|
267479404849SEmese Revfy				dev_pm_ops|
267579404849SEmese Revfy				dma_map_ops|
267679404849SEmese Revfy				extent_io_ops|
267779404849SEmese Revfy				file_lock_operations|
267879404849SEmese Revfy				file_operations|
267979404849SEmese Revfy				hv_ops|
268079404849SEmese Revfy				ide_dma_ops|
268179404849SEmese Revfy				intel_dvo_dev_ops|
268279404849SEmese Revfy				item_operations|
268379404849SEmese Revfy				iwl_ops|
268479404849SEmese Revfy				kgdb_arch|
268579404849SEmese Revfy				kgdb_io|
268679404849SEmese Revfy				kset_uevent_ops|
268779404849SEmese Revfy				lock_manager_operations|
268879404849SEmese Revfy				microcode_ops|
268979404849SEmese Revfy				mtrr_ops|
269079404849SEmese Revfy				neigh_ops|
269179404849SEmese Revfy				nlmsvc_binding|
269279404849SEmese Revfy				pci_raw_ops|
269379404849SEmese Revfy				pipe_buf_operations|
269479404849SEmese Revfy				platform_hibernation_ops|
269579404849SEmese Revfy				platform_suspend_ops|
269679404849SEmese Revfy				proto_ops|
269779404849SEmese Revfy				rpc_pipe_ops|
269879404849SEmese Revfy				seq_operations|
269979404849SEmese Revfy				snd_ac97_build_ops|
270079404849SEmese Revfy				soc_pcmcia_socket_ops|
270179404849SEmese Revfy				stacktrace_ops|
270279404849SEmese Revfy				sysfs_ops|
270379404849SEmese Revfy				tty_operations|
270479404849SEmese Revfy				usb_mon_operations|
270579404849SEmese Revfy				wd_ops}x;
27066903ffb2SAndy Whitcroft		if ($line !~ /\bconst\b/ &&
270779404849SEmese Revfy		    $line =~ /\bstruct\s+($struct_ops)\b/) {
27086903ffb2SAndy Whitcroft			WARN("struct $1 should normally be const\n" .
27096903ffb2SAndy Whitcroft				$herecurr);
27102b6db5cbSAndy Whitcroft		}
2711773647a0SAndy Whitcroft
2712773647a0SAndy Whitcroft# use of NR_CPUS is usually wrong
2713773647a0SAndy Whitcroft# ignore definitions of NR_CPUS and usage to define arrays as likely right
2714773647a0SAndy Whitcroft		if ($line =~ /\bNR_CPUS\b/ &&
2715c45dcabdSAndy Whitcroft		    $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
2716c45dcabdSAndy Whitcroft		    $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
2717171ae1a4SAndy Whitcroft		    $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
2718171ae1a4SAndy Whitcroft		    $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
2719171ae1a4SAndy Whitcroft		    $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
2720773647a0SAndy Whitcroft		{
2721773647a0SAndy Whitcroft			WARN("usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
2722773647a0SAndy Whitcroft		}
27239c9ba34eSAndy Whitcroft
27249c9ba34eSAndy Whitcroft# check for %L{u,d,i} in strings
27259c9ba34eSAndy Whitcroft		my $string;
27269c9ba34eSAndy Whitcroft		while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
27279c9ba34eSAndy Whitcroft			$string = substr($rawline, $-[1], $+[1] - $-[1]);
27282a1bc5d5SAndy Whitcroft			$string =~ s/%%/__/g;
27299c9ba34eSAndy Whitcroft			if ($string =~ /(?<!%)%L[udi]/) {
27309c9ba34eSAndy Whitcroft				WARN("\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
27319c9ba34eSAndy Whitcroft				last;
27329c9ba34eSAndy Whitcroft			}
27339c9ba34eSAndy Whitcroft		}
2734691d77b6SAndy Whitcroft
2735691d77b6SAndy Whitcroft# whine mightly about in_atomic
2736691d77b6SAndy Whitcroft		if ($line =~ /\bin_atomic\s*\(/) {
2737691d77b6SAndy Whitcroft			if ($realfile =~ m@^drivers/@) {
2738691d77b6SAndy Whitcroft				ERROR("do not use in_atomic in drivers\n" . $herecurr);
2739f4a87736SAndy Whitcroft			} elsif ($realfile !~ m@^kernel/@) {
2740691d77b6SAndy Whitcroft				WARN("use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
2741691d77b6SAndy Whitcroft			}
2742691d77b6SAndy Whitcroft		}
274313214adfSAndy Whitcroft	}
274413214adfSAndy Whitcroft
274513214adfSAndy Whitcroft	# If we have no input at all, then there is nothing to report on
274613214adfSAndy Whitcroft	# so just keep quiet.
274713214adfSAndy Whitcroft	if ($#rawlines == -1) {
274813214adfSAndy Whitcroft		exit(0);
27490a920b5bSAndy Whitcroft	}
27500a920b5bSAndy Whitcroft
27518905a67cSAndy Whitcroft	# In mailback mode only produce a report in the negative, for
27528905a67cSAndy Whitcroft	# things that appear to be patches.
27538905a67cSAndy Whitcroft	if ($mailback && ($clean == 1 || !$is_patch)) {
27548905a67cSAndy Whitcroft		exit(0);
27558905a67cSAndy Whitcroft	}
27568905a67cSAndy Whitcroft
27578905a67cSAndy Whitcroft	# This is not a patch, and we are are in 'no-patch' mode so
27588905a67cSAndy Whitcroft	# just keep quiet.
27598905a67cSAndy Whitcroft	if (!$chk_patch && !$is_patch) {
27608905a67cSAndy Whitcroft		exit(0);
27618905a67cSAndy Whitcroft	}
27628905a67cSAndy Whitcroft
27638905a67cSAndy Whitcroft	if (!$is_patch) {
2764de7d4f0eSAndy Whitcroft		ERROR("Does not appear to be a unified-diff format patch\n");
27650a920b5bSAndy Whitcroft	}
27660a920b5bSAndy Whitcroft	if ($is_patch && $chk_signoff && $signoff == 0) {
2767de7d4f0eSAndy Whitcroft		ERROR("Missing Signed-off-by: line(s)\n");
27680a920b5bSAndy Whitcroft	}
27690a920b5bSAndy Whitcroft
2770f0a594c1SAndy Whitcroft	print report_dump();
277113214adfSAndy Whitcroft	if ($summary && !($clean == 1 && $quiet == 1)) {
277213214adfSAndy Whitcroft		print "$filename " if ($summary_file);
27736c72ffaaSAndy Whitcroft		print "total: $cnt_error errors, $cnt_warn warnings, " .
27746c72ffaaSAndy Whitcroft			(($check)? "$cnt_chk checks, " : "") .
27756c72ffaaSAndy Whitcroft			"$cnt_lines lines checked\n";
27768905a67cSAndy Whitcroft		print "\n" if ($quiet == 0);
27776c72ffaaSAndy Whitcroft	}
27788905a67cSAndy Whitcroft
27790a920b5bSAndy Whitcroft	if ($clean == 1 && $quiet == 0) {
2780c2fdda0dSAndy Whitcroft		print "$vname has no obvious style problems and is ready for submission.\n"
27810a920b5bSAndy Whitcroft	}
27820a920b5bSAndy Whitcroft	if ($clean == 0 && $quiet == 0) {
2783c2fdda0dSAndy Whitcroft		print "$vname has style problems, please review.  If any of these errors\n";
27840a920b5bSAndy Whitcroft		print "are false positives report them to the maintainer, see\n";
27850a920b5bSAndy Whitcroft		print "CHECKPATCH in MAINTAINERS.\n";
27860a920b5bSAndy Whitcroft	}
278713214adfSAndy Whitcroft
27880a920b5bSAndy Whitcroft	return $clean;
27890a920b5bSAndy Whitcroft}
2790