xref: /linux-6.15/scripts/checkpatch.pl (revision d5e616fc)
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;
9c707a81dSJoe 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;
403c707a81dSJoe Perches	my $camelcase_cache = "";
404c707a81dSJoe Perches	my @include_files = ();
405c707a81dSJoe Perches
406c707a81dSJoe 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;
411c707a81dSJoe Perches		$camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit";
412c707a81dSJoe Perches	} else {
413c707a81dSJoe Perches		my $last_mod_date = 0;
414c707a81dSJoe Perches		$files = `find $root/include -name "*.h"`;
415c707a81dSJoe Perches		@include_files = split('\n', $files);
416c707a81dSJoe Perches		foreach my $file (@include_files) {
417c707a81dSJoe Perches			my $date = POSIX::strftime("%Y%m%d%H%M",
418c707a81dSJoe Perches						   localtime((stat $file)[9]));
419c707a81dSJoe Perches			$last_mod_date = $date if ($last_mod_date < $date);
420c707a81dSJoe Perches		}
421c707a81dSJoe Perches		$camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date";
422c707a81dSJoe Perches	}
423c707a81dSJoe Perches
424c707a81dSJoe Perches	if ($camelcase_cache ne "" && -f $camelcase_cache) {
425c707a81dSJoe Perches		open(my $camelcase_file, '<', "$camelcase_cache")
426c707a81dSJoe 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	}
435c707a81dSJoe Perches
436c707a81dSJoe Perches	if (-d ".git") {
437c707a81dSJoe Perches		$files = `git ls-files "include/*.h"`;
438c707a81dSJoe Perches		@include_files = split('\n', $files);
4393445686aSJoe Perches	}
440c707a81dSJoe Perches
4413445686aSJoe Perches	foreach my $file (@include_files) {
4423445686aSJoe Perches		seed_camelcase_file($file);
4433445686aSJoe Perches	}
444351b2a1fSJoe Perches
445c707a81dSJoe Perches	if ($camelcase_cache ne "") {
446351b2a1fSJoe Perches		unlink glob ".checkpatch-camelcase.*";
447c707a81dSJoe Perches		open(my $camelcase_file, '>', "$camelcase_cache")
448c707a81dSJoe 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";
1848*d5e616fcSJoe Perches			if (ERROR("DOS_LINE_ENDINGS",
1849*d5e616fcSJoe Perches				  "DOS line endings\n" . $herevet) &&
1850*d5e616fcSJoe Perches			    $fix) {
1851*d5e616fcSJoe Perches				$fixed[$linenr - 1] =~ s/[\s\015]+$//;
1852*d5e616fcSJoe Perches			}
1853c2fdda0dSAndy Whitcroft		} elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
1854c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
18553705ce5bSJoe Perches			if (ERROR("TRAILING_WHITESPACE",
18563705ce5bSJoe Perches				  "trailing whitespace\n" . $herevet) &&
18573705ce5bSJoe Perches			    $fix) {
1858*d5e616fcSJoe Perches				$fixed[$linenr - 1] =~ s/\s+$//;
18593705ce5bSJoe Perches			}
18603705ce5bSJoe Perches
1861d2c0a235SAndy Whitcroft			$rpt_cleaners = 1;
18620a920b5bSAndy Whitcroft		}
18635368df20SAndy Whitcroft
18643354957aSAndi Kleen# check for Kconfig help text having a real description
18659fe287d7SAndy Whitcroft# Only applies when adding the entry originally, after that we do not have
18669fe287d7SAndy Whitcroft# sufficient context to determine whether it is indeed long enough.
18673354957aSAndi Kleen		if ($realfile =~ /Kconfig/ &&
1868a1385803SAndy Whitcroft		    $line =~ /.\s*config\s+/) {
18693354957aSAndi Kleen			my $length = 0;
18709fe287d7SAndy Whitcroft			my $cnt = $realcnt;
18719fe287d7SAndy Whitcroft			my $ln = $linenr + 1;
18729fe287d7SAndy Whitcroft			my $f;
1873a1385803SAndy Whitcroft			my $is_start = 0;
18749fe287d7SAndy Whitcroft			my $is_end = 0;
1875a1385803SAndy Whitcroft			for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
18769fe287d7SAndy Whitcroft				$f = $lines[$ln - 1];
18779fe287d7SAndy Whitcroft				$cnt-- if ($lines[$ln - 1] !~ /^-/);
18789fe287d7SAndy Whitcroft				$is_end = $lines[$ln - 1] =~ /^\+/;
18799fe287d7SAndy Whitcroft
18809fe287d7SAndy Whitcroft				next if ($f =~ /^-/);
1881a1385803SAndy Whitcroft
1882a1385803SAndy Whitcroft				if ($lines[$ln - 1] =~ /.\s*(?:bool|tristate)\s*\"/) {
1883a1385803SAndy Whitcroft					$is_start = 1;
1884a1385803SAndy Whitcroft				} elsif ($lines[$ln - 1] =~ /.\s*(?:---)?help(?:---)?$/) {
1885a1385803SAndy Whitcroft					$length = -1;
1886a1385803SAndy Whitcroft				}
1887a1385803SAndy Whitcroft
18889fe287d7SAndy Whitcroft				$f =~ s/^.//;
18893354957aSAndi Kleen				$f =~ s/#.*//;
18903354957aSAndi Kleen				$f =~ s/^\s+//;
18913354957aSAndi Kleen				next if ($f =~ /^$/);
18929fe287d7SAndy Whitcroft				if ($f =~ /^\s*config\s/) {
18939fe287d7SAndy Whitcroft					$is_end = 1;
18949fe287d7SAndy Whitcroft					last;
18959fe287d7SAndy Whitcroft				}
18963354957aSAndi Kleen				$length++;
18973354957aSAndi Kleen			}
1898000d1cc1SJoe Perches			WARN("CONFIG_DESCRIPTION",
1899a1385803SAndy Whitcroft			     "please write a paragraph that describes the config symbol fully\n" . $herecurr) if ($is_start && $is_end && $length < 4);
1900a1385803SAndy Whitcroft			#print "is_start<$is_start> is_end<$is_end> length<$length>\n";
19013354957aSAndi Kleen		}
19023354957aSAndi Kleen
19031ba8dfd1SKees Cook# discourage the addition of CONFIG_EXPERIMENTAL in Kconfig.
19041ba8dfd1SKees Cook		if ($realfile =~ /Kconfig/ &&
19051ba8dfd1SKees Cook		    $line =~ /.\s*depends on\s+.*\bEXPERIMENTAL\b/) {
19061ba8dfd1SKees Cook			WARN("CONFIG_EXPERIMENTAL",
19071ba8dfd1SKees Cook			     "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
19081ba8dfd1SKees Cook		}
19091ba8dfd1SKees Cook
1910c68e5878SArnaud Lacombe		if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
1911c68e5878SArnaud Lacombe		    ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
1912c68e5878SArnaud Lacombe			my $flag = $1;
1913c68e5878SArnaud Lacombe			my $replacement = {
1914c68e5878SArnaud Lacombe				'EXTRA_AFLAGS' =>   'asflags-y',
1915c68e5878SArnaud Lacombe				'EXTRA_CFLAGS' =>   'ccflags-y',
1916c68e5878SArnaud Lacombe				'EXTRA_CPPFLAGS' => 'cppflags-y',
1917c68e5878SArnaud Lacombe				'EXTRA_LDFLAGS' =>  'ldflags-y',
1918c68e5878SArnaud Lacombe			};
1919c68e5878SArnaud Lacombe
1920c68e5878SArnaud Lacombe			WARN("DEPRECATED_VARIABLE",
1921c68e5878SArnaud Lacombe			     "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
1922c68e5878SArnaud Lacombe		}
1923c68e5878SArnaud Lacombe
19245368df20SAndy Whitcroft# check we are in a valid source file if not then ignore this hunk
19255368df20SAndy Whitcroft		next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
19265368df20SAndy Whitcroft
19276cd7f386SJoe Perches#line length limit
1928c45dcabdSAndy Whitcroft		if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ &&
1929f4c014c0SAndy Whitcroft		    $rawline !~ /^.\s*\*\s*\@$Ident\s/ &&
19300fccc622SJoe Perches		    !($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(KERN_\S+\s*|[^"]*))?"[X\t]*"\s*(?:|,|\)\s*;)\s*$/ ||
19318bbea968SJoe Perches		    $line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) &&
19326cd7f386SJoe Perches		    $length > $max_line_length)
1933c45dcabdSAndy Whitcroft		{
1934000d1cc1SJoe Perches			WARN("LONG_LINE",
19356cd7f386SJoe Perches			     "line over $max_line_length characters\n" . $herecurr);
19360a920b5bSAndy Whitcroft		}
19370a920b5bSAndy Whitcroft
1938ca56dc09SJosh Triplett# Check for user-visible strings broken across lines, which breaks the ability
1939ca56dc09SJosh Triplett# to grep for the string.  Limited to strings used as parameters (those
1940ca56dc09SJosh Triplett# following an open parenthesis), which almost completely eliminates false
1941ca56dc09SJosh Triplett# positives, as well as warning only once per parameter rather than once per
1942ca56dc09SJosh Triplett# line of the string.  Make an exception when the previous string ends in a
1943ca56dc09SJosh Triplett# newline (multiple lines in one string constant) or \n\t (common in inline
1944ca56dc09SJosh Triplett# assembly to indent the instruction on the following line).
1945ca56dc09SJosh Triplett		if ($line =~ /^\+\s*"/ &&
1946ca56dc09SJosh Triplett		    $prevline =~ /"\s*$/ &&
1947ca56dc09SJosh Triplett		    $prevline =~ /\(/ &&
1948ca56dc09SJosh Triplett		    $prevrawline !~ /\\n(?:\\t)*"\s*$/) {
1949ca56dc09SJosh Triplett			WARN("SPLIT_STRING",
1950ca56dc09SJosh Triplett			     "quoted string split across lines\n" . $hereprev);
1951ca56dc09SJosh Triplett		}
1952ca56dc09SJosh Triplett
19535e79d96eSJoe Perches# check for spaces before a quoted newline
19545e79d96eSJoe Perches		if ($rawline =~ /^.*\".*\s\\n/) {
19553705ce5bSJoe Perches			if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
19563705ce5bSJoe Perches				 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
19573705ce5bSJoe Perches			    $fix) {
19583705ce5bSJoe Perches				$fixed[$linenr - 1] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
19593705ce5bSJoe Perches			}
19603705ce5bSJoe Perches
19615e79d96eSJoe Perches		}
19625e79d96eSJoe Perches
19638905a67cSAndy Whitcroft# check for adding lines without a newline.
19648905a67cSAndy Whitcroft		if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
1965000d1cc1SJoe Perches			WARN("MISSING_EOF_NEWLINE",
1966000d1cc1SJoe Perches			     "adding a line without newline at end of file\n" . $herecurr);
19678905a67cSAndy Whitcroft		}
19688905a67cSAndy Whitcroft
196942e41c54SMike Frysinger# Blackfin: use hi/lo macros
197042e41c54SMike Frysinger		if ($realfile =~ m@arch/blackfin/.*\.S$@) {
197142e41c54SMike Frysinger			if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
197242e41c54SMike Frysinger				my $herevet = "$here\n" . cat_vet($line) . "\n";
1973000d1cc1SJoe Perches				ERROR("LO_MACRO",
1974000d1cc1SJoe Perches				      "use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
197542e41c54SMike Frysinger			}
197642e41c54SMike Frysinger			if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
197742e41c54SMike Frysinger				my $herevet = "$here\n" . cat_vet($line) . "\n";
1978000d1cc1SJoe Perches				ERROR("HI_MACRO",
1979000d1cc1SJoe Perches				      "use the HI() macro, not (... >> 16)\n" . $herevet);
198042e41c54SMike Frysinger			}
198142e41c54SMike Frysinger		}
198242e41c54SMike Frysinger
1983b9ea10d6SAndy Whitcroft# check we are in a valid source file C or perl if not then ignore this hunk
1984b9ea10d6SAndy Whitcroft		next if ($realfile !~ /\.(h|c|pl)$/);
19850a920b5bSAndy Whitcroft
19860a920b5bSAndy Whitcroft# at the beginning of a line any tabs must come first and anything
19870a920b5bSAndy Whitcroft# more than 8 must use tabs.
1988c2fdda0dSAndy Whitcroft		if ($rawline =~ /^\+\s* \t\s*\S/ ||
1989c2fdda0dSAndy Whitcroft		    $rawline =~ /^\+\s*        \s*/) {
1990c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1991d2c0a235SAndy Whitcroft			$rpt_cleaners = 1;
19923705ce5bSJoe Perches			if (ERROR("CODE_INDENT",
19933705ce5bSJoe Perches				  "code indent should use tabs where possible\n" . $herevet) &&
19943705ce5bSJoe Perches			    $fix) {
19953705ce5bSJoe Perches				$fixed[$linenr - 1] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
19963705ce5bSJoe Perches			}
19970a920b5bSAndy Whitcroft		}
19980a920b5bSAndy Whitcroft
199908e44365SAlberto Panizzo# check for space before tabs.
200008e44365SAlberto Panizzo		if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
200108e44365SAlberto Panizzo			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
20023705ce5bSJoe Perches			if (WARN("SPACE_BEFORE_TAB",
20033705ce5bSJoe Perches				"please, no space before tabs\n" . $herevet) &&
20043705ce5bSJoe Perches			    $fix) {
20053705ce5bSJoe Perches				$fixed[$linenr - 1] =~
20063705ce5bSJoe Perches				    s/(^\+.*) +\t/$1\t/;
20073705ce5bSJoe Perches			}
200808e44365SAlberto Panizzo		}
200908e44365SAlberto Panizzo
2010d1fe9c09SJoe Perches# check for && or || at the start of a line
2011d1fe9c09SJoe Perches		if ($rawline =~ /^\+\s*(&&|\|\|)/) {
2012d1fe9c09SJoe Perches			CHK("LOGICAL_CONTINUATIONS",
2013d1fe9c09SJoe Perches			    "Logical continuations should be on the previous line\n" . $hereprev);
2014d1fe9c09SJoe Perches		}
2015d1fe9c09SJoe Perches
2016d1fe9c09SJoe Perches# check multi-line statement indentation matches previous line
2017d1fe9c09SJoe Perches		if ($^V && $^V ge 5.10.0 &&
2018d1fe9c09SJoe Perches		    $prevline =~ /^\+(\t*)(if \(|$Ident\().*(\&\&|\|\||,)\s*$/) {
2019d1fe9c09SJoe Perches			$prevline =~ /^\+(\t*)(.*)$/;
2020d1fe9c09SJoe Perches			my $oldindent = $1;
2021d1fe9c09SJoe Perches			my $rest = $2;
2022d1fe9c09SJoe Perches
2023d1fe9c09SJoe Perches			my $pos = pos_last_openparen($rest);
2024d1fe9c09SJoe Perches			if ($pos >= 0) {
2025b34a26f3SJoe Perches				$line =~ /^(\+| )([ \t]*)/;
2026b34a26f3SJoe Perches				my $newindent = $2;
2027d1fe9c09SJoe Perches
2028d1fe9c09SJoe Perches				my $goodtabindent = $oldindent .
2029d1fe9c09SJoe Perches					"\t" x ($pos / 8) .
2030d1fe9c09SJoe Perches					" "  x ($pos % 8);
2031d1fe9c09SJoe Perches				my $goodspaceindent = $oldindent . " "  x $pos;
2032d1fe9c09SJoe Perches
2033d1fe9c09SJoe Perches				if ($newindent ne $goodtabindent &&
2034d1fe9c09SJoe Perches				    $newindent ne $goodspaceindent) {
20353705ce5bSJoe Perches
20363705ce5bSJoe Perches					if (CHK("PARENTHESIS_ALIGNMENT",
20373705ce5bSJoe Perches						"Alignment should match open parenthesis\n" . $hereprev) &&
20383705ce5bSJoe Perches					    $fix && $line =~ /^\+/) {
20393705ce5bSJoe Perches						$fixed[$linenr - 1] =~
20403705ce5bSJoe Perches						    s/^\+[ \t]*/\+$goodtabindent/;
20413705ce5bSJoe Perches					}
2042d1fe9c09SJoe Perches				}
2043d1fe9c09SJoe Perches			}
2044d1fe9c09SJoe Perches		}
2045d1fe9c09SJoe Perches
204623f780c9SJoe Perches		if ($line =~ /^\+.*\*[ \t]*\)[ \t]+(?!$Assignment|$Arithmetic)/) {
20473705ce5bSJoe Perches			if (CHK("SPACING",
20483705ce5bSJoe Perches				"No space is necessary after a cast\n" . $hereprev) &&
20493705ce5bSJoe Perches			    $fix) {
20503705ce5bSJoe Perches				$fixed[$linenr - 1] =~
20513705ce5bSJoe Perches				    s/^(\+.*\*[ \t]*\))[ \t]+/$1/;
20523705ce5bSJoe Perches			}
2053aad4f614SJoe Perches		}
2054aad4f614SJoe Perches
205505880600SJoe Perches		if ($realfile =~ m@^(drivers/net/|net/)@ &&
2056fdb4bcd6SJoe Perches		    $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
2057fdb4bcd6SJoe Perches		    $rawline =~ /^\+[ \t]*\*/) {
205805880600SJoe Perches			WARN("NETWORKING_BLOCK_COMMENT_STYLE",
205905880600SJoe Perches			     "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
206005880600SJoe Perches		}
206105880600SJoe Perches
206205880600SJoe Perches		if ($realfile =~ m@^(drivers/net/|net/)@ &&
2063a605e32eSJoe Perches		    $prevrawline =~ /^\+[ \t]*\/\*/ &&		#starting /*
2064a605e32eSJoe Perches		    $prevrawline !~ /\*\/[ \t]*$/ &&		#no trailing */
2065a605e32eSJoe Perches		    $rawline !~ /^\+[ \t]*\*/) {		#no leading *
2066a605e32eSJoe Perches			WARN("NETWORKING_BLOCK_COMMENT_STYLE",
2067a605e32eSJoe Perches			     "networking block comments start with * on subsequent lines\n" . $hereprev);
2068a605e32eSJoe Perches		}
2069a605e32eSJoe Perches
2070a605e32eSJoe Perches		if ($realfile =~ m@^(drivers/net/|net/)@ &&
2071c24f9f19SJoe Perches		    $rawline !~ m@^\+[ \t]*\*/[ \t]*$@ &&	#trailing */
2072c24f9f19SJoe Perches		    $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ &&	#inline /*...*/
2073c24f9f19SJoe Perches		    $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ &&	#trailing **/
2074c24f9f19SJoe Perches		    $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) {	#non blank */
207505880600SJoe Perches			WARN("NETWORKING_BLOCK_COMMENT_STYLE",
207605880600SJoe Perches			     "networking block comments put the trailing */ on a separate line\n" . $herecurr);
207705880600SJoe Perches		}
207805880600SJoe Perches
20795f7ddae6SRaffaele Recalcati# check for spaces at the beginning of a line.
20806b4c5bebSAndy Whitcroft# Exceptions:
20816b4c5bebSAndy Whitcroft#  1) within comments
20826b4c5bebSAndy Whitcroft#  2) indented preprocessor commands
20836b4c5bebSAndy Whitcroft#  3) hanging labels
20843705ce5bSJoe Perches		if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/)  {
20855f7ddae6SRaffaele Recalcati			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
20863705ce5bSJoe Perches			if (WARN("LEADING_SPACE",
20873705ce5bSJoe Perches				 "please, no spaces at the start of a line\n" . $herevet) &&
20883705ce5bSJoe Perches			    $fix) {
20893705ce5bSJoe Perches				$fixed[$linenr - 1] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
20903705ce5bSJoe Perches			}
20915f7ddae6SRaffaele Recalcati		}
20925f7ddae6SRaffaele Recalcati
2093b9ea10d6SAndy Whitcroft# check we are in a valid C source file if not then ignore this hunk
2094b9ea10d6SAndy Whitcroft		next if ($realfile !~ /\.(h|c)$/);
2095b9ea10d6SAndy Whitcroft
20961ba8dfd1SKees Cook# discourage the addition of CONFIG_EXPERIMENTAL in #if(def).
20971ba8dfd1SKees Cook		if ($line =~ /^\+\s*\#\s*if.*\bCONFIG_EXPERIMENTAL\b/) {
20981ba8dfd1SKees Cook			WARN("CONFIG_EXPERIMENTAL",
20991ba8dfd1SKees Cook			     "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
21001ba8dfd1SKees Cook		}
21011ba8dfd1SKees Cook
2102c2fdda0dSAndy Whitcroft# check for RCS/CVS revision markers
2103cf655043SAndy Whitcroft		if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
2104000d1cc1SJoe Perches			WARN("CVS_KEYWORD",
2105000d1cc1SJoe Perches			     "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
2106c2fdda0dSAndy Whitcroft		}
210722f2a2efSAndy Whitcroft
210842e41c54SMike Frysinger# Blackfin: don't use __builtin_bfin_[cs]sync
210942e41c54SMike Frysinger		if ($line =~ /__builtin_bfin_csync/) {
211042e41c54SMike Frysinger			my $herevet = "$here\n" . cat_vet($line) . "\n";
2111000d1cc1SJoe Perches			ERROR("CSYNC",
2112000d1cc1SJoe Perches			      "use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
211342e41c54SMike Frysinger		}
211442e41c54SMike Frysinger		if ($line =~ /__builtin_bfin_ssync/) {
211542e41c54SMike Frysinger			my $herevet = "$here\n" . cat_vet($line) . "\n";
2116000d1cc1SJoe Perches			ERROR("SSYNC",
2117000d1cc1SJoe Perches			      "use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
211842e41c54SMike Frysinger		}
211942e41c54SMike Frysinger
212056e77d70SJoe Perches# check for old HOTPLUG __dev<foo> section markings
212156e77d70SJoe Perches		if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
212256e77d70SJoe Perches			WARN("HOTPLUG_SECTION",
212356e77d70SJoe Perches			     "Using $1 is unnecessary\n" . $herecurr);
212456e77d70SJoe Perches		}
212556e77d70SJoe Perches
21269c0ca6f9SAndy Whitcroft# Check for potential 'bare' types
21272b474a1aSAndy Whitcroft		my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
21282b474a1aSAndy Whitcroft		    $realline_next);
21293e469cdcSAndy Whitcroft#print "LINE<$line>\n";
21303e469cdcSAndy Whitcroft		if ($linenr >= $suppress_statement &&
21313e469cdcSAndy Whitcroft		    $realcnt && $line =~ /.\s*\S/) {
2132170d3a22SAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
2133f5fe35ddSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0);
2134171ae1a4SAndy Whitcroft			$stat =~ s/\n./\n /g;
2135171ae1a4SAndy Whitcroft			$cond =~ s/\n./\n /g;
2136171ae1a4SAndy Whitcroft
21373e469cdcSAndy Whitcroft#print "linenr<$linenr> <$stat>\n";
21383e469cdcSAndy Whitcroft			# If this statement has no statement boundaries within
21393e469cdcSAndy Whitcroft			# it there is no point in retrying a statement scan
21403e469cdcSAndy Whitcroft			# until we hit end of it.
21413e469cdcSAndy Whitcroft			my $frag = $stat; $frag =~ s/;+\s*$//;
21423e469cdcSAndy Whitcroft			if ($frag !~ /(?:{|;)/) {
21433e469cdcSAndy Whitcroft#print "skip<$line_nr_next>\n";
21443e469cdcSAndy Whitcroft				$suppress_statement = $line_nr_next;
21453e469cdcSAndy Whitcroft			}
2146f74bd194SAndy Whitcroft
21472b474a1aSAndy Whitcroft			# Find the real next line.
21482b474a1aSAndy Whitcroft			$realline_next = $line_nr_next;
21492b474a1aSAndy Whitcroft			if (defined $realline_next &&
21502b474a1aSAndy Whitcroft			    (!defined $lines[$realline_next - 1] ||
21512b474a1aSAndy Whitcroft			     substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
21522b474a1aSAndy Whitcroft				$realline_next++;
21532b474a1aSAndy Whitcroft			}
21542b474a1aSAndy Whitcroft
2155171ae1a4SAndy Whitcroft			my $s = $stat;
2156171ae1a4SAndy Whitcroft			$s =~ s/{.*$//s;
2157cf655043SAndy Whitcroft
2158c2fdda0dSAndy Whitcroft			# Ignore goto labels.
2159171ae1a4SAndy Whitcroft			if ($s =~ /$Ident:\*$/s) {
2160c2fdda0dSAndy Whitcroft
2161c2fdda0dSAndy Whitcroft			# Ignore functions being called
2162171ae1a4SAndy Whitcroft			} elsif ($s =~ /^.\s*$Ident\s*\(/s) {
2163c2fdda0dSAndy Whitcroft
2164463f2864SAndy Whitcroft			} elsif ($s =~ /^.\s*else\b/s) {
2165463f2864SAndy Whitcroft
2166c45dcabdSAndy Whitcroft			# declarations always start with types
2167d2506586SAndy 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) {
2168c45dcabdSAndy Whitcroft				my $type = $1;
2169c45dcabdSAndy Whitcroft				$type =~ s/\s+/ /g;
2170c45dcabdSAndy Whitcroft				possible($type, "A:" . $s);
2171c45dcabdSAndy Whitcroft
21726c72ffaaSAndy Whitcroft			# definitions in global scope can only start with types
2173a6a84062SAndy Whitcroft			} elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
2174c45dcabdSAndy Whitcroft				possible($1, "B:" . $s);
2175c2fdda0dSAndy Whitcroft			}
21768905a67cSAndy Whitcroft
21776c72ffaaSAndy Whitcroft			# any (foo ... *) is a pointer cast, and foo is a type
217865863862SAndy Whitcroft			while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
2179c45dcabdSAndy Whitcroft				possible($1, "C:" . $s);
21809c0ca6f9SAndy Whitcroft			}
21818905a67cSAndy Whitcroft
21828905a67cSAndy Whitcroft			# Check for any sort of function declaration.
21838905a67cSAndy Whitcroft			# int foo(something bar, other baz);
21848905a67cSAndy Whitcroft			# void (*store_gdt)(x86_descr_ptr *);
2185171ae1a4SAndy Whitcroft			if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
21868905a67cSAndy Whitcroft				my ($name_len) = length($1);
21878905a67cSAndy Whitcroft
2188cf655043SAndy Whitcroft				my $ctx = $s;
2189773647a0SAndy Whitcroft				substr($ctx, 0, $name_len + 1, '');
21908905a67cSAndy Whitcroft				$ctx =~ s/\)[^\)]*$//;
2191cf655043SAndy Whitcroft
21928905a67cSAndy Whitcroft				for my $arg (split(/\s*,\s*/, $ctx)) {
2193c45dcabdSAndy Whitcroft					if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
21948905a67cSAndy Whitcroft
2195c45dcabdSAndy Whitcroft						possible($1, "D:" . $s);
21968905a67cSAndy Whitcroft					}
21978905a67cSAndy Whitcroft				}
21988905a67cSAndy Whitcroft			}
21998905a67cSAndy Whitcroft
22009c0ca6f9SAndy Whitcroft		}
22019c0ca6f9SAndy Whitcroft
220200df344fSAndy Whitcroft#
220300df344fSAndy Whitcroft# Checks which may be anchored in the context.
220400df344fSAndy Whitcroft#
220500df344fSAndy Whitcroft
220600df344fSAndy Whitcroft# Check for switch () and associated case and default
220700df344fSAndy Whitcroft# statements should be at the same indent.
220800df344fSAndy Whitcroft		if ($line=~/\bswitch\s*\(.*\)/) {
220900df344fSAndy Whitcroft			my $err = '';
221000df344fSAndy Whitcroft			my $sep = '';
221100df344fSAndy Whitcroft			my @ctx = ctx_block_outer($linenr, $realcnt);
221200df344fSAndy Whitcroft			shift(@ctx);
221300df344fSAndy Whitcroft			for my $ctx (@ctx) {
221400df344fSAndy Whitcroft				my ($clen, $cindent) = line_stats($ctx);
221500df344fSAndy Whitcroft				if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
221600df344fSAndy Whitcroft							$indent != $cindent) {
221700df344fSAndy Whitcroft					$err .= "$sep$ctx\n";
221800df344fSAndy Whitcroft					$sep = '';
221900df344fSAndy Whitcroft				} else {
222000df344fSAndy Whitcroft					$sep = "[...]\n";
222100df344fSAndy Whitcroft				}
222200df344fSAndy Whitcroft			}
222300df344fSAndy Whitcroft			if ($err ne '') {
2224000d1cc1SJoe Perches				ERROR("SWITCH_CASE_INDENT_LEVEL",
2225000d1cc1SJoe Perches				      "switch and case should be at the same indent\n$hereline$err");
2226de7d4f0eSAndy Whitcroft			}
2227de7d4f0eSAndy Whitcroft		}
2228de7d4f0eSAndy Whitcroft
2229de7d4f0eSAndy Whitcroft# if/while/etc brace do not go on next line, unless defining a do while loop,
2230de7d4f0eSAndy Whitcroft# or if that brace on the next line is for something else
2231c45dcabdSAndy Whitcroft		if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
2232773647a0SAndy Whitcroft			my $pre_ctx = "$1$2";
2233773647a0SAndy Whitcroft
22349c0ca6f9SAndy Whitcroft			my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
22358eef05ddSJoe Perches
22368eef05ddSJoe Perches			if ($line =~ /^\+\t{6,}/) {
22378eef05ddSJoe Perches				WARN("DEEP_INDENTATION",
22388eef05ddSJoe Perches				     "Too many leading tabs - consider code refactoring\n" . $herecurr);
22398eef05ddSJoe Perches			}
22408eef05ddSJoe Perches
2241de7d4f0eSAndy Whitcroft			my $ctx_cnt = $realcnt - $#ctx - 1;
2242de7d4f0eSAndy Whitcroft			my $ctx = join("\n", @ctx);
2243de7d4f0eSAndy Whitcroft
2244548596d5SAndy Whitcroft			my $ctx_ln = $linenr;
2245548596d5SAndy Whitcroft			my $ctx_skip = $realcnt;
2246de7d4f0eSAndy Whitcroft
2247548596d5SAndy Whitcroft			while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
2248548596d5SAndy Whitcroft					defined $lines[$ctx_ln - 1] &&
2249548596d5SAndy Whitcroft					$lines[$ctx_ln - 1] =~ /^-/)) {
2250548596d5SAndy Whitcroft				##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
2251548596d5SAndy Whitcroft				$ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
2252773647a0SAndy Whitcroft				$ctx_ln++;
2253773647a0SAndy Whitcroft			}
2254548596d5SAndy Whitcroft
225553210168SAndy Whitcroft			#print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
225653210168SAndy Whitcroft			#print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
2257773647a0SAndy Whitcroft
2258773647a0SAndy Whitcroft			if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln -1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
2259000d1cc1SJoe Perches				ERROR("OPEN_BRACE",
2260000d1cc1SJoe Perches				      "that open brace { should be on the previous line\n" .
226101464f30SAndy Whitcroft					"$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
226200df344fSAndy Whitcroft			}
2263773647a0SAndy Whitcroft			if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
2264773647a0SAndy Whitcroft			    $ctx =~ /\)\s*\;\s*$/ &&
2265773647a0SAndy Whitcroft			    defined $lines[$ctx_ln - 1])
2266773647a0SAndy Whitcroft			{
22679c0ca6f9SAndy Whitcroft				my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
22689c0ca6f9SAndy Whitcroft				if ($nindent > $indent) {
2269000d1cc1SJoe Perches					WARN("TRAILING_SEMICOLON",
2270000d1cc1SJoe Perches					     "trailing semicolon indicates no statements, indent implies otherwise\n" .
227101464f30SAndy Whitcroft						"$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
22729c0ca6f9SAndy Whitcroft				}
22739c0ca6f9SAndy Whitcroft			}
227400df344fSAndy Whitcroft		}
227500df344fSAndy Whitcroft
22764d001e4dSAndy Whitcroft# Check relative indent for conditionals and blocks.
22774d001e4dSAndy Whitcroft		if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
22783e469cdcSAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
22793e469cdcSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0)
22803e469cdcSAndy Whitcroft					if (!defined $stat);
22814d001e4dSAndy Whitcroft			my ($s, $c) = ($stat, $cond);
22824d001e4dSAndy Whitcroft
22834d001e4dSAndy Whitcroft			substr($s, 0, length($c), '');
22844d001e4dSAndy Whitcroft
22854d001e4dSAndy Whitcroft			# Make sure we remove the line prefixes as we have
22864d001e4dSAndy Whitcroft			# none on the first line, and are going to readd them
22874d001e4dSAndy Whitcroft			# where necessary.
22884d001e4dSAndy Whitcroft			$s =~ s/\n./\n/gs;
22894d001e4dSAndy Whitcroft
22904d001e4dSAndy Whitcroft			# Find out how long the conditional actually is.
22916f779c18SAndy Whitcroft			my @newlines = ($c =~ /\n/gs);
22926f779c18SAndy Whitcroft			my $cond_lines = 1 + $#newlines;
22934d001e4dSAndy Whitcroft
22944d001e4dSAndy Whitcroft			# We want to check the first line inside the block
22954d001e4dSAndy Whitcroft			# starting at the end of the conditional, so remove:
22964d001e4dSAndy Whitcroft			#  1) any blank line termination
22974d001e4dSAndy Whitcroft			#  2) any opening brace { on end of the line
22984d001e4dSAndy Whitcroft			#  3) any do (...) {
22994d001e4dSAndy Whitcroft			my $continuation = 0;
23004d001e4dSAndy Whitcroft			my $check = 0;
23014d001e4dSAndy Whitcroft			$s =~ s/^.*\bdo\b//;
23024d001e4dSAndy Whitcroft			$s =~ s/^\s*{//;
23034d001e4dSAndy Whitcroft			if ($s =~ s/^\s*\\//) {
23044d001e4dSAndy Whitcroft				$continuation = 1;
23054d001e4dSAndy Whitcroft			}
23069bd49efeSAndy Whitcroft			if ($s =~ s/^\s*?\n//) {
23074d001e4dSAndy Whitcroft				$check = 1;
23084d001e4dSAndy Whitcroft				$cond_lines++;
23094d001e4dSAndy Whitcroft			}
23104d001e4dSAndy Whitcroft
23114d001e4dSAndy Whitcroft			# Also ignore a loop construct at the end of a
23124d001e4dSAndy Whitcroft			# preprocessor statement.
23134d001e4dSAndy Whitcroft			if (($prevline =~ /^.\s*#\s*define\s/ ||
23144d001e4dSAndy Whitcroft			    $prevline =~ /\\\s*$/) && $continuation == 0) {
23154d001e4dSAndy Whitcroft				$check = 0;
23164d001e4dSAndy Whitcroft			}
23174d001e4dSAndy Whitcroft
23189bd49efeSAndy Whitcroft			my $cond_ptr = -1;
2319740504c6SAndy Whitcroft			$continuation = 0;
23209bd49efeSAndy Whitcroft			while ($cond_ptr != $cond_lines) {
23219bd49efeSAndy Whitcroft				$cond_ptr = $cond_lines;
23224d001e4dSAndy Whitcroft
2323f16fa28fSAndy Whitcroft				# If we see an #else/#elif then the code
2324f16fa28fSAndy Whitcroft				# is not linear.
2325f16fa28fSAndy Whitcroft				if ($s =~ /^\s*\#\s*(?:else|elif)/) {
2326f16fa28fSAndy Whitcroft					$check = 0;
2327f16fa28fSAndy Whitcroft				}
2328f16fa28fSAndy Whitcroft
23299bd49efeSAndy Whitcroft				# Ignore:
23309bd49efeSAndy Whitcroft				#  1) blank lines, they should be at 0,
23319bd49efeSAndy Whitcroft				#  2) preprocessor lines, and
23329bd49efeSAndy Whitcroft				#  3) labels.
2333740504c6SAndy Whitcroft				if ($continuation ||
2334740504c6SAndy Whitcroft				    $s =~ /^\s*?\n/ ||
23359bd49efeSAndy Whitcroft				    $s =~ /^\s*#\s*?/ ||
23369bd49efeSAndy Whitcroft				    $s =~ /^\s*$Ident\s*:/) {
2337740504c6SAndy Whitcroft					$continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
233830dad6ebSAndy Whitcroft					if ($s =~ s/^.*?\n//) {
23399bd49efeSAndy Whitcroft						$cond_lines++;
23409bd49efeSAndy Whitcroft					}
23414d001e4dSAndy Whitcroft				}
234230dad6ebSAndy Whitcroft			}
23434d001e4dSAndy Whitcroft
23444d001e4dSAndy Whitcroft			my (undef, $sindent) = line_stats("+" . $s);
23454d001e4dSAndy Whitcroft			my $stat_real = raw_line($linenr, $cond_lines);
23464d001e4dSAndy Whitcroft
23474d001e4dSAndy Whitcroft			# Check if either of these lines are modified, else
23484d001e4dSAndy Whitcroft			# this is not this patch's fault.
23494d001e4dSAndy Whitcroft			if (!defined($stat_real) ||
23504d001e4dSAndy Whitcroft			    $stat !~ /^\+/ && $stat_real !~ /^\+/) {
23514d001e4dSAndy Whitcroft				$check = 0;
23524d001e4dSAndy Whitcroft			}
23534d001e4dSAndy Whitcroft			if (defined($stat_real) && $cond_lines > 1) {
23544d001e4dSAndy Whitcroft				$stat_real = "[...]\n$stat_real";
23554d001e4dSAndy Whitcroft			}
23564d001e4dSAndy Whitcroft
23579bd49efeSAndy 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";
23584d001e4dSAndy Whitcroft
23594d001e4dSAndy Whitcroft			if ($check && (($sindent % 8) != 0 ||
23604d001e4dSAndy Whitcroft			    ($sindent <= $indent && $s ne ''))) {
2361000d1cc1SJoe Perches				WARN("SUSPECT_CODE_INDENT",
2362000d1cc1SJoe Perches				     "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
23634d001e4dSAndy Whitcroft			}
23644d001e4dSAndy Whitcroft		}
23654d001e4dSAndy Whitcroft
23666c72ffaaSAndy Whitcroft		# Track the 'values' across context and added lines.
23676c72ffaaSAndy Whitcroft		my $opline = $line; $opline =~ s/^./ /;
23681f65f947SAndy Whitcroft		my ($curr_values, $curr_vars) =
23691f65f947SAndy Whitcroft				annotate_values($opline . "\n", $prev_values);
23706c72ffaaSAndy Whitcroft		$curr_values = $prev_values . $curr_values;
2371c2fdda0dSAndy Whitcroft		if ($dbg_values) {
2372c2fdda0dSAndy Whitcroft			my $outline = $opline; $outline =~ s/\t/ /g;
2373cf655043SAndy Whitcroft			print "$linenr > .$outline\n";
2374cf655043SAndy Whitcroft			print "$linenr > $curr_values\n";
23751f65f947SAndy Whitcroft			print "$linenr >  $curr_vars\n";
2376c2fdda0dSAndy Whitcroft		}
23776c72ffaaSAndy Whitcroft		$prev_values = substr($curr_values, -1);
23786c72ffaaSAndy Whitcroft
237900df344fSAndy Whitcroft#ignore lines not being added
23803705ce5bSJoe Perches		next if ($line =~ /^[^\+]/);
238100df344fSAndy Whitcroft
2382653d4876SAndy Whitcroft# TEST: allow direct testing of the type matcher.
23837429c690SAndy Whitcroft		if ($dbg_type) {
23847429c690SAndy Whitcroft			if ($line =~ /^.\s*$Declare\s*$/) {
2385000d1cc1SJoe Perches				ERROR("TEST_TYPE",
2386000d1cc1SJoe Perches				      "TEST: is type\n" . $herecurr);
23877429c690SAndy Whitcroft			} elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
2388000d1cc1SJoe Perches				ERROR("TEST_NOT_TYPE",
2389000d1cc1SJoe Perches				      "TEST: is not type ($1 is)\n". $herecurr);
23907429c690SAndy Whitcroft			}
2391653d4876SAndy Whitcroft			next;
2392653d4876SAndy Whitcroft		}
2393a1ef277eSAndy Whitcroft# TEST: allow direct testing of the attribute matcher.
2394a1ef277eSAndy Whitcroft		if ($dbg_attr) {
23959360b0e5SAndy Whitcroft			if ($line =~ /^.\s*$Modifier\s*$/) {
2396000d1cc1SJoe Perches				ERROR("TEST_ATTR",
2397000d1cc1SJoe Perches				      "TEST: is attr\n" . $herecurr);
23989360b0e5SAndy Whitcroft			} elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
2399000d1cc1SJoe Perches				ERROR("TEST_NOT_ATTR",
2400000d1cc1SJoe Perches				      "TEST: is not attr ($1 is)\n". $herecurr);
2401a1ef277eSAndy Whitcroft			}
2402a1ef277eSAndy Whitcroft			next;
2403a1ef277eSAndy Whitcroft		}
2404653d4876SAndy Whitcroft
2405f0a594c1SAndy Whitcroft# check for initialisation to aggregates open brace on the next line
240699423c20SAndy Whitcroft		if ($line =~ /^.\s*{/ &&
240799423c20SAndy Whitcroft		    $prevline =~ /(?:^|[^=])=\s*$/) {
2408000d1cc1SJoe Perches			ERROR("OPEN_BRACE",
2409000d1cc1SJoe Perches			      "that open brace { should be on the previous line\n" . $hereprev);
2410f0a594c1SAndy Whitcroft		}
2411f0a594c1SAndy Whitcroft
241200df344fSAndy Whitcroft#
241300df344fSAndy Whitcroft# Checks which are anchored on the added line.
241400df344fSAndy Whitcroft#
241500df344fSAndy Whitcroft
2416653d4876SAndy Whitcroft# check for malformed paths in #include statements (uses RAW line)
2417c45dcabdSAndy Whitcroft		if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
2418653d4876SAndy Whitcroft			my $path = $1;
2419653d4876SAndy Whitcroft			if ($path =~ m{//}) {
2420000d1cc1SJoe Perches				ERROR("MALFORMED_INCLUDE",
2421495e9d84SJoe Perches				      "malformed #include filename\n" . $herecurr);
2422495e9d84SJoe Perches			}
2423495e9d84SJoe Perches			if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
2424495e9d84SJoe Perches				ERROR("UAPI_INCLUDE",
2425495e9d84SJoe Perches				      "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
2426653d4876SAndy Whitcroft			}
2427653d4876SAndy Whitcroft		}
2428653d4876SAndy Whitcroft
242900df344fSAndy Whitcroft# no C99 // comments
243000df344fSAndy Whitcroft		if ($line =~ m{//}) {
24313705ce5bSJoe Perches			if (ERROR("C99_COMMENTS",
24323705ce5bSJoe Perches				  "do not use C99 // comments\n" . $herecurr) &&
24333705ce5bSJoe Perches			    $fix) {
24343705ce5bSJoe Perches				my $line = $fixed[$linenr - 1];
24353705ce5bSJoe Perches				if ($line =~ /\/\/(.*)$/) {
24363705ce5bSJoe Perches					my $comment = trim($1);
24373705ce5bSJoe Perches					$fixed[$linenr - 1] =~ s@\/\/(.*)$@/\* $comment \*/@;
24383705ce5bSJoe Perches				}
24393705ce5bSJoe Perches			}
244000df344fSAndy Whitcroft		}
244100df344fSAndy Whitcroft		# Remove C99 comments.
24420a920b5bSAndy Whitcroft		$line =~ s@//.*@@;
24436c72ffaaSAndy Whitcroft		$opline =~ s@//.*@@;
24440a920b5bSAndy Whitcroft
24452b474a1aSAndy Whitcroft# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
24462b474a1aSAndy Whitcroft# the whole statement.
24472b474a1aSAndy Whitcroft#print "APW <$lines[$realline_next - 1]>\n";
24482b474a1aSAndy Whitcroft		if (defined $realline_next &&
24492b474a1aSAndy Whitcroft		    exists $lines[$realline_next - 1] &&
24502b474a1aSAndy Whitcroft		    !defined $suppress_export{$realline_next} &&
24512b474a1aSAndy Whitcroft		    ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
24522b474a1aSAndy Whitcroft		     $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
24533cbf62dfSAndy Whitcroft			# Handle definitions which produce identifiers with
24543cbf62dfSAndy Whitcroft			# a prefix:
24553cbf62dfSAndy Whitcroft			#   XXX(foo);
24563cbf62dfSAndy Whitcroft			#   EXPORT_SYMBOL(something_foo);
2457653d4876SAndy Whitcroft			my $name = $1;
245887a53877SAndy Whitcroft			if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
24593cbf62dfSAndy Whitcroft			    $name =~ /^${Ident}_$2/) {
24603cbf62dfSAndy Whitcroft#print "FOO C name<$name>\n";
24613cbf62dfSAndy Whitcroft				$suppress_export{$realline_next} = 1;
24623cbf62dfSAndy Whitcroft
24633cbf62dfSAndy Whitcroft			} elsif ($stat !~ /(?:
24642b474a1aSAndy Whitcroft				\n.}\s*$|
246548012058SAndy Whitcroft				^.DEFINE_$Ident\(\Q$name\E\)|
246648012058SAndy Whitcroft				^.DECLARE_$Ident\(\Q$name\E\)|
246748012058SAndy Whitcroft				^.LIST_HEAD\(\Q$name\E\)|
24682b474a1aSAndy Whitcroft				^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
24692b474a1aSAndy Whitcroft				\b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
247048012058SAndy Whitcroft			    )/x) {
24712b474a1aSAndy Whitcroft#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
24722b474a1aSAndy Whitcroft				$suppress_export{$realline_next} = 2;
24732b474a1aSAndy Whitcroft			} else {
24742b474a1aSAndy Whitcroft				$suppress_export{$realline_next} = 1;
24750a920b5bSAndy Whitcroft			}
24760a920b5bSAndy Whitcroft		}
24772b474a1aSAndy Whitcroft		if (!defined $suppress_export{$linenr} &&
24782b474a1aSAndy Whitcroft		    $prevline =~ /^.\s*$/ &&
24792b474a1aSAndy Whitcroft		    ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
24802b474a1aSAndy Whitcroft		     $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
24812b474a1aSAndy Whitcroft#print "FOO B <$lines[$linenr - 1]>\n";
24822b474a1aSAndy Whitcroft			$suppress_export{$linenr} = 2;
24832b474a1aSAndy Whitcroft		}
24842b474a1aSAndy Whitcroft		if (defined $suppress_export{$linenr} &&
24852b474a1aSAndy Whitcroft		    $suppress_export{$linenr} == 2) {
2486000d1cc1SJoe Perches			WARN("EXPORT_SYMBOL",
2487000d1cc1SJoe Perches			     "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
24882b474a1aSAndy Whitcroft		}
24890a920b5bSAndy Whitcroft
24905150bda4SJoe Eloff# check for global initialisers.
2491*d5e616fcSJoe Perches		if ($line =~ /^\+(\s*$Type\s*$Ident\s*(?:\s+$Modifier))*\s*=\s*(0|NULL|false)\s*;/) {
2492*d5e616fcSJoe Perches			if (ERROR("GLOBAL_INITIALISERS",
2493000d1cc1SJoe Perches				  "do not initialise globals to 0 or NULL\n" .
2494*d5e616fcSJoe Perches				      $herecurr) &&
2495*d5e616fcSJoe Perches			    $fix) {
2496*d5e616fcSJoe Perches				$fixed[$linenr - 1] =~ s/($Type\s*$Ident\s*(?:\s+$Modifier))*\s*=\s*(0|NULL|false)\s*;/$1;/;
2497*d5e616fcSJoe Perches			}
2498f0a594c1SAndy Whitcroft		}
24990a920b5bSAndy Whitcroft# check for static initialisers.
2500*d5e616fcSJoe Perches		if ($line =~ /^\+.*\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
2501*d5e616fcSJoe Perches			if (ERROR("INITIALISED_STATIC",
2502000d1cc1SJoe Perches				  "do not initialise statics to 0 or NULL\n" .
2503*d5e616fcSJoe Perches				      $herecurr) &&
2504*d5e616fcSJoe Perches			    $fix) {
2505*d5e616fcSJoe Perches				$fixed[$linenr - 1] =~ s/(\bstatic\s.*?)\s*=\s*(0|NULL|false)\s*;/$1;/;
2506*d5e616fcSJoe Perches			}
25070a920b5bSAndy Whitcroft		}
25080a920b5bSAndy Whitcroft
2509cb710ecaSJoe Perches# check for static const char * arrays.
2510cb710ecaSJoe Perches		if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
2511000d1cc1SJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
2512000d1cc1SJoe Perches			     "static const char * array should probably be static const char * const\n" .
2513cb710ecaSJoe Perches				$herecurr);
2514cb710ecaSJoe Perches               }
2515cb710ecaSJoe Perches
2516cb710ecaSJoe Perches# check for static char foo[] = "bar" declarations.
2517cb710ecaSJoe Perches		if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
2518000d1cc1SJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
2519000d1cc1SJoe Perches			     "static char array declaration should probably be static const char\n" .
2520cb710ecaSJoe Perches				$herecurr);
2521cb710ecaSJoe Perches               }
2522cb710ecaSJoe Perches
252393ed0e2dSJoe Perches# check for declarations of struct pci_device_id
252493ed0e2dSJoe Perches		if ($line =~ /\bstruct\s+pci_device_id\s+\w+\s*\[\s*\]\s*\=\s*\{/) {
2525000d1cc1SJoe Perches			WARN("DEFINE_PCI_DEVICE_TABLE",
2526000d1cc1SJoe Perches			     "Use DEFINE_PCI_DEVICE_TABLE for struct pci_device_id\n" . $herecurr);
252793ed0e2dSJoe Perches		}
252893ed0e2dSJoe Perches
2529653d4876SAndy Whitcroft# check for new typedefs, only function parameters and sparse annotations
2530653d4876SAndy Whitcroft# make sense.
2531653d4876SAndy Whitcroft		if ($line =~ /\btypedef\s/ &&
25328054576dSAndy Whitcroft		    $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
2533c45dcabdSAndy Whitcroft		    $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
25348ed22cadSAndy Whitcroft		    $line !~ /\b$typeTypedefs\b/ &&
2535653d4876SAndy Whitcroft		    $line !~ /\b__bitwise(?:__|)\b/) {
2536000d1cc1SJoe Perches			WARN("NEW_TYPEDEFS",
2537000d1cc1SJoe Perches			     "do not add new typedefs\n" . $herecurr);
25380a920b5bSAndy Whitcroft		}
25390a920b5bSAndy Whitcroft
25400a920b5bSAndy Whitcroft# * goes on variable not on type
254165863862SAndy Whitcroft		# (char*[ const])
2542bfcb2cc7SAndy Whitcroft		while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
2543bfcb2cc7SAndy Whitcroft			#print "AA<$1>\n";
25443705ce5bSJoe Perches			my ($ident, $from, $to) = ($1, $2, $2);
2545d8aaf121SAndy Whitcroft
254665863862SAndy Whitcroft			# Should start with a space.
254765863862SAndy Whitcroft			$to =~ s/^(\S)/ $1/;
254865863862SAndy Whitcroft			# Should not end with a space.
254965863862SAndy Whitcroft			$to =~ s/\s+$//;
255065863862SAndy Whitcroft			# '*'s should not have spaces between.
2551f9a0b3d1SAndy Whitcroft			while ($to =~ s/\*\s+\*/\*\*/) {
255265863862SAndy Whitcroft			}
2553d8aaf121SAndy Whitcroft
25543705ce5bSJoe Perches##			print "1: from<$from> to<$to> ident<$ident>\n";
255565863862SAndy Whitcroft			if ($from ne $to) {
25563705ce5bSJoe Perches				if (ERROR("POINTER_LOCATION",
25573705ce5bSJoe Perches					  "\"(foo$from)\" should be \"(foo$to)\"\n" .  $herecurr) &&
25583705ce5bSJoe Perches				    $fix) {
25593705ce5bSJoe Perches					my $sub_from = $ident;
25603705ce5bSJoe Perches					my $sub_to = $ident;
25613705ce5bSJoe Perches					$sub_to =~ s/\Q$from\E/$to/;
25623705ce5bSJoe Perches					$fixed[$linenr - 1] =~
25633705ce5bSJoe Perches					    s@\Q$sub_from\E@$sub_to@;
25643705ce5bSJoe Perches				}
256565863862SAndy Whitcroft			}
2566bfcb2cc7SAndy Whitcroft		}
2567bfcb2cc7SAndy Whitcroft		while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
2568bfcb2cc7SAndy Whitcroft			#print "BB<$1>\n";
25693705ce5bSJoe Perches			my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
2570d8aaf121SAndy Whitcroft
257165863862SAndy Whitcroft			# Should start with a space.
257265863862SAndy Whitcroft			$to =~ s/^(\S)/ $1/;
257365863862SAndy Whitcroft			# Should not end with a space.
257465863862SAndy Whitcroft			$to =~ s/\s+$//;
257565863862SAndy Whitcroft			# '*'s should not have spaces between.
2576f9a0b3d1SAndy Whitcroft			while ($to =~ s/\*\s+\*/\*\*/) {
257765863862SAndy Whitcroft			}
257865863862SAndy Whitcroft			# Modifiers should have spaces.
257965863862SAndy Whitcroft			$to =~ s/(\b$Modifier$)/$1 /;
258065863862SAndy Whitcroft
25813705ce5bSJoe Perches##			print "2: from<$from> to<$to> ident<$ident>\n";
2582667026e7SAndy Whitcroft			if ($from ne $to && $ident !~ /^$Modifier$/) {
25833705ce5bSJoe Perches				if (ERROR("POINTER_LOCATION",
25843705ce5bSJoe Perches					  "\"foo${from}bar\" should be \"foo${to}bar\"\n" .  $herecurr) &&
25853705ce5bSJoe Perches				    $fix) {
25863705ce5bSJoe Perches
25873705ce5bSJoe Perches					my $sub_from = $match;
25883705ce5bSJoe Perches					my $sub_to = $match;
25893705ce5bSJoe Perches					$sub_to =~ s/\Q$from\E/$to/;
25903705ce5bSJoe Perches					$fixed[$linenr - 1] =~
25913705ce5bSJoe Perches					    s@\Q$sub_from\E@$sub_to@;
25923705ce5bSJoe Perches				}
259365863862SAndy Whitcroft			}
25940a920b5bSAndy Whitcroft		}
25950a920b5bSAndy Whitcroft
25960a920b5bSAndy Whitcroft# # no BUG() or BUG_ON()
25970a920b5bSAndy Whitcroft# 		if ($line =~ /\b(BUG|BUG_ON)\b/) {
25980a920b5bSAndy Whitcroft# 			print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
25990a920b5bSAndy Whitcroft# 			print "$herecurr";
26000a920b5bSAndy Whitcroft# 			$clean = 0;
26010a920b5bSAndy Whitcroft# 		}
26020a920b5bSAndy Whitcroft
26038905a67cSAndy Whitcroft		if ($line =~ /\bLINUX_VERSION_CODE\b/) {
2604000d1cc1SJoe Perches			WARN("LINUX_VERSION_CODE",
2605000d1cc1SJoe Perches			     "LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
26068905a67cSAndy Whitcroft		}
26078905a67cSAndy Whitcroft
260817441227SJoe Perches# check for uses of printk_ratelimit
260917441227SJoe Perches		if ($line =~ /\bprintk_ratelimit\s*\(/) {
2610000d1cc1SJoe Perches			WARN("PRINTK_RATELIMITED",
2611000d1cc1SJoe Perches"Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
261217441227SJoe Perches		}
261317441227SJoe Perches
261400df344fSAndy Whitcroft# printk should use KERN_* levels.  Note that follow on printk's on the
261500df344fSAndy Whitcroft# same line do not need a level, so we use the current block context
261600df344fSAndy Whitcroft# to try and find and validate the current printk.  In summary the current
261725985edcSLucas De Marchi# printk includes all preceding printk's which have no newline on the end.
261800df344fSAndy Whitcroft# we assume the first bad printk is the one to report.
2619f0a594c1SAndy Whitcroft		if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
262000df344fSAndy Whitcroft			my $ok = 0;
262100df344fSAndy Whitcroft			for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
262200df344fSAndy Whitcroft				#print "CHECK<$lines[$ln - 1]\n";
262325985edcSLucas De Marchi				# we have a preceding printk if it ends
262400df344fSAndy Whitcroft				# with "\n" ignore it, else it is to blame
262500df344fSAndy Whitcroft				if ($lines[$ln - 1] =~ m{\bprintk\(}) {
262600df344fSAndy Whitcroft					if ($rawlines[$ln - 1] !~ m{\\n"}) {
262700df344fSAndy Whitcroft						$ok = 1;
262800df344fSAndy Whitcroft					}
262900df344fSAndy Whitcroft					last;
263000df344fSAndy Whitcroft				}
263100df344fSAndy Whitcroft			}
263200df344fSAndy Whitcroft			if ($ok == 0) {
2633000d1cc1SJoe Perches				WARN("PRINTK_WITHOUT_KERN_LEVEL",
2634000d1cc1SJoe Perches				     "printk() should include KERN_ facility level\n" . $herecurr);
26350a920b5bSAndy Whitcroft			}
263600df344fSAndy Whitcroft		}
26370a920b5bSAndy Whitcroft
2638243f3803SJoe Perches		if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
2639243f3803SJoe Perches			my $orig = $1;
2640243f3803SJoe Perches			my $level = lc($orig);
2641243f3803SJoe Perches			$level = "warn" if ($level eq "warning");
26428f26b837SJoe Perches			my $level2 = $level;
26438f26b837SJoe Perches			$level2 = "dbg" if ($level eq "debug");
2644243f3803SJoe Perches			WARN("PREFER_PR_LEVEL",
26458f26b837SJoe Perches			     "Prefer netdev_$level2(netdev, ... then dev_$level2(dev, ... then pr_$level(...  to printk(KERN_$orig ...\n" . $herecurr);
2646243f3803SJoe Perches		}
2647243f3803SJoe Perches
2648243f3803SJoe Perches		if ($line =~ /\bpr_warning\s*\(/) {
2649*d5e616fcSJoe Perches			if (WARN("PREFER_PR_LEVEL",
2650*d5e616fcSJoe Perches				 "Prefer pr_warn(... to pr_warning(...\n" . $herecurr) &&
2651*d5e616fcSJoe Perches			    $fix) {
2652*d5e616fcSJoe Perches				$fixed[$linenr - 1] =~
2653*d5e616fcSJoe Perches				    s/\bpr_warning\b/pr_warn/;
2654*d5e616fcSJoe Perches			}
2655243f3803SJoe Perches		}
2656243f3803SJoe Perches
2657dc139313SJoe Perches		if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
2658dc139313SJoe Perches			my $orig = $1;
2659dc139313SJoe Perches			my $level = lc($orig);
2660dc139313SJoe Perches			$level = "warn" if ($level eq "warning");
2661dc139313SJoe Perches			$level = "dbg" if ($level eq "debug");
2662dc139313SJoe Perches			WARN("PREFER_DEV_LEVEL",
2663dc139313SJoe Perches			     "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
2664dc139313SJoe Perches		}
2665dc139313SJoe Perches
2666653d4876SAndy Whitcroft# function brace can't be on same line, except for #defines of do while,
2667653d4876SAndy Whitcroft# or if closed on same line
2668c45dcabdSAndy Whitcroft		if (($line=~/$Type\s*$Ident\(.*\).*\s{/) and
2669c45dcabdSAndy Whitcroft		    !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) {
2670000d1cc1SJoe Perches			ERROR("OPEN_BRACE",
2671000d1cc1SJoe Perches			      "open brace '{' following function declarations go on the next line\n" . $herecurr);
26720a920b5bSAndy Whitcroft		}
2673653d4876SAndy Whitcroft
26748905a67cSAndy Whitcroft# open braces for enum, union and struct go on the same line.
26758905a67cSAndy Whitcroft		if ($line =~ /^.\s*{/ &&
26768905a67cSAndy Whitcroft		    $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
2677000d1cc1SJoe Perches			ERROR("OPEN_BRACE",
2678000d1cc1SJoe Perches			      "open brace '{' following $1 go on the same line\n" . $hereprev);
26798905a67cSAndy Whitcroft		}
26808905a67cSAndy Whitcroft
26810c73b4ebSAndy Whitcroft# missing space after union, struct or enum definition
26823705ce5bSJoe Perches		if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
26833705ce5bSJoe Perches			if (WARN("SPACING",
26843705ce5bSJoe Perches				 "missing space after $1 definition\n" . $herecurr) &&
26853705ce5bSJoe Perches			    $fix) {
26863705ce5bSJoe Perches				$fixed[$linenr - 1] =~
26873705ce5bSJoe Perches				    s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
26883705ce5bSJoe Perches			}
26890c73b4ebSAndy Whitcroft		}
26900c73b4ebSAndy Whitcroft
26918d31cfceSAndy Whitcroft# check for spacing round square brackets; allowed:
26928d31cfceSAndy Whitcroft#  1. with a type on the left -- int [] a;
2693fe2a7dbcSAndy Whitcroft#  2. at the beginning of a line for slice initialisers -- [0...10] = 5,
2694fe2a7dbcSAndy Whitcroft#  3. inside a curly brace -- = { [0...10] = 5 }
26958d31cfceSAndy Whitcroft		while ($line =~ /(.*?\s)\[/g) {
26968d31cfceSAndy Whitcroft			my ($where, $prefix) = ($-[1], $1);
26978d31cfceSAndy Whitcroft			if ($prefix !~ /$Type\s+$/ &&
2698fe2a7dbcSAndy Whitcroft			    ($where != 0 || $prefix !~ /^.\s+$/) &&
2699daebc534SAndy Whitcroft			    $prefix !~ /[{,]\s+$/) {
27003705ce5bSJoe Perches				if (ERROR("BRACKET_SPACE",
27013705ce5bSJoe Perches					  "space prohibited before open square bracket '['\n" . $herecurr) &&
27023705ce5bSJoe Perches				    $fix) {
27033705ce5bSJoe Perches				    $fixed[$linenr - 1] =~
27043705ce5bSJoe Perches					s/^(\+.*?)\s+\[/$1\[/;
27053705ce5bSJoe Perches				}
27068d31cfceSAndy Whitcroft			}
27078d31cfceSAndy Whitcroft		}
27088d31cfceSAndy Whitcroft
2709f0a594c1SAndy Whitcroft# check for spaces between functions and their parentheses.
27106c72ffaaSAndy Whitcroft		while ($line =~ /($Ident)\s+\(/g) {
2711c2fdda0dSAndy Whitcroft			my $name = $1;
2712773647a0SAndy Whitcroft			my $ctx_before = substr($line, 0, $-[1]);
2713773647a0SAndy Whitcroft			my $ctx = "$ctx_before$name";
2714c2fdda0dSAndy Whitcroft
2715c2fdda0dSAndy Whitcroft			# Ignore those directives where spaces _are_ permitted.
2716773647a0SAndy Whitcroft			if ($name =~ /^(?:
2717773647a0SAndy Whitcroft				if|for|while|switch|return|case|
2718773647a0SAndy Whitcroft				volatile|__volatile__|
2719773647a0SAndy Whitcroft				__attribute__|format|__extension__|
2720773647a0SAndy Whitcroft				asm|__asm__)$/x)
2721773647a0SAndy Whitcroft			{
2722c2fdda0dSAndy Whitcroft			# cpp #define statements have non-optional spaces, ie
2723c2fdda0dSAndy Whitcroft			# if there is a space between the name and the open
2724c2fdda0dSAndy Whitcroft			# parenthesis it is simply not a parameter group.
2725c45dcabdSAndy Whitcroft			} elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
2726773647a0SAndy Whitcroft
2727773647a0SAndy Whitcroft			# cpp #elif statement condition may start with a (
2728c45dcabdSAndy Whitcroft			} elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
2729c2fdda0dSAndy Whitcroft
2730c2fdda0dSAndy Whitcroft			# If this whole things ends with a type its most
2731c2fdda0dSAndy Whitcroft			# likely a typedef for a function.
2732773647a0SAndy Whitcroft			} elsif ($ctx =~ /$Type$/) {
2733c2fdda0dSAndy Whitcroft
2734c2fdda0dSAndy Whitcroft			} else {
27353705ce5bSJoe Perches				if (WARN("SPACING",
27363705ce5bSJoe Perches					 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
27373705ce5bSJoe Perches					     $fix) {
27383705ce5bSJoe Perches					$fixed[$linenr - 1] =~
27393705ce5bSJoe Perches					    s/\b$name\s+\(/$name\(/;
27403705ce5bSJoe Perches				}
2741f0a594c1SAndy Whitcroft			}
27426c72ffaaSAndy Whitcroft		}
27439a4cad4eSEric Nelson
2744653d4876SAndy Whitcroft# Check operator spacing.
27450a920b5bSAndy Whitcroft		if (!($line=~/\#\s*include/)) {
27463705ce5bSJoe Perches			my $fixed_line = "";
27473705ce5bSJoe Perches			my $line_fixed = 0;
27483705ce5bSJoe Perches
27499c0ca6f9SAndy Whitcroft			my $ops = qr{
27509c0ca6f9SAndy Whitcroft				<<=|>>=|<=|>=|==|!=|
27519c0ca6f9SAndy Whitcroft				\+=|-=|\*=|\/=|%=|\^=|\|=|&=|
27529c0ca6f9SAndy Whitcroft				=>|->|<<|>>|<|>|=|!|~|
27531f65f947SAndy Whitcroft				&&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
27541f65f947SAndy Whitcroft				\?|:
27559c0ca6f9SAndy Whitcroft			}x;
2756cf655043SAndy Whitcroft			my @elements = split(/($ops|;)/, $opline);
27573705ce5bSJoe Perches
27583705ce5bSJoe Perches##			print("element count: <" . $#elements . ">\n");
27593705ce5bSJoe Perches##			foreach my $el (@elements) {
27603705ce5bSJoe Perches##				print("el: <$el>\n");
27613705ce5bSJoe Perches##			}
27623705ce5bSJoe Perches
27633705ce5bSJoe Perches			my @fix_elements = ();
276400df344fSAndy Whitcroft			my $off = 0;
27656c72ffaaSAndy Whitcroft
27663705ce5bSJoe Perches			foreach my $el (@elements) {
27673705ce5bSJoe Perches				push(@fix_elements, substr($rawline, $off, length($el)));
27683705ce5bSJoe Perches				$off += length($el);
27693705ce5bSJoe Perches			}
27703705ce5bSJoe Perches
27713705ce5bSJoe Perches			$off = 0;
27723705ce5bSJoe Perches
27736c72ffaaSAndy Whitcroft			my $blank = copy_spacing($opline);
27746c72ffaaSAndy Whitcroft
27750a920b5bSAndy Whitcroft			for (my $n = 0; $n < $#elements; $n += 2) {
27763705ce5bSJoe Perches
27773705ce5bSJoe Perches				my $good = $fix_elements[$n] . $fix_elements[$n + 1];
27783705ce5bSJoe Perches
27793705ce5bSJoe Perches##				print("n: <$n> good: <$good>\n");
27803705ce5bSJoe Perches
27814a0df2efSAndy Whitcroft				$off += length($elements[$n]);
27824a0df2efSAndy Whitcroft
278325985edcSLucas De Marchi				# Pick up the preceding and succeeding characters.
2784773647a0SAndy Whitcroft				my $ca = substr($opline, 0, $off);
2785773647a0SAndy Whitcroft				my $cc = '';
2786773647a0SAndy Whitcroft				if (length($opline) >= ($off + length($elements[$n + 1]))) {
2787773647a0SAndy Whitcroft					$cc = substr($opline, $off + length($elements[$n + 1]));
2788773647a0SAndy Whitcroft				}
2789773647a0SAndy Whitcroft				my $cb = "$ca$;$cc";
2790773647a0SAndy Whitcroft
27914a0df2efSAndy Whitcroft				my $a = '';
27924a0df2efSAndy Whitcroft				$a = 'V' if ($elements[$n] ne '');
27934a0df2efSAndy Whitcroft				$a = 'W' if ($elements[$n] =~ /\s$/);
2794cf655043SAndy Whitcroft				$a = 'C' if ($elements[$n] =~ /$;$/);
27954a0df2efSAndy Whitcroft				$a = 'B' if ($elements[$n] =~ /(\[|\()$/);
27964a0df2efSAndy Whitcroft				$a = 'O' if ($elements[$n] eq '');
2797773647a0SAndy Whitcroft				$a = 'E' if ($ca =~ /^\s*$/);
27984a0df2efSAndy Whitcroft
27990a920b5bSAndy Whitcroft				my $op = $elements[$n + 1];
28004a0df2efSAndy Whitcroft
28014a0df2efSAndy Whitcroft				my $c = '';
28020a920b5bSAndy Whitcroft				if (defined $elements[$n + 2]) {
28034a0df2efSAndy Whitcroft					$c = 'V' if ($elements[$n + 2] ne '');
28044a0df2efSAndy Whitcroft					$c = 'W' if ($elements[$n + 2] =~ /^\s/);
2805cf655043SAndy Whitcroft					$c = 'C' if ($elements[$n + 2] =~ /^$;/);
28064a0df2efSAndy Whitcroft					$c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
28074a0df2efSAndy Whitcroft					$c = 'O' if ($elements[$n + 2] eq '');
28088b1b3378SAndy Whitcroft					$c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
28094a0df2efSAndy Whitcroft				} else {
28104a0df2efSAndy Whitcroft					$c = 'E';
28110a920b5bSAndy Whitcroft				}
28120a920b5bSAndy Whitcroft
28134a0df2efSAndy Whitcroft				my $ctx = "${a}x${c}";
28144a0df2efSAndy Whitcroft
28154a0df2efSAndy Whitcroft				my $at = "(ctx:$ctx)";
28164a0df2efSAndy Whitcroft
28176c72ffaaSAndy Whitcroft				my $ptr = substr($blank, 0, $off) . "^";
2818de7d4f0eSAndy Whitcroft				my $hereptr = "$hereline$ptr\n";
28190a920b5bSAndy Whitcroft
282074048ed8SAndy Whitcroft				# Pull out the value of this operator.
28216c72ffaaSAndy Whitcroft				my $op_type = substr($curr_values, $off + 1, 1);
28220a920b5bSAndy Whitcroft
28231f65f947SAndy Whitcroft				# Get the full operator variant.
28241f65f947SAndy Whitcroft				my $opv = $op . substr($curr_vars, $off, 1);
28251f65f947SAndy Whitcroft
282613214adfSAndy Whitcroft				# Ignore operators passed as parameters.
282713214adfSAndy Whitcroft				if ($op_type ne 'V' &&
282813214adfSAndy Whitcroft				    $ca =~ /\s$/ && $cc =~ /^\s*,/) {
282913214adfSAndy Whitcroft
2830cf655043SAndy Whitcroft#				# Ignore comments
2831cf655043SAndy Whitcroft#				} elsif ($op =~ /^$;+$/) {
283213214adfSAndy Whitcroft
2833d8aaf121SAndy Whitcroft				# ; should have either the end of line or a space or \ after it
283413214adfSAndy Whitcroft				} elsif ($op eq ';') {
2835cf655043SAndy Whitcroft					if ($ctx !~ /.x[WEBC]/ &&
2836cf655043SAndy Whitcroft					    $cc !~ /^\\/ && $cc !~ /^;/) {
28373705ce5bSJoe Perches						if (ERROR("SPACING",
28383705ce5bSJoe Perches							  "space required after that '$op' $at\n" . $hereptr)) {
28393705ce5bSJoe Perches							$good = trim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
28403705ce5bSJoe Perches							$line_fixed = 1;
28413705ce5bSJoe Perches						}
2842d8aaf121SAndy Whitcroft					}
2843d8aaf121SAndy Whitcroft
2844d8aaf121SAndy Whitcroft				# // is a comment
2845d8aaf121SAndy Whitcroft				} elsif ($op eq '//') {
28460a920b5bSAndy Whitcroft
28471f65f947SAndy Whitcroft				# No spaces for:
28481f65f947SAndy Whitcroft				#   ->
28491f65f947SAndy Whitcroft				#   :   when part of a bitfield
28501f65f947SAndy Whitcroft				} elsif ($op eq '->' || $opv eq ':B') {
28514a0df2efSAndy Whitcroft					if ($ctx =~ /Wx.|.xW/) {
28523705ce5bSJoe Perches						if (ERROR("SPACING",
28533705ce5bSJoe Perches							  "spaces prohibited around that '$op' $at\n" . $hereptr)) {
28543705ce5bSJoe Perches							$good = trim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
28553705ce5bSJoe Perches							$line_fixed = 1;
28563705ce5bSJoe Perches							if (defined $fix_elements[$n + 2]) {
28573705ce5bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
28583705ce5bSJoe Perches							}
28593705ce5bSJoe Perches						}
28600a920b5bSAndy Whitcroft					}
28610a920b5bSAndy Whitcroft
28620a920b5bSAndy Whitcroft				# , must have a space on the right.
28630a920b5bSAndy Whitcroft				} elsif ($op eq ',') {
2864cf655043SAndy Whitcroft					if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
28653705ce5bSJoe Perches						if (ERROR("SPACING",
28663705ce5bSJoe Perches							  "space required after that '$op' $at\n" . $hereptr)) {
28673705ce5bSJoe Perches							$good = trim($fix_elements[$n]) . trim($fix_elements[$n + 1]) . " ";
28683705ce5bSJoe Perches							$line_fixed = 1;
28693705ce5bSJoe Perches						}
28700a920b5bSAndy Whitcroft					}
28710a920b5bSAndy Whitcroft
28729c0ca6f9SAndy Whitcroft				# '*' as part of a type definition -- reported already.
287374048ed8SAndy Whitcroft				} elsif ($opv eq '*_') {
28749c0ca6f9SAndy Whitcroft					#warn "'*' is part of type\n";
28759c0ca6f9SAndy Whitcroft
28769c0ca6f9SAndy Whitcroft				# unary operators should have a space before and
28779c0ca6f9SAndy Whitcroft				# none after.  May be left adjacent to another
28789c0ca6f9SAndy Whitcroft				# unary operator, or a cast
28799c0ca6f9SAndy Whitcroft				} elsif ($op eq '!' || $op eq '~' ||
288074048ed8SAndy Whitcroft					 $opv eq '*U' || $opv eq '-U' ||
28810d413866SAndy Whitcroft					 $opv eq '&U' || $opv eq '&&U') {
2882cf655043SAndy Whitcroft					if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
28833705ce5bSJoe Perches						if (ERROR("SPACING",
28843705ce5bSJoe Perches							  "space required before that '$op' $at\n" . $hereptr)) {
28853705ce5bSJoe Perches							$good = trim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]);
28863705ce5bSJoe Perches							$line_fixed = 1;
28873705ce5bSJoe Perches						}
28880a920b5bSAndy Whitcroft					}
2889a3340b35SAndy Whitcroft					if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
2890171ae1a4SAndy Whitcroft						# A unary '*' may be const
2891171ae1a4SAndy Whitcroft
2892171ae1a4SAndy Whitcroft					} elsif ($ctx =~ /.xW/) {
28933705ce5bSJoe Perches						if (ERROR("SPACING",
28943705ce5bSJoe Perches							  "space prohibited after that '$op' $at\n" . $hereptr)) {
28953705ce5bSJoe Perches							$fixed_line =~ s/\s+$//;
28963705ce5bSJoe Perches							$good = trim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
28973705ce5bSJoe Perches							$line_fixed = 1;
28983705ce5bSJoe Perches							if (defined $fix_elements[$n + 2]) {
28993705ce5bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
29003705ce5bSJoe Perches							}
29013705ce5bSJoe Perches						}
29020a920b5bSAndy Whitcroft					}
29030a920b5bSAndy Whitcroft
29040a920b5bSAndy Whitcroft				# unary ++ and unary -- are allowed no space on one side.
29050a920b5bSAndy Whitcroft				} elsif ($op eq '++' or $op eq '--') {
2906773647a0SAndy Whitcroft					if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
29073705ce5bSJoe Perches						if (ERROR("SPACING",
29083705ce5bSJoe Perches							  "space required one side of that '$op' $at\n" . $hereptr)) {
29093705ce5bSJoe Perches							$fixed_line =~ s/\s+$//;
29103705ce5bSJoe Perches							$good = trim($fix_elements[$n]) . trim($fix_elements[$n + 1]) . " ";
29113705ce5bSJoe Perches							$line_fixed = 1;
29123705ce5bSJoe Perches						}
29130a920b5bSAndy Whitcroft					}
2914773647a0SAndy Whitcroft					if ($ctx =~ /Wx[BE]/ ||
2915773647a0SAndy Whitcroft					    ($ctx =~ /Wx./ && $cc =~ /^;/)) {
29163705ce5bSJoe Perches						if (ERROR("SPACING",
29173705ce5bSJoe Perches							  "space prohibited before that '$op' $at\n" . $hereptr)) {
29183705ce5bSJoe Perches							$fixed_line =~ s/\s+$//;
29193705ce5bSJoe Perches							$good = trim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
29203705ce5bSJoe Perches							$line_fixed = 1;
29213705ce5bSJoe Perches						}
2922653d4876SAndy Whitcroft					}
2923773647a0SAndy Whitcroft					if ($ctx =~ /ExW/) {
29243705ce5bSJoe Perches						if (ERROR("SPACING",
29253705ce5bSJoe Perches							  "space prohibited after that '$op' $at\n" . $hereptr)) {
29263705ce5bSJoe Perches							$fixed_line =~ s/\s+$//;
29273705ce5bSJoe Perches							$good = trim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
29283705ce5bSJoe Perches							$line_fixed = 1;
29293705ce5bSJoe Perches							if (defined $fix_elements[$n + 2]) {
29303705ce5bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
2931773647a0SAndy Whitcroft							}
29323705ce5bSJoe Perches						}
29333705ce5bSJoe Perches					}
29340a920b5bSAndy Whitcroft
29350a920b5bSAndy Whitcroft				# << and >> may either have or not have spaces both sides
29369c0ca6f9SAndy Whitcroft				} elsif ($op eq '<<' or $op eq '>>' or
29379c0ca6f9SAndy Whitcroft					 $op eq '&' or $op eq '^' or $op eq '|' or
29389c0ca6f9SAndy Whitcroft					 $op eq '+' or $op eq '-' or
2939c2fdda0dSAndy Whitcroft					 $op eq '*' or $op eq '/' or
2940c2fdda0dSAndy Whitcroft					 $op eq '%')
29410a920b5bSAndy Whitcroft				{
2942773647a0SAndy Whitcroft					if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
29433705ce5bSJoe Perches						if (ERROR("SPACING",
29443705ce5bSJoe Perches							  "need consistent spacing around '$op' $at\n" . $hereptr)) {
29453705ce5bSJoe Perches							$fixed_line =~ s/\s+$//;
29463705ce5bSJoe Perches							$good = trim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
29473705ce5bSJoe Perches							$line_fixed = 1;
29483705ce5bSJoe Perches						}
29490a920b5bSAndy Whitcroft					}
29500a920b5bSAndy Whitcroft
29511f65f947SAndy Whitcroft				# A colon needs no spaces before when it is
29521f65f947SAndy Whitcroft				# terminating a case value or a label.
29531f65f947SAndy Whitcroft				} elsif ($opv eq ':C' || $opv eq ':L') {
29541f65f947SAndy Whitcroft					if ($ctx =~ /Wx./) {
29553705ce5bSJoe Perches						if (ERROR("SPACING",
29563705ce5bSJoe Perches							  "space prohibited before that '$op' $at\n" . $hereptr)) {
29573705ce5bSJoe Perches							$good = trim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
29583705ce5bSJoe Perches							$line_fixed = 1;
29593705ce5bSJoe Perches						}
29601f65f947SAndy Whitcroft					}
29611f65f947SAndy Whitcroft
29620a920b5bSAndy Whitcroft				# All the others need spaces both sides.
2963cf655043SAndy Whitcroft				} elsif ($ctx !~ /[EWC]x[CWE]/) {
29641f65f947SAndy Whitcroft					my $ok = 0;
29651f65f947SAndy Whitcroft
296622f2a2efSAndy Whitcroft					# Ignore email addresses <foo@bar>
29671f65f947SAndy Whitcroft					if (($op eq '<' &&
29681f65f947SAndy Whitcroft					     $cc =~ /^\S+\@\S+>/) ||
29691f65f947SAndy Whitcroft					    ($op eq '>' &&
29701f65f947SAndy Whitcroft					     $ca =~ /<\S+\@\S+$/))
29711f65f947SAndy Whitcroft					{
29721f65f947SAndy Whitcroft					    	$ok = 1;
29731f65f947SAndy Whitcroft					}
29741f65f947SAndy Whitcroft
29751f65f947SAndy Whitcroft					# Ignore ?:
29761f65f947SAndy Whitcroft					if (($opv eq ':O' && $ca =~ /\?$/) ||
29771f65f947SAndy Whitcroft					    ($op eq '?' && $cc =~ /^:/)) {
29781f65f947SAndy Whitcroft					    	$ok = 1;
29791f65f947SAndy Whitcroft					}
29801f65f947SAndy Whitcroft
29811f65f947SAndy Whitcroft					if ($ok == 0) {
29823705ce5bSJoe Perches						if (ERROR("SPACING",
29833705ce5bSJoe Perches							  "spaces required around that '$op' $at\n" . $hereptr)) {
29843705ce5bSJoe Perches							$good = trim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
29853705ce5bSJoe Perches							$good = $fix_elements[$n] . " " . trim($fix_elements[$n + 1]) . " ";
29863705ce5bSJoe Perches							$line_fixed = 1;
29873705ce5bSJoe Perches						}
29880a920b5bSAndy Whitcroft					}
298922f2a2efSAndy Whitcroft				}
29904a0df2efSAndy Whitcroft				$off += length($elements[$n + 1]);
29913705ce5bSJoe Perches
29923705ce5bSJoe Perches##				print("n: <$n> GOOD: <$good>\n");
29933705ce5bSJoe Perches
29943705ce5bSJoe Perches				$fixed_line = $fixed_line . $good;
29950a920b5bSAndy Whitcroft			}
29963705ce5bSJoe Perches
29973705ce5bSJoe Perches			if (($#elements % 2) == 0) {
29983705ce5bSJoe Perches				$fixed_line = $fixed_line . $fix_elements[$#elements];
29993705ce5bSJoe Perches			}
30003705ce5bSJoe Perches
30013705ce5bSJoe Perches			if ($fix && $line_fixed && $fixed_line ne $fixed[$linenr - 1]) {
30023705ce5bSJoe Perches				$fixed[$linenr - 1] = $fixed_line;
30033705ce5bSJoe Perches			}
30043705ce5bSJoe Perches
30053705ce5bSJoe Perches
30060a920b5bSAndy Whitcroft		}
30070a920b5bSAndy Whitcroft
3008786b6326SJoe Perches# check for whitespace before a non-naked semicolon
3009786b6326SJoe Perches		if ($line =~ /^\+.*\S\s+;/) {
3010786b6326SJoe Perches			if (WARN("SPACING",
3011786b6326SJoe Perches				 "space prohibited before semicolon\n" . $herecurr) &&
3012786b6326SJoe Perches			    $fix) {
3013786b6326SJoe Perches				1 while $fixed[$linenr - 1] =~
3014786b6326SJoe Perches				    s/^(\+.*\S)\s+;/$1;/;
3015786b6326SJoe Perches			}
3016786b6326SJoe Perches		}
3017786b6326SJoe Perches
3018f0a594c1SAndy Whitcroft# check for multiple assignments
3019f0a594c1SAndy Whitcroft		if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
3020000d1cc1SJoe Perches			CHK("MULTIPLE_ASSIGNMENTS",
3021000d1cc1SJoe Perches			    "multiple assignments should be avoided\n" . $herecurr);
3022f0a594c1SAndy Whitcroft		}
3023f0a594c1SAndy Whitcroft
302422f2a2efSAndy Whitcroft## # check for multiple declarations, allowing for a function declaration
302522f2a2efSAndy Whitcroft## # continuation.
302622f2a2efSAndy Whitcroft## 		if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
302722f2a2efSAndy Whitcroft## 		    $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
302822f2a2efSAndy Whitcroft##
302922f2a2efSAndy Whitcroft## 			# Remove any bracketed sections to ensure we do not
303022f2a2efSAndy Whitcroft## 			# falsly report the parameters of functions.
303122f2a2efSAndy Whitcroft## 			my $ln = $line;
303222f2a2efSAndy Whitcroft## 			while ($ln =~ s/\([^\(\)]*\)//g) {
303322f2a2efSAndy Whitcroft## 			}
303422f2a2efSAndy Whitcroft## 			if ($ln =~ /,/) {
3035000d1cc1SJoe Perches## 				WARN("MULTIPLE_DECLARATION",
3036000d1cc1SJoe Perches##				     "declaring multiple variables together should be avoided\n" . $herecurr);
303722f2a2efSAndy Whitcroft## 			}
303822f2a2efSAndy Whitcroft## 		}
3039f0a594c1SAndy Whitcroft
30400a920b5bSAndy Whitcroft#need space before brace following if, while, etc
304122f2a2efSAndy Whitcroft		if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
304222f2a2efSAndy Whitcroft		    $line =~ /do{/) {
30433705ce5bSJoe Perches			if (ERROR("SPACING",
30443705ce5bSJoe Perches				  "space required before the open brace '{'\n" . $herecurr) &&
30453705ce5bSJoe Perches			    $fix) {
3046*d5e616fcSJoe Perches				$fixed[$linenr - 1] =~ s/^(\+.*(?:do|\))){/$1 {/;
30473705ce5bSJoe Perches			}
3048de7d4f0eSAndy Whitcroft		}
3049de7d4f0eSAndy Whitcroft
3050c4a62ef9SJoe Perches## # check for blank lines before declarations
3051c4a62ef9SJoe Perches##		if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
3052c4a62ef9SJoe Perches##		    $prevrawline =~ /^.\s*$/) {
3053c4a62ef9SJoe Perches##			WARN("SPACING",
3054c4a62ef9SJoe Perches##			     "No blank lines before declarations\n" . $hereprev);
3055c4a62ef9SJoe Perches##		}
3056c4a62ef9SJoe Perches##
3057c4a62ef9SJoe Perches
3058de7d4f0eSAndy Whitcroft# closing brace should have a space following it when it has anything
3059de7d4f0eSAndy Whitcroft# on the line
3060de7d4f0eSAndy Whitcroft		if ($line =~ /}(?!(?:,|;|\)))\S/) {
3061*d5e616fcSJoe Perches			if (ERROR("SPACING",
3062*d5e616fcSJoe Perches				  "space required after that close brace '}'\n" . $herecurr) &&
3063*d5e616fcSJoe Perches			    $fix) {
3064*d5e616fcSJoe Perches				$fixed[$linenr - 1] =~
3065*d5e616fcSJoe Perches				    s/}((?!(?:,|;|\)))\S)/} $1/;
3066*d5e616fcSJoe Perches			}
30670a920b5bSAndy Whitcroft		}
30680a920b5bSAndy Whitcroft
306922f2a2efSAndy Whitcroft# check spacing on square brackets
307022f2a2efSAndy Whitcroft		if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
30713705ce5bSJoe Perches			if (ERROR("SPACING",
30723705ce5bSJoe Perches				  "space prohibited after that open square bracket '['\n" . $herecurr) &&
30733705ce5bSJoe Perches			    $fix) {
30743705ce5bSJoe Perches				$fixed[$linenr - 1] =~
30753705ce5bSJoe Perches				    s/\[\s+/\[/;
30763705ce5bSJoe Perches			}
307722f2a2efSAndy Whitcroft		}
307822f2a2efSAndy Whitcroft		if ($line =~ /\s\]/) {
30793705ce5bSJoe Perches			if (ERROR("SPACING",
30803705ce5bSJoe Perches				  "space prohibited before that close square bracket ']'\n" . $herecurr) &&
30813705ce5bSJoe Perches			    $fix) {
30823705ce5bSJoe Perches				$fixed[$linenr - 1] =~
30833705ce5bSJoe Perches				    s/\s+\]/\]/;
30843705ce5bSJoe Perches			}
308522f2a2efSAndy Whitcroft		}
308622f2a2efSAndy Whitcroft
3087c45dcabdSAndy Whitcroft# check spacing on parentheses
30889c0ca6f9SAndy Whitcroft		if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
30899c0ca6f9SAndy Whitcroft		    $line !~ /for\s*\(\s+;/) {
30903705ce5bSJoe Perches			if (ERROR("SPACING",
30913705ce5bSJoe Perches				  "space prohibited after that open parenthesis '('\n" . $herecurr) &&
30923705ce5bSJoe Perches			    $fix) {
30933705ce5bSJoe Perches				$fixed[$linenr - 1] =~
30943705ce5bSJoe Perches				    s/\(\s+/\(/;
30953705ce5bSJoe Perches			}
309622f2a2efSAndy Whitcroft		}
309713214adfSAndy Whitcroft		if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
3098c45dcabdSAndy Whitcroft		    $line !~ /for\s*\(.*;\s+\)/ &&
3099c45dcabdSAndy Whitcroft		    $line !~ /:\s+\)/) {
31003705ce5bSJoe Perches			if (ERROR("SPACING",
31013705ce5bSJoe Perches				  "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
31023705ce5bSJoe Perches			    $fix) {
31033705ce5bSJoe Perches				$fixed[$linenr - 1] =~
31043705ce5bSJoe Perches				    s/\s+\)/\)/;
31053705ce5bSJoe Perches			}
310622f2a2efSAndy Whitcroft		}
310722f2a2efSAndy Whitcroft
31080a920b5bSAndy Whitcroft#goto labels aren't indented, allow a single space however
31094a0df2efSAndy Whitcroft		if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
31100a920b5bSAndy Whitcroft		   !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
31113705ce5bSJoe Perches			if (WARN("INDENTED_LABEL",
31123705ce5bSJoe Perches				 "labels should not be indented\n" . $herecurr) &&
31133705ce5bSJoe Perches			    $fix) {
31143705ce5bSJoe Perches				$fixed[$linenr - 1] =~
31153705ce5bSJoe Perches				    s/^(.)\s+/$1/;
31163705ce5bSJoe Perches			}
31170a920b5bSAndy Whitcroft		}
31180a920b5bSAndy Whitcroft
3119c45dcabdSAndy Whitcroft# Return is not a function.
3120c45dcabdSAndy Whitcroft		if (defined($stat) && $stat =~ /^.\s*return(\s*)(\(.*);/s) {
3121c45dcabdSAndy Whitcroft			my $spacing = $1;
3122c45dcabdSAndy Whitcroft			my $value = $2;
3123c45dcabdSAndy Whitcroft
312486f9d059SAndy Whitcroft			# Flatten any parentheses
3125fb2d2c1bSAndy Whitcroft			$value =~ s/\(/ \(/g;
3126fb2d2c1bSAndy Whitcroft			$value =~ s/\)/\) /g;
3127e01886adSAndy Whitcroft			while ($value =~ s/\[[^\[\]]*\]/1/ ||
312863f17f89SAndy Whitcroft			       $value !~ /(?:$Ident|-?$Constant)\s*
312963f17f89SAndy Whitcroft					     $Compare\s*
313063f17f89SAndy Whitcroft					     (?:$Ident|-?$Constant)/x &&
313163f17f89SAndy Whitcroft			       $value =~ s/\([^\(\)]*\)/1/) {
3132c45dcabdSAndy Whitcroft			}
3133fb2d2c1bSAndy Whitcroft#print "value<$value>\n";
3134fb2d2c1bSAndy Whitcroft			if ($value =~ /^\s*(?:$Ident|-?$Constant)\s*$/) {
3135000d1cc1SJoe Perches				ERROR("RETURN_PARENTHESES",
3136000d1cc1SJoe Perches				      "return is not a function, parentheses are not required\n" . $herecurr);
3137c45dcabdSAndy Whitcroft
3138c45dcabdSAndy Whitcroft			} elsif ($spacing !~ /\s+/) {
3139000d1cc1SJoe Perches				ERROR("SPACING",
3140000d1cc1SJoe Perches				      "space required before the open parenthesis '('\n" . $herecurr);
3141c45dcabdSAndy Whitcroft			}
3142c45dcabdSAndy Whitcroft		}
314353a3c448SAndy Whitcroft# Return of what appears to be an errno should normally be -'ve
314453a3c448SAndy Whitcroft		if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) {
314553a3c448SAndy Whitcroft			my $name = $1;
314653a3c448SAndy Whitcroft			if ($name ne 'EOF' && $name ne 'ERROR') {
3147000d1cc1SJoe Perches				WARN("USE_NEGATIVE_ERRNO",
3148000d1cc1SJoe Perches				     "return of an errno should typically be -ve (return -$1)\n" . $herecurr);
314953a3c448SAndy Whitcroft			}
315053a3c448SAndy Whitcroft		}
3151c45dcabdSAndy Whitcroft
31520a920b5bSAndy Whitcroft# Need a space before open parenthesis after if, while etc
31534a0df2efSAndy Whitcroft		if ($line =~ /\b(if|while|for|switch)\(/) {
31543705ce5bSJoe Perches			if (ERROR("SPACING",
31553705ce5bSJoe Perches				  "space required before the open parenthesis '('\n" . $herecurr) &&
31563705ce5bSJoe Perches			    $fix) {
31573705ce5bSJoe Perches				$fixed[$linenr - 1] =~
31583705ce5bSJoe Perches				    s/\b(if|while|for|switch)\(/$1 \(/;
31593705ce5bSJoe Perches			}
31600a920b5bSAndy Whitcroft		}
31610a920b5bSAndy Whitcroft
3162f5fe35ddSAndy Whitcroft# Check for illegal assignment in if conditional -- and check for trailing
3163f5fe35ddSAndy Whitcroft# statements after the conditional.
3164170d3a22SAndy Whitcroft		if ($line =~ /do\s*(?!{)/) {
31653e469cdcSAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
31663e469cdcSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0)
31673e469cdcSAndy Whitcroft					if (!defined $stat);
3168170d3a22SAndy Whitcroft			my ($stat_next) = ctx_statement_block($line_nr_next,
3169170d3a22SAndy Whitcroft						$remain_next, $off_next);
3170170d3a22SAndy Whitcroft			$stat_next =~ s/\n./\n /g;
3171170d3a22SAndy Whitcroft			##print "stat<$stat> stat_next<$stat_next>\n";
3172170d3a22SAndy Whitcroft
3173170d3a22SAndy Whitcroft			if ($stat_next =~ /^\s*while\b/) {
3174170d3a22SAndy Whitcroft				# If the statement carries leading newlines,
3175170d3a22SAndy Whitcroft				# then count those as offsets.
3176170d3a22SAndy Whitcroft				my ($whitespace) =
3177170d3a22SAndy Whitcroft					($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
3178170d3a22SAndy Whitcroft				my $offset =
3179170d3a22SAndy Whitcroft					statement_rawlines($whitespace) - 1;
3180170d3a22SAndy Whitcroft
3181170d3a22SAndy Whitcroft				$suppress_whiletrailers{$line_nr_next +
3182170d3a22SAndy Whitcroft								$offset} = 1;
3183170d3a22SAndy Whitcroft			}
3184170d3a22SAndy Whitcroft		}
3185170d3a22SAndy Whitcroft		if (!defined $suppress_whiletrailers{$linenr} &&
3186170d3a22SAndy Whitcroft		    $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
3187171ae1a4SAndy Whitcroft			my ($s, $c) = ($stat, $cond);
31888905a67cSAndy Whitcroft
3189b53c8e10SAndy Whitcroft			if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
3190000d1cc1SJoe Perches				ERROR("ASSIGN_IN_IF",
3191000d1cc1SJoe Perches				      "do not use assignment in if condition\n" . $herecurr);
31928905a67cSAndy Whitcroft			}
31938905a67cSAndy Whitcroft
31948905a67cSAndy Whitcroft			# Find out what is on the end of the line after the
31958905a67cSAndy Whitcroft			# conditional.
3196773647a0SAndy Whitcroft			substr($s, 0, length($c), '');
31978905a67cSAndy Whitcroft			$s =~ s/\n.*//g;
319813214adfSAndy Whitcroft			$s =~ s/$;//g; 	# Remove any comments
319953210168SAndy Whitcroft			if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
320053210168SAndy Whitcroft			    $c !~ /}\s*while\s*/)
3201773647a0SAndy Whitcroft			{
3202bb44ad39SAndy Whitcroft				# Find out how long the conditional actually is.
3203bb44ad39SAndy Whitcroft				my @newlines = ($c =~ /\n/gs);
3204bb44ad39SAndy Whitcroft				my $cond_lines = 1 + $#newlines;
320542bdf74cSHidetoshi Seto				my $stat_real = '';
3206bb44ad39SAndy Whitcroft
320742bdf74cSHidetoshi Seto				$stat_real = raw_line($linenr, $cond_lines)
320842bdf74cSHidetoshi Seto							. "\n" if ($cond_lines);
3209bb44ad39SAndy Whitcroft				if (defined($stat_real) && $cond_lines > 1) {
3210bb44ad39SAndy Whitcroft					$stat_real = "[...]\n$stat_real";
3211bb44ad39SAndy Whitcroft				}
3212bb44ad39SAndy Whitcroft
3213000d1cc1SJoe Perches				ERROR("TRAILING_STATEMENTS",
3214000d1cc1SJoe Perches				      "trailing statements should be on next line\n" . $herecurr . $stat_real);
32158905a67cSAndy Whitcroft			}
32168905a67cSAndy Whitcroft		}
32178905a67cSAndy Whitcroft
321813214adfSAndy Whitcroft# Check for bitwise tests written as boolean
321913214adfSAndy Whitcroft		if ($line =~ /
322013214adfSAndy Whitcroft			(?:
322113214adfSAndy Whitcroft				(?:\[|\(|\&\&|\|\|)
322213214adfSAndy Whitcroft				\s*0[xX][0-9]+\s*
322313214adfSAndy Whitcroft				(?:\&\&|\|\|)
322413214adfSAndy Whitcroft			|
322513214adfSAndy Whitcroft				(?:\&\&|\|\|)
322613214adfSAndy Whitcroft				\s*0[xX][0-9]+\s*
322713214adfSAndy Whitcroft				(?:\&\&|\|\||\)|\])
322813214adfSAndy Whitcroft			)/x)
322913214adfSAndy Whitcroft		{
3230000d1cc1SJoe Perches			WARN("HEXADECIMAL_BOOLEAN_TEST",
3231000d1cc1SJoe Perches			     "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
323213214adfSAndy Whitcroft		}
323313214adfSAndy Whitcroft
32348905a67cSAndy Whitcroft# if and else should not have general statements after it
323513214adfSAndy Whitcroft		if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
323613214adfSAndy Whitcroft			my $s = $1;
323713214adfSAndy Whitcroft			$s =~ s/$;//g; 	# Remove any comments
323813214adfSAndy Whitcroft			if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
3239000d1cc1SJoe Perches				ERROR("TRAILING_STATEMENTS",
3240000d1cc1SJoe Perches				      "trailing statements should be on next line\n" . $herecurr);
32410a920b5bSAndy Whitcroft			}
324213214adfSAndy Whitcroft		}
324339667782SAndy Whitcroft# if should not continue a brace
324439667782SAndy Whitcroft		if ($line =~ /}\s*if\b/) {
3245000d1cc1SJoe Perches			ERROR("TRAILING_STATEMENTS",
3246000d1cc1SJoe Perches			      "trailing statements should be on next line\n" .
324739667782SAndy Whitcroft				$herecurr);
324839667782SAndy Whitcroft		}
3249a1080bf8SAndy Whitcroft# case and default should not have general statements after them
3250a1080bf8SAndy Whitcroft		if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
3251a1080bf8SAndy Whitcroft		    $line !~ /\G(?:
32523fef12d6SAndy Whitcroft			(?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
3253a1080bf8SAndy Whitcroft			\s*return\s+
3254a1080bf8SAndy Whitcroft		    )/xg)
3255a1080bf8SAndy Whitcroft		{
3256000d1cc1SJoe Perches			ERROR("TRAILING_STATEMENTS",
3257000d1cc1SJoe Perches			      "trailing statements should be on next line\n" . $herecurr);
3258a1080bf8SAndy Whitcroft		}
32590a920b5bSAndy Whitcroft
32600a920b5bSAndy Whitcroft		# Check for }<nl>else {, these must be at the same
32610a920b5bSAndy Whitcroft		# indent level to be relevant to each other.
32620a920b5bSAndy Whitcroft		if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
32630a920b5bSAndy Whitcroft						$previndent == $indent) {
3264000d1cc1SJoe Perches			ERROR("ELSE_AFTER_BRACE",
3265000d1cc1SJoe Perches			      "else should follow close brace '}'\n" . $hereprev);
32660a920b5bSAndy Whitcroft		}
32670a920b5bSAndy Whitcroft
3268c2fdda0dSAndy Whitcroft		if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and
3269c2fdda0dSAndy Whitcroft						$previndent == $indent) {
3270c2fdda0dSAndy Whitcroft			my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
3271c2fdda0dSAndy Whitcroft
3272c2fdda0dSAndy Whitcroft			# Find out what is on the end of the line after the
3273c2fdda0dSAndy Whitcroft			# conditional.
3274773647a0SAndy Whitcroft			substr($s, 0, length($c), '');
3275c2fdda0dSAndy Whitcroft			$s =~ s/\n.*//g;
3276c2fdda0dSAndy Whitcroft
3277c2fdda0dSAndy Whitcroft			if ($s =~ /^\s*;/) {
3278000d1cc1SJoe Perches				ERROR("WHILE_AFTER_BRACE",
3279000d1cc1SJoe Perches				      "while should follow close brace '}'\n" . $hereprev);
3280c2fdda0dSAndy Whitcroft			}
3281c2fdda0dSAndy Whitcroft		}
3282c2fdda0dSAndy Whitcroft
328395e2c602SJoe Perches#Specific variable tests
3284323c1260SJoe Perches		while ($line =~ m{($Constant|$Lval)}g) {
3285323c1260SJoe Perches			my $var = $1;
328695e2c602SJoe Perches
328795e2c602SJoe Perches#gcc binary extension
328895e2c602SJoe Perches			if ($var =~ /^$Binary$/) {
3289*d5e616fcSJoe Perches				if (WARN("GCC_BINARY_CONSTANT",
3290*d5e616fcSJoe Perches					 "Avoid gcc v4.3+ binary constant extension: <$var>\n" . $herecurr) &&
3291*d5e616fcSJoe Perches				    $fix) {
3292*d5e616fcSJoe Perches					my $hexval = sprintf("0x%x", oct($var));
3293*d5e616fcSJoe Perches					$fixed[$linenr - 1] =~
3294*d5e616fcSJoe Perches					    s/\b$var\b/$hexval/;
3295*d5e616fcSJoe Perches				}
329695e2c602SJoe Perches			}
329795e2c602SJoe Perches
329895e2c602SJoe Perches#CamelCase
3299807bd26cSJoe Perches			if ($var !~ /^$Constant$/ &&
3300be79794bSJoe Perches			    $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
330122735ce8SJoe Perches#Ignore Page<foo> variants
3302807bd26cSJoe Perches			    $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
330322735ce8SJoe Perches#Ignore SI style variants like nS, mV and dB (ie: max_uV, regulator_min_uA_show)
33043445686aSJoe Perches			    $var !~ /^(?:[a-z_]*?)_?[a-z][A-Z](?:_[a-z_]+)?$/) {
33053445686aSJoe Perches				seed_camelcase_includes() if ($check);
33063445686aSJoe Perches				if (!defined $camelcase{$var}) {
3307323c1260SJoe Perches					$camelcase{$var} = 1;
3308be79794bSJoe Perches					CHK("CAMELCASE",
3309323c1260SJoe Perches					    "Avoid CamelCase: <$var>\n" . $herecurr);
3310323c1260SJoe Perches				}
3311323c1260SJoe Perches			}
33123445686aSJoe Perches		}
33130a920b5bSAndy Whitcroft
33140a920b5bSAndy Whitcroft#no spaces allowed after \ in define
3315*d5e616fcSJoe Perches		if ($line =~ /\#\s*define.*\\\s+$/) {
3316*d5e616fcSJoe Perches			if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
3317*d5e616fcSJoe Perches				 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
3318*d5e616fcSJoe Perches			    $fix) {
3319*d5e616fcSJoe Perches				$fixed[$linenr - 1] =~ s/\s+$//;
3320*d5e616fcSJoe Perches			}
33210a920b5bSAndy Whitcroft		}
33220a920b5bSAndy Whitcroft
3323653d4876SAndy Whitcroft#warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
3324c45dcabdSAndy Whitcroft		if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
3325e09dec48SAndy Whitcroft			my $file = "$1.h";
3326e09dec48SAndy Whitcroft			my $checkfile = "include/linux/$file";
3327e09dec48SAndy Whitcroft			if (-f "$root/$checkfile" &&
3328e09dec48SAndy Whitcroft			    $realfile ne $checkfile &&
33297840a94cSWolfram Sang			    $1 !~ /$allowed_asm_includes/)
3330c45dcabdSAndy Whitcroft			{
3331e09dec48SAndy Whitcroft				if ($realfile =~ m{^arch/}) {
3332000d1cc1SJoe Perches					CHK("ARCH_INCLUDE_LINUX",
3333000d1cc1SJoe Perches					    "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
3334e09dec48SAndy Whitcroft				} else {
3335000d1cc1SJoe Perches					WARN("INCLUDE_LINUX",
3336000d1cc1SJoe Perches					     "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
3337e09dec48SAndy Whitcroft				}
33380a920b5bSAndy Whitcroft			}
33390a920b5bSAndy Whitcroft		}
33400a920b5bSAndy Whitcroft
3341653d4876SAndy Whitcroft# multi-statement macros should be enclosed in a do while loop, grab the
3342653d4876SAndy Whitcroft# first statement and ensure its the whole macro if its not enclosed
3343cf655043SAndy Whitcroft# in a known good container
3344b8f96a31SAndy Whitcroft		if ($realfile !~ m@/vmlinux.lds.h$@ &&
3345b8f96a31SAndy Whitcroft		    $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
3346d8aaf121SAndy Whitcroft			my $ln = $linenr;
3347d8aaf121SAndy Whitcroft			my $cnt = $realcnt;
3348c45dcabdSAndy Whitcroft			my ($off, $dstat, $dcond, $rest);
3349c45dcabdSAndy Whitcroft			my $ctx = '';
3350c45dcabdSAndy Whitcroft			($dstat, $dcond, $ln, $cnt, $off) =
3351f74bd194SAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0);
3352f74bd194SAndy Whitcroft			$ctx = $dstat;
3353c45dcabdSAndy Whitcroft			#print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
3354a3bb97a7SAndy Whitcroft			#print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
3355c45dcabdSAndy Whitcroft
3356f74bd194SAndy Whitcroft			$dstat =~ s/^.\s*\#\s*define\s+$Ident(?:\([^\)]*\))?\s*//;
3357292f1a9bSAndy Whitcroft			$dstat =~ s/$;//g;
3358c45dcabdSAndy Whitcroft			$dstat =~ s/\\\n.//g;
3359c45dcabdSAndy Whitcroft			$dstat =~ s/^\s*//s;
3360c45dcabdSAndy Whitcroft			$dstat =~ s/\s*$//s;
3361c45dcabdSAndy Whitcroft
3362c45dcabdSAndy Whitcroft			# Flatten any parentheses and braces
3363bf30d6edSAndy Whitcroft			while ($dstat =~ s/\([^\(\)]*\)/1/ ||
3364bf30d6edSAndy Whitcroft			       $dstat =~ s/\{[^\{\}]*\}/1/ ||
3365c81769fdSAndy Whitcroft			       $dstat =~ s/\[[^\[\]]*\]/1/)
3366bf30d6edSAndy Whitcroft			{
3367c45dcabdSAndy Whitcroft			}
3368c45dcabdSAndy Whitcroft
3369e45bab8eSAndy Whitcroft			# Flatten any obvious string concatentation.
3370e45bab8eSAndy Whitcroft			while ($dstat =~ s/("X*")\s*$Ident/$1/ ||
3371e45bab8eSAndy Whitcroft			       $dstat =~ s/$Ident\s*("X*")/$1/)
3372e45bab8eSAndy Whitcroft			{
3373e45bab8eSAndy Whitcroft			}
3374e45bab8eSAndy Whitcroft
3375c45dcabdSAndy Whitcroft			my $exceptions = qr{
3376c45dcabdSAndy Whitcroft				$Declare|
3377c45dcabdSAndy Whitcroft				module_param_named|
3378a0a0a7a9SKees Cook				MODULE_PARM_DESC|
3379c45dcabdSAndy Whitcroft				DECLARE_PER_CPU|
3380c45dcabdSAndy Whitcroft				DEFINE_PER_CPU|
3381383099fdSAndy Whitcroft				__typeof__\(|
338222fd2d3eSStefani Seibold				union|
338322fd2d3eSStefani Seibold				struct|
3384ea71a0a0SAndy Whitcroft				\.$Ident\s*=\s*|
3385ea71a0a0SAndy Whitcroft				^\"|\"$
3386c45dcabdSAndy Whitcroft			}x;
33875eaa20b9SAndy Whitcroft			#print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
3388f74bd194SAndy Whitcroft			if ($dstat ne '' &&
3389f74bd194SAndy Whitcroft			    $dstat !~ /^(?:$Ident|-?$Constant),$/ &&			# 10, // foo(),
3390f74bd194SAndy Whitcroft			    $dstat !~ /^(?:$Ident|-?$Constant);$/ &&			# foo();
33913cc4b1c3SJoe Perches			    $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ &&		# 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
3392b9df76acSAndy Whitcroft			    $dstat !~ /^'X'$/ &&					# character constants
3393f74bd194SAndy Whitcroft			    $dstat !~ /$exceptions/ &&
3394f74bd194SAndy Whitcroft			    $dstat !~ /^\.$Ident\s*=/ &&				# .foo =
3395e942e2c3SJoe Perches			    $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ &&		# stringification #foo
339672f115f9SAndy Whitcroft			    $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ &&	# do {...} while (...); // do {...} while (...)
3397f74bd194SAndy Whitcroft			    $dstat !~ /^for\s*$Constant$/ &&				# for (...)
3398f74bd194SAndy Whitcroft			    $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ &&	# for (...) bar()
3399f74bd194SAndy Whitcroft			    $dstat !~ /^do\s*{/ &&					# do {...
3400f74bd194SAndy Whitcroft			    $dstat !~ /^\({/)						# ({...
3401c45dcabdSAndy Whitcroft			{
3402f74bd194SAndy Whitcroft				$ctx =~ s/\n*$//;
3403f74bd194SAndy Whitcroft				my $herectx = $here . "\n";
3404f74bd194SAndy Whitcroft				my $cnt = statement_rawlines($ctx);
3405f74bd194SAndy Whitcroft
3406f74bd194SAndy Whitcroft				for (my $n = 0; $n < $cnt; $n++) {
3407f74bd194SAndy Whitcroft					$herectx .= raw_line($linenr, $n) . "\n";
3408c45dcabdSAndy Whitcroft				}
3409c45dcabdSAndy Whitcroft
3410f74bd194SAndy Whitcroft				if ($dstat =~ /;/) {
3411f74bd194SAndy Whitcroft					ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
3412f74bd194SAndy Whitcroft					      "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
3413f74bd194SAndy Whitcroft				} else {
3414000d1cc1SJoe Perches					ERROR("COMPLEX_MACRO",
3415f74bd194SAndy Whitcroft					      "Macros with complex values should be enclosed in parenthesis\n" . "$herectx");
3416d8aaf121SAndy Whitcroft				}
34170a920b5bSAndy Whitcroft			}
34185023d347SJoe Perches
3419481eb486SJoe Perches# check for line continuations outside of #defines, preprocessor #, and asm
34205023d347SJoe Perches
34215023d347SJoe Perches		} else {
34225023d347SJoe Perches			if ($prevline !~ /^..*\\$/ &&
3423481eb486SJoe Perches			    $line !~ /^\+\s*\#.*\\$/ &&		# preprocessor
3424481eb486SJoe Perches			    $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ &&	# asm
34255023d347SJoe Perches			    $line =~ /^\+.*\\$/) {
34265023d347SJoe Perches				WARN("LINE_CONTINUATIONS",
34275023d347SJoe Perches				     "Avoid unnecessary line continuations\n" . $herecurr);
34285023d347SJoe Perches			}
3429653d4876SAndy Whitcroft		}
34300a920b5bSAndy Whitcroft
3431b13edf7fSJoe Perches# do {} while (0) macro tests:
3432b13edf7fSJoe Perches# single-statement macros do not need to be enclosed in do while (0) loop,
3433b13edf7fSJoe Perches# macro should not end with a semicolon
3434b13edf7fSJoe Perches		if ($^V && $^V ge 5.10.0 &&
3435b13edf7fSJoe Perches		    $realfile !~ m@/vmlinux.lds.h$@ &&
3436b13edf7fSJoe Perches		    $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
3437b13edf7fSJoe Perches			my $ln = $linenr;
3438b13edf7fSJoe Perches			my $cnt = $realcnt;
3439b13edf7fSJoe Perches			my ($off, $dstat, $dcond, $rest);
3440b13edf7fSJoe Perches			my $ctx = '';
3441b13edf7fSJoe Perches			($dstat, $dcond, $ln, $cnt, $off) =
3442b13edf7fSJoe Perches				ctx_statement_block($linenr, $realcnt, 0);
3443b13edf7fSJoe Perches			$ctx = $dstat;
3444b13edf7fSJoe Perches
3445b13edf7fSJoe Perches			$dstat =~ s/\\\n.//g;
3446b13edf7fSJoe Perches
3447b13edf7fSJoe Perches			if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
3448b13edf7fSJoe Perches				my $stmts = $2;
3449b13edf7fSJoe Perches				my $semis = $3;
3450b13edf7fSJoe Perches
3451b13edf7fSJoe Perches				$ctx =~ s/\n*$//;
3452b13edf7fSJoe Perches				my $cnt = statement_rawlines($ctx);
3453b13edf7fSJoe Perches				my $herectx = $here . "\n";
3454b13edf7fSJoe Perches
3455b13edf7fSJoe Perches				for (my $n = 0; $n < $cnt; $n++) {
3456b13edf7fSJoe Perches					$herectx .= raw_line($linenr, $n) . "\n";
3457b13edf7fSJoe Perches				}
3458b13edf7fSJoe Perches
3459ac8e97f8SJoe Perches				if (($stmts =~ tr/;/;/) == 1 &&
3460ac8e97f8SJoe Perches				    $stmts !~ /^\s*(if|while|for|switch)\b/) {
3461b13edf7fSJoe Perches					WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
3462b13edf7fSJoe Perches					     "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
3463b13edf7fSJoe Perches				}
3464b13edf7fSJoe Perches				if (defined $semis && $semis ne "") {
3465b13edf7fSJoe Perches					WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
3466b13edf7fSJoe Perches					     "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
3467b13edf7fSJoe Perches				}
3468b13edf7fSJoe Perches			}
3469b13edf7fSJoe Perches		}
3470b13edf7fSJoe Perches
3471080ba929SMike Frysinger# make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
3472080ba929SMike Frysinger# all assignments may have only one of the following with an assignment:
3473080ba929SMike Frysinger#	.
3474080ba929SMike Frysinger#	ALIGN(...)
3475080ba929SMike Frysinger#	VMLINUX_SYMBOL(...)
3476080ba929SMike Frysinger		if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
3477000d1cc1SJoe Perches			WARN("MISSING_VMLINUX_SYMBOL",
3478000d1cc1SJoe Perches			     "vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
3479080ba929SMike Frysinger		}
3480080ba929SMike Frysinger
3481f0a594c1SAndy Whitcroft# check for redundant bracing round if etc
348213214adfSAndy Whitcroft		if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
348313214adfSAndy Whitcroft			my ($level, $endln, @chunks) =
3484cf655043SAndy Whitcroft				ctx_statement_full($linenr, $realcnt, 1);
348513214adfSAndy Whitcroft			#print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
3486cf655043SAndy Whitcroft			#print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
3487cf655043SAndy Whitcroft			if ($#chunks > 0 && $level == 0) {
3488aad4f614SJoe Perches				my @allowed = ();
3489aad4f614SJoe Perches				my $allow = 0;
349013214adfSAndy Whitcroft				my $seen = 0;
3491773647a0SAndy Whitcroft				my $herectx = $here . "\n";
3492cf655043SAndy Whitcroft				my $ln = $linenr - 1;
349313214adfSAndy Whitcroft				for my $chunk (@chunks) {
349413214adfSAndy Whitcroft					my ($cond, $block) = @{$chunk};
349513214adfSAndy Whitcroft
3496773647a0SAndy Whitcroft					# If the condition carries leading newlines, then count those as offsets.
3497773647a0SAndy Whitcroft					my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
3498773647a0SAndy Whitcroft					my $offset = statement_rawlines($whitespace) - 1;
3499773647a0SAndy Whitcroft
3500aad4f614SJoe Perches					$allowed[$allow] = 0;
3501773647a0SAndy Whitcroft					#print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
3502773647a0SAndy Whitcroft
3503773647a0SAndy Whitcroft					# We have looked at and allowed this specific line.
3504773647a0SAndy Whitcroft					$suppress_ifbraces{$ln + $offset} = 1;
3505773647a0SAndy Whitcroft
3506773647a0SAndy Whitcroft					$herectx .= "$rawlines[$ln + $offset]\n[...]\n";
3507cf655043SAndy Whitcroft					$ln += statement_rawlines($block) - 1;
3508cf655043SAndy Whitcroft
3509773647a0SAndy Whitcroft					substr($block, 0, length($cond), '');
351013214adfSAndy Whitcroft
351113214adfSAndy Whitcroft					$seen++ if ($block =~ /^\s*{/);
351213214adfSAndy Whitcroft
3513aad4f614SJoe Perches					#print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
3514cf655043SAndy Whitcroft					if (statement_lines($cond) > 1) {
3515cf655043SAndy Whitcroft						#print "APW: ALLOWED: cond<$cond>\n";
3516aad4f614SJoe Perches						$allowed[$allow] = 1;
351713214adfSAndy Whitcroft					}
351813214adfSAndy Whitcroft					if ($block =~/\b(?:if|for|while)\b/) {
3519cf655043SAndy Whitcroft						#print "APW: ALLOWED: block<$block>\n";
3520aad4f614SJoe Perches						$allowed[$allow] = 1;
352113214adfSAndy Whitcroft					}
3522cf655043SAndy Whitcroft					if (statement_block_size($block) > 1) {
3523cf655043SAndy Whitcroft						#print "APW: ALLOWED: lines block<$block>\n";
3524aad4f614SJoe Perches						$allowed[$allow] = 1;
352513214adfSAndy Whitcroft					}
3526aad4f614SJoe Perches					$allow++;
352713214adfSAndy Whitcroft				}
3528aad4f614SJoe Perches				if ($seen) {
3529aad4f614SJoe Perches					my $sum_allowed = 0;
3530aad4f614SJoe Perches					foreach (@allowed) {
3531aad4f614SJoe Perches						$sum_allowed += $_;
3532aad4f614SJoe Perches					}
3533aad4f614SJoe Perches					if ($sum_allowed == 0) {
3534000d1cc1SJoe Perches						WARN("BRACES",
3535000d1cc1SJoe Perches						     "braces {} are not necessary for any arm of this statement\n" . $herectx);
3536aad4f614SJoe Perches					} elsif ($sum_allowed != $allow &&
3537aad4f614SJoe Perches						 $seen != $allow) {
3538aad4f614SJoe Perches						CHK("BRACES",
3539aad4f614SJoe Perches						    "braces {} should be used on all arms of this statement\n" . $herectx);
3540aad4f614SJoe Perches					}
354113214adfSAndy Whitcroft				}
354213214adfSAndy Whitcroft			}
354313214adfSAndy Whitcroft		}
3544773647a0SAndy Whitcroft		if (!defined $suppress_ifbraces{$linenr - 1} &&
354513214adfSAndy Whitcroft					$line =~ /\b(if|while|for|else)\b/) {
3546cf655043SAndy Whitcroft			my $allowed = 0;
3547f0a594c1SAndy Whitcroft
3548cf655043SAndy Whitcroft			# Check the pre-context.
3549cf655043SAndy Whitcroft			if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
3550cf655043SAndy Whitcroft				#print "APW: ALLOWED: pre<$1>\n";
3551cf655043SAndy Whitcroft				$allowed = 1;
3552f0a594c1SAndy Whitcroft			}
3553773647a0SAndy Whitcroft
3554773647a0SAndy Whitcroft			my ($level, $endln, @chunks) =
3555773647a0SAndy Whitcroft				ctx_statement_full($linenr, $realcnt, $-[0]);
3556773647a0SAndy Whitcroft
3557cf655043SAndy Whitcroft			# Check the condition.
3558cf655043SAndy Whitcroft			my ($cond, $block) = @{$chunks[0]};
3559773647a0SAndy Whitcroft			#print "CHECKING<$linenr> cond<$cond> block<$block>\n";
3560cf655043SAndy Whitcroft			if (defined $cond) {
3561773647a0SAndy Whitcroft				substr($block, 0, length($cond), '');
3562cf655043SAndy Whitcroft			}
3563cf655043SAndy Whitcroft			if (statement_lines($cond) > 1) {
3564cf655043SAndy Whitcroft				#print "APW: ALLOWED: cond<$cond>\n";
3565cf655043SAndy Whitcroft				$allowed = 1;
3566cf655043SAndy Whitcroft			}
3567cf655043SAndy Whitcroft			if ($block =~/\b(?:if|for|while)\b/) {
3568cf655043SAndy Whitcroft				#print "APW: ALLOWED: block<$block>\n";
3569cf655043SAndy Whitcroft				$allowed = 1;
3570cf655043SAndy Whitcroft			}
3571cf655043SAndy Whitcroft			if (statement_block_size($block) > 1) {
3572cf655043SAndy Whitcroft				#print "APW: ALLOWED: lines block<$block>\n";
3573cf655043SAndy Whitcroft				$allowed = 1;
3574cf655043SAndy Whitcroft			}
3575cf655043SAndy Whitcroft			# Check the post-context.
3576cf655043SAndy Whitcroft			if (defined $chunks[1]) {
3577cf655043SAndy Whitcroft				my ($cond, $block) = @{$chunks[1]};
3578cf655043SAndy Whitcroft				if (defined $cond) {
3579773647a0SAndy Whitcroft					substr($block, 0, length($cond), '');
3580cf655043SAndy Whitcroft				}
3581cf655043SAndy Whitcroft				if ($block =~ /^\s*\{/) {
3582cf655043SAndy Whitcroft					#print "APW: ALLOWED: chunk-1 block<$block>\n";
3583cf655043SAndy Whitcroft					$allowed = 1;
3584cf655043SAndy Whitcroft				}
3585cf655043SAndy Whitcroft			}
3586cf655043SAndy Whitcroft			if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
358769932487SJustin P. Mattock				my $herectx = $here . "\n";
3588f055663cSAndy Whitcroft				my $cnt = statement_rawlines($block);
3589cf655043SAndy Whitcroft
3590f055663cSAndy Whitcroft				for (my $n = 0; $n < $cnt; $n++) {
359169932487SJustin P. Mattock					$herectx .= raw_line($linenr, $n) . "\n";
3592cf655043SAndy Whitcroft				}
3593cf655043SAndy Whitcroft
3594000d1cc1SJoe Perches				WARN("BRACES",
3595000d1cc1SJoe Perches				     "braces {} are not necessary for single statement blocks\n" . $herectx);
3596f0a594c1SAndy Whitcroft			}
3597f0a594c1SAndy Whitcroft		}
3598f0a594c1SAndy Whitcroft
35990979ae66SJoe Perches# check for unnecessary blank lines around braces
360077b9a53aSJoe Perches		if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
36010979ae66SJoe Perches			CHK("BRACES",
36020979ae66SJoe Perches			    "Blank lines aren't necessary before a close brace '}'\n" . $hereprev);
36030979ae66SJoe Perches		}
360477b9a53aSJoe Perches		if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
36050979ae66SJoe Perches			CHK("BRACES",
36060979ae66SJoe Perches			    "Blank lines aren't necessary after an open brace '{'\n" . $hereprev);
36070979ae66SJoe Perches		}
36080979ae66SJoe Perches
36094a0df2efSAndy Whitcroft# no volatiles please
36106c72ffaaSAndy Whitcroft		my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
36116c72ffaaSAndy Whitcroft		if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
3612000d1cc1SJoe Perches			WARN("VOLATILE",
3613000d1cc1SJoe Perches			     "Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
36144a0df2efSAndy Whitcroft		}
36154a0df2efSAndy Whitcroft
361600df344fSAndy Whitcroft# warn about #if 0
3617c45dcabdSAndy Whitcroft		if ($line =~ /^.\s*\#\s*if\s+0\b/) {
3618000d1cc1SJoe Perches			CHK("REDUNDANT_CODE",
3619000d1cc1SJoe Perches			    "if this code is redundant consider removing it\n" .
3620de7d4f0eSAndy Whitcroft				$herecurr);
36214a0df2efSAndy Whitcroft		}
36224a0df2efSAndy Whitcroft
362303df4b51SAndy Whitcroft# check for needless "if (<foo>) fn(<foo>)" uses
362403df4b51SAndy Whitcroft		if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
362503df4b51SAndy Whitcroft			my $expr = '\s*\(\s*' . quotemeta($1) . '\s*\)\s*;';
362603df4b51SAndy Whitcroft			if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?)$expr/) {
362703df4b51SAndy Whitcroft				WARN('NEEDLESS_IF',
362803df4b51SAndy Whitcroft				     "$1(NULL) is safe this check is probably not required\n" . $hereprev);
36294c432a8fSGreg Kroah-Hartman			}
36304c432a8fSGreg Kroah-Hartman		}
3631f0a594c1SAndy Whitcroft
36321a15a250SPatrick Pannuto# prefer usleep_range over udelay
363337581c28SBruce Allan		if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
36341a15a250SPatrick Pannuto			# ignore udelay's < 10, however
363537581c28SBruce Allan			if (! ($1 < 10) ) {
3636000d1cc1SJoe Perches				CHK("USLEEP_RANGE",
3637000d1cc1SJoe Perches				    "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $line);
36381a15a250SPatrick Pannuto			}
36391a15a250SPatrick Pannuto		}
36401a15a250SPatrick Pannuto
364109ef8725SPatrick Pannuto# warn about unexpectedly long msleep's
364209ef8725SPatrick Pannuto		if ($line =~ /\bmsleep\s*\((\d+)\);/) {
364309ef8725SPatrick Pannuto			if ($1 < 20) {
3644000d1cc1SJoe Perches				WARN("MSLEEP",
3645000d1cc1SJoe Perches				     "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $line);
364609ef8725SPatrick Pannuto			}
364709ef8725SPatrick Pannuto		}
364809ef8725SPatrick Pannuto
364936ec1939SJoe Perches# check for comparisons of jiffies
365036ec1939SJoe Perches		if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
365136ec1939SJoe Perches			WARN("JIFFIES_COMPARISON",
365236ec1939SJoe Perches			     "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
365336ec1939SJoe Perches		}
365436ec1939SJoe Perches
36559d7a34a5SJoe Perches# check for comparisons of get_jiffies_64()
36569d7a34a5SJoe Perches		if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
36579d7a34a5SJoe Perches			WARN("JIFFIES_COMPARISON",
36589d7a34a5SJoe Perches			     "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
36599d7a34a5SJoe Perches		}
36609d7a34a5SJoe Perches
366100df344fSAndy Whitcroft# warn about #ifdefs in C files
3662c45dcabdSAndy Whitcroft#		if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
366300df344fSAndy Whitcroft#			print "#ifdef in C files should be avoided\n";
366400df344fSAndy Whitcroft#			print "$herecurr";
366500df344fSAndy Whitcroft#			$clean = 0;
366600df344fSAndy Whitcroft#		}
366700df344fSAndy Whitcroft
366822f2a2efSAndy Whitcroft# warn about spacing in #ifdefs
3669c45dcabdSAndy Whitcroft		if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
36703705ce5bSJoe Perches			if (ERROR("SPACING",
36713705ce5bSJoe Perches				  "exactly one space required after that #$1\n" . $herecurr) &&
36723705ce5bSJoe Perches			    $fix) {
36733705ce5bSJoe Perches				$fixed[$linenr - 1] =~
36743705ce5bSJoe Perches				    s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
36753705ce5bSJoe Perches			}
36763705ce5bSJoe Perches
367722f2a2efSAndy Whitcroft		}
367822f2a2efSAndy Whitcroft
36794a0df2efSAndy Whitcroft# check for spinlock_t definitions without a comment.
3680171ae1a4SAndy Whitcroft		if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
3681171ae1a4SAndy Whitcroft		    $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
36824a0df2efSAndy Whitcroft			my $which = $1;
36834a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
3684000d1cc1SJoe Perches				CHK("UNCOMMENTED_DEFINITION",
3685000d1cc1SJoe Perches				    "$1 definition without comment\n" . $herecurr);
36864a0df2efSAndy Whitcroft			}
36874a0df2efSAndy Whitcroft		}
36884a0df2efSAndy Whitcroft# check for memory barriers without a comment.
36894a0df2efSAndy Whitcroft		if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
36904a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
3691000d1cc1SJoe Perches				CHK("MEMORY_BARRIER",
3692000d1cc1SJoe Perches				    "memory barrier without comment\n" . $herecurr);
36934a0df2efSAndy Whitcroft			}
36944a0df2efSAndy Whitcroft		}
36954a0df2efSAndy Whitcroft# check of hardware specific defines
3696c45dcabdSAndy Whitcroft		if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
3697000d1cc1SJoe Perches			CHK("ARCH_DEFINES",
3698000d1cc1SJoe Perches			    "architecture specific defines should be avoided\n" .  $herecurr);
36990a920b5bSAndy Whitcroft		}
3700653d4876SAndy Whitcroft
3701d4977c78STobias Klauser# Check that the storage class is at the beginning of a declaration
3702d4977c78STobias Klauser		if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
3703000d1cc1SJoe Perches			WARN("STORAGE_CLASS",
3704000d1cc1SJoe Perches			     "storage class should be at the beginning of the declaration\n" . $herecurr)
3705d4977c78STobias Klauser		}
3706d4977c78STobias Klauser
3707de7d4f0eSAndy Whitcroft# check the location of the inline attribute, that it is between
3708de7d4f0eSAndy Whitcroft# storage class and type.
37099c0ca6f9SAndy Whitcroft		if ($line =~ /\b$Type\s+$Inline\b/ ||
37109c0ca6f9SAndy Whitcroft		    $line =~ /\b$Inline\s+$Storage\b/) {
3711000d1cc1SJoe Perches			ERROR("INLINE_LOCATION",
3712000d1cc1SJoe Perches			      "inline keyword should sit between storage class and type\n" . $herecurr);
3713de7d4f0eSAndy Whitcroft		}
3714de7d4f0eSAndy Whitcroft
37158905a67cSAndy Whitcroft# Check for __inline__ and __inline, prefer inline
37168905a67cSAndy Whitcroft		if ($line =~ /\b(__inline__|__inline)\b/) {
3717*d5e616fcSJoe Perches			if (WARN("INLINE",
3718*d5e616fcSJoe Perches				 "plain inline is preferred over $1\n" . $herecurr) &&
3719*d5e616fcSJoe Perches			    $fix) {
3720*d5e616fcSJoe Perches				$fixed[$linenr - 1] =~ s/\b(__inline__|__inline)\b/inline/;
3721*d5e616fcSJoe Perches
3722*d5e616fcSJoe Perches			}
37238905a67cSAndy Whitcroft		}
37248905a67cSAndy Whitcroft
37253d130fd0SJoe Perches# Check for __attribute__ packed, prefer __packed
37263d130fd0SJoe Perches		if ($line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
3727000d1cc1SJoe Perches			WARN("PREFER_PACKED",
3728000d1cc1SJoe Perches			     "__packed is preferred over __attribute__((packed))\n" . $herecurr);
37293d130fd0SJoe Perches		}
37303d130fd0SJoe Perches
373139b7e287SJoe Perches# Check for __attribute__ aligned, prefer __aligned
373239b7e287SJoe Perches		if ($line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
3733000d1cc1SJoe Perches			WARN("PREFER_ALIGNED",
3734000d1cc1SJoe Perches			     "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
373539b7e287SJoe Perches		}
373639b7e287SJoe Perches
37375f14d3bdSJoe Perches# Check for __attribute__ format(printf, prefer __printf
37385f14d3bdSJoe Perches		if ($line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
3739*d5e616fcSJoe Perches			if (WARN("PREFER_PRINTF",
3740*d5e616fcSJoe Perches				 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) &&
3741*d5e616fcSJoe Perches			    $fix) {
3742*d5e616fcSJoe Perches				$fixed[$linenr - 1] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf\s*,\s*(.*)\)\s*\)\s*\)/"__printf(" . trim($1) . ")"/ex;
3743*d5e616fcSJoe Perches
3744*d5e616fcSJoe Perches			}
37455f14d3bdSJoe Perches		}
37465f14d3bdSJoe Perches
37476061d949SJoe Perches# Check for __attribute__ format(scanf, prefer __scanf
37486061d949SJoe Perches		if ($line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
3749*d5e616fcSJoe Perches			if (WARN("PREFER_SCANF",
3750*d5e616fcSJoe Perches				 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) &&
3751*d5e616fcSJoe Perches			    $fix) {
3752*d5e616fcSJoe Perches				$fixed[$linenr - 1] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\s*,\s*(.*)\)\s*\)\s*\)/"__scanf(" . trim($1) . ")"/ex;
3753*d5e616fcSJoe Perches			}
37546061d949SJoe Perches		}
37556061d949SJoe Perches
37568f53a9b8SJoe Perches# check for sizeof(&)
37578f53a9b8SJoe Perches		if ($line =~ /\bsizeof\s*\(\s*\&/) {
3758000d1cc1SJoe Perches			WARN("SIZEOF_ADDRESS",
3759000d1cc1SJoe Perches			     "sizeof(& should be avoided\n" . $herecurr);
37608f53a9b8SJoe Perches		}
37618f53a9b8SJoe Perches
376266c80b60SJoe Perches# check for sizeof without parenthesis
376366c80b60SJoe Perches		if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
3764*d5e616fcSJoe Perches			if (WARN("SIZEOF_PARENTHESIS",
3765*d5e616fcSJoe Perches				 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
3766*d5e616fcSJoe Perches			    $fix) {
3767*d5e616fcSJoe Perches				$fixed[$linenr - 1] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
3768*d5e616fcSJoe Perches			}
376966c80b60SJoe Perches		}
377066c80b60SJoe Perches
3771428e2fdcSJoe Perches# check for line continuations in quoted strings with odd counts of "
3772428e2fdcSJoe Perches		if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) {
3773000d1cc1SJoe Perches			WARN("LINE_CONTINUATIONS",
3774000d1cc1SJoe Perches			     "Avoid line continuations in quoted strings\n" . $herecurr);
3775428e2fdcSJoe Perches		}
3776428e2fdcSJoe Perches
377788982feaSJoe Perches# check for struct spinlock declarations
377888982feaSJoe Perches		if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
377988982feaSJoe Perches			WARN("USE_SPINLOCK_T",
378088982feaSJoe Perches			     "struct spinlock should be spinlock_t\n" . $herecurr);
378188982feaSJoe Perches		}
378288982feaSJoe Perches
3783a6962d72SJoe Perches# check for seq_printf uses that could be seq_puts
3784a6962d72SJoe Perches		if ($line =~ /\bseq_printf\s*\(/) {
3785a6962d72SJoe Perches			my $fmt = get_quoted_string($line, $rawline);
3786a6962d72SJoe Perches			if ($fmt !~ /[^\\]\%/) {
3787*d5e616fcSJoe Perches				if (WARN("PREFER_SEQ_PUTS",
3788*d5e616fcSJoe Perches					 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
3789*d5e616fcSJoe Perches				    $fix) {
3790*d5e616fcSJoe Perches					$fixed[$linenr - 1] =~ s/\bseq_printf\b/seq_puts/;
3791*d5e616fcSJoe Perches				}
3792a6962d72SJoe Perches			}
3793a6962d72SJoe Perches		}
3794a6962d72SJoe Perches
3795554e165cSAndy Whitcroft# Check for misused memsets
3796d1fe9c09SJoe Perches		if ($^V && $^V ge 5.10.0 &&
3797d1fe9c09SJoe Perches		    defined $stat &&
3798d7c76ba7SJoe Perches		    $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/s) {
3799554e165cSAndy Whitcroft
3800d7c76ba7SJoe Perches			my $ms_addr = $2;
3801d1fe9c09SJoe Perches			my $ms_val = $7;
3802d1fe9c09SJoe Perches			my $ms_size = $12;
3803d7c76ba7SJoe Perches
3804554e165cSAndy Whitcroft			if ($ms_size =~ /^(0x|)0$/i) {
3805554e165cSAndy Whitcroft				ERROR("MEMSET",
3806d7c76ba7SJoe Perches				      "memset to 0's uses 0 as the 2nd argument, not the 3rd\n" . "$here\n$stat\n");
3807554e165cSAndy Whitcroft			} elsif ($ms_size =~ /^(0x|)1$/i) {
3808554e165cSAndy Whitcroft				WARN("MEMSET",
3809d7c76ba7SJoe Perches				     "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
3810d7c76ba7SJoe Perches			}
3811d7c76ba7SJoe Perches		}
3812d7c76ba7SJoe Perches
3813d7c76ba7SJoe Perches# typecasts on min/max could be min_t/max_t
3814d1fe9c09SJoe Perches		if ($^V && $^V ge 5.10.0 &&
3815d1fe9c09SJoe Perches		    defined $stat &&
3816d7c76ba7SJoe Perches		    $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
3817d1fe9c09SJoe Perches			if (defined $2 || defined $7) {
3818d7c76ba7SJoe Perches				my $call = $1;
3819d7c76ba7SJoe Perches				my $cast1 = deparenthesize($2);
3820d7c76ba7SJoe Perches				my $arg1 = $3;
3821d1fe9c09SJoe Perches				my $cast2 = deparenthesize($7);
3822d1fe9c09SJoe Perches				my $arg2 = $8;
3823d7c76ba7SJoe Perches				my $cast;
3824d7c76ba7SJoe Perches
3825d1fe9c09SJoe Perches				if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
3826d7c76ba7SJoe Perches					$cast = "$cast1 or $cast2";
3827d7c76ba7SJoe Perches				} elsif ($cast1 ne "") {
3828d7c76ba7SJoe Perches					$cast = $cast1;
3829d7c76ba7SJoe Perches				} else {
3830d7c76ba7SJoe Perches					$cast = $cast2;
3831d7c76ba7SJoe Perches				}
3832d7c76ba7SJoe Perches				WARN("MINMAX",
3833d7c76ba7SJoe Perches				     "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
3834554e165cSAndy Whitcroft			}
3835554e165cSAndy Whitcroft		}
3836554e165cSAndy Whitcroft
38374a273195SJoe Perches# check usleep_range arguments
38384a273195SJoe Perches		if ($^V && $^V ge 5.10.0 &&
38394a273195SJoe Perches		    defined $stat &&
38404a273195SJoe Perches		    $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
38414a273195SJoe Perches			my $min = $1;
38424a273195SJoe Perches			my $max = $7;
38434a273195SJoe Perches			if ($min eq $max) {
38444a273195SJoe Perches				WARN("USLEEP_RANGE",
38454a273195SJoe Perches				     "usleep_range should not use min == max args; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
38464a273195SJoe Perches			} elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
38474a273195SJoe Perches				 $min > $max) {
38484a273195SJoe Perches				WARN("USLEEP_RANGE",
38494a273195SJoe Perches				     "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
38504a273195SJoe Perches			}
38514a273195SJoe Perches		}
38524a273195SJoe Perches
3853de7d4f0eSAndy Whitcroft# check for new externs in .c files.
3854171ae1a4SAndy Whitcroft		if ($realfile =~ /\.c$/ && defined $stat &&
3855c45dcabdSAndy Whitcroft		    $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
3856171ae1a4SAndy Whitcroft		{
3857c45dcabdSAndy Whitcroft			my $function_name = $1;
3858c45dcabdSAndy Whitcroft			my $paren_space = $2;
3859171ae1a4SAndy Whitcroft
3860171ae1a4SAndy Whitcroft			my $s = $stat;
3861171ae1a4SAndy Whitcroft			if (defined $cond) {
3862171ae1a4SAndy Whitcroft				substr($s, 0, length($cond), '');
3863171ae1a4SAndy Whitcroft			}
3864c45dcabdSAndy Whitcroft			if ($s =~ /^\s*;/ &&
3865c45dcabdSAndy Whitcroft			    $function_name ne 'uninitialized_var')
3866c45dcabdSAndy Whitcroft			{
3867000d1cc1SJoe Perches				WARN("AVOID_EXTERNS",
3868000d1cc1SJoe Perches				     "externs should be avoided in .c files\n" .  $herecurr);
3869de7d4f0eSAndy Whitcroft			}
3870de7d4f0eSAndy Whitcroft
3871171ae1a4SAndy Whitcroft			if ($paren_space =~ /\n/) {
3872000d1cc1SJoe Perches				WARN("FUNCTION_ARGUMENTS",
3873000d1cc1SJoe Perches				     "arguments for function declarations should follow identifier\n" . $herecurr);
3874171ae1a4SAndy Whitcroft			}
38759c9ba34eSAndy Whitcroft
38769c9ba34eSAndy Whitcroft		} elsif ($realfile =~ /\.c$/ && defined $stat &&
38779c9ba34eSAndy Whitcroft		    $stat =~ /^.\s*extern\s+/)
38789c9ba34eSAndy Whitcroft		{
3879000d1cc1SJoe Perches			WARN("AVOID_EXTERNS",
3880000d1cc1SJoe Perches			     "externs should be avoided in .c files\n" .  $herecurr);
3881171ae1a4SAndy Whitcroft		}
3882171ae1a4SAndy Whitcroft
3883de7d4f0eSAndy Whitcroft# checks for new __setup's
3884de7d4f0eSAndy Whitcroft		if ($rawline =~ /\b__setup\("([^"]*)"/) {
3885de7d4f0eSAndy Whitcroft			my $name = $1;
3886de7d4f0eSAndy Whitcroft
3887de7d4f0eSAndy Whitcroft			if (!grep(/$name/, @setup_docs)) {
3888000d1cc1SJoe Perches				CHK("UNDOCUMENTED_SETUP",
3889000d1cc1SJoe Perches				    "__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
3890de7d4f0eSAndy Whitcroft			}
3891653d4876SAndy Whitcroft		}
38929c0ca6f9SAndy Whitcroft
38939c0ca6f9SAndy Whitcroft# check for pointless casting of kmalloc return
3894caf2a54fSJoe Perches		if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) {
3895000d1cc1SJoe Perches			WARN("UNNECESSARY_CASTS",
3896000d1cc1SJoe Perches			     "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
38979c0ca6f9SAndy Whitcroft		}
389813214adfSAndy Whitcroft
3899a640d25cSJoe Perches# alloc style
3900a640d25cSJoe Perches# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
3901a640d25cSJoe Perches		if ($^V && $^V ge 5.10.0 &&
3902a640d25cSJoe Perches		    $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*([kv][mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
3903a640d25cSJoe Perches			CHK("ALLOC_SIZEOF_STRUCT",
3904a640d25cSJoe Perches			    "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
3905a640d25cSJoe Perches		}
3906a640d25cSJoe Perches
3907972fdea2SJoe Perches# check for krealloc arg reuse
3908972fdea2SJoe Perches		if ($^V && $^V ge 5.10.0 &&
3909972fdea2SJoe Perches		    $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*\1\s*,/) {
3910972fdea2SJoe Perches			WARN("KREALLOC_ARG_REUSE",
3911972fdea2SJoe Perches			     "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
3912972fdea2SJoe Perches		}
3913972fdea2SJoe Perches
39145ce59ae0SJoe Perches# check for alloc argument mismatch
39155ce59ae0SJoe Perches		if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
39165ce59ae0SJoe Perches			WARN("ALLOC_ARRAY_ARGS",
39175ce59ae0SJoe Perches			     "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
39185ce59ae0SJoe Perches		}
39195ce59ae0SJoe Perches
3920caf2a54fSJoe Perches# check for multiple semicolons
3921caf2a54fSJoe Perches		if ($line =~ /;\s*;\s*$/) {
3922*d5e616fcSJoe Perches			if (WARN("ONE_SEMICOLON",
3923*d5e616fcSJoe Perches				 "Statements terminations use 1 semicolon\n" . $herecurr) &&
3924*d5e616fcSJoe Perches			    $fix) {
3925*d5e616fcSJoe Perches				$fixed[$linenr - 1] =~ s/(\s*;\s*){2,}$/;/g;
3926*d5e616fcSJoe Perches			}
3927d1e2ad07SJoe Perches		}
3928d1e2ad07SJoe Perches
3929d1e2ad07SJoe Perches# check for switch/default statements without a break;
3930d1e2ad07SJoe Perches		if ($^V && $^V ge 5.10.0 &&
3931d1e2ad07SJoe Perches		    defined $stat &&
3932d1e2ad07SJoe Perches		    $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
3933d1e2ad07SJoe Perches			my $ctx = '';
3934d1e2ad07SJoe Perches			my $herectx = $here . "\n";
3935d1e2ad07SJoe Perches			my $cnt = statement_rawlines($stat);
3936d1e2ad07SJoe Perches			for (my $n = 0; $n < $cnt; $n++) {
3937d1e2ad07SJoe Perches				$herectx .= raw_line($linenr, $n) . "\n";
3938d1e2ad07SJoe Perches			}
3939d1e2ad07SJoe Perches			WARN("DEFAULT_NO_BREAK",
3940d1e2ad07SJoe Perches			     "switch default: should use break\n" . $herectx);
3941caf2a54fSJoe Perches		}
3942caf2a54fSJoe Perches
394313214adfSAndy Whitcroft# check for gcc specific __FUNCTION__
3944*d5e616fcSJoe Perches		if ($line =~ /\b__FUNCTION__\b/) {
3945*d5e616fcSJoe Perches			if (WARN("USE_FUNC",
3946*d5e616fcSJoe Perches				 "__func__ should be used instead of gcc specific __FUNCTION__\n"  . $herecurr) &&
3947*d5e616fcSJoe Perches			    $fix) {
3948*d5e616fcSJoe Perches				$fixed[$linenr - 1] =~ s/\b__FUNCTION__\b/__func__/g;
3949*d5e616fcSJoe Perches			}
395013214adfSAndy Whitcroft		}
3951773647a0SAndy Whitcroft
39522c92488aSJoe Perches# check for use of yield()
39532c92488aSJoe Perches		if ($line =~ /\byield\s*\(\s*\)/) {
39542c92488aSJoe Perches			WARN("YIELD",
39552c92488aSJoe Perches			     "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n"  . $herecurr);
39562c92488aSJoe Perches		}
39572c92488aSJoe Perches
3958179f8f40SJoe Perches# check for comparisons against true and false
3959179f8f40SJoe Perches		if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
3960179f8f40SJoe Perches			my $lead = $1;
3961179f8f40SJoe Perches			my $arg = $2;
3962179f8f40SJoe Perches			my $test = $3;
3963179f8f40SJoe Perches			my $otype = $4;
3964179f8f40SJoe Perches			my $trail = $5;
3965179f8f40SJoe Perches			my $op = "!";
3966179f8f40SJoe Perches
3967179f8f40SJoe Perches			($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
3968179f8f40SJoe Perches
3969179f8f40SJoe Perches			my $type = lc($otype);
3970179f8f40SJoe Perches			if ($type =~ /^(?:true|false)$/) {
3971179f8f40SJoe Perches				if (("$test" eq "==" && "$type" eq "true") ||
3972179f8f40SJoe Perches				    ("$test" eq "!=" && "$type" eq "false")) {
3973179f8f40SJoe Perches					$op = "";
3974179f8f40SJoe Perches				}
3975179f8f40SJoe Perches
3976179f8f40SJoe Perches				CHK("BOOL_COMPARISON",
3977179f8f40SJoe Perches				    "Using comparison to $otype is error prone\n" . $herecurr);
3978179f8f40SJoe Perches
3979179f8f40SJoe Perches## maybe suggesting a correct construct would better
3980179f8f40SJoe Perches##				    "Using comparison to $otype is error prone.  Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
3981179f8f40SJoe Perches
3982179f8f40SJoe Perches			}
3983179f8f40SJoe Perches		}
3984179f8f40SJoe Perches
39854882720bSThomas Gleixner# check for semaphores initialized locked
39864882720bSThomas Gleixner		if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
3987000d1cc1SJoe Perches			WARN("CONSIDER_COMPLETION",
3988000d1cc1SJoe Perches			     "consider using a completion\n" . $herecurr);
3989773647a0SAndy Whitcroft		}
39906712d858SJoe Perches
399167d0a075SJoe Perches# recommend kstrto* over simple_strto* and strict_strto*
399267d0a075SJoe Perches		if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
3993000d1cc1SJoe Perches			WARN("CONSIDER_KSTRTO",
399467d0a075SJoe Perches			     "$1 is obsolete, use k$3 instead\n" . $herecurr);
3995773647a0SAndy Whitcroft		}
39966712d858SJoe Perches
3997f3db6639SMichael Ellerman# check for __initcall(), use device_initcall() explicitly please
3998f3db6639SMichael Ellerman		if ($line =~ /^.\s*__initcall\s*\(/) {
3999000d1cc1SJoe Perches			WARN("USE_DEVICE_INITCALL",
4000000d1cc1SJoe Perches			     "please use device_initcall() instead of __initcall()\n" . $herecurr);
4001f3db6639SMichael Ellerman		}
40026712d858SJoe Perches
400379404849SEmese Revfy# check for various ops structs, ensure they are const.
400479404849SEmese Revfy		my $struct_ops = qr{acpi_dock_ops|
400579404849SEmese Revfy				address_space_operations|
400679404849SEmese Revfy				backlight_ops|
400779404849SEmese Revfy				block_device_operations|
400879404849SEmese Revfy				dentry_operations|
400979404849SEmese Revfy				dev_pm_ops|
401079404849SEmese Revfy				dma_map_ops|
401179404849SEmese Revfy				extent_io_ops|
401279404849SEmese Revfy				file_lock_operations|
401379404849SEmese Revfy				file_operations|
401479404849SEmese Revfy				hv_ops|
401579404849SEmese Revfy				ide_dma_ops|
401679404849SEmese Revfy				intel_dvo_dev_ops|
401779404849SEmese Revfy				item_operations|
401879404849SEmese Revfy				iwl_ops|
401979404849SEmese Revfy				kgdb_arch|
402079404849SEmese Revfy				kgdb_io|
402179404849SEmese Revfy				kset_uevent_ops|
402279404849SEmese Revfy				lock_manager_operations|
402379404849SEmese Revfy				microcode_ops|
402479404849SEmese Revfy				mtrr_ops|
402579404849SEmese Revfy				neigh_ops|
402679404849SEmese Revfy				nlmsvc_binding|
402779404849SEmese Revfy				pci_raw_ops|
402879404849SEmese Revfy				pipe_buf_operations|
402979404849SEmese Revfy				platform_hibernation_ops|
403079404849SEmese Revfy				platform_suspend_ops|
403179404849SEmese Revfy				proto_ops|
403279404849SEmese Revfy				rpc_pipe_ops|
403379404849SEmese Revfy				seq_operations|
403479404849SEmese Revfy				snd_ac97_build_ops|
403579404849SEmese Revfy				soc_pcmcia_socket_ops|
403679404849SEmese Revfy				stacktrace_ops|
403779404849SEmese Revfy				sysfs_ops|
403879404849SEmese Revfy				tty_operations|
403979404849SEmese Revfy				usb_mon_operations|
404079404849SEmese Revfy				wd_ops}x;
40416903ffb2SAndy Whitcroft		if ($line !~ /\bconst\b/ &&
404279404849SEmese Revfy		    $line =~ /\bstruct\s+($struct_ops)\b/) {
4043000d1cc1SJoe Perches			WARN("CONST_STRUCT",
4044000d1cc1SJoe Perches			     "struct $1 should normally be const\n" .
40456903ffb2SAndy Whitcroft				$herecurr);
40462b6db5cbSAndy Whitcroft		}
4047773647a0SAndy Whitcroft
4048773647a0SAndy Whitcroft# use of NR_CPUS is usually wrong
4049773647a0SAndy Whitcroft# ignore definitions of NR_CPUS and usage to define arrays as likely right
4050773647a0SAndy Whitcroft		if ($line =~ /\bNR_CPUS\b/ &&
4051c45dcabdSAndy Whitcroft		    $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
4052c45dcabdSAndy Whitcroft		    $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
4053171ae1a4SAndy Whitcroft		    $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
4054171ae1a4SAndy Whitcroft		    $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
4055171ae1a4SAndy Whitcroft		    $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
4056773647a0SAndy Whitcroft		{
4057000d1cc1SJoe Perches			WARN("NR_CPUS",
4058000d1cc1SJoe Perches			     "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
4059773647a0SAndy Whitcroft		}
40609c9ba34eSAndy Whitcroft
40619c9ba34eSAndy Whitcroft# check for %L{u,d,i} in strings
40629c9ba34eSAndy Whitcroft		my $string;
40639c9ba34eSAndy Whitcroft		while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
40649c9ba34eSAndy Whitcroft			$string = substr($rawline, $-[1], $+[1] - $-[1]);
40652a1bc5d5SAndy Whitcroft			$string =~ s/%%/__/g;
40669c9ba34eSAndy Whitcroft			if ($string =~ /(?<!%)%L[udi]/) {
4067000d1cc1SJoe Perches				WARN("PRINTF_L",
4068000d1cc1SJoe Perches				     "\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
40699c9ba34eSAndy Whitcroft				last;
40709c9ba34eSAndy Whitcroft			}
40719c9ba34eSAndy Whitcroft		}
4072691d77b6SAndy Whitcroft
4073691d77b6SAndy Whitcroft# whine mightly about in_atomic
4074691d77b6SAndy Whitcroft		if ($line =~ /\bin_atomic\s*\(/) {
4075691d77b6SAndy Whitcroft			if ($realfile =~ m@^drivers/@) {
4076000d1cc1SJoe Perches				ERROR("IN_ATOMIC",
4077000d1cc1SJoe Perches				      "do not use in_atomic in drivers\n" . $herecurr);
4078f4a87736SAndy Whitcroft			} elsif ($realfile !~ m@^kernel/@) {
4079000d1cc1SJoe Perches				WARN("IN_ATOMIC",
4080000d1cc1SJoe Perches				     "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
4081691d77b6SAndy Whitcroft			}
4082691d77b6SAndy Whitcroft		}
40831704f47bSPeter Zijlstra
40841704f47bSPeter Zijlstra# check for lockdep_set_novalidate_class
40851704f47bSPeter Zijlstra		if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
40861704f47bSPeter Zijlstra		    $line =~ /__lockdep_no_validate__\s*\)/ ) {
40871704f47bSPeter Zijlstra			if ($realfile !~ m@^kernel/lockdep@ &&
40881704f47bSPeter Zijlstra			    $realfile !~ m@^include/linux/lockdep@ &&
40891704f47bSPeter Zijlstra			    $realfile !~ m@^drivers/base/core@) {
4090000d1cc1SJoe Perches				ERROR("LOCKDEP",
4091000d1cc1SJoe Perches				      "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
40921704f47bSPeter Zijlstra			}
40931704f47bSPeter Zijlstra		}
409488f8831cSDave Jones
409588f8831cSDave Jones		if ($line =~ /debugfs_create_file.*S_IWUGO/ ||
409688f8831cSDave Jones		    $line =~ /DEVICE_ATTR.*S_IWUGO/ ) {
4097000d1cc1SJoe Perches			WARN("EXPORTED_WORLD_WRITABLE",
4098000d1cc1SJoe Perches			     "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
409988f8831cSDave Jones		}
410013214adfSAndy Whitcroft	}
410113214adfSAndy Whitcroft
410213214adfSAndy Whitcroft	# If we have no input at all, then there is nothing to report on
410313214adfSAndy Whitcroft	# so just keep quiet.
410413214adfSAndy Whitcroft	if ($#rawlines == -1) {
410513214adfSAndy Whitcroft		exit(0);
41060a920b5bSAndy Whitcroft	}
41070a920b5bSAndy Whitcroft
41088905a67cSAndy Whitcroft	# In mailback mode only produce a report in the negative, for
41098905a67cSAndy Whitcroft	# things that appear to be patches.
41108905a67cSAndy Whitcroft	if ($mailback && ($clean == 1 || !$is_patch)) {
41118905a67cSAndy Whitcroft		exit(0);
41128905a67cSAndy Whitcroft	}
41138905a67cSAndy Whitcroft
41148905a67cSAndy Whitcroft	# This is not a patch, and we are are in 'no-patch' mode so
41158905a67cSAndy Whitcroft	# just keep quiet.
41168905a67cSAndy Whitcroft	if (!$chk_patch && !$is_patch) {
41178905a67cSAndy Whitcroft		exit(0);
41188905a67cSAndy Whitcroft	}
41198905a67cSAndy Whitcroft
41208905a67cSAndy Whitcroft	if (!$is_patch) {
4121000d1cc1SJoe Perches		ERROR("NOT_UNIFIED_DIFF",
4122000d1cc1SJoe Perches		      "Does not appear to be a unified-diff format patch\n");
41230a920b5bSAndy Whitcroft	}
41240a920b5bSAndy Whitcroft	if ($is_patch && $chk_signoff && $signoff == 0) {
4125000d1cc1SJoe Perches		ERROR("MISSING_SIGN_OFF",
4126000d1cc1SJoe Perches		      "Missing Signed-off-by: line(s)\n");
41270a920b5bSAndy Whitcroft	}
41280a920b5bSAndy Whitcroft
4129f0a594c1SAndy Whitcroft	print report_dump();
413013214adfSAndy Whitcroft	if ($summary && !($clean == 1 && $quiet == 1)) {
413113214adfSAndy Whitcroft		print "$filename " if ($summary_file);
41326c72ffaaSAndy Whitcroft		print "total: $cnt_error errors, $cnt_warn warnings, " .
41336c72ffaaSAndy Whitcroft			(($check)? "$cnt_chk checks, " : "") .
41346c72ffaaSAndy Whitcroft			"$cnt_lines lines checked\n";
41358905a67cSAndy Whitcroft		print "\n" if ($quiet == 0);
41366c72ffaaSAndy Whitcroft	}
41378905a67cSAndy Whitcroft
4138d2c0a235SAndy Whitcroft	if ($quiet == 0) {
4139d1fe9c09SJoe Perches
4140d1fe9c09SJoe Perches		if ($^V lt 5.10.0) {
4141d1fe9c09SJoe Perches			print("NOTE: perl $^V is not modern enough to detect all possible issues.\n");
4142d1fe9c09SJoe Perches			print("An upgrade to at least perl v5.10.0 is suggested.\n\n");
4143d1fe9c09SJoe Perches		}
4144d1fe9c09SJoe Perches
4145d2c0a235SAndy Whitcroft		# If there were whitespace errors which cleanpatch can fix
4146d2c0a235SAndy Whitcroft		# then suggest that.
4147d2c0a235SAndy Whitcroft		if ($rpt_cleaners) {
4148d2c0a235SAndy Whitcroft			print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n";
4149d2c0a235SAndy Whitcroft			print "      scripts/cleanfile\n\n";
4150b0781216SMike Frysinger			$rpt_cleaners = 0;
4151d2c0a235SAndy Whitcroft		}
4152d2c0a235SAndy Whitcroft	}
4153d2c0a235SAndy Whitcroft
415411232688SArtem Bityutskiy	if ($quiet == 0 && keys %ignore_type) {
4155000d1cc1SJoe Perches	    print "NOTE: Ignored message types:";
4156000d1cc1SJoe Perches	    foreach my $ignore (sort keys %ignore_type) {
4157000d1cc1SJoe Perches		print " $ignore";
4158000d1cc1SJoe Perches	    }
415911232688SArtem Bityutskiy	    print "\n\n";
4160000d1cc1SJoe Perches	}
4161000d1cc1SJoe Perches
41623705ce5bSJoe Perches	if ($clean == 0 && $fix && "@rawlines" ne "@fixed") {
41633705ce5bSJoe Perches		my $newfile = $filename . ".EXPERIMENTAL-checkpatch-fixes";
41643705ce5bSJoe Perches		my $linecount = 0;
41653705ce5bSJoe Perches		my $f;
41663705ce5bSJoe Perches
41673705ce5bSJoe Perches		open($f, '>', $newfile)
41683705ce5bSJoe Perches		    or die "$P: Can't open $newfile for write\n";
41693705ce5bSJoe Perches		foreach my $fixed_line (@fixed) {
41703705ce5bSJoe Perches			$linecount++;
41713705ce5bSJoe Perches			if ($file) {
41723705ce5bSJoe Perches				if ($linecount > 3) {
41733705ce5bSJoe Perches					$fixed_line =~ s/^\+//;
41743705ce5bSJoe Perches					print $f $fixed_line. "\n";
41753705ce5bSJoe Perches				}
41763705ce5bSJoe Perches			} else {
41773705ce5bSJoe Perches				print $f $fixed_line . "\n";
41783705ce5bSJoe Perches			}
41793705ce5bSJoe Perches		}
41803705ce5bSJoe Perches		close($f);
41813705ce5bSJoe Perches
41823705ce5bSJoe Perches		if (!$quiet) {
41833705ce5bSJoe Perches			print << "EOM";
41843705ce5bSJoe PerchesWrote EXPERIMENTAL --fix correction(s) to '$newfile'
41853705ce5bSJoe Perches
41863705ce5bSJoe PerchesDo _NOT_ trust the results written to this file.
41873705ce5bSJoe PerchesDo _NOT_ submit these changes without inspecting them for correctness.
41883705ce5bSJoe Perches
41893705ce5bSJoe PerchesThis EXPERIMENTAL file is simply a convenience to help rewrite patches.
41903705ce5bSJoe PerchesNo warranties, expressed or implied...
41913705ce5bSJoe Perches
41923705ce5bSJoe PerchesEOM
41933705ce5bSJoe Perches		}
41943705ce5bSJoe Perches	}
41953705ce5bSJoe Perches
41960a920b5bSAndy Whitcroft	if ($clean == 1 && $quiet == 0) {
4197c2fdda0dSAndy Whitcroft		print "$vname has no obvious style problems and is ready for submission.\n"
41980a920b5bSAndy Whitcroft	}
41990a920b5bSAndy Whitcroft	if ($clean == 0 && $quiet == 0) {
4200000d1cc1SJoe Perches		print << "EOM";
4201000d1cc1SJoe Perches$vname has style problems, please review.
4202000d1cc1SJoe Perches
4203000d1cc1SJoe PerchesIf any of these errors are false positives, please report
4204000d1cc1SJoe Perchesthem to the maintainer, see CHECKPATCH in MAINTAINERS.
4205000d1cc1SJoe PerchesEOM
42060a920b5bSAndy Whitcroft	}
420713214adfSAndy Whitcroft
42080a920b5bSAndy Whitcroft	return $clean;
42090a920b5bSAndy Whitcroft}
4210