xref: /linux-6.15/scripts/checkpatch.pl (revision d8b07710)
10a920b5bSAndy Whitcroft#!/usr/bin/perl -w
2dbf004d7SDave Jones# (c) 2001, Dave Jones. (the file handling bit)
300df344fSAndy Whitcroft# (c) 2005, Joel Schopp <[email protected]> (the ugly bit)
42a5a2c25SAndy Whitcroft# (c) 2007,2008, Andy Whitcroft <[email protected]> (new conditions, test suite)
5015830beSAndy Whitcroft# (c) 2008-2010 Andy Whitcroft <[email protected]>
60a920b5bSAndy Whitcroft# Licensed under the terms of the GNU GPL License version 2
70a920b5bSAndy Whitcroft
80a920b5bSAndy Whitcroftuse strict;
9c707a81dSJoe Perchesuse POSIX;
100a920b5bSAndy Whitcroft
110a920b5bSAndy Whitcroftmy $P = $0;
1200df344fSAndy Whitcroft$P =~ s@.*/@@g;
130a920b5bSAndy Whitcroft
14000d1cc1SJoe Perchesmy $V = '0.32';
150a920b5bSAndy Whitcroft
160a920b5bSAndy Whitcroftuse Getopt::Long qw(:config no_auto_abbrev);
170a920b5bSAndy Whitcroft
180a920b5bSAndy Whitcroftmy $quiet = 0;
190a920b5bSAndy Whitcroftmy $tree = 1;
200a920b5bSAndy Whitcroftmy $chk_signoff = 1;
210a920b5bSAndy Whitcroftmy $chk_patch = 1;
22773647a0SAndy Whitcroftmy $tst_only;
236c72ffaaSAndy Whitcroftmy $emacs = 0;
248905a67cSAndy Whitcroftmy $terse = 0;
256c72ffaaSAndy Whitcroftmy $file = 0;
266c72ffaaSAndy Whitcroftmy $check = 0;
278905a67cSAndy Whitcroftmy $summary = 1;
288905a67cSAndy Whitcroftmy $mailback = 0;
2913214adfSAndy Whitcroftmy $summary_file = 0;
30000d1cc1SJoe Perchesmy $show_types = 0;
313705ce5bSJoe Perchesmy $fix = 0;
326c72ffaaSAndy Whitcroftmy $root;
33c2fdda0dSAndy Whitcroftmy %debug;
343445686aSJoe Perchesmy %camelcase = ();
3591bfe484SJoe Perchesmy %use_type = ();
3691bfe484SJoe Perchesmy @use = ();
3791bfe484SJoe Perchesmy %ignore_type = ();
38000d1cc1SJoe Perchesmy @ignore = ();
3977f5b10aSHannes Edermy $help = 0;
40000d1cc1SJoe Perchesmy $configuration_file = ".checkpatch.conf";
416cd7f386SJoe Perchesmy $max_line_length = 80;
42d62a201fSDave Hansenmy $ignore_perl_version = 0;
43d62a201fSDave Hansenmy $minimum_perl_version = 5.10.0;
4477f5b10aSHannes Eder
4577f5b10aSHannes Edersub help {
4677f5b10aSHannes Eder	my ($exitcode) = @_;
4777f5b10aSHannes Eder
4877f5b10aSHannes Eder	print << "EOM";
4977f5b10aSHannes EderUsage: $P [OPTION]... [FILE]...
5077f5b10aSHannes EderVersion: $V
5177f5b10aSHannes Eder
5277f5b10aSHannes EderOptions:
5377f5b10aSHannes Eder  -q, --quiet                quiet
5477f5b10aSHannes Eder  --no-tree                  run without a kernel tree
5577f5b10aSHannes Eder  --no-signoff               do not check for 'Signed-off-by' line
5677f5b10aSHannes Eder  --patch                    treat FILE as patchfile (default)
5777f5b10aSHannes Eder  --emacs                    emacs compile window format
5877f5b10aSHannes Eder  --terse                    one line per report
5977f5b10aSHannes Eder  -f, --file                 treat FILE as regular source file
6077f5b10aSHannes Eder  --subjective, --strict     enable more subjective tests
6191bfe484SJoe Perches  --types TYPE(,TYPE2...)    show only these comma separated message types
62000d1cc1SJoe Perches  --ignore TYPE(,TYPE2...)   ignore various comma separated message types
636cd7f386SJoe Perches  --max-line-length=n        set the maximum line length, if exceeded, warn
64000d1cc1SJoe Perches  --show-types               show the message "types" in the output
6577f5b10aSHannes Eder  --root=PATH                PATH to the kernel tree root
6677f5b10aSHannes Eder  --no-summary               suppress the per-file summary
6777f5b10aSHannes Eder  --mailback                 only produce a report in case of warnings/errors
6877f5b10aSHannes Eder  --summary-file             include the filename in summary
6977f5b10aSHannes Eder  --debug KEY=[0|1]          turn on/off debugging of KEY, where KEY is one of
7077f5b10aSHannes Eder                             'values', 'possible', 'type', and 'attr' (default
7177f5b10aSHannes Eder                             is all off)
7277f5b10aSHannes Eder  --test-only=WORD           report only warnings/errors containing WORD
7377f5b10aSHannes Eder                             literally
743705ce5bSJoe Perches  --fix                      EXPERIMENTAL - may create horrible results
753705ce5bSJoe Perches                             If correctable single-line errors exist, create
763705ce5bSJoe Perches                             "<inputfile>.EXPERIMENTAL-checkpatch-fixes"
773705ce5bSJoe Perches                             with potential errors corrected to the preferred
783705ce5bSJoe Perches                             checkpatch style
79d62a201fSDave Hansen  --ignore-perl-version      override checking of perl version.  expect
80d62a201fSDave Hansen                             runtime errors.
8177f5b10aSHannes Eder  -h, --help, --version      display this help and exit
8277f5b10aSHannes Eder
8377f5b10aSHannes EderWhen FILE is - read standard input.
8477f5b10aSHannes EderEOM
8577f5b10aSHannes Eder
8677f5b10aSHannes Eder	exit($exitcode);
8777f5b10aSHannes Eder}
8877f5b10aSHannes Eder
89000d1cc1SJoe Perchesmy $conf = which_conf($configuration_file);
90000d1cc1SJoe Perchesif (-f $conf) {
91000d1cc1SJoe Perches	my @conf_args;
92000d1cc1SJoe Perches	open(my $conffile, '<', "$conf")
93000d1cc1SJoe Perches	    or warn "$P: Can't find a readable $configuration_file file $!\n";
94000d1cc1SJoe Perches
95000d1cc1SJoe Perches	while (<$conffile>) {
96000d1cc1SJoe Perches		my $line = $_;
97000d1cc1SJoe Perches
98000d1cc1SJoe Perches		$line =~ s/\s*\n?$//g;
99000d1cc1SJoe Perches		$line =~ s/^\s*//g;
100000d1cc1SJoe Perches		$line =~ s/\s+/ /g;
101000d1cc1SJoe Perches
102000d1cc1SJoe Perches		next if ($line =~ m/^\s*#/);
103000d1cc1SJoe Perches		next if ($line =~ m/^\s*$/);
104000d1cc1SJoe Perches
105000d1cc1SJoe Perches		my @words = split(" ", $line);
106000d1cc1SJoe Perches		foreach my $word (@words) {
107000d1cc1SJoe Perches			last if ($word =~ m/^#/);
108000d1cc1SJoe Perches			push (@conf_args, $word);
109000d1cc1SJoe Perches		}
110000d1cc1SJoe Perches	}
111000d1cc1SJoe Perches	close($conffile);
112000d1cc1SJoe Perches	unshift(@ARGV, @conf_args) if @conf_args;
113000d1cc1SJoe Perches}
114000d1cc1SJoe Perches
1150a920b5bSAndy WhitcroftGetOptions(
1166c72ffaaSAndy Whitcroft	'q|quiet+'	=> \$quiet,
1170a920b5bSAndy Whitcroft	'tree!'		=> \$tree,
1180a920b5bSAndy Whitcroft	'signoff!'	=> \$chk_signoff,
1190a920b5bSAndy Whitcroft	'patch!'	=> \$chk_patch,
1206c72ffaaSAndy Whitcroft	'emacs!'	=> \$emacs,
1218905a67cSAndy Whitcroft	'terse!'	=> \$terse,
12277f5b10aSHannes Eder	'f|file!'	=> \$file,
1236c72ffaaSAndy Whitcroft	'subjective!'	=> \$check,
1246c72ffaaSAndy Whitcroft	'strict!'	=> \$check,
125000d1cc1SJoe Perches	'ignore=s'	=> \@ignore,
12691bfe484SJoe Perches	'types=s'	=> \@use,
127000d1cc1SJoe Perches	'show-types!'	=> \$show_types,
1286cd7f386SJoe Perches	'max-line-length=i' => \$max_line_length,
1296c72ffaaSAndy Whitcroft	'root=s'	=> \$root,
1308905a67cSAndy Whitcroft	'summary!'	=> \$summary,
1318905a67cSAndy Whitcroft	'mailback!'	=> \$mailback,
13213214adfSAndy Whitcroft	'summary-file!'	=> \$summary_file,
1333705ce5bSJoe Perches	'fix!'		=> \$fix,
134d62a201fSDave Hansen	'ignore-perl-version!' => \$ignore_perl_version,
135c2fdda0dSAndy Whitcroft	'debug=s'	=> \%debug,
136773647a0SAndy Whitcroft	'test-only=s'	=> \$tst_only,
13777f5b10aSHannes Eder	'h|help'	=> \$help,
13877f5b10aSHannes Eder	'version'	=> \$help
13977f5b10aSHannes Eder) or help(1);
14077f5b10aSHannes Eder
14177f5b10aSHannes Ederhelp(0) if ($help);
1420a920b5bSAndy Whitcroft
1430a920b5bSAndy Whitcroftmy $exit = 0;
1440a920b5bSAndy Whitcroft
145d62a201fSDave Hansenif ($^V && $^V lt $minimum_perl_version) {
146d62a201fSDave Hansen	printf "$P: requires at least perl version %vd\n", $minimum_perl_version;
147d62a201fSDave Hansen	if (!$ignore_perl_version) {
148d62a201fSDave Hansen		exit(1);
149d62a201fSDave Hansen	}
150d62a201fSDave Hansen}
151d62a201fSDave Hansen
1520a920b5bSAndy Whitcroftif ($#ARGV < 0) {
15377f5b10aSHannes Eder	print "$P: no input files\n";
1540a920b5bSAndy Whitcroft	exit(1);
1550a920b5bSAndy Whitcroft}
1560a920b5bSAndy Whitcroft
15791bfe484SJoe Perchessub hash_save_array_words {
15891bfe484SJoe Perches	my ($hashRef, $arrayRef) = @_;
15991bfe484SJoe Perches
16091bfe484SJoe Perches	my @array = split(/,/, join(',', @$arrayRef));
16191bfe484SJoe Perches	foreach my $word (@array) {
162000d1cc1SJoe Perches		$word =~ s/\s*\n?$//g;
163000d1cc1SJoe Perches		$word =~ s/^\s*//g;
164000d1cc1SJoe Perches		$word =~ s/\s+/ /g;
165000d1cc1SJoe Perches		$word =~ tr/[a-z]/[A-Z]/;
166000d1cc1SJoe Perches
167000d1cc1SJoe Perches		next if ($word =~ m/^\s*#/);
168000d1cc1SJoe Perches		next if ($word =~ m/^\s*$/);
169000d1cc1SJoe Perches
17091bfe484SJoe Perches		$hashRef->{$word}++;
171000d1cc1SJoe Perches	}
17291bfe484SJoe Perches}
17391bfe484SJoe Perches
17491bfe484SJoe Perchessub hash_show_words {
17591bfe484SJoe Perches	my ($hashRef, $prefix) = @_;
17691bfe484SJoe Perches
17758cb3cf6SJoe Perches	if ($quiet == 0 && keys %$hashRef) {
17891bfe484SJoe Perches		print "NOTE: $prefix message types:";
17958cb3cf6SJoe Perches		foreach my $word (sort keys %$hashRef) {
18091bfe484SJoe Perches			print " $word";
18191bfe484SJoe Perches		}
18291bfe484SJoe Perches		print "\n\n";
18391bfe484SJoe Perches	}
18491bfe484SJoe Perches}
18591bfe484SJoe Perches
18691bfe484SJoe Percheshash_save_array_words(\%ignore_type, \@ignore);
18791bfe484SJoe Percheshash_save_array_words(\%use_type, \@use);
188000d1cc1SJoe Perches
189c2fdda0dSAndy Whitcroftmy $dbg_values = 0;
190c2fdda0dSAndy Whitcroftmy $dbg_possible = 0;
1917429c690SAndy Whitcroftmy $dbg_type = 0;
192a1ef277eSAndy Whitcroftmy $dbg_attr = 0;
193c2fdda0dSAndy Whitcroftfor my $key (keys %debug) {
19421caa13cSAndy Whitcroft	## no critic
19521caa13cSAndy Whitcroft	eval "\${dbg_$key} = '$debug{$key}';";
19621caa13cSAndy Whitcroft	die "$@" if ($@);
197c2fdda0dSAndy Whitcroft}
198c2fdda0dSAndy Whitcroft
199d2c0a235SAndy Whitcroftmy $rpt_cleaners = 0;
200d2c0a235SAndy Whitcroft
2018905a67cSAndy Whitcroftif ($terse) {
2028905a67cSAndy Whitcroft	$emacs = 1;
2038905a67cSAndy Whitcroft	$quiet++;
2048905a67cSAndy Whitcroft}
2058905a67cSAndy Whitcroft
2066c72ffaaSAndy Whitcroftif ($tree) {
2076c72ffaaSAndy Whitcroft	if (defined $root) {
2086c72ffaaSAndy Whitcroft		if (!top_of_kernel_tree($root)) {
2096c72ffaaSAndy Whitcroft			die "$P: $root: --root does not point at a valid tree\n";
2106c72ffaaSAndy Whitcroft		}
2116c72ffaaSAndy Whitcroft	} else {
2126c72ffaaSAndy Whitcroft		if (top_of_kernel_tree('.')) {
2136c72ffaaSAndy Whitcroft			$root = '.';
2146c72ffaaSAndy Whitcroft		} elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
2156c72ffaaSAndy Whitcroft						top_of_kernel_tree($1)) {
2166c72ffaaSAndy Whitcroft			$root = $1;
2176c72ffaaSAndy Whitcroft		}
2186c72ffaaSAndy Whitcroft	}
2196c72ffaaSAndy Whitcroft
2206c72ffaaSAndy Whitcroft	if (!defined $root) {
2210a920b5bSAndy Whitcroft		print "Must be run from the top-level dir. of a kernel tree\n";
2220a920b5bSAndy Whitcroft		exit(2);
2230a920b5bSAndy Whitcroft	}
2246c72ffaaSAndy Whitcroft}
2256c72ffaaSAndy Whitcroft
2266c72ffaaSAndy Whitcroftmy $emitted_corrupt = 0;
2276c72ffaaSAndy Whitcroft
2282ceb532bSAndy Whitcroftour $Ident	= qr{
2292ceb532bSAndy Whitcroft			[A-Za-z_][A-Za-z\d_]*
2302ceb532bSAndy Whitcroft			(?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
2312ceb532bSAndy Whitcroft		}x;
2326c72ffaaSAndy Whitcroftour $Storage	= qr{extern|static|asmlinkage};
2336c72ffaaSAndy Whitcroftour $Sparse	= qr{
2346c72ffaaSAndy Whitcroft			__user|
2356c72ffaaSAndy Whitcroft			__kernel|
2366c72ffaaSAndy Whitcroft			__force|
2376c72ffaaSAndy Whitcroft			__iomem|
2386c72ffaaSAndy Whitcroft			__must_check|
2396c72ffaaSAndy Whitcroft			__init_refok|
240417495edSAndy Whitcroft			__kprobes|
241165e72a6SSven Eckelmann			__ref|
242165e72a6SSven Eckelmann			__rcu
2436c72ffaaSAndy Whitcroft		}x;
24452131292SWolfram Sang
2458716de38SJoe Perchesour $InitAttribute = qr{__(?:mem|cpu|dev|net_|)(?:initdata|initconst|init\b)};
2468716de38SJoe Perches
24752131292SWolfram Sang# Notes to $Attribute:
24852131292SWolfram Sang# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
2496c72ffaaSAndy Whitcroftour $Attribute	= qr{
2506c72ffaaSAndy Whitcroft			const|
25103f1df7dSJoe Perches			__percpu|
25203f1df7dSJoe Perches			__nocast|
25303f1df7dSJoe Perches			__safe|
25403f1df7dSJoe Perches			__bitwise__|
25503f1df7dSJoe Perches			__packed__|
25603f1df7dSJoe Perches			__packed2__|
25703f1df7dSJoe Perches			__naked|
25803f1df7dSJoe Perches			__maybe_unused|
25903f1df7dSJoe Perches			__always_unused|
26003f1df7dSJoe Perches			__noreturn|
26103f1df7dSJoe Perches			__used|
26203f1df7dSJoe Perches			__cold|
26303f1df7dSJoe Perches			__noclone|
26403f1df7dSJoe Perches			__deprecated|
2656c72ffaaSAndy Whitcroft			__read_mostly|
2666c72ffaaSAndy Whitcroft			__kprobes|
2678716de38SJoe Perches			$InitAttribute|
26824e1d81aSAndy Whitcroft			____cacheline_aligned|
26924e1d81aSAndy Whitcroft			____cacheline_aligned_in_smp|
2705fe3af11SAndy Whitcroft			____cacheline_internodealigned_in_smp|
2715fe3af11SAndy Whitcroft			__weak
2726c72ffaaSAndy Whitcroft		  }x;
273c45dcabdSAndy Whitcroftour $Modifier;
2746c72ffaaSAndy Whitcroftour $Inline	= qr{inline|__always_inline|noinline};
2756c72ffaaSAndy Whitcroftour $Member	= qr{->$Ident|\.$Ident|\[[^]]*\]};
2766c72ffaaSAndy Whitcroftour $Lval	= qr{$Ident(?:$Member)*};
2776c72ffaaSAndy Whitcroft
27895e2c602SJoe Perchesour $Int_type	= qr{(?i)llu|ull|ll|lu|ul|l|u};
27995e2c602SJoe Perchesour $Binary	= qr{(?i)0b[01]+$Int_type?};
28095e2c602SJoe Perchesour $Hex	= qr{(?i)0x[0-9a-f]+$Int_type?};
28195e2c602SJoe Perchesour $Int	= qr{[0-9]+$Int_type?};
282326b1ffcSJoe Perchesour $Float_hex	= qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?};
283326b1ffcSJoe Perchesour $Float_dec	= qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?};
284326b1ffcSJoe Perchesour $Float_int	= qr{(?i)[0-9]+e-?[0-9]+[fl]?};
28574349bccSJoe Perchesour $Float	= qr{$Float_hex|$Float_dec|$Float_int};
28695e2c602SJoe Perchesour $Constant	= qr{$Float|$Binary|$Hex|$Int};
287326b1ffcSJoe Perchesour $Assignment	= qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=};
28886f9d059SAndy Whitcroftour $Compare    = qr{<=|>=|==|!=|<|>};
28923f780c9SJoe Perchesour $Arithmetic = qr{\+|-|\*|\/|%};
2906c72ffaaSAndy Whitcroftour $Operators	= qr{
2916c72ffaaSAndy Whitcroft			<=|>=|==|!=|
2926c72ffaaSAndy Whitcroft			=>|->|<<|>>|<|>|!|~|
29323f780c9SJoe Perches			&&|\|\||,|\^|\+\+|--|&|\||$Arithmetic
2946c72ffaaSAndy Whitcroft		  }x;
2956c72ffaaSAndy Whitcroft
2968905a67cSAndy Whitcroftour $NonptrType;
2978716de38SJoe Perchesour $NonptrTypeWithAttr;
2988905a67cSAndy Whitcroftour $Type;
2998905a67cSAndy Whitcroftour $Declare;
3008905a67cSAndy Whitcroft
30115662b3eSJoe Perchesour $NON_ASCII_UTF8	= qr{
30215662b3eSJoe Perches	[\xC2-\xDF][\x80-\xBF]               # non-overlong 2-byte
303171ae1a4SAndy Whitcroft	|  \xE0[\xA0-\xBF][\x80-\xBF]        # excluding overlongs
304171ae1a4SAndy Whitcroft	| [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}  # straight 3-byte
305171ae1a4SAndy Whitcroft	|  \xED[\x80-\x9F][\x80-\xBF]        # excluding surrogates
306171ae1a4SAndy Whitcroft	|  \xF0[\x90-\xBF][\x80-\xBF]{2}     # planes 1-3
307171ae1a4SAndy Whitcroft	| [\xF1-\xF3][\x80-\xBF]{3}          # planes 4-15
308171ae1a4SAndy Whitcroft	|  \xF4[\x80-\x8F][\x80-\xBF]{2}     # plane 16
309171ae1a4SAndy Whitcroft}x;
310171ae1a4SAndy Whitcroft
31115662b3eSJoe Perchesour $UTF8	= qr{
31215662b3eSJoe Perches	[\x09\x0A\x0D\x20-\x7E]              # ASCII
31315662b3eSJoe Perches	| $NON_ASCII_UTF8
31415662b3eSJoe Perches}x;
31515662b3eSJoe Perches
3168ed22cadSAndy Whitcroftour $typeTypedefs = qr{(?x:
317fb9e9096SAndy Whitcroft	(?:__)?(?:u|s|be|le)(?:8|16|32|64)|
3188ed22cadSAndy Whitcroft	atomic_t
3198ed22cadSAndy Whitcroft)};
3208ed22cadSAndy Whitcroft
321691e669bSJoe Perchesour $logFunctions = qr{(?x:
3226e60c02eSJoe Perches	printk(?:_ratelimited|_once|)|
3237d0b6594SJacob Keller	(?:[a-z0-9]+_){1,2}(?:printk|emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)(?:_ratelimited|_once|)|
3246e60c02eSJoe Perches	WARN(?:_RATELIMIT|_ONCE|)|
325b0531722SJoe Perches	panic|
326b0531722SJoe Perches	MODULE_[A-Z_]+
327691e669bSJoe Perches)};
328691e669bSJoe Perches
32920112475SJoe Perchesour $signature_tags = qr{(?xi:
33020112475SJoe Perches	Signed-off-by:|
33120112475SJoe Perches	Acked-by:|
33220112475SJoe Perches	Tested-by:|
33320112475SJoe Perches	Reviewed-by:|
33420112475SJoe Perches	Reported-by:|
3358543ae12SMugunthan V N	Suggested-by:|
33620112475SJoe Perches	To:|
33720112475SJoe Perches	Cc:
33820112475SJoe Perches)};
33920112475SJoe Perches
3408905a67cSAndy Whitcroftour @typeList = (
3418905a67cSAndy Whitcroft	qr{void},
342c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?char},
343c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?short},
344c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?int},
345c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?long},
346c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?long\s+int},
347c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?long\s+long},
348c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?long\s+long\s+int},
3498905a67cSAndy Whitcroft	qr{unsigned},
3508905a67cSAndy Whitcroft	qr{float},
3518905a67cSAndy Whitcroft	qr{double},
3528905a67cSAndy Whitcroft	qr{bool},
3538905a67cSAndy Whitcroft	qr{struct\s+$Ident},
3548905a67cSAndy Whitcroft	qr{union\s+$Ident},
3558905a67cSAndy Whitcroft	qr{enum\s+$Ident},
3568905a67cSAndy Whitcroft	qr{${Ident}_t},
3578905a67cSAndy Whitcroft	qr{${Ident}_handler},
3588905a67cSAndy Whitcroft	qr{${Ident}_handler_fn},
3598905a67cSAndy Whitcroft);
3608716de38SJoe Perchesour @typeListWithAttr = (
3618716de38SJoe Perches	@typeList,
3628716de38SJoe Perches	qr{struct\s+$InitAttribute\s+$Ident},
3638716de38SJoe Perches	qr{union\s+$InitAttribute\s+$Ident},
3648716de38SJoe Perches);
3658716de38SJoe Perches
366c45dcabdSAndy Whitcroftour @modifierList = (
367c45dcabdSAndy Whitcroft	qr{fastcall},
368c45dcabdSAndy Whitcroft);
3698905a67cSAndy Whitcroft
3707840a94cSWolfram Sangour $allowed_asm_includes = qr{(?x:
3717840a94cSWolfram Sang	irq|
3727840a94cSWolfram Sang	memory
3737840a94cSWolfram Sang)};
3747840a94cSWolfram Sang# memory.h: ARM has a custom one
3757840a94cSWolfram Sang
3768905a67cSAndy Whitcroftsub build_types {
377d2172eb5SAndy Whitcroft	my $mods = "(?x:  \n" . join("|\n  ", @modifierList) . "\n)";
378d2172eb5SAndy Whitcroft	my $all = "(?x:  \n" . join("|\n  ", @typeList) . "\n)";
3798716de38SJoe Perches	my $allWithAttr = "(?x:  \n" . join("|\n  ", @typeListWithAttr) . "\n)";
380c8cb2ca3SAndy Whitcroft	$Modifier	= qr{(?:$Attribute|$Sparse|$mods)};
3818905a67cSAndy Whitcroft	$NonptrType	= qr{
382d2172eb5SAndy Whitcroft			(?:$Modifier\s+|const\s+)*
383cf655043SAndy Whitcroft			(?:
3846b48db24SAndy Whitcroft				(?:typeof|__typeof__)\s*\([^\)]*\)|
3858ed22cadSAndy Whitcroft				(?:$typeTypedefs\b)|
386c45dcabdSAndy Whitcroft				(?:${all}\b)
387cf655043SAndy Whitcroft			)
388c8cb2ca3SAndy Whitcroft			(?:\s+$Modifier|\s+const)*
3898905a67cSAndy Whitcroft		  }x;
3908716de38SJoe Perches	$NonptrTypeWithAttr	= qr{
3918716de38SJoe Perches			(?:$Modifier\s+|const\s+)*
3928716de38SJoe Perches			(?:
3938716de38SJoe Perches				(?:typeof|__typeof__)\s*\([^\)]*\)|
3948716de38SJoe Perches				(?:$typeTypedefs\b)|
3958716de38SJoe Perches				(?:${allWithAttr}\b)
3968716de38SJoe Perches			)
3978716de38SJoe Perches			(?:\s+$Modifier|\s+const)*
3988716de38SJoe Perches		  }x;
3998905a67cSAndy Whitcroft	$Type	= qr{
400c45dcabdSAndy Whitcroft			$NonptrType
401b337d8b8SAndy Whitcroft			(?:(?:\s|\*|\[\])+\s*const|(?:\s|\*|\[\])+|(?:\s*\[\s*\])+)?
402c8cb2ca3SAndy Whitcroft			(?:\s+$Inline|\s+$Modifier)*
4038905a67cSAndy Whitcroft		  }x;
4048905a67cSAndy Whitcroft	$Declare	= qr{(?:$Storage\s+)?$Type};
4058905a67cSAndy Whitcroft}
4068905a67cSAndy Whitcroftbuild_types();
4076c72ffaaSAndy Whitcroft
4087d2367afSJoe Perchesour $Typecast	= qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
409d1fe9c09SJoe Perches
410d1fe9c09SJoe Perches# Using $balanced_parens, $LvalOrFunc, or $FuncArg
411d1fe9c09SJoe Perches# requires at least perl version v5.10.0
412d1fe9c09SJoe Perches# Any use must be runtime checked with $^V
413d1fe9c09SJoe Perches
414d1fe9c09SJoe Perchesour $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
415d1fe9c09SJoe Perchesour $LvalOrFunc	= qr{($Lval)\s*($balanced_parens{0,1})\s*};
416d7c76ba7SJoe Perchesour $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant)};
4177d2367afSJoe Perches
4187d2367afSJoe Perchessub deparenthesize {
4197d2367afSJoe Perches	my ($string) = @_;
4207d2367afSJoe Perches	return "" if (!defined($string));
4217d2367afSJoe Perches	$string =~ s@^\s*\(\s*@@g;
4227d2367afSJoe Perches	$string =~ s@\s*\)\s*$@@g;
4237d2367afSJoe Perches	$string =~ s@\s+@ @g;
4247d2367afSJoe Perches	return $string;
4257d2367afSJoe Perches}
4267d2367afSJoe Perches
4273445686aSJoe Perchessub seed_camelcase_file {
4283445686aSJoe Perches	my ($file) = @_;
4293445686aSJoe Perches
4303445686aSJoe Perches	return if (!(-f $file));
4313445686aSJoe Perches
4323445686aSJoe Perches	local $/;
4333445686aSJoe Perches
4343445686aSJoe Perches	open(my $include_file, '<', "$file")
4353445686aSJoe Perches	    or warn "$P: Can't read '$file' $!\n";
4363445686aSJoe Perches	my $text = <$include_file>;
4373445686aSJoe Perches	close($include_file);
4383445686aSJoe Perches
4393445686aSJoe Perches	my @lines = split('\n', $text);
4403445686aSJoe Perches
4413445686aSJoe Perches	foreach my $line (@lines) {
4423445686aSJoe Perches		next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/);
4433445686aSJoe Perches		if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) {
4443445686aSJoe Perches			$camelcase{$1} = 1;
4453445686aSJoe Perches		}
446*d8b07710SJoe Perches	        elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) {
4473445686aSJoe Perches			$camelcase{$1} = 1;
4483445686aSJoe Perches		}
4493445686aSJoe Perches	}
4503445686aSJoe Perches}
4513445686aSJoe Perches
4523445686aSJoe Perchesmy $camelcase_seeded = 0;
4533445686aSJoe Perchessub seed_camelcase_includes {
4543445686aSJoe Perches	return if ($camelcase_seeded);
4553445686aSJoe Perches
4563445686aSJoe Perches	my $files;
457c707a81dSJoe Perches	my $camelcase_cache = "";
458c707a81dSJoe Perches	my @include_files = ();
459c707a81dSJoe Perches
460c707a81dSJoe Perches	$camelcase_seeded = 1;
461351b2a1fSJoe Perches
4623445686aSJoe Perches	if (-d ".git") {
463351b2a1fSJoe Perches		my $git_last_include_commit = `git log --no-merges --pretty=format:"%h%n" -1 -- include`;
464351b2a1fSJoe Perches		chomp $git_last_include_commit;
465c707a81dSJoe Perches		$camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit";
466c707a81dSJoe Perches	} else {
467c707a81dSJoe Perches		my $last_mod_date = 0;
468c707a81dSJoe Perches		$files = `find $root/include -name "*.h"`;
469c707a81dSJoe Perches		@include_files = split('\n', $files);
470c707a81dSJoe Perches		foreach my $file (@include_files) {
471c707a81dSJoe Perches			my $date = POSIX::strftime("%Y%m%d%H%M",
472c707a81dSJoe Perches						   localtime((stat $file)[9]));
473c707a81dSJoe Perches			$last_mod_date = $date if ($last_mod_date < $date);
474c707a81dSJoe Perches		}
475c707a81dSJoe Perches		$camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date";
476c707a81dSJoe Perches	}
477c707a81dSJoe Perches
478c707a81dSJoe Perches	if ($camelcase_cache ne "" && -f $camelcase_cache) {
479c707a81dSJoe Perches		open(my $camelcase_file, '<', "$camelcase_cache")
480c707a81dSJoe Perches		    or warn "$P: Can't read '$camelcase_cache' $!\n";
481351b2a1fSJoe Perches		while (<$camelcase_file>) {
482351b2a1fSJoe Perches			chomp;
483351b2a1fSJoe Perches			$camelcase{$_} = 1;
484351b2a1fSJoe Perches		}
485351b2a1fSJoe Perches		close($camelcase_file);
486351b2a1fSJoe Perches
487351b2a1fSJoe Perches		return;
488351b2a1fSJoe Perches	}
489c707a81dSJoe Perches
490c707a81dSJoe Perches	if (-d ".git") {
491c707a81dSJoe Perches		$files = `git ls-files "include/*.h"`;
492c707a81dSJoe Perches		@include_files = split('\n', $files);
4933445686aSJoe Perches	}
494c707a81dSJoe Perches
4953445686aSJoe Perches	foreach my $file (@include_files) {
4963445686aSJoe Perches		seed_camelcase_file($file);
4973445686aSJoe Perches	}
498351b2a1fSJoe Perches
499c707a81dSJoe Perches	if ($camelcase_cache ne "") {
500351b2a1fSJoe Perches		unlink glob ".checkpatch-camelcase.*";
501c707a81dSJoe Perches		open(my $camelcase_file, '>', "$camelcase_cache")
502c707a81dSJoe Perches		    or warn "$P: Can't write '$camelcase_cache' $!\n";
503351b2a1fSJoe Perches		foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) {
504351b2a1fSJoe Perches			print $camelcase_file ("$_\n");
505351b2a1fSJoe Perches		}
506351b2a1fSJoe Perches		close($camelcase_file);
507351b2a1fSJoe Perches	}
5083445686aSJoe Perches}
5093445686aSJoe Perches
5106c72ffaaSAndy Whitcroft$chk_signoff = 0 if ($file);
5110a920b5bSAndy Whitcroft
51200df344fSAndy Whitcroftmy @rawlines = ();
513c2fdda0dSAndy Whitcroftmy @lines = ();
5143705ce5bSJoe Perchesmy @fixed = ();
515c2fdda0dSAndy Whitcroftmy $vname;
5166c72ffaaSAndy Whitcroftfor my $filename (@ARGV) {
51721caa13cSAndy Whitcroft	my $FILE;
5186c72ffaaSAndy Whitcroft	if ($file) {
51921caa13cSAndy Whitcroft		open($FILE, '-|', "diff -u /dev/null $filename") ||
5206c72ffaaSAndy Whitcroft			die "$P: $filename: diff failed - $!\n";
52121caa13cSAndy Whitcroft	} elsif ($filename eq '-') {
52221caa13cSAndy Whitcroft		open($FILE, '<&STDIN');
5236c72ffaaSAndy Whitcroft	} else {
52421caa13cSAndy Whitcroft		open($FILE, '<', "$filename") ||
5256c72ffaaSAndy Whitcroft			die "$P: $filename: open failed - $!\n";
5266c72ffaaSAndy Whitcroft	}
527c2fdda0dSAndy Whitcroft	if ($filename eq '-') {
528c2fdda0dSAndy Whitcroft		$vname = 'Your patch';
529c2fdda0dSAndy Whitcroft	} else {
530c2fdda0dSAndy Whitcroft		$vname = $filename;
531c2fdda0dSAndy Whitcroft	}
53221caa13cSAndy Whitcroft	while (<$FILE>) {
5330a920b5bSAndy Whitcroft		chomp;
53400df344fSAndy Whitcroft		push(@rawlines, $_);
5356c72ffaaSAndy Whitcroft	}
53621caa13cSAndy Whitcroft	close($FILE);
537c2fdda0dSAndy Whitcroft	if (!process($filename)) {
5380a920b5bSAndy Whitcroft		$exit = 1;
5390a920b5bSAndy Whitcroft	}
54000df344fSAndy Whitcroft	@rawlines = ();
54113214adfSAndy Whitcroft	@lines = ();
5423705ce5bSJoe Perches	@fixed = ();
5430a920b5bSAndy Whitcroft}
5440a920b5bSAndy Whitcroft
5450a920b5bSAndy Whitcroftexit($exit);
5460a920b5bSAndy Whitcroft
5470a920b5bSAndy Whitcroftsub top_of_kernel_tree {
5486c72ffaaSAndy Whitcroft	my ($root) = @_;
5496c72ffaaSAndy Whitcroft
5506c72ffaaSAndy Whitcroft	my @tree_check = (
5516c72ffaaSAndy Whitcroft		"COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
5526c72ffaaSAndy Whitcroft		"README", "Documentation", "arch", "include", "drivers",
5536c72ffaaSAndy Whitcroft		"fs", "init", "ipc", "kernel", "lib", "scripts",
5546c72ffaaSAndy Whitcroft	);
5556c72ffaaSAndy Whitcroft
5566c72ffaaSAndy Whitcroft	foreach my $check (@tree_check) {
5576c72ffaaSAndy Whitcroft		if (! -e $root . '/' . $check) {
5580a920b5bSAndy Whitcroft			return 0;
5590a920b5bSAndy Whitcroft		}
5606c72ffaaSAndy Whitcroft	}
5616c72ffaaSAndy Whitcroft	return 1;
5626c72ffaaSAndy Whitcroft}
5630a920b5bSAndy Whitcroft
56420112475SJoe Perchessub parse_email {
56520112475SJoe Perches	my ($formatted_email) = @_;
56620112475SJoe Perches
56720112475SJoe Perches	my $name = "";
56820112475SJoe Perches	my $address = "";
56920112475SJoe Perches	my $comment = "";
57020112475SJoe Perches
57120112475SJoe Perches	if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
57220112475SJoe Perches		$name = $1;
57320112475SJoe Perches		$address = $2;
57420112475SJoe Perches		$comment = $3 if defined $3;
57520112475SJoe Perches	} elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
57620112475SJoe Perches		$address = $1;
57720112475SJoe Perches		$comment = $2 if defined $2;
57820112475SJoe Perches	} elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
57920112475SJoe Perches		$address = $1;
58020112475SJoe Perches		$comment = $2 if defined $2;
58120112475SJoe Perches		$formatted_email =~ s/$address.*$//;
58220112475SJoe Perches		$name = $formatted_email;
5833705ce5bSJoe Perches		$name = trim($name);
58420112475SJoe Perches		$name =~ s/^\"|\"$//g;
58520112475SJoe Perches		# If there's a name left after stripping spaces and
58620112475SJoe Perches		# leading quotes, and the address doesn't have both
58720112475SJoe Perches		# leading and trailing angle brackets, the address
58820112475SJoe Perches		# is invalid. ie:
58920112475SJoe Perches		#   "joe smith [email protected]" bad
59020112475SJoe Perches		#   "joe smith <[email protected]" bad
59120112475SJoe Perches		if ($name ne "" && $address !~ /^<[^>]+>$/) {
59220112475SJoe Perches			$name = "";
59320112475SJoe Perches			$address = "";
59420112475SJoe Perches			$comment = "";
59520112475SJoe Perches		}
59620112475SJoe Perches	}
59720112475SJoe Perches
5983705ce5bSJoe Perches	$name = trim($name);
59920112475SJoe Perches	$name =~ s/^\"|\"$//g;
6003705ce5bSJoe Perches	$address = trim($address);
60120112475SJoe Perches	$address =~ s/^\<|\>$//g;
60220112475SJoe Perches
60320112475SJoe Perches	if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
60420112475SJoe Perches		$name =~ s/(?<!\\)"/\\"/g; ##escape quotes
60520112475SJoe Perches		$name = "\"$name\"";
60620112475SJoe Perches	}
60720112475SJoe Perches
60820112475SJoe Perches	return ($name, $address, $comment);
60920112475SJoe Perches}
61020112475SJoe Perches
61120112475SJoe Perchessub format_email {
61220112475SJoe Perches	my ($name, $address) = @_;
61320112475SJoe Perches
61420112475SJoe Perches	my $formatted_email;
61520112475SJoe Perches
6163705ce5bSJoe Perches	$name = trim($name);
61720112475SJoe Perches	$name =~ s/^\"|\"$//g;
6183705ce5bSJoe Perches	$address = trim($address);
61920112475SJoe Perches
62020112475SJoe Perches	if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
62120112475SJoe Perches		$name =~ s/(?<!\\)"/\\"/g; ##escape quotes
62220112475SJoe Perches		$name = "\"$name\"";
62320112475SJoe Perches	}
62420112475SJoe Perches
62520112475SJoe Perches	if ("$name" eq "") {
62620112475SJoe Perches		$formatted_email = "$address";
62720112475SJoe Perches	} else {
62820112475SJoe Perches		$formatted_email = "$name <$address>";
62920112475SJoe Perches	}
63020112475SJoe Perches
63120112475SJoe Perches	return $formatted_email;
63220112475SJoe Perches}
63320112475SJoe Perches
634000d1cc1SJoe Perchessub which_conf {
635000d1cc1SJoe Perches	my ($conf) = @_;
636000d1cc1SJoe Perches
637000d1cc1SJoe Perches	foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
638000d1cc1SJoe Perches		if (-e "$path/$conf") {
639000d1cc1SJoe Perches			return "$path/$conf";
640000d1cc1SJoe Perches		}
641000d1cc1SJoe Perches	}
642000d1cc1SJoe Perches
643000d1cc1SJoe Perches	return "";
644000d1cc1SJoe Perches}
645000d1cc1SJoe Perches
6460a920b5bSAndy Whitcroftsub expand_tabs {
6470a920b5bSAndy Whitcroft	my ($str) = @_;
6480a920b5bSAndy Whitcroft
6490a920b5bSAndy Whitcroft	my $res = '';
6500a920b5bSAndy Whitcroft	my $n = 0;
6510a920b5bSAndy Whitcroft	for my $c (split(//, $str)) {
6520a920b5bSAndy Whitcroft		if ($c eq "\t") {
6530a920b5bSAndy Whitcroft			$res .= ' ';
6540a920b5bSAndy Whitcroft			$n++;
6550a920b5bSAndy Whitcroft			for (; ($n % 8) != 0; $n++) {
6560a920b5bSAndy Whitcroft				$res .= ' ';
6570a920b5bSAndy Whitcroft			}
6580a920b5bSAndy Whitcroft			next;
6590a920b5bSAndy Whitcroft		}
6600a920b5bSAndy Whitcroft		$res .= $c;
6610a920b5bSAndy Whitcroft		$n++;
6620a920b5bSAndy Whitcroft	}
6630a920b5bSAndy Whitcroft
6640a920b5bSAndy Whitcroft	return $res;
6650a920b5bSAndy Whitcroft}
6666c72ffaaSAndy Whitcroftsub copy_spacing {
667773647a0SAndy Whitcroft	(my $res = shift) =~ tr/\t/ /c;
6686c72ffaaSAndy Whitcroft	return $res;
6696c72ffaaSAndy Whitcroft}
6700a920b5bSAndy Whitcroft
6714a0df2efSAndy Whitcroftsub line_stats {
6724a0df2efSAndy Whitcroft	my ($line) = @_;
6734a0df2efSAndy Whitcroft
6744a0df2efSAndy Whitcroft	# Drop the diff line leader and expand tabs
6754a0df2efSAndy Whitcroft	$line =~ s/^.//;
6764a0df2efSAndy Whitcroft	$line = expand_tabs($line);
6774a0df2efSAndy Whitcroft
6784a0df2efSAndy Whitcroft	# Pick the indent from the front of the line.
6794a0df2efSAndy Whitcroft	my ($white) = ($line =~ /^(\s*)/);
6804a0df2efSAndy Whitcroft
6814a0df2efSAndy Whitcroft	return (length($line), length($white));
6824a0df2efSAndy Whitcroft}
6834a0df2efSAndy Whitcroft
684773647a0SAndy Whitcroftmy $sanitise_quote = '';
685773647a0SAndy Whitcroft
686773647a0SAndy Whitcroftsub sanitise_line_reset {
687773647a0SAndy Whitcroft	my ($in_comment) = @_;
688773647a0SAndy Whitcroft
689773647a0SAndy Whitcroft	if ($in_comment) {
690773647a0SAndy Whitcroft		$sanitise_quote = '*/';
691773647a0SAndy Whitcroft	} else {
692773647a0SAndy Whitcroft		$sanitise_quote = '';
693773647a0SAndy Whitcroft	}
694773647a0SAndy Whitcroft}
69500df344fSAndy Whitcroftsub sanitise_line {
69600df344fSAndy Whitcroft	my ($line) = @_;
69700df344fSAndy Whitcroft
69800df344fSAndy Whitcroft	my $res = '';
69900df344fSAndy Whitcroft	my $l = '';
70000df344fSAndy Whitcroft
701c2fdda0dSAndy Whitcroft	my $qlen = 0;
702773647a0SAndy Whitcroft	my $off = 0;
703773647a0SAndy Whitcroft	my $c;
70400df344fSAndy Whitcroft
705773647a0SAndy Whitcroft	# Always copy over the diff marker.
706773647a0SAndy Whitcroft	$res = substr($line, 0, 1);
707773647a0SAndy Whitcroft
708773647a0SAndy Whitcroft	for ($off = 1; $off < length($line); $off++) {
709773647a0SAndy Whitcroft		$c = substr($line, $off, 1);
710773647a0SAndy Whitcroft
711773647a0SAndy Whitcroft		# Comments we are wacking completly including the begin
712773647a0SAndy Whitcroft		# and end, all to $;.
713773647a0SAndy Whitcroft		if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
714773647a0SAndy Whitcroft			$sanitise_quote = '*/';
715773647a0SAndy Whitcroft
716773647a0SAndy Whitcroft			substr($res, $off, 2, "$;$;");
717773647a0SAndy Whitcroft			$off++;
71800df344fSAndy Whitcroft			next;
719773647a0SAndy Whitcroft		}
72081bc0e02SAndy Whitcroft		if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
721773647a0SAndy Whitcroft			$sanitise_quote = '';
722773647a0SAndy Whitcroft			substr($res, $off, 2, "$;$;");
723773647a0SAndy Whitcroft			$off++;
724773647a0SAndy Whitcroft			next;
725773647a0SAndy Whitcroft		}
726113f04a8SDaniel Walker		if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
727113f04a8SDaniel Walker			$sanitise_quote = '//';
728113f04a8SDaniel Walker
729113f04a8SDaniel Walker			substr($res, $off, 2, $sanitise_quote);
730113f04a8SDaniel Walker			$off++;
731113f04a8SDaniel Walker			next;
732113f04a8SDaniel Walker		}
733773647a0SAndy Whitcroft
734773647a0SAndy Whitcroft		# A \ in a string means ignore the next character.
735773647a0SAndy Whitcroft		if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
736773647a0SAndy Whitcroft		    $c eq "\\") {
737773647a0SAndy Whitcroft			substr($res, $off, 2, 'XX');
738773647a0SAndy Whitcroft			$off++;
739773647a0SAndy Whitcroft			next;
740773647a0SAndy Whitcroft		}
741773647a0SAndy Whitcroft		# Regular quotes.
742773647a0SAndy Whitcroft		if ($c eq "'" || $c eq '"') {
743773647a0SAndy Whitcroft			if ($sanitise_quote eq '') {
744773647a0SAndy Whitcroft				$sanitise_quote = $c;
745773647a0SAndy Whitcroft
746773647a0SAndy Whitcroft				substr($res, $off, 1, $c);
747773647a0SAndy Whitcroft				next;
748773647a0SAndy Whitcroft			} elsif ($sanitise_quote eq $c) {
749773647a0SAndy Whitcroft				$sanitise_quote = '';
75000df344fSAndy Whitcroft			}
75100df344fSAndy Whitcroft		}
752773647a0SAndy Whitcroft
753fae17daeSAndy Whitcroft		#print "c<$c> SQ<$sanitise_quote>\n";
754773647a0SAndy Whitcroft		if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
755773647a0SAndy Whitcroft			substr($res, $off, 1, $;);
756113f04a8SDaniel Walker		} elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
757113f04a8SDaniel Walker			substr($res, $off, 1, $;);
758773647a0SAndy Whitcroft		} elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
759773647a0SAndy Whitcroft			substr($res, $off, 1, 'X');
76000df344fSAndy Whitcroft		} else {
761773647a0SAndy Whitcroft			substr($res, $off, 1, $c);
76200df344fSAndy Whitcroft		}
763c2fdda0dSAndy Whitcroft	}
764c2fdda0dSAndy Whitcroft
765113f04a8SDaniel Walker	if ($sanitise_quote eq '//') {
766113f04a8SDaniel Walker		$sanitise_quote = '';
767113f04a8SDaniel Walker	}
768113f04a8SDaniel Walker
769c2fdda0dSAndy Whitcroft	# The pathname on a #include may be surrounded by '<' and '>'.
770c45dcabdSAndy Whitcroft	if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
771c2fdda0dSAndy Whitcroft		my $clean = 'X' x length($1);
772c2fdda0dSAndy Whitcroft		$res =~ s@\<.*\>@<$clean>@;
773c2fdda0dSAndy Whitcroft
774c2fdda0dSAndy Whitcroft	# The whole of a #error is a string.
775c45dcabdSAndy Whitcroft	} elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
776c2fdda0dSAndy Whitcroft		my $clean = 'X' x length($1);
777c45dcabdSAndy Whitcroft		$res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
778c2fdda0dSAndy Whitcroft	}
779c2fdda0dSAndy Whitcroft
78000df344fSAndy Whitcroft	return $res;
78100df344fSAndy Whitcroft}
78200df344fSAndy Whitcroft
783a6962d72SJoe Perchessub get_quoted_string {
784a6962d72SJoe Perches	my ($line, $rawline) = @_;
785a6962d72SJoe Perches
786a6962d72SJoe Perches	return "" if ($line !~ m/(\"[X]+\")/g);
787a6962d72SJoe Perches	return substr($rawline, $-[0], $+[0] - $-[0]);
788a6962d72SJoe Perches}
789a6962d72SJoe Perches
7908905a67cSAndy Whitcroftsub ctx_statement_block {
7918905a67cSAndy Whitcroft	my ($linenr, $remain, $off) = @_;
7928905a67cSAndy Whitcroft	my $line = $linenr - 1;
7938905a67cSAndy Whitcroft	my $blk = '';
7948905a67cSAndy Whitcroft	my $soff = $off;
7958905a67cSAndy Whitcroft	my $coff = $off - 1;
796773647a0SAndy Whitcroft	my $coff_set = 0;
7978905a67cSAndy Whitcroft
79813214adfSAndy Whitcroft	my $loff = 0;
79913214adfSAndy Whitcroft
8008905a67cSAndy Whitcroft	my $type = '';
8018905a67cSAndy Whitcroft	my $level = 0;
802a2750645SAndy Whitcroft	my @stack = ();
803cf655043SAndy Whitcroft	my $p;
8048905a67cSAndy Whitcroft	my $c;
8058905a67cSAndy Whitcroft	my $len = 0;
80613214adfSAndy Whitcroft
80713214adfSAndy Whitcroft	my $remainder;
8088905a67cSAndy Whitcroft	while (1) {
809a2750645SAndy Whitcroft		@stack = (['', 0]) if ($#stack == -1);
810a2750645SAndy Whitcroft
811773647a0SAndy Whitcroft		#warn "CSB: blk<$blk> remain<$remain>\n";
8128905a67cSAndy Whitcroft		# If we are about to drop off the end, pull in more
8138905a67cSAndy Whitcroft		# context.
8148905a67cSAndy Whitcroft		if ($off >= $len) {
8158905a67cSAndy Whitcroft			for (; $remain > 0; $line++) {
816dea33496SAndy Whitcroft				last if (!defined $lines[$line]);
817c2fdda0dSAndy Whitcroft				next if ($lines[$line] =~ /^-/);
8188905a67cSAndy Whitcroft				$remain--;
81913214adfSAndy Whitcroft				$loff = $len;
820c2fdda0dSAndy Whitcroft				$blk .= $lines[$line] . "\n";
8218905a67cSAndy Whitcroft				$len = length($blk);
8228905a67cSAndy Whitcroft				$line++;
8238905a67cSAndy Whitcroft				last;
8248905a67cSAndy Whitcroft			}
8258905a67cSAndy Whitcroft			# Bail if there is no further context.
8268905a67cSAndy Whitcroft			#warn "CSB: blk<$blk> off<$off> len<$len>\n";
82713214adfSAndy Whitcroft			if ($off >= $len) {
8288905a67cSAndy Whitcroft				last;
8298905a67cSAndy Whitcroft			}
830f74bd194SAndy Whitcroft			if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
831f74bd194SAndy Whitcroft				$level++;
832f74bd194SAndy Whitcroft				$type = '#';
833f74bd194SAndy Whitcroft			}
8348905a67cSAndy Whitcroft		}
835cf655043SAndy Whitcroft		$p = $c;
8368905a67cSAndy Whitcroft		$c = substr($blk, $off, 1);
83713214adfSAndy Whitcroft		$remainder = substr($blk, $off);
8388905a67cSAndy Whitcroft
839773647a0SAndy Whitcroft		#warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
8404635f4fbSAndy Whitcroft
8414635f4fbSAndy Whitcroft		# Handle nested #if/#else.
8424635f4fbSAndy Whitcroft		if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
8434635f4fbSAndy Whitcroft			push(@stack, [ $type, $level ]);
8444635f4fbSAndy Whitcroft		} elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
8454635f4fbSAndy Whitcroft			($type, $level) = @{$stack[$#stack - 1]};
8464635f4fbSAndy Whitcroft		} elsif ($remainder =~ /^#\s*endif\b/) {
8474635f4fbSAndy Whitcroft			($type, $level) = @{pop(@stack)};
8484635f4fbSAndy Whitcroft		}
8494635f4fbSAndy Whitcroft
8508905a67cSAndy Whitcroft		# Statement ends at the ';' or a close '}' at the
8518905a67cSAndy Whitcroft		# outermost level.
8528905a67cSAndy Whitcroft		if ($level == 0 && $c eq ';') {
8538905a67cSAndy Whitcroft			last;
8548905a67cSAndy Whitcroft		}
8558905a67cSAndy Whitcroft
85613214adfSAndy Whitcroft		# An else is really a conditional as long as its not else if
857773647a0SAndy Whitcroft		if ($level == 0 && $coff_set == 0 &&
858773647a0SAndy Whitcroft				(!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
859773647a0SAndy Whitcroft				$remainder =~ /^(else)(?:\s|{)/ &&
860773647a0SAndy Whitcroft				$remainder !~ /^else\s+if\b/) {
861773647a0SAndy Whitcroft			$coff = $off + length($1) - 1;
862773647a0SAndy Whitcroft			$coff_set = 1;
863773647a0SAndy Whitcroft			#warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
864773647a0SAndy Whitcroft			#warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
86513214adfSAndy Whitcroft		}
86613214adfSAndy Whitcroft
8678905a67cSAndy Whitcroft		if (($type eq '' || $type eq '(') && $c eq '(') {
8688905a67cSAndy Whitcroft			$level++;
8698905a67cSAndy Whitcroft			$type = '(';
8708905a67cSAndy Whitcroft		}
8718905a67cSAndy Whitcroft		if ($type eq '(' && $c eq ')') {
8728905a67cSAndy Whitcroft			$level--;
8738905a67cSAndy Whitcroft			$type = ($level != 0)? '(' : '';
8748905a67cSAndy Whitcroft
8758905a67cSAndy Whitcroft			if ($level == 0 && $coff < $soff) {
8768905a67cSAndy Whitcroft				$coff = $off;
877773647a0SAndy Whitcroft				$coff_set = 1;
878773647a0SAndy Whitcroft				#warn "CSB: mark coff<$coff>\n";
8798905a67cSAndy Whitcroft			}
8808905a67cSAndy Whitcroft		}
8818905a67cSAndy Whitcroft		if (($type eq '' || $type eq '{') && $c eq '{') {
8828905a67cSAndy Whitcroft			$level++;
8838905a67cSAndy Whitcroft			$type = '{';
8848905a67cSAndy Whitcroft		}
8858905a67cSAndy Whitcroft		if ($type eq '{' && $c eq '}') {
8868905a67cSAndy Whitcroft			$level--;
8878905a67cSAndy Whitcroft			$type = ($level != 0)? '{' : '';
8888905a67cSAndy Whitcroft
8898905a67cSAndy Whitcroft			if ($level == 0) {
890b998e001SPatrick Pannuto				if (substr($blk, $off + 1, 1) eq ';') {
891b998e001SPatrick Pannuto					$off++;
892b998e001SPatrick Pannuto				}
8938905a67cSAndy Whitcroft				last;
8948905a67cSAndy Whitcroft			}
8958905a67cSAndy Whitcroft		}
896f74bd194SAndy Whitcroft		# Preprocessor commands end at the newline unless escaped.
897f74bd194SAndy Whitcroft		if ($type eq '#' && $c eq "\n" && $p ne "\\") {
898f74bd194SAndy Whitcroft			$level--;
899f74bd194SAndy Whitcroft			$type = '';
900f74bd194SAndy Whitcroft			$off++;
901f74bd194SAndy Whitcroft			last;
902f74bd194SAndy Whitcroft		}
9038905a67cSAndy Whitcroft		$off++;
9048905a67cSAndy Whitcroft	}
905a3bb97a7SAndy Whitcroft	# We are truly at the end, so shuffle to the next line.
90613214adfSAndy Whitcroft	if ($off == $len) {
907a3bb97a7SAndy Whitcroft		$loff = $len + 1;
90813214adfSAndy Whitcroft		$line++;
90913214adfSAndy Whitcroft		$remain--;
91013214adfSAndy Whitcroft	}
9118905a67cSAndy Whitcroft
9128905a67cSAndy Whitcroft	my $statement = substr($blk, $soff, $off - $soff + 1);
9138905a67cSAndy Whitcroft	my $condition = substr($blk, $soff, $coff - $soff + 1);
9148905a67cSAndy Whitcroft
9158905a67cSAndy Whitcroft	#warn "STATEMENT<$statement>\n";
9168905a67cSAndy Whitcroft	#warn "CONDITION<$condition>\n";
9178905a67cSAndy Whitcroft
918773647a0SAndy Whitcroft	#print "coff<$coff> soff<$off> loff<$loff>\n";
91913214adfSAndy Whitcroft
92013214adfSAndy Whitcroft	return ($statement, $condition,
92113214adfSAndy Whitcroft			$line, $remain + 1, $off - $loff + 1, $level);
92213214adfSAndy Whitcroft}
92313214adfSAndy Whitcroft
924cf655043SAndy Whitcroftsub statement_lines {
925cf655043SAndy Whitcroft	my ($stmt) = @_;
926cf655043SAndy Whitcroft
927cf655043SAndy Whitcroft	# Strip the diff line prefixes and rip blank lines at start and end.
928cf655043SAndy Whitcroft	$stmt =~ s/(^|\n)./$1/g;
929cf655043SAndy Whitcroft	$stmt =~ s/^\s*//;
930cf655043SAndy Whitcroft	$stmt =~ s/\s*$//;
931cf655043SAndy Whitcroft
932cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
933cf655043SAndy Whitcroft
934cf655043SAndy Whitcroft	return $#stmt_lines + 2;
935cf655043SAndy Whitcroft}
936cf655043SAndy Whitcroft
937cf655043SAndy Whitcroftsub statement_rawlines {
938cf655043SAndy Whitcroft	my ($stmt) = @_;
939cf655043SAndy Whitcroft
940cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
941cf655043SAndy Whitcroft
942cf655043SAndy Whitcroft	return $#stmt_lines + 2;
943cf655043SAndy Whitcroft}
944cf655043SAndy Whitcroft
945cf655043SAndy Whitcroftsub statement_block_size {
946cf655043SAndy Whitcroft	my ($stmt) = @_;
947cf655043SAndy Whitcroft
948cf655043SAndy Whitcroft	$stmt =~ s/(^|\n)./$1/g;
949cf655043SAndy Whitcroft	$stmt =~ s/^\s*{//;
950cf655043SAndy Whitcroft	$stmt =~ s/}\s*$//;
951cf655043SAndy Whitcroft	$stmt =~ s/^\s*//;
952cf655043SAndy Whitcroft	$stmt =~ s/\s*$//;
953cf655043SAndy Whitcroft
954cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
955cf655043SAndy Whitcroft	my @stmt_statements = ($stmt =~ /;/g);
956cf655043SAndy Whitcroft
957cf655043SAndy Whitcroft	my $stmt_lines = $#stmt_lines + 2;
958cf655043SAndy Whitcroft	my $stmt_statements = $#stmt_statements + 1;
959cf655043SAndy Whitcroft
960cf655043SAndy Whitcroft	if ($stmt_lines > $stmt_statements) {
961cf655043SAndy Whitcroft		return $stmt_lines;
962cf655043SAndy Whitcroft	} else {
963cf655043SAndy Whitcroft		return $stmt_statements;
964cf655043SAndy Whitcroft	}
965cf655043SAndy Whitcroft}
966cf655043SAndy Whitcroft
96713214adfSAndy Whitcroftsub ctx_statement_full {
96813214adfSAndy Whitcroft	my ($linenr, $remain, $off) = @_;
96913214adfSAndy Whitcroft	my ($statement, $condition, $level);
97013214adfSAndy Whitcroft
97113214adfSAndy Whitcroft	my (@chunks);
97213214adfSAndy Whitcroft
973cf655043SAndy Whitcroft	# Grab the first conditional/block pair.
97413214adfSAndy Whitcroft	($statement, $condition, $linenr, $remain, $off, $level) =
97513214adfSAndy Whitcroft				ctx_statement_block($linenr, $remain, $off);
976773647a0SAndy Whitcroft	#print "F: c<$condition> s<$statement> remain<$remain>\n";
97713214adfSAndy Whitcroft	push(@chunks, [ $condition, $statement ]);
978cf655043SAndy Whitcroft	if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
979cf655043SAndy Whitcroft		return ($level, $linenr, @chunks);
980cf655043SAndy Whitcroft	}
981cf655043SAndy Whitcroft
982cf655043SAndy Whitcroft	# Pull in the following conditional/block pairs and see if they
983cf655043SAndy Whitcroft	# could continue the statement.
984cf655043SAndy Whitcroft	for (;;) {
98513214adfSAndy Whitcroft		($statement, $condition, $linenr, $remain, $off, $level) =
98613214adfSAndy Whitcroft				ctx_statement_block($linenr, $remain, $off);
987cf655043SAndy Whitcroft		#print "C: c<$condition> s<$statement> remain<$remain>\n";
988773647a0SAndy Whitcroft		last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
989cf655043SAndy Whitcroft		#print "C: push\n";
990cf655043SAndy Whitcroft		push(@chunks, [ $condition, $statement ]);
99113214adfSAndy Whitcroft	}
99213214adfSAndy Whitcroft
99313214adfSAndy Whitcroft	return ($level, $linenr, @chunks);
9948905a67cSAndy Whitcroft}
9958905a67cSAndy Whitcroft
9964a0df2efSAndy Whitcroftsub ctx_block_get {
997f0a594c1SAndy Whitcroft	my ($linenr, $remain, $outer, $open, $close, $off) = @_;
9984a0df2efSAndy Whitcroft	my $line;
9994a0df2efSAndy Whitcroft	my $start = $linenr - 1;
10004a0df2efSAndy Whitcroft	my $blk = '';
10014a0df2efSAndy Whitcroft	my @o;
10024a0df2efSAndy Whitcroft	my @c;
10034a0df2efSAndy Whitcroft	my @res = ();
10044a0df2efSAndy Whitcroft
1005f0a594c1SAndy Whitcroft	my $level = 0;
10064635f4fbSAndy Whitcroft	my @stack = ($level);
100700df344fSAndy Whitcroft	for ($line = $start; $remain > 0; $line++) {
100800df344fSAndy Whitcroft		next if ($rawlines[$line] =~ /^-/);
100900df344fSAndy Whitcroft		$remain--;
101000df344fSAndy Whitcroft
101100df344fSAndy Whitcroft		$blk .= $rawlines[$line];
10124635f4fbSAndy Whitcroft
10134635f4fbSAndy Whitcroft		# Handle nested #if/#else.
101401464f30SAndy Whitcroft		if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
10154635f4fbSAndy Whitcroft			push(@stack, $level);
101601464f30SAndy Whitcroft		} elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
10174635f4fbSAndy Whitcroft			$level = $stack[$#stack - 1];
101801464f30SAndy Whitcroft		} elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
10194635f4fbSAndy Whitcroft			$level = pop(@stack);
10204635f4fbSAndy Whitcroft		}
10214635f4fbSAndy Whitcroft
102201464f30SAndy Whitcroft		foreach my $c (split(//, $lines[$line])) {
1023f0a594c1SAndy Whitcroft			##print "C<$c>L<$level><$open$close>O<$off>\n";
1024f0a594c1SAndy Whitcroft			if ($off > 0) {
1025f0a594c1SAndy Whitcroft				$off--;
1026f0a594c1SAndy Whitcroft				next;
1027f0a594c1SAndy Whitcroft			}
10284a0df2efSAndy Whitcroft
1029f0a594c1SAndy Whitcroft			if ($c eq $close && $level > 0) {
1030f0a594c1SAndy Whitcroft				$level--;
1031f0a594c1SAndy Whitcroft				last if ($level == 0);
1032f0a594c1SAndy Whitcroft			} elsif ($c eq $open) {
1033f0a594c1SAndy Whitcroft				$level++;
1034f0a594c1SAndy Whitcroft			}
1035f0a594c1SAndy Whitcroft		}
10364a0df2efSAndy Whitcroft
1037f0a594c1SAndy Whitcroft		if (!$outer || $level <= 1) {
103800df344fSAndy Whitcroft			push(@res, $rawlines[$line]);
10394a0df2efSAndy Whitcroft		}
10404a0df2efSAndy Whitcroft
1041f0a594c1SAndy Whitcroft		last if ($level == 0);
10424a0df2efSAndy Whitcroft	}
10434a0df2efSAndy Whitcroft
1044f0a594c1SAndy Whitcroft	return ($level, @res);
10454a0df2efSAndy Whitcroft}
10464a0df2efSAndy Whitcroftsub ctx_block_outer {
10474a0df2efSAndy Whitcroft	my ($linenr, $remain) = @_;
10484a0df2efSAndy Whitcroft
1049f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
1050f0a594c1SAndy Whitcroft	return @r;
10514a0df2efSAndy Whitcroft}
10524a0df2efSAndy Whitcroftsub ctx_block {
10534a0df2efSAndy Whitcroft	my ($linenr, $remain) = @_;
10544a0df2efSAndy Whitcroft
1055f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1056f0a594c1SAndy Whitcroft	return @r;
1057653d4876SAndy Whitcroft}
1058653d4876SAndy Whitcroftsub ctx_statement {
1059f0a594c1SAndy Whitcroft	my ($linenr, $remain, $off) = @_;
1060f0a594c1SAndy Whitcroft
1061f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1062f0a594c1SAndy Whitcroft	return @r;
1063f0a594c1SAndy Whitcroft}
1064f0a594c1SAndy Whitcroftsub ctx_block_level {
1065653d4876SAndy Whitcroft	my ($linenr, $remain) = @_;
1066653d4876SAndy Whitcroft
1067f0a594c1SAndy Whitcroft	return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
10684a0df2efSAndy Whitcroft}
10699c0ca6f9SAndy Whitcroftsub ctx_statement_level {
10709c0ca6f9SAndy Whitcroft	my ($linenr, $remain, $off) = @_;
10719c0ca6f9SAndy Whitcroft
10729c0ca6f9SAndy Whitcroft	return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
10739c0ca6f9SAndy Whitcroft}
10744a0df2efSAndy Whitcroft
10754a0df2efSAndy Whitcroftsub ctx_locate_comment {
10764a0df2efSAndy Whitcroft	my ($first_line, $end_line) = @_;
10774a0df2efSAndy Whitcroft
10784a0df2efSAndy Whitcroft	# Catch a comment on the end of the line itself.
1079beae6332SAndy Whitcroft	my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
10804a0df2efSAndy Whitcroft	return $current_comment if (defined $current_comment);
10814a0df2efSAndy Whitcroft
10824a0df2efSAndy Whitcroft	# Look through the context and try and figure out if there is a
10834a0df2efSAndy Whitcroft	# comment.
10844a0df2efSAndy Whitcroft	my $in_comment = 0;
10854a0df2efSAndy Whitcroft	$current_comment = '';
10864a0df2efSAndy Whitcroft	for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
108700df344fSAndy Whitcroft		my $line = $rawlines[$linenr - 1];
108800df344fSAndy Whitcroft		#warn "           $line\n";
10894a0df2efSAndy Whitcroft		if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
10904a0df2efSAndy Whitcroft			$in_comment = 1;
10914a0df2efSAndy Whitcroft		}
10924a0df2efSAndy Whitcroft		if ($line =~ m@/\*@) {
10934a0df2efSAndy Whitcroft			$in_comment = 1;
10944a0df2efSAndy Whitcroft		}
10954a0df2efSAndy Whitcroft		if (!$in_comment && $current_comment ne '') {
10964a0df2efSAndy Whitcroft			$current_comment = '';
10974a0df2efSAndy Whitcroft		}
10984a0df2efSAndy Whitcroft		$current_comment .= $line . "\n" if ($in_comment);
10994a0df2efSAndy Whitcroft		if ($line =~ m@\*/@) {
11004a0df2efSAndy Whitcroft			$in_comment = 0;
11014a0df2efSAndy Whitcroft		}
11024a0df2efSAndy Whitcroft	}
11034a0df2efSAndy Whitcroft
11044a0df2efSAndy Whitcroft	chomp($current_comment);
11054a0df2efSAndy Whitcroft	return($current_comment);
11064a0df2efSAndy Whitcroft}
11074a0df2efSAndy Whitcroftsub ctx_has_comment {
11084a0df2efSAndy Whitcroft	my ($first_line, $end_line) = @_;
11094a0df2efSAndy Whitcroft	my $cmt = ctx_locate_comment($first_line, $end_line);
11104a0df2efSAndy Whitcroft
111100df344fSAndy Whitcroft	##print "LINE: $rawlines[$end_line - 1 ]\n";
11124a0df2efSAndy Whitcroft	##print "CMMT: $cmt\n";
11134a0df2efSAndy Whitcroft
11144a0df2efSAndy Whitcroft	return ($cmt ne '');
11154a0df2efSAndy Whitcroft}
11164a0df2efSAndy Whitcroft
11174d001e4dSAndy Whitcroftsub raw_line {
11184d001e4dSAndy Whitcroft	my ($linenr, $cnt) = @_;
11194d001e4dSAndy Whitcroft
11204d001e4dSAndy Whitcroft	my $offset = $linenr - 1;
11214d001e4dSAndy Whitcroft	$cnt++;
11224d001e4dSAndy Whitcroft
11234d001e4dSAndy Whitcroft	my $line;
11244d001e4dSAndy Whitcroft	while ($cnt) {
11254d001e4dSAndy Whitcroft		$line = $rawlines[$offset++];
11264d001e4dSAndy Whitcroft		next if (defined($line) && $line =~ /^-/);
11274d001e4dSAndy Whitcroft		$cnt--;
11284d001e4dSAndy Whitcroft	}
11294d001e4dSAndy Whitcroft
11304d001e4dSAndy Whitcroft	return $line;
11314d001e4dSAndy Whitcroft}
11324d001e4dSAndy Whitcroft
11330a920b5bSAndy Whitcroftsub cat_vet {
11340a920b5bSAndy Whitcroft	my ($vet) = @_;
11359c0ca6f9SAndy Whitcroft	my ($res, $coded);
11360a920b5bSAndy Whitcroft
11379c0ca6f9SAndy Whitcroft	$res = '';
11386c72ffaaSAndy Whitcroft	while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
11396c72ffaaSAndy Whitcroft		$res .= $1;
11406c72ffaaSAndy Whitcroft		if ($2 ne '') {
11419c0ca6f9SAndy Whitcroft			$coded = sprintf("^%c", unpack('C', $2) + 64);
11426c72ffaaSAndy Whitcroft			$res .= $coded;
11436c72ffaaSAndy Whitcroft		}
11449c0ca6f9SAndy Whitcroft	}
11459c0ca6f9SAndy Whitcroft	$res =~ s/$/\$/;
11460a920b5bSAndy Whitcroft
11479c0ca6f9SAndy Whitcroft	return $res;
11480a920b5bSAndy Whitcroft}
11490a920b5bSAndy Whitcroft
1150c2fdda0dSAndy Whitcroftmy $av_preprocessor = 0;
1151cf655043SAndy Whitcroftmy $av_pending;
1152c2fdda0dSAndy Whitcroftmy @av_paren_type;
11531f65f947SAndy Whitcroftmy $av_pend_colon;
1154c2fdda0dSAndy Whitcroft
1155c2fdda0dSAndy Whitcroftsub annotate_reset {
1156c2fdda0dSAndy Whitcroft	$av_preprocessor = 0;
1157cf655043SAndy Whitcroft	$av_pending = '_';
1158cf655043SAndy Whitcroft	@av_paren_type = ('E');
11591f65f947SAndy Whitcroft	$av_pend_colon = 'O';
1160c2fdda0dSAndy Whitcroft}
1161c2fdda0dSAndy Whitcroft
11626c72ffaaSAndy Whitcroftsub annotate_values {
11636c72ffaaSAndy Whitcroft	my ($stream, $type) = @_;
11646c72ffaaSAndy Whitcroft
11656c72ffaaSAndy Whitcroft	my $res;
11661f65f947SAndy Whitcroft	my $var = '_' x length($stream);
11676c72ffaaSAndy Whitcroft	my $cur = $stream;
11686c72ffaaSAndy Whitcroft
1169c2fdda0dSAndy Whitcroft	print "$stream\n" if ($dbg_values > 1);
11706c72ffaaSAndy Whitcroft
11716c72ffaaSAndy Whitcroft	while (length($cur)) {
1172773647a0SAndy Whitcroft		@av_paren_type = ('E') if ($#av_paren_type < 0);
1173cf655043SAndy Whitcroft		print " <" . join('', @av_paren_type) .
1174171ae1a4SAndy Whitcroft				"> <$type> <$av_pending>" if ($dbg_values > 1);
11756c72ffaaSAndy Whitcroft		if ($cur =~ /^(\s+)/o) {
1176c2fdda0dSAndy Whitcroft			print "WS($1)\n" if ($dbg_values > 1);
1177c2fdda0dSAndy Whitcroft			if ($1 =~ /\n/ && $av_preprocessor) {
1178cf655043SAndy Whitcroft				$type = pop(@av_paren_type);
1179c2fdda0dSAndy Whitcroft				$av_preprocessor = 0;
11806c72ffaaSAndy Whitcroft			}
11816c72ffaaSAndy Whitcroft
1182c023e473SFlorian Mickler		} elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
11839446ef56SAndy Whitcroft			print "CAST($1)\n" if ($dbg_values > 1);
11849446ef56SAndy Whitcroft			push(@av_paren_type, $type);
1185addcdceaSAndy Whitcroft			$type = 'c';
11869446ef56SAndy Whitcroft
1187e91b6e26SAndy Whitcroft		} elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
1188c2fdda0dSAndy Whitcroft			print "DECLARE($1)\n" if ($dbg_values > 1);
11896c72ffaaSAndy Whitcroft			$type = 'T';
11906c72ffaaSAndy Whitcroft
1191389a2fe5SAndy Whitcroft		} elsif ($cur =~ /^($Modifier)\s*/) {
1192389a2fe5SAndy Whitcroft			print "MODIFIER($1)\n" if ($dbg_values > 1);
1193389a2fe5SAndy Whitcroft			$type = 'T';
1194389a2fe5SAndy Whitcroft
1195c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
1196171ae1a4SAndy Whitcroft			print "DEFINE($1,$2)\n" if ($dbg_values > 1);
1197c2fdda0dSAndy Whitcroft			$av_preprocessor = 1;
1198171ae1a4SAndy Whitcroft			push(@av_paren_type, $type);
1199171ae1a4SAndy Whitcroft			if ($2 ne '') {
1200cf655043SAndy Whitcroft				$av_pending = 'N';
1201171ae1a4SAndy Whitcroft			}
1202171ae1a4SAndy Whitcroft			$type = 'E';
1203171ae1a4SAndy Whitcroft
1204c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
1205171ae1a4SAndy Whitcroft			print "UNDEF($1)\n" if ($dbg_values > 1);
1206171ae1a4SAndy Whitcroft			$av_preprocessor = 1;
1207171ae1a4SAndy Whitcroft			push(@av_paren_type, $type);
12086c72ffaaSAndy Whitcroft
1209c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
1210cf655043SAndy Whitcroft			print "PRE_START($1)\n" if ($dbg_values > 1);
1211c2fdda0dSAndy Whitcroft			$av_preprocessor = 1;
1212cf655043SAndy Whitcroft
1213cf655043SAndy Whitcroft			push(@av_paren_type, $type);
1214cf655043SAndy Whitcroft			push(@av_paren_type, $type);
1215171ae1a4SAndy Whitcroft			$type = 'E';
1216cf655043SAndy Whitcroft
1217c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
1218cf655043SAndy Whitcroft			print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1219cf655043SAndy Whitcroft			$av_preprocessor = 1;
1220cf655043SAndy Whitcroft
1221cf655043SAndy Whitcroft			push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1222cf655043SAndy Whitcroft
1223171ae1a4SAndy Whitcroft			$type = 'E';
1224cf655043SAndy Whitcroft
1225c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:endif))/o) {
1226cf655043SAndy Whitcroft			print "PRE_END($1)\n" if ($dbg_values > 1);
1227cf655043SAndy Whitcroft
1228cf655043SAndy Whitcroft			$av_preprocessor = 1;
1229cf655043SAndy Whitcroft
1230cf655043SAndy Whitcroft			# Assume all arms of the conditional end as this
1231cf655043SAndy Whitcroft			# one does, and continue as if the #endif was not here.
1232cf655043SAndy Whitcroft			pop(@av_paren_type);
1233cf655043SAndy Whitcroft			push(@av_paren_type, $type);
1234171ae1a4SAndy Whitcroft			$type = 'E';
12356c72ffaaSAndy Whitcroft
12366c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\\\n)/o) {
1237c2fdda0dSAndy Whitcroft			print "PRECONT($1)\n" if ($dbg_values > 1);
12386c72ffaaSAndy Whitcroft
1239171ae1a4SAndy Whitcroft		} elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1240171ae1a4SAndy Whitcroft			print "ATTR($1)\n" if ($dbg_values > 1);
1241171ae1a4SAndy Whitcroft			$av_pending = $type;
1242171ae1a4SAndy Whitcroft			$type = 'N';
1243171ae1a4SAndy Whitcroft
12446c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
1245c2fdda0dSAndy Whitcroft			print "SIZEOF($1)\n" if ($dbg_values > 1);
12466c72ffaaSAndy Whitcroft			if (defined $2) {
1247cf655043SAndy Whitcroft				$av_pending = 'V';
12486c72ffaaSAndy Whitcroft			}
12496c72ffaaSAndy Whitcroft			$type = 'N';
12506c72ffaaSAndy Whitcroft
125114b111c1SAndy Whitcroft		} elsif ($cur =~ /^(if|while|for)\b/o) {
1252c2fdda0dSAndy Whitcroft			print "COND($1)\n" if ($dbg_values > 1);
125314b111c1SAndy Whitcroft			$av_pending = 'E';
12546c72ffaaSAndy Whitcroft			$type = 'N';
12556c72ffaaSAndy Whitcroft
12561f65f947SAndy Whitcroft		} elsif ($cur =~/^(case)/o) {
12571f65f947SAndy Whitcroft			print "CASE($1)\n" if ($dbg_values > 1);
12581f65f947SAndy Whitcroft			$av_pend_colon = 'C';
12591f65f947SAndy Whitcroft			$type = 'N';
12601f65f947SAndy Whitcroft
126114b111c1SAndy Whitcroft		} elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
1262c2fdda0dSAndy Whitcroft			print "KEYWORD($1)\n" if ($dbg_values > 1);
12636c72ffaaSAndy Whitcroft			$type = 'N';
12646c72ffaaSAndy Whitcroft
12656c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\()/o) {
1266c2fdda0dSAndy Whitcroft			print "PAREN('$1')\n" if ($dbg_values > 1);
1267cf655043SAndy Whitcroft			push(@av_paren_type, $av_pending);
1268cf655043SAndy Whitcroft			$av_pending = '_';
12696c72ffaaSAndy Whitcroft			$type = 'N';
12706c72ffaaSAndy Whitcroft
12716c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\))/o) {
1272cf655043SAndy Whitcroft			my $new_type = pop(@av_paren_type);
1273cf655043SAndy Whitcroft			if ($new_type ne '_') {
1274cf655043SAndy Whitcroft				$type = $new_type;
1275c2fdda0dSAndy Whitcroft				print "PAREN('$1') -> $type\n"
1276c2fdda0dSAndy Whitcroft							if ($dbg_values > 1);
12776c72ffaaSAndy Whitcroft			} else {
1278c2fdda0dSAndy Whitcroft				print "PAREN('$1')\n" if ($dbg_values > 1);
12796c72ffaaSAndy Whitcroft			}
12806c72ffaaSAndy Whitcroft
1281c8cb2ca3SAndy Whitcroft		} elsif ($cur =~ /^($Ident)\s*\(/o) {
1282c2fdda0dSAndy Whitcroft			print "FUNC($1)\n" if ($dbg_values > 1);
1283c8cb2ca3SAndy Whitcroft			$type = 'V';
1284cf655043SAndy Whitcroft			$av_pending = 'V';
12856c72ffaaSAndy Whitcroft
12868e761b04SAndy Whitcroft		} elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
12878e761b04SAndy Whitcroft			if (defined $2 && $type eq 'C' || $type eq 'T') {
12881f65f947SAndy Whitcroft				$av_pend_colon = 'B';
12898e761b04SAndy Whitcroft			} elsif ($type eq 'E') {
12908e761b04SAndy Whitcroft				$av_pend_colon = 'L';
12911f65f947SAndy Whitcroft			}
12921f65f947SAndy Whitcroft			print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
12931f65f947SAndy Whitcroft			$type = 'V';
12941f65f947SAndy Whitcroft
12956c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Ident|$Constant)/o) {
1296c2fdda0dSAndy Whitcroft			print "IDENT($1)\n" if ($dbg_values > 1);
12976c72ffaaSAndy Whitcroft			$type = 'V';
12986c72ffaaSAndy Whitcroft
12996c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Assignment)/o) {
1300c2fdda0dSAndy Whitcroft			print "ASSIGN($1)\n" if ($dbg_values > 1);
13016c72ffaaSAndy Whitcroft			$type = 'N';
13026c72ffaaSAndy Whitcroft
1303cf655043SAndy Whitcroft		} elsif ($cur =~/^(;|{|})/) {
1304c2fdda0dSAndy Whitcroft			print "END($1)\n" if ($dbg_values > 1);
130513214adfSAndy Whitcroft			$type = 'E';
13061f65f947SAndy Whitcroft			$av_pend_colon = 'O';
130713214adfSAndy Whitcroft
13088e761b04SAndy Whitcroft		} elsif ($cur =~/^(,)/) {
13098e761b04SAndy Whitcroft			print "COMMA($1)\n" if ($dbg_values > 1);
13108e761b04SAndy Whitcroft			$type = 'C';
13118e761b04SAndy Whitcroft
13121f65f947SAndy Whitcroft		} elsif ($cur =~ /^(\?)/o) {
13131f65f947SAndy Whitcroft			print "QUESTION($1)\n" if ($dbg_values > 1);
13141f65f947SAndy Whitcroft			$type = 'N';
13151f65f947SAndy Whitcroft
13161f65f947SAndy Whitcroft		} elsif ($cur =~ /^(:)/o) {
13171f65f947SAndy Whitcroft			print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
13181f65f947SAndy Whitcroft
13191f65f947SAndy Whitcroft			substr($var, length($res), 1, $av_pend_colon);
13201f65f947SAndy Whitcroft			if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
13211f65f947SAndy Whitcroft				$type = 'E';
13221f65f947SAndy Whitcroft			} else {
13231f65f947SAndy Whitcroft				$type = 'N';
13241f65f947SAndy Whitcroft			}
13251f65f947SAndy Whitcroft			$av_pend_colon = 'O';
13261f65f947SAndy Whitcroft
13278e761b04SAndy Whitcroft		} elsif ($cur =~ /^(\[)/o) {
132813214adfSAndy Whitcroft			print "CLOSE($1)\n" if ($dbg_values > 1);
13296c72ffaaSAndy Whitcroft			$type = 'N';
13306c72ffaaSAndy Whitcroft
13310d413866SAndy Whitcroft		} elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
133274048ed8SAndy Whitcroft			my $variant;
133374048ed8SAndy Whitcroft
133474048ed8SAndy Whitcroft			print "OPV($1)\n" if ($dbg_values > 1);
133574048ed8SAndy Whitcroft			if ($type eq 'V') {
133674048ed8SAndy Whitcroft				$variant = 'B';
133774048ed8SAndy Whitcroft			} else {
133874048ed8SAndy Whitcroft				$variant = 'U';
133974048ed8SAndy Whitcroft			}
134074048ed8SAndy Whitcroft
134174048ed8SAndy Whitcroft			substr($var, length($res), 1, $variant);
134274048ed8SAndy Whitcroft			$type = 'N';
134374048ed8SAndy Whitcroft
13446c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Operators)/o) {
1345c2fdda0dSAndy Whitcroft			print "OP($1)\n" if ($dbg_values > 1);
13466c72ffaaSAndy Whitcroft			if ($1 ne '++' && $1 ne '--') {
13476c72ffaaSAndy Whitcroft				$type = 'N';
13486c72ffaaSAndy Whitcroft			}
13496c72ffaaSAndy Whitcroft
13506c72ffaaSAndy Whitcroft		} elsif ($cur =~ /(^.)/o) {
1351c2fdda0dSAndy Whitcroft			print "C($1)\n" if ($dbg_values > 1);
13526c72ffaaSAndy Whitcroft		}
13536c72ffaaSAndy Whitcroft		if (defined $1) {
13546c72ffaaSAndy Whitcroft			$cur = substr($cur, length($1));
13556c72ffaaSAndy Whitcroft			$res .= $type x length($1);
13566c72ffaaSAndy Whitcroft		}
13576c72ffaaSAndy Whitcroft	}
13586c72ffaaSAndy Whitcroft
13591f65f947SAndy Whitcroft	return ($res, $var);
13606c72ffaaSAndy Whitcroft}
13616c72ffaaSAndy Whitcroft
13628905a67cSAndy Whitcroftsub possible {
136313214adfSAndy Whitcroft	my ($possible, $line) = @_;
13649a974fdbSAndy Whitcroft	my $notPermitted = qr{(?:
13650776e594SAndy Whitcroft		^(?:
13660776e594SAndy Whitcroft			$Modifier|
13670776e594SAndy Whitcroft			$Storage|
13680776e594SAndy Whitcroft			$Type|
13699a974fdbSAndy Whitcroft			DEFINE_\S+
13709a974fdbSAndy Whitcroft		)$|
13719a974fdbSAndy Whitcroft		^(?:
13720776e594SAndy Whitcroft			goto|
13730776e594SAndy Whitcroft			return|
13740776e594SAndy Whitcroft			case|
13750776e594SAndy Whitcroft			else|
13760776e594SAndy Whitcroft			asm|__asm__|
137789a88353SAndy Whitcroft			do|
137889a88353SAndy Whitcroft			\#|
137989a88353SAndy Whitcroft			\#\#|
13809a974fdbSAndy Whitcroft		)(?:\s|$)|
13810776e594SAndy Whitcroft		^(?:typedef|struct|enum)\b
13829a974fdbSAndy Whitcroft	    )}x;
13839a974fdbSAndy Whitcroft	warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
13849a974fdbSAndy Whitcroft	if ($possible !~ $notPermitted) {
1385c45dcabdSAndy Whitcroft		# Check for modifiers.
1386c45dcabdSAndy Whitcroft		$possible =~ s/\s*$Storage\s*//g;
1387c45dcabdSAndy Whitcroft		$possible =~ s/\s*$Sparse\s*//g;
1388c45dcabdSAndy Whitcroft		if ($possible =~ /^\s*$/) {
1389c45dcabdSAndy Whitcroft
1390c45dcabdSAndy Whitcroft		} elsif ($possible =~ /\s/) {
1391c45dcabdSAndy Whitcroft			$possible =~ s/\s*$Type\s*//g;
1392d2506586SAndy Whitcroft			for my $modifier (split(' ', $possible)) {
13939a974fdbSAndy Whitcroft				if ($modifier !~ $notPermitted) {
1394d2506586SAndy Whitcroft					warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1395d2506586SAndy Whitcroft					push(@modifierList, $modifier);
1396d2506586SAndy Whitcroft				}
13979a974fdbSAndy Whitcroft			}
1398c45dcabdSAndy Whitcroft
1399c45dcabdSAndy Whitcroft		} else {
140013214adfSAndy Whitcroft			warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
14018905a67cSAndy Whitcroft			push(@typeList, $possible);
1402c45dcabdSAndy Whitcroft		}
14038905a67cSAndy Whitcroft		build_types();
14040776e594SAndy Whitcroft	} else {
14050776e594SAndy Whitcroft		warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
14068905a67cSAndy Whitcroft	}
14078905a67cSAndy Whitcroft}
14088905a67cSAndy Whitcroft
14096c72ffaaSAndy Whitcroftmy $prefix = '';
14106c72ffaaSAndy Whitcroft
1411000d1cc1SJoe Perchessub show_type {
141291bfe484SJoe Perches	return defined $use_type{$_[0]} if (scalar keys %use_type > 0);
141391bfe484SJoe Perches
1414000d1cc1SJoe Perches	return !defined $ignore_type{$_[0]};
1415000d1cc1SJoe Perches}
1416000d1cc1SJoe Perches
1417f0a594c1SAndy Whitcroftsub report {
1418000d1cc1SJoe Perches	if (!show_type($_[1]) ||
1419000d1cc1SJoe Perches	    (defined $tst_only && $_[2] !~ /\Q$tst_only\E/)) {
1420773647a0SAndy Whitcroft		return 0;
1421773647a0SAndy Whitcroft	}
1422000d1cc1SJoe Perches	my $line;
1423000d1cc1SJoe Perches	if ($show_types) {
1424000d1cc1SJoe Perches		$line = "$prefix$_[0]:$_[1]: $_[2]\n";
1425000d1cc1SJoe Perches	} else {
1426000d1cc1SJoe Perches		$line = "$prefix$_[0]: $_[2]\n";
1427000d1cc1SJoe Perches	}
14288905a67cSAndy Whitcroft	$line = (split('\n', $line))[0] . "\n" if ($terse);
14298905a67cSAndy Whitcroft
143013214adfSAndy Whitcroft	push(our @report, $line);
1431773647a0SAndy Whitcroft
1432773647a0SAndy Whitcroft	return 1;
1433f0a594c1SAndy Whitcroft}
1434f0a594c1SAndy Whitcroftsub report_dump {
143513214adfSAndy Whitcroft	our @report;
1436f0a594c1SAndy Whitcroft}
1437000d1cc1SJoe Perches
1438de7d4f0eSAndy Whitcroftsub ERROR {
1439000d1cc1SJoe Perches	if (report("ERROR", $_[0], $_[1])) {
1440de7d4f0eSAndy Whitcroft		our $clean = 0;
14416c72ffaaSAndy Whitcroft		our $cnt_error++;
14423705ce5bSJoe Perches		return 1;
1443de7d4f0eSAndy Whitcroft	}
14443705ce5bSJoe Perches	return 0;
1445773647a0SAndy Whitcroft}
1446de7d4f0eSAndy Whitcroftsub WARN {
1447000d1cc1SJoe Perches	if (report("WARNING", $_[0], $_[1])) {
1448de7d4f0eSAndy Whitcroft		our $clean = 0;
14496c72ffaaSAndy Whitcroft		our $cnt_warn++;
14503705ce5bSJoe Perches		return 1;
1451de7d4f0eSAndy Whitcroft	}
14523705ce5bSJoe Perches	return 0;
1453773647a0SAndy Whitcroft}
1454de7d4f0eSAndy Whitcroftsub CHK {
1455000d1cc1SJoe Perches	if ($check && report("CHECK", $_[0], $_[1])) {
1456de7d4f0eSAndy Whitcroft		our $clean = 0;
14576c72ffaaSAndy Whitcroft		our $cnt_chk++;
14583705ce5bSJoe Perches		return 1;
14596c72ffaaSAndy Whitcroft	}
14603705ce5bSJoe Perches	return 0;
1461de7d4f0eSAndy Whitcroft}
1462de7d4f0eSAndy Whitcroft
14636ecd9674SAndy Whitcroftsub check_absolute_file {
14646ecd9674SAndy Whitcroft	my ($absolute, $herecurr) = @_;
14656ecd9674SAndy Whitcroft	my $file = $absolute;
14666ecd9674SAndy Whitcroft
14676ecd9674SAndy Whitcroft	##print "absolute<$absolute>\n";
14686ecd9674SAndy Whitcroft
14696ecd9674SAndy Whitcroft	# See if any suffix of this path is a path within the tree.
14706ecd9674SAndy Whitcroft	while ($file =~ s@^[^/]*/@@) {
14716ecd9674SAndy Whitcroft		if (-f "$root/$file") {
14726ecd9674SAndy Whitcroft			##print "file<$file>\n";
14736ecd9674SAndy Whitcroft			last;
14746ecd9674SAndy Whitcroft		}
14756ecd9674SAndy Whitcroft	}
14766ecd9674SAndy Whitcroft	if (! -f _)  {
14776ecd9674SAndy Whitcroft		return 0;
14786ecd9674SAndy Whitcroft	}
14796ecd9674SAndy Whitcroft
14806ecd9674SAndy Whitcroft	# It is, so see if the prefix is acceptable.
14816ecd9674SAndy Whitcroft	my $prefix = $absolute;
14826ecd9674SAndy Whitcroft	substr($prefix, -length($file)) = '';
14836ecd9674SAndy Whitcroft
14846ecd9674SAndy Whitcroft	##print "prefix<$prefix>\n";
14856ecd9674SAndy Whitcroft	if ($prefix ne ".../") {
1486000d1cc1SJoe Perches		WARN("USE_RELATIVE_PATH",
1487000d1cc1SJoe Perches		     "use relative pathname instead of absolute in changelog text\n" . $herecurr);
14886ecd9674SAndy Whitcroft	}
14896ecd9674SAndy Whitcroft}
14906ecd9674SAndy Whitcroft
14913705ce5bSJoe Perchessub trim {
14923705ce5bSJoe Perches	my ($string) = @_;
14933705ce5bSJoe Perches
1494b34c648bSJoe Perches	$string =~ s/^\s+|\s+$//g;
1495b34c648bSJoe Perches
1496b34c648bSJoe Perches	return $string;
1497b34c648bSJoe Perches}
1498b34c648bSJoe Perches
1499b34c648bSJoe Perchessub ltrim {
1500b34c648bSJoe Perches	my ($string) = @_;
1501b34c648bSJoe Perches
1502b34c648bSJoe Perches	$string =~ s/^\s+//;
1503b34c648bSJoe Perches
1504b34c648bSJoe Perches	return $string;
1505b34c648bSJoe Perches}
1506b34c648bSJoe Perches
1507b34c648bSJoe Perchessub rtrim {
1508b34c648bSJoe Perches	my ($string) = @_;
1509b34c648bSJoe Perches
1510b34c648bSJoe Perches	$string =~ s/\s+$//;
15113705ce5bSJoe Perches
15123705ce5bSJoe Perches	return $string;
15133705ce5bSJoe Perches}
15143705ce5bSJoe Perches
15153705ce5bSJoe Perchessub tabify {
15163705ce5bSJoe Perches	my ($leading) = @_;
15173705ce5bSJoe Perches
15183705ce5bSJoe Perches	my $source_indent = 8;
15193705ce5bSJoe Perches	my $max_spaces_before_tab = $source_indent - 1;
15203705ce5bSJoe Perches	my $spaces_to_tab = " " x $source_indent;
15213705ce5bSJoe Perches
15223705ce5bSJoe Perches	#convert leading spaces to tabs
15233705ce5bSJoe Perches	1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
15243705ce5bSJoe Perches	#Remove spaces before a tab
15253705ce5bSJoe Perches	1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
15263705ce5bSJoe Perches
15273705ce5bSJoe Perches	return "$leading";
15283705ce5bSJoe Perches}
15293705ce5bSJoe Perches
1530d1fe9c09SJoe Perchessub pos_last_openparen {
1531d1fe9c09SJoe Perches	my ($line) = @_;
1532d1fe9c09SJoe Perches
1533d1fe9c09SJoe Perches	my $pos = 0;
1534d1fe9c09SJoe Perches
1535d1fe9c09SJoe Perches	my $opens = $line =~ tr/\(/\(/;
1536d1fe9c09SJoe Perches	my $closes = $line =~ tr/\)/\)/;
1537d1fe9c09SJoe Perches
1538d1fe9c09SJoe Perches	my $last_openparen = 0;
1539d1fe9c09SJoe Perches
1540d1fe9c09SJoe Perches	if (($opens == 0) || ($closes >= $opens)) {
1541d1fe9c09SJoe Perches		return -1;
1542d1fe9c09SJoe Perches	}
1543d1fe9c09SJoe Perches
1544d1fe9c09SJoe Perches	my $len = length($line);
1545d1fe9c09SJoe Perches
1546d1fe9c09SJoe Perches	for ($pos = 0; $pos < $len; $pos++) {
1547d1fe9c09SJoe Perches		my $string = substr($line, $pos);
1548d1fe9c09SJoe Perches		if ($string =~ /^($FuncArg|$balanced_parens)/) {
1549d1fe9c09SJoe Perches			$pos += length($1) - 1;
1550d1fe9c09SJoe Perches		} elsif (substr($line, $pos, 1) eq '(') {
1551d1fe9c09SJoe Perches			$last_openparen = $pos;
1552d1fe9c09SJoe Perches		} elsif (index($string, '(') == -1) {
1553d1fe9c09SJoe Perches			last;
1554d1fe9c09SJoe Perches		}
1555d1fe9c09SJoe Perches	}
1556d1fe9c09SJoe Perches
1557d1fe9c09SJoe Perches	return $last_openparen + 1;
1558d1fe9c09SJoe Perches}
1559d1fe9c09SJoe Perches
15600a920b5bSAndy Whitcroftsub process {
15610a920b5bSAndy Whitcroft	my $filename = shift;
15620a920b5bSAndy Whitcroft
15630a920b5bSAndy Whitcroft	my $linenr=0;
15640a920b5bSAndy Whitcroft	my $prevline="";
1565c2fdda0dSAndy Whitcroft	my $prevrawline="";
15660a920b5bSAndy Whitcroft	my $stashline="";
1567c2fdda0dSAndy Whitcroft	my $stashrawline="";
15680a920b5bSAndy Whitcroft
15694a0df2efSAndy Whitcroft	my $length;
15700a920b5bSAndy Whitcroft	my $indent;
15710a920b5bSAndy Whitcroft	my $previndent=0;
15720a920b5bSAndy Whitcroft	my $stashindent=0;
15730a920b5bSAndy Whitcroft
1574de7d4f0eSAndy Whitcroft	our $clean = 1;
15750a920b5bSAndy Whitcroft	my $signoff = 0;
15760a920b5bSAndy Whitcroft	my $is_patch = 0;
15770a920b5bSAndy Whitcroft
157815662b3eSJoe Perches	my $in_header_lines = 1;
157915662b3eSJoe Perches	my $in_commit_log = 0;		#Scanning lines before patch
158015662b3eSJoe Perches
1581fa64205dSPasi Savanainen	my $non_utf8_charset = 0;
1582fa64205dSPasi Savanainen
158313214adfSAndy Whitcroft	our @report = ();
15846c72ffaaSAndy Whitcroft	our $cnt_lines = 0;
15856c72ffaaSAndy Whitcroft	our $cnt_error = 0;
15866c72ffaaSAndy Whitcroft	our $cnt_warn = 0;
15876c72ffaaSAndy Whitcroft	our $cnt_chk = 0;
15886c72ffaaSAndy Whitcroft
15890a920b5bSAndy Whitcroft	# Trace the real file/line as we go.
15900a920b5bSAndy Whitcroft	my $realfile = '';
15910a920b5bSAndy Whitcroft	my $realline = 0;
15920a920b5bSAndy Whitcroft	my $realcnt = 0;
15930a920b5bSAndy Whitcroft	my $here = '';
15940a920b5bSAndy Whitcroft	my $in_comment = 0;
1595c2fdda0dSAndy Whitcroft	my $comment_edge = 0;
15960a920b5bSAndy Whitcroft	my $first_line = 0;
15971e855726SWolfram Sang	my $p1_prefix = '';
15980a920b5bSAndy Whitcroft
159913214adfSAndy Whitcroft	my $prev_values = 'E';
160013214adfSAndy Whitcroft
160113214adfSAndy Whitcroft	# suppression flags
1602773647a0SAndy Whitcroft	my %suppress_ifbraces;
1603170d3a22SAndy Whitcroft	my %suppress_whiletrailers;
16042b474a1aSAndy Whitcroft	my %suppress_export;
16053e469cdcSAndy Whitcroft	my $suppress_statement = 0;
1606653d4876SAndy Whitcroft
16077e51f197SJoe Perches	my %signatures = ();
1608323c1260SJoe Perches
1609c2fdda0dSAndy Whitcroft	# Pre-scan the patch sanitizing the lines.
1610de7d4f0eSAndy Whitcroft	# Pre-scan the patch looking for any __setup documentation.
1611c2fdda0dSAndy Whitcroft	#
1612de7d4f0eSAndy Whitcroft	my @setup_docs = ();
1613de7d4f0eSAndy Whitcroft	my $setup_docs = 0;
1614773647a0SAndy Whitcroft
1615*d8b07710SJoe Perches	my $camelcase_file_seeded = 0;
1616*d8b07710SJoe Perches
1617773647a0SAndy Whitcroft	sanitise_line_reset();
1618c2fdda0dSAndy Whitcroft	my $line;
1619c2fdda0dSAndy Whitcroft	foreach my $rawline (@rawlines) {
1620773647a0SAndy Whitcroft		$linenr++;
1621773647a0SAndy Whitcroft		$line = $rawline;
1622c2fdda0dSAndy Whitcroft
16233705ce5bSJoe Perches		push(@fixed, $rawline) if ($fix);
16243705ce5bSJoe Perches
1625773647a0SAndy Whitcroft		if ($rawline=~/^\+\+\+\s+(\S+)/) {
1626de7d4f0eSAndy Whitcroft			$setup_docs = 0;
1627de7d4f0eSAndy Whitcroft			if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
1628de7d4f0eSAndy Whitcroft				$setup_docs = 1;
1629de7d4f0eSAndy Whitcroft			}
1630773647a0SAndy Whitcroft			#next;
1631de7d4f0eSAndy Whitcroft		}
1632773647a0SAndy Whitcroft		if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1633773647a0SAndy Whitcroft			$realline=$1-1;
1634773647a0SAndy Whitcroft			if (defined $2) {
1635773647a0SAndy Whitcroft				$realcnt=$3+1;
1636773647a0SAndy Whitcroft			} else {
1637773647a0SAndy Whitcroft				$realcnt=1+1;
1638773647a0SAndy Whitcroft			}
1639c45dcabdSAndy Whitcroft			$in_comment = 0;
1640773647a0SAndy Whitcroft
1641773647a0SAndy Whitcroft			# Guestimate if this is a continuing comment.  Run
1642773647a0SAndy Whitcroft			# the context looking for a comment "edge".  If this
1643773647a0SAndy Whitcroft			# edge is a close comment then we must be in a comment
1644773647a0SAndy Whitcroft			# at context start.
1645773647a0SAndy Whitcroft			my $edge;
164601fa9147SAndy Whitcroft			my $cnt = $realcnt;
164701fa9147SAndy Whitcroft			for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
164801fa9147SAndy Whitcroft				next if (defined $rawlines[$ln - 1] &&
164901fa9147SAndy Whitcroft					 $rawlines[$ln - 1] =~ /^-/);
165001fa9147SAndy Whitcroft				$cnt--;
165101fa9147SAndy Whitcroft				#print "RAW<$rawlines[$ln - 1]>\n";
1652721c1cb6SAndy Whitcroft				last if (!defined $rawlines[$ln - 1]);
1653fae17daeSAndy Whitcroft				if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
1654fae17daeSAndy Whitcroft				    $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
1655fae17daeSAndy Whitcroft					($edge) = $1;
1656fae17daeSAndy Whitcroft					last;
1657fae17daeSAndy Whitcroft				}
1658773647a0SAndy Whitcroft			}
1659773647a0SAndy Whitcroft			if (defined $edge && $edge eq '*/') {
1660773647a0SAndy Whitcroft				$in_comment = 1;
1661773647a0SAndy Whitcroft			}
1662773647a0SAndy Whitcroft
1663773647a0SAndy Whitcroft			# Guestimate if this is a continuing comment.  If this
1664773647a0SAndy Whitcroft			# is the start of a diff block and this line starts
1665773647a0SAndy Whitcroft			# ' *' then it is very likely a comment.
1666773647a0SAndy Whitcroft			if (!defined $edge &&
166783242e0cSAndy Whitcroft			    $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
1668773647a0SAndy Whitcroft			{
1669773647a0SAndy Whitcroft				$in_comment = 1;
1670773647a0SAndy Whitcroft			}
1671773647a0SAndy Whitcroft
1672773647a0SAndy Whitcroft			##print "COMMENT:$in_comment edge<$edge> $rawline\n";
1673773647a0SAndy Whitcroft			sanitise_line_reset($in_comment);
1674773647a0SAndy Whitcroft
1675171ae1a4SAndy Whitcroft		} elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
1676773647a0SAndy Whitcroft			# Standardise the strings and chars within the input to
1677171ae1a4SAndy Whitcroft			# simplify matching -- only bother with positive lines.
1678773647a0SAndy Whitcroft			$line = sanitise_line($rawline);
1679773647a0SAndy Whitcroft		}
1680773647a0SAndy Whitcroft		push(@lines, $line);
1681773647a0SAndy Whitcroft
1682773647a0SAndy Whitcroft		if ($realcnt > 1) {
1683773647a0SAndy Whitcroft			$realcnt-- if ($line =~ /^(?:\+| |$)/);
1684773647a0SAndy Whitcroft		} else {
1685773647a0SAndy Whitcroft			$realcnt = 0;
1686773647a0SAndy Whitcroft		}
1687773647a0SAndy Whitcroft
1688773647a0SAndy Whitcroft		#print "==>$rawline\n";
1689773647a0SAndy Whitcroft		#print "-->$line\n";
1690de7d4f0eSAndy Whitcroft
1691de7d4f0eSAndy Whitcroft		if ($setup_docs && $line =~ /^\+/) {
1692de7d4f0eSAndy Whitcroft			push(@setup_docs, $line);
1693de7d4f0eSAndy Whitcroft		}
1694de7d4f0eSAndy Whitcroft	}
1695de7d4f0eSAndy Whitcroft
16966c72ffaaSAndy Whitcroft	$prefix = '';
16976c72ffaaSAndy Whitcroft
1698773647a0SAndy Whitcroft	$realcnt = 0;
1699773647a0SAndy Whitcroft	$linenr = 0;
17000a920b5bSAndy Whitcroft	foreach my $line (@lines) {
17010a920b5bSAndy Whitcroft		$linenr++;
17021b5539b1SJoe Perches		my $sline = $line;	#copy of $line
17031b5539b1SJoe Perches		$sline =~ s/$;/ /g;	#with comments as spaces
17040a920b5bSAndy Whitcroft
1705c2fdda0dSAndy Whitcroft		my $rawline = $rawlines[$linenr - 1];
17066c72ffaaSAndy Whitcroft
17070a920b5bSAndy Whitcroft#extract the line range in the file after the patch is applied
17086c72ffaaSAndy Whitcroft		if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
17090a920b5bSAndy Whitcroft			$is_patch = 1;
17104a0df2efSAndy Whitcroft			$first_line = $linenr + 1;
17110a920b5bSAndy Whitcroft			$realline=$1-1;
17120a920b5bSAndy Whitcroft			if (defined $2) {
17130a920b5bSAndy Whitcroft				$realcnt=$3+1;
17140a920b5bSAndy Whitcroft			} else {
17150a920b5bSAndy Whitcroft				$realcnt=1+1;
17160a920b5bSAndy Whitcroft			}
1717c2fdda0dSAndy Whitcroft			annotate_reset();
171813214adfSAndy Whitcroft			$prev_values = 'E';
171913214adfSAndy Whitcroft
1720773647a0SAndy Whitcroft			%suppress_ifbraces = ();
1721170d3a22SAndy Whitcroft			%suppress_whiletrailers = ();
17222b474a1aSAndy Whitcroft			%suppress_export = ();
17233e469cdcSAndy Whitcroft			$suppress_statement = 0;
17240a920b5bSAndy Whitcroft			next;
17250a920b5bSAndy Whitcroft
17264a0df2efSAndy Whitcroft# track the line number as we move through the hunk, note that
17274a0df2efSAndy Whitcroft# new versions of GNU diff omit the leading space on completely
17284a0df2efSAndy Whitcroft# blank context lines so we need to count that too.
1729773647a0SAndy Whitcroft		} elsif ($line =~ /^( |\+|$)/) {
17300a920b5bSAndy Whitcroft			$realline++;
1731d8aaf121SAndy Whitcroft			$realcnt-- if ($realcnt != 0);
17320a920b5bSAndy Whitcroft
17334a0df2efSAndy Whitcroft			# Measure the line length and indent.
1734c2fdda0dSAndy Whitcroft			($length, $indent) = line_stats($rawline);
17350a920b5bSAndy Whitcroft
17360a920b5bSAndy Whitcroft			# Track the previous line.
17370a920b5bSAndy Whitcroft			($prevline, $stashline) = ($stashline, $line);
17380a920b5bSAndy Whitcroft			($previndent, $stashindent) = ($stashindent, $indent);
1739c2fdda0dSAndy Whitcroft			($prevrawline, $stashrawline) = ($stashrawline, $rawline);
1740c2fdda0dSAndy Whitcroft
1741773647a0SAndy Whitcroft			#warn "line<$line>\n";
17426c72ffaaSAndy Whitcroft
1743d8aaf121SAndy Whitcroft		} elsif ($realcnt == 1) {
1744d8aaf121SAndy Whitcroft			$realcnt--;
17450a920b5bSAndy Whitcroft		}
17460a920b5bSAndy Whitcroft
1747cc77cdcaSAndy Whitcroft		my $hunk_line = ($realcnt != 0);
1748cc77cdcaSAndy Whitcroft
17490a920b5bSAndy Whitcroft#make up the handle for any error we report on this line
1750773647a0SAndy Whitcroft		$prefix = "$filename:$realline: " if ($emacs && $file);
1751773647a0SAndy Whitcroft		$prefix = "$filename:$linenr: " if ($emacs && !$file);
1752773647a0SAndy Whitcroft
17536c72ffaaSAndy Whitcroft		$here = "#$linenr: " if (!$file);
17546c72ffaaSAndy Whitcroft		$here = "#$realline: " if ($file);
1755773647a0SAndy Whitcroft
1756773647a0SAndy Whitcroft		# extract the filename as it passes
17573bf9a009SRabin Vincent		if ($line =~ /^diff --git.*?(\S+)$/) {
17583bf9a009SRabin Vincent			$realfile = $1;
17593bf9a009SRabin Vincent			$realfile =~ s@^([^/]*)/@@;
1760270c49a0SJoe Perches			$in_commit_log = 0;
17613bf9a009SRabin Vincent		} elsif ($line =~ /^\+\+\+\s+(\S+)/) {
1762773647a0SAndy Whitcroft			$realfile = $1;
17631e855726SWolfram Sang			$realfile =~ s@^([^/]*)/@@;
1764270c49a0SJoe Perches			$in_commit_log = 0;
17651e855726SWolfram Sang
17661e855726SWolfram Sang			$p1_prefix = $1;
1767e2f7aa4bSAndy Whitcroft			if (!$file && $tree && $p1_prefix ne '' &&
1768e2f7aa4bSAndy Whitcroft			    -e "$root/$p1_prefix") {
1769000d1cc1SJoe Perches				WARN("PATCH_PREFIX",
1770000d1cc1SJoe Perches				     "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
17711e855726SWolfram Sang			}
1772773647a0SAndy Whitcroft
1773c1ab3326SAndy Whitcroft			if ($realfile =~ m@^include/asm/@) {
1774000d1cc1SJoe Perches				ERROR("MODIFIED_INCLUDE_ASM",
1775000d1cc1SJoe Perches				      "do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
1776773647a0SAndy Whitcroft			}
1777773647a0SAndy Whitcroft			next;
1778773647a0SAndy Whitcroft		}
1779773647a0SAndy Whitcroft
1780389834b6SRandy Dunlap		$here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
17810a920b5bSAndy Whitcroft
1782c2fdda0dSAndy Whitcroft		my $hereline = "$here\n$rawline\n";
1783c2fdda0dSAndy Whitcroft		my $herecurr = "$here\n$rawline\n";
1784c2fdda0dSAndy Whitcroft		my $hereprev = "$here\n$prevrawline\n$rawline\n";
17850a920b5bSAndy Whitcroft
17866c72ffaaSAndy Whitcroft		$cnt_lines++ if ($realcnt != 0);
17876c72ffaaSAndy Whitcroft
17883bf9a009SRabin Vincent# Check for incorrect file permissions
17893bf9a009SRabin Vincent		if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
17903bf9a009SRabin Vincent			my $permhere = $here . "FILE: $realfile\n";
179104db4d25SJoe Perches			if ($realfile !~ m@scripts/@ &&
179204db4d25SJoe Perches			    $realfile !~ /\.(py|pl|awk|sh)$/) {
1793000d1cc1SJoe Perches				ERROR("EXECUTE_PERMISSIONS",
1794000d1cc1SJoe Perches				      "do not set execute permissions for source files\n" . $permhere);
17953bf9a009SRabin Vincent			}
17963bf9a009SRabin Vincent		}
17973bf9a009SRabin Vincent
179820112475SJoe Perches# Check the patch for a signoff:
1799d8aaf121SAndy Whitcroft		if ($line =~ /^\s*signed-off-by:/i) {
18004a0df2efSAndy Whitcroft			$signoff++;
180115662b3eSJoe Perches			$in_commit_log = 0;
18020a920b5bSAndy Whitcroft		}
180320112475SJoe Perches
180420112475SJoe Perches# Check signature styles
1805270c49a0SJoe Perches		if (!$in_header_lines &&
1806ce0338dfSJoe Perches		    $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
180720112475SJoe Perches			my $space_before = $1;
180820112475SJoe Perches			my $sign_off = $2;
180920112475SJoe Perches			my $space_after = $3;
181020112475SJoe Perches			my $email = $4;
181120112475SJoe Perches			my $ucfirst_sign_off = ucfirst(lc($sign_off));
181220112475SJoe Perches
1813ce0338dfSJoe Perches			if ($sign_off !~ /$signature_tags/) {
1814ce0338dfSJoe Perches				WARN("BAD_SIGN_OFF",
1815ce0338dfSJoe Perches				     "Non-standard signature: $sign_off\n" . $herecurr);
1816ce0338dfSJoe Perches			}
181720112475SJoe Perches			if (defined $space_before && $space_before ne "") {
18183705ce5bSJoe Perches				if (WARN("BAD_SIGN_OFF",
18193705ce5bSJoe Perches					 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
18203705ce5bSJoe Perches				    $fix) {
18213705ce5bSJoe Perches					$fixed[$linenr - 1] =
18223705ce5bSJoe Perches					    "$ucfirst_sign_off $email";
18233705ce5bSJoe Perches				}
182420112475SJoe Perches			}
182520112475SJoe Perches			if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
18263705ce5bSJoe Perches				if (WARN("BAD_SIGN_OFF",
18273705ce5bSJoe Perches					 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
18283705ce5bSJoe Perches				    $fix) {
18293705ce5bSJoe Perches					$fixed[$linenr - 1] =
18303705ce5bSJoe Perches					    "$ucfirst_sign_off $email";
18313705ce5bSJoe Perches				}
18323705ce5bSJoe Perches
183320112475SJoe Perches			}
183420112475SJoe Perches			if (!defined $space_after || $space_after ne " ") {
18353705ce5bSJoe Perches				if (WARN("BAD_SIGN_OFF",
18363705ce5bSJoe Perches					 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
18373705ce5bSJoe Perches				    $fix) {
18383705ce5bSJoe Perches					$fixed[$linenr - 1] =
18393705ce5bSJoe Perches					    "$ucfirst_sign_off $email";
18403705ce5bSJoe Perches				}
184120112475SJoe Perches			}
184220112475SJoe Perches
184320112475SJoe Perches			my ($email_name, $email_address, $comment) = parse_email($email);
184420112475SJoe Perches			my $suggested_email = format_email(($email_name, $email_address));
184520112475SJoe Perches			if ($suggested_email eq "") {
1846000d1cc1SJoe Perches				ERROR("BAD_SIGN_OFF",
1847000d1cc1SJoe Perches				      "Unrecognized email address: '$email'\n" . $herecurr);
184820112475SJoe Perches			} else {
184920112475SJoe Perches				my $dequoted = $suggested_email;
185020112475SJoe Perches				$dequoted =~ s/^"//;
185120112475SJoe Perches				$dequoted =~ s/" </ </;
185220112475SJoe Perches				# Don't force email to have quotes
185320112475SJoe Perches				# Allow just an angle bracketed address
185420112475SJoe Perches				if ("$dequoted$comment" ne $email &&
185520112475SJoe Perches				    "<$email_address>$comment" ne $email &&
185620112475SJoe Perches				    "$suggested_email$comment" ne $email) {
1857000d1cc1SJoe Perches					WARN("BAD_SIGN_OFF",
1858000d1cc1SJoe Perches					     "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
185920112475SJoe Perches				}
18600a920b5bSAndy Whitcroft			}
18617e51f197SJoe Perches
18627e51f197SJoe Perches# Check for duplicate signatures
18637e51f197SJoe Perches			my $sig_nospace = $line;
18647e51f197SJoe Perches			$sig_nospace =~ s/\s//g;
18657e51f197SJoe Perches			$sig_nospace = lc($sig_nospace);
18667e51f197SJoe Perches			if (defined $signatures{$sig_nospace}) {
18677e51f197SJoe Perches				WARN("BAD_SIGN_OFF",
18687e51f197SJoe Perches				     "Duplicate signature\n" . $herecurr);
18697e51f197SJoe Perches			} else {
18707e51f197SJoe Perches				$signatures{$sig_nospace} = 1;
18717e51f197SJoe Perches			}
18720a920b5bSAndy Whitcroft		}
18730a920b5bSAndy Whitcroft
187400df344fSAndy Whitcroft# Check for wrappage within a valid hunk of the file
18758905a67cSAndy Whitcroft		if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
1876000d1cc1SJoe Perches			ERROR("CORRUPTED_PATCH",
1877000d1cc1SJoe Perches			      "patch seems to be corrupt (line wrapped?)\n" .
18786c72ffaaSAndy Whitcroft				$herecurr) if (!$emitted_corrupt++);
1879de7d4f0eSAndy Whitcroft		}
1880de7d4f0eSAndy Whitcroft
18816ecd9674SAndy Whitcroft# Check for absolute kernel paths.
18826ecd9674SAndy Whitcroft		if ($tree) {
18836ecd9674SAndy Whitcroft			while ($line =~ m{(?:^|\s)(/\S*)}g) {
18846ecd9674SAndy Whitcroft				my $file = $1;
18856ecd9674SAndy Whitcroft
18866ecd9674SAndy Whitcroft				if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
18876ecd9674SAndy Whitcroft				    check_absolute_file($1, $herecurr)) {
18886ecd9674SAndy Whitcroft					#
18896ecd9674SAndy Whitcroft				} else {
18906ecd9674SAndy Whitcroft					check_absolute_file($file, $herecurr);
18916ecd9674SAndy Whitcroft				}
18926ecd9674SAndy Whitcroft			}
18936ecd9674SAndy Whitcroft		}
18946ecd9674SAndy Whitcroft
1895de7d4f0eSAndy Whitcroft# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
1896de7d4f0eSAndy Whitcroft		if (($realfile =~ /^$/ || $line =~ /^\+/) &&
1897171ae1a4SAndy Whitcroft		    $rawline !~ m/^$UTF8*$/) {
1898171ae1a4SAndy Whitcroft			my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
1899171ae1a4SAndy Whitcroft
1900171ae1a4SAndy Whitcroft			my $blank = copy_spacing($rawline);
1901171ae1a4SAndy Whitcroft			my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
1902171ae1a4SAndy Whitcroft			my $hereptr = "$hereline$ptr\n";
1903171ae1a4SAndy Whitcroft
190434d99219SJoe Perches			CHK("INVALID_UTF8",
1905000d1cc1SJoe Perches			    "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
190600df344fSAndy Whitcroft		}
19070a920b5bSAndy Whitcroft
190815662b3eSJoe Perches# Check if it's the start of a commit log
190915662b3eSJoe Perches# (not a header line and we haven't seen the patch filename)
191015662b3eSJoe Perches		if ($in_header_lines && $realfile =~ /^$/ &&
1911270c49a0SJoe Perches		    $rawline !~ /^(commit\b|from\b|[\w-]+:).+$/i) {
191215662b3eSJoe Perches			$in_header_lines = 0;
191315662b3eSJoe Perches			$in_commit_log = 1;
191415662b3eSJoe Perches		}
191515662b3eSJoe Perches
1916fa64205dSPasi Savanainen# Check if there is UTF-8 in a commit log when a mail header has explicitly
1917fa64205dSPasi Savanainen# declined it, i.e defined some charset where it is missing.
1918fa64205dSPasi Savanainen		if ($in_header_lines &&
1919fa64205dSPasi Savanainen		    $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
1920fa64205dSPasi Savanainen		    $1 !~ /utf-8/i) {
1921fa64205dSPasi Savanainen			$non_utf8_charset = 1;
1922fa64205dSPasi Savanainen		}
1923fa64205dSPasi Savanainen
1924fa64205dSPasi Savanainen		if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
192515662b3eSJoe Perches		    $rawline =~ /$NON_ASCII_UTF8/) {
1926fa64205dSPasi Savanainen			WARN("UTF8_BEFORE_PATCH",
192715662b3eSJoe Perches			    "8-bit UTF-8 used in possible commit log\n" . $herecurr);
192815662b3eSJoe Perches		}
192915662b3eSJoe Perches
193030670854SAndy Whitcroft# ignore non-hunk lines and lines being removed
193130670854SAndy Whitcroft		next if (!$hunk_line || $line =~ /^-/);
193200df344fSAndy Whitcroft
19330a920b5bSAndy Whitcroft#trailing whitespace
19349c0ca6f9SAndy Whitcroft		if ($line =~ /^\+.*\015/) {
1935c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1936d5e616fcSJoe Perches			if (ERROR("DOS_LINE_ENDINGS",
1937d5e616fcSJoe Perches				  "DOS line endings\n" . $herevet) &&
1938d5e616fcSJoe Perches			    $fix) {
1939d5e616fcSJoe Perches				$fixed[$linenr - 1] =~ s/[\s\015]+$//;
1940d5e616fcSJoe Perches			}
1941c2fdda0dSAndy Whitcroft		} elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
1942c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
19433705ce5bSJoe Perches			if (ERROR("TRAILING_WHITESPACE",
19443705ce5bSJoe Perches				  "trailing whitespace\n" . $herevet) &&
19453705ce5bSJoe Perches			    $fix) {
1946d5e616fcSJoe Perches				$fixed[$linenr - 1] =~ s/\s+$//;
19473705ce5bSJoe Perches			}
19483705ce5bSJoe Perches
1949d2c0a235SAndy Whitcroft			$rpt_cleaners = 1;
19500a920b5bSAndy Whitcroft		}
19515368df20SAndy Whitcroft
19523354957aSAndi Kleen# check for Kconfig help text having a real description
19539fe287d7SAndy Whitcroft# Only applies when adding the entry originally, after that we do not have
19549fe287d7SAndy Whitcroft# sufficient context to determine whether it is indeed long enough.
19553354957aSAndi Kleen		if ($realfile =~ /Kconfig/ &&
1956a1385803SAndy Whitcroft		    $line =~ /.\s*config\s+/) {
19573354957aSAndi Kleen			my $length = 0;
19589fe287d7SAndy Whitcroft			my $cnt = $realcnt;
19599fe287d7SAndy Whitcroft			my $ln = $linenr + 1;
19609fe287d7SAndy Whitcroft			my $f;
1961a1385803SAndy Whitcroft			my $is_start = 0;
19629fe287d7SAndy Whitcroft			my $is_end = 0;
1963a1385803SAndy Whitcroft			for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
19649fe287d7SAndy Whitcroft				$f = $lines[$ln - 1];
19659fe287d7SAndy Whitcroft				$cnt-- if ($lines[$ln - 1] !~ /^-/);
19669fe287d7SAndy Whitcroft				$is_end = $lines[$ln - 1] =~ /^\+/;
19679fe287d7SAndy Whitcroft
19689fe287d7SAndy Whitcroft				next if ($f =~ /^-/);
1969a1385803SAndy Whitcroft
1970a1385803SAndy Whitcroft				if ($lines[$ln - 1] =~ /.\s*(?:bool|tristate)\s*\"/) {
1971a1385803SAndy Whitcroft					$is_start = 1;
1972a1385803SAndy Whitcroft				} elsif ($lines[$ln - 1] =~ /.\s*(?:---)?help(?:---)?$/) {
1973a1385803SAndy Whitcroft					$length = -1;
1974a1385803SAndy Whitcroft				}
1975a1385803SAndy Whitcroft
19769fe287d7SAndy Whitcroft				$f =~ s/^.//;
19773354957aSAndi Kleen				$f =~ s/#.*//;
19783354957aSAndi Kleen				$f =~ s/^\s+//;
19793354957aSAndi Kleen				next if ($f =~ /^$/);
19809fe287d7SAndy Whitcroft				if ($f =~ /^\s*config\s/) {
19819fe287d7SAndy Whitcroft					$is_end = 1;
19829fe287d7SAndy Whitcroft					last;
19839fe287d7SAndy Whitcroft				}
19843354957aSAndi Kleen				$length++;
19853354957aSAndi Kleen			}
1986000d1cc1SJoe Perches			WARN("CONFIG_DESCRIPTION",
1987a1385803SAndy Whitcroft			     "please write a paragraph that describes the config symbol fully\n" . $herecurr) if ($is_start && $is_end && $length < 4);
1988a1385803SAndy Whitcroft			#print "is_start<$is_start> is_end<$is_end> length<$length>\n";
19893354957aSAndi Kleen		}
19903354957aSAndi Kleen
19911ba8dfd1SKees Cook# discourage the addition of CONFIG_EXPERIMENTAL in Kconfig.
19921ba8dfd1SKees Cook		if ($realfile =~ /Kconfig/ &&
19931ba8dfd1SKees Cook		    $line =~ /.\s*depends on\s+.*\bEXPERIMENTAL\b/) {
19941ba8dfd1SKees Cook			WARN("CONFIG_EXPERIMENTAL",
19951ba8dfd1SKees Cook			     "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
19961ba8dfd1SKees Cook		}
19971ba8dfd1SKees Cook
1998c68e5878SArnaud Lacombe		if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
1999c68e5878SArnaud Lacombe		    ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
2000c68e5878SArnaud Lacombe			my $flag = $1;
2001c68e5878SArnaud Lacombe			my $replacement = {
2002c68e5878SArnaud Lacombe				'EXTRA_AFLAGS' =>   'asflags-y',
2003c68e5878SArnaud Lacombe				'EXTRA_CFLAGS' =>   'ccflags-y',
2004c68e5878SArnaud Lacombe				'EXTRA_CPPFLAGS' => 'cppflags-y',
2005c68e5878SArnaud Lacombe				'EXTRA_LDFLAGS' =>  'ldflags-y',
2006c68e5878SArnaud Lacombe			};
2007c68e5878SArnaud Lacombe
2008c68e5878SArnaud Lacombe			WARN("DEPRECATED_VARIABLE",
2009c68e5878SArnaud Lacombe			     "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
2010c68e5878SArnaud Lacombe		}
2011c68e5878SArnaud Lacombe
20125368df20SAndy Whitcroft# check we are in a valid source file if not then ignore this hunk
20135368df20SAndy Whitcroft		next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
20145368df20SAndy Whitcroft
20156cd7f386SJoe Perches#line length limit
2016c45dcabdSAndy Whitcroft		if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ &&
2017f4c014c0SAndy Whitcroft		    $rawline !~ /^.\s*\*\s*\@$Ident\s/ &&
20180fccc622SJoe Perches		    !($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(KERN_\S+\s*|[^"]*))?"[X\t]*"\s*(?:|,|\)\s*;)\s*$/ ||
20198bbea968SJoe Perches		    $line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) &&
20206cd7f386SJoe Perches		    $length > $max_line_length)
2021c45dcabdSAndy Whitcroft		{
2022000d1cc1SJoe Perches			WARN("LONG_LINE",
20236cd7f386SJoe Perches			     "line over $max_line_length characters\n" . $herecurr);
20240a920b5bSAndy Whitcroft		}
20250a920b5bSAndy Whitcroft
2026ca56dc09SJosh Triplett# Check for user-visible strings broken across lines, which breaks the ability
2027ca56dc09SJosh Triplett# to grep for the string.  Limited to strings used as parameters (those
2028ca56dc09SJosh Triplett# following an open parenthesis), which almost completely eliminates false
2029ca56dc09SJosh Triplett# positives, as well as warning only once per parameter rather than once per
2030ca56dc09SJosh Triplett# line of the string.  Make an exception when the previous string ends in a
2031ca56dc09SJosh Triplett# newline (multiple lines in one string constant) or \n\t (common in inline
2032ca56dc09SJosh Triplett# assembly to indent the instruction on the following line).
2033ca56dc09SJosh Triplett		if ($line =~ /^\+\s*"/ &&
2034ca56dc09SJosh Triplett		    $prevline =~ /"\s*$/ &&
2035ca56dc09SJosh Triplett		    $prevline =~ /\(/ &&
2036ca56dc09SJosh Triplett		    $prevrawline !~ /\\n(?:\\t)*"\s*$/) {
2037ca56dc09SJosh Triplett			WARN("SPLIT_STRING",
2038ca56dc09SJosh Triplett			     "quoted string split across lines\n" . $hereprev);
2039ca56dc09SJosh Triplett		}
2040ca56dc09SJosh Triplett
20415e79d96eSJoe Perches# check for spaces before a quoted newline
20425e79d96eSJoe Perches		if ($rawline =~ /^.*\".*\s\\n/) {
20433705ce5bSJoe Perches			if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
20443705ce5bSJoe Perches				 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
20453705ce5bSJoe Perches			    $fix) {
20463705ce5bSJoe Perches				$fixed[$linenr - 1] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
20473705ce5bSJoe Perches			}
20483705ce5bSJoe Perches
20495e79d96eSJoe Perches		}
20505e79d96eSJoe Perches
20518905a67cSAndy Whitcroft# check for adding lines without a newline.
20528905a67cSAndy Whitcroft		if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
2053000d1cc1SJoe Perches			WARN("MISSING_EOF_NEWLINE",
2054000d1cc1SJoe Perches			     "adding a line without newline at end of file\n" . $herecurr);
20558905a67cSAndy Whitcroft		}
20568905a67cSAndy Whitcroft
205742e41c54SMike Frysinger# Blackfin: use hi/lo macros
205842e41c54SMike Frysinger		if ($realfile =~ m@arch/blackfin/.*\.S$@) {
205942e41c54SMike Frysinger			if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
206042e41c54SMike Frysinger				my $herevet = "$here\n" . cat_vet($line) . "\n";
2061000d1cc1SJoe Perches				ERROR("LO_MACRO",
2062000d1cc1SJoe Perches				      "use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
206342e41c54SMike Frysinger			}
206442e41c54SMike Frysinger			if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
206542e41c54SMike Frysinger				my $herevet = "$here\n" . cat_vet($line) . "\n";
2066000d1cc1SJoe Perches				ERROR("HI_MACRO",
2067000d1cc1SJoe Perches				      "use the HI() macro, not (... >> 16)\n" . $herevet);
206842e41c54SMike Frysinger			}
206942e41c54SMike Frysinger		}
207042e41c54SMike Frysinger
2071b9ea10d6SAndy Whitcroft# check we are in a valid source file C or perl if not then ignore this hunk
2072b9ea10d6SAndy Whitcroft		next if ($realfile !~ /\.(h|c|pl)$/);
20730a920b5bSAndy Whitcroft
20740a920b5bSAndy Whitcroft# at the beginning of a line any tabs must come first and anything
20750a920b5bSAndy Whitcroft# more than 8 must use tabs.
2076c2fdda0dSAndy Whitcroft		if ($rawline =~ /^\+\s* \t\s*\S/ ||
2077c2fdda0dSAndy Whitcroft		    $rawline =~ /^\+\s*        \s*/) {
2078c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2079d2c0a235SAndy Whitcroft			$rpt_cleaners = 1;
20803705ce5bSJoe Perches			if (ERROR("CODE_INDENT",
20813705ce5bSJoe Perches				  "code indent should use tabs where possible\n" . $herevet) &&
20823705ce5bSJoe Perches			    $fix) {
20833705ce5bSJoe Perches				$fixed[$linenr - 1] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
20843705ce5bSJoe Perches			}
20850a920b5bSAndy Whitcroft		}
20860a920b5bSAndy Whitcroft
208708e44365SAlberto Panizzo# check for space before tabs.
208808e44365SAlberto Panizzo		if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
208908e44365SAlberto Panizzo			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
20903705ce5bSJoe Perches			if (WARN("SPACE_BEFORE_TAB",
20913705ce5bSJoe Perches				"please, no space before tabs\n" . $herevet) &&
20923705ce5bSJoe Perches			    $fix) {
20933705ce5bSJoe Perches				$fixed[$linenr - 1] =~
20943705ce5bSJoe Perches				    s/(^\+.*) +\t/$1\t/;
20953705ce5bSJoe Perches			}
209608e44365SAlberto Panizzo		}
209708e44365SAlberto Panizzo
2098d1fe9c09SJoe Perches# check for && or || at the start of a line
2099d1fe9c09SJoe Perches		if ($rawline =~ /^\+\s*(&&|\|\|)/) {
2100d1fe9c09SJoe Perches			CHK("LOGICAL_CONTINUATIONS",
2101d1fe9c09SJoe Perches			    "Logical continuations should be on the previous line\n" . $hereprev);
2102d1fe9c09SJoe Perches		}
2103d1fe9c09SJoe Perches
2104d1fe9c09SJoe Perches# check multi-line statement indentation matches previous line
2105d1fe9c09SJoe Perches		if ($^V && $^V ge 5.10.0 &&
2106d1fe9c09SJoe Perches		    $prevline =~ /^\+(\t*)(if \(|$Ident\().*(\&\&|\|\||,)\s*$/) {
2107d1fe9c09SJoe Perches			$prevline =~ /^\+(\t*)(.*)$/;
2108d1fe9c09SJoe Perches			my $oldindent = $1;
2109d1fe9c09SJoe Perches			my $rest = $2;
2110d1fe9c09SJoe Perches
2111d1fe9c09SJoe Perches			my $pos = pos_last_openparen($rest);
2112d1fe9c09SJoe Perches			if ($pos >= 0) {
2113b34a26f3SJoe Perches				$line =~ /^(\+| )([ \t]*)/;
2114b34a26f3SJoe Perches				my $newindent = $2;
2115d1fe9c09SJoe Perches
2116d1fe9c09SJoe Perches				my $goodtabindent = $oldindent .
2117d1fe9c09SJoe Perches					"\t" x ($pos / 8) .
2118d1fe9c09SJoe Perches					" "  x ($pos % 8);
2119d1fe9c09SJoe Perches				my $goodspaceindent = $oldindent . " "  x $pos;
2120d1fe9c09SJoe Perches
2121d1fe9c09SJoe Perches				if ($newindent ne $goodtabindent &&
2122d1fe9c09SJoe Perches				    $newindent ne $goodspaceindent) {
21233705ce5bSJoe Perches
21243705ce5bSJoe Perches					if (CHK("PARENTHESIS_ALIGNMENT",
21253705ce5bSJoe Perches						"Alignment should match open parenthesis\n" . $hereprev) &&
21263705ce5bSJoe Perches					    $fix && $line =~ /^\+/) {
21273705ce5bSJoe Perches						$fixed[$linenr - 1] =~
21283705ce5bSJoe Perches						    s/^\+[ \t]*/\+$goodtabindent/;
21293705ce5bSJoe Perches					}
2130d1fe9c09SJoe Perches				}
2131d1fe9c09SJoe Perches			}
2132d1fe9c09SJoe Perches		}
2133d1fe9c09SJoe Perches
213423f780c9SJoe Perches		if ($line =~ /^\+.*\*[ \t]*\)[ \t]+(?!$Assignment|$Arithmetic)/) {
21353705ce5bSJoe Perches			if (CHK("SPACING",
21363705ce5bSJoe Perches				"No space is necessary after a cast\n" . $hereprev) &&
21373705ce5bSJoe Perches			    $fix) {
21383705ce5bSJoe Perches				$fixed[$linenr - 1] =~
21393705ce5bSJoe Perches				    s/^(\+.*\*[ \t]*\))[ \t]+/$1/;
21403705ce5bSJoe Perches			}
2141aad4f614SJoe Perches		}
2142aad4f614SJoe Perches
214305880600SJoe Perches		if ($realfile =~ m@^(drivers/net/|net/)@ &&
2144fdb4bcd6SJoe Perches		    $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
2145fdb4bcd6SJoe Perches		    $rawline =~ /^\+[ \t]*\*/) {
214605880600SJoe Perches			WARN("NETWORKING_BLOCK_COMMENT_STYLE",
214705880600SJoe Perches			     "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
214805880600SJoe Perches		}
214905880600SJoe Perches
215005880600SJoe Perches		if ($realfile =~ m@^(drivers/net/|net/)@ &&
2151a605e32eSJoe Perches		    $prevrawline =~ /^\+[ \t]*\/\*/ &&		#starting /*
2152a605e32eSJoe Perches		    $prevrawline !~ /\*\/[ \t]*$/ &&		#no trailing */
215361135e96SJoe Perches		    $rawline =~ /^\+/ &&			#line is new
2154a605e32eSJoe Perches		    $rawline !~ /^\+[ \t]*\*/) {		#no leading *
2155a605e32eSJoe Perches			WARN("NETWORKING_BLOCK_COMMENT_STYLE",
2156a605e32eSJoe Perches			     "networking block comments start with * on subsequent lines\n" . $hereprev);
2157a605e32eSJoe Perches		}
2158a605e32eSJoe Perches
2159a605e32eSJoe Perches		if ($realfile =~ m@^(drivers/net/|net/)@ &&
2160c24f9f19SJoe Perches		    $rawline !~ m@^\+[ \t]*\*/[ \t]*$@ &&	#trailing */
2161c24f9f19SJoe Perches		    $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ &&	#inline /*...*/
2162c24f9f19SJoe Perches		    $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ &&	#trailing **/
2163c24f9f19SJoe Perches		    $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) {	#non blank */
216405880600SJoe Perches			WARN("NETWORKING_BLOCK_COMMENT_STYLE",
216505880600SJoe Perches			     "networking block comments put the trailing */ on a separate line\n" . $herecurr);
216605880600SJoe Perches		}
216705880600SJoe Perches
21685f7ddae6SRaffaele Recalcati# check for spaces at the beginning of a line.
21696b4c5bebSAndy Whitcroft# Exceptions:
21706b4c5bebSAndy Whitcroft#  1) within comments
21716b4c5bebSAndy Whitcroft#  2) indented preprocessor commands
21726b4c5bebSAndy Whitcroft#  3) hanging labels
21733705ce5bSJoe Perches		if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/)  {
21745f7ddae6SRaffaele Recalcati			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
21753705ce5bSJoe Perches			if (WARN("LEADING_SPACE",
21763705ce5bSJoe Perches				 "please, no spaces at the start of a line\n" . $herevet) &&
21773705ce5bSJoe Perches			    $fix) {
21783705ce5bSJoe Perches				$fixed[$linenr - 1] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
21793705ce5bSJoe Perches			}
21805f7ddae6SRaffaele Recalcati		}
21815f7ddae6SRaffaele Recalcati
2182b9ea10d6SAndy Whitcroft# check we are in a valid C source file if not then ignore this hunk
2183b9ea10d6SAndy Whitcroft		next if ($realfile !~ /\.(h|c)$/);
2184b9ea10d6SAndy Whitcroft
21851ba8dfd1SKees Cook# discourage the addition of CONFIG_EXPERIMENTAL in #if(def).
21861ba8dfd1SKees Cook		if ($line =~ /^\+\s*\#\s*if.*\bCONFIG_EXPERIMENTAL\b/) {
21871ba8dfd1SKees Cook			WARN("CONFIG_EXPERIMENTAL",
21881ba8dfd1SKees Cook			     "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
21891ba8dfd1SKees Cook		}
21901ba8dfd1SKees Cook
2191c2fdda0dSAndy Whitcroft# check for RCS/CVS revision markers
2192cf655043SAndy Whitcroft		if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
2193000d1cc1SJoe Perches			WARN("CVS_KEYWORD",
2194000d1cc1SJoe Perches			     "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
2195c2fdda0dSAndy Whitcroft		}
219622f2a2efSAndy Whitcroft
219742e41c54SMike Frysinger# Blackfin: don't use __builtin_bfin_[cs]sync
219842e41c54SMike Frysinger		if ($line =~ /__builtin_bfin_csync/) {
219942e41c54SMike Frysinger			my $herevet = "$here\n" . cat_vet($line) . "\n";
2200000d1cc1SJoe Perches			ERROR("CSYNC",
2201000d1cc1SJoe Perches			      "use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
220242e41c54SMike Frysinger		}
220342e41c54SMike Frysinger		if ($line =~ /__builtin_bfin_ssync/) {
220442e41c54SMike Frysinger			my $herevet = "$here\n" . cat_vet($line) . "\n";
2205000d1cc1SJoe Perches			ERROR("SSYNC",
2206000d1cc1SJoe Perches			      "use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
220742e41c54SMike Frysinger		}
220842e41c54SMike Frysinger
220956e77d70SJoe Perches# check for old HOTPLUG __dev<foo> section markings
221056e77d70SJoe Perches		if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
221156e77d70SJoe Perches			WARN("HOTPLUG_SECTION",
221256e77d70SJoe Perches			     "Using $1 is unnecessary\n" . $herecurr);
221356e77d70SJoe Perches		}
221456e77d70SJoe Perches
22159c0ca6f9SAndy Whitcroft# Check for potential 'bare' types
22162b474a1aSAndy Whitcroft		my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
22172b474a1aSAndy Whitcroft		    $realline_next);
22183e469cdcSAndy Whitcroft#print "LINE<$line>\n";
22193e469cdcSAndy Whitcroft		if ($linenr >= $suppress_statement &&
22201b5539b1SJoe Perches		    $realcnt && $sline =~ /.\s*\S/) {
2221170d3a22SAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
2222f5fe35ddSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0);
2223171ae1a4SAndy Whitcroft			$stat =~ s/\n./\n /g;
2224171ae1a4SAndy Whitcroft			$cond =~ s/\n./\n /g;
2225171ae1a4SAndy Whitcroft
22263e469cdcSAndy Whitcroft#print "linenr<$linenr> <$stat>\n";
22273e469cdcSAndy Whitcroft			# If this statement has no statement boundaries within
22283e469cdcSAndy Whitcroft			# it there is no point in retrying a statement scan
22293e469cdcSAndy Whitcroft			# until we hit end of it.
22303e469cdcSAndy Whitcroft			my $frag = $stat; $frag =~ s/;+\s*$//;
22313e469cdcSAndy Whitcroft			if ($frag !~ /(?:{|;)/) {
22323e469cdcSAndy Whitcroft#print "skip<$line_nr_next>\n";
22333e469cdcSAndy Whitcroft				$suppress_statement = $line_nr_next;
22343e469cdcSAndy Whitcroft			}
2235f74bd194SAndy Whitcroft
22362b474a1aSAndy Whitcroft			# Find the real next line.
22372b474a1aSAndy Whitcroft			$realline_next = $line_nr_next;
22382b474a1aSAndy Whitcroft			if (defined $realline_next &&
22392b474a1aSAndy Whitcroft			    (!defined $lines[$realline_next - 1] ||
22402b474a1aSAndy Whitcroft			     substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
22412b474a1aSAndy Whitcroft				$realline_next++;
22422b474a1aSAndy Whitcroft			}
22432b474a1aSAndy Whitcroft
2244171ae1a4SAndy Whitcroft			my $s = $stat;
2245171ae1a4SAndy Whitcroft			$s =~ s/{.*$//s;
2246cf655043SAndy Whitcroft
2247c2fdda0dSAndy Whitcroft			# Ignore goto labels.
2248171ae1a4SAndy Whitcroft			if ($s =~ /$Ident:\*$/s) {
2249c2fdda0dSAndy Whitcroft
2250c2fdda0dSAndy Whitcroft			# Ignore functions being called
2251171ae1a4SAndy Whitcroft			} elsif ($s =~ /^.\s*$Ident\s*\(/s) {
2252c2fdda0dSAndy Whitcroft
2253463f2864SAndy Whitcroft			} elsif ($s =~ /^.\s*else\b/s) {
2254463f2864SAndy Whitcroft
2255c45dcabdSAndy Whitcroft			# declarations always start with types
2256d2506586SAndy 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) {
2257c45dcabdSAndy Whitcroft				my $type = $1;
2258c45dcabdSAndy Whitcroft				$type =~ s/\s+/ /g;
2259c45dcabdSAndy Whitcroft				possible($type, "A:" . $s);
2260c45dcabdSAndy Whitcroft
22616c72ffaaSAndy Whitcroft			# definitions in global scope can only start with types
2262a6a84062SAndy Whitcroft			} elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
2263c45dcabdSAndy Whitcroft				possible($1, "B:" . $s);
2264c2fdda0dSAndy Whitcroft			}
22658905a67cSAndy Whitcroft
22666c72ffaaSAndy Whitcroft			# any (foo ... *) is a pointer cast, and foo is a type
226765863862SAndy Whitcroft			while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
2268c45dcabdSAndy Whitcroft				possible($1, "C:" . $s);
22699c0ca6f9SAndy Whitcroft			}
22708905a67cSAndy Whitcroft
22718905a67cSAndy Whitcroft			# Check for any sort of function declaration.
22728905a67cSAndy Whitcroft			# int foo(something bar, other baz);
22738905a67cSAndy Whitcroft			# void (*store_gdt)(x86_descr_ptr *);
2274171ae1a4SAndy Whitcroft			if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
22758905a67cSAndy Whitcroft				my ($name_len) = length($1);
22768905a67cSAndy Whitcroft
2277cf655043SAndy Whitcroft				my $ctx = $s;
2278773647a0SAndy Whitcroft				substr($ctx, 0, $name_len + 1, '');
22798905a67cSAndy Whitcroft				$ctx =~ s/\)[^\)]*$//;
2280cf655043SAndy Whitcroft
22818905a67cSAndy Whitcroft				for my $arg (split(/\s*,\s*/, $ctx)) {
2282c45dcabdSAndy Whitcroft					if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
22838905a67cSAndy Whitcroft
2284c45dcabdSAndy Whitcroft						possible($1, "D:" . $s);
22858905a67cSAndy Whitcroft					}
22868905a67cSAndy Whitcroft				}
22878905a67cSAndy Whitcroft			}
22888905a67cSAndy Whitcroft
22899c0ca6f9SAndy Whitcroft		}
22909c0ca6f9SAndy Whitcroft
229100df344fSAndy Whitcroft#
229200df344fSAndy Whitcroft# Checks which may be anchored in the context.
229300df344fSAndy Whitcroft#
229400df344fSAndy Whitcroft
229500df344fSAndy Whitcroft# Check for switch () and associated case and default
229600df344fSAndy Whitcroft# statements should be at the same indent.
229700df344fSAndy Whitcroft		if ($line=~/\bswitch\s*\(.*\)/) {
229800df344fSAndy Whitcroft			my $err = '';
229900df344fSAndy Whitcroft			my $sep = '';
230000df344fSAndy Whitcroft			my @ctx = ctx_block_outer($linenr, $realcnt);
230100df344fSAndy Whitcroft			shift(@ctx);
230200df344fSAndy Whitcroft			for my $ctx (@ctx) {
230300df344fSAndy Whitcroft				my ($clen, $cindent) = line_stats($ctx);
230400df344fSAndy Whitcroft				if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
230500df344fSAndy Whitcroft							$indent != $cindent) {
230600df344fSAndy Whitcroft					$err .= "$sep$ctx\n";
230700df344fSAndy Whitcroft					$sep = '';
230800df344fSAndy Whitcroft				} else {
230900df344fSAndy Whitcroft					$sep = "[...]\n";
231000df344fSAndy Whitcroft				}
231100df344fSAndy Whitcroft			}
231200df344fSAndy Whitcroft			if ($err ne '') {
2313000d1cc1SJoe Perches				ERROR("SWITCH_CASE_INDENT_LEVEL",
2314000d1cc1SJoe Perches				      "switch and case should be at the same indent\n$hereline$err");
2315de7d4f0eSAndy Whitcroft			}
2316de7d4f0eSAndy Whitcroft		}
2317de7d4f0eSAndy Whitcroft
2318de7d4f0eSAndy Whitcroft# if/while/etc brace do not go on next line, unless defining a do while loop,
2319de7d4f0eSAndy Whitcroft# or if that brace on the next line is for something else
2320c45dcabdSAndy Whitcroft		if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
2321773647a0SAndy Whitcroft			my $pre_ctx = "$1$2";
2322773647a0SAndy Whitcroft
23239c0ca6f9SAndy Whitcroft			my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
23248eef05ddSJoe Perches
23258eef05ddSJoe Perches			if ($line =~ /^\+\t{6,}/) {
23268eef05ddSJoe Perches				WARN("DEEP_INDENTATION",
23278eef05ddSJoe Perches				     "Too many leading tabs - consider code refactoring\n" . $herecurr);
23288eef05ddSJoe Perches			}
23298eef05ddSJoe Perches
2330de7d4f0eSAndy Whitcroft			my $ctx_cnt = $realcnt - $#ctx - 1;
2331de7d4f0eSAndy Whitcroft			my $ctx = join("\n", @ctx);
2332de7d4f0eSAndy Whitcroft
2333548596d5SAndy Whitcroft			my $ctx_ln = $linenr;
2334548596d5SAndy Whitcroft			my $ctx_skip = $realcnt;
2335de7d4f0eSAndy Whitcroft
2336548596d5SAndy Whitcroft			while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
2337548596d5SAndy Whitcroft					defined $lines[$ctx_ln - 1] &&
2338548596d5SAndy Whitcroft					$lines[$ctx_ln - 1] =~ /^-/)) {
2339548596d5SAndy Whitcroft				##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
2340548596d5SAndy Whitcroft				$ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
2341773647a0SAndy Whitcroft				$ctx_ln++;
2342773647a0SAndy Whitcroft			}
2343548596d5SAndy Whitcroft
234453210168SAndy Whitcroft			#print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
234553210168SAndy Whitcroft			#print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
2346773647a0SAndy Whitcroft
2347773647a0SAndy Whitcroft			if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln -1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
2348000d1cc1SJoe Perches				ERROR("OPEN_BRACE",
2349000d1cc1SJoe Perches				      "that open brace { should be on the previous line\n" .
235001464f30SAndy Whitcroft					"$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
235100df344fSAndy Whitcroft			}
2352773647a0SAndy Whitcroft			if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
2353773647a0SAndy Whitcroft			    $ctx =~ /\)\s*\;\s*$/ &&
2354773647a0SAndy Whitcroft			    defined $lines[$ctx_ln - 1])
2355773647a0SAndy Whitcroft			{
23569c0ca6f9SAndy Whitcroft				my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
23579c0ca6f9SAndy Whitcroft				if ($nindent > $indent) {
2358000d1cc1SJoe Perches					WARN("TRAILING_SEMICOLON",
2359000d1cc1SJoe Perches					     "trailing semicolon indicates no statements, indent implies otherwise\n" .
236001464f30SAndy Whitcroft						"$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
23619c0ca6f9SAndy Whitcroft				}
23629c0ca6f9SAndy Whitcroft			}
236300df344fSAndy Whitcroft		}
236400df344fSAndy Whitcroft
23654d001e4dSAndy Whitcroft# Check relative indent for conditionals and blocks.
23664d001e4dSAndy Whitcroft		if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
23673e469cdcSAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
23683e469cdcSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0)
23693e469cdcSAndy Whitcroft					if (!defined $stat);
23704d001e4dSAndy Whitcroft			my ($s, $c) = ($stat, $cond);
23714d001e4dSAndy Whitcroft
23724d001e4dSAndy Whitcroft			substr($s, 0, length($c), '');
23734d001e4dSAndy Whitcroft
23744d001e4dSAndy Whitcroft			# Make sure we remove the line prefixes as we have
23754d001e4dSAndy Whitcroft			# none on the first line, and are going to readd them
23764d001e4dSAndy Whitcroft			# where necessary.
23774d001e4dSAndy Whitcroft			$s =~ s/\n./\n/gs;
23784d001e4dSAndy Whitcroft
23794d001e4dSAndy Whitcroft			# Find out how long the conditional actually is.
23806f779c18SAndy Whitcroft			my @newlines = ($c =~ /\n/gs);
23816f779c18SAndy Whitcroft			my $cond_lines = 1 + $#newlines;
23824d001e4dSAndy Whitcroft
23834d001e4dSAndy Whitcroft			# We want to check the first line inside the block
23844d001e4dSAndy Whitcroft			# starting at the end of the conditional, so remove:
23854d001e4dSAndy Whitcroft			#  1) any blank line termination
23864d001e4dSAndy Whitcroft			#  2) any opening brace { on end of the line
23874d001e4dSAndy Whitcroft			#  3) any do (...) {
23884d001e4dSAndy Whitcroft			my $continuation = 0;
23894d001e4dSAndy Whitcroft			my $check = 0;
23904d001e4dSAndy Whitcroft			$s =~ s/^.*\bdo\b//;
23914d001e4dSAndy Whitcroft			$s =~ s/^\s*{//;
23924d001e4dSAndy Whitcroft			if ($s =~ s/^\s*\\//) {
23934d001e4dSAndy Whitcroft				$continuation = 1;
23944d001e4dSAndy Whitcroft			}
23959bd49efeSAndy Whitcroft			if ($s =~ s/^\s*?\n//) {
23964d001e4dSAndy Whitcroft				$check = 1;
23974d001e4dSAndy Whitcroft				$cond_lines++;
23984d001e4dSAndy Whitcroft			}
23994d001e4dSAndy Whitcroft
24004d001e4dSAndy Whitcroft			# Also ignore a loop construct at the end of a
24014d001e4dSAndy Whitcroft			# preprocessor statement.
24024d001e4dSAndy Whitcroft			if (($prevline =~ /^.\s*#\s*define\s/ ||
24034d001e4dSAndy Whitcroft			    $prevline =~ /\\\s*$/) && $continuation == 0) {
24044d001e4dSAndy Whitcroft				$check = 0;
24054d001e4dSAndy Whitcroft			}
24064d001e4dSAndy Whitcroft
24079bd49efeSAndy Whitcroft			my $cond_ptr = -1;
2408740504c6SAndy Whitcroft			$continuation = 0;
24099bd49efeSAndy Whitcroft			while ($cond_ptr != $cond_lines) {
24109bd49efeSAndy Whitcroft				$cond_ptr = $cond_lines;
24114d001e4dSAndy Whitcroft
2412f16fa28fSAndy Whitcroft				# If we see an #else/#elif then the code
2413f16fa28fSAndy Whitcroft				# is not linear.
2414f16fa28fSAndy Whitcroft				if ($s =~ /^\s*\#\s*(?:else|elif)/) {
2415f16fa28fSAndy Whitcroft					$check = 0;
2416f16fa28fSAndy Whitcroft				}
2417f16fa28fSAndy Whitcroft
24189bd49efeSAndy Whitcroft				# Ignore:
24199bd49efeSAndy Whitcroft				#  1) blank lines, they should be at 0,
24209bd49efeSAndy Whitcroft				#  2) preprocessor lines, and
24219bd49efeSAndy Whitcroft				#  3) labels.
2422740504c6SAndy Whitcroft				if ($continuation ||
2423740504c6SAndy Whitcroft				    $s =~ /^\s*?\n/ ||
24249bd49efeSAndy Whitcroft				    $s =~ /^\s*#\s*?/ ||
24259bd49efeSAndy Whitcroft				    $s =~ /^\s*$Ident\s*:/) {
2426740504c6SAndy Whitcroft					$continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
242730dad6ebSAndy Whitcroft					if ($s =~ s/^.*?\n//) {
24289bd49efeSAndy Whitcroft						$cond_lines++;
24299bd49efeSAndy Whitcroft					}
24304d001e4dSAndy Whitcroft				}
243130dad6ebSAndy Whitcroft			}
24324d001e4dSAndy Whitcroft
24334d001e4dSAndy Whitcroft			my (undef, $sindent) = line_stats("+" . $s);
24344d001e4dSAndy Whitcroft			my $stat_real = raw_line($linenr, $cond_lines);
24354d001e4dSAndy Whitcroft
24364d001e4dSAndy Whitcroft			# Check if either of these lines are modified, else
24374d001e4dSAndy Whitcroft			# this is not this patch's fault.
24384d001e4dSAndy Whitcroft			if (!defined($stat_real) ||
24394d001e4dSAndy Whitcroft			    $stat !~ /^\+/ && $stat_real !~ /^\+/) {
24404d001e4dSAndy Whitcroft				$check = 0;
24414d001e4dSAndy Whitcroft			}
24424d001e4dSAndy Whitcroft			if (defined($stat_real) && $cond_lines > 1) {
24434d001e4dSAndy Whitcroft				$stat_real = "[...]\n$stat_real";
24444d001e4dSAndy Whitcroft			}
24454d001e4dSAndy Whitcroft
24469bd49efeSAndy 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";
24474d001e4dSAndy Whitcroft
24484d001e4dSAndy Whitcroft			if ($check && (($sindent % 8) != 0 ||
24494d001e4dSAndy Whitcroft			    ($sindent <= $indent && $s ne ''))) {
2450000d1cc1SJoe Perches				WARN("SUSPECT_CODE_INDENT",
2451000d1cc1SJoe Perches				     "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
24524d001e4dSAndy Whitcroft			}
24534d001e4dSAndy Whitcroft		}
24544d001e4dSAndy Whitcroft
24556c72ffaaSAndy Whitcroft		# Track the 'values' across context and added lines.
24566c72ffaaSAndy Whitcroft		my $opline = $line; $opline =~ s/^./ /;
24571f65f947SAndy Whitcroft		my ($curr_values, $curr_vars) =
24581f65f947SAndy Whitcroft				annotate_values($opline . "\n", $prev_values);
24596c72ffaaSAndy Whitcroft		$curr_values = $prev_values . $curr_values;
2460c2fdda0dSAndy Whitcroft		if ($dbg_values) {
2461c2fdda0dSAndy Whitcroft			my $outline = $opline; $outline =~ s/\t/ /g;
2462cf655043SAndy Whitcroft			print "$linenr > .$outline\n";
2463cf655043SAndy Whitcroft			print "$linenr > $curr_values\n";
24641f65f947SAndy Whitcroft			print "$linenr >  $curr_vars\n";
2465c2fdda0dSAndy Whitcroft		}
24666c72ffaaSAndy Whitcroft		$prev_values = substr($curr_values, -1);
24676c72ffaaSAndy Whitcroft
246800df344fSAndy Whitcroft#ignore lines not being added
24693705ce5bSJoe Perches		next if ($line =~ /^[^\+]/);
247000df344fSAndy Whitcroft
2471653d4876SAndy Whitcroft# TEST: allow direct testing of the type matcher.
24727429c690SAndy Whitcroft		if ($dbg_type) {
24737429c690SAndy Whitcroft			if ($line =~ /^.\s*$Declare\s*$/) {
2474000d1cc1SJoe Perches				ERROR("TEST_TYPE",
2475000d1cc1SJoe Perches				      "TEST: is type\n" . $herecurr);
24767429c690SAndy Whitcroft			} elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
2477000d1cc1SJoe Perches				ERROR("TEST_NOT_TYPE",
2478000d1cc1SJoe Perches				      "TEST: is not type ($1 is)\n". $herecurr);
24797429c690SAndy Whitcroft			}
2480653d4876SAndy Whitcroft			next;
2481653d4876SAndy Whitcroft		}
2482a1ef277eSAndy Whitcroft# TEST: allow direct testing of the attribute matcher.
2483a1ef277eSAndy Whitcroft		if ($dbg_attr) {
24849360b0e5SAndy Whitcroft			if ($line =~ /^.\s*$Modifier\s*$/) {
2485000d1cc1SJoe Perches				ERROR("TEST_ATTR",
2486000d1cc1SJoe Perches				      "TEST: is attr\n" . $herecurr);
24879360b0e5SAndy Whitcroft			} elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
2488000d1cc1SJoe Perches				ERROR("TEST_NOT_ATTR",
2489000d1cc1SJoe Perches				      "TEST: is not attr ($1 is)\n". $herecurr);
2490a1ef277eSAndy Whitcroft			}
2491a1ef277eSAndy Whitcroft			next;
2492a1ef277eSAndy Whitcroft		}
2493653d4876SAndy Whitcroft
2494f0a594c1SAndy Whitcroft# check for initialisation to aggregates open brace on the next line
249599423c20SAndy Whitcroft		if ($line =~ /^.\s*{/ &&
249699423c20SAndy Whitcroft		    $prevline =~ /(?:^|[^=])=\s*$/) {
2497000d1cc1SJoe Perches			ERROR("OPEN_BRACE",
2498000d1cc1SJoe Perches			      "that open brace { should be on the previous line\n" . $hereprev);
2499f0a594c1SAndy Whitcroft		}
2500f0a594c1SAndy Whitcroft
250100df344fSAndy Whitcroft#
250200df344fSAndy Whitcroft# Checks which are anchored on the added line.
250300df344fSAndy Whitcroft#
250400df344fSAndy Whitcroft
2505653d4876SAndy Whitcroft# check for malformed paths in #include statements (uses RAW line)
2506c45dcabdSAndy Whitcroft		if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
2507653d4876SAndy Whitcroft			my $path = $1;
2508653d4876SAndy Whitcroft			if ($path =~ m{//}) {
2509000d1cc1SJoe Perches				ERROR("MALFORMED_INCLUDE",
2510495e9d84SJoe Perches				      "malformed #include filename\n" . $herecurr);
2511495e9d84SJoe Perches			}
2512495e9d84SJoe Perches			if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
2513495e9d84SJoe Perches				ERROR("UAPI_INCLUDE",
2514495e9d84SJoe Perches				      "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
2515653d4876SAndy Whitcroft			}
2516653d4876SAndy Whitcroft		}
2517653d4876SAndy Whitcroft
251800df344fSAndy Whitcroft# no C99 // comments
251900df344fSAndy Whitcroft		if ($line =~ m{//}) {
25203705ce5bSJoe Perches			if (ERROR("C99_COMMENTS",
25213705ce5bSJoe Perches				  "do not use C99 // comments\n" . $herecurr) &&
25223705ce5bSJoe Perches			    $fix) {
25233705ce5bSJoe Perches				my $line = $fixed[$linenr - 1];
25243705ce5bSJoe Perches				if ($line =~ /\/\/(.*)$/) {
25253705ce5bSJoe Perches					my $comment = trim($1);
25263705ce5bSJoe Perches					$fixed[$linenr - 1] =~ s@\/\/(.*)$@/\* $comment \*/@;
25273705ce5bSJoe Perches				}
25283705ce5bSJoe Perches			}
252900df344fSAndy Whitcroft		}
253000df344fSAndy Whitcroft		# Remove C99 comments.
25310a920b5bSAndy Whitcroft		$line =~ s@//.*@@;
25326c72ffaaSAndy Whitcroft		$opline =~ s@//.*@@;
25330a920b5bSAndy Whitcroft
25342b474a1aSAndy Whitcroft# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
25352b474a1aSAndy Whitcroft# the whole statement.
25362b474a1aSAndy Whitcroft#print "APW <$lines[$realline_next - 1]>\n";
25372b474a1aSAndy Whitcroft		if (defined $realline_next &&
25382b474a1aSAndy Whitcroft		    exists $lines[$realline_next - 1] &&
25392b474a1aSAndy Whitcroft		    !defined $suppress_export{$realline_next} &&
25402b474a1aSAndy Whitcroft		    ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
25412b474a1aSAndy Whitcroft		     $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
25423cbf62dfSAndy Whitcroft			# Handle definitions which produce identifiers with
25433cbf62dfSAndy Whitcroft			# a prefix:
25443cbf62dfSAndy Whitcroft			#   XXX(foo);
25453cbf62dfSAndy Whitcroft			#   EXPORT_SYMBOL(something_foo);
2546653d4876SAndy Whitcroft			my $name = $1;
254787a53877SAndy Whitcroft			if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
25483cbf62dfSAndy Whitcroft			    $name =~ /^${Ident}_$2/) {
25493cbf62dfSAndy Whitcroft#print "FOO C name<$name>\n";
25503cbf62dfSAndy Whitcroft				$suppress_export{$realline_next} = 1;
25513cbf62dfSAndy Whitcroft
25523cbf62dfSAndy Whitcroft			} elsif ($stat !~ /(?:
25532b474a1aSAndy Whitcroft				\n.}\s*$|
255448012058SAndy Whitcroft				^.DEFINE_$Ident\(\Q$name\E\)|
255548012058SAndy Whitcroft				^.DECLARE_$Ident\(\Q$name\E\)|
255648012058SAndy Whitcroft				^.LIST_HEAD\(\Q$name\E\)|
25572b474a1aSAndy Whitcroft				^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
25582b474a1aSAndy Whitcroft				\b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
255948012058SAndy Whitcroft			    )/x) {
25602b474a1aSAndy Whitcroft#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
25612b474a1aSAndy Whitcroft				$suppress_export{$realline_next} = 2;
25622b474a1aSAndy Whitcroft			} else {
25632b474a1aSAndy Whitcroft				$suppress_export{$realline_next} = 1;
25640a920b5bSAndy Whitcroft			}
25650a920b5bSAndy Whitcroft		}
25662b474a1aSAndy Whitcroft		if (!defined $suppress_export{$linenr} &&
25672b474a1aSAndy Whitcroft		    $prevline =~ /^.\s*$/ &&
25682b474a1aSAndy Whitcroft		    ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
25692b474a1aSAndy Whitcroft		     $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
25702b474a1aSAndy Whitcroft#print "FOO B <$lines[$linenr - 1]>\n";
25712b474a1aSAndy Whitcroft			$suppress_export{$linenr} = 2;
25722b474a1aSAndy Whitcroft		}
25732b474a1aSAndy Whitcroft		if (defined $suppress_export{$linenr} &&
25742b474a1aSAndy Whitcroft		    $suppress_export{$linenr} == 2) {
2575000d1cc1SJoe Perches			WARN("EXPORT_SYMBOL",
2576000d1cc1SJoe Perches			     "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
25772b474a1aSAndy Whitcroft		}
25780a920b5bSAndy Whitcroft
25795150bda4SJoe Eloff# check for global initialisers.
2580d5e616fcSJoe Perches		if ($line =~ /^\+(\s*$Type\s*$Ident\s*(?:\s+$Modifier))*\s*=\s*(0|NULL|false)\s*;/) {
2581d5e616fcSJoe Perches			if (ERROR("GLOBAL_INITIALISERS",
2582000d1cc1SJoe Perches				  "do not initialise globals to 0 or NULL\n" .
2583d5e616fcSJoe Perches				      $herecurr) &&
2584d5e616fcSJoe Perches			    $fix) {
2585d5e616fcSJoe Perches				$fixed[$linenr - 1] =~ s/($Type\s*$Ident\s*(?:\s+$Modifier))*\s*=\s*(0|NULL|false)\s*;/$1;/;
2586d5e616fcSJoe Perches			}
2587f0a594c1SAndy Whitcroft		}
25880a920b5bSAndy Whitcroft# check for static initialisers.
2589d5e616fcSJoe Perches		if ($line =~ /^\+.*\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
2590d5e616fcSJoe Perches			if (ERROR("INITIALISED_STATIC",
2591000d1cc1SJoe Perches				  "do not initialise statics to 0 or NULL\n" .
2592d5e616fcSJoe Perches				      $herecurr) &&
2593d5e616fcSJoe Perches			    $fix) {
2594d5e616fcSJoe Perches				$fixed[$linenr - 1] =~ s/(\bstatic\s.*?)\s*=\s*(0|NULL|false)\s*;/$1;/;
2595d5e616fcSJoe Perches			}
25960a920b5bSAndy Whitcroft		}
25970a920b5bSAndy Whitcroft
2598cb710ecaSJoe Perches# check for static const char * arrays.
2599cb710ecaSJoe Perches		if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
2600000d1cc1SJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
2601000d1cc1SJoe Perches			     "static const char * array should probably be static const char * const\n" .
2602cb710ecaSJoe Perches				$herecurr);
2603cb710ecaSJoe Perches               }
2604cb710ecaSJoe Perches
2605cb710ecaSJoe Perches# check for static char foo[] = "bar" declarations.
2606cb710ecaSJoe Perches		if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
2607000d1cc1SJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
2608000d1cc1SJoe Perches			     "static char array declaration should probably be static const char\n" .
2609cb710ecaSJoe Perches				$herecurr);
2610cb710ecaSJoe Perches               }
2611cb710ecaSJoe Perches
261293ed0e2dSJoe Perches# check for declarations of struct pci_device_id
261393ed0e2dSJoe Perches		if ($line =~ /\bstruct\s+pci_device_id\s+\w+\s*\[\s*\]\s*\=\s*\{/) {
2614000d1cc1SJoe Perches			WARN("DEFINE_PCI_DEVICE_TABLE",
2615000d1cc1SJoe Perches			     "Use DEFINE_PCI_DEVICE_TABLE for struct pci_device_id\n" . $herecurr);
261693ed0e2dSJoe Perches		}
261793ed0e2dSJoe Perches
2618653d4876SAndy Whitcroft# check for new typedefs, only function parameters and sparse annotations
2619653d4876SAndy Whitcroft# make sense.
2620653d4876SAndy Whitcroft		if ($line =~ /\btypedef\s/ &&
26218054576dSAndy Whitcroft		    $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
2622c45dcabdSAndy Whitcroft		    $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
26238ed22cadSAndy Whitcroft		    $line !~ /\b$typeTypedefs\b/ &&
2624653d4876SAndy Whitcroft		    $line !~ /\b__bitwise(?:__|)\b/) {
2625000d1cc1SJoe Perches			WARN("NEW_TYPEDEFS",
2626000d1cc1SJoe Perches			     "do not add new typedefs\n" . $herecurr);
26270a920b5bSAndy Whitcroft		}
26280a920b5bSAndy Whitcroft
26290a920b5bSAndy Whitcroft# * goes on variable not on type
263065863862SAndy Whitcroft		# (char*[ const])
2631bfcb2cc7SAndy Whitcroft		while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
2632bfcb2cc7SAndy Whitcroft			#print "AA<$1>\n";
26333705ce5bSJoe Perches			my ($ident, $from, $to) = ($1, $2, $2);
2634d8aaf121SAndy Whitcroft
263565863862SAndy Whitcroft			# Should start with a space.
263665863862SAndy Whitcroft			$to =~ s/^(\S)/ $1/;
263765863862SAndy Whitcroft			# Should not end with a space.
263865863862SAndy Whitcroft			$to =~ s/\s+$//;
263965863862SAndy Whitcroft			# '*'s should not have spaces between.
2640f9a0b3d1SAndy Whitcroft			while ($to =~ s/\*\s+\*/\*\*/) {
264165863862SAndy Whitcroft			}
2642d8aaf121SAndy Whitcroft
26433705ce5bSJoe Perches##			print "1: from<$from> to<$to> ident<$ident>\n";
264465863862SAndy Whitcroft			if ($from ne $to) {
26453705ce5bSJoe Perches				if (ERROR("POINTER_LOCATION",
26463705ce5bSJoe Perches					  "\"(foo$from)\" should be \"(foo$to)\"\n" .  $herecurr) &&
26473705ce5bSJoe Perches				    $fix) {
26483705ce5bSJoe Perches					my $sub_from = $ident;
26493705ce5bSJoe Perches					my $sub_to = $ident;
26503705ce5bSJoe Perches					$sub_to =~ s/\Q$from\E/$to/;
26513705ce5bSJoe Perches					$fixed[$linenr - 1] =~
26523705ce5bSJoe Perches					    s@\Q$sub_from\E@$sub_to@;
26533705ce5bSJoe Perches				}
265465863862SAndy Whitcroft			}
2655bfcb2cc7SAndy Whitcroft		}
2656bfcb2cc7SAndy Whitcroft		while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
2657bfcb2cc7SAndy Whitcroft			#print "BB<$1>\n";
26583705ce5bSJoe Perches			my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
2659d8aaf121SAndy Whitcroft
266065863862SAndy Whitcroft			# Should start with a space.
266165863862SAndy Whitcroft			$to =~ s/^(\S)/ $1/;
266265863862SAndy Whitcroft			# Should not end with a space.
266365863862SAndy Whitcroft			$to =~ s/\s+$//;
266465863862SAndy Whitcroft			# '*'s should not have spaces between.
2665f9a0b3d1SAndy Whitcroft			while ($to =~ s/\*\s+\*/\*\*/) {
266665863862SAndy Whitcroft			}
266765863862SAndy Whitcroft			# Modifiers should have spaces.
266865863862SAndy Whitcroft			$to =~ s/(\b$Modifier$)/$1 /;
266965863862SAndy Whitcroft
26703705ce5bSJoe Perches##			print "2: from<$from> to<$to> ident<$ident>\n";
2671667026e7SAndy Whitcroft			if ($from ne $to && $ident !~ /^$Modifier$/) {
26723705ce5bSJoe Perches				if (ERROR("POINTER_LOCATION",
26733705ce5bSJoe Perches					  "\"foo${from}bar\" should be \"foo${to}bar\"\n" .  $herecurr) &&
26743705ce5bSJoe Perches				    $fix) {
26753705ce5bSJoe Perches
26763705ce5bSJoe Perches					my $sub_from = $match;
26773705ce5bSJoe Perches					my $sub_to = $match;
26783705ce5bSJoe Perches					$sub_to =~ s/\Q$from\E/$to/;
26793705ce5bSJoe Perches					$fixed[$linenr - 1] =~
26803705ce5bSJoe Perches					    s@\Q$sub_from\E@$sub_to@;
26813705ce5bSJoe Perches				}
268265863862SAndy Whitcroft			}
26830a920b5bSAndy Whitcroft		}
26840a920b5bSAndy Whitcroft
26850a920b5bSAndy Whitcroft# # no BUG() or BUG_ON()
26860a920b5bSAndy Whitcroft# 		if ($line =~ /\b(BUG|BUG_ON)\b/) {
26870a920b5bSAndy Whitcroft# 			print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
26880a920b5bSAndy Whitcroft# 			print "$herecurr";
26890a920b5bSAndy Whitcroft# 			$clean = 0;
26900a920b5bSAndy Whitcroft# 		}
26910a920b5bSAndy Whitcroft
26928905a67cSAndy Whitcroft		if ($line =~ /\bLINUX_VERSION_CODE\b/) {
2693000d1cc1SJoe Perches			WARN("LINUX_VERSION_CODE",
2694000d1cc1SJoe Perches			     "LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
26958905a67cSAndy Whitcroft		}
26968905a67cSAndy Whitcroft
269717441227SJoe Perches# check for uses of printk_ratelimit
269817441227SJoe Perches		if ($line =~ /\bprintk_ratelimit\s*\(/) {
2699000d1cc1SJoe Perches			WARN("PRINTK_RATELIMITED",
2700000d1cc1SJoe Perches"Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
270117441227SJoe Perches		}
270217441227SJoe Perches
270300df344fSAndy Whitcroft# printk should use KERN_* levels.  Note that follow on printk's on the
270400df344fSAndy Whitcroft# same line do not need a level, so we use the current block context
270500df344fSAndy Whitcroft# to try and find and validate the current printk.  In summary the current
270625985edcSLucas De Marchi# printk includes all preceding printk's which have no newline on the end.
270700df344fSAndy Whitcroft# we assume the first bad printk is the one to report.
2708f0a594c1SAndy Whitcroft		if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
270900df344fSAndy Whitcroft			my $ok = 0;
271000df344fSAndy Whitcroft			for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
271100df344fSAndy Whitcroft				#print "CHECK<$lines[$ln - 1]\n";
271225985edcSLucas De Marchi				# we have a preceding printk if it ends
271300df344fSAndy Whitcroft				# with "\n" ignore it, else it is to blame
271400df344fSAndy Whitcroft				if ($lines[$ln - 1] =~ m{\bprintk\(}) {
271500df344fSAndy Whitcroft					if ($rawlines[$ln - 1] !~ m{\\n"}) {
271600df344fSAndy Whitcroft						$ok = 1;
271700df344fSAndy Whitcroft					}
271800df344fSAndy Whitcroft					last;
271900df344fSAndy Whitcroft				}
272000df344fSAndy Whitcroft			}
272100df344fSAndy Whitcroft			if ($ok == 0) {
2722000d1cc1SJoe Perches				WARN("PRINTK_WITHOUT_KERN_LEVEL",
2723000d1cc1SJoe Perches				     "printk() should include KERN_ facility level\n" . $herecurr);
27240a920b5bSAndy Whitcroft			}
272500df344fSAndy Whitcroft		}
27260a920b5bSAndy Whitcroft
2727243f3803SJoe Perches		if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
2728243f3803SJoe Perches			my $orig = $1;
2729243f3803SJoe Perches			my $level = lc($orig);
2730243f3803SJoe Perches			$level = "warn" if ($level eq "warning");
27318f26b837SJoe Perches			my $level2 = $level;
27328f26b837SJoe Perches			$level2 = "dbg" if ($level eq "debug");
2733243f3803SJoe Perches			WARN("PREFER_PR_LEVEL",
27348f26b837SJoe Perches			     "Prefer netdev_$level2(netdev, ... then dev_$level2(dev, ... then pr_$level(...  to printk(KERN_$orig ...\n" . $herecurr);
2735243f3803SJoe Perches		}
2736243f3803SJoe Perches
2737243f3803SJoe Perches		if ($line =~ /\bpr_warning\s*\(/) {
2738d5e616fcSJoe Perches			if (WARN("PREFER_PR_LEVEL",
2739d5e616fcSJoe Perches				 "Prefer pr_warn(... to pr_warning(...\n" . $herecurr) &&
2740d5e616fcSJoe Perches			    $fix) {
2741d5e616fcSJoe Perches				$fixed[$linenr - 1] =~
2742d5e616fcSJoe Perches				    s/\bpr_warning\b/pr_warn/;
2743d5e616fcSJoe Perches			}
2744243f3803SJoe Perches		}
2745243f3803SJoe Perches
2746dc139313SJoe Perches		if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
2747dc139313SJoe Perches			my $orig = $1;
2748dc139313SJoe Perches			my $level = lc($orig);
2749dc139313SJoe Perches			$level = "warn" if ($level eq "warning");
2750dc139313SJoe Perches			$level = "dbg" if ($level eq "debug");
2751dc139313SJoe Perches			WARN("PREFER_DEV_LEVEL",
2752dc139313SJoe Perches			     "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
2753dc139313SJoe Perches		}
2754dc139313SJoe Perches
2755653d4876SAndy Whitcroft# function brace can't be on same line, except for #defines of do while,
2756653d4876SAndy Whitcroft# or if closed on same line
2757c45dcabdSAndy Whitcroft		if (($line=~/$Type\s*$Ident\(.*\).*\s{/) and
2758c45dcabdSAndy Whitcroft		    !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) {
2759000d1cc1SJoe Perches			ERROR("OPEN_BRACE",
2760000d1cc1SJoe Perches			      "open brace '{' following function declarations go on the next line\n" . $herecurr);
27610a920b5bSAndy Whitcroft		}
2762653d4876SAndy Whitcroft
27638905a67cSAndy Whitcroft# open braces for enum, union and struct go on the same line.
27648905a67cSAndy Whitcroft		if ($line =~ /^.\s*{/ &&
27658905a67cSAndy Whitcroft		    $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
2766000d1cc1SJoe Perches			ERROR("OPEN_BRACE",
2767000d1cc1SJoe Perches			      "open brace '{' following $1 go on the same line\n" . $hereprev);
27688905a67cSAndy Whitcroft		}
27698905a67cSAndy Whitcroft
27700c73b4ebSAndy Whitcroft# missing space after union, struct or enum definition
27713705ce5bSJoe Perches		if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
27723705ce5bSJoe Perches			if (WARN("SPACING",
27733705ce5bSJoe Perches				 "missing space after $1 definition\n" . $herecurr) &&
27743705ce5bSJoe Perches			    $fix) {
27753705ce5bSJoe Perches				$fixed[$linenr - 1] =~
27763705ce5bSJoe Perches				    s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
27773705ce5bSJoe Perches			}
27780c73b4ebSAndy Whitcroft		}
27790c73b4ebSAndy Whitcroft
27808d31cfceSAndy Whitcroft# check for spacing round square brackets; allowed:
27818d31cfceSAndy Whitcroft#  1. with a type on the left -- int [] a;
2782fe2a7dbcSAndy Whitcroft#  2. at the beginning of a line for slice initialisers -- [0...10] = 5,
2783fe2a7dbcSAndy Whitcroft#  3. inside a curly brace -- = { [0...10] = 5 }
27848d31cfceSAndy Whitcroft		while ($line =~ /(.*?\s)\[/g) {
27858d31cfceSAndy Whitcroft			my ($where, $prefix) = ($-[1], $1);
27868d31cfceSAndy Whitcroft			if ($prefix !~ /$Type\s+$/ &&
2787fe2a7dbcSAndy Whitcroft			    ($where != 0 || $prefix !~ /^.\s+$/) &&
2788daebc534SAndy Whitcroft			    $prefix !~ /[{,]\s+$/) {
27893705ce5bSJoe Perches				if (ERROR("BRACKET_SPACE",
27903705ce5bSJoe Perches					  "space prohibited before open square bracket '['\n" . $herecurr) &&
27913705ce5bSJoe Perches				    $fix) {
27923705ce5bSJoe Perches				    $fixed[$linenr - 1] =~
27933705ce5bSJoe Perches					s/^(\+.*?)\s+\[/$1\[/;
27943705ce5bSJoe Perches				}
27958d31cfceSAndy Whitcroft			}
27968d31cfceSAndy Whitcroft		}
27978d31cfceSAndy Whitcroft
2798f0a594c1SAndy Whitcroft# check for spaces between functions and their parentheses.
27996c72ffaaSAndy Whitcroft		while ($line =~ /($Ident)\s+\(/g) {
2800c2fdda0dSAndy Whitcroft			my $name = $1;
2801773647a0SAndy Whitcroft			my $ctx_before = substr($line, 0, $-[1]);
2802773647a0SAndy Whitcroft			my $ctx = "$ctx_before$name";
2803c2fdda0dSAndy Whitcroft
2804c2fdda0dSAndy Whitcroft			# Ignore those directives where spaces _are_ permitted.
2805773647a0SAndy Whitcroft			if ($name =~ /^(?:
2806773647a0SAndy Whitcroft				if|for|while|switch|return|case|
2807773647a0SAndy Whitcroft				volatile|__volatile__|
2808773647a0SAndy Whitcroft				__attribute__|format|__extension__|
2809773647a0SAndy Whitcroft				asm|__asm__)$/x)
2810773647a0SAndy Whitcroft			{
2811c2fdda0dSAndy Whitcroft			# cpp #define statements have non-optional spaces, ie
2812c2fdda0dSAndy Whitcroft			# if there is a space between the name and the open
2813c2fdda0dSAndy Whitcroft			# parenthesis it is simply not a parameter group.
2814c45dcabdSAndy Whitcroft			} elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
2815773647a0SAndy Whitcroft
2816773647a0SAndy Whitcroft			# cpp #elif statement condition may start with a (
2817c45dcabdSAndy Whitcroft			} elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
2818c2fdda0dSAndy Whitcroft
2819c2fdda0dSAndy Whitcroft			# If this whole things ends with a type its most
2820c2fdda0dSAndy Whitcroft			# likely a typedef for a function.
2821773647a0SAndy Whitcroft			} elsif ($ctx =~ /$Type$/) {
2822c2fdda0dSAndy Whitcroft
2823c2fdda0dSAndy Whitcroft			} else {
28243705ce5bSJoe Perches				if (WARN("SPACING",
28253705ce5bSJoe Perches					 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
28263705ce5bSJoe Perches					     $fix) {
28273705ce5bSJoe Perches					$fixed[$linenr - 1] =~
28283705ce5bSJoe Perches					    s/\b$name\s+\(/$name\(/;
28293705ce5bSJoe Perches				}
2830f0a594c1SAndy Whitcroft			}
28316c72ffaaSAndy Whitcroft		}
28329a4cad4eSEric Nelson
2833653d4876SAndy Whitcroft# Check operator spacing.
28340a920b5bSAndy Whitcroft		if (!($line=~/\#\s*include/)) {
28353705ce5bSJoe Perches			my $fixed_line = "";
28363705ce5bSJoe Perches			my $line_fixed = 0;
28373705ce5bSJoe Perches
28389c0ca6f9SAndy Whitcroft			my $ops = qr{
28399c0ca6f9SAndy Whitcroft				<<=|>>=|<=|>=|==|!=|
28409c0ca6f9SAndy Whitcroft				\+=|-=|\*=|\/=|%=|\^=|\|=|&=|
28419c0ca6f9SAndy Whitcroft				=>|->|<<|>>|<|>|=|!|~|
28421f65f947SAndy Whitcroft				&&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
284384731623SJoe Perches				\?:|\?|:
28449c0ca6f9SAndy Whitcroft			}x;
2845cf655043SAndy Whitcroft			my @elements = split(/($ops|;)/, $opline);
28463705ce5bSJoe Perches
28473705ce5bSJoe Perches##			print("element count: <" . $#elements . ">\n");
28483705ce5bSJoe Perches##			foreach my $el (@elements) {
28493705ce5bSJoe Perches##				print("el: <$el>\n");
28503705ce5bSJoe Perches##			}
28513705ce5bSJoe Perches
28523705ce5bSJoe Perches			my @fix_elements = ();
285300df344fSAndy Whitcroft			my $off = 0;
28546c72ffaaSAndy Whitcroft
28553705ce5bSJoe Perches			foreach my $el (@elements) {
28563705ce5bSJoe Perches				push(@fix_elements, substr($rawline, $off, length($el)));
28573705ce5bSJoe Perches				$off += length($el);
28583705ce5bSJoe Perches			}
28593705ce5bSJoe Perches
28603705ce5bSJoe Perches			$off = 0;
28613705ce5bSJoe Perches
28626c72ffaaSAndy Whitcroft			my $blank = copy_spacing($opline);
2863b34c648bSJoe Perches			my $last_after = -1;
28646c72ffaaSAndy Whitcroft
28650a920b5bSAndy Whitcroft			for (my $n = 0; $n < $#elements; $n += 2) {
28663705ce5bSJoe Perches
28673705ce5bSJoe Perches				my $good = $fix_elements[$n] . $fix_elements[$n + 1];
28683705ce5bSJoe Perches
28693705ce5bSJoe Perches##				print("n: <$n> good: <$good>\n");
28703705ce5bSJoe Perches
28714a0df2efSAndy Whitcroft				$off += length($elements[$n]);
28724a0df2efSAndy Whitcroft
287325985edcSLucas De Marchi				# Pick up the preceding and succeeding characters.
2874773647a0SAndy Whitcroft				my $ca = substr($opline, 0, $off);
2875773647a0SAndy Whitcroft				my $cc = '';
2876773647a0SAndy Whitcroft				if (length($opline) >= ($off + length($elements[$n + 1]))) {
2877773647a0SAndy Whitcroft					$cc = substr($opline, $off + length($elements[$n + 1]));
2878773647a0SAndy Whitcroft				}
2879773647a0SAndy Whitcroft				my $cb = "$ca$;$cc";
2880773647a0SAndy Whitcroft
28814a0df2efSAndy Whitcroft				my $a = '';
28824a0df2efSAndy Whitcroft				$a = 'V' if ($elements[$n] ne '');
28834a0df2efSAndy Whitcroft				$a = 'W' if ($elements[$n] =~ /\s$/);
2884cf655043SAndy Whitcroft				$a = 'C' if ($elements[$n] =~ /$;$/);
28854a0df2efSAndy Whitcroft				$a = 'B' if ($elements[$n] =~ /(\[|\()$/);
28864a0df2efSAndy Whitcroft				$a = 'O' if ($elements[$n] eq '');
2887773647a0SAndy Whitcroft				$a = 'E' if ($ca =~ /^\s*$/);
28884a0df2efSAndy Whitcroft
28890a920b5bSAndy Whitcroft				my $op = $elements[$n + 1];
28904a0df2efSAndy Whitcroft
28914a0df2efSAndy Whitcroft				my $c = '';
28920a920b5bSAndy Whitcroft				if (defined $elements[$n + 2]) {
28934a0df2efSAndy Whitcroft					$c = 'V' if ($elements[$n + 2] ne '');
28944a0df2efSAndy Whitcroft					$c = 'W' if ($elements[$n + 2] =~ /^\s/);
2895cf655043SAndy Whitcroft					$c = 'C' if ($elements[$n + 2] =~ /^$;/);
28964a0df2efSAndy Whitcroft					$c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
28974a0df2efSAndy Whitcroft					$c = 'O' if ($elements[$n + 2] eq '');
28988b1b3378SAndy Whitcroft					$c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
28994a0df2efSAndy Whitcroft				} else {
29004a0df2efSAndy Whitcroft					$c = 'E';
29010a920b5bSAndy Whitcroft				}
29020a920b5bSAndy Whitcroft
29034a0df2efSAndy Whitcroft				my $ctx = "${a}x${c}";
29044a0df2efSAndy Whitcroft
29054a0df2efSAndy Whitcroft				my $at = "(ctx:$ctx)";
29064a0df2efSAndy Whitcroft
29076c72ffaaSAndy Whitcroft				my $ptr = substr($blank, 0, $off) . "^";
2908de7d4f0eSAndy Whitcroft				my $hereptr = "$hereline$ptr\n";
29090a920b5bSAndy Whitcroft
291074048ed8SAndy Whitcroft				# Pull out the value of this operator.
29116c72ffaaSAndy Whitcroft				my $op_type = substr($curr_values, $off + 1, 1);
29120a920b5bSAndy Whitcroft
29131f65f947SAndy Whitcroft				# Get the full operator variant.
29141f65f947SAndy Whitcroft				my $opv = $op . substr($curr_vars, $off, 1);
29151f65f947SAndy Whitcroft
291613214adfSAndy Whitcroft				# Ignore operators passed as parameters.
291713214adfSAndy Whitcroft				if ($op_type ne 'V' &&
291813214adfSAndy Whitcroft				    $ca =~ /\s$/ && $cc =~ /^\s*,/) {
291913214adfSAndy Whitcroft
2920cf655043SAndy Whitcroft#				# Ignore comments
2921cf655043SAndy Whitcroft#				} elsif ($op =~ /^$;+$/) {
292213214adfSAndy Whitcroft
2923d8aaf121SAndy Whitcroft				# ; should have either the end of line or a space or \ after it
292413214adfSAndy Whitcroft				} elsif ($op eq ';') {
2925cf655043SAndy Whitcroft					if ($ctx !~ /.x[WEBC]/ &&
2926cf655043SAndy Whitcroft					    $cc !~ /^\\/ && $cc !~ /^;/) {
29273705ce5bSJoe Perches						if (ERROR("SPACING",
29283705ce5bSJoe Perches							  "space required after that '$op' $at\n" . $hereptr)) {
2929b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
29303705ce5bSJoe Perches							$line_fixed = 1;
29313705ce5bSJoe Perches						}
2932d8aaf121SAndy Whitcroft					}
2933d8aaf121SAndy Whitcroft
2934d8aaf121SAndy Whitcroft				# // is a comment
2935d8aaf121SAndy Whitcroft				} elsif ($op eq '//') {
29360a920b5bSAndy Whitcroft
29371f65f947SAndy Whitcroft				# No spaces for:
29381f65f947SAndy Whitcroft				#   ->
29391f65f947SAndy Whitcroft				#   :   when part of a bitfield
29401f65f947SAndy Whitcroft				} elsif ($op eq '->' || $opv eq ':B') {
29414a0df2efSAndy Whitcroft					if ($ctx =~ /Wx.|.xW/) {
29423705ce5bSJoe Perches						if (ERROR("SPACING",
29433705ce5bSJoe Perches							  "spaces prohibited around that '$op' $at\n" . $hereptr)) {
2944b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
29453705ce5bSJoe Perches							if (defined $fix_elements[$n + 2]) {
29463705ce5bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
29473705ce5bSJoe Perches							}
2948b34c648bSJoe Perches							$line_fixed = 1;
29493705ce5bSJoe Perches						}
29500a920b5bSAndy Whitcroft					}
29510a920b5bSAndy Whitcroft
29520a920b5bSAndy Whitcroft				# , must have a space on the right.
29530a920b5bSAndy Whitcroft				} elsif ($op eq ',') {
2954cf655043SAndy Whitcroft					if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
29553705ce5bSJoe Perches						if (ERROR("SPACING",
29563705ce5bSJoe Perches							  "space required after that '$op' $at\n" . $hereptr)) {
2957b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
29583705ce5bSJoe Perches							$line_fixed = 1;
2959b34c648bSJoe Perches							$last_after = $n;
29603705ce5bSJoe Perches						}
29610a920b5bSAndy Whitcroft					}
29620a920b5bSAndy Whitcroft
29639c0ca6f9SAndy Whitcroft				# '*' as part of a type definition -- reported already.
296474048ed8SAndy Whitcroft				} elsif ($opv eq '*_') {
29659c0ca6f9SAndy Whitcroft					#warn "'*' is part of type\n";
29669c0ca6f9SAndy Whitcroft
29679c0ca6f9SAndy Whitcroft				# unary operators should have a space before and
29689c0ca6f9SAndy Whitcroft				# none after.  May be left adjacent to another
29699c0ca6f9SAndy Whitcroft				# unary operator, or a cast
29709c0ca6f9SAndy Whitcroft				} elsif ($op eq '!' || $op eq '~' ||
297174048ed8SAndy Whitcroft					 $opv eq '*U' || $opv eq '-U' ||
29720d413866SAndy Whitcroft					 $opv eq '&U' || $opv eq '&&U') {
2973cf655043SAndy Whitcroft					if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
29743705ce5bSJoe Perches						if (ERROR("SPACING",
29753705ce5bSJoe Perches							  "space required before that '$op' $at\n" . $hereptr)) {
2976b34c648bSJoe Perches							if ($n != $last_after + 2) {
2977b34c648bSJoe Perches								$good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]);
29783705ce5bSJoe Perches								$line_fixed = 1;
29793705ce5bSJoe Perches							}
29800a920b5bSAndy Whitcroft						}
2981b34c648bSJoe Perches					}
2982a3340b35SAndy Whitcroft					if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
2983171ae1a4SAndy Whitcroft						# A unary '*' may be const
2984171ae1a4SAndy Whitcroft
2985171ae1a4SAndy Whitcroft					} elsif ($ctx =~ /.xW/) {
29863705ce5bSJoe Perches						if (ERROR("SPACING",
29873705ce5bSJoe Perches							  "space prohibited after that '$op' $at\n" . $hereptr)) {
2988b34c648bSJoe Perches							$good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]);
29893705ce5bSJoe Perches							if (defined $fix_elements[$n + 2]) {
29903705ce5bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
29913705ce5bSJoe Perches							}
2992b34c648bSJoe Perches							$line_fixed = 1;
29933705ce5bSJoe Perches						}
29940a920b5bSAndy Whitcroft					}
29950a920b5bSAndy Whitcroft
29960a920b5bSAndy Whitcroft				# unary ++ and unary -- are allowed no space on one side.
29970a920b5bSAndy Whitcroft				} elsif ($op eq '++' or $op eq '--') {
2998773647a0SAndy Whitcroft					if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
29993705ce5bSJoe Perches						if (ERROR("SPACING",
30003705ce5bSJoe Perches							  "space required one side of that '$op' $at\n" . $hereptr)) {
3001b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
30023705ce5bSJoe Perches							$line_fixed = 1;
30033705ce5bSJoe Perches						}
30040a920b5bSAndy Whitcroft					}
3005773647a0SAndy Whitcroft					if ($ctx =~ /Wx[BE]/ ||
3006773647a0SAndy Whitcroft					    ($ctx =~ /Wx./ && $cc =~ /^;/)) {
30073705ce5bSJoe Perches						if (ERROR("SPACING",
30083705ce5bSJoe Perches							  "space prohibited before that '$op' $at\n" . $hereptr)) {
3009b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
30103705ce5bSJoe Perches							$line_fixed = 1;
30113705ce5bSJoe Perches						}
3012653d4876SAndy Whitcroft					}
3013773647a0SAndy Whitcroft					if ($ctx =~ /ExW/) {
30143705ce5bSJoe Perches						if (ERROR("SPACING",
30153705ce5bSJoe Perches							  "space prohibited after that '$op' $at\n" . $hereptr)) {
3016b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
30173705ce5bSJoe Perches							if (defined $fix_elements[$n + 2]) {
30183705ce5bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
3019773647a0SAndy Whitcroft							}
3020b34c648bSJoe Perches							$line_fixed = 1;
30213705ce5bSJoe Perches						}
30223705ce5bSJoe Perches					}
30230a920b5bSAndy Whitcroft
30240a920b5bSAndy Whitcroft				# << and >> may either have or not have spaces both sides
30259c0ca6f9SAndy Whitcroft				} elsif ($op eq '<<' or $op eq '>>' or
30269c0ca6f9SAndy Whitcroft					 $op eq '&' or $op eq '^' or $op eq '|' or
30279c0ca6f9SAndy Whitcroft					 $op eq '+' or $op eq '-' or
3028c2fdda0dSAndy Whitcroft					 $op eq '*' or $op eq '/' or
3029c2fdda0dSAndy Whitcroft					 $op eq '%')
30300a920b5bSAndy Whitcroft				{
3031773647a0SAndy Whitcroft					if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
30323705ce5bSJoe Perches						if (ERROR("SPACING",
30333705ce5bSJoe Perches							  "need consistent spacing around '$op' $at\n" . $hereptr)) {
3034b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
3035b34c648bSJoe Perches							if (defined $fix_elements[$n + 2]) {
3036b34c648bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
3037b34c648bSJoe Perches							}
30383705ce5bSJoe Perches							$line_fixed = 1;
30393705ce5bSJoe Perches						}
30400a920b5bSAndy Whitcroft					}
30410a920b5bSAndy Whitcroft
30421f65f947SAndy Whitcroft				# A colon needs no spaces before when it is
30431f65f947SAndy Whitcroft				# terminating a case value or a label.
30441f65f947SAndy Whitcroft				} elsif ($opv eq ':C' || $opv eq ':L') {
30451f65f947SAndy Whitcroft					if ($ctx =~ /Wx./) {
30463705ce5bSJoe Perches						if (ERROR("SPACING",
30473705ce5bSJoe Perches							  "space prohibited before that '$op' $at\n" . $hereptr)) {
3048b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
30493705ce5bSJoe Perches							$line_fixed = 1;
30503705ce5bSJoe Perches						}
30511f65f947SAndy Whitcroft					}
30521f65f947SAndy Whitcroft
30530a920b5bSAndy Whitcroft				# All the others need spaces both sides.
3054cf655043SAndy Whitcroft				} elsif ($ctx !~ /[EWC]x[CWE]/) {
30551f65f947SAndy Whitcroft					my $ok = 0;
30561f65f947SAndy Whitcroft
305722f2a2efSAndy Whitcroft					# Ignore email addresses <foo@bar>
30581f65f947SAndy Whitcroft					if (($op eq '<' &&
30591f65f947SAndy Whitcroft					     $cc =~ /^\S+\@\S+>/) ||
30601f65f947SAndy Whitcroft					    ($op eq '>' &&
30611f65f947SAndy Whitcroft					     $ca =~ /<\S+\@\S+$/))
30621f65f947SAndy Whitcroft					{
30631f65f947SAndy Whitcroft					    	$ok = 1;
30641f65f947SAndy Whitcroft					}
30651f65f947SAndy Whitcroft
306684731623SJoe Perches					# messages are ERROR, but ?: are CHK
30671f65f947SAndy Whitcroft					if ($ok == 0) {
306884731623SJoe Perches						my $msg_type = \&ERROR;
306984731623SJoe Perches						$msg_type = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
307084731623SJoe Perches
307184731623SJoe Perches						if (&{$msg_type}("SPACING",
30723705ce5bSJoe Perches								 "spaces required around that '$op' $at\n" . $hereptr)) {
3073b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
3074b34c648bSJoe Perches							if (defined $fix_elements[$n + 2]) {
3075b34c648bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
3076b34c648bSJoe Perches							}
30773705ce5bSJoe Perches							$line_fixed = 1;
30783705ce5bSJoe Perches						}
30790a920b5bSAndy Whitcroft					}
308022f2a2efSAndy Whitcroft				}
30814a0df2efSAndy Whitcroft				$off += length($elements[$n + 1]);
30823705ce5bSJoe Perches
30833705ce5bSJoe Perches##				print("n: <$n> GOOD: <$good>\n");
30843705ce5bSJoe Perches
30853705ce5bSJoe Perches				$fixed_line = $fixed_line . $good;
30860a920b5bSAndy Whitcroft			}
30873705ce5bSJoe Perches
30883705ce5bSJoe Perches			if (($#elements % 2) == 0) {
30893705ce5bSJoe Perches				$fixed_line = $fixed_line . $fix_elements[$#elements];
30903705ce5bSJoe Perches			}
30913705ce5bSJoe Perches
30923705ce5bSJoe Perches			if ($fix && $line_fixed && $fixed_line ne $fixed[$linenr - 1]) {
30933705ce5bSJoe Perches				$fixed[$linenr - 1] = $fixed_line;
30943705ce5bSJoe Perches			}
30953705ce5bSJoe Perches
30963705ce5bSJoe Perches
30970a920b5bSAndy Whitcroft		}
30980a920b5bSAndy Whitcroft
3099786b6326SJoe Perches# check for whitespace before a non-naked semicolon
3100786b6326SJoe Perches		if ($line =~ /^\+.*\S\s+;/) {
3101786b6326SJoe Perches			if (WARN("SPACING",
3102786b6326SJoe Perches				 "space prohibited before semicolon\n" . $herecurr) &&
3103786b6326SJoe Perches			    $fix) {
3104786b6326SJoe Perches				1 while $fixed[$linenr - 1] =~
3105786b6326SJoe Perches				    s/^(\+.*\S)\s+;/$1;/;
3106786b6326SJoe Perches			}
3107786b6326SJoe Perches		}
3108786b6326SJoe Perches
3109f0a594c1SAndy Whitcroft# check for multiple assignments
3110f0a594c1SAndy Whitcroft		if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
3111000d1cc1SJoe Perches			CHK("MULTIPLE_ASSIGNMENTS",
3112000d1cc1SJoe Perches			    "multiple assignments should be avoided\n" . $herecurr);
3113f0a594c1SAndy Whitcroft		}
3114f0a594c1SAndy Whitcroft
311522f2a2efSAndy Whitcroft## # check for multiple declarations, allowing for a function declaration
311622f2a2efSAndy Whitcroft## # continuation.
311722f2a2efSAndy Whitcroft## 		if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
311822f2a2efSAndy Whitcroft## 		    $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
311922f2a2efSAndy Whitcroft##
312022f2a2efSAndy Whitcroft## 			# Remove any bracketed sections to ensure we do not
312122f2a2efSAndy Whitcroft## 			# falsly report the parameters of functions.
312222f2a2efSAndy Whitcroft## 			my $ln = $line;
312322f2a2efSAndy Whitcroft## 			while ($ln =~ s/\([^\(\)]*\)//g) {
312422f2a2efSAndy Whitcroft## 			}
312522f2a2efSAndy Whitcroft## 			if ($ln =~ /,/) {
3126000d1cc1SJoe Perches## 				WARN("MULTIPLE_DECLARATION",
3127000d1cc1SJoe Perches##				     "declaring multiple variables together should be avoided\n" . $herecurr);
312822f2a2efSAndy Whitcroft## 			}
312922f2a2efSAndy Whitcroft## 		}
3130f0a594c1SAndy Whitcroft
31310a920b5bSAndy Whitcroft#need space before brace following if, while, etc
313222f2a2efSAndy Whitcroft		if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
313322f2a2efSAndy Whitcroft		    $line =~ /do{/) {
31343705ce5bSJoe Perches			if (ERROR("SPACING",
31353705ce5bSJoe Perches				  "space required before the open brace '{'\n" . $herecurr) &&
31363705ce5bSJoe Perches			    $fix) {
3137d5e616fcSJoe Perches				$fixed[$linenr - 1] =~ s/^(\+.*(?:do|\))){/$1 {/;
31383705ce5bSJoe Perches			}
3139de7d4f0eSAndy Whitcroft		}
3140de7d4f0eSAndy Whitcroft
3141c4a62ef9SJoe Perches## # check for blank lines before declarations
3142c4a62ef9SJoe Perches##		if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
3143c4a62ef9SJoe Perches##		    $prevrawline =~ /^.\s*$/) {
3144c4a62ef9SJoe Perches##			WARN("SPACING",
3145c4a62ef9SJoe Perches##			     "No blank lines before declarations\n" . $hereprev);
3146c4a62ef9SJoe Perches##		}
3147c4a62ef9SJoe Perches##
3148c4a62ef9SJoe Perches
3149de7d4f0eSAndy Whitcroft# closing brace should have a space following it when it has anything
3150de7d4f0eSAndy Whitcroft# on the line
3151de7d4f0eSAndy Whitcroft		if ($line =~ /}(?!(?:,|;|\)))\S/) {
3152d5e616fcSJoe Perches			if (ERROR("SPACING",
3153d5e616fcSJoe Perches				  "space required after that close brace '}'\n" . $herecurr) &&
3154d5e616fcSJoe Perches			    $fix) {
3155d5e616fcSJoe Perches				$fixed[$linenr - 1] =~
3156d5e616fcSJoe Perches				    s/}((?!(?:,|;|\)))\S)/} $1/;
3157d5e616fcSJoe Perches			}
31580a920b5bSAndy Whitcroft		}
31590a920b5bSAndy Whitcroft
316022f2a2efSAndy Whitcroft# check spacing on square brackets
316122f2a2efSAndy Whitcroft		if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
31623705ce5bSJoe Perches			if (ERROR("SPACING",
31633705ce5bSJoe Perches				  "space prohibited after that open square bracket '['\n" . $herecurr) &&
31643705ce5bSJoe Perches			    $fix) {
31653705ce5bSJoe Perches				$fixed[$linenr - 1] =~
31663705ce5bSJoe Perches				    s/\[\s+/\[/;
31673705ce5bSJoe Perches			}
316822f2a2efSAndy Whitcroft		}
316922f2a2efSAndy Whitcroft		if ($line =~ /\s\]/) {
31703705ce5bSJoe Perches			if (ERROR("SPACING",
31713705ce5bSJoe Perches				  "space prohibited before that close square bracket ']'\n" . $herecurr) &&
31723705ce5bSJoe Perches			    $fix) {
31733705ce5bSJoe Perches				$fixed[$linenr - 1] =~
31743705ce5bSJoe Perches				    s/\s+\]/\]/;
31753705ce5bSJoe Perches			}
317622f2a2efSAndy Whitcroft		}
317722f2a2efSAndy Whitcroft
3178c45dcabdSAndy Whitcroft# check spacing on parentheses
31799c0ca6f9SAndy Whitcroft		if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
31809c0ca6f9SAndy Whitcroft		    $line !~ /for\s*\(\s+;/) {
31813705ce5bSJoe Perches			if (ERROR("SPACING",
31823705ce5bSJoe Perches				  "space prohibited after that open parenthesis '('\n" . $herecurr) &&
31833705ce5bSJoe Perches			    $fix) {
31843705ce5bSJoe Perches				$fixed[$linenr - 1] =~
31853705ce5bSJoe Perches				    s/\(\s+/\(/;
31863705ce5bSJoe Perches			}
318722f2a2efSAndy Whitcroft		}
318813214adfSAndy Whitcroft		if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
3189c45dcabdSAndy Whitcroft		    $line !~ /for\s*\(.*;\s+\)/ &&
3190c45dcabdSAndy Whitcroft		    $line !~ /:\s+\)/) {
31913705ce5bSJoe Perches			if (ERROR("SPACING",
31923705ce5bSJoe Perches				  "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
31933705ce5bSJoe Perches			    $fix) {
31943705ce5bSJoe Perches				$fixed[$linenr - 1] =~
31953705ce5bSJoe Perches				    s/\s+\)/\)/;
31963705ce5bSJoe Perches			}
319722f2a2efSAndy Whitcroft		}
319822f2a2efSAndy Whitcroft
31990a920b5bSAndy Whitcroft#goto labels aren't indented, allow a single space however
32004a0df2efSAndy Whitcroft		if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
32010a920b5bSAndy Whitcroft		   !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
32023705ce5bSJoe Perches			if (WARN("INDENTED_LABEL",
32033705ce5bSJoe Perches				 "labels should not be indented\n" . $herecurr) &&
32043705ce5bSJoe Perches			    $fix) {
32053705ce5bSJoe Perches				$fixed[$linenr - 1] =~
32063705ce5bSJoe Perches				    s/^(.)\s+/$1/;
32073705ce5bSJoe Perches			}
32080a920b5bSAndy Whitcroft		}
32090a920b5bSAndy Whitcroft
3210c45dcabdSAndy Whitcroft# Return is not a function.
3211c45dcabdSAndy Whitcroft		if (defined($stat) && $stat =~ /^.\s*return(\s*)(\(.*);/s) {
3212c45dcabdSAndy Whitcroft			my $spacing = $1;
3213c45dcabdSAndy Whitcroft			my $value = $2;
3214c45dcabdSAndy Whitcroft
321586f9d059SAndy Whitcroft			# Flatten any parentheses
3216fb2d2c1bSAndy Whitcroft			$value =~ s/\(/ \(/g;
3217fb2d2c1bSAndy Whitcroft			$value =~ s/\)/\) /g;
3218e01886adSAndy Whitcroft			while ($value =~ s/\[[^\[\]]*\]/1/ ||
321963f17f89SAndy Whitcroft			       $value !~ /(?:$Ident|-?$Constant)\s*
322063f17f89SAndy Whitcroft					     $Compare\s*
322163f17f89SAndy Whitcroft					     (?:$Ident|-?$Constant)/x &&
322263f17f89SAndy Whitcroft			       $value =~ s/\([^\(\)]*\)/1/) {
3223c45dcabdSAndy Whitcroft			}
3224fb2d2c1bSAndy Whitcroft#print "value<$value>\n";
3225fb2d2c1bSAndy Whitcroft			if ($value =~ /^\s*(?:$Ident|-?$Constant)\s*$/) {
3226000d1cc1SJoe Perches				ERROR("RETURN_PARENTHESES",
3227000d1cc1SJoe Perches				      "return is not a function, parentheses are not required\n" . $herecurr);
3228c45dcabdSAndy Whitcroft
3229c45dcabdSAndy Whitcroft			} elsif ($spacing !~ /\s+/) {
3230000d1cc1SJoe Perches				ERROR("SPACING",
3231000d1cc1SJoe Perches				      "space required before the open parenthesis '('\n" . $herecurr);
3232c45dcabdSAndy Whitcroft			}
3233c45dcabdSAndy Whitcroft		}
323453a3c448SAndy Whitcroft# Return of what appears to be an errno should normally be -'ve
323553a3c448SAndy Whitcroft		if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) {
323653a3c448SAndy Whitcroft			my $name = $1;
323753a3c448SAndy Whitcroft			if ($name ne 'EOF' && $name ne 'ERROR') {
3238000d1cc1SJoe Perches				WARN("USE_NEGATIVE_ERRNO",
3239000d1cc1SJoe Perches				     "return of an errno should typically be -ve (return -$1)\n" . $herecurr);
324053a3c448SAndy Whitcroft			}
324153a3c448SAndy Whitcroft		}
3242c45dcabdSAndy Whitcroft
32430a920b5bSAndy Whitcroft# Need a space before open parenthesis after if, while etc
32444a0df2efSAndy Whitcroft		if ($line =~ /\b(if|while|for|switch)\(/) {
32453705ce5bSJoe Perches			if (ERROR("SPACING",
32463705ce5bSJoe Perches				  "space required before the open parenthesis '('\n" . $herecurr) &&
32473705ce5bSJoe Perches			    $fix) {
32483705ce5bSJoe Perches				$fixed[$linenr - 1] =~
32493705ce5bSJoe Perches				    s/\b(if|while|for|switch)\(/$1 \(/;
32503705ce5bSJoe Perches			}
32510a920b5bSAndy Whitcroft		}
32520a920b5bSAndy Whitcroft
3253f5fe35ddSAndy Whitcroft# Check for illegal assignment in if conditional -- and check for trailing
3254f5fe35ddSAndy Whitcroft# statements after the conditional.
3255170d3a22SAndy Whitcroft		if ($line =~ /do\s*(?!{)/) {
32563e469cdcSAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
32573e469cdcSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0)
32583e469cdcSAndy Whitcroft					if (!defined $stat);
3259170d3a22SAndy Whitcroft			my ($stat_next) = ctx_statement_block($line_nr_next,
3260170d3a22SAndy Whitcroft						$remain_next, $off_next);
3261170d3a22SAndy Whitcroft			$stat_next =~ s/\n./\n /g;
3262170d3a22SAndy Whitcroft			##print "stat<$stat> stat_next<$stat_next>\n";
3263170d3a22SAndy Whitcroft
3264170d3a22SAndy Whitcroft			if ($stat_next =~ /^\s*while\b/) {
3265170d3a22SAndy Whitcroft				# If the statement carries leading newlines,
3266170d3a22SAndy Whitcroft				# then count those as offsets.
3267170d3a22SAndy Whitcroft				my ($whitespace) =
3268170d3a22SAndy Whitcroft					($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
3269170d3a22SAndy Whitcroft				my $offset =
3270170d3a22SAndy Whitcroft					statement_rawlines($whitespace) - 1;
3271170d3a22SAndy Whitcroft
3272170d3a22SAndy Whitcroft				$suppress_whiletrailers{$line_nr_next +
3273170d3a22SAndy Whitcroft								$offset} = 1;
3274170d3a22SAndy Whitcroft			}
3275170d3a22SAndy Whitcroft		}
3276170d3a22SAndy Whitcroft		if (!defined $suppress_whiletrailers{$linenr} &&
3277170d3a22SAndy Whitcroft		    $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
3278171ae1a4SAndy Whitcroft			my ($s, $c) = ($stat, $cond);
32798905a67cSAndy Whitcroft
3280b53c8e10SAndy Whitcroft			if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
3281000d1cc1SJoe Perches				ERROR("ASSIGN_IN_IF",
3282000d1cc1SJoe Perches				      "do not use assignment in if condition\n" . $herecurr);
32838905a67cSAndy Whitcroft			}
32848905a67cSAndy Whitcroft
32858905a67cSAndy Whitcroft			# Find out what is on the end of the line after the
32868905a67cSAndy Whitcroft			# conditional.
3287773647a0SAndy Whitcroft			substr($s, 0, length($c), '');
32888905a67cSAndy Whitcroft			$s =~ s/\n.*//g;
328913214adfSAndy Whitcroft			$s =~ s/$;//g; 	# Remove any comments
329053210168SAndy Whitcroft			if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
329153210168SAndy Whitcroft			    $c !~ /}\s*while\s*/)
3292773647a0SAndy Whitcroft			{
3293bb44ad39SAndy Whitcroft				# Find out how long the conditional actually is.
3294bb44ad39SAndy Whitcroft				my @newlines = ($c =~ /\n/gs);
3295bb44ad39SAndy Whitcroft				my $cond_lines = 1 + $#newlines;
329642bdf74cSHidetoshi Seto				my $stat_real = '';
3297bb44ad39SAndy Whitcroft
329842bdf74cSHidetoshi Seto				$stat_real = raw_line($linenr, $cond_lines)
329942bdf74cSHidetoshi Seto							. "\n" if ($cond_lines);
3300bb44ad39SAndy Whitcroft				if (defined($stat_real) && $cond_lines > 1) {
3301bb44ad39SAndy Whitcroft					$stat_real = "[...]\n$stat_real";
3302bb44ad39SAndy Whitcroft				}
3303bb44ad39SAndy Whitcroft
3304000d1cc1SJoe Perches				ERROR("TRAILING_STATEMENTS",
3305000d1cc1SJoe Perches				      "trailing statements should be on next line\n" . $herecurr . $stat_real);
33068905a67cSAndy Whitcroft			}
33078905a67cSAndy Whitcroft		}
33088905a67cSAndy Whitcroft
330913214adfSAndy Whitcroft# Check for bitwise tests written as boolean
331013214adfSAndy Whitcroft		if ($line =~ /
331113214adfSAndy Whitcroft			(?:
331213214adfSAndy Whitcroft				(?:\[|\(|\&\&|\|\|)
331313214adfSAndy Whitcroft				\s*0[xX][0-9]+\s*
331413214adfSAndy Whitcroft				(?:\&\&|\|\|)
331513214adfSAndy Whitcroft			|
331613214adfSAndy Whitcroft				(?:\&\&|\|\|)
331713214adfSAndy Whitcroft				\s*0[xX][0-9]+\s*
331813214adfSAndy Whitcroft				(?:\&\&|\|\||\)|\])
331913214adfSAndy Whitcroft			)/x)
332013214adfSAndy Whitcroft		{
3321000d1cc1SJoe Perches			WARN("HEXADECIMAL_BOOLEAN_TEST",
3322000d1cc1SJoe Perches			     "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
332313214adfSAndy Whitcroft		}
332413214adfSAndy Whitcroft
33258905a67cSAndy Whitcroft# if and else should not have general statements after it
332613214adfSAndy Whitcroft		if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
332713214adfSAndy Whitcroft			my $s = $1;
332813214adfSAndy Whitcroft			$s =~ s/$;//g; 	# Remove any comments
332913214adfSAndy Whitcroft			if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
3330000d1cc1SJoe Perches				ERROR("TRAILING_STATEMENTS",
3331000d1cc1SJoe Perches				      "trailing statements should be on next line\n" . $herecurr);
33320a920b5bSAndy Whitcroft			}
333313214adfSAndy Whitcroft		}
333439667782SAndy Whitcroft# if should not continue a brace
333539667782SAndy Whitcroft		if ($line =~ /}\s*if\b/) {
3336000d1cc1SJoe Perches			ERROR("TRAILING_STATEMENTS",
3337000d1cc1SJoe Perches			      "trailing statements should be on next line\n" .
333839667782SAndy Whitcroft				$herecurr);
333939667782SAndy Whitcroft		}
3340a1080bf8SAndy Whitcroft# case and default should not have general statements after them
3341a1080bf8SAndy Whitcroft		if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
3342a1080bf8SAndy Whitcroft		    $line !~ /\G(?:
33433fef12d6SAndy Whitcroft			(?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
3344a1080bf8SAndy Whitcroft			\s*return\s+
3345a1080bf8SAndy Whitcroft		    )/xg)
3346a1080bf8SAndy Whitcroft		{
3347000d1cc1SJoe Perches			ERROR("TRAILING_STATEMENTS",
3348000d1cc1SJoe Perches			      "trailing statements should be on next line\n" . $herecurr);
3349a1080bf8SAndy Whitcroft		}
33500a920b5bSAndy Whitcroft
33510a920b5bSAndy Whitcroft		# Check for }<nl>else {, these must be at the same
33520a920b5bSAndy Whitcroft		# indent level to be relevant to each other.
33530a920b5bSAndy Whitcroft		if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
33540a920b5bSAndy Whitcroft						$previndent == $indent) {
3355000d1cc1SJoe Perches			ERROR("ELSE_AFTER_BRACE",
3356000d1cc1SJoe Perches			      "else should follow close brace '}'\n" . $hereprev);
33570a920b5bSAndy Whitcroft		}
33580a920b5bSAndy Whitcroft
3359c2fdda0dSAndy Whitcroft		if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and
3360c2fdda0dSAndy Whitcroft						$previndent == $indent) {
3361c2fdda0dSAndy Whitcroft			my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
3362c2fdda0dSAndy Whitcroft
3363c2fdda0dSAndy Whitcroft			# Find out what is on the end of the line after the
3364c2fdda0dSAndy Whitcroft			# conditional.
3365773647a0SAndy Whitcroft			substr($s, 0, length($c), '');
3366c2fdda0dSAndy Whitcroft			$s =~ s/\n.*//g;
3367c2fdda0dSAndy Whitcroft
3368c2fdda0dSAndy Whitcroft			if ($s =~ /^\s*;/) {
3369000d1cc1SJoe Perches				ERROR("WHILE_AFTER_BRACE",
3370000d1cc1SJoe Perches				      "while should follow close brace '}'\n" . $hereprev);
3371c2fdda0dSAndy Whitcroft			}
3372c2fdda0dSAndy Whitcroft		}
3373c2fdda0dSAndy Whitcroft
337495e2c602SJoe Perches#Specific variable tests
3375323c1260SJoe Perches		while ($line =~ m{($Constant|$Lval)}g) {
3376323c1260SJoe Perches			my $var = $1;
337795e2c602SJoe Perches
337895e2c602SJoe Perches#gcc binary extension
337995e2c602SJoe Perches			if ($var =~ /^$Binary$/) {
3380d5e616fcSJoe Perches				if (WARN("GCC_BINARY_CONSTANT",
3381d5e616fcSJoe Perches					 "Avoid gcc v4.3+ binary constant extension: <$var>\n" . $herecurr) &&
3382d5e616fcSJoe Perches				    $fix) {
3383d5e616fcSJoe Perches					my $hexval = sprintf("0x%x", oct($var));
3384d5e616fcSJoe Perches					$fixed[$linenr - 1] =~
3385d5e616fcSJoe Perches					    s/\b$var\b/$hexval/;
3386d5e616fcSJoe Perches				}
338795e2c602SJoe Perches			}
338895e2c602SJoe Perches
338995e2c602SJoe Perches#CamelCase
3390807bd26cSJoe Perches			if ($var !~ /^$Constant$/ &&
3391be79794bSJoe Perches			    $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
339222735ce8SJoe Perches#Ignore Page<foo> variants
3393807bd26cSJoe Perches			    $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
339422735ce8SJoe Perches#Ignore SI style variants like nS, mV and dB (ie: max_uV, regulator_min_uA_show)
33953445686aSJoe Perches			    $var !~ /^(?:[a-z_]*?)_?[a-z][A-Z](?:_[a-z_]+)?$/) {
33967e781f67SJoe Perches				while ($var =~ m{($Ident)}g) {
33977e781f67SJoe Perches					my $word = $1;
33987e781f67SJoe Perches					next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
3399*d8b07710SJoe Perches					if ($check) {
3400*d8b07710SJoe Perches						seed_camelcase_includes();
3401*d8b07710SJoe Perches						if (!$file && !$camelcase_file_seeded) {
3402*d8b07710SJoe Perches							seed_camelcase_file($realfile);
3403*d8b07710SJoe Perches							$camelcase_file_seeded = 1;
3404*d8b07710SJoe Perches						}
3405*d8b07710SJoe Perches					}
34067e781f67SJoe Perches					if (!defined $camelcase{$word}) {
34077e781f67SJoe Perches						$camelcase{$word} = 1;
3408be79794bSJoe Perches						CHK("CAMELCASE",
34097e781f67SJoe Perches						    "Avoid CamelCase: <$word>\n" . $herecurr);
34107e781f67SJoe Perches					}
3411323c1260SJoe Perches				}
3412323c1260SJoe Perches			}
34133445686aSJoe Perches		}
34140a920b5bSAndy Whitcroft
34150a920b5bSAndy Whitcroft#no spaces allowed after \ in define
3416d5e616fcSJoe Perches		if ($line =~ /\#\s*define.*\\\s+$/) {
3417d5e616fcSJoe Perches			if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
3418d5e616fcSJoe Perches				 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
3419d5e616fcSJoe Perches			    $fix) {
3420d5e616fcSJoe Perches				$fixed[$linenr - 1] =~ s/\s+$//;
3421d5e616fcSJoe Perches			}
34220a920b5bSAndy Whitcroft		}
34230a920b5bSAndy Whitcroft
3424653d4876SAndy Whitcroft#warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
3425c45dcabdSAndy Whitcroft		if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
3426e09dec48SAndy Whitcroft			my $file = "$1.h";
3427e09dec48SAndy Whitcroft			my $checkfile = "include/linux/$file";
3428e09dec48SAndy Whitcroft			if (-f "$root/$checkfile" &&
3429e09dec48SAndy Whitcroft			    $realfile ne $checkfile &&
34307840a94cSWolfram Sang			    $1 !~ /$allowed_asm_includes/)
3431c45dcabdSAndy Whitcroft			{
3432e09dec48SAndy Whitcroft				if ($realfile =~ m{^arch/}) {
3433000d1cc1SJoe Perches					CHK("ARCH_INCLUDE_LINUX",
3434000d1cc1SJoe Perches					    "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
3435e09dec48SAndy Whitcroft				} else {
3436000d1cc1SJoe Perches					WARN("INCLUDE_LINUX",
3437000d1cc1SJoe Perches					     "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
3438e09dec48SAndy Whitcroft				}
34390a920b5bSAndy Whitcroft			}
34400a920b5bSAndy Whitcroft		}
34410a920b5bSAndy Whitcroft
3442653d4876SAndy Whitcroft# multi-statement macros should be enclosed in a do while loop, grab the
3443653d4876SAndy Whitcroft# first statement and ensure its the whole macro if its not enclosed
3444cf655043SAndy Whitcroft# in a known good container
3445b8f96a31SAndy Whitcroft		if ($realfile !~ m@/vmlinux.lds.h$@ &&
3446b8f96a31SAndy Whitcroft		    $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
3447d8aaf121SAndy Whitcroft			my $ln = $linenr;
3448d8aaf121SAndy Whitcroft			my $cnt = $realcnt;
3449c45dcabdSAndy Whitcroft			my ($off, $dstat, $dcond, $rest);
3450c45dcabdSAndy Whitcroft			my $ctx = '';
3451c45dcabdSAndy Whitcroft			($dstat, $dcond, $ln, $cnt, $off) =
3452f74bd194SAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0);
3453f74bd194SAndy Whitcroft			$ctx = $dstat;
3454c45dcabdSAndy Whitcroft			#print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
3455a3bb97a7SAndy Whitcroft			#print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
3456c45dcabdSAndy Whitcroft
3457f74bd194SAndy Whitcroft			$dstat =~ s/^.\s*\#\s*define\s+$Ident(?:\([^\)]*\))?\s*//;
3458292f1a9bSAndy Whitcroft			$dstat =~ s/$;//g;
3459c45dcabdSAndy Whitcroft			$dstat =~ s/\\\n.//g;
3460c45dcabdSAndy Whitcroft			$dstat =~ s/^\s*//s;
3461c45dcabdSAndy Whitcroft			$dstat =~ s/\s*$//s;
3462c45dcabdSAndy Whitcroft
3463c45dcabdSAndy Whitcroft			# Flatten any parentheses and braces
3464bf30d6edSAndy Whitcroft			while ($dstat =~ s/\([^\(\)]*\)/1/ ||
3465bf30d6edSAndy Whitcroft			       $dstat =~ s/\{[^\{\}]*\}/1/ ||
3466c81769fdSAndy Whitcroft			       $dstat =~ s/\[[^\[\]]*\]/1/)
3467bf30d6edSAndy Whitcroft			{
3468c45dcabdSAndy Whitcroft			}
3469c45dcabdSAndy Whitcroft
3470e45bab8eSAndy Whitcroft			# Flatten any obvious string concatentation.
3471e45bab8eSAndy Whitcroft			while ($dstat =~ s/("X*")\s*$Ident/$1/ ||
3472e45bab8eSAndy Whitcroft			       $dstat =~ s/$Ident\s*("X*")/$1/)
3473e45bab8eSAndy Whitcroft			{
3474e45bab8eSAndy Whitcroft			}
3475e45bab8eSAndy Whitcroft
3476c45dcabdSAndy Whitcroft			my $exceptions = qr{
3477c45dcabdSAndy Whitcroft				$Declare|
3478c45dcabdSAndy Whitcroft				module_param_named|
3479a0a0a7a9SKees Cook				MODULE_PARM_DESC|
3480c45dcabdSAndy Whitcroft				DECLARE_PER_CPU|
3481c45dcabdSAndy Whitcroft				DEFINE_PER_CPU|
3482383099fdSAndy Whitcroft				__typeof__\(|
348322fd2d3eSStefani Seibold				union|
348422fd2d3eSStefani Seibold				struct|
3485ea71a0a0SAndy Whitcroft				\.$Ident\s*=\s*|
3486ea71a0a0SAndy Whitcroft				^\"|\"$
3487c45dcabdSAndy Whitcroft			}x;
34885eaa20b9SAndy Whitcroft			#print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
3489f74bd194SAndy Whitcroft			if ($dstat ne '' &&
3490f74bd194SAndy Whitcroft			    $dstat !~ /^(?:$Ident|-?$Constant),$/ &&			# 10, // foo(),
3491f74bd194SAndy Whitcroft			    $dstat !~ /^(?:$Ident|-?$Constant);$/ &&			# foo();
34923cc4b1c3SJoe Perches			    $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ &&		# 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
3493b9df76acSAndy Whitcroft			    $dstat !~ /^'X'$/ &&					# character constants
3494f74bd194SAndy Whitcroft			    $dstat !~ /$exceptions/ &&
3495f74bd194SAndy Whitcroft			    $dstat !~ /^\.$Ident\s*=/ &&				# .foo =
3496e942e2c3SJoe Perches			    $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ &&		# stringification #foo
349772f115f9SAndy Whitcroft			    $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ &&	# do {...} while (...); // do {...} while (...)
3498f74bd194SAndy Whitcroft			    $dstat !~ /^for\s*$Constant$/ &&				# for (...)
3499f74bd194SAndy Whitcroft			    $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ &&	# for (...) bar()
3500f74bd194SAndy Whitcroft			    $dstat !~ /^do\s*{/ &&					# do {...
3501f95a7e6aSJoe Perches			    $dstat !~ /^\({/ &&						# ({...
3502f95a7e6aSJoe Perches			    $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
3503c45dcabdSAndy Whitcroft			{
3504f74bd194SAndy Whitcroft				$ctx =~ s/\n*$//;
3505f74bd194SAndy Whitcroft				my $herectx = $here . "\n";
3506f74bd194SAndy Whitcroft				my $cnt = statement_rawlines($ctx);
3507f74bd194SAndy Whitcroft
3508f74bd194SAndy Whitcroft				for (my $n = 0; $n < $cnt; $n++) {
3509f74bd194SAndy Whitcroft					$herectx .= raw_line($linenr, $n) . "\n";
3510c45dcabdSAndy Whitcroft				}
3511c45dcabdSAndy Whitcroft
3512f74bd194SAndy Whitcroft				if ($dstat =~ /;/) {
3513f74bd194SAndy Whitcroft					ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
3514f74bd194SAndy Whitcroft					      "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
3515f74bd194SAndy Whitcroft				} else {
3516000d1cc1SJoe Perches					ERROR("COMPLEX_MACRO",
3517f74bd194SAndy Whitcroft					      "Macros with complex values should be enclosed in parenthesis\n" . "$herectx");
3518d8aaf121SAndy Whitcroft				}
35190a920b5bSAndy Whitcroft			}
35205023d347SJoe Perches
3521481eb486SJoe Perches# check for line continuations outside of #defines, preprocessor #, and asm
35225023d347SJoe Perches
35235023d347SJoe Perches		} else {
35245023d347SJoe Perches			if ($prevline !~ /^..*\\$/ &&
3525481eb486SJoe Perches			    $line !~ /^\+\s*\#.*\\$/ &&		# preprocessor
3526481eb486SJoe Perches			    $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ &&	# asm
35275023d347SJoe Perches			    $line =~ /^\+.*\\$/) {
35285023d347SJoe Perches				WARN("LINE_CONTINUATIONS",
35295023d347SJoe Perches				     "Avoid unnecessary line continuations\n" . $herecurr);
35305023d347SJoe Perches			}
3531653d4876SAndy Whitcroft		}
35320a920b5bSAndy Whitcroft
3533b13edf7fSJoe Perches# do {} while (0) macro tests:
3534b13edf7fSJoe Perches# single-statement macros do not need to be enclosed in do while (0) loop,
3535b13edf7fSJoe Perches# macro should not end with a semicolon
3536b13edf7fSJoe Perches		if ($^V && $^V ge 5.10.0 &&
3537b13edf7fSJoe Perches		    $realfile !~ m@/vmlinux.lds.h$@ &&
3538b13edf7fSJoe Perches		    $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
3539b13edf7fSJoe Perches			my $ln = $linenr;
3540b13edf7fSJoe Perches			my $cnt = $realcnt;
3541b13edf7fSJoe Perches			my ($off, $dstat, $dcond, $rest);
3542b13edf7fSJoe Perches			my $ctx = '';
3543b13edf7fSJoe Perches			($dstat, $dcond, $ln, $cnt, $off) =
3544b13edf7fSJoe Perches				ctx_statement_block($linenr, $realcnt, 0);
3545b13edf7fSJoe Perches			$ctx = $dstat;
3546b13edf7fSJoe Perches
3547b13edf7fSJoe Perches			$dstat =~ s/\\\n.//g;
3548b13edf7fSJoe Perches
3549b13edf7fSJoe Perches			if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
3550b13edf7fSJoe Perches				my $stmts = $2;
3551b13edf7fSJoe Perches				my $semis = $3;
3552b13edf7fSJoe Perches
3553b13edf7fSJoe Perches				$ctx =~ s/\n*$//;
3554b13edf7fSJoe Perches				my $cnt = statement_rawlines($ctx);
3555b13edf7fSJoe Perches				my $herectx = $here . "\n";
3556b13edf7fSJoe Perches
3557b13edf7fSJoe Perches				for (my $n = 0; $n < $cnt; $n++) {
3558b13edf7fSJoe Perches					$herectx .= raw_line($linenr, $n) . "\n";
3559b13edf7fSJoe Perches				}
3560b13edf7fSJoe Perches
3561ac8e97f8SJoe Perches				if (($stmts =~ tr/;/;/) == 1 &&
3562ac8e97f8SJoe Perches				    $stmts !~ /^\s*(if|while|for|switch)\b/) {
3563b13edf7fSJoe Perches					WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
3564b13edf7fSJoe Perches					     "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
3565b13edf7fSJoe Perches				}
3566b13edf7fSJoe Perches				if (defined $semis && $semis ne "") {
3567b13edf7fSJoe Perches					WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
3568b13edf7fSJoe Perches					     "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
3569b13edf7fSJoe Perches				}
3570b13edf7fSJoe Perches			}
3571b13edf7fSJoe Perches		}
3572b13edf7fSJoe Perches
3573080ba929SMike Frysinger# make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
3574080ba929SMike Frysinger# all assignments may have only one of the following with an assignment:
3575080ba929SMike Frysinger#	.
3576080ba929SMike Frysinger#	ALIGN(...)
3577080ba929SMike Frysinger#	VMLINUX_SYMBOL(...)
3578080ba929SMike Frysinger		if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
3579000d1cc1SJoe Perches			WARN("MISSING_VMLINUX_SYMBOL",
3580000d1cc1SJoe Perches			     "vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
3581080ba929SMike Frysinger		}
3582080ba929SMike Frysinger
3583f0a594c1SAndy Whitcroft# check for redundant bracing round if etc
358413214adfSAndy Whitcroft		if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
358513214adfSAndy Whitcroft			my ($level, $endln, @chunks) =
3586cf655043SAndy Whitcroft				ctx_statement_full($linenr, $realcnt, 1);
358713214adfSAndy Whitcroft			#print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
3588cf655043SAndy Whitcroft			#print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
3589cf655043SAndy Whitcroft			if ($#chunks > 0 && $level == 0) {
3590aad4f614SJoe Perches				my @allowed = ();
3591aad4f614SJoe Perches				my $allow = 0;
359213214adfSAndy Whitcroft				my $seen = 0;
3593773647a0SAndy Whitcroft				my $herectx = $here . "\n";
3594cf655043SAndy Whitcroft				my $ln = $linenr - 1;
359513214adfSAndy Whitcroft				for my $chunk (@chunks) {
359613214adfSAndy Whitcroft					my ($cond, $block) = @{$chunk};
359713214adfSAndy Whitcroft
3598773647a0SAndy Whitcroft					# If the condition carries leading newlines, then count those as offsets.
3599773647a0SAndy Whitcroft					my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
3600773647a0SAndy Whitcroft					my $offset = statement_rawlines($whitespace) - 1;
3601773647a0SAndy Whitcroft
3602aad4f614SJoe Perches					$allowed[$allow] = 0;
3603773647a0SAndy Whitcroft					#print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
3604773647a0SAndy Whitcroft
3605773647a0SAndy Whitcroft					# We have looked at and allowed this specific line.
3606773647a0SAndy Whitcroft					$suppress_ifbraces{$ln + $offset} = 1;
3607773647a0SAndy Whitcroft
3608773647a0SAndy Whitcroft					$herectx .= "$rawlines[$ln + $offset]\n[...]\n";
3609cf655043SAndy Whitcroft					$ln += statement_rawlines($block) - 1;
3610cf655043SAndy Whitcroft
3611773647a0SAndy Whitcroft					substr($block, 0, length($cond), '');
361213214adfSAndy Whitcroft
361313214adfSAndy Whitcroft					$seen++ if ($block =~ /^\s*{/);
361413214adfSAndy Whitcroft
3615aad4f614SJoe Perches					#print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
3616cf655043SAndy Whitcroft					if (statement_lines($cond) > 1) {
3617cf655043SAndy Whitcroft						#print "APW: ALLOWED: cond<$cond>\n";
3618aad4f614SJoe Perches						$allowed[$allow] = 1;
361913214adfSAndy Whitcroft					}
362013214adfSAndy Whitcroft					if ($block =~/\b(?:if|for|while)\b/) {
3621cf655043SAndy Whitcroft						#print "APW: ALLOWED: block<$block>\n";
3622aad4f614SJoe Perches						$allowed[$allow] = 1;
362313214adfSAndy Whitcroft					}
3624cf655043SAndy Whitcroft					if (statement_block_size($block) > 1) {
3625cf655043SAndy Whitcroft						#print "APW: ALLOWED: lines block<$block>\n";
3626aad4f614SJoe Perches						$allowed[$allow] = 1;
362713214adfSAndy Whitcroft					}
3628aad4f614SJoe Perches					$allow++;
362913214adfSAndy Whitcroft				}
3630aad4f614SJoe Perches				if ($seen) {
3631aad4f614SJoe Perches					my $sum_allowed = 0;
3632aad4f614SJoe Perches					foreach (@allowed) {
3633aad4f614SJoe Perches						$sum_allowed += $_;
3634aad4f614SJoe Perches					}
3635aad4f614SJoe Perches					if ($sum_allowed == 0) {
3636000d1cc1SJoe Perches						WARN("BRACES",
3637000d1cc1SJoe Perches						     "braces {} are not necessary for any arm of this statement\n" . $herectx);
3638aad4f614SJoe Perches					} elsif ($sum_allowed != $allow &&
3639aad4f614SJoe Perches						 $seen != $allow) {
3640aad4f614SJoe Perches						CHK("BRACES",
3641aad4f614SJoe Perches						    "braces {} should be used on all arms of this statement\n" . $herectx);
3642aad4f614SJoe Perches					}
364313214adfSAndy Whitcroft				}
364413214adfSAndy Whitcroft			}
364513214adfSAndy Whitcroft		}
3646773647a0SAndy Whitcroft		if (!defined $suppress_ifbraces{$linenr - 1} &&
364713214adfSAndy Whitcroft					$line =~ /\b(if|while|for|else)\b/) {
3648cf655043SAndy Whitcroft			my $allowed = 0;
3649f0a594c1SAndy Whitcroft
3650cf655043SAndy Whitcroft			# Check the pre-context.
3651cf655043SAndy Whitcroft			if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
3652cf655043SAndy Whitcroft				#print "APW: ALLOWED: pre<$1>\n";
3653cf655043SAndy Whitcroft				$allowed = 1;
3654f0a594c1SAndy Whitcroft			}
3655773647a0SAndy Whitcroft
3656773647a0SAndy Whitcroft			my ($level, $endln, @chunks) =
3657773647a0SAndy Whitcroft				ctx_statement_full($linenr, $realcnt, $-[0]);
3658773647a0SAndy Whitcroft
3659cf655043SAndy Whitcroft			# Check the condition.
3660cf655043SAndy Whitcroft			my ($cond, $block) = @{$chunks[0]};
3661773647a0SAndy Whitcroft			#print "CHECKING<$linenr> cond<$cond> block<$block>\n";
3662cf655043SAndy Whitcroft			if (defined $cond) {
3663773647a0SAndy Whitcroft				substr($block, 0, length($cond), '');
3664cf655043SAndy Whitcroft			}
3665cf655043SAndy Whitcroft			if (statement_lines($cond) > 1) {
3666cf655043SAndy Whitcroft				#print "APW: ALLOWED: cond<$cond>\n";
3667cf655043SAndy Whitcroft				$allowed = 1;
3668cf655043SAndy Whitcroft			}
3669cf655043SAndy Whitcroft			if ($block =~/\b(?:if|for|while)\b/) {
3670cf655043SAndy Whitcroft				#print "APW: ALLOWED: block<$block>\n";
3671cf655043SAndy Whitcroft				$allowed = 1;
3672cf655043SAndy Whitcroft			}
3673cf655043SAndy Whitcroft			if (statement_block_size($block) > 1) {
3674cf655043SAndy Whitcroft				#print "APW: ALLOWED: lines block<$block>\n";
3675cf655043SAndy Whitcroft				$allowed = 1;
3676cf655043SAndy Whitcroft			}
3677cf655043SAndy Whitcroft			# Check the post-context.
3678cf655043SAndy Whitcroft			if (defined $chunks[1]) {
3679cf655043SAndy Whitcroft				my ($cond, $block) = @{$chunks[1]};
3680cf655043SAndy Whitcroft				if (defined $cond) {
3681773647a0SAndy Whitcroft					substr($block, 0, length($cond), '');
3682cf655043SAndy Whitcroft				}
3683cf655043SAndy Whitcroft				if ($block =~ /^\s*\{/) {
3684cf655043SAndy Whitcroft					#print "APW: ALLOWED: chunk-1 block<$block>\n";
3685cf655043SAndy Whitcroft					$allowed = 1;
3686cf655043SAndy Whitcroft				}
3687cf655043SAndy Whitcroft			}
3688cf655043SAndy Whitcroft			if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
368969932487SJustin P. Mattock				my $herectx = $here . "\n";
3690f055663cSAndy Whitcroft				my $cnt = statement_rawlines($block);
3691cf655043SAndy Whitcroft
3692f055663cSAndy Whitcroft				for (my $n = 0; $n < $cnt; $n++) {
369369932487SJustin P. Mattock					$herectx .= raw_line($linenr, $n) . "\n";
3694cf655043SAndy Whitcroft				}
3695cf655043SAndy Whitcroft
3696000d1cc1SJoe Perches				WARN("BRACES",
3697000d1cc1SJoe Perches				     "braces {} are not necessary for single statement blocks\n" . $herectx);
3698f0a594c1SAndy Whitcroft			}
3699f0a594c1SAndy Whitcroft		}
3700f0a594c1SAndy Whitcroft
37010979ae66SJoe Perches# check for unnecessary blank lines around braces
370277b9a53aSJoe Perches		if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
37030979ae66SJoe Perches			CHK("BRACES",
37040979ae66SJoe Perches			    "Blank lines aren't necessary before a close brace '}'\n" . $hereprev);
37050979ae66SJoe Perches		}
370677b9a53aSJoe Perches		if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
37070979ae66SJoe Perches			CHK("BRACES",
37080979ae66SJoe Perches			    "Blank lines aren't necessary after an open brace '{'\n" . $hereprev);
37090979ae66SJoe Perches		}
37100979ae66SJoe Perches
37114a0df2efSAndy Whitcroft# no volatiles please
37126c72ffaaSAndy Whitcroft		my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
37136c72ffaaSAndy Whitcroft		if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
3714000d1cc1SJoe Perches			WARN("VOLATILE",
3715000d1cc1SJoe Perches			     "Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
37164a0df2efSAndy Whitcroft		}
37174a0df2efSAndy Whitcroft
371800df344fSAndy Whitcroft# warn about #if 0
3719c45dcabdSAndy Whitcroft		if ($line =~ /^.\s*\#\s*if\s+0\b/) {
3720000d1cc1SJoe Perches			CHK("REDUNDANT_CODE",
3721000d1cc1SJoe Perches			    "if this code is redundant consider removing it\n" .
3722de7d4f0eSAndy Whitcroft				$herecurr);
37234a0df2efSAndy Whitcroft		}
37244a0df2efSAndy Whitcroft
372503df4b51SAndy Whitcroft# check for needless "if (<foo>) fn(<foo>)" uses
372603df4b51SAndy Whitcroft		if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
372703df4b51SAndy Whitcroft			my $expr = '\s*\(\s*' . quotemeta($1) . '\s*\)\s*;';
372803df4b51SAndy Whitcroft			if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?)$expr/) {
372903df4b51SAndy Whitcroft				WARN('NEEDLESS_IF',
373003df4b51SAndy Whitcroft				     "$1(NULL) is safe this check is probably not required\n" . $hereprev);
37314c432a8fSGreg Kroah-Hartman			}
37324c432a8fSGreg Kroah-Hartman		}
3733f0a594c1SAndy Whitcroft
37348716de38SJoe Perchessub string_find_replace {
37358716de38SJoe Perches	my ($string, $find, $replace) = @_;
37368716de38SJoe Perches
37378716de38SJoe Perches	$string =~ s/$find/$replace/g;
37388716de38SJoe Perches
37398716de38SJoe Perches	return $string;
37408716de38SJoe Perches}
37418716de38SJoe Perches
37428716de38SJoe Perches# check for bad placement of section $InitAttribute (e.g.: __initdata)
37438716de38SJoe Perches		if ($line =~ /(\b$InitAttribute\b)/) {
37448716de38SJoe Perches			my $attr = $1;
37458716de38SJoe Perches			if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
37468716de38SJoe Perches				my $ptr = $1;
37478716de38SJoe Perches				my $var = $2;
37488716de38SJoe Perches				if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
37498716de38SJoe Perches				      ERROR("MISPLACED_INIT",
37508716de38SJoe Perches					    "$attr should be placed after $var\n" . $herecurr)) ||
37518716de38SJoe Perches				     ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
37528716de38SJoe Perches				      WARN("MISPLACED_INIT",
37538716de38SJoe Perches					   "$attr should be placed after $var\n" . $herecurr))) &&
37548716de38SJoe Perches				    $fix) {
37558716de38SJoe Perches					$fixed[$linenr - 1] =~ s/(\bstatic\s+(?:const\s+)?)(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*([=;])\s*/"$1" . trim(string_find_replace($2, "\\s*$attr\\s*", " ")) . " " . trim(string_find_replace($3, "\\s*$attr\\s*", "")) . " $attr" . ("$4" eq ";" ? ";" : " = ")/e;
37568716de38SJoe Perches				}
37578716de38SJoe Perches			}
37588716de38SJoe Perches		}
37598716de38SJoe Perches
37601a15a250SPatrick Pannuto# prefer usleep_range over udelay
376137581c28SBruce Allan		if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
37621a15a250SPatrick Pannuto			# ignore udelay's < 10, however
376337581c28SBruce Allan			if (! ($1 < 10) ) {
3764000d1cc1SJoe Perches				CHK("USLEEP_RANGE",
3765000d1cc1SJoe Perches				    "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $line);
37661a15a250SPatrick Pannuto			}
37671a15a250SPatrick Pannuto		}
37681a15a250SPatrick Pannuto
376909ef8725SPatrick Pannuto# warn about unexpectedly long msleep's
377009ef8725SPatrick Pannuto		if ($line =~ /\bmsleep\s*\((\d+)\);/) {
377109ef8725SPatrick Pannuto			if ($1 < 20) {
3772000d1cc1SJoe Perches				WARN("MSLEEP",
3773000d1cc1SJoe Perches				     "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $line);
377409ef8725SPatrick Pannuto			}
377509ef8725SPatrick Pannuto		}
377609ef8725SPatrick Pannuto
377736ec1939SJoe Perches# check for comparisons of jiffies
377836ec1939SJoe Perches		if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
377936ec1939SJoe Perches			WARN("JIFFIES_COMPARISON",
378036ec1939SJoe Perches			     "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
378136ec1939SJoe Perches		}
378236ec1939SJoe Perches
37839d7a34a5SJoe Perches# check for comparisons of get_jiffies_64()
37849d7a34a5SJoe Perches		if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
37859d7a34a5SJoe Perches			WARN("JIFFIES_COMPARISON",
37869d7a34a5SJoe Perches			     "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
37879d7a34a5SJoe Perches		}
37889d7a34a5SJoe Perches
378900df344fSAndy Whitcroft# warn about #ifdefs in C files
3790c45dcabdSAndy Whitcroft#		if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
379100df344fSAndy Whitcroft#			print "#ifdef in C files should be avoided\n";
379200df344fSAndy Whitcroft#			print "$herecurr";
379300df344fSAndy Whitcroft#			$clean = 0;
379400df344fSAndy Whitcroft#		}
379500df344fSAndy Whitcroft
379622f2a2efSAndy Whitcroft# warn about spacing in #ifdefs
3797c45dcabdSAndy Whitcroft		if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
37983705ce5bSJoe Perches			if (ERROR("SPACING",
37993705ce5bSJoe Perches				  "exactly one space required after that #$1\n" . $herecurr) &&
38003705ce5bSJoe Perches			    $fix) {
38013705ce5bSJoe Perches				$fixed[$linenr - 1] =~
38023705ce5bSJoe Perches				    s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
38033705ce5bSJoe Perches			}
38043705ce5bSJoe Perches
380522f2a2efSAndy Whitcroft		}
380622f2a2efSAndy Whitcroft
38074a0df2efSAndy Whitcroft# check for spinlock_t definitions without a comment.
3808171ae1a4SAndy Whitcroft		if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
3809171ae1a4SAndy Whitcroft		    $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
38104a0df2efSAndy Whitcroft			my $which = $1;
38114a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
3812000d1cc1SJoe Perches				CHK("UNCOMMENTED_DEFINITION",
3813000d1cc1SJoe Perches				    "$1 definition without comment\n" . $herecurr);
38144a0df2efSAndy Whitcroft			}
38154a0df2efSAndy Whitcroft		}
38164a0df2efSAndy Whitcroft# check for memory barriers without a comment.
38174a0df2efSAndy Whitcroft		if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
38184a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
3819000d1cc1SJoe Perches				CHK("MEMORY_BARRIER",
3820000d1cc1SJoe Perches				    "memory barrier without comment\n" . $herecurr);
38214a0df2efSAndy Whitcroft			}
38224a0df2efSAndy Whitcroft		}
38234a0df2efSAndy Whitcroft# check of hardware specific defines
3824c45dcabdSAndy Whitcroft		if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
3825000d1cc1SJoe Perches			CHK("ARCH_DEFINES",
3826000d1cc1SJoe Perches			    "architecture specific defines should be avoided\n" .  $herecurr);
38270a920b5bSAndy Whitcroft		}
3828653d4876SAndy Whitcroft
3829d4977c78STobias Klauser# Check that the storage class is at the beginning of a declaration
3830d4977c78STobias Klauser		if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
3831000d1cc1SJoe Perches			WARN("STORAGE_CLASS",
3832000d1cc1SJoe Perches			     "storage class should be at the beginning of the declaration\n" . $herecurr)
3833d4977c78STobias Klauser		}
3834d4977c78STobias Klauser
3835de7d4f0eSAndy Whitcroft# check the location of the inline attribute, that it is between
3836de7d4f0eSAndy Whitcroft# storage class and type.
38379c0ca6f9SAndy Whitcroft		if ($line =~ /\b$Type\s+$Inline\b/ ||
38389c0ca6f9SAndy Whitcroft		    $line =~ /\b$Inline\s+$Storage\b/) {
3839000d1cc1SJoe Perches			ERROR("INLINE_LOCATION",
3840000d1cc1SJoe Perches			      "inline keyword should sit between storage class and type\n" . $herecurr);
3841de7d4f0eSAndy Whitcroft		}
3842de7d4f0eSAndy Whitcroft
38438905a67cSAndy Whitcroft# Check for __inline__ and __inline, prefer inline
38448905a67cSAndy Whitcroft		if ($line =~ /\b(__inline__|__inline)\b/) {
3845d5e616fcSJoe Perches			if (WARN("INLINE",
3846d5e616fcSJoe Perches				 "plain inline is preferred over $1\n" . $herecurr) &&
3847d5e616fcSJoe Perches			    $fix) {
3848d5e616fcSJoe Perches				$fixed[$linenr - 1] =~ s/\b(__inline__|__inline)\b/inline/;
3849d5e616fcSJoe Perches
3850d5e616fcSJoe Perches			}
38518905a67cSAndy Whitcroft		}
38528905a67cSAndy Whitcroft
38533d130fd0SJoe Perches# Check for __attribute__ packed, prefer __packed
38543d130fd0SJoe Perches		if ($line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
3855000d1cc1SJoe Perches			WARN("PREFER_PACKED",
3856000d1cc1SJoe Perches			     "__packed is preferred over __attribute__((packed))\n" . $herecurr);
38573d130fd0SJoe Perches		}
38583d130fd0SJoe Perches
385939b7e287SJoe Perches# Check for __attribute__ aligned, prefer __aligned
386039b7e287SJoe Perches		if ($line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
3861000d1cc1SJoe Perches			WARN("PREFER_ALIGNED",
3862000d1cc1SJoe Perches			     "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
386339b7e287SJoe Perches		}
386439b7e287SJoe Perches
38655f14d3bdSJoe Perches# Check for __attribute__ format(printf, prefer __printf
38665f14d3bdSJoe Perches		if ($line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
3867d5e616fcSJoe Perches			if (WARN("PREFER_PRINTF",
3868d5e616fcSJoe Perches				 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) &&
3869d5e616fcSJoe Perches			    $fix) {
3870d5e616fcSJoe Perches				$fixed[$linenr - 1] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf\s*,\s*(.*)\)\s*\)\s*\)/"__printf(" . trim($1) . ")"/ex;
3871d5e616fcSJoe Perches
3872d5e616fcSJoe Perches			}
38735f14d3bdSJoe Perches		}
38745f14d3bdSJoe Perches
38756061d949SJoe Perches# Check for __attribute__ format(scanf, prefer __scanf
38766061d949SJoe Perches		if ($line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
3877d5e616fcSJoe Perches			if (WARN("PREFER_SCANF",
3878d5e616fcSJoe Perches				 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) &&
3879d5e616fcSJoe Perches			    $fix) {
3880d5e616fcSJoe Perches				$fixed[$linenr - 1] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\s*,\s*(.*)\)\s*\)\s*\)/"__scanf(" . trim($1) . ")"/ex;
3881d5e616fcSJoe Perches			}
38826061d949SJoe Perches		}
38836061d949SJoe Perches
38848f53a9b8SJoe Perches# check for sizeof(&)
38858f53a9b8SJoe Perches		if ($line =~ /\bsizeof\s*\(\s*\&/) {
3886000d1cc1SJoe Perches			WARN("SIZEOF_ADDRESS",
3887000d1cc1SJoe Perches			     "sizeof(& should be avoided\n" . $herecurr);
38888f53a9b8SJoe Perches		}
38898f53a9b8SJoe Perches
389066c80b60SJoe Perches# check for sizeof without parenthesis
389166c80b60SJoe Perches		if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
3892d5e616fcSJoe Perches			if (WARN("SIZEOF_PARENTHESIS",
3893d5e616fcSJoe Perches				 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
3894d5e616fcSJoe Perches			    $fix) {
3895d5e616fcSJoe Perches				$fixed[$linenr - 1] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
3896d5e616fcSJoe Perches			}
389766c80b60SJoe Perches		}
389866c80b60SJoe Perches
3899428e2fdcSJoe Perches# check for line continuations in quoted strings with odd counts of "
3900428e2fdcSJoe Perches		if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) {
3901000d1cc1SJoe Perches			WARN("LINE_CONTINUATIONS",
3902000d1cc1SJoe Perches			     "Avoid line continuations in quoted strings\n" . $herecurr);
3903428e2fdcSJoe Perches		}
3904428e2fdcSJoe Perches
390588982feaSJoe Perches# check for struct spinlock declarations
390688982feaSJoe Perches		if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
390788982feaSJoe Perches			WARN("USE_SPINLOCK_T",
390888982feaSJoe Perches			     "struct spinlock should be spinlock_t\n" . $herecurr);
390988982feaSJoe Perches		}
391088982feaSJoe Perches
3911a6962d72SJoe Perches# check for seq_printf uses that could be seq_puts
3912a6962d72SJoe Perches		if ($line =~ /\bseq_printf\s*\(/) {
3913a6962d72SJoe Perches			my $fmt = get_quoted_string($line, $rawline);
3914a6962d72SJoe Perches			if ($fmt !~ /[^\\]\%/) {
3915d5e616fcSJoe Perches				if (WARN("PREFER_SEQ_PUTS",
3916d5e616fcSJoe Perches					 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
3917d5e616fcSJoe Perches				    $fix) {
3918d5e616fcSJoe Perches					$fixed[$linenr - 1] =~ s/\bseq_printf\b/seq_puts/;
3919d5e616fcSJoe Perches				}
3920a6962d72SJoe Perches			}
3921a6962d72SJoe Perches		}
3922a6962d72SJoe Perches
3923554e165cSAndy Whitcroft# Check for misused memsets
3924d1fe9c09SJoe Perches		if ($^V && $^V ge 5.10.0 &&
3925d1fe9c09SJoe Perches		    defined $stat &&
3926d7c76ba7SJoe Perches		    $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/s) {
3927554e165cSAndy Whitcroft
3928d7c76ba7SJoe Perches			my $ms_addr = $2;
3929d1fe9c09SJoe Perches			my $ms_val = $7;
3930d1fe9c09SJoe Perches			my $ms_size = $12;
3931d7c76ba7SJoe Perches
3932554e165cSAndy Whitcroft			if ($ms_size =~ /^(0x|)0$/i) {
3933554e165cSAndy Whitcroft				ERROR("MEMSET",
3934d7c76ba7SJoe Perches				      "memset to 0's uses 0 as the 2nd argument, not the 3rd\n" . "$here\n$stat\n");
3935554e165cSAndy Whitcroft			} elsif ($ms_size =~ /^(0x|)1$/i) {
3936554e165cSAndy Whitcroft				WARN("MEMSET",
3937d7c76ba7SJoe Perches				     "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
3938d7c76ba7SJoe Perches			}
3939d7c76ba7SJoe Perches		}
3940d7c76ba7SJoe Perches
3941d7c76ba7SJoe Perches# typecasts on min/max could be min_t/max_t
3942d1fe9c09SJoe Perches		if ($^V && $^V ge 5.10.0 &&
3943d1fe9c09SJoe Perches		    defined $stat &&
3944d7c76ba7SJoe Perches		    $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
3945d1fe9c09SJoe Perches			if (defined $2 || defined $7) {
3946d7c76ba7SJoe Perches				my $call = $1;
3947d7c76ba7SJoe Perches				my $cast1 = deparenthesize($2);
3948d7c76ba7SJoe Perches				my $arg1 = $3;
3949d1fe9c09SJoe Perches				my $cast2 = deparenthesize($7);
3950d1fe9c09SJoe Perches				my $arg2 = $8;
3951d7c76ba7SJoe Perches				my $cast;
3952d7c76ba7SJoe Perches
3953d1fe9c09SJoe Perches				if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
3954d7c76ba7SJoe Perches					$cast = "$cast1 or $cast2";
3955d7c76ba7SJoe Perches				} elsif ($cast1 ne "") {
3956d7c76ba7SJoe Perches					$cast = $cast1;
3957d7c76ba7SJoe Perches				} else {
3958d7c76ba7SJoe Perches					$cast = $cast2;
3959d7c76ba7SJoe Perches				}
3960d7c76ba7SJoe Perches				WARN("MINMAX",
3961d7c76ba7SJoe Perches				     "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
3962554e165cSAndy Whitcroft			}
3963554e165cSAndy Whitcroft		}
3964554e165cSAndy Whitcroft
39654a273195SJoe Perches# check usleep_range arguments
39664a273195SJoe Perches		if ($^V && $^V ge 5.10.0 &&
39674a273195SJoe Perches		    defined $stat &&
39684a273195SJoe Perches		    $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
39694a273195SJoe Perches			my $min = $1;
39704a273195SJoe Perches			my $max = $7;
39714a273195SJoe Perches			if ($min eq $max) {
39724a273195SJoe Perches				WARN("USLEEP_RANGE",
39734a273195SJoe Perches				     "usleep_range should not use min == max args; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
39744a273195SJoe Perches			} elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
39754a273195SJoe Perches				 $min > $max) {
39764a273195SJoe Perches				WARN("USLEEP_RANGE",
39774a273195SJoe Perches				     "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
39784a273195SJoe Perches			}
39794a273195SJoe Perches		}
39804a273195SJoe Perches
398170dc8a48SJoe Perches# check for new externs in .h files.
398270dc8a48SJoe Perches		if ($realfile =~ /\.h$/ &&
398370dc8a48SJoe Perches		    $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
3984d1d85780SJoe Perches			if (CHK("AVOID_EXTERNS",
398570dc8a48SJoe Perches				"extern prototypes should be avoided in .h files\n" . $herecurr) &&
398670dc8a48SJoe Perches			    $fix) {
398770dc8a48SJoe Perches				$fixed[$linenr - 1] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
398870dc8a48SJoe Perches			}
398970dc8a48SJoe Perches		}
399070dc8a48SJoe Perches
3991de7d4f0eSAndy Whitcroft# check for new externs in .c files.
3992171ae1a4SAndy Whitcroft		if ($realfile =~ /\.c$/ && defined $stat &&
3993c45dcabdSAndy Whitcroft		    $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
3994171ae1a4SAndy Whitcroft		{
3995c45dcabdSAndy Whitcroft			my $function_name = $1;
3996c45dcabdSAndy Whitcroft			my $paren_space = $2;
3997171ae1a4SAndy Whitcroft
3998171ae1a4SAndy Whitcroft			my $s = $stat;
3999171ae1a4SAndy Whitcroft			if (defined $cond) {
4000171ae1a4SAndy Whitcroft				substr($s, 0, length($cond), '');
4001171ae1a4SAndy Whitcroft			}
4002c45dcabdSAndy Whitcroft			if ($s =~ /^\s*;/ &&
4003c45dcabdSAndy Whitcroft			    $function_name ne 'uninitialized_var')
4004c45dcabdSAndy Whitcroft			{
4005000d1cc1SJoe Perches				WARN("AVOID_EXTERNS",
4006000d1cc1SJoe Perches				     "externs should be avoided in .c files\n" .  $herecurr);
4007de7d4f0eSAndy Whitcroft			}
4008de7d4f0eSAndy Whitcroft
4009171ae1a4SAndy Whitcroft			if ($paren_space =~ /\n/) {
4010000d1cc1SJoe Perches				WARN("FUNCTION_ARGUMENTS",
4011000d1cc1SJoe Perches				     "arguments for function declarations should follow identifier\n" . $herecurr);
4012171ae1a4SAndy Whitcroft			}
40139c9ba34eSAndy Whitcroft
40149c9ba34eSAndy Whitcroft		} elsif ($realfile =~ /\.c$/ && defined $stat &&
40159c9ba34eSAndy Whitcroft		    $stat =~ /^.\s*extern\s+/)
40169c9ba34eSAndy Whitcroft		{
4017000d1cc1SJoe Perches			WARN("AVOID_EXTERNS",
4018000d1cc1SJoe Perches			     "externs should be avoided in .c files\n" .  $herecurr);
4019171ae1a4SAndy Whitcroft		}
4020171ae1a4SAndy Whitcroft
4021de7d4f0eSAndy Whitcroft# checks for new __setup's
4022de7d4f0eSAndy Whitcroft		if ($rawline =~ /\b__setup\("([^"]*)"/) {
4023de7d4f0eSAndy Whitcroft			my $name = $1;
4024de7d4f0eSAndy Whitcroft
4025de7d4f0eSAndy Whitcroft			if (!grep(/$name/, @setup_docs)) {
4026000d1cc1SJoe Perches				CHK("UNDOCUMENTED_SETUP",
4027000d1cc1SJoe Perches				    "__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
4028de7d4f0eSAndy Whitcroft			}
4029653d4876SAndy Whitcroft		}
40309c0ca6f9SAndy Whitcroft
40319c0ca6f9SAndy Whitcroft# check for pointless casting of kmalloc return
4032caf2a54fSJoe Perches		if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) {
4033000d1cc1SJoe Perches			WARN("UNNECESSARY_CASTS",
4034000d1cc1SJoe Perches			     "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
40359c0ca6f9SAndy Whitcroft		}
403613214adfSAndy Whitcroft
4037a640d25cSJoe Perches# alloc style
4038a640d25cSJoe Perches# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
4039a640d25cSJoe Perches		if ($^V && $^V ge 5.10.0 &&
4040a640d25cSJoe Perches		    $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*([kv][mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
4041a640d25cSJoe Perches			CHK("ALLOC_SIZEOF_STRUCT",
4042a640d25cSJoe Perches			    "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
4043a640d25cSJoe Perches		}
4044a640d25cSJoe Perches
4045972fdea2SJoe Perches# check for krealloc arg reuse
4046972fdea2SJoe Perches		if ($^V && $^V ge 5.10.0 &&
4047972fdea2SJoe Perches		    $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*\1\s*,/) {
4048972fdea2SJoe Perches			WARN("KREALLOC_ARG_REUSE",
4049972fdea2SJoe Perches			     "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
4050972fdea2SJoe Perches		}
4051972fdea2SJoe Perches
40525ce59ae0SJoe Perches# check for alloc argument mismatch
40535ce59ae0SJoe Perches		if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
40545ce59ae0SJoe Perches			WARN("ALLOC_ARRAY_ARGS",
40555ce59ae0SJoe Perches			     "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
40565ce59ae0SJoe Perches		}
40575ce59ae0SJoe Perches
4058caf2a54fSJoe Perches# check for multiple semicolons
4059caf2a54fSJoe Perches		if ($line =~ /;\s*;\s*$/) {
4060d5e616fcSJoe Perches			if (WARN("ONE_SEMICOLON",
4061d5e616fcSJoe Perches				 "Statements terminations use 1 semicolon\n" . $herecurr) &&
4062d5e616fcSJoe Perches			    $fix) {
4063d5e616fcSJoe Perches				$fixed[$linenr - 1] =~ s/(\s*;\s*){2,}$/;/g;
4064d5e616fcSJoe Perches			}
4065d1e2ad07SJoe Perches		}
4066d1e2ad07SJoe Perches
4067d1e2ad07SJoe Perches# check for switch/default statements without a break;
4068d1e2ad07SJoe Perches		if ($^V && $^V ge 5.10.0 &&
4069d1e2ad07SJoe Perches		    defined $stat &&
4070d1e2ad07SJoe Perches		    $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
4071d1e2ad07SJoe Perches			my $ctx = '';
4072d1e2ad07SJoe Perches			my $herectx = $here . "\n";
4073d1e2ad07SJoe Perches			my $cnt = statement_rawlines($stat);
4074d1e2ad07SJoe Perches			for (my $n = 0; $n < $cnt; $n++) {
4075d1e2ad07SJoe Perches				$herectx .= raw_line($linenr, $n) . "\n";
4076d1e2ad07SJoe Perches			}
4077d1e2ad07SJoe Perches			WARN("DEFAULT_NO_BREAK",
4078d1e2ad07SJoe Perches			     "switch default: should use break\n" . $herectx);
4079caf2a54fSJoe Perches		}
4080caf2a54fSJoe Perches
408113214adfSAndy Whitcroft# check for gcc specific __FUNCTION__
4082d5e616fcSJoe Perches		if ($line =~ /\b__FUNCTION__\b/) {
4083d5e616fcSJoe Perches			if (WARN("USE_FUNC",
4084d5e616fcSJoe Perches				 "__func__ should be used instead of gcc specific __FUNCTION__\n"  . $herecurr) &&
4085d5e616fcSJoe Perches			    $fix) {
4086d5e616fcSJoe Perches				$fixed[$linenr - 1] =~ s/\b__FUNCTION__\b/__func__/g;
4087d5e616fcSJoe Perches			}
408813214adfSAndy Whitcroft		}
4089773647a0SAndy Whitcroft
40902c92488aSJoe Perches# check for use of yield()
40912c92488aSJoe Perches		if ($line =~ /\byield\s*\(\s*\)/) {
40922c92488aSJoe Perches			WARN("YIELD",
40932c92488aSJoe Perches			     "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n"  . $herecurr);
40942c92488aSJoe Perches		}
40952c92488aSJoe Perches
4096179f8f40SJoe Perches# check for comparisons against true and false
4097179f8f40SJoe Perches		if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
4098179f8f40SJoe Perches			my $lead = $1;
4099179f8f40SJoe Perches			my $arg = $2;
4100179f8f40SJoe Perches			my $test = $3;
4101179f8f40SJoe Perches			my $otype = $4;
4102179f8f40SJoe Perches			my $trail = $5;
4103179f8f40SJoe Perches			my $op = "!";
4104179f8f40SJoe Perches
4105179f8f40SJoe Perches			($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
4106179f8f40SJoe Perches
4107179f8f40SJoe Perches			my $type = lc($otype);
4108179f8f40SJoe Perches			if ($type =~ /^(?:true|false)$/) {
4109179f8f40SJoe Perches				if (("$test" eq "==" && "$type" eq "true") ||
4110179f8f40SJoe Perches				    ("$test" eq "!=" && "$type" eq "false")) {
4111179f8f40SJoe Perches					$op = "";
4112179f8f40SJoe Perches				}
4113179f8f40SJoe Perches
4114179f8f40SJoe Perches				CHK("BOOL_COMPARISON",
4115179f8f40SJoe Perches				    "Using comparison to $otype is error prone\n" . $herecurr);
4116179f8f40SJoe Perches
4117179f8f40SJoe Perches## maybe suggesting a correct construct would better
4118179f8f40SJoe Perches##				    "Using comparison to $otype is error prone.  Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
4119179f8f40SJoe Perches
4120179f8f40SJoe Perches			}
4121179f8f40SJoe Perches		}
4122179f8f40SJoe Perches
41234882720bSThomas Gleixner# check for semaphores initialized locked
41244882720bSThomas Gleixner		if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
4125000d1cc1SJoe Perches			WARN("CONSIDER_COMPLETION",
4126000d1cc1SJoe Perches			     "consider using a completion\n" . $herecurr);
4127773647a0SAndy Whitcroft		}
41286712d858SJoe Perches
412967d0a075SJoe Perches# recommend kstrto* over simple_strto* and strict_strto*
413067d0a075SJoe Perches		if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
4131000d1cc1SJoe Perches			WARN("CONSIDER_KSTRTO",
413267d0a075SJoe Perches			     "$1 is obsolete, use k$3 instead\n" . $herecurr);
4133773647a0SAndy Whitcroft		}
41346712d858SJoe Perches
4135f3db6639SMichael Ellerman# check for __initcall(), use device_initcall() explicitly please
4136f3db6639SMichael Ellerman		if ($line =~ /^.\s*__initcall\s*\(/) {
4137000d1cc1SJoe Perches			WARN("USE_DEVICE_INITCALL",
4138000d1cc1SJoe Perches			     "please use device_initcall() instead of __initcall()\n" . $herecurr);
4139f3db6639SMichael Ellerman		}
41406712d858SJoe Perches
414179404849SEmese Revfy# check for various ops structs, ensure they are const.
414279404849SEmese Revfy		my $struct_ops = qr{acpi_dock_ops|
414379404849SEmese Revfy				address_space_operations|
414479404849SEmese Revfy				backlight_ops|
414579404849SEmese Revfy				block_device_operations|
414679404849SEmese Revfy				dentry_operations|
414779404849SEmese Revfy				dev_pm_ops|
414879404849SEmese Revfy				dma_map_ops|
414979404849SEmese Revfy				extent_io_ops|
415079404849SEmese Revfy				file_lock_operations|
415179404849SEmese Revfy				file_operations|
415279404849SEmese Revfy				hv_ops|
415379404849SEmese Revfy				ide_dma_ops|
415479404849SEmese Revfy				intel_dvo_dev_ops|
415579404849SEmese Revfy				item_operations|
415679404849SEmese Revfy				iwl_ops|
415779404849SEmese Revfy				kgdb_arch|
415879404849SEmese Revfy				kgdb_io|
415979404849SEmese Revfy				kset_uevent_ops|
416079404849SEmese Revfy				lock_manager_operations|
416179404849SEmese Revfy				microcode_ops|
416279404849SEmese Revfy				mtrr_ops|
416379404849SEmese Revfy				neigh_ops|
416479404849SEmese Revfy				nlmsvc_binding|
416579404849SEmese Revfy				pci_raw_ops|
416679404849SEmese Revfy				pipe_buf_operations|
416779404849SEmese Revfy				platform_hibernation_ops|
416879404849SEmese Revfy				platform_suspend_ops|
416979404849SEmese Revfy				proto_ops|
417079404849SEmese Revfy				rpc_pipe_ops|
417179404849SEmese Revfy				seq_operations|
417279404849SEmese Revfy				snd_ac97_build_ops|
417379404849SEmese Revfy				soc_pcmcia_socket_ops|
417479404849SEmese Revfy				stacktrace_ops|
417579404849SEmese Revfy				sysfs_ops|
417679404849SEmese Revfy				tty_operations|
417779404849SEmese Revfy				usb_mon_operations|
417879404849SEmese Revfy				wd_ops}x;
41796903ffb2SAndy Whitcroft		if ($line !~ /\bconst\b/ &&
418079404849SEmese Revfy		    $line =~ /\bstruct\s+($struct_ops)\b/) {
4181000d1cc1SJoe Perches			WARN("CONST_STRUCT",
4182000d1cc1SJoe Perches			     "struct $1 should normally be const\n" .
41836903ffb2SAndy Whitcroft				$herecurr);
41842b6db5cbSAndy Whitcroft		}
4185773647a0SAndy Whitcroft
4186773647a0SAndy Whitcroft# use of NR_CPUS is usually wrong
4187773647a0SAndy Whitcroft# ignore definitions of NR_CPUS and usage to define arrays as likely right
4188773647a0SAndy Whitcroft		if ($line =~ /\bNR_CPUS\b/ &&
4189c45dcabdSAndy Whitcroft		    $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
4190c45dcabdSAndy Whitcroft		    $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
4191171ae1a4SAndy Whitcroft		    $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
4192171ae1a4SAndy Whitcroft		    $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
4193171ae1a4SAndy Whitcroft		    $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
4194773647a0SAndy Whitcroft		{
4195000d1cc1SJoe Perches			WARN("NR_CPUS",
4196000d1cc1SJoe Perches			     "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
4197773647a0SAndy Whitcroft		}
41989c9ba34eSAndy Whitcroft
41999c9ba34eSAndy Whitcroft# check for %L{u,d,i} in strings
42009c9ba34eSAndy Whitcroft		my $string;
42019c9ba34eSAndy Whitcroft		while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
42029c9ba34eSAndy Whitcroft			$string = substr($rawline, $-[1], $+[1] - $-[1]);
42032a1bc5d5SAndy Whitcroft			$string =~ s/%%/__/g;
42049c9ba34eSAndy Whitcroft			if ($string =~ /(?<!%)%L[udi]/) {
4205000d1cc1SJoe Perches				WARN("PRINTF_L",
4206000d1cc1SJoe Perches				     "\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
42079c9ba34eSAndy Whitcroft				last;
42089c9ba34eSAndy Whitcroft			}
42099c9ba34eSAndy Whitcroft		}
4210691d77b6SAndy Whitcroft
4211691d77b6SAndy Whitcroft# whine mightly about in_atomic
4212691d77b6SAndy Whitcroft		if ($line =~ /\bin_atomic\s*\(/) {
4213691d77b6SAndy Whitcroft			if ($realfile =~ m@^drivers/@) {
4214000d1cc1SJoe Perches				ERROR("IN_ATOMIC",
4215000d1cc1SJoe Perches				      "do not use in_atomic in drivers\n" . $herecurr);
4216f4a87736SAndy Whitcroft			} elsif ($realfile !~ m@^kernel/@) {
4217000d1cc1SJoe Perches				WARN("IN_ATOMIC",
4218000d1cc1SJoe Perches				     "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
4219691d77b6SAndy Whitcroft			}
4220691d77b6SAndy Whitcroft		}
42211704f47bSPeter Zijlstra
42221704f47bSPeter Zijlstra# check for lockdep_set_novalidate_class
42231704f47bSPeter Zijlstra		if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
42241704f47bSPeter Zijlstra		    $line =~ /__lockdep_no_validate__\s*\)/ ) {
42251704f47bSPeter Zijlstra			if ($realfile !~ m@^kernel/lockdep@ &&
42261704f47bSPeter Zijlstra			    $realfile !~ m@^include/linux/lockdep@ &&
42271704f47bSPeter Zijlstra			    $realfile !~ m@^drivers/base/core@) {
4228000d1cc1SJoe Perches				ERROR("LOCKDEP",
4229000d1cc1SJoe Perches				      "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
42301704f47bSPeter Zijlstra			}
42311704f47bSPeter Zijlstra		}
423288f8831cSDave Jones
423388f8831cSDave Jones		if ($line =~ /debugfs_create_file.*S_IWUGO/ ||
423488f8831cSDave Jones		    $line =~ /DEVICE_ATTR.*S_IWUGO/ ) {
4235000d1cc1SJoe Perches			WARN("EXPORTED_WORLD_WRITABLE",
4236000d1cc1SJoe Perches			     "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
423788f8831cSDave Jones		}
423813214adfSAndy Whitcroft	}
423913214adfSAndy Whitcroft
424013214adfSAndy Whitcroft	# If we have no input at all, then there is nothing to report on
424113214adfSAndy Whitcroft	# so just keep quiet.
424213214adfSAndy Whitcroft	if ($#rawlines == -1) {
424313214adfSAndy Whitcroft		exit(0);
42440a920b5bSAndy Whitcroft	}
42450a920b5bSAndy Whitcroft
42468905a67cSAndy Whitcroft	# In mailback mode only produce a report in the negative, for
42478905a67cSAndy Whitcroft	# things that appear to be patches.
42488905a67cSAndy Whitcroft	if ($mailback && ($clean == 1 || !$is_patch)) {
42498905a67cSAndy Whitcroft		exit(0);
42508905a67cSAndy Whitcroft	}
42518905a67cSAndy Whitcroft
42528905a67cSAndy Whitcroft	# This is not a patch, and we are are in 'no-patch' mode so
42538905a67cSAndy Whitcroft	# just keep quiet.
42548905a67cSAndy Whitcroft	if (!$chk_patch && !$is_patch) {
42558905a67cSAndy Whitcroft		exit(0);
42568905a67cSAndy Whitcroft	}
42578905a67cSAndy Whitcroft
42588905a67cSAndy Whitcroft	if (!$is_patch) {
4259000d1cc1SJoe Perches		ERROR("NOT_UNIFIED_DIFF",
4260000d1cc1SJoe Perches		      "Does not appear to be a unified-diff format patch\n");
42610a920b5bSAndy Whitcroft	}
42620a920b5bSAndy Whitcroft	if ($is_patch && $chk_signoff && $signoff == 0) {
4263000d1cc1SJoe Perches		ERROR("MISSING_SIGN_OFF",
4264000d1cc1SJoe Perches		      "Missing Signed-off-by: line(s)\n");
42650a920b5bSAndy Whitcroft	}
42660a920b5bSAndy Whitcroft
4267f0a594c1SAndy Whitcroft	print report_dump();
426813214adfSAndy Whitcroft	if ($summary && !($clean == 1 && $quiet == 1)) {
426913214adfSAndy Whitcroft		print "$filename " if ($summary_file);
42706c72ffaaSAndy Whitcroft		print "total: $cnt_error errors, $cnt_warn warnings, " .
42716c72ffaaSAndy Whitcroft			(($check)? "$cnt_chk checks, " : "") .
42726c72ffaaSAndy Whitcroft			"$cnt_lines lines checked\n";
42738905a67cSAndy Whitcroft		print "\n" if ($quiet == 0);
42746c72ffaaSAndy Whitcroft	}
42758905a67cSAndy Whitcroft
4276d2c0a235SAndy Whitcroft	if ($quiet == 0) {
4277d1fe9c09SJoe Perches
4278d1fe9c09SJoe Perches		if ($^V lt 5.10.0) {
4279d1fe9c09SJoe Perches			print("NOTE: perl $^V is not modern enough to detect all possible issues.\n");
4280d1fe9c09SJoe Perches			print("An upgrade to at least perl v5.10.0 is suggested.\n\n");
4281d1fe9c09SJoe Perches		}
4282d1fe9c09SJoe Perches
4283d2c0a235SAndy Whitcroft		# If there were whitespace errors which cleanpatch can fix
4284d2c0a235SAndy Whitcroft		# then suggest that.
4285d2c0a235SAndy Whitcroft		if ($rpt_cleaners) {
4286d2c0a235SAndy Whitcroft			print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n";
4287d2c0a235SAndy Whitcroft			print "      scripts/cleanfile\n\n";
4288b0781216SMike Frysinger			$rpt_cleaners = 0;
4289d2c0a235SAndy Whitcroft		}
4290d2c0a235SAndy Whitcroft	}
4291d2c0a235SAndy Whitcroft
429291bfe484SJoe Perches	hash_show_words(\%use_type, "Used");
429391bfe484SJoe Perches	hash_show_words(\%ignore_type, "Ignored");
4294000d1cc1SJoe Perches
42953705ce5bSJoe Perches	if ($clean == 0 && $fix && "@rawlines" ne "@fixed") {
42963705ce5bSJoe Perches		my $newfile = $filename . ".EXPERIMENTAL-checkpatch-fixes";
42973705ce5bSJoe Perches		my $linecount = 0;
42983705ce5bSJoe Perches		my $f;
42993705ce5bSJoe Perches
43003705ce5bSJoe Perches		open($f, '>', $newfile)
43013705ce5bSJoe Perches		    or die "$P: Can't open $newfile for write\n";
43023705ce5bSJoe Perches		foreach my $fixed_line (@fixed) {
43033705ce5bSJoe Perches			$linecount++;
43043705ce5bSJoe Perches			if ($file) {
43053705ce5bSJoe Perches				if ($linecount > 3) {
43063705ce5bSJoe Perches					$fixed_line =~ s/^\+//;
43073705ce5bSJoe Perches					print $f $fixed_line. "\n";
43083705ce5bSJoe Perches				}
43093705ce5bSJoe Perches			} else {
43103705ce5bSJoe Perches				print $f $fixed_line . "\n";
43113705ce5bSJoe Perches			}
43123705ce5bSJoe Perches		}
43133705ce5bSJoe Perches		close($f);
43143705ce5bSJoe Perches
43153705ce5bSJoe Perches		if (!$quiet) {
43163705ce5bSJoe Perches			print << "EOM";
43173705ce5bSJoe PerchesWrote EXPERIMENTAL --fix correction(s) to '$newfile'
43183705ce5bSJoe Perches
43193705ce5bSJoe PerchesDo _NOT_ trust the results written to this file.
43203705ce5bSJoe PerchesDo _NOT_ submit these changes without inspecting them for correctness.
43213705ce5bSJoe Perches
43223705ce5bSJoe PerchesThis EXPERIMENTAL file is simply a convenience to help rewrite patches.
43233705ce5bSJoe PerchesNo warranties, expressed or implied...
43243705ce5bSJoe Perches
43253705ce5bSJoe PerchesEOM
43263705ce5bSJoe Perches		}
43273705ce5bSJoe Perches	}
43283705ce5bSJoe Perches
43290a920b5bSAndy Whitcroft	if ($clean == 1 && $quiet == 0) {
4330c2fdda0dSAndy Whitcroft		print "$vname has no obvious style problems and is ready for submission.\n"
43310a920b5bSAndy Whitcroft	}
43320a920b5bSAndy Whitcroft	if ($clean == 0 && $quiet == 0) {
4333000d1cc1SJoe Perches		print << "EOM";
4334000d1cc1SJoe Perches$vname has style problems, please review.
4335000d1cc1SJoe Perches
4336000d1cc1SJoe PerchesIf any of these errors are false positives, please report
4337000d1cc1SJoe Perchesthem to the maintainer, see CHECKPATCH in MAINTAINERS.
4338000d1cc1SJoe PerchesEOM
43390a920b5bSAndy Whitcroft	}
434013214adfSAndy Whitcroft
43410a920b5bSAndy Whitcroft	return $clean;
43420a920b5bSAndy Whitcroft}
4343