xref: /linux-6.15/scripts/checkpatch.pl (revision c707a81d)
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;
9*c707a81dSJoe Perchesuse POSIX;
100a920b5bSAndy Whitcroft
110a920b5bSAndy Whitcroftmy $P = $0;
1200df344fSAndy Whitcroft$P =~ s@.*/@@g;
130a920b5bSAndy Whitcroft
14000d1cc1SJoe Perchesmy $V = '0.32';
150a920b5bSAndy Whitcroft
160a920b5bSAndy Whitcroftuse Getopt::Long qw(:config no_auto_abbrev);
170a920b5bSAndy Whitcroft
180a920b5bSAndy Whitcroftmy $quiet = 0;
190a920b5bSAndy Whitcroftmy $tree = 1;
200a920b5bSAndy Whitcroftmy $chk_signoff = 1;
210a920b5bSAndy Whitcroftmy $chk_patch = 1;
22773647a0SAndy Whitcroftmy $tst_only;
236c72ffaaSAndy Whitcroftmy $emacs = 0;
248905a67cSAndy Whitcroftmy $terse = 0;
256c72ffaaSAndy Whitcroftmy $file = 0;
266c72ffaaSAndy Whitcroftmy $check = 0;
278905a67cSAndy Whitcroftmy $summary = 1;
288905a67cSAndy Whitcroftmy $mailback = 0;
2913214adfSAndy Whitcroftmy $summary_file = 0;
30000d1cc1SJoe Perchesmy $show_types = 0;
313705ce5bSJoe Perchesmy $fix = 0;
326c72ffaaSAndy Whitcroftmy $root;
33c2fdda0dSAndy Whitcroftmy %debug;
34000d1cc1SJoe Perchesmy %ignore_type = ();
353445686aSJoe Perchesmy %camelcase = ();
36000d1cc1SJoe Perchesmy @ignore = ();
3777f5b10aSHannes Edermy $help = 0;
38000d1cc1SJoe Perchesmy $configuration_file = ".checkpatch.conf";
396cd7f386SJoe Perchesmy $max_line_length = 80;
4077f5b10aSHannes Eder
4177f5b10aSHannes Edersub help {
4277f5b10aSHannes Eder	my ($exitcode) = @_;
4377f5b10aSHannes Eder
4477f5b10aSHannes Eder	print << "EOM";
4577f5b10aSHannes EderUsage: $P [OPTION]... [FILE]...
4677f5b10aSHannes EderVersion: $V
4777f5b10aSHannes Eder
4877f5b10aSHannes EderOptions:
4977f5b10aSHannes Eder  -q, --quiet                quiet
5077f5b10aSHannes Eder  --no-tree                  run without a kernel tree
5177f5b10aSHannes Eder  --no-signoff               do not check for 'Signed-off-by' line
5277f5b10aSHannes Eder  --patch                    treat FILE as patchfile (default)
5377f5b10aSHannes Eder  --emacs                    emacs compile window format
5477f5b10aSHannes Eder  --terse                    one line per report
5577f5b10aSHannes Eder  -f, --file                 treat FILE as regular source file
5677f5b10aSHannes Eder  --subjective, --strict     enable more subjective tests
57000d1cc1SJoe Perches  --ignore TYPE(,TYPE2...)   ignore various comma separated message types
586cd7f386SJoe Perches  --max-line-length=n        set the maximum line length, if exceeded, warn
59000d1cc1SJoe Perches  --show-types               show the message "types" in the output
6077f5b10aSHannes Eder  --root=PATH                PATH to the kernel tree root
6177f5b10aSHannes Eder  --no-summary               suppress the per-file summary
6277f5b10aSHannes Eder  --mailback                 only produce a report in case of warnings/errors
6377f5b10aSHannes Eder  --summary-file             include the filename in summary
6477f5b10aSHannes Eder  --debug KEY=[0|1]          turn on/off debugging of KEY, where KEY is one of
6577f5b10aSHannes Eder                             'values', 'possible', 'type', and 'attr' (default
6677f5b10aSHannes Eder                             is all off)
6777f5b10aSHannes Eder  --test-only=WORD           report only warnings/errors containing WORD
6877f5b10aSHannes Eder                             literally
693705ce5bSJoe Perches  --fix                      EXPERIMENTAL - may create horrible results
703705ce5bSJoe Perches                             If correctable single-line errors exist, create
713705ce5bSJoe Perches                             "<inputfile>.EXPERIMENTAL-checkpatch-fixes"
723705ce5bSJoe Perches                             with potential errors corrected to the preferred
733705ce5bSJoe Perches                             checkpatch style
7477f5b10aSHannes Eder  -h, --help, --version      display this help and exit
7577f5b10aSHannes Eder
7677f5b10aSHannes EderWhen FILE is - read standard input.
7777f5b10aSHannes EderEOM
7877f5b10aSHannes Eder
7977f5b10aSHannes Eder	exit($exitcode);
8077f5b10aSHannes Eder}
8177f5b10aSHannes Eder
82000d1cc1SJoe Perchesmy $conf = which_conf($configuration_file);
83000d1cc1SJoe Perchesif (-f $conf) {
84000d1cc1SJoe Perches	my @conf_args;
85000d1cc1SJoe Perches	open(my $conffile, '<', "$conf")
86000d1cc1SJoe Perches	    or warn "$P: Can't find a readable $configuration_file file $!\n";
87000d1cc1SJoe Perches
88000d1cc1SJoe Perches	while (<$conffile>) {
89000d1cc1SJoe Perches		my $line = $_;
90000d1cc1SJoe Perches
91000d1cc1SJoe Perches		$line =~ s/\s*\n?$//g;
92000d1cc1SJoe Perches		$line =~ s/^\s*//g;
93000d1cc1SJoe Perches		$line =~ s/\s+/ /g;
94000d1cc1SJoe Perches
95000d1cc1SJoe Perches		next if ($line =~ m/^\s*#/);
96000d1cc1SJoe Perches		next if ($line =~ m/^\s*$/);
97000d1cc1SJoe Perches
98000d1cc1SJoe Perches		my @words = split(" ", $line);
99000d1cc1SJoe Perches		foreach my $word (@words) {
100000d1cc1SJoe Perches			last if ($word =~ m/^#/);
101000d1cc1SJoe Perches			push (@conf_args, $word);
102000d1cc1SJoe Perches		}
103000d1cc1SJoe Perches	}
104000d1cc1SJoe Perches	close($conffile);
105000d1cc1SJoe Perches	unshift(@ARGV, @conf_args) if @conf_args;
106000d1cc1SJoe Perches}
107000d1cc1SJoe Perches
1080a920b5bSAndy WhitcroftGetOptions(
1096c72ffaaSAndy Whitcroft	'q|quiet+'	=> \$quiet,
1100a920b5bSAndy Whitcroft	'tree!'		=> \$tree,
1110a920b5bSAndy Whitcroft	'signoff!'	=> \$chk_signoff,
1120a920b5bSAndy Whitcroft	'patch!'	=> \$chk_patch,
1136c72ffaaSAndy Whitcroft	'emacs!'	=> \$emacs,
1148905a67cSAndy Whitcroft	'terse!'	=> \$terse,
11577f5b10aSHannes Eder	'f|file!'	=> \$file,
1166c72ffaaSAndy Whitcroft	'subjective!'	=> \$check,
1176c72ffaaSAndy Whitcroft	'strict!'	=> \$check,
118000d1cc1SJoe Perches	'ignore=s'	=> \@ignore,
119000d1cc1SJoe Perches	'show-types!'	=> \$show_types,
1206cd7f386SJoe Perches	'max-line-length=i' => \$max_line_length,
1216c72ffaaSAndy Whitcroft	'root=s'	=> \$root,
1228905a67cSAndy Whitcroft	'summary!'	=> \$summary,
1238905a67cSAndy Whitcroft	'mailback!'	=> \$mailback,
12413214adfSAndy Whitcroft	'summary-file!'	=> \$summary_file,
1253705ce5bSJoe Perches	'fix!'		=> \$fix,
126c2fdda0dSAndy Whitcroft	'debug=s'	=> \%debug,
127773647a0SAndy Whitcroft	'test-only=s'	=> \$tst_only,
12877f5b10aSHannes Eder	'h|help'	=> \$help,
12977f5b10aSHannes Eder	'version'	=> \$help
13077f5b10aSHannes Eder) or help(1);
13177f5b10aSHannes Eder
13277f5b10aSHannes Ederhelp(0) if ($help);
1330a920b5bSAndy Whitcroft
1340a920b5bSAndy Whitcroftmy $exit = 0;
1350a920b5bSAndy Whitcroft
1360a920b5bSAndy Whitcroftif ($#ARGV < 0) {
13777f5b10aSHannes Eder	print "$P: no input files\n";
1380a920b5bSAndy Whitcroft	exit(1);
1390a920b5bSAndy Whitcroft}
1400a920b5bSAndy Whitcroft
141000d1cc1SJoe Perches@ignore = split(/,/, join(',',@ignore));
142000d1cc1SJoe Perchesforeach my $word (@ignore) {
143000d1cc1SJoe Perches	$word =~ s/\s*\n?$//g;
144000d1cc1SJoe Perches	$word =~ s/^\s*//g;
145000d1cc1SJoe Perches	$word =~ s/\s+/ /g;
146000d1cc1SJoe Perches	$word =~ tr/[a-z]/[A-Z]/;
147000d1cc1SJoe Perches
148000d1cc1SJoe Perches	next if ($word =~ m/^\s*#/);
149000d1cc1SJoe Perches	next if ($word =~ m/^\s*$/);
150000d1cc1SJoe Perches
151000d1cc1SJoe Perches	$ignore_type{$word}++;
152000d1cc1SJoe Perches}
153000d1cc1SJoe Perches
154c2fdda0dSAndy Whitcroftmy $dbg_values = 0;
155c2fdda0dSAndy Whitcroftmy $dbg_possible = 0;
1567429c690SAndy Whitcroftmy $dbg_type = 0;
157a1ef277eSAndy Whitcroftmy $dbg_attr = 0;
158c2fdda0dSAndy Whitcroftfor my $key (keys %debug) {
15921caa13cSAndy Whitcroft	## no critic
16021caa13cSAndy Whitcroft	eval "\${dbg_$key} = '$debug{$key}';";
16121caa13cSAndy Whitcroft	die "$@" if ($@);
162c2fdda0dSAndy Whitcroft}
163c2fdda0dSAndy Whitcroft
164d2c0a235SAndy Whitcroftmy $rpt_cleaners = 0;
165d2c0a235SAndy Whitcroft
1668905a67cSAndy Whitcroftif ($terse) {
1678905a67cSAndy Whitcroft	$emacs = 1;
1688905a67cSAndy Whitcroft	$quiet++;
1698905a67cSAndy Whitcroft}
1708905a67cSAndy Whitcroft
1716c72ffaaSAndy Whitcroftif ($tree) {
1726c72ffaaSAndy Whitcroft	if (defined $root) {
1736c72ffaaSAndy Whitcroft		if (!top_of_kernel_tree($root)) {
1746c72ffaaSAndy Whitcroft			die "$P: $root: --root does not point at a valid tree\n";
1756c72ffaaSAndy Whitcroft		}
1766c72ffaaSAndy Whitcroft	} else {
1776c72ffaaSAndy Whitcroft		if (top_of_kernel_tree('.')) {
1786c72ffaaSAndy Whitcroft			$root = '.';
1796c72ffaaSAndy Whitcroft		} elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
1806c72ffaaSAndy Whitcroft						top_of_kernel_tree($1)) {
1816c72ffaaSAndy Whitcroft			$root = $1;
1826c72ffaaSAndy Whitcroft		}
1836c72ffaaSAndy Whitcroft	}
1846c72ffaaSAndy Whitcroft
1856c72ffaaSAndy Whitcroft	if (!defined $root) {
1860a920b5bSAndy Whitcroft		print "Must be run from the top-level dir. of a kernel tree\n";
1870a920b5bSAndy Whitcroft		exit(2);
1880a920b5bSAndy Whitcroft	}
1896c72ffaaSAndy Whitcroft}
1906c72ffaaSAndy Whitcroft
1916c72ffaaSAndy Whitcroftmy $emitted_corrupt = 0;
1926c72ffaaSAndy Whitcroft
1932ceb532bSAndy Whitcroftour $Ident	= qr{
1942ceb532bSAndy Whitcroft			[A-Za-z_][A-Za-z\d_]*
1952ceb532bSAndy Whitcroft			(?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
1962ceb532bSAndy Whitcroft		}x;
1976c72ffaaSAndy Whitcroftour $Storage	= qr{extern|static|asmlinkage};
1986c72ffaaSAndy Whitcroftour $Sparse	= qr{
1996c72ffaaSAndy Whitcroft			__user|
2006c72ffaaSAndy Whitcroft			__kernel|
2016c72ffaaSAndy Whitcroft			__force|
2026c72ffaaSAndy Whitcroft			__iomem|
2036c72ffaaSAndy Whitcroft			__must_check|
2046c72ffaaSAndy Whitcroft			__init_refok|
205417495edSAndy Whitcroft			__kprobes|
206165e72a6SSven Eckelmann			__ref|
207165e72a6SSven Eckelmann			__rcu
2086c72ffaaSAndy Whitcroft		}x;
20952131292SWolfram Sang
21052131292SWolfram Sang# Notes to $Attribute:
21152131292SWolfram Sang# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
2126c72ffaaSAndy Whitcroftour $Attribute	= qr{
2136c72ffaaSAndy Whitcroft			const|
21403f1df7dSJoe Perches			__percpu|
21503f1df7dSJoe Perches			__nocast|
21603f1df7dSJoe Perches			__safe|
21703f1df7dSJoe Perches			__bitwise__|
21803f1df7dSJoe Perches			__packed__|
21903f1df7dSJoe Perches			__packed2__|
22003f1df7dSJoe Perches			__naked|
22103f1df7dSJoe Perches			__maybe_unused|
22203f1df7dSJoe Perches			__always_unused|
22303f1df7dSJoe Perches			__noreturn|
22403f1df7dSJoe Perches			__used|
22503f1df7dSJoe Perches			__cold|
22603f1df7dSJoe Perches			__noclone|
22703f1df7dSJoe Perches			__deprecated|
2286c72ffaaSAndy Whitcroft			__read_mostly|
2296c72ffaaSAndy Whitcroft			__kprobes|
23052131292SWolfram Sang			__(?:mem|cpu|dev|)(?:initdata|initconst|init\b)|
23124e1d81aSAndy Whitcroft			____cacheline_aligned|
23224e1d81aSAndy Whitcroft			____cacheline_aligned_in_smp|
2335fe3af11SAndy Whitcroft			____cacheline_internodealigned_in_smp|
2345fe3af11SAndy Whitcroft			__weak
2356c72ffaaSAndy Whitcroft		  }x;
236c45dcabdSAndy Whitcroftour $Modifier;
2376c72ffaaSAndy Whitcroftour $Inline	= qr{inline|__always_inline|noinline};
2386c72ffaaSAndy Whitcroftour $Member	= qr{->$Ident|\.$Ident|\[[^]]*\]};
2396c72ffaaSAndy Whitcroftour $Lval	= qr{$Ident(?:$Member)*};
2406c72ffaaSAndy Whitcroft
24195e2c602SJoe Perchesour $Int_type	= qr{(?i)llu|ull|ll|lu|ul|l|u};
24295e2c602SJoe Perchesour $Binary	= qr{(?i)0b[01]+$Int_type?};
24395e2c602SJoe Perchesour $Hex	= qr{(?i)0x[0-9a-f]+$Int_type?};
24495e2c602SJoe Perchesour $Int	= qr{[0-9]+$Int_type?};
245326b1ffcSJoe Perchesour $Float_hex	= qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?};
246326b1ffcSJoe Perchesour $Float_dec	= qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?};
247326b1ffcSJoe Perchesour $Float_int	= qr{(?i)[0-9]+e-?[0-9]+[fl]?};
24874349bccSJoe Perchesour $Float	= qr{$Float_hex|$Float_dec|$Float_int};
24995e2c602SJoe Perchesour $Constant	= qr{$Float|$Binary|$Hex|$Int};
250326b1ffcSJoe Perchesour $Assignment	= qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=};
25186f9d059SAndy Whitcroftour $Compare    = qr{<=|>=|==|!=|<|>};
25223f780c9SJoe Perchesour $Arithmetic = qr{\+|-|\*|\/|%};
2536c72ffaaSAndy Whitcroftour $Operators	= qr{
2546c72ffaaSAndy Whitcroft			<=|>=|==|!=|
2556c72ffaaSAndy Whitcroft			=>|->|<<|>>|<|>|!|~|
25623f780c9SJoe Perches			&&|\|\||,|\^|\+\+|--|&|\||$Arithmetic
2576c72ffaaSAndy Whitcroft		  }x;
2586c72ffaaSAndy Whitcroft
2598905a67cSAndy Whitcroftour $NonptrType;
2608905a67cSAndy Whitcroftour $Type;
2618905a67cSAndy Whitcroftour $Declare;
2628905a67cSAndy Whitcroft
26315662b3eSJoe Perchesour $NON_ASCII_UTF8	= qr{
26415662b3eSJoe Perches	[\xC2-\xDF][\x80-\xBF]               # non-overlong 2-byte
265171ae1a4SAndy Whitcroft	|  \xE0[\xA0-\xBF][\x80-\xBF]        # excluding overlongs
266171ae1a4SAndy Whitcroft	| [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}  # straight 3-byte
267171ae1a4SAndy Whitcroft	|  \xED[\x80-\x9F][\x80-\xBF]        # excluding surrogates
268171ae1a4SAndy Whitcroft	|  \xF0[\x90-\xBF][\x80-\xBF]{2}     # planes 1-3
269171ae1a4SAndy Whitcroft	| [\xF1-\xF3][\x80-\xBF]{3}          # planes 4-15
270171ae1a4SAndy Whitcroft	|  \xF4[\x80-\x8F][\x80-\xBF]{2}     # plane 16
271171ae1a4SAndy Whitcroft}x;
272171ae1a4SAndy Whitcroft
27315662b3eSJoe Perchesour $UTF8	= qr{
27415662b3eSJoe Perches	[\x09\x0A\x0D\x20-\x7E]              # ASCII
27515662b3eSJoe Perches	| $NON_ASCII_UTF8
27615662b3eSJoe Perches}x;
27715662b3eSJoe Perches
2788ed22cadSAndy Whitcroftour $typeTypedefs = qr{(?x:
279fb9e9096SAndy Whitcroft	(?:__)?(?:u|s|be|le)(?:8|16|32|64)|
2808ed22cadSAndy Whitcroft	atomic_t
2818ed22cadSAndy Whitcroft)};
2828ed22cadSAndy Whitcroft
283691e669bSJoe Perchesour $logFunctions = qr{(?x:
2846e60c02eSJoe Perches	printk(?:_ratelimited|_once|)|
2857d0b6594SJacob Keller	(?:[a-z0-9]+_){1,2}(?:printk|emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)(?:_ratelimited|_once|)|
2866e60c02eSJoe Perches	WARN(?:_RATELIMIT|_ONCE|)|
287b0531722SJoe Perches	panic|
288b0531722SJoe Perches	MODULE_[A-Z_]+
289691e669bSJoe Perches)};
290691e669bSJoe Perches
29120112475SJoe Perchesour $signature_tags = qr{(?xi:
29220112475SJoe Perches	Signed-off-by:|
29320112475SJoe Perches	Acked-by:|
29420112475SJoe Perches	Tested-by:|
29520112475SJoe Perches	Reviewed-by:|
29620112475SJoe Perches	Reported-by:|
2978543ae12SMugunthan V N	Suggested-by:|
29820112475SJoe Perches	To:|
29920112475SJoe Perches	Cc:
30020112475SJoe Perches)};
30120112475SJoe Perches
3028905a67cSAndy Whitcroftour @typeList = (
3038905a67cSAndy Whitcroft	qr{void},
304c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?char},
305c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?short},
306c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?int},
307c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?long},
308c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?long\s+int},
309c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?long\s+long},
310c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?long\s+long\s+int},
3118905a67cSAndy Whitcroft	qr{unsigned},
3128905a67cSAndy Whitcroft	qr{float},
3138905a67cSAndy Whitcroft	qr{double},
3148905a67cSAndy Whitcroft	qr{bool},
3158905a67cSAndy Whitcroft	qr{struct\s+$Ident},
3168905a67cSAndy Whitcroft	qr{union\s+$Ident},
3178905a67cSAndy Whitcroft	qr{enum\s+$Ident},
3188905a67cSAndy Whitcroft	qr{${Ident}_t},
3198905a67cSAndy Whitcroft	qr{${Ident}_handler},
3208905a67cSAndy Whitcroft	qr{${Ident}_handler_fn},
3218905a67cSAndy Whitcroft);
322c45dcabdSAndy Whitcroftour @modifierList = (
323c45dcabdSAndy Whitcroft	qr{fastcall},
324c45dcabdSAndy Whitcroft);
3258905a67cSAndy Whitcroft
3267840a94cSWolfram Sangour $allowed_asm_includes = qr{(?x:
3277840a94cSWolfram Sang	irq|
3287840a94cSWolfram Sang	memory
3297840a94cSWolfram Sang)};
3307840a94cSWolfram Sang# memory.h: ARM has a custom one
3317840a94cSWolfram Sang
3328905a67cSAndy Whitcroftsub build_types {
333d2172eb5SAndy Whitcroft	my $mods = "(?x:  \n" . join("|\n  ", @modifierList) . "\n)";
334d2172eb5SAndy Whitcroft	my $all = "(?x:  \n" . join("|\n  ", @typeList) . "\n)";
335c8cb2ca3SAndy Whitcroft	$Modifier	= qr{(?:$Attribute|$Sparse|$mods)};
3368905a67cSAndy Whitcroft	$NonptrType	= qr{
337d2172eb5SAndy Whitcroft			(?:$Modifier\s+|const\s+)*
338cf655043SAndy Whitcroft			(?:
3396b48db24SAndy Whitcroft				(?:typeof|__typeof__)\s*\([^\)]*\)|
3408ed22cadSAndy Whitcroft				(?:$typeTypedefs\b)|
341c45dcabdSAndy Whitcroft				(?:${all}\b)
342cf655043SAndy Whitcroft			)
343c8cb2ca3SAndy Whitcroft			(?:\s+$Modifier|\s+const)*
3448905a67cSAndy Whitcroft		  }x;
3458905a67cSAndy Whitcroft	$Type	= qr{
346c45dcabdSAndy Whitcroft			$NonptrType
347b337d8b8SAndy Whitcroft			(?:(?:\s|\*|\[\])+\s*const|(?:\s|\*|\[\])+|(?:\s*\[\s*\])+)?
348c8cb2ca3SAndy Whitcroft			(?:\s+$Inline|\s+$Modifier)*
3498905a67cSAndy Whitcroft		  }x;
3508905a67cSAndy Whitcroft	$Declare	= qr{(?:$Storage\s+)?$Type};
3518905a67cSAndy Whitcroft}
3528905a67cSAndy Whitcroftbuild_types();
3536c72ffaaSAndy Whitcroft
3547d2367afSJoe Perchesour $Typecast	= qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
355d1fe9c09SJoe Perches
356d1fe9c09SJoe Perches# Using $balanced_parens, $LvalOrFunc, or $FuncArg
357d1fe9c09SJoe Perches# requires at least perl version v5.10.0
358d1fe9c09SJoe Perches# Any use must be runtime checked with $^V
359d1fe9c09SJoe Perches
360d1fe9c09SJoe Perchesour $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
361d1fe9c09SJoe Perchesour $LvalOrFunc	= qr{($Lval)\s*($balanced_parens{0,1})\s*};
362d7c76ba7SJoe Perchesour $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant)};
3637d2367afSJoe Perches
3647d2367afSJoe Perchessub deparenthesize {
3657d2367afSJoe Perches	my ($string) = @_;
3667d2367afSJoe Perches	return "" if (!defined($string));
3677d2367afSJoe Perches	$string =~ s@^\s*\(\s*@@g;
3687d2367afSJoe Perches	$string =~ s@\s*\)\s*$@@g;
3697d2367afSJoe Perches	$string =~ s@\s+@ @g;
3707d2367afSJoe Perches	return $string;
3717d2367afSJoe Perches}
3727d2367afSJoe Perches
3733445686aSJoe Perchessub seed_camelcase_file {
3743445686aSJoe Perches	my ($file) = @_;
3753445686aSJoe Perches
3763445686aSJoe Perches	return if (!(-f $file));
3773445686aSJoe Perches
3783445686aSJoe Perches	local $/;
3793445686aSJoe Perches
3803445686aSJoe Perches	open(my $include_file, '<', "$file")
3813445686aSJoe Perches	    or warn "$P: Can't read '$file' $!\n";
3823445686aSJoe Perches	my $text = <$include_file>;
3833445686aSJoe Perches	close($include_file);
3843445686aSJoe Perches
3853445686aSJoe Perches	my @lines = split('\n', $text);
3863445686aSJoe Perches
3873445686aSJoe Perches	foreach my $line (@lines) {
3883445686aSJoe Perches		next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/);
3893445686aSJoe Perches		if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) {
3903445686aSJoe Perches			$camelcase{$1} = 1;
3913445686aSJoe Perches		}
3923445686aSJoe Perches	        elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*\(/) {
3933445686aSJoe Perches			$camelcase{$1} = 1;
3943445686aSJoe Perches		}
3953445686aSJoe Perches	}
3963445686aSJoe Perches}
3973445686aSJoe Perches
3983445686aSJoe Perchesmy $camelcase_seeded = 0;
3993445686aSJoe Perchessub seed_camelcase_includes {
4003445686aSJoe Perches	return if ($camelcase_seeded);
4013445686aSJoe Perches
4023445686aSJoe Perches	my $files;
403*c707a81dSJoe Perches	my $camelcase_cache = "";
404*c707a81dSJoe Perches	my @include_files = ();
405*c707a81dSJoe Perches
406*c707a81dSJoe Perches	$camelcase_seeded = 1;
407351b2a1fSJoe Perches
4083445686aSJoe Perches	if (-d ".git") {
409351b2a1fSJoe Perches		my $git_last_include_commit = `git log --no-merges --pretty=format:"%h%n" -1 -- include`;
410351b2a1fSJoe Perches		chomp $git_last_include_commit;
411*c707a81dSJoe Perches		$camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit";
412*c707a81dSJoe Perches	} else {
413*c707a81dSJoe Perches		my $last_mod_date = 0;
414*c707a81dSJoe Perches		$files = `find $root/include -name "*.h"`;
415*c707a81dSJoe Perches		@include_files = split('\n', $files);
416*c707a81dSJoe Perches		foreach my $file (@include_files) {
417*c707a81dSJoe Perches			my $date = POSIX::strftime("%Y%m%d%H%M",
418*c707a81dSJoe Perches						   localtime((stat $file)[9]));
419*c707a81dSJoe Perches			$last_mod_date = $date if ($last_mod_date < $date);
420*c707a81dSJoe Perches		}
421*c707a81dSJoe Perches		$camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date";
422*c707a81dSJoe Perches	}
423*c707a81dSJoe Perches
424*c707a81dSJoe Perches	if ($camelcase_cache ne "" && -f $camelcase_cache) {
425*c707a81dSJoe Perches		open(my $camelcase_file, '<', "$camelcase_cache")
426*c707a81dSJoe Perches		    or warn "$P: Can't read '$camelcase_cache' $!\n";
427351b2a1fSJoe Perches		while (<$camelcase_file>) {
428351b2a1fSJoe Perches			chomp;
429351b2a1fSJoe Perches			$camelcase{$_} = 1;
430351b2a1fSJoe Perches		}
431351b2a1fSJoe Perches		close($camelcase_file);
432351b2a1fSJoe Perches
433351b2a1fSJoe Perches		return;
434351b2a1fSJoe Perches	}
435*c707a81dSJoe Perches
436*c707a81dSJoe Perches	if (-d ".git") {
437*c707a81dSJoe Perches		$files = `git ls-files "include/*.h"`;
438*c707a81dSJoe Perches		@include_files = split('\n', $files);
4393445686aSJoe Perches	}
440*c707a81dSJoe Perches
4413445686aSJoe Perches	foreach my $file (@include_files) {
4423445686aSJoe Perches		seed_camelcase_file($file);
4433445686aSJoe Perches	}
444351b2a1fSJoe Perches
445*c707a81dSJoe Perches	if ($camelcase_cache ne "") {
446351b2a1fSJoe Perches		unlink glob ".checkpatch-camelcase.*";
447*c707a81dSJoe Perches		open(my $camelcase_file, '>', "$camelcase_cache")
448*c707a81dSJoe Perches		    or warn "$P: Can't write '$camelcase_cache' $!\n";
449351b2a1fSJoe Perches		foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) {
450351b2a1fSJoe Perches			print $camelcase_file ("$_\n");
451351b2a1fSJoe Perches		}
452351b2a1fSJoe Perches		close($camelcase_file);
453351b2a1fSJoe Perches	}
4543445686aSJoe Perches}
4553445686aSJoe Perches
4566c72ffaaSAndy Whitcroft$chk_signoff = 0 if ($file);
4570a920b5bSAndy Whitcroft
45800df344fSAndy Whitcroftmy @rawlines = ();
459c2fdda0dSAndy Whitcroftmy @lines = ();
4603705ce5bSJoe Perchesmy @fixed = ();
461c2fdda0dSAndy Whitcroftmy $vname;
4626c72ffaaSAndy Whitcroftfor my $filename (@ARGV) {
46321caa13cSAndy Whitcroft	my $FILE;
4646c72ffaaSAndy Whitcroft	if ($file) {
46521caa13cSAndy Whitcroft		open($FILE, '-|', "diff -u /dev/null $filename") ||
4666c72ffaaSAndy Whitcroft			die "$P: $filename: diff failed - $!\n";
46721caa13cSAndy Whitcroft	} elsif ($filename eq '-') {
46821caa13cSAndy Whitcroft		open($FILE, '<&STDIN');
4696c72ffaaSAndy Whitcroft	} else {
47021caa13cSAndy Whitcroft		open($FILE, '<', "$filename") ||
4716c72ffaaSAndy Whitcroft			die "$P: $filename: open failed - $!\n";
4726c72ffaaSAndy Whitcroft	}
473c2fdda0dSAndy Whitcroft	if ($filename eq '-') {
474c2fdda0dSAndy Whitcroft		$vname = 'Your patch';
475c2fdda0dSAndy Whitcroft	} else {
476c2fdda0dSAndy Whitcroft		$vname = $filename;
477c2fdda0dSAndy Whitcroft	}
47821caa13cSAndy Whitcroft	while (<$FILE>) {
4790a920b5bSAndy Whitcroft		chomp;
48000df344fSAndy Whitcroft		push(@rawlines, $_);
4816c72ffaaSAndy Whitcroft	}
48221caa13cSAndy Whitcroft	close($FILE);
483c2fdda0dSAndy Whitcroft	if (!process($filename)) {
4840a920b5bSAndy Whitcroft		$exit = 1;
4850a920b5bSAndy Whitcroft	}
48600df344fSAndy Whitcroft	@rawlines = ();
48713214adfSAndy Whitcroft	@lines = ();
4883705ce5bSJoe Perches	@fixed = ();
4890a920b5bSAndy Whitcroft}
4900a920b5bSAndy Whitcroft
4910a920b5bSAndy Whitcroftexit($exit);
4920a920b5bSAndy Whitcroft
4930a920b5bSAndy Whitcroftsub top_of_kernel_tree {
4946c72ffaaSAndy Whitcroft	my ($root) = @_;
4956c72ffaaSAndy Whitcroft
4966c72ffaaSAndy Whitcroft	my @tree_check = (
4976c72ffaaSAndy Whitcroft		"COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
4986c72ffaaSAndy Whitcroft		"README", "Documentation", "arch", "include", "drivers",
4996c72ffaaSAndy Whitcroft		"fs", "init", "ipc", "kernel", "lib", "scripts",
5006c72ffaaSAndy Whitcroft	);
5016c72ffaaSAndy Whitcroft
5026c72ffaaSAndy Whitcroft	foreach my $check (@tree_check) {
5036c72ffaaSAndy Whitcroft		if (! -e $root . '/' . $check) {
5040a920b5bSAndy Whitcroft			return 0;
5050a920b5bSAndy Whitcroft		}
5066c72ffaaSAndy Whitcroft	}
5076c72ffaaSAndy Whitcroft	return 1;
5086c72ffaaSAndy Whitcroft}
5090a920b5bSAndy Whitcroft
51020112475SJoe Perchessub parse_email {
51120112475SJoe Perches	my ($formatted_email) = @_;
51220112475SJoe Perches
51320112475SJoe Perches	my $name = "";
51420112475SJoe Perches	my $address = "";
51520112475SJoe Perches	my $comment = "";
51620112475SJoe Perches
51720112475SJoe Perches	if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
51820112475SJoe Perches		$name = $1;
51920112475SJoe Perches		$address = $2;
52020112475SJoe Perches		$comment = $3 if defined $3;
52120112475SJoe Perches	} elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
52220112475SJoe Perches		$address = $1;
52320112475SJoe Perches		$comment = $2 if defined $2;
52420112475SJoe Perches	} elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
52520112475SJoe Perches		$address = $1;
52620112475SJoe Perches		$comment = $2 if defined $2;
52720112475SJoe Perches		$formatted_email =~ s/$address.*$//;
52820112475SJoe Perches		$name = $formatted_email;
5293705ce5bSJoe Perches		$name = trim($name);
53020112475SJoe Perches		$name =~ s/^\"|\"$//g;
53120112475SJoe Perches		# If there's a name left after stripping spaces and
53220112475SJoe Perches		# leading quotes, and the address doesn't have both
53320112475SJoe Perches		# leading and trailing angle brackets, the address
53420112475SJoe Perches		# is invalid. ie:
53520112475SJoe Perches		#   "joe smith [email protected]" bad
53620112475SJoe Perches		#   "joe smith <[email protected]" bad
53720112475SJoe Perches		if ($name ne "" && $address !~ /^<[^>]+>$/) {
53820112475SJoe Perches			$name = "";
53920112475SJoe Perches			$address = "";
54020112475SJoe Perches			$comment = "";
54120112475SJoe Perches		}
54220112475SJoe Perches	}
54320112475SJoe Perches
5443705ce5bSJoe Perches	$name = trim($name);
54520112475SJoe Perches	$name =~ s/^\"|\"$//g;
5463705ce5bSJoe Perches	$address = trim($address);
54720112475SJoe Perches	$address =~ s/^\<|\>$//g;
54820112475SJoe Perches
54920112475SJoe Perches	if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
55020112475SJoe Perches		$name =~ s/(?<!\\)"/\\"/g; ##escape quotes
55120112475SJoe Perches		$name = "\"$name\"";
55220112475SJoe Perches	}
55320112475SJoe Perches
55420112475SJoe Perches	return ($name, $address, $comment);
55520112475SJoe Perches}
55620112475SJoe Perches
55720112475SJoe Perchessub format_email {
55820112475SJoe Perches	my ($name, $address) = @_;
55920112475SJoe Perches
56020112475SJoe Perches	my $formatted_email;
56120112475SJoe Perches
5623705ce5bSJoe Perches	$name = trim($name);
56320112475SJoe Perches	$name =~ s/^\"|\"$//g;
5643705ce5bSJoe Perches	$address = trim($address);
56520112475SJoe Perches
56620112475SJoe Perches	if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
56720112475SJoe Perches		$name =~ s/(?<!\\)"/\\"/g; ##escape quotes
56820112475SJoe Perches		$name = "\"$name\"";
56920112475SJoe Perches	}
57020112475SJoe Perches
57120112475SJoe Perches	if ("$name" eq "") {
57220112475SJoe Perches		$formatted_email = "$address";
57320112475SJoe Perches	} else {
57420112475SJoe Perches		$formatted_email = "$name <$address>";
57520112475SJoe Perches	}
57620112475SJoe Perches
57720112475SJoe Perches	return $formatted_email;
57820112475SJoe Perches}
57920112475SJoe Perches
580000d1cc1SJoe Perchessub which_conf {
581000d1cc1SJoe Perches	my ($conf) = @_;
582000d1cc1SJoe Perches
583000d1cc1SJoe Perches	foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
584000d1cc1SJoe Perches		if (-e "$path/$conf") {
585000d1cc1SJoe Perches			return "$path/$conf";
586000d1cc1SJoe Perches		}
587000d1cc1SJoe Perches	}
588000d1cc1SJoe Perches
589000d1cc1SJoe Perches	return "";
590000d1cc1SJoe Perches}
591000d1cc1SJoe Perches
5920a920b5bSAndy Whitcroftsub expand_tabs {
5930a920b5bSAndy Whitcroft	my ($str) = @_;
5940a920b5bSAndy Whitcroft
5950a920b5bSAndy Whitcroft	my $res = '';
5960a920b5bSAndy Whitcroft	my $n = 0;
5970a920b5bSAndy Whitcroft	for my $c (split(//, $str)) {
5980a920b5bSAndy Whitcroft		if ($c eq "\t") {
5990a920b5bSAndy Whitcroft			$res .= ' ';
6000a920b5bSAndy Whitcroft			$n++;
6010a920b5bSAndy Whitcroft			for (; ($n % 8) != 0; $n++) {
6020a920b5bSAndy Whitcroft				$res .= ' ';
6030a920b5bSAndy Whitcroft			}
6040a920b5bSAndy Whitcroft			next;
6050a920b5bSAndy Whitcroft		}
6060a920b5bSAndy Whitcroft		$res .= $c;
6070a920b5bSAndy Whitcroft		$n++;
6080a920b5bSAndy Whitcroft	}
6090a920b5bSAndy Whitcroft
6100a920b5bSAndy Whitcroft	return $res;
6110a920b5bSAndy Whitcroft}
6126c72ffaaSAndy Whitcroftsub copy_spacing {
613773647a0SAndy Whitcroft	(my $res = shift) =~ tr/\t/ /c;
6146c72ffaaSAndy Whitcroft	return $res;
6156c72ffaaSAndy Whitcroft}
6160a920b5bSAndy Whitcroft
6174a0df2efSAndy Whitcroftsub line_stats {
6184a0df2efSAndy Whitcroft	my ($line) = @_;
6194a0df2efSAndy Whitcroft
6204a0df2efSAndy Whitcroft	# Drop the diff line leader and expand tabs
6214a0df2efSAndy Whitcroft	$line =~ s/^.//;
6224a0df2efSAndy Whitcroft	$line = expand_tabs($line);
6234a0df2efSAndy Whitcroft
6244a0df2efSAndy Whitcroft	# Pick the indent from the front of the line.
6254a0df2efSAndy Whitcroft	my ($white) = ($line =~ /^(\s*)/);
6264a0df2efSAndy Whitcroft
6274a0df2efSAndy Whitcroft	return (length($line), length($white));
6284a0df2efSAndy Whitcroft}
6294a0df2efSAndy Whitcroft
630773647a0SAndy Whitcroftmy $sanitise_quote = '';
631773647a0SAndy Whitcroft
632773647a0SAndy Whitcroftsub sanitise_line_reset {
633773647a0SAndy Whitcroft	my ($in_comment) = @_;
634773647a0SAndy Whitcroft
635773647a0SAndy Whitcroft	if ($in_comment) {
636773647a0SAndy Whitcroft		$sanitise_quote = '*/';
637773647a0SAndy Whitcroft	} else {
638773647a0SAndy Whitcroft		$sanitise_quote = '';
639773647a0SAndy Whitcroft	}
640773647a0SAndy Whitcroft}
64100df344fSAndy Whitcroftsub sanitise_line {
64200df344fSAndy Whitcroft	my ($line) = @_;
64300df344fSAndy Whitcroft
64400df344fSAndy Whitcroft	my $res = '';
64500df344fSAndy Whitcroft	my $l = '';
64600df344fSAndy Whitcroft
647c2fdda0dSAndy Whitcroft	my $qlen = 0;
648773647a0SAndy Whitcroft	my $off = 0;
649773647a0SAndy Whitcroft	my $c;
65000df344fSAndy Whitcroft
651773647a0SAndy Whitcroft	# Always copy over the diff marker.
652773647a0SAndy Whitcroft	$res = substr($line, 0, 1);
653773647a0SAndy Whitcroft
654773647a0SAndy Whitcroft	for ($off = 1; $off < length($line); $off++) {
655773647a0SAndy Whitcroft		$c = substr($line, $off, 1);
656773647a0SAndy Whitcroft
657773647a0SAndy Whitcroft		# Comments we are wacking completly including the begin
658773647a0SAndy Whitcroft		# and end, all to $;.
659773647a0SAndy Whitcroft		if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
660773647a0SAndy Whitcroft			$sanitise_quote = '*/';
661773647a0SAndy Whitcroft
662773647a0SAndy Whitcroft			substr($res, $off, 2, "$;$;");
663773647a0SAndy Whitcroft			$off++;
66400df344fSAndy Whitcroft			next;
665773647a0SAndy Whitcroft		}
66681bc0e02SAndy Whitcroft		if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
667773647a0SAndy Whitcroft			$sanitise_quote = '';
668773647a0SAndy Whitcroft			substr($res, $off, 2, "$;$;");
669773647a0SAndy Whitcroft			$off++;
670773647a0SAndy Whitcroft			next;
671773647a0SAndy Whitcroft		}
672113f04a8SDaniel Walker		if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
673113f04a8SDaniel Walker			$sanitise_quote = '//';
674113f04a8SDaniel Walker
675113f04a8SDaniel Walker			substr($res, $off, 2, $sanitise_quote);
676113f04a8SDaniel Walker			$off++;
677113f04a8SDaniel Walker			next;
678113f04a8SDaniel Walker		}
679773647a0SAndy Whitcroft
680773647a0SAndy Whitcroft		# A \ in a string means ignore the next character.
681773647a0SAndy Whitcroft		if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
682773647a0SAndy Whitcroft		    $c eq "\\") {
683773647a0SAndy Whitcroft			substr($res, $off, 2, 'XX');
684773647a0SAndy Whitcroft			$off++;
685773647a0SAndy Whitcroft			next;
686773647a0SAndy Whitcroft		}
687773647a0SAndy Whitcroft		# Regular quotes.
688773647a0SAndy Whitcroft		if ($c eq "'" || $c eq '"') {
689773647a0SAndy Whitcroft			if ($sanitise_quote eq '') {
690773647a0SAndy Whitcroft				$sanitise_quote = $c;
691773647a0SAndy Whitcroft
692773647a0SAndy Whitcroft				substr($res, $off, 1, $c);
693773647a0SAndy Whitcroft				next;
694773647a0SAndy Whitcroft			} elsif ($sanitise_quote eq $c) {
695773647a0SAndy Whitcroft				$sanitise_quote = '';
69600df344fSAndy Whitcroft			}
69700df344fSAndy Whitcroft		}
698773647a0SAndy Whitcroft
699fae17daeSAndy Whitcroft		#print "c<$c> SQ<$sanitise_quote>\n";
700773647a0SAndy Whitcroft		if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
701773647a0SAndy Whitcroft			substr($res, $off, 1, $;);
702113f04a8SDaniel Walker		} elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
703113f04a8SDaniel Walker			substr($res, $off, 1, $;);
704773647a0SAndy Whitcroft		} elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
705773647a0SAndy Whitcroft			substr($res, $off, 1, 'X');
70600df344fSAndy Whitcroft		} else {
707773647a0SAndy Whitcroft			substr($res, $off, 1, $c);
70800df344fSAndy Whitcroft		}
709c2fdda0dSAndy Whitcroft	}
710c2fdda0dSAndy Whitcroft
711113f04a8SDaniel Walker	if ($sanitise_quote eq '//') {
712113f04a8SDaniel Walker		$sanitise_quote = '';
713113f04a8SDaniel Walker	}
714113f04a8SDaniel Walker
715c2fdda0dSAndy Whitcroft	# The pathname on a #include may be surrounded by '<' and '>'.
716c45dcabdSAndy Whitcroft	if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
717c2fdda0dSAndy Whitcroft		my $clean = 'X' x length($1);
718c2fdda0dSAndy Whitcroft		$res =~ s@\<.*\>@<$clean>@;
719c2fdda0dSAndy Whitcroft
720c2fdda0dSAndy Whitcroft	# The whole of a #error is a string.
721c45dcabdSAndy Whitcroft	} elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
722c2fdda0dSAndy Whitcroft		my $clean = 'X' x length($1);
723c45dcabdSAndy Whitcroft		$res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
724c2fdda0dSAndy Whitcroft	}
725c2fdda0dSAndy Whitcroft
72600df344fSAndy Whitcroft	return $res;
72700df344fSAndy Whitcroft}
72800df344fSAndy Whitcroft
729a6962d72SJoe Perchessub get_quoted_string {
730a6962d72SJoe Perches	my ($line, $rawline) = @_;
731a6962d72SJoe Perches
732a6962d72SJoe Perches	return "" if ($line !~ m/(\"[X]+\")/g);
733a6962d72SJoe Perches	return substr($rawline, $-[0], $+[0] - $-[0]);
734a6962d72SJoe Perches}
735a6962d72SJoe Perches
7368905a67cSAndy Whitcroftsub ctx_statement_block {
7378905a67cSAndy Whitcroft	my ($linenr, $remain, $off) = @_;
7388905a67cSAndy Whitcroft	my $line = $linenr - 1;
7398905a67cSAndy Whitcroft	my $blk = '';
7408905a67cSAndy Whitcroft	my $soff = $off;
7418905a67cSAndy Whitcroft	my $coff = $off - 1;
742773647a0SAndy Whitcroft	my $coff_set = 0;
7438905a67cSAndy Whitcroft
74413214adfSAndy Whitcroft	my $loff = 0;
74513214adfSAndy Whitcroft
7468905a67cSAndy Whitcroft	my $type = '';
7478905a67cSAndy Whitcroft	my $level = 0;
748a2750645SAndy Whitcroft	my @stack = ();
749cf655043SAndy Whitcroft	my $p;
7508905a67cSAndy Whitcroft	my $c;
7518905a67cSAndy Whitcroft	my $len = 0;
75213214adfSAndy Whitcroft
75313214adfSAndy Whitcroft	my $remainder;
7548905a67cSAndy Whitcroft	while (1) {
755a2750645SAndy Whitcroft		@stack = (['', 0]) if ($#stack == -1);
756a2750645SAndy Whitcroft
757773647a0SAndy Whitcroft		#warn "CSB: blk<$blk> remain<$remain>\n";
7588905a67cSAndy Whitcroft		# If we are about to drop off the end, pull in more
7598905a67cSAndy Whitcroft		# context.
7608905a67cSAndy Whitcroft		if ($off >= $len) {
7618905a67cSAndy Whitcroft			for (; $remain > 0; $line++) {
762dea33496SAndy Whitcroft				last if (!defined $lines[$line]);
763c2fdda0dSAndy Whitcroft				next if ($lines[$line] =~ /^-/);
7648905a67cSAndy Whitcroft				$remain--;
76513214adfSAndy Whitcroft				$loff = $len;
766c2fdda0dSAndy Whitcroft				$blk .= $lines[$line] . "\n";
7678905a67cSAndy Whitcroft				$len = length($blk);
7688905a67cSAndy Whitcroft				$line++;
7698905a67cSAndy Whitcroft				last;
7708905a67cSAndy Whitcroft			}
7718905a67cSAndy Whitcroft			# Bail if there is no further context.
7728905a67cSAndy Whitcroft			#warn "CSB: blk<$blk> off<$off> len<$len>\n";
77313214adfSAndy Whitcroft			if ($off >= $len) {
7748905a67cSAndy Whitcroft				last;
7758905a67cSAndy Whitcroft			}
776f74bd194SAndy Whitcroft			if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
777f74bd194SAndy Whitcroft				$level++;
778f74bd194SAndy Whitcroft				$type = '#';
779f74bd194SAndy Whitcroft			}
7808905a67cSAndy Whitcroft		}
781cf655043SAndy Whitcroft		$p = $c;
7828905a67cSAndy Whitcroft		$c = substr($blk, $off, 1);
78313214adfSAndy Whitcroft		$remainder = substr($blk, $off);
7848905a67cSAndy Whitcroft
785773647a0SAndy Whitcroft		#warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
7864635f4fbSAndy Whitcroft
7874635f4fbSAndy Whitcroft		# Handle nested #if/#else.
7884635f4fbSAndy Whitcroft		if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
7894635f4fbSAndy Whitcroft			push(@stack, [ $type, $level ]);
7904635f4fbSAndy Whitcroft		} elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
7914635f4fbSAndy Whitcroft			($type, $level) = @{$stack[$#stack - 1]};
7924635f4fbSAndy Whitcroft		} elsif ($remainder =~ /^#\s*endif\b/) {
7934635f4fbSAndy Whitcroft			($type, $level) = @{pop(@stack)};
7944635f4fbSAndy Whitcroft		}
7954635f4fbSAndy Whitcroft
7968905a67cSAndy Whitcroft		# Statement ends at the ';' or a close '}' at the
7978905a67cSAndy Whitcroft		# outermost level.
7988905a67cSAndy Whitcroft		if ($level == 0 && $c eq ';') {
7998905a67cSAndy Whitcroft			last;
8008905a67cSAndy Whitcroft		}
8018905a67cSAndy Whitcroft
80213214adfSAndy Whitcroft		# An else is really a conditional as long as its not else if
803773647a0SAndy Whitcroft		if ($level == 0 && $coff_set == 0 &&
804773647a0SAndy Whitcroft				(!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
805773647a0SAndy Whitcroft				$remainder =~ /^(else)(?:\s|{)/ &&
806773647a0SAndy Whitcroft				$remainder !~ /^else\s+if\b/) {
807773647a0SAndy Whitcroft			$coff = $off + length($1) - 1;
808773647a0SAndy Whitcroft			$coff_set = 1;
809773647a0SAndy Whitcroft			#warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
810773647a0SAndy Whitcroft			#warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
81113214adfSAndy Whitcroft		}
81213214adfSAndy Whitcroft
8138905a67cSAndy Whitcroft		if (($type eq '' || $type eq '(') && $c eq '(') {
8148905a67cSAndy Whitcroft			$level++;
8158905a67cSAndy Whitcroft			$type = '(';
8168905a67cSAndy Whitcroft		}
8178905a67cSAndy Whitcroft		if ($type eq '(' && $c eq ')') {
8188905a67cSAndy Whitcroft			$level--;
8198905a67cSAndy Whitcroft			$type = ($level != 0)? '(' : '';
8208905a67cSAndy Whitcroft
8218905a67cSAndy Whitcroft			if ($level == 0 && $coff < $soff) {
8228905a67cSAndy Whitcroft				$coff = $off;
823773647a0SAndy Whitcroft				$coff_set = 1;
824773647a0SAndy Whitcroft				#warn "CSB: mark coff<$coff>\n";
8258905a67cSAndy Whitcroft			}
8268905a67cSAndy Whitcroft		}
8278905a67cSAndy Whitcroft		if (($type eq '' || $type eq '{') && $c eq '{') {
8288905a67cSAndy Whitcroft			$level++;
8298905a67cSAndy Whitcroft			$type = '{';
8308905a67cSAndy Whitcroft		}
8318905a67cSAndy Whitcroft		if ($type eq '{' && $c eq '}') {
8328905a67cSAndy Whitcroft			$level--;
8338905a67cSAndy Whitcroft			$type = ($level != 0)? '{' : '';
8348905a67cSAndy Whitcroft
8358905a67cSAndy Whitcroft			if ($level == 0) {
836b998e001SPatrick Pannuto				if (substr($blk, $off + 1, 1) eq ';') {
837b998e001SPatrick Pannuto					$off++;
838b998e001SPatrick Pannuto				}
8398905a67cSAndy Whitcroft				last;
8408905a67cSAndy Whitcroft			}
8418905a67cSAndy Whitcroft		}
842f74bd194SAndy Whitcroft		# Preprocessor commands end at the newline unless escaped.
843f74bd194SAndy Whitcroft		if ($type eq '#' && $c eq "\n" && $p ne "\\") {
844f74bd194SAndy Whitcroft			$level--;
845f74bd194SAndy Whitcroft			$type = '';
846f74bd194SAndy Whitcroft			$off++;
847f74bd194SAndy Whitcroft			last;
848f74bd194SAndy Whitcroft		}
8498905a67cSAndy Whitcroft		$off++;
8508905a67cSAndy Whitcroft	}
851a3bb97a7SAndy Whitcroft	# We are truly at the end, so shuffle to the next line.
85213214adfSAndy Whitcroft	if ($off == $len) {
853a3bb97a7SAndy Whitcroft		$loff = $len + 1;
85413214adfSAndy Whitcroft		$line++;
85513214adfSAndy Whitcroft		$remain--;
85613214adfSAndy Whitcroft	}
8578905a67cSAndy Whitcroft
8588905a67cSAndy Whitcroft	my $statement = substr($blk, $soff, $off - $soff + 1);
8598905a67cSAndy Whitcroft	my $condition = substr($blk, $soff, $coff - $soff + 1);
8608905a67cSAndy Whitcroft
8618905a67cSAndy Whitcroft	#warn "STATEMENT<$statement>\n";
8628905a67cSAndy Whitcroft	#warn "CONDITION<$condition>\n";
8638905a67cSAndy Whitcroft
864773647a0SAndy Whitcroft	#print "coff<$coff> soff<$off> loff<$loff>\n";
86513214adfSAndy Whitcroft
86613214adfSAndy Whitcroft	return ($statement, $condition,
86713214adfSAndy Whitcroft			$line, $remain + 1, $off - $loff + 1, $level);
86813214adfSAndy Whitcroft}
86913214adfSAndy Whitcroft
870cf655043SAndy Whitcroftsub statement_lines {
871cf655043SAndy Whitcroft	my ($stmt) = @_;
872cf655043SAndy Whitcroft
873cf655043SAndy Whitcroft	# Strip the diff line prefixes and rip blank lines at start and end.
874cf655043SAndy Whitcroft	$stmt =~ s/(^|\n)./$1/g;
875cf655043SAndy Whitcroft	$stmt =~ s/^\s*//;
876cf655043SAndy Whitcroft	$stmt =~ s/\s*$//;
877cf655043SAndy Whitcroft
878cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
879cf655043SAndy Whitcroft
880cf655043SAndy Whitcroft	return $#stmt_lines + 2;
881cf655043SAndy Whitcroft}
882cf655043SAndy Whitcroft
883cf655043SAndy Whitcroftsub statement_rawlines {
884cf655043SAndy Whitcroft	my ($stmt) = @_;
885cf655043SAndy Whitcroft
886cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
887cf655043SAndy Whitcroft
888cf655043SAndy Whitcroft	return $#stmt_lines + 2;
889cf655043SAndy Whitcroft}
890cf655043SAndy Whitcroft
891cf655043SAndy Whitcroftsub statement_block_size {
892cf655043SAndy Whitcroft	my ($stmt) = @_;
893cf655043SAndy Whitcroft
894cf655043SAndy Whitcroft	$stmt =~ s/(^|\n)./$1/g;
895cf655043SAndy Whitcroft	$stmt =~ s/^\s*{//;
896cf655043SAndy Whitcroft	$stmt =~ s/}\s*$//;
897cf655043SAndy Whitcroft	$stmt =~ s/^\s*//;
898cf655043SAndy Whitcroft	$stmt =~ s/\s*$//;
899cf655043SAndy Whitcroft
900cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
901cf655043SAndy Whitcroft	my @stmt_statements = ($stmt =~ /;/g);
902cf655043SAndy Whitcroft
903cf655043SAndy Whitcroft	my $stmt_lines = $#stmt_lines + 2;
904cf655043SAndy Whitcroft	my $stmt_statements = $#stmt_statements + 1;
905cf655043SAndy Whitcroft
906cf655043SAndy Whitcroft	if ($stmt_lines > $stmt_statements) {
907cf655043SAndy Whitcroft		return $stmt_lines;
908cf655043SAndy Whitcroft	} else {
909cf655043SAndy Whitcroft		return $stmt_statements;
910cf655043SAndy Whitcroft	}
911cf655043SAndy Whitcroft}
912cf655043SAndy Whitcroft
91313214adfSAndy Whitcroftsub ctx_statement_full {
91413214adfSAndy Whitcroft	my ($linenr, $remain, $off) = @_;
91513214adfSAndy Whitcroft	my ($statement, $condition, $level);
91613214adfSAndy Whitcroft
91713214adfSAndy Whitcroft	my (@chunks);
91813214adfSAndy Whitcroft
919cf655043SAndy Whitcroft	# Grab the first conditional/block pair.
92013214adfSAndy Whitcroft	($statement, $condition, $linenr, $remain, $off, $level) =
92113214adfSAndy Whitcroft				ctx_statement_block($linenr, $remain, $off);
922773647a0SAndy Whitcroft	#print "F: c<$condition> s<$statement> remain<$remain>\n";
92313214adfSAndy Whitcroft	push(@chunks, [ $condition, $statement ]);
924cf655043SAndy Whitcroft	if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
925cf655043SAndy Whitcroft		return ($level, $linenr, @chunks);
926cf655043SAndy Whitcroft	}
927cf655043SAndy Whitcroft
928cf655043SAndy Whitcroft	# Pull in the following conditional/block pairs and see if they
929cf655043SAndy Whitcroft	# could continue the statement.
930cf655043SAndy Whitcroft	for (;;) {
93113214adfSAndy Whitcroft		($statement, $condition, $linenr, $remain, $off, $level) =
93213214adfSAndy Whitcroft				ctx_statement_block($linenr, $remain, $off);
933cf655043SAndy Whitcroft		#print "C: c<$condition> s<$statement> remain<$remain>\n";
934773647a0SAndy Whitcroft		last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
935cf655043SAndy Whitcroft		#print "C: push\n";
936cf655043SAndy Whitcroft		push(@chunks, [ $condition, $statement ]);
93713214adfSAndy Whitcroft	}
93813214adfSAndy Whitcroft
93913214adfSAndy Whitcroft	return ($level, $linenr, @chunks);
9408905a67cSAndy Whitcroft}
9418905a67cSAndy Whitcroft
9424a0df2efSAndy Whitcroftsub ctx_block_get {
943f0a594c1SAndy Whitcroft	my ($linenr, $remain, $outer, $open, $close, $off) = @_;
9444a0df2efSAndy Whitcroft	my $line;
9454a0df2efSAndy Whitcroft	my $start = $linenr - 1;
9464a0df2efSAndy Whitcroft	my $blk = '';
9474a0df2efSAndy Whitcroft	my @o;
9484a0df2efSAndy Whitcroft	my @c;
9494a0df2efSAndy Whitcroft	my @res = ();
9504a0df2efSAndy Whitcroft
951f0a594c1SAndy Whitcroft	my $level = 0;
9524635f4fbSAndy Whitcroft	my @stack = ($level);
95300df344fSAndy Whitcroft	for ($line = $start; $remain > 0; $line++) {
95400df344fSAndy Whitcroft		next if ($rawlines[$line] =~ /^-/);
95500df344fSAndy Whitcroft		$remain--;
95600df344fSAndy Whitcroft
95700df344fSAndy Whitcroft		$blk .= $rawlines[$line];
9584635f4fbSAndy Whitcroft
9594635f4fbSAndy Whitcroft		# Handle nested #if/#else.
96001464f30SAndy Whitcroft		if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
9614635f4fbSAndy Whitcroft			push(@stack, $level);
96201464f30SAndy Whitcroft		} elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
9634635f4fbSAndy Whitcroft			$level = $stack[$#stack - 1];
96401464f30SAndy Whitcroft		} elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
9654635f4fbSAndy Whitcroft			$level = pop(@stack);
9664635f4fbSAndy Whitcroft		}
9674635f4fbSAndy Whitcroft
96801464f30SAndy Whitcroft		foreach my $c (split(//, $lines[$line])) {
969f0a594c1SAndy Whitcroft			##print "C<$c>L<$level><$open$close>O<$off>\n";
970f0a594c1SAndy Whitcroft			if ($off > 0) {
971f0a594c1SAndy Whitcroft				$off--;
972f0a594c1SAndy Whitcroft				next;
973f0a594c1SAndy Whitcroft			}
9744a0df2efSAndy Whitcroft
975f0a594c1SAndy Whitcroft			if ($c eq $close && $level > 0) {
976f0a594c1SAndy Whitcroft				$level--;
977f0a594c1SAndy Whitcroft				last if ($level == 0);
978f0a594c1SAndy Whitcroft			} elsif ($c eq $open) {
979f0a594c1SAndy Whitcroft				$level++;
980f0a594c1SAndy Whitcroft			}
981f0a594c1SAndy Whitcroft		}
9824a0df2efSAndy Whitcroft
983f0a594c1SAndy Whitcroft		if (!$outer || $level <= 1) {
98400df344fSAndy Whitcroft			push(@res, $rawlines[$line]);
9854a0df2efSAndy Whitcroft		}
9864a0df2efSAndy Whitcroft
987f0a594c1SAndy Whitcroft		last if ($level == 0);
9884a0df2efSAndy Whitcroft	}
9894a0df2efSAndy Whitcroft
990f0a594c1SAndy Whitcroft	return ($level, @res);
9914a0df2efSAndy Whitcroft}
9924a0df2efSAndy Whitcroftsub ctx_block_outer {
9934a0df2efSAndy Whitcroft	my ($linenr, $remain) = @_;
9944a0df2efSAndy Whitcroft
995f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
996f0a594c1SAndy Whitcroft	return @r;
9974a0df2efSAndy Whitcroft}
9984a0df2efSAndy Whitcroftsub ctx_block {
9994a0df2efSAndy Whitcroft	my ($linenr, $remain) = @_;
10004a0df2efSAndy Whitcroft
1001f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1002f0a594c1SAndy Whitcroft	return @r;
1003653d4876SAndy Whitcroft}
1004653d4876SAndy Whitcroftsub ctx_statement {
1005f0a594c1SAndy Whitcroft	my ($linenr, $remain, $off) = @_;
1006f0a594c1SAndy Whitcroft
1007f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1008f0a594c1SAndy Whitcroft	return @r;
1009f0a594c1SAndy Whitcroft}
1010f0a594c1SAndy Whitcroftsub ctx_block_level {
1011653d4876SAndy Whitcroft	my ($linenr, $remain) = @_;
1012653d4876SAndy Whitcroft
1013f0a594c1SAndy Whitcroft	return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
10144a0df2efSAndy Whitcroft}
10159c0ca6f9SAndy Whitcroftsub ctx_statement_level {
10169c0ca6f9SAndy Whitcroft	my ($linenr, $remain, $off) = @_;
10179c0ca6f9SAndy Whitcroft
10189c0ca6f9SAndy Whitcroft	return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
10199c0ca6f9SAndy Whitcroft}
10204a0df2efSAndy Whitcroft
10214a0df2efSAndy Whitcroftsub ctx_locate_comment {
10224a0df2efSAndy Whitcroft	my ($first_line, $end_line) = @_;
10234a0df2efSAndy Whitcroft
10244a0df2efSAndy Whitcroft	# Catch a comment on the end of the line itself.
1025beae6332SAndy Whitcroft	my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
10264a0df2efSAndy Whitcroft	return $current_comment if (defined $current_comment);
10274a0df2efSAndy Whitcroft
10284a0df2efSAndy Whitcroft	# Look through the context and try and figure out if there is a
10294a0df2efSAndy Whitcroft	# comment.
10304a0df2efSAndy Whitcroft	my $in_comment = 0;
10314a0df2efSAndy Whitcroft	$current_comment = '';
10324a0df2efSAndy Whitcroft	for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
103300df344fSAndy Whitcroft		my $line = $rawlines[$linenr - 1];
103400df344fSAndy Whitcroft		#warn "           $line\n";
10354a0df2efSAndy Whitcroft		if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
10364a0df2efSAndy Whitcroft			$in_comment = 1;
10374a0df2efSAndy Whitcroft		}
10384a0df2efSAndy Whitcroft		if ($line =~ m@/\*@) {
10394a0df2efSAndy Whitcroft			$in_comment = 1;
10404a0df2efSAndy Whitcroft		}
10414a0df2efSAndy Whitcroft		if (!$in_comment && $current_comment ne '') {
10424a0df2efSAndy Whitcroft			$current_comment = '';
10434a0df2efSAndy Whitcroft		}
10444a0df2efSAndy Whitcroft		$current_comment .= $line . "\n" if ($in_comment);
10454a0df2efSAndy Whitcroft		if ($line =~ m@\*/@) {
10464a0df2efSAndy Whitcroft			$in_comment = 0;
10474a0df2efSAndy Whitcroft		}
10484a0df2efSAndy Whitcroft	}
10494a0df2efSAndy Whitcroft
10504a0df2efSAndy Whitcroft	chomp($current_comment);
10514a0df2efSAndy Whitcroft	return($current_comment);
10524a0df2efSAndy Whitcroft}
10534a0df2efSAndy Whitcroftsub ctx_has_comment {
10544a0df2efSAndy Whitcroft	my ($first_line, $end_line) = @_;
10554a0df2efSAndy Whitcroft	my $cmt = ctx_locate_comment($first_line, $end_line);
10564a0df2efSAndy Whitcroft
105700df344fSAndy Whitcroft	##print "LINE: $rawlines[$end_line - 1 ]\n";
10584a0df2efSAndy Whitcroft	##print "CMMT: $cmt\n";
10594a0df2efSAndy Whitcroft
10604a0df2efSAndy Whitcroft	return ($cmt ne '');
10614a0df2efSAndy Whitcroft}
10624a0df2efSAndy Whitcroft
10634d001e4dSAndy Whitcroftsub raw_line {
10644d001e4dSAndy Whitcroft	my ($linenr, $cnt) = @_;
10654d001e4dSAndy Whitcroft
10664d001e4dSAndy Whitcroft	my $offset = $linenr - 1;
10674d001e4dSAndy Whitcroft	$cnt++;
10684d001e4dSAndy Whitcroft
10694d001e4dSAndy Whitcroft	my $line;
10704d001e4dSAndy Whitcroft	while ($cnt) {
10714d001e4dSAndy Whitcroft		$line = $rawlines[$offset++];
10724d001e4dSAndy Whitcroft		next if (defined($line) && $line =~ /^-/);
10734d001e4dSAndy Whitcroft		$cnt--;
10744d001e4dSAndy Whitcroft	}
10754d001e4dSAndy Whitcroft
10764d001e4dSAndy Whitcroft	return $line;
10774d001e4dSAndy Whitcroft}
10784d001e4dSAndy Whitcroft
10790a920b5bSAndy Whitcroftsub cat_vet {
10800a920b5bSAndy Whitcroft	my ($vet) = @_;
10819c0ca6f9SAndy Whitcroft	my ($res, $coded);
10820a920b5bSAndy Whitcroft
10839c0ca6f9SAndy Whitcroft	$res = '';
10846c72ffaaSAndy Whitcroft	while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
10856c72ffaaSAndy Whitcroft		$res .= $1;
10866c72ffaaSAndy Whitcroft		if ($2 ne '') {
10879c0ca6f9SAndy Whitcroft			$coded = sprintf("^%c", unpack('C', $2) + 64);
10886c72ffaaSAndy Whitcroft			$res .= $coded;
10896c72ffaaSAndy Whitcroft		}
10909c0ca6f9SAndy Whitcroft	}
10919c0ca6f9SAndy Whitcroft	$res =~ s/$/\$/;
10920a920b5bSAndy Whitcroft
10939c0ca6f9SAndy Whitcroft	return $res;
10940a920b5bSAndy Whitcroft}
10950a920b5bSAndy Whitcroft
1096c2fdda0dSAndy Whitcroftmy $av_preprocessor = 0;
1097cf655043SAndy Whitcroftmy $av_pending;
1098c2fdda0dSAndy Whitcroftmy @av_paren_type;
10991f65f947SAndy Whitcroftmy $av_pend_colon;
1100c2fdda0dSAndy Whitcroft
1101c2fdda0dSAndy Whitcroftsub annotate_reset {
1102c2fdda0dSAndy Whitcroft	$av_preprocessor = 0;
1103cf655043SAndy Whitcroft	$av_pending = '_';
1104cf655043SAndy Whitcroft	@av_paren_type = ('E');
11051f65f947SAndy Whitcroft	$av_pend_colon = 'O';
1106c2fdda0dSAndy Whitcroft}
1107c2fdda0dSAndy Whitcroft
11086c72ffaaSAndy Whitcroftsub annotate_values {
11096c72ffaaSAndy Whitcroft	my ($stream, $type) = @_;
11106c72ffaaSAndy Whitcroft
11116c72ffaaSAndy Whitcroft	my $res;
11121f65f947SAndy Whitcroft	my $var = '_' x length($stream);
11136c72ffaaSAndy Whitcroft	my $cur = $stream;
11146c72ffaaSAndy Whitcroft
1115c2fdda0dSAndy Whitcroft	print "$stream\n" if ($dbg_values > 1);
11166c72ffaaSAndy Whitcroft
11176c72ffaaSAndy Whitcroft	while (length($cur)) {
1118773647a0SAndy Whitcroft		@av_paren_type = ('E') if ($#av_paren_type < 0);
1119cf655043SAndy Whitcroft		print " <" . join('', @av_paren_type) .
1120171ae1a4SAndy Whitcroft				"> <$type> <$av_pending>" if ($dbg_values > 1);
11216c72ffaaSAndy Whitcroft		if ($cur =~ /^(\s+)/o) {
1122c2fdda0dSAndy Whitcroft			print "WS($1)\n" if ($dbg_values > 1);
1123c2fdda0dSAndy Whitcroft			if ($1 =~ /\n/ && $av_preprocessor) {
1124cf655043SAndy Whitcroft				$type = pop(@av_paren_type);
1125c2fdda0dSAndy Whitcroft				$av_preprocessor = 0;
11266c72ffaaSAndy Whitcroft			}
11276c72ffaaSAndy Whitcroft
1128c023e473SFlorian Mickler		} elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
11299446ef56SAndy Whitcroft			print "CAST($1)\n" if ($dbg_values > 1);
11309446ef56SAndy Whitcroft			push(@av_paren_type, $type);
1131addcdceaSAndy Whitcroft			$type = 'c';
11329446ef56SAndy Whitcroft
1133e91b6e26SAndy Whitcroft		} elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
1134c2fdda0dSAndy Whitcroft			print "DECLARE($1)\n" if ($dbg_values > 1);
11356c72ffaaSAndy Whitcroft			$type = 'T';
11366c72ffaaSAndy Whitcroft
1137389a2fe5SAndy Whitcroft		} elsif ($cur =~ /^($Modifier)\s*/) {
1138389a2fe5SAndy Whitcroft			print "MODIFIER($1)\n" if ($dbg_values > 1);
1139389a2fe5SAndy Whitcroft			$type = 'T';
1140389a2fe5SAndy Whitcroft
1141c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
1142171ae1a4SAndy Whitcroft			print "DEFINE($1,$2)\n" if ($dbg_values > 1);
1143c2fdda0dSAndy Whitcroft			$av_preprocessor = 1;
1144171ae1a4SAndy Whitcroft			push(@av_paren_type, $type);
1145171ae1a4SAndy Whitcroft			if ($2 ne '') {
1146cf655043SAndy Whitcroft				$av_pending = 'N';
1147171ae1a4SAndy Whitcroft			}
1148171ae1a4SAndy Whitcroft			$type = 'E';
1149171ae1a4SAndy Whitcroft
1150c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
1151171ae1a4SAndy Whitcroft			print "UNDEF($1)\n" if ($dbg_values > 1);
1152171ae1a4SAndy Whitcroft			$av_preprocessor = 1;
1153171ae1a4SAndy Whitcroft			push(@av_paren_type, $type);
11546c72ffaaSAndy Whitcroft
1155c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
1156cf655043SAndy Whitcroft			print "PRE_START($1)\n" if ($dbg_values > 1);
1157c2fdda0dSAndy Whitcroft			$av_preprocessor = 1;
1158cf655043SAndy Whitcroft
1159cf655043SAndy Whitcroft			push(@av_paren_type, $type);
1160cf655043SAndy Whitcroft			push(@av_paren_type, $type);
1161171ae1a4SAndy Whitcroft			$type = 'E';
1162cf655043SAndy Whitcroft
1163c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
1164cf655043SAndy Whitcroft			print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1165cf655043SAndy Whitcroft			$av_preprocessor = 1;
1166cf655043SAndy Whitcroft
1167cf655043SAndy Whitcroft			push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1168cf655043SAndy Whitcroft
1169171ae1a4SAndy Whitcroft			$type = 'E';
1170cf655043SAndy Whitcroft
1171c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:endif))/o) {
1172cf655043SAndy Whitcroft			print "PRE_END($1)\n" if ($dbg_values > 1);
1173cf655043SAndy Whitcroft
1174cf655043SAndy Whitcroft			$av_preprocessor = 1;
1175cf655043SAndy Whitcroft
1176cf655043SAndy Whitcroft			# Assume all arms of the conditional end as this
1177cf655043SAndy Whitcroft			# one does, and continue as if the #endif was not here.
1178cf655043SAndy Whitcroft			pop(@av_paren_type);
1179cf655043SAndy Whitcroft			push(@av_paren_type, $type);
1180171ae1a4SAndy Whitcroft			$type = 'E';
11816c72ffaaSAndy Whitcroft
11826c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\\\n)/o) {
1183c2fdda0dSAndy Whitcroft			print "PRECONT($1)\n" if ($dbg_values > 1);
11846c72ffaaSAndy Whitcroft
1185171ae1a4SAndy Whitcroft		} elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1186171ae1a4SAndy Whitcroft			print "ATTR($1)\n" if ($dbg_values > 1);
1187171ae1a4SAndy Whitcroft			$av_pending = $type;
1188171ae1a4SAndy Whitcroft			$type = 'N';
1189171ae1a4SAndy Whitcroft
11906c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
1191c2fdda0dSAndy Whitcroft			print "SIZEOF($1)\n" if ($dbg_values > 1);
11926c72ffaaSAndy Whitcroft			if (defined $2) {
1193cf655043SAndy Whitcroft				$av_pending = 'V';
11946c72ffaaSAndy Whitcroft			}
11956c72ffaaSAndy Whitcroft			$type = 'N';
11966c72ffaaSAndy Whitcroft
119714b111c1SAndy Whitcroft		} elsif ($cur =~ /^(if|while|for)\b/o) {
1198c2fdda0dSAndy Whitcroft			print "COND($1)\n" if ($dbg_values > 1);
119914b111c1SAndy Whitcroft			$av_pending = 'E';
12006c72ffaaSAndy Whitcroft			$type = 'N';
12016c72ffaaSAndy Whitcroft
12021f65f947SAndy Whitcroft		} elsif ($cur =~/^(case)/o) {
12031f65f947SAndy Whitcroft			print "CASE($1)\n" if ($dbg_values > 1);
12041f65f947SAndy Whitcroft			$av_pend_colon = 'C';
12051f65f947SAndy Whitcroft			$type = 'N';
12061f65f947SAndy Whitcroft
120714b111c1SAndy Whitcroft		} elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
1208c2fdda0dSAndy Whitcroft			print "KEYWORD($1)\n" if ($dbg_values > 1);
12096c72ffaaSAndy Whitcroft			$type = 'N';
12106c72ffaaSAndy Whitcroft
12116c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\()/o) {
1212c2fdda0dSAndy Whitcroft			print "PAREN('$1')\n" if ($dbg_values > 1);
1213cf655043SAndy Whitcroft			push(@av_paren_type, $av_pending);
1214cf655043SAndy Whitcroft			$av_pending = '_';
12156c72ffaaSAndy Whitcroft			$type = 'N';
12166c72ffaaSAndy Whitcroft
12176c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\))/o) {
1218cf655043SAndy Whitcroft			my $new_type = pop(@av_paren_type);
1219cf655043SAndy Whitcroft			if ($new_type ne '_') {
1220cf655043SAndy Whitcroft				$type = $new_type;
1221c2fdda0dSAndy Whitcroft				print "PAREN('$1') -> $type\n"
1222c2fdda0dSAndy Whitcroft							if ($dbg_values > 1);
12236c72ffaaSAndy Whitcroft			} else {
1224c2fdda0dSAndy Whitcroft				print "PAREN('$1')\n" if ($dbg_values > 1);
12256c72ffaaSAndy Whitcroft			}
12266c72ffaaSAndy Whitcroft
1227c8cb2ca3SAndy Whitcroft		} elsif ($cur =~ /^($Ident)\s*\(/o) {
1228c2fdda0dSAndy Whitcroft			print "FUNC($1)\n" if ($dbg_values > 1);
1229c8cb2ca3SAndy Whitcroft			$type = 'V';
1230cf655043SAndy Whitcroft			$av_pending = 'V';
12316c72ffaaSAndy Whitcroft
12328e761b04SAndy Whitcroft		} elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
12338e761b04SAndy Whitcroft			if (defined $2 && $type eq 'C' || $type eq 'T') {
12341f65f947SAndy Whitcroft				$av_pend_colon = 'B';
12358e761b04SAndy Whitcroft			} elsif ($type eq 'E') {
12368e761b04SAndy Whitcroft				$av_pend_colon = 'L';
12371f65f947SAndy Whitcroft			}
12381f65f947SAndy Whitcroft			print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
12391f65f947SAndy Whitcroft			$type = 'V';
12401f65f947SAndy Whitcroft
12416c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Ident|$Constant)/o) {
1242c2fdda0dSAndy Whitcroft			print "IDENT($1)\n" if ($dbg_values > 1);
12436c72ffaaSAndy Whitcroft			$type = 'V';
12446c72ffaaSAndy Whitcroft
12456c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Assignment)/o) {
1246c2fdda0dSAndy Whitcroft			print "ASSIGN($1)\n" if ($dbg_values > 1);
12476c72ffaaSAndy Whitcroft			$type = 'N';
12486c72ffaaSAndy Whitcroft
1249cf655043SAndy Whitcroft		} elsif ($cur =~/^(;|{|})/) {
1250c2fdda0dSAndy Whitcroft			print "END($1)\n" if ($dbg_values > 1);
125113214adfSAndy Whitcroft			$type = 'E';
12521f65f947SAndy Whitcroft			$av_pend_colon = 'O';
125313214adfSAndy Whitcroft
12548e761b04SAndy Whitcroft		} elsif ($cur =~/^(,)/) {
12558e761b04SAndy Whitcroft			print "COMMA($1)\n" if ($dbg_values > 1);
12568e761b04SAndy Whitcroft			$type = 'C';
12578e761b04SAndy Whitcroft
12581f65f947SAndy Whitcroft		} elsif ($cur =~ /^(\?)/o) {
12591f65f947SAndy Whitcroft			print "QUESTION($1)\n" if ($dbg_values > 1);
12601f65f947SAndy Whitcroft			$type = 'N';
12611f65f947SAndy Whitcroft
12621f65f947SAndy Whitcroft		} elsif ($cur =~ /^(:)/o) {
12631f65f947SAndy Whitcroft			print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
12641f65f947SAndy Whitcroft
12651f65f947SAndy Whitcroft			substr($var, length($res), 1, $av_pend_colon);
12661f65f947SAndy Whitcroft			if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
12671f65f947SAndy Whitcroft				$type = 'E';
12681f65f947SAndy Whitcroft			} else {
12691f65f947SAndy Whitcroft				$type = 'N';
12701f65f947SAndy Whitcroft			}
12711f65f947SAndy Whitcroft			$av_pend_colon = 'O';
12721f65f947SAndy Whitcroft
12738e761b04SAndy Whitcroft		} elsif ($cur =~ /^(\[)/o) {
127413214adfSAndy Whitcroft			print "CLOSE($1)\n" if ($dbg_values > 1);
12756c72ffaaSAndy Whitcroft			$type = 'N';
12766c72ffaaSAndy Whitcroft
12770d413866SAndy Whitcroft		} elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
127874048ed8SAndy Whitcroft			my $variant;
127974048ed8SAndy Whitcroft
128074048ed8SAndy Whitcroft			print "OPV($1)\n" if ($dbg_values > 1);
128174048ed8SAndy Whitcroft			if ($type eq 'V') {
128274048ed8SAndy Whitcroft				$variant = 'B';
128374048ed8SAndy Whitcroft			} else {
128474048ed8SAndy Whitcroft				$variant = 'U';
128574048ed8SAndy Whitcroft			}
128674048ed8SAndy Whitcroft
128774048ed8SAndy Whitcroft			substr($var, length($res), 1, $variant);
128874048ed8SAndy Whitcroft			$type = 'N';
128974048ed8SAndy Whitcroft
12906c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Operators)/o) {
1291c2fdda0dSAndy Whitcroft			print "OP($1)\n" if ($dbg_values > 1);
12926c72ffaaSAndy Whitcroft			if ($1 ne '++' && $1 ne '--') {
12936c72ffaaSAndy Whitcroft				$type = 'N';
12946c72ffaaSAndy Whitcroft			}
12956c72ffaaSAndy Whitcroft
12966c72ffaaSAndy Whitcroft		} elsif ($cur =~ /(^.)/o) {
1297c2fdda0dSAndy Whitcroft			print "C($1)\n" if ($dbg_values > 1);
12986c72ffaaSAndy Whitcroft		}
12996c72ffaaSAndy Whitcroft		if (defined $1) {
13006c72ffaaSAndy Whitcroft			$cur = substr($cur, length($1));
13016c72ffaaSAndy Whitcroft			$res .= $type x length($1);
13026c72ffaaSAndy Whitcroft		}
13036c72ffaaSAndy Whitcroft	}
13046c72ffaaSAndy Whitcroft
13051f65f947SAndy Whitcroft	return ($res, $var);
13066c72ffaaSAndy Whitcroft}
13076c72ffaaSAndy Whitcroft
13088905a67cSAndy Whitcroftsub possible {
130913214adfSAndy Whitcroft	my ($possible, $line) = @_;
13109a974fdbSAndy Whitcroft	my $notPermitted = qr{(?:
13110776e594SAndy Whitcroft		^(?:
13120776e594SAndy Whitcroft			$Modifier|
13130776e594SAndy Whitcroft			$Storage|
13140776e594SAndy Whitcroft			$Type|
13159a974fdbSAndy Whitcroft			DEFINE_\S+
13169a974fdbSAndy Whitcroft		)$|
13179a974fdbSAndy Whitcroft		^(?:
13180776e594SAndy Whitcroft			goto|
13190776e594SAndy Whitcroft			return|
13200776e594SAndy Whitcroft			case|
13210776e594SAndy Whitcroft			else|
13220776e594SAndy Whitcroft			asm|__asm__|
132389a88353SAndy Whitcroft			do|
132489a88353SAndy Whitcroft			\#|
132589a88353SAndy Whitcroft			\#\#|
13269a974fdbSAndy Whitcroft		)(?:\s|$)|
13270776e594SAndy Whitcroft		^(?:typedef|struct|enum)\b
13289a974fdbSAndy Whitcroft	    )}x;
13299a974fdbSAndy Whitcroft	warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
13309a974fdbSAndy Whitcroft	if ($possible !~ $notPermitted) {
1331c45dcabdSAndy Whitcroft		# Check for modifiers.
1332c45dcabdSAndy Whitcroft		$possible =~ s/\s*$Storage\s*//g;
1333c45dcabdSAndy Whitcroft		$possible =~ s/\s*$Sparse\s*//g;
1334c45dcabdSAndy Whitcroft		if ($possible =~ /^\s*$/) {
1335c45dcabdSAndy Whitcroft
1336c45dcabdSAndy Whitcroft		} elsif ($possible =~ /\s/) {
1337c45dcabdSAndy Whitcroft			$possible =~ s/\s*$Type\s*//g;
1338d2506586SAndy Whitcroft			for my $modifier (split(' ', $possible)) {
13399a974fdbSAndy Whitcroft				if ($modifier !~ $notPermitted) {
1340d2506586SAndy Whitcroft					warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1341d2506586SAndy Whitcroft					push(@modifierList, $modifier);
1342d2506586SAndy Whitcroft				}
13439a974fdbSAndy Whitcroft			}
1344c45dcabdSAndy Whitcroft
1345c45dcabdSAndy Whitcroft		} else {
134613214adfSAndy Whitcroft			warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
13478905a67cSAndy Whitcroft			push(@typeList, $possible);
1348c45dcabdSAndy Whitcroft		}
13498905a67cSAndy Whitcroft		build_types();
13500776e594SAndy Whitcroft	} else {
13510776e594SAndy Whitcroft		warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
13528905a67cSAndy Whitcroft	}
13538905a67cSAndy Whitcroft}
13548905a67cSAndy Whitcroft
13556c72ffaaSAndy Whitcroftmy $prefix = '';
13566c72ffaaSAndy Whitcroft
1357000d1cc1SJoe Perchessub show_type {
1358000d1cc1SJoe Perches       return !defined $ignore_type{$_[0]};
1359000d1cc1SJoe Perches}
1360000d1cc1SJoe Perches
1361f0a594c1SAndy Whitcroftsub report {
1362000d1cc1SJoe Perches	if (!show_type($_[1]) ||
1363000d1cc1SJoe Perches	    (defined $tst_only && $_[2] !~ /\Q$tst_only\E/)) {
1364773647a0SAndy Whitcroft		return 0;
1365773647a0SAndy Whitcroft	}
1366000d1cc1SJoe Perches	my $line;
1367000d1cc1SJoe Perches	if ($show_types) {
1368000d1cc1SJoe Perches		$line = "$prefix$_[0]:$_[1]: $_[2]\n";
1369000d1cc1SJoe Perches	} else {
1370000d1cc1SJoe Perches		$line = "$prefix$_[0]: $_[2]\n";
1371000d1cc1SJoe Perches	}
13728905a67cSAndy Whitcroft	$line = (split('\n', $line))[0] . "\n" if ($terse);
13738905a67cSAndy Whitcroft
137413214adfSAndy Whitcroft	push(our @report, $line);
1375773647a0SAndy Whitcroft
1376773647a0SAndy Whitcroft	return 1;
1377f0a594c1SAndy Whitcroft}
1378f0a594c1SAndy Whitcroftsub report_dump {
137913214adfSAndy Whitcroft	our @report;
1380f0a594c1SAndy Whitcroft}
1381000d1cc1SJoe Perches
1382de7d4f0eSAndy Whitcroftsub ERROR {
1383000d1cc1SJoe Perches	if (report("ERROR", $_[0], $_[1])) {
1384de7d4f0eSAndy Whitcroft		our $clean = 0;
13856c72ffaaSAndy Whitcroft		our $cnt_error++;
13863705ce5bSJoe Perches		return 1;
1387de7d4f0eSAndy Whitcroft	}
13883705ce5bSJoe Perches	return 0;
1389773647a0SAndy Whitcroft}
1390de7d4f0eSAndy Whitcroftsub WARN {
1391000d1cc1SJoe Perches	if (report("WARNING", $_[0], $_[1])) {
1392de7d4f0eSAndy Whitcroft		our $clean = 0;
13936c72ffaaSAndy Whitcroft		our $cnt_warn++;
13943705ce5bSJoe Perches		return 1;
1395de7d4f0eSAndy Whitcroft	}
13963705ce5bSJoe Perches	return 0;
1397773647a0SAndy Whitcroft}
1398de7d4f0eSAndy Whitcroftsub CHK {
1399000d1cc1SJoe Perches	if ($check && report("CHECK", $_[0], $_[1])) {
1400de7d4f0eSAndy Whitcroft		our $clean = 0;
14016c72ffaaSAndy Whitcroft		our $cnt_chk++;
14023705ce5bSJoe Perches		return 1;
14036c72ffaaSAndy Whitcroft	}
14043705ce5bSJoe Perches	return 0;
1405de7d4f0eSAndy Whitcroft}
1406de7d4f0eSAndy Whitcroft
14076ecd9674SAndy Whitcroftsub check_absolute_file {
14086ecd9674SAndy Whitcroft	my ($absolute, $herecurr) = @_;
14096ecd9674SAndy Whitcroft	my $file = $absolute;
14106ecd9674SAndy Whitcroft
14116ecd9674SAndy Whitcroft	##print "absolute<$absolute>\n";
14126ecd9674SAndy Whitcroft
14136ecd9674SAndy Whitcroft	# See if any suffix of this path is a path within the tree.
14146ecd9674SAndy Whitcroft	while ($file =~ s@^[^/]*/@@) {
14156ecd9674SAndy Whitcroft		if (-f "$root/$file") {
14166ecd9674SAndy Whitcroft			##print "file<$file>\n";
14176ecd9674SAndy Whitcroft			last;
14186ecd9674SAndy Whitcroft		}
14196ecd9674SAndy Whitcroft	}
14206ecd9674SAndy Whitcroft	if (! -f _)  {
14216ecd9674SAndy Whitcroft		return 0;
14226ecd9674SAndy Whitcroft	}
14236ecd9674SAndy Whitcroft
14246ecd9674SAndy Whitcroft	# It is, so see if the prefix is acceptable.
14256ecd9674SAndy Whitcroft	my $prefix = $absolute;
14266ecd9674SAndy Whitcroft	substr($prefix, -length($file)) = '';
14276ecd9674SAndy Whitcroft
14286ecd9674SAndy Whitcroft	##print "prefix<$prefix>\n";
14296ecd9674SAndy Whitcroft	if ($prefix ne ".../") {
1430000d1cc1SJoe Perches		WARN("USE_RELATIVE_PATH",
1431000d1cc1SJoe Perches		     "use relative pathname instead of absolute in changelog text\n" . $herecurr);
14326ecd9674SAndy Whitcroft	}
14336ecd9674SAndy Whitcroft}
14346ecd9674SAndy Whitcroft
14353705ce5bSJoe Perchessub trim {
14363705ce5bSJoe Perches	my ($string) = @_;
14373705ce5bSJoe Perches
14383705ce5bSJoe Perches	$string =~ s/(^\s+|\s+$)//g;
14393705ce5bSJoe Perches
14403705ce5bSJoe Perches	return $string;
14413705ce5bSJoe Perches}
14423705ce5bSJoe Perches
14433705ce5bSJoe Perchessub tabify {
14443705ce5bSJoe Perches	my ($leading) = @_;
14453705ce5bSJoe Perches
14463705ce5bSJoe Perches	my $source_indent = 8;
14473705ce5bSJoe Perches	my $max_spaces_before_tab = $source_indent - 1;
14483705ce5bSJoe Perches	my $spaces_to_tab = " " x $source_indent;
14493705ce5bSJoe Perches
14503705ce5bSJoe Perches	#convert leading spaces to tabs
14513705ce5bSJoe Perches	1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
14523705ce5bSJoe Perches	#Remove spaces before a tab
14533705ce5bSJoe Perches	1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
14543705ce5bSJoe Perches
14553705ce5bSJoe Perches	return "$leading";
14563705ce5bSJoe Perches}
14573705ce5bSJoe Perches
1458d1fe9c09SJoe Perchessub pos_last_openparen {
1459d1fe9c09SJoe Perches	my ($line) = @_;
1460d1fe9c09SJoe Perches
1461d1fe9c09SJoe Perches	my $pos = 0;
1462d1fe9c09SJoe Perches
1463d1fe9c09SJoe Perches	my $opens = $line =~ tr/\(/\(/;
1464d1fe9c09SJoe Perches	my $closes = $line =~ tr/\)/\)/;
1465d1fe9c09SJoe Perches
1466d1fe9c09SJoe Perches	my $last_openparen = 0;
1467d1fe9c09SJoe Perches
1468d1fe9c09SJoe Perches	if (($opens == 0) || ($closes >= $opens)) {
1469d1fe9c09SJoe Perches		return -1;
1470d1fe9c09SJoe Perches	}
1471d1fe9c09SJoe Perches
1472d1fe9c09SJoe Perches	my $len = length($line);
1473d1fe9c09SJoe Perches
1474d1fe9c09SJoe Perches	for ($pos = 0; $pos < $len; $pos++) {
1475d1fe9c09SJoe Perches		my $string = substr($line, $pos);
1476d1fe9c09SJoe Perches		if ($string =~ /^($FuncArg|$balanced_parens)/) {
1477d1fe9c09SJoe Perches			$pos += length($1) - 1;
1478d1fe9c09SJoe Perches		} elsif (substr($line, $pos, 1) eq '(') {
1479d1fe9c09SJoe Perches			$last_openparen = $pos;
1480d1fe9c09SJoe Perches		} elsif (index($string, '(') == -1) {
1481d1fe9c09SJoe Perches			last;
1482d1fe9c09SJoe Perches		}
1483d1fe9c09SJoe Perches	}
1484d1fe9c09SJoe Perches
1485d1fe9c09SJoe Perches	return $last_openparen + 1;
1486d1fe9c09SJoe Perches}
1487d1fe9c09SJoe Perches
14880a920b5bSAndy Whitcroftsub process {
14890a920b5bSAndy Whitcroft	my $filename = shift;
14900a920b5bSAndy Whitcroft
14910a920b5bSAndy Whitcroft	my $linenr=0;
14920a920b5bSAndy Whitcroft	my $prevline="";
1493c2fdda0dSAndy Whitcroft	my $prevrawline="";
14940a920b5bSAndy Whitcroft	my $stashline="";
1495c2fdda0dSAndy Whitcroft	my $stashrawline="";
14960a920b5bSAndy Whitcroft
14974a0df2efSAndy Whitcroft	my $length;
14980a920b5bSAndy Whitcroft	my $indent;
14990a920b5bSAndy Whitcroft	my $previndent=0;
15000a920b5bSAndy Whitcroft	my $stashindent=0;
15010a920b5bSAndy Whitcroft
1502de7d4f0eSAndy Whitcroft	our $clean = 1;
15030a920b5bSAndy Whitcroft	my $signoff = 0;
15040a920b5bSAndy Whitcroft	my $is_patch = 0;
15050a920b5bSAndy Whitcroft
150615662b3eSJoe Perches	my $in_header_lines = 1;
150715662b3eSJoe Perches	my $in_commit_log = 0;		#Scanning lines before patch
150815662b3eSJoe Perches
1509fa64205dSPasi Savanainen	my $non_utf8_charset = 0;
1510fa64205dSPasi Savanainen
151113214adfSAndy Whitcroft	our @report = ();
15126c72ffaaSAndy Whitcroft	our $cnt_lines = 0;
15136c72ffaaSAndy Whitcroft	our $cnt_error = 0;
15146c72ffaaSAndy Whitcroft	our $cnt_warn = 0;
15156c72ffaaSAndy Whitcroft	our $cnt_chk = 0;
15166c72ffaaSAndy Whitcroft
15170a920b5bSAndy Whitcroft	# Trace the real file/line as we go.
15180a920b5bSAndy Whitcroft	my $realfile = '';
15190a920b5bSAndy Whitcroft	my $realline = 0;
15200a920b5bSAndy Whitcroft	my $realcnt = 0;
15210a920b5bSAndy Whitcroft	my $here = '';
15220a920b5bSAndy Whitcroft	my $in_comment = 0;
1523c2fdda0dSAndy Whitcroft	my $comment_edge = 0;
15240a920b5bSAndy Whitcroft	my $first_line = 0;
15251e855726SWolfram Sang	my $p1_prefix = '';
15260a920b5bSAndy Whitcroft
152713214adfSAndy Whitcroft	my $prev_values = 'E';
152813214adfSAndy Whitcroft
152913214adfSAndy Whitcroft	# suppression flags
1530773647a0SAndy Whitcroft	my %suppress_ifbraces;
1531170d3a22SAndy Whitcroft	my %suppress_whiletrailers;
15322b474a1aSAndy Whitcroft	my %suppress_export;
15333e469cdcSAndy Whitcroft	my $suppress_statement = 0;
1534653d4876SAndy Whitcroft
1535323c1260SJoe Perches
1536c2fdda0dSAndy Whitcroft	# Pre-scan the patch sanitizing the lines.
1537de7d4f0eSAndy Whitcroft	# Pre-scan the patch looking for any __setup documentation.
1538c2fdda0dSAndy Whitcroft	#
1539de7d4f0eSAndy Whitcroft	my @setup_docs = ();
1540de7d4f0eSAndy Whitcroft	my $setup_docs = 0;
1541773647a0SAndy Whitcroft
1542773647a0SAndy Whitcroft	sanitise_line_reset();
1543c2fdda0dSAndy Whitcroft	my $line;
1544c2fdda0dSAndy Whitcroft	foreach my $rawline (@rawlines) {
1545773647a0SAndy Whitcroft		$linenr++;
1546773647a0SAndy Whitcroft		$line = $rawline;
1547c2fdda0dSAndy Whitcroft
15483705ce5bSJoe Perches		push(@fixed, $rawline) if ($fix);
15493705ce5bSJoe Perches
1550773647a0SAndy Whitcroft		if ($rawline=~/^\+\+\+\s+(\S+)/) {
1551de7d4f0eSAndy Whitcroft			$setup_docs = 0;
1552de7d4f0eSAndy Whitcroft			if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
1553de7d4f0eSAndy Whitcroft				$setup_docs = 1;
1554de7d4f0eSAndy Whitcroft			}
1555773647a0SAndy Whitcroft			#next;
1556de7d4f0eSAndy Whitcroft		}
1557773647a0SAndy Whitcroft		if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1558773647a0SAndy Whitcroft			$realline=$1-1;
1559773647a0SAndy Whitcroft			if (defined $2) {
1560773647a0SAndy Whitcroft				$realcnt=$3+1;
1561773647a0SAndy Whitcroft			} else {
1562773647a0SAndy Whitcroft				$realcnt=1+1;
1563773647a0SAndy Whitcroft			}
1564c45dcabdSAndy Whitcroft			$in_comment = 0;
1565773647a0SAndy Whitcroft
1566773647a0SAndy Whitcroft			# Guestimate if this is a continuing comment.  Run
1567773647a0SAndy Whitcroft			# the context looking for a comment "edge".  If this
1568773647a0SAndy Whitcroft			# edge is a close comment then we must be in a comment
1569773647a0SAndy Whitcroft			# at context start.
1570773647a0SAndy Whitcroft			my $edge;
157101fa9147SAndy Whitcroft			my $cnt = $realcnt;
157201fa9147SAndy Whitcroft			for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
157301fa9147SAndy Whitcroft				next if (defined $rawlines[$ln - 1] &&
157401fa9147SAndy Whitcroft					 $rawlines[$ln - 1] =~ /^-/);
157501fa9147SAndy Whitcroft				$cnt--;
157601fa9147SAndy Whitcroft				#print "RAW<$rawlines[$ln - 1]>\n";
1577721c1cb6SAndy Whitcroft				last if (!defined $rawlines[$ln - 1]);
1578fae17daeSAndy Whitcroft				if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
1579fae17daeSAndy Whitcroft				    $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
1580fae17daeSAndy Whitcroft					($edge) = $1;
1581fae17daeSAndy Whitcroft					last;
1582fae17daeSAndy Whitcroft				}
1583773647a0SAndy Whitcroft			}
1584773647a0SAndy Whitcroft			if (defined $edge && $edge eq '*/') {
1585773647a0SAndy Whitcroft				$in_comment = 1;
1586773647a0SAndy Whitcroft			}
1587773647a0SAndy Whitcroft
1588773647a0SAndy Whitcroft			# Guestimate if this is a continuing comment.  If this
1589773647a0SAndy Whitcroft			# is the start of a diff block and this line starts
1590773647a0SAndy Whitcroft			# ' *' then it is very likely a comment.
1591773647a0SAndy Whitcroft			if (!defined $edge &&
159283242e0cSAndy Whitcroft			    $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
1593773647a0SAndy Whitcroft			{
1594773647a0SAndy Whitcroft				$in_comment = 1;
1595773647a0SAndy Whitcroft			}
1596773647a0SAndy Whitcroft
1597773647a0SAndy Whitcroft			##print "COMMENT:$in_comment edge<$edge> $rawline\n";
1598773647a0SAndy Whitcroft			sanitise_line_reset($in_comment);
1599773647a0SAndy Whitcroft
1600171ae1a4SAndy Whitcroft		} elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
1601773647a0SAndy Whitcroft			# Standardise the strings and chars within the input to
1602171ae1a4SAndy Whitcroft			# simplify matching -- only bother with positive lines.
1603773647a0SAndy Whitcroft			$line = sanitise_line($rawline);
1604773647a0SAndy Whitcroft		}
1605773647a0SAndy Whitcroft		push(@lines, $line);
1606773647a0SAndy Whitcroft
1607773647a0SAndy Whitcroft		if ($realcnt > 1) {
1608773647a0SAndy Whitcroft			$realcnt-- if ($line =~ /^(?:\+| |$)/);
1609773647a0SAndy Whitcroft		} else {
1610773647a0SAndy Whitcroft			$realcnt = 0;
1611773647a0SAndy Whitcroft		}
1612773647a0SAndy Whitcroft
1613773647a0SAndy Whitcroft		#print "==>$rawline\n";
1614773647a0SAndy Whitcroft		#print "-->$line\n";
1615de7d4f0eSAndy Whitcroft
1616de7d4f0eSAndy Whitcroft		if ($setup_docs && $line =~ /^\+/) {
1617de7d4f0eSAndy Whitcroft			push(@setup_docs, $line);
1618de7d4f0eSAndy Whitcroft		}
1619de7d4f0eSAndy Whitcroft	}
1620de7d4f0eSAndy Whitcroft
16216c72ffaaSAndy Whitcroft	$prefix = '';
16226c72ffaaSAndy Whitcroft
1623773647a0SAndy Whitcroft	$realcnt = 0;
1624773647a0SAndy Whitcroft	$linenr = 0;
16250a920b5bSAndy Whitcroft	foreach my $line (@lines) {
16260a920b5bSAndy Whitcroft		$linenr++;
16270a920b5bSAndy Whitcroft
1628c2fdda0dSAndy Whitcroft		my $rawline = $rawlines[$linenr - 1];
16296c72ffaaSAndy Whitcroft
16300a920b5bSAndy Whitcroft#extract the line range in the file after the patch is applied
16316c72ffaaSAndy Whitcroft		if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
16320a920b5bSAndy Whitcroft			$is_patch = 1;
16334a0df2efSAndy Whitcroft			$first_line = $linenr + 1;
16340a920b5bSAndy Whitcroft			$realline=$1-1;
16350a920b5bSAndy Whitcroft			if (defined $2) {
16360a920b5bSAndy Whitcroft				$realcnt=$3+1;
16370a920b5bSAndy Whitcroft			} else {
16380a920b5bSAndy Whitcroft				$realcnt=1+1;
16390a920b5bSAndy Whitcroft			}
1640c2fdda0dSAndy Whitcroft			annotate_reset();
164113214adfSAndy Whitcroft			$prev_values = 'E';
164213214adfSAndy Whitcroft
1643773647a0SAndy Whitcroft			%suppress_ifbraces = ();
1644170d3a22SAndy Whitcroft			%suppress_whiletrailers = ();
16452b474a1aSAndy Whitcroft			%suppress_export = ();
16463e469cdcSAndy Whitcroft			$suppress_statement = 0;
16470a920b5bSAndy Whitcroft			next;
16480a920b5bSAndy Whitcroft
16494a0df2efSAndy Whitcroft# track the line number as we move through the hunk, note that
16504a0df2efSAndy Whitcroft# new versions of GNU diff omit the leading space on completely
16514a0df2efSAndy Whitcroft# blank context lines so we need to count that too.
1652773647a0SAndy Whitcroft		} elsif ($line =~ /^( |\+|$)/) {
16530a920b5bSAndy Whitcroft			$realline++;
1654d8aaf121SAndy Whitcroft			$realcnt-- if ($realcnt != 0);
16550a920b5bSAndy Whitcroft
16564a0df2efSAndy Whitcroft			# Measure the line length and indent.
1657c2fdda0dSAndy Whitcroft			($length, $indent) = line_stats($rawline);
16580a920b5bSAndy Whitcroft
16590a920b5bSAndy Whitcroft			# Track the previous line.
16600a920b5bSAndy Whitcroft			($prevline, $stashline) = ($stashline, $line);
16610a920b5bSAndy Whitcroft			($previndent, $stashindent) = ($stashindent, $indent);
1662c2fdda0dSAndy Whitcroft			($prevrawline, $stashrawline) = ($stashrawline, $rawline);
1663c2fdda0dSAndy Whitcroft
1664773647a0SAndy Whitcroft			#warn "line<$line>\n";
16656c72ffaaSAndy Whitcroft
1666d8aaf121SAndy Whitcroft		} elsif ($realcnt == 1) {
1667d8aaf121SAndy Whitcroft			$realcnt--;
16680a920b5bSAndy Whitcroft		}
16690a920b5bSAndy Whitcroft
1670cc77cdcaSAndy Whitcroft		my $hunk_line = ($realcnt != 0);
1671cc77cdcaSAndy Whitcroft
16720a920b5bSAndy Whitcroft#make up the handle for any error we report on this line
1673773647a0SAndy Whitcroft		$prefix = "$filename:$realline: " if ($emacs && $file);
1674773647a0SAndy Whitcroft		$prefix = "$filename:$linenr: " if ($emacs && !$file);
1675773647a0SAndy Whitcroft
16766c72ffaaSAndy Whitcroft		$here = "#$linenr: " if (!$file);
16776c72ffaaSAndy Whitcroft		$here = "#$realline: " if ($file);
1678773647a0SAndy Whitcroft
1679773647a0SAndy Whitcroft		# extract the filename as it passes
16803bf9a009SRabin Vincent		if ($line =~ /^diff --git.*?(\S+)$/) {
16813bf9a009SRabin Vincent			$realfile = $1;
16823bf9a009SRabin Vincent			$realfile =~ s@^([^/]*)/@@;
1683270c49a0SJoe Perches			$in_commit_log = 0;
16843bf9a009SRabin Vincent		} elsif ($line =~ /^\+\+\+\s+(\S+)/) {
1685773647a0SAndy Whitcroft			$realfile = $1;
16861e855726SWolfram Sang			$realfile =~ s@^([^/]*)/@@;
1687270c49a0SJoe Perches			$in_commit_log = 0;
16881e855726SWolfram Sang
16891e855726SWolfram Sang			$p1_prefix = $1;
1690e2f7aa4bSAndy Whitcroft			if (!$file && $tree && $p1_prefix ne '' &&
1691e2f7aa4bSAndy Whitcroft			    -e "$root/$p1_prefix") {
1692000d1cc1SJoe Perches				WARN("PATCH_PREFIX",
1693000d1cc1SJoe Perches				     "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
16941e855726SWolfram Sang			}
1695773647a0SAndy Whitcroft
1696c1ab3326SAndy Whitcroft			if ($realfile =~ m@^include/asm/@) {
1697000d1cc1SJoe Perches				ERROR("MODIFIED_INCLUDE_ASM",
1698000d1cc1SJoe Perches				      "do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
1699773647a0SAndy Whitcroft			}
1700773647a0SAndy Whitcroft			next;
1701773647a0SAndy Whitcroft		}
1702773647a0SAndy Whitcroft
1703389834b6SRandy Dunlap		$here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
17040a920b5bSAndy Whitcroft
1705c2fdda0dSAndy Whitcroft		my $hereline = "$here\n$rawline\n";
1706c2fdda0dSAndy Whitcroft		my $herecurr = "$here\n$rawline\n";
1707c2fdda0dSAndy Whitcroft		my $hereprev = "$here\n$prevrawline\n$rawline\n";
17080a920b5bSAndy Whitcroft
17096c72ffaaSAndy Whitcroft		$cnt_lines++ if ($realcnt != 0);
17106c72ffaaSAndy Whitcroft
17113bf9a009SRabin Vincent# Check for incorrect file permissions
17123bf9a009SRabin Vincent		if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
17133bf9a009SRabin Vincent			my $permhere = $here . "FILE: $realfile\n";
171404db4d25SJoe Perches			if ($realfile !~ m@scripts/@ &&
171504db4d25SJoe Perches			    $realfile !~ /\.(py|pl|awk|sh)$/) {
1716000d1cc1SJoe Perches				ERROR("EXECUTE_PERMISSIONS",
1717000d1cc1SJoe Perches				      "do not set execute permissions for source files\n" . $permhere);
17183bf9a009SRabin Vincent			}
17193bf9a009SRabin Vincent		}
17203bf9a009SRabin Vincent
172120112475SJoe Perches# Check the patch for a signoff:
1722d8aaf121SAndy Whitcroft		if ($line =~ /^\s*signed-off-by:/i) {
17234a0df2efSAndy Whitcroft			$signoff++;
172415662b3eSJoe Perches			$in_commit_log = 0;
17250a920b5bSAndy Whitcroft		}
172620112475SJoe Perches
172720112475SJoe Perches# Check signature styles
1728270c49a0SJoe Perches		if (!$in_header_lines &&
1729ce0338dfSJoe Perches		    $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
173020112475SJoe Perches			my $space_before = $1;
173120112475SJoe Perches			my $sign_off = $2;
173220112475SJoe Perches			my $space_after = $3;
173320112475SJoe Perches			my $email = $4;
173420112475SJoe Perches			my $ucfirst_sign_off = ucfirst(lc($sign_off));
173520112475SJoe Perches
1736ce0338dfSJoe Perches			if ($sign_off !~ /$signature_tags/) {
1737ce0338dfSJoe Perches				WARN("BAD_SIGN_OFF",
1738ce0338dfSJoe Perches				     "Non-standard signature: $sign_off\n" . $herecurr);
1739ce0338dfSJoe Perches			}
174020112475SJoe Perches			if (defined $space_before && $space_before ne "") {
17413705ce5bSJoe Perches				if (WARN("BAD_SIGN_OFF",
17423705ce5bSJoe Perches					 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
17433705ce5bSJoe Perches				    $fix) {
17443705ce5bSJoe Perches					$fixed[$linenr - 1] =
17453705ce5bSJoe Perches					    "$ucfirst_sign_off $email";
17463705ce5bSJoe Perches				}
174720112475SJoe Perches			}
174820112475SJoe Perches			if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
17493705ce5bSJoe Perches				if (WARN("BAD_SIGN_OFF",
17503705ce5bSJoe Perches					 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
17513705ce5bSJoe Perches				    $fix) {
17523705ce5bSJoe Perches					$fixed[$linenr - 1] =
17533705ce5bSJoe Perches					    "$ucfirst_sign_off $email";
17543705ce5bSJoe Perches				}
17553705ce5bSJoe Perches
175620112475SJoe Perches			}
175720112475SJoe Perches			if (!defined $space_after || $space_after ne " ") {
17583705ce5bSJoe Perches				if (WARN("BAD_SIGN_OFF",
17593705ce5bSJoe Perches					 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
17603705ce5bSJoe Perches				    $fix) {
17613705ce5bSJoe Perches					$fixed[$linenr - 1] =
17623705ce5bSJoe Perches					    "$ucfirst_sign_off $email";
17633705ce5bSJoe Perches				}
176420112475SJoe Perches			}
176520112475SJoe Perches
176620112475SJoe Perches			my ($email_name, $email_address, $comment) = parse_email($email);
176720112475SJoe Perches			my $suggested_email = format_email(($email_name, $email_address));
176820112475SJoe Perches			if ($suggested_email eq "") {
1769000d1cc1SJoe Perches				ERROR("BAD_SIGN_OFF",
1770000d1cc1SJoe Perches				      "Unrecognized email address: '$email'\n" . $herecurr);
177120112475SJoe Perches			} else {
177220112475SJoe Perches				my $dequoted = $suggested_email;
177320112475SJoe Perches				$dequoted =~ s/^"//;
177420112475SJoe Perches				$dequoted =~ s/" </ </;
177520112475SJoe Perches				# Don't force email to have quotes
177620112475SJoe Perches				# Allow just an angle bracketed address
177720112475SJoe Perches				if ("$dequoted$comment" ne $email &&
177820112475SJoe Perches				    "<$email_address>$comment" ne $email &&
177920112475SJoe Perches				    "$suggested_email$comment" ne $email) {
1780000d1cc1SJoe Perches					WARN("BAD_SIGN_OFF",
1781000d1cc1SJoe Perches					     "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
178220112475SJoe Perches				}
17830a920b5bSAndy Whitcroft			}
17840a920b5bSAndy Whitcroft		}
17850a920b5bSAndy Whitcroft
178600df344fSAndy Whitcroft# Check for wrappage within a valid hunk of the file
17878905a67cSAndy Whitcroft		if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
1788000d1cc1SJoe Perches			ERROR("CORRUPTED_PATCH",
1789000d1cc1SJoe Perches			      "patch seems to be corrupt (line wrapped?)\n" .
17906c72ffaaSAndy Whitcroft				$herecurr) if (!$emitted_corrupt++);
1791de7d4f0eSAndy Whitcroft		}
1792de7d4f0eSAndy Whitcroft
17936ecd9674SAndy Whitcroft# Check for absolute kernel paths.
17946ecd9674SAndy Whitcroft		if ($tree) {
17956ecd9674SAndy Whitcroft			while ($line =~ m{(?:^|\s)(/\S*)}g) {
17966ecd9674SAndy Whitcroft				my $file = $1;
17976ecd9674SAndy Whitcroft
17986ecd9674SAndy Whitcroft				if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
17996ecd9674SAndy Whitcroft				    check_absolute_file($1, $herecurr)) {
18006ecd9674SAndy Whitcroft					#
18016ecd9674SAndy Whitcroft				} else {
18026ecd9674SAndy Whitcroft					check_absolute_file($file, $herecurr);
18036ecd9674SAndy Whitcroft				}
18046ecd9674SAndy Whitcroft			}
18056ecd9674SAndy Whitcroft		}
18066ecd9674SAndy Whitcroft
1807de7d4f0eSAndy Whitcroft# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
1808de7d4f0eSAndy Whitcroft		if (($realfile =~ /^$/ || $line =~ /^\+/) &&
1809171ae1a4SAndy Whitcroft		    $rawline !~ m/^$UTF8*$/) {
1810171ae1a4SAndy Whitcroft			my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
1811171ae1a4SAndy Whitcroft
1812171ae1a4SAndy Whitcroft			my $blank = copy_spacing($rawline);
1813171ae1a4SAndy Whitcroft			my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
1814171ae1a4SAndy Whitcroft			my $hereptr = "$hereline$ptr\n";
1815171ae1a4SAndy Whitcroft
181634d99219SJoe Perches			CHK("INVALID_UTF8",
1817000d1cc1SJoe Perches			    "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
181800df344fSAndy Whitcroft		}
18190a920b5bSAndy Whitcroft
182015662b3eSJoe Perches# Check if it's the start of a commit log
182115662b3eSJoe Perches# (not a header line and we haven't seen the patch filename)
182215662b3eSJoe Perches		if ($in_header_lines && $realfile =~ /^$/ &&
1823270c49a0SJoe Perches		    $rawline !~ /^(commit\b|from\b|[\w-]+:).+$/i) {
182415662b3eSJoe Perches			$in_header_lines = 0;
182515662b3eSJoe Perches			$in_commit_log = 1;
182615662b3eSJoe Perches		}
182715662b3eSJoe Perches
1828fa64205dSPasi Savanainen# Check if there is UTF-8 in a commit log when a mail header has explicitly
1829fa64205dSPasi Savanainen# declined it, i.e defined some charset where it is missing.
1830fa64205dSPasi Savanainen		if ($in_header_lines &&
1831fa64205dSPasi Savanainen		    $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
1832fa64205dSPasi Savanainen		    $1 !~ /utf-8/i) {
1833fa64205dSPasi Savanainen			$non_utf8_charset = 1;
1834fa64205dSPasi Savanainen		}
1835fa64205dSPasi Savanainen
1836fa64205dSPasi Savanainen		if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
183715662b3eSJoe Perches		    $rawline =~ /$NON_ASCII_UTF8/) {
1838fa64205dSPasi Savanainen			WARN("UTF8_BEFORE_PATCH",
183915662b3eSJoe Perches			    "8-bit UTF-8 used in possible commit log\n" . $herecurr);
184015662b3eSJoe Perches		}
184115662b3eSJoe Perches
184230670854SAndy Whitcroft# ignore non-hunk lines and lines being removed
184330670854SAndy Whitcroft		next if (!$hunk_line || $line =~ /^-/);
184400df344fSAndy Whitcroft
18450a920b5bSAndy Whitcroft#trailing whitespace
18469c0ca6f9SAndy Whitcroft		if ($line =~ /^\+.*\015/) {
1847c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1848000d1cc1SJoe Perches			ERROR("DOS_LINE_ENDINGS",
1849000d1cc1SJoe Perches			      "DOS line endings\n" . $herevet);
18509c0ca6f9SAndy Whitcroft
1851c2fdda0dSAndy Whitcroft		} elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
1852c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
18533705ce5bSJoe Perches			if (ERROR("TRAILING_WHITESPACE",
18543705ce5bSJoe Perches				  "trailing whitespace\n" . $herevet) &&
18553705ce5bSJoe Perches			    $fix) {
18563705ce5bSJoe Perches				$fixed[$linenr - 1] =~ s/^(\+.*?)\s+$/$1/;
18573705ce5bSJoe Perches			}
18583705ce5bSJoe Perches
1859d2c0a235SAndy Whitcroft			$rpt_cleaners = 1;
18600a920b5bSAndy Whitcroft		}
18615368df20SAndy Whitcroft
18623354957aSAndi Kleen# check for Kconfig help text having a real description
18639fe287d7SAndy Whitcroft# Only applies when adding the entry originally, after that we do not have
18649fe287d7SAndy Whitcroft# sufficient context to determine whether it is indeed long enough.
18653354957aSAndi Kleen		if ($realfile =~ /Kconfig/ &&
1866a1385803SAndy Whitcroft		    $line =~ /.\s*config\s+/) {
18673354957aSAndi Kleen			my $length = 0;
18689fe287d7SAndy Whitcroft			my $cnt = $realcnt;
18699fe287d7SAndy Whitcroft			my $ln = $linenr + 1;
18709fe287d7SAndy Whitcroft			my $f;
1871a1385803SAndy Whitcroft			my $is_start = 0;
18729fe287d7SAndy Whitcroft			my $is_end = 0;
1873a1385803SAndy Whitcroft			for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
18749fe287d7SAndy Whitcroft				$f = $lines[$ln - 1];
18759fe287d7SAndy Whitcroft				$cnt-- if ($lines[$ln - 1] !~ /^-/);
18769fe287d7SAndy Whitcroft				$is_end = $lines[$ln - 1] =~ /^\+/;
18779fe287d7SAndy Whitcroft
18789fe287d7SAndy Whitcroft				next if ($f =~ /^-/);
1879a1385803SAndy Whitcroft
1880a1385803SAndy Whitcroft				if ($lines[$ln - 1] =~ /.\s*(?:bool|tristate)\s*\"/) {
1881a1385803SAndy Whitcroft					$is_start = 1;
1882a1385803SAndy Whitcroft				} elsif ($lines[$ln - 1] =~ /.\s*(?:---)?help(?:---)?$/) {
1883a1385803SAndy Whitcroft					$length = -1;
1884a1385803SAndy Whitcroft				}
1885a1385803SAndy Whitcroft
18869fe287d7SAndy Whitcroft				$f =~ s/^.//;
18873354957aSAndi Kleen				$f =~ s/#.*//;
18883354957aSAndi Kleen				$f =~ s/^\s+//;
18893354957aSAndi Kleen				next if ($f =~ /^$/);
18909fe287d7SAndy Whitcroft				if ($f =~ /^\s*config\s/) {
18919fe287d7SAndy Whitcroft					$is_end = 1;
18929fe287d7SAndy Whitcroft					last;
18939fe287d7SAndy Whitcroft				}
18943354957aSAndi Kleen				$length++;
18953354957aSAndi Kleen			}
1896000d1cc1SJoe Perches			WARN("CONFIG_DESCRIPTION",
1897a1385803SAndy Whitcroft			     "please write a paragraph that describes the config symbol fully\n" . $herecurr) if ($is_start && $is_end && $length < 4);
1898a1385803SAndy Whitcroft			#print "is_start<$is_start> is_end<$is_end> length<$length>\n";
18993354957aSAndi Kleen		}
19003354957aSAndi Kleen
19011ba8dfd1SKees Cook# discourage the addition of CONFIG_EXPERIMENTAL in Kconfig.
19021ba8dfd1SKees Cook		if ($realfile =~ /Kconfig/ &&
19031ba8dfd1SKees Cook		    $line =~ /.\s*depends on\s+.*\bEXPERIMENTAL\b/) {
19041ba8dfd1SKees Cook			WARN("CONFIG_EXPERIMENTAL",
19051ba8dfd1SKees Cook			     "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
19061ba8dfd1SKees Cook		}
19071ba8dfd1SKees Cook
1908c68e5878SArnaud Lacombe		if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
1909c68e5878SArnaud Lacombe		    ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
1910c68e5878SArnaud Lacombe			my $flag = $1;
1911c68e5878SArnaud Lacombe			my $replacement = {
1912c68e5878SArnaud Lacombe				'EXTRA_AFLAGS' =>   'asflags-y',
1913c68e5878SArnaud Lacombe				'EXTRA_CFLAGS' =>   'ccflags-y',
1914c68e5878SArnaud Lacombe				'EXTRA_CPPFLAGS' => 'cppflags-y',
1915c68e5878SArnaud Lacombe				'EXTRA_LDFLAGS' =>  'ldflags-y',
1916c68e5878SArnaud Lacombe			};
1917c68e5878SArnaud Lacombe
1918c68e5878SArnaud Lacombe			WARN("DEPRECATED_VARIABLE",
1919c68e5878SArnaud Lacombe			     "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
1920c68e5878SArnaud Lacombe		}
1921c68e5878SArnaud Lacombe
19225368df20SAndy Whitcroft# check we are in a valid source file if not then ignore this hunk
19235368df20SAndy Whitcroft		next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
19245368df20SAndy Whitcroft
19256cd7f386SJoe Perches#line length limit
1926c45dcabdSAndy Whitcroft		if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ &&
1927f4c014c0SAndy Whitcroft		    $rawline !~ /^.\s*\*\s*\@$Ident\s/ &&
19280fccc622SJoe Perches		    !($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(KERN_\S+\s*|[^"]*))?"[X\t]*"\s*(?:|,|\)\s*;)\s*$/ ||
19298bbea968SJoe Perches		    $line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) &&
19306cd7f386SJoe Perches		    $length > $max_line_length)
1931c45dcabdSAndy Whitcroft		{
1932000d1cc1SJoe Perches			WARN("LONG_LINE",
19336cd7f386SJoe Perches			     "line over $max_line_length characters\n" . $herecurr);
19340a920b5bSAndy Whitcroft		}
19350a920b5bSAndy Whitcroft
1936ca56dc09SJosh Triplett# Check for user-visible strings broken across lines, which breaks the ability
1937ca56dc09SJosh Triplett# to grep for the string.  Limited to strings used as parameters (those
1938ca56dc09SJosh Triplett# following an open parenthesis), which almost completely eliminates false
1939ca56dc09SJosh Triplett# positives, as well as warning only once per parameter rather than once per
1940ca56dc09SJosh Triplett# line of the string.  Make an exception when the previous string ends in a
1941ca56dc09SJosh Triplett# newline (multiple lines in one string constant) or \n\t (common in inline
1942ca56dc09SJosh Triplett# assembly to indent the instruction on the following line).
1943ca56dc09SJosh Triplett		if ($line =~ /^\+\s*"/ &&
1944ca56dc09SJosh Triplett		    $prevline =~ /"\s*$/ &&
1945ca56dc09SJosh Triplett		    $prevline =~ /\(/ &&
1946ca56dc09SJosh Triplett		    $prevrawline !~ /\\n(?:\\t)*"\s*$/) {
1947ca56dc09SJosh Triplett			WARN("SPLIT_STRING",
1948ca56dc09SJosh Triplett			     "quoted string split across lines\n" . $hereprev);
1949ca56dc09SJosh Triplett		}
1950ca56dc09SJosh Triplett
19515e79d96eSJoe Perches# check for spaces before a quoted newline
19525e79d96eSJoe Perches		if ($rawline =~ /^.*\".*\s\\n/) {
19533705ce5bSJoe Perches			if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
19543705ce5bSJoe Perches				 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
19553705ce5bSJoe Perches			    $fix) {
19563705ce5bSJoe Perches				$fixed[$linenr - 1] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
19573705ce5bSJoe Perches			}
19583705ce5bSJoe Perches
19595e79d96eSJoe Perches		}
19605e79d96eSJoe Perches
19618905a67cSAndy Whitcroft# check for adding lines without a newline.
19628905a67cSAndy Whitcroft		if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
1963000d1cc1SJoe Perches			WARN("MISSING_EOF_NEWLINE",
1964000d1cc1SJoe Perches			     "adding a line without newline at end of file\n" . $herecurr);
19658905a67cSAndy Whitcroft		}
19668905a67cSAndy Whitcroft
196742e41c54SMike Frysinger# Blackfin: use hi/lo macros
196842e41c54SMike Frysinger		if ($realfile =~ m@arch/blackfin/.*\.S$@) {
196942e41c54SMike Frysinger			if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
197042e41c54SMike Frysinger				my $herevet = "$here\n" . cat_vet($line) . "\n";
1971000d1cc1SJoe Perches				ERROR("LO_MACRO",
1972000d1cc1SJoe Perches				      "use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
197342e41c54SMike Frysinger			}
197442e41c54SMike Frysinger			if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
197542e41c54SMike Frysinger				my $herevet = "$here\n" . cat_vet($line) . "\n";
1976000d1cc1SJoe Perches				ERROR("HI_MACRO",
1977000d1cc1SJoe Perches				      "use the HI() macro, not (... >> 16)\n" . $herevet);
197842e41c54SMike Frysinger			}
197942e41c54SMike Frysinger		}
198042e41c54SMike Frysinger
1981b9ea10d6SAndy Whitcroft# check we are in a valid source file C or perl if not then ignore this hunk
1982b9ea10d6SAndy Whitcroft		next if ($realfile !~ /\.(h|c|pl)$/);
19830a920b5bSAndy Whitcroft
19840a920b5bSAndy Whitcroft# at the beginning of a line any tabs must come first and anything
19850a920b5bSAndy Whitcroft# more than 8 must use tabs.
1986c2fdda0dSAndy Whitcroft		if ($rawline =~ /^\+\s* \t\s*\S/ ||
1987c2fdda0dSAndy Whitcroft		    $rawline =~ /^\+\s*        \s*/) {
1988c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1989d2c0a235SAndy Whitcroft			$rpt_cleaners = 1;
19903705ce5bSJoe Perches			if (ERROR("CODE_INDENT",
19913705ce5bSJoe Perches				  "code indent should use tabs where possible\n" . $herevet) &&
19923705ce5bSJoe Perches			    $fix) {
19933705ce5bSJoe Perches				$fixed[$linenr - 1] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
19943705ce5bSJoe Perches			}
19950a920b5bSAndy Whitcroft		}
19960a920b5bSAndy Whitcroft
199708e44365SAlberto Panizzo# check for space before tabs.
199808e44365SAlberto Panizzo		if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
199908e44365SAlberto Panizzo			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
20003705ce5bSJoe Perches			if (WARN("SPACE_BEFORE_TAB",
20013705ce5bSJoe Perches				"please, no space before tabs\n" . $herevet) &&
20023705ce5bSJoe Perches			    $fix) {
20033705ce5bSJoe Perches				$fixed[$linenr - 1] =~
20043705ce5bSJoe Perches				    s/(^\+.*) +\t/$1\t/;
20053705ce5bSJoe Perches			}
200608e44365SAlberto Panizzo		}
200708e44365SAlberto Panizzo
2008d1fe9c09SJoe Perches# check for && or || at the start of a line
2009d1fe9c09SJoe Perches		if ($rawline =~ /^\+\s*(&&|\|\|)/) {
2010d1fe9c09SJoe Perches			CHK("LOGICAL_CONTINUATIONS",
2011d1fe9c09SJoe Perches			    "Logical continuations should be on the previous line\n" . $hereprev);
2012d1fe9c09SJoe Perches		}
2013d1fe9c09SJoe Perches
2014d1fe9c09SJoe Perches# check multi-line statement indentation matches previous line
2015d1fe9c09SJoe Perches		if ($^V && $^V ge 5.10.0 &&
2016d1fe9c09SJoe Perches		    $prevline =~ /^\+(\t*)(if \(|$Ident\().*(\&\&|\|\||,)\s*$/) {
2017d1fe9c09SJoe Perches			$prevline =~ /^\+(\t*)(.*)$/;
2018d1fe9c09SJoe Perches			my $oldindent = $1;
2019d1fe9c09SJoe Perches			my $rest = $2;
2020d1fe9c09SJoe Perches
2021d1fe9c09SJoe Perches			my $pos = pos_last_openparen($rest);
2022d1fe9c09SJoe Perches			if ($pos >= 0) {
2023b34a26f3SJoe Perches				$line =~ /^(\+| )([ \t]*)/;
2024b34a26f3SJoe Perches				my $newindent = $2;
2025d1fe9c09SJoe Perches
2026d1fe9c09SJoe Perches				my $goodtabindent = $oldindent .
2027d1fe9c09SJoe Perches					"\t" x ($pos / 8) .
2028d1fe9c09SJoe Perches					" "  x ($pos % 8);
2029d1fe9c09SJoe Perches				my $goodspaceindent = $oldindent . " "  x $pos;
2030d1fe9c09SJoe Perches
2031d1fe9c09SJoe Perches				if ($newindent ne $goodtabindent &&
2032d1fe9c09SJoe Perches				    $newindent ne $goodspaceindent) {
20333705ce5bSJoe Perches
20343705ce5bSJoe Perches					if (CHK("PARENTHESIS_ALIGNMENT",
20353705ce5bSJoe Perches						"Alignment should match open parenthesis\n" . $hereprev) &&
20363705ce5bSJoe Perches					    $fix && $line =~ /^\+/) {
20373705ce5bSJoe Perches						$fixed[$linenr - 1] =~
20383705ce5bSJoe Perches						    s/^\+[ \t]*/\+$goodtabindent/;
20393705ce5bSJoe Perches					}
2040d1fe9c09SJoe Perches				}
2041d1fe9c09SJoe Perches			}
2042d1fe9c09SJoe Perches		}
2043d1fe9c09SJoe Perches
204423f780c9SJoe Perches		if ($line =~ /^\+.*\*[ \t]*\)[ \t]+(?!$Assignment|$Arithmetic)/) {
20453705ce5bSJoe Perches			if (CHK("SPACING",
20463705ce5bSJoe Perches				"No space is necessary after a cast\n" . $hereprev) &&
20473705ce5bSJoe Perches			    $fix) {
20483705ce5bSJoe Perches				$fixed[$linenr - 1] =~
20493705ce5bSJoe Perches				    s/^(\+.*\*[ \t]*\))[ \t]+/$1/;
20503705ce5bSJoe Perches			}
2051aad4f614SJoe Perches		}
2052aad4f614SJoe Perches
205305880600SJoe Perches		if ($realfile =~ m@^(drivers/net/|net/)@ &&
2054fdb4bcd6SJoe Perches		    $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
2055fdb4bcd6SJoe Perches		    $rawline =~ /^\+[ \t]*\*/) {
205605880600SJoe Perches			WARN("NETWORKING_BLOCK_COMMENT_STYLE",
205705880600SJoe Perches			     "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
205805880600SJoe Perches		}
205905880600SJoe Perches
206005880600SJoe Perches		if ($realfile =~ m@^(drivers/net/|net/)@ &&
2061a605e32eSJoe Perches		    $prevrawline =~ /^\+[ \t]*\/\*/ &&		#starting /*
2062a605e32eSJoe Perches		    $prevrawline !~ /\*\/[ \t]*$/ &&		#no trailing */
2063a605e32eSJoe Perches		    $rawline !~ /^\+[ \t]*\*/) {		#no leading *
2064a605e32eSJoe Perches			WARN("NETWORKING_BLOCK_COMMENT_STYLE",
2065a605e32eSJoe Perches			     "networking block comments start with * on subsequent lines\n" . $hereprev);
2066a605e32eSJoe Perches		}
2067a605e32eSJoe Perches
2068a605e32eSJoe Perches		if ($realfile =~ m@^(drivers/net/|net/)@ &&
2069c24f9f19SJoe Perches		    $rawline !~ m@^\+[ \t]*\*/[ \t]*$@ &&	#trailing */
2070c24f9f19SJoe Perches		    $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ &&	#inline /*...*/
2071c24f9f19SJoe Perches		    $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ &&	#trailing **/
2072c24f9f19SJoe Perches		    $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) {	#non blank */
207305880600SJoe Perches			WARN("NETWORKING_BLOCK_COMMENT_STYLE",
207405880600SJoe Perches			     "networking block comments put the trailing */ on a separate line\n" . $herecurr);
207505880600SJoe Perches		}
207605880600SJoe Perches
20775f7ddae6SRaffaele Recalcati# check for spaces at the beginning of a line.
20786b4c5bebSAndy Whitcroft# Exceptions:
20796b4c5bebSAndy Whitcroft#  1) within comments
20806b4c5bebSAndy Whitcroft#  2) indented preprocessor commands
20816b4c5bebSAndy Whitcroft#  3) hanging labels
20823705ce5bSJoe Perches		if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/)  {
20835f7ddae6SRaffaele Recalcati			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
20843705ce5bSJoe Perches			if (WARN("LEADING_SPACE",
20853705ce5bSJoe Perches				 "please, no spaces at the start of a line\n" . $herevet) &&
20863705ce5bSJoe Perches			    $fix) {
20873705ce5bSJoe Perches				$fixed[$linenr - 1] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
20883705ce5bSJoe Perches			}
20895f7ddae6SRaffaele Recalcati		}
20905f7ddae6SRaffaele Recalcati
2091b9ea10d6SAndy Whitcroft# check we are in a valid C source file if not then ignore this hunk
2092b9ea10d6SAndy Whitcroft		next if ($realfile !~ /\.(h|c)$/);
2093b9ea10d6SAndy Whitcroft
20941ba8dfd1SKees Cook# discourage the addition of CONFIG_EXPERIMENTAL in #if(def).
20951ba8dfd1SKees Cook		if ($line =~ /^\+\s*\#\s*if.*\bCONFIG_EXPERIMENTAL\b/) {
20961ba8dfd1SKees Cook			WARN("CONFIG_EXPERIMENTAL",
20971ba8dfd1SKees Cook			     "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
20981ba8dfd1SKees Cook		}
20991ba8dfd1SKees Cook
2100c2fdda0dSAndy Whitcroft# check for RCS/CVS revision markers
2101cf655043SAndy Whitcroft		if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
2102000d1cc1SJoe Perches			WARN("CVS_KEYWORD",
2103000d1cc1SJoe Perches			     "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
2104c2fdda0dSAndy Whitcroft		}
210522f2a2efSAndy Whitcroft
210642e41c54SMike Frysinger# Blackfin: don't use __builtin_bfin_[cs]sync
210742e41c54SMike Frysinger		if ($line =~ /__builtin_bfin_csync/) {
210842e41c54SMike Frysinger			my $herevet = "$here\n" . cat_vet($line) . "\n";
2109000d1cc1SJoe Perches			ERROR("CSYNC",
2110000d1cc1SJoe Perches			      "use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
211142e41c54SMike Frysinger		}
211242e41c54SMike Frysinger		if ($line =~ /__builtin_bfin_ssync/) {
211342e41c54SMike Frysinger			my $herevet = "$here\n" . cat_vet($line) . "\n";
2114000d1cc1SJoe Perches			ERROR("SSYNC",
2115000d1cc1SJoe Perches			      "use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
211642e41c54SMike Frysinger		}
211742e41c54SMike Frysinger
211856e77d70SJoe Perches# check for old HOTPLUG __dev<foo> section markings
211956e77d70SJoe Perches		if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
212056e77d70SJoe Perches			WARN("HOTPLUG_SECTION",
212156e77d70SJoe Perches			     "Using $1 is unnecessary\n" . $herecurr);
212256e77d70SJoe Perches		}
212356e77d70SJoe Perches
21249c0ca6f9SAndy Whitcroft# Check for potential 'bare' types
21252b474a1aSAndy Whitcroft		my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
21262b474a1aSAndy Whitcroft		    $realline_next);
21273e469cdcSAndy Whitcroft#print "LINE<$line>\n";
21283e469cdcSAndy Whitcroft		if ($linenr >= $suppress_statement &&
21293e469cdcSAndy Whitcroft		    $realcnt && $line =~ /.\s*\S/) {
2130170d3a22SAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
2131f5fe35ddSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0);
2132171ae1a4SAndy Whitcroft			$stat =~ s/\n./\n /g;
2133171ae1a4SAndy Whitcroft			$cond =~ s/\n./\n /g;
2134171ae1a4SAndy Whitcroft
21353e469cdcSAndy Whitcroft#print "linenr<$linenr> <$stat>\n";
21363e469cdcSAndy Whitcroft			# If this statement has no statement boundaries within
21373e469cdcSAndy Whitcroft			# it there is no point in retrying a statement scan
21383e469cdcSAndy Whitcroft			# until we hit end of it.
21393e469cdcSAndy Whitcroft			my $frag = $stat; $frag =~ s/;+\s*$//;
21403e469cdcSAndy Whitcroft			if ($frag !~ /(?:{|;)/) {
21413e469cdcSAndy Whitcroft#print "skip<$line_nr_next>\n";
21423e469cdcSAndy Whitcroft				$suppress_statement = $line_nr_next;
21433e469cdcSAndy Whitcroft			}
2144f74bd194SAndy Whitcroft
21452b474a1aSAndy Whitcroft			# Find the real next line.
21462b474a1aSAndy Whitcroft			$realline_next = $line_nr_next;
21472b474a1aSAndy Whitcroft			if (defined $realline_next &&
21482b474a1aSAndy Whitcroft			    (!defined $lines[$realline_next - 1] ||
21492b474a1aSAndy Whitcroft			     substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
21502b474a1aSAndy Whitcroft				$realline_next++;
21512b474a1aSAndy Whitcroft			}
21522b474a1aSAndy Whitcroft
2153171ae1a4SAndy Whitcroft			my $s = $stat;
2154171ae1a4SAndy Whitcroft			$s =~ s/{.*$//s;
2155cf655043SAndy Whitcroft
2156c2fdda0dSAndy Whitcroft			# Ignore goto labels.
2157171ae1a4SAndy Whitcroft			if ($s =~ /$Ident:\*$/s) {
2158c2fdda0dSAndy Whitcroft
2159c2fdda0dSAndy Whitcroft			# Ignore functions being called
2160171ae1a4SAndy Whitcroft			} elsif ($s =~ /^.\s*$Ident\s*\(/s) {
2161c2fdda0dSAndy Whitcroft
2162463f2864SAndy Whitcroft			} elsif ($s =~ /^.\s*else\b/s) {
2163463f2864SAndy Whitcroft
2164c45dcabdSAndy Whitcroft			# declarations always start with types
2165d2506586SAndy 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) {
2166c45dcabdSAndy Whitcroft				my $type = $1;
2167c45dcabdSAndy Whitcroft				$type =~ s/\s+/ /g;
2168c45dcabdSAndy Whitcroft				possible($type, "A:" . $s);
2169c45dcabdSAndy Whitcroft
21706c72ffaaSAndy Whitcroft			# definitions in global scope can only start with types
2171a6a84062SAndy Whitcroft			} elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
2172c45dcabdSAndy Whitcroft				possible($1, "B:" . $s);
2173c2fdda0dSAndy Whitcroft			}
21748905a67cSAndy Whitcroft
21756c72ffaaSAndy Whitcroft			# any (foo ... *) is a pointer cast, and foo is a type
217665863862SAndy Whitcroft			while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
2177c45dcabdSAndy Whitcroft				possible($1, "C:" . $s);
21789c0ca6f9SAndy Whitcroft			}
21798905a67cSAndy Whitcroft
21808905a67cSAndy Whitcroft			# Check for any sort of function declaration.
21818905a67cSAndy Whitcroft			# int foo(something bar, other baz);
21828905a67cSAndy Whitcroft			# void (*store_gdt)(x86_descr_ptr *);
2183171ae1a4SAndy Whitcroft			if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
21848905a67cSAndy Whitcroft				my ($name_len) = length($1);
21858905a67cSAndy Whitcroft
2186cf655043SAndy Whitcroft				my $ctx = $s;
2187773647a0SAndy Whitcroft				substr($ctx, 0, $name_len + 1, '');
21888905a67cSAndy Whitcroft				$ctx =~ s/\)[^\)]*$//;
2189cf655043SAndy Whitcroft
21908905a67cSAndy Whitcroft				for my $arg (split(/\s*,\s*/, $ctx)) {
2191c45dcabdSAndy Whitcroft					if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
21928905a67cSAndy Whitcroft
2193c45dcabdSAndy Whitcroft						possible($1, "D:" . $s);
21948905a67cSAndy Whitcroft					}
21958905a67cSAndy Whitcroft				}
21968905a67cSAndy Whitcroft			}
21978905a67cSAndy Whitcroft
21989c0ca6f9SAndy Whitcroft		}
21999c0ca6f9SAndy Whitcroft
220000df344fSAndy Whitcroft#
220100df344fSAndy Whitcroft# Checks which may be anchored in the context.
220200df344fSAndy Whitcroft#
220300df344fSAndy Whitcroft
220400df344fSAndy Whitcroft# Check for switch () and associated case and default
220500df344fSAndy Whitcroft# statements should be at the same indent.
220600df344fSAndy Whitcroft		if ($line=~/\bswitch\s*\(.*\)/) {
220700df344fSAndy Whitcroft			my $err = '';
220800df344fSAndy Whitcroft			my $sep = '';
220900df344fSAndy Whitcroft			my @ctx = ctx_block_outer($linenr, $realcnt);
221000df344fSAndy Whitcroft			shift(@ctx);
221100df344fSAndy Whitcroft			for my $ctx (@ctx) {
221200df344fSAndy Whitcroft				my ($clen, $cindent) = line_stats($ctx);
221300df344fSAndy Whitcroft				if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
221400df344fSAndy Whitcroft							$indent != $cindent) {
221500df344fSAndy Whitcroft					$err .= "$sep$ctx\n";
221600df344fSAndy Whitcroft					$sep = '';
221700df344fSAndy Whitcroft				} else {
221800df344fSAndy Whitcroft					$sep = "[...]\n";
221900df344fSAndy Whitcroft				}
222000df344fSAndy Whitcroft			}
222100df344fSAndy Whitcroft			if ($err ne '') {
2222000d1cc1SJoe Perches				ERROR("SWITCH_CASE_INDENT_LEVEL",
2223000d1cc1SJoe Perches				      "switch and case should be at the same indent\n$hereline$err");
2224de7d4f0eSAndy Whitcroft			}
2225de7d4f0eSAndy Whitcroft		}
2226de7d4f0eSAndy Whitcroft
2227de7d4f0eSAndy Whitcroft# if/while/etc brace do not go on next line, unless defining a do while loop,
2228de7d4f0eSAndy Whitcroft# or if that brace on the next line is for something else
2229c45dcabdSAndy Whitcroft		if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
2230773647a0SAndy Whitcroft			my $pre_ctx = "$1$2";
2231773647a0SAndy Whitcroft
22329c0ca6f9SAndy Whitcroft			my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
22338eef05ddSJoe Perches
22348eef05ddSJoe Perches			if ($line =~ /^\+\t{6,}/) {
22358eef05ddSJoe Perches				WARN("DEEP_INDENTATION",
22368eef05ddSJoe Perches				     "Too many leading tabs - consider code refactoring\n" . $herecurr);
22378eef05ddSJoe Perches			}
22388eef05ddSJoe Perches
2239de7d4f0eSAndy Whitcroft			my $ctx_cnt = $realcnt - $#ctx - 1;
2240de7d4f0eSAndy Whitcroft			my $ctx = join("\n", @ctx);
2241de7d4f0eSAndy Whitcroft
2242548596d5SAndy Whitcroft			my $ctx_ln = $linenr;
2243548596d5SAndy Whitcroft			my $ctx_skip = $realcnt;
2244de7d4f0eSAndy Whitcroft
2245548596d5SAndy Whitcroft			while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
2246548596d5SAndy Whitcroft					defined $lines[$ctx_ln - 1] &&
2247548596d5SAndy Whitcroft					$lines[$ctx_ln - 1] =~ /^-/)) {
2248548596d5SAndy Whitcroft				##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
2249548596d5SAndy Whitcroft				$ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
2250773647a0SAndy Whitcroft				$ctx_ln++;
2251773647a0SAndy Whitcroft			}
2252548596d5SAndy Whitcroft
225353210168SAndy Whitcroft			#print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
225453210168SAndy Whitcroft			#print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
2255773647a0SAndy Whitcroft
2256773647a0SAndy Whitcroft			if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln -1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
2257000d1cc1SJoe Perches				ERROR("OPEN_BRACE",
2258000d1cc1SJoe Perches				      "that open brace { should be on the previous line\n" .
225901464f30SAndy Whitcroft					"$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
226000df344fSAndy Whitcroft			}
2261773647a0SAndy Whitcroft			if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
2262773647a0SAndy Whitcroft			    $ctx =~ /\)\s*\;\s*$/ &&
2263773647a0SAndy Whitcroft			    defined $lines[$ctx_ln - 1])
2264773647a0SAndy Whitcroft			{
22659c0ca6f9SAndy Whitcroft				my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
22669c0ca6f9SAndy Whitcroft				if ($nindent > $indent) {
2267000d1cc1SJoe Perches					WARN("TRAILING_SEMICOLON",
2268000d1cc1SJoe Perches					     "trailing semicolon indicates no statements, indent implies otherwise\n" .
226901464f30SAndy Whitcroft						"$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
22709c0ca6f9SAndy Whitcroft				}
22719c0ca6f9SAndy Whitcroft			}
227200df344fSAndy Whitcroft		}
227300df344fSAndy Whitcroft
22744d001e4dSAndy Whitcroft# Check relative indent for conditionals and blocks.
22754d001e4dSAndy Whitcroft		if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
22763e469cdcSAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
22773e469cdcSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0)
22783e469cdcSAndy Whitcroft					if (!defined $stat);
22794d001e4dSAndy Whitcroft			my ($s, $c) = ($stat, $cond);
22804d001e4dSAndy Whitcroft
22814d001e4dSAndy Whitcroft			substr($s, 0, length($c), '');
22824d001e4dSAndy Whitcroft
22834d001e4dSAndy Whitcroft			# Make sure we remove the line prefixes as we have
22844d001e4dSAndy Whitcroft			# none on the first line, and are going to readd them
22854d001e4dSAndy Whitcroft			# where necessary.
22864d001e4dSAndy Whitcroft			$s =~ s/\n./\n/gs;
22874d001e4dSAndy Whitcroft
22884d001e4dSAndy Whitcroft			# Find out how long the conditional actually is.
22896f779c18SAndy Whitcroft			my @newlines = ($c =~ /\n/gs);
22906f779c18SAndy Whitcroft			my $cond_lines = 1 + $#newlines;
22914d001e4dSAndy Whitcroft
22924d001e4dSAndy Whitcroft			# We want to check the first line inside the block
22934d001e4dSAndy Whitcroft			# starting at the end of the conditional, so remove:
22944d001e4dSAndy Whitcroft			#  1) any blank line termination
22954d001e4dSAndy Whitcroft			#  2) any opening brace { on end of the line
22964d001e4dSAndy Whitcroft			#  3) any do (...) {
22974d001e4dSAndy Whitcroft			my $continuation = 0;
22984d001e4dSAndy Whitcroft			my $check = 0;
22994d001e4dSAndy Whitcroft			$s =~ s/^.*\bdo\b//;
23004d001e4dSAndy Whitcroft			$s =~ s/^\s*{//;
23014d001e4dSAndy Whitcroft			if ($s =~ s/^\s*\\//) {
23024d001e4dSAndy Whitcroft				$continuation = 1;
23034d001e4dSAndy Whitcroft			}
23049bd49efeSAndy Whitcroft			if ($s =~ s/^\s*?\n//) {
23054d001e4dSAndy Whitcroft				$check = 1;
23064d001e4dSAndy Whitcroft				$cond_lines++;
23074d001e4dSAndy Whitcroft			}
23084d001e4dSAndy Whitcroft
23094d001e4dSAndy Whitcroft			# Also ignore a loop construct at the end of a
23104d001e4dSAndy Whitcroft			# preprocessor statement.
23114d001e4dSAndy Whitcroft			if (($prevline =~ /^.\s*#\s*define\s/ ||
23124d001e4dSAndy Whitcroft			    $prevline =~ /\\\s*$/) && $continuation == 0) {
23134d001e4dSAndy Whitcroft				$check = 0;
23144d001e4dSAndy Whitcroft			}
23154d001e4dSAndy Whitcroft
23169bd49efeSAndy Whitcroft			my $cond_ptr = -1;
2317740504c6SAndy Whitcroft			$continuation = 0;
23189bd49efeSAndy Whitcroft			while ($cond_ptr != $cond_lines) {
23199bd49efeSAndy Whitcroft				$cond_ptr = $cond_lines;
23204d001e4dSAndy Whitcroft
2321f16fa28fSAndy Whitcroft				# If we see an #else/#elif then the code
2322f16fa28fSAndy Whitcroft				# is not linear.
2323f16fa28fSAndy Whitcroft				if ($s =~ /^\s*\#\s*(?:else|elif)/) {
2324f16fa28fSAndy Whitcroft					$check = 0;
2325f16fa28fSAndy Whitcroft				}
2326f16fa28fSAndy Whitcroft
23279bd49efeSAndy Whitcroft				# Ignore:
23289bd49efeSAndy Whitcroft				#  1) blank lines, they should be at 0,
23299bd49efeSAndy Whitcroft				#  2) preprocessor lines, and
23309bd49efeSAndy Whitcroft				#  3) labels.
2331740504c6SAndy Whitcroft				if ($continuation ||
2332740504c6SAndy Whitcroft				    $s =~ /^\s*?\n/ ||
23339bd49efeSAndy Whitcroft				    $s =~ /^\s*#\s*?/ ||
23349bd49efeSAndy Whitcroft				    $s =~ /^\s*$Ident\s*:/) {
2335740504c6SAndy Whitcroft					$continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
233630dad6ebSAndy Whitcroft					if ($s =~ s/^.*?\n//) {
23379bd49efeSAndy Whitcroft						$cond_lines++;
23389bd49efeSAndy Whitcroft					}
23394d001e4dSAndy Whitcroft				}
234030dad6ebSAndy Whitcroft			}
23414d001e4dSAndy Whitcroft
23424d001e4dSAndy Whitcroft			my (undef, $sindent) = line_stats("+" . $s);
23434d001e4dSAndy Whitcroft			my $stat_real = raw_line($linenr, $cond_lines);
23444d001e4dSAndy Whitcroft
23454d001e4dSAndy Whitcroft			# Check if either of these lines are modified, else
23464d001e4dSAndy Whitcroft			# this is not this patch's fault.
23474d001e4dSAndy Whitcroft			if (!defined($stat_real) ||
23484d001e4dSAndy Whitcroft			    $stat !~ /^\+/ && $stat_real !~ /^\+/) {
23494d001e4dSAndy Whitcroft				$check = 0;
23504d001e4dSAndy Whitcroft			}
23514d001e4dSAndy Whitcroft			if (defined($stat_real) && $cond_lines > 1) {
23524d001e4dSAndy Whitcroft				$stat_real = "[...]\n$stat_real";
23534d001e4dSAndy Whitcroft			}
23544d001e4dSAndy Whitcroft
23559bd49efeSAndy 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";
23564d001e4dSAndy Whitcroft
23574d001e4dSAndy Whitcroft			if ($check && (($sindent % 8) != 0 ||
23584d001e4dSAndy Whitcroft			    ($sindent <= $indent && $s ne ''))) {
2359000d1cc1SJoe Perches				WARN("SUSPECT_CODE_INDENT",
2360000d1cc1SJoe Perches				     "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
23614d001e4dSAndy Whitcroft			}
23624d001e4dSAndy Whitcroft		}
23634d001e4dSAndy Whitcroft
23646c72ffaaSAndy Whitcroft		# Track the 'values' across context and added lines.
23656c72ffaaSAndy Whitcroft		my $opline = $line; $opline =~ s/^./ /;
23661f65f947SAndy Whitcroft		my ($curr_values, $curr_vars) =
23671f65f947SAndy Whitcroft				annotate_values($opline . "\n", $prev_values);
23686c72ffaaSAndy Whitcroft		$curr_values = $prev_values . $curr_values;
2369c2fdda0dSAndy Whitcroft		if ($dbg_values) {
2370c2fdda0dSAndy Whitcroft			my $outline = $opline; $outline =~ s/\t/ /g;
2371cf655043SAndy Whitcroft			print "$linenr > .$outline\n";
2372cf655043SAndy Whitcroft			print "$linenr > $curr_values\n";
23731f65f947SAndy Whitcroft			print "$linenr >  $curr_vars\n";
2374c2fdda0dSAndy Whitcroft		}
23756c72ffaaSAndy Whitcroft		$prev_values = substr($curr_values, -1);
23766c72ffaaSAndy Whitcroft
237700df344fSAndy Whitcroft#ignore lines not being added
23783705ce5bSJoe Perches		next if ($line =~ /^[^\+]/);
237900df344fSAndy Whitcroft
2380653d4876SAndy Whitcroft# TEST: allow direct testing of the type matcher.
23817429c690SAndy Whitcroft		if ($dbg_type) {
23827429c690SAndy Whitcroft			if ($line =~ /^.\s*$Declare\s*$/) {
2383000d1cc1SJoe Perches				ERROR("TEST_TYPE",
2384000d1cc1SJoe Perches				      "TEST: is type\n" . $herecurr);
23857429c690SAndy Whitcroft			} elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
2386000d1cc1SJoe Perches				ERROR("TEST_NOT_TYPE",
2387000d1cc1SJoe Perches				      "TEST: is not type ($1 is)\n". $herecurr);
23887429c690SAndy Whitcroft			}
2389653d4876SAndy Whitcroft			next;
2390653d4876SAndy Whitcroft		}
2391a1ef277eSAndy Whitcroft# TEST: allow direct testing of the attribute matcher.
2392a1ef277eSAndy Whitcroft		if ($dbg_attr) {
23939360b0e5SAndy Whitcroft			if ($line =~ /^.\s*$Modifier\s*$/) {
2394000d1cc1SJoe Perches				ERROR("TEST_ATTR",
2395000d1cc1SJoe Perches				      "TEST: is attr\n" . $herecurr);
23969360b0e5SAndy Whitcroft			} elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
2397000d1cc1SJoe Perches				ERROR("TEST_NOT_ATTR",
2398000d1cc1SJoe Perches				      "TEST: is not attr ($1 is)\n". $herecurr);
2399a1ef277eSAndy Whitcroft			}
2400a1ef277eSAndy Whitcroft			next;
2401a1ef277eSAndy Whitcroft		}
2402653d4876SAndy Whitcroft
2403f0a594c1SAndy Whitcroft# check for initialisation to aggregates open brace on the next line
240499423c20SAndy Whitcroft		if ($line =~ /^.\s*{/ &&
240599423c20SAndy Whitcroft		    $prevline =~ /(?:^|[^=])=\s*$/) {
2406000d1cc1SJoe Perches			ERROR("OPEN_BRACE",
2407000d1cc1SJoe Perches			      "that open brace { should be on the previous line\n" . $hereprev);
2408f0a594c1SAndy Whitcroft		}
2409f0a594c1SAndy Whitcroft
241000df344fSAndy Whitcroft#
241100df344fSAndy Whitcroft# Checks which are anchored on the added line.
241200df344fSAndy Whitcroft#
241300df344fSAndy Whitcroft
2414653d4876SAndy Whitcroft# check for malformed paths in #include statements (uses RAW line)
2415c45dcabdSAndy Whitcroft		if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
2416653d4876SAndy Whitcroft			my $path = $1;
2417653d4876SAndy Whitcroft			if ($path =~ m{//}) {
2418000d1cc1SJoe Perches				ERROR("MALFORMED_INCLUDE",
2419495e9d84SJoe Perches				      "malformed #include filename\n" . $herecurr);
2420495e9d84SJoe Perches			}
2421495e9d84SJoe Perches			if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
2422495e9d84SJoe Perches				ERROR("UAPI_INCLUDE",
2423495e9d84SJoe Perches				      "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
2424653d4876SAndy Whitcroft			}
2425653d4876SAndy Whitcroft		}
2426653d4876SAndy Whitcroft
242700df344fSAndy Whitcroft# no C99 // comments
242800df344fSAndy Whitcroft		if ($line =~ m{//}) {
24293705ce5bSJoe Perches			if (ERROR("C99_COMMENTS",
24303705ce5bSJoe Perches				  "do not use C99 // comments\n" . $herecurr) &&
24313705ce5bSJoe Perches			    $fix) {
24323705ce5bSJoe Perches				my $line = $fixed[$linenr - 1];
24333705ce5bSJoe Perches				if ($line =~ /\/\/(.*)$/) {
24343705ce5bSJoe Perches					my $comment = trim($1);
24353705ce5bSJoe Perches					$fixed[$linenr - 1] =~ s@\/\/(.*)$@/\* $comment \*/@;
24363705ce5bSJoe Perches				}
24373705ce5bSJoe Perches			}
243800df344fSAndy Whitcroft		}
243900df344fSAndy Whitcroft		# Remove C99 comments.
24400a920b5bSAndy Whitcroft		$line =~ s@//.*@@;
24416c72ffaaSAndy Whitcroft		$opline =~ s@//.*@@;
24420a920b5bSAndy Whitcroft
24432b474a1aSAndy Whitcroft# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
24442b474a1aSAndy Whitcroft# the whole statement.
24452b474a1aSAndy Whitcroft#print "APW <$lines[$realline_next - 1]>\n";
24462b474a1aSAndy Whitcroft		if (defined $realline_next &&
24472b474a1aSAndy Whitcroft		    exists $lines[$realline_next - 1] &&
24482b474a1aSAndy Whitcroft		    !defined $suppress_export{$realline_next} &&
24492b474a1aSAndy Whitcroft		    ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
24502b474a1aSAndy Whitcroft		     $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
24513cbf62dfSAndy Whitcroft			# Handle definitions which produce identifiers with
24523cbf62dfSAndy Whitcroft			# a prefix:
24533cbf62dfSAndy Whitcroft			#   XXX(foo);
24543cbf62dfSAndy Whitcroft			#   EXPORT_SYMBOL(something_foo);
2455653d4876SAndy Whitcroft			my $name = $1;
245687a53877SAndy Whitcroft			if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
24573cbf62dfSAndy Whitcroft			    $name =~ /^${Ident}_$2/) {
24583cbf62dfSAndy Whitcroft#print "FOO C name<$name>\n";
24593cbf62dfSAndy Whitcroft				$suppress_export{$realline_next} = 1;
24603cbf62dfSAndy Whitcroft
24613cbf62dfSAndy Whitcroft			} elsif ($stat !~ /(?:
24622b474a1aSAndy Whitcroft				\n.}\s*$|
246348012058SAndy Whitcroft				^.DEFINE_$Ident\(\Q$name\E\)|
246448012058SAndy Whitcroft				^.DECLARE_$Ident\(\Q$name\E\)|
246548012058SAndy Whitcroft				^.LIST_HEAD\(\Q$name\E\)|
24662b474a1aSAndy Whitcroft				^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
24672b474a1aSAndy Whitcroft				\b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
246848012058SAndy Whitcroft			    )/x) {
24692b474a1aSAndy Whitcroft#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
24702b474a1aSAndy Whitcroft				$suppress_export{$realline_next} = 2;
24712b474a1aSAndy Whitcroft			} else {
24722b474a1aSAndy Whitcroft				$suppress_export{$realline_next} = 1;
24730a920b5bSAndy Whitcroft			}
24740a920b5bSAndy Whitcroft		}
24752b474a1aSAndy Whitcroft		if (!defined $suppress_export{$linenr} &&
24762b474a1aSAndy Whitcroft		    $prevline =~ /^.\s*$/ &&
24772b474a1aSAndy Whitcroft		    ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
24782b474a1aSAndy Whitcroft		     $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
24792b474a1aSAndy Whitcroft#print "FOO B <$lines[$linenr - 1]>\n";
24802b474a1aSAndy Whitcroft			$suppress_export{$linenr} = 2;
24812b474a1aSAndy Whitcroft		}
24822b474a1aSAndy Whitcroft		if (defined $suppress_export{$linenr} &&
24832b474a1aSAndy Whitcroft		    $suppress_export{$linenr} == 2) {
2484000d1cc1SJoe Perches			WARN("EXPORT_SYMBOL",
2485000d1cc1SJoe Perches			     "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
24862b474a1aSAndy Whitcroft		}
24870a920b5bSAndy Whitcroft
24885150bda4SJoe Eloff# check for global initialisers.
2489c45dcabdSAndy Whitcroft		if ($line =~ /^.$Type\s*$Ident\s*(?:\s+$Modifier)*\s*=\s*(0|NULL|false)\s*;/) {
2490000d1cc1SJoe Perches			ERROR("GLOBAL_INITIALISERS",
2491000d1cc1SJoe Perches			      "do not initialise globals to 0 or NULL\n" .
2492f0a594c1SAndy Whitcroft				$herecurr);
2493f0a594c1SAndy Whitcroft		}
24940a920b5bSAndy Whitcroft# check for static initialisers.
24952d1bafd7SAndy Whitcroft		if ($line =~ /\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
2496000d1cc1SJoe Perches			ERROR("INITIALISED_STATIC",
2497000d1cc1SJoe Perches			      "do not initialise statics to 0 or NULL\n" .
2498de7d4f0eSAndy Whitcroft				$herecurr);
24990a920b5bSAndy Whitcroft		}
25000a920b5bSAndy Whitcroft
2501cb710ecaSJoe Perches# check for static const char * arrays.
2502cb710ecaSJoe Perches		if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
2503000d1cc1SJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
2504000d1cc1SJoe Perches			     "static const char * array should probably be static const char * const\n" .
2505cb710ecaSJoe Perches				$herecurr);
2506cb710ecaSJoe Perches               }
2507cb710ecaSJoe Perches
2508cb710ecaSJoe Perches# check for static char foo[] = "bar" declarations.
2509cb710ecaSJoe Perches		if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
2510000d1cc1SJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
2511000d1cc1SJoe Perches			     "static char array declaration should probably be static const char\n" .
2512cb710ecaSJoe Perches				$herecurr);
2513cb710ecaSJoe Perches               }
2514cb710ecaSJoe Perches
251593ed0e2dSJoe Perches# check for declarations of struct pci_device_id
251693ed0e2dSJoe Perches		if ($line =~ /\bstruct\s+pci_device_id\s+\w+\s*\[\s*\]\s*\=\s*\{/) {
2517000d1cc1SJoe Perches			WARN("DEFINE_PCI_DEVICE_TABLE",
2518000d1cc1SJoe Perches			     "Use DEFINE_PCI_DEVICE_TABLE for struct pci_device_id\n" . $herecurr);
251993ed0e2dSJoe Perches		}
252093ed0e2dSJoe Perches
2521653d4876SAndy Whitcroft# check for new typedefs, only function parameters and sparse annotations
2522653d4876SAndy Whitcroft# make sense.
2523653d4876SAndy Whitcroft		if ($line =~ /\btypedef\s/ &&
25248054576dSAndy Whitcroft		    $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
2525c45dcabdSAndy Whitcroft		    $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
25268ed22cadSAndy Whitcroft		    $line !~ /\b$typeTypedefs\b/ &&
2527653d4876SAndy Whitcroft		    $line !~ /\b__bitwise(?:__|)\b/) {
2528000d1cc1SJoe Perches			WARN("NEW_TYPEDEFS",
2529000d1cc1SJoe Perches			     "do not add new typedefs\n" . $herecurr);
25300a920b5bSAndy Whitcroft		}
25310a920b5bSAndy Whitcroft
25320a920b5bSAndy Whitcroft# * goes on variable not on type
253365863862SAndy Whitcroft		# (char*[ const])
2534bfcb2cc7SAndy Whitcroft		while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
2535bfcb2cc7SAndy Whitcroft			#print "AA<$1>\n";
25363705ce5bSJoe Perches			my ($ident, $from, $to) = ($1, $2, $2);
2537d8aaf121SAndy Whitcroft
253865863862SAndy Whitcroft			# Should start with a space.
253965863862SAndy Whitcroft			$to =~ s/^(\S)/ $1/;
254065863862SAndy Whitcroft			# Should not end with a space.
254165863862SAndy Whitcroft			$to =~ s/\s+$//;
254265863862SAndy Whitcroft			# '*'s should not have spaces between.
2543f9a0b3d1SAndy Whitcroft			while ($to =~ s/\*\s+\*/\*\*/) {
254465863862SAndy Whitcroft			}
2545d8aaf121SAndy Whitcroft
25463705ce5bSJoe Perches##			print "1: from<$from> to<$to> ident<$ident>\n";
254765863862SAndy Whitcroft			if ($from ne $to) {
25483705ce5bSJoe Perches				if (ERROR("POINTER_LOCATION",
25493705ce5bSJoe Perches					  "\"(foo$from)\" should be \"(foo$to)\"\n" .  $herecurr) &&
25503705ce5bSJoe Perches				    $fix) {
25513705ce5bSJoe Perches					my $sub_from = $ident;
25523705ce5bSJoe Perches					my $sub_to = $ident;
25533705ce5bSJoe Perches					$sub_to =~ s/\Q$from\E/$to/;
25543705ce5bSJoe Perches					$fixed[$linenr - 1] =~
25553705ce5bSJoe Perches					    s@\Q$sub_from\E@$sub_to@;
25563705ce5bSJoe Perches				}
255765863862SAndy Whitcroft			}
2558bfcb2cc7SAndy Whitcroft		}
2559bfcb2cc7SAndy Whitcroft		while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
2560bfcb2cc7SAndy Whitcroft			#print "BB<$1>\n";
25613705ce5bSJoe Perches			my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
2562d8aaf121SAndy Whitcroft
256365863862SAndy Whitcroft			# Should start with a space.
256465863862SAndy Whitcroft			$to =~ s/^(\S)/ $1/;
256565863862SAndy Whitcroft			# Should not end with a space.
256665863862SAndy Whitcroft			$to =~ s/\s+$//;
256765863862SAndy Whitcroft			# '*'s should not have spaces between.
2568f9a0b3d1SAndy Whitcroft			while ($to =~ s/\*\s+\*/\*\*/) {
256965863862SAndy Whitcroft			}
257065863862SAndy Whitcroft			# Modifiers should have spaces.
257165863862SAndy Whitcroft			$to =~ s/(\b$Modifier$)/$1 /;
257265863862SAndy Whitcroft
25733705ce5bSJoe Perches##			print "2: from<$from> to<$to> ident<$ident>\n";
2574667026e7SAndy Whitcroft			if ($from ne $to && $ident !~ /^$Modifier$/) {
25753705ce5bSJoe Perches				if (ERROR("POINTER_LOCATION",
25763705ce5bSJoe Perches					  "\"foo${from}bar\" should be \"foo${to}bar\"\n" .  $herecurr) &&
25773705ce5bSJoe Perches				    $fix) {
25783705ce5bSJoe Perches
25793705ce5bSJoe Perches					my $sub_from = $match;
25803705ce5bSJoe Perches					my $sub_to = $match;
25813705ce5bSJoe Perches					$sub_to =~ s/\Q$from\E/$to/;
25823705ce5bSJoe Perches					$fixed[$linenr - 1] =~
25833705ce5bSJoe Perches					    s@\Q$sub_from\E@$sub_to@;
25843705ce5bSJoe Perches				}
258565863862SAndy Whitcroft			}
25860a920b5bSAndy Whitcroft		}
25870a920b5bSAndy Whitcroft
25880a920b5bSAndy Whitcroft# # no BUG() or BUG_ON()
25890a920b5bSAndy Whitcroft# 		if ($line =~ /\b(BUG|BUG_ON)\b/) {
25900a920b5bSAndy Whitcroft# 			print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
25910a920b5bSAndy Whitcroft# 			print "$herecurr";
25920a920b5bSAndy Whitcroft# 			$clean = 0;
25930a920b5bSAndy Whitcroft# 		}
25940a920b5bSAndy Whitcroft
25958905a67cSAndy Whitcroft		if ($line =~ /\bLINUX_VERSION_CODE\b/) {
2596000d1cc1SJoe Perches			WARN("LINUX_VERSION_CODE",
2597000d1cc1SJoe Perches			     "LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
25988905a67cSAndy Whitcroft		}
25998905a67cSAndy Whitcroft
260017441227SJoe Perches# check for uses of printk_ratelimit
260117441227SJoe Perches		if ($line =~ /\bprintk_ratelimit\s*\(/) {
2602000d1cc1SJoe Perches			WARN("PRINTK_RATELIMITED",
2603000d1cc1SJoe Perches"Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
260417441227SJoe Perches		}
260517441227SJoe Perches
260600df344fSAndy Whitcroft# printk should use KERN_* levels.  Note that follow on printk's on the
260700df344fSAndy Whitcroft# same line do not need a level, so we use the current block context
260800df344fSAndy Whitcroft# to try and find and validate the current printk.  In summary the current
260925985edcSLucas De Marchi# printk includes all preceding printk's which have no newline on the end.
261000df344fSAndy Whitcroft# we assume the first bad printk is the one to report.
2611f0a594c1SAndy Whitcroft		if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
261200df344fSAndy Whitcroft			my $ok = 0;
261300df344fSAndy Whitcroft			for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
261400df344fSAndy Whitcroft				#print "CHECK<$lines[$ln - 1]\n";
261525985edcSLucas De Marchi				# we have a preceding printk if it ends
261600df344fSAndy Whitcroft				# with "\n" ignore it, else it is to blame
261700df344fSAndy Whitcroft				if ($lines[$ln - 1] =~ m{\bprintk\(}) {
261800df344fSAndy Whitcroft					if ($rawlines[$ln - 1] !~ m{\\n"}) {
261900df344fSAndy Whitcroft						$ok = 1;
262000df344fSAndy Whitcroft					}
262100df344fSAndy Whitcroft					last;
262200df344fSAndy Whitcroft				}
262300df344fSAndy Whitcroft			}
262400df344fSAndy Whitcroft			if ($ok == 0) {
2625000d1cc1SJoe Perches				WARN("PRINTK_WITHOUT_KERN_LEVEL",
2626000d1cc1SJoe Perches				     "printk() should include KERN_ facility level\n" . $herecurr);
26270a920b5bSAndy Whitcroft			}
262800df344fSAndy Whitcroft		}
26290a920b5bSAndy Whitcroft
2630243f3803SJoe Perches		if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
2631243f3803SJoe Perches			my $orig = $1;
2632243f3803SJoe Perches			my $level = lc($orig);
2633243f3803SJoe Perches			$level = "warn" if ($level eq "warning");
26348f26b837SJoe Perches			my $level2 = $level;
26358f26b837SJoe Perches			$level2 = "dbg" if ($level eq "debug");
2636243f3803SJoe Perches			WARN("PREFER_PR_LEVEL",
26378f26b837SJoe Perches			     "Prefer netdev_$level2(netdev, ... then dev_$level2(dev, ... then pr_$level(...  to printk(KERN_$orig ...\n" . $herecurr);
2638243f3803SJoe Perches		}
2639243f3803SJoe Perches
2640243f3803SJoe Perches		if ($line =~ /\bpr_warning\s*\(/) {
2641243f3803SJoe Perches			WARN("PREFER_PR_LEVEL",
2642243f3803SJoe Perches			     "Prefer pr_warn(... to pr_warning(...\n" . $herecurr);
2643243f3803SJoe Perches		}
2644243f3803SJoe Perches
2645dc139313SJoe Perches		if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
2646dc139313SJoe Perches			my $orig = $1;
2647dc139313SJoe Perches			my $level = lc($orig);
2648dc139313SJoe Perches			$level = "warn" if ($level eq "warning");
2649dc139313SJoe Perches			$level = "dbg" if ($level eq "debug");
2650dc139313SJoe Perches			WARN("PREFER_DEV_LEVEL",
2651dc139313SJoe Perches			     "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
2652dc139313SJoe Perches		}
2653dc139313SJoe Perches
2654653d4876SAndy Whitcroft# function brace can't be on same line, except for #defines of do while,
2655653d4876SAndy Whitcroft# or if closed on same line
2656c45dcabdSAndy Whitcroft		if (($line=~/$Type\s*$Ident\(.*\).*\s{/) and
2657c45dcabdSAndy Whitcroft		    !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) {
2658000d1cc1SJoe Perches			ERROR("OPEN_BRACE",
2659000d1cc1SJoe Perches			      "open brace '{' following function declarations go on the next line\n" . $herecurr);
26600a920b5bSAndy Whitcroft		}
2661653d4876SAndy Whitcroft
26628905a67cSAndy Whitcroft# open braces for enum, union and struct go on the same line.
26638905a67cSAndy Whitcroft		if ($line =~ /^.\s*{/ &&
26648905a67cSAndy Whitcroft		    $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
2665000d1cc1SJoe Perches			ERROR("OPEN_BRACE",
2666000d1cc1SJoe Perches			      "open brace '{' following $1 go on the same line\n" . $hereprev);
26678905a67cSAndy Whitcroft		}
26688905a67cSAndy Whitcroft
26690c73b4ebSAndy Whitcroft# missing space after union, struct or enum definition
26703705ce5bSJoe Perches		if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
26713705ce5bSJoe Perches			if (WARN("SPACING",
26723705ce5bSJoe Perches				 "missing space after $1 definition\n" . $herecurr) &&
26733705ce5bSJoe Perches			    $fix) {
26743705ce5bSJoe Perches				$fixed[$linenr - 1] =~
26753705ce5bSJoe Perches				    s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
26763705ce5bSJoe Perches			}
26770c73b4ebSAndy Whitcroft		}
26780c73b4ebSAndy Whitcroft
26798d31cfceSAndy Whitcroft# check for spacing round square brackets; allowed:
26808d31cfceSAndy Whitcroft#  1. with a type on the left -- int [] a;
2681fe2a7dbcSAndy Whitcroft#  2. at the beginning of a line for slice initialisers -- [0...10] = 5,
2682fe2a7dbcSAndy Whitcroft#  3. inside a curly brace -- = { [0...10] = 5 }
26838d31cfceSAndy Whitcroft		while ($line =~ /(.*?\s)\[/g) {
26848d31cfceSAndy Whitcroft			my ($where, $prefix) = ($-[1], $1);
26858d31cfceSAndy Whitcroft			if ($prefix !~ /$Type\s+$/ &&
2686fe2a7dbcSAndy Whitcroft			    ($where != 0 || $prefix !~ /^.\s+$/) &&
2687daebc534SAndy Whitcroft			    $prefix !~ /[{,]\s+$/) {
26883705ce5bSJoe Perches				if (ERROR("BRACKET_SPACE",
26893705ce5bSJoe Perches					  "space prohibited before open square bracket '['\n" . $herecurr) &&
26903705ce5bSJoe Perches				    $fix) {
26913705ce5bSJoe Perches				    $fixed[$linenr - 1] =~
26923705ce5bSJoe Perches					s/^(\+.*?)\s+\[/$1\[/;
26933705ce5bSJoe Perches				}
26948d31cfceSAndy Whitcroft			}
26958d31cfceSAndy Whitcroft		}
26968d31cfceSAndy Whitcroft
2697f0a594c1SAndy Whitcroft# check for spaces between functions and their parentheses.
26986c72ffaaSAndy Whitcroft		while ($line =~ /($Ident)\s+\(/g) {
2699c2fdda0dSAndy Whitcroft			my $name = $1;
2700773647a0SAndy Whitcroft			my $ctx_before = substr($line, 0, $-[1]);
2701773647a0SAndy Whitcroft			my $ctx = "$ctx_before$name";
2702c2fdda0dSAndy Whitcroft
2703c2fdda0dSAndy Whitcroft			# Ignore those directives where spaces _are_ permitted.
2704773647a0SAndy Whitcroft			if ($name =~ /^(?:
2705773647a0SAndy Whitcroft				if|for|while|switch|return|case|
2706773647a0SAndy Whitcroft				volatile|__volatile__|
2707773647a0SAndy Whitcroft				__attribute__|format|__extension__|
2708773647a0SAndy Whitcroft				asm|__asm__)$/x)
2709773647a0SAndy Whitcroft			{
2710c2fdda0dSAndy Whitcroft			# cpp #define statements have non-optional spaces, ie
2711c2fdda0dSAndy Whitcroft			# if there is a space between the name and the open
2712c2fdda0dSAndy Whitcroft			# parenthesis it is simply not a parameter group.
2713c45dcabdSAndy Whitcroft			} elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
2714773647a0SAndy Whitcroft
2715773647a0SAndy Whitcroft			# cpp #elif statement condition may start with a (
2716c45dcabdSAndy Whitcroft			} elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
2717c2fdda0dSAndy Whitcroft
2718c2fdda0dSAndy Whitcroft			# If this whole things ends with a type its most
2719c2fdda0dSAndy Whitcroft			# likely a typedef for a function.
2720773647a0SAndy Whitcroft			} elsif ($ctx =~ /$Type$/) {
2721c2fdda0dSAndy Whitcroft
2722c2fdda0dSAndy Whitcroft			} else {
27233705ce5bSJoe Perches				if (WARN("SPACING",
27243705ce5bSJoe Perches					 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
27253705ce5bSJoe Perches					     $fix) {
27263705ce5bSJoe Perches					$fixed[$linenr - 1] =~
27273705ce5bSJoe Perches					    s/\b$name\s+\(/$name\(/;
27283705ce5bSJoe Perches				}
2729f0a594c1SAndy Whitcroft			}
27306c72ffaaSAndy Whitcroft		}
27319a4cad4eSEric Nelson
2732653d4876SAndy Whitcroft# Check operator spacing.
27330a920b5bSAndy Whitcroft		if (!($line=~/\#\s*include/)) {
27343705ce5bSJoe Perches			my $fixed_line = "";
27353705ce5bSJoe Perches			my $line_fixed = 0;
27363705ce5bSJoe Perches
27379c0ca6f9SAndy Whitcroft			my $ops = qr{
27389c0ca6f9SAndy Whitcroft				<<=|>>=|<=|>=|==|!=|
27399c0ca6f9SAndy Whitcroft				\+=|-=|\*=|\/=|%=|\^=|\|=|&=|
27409c0ca6f9SAndy Whitcroft				=>|->|<<|>>|<|>|=|!|~|
27411f65f947SAndy Whitcroft				&&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
27421f65f947SAndy Whitcroft				\?|:
27439c0ca6f9SAndy Whitcroft			}x;
2744cf655043SAndy Whitcroft			my @elements = split(/($ops|;)/, $opline);
27453705ce5bSJoe Perches
27463705ce5bSJoe Perches##			print("element count: <" . $#elements . ">\n");
27473705ce5bSJoe Perches##			foreach my $el (@elements) {
27483705ce5bSJoe Perches##				print("el: <$el>\n");
27493705ce5bSJoe Perches##			}
27503705ce5bSJoe Perches
27513705ce5bSJoe Perches			my @fix_elements = ();
275200df344fSAndy Whitcroft			my $off = 0;
27536c72ffaaSAndy Whitcroft
27543705ce5bSJoe Perches			foreach my $el (@elements) {
27553705ce5bSJoe Perches				push(@fix_elements, substr($rawline, $off, length($el)));
27563705ce5bSJoe Perches				$off += length($el);
27573705ce5bSJoe Perches			}
27583705ce5bSJoe Perches
27593705ce5bSJoe Perches			$off = 0;
27603705ce5bSJoe Perches
27616c72ffaaSAndy Whitcroft			my $blank = copy_spacing($opline);
27626c72ffaaSAndy Whitcroft
27630a920b5bSAndy Whitcroft			for (my $n = 0; $n < $#elements; $n += 2) {
27643705ce5bSJoe Perches
27653705ce5bSJoe Perches				my $good = $fix_elements[$n] . $fix_elements[$n + 1];
27663705ce5bSJoe Perches
27673705ce5bSJoe Perches##				print("n: <$n> good: <$good>\n");
27683705ce5bSJoe Perches
27694a0df2efSAndy Whitcroft				$off += length($elements[$n]);
27704a0df2efSAndy Whitcroft
277125985edcSLucas De Marchi				# Pick up the preceding and succeeding characters.
2772773647a0SAndy Whitcroft				my $ca = substr($opline, 0, $off);
2773773647a0SAndy Whitcroft				my $cc = '';
2774773647a0SAndy Whitcroft				if (length($opline) >= ($off + length($elements[$n + 1]))) {
2775773647a0SAndy Whitcroft					$cc = substr($opline, $off + length($elements[$n + 1]));
2776773647a0SAndy Whitcroft				}
2777773647a0SAndy Whitcroft				my $cb = "$ca$;$cc";
2778773647a0SAndy Whitcroft
27794a0df2efSAndy Whitcroft				my $a = '';
27804a0df2efSAndy Whitcroft				$a = 'V' if ($elements[$n] ne '');
27814a0df2efSAndy Whitcroft				$a = 'W' if ($elements[$n] =~ /\s$/);
2782cf655043SAndy Whitcroft				$a = 'C' if ($elements[$n] =~ /$;$/);
27834a0df2efSAndy Whitcroft				$a = 'B' if ($elements[$n] =~ /(\[|\()$/);
27844a0df2efSAndy Whitcroft				$a = 'O' if ($elements[$n] eq '');
2785773647a0SAndy Whitcroft				$a = 'E' if ($ca =~ /^\s*$/);
27864a0df2efSAndy Whitcroft
27870a920b5bSAndy Whitcroft				my $op = $elements[$n + 1];
27884a0df2efSAndy Whitcroft
27894a0df2efSAndy Whitcroft				my $c = '';
27900a920b5bSAndy Whitcroft				if (defined $elements[$n + 2]) {
27914a0df2efSAndy Whitcroft					$c = 'V' if ($elements[$n + 2] ne '');
27924a0df2efSAndy Whitcroft					$c = 'W' if ($elements[$n + 2] =~ /^\s/);
2793cf655043SAndy Whitcroft					$c = 'C' if ($elements[$n + 2] =~ /^$;/);
27944a0df2efSAndy Whitcroft					$c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
27954a0df2efSAndy Whitcroft					$c = 'O' if ($elements[$n + 2] eq '');
27968b1b3378SAndy Whitcroft					$c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
27974a0df2efSAndy Whitcroft				} else {
27984a0df2efSAndy Whitcroft					$c = 'E';
27990a920b5bSAndy Whitcroft				}
28000a920b5bSAndy Whitcroft
28014a0df2efSAndy Whitcroft				my $ctx = "${a}x${c}";
28024a0df2efSAndy Whitcroft
28034a0df2efSAndy Whitcroft				my $at = "(ctx:$ctx)";
28044a0df2efSAndy Whitcroft
28056c72ffaaSAndy Whitcroft				my $ptr = substr($blank, 0, $off) . "^";
2806de7d4f0eSAndy Whitcroft				my $hereptr = "$hereline$ptr\n";
28070a920b5bSAndy Whitcroft
280874048ed8SAndy Whitcroft				# Pull out the value of this operator.
28096c72ffaaSAndy Whitcroft				my $op_type = substr($curr_values, $off + 1, 1);
28100a920b5bSAndy Whitcroft
28111f65f947SAndy Whitcroft				# Get the full operator variant.
28121f65f947SAndy Whitcroft				my $opv = $op . substr($curr_vars, $off, 1);
28131f65f947SAndy Whitcroft
281413214adfSAndy Whitcroft				# Ignore operators passed as parameters.
281513214adfSAndy Whitcroft				if ($op_type ne 'V' &&
281613214adfSAndy Whitcroft				    $ca =~ /\s$/ && $cc =~ /^\s*,/) {
281713214adfSAndy Whitcroft
2818cf655043SAndy Whitcroft#				# Ignore comments
2819cf655043SAndy Whitcroft#				} elsif ($op =~ /^$;+$/) {
282013214adfSAndy Whitcroft
2821d8aaf121SAndy Whitcroft				# ; should have either the end of line or a space or \ after it
282213214adfSAndy Whitcroft				} elsif ($op eq ';') {
2823cf655043SAndy Whitcroft					if ($ctx !~ /.x[WEBC]/ &&
2824cf655043SAndy Whitcroft					    $cc !~ /^\\/ && $cc !~ /^;/) {
28253705ce5bSJoe Perches						if (ERROR("SPACING",
28263705ce5bSJoe Perches							  "space required after that '$op' $at\n" . $hereptr)) {
28273705ce5bSJoe Perches							$good = trim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
28283705ce5bSJoe Perches							$line_fixed = 1;
28293705ce5bSJoe Perches						}
2830d8aaf121SAndy Whitcroft					}
2831d8aaf121SAndy Whitcroft
2832d8aaf121SAndy Whitcroft				# // is a comment
2833d8aaf121SAndy Whitcroft				} elsif ($op eq '//') {
28340a920b5bSAndy Whitcroft
28351f65f947SAndy Whitcroft				# No spaces for:
28361f65f947SAndy Whitcroft				#   ->
28371f65f947SAndy Whitcroft				#   :   when part of a bitfield
28381f65f947SAndy Whitcroft				} elsif ($op eq '->' || $opv eq ':B') {
28394a0df2efSAndy Whitcroft					if ($ctx =~ /Wx.|.xW/) {
28403705ce5bSJoe Perches						if (ERROR("SPACING",
28413705ce5bSJoe Perches							  "spaces prohibited around that '$op' $at\n" . $hereptr)) {
28423705ce5bSJoe Perches							$good = trim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
28433705ce5bSJoe Perches							$line_fixed = 1;
28443705ce5bSJoe Perches							if (defined $fix_elements[$n + 2]) {
28453705ce5bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
28463705ce5bSJoe Perches							}
28473705ce5bSJoe Perches						}
28480a920b5bSAndy Whitcroft					}
28490a920b5bSAndy Whitcroft
28500a920b5bSAndy Whitcroft				# , must have a space on the right.
28510a920b5bSAndy Whitcroft				} elsif ($op eq ',') {
2852cf655043SAndy Whitcroft					if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
28533705ce5bSJoe Perches						if (ERROR("SPACING",
28543705ce5bSJoe Perches							  "space required after that '$op' $at\n" . $hereptr)) {
28553705ce5bSJoe Perches							$good = trim($fix_elements[$n]) . trim($fix_elements[$n + 1]) . " ";
28563705ce5bSJoe Perches							$line_fixed = 1;
28573705ce5bSJoe Perches						}
28580a920b5bSAndy Whitcroft					}
28590a920b5bSAndy Whitcroft
28609c0ca6f9SAndy Whitcroft				# '*' as part of a type definition -- reported already.
286174048ed8SAndy Whitcroft				} elsif ($opv eq '*_') {
28629c0ca6f9SAndy Whitcroft					#warn "'*' is part of type\n";
28639c0ca6f9SAndy Whitcroft
28649c0ca6f9SAndy Whitcroft				# unary operators should have a space before and
28659c0ca6f9SAndy Whitcroft				# none after.  May be left adjacent to another
28669c0ca6f9SAndy Whitcroft				# unary operator, or a cast
28679c0ca6f9SAndy Whitcroft				} elsif ($op eq '!' || $op eq '~' ||
286874048ed8SAndy Whitcroft					 $opv eq '*U' || $opv eq '-U' ||
28690d413866SAndy Whitcroft					 $opv eq '&U' || $opv eq '&&U') {
2870cf655043SAndy Whitcroft					if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
28713705ce5bSJoe Perches						if (ERROR("SPACING",
28723705ce5bSJoe Perches							  "space required before that '$op' $at\n" . $hereptr)) {
28733705ce5bSJoe Perches							$good = trim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]);
28743705ce5bSJoe Perches							$line_fixed = 1;
28753705ce5bSJoe Perches						}
28760a920b5bSAndy Whitcroft					}
2877a3340b35SAndy Whitcroft					if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
2878171ae1a4SAndy Whitcroft						# A unary '*' may be const
2879171ae1a4SAndy Whitcroft
2880171ae1a4SAndy Whitcroft					} elsif ($ctx =~ /.xW/) {
28813705ce5bSJoe Perches						if (ERROR("SPACING",
28823705ce5bSJoe Perches							  "space prohibited after that '$op' $at\n" . $hereptr)) {
28833705ce5bSJoe Perches							$fixed_line =~ s/\s+$//;
28843705ce5bSJoe Perches							$good = trim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
28853705ce5bSJoe Perches							$line_fixed = 1;
28863705ce5bSJoe Perches							if (defined $fix_elements[$n + 2]) {
28873705ce5bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
28883705ce5bSJoe Perches							}
28893705ce5bSJoe Perches						}
28900a920b5bSAndy Whitcroft					}
28910a920b5bSAndy Whitcroft
28920a920b5bSAndy Whitcroft				# unary ++ and unary -- are allowed no space on one side.
28930a920b5bSAndy Whitcroft				} elsif ($op eq '++' or $op eq '--') {
2894773647a0SAndy Whitcroft					if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
28953705ce5bSJoe Perches						if (ERROR("SPACING",
28963705ce5bSJoe Perches							  "space required one side of that '$op' $at\n" . $hereptr)) {
28973705ce5bSJoe Perches							$fixed_line =~ s/\s+$//;
28983705ce5bSJoe Perches							$good = trim($fix_elements[$n]) . trim($fix_elements[$n + 1]) . " ";
28993705ce5bSJoe Perches							$line_fixed = 1;
29003705ce5bSJoe Perches						}
29010a920b5bSAndy Whitcroft					}
2902773647a0SAndy Whitcroft					if ($ctx =~ /Wx[BE]/ ||
2903773647a0SAndy Whitcroft					    ($ctx =~ /Wx./ && $cc =~ /^;/)) {
29043705ce5bSJoe Perches						if (ERROR("SPACING",
29053705ce5bSJoe Perches							  "space prohibited before that '$op' $at\n" . $hereptr)) {
29063705ce5bSJoe Perches							$fixed_line =~ s/\s+$//;
29073705ce5bSJoe Perches							$good = trim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
29083705ce5bSJoe Perches							$line_fixed = 1;
29093705ce5bSJoe Perches						}
2910653d4876SAndy Whitcroft					}
2911773647a0SAndy Whitcroft					if ($ctx =~ /ExW/) {
29123705ce5bSJoe Perches						if (ERROR("SPACING",
29133705ce5bSJoe Perches							  "space prohibited after that '$op' $at\n" . $hereptr)) {
29143705ce5bSJoe Perches							$fixed_line =~ s/\s+$//;
29153705ce5bSJoe Perches							$good = trim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
29163705ce5bSJoe Perches							$line_fixed = 1;
29173705ce5bSJoe Perches							if (defined $fix_elements[$n + 2]) {
29183705ce5bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
2919773647a0SAndy Whitcroft							}
29203705ce5bSJoe Perches						}
29213705ce5bSJoe Perches					}
29220a920b5bSAndy Whitcroft
29230a920b5bSAndy Whitcroft				# << and >> may either have or not have spaces both sides
29249c0ca6f9SAndy Whitcroft				} elsif ($op eq '<<' or $op eq '>>' or
29259c0ca6f9SAndy Whitcroft					 $op eq '&' or $op eq '^' or $op eq '|' or
29269c0ca6f9SAndy Whitcroft					 $op eq '+' or $op eq '-' or
2927c2fdda0dSAndy Whitcroft					 $op eq '*' or $op eq '/' or
2928c2fdda0dSAndy Whitcroft					 $op eq '%')
29290a920b5bSAndy Whitcroft				{
2930773647a0SAndy Whitcroft					if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
29313705ce5bSJoe Perches						if (ERROR("SPACING",
29323705ce5bSJoe Perches							  "need consistent spacing around '$op' $at\n" . $hereptr)) {
29333705ce5bSJoe Perches							$fixed_line =~ s/\s+$//;
29343705ce5bSJoe Perches							$good = trim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
29353705ce5bSJoe Perches							$line_fixed = 1;
29363705ce5bSJoe Perches						}
29370a920b5bSAndy Whitcroft					}
29380a920b5bSAndy Whitcroft
29391f65f947SAndy Whitcroft				# A colon needs no spaces before when it is
29401f65f947SAndy Whitcroft				# terminating a case value or a label.
29411f65f947SAndy Whitcroft				} elsif ($opv eq ':C' || $opv eq ':L') {
29421f65f947SAndy Whitcroft					if ($ctx =~ /Wx./) {
29433705ce5bSJoe Perches						if (ERROR("SPACING",
29443705ce5bSJoe Perches							  "space prohibited before that '$op' $at\n" . $hereptr)) {
29453705ce5bSJoe Perches							$good = trim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
29463705ce5bSJoe Perches							$line_fixed = 1;
29473705ce5bSJoe Perches						}
29481f65f947SAndy Whitcroft					}
29491f65f947SAndy Whitcroft
29500a920b5bSAndy Whitcroft				# All the others need spaces both sides.
2951cf655043SAndy Whitcroft				} elsif ($ctx !~ /[EWC]x[CWE]/) {
29521f65f947SAndy Whitcroft					my $ok = 0;
29531f65f947SAndy Whitcroft
295422f2a2efSAndy Whitcroft					# Ignore email addresses <foo@bar>
29551f65f947SAndy Whitcroft					if (($op eq '<' &&
29561f65f947SAndy Whitcroft					     $cc =~ /^\S+\@\S+>/) ||
29571f65f947SAndy Whitcroft					    ($op eq '>' &&
29581f65f947SAndy Whitcroft					     $ca =~ /<\S+\@\S+$/))
29591f65f947SAndy Whitcroft					{
29601f65f947SAndy Whitcroft					    	$ok = 1;
29611f65f947SAndy Whitcroft					}
29621f65f947SAndy Whitcroft
29631f65f947SAndy Whitcroft					# Ignore ?:
29641f65f947SAndy Whitcroft					if (($opv eq ':O' && $ca =~ /\?$/) ||
29651f65f947SAndy Whitcroft					    ($op eq '?' && $cc =~ /^:/)) {
29661f65f947SAndy Whitcroft					    	$ok = 1;
29671f65f947SAndy Whitcroft					}
29681f65f947SAndy Whitcroft
29691f65f947SAndy Whitcroft					if ($ok == 0) {
29703705ce5bSJoe Perches						if (ERROR("SPACING",
29713705ce5bSJoe Perches							  "spaces required around that '$op' $at\n" . $hereptr)) {
29723705ce5bSJoe Perches							$good = trim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
29733705ce5bSJoe Perches							$good = $fix_elements[$n] . " " . trim($fix_elements[$n + 1]) . " ";
29743705ce5bSJoe Perches							$line_fixed = 1;
29753705ce5bSJoe Perches						}
29760a920b5bSAndy Whitcroft					}
297722f2a2efSAndy Whitcroft				}
29784a0df2efSAndy Whitcroft				$off += length($elements[$n + 1]);
29793705ce5bSJoe Perches
29803705ce5bSJoe Perches##				print("n: <$n> GOOD: <$good>\n");
29813705ce5bSJoe Perches
29823705ce5bSJoe Perches				$fixed_line = $fixed_line . $good;
29830a920b5bSAndy Whitcroft			}
29843705ce5bSJoe Perches
29853705ce5bSJoe Perches			if (($#elements % 2) == 0) {
29863705ce5bSJoe Perches				$fixed_line = $fixed_line . $fix_elements[$#elements];
29873705ce5bSJoe Perches			}
29883705ce5bSJoe Perches
29893705ce5bSJoe Perches			if ($fix && $line_fixed && $fixed_line ne $fixed[$linenr - 1]) {
29903705ce5bSJoe Perches				$fixed[$linenr - 1] = $fixed_line;
29913705ce5bSJoe Perches			}
29923705ce5bSJoe Perches
29933705ce5bSJoe Perches
29940a920b5bSAndy Whitcroft		}
29950a920b5bSAndy Whitcroft
2996786b6326SJoe Perches# check for whitespace before a non-naked semicolon
2997786b6326SJoe Perches		if ($line =~ /^\+.*\S\s+;/) {
2998786b6326SJoe Perches			if (WARN("SPACING",
2999786b6326SJoe Perches				 "space prohibited before semicolon\n" . $herecurr) &&
3000786b6326SJoe Perches			    $fix) {
3001786b6326SJoe Perches				1 while $fixed[$linenr - 1] =~
3002786b6326SJoe Perches				    s/^(\+.*\S)\s+;/$1;/;
3003786b6326SJoe Perches			}
3004786b6326SJoe Perches		}
3005786b6326SJoe Perches
3006f0a594c1SAndy Whitcroft# check for multiple assignments
3007f0a594c1SAndy Whitcroft		if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
3008000d1cc1SJoe Perches			CHK("MULTIPLE_ASSIGNMENTS",
3009000d1cc1SJoe Perches			    "multiple assignments should be avoided\n" . $herecurr);
3010f0a594c1SAndy Whitcroft		}
3011f0a594c1SAndy Whitcroft
301222f2a2efSAndy Whitcroft## # check for multiple declarations, allowing for a function declaration
301322f2a2efSAndy Whitcroft## # continuation.
301422f2a2efSAndy Whitcroft## 		if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
301522f2a2efSAndy Whitcroft## 		    $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
301622f2a2efSAndy Whitcroft##
301722f2a2efSAndy Whitcroft## 			# Remove any bracketed sections to ensure we do not
301822f2a2efSAndy Whitcroft## 			# falsly report the parameters of functions.
301922f2a2efSAndy Whitcroft## 			my $ln = $line;
302022f2a2efSAndy Whitcroft## 			while ($ln =~ s/\([^\(\)]*\)//g) {
302122f2a2efSAndy Whitcroft## 			}
302222f2a2efSAndy Whitcroft## 			if ($ln =~ /,/) {
3023000d1cc1SJoe Perches## 				WARN("MULTIPLE_DECLARATION",
3024000d1cc1SJoe Perches##				     "declaring multiple variables together should be avoided\n" . $herecurr);
302522f2a2efSAndy Whitcroft## 			}
302622f2a2efSAndy Whitcroft## 		}
3027f0a594c1SAndy Whitcroft
30280a920b5bSAndy Whitcroft#need space before brace following if, while, etc
302922f2a2efSAndy Whitcroft		if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
303022f2a2efSAndy Whitcroft		    $line =~ /do{/) {
30313705ce5bSJoe Perches			if (ERROR("SPACING",
30323705ce5bSJoe Perches				  "space required before the open brace '{'\n" . $herecurr) &&
30333705ce5bSJoe Perches			    $fix) {
30343705ce5bSJoe Perches				$fixed[$linenr - 1] =~
30353705ce5bSJoe Perches				    s/^(\+.*(?:do|\))){/$1 {/;
30363705ce5bSJoe Perches			}
3037de7d4f0eSAndy Whitcroft		}
3038de7d4f0eSAndy Whitcroft
3039c4a62ef9SJoe Perches## # check for blank lines before declarations
3040c4a62ef9SJoe Perches##		if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
3041c4a62ef9SJoe Perches##		    $prevrawline =~ /^.\s*$/) {
3042c4a62ef9SJoe Perches##			WARN("SPACING",
3043c4a62ef9SJoe Perches##			     "No blank lines before declarations\n" . $hereprev);
3044c4a62ef9SJoe Perches##		}
3045c4a62ef9SJoe Perches##
3046c4a62ef9SJoe Perches
3047de7d4f0eSAndy Whitcroft# closing brace should have a space following it when it has anything
3048de7d4f0eSAndy Whitcroft# on the line
3049de7d4f0eSAndy Whitcroft		if ($line =~ /}(?!(?:,|;|\)))\S/) {
3050000d1cc1SJoe Perches			ERROR("SPACING",
3051000d1cc1SJoe Perches			      "space required after that close brace '}'\n" . $herecurr);
30520a920b5bSAndy Whitcroft		}
30530a920b5bSAndy Whitcroft
305422f2a2efSAndy Whitcroft# check spacing on square brackets
305522f2a2efSAndy Whitcroft		if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
30563705ce5bSJoe Perches			if (ERROR("SPACING",
30573705ce5bSJoe Perches				  "space prohibited after that open square bracket '['\n" . $herecurr) &&
30583705ce5bSJoe Perches			    $fix) {
30593705ce5bSJoe Perches				$fixed[$linenr - 1] =~
30603705ce5bSJoe Perches				    s/\[\s+/\[/;
30613705ce5bSJoe Perches			}
306222f2a2efSAndy Whitcroft		}
306322f2a2efSAndy Whitcroft		if ($line =~ /\s\]/) {
30643705ce5bSJoe Perches			if (ERROR("SPACING",
30653705ce5bSJoe Perches				  "space prohibited before that close square bracket ']'\n" . $herecurr) &&
30663705ce5bSJoe Perches			    $fix) {
30673705ce5bSJoe Perches				$fixed[$linenr - 1] =~
30683705ce5bSJoe Perches				    s/\s+\]/\]/;
30693705ce5bSJoe Perches			}
307022f2a2efSAndy Whitcroft		}
307122f2a2efSAndy Whitcroft
3072c45dcabdSAndy Whitcroft# check spacing on parentheses
30739c0ca6f9SAndy Whitcroft		if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
30749c0ca6f9SAndy Whitcroft		    $line !~ /for\s*\(\s+;/) {
30753705ce5bSJoe Perches			if (ERROR("SPACING",
30763705ce5bSJoe Perches				  "space prohibited after that open parenthesis '('\n" . $herecurr) &&
30773705ce5bSJoe Perches			    $fix) {
30783705ce5bSJoe Perches				$fixed[$linenr - 1] =~
30793705ce5bSJoe Perches				    s/\(\s+/\(/;
30803705ce5bSJoe Perches			}
308122f2a2efSAndy Whitcroft		}
308213214adfSAndy Whitcroft		if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
3083c45dcabdSAndy Whitcroft		    $line !~ /for\s*\(.*;\s+\)/ &&
3084c45dcabdSAndy Whitcroft		    $line !~ /:\s+\)/) {
30853705ce5bSJoe Perches			if (ERROR("SPACING",
30863705ce5bSJoe Perches				  "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
30873705ce5bSJoe Perches			    $fix) {
30883705ce5bSJoe Perches				$fixed[$linenr - 1] =~
30893705ce5bSJoe Perches				    s/\s+\)/\)/;
30903705ce5bSJoe Perches			}
309122f2a2efSAndy Whitcroft		}
309222f2a2efSAndy Whitcroft
30930a920b5bSAndy Whitcroft#goto labels aren't indented, allow a single space however
30944a0df2efSAndy Whitcroft		if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
30950a920b5bSAndy Whitcroft		   !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
30963705ce5bSJoe Perches			if (WARN("INDENTED_LABEL",
30973705ce5bSJoe Perches				 "labels should not be indented\n" . $herecurr) &&
30983705ce5bSJoe Perches			    $fix) {
30993705ce5bSJoe Perches				$fixed[$linenr - 1] =~
31003705ce5bSJoe Perches				    s/^(.)\s+/$1/;
31013705ce5bSJoe Perches			}
31020a920b5bSAndy Whitcroft		}
31030a920b5bSAndy Whitcroft
3104c45dcabdSAndy Whitcroft# Return is not a function.
3105c45dcabdSAndy Whitcroft		if (defined($stat) && $stat =~ /^.\s*return(\s*)(\(.*);/s) {
3106c45dcabdSAndy Whitcroft			my $spacing = $1;
3107c45dcabdSAndy Whitcroft			my $value = $2;
3108c45dcabdSAndy Whitcroft
310986f9d059SAndy Whitcroft			# Flatten any parentheses
3110fb2d2c1bSAndy Whitcroft			$value =~ s/\(/ \(/g;
3111fb2d2c1bSAndy Whitcroft			$value =~ s/\)/\) /g;
3112e01886adSAndy Whitcroft			while ($value =~ s/\[[^\[\]]*\]/1/ ||
311363f17f89SAndy Whitcroft			       $value !~ /(?:$Ident|-?$Constant)\s*
311463f17f89SAndy Whitcroft					     $Compare\s*
311563f17f89SAndy Whitcroft					     (?:$Ident|-?$Constant)/x &&
311663f17f89SAndy Whitcroft			       $value =~ s/\([^\(\)]*\)/1/) {
3117c45dcabdSAndy Whitcroft			}
3118fb2d2c1bSAndy Whitcroft#print "value<$value>\n";
3119fb2d2c1bSAndy Whitcroft			if ($value =~ /^\s*(?:$Ident|-?$Constant)\s*$/) {
3120000d1cc1SJoe Perches				ERROR("RETURN_PARENTHESES",
3121000d1cc1SJoe Perches				      "return is not a function, parentheses are not required\n" . $herecurr);
3122c45dcabdSAndy Whitcroft
3123c45dcabdSAndy Whitcroft			} elsif ($spacing !~ /\s+/) {
3124000d1cc1SJoe Perches				ERROR("SPACING",
3125000d1cc1SJoe Perches				      "space required before the open parenthesis '('\n" . $herecurr);
3126c45dcabdSAndy Whitcroft			}
3127c45dcabdSAndy Whitcroft		}
312853a3c448SAndy Whitcroft# Return of what appears to be an errno should normally be -'ve
312953a3c448SAndy Whitcroft		if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) {
313053a3c448SAndy Whitcroft			my $name = $1;
313153a3c448SAndy Whitcroft			if ($name ne 'EOF' && $name ne 'ERROR') {
3132000d1cc1SJoe Perches				WARN("USE_NEGATIVE_ERRNO",
3133000d1cc1SJoe Perches				     "return of an errno should typically be -ve (return -$1)\n" . $herecurr);
313453a3c448SAndy Whitcroft			}
313553a3c448SAndy Whitcroft		}
3136c45dcabdSAndy Whitcroft
31370a920b5bSAndy Whitcroft# Need a space before open parenthesis after if, while etc
31384a0df2efSAndy Whitcroft		if ($line =~ /\b(if|while|for|switch)\(/) {
31393705ce5bSJoe Perches			if (ERROR("SPACING",
31403705ce5bSJoe Perches				  "space required before the open parenthesis '('\n" . $herecurr) &&
31413705ce5bSJoe Perches			    $fix) {
31423705ce5bSJoe Perches				$fixed[$linenr - 1] =~
31433705ce5bSJoe Perches				    s/\b(if|while|for|switch)\(/$1 \(/;
31443705ce5bSJoe Perches			}
31450a920b5bSAndy Whitcroft		}
31460a920b5bSAndy Whitcroft
3147f5fe35ddSAndy Whitcroft# Check for illegal assignment in if conditional -- and check for trailing
3148f5fe35ddSAndy Whitcroft# statements after the conditional.
3149170d3a22SAndy Whitcroft		if ($line =~ /do\s*(?!{)/) {
31503e469cdcSAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
31513e469cdcSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0)
31523e469cdcSAndy Whitcroft					if (!defined $stat);
3153170d3a22SAndy Whitcroft			my ($stat_next) = ctx_statement_block($line_nr_next,
3154170d3a22SAndy Whitcroft						$remain_next, $off_next);
3155170d3a22SAndy Whitcroft			$stat_next =~ s/\n./\n /g;
3156170d3a22SAndy Whitcroft			##print "stat<$stat> stat_next<$stat_next>\n";
3157170d3a22SAndy Whitcroft
3158170d3a22SAndy Whitcroft			if ($stat_next =~ /^\s*while\b/) {
3159170d3a22SAndy Whitcroft				# If the statement carries leading newlines,
3160170d3a22SAndy Whitcroft				# then count those as offsets.
3161170d3a22SAndy Whitcroft				my ($whitespace) =
3162170d3a22SAndy Whitcroft					($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
3163170d3a22SAndy Whitcroft				my $offset =
3164170d3a22SAndy Whitcroft					statement_rawlines($whitespace) - 1;
3165170d3a22SAndy Whitcroft
3166170d3a22SAndy Whitcroft				$suppress_whiletrailers{$line_nr_next +
3167170d3a22SAndy Whitcroft								$offset} = 1;
3168170d3a22SAndy Whitcroft			}
3169170d3a22SAndy Whitcroft		}
3170170d3a22SAndy Whitcroft		if (!defined $suppress_whiletrailers{$linenr} &&
3171170d3a22SAndy Whitcroft		    $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
3172171ae1a4SAndy Whitcroft			my ($s, $c) = ($stat, $cond);
31738905a67cSAndy Whitcroft
3174b53c8e10SAndy Whitcroft			if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
3175000d1cc1SJoe Perches				ERROR("ASSIGN_IN_IF",
3176000d1cc1SJoe Perches				      "do not use assignment in if condition\n" . $herecurr);
31778905a67cSAndy Whitcroft			}
31788905a67cSAndy Whitcroft
31798905a67cSAndy Whitcroft			# Find out what is on the end of the line after the
31808905a67cSAndy Whitcroft			# conditional.
3181773647a0SAndy Whitcroft			substr($s, 0, length($c), '');
31828905a67cSAndy Whitcroft			$s =~ s/\n.*//g;
318313214adfSAndy Whitcroft			$s =~ s/$;//g; 	# Remove any comments
318453210168SAndy Whitcroft			if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
318553210168SAndy Whitcroft			    $c !~ /}\s*while\s*/)
3186773647a0SAndy Whitcroft			{
3187bb44ad39SAndy Whitcroft				# Find out how long the conditional actually is.
3188bb44ad39SAndy Whitcroft				my @newlines = ($c =~ /\n/gs);
3189bb44ad39SAndy Whitcroft				my $cond_lines = 1 + $#newlines;
319042bdf74cSHidetoshi Seto				my $stat_real = '';
3191bb44ad39SAndy Whitcroft
319242bdf74cSHidetoshi Seto				$stat_real = raw_line($linenr, $cond_lines)
319342bdf74cSHidetoshi Seto							. "\n" if ($cond_lines);
3194bb44ad39SAndy Whitcroft				if (defined($stat_real) && $cond_lines > 1) {
3195bb44ad39SAndy Whitcroft					$stat_real = "[...]\n$stat_real";
3196bb44ad39SAndy Whitcroft				}
3197bb44ad39SAndy Whitcroft
3198000d1cc1SJoe Perches				ERROR("TRAILING_STATEMENTS",
3199000d1cc1SJoe Perches				      "trailing statements should be on next line\n" . $herecurr . $stat_real);
32008905a67cSAndy Whitcroft			}
32018905a67cSAndy Whitcroft		}
32028905a67cSAndy Whitcroft
320313214adfSAndy Whitcroft# Check for bitwise tests written as boolean
320413214adfSAndy Whitcroft		if ($line =~ /
320513214adfSAndy Whitcroft			(?:
320613214adfSAndy Whitcroft				(?:\[|\(|\&\&|\|\|)
320713214adfSAndy Whitcroft				\s*0[xX][0-9]+\s*
320813214adfSAndy Whitcroft				(?:\&\&|\|\|)
320913214adfSAndy Whitcroft			|
321013214adfSAndy Whitcroft				(?:\&\&|\|\|)
321113214adfSAndy Whitcroft				\s*0[xX][0-9]+\s*
321213214adfSAndy Whitcroft				(?:\&\&|\|\||\)|\])
321313214adfSAndy Whitcroft			)/x)
321413214adfSAndy Whitcroft		{
3215000d1cc1SJoe Perches			WARN("HEXADECIMAL_BOOLEAN_TEST",
3216000d1cc1SJoe Perches			     "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
321713214adfSAndy Whitcroft		}
321813214adfSAndy Whitcroft
32198905a67cSAndy Whitcroft# if and else should not have general statements after it
322013214adfSAndy Whitcroft		if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
322113214adfSAndy Whitcroft			my $s = $1;
322213214adfSAndy Whitcroft			$s =~ s/$;//g; 	# Remove any comments
322313214adfSAndy Whitcroft			if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
3224000d1cc1SJoe Perches				ERROR("TRAILING_STATEMENTS",
3225000d1cc1SJoe Perches				      "trailing statements should be on next line\n" . $herecurr);
32260a920b5bSAndy Whitcroft			}
322713214adfSAndy Whitcroft		}
322839667782SAndy Whitcroft# if should not continue a brace
322939667782SAndy Whitcroft		if ($line =~ /}\s*if\b/) {
3230000d1cc1SJoe Perches			ERROR("TRAILING_STATEMENTS",
3231000d1cc1SJoe Perches			      "trailing statements should be on next line\n" .
323239667782SAndy Whitcroft				$herecurr);
323339667782SAndy Whitcroft		}
3234a1080bf8SAndy Whitcroft# case and default should not have general statements after them
3235a1080bf8SAndy Whitcroft		if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
3236a1080bf8SAndy Whitcroft		    $line !~ /\G(?:
32373fef12d6SAndy Whitcroft			(?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
3238a1080bf8SAndy Whitcroft			\s*return\s+
3239a1080bf8SAndy Whitcroft		    )/xg)
3240a1080bf8SAndy Whitcroft		{
3241000d1cc1SJoe Perches			ERROR("TRAILING_STATEMENTS",
3242000d1cc1SJoe Perches			      "trailing statements should be on next line\n" . $herecurr);
3243a1080bf8SAndy Whitcroft		}
32440a920b5bSAndy Whitcroft
32450a920b5bSAndy Whitcroft		# Check for }<nl>else {, these must be at the same
32460a920b5bSAndy Whitcroft		# indent level to be relevant to each other.
32470a920b5bSAndy Whitcroft		if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
32480a920b5bSAndy Whitcroft						$previndent == $indent) {
3249000d1cc1SJoe Perches			ERROR("ELSE_AFTER_BRACE",
3250000d1cc1SJoe Perches			      "else should follow close brace '}'\n" . $hereprev);
32510a920b5bSAndy Whitcroft		}
32520a920b5bSAndy Whitcroft
3253c2fdda0dSAndy Whitcroft		if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and
3254c2fdda0dSAndy Whitcroft						$previndent == $indent) {
3255c2fdda0dSAndy Whitcroft			my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
3256c2fdda0dSAndy Whitcroft
3257c2fdda0dSAndy Whitcroft			# Find out what is on the end of the line after the
3258c2fdda0dSAndy Whitcroft			# conditional.
3259773647a0SAndy Whitcroft			substr($s, 0, length($c), '');
3260c2fdda0dSAndy Whitcroft			$s =~ s/\n.*//g;
3261c2fdda0dSAndy Whitcroft
3262c2fdda0dSAndy Whitcroft			if ($s =~ /^\s*;/) {
3263000d1cc1SJoe Perches				ERROR("WHILE_AFTER_BRACE",
3264000d1cc1SJoe Perches				      "while should follow close brace '}'\n" . $hereprev);
3265c2fdda0dSAndy Whitcroft			}
3266c2fdda0dSAndy Whitcroft		}
3267c2fdda0dSAndy Whitcroft
326895e2c602SJoe Perches#Specific variable tests
3269323c1260SJoe Perches		while ($line =~ m{($Constant|$Lval)}g) {
3270323c1260SJoe Perches			my $var = $1;
327195e2c602SJoe Perches
327295e2c602SJoe Perches#gcc binary extension
327395e2c602SJoe Perches			if ($var =~ /^$Binary$/) {
327495e2c602SJoe Perches				WARN("GCC_BINARY_CONSTANT",
327595e2c602SJoe Perches				     "Avoid gcc v4.3+ binary constant extension: <$var>\n" . $herecurr);
327695e2c602SJoe Perches			}
327795e2c602SJoe Perches
327895e2c602SJoe Perches#CamelCase
3279807bd26cSJoe Perches			if ($var !~ /^$Constant$/ &&
3280be79794bSJoe Perches			    $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
328122735ce8SJoe Perches#Ignore Page<foo> variants
3282807bd26cSJoe Perches			    $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
328322735ce8SJoe Perches#Ignore SI style variants like nS, mV and dB (ie: max_uV, regulator_min_uA_show)
32843445686aSJoe Perches			    $var !~ /^(?:[a-z_]*?)_?[a-z][A-Z](?:_[a-z_]+)?$/) {
32853445686aSJoe Perches				seed_camelcase_includes() if ($check);
32863445686aSJoe Perches				if (!defined $camelcase{$var}) {
3287323c1260SJoe Perches					$camelcase{$var} = 1;
3288be79794bSJoe Perches					CHK("CAMELCASE",
3289323c1260SJoe Perches					    "Avoid CamelCase: <$var>\n" . $herecurr);
3290323c1260SJoe Perches				}
3291323c1260SJoe Perches			}
32923445686aSJoe Perches		}
32930a920b5bSAndy Whitcroft
32940a920b5bSAndy Whitcroft#no spaces allowed after \ in define
3295c45dcabdSAndy Whitcroft		if ($line=~/\#\s*define.*\\\s$/) {
3296000d1cc1SJoe Perches			WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
3297000d1cc1SJoe Perches			     "Whitepspace after \\ makes next lines useless\n" . $herecurr);
32980a920b5bSAndy Whitcroft		}
32990a920b5bSAndy Whitcroft
3300653d4876SAndy Whitcroft#warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
3301c45dcabdSAndy Whitcroft		if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
3302e09dec48SAndy Whitcroft			my $file = "$1.h";
3303e09dec48SAndy Whitcroft			my $checkfile = "include/linux/$file";
3304e09dec48SAndy Whitcroft			if (-f "$root/$checkfile" &&
3305e09dec48SAndy Whitcroft			    $realfile ne $checkfile &&
33067840a94cSWolfram Sang			    $1 !~ /$allowed_asm_includes/)
3307c45dcabdSAndy Whitcroft			{
3308e09dec48SAndy Whitcroft				if ($realfile =~ m{^arch/}) {
3309000d1cc1SJoe Perches					CHK("ARCH_INCLUDE_LINUX",
3310000d1cc1SJoe Perches					    "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
3311e09dec48SAndy Whitcroft				} else {
3312000d1cc1SJoe Perches					WARN("INCLUDE_LINUX",
3313000d1cc1SJoe Perches					     "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
3314e09dec48SAndy Whitcroft				}
33150a920b5bSAndy Whitcroft			}
33160a920b5bSAndy Whitcroft		}
33170a920b5bSAndy Whitcroft
3318653d4876SAndy Whitcroft# multi-statement macros should be enclosed in a do while loop, grab the
3319653d4876SAndy Whitcroft# first statement and ensure its the whole macro if its not enclosed
3320cf655043SAndy Whitcroft# in a known good container
3321b8f96a31SAndy Whitcroft		if ($realfile !~ m@/vmlinux.lds.h$@ &&
3322b8f96a31SAndy Whitcroft		    $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
3323d8aaf121SAndy Whitcroft			my $ln = $linenr;
3324d8aaf121SAndy Whitcroft			my $cnt = $realcnt;
3325c45dcabdSAndy Whitcroft			my ($off, $dstat, $dcond, $rest);
3326c45dcabdSAndy Whitcroft			my $ctx = '';
3327c45dcabdSAndy Whitcroft			($dstat, $dcond, $ln, $cnt, $off) =
3328f74bd194SAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0);
3329f74bd194SAndy Whitcroft			$ctx = $dstat;
3330c45dcabdSAndy Whitcroft			#print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
3331a3bb97a7SAndy Whitcroft			#print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
3332c45dcabdSAndy Whitcroft
3333f74bd194SAndy Whitcroft			$dstat =~ s/^.\s*\#\s*define\s+$Ident(?:\([^\)]*\))?\s*//;
3334292f1a9bSAndy Whitcroft			$dstat =~ s/$;//g;
3335c45dcabdSAndy Whitcroft			$dstat =~ s/\\\n.//g;
3336c45dcabdSAndy Whitcroft			$dstat =~ s/^\s*//s;
3337c45dcabdSAndy Whitcroft			$dstat =~ s/\s*$//s;
3338c45dcabdSAndy Whitcroft
3339c45dcabdSAndy Whitcroft			# Flatten any parentheses and braces
3340bf30d6edSAndy Whitcroft			while ($dstat =~ s/\([^\(\)]*\)/1/ ||
3341bf30d6edSAndy Whitcroft			       $dstat =~ s/\{[^\{\}]*\}/1/ ||
3342c81769fdSAndy Whitcroft			       $dstat =~ s/\[[^\[\]]*\]/1/)
3343bf30d6edSAndy Whitcroft			{
3344c45dcabdSAndy Whitcroft			}
3345c45dcabdSAndy Whitcroft
3346e45bab8eSAndy Whitcroft			# Flatten any obvious string concatentation.
3347e45bab8eSAndy Whitcroft			while ($dstat =~ s/("X*")\s*$Ident/$1/ ||
3348e45bab8eSAndy Whitcroft			       $dstat =~ s/$Ident\s*("X*")/$1/)
3349e45bab8eSAndy Whitcroft			{
3350e45bab8eSAndy Whitcroft			}
3351e45bab8eSAndy Whitcroft
3352c45dcabdSAndy Whitcroft			my $exceptions = qr{
3353c45dcabdSAndy Whitcroft				$Declare|
3354c45dcabdSAndy Whitcroft				module_param_named|
3355a0a0a7a9SKees Cook				MODULE_PARM_DESC|
3356c45dcabdSAndy Whitcroft				DECLARE_PER_CPU|
3357c45dcabdSAndy Whitcroft				DEFINE_PER_CPU|
3358383099fdSAndy Whitcroft				__typeof__\(|
335922fd2d3eSStefani Seibold				union|
336022fd2d3eSStefani Seibold				struct|
3361ea71a0a0SAndy Whitcroft				\.$Ident\s*=\s*|
3362ea71a0a0SAndy Whitcroft				^\"|\"$
3363c45dcabdSAndy Whitcroft			}x;
33645eaa20b9SAndy Whitcroft			#print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
3365f74bd194SAndy Whitcroft			if ($dstat ne '' &&
3366f74bd194SAndy Whitcroft			    $dstat !~ /^(?:$Ident|-?$Constant),$/ &&			# 10, // foo(),
3367f74bd194SAndy Whitcroft			    $dstat !~ /^(?:$Ident|-?$Constant);$/ &&			# foo();
33683cc4b1c3SJoe Perches			    $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ &&		# 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
3369b9df76acSAndy Whitcroft			    $dstat !~ /^'X'$/ &&					# character constants
3370f74bd194SAndy Whitcroft			    $dstat !~ /$exceptions/ &&
3371f74bd194SAndy Whitcroft			    $dstat !~ /^\.$Ident\s*=/ &&				# .foo =
3372e942e2c3SJoe Perches			    $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ &&		# stringification #foo
337372f115f9SAndy Whitcroft			    $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ &&	# do {...} while (...); // do {...} while (...)
3374f74bd194SAndy Whitcroft			    $dstat !~ /^for\s*$Constant$/ &&				# for (...)
3375f74bd194SAndy Whitcroft			    $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ &&	# for (...) bar()
3376f74bd194SAndy Whitcroft			    $dstat !~ /^do\s*{/ &&					# do {...
3377f74bd194SAndy Whitcroft			    $dstat !~ /^\({/)						# ({...
3378c45dcabdSAndy Whitcroft			{
3379f74bd194SAndy Whitcroft				$ctx =~ s/\n*$//;
3380f74bd194SAndy Whitcroft				my $herectx = $here . "\n";
3381f74bd194SAndy Whitcroft				my $cnt = statement_rawlines($ctx);
3382f74bd194SAndy Whitcroft
3383f74bd194SAndy Whitcroft				for (my $n = 0; $n < $cnt; $n++) {
3384f74bd194SAndy Whitcroft					$herectx .= raw_line($linenr, $n) . "\n";
3385c45dcabdSAndy Whitcroft				}
3386c45dcabdSAndy Whitcroft
3387f74bd194SAndy Whitcroft				if ($dstat =~ /;/) {
3388f74bd194SAndy Whitcroft					ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
3389f74bd194SAndy Whitcroft					      "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
3390f74bd194SAndy Whitcroft				} else {
3391000d1cc1SJoe Perches					ERROR("COMPLEX_MACRO",
3392f74bd194SAndy Whitcroft					      "Macros with complex values should be enclosed in parenthesis\n" . "$herectx");
3393d8aaf121SAndy Whitcroft				}
33940a920b5bSAndy Whitcroft			}
33955023d347SJoe Perches
3396481eb486SJoe Perches# check for line continuations outside of #defines, preprocessor #, and asm
33975023d347SJoe Perches
33985023d347SJoe Perches		} else {
33995023d347SJoe Perches			if ($prevline !~ /^..*\\$/ &&
3400481eb486SJoe Perches			    $line !~ /^\+\s*\#.*\\$/ &&		# preprocessor
3401481eb486SJoe Perches			    $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ &&	# asm
34025023d347SJoe Perches			    $line =~ /^\+.*\\$/) {
34035023d347SJoe Perches				WARN("LINE_CONTINUATIONS",
34045023d347SJoe Perches				     "Avoid unnecessary line continuations\n" . $herecurr);
34055023d347SJoe Perches			}
3406653d4876SAndy Whitcroft		}
34070a920b5bSAndy Whitcroft
3408b13edf7fSJoe Perches# do {} while (0) macro tests:
3409b13edf7fSJoe Perches# single-statement macros do not need to be enclosed in do while (0) loop,
3410b13edf7fSJoe Perches# macro should not end with a semicolon
3411b13edf7fSJoe Perches		if ($^V && $^V ge 5.10.0 &&
3412b13edf7fSJoe Perches		    $realfile !~ m@/vmlinux.lds.h$@ &&
3413b13edf7fSJoe Perches		    $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
3414b13edf7fSJoe Perches			my $ln = $linenr;
3415b13edf7fSJoe Perches			my $cnt = $realcnt;
3416b13edf7fSJoe Perches			my ($off, $dstat, $dcond, $rest);
3417b13edf7fSJoe Perches			my $ctx = '';
3418b13edf7fSJoe Perches			($dstat, $dcond, $ln, $cnt, $off) =
3419b13edf7fSJoe Perches				ctx_statement_block($linenr, $realcnt, 0);
3420b13edf7fSJoe Perches			$ctx = $dstat;
3421b13edf7fSJoe Perches
3422b13edf7fSJoe Perches			$dstat =~ s/\\\n.//g;
3423b13edf7fSJoe Perches
3424b13edf7fSJoe Perches			if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
3425b13edf7fSJoe Perches				my $stmts = $2;
3426b13edf7fSJoe Perches				my $semis = $3;
3427b13edf7fSJoe Perches
3428b13edf7fSJoe Perches				$ctx =~ s/\n*$//;
3429b13edf7fSJoe Perches				my $cnt = statement_rawlines($ctx);
3430b13edf7fSJoe Perches				my $herectx = $here . "\n";
3431b13edf7fSJoe Perches
3432b13edf7fSJoe Perches				for (my $n = 0; $n < $cnt; $n++) {
3433b13edf7fSJoe Perches					$herectx .= raw_line($linenr, $n) . "\n";
3434b13edf7fSJoe Perches				}
3435b13edf7fSJoe Perches
3436ac8e97f8SJoe Perches				if (($stmts =~ tr/;/;/) == 1 &&
3437ac8e97f8SJoe Perches				    $stmts !~ /^\s*(if|while|for|switch)\b/) {
3438b13edf7fSJoe Perches					WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
3439b13edf7fSJoe Perches					     "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
3440b13edf7fSJoe Perches				}
3441b13edf7fSJoe Perches				if (defined $semis && $semis ne "") {
3442b13edf7fSJoe Perches					WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
3443b13edf7fSJoe Perches					     "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
3444b13edf7fSJoe Perches				}
3445b13edf7fSJoe Perches			}
3446b13edf7fSJoe Perches		}
3447b13edf7fSJoe Perches
3448080ba929SMike Frysinger# make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
3449080ba929SMike Frysinger# all assignments may have only one of the following with an assignment:
3450080ba929SMike Frysinger#	.
3451080ba929SMike Frysinger#	ALIGN(...)
3452080ba929SMike Frysinger#	VMLINUX_SYMBOL(...)
3453080ba929SMike Frysinger		if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
3454000d1cc1SJoe Perches			WARN("MISSING_VMLINUX_SYMBOL",
3455000d1cc1SJoe Perches			     "vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
3456080ba929SMike Frysinger		}
3457080ba929SMike Frysinger
3458f0a594c1SAndy Whitcroft# check for redundant bracing round if etc
345913214adfSAndy Whitcroft		if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
346013214adfSAndy Whitcroft			my ($level, $endln, @chunks) =
3461cf655043SAndy Whitcroft				ctx_statement_full($linenr, $realcnt, 1);
346213214adfSAndy Whitcroft			#print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
3463cf655043SAndy Whitcroft			#print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
3464cf655043SAndy Whitcroft			if ($#chunks > 0 && $level == 0) {
3465aad4f614SJoe Perches				my @allowed = ();
3466aad4f614SJoe Perches				my $allow = 0;
346713214adfSAndy Whitcroft				my $seen = 0;
3468773647a0SAndy Whitcroft				my $herectx = $here . "\n";
3469cf655043SAndy Whitcroft				my $ln = $linenr - 1;
347013214adfSAndy Whitcroft				for my $chunk (@chunks) {
347113214adfSAndy Whitcroft					my ($cond, $block) = @{$chunk};
347213214adfSAndy Whitcroft
3473773647a0SAndy Whitcroft					# If the condition carries leading newlines, then count those as offsets.
3474773647a0SAndy Whitcroft					my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
3475773647a0SAndy Whitcroft					my $offset = statement_rawlines($whitespace) - 1;
3476773647a0SAndy Whitcroft
3477aad4f614SJoe Perches					$allowed[$allow] = 0;
3478773647a0SAndy Whitcroft					#print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
3479773647a0SAndy Whitcroft
3480773647a0SAndy Whitcroft					# We have looked at and allowed this specific line.
3481773647a0SAndy Whitcroft					$suppress_ifbraces{$ln + $offset} = 1;
3482773647a0SAndy Whitcroft
3483773647a0SAndy Whitcroft					$herectx .= "$rawlines[$ln + $offset]\n[...]\n";
3484cf655043SAndy Whitcroft					$ln += statement_rawlines($block) - 1;
3485cf655043SAndy Whitcroft
3486773647a0SAndy Whitcroft					substr($block, 0, length($cond), '');
348713214adfSAndy Whitcroft
348813214adfSAndy Whitcroft					$seen++ if ($block =~ /^\s*{/);
348913214adfSAndy Whitcroft
3490aad4f614SJoe Perches					#print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
3491cf655043SAndy Whitcroft					if (statement_lines($cond) > 1) {
3492cf655043SAndy Whitcroft						#print "APW: ALLOWED: cond<$cond>\n";
3493aad4f614SJoe Perches						$allowed[$allow] = 1;
349413214adfSAndy Whitcroft					}
349513214adfSAndy Whitcroft					if ($block =~/\b(?:if|for|while)\b/) {
3496cf655043SAndy Whitcroft						#print "APW: ALLOWED: block<$block>\n";
3497aad4f614SJoe Perches						$allowed[$allow] = 1;
349813214adfSAndy Whitcroft					}
3499cf655043SAndy Whitcroft					if (statement_block_size($block) > 1) {
3500cf655043SAndy Whitcroft						#print "APW: ALLOWED: lines block<$block>\n";
3501aad4f614SJoe Perches						$allowed[$allow] = 1;
350213214adfSAndy Whitcroft					}
3503aad4f614SJoe Perches					$allow++;
350413214adfSAndy Whitcroft				}
3505aad4f614SJoe Perches				if ($seen) {
3506aad4f614SJoe Perches					my $sum_allowed = 0;
3507aad4f614SJoe Perches					foreach (@allowed) {
3508aad4f614SJoe Perches						$sum_allowed += $_;
3509aad4f614SJoe Perches					}
3510aad4f614SJoe Perches					if ($sum_allowed == 0) {
3511000d1cc1SJoe Perches						WARN("BRACES",
3512000d1cc1SJoe Perches						     "braces {} are not necessary for any arm of this statement\n" . $herectx);
3513aad4f614SJoe Perches					} elsif ($sum_allowed != $allow &&
3514aad4f614SJoe Perches						 $seen != $allow) {
3515aad4f614SJoe Perches						CHK("BRACES",
3516aad4f614SJoe Perches						    "braces {} should be used on all arms of this statement\n" . $herectx);
3517aad4f614SJoe Perches					}
351813214adfSAndy Whitcroft				}
351913214adfSAndy Whitcroft			}
352013214adfSAndy Whitcroft		}
3521773647a0SAndy Whitcroft		if (!defined $suppress_ifbraces{$linenr - 1} &&
352213214adfSAndy Whitcroft					$line =~ /\b(if|while|for|else)\b/) {
3523cf655043SAndy Whitcroft			my $allowed = 0;
3524f0a594c1SAndy Whitcroft
3525cf655043SAndy Whitcroft			# Check the pre-context.
3526cf655043SAndy Whitcroft			if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
3527cf655043SAndy Whitcroft				#print "APW: ALLOWED: pre<$1>\n";
3528cf655043SAndy Whitcroft				$allowed = 1;
3529f0a594c1SAndy Whitcroft			}
3530773647a0SAndy Whitcroft
3531773647a0SAndy Whitcroft			my ($level, $endln, @chunks) =
3532773647a0SAndy Whitcroft				ctx_statement_full($linenr, $realcnt, $-[0]);
3533773647a0SAndy Whitcroft
3534cf655043SAndy Whitcroft			# Check the condition.
3535cf655043SAndy Whitcroft			my ($cond, $block) = @{$chunks[0]};
3536773647a0SAndy Whitcroft			#print "CHECKING<$linenr> cond<$cond> block<$block>\n";
3537cf655043SAndy Whitcroft			if (defined $cond) {
3538773647a0SAndy Whitcroft				substr($block, 0, length($cond), '');
3539cf655043SAndy Whitcroft			}
3540cf655043SAndy Whitcroft			if (statement_lines($cond) > 1) {
3541cf655043SAndy Whitcroft				#print "APW: ALLOWED: cond<$cond>\n";
3542cf655043SAndy Whitcroft				$allowed = 1;
3543cf655043SAndy Whitcroft			}
3544cf655043SAndy Whitcroft			if ($block =~/\b(?:if|for|while)\b/) {
3545cf655043SAndy Whitcroft				#print "APW: ALLOWED: block<$block>\n";
3546cf655043SAndy Whitcroft				$allowed = 1;
3547cf655043SAndy Whitcroft			}
3548cf655043SAndy Whitcroft			if (statement_block_size($block) > 1) {
3549cf655043SAndy Whitcroft				#print "APW: ALLOWED: lines block<$block>\n";
3550cf655043SAndy Whitcroft				$allowed = 1;
3551cf655043SAndy Whitcroft			}
3552cf655043SAndy Whitcroft			# Check the post-context.
3553cf655043SAndy Whitcroft			if (defined $chunks[1]) {
3554cf655043SAndy Whitcroft				my ($cond, $block) = @{$chunks[1]};
3555cf655043SAndy Whitcroft				if (defined $cond) {
3556773647a0SAndy Whitcroft					substr($block, 0, length($cond), '');
3557cf655043SAndy Whitcroft				}
3558cf655043SAndy Whitcroft				if ($block =~ /^\s*\{/) {
3559cf655043SAndy Whitcroft					#print "APW: ALLOWED: chunk-1 block<$block>\n";
3560cf655043SAndy Whitcroft					$allowed = 1;
3561cf655043SAndy Whitcroft				}
3562cf655043SAndy Whitcroft			}
3563cf655043SAndy Whitcroft			if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
356469932487SJustin P. Mattock				my $herectx = $here . "\n";
3565f055663cSAndy Whitcroft				my $cnt = statement_rawlines($block);
3566cf655043SAndy Whitcroft
3567f055663cSAndy Whitcroft				for (my $n = 0; $n < $cnt; $n++) {
356869932487SJustin P. Mattock					$herectx .= raw_line($linenr, $n) . "\n";
3569cf655043SAndy Whitcroft				}
3570cf655043SAndy Whitcroft
3571000d1cc1SJoe Perches				WARN("BRACES",
3572000d1cc1SJoe Perches				     "braces {} are not necessary for single statement blocks\n" . $herectx);
3573f0a594c1SAndy Whitcroft			}
3574f0a594c1SAndy Whitcroft		}
3575f0a594c1SAndy Whitcroft
35760979ae66SJoe Perches# check for unnecessary blank lines around braces
357777b9a53aSJoe Perches		if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
35780979ae66SJoe Perches			CHK("BRACES",
35790979ae66SJoe Perches			    "Blank lines aren't necessary before a close brace '}'\n" . $hereprev);
35800979ae66SJoe Perches		}
358177b9a53aSJoe Perches		if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
35820979ae66SJoe Perches			CHK("BRACES",
35830979ae66SJoe Perches			    "Blank lines aren't necessary after an open brace '{'\n" . $hereprev);
35840979ae66SJoe Perches		}
35850979ae66SJoe Perches
35864a0df2efSAndy Whitcroft# no volatiles please
35876c72ffaaSAndy Whitcroft		my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
35886c72ffaaSAndy Whitcroft		if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
3589000d1cc1SJoe Perches			WARN("VOLATILE",
3590000d1cc1SJoe Perches			     "Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
35914a0df2efSAndy Whitcroft		}
35924a0df2efSAndy Whitcroft
359300df344fSAndy Whitcroft# warn about #if 0
3594c45dcabdSAndy Whitcroft		if ($line =~ /^.\s*\#\s*if\s+0\b/) {
3595000d1cc1SJoe Perches			CHK("REDUNDANT_CODE",
3596000d1cc1SJoe Perches			    "if this code is redundant consider removing it\n" .
3597de7d4f0eSAndy Whitcroft				$herecurr);
35984a0df2efSAndy Whitcroft		}
35994a0df2efSAndy Whitcroft
360003df4b51SAndy Whitcroft# check for needless "if (<foo>) fn(<foo>)" uses
360103df4b51SAndy Whitcroft		if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
360203df4b51SAndy Whitcroft			my $expr = '\s*\(\s*' . quotemeta($1) . '\s*\)\s*;';
360303df4b51SAndy Whitcroft			if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?)$expr/) {
360403df4b51SAndy Whitcroft				WARN('NEEDLESS_IF',
360503df4b51SAndy Whitcroft				     "$1(NULL) is safe this check is probably not required\n" . $hereprev);
36064c432a8fSGreg Kroah-Hartman			}
36074c432a8fSGreg Kroah-Hartman		}
3608f0a594c1SAndy Whitcroft
36091a15a250SPatrick Pannuto# prefer usleep_range over udelay
361037581c28SBruce Allan		if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
36111a15a250SPatrick Pannuto			# ignore udelay's < 10, however
361237581c28SBruce Allan			if (! ($1 < 10) ) {
3613000d1cc1SJoe Perches				CHK("USLEEP_RANGE",
3614000d1cc1SJoe Perches				    "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $line);
36151a15a250SPatrick Pannuto			}
36161a15a250SPatrick Pannuto		}
36171a15a250SPatrick Pannuto
361809ef8725SPatrick Pannuto# warn about unexpectedly long msleep's
361909ef8725SPatrick Pannuto		if ($line =~ /\bmsleep\s*\((\d+)\);/) {
362009ef8725SPatrick Pannuto			if ($1 < 20) {
3621000d1cc1SJoe Perches				WARN("MSLEEP",
3622000d1cc1SJoe Perches				     "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $line);
362309ef8725SPatrick Pannuto			}
362409ef8725SPatrick Pannuto		}
362509ef8725SPatrick Pannuto
362636ec1939SJoe Perches# check for comparisons of jiffies
362736ec1939SJoe Perches		if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
362836ec1939SJoe Perches			WARN("JIFFIES_COMPARISON",
362936ec1939SJoe Perches			     "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
363036ec1939SJoe Perches		}
363136ec1939SJoe Perches
36329d7a34a5SJoe Perches# check for comparisons of get_jiffies_64()
36339d7a34a5SJoe Perches		if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
36349d7a34a5SJoe Perches			WARN("JIFFIES_COMPARISON",
36359d7a34a5SJoe Perches			     "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
36369d7a34a5SJoe Perches		}
36379d7a34a5SJoe Perches
363800df344fSAndy Whitcroft# warn about #ifdefs in C files
3639c45dcabdSAndy Whitcroft#		if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
364000df344fSAndy Whitcroft#			print "#ifdef in C files should be avoided\n";
364100df344fSAndy Whitcroft#			print "$herecurr";
364200df344fSAndy Whitcroft#			$clean = 0;
364300df344fSAndy Whitcroft#		}
364400df344fSAndy Whitcroft
364522f2a2efSAndy Whitcroft# warn about spacing in #ifdefs
3646c45dcabdSAndy Whitcroft		if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
36473705ce5bSJoe Perches			if (ERROR("SPACING",
36483705ce5bSJoe Perches				  "exactly one space required after that #$1\n" . $herecurr) &&
36493705ce5bSJoe Perches			    $fix) {
36503705ce5bSJoe Perches				$fixed[$linenr - 1] =~
36513705ce5bSJoe Perches				    s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
36523705ce5bSJoe Perches			}
36533705ce5bSJoe Perches
365422f2a2efSAndy Whitcroft		}
365522f2a2efSAndy Whitcroft
36564a0df2efSAndy Whitcroft# check for spinlock_t definitions without a comment.
3657171ae1a4SAndy Whitcroft		if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
3658171ae1a4SAndy Whitcroft		    $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
36594a0df2efSAndy Whitcroft			my $which = $1;
36604a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
3661000d1cc1SJoe Perches				CHK("UNCOMMENTED_DEFINITION",
3662000d1cc1SJoe Perches				    "$1 definition without comment\n" . $herecurr);
36634a0df2efSAndy Whitcroft			}
36644a0df2efSAndy Whitcroft		}
36654a0df2efSAndy Whitcroft# check for memory barriers without a comment.
36664a0df2efSAndy Whitcroft		if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
36674a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
3668000d1cc1SJoe Perches				CHK("MEMORY_BARRIER",
3669000d1cc1SJoe Perches				    "memory barrier without comment\n" . $herecurr);
36704a0df2efSAndy Whitcroft			}
36714a0df2efSAndy Whitcroft		}
36724a0df2efSAndy Whitcroft# check of hardware specific defines
3673c45dcabdSAndy Whitcroft		if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
3674000d1cc1SJoe Perches			CHK("ARCH_DEFINES",
3675000d1cc1SJoe Perches			    "architecture specific defines should be avoided\n" .  $herecurr);
36760a920b5bSAndy Whitcroft		}
3677653d4876SAndy Whitcroft
3678d4977c78STobias Klauser# Check that the storage class is at the beginning of a declaration
3679d4977c78STobias Klauser		if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
3680000d1cc1SJoe Perches			WARN("STORAGE_CLASS",
3681000d1cc1SJoe Perches			     "storage class should be at the beginning of the declaration\n" . $herecurr)
3682d4977c78STobias Klauser		}
3683d4977c78STobias Klauser
3684de7d4f0eSAndy Whitcroft# check the location of the inline attribute, that it is between
3685de7d4f0eSAndy Whitcroft# storage class and type.
36869c0ca6f9SAndy Whitcroft		if ($line =~ /\b$Type\s+$Inline\b/ ||
36879c0ca6f9SAndy Whitcroft		    $line =~ /\b$Inline\s+$Storage\b/) {
3688000d1cc1SJoe Perches			ERROR("INLINE_LOCATION",
3689000d1cc1SJoe Perches			      "inline keyword should sit between storage class and type\n" . $herecurr);
3690de7d4f0eSAndy Whitcroft		}
3691de7d4f0eSAndy Whitcroft
36928905a67cSAndy Whitcroft# Check for __inline__ and __inline, prefer inline
36938905a67cSAndy Whitcroft		if ($line =~ /\b(__inline__|__inline)\b/) {
3694000d1cc1SJoe Perches			WARN("INLINE",
3695000d1cc1SJoe Perches			     "plain inline is preferred over $1\n" . $herecurr);
36968905a67cSAndy Whitcroft		}
36978905a67cSAndy Whitcroft
36983d130fd0SJoe Perches# Check for __attribute__ packed, prefer __packed
36993d130fd0SJoe Perches		if ($line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
3700000d1cc1SJoe Perches			WARN("PREFER_PACKED",
3701000d1cc1SJoe Perches			     "__packed is preferred over __attribute__((packed))\n" . $herecurr);
37023d130fd0SJoe Perches		}
37033d130fd0SJoe Perches
370439b7e287SJoe Perches# Check for __attribute__ aligned, prefer __aligned
370539b7e287SJoe Perches		if ($line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
3706000d1cc1SJoe Perches			WARN("PREFER_ALIGNED",
3707000d1cc1SJoe Perches			     "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
370839b7e287SJoe Perches		}
370939b7e287SJoe Perches
37105f14d3bdSJoe Perches# Check for __attribute__ format(printf, prefer __printf
37115f14d3bdSJoe Perches		if ($line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
37125f14d3bdSJoe Perches			WARN("PREFER_PRINTF",
37135f14d3bdSJoe Perches			     "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr);
37145f14d3bdSJoe Perches		}
37155f14d3bdSJoe Perches
37166061d949SJoe Perches# Check for __attribute__ format(scanf, prefer __scanf
37176061d949SJoe Perches		if ($line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
37186061d949SJoe Perches			WARN("PREFER_SCANF",
37196061d949SJoe Perches			     "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr);
37206061d949SJoe Perches		}
37216061d949SJoe Perches
37228f53a9b8SJoe Perches# check for sizeof(&)
37238f53a9b8SJoe Perches		if ($line =~ /\bsizeof\s*\(\s*\&/) {
3724000d1cc1SJoe Perches			WARN("SIZEOF_ADDRESS",
3725000d1cc1SJoe Perches			     "sizeof(& should be avoided\n" . $herecurr);
37268f53a9b8SJoe Perches		}
37278f53a9b8SJoe Perches
372866c80b60SJoe Perches# check for sizeof without parenthesis
372966c80b60SJoe Perches		if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
373066c80b60SJoe Perches			WARN("SIZEOF_PARENTHESIS",
373166c80b60SJoe Perches			     "sizeof $1 should be sizeof($1)\n" . $herecurr);
373266c80b60SJoe Perches		}
373366c80b60SJoe Perches
3734428e2fdcSJoe Perches# check for line continuations in quoted strings with odd counts of "
3735428e2fdcSJoe Perches		if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) {
3736000d1cc1SJoe Perches			WARN("LINE_CONTINUATIONS",
3737000d1cc1SJoe Perches			     "Avoid line continuations in quoted strings\n" . $herecurr);
3738428e2fdcSJoe Perches		}
3739428e2fdcSJoe Perches
374088982feaSJoe Perches# check for struct spinlock declarations
374188982feaSJoe Perches		if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
374288982feaSJoe Perches			WARN("USE_SPINLOCK_T",
374388982feaSJoe Perches			     "struct spinlock should be spinlock_t\n" . $herecurr);
374488982feaSJoe Perches		}
374588982feaSJoe Perches
3746a6962d72SJoe Perches# check for seq_printf uses that could be seq_puts
3747a6962d72SJoe Perches		if ($line =~ /\bseq_printf\s*\(/) {
3748a6962d72SJoe Perches			my $fmt = get_quoted_string($line, $rawline);
3749a6962d72SJoe Perches			if ($fmt !~ /[^\\]\%/) {
3750a6962d72SJoe Perches				WARN("PREFER_SEQ_PUTS",
3751a6962d72SJoe Perches				     "Prefer seq_puts to seq_printf\n" . $herecurr);
3752a6962d72SJoe Perches			}
3753a6962d72SJoe Perches		}
3754a6962d72SJoe Perches
3755554e165cSAndy Whitcroft# Check for misused memsets
3756d1fe9c09SJoe Perches		if ($^V && $^V ge 5.10.0 &&
3757d1fe9c09SJoe Perches		    defined $stat &&
3758d7c76ba7SJoe Perches		    $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/s) {
3759554e165cSAndy Whitcroft
3760d7c76ba7SJoe Perches			my $ms_addr = $2;
3761d1fe9c09SJoe Perches			my $ms_val = $7;
3762d1fe9c09SJoe Perches			my $ms_size = $12;
3763d7c76ba7SJoe Perches
3764554e165cSAndy Whitcroft			if ($ms_size =~ /^(0x|)0$/i) {
3765554e165cSAndy Whitcroft				ERROR("MEMSET",
3766d7c76ba7SJoe Perches				      "memset to 0's uses 0 as the 2nd argument, not the 3rd\n" . "$here\n$stat\n");
3767554e165cSAndy Whitcroft			} elsif ($ms_size =~ /^(0x|)1$/i) {
3768554e165cSAndy Whitcroft				WARN("MEMSET",
3769d7c76ba7SJoe Perches				     "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
3770d7c76ba7SJoe Perches			}
3771d7c76ba7SJoe Perches		}
3772d7c76ba7SJoe Perches
3773d7c76ba7SJoe Perches# typecasts on min/max could be min_t/max_t
3774d1fe9c09SJoe Perches		if ($^V && $^V ge 5.10.0 &&
3775d1fe9c09SJoe Perches		    defined $stat &&
3776d7c76ba7SJoe Perches		    $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
3777d1fe9c09SJoe Perches			if (defined $2 || defined $7) {
3778d7c76ba7SJoe Perches				my $call = $1;
3779d7c76ba7SJoe Perches				my $cast1 = deparenthesize($2);
3780d7c76ba7SJoe Perches				my $arg1 = $3;
3781d1fe9c09SJoe Perches				my $cast2 = deparenthesize($7);
3782d1fe9c09SJoe Perches				my $arg2 = $8;
3783d7c76ba7SJoe Perches				my $cast;
3784d7c76ba7SJoe Perches
3785d1fe9c09SJoe Perches				if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
3786d7c76ba7SJoe Perches					$cast = "$cast1 or $cast2";
3787d7c76ba7SJoe Perches				} elsif ($cast1 ne "") {
3788d7c76ba7SJoe Perches					$cast = $cast1;
3789d7c76ba7SJoe Perches				} else {
3790d7c76ba7SJoe Perches					$cast = $cast2;
3791d7c76ba7SJoe Perches				}
3792d7c76ba7SJoe Perches				WARN("MINMAX",
3793d7c76ba7SJoe Perches				     "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
3794554e165cSAndy Whitcroft			}
3795554e165cSAndy Whitcroft		}
3796554e165cSAndy Whitcroft
37974a273195SJoe Perches# check usleep_range arguments
37984a273195SJoe Perches		if ($^V && $^V ge 5.10.0 &&
37994a273195SJoe Perches		    defined $stat &&
38004a273195SJoe Perches		    $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
38014a273195SJoe Perches			my $min = $1;
38024a273195SJoe Perches			my $max = $7;
38034a273195SJoe Perches			if ($min eq $max) {
38044a273195SJoe Perches				WARN("USLEEP_RANGE",
38054a273195SJoe Perches				     "usleep_range should not use min == max args; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
38064a273195SJoe Perches			} elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
38074a273195SJoe Perches				 $min > $max) {
38084a273195SJoe Perches				WARN("USLEEP_RANGE",
38094a273195SJoe Perches				     "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
38104a273195SJoe Perches			}
38114a273195SJoe Perches		}
38124a273195SJoe Perches
3813de7d4f0eSAndy Whitcroft# check for new externs in .c files.
3814171ae1a4SAndy Whitcroft		if ($realfile =~ /\.c$/ && defined $stat &&
3815c45dcabdSAndy Whitcroft		    $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
3816171ae1a4SAndy Whitcroft		{
3817c45dcabdSAndy Whitcroft			my $function_name = $1;
3818c45dcabdSAndy Whitcroft			my $paren_space = $2;
3819171ae1a4SAndy Whitcroft
3820171ae1a4SAndy Whitcroft			my $s = $stat;
3821171ae1a4SAndy Whitcroft			if (defined $cond) {
3822171ae1a4SAndy Whitcroft				substr($s, 0, length($cond), '');
3823171ae1a4SAndy Whitcroft			}
3824c45dcabdSAndy Whitcroft			if ($s =~ /^\s*;/ &&
3825c45dcabdSAndy Whitcroft			    $function_name ne 'uninitialized_var')
3826c45dcabdSAndy Whitcroft			{
3827000d1cc1SJoe Perches				WARN("AVOID_EXTERNS",
3828000d1cc1SJoe Perches				     "externs should be avoided in .c files\n" .  $herecurr);
3829de7d4f0eSAndy Whitcroft			}
3830de7d4f0eSAndy Whitcroft
3831171ae1a4SAndy Whitcroft			if ($paren_space =~ /\n/) {
3832000d1cc1SJoe Perches				WARN("FUNCTION_ARGUMENTS",
3833000d1cc1SJoe Perches				     "arguments for function declarations should follow identifier\n" . $herecurr);
3834171ae1a4SAndy Whitcroft			}
38359c9ba34eSAndy Whitcroft
38369c9ba34eSAndy Whitcroft		} elsif ($realfile =~ /\.c$/ && defined $stat &&
38379c9ba34eSAndy Whitcroft		    $stat =~ /^.\s*extern\s+/)
38389c9ba34eSAndy Whitcroft		{
3839000d1cc1SJoe Perches			WARN("AVOID_EXTERNS",
3840000d1cc1SJoe Perches			     "externs should be avoided in .c files\n" .  $herecurr);
3841171ae1a4SAndy Whitcroft		}
3842171ae1a4SAndy Whitcroft
3843de7d4f0eSAndy Whitcroft# checks for new __setup's
3844de7d4f0eSAndy Whitcroft		if ($rawline =~ /\b__setup\("([^"]*)"/) {
3845de7d4f0eSAndy Whitcroft			my $name = $1;
3846de7d4f0eSAndy Whitcroft
3847de7d4f0eSAndy Whitcroft			if (!grep(/$name/, @setup_docs)) {
3848000d1cc1SJoe Perches				CHK("UNDOCUMENTED_SETUP",
3849000d1cc1SJoe Perches				    "__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
3850de7d4f0eSAndy Whitcroft			}
3851653d4876SAndy Whitcroft		}
38529c0ca6f9SAndy Whitcroft
38539c0ca6f9SAndy Whitcroft# check for pointless casting of kmalloc return
3854caf2a54fSJoe Perches		if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) {
3855000d1cc1SJoe Perches			WARN("UNNECESSARY_CASTS",
3856000d1cc1SJoe Perches			     "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
38579c0ca6f9SAndy Whitcroft		}
385813214adfSAndy Whitcroft
3859a640d25cSJoe Perches# alloc style
3860a640d25cSJoe Perches# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
3861a640d25cSJoe Perches		if ($^V && $^V ge 5.10.0 &&
3862a640d25cSJoe Perches		    $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*([kv][mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
3863a640d25cSJoe Perches			CHK("ALLOC_SIZEOF_STRUCT",
3864a640d25cSJoe Perches			    "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
3865a640d25cSJoe Perches		}
3866a640d25cSJoe Perches
3867972fdea2SJoe Perches# check for krealloc arg reuse
3868972fdea2SJoe Perches		if ($^V && $^V ge 5.10.0 &&
3869972fdea2SJoe Perches		    $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*\1\s*,/) {
3870972fdea2SJoe Perches			WARN("KREALLOC_ARG_REUSE",
3871972fdea2SJoe Perches			     "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
3872972fdea2SJoe Perches		}
3873972fdea2SJoe Perches
38745ce59ae0SJoe Perches# check for alloc argument mismatch
38755ce59ae0SJoe Perches		if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
38765ce59ae0SJoe Perches			WARN("ALLOC_ARRAY_ARGS",
38775ce59ae0SJoe Perches			     "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
38785ce59ae0SJoe Perches		}
38795ce59ae0SJoe Perches
3880caf2a54fSJoe Perches# check for multiple semicolons
3881caf2a54fSJoe Perches		if ($line =~ /;\s*;\s*$/) {
3882000d1cc1SJoe Perches			WARN("ONE_SEMICOLON",
3883000d1cc1SJoe Perches			     "Statements terminations use 1 semicolon\n" . $herecurr);
3884d1e2ad07SJoe Perches		}
3885d1e2ad07SJoe Perches
3886d1e2ad07SJoe Perches# check for switch/default statements without a break;
3887d1e2ad07SJoe Perches		if ($^V && $^V ge 5.10.0 &&
3888d1e2ad07SJoe Perches		    defined $stat &&
3889d1e2ad07SJoe Perches		    $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
3890d1e2ad07SJoe Perches			my $ctx = '';
3891d1e2ad07SJoe Perches			my $herectx = $here . "\n";
3892d1e2ad07SJoe Perches			my $cnt = statement_rawlines($stat);
3893d1e2ad07SJoe Perches			for (my $n = 0; $n < $cnt; $n++) {
3894d1e2ad07SJoe Perches				$herectx .= raw_line($linenr, $n) . "\n";
3895d1e2ad07SJoe Perches			}
3896d1e2ad07SJoe Perches			WARN("DEFAULT_NO_BREAK",
3897d1e2ad07SJoe Perches			     "switch default: should use break\n" . $herectx);
3898caf2a54fSJoe Perches		}
3899caf2a54fSJoe Perches
390013214adfSAndy Whitcroft# check for gcc specific __FUNCTION__
390113214adfSAndy Whitcroft		if ($line =~ /__FUNCTION__/) {
3902000d1cc1SJoe Perches			WARN("USE_FUNC",
3903000d1cc1SJoe Perches			     "__func__ should be used instead of gcc specific __FUNCTION__\n"  . $herecurr);
390413214adfSAndy Whitcroft		}
3905773647a0SAndy Whitcroft
39062c92488aSJoe Perches# check for use of yield()
39072c92488aSJoe Perches		if ($line =~ /\byield\s*\(\s*\)/) {
39082c92488aSJoe Perches			WARN("YIELD",
39092c92488aSJoe Perches			     "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n"  . $herecurr);
39102c92488aSJoe Perches		}
39112c92488aSJoe Perches
3912179f8f40SJoe Perches# check for comparisons against true and false
3913179f8f40SJoe Perches		if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
3914179f8f40SJoe Perches			my $lead = $1;
3915179f8f40SJoe Perches			my $arg = $2;
3916179f8f40SJoe Perches			my $test = $3;
3917179f8f40SJoe Perches			my $otype = $4;
3918179f8f40SJoe Perches			my $trail = $5;
3919179f8f40SJoe Perches			my $op = "!";
3920179f8f40SJoe Perches
3921179f8f40SJoe Perches			($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
3922179f8f40SJoe Perches
3923179f8f40SJoe Perches			my $type = lc($otype);
3924179f8f40SJoe Perches			if ($type =~ /^(?:true|false)$/) {
3925179f8f40SJoe Perches				if (("$test" eq "==" && "$type" eq "true") ||
3926179f8f40SJoe Perches				    ("$test" eq "!=" && "$type" eq "false")) {
3927179f8f40SJoe Perches					$op = "";
3928179f8f40SJoe Perches				}
3929179f8f40SJoe Perches
3930179f8f40SJoe Perches				CHK("BOOL_COMPARISON",
3931179f8f40SJoe Perches				    "Using comparison to $otype is error prone\n" . $herecurr);
3932179f8f40SJoe Perches
3933179f8f40SJoe Perches## maybe suggesting a correct construct would better
3934179f8f40SJoe Perches##				    "Using comparison to $otype is error prone.  Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
3935179f8f40SJoe Perches
3936179f8f40SJoe Perches			}
3937179f8f40SJoe Perches		}
3938179f8f40SJoe Perches
39394882720bSThomas Gleixner# check for semaphores initialized locked
39404882720bSThomas Gleixner		if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
3941000d1cc1SJoe Perches			WARN("CONSIDER_COMPLETION",
3942000d1cc1SJoe Perches			     "consider using a completion\n" . $herecurr);
3943773647a0SAndy Whitcroft		}
39446712d858SJoe Perches
394567d0a075SJoe Perches# recommend kstrto* over simple_strto* and strict_strto*
394667d0a075SJoe Perches		if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
3947000d1cc1SJoe Perches			WARN("CONSIDER_KSTRTO",
394867d0a075SJoe Perches			     "$1 is obsolete, use k$3 instead\n" . $herecurr);
3949773647a0SAndy Whitcroft		}
39506712d858SJoe Perches
3951f3db6639SMichael Ellerman# check for __initcall(), use device_initcall() explicitly please
3952f3db6639SMichael Ellerman		if ($line =~ /^.\s*__initcall\s*\(/) {
3953000d1cc1SJoe Perches			WARN("USE_DEVICE_INITCALL",
3954000d1cc1SJoe Perches			     "please use device_initcall() instead of __initcall()\n" . $herecurr);
3955f3db6639SMichael Ellerman		}
39566712d858SJoe Perches
395779404849SEmese Revfy# check for various ops structs, ensure they are const.
395879404849SEmese Revfy		my $struct_ops = qr{acpi_dock_ops|
395979404849SEmese Revfy				address_space_operations|
396079404849SEmese Revfy				backlight_ops|
396179404849SEmese Revfy				block_device_operations|
396279404849SEmese Revfy				dentry_operations|
396379404849SEmese Revfy				dev_pm_ops|
396479404849SEmese Revfy				dma_map_ops|
396579404849SEmese Revfy				extent_io_ops|
396679404849SEmese Revfy				file_lock_operations|
396779404849SEmese Revfy				file_operations|
396879404849SEmese Revfy				hv_ops|
396979404849SEmese Revfy				ide_dma_ops|
397079404849SEmese Revfy				intel_dvo_dev_ops|
397179404849SEmese Revfy				item_operations|
397279404849SEmese Revfy				iwl_ops|
397379404849SEmese Revfy				kgdb_arch|
397479404849SEmese Revfy				kgdb_io|
397579404849SEmese Revfy				kset_uevent_ops|
397679404849SEmese Revfy				lock_manager_operations|
397779404849SEmese Revfy				microcode_ops|
397879404849SEmese Revfy				mtrr_ops|
397979404849SEmese Revfy				neigh_ops|
398079404849SEmese Revfy				nlmsvc_binding|
398179404849SEmese Revfy				pci_raw_ops|
398279404849SEmese Revfy				pipe_buf_operations|
398379404849SEmese Revfy				platform_hibernation_ops|
398479404849SEmese Revfy				platform_suspend_ops|
398579404849SEmese Revfy				proto_ops|
398679404849SEmese Revfy				rpc_pipe_ops|
398779404849SEmese Revfy				seq_operations|
398879404849SEmese Revfy				snd_ac97_build_ops|
398979404849SEmese Revfy				soc_pcmcia_socket_ops|
399079404849SEmese Revfy				stacktrace_ops|
399179404849SEmese Revfy				sysfs_ops|
399279404849SEmese Revfy				tty_operations|
399379404849SEmese Revfy				usb_mon_operations|
399479404849SEmese Revfy				wd_ops}x;
39956903ffb2SAndy Whitcroft		if ($line !~ /\bconst\b/ &&
399679404849SEmese Revfy		    $line =~ /\bstruct\s+($struct_ops)\b/) {
3997000d1cc1SJoe Perches			WARN("CONST_STRUCT",
3998000d1cc1SJoe Perches			     "struct $1 should normally be const\n" .
39996903ffb2SAndy Whitcroft				$herecurr);
40002b6db5cbSAndy Whitcroft		}
4001773647a0SAndy Whitcroft
4002773647a0SAndy Whitcroft# use of NR_CPUS is usually wrong
4003773647a0SAndy Whitcroft# ignore definitions of NR_CPUS and usage to define arrays as likely right
4004773647a0SAndy Whitcroft		if ($line =~ /\bNR_CPUS\b/ &&
4005c45dcabdSAndy Whitcroft		    $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
4006c45dcabdSAndy Whitcroft		    $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
4007171ae1a4SAndy Whitcroft		    $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
4008171ae1a4SAndy Whitcroft		    $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
4009171ae1a4SAndy Whitcroft		    $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
4010773647a0SAndy Whitcroft		{
4011000d1cc1SJoe Perches			WARN("NR_CPUS",
4012000d1cc1SJoe Perches			     "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
4013773647a0SAndy Whitcroft		}
40149c9ba34eSAndy Whitcroft
40159c9ba34eSAndy Whitcroft# check for %L{u,d,i} in strings
40169c9ba34eSAndy Whitcroft		my $string;
40179c9ba34eSAndy Whitcroft		while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
40189c9ba34eSAndy Whitcroft			$string = substr($rawline, $-[1], $+[1] - $-[1]);
40192a1bc5d5SAndy Whitcroft			$string =~ s/%%/__/g;
40209c9ba34eSAndy Whitcroft			if ($string =~ /(?<!%)%L[udi]/) {
4021000d1cc1SJoe Perches				WARN("PRINTF_L",
4022000d1cc1SJoe Perches				     "\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
40239c9ba34eSAndy Whitcroft				last;
40249c9ba34eSAndy Whitcroft			}
40259c9ba34eSAndy Whitcroft		}
4026691d77b6SAndy Whitcroft
4027691d77b6SAndy Whitcroft# whine mightly about in_atomic
4028691d77b6SAndy Whitcroft		if ($line =~ /\bin_atomic\s*\(/) {
4029691d77b6SAndy Whitcroft			if ($realfile =~ m@^drivers/@) {
4030000d1cc1SJoe Perches				ERROR("IN_ATOMIC",
4031000d1cc1SJoe Perches				      "do not use in_atomic in drivers\n" . $herecurr);
4032f4a87736SAndy Whitcroft			} elsif ($realfile !~ m@^kernel/@) {
4033000d1cc1SJoe Perches				WARN("IN_ATOMIC",
4034000d1cc1SJoe Perches				     "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
4035691d77b6SAndy Whitcroft			}
4036691d77b6SAndy Whitcroft		}
40371704f47bSPeter Zijlstra
40381704f47bSPeter Zijlstra# check for lockdep_set_novalidate_class
40391704f47bSPeter Zijlstra		if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
40401704f47bSPeter Zijlstra		    $line =~ /__lockdep_no_validate__\s*\)/ ) {
40411704f47bSPeter Zijlstra			if ($realfile !~ m@^kernel/lockdep@ &&
40421704f47bSPeter Zijlstra			    $realfile !~ m@^include/linux/lockdep@ &&
40431704f47bSPeter Zijlstra			    $realfile !~ m@^drivers/base/core@) {
4044000d1cc1SJoe Perches				ERROR("LOCKDEP",
4045000d1cc1SJoe Perches				      "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
40461704f47bSPeter Zijlstra			}
40471704f47bSPeter Zijlstra		}
404888f8831cSDave Jones
404988f8831cSDave Jones		if ($line =~ /debugfs_create_file.*S_IWUGO/ ||
405088f8831cSDave Jones		    $line =~ /DEVICE_ATTR.*S_IWUGO/ ) {
4051000d1cc1SJoe Perches			WARN("EXPORTED_WORLD_WRITABLE",
4052000d1cc1SJoe Perches			     "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
405388f8831cSDave Jones		}
405413214adfSAndy Whitcroft	}
405513214adfSAndy Whitcroft
405613214adfSAndy Whitcroft	# If we have no input at all, then there is nothing to report on
405713214adfSAndy Whitcroft	# so just keep quiet.
405813214adfSAndy Whitcroft	if ($#rawlines == -1) {
405913214adfSAndy Whitcroft		exit(0);
40600a920b5bSAndy Whitcroft	}
40610a920b5bSAndy Whitcroft
40628905a67cSAndy Whitcroft	# In mailback mode only produce a report in the negative, for
40638905a67cSAndy Whitcroft	# things that appear to be patches.
40648905a67cSAndy Whitcroft	if ($mailback && ($clean == 1 || !$is_patch)) {
40658905a67cSAndy Whitcroft		exit(0);
40668905a67cSAndy Whitcroft	}
40678905a67cSAndy Whitcroft
40688905a67cSAndy Whitcroft	# This is not a patch, and we are are in 'no-patch' mode so
40698905a67cSAndy Whitcroft	# just keep quiet.
40708905a67cSAndy Whitcroft	if (!$chk_patch && !$is_patch) {
40718905a67cSAndy Whitcroft		exit(0);
40728905a67cSAndy Whitcroft	}
40738905a67cSAndy Whitcroft
40748905a67cSAndy Whitcroft	if (!$is_patch) {
4075000d1cc1SJoe Perches		ERROR("NOT_UNIFIED_DIFF",
4076000d1cc1SJoe Perches		      "Does not appear to be a unified-diff format patch\n");
40770a920b5bSAndy Whitcroft	}
40780a920b5bSAndy Whitcroft	if ($is_patch && $chk_signoff && $signoff == 0) {
4079000d1cc1SJoe Perches		ERROR("MISSING_SIGN_OFF",
4080000d1cc1SJoe Perches		      "Missing Signed-off-by: line(s)\n");
40810a920b5bSAndy Whitcroft	}
40820a920b5bSAndy Whitcroft
4083f0a594c1SAndy Whitcroft	print report_dump();
408413214adfSAndy Whitcroft	if ($summary && !($clean == 1 && $quiet == 1)) {
408513214adfSAndy Whitcroft		print "$filename " if ($summary_file);
40866c72ffaaSAndy Whitcroft		print "total: $cnt_error errors, $cnt_warn warnings, " .
40876c72ffaaSAndy Whitcroft			(($check)? "$cnt_chk checks, " : "") .
40886c72ffaaSAndy Whitcroft			"$cnt_lines lines checked\n";
40898905a67cSAndy Whitcroft		print "\n" if ($quiet == 0);
40906c72ffaaSAndy Whitcroft	}
40918905a67cSAndy Whitcroft
4092d2c0a235SAndy Whitcroft	if ($quiet == 0) {
4093d1fe9c09SJoe Perches
4094d1fe9c09SJoe Perches		if ($^V lt 5.10.0) {
4095d1fe9c09SJoe Perches			print("NOTE: perl $^V is not modern enough to detect all possible issues.\n");
4096d1fe9c09SJoe Perches			print("An upgrade to at least perl v5.10.0 is suggested.\n\n");
4097d1fe9c09SJoe Perches		}
4098d1fe9c09SJoe Perches
4099d2c0a235SAndy Whitcroft		# If there were whitespace errors which cleanpatch can fix
4100d2c0a235SAndy Whitcroft		# then suggest that.
4101d2c0a235SAndy Whitcroft		if ($rpt_cleaners) {
4102d2c0a235SAndy Whitcroft			print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n";
4103d2c0a235SAndy Whitcroft			print "      scripts/cleanfile\n\n";
4104b0781216SMike Frysinger			$rpt_cleaners = 0;
4105d2c0a235SAndy Whitcroft		}
4106d2c0a235SAndy Whitcroft	}
4107d2c0a235SAndy Whitcroft
410811232688SArtem Bityutskiy	if ($quiet == 0 && keys %ignore_type) {
4109000d1cc1SJoe Perches	    print "NOTE: Ignored message types:";
4110000d1cc1SJoe Perches	    foreach my $ignore (sort keys %ignore_type) {
4111000d1cc1SJoe Perches		print " $ignore";
4112000d1cc1SJoe Perches	    }
411311232688SArtem Bityutskiy	    print "\n\n";
4114000d1cc1SJoe Perches	}
4115000d1cc1SJoe Perches
41163705ce5bSJoe Perches	if ($clean == 0 && $fix && "@rawlines" ne "@fixed") {
41173705ce5bSJoe Perches		my $newfile = $filename . ".EXPERIMENTAL-checkpatch-fixes";
41183705ce5bSJoe Perches		my $linecount = 0;
41193705ce5bSJoe Perches		my $f;
41203705ce5bSJoe Perches
41213705ce5bSJoe Perches		open($f, '>', $newfile)
41223705ce5bSJoe Perches		    or die "$P: Can't open $newfile for write\n";
41233705ce5bSJoe Perches		foreach my $fixed_line (@fixed) {
41243705ce5bSJoe Perches			$linecount++;
41253705ce5bSJoe Perches			if ($file) {
41263705ce5bSJoe Perches				if ($linecount > 3) {
41273705ce5bSJoe Perches					$fixed_line =~ s/^\+//;
41283705ce5bSJoe Perches					print $f $fixed_line. "\n";
41293705ce5bSJoe Perches				}
41303705ce5bSJoe Perches			} else {
41313705ce5bSJoe Perches				print $f $fixed_line . "\n";
41323705ce5bSJoe Perches			}
41333705ce5bSJoe Perches		}
41343705ce5bSJoe Perches		close($f);
41353705ce5bSJoe Perches
41363705ce5bSJoe Perches		if (!$quiet) {
41373705ce5bSJoe Perches			print << "EOM";
41383705ce5bSJoe PerchesWrote EXPERIMENTAL --fix correction(s) to '$newfile'
41393705ce5bSJoe Perches
41403705ce5bSJoe PerchesDo _NOT_ trust the results written to this file.
41413705ce5bSJoe PerchesDo _NOT_ submit these changes without inspecting them for correctness.
41423705ce5bSJoe Perches
41433705ce5bSJoe PerchesThis EXPERIMENTAL file is simply a convenience to help rewrite patches.
41443705ce5bSJoe PerchesNo warranties, expressed or implied...
41453705ce5bSJoe Perches
41463705ce5bSJoe PerchesEOM
41473705ce5bSJoe Perches		}
41483705ce5bSJoe Perches	}
41493705ce5bSJoe Perches
41500a920b5bSAndy Whitcroft	if ($clean == 1 && $quiet == 0) {
4151c2fdda0dSAndy Whitcroft		print "$vname has no obvious style problems and is ready for submission.\n"
41520a920b5bSAndy Whitcroft	}
41530a920b5bSAndy Whitcroft	if ($clean == 0 && $quiet == 0) {
4154000d1cc1SJoe Perches		print << "EOM";
4155000d1cc1SJoe Perches$vname has style problems, please review.
4156000d1cc1SJoe Perches
4157000d1cc1SJoe PerchesIf any of these errors are false positives, please report
4158000d1cc1SJoe Perchesthem to the maintainer, see CHECKPATCH in MAINTAINERS.
4159000d1cc1SJoe PerchesEOM
41600a920b5bSAndy Whitcroft	}
416113214adfSAndy Whitcroft
41620a920b5bSAndy Whitcroft	return $clean;
41630a920b5bSAndy Whitcroft}
4164