xref: /linux-6.15/scripts/checkpatch.pl (revision 351b2a1f)
10a920b5bSAndy Whitcroft#!/usr/bin/perl -w
2dbf004d7SDave Jones# (c) 2001, Dave Jones. (the file handling bit)
300df344fSAndy Whitcroft# (c) 2005, Joel Schopp <[email protected]> (the ugly bit)
42a5a2c25SAndy Whitcroft# (c) 2007,2008, Andy Whitcroft <[email protected]> (new conditions, test suite)
5015830beSAndy Whitcroft# (c) 2008-2010 Andy Whitcroft <[email protected]>
60a920b5bSAndy Whitcroft# Licensed under the terms of the GNU GPL License version 2
70a920b5bSAndy Whitcroft
80a920b5bSAndy Whitcroftuse strict;
90a920b5bSAndy Whitcroft
100a920b5bSAndy Whitcroftmy $P = $0;
1100df344fSAndy Whitcroft$P =~ s@.*/@@g;
120a920b5bSAndy Whitcroft
13000d1cc1SJoe Perchesmy $V = '0.32';
140a920b5bSAndy Whitcroft
150a920b5bSAndy Whitcroftuse Getopt::Long qw(:config no_auto_abbrev);
160a920b5bSAndy Whitcroft
170a920b5bSAndy Whitcroftmy $quiet = 0;
180a920b5bSAndy Whitcroftmy $tree = 1;
190a920b5bSAndy Whitcroftmy $chk_signoff = 1;
200a920b5bSAndy Whitcroftmy $chk_patch = 1;
21773647a0SAndy Whitcroftmy $tst_only;
226c72ffaaSAndy Whitcroftmy $emacs = 0;
238905a67cSAndy Whitcroftmy $terse = 0;
246c72ffaaSAndy Whitcroftmy $file = 0;
256c72ffaaSAndy Whitcroftmy $check = 0;
268905a67cSAndy Whitcroftmy $summary = 1;
278905a67cSAndy Whitcroftmy $mailback = 0;
2813214adfSAndy Whitcroftmy $summary_file = 0;
29000d1cc1SJoe Perchesmy $show_types = 0;
303705ce5bSJoe Perchesmy $fix = 0;
316c72ffaaSAndy Whitcroftmy $root;
32c2fdda0dSAndy Whitcroftmy %debug;
33000d1cc1SJoe Perchesmy %ignore_type = ();
343445686aSJoe Perchesmy %camelcase = ();
35000d1cc1SJoe Perchesmy @ignore = ();
3677f5b10aSHannes Edermy $help = 0;
37000d1cc1SJoe Perchesmy $configuration_file = ".checkpatch.conf";
386cd7f386SJoe Perchesmy $max_line_length = 80;
3977f5b10aSHannes Eder
4077f5b10aSHannes Edersub help {
4177f5b10aSHannes Eder	my ($exitcode) = @_;
4277f5b10aSHannes Eder
4377f5b10aSHannes Eder	print << "EOM";
4477f5b10aSHannes EderUsage: $P [OPTION]... [FILE]...
4577f5b10aSHannes EderVersion: $V
4677f5b10aSHannes Eder
4777f5b10aSHannes EderOptions:
4877f5b10aSHannes Eder  -q, --quiet                quiet
4977f5b10aSHannes Eder  --no-tree                  run without a kernel tree
5077f5b10aSHannes Eder  --no-signoff               do not check for 'Signed-off-by' line
5177f5b10aSHannes Eder  --patch                    treat FILE as patchfile (default)
5277f5b10aSHannes Eder  --emacs                    emacs compile window format
5377f5b10aSHannes Eder  --terse                    one line per report
5477f5b10aSHannes Eder  -f, --file                 treat FILE as regular source file
5577f5b10aSHannes Eder  --subjective, --strict     enable more subjective tests
56000d1cc1SJoe Perches  --ignore TYPE(,TYPE2...)   ignore various comma separated message types
576cd7f386SJoe Perches  --max-line-length=n        set the maximum line length, if exceeded, warn
58000d1cc1SJoe Perches  --show-types               show the message "types" in the output
5977f5b10aSHannes Eder  --root=PATH                PATH to the kernel tree root
6077f5b10aSHannes Eder  --no-summary               suppress the per-file summary
6177f5b10aSHannes Eder  --mailback                 only produce a report in case of warnings/errors
6277f5b10aSHannes Eder  --summary-file             include the filename in summary
6377f5b10aSHannes Eder  --debug KEY=[0|1]          turn on/off debugging of KEY, where KEY is one of
6477f5b10aSHannes Eder                             'values', 'possible', 'type', and 'attr' (default
6577f5b10aSHannes Eder                             is all off)
6677f5b10aSHannes Eder  --test-only=WORD           report only warnings/errors containing WORD
6777f5b10aSHannes Eder                             literally
683705ce5bSJoe Perches  --fix                      EXPERIMENTAL - may create horrible results
693705ce5bSJoe Perches                             If correctable single-line errors exist, create
703705ce5bSJoe Perches                             "<inputfile>.EXPERIMENTAL-checkpatch-fixes"
713705ce5bSJoe Perches                             with potential errors corrected to the preferred
723705ce5bSJoe Perches                             checkpatch style
7377f5b10aSHannes Eder  -h, --help, --version      display this help and exit
7477f5b10aSHannes Eder
7577f5b10aSHannes EderWhen FILE is - read standard input.
7677f5b10aSHannes EderEOM
7777f5b10aSHannes Eder
7877f5b10aSHannes Eder	exit($exitcode);
7977f5b10aSHannes Eder}
8077f5b10aSHannes Eder
81000d1cc1SJoe Perchesmy $conf = which_conf($configuration_file);
82000d1cc1SJoe Perchesif (-f $conf) {
83000d1cc1SJoe Perches	my @conf_args;
84000d1cc1SJoe Perches	open(my $conffile, '<', "$conf")
85000d1cc1SJoe Perches	    or warn "$P: Can't find a readable $configuration_file file $!\n";
86000d1cc1SJoe Perches
87000d1cc1SJoe Perches	while (<$conffile>) {
88000d1cc1SJoe Perches		my $line = $_;
89000d1cc1SJoe Perches
90000d1cc1SJoe Perches		$line =~ s/\s*\n?$//g;
91000d1cc1SJoe Perches		$line =~ s/^\s*//g;
92000d1cc1SJoe Perches		$line =~ s/\s+/ /g;
93000d1cc1SJoe Perches
94000d1cc1SJoe Perches		next if ($line =~ m/^\s*#/);
95000d1cc1SJoe Perches		next if ($line =~ m/^\s*$/);
96000d1cc1SJoe Perches
97000d1cc1SJoe Perches		my @words = split(" ", $line);
98000d1cc1SJoe Perches		foreach my $word (@words) {
99000d1cc1SJoe Perches			last if ($word =~ m/^#/);
100000d1cc1SJoe Perches			push (@conf_args, $word);
101000d1cc1SJoe Perches		}
102000d1cc1SJoe Perches	}
103000d1cc1SJoe Perches	close($conffile);
104000d1cc1SJoe Perches	unshift(@ARGV, @conf_args) if @conf_args;
105000d1cc1SJoe Perches}
106000d1cc1SJoe Perches
1070a920b5bSAndy WhitcroftGetOptions(
1086c72ffaaSAndy Whitcroft	'q|quiet+'	=> \$quiet,
1090a920b5bSAndy Whitcroft	'tree!'		=> \$tree,
1100a920b5bSAndy Whitcroft	'signoff!'	=> \$chk_signoff,
1110a920b5bSAndy Whitcroft	'patch!'	=> \$chk_patch,
1126c72ffaaSAndy Whitcroft	'emacs!'	=> \$emacs,
1138905a67cSAndy Whitcroft	'terse!'	=> \$terse,
11477f5b10aSHannes Eder	'f|file!'	=> \$file,
1156c72ffaaSAndy Whitcroft	'subjective!'	=> \$check,
1166c72ffaaSAndy Whitcroft	'strict!'	=> \$check,
117000d1cc1SJoe Perches	'ignore=s'	=> \@ignore,
118000d1cc1SJoe Perches	'show-types!'	=> \$show_types,
1196cd7f386SJoe Perches	'max-line-length=i' => \$max_line_length,
1206c72ffaaSAndy Whitcroft	'root=s'	=> \$root,
1218905a67cSAndy Whitcroft	'summary!'	=> \$summary,
1228905a67cSAndy Whitcroft	'mailback!'	=> \$mailback,
12313214adfSAndy Whitcroft	'summary-file!'	=> \$summary_file,
1243705ce5bSJoe Perches	'fix!'		=> \$fix,
125c2fdda0dSAndy Whitcroft	'debug=s'	=> \%debug,
126773647a0SAndy Whitcroft	'test-only=s'	=> \$tst_only,
12777f5b10aSHannes Eder	'h|help'	=> \$help,
12877f5b10aSHannes Eder	'version'	=> \$help
12977f5b10aSHannes Eder) or help(1);
13077f5b10aSHannes Eder
13177f5b10aSHannes Ederhelp(0) if ($help);
1320a920b5bSAndy Whitcroft
1330a920b5bSAndy Whitcroftmy $exit = 0;
1340a920b5bSAndy Whitcroft
1350a920b5bSAndy Whitcroftif ($#ARGV < 0) {
13677f5b10aSHannes Eder	print "$P: no input files\n";
1370a920b5bSAndy Whitcroft	exit(1);
1380a920b5bSAndy Whitcroft}
1390a920b5bSAndy Whitcroft
140000d1cc1SJoe Perches@ignore = split(/,/, join(',',@ignore));
141000d1cc1SJoe Perchesforeach my $word (@ignore) {
142000d1cc1SJoe Perches	$word =~ s/\s*\n?$//g;
143000d1cc1SJoe Perches	$word =~ s/^\s*//g;
144000d1cc1SJoe Perches	$word =~ s/\s+/ /g;
145000d1cc1SJoe Perches	$word =~ tr/[a-z]/[A-Z]/;
146000d1cc1SJoe Perches
147000d1cc1SJoe Perches	next if ($word =~ m/^\s*#/);
148000d1cc1SJoe Perches	next if ($word =~ m/^\s*$/);
149000d1cc1SJoe Perches
150000d1cc1SJoe Perches	$ignore_type{$word}++;
151000d1cc1SJoe Perches}
152000d1cc1SJoe Perches
153c2fdda0dSAndy Whitcroftmy $dbg_values = 0;
154c2fdda0dSAndy Whitcroftmy $dbg_possible = 0;
1557429c690SAndy Whitcroftmy $dbg_type = 0;
156a1ef277eSAndy Whitcroftmy $dbg_attr = 0;
157c2fdda0dSAndy Whitcroftfor my $key (keys %debug) {
15821caa13cSAndy Whitcroft	## no critic
15921caa13cSAndy Whitcroft	eval "\${dbg_$key} = '$debug{$key}';";
16021caa13cSAndy Whitcroft	die "$@" if ($@);
161c2fdda0dSAndy Whitcroft}
162c2fdda0dSAndy Whitcroft
163d2c0a235SAndy Whitcroftmy $rpt_cleaners = 0;
164d2c0a235SAndy Whitcroft
1658905a67cSAndy Whitcroftif ($terse) {
1668905a67cSAndy Whitcroft	$emacs = 1;
1678905a67cSAndy Whitcroft	$quiet++;
1688905a67cSAndy Whitcroft}
1698905a67cSAndy Whitcroft
1706c72ffaaSAndy Whitcroftif ($tree) {
1716c72ffaaSAndy Whitcroft	if (defined $root) {
1726c72ffaaSAndy Whitcroft		if (!top_of_kernel_tree($root)) {
1736c72ffaaSAndy Whitcroft			die "$P: $root: --root does not point at a valid tree\n";
1746c72ffaaSAndy Whitcroft		}
1756c72ffaaSAndy Whitcroft	} else {
1766c72ffaaSAndy Whitcroft		if (top_of_kernel_tree('.')) {
1776c72ffaaSAndy Whitcroft			$root = '.';
1786c72ffaaSAndy Whitcroft		} elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
1796c72ffaaSAndy Whitcroft						top_of_kernel_tree($1)) {
1806c72ffaaSAndy Whitcroft			$root = $1;
1816c72ffaaSAndy Whitcroft		}
1826c72ffaaSAndy Whitcroft	}
1836c72ffaaSAndy Whitcroft
1846c72ffaaSAndy Whitcroft	if (!defined $root) {
1850a920b5bSAndy Whitcroft		print "Must be run from the top-level dir. of a kernel tree\n";
1860a920b5bSAndy Whitcroft		exit(2);
1870a920b5bSAndy Whitcroft	}
1886c72ffaaSAndy Whitcroft}
1896c72ffaaSAndy Whitcroft
1906c72ffaaSAndy Whitcroftmy $emitted_corrupt = 0;
1916c72ffaaSAndy Whitcroft
1922ceb532bSAndy Whitcroftour $Ident	= qr{
1932ceb532bSAndy Whitcroft			[A-Za-z_][A-Za-z\d_]*
1942ceb532bSAndy Whitcroft			(?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
1952ceb532bSAndy Whitcroft		}x;
1966c72ffaaSAndy Whitcroftour $Storage	= qr{extern|static|asmlinkage};
1976c72ffaaSAndy Whitcroftour $Sparse	= qr{
1986c72ffaaSAndy Whitcroft			__user|
1996c72ffaaSAndy Whitcroft			__kernel|
2006c72ffaaSAndy Whitcroft			__force|
2016c72ffaaSAndy Whitcroft			__iomem|
2026c72ffaaSAndy Whitcroft			__must_check|
2036c72ffaaSAndy Whitcroft			__init_refok|
204417495edSAndy Whitcroft			__kprobes|
205165e72a6SSven Eckelmann			__ref|
206165e72a6SSven Eckelmann			__rcu
2076c72ffaaSAndy Whitcroft		}x;
20852131292SWolfram Sang
20952131292SWolfram Sang# Notes to $Attribute:
21052131292SWolfram Sang# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
2116c72ffaaSAndy Whitcroftour $Attribute	= qr{
2126c72ffaaSAndy Whitcroft			const|
21303f1df7dSJoe Perches			__percpu|
21403f1df7dSJoe Perches			__nocast|
21503f1df7dSJoe Perches			__safe|
21603f1df7dSJoe Perches			__bitwise__|
21703f1df7dSJoe Perches			__packed__|
21803f1df7dSJoe Perches			__packed2__|
21903f1df7dSJoe Perches			__naked|
22003f1df7dSJoe Perches			__maybe_unused|
22103f1df7dSJoe Perches			__always_unused|
22203f1df7dSJoe Perches			__noreturn|
22303f1df7dSJoe Perches			__used|
22403f1df7dSJoe Perches			__cold|
22503f1df7dSJoe Perches			__noclone|
22603f1df7dSJoe Perches			__deprecated|
2276c72ffaaSAndy Whitcroft			__read_mostly|
2286c72ffaaSAndy Whitcroft			__kprobes|
22952131292SWolfram Sang			__(?:mem|cpu|dev|)(?:initdata|initconst|init\b)|
23024e1d81aSAndy Whitcroft			____cacheline_aligned|
23124e1d81aSAndy Whitcroft			____cacheline_aligned_in_smp|
2325fe3af11SAndy Whitcroft			____cacheline_internodealigned_in_smp|
2335fe3af11SAndy Whitcroft			__weak
2346c72ffaaSAndy Whitcroft		  }x;
235c45dcabdSAndy Whitcroftour $Modifier;
2366c72ffaaSAndy Whitcroftour $Inline	= qr{inline|__always_inline|noinline};
2376c72ffaaSAndy Whitcroftour $Member	= qr{->$Ident|\.$Ident|\[[^]]*\]};
2386c72ffaaSAndy Whitcroftour $Lval	= qr{$Ident(?:$Member)*};
2396c72ffaaSAndy Whitcroft
24095e2c602SJoe Perchesour $Int_type	= qr{(?i)llu|ull|ll|lu|ul|l|u};
24195e2c602SJoe Perchesour $Binary	= qr{(?i)0b[01]+$Int_type?};
24295e2c602SJoe Perchesour $Hex	= qr{(?i)0x[0-9a-f]+$Int_type?};
24395e2c602SJoe Perchesour $Int	= qr{[0-9]+$Int_type?};
244326b1ffcSJoe Perchesour $Float_hex	= qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?};
245326b1ffcSJoe Perchesour $Float_dec	= qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?};
246326b1ffcSJoe Perchesour $Float_int	= qr{(?i)[0-9]+e-?[0-9]+[fl]?};
24774349bccSJoe Perchesour $Float	= qr{$Float_hex|$Float_dec|$Float_int};
24895e2c602SJoe Perchesour $Constant	= qr{$Float|$Binary|$Hex|$Int};
249326b1ffcSJoe Perchesour $Assignment	= qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=};
25086f9d059SAndy Whitcroftour $Compare    = qr{<=|>=|==|!=|<|>};
25123f780c9SJoe Perchesour $Arithmetic = qr{\+|-|\*|\/|%};
2526c72ffaaSAndy Whitcroftour $Operators	= qr{
2536c72ffaaSAndy Whitcroft			<=|>=|==|!=|
2546c72ffaaSAndy Whitcroft			=>|->|<<|>>|<|>|!|~|
25523f780c9SJoe Perches			&&|\|\||,|\^|\+\+|--|&|\||$Arithmetic
2566c72ffaaSAndy Whitcroft		  }x;
2576c72ffaaSAndy Whitcroft
2588905a67cSAndy Whitcroftour $NonptrType;
2598905a67cSAndy Whitcroftour $Type;
2608905a67cSAndy Whitcroftour $Declare;
2618905a67cSAndy Whitcroft
26215662b3eSJoe Perchesour $NON_ASCII_UTF8	= qr{
26315662b3eSJoe Perches	[\xC2-\xDF][\x80-\xBF]               # non-overlong 2-byte
264171ae1a4SAndy Whitcroft	|  \xE0[\xA0-\xBF][\x80-\xBF]        # excluding overlongs
265171ae1a4SAndy Whitcroft	| [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}  # straight 3-byte
266171ae1a4SAndy Whitcroft	|  \xED[\x80-\x9F][\x80-\xBF]        # excluding surrogates
267171ae1a4SAndy Whitcroft	|  \xF0[\x90-\xBF][\x80-\xBF]{2}     # planes 1-3
268171ae1a4SAndy Whitcroft	| [\xF1-\xF3][\x80-\xBF]{3}          # planes 4-15
269171ae1a4SAndy Whitcroft	|  \xF4[\x80-\x8F][\x80-\xBF]{2}     # plane 16
270171ae1a4SAndy Whitcroft}x;
271171ae1a4SAndy Whitcroft
27215662b3eSJoe Perchesour $UTF8	= qr{
27315662b3eSJoe Perches	[\x09\x0A\x0D\x20-\x7E]              # ASCII
27415662b3eSJoe Perches	| $NON_ASCII_UTF8
27515662b3eSJoe Perches}x;
27615662b3eSJoe Perches
2778ed22cadSAndy Whitcroftour $typeTypedefs = qr{(?x:
278fb9e9096SAndy Whitcroft	(?:__)?(?:u|s|be|le)(?:8|16|32|64)|
2798ed22cadSAndy Whitcroft	atomic_t
2808ed22cadSAndy Whitcroft)};
2818ed22cadSAndy Whitcroft
282691e669bSJoe Perchesour $logFunctions = qr{(?x:
2836e60c02eSJoe Perches	printk(?:_ratelimited|_once|)|
2847d0b6594SJacob Keller	(?:[a-z0-9]+_){1,2}(?:printk|emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)(?:_ratelimited|_once|)|
2856e60c02eSJoe Perches	WARN(?:_RATELIMIT|_ONCE|)|
286b0531722SJoe Perches	panic|
287b0531722SJoe Perches	MODULE_[A-Z_]+
288691e669bSJoe Perches)};
289691e669bSJoe Perches
29020112475SJoe Perchesour $signature_tags = qr{(?xi:
29120112475SJoe Perches	Signed-off-by:|
29220112475SJoe Perches	Acked-by:|
29320112475SJoe Perches	Tested-by:|
29420112475SJoe Perches	Reviewed-by:|
29520112475SJoe Perches	Reported-by:|
2968543ae12SMugunthan V N	Suggested-by:|
29720112475SJoe Perches	To:|
29820112475SJoe Perches	Cc:
29920112475SJoe Perches)};
30020112475SJoe Perches
3018905a67cSAndy Whitcroftour @typeList = (
3028905a67cSAndy Whitcroft	qr{void},
303c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?char},
304c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?short},
305c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?int},
306c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?long},
307c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?long\s+int},
308c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?long\s+long},
309c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?long\s+long\s+int},
3108905a67cSAndy Whitcroft	qr{unsigned},
3118905a67cSAndy Whitcroft	qr{float},
3128905a67cSAndy Whitcroft	qr{double},
3138905a67cSAndy Whitcroft	qr{bool},
3148905a67cSAndy Whitcroft	qr{struct\s+$Ident},
3158905a67cSAndy Whitcroft	qr{union\s+$Ident},
3168905a67cSAndy Whitcroft	qr{enum\s+$Ident},
3178905a67cSAndy Whitcroft	qr{${Ident}_t},
3188905a67cSAndy Whitcroft	qr{${Ident}_handler},
3198905a67cSAndy Whitcroft	qr{${Ident}_handler_fn},
3208905a67cSAndy Whitcroft);
321c45dcabdSAndy Whitcroftour @modifierList = (
322c45dcabdSAndy Whitcroft	qr{fastcall},
323c45dcabdSAndy Whitcroft);
3248905a67cSAndy Whitcroft
3257840a94cSWolfram Sangour $allowed_asm_includes = qr{(?x:
3267840a94cSWolfram Sang	irq|
3277840a94cSWolfram Sang	memory
3287840a94cSWolfram Sang)};
3297840a94cSWolfram Sang# memory.h: ARM has a custom one
3307840a94cSWolfram Sang
3318905a67cSAndy Whitcroftsub build_types {
332d2172eb5SAndy Whitcroft	my $mods = "(?x:  \n" . join("|\n  ", @modifierList) . "\n)";
333d2172eb5SAndy Whitcroft	my $all = "(?x:  \n" . join("|\n  ", @typeList) . "\n)";
334c8cb2ca3SAndy Whitcroft	$Modifier	= qr{(?:$Attribute|$Sparse|$mods)};
3358905a67cSAndy Whitcroft	$NonptrType	= qr{
336d2172eb5SAndy Whitcroft			(?:$Modifier\s+|const\s+)*
337cf655043SAndy Whitcroft			(?:
3386b48db24SAndy Whitcroft				(?:typeof|__typeof__)\s*\([^\)]*\)|
3398ed22cadSAndy Whitcroft				(?:$typeTypedefs\b)|
340c45dcabdSAndy Whitcroft				(?:${all}\b)
341cf655043SAndy Whitcroft			)
342c8cb2ca3SAndy Whitcroft			(?:\s+$Modifier|\s+const)*
3438905a67cSAndy Whitcroft		  }x;
3448905a67cSAndy Whitcroft	$Type	= qr{
345c45dcabdSAndy Whitcroft			$NonptrType
346b337d8b8SAndy Whitcroft			(?:(?:\s|\*|\[\])+\s*const|(?:\s|\*|\[\])+|(?:\s*\[\s*\])+)?
347c8cb2ca3SAndy Whitcroft			(?:\s+$Inline|\s+$Modifier)*
3488905a67cSAndy Whitcroft		  }x;
3498905a67cSAndy Whitcroft	$Declare	= qr{(?:$Storage\s+)?$Type};
3508905a67cSAndy Whitcroft}
3518905a67cSAndy Whitcroftbuild_types();
3526c72ffaaSAndy Whitcroft
3537d2367afSJoe Perchesour $Typecast	= qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
354d1fe9c09SJoe Perches
355d1fe9c09SJoe Perches# Using $balanced_parens, $LvalOrFunc, or $FuncArg
356d1fe9c09SJoe Perches# requires at least perl version v5.10.0
357d1fe9c09SJoe Perches# Any use must be runtime checked with $^V
358d1fe9c09SJoe Perches
359d1fe9c09SJoe Perchesour $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
360d1fe9c09SJoe Perchesour $LvalOrFunc	= qr{($Lval)\s*($balanced_parens{0,1})\s*};
361d7c76ba7SJoe Perchesour $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant)};
3627d2367afSJoe Perches
3637d2367afSJoe Perchessub deparenthesize {
3647d2367afSJoe Perches	my ($string) = @_;
3657d2367afSJoe Perches	return "" if (!defined($string));
3667d2367afSJoe Perches	$string =~ s@^\s*\(\s*@@g;
3677d2367afSJoe Perches	$string =~ s@\s*\)\s*$@@g;
3687d2367afSJoe Perches	$string =~ s@\s+@ @g;
3697d2367afSJoe Perches	return $string;
3707d2367afSJoe Perches}
3717d2367afSJoe Perches
3723445686aSJoe Perchessub seed_camelcase_file {
3733445686aSJoe Perches	my ($file) = @_;
3743445686aSJoe Perches
3753445686aSJoe Perches	return if (!(-f $file));
3763445686aSJoe Perches
3773445686aSJoe Perches	local $/;
3783445686aSJoe Perches
3793445686aSJoe Perches	open(my $include_file, '<', "$file")
3803445686aSJoe Perches	    or warn "$P: Can't read '$file' $!\n";
3813445686aSJoe Perches	my $text = <$include_file>;
3823445686aSJoe Perches	close($include_file);
3833445686aSJoe Perches
3843445686aSJoe Perches	my @lines = split('\n', $text);
3853445686aSJoe Perches
3863445686aSJoe Perches	foreach my $line (@lines) {
3873445686aSJoe Perches		next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/);
3883445686aSJoe Perches		if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) {
3893445686aSJoe Perches			$camelcase{$1} = 1;
3903445686aSJoe Perches		}
3913445686aSJoe Perches	        elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*\(/) {
3923445686aSJoe Perches			$camelcase{$1} = 1;
3933445686aSJoe Perches		}
3943445686aSJoe Perches	}
3953445686aSJoe Perches}
3963445686aSJoe Perches
3973445686aSJoe Perchesmy $camelcase_seeded = 0;
3983445686aSJoe Perchessub seed_camelcase_includes {
3993445686aSJoe Perches	return if ($camelcase_seeded);
4003445686aSJoe Perches
4013445686aSJoe Perches	my $files;
402*351b2a1fSJoe Perches	my $camelcase_git_file = "";
403*351b2a1fSJoe Perches
4043445686aSJoe Perches	if (-d ".git") {
405*351b2a1fSJoe Perches		my $git_last_include_commit = `git log --no-merges --pretty=format:"%h%n" -1 -- include`;
406*351b2a1fSJoe Perches		chomp $git_last_include_commit;
407*351b2a1fSJoe Perches		$camelcase_git_file = ".checkpatch-camelcase.$git_last_include_commit";
408*351b2a1fSJoe Perches		if (-f $camelcase_git_file) {
409*351b2a1fSJoe Perches			open(my $camelcase_file, '<', "$camelcase_git_file")
410*351b2a1fSJoe Perches			    or warn "$P: Can't read '$camelcase_git_file' $!\n";
411*351b2a1fSJoe Perches			while (<$camelcase_file>) {
412*351b2a1fSJoe Perches				chomp;
413*351b2a1fSJoe Perches				$camelcase{$_} = 1;
414*351b2a1fSJoe Perches			}
415*351b2a1fSJoe Perches			close($camelcase_file);
416*351b2a1fSJoe Perches
417*351b2a1fSJoe Perches			return;
418*351b2a1fSJoe Perches		}
4193445686aSJoe Perches		$files = `git ls-files include`;
4203445686aSJoe Perches	} else {
4213445686aSJoe Perches		$files = `find $root/include -name "*.h"`;
4223445686aSJoe Perches	}
4233445686aSJoe Perches	my @include_files = split('\n', $files);
4243445686aSJoe Perches	foreach my $file (@include_files) {
4253445686aSJoe Perches		seed_camelcase_file($file);
4263445686aSJoe Perches	}
4273445686aSJoe Perches	$camelcase_seeded = 1;
428*351b2a1fSJoe Perches
429*351b2a1fSJoe Perches	if ($camelcase_git_file ne "") {
430*351b2a1fSJoe Perches		unlink glob ".checkpatch-camelcase.*";
431*351b2a1fSJoe Perches		open(my $camelcase_file, '>', "$camelcase_git_file")
432*351b2a1fSJoe Perches		    or warn "$P: Can't write '$camelcase_git_file' $!\n";
433*351b2a1fSJoe Perches		foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) {
434*351b2a1fSJoe Perches			print $camelcase_file ("$_\n");
435*351b2a1fSJoe Perches		}
436*351b2a1fSJoe Perches		close($camelcase_file);
437*351b2a1fSJoe Perches	}
4383445686aSJoe Perches}
4393445686aSJoe Perches
4406c72ffaaSAndy Whitcroft$chk_signoff = 0 if ($file);
4410a920b5bSAndy Whitcroft
44200df344fSAndy Whitcroftmy @rawlines = ();
443c2fdda0dSAndy Whitcroftmy @lines = ();
4443705ce5bSJoe Perchesmy @fixed = ();
445c2fdda0dSAndy Whitcroftmy $vname;
4466c72ffaaSAndy Whitcroftfor my $filename (@ARGV) {
44721caa13cSAndy Whitcroft	my $FILE;
4486c72ffaaSAndy Whitcroft	if ($file) {
44921caa13cSAndy Whitcroft		open($FILE, '-|', "diff -u /dev/null $filename") ||
4506c72ffaaSAndy Whitcroft			die "$P: $filename: diff failed - $!\n";
45121caa13cSAndy Whitcroft	} elsif ($filename eq '-') {
45221caa13cSAndy Whitcroft		open($FILE, '<&STDIN');
4536c72ffaaSAndy Whitcroft	} else {
45421caa13cSAndy Whitcroft		open($FILE, '<', "$filename") ||
4556c72ffaaSAndy Whitcroft			die "$P: $filename: open failed - $!\n";
4566c72ffaaSAndy Whitcroft	}
457c2fdda0dSAndy Whitcroft	if ($filename eq '-') {
458c2fdda0dSAndy Whitcroft		$vname = 'Your patch';
459c2fdda0dSAndy Whitcroft	} else {
460c2fdda0dSAndy Whitcroft		$vname = $filename;
461c2fdda0dSAndy Whitcroft	}
46221caa13cSAndy Whitcroft	while (<$FILE>) {
4630a920b5bSAndy Whitcroft		chomp;
46400df344fSAndy Whitcroft		push(@rawlines, $_);
4656c72ffaaSAndy Whitcroft	}
46621caa13cSAndy Whitcroft	close($FILE);
467c2fdda0dSAndy Whitcroft	if (!process($filename)) {
4680a920b5bSAndy Whitcroft		$exit = 1;
4690a920b5bSAndy Whitcroft	}
47000df344fSAndy Whitcroft	@rawlines = ();
47113214adfSAndy Whitcroft	@lines = ();
4723705ce5bSJoe Perches	@fixed = ();
4730a920b5bSAndy Whitcroft}
4740a920b5bSAndy Whitcroft
4750a920b5bSAndy Whitcroftexit($exit);
4760a920b5bSAndy Whitcroft
4770a920b5bSAndy Whitcroftsub top_of_kernel_tree {
4786c72ffaaSAndy Whitcroft	my ($root) = @_;
4796c72ffaaSAndy Whitcroft
4806c72ffaaSAndy Whitcroft	my @tree_check = (
4816c72ffaaSAndy Whitcroft		"COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
4826c72ffaaSAndy Whitcroft		"README", "Documentation", "arch", "include", "drivers",
4836c72ffaaSAndy Whitcroft		"fs", "init", "ipc", "kernel", "lib", "scripts",
4846c72ffaaSAndy Whitcroft	);
4856c72ffaaSAndy Whitcroft
4866c72ffaaSAndy Whitcroft	foreach my $check (@tree_check) {
4876c72ffaaSAndy Whitcroft		if (! -e $root . '/' . $check) {
4880a920b5bSAndy Whitcroft			return 0;
4890a920b5bSAndy Whitcroft		}
4906c72ffaaSAndy Whitcroft	}
4916c72ffaaSAndy Whitcroft	return 1;
4926c72ffaaSAndy Whitcroft}
4930a920b5bSAndy Whitcroft
49420112475SJoe Perchessub parse_email {
49520112475SJoe Perches	my ($formatted_email) = @_;
49620112475SJoe Perches
49720112475SJoe Perches	my $name = "";
49820112475SJoe Perches	my $address = "";
49920112475SJoe Perches	my $comment = "";
50020112475SJoe Perches
50120112475SJoe Perches	if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
50220112475SJoe Perches		$name = $1;
50320112475SJoe Perches		$address = $2;
50420112475SJoe Perches		$comment = $3 if defined $3;
50520112475SJoe Perches	} elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
50620112475SJoe Perches		$address = $1;
50720112475SJoe Perches		$comment = $2 if defined $2;
50820112475SJoe Perches	} elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
50920112475SJoe Perches		$address = $1;
51020112475SJoe Perches		$comment = $2 if defined $2;
51120112475SJoe Perches		$formatted_email =~ s/$address.*$//;
51220112475SJoe Perches		$name = $formatted_email;
5133705ce5bSJoe Perches		$name = trim($name);
51420112475SJoe Perches		$name =~ s/^\"|\"$//g;
51520112475SJoe Perches		# If there's a name left after stripping spaces and
51620112475SJoe Perches		# leading quotes, and the address doesn't have both
51720112475SJoe Perches		# leading and trailing angle brackets, the address
51820112475SJoe Perches		# is invalid. ie:
51920112475SJoe Perches		#   "joe smith [email protected]" bad
52020112475SJoe Perches		#   "joe smith <[email protected]" bad
52120112475SJoe Perches		if ($name ne "" && $address !~ /^<[^>]+>$/) {
52220112475SJoe Perches			$name = "";
52320112475SJoe Perches			$address = "";
52420112475SJoe Perches			$comment = "";
52520112475SJoe Perches		}
52620112475SJoe Perches	}
52720112475SJoe Perches
5283705ce5bSJoe Perches	$name = trim($name);
52920112475SJoe Perches	$name =~ s/^\"|\"$//g;
5303705ce5bSJoe Perches	$address = trim($address);
53120112475SJoe Perches	$address =~ s/^\<|\>$//g;
53220112475SJoe Perches
53320112475SJoe Perches	if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
53420112475SJoe Perches		$name =~ s/(?<!\\)"/\\"/g; ##escape quotes
53520112475SJoe Perches		$name = "\"$name\"";
53620112475SJoe Perches	}
53720112475SJoe Perches
53820112475SJoe Perches	return ($name, $address, $comment);
53920112475SJoe Perches}
54020112475SJoe Perches
54120112475SJoe Perchessub format_email {
54220112475SJoe Perches	my ($name, $address) = @_;
54320112475SJoe Perches
54420112475SJoe Perches	my $formatted_email;
54520112475SJoe Perches
5463705ce5bSJoe Perches	$name = trim($name);
54720112475SJoe Perches	$name =~ s/^\"|\"$//g;
5483705ce5bSJoe Perches	$address = trim($address);
54920112475SJoe Perches
55020112475SJoe Perches	if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
55120112475SJoe Perches		$name =~ s/(?<!\\)"/\\"/g; ##escape quotes
55220112475SJoe Perches		$name = "\"$name\"";
55320112475SJoe Perches	}
55420112475SJoe Perches
55520112475SJoe Perches	if ("$name" eq "") {
55620112475SJoe Perches		$formatted_email = "$address";
55720112475SJoe Perches	} else {
55820112475SJoe Perches		$formatted_email = "$name <$address>";
55920112475SJoe Perches	}
56020112475SJoe Perches
56120112475SJoe Perches	return $formatted_email;
56220112475SJoe Perches}
56320112475SJoe Perches
564000d1cc1SJoe Perchessub which_conf {
565000d1cc1SJoe Perches	my ($conf) = @_;
566000d1cc1SJoe Perches
567000d1cc1SJoe Perches	foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
568000d1cc1SJoe Perches		if (-e "$path/$conf") {
569000d1cc1SJoe Perches			return "$path/$conf";
570000d1cc1SJoe Perches		}
571000d1cc1SJoe Perches	}
572000d1cc1SJoe Perches
573000d1cc1SJoe Perches	return "";
574000d1cc1SJoe Perches}
575000d1cc1SJoe Perches
5760a920b5bSAndy Whitcroftsub expand_tabs {
5770a920b5bSAndy Whitcroft	my ($str) = @_;
5780a920b5bSAndy Whitcroft
5790a920b5bSAndy Whitcroft	my $res = '';
5800a920b5bSAndy Whitcroft	my $n = 0;
5810a920b5bSAndy Whitcroft	for my $c (split(//, $str)) {
5820a920b5bSAndy Whitcroft		if ($c eq "\t") {
5830a920b5bSAndy Whitcroft			$res .= ' ';
5840a920b5bSAndy Whitcroft			$n++;
5850a920b5bSAndy Whitcroft			for (; ($n % 8) != 0; $n++) {
5860a920b5bSAndy Whitcroft				$res .= ' ';
5870a920b5bSAndy Whitcroft			}
5880a920b5bSAndy Whitcroft			next;
5890a920b5bSAndy Whitcroft		}
5900a920b5bSAndy Whitcroft		$res .= $c;
5910a920b5bSAndy Whitcroft		$n++;
5920a920b5bSAndy Whitcroft	}
5930a920b5bSAndy Whitcroft
5940a920b5bSAndy Whitcroft	return $res;
5950a920b5bSAndy Whitcroft}
5966c72ffaaSAndy Whitcroftsub copy_spacing {
597773647a0SAndy Whitcroft	(my $res = shift) =~ tr/\t/ /c;
5986c72ffaaSAndy Whitcroft	return $res;
5996c72ffaaSAndy Whitcroft}
6000a920b5bSAndy Whitcroft
6014a0df2efSAndy Whitcroftsub line_stats {
6024a0df2efSAndy Whitcroft	my ($line) = @_;
6034a0df2efSAndy Whitcroft
6044a0df2efSAndy Whitcroft	# Drop the diff line leader and expand tabs
6054a0df2efSAndy Whitcroft	$line =~ s/^.//;
6064a0df2efSAndy Whitcroft	$line = expand_tabs($line);
6074a0df2efSAndy Whitcroft
6084a0df2efSAndy Whitcroft	# Pick the indent from the front of the line.
6094a0df2efSAndy Whitcroft	my ($white) = ($line =~ /^(\s*)/);
6104a0df2efSAndy Whitcroft
6114a0df2efSAndy Whitcroft	return (length($line), length($white));
6124a0df2efSAndy Whitcroft}
6134a0df2efSAndy Whitcroft
614773647a0SAndy Whitcroftmy $sanitise_quote = '';
615773647a0SAndy Whitcroft
616773647a0SAndy Whitcroftsub sanitise_line_reset {
617773647a0SAndy Whitcroft	my ($in_comment) = @_;
618773647a0SAndy Whitcroft
619773647a0SAndy Whitcroft	if ($in_comment) {
620773647a0SAndy Whitcroft		$sanitise_quote = '*/';
621773647a0SAndy Whitcroft	} else {
622773647a0SAndy Whitcroft		$sanitise_quote = '';
623773647a0SAndy Whitcroft	}
624773647a0SAndy Whitcroft}
62500df344fSAndy Whitcroftsub sanitise_line {
62600df344fSAndy Whitcroft	my ($line) = @_;
62700df344fSAndy Whitcroft
62800df344fSAndy Whitcroft	my $res = '';
62900df344fSAndy Whitcroft	my $l = '';
63000df344fSAndy Whitcroft
631c2fdda0dSAndy Whitcroft	my $qlen = 0;
632773647a0SAndy Whitcroft	my $off = 0;
633773647a0SAndy Whitcroft	my $c;
63400df344fSAndy Whitcroft
635773647a0SAndy Whitcroft	# Always copy over the diff marker.
636773647a0SAndy Whitcroft	$res = substr($line, 0, 1);
637773647a0SAndy Whitcroft
638773647a0SAndy Whitcroft	for ($off = 1; $off < length($line); $off++) {
639773647a0SAndy Whitcroft		$c = substr($line, $off, 1);
640773647a0SAndy Whitcroft
641773647a0SAndy Whitcroft		# Comments we are wacking completly including the begin
642773647a0SAndy Whitcroft		# and end, all to $;.
643773647a0SAndy Whitcroft		if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
644773647a0SAndy Whitcroft			$sanitise_quote = '*/';
645773647a0SAndy Whitcroft
646773647a0SAndy Whitcroft			substr($res, $off, 2, "$;$;");
647773647a0SAndy Whitcroft			$off++;
64800df344fSAndy Whitcroft			next;
649773647a0SAndy Whitcroft		}
65081bc0e02SAndy Whitcroft		if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
651773647a0SAndy Whitcroft			$sanitise_quote = '';
652773647a0SAndy Whitcroft			substr($res, $off, 2, "$;$;");
653773647a0SAndy Whitcroft			$off++;
654773647a0SAndy Whitcroft			next;
655773647a0SAndy Whitcroft		}
656113f04a8SDaniel Walker		if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
657113f04a8SDaniel Walker			$sanitise_quote = '//';
658113f04a8SDaniel Walker
659113f04a8SDaniel Walker			substr($res, $off, 2, $sanitise_quote);
660113f04a8SDaniel Walker			$off++;
661113f04a8SDaniel Walker			next;
662113f04a8SDaniel Walker		}
663773647a0SAndy Whitcroft
664773647a0SAndy Whitcroft		# A \ in a string means ignore the next character.
665773647a0SAndy Whitcroft		if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
666773647a0SAndy Whitcroft		    $c eq "\\") {
667773647a0SAndy Whitcroft			substr($res, $off, 2, 'XX');
668773647a0SAndy Whitcroft			$off++;
669773647a0SAndy Whitcroft			next;
670773647a0SAndy Whitcroft		}
671773647a0SAndy Whitcroft		# Regular quotes.
672773647a0SAndy Whitcroft		if ($c eq "'" || $c eq '"') {
673773647a0SAndy Whitcroft			if ($sanitise_quote eq '') {
674773647a0SAndy Whitcroft				$sanitise_quote = $c;
675773647a0SAndy Whitcroft
676773647a0SAndy Whitcroft				substr($res, $off, 1, $c);
677773647a0SAndy Whitcroft				next;
678773647a0SAndy Whitcroft			} elsif ($sanitise_quote eq $c) {
679773647a0SAndy Whitcroft				$sanitise_quote = '';
68000df344fSAndy Whitcroft			}
68100df344fSAndy Whitcroft		}
682773647a0SAndy Whitcroft
683fae17daeSAndy Whitcroft		#print "c<$c> SQ<$sanitise_quote>\n";
684773647a0SAndy Whitcroft		if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
685773647a0SAndy Whitcroft			substr($res, $off, 1, $;);
686113f04a8SDaniel Walker		} elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
687113f04a8SDaniel Walker			substr($res, $off, 1, $;);
688773647a0SAndy Whitcroft		} elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
689773647a0SAndy Whitcroft			substr($res, $off, 1, 'X');
69000df344fSAndy Whitcroft		} else {
691773647a0SAndy Whitcroft			substr($res, $off, 1, $c);
69200df344fSAndy Whitcroft		}
693c2fdda0dSAndy Whitcroft	}
694c2fdda0dSAndy Whitcroft
695113f04a8SDaniel Walker	if ($sanitise_quote eq '//') {
696113f04a8SDaniel Walker		$sanitise_quote = '';
697113f04a8SDaniel Walker	}
698113f04a8SDaniel Walker
699c2fdda0dSAndy Whitcroft	# The pathname on a #include may be surrounded by '<' and '>'.
700c45dcabdSAndy Whitcroft	if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
701c2fdda0dSAndy Whitcroft		my $clean = 'X' x length($1);
702c2fdda0dSAndy Whitcroft		$res =~ s@\<.*\>@<$clean>@;
703c2fdda0dSAndy Whitcroft
704c2fdda0dSAndy Whitcroft	# The whole of a #error is a string.
705c45dcabdSAndy Whitcroft	} elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
706c2fdda0dSAndy Whitcroft		my $clean = 'X' x length($1);
707c45dcabdSAndy Whitcroft		$res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
708c2fdda0dSAndy Whitcroft	}
709c2fdda0dSAndy Whitcroft
71000df344fSAndy Whitcroft	return $res;
71100df344fSAndy Whitcroft}
71200df344fSAndy Whitcroft
713a6962d72SJoe Perchessub get_quoted_string {
714a6962d72SJoe Perches	my ($line, $rawline) = @_;
715a6962d72SJoe Perches
716a6962d72SJoe Perches	return "" if ($line !~ m/(\"[X]+\")/g);
717a6962d72SJoe Perches	return substr($rawline, $-[0], $+[0] - $-[0]);
718a6962d72SJoe Perches}
719a6962d72SJoe Perches
7208905a67cSAndy Whitcroftsub ctx_statement_block {
7218905a67cSAndy Whitcroft	my ($linenr, $remain, $off) = @_;
7228905a67cSAndy Whitcroft	my $line = $linenr - 1;
7238905a67cSAndy Whitcroft	my $blk = '';
7248905a67cSAndy Whitcroft	my $soff = $off;
7258905a67cSAndy Whitcroft	my $coff = $off - 1;
726773647a0SAndy Whitcroft	my $coff_set = 0;
7278905a67cSAndy Whitcroft
72813214adfSAndy Whitcroft	my $loff = 0;
72913214adfSAndy Whitcroft
7308905a67cSAndy Whitcroft	my $type = '';
7318905a67cSAndy Whitcroft	my $level = 0;
732a2750645SAndy Whitcroft	my @stack = ();
733cf655043SAndy Whitcroft	my $p;
7348905a67cSAndy Whitcroft	my $c;
7358905a67cSAndy Whitcroft	my $len = 0;
73613214adfSAndy Whitcroft
73713214adfSAndy Whitcroft	my $remainder;
7388905a67cSAndy Whitcroft	while (1) {
739a2750645SAndy Whitcroft		@stack = (['', 0]) if ($#stack == -1);
740a2750645SAndy Whitcroft
741773647a0SAndy Whitcroft		#warn "CSB: blk<$blk> remain<$remain>\n";
7428905a67cSAndy Whitcroft		# If we are about to drop off the end, pull in more
7438905a67cSAndy Whitcroft		# context.
7448905a67cSAndy Whitcroft		if ($off >= $len) {
7458905a67cSAndy Whitcroft			for (; $remain > 0; $line++) {
746dea33496SAndy Whitcroft				last if (!defined $lines[$line]);
747c2fdda0dSAndy Whitcroft				next if ($lines[$line] =~ /^-/);
7488905a67cSAndy Whitcroft				$remain--;
74913214adfSAndy Whitcroft				$loff = $len;
750c2fdda0dSAndy Whitcroft				$blk .= $lines[$line] . "\n";
7518905a67cSAndy Whitcroft				$len = length($blk);
7528905a67cSAndy Whitcroft				$line++;
7538905a67cSAndy Whitcroft				last;
7548905a67cSAndy Whitcroft			}
7558905a67cSAndy Whitcroft			# Bail if there is no further context.
7568905a67cSAndy Whitcroft			#warn "CSB: blk<$blk> off<$off> len<$len>\n";
75713214adfSAndy Whitcroft			if ($off >= $len) {
7588905a67cSAndy Whitcroft				last;
7598905a67cSAndy Whitcroft			}
760f74bd194SAndy Whitcroft			if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
761f74bd194SAndy Whitcroft				$level++;
762f74bd194SAndy Whitcroft				$type = '#';
763f74bd194SAndy Whitcroft			}
7648905a67cSAndy Whitcroft		}
765cf655043SAndy Whitcroft		$p = $c;
7668905a67cSAndy Whitcroft		$c = substr($blk, $off, 1);
76713214adfSAndy Whitcroft		$remainder = substr($blk, $off);
7688905a67cSAndy Whitcroft
769773647a0SAndy Whitcroft		#warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
7704635f4fbSAndy Whitcroft
7714635f4fbSAndy Whitcroft		# Handle nested #if/#else.
7724635f4fbSAndy Whitcroft		if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
7734635f4fbSAndy Whitcroft			push(@stack, [ $type, $level ]);
7744635f4fbSAndy Whitcroft		} elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
7754635f4fbSAndy Whitcroft			($type, $level) = @{$stack[$#stack - 1]};
7764635f4fbSAndy Whitcroft		} elsif ($remainder =~ /^#\s*endif\b/) {
7774635f4fbSAndy Whitcroft			($type, $level) = @{pop(@stack)};
7784635f4fbSAndy Whitcroft		}
7794635f4fbSAndy Whitcroft
7808905a67cSAndy Whitcroft		# Statement ends at the ';' or a close '}' at the
7818905a67cSAndy Whitcroft		# outermost level.
7828905a67cSAndy Whitcroft		if ($level == 0 && $c eq ';') {
7838905a67cSAndy Whitcroft			last;
7848905a67cSAndy Whitcroft		}
7858905a67cSAndy Whitcroft
78613214adfSAndy Whitcroft		# An else is really a conditional as long as its not else if
787773647a0SAndy Whitcroft		if ($level == 0 && $coff_set == 0 &&
788773647a0SAndy Whitcroft				(!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
789773647a0SAndy Whitcroft				$remainder =~ /^(else)(?:\s|{)/ &&
790773647a0SAndy Whitcroft				$remainder !~ /^else\s+if\b/) {
791773647a0SAndy Whitcroft			$coff = $off + length($1) - 1;
792773647a0SAndy Whitcroft			$coff_set = 1;
793773647a0SAndy Whitcroft			#warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
794773647a0SAndy Whitcroft			#warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
79513214adfSAndy Whitcroft		}
79613214adfSAndy Whitcroft
7978905a67cSAndy Whitcroft		if (($type eq '' || $type eq '(') && $c eq '(') {
7988905a67cSAndy Whitcroft			$level++;
7998905a67cSAndy Whitcroft			$type = '(';
8008905a67cSAndy Whitcroft		}
8018905a67cSAndy Whitcroft		if ($type eq '(' && $c eq ')') {
8028905a67cSAndy Whitcroft			$level--;
8038905a67cSAndy Whitcroft			$type = ($level != 0)? '(' : '';
8048905a67cSAndy Whitcroft
8058905a67cSAndy Whitcroft			if ($level == 0 && $coff < $soff) {
8068905a67cSAndy Whitcroft				$coff = $off;
807773647a0SAndy Whitcroft				$coff_set = 1;
808773647a0SAndy Whitcroft				#warn "CSB: mark coff<$coff>\n";
8098905a67cSAndy Whitcroft			}
8108905a67cSAndy Whitcroft		}
8118905a67cSAndy Whitcroft		if (($type eq '' || $type eq '{') && $c eq '{') {
8128905a67cSAndy Whitcroft			$level++;
8138905a67cSAndy Whitcroft			$type = '{';
8148905a67cSAndy Whitcroft		}
8158905a67cSAndy Whitcroft		if ($type eq '{' && $c eq '}') {
8168905a67cSAndy Whitcroft			$level--;
8178905a67cSAndy Whitcroft			$type = ($level != 0)? '{' : '';
8188905a67cSAndy Whitcroft
8198905a67cSAndy Whitcroft			if ($level == 0) {
820b998e001SPatrick Pannuto				if (substr($blk, $off + 1, 1) eq ';') {
821b998e001SPatrick Pannuto					$off++;
822b998e001SPatrick Pannuto				}
8238905a67cSAndy Whitcroft				last;
8248905a67cSAndy Whitcroft			}
8258905a67cSAndy Whitcroft		}
826f74bd194SAndy Whitcroft		# Preprocessor commands end at the newline unless escaped.
827f74bd194SAndy Whitcroft		if ($type eq '#' && $c eq "\n" && $p ne "\\") {
828f74bd194SAndy Whitcroft			$level--;
829f74bd194SAndy Whitcroft			$type = '';
830f74bd194SAndy Whitcroft			$off++;
831f74bd194SAndy Whitcroft			last;
832f74bd194SAndy Whitcroft		}
8338905a67cSAndy Whitcroft		$off++;
8348905a67cSAndy Whitcroft	}
835a3bb97a7SAndy Whitcroft	# We are truly at the end, so shuffle to the next line.
83613214adfSAndy Whitcroft	if ($off == $len) {
837a3bb97a7SAndy Whitcroft		$loff = $len + 1;
83813214adfSAndy Whitcroft		$line++;
83913214adfSAndy Whitcroft		$remain--;
84013214adfSAndy Whitcroft	}
8418905a67cSAndy Whitcroft
8428905a67cSAndy Whitcroft	my $statement = substr($blk, $soff, $off - $soff + 1);
8438905a67cSAndy Whitcroft	my $condition = substr($blk, $soff, $coff - $soff + 1);
8448905a67cSAndy Whitcroft
8458905a67cSAndy Whitcroft	#warn "STATEMENT<$statement>\n";
8468905a67cSAndy Whitcroft	#warn "CONDITION<$condition>\n";
8478905a67cSAndy Whitcroft
848773647a0SAndy Whitcroft	#print "coff<$coff> soff<$off> loff<$loff>\n";
84913214adfSAndy Whitcroft
85013214adfSAndy Whitcroft	return ($statement, $condition,
85113214adfSAndy Whitcroft			$line, $remain + 1, $off - $loff + 1, $level);
85213214adfSAndy Whitcroft}
85313214adfSAndy Whitcroft
854cf655043SAndy Whitcroftsub statement_lines {
855cf655043SAndy Whitcroft	my ($stmt) = @_;
856cf655043SAndy Whitcroft
857cf655043SAndy Whitcroft	# Strip the diff line prefixes and rip blank lines at start and end.
858cf655043SAndy Whitcroft	$stmt =~ s/(^|\n)./$1/g;
859cf655043SAndy Whitcroft	$stmt =~ s/^\s*//;
860cf655043SAndy Whitcroft	$stmt =~ s/\s*$//;
861cf655043SAndy Whitcroft
862cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
863cf655043SAndy Whitcroft
864cf655043SAndy Whitcroft	return $#stmt_lines + 2;
865cf655043SAndy Whitcroft}
866cf655043SAndy Whitcroft
867cf655043SAndy Whitcroftsub statement_rawlines {
868cf655043SAndy Whitcroft	my ($stmt) = @_;
869cf655043SAndy Whitcroft
870cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
871cf655043SAndy Whitcroft
872cf655043SAndy Whitcroft	return $#stmt_lines + 2;
873cf655043SAndy Whitcroft}
874cf655043SAndy Whitcroft
875cf655043SAndy Whitcroftsub statement_block_size {
876cf655043SAndy Whitcroft	my ($stmt) = @_;
877cf655043SAndy Whitcroft
878cf655043SAndy Whitcroft	$stmt =~ s/(^|\n)./$1/g;
879cf655043SAndy Whitcroft	$stmt =~ s/^\s*{//;
880cf655043SAndy Whitcroft	$stmt =~ s/}\s*$//;
881cf655043SAndy Whitcroft	$stmt =~ s/^\s*//;
882cf655043SAndy Whitcroft	$stmt =~ s/\s*$//;
883cf655043SAndy Whitcroft
884cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
885cf655043SAndy Whitcroft	my @stmt_statements = ($stmt =~ /;/g);
886cf655043SAndy Whitcroft
887cf655043SAndy Whitcroft	my $stmt_lines = $#stmt_lines + 2;
888cf655043SAndy Whitcroft	my $stmt_statements = $#stmt_statements + 1;
889cf655043SAndy Whitcroft
890cf655043SAndy Whitcroft	if ($stmt_lines > $stmt_statements) {
891cf655043SAndy Whitcroft		return $stmt_lines;
892cf655043SAndy Whitcroft	} else {
893cf655043SAndy Whitcroft		return $stmt_statements;
894cf655043SAndy Whitcroft	}
895cf655043SAndy Whitcroft}
896cf655043SAndy Whitcroft
89713214adfSAndy Whitcroftsub ctx_statement_full {
89813214adfSAndy Whitcroft	my ($linenr, $remain, $off) = @_;
89913214adfSAndy Whitcroft	my ($statement, $condition, $level);
90013214adfSAndy Whitcroft
90113214adfSAndy Whitcroft	my (@chunks);
90213214adfSAndy Whitcroft
903cf655043SAndy Whitcroft	# Grab the first conditional/block pair.
90413214adfSAndy Whitcroft	($statement, $condition, $linenr, $remain, $off, $level) =
90513214adfSAndy Whitcroft				ctx_statement_block($linenr, $remain, $off);
906773647a0SAndy Whitcroft	#print "F: c<$condition> s<$statement> remain<$remain>\n";
90713214adfSAndy Whitcroft	push(@chunks, [ $condition, $statement ]);
908cf655043SAndy Whitcroft	if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
909cf655043SAndy Whitcroft		return ($level, $linenr, @chunks);
910cf655043SAndy Whitcroft	}
911cf655043SAndy Whitcroft
912cf655043SAndy Whitcroft	# Pull in the following conditional/block pairs and see if they
913cf655043SAndy Whitcroft	# could continue the statement.
914cf655043SAndy Whitcroft	for (;;) {
91513214adfSAndy Whitcroft		($statement, $condition, $linenr, $remain, $off, $level) =
91613214adfSAndy Whitcroft				ctx_statement_block($linenr, $remain, $off);
917cf655043SAndy Whitcroft		#print "C: c<$condition> s<$statement> remain<$remain>\n";
918773647a0SAndy Whitcroft		last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
919cf655043SAndy Whitcroft		#print "C: push\n";
920cf655043SAndy Whitcroft		push(@chunks, [ $condition, $statement ]);
92113214adfSAndy Whitcroft	}
92213214adfSAndy Whitcroft
92313214adfSAndy Whitcroft	return ($level, $linenr, @chunks);
9248905a67cSAndy Whitcroft}
9258905a67cSAndy Whitcroft
9264a0df2efSAndy Whitcroftsub ctx_block_get {
927f0a594c1SAndy Whitcroft	my ($linenr, $remain, $outer, $open, $close, $off) = @_;
9284a0df2efSAndy Whitcroft	my $line;
9294a0df2efSAndy Whitcroft	my $start = $linenr - 1;
9304a0df2efSAndy Whitcroft	my $blk = '';
9314a0df2efSAndy Whitcroft	my @o;
9324a0df2efSAndy Whitcroft	my @c;
9334a0df2efSAndy Whitcroft	my @res = ();
9344a0df2efSAndy Whitcroft
935f0a594c1SAndy Whitcroft	my $level = 0;
9364635f4fbSAndy Whitcroft	my @stack = ($level);
93700df344fSAndy Whitcroft	for ($line = $start; $remain > 0; $line++) {
93800df344fSAndy Whitcroft		next if ($rawlines[$line] =~ /^-/);
93900df344fSAndy Whitcroft		$remain--;
94000df344fSAndy Whitcroft
94100df344fSAndy Whitcroft		$blk .= $rawlines[$line];
9424635f4fbSAndy Whitcroft
9434635f4fbSAndy Whitcroft		# Handle nested #if/#else.
94401464f30SAndy Whitcroft		if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
9454635f4fbSAndy Whitcroft			push(@stack, $level);
94601464f30SAndy Whitcroft		} elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
9474635f4fbSAndy Whitcroft			$level = $stack[$#stack - 1];
94801464f30SAndy Whitcroft		} elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
9494635f4fbSAndy Whitcroft			$level = pop(@stack);
9504635f4fbSAndy Whitcroft		}
9514635f4fbSAndy Whitcroft
95201464f30SAndy Whitcroft		foreach my $c (split(//, $lines[$line])) {
953f0a594c1SAndy Whitcroft			##print "C<$c>L<$level><$open$close>O<$off>\n";
954f0a594c1SAndy Whitcroft			if ($off > 0) {
955f0a594c1SAndy Whitcroft				$off--;
956f0a594c1SAndy Whitcroft				next;
957f0a594c1SAndy Whitcroft			}
9584a0df2efSAndy Whitcroft
959f0a594c1SAndy Whitcroft			if ($c eq $close && $level > 0) {
960f0a594c1SAndy Whitcroft				$level--;
961f0a594c1SAndy Whitcroft				last if ($level == 0);
962f0a594c1SAndy Whitcroft			} elsif ($c eq $open) {
963f0a594c1SAndy Whitcroft				$level++;
964f0a594c1SAndy Whitcroft			}
965f0a594c1SAndy Whitcroft		}
9664a0df2efSAndy Whitcroft
967f0a594c1SAndy Whitcroft		if (!$outer || $level <= 1) {
96800df344fSAndy Whitcroft			push(@res, $rawlines[$line]);
9694a0df2efSAndy Whitcroft		}
9704a0df2efSAndy Whitcroft
971f0a594c1SAndy Whitcroft		last if ($level == 0);
9724a0df2efSAndy Whitcroft	}
9734a0df2efSAndy Whitcroft
974f0a594c1SAndy Whitcroft	return ($level, @res);
9754a0df2efSAndy Whitcroft}
9764a0df2efSAndy Whitcroftsub ctx_block_outer {
9774a0df2efSAndy Whitcroft	my ($linenr, $remain) = @_;
9784a0df2efSAndy Whitcroft
979f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
980f0a594c1SAndy Whitcroft	return @r;
9814a0df2efSAndy Whitcroft}
9824a0df2efSAndy Whitcroftsub ctx_block {
9834a0df2efSAndy Whitcroft	my ($linenr, $remain) = @_;
9844a0df2efSAndy Whitcroft
985f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
986f0a594c1SAndy Whitcroft	return @r;
987653d4876SAndy Whitcroft}
988653d4876SAndy Whitcroftsub ctx_statement {
989f0a594c1SAndy Whitcroft	my ($linenr, $remain, $off) = @_;
990f0a594c1SAndy Whitcroft
991f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
992f0a594c1SAndy Whitcroft	return @r;
993f0a594c1SAndy Whitcroft}
994f0a594c1SAndy Whitcroftsub ctx_block_level {
995653d4876SAndy Whitcroft	my ($linenr, $remain) = @_;
996653d4876SAndy Whitcroft
997f0a594c1SAndy Whitcroft	return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
9984a0df2efSAndy Whitcroft}
9999c0ca6f9SAndy Whitcroftsub ctx_statement_level {
10009c0ca6f9SAndy Whitcroft	my ($linenr, $remain, $off) = @_;
10019c0ca6f9SAndy Whitcroft
10029c0ca6f9SAndy Whitcroft	return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
10039c0ca6f9SAndy Whitcroft}
10044a0df2efSAndy Whitcroft
10054a0df2efSAndy Whitcroftsub ctx_locate_comment {
10064a0df2efSAndy Whitcroft	my ($first_line, $end_line) = @_;
10074a0df2efSAndy Whitcroft
10084a0df2efSAndy Whitcroft	# Catch a comment on the end of the line itself.
1009beae6332SAndy Whitcroft	my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
10104a0df2efSAndy Whitcroft	return $current_comment if (defined $current_comment);
10114a0df2efSAndy Whitcroft
10124a0df2efSAndy Whitcroft	# Look through the context and try and figure out if there is a
10134a0df2efSAndy Whitcroft	# comment.
10144a0df2efSAndy Whitcroft	my $in_comment = 0;
10154a0df2efSAndy Whitcroft	$current_comment = '';
10164a0df2efSAndy Whitcroft	for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
101700df344fSAndy Whitcroft		my $line = $rawlines[$linenr - 1];
101800df344fSAndy Whitcroft		#warn "           $line\n";
10194a0df2efSAndy Whitcroft		if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
10204a0df2efSAndy Whitcroft			$in_comment = 1;
10214a0df2efSAndy Whitcroft		}
10224a0df2efSAndy Whitcroft		if ($line =~ m@/\*@) {
10234a0df2efSAndy Whitcroft			$in_comment = 1;
10244a0df2efSAndy Whitcroft		}
10254a0df2efSAndy Whitcroft		if (!$in_comment && $current_comment ne '') {
10264a0df2efSAndy Whitcroft			$current_comment = '';
10274a0df2efSAndy Whitcroft		}
10284a0df2efSAndy Whitcroft		$current_comment .= $line . "\n" if ($in_comment);
10294a0df2efSAndy Whitcroft		if ($line =~ m@\*/@) {
10304a0df2efSAndy Whitcroft			$in_comment = 0;
10314a0df2efSAndy Whitcroft		}
10324a0df2efSAndy Whitcroft	}
10334a0df2efSAndy Whitcroft
10344a0df2efSAndy Whitcroft	chomp($current_comment);
10354a0df2efSAndy Whitcroft	return($current_comment);
10364a0df2efSAndy Whitcroft}
10374a0df2efSAndy Whitcroftsub ctx_has_comment {
10384a0df2efSAndy Whitcroft	my ($first_line, $end_line) = @_;
10394a0df2efSAndy Whitcroft	my $cmt = ctx_locate_comment($first_line, $end_line);
10404a0df2efSAndy Whitcroft
104100df344fSAndy Whitcroft	##print "LINE: $rawlines[$end_line - 1 ]\n";
10424a0df2efSAndy Whitcroft	##print "CMMT: $cmt\n";
10434a0df2efSAndy Whitcroft
10444a0df2efSAndy Whitcroft	return ($cmt ne '');
10454a0df2efSAndy Whitcroft}
10464a0df2efSAndy Whitcroft
10474d001e4dSAndy Whitcroftsub raw_line {
10484d001e4dSAndy Whitcroft	my ($linenr, $cnt) = @_;
10494d001e4dSAndy Whitcroft
10504d001e4dSAndy Whitcroft	my $offset = $linenr - 1;
10514d001e4dSAndy Whitcroft	$cnt++;
10524d001e4dSAndy Whitcroft
10534d001e4dSAndy Whitcroft	my $line;
10544d001e4dSAndy Whitcroft	while ($cnt) {
10554d001e4dSAndy Whitcroft		$line = $rawlines[$offset++];
10564d001e4dSAndy Whitcroft		next if (defined($line) && $line =~ /^-/);
10574d001e4dSAndy Whitcroft		$cnt--;
10584d001e4dSAndy Whitcroft	}
10594d001e4dSAndy Whitcroft
10604d001e4dSAndy Whitcroft	return $line;
10614d001e4dSAndy Whitcroft}
10624d001e4dSAndy Whitcroft
10630a920b5bSAndy Whitcroftsub cat_vet {
10640a920b5bSAndy Whitcroft	my ($vet) = @_;
10659c0ca6f9SAndy Whitcroft	my ($res, $coded);
10660a920b5bSAndy Whitcroft
10679c0ca6f9SAndy Whitcroft	$res = '';
10686c72ffaaSAndy Whitcroft	while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
10696c72ffaaSAndy Whitcroft		$res .= $1;
10706c72ffaaSAndy Whitcroft		if ($2 ne '') {
10719c0ca6f9SAndy Whitcroft			$coded = sprintf("^%c", unpack('C', $2) + 64);
10726c72ffaaSAndy Whitcroft			$res .= $coded;
10736c72ffaaSAndy Whitcroft		}
10749c0ca6f9SAndy Whitcroft	}
10759c0ca6f9SAndy Whitcroft	$res =~ s/$/\$/;
10760a920b5bSAndy Whitcroft
10779c0ca6f9SAndy Whitcroft	return $res;
10780a920b5bSAndy Whitcroft}
10790a920b5bSAndy Whitcroft
1080c2fdda0dSAndy Whitcroftmy $av_preprocessor = 0;
1081cf655043SAndy Whitcroftmy $av_pending;
1082c2fdda0dSAndy Whitcroftmy @av_paren_type;
10831f65f947SAndy Whitcroftmy $av_pend_colon;
1084c2fdda0dSAndy Whitcroft
1085c2fdda0dSAndy Whitcroftsub annotate_reset {
1086c2fdda0dSAndy Whitcroft	$av_preprocessor = 0;
1087cf655043SAndy Whitcroft	$av_pending = '_';
1088cf655043SAndy Whitcroft	@av_paren_type = ('E');
10891f65f947SAndy Whitcroft	$av_pend_colon = 'O';
1090c2fdda0dSAndy Whitcroft}
1091c2fdda0dSAndy Whitcroft
10926c72ffaaSAndy Whitcroftsub annotate_values {
10936c72ffaaSAndy Whitcroft	my ($stream, $type) = @_;
10946c72ffaaSAndy Whitcroft
10956c72ffaaSAndy Whitcroft	my $res;
10961f65f947SAndy Whitcroft	my $var = '_' x length($stream);
10976c72ffaaSAndy Whitcroft	my $cur = $stream;
10986c72ffaaSAndy Whitcroft
1099c2fdda0dSAndy Whitcroft	print "$stream\n" if ($dbg_values > 1);
11006c72ffaaSAndy Whitcroft
11016c72ffaaSAndy Whitcroft	while (length($cur)) {
1102773647a0SAndy Whitcroft		@av_paren_type = ('E') if ($#av_paren_type < 0);
1103cf655043SAndy Whitcroft		print " <" . join('', @av_paren_type) .
1104171ae1a4SAndy Whitcroft				"> <$type> <$av_pending>" if ($dbg_values > 1);
11056c72ffaaSAndy Whitcroft		if ($cur =~ /^(\s+)/o) {
1106c2fdda0dSAndy Whitcroft			print "WS($1)\n" if ($dbg_values > 1);
1107c2fdda0dSAndy Whitcroft			if ($1 =~ /\n/ && $av_preprocessor) {
1108cf655043SAndy Whitcroft				$type = pop(@av_paren_type);
1109c2fdda0dSAndy Whitcroft				$av_preprocessor = 0;
11106c72ffaaSAndy Whitcroft			}
11116c72ffaaSAndy Whitcroft
1112c023e473SFlorian Mickler		} elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
11139446ef56SAndy Whitcroft			print "CAST($1)\n" if ($dbg_values > 1);
11149446ef56SAndy Whitcroft			push(@av_paren_type, $type);
1115addcdceaSAndy Whitcroft			$type = 'c';
11169446ef56SAndy Whitcroft
1117e91b6e26SAndy Whitcroft		} elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
1118c2fdda0dSAndy Whitcroft			print "DECLARE($1)\n" if ($dbg_values > 1);
11196c72ffaaSAndy Whitcroft			$type = 'T';
11206c72ffaaSAndy Whitcroft
1121389a2fe5SAndy Whitcroft		} elsif ($cur =~ /^($Modifier)\s*/) {
1122389a2fe5SAndy Whitcroft			print "MODIFIER($1)\n" if ($dbg_values > 1);
1123389a2fe5SAndy Whitcroft			$type = 'T';
1124389a2fe5SAndy Whitcroft
1125c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
1126171ae1a4SAndy Whitcroft			print "DEFINE($1,$2)\n" if ($dbg_values > 1);
1127c2fdda0dSAndy Whitcroft			$av_preprocessor = 1;
1128171ae1a4SAndy Whitcroft			push(@av_paren_type, $type);
1129171ae1a4SAndy Whitcroft			if ($2 ne '') {
1130cf655043SAndy Whitcroft				$av_pending = 'N';
1131171ae1a4SAndy Whitcroft			}
1132171ae1a4SAndy Whitcroft			$type = 'E';
1133171ae1a4SAndy Whitcroft
1134c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
1135171ae1a4SAndy Whitcroft			print "UNDEF($1)\n" if ($dbg_values > 1);
1136171ae1a4SAndy Whitcroft			$av_preprocessor = 1;
1137171ae1a4SAndy Whitcroft			push(@av_paren_type, $type);
11386c72ffaaSAndy Whitcroft
1139c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
1140cf655043SAndy Whitcroft			print "PRE_START($1)\n" if ($dbg_values > 1);
1141c2fdda0dSAndy Whitcroft			$av_preprocessor = 1;
1142cf655043SAndy Whitcroft
1143cf655043SAndy Whitcroft			push(@av_paren_type, $type);
1144cf655043SAndy Whitcroft			push(@av_paren_type, $type);
1145171ae1a4SAndy Whitcroft			$type = 'E';
1146cf655043SAndy Whitcroft
1147c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
1148cf655043SAndy Whitcroft			print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1149cf655043SAndy Whitcroft			$av_preprocessor = 1;
1150cf655043SAndy Whitcroft
1151cf655043SAndy Whitcroft			push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1152cf655043SAndy Whitcroft
1153171ae1a4SAndy Whitcroft			$type = 'E';
1154cf655043SAndy Whitcroft
1155c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:endif))/o) {
1156cf655043SAndy Whitcroft			print "PRE_END($1)\n" if ($dbg_values > 1);
1157cf655043SAndy Whitcroft
1158cf655043SAndy Whitcroft			$av_preprocessor = 1;
1159cf655043SAndy Whitcroft
1160cf655043SAndy Whitcroft			# Assume all arms of the conditional end as this
1161cf655043SAndy Whitcroft			# one does, and continue as if the #endif was not here.
1162cf655043SAndy Whitcroft			pop(@av_paren_type);
1163cf655043SAndy Whitcroft			push(@av_paren_type, $type);
1164171ae1a4SAndy Whitcroft			$type = 'E';
11656c72ffaaSAndy Whitcroft
11666c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\\\n)/o) {
1167c2fdda0dSAndy Whitcroft			print "PRECONT($1)\n" if ($dbg_values > 1);
11686c72ffaaSAndy Whitcroft
1169171ae1a4SAndy Whitcroft		} elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1170171ae1a4SAndy Whitcroft			print "ATTR($1)\n" if ($dbg_values > 1);
1171171ae1a4SAndy Whitcroft			$av_pending = $type;
1172171ae1a4SAndy Whitcroft			$type = 'N';
1173171ae1a4SAndy Whitcroft
11746c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
1175c2fdda0dSAndy Whitcroft			print "SIZEOF($1)\n" if ($dbg_values > 1);
11766c72ffaaSAndy Whitcroft			if (defined $2) {
1177cf655043SAndy Whitcroft				$av_pending = 'V';
11786c72ffaaSAndy Whitcroft			}
11796c72ffaaSAndy Whitcroft			$type = 'N';
11806c72ffaaSAndy Whitcroft
118114b111c1SAndy Whitcroft		} elsif ($cur =~ /^(if|while|for)\b/o) {
1182c2fdda0dSAndy Whitcroft			print "COND($1)\n" if ($dbg_values > 1);
118314b111c1SAndy Whitcroft			$av_pending = 'E';
11846c72ffaaSAndy Whitcroft			$type = 'N';
11856c72ffaaSAndy Whitcroft
11861f65f947SAndy Whitcroft		} elsif ($cur =~/^(case)/o) {
11871f65f947SAndy Whitcroft			print "CASE($1)\n" if ($dbg_values > 1);
11881f65f947SAndy Whitcroft			$av_pend_colon = 'C';
11891f65f947SAndy Whitcroft			$type = 'N';
11901f65f947SAndy Whitcroft
119114b111c1SAndy Whitcroft		} elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
1192c2fdda0dSAndy Whitcroft			print "KEYWORD($1)\n" if ($dbg_values > 1);
11936c72ffaaSAndy Whitcroft			$type = 'N';
11946c72ffaaSAndy Whitcroft
11956c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\()/o) {
1196c2fdda0dSAndy Whitcroft			print "PAREN('$1')\n" if ($dbg_values > 1);
1197cf655043SAndy Whitcroft			push(@av_paren_type, $av_pending);
1198cf655043SAndy Whitcroft			$av_pending = '_';
11996c72ffaaSAndy Whitcroft			$type = 'N';
12006c72ffaaSAndy Whitcroft
12016c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\))/o) {
1202cf655043SAndy Whitcroft			my $new_type = pop(@av_paren_type);
1203cf655043SAndy Whitcroft			if ($new_type ne '_') {
1204cf655043SAndy Whitcroft				$type = $new_type;
1205c2fdda0dSAndy Whitcroft				print "PAREN('$1') -> $type\n"
1206c2fdda0dSAndy Whitcroft							if ($dbg_values > 1);
12076c72ffaaSAndy Whitcroft			} else {
1208c2fdda0dSAndy Whitcroft				print "PAREN('$1')\n" if ($dbg_values > 1);
12096c72ffaaSAndy Whitcroft			}
12106c72ffaaSAndy Whitcroft
1211c8cb2ca3SAndy Whitcroft		} elsif ($cur =~ /^($Ident)\s*\(/o) {
1212c2fdda0dSAndy Whitcroft			print "FUNC($1)\n" if ($dbg_values > 1);
1213c8cb2ca3SAndy Whitcroft			$type = 'V';
1214cf655043SAndy Whitcroft			$av_pending = 'V';
12156c72ffaaSAndy Whitcroft
12168e761b04SAndy Whitcroft		} elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
12178e761b04SAndy Whitcroft			if (defined $2 && $type eq 'C' || $type eq 'T') {
12181f65f947SAndy Whitcroft				$av_pend_colon = 'B';
12198e761b04SAndy Whitcroft			} elsif ($type eq 'E') {
12208e761b04SAndy Whitcroft				$av_pend_colon = 'L';
12211f65f947SAndy Whitcroft			}
12221f65f947SAndy Whitcroft			print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
12231f65f947SAndy Whitcroft			$type = 'V';
12241f65f947SAndy Whitcroft
12256c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Ident|$Constant)/o) {
1226c2fdda0dSAndy Whitcroft			print "IDENT($1)\n" if ($dbg_values > 1);
12276c72ffaaSAndy Whitcroft			$type = 'V';
12286c72ffaaSAndy Whitcroft
12296c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Assignment)/o) {
1230c2fdda0dSAndy Whitcroft			print "ASSIGN($1)\n" if ($dbg_values > 1);
12316c72ffaaSAndy Whitcroft			$type = 'N';
12326c72ffaaSAndy Whitcroft
1233cf655043SAndy Whitcroft		} elsif ($cur =~/^(;|{|})/) {
1234c2fdda0dSAndy Whitcroft			print "END($1)\n" if ($dbg_values > 1);
123513214adfSAndy Whitcroft			$type = 'E';
12361f65f947SAndy Whitcroft			$av_pend_colon = 'O';
123713214adfSAndy Whitcroft
12388e761b04SAndy Whitcroft		} elsif ($cur =~/^(,)/) {
12398e761b04SAndy Whitcroft			print "COMMA($1)\n" if ($dbg_values > 1);
12408e761b04SAndy Whitcroft			$type = 'C';
12418e761b04SAndy Whitcroft
12421f65f947SAndy Whitcroft		} elsif ($cur =~ /^(\?)/o) {
12431f65f947SAndy Whitcroft			print "QUESTION($1)\n" if ($dbg_values > 1);
12441f65f947SAndy Whitcroft			$type = 'N';
12451f65f947SAndy Whitcroft
12461f65f947SAndy Whitcroft		} elsif ($cur =~ /^(:)/o) {
12471f65f947SAndy Whitcroft			print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
12481f65f947SAndy Whitcroft
12491f65f947SAndy Whitcroft			substr($var, length($res), 1, $av_pend_colon);
12501f65f947SAndy Whitcroft			if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
12511f65f947SAndy Whitcroft				$type = 'E';
12521f65f947SAndy Whitcroft			} else {
12531f65f947SAndy Whitcroft				$type = 'N';
12541f65f947SAndy Whitcroft			}
12551f65f947SAndy Whitcroft			$av_pend_colon = 'O';
12561f65f947SAndy Whitcroft
12578e761b04SAndy Whitcroft		} elsif ($cur =~ /^(\[)/o) {
125813214adfSAndy Whitcroft			print "CLOSE($1)\n" if ($dbg_values > 1);
12596c72ffaaSAndy Whitcroft			$type = 'N';
12606c72ffaaSAndy Whitcroft
12610d413866SAndy Whitcroft		} elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
126274048ed8SAndy Whitcroft			my $variant;
126374048ed8SAndy Whitcroft
126474048ed8SAndy Whitcroft			print "OPV($1)\n" if ($dbg_values > 1);
126574048ed8SAndy Whitcroft			if ($type eq 'V') {
126674048ed8SAndy Whitcroft				$variant = 'B';
126774048ed8SAndy Whitcroft			} else {
126874048ed8SAndy Whitcroft				$variant = 'U';
126974048ed8SAndy Whitcroft			}
127074048ed8SAndy Whitcroft
127174048ed8SAndy Whitcroft			substr($var, length($res), 1, $variant);
127274048ed8SAndy Whitcroft			$type = 'N';
127374048ed8SAndy Whitcroft
12746c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Operators)/o) {
1275c2fdda0dSAndy Whitcroft			print "OP($1)\n" if ($dbg_values > 1);
12766c72ffaaSAndy Whitcroft			if ($1 ne '++' && $1 ne '--') {
12776c72ffaaSAndy Whitcroft				$type = 'N';
12786c72ffaaSAndy Whitcroft			}
12796c72ffaaSAndy Whitcroft
12806c72ffaaSAndy Whitcroft		} elsif ($cur =~ /(^.)/o) {
1281c2fdda0dSAndy Whitcroft			print "C($1)\n" if ($dbg_values > 1);
12826c72ffaaSAndy Whitcroft		}
12836c72ffaaSAndy Whitcroft		if (defined $1) {
12846c72ffaaSAndy Whitcroft			$cur = substr($cur, length($1));
12856c72ffaaSAndy Whitcroft			$res .= $type x length($1);
12866c72ffaaSAndy Whitcroft		}
12876c72ffaaSAndy Whitcroft	}
12886c72ffaaSAndy Whitcroft
12891f65f947SAndy Whitcroft	return ($res, $var);
12906c72ffaaSAndy Whitcroft}
12916c72ffaaSAndy Whitcroft
12928905a67cSAndy Whitcroftsub possible {
129313214adfSAndy Whitcroft	my ($possible, $line) = @_;
12949a974fdbSAndy Whitcroft	my $notPermitted = qr{(?:
12950776e594SAndy Whitcroft		^(?:
12960776e594SAndy Whitcroft			$Modifier|
12970776e594SAndy Whitcroft			$Storage|
12980776e594SAndy Whitcroft			$Type|
12999a974fdbSAndy Whitcroft			DEFINE_\S+
13009a974fdbSAndy Whitcroft		)$|
13019a974fdbSAndy Whitcroft		^(?:
13020776e594SAndy Whitcroft			goto|
13030776e594SAndy Whitcroft			return|
13040776e594SAndy Whitcroft			case|
13050776e594SAndy Whitcroft			else|
13060776e594SAndy Whitcroft			asm|__asm__|
130789a88353SAndy Whitcroft			do|
130889a88353SAndy Whitcroft			\#|
130989a88353SAndy Whitcroft			\#\#|
13109a974fdbSAndy Whitcroft		)(?:\s|$)|
13110776e594SAndy Whitcroft		^(?:typedef|struct|enum)\b
13129a974fdbSAndy Whitcroft	    )}x;
13139a974fdbSAndy Whitcroft	warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
13149a974fdbSAndy Whitcroft	if ($possible !~ $notPermitted) {
1315c45dcabdSAndy Whitcroft		# Check for modifiers.
1316c45dcabdSAndy Whitcroft		$possible =~ s/\s*$Storage\s*//g;
1317c45dcabdSAndy Whitcroft		$possible =~ s/\s*$Sparse\s*//g;
1318c45dcabdSAndy Whitcroft		if ($possible =~ /^\s*$/) {
1319c45dcabdSAndy Whitcroft
1320c45dcabdSAndy Whitcroft		} elsif ($possible =~ /\s/) {
1321c45dcabdSAndy Whitcroft			$possible =~ s/\s*$Type\s*//g;
1322d2506586SAndy Whitcroft			for my $modifier (split(' ', $possible)) {
13239a974fdbSAndy Whitcroft				if ($modifier !~ $notPermitted) {
1324d2506586SAndy Whitcroft					warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1325d2506586SAndy Whitcroft					push(@modifierList, $modifier);
1326d2506586SAndy Whitcroft				}
13279a974fdbSAndy Whitcroft			}
1328c45dcabdSAndy Whitcroft
1329c45dcabdSAndy Whitcroft		} else {
133013214adfSAndy Whitcroft			warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
13318905a67cSAndy Whitcroft			push(@typeList, $possible);
1332c45dcabdSAndy Whitcroft		}
13338905a67cSAndy Whitcroft		build_types();
13340776e594SAndy Whitcroft	} else {
13350776e594SAndy Whitcroft		warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
13368905a67cSAndy Whitcroft	}
13378905a67cSAndy Whitcroft}
13388905a67cSAndy Whitcroft
13396c72ffaaSAndy Whitcroftmy $prefix = '';
13406c72ffaaSAndy Whitcroft
1341000d1cc1SJoe Perchessub show_type {
1342000d1cc1SJoe Perches       return !defined $ignore_type{$_[0]};
1343000d1cc1SJoe Perches}
1344000d1cc1SJoe Perches
1345f0a594c1SAndy Whitcroftsub report {
1346000d1cc1SJoe Perches	if (!show_type($_[1]) ||
1347000d1cc1SJoe Perches	    (defined $tst_only && $_[2] !~ /\Q$tst_only\E/)) {
1348773647a0SAndy Whitcroft		return 0;
1349773647a0SAndy Whitcroft	}
1350000d1cc1SJoe Perches	my $line;
1351000d1cc1SJoe Perches	if ($show_types) {
1352000d1cc1SJoe Perches		$line = "$prefix$_[0]:$_[1]: $_[2]\n";
1353000d1cc1SJoe Perches	} else {
1354000d1cc1SJoe Perches		$line = "$prefix$_[0]: $_[2]\n";
1355000d1cc1SJoe Perches	}
13568905a67cSAndy Whitcroft	$line = (split('\n', $line))[0] . "\n" if ($terse);
13578905a67cSAndy Whitcroft
135813214adfSAndy Whitcroft	push(our @report, $line);
1359773647a0SAndy Whitcroft
1360773647a0SAndy Whitcroft	return 1;
1361f0a594c1SAndy Whitcroft}
1362f0a594c1SAndy Whitcroftsub report_dump {
136313214adfSAndy Whitcroft	our @report;
1364f0a594c1SAndy Whitcroft}
1365000d1cc1SJoe Perches
1366de7d4f0eSAndy Whitcroftsub ERROR {
1367000d1cc1SJoe Perches	if (report("ERROR", $_[0], $_[1])) {
1368de7d4f0eSAndy Whitcroft		our $clean = 0;
13696c72ffaaSAndy Whitcroft		our $cnt_error++;
13703705ce5bSJoe Perches		return 1;
1371de7d4f0eSAndy Whitcroft	}
13723705ce5bSJoe Perches	return 0;
1373773647a0SAndy Whitcroft}
1374de7d4f0eSAndy Whitcroftsub WARN {
1375000d1cc1SJoe Perches	if (report("WARNING", $_[0], $_[1])) {
1376de7d4f0eSAndy Whitcroft		our $clean = 0;
13776c72ffaaSAndy Whitcroft		our $cnt_warn++;
13783705ce5bSJoe Perches		return 1;
1379de7d4f0eSAndy Whitcroft	}
13803705ce5bSJoe Perches	return 0;
1381773647a0SAndy Whitcroft}
1382de7d4f0eSAndy Whitcroftsub CHK {
1383000d1cc1SJoe Perches	if ($check && report("CHECK", $_[0], $_[1])) {
1384de7d4f0eSAndy Whitcroft		our $clean = 0;
13856c72ffaaSAndy Whitcroft		our $cnt_chk++;
13863705ce5bSJoe Perches		return 1;
13876c72ffaaSAndy Whitcroft	}
13883705ce5bSJoe Perches	return 0;
1389de7d4f0eSAndy Whitcroft}
1390de7d4f0eSAndy Whitcroft
13916ecd9674SAndy Whitcroftsub check_absolute_file {
13926ecd9674SAndy Whitcroft	my ($absolute, $herecurr) = @_;
13936ecd9674SAndy Whitcroft	my $file = $absolute;
13946ecd9674SAndy Whitcroft
13956ecd9674SAndy Whitcroft	##print "absolute<$absolute>\n";
13966ecd9674SAndy Whitcroft
13976ecd9674SAndy Whitcroft	# See if any suffix of this path is a path within the tree.
13986ecd9674SAndy Whitcroft	while ($file =~ s@^[^/]*/@@) {
13996ecd9674SAndy Whitcroft		if (-f "$root/$file") {
14006ecd9674SAndy Whitcroft			##print "file<$file>\n";
14016ecd9674SAndy Whitcroft			last;
14026ecd9674SAndy Whitcroft		}
14036ecd9674SAndy Whitcroft	}
14046ecd9674SAndy Whitcroft	if (! -f _)  {
14056ecd9674SAndy Whitcroft		return 0;
14066ecd9674SAndy Whitcroft	}
14076ecd9674SAndy Whitcroft
14086ecd9674SAndy Whitcroft	# It is, so see if the prefix is acceptable.
14096ecd9674SAndy Whitcroft	my $prefix = $absolute;
14106ecd9674SAndy Whitcroft	substr($prefix, -length($file)) = '';
14116ecd9674SAndy Whitcroft
14126ecd9674SAndy Whitcroft	##print "prefix<$prefix>\n";
14136ecd9674SAndy Whitcroft	if ($prefix ne ".../") {
1414000d1cc1SJoe Perches		WARN("USE_RELATIVE_PATH",
1415000d1cc1SJoe Perches		     "use relative pathname instead of absolute in changelog text\n" . $herecurr);
14166ecd9674SAndy Whitcroft	}
14176ecd9674SAndy Whitcroft}
14186ecd9674SAndy Whitcroft
14193705ce5bSJoe Perchessub trim {
14203705ce5bSJoe Perches	my ($string) = @_;
14213705ce5bSJoe Perches
14223705ce5bSJoe Perches	$string =~ s/(^\s+|\s+$)//g;
14233705ce5bSJoe Perches
14243705ce5bSJoe Perches	return $string;
14253705ce5bSJoe Perches}
14263705ce5bSJoe Perches
14273705ce5bSJoe Perchessub tabify {
14283705ce5bSJoe Perches	my ($leading) = @_;
14293705ce5bSJoe Perches
14303705ce5bSJoe Perches	my $source_indent = 8;
14313705ce5bSJoe Perches	my $max_spaces_before_tab = $source_indent - 1;
14323705ce5bSJoe Perches	my $spaces_to_tab = " " x $source_indent;
14333705ce5bSJoe Perches
14343705ce5bSJoe Perches	#convert leading spaces to tabs
14353705ce5bSJoe Perches	1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
14363705ce5bSJoe Perches	#Remove spaces before a tab
14373705ce5bSJoe Perches	1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
14383705ce5bSJoe Perches
14393705ce5bSJoe Perches	return "$leading";
14403705ce5bSJoe Perches}
14413705ce5bSJoe Perches
1442d1fe9c09SJoe Perchessub pos_last_openparen {
1443d1fe9c09SJoe Perches	my ($line) = @_;
1444d1fe9c09SJoe Perches
1445d1fe9c09SJoe Perches	my $pos = 0;
1446d1fe9c09SJoe Perches
1447d1fe9c09SJoe Perches	my $opens = $line =~ tr/\(/\(/;
1448d1fe9c09SJoe Perches	my $closes = $line =~ tr/\)/\)/;
1449d1fe9c09SJoe Perches
1450d1fe9c09SJoe Perches	my $last_openparen = 0;
1451d1fe9c09SJoe Perches
1452d1fe9c09SJoe Perches	if (($opens == 0) || ($closes >= $opens)) {
1453d1fe9c09SJoe Perches		return -1;
1454d1fe9c09SJoe Perches	}
1455d1fe9c09SJoe Perches
1456d1fe9c09SJoe Perches	my $len = length($line);
1457d1fe9c09SJoe Perches
1458d1fe9c09SJoe Perches	for ($pos = 0; $pos < $len; $pos++) {
1459d1fe9c09SJoe Perches		my $string = substr($line, $pos);
1460d1fe9c09SJoe Perches		if ($string =~ /^($FuncArg|$balanced_parens)/) {
1461d1fe9c09SJoe Perches			$pos += length($1) - 1;
1462d1fe9c09SJoe Perches		} elsif (substr($line, $pos, 1) eq '(') {
1463d1fe9c09SJoe Perches			$last_openparen = $pos;
1464d1fe9c09SJoe Perches		} elsif (index($string, '(') == -1) {
1465d1fe9c09SJoe Perches			last;
1466d1fe9c09SJoe Perches		}
1467d1fe9c09SJoe Perches	}
1468d1fe9c09SJoe Perches
1469d1fe9c09SJoe Perches	return $last_openparen + 1;
1470d1fe9c09SJoe Perches}
1471d1fe9c09SJoe Perches
14720a920b5bSAndy Whitcroftsub process {
14730a920b5bSAndy Whitcroft	my $filename = shift;
14740a920b5bSAndy Whitcroft
14750a920b5bSAndy Whitcroft	my $linenr=0;
14760a920b5bSAndy Whitcroft	my $prevline="";
1477c2fdda0dSAndy Whitcroft	my $prevrawline="";
14780a920b5bSAndy Whitcroft	my $stashline="";
1479c2fdda0dSAndy Whitcroft	my $stashrawline="";
14800a920b5bSAndy Whitcroft
14814a0df2efSAndy Whitcroft	my $length;
14820a920b5bSAndy Whitcroft	my $indent;
14830a920b5bSAndy Whitcroft	my $previndent=0;
14840a920b5bSAndy Whitcroft	my $stashindent=0;
14850a920b5bSAndy Whitcroft
1486de7d4f0eSAndy Whitcroft	our $clean = 1;
14870a920b5bSAndy Whitcroft	my $signoff = 0;
14880a920b5bSAndy Whitcroft	my $is_patch = 0;
14890a920b5bSAndy Whitcroft
149015662b3eSJoe Perches	my $in_header_lines = 1;
149115662b3eSJoe Perches	my $in_commit_log = 0;		#Scanning lines before patch
149215662b3eSJoe Perches
1493fa64205dSPasi Savanainen	my $non_utf8_charset = 0;
1494fa64205dSPasi Savanainen
149513214adfSAndy Whitcroft	our @report = ();
14966c72ffaaSAndy Whitcroft	our $cnt_lines = 0;
14976c72ffaaSAndy Whitcroft	our $cnt_error = 0;
14986c72ffaaSAndy Whitcroft	our $cnt_warn = 0;
14996c72ffaaSAndy Whitcroft	our $cnt_chk = 0;
15006c72ffaaSAndy Whitcroft
15010a920b5bSAndy Whitcroft	# Trace the real file/line as we go.
15020a920b5bSAndy Whitcroft	my $realfile = '';
15030a920b5bSAndy Whitcroft	my $realline = 0;
15040a920b5bSAndy Whitcroft	my $realcnt = 0;
15050a920b5bSAndy Whitcroft	my $here = '';
15060a920b5bSAndy Whitcroft	my $in_comment = 0;
1507c2fdda0dSAndy Whitcroft	my $comment_edge = 0;
15080a920b5bSAndy Whitcroft	my $first_line = 0;
15091e855726SWolfram Sang	my $p1_prefix = '';
15100a920b5bSAndy Whitcroft
151113214adfSAndy Whitcroft	my $prev_values = 'E';
151213214adfSAndy Whitcroft
151313214adfSAndy Whitcroft	# suppression flags
1514773647a0SAndy Whitcroft	my %suppress_ifbraces;
1515170d3a22SAndy Whitcroft	my %suppress_whiletrailers;
15162b474a1aSAndy Whitcroft	my %suppress_export;
15173e469cdcSAndy Whitcroft	my $suppress_statement = 0;
1518653d4876SAndy Whitcroft
1519323c1260SJoe Perches
1520c2fdda0dSAndy Whitcroft	# Pre-scan the patch sanitizing the lines.
1521de7d4f0eSAndy Whitcroft	# Pre-scan the patch looking for any __setup documentation.
1522c2fdda0dSAndy Whitcroft	#
1523de7d4f0eSAndy Whitcroft	my @setup_docs = ();
1524de7d4f0eSAndy Whitcroft	my $setup_docs = 0;
1525773647a0SAndy Whitcroft
1526773647a0SAndy Whitcroft	sanitise_line_reset();
1527c2fdda0dSAndy Whitcroft	my $line;
1528c2fdda0dSAndy Whitcroft	foreach my $rawline (@rawlines) {
1529773647a0SAndy Whitcroft		$linenr++;
1530773647a0SAndy Whitcroft		$line = $rawline;
1531c2fdda0dSAndy Whitcroft
15323705ce5bSJoe Perches		push(@fixed, $rawline) if ($fix);
15333705ce5bSJoe Perches
1534773647a0SAndy Whitcroft		if ($rawline=~/^\+\+\+\s+(\S+)/) {
1535de7d4f0eSAndy Whitcroft			$setup_docs = 0;
1536de7d4f0eSAndy Whitcroft			if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
1537de7d4f0eSAndy Whitcroft				$setup_docs = 1;
1538de7d4f0eSAndy Whitcroft			}
1539773647a0SAndy Whitcroft			#next;
1540de7d4f0eSAndy Whitcroft		}
1541773647a0SAndy Whitcroft		if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1542773647a0SAndy Whitcroft			$realline=$1-1;
1543773647a0SAndy Whitcroft			if (defined $2) {
1544773647a0SAndy Whitcroft				$realcnt=$3+1;
1545773647a0SAndy Whitcroft			} else {
1546773647a0SAndy Whitcroft				$realcnt=1+1;
1547773647a0SAndy Whitcroft			}
1548c45dcabdSAndy Whitcroft			$in_comment = 0;
1549773647a0SAndy Whitcroft
1550773647a0SAndy Whitcroft			# Guestimate if this is a continuing comment.  Run
1551773647a0SAndy Whitcroft			# the context looking for a comment "edge".  If this
1552773647a0SAndy Whitcroft			# edge is a close comment then we must be in a comment
1553773647a0SAndy Whitcroft			# at context start.
1554773647a0SAndy Whitcroft			my $edge;
155501fa9147SAndy Whitcroft			my $cnt = $realcnt;
155601fa9147SAndy Whitcroft			for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
155701fa9147SAndy Whitcroft				next if (defined $rawlines[$ln - 1] &&
155801fa9147SAndy Whitcroft					 $rawlines[$ln - 1] =~ /^-/);
155901fa9147SAndy Whitcroft				$cnt--;
156001fa9147SAndy Whitcroft				#print "RAW<$rawlines[$ln - 1]>\n";
1561721c1cb6SAndy Whitcroft				last if (!defined $rawlines[$ln - 1]);
1562fae17daeSAndy Whitcroft				if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
1563fae17daeSAndy Whitcroft				    $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
1564fae17daeSAndy Whitcroft					($edge) = $1;
1565fae17daeSAndy Whitcroft					last;
1566fae17daeSAndy Whitcroft				}
1567773647a0SAndy Whitcroft			}
1568773647a0SAndy Whitcroft			if (defined $edge && $edge eq '*/') {
1569773647a0SAndy Whitcroft				$in_comment = 1;
1570773647a0SAndy Whitcroft			}
1571773647a0SAndy Whitcroft
1572773647a0SAndy Whitcroft			# Guestimate if this is a continuing comment.  If this
1573773647a0SAndy Whitcroft			# is the start of a diff block and this line starts
1574773647a0SAndy Whitcroft			# ' *' then it is very likely a comment.
1575773647a0SAndy Whitcroft			if (!defined $edge &&
157683242e0cSAndy Whitcroft			    $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
1577773647a0SAndy Whitcroft			{
1578773647a0SAndy Whitcroft				$in_comment = 1;
1579773647a0SAndy Whitcroft			}
1580773647a0SAndy Whitcroft
1581773647a0SAndy Whitcroft			##print "COMMENT:$in_comment edge<$edge> $rawline\n";
1582773647a0SAndy Whitcroft			sanitise_line_reset($in_comment);
1583773647a0SAndy Whitcroft
1584171ae1a4SAndy Whitcroft		} elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
1585773647a0SAndy Whitcroft			# Standardise the strings and chars within the input to
1586171ae1a4SAndy Whitcroft			# simplify matching -- only bother with positive lines.
1587773647a0SAndy Whitcroft			$line = sanitise_line($rawline);
1588773647a0SAndy Whitcroft		}
1589773647a0SAndy Whitcroft		push(@lines, $line);
1590773647a0SAndy Whitcroft
1591773647a0SAndy Whitcroft		if ($realcnt > 1) {
1592773647a0SAndy Whitcroft			$realcnt-- if ($line =~ /^(?:\+| |$)/);
1593773647a0SAndy Whitcroft		} else {
1594773647a0SAndy Whitcroft			$realcnt = 0;
1595773647a0SAndy Whitcroft		}
1596773647a0SAndy Whitcroft
1597773647a0SAndy Whitcroft		#print "==>$rawline\n";
1598773647a0SAndy Whitcroft		#print "-->$line\n";
1599de7d4f0eSAndy Whitcroft
1600de7d4f0eSAndy Whitcroft		if ($setup_docs && $line =~ /^\+/) {
1601de7d4f0eSAndy Whitcroft			push(@setup_docs, $line);
1602de7d4f0eSAndy Whitcroft		}
1603de7d4f0eSAndy Whitcroft	}
1604de7d4f0eSAndy Whitcroft
16056c72ffaaSAndy Whitcroft	$prefix = '';
16066c72ffaaSAndy Whitcroft
1607773647a0SAndy Whitcroft	$realcnt = 0;
1608773647a0SAndy Whitcroft	$linenr = 0;
16090a920b5bSAndy Whitcroft	foreach my $line (@lines) {
16100a920b5bSAndy Whitcroft		$linenr++;
16110a920b5bSAndy Whitcroft
1612c2fdda0dSAndy Whitcroft		my $rawline = $rawlines[$linenr - 1];
16136c72ffaaSAndy Whitcroft
16140a920b5bSAndy Whitcroft#extract the line range in the file after the patch is applied
16156c72ffaaSAndy Whitcroft		if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
16160a920b5bSAndy Whitcroft			$is_patch = 1;
16174a0df2efSAndy Whitcroft			$first_line = $linenr + 1;
16180a920b5bSAndy Whitcroft			$realline=$1-1;
16190a920b5bSAndy Whitcroft			if (defined $2) {
16200a920b5bSAndy Whitcroft				$realcnt=$3+1;
16210a920b5bSAndy Whitcroft			} else {
16220a920b5bSAndy Whitcroft				$realcnt=1+1;
16230a920b5bSAndy Whitcroft			}
1624c2fdda0dSAndy Whitcroft			annotate_reset();
162513214adfSAndy Whitcroft			$prev_values = 'E';
162613214adfSAndy Whitcroft
1627773647a0SAndy Whitcroft			%suppress_ifbraces = ();
1628170d3a22SAndy Whitcroft			%suppress_whiletrailers = ();
16292b474a1aSAndy Whitcroft			%suppress_export = ();
16303e469cdcSAndy Whitcroft			$suppress_statement = 0;
16310a920b5bSAndy Whitcroft			next;
16320a920b5bSAndy Whitcroft
16334a0df2efSAndy Whitcroft# track the line number as we move through the hunk, note that
16344a0df2efSAndy Whitcroft# new versions of GNU diff omit the leading space on completely
16354a0df2efSAndy Whitcroft# blank context lines so we need to count that too.
1636773647a0SAndy Whitcroft		} elsif ($line =~ /^( |\+|$)/) {
16370a920b5bSAndy Whitcroft			$realline++;
1638d8aaf121SAndy Whitcroft			$realcnt-- if ($realcnt != 0);
16390a920b5bSAndy Whitcroft
16404a0df2efSAndy Whitcroft			# Measure the line length and indent.
1641c2fdda0dSAndy Whitcroft			($length, $indent) = line_stats($rawline);
16420a920b5bSAndy Whitcroft
16430a920b5bSAndy Whitcroft			# Track the previous line.
16440a920b5bSAndy Whitcroft			($prevline, $stashline) = ($stashline, $line);
16450a920b5bSAndy Whitcroft			($previndent, $stashindent) = ($stashindent, $indent);
1646c2fdda0dSAndy Whitcroft			($prevrawline, $stashrawline) = ($stashrawline, $rawline);
1647c2fdda0dSAndy Whitcroft
1648773647a0SAndy Whitcroft			#warn "line<$line>\n";
16496c72ffaaSAndy Whitcroft
1650d8aaf121SAndy Whitcroft		} elsif ($realcnt == 1) {
1651d8aaf121SAndy Whitcroft			$realcnt--;
16520a920b5bSAndy Whitcroft		}
16530a920b5bSAndy Whitcroft
1654cc77cdcaSAndy Whitcroft		my $hunk_line = ($realcnt != 0);
1655cc77cdcaSAndy Whitcroft
16560a920b5bSAndy Whitcroft#make up the handle for any error we report on this line
1657773647a0SAndy Whitcroft		$prefix = "$filename:$realline: " if ($emacs && $file);
1658773647a0SAndy Whitcroft		$prefix = "$filename:$linenr: " if ($emacs && !$file);
1659773647a0SAndy Whitcroft
16606c72ffaaSAndy Whitcroft		$here = "#$linenr: " if (!$file);
16616c72ffaaSAndy Whitcroft		$here = "#$realline: " if ($file);
1662773647a0SAndy Whitcroft
1663773647a0SAndy Whitcroft		# extract the filename as it passes
16643bf9a009SRabin Vincent		if ($line =~ /^diff --git.*?(\S+)$/) {
16653bf9a009SRabin Vincent			$realfile = $1;
16663bf9a009SRabin Vincent			$realfile =~ s@^([^/]*)/@@;
1667270c49a0SJoe Perches			$in_commit_log = 0;
16683bf9a009SRabin Vincent		} elsif ($line =~ /^\+\+\+\s+(\S+)/) {
1669773647a0SAndy Whitcroft			$realfile = $1;
16701e855726SWolfram Sang			$realfile =~ s@^([^/]*)/@@;
1671270c49a0SJoe Perches			$in_commit_log = 0;
16721e855726SWolfram Sang
16731e855726SWolfram Sang			$p1_prefix = $1;
1674e2f7aa4bSAndy Whitcroft			if (!$file && $tree && $p1_prefix ne '' &&
1675e2f7aa4bSAndy Whitcroft			    -e "$root/$p1_prefix") {
1676000d1cc1SJoe Perches				WARN("PATCH_PREFIX",
1677000d1cc1SJoe Perches				     "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
16781e855726SWolfram Sang			}
1679773647a0SAndy Whitcroft
1680c1ab3326SAndy Whitcroft			if ($realfile =~ m@^include/asm/@) {
1681000d1cc1SJoe Perches				ERROR("MODIFIED_INCLUDE_ASM",
1682000d1cc1SJoe Perches				      "do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
1683773647a0SAndy Whitcroft			}
1684773647a0SAndy Whitcroft			next;
1685773647a0SAndy Whitcroft		}
1686773647a0SAndy Whitcroft
1687389834b6SRandy Dunlap		$here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
16880a920b5bSAndy Whitcroft
1689c2fdda0dSAndy Whitcroft		my $hereline = "$here\n$rawline\n";
1690c2fdda0dSAndy Whitcroft		my $herecurr = "$here\n$rawline\n";
1691c2fdda0dSAndy Whitcroft		my $hereprev = "$here\n$prevrawline\n$rawline\n";
16920a920b5bSAndy Whitcroft
16936c72ffaaSAndy Whitcroft		$cnt_lines++ if ($realcnt != 0);
16946c72ffaaSAndy Whitcroft
16953bf9a009SRabin Vincent# Check for incorrect file permissions
16963bf9a009SRabin Vincent		if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
16973bf9a009SRabin Vincent			my $permhere = $here . "FILE: $realfile\n";
169804db4d25SJoe Perches			if ($realfile !~ m@scripts/@ &&
169904db4d25SJoe Perches			    $realfile !~ /\.(py|pl|awk|sh)$/) {
1700000d1cc1SJoe Perches				ERROR("EXECUTE_PERMISSIONS",
1701000d1cc1SJoe Perches				      "do not set execute permissions for source files\n" . $permhere);
17023bf9a009SRabin Vincent			}
17033bf9a009SRabin Vincent		}
17043bf9a009SRabin Vincent
170520112475SJoe Perches# Check the patch for a signoff:
1706d8aaf121SAndy Whitcroft		if ($line =~ /^\s*signed-off-by:/i) {
17074a0df2efSAndy Whitcroft			$signoff++;
170815662b3eSJoe Perches			$in_commit_log = 0;
17090a920b5bSAndy Whitcroft		}
171020112475SJoe Perches
171120112475SJoe Perches# Check signature styles
1712270c49a0SJoe Perches		if (!$in_header_lines &&
1713ce0338dfSJoe Perches		    $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
171420112475SJoe Perches			my $space_before = $1;
171520112475SJoe Perches			my $sign_off = $2;
171620112475SJoe Perches			my $space_after = $3;
171720112475SJoe Perches			my $email = $4;
171820112475SJoe Perches			my $ucfirst_sign_off = ucfirst(lc($sign_off));
171920112475SJoe Perches
1720ce0338dfSJoe Perches			if ($sign_off !~ /$signature_tags/) {
1721ce0338dfSJoe Perches				WARN("BAD_SIGN_OFF",
1722ce0338dfSJoe Perches				     "Non-standard signature: $sign_off\n" . $herecurr);
1723ce0338dfSJoe Perches			}
172420112475SJoe Perches			if (defined $space_before && $space_before ne "") {
17253705ce5bSJoe Perches				if (WARN("BAD_SIGN_OFF",
17263705ce5bSJoe Perches					 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
17273705ce5bSJoe Perches				    $fix) {
17283705ce5bSJoe Perches					$fixed[$linenr - 1] =
17293705ce5bSJoe Perches					    "$ucfirst_sign_off $email";
17303705ce5bSJoe Perches				}
173120112475SJoe Perches			}
173220112475SJoe Perches			if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
17333705ce5bSJoe Perches				if (WARN("BAD_SIGN_OFF",
17343705ce5bSJoe Perches					 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
17353705ce5bSJoe Perches				    $fix) {
17363705ce5bSJoe Perches					$fixed[$linenr - 1] =
17373705ce5bSJoe Perches					    "$ucfirst_sign_off $email";
17383705ce5bSJoe Perches				}
17393705ce5bSJoe Perches
174020112475SJoe Perches			}
174120112475SJoe Perches			if (!defined $space_after || $space_after ne " ") {
17423705ce5bSJoe Perches				if (WARN("BAD_SIGN_OFF",
17433705ce5bSJoe Perches					 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
17443705ce5bSJoe Perches				    $fix) {
17453705ce5bSJoe Perches					$fixed[$linenr - 1] =
17463705ce5bSJoe Perches					    "$ucfirst_sign_off $email";
17473705ce5bSJoe Perches				}
174820112475SJoe Perches			}
174920112475SJoe Perches
175020112475SJoe Perches			my ($email_name, $email_address, $comment) = parse_email($email);
175120112475SJoe Perches			my $suggested_email = format_email(($email_name, $email_address));
175220112475SJoe Perches			if ($suggested_email eq "") {
1753000d1cc1SJoe Perches				ERROR("BAD_SIGN_OFF",
1754000d1cc1SJoe Perches				      "Unrecognized email address: '$email'\n" . $herecurr);
175520112475SJoe Perches			} else {
175620112475SJoe Perches				my $dequoted = $suggested_email;
175720112475SJoe Perches				$dequoted =~ s/^"//;
175820112475SJoe Perches				$dequoted =~ s/" </ </;
175920112475SJoe Perches				# Don't force email to have quotes
176020112475SJoe Perches				# Allow just an angle bracketed address
176120112475SJoe Perches				if ("$dequoted$comment" ne $email &&
176220112475SJoe Perches				    "<$email_address>$comment" ne $email &&
176320112475SJoe Perches				    "$suggested_email$comment" ne $email) {
1764000d1cc1SJoe Perches					WARN("BAD_SIGN_OFF",
1765000d1cc1SJoe Perches					     "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
176620112475SJoe Perches				}
17670a920b5bSAndy Whitcroft			}
17680a920b5bSAndy Whitcroft		}
17690a920b5bSAndy Whitcroft
177000df344fSAndy Whitcroft# Check for wrappage within a valid hunk of the file
17718905a67cSAndy Whitcroft		if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
1772000d1cc1SJoe Perches			ERROR("CORRUPTED_PATCH",
1773000d1cc1SJoe Perches			      "patch seems to be corrupt (line wrapped?)\n" .
17746c72ffaaSAndy Whitcroft				$herecurr) if (!$emitted_corrupt++);
1775de7d4f0eSAndy Whitcroft		}
1776de7d4f0eSAndy Whitcroft
17776ecd9674SAndy Whitcroft# Check for absolute kernel paths.
17786ecd9674SAndy Whitcroft		if ($tree) {
17796ecd9674SAndy Whitcroft			while ($line =~ m{(?:^|\s)(/\S*)}g) {
17806ecd9674SAndy Whitcroft				my $file = $1;
17816ecd9674SAndy Whitcroft
17826ecd9674SAndy Whitcroft				if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
17836ecd9674SAndy Whitcroft				    check_absolute_file($1, $herecurr)) {
17846ecd9674SAndy Whitcroft					#
17856ecd9674SAndy Whitcroft				} else {
17866ecd9674SAndy Whitcroft					check_absolute_file($file, $herecurr);
17876ecd9674SAndy Whitcroft				}
17886ecd9674SAndy Whitcroft			}
17896ecd9674SAndy Whitcroft		}
17906ecd9674SAndy Whitcroft
1791de7d4f0eSAndy Whitcroft# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
1792de7d4f0eSAndy Whitcroft		if (($realfile =~ /^$/ || $line =~ /^\+/) &&
1793171ae1a4SAndy Whitcroft		    $rawline !~ m/^$UTF8*$/) {
1794171ae1a4SAndy Whitcroft			my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
1795171ae1a4SAndy Whitcroft
1796171ae1a4SAndy Whitcroft			my $blank = copy_spacing($rawline);
1797171ae1a4SAndy Whitcroft			my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
1798171ae1a4SAndy Whitcroft			my $hereptr = "$hereline$ptr\n";
1799171ae1a4SAndy Whitcroft
180034d99219SJoe Perches			CHK("INVALID_UTF8",
1801000d1cc1SJoe Perches			    "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
180200df344fSAndy Whitcroft		}
18030a920b5bSAndy Whitcroft
180415662b3eSJoe Perches# Check if it's the start of a commit log
180515662b3eSJoe Perches# (not a header line and we haven't seen the patch filename)
180615662b3eSJoe Perches		if ($in_header_lines && $realfile =~ /^$/ &&
1807270c49a0SJoe Perches		    $rawline !~ /^(commit\b|from\b|[\w-]+:).+$/i) {
180815662b3eSJoe Perches			$in_header_lines = 0;
180915662b3eSJoe Perches			$in_commit_log = 1;
181015662b3eSJoe Perches		}
181115662b3eSJoe Perches
1812fa64205dSPasi Savanainen# Check if there is UTF-8 in a commit log when a mail header has explicitly
1813fa64205dSPasi Savanainen# declined it, i.e defined some charset where it is missing.
1814fa64205dSPasi Savanainen		if ($in_header_lines &&
1815fa64205dSPasi Savanainen		    $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
1816fa64205dSPasi Savanainen		    $1 !~ /utf-8/i) {
1817fa64205dSPasi Savanainen			$non_utf8_charset = 1;
1818fa64205dSPasi Savanainen		}
1819fa64205dSPasi Savanainen
1820fa64205dSPasi Savanainen		if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
182115662b3eSJoe Perches		    $rawline =~ /$NON_ASCII_UTF8/) {
1822fa64205dSPasi Savanainen			WARN("UTF8_BEFORE_PATCH",
182315662b3eSJoe Perches			    "8-bit UTF-8 used in possible commit log\n" . $herecurr);
182415662b3eSJoe Perches		}
182515662b3eSJoe Perches
182630670854SAndy Whitcroft# ignore non-hunk lines and lines being removed
182730670854SAndy Whitcroft		next if (!$hunk_line || $line =~ /^-/);
182800df344fSAndy Whitcroft
18290a920b5bSAndy Whitcroft#trailing whitespace
18309c0ca6f9SAndy Whitcroft		if ($line =~ /^\+.*\015/) {
1831c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1832000d1cc1SJoe Perches			ERROR("DOS_LINE_ENDINGS",
1833000d1cc1SJoe Perches			      "DOS line endings\n" . $herevet);
18349c0ca6f9SAndy Whitcroft
1835c2fdda0dSAndy Whitcroft		} elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
1836c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
18373705ce5bSJoe Perches			if (ERROR("TRAILING_WHITESPACE",
18383705ce5bSJoe Perches				  "trailing whitespace\n" . $herevet) &&
18393705ce5bSJoe Perches			    $fix) {
18403705ce5bSJoe Perches				$fixed[$linenr - 1] =~ s/^(\+.*?)\s+$/$1/;
18413705ce5bSJoe Perches			}
18423705ce5bSJoe Perches
1843d2c0a235SAndy Whitcroft			$rpt_cleaners = 1;
18440a920b5bSAndy Whitcroft		}
18455368df20SAndy Whitcroft
18463354957aSAndi Kleen# check for Kconfig help text having a real description
18479fe287d7SAndy Whitcroft# Only applies when adding the entry originally, after that we do not have
18489fe287d7SAndy Whitcroft# sufficient context to determine whether it is indeed long enough.
18493354957aSAndi Kleen		if ($realfile =~ /Kconfig/ &&
1850a1385803SAndy Whitcroft		    $line =~ /.\s*config\s+/) {
18513354957aSAndi Kleen			my $length = 0;
18529fe287d7SAndy Whitcroft			my $cnt = $realcnt;
18539fe287d7SAndy Whitcroft			my $ln = $linenr + 1;
18549fe287d7SAndy Whitcroft			my $f;
1855a1385803SAndy Whitcroft			my $is_start = 0;
18569fe287d7SAndy Whitcroft			my $is_end = 0;
1857a1385803SAndy Whitcroft			for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
18589fe287d7SAndy Whitcroft				$f = $lines[$ln - 1];
18599fe287d7SAndy Whitcroft				$cnt-- if ($lines[$ln - 1] !~ /^-/);
18609fe287d7SAndy Whitcroft				$is_end = $lines[$ln - 1] =~ /^\+/;
18619fe287d7SAndy Whitcroft
18629fe287d7SAndy Whitcroft				next if ($f =~ /^-/);
1863a1385803SAndy Whitcroft
1864a1385803SAndy Whitcroft				if ($lines[$ln - 1] =~ /.\s*(?:bool|tristate)\s*\"/) {
1865a1385803SAndy Whitcroft					$is_start = 1;
1866a1385803SAndy Whitcroft				} elsif ($lines[$ln - 1] =~ /.\s*(?:---)?help(?:---)?$/) {
1867a1385803SAndy Whitcroft					$length = -1;
1868a1385803SAndy Whitcroft				}
1869a1385803SAndy Whitcroft
18709fe287d7SAndy Whitcroft				$f =~ s/^.//;
18713354957aSAndi Kleen				$f =~ s/#.*//;
18723354957aSAndi Kleen				$f =~ s/^\s+//;
18733354957aSAndi Kleen				next if ($f =~ /^$/);
18749fe287d7SAndy Whitcroft				if ($f =~ /^\s*config\s/) {
18759fe287d7SAndy Whitcroft					$is_end = 1;
18769fe287d7SAndy Whitcroft					last;
18779fe287d7SAndy Whitcroft				}
18783354957aSAndi Kleen				$length++;
18793354957aSAndi Kleen			}
1880000d1cc1SJoe Perches			WARN("CONFIG_DESCRIPTION",
1881a1385803SAndy Whitcroft			     "please write a paragraph that describes the config symbol fully\n" . $herecurr) if ($is_start && $is_end && $length < 4);
1882a1385803SAndy Whitcroft			#print "is_start<$is_start> is_end<$is_end> length<$length>\n";
18833354957aSAndi Kleen		}
18843354957aSAndi Kleen
18851ba8dfd1SKees Cook# discourage the addition of CONFIG_EXPERIMENTAL in Kconfig.
18861ba8dfd1SKees Cook		if ($realfile =~ /Kconfig/ &&
18871ba8dfd1SKees Cook		    $line =~ /.\s*depends on\s+.*\bEXPERIMENTAL\b/) {
18881ba8dfd1SKees Cook			WARN("CONFIG_EXPERIMENTAL",
18891ba8dfd1SKees Cook			     "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
18901ba8dfd1SKees Cook		}
18911ba8dfd1SKees Cook
1892c68e5878SArnaud Lacombe		if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
1893c68e5878SArnaud Lacombe		    ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
1894c68e5878SArnaud Lacombe			my $flag = $1;
1895c68e5878SArnaud Lacombe			my $replacement = {
1896c68e5878SArnaud Lacombe				'EXTRA_AFLAGS' =>   'asflags-y',
1897c68e5878SArnaud Lacombe				'EXTRA_CFLAGS' =>   'ccflags-y',
1898c68e5878SArnaud Lacombe				'EXTRA_CPPFLAGS' => 'cppflags-y',
1899c68e5878SArnaud Lacombe				'EXTRA_LDFLAGS' =>  'ldflags-y',
1900c68e5878SArnaud Lacombe			};
1901c68e5878SArnaud Lacombe
1902c68e5878SArnaud Lacombe			WARN("DEPRECATED_VARIABLE",
1903c68e5878SArnaud Lacombe			     "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
1904c68e5878SArnaud Lacombe		}
1905c68e5878SArnaud Lacombe
19065368df20SAndy Whitcroft# check we are in a valid source file if not then ignore this hunk
19075368df20SAndy Whitcroft		next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
19085368df20SAndy Whitcroft
19096cd7f386SJoe Perches#line length limit
1910c45dcabdSAndy Whitcroft		if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ &&
1911f4c014c0SAndy Whitcroft		    $rawline !~ /^.\s*\*\s*\@$Ident\s/ &&
19120fccc622SJoe Perches		    !($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(KERN_\S+\s*|[^"]*))?"[X\t]*"\s*(?:|,|\)\s*;)\s*$/ ||
19138bbea968SJoe Perches		    $line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) &&
19146cd7f386SJoe Perches		    $length > $max_line_length)
1915c45dcabdSAndy Whitcroft		{
1916000d1cc1SJoe Perches			WARN("LONG_LINE",
19176cd7f386SJoe Perches			     "line over $max_line_length characters\n" . $herecurr);
19180a920b5bSAndy Whitcroft		}
19190a920b5bSAndy Whitcroft
1920ca56dc09SJosh Triplett# Check for user-visible strings broken across lines, which breaks the ability
1921ca56dc09SJosh Triplett# to grep for the string.  Limited to strings used as parameters (those
1922ca56dc09SJosh Triplett# following an open parenthesis), which almost completely eliminates false
1923ca56dc09SJosh Triplett# positives, as well as warning only once per parameter rather than once per
1924ca56dc09SJosh Triplett# line of the string.  Make an exception when the previous string ends in a
1925ca56dc09SJosh Triplett# newline (multiple lines in one string constant) or \n\t (common in inline
1926ca56dc09SJosh Triplett# assembly to indent the instruction on the following line).
1927ca56dc09SJosh Triplett		if ($line =~ /^\+\s*"/ &&
1928ca56dc09SJosh Triplett		    $prevline =~ /"\s*$/ &&
1929ca56dc09SJosh Triplett		    $prevline =~ /\(/ &&
1930ca56dc09SJosh Triplett		    $prevrawline !~ /\\n(?:\\t)*"\s*$/) {
1931ca56dc09SJosh Triplett			WARN("SPLIT_STRING",
1932ca56dc09SJosh Triplett			     "quoted string split across lines\n" . $hereprev);
1933ca56dc09SJosh Triplett		}
1934ca56dc09SJosh Triplett
19355e79d96eSJoe Perches# check for spaces before a quoted newline
19365e79d96eSJoe Perches		if ($rawline =~ /^.*\".*\s\\n/) {
19373705ce5bSJoe Perches			if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
19383705ce5bSJoe Perches				 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
19393705ce5bSJoe Perches			    $fix) {
19403705ce5bSJoe Perches				$fixed[$linenr - 1] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
19413705ce5bSJoe Perches			}
19423705ce5bSJoe Perches
19435e79d96eSJoe Perches		}
19445e79d96eSJoe Perches
19458905a67cSAndy Whitcroft# check for adding lines without a newline.
19468905a67cSAndy Whitcroft		if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
1947000d1cc1SJoe Perches			WARN("MISSING_EOF_NEWLINE",
1948000d1cc1SJoe Perches			     "adding a line without newline at end of file\n" . $herecurr);
19498905a67cSAndy Whitcroft		}
19508905a67cSAndy Whitcroft
195142e41c54SMike Frysinger# Blackfin: use hi/lo macros
195242e41c54SMike Frysinger		if ($realfile =~ m@arch/blackfin/.*\.S$@) {
195342e41c54SMike Frysinger			if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
195442e41c54SMike Frysinger				my $herevet = "$here\n" . cat_vet($line) . "\n";
1955000d1cc1SJoe Perches				ERROR("LO_MACRO",
1956000d1cc1SJoe Perches				      "use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
195742e41c54SMike Frysinger			}
195842e41c54SMike Frysinger			if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
195942e41c54SMike Frysinger				my $herevet = "$here\n" . cat_vet($line) . "\n";
1960000d1cc1SJoe Perches				ERROR("HI_MACRO",
1961000d1cc1SJoe Perches				      "use the HI() macro, not (... >> 16)\n" . $herevet);
196242e41c54SMike Frysinger			}
196342e41c54SMike Frysinger		}
196442e41c54SMike Frysinger
1965b9ea10d6SAndy Whitcroft# check we are in a valid source file C or perl if not then ignore this hunk
1966b9ea10d6SAndy Whitcroft		next if ($realfile !~ /\.(h|c|pl)$/);
19670a920b5bSAndy Whitcroft
19680a920b5bSAndy Whitcroft# at the beginning of a line any tabs must come first and anything
19690a920b5bSAndy Whitcroft# more than 8 must use tabs.
1970c2fdda0dSAndy Whitcroft		if ($rawline =~ /^\+\s* \t\s*\S/ ||
1971c2fdda0dSAndy Whitcroft		    $rawline =~ /^\+\s*        \s*/) {
1972c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1973d2c0a235SAndy Whitcroft			$rpt_cleaners = 1;
19743705ce5bSJoe Perches			if (ERROR("CODE_INDENT",
19753705ce5bSJoe Perches				  "code indent should use tabs where possible\n" . $herevet) &&
19763705ce5bSJoe Perches			    $fix) {
19773705ce5bSJoe Perches				$fixed[$linenr - 1] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
19783705ce5bSJoe Perches			}
19790a920b5bSAndy Whitcroft		}
19800a920b5bSAndy Whitcroft
198108e44365SAlberto Panizzo# check for space before tabs.
198208e44365SAlberto Panizzo		if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
198308e44365SAlberto Panizzo			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
19843705ce5bSJoe Perches			if (WARN("SPACE_BEFORE_TAB",
19853705ce5bSJoe Perches				"please, no space before tabs\n" . $herevet) &&
19863705ce5bSJoe Perches			    $fix) {
19873705ce5bSJoe Perches				$fixed[$linenr - 1] =~
19883705ce5bSJoe Perches				    s/(^\+.*) +\t/$1\t/;
19893705ce5bSJoe Perches			}
199008e44365SAlberto Panizzo		}
199108e44365SAlberto Panizzo
1992d1fe9c09SJoe Perches# check for && or || at the start of a line
1993d1fe9c09SJoe Perches		if ($rawline =~ /^\+\s*(&&|\|\|)/) {
1994d1fe9c09SJoe Perches			CHK("LOGICAL_CONTINUATIONS",
1995d1fe9c09SJoe Perches			    "Logical continuations should be on the previous line\n" . $hereprev);
1996d1fe9c09SJoe Perches		}
1997d1fe9c09SJoe Perches
1998d1fe9c09SJoe Perches# check multi-line statement indentation matches previous line
1999d1fe9c09SJoe Perches		if ($^V && $^V ge 5.10.0 &&
2000d1fe9c09SJoe Perches		    $prevline =~ /^\+(\t*)(if \(|$Ident\().*(\&\&|\|\||,)\s*$/) {
2001d1fe9c09SJoe Perches			$prevline =~ /^\+(\t*)(.*)$/;
2002d1fe9c09SJoe Perches			my $oldindent = $1;
2003d1fe9c09SJoe Perches			my $rest = $2;
2004d1fe9c09SJoe Perches
2005d1fe9c09SJoe Perches			my $pos = pos_last_openparen($rest);
2006d1fe9c09SJoe Perches			if ($pos >= 0) {
2007b34a26f3SJoe Perches				$line =~ /^(\+| )([ \t]*)/;
2008b34a26f3SJoe Perches				my $newindent = $2;
2009d1fe9c09SJoe Perches
2010d1fe9c09SJoe Perches				my $goodtabindent = $oldindent .
2011d1fe9c09SJoe Perches					"\t" x ($pos / 8) .
2012d1fe9c09SJoe Perches					" "  x ($pos % 8);
2013d1fe9c09SJoe Perches				my $goodspaceindent = $oldindent . " "  x $pos;
2014d1fe9c09SJoe Perches
2015d1fe9c09SJoe Perches				if ($newindent ne $goodtabindent &&
2016d1fe9c09SJoe Perches				    $newindent ne $goodspaceindent) {
20173705ce5bSJoe Perches
20183705ce5bSJoe Perches					if (CHK("PARENTHESIS_ALIGNMENT",
20193705ce5bSJoe Perches						"Alignment should match open parenthesis\n" . $hereprev) &&
20203705ce5bSJoe Perches					    $fix && $line =~ /^\+/) {
20213705ce5bSJoe Perches						$fixed[$linenr - 1] =~
20223705ce5bSJoe Perches						    s/^\+[ \t]*/\+$goodtabindent/;
20233705ce5bSJoe Perches					}
2024d1fe9c09SJoe Perches				}
2025d1fe9c09SJoe Perches			}
2026d1fe9c09SJoe Perches		}
2027d1fe9c09SJoe Perches
202823f780c9SJoe Perches		if ($line =~ /^\+.*\*[ \t]*\)[ \t]+(?!$Assignment|$Arithmetic)/) {
20293705ce5bSJoe Perches			if (CHK("SPACING",
20303705ce5bSJoe Perches				"No space is necessary after a cast\n" . $hereprev) &&
20313705ce5bSJoe Perches			    $fix) {
20323705ce5bSJoe Perches				$fixed[$linenr - 1] =~
20333705ce5bSJoe Perches				    s/^(\+.*\*[ \t]*\))[ \t]+/$1/;
20343705ce5bSJoe Perches			}
2035aad4f614SJoe Perches		}
2036aad4f614SJoe Perches
203705880600SJoe Perches		if ($realfile =~ m@^(drivers/net/|net/)@ &&
2038fdb4bcd6SJoe Perches		    $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
2039fdb4bcd6SJoe Perches		    $rawline =~ /^\+[ \t]*\*/) {
204005880600SJoe Perches			WARN("NETWORKING_BLOCK_COMMENT_STYLE",
204105880600SJoe Perches			     "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
204205880600SJoe Perches		}
204305880600SJoe Perches
204405880600SJoe Perches		if ($realfile =~ m@^(drivers/net/|net/)@ &&
2045a605e32eSJoe Perches		    $prevrawline =~ /^\+[ \t]*\/\*/ &&		#starting /*
2046a605e32eSJoe Perches		    $prevrawline !~ /\*\/[ \t]*$/ &&		#no trailing */
2047a605e32eSJoe Perches		    $rawline !~ /^\+[ \t]*\*/) {		#no leading *
2048a605e32eSJoe Perches			WARN("NETWORKING_BLOCK_COMMENT_STYLE",
2049a605e32eSJoe Perches			     "networking block comments start with * on subsequent lines\n" . $hereprev);
2050a605e32eSJoe Perches		}
2051a605e32eSJoe Perches
2052a605e32eSJoe Perches		if ($realfile =~ m@^(drivers/net/|net/)@ &&
2053c24f9f19SJoe Perches		    $rawline !~ m@^\+[ \t]*\*/[ \t]*$@ &&	#trailing */
2054c24f9f19SJoe Perches		    $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ &&	#inline /*...*/
2055c24f9f19SJoe Perches		    $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ &&	#trailing **/
2056c24f9f19SJoe Perches		    $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) {	#non blank */
205705880600SJoe Perches			WARN("NETWORKING_BLOCK_COMMENT_STYLE",
205805880600SJoe Perches			     "networking block comments put the trailing */ on a separate line\n" . $herecurr);
205905880600SJoe Perches		}
206005880600SJoe Perches
20615f7ddae6SRaffaele Recalcati# check for spaces at the beginning of a line.
20626b4c5bebSAndy Whitcroft# Exceptions:
20636b4c5bebSAndy Whitcroft#  1) within comments
20646b4c5bebSAndy Whitcroft#  2) indented preprocessor commands
20656b4c5bebSAndy Whitcroft#  3) hanging labels
20663705ce5bSJoe Perches		if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/)  {
20675f7ddae6SRaffaele Recalcati			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
20683705ce5bSJoe Perches			if (WARN("LEADING_SPACE",
20693705ce5bSJoe Perches				 "please, no spaces at the start of a line\n" . $herevet) &&
20703705ce5bSJoe Perches			    $fix) {
20713705ce5bSJoe Perches				$fixed[$linenr - 1] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
20723705ce5bSJoe Perches			}
20735f7ddae6SRaffaele Recalcati		}
20745f7ddae6SRaffaele Recalcati
2075b9ea10d6SAndy Whitcroft# check we are in a valid C source file if not then ignore this hunk
2076b9ea10d6SAndy Whitcroft		next if ($realfile !~ /\.(h|c)$/);
2077b9ea10d6SAndy Whitcroft
20781ba8dfd1SKees Cook# discourage the addition of CONFIG_EXPERIMENTAL in #if(def).
20791ba8dfd1SKees Cook		if ($line =~ /^\+\s*\#\s*if.*\bCONFIG_EXPERIMENTAL\b/) {
20801ba8dfd1SKees Cook			WARN("CONFIG_EXPERIMENTAL",
20811ba8dfd1SKees Cook			     "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
20821ba8dfd1SKees Cook		}
20831ba8dfd1SKees Cook
2084c2fdda0dSAndy Whitcroft# check for RCS/CVS revision markers
2085cf655043SAndy Whitcroft		if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
2086000d1cc1SJoe Perches			WARN("CVS_KEYWORD",
2087000d1cc1SJoe Perches			     "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
2088c2fdda0dSAndy Whitcroft		}
208922f2a2efSAndy Whitcroft
209042e41c54SMike Frysinger# Blackfin: don't use __builtin_bfin_[cs]sync
209142e41c54SMike Frysinger		if ($line =~ /__builtin_bfin_csync/) {
209242e41c54SMike Frysinger			my $herevet = "$here\n" . cat_vet($line) . "\n";
2093000d1cc1SJoe Perches			ERROR("CSYNC",
2094000d1cc1SJoe Perches			      "use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
209542e41c54SMike Frysinger		}
209642e41c54SMike Frysinger		if ($line =~ /__builtin_bfin_ssync/) {
209742e41c54SMike Frysinger			my $herevet = "$here\n" . cat_vet($line) . "\n";
2098000d1cc1SJoe Perches			ERROR("SSYNC",
2099000d1cc1SJoe Perches			      "use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
210042e41c54SMike Frysinger		}
210142e41c54SMike Frysinger
210256e77d70SJoe Perches# check for old HOTPLUG __dev<foo> section markings
210356e77d70SJoe Perches		if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
210456e77d70SJoe Perches			WARN("HOTPLUG_SECTION",
210556e77d70SJoe Perches			     "Using $1 is unnecessary\n" . $herecurr);
210656e77d70SJoe Perches		}
210756e77d70SJoe Perches
21089c0ca6f9SAndy Whitcroft# Check for potential 'bare' types
21092b474a1aSAndy Whitcroft		my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
21102b474a1aSAndy Whitcroft		    $realline_next);
21113e469cdcSAndy Whitcroft#print "LINE<$line>\n";
21123e469cdcSAndy Whitcroft		if ($linenr >= $suppress_statement &&
21133e469cdcSAndy Whitcroft		    $realcnt && $line =~ /.\s*\S/) {
2114170d3a22SAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
2115f5fe35ddSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0);
2116171ae1a4SAndy Whitcroft			$stat =~ s/\n./\n /g;
2117171ae1a4SAndy Whitcroft			$cond =~ s/\n./\n /g;
2118171ae1a4SAndy Whitcroft
21193e469cdcSAndy Whitcroft#print "linenr<$linenr> <$stat>\n";
21203e469cdcSAndy Whitcroft			# If this statement has no statement boundaries within
21213e469cdcSAndy Whitcroft			# it there is no point in retrying a statement scan
21223e469cdcSAndy Whitcroft			# until we hit end of it.
21233e469cdcSAndy Whitcroft			my $frag = $stat; $frag =~ s/;+\s*$//;
21243e469cdcSAndy Whitcroft			if ($frag !~ /(?:{|;)/) {
21253e469cdcSAndy Whitcroft#print "skip<$line_nr_next>\n";
21263e469cdcSAndy Whitcroft				$suppress_statement = $line_nr_next;
21273e469cdcSAndy Whitcroft			}
2128f74bd194SAndy Whitcroft
21292b474a1aSAndy Whitcroft			# Find the real next line.
21302b474a1aSAndy Whitcroft			$realline_next = $line_nr_next;
21312b474a1aSAndy Whitcroft			if (defined $realline_next &&
21322b474a1aSAndy Whitcroft			    (!defined $lines[$realline_next - 1] ||
21332b474a1aSAndy Whitcroft			     substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
21342b474a1aSAndy Whitcroft				$realline_next++;
21352b474a1aSAndy Whitcroft			}
21362b474a1aSAndy Whitcroft
2137171ae1a4SAndy Whitcroft			my $s = $stat;
2138171ae1a4SAndy Whitcroft			$s =~ s/{.*$//s;
2139cf655043SAndy Whitcroft
2140c2fdda0dSAndy Whitcroft			# Ignore goto labels.
2141171ae1a4SAndy Whitcroft			if ($s =~ /$Ident:\*$/s) {
2142c2fdda0dSAndy Whitcroft
2143c2fdda0dSAndy Whitcroft			# Ignore functions being called
2144171ae1a4SAndy Whitcroft			} elsif ($s =~ /^.\s*$Ident\s*\(/s) {
2145c2fdda0dSAndy Whitcroft
2146463f2864SAndy Whitcroft			} elsif ($s =~ /^.\s*else\b/s) {
2147463f2864SAndy Whitcroft
2148c45dcabdSAndy Whitcroft			# declarations always start with types
2149d2506586SAndy 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) {
2150c45dcabdSAndy Whitcroft				my $type = $1;
2151c45dcabdSAndy Whitcroft				$type =~ s/\s+/ /g;
2152c45dcabdSAndy Whitcroft				possible($type, "A:" . $s);
2153c45dcabdSAndy Whitcroft
21546c72ffaaSAndy Whitcroft			# definitions in global scope can only start with types
2155a6a84062SAndy Whitcroft			} elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
2156c45dcabdSAndy Whitcroft				possible($1, "B:" . $s);
2157c2fdda0dSAndy Whitcroft			}
21588905a67cSAndy Whitcroft
21596c72ffaaSAndy Whitcroft			# any (foo ... *) is a pointer cast, and foo is a type
216065863862SAndy Whitcroft			while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
2161c45dcabdSAndy Whitcroft				possible($1, "C:" . $s);
21629c0ca6f9SAndy Whitcroft			}
21638905a67cSAndy Whitcroft
21648905a67cSAndy Whitcroft			# Check for any sort of function declaration.
21658905a67cSAndy Whitcroft			# int foo(something bar, other baz);
21668905a67cSAndy Whitcroft			# void (*store_gdt)(x86_descr_ptr *);
2167171ae1a4SAndy Whitcroft			if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
21688905a67cSAndy Whitcroft				my ($name_len) = length($1);
21698905a67cSAndy Whitcroft
2170cf655043SAndy Whitcroft				my $ctx = $s;
2171773647a0SAndy Whitcroft				substr($ctx, 0, $name_len + 1, '');
21728905a67cSAndy Whitcroft				$ctx =~ s/\)[^\)]*$//;
2173cf655043SAndy Whitcroft
21748905a67cSAndy Whitcroft				for my $arg (split(/\s*,\s*/, $ctx)) {
2175c45dcabdSAndy Whitcroft					if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
21768905a67cSAndy Whitcroft
2177c45dcabdSAndy Whitcroft						possible($1, "D:" . $s);
21788905a67cSAndy Whitcroft					}
21798905a67cSAndy Whitcroft				}
21808905a67cSAndy Whitcroft			}
21818905a67cSAndy Whitcroft
21829c0ca6f9SAndy Whitcroft		}
21839c0ca6f9SAndy Whitcroft
218400df344fSAndy Whitcroft#
218500df344fSAndy Whitcroft# Checks which may be anchored in the context.
218600df344fSAndy Whitcroft#
218700df344fSAndy Whitcroft
218800df344fSAndy Whitcroft# Check for switch () and associated case and default
218900df344fSAndy Whitcroft# statements should be at the same indent.
219000df344fSAndy Whitcroft		if ($line=~/\bswitch\s*\(.*\)/) {
219100df344fSAndy Whitcroft			my $err = '';
219200df344fSAndy Whitcroft			my $sep = '';
219300df344fSAndy Whitcroft			my @ctx = ctx_block_outer($linenr, $realcnt);
219400df344fSAndy Whitcroft			shift(@ctx);
219500df344fSAndy Whitcroft			for my $ctx (@ctx) {
219600df344fSAndy Whitcroft				my ($clen, $cindent) = line_stats($ctx);
219700df344fSAndy Whitcroft				if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
219800df344fSAndy Whitcroft							$indent != $cindent) {
219900df344fSAndy Whitcroft					$err .= "$sep$ctx\n";
220000df344fSAndy Whitcroft					$sep = '';
220100df344fSAndy Whitcroft				} else {
220200df344fSAndy Whitcroft					$sep = "[...]\n";
220300df344fSAndy Whitcroft				}
220400df344fSAndy Whitcroft			}
220500df344fSAndy Whitcroft			if ($err ne '') {
2206000d1cc1SJoe Perches				ERROR("SWITCH_CASE_INDENT_LEVEL",
2207000d1cc1SJoe Perches				      "switch and case should be at the same indent\n$hereline$err");
2208de7d4f0eSAndy Whitcroft			}
2209de7d4f0eSAndy Whitcroft		}
2210de7d4f0eSAndy Whitcroft
2211de7d4f0eSAndy Whitcroft# if/while/etc brace do not go on next line, unless defining a do while loop,
2212de7d4f0eSAndy Whitcroft# or if that brace on the next line is for something else
2213c45dcabdSAndy Whitcroft		if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
2214773647a0SAndy Whitcroft			my $pre_ctx = "$1$2";
2215773647a0SAndy Whitcroft
22169c0ca6f9SAndy Whitcroft			my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
22178eef05ddSJoe Perches
22188eef05ddSJoe Perches			if ($line =~ /^\+\t{6,}/) {
22198eef05ddSJoe Perches				WARN("DEEP_INDENTATION",
22208eef05ddSJoe Perches				     "Too many leading tabs - consider code refactoring\n" . $herecurr);
22218eef05ddSJoe Perches			}
22228eef05ddSJoe Perches
2223de7d4f0eSAndy Whitcroft			my $ctx_cnt = $realcnt - $#ctx - 1;
2224de7d4f0eSAndy Whitcroft			my $ctx = join("\n", @ctx);
2225de7d4f0eSAndy Whitcroft
2226548596d5SAndy Whitcroft			my $ctx_ln = $linenr;
2227548596d5SAndy Whitcroft			my $ctx_skip = $realcnt;
2228de7d4f0eSAndy Whitcroft
2229548596d5SAndy Whitcroft			while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
2230548596d5SAndy Whitcroft					defined $lines[$ctx_ln - 1] &&
2231548596d5SAndy Whitcroft					$lines[$ctx_ln - 1] =~ /^-/)) {
2232548596d5SAndy Whitcroft				##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
2233548596d5SAndy Whitcroft				$ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
2234773647a0SAndy Whitcroft				$ctx_ln++;
2235773647a0SAndy Whitcroft			}
2236548596d5SAndy Whitcroft
223753210168SAndy Whitcroft			#print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
223853210168SAndy Whitcroft			#print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
2239773647a0SAndy Whitcroft
2240773647a0SAndy Whitcroft			if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln -1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
2241000d1cc1SJoe Perches				ERROR("OPEN_BRACE",
2242000d1cc1SJoe Perches				      "that open brace { should be on the previous line\n" .
224301464f30SAndy Whitcroft					"$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
224400df344fSAndy Whitcroft			}
2245773647a0SAndy Whitcroft			if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
2246773647a0SAndy Whitcroft			    $ctx =~ /\)\s*\;\s*$/ &&
2247773647a0SAndy Whitcroft			    defined $lines[$ctx_ln - 1])
2248773647a0SAndy Whitcroft			{
22499c0ca6f9SAndy Whitcroft				my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
22509c0ca6f9SAndy Whitcroft				if ($nindent > $indent) {
2251000d1cc1SJoe Perches					WARN("TRAILING_SEMICOLON",
2252000d1cc1SJoe Perches					     "trailing semicolon indicates no statements, indent implies otherwise\n" .
225301464f30SAndy Whitcroft						"$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
22549c0ca6f9SAndy Whitcroft				}
22559c0ca6f9SAndy Whitcroft			}
225600df344fSAndy Whitcroft		}
225700df344fSAndy Whitcroft
22584d001e4dSAndy Whitcroft# Check relative indent for conditionals and blocks.
22594d001e4dSAndy Whitcroft		if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
22603e469cdcSAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
22613e469cdcSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0)
22623e469cdcSAndy Whitcroft					if (!defined $stat);
22634d001e4dSAndy Whitcroft			my ($s, $c) = ($stat, $cond);
22644d001e4dSAndy Whitcroft
22654d001e4dSAndy Whitcroft			substr($s, 0, length($c), '');
22664d001e4dSAndy Whitcroft
22674d001e4dSAndy Whitcroft			# Make sure we remove the line prefixes as we have
22684d001e4dSAndy Whitcroft			# none on the first line, and are going to readd them
22694d001e4dSAndy Whitcroft			# where necessary.
22704d001e4dSAndy Whitcroft			$s =~ s/\n./\n/gs;
22714d001e4dSAndy Whitcroft
22724d001e4dSAndy Whitcroft			# Find out how long the conditional actually is.
22736f779c18SAndy Whitcroft			my @newlines = ($c =~ /\n/gs);
22746f779c18SAndy Whitcroft			my $cond_lines = 1 + $#newlines;
22754d001e4dSAndy Whitcroft
22764d001e4dSAndy Whitcroft			# We want to check the first line inside the block
22774d001e4dSAndy Whitcroft			# starting at the end of the conditional, so remove:
22784d001e4dSAndy Whitcroft			#  1) any blank line termination
22794d001e4dSAndy Whitcroft			#  2) any opening brace { on end of the line
22804d001e4dSAndy Whitcroft			#  3) any do (...) {
22814d001e4dSAndy Whitcroft			my $continuation = 0;
22824d001e4dSAndy Whitcroft			my $check = 0;
22834d001e4dSAndy Whitcroft			$s =~ s/^.*\bdo\b//;
22844d001e4dSAndy Whitcroft			$s =~ s/^\s*{//;
22854d001e4dSAndy Whitcroft			if ($s =~ s/^\s*\\//) {
22864d001e4dSAndy Whitcroft				$continuation = 1;
22874d001e4dSAndy Whitcroft			}
22889bd49efeSAndy Whitcroft			if ($s =~ s/^\s*?\n//) {
22894d001e4dSAndy Whitcroft				$check = 1;
22904d001e4dSAndy Whitcroft				$cond_lines++;
22914d001e4dSAndy Whitcroft			}
22924d001e4dSAndy Whitcroft
22934d001e4dSAndy Whitcroft			# Also ignore a loop construct at the end of a
22944d001e4dSAndy Whitcroft			# preprocessor statement.
22954d001e4dSAndy Whitcroft			if (($prevline =~ /^.\s*#\s*define\s/ ||
22964d001e4dSAndy Whitcroft			    $prevline =~ /\\\s*$/) && $continuation == 0) {
22974d001e4dSAndy Whitcroft				$check = 0;
22984d001e4dSAndy Whitcroft			}
22994d001e4dSAndy Whitcroft
23009bd49efeSAndy Whitcroft			my $cond_ptr = -1;
2301740504c6SAndy Whitcroft			$continuation = 0;
23029bd49efeSAndy Whitcroft			while ($cond_ptr != $cond_lines) {
23039bd49efeSAndy Whitcroft				$cond_ptr = $cond_lines;
23044d001e4dSAndy Whitcroft
2305f16fa28fSAndy Whitcroft				# If we see an #else/#elif then the code
2306f16fa28fSAndy Whitcroft				# is not linear.
2307f16fa28fSAndy Whitcroft				if ($s =~ /^\s*\#\s*(?:else|elif)/) {
2308f16fa28fSAndy Whitcroft					$check = 0;
2309f16fa28fSAndy Whitcroft				}
2310f16fa28fSAndy Whitcroft
23119bd49efeSAndy Whitcroft				# Ignore:
23129bd49efeSAndy Whitcroft				#  1) blank lines, they should be at 0,
23139bd49efeSAndy Whitcroft				#  2) preprocessor lines, and
23149bd49efeSAndy Whitcroft				#  3) labels.
2315740504c6SAndy Whitcroft				if ($continuation ||
2316740504c6SAndy Whitcroft				    $s =~ /^\s*?\n/ ||
23179bd49efeSAndy Whitcroft				    $s =~ /^\s*#\s*?/ ||
23189bd49efeSAndy Whitcroft				    $s =~ /^\s*$Ident\s*:/) {
2319740504c6SAndy Whitcroft					$continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
232030dad6ebSAndy Whitcroft					if ($s =~ s/^.*?\n//) {
23219bd49efeSAndy Whitcroft						$cond_lines++;
23229bd49efeSAndy Whitcroft					}
23234d001e4dSAndy Whitcroft				}
232430dad6ebSAndy Whitcroft			}
23254d001e4dSAndy Whitcroft
23264d001e4dSAndy Whitcroft			my (undef, $sindent) = line_stats("+" . $s);
23274d001e4dSAndy Whitcroft			my $stat_real = raw_line($linenr, $cond_lines);
23284d001e4dSAndy Whitcroft
23294d001e4dSAndy Whitcroft			# Check if either of these lines are modified, else
23304d001e4dSAndy Whitcroft			# this is not this patch's fault.
23314d001e4dSAndy Whitcroft			if (!defined($stat_real) ||
23324d001e4dSAndy Whitcroft			    $stat !~ /^\+/ && $stat_real !~ /^\+/) {
23334d001e4dSAndy Whitcroft				$check = 0;
23344d001e4dSAndy Whitcroft			}
23354d001e4dSAndy Whitcroft			if (defined($stat_real) && $cond_lines > 1) {
23364d001e4dSAndy Whitcroft				$stat_real = "[...]\n$stat_real";
23374d001e4dSAndy Whitcroft			}
23384d001e4dSAndy Whitcroft
23399bd49efeSAndy 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";
23404d001e4dSAndy Whitcroft
23414d001e4dSAndy Whitcroft			if ($check && (($sindent % 8) != 0 ||
23424d001e4dSAndy Whitcroft			    ($sindent <= $indent && $s ne ''))) {
2343000d1cc1SJoe Perches				WARN("SUSPECT_CODE_INDENT",
2344000d1cc1SJoe Perches				     "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
23454d001e4dSAndy Whitcroft			}
23464d001e4dSAndy Whitcroft		}
23474d001e4dSAndy Whitcroft
23486c72ffaaSAndy Whitcroft		# Track the 'values' across context and added lines.
23496c72ffaaSAndy Whitcroft		my $opline = $line; $opline =~ s/^./ /;
23501f65f947SAndy Whitcroft		my ($curr_values, $curr_vars) =
23511f65f947SAndy Whitcroft				annotate_values($opline . "\n", $prev_values);
23526c72ffaaSAndy Whitcroft		$curr_values = $prev_values . $curr_values;
2353c2fdda0dSAndy Whitcroft		if ($dbg_values) {
2354c2fdda0dSAndy Whitcroft			my $outline = $opline; $outline =~ s/\t/ /g;
2355cf655043SAndy Whitcroft			print "$linenr > .$outline\n";
2356cf655043SAndy Whitcroft			print "$linenr > $curr_values\n";
23571f65f947SAndy Whitcroft			print "$linenr >  $curr_vars\n";
2358c2fdda0dSAndy Whitcroft		}
23596c72ffaaSAndy Whitcroft		$prev_values = substr($curr_values, -1);
23606c72ffaaSAndy Whitcroft
236100df344fSAndy Whitcroft#ignore lines not being added
23623705ce5bSJoe Perches		next if ($line =~ /^[^\+]/);
236300df344fSAndy Whitcroft
2364653d4876SAndy Whitcroft# TEST: allow direct testing of the type matcher.
23657429c690SAndy Whitcroft		if ($dbg_type) {
23667429c690SAndy Whitcroft			if ($line =~ /^.\s*$Declare\s*$/) {
2367000d1cc1SJoe Perches				ERROR("TEST_TYPE",
2368000d1cc1SJoe Perches				      "TEST: is type\n" . $herecurr);
23697429c690SAndy Whitcroft			} elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
2370000d1cc1SJoe Perches				ERROR("TEST_NOT_TYPE",
2371000d1cc1SJoe Perches				      "TEST: is not type ($1 is)\n". $herecurr);
23727429c690SAndy Whitcroft			}
2373653d4876SAndy Whitcroft			next;
2374653d4876SAndy Whitcroft		}
2375a1ef277eSAndy Whitcroft# TEST: allow direct testing of the attribute matcher.
2376a1ef277eSAndy Whitcroft		if ($dbg_attr) {
23779360b0e5SAndy Whitcroft			if ($line =~ /^.\s*$Modifier\s*$/) {
2378000d1cc1SJoe Perches				ERROR("TEST_ATTR",
2379000d1cc1SJoe Perches				      "TEST: is attr\n" . $herecurr);
23809360b0e5SAndy Whitcroft			} elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
2381000d1cc1SJoe Perches				ERROR("TEST_NOT_ATTR",
2382000d1cc1SJoe Perches				      "TEST: is not attr ($1 is)\n". $herecurr);
2383a1ef277eSAndy Whitcroft			}
2384a1ef277eSAndy Whitcroft			next;
2385a1ef277eSAndy Whitcroft		}
2386653d4876SAndy Whitcroft
2387f0a594c1SAndy Whitcroft# check for initialisation to aggregates open brace on the next line
238899423c20SAndy Whitcroft		if ($line =~ /^.\s*{/ &&
238999423c20SAndy Whitcroft		    $prevline =~ /(?:^|[^=])=\s*$/) {
2390000d1cc1SJoe Perches			ERROR("OPEN_BRACE",
2391000d1cc1SJoe Perches			      "that open brace { should be on the previous line\n" . $hereprev);
2392f0a594c1SAndy Whitcroft		}
2393f0a594c1SAndy Whitcroft
239400df344fSAndy Whitcroft#
239500df344fSAndy Whitcroft# Checks which are anchored on the added line.
239600df344fSAndy Whitcroft#
239700df344fSAndy Whitcroft
2398653d4876SAndy Whitcroft# check for malformed paths in #include statements (uses RAW line)
2399c45dcabdSAndy Whitcroft		if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
2400653d4876SAndy Whitcroft			my $path = $1;
2401653d4876SAndy Whitcroft			if ($path =~ m{//}) {
2402000d1cc1SJoe Perches				ERROR("MALFORMED_INCLUDE",
2403495e9d84SJoe Perches				      "malformed #include filename\n" . $herecurr);
2404495e9d84SJoe Perches			}
2405495e9d84SJoe Perches			if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
2406495e9d84SJoe Perches				ERROR("UAPI_INCLUDE",
2407495e9d84SJoe Perches				      "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
2408653d4876SAndy Whitcroft			}
2409653d4876SAndy Whitcroft		}
2410653d4876SAndy Whitcroft
241100df344fSAndy Whitcroft# no C99 // comments
241200df344fSAndy Whitcroft		if ($line =~ m{//}) {
24133705ce5bSJoe Perches			if (ERROR("C99_COMMENTS",
24143705ce5bSJoe Perches				  "do not use C99 // comments\n" . $herecurr) &&
24153705ce5bSJoe Perches			    $fix) {
24163705ce5bSJoe Perches				my $line = $fixed[$linenr - 1];
24173705ce5bSJoe Perches				if ($line =~ /\/\/(.*)$/) {
24183705ce5bSJoe Perches					my $comment = trim($1);
24193705ce5bSJoe Perches					$fixed[$linenr - 1] =~ s@\/\/(.*)$@/\* $comment \*/@;
24203705ce5bSJoe Perches				}
24213705ce5bSJoe Perches			}
242200df344fSAndy Whitcroft		}
242300df344fSAndy Whitcroft		# Remove C99 comments.
24240a920b5bSAndy Whitcroft		$line =~ s@//.*@@;
24256c72ffaaSAndy Whitcroft		$opline =~ s@//.*@@;
24260a920b5bSAndy Whitcroft
24272b474a1aSAndy Whitcroft# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
24282b474a1aSAndy Whitcroft# the whole statement.
24292b474a1aSAndy Whitcroft#print "APW <$lines[$realline_next - 1]>\n";
24302b474a1aSAndy Whitcroft		if (defined $realline_next &&
24312b474a1aSAndy Whitcroft		    exists $lines[$realline_next - 1] &&
24322b474a1aSAndy Whitcroft		    !defined $suppress_export{$realline_next} &&
24332b474a1aSAndy Whitcroft		    ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
24342b474a1aSAndy Whitcroft		     $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
24353cbf62dfSAndy Whitcroft			# Handle definitions which produce identifiers with
24363cbf62dfSAndy Whitcroft			# a prefix:
24373cbf62dfSAndy Whitcroft			#   XXX(foo);
24383cbf62dfSAndy Whitcroft			#   EXPORT_SYMBOL(something_foo);
2439653d4876SAndy Whitcroft			my $name = $1;
244087a53877SAndy Whitcroft			if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
24413cbf62dfSAndy Whitcroft			    $name =~ /^${Ident}_$2/) {
24423cbf62dfSAndy Whitcroft#print "FOO C name<$name>\n";
24433cbf62dfSAndy Whitcroft				$suppress_export{$realline_next} = 1;
24443cbf62dfSAndy Whitcroft
24453cbf62dfSAndy Whitcroft			} elsif ($stat !~ /(?:
24462b474a1aSAndy Whitcroft				\n.}\s*$|
244748012058SAndy Whitcroft				^.DEFINE_$Ident\(\Q$name\E\)|
244848012058SAndy Whitcroft				^.DECLARE_$Ident\(\Q$name\E\)|
244948012058SAndy Whitcroft				^.LIST_HEAD\(\Q$name\E\)|
24502b474a1aSAndy Whitcroft				^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
24512b474a1aSAndy Whitcroft				\b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
245248012058SAndy Whitcroft			    )/x) {
24532b474a1aSAndy Whitcroft#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
24542b474a1aSAndy Whitcroft				$suppress_export{$realline_next} = 2;
24552b474a1aSAndy Whitcroft			} else {
24562b474a1aSAndy Whitcroft				$suppress_export{$realline_next} = 1;
24570a920b5bSAndy Whitcroft			}
24580a920b5bSAndy Whitcroft		}
24592b474a1aSAndy Whitcroft		if (!defined $suppress_export{$linenr} &&
24602b474a1aSAndy Whitcroft		    $prevline =~ /^.\s*$/ &&
24612b474a1aSAndy Whitcroft		    ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
24622b474a1aSAndy Whitcroft		     $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
24632b474a1aSAndy Whitcroft#print "FOO B <$lines[$linenr - 1]>\n";
24642b474a1aSAndy Whitcroft			$suppress_export{$linenr} = 2;
24652b474a1aSAndy Whitcroft		}
24662b474a1aSAndy Whitcroft		if (defined $suppress_export{$linenr} &&
24672b474a1aSAndy Whitcroft		    $suppress_export{$linenr} == 2) {
2468000d1cc1SJoe Perches			WARN("EXPORT_SYMBOL",
2469000d1cc1SJoe Perches			     "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
24702b474a1aSAndy Whitcroft		}
24710a920b5bSAndy Whitcroft
24725150bda4SJoe Eloff# check for global initialisers.
2473c45dcabdSAndy Whitcroft		if ($line =~ /^.$Type\s*$Ident\s*(?:\s+$Modifier)*\s*=\s*(0|NULL|false)\s*;/) {
2474000d1cc1SJoe Perches			ERROR("GLOBAL_INITIALISERS",
2475000d1cc1SJoe Perches			      "do not initialise globals to 0 or NULL\n" .
2476f0a594c1SAndy Whitcroft				$herecurr);
2477f0a594c1SAndy Whitcroft		}
24780a920b5bSAndy Whitcroft# check for static initialisers.
24792d1bafd7SAndy Whitcroft		if ($line =~ /\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
2480000d1cc1SJoe Perches			ERROR("INITIALISED_STATIC",
2481000d1cc1SJoe Perches			      "do not initialise statics to 0 or NULL\n" .
2482de7d4f0eSAndy Whitcroft				$herecurr);
24830a920b5bSAndy Whitcroft		}
24840a920b5bSAndy Whitcroft
2485cb710ecaSJoe Perches# check for static const char * arrays.
2486cb710ecaSJoe Perches		if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
2487000d1cc1SJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
2488000d1cc1SJoe Perches			     "static const char * array should probably be static const char * const\n" .
2489cb710ecaSJoe Perches				$herecurr);
2490cb710ecaSJoe Perches               }
2491cb710ecaSJoe Perches
2492cb710ecaSJoe Perches# check for static char foo[] = "bar" declarations.
2493cb710ecaSJoe Perches		if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
2494000d1cc1SJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
2495000d1cc1SJoe Perches			     "static char array declaration should probably be static const char\n" .
2496cb710ecaSJoe Perches				$herecurr);
2497cb710ecaSJoe Perches               }
2498cb710ecaSJoe Perches
249993ed0e2dSJoe Perches# check for declarations of struct pci_device_id
250093ed0e2dSJoe Perches		if ($line =~ /\bstruct\s+pci_device_id\s+\w+\s*\[\s*\]\s*\=\s*\{/) {
2501000d1cc1SJoe Perches			WARN("DEFINE_PCI_DEVICE_TABLE",
2502000d1cc1SJoe Perches			     "Use DEFINE_PCI_DEVICE_TABLE for struct pci_device_id\n" . $herecurr);
250393ed0e2dSJoe Perches		}
250493ed0e2dSJoe Perches
2505653d4876SAndy Whitcroft# check for new typedefs, only function parameters and sparse annotations
2506653d4876SAndy Whitcroft# make sense.
2507653d4876SAndy Whitcroft		if ($line =~ /\btypedef\s/ &&
25088054576dSAndy Whitcroft		    $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
2509c45dcabdSAndy Whitcroft		    $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
25108ed22cadSAndy Whitcroft		    $line !~ /\b$typeTypedefs\b/ &&
2511653d4876SAndy Whitcroft		    $line !~ /\b__bitwise(?:__|)\b/) {
2512000d1cc1SJoe Perches			WARN("NEW_TYPEDEFS",
2513000d1cc1SJoe Perches			     "do not add new typedefs\n" . $herecurr);
25140a920b5bSAndy Whitcroft		}
25150a920b5bSAndy Whitcroft
25160a920b5bSAndy Whitcroft# * goes on variable not on type
251765863862SAndy Whitcroft		# (char*[ const])
2518bfcb2cc7SAndy Whitcroft		while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
2519bfcb2cc7SAndy Whitcroft			#print "AA<$1>\n";
25203705ce5bSJoe Perches			my ($ident, $from, $to) = ($1, $2, $2);
2521d8aaf121SAndy Whitcroft
252265863862SAndy Whitcroft			# Should start with a space.
252365863862SAndy Whitcroft			$to =~ s/^(\S)/ $1/;
252465863862SAndy Whitcroft			# Should not end with a space.
252565863862SAndy Whitcroft			$to =~ s/\s+$//;
252665863862SAndy Whitcroft			# '*'s should not have spaces between.
2527f9a0b3d1SAndy Whitcroft			while ($to =~ s/\*\s+\*/\*\*/) {
252865863862SAndy Whitcroft			}
2529d8aaf121SAndy Whitcroft
25303705ce5bSJoe Perches##			print "1: from<$from> to<$to> ident<$ident>\n";
253165863862SAndy Whitcroft			if ($from ne $to) {
25323705ce5bSJoe Perches				if (ERROR("POINTER_LOCATION",
25333705ce5bSJoe Perches					  "\"(foo$from)\" should be \"(foo$to)\"\n" .  $herecurr) &&
25343705ce5bSJoe Perches				    $fix) {
25353705ce5bSJoe Perches					my $sub_from = $ident;
25363705ce5bSJoe Perches					my $sub_to = $ident;
25373705ce5bSJoe Perches					$sub_to =~ s/\Q$from\E/$to/;
25383705ce5bSJoe Perches					$fixed[$linenr - 1] =~
25393705ce5bSJoe Perches					    s@\Q$sub_from\E@$sub_to@;
25403705ce5bSJoe Perches				}
254165863862SAndy Whitcroft			}
2542bfcb2cc7SAndy Whitcroft		}
2543bfcb2cc7SAndy Whitcroft		while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
2544bfcb2cc7SAndy Whitcroft			#print "BB<$1>\n";
25453705ce5bSJoe Perches			my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
2546d8aaf121SAndy Whitcroft
254765863862SAndy Whitcroft			# Should start with a space.
254865863862SAndy Whitcroft			$to =~ s/^(\S)/ $1/;
254965863862SAndy Whitcroft			# Should not end with a space.
255065863862SAndy Whitcroft			$to =~ s/\s+$//;
255165863862SAndy Whitcroft			# '*'s should not have spaces between.
2552f9a0b3d1SAndy Whitcroft			while ($to =~ s/\*\s+\*/\*\*/) {
255365863862SAndy Whitcroft			}
255465863862SAndy Whitcroft			# Modifiers should have spaces.
255565863862SAndy Whitcroft			$to =~ s/(\b$Modifier$)/$1 /;
255665863862SAndy Whitcroft
25573705ce5bSJoe Perches##			print "2: from<$from> to<$to> ident<$ident>\n";
2558667026e7SAndy Whitcroft			if ($from ne $to && $ident !~ /^$Modifier$/) {
25593705ce5bSJoe Perches				if (ERROR("POINTER_LOCATION",
25603705ce5bSJoe Perches					  "\"foo${from}bar\" should be \"foo${to}bar\"\n" .  $herecurr) &&
25613705ce5bSJoe Perches				    $fix) {
25623705ce5bSJoe Perches
25633705ce5bSJoe Perches					my $sub_from = $match;
25643705ce5bSJoe Perches					my $sub_to = $match;
25653705ce5bSJoe Perches					$sub_to =~ s/\Q$from\E/$to/;
25663705ce5bSJoe Perches					$fixed[$linenr - 1] =~
25673705ce5bSJoe Perches					    s@\Q$sub_from\E@$sub_to@;
25683705ce5bSJoe Perches				}
256965863862SAndy Whitcroft			}
25700a920b5bSAndy Whitcroft		}
25710a920b5bSAndy Whitcroft
25720a920b5bSAndy Whitcroft# # no BUG() or BUG_ON()
25730a920b5bSAndy Whitcroft# 		if ($line =~ /\b(BUG|BUG_ON)\b/) {
25740a920b5bSAndy Whitcroft# 			print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
25750a920b5bSAndy Whitcroft# 			print "$herecurr";
25760a920b5bSAndy Whitcroft# 			$clean = 0;
25770a920b5bSAndy Whitcroft# 		}
25780a920b5bSAndy Whitcroft
25798905a67cSAndy Whitcroft		if ($line =~ /\bLINUX_VERSION_CODE\b/) {
2580000d1cc1SJoe Perches			WARN("LINUX_VERSION_CODE",
2581000d1cc1SJoe Perches			     "LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
25828905a67cSAndy Whitcroft		}
25838905a67cSAndy Whitcroft
258417441227SJoe Perches# check for uses of printk_ratelimit
258517441227SJoe Perches		if ($line =~ /\bprintk_ratelimit\s*\(/) {
2586000d1cc1SJoe Perches			WARN("PRINTK_RATELIMITED",
2587000d1cc1SJoe Perches"Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
258817441227SJoe Perches		}
258917441227SJoe Perches
259000df344fSAndy Whitcroft# printk should use KERN_* levels.  Note that follow on printk's on the
259100df344fSAndy Whitcroft# same line do not need a level, so we use the current block context
259200df344fSAndy Whitcroft# to try and find and validate the current printk.  In summary the current
259325985edcSLucas De Marchi# printk includes all preceding printk's which have no newline on the end.
259400df344fSAndy Whitcroft# we assume the first bad printk is the one to report.
2595f0a594c1SAndy Whitcroft		if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
259600df344fSAndy Whitcroft			my $ok = 0;
259700df344fSAndy Whitcroft			for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
259800df344fSAndy Whitcroft				#print "CHECK<$lines[$ln - 1]\n";
259925985edcSLucas De Marchi				# we have a preceding printk if it ends
260000df344fSAndy Whitcroft				# with "\n" ignore it, else it is to blame
260100df344fSAndy Whitcroft				if ($lines[$ln - 1] =~ m{\bprintk\(}) {
260200df344fSAndy Whitcroft					if ($rawlines[$ln - 1] !~ m{\\n"}) {
260300df344fSAndy Whitcroft						$ok = 1;
260400df344fSAndy Whitcroft					}
260500df344fSAndy Whitcroft					last;
260600df344fSAndy Whitcroft				}
260700df344fSAndy Whitcroft			}
260800df344fSAndy Whitcroft			if ($ok == 0) {
2609000d1cc1SJoe Perches				WARN("PRINTK_WITHOUT_KERN_LEVEL",
2610000d1cc1SJoe Perches				     "printk() should include KERN_ facility level\n" . $herecurr);
26110a920b5bSAndy Whitcroft			}
261200df344fSAndy Whitcroft		}
26130a920b5bSAndy Whitcroft
2614243f3803SJoe Perches		if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
2615243f3803SJoe Perches			my $orig = $1;
2616243f3803SJoe Perches			my $level = lc($orig);
2617243f3803SJoe Perches			$level = "warn" if ($level eq "warning");
26188f26b837SJoe Perches			my $level2 = $level;
26198f26b837SJoe Perches			$level2 = "dbg" if ($level eq "debug");
2620243f3803SJoe Perches			WARN("PREFER_PR_LEVEL",
26218f26b837SJoe Perches			     "Prefer netdev_$level2(netdev, ... then dev_$level2(dev, ... then pr_$level(...  to printk(KERN_$orig ...\n" . $herecurr);
2622243f3803SJoe Perches		}
2623243f3803SJoe Perches
2624243f3803SJoe Perches		if ($line =~ /\bpr_warning\s*\(/) {
2625243f3803SJoe Perches			WARN("PREFER_PR_LEVEL",
2626243f3803SJoe Perches			     "Prefer pr_warn(... to pr_warning(...\n" . $herecurr);
2627243f3803SJoe Perches		}
2628243f3803SJoe Perches
2629dc139313SJoe Perches		if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
2630dc139313SJoe Perches			my $orig = $1;
2631dc139313SJoe Perches			my $level = lc($orig);
2632dc139313SJoe Perches			$level = "warn" if ($level eq "warning");
2633dc139313SJoe Perches			$level = "dbg" if ($level eq "debug");
2634dc139313SJoe Perches			WARN("PREFER_DEV_LEVEL",
2635dc139313SJoe Perches			     "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
2636dc139313SJoe Perches		}
2637dc139313SJoe Perches
2638653d4876SAndy Whitcroft# function brace can't be on same line, except for #defines of do while,
2639653d4876SAndy Whitcroft# or if closed on same line
2640c45dcabdSAndy Whitcroft		if (($line=~/$Type\s*$Ident\(.*\).*\s{/) and
2641c45dcabdSAndy Whitcroft		    !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) {
2642000d1cc1SJoe Perches			ERROR("OPEN_BRACE",
2643000d1cc1SJoe Perches			      "open brace '{' following function declarations go on the next line\n" . $herecurr);
26440a920b5bSAndy Whitcroft		}
2645653d4876SAndy Whitcroft
26468905a67cSAndy Whitcroft# open braces for enum, union and struct go on the same line.
26478905a67cSAndy Whitcroft		if ($line =~ /^.\s*{/ &&
26488905a67cSAndy Whitcroft		    $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
2649000d1cc1SJoe Perches			ERROR("OPEN_BRACE",
2650000d1cc1SJoe Perches			      "open brace '{' following $1 go on the same line\n" . $hereprev);
26518905a67cSAndy Whitcroft		}
26528905a67cSAndy Whitcroft
26530c73b4ebSAndy Whitcroft# missing space after union, struct or enum definition
26543705ce5bSJoe Perches		if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
26553705ce5bSJoe Perches			if (WARN("SPACING",
26563705ce5bSJoe Perches				 "missing space after $1 definition\n" . $herecurr) &&
26573705ce5bSJoe Perches			    $fix) {
26583705ce5bSJoe Perches				$fixed[$linenr - 1] =~
26593705ce5bSJoe Perches				    s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
26603705ce5bSJoe Perches			}
26610c73b4ebSAndy Whitcroft		}
26620c73b4ebSAndy Whitcroft
26638d31cfceSAndy Whitcroft# check for spacing round square brackets; allowed:
26648d31cfceSAndy Whitcroft#  1. with a type on the left -- int [] a;
2665fe2a7dbcSAndy Whitcroft#  2. at the beginning of a line for slice initialisers -- [0...10] = 5,
2666fe2a7dbcSAndy Whitcroft#  3. inside a curly brace -- = { [0...10] = 5 }
26678d31cfceSAndy Whitcroft		while ($line =~ /(.*?\s)\[/g) {
26688d31cfceSAndy Whitcroft			my ($where, $prefix) = ($-[1], $1);
26698d31cfceSAndy Whitcroft			if ($prefix !~ /$Type\s+$/ &&
2670fe2a7dbcSAndy Whitcroft			    ($where != 0 || $prefix !~ /^.\s+$/) &&
2671daebc534SAndy Whitcroft			    $prefix !~ /[{,]\s+$/) {
26723705ce5bSJoe Perches				if (ERROR("BRACKET_SPACE",
26733705ce5bSJoe Perches					  "space prohibited before open square bracket '['\n" . $herecurr) &&
26743705ce5bSJoe Perches				    $fix) {
26753705ce5bSJoe Perches				    $fixed[$linenr - 1] =~
26763705ce5bSJoe Perches					s/^(\+.*?)\s+\[/$1\[/;
26773705ce5bSJoe Perches				}
26788d31cfceSAndy Whitcroft			}
26798d31cfceSAndy Whitcroft		}
26808d31cfceSAndy Whitcroft
2681f0a594c1SAndy Whitcroft# check for spaces between functions and their parentheses.
26826c72ffaaSAndy Whitcroft		while ($line =~ /($Ident)\s+\(/g) {
2683c2fdda0dSAndy Whitcroft			my $name = $1;
2684773647a0SAndy Whitcroft			my $ctx_before = substr($line, 0, $-[1]);
2685773647a0SAndy Whitcroft			my $ctx = "$ctx_before$name";
2686c2fdda0dSAndy Whitcroft
2687c2fdda0dSAndy Whitcroft			# Ignore those directives where spaces _are_ permitted.
2688773647a0SAndy Whitcroft			if ($name =~ /^(?:
2689773647a0SAndy Whitcroft				if|for|while|switch|return|case|
2690773647a0SAndy Whitcroft				volatile|__volatile__|
2691773647a0SAndy Whitcroft				__attribute__|format|__extension__|
2692773647a0SAndy Whitcroft				asm|__asm__)$/x)
2693773647a0SAndy Whitcroft			{
2694c2fdda0dSAndy Whitcroft			# cpp #define statements have non-optional spaces, ie
2695c2fdda0dSAndy Whitcroft			# if there is a space between the name and the open
2696c2fdda0dSAndy Whitcroft			# parenthesis it is simply not a parameter group.
2697c45dcabdSAndy Whitcroft			} elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
2698773647a0SAndy Whitcroft
2699773647a0SAndy Whitcroft			# cpp #elif statement condition may start with a (
2700c45dcabdSAndy Whitcroft			} elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
2701c2fdda0dSAndy Whitcroft
2702c2fdda0dSAndy Whitcroft			# If this whole things ends with a type its most
2703c2fdda0dSAndy Whitcroft			# likely a typedef for a function.
2704773647a0SAndy Whitcroft			} elsif ($ctx =~ /$Type$/) {
2705c2fdda0dSAndy Whitcroft
2706c2fdda0dSAndy Whitcroft			} else {
27073705ce5bSJoe Perches				if (WARN("SPACING",
27083705ce5bSJoe Perches					 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
27093705ce5bSJoe Perches					     $fix) {
27103705ce5bSJoe Perches					$fixed[$linenr - 1] =~
27113705ce5bSJoe Perches					    s/\b$name\s+\(/$name\(/;
27123705ce5bSJoe Perches				}
2713f0a594c1SAndy Whitcroft			}
27146c72ffaaSAndy Whitcroft		}
27159a4cad4eSEric Nelson
2716653d4876SAndy Whitcroft# Check operator spacing.
27170a920b5bSAndy Whitcroft		if (!($line=~/\#\s*include/)) {
27183705ce5bSJoe Perches			my $fixed_line = "";
27193705ce5bSJoe Perches			my $line_fixed = 0;
27203705ce5bSJoe Perches
27219c0ca6f9SAndy Whitcroft			my $ops = qr{
27229c0ca6f9SAndy Whitcroft				<<=|>>=|<=|>=|==|!=|
27239c0ca6f9SAndy Whitcroft				\+=|-=|\*=|\/=|%=|\^=|\|=|&=|
27249c0ca6f9SAndy Whitcroft				=>|->|<<|>>|<|>|=|!|~|
27251f65f947SAndy Whitcroft				&&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
27261f65f947SAndy Whitcroft				\?|:
27279c0ca6f9SAndy Whitcroft			}x;
2728cf655043SAndy Whitcroft			my @elements = split(/($ops|;)/, $opline);
27293705ce5bSJoe Perches
27303705ce5bSJoe Perches##			print("element count: <" . $#elements . ">\n");
27313705ce5bSJoe Perches##			foreach my $el (@elements) {
27323705ce5bSJoe Perches##				print("el: <$el>\n");
27333705ce5bSJoe Perches##			}
27343705ce5bSJoe Perches
27353705ce5bSJoe Perches			my @fix_elements = ();
273600df344fSAndy Whitcroft			my $off = 0;
27376c72ffaaSAndy Whitcroft
27383705ce5bSJoe Perches			foreach my $el (@elements) {
27393705ce5bSJoe Perches				push(@fix_elements, substr($rawline, $off, length($el)));
27403705ce5bSJoe Perches				$off += length($el);
27413705ce5bSJoe Perches			}
27423705ce5bSJoe Perches
27433705ce5bSJoe Perches			$off = 0;
27443705ce5bSJoe Perches
27456c72ffaaSAndy Whitcroft			my $blank = copy_spacing($opline);
27466c72ffaaSAndy Whitcroft
27470a920b5bSAndy Whitcroft			for (my $n = 0; $n < $#elements; $n += 2) {
27483705ce5bSJoe Perches
27493705ce5bSJoe Perches				my $good = $fix_elements[$n] . $fix_elements[$n + 1];
27503705ce5bSJoe Perches
27513705ce5bSJoe Perches##				print("n: <$n> good: <$good>\n");
27523705ce5bSJoe Perches
27534a0df2efSAndy Whitcroft				$off += length($elements[$n]);
27544a0df2efSAndy Whitcroft
275525985edcSLucas De Marchi				# Pick up the preceding and succeeding characters.
2756773647a0SAndy Whitcroft				my $ca = substr($opline, 0, $off);
2757773647a0SAndy Whitcroft				my $cc = '';
2758773647a0SAndy Whitcroft				if (length($opline) >= ($off + length($elements[$n + 1]))) {
2759773647a0SAndy Whitcroft					$cc = substr($opline, $off + length($elements[$n + 1]));
2760773647a0SAndy Whitcroft				}
2761773647a0SAndy Whitcroft				my $cb = "$ca$;$cc";
2762773647a0SAndy Whitcroft
27634a0df2efSAndy Whitcroft				my $a = '';
27644a0df2efSAndy Whitcroft				$a = 'V' if ($elements[$n] ne '');
27654a0df2efSAndy Whitcroft				$a = 'W' if ($elements[$n] =~ /\s$/);
2766cf655043SAndy Whitcroft				$a = 'C' if ($elements[$n] =~ /$;$/);
27674a0df2efSAndy Whitcroft				$a = 'B' if ($elements[$n] =~ /(\[|\()$/);
27684a0df2efSAndy Whitcroft				$a = 'O' if ($elements[$n] eq '');
2769773647a0SAndy Whitcroft				$a = 'E' if ($ca =~ /^\s*$/);
27704a0df2efSAndy Whitcroft
27710a920b5bSAndy Whitcroft				my $op = $elements[$n + 1];
27724a0df2efSAndy Whitcroft
27734a0df2efSAndy Whitcroft				my $c = '';
27740a920b5bSAndy Whitcroft				if (defined $elements[$n + 2]) {
27754a0df2efSAndy Whitcroft					$c = 'V' if ($elements[$n + 2] ne '');
27764a0df2efSAndy Whitcroft					$c = 'W' if ($elements[$n + 2] =~ /^\s/);
2777cf655043SAndy Whitcroft					$c = 'C' if ($elements[$n + 2] =~ /^$;/);
27784a0df2efSAndy Whitcroft					$c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
27794a0df2efSAndy Whitcroft					$c = 'O' if ($elements[$n + 2] eq '');
27808b1b3378SAndy Whitcroft					$c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
27814a0df2efSAndy Whitcroft				} else {
27824a0df2efSAndy Whitcroft					$c = 'E';
27830a920b5bSAndy Whitcroft				}
27840a920b5bSAndy Whitcroft
27854a0df2efSAndy Whitcroft				my $ctx = "${a}x${c}";
27864a0df2efSAndy Whitcroft
27874a0df2efSAndy Whitcroft				my $at = "(ctx:$ctx)";
27884a0df2efSAndy Whitcroft
27896c72ffaaSAndy Whitcroft				my $ptr = substr($blank, 0, $off) . "^";
2790de7d4f0eSAndy Whitcroft				my $hereptr = "$hereline$ptr\n";
27910a920b5bSAndy Whitcroft
279274048ed8SAndy Whitcroft				# Pull out the value of this operator.
27936c72ffaaSAndy Whitcroft				my $op_type = substr($curr_values, $off + 1, 1);
27940a920b5bSAndy Whitcroft
27951f65f947SAndy Whitcroft				# Get the full operator variant.
27961f65f947SAndy Whitcroft				my $opv = $op . substr($curr_vars, $off, 1);
27971f65f947SAndy Whitcroft
279813214adfSAndy Whitcroft				# Ignore operators passed as parameters.
279913214adfSAndy Whitcroft				if ($op_type ne 'V' &&
280013214adfSAndy Whitcroft				    $ca =~ /\s$/ && $cc =~ /^\s*,/) {
280113214adfSAndy Whitcroft
2802cf655043SAndy Whitcroft#				# Ignore comments
2803cf655043SAndy Whitcroft#				} elsif ($op =~ /^$;+$/) {
280413214adfSAndy Whitcroft
2805d8aaf121SAndy Whitcroft				# ; should have either the end of line or a space or \ after it
280613214adfSAndy Whitcroft				} elsif ($op eq ';') {
2807cf655043SAndy Whitcroft					if ($ctx !~ /.x[WEBC]/ &&
2808cf655043SAndy Whitcroft					    $cc !~ /^\\/ && $cc !~ /^;/) {
28093705ce5bSJoe Perches						if (ERROR("SPACING",
28103705ce5bSJoe Perches							  "space required after that '$op' $at\n" . $hereptr)) {
28113705ce5bSJoe Perches							$good = trim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
28123705ce5bSJoe Perches							$line_fixed = 1;
28133705ce5bSJoe Perches						}
2814d8aaf121SAndy Whitcroft					}
2815d8aaf121SAndy Whitcroft
2816d8aaf121SAndy Whitcroft				# // is a comment
2817d8aaf121SAndy Whitcroft				} elsif ($op eq '//') {
28180a920b5bSAndy Whitcroft
28191f65f947SAndy Whitcroft				# No spaces for:
28201f65f947SAndy Whitcroft				#   ->
28211f65f947SAndy Whitcroft				#   :   when part of a bitfield
28221f65f947SAndy Whitcroft				} elsif ($op eq '->' || $opv eq ':B') {
28234a0df2efSAndy Whitcroft					if ($ctx =~ /Wx.|.xW/) {
28243705ce5bSJoe Perches						if (ERROR("SPACING",
28253705ce5bSJoe Perches							  "spaces prohibited around that '$op' $at\n" . $hereptr)) {
28263705ce5bSJoe Perches							$good = trim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
28273705ce5bSJoe Perches							$line_fixed = 1;
28283705ce5bSJoe Perches							if (defined $fix_elements[$n + 2]) {
28293705ce5bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
28303705ce5bSJoe Perches							}
28313705ce5bSJoe Perches						}
28320a920b5bSAndy Whitcroft					}
28330a920b5bSAndy Whitcroft
28340a920b5bSAndy Whitcroft				# , must have a space on the right.
28350a920b5bSAndy Whitcroft				} elsif ($op eq ',') {
2836cf655043SAndy Whitcroft					if ($ctx !~ /.x[WEC]/ && $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						}
28420a920b5bSAndy Whitcroft					}
28430a920b5bSAndy Whitcroft
28449c0ca6f9SAndy Whitcroft				# '*' as part of a type definition -- reported already.
284574048ed8SAndy Whitcroft				} elsif ($opv eq '*_') {
28469c0ca6f9SAndy Whitcroft					#warn "'*' is part of type\n";
28479c0ca6f9SAndy Whitcroft
28489c0ca6f9SAndy Whitcroft				# unary operators should have a space before and
28499c0ca6f9SAndy Whitcroft				# none after.  May be left adjacent to another
28509c0ca6f9SAndy Whitcroft				# unary operator, or a cast
28519c0ca6f9SAndy Whitcroft				} elsif ($op eq '!' || $op eq '~' ||
285274048ed8SAndy Whitcroft					 $opv eq '*U' || $opv eq '-U' ||
28530d413866SAndy Whitcroft					 $opv eq '&U' || $opv eq '&&U') {
2854cf655043SAndy Whitcroft					if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
28553705ce5bSJoe Perches						if (ERROR("SPACING",
28563705ce5bSJoe Perches							  "space required before that '$op' $at\n" . $hereptr)) {
28573705ce5bSJoe Perches							$good = trim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]);
28583705ce5bSJoe Perches							$line_fixed = 1;
28593705ce5bSJoe Perches						}
28600a920b5bSAndy Whitcroft					}
2861a3340b35SAndy Whitcroft					if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
2862171ae1a4SAndy Whitcroft						# A unary '*' may be const
2863171ae1a4SAndy Whitcroft
2864171ae1a4SAndy Whitcroft					} elsif ($ctx =~ /.xW/) {
28653705ce5bSJoe Perches						if (ERROR("SPACING",
28663705ce5bSJoe Perches							  "space prohibited after that '$op' $at\n" . $hereptr)) {
28673705ce5bSJoe Perches							$fixed_line =~ s/\s+$//;
28683705ce5bSJoe Perches							$good = trim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
28693705ce5bSJoe Perches							$line_fixed = 1;
28703705ce5bSJoe Perches							if (defined $fix_elements[$n + 2]) {
28713705ce5bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
28723705ce5bSJoe Perches							}
28733705ce5bSJoe Perches						}
28740a920b5bSAndy Whitcroft					}
28750a920b5bSAndy Whitcroft
28760a920b5bSAndy Whitcroft				# unary ++ and unary -- are allowed no space on one side.
28770a920b5bSAndy Whitcroft				} elsif ($op eq '++' or $op eq '--') {
2878773647a0SAndy Whitcroft					if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
28793705ce5bSJoe Perches						if (ERROR("SPACING",
28803705ce5bSJoe Perches							  "space required one side of that '$op' $at\n" . $hereptr)) {
28813705ce5bSJoe Perches							$fixed_line =~ s/\s+$//;
28823705ce5bSJoe Perches							$good = trim($fix_elements[$n]) . trim($fix_elements[$n + 1]) . " ";
28833705ce5bSJoe Perches							$line_fixed = 1;
28843705ce5bSJoe Perches						}
28850a920b5bSAndy Whitcroft					}
2886773647a0SAndy Whitcroft					if ($ctx =~ /Wx[BE]/ ||
2887773647a0SAndy Whitcroft					    ($ctx =~ /Wx./ && $cc =~ /^;/)) {
28883705ce5bSJoe Perches						if (ERROR("SPACING",
28893705ce5bSJoe Perches							  "space prohibited before that '$op' $at\n" . $hereptr)) {
28903705ce5bSJoe Perches							$fixed_line =~ s/\s+$//;
28913705ce5bSJoe Perches							$good = trim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
28923705ce5bSJoe Perches							$line_fixed = 1;
28933705ce5bSJoe Perches						}
2894653d4876SAndy Whitcroft					}
2895773647a0SAndy Whitcroft					if ($ctx =~ /ExW/) {
28963705ce5bSJoe Perches						if (ERROR("SPACING",
28973705ce5bSJoe Perches							  "space prohibited after that '$op' $at\n" . $hereptr)) {
28983705ce5bSJoe Perches							$fixed_line =~ s/\s+$//;
28993705ce5bSJoe Perches							$good = trim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
29003705ce5bSJoe Perches							$line_fixed = 1;
29013705ce5bSJoe Perches							if (defined $fix_elements[$n + 2]) {
29023705ce5bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
2903773647a0SAndy Whitcroft							}
29043705ce5bSJoe Perches						}
29053705ce5bSJoe Perches					}
29060a920b5bSAndy Whitcroft
29070a920b5bSAndy Whitcroft				# << and >> may either have or not have spaces both sides
29089c0ca6f9SAndy Whitcroft				} elsif ($op eq '<<' or $op eq '>>' or
29099c0ca6f9SAndy Whitcroft					 $op eq '&' or $op eq '^' or $op eq '|' or
29109c0ca6f9SAndy Whitcroft					 $op eq '+' or $op eq '-' or
2911c2fdda0dSAndy Whitcroft					 $op eq '*' or $op eq '/' or
2912c2fdda0dSAndy Whitcroft					 $op eq '%')
29130a920b5bSAndy Whitcroft				{
2914773647a0SAndy Whitcroft					if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
29153705ce5bSJoe Perches						if (ERROR("SPACING",
29163705ce5bSJoe Perches							  "need consistent spacing around '$op' $at\n" . $hereptr)) {
29173705ce5bSJoe Perches							$fixed_line =~ s/\s+$//;
29183705ce5bSJoe Perches							$good = trim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
29193705ce5bSJoe Perches							$line_fixed = 1;
29203705ce5bSJoe Perches						}
29210a920b5bSAndy Whitcroft					}
29220a920b5bSAndy Whitcroft
29231f65f947SAndy Whitcroft				# A colon needs no spaces before when it is
29241f65f947SAndy Whitcroft				# terminating a case value or a label.
29251f65f947SAndy Whitcroft				} elsif ($opv eq ':C' || $opv eq ':L') {
29261f65f947SAndy Whitcroft					if ($ctx =~ /Wx./) {
29273705ce5bSJoe Perches						if (ERROR("SPACING",
29283705ce5bSJoe Perches							  "space prohibited before that '$op' $at\n" . $hereptr)) {
29293705ce5bSJoe Perches							$good = trim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
29303705ce5bSJoe Perches							$line_fixed = 1;
29313705ce5bSJoe Perches						}
29321f65f947SAndy Whitcroft					}
29331f65f947SAndy Whitcroft
29340a920b5bSAndy Whitcroft				# All the others need spaces both sides.
2935cf655043SAndy Whitcroft				} elsif ($ctx !~ /[EWC]x[CWE]/) {
29361f65f947SAndy Whitcroft					my $ok = 0;
29371f65f947SAndy Whitcroft
293822f2a2efSAndy Whitcroft					# Ignore email addresses <foo@bar>
29391f65f947SAndy Whitcroft					if (($op eq '<' &&
29401f65f947SAndy Whitcroft					     $cc =~ /^\S+\@\S+>/) ||
29411f65f947SAndy Whitcroft					    ($op eq '>' &&
29421f65f947SAndy Whitcroft					     $ca =~ /<\S+\@\S+$/))
29431f65f947SAndy Whitcroft					{
29441f65f947SAndy Whitcroft					    	$ok = 1;
29451f65f947SAndy Whitcroft					}
29461f65f947SAndy Whitcroft
29471f65f947SAndy Whitcroft					# Ignore ?:
29481f65f947SAndy Whitcroft					if (($opv eq ':O' && $ca =~ /\?$/) ||
29491f65f947SAndy Whitcroft					    ($op eq '?' && $cc =~ /^:/)) {
29501f65f947SAndy Whitcroft					    	$ok = 1;
29511f65f947SAndy Whitcroft					}
29521f65f947SAndy Whitcroft
29531f65f947SAndy Whitcroft					if ($ok == 0) {
29543705ce5bSJoe Perches						if (ERROR("SPACING",
29553705ce5bSJoe Perches							  "spaces required around that '$op' $at\n" . $hereptr)) {
29563705ce5bSJoe Perches							$good = trim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
29573705ce5bSJoe Perches							$good = $fix_elements[$n] . " " . trim($fix_elements[$n + 1]) . " ";
29583705ce5bSJoe Perches							$line_fixed = 1;
29593705ce5bSJoe Perches						}
29600a920b5bSAndy Whitcroft					}
296122f2a2efSAndy Whitcroft				}
29624a0df2efSAndy Whitcroft				$off += length($elements[$n + 1]);
29633705ce5bSJoe Perches
29643705ce5bSJoe Perches##				print("n: <$n> GOOD: <$good>\n");
29653705ce5bSJoe Perches
29663705ce5bSJoe Perches				$fixed_line = $fixed_line . $good;
29670a920b5bSAndy Whitcroft			}
29683705ce5bSJoe Perches
29693705ce5bSJoe Perches			if (($#elements % 2) == 0) {
29703705ce5bSJoe Perches				$fixed_line = $fixed_line . $fix_elements[$#elements];
29713705ce5bSJoe Perches			}
29723705ce5bSJoe Perches
29733705ce5bSJoe Perches			if ($fix && $line_fixed && $fixed_line ne $fixed[$linenr - 1]) {
29743705ce5bSJoe Perches				$fixed[$linenr - 1] = $fixed_line;
29753705ce5bSJoe Perches			}
29763705ce5bSJoe Perches
29773705ce5bSJoe Perches
29780a920b5bSAndy Whitcroft		}
29790a920b5bSAndy Whitcroft
2980786b6326SJoe Perches# check for whitespace before a non-naked semicolon
2981786b6326SJoe Perches		if ($line =~ /^\+.*\S\s+;/) {
2982786b6326SJoe Perches			if (WARN("SPACING",
2983786b6326SJoe Perches				 "space prohibited before semicolon\n" . $herecurr) &&
2984786b6326SJoe Perches			    $fix) {
2985786b6326SJoe Perches				1 while $fixed[$linenr - 1] =~
2986786b6326SJoe Perches				    s/^(\+.*\S)\s+;/$1;/;
2987786b6326SJoe Perches			}
2988786b6326SJoe Perches		}
2989786b6326SJoe Perches
2990f0a594c1SAndy Whitcroft# check for multiple assignments
2991f0a594c1SAndy Whitcroft		if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
2992000d1cc1SJoe Perches			CHK("MULTIPLE_ASSIGNMENTS",
2993000d1cc1SJoe Perches			    "multiple assignments should be avoided\n" . $herecurr);
2994f0a594c1SAndy Whitcroft		}
2995f0a594c1SAndy Whitcroft
299622f2a2efSAndy Whitcroft## # check for multiple declarations, allowing for a function declaration
299722f2a2efSAndy Whitcroft## # continuation.
299822f2a2efSAndy Whitcroft## 		if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
299922f2a2efSAndy Whitcroft## 		    $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
300022f2a2efSAndy Whitcroft##
300122f2a2efSAndy Whitcroft## 			# Remove any bracketed sections to ensure we do not
300222f2a2efSAndy Whitcroft## 			# falsly report the parameters of functions.
300322f2a2efSAndy Whitcroft## 			my $ln = $line;
300422f2a2efSAndy Whitcroft## 			while ($ln =~ s/\([^\(\)]*\)//g) {
300522f2a2efSAndy Whitcroft## 			}
300622f2a2efSAndy Whitcroft## 			if ($ln =~ /,/) {
3007000d1cc1SJoe Perches## 				WARN("MULTIPLE_DECLARATION",
3008000d1cc1SJoe Perches##				     "declaring multiple variables together should be avoided\n" . $herecurr);
300922f2a2efSAndy Whitcroft## 			}
301022f2a2efSAndy Whitcroft## 		}
3011f0a594c1SAndy Whitcroft
30120a920b5bSAndy Whitcroft#need space before brace following if, while, etc
301322f2a2efSAndy Whitcroft		if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
301422f2a2efSAndy Whitcroft		    $line =~ /do{/) {
30153705ce5bSJoe Perches			if (ERROR("SPACING",
30163705ce5bSJoe Perches				  "space required before the open brace '{'\n" . $herecurr) &&
30173705ce5bSJoe Perches			    $fix) {
30183705ce5bSJoe Perches				$fixed[$linenr - 1] =~
30193705ce5bSJoe Perches				    s/^(\+.*(?:do|\))){/$1 {/;
30203705ce5bSJoe Perches			}
3021de7d4f0eSAndy Whitcroft		}
3022de7d4f0eSAndy Whitcroft
3023c4a62ef9SJoe Perches## # check for blank lines before declarations
3024c4a62ef9SJoe Perches##		if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
3025c4a62ef9SJoe Perches##		    $prevrawline =~ /^.\s*$/) {
3026c4a62ef9SJoe Perches##			WARN("SPACING",
3027c4a62ef9SJoe Perches##			     "No blank lines before declarations\n" . $hereprev);
3028c4a62ef9SJoe Perches##		}
3029c4a62ef9SJoe Perches##
3030c4a62ef9SJoe Perches
3031de7d4f0eSAndy Whitcroft# closing brace should have a space following it when it has anything
3032de7d4f0eSAndy Whitcroft# on the line
3033de7d4f0eSAndy Whitcroft		if ($line =~ /}(?!(?:,|;|\)))\S/) {
3034000d1cc1SJoe Perches			ERROR("SPACING",
3035000d1cc1SJoe Perches			      "space required after that close brace '}'\n" . $herecurr);
30360a920b5bSAndy Whitcroft		}
30370a920b5bSAndy Whitcroft
303822f2a2efSAndy Whitcroft# check spacing on square brackets
303922f2a2efSAndy Whitcroft		if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
30403705ce5bSJoe Perches			if (ERROR("SPACING",
30413705ce5bSJoe Perches				  "space prohibited after that open square bracket '['\n" . $herecurr) &&
30423705ce5bSJoe Perches			    $fix) {
30433705ce5bSJoe Perches				$fixed[$linenr - 1] =~
30443705ce5bSJoe Perches				    s/\[\s+/\[/;
30453705ce5bSJoe Perches			}
304622f2a2efSAndy Whitcroft		}
304722f2a2efSAndy Whitcroft		if ($line =~ /\s\]/) {
30483705ce5bSJoe Perches			if (ERROR("SPACING",
30493705ce5bSJoe Perches				  "space prohibited before that close square bracket ']'\n" . $herecurr) &&
30503705ce5bSJoe Perches			    $fix) {
30513705ce5bSJoe Perches				$fixed[$linenr - 1] =~
30523705ce5bSJoe Perches				    s/\s+\]/\]/;
30533705ce5bSJoe Perches			}
305422f2a2efSAndy Whitcroft		}
305522f2a2efSAndy Whitcroft
3056c45dcabdSAndy Whitcroft# check spacing on parentheses
30579c0ca6f9SAndy Whitcroft		if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
30589c0ca6f9SAndy Whitcroft		    $line !~ /for\s*\(\s+;/) {
30593705ce5bSJoe Perches			if (ERROR("SPACING",
30603705ce5bSJoe Perches				  "space prohibited after that open parenthesis '('\n" . $herecurr) &&
30613705ce5bSJoe Perches			    $fix) {
30623705ce5bSJoe Perches				$fixed[$linenr - 1] =~
30633705ce5bSJoe Perches				    s/\(\s+/\(/;
30643705ce5bSJoe Perches			}
306522f2a2efSAndy Whitcroft		}
306613214adfSAndy Whitcroft		if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
3067c45dcabdSAndy Whitcroft		    $line !~ /for\s*\(.*;\s+\)/ &&
3068c45dcabdSAndy Whitcroft		    $line !~ /:\s+\)/) {
30693705ce5bSJoe Perches			if (ERROR("SPACING",
30703705ce5bSJoe Perches				  "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
30713705ce5bSJoe Perches			    $fix) {
30723705ce5bSJoe Perches				$fixed[$linenr - 1] =~
30733705ce5bSJoe Perches				    s/\s+\)/\)/;
30743705ce5bSJoe Perches			}
307522f2a2efSAndy Whitcroft		}
307622f2a2efSAndy Whitcroft
30770a920b5bSAndy Whitcroft#goto labels aren't indented, allow a single space however
30784a0df2efSAndy Whitcroft		if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
30790a920b5bSAndy Whitcroft		   !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
30803705ce5bSJoe Perches			if (WARN("INDENTED_LABEL",
30813705ce5bSJoe Perches				 "labels should not be indented\n" . $herecurr) &&
30823705ce5bSJoe Perches			    $fix) {
30833705ce5bSJoe Perches				$fixed[$linenr - 1] =~
30843705ce5bSJoe Perches				    s/^(.)\s+/$1/;
30853705ce5bSJoe Perches			}
30860a920b5bSAndy Whitcroft		}
30870a920b5bSAndy Whitcroft
3088c45dcabdSAndy Whitcroft# Return is not a function.
3089c45dcabdSAndy Whitcroft		if (defined($stat) && $stat =~ /^.\s*return(\s*)(\(.*);/s) {
3090c45dcabdSAndy Whitcroft			my $spacing = $1;
3091c45dcabdSAndy Whitcroft			my $value = $2;
3092c45dcabdSAndy Whitcroft
309386f9d059SAndy Whitcroft			# Flatten any parentheses
3094fb2d2c1bSAndy Whitcroft			$value =~ s/\(/ \(/g;
3095fb2d2c1bSAndy Whitcroft			$value =~ s/\)/\) /g;
3096e01886adSAndy Whitcroft			while ($value =~ s/\[[^\[\]]*\]/1/ ||
309763f17f89SAndy Whitcroft			       $value !~ /(?:$Ident|-?$Constant)\s*
309863f17f89SAndy Whitcroft					     $Compare\s*
309963f17f89SAndy Whitcroft					     (?:$Ident|-?$Constant)/x &&
310063f17f89SAndy Whitcroft			       $value =~ s/\([^\(\)]*\)/1/) {
3101c45dcabdSAndy Whitcroft			}
3102fb2d2c1bSAndy Whitcroft#print "value<$value>\n";
3103fb2d2c1bSAndy Whitcroft			if ($value =~ /^\s*(?:$Ident|-?$Constant)\s*$/) {
3104000d1cc1SJoe Perches				ERROR("RETURN_PARENTHESES",
3105000d1cc1SJoe Perches				      "return is not a function, parentheses are not required\n" . $herecurr);
3106c45dcabdSAndy Whitcroft
3107c45dcabdSAndy Whitcroft			} elsif ($spacing !~ /\s+/) {
3108000d1cc1SJoe Perches				ERROR("SPACING",
3109000d1cc1SJoe Perches				      "space required before the open parenthesis '('\n" . $herecurr);
3110c45dcabdSAndy Whitcroft			}
3111c45dcabdSAndy Whitcroft		}
311253a3c448SAndy Whitcroft# Return of what appears to be an errno should normally be -'ve
311353a3c448SAndy Whitcroft		if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) {
311453a3c448SAndy Whitcroft			my $name = $1;
311553a3c448SAndy Whitcroft			if ($name ne 'EOF' && $name ne 'ERROR') {
3116000d1cc1SJoe Perches				WARN("USE_NEGATIVE_ERRNO",
3117000d1cc1SJoe Perches				     "return of an errno should typically be -ve (return -$1)\n" . $herecurr);
311853a3c448SAndy Whitcroft			}
311953a3c448SAndy Whitcroft		}
3120c45dcabdSAndy Whitcroft
31210a920b5bSAndy Whitcroft# Need a space before open parenthesis after if, while etc
31224a0df2efSAndy Whitcroft		if ($line =~ /\b(if|while|for|switch)\(/) {
31233705ce5bSJoe Perches			if (ERROR("SPACING",
31243705ce5bSJoe Perches				  "space required before the open parenthesis '('\n" . $herecurr) &&
31253705ce5bSJoe Perches			    $fix) {
31263705ce5bSJoe Perches				$fixed[$linenr - 1] =~
31273705ce5bSJoe Perches				    s/\b(if|while|for|switch)\(/$1 \(/;
31283705ce5bSJoe Perches			}
31290a920b5bSAndy Whitcroft		}
31300a920b5bSAndy Whitcroft
3131f5fe35ddSAndy Whitcroft# Check for illegal assignment in if conditional -- and check for trailing
3132f5fe35ddSAndy Whitcroft# statements after the conditional.
3133170d3a22SAndy Whitcroft		if ($line =~ /do\s*(?!{)/) {
31343e469cdcSAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
31353e469cdcSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0)
31363e469cdcSAndy Whitcroft					if (!defined $stat);
3137170d3a22SAndy Whitcroft			my ($stat_next) = ctx_statement_block($line_nr_next,
3138170d3a22SAndy Whitcroft						$remain_next, $off_next);
3139170d3a22SAndy Whitcroft			$stat_next =~ s/\n./\n /g;
3140170d3a22SAndy Whitcroft			##print "stat<$stat> stat_next<$stat_next>\n";
3141170d3a22SAndy Whitcroft
3142170d3a22SAndy Whitcroft			if ($stat_next =~ /^\s*while\b/) {
3143170d3a22SAndy Whitcroft				# If the statement carries leading newlines,
3144170d3a22SAndy Whitcroft				# then count those as offsets.
3145170d3a22SAndy Whitcroft				my ($whitespace) =
3146170d3a22SAndy Whitcroft					($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
3147170d3a22SAndy Whitcroft				my $offset =
3148170d3a22SAndy Whitcroft					statement_rawlines($whitespace) - 1;
3149170d3a22SAndy Whitcroft
3150170d3a22SAndy Whitcroft				$suppress_whiletrailers{$line_nr_next +
3151170d3a22SAndy Whitcroft								$offset} = 1;
3152170d3a22SAndy Whitcroft			}
3153170d3a22SAndy Whitcroft		}
3154170d3a22SAndy Whitcroft		if (!defined $suppress_whiletrailers{$linenr} &&
3155170d3a22SAndy Whitcroft		    $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
3156171ae1a4SAndy Whitcroft			my ($s, $c) = ($stat, $cond);
31578905a67cSAndy Whitcroft
3158b53c8e10SAndy Whitcroft			if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
3159000d1cc1SJoe Perches				ERROR("ASSIGN_IN_IF",
3160000d1cc1SJoe Perches				      "do not use assignment in if condition\n" . $herecurr);
31618905a67cSAndy Whitcroft			}
31628905a67cSAndy Whitcroft
31638905a67cSAndy Whitcroft			# Find out what is on the end of the line after the
31648905a67cSAndy Whitcroft			# conditional.
3165773647a0SAndy Whitcroft			substr($s, 0, length($c), '');
31668905a67cSAndy Whitcroft			$s =~ s/\n.*//g;
316713214adfSAndy Whitcroft			$s =~ s/$;//g; 	# Remove any comments
316853210168SAndy Whitcroft			if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
316953210168SAndy Whitcroft			    $c !~ /}\s*while\s*/)
3170773647a0SAndy Whitcroft			{
3171bb44ad39SAndy Whitcroft				# Find out how long the conditional actually is.
3172bb44ad39SAndy Whitcroft				my @newlines = ($c =~ /\n/gs);
3173bb44ad39SAndy Whitcroft				my $cond_lines = 1 + $#newlines;
317442bdf74cSHidetoshi Seto				my $stat_real = '';
3175bb44ad39SAndy Whitcroft
317642bdf74cSHidetoshi Seto				$stat_real = raw_line($linenr, $cond_lines)
317742bdf74cSHidetoshi Seto							. "\n" if ($cond_lines);
3178bb44ad39SAndy Whitcroft				if (defined($stat_real) && $cond_lines > 1) {
3179bb44ad39SAndy Whitcroft					$stat_real = "[...]\n$stat_real";
3180bb44ad39SAndy Whitcroft				}
3181bb44ad39SAndy Whitcroft
3182000d1cc1SJoe Perches				ERROR("TRAILING_STATEMENTS",
3183000d1cc1SJoe Perches				      "trailing statements should be on next line\n" . $herecurr . $stat_real);
31848905a67cSAndy Whitcroft			}
31858905a67cSAndy Whitcroft		}
31868905a67cSAndy Whitcroft
318713214adfSAndy Whitcroft# Check for bitwise tests written as boolean
318813214adfSAndy Whitcroft		if ($line =~ /
318913214adfSAndy Whitcroft			(?:
319013214adfSAndy Whitcroft				(?:\[|\(|\&\&|\|\|)
319113214adfSAndy Whitcroft				\s*0[xX][0-9]+\s*
319213214adfSAndy Whitcroft				(?:\&\&|\|\|)
319313214adfSAndy Whitcroft			|
319413214adfSAndy Whitcroft				(?:\&\&|\|\|)
319513214adfSAndy Whitcroft				\s*0[xX][0-9]+\s*
319613214adfSAndy Whitcroft				(?:\&\&|\|\||\)|\])
319713214adfSAndy Whitcroft			)/x)
319813214adfSAndy Whitcroft		{
3199000d1cc1SJoe Perches			WARN("HEXADECIMAL_BOOLEAN_TEST",
3200000d1cc1SJoe Perches			     "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
320113214adfSAndy Whitcroft		}
320213214adfSAndy Whitcroft
32038905a67cSAndy Whitcroft# if and else should not have general statements after it
320413214adfSAndy Whitcroft		if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
320513214adfSAndy Whitcroft			my $s = $1;
320613214adfSAndy Whitcroft			$s =~ s/$;//g; 	# Remove any comments
320713214adfSAndy Whitcroft			if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
3208000d1cc1SJoe Perches				ERROR("TRAILING_STATEMENTS",
3209000d1cc1SJoe Perches				      "trailing statements should be on next line\n" . $herecurr);
32100a920b5bSAndy Whitcroft			}
321113214adfSAndy Whitcroft		}
321239667782SAndy Whitcroft# if should not continue a brace
321339667782SAndy Whitcroft		if ($line =~ /}\s*if\b/) {
3214000d1cc1SJoe Perches			ERROR("TRAILING_STATEMENTS",
3215000d1cc1SJoe Perches			      "trailing statements should be on next line\n" .
321639667782SAndy Whitcroft				$herecurr);
321739667782SAndy Whitcroft		}
3218a1080bf8SAndy Whitcroft# case and default should not have general statements after them
3219a1080bf8SAndy Whitcroft		if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
3220a1080bf8SAndy Whitcroft		    $line !~ /\G(?:
32213fef12d6SAndy Whitcroft			(?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
3222a1080bf8SAndy Whitcroft			\s*return\s+
3223a1080bf8SAndy Whitcroft		    )/xg)
3224a1080bf8SAndy Whitcroft		{
3225000d1cc1SJoe Perches			ERROR("TRAILING_STATEMENTS",
3226000d1cc1SJoe Perches			      "trailing statements should be on next line\n" . $herecurr);
3227a1080bf8SAndy Whitcroft		}
32280a920b5bSAndy Whitcroft
32290a920b5bSAndy Whitcroft		# Check for }<nl>else {, these must be at the same
32300a920b5bSAndy Whitcroft		# indent level to be relevant to each other.
32310a920b5bSAndy Whitcroft		if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
32320a920b5bSAndy Whitcroft						$previndent == $indent) {
3233000d1cc1SJoe Perches			ERROR("ELSE_AFTER_BRACE",
3234000d1cc1SJoe Perches			      "else should follow close brace '}'\n" . $hereprev);
32350a920b5bSAndy Whitcroft		}
32360a920b5bSAndy Whitcroft
3237c2fdda0dSAndy Whitcroft		if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and
3238c2fdda0dSAndy Whitcroft						$previndent == $indent) {
3239c2fdda0dSAndy Whitcroft			my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
3240c2fdda0dSAndy Whitcroft
3241c2fdda0dSAndy Whitcroft			# Find out what is on the end of the line after the
3242c2fdda0dSAndy Whitcroft			# conditional.
3243773647a0SAndy Whitcroft			substr($s, 0, length($c), '');
3244c2fdda0dSAndy Whitcroft			$s =~ s/\n.*//g;
3245c2fdda0dSAndy Whitcroft
3246c2fdda0dSAndy Whitcroft			if ($s =~ /^\s*;/) {
3247000d1cc1SJoe Perches				ERROR("WHILE_AFTER_BRACE",
3248000d1cc1SJoe Perches				      "while should follow close brace '}'\n" . $hereprev);
3249c2fdda0dSAndy Whitcroft			}
3250c2fdda0dSAndy Whitcroft		}
3251c2fdda0dSAndy Whitcroft
325295e2c602SJoe Perches#Specific variable tests
3253323c1260SJoe Perches		while ($line =~ m{($Constant|$Lval)}g) {
3254323c1260SJoe Perches			my $var = $1;
325595e2c602SJoe Perches
325695e2c602SJoe Perches#gcc binary extension
325795e2c602SJoe Perches			if ($var =~ /^$Binary$/) {
325895e2c602SJoe Perches				WARN("GCC_BINARY_CONSTANT",
325995e2c602SJoe Perches				     "Avoid gcc v4.3+ binary constant extension: <$var>\n" . $herecurr);
326095e2c602SJoe Perches			}
326195e2c602SJoe Perches
326295e2c602SJoe Perches#CamelCase
3263807bd26cSJoe Perches			if ($var !~ /^$Constant$/ &&
3264be79794bSJoe Perches			    $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
326522735ce8SJoe Perches#Ignore Page<foo> variants
3266807bd26cSJoe Perches			    $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
326722735ce8SJoe Perches#Ignore SI style variants like nS, mV and dB (ie: max_uV, regulator_min_uA_show)
32683445686aSJoe Perches			    $var !~ /^(?:[a-z_]*?)_?[a-z][A-Z](?:_[a-z_]+)?$/) {
32693445686aSJoe Perches				seed_camelcase_includes() if ($check);
32703445686aSJoe Perches				if (!defined $camelcase{$var}) {
3271323c1260SJoe Perches					$camelcase{$var} = 1;
3272be79794bSJoe Perches					CHK("CAMELCASE",
3273323c1260SJoe Perches					    "Avoid CamelCase: <$var>\n" . $herecurr);
3274323c1260SJoe Perches				}
3275323c1260SJoe Perches			}
32763445686aSJoe Perches		}
32770a920b5bSAndy Whitcroft
32780a920b5bSAndy Whitcroft#no spaces allowed after \ in define
3279c45dcabdSAndy Whitcroft		if ($line=~/\#\s*define.*\\\s$/) {
3280000d1cc1SJoe Perches			WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
3281000d1cc1SJoe Perches			     "Whitepspace after \\ makes next lines useless\n" . $herecurr);
32820a920b5bSAndy Whitcroft		}
32830a920b5bSAndy Whitcroft
3284653d4876SAndy Whitcroft#warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
3285c45dcabdSAndy Whitcroft		if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
3286e09dec48SAndy Whitcroft			my $file = "$1.h";
3287e09dec48SAndy Whitcroft			my $checkfile = "include/linux/$file";
3288e09dec48SAndy Whitcroft			if (-f "$root/$checkfile" &&
3289e09dec48SAndy Whitcroft			    $realfile ne $checkfile &&
32907840a94cSWolfram Sang			    $1 !~ /$allowed_asm_includes/)
3291c45dcabdSAndy Whitcroft			{
3292e09dec48SAndy Whitcroft				if ($realfile =~ m{^arch/}) {
3293000d1cc1SJoe Perches					CHK("ARCH_INCLUDE_LINUX",
3294000d1cc1SJoe Perches					    "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
3295e09dec48SAndy Whitcroft				} else {
3296000d1cc1SJoe Perches					WARN("INCLUDE_LINUX",
3297000d1cc1SJoe Perches					     "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
3298e09dec48SAndy Whitcroft				}
32990a920b5bSAndy Whitcroft			}
33000a920b5bSAndy Whitcroft		}
33010a920b5bSAndy Whitcroft
3302653d4876SAndy Whitcroft# multi-statement macros should be enclosed in a do while loop, grab the
3303653d4876SAndy Whitcroft# first statement and ensure its the whole macro if its not enclosed
3304cf655043SAndy Whitcroft# in a known good container
3305b8f96a31SAndy Whitcroft		if ($realfile !~ m@/vmlinux.lds.h$@ &&
3306b8f96a31SAndy Whitcroft		    $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
3307d8aaf121SAndy Whitcroft			my $ln = $linenr;
3308d8aaf121SAndy Whitcroft			my $cnt = $realcnt;
3309c45dcabdSAndy Whitcroft			my ($off, $dstat, $dcond, $rest);
3310c45dcabdSAndy Whitcroft			my $ctx = '';
3311c45dcabdSAndy Whitcroft			($dstat, $dcond, $ln, $cnt, $off) =
3312f74bd194SAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0);
3313f74bd194SAndy Whitcroft			$ctx = $dstat;
3314c45dcabdSAndy Whitcroft			#print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
3315a3bb97a7SAndy Whitcroft			#print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
3316c45dcabdSAndy Whitcroft
3317f74bd194SAndy Whitcroft			$dstat =~ s/^.\s*\#\s*define\s+$Ident(?:\([^\)]*\))?\s*//;
3318292f1a9bSAndy Whitcroft			$dstat =~ s/$;//g;
3319c45dcabdSAndy Whitcroft			$dstat =~ s/\\\n.//g;
3320c45dcabdSAndy Whitcroft			$dstat =~ s/^\s*//s;
3321c45dcabdSAndy Whitcroft			$dstat =~ s/\s*$//s;
3322c45dcabdSAndy Whitcroft
3323c45dcabdSAndy Whitcroft			# Flatten any parentheses and braces
3324bf30d6edSAndy Whitcroft			while ($dstat =~ s/\([^\(\)]*\)/1/ ||
3325bf30d6edSAndy Whitcroft			       $dstat =~ s/\{[^\{\}]*\}/1/ ||
3326c81769fdSAndy Whitcroft			       $dstat =~ s/\[[^\[\]]*\]/1/)
3327bf30d6edSAndy Whitcroft			{
3328c45dcabdSAndy Whitcroft			}
3329c45dcabdSAndy Whitcroft
3330e45bab8eSAndy Whitcroft			# Flatten any obvious string concatentation.
3331e45bab8eSAndy Whitcroft			while ($dstat =~ s/("X*")\s*$Ident/$1/ ||
3332e45bab8eSAndy Whitcroft			       $dstat =~ s/$Ident\s*("X*")/$1/)
3333e45bab8eSAndy Whitcroft			{
3334e45bab8eSAndy Whitcroft			}
3335e45bab8eSAndy Whitcroft
3336c45dcabdSAndy Whitcroft			my $exceptions = qr{
3337c45dcabdSAndy Whitcroft				$Declare|
3338c45dcabdSAndy Whitcroft				module_param_named|
3339a0a0a7a9SKees Cook				MODULE_PARM_DESC|
3340c45dcabdSAndy Whitcroft				DECLARE_PER_CPU|
3341c45dcabdSAndy Whitcroft				DEFINE_PER_CPU|
3342383099fdSAndy Whitcroft				__typeof__\(|
334322fd2d3eSStefani Seibold				union|
334422fd2d3eSStefani Seibold				struct|
3345ea71a0a0SAndy Whitcroft				\.$Ident\s*=\s*|
3346ea71a0a0SAndy Whitcroft				^\"|\"$
3347c45dcabdSAndy Whitcroft			}x;
33485eaa20b9SAndy Whitcroft			#print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
3349f74bd194SAndy Whitcroft			if ($dstat ne '' &&
3350f74bd194SAndy Whitcroft			    $dstat !~ /^(?:$Ident|-?$Constant),$/ &&			# 10, // foo(),
3351f74bd194SAndy Whitcroft			    $dstat !~ /^(?:$Ident|-?$Constant);$/ &&			# foo();
33523cc4b1c3SJoe Perches			    $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ &&		# 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
3353b9df76acSAndy Whitcroft			    $dstat !~ /^'X'$/ &&					# character constants
3354f74bd194SAndy Whitcroft			    $dstat !~ /$exceptions/ &&
3355f74bd194SAndy Whitcroft			    $dstat !~ /^\.$Ident\s*=/ &&				# .foo =
3356e942e2c3SJoe Perches			    $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ &&		# stringification #foo
335772f115f9SAndy Whitcroft			    $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ &&	# do {...} while (...); // do {...} while (...)
3358f74bd194SAndy Whitcroft			    $dstat !~ /^for\s*$Constant$/ &&				# for (...)
3359f74bd194SAndy Whitcroft			    $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ &&	# for (...) bar()
3360f74bd194SAndy Whitcroft			    $dstat !~ /^do\s*{/ &&					# do {...
3361f74bd194SAndy Whitcroft			    $dstat !~ /^\({/)						# ({...
3362c45dcabdSAndy Whitcroft			{
3363f74bd194SAndy Whitcroft				$ctx =~ s/\n*$//;
3364f74bd194SAndy Whitcroft				my $herectx = $here . "\n";
3365f74bd194SAndy Whitcroft				my $cnt = statement_rawlines($ctx);
3366f74bd194SAndy Whitcroft
3367f74bd194SAndy Whitcroft				for (my $n = 0; $n < $cnt; $n++) {
3368f74bd194SAndy Whitcroft					$herectx .= raw_line($linenr, $n) . "\n";
3369c45dcabdSAndy Whitcroft				}
3370c45dcabdSAndy Whitcroft
3371f74bd194SAndy Whitcroft				if ($dstat =~ /;/) {
3372f74bd194SAndy Whitcroft					ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
3373f74bd194SAndy Whitcroft					      "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
3374f74bd194SAndy Whitcroft				} else {
3375000d1cc1SJoe Perches					ERROR("COMPLEX_MACRO",
3376f74bd194SAndy Whitcroft					      "Macros with complex values should be enclosed in parenthesis\n" . "$herectx");
3377d8aaf121SAndy Whitcroft				}
33780a920b5bSAndy Whitcroft			}
33795023d347SJoe Perches
3380481eb486SJoe Perches# check for line continuations outside of #defines, preprocessor #, and asm
33815023d347SJoe Perches
33825023d347SJoe Perches		} else {
33835023d347SJoe Perches			if ($prevline !~ /^..*\\$/ &&
3384481eb486SJoe Perches			    $line !~ /^\+\s*\#.*\\$/ &&		# preprocessor
3385481eb486SJoe Perches			    $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ &&	# asm
33865023d347SJoe Perches			    $line =~ /^\+.*\\$/) {
33875023d347SJoe Perches				WARN("LINE_CONTINUATIONS",
33885023d347SJoe Perches				     "Avoid unnecessary line continuations\n" . $herecurr);
33895023d347SJoe Perches			}
3390653d4876SAndy Whitcroft		}
33910a920b5bSAndy Whitcroft
3392b13edf7fSJoe Perches# do {} while (0) macro tests:
3393b13edf7fSJoe Perches# single-statement macros do not need to be enclosed in do while (0) loop,
3394b13edf7fSJoe Perches# macro should not end with a semicolon
3395b13edf7fSJoe Perches		if ($^V && $^V ge 5.10.0 &&
3396b13edf7fSJoe Perches		    $realfile !~ m@/vmlinux.lds.h$@ &&
3397b13edf7fSJoe Perches		    $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
3398b13edf7fSJoe Perches			my $ln = $linenr;
3399b13edf7fSJoe Perches			my $cnt = $realcnt;
3400b13edf7fSJoe Perches			my ($off, $dstat, $dcond, $rest);
3401b13edf7fSJoe Perches			my $ctx = '';
3402b13edf7fSJoe Perches			($dstat, $dcond, $ln, $cnt, $off) =
3403b13edf7fSJoe Perches				ctx_statement_block($linenr, $realcnt, 0);
3404b13edf7fSJoe Perches			$ctx = $dstat;
3405b13edf7fSJoe Perches
3406b13edf7fSJoe Perches			$dstat =~ s/\\\n.//g;
3407b13edf7fSJoe Perches
3408b13edf7fSJoe Perches			if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
3409b13edf7fSJoe Perches				my $stmts = $2;
3410b13edf7fSJoe Perches				my $semis = $3;
3411b13edf7fSJoe Perches
3412b13edf7fSJoe Perches				$ctx =~ s/\n*$//;
3413b13edf7fSJoe Perches				my $cnt = statement_rawlines($ctx);
3414b13edf7fSJoe Perches				my $herectx = $here . "\n";
3415b13edf7fSJoe Perches
3416b13edf7fSJoe Perches				for (my $n = 0; $n < $cnt; $n++) {
3417b13edf7fSJoe Perches					$herectx .= raw_line($linenr, $n) . "\n";
3418b13edf7fSJoe Perches				}
3419b13edf7fSJoe Perches
3420ac8e97f8SJoe Perches				if (($stmts =~ tr/;/;/) == 1 &&
3421ac8e97f8SJoe Perches				    $stmts !~ /^\s*(if|while|for|switch)\b/) {
3422b13edf7fSJoe Perches					WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
3423b13edf7fSJoe Perches					     "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
3424b13edf7fSJoe Perches				}
3425b13edf7fSJoe Perches				if (defined $semis && $semis ne "") {
3426b13edf7fSJoe Perches					WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
3427b13edf7fSJoe Perches					     "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
3428b13edf7fSJoe Perches				}
3429b13edf7fSJoe Perches			}
3430b13edf7fSJoe Perches		}
3431b13edf7fSJoe Perches
3432080ba929SMike Frysinger# make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
3433080ba929SMike Frysinger# all assignments may have only one of the following with an assignment:
3434080ba929SMike Frysinger#	.
3435080ba929SMike Frysinger#	ALIGN(...)
3436080ba929SMike Frysinger#	VMLINUX_SYMBOL(...)
3437080ba929SMike Frysinger		if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
3438000d1cc1SJoe Perches			WARN("MISSING_VMLINUX_SYMBOL",
3439000d1cc1SJoe Perches			     "vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
3440080ba929SMike Frysinger		}
3441080ba929SMike Frysinger
3442f0a594c1SAndy Whitcroft# check for redundant bracing round if etc
344313214adfSAndy Whitcroft		if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
344413214adfSAndy Whitcroft			my ($level, $endln, @chunks) =
3445cf655043SAndy Whitcroft				ctx_statement_full($linenr, $realcnt, 1);
344613214adfSAndy Whitcroft			#print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
3447cf655043SAndy Whitcroft			#print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
3448cf655043SAndy Whitcroft			if ($#chunks > 0 && $level == 0) {
3449aad4f614SJoe Perches				my @allowed = ();
3450aad4f614SJoe Perches				my $allow = 0;
345113214adfSAndy Whitcroft				my $seen = 0;
3452773647a0SAndy Whitcroft				my $herectx = $here . "\n";
3453cf655043SAndy Whitcroft				my $ln = $linenr - 1;
345413214adfSAndy Whitcroft				for my $chunk (@chunks) {
345513214adfSAndy Whitcroft					my ($cond, $block) = @{$chunk};
345613214adfSAndy Whitcroft
3457773647a0SAndy Whitcroft					# If the condition carries leading newlines, then count those as offsets.
3458773647a0SAndy Whitcroft					my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
3459773647a0SAndy Whitcroft					my $offset = statement_rawlines($whitespace) - 1;
3460773647a0SAndy Whitcroft
3461aad4f614SJoe Perches					$allowed[$allow] = 0;
3462773647a0SAndy Whitcroft					#print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
3463773647a0SAndy Whitcroft
3464773647a0SAndy Whitcroft					# We have looked at and allowed this specific line.
3465773647a0SAndy Whitcroft					$suppress_ifbraces{$ln + $offset} = 1;
3466773647a0SAndy Whitcroft
3467773647a0SAndy Whitcroft					$herectx .= "$rawlines[$ln + $offset]\n[...]\n";
3468cf655043SAndy Whitcroft					$ln += statement_rawlines($block) - 1;
3469cf655043SAndy Whitcroft
3470773647a0SAndy Whitcroft					substr($block, 0, length($cond), '');
347113214adfSAndy Whitcroft
347213214adfSAndy Whitcroft					$seen++ if ($block =~ /^\s*{/);
347313214adfSAndy Whitcroft
3474aad4f614SJoe Perches					#print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
3475cf655043SAndy Whitcroft					if (statement_lines($cond) > 1) {
3476cf655043SAndy Whitcroft						#print "APW: ALLOWED: cond<$cond>\n";
3477aad4f614SJoe Perches						$allowed[$allow] = 1;
347813214adfSAndy Whitcroft					}
347913214adfSAndy Whitcroft					if ($block =~/\b(?:if|for|while)\b/) {
3480cf655043SAndy Whitcroft						#print "APW: ALLOWED: block<$block>\n";
3481aad4f614SJoe Perches						$allowed[$allow] = 1;
348213214adfSAndy Whitcroft					}
3483cf655043SAndy Whitcroft					if (statement_block_size($block) > 1) {
3484cf655043SAndy Whitcroft						#print "APW: ALLOWED: lines block<$block>\n";
3485aad4f614SJoe Perches						$allowed[$allow] = 1;
348613214adfSAndy Whitcroft					}
3487aad4f614SJoe Perches					$allow++;
348813214adfSAndy Whitcroft				}
3489aad4f614SJoe Perches				if ($seen) {
3490aad4f614SJoe Perches					my $sum_allowed = 0;
3491aad4f614SJoe Perches					foreach (@allowed) {
3492aad4f614SJoe Perches						$sum_allowed += $_;
3493aad4f614SJoe Perches					}
3494aad4f614SJoe Perches					if ($sum_allowed == 0) {
3495000d1cc1SJoe Perches						WARN("BRACES",
3496000d1cc1SJoe Perches						     "braces {} are not necessary for any arm of this statement\n" . $herectx);
3497aad4f614SJoe Perches					} elsif ($sum_allowed != $allow &&
3498aad4f614SJoe Perches						 $seen != $allow) {
3499aad4f614SJoe Perches						CHK("BRACES",
3500aad4f614SJoe Perches						    "braces {} should be used on all arms of this statement\n" . $herectx);
3501aad4f614SJoe Perches					}
350213214adfSAndy Whitcroft				}
350313214adfSAndy Whitcroft			}
350413214adfSAndy Whitcroft		}
3505773647a0SAndy Whitcroft		if (!defined $suppress_ifbraces{$linenr - 1} &&
350613214adfSAndy Whitcroft					$line =~ /\b(if|while|for|else)\b/) {
3507cf655043SAndy Whitcroft			my $allowed = 0;
3508f0a594c1SAndy Whitcroft
3509cf655043SAndy Whitcroft			# Check the pre-context.
3510cf655043SAndy Whitcroft			if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
3511cf655043SAndy Whitcroft				#print "APW: ALLOWED: pre<$1>\n";
3512cf655043SAndy Whitcroft				$allowed = 1;
3513f0a594c1SAndy Whitcroft			}
3514773647a0SAndy Whitcroft
3515773647a0SAndy Whitcroft			my ($level, $endln, @chunks) =
3516773647a0SAndy Whitcroft				ctx_statement_full($linenr, $realcnt, $-[0]);
3517773647a0SAndy Whitcroft
3518cf655043SAndy Whitcroft			# Check the condition.
3519cf655043SAndy Whitcroft			my ($cond, $block) = @{$chunks[0]};
3520773647a0SAndy Whitcroft			#print "CHECKING<$linenr> cond<$cond> block<$block>\n";
3521cf655043SAndy Whitcroft			if (defined $cond) {
3522773647a0SAndy Whitcroft				substr($block, 0, length($cond), '');
3523cf655043SAndy Whitcroft			}
3524cf655043SAndy Whitcroft			if (statement_lines($cond) > 1) {
3525cf655043SAndy Whitcroft				#print "APW: ALLOWED: cond<$cond>\n";
3526cf655043SAndy Whitcroft				$allowed = 1;
3527cf655043SAndy Whitcroft			}
3528cf655043SAndy Whitcroft			if ($block =~/\b(?:if|for|while)\b/) {
3529cf655043SAndy Whitcroft				#print "APW: ALLOWED: block<$block>\n";
3530cf655043SAndy Whitcroft				$allowed = 1;
3531cf655043SAndy Whitcroft			}
3532cf655043SAndy Whitcroft			if (statement_block_size($block) > 1) {
3533cf655043SAndy Whitcroft				#print "APW: ALLOWED: lines block<$block>\n";
3534cf655043SAndy Whitcroft				$allowed = 1;
3535cf655043SAndy Whitcroft			}
3536cf655043SAndy Whitcroft			# Check the post-context.
3537cf655043SAndy Whitcroft			if (defined $chunks[1]) {
3538cf655043SAndy Whitcroft				my ($cond, $block) = @{$chunks[1]};
3539cf655043SAndy Whitcroft				if (defined $cond) {
3540773647a0SAndy Whitcroft					substr($block, 0, length($cond), '');
3541cf655043SAndy Whitcroft				}
3542cf655043SAndy Whitcroft				if ($block =~ /^\s*\{/) {
3543cf655043SAndy Whitcroft					#print "APW: ALLOWED: chunk-1 block<$block>\n";
3544cf655043SAndy Whitcroft					$allowed = 1;
3545cf655043SAndy Whitcroft				}
3546cf655043SAndy Whitcroft			}
3547cf655043SAndy Whitcroft			if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
354869932487SJustin P. Mattock				my $herectx = $here . "\n";
3549f055663cSAndy Whitcroft				my $cnt = statement_rawlines($block);
3550cf655043SAndy Whitcroft
3551f055663cSAndy Whitcroft				for (my $n = 0; $n < $cnt; $n++) {
355269932487SJustin P. Mattock					$herectx .= raw_line($linenr, $n) . "\n";
3553cf655043SAndy Whitcroft				}
3554cf655043SAndy Whitcroft
3555000d1cc1SJoe Perches				WARN("BRACES",
3556000d1cc1SJoe Perches				     "braces {} are not necessary for single statement blocks\n" . $herectx);
3557f0a594c1SAndy Whitcroft			}
3558f0a594c1SAndy Whitcroft		}
3559f0a594c1SAndy Whitcroft
35600979ae66SJoe Perches# check for unnecessary blank lines around braces
356177b9a53aSJoe Perches		if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
35620979ae66SJoe Perches			CHK("BRACES",
35630979ae66SJoe Perches			    "Blank lines aren't necessary before a close brace '}'\n" . $hereprev);
35640979ae66SJoe Perches		}
356577b9a53aSJoe Perches		if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
35660979ae66SJoe Perches			CHK("BRACES",
35670979ae66SJoe Perches			    "Blank lines aren't necessary after an open brace '{'\n" . $hereprev);
35680979ae66SJoe Perches		}
35690979ae66SJoe Perches
35704a0df2efSAndy Whitcroft# no volatiles please
35716c72ffaaSAndy Whitcroft		my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
35726c72ffaaSAndy Whitcroft		if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
3573000d1cc1SJoe Perches			WARN("VOLATILE",
3574000d1cc1SJoe Perches			     "Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
35754a0df2efSAndy Whitcroft		}
35764a0df2efSAndy Whitcroft
357700df344fSAndy Whitcroft# warn about #if 0
3578c45dcabdSAndy Whitcroft		if ($line =~ /^.\s*\#\s*if\s+0\b/) {
3579000d1cc1SJoe Perches			CHK("REDUNDANT_CODE",
3580000d1cc1SJoe Perches			    "if this code is redundant consider removing it\n" .
3581de7d4f0eSAndy Whitcroft				$herecurr);
35824a0df2efSAndy Whitcroft		}
35834a0df2efSAndy Whitcroft
358403df4b51SAndy Whitcroft# check for needless "if (<foo>) fn(<foo>)" uses
358503df4b51SAndy Whitcroft		if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
358603df4b51SAndy Whitcroft			my $expr = '\s*\(\s*' . quotemeta($1) . '\s*\)\s*;';
358703df4b51SAndy Whitcroft			if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?)$expr/) {
358803df4b51SAndy Whitcroft				WARN('NEEDLESS_IF',
358903df4b51SAndy Whitcroft				     "$1(NULL) is safe this check is probably not required\n" . $hereprev);
35904c432a8fSGreg Kroah-Hartman			}
35914c432a8fSGreg Kroah-Hartman		}
3592f0a594c1SAndy Whitcroft
35931a15a250SPatrick Pannuto# prefer usleep_range over udelay
359437581c28SBruce Allan		if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
35951a15a250SPatrick Pannuto			# ignore udelay's < 10, however
359637581c28SBruce Allan			if (! ($1 < 10) ) {
3597000d1cc1SJoe Perches				CHK("USLEEP_RANGE",
3598000d1cc1SJoe Perches				    "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $line);
35991a15a250SPatrick Pannuto			}
36001a15a250SPatrick Pannuto		}
36011a15a250SPatrick Pannuto
360209ef8725SPatrick Pannuto# warn about unexpectedly long msleep's
360309ef8725SPatrick Pannuto		if ($line =~ /\bmsleep\s*\((\d+)\);/) {
360409ef8725SPatrick Pannuto			if ($1 < 20) {
3605000d1cc1SJoe Perches				WARN("MSLEEP",
3606000d1cc1SJoe Perches				     "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $line);
360709ef8725SPatrick Pannuto			}
360809ef8725SPatrick Pannuto		}
360909ef8725SPatrick Pannuto
361036ec1939SJoe Perches# check for comparisons of jiffies
361136ec1939SJoe Perches		if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
361236ec1939SJoe Perches			WARN("JIFFIES_COMPARISON",
361336ec1939SJoe Perches			     "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
361436ec1939SJoe Perches		}
361536ec1939SJoe Perches
36169d7a34a5SJoe Perches# check for comparisons of get_jiffies_64()
36179d7a34a5SJoe Perches		if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
36189d7a34a5SJoe Perches			WARN("JIFFIES_COMPARISON",
36199d7a34a5SJoe Perches			     "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
36209d7a34a5SJoe Perches		}
36219d7a34a5SJoe Perches
362200df344fSAndy Whitcroft# warn about #ifdefs in C files
3623c45dcabdSAndy Whitcroft#		if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
362400df344fSAndy Whitcroft#			print "#ifdef in C files should be avoided\n";
362500df344fSAndy Whitcroft#			print "$herecurr";
362600df344fSAndy Whitcroft#			$clean = 0;
362700df344fSAndy Whitcroft#		}
362800df344fSAndy Whitcroft
362922f2a2efSAndy Whitcroft# warn about spacing in #ifdefs
3630c45dcabdSAndy Whitcroft		if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
36313705ce5bSJoe Perches			if (ERROR("SPACING",
36323705ce5bSJoe Perches				  "exactly one space required after that #$1\n" . $herecurr) &&
36333705ce5bSJoe Perches			    $fix) {
36343705ce5bSJoe Perches				$fixed[$linenr - 1] =~
36353705ce5bSJoe Perches				    s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
36363705ce5bSJoe Perches			}
36373705ce5bSJoe Perches
363822f2a2efSAndy Whitcroft		}
363922f2a2efSAndy Whitcroft
36404a0df2efSAndy Whitcroft# check for spinlock_t definitions without a comment.
3641171ae1a4SAndy Whitcroft		if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
3642171ae1a4SAndy Whitcroft		    $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
36434a0df2efSAndy Whitcroft			my $which = $1;
36444a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
3645000d1cc1SJoe Perches				CHK("UNCOMMENTED_DEFINITION",
3646000d1cc1SJoe Perches				    "$1 definition without comment\n" . $herecurr);
36474a0df2efSAndy Whitcroft			}
36484a0df2efSAndy Whitcroft		}
36494a0df2efSAndy Whitcroft# check for memory barriers without a comment.
36504a0df2efSAndy Whitcroft		if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
36514a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
3652000d1cc1SJoe Perches				CHK("MEMORY_BARRIER",
3653000d1cc1SJoe Perches				    "memory barrier without comment\n" . $herecurr);
36544a0df2efSAndy Whitcroft			}
36554a0df2efSAndy Whitcroft		}
36564a0df2efSAndy Whitcroft# check of hardware specific defines
3657c45dcabdSAndy Whitcroft		if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
3658000d1cc1SJoe Perches			CHK("ARCH_DEFINES",
3659000d1cc1SJoe Perches			    "architecture specific defines should be avoided\n" .  $herecurr);
36600a920b5bSAndy Whitcroft		}
3661653d4876SAndy Whitcroft
3662d4977c78STobias Klauser# Check that the storage class is at the beginning of a declaration
3663d4977c78STobias Klauser		if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
3664000d1cc1SJoe Perches			WARN("STORAGE_CLASS",
3665000d1cc1SJoe Perches			     "storage class should be at the beginning of the declaration\n" . $herecurr)
3666d4977c78STobias Klauser		}
3667d4977c78STobias Klauser
3668de7d4f0eSAndy Whitcroft# check the location of the inline attribute, that it is between
3669de7d4f0eSAndy Whitcroft# storage class and type.
36709c0ca6f9SAndy Whitcroft		if ($line =~ /\b$Type\s+$Inline\b/ ||
36719c0ca6f9SAndy Whitcroft		    $line =~ /\b$Inline\s+$Storage\b/) {
3672000d1cc1SJoe Perches			ERROR("INLINE_LOCATION",
3673000d1cc1SJoe Perches			      "inline keyword should sit between storage class and type\n" . $herecurr);
3674de7d4f0eSAndy Whitcroft		}
3675de7d4f0eSAndy Whitcroft
36768905a67cSAndy Whitcroft# Check for __inline__ and __inline, prefer inline
36778905a67cSAndy Whitcroft		if ($line =~ /\b(__inline__|__inline)\b/) {
3678000d1cc1SJoe Perches			WARN("INLINE",
3679000d1cc1SJoe Perches			     "plain inline is preferred over $1\n" . $herecurr);
36808905a67cSAndy Whitcroft		}
36818905a67cSAndy Whitcroft
36823d130fd0SJoe Perches# Check for __attribute__ packed, prefer __packed
36833d130fd0SJoe Perches		if ($line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
3684000d1cc1SJoe Perches			WARN("PREFER_PACKED",
3685000d1cc1SJoe Perches			     "__packed is preferred over __attribute__((packed))\n" . $herecurr);
36863d130fd0SJoe Perches		}
36873d130fd0SJoe Perches
368839b7e287SJoe Perches# Check for __attribute__ aligned, prefer __aligned
368939b7e287SJoe Perches		if ($line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
3690000d1cc1SJoe Perches			WARN("PREFER_ALIGNED",
3691000d1cc1SJoe Perches			     "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
369239b7e287SJoe Perches		}
369339b7e287SJoe Perches
36945f14d3bdSJoe Perches# Check for __attribute__ format(printf, prefer __printf
36955f14d3bdSJoe Perches		if ($line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
36965f14d3bdSJoe Perches			WARN("PREFER_PRINTF",
36975f14d3bdSJoe Perches			     "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr);
36985f14d3bdSJoe Perches		}
36995f14d3bdSJoe Perches
37006061d949SJoe Perches# Check for __attribute__ format(scanf, prefer __scanf
37016061d949SJoe Perches		if ($line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
37026061d949SJoe Perches			WARN("PREFER_SCANF",
37036061d949SJoe Perches			     "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr);
37046061d949SJoe Perches		}
37056061d949SJoe Perches
37068f53a9b8SJoe Perches# check for sizeof(&)
37078f53a9b8SJoe Perches		if ($line =~ /\bsizeof\s*\(\s*\&/) {
3708000d1cc1SJoe Perches			WARN("SIZEOF_ADDRESS",
3709000d1cc1SJoe Perches			     "sizeof(& should be avoided\n" . $herecurr);
37108f53a9b8SJoe Perches		}
37118f53a9b8SJoe Perches
371266c80b60SJoe Perches# check for sizeof without parenthesis
371366c80b60SJoe Perches		if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
371466c80b60SJoe Perches			WARN("SIZEOF_PARENTHESIS",
371566c80b60SJoe Perches			     "sizeof $1 should be sizeof($1)\n" . $herecurr);
371666c80b60SJoe Perches		}
371766c80b60SJoe Perches
3718428e2fdcSJoe Perches# check for line continuations in quoted strings with odd counts of "
3719428e2fdcSJoe Perches		if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) {
3720000d1cc1SJoe Perches			WARN("LINE_CONTINUATIONS",
3721000d1cc1SJoe Perches			     "Avoid line continuations in quoted strings\n" . $herecurr);
3722428e2fdcSJoe Perches		}
3723428e2fdcSJoe Perches
372488982feaSJoe Perches# check for struct spinlock declarations
372588982feaSJoe Perches		if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
372688982feaSJoe Perches			WARN("USE_SPINLOCK_T",
372788982feaSJoe Perches			     "struct spinlock should be spinlock_t\n" . $herecurr);
372888982feaSJoe Perches		}
372988982feaSJoe Perches
3730a6962d72SJoe Perches# check for seq_printf uses that could be seq_puts
3731a6962d72SJoe Perches		if ($line =~ /\bseq_printf\s*\(/) {
3732a6962d72SJoe Perches			my $fmt = get_quoted_string($line, $rawline);
3733a6962d72SJoe Perches			if ($fmt !~ /[^\\]\%/) {
3734a6962d72SJoe Perches				WARN("PREFER_SEQ_PUTS",
3735a6962d72SJoe Perches				     "Prefer seq_puts to seq_printf\n" . $herecurr);
3736a6962d72SJoe Perches			}
3737a6962d72SJoe Perches		}
3738a6962d72SJoe Perches
3739554e165cSAndy Whitcroft# Check for misused memsets
3740d1fe9c09SJoe Perches		if ($^V && $^V ge 5.10.0 &&
3741d1fe9c09SJoe Perches		    defined $stat &&
3742d7c76ba7SJoe Perches		    $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/s) {
3743554e165cSAndy Whitcroft
3744d7c76ba7SJoe Perches			my $ms_addr = $2;
3745d1fe9c09SJoe Perches			my $ms_val = $7;
3746d1fe9c09SJoe Perches			my $ms_size = $12;
3747d7c76ba7SJoe Perches
3748554e165cSAndy Whitcroft			if ($ms_size =~ /^(0x|)0$/i) {
3749554e165cSAndy Whitcroft				ERROR("MEMSET",
3750d7c76ba7SJoe Perches				      "memset to 0's uses 0 as the 2nd argument, not the 3rd\n" . "$here\n$stat\n");
3751554e165cSAndy Whitcroft			} elsif ($ms_size =~ /^(0x|)1$/i) {
3752554e165cSAndy Whitcroft				WARN("MEMSET",
3753d7c76ba7SJoe Perches				     "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
3754d7c76ba7SJoe Perches			}
3755d7c76ba7SJoe Perches		}
3756d7c76ba7SJoe Perches
3757d7c76ba7SJoe Perches# typecasts on min/max could be min_t/max_t
3758d1fe9c09SJoe Perches		if ($^V && $^V ge 5.10.0 &&
3759d1fe9c09SJoe Perches		    defined $stat &&
3760d7c76ba7SJoe Perches		    $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
3761d1fe9c09SJoe Perches			if (defined $2 || defined $7) {
3762d7c76ba7SJoe Perches				my $call = $1;
3763d7c76ba7SJoe Perches				my $cast1 = deparenthesize($2);
3764d7c76ba7SJoe Perches				my $arg1 = $3;
3765d1fe9c09SJoe Perches				my $cast2 = deparenthesize($7);
3766d1fe9c09SJoe Perches				my $arg2 = $8;
3767d7c76ba7SJoe Perches				my $cast;
3768d7c76ba7SJoe Perches
3769d1fe9c09SJoe Perches				if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
3770d7c76ba7SJoe Perches					$cast = "$cast1 or $cast2";
3771d7c76ba7SJoe Perches				} elsif ($cast1 ne "") {
3772d7c76ba7SJoe Perches					$cast = $cast1;
3773d7c76ba7SJoe Perches				} else {
3774d7c76ba7SJoe Perches					$cast = $cast2;
3775d7c76ba7SJoe Perches				}
3776d7c76ba7SJoe Perches				WARN("MINMAX",
3777d7c76ba7SJoe Perches				     "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
3778554e165cSAndy Whitcroft			}
3779554e165cSAndy Whitcroft		}
3780554e165cSAndy Whitcroft
37814a273195SJoe Perches# check usleep_range arguments
37824a273195SJoe Perches		if ($^V && $^V ge 5.10.0 &&
37834a273195SJoe Perches		    defined $stat &&
37844a273195SJoe Perches		    $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
37854a273195SJoe Perches			my $min = $1;
37864a273195SJoe Perches			my $max = $7;
37874a273195SJoe Perches			if ($min eq $max) {
37884a273195SJoe Perches				WARN("USLEEP_RANGE",
37894a273195SJoe Perches				     "usleep_range should not use min == max args; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
37904a273195SJoe Perches			} elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
37914a273195SJoe Perches				 $min > $max) {
37924a273195SJoe Perches				WARN("USLEEP_RANGE",
37934a273195SJoe Perches				     "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
37944a273195SJoe Perches			}
37954a273195SJoe Perches		}
37964a273195SJoe Perches
3797de7d4f0eSAndy Whitcroft# check for new externs in .c files.
3798171ae1a4SAndy Whitcroft		if ($realfile =~ /\.c$/ && defined $stat &&
3799c45dcabdSAndy Whitcroft		    $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
3800171ae1a4SAndy Whitcroft		{
3801c45dcabdSAndy Whitcroft			my $function_name = $1;
3802c45dcabdSAndy Whitcroft			my $paren_space = $2;
3803171ae1a4SAndy Whitcroft
3804171ae1a4SAndy Whitcroft			my $s = $stat;
3805171ae1a4SAndy Whitcroft			if (defined $cond) {
3806171ae1a4SAndy Whitcroft				substr($s, 0, length($cond), '');
3807171ae1a4SAndy Whitcroft			}
3808c45dcabdSAndy Whitcroft			if ($s =~ /^\s*;/ &&
3809c45dcabdSAndy Whitcroft			    $function_name ne 'uninitialized_var')
3810c45dcabdSAndy Whitcroft			{
3811000d1cc1SJoe Perches				WARN("AVOID_EXTERNS",
3812000d1cc1SJoe Perches				     "externs should be avoided in .c files\n" .  $herecurr);
3813de7d4f0eSAndy Whitcroft			}
3814de7d4f0eSAndy Whitcroft
3815171ae1a4SAndy Whitcroft			if ($paren_space =~ /\n/) {
3816000d1cc1SJoe Perches				WARN("FUNCTION_ARGUMENTS",
3817000d1cc1SJoe Perches				     "arguments for function declarations should follow identifier\n" . $herecurr);
3818171ae1a4SAndy Whitcroft			}
38199c9ba34eSAndy Whitcroft
38209c9ba34eSAndy Whitcroft		} elsif ($realfile =~ /\.c$/ && defined $stat &&
38219c9ba34eSAndy Whitcroft		    $stat =~ /^.\s*extern\s+/)
38229c9ba34eSAndy Whitcroft		{
3823000d1cc1SJoe Perches			WARN("AVOID_EXTERNS",
3824000d1cc1SJoe Perches			     "externs should be avoided in .c files\n" .  $herecurr);
3825171ae1a4SAndy Whitcroft		}
3826171ae1a4SAndy Whitcroft
3827de7d4f0eSAndy Whitcroft# checks for new __setup's
3828de7d4f0eSAndy Whitcroft		if ($rawline =~ /\b__setup\("([^"]*)"/) {
3829de7d4f0eSAndy Whitcroft			my $name = $1;
3830de7d4f0eSAndy Whitcroft
3831de7d4f0eSAndy Whitcroft			if (!grep(/$name/, @setup_docs)) {
3832000d1cc1SJoe Perches				CHK("UNDOCUMENTED_SETUP",
3833000d1cc1SJoe Perches				    "__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
3834de7d4f0eSAndy Whitcroft			}
3835653d4876SAndy Whitcroft		}
38369c0ca6f9SAndy Whitcroft
38379c0ca6f9SAndy Whitcroft# check for pointless casting of kmalloc return
3838caf2a54fSJoe Perches		if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) {
3839000d1cc1SJoe Perches			WARN("UNNECESSARY_CASTS",
3840000d1cc1SJoe Perches			     "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
38419c0ca6f9SAndy Whitcroft		}
384213214adfSAndy Whitcroft
3843a640d25cSJoe Perches# alloc style
3844a640d25cSJoe Perches# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
3845a640d25cSJoe Perches		if ($^V && $^V ge 5.10.0 &&
3846a640d25cSJoe Perches		    $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*([kv][mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
3847a640d25cSJoe Perches			CHK("ALLOC_SIZEOF_STRUCT",
3848a640d25cSJoe Perches			    "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
3849a640d25cSJoe Perches		}
3850a640d25cSJoe Perches
3851972fdea2SJoe Perches# check for krealloc arg reuse
3852972fdea2SJoe Perches		if ($^V && $^V ge 5.10.0 &&
3853972fdea2SJoe Perches		    $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*\1\s*,/) {
3854972fdea2SJoe Perches			WARN("KREALLOC_ARG_REUSE",
3855972fdea2SJoe Perches			     "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
3856972fdea2SJoe Perches		}
3857972fdea2SJoe Perches
38585ce59ae0SJoe Perches# check for alloc argument mismatch
38595ce59ae0SJoe Perches		if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
38605ce59ae0SJoe Perches			WARN("ALLOC_ARRAY_ARGS",
38615ce59ae0SJoe Perches			     "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
38625ce59ae0SJoe Perches		}
38635ce59ae0SJoe Perches
3864caf2a54fSJoe Perches# check for multiple semicolons
3865caf2a54fSJoe Perches		if ($line =~ /;\s*;\s*$/) {
3866000d1cc1SJoe Perches			WARN("ONE_SEMICOLON",
3867000d1cc1SJoe Perches			     "Statements terminations use 1 semicolon\n" . $herecurr);
3868d1e2ad07SJoe Perches		}
3869d1e2ad07SJoe Perches
3870d1e2ad07SJoe Perches# check for switch/default statements without a break;
3871d1e2ad07SJoe Perches		if ($^V && $^V ge 5.10.0 &&
3872d1e2ad07SJoe Perches		    defined $stat &&
3873d1e2ad07SJoe Perches		    $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
3874d1e2ad07SJoe Perches			my $ctx = '';
3875d1e2ad07SJoe Perches			my $herectx = $here . "\n";
3876d1e2ad07SJoe Perches			my $cnt = statement_rawlines($stat);
3877d1e2ad07SJoe Perches			for (my $n = 0; $n < $cnt; $n++) {
3878d1e2ad07SJoe Perches				$herectx .= raw_line($linenr, $n) . "\n";
3879d1e2ad07SJoe Perches			}
3880d1e2ad07SJoe Perches			WARN("DEFAULT_NO_BREAK",
3881d1e2ad07SJoe Perches			     "switch default: should use break\n" . $herectx);
3882caf2a54fSJoe Perches		}
3883caf2a54fSJoe Perches
388413214adfSAndy Whitcroft# check for gcc specific __FUNCTION__
388513214adfSAndy Whitcroft		if ($line =~ /__FUNCTION__/) {
3886000d1cc1SJoe Perches			WARN("USE_FUNC",
3887000d1cc1SJoe Perches			     "__func__ should be used instead of gcc specific __FUNCTION__\n"  . $herecurr);
388813214adfSAndy Whitcroft		}
3889773647a0SAndy Whitcroft
38902c92488aSJoe Perches# check for use of yield()
38912c92488aSJoe Perches		if ($line =~ /\byield\s*\(\s*\)/) {
38922c92488aSJoe Perches			WARN("YIELD",
38932c92488aSJoe Perches			     "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n"  . $herecurr);
38942c92488aSJoe Perches		}
38952c92488aSJoe Perches
3896179f8f40SJoe Perches# check for comparisons against true and false
3897179f8f40SJoe Perches		if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
3898179f8f40SJoe Perches			my $lead = $1;
3899179f8f40SJoe Perches			my $arg = $2;
3900179f8f40SJoe Perches			my $test = $3;
3901179f8f40SJoe Perches			my $otype = $4;
3902179f8f40SJoe Perches			my $trail = $5;
3903179f8f40SJoe Perches			my $op = "!";
3904179f8f40SJoe Perches
3905179f8f40SJoe Perches			($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
3906179f8f40SJoe Perches
3907179f8f40SJoe Perches			my $type = lc($otype);
3908179f8f40SJoe Perches			if ($type =~ /^(?:true|false)$/) {
3909179f8f40SJoe Perches				if (("$test" eq "==" && "$type" eq "true") ||
3910179f8f40SJoe Perches				    ("$test" eq "!=" && "$type" eq "false")) {
3911179f8f40SJoe Perches					$op = "";
3912179f8f40SJoe Perches				}
3913179f8f40SJoe Perches
3914179f8f40SJoe Perches				CHK("BOOL_COMPARISON",
3915179f8f40SJoe Perches				    "Using comparison to $otype is error prone\n" . $herecurr);
3916179f8f40SJoe Perches
3917179f8f40SJoe Perches## maybe suggesting a correct construct would better
3918179f8f40SJoe Perches##				    "Using comparison to $otype is error prone.  Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
3919179f8f40SJoe Perches
3920179f8f40SJoe Perches			}
3921179f8f40SJoe Perches		}
3922179f8f40SJoe Perches
39234882720bSThomas Gleixner# check for semaphores initialized locked
39244882720bSThomas Gleixner		if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
3925000d1cc1SJoe Perches			WARN("CONSIDER_COMPLETION",
3926000d1cc1SJoe Perches			     "consider using a completion\n" . $herecurr);
3927773647a0SAndy Whitcroft		}
39286712d858SJoe Perches
392967d0a075SJoe Perches# recommend kstrto* over simple_strto* and strict_strto*
393067d0a075SJoe Perches		if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
3931000d1cc1SJoe Perches			WARN("CONSIDER_KSTRTO",
393267d0a075SJoe Perches			     "$1 is obsolete, use k$3 instead\n" . $herecurr);
3933773647a0SAndy Whitcroft		}
39346712d858SJoe Perches
3935f3db6639SMichael Ellerman# check for __initcall(), use device_initcall() explicitly please
3936f3db6639SMichael Ellerman		if ($line =~ /^.\s*__initcall\s*\(/) {
3937000d1cc1SJoe Perches			WARN("USE_DEVICE_INITCALL",
3938000d1cc1SJoe Perches			     "please use device_initcall() instead of __initcall()\n" . $herecurr);
3939f3db6639SMichael Ellerman		}
39406712d858SJoe Perches
394179404849SEmese Revfy# check for various ops structs, ensure they are const.
394279404849SEmese Revfy		my $struct_ops = qr{acpi_dock_ops|
394379404849SEmese Revfy				address_space_operations|
394479404849SEmese Revfy				backlight_ops|
394579404849SEmese Revfy				block_device_operations|
394679404849SEmese Revfy				dentry_operations|
394779404849SEmese Revfy				dev_pm_ops|
394879404849SEmese Revfy				dma_map_ops|
394979404849SEmese Revfy				extent_io_ops|
395079404849SEmese Revfy				file_lock_operations|
395179404849SEmese Revfy				file_operations|
395279404849SEmese Revfy				hv_ops|
395379404849SEmese Revfy				ide_dma_ops|
395479404849SEmese Revfy				intel_dvo_dev_ops|
395579404849SEmese Revfy				item_operations|
395679404849SEmese Revfy				iwl_ops|
395779404849SEmese Revfy				kgdb_arch|
395879404849SEmese Revfy				kgdb_io|
395979404849SEmese Revfy				kset_uevent_ops|
396079404849SEmese Revfy				lock_manager_operations|
396179404849SEmese Revfy				microcode_ops|
396279404849SEmese Revfy				mtrr_ops|
396379404849SEmese Revfy				neigh_ops|
396479404849SEmese Revfy				nlmsvc_binding|
396579404849SEmese Revfy				pci_raw_ops|
396679404849SEmese Revfy				pipe_buf_operations|
396779404849SEmese Revfy				platform_hibernation_ops|
396879404849SEmese Revfy				platform_suspend_ops|
396979404849SEmese Revfy				proto_ops|
397079404849SEmese Revfy				rpc_pipe_ops|
397179404849SEmese Revfy				seq_operations|
397279404849SEmese Revfy				snd_ac97_build_ops|
397379404849SEmese Revfy				soc_pcmcia_socket_ops|
397479404849SEmese Revfy				stacktrace_ops|
397579404849SEmese Revfy				sysfs_ops|
397679404849SEmese Revfy				tty_operations|
397779404849SEmese Revfy				usb_mon_operations|
397879404849SEmese Revfy				wd_ops}x;
39796903ffb2SAndy Whitcroft		if ($line !~ /\bconst\b/ &&
398079404849SEmese Revfy		    $line =~ /\bstruct\s+($struct_ops)\b/) {
3981000d1cc1SJoe Perches			WARN("CONST_STRUCT",
3982000d1cc1SJoe Perches			     "struct $1 should normally be const\n" .
39836903ffb2SAndy Whitcroft				$herecurr);
39842b6db5cbSAndy Whitcroft		}
3985773647a0SAndy Whitcroft
3986773647a0SAndy Whitcroft# use of NR_CPUS is usually wrong
3987773647a0SAndy Whitcroft# ignore definitions of NR_CPUS and usage to define arrays as likely right
3988773647a0SAndy Whitcroft		if ($line =~ /\bNR_CPUS\b/ &&
3989c45dcabdSAndy Whitcroft		    $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
3990c45dcabdSAndy Whitcroft		    $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
3991171ae1a4SAndy Whitcroft		    $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
3992171ae1a4SAndy Whitcroft		    $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
3993171ae1a4SAndy Whitcroft		    $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
3994773647a0SAndy Whitcroft		{
3995000d1cc1SJoe Perches			WARN("NR_CPUS",
3996000d1cc1SJoe Perches			     "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
3997773647a0SAndy Whitcroft		}
39989c9ba34eSAndy Whitcroft
39999c9ba34eSAndy Whitcroft# check for %L{u,d,i} in strings
40009c9ba34eSAndy Whitcroft		my $string;
40019c9ba34eSAndy Whitcroft		while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
40029c9ba34eSAndy Whitcroft			$string = substr($rawline, $-[1], $+[1] - $-[1]);
40032a1bc5d5SAndy Whitcroft			$string =~ s/%%/__/g;
40049c9ba34eSAndy Whitcroft			if ($string =~ /(?<!%)%L[udi]/) {
4005000d1cc1SJoe Perches				WARN("PRINTF_L",
4006000d1cc1SJoe Perches				     "\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
40079c9ba34eSAndy Whitcroft				last;
40089c9ba34eSAndy Whitcroft			}
40099c9ba34eSAndy Whitcroft		}
4010691d77b6SAndy Whitcroft
4011691d77b6SAndy Whitcroft# whine mightly about in_atomic
4012691d77b6SAndy Whitcroft		if ($line =~ /\bin_atomic\s*\(/) {
4013691d77b6SAndy Whitcroft			if ($realfile =~ m@^drivers/@) {
4014000d1cc1SJoe Perches				ERROR("IN_ATOMIC",
4015000d1cc1SJoe Perches				      "do not use in_atomic in drivers\n" . $herecurr);
4016f4a87736SAndy Whitcroft			} elsif ($realfile !~ m@^kernel/@) {
4017000d1cc1SJoe Perches				WARN("IN_ATOMIC",
4018000d1cc1SJoe Perches				     "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
4019691d77b6SAndy Whitcroft			}
4020691d77b6SAndy Whitcroft		}
40211704f47bSPeter Zijlstra
40221704f47bSPeter Zijlstra# check for lockdep_set_novalidate_class
40231704f47bSPeter Zijlstra		if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
40241704f47bSPeter Zijlstra		    $line =~ /__lockdep_no_validate__\s*\)/ ) {
40251704f47bSPeter Zijlstra			if ($realfile !~ m@^kernel/lockdep@ &&
40261704f47bSPeter Zijlstra			    $realfile !~ m@^include/linux/lockdep@ &&
40271704f47bSPeter Zijlstra			    $realfile !~ m@^drivers/base/core@) {
4028000d1cc1SJoe Perches				ERROR("LOCKDEP",
4029000d1cc1SJoe Perches				      "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
40301704f47bSPeter Zijlstra			}
40311704f47bSPeter Zijlstra		}
403288f8831cSDave Jones
403388f8831cSDave Jones		if ($line =~ /debugfs_create_file.*S_IWUGO/ ||
403488f8831cSDave Jones		    $line =~ /DEVICE_ATTR.*S_IWUGO/ ) {
4035000d1cc1SJoe Perches			WARN("EXPORTED_WORLD_WRITABLE",
4036000d1cc1SJoe Perches			     "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
403788f8831cSDave Jones		}
403813214adfSAndy Whitcroft	}
403913214adfSAndy Whitcroft
404013214adfSAndy Whitcroft	# If we have no input at all, then there is nothing to report on
404113214adfSAndy Whitcroft	# so just keep quiet.
404213214adfSAndy Whitcroft	if ($#rawlines == -1) {
404313214adfSAndy Whitcroft		exit(0);
40440a920b5bSAndy Whitcroft	}
40450a920b5bSAndy Whitcroft
40468905a67cSAndy Whitcroft	# In mailback mode only produce a report in the negative, for
40478905a67cSAndy Whitcroft	# things that appear to be patches.
40488905a67cSAndy Whitcroft	if ($mailback && ($clean == 1 || !$is_patch)) {
40498905a67cSAndy Whitcroft		exit(0);
40508905a67cSAndy Whitcroft	}
40518905a67cSAndy Whitcroft
40528905a67cSAndy Whitcroft	# This is not a patch, and we are are in 'no-patch' mode so
40538905a67cSAndy Whitcroft	# just keep quiet.
40548905a67cSAndy Whitcroft	if (!$chk_patch && !$is_patch) {
40558905a67cSAndy Whitcroft		exit(0);
40568905a67cSAndy Whitcroft	}
40578905a67cSAndy Whitcroft
40588905a67cSAndy Whitcroft	if (!$is_patch) {
4059000d1cc1SJoe Perches		ERROR("NOT_UNIFIED_DIFF",
4060000d1cc1SJoe Perches		      "Does not appear to be a unified-diff format patch\n");
40610a920b5bSAndy Whitcroft	}
40620a920b5bSAndy Whitcroft	if ($is_patch && $chk_signoff && $signoff == 0) {
4063000d1cc1SJoe Perches		ERROR("MISSING_SIGN_OFF",
4064000d1cc1SJoe Perches		      "Missing Signed-off-by: line(s)\n");
40650a920b5bSAndy Whitcroft	}
40660a920b5bSAndy Whitcroft
4067f0a594c1SAndy Whitcroft	print report_dump();
406813214adfSAndy Whitcroft	if ($summary && !($clean == 1 && $quiet == 1)) {
406913214adfSAndy Whitcroft		print "$filename " if ($summary_file);
40706c72ffaaSAndy Whitcroft		print "total: $cnt_error errors, $cnt_warn warnings, " .
40716c72ffaaSAndy Whitcroft			(($check)? "$cnt_chk checks, " : "") .
40726c72ffaaSAndy Whitcroft			"$cnt_lines lines checked\n";
40738905a67cSAndy Whitcroft		print "\n" if ($quiet == 0);
40746c72ffaaSAndy Whitcroft	}
40758905a67cSAndy Whitcroft
4076d2c0a235SAndy Whitcroft	if ($quiet == 0) {
4077d1fe9c09SJoe Perches
4078d1fe9c09SJoe Perches		if ($^V lt 5.10.0) {
4079d1fe9c09SJoe Perches			print("NOTE: perl $^V is not modern enough to detect all possible issues.\n");
4080d1fe9c09SJoe Perches			print("An upgrade to at least perl v5.10.0 is suggested.\n\n");
4081d1fe9c09SJoe Perches		}
4082d1fe9c09SJoe Perches
4083d2c0a235SAndy Whitcroft		# If there were whitespace errors which cleanpatch can fix
4084d2c0a235SAndy Whitcroft		# then suggest that.
4085d2c0a235SAndy Whitcroft		if ($rpt_cleaners) {
4086d2c0a235SAndy Whitcroft			print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n";
4087d2c0a235SAndy Whitcroft			print "      scripts/cleanfile\n\n";
4088b0781216SMike Frysinger			$rpt_cleaners = 0;
4089d2c0a235SAndy Whitcroft		}
4090d2c0a235SAndy Whitcroft	}
4091d2c0a235SAndy Whitcroft
409211232688SArtem Bityutskiy	if ($quiet == 0 && keys %ignore_type) {
4093000d1cc1SJoe Perches	    print "NOTE: Ignored message types:";
4094000d1cc1SJoe Perches	    foreach my $ignore (sort keys %ignore_type) {
4095000d1cc1SJoe Perches		print " $ignore";
4096000d1cc1SJoe Perches	    }
409711232688SArtem Bityutskiy	    print "\n\n";
4098000d1cc1SJoe Perches	}
4099000d1cc1SJoe Perches
41003705ce5bSJoe Perches	if ($clean == 0 && $fix && "@rawlines" ne "@fixed") {
41013705ce5bSJoe Perches		my $newfile = $filename . ".EXPERIMENTAL-checkpatch-fixes";
41023705ce5bSJoe Perches		my $linecount = 0;
41033705ce5bSJoe Perches		my $f;
41043705ce5bSJoe Perches
41053705ce5bSJoe Perches		open($f, '>', $newfile)
41063705ce5bSJoe Perches		    or die "$P: Can't open $newfile for write\n";
41073705ce5bSJoe Perches		foreach my $fixed_line (@fixed) {
41083705ce5bSJoe Perches			$linecount++;
41093705ce5bSJoe Perches			if ($file) {
41103705ce5bSJoe Perches				if ($linecount > 3) {
41113705ce5bSJoe Perches					$fixed_line =~ s/^\+//;
41123705ce5bSJoe Perches					print $f $fixed_line. "\n";
41133705ce5bSJoe Perches				}
41143705ce5bSJoe Perches			} else {
41153705ce5bSJoe Perches				print $f $fixed_line . "\n";
41163705ce5bSJoe Perches			}
41173705ce5bSJoe Perches		}
41183705ce5bSJoe Perches		close($f);
41193705ce5bSJoe Perches
41203705ce5bSJoe Perches		if (!$quiet) {
41213705ce5bSJoe Perches			print << "EOM";
41223705ce5bSJoe PerchesWrote EXPERIMENTAL --fix correction(s) to '$newfile'
41233705ce5bSJoe Perches
41243705ce5bSJoe PerchesDo _NOT_ trust the results written to this file.
41253705ce5bSJoe PerchesDo _NOT_ submit these changes without inspecting them for correctness.
41263705ce5bSJoe Perches
41273705ce5bSJoe PerchesThis EXPERIMENTAL file is simply a convenience to help rewrite patches.
41283705ce5bSJoe PerchesNo warranties, expressed or implied...
41293705ce5bSJoe Perches
41303705ce5bSJoe PerchesEOM
41313705ce5bSJoe Perches		}
41323705ce5bSJoe Perches	}
41333705ce5bSJoe Perches
41340a920b5bSAndy Whitcroft	if ($clean == 1 && $quiet == 0) {
4135c2fdda0dSAndy Whitcroft		print "$vname has no obvious style problems and is ready for submission.\n"
41360a920b5bSAndy Whitcroft	}
41370a920b5bSAndy Whitcroft	if ($clean == 0 && $quiet == 0) {
4138000d1cc1SJoe Perches		print << "EOM";
4139000d1cc1SJoe Perches$vname has style problems, please review.
4140000d1cc1SJoe Perches
4141000d1cc1SJoe PerchesIf any of these errors are false positives, please report
4142000d1cc1SJoe Perchesthem to the maintainer, see CHECKPATCH in MAINTAINERS.
4143000d1cc1SJoe PerchesEOM
41440a920b5bSAndy Whitcroft	}
414513214adfSAndy Whitcroft
41460a920b5bSAndy Whitcroft	return $clean;
41470a920b5bSAndy Whitcroft}
4148