xref: /linux-6.15/scripts/checkpatch.pl (revision 554e165c)
10a920b5bSAndy Whitcroft#!/usr/bin/perl -w
2dbf004d7SDave Jones# (c) 2001, Dave Jones. (the file handling bit)
300df344fSAndy Whitcroft# (c) 2005, Joel Schopp <[email protected]> (the ugly bit)
42a5a2c25SAndy Whitcroft# (c) 2007,2008, Andy Whitcroft <[email protected]> (new conditions, test suite)
5015830beSAndy Whitcroft# (c) 2008-2010 Andy Whitcroft <[email protected]>
60a920b5bSAndy Whitcroft# Licensed under the terms of the GNU GPL License version 2
70a920b5bSAndy Whitcroft
80a920b5bSAndy Whitcroftuse strict;
90a920b5bSAndy Whitcroft
100a920b5bSAndy Whitcroftmy $P = $0;
1100df344fSAndy Whitcroft$P =~ s@.*/@@g;
120a920b5bSAndy Whitcroft
13000d1cc1SJoe Perchesmy $V = '0.32';
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;
29000d1cc1SJoe Perchesmy $show_types = 0;
306c72ffaaSAndy Whitcroftmy $root;
31c2fdda0dSAndy Whitcroftmy %debug;
32000d1cc1SJoe Perchesmy %ignore_type = ();
33000d1cc1SJoe Perchesmy @ignore = ();
3477f5b10aSHannes Edermy $help = 0;
35000d1cc1SJoe Perchesmy $configuration_file = ".checkpatch.conf";
3677f5b10aSHannes Eder
3777f5b10aSHannes Edersub help {
3877f5b10aSHannes Eder	my ($exitcode) = @_;
3977f5b10aSHannes Eder
4077f5b10aSHannes Eder	print << "EOM";
4177f5b10aSHannes EderUsage: $P [OPTION]... [FILE]...
4277f5b10aSHannes EderVersion: $V
4377f5b10aSHannes Eder
4477f5b10aSHannes EderOptions:
4577f5b10aSHannes Eder  -q, --quiet                quiet
4677f5b10aSHannes Eder  --no-tree                  run without a kernel tree
4777f5b10aSHannes Eder  --no-signoff               do not check for 'Signed-off-by' line
4877f5b10aSHannes Eder  --patch                    treat FILE as patchfile (default)
4977f5b10aSHannes Eder  --emacs                    emacs compile window format
5077f5b10aSHannes Eder  --terse                    one line per report
5177f5b10aSHannes Eder  -f, --file                 treat FILE as regular source file
5277f5b10aSHannes Eder  --subjective, --strict     enable more subjective tests
53000d1cc1SJoe Perches  --ignore TYPE(,TYPE2...)   ignore various comma separated message types
54000d1cc1SJoe Perches  --show-types               show the message "types" in the output
5577f5b10aSHannes Eder  --root=PATH                PATH to the kernel tree root
5677f5b10aSHannes Eder  --no-summary               suppress the per-file summary
5777f5b10aSHannes Eder  --mailback                 only produce a report in case of warnings/errors
5877f5b10aSHannes Eder  --summary-file             include the filename in summary
5977f5b10aSHannes Eder  --debug KEY=[0|1]          turn on/off debugging of KEY, where KEY is one of
6077f5b10aSHannes Eder                             'values', 'possible', 'type', and 'attr' (default
6177f5b10aSHannes Eder                             is all off)
6277f5b10aSHannes Eder  --test-only=WORD           report only warnings/errors containing WORD
6377f5b10aSHannes Eder                             literally
6477f5b10aSHannes Eder  -h, --help, --version      display this help and exit
6577f5b10aSHannes Eder
6677f5b10aSHannes EderWhen FILE is - read standard input.
6777f5b10aSHannes EderEOM
6877f5b10aSHannes Eder
6977f5b10aSHannes Eder	exit($exitcode);
7077f5b10aSHannes Eder}
7177f5b10aSHannes Eder
72000d1cc1SJoe Perchesmy $conf = which_conf($configuration_file);
73000d1cc1SJoe Perchesif (-f $conf) {
74000d1cc1SJoe Perches	my @conf_args;
75000d1cc1SJoe Perches	open(my $conffile, '<', "$conf")
76000d1cc1SJoe Perches	    or warn "$P: Can't find a readable $configuration_file file $!\n";
77000d1cc1SJoe Perches
78000d1cc1SJoe Perches	while (<$conffile>) {
79000d1cc1SJoe Perches		my $line = $_;
80000d1cc1SJoe Perches
81000d1cc1SJoe Perches		$line =~ s/\s*\n?$//g;
82000d1cc1SJoe Perches		$line =~ s/^\s*//g;
83000d1cc1SJoe Perches		$line =~ s/\s+/ /g;
84000d1cc1SJoe Perches
85000d1cc1SJoe Perches		next if ($line =~ m/^\s*#/);
86000d1cc1SJoe Perches		next if ($line =~ m/^\s*$/);
87000d1cc1SJoe Perches
88000d1cc1SJoe Perches		my @words = split(" ", $line);
89000d1cc1SJoe Perches		foreach my $word (@words) {
90000d1cc1SJoe Perches			last if ($word =~ m/^#/);
91000d1cc1SJoe Perches			push (@conf_args, $word);
92000d1cc1SJoe Perches		}
93000d1cc1SJoe Perches	}
94000d1cc1SJoe Perches	close($conffile);
95000d1cc1SJoe Perches	unshift(@ARGV, @conf_args) if @conf_args;
96000d1cc1SJoe Perches}
97000d1cc1SJoe Perches
980a920b5bSAndy WhitcroftGetOptions(
996c72ffaaSAndy Whitcroft	'q|quiet+'	=> \$quiet,
1000a920b5bSAndy Whitcroft	'tree!'		=> \$tree,
1010a920b5bSAndy Whitcroft	'signoff!'	=> \$chk_signoff,
1020a920b5bSAndy Whitcroft	'patch!'	=> \$chk_patch,
1036c72ffaaSAndy Whitcroft	'emacs!'	=> \$emacs,
1048905a67cSAndy Whitcroft	'terse!'	=> \$terse,
10577f5b10aSHannes Eder	'f|file!'	=> \$file,
1066c72ffaaSAndy Whitcroft	'subjective!'	=> \$check,
1076c72ffaaSAndy Whitcroft	'strict!'	=> \$check,
108000d1cc1SJoe Perches	'ignore=s'	=> \@ignore,
109000d1cc1SJoe Perches	'show-types!'	=> \$show_types,
1106c72ffaaSAndy Whitcroft	'root=s'	=> \$root,
1118905a67cSAndy Whitcroft	'summary!'	=> \$summary,
1128905a67cSAndy Whitcroft	'mailback!'	=> \$mailback,
11313214adfSAndy Whitcroft	'summary-file!'	=> \$summary_file,
11413214adfSAndy Whitcroft
115c2fdda0dSAndy Whitcroft	'debug=s'	=> \%debug,
116773647a0SAndy Whitcroft	'test-only=s'	=> \$tst_only,
11777f5b10aSHannes Eder	'h|help'	=> \$help,
11877f5b10aSHannes Eder	'version'	=> \$help
11977f5b10aSHannes Eder) or help(1);
12077f5b10aSHannes Eder
12177f5b10aSHannes Ederhelp(0) if ($help);
1220a920b5bSAndy Whitcroft
1230a920b5bSAndy Whitcroftmy $exit = 0;
1240a920b5bSAndy Whitcroft
1250a920b5bSAndy Whitcroftif ($#ARGV < 0) {
12677f5b10aSHannes Eder	print "$P: no input files\n";
1270a920b5bSAndy Whitcroft	exit(1);
1280a920b5bSAndy Whitcroft}
1290a920b5bSAndy Whitcroft
130000d1cc1SJoe Perches@ignore = split(/,/, join(',',@ignore));
131000d1cc1SJoe Perchesforeach my $word (@ignore) {
132000d1cc1SJoe Perches	$word =~ s/\s*\n?$//g;
133000d1cc1SJoe Perches	$word =~ s/^\s*//g;
134000d1cc1SJoe Perches	$word =~ s/\s+/ /g;
135000d1cc1SJoe Perches	$word =~ tr/[a-z]/[A-Z]/;
136000d1cc1SJoe Perches
137000d1cc1SJoe Perches	next if ($word =~ m/^\s*#/);
138000d1cc1SJoe Perches	next if ($word =~ m/^\s*$/);
139000d1cc1SJoe Perches
140000d1cc1SJoe Perches	$ignore_type{$word}++;
141000d1cc1SJoe Perches}
142000d1cc1SJoe Perches
143c2fdda0dSAndy Whitcroftmy $dbg_values = 0;
144c2fdda0dSAndy Whitcroftmy $dbg_possible = 0;
1457429c690SAndy Whitcroftmy $dbg_type = 0;
146a1ef277eSAndy Whitcroftmy $dbg_attr = 0;
147c2fdda0dSAndy Whitcroftfor my $key (keys %debug) {
14821caa13cSAndy Whitcroft	## no critic
14921caa13cSAndy Whitcroft	eval "\${dbg_$key} = '$debug{$key}';";
15021caa13cSAndy Whitcroft	die "$@" if ($@);
151c2fdda0dSAndy Whitcroft}
152c2fdda0dSAndy Whitcroft
153d2c0a235SAndy Whitcroftmy $rpt_cleaners = 0;
154d2c0a235SAndy Whitcroft
1558905a67cSAndy Whitcroftif ($terse) {
1568905a67cSAndy Whitcroft	$emacs = 1;
1578905a67cSAndy Whitcroft	$quiet++;
1588905a67cSAndy Whitcroft}
1598905a67cSAndy Whitcroft
1606c72ffaaSAndy Whitcroftif ($tree) {
1616c72ffaaSAndy Whitcroft	if (defined $root) {
1626c72ffaaSAndy Whitcroft		if (!top_of_kernel_tree($root)) {
1636c72ffaaSAndy Whitcroft			die "$P: $root: --root does not point at a valid tree\n";
1646c72ffaaSAndy Whitcroft		}
1656c72ffaaSAndy Whitcroft	} else {
1666c72ffaaSAndy Whitcroft		if (top_of_kernel_tree('.')) {
1676c72ffaaSAndy Whitcroft			$root = '.';
1686c72ffaaSAndy Whitcroft		} elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
1696c72ffaaSAndy Whitcroft						top_of_kernel_tree($1)) {
1706c72ffaaSAndy Whitcroft			$root = $1;
1716c72ffaaSAndy Whitcroft		}
1726c72ffaaSAndy Whitcroft	}
1736c72ffaaSAndy Whitcroft
1746c72ffaaSAndy Whitcroft	if (!defined $root) {
1750a920b5bSAndy Whitcroft		print "Must be run from the top-level dir. of a kernel tree\n";
1760a920b5bSAndy Whitcroft		exit(2);
1770a920b5bSAndy Whitcroft	}
1786c72ffaaSAndy Whitcroft}
1796c72ffaaSAndy Whitcroft
1806c72ffaaSAndy Whitcroftmy $emitted_corrupt = 0;
1816c72ffaaSAndy Whitcroft
1822ceb532bSAndy Whitcroftour $Ident	= qr{
1832ceb532bSAndy Whitcroft			[A-Za-z_][A-Za-z\d_]*
1842ceb532bSAndy Whitcroft			(?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
1852ceb532bSAndy Whitcroft		}x;
1866c72ffaaSAndy Whitcroftour $Storage	= qr{extern|static|asmlinkage};
1876c72ffaaSAndy Whitcroftour $Sparse	= qr{
1886c72ffaaSAndy Whitcroft			__user|
1896c72ffaaSAndy Whitcroft			__kernel|
1906c72ffaaSAndy Whitcroft			__force|
1916c72ffaaSAndy Whitcroft			__iomem|
1926c72ffaaSAndy Whitcroft			__must_check|
1936c72ffaaSAndy Whitcroft			__init_refok|
194417495edSAndy Whitcroft			__kprobes|
195165e72a6SSven Eckelmann			__ref|
196165e72a6SSven Eckelmann			__rcu
1976c72ffaaSAndy Whitcroft		}x;
19852131292SWolfram Sang
19952131292SWolfram Sang# Notes to $Attribute:
20052131292SWolfram Sang# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
2016c72ffaaSAndy Whitcroftour $Attribute	= qr{
2026c72ffaaSAndy Whitcroft			const|
20303f1df7dSJoe Perches			__percpu|
20403f1df7dSJoe Perches			__nocast|
20503f1df7dSJoe Perches			__safe|
20603f1df7dSJoe Perches			__bitwise__|
20703f1df7dSJoe Perches			__packed__|
20803f1df7dSJoe Perches			__packed2__|
20903f1df7dSJoe Perches			__naked|
21003f1df7dSJoe Perches			__maybe_unused|
21103f1df7dSJoe Perches			__always_unused|
21203f1df7dSJoe Perches			__noreturn|
21303f1df7dSJoe Perches			__used|
21403f1df7dSJoe Perches			__cold|
21503f1df7dSJoe Perches			__noclone|
21603f1df7dSJoe Perches			__deprecated|
2176c72ffaaSAndy Whitcroft			__read_mostly|
2186c72ffaaSAndy Whitcroft			__kprobes|
21952131292SWolfram Sang			__(?:mem|cpu|dev|)(?:initdata|initconst|init\b)|
22024e1d81aSAndy Whitcroft			____cacheline_aligned|
22124e1d81aSAndy Whitcroft			____cacheline_aligned_in_smp|
2225fe3af11SAndy Whitcroft			____cacheline_internodealigned_in_smp|
2235fe3af11SAndy Whitcroft			__weak
2246c72ffaaSAndy Whitcroft		  }x;
225c45dcabdSAndy Whitcroftour $Modifier;
2266c72ffaaSAndy Whitcroftour $Inline	= qr{inline|__always_inline|noinline};
2276c72ffaaSAndy Whitcroftour $Member	= qr{->$Ident|\.$Ident|\[[^]]*\]};
2286c72ffaaSAndy Whitcroftour $Lval	= qr{$Ident(?:$Member)*};
2296c72ffaaSAndy Whitcroft
2306c72ffaaSAndy Whitcroftour $Constant	= qr{(?:[0-9]+|0x[0-9a-fA-F]+)[UL]*};
2316c72ffaaSAndy Whitcroftour $Assignment	= qr{(?:\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=)};
23286f9d059SAndy Whitcroftour $Compare    = qr{<=|>=|==|!=|<|>};
2336c72ffaaSAndy Whitcroftour $Operators	= qr{
2346c72ffaaSAndy Whitcroft			<=|>=|==|!=|
2356c72ffaaSAndy Whitcroft			=>|->|<<|>>|<|>|!|~|
236c2fdda0dSAndy Whitcroft			&&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%
2376c72ffaaSAndy Whitcroft		  }x;
2386c72ffaaSAndy Whitcroft
2398905a67cSAndy Whitcroftour $NonptrType;
2408905a67cSAndy Whitcroftour $Type;
2418905a67cSAndy Whitcroftour $Declare;
2428905a67cSAndy Whitcroft
24315662b3eSJoe Perchesour $NON_ASCII_UTF8	= qr{
24415662b3eSJoe Perches	[\xC2-\xDF][\x80-\xBF]               # non-overlong 2-byte
245171ae1a4SAndy Whitcroft	|  \xE0[\xA0-\xBF][\x80-\xBF]        # excluding overlongs
246171ae1a4SAndy Whitcroft	| [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}  # straight 3-byte
247171ae1a4SAndy Whitcroft	|  \xED[\x80-\x9F][\x80-\xBF]        # excluding surrogates
248171ae1a4SAndy Whitcroft	|  \xF0[\x90-\xBF][\x80-\xBF]{2}     # planes 1-3
249171ae1a4SAndy Whitcroft	| [\xF1-\xF3][\x80-\xBF]{3}          # planes 4-15
250171ae1a4SAndy Whitcroft	|  \xF4[\x80-\x8F][\x80-\xBF]{2}     # plane 16
251171ae1a4SAndy Whitcroft}x;
252171ae1a4SAndy Whitcroft
25315662b3eSJoe Perchesour $UTF8	= qr{
25415662b3eSJoe Perches	[\x09\x0A\x0D\x20-\x7E]              # ASCII
25515662b3eSJoe Perches	| $NON_ASCII_UTF8
25615662b3eSJoe Perches}x;
25715662b3eSJoe Perches
2588ed22cadSAndy Whitcroftour $typeTypedefs = qr{(?x:
259fb9e9096SAndy Whitcroft	(?:__)?(?:u|s|be|le)(?:8|16|32|64)|
2608ed22cadSAndy Whitcroft	atomic_t
2618ed22cadSAndy Whitcroft)};
2628ed22cadSAndy Whitcroft
263691e669bSJoe Perchesour $logFunctions = qr{(?x:
2646e60c02eSJoe Perches	printk(?:_ratelimited|_once|)|
2656e60c02eSJoe Perches	[a-z0-9]+_(?:printk|emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)(?:_ratelimited|_once|)|
2666e60c02eSJoe Perches	WARN(?:_RATELIMIT|_ONCE|)|
267b0531722SJoe Perches	panic|
268b0531722SJoe Perches	MODULE_[A-Z_]+
269691e669bSJoe Perches)};
270691e669bSJoe Perches
27120112475SJoe Perchesour $signature_tags = qr{(?xi:
27220112475SJoe Perches	Signed-off-by:|
27320112475SJoe Perches	Acked-by:|
27420112475SJoe Perches	Tested-by:|
27520112475SJoe Perches	Reviewed-by:|
27620112475SJoe Perches	Reported-by:|
27720112475SJoe Perches	To:|
27820112475SJoe Perches	Cc:
27920112475SJoe Perches)};
28020112475SJoe Perches
2818905a67cSAndy Whitcroftour @typeList = (
2828905a67cSAndy Whitcroft	qr{void},
283c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?char},
284c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?short},
285c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?int},
286c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?long},
287c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?long\s+int},
288c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?long\s+long},
289c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?long\s+long\s+int},
2908905a67cSAndy Whitcroft	qr{unsigned},
2918905a67cSAndy Whitcroft	qr{float},
2928905a67cSAndy Whitcroft	qr{double},
2938905a67cSAndy Whitcroft	qr{bool},
2948905a67cSAndy Whitcroft	qr{struct\s+$Ident},
2958905a67cSAndy Whitcroft	qr{union\s+$Ident},
2968905a67cSAndy Whitcroft	qr{enum\s+$Ident},
2978905a67cSAndy Whitcroft	qr{${Ident}_t},
2988905a67cSAndy Whitcroft	qr{${Ident}_handler},
2998905a67cSAndy Whitcroft	qr{${Ident}_handler_fn},
3008905a67cSAndy Whitcroft);
301c45dcabdSAndy Whitcroftour @modifierList = (
302c45dcabdSAndy Whitcroft	qr{fastcall},
303c45dcabdSAndy Whitcroft);
3048905a67cSAndy Whitcroft
3057840a94cSWolfram Sangour $allowed_asm_includes = qr{(?x:
3067840a94cSWolfram Sang	irq|
3077840a94cSWolfram Sang	memory
3087840a94cSWolfram Sang)};
3097840a94cSWolfram Sang# memory.h: ARM has a custom one
3107840a94cSWolfram Sang
3118905a67cSAndy Whitcroftsub build_types {
312d2172eb5SAndy Whitcroft	my $mods = "(?x:  \n" . join("|\n  ", @modifierList) . "\n)";
313d2172eb5SAndy Whitcroft	my $all = "(?x:  \n" . join("|\n  ", @typeList) . "\n)";
314c8cb2ca3SAndy Whitcroft	$Modifier	= qr{(?:$Attribute|$Sparse|$mods)};
3158905a67cSAndy Whitcroft	$NonptrType	= qr{
316d2172eb5SAndy Whitcroft			(?:$Modifier\s+|const\s+)*
317cf655043SAndy Whitcroft			(?:
318c45dcabdSAndy Whitcroft				(?:typeof|__typeof__)\s*\(\s*\**\s*$Ident\s*\)|
3198ed22cadSAndy Whitcroft				(?:$typeTypedefs\b)|
320c45dcabdSAndy Whitcroft				(?:${all}\b)
321cf655043SAndy Whitcroft			)
322c8cb2ca3SAndy Whitcroft			(?:\s+$Modifier|\s+const)*
3238905a67cSAndy Whitcroft		  }x;
3248905a67cSAndy Whitcroft	$Type	= qr{
325c45dcabdSAndy Whitcroft			$NonptrType
32665863862SAndy Whitcroft			(?:[\s\*]+\s*const|[\s\*]+|(?:\s*\[\s*\])+)?
327c8cb2ca3SAndy Whitcroft			(?:\s+$Inline|\s+$Modifier)*
3288905a67cSAndy Whitcroft		  }x;
3298905a67cSAndy Whitcroft	$Declare	= qr{(?:$Storage\s+)?$Type};
3308905a67cSAndy Whitcroft}
3318905a67cSAndy Whitcroftbuild_types();
3326c72ffaaSAndy Whitcroft
3337d2367afSJoe Perchesour $match_balanced_parentheses = qr/(\((?:[^\(\)]+|(-1))*\))/;
3347d2367afSJoe Perches
3357d2367afSJoe Perchesour $Typecast	= qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
3367d2367afSJoe Perchesour $LvalOrFunc	= qr{($Lval)\s*($match_balanced_parentheses{0,1})\s*};
3377d2367afSJoe Perches
3387d2367afSJoe Perchessub deparenthesize {
3397d2367afSJoe Perches	my ($string) = @_;
3407d2367afSJoe Perches	return "" if (!defined($string));
3417d2367afSJoe Perches	$string =~ s@^\s*\(\s*@@g;
3427d2367afSJoe Perches	$string =~ s@\s*\)\s*$@@g;
3437d2367afSJoe Perches	$string =~ s@\s+@ @g;
3447d2367afSJoe Perches	return $string;
3457d2367afSJoe Perches}
3467d2367afSJoe Perches
3476c72ffaaSAndy Whitcroft$chk_signoff = 0 if ($file);
3480a920b5bSAndy Whitcroft
3494a0df2efSAndy Whitcroftmy @dep_includes = ();
3504a0df2efSAndy Whitcroftmy @dep_functions = ();
3516c72ffaaSAndy Whitcroftmy $removal = "Documentation/feature-removal-schedule.txt";
3526c72ffaaSAndy Whitcroftif ($tree && -f "$root/$removal") {
35321caa13cSAndy Whitcroft	open(my $REMOVE, '<', "$root/$removal") ||
3546c72ffaaSAndy Whitcroft				die "$P: $removal: open failed - $!\n";
35521caa13cSAndy Whitcroft	while (<$REMOVE>) {
356f0a594c1SAndy Whitcroft		if (/^Check:\s+(.*\S)/) {
357f0a594c1SAndy Whitcroft			for my $entry (split(/[, ]+/, $1)) {
358f0a594c1SAndy Whitcroft				if ($entry =~ m@include/(.*)@) {
3594a0df2efSAndy Whitcroft					push(@dep_includes, $1);
3604a0df2efSAndy Whitcroft
361f0a594c1SAndy Whitcroft				} elsif ($entry !~ m@/@) {
362f0a594c1SAndy Whitcroft					push(@dep_functions, $entry);
363f0a594c1SAndy Whitcroft				}
3644a0df2efSAndy Whitcroft			}
3650a920b5bSAndy Whitcroft		}
3660a920b5bSAndy Whitcroft	}
36721caa13cSAndy Whitcroft	close($REMOVE);
3680a920b5bSAndy Whitcroft}
3690a920b5bSAndy Whitcroft
37000df344fSAndy Whitcroftmy @rawlines = ();
371c2fdda0dSAndy Whitcroftmy @lines = ();
372c2fdda0dSAndy Whitcroftmy $vname;
3736c72ffaaSAndy Whitcroftfor my $filename (@ARGV) {
37421caa13cSAndy Whitcroft	my $FILE;
3756c72ffaaSAndy Whitcroft	if ($file) {
37621caa13cSAndy Whitcroft		open($FILE, '-|', "diff -u /dev/null $filename") ||
3776c72ffaaSAndy Whitcroft			die "$P: $filename: diff failed - $!\n";
37821caa13cSAndy Whitcroft	} elsif ($filename eq '-') {
37921caa13cSAndy Whitcroft		open($FILE, '<&STDIN');
3806c72ffaaSAndy Whitcroft	} else {
38121caa13cSAndy Whitcroft		open($FILE, '<', "$filename") ||
3826c72ffaaSAndy Whitcroft			die "$P: $filename: open failed - $!\n";
3836c72ffaaSAndy Whitcroft	}
384c2fdda0dSAndy Whitcroft	if ($filename eq '-') {
385c2fdda0dSAndy Whitcroft		$vname = 'Your patch';
386c2fdda0dSAndy Whitcroft	} else {
387c2fdda0dSAndy Whitcroft		$vname = $filename;
388c2fdda0dSAndy Whitcroft	}
38921caa13cSAndy Whitcroft	while (<$FILE>) {
3900a920b5bSAndy Whitcroft		chomp;
39100df344fSAndy Whitcroft		push(@rawlines, $_);
3926c72ffaaSAndy Whitcroft	}
39321caa13cSAndy Whitcroft	close($FILE);
394c2fdda0dSAndy Whitcroft	if (!process($filename)) {
3950a920b5bSAndy Whitcroft		$exit = 1;
3960a920b5bSAndy Whitcroft	}
39700df344fSAndy Whitcroft	@rawlines = ();
39813214adfSAndy Whitcroft	@lines = ();
3990a920b5bSAndy Whitcroft}
4000a920b5bSAndy Whitcroft
4010a920b5bSAndy Whitcroftexit($exit);
4020a920b5bSAndy Whitcroft
4030a920b5bSAndy Whitcroftsub top_of_kernel_tree {
4046c72ffaaSAndy Whitcroft	my ($root) = @_;
4056c72ffaaSAndy Whitcroft
4066c72ffaaSAndy Whitcroft	my @tree_check = (
4076c72ffaaSAndy Whitcroft		"COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
4086c72ffaaSAndy Whitcroft		"README", "Documentation", "arch", "include", "drivers",
4096c72ffaaSAndy Whitcroft		"fs", "init", "ipc", "kernel", "lib", "scripts",
4106c72ffaaSAndy Whitcroft	);
4116c72ffaaSAndy Whitcroft
4126c72ffaaSAndy Whitcroft	foreach my $check (@tree_check) {
4136c72ffaaSAndy Whitcroft		if (! -e $root . '/' . $check) {
4140a920b5bSAndy Whitcroft			return 0;
4150a920b5bSAndy Whitcroft		}
4166c72ffaaSAndy Whitcroft	}
4176c72ffaaSAndy Whitcroft	return 1;
4186c72ffaaSAndy Whitcroft    }
4190a920b5bSAndy Whitcroft
42020112475SJoe Perchessub parse_email {
42120112475SJoe Perches	my ($formatted_email) = @_;
42220112475SJoe Perches
42320112475SJoe Perches	my $name = "";
42420112475SJoe Perches	my $address = "";
42520112475SJoe Perches	my $comment = "";
42620112475SJoe Perches
42720112475SJoe Perches	if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
42820112475SJoe Perches		$name = $1;
42920112475SJoe Perches		$address = $2;
43020112475SJoe Perches		$comment = $3 if defined $3;
43120112475SJoe Perches	} elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
43220112475SJoe Perches		$address = $1;
43320112475SJoe Perches		$comment = $2 if defined $2;
43420112475SJoe Perches	} elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
43520112475SJoe Perches		$address = $1;
43620112475SJoe Perches		$comment = $2 if defined $2;
43720112475SJoe Perches		$formatted_email =~ s/$address.*$//;
43820112475SJoe Perches		$name = $formatted_email;
43920112475SJoe Perches		$name =~ s/^\s+|\s+$//g;
44020112475SJoe Perches		$name =~ s/^\"|\"$//g;
44120112475SJoe Perches		# If there's a name left after stripping spaces and
44220112475SJoe Perches		# leading quotes, and the address doesn't have both
44320112475SJoe Perches		# leading and trailing angle brackets, the address
44420112475SJoe Perches		# is invalid. ie:
44520112475SJoe Perches		#   "joe smith [email protected]" bad
44620112475SJoe Perches		#   "joe smith <[email protected]" bad
44720112475SJoe Perches		if ($name ne "" && $address !~ /^<[^>]+>$/) {
44820112475SJoe Perches			$name = "";
44920112475SJoe Perches			$address = "";
45020112475SJoe Perches			$comment = "";
45120112475SJoe Perches		}
45220112475SJoe Perches	}
45320112475SJoe Perches
45420112475SJoe Perches	$name =~ s/^\s+|\s+$//g;
45520112475SJoe Perches	$name =~ s/^\"|\"$//g;
45620112475SJoe Perches	$address =~ s/^\s+|\s+$//g;
45720112475SJoe Perches	$address =~ s/^\<|\>$//g;
45820112475SJoe Perches
45920112475SJoe Perches	if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
46020112475SJoe Perches		$name =~ s/(?<!\\)"/\\"/g; ##escape quotes
46120112475SJoe Perches		$name = "\"$name\"";
46220112475SJoe Perches	}
46320112475SJoe Perches
46420112475SJoe Perches	return ($name, $address, $comment);
46520112475SJoe Perches}
46620112475SJoe Perches
46720112475SJoe Perchessub format_email {
46820112475SJoe Perches	my ($name, $address) = @_;
46920112475SJoe Perches
47020112475SJoe Perches	my $formatted_email;
47120112475SJoe Perches
47220112475SJoe Perches	$name =~ s/^\s+|\s+$//g;
47320112475SJoe Perches	$name =~ s/^\"|\"$//g;
47420112475SJoe Perches	$address =~ s/^\s+|\s+$//g;
47520112475SJoe Perches
47620112475SJoe Perches	if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
47720112475SJoe Perches		$name =~ s/(?<!\\)"/\\"/g; ##escape quotes
47820112475SJoe Perches		$name = "\"$name\"";
47920112475SJoe Perches	}
48020112475SJoe Perches
48120112475SJoe Perches	if ("$name" eq "") {
48220112475SJoe Perches		$formatted_email = "$address";
48320112475SJoe Perches	} else {
48420112475SJoe Perches		$formatted_email = "$name <$address>";
48520112475SJoe Perches	}
48620112475SJoe Perches
48720112475SJoe Perches	return $formatted_email;
48820112475SJoe Perches}
48920112475SJoe Perches
490000d1cc1SJoe Perchessub which_conf {
491000d1cc1SJoe Perches	my ($conf) = @_;
492000d1cc1SJoe Perches
493000d1cc1SJoe Perches	foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
494000d1cc1SJoe Perches		if (-e "$path/$conf") {
495000d1cc1SJoe Perches			return "$path/$conf";
496000d1cc1SJoe Perches		}
497000d1cc1SJoe Perches	}
498000d1cc1SJoe Perches
499000d1cc1SJoe Perches	return "";
500000d1cc1SJoe Perches}
501000d1cc1SJoe Perches
5020a920b5bSAndy Whitcroftsub expand_tabs {
5030a920b5bSAndy Whitcroft	my ($str) = @_;
5040a920b5bSAndy Whitcroft
5050a920b5bSAndy Whitcroft	my $res = '';
5060a920b5bSAndy Whitcroft	my $n = 0;
5070a920b5bSAndy Whitcroft	for my $c (split(//, $str)) {
5080a920b5bSAndy Whitcroft		if ($c eq "\t") {
5090a920b5bSAndy Whitcroft			$res .= ' ';
5100a920b5bSAndy Whitcroft			$n++;
5110a920b5bSAndy Whitcroft			for (; ($n % 8) != 0; $n++) {
5120a920b5bSAndy Whitcroft				$res .= ' ';
5130a920b5bSAndy Whitcroft			}
5140a920b5bSAndy Whitcroft			next;
5150a920b5bSAndy Whitcroft		}
5160a920b5bSAndy Whitcroft		$res .= $c;
5170a920b5bSAndy Whitcroft		$n++;
5180a920b5bSAndy Whitcroft	}
5190a920b5bSAndy Whitcroft
5200a920b5bSAndy Whitcroft	return $res;
5210a920b5bSAndy Whitcroft}
5226c72ffaaSAndy Whitcroftsub copy_spacing {
523773647a0SAndy Whitcroft	(my $res = shift) =~ tr/\t/ /c;
5246c72ffaaSAndy Whitcroft	return $res;
5256c72ffaaSAndy Whitcroft}
5260a920b5bSAndy Whitcroft
5274a0df2efSAndy Whitcroftsub line_stats {
5284a0df2efSAndy Whitcroft	my ($line) = @_;
5294a0df2efSAndy Whitcroft
5304a0df2efSAndy Whitcroft	# Drop the diff line leader and expand tabs
5314a0df2efSAndy Whitcroft	$line =~ s/^.//;
5324a0df2efSAndy Whitcroft	$line = expand_tabs($line);
5334a0df2efSAndy Whitcroft
5344a0df2efSAndy Whitcroft	# Pick the indent from the front of the line.
5354a0df2efSAndy Whitcroft	my ($white) = ($line =~ /^(\s*)/);
5364a0df2efSAndy Whitcroft
5374a0df2efSAndy Whitcroft	return (length($line), length($white));
5384a0df2efSAndy Whitcroft}
5394a0df2efSAndy Whitcroft
540773647a0SAndy Whitcroftmy $sanitise_quote = '';
541773647a0SAndy Whitcroft
542773647a0SAndy Whitcroftsub sanitise_line_reset {
543773647a0SAndy Whitcroft	my ($in_comment) = @_;
544773647a0SAndy Whitcroft
545773647a0SAndy Whitcroft	if ($in_comment) {
546773647a0SAndy Whitcroft		$sanitise_quote = '*/';
547773647a0SAndy Whitcroft	} else {
548773647a0SAndy Whitcroft		$sanitise_quote = '';
549773647a0SAndy Whitcroft	}
550773647a0SAndy Whitcroft}
55100df344fSAndy Whitcroftsub sanitise_line {
55200df344fSAndy Whitcroft	my ($line) = @_;
55300df344fSAndy Whitcroft
55400df344fSAndy Whitcroft	my $res = '';
55500df344fSAndy Whitcroft	my $l = '';
55600df344fSAndy Whitcroft
557c2fdda0dSAndy Whitcroft	my $qlen = 0;
558773647a0SAndy Whitcroft	my $off = 0;
559773647a0SAndy Whitcroft	my $c;
56000df344fSAndy Whitcroft
561773647a0SAndy Whitcroft	# Always copy over the diff marker.
562773647a0SAndy Whitcroft	$res = substr($line, 0, 1);
563773647a0SAndy Whitcroft
564773647a0SAndy Whitcroft	for ($off = 1; $off < length($line); $off++) {
565773647a0SAndy Whitcroft		$c = substr($line, $off, 1);
566773647a0SAndy Whitcroft
567773647a0SAndy Whitcroft		# Comments we are wacking completly including the begin
568773647a0SAndy Whitcroft		# and end, all to $;.
569773647a0SAndy Whitcroft		if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
570773647a0SAndy Whitcroft			$sanitise_quote = '*/';
571773647a0SAndy Whitcroft
572773647a0SAndy Whitcroft			substr($res, $off, 2, "$;$;");
573773647a0SAndy Whitcroft			$off++;
57400df344fSAndy Whitcroft			next;
575773647a0SAndy Whitcroft		}
57681bc0e02SAndy Whitcroft		if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
577773647a0SAndy Whitcroft			$sanitise_quote = '';
578773647a0SAndy Whitcroft			substr($res, $off, 2, "$;$;");
579773647a0SAndy Whitcroft			$off++;
580773647a0SAndy Whitcroft			next;
581773647a0SAndy Whitcroft		}
582113f04a8SDaniel Walker		if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
583113f04a8SDaniel Walker			$sanitise_quote = '//';
584113f04a8SDaniel Walker
585113f04a8SDaniel Walker			substr($res, $off, 2, $sanitise_quote);
586113f04a8SDaniel Walker			$off++;
587113f04a8SDaniel Walker			next;
588113f04a8SDaniel Walker		}
589773647a0SAndy Whitcroft
590773647a0SAndy Whitcroft		# A \ in a string means ignore the next character.
591773647a0SAndy Whitcroft		if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
592773647a0SAndy Whitcroft		    $c eq "\\") {
593773647a0SAndy Whitcroft			substr($res, $off, 2, 'XX');
594773647a0SAndy Whitcroft			$off++;
595773647a0SAndy Whitcroft			next;
596773647a0SAndy Whitcroft		}
597773647a0SAndy Whitcroft		# Regular quotes.
598773647a0SAndy Whitcroft		if ($c eq "'" || $c eq '"') {
599773647a0SAndy Whitcroft			if ($sanitise_quote eq '') {
600773647a0SAndy Whitcroft				$sanitise_quote = $c;
601773647a0SAndy Whitcroft
602773647a0SAndy Whitcroft				substr($res, $off, 1, $c);
603773647a0SAndy Whitcroft				next;
604773647a0SAndy Whitcroft			} elsif ($sanitise_quote eq $c) {
605773647a0SAndy Whitcroft				$sanitise_quote = '';
60600df344fSAndy Whitcroft			}
60700df344fSAndy Whitcroft		}
608773647a0SAndy Whitcroft
609fae17daeSAndy Whitcroft		#print "c<$c> SQ<$sanitise_quote>\n";
610773647a0SAndy Whitcroft		if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
611773647a0SAndy Whitcroft			substr($res, $off, 1, $;);
612113f04a8SDaniel Walker		} elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
613113f04a8SDaniel Walker			substr($res, $off, 1, $;);
614773647a0SAndy Whitcroft		} elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
615773647a0SAndy Whitcroft			substr($res, $off, 1, 'X');
61600df344fSAndy Whitcroft		} else {
617773647a0SAndy Whitcroft			substr($res, $off, 1, $c);
61800df344fSAndy Whitcroft		}
619c2fdda0dSAndy Whitcroft	}
620c2fdda0dSAndy Whitcroft
621113f04a8SDaniel Walker	if ($sanitise_quote eq '//') {
622113f04a8SDaniel Walker		$sanitise_quote = '';
623113f04a8SDaniel Walker	}
624113f04a8SDaniel Walker
625c2fdda0dSAndy Whitcroft	# The pathname on a #include may be surrounded by '<' and '>'.
626c45dcabdSAndy Whitcroft	if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
627c2fdda0dSAndy Whitcroft		my $clean = 'X' x length($1);
628c2fdda0dSAndy Whitcroft		$res =~ s@\<.*\>@<$clean>@;
629c2fdda0dSAndy Whitcroft
630c2fdda0dSAndy Whitcroft	# The whole of a #error is a string.
631c45dcabdSAndy Whitcroft	} elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
632c2fdda0dSAndy Whitcroft		my $clean = 'X' x length($1);
633c45dcabdSAndy Whitcroft		$res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
634c2fdda0dSAndy Whitcroft	}
635c2fdda0dSAndy Whitcroft
63600df344fSAndy Whitcroft	return $res;
63700df344fSAndy Whitcroft}
63800df344fSAndy Whitcroft
6398905a67cSAndy Whitcroftsub ctx_statement_block {
6408905a67cSAndy Whitcroft	my ($linenr, $remain, $off) = @_;
6418905a67cSAndy Whitcroft	my $line = $linenr - 1;
6428905a67cSAndy Whitcroft	my $blk = '';
6438905a67cSAndy Whitcroft	my $soff = $off;
6448905a67cSAndy Whitcroft	my $coff = $off - 1;
645773647a0SAndy Whitcroft	my $coff_set = 0;
6468905a67cSAndy Whitcroft
64713214adfSAndy Whitcroft	my $loff = 0;
64813214adfSAndy Whitcroft
6498905a67cSAndy Whitcroft	my $type = '';
6508905a67cSAndy Whitcroft	my $level = 0;
651a2750645SAndy Whitcroft	my @stack = ();
652cf655043SAndy Whitcroft	my $p;
6538905a67cSAndy Whitcroft	my $c;
6548905a67cSAndy Whitcroft	my $len = 0;
65513214adfSAndy Whitcroft
65613214adfSAndy Whitcroft	my $remainder;
6578905a67cSAndy Whitcroft	while (1) {
658a2750645SAndy Whitcroft		@stack = (['', 0]) if ($#stack == -1);
659a2750645SAndy Whitcroft
660773647a0SAndy Whitcroft		#warn "CSB: blk<$blk> remain<$remain>\n";
6618905a67cSAndy Whitcroft		# If we are about to drop off the end, pull in more
6628905a67cSAndy Whitcroft		# context.
6638905a67cSAndy Whitcroft		if ($off >= $len) {
6648905a67cSAndy Whitcroft			for (; $remain > 0; $line++) {
665dea33496SAndy Whitcroft				last if (!defined $lines[$line]);
666c2fdda0dSAndy Whitcroft				next if ($lines[$line] =~ /^-/);
6678905a67cSAndy Whitcroft				$remain--;
66813214adfSAndy Whitcroft				$loff = $len;
669c2fdda0dSAndy Whitcroft				$blk .= $lines[$line] . "\n";
6708905a67cSAndy Whitcroft				$len = length($blk);
6718905a67cSAndy Whitcroft				$line++;
6728905a67cSAndy Whitcroft				last;
6738905a67cSAndy Whitcroft			}
6748905a67cSAndy Whitcroft			# Bail if there is no further context.
6758905a67cSAndy Whitcroft			#warn "CSB: blk<$blk> off<$off> len<$len>\n";
67613214adfSAndy Whitcroft			if ($off >= $len) {
6778905a67cSAndy Whitcroft				last;
6788905a67cSAndy Whitcroft			}
679f74bd194SAndy Whitcroft			if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
680f74bd194SAndy Whitcroft				$level++;
681f74bd194SAndy Whitcroft				$type = '#';
682f74bd194SAndy Whitcroft			}
6838905a67cSAndy Whitcroft		}
684cf655043SAndy Whitcroft		$p = $c;
6858905a67cSAndy Whitcroft		$c = substr($blk, $off, 1);
68613214adfSAndy Whitcroft		$remainder = substr($blk, $off);
6878905a67cSAndy Whitcroft
688773647a0SAndy Whitcroft		#warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
6894635f4fbSAndy Whitcroft
6904635f4fbSAndy Whitcroft		# Handle nested #if/#else.
6914635f4fbSAndy Whitcroft		if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
6924635f4fbSAndy Whitcroft			push(@stack, [ $type, $level ]);
6934635f4fbSAndy Whitcroft		} elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
6944635f4fbSAndy Whitcroft			($type, $level) = @{$stack[$#stack - 1]};
6954635f4fbSAndy Whitcroft		} elsif ($remainder =~ /^#\s*endif\b/) {
6964635f4fbSAndy Whitcroft			($type, $level) = @{pop(@stack)};
6974635f4fbSAndy Whitcroft		}
6984635f4fbSAndy Whitcroft
6998905a67cSAndy Whitcroft		# Statement ends at the ';' or a close '}' at the
7008905a67cSAndy Whitcroft		# outermost level.
7018905a67cSAndy Whitcroft		if ($level == 0 && $c eq ';') {
7028905a67cSAndy Whitcroft			last;
7038905a67cSAndy Whitcroft		}
7048905a67cSAndy Whitcroft
70513214adfSAndy Whitcroft		# An else is really a conditional as long as its not else if
706773647a0SAndy Whitcroft		if ($level == 0 && $coff_set == 0 &&
707773647a0SAndy Whitcroft				(!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
708773647a0SAndy Whitcroft				$remainder =~ /^(else)(?:\s|{)/ &&
709773647a0SAndy Whitcroft				$remainder !~ /^else\s+if\b/) {
710773647a0SAndy Whitcroft			$coff = $off + length($1) - 1;
711773647a0SAndy Whitcroft			$coff_set = 1;
712773647a0SAndy Whitcroft			#warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
713773647a0SAndy Whitcroft			#warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
71413214adfSAndy Whitcroft		}
71513214adfSAndy Whitcroft
7168905a67cSAndy Whitcroft		if (($type eq '' || $type eq '(') && $c eq '(') {
7178905a67cSAndy Whitcroft			$level++;
7188905a67cSAndy Whitcroft			$type = '(';
7198905a67cSAndy Whitcroft		}
7208905a67cSAndy Whitcroft		if ($type eq '(' && $c eq ')') {
7218905a67cSAndy Whitcroft			$level--;
7228905a67cSAndy Whitcroft			$type = ($level != 0)? '(' : '';
7238905a67cSAndy Whitcroft
7248905a67cSAndy Whitcroft			if ($level == 0 && $coff < $soff) {
7258905a67cSAndy Whitcroft				$coff = $off;
726773647a0SAndy Whitcroft				$coff_set = 1;
727773647a0SAndy Whitcroft				#warn "CSB: mark coff<$coff>\n";
7288905a67cSAndy Whitcroft			}
7298905a67cSAndy Whitcroft		}
7308905a67cSAndy Whitcroft		if (($type eq '' || $type eq '{') && $c eq '{') {
7318905a67cSAndy Whitcroft			$level++;
7328905a67cSAndy Whitcroft			$type = '{';
7338905a67cSAndy Whitcroft		}
7348905a67cSAndy Whitcroft		if ($type eq '{' && $c eq '}') {
7358905a67cSAndy Whitcroft			$level--;
7368905a67cSAndy Whitcroft			$type = ($level != 0)? '{' : '';
7378905a67cSAndy Whitcroft
7388905a67cSAndy Whitcroft			if ($level == 0) {
739b998e001SPatrick Pannuto				if (substr($blk, $off + 1, 1) eq ';') {
740b998e001SPatrick Pannuto					$off++;
741b998e001SPatrick Pannuto				}
7428905a67cSAndy Whitcroft				last;
7438905a67cSAndy Whitcroft			}
7448905a67cSAndy Whitcroft		}
745f74bd194SAndy Whitcroft		# Preprocessor commands end at the newline unless escaped.
746f74bd194SAndy Whitcroft		if ($type eq '#' && $c eq "\n" && $p ne "\\") {
747f74bd194SAndy Whitcroft			$level--;
748f74bd194SAndy Whitcroft			$type = '';
749f74bd194SAndy Whitcroft			$off++;
750f74bd194SAndy Whitcroft			last;
751f74bd194SAndy Whitcroft		}
7528905a67cSAndy Whitcroft		$off++;
7538905a67cSAndy Whitcroft	}
754a3bb97a7SAndy Whitcroft	# We are truly at the end, so shuffle to the next line.
75513214adfSAndy Whitcroft	if ($off == $len) {
756a3bb97a7SAndy Whitcroft		$loff = $len + 1;
75713214adfSAndy Whitcroft		$line++;
75813214adfSAndy Whitcroft		$remain--;
75913214adfSAndy Whitcroft	}
7608905a67cSAndy Whitcroft
7618905a67cSAndy Whitcroft	my $statement = substr($blk, $soff, $off - $soff + 1);
7628905a67cSAndy Whitcroft	my $condition = substr($blk, $soff, $coff - $soff + 1);
7638905a67cSAndy Whitcroft
7648905a67cSAndy Whitcroft	#warn "STATEMENT<$statement>\n";
7658905a67cSAndy Whitcroft	#warn "CONDITION<$condition>\n";
7668905a67cSAndy Whitcroft
767773647a0SAndy Whitcroft	#print "coff<$coff> soff<$off> loff<$loff>\n";
76813214adfSAndy Whitcroft
76913214adfSAndy Whitcroft	return ($statement, $condition,
77013214adfSAndy Whitcroft			$line, $remain + 1, $off - $loff + 1, $level);
77113214adfSAndy Whitcroft}
77213214adfSAndy Whitcroft
773cf655043SAndy Whitcroftsub statement_lines {
774cf655043SAndy Whitcroft	my ($stmt) = @_;
775cf655043SAndy Whitcroft
776cf655043SAndy Whitcroft	# Strip the diff line prefixes and rip blank lines at start and end.
777cf655043SAndy Whitcroft	$stmt =~ s/(^|\n)./$1/g;
778cf655043SAndy Whitcroft	$stmt =~ s/^\s*//;
779cf655043SAndy Whitcroft	$stmt =~ s/\s*$//;
780cf655043SAndy Whitcroft
781cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
782cf655043SAndy Whitcroft
783cf655043SAndy Whitcroft	return $#stmt_lines + 2;
784cf655043SAndy Whitcroft}
785cf655043SAndy Whitcroft
786cf655043SAndy Whitcroftsub statement_rawlines {
787cf655043SAndy Whitcroft	my ($stmt) = @_;
788cf655043SAndy Whitcroft
789cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
790cf655043SAndy Whitcroft
791cf655043SAndy Whitcroft	return $#stmt_lines + 2;
792cf655043SAndy Whitcroft}
793cf655043SAndy Whitcroft
794cf655043SAndy Whitcroftsub statement_block_size {
795cf655043SAndy Whitcroft	my ($stmt) = @_;
796cf655043SAndy Whitcroft
797cf655043SAndy Whitcroft	$stmt =~ s/(^|\n)./$1/g;
798cf655043SAndy Whitcroft	$stmt =~ s/^\s*{//;
799cf655043SAndy Whitcroft	$stmt =~ s/}\s*$//;
800cf655043SAndy Whitcroft	$stmt =~ s/^\s*//;
801cf655043SAndy Whitcroft	$stmt =~ s/\s*$//;
802cf655043SAndy Whitcroft
803cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
804cf655043SAndy Whitcroft	my @stmt_statements = ($stmt =~ /;/g);
805cf655043SAndy Whitcroft
806cf655043SAndy Whitcroft	my $stmt_lines = $#stmt_lines + 2;
807cf655043SAndy Whitcroft	my $stmt_statements = $#stmt_statements + 1;
808cf655043SAndy Whitcroft
809cf655043SAndy Whitcroft	if ($stmt_lines > $stmt_statements) {
810cf655043SAndy Whitcroft		return $stmt_lines;
811cf655043SAndy Whitcroft	} else {
812cf655043SAndy Whitcroft		return $stmt_statements;
813cf655043SAndy Whitcroft	}
814cf655043SAndy Whitcroft}
815cf655043SAndy Whitcroft
81613214adfSAndy Whitcroftsub ctx_statement_full {
81713214adfSAndy Whitcroft	my ($linenr, $remain, $off) = @_;
81813214adfSAndy Whitcroft	my ($statement, $condition, $level);
81913214adfSAndy Whitcroft
82013214adfSAndy Whitcroft	my (@chunks);
82113214adfSAndy Whitcroft
822cf655043SAndy Whitcroft	# Grab the first conditional/block pair.
82313214adfSAndy Whitcroft	($statement, $condition, $linenr, $remain, $off, $level) =
82413214adfSAndy Whitcroft				ctx_statement_block($linenr, $remain, $off);
825773647a0SAndy Whitcroft	#print "F: c<$condition> s<$statement> remain<$remain>\n";
82613214adfSAndy Whitcroft	push(@chunks, [ $condition, $statement ]);
827cf655043SAndy Whitcroft	if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
828cf655043SAndy Whitcroft		return ($level, $linenr, @chunks);
829cf655043SAndy Whitcroft	}
830cf655043SAndy Whitcroft
831cf655043SAndy Whitcroft	# Pull in the following conditional/block pairs and see if they
832cf655043SAndy Whitcroft	# could continue the statement.
833cf655043SAndy Whitcroft	for (;;) {
83413214adfSAndy Whitcroft		($statement, $condition, $linenr, $remain, $off, $level) =
83513214adfSAndy Whitcroft				ctx_statement_block($linenr, $remain, $off);
836cf655043SAndy Whitcroft		#print "C: c<$condition> s<$statement> remain<$remain>\n";
837773647a0SAndy Whitcroft		last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
838cf655043SAndy Whitcroft		#print "C: push\n";
839cf655043SAndy Whitcroft		push(@chunks, [ $condition, $statement ]);
84013214adfSAndy Whitcroft	}
84113214adfSAndy Whitcroft
84213214adfSAndy Whitcroft	return ($level, $linenr, @chunks);
8438905a67cSAndy Whitcroft}
8448905a67cSAndy Whitcroft
8454a0df2efSAndy Whitcroftsub ctx_block_get {
846f0a594c1SAndy Whitcroft	my ($linenr, $remain, $outer, $open, $close, $off) = @_;
8474a0df2efSAndy Whitcroft	my $line;
8484a0df2efSAndy Whitcroft	my $start = $linenr - 1;
8494a0df2efSAndy Whitcroft	my $blk = '';
8504a0df2efSAndy Whitcroft	my @o;
8514a0df2efSAndy Whitcroft	my @c;
8524a0df2efSAndy Whitcroft	my @res = ();
8534a0df2efSAndy Whitcroft
854f0a594c1SAndy Whitcroft	my $level = 0;
8554635f4fbSAndy Whitcroft	my @stack = ($level);
85600df344fSAndy Whitcroft	for ($line = $start; $remain > 0; $line++) {
85700df344fSAndy Whitcroft		next if ($rawlines[$line] =~ /^-/);
85800df344fSAndy Whitcroft		$remain--;
85900df344fSAndy Whitcroft
86000df344fSAndy Whitcroft		$blk .= $rawlines[$line];
8614635f4fbSAndy Whitcroft
8624635f4fbSAndy Whitcroft		# Handle nested #if/#else.
86301464f30SAndy Whitcroft		if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
8644635f4fbSAndy Whitcroft			push(@stack, $level);
86501464f30SAndy Whitcroft		} elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
8664635f4fbSAndy Whitcroft			$level = $stack[$#stack - 1];
86701464f30SAndy Whitcroft		} elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
8684635f4fbSAndy Whitcroft			$level = pop(@stack);
8694635f4fbSAndy Whitcroft		}
8704635f4fbSAndy Whitcroft
87101464f30SAndy Whitcroft		foreach my $c (split(//, $lines[$line])) {
872f0a594c1SAndy Whitcroft			##print "C<$c>L<$level><$open$close>O<$off>\n";
873f0a594c1SAndy Whitcroft			if ($off > 0) {
874f0a594c1SAndy Whitcroft				$off--;
875f0a594c1SAndy Whitcroft				next;
876f0a594c1SAndy Whitcroft			}
8774a0df2efSAndy Whitcroft
878f0a594c1SAndy Whitcroft			if ($c eq $close && $level > 0) {
879f0a594c1SAndy Whitcroft				$level--;
880f0a594c1SAndy Whitcroft				last if ($level == 0);
881f0a594c1SAndy Whitcroft			} elsif ($c eq $open) {
882f0a594c1SAndy Whitcroft				$level++;
883f0a594c1SAndy Whitcroft			}
884f0a594c1SAndy Whitcroft		}
8854a0df2efSAndy Whitcroft
886f0a594c1SAndy Whitcroft		if (!$outer || $level <= 1) {
88700df344fSAndy Whitcroft			push(@res, $rawlines[$line]);
8884a0df2efSAndy Whitcroft		}
8894a0df2efSAndy Whitcroft
890f0a594c1SAndy Whitcroft		last if ($level == 0);
8914a0df2efSAndy Whitcroft	}
8924a0df2efSAndy Whitcroft
893f0a594c1SAndy Whitcroft	return ($level, @res);
8944a0df2efSAndy Whitcroft}
8954a0df2efSAndy Whitcroftsub ctx_block_outer {
8964a0df2efSAndy Whitcroft	my ($linenr, $remain) = @_;
8974a0df2efSAndy Whitcroft
898f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
899f0a594c1SAndy Whitcroft	return @r;
9004a0df2efSAndy Whitcroft}
9014a0df2efSAndy Whitcroftsub ctx_block {
9024a0df2efSAndy Whitcroft	my ($linenr, $remain) = @_;
9034a0df2efSAndy Whitcroft
904f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
905f0a594c1SAndy Whitcroft	return @r;
906653d4876SAndy Whitcroft}
907653d4876SAndy Whitcroftsub ctx_statement {
908f0a594c1SAndy Whitcroft	my ($linenr, $remain, $off) = @_;
909f0a594c1SAndy Whitcroft
910f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
911f0a594c1SAndy Whitcroft	return @r;
912f0a594c1SAndy Whitcroft}
913f0a594c1SAndy Whitcroftsub ctx_block_level {
914653d4876SAndy Whitcroft	my ($linenr, $remain) = @_;
915653d4876SAndy Whitcroft
916f0a594c1SAndy Whitcroft	return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
9174a0df2efSAndy Whitcroft}
9189c0ca6f9SAndy Whitcroftsub ctx_statement_level {
9199c0ca6f9SAndy Whitcroft	my ($linenr, $remain, $off) = @_;
9209c0ca6f9SAndy Whitcroft
9219c0ca6f9SAndy Whitcroft	return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
9229c0ca6f9SAndy Whitcroft}
9234a0df2efSAndy Whitcroft
9244a0df2efSAndy Whitcroftsub ctx_locate_comment {
9254a0df2efSAndy Whitcroft	my ($first_line, $end_line) = @_;
9264a0df2efSAndy Whitcroft
9274a0df2efSAndy Whitcroft	# Catch a comment on the end of the line itself.
928beae6332SAndy Whitcroft	my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
9294a0df2efSAndy Whitcroft	return $current_comment if (defined $current_comment);
9304a0df2efSAndy Whitcroft
9314a0df2efSAndy Whitcroft	# Look through the context and try and figure out if there is a
9324a0df2efSAndy Whitcroft	# comment.
9334a0df2efSAndy Whitcroft	my $in_comment = 0;
9344a0df2efSAndy Whitcroft	$current_comment = '';
9354a0df2efSAndy Whitcroft	for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
93600df344fSAndy Whitcroft		my $line = $rawlines[$linenr - 1];
93700df344fSAndy Whitcroft		#warn "           $line\n";
9384a0df2efSAndy Whitcroft		if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
9394a0df2efSAndy Whitcroft			$in_comment = 1;
9404a0df2efSAndy Whitcroft		}
9414a0df2efSAndy Whitcroft		if ($line =~ m@/\*@) {
9424a0df2efSAndy Whitcroft			$in_comment = 1;
9434a0df2efSAndy Whitcroft		}
9444a0df2efSAndy Whitcroft		if (!$in_comment && $current_comment ne '') {
9454a0df2efSAndy Whitcroft			$current_comment = '';
9464a0df2efSAndy Whitcroft		}
9474a0df2efSAndy Whitcroft		$current_comment .= $line . "\n" if ($in_comment);
9484a0df2efSAndy Whitcroft		if ($line =~ m@\*/@) {
9494a0df2efSAndy Whitcroft			$in_comment = 0;
9504a0df2efSAndy Whitcroft		}
9514a0df2efSAndy Whitcroft	}
9524a0df2efSAndy Whitcroft
9534a0df2efSAndy Whitcroft	chomp($current_comment);
9544a0df2efSAndy Whitcroft	return($current_comment);
9554a0df2efSAndy Whitcroft}
9564a0df2efSAndy Whitcroftsub ctx_has_comment {
9574a0df2efSAndy Whitcroft	my ($first_line, $end_line) = @_;
9584a0df2efSAndy Whitcroft	my $cmt = ctx_locate_comment($first_line, $end_line);
9594a0df2efSAndy Whitcroft
96000df344fSAndy Whitcroft	##print "LINE: $rawlines[$end_line - 1 ]\n";
9614a0df2efSAndy Whitcroft	##print "CMMT: $cmt\n";
9624a0df2efSAndy Whitcroft
9634a0df2efSAndy Whitcroft	return ($cmt ne '');
9644a0df2efSAndy Whitcroft}
9654a0df2efSAndy Whitcroft
9664d001e4dSAndy Whitcroftsub raw_line {
9674d001e4dSAndy Whitcroft	my ($linenr, $cnt) = @_;
9684d001e4dSAndy Whitcroft
9694d001e4dSAndy Whitcroft	my $offset = $linenr - 1;
9704d001e4dSAndy Whitcroft	$cnt++;
9714d001e4dSAndy Whitcroft
9724d001e4dSAndy Whitcroft	my $line;
9734d001e4dSAndy Whitcroft	while ($cnt) {
9744d001e4dSAndy Whitcroft		$line = $rawlines[$offset++];
9754d001e4dSAndy Whitcroft		next if (defined($line) && $line =~ /^-/);
9764d001e4dSAndy Whitcroft		$cnt--;
9774d001e4dSAndy Whitcroft	}
9784d001e4dSAndy Whitcroft
9794d001e4dSAndy Whitcroft	return $line;
9804d001e4dSAndy Whitcroft}
9814d001e4dSAndy Whitcroft
9820a920b5bSAndy Whitcroftsub cat_vet {
9830a920b5bSAndy Whitcroft	my ($vet) = @_;
9849c0ca6f9SAndy Whitcroft	my ($res, $coded);
9850a920b5bSAndy Whitcroft
9869c0ca6f9SAndy Whitcroft	$res = '';
9876c72ffaaSAndy Whitcroft	while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
9886c72ffaaSAndy Whitcroft		$res .= $1;
9896c72ffaaSAndy Whitcroft		if ($2 ne '') {
9909c0ca6f9SAndy Whitcroft			$coded = sprintf("^%c", unpack('C', $2) + 64);
9916c72ffaaSAndy Whitcroft			$res .= $coded;
9926c72ffaaSAndy Whitcroft		}
9939c0ca6f9SAndy Whitcroft	}
9949c0ca6f9SAndy Whitcroft	$res =~ s/$/\$/;
9950a920b5bSAndy Whitcroft
9969c0ca6f9SAndy Whitcroft	return $res;
9970a920b5bSAndy Whitcroft}
9980a920b5bSAndy Whitcroft
999c2fdda0dSAndy Whitcroftmy $av_preprocessor = 0;
1000cf655043SAndy Whitcroftmy $av_pending;
1001c2fdda0dSAndy Whitcroftmy @av_paren_type;
10021f65f947SAndy Whitcroftmy $av_pend_colon;
1003c2fdda0dSAndy Whitcroft
1004c2fdda0dSAndy Whitcroftsub annotate_reset {
1005c2fdda0dSAndy Whitcroft	$av_preprocessor = 0;
1006cf655043SAndy Whitcroft	$av_pending = '_';
1007cf655043SAndy Whitcroft	@av_paren_type = ('E');
10081f65f947SAndy Whitcroft	$av_pend_colon = 'O';
1009c2fdda0dSAndy Whitcroft}
1010c2fdda0dSAndy Whitcroft
10116c72ffaaSAndy Whitcroftsub annotate_values {
10126c72ffaaSAndy Whitcroft	my ($stream, $type) = @_;
10136c72ffaaSAndy Whitcroft
10146c72ffaaSAndy Whitcroft	my $res;
10151f65f947SAndy Whitcroft	my $var = '_' x length($stream);
10166c72ffaaSAndy Whitcroft	my $cur = $stream;
10176c72ffaaSAndy Whitcroft
1018c2fdda0dSAndy Whitcroft	print "$stream\n" if ($dbg_values > 1);
10196c72ffaaSAndy Whitcroft
10206c72ffaaSAndy Whitcroft	while (length($cur)) {
1021773647a0SAndy Whitcroft		@av_paren_type = ('E') if ($#av_paren_type < 0);
1022cf655043SAndy Whitcroft		print " <" . join('', @av_paren_type) .
1023171ae1a4SAndy Whitcroft				"> <$type> <$av_pending>" if ($dbg_values > 1);
10246c72ffaaSAndy Whitcroft		if ($cur =~ /^(\s+)/o) {
1025c2fdda0dSAndy Whitcroft			print "WS($1)\n" if ($dbg_values > 1);
1026c2fdda0dSAndy Whitcroft			if ($1 =~ /\n/ && $av_preprocessor) {
1027cf655043SAndy Whitcroft				$type = pop(@av_paren_type);
1028c2fdda0dSAndy Whitcroft				$av_preprocessor = 0;
10296c72ffaaSAndy Whitcroft			}
10306c72ffaaSAndy Whitcroft
1031c023e473SFlorian Mickler		} elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
10329446ef56SAndy Whitcroft			print "CAST($1)\n" if ($dbg_values > 1);
10339446ef56SAndy Whitcroft			push(@av_paren_type, $type);
10349446ef56SAndy Whitcroft			$type = 'C';
10359446ef56SAndy Whitcroft
1036e91b6e26SAndy Whitcroft		} elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
1037c2fdda0dSAndy Whitcroft			print "DECLARE($1)\n" if ($dbg_values > 1);
10386c72ffaaSAndy Whitcroft			$type = 'T';
10396c72ffaaSAndy Whitcroft
1040389a2fe5SAndy Whitcroft		} elsif ($cur =~ /^($Modifier)\s*/) {
1041389a2fe5SAndy Whitcroft			print "MODIFIER($1)\n" if ($dbg_values > 1);
1042389a2fe5SAndy Whitcroft			$type = 'T';
1043389a2fe5SAndy Whitcroft
1044c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
1045171ae1a4SAndy Whitcroft			print "DEFINE($1,$2)\n" if ($dbg_values > 1);
1046c2fdda0dSAndy Whitcroft			$av_preprocessor = 1;
1047171ae1a4SAndy Whitcroft			push(@av_paren_type, $type);
1048171ae1a4SAndy Whitcroft			if ($2 ne '') {
1049cf655043SAndy Whitcroft				$av_pending = 'N';
1050171ae1a4SAndy Whitcroft			}
1051171ae1a4SAndy Whitcroft			$type = 'E';
1052171ae1a4SAndy Whitcroft
1053c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
1054171ae1a4SAndy Whitcroft			print "UNDEF($1)\n" if ($dbg_values > 1);
1055171ae1a4SAndy Whitcroft			$av_preprocessor = 1;
1056171ae1a4SAndy Whitcroft			push(@av_paren_type, $type);
10576c72ffaaSAndy Whitcroft
1058c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
1059cf655043SAndy Whitcroft			print "PRE_START($1)\n" if ($dbg_values > 1);
1060c2fdda0dSAndy Whitcroft			$av_preprocessor = 1;
1061cf655043SAndy Whitcroft
1062cf655043SAndy Whitcroft			push(@av_paren_type, $type);
1063cf655043SAndy Whitcroft			push(@av_paren_type, $type);
1064171ae1a4SAndy Whitcroft			$type = 'E';
1065cf655043SAndy Whitcroft
1066c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
1067cf655043SAndy Whitcroft			print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1068cf655043SAndy Whitcroft			$av_preprocessor = 1;
1069cf655043SAndy Whitcroft
1070cf655043SAndy Whitcroft			push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1071cf655043SAndy Whitcroft
1072171ae1a4SAndy Whitcroft			$type = 'E';
1073cf655043SAndy Whitcroft
1074c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:endif))/o) {
1075cf655043SAndy Whitcroft			print "PRE_END($1)\n" if ($dbg_values > 1);
1076cf655043SAndy Whitcroft
1077cf655043SAndy Whitcroft			$av_preprocessor = 1;
1078cf655043SAndy Whitcroft
1079cf655043SAndy Whitcroft			# Assume all arms of the conditional end as this
1080cf655043SAndy Whitcroft			# one does, and continue as if the #endif was not here.
1081cf655043SAndy Whitcroft			pop(@av_paren_type);
1082cf655043SAndy Whitcroft			push(@av_paren_type, $type);
1083171ae1a4SAndy Whitcroft			$type = 'E';
10846c72ffaaSAndy Whitcroft
10856c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\\\n)/o) {
1086c2fdda0dSAndy Whitcroft			print "PRECONT($1)\n" if ($dbg_values > 1);
10876c72ffaaSAndy Whitcroft
1088171ae1a4SAndy Whitcroft		} elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1089171ae1a4SAndy Whitcroft			print "ATTR($1)\n" if ($dbg_values > 1);
1090171ae1a4SAndy Whitcroft			$av_pending = $type;
1091171ae1a4SAndy Whitcroft			$type = 'N';
1092171ae1a4SAndy Whitcroft
10936c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
1094c2fdda0dSAndy Whitcroft			print "SIZEOF($1)\n" if ($dbg_values > 1);
10956c72ffaaSAndy Whitcroft			if (defined $2) {
1096cf655043SAndy Whitcroft				$av_pending = 'V';
10976c72ffaaSAndy Whitcroft			}
10986c72ffaaSAndy Whitcroft			$type = 'N';
10996c72ffaaSAndy Whitcroft
110014b111c1SAndy Whitcroft		} elsif ($cur =~ /^(if|while|for)\b/o) {
1101c2fdda0dSAndy Whitcroft			print "COND($1)\n" if ($dbg_values > 1);
110214b111c1SAndy Whitcroft			$av_pending = 'E';
11036c72ffaaSAndy Whitcroft			$type = 'N';
11046c72ffaaSAndy Whitcroft
11051f65f947SAndy Whitcroft		} elsif ($cur =~/^(case)/o) {
11061f65f947SAndy Whitcroft			print "CASE($1)\n" if ($dbg_values > 1);
11071f65f947SAndy Whitcroft			$av_pend_colon = 'C';
11081f65f947SAndy Whitcroft			$type = 'N';
11091f65f947SAndy Whitcroft
111014b111c1SAndy Whitcroft		} elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
1111c2fdda0dSAndy Whitcroft			print "KEYWORD($1)\n" if ($dbg_values > 1);
11126c72ffaaSAndy Whitcroft			$type = 'N';
11136c72ffaaSAndy Whitcroft
11146c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\()/o) {
1115c2fdda0dSAndy Whitcroft			print "PAREN('$1')\n" if ($dbg_values > 1);
1116cf655043SAndy Whitcroft			push(@av_paren_type, $av_pending);
1117cf655043SAndy Whitcroft			$av_pending = '_';
11186c72ffaaSAndy Whitcroft			$type = 'N';
11196c72ffaaSAndy Whitcroft
11206c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\))/o) {
1121cf655043SAndy Whitcroft			my $new_type = pop(@av_paren_type);
1122cf655043SAndy Whitcroft			if ($new_type ne '_') {
1123cf655043SAndy Whitcroft				$type = $new_type;
1124c2fdda0dSAndy Whitcroft				print "PAREN('$1') -> $type\n"
1125c2fdda0dSAndy Whitcroft							if ($dbg_values > 1);
11266c72ffaaSAndy Whitcroft			} else {
1127c2fdda0dSAndy Whitcroft				print "PAREN('$1')\n" if ($dbg_values > 1);
11286c72ffaaSAndy Whitcroft			}
11296c72ffaaSAndy Whitcroft
1130c8cb2ca3SAndy Whitcroft		} elsif ($cur =~ /^($Ident)\s*\(/o) {
1131c2fdda0dSAndy Whitcroft			print "FUNC($1)\n" if ($dbg_values > 1);
1132c8cb2ca3SAndy Whitcroft			$type = 'V';
1133cf655043SAndy Whitcroft			$av_pending = 'V';
11346c72ffaaSAndy Whitcroft
11358e761b04SAndy Whitcroft		} elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
11368e761b04SAndy Whitcroft			if (defined $2 && $type eq 'C' || $type eq 'T') {
11371f65f947SAndy Whitcroft				$av_pend_colon = 'B';
11388e761b04SAndy Whitcroft			} elsif ($type eq 'E') {
11398e761b04SAndy Whitcroft				$av_pend_colon = 'L';
11401f65f947SAndy Whitcroft			}
11411f65f947SAndy Whitcroft			print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
11421f65f947SAndy Whitcroft			$type = 'V';
11431f65f947SAndy Whitcroft
11446c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Ident|$Constant)/o) {
1145c2fdda0dSAndy Whitcroft			print "IDENT($1)\n" if ($dbg_values > 1);
11466c72ffaaSAndy Whitcroft			$type = 'V';
11476c72ffaaSAndy Whitcroft
11486c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Assignment)/o) {
1149c2fdda0dSAndy Whitcroft			print "ASSIGN($1)\n" if ($dbg_values > 1);
11506c72ffaaSAndy Whitcroft			$type = 'N';
11516c72ffaaSAndy Whitcroft
1152cf655043SAndy Whitcroft		} elsif ($cur =~/^(;|{|})/) {
1153c2fdda0dSAndy Whitcroft			print "END($1)\n" if ($dbg_values > 1);
115413214adfSAndy Whitcroft			$type = 'E';
11551f65f947SAndy Whitcroft			$av_pend_colon = 'O';
115613214adfSAndy Whitcroft
11578e761b04SAndy Whitcroft		} elsif ($cur =~/^(,)/) {
11588e761b04SAndy Whitcroft			print "COMMA($1)\n" if ($dbg_values > 1);
11598e761b04SAndy Whitcroft			$type = 'C';
11608e761b04SAndy Whitcroft
11611f65f947SAndy Whitcroft		} elsif ($cur =~ /^(\?)/o) {
11621f65f947SAndy Whitcroft			print "QUESTION($1)\n" if ($dbg_values > 1);
11631f65f947SAndy Whitcroft			$type = 'N';
11641f65f947SAndy Whitcroft
11651f65f947SAndy Whitcroft		} elsif ($cur =~ /^(:)/o) {
11661f65f947SAndy Whitcroft			print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
11671f65f947SAndy Whitcroft
11681f65f947SAndy Whitcroft			substr($var, length($res), 1, $av_pend_colon);
11691f65f947SAndy Whitcroft			if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
11701f65f947SAndy Whitcroft				$type = 'E';
11711f65f947SAndy Whitcroft			} else {
11721f65f947SAndy Whitcroft				$type = 'N';
11731f65f947SAndy Whitcroft			}
11741f65f947SAndy Whitcroft			$av_pend_colon = 'O';
11751f65f947SAndy Whitcroft
11768e761b04SAndy Whitcroft		} elsif ($cur =~ /^(\[)/o) {
117713214adfSAndy Whitcroft			print "CLOSE($1)\n" if ($dbg_values > 1);
11786c72ffaaSAndy Whitcroft			$type = 'N';
11796c72ffaaSAndy Whitcroft
11800d413866SAndy Whitcroft		} elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
118174048ed8SAndy Whitcroft			my $variant;
118274048ed8SAndy Whitcroft
118374048ed8SAndy Whitcroft			print "OPV($1)\n" if ($dbg_values > 1);
118474048ed8SAndy Whitcroft			if ($type eq 'V') {
118574048ed8SAndy Whitcroft				$variant = 'B';
118674048ed8SAndy Whitcroft			} else {
118774048ed8SAndy Whitcroft				$variant = 'U';
118874048ed8SAndy Whitcroft			}
118974048ed8SAndy Whitcroft
119074048ed8SAndy Whitcroft			substr($var, length($res), 1, $variant);
119174048ed8SAndy Whitcroft			$type = 'N';
119274048ed8SAndy Whitcroft
11936c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Operators)/o) {
1194c2fdda0dSAndy Whitcroft			print "OP($1)\n" if ($dbg_values > 1);
11956c72ffaaSAndy Whitcroft			if ($1 ne '++' && $1 ne '--') {
11966c72ffaaSAndy Whitcroft				$type = 'N';
11976c72ffaaSAndy Whitcroft			}
11986c72ffaaSAndy Whitcroft
11996c72ffaaSAndy Whitcroft		} elsif ($cur =~ /(^.)/o) {
1200c2fdda0dSAndy Whitcroft			print "C($1)\n" if ($dbg_values > 1);
12016c72ffaaSAndy Whitcroft		}
12026c72ffaaSAndy Whitcroft		if (defined $1) {
12036c72ffaaSAndy Whitcroft			$cur = substr($cur, length($1));
12046c72ffaaSAndy Whitcroft			$res .= $type x length($1);
12056c72ffaaSAndy Whitcroft		}
12066c72ffaaSAndy Whitcroft	}
12076c72ffaaSAndy Whitcroft
12081f65f947SAndy Whitcroft	return ($res, $var);
12096c72ffaaSAndy Whitcroft}
12106c72ffaaSAndy Whitcroft
12118905a67cSAndy Whitcroftsub possible {
121213214adfSAndy Whitcroft	my ($possible, $line) = @_;
12139a974fdbSAndy Whitcroft	my $notPermitted = qr{(?:
12140776e594SAndy Whitcroft		^(?:
12150776e594SAndy Whitcroft			$Modifier|
12160776e594SAndy Whitcroft			$Storage|
12170776e594SAndy Whitcroft			$Type|
12189a974fdbSAndy Whitcroft			DEFINE_\S+
12199a974fdbSAndy Whitcroft		)$|
12209a974fdbSAndy Whitcroft		^(?:
12210776e594SAndy Whitcroft			goto|
12220776e594SAndy Whitcroft			return|
12230776e594SAndy Whitcroft			case|
12240776e594SAndy Whitcroft			else|
12250776e594SAndy Whitcroft			asm|__asm__|
12260776e594SAndy Whitcroft			do
12279a974fdbSAndy Whitcroft		)(?:\s|$)|
12280776e594SAndy Whitcroft		^(?:typedef|struct|enum)\b
12299a974fdbSAndy Whitcroft	    )}x;
12309a974fdbSAndy Whitcroft	warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
12319a974fdbSAndy Whitcroft	if ($possible !~ $notPermitted) {
1232c45dcabdSAndy Whitcroft		# Check for modifiers.
1233c45dcabdSAndy Whitcroft		$possible =~ s/\s*$Storage\s*//g;
1234c45dcabdSAndy Whitcroft		$possible =~ s/\s*$Sparse\s*//g;
1235c45dcabdSAndy Whitcroft		if ($possible =~ /^\s*$/) {
1236c45dcabdSAndy Whitcroft
1237c45dcabdSAndy Whitcroft		} elsif ($possible =~ /\s/) {
1238c45dcabdSAndy Whitcroft			$possible =~ s/\s*$Type\s*//g;
1239d2506586SAndy Whitcroft			for my $modifier (split(' ', $possible)) {
12409a974fdbSAndy Whitcroft				if ($modifier !~ $notPermitted) {
1241d2506586SAndy Whitcroft					warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1242d2506586SAndy Whitcroft					push(@modifierList, $modifier);
1243d2506586SAndy Whitcroft				}
12449a974fdbSAndy Whitcroft			}
1245c45dcabdSAndy Whitcroft
1246c45dcabdSAndy Whitcroft		} else {
124713214adfSAndy Whitcroft			warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
12488905a67cSAndy Whitcroft			push(@typeList, $possible);
1249c45dcabdSAndy Whitcroft		}
12508905a67cSAndy Whitcroft		build_types();
12510776e594SAndy Whitcroft	} else {
12520776e594SAndy Whitcroft		warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
12538905a67cSAndy Whitcroft	}
12548905a67cSAndy Whitcroft}
12558905a67cSAndy Whitcroft
12566c72ffaaSAndy Whitcroftmy $prefix = '';
12576c72ffaaSAndy Whitcroft
1258000d1cc1SJoe Perchessub show_type {
1259000d1cc1SJoe Perches       return !defined $ignore_type{$_[0]};
1260000d1cc1SJoe Perches}
1261000d1cc1SJoe Perches
1262f0a594c1SAndy Whitcroftsub report {
1263000d1cc1SJoe Perches	if (!show_type($_[1]) ||
1264000d1cc1SJoe Perches	    (defined $tst_only && $_[2] !~ /\Q$tst_only\E/)) {
1265773647a0SAndy Whitcroft		return 0;
1266773647a0SAndy Whitcroft	}
1267000d1cc1SJoe Perches	my $line;
1268000d1cc1SJoe Perches	if ($show_types) {
1269000d1cc1SJoe Perches		$line = "$prefix$_[0]:$_[1]: $_[2]\n";
1270000d1cc1SJoe Perches	} else {
1271000d1cc1SJoe Perches		$line = "$prefix$_[0]: $_[2]\n";
1272000d1cc1SJoe Perches	}
12738905a67cSAndy Whitcroft	$line = (split('\n', $line))[0] . "\n" if ($terse);
12748905a67cSAndy Whitcroft
127513214adfSAndy Whitcroft	push(our @report, $line);
1276773647a0SAndy Whitcroft
1277773647a0SAndy Whitcroft	return 1;
1278f0a594c1SAndy Whitcroft}
1279f0a594c1SAndy Whitcroftsub report_dump {
128013214adfSAndy Whitcroft	our @report;
1281f0a594c1SAndy Whitcroft}
1282000d1cc1SJoe Perches
1283de7d4f0eSAndy Whitcroftsub ERROR {
1284000d1cc1SJoe Perches	if (report("ERROR", $_[0], $_[1])) {
1285de7d4f0eSAndy Whitcroft		our $clean = 0;
12866c72ffaaSAndy Whitcroft		our $cnt_error++;
1287de7d4f0eSAndy Whitcroft	}
1288773647a0SAndy Whitcroft}
1289de7d4f0eSAndy Whitcroftsub WARN {
1290000d1cc1SJoe Perches	if (report("WARNING", $_[0], $_[1])) {
1291de7d4f0eSAndy Whitcroft		our $clean = 0;
12926c72ffaaSAndy Whitcroft		our $cnt_warn++;
1293de7d4f0eSAndy Whitcroft	}
1294773647a0SAndy Whitcroft}
1295de7d4f0eSAndy Whitcroftsub CHK {
1296000d1cc1SJoe Perches	if ($check && report("CHECK", $_[0], $_[1])) {
1297de7d4f0eSAndy Whitcroft		our $clean = 0;
12986c72ffaaSAndy Whitcroft		our $cnt_chk++;
12996c72ffaaSAndy Whitcroft	}
1300de7d4f0eSAndy Whitcroft}
1301de7d4f0eSAndy Whitcroft
13026ecd9674SAndy Whitcroftsub check_absolute_file {
13036ecd9674SAndy Whitcroft	my ($absolute, $herecurr) = @_;
13046ecd9674SAndy Whitcroft	my $file = $absolute;
13056ecd9674SAndy Whitcroft
13066ecd9674SAndy Whitcroft	##print "absolute<$absolute>\n";
13076ecd9674SAndy Whitcroft
13086ecd9674SAndy Whitcroft	# See if any suffix of this path is a path within the tree.
13096ecd9674SAndy Whitcroft	while ($file =~ s@^[^/]*/@@) {
13106ecd9674SAndy Whitcroft		if (-f "$root/$file") {
13116ecd9674SAndy Whitcroft			##print "file<$file>\n";
13126ecd9674SAndy Whitcroft			last;
13136ecd9674SAndy Whitcroft		}
13146ecd9674SAndy Whitcroft	}
13156ecd9674SAndy Whitcroft	if (! -f _)  {
13166ecd9674SAndy Whitcroft		return 0;
13176ecd9674SAndy Whitcroft	}
13186ecd9674SAndy Whitcroft
13196ecd9674SAndy Whitcroft	# It is, so see if the prefix is acceptable.
13206ecd9674SAndy Whitcroft	my $prefix = $absolute;
13216ecd9674SAndy Whitcroft	substr($prefix, -length($file)) = '';
13226ecd9674SAndy Whitcroft
13236ecd9674SAndy Whitcroft	##print "prefix<$prefix>\n";
13246ecd9674SAndy Whitcroft	if ($prefix ne ".../") {
1325000d1cc1SJoe Perches		WARN("USE_RELATIVE_PATH",
1326000d1cc1SJoe Perches		     "use relative pathname instead of absolute in changelog text\n" . $herecurr);
13276ecd9674SAndy Whitcroft	}
13286ecd9674SAndy Whitcroft}
13296ecd9674SAndy Whitcroft
13300a920b5bSAndy Whitcroftsub process {
13310a920b5bSAndy Whitcroft	my $filename = shift;
13320a920b5bSAndy Whitcroft
13330a920b5bSAndy Whitcroft	my $linenr=0;
13340a920b5bSAndy Whitcroft	my $prevline="";
1335c2fdda0dSAndy Whitcroft	my $prevrawline="";
13360a920b5bSAndy Whitcroft	my $stashline="";
1337c2fdda0dSAndy Whitcroft	my $stashrawline="";
13380a920b5bSAndy Whitcroft
13394a0df2efSAndy Whitcroft	my $length;
13400a920b5bSAndy Whitcroft	my $indent;
13410a920b5bSAndy Whitcroft	my $previndent=0;
13420a920b5bSAndy Whitcroft	my $stashindent=0;
13430a920b5bSAndy Whitcroft
1344de7d4f0eSAndy Whitcroft	our $clean = 1;
13450a920b5bSAndy Whitcroft	my $signoff = 0;
13460a920b5bSAndy Whitcroft	my $is_patch = 0;
13470a920b5bSAndy Whitcroft
134815662b3eSJoe Perches	my $in_header_lines = 1;
134915662b3eSJoe Perches	my $in_commit_log = 0;		#Scanning lines before patch
135015662b3eSJoe Perches
135113214adfSAndy Whitcroft	our @report = ();
13526c72ffaaSAndy Whitcroft	our $cnt_lines = 0;
13536c72ffaaSAndy Whitcroft	our $cnt_error = 0;
13546c72ffaaSAndy Whitcroft	our $cnt_warn = 0;
13556c72ffaaSAndy Whitcroft	our $cnt_chk = 0;
13566c72ffaaSAndy Whitcroft
13570a920b5bSAndy Whitcroft	# Trace the real file/line as we go.
13580a920b5bSAndy Whitcroft	my $realfile = '';
13590a920b5bSAndy Whitcroft	my $realline = 0;
13600a920b5bSAndy Whitcroft	my $realcnt = 0;
13610a920b5bSAndy Whitcroft	my $here = '';
13620a920b5bSAndy Whitcroft	my $in_comment = 0;
1363c2fdda0dSAndy Whitcroft	my $comment_edge = 0;
13640a920b5bSAndy Whitcroft	my $first_line = 0;
13651e855726SWolfram Sang	my $p1_prefix = '';
13660a920b5bSAndy Whitcroft
136713214adfSAndy Whitcroft	my $prev_values = 'E';
136813214adfSAndy Whitcroft
136913214adfSAndy Whitcroft	# suppression flags
1370773647a0SAndy Whitcroft	my %suppress_ifbraces;
1371170d3a22SAndy Whitcroft	my %suppress_whiletrailers;
13722b474a1aSAndy Whitcroft	my %suppress_export;
1373653d4876SAndy Whitcroft
1374c2fdda0dSAndy Whitcroft	# Pre-scan the patch sanitizing the lines.
1375de7d4f0eSAndy Whitcroft	# Pre-scan the patch looking for any __setup documentation.
1376c2fdda0dSAndy Whitcroft	#
1377de7d4f0eSAndy Whitcroft	my @setup_docs = ();
1378de7d4f0eSAndy Whitcroft	my $setup_docs = 0;
1379773647a0SAndy Whitcroft
1380773647a0SAndy Whitcroft	sanitise_line_reset();
1381c2fdda0dSAndy Whitcroft	my $line;
1382c2fdda0dSAndy Whitcroft	foreach my $rawline (@rawlines) {
1383773647a0SAndy Whitcroft		$linenr++;
1384773647a0SAndy Whitcroft		$line = $rawline;
1385c2fdda0dSAndy Whitcroft
1386773647a0SAndy Whitcroft		if ($rawline=~/^\+\+\+\s+(\S+)/) {
1387de7d4f0eSAndy Whitcroft			$setup_docs = 0;
1388de7d4f0eSAndy Whitcroft			if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
1389de7d4f0eSAndy Whitcroft				$setup_docs = 1;
1390de7d4f0eSAndy Whitcroft			}
1391773647a0SAndy Whitcroft			#next;
1392de7d4f0eSAndy Whitcroft		}
1393773647a0SAndy Whitcroft		if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1394773647a0SAndy Whitcroft			$realline=$1-1;
1395773647a0SAndy Whitcroft			if (defined $2) {
1396773647a0SAndy Whitcroft				$realcnt=$3+1;
1397773647a0SAndy Whitcroft			} else {
1398773647a0SAndy Whitcroft				$realcnt=1+1;
1399773647a0SAndy Whitcroft			}
1400c45dcabdSAndy Whitcroft			$in_comment = 0;
1401773647a0SAndy Whitcroft
1402773647a0SAndy Whitcroft			# Guestimate if this is a continuing comment.  Run
1403773647a0SAndy Whitcroft			# the context looking for a comment "edge".  If this
1404773647a0SAndy Whitcroft			# edge is a close comment then we must be in a comment
1405773647a0SAndy Whitcroft			# at context start.
1406773647a0SAndy Whitcroft			my $edge;
140701fa9147SAndy Whitcroft			my $cnt = $realcnt;
140801fa9147SAndy Whitcroft			for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
140901fa9147SAndy Whitcroft				next if (defined $rawlines[$ln - 1] &&
141001fa9147SAndy Whitcroft					 $rawlines[$ln - 1] =~ /^-/);
141101fa9147SAndy Whitcroft				$cnt--;
141201fa9147SAndy Whitcroft				#print "RAW<$rawlines[$ln - 1]>\n";
1413721c1cb6SAndy Whitcroft				last if (!defined $rawlines[$ln - 1]);
1414fae17daeSAndy Whitcroft				if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
1415fae17daeSAndy Whitcroft				    $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
1416fae17daeSAndy Whitcroft					($edge) = $1;
1417fae17daeSAndy Whitcroft					last;
1418fae17daeSAndy Whitcroft				}
1419773647a0SAndy Whitcroft			}
1420773647a0SAndy Whitcroft			if (defined $edge && $edge eq '*/') {
1421773647a0SAndy Whitcroft				$in_comment = 1;
1422773647a0SAndy Whitcroft			}
1423773647a0SAndy Whitcroft
1424773647a0SAndy Whitcroft			# Guestimate if this is a continuing comment.  If this
1425773647a0SAndy Whitcroft			# is the start of a diff block and this line starts
1426773647a0SAndy Whitcroft			# ' *' then it is very likely a comment.
1427773647a0SAndy Whitcroft			if (!defined $edge &&
142883242e0cSAndy Whitcroft			    $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
1429773647a0SAndy Whitcroft			{
1430773647a0SAndy Whitcroft				$in_comment = 1;
1431773647a0SAndy Whitcroft			}
1432773647a0SAndy Whitcroft
1433773647a0SAndy Whitcroft			##print "COMMENT:$in_comment edge<$edge> $rawline\n";
1434773647a0SAndy Whitcroft			sanitise_line_reset($in_comment);
1435773647a0SAndy Whitcroft
1436171ae1a4SAndy Whitcroft		} elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
1437773647a0SAndy Whitcroft			# Standardise the strings and chars within the input to
1438171ae1a4SAndy Whitcroft			# simplify matching -- only bother with positive lines.
1439773647a0SAndy Whitcroft			$line = sanitise_line($rawline);
1440773647a0SAndy Whitcroft		}
1441773647a0SAndy Whitcroft		push(@lines, $line);
1442773647a0SAndy Whitcroft
1443773647a0SAndy Whitcroft		if ($realcnt > 1) {
1444773647a0SAndy Whitcroft			$realcnt-- if ($line =~ /^(?:\+| |$)/);
1445773647a0SAndy Whitcroft		} else {
1446773647a0SAndy Whitcroft			$realcnt = 0;
1447773647a0SAndy Whitcroft		}
1448773647a0SAndy Whitcroft
1449773647a0SAndy Whitcroft		#print "==>$rawline\n";
1450773647a0SAndy Whitcroft		#print "-->$line\n";
1451de7d4f0eSAndy Whitcroft
1452de7d4f0eSAndy Whitcroft		if ($setup_docs && $line =~ /^\+/) {
1453de7d4f0eSAndy Whitcroft			push(@setup_docs, $line);
1454de7d4f0eSAndy Whitcroft		}
1455de7d4f0eSAndy Whitcroft	}
1456de7d4f0eSAndy Whitcroft
14576c72ffaaSAndy Whitcroft	$prefix = '';
14586c72ffaaSAndy Whitcroft
1459773647a0SAndy Whitcroft	$realcnt = 0;
1460773647a0SAndy Whitcroft	$linenr = 0;
14610a920b5bSAndy Whitcroft	foreach my $line (@lines) {
14620a920b5bSAndy Whitcroft		$linenr++;
14630a920b5bSAndy Whitcroft
1464c2fdda0dSAndy Whitcroft		my $rawline = $rawlines[$linenr - 1];
14656c72ffaaSAndy Whitcroft
14660a920b5bSAndy Whitcroft#extract the line range in the file after the patch is applied
14676c72ffaaSAndy Whitcroft		if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
14680a920b5bSAndy Whitcroft			$is_patch = 1;
14694a0df2efSAndy Whitcroft			$first_line = $linenr + 1;
14700a920b5bSAndy Whitcroft			$realline=$1-1;
14710a920b5bSAndy Whitcroft			if (defined $2) {
14720a920b5bSAndy Whitcroft				$realcnt=$3+1;
14730a920b5bSAndy Whitcroft			} else {
14740a920b5bSAndy Whitcroft				$realcnt=1+1;
14750a920b5bSAndy Whitcroft			}
1476c2fdda0dSAndy Whitcroft			annotate_reset();
147713214adfSAndy Whitcroft			$prev_values = 'E';
147813214adfSAndy Whitcroft
1479773647a0SAndy Whitcroft			%suppress_ifbraces = ();
1480170d3a22SAndy Whitcroft			%suppress_whiletrailers = ();
14812b474a1aSAndy Whitcroft			%suppress_export = ();
14820a920b5bSAndy Whitcroft			next;
14830a920b5bSAndy Whitcroft
14844a0df2efSAndy Whitcroft# track the line number as we move through the hunk, note that
14854a0df2efSAndy Whitcroft# new versions of GNU diff omit the leading space on completely
14864a0df2efSAndy Whitcroft# blank context lines so we need to count that too.
1487773647a0SAndy Whitcroft		} elsif ($line =~ /^( |\+|$)/) {
14880a920b5bSAndy Whitcroft			$realline++;
1489d8aaf121SAndy Whitcroft			$realcnt-- if ($realcnt != 0);
14900a920b5bSAndy Whitcroft
14914a0df2efSAndy Whitcroft			# Measure the line length and indent.
1492c2fdda0dSAndy Whitcroft			($length, $indent) = line_stats($rawline);
14930a920b5bSAndy Whitcroft
14940a920b5bSAndy Whitcroft			# Track the previous line.
14950a920b5bSAndy Whitcroft			($prevline, $stashline) = ($stashline, $line);
14960a920b5bSAndy Whitcroft			($previndent, $stashindent) = ($stashindent, $indent);
1497c2fdda0dSAndy Whitcroft			($prevrawline, $stashrawline) = ($stashrawline, $rawline);
1498c2fdda0dSAndy Whitcroft
1499773647a0SAndy Whitcroft			#warn "line<$line>\n";
15006c72ffaaSAndy Whitcroft
1501d8aaf121SAndy Whitcroft		} elsif ($realcnt == 1) {
1502d8aaf121SAndy Whitcroft			$realcnt--;
15030a920b5bSAndy Whitcroft		}
15040a920b5bSAndy Whitcroft
1505cc77cdcaSAndy Whitcroft		my $hunk_line = ($realcnt != 0);
1506cc77cdcaSAndy Whitcroft
15070a920b5bSAndy Whitcroft#make up the handle for any error we report on this line
1508773647a0SAndy Whitcroft		$prefix = "$filename:$realline: " if ($emacs && $file);
1509773647a0SAndy Whitcroft		$prefix = "$filename:$linenr: " if ($emacs && !$file);
1510773647a0SAndy Whitcroft
15116c72ffaaSAndy Whitcroft		$here = "#$linenr: " if (!$file);
15126c72ffaaSAndy Whitcroft		$here = "#$realline: " if ($file);
1513773647a0SAndy Whitcroft
1514773647a0SAndy Whitcroft		# extract the filename as it passes
15153bf9a009SRabin Vincent		if ($line =~ /^diff --git.*?(\S+)$/) {
15163bf9a009SRabin Vincent			$realfile = $1;
15173bf9a009SRabin Vincent			$realfile =~ s@^([^/]*)/@@;
1518270c49a0SJoe Perches			$in_commit_log = 0;
15193bf9a009SRabin Vincent		} elsif ($line =~ /^\+\+\+\s+(\S+)/) {
1520773647a0SAndy Whitcroft			$realfile = $1;
15211e855726SWolfram Sang			$realfile =~ s@^([^/]*)/@@;
1522270c49a0SJoe Perches			$in_commit_log = 0;
15231e855726SWolfram Sang
15241e855726SWolfram Sang			$p1_prefix = $1;
1525e2f7aa4bSAndy Whitcroft			if (!$file && $tree && $p1_prefix ne '' &&
1526e2f7aa4bSAndy Whitcroft			    -e "$root/$p1_prefix") {
1527000d1cc1SJoe Perches				WARN("PATCH_PREFIX",
1528000d1cc1SJoe Perches				     "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
15291e855726SWolfram Sang			}
1530773647a0SAndy Whitcroft
1531c1ab3326SAndy Whitcroft			if ($realfile =~ m@^include/asm/@) {
1532000d1cc1SJoe Perches				ERROR("MODIFIED_INCLUDE_ASM",
1533000d1cc1SJoe Perches				      "do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
1534773647a0SAndy Whitcroft			}
1535773647a0SAndy Whitcroft			next;
1536773647a0SAndy Whitcroft		}
1537773647a0SAndy Whitcroft
1538389834b6SRandy Dunlap		$here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
15390a920b5bSAndy Whitcroft
1540c2fdda0dSAndy Whitcroft		my $hereline = "$here\n$rawline\n";
1541c2fdda0dSAndy Whitcroft		my $herecurr = "$here\n$rawline\n";
1542c2fdda0dSAndy Whitcroft		my $hereprev = "$here\n$prevrawline\n$rawline\n";
15430a920b5bSAndy Whitcroft
15446c72ffaaSAndy Whitcroft		$cnt_lines++ if ($realcnt != 0);
15456c72ffaaSAndy Whitcroft
15463bf9a009SRabin Vincent# Check for incorrect file permissions
15473bf9a009SRabin Vincent		if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
15483bf9a009SRabin Vincent			my $permhere = $here . "FILE: $realfile\n";
15493bf9a009SRabin Vincent			if ($realfile =~ /(Makefile|Kconfig|\.c|\.h|\.S|\.tmpl)$/) {
1550000d1cc1SJoe Perches				ERROR("EXECUTE_PERMISSIONS",
1551000d1cc1SJoe Perches				      "do not set execute permissions for source files\n" . $permhere);
15523bf9a009SRabin Vincent			}
15533bf9a009SRabin Vincent		}
15543bf9a009SRabin Vincent
155520112475SJoe Perches# Check the patch for a signoff:
1556d8aaf121SAndy Whitcroft		if ($line =~ /^\s*signed-off-by:/i) {
15574a0df2efSAndy Whitcroft			$signoff++;
155815662b3eSJoe Perches			$in_commit_log = 0;
15590a920b5bSAndy Whitcroft		}
156020112475SJoe Perches
156120112475SJoe Perches# Check signature styles
1562270c49a0SJoe Perches		if (!$in_header_lines &&
1563270c49a0SJoe Perches		    $line =~ /^(\s*)($signature_tags)(\s*)(.*)/) {
156420112475SJoe Perches			my $space_before = $1;
156520112475SJoe Perches			my $sign_off = $2;
156620112475SJoe Perches			my $space_after = $3;
156720112475SJoe Perches			my $email = $4;
156820112475SJoe Perches			my $ucfirst_sign_off = ucfirst(lc($sign_off));
156920112475SJoe Perches
157020112475SJoe Perches			if (defined $space_before && $space_before ne "") {
1571000d1cc1SJoe Perches				WARN("BAD_SIGN_OFF",
1572000d1cc1SJoe Perches				     "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr);
157320112475SJoe Perches			}
157420112475SJoe Perches			if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
1575000d1cc1SJoe Perches				WARN("BAD_SIGN_OFF",
1576000d1cc1SJoe Perches				     "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr);
157720112475SJoe Perches			}
157820112475SJoe Perches			if (!defined $space_after || $space_after ne " ") {
1579000d1cc1SJoe Perches				WARN("BAD_SIGN_OFF",
1580000d1cc1SJoe Perches				     "Use a single space after $ucfirst_sign_off\n" . $herecurr);
158120112475SJoe Perches			}
158220112475SJoe Perches
158320112475SJoe Perches			my ($email_name, $email_address, $comment) = parse_email($email);
158420112475SJoe Perches			my $suggested_email = format_email(($email_name, $email_address));
158520112475SJoe Perches			if ($suggested_email eq "") {
1586000d1cc1SJoe Perches				ERROR("BAD_SIGN_OFF",
1587000d1cc1SJoe Perches				      "Unrecognized email address: '$email'\n" . $herecurr);
158820112475SJoe Perches			} else {
158920112475SJoe Perches				my $dequoted = $suggested_email;
159020112475SJoe Perches				$dequoted =~ s/^"//;
159120112475SJoe Perches				$dequoted =~ s/" </ </;
159220112475SJoe Perches				# Don't force email to have quotes
159320112475SJoe Perches				# Allow just an angle bracketed address
159420112475SJoe Perches				if ("$dequoted$comment" ne $email &&
159520112475SJoe Perches				    "<$email_address>$comment" ne $email &&
159620112475SJoe Perches				    "$suggested_email$comment" ne $email) {
1597000d1cc1SJoe Perches					WARN("BAD_SIGN_OFF",
1598000d1cc1SJoe Perches					     "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
159920112475SJoe Perches				}
16000a920b5bSAndy Whitcroft			}
16010a920b5bSAndy Whitcroft		}
16020a920b5bSAndy Whitcroft
160300df344fSAndy Whitcroft# Check for wrappage within a valid hunk of the file
16048905a67cSAndy Whitcroft		if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
1605000d1cc1SJoe Perches			ERROR("CORRUPTED_PATCH",
1606000d1cc1SJoe Perches			      "patch seems to be corrupt (line wrapped?)\n" .
16076c72ffaaSAndy Whitcroft				$herecurr) if (!$emitted_corrupt++);
1608de7d4f0eSAndy Whitcroft		}
1609de7d4f0eSAndy Whitcroft
16106ecd9674SAndy Whitcroft# Check for absolute kernel paths.
16116ecd9674SAndy Whitcroft		if ($tree) {
16126ecd9674SAndy Whitcroft			while ($line =~ m{(?:^|\s)(/\S*)}g) {
16136ecd9674SAndy Whitcroft				my $file = $1;
16146ecd9674SAndy Whitcroft
16156ecd9674SAndy Whitcroft				if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
16166ecd9674SAndy Whitcroft				    check_absolute_file($1, $herecurr)) {
16176ecd9674SAndy Whitcroft					#
16186ecd9674SAndy Whitcroft				} else {
16196ecd9674SAndy Whitcroft					check_absolute_file($file, $herecurr);
16206ecd9674SAndy Whitcroft				}
16216ecd9674SAndy Whitcroft			}
16226ecd9674SAndy Whitcroft		}
16236ecd9674SAndy Whitcroft
1624de7d4f0eSAndy Whitcroft# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
1625de7d4f0eSAndy Whitcroft		if (($realfile =~ /^$/ || $line =~ /^\+/) &&
1626171ae1a4SAndy Whitcroft		    $rawline !~ m/^$UTF8*$/) {
1627171ae1a4SAndy Whitcroft			my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
1628171ae1a4SAndy Whitcroft
1629171ae1a4SAndy Whitcroft			my $blank = copy_spacing($rawline);
1630171ae1a4SAndy Whitcroft			my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
1631171ae1a4SAndy Whitcroft			my $hereptr = "$hereline$ptr\n";
1632171ae1a4SAndy Whitcroft
163334d99219SJoe Perches			CHK("INVALID_UTF8",
1634000d1cc1SJoe Perches			    "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
163500df344fSAndy Whitcroft		}
16360a920b5bSAndy Whitcroft
163715662b3eSJoe Perches# Check if it's the start of a commit log
163815662b3eSJoe Perches# (not a header line and we haven't seen the patch filename)
163915662b3eSJoe Perches		if ($in_header_lines && $realfile =~ /^$/ &&
1640270c49a0SJoe Perches		    $rawline !~ /^(commit\b|from\b|[\w-]+:).+$/i) {
164115662b3eSJoe Perches			$in_header_lines = 0;
164215662b3eSJoe Perches			$in_commit_log = 1;
164315662b3eSJoe Perches		}
164415662b3eSJoe Perches
164515662b3eSJoe Perches# Still not yet in a patch, check for any UTF-8
164615662b3eSJoe Perches		if ($in_commit_log && $realfile =~ /^$/ &&
164715662b3eSJoe Perches		    $rawline =~ /$NON_ASCII_UTF8/) {
164815662b3eSJoe Perches			CHK("UTF8_BEFORE_PATCH",
164915662b3eSJoe Perches			    "8-bit UTF-8 used in possible commit log\n" . $herecurr);
165015662b3eSJoe Perches		}
165115662b3eSJoe Perches
165230670854SAndy Whitcroft# ignore non-hunk lines and lines being removed
165330670854SAndy Whitcroft		next if (!$hunk_line || $line =~ /^-/);
165400df344fSAndy Whitcroft
16550a920b5bSAndy Whitcroft#trailing whitespace
16569c0ca6f9SAndy Whitcroft		if ($line =~ /^\+.*\015/) {
1657c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1658000d1cc1SJoe Perches			ERROR("DOS_LINE_ENDINGS",
1659000d1cc1SJoe Perches			      "DOS line endings\n" . $herevet);
16609c0ca6f9SAndy Whitcroft
1661c2fdda0dSAndy Whitcroft		} elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
1662c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1663000d1cc1SJoe Perches			ERROR("TRAILING_WHITESPACE",
1664000d1cc1SJoe Perches			      "trailing whitespace\n" . $herevet);
1665d2c0a235SAndy Whitcroft			$rpt_cleaners = 1;
16660a920b5bSAndy Whitcroft		}
16675368df20SAndy Whitcroft
16683354957aSAndi Kleen# check for Kconfig help text having a real description
16699fe287d7SAndy Whitcroft# Only applies when adding the entry originally, after that we do not have
16709fe287d7SAndy Whitcroft# sufficient context to determine whether it is indeed long enough.
16713354957aSAndi Kleen		if ($realfile =~ /Kconfig/ &&
16729fe287d7SAndy Whitcroft		    $line =~ /\+\s*(?:---)?help(?:---)?$/) {
16733354957aSAndi Kleen			my $length = 0;
16749fe287d7SAndy Whitcroft			my $cnt = $realcnt;
16759fe287d7SAndy Whitcroft			my $ln = $linenr + 1;
16769fe287d7SAndy Whitcroft			my $f;
16779fe287d7SAndy Whitcroft			my $is_end = 0;
16789fe287d7SAndy Whitcroft			while ($cnt > 0 && defined $lines[$ln - 1]) {
16799fe287d7SAndy Whitcroft				$f = $lines[$ln - 1];
16809fe287d7SAndy Whitcroft				$cnt-- if ($lines[$ln - 1] !~ /^-/);
16819fe287d7SAndy Whitcroft				$is_end = $lines[$ln - 1] =~ /^\+/;
16829fe287d7SAndy Whitcroft				$ln++;
16839fe287d7SAndy Whitcroft
16849fe287d7SAndy Whitcroft				next if ($f =~ /^-/);
16859fe287d7SAndy Whitcroft				$f =~ s/^.//;
16863354957aSAndi Kleen				$f =~ s/#.*//;
16873354957aSAndi Kleen				$f =~ s/^\s+//;
16883354957aSAndi Kleen				next if ($f =~ /^$/);
16899fe287d7SAndy Whitcroft				if ($f =~ /^\s*config\s/) {
16909fe287d7SAndy Whitcroft					$is_end = 1;
16919fe287d7SAndy Whitcroft					last;
16929fe287d7SAndy Whitcroft				}
16933354957aSAndi Kleen				$length++;
16943354957aSAndi Kleen			}
1695000d1cc1SJoe Perches			WARN("CONFIG_DESCRIPTION",
1696000d1cc1SJoe Perches			     "please write a paragraph that describes the config symbol fully\n" . $herecurr) if ($is_end && $length < 4);
16979fe287d7SAndy Whitcroft			#print "is_end<$is_end> length<$length>\n";
16983354957aSAndi Kleen		}
16993354957aSAndi Kleen
1700c68e5878SArnaud Lacombe		if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
1701c68e5878SArnaud Lacombe		    ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
1702c68e5878SArnaud Lacombe			my $flag = $1;
1703c68e5878SArnaud Lacombe			my $replacement = {
1704c68e5878SArnaud Lacombe				'EXTRA_AFLAGS' =>   'asflags-y',
1705c68e5878SArnaud Lacombe				'EXTRA_CFLAGS' =>   'ccflags-y',
1706c68e5878SArnaud Lacombe				'EXTRA_CPPFLAGS' => 'cppflags-y',
1707c68e5878SArnaud Lacombe				'EXTRA_LDFLAGS' =>  'ldflags-y',
1708c68e5878SArnaud Lacombe			};
1709c68e5878SArnaud Lacombe
1710c68e5878SArnaud Lacombe			WARN("DEPRECATED_VARIABLE",
1711c68e5878SArnaud Lacombe			     "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
1712c68e5878SArnaud Lacombe		}
1713c68e5878SArnaud Lacombe
17145368df20SAndy Whitcroft# check we are in a valid source file if not then ignore this hunk
17155368df20SAndy Whitcroft		next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
17165368df20SAndy Whitcroft
17170a920b5bSAndy Whitcroft#80 column limit
1718c45dcabdSAndy Whitcroft		if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ &&
1719f4c014c0SAndy Whitcroft		    $rawline !~ /^.\s*\*\s*\@$Ident\s/ &&
17200fccc622SJoe Perches		    !($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(KERN_\S+\s*|[^"]*))?"[X\t]*"\s*(?:|,|\)\s*;)\s*$/ ||
17218bbea968SJoe Perches		    $line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) &&
1722f4c014c0SAndy Whitcroft		    $length > 80)
1723c45dcabdSAndy Whitcroft		{
1724000d1cc1SJoe Perches			WARN("LONG_LINE",
1725000d1cc1SJoe Perches			     "line over 80 characters\n" . $herecurr);
17260a920b5bSAndy Whitcroft		}
17270a920b5bSAndy Whitcroft
17285e79d96eSJoe Perches# check for spaces before a quoted newline
17295e79d96eSJoe Perches		if ($rawline =~ /^.*\".*\s\\n/) {
1730000d1cc1SJoe Perches			WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
1731000d1cc1SJoe Perches			     "unnecessary whitespace before a quoted newline\n" . $herecurr);
17325e79d96eSJoe Perches		}
17335e79d96eSJoe Perches
17348905a67cSAndy Whitcroft# check for adding lines without a newline.
17358905a67cSAndy Whitcroft		if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
1736000d1cc1SJoe Perches			WARN("MISSING_EOF_NEWLINE",
1737000d1cc1SJoe Perches			     "adding a line without newline at end of file\n" . $herecurr);
17388905a67cSAndy Whitcroft		}
17398905a67cSAndy Whitcroft
174042e41c54SMike Frysinger# Blackfin: use hi/lo macros
174142e41c54SMike Frysinger		if ($realfile =~ m@arch/blackfin/.*\.S$@) {
174242e41c54SMike Frysinger			if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
174342e41c54SMike Frysinger				my $herevet = "$here\n" . cat_vet($line) . "\n";
1744000d1cc1SJoe Perches				ERROR("LO_MACRO",
1745000d1cc1SJoe Perches				      "use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
174642e41c54SMike Frysinger			}
174742e41c54SMike Frysinger			if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
174842e41c54SMike Frysinger				my $herevet = "$here\n" . cat_vet($line) . "\n";
1749000d1cc1SJoe Perches				ERROR("HI_MACRO",
1750000d1cc1SJoe Perches				      "use the HI() macro, not (... >> 16)\n" . $herevet);
175142e41c54SMike Frysinger			}
175242e41c54SMike Frysinger		}
175342e41c54SMike Frysinger
1754b9ea10d6SAndy Whitcroft# check we are in a valid source file C or perl if not then ignore this hunk
1755b9ea10d6SAndy Whitcroft		next if ($realfile !~ /\.(h|c|pl)$/);
17560a920b5bSAndy Whitcroft
17570a920b5bSAndy Whitcroft# at the beginning of a line any tabs must come first and anything
17580a920b5bSAndy Whitcroft# more than 8 must use tabs.
1759c2fdda0dSAndy Whitcroft		if ($rawline =~ /^\+\s* \t\s*\S/ ||
1760c2fdda0dSAndy Whitcroft		    $rawline =~ /^\+\s*        \s*/) {
1761c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1762000d1cc1SJoe Perches			ERROR("CODE_INDENT",
1763000d1cc1SJoe Perches			      "code indent should use tabs where possible\n" . $herevet);
1764d2c0a235SAndy Whitcroft			$rpt_cleaners = 1;
17650a920b5bSAndy Whitcroft		}
17660a920b5bSAndy Whitcroft
176708e44365SAlberto Panizzo# check for space before tabs.
176808e44365SAlberto Panizzo		if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
176908e44365SAlberto Panizzo			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1770000d1cc1SJoe Perches			WARN("SPACE_BEFORE_TAB",
1771000d1cc1SJoe Perches			     "please, no space before tabs\n" . $herevet);
177208e44365SAlberto Panizzo		}
177308e44365SAlberto Panizzo
17745f7ddae6SRaffaele Recalcati# check for spaces at the beginning of a line.
17756b4c5bebSAndy Whitcroft# Exceptions:
17766b4c5bebSAndy Whitcroft#  1) within comments
17776b4c5bebSAndy Whitcroft#  2) indented preprocessor commands
17786b4c5bebSAndy Whitcroft#  3) hanging labels
17796b4c5bebSAndy Whitcroft		if ($rawline =~ /^\+ / && $line !~ /\+ *(?:$;|#|$Ident:)/)  {
17805f7ddae6SRaffaele Recalcati			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1781000d1cc1SJoe Perches			WARN("LEADING_SPACE",
1782000d1cc1SJoe Perches			     "please, no spaces at the start of a line\n" . $herevet);
17835f7ddae6SRaffaele Recalcati		}
17845f7ddae6SRaffaele Recalcati
1785b9ea10d6SAndy Whitcroft# check we are in a valid C source file if not then ignore this hunk
1786b9ea10d6SAndy Whitcroft		next if ($realfile !~ /\.(h|c)$/);
1787b9ea10d6SAndy Whitcroft
1788c2fdda0dSAndy Whitcroft# check for RCS/CVS revision markers
1789cf655043SAndy Whitcroft		if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
1790000d1cc1SJoe Perches			WARN("CVS_KEYWORD",
1791000d1cc1SJoe Perches			     "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
1792c2fdda0dSAndy Whitcroft		}
179322f2a2efSAndy Whitcroft
179442e41c54SMike Frysinger# Blackfin: don't use __builtin_bfin_[cs]sync
179542e41c54SMike Frysinger		if ($line =~ /__builtin_bfin_csync/) {
179642e41c54SMike Frysinger			my $herevet = "$here\n" . cat_vet($line) . "\n";
1797000d1cc1SJoe Perches			ERROR("CSYNC",
1798000d1cc1SJoe Perches			      "use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
179942e41c54SMike Frysinger		}
180042e41c54SMike Frysinger		if ($line =~ /__builtin_bfin_ssync/) {
180142e41c54SMike Frysinger			my $herevet = "$here\n" . cat_vet($line) . "\n";
1802000d1cc1SJoe Perches			ERROR("SSYNC",
1803000d1cc1SJoe Perches			      "use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
180442e41c54SMike Frysinger		}
180542e41c54SMike Frysinger
18069c0ca6f9SAndy Whitcroft# Check for potential 'bare' types
18072b474a1aSAndy Whitcroft		my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
18082b474a1aSAndy Whitcroft		    $realline_next);
18099c9ba34eSAndy Whitcroft		if ($realcnt && $line =~ /.\s*\S/) {
1810170d3a22SAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
1811f5fe35ddSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0);
1812171ae1a4SAndy Whitcroft			$stat =~ s/\n./\n /g;
1813171ae1a4SAndy Whitcroft			$cond =~ s/\n./\n /g;
1814171ae1a4SAndy Whitcroft
1815f74bd194SAndy Whitcroft#print "stat<$stat>\n";
1816f74bd194SAndy Whitcroft
18172b474a1aSAndy Whitcroft			# Find the real next line.
18182b474a1aSAndy Whitcroft			$realline_next = $line_nr_next;
18192b474a1aSAndy Whitcroft			if (defined $realline_next &&
18202b474a1aSAndy Whitcroft			    (!defined $lines[$realline_next - 1] ||
18212b474a1aSAndy Whitcroft			     substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
18222b474a1aSAndy Whitcroft				$realline_next++;
18232b474a1aSAndy Whitcroft			}
18242b474a1aSAndy Whitcroft
1825171ae1a4SAndy Whitcroft			my $s = $stat;
1826171ae1a4SAndy Whitcroft			$s =~ s/{.*$//s;
1827cf655043SAndy Whitcroft
1828c2fdda0dSAndy Whitcroft			# Ignore goto labels.
1829171ae1a4SAndy Whitcroft			if ($s =~ /$Ident:\*$/s) {
1830c2fdda0dSAndy Whitcroft
1831c2fdda0dSAndy Whitcroft			# Ignore functions being called
1832171ae1a4SAndy Whitcroft			} elsif ($s =~ /^.\s*$Ident\s*\(/s) {
1833c2fdda0dSAndy Whitcroft
1834463f2864SAndy Whitcroft			} elsif ($s =~ /^.\s*else\b/s) {
1835463f2864SAndy Whitcroft
1836c45dcabdSAndy Whitcroft			# declarations always start with types
1837d2506586SAndy 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) {
1838c45dcabdSAndy Whitcroft				my $type = $1;
1839c45dcabdSAndy Whitcroft				$type =~ s/\s+/ /g;
1840c45dcabdSAndy Whitcroft				possible($type, "A:" . $s);
1841c45dcabdSAndy Whitcroft
18426c72ffaaSAndy Whitcroft			# definitions in global scope can only start with types
1843a6a84062SAndy Whitcroft			} elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
1844c45dcabdSAndy Whitcroft				possible($1, "B:" . $s);
1845c2fdda0dSAndy Whitcroft			}
18468905a67cSAndy Whitcroft
18476c72ffaaSAndy Whitcroft			# any (foo ... *) is a pointer cast, and foo is a type
184865863862SAndy Whitcroft			while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
1849c45dcabdSAndy Whitcroft				possible($1, "C:" . $s);
18509c0ca6f9SAndy Whitcroft			}
18518905a67cSAndy Whitcroft
18528905a67cSAndy Whitcroft			# Check for any sort of function declaration.
18538905a67cSAndy Whitcroft			# int foo(something bar, other baz);
18548905a67cSAndy Whitcroft			# void (*store_gdt)(x86_descr_ptr *);
1855171ae1a4SAndy Whitcroft			if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
18568905a67cSAndy Whitcroft				my ($name_len) = length($1);
18578905a67cSAndy Whitcroft
1858cf655043SAndy Whitcroft				my $ctx = $s;
1859773647a0SAndy Whitcroft				substr($ctx, 0, $name_len + 1, '');
18608905a67cSAndy Whitcroft				$ctx =~ s/\)[^\)]*$//;
1861cf655043SAndy Whitcroft
18628905a67cSAndy Whitcroft				for my $arg (split(/\s*,\s*/, $ctx)) {
1863c45dcabdSAndy Whitcroft					if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
18648905a67cSAndy Whitcroft
1865c45dcabdSAndy Whitcroft						possible($1, "D:" . $s);
18668905a67cSAndy Whitcroft					}
18678905a67cSAndy Whitcroft				}
18688905a67cSAndy Whitcroft			}
18698905a67cSAndy Whitcroft
18709c0ca6f9SAndy Whitcroft		}
18719c0ca6f9SAndy Whitcroft
187200df344fSAndy Whitcroft#
187300df344fSAndy Whitcroft# Checks which may be anchored in the context.
187400df344fSAndy Whitcroft#
187500df344fSAndy Whitcroft
187600df344fSAndy Whitcroft# Check for switch () and associated case and default
187700df344fSAndy Whitcroft# statements should be at the same indent.
187800df344fSAndy Whitcroft		if ($line=~/\bswitch\s*\(.*\)/) {
187900df344fSAndy Whitcroft			my $err = '';
188000df344fSAndy Whitcroft			my $sep = '';
188100df344fSAndy Whitcroft			my @ctx = ctx_block_outer($linenr, $realcnt);
188200df344fSAndy Whitcroft			shift(@ctx);
188300df344fSAndy Whitcroft			for my $ctx (@ctx) {
188400df344fSAndy Whitcroft				my ($clen, $cindent) = line_stats($ctx);
188500df344fSAndy Whitcroft				if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
188600df344fSAndy Whitcroft							$indent != $cindent) {
188700df344fSAndy Whitcroft					$err .= "$sep$ctx\n";
188800df344fSAndy Whitcroft					$sep = '';
188900df344fSAndy Whitcroft				} else {
189000df344fSAndy Whitcroft					$sep = "[...]\n";
189100df344fSAndy Whitcroft				}
189200df344fSAndy Whitcroft			}
189300df344fSAndy Whitcroft			if ($err ne '') {
1894000d1cc1SJoe Perches				ERROR("SWITCH_CASE_INDENT_LEVEL",
1895000d1cc1SJoe Perches				      "switch and case should be at the same indent\n$hereline$err");
1896de7d4f0eSAndy Whitcroft			}
1897de7d4f0eSAndy Whitcroft		}
1898de7d4f0eSAndy Whitcroft
1899de7d4f0eSAndy Whitcroft# if/while/etc brace do not go on next line, unless defining a do while loop,
1900de7d4f0eSAndy Whitcroft# or if that brace on the next line is for something else
1901c45dcabdSAndy Whitcroft		if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
1902773647a0SAndy Whitcroft			my $pre_ctx = "$1$2";
1903773647a0SAndy Whitcroft
19049c0ca6f9SAndy Whitcroft			my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
1905de7d4f0eSAndy Whitcroft			my $ctx_cnt = $realcnt - $#ctx - 1;
1906de7d4f0eSAndy Whitcroft			my $ctx = join("\n", @ctx);
1907de7d4f0eSAndy Whitcroft
1908548596d5SAndy Whitcroft			my $ctx_ln = $linenr;
1909548596d5SAndy Whitcroft			my $ctx_skip = $realcnt;
1910de7d4f0eSAndy Whitcroft
1911548596d5SAndy Whitcroft			while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
1912548596d5SAndy Whitcroft					defined $lines[$ctx_ln - 1] &&
1913548596d5SAndy Whitcroft					$lines[$ctx_ln - 1] =~ /^-/)) {
1914548596d5SAndy Whitcroft				##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
1915548596d5SAndy Whitcroft				$ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
1916773647a0SAndy Whitcroft				$ctx_ln++;
1917773647a0SAndy Whitcroft			}
1918548596d5SAndy Whitcroft
191953210168SAndy Whitcroft			#print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
192053210168SAndy Whitcroft			#print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
1921773647a0SAndy Whitcroft
1922773647a0SAndy Whitcroft			if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln -1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
1923000d1cc1SJoe Perches				ERROR("OPEN_BRACE",
1924000d1cc1SJoe Perches				      "that open brace { should be on the previous line\n" .
192501464f30SAndy Whitcroft					"$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
192600df344fSAndy Whitcroft			}
1927773647a0SAndy Whitcroft			if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
1928773647a0SAndy Whitcroft			    $ctx =~ /\)\s*\;\s*$/ &&
1929773647a0SAndy Whitcroft			    defined $lines[$ctx_ln - 1])
1930773647a0SAndy Whitcroft			{
19319c0ca6f9SAndy Whitcroft				my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
19329c0ca6f9SAndy Whitcroft				if ($nindent > $indent) {
1933000d1cc1SJoe Perches					WARN("TRAILING_SEMICOLON",
1934000d1cc1SJoe Perches					     "trailing semicolon indicates no statements, indent implies otherwise\n" .
193501464f30SAndy Whitcroft						"$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
19369c0ca6f9SAndy Whitcroft				}
19379c0ca6f9SAndy Whitcroft			}
193800df344fSAndy Whitcroft		}
193900df344fSAndy Whitcroft
19404d001e4dSAndy Whitcroft# Check relative indent for conditionals and blocks.
19414d001e4dSAndy Whitcroft		if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
19424d001e4dSAndy Whitcroft			my ($s, $c) = ($stat, $cond);
19434d001e4dSAndy Whitcroft
19444d001e4dSAndy Whitcroft			substr($s, 0, length($c), '');
19454d001e4dSAndy Whitcroft
19464d001e4dSAndy Whitcroft			# Make sure we remove the line prefixes as we have
19474d001e4dSAndy Whitcroft			# none on the first line, and are going to readd them
19484d001e4dSAndy Whitcroft			# where necessary.
19494d001e4dSAndy Whitcroft			$s =~ s/\n./\n/gs;
19504d001e4dSAndy Whitcroft
19514d001e4dSAndy Whitcroft			# Find out how long the conditional actually is.
19526f779c18SAndy Whitcroft			my @newlines = ($c =~ /\n/gs);
19536f779c18SAndy Whitcroft			my $cond_lines = 1 + $#newlines;
19544d001e4dSAndy Whitcroft
19554d001e4dSAndy Whitcroft			# We want to check the first line inside the block
19564d001e4dSAndy Whitcroft			# starting at the end of the conditional, so remove:
19574d001e4dSAndy Whitcroft			#  1) any blank line termination
19584d001e4dSAndy Whitcroft			#  2) any opening brace { on end of the line
19594d001e4dSAndy Whitcroft			#  3) any do (...) {
19604d001e4dSAndy Whitcroft			my $continuation = 0;
19614d001e4dSAndy Whitcroft			my $check = 0;
19624d001e4dSAndy Whitcroft			$s =~ s/^.*\bdo\b//;
19634d001e4dSAndy Whitcroft			$s =~ s/^\s*{//;
19644d001e4dSAndy Whitcroft			if ($s =~ s/^\s*\\//) {
19654d001e4dSAndy Whitcroft				$continuation = 1;
19664d001e4dSAndy Whitcroft			}
19679bd49efeSAndy Whitcroft			if ($s =~ s/^\s*?\n//) {
19684d001e4dSAndy Whitcroft				$check = 1;
19694d001e4dSAndy Whitcroft				$cond_lines++;
19704d001e4dSAndy Whitcroft			}
19714d001e4dSAndy Whitcroft
19724d001e4dSAndy Whitcroft			# Also ignore a loop construct at the end of a
19734d001e4dSAndy Whitcroft			# preprocessor statement.
19744d001e4dSAndy Whitcroft			if (($prevline =~ /^.\s*#\s*define\s/ ||
19754d001e4dSAndy Whitcroft			    $prevline =~ /\\\s*$/) && $continuation == 0) {
19764d001e4dSAndy Whitcroft				$check = 0;
19774d001e4dSAndy Whitcroft			}
19784d001e4dSAndy Whitcroft
19799bd49efeSAndy Whitcroft			my $cond_ptr = -1;
1980740504c6SAndy Whitcroft			$continuation = 0;
19819bd49efeSAndy Whitcroft			while ($cond_ptr != $cond_lines) {
19829bd49efeSAndy Whitcroft				$cond_ptr = $cond_lines;
19834d001e4dSAndy Whitcroft
1984f16fa28fSAndy Whitcroft				# If we see an #else/#elif then the code
1985f16fa28fSAndy Whitcroft				# is not linear.
1986f16fa28fSAndy Whitcroft				if ($s =~ /^\s*\#\s*(?:else|elif)/) {
1987f16fa28fSAndy Whitcroft					$check = 0;
1988f16fa28fSAndy Whitcroft				}
1989f16fa28fSAndy Whitcroft
19909bd49efeSAndy Whitcroft				# Ignore:
19919bd49efeSAndy Whitcroft				#  1) blank lines, they should be at 0,
19929bd49efeSAndy Whitcroft				#  2) preprocessor lines, and
19939bd49efeSAndy Whitcroft				#  3) labels.
1994740504c6SAndy Whitcroft				if ($continuation ||
1995740504c6SAndy Whitcroft				    $s =~ /^\s*?\n/ ||
19969bd49efeSAndy Whitcroft				    $s =~ /^\s*#\s*?/ ||
19979bd49efeSAndy Whitcroft				    $s =~ /^\s*$Ident\s*:/) {
1998740504c6SAndy Whitcroft					$continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
199930dad6ebSAndy Whitcroft					if ($s =~ s/^.*?\n//) {
20009bd49efeSAndy Whitcroft						$cond_lines++;
20019bd49efeSAndy Whitcroft					}
20024d001e4dSAndy Whitcroft				}
200330dad6ebSAndy Whitcroft			}
20044d001e4dSAndy Whitcroft
20054d001e4dSAndy Whitcroft			my (undef, $sindent) = line_stats("+" . $s);
20064d001e4dSAndy Whitcroft			my $stat_real = raw_line($linenr, $cond_lines);
20074d001e4dSAndy Whitcroft
20084d001e4dSAndy Whitcroft			# Check if either of these lines are modified, else
20094d001e4dSAndy Whitcroft			# this is not this patch's fault.
20104d001e4dSAndy Whitcroft			if (!defined($stat_real) ||
20114d001e4dSAndy Whitcroft			    $stat !~ /^\+/ && $stat_real !~ /^\+/) {
20124d001e4dSAndy Whitcroft				$check = 0;
20134d001e4dSAndy Whitcroft			}
20144d001e4dSAndy Whitcroft			if (defined($stat_real) && $cond_lines > 1) {
20154d001e4dSAndy Whitcroft				$stat_real = "[...]\n$stat_real";
20164d001e4dSAndy Whitcroft			}
20174d001e4dSAndy Whitcroft
20189bd49efeSAndy 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";
20194d001e4dSAndy Whitcroft
20204d001e4dSAndy Whitcroft			if ($check && (($sindent % 8) != 0 ||
20214d001e4dSAndy Whitcroft			    ($sindent <= $indent && $s ne ''))) {
2022000d1cc1SJoe Perches				WARN("SUSPECT_CODE_INDENT",
2023000d1cc1SJoe Perches				     "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
20244d001e4dSAndy Whitcroft			}
20254d001e4dSAndy Whitcroft		}
20264d001e4dSAndy Whitcroft
20276c72ffaaSAndy Whitcroft		# Track the 'values' across context and added lines.
20286c72ffaaSAndy Whitcroft		my $opline = $line; $opline =~ s/^./ /;
20291f65f947SAndy Whitcroft		my ($curr_values, $curr_vars) =
20301f65f947SAndy Whitcroft				annotate_values($opline . "\n", $prev_values);
20316c72ffaaSAndy Whitcroft		$curr_values = $prev_values . $curr_values;
2032c2fdda0dSAndy Whitcroft		if ($dbg_values) {
2033c2fdda0dSAndy Whitcroft			my $outline = $opline; $outline =~ s/\t/ /g;
2034cf655043SAndy Whitcroft			print "$linenr > .$outline\n";
2035cf655043SAndy Whitcroft			print "$linenr > $curr_values\n";
20361f65f947SAndy Whitcroft			print "$linenr >  $curr_vars\n";
2037c2fdda0dSAndy Whitcroft		}
20386c72ffaaSAndy Whitcroft		$prev_values = substr($curr_values, -1);
20396c72ffaaSAndy Whitcroft
204000df344fSAndy Whitcroft#ignore lines not being added
204100df344fSAndy Whitcroft		if ($line=~/^[^\+]/) {next;}
204200df344fSAndy Whitcroft
2043653d4876SAndy Whitcroft# TEST: allow direct testing of the type matcher.
20447429c690SAndy Whitcroft		if ($dbg_type) {
20457429c690SAndy Whitcroft			if ($line =~ /^.\s*$Declare\s*$/) {
2046000d1cc1SJoe Perches				ERROR("TEST_TYPE",
2047000d1cc1SJoe Perches				      "TEST: is type\n" . $herecurr);
20487429c690SAndy Whitcroft			} elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
2049000d1cc1SJoe Perches				ERROR("TEST_NOT_TYPE",
2050000d1cc1SJoe Perches				      "TEST: is not type ($1 is)\n". $herecurr);
20517429c690SAndy Whitcroft			}
2052653d4876SAndy Whitcroft			next;
2053653d4876SAndy Whitcroft		}
2054a1ef277eSAndy Whitcroft# TEST: allow direct testing of the attribute matcher.
2055a1ef277eSAndy Whitcroft		if ($dbg_attr) {
20569360b0e5SAndy Whitcroft			if ($line =~ /^.\s*$Modifier\s*$/) {
2057000d1cc1SJoe Perches				ERROR("TEST_ATTR",
2058000d1cc1SJoe Perches				      "TEST: is attr\n" . $herecurr);
20599360b0e5SAndy Whitcroft			} elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
2060000d1cc1SJoe Perches				ERROR("TEST_NOT_ATTR",
2061000d1cc1SJoe Perches				      "TEST: is not attr ($1 is)\n". $herecurr);
2062a1ef277eSAndy Whitcroft			}
2063a1ef277eSAndy Whitcroft			next;
2064a1ef277eSAndy Whitcroft		}
2065653d4876SAndy Whitcroft
2066f0a594c1SAndy Whitcroft# check for initialisation to aggregates open brace on the next line
206799423c20SAndy Whitcroft		if ($line =~ /^.\s*{/ &&
206899423c20SAndy Whitcroft		    $prevline =~ /(?:^|[^=])=\s*$/) {
2069000d1cc1SJoe Perches			ERROR("OPEN_BRACE",
2070000d1cc1SJoe Perches			      "that open brace { should be on the previous line\n" . $hereprev);
2071f0a594c1SAndy Whitcroft		}
2072f0a594c1SAndy Whitcroft
207300df344fSAndy Whitcroft#
207400df344fSAndy Whitcroft# Checks which are anchored on the added line.
207500df344fSAndy Whitcroft#
207600df344fSAndy Whitcroft
2077653d4876SAndy Whitcroft# check for malformed paths in #include statements (uses RAW line)
2078c45dcabdSAndy Whitcroft		if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
2079653d4876SAndy Whitcroft			my $path = $1;
2080653d4876SAndy Whitcroft			if ($path =~ m{//}) {
2081000d1cc1SJoe Perches				ERROR("MALFORMED_INCLUDE",
2082000d1cc1SJoe Perches				      "malformed #include filename\n" .
2083de7d4f0eSAndy Whitcroft					$herecurr);
2084653d4876SAndy Whitcroft			}
2085653d4876SAndy Whitcroft		}
2086653d4876SAndy Whitcroft
208700df344fSAndy Whitcroft# no C99 // comments
208800df344fSAndy Whitcroft		if ($line =~ m{//}) {
2089000d1cc1SJoe Perches			ERROR("C99_COMMENTS",
2090000d1cc1SJoe Perches			      "do not use C99 // comments\n" . $herecurr);
209100df344fSAndy Whitcroft		}
209200df344fSAndy Whitcroft		# Remove C99 comments.
20930a920b5bSAndy Whitcroft		$line =~ s@//.*@@;
20946c72ffaaSAndy Whitcroft		$opline =~ s@//.*@@;
20950a920b5bSAndy Whitcroft
20962b474a1aSAndy Whitcroft# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
20972b474a1aSAndy Whitcroft# the whole statement.
20982b474a1aSAndy Whitcroft#print "APW <$lines[$realline_next - 1]>\n";
20992b474a1aSAndy Whitcroft		if (defined $realline_next &&
21002b474a1aSAndy Whitcroft		    exists $lines[$realline_next - 1] &&
21012b474a1aSAndy Whitcroft		    !defined $suppress_export{$realline_next} &&
21022b474a1aSAndy Whitcroft		    ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
21032b474a1aSAndy Whitcroft		     $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
21043cbf62dfSAndy Whitcroft			# Handle definitions which produce identifiers with
21053cbf62dfSAndy Whitcroft			# a prefix:
21063cbf62dfSAndy Whitcroft			#   XXX(foo);
21073cbf62dfSAndy Whitcroft			#   EXPORT_SYMBOL(something_foo);
2108653d4876SAndy Whitcroft			my $name = $1;
21093cbf62dfSAndy Whitcroft			if ($stat =~ /^.([A-Z_]+)\s*\(\s*($Ident)/ &&
21103cbf62dfSAndy Whitcroft			    $name =~ /^${Ident}_$2/) {
21113cbf62dfSAndy Whitcroft#print "FOO C name<$name>\n";
21123cbf62dfSAndy Whitcroft				$suppress_export{$realline_next} = 1;
21133cbf62dfSAndy Whitcroft
21143cbf62dfSAndy Whitcroft			} elsif ($stat !~ /(?:
21152b474a1aSAndy Whitcroft				\n.}\s*$|
211648012058SAndy Whitcroft				^.DEFINE_$Ident\(\Q$name\E\)|
211748012058SAndy Whitcroft				^.DECLARE_$Ident\(\Q$name\E\)|
211848012058SAndy Whitcroft				^.LIST_HEAD\(\Q$name\E\)|
21192b474a1aSAndy Whitcroft				^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
21202b474a1aSAndy Whitcroft				\b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
212148012058SAndy Whitcroft			    )/x) {
21222b474a1aSAndy Whitcroft#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
21232b474a1aSAndy Whitcroft				$suppress_export{$realline_next} = 2;
21242b474a1aSAndy Whitcroft			} else {
21252b474a1aSAndy Whitcroft				$suppress_export{$realline_next} = 1;
21260a920b5bSAndy Whitcroft			}
21270a920b5bSAndy Whitcroft		}
21282b474a1aSAndy Whitcroft		if (!defined $suppress_export{$linenr} &&
21292b474a1aSAndy Whitcroft		    $prevline =~ /^.\s*$/ &&
21302b474a1aSAndy Whitcroft		    ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
21312b474a1aSAndy Whitcroft		     $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
21322b474a1aSAndy Whitcroft#print "FOO B <$lines[$linenr - 1]>\n";
21332b474a1aSAndy Whitcroft			$suppress_export{$linenr} = 2;
21342b474a1aSAndy Whitcroft		}
21352b474a1aSAndy Whitcroft		if (defined $suppress_export{$linenr} &&
21362b474a1aSAndy Whitcroft		    $suppress_export{$linenr} == 2) {
2137000d1cc1SJoe Perches			WARN("EXPORT_SYMBOL",
2138000d1cc1SJoe Perches			     "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
21392b474a1aSAndy Whitcroft		}
21400a920b5bSAndy Whitcroft
21415150bda4SJoe Eloff# check for global initialisers.
2142c45dcabdSAndy Whitcroft		if ($line =~ /^.$Type\s*$Ident\s*(?:\s+$Modifier)*\s*=\s*(0|NULL|false)\s*;/) {
2143000d1cc1SJoe Perches			ERROR("GLOBAL_INITIALISERS",
2144000d1cc1SJoe Perches			      "do not initialise globals to 0 or NULL\n" .
2145f0a594c1SAndy Whitcroft				$herecurr);
2146f0a594c1SAndy Whitcroft		}
21470a920b5bSAndy Whitcroft# check for static initialisers.
21482d1bafd7SAndy Whitcroft		if ($line =~ /\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
2149000d1cc1SJoe Perches			ERROR("INITIALISED_STATIC",
2150000d1cc1SJoe Perches			      "do not initialise statics to 0 or NULL\n" .
2151de7d4f0eSAndy Whitcroft				$herecurr);
21520a920b5bSAndy Whitcroft		}
21530a920b5bSAndy Whitcroft
2154cb710ecaSJoe Perches# check for static const char * arrays.
2155cb710ecaSJoe Perches		if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
2156000d1cc1SJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
2157000d1cc1SJoe Perches			     "static const char * array should probably be static const char * const\n" .
2158cb710ecaSJoe Perches				$herecurr);
2159cb710ecaSJoe Perches               }
2160cb710ecaSJoe Perches
2161cb710ecaSJoe Perches# check for static char foo[] = "bar" declarations.
2162cb710ecaSJoe Perches		if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
2163000d1cc1SJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
2164000d1cc1SJoe Perches			     "static char array declaration should probably be static const char\n" .
2165cb710ecaSJoe Perches				$herecurr);
2166cb710ecaSJoe Perches               }
2167cb710ecaSJoe Perches
216893ed0e2dSJoe Perches# check for declarations of struct pci_device_id
216993ed0e2dSJoe Perches		if ($line =~ /\bstruct\s+pci_device_id\s+\w+\s*\[\s*\]\s*\=\s*\{/) {
2170000d1cc1SJoe Perches			WARN("DEFINE_PCI_DEVICE_TABLE",
2171000d1cc1SJoe Perches			     "Use DEFINE_PCI_DEVICE_TABLE for struct pci_device_id\n" . $herecurr);
217293ed0e2dSJoe Perches		}
217393ed0e2dSJoe Perches
2174653d4876SAndy Whitcroft# check for new typedefs, only function parameters and sparse annotations
2175653d4876SAndy Whitcroft# make sense.
2176653d4876SAndy Whitcroft		if ($line =~ /\btypedef\s/ &&
21778054576dSAndy Whitcroft		    $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
2178c45dcabdSAndy Whitcroft		    $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
21798ed22cadSAndy Whitcroft		    $line !~ /\b$typeTypedefs\b/ &&
2180653d4876SAndy Whitcroft		    $line !~ /\b__bitwise(?:__|)\b/) {
2181000d1cc1SJoe Perches			WARN("NEW_TYPEDEFS",
2182000d1cc1SJoe Perches			     "do not add new typedefs\n" . $herecurr);
21830a920b5bSAndy Whitcroft		}
21840a920b5bSAndy Whitcroft
21850a920b5bSAndy Whitcroft# * goes on variable not on type
218665863862SAndy Whitcroft		# (char*[ const])
218700ef4eceSAndy Whitcroft		if ($line =~ m{\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\)}) {
218865863862SAndy Whitcroft			my ($from, $to) = ($1, $1);
2189d8aaf121SAndy Whitcroft
219065863862SAndy Whitcroft			# Should start with a space.
219165863862SAndy Whitcroft			$to =~ s/^(\S)/ $1/;
219265863862SAndy Whitcroft			# Should not end with a space.
219365863862SAndy Whitcroft			$to =~ s/\s+$//;
219465863862SAndy Whitcroft			# '*'s should not have spaces between.
2195f9a0b3d1SAndy Whitcroft			while ($to =~ s/\*\s+\*/\*\*/) {
219665863862SAndy Whitcroft			}
2197d8aaf121SAndy Whitcroft
219865863862SAndy Whitcroft			#print "from<$from> to<$to>\n";
219965863862SAndy Whitcroft			if ($from ne $to) {
2200000d1cc1SJoe Perches				ERROR("POINTER_LOCATION",
2201000d1cc1SJoe Perches				      "\"(foo$from)\" should be \"(foo$to)\"\n" .  $herecurr);
220265863862SAndy Whitcroft			}
220300ef4eceSAndy Whitcroft		} elsif ($line =~ m{\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident)}) {
220465863862SAndy Whitcroft			my ($from, $to, $ident) = ($1, $1, $2);
2205d8aaf121SAndy Whitcroft
220665863862SAndy Whitcroft			# Should start with a space.
220765863862SAndy Whitcroft			$to =~ s/^(\S)/ $1/;
220865863862SAndy Whitcroft			# Should not end with a space.
220965863862SAndy Whitcroft			$to =~ s/\s+$//;
221065863862SAndy Whitcroft			# '*'s should not have spaces between.
2211f9a0b3d1SAndy Whitcroft			while ($to =~ s/\*\s+\*/\*\*/) {
221265863862SAndy Whitcroft			}
221365863862SAndy Whitcroft			# Modifiers should have spaces.
221465863862SAndy Whitcroft			$to =~ s/(\b$Modifier$)/$1 /;
221565863862SAndy Whitcroft
2216667026e7SAndy Whitcroft			#print "from<$from> to<$to> ident<$ident>\n";
2217667026e7SAndy Whitcroft			if ($from ne $to && $ident !~ /^$Modifier$/) {
2218000d1cc1SJoe Perches				ERROR("POINTER_LOCATION",
2219000d1cc1SJoe Perches				      "\"foo${from}bar\" should be \"foo${to}bar\"\n" .  $herecurr);
222065863862SAndy Whitcroft			}
22210a920b5bSAndy Whitcroft		}
22220a920b5bSAndy Whitcroft
22230a920b5bSAndy Whitcroft# # no BUG() or BUG_ON()
22240a920b5bSAndy Whitcroft# 		if ($line =~ /\b(BUG|BUG_ON)\b/) {
22250a920b5bSAndy Whitcroft# 			print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
22260a920b5bSAndy Whitcroft# 			print "$herecurr";
22270a920b5bSAndy Whitcroft# 			$clean = 0;
22280a920b5bSAndy Whitcroft# 		}
22290a920b5bSAndy Whitcroft
22308905a67cSAndy Whitcroft		if ($line =~ /\bLINUX_VERSION_CODE\b/) {
2231000d1cc1SJoe Perches			WARN("LINUX_VERSION_CODE",
2232000d1cc1SJoe Perches			     "LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
22338905a67cSAndy Whitcroft		}
22348905a67cSAndy Whitcroft
223517441227SJoe Perches# check for uses of printk_ratelimit
223617441227SJoe Perches		if ($line =~ /\bprintk_ratelimit\s*\(/) {
2237000d1cc1SJoe Perches			WARN("PRINTK_RATELIMITED",
2238000d1cc1SJoe Perches"Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
223917441227SJoe Perches		}
224017441227SJoe Perches
224100df344fSAndy Whitcroft# printk should use KERN_* levels.  Note that follow on printk's on the
224200df344fSAndy Whitcroft# same line do not need a level, so we use the current block context
224300df344fSAndy Whitcroft# to try and find and validate the current printk.  In summary the current
224425985edcSLucas De Marchi# printk includes all preceding printk's which have no newline on the end.
224500df344fSAndy Whitcroft# we assume the first bad printk is the one to report.
2246f0a594c1SAndy Whitcroft		if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
224700df344fSAndy Whitcroft			my $ok = 0;
224800df344fSAndy Whitcroft			for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
224900df344fSAndy Whitcroft				#print "CHECK<$lines[$ln - 1]\n";
225025985edcSLucas De Marchi				# we have a preceding printk if it ends
225100df344fSAndy Whitcroft				# with "\n" ignore it, else it is to blame
225200df344fSAndy Whitcroft				if ($lines[$ln - 1] =~ m{\bprintk\(}) {
225300df344fSAndy Whitcroft					if ($rawlines[$ln - 1] !~ m{\\n"}) {
225400df344fSAndy Whitcroft						$ok = 1;
225500df344fSAndy Whitcroft					}
225600df344fSAndy Whitcroft					last;
225700df344fSAndy Whitcroft				}
225800df344fSAndy Whitcroft			}
225900df344fSAndy Whitcroft			if ($ok == 0) {
2260000d1cc1SJoe Perches				WARN("PRINTK_WITHOUT_KERN_LEVEL",
2261000d1cc1SJoe Perches				     "printk() should include KERN_ facility level\n" . $herecurr);
22620a920b5bSAndy Whitcroft			}
226300df344fSAndy Whitcroft		}
22640a920b5bSAndy Whitcroft
2265653d4876SAndy Whitcroft# function brace can't be on same line, except for #defines of do while,
2266653d4876SAndy Whitcroft# or if closed on same line
2267c45dcabdSAndy Whitcroft		if (($line=~/$Type\s*$Ident\(.*\).*\s{/) and
2268c45dcabdSAndy Whitcroft		    !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) {
2269000d1cc1SJoe Perches			ERROR("OPEN_BRACE",
2270000d1cc1SJoe Perches			      "open brace '{' following function declarations go on the next line\n" . $herecurr);
22710a920b5bSAndy Whitcroft		}
2272653d4876SAndy Whitcroft
22738905a67cSAndy Whitcroft# open braces for enum, union and struct go on the same line.
22748905a67cSAndy Whitcroft		if ($line =~ /^.\s*{/ &&
22758905a67cSAndy Whitcroft		    $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
2276000d1cc1SJoe Perches			ERROR("OPEN_BRACE",
2277000d1cc1SJoe Perches			      "open brace '{' following $1 go on the same line\n" . $hereprev);
22788905a67cSAndy Whitcroft		}
22798905a67cSAndy Whitcroft
22800c73b4ebSAndy Whitcroft# missing space after union, struct or enum definition
22810c73b4ebSAndy Whitcroft		if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?(?:\s+$Ident)?[=\{]/) {
2282000d1cc1SJoe Perches		    WARN("SPACING",
2283000d1cc1SJoe Perches			 "missing space after $1 definition\n" . $herecurr);
22840c73b4ebSAndy Whitcroft		}
22850c73b4ebSAndy Whitcroft
22868d31cfceSAndy Whitcroft# check for spacing round square brackets; allowed:
22878d31cfceSAndy Whitcroft#  1. with a type on the left -- int [] a;
2288fe2a7dbcSAndy Whitcroft#  2. at the beginning of a line for slice initialisers -- [0...10] = 5,
2289fe2a7dbcSAndy Whitcroft#  3. inside a curly brace -- = { [0...10] = 5 }
22908d31cfceSAndy Whitcroft		while ($line =~ /(.*?\s)\[/g) {
22918d31cfceSAndy Whitcroft			my ($where, $prefix) = ($-[1], $1);
22928d31cfceSAndy Whitcroft			if ($prefix !~ /$Type\s+$/ &&
2293fe2a7dbcSAndy Whitcroft			    ($where != 0 || $prefix !~ /^.\s+$/) &&
2294fe2a7dbcSAndy Whitcroft			    $prefix !~ /{\s+$/) {
2295000d1cc1SJoe Perches				ERROR("BRACKET_SPACE",
2296000d1cc1SJoe Perches				      "space prohibited before open square bracket '['\n" . $herecurr);
22978d31cfceSAndy Whitcroft			}
22988d31cfceSAndy Whitcroft		}
22998d31cfceSAndy Whitcroft
2300f0a594c1SAndy Whitcroft# check for spaces between functions and their parentheses.
23016c72ffaaSAndy Whitcroft		while ($line =~ /($Ident)\s+\(/g) {
2302c2fdda0dSAndy Whitcroft			my $name = $1;
2303773647a0SAndy Whitcroft			my $ctx_before = substr($line, 0, $-[1]);
2304773647a0SAndy Whitcroft			my $ctx = "$ctx_before$name";
2305c2fdda0dSAndy Whitcroft
2306c2fdda0dSAndy Whitcroft			# Ignore those directives where spaces _are_ permitted.
2307773647a0SAndy Whitcroft			if ($name =~ /^(?:
2308773647a0SAndy Whitcroft				if|for|while|switch|return|case|
2309773647a0SAndy Whitcroft				volatile|__volatile__|
2310773647a0SAndy Whitcroft				__attribute__|format|__extension__|
2311773647a0SAndy Whitcroft				asm|__asm__)$/x)
2312773647a0SAndy Whitcroft			{
2313c2fdda0dSAndy Whitcroft
2314c2fdda0dSAndy Whitcroft			# cpp #define statements have non-optional spaces, ie
2315c2fdda0dSAndy Whitcroft			# if there is a space between the name and the open
2316c2fdda0dSAndy Whitcroft			# parenthesis it is simply not a parameter group.
2317c45dcabdSAndy Whitcroft			} elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
2318773647a0SAndy Whitcroft
2319773647a0SAndy Whitcroft			# cpp #elif statement condition may start with a (
2320c45dcabdSAndy Whitcroft			} elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
2321c2fdda0dSAndy Whitcroft
2322c2fdda0dSAndy Whitcroft			# If this whole things ends with a type its most
2323c2fdda0dSAndy Whitcroft			# likely a typedef for a function.
2324773647a0SAndy Whitcroft			} elsif ($ctx =~ /$Type$/) {
2325c2fdda0dSAndy Whitcroft
2326c2fdda0dSAndy Whitcroft			} else {
2327000d1cc1SJoe Perches				WARN("SPACING",
2328000d1cc1SJoe Perches				     "space prohibited between function name and open parenthesis '('\n" . $herecurr);
2329f0a594c1SAndy Whitcroft			}
23306c72ffaaSAndy Whitcroft		}
2331653d4876SAndy Whitcroft# Check operator spacing.
23320a920b5bSAndy Whitcroft		if (!($line=~/\#\s*include/)) {
23339c0ca6f9SAndy Whitcroft			my $ops = qr{
23349c0ca6f9SAndy Whitcroft				<<=|>>=|<=|>=|==|!=|
23359c0ca6f9SAndy Whitcroft				\+=|-=|\*=|\/=|%=|\^=|\|=|&=|
23369c0ca6f9SAndy Whitcroft				=>|->|<<|>>|<|>|=|!|~|
23371f65f947SAndy Whitcroft				&&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
23381f65f947SAndy Whitcroft				\?|:
23399c0ca6f9SAndy Whitcroft			}x;
2340cf655043SAndy Whitcroft			my @elements = split(/($ops|;)/, $opline);
234100df344fSAndy Whitcroft			my $off = 0;
23426c72ffaaSAndy Whitcroft
23436c72ffaaSAndy Whitcroft			my $blank = copy_spacing($opline);
23446c72ffaaSAndy Whitcroft
23450a920b5bSAndy Whitcroft			for (my $n = 0; $n < $#elements; $n += 2) {
23464a0df2efSAndy Whitcroft				$off += length($elements[$n]);
23474a0df2efSAndy Whitcroft
234825985edcSLucas De Marchi				# Pick up the preceding and succeeding characters.
2349773647a0SAndy Whitcroft				my $ca = substr($opline, 0, $off);
2350773647a0SAndy Whitcroft				my $cc = '';
2351773647a0SAndy Whitcroft				if (length($opline) >= ($off + length($elements[$n + 1]))) {
2352773647a0SAndy Whitcroft					$cc = substr($opline, $off + length($elements[$n + 1]));
2353773647a0SAndy Whitcroft				}
2354773647a0SAndy Whitcroft				my $cb = "$ca$;$cc";
2355773647a0SAndy Whitcroft
23564a0df2efSAndy Whitcroft				my $a = '';
23574a0df2efSAndy Whitcroft				$a = 'V' if ($elements[$n] ne '');
23584a0df2efSAndy Whitcroft				$a = 'W' if ($elements[$n] =~ /\s$/);
2359cf655043SAndy Whitcroft				$a = 'C' if ($elements[$n] =~ /$;$/);
23604a0df2efSAndy Whitcroft				$a = 'B' if ($elements[$n] =~ /(\[|\()$/);
23614a0df2efSAndy Whitcroft				$a = 'O' if ($elements[$n] eq '');
2362773647a0SAndy Whitcroft				$a = 'E' if ($ca =~ /^\s*$/);
23634a0df2efSAndy Whitcroft
23640a920b5bSAndy Whitcroft				my $op = $elements[$n + 1];
23654a0df2efSAndy Whitcroft
23664a0df2efSAndy Whitcroft				my $c = '';
23670a920b5bSAndy Whitcroft				if (defined $elements[$n + 2]) {
23684a0df2efSAndy Whitcroft					$c = 'V' if ($elements[$n + 2] ne '');
23694a0df2efSAndy Whitcroft					$c = 'W' if ($elements[$n + 2] =~ /^\s/);
2370cf655043SAndy Whitcroft					$c = 'C' if ($elements[$n + 2] =~ /^$;/);
23714a0df2efSAndy Whitcroft					$c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
23724a0df2efSAndy Whitcroft					$c = 'O' if ($elements[$n + 2] eq '');
23738b1b3378SAndy Whitcroft					$c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
23744a0df2efSAndy Whitcroft				} else {
23754a0df2efSAndy Whitcroft					$c = 'E';
23760a920b5bSAndy Whitcroft				}
23770a920b5bSAndy Whitcroft
23784a0df2efSAndy Whitcroft				my $ctx = "${a}x${c}";
23794a0df2efSAndy Whitcroft
23804a0df2efSAndy Whitcroft				my $at = "(ctx:$ctx)";
23814a0df2efSAndy Whitcroft
23826c72ffaaSAndy Whitcroft				my $ptr = substr($blank, 0, $off) . "^";
2383de7d4f0eSAndy Whitcroft				my $hereptr = "$hereline$ptr\n";
23840a920b5bSAndy Whitcroft
238574048ed8SAndy Whitcroft				# Pull out the value of this operator.
23866c72ffaaSAndy Whitcroft				my $op_type = substr($curr_values, $off + 1, 1);
23870a920b5bSAndy Whitcroft
23881f65f947SAndy Whitcroft				# Get the full operator variant.
23891f65f947SAndy Whitcroft				my $opv = $op . substr($curr_vars, $off, 1);
23901f65f947SAndy Whitcroft
239113214adfSAndy Whitcroft				# Ignore operators passed as parameters.
239213214adfSAndy Whitcroft				if ($op_type ne 'V' &&
239313214adfSAndy Whitcroft				    $ca =~ /\s$/ && $cc =~ /^\s*,/) {
239413214adfSAndy Whitcroft
2395cf655043SAndy Whitcroft#				# Ignore comments
2396cf655043SAndy Whitcroft#				} elsif ($op =~ /^$;+$/) {
239713214adfSAndy Whitcroft
2398d8aaf121SAndy Whitcroft				# ; should have either the end of line or a space or \ after it
239913214adfSAndy Whitcroft				} elsif ($op eq ';') {
2400cf655043SAndy Whitcroft					if ($ctx !~ /.x[WEBC]/ &&
2401cf655043SAndy Whitcroft					    $cc !~ /^\\/ && $cc !~ /^;/) {
2402000d1cc1SJoe Perches						ERROR("SPACING",
2403000d1cc1SJoe Perches						      "space required after that '$op' $at\n" . $hereptr);
2404d8aaf121SAndy Whitcroft					}
2405d8aaf121SAndy Whitcroft
2406d8aaf121SAndy Whitcroft				# // is a comment
2407d8aaf121SAndy Whitcroft				} elsif ($op eq '//') {
24080a920b5bSAndy Whitcroft
24091f65f947SAndy Whitcroft				# No spaces for:
24101f65f947SAndy Whitcroft				#   ->
24111f65f947SAndy Whitcroft				#   :   when part of a bitfield
24121f65f947SAndy Whitcroft				} elsif ($op eq '->' || $opv eq ':B') {
24134a0df2efSAndy Whitcroft					if ($ctx =~ /Wx.|.xW/) {
2414000d1cc1SJoe Perches						ERROR("SPACING",
2415000d1cc1SJoe Perches						      "spaces prohibited around that '$op' $at\n" . $hereptr);
24160a920b5bSAndy Whitcroft					}
24170a920b5bSAndy Whitcroft
24180a920b5bSAndy Whitcroft				# , must have a space on the right.
24190a920b5bSAndy Whitcroft				} elsif ($op eq ',') {
2420cf655043SAndy Whitcroft					if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
2421000d1cc1SJoe Perches						ERROR("SPACING",
2422000d1cc1SJoe Perches						      "space required after that '$op' $at\n" . $hereptr);
24230a920b5bSAndy Whitcroft					}
24240a920b5bSAndy Whitcroft
24259c0ca6f9SAndy Whitcroft				# '*' as part of a type definition -- reported already.
242674048ed8SAndy Whitcroft				} elsif ($opv eq '*_') {
24279c0ca6f9SAndy Whitcroft					#warn "'*' is part of type\n";
24289c0ca6f9SAndy Whitcroft
24299c0ca6f9SAndy Whitcroft				# unary operators should have a space before and
24309c0ca6f9SAndy Whitcroft				# none after.  May be left adjacent to another
24319c0ca6f9SAndy Whitcroft				# unary operator, or a cast
24329c0ca6f9SAndy Whitcroft				} elsif ($op eq '!' || $op eq '~' ||
243374048ed8SAndy Whitcroft					 $opv eq '*U' || $opv eq '-U' ||
24340d413866SAndy Whitcroft					 $opv eq '&U' || $opv eq '&&U') {
2435cf655043SAndy Whitcroft					if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
2436000d1cc1SJoe Perches						ERROR("SPACING",
2437000d1cc1SJoe Perches						      "space required before that '$op' $at\n" . $hereptr);
24380a920b5bSAndy Whitcroft					}
2439a3340b35SAndy Whitcroft					if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
2440171ae1a4SAndy Whitcroft						# A unary '*' may be const
2441171ae1a4SAndy Whitcroft
2442171ae1a4SAndy Whitcroft					} elsif ($ctx =~ /.xW/) {
2443000d1cc1SJoe Perches						ERROR("SPACING",
2444000d1cc1SJoe Perches						      "space prohibited after that '$op' $at\n" . $hereptr);
24450a920b5bSAndy Whitcroft					}
24460a920b5bSAndy Whitcroft
24470a920b5bSAndy Whitcroft				# unary ++ and unary -- are allowed no space on one side.
24480a920b5bSAndy Whitcroft				} elsif ($op eq '++' or $op eq '--') {
2449773647a0SAndy Whitcroft					if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
2450000d1cc1SJoe Perches						ERROR("SPACING",
2451000d1cc1SJoe Perches						      "space required one side of that '$op' $at\n" . $hereptr);
24520a920b5bSAndy Whitcroft					}
2453773647a0SAndy Whitcroft					if ($ctx =~ /Wx[BE]/ ||
2454773647a0SAndy Whitcroft					    ($ctx =~ /Wx./ && $cc =~ /^;/)) {
2455000d1cc1SJoe Perches						ERROR("SPACING",
2456000d1cc1SJoe Perches						      "space prohibited before that '$op' $at\n" . $hereptr);
2457653d4876SAndy Whitcroft					}
2458773647a0SAndy Whitcroft					if ($ctx =~ /ExW/) {
2459000d1cc1SJoe Perches						ERROR("SPACING",
2460000d1cc1SJoe Perches						      "space prohibited after that '$op' $at\n" . $hereptr);
2461773647a0SAndy Whitcroft					}
2462773647a0SAndy Whitcroft
24630a920b5bSAndy Whitcroft
24640a920b5bSAndy Whitcroft				# << and >> may either have or not have spaces both sides
24659c0ca6f9SAndy Whitcroft				} elsif ($op eq '<<' or $op eq '>>' or
24669c0ca6f9SAndy Whitcroft					 $op eq '&' or $op eq '^' or $op eq '|' or
24679c0ca6f9SAndy Whitcroft					 $op eq '+' or $op eq '-' or
2468c2fdda0dSAndy Whitcroft					 $op eq '*' or $op eq '/' or
2469c2fdda0dSAndy Whitcroft					 $op eq '%')
24700a920b5bSAndy Whitcroft				{
2471773647a0SAndy Whitcroft					if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
2472000d1cc1SJoe Perches						ERROR("SPACING",
2473000d1cc1SJoe Perches						      "need consistent spacing around '$op' $at\n" .
2474de7d4f0eSAndy Whitcroft							$hereptr);
24750a920b5bSAndy Whitcroft					}
24760a920b5bSAndy Whitcroft
24771f65f947SAndy Whitcroft				# A colon needs no spaces before when it is
24781f65f947SAndy Whitcroft				# terminating a case value or a label.
24791f65f947SAndy Whitcroft				} elsif ($opv eq ':C' || $opv eq ':L') {
24801f65f947SAndy Whitcroft					if ($ctx =~ /Wx./) {
2481000d1cc1SJoe Perches						ERROR("SPACING",
2482000d1cc1SJoe Perches						      "space prohibited before that '$op' $at\n" . $hereptr);
24831f65f947SAndy Whitcroft					}
24841f65f947SAndy Whitcroft
24850a920b5bSAndy Whitcroft				# All the others need spaces both sides.
2486cf655043SAndy Whitcroft				} elsif ($ctx !~ /[EWC]x[CWE]/) {
24871f65f947SAndy Whitcroft					my $ok = 0;
24881f65f947SAndy Whitcroft
248922f2a2efSAndy Whitcroft					# Ignore email addresses <foo@bar>
24901f65f947SAndy Whitcroft					if (($op eq '<' &&
24911f65f947SAndy Whitcroft					     $cc =~ /^\S+\@\S+>/) ||
24921f65f947SAndy Whitcroft					    ($op eq '>' &&
24931f65f947SAndy Whitcroft					     $ca =~ /<\S+\@\S+$/))
24941f65f947SAndy Whitcroft					{
24951f65f947SAndy Whitcroft					    	$ok = 1;
24961f65f947SAndy Whitcroft					}
24971f65f947SAndy Whitcroft
24981f65f947SAndy Whitcroft					# Ignore ?:
24991f65f947SAndy Whitcroft					if (($opv eq ':O' && $ca =~ /\?$/) ||
25001f65f947SAndy Whitcroft					    ($op eq '?' && $cc =~ /^:/)) {
25011f65f947SAndy Whitcroft					    	$ok = 1;
25021f65f947SAndy Whitcroft					}
25031f65f947SAndy Whitcroft
25041f65f947SAndy Whitcroft					if ($ok == 0) {
2505000d1cc1SJoe Perches						ERROR("SPACING",
2506000d1cc1SJoe Perches						      "spaces required around that '$op' $at\n" . $hereptr);
25070a920b5bSAndy Whitcroft					}
250822f2a2efSAndy Whitcroft				}
25094a0df2efSAndy Whitcroft				$off += length($elements[$n + 1]);
25100a920b5bSAndy Whitcroft			}
25110a920b5bSAndy Whitcroft		}
25120a920b5bSAndy Whitcroft
2513f0a594c1SAndy Whitcroft# check for multiple assignments
2514f0a594c1SAndy Whitcroft		if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
2515000d1cc1SJoe Perches			CHK("MULTIPLE_ASSIGNMENTS",
2516000d1cc1SJoe Perches			    "multiple assignments should be avoided\n" . $herecurr);
2517f0a594c1SAndy Whitcroft		}
2518f0a594c1SAndy Whitcroft
251922f2a2efSAndy Whitcroft## # check for multiple declarations, allowing for a function declaration
252022f2a2efSAndy Whitcroft## # continuation.
252122f2a2efSAndy Whitcroft## 		if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
252222f2a2efSAndy Whitcroft## 		    $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
252322f2a2efSAndy Whitcroft##
252422f2a2efSAndy Whitcroft## 			# Remove any bracketed sections to ensure we do not
252522f2a2efSAndy Whitcroft## 			# falsly report the parameters of functions.
252622f2a2efSAndy Whitcroft## 			my $ln = $line;
252722f2a2efSAndy Whitcroft## 			while ($ln =~ s/\([^\(\)]*\)//g) {
252822f2a2efSAndy Whitcroft## 			}
252922f2a2efSAndy Whitcroft## 			if ($ln =~ /,/) {
2530000d1cc1SJoe Perches## 				WARN("MULTIPLE_DECLARATION",
2531000d1cc1SJoe Perches##				     "declaring multiple variables together should be avoided\n" . $herecurr);
253222f2a2efSAndy Whitcroft## 			}
253322f2a2efSAndy Whitcroft## 		}
2534f0a594c1SAndy Whitcroft
25350a920b5bSAndy Whitcroft#need space before brace following if, while, etc
253622f2a2efSAndy Whitcroft		if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
253722f2a2efSAndy Whitcroft		    $line =~ /do{/) {
2538000d1cc1SJoe Perches			ERROR("SPACING",
2539000d1cc1SJoe Perches			      "space required before the open brace '{'\n" . $herecurr);
2540de7d4f0eSAndy Whitcroft		}
2541de7d4f0eSAndy Whitcroft
2542de7d4f0eSAndy Whitcroft# closing brace should have a space following it when it has anything
2543de7d4f0eSAndy Whitcroft# on the line
2544de7d4f0eSAndy Whitcroft		if ($line =~ /}(?!(?:,|;|\)))\S/) {
2545000d1cc1SJoe Perches			ERROR("SPACING",
2546000d1cc1SJoe Perches			      "space required after that close brace '}'\n" . $herecurr);
25470a920b5bSAndy Whitcroft		}
25480a920b5bSAndy Whitcroft
254922f2a2efSAndy Whitcroft# check spacing on square brackets
255022f2a2efSAndy Whitcroft		if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
2551000d1cc1SJoe Perches			ERROR("SPACING",
2552000d1cc1SJoe Perches			      "space prohibited after that open square bracket '['\n" . $herecurr);
255322f2a2efSAndy Whitcroft		}
255422f2a2efSAndy Whitcroft		if ($line =~ /\s\]/) {
2555000d1cc1SJoe Perches			ERROR("SPACING",
2556000d1cc1SJoe Perches			      "space prohibited before that close square bracket ']'\n" . $herecurr);
255722f2a2efSAndy Whitcroft		}
255822f2a2efSAndy Whitcroft
2559c45dcabdSAndy Whitcroft# check spacing on parentheses
25609c0ca6f9SAndy Whitcroft		if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
25619c0ca6f9SAndy Whitcroft		    $line !~ /for\s*\(\s+;/) {
2562000d1cc1SJoe Perches			ERROR("SPACING",
2563000d1cc1SJoe Perches			      "space prohibited after that open parenthesis '('\n" . $herecurr);
256422f2a2efSAndy Whitcroft		}
256513214adfSAndy Whitcroft		if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
2566c45dcabdSAndy Whitcroft		    $line !~ /for\s*\(.*;\s+\)/ &&
2567c45dcabdSAndy Whitcroft		    $line !~ /:\s+\)/) {
2568000d1cc1SJoe Perches			ERROR("SPACING",
2569000d1cc1SJoe Perches			      "space prohibited before that close parenthesis ')'\n" . $herecurr);
257022f2a2efSAndy Whitcroft		}
257122f2a2efSAndy Whitcroft
25720a920b5bSAndy Whitcroft#goto labels aren't indented, allow a single space however
25734a0df2efSAndy Whitcroft		if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
25740a920b5bSAndy Whitcroft		   !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
2575000d1cc1SJoe Perches			WARN("INDENTED_LABEL",
2576000d1cc1SJoe Perches			     "labels should not be indented\n" . $herecurr);
25770a920b5bSAndy Whitcroft		}
25780a920b5bSAndy Whitcroft
2579c45dcabdSAndy Whitcroft# Return is not a function.
2580c45dcabdSAndy Whitcroft		if (defined($stat) && $stat =~ /^.\s*return(\s*)(\(.*);/s) {
2581c45dcabdSAndy Whitcroft			my $spacing = $1;
2582c45dcabdSAndy Whitcroft			my $value = $2;
2583c45dcabdSAndy Whitcroft
258486f9d059SAndy Whitcroft			# Flatten any parentheses
2585fb2d2c1bSAndy Whitcroft			$value =~ s/\(/ \(/g;
2586fb2d2c1bSAndy Whitcroft			$value =~ s/\)/\) /g;
258763f17f89SAndy Whitcroft			while ($value =~ s/\[[^\{\}]*\]/1/ ||
258863f17f89SAndy Whitcroft			       $value !~ /(?:$Ident|-?$Constant)\s*
258963f17f89SAndy Whitcroft					     $Compare\s*
259063f17f89SAndy Whitcroft					     (?:$Ident|-?$Constant)/x &&
259163f17f89SAndy Whitcroft			       $value =~ s/\([^\(\)]*\)/1/) {
2592c45dcabdSAndy Whitcroft			}
2593fb2d2c1bSAndy Whitcroft#print "value<$value>\n";
2594fb2d2c1bSAndy Whitcroft			if ($value =~ /^\s*(?:$Ident|-?$Constant)\s*$/) {
2595000d1cc1SJoe Perches				ERROR("RETURN_PARENTHESES",
2596000d1cc1SJoe Perches				      "return is not a function, parentheses are not required\n" . $herecurr);
2597c45dcabdSAndy Whitcroft
2598c45dcabdSAndy Whitcroft			} elsif ($spacing !~ /\s+/) {
2599000d1cc1SJoe Perches				ERROR("SPACING",
2600000d1cc1SJoe Perches				      "space required before the open parenthesis '('\n" . $herecurr);
2601c45dcabdSAndy Whitcroft			}
2602c45dcabdSAndy Whitcroft		}
260353a3c448SAndy Whitcroft# Return of what appears to be an errno should normally be -'ve
260453a3c448SAndy Whitcroft		if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) {
260553a3c448SAndy Whitcroft			my $name = $1;
260653a3c448SAndy Whitcroft			if ($name ne 'EOF' && $name ne 'ERROR') {
2607000d1cc1SJoe Perches				WARN("USE_NEGATIVE_ERRNO",
2608000d1cc1SJoe Perches				     "return of an errno should typically be -ve (return -$1)\n" . $herecurr);
260953a3c448SAndy Whitcroft			}
261053a3c448SAndy Whitcroft		}
2611c45dcabdSAndy Whitcroft
26127d2367afSJoe Perches# typecasts on min/max could be min_t/max_t
26137d2367afSJoe Perches		if ($line =~ /^\+(?:.*?)\b(min|max)\s*\($Typecast{0,1}($LvalOrFunc)\s*,\s*$Typecast{0,1}($LvalOrFunc)\s*\)/) {
26147d2367afSJoe Perches			if (defined $2 || defined $8) {
26157d2367afSJoe Perches				my $call = $1;
26167d2367afSJoe Perches				my $cast1 = deparenthesize($2);
26177d2367afSJoe Perches				my $arg1 = $3;
26187d2367afSJoe Perches				my $cast2 = deparenthesize($8);
26197d2367afSJoe Perches				my $arg2 = $9;
26207d2367afSJoe Perches				my $cast;
26217d2367afSJoe Perches
26227d2367afSJoe Perches				if ($cast1 ne "" && $cast2 ne "") {
26237d2367afSJoe Perches					$cast = "$cast1 or $cast2";
26247d2367afSJoe Perches				} elsif ($cast1 ne "") {
26257d2367afSJoe Perches					$cast = $cast1;
26267d2367afSJoe Perches				} else {
26277d2367afSJoe Perches					$cast = $cast2;
26287d2367afSJoe Perches				}
262930ecad51SHui Zhu				WARN("MINMAX",
263030ecad51SHui Zhu				     "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . $herecurr);
26317d2367afSJoe Perches			}
26327d2367afSJoe Perches		}
26337d2367afSJoe Perches
26340a920b5bSAndy Whitcroft# Need a space before open parenthesis after if, while etc
26354a0df2efSAndy Whitcroft		if ($line=~/\b(if|while|for|switch)\(/) {
2636000d1cc1SJoe Perches			ERROR("SPACING", "space required before the open parenthesis '('\n" . $herecurr);
26370a920b5bSAndy Whitcroft		}
26380a920b5bSAndy Whitcroft
2639f5fe35ddSAndy Whitcroft# Check for illegal assignment in if conditional -- and check for trailing
2640f5fe35ddSAndy Whitcroft# statements after the conditional.
2641170d3a22SAndy Whitcroft		if ($line =~ /do\s*(?!{)/) {
2642170d3a22SAndy Whitcroft			my ($stat_next) = ctx_statement_block($line_nr_next,
2643170d3a22SAndy Whitcroft						$remain_next, $off_next);
2644170d3a22SAndy Whitcroft			$stat_next =~ s/\n./\n /g;
2645170d3a22SAndy Whitcroft			##print "stat<$stat> stat_next<$stat_next>\n";
2646170d3a22SAndy Whitcroft
2647170d3a22SAndy Whitcroft			if ($stat_next =~ /^\s*while\b/) {
2648170d3a22SAndy Whitcroft				# If the statement carries leading newlines,
2649170d3a22SAndy Whitcroft				# then count those as offsets.
2650170d3a22SAndy Whitcroft				my ($whitespace) =
2651170d3a22SAndy Whitcroft					($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
2652170d3a22SAndy Whitcroft				my $offset =
2653170d3a22SAndy Whitcroft					statement_rawlines($whitespace) - 1;
2654170d3a22SAndy Whitcroft
2655170d3a22SAndy Whitcroft				$suppress_whiletrailers{$line_nr_next +
2656170d3a22SAndy Whitcroft								$offset} = 1;
2657170d3a22SAndy Whitcroft			}
2658170d3a22SAndy Whitcroft		}
2659170d3a22SAndy Whitcroft		if (!defined $suppress_whiletrailers{$linenr} &&
2660170d3a22SAndy Whitcroft		    $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
2661171ae1a4SAndy Whitcroft			my ($s, $c) = ($stat, $cond);
26628905a67cSAndy Whitcroft
2663b53c8e10SAndy Whitcroft			if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
2664000d1cc1SJoe Perches				ERROR("ASSIGN_IN_IF",
2665000d1cc1SJoe Perches				      "do not use assignment in if condition\n" . $herecurr);
26668905a67cSAndy Whitcroft			}
26678905a67cSAndy Whitcroft
26688905a67cSAndy Whitcroft			# Find out what is on the end of the line after the
26698905a67cSAndy Whitcroft			# conditional.
2670773647a0SAndy Whitcroft			substr($s, 0, length($c), '');
26718905a67cSAndy Whitcroft			$s =~ s/\n.*//g;
267213214adfSAndy Whitcroft			$s =~ s/$;//g; 	# Remove any comments
267353210168SAndy Whitcroft			if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
267453210168SAndy Whitcroft			    $c !~ /}\s*while\s*/)
2675773647a0SAndy Whitcroft			{
2676bb44ad39SAndy Whitcroft				# Find out how long the conditional actually is.
2677bb44ad39SAndy Whitcroft				my @newlines = ($c =~ /\n/gs);
2678bb44ad39SAndy Whitcroft				my $cond_lines = 1 + $#newlines;
267942bdf74cSHidetoshi Seto				my $stat_real = '';
2680bb44ad39SAndy Whitcroft
268142bdf74cSHidetoshi Seto				$stat_real = raw_line($linenr, $cond_lines)
268242bdf74cSHidetoshi Seto							. "\n" if ($cond_lines);
2683bb44ad39SAndy Whitcroft				if (defined($stat_real) && $cond_lines > 1) {
2684bb44ad39SAndy Whitcroft					$stat_real = "[...]\n$stat_real";
2685bb44ad39SAndy Whitcroft				}
2686bb44ad39SAndy Whitcroft
2687000d1cc1SJoe Perches				ERROR("TRAILING_STATEMENTS",
2688000d1cc1SJoe Perches				      "trailing statements should be on next line\n" . $herecurr . $stat_real);
26898905a67cSAndy Whitcroft			}
26908905a67cSAndy Whitcroft		}
26918905a67cSAndy Whitcroft
269213214adfSAndy Whitcroft# Check for bitwise tests written as boolean
269313214adfSAndy Whitcroft		if ($line =~ /
269413214adfSAndy Whitcroft			(?:
269513214adfSAndy Whitcroft				(?:\[|\(|\&\&|\|\|)
269613214adfSAndy Whitcroft				\s*0[xX][0-9]+\s*
269713214adfSAndy Whitcroft				(?:\&\&|\|\|)
269813214adfSAndy Whitcroft			|
269913214adfSAndy Whitcroft				(?:\&\&|\|\|)
270013214adfSAndy Whitcroft				\s*0[xX][0-9]+\s*
270113214adfSAndy Whitcroft				(?:\&\&|\|\||\)|\])
270213214adfSAndy Whitcroft			)/x)
270313214adfSAndy Whitcroft		{
2704000d1cc1SJoe Perches			WARN("HEXADECIMAL_BOOLEAN_TEST",
2705000d1cc1SJoe Perches			     "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
270613214adfSAndy Whitcroft		}
270713214adfSAndy Whitcroft
27088905a67cSAndy Whitcroft# if and else should not have general statements after it
270913214adfSAndy Whitcroft		if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
271013214adfSAndy Whitcroft			my $s = $1;
271113214adfSAndy Whitcroft			$s =~ s/$;//g; 	# Remove any comments
271213214adfSAndy Whitcroft			if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
2713000d1cc1SJoe Perches				ERROR("TRAILING_STATEMENTS",
2714000d1cc1SJoe Perches				      "trailing statements should be on next line\n" . $herecurr);
27150a920b5bSAndy Whitcroft			}
271613214adfSAndy Whitcroft		}
271739667782SAndy Whitcroft# if should not continue a brace
271839667782SAndy Whitcroft		if ($line =~ /}\s*if\b/) {
2719000d1cc1SJoe Perches			ERROR("TRAILING_STATEMENTS",
2720000d1cc1SJoe Perches			      "trailing statements should be on next line\n" .
272139667782SAndy Whitcroft				$herecurr);
272239667782SAndy Whitcroft		}
2723a1080bf8SAndy Whitcroft# case and default should not have general statements after them
2724a1080bf8SAndy Whitcroft		if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
2725a1080bf8SAndy Whitcroft		    $line !~ /\G(?:
27263fef12d6SAndy Whitcroft			(?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
2727a1080bf8SAndy Whitcroft			\s*return\s+
2728a1080bf8SAndy Whitcroft		    )/xg)
2729a1080bf8SAndy Whitcroft		{
2730000d1cc1SJoe Perches			ERROR("TRAILING_STATEMENTS",
2731000d1cc1SJoe Perches			      "trailing statements should be on next line\n" . $herecurr);
2732a1080bf8SAndy Whitcroft		}
27330a920b5bSAndy Whitcroft
27340a920b5bSAndy Whitcroft		# Check for }<nl>else {, these must be at the same
27350a920b5bSAndy Whitcroft		# indent level to be relevant to each other.
27360a920b5bSAndy Whitcroft		if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
27370a920b5bSAndy Whitcroft						$previndent == $indent) {
2738000d1cc1SJoe Perches			ERROR("ELSE_AFTER_BRACE",
2739000d1cc1SJoe Perches			      "else should follow close brace '}'\n" . $hereprev);
27400a920b5bSAndy Whitcroft		}
27410a920b5bSAndy Whitcroft
2742c2fdda0dSAndy Whitcroft		if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and
2743c2fdda0dSAndy Whitcroft						$previndent == $indent) {
2744c2fdda0dSAndy Whitcroft			my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
2745c2fdda0dSAndy Whitcroft
2746c2fdda0dSAndy Whitcroft			# Find out what is on the end of the line after the
2747c2fdda0dSAndy Whitcroft			# conditional.
2748773647a0SAndy Whitcroft			substr($s, 0, length($c), '');
2749c2fdda0dSAndy Whitcroft			$s =~ s/\n.*//g;
2750c2fdda0dSAndy Whitcroft
2751c2fdda0dSAndy Whitcroft			if ($s =~ /^\s*;/) {
2752000d1cc1SJoe Perches				ERROR("WHILE_AFTER_BRACE",
2753000d1cc1SJoe Perches				      "while should follow close brace '}'\n" . $hereprev);
2754c2fdda0dSAndy Whitcroft			}
2755c2fdda0dSAndy Whitcroft		}
2756c2fdda0dSAndy Whitcroft
27570a920b5bSAndy Whitcroft#studly caps, commented out until figure out how to distinguish between use of existing and adding new
27580a920b5bSAndy Whitcroft#		if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) {
27590a920b5bSAndy Whitcroft#		    print "No studly caps, use _\n";
27600a920b5bSAndy Whitcroft#		    print "$herecurr";
27610a920b5bSAndy Whitcroft#		    $clean = 0;
27620a920b5bSAndy Whitcroft#		}
27630a920b5bSAndy Whitcroft
27640a920b5bSAndy Whitcroft#no spaces allowed after \ in define
2765c45dcabdSAndy Whitcroft		if ($line=~/\#\s*define.*\\\s$/) {
2766000d1cc1SJoe Perches			WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
2767000d1cc1SJoe Perches			     "Whitepspace after \\ makes next lines useless\n" . $herecurr);
27680a920b5bSAndy Whitcroft		}
27690a920b5bSAndy Whitcroft
2770653d4876SAndy Whitcroft#warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
2771c45dcabdSAndy Whitcroft		if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
2772e09dec48SAndy Whitcroft			my $file = "$1.h";
2773e09dec48SAndy Whitcroft			my $checkfile = "include/linux/$file";
2774e09dec48SAndy Whitcroft			if (-f "$root/$checkfile" &&
2775e09dec48SAndy Whitcroft			    $realfile ne $checkfile &&
27767840a94cSWolfram Sang			    $1 !~ /$allowed_asm_includes/)
2777c45dcabdSAndy Whitcroft			{
2778e09dec48SAndy Whitcroft				if ($realfile =~ m{^arch/}) {
2779000d1cc1SJoe Perches					CHK("ARCH_INCLUDE_LINUX",
2780000d1cc1SJoe Perches					    "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
2781e09dec48SAndy Whitcroft				} else {
2782000d1cc1SJoe Perches					WARN("INCLUDE_LINUX",
2783000d1cc1SJoe Perches					     "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
2784e09dec48SAndy Whitcroft				}
27850a920b5bSAndy Whitcroft			}
27860a920b5bSAndy Whitcroft		}
27870a920b5bSAndy Whitcroft
2788653d4876SAndy Whitcroft# multi-statement macros should be enclosed in a do while loop, grab the
2789653d4876SAndy Whitcroft# first statement and ensure its the whole macro if its not enclosed
2790cf655043SAndy Whitcroft# in a known good container
2791b8f96a31SAndy Whitcroft		if ($realfile !~ m@/vmlinux.lds.h$@ &&
2792b8f96a31SAndy Whitcroft		    $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
2793d8aaf121SAndy Whitcroft			my $ln = $linenr;
2794d8aaf121SAndy Whitcroft			my $cnt = $realcnt;
2795c45dcabdSAndy Whitcroft			my ($off, $dstat, $dcond, $rest);
2796c45dcabdSAndy Whitcroft			my $ctx = '';
2797c45dcabdSAndy Whitcroft			($dstat, $dcond, $ln, $cnt, $off) =
2798f74bd194SAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0);
2799f74bd194SAndy Whitcroft			$ctx = $dstat;
2800c45dcabdSAndy Whitcroft			#print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
2801a3bb97a7SAndy Whitcroft			#print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
2802c45dcabdSAndy Whitcroft
2803f74bd194SAndy Whitcroft			$dstat =~ s/^.\s*\#\s*define\s+$Ident(?:\([^\)]*\))?\s*//;
2804292f1a9bSAndy Whitcroft			$dstat =~ s/$;//g;
2805c45dcabdSAndy Whitcroft			$dstat =~ s/\\\n.//g;
2806c45dcabdSAndy Whitcroft			$dstat =~ s/^\s*//s;
2807c45dcabdSAndy Whitcroft			$dstat =~ s/\s*$//s;
2808c45dcabdSAndy Whitcroft
2809c45dcabdSAndy Whitcroft			# Flatten any parentheses and braces
2810bf30d6edSAndy Whitcroft			while ($dstat =~ s/\([^\(\)]*\)/1/ ||
2811bf30d6edSAndy Whitcroft			       $dstat =~ s/\{[^\{\}]*\}/1/ ||
2812bf30d6edSAndy Whitcroft			       $dstat =~ s/\[[^\{\}]*\]/1/)
2813bf30d6edSAndy Whitcroft			{
2814c45dcabdSAndy Whitcroft			}
2815c45dcabdSAndy Whitcroft
2816c45dcabdSAndy Whitcroft			my $exceptions = qr{
2817c45dcabdSAndy Whitcroft				$Declare|
2818c45dcabdSAndy Whitcroft				module_param_named|
2819c45dcabdSAndy Whitcroft				MODULE_PARAM_DESC|
2820c45dcabdSAndy Whitcroft				DECLARE_PER_CPU|
2821c45dcabdSAndy Whitcroft				DEFINE_PER_CPU|
2822383099fdSAndy Whitcroft				__typeof__\(|
282322fd2d3eSStefani Seibold				union|
282422fd2d3eSStefani Seibold				struct|
2825ea71a0a0SAndy Whitcroft				\.$Ident\s*=\s*|
2826ea71a0a0SAndy Whitcroft				^\"|\"$
2827c45dcabdSAndy Whitcroft			}x;
28285eaa20b9SAndy Whitcroft			#print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
2829f74bd194SAndy Whitcroft			if ($dstat ne '' &&
2830f74bd194SAndy Whitcroft			    $dstat !~ /^(?:$Ident|-?$Constant),$/ &&			# 10, // foo(),
2831f74bd194SAndy Whitcroft			    $dstat !~ /^(?:$Ident|-?$Constant);$/ &&			# foo();
2832f74bd194SAndy Whitcroft			    $dstat !~ /^(?:$Ident|-?$Constant)$/ &&			# 10 // foo()
2833f74bd194SAndy Whitcroft			    $dstat !~ /$exceptions/ &&
2834f74bd194SAndy Whitcroft			    $dstat !~ /^\.$Ident\s*=/ &&				# .foo =
2835f74bd194SAndy Whitcroft			    $dstat !~ /^do\s*$Constant\s*while\s*$Constant;$/ &&	# do {...} while (...);
2836f74bd194SAndy Whitcroft			    $dstat !~ /^for\s*$Constant$/ &&				# for (...)
2837f74bd194SAndy Whitcroft			    $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ &&	# for (...) bar()
2838f74bd194SAndy Whitcroft			    $dstat !~ /^do\s*{/ &&					# do {...
2839f74bd194SAndy Whitcroft			    $dstat !~ /^\({/)						# ({...
2840c45dcabdSAndy Whitcroft			{
2841f74bd194SAndy Whitcroft				$ctx =~ s/\n*$//;
2842f74bd194SAndy Whitcroft				my $herectx = $here . "\n";
2843f74bd194SAndy Whitcroft				my $cnt = statement_rawlines($ctx);
2844f74bd194SAndy Whitcroft
2845f74bd194SAndy Whitcroft				for (my $n = 0; $n < $cnt; $n++) {
2846f74bd194SAndy Whitcroft					$herectx .= raw_line($linenr, $n) . "\n";
2847c45dcabdSAndy Whitcroft				}
2848c45dcabdSAndy Whitcroft
2849f74bd194SAndy Whitcroft				if ($dstat =~ /;/) {
2850f74bd194SAndy Whitcroft					ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
2851f74bd194SAndy Whitcroft					      "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
2852f74bd194SAndy Whitcroft				} else {
2853000d1cc1SJoe Perches					ERROR("COMPLEX_MACRO",
2854f74bd194SAndy Whitcroft					      "Macros with complex values should be enclosed in parenthesis\n" . "$herectx");
2855d8aaf121SAndy Whitcroft				}
28560a920b5bSAndy Whitcroft			}
2857653d4876SAndy Whitcroft		}
28580a920b5bSAndy Whitcroft
2859080ba929SMike Frysinger# make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
2860080ba929SMike Frysinger# all assignments may have only one of the following with an assignment:
2861080ba929SMike Frysinger#	.
2862080ba929SMike Frysinger#	ALIGN(...)
2863080ba929SMike Frysinger#	VMLINUX_SYMBOL(...)
2864080ba929SMike Frysinger		if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
2865000d1cc1SJoe Perches			WARN("MISSING_VMLINUX_SYMBOL",
2866000d1cc1SJoe Perches			     "vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
2867080ba929SMike Frysinger		}
2868080ba929SMike Frysinger
2869f0a594c1SAndy Whitcroft# check for redundant bracing round if etc
287013214adfSAndy Whitcroft		if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
287113214adfSAndy Whitcroft			my ($level, $endln, @chunks) =
2872cf655043SAndy Whitcroft				ctx_statement_full($linenr, $realcnt, 1);
287313214adfSAndy Whitcroft			#print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
2874cf655043SAndy Whitcroft			#print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
2875cf655043SAndy Whitcroft			if ($#chunks > 0 && $level == 0) {
287613214adfSAndy Whitcroft				my $allowed = 0;
287713214adfSAndy Whitcroft				my $seen = 0;
2878773647a0SAndy Whitcroft				my $herectx = $here . "\n";
2879cf655043SAndy Whitcroft				my $ln = $linenr - 1;
288013214adfSAndy Whitcroft				for my $chunk (@chunks) {
288113214adfSAndy Whitcroft					my ($cond, $block) = @{$chunk};
288213214adfSAndy Whitcroft
2883773647a0SAndy Whitcroft					# If the condition carries leading newlines, then count those as offsets.
2884773647a0SAndy Whitcroft					my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
2885773647a0SAndy Whitcroft					my $offset = statement_rawlines($whitespace) - 1;
2886773647a0SAndy Whitcroft
2887773647a0SAndy Whitcroft					#print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
2888773647a0SAndy Whitcroft
2889773647a0SAndy Whitcroft					# We have looked at and allowed this specific line.
2890773647a0SAndy Whitcroft					$suppress_ifbraces{$ln + $offset} = 1;
2891773647a0SAndy Whitcroft
2892773647a0SAndy Whitcroft					$herectx .= "$rawlines[$ln + $offset]\n[...]\n";
2893cf655043SAndy Whitcroft					$ln += statement_rawlines($block) - 1;
2894cf655043SAndy Whitcroft
2895773647a0SAndy Whitcroft					substr($block, 0, length($cond), '');
289613214adfSAndy Whitcroft
289713214adfSAndy Whitcroft					$seen++ if ($block =~ /^\s*{/);
289813214adfSAndy Whitcroft
2899cf655043SAndy Whitcroft					#print "cond<$cond> block<$block> allowed<$allowed>\n";
2900cf655043SAndy Whitcroft					if (statement_lines($cond) > 1) {
2901cf655043SAndy Whitcroft						#print "APW: ALLOWED: cond<$cond>\n";
290213214adfSAndy Whitcroft						$allowed = 1;
290313214adfSAndy Whitcroft					}
290413214adfSAndy Whitcroft					if ($block =~/\b(?:if|for|while)\b/) {
2905cf655043SAndy Whitcroft						#print "APW: ALLOWED: block<$block>\n";
290613214adfSAndy Whitcroft						$allowed = 1;
290713214adfSAndy Whitcroft					}
2908cf655043SAndy Whitcroft					if (statement_block_size($block) > 1) {
2909cf655043SAndy Whitcroft						#print "APW: ALLOWED: lines block<$block>\n";
291013214adfSAndy Whitcroft						$allowed = 1;
291113214adfSAndy Whitcroft					}
291213214adfSAndy Whitcroft				}
291313214adfSAndy Whitcroft				if ($seen && !$allowed) {
2914000d1cc1SJoe Perches					WARN("BRACES",
2915000d1cc1SJoe Perches					     "braces {} are not necessary for any arm of this statement\n" . $herectx);
291613214adfSAndy Whitcroft				}
291713214adfSAndy Whitcroft			}
291813214adfSAndy Whitcroft		}
2919773647a0SAndy Whitcroft		if (!defined $suppress_ifbraces{$linenr - 1} &&
292013214adfSAndy Whitcroft					$line =~ /\b(if|while|for|else)\b/) {
2921cf655043SAndy Whitcroft			my $allowed = 0;
2922f0a594c1SAndy Whitcroft
2923cf655043SAndy Whitcroft			# Check the pre-context.
2924cf655043SAndy Whitcroft			if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
2925cf655043SAndy Whitcroft				#print "APW: ALLOWED: pre<$1>\n";
2926cf655043SAndy Whitcroft				$allowed = 1;
2927f0a594c1SAndy Whitcroft			}
2928773647a0SAndy Whitcroft
2929773647a0SAndy Whitcroft			my ($level, $endln, @chunks) =
2930773647a0SAndy Whitcroft				ctx_statement_full($linenr, $realcnt, $-[0]);
2931773647a0SAndy Whitcroft
2932cf655043SAndy Whitcroft			# Check the condition.
2933cf655043SAndy Whitcroft			my ($cond, $block) = @{$chunks[0]};
2934773647a0SAndy Whitcroft			#print "CHECKING<$linenr> cond<$cond> block<$block>\n";
2935cf655043SAndy Whitcroft			if (defined $cond) {
2936773647a0SAndy Whitcroft				substr($block, 0, length($cond), '');
2937cf655043SAndy Whitcroft			}
2938cf655043SAndy Whitcroft			if (statement_lines($cond) > 1) {
2939cf655043SAndy Whitcroft				#print "APW: ALLOWED: cond<$cond>\n";
2940cf655043SAndy Whitcroft				$allowed = 1;
2941cf655043SAndy Whitcroft			}
2942cf655043SAndy Whitcroft			if ($block =~/\b(?:if|for|while)\b/) {
2943cf655043SAndy Whitcroft				#print "APW: ALLOWED: block<$block>\n";
2944cf655043SAndy Whitcroft				$allowed = 1;
2945cf655043SAndy Whitcroft			}
2946cf655043SAndy Whitcroft			if (statement_block_size($block) > 1) {
2947cf655043SAndy Whitcroft				#print "APW: ALLOWED: lines block<$block>\n";
2948cf655043SAndy Whitcroft				$allowed = 1;
2949cf655043SAndy Whitcroft			}
2950cf655043SAndy Whitcroft			# Check the post-context.
2951cf655043SAndy Whitcroft			if (defined $chunks[1]) {
2952cf655043SAndy Whitcroft				my ($cond, $block) = @{$chunks[1]};
2953cf655043SAndy Whitcroft				if (defined $cond) {
2954773647a0SAndy Whitcroft					substr($block, 0, length($cond), '');
2955cf655043SAndy Whitcroft				}
2956cf655043SAndy Whitcroft				if ($block =~ /^\s*\{/) {
2957cf655043SAndy Whitcroft					#print "APW: ALLOWED: chunk-1 block<$block>\n";
2958cf655043SAndy Whitcroft					$allowed = 1;
2959cf655043SAndy Whitcroft				}
2960cf655043SAndy Whitcroft			}
2961cf655043SAndy Whitcroft			if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
296269932487SJustin P. Mattock				my $herectx = $here . "\n";
2963f055663cSAndy Whitcroft				my $cnt = statement_rawlines($block);
2964cf655043SAndy Whitcroft
2965f055663cSAndy Whitcroft				for (my $n = 0; $n < $cnt; $n++) {
296669932487SJustin P. Mattock					$herectx .= raw_line($linenr, $n) . "\n";
2967cf655043SAndy Whitcroft				}
2968cf655043SAndy Whitcroft
2969000d1cc1SJoe Perches				WARN("BRACES",
2970000d1cc1SJoe Perches				     "braces {} are not necessary for single statement blocks\n" . $herectx);
2971f0a594c1SAndy Whitcroft			}
2972f0a594c1SAndy Whitcroft		}
2973f0a594c1SAndy Whitcroft
2974653d4876SAndy Whitcroft# don't include deprecated include files (uses RAW line)
29754a0df2efSAndy Whitcroft		for my $inc (@dep_includes) {
2976c45dcabdSAndy Whitcroft			if ($rawline =~ m@^.\s*\#\s*include\s*\<$inc>@) {
2977000d1cc1SJoe Perches				ERROR("DEPRECATED_INCLUDE",
2978000d1cc1SJoe Perches				      "Don't use <$inc>: see Documentation/feature-removal-schedule.txt\n" . $herecurr);
29790a920b5bSAndy Whitcroft			}
29800a920b5bSAndy Whitcroft		}
29810a920b5bSAndy Whitcroft
29824a0df2efSAndy Whitcroft# don't use deprecated functions
29834a0df2efSAndy Whitcroft		for my $func (@dep_functions) {
298400df344fSAndy Whitcroft			if ($line =~ /\b$func\b/) {
2985000d1cc1SJoe Perches				ERROR("DEPRECATED_FUNCTION",
2986000d1cc1SJoe Perches				      "Don't use $func(): see Documentation/feature-removal-schedule.txt\n" . $herecurr);
29874a0df2efSAndy Whitcroft			}
29884a0df2efSAndy Whitcroft		}
29894a0df2efSAndy Whitcroft
29904a0df2efSAndy Whitcroft# no volatiles please
29916c72ffaaSAndy Whitcroft		my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
29926c72ffaaSAndy Whitcroft		if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
2993000d1cc1SJoe Perches			WARN("VOLATILE",
2994000d1cc1SJoe Perches			     "Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
29954a0df2efSAndy Whitcroft		}
29964a0df2efSAndy Whitcroft
299700df344fSAndy Whitcroft# warn about #if 0
2998c45dcabdSAndy Whitcroft		if ($line =~ /^.\s*\#\s*if\s+0\b/) {
2999000d1cc1SJoe Perches			CHK("REDUNDANT_CODE",
3000000d1cc1SJoe Perches			    "if this code is redundant consider removing it\n" .
3001de7d4f0eSAndy Whitcroft				$herecurr);
30024a0df2efSAndy Whitcroft		}
30034a0df2efSAndy Whitcroft
3004f0a594c1SAndy Whitcroft# check for needless kfree() checks
3005f0a594c1SAndy Whitcroft		if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
3006f0a594c1SAndy Whitcroft			my $expr = $1;
3007f0a594c1SAndy Whitcroft			if ($line =~ /\bkfree\(\Q$expr\E\);/) {
3008000d1cc1SJoe Perches				WARN("NEEDLESS_KFREE",
3009000d1cc1SJoe Perches				     "kfree(NULL) is safe this check is probably not required\n" . $hereprev);
3010f0a594c1SAndy Whitcroft			}
3011f0a594c1SAndy Whitcroft		}
30124c432a8fSGreg Kroah-Hartman# check for needless usb_free_urb() checks
30134c432a8fSGreg Kroah-Hartman		if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
30144c432a8fSGreg Kroah-Hartman			my $expr = $1;
30154c432a8fSGreg Kroah-Hartman			if ($line =~ /\busb_free_urb\(\Q$expr\E\);/) {
3016000d1cc1SJoe Perches				WARN("NEEDLESS_USB_FREE_URB",
3017000d1cc1SJoe Perches				     "usb_free_urb(NULL) is safe this check is probably not required\n" . $hereprev);
30184c432a8fSGreg Kroah-Hartman			}
30194c432a8fSGreg Kroah-Hartman		}
3020f0a594c1SAndy Whitcroft
30211a15a250SPatrick Pannuto# prefer usleep_range over udelay
30221a15a250SPatrick Pannuto		if ($line =~ /\budelay\s*\(\s*(\w+)\s*\)/) {
30231a15a250SPatrick Pannuto			# ignore udelay's < 10, however
30241a15a250SPatrick Pannuto			if (! (($1 =~ /(\d+)/) && ($1 < 10)) ) {
3025000d1cc1SJoe Perches				CHK("USLEEP_RANGE",
3026000d1cc1SJoe Perches				    "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $line);
30271a15a250SPatrick Pannuto			}
30281a15a250SPatrick Pannuto		}
30291a15a250SPatrick Pannuto
303009ef8725SPatrick Pannuto# warn about unexpectedly long msleep's
303109ef8725SPatrick Pannuto		if ($line =~ /\bmsleep\s*\((\d+)\);/) {
303209ef8725SPatrick Pannuto			if ($1 < 20) {
3033000d1cc1SJoe Perches				WARN("MSLEEP",
3034000d1cc1SJoe Perches				     "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $line);
303509ef8725SPatrick Pannuto			}
303609ef8725SPatrick Pannuto		}
303709ef8725SPatrick Pannuto
303800df344fSAndy Whitcroft# warn about #ifdefs in C files
3039c45dcabdSAndy Whitcroft#		if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
304000df344fSAndy Whitcroft#			print "#ifdef in C files should be avoided\n";
304100df344fSAndy Whitcroft#			print "$herecurr";
304200df344fSAndy Whitcroft#			$clean = 0;
304300df344fSAndy Whitcroft#		}
304400df344fSAndy Whitcroft
304522f2a2efSAndy Whitcroft# warn about spacing in #ifdefs
3046c45dcabdSAndy Whitcroft		if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
3047000d1cc1SJoe Perches			ERROR("SPACING",
3048000d1cc1SJoe Perches			      "exactly one space required after that #$1\n" . $herecurr);
304922f2a2efSAndy Whitcroft		}
305022f2a2efSAndy Whitcroft
30514a0df2efSAndy Whitcroft# check for spinlock_t definitions without a comment.
3052171ae1a4SAndy Whitcroft		if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
3053171ae1a4SAndy Whitcroft		    $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
30544a0df2efSAndy Whitcroft			my $which = $1;
30554a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
3056000d1cc1SJoe Perches				CHK("UNCOMMENTED_DEFINITION",
3057000d1cc1SJoe Perches				    "$1 definition without comment\n" . $herecurr);
30584a0df2efSAndy Whitcroft			}
30594a0df2efSAndy Whitcroft		}
30604a0df2efSAndy Whitcroft# check for memory barriers without a comment.
30614a0df2efSAndy Whitcroft		if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
30624a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
3063000d1cc1SJoe Perches				CHK("MEMORY_BARRIER",
3064000d1cc1SJoe Perches				    "memory barrier without comment\n" . $herecurr);
30654a0df2efSAndy Whitcroft			}
30664a0df2efSAndy Whitcroft		}
30674a0df2efSAndy Whitcroft# check of hardware specific defines
3068c45dcabdSAndy Whitcroft		if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
3069000d1cc1SJoe Perches			CHK("ARCH_DEFINES",
3070000d1cc1SJoe Perches			    "architecture specific defines should be avoided\n" .  $herecurr);
30710a920b5bSAndy Whitcroft		}
3072653d4876SAndy Whitcroft
3073d4977c78STobias Klauser# Check that the storage class is at the beginning of a declaration
3074d4977c78STobias Klauser		if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
3075000d1cc1SJoe Perches			WARN("STORAGE_CLASS",
3076000d1cc1SJoe Perches			     "storage class should be at the beginning of the declaration\n" . $herecurr)
3077d4977c78STobias Klauser		}
3078d4977c78STobias Klauser
3079de7d4f0eSAndy Whitcroft# check the location of the inline attribute, that it is between
3080de7d4f0eSAndy Whitcroft# storage class and type.
30819c0ca6f9SAndy Whitcroft		if ($line =~ /\b$Type\s+$Inline\b/ ||
30829c0ca6f9SAndy Whitcroft		    $line =~ /\b$Inline\s+$Storage\b/) {
3083000d1cc1SJoe Perches			ERROR("INLINE_LOCATION",
3084000d1cc1SJoe Perches			      "inline keyword should sit between storage class and type\n" . $herecurr);
3085de7d4f0eSAndy Whitcroft		}
3086de7d4f0eSAndy Whitcroft
30878905a67cSAndy Whitcroft# Check for __inline__ and __inline, prefer inline
30888905a67cSAndy Whitcroft		if ($line =~ /\b(__inline__|__inline)\b/) {
3089000d1cc1SJoe Perches			WARN("INLINE",
3090000d1cc1SJoe Perches			     "plain inline is preferred over $1\n" . $herecurr);
30918905a67cSAndy Whitcroft		}
30928905a67cSAndy Whitcroft
30933d130fd0SJoe Perches# Check for __attribute__ packed, prefer __packed
30943d130fd0SJoe Perches		if ($line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
3095000d1cc1SJoe Perches			WARN("PREFER_PACKED",
3096000d1cc1SJoe Perches			     "__packed is preferred over __attribute__((packed))\n" . $herecurr);
30973d130fd0SJoe Perches		}
30983d130fd0SJoe Perches
309939b7e287SJoe Perches# Check for __attribute__ aligned, prefer __aligned
310039b7e287SJoe Perches		if ($line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
3101000d1cc1SJoe Perches			WARN("PREFER_ALIGNED",
3102000d1cc1SJoe Perches			     "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
310339b7e287SJoe Perches		}
310439b7e287SJoe Perches
31055f14d3bdSJoe Perches# Check for __attribute__ format(printf, prefer __printf
31065f14d3bdSJoe Perches		if ($line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
31075f14d3bdSJoe Perches			WARN("PREFER_PRINTF",
31085f14d3bdSJoe Perches			     "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr);
31095f14d3bdSJoe Perches		}
31105f14d3bdSJoe Perches
31118f53a9b8SJoe Perches# check for sizeof(&)
31128f53a9b8SJoe Perches		if ($line =~ /\bsizeof\s*\(\s*\&/) {
3113000d1cc1SJoe Perches			WARN("SIZEOF_ADDRESS",
3114000d1cc1SJoe Perches			     "sizeof(& should be avoided\n" . $herecurr);
31158f53a9b8SJoe Perches		}
31168f53a9b8SJoe Perches
3117428e2fdcSJoe Perches# check for line continuations in quoted strings with odd counts of "
3118428e2fdcSJoe Perches		if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) {
3119000d1cc1SJoe Perches			WARN("LINE_CONTINUATIONS",
3120000d1cc1SJoe Perches			     "Avoid line continuations in quoted strings\n" . $herecurr);
3121428e2fdcSJoe Perches		}
3122428e2fdcSJoe Perches
3123*554e165cSAndy Whitcroft# Check for misused memsets
3124*554e165cSAndy Whitcroft		if (defined $stat && $stat =~ /\bmemset\s*\((.*)\)/s) {
3125*554e165cSAndy Whitcroft			my $args = $1;
3126*554e165cSAndy Whitcroft
3127*554e165cSAndy Whitcroft			# Flatten any parentheses and braces
3128*554e165cSAndy Whitcroft			while ($args =~ s/\([^\(\)]*\)/10/s ||
3129*554e165cSAndy Whitcroft			       $args =~ s/\{[^\{\}]*\}/10/s ||
3130*554e165cSAndy Whitcroft			       $args =~ s/\[[^\[\]]*\]/10/s)
3131*554e165cSAndy Whitcroft			{
3132*554e165cSAndy Whitcroft			}
3133*554e165cSAndy Whitcroft			# Extract the simplified arguments.
3134*554e165cSAndy Whitcroft			my ($ms_addr, $ms_val, $ms_size) =
3135*554e165cSAndy Whitcroft						split(/\s*,\s*/, $args);
3136*554e165cSAndy Whitcroft			if ($ms_size =~ /^(0x|)0$/i) {
3137*554e165cSAndy Whitcroft				ERROR("MEMSET",
3138*554e165cSAndy Whitcroft				      "memset size is 3rd argument, not the second.\n" . $herecurr);
3139*554e165cSAndy Whitcroft			} elsif ($ms_size =~ /^(0x|)1$/i) {
3140*554e165cSAndy Whitcroft				WARN("MEMSET",
3141*554e165cSAndy Whitcroft				     "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . $herecurr);
3142*554e165cSAndy Whitcroft			}
3143*554e165cSAndy Whitcroft		}
3144*554e165cSAndy Whitcroft
3145de7d4f0eSAndy Whitcroft# check for new externs in .c files.
3146171ae1a4SAndy Whitcroft		if ($realfile =~ /\.c$/ && defined $stat &&
3147c45dcabdSAndy Whitcroft		    $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
3148171ae1a4SAndy Whitcroft		{
3149c45dcabdSAndy Whitcroft			my $function_name = $1;
3150c45dcabdSAndy Whitcroft			my $paren_space = $2;
3151171ae1a4SAndy Whitcroft
3152171ae1a4SAndy Whitcroft			my $s = $stat;
3153171ae1a4SAndy Whitcroft			if (defined $cond) {
3154171ae1a4SAndy Whitcroft				substr($s, 0, length($cond), '');
3155171ae1a4SAndy Whitcroft			}
3156c45dcabdSAndy Whitcroft			if ($s =~ /^\s*;/ &&
3157c45dcabdSAndy Whitcroft			    $function_name ne 'uninitialized_var')
3158c45dcabdSAndy Whitcroft			{
3159000d1cc1SJoe Perches				WARN("AVOID_EXTERNS",
3160000d1cc1SJoe Perches				     "externs should be avoided in .c files\n" .  $herecurr);
3161de7d4f0eSAndy Whitcroft			}
3162de7d4f0eSAndy Whitcroft
3163171ae1a4SAndy Whitcroft			if ($paren_space =~ /\n/) {
3164000d1cc1SJoe Perches				WARN("FUNCTION_ARGUMENTS",
3165000d1cc1SJoe Perches				     "arguments for function declarations should follow identifier\n" . $herecurr);
3166171ae1a4SAndy Whitcroft			}
31679c9ba34eSAndy Whitcroft
31689c9ba34eSAndy Whitcroft		} elsif ($realfile =~ /\.c$/ && defined $stat &&
31699c9ba34eSAndy Whitcroft		    $stat =~ /^.\s*extern\s+/)
31709c9ba34eSAndy Whitcroft		{
3171000d1cc1SJoe Perches			WARN("AVOID_EXTERNS",
3172000d1cc1SJoe Perches			     "externs should be avoided in .c files\n" .  $herecurr);
3173171ae1a4SAndy Whitcroft		}
3174171ae1a4SAndy Whitcroft
3175de7d4f0eSAndy Whitcroft# checks for new __setup's
3176de7d4f0eSAndy Whitcroft		if ($rawline =~ /\b__setup\("([^"]*)"/) {
3177de7d4f0eSAndy Whitcroft			my $name = $1;
3178de7d4f0eSAndy Whitcroft
3179de7d4f0eSAndy Whitcroft			if (!grep(/$name/, @setup_docs)) {
3180000d1cc1SJoe Perches				CHK("UNDOCUMENTED_SETUP",
3181000d1cc1SJoe Perches				    "__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
3182de7d4f0eSAndy Whitcroft			}
3183653d4876SAndy Whitcroft		}
31849c0ca6f9SAndy Whitcroft
31859c0ca6f9SAndy Whitcroft# check for pointless casting of kmalloc return
3186caf2a54fSJoe Perches		if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) {
3187000d1cc1SJoe Perches			WARN("UNNECESSARY_CASTS",
3188000d1cc1SJoe Perches			     "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
31899c0ca6f9SAndy Whitcroft		}
319013214adfSAndy Whitcroft
3191caf2a54fSJoe Perches# check for multiple semicolons
3192caf2a54fSJoe Perches		if ($line =~ /;\s*;\s*$/) {
3193000d1cc1SJoe Perches		    WARN("ONE_SEMICOLON",
3194000d1cc1SJoe Perches			 "Statements terminations use 1 semicolon\n" . $herecurr);
3195caf2a54fSJoe Perches		}
3196caf2a54fSJoe Perches
319713214adfSAndy Whitcroft# check for gcc specific __FUNCTION__
319813214adfSAndy Whitcroft		if ($line =~ /__FUNCTION__/) {
3199000d1cc1SJoe Perches			WARN("USE_FUNC",
3200000d1cc1SJoe Perches			     "__func__ should be used instead of gcc specific __FUNCTION__\n"  . $herecurr);
320113214adfSAndy Whitcroft		}
3202773647a0SAndy Whitcroft
32034882720bSThomas Gleixner# check for semaphores initialized locked
32044882720bSThomas Gleixner		if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
3205000d1cc1SJoe Perches			WARN("CONSIDER_COMPLETION",
3206000d1cc1SJoe Perches			     "consider using a completion\n" . $herecurr);
32071704f47bSPeter Zijlstra
3208773647a0SAndy Whitcroft		}
320967d0a075SJoe Perches# recommend kstrto* over simple_strto* and strict_strto*
321067d0a075SJoe Perches		if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
3211000d1cc1SJoe Perches			WARN("CONSIDER_KSTRTO",
321267d0a075SJoe Perches			     "$1 is obsolete, use k$3 instead\n" . $herecurr);
3213773647a0SAndy Whitcroft		}
3214f3db6639SMichael Ellerman# check for __initcall(), use device_initcall() explicitly please
3215f3db6639SMichael Ellerman		if ($line =~ /^.\s*__initcall\s*\(/) {
3216000d1cc1SJoe Perches			WARN("USE_DEVICE_INITCALL",
3217000d1cc1SJoe Perches			     "please use device_initcall() instead of __initcall()\n" . $herecurr);
3218f3db6639SMichael Ellerman		}
321979404849SEmese Revfy# check for various ops structs, ensure they are const.
322079404849SEmese Revfy		my $struct_ops = qr{acpi_dock_ops|
322179404849SEmese Revfy				address_space_operations|
322279404849SEmese Revfy				backlight_ops|
322379404849SEmese Revfy				block_device_operations|
322479404849SEmese Revfy				dentry_operations|
322579404849SEmese Revfy				dev_pm_ops|
322679404849SEmese Revfy				dma_map_ops|
322779404849SEmese Revfy				extent_io_ops|
322879404849SEmese Revfy				file_lock_operations|
322979404849SEmese Revfy				file_operations|
323079404849SEmese Revfy				hv_ops|
323179404849SEmese Revfy				ide_dma_ops|
323279404849SEmese Revfy				intel_dvo_dev_ops|
323379404849SEmese Revfy				item_operations|
323479404849SEmese Revfy				iwl_ops|
323579404849SEmese Revfy				kgdb_arch|
323679404849SEmese Revfy				kgdb_io|
323779404849SEmese Revfy				kset_uevent_ops|
323879404849SEmese Revfy				lock_manager_operations|
323979404849SEmese Revfy				microcode_ops|
324079404849SEmese Revfy				mtrr_ops|
324179404849SEmese Revfy				neigh_ops|
324279404849SEmese Revfy				nlmsvc_binding|
324379404849SEmese Revfy				pci_raw_ops|
324479404849SEmese Revfy				pipe_buf_operations|
324579404849SEmese Revfy				platform_hibernation_ops|
324679404849SEmese Revfy				platform_suspend_ops|
324779404849SEmese Revfy				proto_ops|
324879404849SEmese Revfy				rpc_pipe_ops|
324979404849SEmese Revfy				seq_operations|
325079404849SEmese Revfy				snd_ac97_build_ops|
325179404849SEmese Revfy				soc_pcmcia_socket_ops|
325279404849SEmese Revfy				stacktrace_ops|
325379404849SEmese Revfy				sysfs_ops|
325479404849SEmese Revfy				tty_operations|
325579404849SEmese Revfy				usb_mon_operations|
325679404849SEmese Revfy				wd_ops}x;
32576903ffb2SAndy Whitcroft		if ($line !~ /\bconst\b/ &&
325879404849SEmese Revfy		    $line =~ /\bstruct\s+($struct_ops)\b/) {
3259000d1cc1SJoe Perches			WARN("CONST_STRUCT",
3260000d1cc1SJoe Perches			     "struct $1 should normally be const\n" .
32616903ffb2SAndy Whitcroft				$herecurr);
32622b6db5cbSAndy Whitcroft		}
3263773647a0SAndy Whitcroft
3264773647a0SAndy Whitcroft# use of NR_CPUS is usually wrong
3265773647a0SAndy Whitcroft# ignore definitions of NR_CPUS and usage to define arrays as likely right
3266773647a0SAndy Whitcroft		if ($line =~ /\bNR_CPUS\b/ &&
3267c45dcabdSAndy Whitcroft		    $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
3268c45dcabdSAndy Whitcroft		    $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
3269171ae1a4SAndy Whitcroft		    $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
3270171ae1a4SAndy Whitcroft		    $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
3271171ae1a4SAndy Whitcroft		    $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
3272773647a0SAndy Whitcroft		{
3273000d1cc1SJoe Perches			WARN("NR_CPUS",
3274000d1cc1SJoe Perches			     "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
3275773647a0SAndy Whitcroft		}
32769c9ba34eSAndy Whitcroft
32779c9ba34eSAndy Whitcroft# check for %L{u,d,i} in strings
32789c9ba34eSAndy Whitcroft		my $string;
32799c9ba34eSAndy Whitcroft		while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
32809c9ba34eSAndy Whitcroft			$string = substr($rawline, $-[1], $+[1] - $-[1]);
32812a1bc5d5SAndy Whitcroft			$string =~ s/%%/__/g;
32829c9ba34eSAndy Whitcroft			if ($string =~ /(?<!%)%L[udi]/) {
3283000d1cc1SJoe Perches				WARN("PRINTF_L",
3284000d1cc1SJoe Perches				     "\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
32859c9ba34eSAndy Whitcroft				last;
32869c9ba34eSAndy Whitcroft			}
32879c9ba34eSAndy Whitcroft		}
3288691d77b6SAndy Whitcroft
3289691d77b6SAndy Whitcroft# whine mightly about in_atomic
3290691d77b6SAndy Whitcroft		if ($line =~ /\bin_atomic\s*\(/) {
3291691d77b6SAndy Whitcroft			if ($realfile =~ m@^drivers/@) {
3292000d1cc1SJoe Perches				ERROR("IN_ATOMIC",
3293000d1cc1SJoe Perches				      "do not use in_atomic in drivers\n" . $herecurr);
3294f4a87736SAndy Whitcroft			} elsif ($realfile !~ m@^kernel/@) {
3295000d1cc1SJoe Perches				WARN("IN_ATOMIC",
3296000d1cc1SJoe Perches				     "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
3297691d77b6SAndy Whitcroft			}
3298691d77b6SAndy Whitcroft		}
32991704f47bSPeter Zijlstra
33001704f47bSPeter Zijlstra# check for lockdep_set_novalidate_class
33011704f47bSPeter Zijlstra		if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
33021704f47bSPeter Zijlstra		    $line =~ /__lockdep_no_validate__\s*\)/ ) {
33031704f47bSPeter Zijlstra			if ($realfile !~ m@^kernel/lockdep@ &&
33041704f47bSPeter Zijlstra			    $realfile !~ m@^include/linux/lockdep@ &&
33051704f47bSPeter Zijlstra			    $realfile !~ m@^drivers/base/core@) {
3306000d1cc1SJoe Perches				ERROR("LOCKDEP",
3307000d1cc1SJoe Perches				      "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
33081704f47bSPeter Zijlstra			}
33091704f47bSPeter Zijlstra		}
331088f8831cSDave Jones
331188f8831cSDave Jones		if ($line =~ /debugfs_create_file.*S_IWUGO/ ||
331288f8831cSDave Jones		    $line =~ /DEVICE_ATTR.*S_IWUGO/ ) {
3313000d1cc1SJoe Perches			WARN("EXPORTED_WORLD_WRITABLE",
3314000d1cc1SJoe Perches			     "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
331588f8831cSDave Jones		}
331613214adfSAndy Whitcroft	}
331713214adfSAndy Whitcroft
331813214adfSAndy Whitcroft	# If we have no input at all, then there is nothing to report on
331913214adfSAndy Whitcroft	# so just keep quiet.
332013214adfSAndy Whitcroft	if ($#rawlines == -1) {
332113214adfSAndy Whitcroft		exit(0);
33220a920b5bSAndy Whitcroft	}
33230a920b5bSAndy Whitcroft
33248905a67cSAndy Whitcroft	# In mailback mode only produce a report in the negative, for
33258905a67cSAndy Whitcroft	# things that appear to be patches.
33268905a67cSAndy Whitcroft	if ($mailback && ($clean == 1 || !$is_patch)) {
33278905a67cSAndy Whitcroft		exit(0);
33288905a67cSAndy Whitcroft	}
33298905a67cSAndy Whitcroft
33308905a67cSAndy Whitcroft	# This is not a patch, and we are are in 'no-patch' mode so
33318905a67cSAndy Whitcroft	# just keep quiet.
33328905a67cSAndy Whitcroft	if (!$chk_patch && !$is_patch) {
33338905a67cSAndy Whitcroft		exit(0);
33348905a67cSAndy Whitcroft	}
33358905a67cSAndy Whitcroft
33368905a67cSAndy Whitcroft	if (!$is_patch) {
3337000d1cc1SJoe Perches		ERROR("NOT_UNIFIED_DIFF",
3338000d1cc1SJoe Perches		      "Does not appear to be a unified-diff format patch\n");
33390a920b5bSAndy Whitcroft	}
33400a920b5bSAndy Whitcroft	if ($is_patch && $chk_signoff && $signoff == 0) {
3341000d1cc1SJoe Perches		ERROR("MISSING_SIGN_OFF",
3342000d1cc1SJoe Perches		      "Missing Signed-off-by: line(s)\n");
33430a920b5bSAndy Whitcroft	}
33440a920b5bSAndy Whitcroft
3345f0a594c1SAndy Whitcroft	print report_dump();
334613214adfSAndy Whitcroft	if ($summary && !($clean == 1 && $quiet == 1)) {
334713214adfSAndy Whitcroft		print "$filename " if ($summary_file);
33486c72ffaaSAndy Whitcroft		print "total: $cnt_error errors, $cnt_warn warnings, " .
33496c72ffaaSAndy Whitcroft			(($check)? "$cnt_chk checks, " : "") .
33506c72ffaaSAndy Whitcroft			"$cnt_lines lines checked\n";
33518905a67cSAndy Whitcroft		print "\n" if ($quiet == 0);
33526c72ffaaSAndy Whitcroft	}
33538905a67cSAndy Whitcroft
3354d2c0a235SAndy Whitcroft	if ($quiet == 0) {
3355d2c0a235SAndy Whitcroft		# If there were whitespace errors which cleanpatch can fix
3356d2c0a235SAndy Whitcroft		# then suggest that.
3357d2c0a235SAndy Whitcroft		if ($rpt_cleaners) {
3358d2c0a235SAndy Whitcroft			print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n";
3359d2c0a235SAndy Whitcroft			print "      scripts/cleanfile\n\n";
3360b0781216SMike Frysinger			$rpt_cleaners = 0;
3361d2c0a235SAndy Whitcroft		}
3362d2c0a235SAndy Whitcroft	}
3363d2c0a235SAndy Whitcroft
3364000d1cc1SJoe Perches	if (keys %ignore_type) {
3365000d1cc1SJoe Perches	    print "NOTE: Ignored message types:";
3366000d1cc1SJoe Perches	    foreach my $ignore (sort keys %ignore_type) {
3367000d1cc1SJoe Perches		print " $ignore";
3368000d1cc1SJoe Perches	    }
3369000d1cc1SJoe Perches	    print "\n";
3370000d1cc1SJoe Perches	    print "\n" if ($quiet == 0);
3371000d1cc1SJoe Perches	}
3372000d1cc1SJoe Perches
33730a920b5bSAndy Whitcroft	if ($clean == 1 && $quiet == 0) {
3374c2fdda0dSAndy Whitcroft		print "$vname has no obvious style problems and is ready for submission.\n"
33750a920b5bSAndy Whitcroft	}
33760a920b5bSAndy Whitcroft	if ($clean == 0 && $quiet == 0) {
3377000d1cc1SJoe Perches		print << "EOM";
3378000d1cc1SJoe Perches$vname has style problems, please review.
3379000d1cc1SJoe Perches
3380000d1cc1SJoe PerchesIf any of these errors are false positives, please report
3381000d1cc1SJoe Perchesthem to the maintainer, see CHECKPATCH in MAINTAINERS.
3382000d1cc1SJoe PerchesEOM
33830a920b5bSAndy Whitcroft	}
338413214adfSAndy Whitcroft
33850a920b5bSAndy Whitcroft	return $clean;
33860a920b5bSAndy Whitcroft}
3387