xref: /linux-6.15/scripts/checkpatch.pl (revision 91cb5195)
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;
329624b8d6SJoe Perchesmy $fix_inplace = 0;
336c72ffaaSAndy Whitcroftmy $root;
34c2fdda0dSAndy Whitcroftmy %debug;
353445686aSJoe Perchesmy %camelcase = ();
3691bfe484SJoe Perchesmy %use_type = ();
3791bfe484SJoe Perchesmy @use = ();
3891bfe484SJoe Perchesmy %ignore_type = ();
39000d1cc1SJoe Perchesmy @ignore = ();
4077f5b10aSHannes Edermy $help = 0;
41000d1cc1SJoe Perchesmy $configuration_file = ".checkpatch.conf";
426cd7f386SJoe Perchesmy $max_line_length = 80;
43d62a201fSDave Hansenmy $ignore_perl_version = 0;
44d62a201fSDave Hansenmy $minimum_perl_version = 5.10.0;
4577f5b10aSHannes Eder
4677f5b10aSHannes Edersub help {
4777f5b10aSHannes Eder	my ($exitcode) = @_;
4877f5b10aSHannes Eder
4977f5b10aSHannes Eder	print << "EOM";
5077f5b10aSHannes EderUsage: $P [OPTION]... [FILE]...
5177f5b10aSHannes EderVersion: $V
5277f5b10aSHannes Eder
5377f5b10aSHannes EderOptions:
5477f5b10aSHannes Eder  -q, --quiet                quiet
5577f5b10aSHannes Eder  --no-tree                  run without a kernel tree
5677f5b10aSHannes Eder  --no-signoff               do not check for 'Signed-off-by' line
5777f5b10aSHannes Eder  --patch                    treat FILE as patchfile (default)
5877f5b10aSHannes Eder  --emacs                    emacs compile window format
5977f5b10aSHannes Eder  --terse                    one line per report
6077f5b10aSHannes Eder  -f, --file                 treat FILE as regular source file
6177f5b10aSHannes Eder  --subjective, --strict     enable more subjective tests
6291bfe484SJoe Perches  --types TYPE(,TYPE2...)    show only these comma separated message types
63000d1cc1SJoe Perches  --ignore TYPE(,TYPE2...)   ignore various comma separated message types
646cd7f386SJoe Perches  --max-line-length=n        set the maximum line length, if exceeded, warn
65000d1cc1SJoe Perches  --show-types               show the message "types" in the output
6677f5b10aSHannes Eder  --root=PATH                PATH to the kernel tree root
6777f5b10aSHannes Eder  --no-summary               suppress the per-file summary
6877f5b10aSHannes Eder  --mailback                 only produce a report in case of warnings/errors
6977f5b10aSHannes Eder  --summary-file             include the filename in summary
7077f5b10aSHannes Eder  --debug KEY=[0|1]          turn on/off debugging of KEY, where KEY is one of
7177f5b10aSHannes Eder                             'values', 'possible', 'type', and 'attr' (default
7277f5b10aSHannes Eder                             is all off)
7377f5b10aSHannes Eder  --test-only=WORD           report only warnings/errors containing WORD
7477f5b10aSHannes Eder                             literally
753705ce5bSJoe Perches  --fix                      EXPERIMENTAL - may create horrible results
763705ce5bSJoe Perches                             If correctable single-line errors exist, create
773705ce5bSJoe Perches                             "<inputfile>.EXPERIMENTAL-checkpatch-fixes"
783705ce5bSJoe Perches                             with potential errors corrected to the preferred
793705ce5bSJoe Perches                             checkpatch style
809624b8d6SJoe Perches  --fix-inplace              EXPERIMENTAL - may create horrible results
819624b8d6SJoe Perches                             Is the same as --fix, but overwrites the input
829624b8d6SJoe Perches                             file.  It's your fault if there's no backup or git
83d62a201fSDave Hansen  --ignore-perl-version      override checking of perl version.  expect
84d62a201fSDave Hansen                             runtime errors.
8577f5b10aSHannes Eder  -h, --help, --version      display this help and exit
8677f5b10aSHannes Eder
8777f5b10aSHannes EderWhen FILE is - read standard input.
8877f5b10aSHannes EderEOM
8977f5b10aSHannes Eder
9077f5b10aSHannes Eder	exit($exitcode);
9177f5b10aSHannes Eder}
9277f5b10aSHannes Eder
93000d1cc1SJoe Perchesmy $conf = which_conf($configuration_file);
94000d1cc1SJoe Perchesif (-f $conf) {
95000d1cc1SJoe Perches	my @conf_args;
96000d1cc1SJoe Perches	open(my $conffile, '<', "$conf")
97000d1cc1SJoe Perches	    or warn "$P: Can't find a readable $configuration_file file $!\n";
98000d1cc1SJoe Perches
99000d1cc1SJoe Perches	while (<$conffile>) {
100000d1cc1SJoe Perches		my $line = $_;
101000d1cc1SJoe Perches
102000d1cc1SJoe Perches		$line =~ s/\s*\n?$//g;
103000d1cc1SJoe Perches		$line =~ s/^\s*//g;
104000d1cc1SJoe Perches		$line =~ s/\s+/ /g;
105000d1cc1SJoe Perches
106000d1cc1SJoe Perches		next if ($line =~ m/^\s*#/);
107000d1cc1SJoe Perches		next if ($line =~ m/^\s*$/);
108000d1cc1SJoe Perches
109000d1cc1SJoe Perches		my @words = split(" ", $line);
110000d1cc1SJoe Perches		foreach my $word (@words) {
111000d1cc1SJoe Perches			last if ($word =~ m/^#/);
112000d1cc1SJoe Perches			push (@conf_args, $word);
113000d1cc1SJoe Perches		}
114000d1cc1SJoe Perches	}
115000d1cc1SJoe Perches	close($conffile);
116000d1cc1SJoe Perches	unshift(@ARGV, @conf_args) if @conf_args;
117000d1cc1SJoe Perches}
118000d1cc1SJoe Perches
1190a920b5bSAndy WhitcroftGetOptions(
1206c72ffaaSAndy Whitcroft	'q|quiet+'	=> \$quiet,
1210a920b5bSAndy Whitcroft	'tree!'		=> \$tree,
1220a920b5bSAndy Whitcroft	'signoff!'	=> \$chk_signoff,
1230a920b5bSAndy Whitcroft	'patch!'	=> \$chk_patch,
1246c72ffaaSAndy Whitcroft	'emacs!'	=> \$emacs,
1258905a67cSAndy Whitcroft	'terse!'	=> \$terse,
12677f5b10aSHannes Eder	'f|file!'	=> \$file,
1276c72ffaaSAndy Whitcroft	'subjective!'	=> \$check,
1286c72ffaaSAndy Whitcroft	'strict!'	=> \$check,
129000d1cc1SJoe Perches	'ignore=s'	=> \@ignore,
13091bfe484SJoe Perches	'types=s'	=> \@use,
131000d1cc1SJoe Perches	'show-types!'	=> \$show_types,
1326cd7f386SJoe Perches	'max-line-length=i' => \$max_line_length,
1336c72ffaaSAndy Whitcroft	'root=s'	=> \$root,
1348905a67cSAndy Whitcroft	'summary!'	=> \$summary,
1358905a67cSAndy Whitcroft	'mailback!'	=> \$mailback,
13613214adfSAndy Whitcroft	'summary-file!'	=> \$summary_file,
1373705ce5bSJoe Perches	'fix!'		=> \$fix,
1389624b8d6SJoe Perches	'fix-inplace!'	=> \$fix_inplace,
139d62a201fSDave Hansen	'ignore-perl-version!' => \$ignore_perl_version,
140c2fdda0dSAndy Whitcroft	'debug=s'	=> \%debug,
141773647a0SAndy Whitcroft	'test-only=s'	=> \$tst_only,
14277f5b10aSHannes Eder	'h|help'	=> \$help,
14377f5b10aSHannes Eder	'version'	=> \$help
14477f5b10aSHannes Eder) or help(1);
14577f5b10aSHannes Eder
14677f5b10aSHannes Ederhelp(0) if ($help);
1470a920b5bSAndy Whitcroft
1489624b8d6SJoe Perches$fix = 1 if ($fix_inplace);
1499624b8d6SJoe Perches
1500a920b5bSAndy Whitcroftmy $exit = 0;
1510a920b5bSAndy Whitcroft
152d62a201fSDave Hansenif ($^V && $^V lt $minimum_perl_version) {
153d62a201fSDave Hansen	printf "$P: requires at least perl version %vd\n", $minimum_perl_version;
154d62a201fSDave Hansen	if (!$ignore_perl_version) {
155d62a201fSDave Hansen		exit(1);
156d62a201fSDave Hansen	}
157d62a201fSDave Hansen}
158d62a201fSDave Hansen
1590a920b5bSAndy Whitcroftif ($#ARGV < 0) {
16077f5b10aSHannes Eder	print "$P: no input files\n";
1610a920b5bSAndy Whitcroft	exit(1);
1620a920b5bSAndy Whitcroft}
1630a920b5bSAndy Whitcroft
16491bfe484SJoe Perchessub hash_save_array_words {
16591bfe484SJoe Perches	my ($hashRef, $arrayRef) = @_;
16691bfe484SJoe Perches
16791bfe484SJoe Perches	my @array = split(/,/, join(',', @$arrayRef));
16891bfe484SJoe Perches	foreach my $word (@array) {
169000d1cc1SJoe Perches		$word =~ s/\s*\n?$//g;
170000d1cc1SJoe Perches		$word =~ s/^\s*//g;
171000d1cc1SJoe Perches		$word =~ s/\s+/ /g;
172000d1cc1SJoe Perches		$word =~ tr/[a-z]/[A-Z]/;
173000d1cc1SJoe Perches
174000d1cc1SJoe Perches		next if ($word =~ m/^\s*#/);
175000d1cc1SJoe Perches		next if ($word =~ m/^\s*$/);
176000d1cc1SJoe Perches
17791bfe484SJoe Perches		$hashRef->{$word}++;
178000d1cc1SJoe Perches	}
17991bfe484SJoe Perches}
18091bfe484SJoe Perches
18191bfe484SJoe Perchessub hash_show_words {
18291bfe484SJoe Perches	my ($hashRef, $prefix) = @_;
18391bfe484SJoe Perches
18458cb3cf6SJoe Perches	if ($quiet == 0 && keys %$hashRef) {
18591bfe484SJoe Perches		print "NOTE: $prefix message types:";
18658cb3cf6SJoe Perches		foreach my $word (sort keys %$hashRef) {
18791bfe484SJoe Perches			print " $word";
18891bfe484SJoe Perches		}
18991bfe484SJoe Perches		print "\n\n";
19091bfe484SJoe Perches	}
19191bfe484SJoe Perches}
19291bfe484SJoe Perches
19391bfe484SJoe Percheshash_save_array_words(\%ignore_type, \@ignore);
19491bfe484SJoe Percheshash_save_array_words(\%use_type, \@use);
195000d1cc1SJoe Perches
196c2fdda0dSAndy Whitcroftmy $dbg_values = 0;
197c2fdda0dSAndy Whitcroftmy $dbg_possible = 0;
1987429c690SAndy Whitcroftmy $dbg_type = 0;
199a1ef277eSAndy Whitcroftmy $dbg_attr = 0;
200c2fdda0dSAndy Whitcroftfor my $key (keys %debug) {
20121caa13cSAndy Whitcroft	## no critic
20221caa13cSAndy Whitcroft	eval "\${dbg_$key} = '$debug{$key}';";
20321caa13cSAndy Whitcroft	die "$@" if ($@);
204c2fdda0dSAndy Whitcroft}
205c2fdda0dSAndy Whitcroft
206d2c0a235SAndy Whitcroftmy $rpt_cleaners = 0;
207d2c0a235SAndy Whitcroft
2088905a67cSAndy Whitcroftif ($terse) {
2098905a67cSAndy Whitcroft	$emacs = 1;
2108905a67cSAndy Whitcroft	$quiet++;
2118905a67cSAndy Whitcroft}
2128905a67cSAndy Whitcroft
2136c72ffaaSAndy Whitcroftif ($tree) {
2146c72ffaaSAndy Whitcroft	if (defined $root) {
2156c72ffaaSAndy Whitcroft		if (!top_of_kernel_tree($root)) {
2166c72ffaaSAndy Whitcroft			die "$P: $root: --root does not point at a valid tree\n";
2176c72ffaaSAndy Whitcroft		}
2186c72ffaaSAndy Whitcroft	} else {
2196c72ffaaSAndy Whitcroft		if (top_of_kernel_tree('.')) {
2206c72ffaaSAndy Whitcroft			$root = '.';
2216c72ffaaSAndy Whitcroft		} elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
2226c72ffaaSAndy Whitcroft						top_of_kernel_tree($1)) {
2236c72ffaaSAndy Whitcroft			$root = $1;
2246c72ffaaSAndy Whitcroft		}
2256c72ffaaSAndy Whitcroft	}
2266c72ffaaSAndy Whitcroft
2276c72ffaaSAndy Whitcroft	if (!defined $root) {
2280a920b5bSAndy Whitcroft		print "Must be run from the top-level dir. of a kernel tree\n";
2290a920b5bSAndy Whitcroft		exit(2);
2300a920b5bSAndy Whitcroft	}
2316c72ffaaSAndy Whitcroft}
2326c72ffaaSAndy Whitcroft
2336c72ffaaSAndy Whitcroftmy $emitted_corrupt = 0;
2346c72ffaaSAndy Whitcroft
2352ceb532bSAndy Whitcroftour $Ident	= qr{
2362ceb532bSAndy Whitcroft			[A-Za-z_][A-Za-z\d_]*
2372ceb532bSAndy Whitcroft			(?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
2382ceb532bSAndy Whitcroft		}x;
2396c72ffaaSAndy Whitcroftour $Storage	= qr{extern|static|asmlinkage};
2406c72ffaaSAndy Whitcroftour $Sparse	= qr{
2416c72ffaaSAndy Whitcroft			__user|
2426c72ffaaSAndy Whitcroft			__kernel|
2436c72ffaaSAndy Whitcroft			__force|
2446c72ffaaSAndy Whitcroft			__iomem|
2456c72ffaaSAndy Whitcroft			__must_check|
2466c72ffaaSAndy Whitcroft			__init_refok|
247417495edSAndy Whitcroft			__kprobes|
248165e72a6SSven Eckelmann			__ref|
249165e72a6SSven Eckelmann			__rcu
2506c72ffaaSAndy Whitcroft		}x;
251e970b884SJoe Perchesour $InitAttributePrefix = qr{__(?:mem|cpu|dev|net_|)};
252e970b884SJoe Perchesour $InitAttributeData = qr{$InitAttributePrefix(?:initdata\b)};
253e970b884SJoe Perchesour $InitAttributeConst = qr{$InitAttributePrefix(?:initconst\b)};
254e970b884SJoe Perchesour $InitAttributeInit = qr{$InitAttributePrefix(?:init\b)};
255e970b884SJoe Perchesour $InitAttribute = qr{$InitAttributeData|$InitAttributeConst|$InitAttributeInit};
2568716de38SJoe Perches
25752131292SWolfram Sang# Notes to $Attribute:
25852131292SWolfram Sang# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
2596c72ffaaSAndy Whitcroftour $Attribute	= qr{
2606c72ffaaSAndy Whitcroft			const|
26103f1df7dSJoe Perches			__percpu|
26203f1df7dSJoe Perches			__nocast|
26303f1df7dSJoe Perches			__safe|
26403f1df7dSJoe Perches			__bitwise__|
26503f1df7dSJoe Perches			__packed__|
26603f1df7dSJoe Perches			__packed2__|
26703f1df7dSJoe Perches			__naked|
26803f1df7dSJoe Perches			__maybe_unused|
26903f1df7dSJoe Perches			__always_unused|
27003f1df7dSJoe Perches			__noreturn|
27103f1df7dSJoe Perches			__used|
27203f1df7dSJoe Perches			__cold|
27303f1df7dSJoe Perches			__noclone|
27403f1df7dSJoe Perches			__deprecated|
2756c72ffaaSAndy Whitcroft			__read_mostly|
2766c72ffaaSAndy Whitcroft			__kprobes|
2778716de38SJoe Perches			$InitAttribute|
27824e1d81aSAndy Whitcroft			____cacheline_aligned|
27924e1d81aSAndy Whitcroft			____cacheline_aligned_in_smp|
2805fe3af11SAndy Whitcroft			____cacheline_internodealigned_in_smp|
2815fe3af11SAndy Whitcroft			__weak
2826c72ffaaSAndy Whitcroft		  }x;
283c45dcabdSAndy Whitcroftour $Modifier;
284*91cb5195SJoe Perchesour $Inline	= qr{inline|__always_inline|noinline|__inline|__inline__};
2856c72ffaaSAndy Whitcroftour $Member	= qr{->$Ident|\.$Ident|\[[^]]*\]};
2866c72ffaaSAndy Whitcroftour $Lval	= qr{$Ident(?:$Member)*};
2876c72ffaaSAndy Whitcroft
28895e2c602SJoe Perchesour $Int_type	= qr{(?i)llu|ull|ll|lu|ul|l|u};
28995e2c602SJoe Perchesour $Binary	= qr{(?i)0b[01]+$Int_type?};
29095e2c602SJoe Perchesour $Hex	= qr{(?i)0x[0-9a-f]+$Int_type?};
29195e2c602SJoe Perchesour $Int	= qr{[0-9]+$Int_type?};
2922435880fSJoe Perchesour $Octal	= qr{0[0-7]+$Int_type?};
293326b1ffcSJoe Perchesour $Float_hex	= qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?};
294326b1ffcSJoe Perchesour $Float_dec	= qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?};
295326b1ffcSJoe Perchesour $Float_int	= qr{(?i)[0-9]+e-?[0-9]+[fl]?};
29674349bccSJoe Perchesour $Float	= qr{$Float_hex|$Float_dec|$Float_int};
2972435880fSJoe Perchesour $Constant	= qr{$Float|$Binary|$Octal|$Hex|$Int};
298326b1ffcSJoe Perchesour $Assignment	= qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=};
299447432f3SJoe Perchesour $Compare    = qr{<=|>=|==|!=|<|(?<!-)>};
30023f780c9SJoe Perchesour $Arithmetic = qr{\+|-|\*|\/|%};
3016c72ffaaSAndy Whitcroftour $Operators	= qr{
3026c72ffaaSAndy Whitcroft			<=|>=|==|!=|
3036c72ffaaSAndy Whitcroft			=>|->|<<|>>|<|>|!|~|
30423f780c9SJoe Perches			&&|\|\||,|\^|\+\+|--|&|\||$Arithmetic
3056c72ffaaSAndy Whitcroft		  }x;
3066c72ffaaSAndy Whitcroft
307*91cb5195SJoe Perchesour $c90_Keywords = qr{do|for|while|if|else|return|goto|continue|switch|default|case|break}x;
308*91cb5195SJoe Perches
3098905a67cSAndy Whitcroftour $NonptrType;
3108716de38SJoe Perchesour $NonptrTypeWithAttr;
3118905a67cSAndy Whitcroftour $Type;
3128905a67cSAndy Whitcroftour $Declare;
3138905a67cSAndy Whitcroft
31415662b3eSJoe Perchesour $NON_ASCII_UTF8	= qr{
31515662b3eSJoe Perches	[\xC2-\xDF][\x80-\xBF]               # non-overlong 2-byte
316171ae1a4SAndy Whitcroft	|  \xE0[\xA0-\xBF][\x80-\xBF]        # excluding overlongs
317171ae1a4SAndy Whitcroft	| [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}  # straight 3-byte
318171ae1a4SAndy Whitcroft	|  \xED[\x80-\x9F][\x80-\xBF]        # excluding surrogates
319171ae1a4SAndy Whitcroft	|  \xF0[\x90-\xBF][\x80-\xBF]{2}     # planes 1-3
320171ae1a4SAndy Whitcroft	| [\xF1-\xF3][\x80-\xBF]{3}          # planes 4-15
321171ae1a4SAndy Whitcroft	|  \xF4[\x80-\x8F][\x80-\xBF]{2}     # plane 16
322171ae1a4SAndy Whitcroft}x;
323171ae1a4SAndy Whitcroft
32415662b3eSJoe Perchesour $UTF8	= qr{
32515662b3eSJoe Perches	[\x09\x0A\x0D\x20-\x7E]              # ASCII
32615662b3eSJoe Perches	| $NON_ASCII_UTF8
32715662b3eSJoe Perches}x;
32815662b3eSJoe Perches
3298ed22cadSAndy Whitcroftour $typeTypedefs = qr{(?x:
330fb9e9096SAndy Whitcroft	(?:__)?(?:u|s|be|le)(?:8|16|32|64)|
3318ed22cadSAndy Whitcroft	atomic_t
3328ed22cadSAndy Whitcroft)};
3338ed22cadSAndy Whitcroft
334691e669bSJoe Perchesour $logFunctions = qr{(?x:
3356e60c02eSJoe Perches	printk(?:_ratelimited|_once|)|
3367d0b6594SJacob Keller	(?:[a-z0-9]+_){1,2}(?:printk|emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)(?:_ratelimited|_once|)|
3376e60c02eSJoe Perches	WARN(?:_RATELIMIT|_ONCE|)|
338b0531722SJoe Perches	panic|
33906668727SJoe Perches	MODULE_[A-Z_]+|
34006668727SJoe Perches	seq_vprintf|seq_printf|seq_puts
341691e669bSJoe Perches)};
342691e669bSJoe Perches
34320112475SJoe Perchesour $signature_tags = qr{(?xi:
34420112475SJoe Perches	Signed-off-by:|
34520112475SJoe Perches	Acked-by:|
34620112475SJoe Perches	Tested-by:|
34720112475SJoe Perches	Reviewed-by:|
34820112475SJoe Perches	Reported-by:|
3498543ae12SMugunthan V N	Suggested-by:|
35020112475SJoe Perches	To:|
35120112475SJoe Perches	Cc:
35220112475SJoe Perches)};
35320112475SJoe Perches
3548905a67cSAndy Whitcroftour @typeList = (
3558905a67cSAndy Whitcroft	qr{void},
356c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?char},
357c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?short},
358c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?int},
359c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?long},
360c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?long\s+int},
361c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?long\s+long},
362c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?long\s+long\s+int},
3638905a67cSAndy Whitcroft	qr{unsigned},
3648905a67cSAndy Whitcroft	qr{float},
3658905a67cSAndy Whitcroft	qr{double},
3668905a67cSAndy Whitcroft	qr{bool},
3678905a67cSAndy Whitcroft	qr{struct\s+$Ident},
3688905a67cSAndy Whitcroft	qr{union\s+$Ident},
3698905a67cSAndy Whitcroft	qr{enum\s+$Ident},
3708905a67cSAndy Whitcroft	qr{${Ident}_t},
3718905a67cSAndy Whitcroft	qr{${Ident}_handler},
3728905a67cSAndy Whitcroft	qr{${Ident}_handler_fn},
3738905a67cSAndy Whitcroft);
3748716de38SJoe Perchesour @typeListWithAttr = (
3758716de38SJoe Perches	@typeList,
3768716de38SJoe Perches	qr{struct\s+$InitAttribute\s+$Ident},
3778716de38SJoe Perches	qr{union\s+$InitAttribute\s+$Ident},
3788716de38SJoe Perches);
3798716de38SJoe Perches
380c45dcabdSAndy Whitcroftour @modifierList = (
381c45dcabdSAndy Whitcroft	qr{fastcall},
382c45dcabdSAndy Whitcroft);
3838905a67cSAndy Whitcroft
3842435880fSJoe Perchesour @mode_permission_funcs = (
3852435880fSJoe Perches	["module_param", 3],
3862435880fSJoe Perches	["module_param_(?:array|named|string)", 4],
3872435880fSJoe Perches	["module_param_array_named", 5],
3882435880fSJoe Perches	["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2],
3892435880fSJoe Perches	["proc_create(?:_data|)", 2],
3902435880fSJoe Perches	["(?:CLASS|DEVICE|SENSOR)_ATTR", 2],
3912435880fSJoe Perches);
3922435880fSJoe Perches
393515a235eSJoe Perches#Create a search pattern for all these functions to speed up a loop below
394515a235eSJoe Perchesour $mode_perms_search = "";
395515a235eSJoe Perchesforeach my $entry (@mode_permission_funcs) {
396515a235eSJoe Perches	$mode_perms_search .= '|' if ($mode_perms_search ne "");
397515a235eSJoe Perches	$mode_perms_search .= $entry->[0];
398515a235eSJoe Perches}
399515a235eSJoe Perches
4007840a94cSWolfram Sangour $allowed_asm_includes = qr{(?x:
4017840a94cSWolfram Sang	irq|
4027840a94cSWolfram Sang	memory
4037840a94cSWolfram Sang)};
4047840a94cSWolfram Sang# memory.h: ARM has a custom one
4057840a94cSWolfram Sang
4068905a67cSAndy Whitcroftsub build_types {
407d2172eb5SAndy Whitcroft	my $mods = "(?x:  \n" . join("|\n  ", @modifierList) . "\n)";
408d2172eb5SAndy Whitcroft	my $all = "(?x:  \n" . join("|\n  ", @typeList) . "\n)";
4098716de38SJoe Perches	my $allWithAttr = "(?x:  \n" . join("|\n  ", @typeListWithAttr) . "\n)";
410c8cb2ca3SAndy Whitcroft	$Modifier	= qr{(?:$Attribute|$Sparse|$mods)};
4118905a67cSAndy Whitcroft	$NonptrType	= qr{
412d2172eb5SAndy Whitcroft			(?:$Modifier\s+|const\s+)*
413cf655043SAndy Whitcroft			(?:
4146b48db24SAndy Whitcroft				(?:typeof|__typeof__)\s*\([^\)]*\)|
4158ed22cadSAndy Whitcroft				(?:$typeTypedefs\b)|
416c45dcabdSAndy Whitcroft				(?:${all}\b)
417cf655043SAndy Whitcroft			)
418c8cb2ca3SAndy Whitcroft			(?:\s+$Modifier|\s+const)*
4198905a67cSAndy Whitcroft		  }x;
4208716de38SJoe Perches	$NonptrTypeWithAttr	= qr{
4218716de38SJoe Perches			(?:$Modifier\s+|const\s+)*
4228716de38SJoe Perches			(?:
4238716de38SJoe Perches				(?:typeof|__typeof__)\s*\([^\)]*\)|
4248716de38SJoe Perches				(?:$typeTypedefs\b)|
4258716de38SJoe Perches				(?:${allWithAttr}\b)
4268716de38SJoe Perches			)
4278716de38SJoe Perches			(?:\s+$Modifier|\s+const)*
4288716de38SJoe Perches		  }x;
4298905a67cSAndy Whitcroft	$Type	= qr{
430c45dcabdSAndy Whitcroft			$NonptrType
431b337d8b8SAndy Whitcroft			(?:(?:\s|\*|\[\])+\s*const|(?:\s|\*|\[\])+|(?:\s*\[\s*\])+)?
432c8cb2ca3SAndy Whitcroft			(?:\s+$Inline|\s+$Modifier)*
4338905a67cSAndy Whitcroft		  }x;
434*91cb5195SJoe Perches	$Declare	= qr{(?:$Storage\s+(?:$Inline\s+)?)?$Type};
4358905a67cSAndy Whitcroft}
4368905a67cSAndy Whitcroftbuild_types();
4376c72ffaaSAndy Whitcroft
4387d2367afSJoe Perchesour $Typecast	= qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
439d1fe9c09SJoe Perches
440d1fe9c09SJoe Perches# Using $balanced_parens, $LvalOrFunc, or $FuncArg
441d1fe9c09SJoe Perches# requires at least perl version v5.10.0
442d1fe9c09SJoe Perches# Any use must be runtime checked with $^V
443d1fe9c09SJoe Perches
444d1fe9c09SJoe Perchesour $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
4452435880fSJoe Perchesour $LvalOrFunc	= qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*};
446d7c76ba7SJoe Perchesour $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant)};
4477d2367afSJoe Perches
4487d2367afSJoe Perchessub deparenthesize {
4497d2367afSJoe Perches	my ($string) = @_;
4507d2367afSJoe Perches	return "" if (!defined($string));
4515b9553abSJoe Perches
4525b9553abSJoe Perches	while ($string =~ /^\s*\(.*\)\s*$/) {
4535b9553abSJoe Perches		$string =~ s@^\s*\(\s*@@;
4545b9553abSJoe Perches		$string =~ s@\s*\)\s*$@@;
4555b9553abSJoe Perches	}
4565b9553abSJoe Perches
4577d2367afSJoe Perches	$string =~ s@\s+@ @g;
4585b9553abSJoe Perches
4597d2367afSJoe Perches	return $string;
4607d2367afSJoe Perches}
4617d2367afSJoe Perches
4623445686aSJoe Perchessub seed_camelcase_file {
4633445686aSJoe Perches	my ($file) = @_;
4643445686aSJoe Perches
4653445686aSJoe Perches	return if (!(-f $file));
4663445686aSJoe Perches
4673445686aSJoe Perches	local $/;
4683445686aSJoe Perches
4693445686aSJoe Perches	open(my $include_file, '<', "$file")
4703445686aSJoe Perches	    or warn "$P: Can't read '$file' $!\n";
4713445686aSJoe Perches	my $text = <$include_file>;
4723445686aSJoe Perches	close($include_file);
4733445686aSJoe Perches
4743445686aSJoe Perches	my @lines = split('\n', $text);
4753445686aSJoe Perches
4763445686aSJoe Perches	foreach my $line (@lines) {
4773445686aSJoe Perches		next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/);
4783445686aSJoe Perches		if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) {
4793445686aSJoe Perches			$camelcase{$1} = 1;
48011ea516aSJoe Perches		} elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) {
48111ea516aSJoe Perches			$camelcase{$1} = 1;
48211ea516aSJoe Perches		} elsif ($line =~ /^\s*(?:union|struct|enum)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[;\{]/) {
4833445686aSJoe Perches			$camelcase{$1} = 1;
4843445686aSJoe Perches		}
4853445686aSJoe Perches	}
4863445686aSJoe Perches}
4873445686aSJoe Perches
4883445686aSJoe Perchesmy $camelcase_seeded = 0;
4893445686aSJoe Perchessub seed_camelcase_includes {
4903445686aSJoe Perches	return if ($camelcase_seeded);
4913445686aSJoe Perches
4923445686aSJoe Perches	my $files;
493c707a81dSJoe Perches	my $camelcase_cache = "";
494c707a81dSJoe Perches	my @include_files = ();
495c707a81dSJoe Perches
496c707a81dSJoe Perches	$camelcase_seeded = 1;
497351b2a1fSJoe Perches
4983645e328SRichard Genoud	if (-e ".git") {
499351b2a1fSJoe Perches		my $git_last_include_commit = `git log --no-merges --pretty=format:"%h%n" -1 -- include`;
500351b2a1fSJoe Perches		chomp $git_last_include_commit;
501c707a81dSJoe Perches		$camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit";
502c707a81dSJoe Perches	} else {
503c707a81dSJoe Perches		my $last_mod_date = 0;
504c707a81dSJoe Perches		$files = `find $root/include -name "*.h"`;
505c707a81dSJoe Perches		@include_files = split('\n', $files);
506c707a81dSJoe Perches		foreach my $file (@include_files) {
507c707a81dSJoe Perches			my $date = POSIX::strftime("%Y%m%d%H%M",
508c707a81dSJoe Perches						   localtime((stat $file)[9]));
509c707a81dSJoe Perches			$last_mod_date = $date if ($last_mod_date < $date);
510c707a81dSJoe Perches		}
511c707a81dSJoe Perches		$camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date";
512c707a81dSJoe Perches	}
513c707a81dSJoe Perches
514c707a81dSJoe Perches	if ($camelcase_cache ne "" && -f $camelcase_cache) {
515c707a81dSJoe Perches		open(my $camelcase_file, '<', "$camelcase_cache")
516c707a81dSJoe Perches		    or warn "$P: Can't read '$camelcase_cache' $!\n";
517351b2a1fSJoe Perches		while (<$camelcase_file>) {
518351b2a1fSJoe Perches			chomp;
519351b2a1fSJoe Perches			$camelcase{$_} = 1;
520351b2a1fSJoe Perches		}
521351b2a1fSJoe Perches		close($camelcase_file);
522351b2a1fSJoe Perches
523351b2a1fSJoe Perches		return;
524351b2a1fSJoe Perches	}
525c707a81dSJoe Perches
5263645e328SRichard Genoud	if (-e ".git") {
527c707a81dSJoe Perches		$files = `git ls-files "include/*.h"`;
528c707a81dSJoe Perches		@include_files = split('\n', $files);
5293445686aSJoe Perches	}
530c707a81dSJoe Perches
5313445686aSJoe Perches	foreach my $file (@include_files) {
5323445686aSJoe Perches		seed_camelcase_file($file);
5333445686aSJoe Perches	}
534351b2a1fSJoe Perches
535c707a81dSJoe Perches	if ($camelcase_cache ne "") {
536351b2a1fSJoe Perches		unlink glob ".checkpatch-camelcase.*";
537c707a81dSJoe Perches		open(my $camelcase_file, '>', "$camelcase_cache")
538c707a81dSJoe Perches		    or warn "$P: Can't write '$camelcase_cache' $!\n";
539351b2a1fSJoe Perches		foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) {
540351b2a1fSJoe Perches			print $camelcase_file ("$_\n");
541351b2a1fSJoe Perches		}
542351b2a1fSJoe Perches		close($camelcase_file);
543351b2a1fSJoe Perches	}
5443445686aSJoe Perches}
5453445686aSJoe Perches
5466c72ffaaSAndy Whitcroft$chk_signoff = 0 if ($file);
5470a920b5bSAndy Whitcroft
54800df344fSAndy Whitcroftmy @rawlines = ();
549c2fdda0dSAndy Whitcroftmy @lines = ();
5503705ce5bSJoe Perchesmy @fixed = ();
551c2fdda0dSAndy Whitcroftmy $vname;
5526c72ffaaSAndy Whitcroftfor my $filename (@ARGV) {
55321caa13cSAndy Whitcroft	my $FILE;
5546c72ffaaSAndy Whitcroft	if ($file) {
55521caa13cSAndy Whitcroft		open($FILE, '-|', "diff -u /dev/null $filename") ||
5566c72ffaaSAndy Whitcroft			die "$P: $filename: diff failed - $!\n";
55721caa13cSAndy Whitcroft	} elsif ($filename eq '-') {
55821caa13cSAndy Whitcroft		open($FILE, '<&STDIN');
5596c72ffaaSAndy Whitcroft	} else {
56021caa13cSAndy Whitcroft		open($FILE, '<', "$filename") ||
5616c72ffaaSAndy Whitcroft			die "$P: $filename: open failed - $!\n";
5626c72ffaaSAndy Whitcroft	}
563c2fdda0dSAndy Whitcroft	if ($filename eq '-') {
564c2fdda0dSAndy Whitcroft		$vname = 'Your patch';
565c2fdda0dSAndy Whitcroft	} else {
566c2fdda0dSAndy Whitcroft		$vname = $filename;
567c2fdda0dSAndy Whitcroft	}
56821caa13cSAndy Whitcroft	while (<$FILE>) {
5690a920b5bSAndy Whitcroft		chomp;
57000df344fSAndy Whitcroft		push(@rawlines, $_);
5716c72ffaaSAndy Whitcroft	}
57221caa13cSAndy Whitcroft	close($FILE);
573c2fdda0dSAndy Whitcroft	if (!process($filename)) {
5740a920b5bSAndy Whitcroft		$exit = 1;
5750a920b5bSAndy Whitcroft	}
57600df344fSAndy Whitcroft	@rawlines = ();
57713214adfSAndy Whitcroft	@lines = ();
5783705ce5bSJoe Perches	@fixed = ();
5790a920b5bSAndy Whitcroft}
5800a920b5bSAndy Whitcroft
5810a920b5bSAndy Whitcroftexit($exit);
5820a920b5bSAndy Whitcroft
5830a920b5bSAndy Whitcroftsub top_of_kernel_tree {
5846c72ffaaSAndy Whitcroft	my ($root) = @_;
5856c72ffaaSAndy Whitcroft
5866c72ffaaSAndy Whitcroft	my @tree_check = (
5876c72ffaaSAndy Whitcroft		"COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
5886c72ffaaSAndy Whitcroft		"README", "Documentation", "arch", "include", "drivers",
5896c72ffaaSAndy Whitcroft		"fs", "init", "ipc", "kernel", "lib", "scripts",
5906c72ffaaSAndy Whitcroft	);
5916c72ffaaSAndy Whitcroft
5926c72ffaaSAndy Whitcroft	foreach my $check (@tree_check) {
5936c72ffaaSAndy Whitcroft		if (! -e $root . '/' . $check) {
5940a920b5bSAndy Whitcroft			return 0;
5950a920b5bSAndy Whitcroft		}
5966c72ffaaSAndy Whitcroft	}
5976c72ffaaSAndy Whitcroft	return 1;
5986c72ffaaSAndy Whitcroft}
5990a920b5bSAndy Whitcroft
60020112475SJoe Perchessub parse_email {
60120112475SJoe Perches	my ($formatted_email) = @_;
60220112475SJoe Perches
60320112475SJoe Perches	my $name = "";
60420112475SJoe Perches	my $address = "";
60520112475SJoe Perches	my $comment = "";
60620112475SJoe Perches
60720112475SJoe Perches	if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
60820112475SJoe Perches		$name = $1;
60920112475SJoe Perches		$address = $2;
61020112475SJoe Perches		$comment = $3 if defined $3;
61120112475SJoe Perches	} elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
61220112475SJoe Perches		$address = $1;
61320112475SJoe Perches		$comment = $2 if defined $2;
61420112475SJoe Perches	} elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
61520112475SJoe Perches		$address = $1;
61620112475SJoe Perches		$comment = $2 if defined $2;
61720112475SJoe Perches		$formatted_email =~ s/$address.*$//;
61820112475SJoe Perches		$name = $formatted_email;
6193705ce5bSJoe Perches		$name = trim($name);
62020112475SJoe Perches		$name =~ s/^\"|\"$//g;
62120112475SJoe Perches		# If there's a name left after stripping spaces and
62220112475SJoe Perches		# leading quotes, and the address doesn't have both
62320112475SJoe Perches		# leading and trailing angle brackets, the address
62420112475SJoe Perches		# is invalid. ie:
62520112475SJoe Perches		#   "joe smith [email protected]" bad
62620112475SJoe Perches		#   "joe smith <[email protected]" bad
62720112475SJoe Perches		if ($name ne "" && $address !~ /^<[^>]+>$/) {
62820112475SJoe Perches			$name = "";
62920112475SJoe Perches			$address = "";
63020112475SJoe Perches			$comment = "";
63120112475SJoe Perches		}
63220112475SJoe Perches	}
63320112475SJoe Perches
6343705ce5bSJoe Perches	$name = trim($name);
63520112475SJoe Perches	$name =~ s/^\"|\"$//g;
6363705ce5bSJoe Perches	$address = trim($address);
63720112475SJoe Perches	$address =~ s/^\<|\>$//g;
63820112475SJoe Perches
63920112475SJoe Perches	if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
64020112475SJoe Perches		$name =~ s/(?<!\\)"/\\"/g; ##escape quotes
64120112475SJoe Perches		$name = "\"$name\"";
64220112475SJoe Perches	}
64320112475SJoe Perches
64420112475SJoe Perches	return ($name, $address, $comment);
64520112475SJoe Perches}
64620112475SJoe Perches
64720112475SJoe Perchessub format_email {
64820112475SJoe Perches	my ($name, $address) = @_;
64920112475SJoe Perches
65020112475SJoe Perches	my $formatted_email;
65120112475SJoe Perches
6523705ce5bSJoe Perches	$name = trim($name);
65320112475SJoe Perches	$name =~ s/^\"|\"$//g;
6543705ce5bSJoe Perches	$address = trim($address);
65520112475SJoe Perches
65620112475SJoe Perches	if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
65720112475SJoe Perches		$name =~ s/(?<!\\)"/\\"/g; ##escape quotes
65820112475SJoe Perches		$name = "\"$name\"";
65920112475SJoe Perches	}
66020112475SJoe Perches
66120112475SJoe Perches	if ("$name" eq "") {
66220112475SJoe Perches		$formatted_email = "$address";
66320112475SJoe Perches	} else {
66420112475SJoe Perches		$formatted_email = "$name <$address>";
66520112475SJoe Perches	}
66620112475SJoe Perches
66720112475SJoe Perches	return $formatted_email;
66820112475SJoe Perches}
66920112475SJoe Perches
670000d1cc1SJoe Perchessub which_conf {
671000d1cc1SJoe Perches	my ($conf) = @_;
672000d1cc1SJoe Perches
673000d1cc1SJoe Perches	foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
674000d1cc1SJoe Perches		if (-e "$path/$conf") {
675000d1cc1SJoe Perches			return "$path/$conf";
676000d1cc1SJoe Perches		}
677000d1cc1SJoe Perches	}
678000d1cc1SJoe Perches
679000d1cc1SJoe Perches	return "";
680000d1cc1SJoe Perches}
681000d1cc1SJoe Perches
6820a920b5bSAndy Whitcroftsub expand_tabs {
6830a920b5bSAndy Whitcroft	my ($str) = @_;
6840a920b5bSAndy Whitcroft
6850a920b5bSAndy Whitcroft	my $res = '';
6860a920b5bSAndy Whitcroft	my $n = 0;
6870a920b5bSAndy Whitcroft	for my $c (split(//, $str)) {
6880a920b5bSAndy Whitcroft		if ($c eq "\t") {
6890a920b5bSAndy Whitcroft			$res .= ' ';
6900a920b5bSAndy Whitcroft			$n++;
6910a920b5bSAndy Whitcroft			for (; ($n % 8) != 0; $n++) {
6920a920b5bSAndy Whitcroft				$res .= ' ';
6930a920b5bSAndy Whitcroft			}
6940a920b5bSAndy Whitcroft			next;
6950a920b5bSAndy Whitcroft		}
6960a920b5bSAndy Whitcroft		$res .= $c;
6970a920b5bSAndy Whitcroft		$n++;
6980a920b5bSAndy Whitcroft	}
6990a920b5bSAndy Whitcroft
7000a920b5bSAndy Whitcroft	return $res;
7010a920b5bSAndy Whitcroft}
7026c72ffaaSAndy Whitcroftsub copy_spacing {
703773647a0SAndy Whitcroft	(my $res = shift) =~ tr/\t/ /c;
7046c72ffaaSAndy Whitcroft	return $res;
7056c72ffaaSAndy Whitcroft}
7060a920b5bSAndy Whitcroft
7074a0df2efSAndy Whitcroftsub line_stats {
7084a0df2efSAndy Whitcroft	my ($line) = @_;
7094a0df2efSAndy Whitcroft
7104a0df2efSAndy Whitcroft	# Drop the diff line leader and expand tabs
7114a0df2efSAndy Whitcroft	$line =~ s/^.//;
7124a0df2efSAndy Whitcroft	$line = expand_tabs($line);
7134a0df2efSAndy Whitcroft
7144a0df2efSAndy Whitcroft	# Pick the indent from the front of the line.
7154a0df2efSAndy Whitcroft	my ($white) = ($line =~ /^(\s*)/);
7164a0df2efSAndy Whitcroft
7174a0df2efSAndy Whitcroft	return (length($line), length($white));
7184a0df2efSAndy Whitcroft}
7194a0df2efSAndy Whitcroft
720773647a0SAndy Whitcroftmy $sanitise_quote = '';
721773647a0SAndy Whitcroft
722773647a0SAndy Whitcroftsub sanitise_line_reset {
723773647a0SAndy Whitcroft	my ($in_comment) = @_;
724773647a0SAndy Whitcroft
725773647a0SAndy Whitcroft	if ($in_comment) {
726773647a0SAndy Whitcroft		$sanitise_quote = '*/';
727773647a0SAndy Whitcroft	} else {
728773647a0SAndy Whitcroft		$sanitise_quote = '';
729773647a0SAndy Whitcroft	}
730773647a0SAndy Whitcroft}
73100df344fSAndy Whitcroftsub sanitise_line {
73200df344fSAndy Whitcroft	my ($line) = @_;
73300df344fSAndy Whitcroft
73400df344fSAndy Whitcroft	my $res = '';
73500df344fSAndy Whitcroft	my $l = '';
73600df344fSAndy Whitcroft
737c2fdda0dSAndy Whitcroft	my $qlen = 0;
738773647a0SAndy Whitcroft	my $off = 0;
739773647a0SAndy Whitcroft	my $c;
74000df344fSAndy Whitcroft
741773647a0SAndy Whitcroft	# Always copy over the diff marker.
742773647a0SAndy Whitcroft	$res = substr($line, 0, 1);
743773647a0SAndy Whitcroft
744773647a0SAndy Whitcroft	for ($off = 1; $off < length($line); $off++) {
745773647a0SAndy Whitcroft		$c = substr($line, $off, 1);
746773647a0SAndy Whitcroft
747773647a0SAndy Whitcroft		# Comments we are wacking completly including the begin
748773647a0SAndy Whitcroft		# and end, all to $;.
749773647a0SAndy Whitcroft		if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
750773647a0SAndy Whitcroft			$sanitise_quote = '*/';
751773647a0SAndy Whitcroft
752773647a0SAndy Whitcroft			substr($res, $off, 2, "$;$;");
753773647a0SAndy Whitcroft			$off++;
75400df344fSAndy Whitcroft			next;
755773647a0SAndy Whitcroft		}
75681bc0e02SAndy Whitcroft		if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
757773647a0SAndy Whitcroft			$sanitise_quote = '';
758773647a0SAndy Whitcroft			substr($res, $off, 2, "$;$;");
759773647a0SAndy Whitcroft			$off++;
760773647a0SAndy Whitcroft			next;
761773647a0SAndy Whitcroft		}
762113f04a8SDaniel Walker		if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
763113f04a8SDaniel Walker			$sanitise_quote = '//';
764113f04a8SDaniel Walker
765113f04a8SDaniel Walker			substr($res, $off, 2, $sanitise_quote);
766113f04a8SDaniel Walker			$off++;
767113f04a8SDaniel Walker			next;
768113f04a8SDaniel Walker		}
769773647a0SAndy Whitcroft
770773647a0SAndy Whitcroft		# A \ in a string means ignore the next character.
771773647a0SAndy Whitcroft		if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
772773647a0SAndy Whitcroft		    $c eq "\\") {
773773647a0SAndy Whitcroft			substr($res, $off, 2, 'XX');
774773647a0SAndy Whitcroft			$off++;
775773647a0SAndy Whitcroft			next;
776773647a0SAndy Whitcroft		}
777773647a0SAndy Whitcroft		# Regular quotes.
778773647a0SAndy Whitcroft		if ($c eq "'" || $c eq '"') {
779773647a0SAndy Whitcroft			if ($sanitise_quote eq '') {
780773647a0SAndy Whitcroft				$sanitise_quote = $c;
781773647a0SAndy Whitcroft
782773647a0SAndy Whitcroft				substr($res, $off, 1, $c);
783773647a0SAndy Whitcroft				next;
784773647a0SAndy Whitcroft			} elsif ($sanitise_quote eq $c) {
785773647a0SAndy Whitcroft				$sanitise_quote = '';
78600df344fSAndy Whitcroft			}
78700df344fSAndy Whitcroft		}
788773647a0SAndy Whitcroft
789fae17daeSAndy Whitcroft		#print "c<$c> SQ<$sanitise_quote>\n";
790773647a0SAndy Whitcroft		if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
791773647a0SAndy Whitcroft			substr($res, $off, 1, $;);
792113f04a8SDaniel Walker		} elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
793113f04a8SDaniel Walker			substr($res, $off, 1, $;);
794773647a0SAndy Whitcroft		} elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
795773647a0SAndy Whitcroft			substr($res, $off, 1, 'X');
79600df344fSAndy Whitcroft		} else {
797773647a0SAndy Whitcroft			substr($res, $off, 1, $c);
79800df344fSAndy Whitcroft		}
799c2fdda0dSAndy Whitcroft	}
800c2fdda0dSAndy Whitcroft
801113f04a8SDaniel Walker	if ($sanitise_quote eq '//') {
802113f04a8SDaniel Walker		$sanitise_quote = '';
803113f04a8SDaniel Walker	}
804113f04a8SDaniel Walker
805c2fdda0dSAndy Whitcroft	# The pathname on a #include may be surrounded by '<' and '>'.
806c45dcabdSAndy Whitcroft	if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
807c2fdda0dSAndy Whitcroft		my $clean = 'X' x length($1);
808c2fdda0dSAndy Whitcroft		$res =~ s@\<.*\>@<$clean>@;
809c2fdda0dSAndy Whitcroft
810c2fdda0dSAndy Whitcroft	# The whole of a #error is a string.
811c45dcabdSAndy Whitcroft	} elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
812c2fdda0dSAndy Whitcroft		my $clean = 'X' x length($1);
813c45dcabdSAndy Whitcroft		$res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
814c2fdda0dSAndy Whitcroft	}
815c2fdda0dSAndy Whitcroft
81600df344fSAndy Whitcroft	return $res;
81700df344fSAndy Whitcroft}
81800df344fSAndy Whitcroft
819a6962d72SJoe Perchessub get_quoted_string {
820a6962d72SJoe Perches	my ($line, $rawline) = @_;
821a6962d72SJoe Perches
822a6962d72SJoe Perches	return "" if ($line !~ m/(\"[X]+\")/g);
823a6962d72SJoe Perches	return substr($rawline, $-[0], $+[0] - $-[0]);
824a6962d72SJoe Perches}
825a6962d72SJoe Perches
8268905a67cSAndy Whitcroftsub ctx_statement_block {
8278905a67cSAndy Whitcroft	my ($linenr, $remain, $off) = @_;
8288905a67cSAndy Whitcroft	my $line = $linenr - 1;
8298905a67cSAndy Whitcroft	my $blk = '';
8308905a67cSAndy Whitcroft	my $soff = $off;
8318905a67cSAndy Whitcroft	my $coff = $off - 1;
832773647a0SAndy Whitcroft	my $coff_set = 0;
8338905a67cSAndy Whitcroft
83413214adfSAndy Whitcroft	my $loff = 0;
83513214adfSAndy Whitcroft
8368905a67cSAndy Whitcroft	my $type = '';
8378905a67cSAndy Whitcroft	my $level = 0;
838a2750645SAndy Whitcroft	my @stack = ();
839cf655043SAndy Whitcroft	my $p;
8408905a67cSAndy Whitcroft	my $c;
8418905a67cSAndy Whitcroft	my $len = 0;
84213214adfSAndy Whitcroft
84313214adfSAndy Whitcroft	my $remainder;
8448905a67cSAndy Whitcroft	while (1) {
845a2750645SAndy Whitcroft		@stack = (['', 0]) if ($#stack == -1);
846a2750645SAndy Whitcroft
847773647a0SAndy Whitcroft		#warn "CSB: blk<$blk> remain<$remain>\n";
8488905a67cSAndy Whitcroft		# If we are about to drop off the end, pull in more
8498905a67cSAndy Whitcroft		# context.
8508905a67cSAndy Whitcroft		if ($off >= $len) {
8518905a67cSAndy Whitcroft			for (; $remain > 0; $line++) {
852dea33496SAndy Whitcroft				last if (!defined $lines[$line]);
853c2fdda0dSAndy Whitcroft				next if ($lines[$line] =~ /^-/);
8548905a67cSAndy Whitcroft				$remain--;
85513214adfSAndy Whitcroft				$loff = $len;
856c2fdda0dSAndy Whitcroft				$blk .= $lines[$line] . "\n";
8578905a67cSAndy Whitcroft				$len = length($blk);
8588905a67cSAndy Whitcroft				$line++;
8598905a67cSAndy Whitcroft				last;
8608905a67cSAndy Whitcroft			}
8618905a67cSAndy Whitcroft			# Bail if there is no further context.
8628905a67cSAndy Whitcroft			#warn "CSB: blk<$blk> off<$off> len<$len>\n";
86313214adfSAndy Whitcroft			if ($off >= $len) {
8648905a67cSAndy Whitcroft				last;
8658905a67cSAndy Whitcroft			}
866f74bd194SAndy Whitcroft			if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
867f74bd194SAndy Whitcroft				$level++;
868f74bd194SAndy Whitcroft				$type = '#';
869f74bd194SAndy Whitcroft			}
8708905a67cSAndy Whitcroft		}
871cf655043SAndy Whitcroft		$p = $c;
8728905a67cSAndy Whitcroft		$c = substr($blk, $off, 1);
87313214adfSAndy Whitcroft		$remainder = substr($blk, $off);
8748905a67cSAndy Whitcroft
875773647a0SAndy Whitcroft		#warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
8764635f4fbSAndy Whitcroft
8774635f4fbSAndy Whitcroft		# Handle nested #if/#else.
8784635f4fbSAndy Whitcroft		if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
8794635f4fbSAndy Whitcroft			push(@stack, [ $type, $level ]);
8804635f4fbSAndy Whitcroft		} elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
8814635f4fbSAndy Whitcroft			($type, $level) = @{$stack[$#stack - 1]};
8824635f4fbSAndy Whitcroft		} elsif ($remainder =~ /^#\s*endif\b/) {
8834635f4fbSAndy Whitcroft			($type, $level) = @{pop(@stack)};
8844635f4fbSAndy Whitcroft		}
8854635f4fbSAndy Whitcroft
8868905a67cSAndy Whitcroft		# Statement ends at the ';' or a close '}' at the
8878905a67cSAndy Whitcroft		# outermost level.
8888905a67cSAndy Whitcroft		if ($level == 0 && $c eq ';') {
8898905a67cSAndy Whitcroft			last;
8908905a67cSAndy Whitcroft		}
8918905a67cSAndy Whitcroft
89213214adfSAndy Whitcroft		# An else is really a conditional as long as its not else if
893773647a0SAndy Whitcroft		if ($level == 0 && $coff_set == 0 &&
894773647a0SAndy Whitcroft				(!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
895773647a0SAndy Whitcroft				$remainder =~ /^(else)(?:\s|{)/ &&
896773647a0SAndy Whitcroft				$remainder !~ /^else\s+if\b/) {
897773647a0SAndy Whitcroft			$coff = $off + length($1) - 1;
898773647a0SAndy Whitcroft			$coff_set = 1;
899773647a0SAndy Whitcroft			#warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
900773647a0SAndy Whitcroft			#warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
90113214adfSAndy Whitcroft		}
90213214adfSAndy Whitcroft
9038905a67cSAndy Whitcroft		if (($type eq '' || $type eq '(') && $c eq '(') {
9048905a67cSAndy Whitcroft			$level++;
9058905a67cSAndy Whitcroft			$type = '(';
9068905a67cSAndy Whitcroft		}
9078905a67cSAndy Whitcroft		if ($type eq '(' && $c eq ')') {
9088905a67cSAndy Whitcroft			$level--;
9098905a67cSAndy Whitcroft			$type = ($level != 0)? '(' : '';
9108905a67cSAndy Whitcroft
9118905a67cSAndy Whitcroft			if ($level == 0 && $coff < $soff) {
9128905a67cSAndy Whitcroft				$coff = $off;
913773647a0SAndy Whitcroft				$coff_set = 1;
914773647a0SAndy Whitcroft				#warn "CSB: mark coff<$coff>\n";
9158905a67cSAndy Whitcroft			}
9168905a67cSAndy Whitcroft		}
9178905a67cSAndy Whitcroft		if (($type eq '' || $type eq '{') && $c eq '{') {
9188905a67cSAndy Whitcroft			$level++;
9198905a67cSAndy Whitcroft			$type = '{';
9208905a67cSAndy Whitcroft		}
9218905a67cSAndy Whitcroft		if ($type eq '{' && $c eq '}') {
9228905a67cSAndy Whitcroft			$level--;
9238905a67cSAndy Whitcroft			$type = ($level != 0)? '{' : '';
9248905a67cSAndy Whitcroft
9258905a67cSAndy Whitcroft			if ($level == 0) {
926b998e001SPatrick Pannuto				if (substr($blk, $off + 1, 1) eq ';') {
927b998e001SPatrick Pannuto					$off++;
928b998e001SPatrick Pannuto				}
9298905a67cSAndy Whitcroft				last;
9308905a67cSAndy Whitcroft			}
9318905a67cSAndy Whitcroft		}
932f74bd194SAndy Whitcroft		# Preprocessor commands end at the newline unless escaped.
933f74bd194SAndy Whitcroft		if ($type eq '#' && $c eq "\n" && $p ne "\\") {
934f74bd194SAndy Whitcroft			$level--;
935f74bd194SAndy Whitcroft			$type = '';
936f74bd194SAndy Whitcroft			$off++;
937f74bd194SAndy Whitcroft			last;
938f74bd194SAndy Whitcroft		}
9398905a67cSAndy Whitcroft		$off++;
9408905a67cSAndy Whitcroft	}
941a3bb97a7SAndy Whitcroft	# We are truly at the end, so shuffle to the next line.
94213214adfSAndy Whitcroft	if ($off == $len) {
943a3bb97a7SAndy Whitcroft		$loff = $len + 1;
94413214adfSAndy Whitcroft		$line++;
94513214adfSAndy Whitcroft		$remain--;
94613214adfSAndy Whitcroft	}
9478905a67cSAndy Whitcroft
9488905a67cSAndy Whitcroft	my $statement = substr($blk, $soff, $off - $soff + 1);
9498905a67cSAndy Whitcroft	my $condition = substr($blk, $soff, $coff - $soff + 1);
9508905a67cSAndy Whitcroft
9518905a67cSAndy Whitcroft	#warn "STATEMENT<$statement>\n";
9528905a67cSAndy Whitcroft	#warn "CONDITION<$condition>\n";
9538905a67cSAndy Whitcroft
954773647a0SAndy Whitcroft	#print "coff<$coff> soff<$off> loff<$loff>\n";
95513214adfSAndy Whitcroft
95613214adfSAndy Whitcroft	return ($statement, $condition,
95713214adfSAndy Whitcroft			$line, $remain + 1, $off - $loff + 1, $level);
95813214adfSAndy Whitcroft}
95913214adfSAndy Whitcroft
960cf655043SAndy Whitcroftsub statement_lines {
961cf655043SAndy Whitcroft	my ($stmt) = @_;
962cf655043SAndy Whitcroft
963cf655043SAndy Whitcroft	# Strip the diff line prefixes and rip blank lines at start and end.
964cf655043SAndy Whitcroft	$stmt =~ s/(^|\n)./$1/g;
965cf655043SAndy Whitcroft	$stmt =~ s/^\s*//;
966cf655043SAndy Whitcroft	$stmt =~ s/\s*$//;
967cf655043SAndy Whitcroft
968cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
969cf655043SAndy Whitcroft
970cf655043SAndy Whitcroft	return $#stmt_lines + 2;
971cf655043SAndy Whitcroft}
972cf655043SAndy Whitcroft
973cf655043SAndy Whitcroftsub statement_rawlines {
974cf655043SAndy Whitcroft	my ($stmt) = @_;
975cf655043SAndy Whitcroft
976cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
977cf655043SAndy Whitcroft
978cf655043SAndy Whitcroft	return $#stmt_lines + 2;
979cf655043SAndy Whitcroft}
980cf655043SAndy Whitcroft
981cf655043SAndy Whitcroftsub statement_block_size {
982cf655043SAndy Whitcroft	my ($stmt) = @_;
983cf655043SAndy Whitcroft
984cf655043SAndy Whitcroft	$stmt =~ s/(^|\n)./$1/g;
985cf655043SAndy Whitcroft	$stmt =~ s/^\s*{//;
986cf655043SAndy Whitcroft	$stmt =~ s/}\s*$//;
987cf655043SAndy Whitcroft	$stmt =~ s/^\s*//;
988cf655043SAndy Whitcroft	$stmt =~ s/\s*$//;
989cf655043SAndy Whitcroft
990cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
991cf655043SAndy Whitcroft	my @stmt_statements = ($stmt =~ /;/g);
992cf655043SAndy Whitcroft
993cf655043SAndy Whitcroft	my $stmt_lines = $#stmt_lines + 2;
994cf655043SAndy Whitcroft	my $stmt_statements = $#stmt_statements + 1;
995cf655043SAndy Whitcroft
996cf655043SAndy Whitcroft	if ($stmt_lines > $stmt_statements) {
997cf655043SAndy Whitcroft		return $stmt_lines;
998cf655043SAndy Whitcroft	} else {
999cf655043SAndy Whitcroft		return $stmt_statements;
1000cf655043SAndy Whitcroft	}
1001cf655043SAndy Whitcroft}
1002cf655043SAndy Whitcroft
100313214adfSAndy Whitcroftsub ctx_statement_full {
100413214adfSAndy Whitcroft	my ($linenr, $remain, $off) = @_;
100513214adfSAndy Whitcroft	my ($statement, $condition, $level);
100613214adfSAndy Whitcroft
100713214adfSAndy Whitcroft	my (@chunks);
100813214adfSAndy Whitcroft
1009cf655043SAndy Whitcroft	# Grab the first conditional/block pair.
101013214adfSAndy Whitcroft	($statement, $condition, $linenr, $remain, $off, $level) =
101113214adfSAndy Whitcroft				ctx_statement_block($linenr, $remain, $off);
1012773647a0SAndy Whitcroft	#print "F: c<$condition> s<$statement> remain<$remain>\n";
101313214adfSAndy Whitcroft	push(@chunks, [ $condition, $statement ]);
1014cf655043SAndy Whitcroft	if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
1015cf655043SAndy Whitcroft		return ($level, $linenr, @chunks);
1016cf655043SAndy Whitcroft	}
1017cf655043SAndy Whitcroft
1018cf655043SAndy Whitcroft	# Pull in the following conditional/block pairs and see if they
1019cf655043SAndy Whitcroft	# could continue the statement.
1020cf655043SAndy Whitcroft	for (;;) {
102113214adfSAndy Whitcroft		($statement, $condition, $linenr, $remain, $off, $level) =
102213214adfSAndy Whitcroft				ctx_statement_block($linenr, $remain, $off);
1023cf655043SAndy Whitcroft		#print "C: c<$condition> s<$statement> remain<$remain>\n";
1024773647a0SAndy Whitcroft		last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
1025cf655043SAndy Whitcroft		#print "C: push\n";
1026cf655043SAndy Whitcroft		push(@chunks, [ $condition, $statement ]);
102713214adfSAndy Whitcroft	}
102813214adfSAndy Whitcroft
102913214adfSAndy Whitcroft	return ($level, $linenr, @chunks);
10308905a67cSAndy Whitcroft}
10318905a67cSAndy Whitcroft
10324a0df2efSAndy Whitcroftsub ctx_block_get {
1033f0a594c1SAndy Whitcroft	my ($linenr, $remain, $outer, $open, $close, $off) = @_;
10344a0df2efSAndy Whitcroft	my $line;
10354a0df2efSAndy Whitcroft	my $start = $linenr - 1;
10364a0df2efSAndy Whitcroft	my $blk = '';
10374a0df2efSAndy Whitcroft	my @o;
10384a0df2efSAndy Whitcroft	my @c;
10394a0df2efSAndy Whitcroft	my @res = ();
10404a0df2efSAndy Whitcroft
1041f0a594c1SAndy Whitcroft	my $level = 0;
10424635f4fbSAndy Whitcroft	my @stack = ($level);
104300df344fSAndy Whitcroft	for ($line = $start; $remain > 0; $line++) {
104400df344fSAndy Whitcroft		next if ($rawlines[$line] =~ /^-/);
104500df344fSAndy Whitcroft		$remain--;
104600df344fSAndy Whitcroft
104700df344fSAndy Whitcroft		$blk .= $rawlines[$line];
10484635f4fbSAndy Whitcroft
10494635f4fbSAndy Whitcroft		# Handle nested #if/#else.
105001464f30SAndy Whitcroft		if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
10514635f4fbSAndy Whitcroft			push(@stack, $level);
105201464f30SAndy Whitcroft		} elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
10534635f4fbSAndy Whitcroft			$level = $stack[$#stack - 1];
105401464f30SAndy Whitcroft		} elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
10554635f4fbSAndy Whitcroft			$level = pop(@stack);
10564635f4fbSAndy Whitcroft		}
10574635f4fbSAndy Whitcroft
105801464f30SAndy Whitcroft		foreach my $c (split(//, $lines[$line])) {
1059f0a594c1SAndy Whitcroft			##print "C<$c>L<$level><$open$close>O<$off>\n";
1060f0a594c1SAndy Whitcroft			if ($off > 0) {
1061f0a594c1SAndy Whitcroft				$off--;
1062f0a594c1SAndy Whitcroft				next;
1063f0a594c1SAndy Whitcroft			}
10644a0df2efSAndy Whitcroft
1065f0a594c1SAndy Whitcroft			if ($c eq $close && $level > 0) {
1066f0a594c1SAndy Whitcroft				$level--;
1067f0a594c1SAndy Whitcroft				last if ($level == 0);
1068f0a594c1SAndy Whitcroft			} elsif ($c eq $open) {
1069f0a594c1SAndy Whitcroft				$level++;
1070f0a594c1SAndy Whitcroft			}
1071f0a594c1SAndy Whitcroft		}
10724a0df2efSAndy Whitcroft
1073f0a594c1SAndy Whitcroft		if (!$outer || $level <= 1) {
107400df344fSAndy Whitcroft			push(@res, $rawlines[$line]);
10754a0df2efSAndy Whitcroft		}
10764a0df2efSAndy Whitcroft
1077f0a594c1SAndy Whitcroft		last if ($level == 0);
10784a0df2efSAndy Whitcroft	}
10794a0df2efSAndy Whitcroft
1080f0a594c1SAndy Whitcroft	return ($level, @res);
10814a0df2efSAndy Whitcroft}
10824a0df2efSAndy Whitcroftsub ctx_block_outer {
10834a0df2efSAndy Whitcroft	my ($linenr, $remain) = @_;
10844a0df2efSAndy Whitcroft
1085f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
1086f0a594c1SAndy Whitcroft	return @r;
10874a0df2efSAndy Whitcroft}
10884a0df2efSAndy Whitcroftsub ctx_block {
10894a0df2efSAndy Whitcroft	my ($linenr, $remain) = @_;
10904a0df2efSAndy Whitcroft
1091f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1092f0a594c1SAndy Whitcroft	return @r;
1093653d4876SAndy Whitcroft}
1094653d4876SAndy Whitcroftsub ctx_statement {
1095f0a594c1SAndy Whitcroft	my ($linenr, $remain, $off) = @_;
1096f0a594c1SAndy Whitcroft
1097f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1098f0a594c1SAndy Whitcroft	return @r;
1099f0a594c1SAndy Whitcroft}
1100f0a594c1SAndy Whitcroftsub ctx_block_level {
1101653d4876SAndy Whitcroft	my ($linenr, $remain) = @_;
1102653d4876SAndy Whitcroft
1103f0a594c1SAndy Whitcroft	return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
11044a0df2efSAndy Whitcroft}
11059c0ca6f9SAndy Whitcroftsub ctx_statement_level {
11069c0ca6f9SAndy Whitcroft	my ($linenr, $remain, $off) = @_;
11079c0ca6f9SAndy Whitcroft
11089c0ca6f9SAndy Whitcroft	return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
11099c0ca6f9SAndy Whitcroft}
11104a0df2efSAndy Whitcroft
11114a0df2efSAndy Whitcroftsub ctx_locate_comment {
11124a0df2efSAndy Whitcroft	my ($first_line, $end_line) = @_;
11134a0df2efSAndy Whitcroft
11144a0df2efSAndy Whitcroft	# Catch a comment on the end of the line itself.
1115beae6332SAndy Whitcroft	my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
11164a0df2efSAndy Whitcroft	return $current_comment if (defined $current_comment);
11174a0df2efSAndy Whitcroft
11184a0df2efSAndy Whitcroft	# Look through the context and try and figure out if there is a
11194a0df2efSAndy Whitcroft	# comment.
11204a0df2efSAndy Whitcroft	my $in_comment = 0;
11214a0df2efSAndy Whitcroft	$current_comment = '';
11224a0df2efSAndy Whitcroft	for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
112300df344fSAndy Whitcroft		my $line = $rawlines[$linenr - 1];
112400df344fSAndy Whitcroft		#warn "           $line\n";
11254a0df2efSAndy Whitcroft		if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
11264a0df2efSAndy Whitcroft			$in_comment = 1;
11274a0df2efSAndy Whitcroft		}
11284a0df2efSAndy Whitcroft		if ($line =~ m@/\*@) {
11294a0df2efSAndy Whitcroft			$in_comment = 1;
11304a0df2efSAndy Whitcroft		}
11314a0df2efSAndy Whitcroft		if (!$in_comment && $current_comment ne '') {
11324a0df2efSAndy Whitcroft			$current_comment = '';
11334a0df2efSAndy Whitcroft		}
11344a0df2efSAndy Whitcroft		$current_comment .= $line . "\n" if ($in_comment);
11354a0df2efSAndy Whitcroft		if ($line =~ m@\*/@) {
11364a0df2efSAndy Whitcroft			$in_comment = 0;
11374a0df2efSAndy Whitcroft		}
11384a0df2efSAndy Whitcroft	}
11394a0df2efSAndy Whitcroft
11404a0df2efSAndy Whitcroft	chomp($current_comment);
11414a0df2efSAndy Whitcroft	return($current_comment);
11424a0df2efSAndy Whitcroft}
11434a0df2efSAndy Whitcroftsub ctx_has_comment {
11444a0df2efSAndy Whitcroft	my ($first_line, $end_line) = @_;
11454a0df2efSAndy Whitcroft	my $cmt = ctx_locate_comment($first_line, $end_line);
11464a0df2efSAndy Whitcroft
114700df344fSAndy Whitcroft	##print "LINE: $rawlines[$end_line - 1 ]\n";
11484a0df2efSAndy Whitcroft	##print "CMMT: $cmt\n";
11494a0df2efSAndy Whitcroft
11504a0df2efSAndy Whitcroft	return ($cmt ne '');
11514a0df2efSAndy Whitcroft}
11524a0df2efSAndy Whitcroft
11534d001e4dSAndy Whitcroftsub raw_line {
11544d001e4dSAndy Whitcroft	my ($linenr, $cnt) = @_;
11554d001e4dSAndy Whitcroft
11564d001e4dSAndy Whitcroft	my $offset = $linenr - 1;
11574d001e4dSAndy Whitcroft	$cnt++;
11584d001e4dSAndy Whitcroft
11594d001e4dSAndy Whitcroft	my $line;
11604d001e4dSAndy Whitcroft	while ($cnt) {
11614d001e4dSAndy Whitcroft		$line = $rawlines[$offset++];
11624d001e4dSAndy Whitcroft		next if (defined($line) && $line =~ /^-/);
11634d001e4dSAndy Whitcroft		$cnt--;
11644d001e4dSAndy Whitcroft	}
11654d001e4dSAndy Whitcroft
11664d001e4dSAndy Whitcroft	return $line;
11674d001e4dSAndy Whitcroft}
11684d001e4dSAndy Whitcroft
11690a920b5bSAndy Whitcroftsub cat_vet {
11700a920b5bSAndy Whitcroft	my ($vet) = @_;
11719c0ca6f9SAndy Whitcroft	my ($res, $coded);
11720a920b5bSAndy Whitcroft
11739c0ca6f9SAndy Whitcroft	$res = '';
11746c72ffaaSAndy Whitcroft	while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
11756c72ffaaSAndy Whitcroft		$res .= $1;
11766c72ffaaSAndy Whitcroft		if ($2 ne '') {
11779c0ca6f9SAndy Whitcroft			$coded = sprintf("^%c", unpack('C', $2) + 64);
11786c72ffaaSAndy Whitcroft			$res .= $coded;
11796c72ffaaSAndy Whitcroft		}
11809c0ca6f9SAndy Whitcroft	}
11819c0ca6f9SAndy Whitcroft	$res =~ s/$/\$/;
11820a920b5bSAndy Whitcroft
11839c0ca6f9SAndy Whitcroft	return $res;
11840a920b5bSAndy Whitcroft}
11850a920b5bSAndy Whitcroft
1186c2fdda0dSAndy Whitcroftmy $av_preprocessor = 0;
1187cf655043SAndy Whitcroftmy $av_pending;
1188c2fdda0dSAndy Whitcroftmy @av_paren_type;
11891f65f947SAndy Whitcroftmy $av_pend_colon;
1190c2fdda0dSAndy Whitcroft
1191c2fdda0dSAndy Whitcroftsub annotate_reset {
1192c2fdda0dSAndy Whitcroft	$av_preprocessor = 0;
1193cf655043SAndy Whitcroft	$av_pending = '_';
1194cf655043SAndy Whitcroft	@av_paren_type = ('E');
11951f65f947SAndy Whitcroft	$av_pend_colon = 'O';
1196c2fdda0dSAndy Whitcroft}
1197c2fdda0dSAndy Whitcroft
11986c72ffaaSAndy Whitcroftsub annotate_values {
11996c72ffaaSAndy Whitcroft	my ($stream, $type) = @_;
12006c72ffaaSAndy Whitcroft
12016c72ffaaSAndy Whitcroft	my $res;
12021f65f947SAndy Whitcroft	my $var = '_' x length($stream);
12036c72ffaaSAndy Whitcroft	my $cur = $stream;
12046c72ffaaSAndy Whitcroft
1205c2fdda0dSAndy Whitcroft	print "$stream\n" if ($dbg_values > 1);
12066c72ffaaSAndy Whitcroft
12076c72ffaaSAndy Whitcroft	while (length($cur)) {
1208773647a0SAndy Whitcroft		@av_paren_type = ('E') if ($#av_paren_type < 0);
1209cf655043SAndy Whitcroft		print " <" . join('', @av_paren_type) .
1210171ae1a4SAndy Whitcroft				"> <$type> <$av_pending>" if ($dbg_values > 1);
12116c72ffaaSAndy Whitcroft		if ($cur =~ /^(\s+)/o) {
1212c2fdda0dSAndy Whitcroft			print "WS($1)\n" if ($dbg_values > 1);
1213c2fdda0dSAndy Whitcroft			if ($1 =~ /\n/ && $av_preprocessor) {
1214cf655043SAndy Whitcroft				$type = pop(@av_paren_type);
1215c2fdda0dSAndy Whitcroft				$av_preprocessor = 0;
12166c72ffaaSAndy Whitcroft			}
12176c72ffaaSAndy Whitcroft
1218c023e473SFlorian Mickler		} elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
12199446ef56SAndy Whitcroft			print "CAST($1)\n" if ($dbg_values > 1);
12209446ef56SAndy Whitcroft			push(@av_paren_type, $type);
1221addcdceaSAndy Whitcroft			$type = 'c';
12229446ef56SAndy Whitcroft
1223e91b6e26SAndy Whitcroft		} elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
1224c2fdda0dSAndy Whitcroft			print "DECLARE($1)\n" if ($dbg_values > 1);
12256c72ffaaSAndy Whitcroft			$type = 'T';
12266c72ffaaSAndy Whitcroft
1227389a2fe5SAndy Whitcroft		} elsif ($cur =~ /^($Modifier)\s*/) {
1228389a2fe5SAndy Whitcroft			print "MODIFIER($1)\n" if ($dbg_values > 1);
1229389a2fe5SAndy Whitcroft			$type = 'T';
1230389a2fe5SAndy Whitcroft
1231c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
1232171ae1a4SAndy Whitcroft			print "DEFINE($1,$2)\n" if ($dbg_values > 1);
1233c2fdda0dSAndy Whitcroft			$av_preprocessor = 1;
1234171ae1a4SAndy Whitcroft			push(@av_paren_type, $type);
1235171ae1a4SAndy Whitcroft			if ($2 ne '') {
1236cf655043SAndy Whitcroft				$av_pending = 'N';
1237171ae1a4SAndy Whitcroft			}
1238171ae1a4SAndy Whitcroft			$type = 'E';
1239171ae1a4SAndy Whitcroft
1240c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
1241171ae1a4SAndy Whitcroft			print "UNDEF($1)\n" if ($dbg_values > 1);
1242171ae1a4SAndy Whitcroft			$av_preprocessor = 1;
1243171ae1a4SAndy Whitcroft			push(@av_paren_type, $type);
12446c72ffaaSAndy Whitcroft
1245c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
1246cf655043SAndy Whitcroft			print "PRE_START($1)\n" if ($dbg_values > 1);
1247c2fdda0dSAndy Whitcroft			$av_preprocessor = 1;
1248cf655043SAndy Whitcroft
1249cf655043SAndy Whitcroft			push(@av_paren_type, $type);
1250cf655043SAndy Whitcroft			push(@av_paren_type, $type);
1251171ae1a4SAndy Whitcroft			$type = 'E';
1252cf655043SAndy Whitcroft
1253c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
1254cf655043SAndy Whitcroft			print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1255cf655043SAndy Whitcroft			$av_preprocessor = 1;
1256cf655043SAndy Whitcroft
1257cf655043SAndy Whitcroft			push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1258cf655043SAndy Whitcroft
1259171ae1a4SAndy Whitcroft			$type = 'E';
1260cf655043SAndy Whitcroft
1261c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:endif))/o) {
1262cf655043SAndy Whitcroft			print "PRE_END($1)\n" if ($dbg_values > 1);
1263cf655043SAndy Whitcroft
1264cf655043SAndy Whitcroft			$av_preprocessor = 1;
1265cf655043SAndy Whitcroft
1266cf655043SAndy Whitcroft			# Assume all arms of the conditional end as this
1267cf655043SAndy Whitcroft			# one does, and continue as if the #endif was not here.
1268cf655043SAndy Whitcroft			pop(@av_paren_type);
1269cf655043SAndy Whitcroft			push(@av_paren_type, $type);
1270171ae1a4SAndy Whitcroft			$type = 'E';
12716c72ffaaSAndy Whitcroft
12726c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\\\n)/o) {
1273c2fdda0dSAndy Whitcroft			print "PRECONT($1)\n" if ($dbg_values > 1);
12746c72ffaaSAndy Whitcroft
1275171ae1a4SAndy Whitcroft		} elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1276171ae1a4SAndy Whitcroft			print "ATTR($1)\n" if ($dbg_values > 1);
1277171ae1a4SAndy Whitcroft			$av_pending = $type;
1278171ae1a4SAndy Whitcroft			$type = 'N';
1279171ae1a4SAndy Whitcroft
12806c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
1281c2fdda0dSAndy Whitcroft			print "SIZEOF($1)\n" if ($dbg_values > 1);
12826c72ffaaSAndy Whitcroft			if (defined $2) {
1283cf655043SAndy Whitcroft				$av_pending = 'V';
12846c72ffaaSAndy Whitcroft			}
12856c72ffaaSAndy Whitcroft			$type = 'N';
12866c72ffaaSAndy Whitcroft
128714b111c1SAndy Whitcroft		} elsif ($cur =~ /^(if|while|for)\b/o) {
1288c2fdda0dSAndy Whitcroft			print "COND($1)\n" if ($dbg_values > 1);
128914b111c1SAndy Whitcroft			$av_pending = 'E';
12906c72ffaaSAndy Whitcroft			$type = 'N';
12916c72ffaaSAndy Whitcroft
12921f65f947SAndy Whitcroft		} elsif ($cur =~/^(case)/o) {
12931f65f947SAndy Whitcroft			print "CASE($1)\n" if ($dbg_values > 1);
12941f65f947SAndy Whitcroft			$av_pend_colon = 'C';
12951f65f947SAndy Whitcroft			$type = 'N';
12961f65f947SAndy Whitcroft
129714b111c1SAndy Whitcroft		} elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
1298c2fdda0dSAndy Whitcroft			print "KEYWORD($1)\n" if ($dbg_values > 1);
12996c72ffaaSAndy Whitcroft			$type = 'N';
13006c72ffaaSAndy Whitcroft
13016c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\()/o) {
1302c2fdda0dSAndy Whitcroft			print "PAREN('$1')\n" if ($dbg_values > 1);
1303cf655043SAndy Whitcroft			push(@av_paren_type, $av_pending);
1304cf655043SAndy Whitcroft			$av_pending = '_';
13056c72ffaaSAndy Whitcroft			$type = 'N';
13066c72ffaaSAndy Whitcroft
13076c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\))/o) {
1308cf655043SAndy Whitcroft			my $new_type = pop(@av_paren_type);
1309cf655043SAndy Whitcroft			if ($new_type ne '_') {
1310cf655043SAndy Whitcroft				$type = $new_type;
1311c2fdda0dSAndy Whitcroft				print "PAREN('$1') -> $type\n"
1312c2fdda0dSAndy Whitcroft							if ($dbg_values > 1);
13136c72ffaaSAndy Whitcroft			} else {
1314c2fdda0dSAndy Whitcroft				print "PAREN('$1')\n" if ($dbg_values > 1);
13156c72ffaaSAndy Whitcroft			}
13166c72ffaaSAndy Whitcroft
1317c8cb2ca3SAndy Whitcroft		} elsif ($cur =~ /^($Ident)\s*\(/o) {
1318c2fdda0dSAndy Whitcroft			print "FUNC($1)\n" if ($dbg_values > 1);
1319c8cb2ca3SAndy Whitcroft			$type = 'V';
1320cf655043SAndy Whitcroft			$av_pending = 'V';
13216c72ffaaSAndy Whitcroft
13228e761b04SAndy Whitcroft		} elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
13238e761b04SAndy Whitcroft			if (defined $2 && $type eq 'C' || $type eq 'T') {
13241f65f947SAndy Whitcroft				$av_pend_colon = 'B';
13258e761b04SAndy Whitcroft			} elsif ($type eq 'E') {
13268e761b04SAndy Whitcroft				$av_pend_colon = 'L';
13271f65f947SAndy Whitcroft			}
13281f65f947SAndy Whitcroft			print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
13291f65f947SAndy Whitcroft			$type = 'V';
13301f65f947SAndy Whitcroft
13316c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Ident|$Constant)/o) {
1332c2fdda0dSAndy Whitcroft			print "IDENT($1)\n" if ($dbg_values > 1);
13336c72ffaaSAndy Whitcroft			$type = 'V';
13346c72ffaaSAndy Whitcroft
13356c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Assignment)/o) {
1336c2fdda0dSAndy Whitcroft			print "ASSIGN($1)\n" if ($dbg_values > 1);
13376c72ffaaSAndy Whitcroft			$type = 'N';
13386c72ffaaSAndy Whitcroft
1339cf655043SAndy Whitcroft		} elsif ($cur =~/^(;|{|})/) {
1340c2fdda0dSAndy Whitcroft			print "END($1)\n" if ($dbg_values > 1);
134113214adfSAndy Whitcroft			$type = 'E';
13421f65f947SAndy Whitcroft			$av_pend_colon = 'O';
134313214adfSAndy Whitcroft
13448e761b04SAndy Whitcroft		} elsif ($cur =~/^(,)/) {
13458e761b04SAndy Whitcroft			print "COMMA($1)\n" if ($dbg_values > 1);
13468e761b04SAndy Whitcroft			$type = 'C';
13478e761b04SAndy Whitcroft
13481f65f947SAndy Whitcroft		} elsif ($cur =~ /^(\?)/o) {
13491f65f947SAndy Whitcroft			print "QUESTION($1)\n" if ($dbg_values > 1);
13501f65f947SAndy Whitcroft			$type = 'N';
13511f65f947SAndy Whitcroft
13521f65f947SAndy Whitcroft		} elsif ($cur =~ /^(:)/o) {
13531f65f947SAndy Whitcroft			print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
13541f65f947SAndy Whitcroft
13551f65f947SAndy Whitcroft			substr($var, length($res), 1, $av_pend_colon);
13561f65f947SAndy Whitcroft			if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
13571f65f947SAndy Whitcroft				$type = 'E';
13581f65f947SAndy Whitcroft			} else {
13591f65f947SAndy Whitcroft				$type = 'N';
13601f65f947SAndy Whitcroft			}
13611f65f947SAndy Whitcroft			$av_pend_colon = 'O';
13621f65f947SAndy Whitcroft
13638e761b04SAndy Whitcroft		} elsif ($cur =~ /^(\[)/o) {
136413214adfSAndy Whitcroft			print "CLOSE($1)\n" if ($dbg_values > 1);
13656c72ffaaSAndy Whitcroft			$type = 'N';
13666c72ffaaSAndy Whitcroft
13670d413866SAndy Whitcroft		} elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
136874048ed8SAndy Whitcroft			my $variant;
136974048ed8SAndy Whitcroft
137074048ed8SAndy Whitcroft			print "OPV($1)\n" if ($dbg_values > 1);
137174048ed8SAndy Whitcroft			if ($type eq 'V') {
137274048ed8SAndy Whitcroft				$variant = 'B';
137374048ed8SAndy Whitcroft			} else {
137474048ed8SAndy Whitcroft				$variant = 'U';
137574048ed8SAndy Whitcroft			}
137674048ed8SAndy Whitcroft
137774048ed8SAndy Whitcroft			substr($var, length($res), 1, $variant);
137874048ed8SAndy Whitcroft			$type = 'N';
137974048ed8SAndy Whitcroft
13806c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Operators)/o) {
1381c2fdda0dSAndy Whitcroft			print "OP($1)\n" if ($dbg_values > 1);
13826c72ffaaSAndy Whitcroft			if ($1 ne '++' && $1 ne '--') {
13836c72ffaaSAndy Whitcroft				$type = 'N';
13846c72ffaaSAndy Whitcroft			}
13856c72ffaaSAndy Whitcroft
13866c72ffaaSAndy Whitcroft		} elsif ($cur =~ /(^.)/o) {
1387c2fdda0dSAndy Whitcroft			print "C($1)\n" if ($dbg_values > 1);
13886c72ffaaSAndy Whitcroft		}
13896c72ffaaSAndy Whitcroft		if (defined $1) {
13906c72ffaaSAndy Whitcroft			$cur = substr($cur, length($1));
13916c72ffaaSAndy Whitcroft			$res .= $type x length($1);
13926c72ffaaSAndy Whitcroft		}
13936c72ffaaSAndy Whitcroft	}
13946c72ffaaSAndy Whitcroft
13951f65f947SAndy Whitcroft	return ($res, $var);
13966c72ffaaSAndy Whitcroft}
13976c72ffaaSAndy Whitcroft
13988905a67cSAndy Whitcroftsub possible {
139913214adfSAndy Whitcroft	my ($possible, $line) = @_;
14009a974fdbSAndy Whitcroft	my $notPermitted = qr{(?:
14010776e594SAndy Whitcroft		^(?:
14020776e594SAndy Whitcroft			$Modifier|
14030776e594SAndy Whitcroft			$Storage|
14040776e594SAndy Whitcroft			$Type|
14059a974fdbSAndy Whitcroft			DEFINE_\S+
14069a974fdbSAndy Whitcroft		)$|
14079a974fdbSAndy Whitcroft		^(?:
14080776e594SAndy Whitcroft			goto|
14090776e594SAndy Whitcroft			return|
14100776e594SAndy Whitcroft			case|
14110776e594SAndy Whitcroft			else|
14120776e594SAndy Whitcroft			asm|__asm__|
141389a88353SAndy Whitcroft			do|
141489a88353SAndy Whitcroft			\#|
141589a88353SAndy Whitcroft			\#\#|
14169a974fdbSAndy Whitcroft		)(?:\s|$)|
14170776e594SAndy Whitcroft		^(?:typedef|struct|enum)\b
14189a974fdbSAndy Whitcroft	    )}x;
14199a974fdbSAndy Whitcroft	warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
14209a974fdbSAndy Whitcroft	if ($possible !~ $notPermitted) {
1421c45dcabdSAndy Whitcroft		# Check for modifiers.
1422c45dcabdSAndy Whitcroft		$possible =~ s/\s*$Storage\s*//g;
1423c45dcabdSAndy Whitcroft		$possible =~ s/\s*$Sparse\s*//g;
1424c45dcabdSAndy Whitcroft		if ($possible =~ /^\s*$/) {
1425c45dcabdSAndy Whitcroft
1426c45dcabdSAndy Whitcroft		} elsif ($possible =~ /\s/) {
1427c45dcabdSAndy Whitcroft			$possible =~ s/\s*$Type\s*//g;
1428d2506586SAndy Whitcroft			for my $modifier (split(' ', $possible)) {
14299a974fdbSAndy Whitcroft				if ($modifier !~ $notPermitted) {
1430d2506586SAndy Whitcroft					warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1431d2506586SAndy Whitcroft					push(@modifierList, $modifier);
1432d2506586SAndy Whitcroft				}
14339a974fdbSAndy Whitcroft			}
1434c45dcabdSAndy Whitcroft
1435c45dcabdSAndy Whitcroft		} else {
143613214adfSAndy Whitcroft			warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
14378905a67cSAndy Whitcroft			push(@typeList, $possible);
1438c45dcabdSAndy Whitcroft		}
14398905a67cSAndy Whitcroft		build_types();
14400776e594SAndy Whitcroft	} else {
14410776e594SAndy Whitcroft		warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
14428905a67cSAndy Whitcroft	}
14438905a67cSAndy Whitcroft}
14448905a67cSAndy Whitcroft
14456c72ffaaSAndy Whitcroftmy $prefix = '';
14466c72ffaaSAndy Whitcroft
1447000d1cc1SJoe Perchessub show_type {
1448cbec18afSJoe Perches	my ($type) = @_;
144991bfe484SJoe Perches
1450cbec18afSJoe Perches	return defined $use_type{$type} if (scalar keys %use_type > 0);
1451cbec18afSJoe Perches
1452cbec18afSJoe Perches	return !defined $ignore_type{$type};
1453000d1cc1SJoe Perches}
1454000d1cc1SJoe Perches
1455f0a594c1SAndy Whitcroftsub report {
1456cbec18afSJoe Perches	my ($level, $type, $msg) = @_;
1457cbec18afSJoe Perches
1458cbec18afSJoe Perches	if (!show_type($type) ||
1459cbec18afSJoe Perches	    (defined $tst_only && $msg !~ /\Q$tst_only\E/)) {
1460773647a0SAndy Whitcroft		return 0;
1461773647a0SAndy Whitcroft	}
1462000d1cc1SJoe Perches	my $line;
1463000d1cc1SJoe Perches	if ($show_types) {
1464cbec18afSJoe Perches		$line = "$prefix$level:$type: $msg\n";
1465000d1cc1SJoe Perches	} else {
1466cbec18afSJoe Perches		$line = "$prefix$level: $msg\n";
1467000d1cc1SJoe Perches	}
14688905a67cSAndy Whitcroft	$line = (split('\n', $line))[0] . "\n" if ($terse);
14698905a67cSAndy Whitcroft
147013214adfSAndy Whitcroft	push(our @report, $line);
1471773647a0SAndy Whitcroft
1472773647a0SAndy Whitcroft	return 1;
1473f0a594c1SAndy Whitcroft}
1474cbec18afSJoe Perches
1475f0a594c1SAndy Whitcroftsub report_dump {
147613214adfSAndy Whitcroft	our @report;
1477f0a594c1SAndy Whitcroft}
1478000d1cc1SJoe Perches
1479de7d4f0eSAndy Whitcroftsub ERROR {
1480cbec18afSJoe Perches	my ($type, $msg) = @_;
1481cbec18afSJoe Perches
1482cbec18afSJoe Perches	if (report("ERROR", $type, $msg)) {
1483de7d4f0eSAndy Whitcroft		our $clean = 0;
14846c72ffaaSAndy Whitcroft		our $cnt_error++;
14853705ce5bSJoe Perches		return 1;
1486de7d4f0eSAndy Whitcroft	}
14873705ce5bSJoe Perches	return 0;
1488773647a0SAndy Whitcroft}
1489de7d4f0eSAndy Whitcroftsub WARN {
1490cbec18afSJoe Perches	my ($type, $msg) = @_;
1491cbec18afSJoe Perches
1492cbec18afSJoe Perches	if (report("WARNING", $type, $msg)) {
1493de7d4f0eSAndy Whitcroft		our $clean = 0;
14946c72ffaaSAndy Whitcroft		our $cnt_warn++;
14953705ce5bSJoe Perches		return 1;
1496de7d4f0eSAndy Whitcroft	}
14973705ce5bSJoe Perches	return 0;
1498773647a0SAndy Whitcroft}
1499de7d4f0eSAndy Whitcroftsub CHK {
1500cbec18afSJoe Perches	my ($type, $msg) = @_;
1501cbec18afSJoe Perches
1502cbec18afSJoe Perches	if ($check && report("CHECK", $type, $msg)) {
1503de7d4f0eSAndy Whitcroft		our $clean = 0;
15046c72ffaaSAndy Whitcroft		our $cnt_chk++;
15053705ce5bSJoe Perches		return 1;
15066c72ffaaSAndy Whitcroft	}
15073705ce5bSJoe Perches	return 0;
1508de7d4f0eSAndy Whitcroft}
1509de7d4f0eSAndy Whitcroft
15106ecd9674SAndy Whitcroftsub check_absolute_file {
15116ecd9674SAndy Whitcroft	my ($absolute, $herecurr) = @_;
15126ecd9674SAndy Whitcroft	my $file = $absolute;
15136ecd9674SAndy Whitcroft
15146ecd9674SAndy Whitcroft	##print "absolute<$absolute>\n";
15156ecd9674SAndy Whitcroft
15166ecd9674SAndy Whitcroft	# See if any suffix of this path is a path within the tree.
15176ecd9674SAndy Whitcroft	while ($file =~ s@^[^/]*/@@) {
15186ecd9674SAndy Whitcroft		if (-f "$root/$file") {
15196ecd9674SAndy Whitcroft			##print "file<$file>\n";
15206ecd9674SAndy Whitcroft			last;
15216ecd9674SAndy Whitcroft		}
15226ecd9674SAndy Whitcroft	}
15236ecd9674SAndy Whitcroft	if (! -f _)  {
15246ecd9674SAndy Whitcroft		return 0;
15256ecd9674SAndy Whitcroft	}
15266ecd9674SAndy Whitcroft
15276ecd9674SAndy Whitcroft	# It is, so see if the prefix is acceptable.
15286ecd9674SAndy Whitcroft	my $prefix = $absolute;
15296ecd9674SAndy Whitcroft	substr($prefix, -length($file)) = '';
15306ecd9674SAndy Whitcroft
15316ecd9674SAndy Whitcroft	##print "prefix<$prefix>\n";
15326ecd9674SAndy Whitcroft	if ($prefix ne ".../") {
1533000d1cc1SJoe Perches		WARN("USE_RELATIVE_PATH",
1534000d1cc1SJoe Perches		     "use relative pathname instead of absolute in changelog text\n" . $herecurr);
15356ecd9674SAndy Whitcroft	}
15366ecd9674SAndy Whitcroft}
15376ecd9674SAndy Whitcroft
15383705ce5bSJoe Perchessub trim {
15393705ce5bSJoe Perches	my ($string) = @_;
15403705ce5bSJoe Perches
1541b34c648bSJoe Perches	$string =~ s/^\s+|\s+$//g;
1542b34c648bSJoe Perches
1543b34c648bSJoe Perches	return $string;
1544b34c648bSJoe Perches}
1545b34c648bSJoe Perches
1546b34c648bSJoe Perchessub ltrim {
1547b34c648bSJoe Perches	my ($string) = @_;
1548b34c648bSJoe Perches
1549b34c648bSJoe Perches	$string =~ s/^\s+//;
1550b34c648bSJoe Perches
1551b34c648bSJoe Perches	return $string;
1552b34c648bSJoe Perches}
1553b34c648bSJoe Perches
1554b34c648bSJoe Perchessub rtrim {
1555b34c648bSJoe Perches	my ($string) = @_;
1556b34c648bSJoe Perches
1557b34c648bSJoe Perches	$string =~ s/\s+$//;
15583705ce5bSJoe Perches
15593705ce5bSJoe Perches	return $string;
15603705ce5bSJoe Perches}
15613705ce5bSJoe Perches
156252ea8506SJoe Perchessub string_find_replace {
156352ea8506SJoe Perches	my ($string, $find, $replace) = @_;
156452ea8506SJoe Perches
156552ea8506SJoe Perches	$string =~ s/$find/$replace/g;
156652ea8506SJoe Perches
156752ea8506SJoe Perches	return $string;
156852ea8506SJoe Perches}
156952ea8506SJoe Perches
15703705ce5bSJoe Perchessub tabify {
15713705ce5bSJoe Perches	my ($leading) = @_;
15723705ce5bSJoe Perches
15733705ce5bSJoe Perches	my $source_indent = 8;
15743705ce5bSJoe Perches	my $max_spaces_before_tab = $source_indent - 1;
15753705ce5bSJoe Perches	my $spaces_to_tab = " " x $source_indent;
15763705ce5bSJoe Perches
15773705ce5bSJoe Perches	#convert leading spaces to tabs
15783705ce5bSJoe Perches	1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
15793705ce5bSJoe Perches	#Remove spaces before a tab
15803705ce5bSJoe Perches	1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
15813705ce5bSJoe Perches
15823705ce5bSJoe Perches	return "$leading";
15833705ce5bSJoe Perches}
15843705ce5bSJoe Perches
1585d1fe9c09SJoe Perchessub pos_last_openparen {
1586d1fe9c09SJoe Perches	my ($line) = @_;
1587d1fe9c09SJoe Perches
1588d1fe9c09SJoe Perches	my $pos = 0;
1589d1fe9c09SJoe Perches
1590d1fe9c09SJoe Perches	my $opens = $line =~ tr/\(/\(/;
1591d1fe9c09SJoe Perches	my $closes = $line =~ tr/\)/\)/;
1592d1fe9c09SJoe Perches
1593d1fe9c09SJoe Perches	my $last_openparen = 0;
1594d1fe9c09SJoe Perches
1595d1fe9c09SJoe Perches	if (($opens == 0) || ($closes >= $opens)) {
1596d1fe9c09SJoe Perches		return -1;
1597d1fe9c09SJoe Perches	}
1598d1fe9c09SJoe Perches
1599d1fe9c09SJoe Perches	my $len = length($line);
1600d1fe9c09SJoe Perches
1601d1fe9c09SJoe Perches	for ($pos = 0; $pos < $len; $pos++) {
1602d1fe9c09SJoe Perches		my $string = substr($line, $pos);
1603d1fe9c09SJoe Perches		if ($string =~ /^($FuncArg|$balanced_parens)/) {
1604d1fe9c09SJoe Perches			$pos += length($1) - 1;
1605d1fe9c09SJoe Perches		} elsif (substr($line, $pos, 1) eq '(') {
1606d1fe9c09SJoe Perches			$last_openparen = $pos;
1607d1fe9c09SJoe Perches		} elsif (index($string, '(') == -1) {
1608d1fe9c09SJoe Perches			last;
1609d1fe9c09SJoe Perches		}
1610d1fe9c09SJoe Perches	}
1611d1fe9c09SJoe Perches
1612*91cb5195SJoe Perches	return length(expand_tabs(substr($line, 0, $last_openparen))) + 1;
1613d1fe9c09SJoe Perches}
1614d1fe9c09SJoe Perches
16150a920b5bSAndy Whitcroftsub process {
16160a920b5bSAndy Whitcroft	my $filename = shift;
16170a920b5bSAndy Whitcroft
16180a920b5bSAndy Whitcroft	my $linenr=0;
16190a920b5bSAndy Whitcroft	my $prevline="";
1620c2fdda0dSAndy Whitcroft	my $prevrawline="";
16210a920b5bSAndy Whitcroft	my $stashline="";
1622c2fdda0dSAndy Whitcroft	my $stashrawline="";
16230a920b5bSAndy Whitcroft
16244a0df2efSAndy Whitcroft	my $length;
16250a920b5bSAndy Whitcroft	my $indent;
16260a920b5bSAndy Whitcroft	my $previndent=0;
16270a920b5bSAndy Whitcroft	my $stashindent=0;
16280a920b5bSAndy Whitcroft
1629de7d4f0eSAndy Whitcroft	our $clean = 1;
16300a920b5bSAndy Whitcroft	my $signoff = 0;
16310a920b5bSAndy Whitcroft	my $is_patch = 0;
16320a920b5bSAndy Whitcroft
163315662b3eSJoe Perches	my $in_header_lines = 1;
163415662b3eSJoe Perches	my $in_commit_log = 0;		#Scanning lines before patch
163515662b3eSJoe Perches
1636fa64205dSPasi Savanainen	my $non_utf8_charset = 0;
1637fa64205dSPasi Savanainen
163813214adfSAndy Whitcroft	our @report = ();
16396c72ffaaSAndy Whitcroft	our $cnt_lines = 0;
16406c72ffaaSAndy Whitcroft	our $cnt_error = 0;
16416c72ffaaSAndy Whitcroft	our $cnt_warn = 0;
16426c72ffaaSAndy Whitcroft	our $cnt_chk = 0;
16436c72ffaaSAndy Whitcroft
16440a920b5bSAndy Whitcroft	# Trace the real file/line as we go.
16450a920b5bSAndy Whitcroft	my $realfile = '';
16460a920b5bSAndy Whitcroft	my $realline = 0;
16470a920b5bSAndy Whitcroft	my $realcnt = 0;
16480a920b5bSAndy Whitcroft	my $here = '';
16490a920b5bSAndy Whitcroft	my $in_comment = 0;
1650c2fdda0dSAndy Whitcroft	my $comment_edge = 0;
16510a920b5bSAndy Whitcroft	my $first_line = 0;
16521e855726SWolfram Sang	my $p1_prefix = '';
16530a920b5bSAndy Whitcroft
165413214adfSAndy Whitcroft	my $prev_values = 'E';
165513214adfSAndy Whitcroft
165613214adfSAndy Whitcroft	# suppression flags
1657773647a0SAndy Whitcroft	my %suppress_ifbraces;
1658170d3a22SAndy Whitcroft	my %suppress_whiletrailers;
16592b474a1aSAndy Whitcroft	my %suppress_export;
16603e469cdcSAndy Whitcroft	my $suppress_statement = 0;
1661653d4876SAndy Whitcroft
16627e51f197SJoe Perches	my %signatures = ();
1663323c1260SJoe Perches
1664c2fdda0dSAndy Whitcroft	# Pre-scan the patch sanitizing the lines.
1665de7d4f0eSAndy Whitcroft	# Pre-scan the patch looking for any __setup documentation.
1666c2fdda0dSAndy Whitcroft	#
1667de7d4f0eSAndy Whitcroft	my @setup_docs = ();
1668de7d4f0eSAndy Whitcroft	my $setup_docs = 0;
1669773647a0SAndy Whitcroft
1670d8b07710SJoe Perches	my $camelcase_file_seeded = 0;
1671d8b07710SJoe Perches
1672773647a0SAndy Whitcroft	sanitise_line_reset();
1673c2fdda0dSAndy Whitcroft	my $line;
1674c2fdda0dSAndy Whitcroft	foreach my $rawline (@rawlines) {
1675773647a0SAndy Whitcroft		$linenr++;
1676773647a0SAndy Whitcroft		$line = $rawline;
1677c2fdda0dSAndy Whitcroft
16783705ce5bSJoe Perches		push(@fixed, $rawline) if ($fix);
16793705ce5bSJoe Perches
1680773647a0SAndy Whitcroft		if ($rawline=~/^\+\+\+\s+(\S+)/) {
1681de7d4f0eSAndy Whitcroft			$setup_docs = 0;
1682de7d4f0eSAndy Whitcroft			if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
1683de7d4f0eSAndy Whitcroft				$setup_docs = 1;
1684de7d4f0eSAndy Whitcroft			}
1685773647a0SAndy Whitcroft			#next;
1686de7d4f0eSAndy Whitcroft		}
1687773647a0SAndy Whitcroft		if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1688773647a0SAndy Whitcroft			$realline=$1-1;
1689773647a0SAndy Whitcroft			if (defined $2) {
1690773647a0SAndy Whitcroft				$realcnt=$3+1;
1691773647a0SAndy Whitcroft			} else {
1692773647a0SAndy Whitcroft				$realcnt=1+1;
1693773647a0SAndy Whitcroft			}
1694c45dcabdSAndy Whitcroft			$in_comment = 0;
1695773647a0SAndy Whitcroft
1696773647a0SAndy Whitcroft			# Guestimate if this is a continuing comment.  Run
1697773647a0SAndy Whitcroft			# the context looking for a comment "edge".  If this
1698773647a0SAndy Whitcroft			# edge is a close comment then we must be in a comment
1699773647a0SAndy Whitcroft			# at context start.
1700773647a0SAndy Whitcroft			my $edge;
170101fa9147SAndy Whitcroft			my $cnt = $realcnt;
170201fa9147SAndy Whitcroft			for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
170301fa9147SAndy Whitcroft				next if (defined $rawlines[$ln - 1] &&
170401fa9147SAndy Whitcroft					 $rawlines[$ln - 1] =~ /^-/);
170501fa9147SAndy Whitcroft				$cnt--;
170601fa9147SAndy Whitcroft				#print "RAW<$rawlines[$ln - 1]>\n";
1707721c1cb6SAndy Whitcroft				last if (!defined $rawlines[$ln - 1]);
1708fae17daeSAndy Whitcroft				if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
1709fae17daeSAndy Whitcroft				    $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
1710fae17daeSAndy Whitcroft					($edge) = $1;
1711fae17daeSAndy Whitcroft					last;
1712fae17daeSAndy Whitcroft				}
1713773647a0SAndy Whitcroft			}
1714773647a0SAndy Whitcroft			if (defined $edge && $edge eq '*/') {
1715773647a0SAndy Whitcroft				$in_comment = 1;
1716773647a0SAndy Whitcroft			}
1717773647a0SAndy Whitcroft
1718773647a0SAndy Whitcroft			# Guestimate if this is a continuing comment.  If this
1719773647a0SAndy Whitcroft			# is the start of a diff block and this line starts
1720773647a0SAndy Whitcroft			# ' *' then it is very likely a comment.
1721773647a0SAndy Whitcroft			if (!defined $edge &&
172283242e0cSAndy Whitcroft			    $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
1723773647a0SAndy Whitcroft			{
1724773647a0SAndy Whitcroft				$in_comment = 1;
1725773647a0SAndy Whitcroft			}
1726773647a0SAndy Whitcroft
1727773647a0SAndy Whitcroft			##print "COMMENT:$in_comment edge<$edge> $rawline\n";
1728773647a0SAndy Whitcroft			sanitise_line_reset($in_comment);
1729773647a0SAndy Whitcroft
1730171ae1a4SAndy Whitcroft		} elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
1731773647a0SAndy Whitcroft			# Standardise the strings and chars within the input to
1732171ae1a4SAndy Whitcroft			# simplify matching -- only bother with positive lines.
1733773647a0SAndy Whitcroft			$line = sanitise_line($rawline);
1734773647a0SAndy Whitcroft		}
1735773647a0SAndy Whitcroft		push(@lines, $line);
1736773647a0SAndy Whitcroft
1737773647a0SAndy Whitcroft		if ($realcnt > 1) {
1738773647a0SAndy Whitcroft			$realcnt-- if ($line =~ /^(?:\+| |$)/);
1739773647a0SAndy Whitcroft		} else {
1740773647a0SAndy Whitcroft			$realcnt = 0;
1741773647a0SAndy Whitcroft		}
1742773647a0SAndy Whitcroft
1743773647a0SAndy Whitcroft		#print "==>$rawline\n";
1744773647a0SAndy Whitcroft		#print "-->$line\n";
1745de7d4f0eSAndy Whitcroft
1746de7d4f0eSAndy Whitcroft		if ($setup_docs && $line =~ /^\+/) {
1747de7d4f0eSAndy Whitcroft			push(@setup_docs, $line);
1748de7d4f0eSAndy Whitcroft		}
1749de7d4f0eSAndy Whitcroft	}
1750de7d4f0eSAndy Whitcroft
17516c72ffaaSAndy Whitcroft	$prefix = '';
17526c72ffaaSAndy Whitcroft
1753773647a0SAndy Whitcroft	$realcnt = 0;
1754773647a0SAndy Whitcroft	$linenr = 0;
17550a920b5bSAndy Whitcroft	foreach my $line (@lines) {
17560a920b5bSAndy Whitcroft		$linenr++;
17571b5539b1SJoe Perches		my $sline = $line;	#copy of $line
17581b5539b1SJoe Perches		$sline =~ s/$;/ /g;	#with comments as spaces
17590a920b5bSAndy Whitcroft
1760c2fdda0dSAndy Whitcroft		my $rawline = $rawlines[$linenr - 1];
17616c72ffaaSAndy Whitcroft
17620a920b5bSAndy Whitcroft#extract the line range in the file after the patch is applied
17636c72ffaaSAndy Whitcroft		if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
17640a920b5bSAndy Whitcroft			$is_patch = 1;
17654a0df2efSAndy Whitcroft			$first_line = $linenr + 1;
17660a920b5bSAndy Whitcroft			$realline=$1-1;
17670a920b5bSAndy Whitcroft			if (defined $2) {
17680a920b5bSAndy Whitcroft				$realcnt=$3+1;
17690a920b5bSAndy Whitcroft			} else {
17700a920b5bSAndy Whitcroft				$realcnt=1+1;
17710a920b5bSAndy Whitcroft			}
1772c2fdda0dSAndy Whitcroft			annotate_reset();
177313214adfSAndy Whitcroft			$prev_values = 'E';
177413214adfSAndy Whitcroft
1775773647a0SAndy Whitcroft			%suppress_ifbraces = ();
1776170d3a22SAndy Whitcroft			%suppress_whiletrailers = ();
17772b474a1aSAndy Whitcroft			%suppress_export = ();
17783e469cdcSAndy Whitcroft			$suppress_statement = 0;
17790a920b5bSAndy Whitcroft			next;
17800a920b5bSAndy Whitcroft
17814a0df2efSAndy Whitcroft# track the line number as we move through the hunk, note that
17824a0df2efSAndy Whitcroft# new versions of GNU diff omit the leading space on completely
17834a0df2efSAndy Whitcroft# blank context lines so we need to count that too.
1784773647a0SAndy Whitcroft		} elsif ($line =~ /^( |\+|$)/) {
17850a920b5bSAndy Whitcroft			$realline++;
1786d8aaf121SAndy Whitcroft			$realcnt-- if ($realcnt != 0);
17870a920b5bSAndy Whitcroft
17884a0df2efSAndy Whitcroft			# Measure the line length and indent.
1789c2fdda0dSAndy Whitcroft			($length, $indent) = line_stats($rawline);
17900a920b5bSAndy Whitcroft
17910a920b5bSAndy Whitcroft			# Track the previous line.
17920a920b5bSAndy Whitcroft			($prevline, $stashline) = ($stashline, $line);
17930a920b5bSAndy Whitcroft			($previndent, $stashindent) = ($stashindent, $indent);
1794c2fdda0dSAndy Whitcroft			($prevrawline, $stashrawline) = ($stashrawline, $rawline);
1795c2fdda0dSAndy Whitcroft
1796773647a0SAndy Whitcroft			#warn "line<$line>\n";
17976c72ffaaSAndy Whitcroft
1798d8aaf121SAndy Whitcroft		} elsif ($realcnt == 1) {
1799d8aaf121SAndy Whitcroft			$realcnt--;
18000a920b5bSAndy Whitcroft		}
18010a920b5bSAndy Whitcroft
1802cc77cdcaSAndy Whitcroft		my $hunk_line = ($realcnt != 0);
1803cc77cdcaSAndy Whitcroft
18040a920b5bSAndy Whitcroft#make up the handle for any error we report on this line
1805773647a0SAndy Whitcroft		$prefix = "$filename:$realline: " if ($emacs && $file);
1806773647a0SAndy Whitcroft		$prefix = "$filename:$linenr: " if ($emacs && !$file);
1807773647a0SAndy Whitcroft
18086c72ffaaSAndy Whitcroft		$here = "#$linenr: " if (!$file);
18096c72ffaaSAndy Whitcroft		$here = "#$realline: " if ($file);
1810773647a0SAndy Whitcroft
1811773647a0SAndy Whitcroft		# extract the filename as it passes
18123bf9a009SRabin Vincent		if ($line =~ /^diff --git.*?(\S+)$/) {
18133bf9a009SRabin Vincent			$realfile = $1;
18142b7ab453SJoe Perches			$realfile =~ s@^([^/]*)/@@ if (!$file);
1815270c49a0SJoe Perches			$in_commit_log = 0;
18163bf9a009SRabin Vincent		} elsif ($line =~ /^\+\+\+\s+(\S+)/) {
1817773647a0SAndy Whitcroft			$realfile = $1;
18182b7ab453SJoe Perches			$realfile =~ s@^([^/]*)/@@ if (!$file);
1819270c49a0SJoe Perches			$in_commit_log = 0;
18201e855726SWolfram Sang
18211e855726SWolfram Sang			$p1_prefix = $1;
1822e2f7aa4bSAndy Whitcroft			if (!$file && $tree && $p1_prefix ne '' &&
1823e2f7aa4bSAndy Whitcroft			    -e "$root/$p1_prefix") {
1824000d1cc1SJoe Perches				WARN("PATCH_PREFIX",
1825000d1cc1SJoe Perches				     "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
18261e855726SWolfram Sang			}
1827773647a0SAndy Whitcroft
1828c1ab3326SAndy Whitcroft			if ($realfile =~ m@^include/asm/@) {
1829000d1cc1SJoe Perches				ERROR("MODIFIED_INCLUDE_ASM",
1830000d1cc1SJoe Perches				      "do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
1831773647a0SAndy Whitcroft			}
1832773647a0SAndy Whitcroft			next;
1833773647a0SAndy Whitcroft		}
1834773647a0SAndy Whitcroft
1835389834b6SRandy Dunlap		$here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
18360a920b5bSAndy Whitcroft
1837c2fdda0dSAndy Whitcroft		my $hereline = "$here\n$rawline\n";
1838c2fdda0dSAndy Whitcroft		my $herecurr = "$here\n$rawline\n";
1839c2fdda0dSAndy Whitcroft		my $hereprev = "$here\n$prevrawline\n$rawline\n";
18400a920b5bSAndy Whitcroft
18416c72ffaaSAndy Whitcroft		$cnt_lines++ if ($realcnt != 0);
18426c72ffaaSAndy Whitcroft
18433bf9a009SRabin Vincent# Check for incorrect file permissions
18443bf9a009SRabin Vincent		if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
18453bf9a009SRabin Vincent			my $permhere = $here . "FILE: $realfile\n";
184604db4d25SJoe Perches			if ($realfile !~ m@scripts/@ &&
184704db4d25SJoe Perches			    $realfile !~ /\.(py|pl|awk|sh)$/) {
1848000d1cc1SJoe Perches				ERROR("EXECUTE_PERMISSIONS",
1849000d1cc1SJoe Perches				      "do not set execute permissions for source files\n" . $permhere);
18503bf9a009SRabin Vincent			}
18513bf9a009SRabin Vincent		}
18523bf9a009SRabin Vincent
185320112475SJoe Perches# Check the patch for a signoff:
1854d8aaf121SAndy Whitcroft		if ($line =~ /^\s*signed-off-by:/i) {
18554a0df2efSAndy Whitcroft			$signoff++;
185615662b3eSJoe Perches			$in_commit_log = 0;
18570a920b5bSAndy Whitcroft		}
185820112475SJoe Perches
185920112475SJoe Perches# Check signature styles
1860270c49a0SJoe Perches		if (!$in_header_lines &&
1861ce0338dfSJoe Perches		    $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
186220112475SJoe Perches			my $space_before = $1;
186320112475SJoe Perches			my $sign_off = $2;
186420112475SJoe Perches			my $space_after = $3;
186520112475SJoe Perches			my $email = $4;
186620112475SJoe Perches			my $ucfirst_sign_off = ucfirst(lc($sign_off));
186720112475SJoe Perches
1868ce0338dfSJoe Perches			if ($sign_off !~ /$signature_tags/) {
1869ce0338dfSJoe Perches				WARN("BAD_SIGN_OFF",
1870ce0338dfSJoe Perches				     "Non-standard signature: $sign_off\n" . $herecurr);
1871ce0338dfSJoe Perches			}
187220112475SJoe Perches			if (defined $space_before && $space_before ne "") {
18733705ce5bSJoe Perches				if (WARN("BAD_SIGN_OFF",
18743705ce5bSJoe Perches					 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
18753705ce5bSJoe Perches				    $fix) {
18763705ce5bSJoe Perches					$fixed[$linenr - 1] =
18773705ce5bSJoe Perches					    "$ucfirst_sign_off $email";
18783705ce5bSJoe Perches				}
187920112475SJoe Perches			}
188020112475SJoe Perches			if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
18813705ce5bSJoe Perches				if (WARN("BAD_SIGN_OFF",
18823705ce5bSJoe Perches					 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
18833705ce5bSJoe Perches				    $fix) {
18843705ce5bSJoe Perches					$fixed[$linenr - 1] =
18853705ce5bSJoe Perches					    "$ucfirst_sign_off $email";
18863705ce5bSJoe Perches				}
18873705ce5bSJoe Perches
188820112475SJoe Perches			}
188920112475SJoe Perches			if (!defined $space_after || $space_after ne " ") {
18903705ce5bSJoe Perches				if (WARN("BAD_SIGN_OFF",
18913705ce5bSJoe Perches					 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
18923705ce5bSJoe Perches				    $fix) {
18933705ce5bSJoe Perches					$fixed[$linenr - 1] =
18943705ce5bSJoe Perches					    "$ucfirst_sign_off $email";
18953705ce5bSJoe Perches				}
189620112475SJoe Perches			}
189720112475SJoe Perches
189820112475SJoe Perches			my ($email_name, $email_address, $comment) = parse_email($email);
189920112475SJoe Perches			my $suggested_email = format_email(($email_name, $email_address));
190020112475SJoe Perches			if ($suggested_email eq "") {
1901000d1cc1SJoe Perches				ERROR("BAD_SIGN_OFF",
1902000d1cc1SJoe Perches				      "Unrecognized email address: '$email'\n" . $herecurr);
190320112475SJoe Perches			} else {
190420112475SJoe Perches				my $dequoted = $suggested_email;
190520112475SJoe Perches				$dequoted =~ s/^"//;
190620112475SJoe Perches				$dequoted =~ s/" </ </;
190720112475SJoe Perches				# Don't force email to have quotes
190820112475SJoe Perches				# Allow just an angle bracketed address
190920112475SJoe Perches				if ("$dequoted$comment" ne $email &&
191020112475SJoe Perches				    "<$email_address>$comment" ne $email &&
191120112475SJoe Perches				    "$suggested_email$comment" ne $email) {
1912000d1cc1SJoe Perches					WARN("BAD_SIGN_OFF",
1913000d1cc1SJoe Perches					     "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
191420112475SJoe Perches				}
19150a920b5bSAndy Whitcroft			}
19167e51f197SJoe Perches
19177e51f197SJoe Perches# Check for duplicate signatures
19187e51f197SJoe Perches			my $sig_nospace = $line;
19197e51f197SJoe Perches			$sig_nospace =~ s/\s//g;
19207e51f197SJoe Perches			$sig_nospace = lc($sig_nospace);
19217e51f197SJoe Perches			if (defined $signatures{$sig_nospace}) {
19227e51f197SJoe Perches				WARN("BAD_SIGN_OFF",
19237e51f197SJoe Perches				     "Duplicate signature\n" . $herecurr);
19247e51f197SJoe Perches			} else {
19257e51f197SJoe Perches				$signatures{$sig_nospace} = 1;
19267e51f197SJoe Perches			}
19270a920b5bSAndy Whitcroft		}
19280a920b5bSAndy Whitcroft
19297ebd05efSChristopher Covington# Check for unwanted Gerrit info
19307ebd05efSChristopher Covington		if ($in_commit_log && $line =~ /^\s*change-id:/i) {
19317ebd05efSChristopher Covington			ERROR("GERRIT_CHANGE_ID",
19327ebd05efSChristopher Covington			      "Remove Gerrit Change-Id's before submitting upstream.\n" . $herecurr);
19337ebd05efSChristopher Covington		}
19347ebd05efSChristopher Covington
193500df344fSAndy Whitcroft# Check for wrappage within a valid hunk of the file
19368905a67cSAndy Whitcroft		if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
1937000d1cc1SJoe Perches			ERROR("CORRUPTED_PATCH",
1938000d1cc1SJoe Perches			      "patch seems to be corrupt (line wrapped?)\n" .
19396c72ffaaSAndy Whitcroft				$herecurr) if (!$emitted_corrupt++);
1940de7d4f0eSAndy Whitcroft		}
1941de7d4f0eSAndy Whitcroft
19426ecd9674SAndy Whitcroft# Check for absolute kernel paths.
19436ecd9674SAndy Whitcroft		if ($tree) {
19446ecd9674SAndy Whitcroft			while ($line =~ m{(?:^|\s)(/\S*)}g) {
19456ecd9674SAndy Whitcroft				my $file = $1;
19466ecd9674SAndy Whitcroft
19476ecd9674SAndy Whitcroft				if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
19486ecd9674SAndy Whitcroft				    check_absolute_file($1, $herecurr)) {
19496ecd9674SAndy Whitcroft					#
19506ecd9674SAndy Whitcroft				} else {
19516ecd9674SAndy Whitcroft					check_absolute_file($file, $herecurr);
19526ecd9674SAndy Whitcroft				}
19536ecd9674SAndy Whitcroft			}
19546ecd9674SAndy Whitcroft		}
19556ecd9674SAndy Whitcroft
1956de7d4f0eSAndy Whitcroft# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
1957de7d4f0eSAndy Whitcroft		if (($realfile =~ /^$/ || $line =~ /^\+/) &&
1958171ae1a4SAndy Whitcroft		    $rawline !~ m/^$UTF8*$/) {
1959171ae1a4SAndy Whitcroft			my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
1960171ae1a4SAndy Whitcroft
1961171ae1a4SAndy Whitcroft			my $blank = copy_spacing($rawline);
1962171ae1a4SAndy Whitcroft			my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
1963171ae1a4SAndy Whitcroft			my $hereptr = "$hereline$ptr\n";
1964171ae1a4SAndy Whitcroft
196534d99219SJoe Perches			CHK("INVALID_UTF8",
1966000d1cc1SJoe Perches			    "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
196700df344fSAndy Whitcroft		}
19680a920b5bSAndy Whitcroft
196915662b3eSJoe Perches# Check if it's the start of a commit log
197015662b3eSJoe Perches# (not a header line and we haven't seen the patch filename)
197115662b3eSJoe Perches		if ($in_header_lines && $realfile =~ /^$/ &&
1972270c49a0SJoe Perches		    $rawline !~ /^(commit\b|from\b|[\w-]+:).+$/i) {
197315662b3eSJoe Perches			$in_header_lines = 0;
197415662b3eSJoe Perches			$in_commit_log = 1;
197515662b3eSJoe Perches		}
197615662b3eSJoe Perches
1977fa64205dSPasi Savanainen# Check if there is UTF-8 in a commit log when a mail header has explicitly
1978fa64205dSPasi Savanainen# declined it, i.e defined some charset where it is missing.
1979fa64205dSPasi Savanainen		if ($in_header_lines &&
1980fa64205dSPasi Savanainen		    $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
1981fa64205dSPasi Savanainen		    $1 !~ /utf-8/i) {
1982fa64205dSPasi Savanainen			$non_utf8_charset = 1;
1983fa64205dSPasi Savanainen		}
1984fa64205dSPasi Savanainen
1985fa64205dSPasi Savanainen		if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
198615662b3eSJoe Perches		    $rawline =~ /$NON_ASCII_UTF8/) {
1987fa64205dSPasi Savanainen			WARN("UTF8_BEFORE_PATCH",
198815662b3eSJoe Perches			    "8-bit UTF-8 used in possible commit log\n" . $herecurr);
198915662b3eSJoe Perches		}
199015662b3eSJoe Perches
199130670854SAndy Whitcroft# ignore non-hunk lines and lines being removed
199230670854SAndy Whitcroft		next if (!$hunk_line || $line =~ /^-/);
199300df344fSAndy Whitcroft
19940a920b5bSAndy Whitcroft#trailing whitespace
19959c0ca6f9SAndy Whitcroft		if ($line =~ /^\+.*\015/) {
1996c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1997d5e616fcSJoe Perches			if (ERROR("DOS_LINE_ENDINGS",
1998d5e616fcSJoe Perches				  "DOS line endings\n" . $herevet) &&
1999d5e616fcSJoe Perches			    $fix) {
2000d5e616fcSJoe Perches				$fixed[$linenr - 1] =~ s/[\s\015]+$//;
2001d5e616fcSJoe Perches			}
2002c2fdda0dSAndy Whitcroft		} elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
2003c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
20043705ce5bSJoe Perches			if (ERROR("TRAILING_WHITESPACE",
20053705ce5bSJoe Perches				  "trailing whitespace\n" . $herevet) &&
20063705ce5bSJoe Perches			    $fix) {
2007d5e616fcSJoe Perches				$fixed[$linenr - 1] =~ s/\s+$//;
20083705ce5bSJoe Perches			}
20093705ce5bSJoe Perches
2010d2c0a235SAndy Whitcroft			$rpt_cleaners = 1;
20110a920b5bSAndy Whitcroft		}
20125368df20SAndy Whitcroft
20134783f894SJosh Triplett# Check for FSF mailing addresses.
2014109d8cb2SAlexander Duyck		if ($rawline =~ /\bwrite to the Free/i ||
20153e2232f2SJoe Perches		    $rawline =~ /\b59\s+Temple\s+Pl/i ||
20163e2232f2SJoe Perches		    $rawline =~ /\b51\s+Franklin\s+St/i) {
20174783f894SJosh Triplett			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
20184783f894SJosh Triplett			my $msg_type = \&ERROR;
20194783f894SJosh Triplett			$msg_type = \&CHK if ($file);
20204783f894SJosh Triplett			&{$msg_type}("FSF_MAILING_ADDRESS",
20214783f894SJosh Triplett				     "Do not include the paragraph about writing to the Free Software Foundation's mailing address from the sample GPL notice. The FSF has changed addresses in the past, and may do so again. Linux already includes a copy of the GPL.\n" . $herevet)
20224783f894SJosh Triplett		}
20234783f894SJosh Triplett
20243354957aSAndi Kleen# check for Kconfig help text having a real description
20259fe287d7SAndy Whitcroft# Only applies when adding the entry originally, after that we do not have
20269fe287d7SAndy Whitcroft# sufficient context to determine whether it is indeed long enough.
20273354957aSAndi Kleen		if ($realfile =~ /Kconfig/ &&
2028a1385803SAndy Whitcroft		    $line =~ /.\s*config\s+/) {
20293354957aSAndi Kleen			my $length = 0;
20309fe287d7SAndy Whitcroft			my $cnt = $realcnt;
20319fe287d7SAndy Whitcroft			my $ln = $linenr + 1;
20329fe287d7SAndy Whitcroft			my $f;
2033a1385803SAndy Whitcroft			my $is_start = 0;
20349fe287d7SAndy Whitcroft			my $is_end = 0;
2035a1385803SAndy Whitcroft			for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
20369fe287d7SAndy Whitcroft				$f = $lines[$ln - 1];
20379fe287d7SAndy Whitcroft				$cnt-- if ($lines[$ln - 1] !~ /^-/);
20389fe287d7SAndy Whitcroft				$is_end = $lines[$ln - 1] =~ /^\+/;
20399fe287d7SAndy Whitcroft
20409fe287d7SAndy Whitcroft				next if ($f =~ /^-/);
2041a1385803SAndy Whitcroft
2042a1385803SAndy Whitcroft				if ($lines[$ln - 1] =~ /.\s*(?:bool|tristate)\s*\"/) {
2043a1385803SAndy Whitcroft					$is_start = 1;
2044a1385803SAndy Whitcroft				} elsif ($lines[$ln - 1] =~ /.\s*(?:---)?help(?:---)?$/) {
2045a1385803SAndy Whitcroft					$length = -1;
2046a1385803SAndy Whitcroft				}
2047a1385803SAndy Whitcroft
20489fe287d7SAndy Whitcroft				$f =~ s/^.//;
20493354957aSAndi Kleen				$f =~ s/#.*//;
20503354957aSAndi Kleen				$f =~ s/^\s+//;
20513354957aSAndi Kleen				next if ($f =~ /^$/);
20529fe287d7SAndy Whitcroft				if ($f =~ /^\s*config\s/) {
20539fe287d7SAndy Whitcroft					$is_end = 1;
20549fe287d7SAndy Whitcroft					last;
20559fe287d7SAndy Whitcroft				}
20563354957aSAndi Kleen				$length++;
20573354957aSAndi Kleen			}
2058000d1cc1SJoe Perches			WARN("CONFIG_DESCRIPTION",
2059a1385803SAndy Whitcroft			     "please write a paragraph that describes the config symbol fully\n" . $herecurr) if ($is_start && $is_end && $length < 4);
2060a1385803SAndy Whitcroft			#print "is_start<$is_start> is_end<$is_end> length<$length>\n";
20613354957aSAndi Kleen		}
20623354957aSAndi Kleen
20631ba8dfd1SKees Cook# discourage the addition of CONFIG_EXPERIMENTAL in Kconfig.
20641ba8dfd1SKees Cook		if ($realfile =~ /Kconfig/ &&
20651ba8dfd1SKees Cook		    $line =~ /.\s*depends on\s+.*\bEXPERIMENTAL\b/) {
20661ba8dfd1SKees Cook			WARN("CONFIG_EXPERIMENTAL",
20671ba8dfd1SKees Cook			     "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
20681ba8dfd1SKees Cook		}
20691ba8dfd1SKees Cook
2070c68e5878SArnaud Lacombe		if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
2071c68e5878SArnaud Lacombe		    ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
2072c68e5878SArnaud Lacombe			my $flag = $1;
2073c68e5878SArnaud Lacombe			my $replacement = {
2074c68e5878SArnaud Lacombe				'EXTRA_AFLAGS' =>   'asflags-y',
2075c68e5878SArnaud Lacombe				'EXTRA_CFLAGS' =>   'ccflags-y',
2076c68e5878SArnaud Lacombe				'EXTRA_CPPFLAGS' => 'cppflags-y',
2077c68e5878SArnaud Lacombe				'EXTRA_LDFLAGS' =>  'ldflags-y',
2078c68e5878SArnaud Lacombe			};
2079c68e5878SArnaud Lacombe
2080c68e5878SArnaud Lacombe			WARN("DEPRECATED_VARIABLE",
2081c68e5878SArnaud Lacombe			     "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
2082c68e5878SArnaud Lacombe		}
2083c68e5878SArnaud Lacombe
2084bff5da43SRob Herring# check for DT compatible documentation
20857dd05b38SFlorian Vaussard		if (defined $root &&
20867dd05b38SFlorian Vaussard			(($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) ||
20877dd05b38SFlorian Vaussard			 ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) {
20887dd05b38SFlorian Vaussard
2089bff5da43SRob Herring			my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g;
2090bff5da43SRob Herring
2091cc93319bSFlorian Vaussard			my $dt_path = $root . "/Documentation/devicetree/bindings/";
2092cc93319bSFlorian Vaussard			my $vp_file = $dt_path . "vendor-prefixes.txt";
2093cc93319bSFlorian Vaussard
2094bff5da43SRob Herring			foreach my $compat (@compats) {
2095bff5da43SRob Herring				my $compat2 = $compat;
2096bff5da43SRob Herring				$compat2 =~ s/\,[a-z]*\-/\,<\.\*>\-/;
2097bff5da43SRob Herring				`grep -Erq "$compat|$compat2" $dt_path`;
2098bff5da43SRob Herring				if ( $? >> 8 ) {
2099bff5da43SRob Herring					WARN("UNDOCUMENTED_DT_STRING",
2100bff5da43SRob Herring					     "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr);
2101bff5da43SRob Herring				}
2102bff5da43SRob Herring
21034fbf32a6SFlorian Vaussard				next if $compat !~ /^([a-zA-Z0-9\-]+)\,/;
21044fbf32a6SFlorian Vaussard				my $vendor = $1;
2105cc93319bSFlorian Vaussard				`grep -Eq "^$vendor\\b" $vp_file`;
2106bff5da43SRob Herring				if ( $? >> 8 ) {
2107bff5da43SRob Herring					WARN("UNDOCUMENTED_DT_STRING",
2108cc93319bSFlorian Vaussard					     "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr);
2109bff5da43SRob Herring				}
2110bff5da43SRob Herring			}
2111bff5da43SRob Herring		}
2112bff5da43SRob Herring
21135368df20SAndy Whitcroft# check we are in a valid source file if not then ignore this hunk
21145368df20SAndy Whitcroft		next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
21155368df20SAndy Whitcroft
21166cd7f386SJoe Perches#line length limit
2117c45dcabdSAndy Whitcroft		if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ &&
2118f4c014c0SAndy Whitcroft		    $rawline !~ /^.\s*\*\s*\@$Ident\s/ &&
21190fccc622SJoe Perches		    !($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(KERN_\S+\s*|[^"]*))?"[X\t]*"\s*(?:|,|\)\s*;)\s*$/ ||
21208bbea968SJoe Perches		    $line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) &&
21216cd7f386SJoe Perches		    $length > $max_line_length)
2122c45dcabdSAndy Whitcroft		{
2123000d1cc1SJoe Perches			WARN("LONG_LINE",
21246cd7f386SJoe Perches			     "line over $max_line_length characters\n" . $herecurr);
21250a920b5bSAndy Whitcroft		}
21260a920b5bSAndy Whitcroft
2127ca56dc09SJosh Triplett# Check for user-visible strings broken across lines, which breaks the ability
21288c5fcd24SJoe Perches# to grep for the string.  Make exceptions when the previous string ends in a
21298c5fcd24SJoe Perches# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{'
21308c5fcd24SJoe Perches# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value
2131ca56dc09SJosh Triplett		if ($line =~ /^\+\s*"/ &&
2132ca56dc09SJosh Triplett		    $prevline =~ /"\s*$/ &&
21338c5fcd24SJoe Perches		    $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) {
2134ca56dc09SJosh Triplett			WARN("SPLIT_STRING",
2135ca56dc09SJosh Triplett			     "quoted string split across lines\n" . $hereprev);
2136ca56dc09SJosh Triplett		}
2137ca56dc09SJosh Triplett
21385e79d96eSJoe Perches# check for spaces before a quoted newline
21395e79d96eSJoe Perches		if ($rawline =~ /^.*\".*\s\\n/) {
21403705ce5bSJoe Perches			if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
21413705ce5bSJoe Perches				 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
21423705ce5bSJoe Perches			    $fix) {
21433705ce5bSJoe Perches				$fixed[$linenr - 1] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
21443705ce5bSJoe Perches			}
21453705ce5bSJoe Perches
21465e79d96eSJoe Perches		}
21475e79d96eSJoe Perches
21488905a67cSAndy Whitcroft# check for adding lines without a newline.
21498905a67cSAndy Whitcroft		if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
2150000d1cc1SJoe Perches			WARN("MISSING_EOF_NEWLINE",
2151000d1cc1SJoe Perches			     "adding a line without newline at end of file\n" . $herecurr);
21528905a67cSAndy Whitcroft		}
21538905a67cSAndy Whitcroft
215442e41c54SMike Frysinger# Blackfin: use hi/lo macros
215542e41c54SMike Frysinger		if ($realfile =~ m@arch/blackfin/.*\.S$@) {
215642e41c54SMike Frysinger			if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
215742e41c54SMike Frysinger				my $herevet = "$here\n" . cat_vet($line) . "\n";
2158000d1cc1SJoe Perches				ERROR("LO_MACRO",
2159000d1cc1SJoe Perches				      "use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
216042e41c54SMike Frysinger			}
216142e41c54SMike Frysinger			if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
216242e41c54SMike Frysinger				my $herevet = "$here\n" . cat_vet($line) . "\n";
2163000d1cc1SJoe Perches				ERROR("HI_MACRO",
2164000d1cc1SJoe Perches				      "use the HI() macro, not (... >> 16)\n" . $herevet);
216542e41c54SMike Frysinger			}
216642e41c54SMike Frysinger		}
216742e41c54SMike Frysinger
2168b9ea10d6SAndy Whitcroft# check we are in a valid source file C or perl if not then ignore this hunk
2169b9ea10d6SAndy Whitcroft		next if ($realfile !~ /\.(h|c|pl)$/);
21700a920b5bSAndy Whitcroft
21710a920b5bSAndy Whitcroft# at the beginning of a line any tabs must come first and anything
21720a920b5bSAndy Whitcroft# more than 8 must use tabs.
2173c2fdda0dSAndy Whitcroft		if ($rawline =~ /^\+\s* \t\s*\S/ ||
2174c2fdda0dSAndy Whitcroft		    $rawline =~ /^\+\s*        \s*/) {
2175c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2176d2c0a235SAndy Whitcroft			$rpt_cleaners = 1;
21773705ce5bSJoe Perches			if (ERROR("CODE_INDENT",
21783705ce5bSJoe Perches				  "code indent should use tabs where possible\n" . $herevet) &&
21793705ce5bSJoe Perches			    $fix) {
21803705ce5bSJoe Perches				$fixed[$linenr - 1] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
21813705ce5bSJoe Perches			}
21820a920b5bSAndy Whitcroft		}
21830a920b5bSAndy Whitcroft
218408e44365SAlberto Panizzo# check for space before tabs.
218508e44365SAlberto Panizzo		if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
218608e44365SAlberto Panizzo			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
21873705ce5bSJoe Perches			if (WARN("SPACE_BEFORE_TAB",
21883705ce5bSJoe Perches				"please, no space before tabs\n" . $herevet) &&
21893705ce5bSJoe Perches			    $fix) {
2190c76f4cb3SJoe Perches				while ($fixed[$linenr - 1] =~
2191c76f4cb3SJoe Perches					   s/(^\+.*) {8,8}+\t/$1\t\t/) {}
2192c76f4cb3SJoe Perches				while ($fixed[$linenr - 1] =~
2193c76f4cb3SJoe Perches					   s/(^\+.*) +\t/$1\t/) {}
21943705ce5bSJoe Perches			}
219508e44365SAlberto Panizzo		}
219608e44365SAlberto Panizzo
2197d1fe9c09SJoe Perches# check for && or || at the start of a line
2198d1fe9c09SJoe Perches		if ($rawline =~ /^\+\s*(&&|\|\|)/) {
2199d1fe9c09SJoe Perches			CHK("LOGICAL_CONTINUATIONS",
2200d1fe9c09SJoe Perches			    "Logical continuations should be on the previous line\n" . $hereprev);
2201d1fe9c09SJoe Perches		}
2202d1fe9c09SJoe Perches
2203d1fe9c09SJoe Perches# check multi-line statement indentation matches previous line
2204d1fe9c09SJoe Perches		if ($^V && $^V ge 5.10.0 &&
2205*91cb5195SJoe Perches		    $prevline =~ /^\+([ \t]*)((?:$c90_Keywords(?:\s+if)\s*)|(?:$Declare\s*)?(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*|$Ident\s*=\s*$Ident\s*)\(.*(\&\&|\|\||,)\s*$/) {
2206d1fe9c09SJoe Perches			$prevline =~ /^\+(\t*)(.*)$/;
2207d1fe9c09SJoe Perches			my $oldindent = $1;
2208d1fe9c09SJoe Perches			my $rest = $2;
2209d1fe9c09SJoe Perches
2210d1fe9c09SJoe Perches			my $pos = pos_last_openparen($rest);
2211d1fe9c09SJoe Perches			if ($pos >= 0) {
2212b34a26f3SJoe Perches				$line =~ /^(\+| )([ \t]*)/;
2213b34a26f3SJoe Perches				my $newindent = $2;
2214d1fe9c09SJoe Perches
2215d1fe9c09SJoe Perches				my $goodtabindent = $oldindent .
2216d1fe9c09SJoe Perches					"\t" x ($pos / 8) .
2217d1fe9c09SJoe Perches					" "  x ($pos % 8);
2218d1fe9c09SJoe Perches				my $goodspaceindent = $oldindent . " "  x $pos;
2219d1fe9c09SJoe Perches
2220d1fe9c09SJoe Perches				if ($newindent ne $goodtabindent &&
2221d1fe9c09SJoe Perches				    $newindent ne $goodspaceindent) {
22223705ce5bSJoe Perches
22233705ce5bSJoe Perches					if (CHK("PARENTHESIS_ALIGNMENT",
22243705ce5bSJoe Perches						"Alignment should match open parenthesis\n" . $hereprev) &&
22253705ce5bSJoe Perches					    $fix && $line =~ /^\+/) {
22263705ce5bSJoe Perches						$fixed[$linenr - 1] =~
22273705ce5bSJoe Perches						    s/^\+[ \t]*/\+$goodtabindent/;
22283705ce5bSJoe Perches					}
2229d1fe9c09SJoe Perches				}
2230d1fe9c09SJoe Perches			}
2231d1fe9c09SJoe Perches		}
2232d1fe9c09SJoe Perches
223323f780c9SJoe Perches		if ($line =~ /^\+.*\*[ \t]*\)[ \t]+(?!$Assignment|$Arithmetic)/) {
22343705ce5bSJoe Perches			if (CHK("SPACING",
22353705ce5bSJoe Perches				"No space is necessary after a cast\n" . $hereprev) &&
22363705ce5bSJoe Perches			    $fix) {
22373705ce5bSJoe Perches				$fixed[$linenr - 1] =~
22383705ce5bSJoe Perches				    s/^(\+.*\*[ \t]*\))[ \t]+/$1/;
22393705ce5bSJoe Perches			}
2240aad4f614SJoe Perches		}
2241aad4f614SJoe Perches
224205880600SJoe Perches		if ($realfile =~ m@^(drivers/net/|net/)@ &&
2243fdb4bcd6SJoe Perches		    $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
224485ad978cSJoe Perches		    $rawline =~ /^\+[ \t]*\*/ &&
224585ad978cSJoe Perches		    $realline > 2) {
224605880600SJoe Perches			WARN("NETWORKING_BLOCK_COMMENT_STYLE",
224705880600SJoe Perches			     "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
224805880600SJoe Perches		}
224905880600SJoe Perches
225005880600SJoe Perches		if ($realfile =~ m@^(drivers/net/|net/)@ &&
2251a605e32eSJoe Perches		    $prevrawline =~ /^\+[ \t]*\/\*/ &&		#starting /*
2252a605e32eSJoe Perches		    $prevrawline !~ /\*\/[ \t]*$/ &&		#no trailing */
225361135e96SJoe Perches		    $rawline =~ /^\+/ &&			#line is new
2254a605e32eSJoe Perches		    $rawline !~ /^\+[ \t]*\*/) {		#no leading *
2255a605e32eSJoe Perches			WARN("NETWORKING_BLOCK_COMMENT_STYLE",
2256a605e32eSJoe Perches			     "networking block comments start with * on subsequent lines\n" . $hereprev);
2257a605e32eSJoe Perches		}
2258a605e32eSJoe Perches
2259a605e32eSJoe Perches		if ($realfile =~ m@^(drivers/net/|net/)@ &&
2260c24f9f19SJoe Perches		    $rawline !~ m@^\+[ \t]*\*/[ \t]*$@ &&	#trailing */
2261c24f9f19SJoe Perches		    $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ &&	#inline /*...*/
2262c24f9f19SJoe Perches		    $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ &&	#trailing **/
2263c24f9f19SJoe Perches		    $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) {	#non blank */
226405880600SJoe Perches			WARN("NETWORKING_BLOCK_COMMENT_STYLE",
226505880600SJoe Perches			     "networking block comments put the trailing */ on a separate line\n" . $herecurr);
226605880600SJoe Perches		}
226705880600SJoe Perches
22683b617e3bSJoe Perches# check for missing blank lines after declarations
22693b617e3bSJoe Perches		if ($realfile =~ m@^(drivers/net/|net/)@ &&
22703b617e3bSJoe Perches		    $prevline =~ /^\+\s+$Declare\s+$Ident/ &&
22713b617e3bSJoe Perches		    !($prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ ||
22723b617e3bSJoe Perches		      $prevline =~ /(?:\{\s*|\\)$/) &&		#extended lines
22733b617e3bSJoe Perches		    $sline =~ /^\+\s+/ &&			#Not at char 1
22743b617e3bSJoe Perches		    !($sline =~ /^\+\s+$Declare/ ||
22753b617e3bSJoe Perches		      $sline =~ /^\+\s+$Ident\s+$Ident/ ||	#eg: typedef foo
22763b617e3bSJoe Perches		      $sline =~ /^\+\s+(?:union|struct|enum|typedef)\b/ ||
22773b617e3bSJoe Perches		      $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(])/ ||
22783b617e3bSJoe Perches		      $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/)) {
22793b617e3bSJoe Perches			WARN("SPACING",
22803b617e3bSJoe Perches			     "networking uses a blank line after declarations\n" . $hereprev);
22813b617e3bSJoe Perches		}
22823b617e3bSJoe Perches
22835f7ddae6SRaffaele Recalcati# check for spaces at the beginning of a line.
22846b4c5bebSAndy Whitcroft# Exceptions:
22856b4c5bebSAndy Whitcroft#  1) within comments
22866b4c5bebSAndy Whitcroft#  2) indented preprocessor commands
22876b4c5bebSAndy Whitcroft#  3) hanging labels
22883705ce5bSJoe Perches		if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/)  {
22895f7ddae6SRaffaele Recalcati			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
22903705ce5bSJoe Perches			if (WARN("LEADING_SPACE",
22913705ce5bSJoe Perches				 "please, no spaces at the start of a line\n" . $herevet) &&
22923705ce5bSJoe Perches			    $fix) {
22933705ce5bSJoe Perches				$fixed[$linenr - 1] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
22943705ce5bSJoe Perches			}
22955f7ddae6SRaffaele Recalcati		}
22965f7ddae6SRaffaele Recalcati
2297b9ea10d6SAndy Whitcroft# check we are in a valid C source file if not then ignore this hunk
2298b9ea10d6SAndy Whitcroft		next if ($realfile !~ /\.(h|c)$/);
2299b9ea10d6SAndy Whitcroft
23001ba8dfd1SKees Cook# discourage the addition of CONFIG_EXPERIMENTAL in #if(def).
23011ba8dfd1SKees Cook		if ($line =~ /^\+\s*\#\s*if.*\bCONFIG_EXPERIMENTAL\b/) {
23021ba8dfd1SKees Cook			WARN("CONFIG_EXPERIMENTAL",
23031ba8dfd1SKees Cook			     "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
23041ba8dfd1SKees Cook		}
23051ba8dfd1SKees Cook
2306c2fdda0dSAndy Whitcroft# check for RCS/CVS revision markers
2307cf655043SAndy Whitcroft		if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
2308000d1cc1SJoe Perches			WARN("CVS_KEYWORD",
2309000d1cc1SJoe Perches			     "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
2310c2fdda0dSAndy Whitcroft		}
231122f2a2efSAndy Whitcroft
231242e41c54SMike Frysinger# Blackfin: don't use __builtin_bfin_[cs]sync
231342e41c54SMike Frysinger		if ($line =~ /__builtin_bfin_csync/) {
231442e41c54SMike Frysinger			my $herevet = "$here\n" . cat_vet($line) . "\n";
2315000d1cc1SJoe Perches			ERROR("CSYNC",
2316000d1cc1SJoe Perches			      "use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
231742e41c54SMike Frysinger		}
231842e41c54SMike Frysinger		if ($line =~ /__builtin_bfin_ssync/) {
231942e41c54SMike Frysinger			my $herevet = "$here\n" . cat_vet($line) . "\n";
2320000d1cc1SJoe Perches			ERROR("SSYNC",
2321000d1cc1SJoe Perches			      "use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
232242e41c54SMike Frysinger		}
232342e41c54SMike Frysinger
232456e77d70SJoe Perches# check for old HOTPLUG __dev<foo> section markings
232556e77d70SJoe Perches		if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
232656e77d70SJoe Perches			WARN("HOTPLUG_SECTION",
232756e77d70SJoe Perches			     "Using $1 is unnecessary\n" . $herecurr);
232856e77d70SJoe Perches		}
232956e77d70SJoe Perches
23309c0ca6f9SAndy Whitcroft# Check for potential 'bare' types
23312b474a1aSAndy Whitcroft		my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
23322b474a1aSAndy Whitcroft		    $realline_next);
23333e469cdcSAndy Whitcroft#print "LINE<$line>\n";
23343e469cdcSAndy Whitcroft		if ($linenr >= $suppress_statement &&
23351b5539b1SJoe Perches		    $realcnt && $sline =~ /.\s*\S/) {
2336170d3a22SAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
2337f5fe35ddSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0);
2338171ae1a4SAndy Whitcroft			$stat =~ s/\n./\n /g;
2339171ae1a4SAndy Whitcroft			$cond =~ s/\n./\n /g;
2340171ae1a4SAndy Whitcroft
23413e469cdcSAndy Whitcroft#print "linenr<$linenr> <$stat>\n";
23423e469cdcSAndy Whitcroft			# If this statement has no statement boundaries within
23433e469cdcSAndy Whitcroft			# it there is no point in retrying a statement scan
23443e469cdcSAndy Whitcroft			# until we hit end of it.
23453e469cdcSAndy Whitcroft			my $frag = $stat; $frag =~ s/;+\s*$//;
23463e469cdcSAndy Whitcroft			if ($frag !~ /(?:{|;)/) {
23473e469cdcSAndy Whitcroft#print "skip<$line_nr_next>\n";
23483e469cdcSAndy Whitcroft				$suppress_statement = $line_nr_next;
23493e469cdcSAndy Whitcroft			}
2350f74bd194SAndy Whitcroft
23512b474a1aSAndy Whitcroft			# Find the real next line.
23522b474a1aSAndy Whitcroft			$realline_next = $line_nr_next;
23532b474a1aSAndy Whitcroft			if (defined $realline_next &&
23542b474a1aSAndy Whitcroft			    (!defined $lines[$realline_next - 1] ||
23552b474a1aSAndy Whitcroft			     substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
23562b474a1aSAndy Whitcroft				$realline_next++;
23572b474a1aSAndy Whitcroft			}
23582b474a1aSAndy Whitcroft
2359171ae1a4SAndy Whitcroft			my $s = $stat;
2360171ae1a4SAndy Whitcroft			$s =~ s/{.*$//s;
2361cf655043SAndy Whitcroft
2362c2fdda0dSAndy Whitcroft			# Ignore goto labels.
2363171ae1a4SAndy Whitcroft			if ($s =~ /$Ident:\*$/s) {
2364c2fdda0dSAndy Whitcroft
2365c2fdda0dSAndy Whitcroft			# Ignore functions being called
2366171ae1a4SAndy Whitcroft			} elsif ($s =~ /^.\s*$Ident\s*\(/s) {
2367c2fdda0dSAndy Whitcroft
2368463f2864SAndy Whitcroft			} elsif ($s =~ /^.\s*else\b/s) {
2369463f2864SAndy Whitcroft
2370c45dcabdSAndy Whitcroft			# declarations always start with types
2371d2506586SAndy 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) {
2372c45dcabdSAndy Whitcroft				my $type = $1;
2373c45dcabdSAndy Whitcroft				$type =~ s/\s+/ /g;
2374c45dcabdSAndy Whitcroft				possible($type, "A:" . $s);
2375c45dcabdSAndy Whitcroft
23766c72ffaaSAndy Whitcroft			# definitions in global scope can only start with types
2377a6a84062SAndy Whitcroft			} elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
2378c45dcabdSAndy Whitcroft				possible($1, "B:" . $s);
2379c2fdda0dSAndy Whitcroft			}
23808905a67cSAndy Whitcroft
23816c72ffaaSAndy Whitcroft			# any (foo ... *) is a pointer cast, and foo is a type
238265863862SAndy Whitcroft			while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
2383c45dcabdSAndy Whitcroft				possible($1, "C:" . $s);
23849c0ca6f9SAndy Whitcroft			}
23858905a67cSAndy Whitcroft
23868905a67cSAndy Whitcroft			# Check for any sort of function declaration.
23878905a67cSAndy Whitcroft			# int foo(something bar, other baz);
23888905a67cSAndy Whitcroft			# void (*store_gdt)(x86_descr_ptr *);
2389171ae1a4SAndy Whitcroft			if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
23908905a67cSAndy Whitcroft				my ($name_len) = length($1);
23918905a67cSAndy Whitcroft
2392cf655043SAndy Whitcroft				my $ctx = $s;
2393773647a0SAndy Whitcroft				substr($ctx, 0, $name_len + 1, '');
23948905a67cSAndy Whitcroft				$ctx =~ s/\)[^\)]*$//;
2395cf655043SAndy Whitcroft
23968905a67cSAndy Whitcroft				for my $arg (split(/\s*,\s*/, $ctx)) {
2397c45dcabdSAndy Whitcroft					if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
23988905a67cSAndy Whitcroft
2399c45dcabdSAndy Whitcroft						possible($1, "D:" . $s);
24008905a67cSAndy Whitcroft					}
24018905a67cSAndy Whitcroft				}
24028905a67cSAndy Whitcroft			}
24038905a67cSAndy Whitcroft
24049c0ca6f9SAndy Whitcroft		}
24059c0ca6f9SAndy Whitcroft
240600df344fSAndy Whitcroft#
240700df344fSAndy Whitcroft# Checks which may be anchored in the context.
240800df344fSAndy Whitcroft#
240900df344fSAndy Whitcroft
241000df344fSAndy Whitcroft# Check for switch () and associated case and default
241100df344fSAndy Whitcroft# statements should be at the same indent.
241200df344fSAndy Whitcroft		if ($line=~/\bswitch\s*\(.*\)/) {
241300df344fSAndy Whitcroft			my $err = '';
241400df344fSAndy Whitcroft			my $sep = '';
241500df344fSAndy Whitcroft			my @ctx = ctx_block_outer($linenr, $realcnt);
241600df344fSAndy Whitcroft			shift(@ctx);
241700df344fSAndy Whitcroft			for my $ctx (@ctx) {
241800df344fSAndy Whitcroft				my ($clen, $cindent) = line_stats($ctx);
241900df344fSAndy Whitcroft				if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
242000df344fSAndy Whitcroft							$indent != $cindent) {
242100df344fSAndy Whitcroft					$err .= "$sep$ctx\n";
242200df344fSAndy Whitcroft					$sep = '';
242300df344fSAndy Whitcroft				} else {
242400df344fSAndy Whitcroft					$sep = "[...]\n";
242500df344fSAndy Whitcroft				}
242600df344fSAndy Whitcroft			}
242700df344fSAndy Whitcroft			if ($err ne '') {
2428000d1cc1SJoe Perches				ERROR("SWITCH_CASE_INDENT_LEVEL",
2429000d1cc1SJoe Perches				      "switch and case should be at the same indent\n$hereline$err");
2430de7d4f0eSAndy Whitcroft			}
2431de7d4f0eSAndy Whitcroft		}
2432de7d4f0eSAndy Whitcroft
2433de7d4f0eSAndy Whitcroft# if/while/etc brace do not go on next line, unless defining a do while loop,
2434de7d4f0eSAndy Whitcroft# or if that brace on the next line is for something else
2435c45dcabdSAndy Whitcroft		if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
2436773647a0SAndy Whitcroft			my $pre_ctx = "$1$2";
2437773647a0SAndy Whitcroft
24389c0ca6f9SAndy Whitcroft			my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
24398eef05ddSJoe Perches
24408eef05ddSJoe Perches			if ($line =~ /^\+\t{6,}/) {
24418eef05ddSJoe Perches				WARN("DEEP_INDENTATION",
24428eef05ddSJoe Perches				     "Too many leading tabs - consider code refactoring\n" . $herecurr);
24438eef05ddSJoe Perches			}
24448eef05ddSJoe Perches
2445de7d4f0eSAndy Whitcroft			my $ctx_cnt = $realcnt - $#ctx - 1;
2446de7d4f0eSAndy Whitcroft			my $ctx = join("\n", @ctx);
2447de7d4f0eSAndy Whitcroft
2448548596d5SAndy Whitcroft			my $ctx_ln = $linenr;
2449548596d5SAndy Whitcroft			my $ctx_skip = $realcnt;
2450de7d4f0eSAndy Whitcroft
2451548596d5SAndy Whitcroft			while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
2452548596d5SAndy Whitcroft					defined $lines[$ctx_ln - 1] &&
2453548596d5SAndy Whitcroft					$lines[$ctx_ln - 1] =~ /^-/)) {
2454548596d5SAndy Whitcroft				##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
2455548596d5SAndy Whitcroft				$ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
2456773647a0SAndy Whitcroft				$ctx_ln++;
2457773647a0SAndy Whitcroft			}
2458548596d5SAndy Whitcroft
245953210168SAndy Whitcroft			#print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
246053210168SAndy Whitcroft			#print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
2461773647a0SAndy Whitcroft
2462773647a0SAndy Whitcroft			if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln -1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
2463000d1cc1SJoe Perches				ERROR("OPEN_BRACE",
2464000d1cc1SJoe Perches				      "that open brace { should be on the previous line\n" .
246501464f30SAndy Whitcroft					"$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
246600df344fSAndy Whitcroft			}
2467773647a0SAndy Whitcroft			if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
2468773647a0SAndy Whitcroft			    $ctx =~ /\)\s*\;\s*$/ &&
2469773647a0SAndy Whitcroft			    defined $lines[$ctx_ln - 1])
2470773647a0SAndy Whitcroft			{
24719c0ca6f9SAndy Whitcroft				my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
24729c0ca6f9SAndy Whitcroft				if ($nindent > $indent) {
2473000d1cc1SJoe Perches					WARN("TRAILING_SEMICOLON",
2474000d1cc1SJoe Perches					     "trailing semicolon indicates no statements, indent implies otherwise\n" .
247501464f30SAndy Whitcroft						"$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
24769c0ca6f9SAndy Whitcroft				}
24779c0ca6f9SAndy Whitcroft			}
247800df344fSAndy Whitcroft		}
247900df344fSAndy Whitcroft
24804d001e4dSAndy Whitcroft# Check relative indent for conditionals and blocks.
24814d001e4dSAndy Whitcroft		if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
24823e469cdcSAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
24833e469cdcSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0)
24843e469cdcSAndy Whitcroft					if (!defined $stat);
24854d001e4dSAndy Whitcroft			my ($s, $c) = ($stat, $cond);
24864d001e4dSAndy Whitcroft
24874d001e4dSAndy Whitcroft			substr($s, 0, length($c), '');
24884d001e4dSAndy Whitcroft
24894d001e4dSAndy Whitcroft			# Make sure we remove the line prefixes as we have
24904d001e4dSAndy Whitcroft			# none on the first line, and are going to readd them
24914d001e4dSAndy Whitcroft			# where necessary.
24924d001e4dSAndy Whitcroft			$s =~ s/\n./\n/gs;
24934d001e4dSAndy Whitcroft
24944d001e4dSAndy Whitcroft			# Find out how long the conditional actually is.
24956f779c18SAndy Whitcroft			my @newlines = ($c =~ /\n/gs);
24966f779c18SAndy Whitcroft			my $cond_lines = 1 + $#newlines;
24974d001e4dSAndy Whitcroft
24984d001e4dSAndy Whitcroft			# We want to check the first line inside the block
24994d001e4dSAndy Whitcroft			# starting at the end of the conditional, so remove:
25004d001e4dSAndy Whitcroft			#  1) any blank line termination
25014d001e4dSAndy Whitcroft			#  2) any opening brace { on end of the line
25024d001e4dSAndy Whitcroft			#  3) any do (...) {
25034d001e4dSAndy Whitcroft			my $continuation = 0;
25044d001e4dSAndy Whitcroft			my $check = 0;
25054d001e4dSAndy Whitcroft			$s =~ s/^.*\bdo\b//;
25064d001e4dSAndy Whitcroft			$s =~ s/^\s*{//;
25074d001e4dSAndy Whitcroft			if ($s =~ s/^\s*\\//) {
25084d001e4dSAndy Whitcroft				$continuation = 1;
25094d001e4dSAndy Whitcroft			}
25109bd49efeSAndy Whitcroft			if ($s =~ s/^\s*?\n//) {
25114d001e4dSAndy Whitcroft				$check = 1;
25124d001e4dSAndy Whitcroft				$cond_lines++;
25134d001e4dSAndy Whitcroft			}
25144d001e4dSAndy Whitcroft
25154d001e4dSAndy Whitcroft			# Also ignore a loop construct at the end of a
25164d001e4dSAndy Whitcroft			# preprocessor statement.
25174d001e4dSAndy Whitcroft			if (($prevline =~ /^.\s*#\s*define\s/ ||
25184d001e4dSAndy Whitcroft			    $prevline =~ /\\\s*$/) && $continuation == 0) {
25194d001e4dSAndy Whitcroft				$check = 0;
25204d001e4dSAndy Whitcroft			}
25214d001e4dSAndy Whitcroft
25229bd49efeSAndy Whitcroft			my $cond_ptr = -1;
2523740504c6SAndy Whitcroft			$continuation = 0;
25249bd49efeSAndy Whitcroft			while ($cond_ptr != $cond_lines) {
25259bd49efeSAndy Whitcroft				$cond_ptr = $cond_lines;
25264d001e4dSAndy Whitcroft
2527f16fa28fSAndy Whitcroft				# If we see an #else/#elif then the code
2528f16fa28fSAndy Whitcroft				# is not linear.
2529f16fa28fSAndy Whitcroft				if ($s =~ /^\s*\#\s*(?:else|elif)/) {
2530f16fa28fSAndy Whitcroft					$check = 0;
2531f16fa28fSAndy Whitcroft				}
2532f16fa28fSAndy Whitcroft
25339bd49efeSAndy Whitcroft				# Ignore:
25349bd49efeSAndy Whitcroft				#  1) blank lines, they should be at 0,
25359bd49efeSAndy Whitcroft				#  2) preprocessor lines, and
25369bd49efeSAndy Whitcroft				#  3) labels.
2537740504c6SAndy Whitcroft				if ($continuation ||
2538740504c6SAndy Whitcroft				    $s =~ /^\s*?\n/ ||
25399bd49efeSAndy Whitcroft				    $s =~ /^\s*#\s*?/ ||
25409bd49efeSAndy Whitcroft				    $s =~ /^\s*$Ident\s*:/) {
2541740504c6SAndy Whitcroft					$continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
254230dad6ebSAndy Whitcroft					if ($s =~ s/^.*?\n//) {
25439bd49efeSAndy Whitcroft						$cond_lines++;
25449bd49efeSAndy Whitcroft					}
25454d001e4dSAndy Whitcroft				}
254630dad6ebSAndy Whitcroft			}
25474d001e4dSAndy Whitcroft
25484d001e4dSAndy Whitcroft			my (undef, $sindent) = line_stats("+" . $s);
25494d001e4dSAndy Whitcroft			my $stat_real = raw_line($linenr, $cond_lines);
25504d001e4dSAndy Whitcroft
25514d001e4dSAndy Whitcroft			# Check if either of these lines are modified, else
25524d001e4dSAndy Whitcroft			# this is not this patch's fault.
25534d001e4dSAndy Whitcroft			if (!defined($stat_real) ||
25544d001e4dSAndy Whitcroft			    $stat !~ /^\+/ && $stat_real !~ /^\+/) {
25554d001e4dSAndy Whitcroft				$check = 0;
25564d001e4dSAndy Whitcroft			}
25574d001e4dSAndy Whitcroft			if (defined($stat_real) && $cond_lines > 1) {
25584d001e4dSAndy Whitcroft				$stat_real = "[...]\n$stat_real";
25594d001e4dSAndy Whitcroft			}
25604d001e4dSAndy Whitcroft
25619bd49efeSAndy 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";
25624d001e4dSAndy Whitcroft
25634d001e4dSAndy Whitcroft			if ($check && (($sindent % 8) != 0 ||
25644d001e4dSAndy Whitcroft			    ($sindent <= $indent && $s ne ''))) {
2565000d1cc1SJoe Perches				WARN("SUSPECT_CODE_INDENT",
2566000d1cc1SJoe Perches				     "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
25674d001e4dSAndy Whitcroft			}
25684d001e4dSAndy Whitcroft		}
25694d001e4dSAndy Whitcroft
25706c72ffaaSAndy Whitcroft		# Track the 'values' across context and added lines.
25716c72ffaaSAndy Whitcroft		my $opline = $line; $opline =~ s/^./ /;
25721f65f947SAndy Whitcroft		my ($curr_values, $curr_vars) =
25731f65f947SAndy Whitcroft				annotate_values($opline . "\n", $prev_values);
25746c72ffaaSAndy Whitcroft		$curr_values = $prev_values . $curr_values;
2575c2fdda0dSAndy Whitcroft		if ($dbg_values) {
2576c2fdda0dSAndy Whitcroft			my $outline = $opline; $outline =~ s/\t/ /g;
2577cf655043SAndy Whitcroft			print "$linenr > .$outline\n";
2578cf655043SAndy Whitcroft			print "$linenr > $curr_values\n";
25791f65f947SAndy Whitcroft			print "$linenr >  $curr_vars\n";
2580c2fdda0dSAndy Whitcroft		}
25816c72ffaaSAndy Whitcroft		$prev_values = substr($curr_values, -1);
25826c72ffaaSAndy Whitcroft
258300df344fSAndy Whitcroft#ignore lines not being added
25843705ce5bSJoe Perches		next if ($line =~ /^[^\+]/);
258500df344fSAndy Whitcroft
2586653d4876SAndy Whitcroft# TEST: allow direct testing of the type matcher.
25877429c690SAndy Whitcroft		if ($dbg_type) {
25887429c690SAndy Whitcroft			if ($line =~ /^.\s*$Declare\s*$/) {
2589000d1cc1SJoe Perches				ERROR("TEST_TYPE",
2590000d1cc1SJoe Perches				      "TEST: is type\n" . $herecurr);
25917429c690SAndy Whitcroft			} elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
2592000d1cc1SJoe Perches				ERROR("TEST_NOT_TYPE",
2593000d1cc1SJoe Perches				      "TEST: is not type ($1 is)\n". $herecurr);
25947429c690SAndy Whitcroft			}
2595653d4876SAndy Whitcroft			next;
2596653d4876SAndy Whitcroft		}
2597a1ef277eSAndy Whitcroft# TEST: allow direct testing of the attribute matcher.
2598a1ef277eSAndy Whitcroft		if ($dbg_attr) {
25999360b0e5SAndy Whitcroft			if ($line =~ /^.\s*$Modifier\s*$/) {
2600000d1cc1SJoe Perches				ERROR("TEST_ATTR",
2601000d1cc1SJoe Perches				      "TEST: is attr\n" . $herecurr);
26029360b0e5SAndy Whitcroft			} elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
2603000d1cc1SJoe Perches				ERROR("TEST_NOT_ATTR",
2604000d1cc1SJoe Perches				      "TEST: is not attr ($1 is)\n". $herecurr);
2605a1ef277eSAndy Whitcroft			}
2606a1ef277eSAndy Whitcroft			next;
2607a1ef277eSAndy Whitcroft		}
2608653d4876SAndy Whitcroft
2609f0a594c1SAndy Whitcroft# check for initialisation to aggregates open brace on the next line
261099423c20SAndy Whitcroft		if ($line =~ /^.\s*{/ &&
261199423c20SAndy Whitcroft		    $prevline =~ /(?:^|[^=])=\s*$/) {
2612000d1cc1SJoe Perches			ERROR("OPEN_BRACE",
2613000d1cc1SJoe Perches			      "that open brace { should be on the previous line\n" . $hereprev);
2614f0a594c1SAndy Whitcroft		}
2615f0a594c1SAndy Whitcroft
261600df344fSAndy Whitcroft#
261700df344fSAndy Whitcroft# Checks which are anchored on the added line.
261800df344fSAndy Whitcroft#
261900df344fSAndy Whitcroft
2620653d4876SAndy Whitcroft# check for malformed paths in #include statements (uses RAW line)
2621c45dcabdSAndy Whitcroft		if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
2622653d4876SAndy Whitcroft			my $path = $1;
2623653d4876SAndy Whitcroft			if ($path =~ m{//}) {
2624000d1cc1SJoe Perches				ERROR("MALFORMED_INCLUDE",
2625495e9d84SJoe Perches				      "malformed #include filename\n" . $herecurr);
2626495e9d84SJoe Perches			}
2627495e9d84SJoe Perches			if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
2628495e9d84SJoe Perches				ERROR("UAPI_INCLUDE",
2629495e9d84SJoe Perches				      "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
2630653d4876SAndy Whitcroft			}
2631653d4876SAndy Whitcroft		}
2632653d4876SAndy Whitcroft
263300df344fSAndy Whitcroft# no C99 // comments
263400df344fSAndy Whitcroft		if ($line =~ m{//}) {
26353705ce5bSJoe Perches			if (ERROR("C99_COMMENTS",
26363705ce5bSJoe Perches				  "do not use C99 // comments\n" . $herecurr) &&
26373705ce5bSJoe Perches			    $fix) {
26383705ce5bSJoe Perches				my $line = $fixed[$linenr - 1];
26393705ce5bSJoe Perches				if ($line =~ /\/\/(.*)$/) {
26403705ce5bSJoe Perches					my $comment = trim($1);
26413705ce5bSJoe Perches					$fixed[$linenr - 1] =~ s@\/\/(.*)$@/\* $comment \*/@;
26423705ce5bSJoe Perches				}
26433705ce5bSJoe Perches			}
264400df344fSAndy Whitcroft		}
264500df344fSAndy Whitcroft		# Remove C99 comments.
26460a920b5bSAndy Whitcroft		$line =~ s@//.*@@;
26476c72ffaaSAndy Whitcroft		$opline =~ s@//.*@@;
26480a920b5bSAndy Whitcroft
26492b474a1aSAndy Whitcroft# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
26502b474a1aSAndy Whitcroft# the whole statement.
26512b474a1aSAndy Whitcroft#print "APW <$lines[$realline_next - 1]>\n";
26522b474a1aSAndy Whitcroft		if (defined $realline_next &&
26532b474a1aSAndy Whitcroft		    exists $lines[$realline_next - 1] &&
26542b474a1aSAndy Whitcroft		    !defined $suppress_export{$realline_next} &&
26552b474a1aSAndy Whitcroft		    ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
26562b474a1aSAndy Whitcroft		     $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
26573cbf62dfSAndy Whitcroft			# Handle definitions which produce identifiers with
26583cbf62dfSAndy Whitcroft			# a prefix:
26593cbf62dfSAndy Whitcroft			#   XXX(foo);
26603cbf62dfSAndy Whitcroft			#   EXPORT_SYMBOL(something_foo);
2661653d4876SAndy Whitcroft			my $name = $1;
266287a53877SAndy Whitcroft			if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
26633cbf62dfSAndy Whitcroft			    $name =~ /^${Ident}_$2/) {
26643cbf62dfSAndy Whitcroft#print "FOO C name<$name>\n";
26653cbf62dfSAndy Whitcroft				$suppress_export{$realline_next} = 1;
26663cbf62dfSAndy Whitcroft
26673cbf62dfSAndy Whitcroft			} elsif ($stat !~ /(?:
26682b474a1aSAndy Whitcroft				\n.}\s*$|
266948012058SAndy Whitcroft				^.DEFINE_$Ident\(\Q$name\E\)|
267048012058SAndy Whitcroft				^.DECLARE_$Ident\(\Q$name\E\)|
267148012058SAndy Whitcroft				^.LIST_HEAD\(\Q$name\E\)|
26722b474a1aSAndy Whitcroft				^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
26732b474a1aSAndy Whitcroft				\b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
267448012058SAndy Whitcroft			    )/x) {
26752b474a1aSAndy Whitcroft#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
26762b474a1aSAndy Whitcroft				$suppress_export{$realline_next} = 2;
26772b474a1aSAndy Whitcroft			} else {
26782b474a1aSAndy Whitcroft				$suppress_export{$realline_next} = 1;
26790a920b5bSAndy Whitcroft			}
26800a920b5bSAndy Whitcroft		}
26812b474a1aSAndy Whitcroft		if (!defined $suppress_export{$linenr} &&
26822b474a1aSAndy Whitcroft		    $prevline =~ /^.\s*$/ &&
26832b474a1aSAndy Whitcroft		    ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
26842b474a1aSAndy Whitcroft		     $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
26852b474a1aSAndy Whitcroft#print "FOO B <$lines[$linenr - 1]>\n";
26862b474a1aSAndy Whitcroft			$suppress_export{$linenr} = 2;
26872b474a1aSAndy Whitcroft		}
26882b474a1aSAndy Whitcroft		if (defined $suppress_export{$linenr} &&
26892b474a1aSAndy Whitcroft		    $suppress_export{$linenr} == 2) {
2690000d1cc1SJoe Perches			WARN("EXPORT_SYMBOL",
2691000d1cc1SJoe Perches			     "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
26922b474a1aSAndy Whitcroft		}
26930a920b5bSAndy Whitcroft
26945150bda4SJoe Eloff# check for global initialisers.
2695d5e616fcSJoe Perches		if ($line =~ /^\+(\s*$Type\s*$Ident\s*(?:\s+$Modifier))*\s*=\s*(0|NULL|false)\s*;/) {
2696d5e616fcSJoe Perches			if (ERROR("GLOBAL_INITIALISERS",
2697000d1cc1SJoe Perches				  "do not initialise globals to 0 or NULL\n" .
2698d5e616fcSJoe Perches				      $herecurr) &&
2699d5e616fcSJoe Perches			    $fix) {
2700d5e616fcSJoe Perches				$fixed[$linenr - 1] =~ s/($Type\s*$Ident\s*(?:\s+$Modifier))*\s*=\s*(0|NULL|false)\s*;/$1;/;
2701d5e616fcSJoe Perches			}
2702f0a594c1SAndy Whitcroft		}
27030a920b5bSAndy Whitcroft# check for static initialisers.
2704d5e616fcSJoe Perches		if ($line =~ /^\+.*\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
2705d5e616fcSJoe Perches			if (ERROR("INITIALISED_STATIC",
2706000d1cc1SJoe Perches				  "do not initialise statics to 0 or NULL\n" .
2707d5e616fcSJoe Perches				      $herecurr) &&
2708d5e616fcSJoe Perches			    $fix) {
2709d5e616fcSJoe Perches				$fixed[$linenr - 1] =~ s/(\bstatic\s.*?)\s*=\s*(0|NULL|false)\s*;/$1;/;
2710d5e616fcSJoe Perches			}
27110a920b5bSAndy Whitcroft		}
27120a920b5bSAndy Whitcroft
2713cb710ecaSJoe Perches# check for static const char * arrays.
2714cb710ecaSJoe Perches		if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
2715000d1cc1SJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
2716000d1cc1SJoe Perches			     "static const char * array should probably be static const char * const\n" .
2717cb710ecaSJoe Perches				$herecurr);
2718cb710ecaSJoe Perches               }
2719cb710ecaSJoe Perches
2720cb710ecaSJoe Perches# check for static char foo[] = "bar" declarations.
2721cb710ecaSJoe Perches		if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
2722000d1cc1SJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
2723000d1cc1SJoe Perches			     "static char array declaration should probably be static const char\n" .
2724cb710ecaSJoe Perches				$herecurr);
2725cb710ecaSJoe Perches               }
2726cb710ecaSJoe Perches
27279b0fa60dSJoe Perches# check for non-global char *foo[] = {"bar", ...} declarations.
27289b0fa60dSJoe Perches		if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) {
27299b0fa60dSJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
27309b0fa60dSJoe Perches			     "char * array declaration might be better as static const\n" .
27319b0fa60dSJoe Perches				$herecurr);
27329b0fa60dSJoe Perches               }
27339b0fa60dSJoe Perches
2734b36190c5SJoe Perches# check for function declarations without arguments like "int foo()"
2735b36190c5SJoe Perches		if ($line =~ /(\b$Type\s+$Ident)\s*\(\s*\)/) {
2736b36190c5SJoe Perches			if (ERROR("FUNCTION_WITHOUT_ARGS",
2737b36190c5SJoe Perches				  "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) &&
2738b36190c5SJoe Perches			    $fix) {
2739b36190c5SJoe Perches				$fixed[$linenr - 1] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/;
2740b36190c5SJoe Perches			}
2741b36190c5SJoe Perches		}
2742b36190c5SJoe Perches
274392e112fdSJoe Perches# check for uses of DEFINE_PCI_DEVICE_TABLE
274492e112fdSJoe Perches		if ($line =~ /\bDEFINE_PCI_DEVICE_TABLE\s*\(\s*(\w+)\s*\)\s*=/) {
274592e112fdSJoe Perches			if (WARN("DEFINE_PCI_DEVICE_TABLE",
274692e112fdSJoe Perches				 "Prefer struct pci_device_id over deprecated DEFINE_PCI_DEVICE_TABLE\n" . $herecurr) &&
274792e112fdSJoe Perches			    $fix) {
274892e112fdSJoe Perches				$fixed[$linenr - 1] =~ s/\b(?:static\s+|)DEFINE_PCI_DEVICE_TABLE\s*\(\s*(\w+)\s*\)\s*=\s*/static const struct pci_device_id $1\[\] = /;
274992e112fdSJoe Perches			}
275093ed0e2dSJoe Perches		}
275193ed0e2dSJoe Perches
2752653d4876SAndy Whitcroft# check for new typedefs, only function parameters and sparse annotations
2753653d4876SAndy Whitcroft# make sense.
2754653d4876SAndy Whitcroft		if ($line =~ /\btypedef\s/ &&
27558054576dSAndy Whitcroft		    $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
2756c45dcabdSAndy Whitcroft		    $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
27578ed22cadSAndy Whitcroft		    $line !~ /\b$typeTypedefs\b/ &&
2758653d4876SAndy Whitcroft		    $line !~ /\b__bitwise(?:__|)\b/) {
2759000d1cc1SJoe Perches			WARN("NEW_TYPEDEFS",
2760000d1cc1SJoe Perches			     "do not add new typedefs\n" . $herecurr);
27610a920b5bSAndy Whitcroft		}
27620a920b5bSAndy Whitcroft
27630a920b5bSAndy Whitcroft# * goes on variable not on type
276465863862SAndy Whitcroft		# (char*[ const])
2765bfcb2cc7SAndy Whitcroft		while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
2766bfcb2cc7SAndy Whitcroft			#print "AA<$1>\n";
27673705ce5bSJoe Perches			my ($ident, $from, $to) = ($1, $2, $2);
2768d8aaf121SAndy Whitcroft
276965863862SAndy Whitcroft			# Should start with a space.
277065863862SAndy Whitcroft			$to =~ s/^(\S)/ $1/;
277165863862SAndy Whitcroft			# Should not end with a space.
277265863862SAndy Whitcroft			$to =~ s/\s+$//;
277365863862SAndy Whitcroft			# '*'s should not have spaces between.
2774f9a0b3d1SAndy Whitcroft			while ($to =~ s/\*\s+\*/\*\*/) {
277565863862SAndy Whitcroft			}
2776d8aaf121SAndy Whitcroft
27773705ce5bSJoe Perches##			print "1: from<$from> to<$to> ident<$ident>\n";
277865863862SAndy Whitcroft			if ($from ne $to) {
27793705ce5bSJoe Perches				if (ERROR("POINTER_LOCATION",
27803705ce5bSJoe Perches					  "\"(foo$from)\" should be \"(foo$to)\"\n" .  $herecurr) &&
27813705ce5bSJoe Perches				    $fix) {
27823705ce5bSJoe Perches					my $sub_from = $ident;
27833705ce5bSJoe Perches					my $sub_to = $ident;
27843705ce5bSJoe Perches					$sub_to =~ s/\Q$from\E/$to/;
27853705ce5bSJoe Perches					$fixed[$linenr - 1] =~
27863705ce5bSJoe Perches					    s@\Q$sub_from\E@$sub_to@;
27873705ce5bSJoe Perches				}
278865863862SAndy Whitcroft			}
2789bfcb2cc7SAndy Whitcroft		}
2790bfcb2cc7SAndy Whitcroft		while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
2791bfcb2cc7SAndy Whitcroft			#print "BB<$1>\n";
27923705ce5bSJoe Perches			my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
2793d8aaf121SAndy Whitcroft
279465863862SAndy Whitcroft			# Should start with a space.
279565863862SAndy Whitcroft			$to =~ s/^(\S)/ $1/;
279665863862SAndy Whitcroft			# Should not end with a space.
279765863862SAndy Whitcroft			$to =~ s/\s+$//;
279865863862SAndy Whitcroft			# '*'s should not have spaces between.
2799f9a0b3d1SAndy Whitcroft			while ($to =~ s/\*\s+\*/\*\*/) {
280065863862SAndy Whitcroft			}
280165863862SAndy Whitcroft			# Modifiers should have spaces.
280265863862SAndy Whitcroft			$to =~ s/(\b$Modifier$)/$1 /;
280365863862SAndy Whitcroft
28043705ce5bSJoe Perches##			print "2: from<$from> to<$to> ident<$ident>\n";
2805667026e7SAndy Whitcroft			if ($from ne $to && $ident !~ /^$Modifier$/) {
28063705ce5bSJoe Perches				if (ERROR("POINTER_LOCATION",
28073705ce5bSJoe Perches					  "\"foo${from}bar\" should be \"foo${to}bar\"\n" .  $herecurr) &&
28083705ce5bSJoe Perches				    $fix) {
28093705ce5bSJoe Perches
28103705ce5bSJoe Perches					my $sub_from = $match;
28113705ce5bSJoe Perches					my $sub_to = $match;
28123705ce5bSJoe Perches					$sub_to =~ s/\Q$from\E/$to/;
28133705ce5bSJoe Perches					$fixed[$linenr - 1] =~
28143705ce5bSJoe Perches					    s@\Q$sub_from\E@$sub_to@;
28153705ce5bSJoe Perches				}
281665863862SAndy Whitcroft			}
28170a920b5bSAndy Whitcroft		}
28180a920b5bSAndy Whitcroft
28190a920b5bSAndy Whitcroft# # no BUG() or BUG_ON()
28200a920b5bSAndy Whitcroft# 		if ($line =~ /\b(BUG|BUG_ON)\b/) {
28210a920b5bSAndy Whitcroft# 			print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
28220a920b5bSAndy Whitcroft# 			print "$herecurr";
28230a920b5bSAndy Whitcroft# 			$clean = 0;
28240a920b5bSAndy Whitcroft# 		}
28250a920b5bSAndy Whitcroft
28268905a67cSAndy Whitcroft		if ($line =~ /\bLINUX_VERSION_CODE\b/) {
2827000d1cc1SJoe Perches			WARN("LINUX_VERSION_CODE",
2828000d1cc1SJoe Perches			     "LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
28298905a67cSAndy Whitcroft		}
28308905a67cSAndy Whitcroft
283117441227SJoe Perches# check for uses of printk_ratelimit
283217441227SJoe Perches		if ($line =~ /\bprintk_ratelimit\s*\(/) {
2833000d1cc1SJoe Perches			WARN("PRINTK_RATELIMITED",
2834000d1cc1SJoe Perches"Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
283517441227SJoe Perches		}
283617441227SJoe Perches
283700df344fSAndy Whitcroft# printk should use KERN_* levels.  Note that follow on printk's on the
283800df344fSAndy Whitcroft# same line do not need a level, so we use the current block context
283900df344fSAndy Whitcroft# to try and find and validate the current printk.  In summary the current
284025985edcSLucas De Marchi# printk includes all preceding printk's which have no newline on the end.
284100df344fSAndy Whitcroft# we assume the first bad printk is the one to report.
2842f0a594c1SAndy Whitcroft		if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
284300df344fSAndy Whitcroft			my $ok = 0;
284400df344fSAndy Whitcroft			for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
284500df344fSAndy Whitcroft				#print "CHECK<$lines[$ln - 1]\n";
284625985edcSLucas De Marchi				# we have a preceding printk if it ends
284700df344fSAndy Whitcroft				# with "\n" ignore it, else it is to blame
284800df344fSAndy Whitcroft				if ($lines[$ln - 1] =~ m{\bprintk\(}) {
284900df344fSAndy Whitcroft					if ($rawlines[$ln - 1] !~ m{\\n"}) {
285000df344fSAndy Whitcroft						$ok = 1;
285100df344fSAndy Whitcroft					}
285200df344fSAndy Whitcroft					last;
285300df344fSAndy Whitcroft				}
285400df344fSAndy Whitcroft			}
285500df344fSAndy Whitcroft			if ($ok == 0) {
2856000d1cc1SJoe Perches				WARN("PRINTK_WITHOUT_KERN_LEVEL",
2857000d1cc1SJoe Perches				     "printk() should include KERN_ facility level\n" . $herecurr);
28580a920b5bSAndy Whitcroft			}
285900df344fSAndy Whitcroft		}
28600a920b5bSAndy Whitcroft
2861243f3803SJoe Perches		if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
2862243f3803SJoe Perches			my $orig = $1;
2863243f3803SJoe Perches			my $level = lc($orig);
2864243f3803SJoe Perches			$level = "warn" if ($level eq "warning");
28658f26b837SJoe Perches			my $level2 = $level;
28668f26b837SJoe Perches			$level2 = "dbg" if ($level eq "debug");
2867243f3803SJoe Perches			WARN("PREFER_PR_LEVEL",
2868daa8b059SYogesh Chaudhari			     "Prefer [subsystem eg: netdev]_$level2([subsystem]dev, ... then dev_$level2(dev, ... then pr_$level(...  to printk(KERN_$orig ...\n" . $herecurr);
2869243f3803SJoe Perches		}
2870243f3803SJoe Perches
2871243f3803SJoe Perches		if ($line =~ /\bpr_warning\s*\(/) {
2872d5e616fcSJoe Perches			if (WARN("PREFER_PR_LEVEL",
2873d5e616fcSJoe Perches				 "Prefer pr_warn(... to pr_warning(...\n" . $herecurr) &&
2874d5e616fcSJoe Perches			    $fix) {
2875d5e616fcSJoe Perches				$fixed[$linenr - 1] =~
2876d5e616fcSJoe Perches				    s/\bpr_warning\b/pr_warn/;
2877d5e616fcSJoe Perches			}
2878243f3803SJoe Perches		}
2879243f3803SJoe Perches
2880dc139313SJoe Perches		if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
2881dc139313SJoe Perches			my $orig = $1;
2882dc139313SJoe Perches			my $level = lc($orig);
2883dc139313SJoe Perches			$level = "warn" if ($level eq "warning");
2884dc139313SJoe Perches			$level = "dbg" if ($level eq "debug");
2885dc139313SJoe Perches			WARN("PREFER_DEV_LEVEL",
2886dc139313SJoe Perches			     "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
2887dc139313SJoe Perches		}
2888dc139313SJoe Perches
2889653d4876SAndy Whitcroft# function brace can't be on same line, except for #defines of do while,
2890653d4876SAndy Whitcroft# or if closed on same line
2891c45dcabdSAndy Whitcroft		if (($line=~/$Type\s*$Ident\(.*\).*\s{/) and
2892c45dcabdSAndy Whitcroft		    !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) {
2893000d1cc1SJoe Perches			ERROR("OPEN_BRACE",
2894000d1cc1SJoe Perches			      "open brace '{' following function declarations go on the next line\n" . $herecurr);
28950a920b5bSAndy Whitcroft		}
2896653d4876SAndy Whitcroft
28978905a67cSAndy Whitcroft# open braces for enum, union and struct go on the same line.
28988905a67cSAndy Whitcroft		if ($line =~ /^.\s*{/ &&
28998905a67cSAndy Whitcroft		    $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
2900000d1cc1SJoe Perches			ERROR("OPEN_BRACE",
2901000d1cc1SJoe Perches			      "open brace '{' following $1 go on the same line\n" . $hereprev);
29028905a67cSAndy Whitcroft		}
29038905a67cSAndy Whitcroft
29040c73b4ebSAndy Whitcroft# missing space after union, struct or enum definition
29053705ce5bSJoe Perches		if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
29063705ce5bSJoe Perches			if (WARN("SPACING",
29073705ce5bSJoe Perches				 "missing space after $1 definition\n" . $herecurr) &&
29083705ce5bSJoe Perches			    $fix) {
29093705ce5bSJoe Perches				$fixed[$linenr - 1] =~
29103705ce5bSJoe Perches				    s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
29113705ce5bSJoe Perches			}
29120c73b4ebSAndy Whitcroft		}
29130c73b4ebSAndy Whitcroft
291431070b5dSJoe Perches# Function pointer declarations
291531070b5dSJoe Perches# check spacing between type, funcptr, and args
291631070b5dSJoe Perches# canonical declaration is "type (*funcptr)(args...)"
291791f72e9cSJoe Perches		if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) {
291831070b5dSJoe Perches			my $declare = $1;
291931070b5dSJoe Perches			my $pre_pointer_space = $2;
292031070b5dSJoe Perches			my $post_pointer_space = $3;
292131070b5dSJoe Perches			my $funcname = $4;
292231070b5dSJoe Perches			my $post_funcname_space = $5;
292331070b5dSJoe Perches			my $pre_args_space = $6;
292431070b5dSJoe Perches
292591f72e9cSJoe Perches# the $Declare variable will capture all spaces after the type
292691f72e9cSJoe Perches# so check it for a missing trailing missing space but pointer return types
292791f72e9cSJoe Perches# don't need a space so don't warn for those.
292891f72e9cSJoe Perches			my $post_declare_space = "";
292991f72e9cSJoe Perches			if ($declare =~ /(\s+)$/) {
293091f72e9cSJoe Perches				$post_declare_space = $1;
293191f72e9cSJoe Perches				$declare = rtrim($declare);
293291f72e9cSJoe Perches			}
293391f72e9cSJoe Perches			if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) {
293431070b5dSJoe Perches				WARN("SPACING",
293531070b5dSJoe Perches				     "missing space after return type\n" . $herecurr);
293691f72e9cSJoe Perches				$post_declare_space = " ";
293731070b5dSJoe Perches			}
293831070b5dSJoe Perches
293931070b5dSJoe Perches# unnecessary space "type  (*funcptr)(args...)"
294091f72e9cSJoe Perches# This test is not currently implemented because these declarations are
294191f72e9cSJoe Perches# equivalent to
294291f72e9cSJoe Perches#	int  foo(int bar, ...)
294391f72e9cSJoe Perches# and this is form shouldn't/doesn't generate a checkpatch warning.
294491f72e9cSJoe Perches#
294591f72e9cSJoe Perches#			elsif ($declare =~ /\s{2,}$/) {
294691f72e9cSJoe Perches#				WARN("SPACING",
294791f72e9cSJoe Perches#				     "Multiple spaces after return type\n" . $herecurr);
294891f72e9cSJoe Perches#			}
294931070b5dSJoe Perches
295031070b5dSJoe Perches# unnecessary space "type ( *funcptr)(args...)"
295131070b5dSJoe Perches			if (defined $pre_pointer_space &&
295231070b5dSJoe Perches			    $pre_pointer_space =~ /^\s/) {
295331070b5dSJoe Perches				WARN("SPACING",
295431070b5dSJoe Perches				     "Unnecessary space after function pointer open parenthesis\n" . $herecurr);
295531070b5dSJoe Perches			}
295631070b5dSJoe Perches
295731070b5dSJoe Perches# unnecessary space "type (* funcptr)(args...)"
295831070b5dSJoe Perches			if (defined $post_pointer_space &&
295931070b5dSJoe Perches			    $post_pointer_space =~ /^\s/) {
296031070b5dSJoe Perches				WARN("SPACING",
296131070b5dSJoe Perches				     "Unnecessary space before function pointer name\n" . $herecurr);
296231070b5dSJoe Perches			}
296331070b5dSJoe Perches
296431070b5dSJoe Perches# unnecessary space "type (*funcptr )(args...)"
296531070b5dSJoe Perches			if (defined $post_funcname_space &&
296631070b5dSJoe Perches			    $post_funcname_space =~ /^\s/) {
296731070b5dSJoe Perches				WARN("SPACING",
296831070b5dSJoe Perches				     "Unnecessary space after function pointer name\n" . $herecurr);
296931070b5dSJoe Perches			}
297031070b5dSJoe Perches
297131070b5dSJoe Perches# unnecessary space "type (*funcptr) (args...)"
297231070b5dSJoe Perches			if (defined $pre_args_space &&
297331070b5dSJoe Perches			    $pre_args_space =~ /^\s/) {
297431070b5dSJoe Perches				WARN("SPACING",
297531070b5dSJoe Perches				     "Unnecessary space before function pointer arguments\n" . $herecurr);
297631070b5dSJoe Perches			}
297731070b5dSJoe Perches
297831070b5dSJoe Perches			if (show_type("SPACING") && $fix) {
297931070b5dSJoe Perches				$fixed[$linenr - 1] =~
298091f72e9cSJoe Perches				    s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex;
298131070b5dSJoe Perches			}
298231070b5dSJoe Perches		}
298331070b5dSJoe Perches
29848d31cfceSAndy Whitcroft# check for spacing round square brackets; allowed:
29858d31cfceSAndy Whitcroft#  1. with a type on the left -- int [] a;
2986fe2a7dbcSAndy Whitcroft#  2. at the beginning of a line for slice initialisers -- [0...10] = 5,
2987fe2a7dbcSAndy Whitcroft#  3. inside a curly brace -- = { [0...10] = 5 }
29888d31cfceSAndy Whitcroft		while ($line =~ /(.*?\s)\[/g) {
29898d31cfceSAndy Whitcroft			my ($where, $prefix) = ($-[1], $1);
29908d31cfceSAndy Whitcroft			if ($prefix !~ /$Type\s+$/ &&
2991fe2a7dbcSAndy Whitcroft			    ($where != 0 || $prefix !~ /^.\s+$/) &&
2992daebc534SAndy Whitcroft			    $prefix !~ /[{,]\s+$/) {
29933705ce5bSJoe Perches				if (ERROR("BRACKET_SPACE",
29943705ce5bSJoe Perches					  "space prohibited before open square bracket '['\n" . $herecurr) &&
29953705ce5bSJoe Perches				    $fix) {
29963705ce5bSJoe Perches				    $fixed[$linenr - 1] =~
29973705ce5bSJoe Perches					s/^(\+.*?)\s+\[/$1\[/;
29983705ce5bSJoe Perches				}
29998d31cfceSAndy Whitcroft			}
30008d31cfceSAndy Whitcroft		}
30018d31cfceSAndy Whitcroft
3002f0a594c1SAndy Whitcroft# check for spaces between functions and their parentheses.
30036c72ffaaSAndy Whitcroft		while ($line =~ /($Ident)\s+\(/g) {
3004c2fdda0dSAndy Whitcroft			my $name = $1;
3005773647a0SAndy Whitcroft			my $ctx_before = substr($line, 0, $-[1]);
3006773647a0SAndy Whitcroft			my $ctx = "$ctx_before$name";
3007c2fdda0dSAndy Whitcroft
3008c2fdda0dSAndy Whitcroft			# Ignore those directives where spaces _are_ permitted.
3009773647a0SAndy Whitcroft			if ($name =~ /^(?:
3010773647a0SAndy Whitcroft				if|for|while|switch|return|case|
3011773647a0SAndy Whitcroft				volatile|__volatile__|
3012773647a0SAndy Whitcroft				__attribute__|format|__extension__|
3013773647a0SAndy Whitcroft				asm|__asm__)$/x)
3014773647a0SAndy Whitcroft			{
3015c2fdda0dSAndy Whitcroft			# cpp #define statements have non-optional spaces, ie
3016c2fdda0dSAndy Whitcroft			# if there is a space between the name and the open
3017c2fdda0dSAndy Whitcroft			# parenthesis it is simply not a parameter group.
3018c45dcabdSAndy Whitcroft			} elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
3019773647a0SAndy Whitcroft
3020773647a0SAndy Whitcroft			# cpp #elif statement condition may start with a (
3021c45dcabdSAndy Whitcroft			} elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
3022c2fdda0dSAndy Whitcroft
3023c2fdda0dSAndy Whitcroft			# If this whole things ends with a type its most
3024c2fdda0dSAndy Whitcroft			# likely a typedef for a function.
3025773647a0SAndy Whitcroft			} elsif ($ctx =~ /$Type$/) {
3026c2fdda0dSAndy Whitcroft
3027c2fdda0dSAndy Whitcroft			} else {
30283705ce5bSJoe Perches				if (WARN("SPACING",
30293705ce5bSJoe Perches					 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
30303705ce5bSJoe Perches					     $fix) {
30313705ce5bSJoe Perches					$fixed[$linenr - 1] =~
30323705ce5bSJoe Perches					    s/\b$name\s+\(/$name\(/;
30333705ce5bSJoe Perches				}
3034f0a594c1SAndy Whitcroft			}
30356c72ffaaSAndy Whitcroft		}
30369a4cad4eSEric Nelson
3037653d4876SAndy Whitcroft# Check operator spacing.
30380a920b5bSAndy Whitcroft		if (!($line=~/\#\s*include/)) {
30393705ce5bSJoe Perches			my $fixed_line = "";
30403705ce5bSJoe Perches			my $line_fixed = 0;
30413705ce5bSJoe Perches
30429c0ca6f9SAndy Whitcroft			my $ops = qr{
30439c0ca6f9SAndy Whitcroft				<<=|>>=|<=|>=|==|!=|
30449c0ca6f9SAndy Whitcroft				\+=|-=|\*=|\/=|%=|\^=|\|=|&=|
30459c0ca6f9SAndy Whitcroft				=>|->|<<|>>|<|>|=|!|~|
30461f65f947SAndy Whitcroft				&&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
304784731623SJoe Perches				\?:|\?|:
30489c0ca6f9SAndy Whitcroft			}x;
3049cf655043SAndy Whitcroft			my @elements = split(/($ops|;)/, $opline);
30503705ce5bSJoe Perches
30513705ce5bSJoe Perches##			print("element count: <" . $#elements . ">\n");
30523705ce5bSJoe Perches##			foreach my $el (@elements) {
30533705ce5bSJoe Perches##				print("el: <$el>\n");
30543705ce5bSJoe Perches##			}
30553705ce5bSJoe Perches
30563705ce5bSJoe Perches			my @fix_elements = ();
305700df344fSAndy Whitcroft			my $off = 0;
30586c72ffaaSAndy Whitcroft
30593705ce5bSJoe Perches			foreach my $el (@elements) {
30603705ce5bSJoe Perches				push(@fix_elements, substr($rawline, $off, length($el)));
30613705ce5bSJoe Perches				$off += length($el);
30623705ce5bSJoe Perches			}
30633705ce5bSJoe Perches
30643705ce5bSJoe Perches			$off = 0;
30653705ce5bSJoe Perches
30666c72ffaaSAndy Whitcroft			my $blank = copy_spacing($opline);
3067b34c648bSJoe Perches			my $last_after = -1;
30686c72ffaaSAndy Whitcroft
30690a920b5bSAndy Whitcroft			for (my $n = 0; $n < $#elements; $n += 2) {
30703705ce5bSJoe Perches
30713705ce5bSJoe Perches				my $good = $fix_elements[$n] . $fix_elements[$n + 1];
30723705ce5bSJoe Perches
30733705ce5bSJoe Perches##				print("n: <$n> good: <$good>\n");
30743705ce5bSJoe Perches
30754a0df2efSAndy Whitcroft				$off += length($elements[$n]);
30764a0df2efSAndy Whitcroft
307725985edcSLucas De Marchi				# Pick up the preceding and succeeding characters.
3078773647a0SAndy Whitcroft				my $ca = substr($opline, 0, $off);
3079773647a0SAndy Whitcroft				my $cc = '';
3080773647a0SAndy Whitcroft				if (length($opline) >= ($off + length($elements[$n + 1]))) {
3081773647a0SAndy Whitcroft					$cc = substr($opline, $off + length($elements[$n + 1]));
3082773647a0SAndy Whitcroft				}
3083773647a0SAndy Whitcroft				my $cb = "$ca$;$cc";
3084773647a0SAndy Whitcroft
30854a0df2efSAndy Whitcroft				my $a = '';
30864a0df2efSAndy Whitcroft				$a = 'V' if ($elements[$n] ne '');
30874a0df2efSAndy Whitcroft				$a = 'W' if ($elements[$n] =~ /\s$/);
3088cf655043SAndy Whitcroft				$a = 'C' if ($elements[$n] =~ /$;$/);
30894a0df2efSAndy Whitcroft				$a = 'B' if ($elements[$n] =~ /(\[|\()$/);
30904a0df2efSAndy Whitcroft				$a = 'O' if ($elements[$n] eq '');
3091773647a0SAndy Whitcroft				$a = 'E' if ($ca =~ /^\s*$/);
30924a0df2efSAndy Whitcroft
30930a920b5bSAndy Whitcroft				my $op = $elements[$n + 1];
30944a0df2efSAndy Whitcroft
30954a0df2efSAndy Whitcroft				my $c = '';
30960a920b5bSAndy Whitcroft				if (defined $elements[$n + 2]) {
30974a0df2efSAndy Whitcroft					$c = 'V' if ($elements[$n + 2] ne '');
30984a0df2efSAndy Whitcroft					$c = 'W' if ($elements[$n + 2] =~ /^\s/);
3099cf655043SAndy Whitcroft					$c = 'C' if ($elements[$n + 2] =~ /^$;/);
31004a0df2efSAndy Whitcroft					$c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
31014a0df2efSAndy Whitcroft					$c = 'O' if ($elements[$n + 2] eq '');
31028b1b3378SAndy Whitcroft					$c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
31034a0df2efSAndy Whitcroft				} else {
31044a0df2efSAndy Whitcroft					$c = 'E';
31050a920b5bSAndy Whitcroft				}
31060a920b5bSAndy Whitcroft
31074a0df2efSAndy Whitcroft				my $ctx = "${a}x${c}";
31084a0df2efSAndy Whitcroft
31094a0df2efSAndy Whitcroft				my $at = "(ctx:$ctx)";
31104a0df2efSAndy Whitcroft
31116c72ffaaSAndy Whitcroft				my $ptr = substr($blank, 0, $off) . "^";
3112de7d4f0eSAndy Whitcroft				my $hereptr = "$hereline$ptr\n";
31130a920b5bSAndy Whitcroft
311474048ed8SAndy Whitcroft				# Pull out the value of this operator.
31156c72ffaaSAndy Whitcroft				my $op_type = substr($curr_values, $off + 1, 1);
31160a920b5bSAndy Whitcroft
31171f65f947SAndy Whitcroft				# Get the full operator variant.
31181f65f947SAndy Whitcroft				my $opv = $op . substr($curr_vars, $off, 1);
31191f65f947SAndy Whitcroft
312013214adfSAndy Whitcroft				# Ignore operators passed as parameters.
312113214adfSAndy Whitcroft				if ($op_type ne 'V' &&
312213214adfSAndy Whitcroft				    $ca =~ /\s$/ && $cc =~ /^\s*,/) {
312313214adfSAndy Whitcroft
3124cf655043SAndy Whitcroft#				# Ignore comments
3125cf655043SAndy Whitcroft#				} elsif ($op =~ /^$;+$/) {
312613214adfSAndy Whitcroft
3127d8aaf121SAndy Whitcroft				# ; should have either the end of line or a space or \ after it
312813214adfSAndy Whitcroft				} elsif ($op eq ';') {
3129cf655043SAndy Whitcroft					if ($ctx !~ /.x[WEBC]/ &&
3130cf655043SAndy Whitcroft					    $cc !~ /^\\/ && $cc !~ /^;/) {
31313705ce5bSJoe Perches						if (ERROR("SPACING",
31323705ce5bSJoe Perches							  "space required after that '$op' $at\n" . $hereptr)) {
3133b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
31343705ce5bSJoe Perches							$line_fixed = 1;
31353705ce5bSJoe Perches						}
3136d8aaf121SAndy Whitcroft					}
3137d8aaf121SAndy Whitcroft
3138d8aaf121SAndy Whitcroft				# // is a comment
3139d8aaf121SAndy Whitcroft				} elsif ($op eq '//') {
31400a920b5bSAndy Whitcroft
31411f65f947SAndy Whitcroft				# No spaces for:
31421f65f947SAndy Whitcroft				#   ->
31431f65f947SAndy Whitcroft				#   :   when part of a bitfield
31441f65f947SAndy Whitcroft				} elsif ($op eq '->' || $opv eq ':B') {
31454a0df2efSAndy Whitcroft					if ($ctx =~ /Wx.|.xW/) {
31463705ce5bSJoe Perches						if (ERROR("SPACING",
31473705ce5bSJoe Perches							  "spaces prohibited around that '$op' $at\n" . $hereptr)) {
3148b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
31493705ce5bSJoe Perches							if (defined $fix_elements[$n + 2]) {
31503705ce5bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
31513705ce5bSJoe Perches							}
3152b34c648bSJoe Perches							$line_fixed = 1;
31533705ce5bSJoe Perches						}
31540a920b5bSAndy Whitcroft					}
31550a920b5bSAndy Whitcroft
31560a920b5bSAndy Whitcroft				# , must have a space on the right.
31570a920b5bSAndy Whitcroft				} elsif ($op eq ',') {
3158cf655043SAndy Whitcroft					if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
31593705ce5bSJoe Perches						if (ERROR("SPACING",
31603705ce5bSJoe Perches							  "space required after that '$op' $at\n" . $hereptr)) {
3161b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
31623705ce5bSJoe Perches							$line_fixed = 1;
3163b34c648bSJoe Perches							$last_after = $n;
31643705ce5bSJoe Perches						}
31650a920b5bSAndy Whitcroft					}
31660a920b5bSAndy Whitcroft
31679c0ca6f9SAndy Whitcroft				# '*' as part of a type definition -- reported already.
316874048ed8SAndy Whitcroft				} elsif ($opv eq '*_') {
31699c0ca6f9SAndy Whitcroft					#warn "'*' is part of type\n";
31709c0ca6f9SAndy Whitcroft
31719c0ca6f9SAndy Whitcroft				# unary operators should have a space before and
31729c0ca6f9SAndy Whitcroft				# none after.  May be left adjacent to another
31739c0ca6f9SAndy Whitcroft				# unary operator, or a cast
31749c0ca6f9SAndy Whitcroft				} elsif ($op eq '!' || $op eq '~' ||
317574048ed8SAndy Whitcroft					 $opv eq '*U' || $opv eq '-U' ||
31760d413866SAndy Whitcroft					 $opv eq '&U' || $opv eq '&&U') {
3177cf655043SAndy Whitcroft					if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
31783705ce5bSJoe Perches						if (ERROR("SPACING",
31793705ce5bSJoe Perches							  "space required before that '$op' $at\n" . $hereptr)) {
3180b34c648bSJoe Perches							if ($n != $last_after + 2) {
3181b34c648bSJoe Perches								$good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]);
31823705ce5bSJoe Perches								$line_fixed = 1;
31833705ce5bSJoe Perches							}
31840a920b5bSAndy Whitcroft						}
3185b34c648bSJoe Perches					}
3186a3340b35SAndy Whitcroft					if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
3187171ae1a4SAndy Whitcroft						# A unary '*' may be const
3188171ae1a4SAndy Whitcroft
3189171ae1a4SAndy Whitcroft					} elsif ($ctx =~ /.xW/) {
31903705ce5bSJoe Perches						if (ERROR("SPACING",
31913705ce5bSJoe Perches							  "space prohibited after that '$op' $at\n" . $hereptr)) {
3192b34c648bSJoe Perches							$good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]);
31933705ce5bSJoe Perches							if (defined $fix_elements[$n + 2]) {
31943705ce5bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
31953705ce5bSJoe Perches							}
3196b34c648bSJoe Perches							$line_fixed = 1;
31973705ce5bSJoe Perches						}
31980a920b5bSAndy Whitcroft					}
31990a920b5bSAndy Whitcroft
32000a920b5bSAndy Whitcroft				# unary ++ and unary -- are allowed no space on one side.
32010a920b5bSAndy Whitcroft				} elsif ($op eq '++' or $op eq '--') {
3202773647a0SAndy Whitcroft					if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
32033705ce5bSJoe Perches						if (ERROR("SPACING",
32043705ce5bSJoe Perches							  "space required one side of that '$op' $at\n" . $hereptr)) {
3205b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
32063705ce5bSJoe Perches							$line_fixed = 1;
32073705ce5bSJoe Perches						}
32080a920b5bSAndy Whitcroft					}
3209773647a0SAndy Whitcroft					if ($ctx =~ /Wx[BE]/ ||
3210773647a0SAndy Whitcroft					    ($ctx =~ /Wx./ && $cc =~ /^;/)) {
32113705ce5bSJoe Perches						if (ERROR("SPACING",
32123705ce5bSJoe Perches							  "space prohibited before that '$op' $at\n" . $hereptr)) {
3213b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
32143705ce5bSJoe Perches							$line_fixed = 1;
32153705ce5bSJoe Perches						}
3216653d4876SAndy Whitcroft					}
3217773647a0SAndy Whitcroft					if ($ctx =~ /ExW/) {
32183705ce5bSJoe Perches						if (ERROR("SPACING",
32193705ce5bSJoe Perches							  "space prohibited after that '$op' $at\n" . $hereptr)) {
3220b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
32213705ce5bSJoe Perches							if (defined $fix_elements[$n + 2]) {
32223705ce5bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
3223773647a0SAndy Whitcroft							}
3224b34c648bSJoe Perches							$line_fixed = 1;
32253705ce5bSJoe Perches						}
32263705ce5bSJoe Perches					}
32270a920b5bSAndy Whitcroft
32280a920b5bSAndy Whitcroft				# << and >> may either have or not have spaces both sides
32299c0ca6f9SAndy Whitcroft				} elsif ($op eq '<<' or $op eq '>>' or
32309c0ca6f9SAndy Whitcroft					 $op eq '&' or $op eq '^' or $op eq '|' or
32319c0ca6f9SAndy Whitcroft					 $op eq '+' or $op eq '-' or
3232c2fdda0dSAndy Whitcroft					 $op eq '*' or $op eq '/' or
3233c2fdda0dSAndy Whitcroft					 $op eq '%')
32340a920b5bSAndy Whitcroft				{
3235773647a0SAndy Whitcroft					if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
32363705ce5bSJoe Perches						if (ERROR("SPACING",
32373705ce5bSJoe Perches							  "need consistent spacing around '$op' $at\n" . $hereptr)) {
3238b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
3239b34c648bSJoe Perches							if (defined $fix_elements[$n + 2]) {
3240b34c648bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
3241b34c648bSJoe Perches							}
32423705ce5bSJoe Perches							$line_fixed = 1;
32433705ce5bSJoe Perches						}
32440a920b5bSAndy Whitcroft					}
32450a920b5bSAndy Whitcroft
32461f65f947SAndy Whitcroft				# A colon needs no spaces before when it is
32471f65f947SAndy Whitcroft				# terminating a case value or a label.
32481f65f947SAndy Whitcroft				} elsif ($opv eq ':C' || $opv eq ':L') {
32491f65f947SAndy Whitcroft					if ($ctx =~ /Wx./) {
32503705ce5bSJoe Perches						if (ERROR("SPACING",
32513705ce5bSJoe Perches							  "space prohibited before that '$op' $at\n" . $hereptr)) {
3252b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
32533705ce5bSJoe Perches							$line_fixed = 1;
32543705ce5bSJoe Perches						}
32551f65f947SAndy Whitcroft					}
32561f65f947SAndy Whitcroft
32570a920b5bSAndy Whitcroft				# All the others need spaces both sides.
3258cf655043SAndy Whitcroft				} elsif ($ctx !~ /[EWC]x[CWE]/) {
32591f65f947SAndy Whitcroft					my $ok = 0;
32601f65f947SAndy Whitcroft
326122f2a2efSAndy Whitcroft					# Ignore email addresses <foo@bar>
32621f65f947SAndy Whitcroft					if (($op eq '<' &&
32631f65f947SAndy Whitcroft					     $cc =~ /^\S+\@\S+>/) ||
32641f65f947SAndy Whitcroft					    ($op eq '>' &&
32651f65f947SAndy Whitcroft					     $ca =~ /<\S+\@\S+$/))
32661f65f947SAndy Whitcroft					{
32671f65f947SAndy Whitcroft					    	$ok = 1;
32681f65f947SAndy Whitcroft					}
32691f65f947SAndy Whitcroft
327084731623SJoe Perches					# messages are ERROR, but ?: are CHK
32711f65f947SAndy Whitcroft					if ($ok == 0) {
327284731623SJoe Perches						my $msg_type = \&ERROR;
327384731623SJoe Perches						$msg_type = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
327484731623SJoe Perches
327584731623SJoe Perches						if (&{$msg_type}("SPACING",
32763705ce5bSJoe Perches								 "spaces required around that '$op' $at\n" . $hereptr)) {
3277b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
3278b34c648bSJoe Perches							if (defined $fix_elements[$n + 2]) {
3279b34c648bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
3280b34c648bSJoe Perches							}
32813705ce5bSJoe Perches							$line_fixed = 1;
32823705ce5bSJoe Perches						}
32830a920b5bSAndy Whitcroft					}
328422f2a2efSAndy Whitcroft				}
32854a0df2efSAndy Whitcroft				$off += length($elements[$n + 1]);
32863705ce5bSJoe Perches
32873705ce5bSJoe Perches##				print("n: <$n> GOOD: <$good>\n");
32883705ce5bSJoe Perches
32893705ce5bSJoe Perches				$fixed_line = $fixed_line . $good;
32900a920b5bSAndy Whitcroft			}
32913705ce5bSJoe Perches
32923705ce5bSJoe Perches			if (($#elements % 2) == 0) {
32933705ce5bSJoe Perches				$fixed_line = $fixed_line . $fix_elements[$#elements];
32943705ce5bSJoe Perches			}
32953705ce5bSJoe Perches
32963705ce5bSJoe Perches			if ($fix && $line_fixed && $fixed_line ne $fixed[$linenr - 1]) {
32973705ce5bSJoe Perches				$fixed[$linenr - 1] = $fixed_line;
32983705ce5bSJoe Perches			}
32993705ce5bSJoe Perches
33003705ce5bSJoe Perches
33010a920b5bSAndy Whitcroft		}
33020a920b5bSAndy Whitcroft
3303786b6326SJoe Perches# check for whitespace before a non-naked semicolon
3304d2e248e7SJoe Perches		if ($line =~ /^\+.*\S\s+;\s*$/) {
3305786b6326SJoe Perches			if (WARN("SPACING",
3306786b6326SJoe Perches				 "space prohibited before semicolon\n" . $herecurr) &&
3307786b6326SJoe Perches			    $fix) {
3308786b6326SJoe Perches				1 while $fixed[$linenr - 1] =~
3309786b6326SJoe Perches				    s/^(\+.*\S)\s+;/$1;/;
3310786b6326SJoe Perches			}
3311786b6326SJoe Perches		}
3312786b6326SJoe Perches
3313f0a594c1SAndy Whitcroft# check for multiple assignments
3314f0a594c1SAndy Whitcroft		if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
3315000d1cc1SJoe Perches			CHK("MULTIPLE_ASSIGNMENTS",
3316000d1cc1SJoe Perches			    "multiple assignments should be avoided\n" . $herecurr);
3317f0a594c1SAndy Whitcroft		}
3318f0a594c1SAndy Whitcroft
331922f2a2efSAndy Whitcroft## # check for multiple declarations, allowing for a function declaration
332022f2a2efSAndy Whitcroft## # continuation.
332122f2a2efSAndy Whitcroft## 		if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
332222f2a2efSAndy Whitcroft## 		    $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
332322f2a2efSAndy Whitcroft##
332422f2a2efSAndy Whitcroft## 			# Remove any bracketed sections to ensure we do not
332522f2a2efSAndy Whitcroft## 			# falsly report the parameters of functions.
332622f2a2efSAndy Whitcroft## 			my $ln = $line;
332722f2a2efSAndy Whitcroft## 			while ($ln =~ s/\([^\(\)]*\)//g) {
332822f2a2efSAndy Whitcroft## 			}
332922f2a2efSAndy Whitcroft## 			if ($ln =~ /,/) {
3330000d1cc1SJoe Perches## 				WARN("MULTIPLE_DECLARATION",
3331000d1cc1SJoe Perches##				     "declaring multiple variables together should be avoided\n" . $herecurr);
333222f2a2efSAndy Whitcroft## 			}
333322f2a2efSAndy Whitcroft## 		}
3334f0a594c1SAndy Whitcroft
33350a920b5bSAndy Whitcroft#need space before brace following if, while, etc
333622f2a2efSAndy Whitcroft		if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
333722f2a2efSAndy Whitcroft		    $line =~ /do{/) {
33383705ce5bSJoe Perches			if (ERROR("SPACING",
33393705ce5bSJoe Perches				  "space required before the open brace '{'\n" . $herecurr) &&
33403705ce5bSJoe Perches			    $fix) {
3341d5e616fcSJoe Perches				$fixed[$linenr - 1] =~ s/^(\+.*(?:do|\))){/$1 {/;
33423705ce5bSJoe Perches			}
3343de7d4f0eSAndy Whitcroft		}
3344de7d4f0eSAndy Whitcroft
3345c4a62ef9SJoe Perches## # check for blank lines before declarations
3346c4a62ef9SJoe Perches##		if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
3347c4a62ef9SJoe Perches##		    $prevrawline =~ /^.\s*$/) {
3348c4a62ef9SJoe Perches##			WARN("SPACING",
3349c4a62ef9SJoe Perches##			     "No blank lines before declarations\n" . $hereprev);
3350c4a62ef9SJoe Perches##		}
3351c4a62ef9SJoe Perches##
3352c4a62ef9SJoe Perches
3353de7d4f0eSAndy Whitcroft# closing brace should have a space following it when it has anything
3354de7d4f0eSAndy Whitcroft# on the line
3355de7d4f0eSAndy Whitcroft		if ($line =~ /}(?!(?:,|;|\)))\S/) {
3356d5e616fcSJoe Perches			if (ERROR("SPACING",
3357d5e616fcSJoe Perches				  "space required after that close brace '}'\n" . $herecurr) &&
3358d5e616fcSJoe Perches			    $fix) {
3359d5e616fcSJoe Perches				$fixed[$linenr - 1] =~
3360d5e616fcSJoe Perches				    s/}((?!(?:,|;|\)))\S)/} $1/;
3361d5e616fcSJoe Perches			}
33620a920b5bSAndy Whitcroft		}
33630a920b5bSAndy Whitcroft
336422f2a2efSAndy Whitcroft# check spacing on square brackets
336522f2a2efSAndy Whitcroft		if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
33663705ce5bSJoe Perches			if (ERROR("SPACING",
33673705ce5bSJoe Perches				  "space prohibited after that open square bracket '['\n" . $herecurr) &&
33683705ce5bSJoe Perches			    $fix) {
33693705ce5bSJoe Perches				$fixed[$linenr - 1] =~
33703705ce5bSJoe Perches				    s/\[\s+/\[/;
33713705ce5bSJoe Perches			}
337222f2a2efSAndy Whitcroft		}
337322f2a2efSAndy Whitcroft		if ($line =~ /\s\]/) {
33743705ce5bSJoe Perches			if (ERROR("SPACING",
33753705ce5bSJoe Perches				  "space prohibited before that close square bracket ']'\n" . $herecurr) &&
33763705ce5bSJoe Perches			    $fix) {
33773705ce5bSJoe Perches				$fixed[$linenr - 1] =~
33783705ce5bSJoe Perches				    s/\s+\]/\]/;
33793705ce5bSJoe Perches			}
338022f2a2efSAndy Whitcroft		}
338122f2a2efSAndy Whitcroft
3382c45dcabdSAndy Whitcroft# check spacing on parentheses
33839c0ca6f9SAndy Whitcroft		if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
33849c0ca6f9SAndy Whitcroft		    $line !~ /for\s*\(\s+;/) {
33853705ce5bSJoe Perches			if (ERROR("SPACING",
33863705ce5bSJoe Perches				  "space prohibited after that open parenthesis '('\n" . $herecurr) &&
33873705ce5bSJoe Perches			    $fix) {
33883705ce5bSJoe Perches				$fixed[$linenr - 1] =~
33893705ce5bSJoe Perches				    s/\(\s+/\(/;
33903705ce5bSJoe Perches			}
339122f2a2efSAndy Whitcroft		}
339213214adfSAndy Whitcroft		if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
3393c45dcabdSAndy Whitcroft		    $line !~ /for\s*\(.*;\s+\)/ &&
3394c45dcabdSAndy Whitcroft		    $line !~ /:\s+\)/) {
33953705ce5bSJoe Perches			if (ERROR("SPACING",
33963705ce5bSJoe Perches				  "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
33973705ce5bSJoe Perches			    $fix) {
33983705ce5bSJoe Perches				$fixed[$linenr - 1] =~
33993705ce5bSJoe Perches				    s/\s+\)/\)/;
34003705ce5bSJoe Perches			}
340122f2a2efSAndy Whitcroft		}
340222f2a2efSAndy Whitcroft
34030a920b5bSAndy Whitcroft#goto labels aren't indented, allow a single space however
34044a0df2efSAndy Whitcroft		if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
34050a920b5bSAndy Whitcroft		   !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
34063705ce5bSJoe Perches			if (WARN("INDENTED_LABEL",
34073705ce5bSJoe Perches				 "labels should not be indented\n" . $herecurr) &&
34083705ce5bSJoe Perches			    $fix) {
34093705ce5bSJoe Perches				$fixed[$linenr - 1] =~
34103705ce5bSJoe Perches				    s/^(.)\s+/$1/;
34113705ce5bSJoe Perches			}
34120a920b5bSAndy Whitcroft		}
34130a920b5bSAndy Whitcroft
34145b9553abSJoe Perches# return is not a function
3415507e5141SJoe Perches		if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {
3416c45dcabdSAndy Whitcroft			my $spacing = $1;
3417507e5141SJoe Perches			if ($^V && $^V ge 5.10.0 &&
34185b9553abSJoe Perches			    $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) {
34195b9553abSJoe Perches				my $value = $1;
34205b9553abSJoe Perches				$value = deparenthesize($value);
34215b9553abSJoe Perches				if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) {
3422000d1cc1SJoe Perches					ERROR("RETURN_PARENTHESES",
3423000d1cc1SJoe Perches					      "return is not a function, parentheses are not required\n" . $herecurr);
34245b9553abSJoe Perches				}
3425c45dcabdSAndy Whitcroft			} elsif ($spacing !~ /\s+/) {
3426000d1cc1SJoe Perches				ERROR("SPACING",
3427000d1cc1SJoe Perches				      "space required before the open parenthesis '('\n" . $herecurr);
3428c45dcabdSAndy Whitcroft			}
3429c45dcabdSAndy Whitcroft		}
3430507e5141SJoe Perches
3431189248d8SJoe Perches# if statements using unnecessary parentheses - ie: if ((foo == bar))
3432189248d8SJoe Perches		if ($^V && $^V ge 5.10.0 &&
3433189248d8SJoe Perches		    $line =~ /\bif\s*((?:\(\s*){2,})/) {
3434189248d8SJoe Perches			my $openparens = $1;
3435189248d8SJoe Perches			my $count = $openparens =~ tr@\(@\(@;
3436189248d8SJoe Perches			my $msg = "";
3437189248d8SJoe Perches			if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) {
3438189248d8SJoe Perches				my $comp = $4;	#Not $1 because of $LvalOrFunc
3439189248d8SJoe Perches				$msg = " - maybe == should be = ?" if ($comp eq "==");
3440189248d8SJoe Perches				WARN("UNNECESSARY_PARENTHESES",
3441189248d8SJoe Perches				     "Unnecessary parentheses$msg\n" . $herecurr);
3442189248d8SJoe Perches			}
3443189248d8SJoe Perches		}
3444189248d8SJoe Perches
344553a3c448SAndy Whitcroft# Return of what appears to be an errno should normally be -'ve
344653a3c448SAndy Whitcroft		if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) {
344753a3c448SAndy Whitcroft			my $name = $1;
344853a3c448SAndy Whitcroft			if ($name ne 'EOF' && $name ne 'ERROR') {
3449000d1cc1SJoe Perches				WARN("USE_NEGATIVE_ERRNO",
3450000d1cc1SJoe Perches				     "return of an errno should typically be -ve (return -$1)\n" . $herecurr);
345153a3c448SAndy Whitcroft			}
345253a3c448SAndy Whitcroft		}
3453c45dcabdSAndy Whitcroft
34540a920b5bSAndy Whitcroft# Need a space before open parenthesis after if, while etc
34554a0df2efSAndy Whitcroft		if ($line =~ /\b(if|while|for|switch)\(/) {
34563705ce5bSJoe Perches			if (ERROR("SPACING",
34573705ce5bSJoe Perches				  "space required before the open parenthesis '('\n" . $herecurr) &&
34583705ce5bSJoe Perches			    $fix) {
34593705ce5bSJoe Perches				$fixed[$linenr - 1] =~
34603705ce5bSJoe Perches				    s/\b(if|while|for|switch)\(/$1 \(/;
34613705ce5bSJoe Perches			}
34620a920b5bSAndy Whitcroft		}
34630a920b5bSAndy Whitcroft
3464f5fe35ddSAndy Whitcroft# Check for illegal assignment in if conditional -- and check for trailing
3465f5fe35ddSAndy Whitcroft# statements after the conditional.
3466170d3a22SAndy Whitcroft		if ($line =~ /do\s*(?!{)/) {
34673e469cdcSAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
34683e469cdcSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0)
34693e469cdcSAndy Whitcroft					if (!defined $stat);
3470170d3a22SAndy Whitcroft			my ($stat_next) = ctx_statement_block($line_nr_next,
3471170d3a22SAndy Whitcroft						$remain_next, $off_next);
3472170d3a22SAndy Whitcroft			$stat_next =~ s/\n./\n /g;
3473170d3a22SAndy Whitcroft			##print "stat<$stat> stat_next<$stat_next>\n";
3474170d3a22SAndy Whitcroft
3475170d3a22SAndy Whitcroft			if ($stat_next =~ /^\s*while\b/) {
3476170d3a22SAndy Whitcroft				# If the statement carries leading newlines,
3477170d3a22SAndy Whitcroft				# then count those as offsets.
3478170d3a22SAndy Whitcroft				my ($whitespace) =
3479170d3a22SAndy Whitcroft					($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
3480170d3a22SAndy Whitcroft				my $offset =
3481170d3a22SAndy Whitcroft					statement_rawlines($whitespace) - 1;
3482170d3a22SAndy Whitcroft
3483170d3a22SAndy Whitcroft				$suppress_whiletrailers{$line_nr_next +
3484170d3a22SAndy Whitcroft								$offset} = 1;
3485170d3a22SAndy Whitcroft			}
3486170d3a22SAndy Whitcroft		}
3487170d3a22SAndy Whitcroft		if (!defined $suppress_whiletrailers{$linenr} &&
3488c11230f4SJoe Perches		    defined($stat) && defined($cond) &&
3489170d3a22SAndy Whitcroft		    $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
3490171ae1a4SAndy Whitcroft			my ($s, $c) = ($stat, $cond);
34918905a67cSAndy Whitcroft
3492b53c8e10SAndy Whitcroft			if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
3493000d1cc1SJoe Perches				ERROR("ASSIGN_IN_IF",
3494000d1cc1SJoe Perches				      "do not use assignment in if condition\n" . $herecurr);
34958905a67cSAndy Whitcroft			}
34968905a67cSAndy Whitcroft
34978905a67cSAndy Whitcroft			# Find out what is on the end of the line after the
34988905a67cSAndy Whitcroft			# conditional.
3499773647a0SAndy Whitcroft			substr($s, 0, length($c), '');
35008905a67cSAndy Whitcroft			$s =~ s/\n.*//g;
350113214adfSAndy Whitcroft			$s =~ s/$;//g; 	# Remove any comments
350253210168SAndy Whitcroft			if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
350353210168SAndy Whitcroft			    $c !~ /}\s*while\s*/)
3504773647a0SAndy Whitcroft			{
3505bb44ad39SAndy Whitcroft				# Find out how long the conditional actually is.
3506bb44ad39SAndy Whitcroft				my @newlines = ($c =~ /\n/gs);
3507bb44ad39SAndy Whitcroft				my $cond_lines = 1 + $#newlines;
350842bdf74cSHidetoshi Seto				my $stat_real = '';
3509bb44ad39SAndy Whitcroft
351042bdf74cSHidetoshi Seto				$stat_real = raw_line($linenr, $cond_lines)
351142bdf74cSHidetoshi Seto							. "\n" if ($cond_lines);
3512bb44ad39SAndy Whitcroft				if (defined($stat_real) && $cond_lines > 1) {
3513bb44ad39SAndy Whitcroft					$stat_real = "[...]\n$stat_real";
3514bb44ad39SAndy Whitcroft				}
3515bb44ad39SAndy Whitcroft
3516000d1cc1SJoe Perches				ERROR("TRAILING_STATEMENTS",
3517000d1cc1SJoe Perches				      "trailing statements should be on next line\n" . $herecurr . $stat_real);
35188905a67cSAndy Whitcroft			}
35198905a67cSAndy Whitcroft		}
35208905a67cSAndy Whitcroft
352113214adfSAndy Whitcroft# Check for bitwise tests written as boolean
352213214adfSAndy Whitcroft		if ($line =~ /
352313214adfSAndy Whitcroft			(?:
352413214adfSAndy Whitcroft				(?:\[|\(|\&\&|\|\|)
352513214adfSAndy Whitcroft				\s*0[xX][0-9]+\s*
352613214adfSAndy Whitcroft				(?:\&\&|\|\|)
352713214adfSAndy Whitcroft			|
352813214adfSAndy Whitcroft				(?:\&\&|\|\|)
352913214adfSAndy Whitcroft				\s*0[xX][0-9]+\s*
353013214adfSAndy Whitcroft				(?:\&\&|\|\||\)|\])
353113214adfSAndy Whitcroft			)/x)
353213214adfSAndy Whitcroft		{
3533000d1cc1SJoe Perches			WARN("HEXADECIMAL_BOOLEAN_TEST",
3534000d1cc1SJoe Perches			     "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
353513214adfSAndy Whitcroft		}
353613214adfSAndy Whitcroft
35378905a67cSAndy Whitcroft# if and else should not have general statements after it
353813214adfSAndy Whitcroft		if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
353913214adfSAndy Whitcroft			my $s = $1;
354013214adfSAndy Whitcroft			$s =~ s/$;//g; 	# Remove any comments
354113214adfSAndy Whitcroft			if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
3542000d1cc1SJoe Perches				ERROR("TRAILING_STATEMENTS",
3543000d1cc1SJoe Perches				      "trailing statements should be on next line\n" . $herecurr);
35440a920b5bSAndy Whitcroft			}
354513214adfSAndy Whitcroft		}
354639667782SAndy Whitcroft# if should not continue a brace
354739667782SAndy Whitcroft		if ($line =~ /}\s*if\b/) {
3548000d1cc1SJoe Perches			ERROR("TRAILING_STATEMENTS",
3549000d1cc1SJoe Perches			      "trailing statements should be on next line\n" .
355039667782SAndy Whitcroft				$herecurr);
355139667782SAndy Whitcroft		}
3552a1080bf8SAndy Whitcroft# case and default should not have general statements after them
3553a1080bf8SAndy Whitcroft		if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
3554a1080bf8SAndy Whitcroft		    $line !~ /\G(?:
35553fef12d6SAndy Whitcroft			(?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
3556a1080bf8SAndy Whitcroft			\s*return\s+
3557a1080bf8SAndy Whitcroft		    )/xg)
3558a1080bf8SAndy Whitcroft		{
3559000d1cc1SJoe Perches			ERROR("TRAILING_STATEMENTS",
3560000d1cc1SJoe Perches			      "trailing statements should be on next line\n" . $herecurr);
3561a1080bf8SAndy Whitcroft		}
35620a920b5bSAndy Whitcroft
35630a920b5bSAndy Whitcroft		# Check for }<nl>else {, these must be at the same
35640a920b5bSAndy Whitcroft		# indent level to be relevant to each other.
35650a920b5bSAndy Whitcroft		if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
35660a920b5bSAndy Whitcroft						$previndent == $indent) {
3567000d1cc1SJoe Perches			ERROR("ELSE_AFTER_BRACE",
3568000d1cc1SJoe Perches			      "else should follow close brace '}'\n" . $hereprev);
35690a920b5bSAndy Whitcroft		}
35700a920b5bSAndy Whitcroft
3571c2fdda0dSAndy Whitcroft		if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and
3572c2fdda0dSAndy Whitcroft						$previndent == $indent) {
3573c2fdda0dSAndy Whitcroft			my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
3574c2fdda0dSAndy Whitcroft
3575c2fdda0dSAndy Whitcroft			# Find out what is on the end of the line after the
3576c2fdda0dSAndy Whitcroft			# conditional.
3577773647a0SAndy Whitcroft			substr($s, 0, length($c), '');
3578c2fdda0dSAndy Whitcroft			$s =~ s/\n.*//g;
3579c2fdda0dSAndy Whitcroft
3580c2fdda0dSAndy Whitcroft			if ($s =~ /^\s*;/) {
3581000d1cc1SJoe Perches				ERROR("WHILE_AFTER_BRACE",
3582000d1cc1SJoe Perches				      "while should follow close brace '}'\n" . $hereprev);
3583c2fdda0dSAndy Whitcroft			}
3584c2fdda0dSAndy Whitcroft		}
3585c2fdda0dSAndy Whitcroft
358695e2c602SJoe Perches#Specific variable tests
3587323c1260SJoe Perches		while ($line =~ m{($Constant|$Lval)}g) {
3588323c1260SJoe Perches			my $var = $1;
358995e2c602SJoe Perches
359095e2c602SJoe Perches#gcc binary extension
359195e2c602SJoe Perches			if ($var =~ /^$Binary$/) {
3592d5e616fcSJoe Perches				if (WARN("GCC_BINARY_CONSTANT",
3593d5e616fcSJoe Perches					 "Avoid gcc v4.3+ binary constant extension: <$var>\n" . $herecurr) &&
3594d5e616fcSJoe Perches				    $fix) {
3595d5e616fcSJoe Perches					my $hexval = sprintf("0x%x", oct($var));
3596d5e616fcSJoe Perches					$fixed[$linenr - 1] =~
3597d5e616fcSJoe Perches					    s/\b$var\b/$hexval/;
3598d5e616fcSJoe Perches				}
359995e2c602SJoe Perches			}
360095e2c602SJoe Perches
360195e2c602SJoe Perches#CamelCase
3602807bd26cSJoe Perches			if ($var !~ /^$Constant$/ &&
3603be79794bSJoe Perches			    $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
360422735ce8SJoe Perches#Ignore Page<foo> variants
3605807bd26cSJoe Perches			    $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
360622735ce8SJoe Perches#Ignore SI style variants like nS, mV and dB (ie: max_uV, regulator_min_uA_show)
36073445686aSJoe Perches			    $var !~ /^(?:[a-z_]*?)_?[a-z][A-Z](?:_[a-z_]+)?$/) {
36087e781f67SJoe Perches				while ($var =~ m{($Ident)}g) {
36097e781f67SJoe Perches					my $word = $1;
36107e781f67SJoe Perches					next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
3611d8b07710SJoe Perches					if ($check) {
3612d8b07710SJoe Perches						seed_camelcase_includes();
3613d8b07710SJoe Perches						if (!$file && !$camelcase_file_seeded) {
3614d8b07710SJoe Perches							seed_camelcase_file($realfile);
3615d8b07710SJoe Perches							$camelcase_file_seeded = 1;
3616d8b07710SJoe Perches						}
3617d8b07710SJoe Perches					}
36187e781f67SJoe Perches					if (!defined $camelcase{$word}) {
36197e781f67SJoe Perches						$camelcase{$word} = 1;
3620be79794bSJoe Perches						CHK("CAMELCASE",
36217e781f67SJoe Perches						    "Avoid CamelCase: <$word>\n" . $herecurr);
36227e781f67SJoe Perches					}
3623323c1260SJoe Perches				}
3624323c1260SJoe Perches			}
36253445686aSJoe Perches		}
36260a920b5bSAndy Whitcroft
36270a920b5bSAndy Whitcroft#no spaces allowed after \ in define
3628d5e616fcSJoe Perches		if ($line =~ /\#\s*define.*\\\s+$/) {
3629d5e616fcSJoe Perches			if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
3630d5e616fcSJoe Perches				 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
3631d5e616fcSJoe Perches			    $fix) {
3632d5e616fcSJoe Perches				$fixed[$linenr - 1] =~ s/\s+$//;
3633d5e616fcSJoe Perches			}
36340a920b5bSAndy Whitcroft		}
36350a920b5bSAndy Whitcroft
3636653d4876SAndy Whitcroft#warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
3637c45dcabdSAndy Whitcroft		if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
3638e09dec48SAndy Whitcroft			my $file = "$1.h";
3639e09dec48SAndy Whitcroft			my $checkfile = "include/linux/$file";
3640e09dec48SAndy Whitcroft			if (-f "$root/$checkfile" &&
3641e09dec48SAndy Whitcroft			    $realfile ne $checkfile &&
36427840a94cSWolfram Sang			    $1 !~ /$allowed_asm_includes/)
3643c45dcabdSAndy Whitcroft			{
3644e09dec48SAndy Whitcroft				if ($realfile =~ m{^arch/}) {
3645000d1cc1SJoe Perches					CHK("ARCH_INCLUDE_LINUX",
3646000d1cc1SJoe Perches					    "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
3647e09dec48SAndy Whitcroft				} else {
3648000d1cc1SJoe Perches					WARN("INCLUDE_LINUX",
3649000d1cc1SJoe Perches					     "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
3650e09dec48SAndy Whitcroft				}
36510a920b5bSAndy Whitcroft			}
36520a920b5bSAndy Whitcroft		}
36530a920b5bSAndy Whitcroft
3654653d4876SAndy Whitcroft# multi-statement macros should be enclosed in a do while loop, grab the
3655653d4876SAndy Whitcroft# first statement and ensure its the whole macro if its not enclosed
3656cf655043SAndy Whitcroft# in a known good container
3657b8f96a31SAndy Whitcroft		if ($realfile !~ m@/vmlinux.lds.h$@ &&
3658b8f96a31SAndy Whitcroft		    $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
3659d8aaf121SAndy Whitcroft			my $ln = $linenr;
3660d8aaf121SAndy Whitcroft			my $cnt = $realcnt;
3661c45dcabdSAndy Whitcroft			my ($off, $dstat, $dcond, $rest);
3662c45dcabdSAndy Whitcroft			my $ctx = '';
3663c45dcabdSAndy Whitcroft			($dstat, $dcond, $ln, $cnt, $off) =
3664f74bd194SAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0);
3665f74bd194SAndy Whitcroft			$ctx = $dstat;
3666c45dcabdSAndy Whitcroft			#print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
3667a3bb97a7SAndy Whitcroft			#print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
3668c45dcabdSAndy Whitcroft
3669f74bd194SAndy Whitcroft			$dstat =~ s/^.\s*\#\s*define\s+$Ident(?:\([^\)]*\))?\s*//;
3670292f1a9bSAndy Whitcroft			$dstat =~ s/$;//g;
3671c45dcabdSAndy Whitcroft			$dstat =~ s/\\\n.//g;
3672c45dcabdSAndy Whitcroft			$dstat =~ s/^\s*//s;
3673c45dcabdSAndy Whitcroft			$dstat =~ s/\s*$//s;
3674c45dcabdSAndy Whitcroft
3675c45dcabdSAndy Whitcroft			# Flatten any parentheses and braces
3676bf30d6edSAndy Whitcroft			while ($dstat =~ s/\([^\(\)]*\)/1/ ||
3677bf30d6edSAndy Whitcroft			       $dstat =~ s/\{[^\{\}]*\}/1/ ||
3678c81769fdSAndy Whitcroft			       $dstat =~ s/\[[^\[\]]*\]/1/)
3679bf30d6edSAndy Whitcroft			{
3680c45dcabdSAndy Whitcroft			}
3681c45dcabdSAndy Whitcroft
3682e45bab8eSAndy Whitcroft			# Flatten any obvious string concatentation.
3683e45bab8eSAndy Whitcroft			while ($dstat =~ s/("X*")\s*$Ident/$1/ ||
3684e45bab8eSAndy Whitcroft			       $dstat =~ s/$Ident\s*("X*")/$1/)
3685e45bab8eSAndy Whitcroft			{
3686e45bab8eSAndy Whitcroft			}
3687e45bab8eSAndy Whitcroft
3688c45dcabdSAndy Whitcroft			my $exceptions = qr{
3689c45dcabdSAndy Whitcroft				$Declare|
3690c45dcabdSAndy Whitcroft				module_param_named|
3691a0a0a7a9SKees Cook				MODULE_PARM_DESC|
3692c45dcabdSAndy Whitcroft				DECLARE_PER_CPU|
3693c45dcabdSAndy Whitcroft				DEFINE_PER_CPU|
3694383099fdSAndy Whitcroft				__typeof__\(|
369522fd2d3eSStefani Seibold				union|
369622fd2d3eSStefani Seibold				struct|
3697ea71a0a0SAndy Whitcroft				\.$Ident\s*=\s*|
3698ea71a0a0SAndy Whitcroft				^\"|\"$
3699c45dcabdSAndy Whitcroft			}x;
37005eaa20b9SAndy Whitcroft			#print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
3701f74bd194SAndy Whitcroft			if ($dstat ne '' &&
3702f74bd194SAndy Whitcroft			    $dstat !~ /^(?:$Ident|-?$Constant),$/ &&			# 10, // foo(),
3703f74bd194SAndy Whitcroft			    $dstat !~ /^(?:$Ident|-?$Constant);$/ &&			# foo();
37043cc4b1c3SJoe Perches			    $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ &&		# 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
3705b9df76acSAndy Whitcroft			    $dstat !~ /^'X'$/ &&					# character constants
3706f74bd194SAndy Whitcroft			    $dstat !~ /$exceptions/ &&
3707f74bd194SAndy Whitcroft			    $dstat !~ /^\.$Ident\s*=/ &&				# .foo =
3708e942e2c3SJoe Perches			    $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ &&		# stringification #foo
370972f115f9SAndy Whitcroft			    $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ &&	# do {...} while (...); // do {...} while (...)
3710f74bd194SAndy Whitcroft			    $dstat !~ /^for\s*$Constant$/ &&				# for (...)
3711f74bd194SAndy Whitcroft			    $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ &&	# for (...) bar()
3712f74bd194SAndy Whitcroft			    $dstat !~ /^do\s*{/ &&					# do {...
3713f95a7e6aSJoe Perches			    $dstat !~ /^\({/ &&						# ({...
3714f95a7e6aSJoe Perches			    $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
3715c45dcabdSAndy Whitcroft			{
3716f74bd194SAndy Whitcroft				$ctx =~ s/\n*$//;
3717f74bd194SAndy Whitcroft				my $herectx = $here . "\n";
3718f74bd194SAndy Whitcroft				my $cnt = statement_rawlines($ctx);
3719f74bd194SAndy Whitcroft
3720f74bd194SAndy Whitcroft				for (my $n = 0; $n < $cnt; $n++) {
3721f74bd194SAndy Whitcroft					$herectx .= raw_line($linenr, $n) . "\n";
3722c45dcabdSAndy Whitcroft				}
3723c45dcabdSAndy Whitcroft
3724f74bd194SAndy Whitcroft				if ($dstat =~ /;/) {
3725f74bd194SAndy Whitcroft					ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
3726f74bd194SAndy Whitcroft					      "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
3727f74bd194SAndy Whitcroft				} else {
3728000d1cc1SJoe Perches					ERROR("COMPLEX_MACRO",
3729f74bd194SAndy Whitcroft					      "Macros with complex values should be enclosed in parenthesis\n" . "$herectx");
3730d8aaf121SAndy Whitcroft				}
37310a920b5bSAndy Whitcroft			}
37325023d347SJoe Perches
3733481eb486SJoe Perches# check for line continuations outside of #defines, preprocessor #, and asm
37345023d347SJoe Perches
37355023d347SJoe Perches		} else {
37365023d347SJoe Perches			if ($prevline !~ /^..*\\$/ &&
3737481eb486SJoe Perches			    $line !~ /^\+\s*\#.*\\$/ &&		# preprocessor
3738481eb486SJoe Perches			    $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ &&	# asm
37395023d347SJoe Perches			    $line =~ /^\+.*\\$/) {
37405023d347SJoe Perches				WARN("LINE_CONTINUATIONS",
37415023d347SJoe Perches				     "Avoid unnecessary line continuations\n" . $herecurr);
37425023d347SJoe Perches			}
3743653d4876SAndy Whitcroft		}
37440a920b5bSAndy Whitcroft
3745b13edf7fSJoe Perches# do {} while (0) macro tests:
3746b13edf7fSJoe Perches# single-statement macros do not need to be enclosed in do while (0) loop,
3747b13edf7fSJoe Perches# macro should not end with a semicolon
3748b13edf7fSJoe Perches		if ($^V && $^V ge 5.10.0 &&
3749b13edf7fSJoe Perches		    $realfile !~ m@/vmlinux.lds.h$@ &&
3750b13edf7fSJoe Perches		    $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
3751b13edf7fSJoe Perches			my $ln = $linenr;
3752b13edf7fSJoe Perches			my $cnt = $realcnt;
3753b13edf7fSJoe Perches			my ($off, $dstat, $dcond, $rest);
3754b13edf7fSJoe Perches			my $ctx = '';
3755b13edf7fSJoe Perches			($dstat, $dcond, $ln, $cnt, $off) =
3756b13edf7fSJoe Perches				ctx_statement_block($linenr, $realcnt, 0);
3757b13edf7fSJoe Perches			$ctx = $dstat;
3758b13edf7fSJoe Perches
3759b13edf7fSJoe Perches			$dstat =~ s/\\\n.//g;
3760b13edf7fSJoe Perches
3761b13edf7fSJoe Perches			if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
3762b13edf7fSJoe Perches				my $stmts = $2;
3763b13edf7fSJoe Perches				my $semis = $3;
3764b13edf7fSJoe Perches
3765b13edf7fSJoe Perches				$ctx =~ s/\n*$//;
3766b13edf7fSJoe Perches				my $cnt = statement_rawlines($ctx);
3767b13edf7fSJoe Perches				my $herectx = $here . "\n";
3768b13edf7fSJoe Perches
3769b13edf7fSJoe Perches				for (my $n = 0; $n < $cnt; $n++) {
3770b13edf7fSJoe Perches					$herectx .= raw_line($linenr, $n) . "\n";
3771b13edf7fSJoe Perches				}
3772b13edf7fSJoe Perches
3773ac8e97f8SJoe Perches				if (($stmts =~ tr/;/;/) == 1 &&
3774ac8e97f8SJoe Perches				    $stmts !~ /^\s*(if|while|for|switch)\b/) {
3775b13edf7fSJoe Perches					WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
3776b13edf7fSJoe Perches					     "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
3777b13edf7fSJoe Perches				}
3778b13edf7fSJoe Perches				if (defined $semis && $semis ne "") {
3779b13edf7fSJoe Perches					WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
3780b13edf7fSJoe Perches					     "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
3781b13edf7fSJoe Perches				}
3782b13edf7fSJoe Perches			}
3783b13edf7fSJoe Perches		}
3784b13edf7fSJoe Perches
3785080ba929SMike Frysinger# make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
3786080ba929SMike Frysinger# all assignments may have only one of the following with an assignment:
3787080ba929SMike Frysinger#	.
3788080ba929SMike Frysinger#	ALIGN(...)
3789080ba929SMike Frysinger#	VMLINUX_SYMBOL(...)
3790080ba929SMike Frysinger		if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
3791000d1cc1SJoe Perches			WARN("MISSING_VMLINUX_SYMBOL",
3792000d1cc1SJoe Perches			     "vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
3793080ba929SMike Frysinger		}
3794080ba929SMike Frysinger
3795f0a594c1SAndy Whitcroft# check for redundant bracing round if etc
379613214adfSAndy Whitcroft		if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
379713214adfSAndy Whitcroft			my ($level, $endln, @chunks) =
3798cf655043SAndy Whitcroft				ctx_statement_full($linenr, $realcnt, 1);
379913214adfSAndy Whitcroft			#print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
3800cf655043SAndy Whitcroft			#print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
3801cf655043SAndy Whitcroft			if ($#chunks > 0 && $level == 0) {
3802aad4f614SJoe Perches				my @allowed = ();
3803aad4f614SJoe Perches				my $allow = 0;
380413214adfSAndy Whitcroft				my $seen = 0;
3805773647a0SAndy Whitcroft				my $herectx = $here . "\n";
3806cf655043SAndy Whitcroft				my $ln = $linenr - 1;
380713214adfSAndy Whitcroft				for my $chunk (@chunks) {
380813214adfSAndy Whitcroft					my ($cond, $block) = @{$chunk};
380913214adfSAndy Whitcroft
3810773647a0SAndy Whitcroft					# If the condition carries leading newlines, then count those as offsets.
3811773647a0SAndy Whitcroft					my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
3812773647a0SAndy Whitcroft					my $offset = statement_rawlines($whitespace) - 1;
3813773647a0SAndy Whitcroft
3814aad4f614SJoe Perches					$allowed[$allow] = 0;
3815773647a0SAndy Whitcroft					#print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
3816773647a0SAndy Whitcroft
3817773647a0SAndy Whitcroft					# We have looked at and allowed this specific line.
3818773647a0SAndy Whitcroft					$suppress_ifbraces{$ln + $offset} = 1;
3819773647a0SAndy Whitcroft
3820773647a0SAndy Whitcroft					$herectx .= "$rawlines[$ln + $offset]\n[...]\n";
3821cf655043SAndy Whitcroft					$ln += statement_rawlines($block) - 1;
3822cf655043SAndy Whitcroft
3823773647a0SAndy Whitcroft					substr($block, 0, length($cond), '');
382413214adfSAndy Whitcroft
382513214adfSAndy Whitcroft					$seen++ if ($block =~ /^\s*{/);
382613214adfSAndy Whitcroft
3827aad4f614SJoe Perches					#print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
3828cf655043SAndy Whitcroft					if (statement_lines($cond) > 1) {
3829cf655043SAndy Whitcroft						#print "APW: ALLOWED: cond<$cond>\n";
3830aad4f614SJoe Perches						$allowed[$allow] = 1;
383113214adfSAndy Whitcroft					}
383213214adfSAndy Whitcroft					if ($block =~/\b(?:if|for|while)\b/) {
3833cf655043SAndy Whitcroft						#print "APW: ALLOWED: block<$block>\n";
3834aad4f614SJoe Perches						$allowed[$allow] = 1;
383513214adfSAndy Whitcroft					}
3836cf655043SAndy Whitcroft					if (statement_block_size($block) > 1) {
3837cf655043SAndy Whitcroft						#print "APW: ALLOWED: lines block<$block>\n";
3838aad4f614SJoe Perches						$allowed[$allow] = 1;
383913214adfSAndy Whitcroft					}
3840aad4f614SJoe Perches					$allow++;
384113214adfSAndy Whitcroft				}
3842aad4f614SJoe Perches				if ($seen) {
3843aad4f614SJoe Perches					my $sum_allowed = 0;
3844aad4f614SJoe Perches					foreach (@allowed) {
3845aad4f614SJoe Perches						$sum_allowed += $_;
3846aad4f614SJoe Perches					}
3847aad4f614SJoe Perches					if ($sum_allowed == 0) {
3848000d1cc1SJoe Perches						WARN("BRACES",
3849000d1cc1SJoe Perches						     "braces {} are not necessary for any arm of this statement\n" . $herectx);
3850aad4f614SJoe Perches					} elsif ($sum_allowed != $allow &&
3851aad4f614SJoe Perches						 $seen != $allow) {
3852aad4f614SJoe Perches						CHK("BRACES",
3853aad4f614SJoe Perches						    "braces {} should be used on all arms of this statement\n" . $herectx);
3854aad4f614SJoe Perches					}
385513214adfSAndy Whitcroft				}
385613214adfSAndy Whitcroft			}
385713214adfSAndy Whitcroft		}
3858773647a0SAndy Whitcroft		if (!defined $suppress_ifbraces{$linenr - 1} &&
385913214adfSAndy Whitcroft					$line =~ /\b(if|while|for|else)\b/) {
3860cf655043SAndy Whitcroft			my $allowed = 0;
3861f0a594c1SAndy Whitcroft
3862cf655043SAndy Whitcroft			# Check the pre-context.
3863cf655043SAndy Whitcroft			if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
3864cf655043SAndy Whitcroft				#print "APW: ALLOWED: pre<$1>\n";
3865cf655043SAndy Whitcroft				$allowed = 1;
3866f0a594c1SAndy Whitcroft			}
3867773647a0SAndy Whitcroft
3868773647a0SAndy Whitcroft			my ($level, $endln, @chunks) =
3869773647a0SAndy Whitcroft				ctx_statement_full($linenr, $realcnt, $-[0]);
3870773647a0SAndy Whitcroft
3871cf655043SAndy Whitcroft			# Check the condition.
3872cf655043SAndy Whitcroft			my ($cond, $block) = @{$chunks[0]};
3873773647a0SAndy Whitcroft			#print "CHECKING<$linenr> cond<$cond> block<$block>\n";
3874cf655043SAndy Whitcroft			if (defined $cond) {
3875773647a0SAndy Whitcroft				substr($block, 0, length($cond), '');
3876cf655043SAndy Whitcroft			}
3877cf655043SAndy Whitcroft			if (statement_lines($cond) > 1) {
3878cf655043SAndy Whitcroft				#print "APW: ALLOWED: cond<$cond>\n";
3879cf655043SAndy Whitcroft				$allowed = 1;
3880cf655043SAndy Whitcroft			}
3881cf655043SAndy Whitcroft			if ($block =~/\b(?:if|for|while)\b/) {
3882cf655043SAndy Whitcroft				#print "APW: ALLOWED: block<$block>\n";
3883cf655043SAndy Whitcroft				$allowed = 1;
3884cf655043SAndy Whitcroft			}
3885cf655043SAndy Whitcroft			if (statement_block_size($block) > 1) {
3886cf655043SAndy Whitcroft				#print "APW: ALLOWED: lines block<$block>\n";
3887cf655043SAndy Whitcroft				$allowed = 1;
3888cf655043SAndy Whitcroft			}
3889cf655043SAndy Whitcroft			# Check the post-context.
3890cf655043SAndy Whitcroft			if (defined $chunks[1]) {
3891cf655043SAndy Whitcroft				my ($cond, $block) = @{$chunks[1]};
3892cf655043SAndy Whitcroft				if (defined $cond) {
3893773647a0SAndy Whitcroft					substr($block, 0, length($cond), '');
3894cf655043SAndy Whitcroft				}
3895cf655043SAndy Whitcroft				if ($block =~ /^\s*\{/) {
3896cf655043SAndy Whitcroft					#print "APW: ALLOWED: chunk-1 block<$block>\n";
3897cf655043SAndy Whitcroft					$allowed = 1;
3898cf655043SAndy Whitcroft				}
3899cf655043SAndy Whitcroft			}
3900cf655043SAndy Whitcroft			if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
390169932487SJustin P. Mattock				my $herectx = $here . "\n";
3902f055663cSAndy Whitcroft				my $cnt = statement_rawlines($block);
3903cf655043SAndy Whitcroft
3904f055663cSAndy Whitcroft				for (my $n = 0; $n < $cnt; $n++) {
390569932487SJustin P. Mattock					$herectx .= raw_line($linenr, $n) . "\n";
3906cf655043SAndy Whitcroft				}
3907cf655043SAndy Whitcroft
3908000d1cc1SJoe Perches				WARN("BRACES",
3909000d1cc1SJoe Perches				     "braces {} are not necessary for single statement blocks\n" . $herectx);
3910f0a594c1SAndy Whitcroft			}
3911f0a594c1SAndy Whitcroft		}
3912f0a594c1SAndy Whitcroft
39130979ae66SJoe Perches# check for unnecessary blank lines around braces
391477b9a53aSJoe Perches		if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
39150979ae66SJoe Perches			CHK("BRACES",
39160979ae66SJoe Perches			    "Blank lines aren't necessary before a close brace '}'\n" . $hereprev);
39170979ae66SJoe Perches		}
391877b9a53aSJoe Perches		if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
39190979ae66SJoe Perches			CHK("BRACES",
39200979ae66SJoe Perches			    "Blank lines aren't necessary after an open brace '{'\n" . $hereprev);
39210979ae66SJoe Perches		}
39220979ae66SJoe Perches
39234a0df2efSAndy Whitcroft# no volatiles please
39246c72ffaaSAndy Whitcroft		my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
39256c72ffaaSAndy Whitcroft		if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
3926000d1cc1SJoe Perches			WARN("VOLATILE",
3927000d1cc1SJoe Perches			     "Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
39284a0df2efSAndy Whitcroft		}
39294a0df2efSAndy Whitcroft
393000df344fSAndy Whitcroft# warn about #if 0
3931c45dcabdSAndy Whitcroft		if ($line =~ /^.\s*\#\s*if\s+0\b/) {
3932000d1cc1SJoe Perches			CHK("REDUNDANT_CODE",
3933000d1cc1SJoe Perches			    "if this code is redundant consider removing it\n" .
3934de7d4f0eSAndy Whitcroft				$herecurr);
39354a0df2efSAndy Whitcroft		}
39364a0df2efSAndy Whitcroft
393703df4b51SAndy Whitcroft# check for needless "if (<foo>) fn(<foo>)" uses
393803df4b51SAndy Whitcroft		if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
393903df4b51SAndy Whitcroft			my $expr = '\s*\(\s*' . quotemeta($1) . '\s*\)\s*;';
394003df4b51SAndy Whitcroft			if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?)$expr/) {
394103df4b51SAndy Whitcroft				WARN('NEEDLESS_IF',
394203df4b51SAndy Whitcroft				     "$1(NULL) is safe this check is probably not required\n" . $hereprev);
39434c432a8fSGreg Kroah-Hartman			}
39444c432a8fSGreg Kroah-Hartman		}
3945f0a594c1SAndy Whitcroft
39468716de38SJoe Perches# check for bad placement of section $InitAttribute (e.g.: __initdata)
39478716de38SJoe Perches		if ($line =~ /(\b$InitAttribute\b)/) {
39488716de38SJoe Perches			my $attr = $1;
39498716de38SJoe Perches			if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
39508716de38SJoe Perches				my $ptr = $1;
39518716de38SJoe Perches				my $var = $2;
39528716de38SJoe Perches				if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
39538716de38SJoe Perches				      ERROR("MISPLACED_INIT",
39548716de38SJoe Perches					    "$attr should be placed after $var\n" . $herecurr)) ||
39558716de38SJoe Perches				     ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
39568716de38SJoe Perches				      WARN("MISPLACED_INIT",
39578716de38SJoe Perches					   "$attr should be placed after $var\n" . $herecurr))) &&
39588716de38SJoe Perches				    $fix) {
39598716de38SJoe 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;
39608716de38SJoe Perches				}
39618716de38SJoe Perches			}
39628716de38SJoe Perches		}
39638716de38SJoe Perches
3964e970b884SJoe Perches# check for $InitAttributeData (ie: __initdata) with const
3965e970b884SJoe Perches		if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
3966e970b884SJoe Perches			my $attr = $1;
3967e970b884SJoe Perches			$attr =~ /($InitAttributePrefix)(.*)/;
3968e970b884SJoe Perches			my $attr_prefix = $1;
3969e970b884SJoe Perches			my $attr_type = $2;
3970e970b884SJoe Perches			if (ERROR("INIT_ATTRIBUTE",
3971e970b884SJoe Perches				  "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
3972e970b884SJoe Perches			    $fix) {
3973e970b884SJoe Perches				$fixed[$linenr - 1] =~
3974e970b884SJoe Perches				    s/$InitAttributeData/${attr_prefix}initconst/;
3975e970b884SJoe Perches			}
3976e970b884SJoe Perches		}
3977e970b884SJoe Perches
3978e970b884SJoe Perches# check for $InitAttributeConst (ie: __initconst) without const
3979e970b884SJoe Perches		if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
3980e970b884SJoe Perches			my $attr = $1;
3981e970b884SJoe Perches			if (ERROR("INIT_ATTRIBUTE",
3982e970b884SJoe Perches				  "Use of $attr requires a separate use of const\n" . $herecurr) &&
3983e970b884SJoe Perches			    $fix) {
3984e970b884SJoe Perches				my $lead = $fixed[$linenr - 1] =~
3985e970b884SJoe Perches				    /(^\+\s*(?:static\s+))/;
3986e970b884SJoe Perches				$lead = rtrim($1);
3987e970b884SJoe Perches				$lead = "$lead " if ($lead !~ /^\+$/);
3988e970b884SJoe Perches				$lead = "${lead}const ";
3989e970b884SJoe Perches				$fixed[$linenr - 1] =~ s/(^\+\s*(?:static\s+))/$lead/;
3990e970b884SJoe Perches			}
3991e970b884SJoe Perches		}
3992e970b884SJoe Perches
3993fbdb8138SJoe Perches# don't use __constant_<foo> functions outside of include/uapi/
3994fbdb8138SJoe Perches		if ($realfile !~ m@^include/uapi/@ &&
3995fbdb8138SJoe Perches		    $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) {
3996fbdb8138SJoe Perches			my $constant_func = $1;
3997fbdb8138SJoe Perches			my $func = $constant_func;
3998fbdb8138SJoe Perches			$func =~ s/^__constant_//;
3999fbdb8138SJoe Perches			if (WARN("CONSTANT_CONVERSION",
4000fbdb8138SJoe Perches				 "$constant_func should be $func\n" . $herecurr) &&
4001fbdb8138SJoe Perches			    $fix) {
4002fbdb8138SJoe Perches				$fixed[$linenr - 1] =~ s/\b$constant_func\b/$func/g;
4003fbdb8138SJoe Perches			}
4004fbdb8138SJoe Perches		}
4005fbdb8138SJoe Perches
40061a15a250SPatrick Pannuto# prefer usleep_range over udelay
400737581c28SBruce Allan		if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
400843c1d77cSJoe Perches			my $delay = $1;
40091a15a250SPatrick Pannuto			# ignore udelay's < 10, however
401043c1d77cSJoe Perches			if (! ($delay < 10) ) {
4011000d1cc1SJoe Perches				CHK("USLEEP_RANGE",
401243c1d77cSJoe Perches				    "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $herecurr);
401343c1d77cSJoe Perches			}
401443c1d77cSJoe Perches			if ($delay > 2000) {
401543c1d77cSJoe Perches				WARN("LONG_UDELAY",
401643c1d77cSJoe Perches				     "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr);
40171a15a250SPatrick Pannuto			}
40181a15a250SPatrick Pannuto		}
40191a15a250SPatrick Pannuto
402009ef8725SPatrick Pannuto# warn about unexpectedly long msleep's
402109ef8725SPatrick Pannuto		if ($line =~ /\bmsleep\s*\((\d+)\);/) {
402209ef8725SPatrick Pannuto			if ($1 < 20) {
4023000d1cc1SJoe Perches				WARN("MSLEEP",
402443c1d77cSJoe Perches				     "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $herecurr);
402509ef8725SPatrick Pannuto			}
402609ef8725SPatrick Pannuto		}
402709ef8725SPatrick Pannuto
402836ec1939SJoe Perches# check for comparisons of jiffies
402936ec1939SJoe Perches		if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
403036ec1939SJoe Perches			WARN("JIFFIES_COMPARISON",
403136ec1939SJoe Perches			     "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
403236ec1939SJoe Perches		}
403336ec1939SJoe Perches
40349d7a34a5SJoe Perches# check for comparisons of get_jiffies_64()
40359d7a34a5SJoe Perches		if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
40369d7a34a5SJoe Perches			WARN("JIFFIES_COMPARISON",
40379d7a34a5SJoe Perches			     "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
40389d7a34a5SJoe Perches		}
40399d7a34a5SJoe Perches
404000df344fSAndy Whitcroft# warn about #ifdefs in C files
4041c45dcabdSAndy Whitcroft#		if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
404200df344fSAndy Whitcroft#			print "#ifdef in C files should be avoided\n";
404300df344fSAndy Whitcroft#			print "$herecurr";
404400df344fSAndy Whitcroft#			$clean = 0;
404500df344fSAndy Whitcroft#		}
404600df344fSAndy Whitcroft
404722f2a2efSAndy Whitcroft# warn about spacing in #ifdefs
4048c45dcabdSAndy Whitcroft		if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
40493705ce5bSJoe Perches			if (ERROR("SPACING",
40503705ce5bSJoe Perches				  "exactly one space required after that #$1\n" . $herecurr) &&
40513705ce5bSJoe Perches			    $fix) {
40523705ce5bSJoe Perches				$fixed[$linenr - 1] =~
40533705ce5bSJoe Perches				    s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
40543705ce5bSJoe Perches			}
40553705ce5bSJoe Perches
405622f2a2efSAndy Whitcroft		}
405722f2a2efSAndy Whitcroft
40584a0df2efSAndy Whitcroft# check for spinlock_t definitions without a comment.
4059171ae1a4SAndy Whitcroft		if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
4060171ae1a4SAndy Whitcroft		    $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
40614a0df2efSAndy Whitcroft			my $which = $1;
40624a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
4063000d1cc1SJoe Perches				CHK("UNCOMMENTED_DEFINITION",
4064000d1cc1SJoe Perches				    "$1 definition without comment\n" . $herecurr);
40654a0df2efSAndy Whitcroft			}
40664a0df2efSAndy Whitcroft		}
40674a0df2efSAndy Whitcroft# check for memory barriers without a comment.
40684a0df2efSAndy Whitcroft		if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
40694a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
4070c1fd7bb9SJoe Perches				WARN("MEMORY_BARRIER",
4071000d1cc1SJoe Perches				     "memory barrier without comment\n" . $herecurr);
40724a0df2efSAndy Whitcroft			}
40734a0df2efSAndy Whitcroft		}
40744a0df2efSAndy Whitcroft# check of hardware specific defines
4075c45dcabdSAndy Whitcroft		if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
4076000d1cc1SJoe Perches			CHK("ARCH_DEFINES",
4077000d1cc1SJoe Perches			    "architecture specific defines should be avoided\n" .  $herecurr);
40780a920b5bSAndy Whitcroft		}
4079653d4876SAndy Whitcroft
4080d4977c78STobias Klauser# Check that the storage class is at the beginning of a declaration
4081d4977c78STobias Klauser		if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
4082000d1cc1SJoe Perches			WARN("STORAGE_CLASS",
4083000d1cc1SJoe Perches			     "storage class should be at the beginning of the declaration\n" . $herecurr)
4084d4977c78STobias Klauser		}
4085d4977c78STobias Klauser
4086de7d4f0eSAndy Whitcroft# check the location of the inline attribute, that it is between
4087de7d4f0eSAndy Whitcroft# storage class and type.
40889c0ca6f9SAndy Whitcroft		if ($line =~ /\b$Type\s+$Inline\b/ ||
40899c0ca6f9SAndy Whitcroft		    $line =~ /\b$Inline\s+$Storage\b/) {
4090000d1cc1SJoe Perches			ERROR("INLINE_LOCATION",
4091000d1cc1SJoe Perches			      "inline keyword should sit between storage class and type\n" . $herecurr);
4092de7d4f0eSAndy Whitcroft		}
4093de7d4f0eSAndy Whitcroft
40948905a67cSAndy Whitcroft# Check for __inline__ and __inline, prefer inline
40952b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
40962b7ab453SJoe Perches		    $line =~ /\b(__inline__|__inline)\b/) {
4097d5e616fcSJoe Perches			if (WARN("INLINE",
4098d5e616fcSJoe Perches				 "plain inline is preferred over $1\n" . $herecurr) &&
4099d5e616fcSJoe Perches			    $fix) {
4100d5e616fcSJoe Perches				$fixed[$linenr - 1] =~ s/\b(__inline__|__inline)\b/inline/;
4101d5e616fcSJoe Perches
4102d5e616fcSJoe Perches			}
41038905a67cSAndy Whitcroft		}
41048905a67cSAndy Whitcroft
41053d130fd0SJoe Perches# Check for __attribute__ packed, prefer __packed
41062b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
41072b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
4108000d1cc1SJoe Perches			WARN("PREFER_PACKED",
4109000d1cc1SJoe Perches			     "__packed is preferred over __attribute__((packed))\n" . $herecurr);
41103d130fd0SJoe Perches		}
41113d130fd0SJoe Perches
411239b7e287SJoe Perches# Check for __attribute__ aligned, prefer __aligned
41132b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
41142b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
4115000d1cc1SJoe Perches			WARN("PREFER_ALIGNED",
4116000d1cc1SJoe Perches			     "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
411739b7e287SJoe Perches		}
411839b7e287SJoe Perches
41195f14d3bdSJoe Perches# Check for __attribute__ format(printf, prefer __printf
41202b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
41212b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
4122d5e616fcSJoe Perches			if (WARN("PREFER_PRINTF",
4123d5e616fcSJoe Perches				 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) &&
4124d5e616fcSJoe Perches			    $fix) {
4125d5e616fcSJoe Perches				$fixed[$linenr - 1] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf\s*,\s*(.*)\)\s*\)\s*\)/"__printf(" . trim($1) . ")"/ex;
4126d5e616fcSJoe Perches
4127d5e616fcSJoe Perches			}
41285f14d3bdSJoe Perches		}
41295f14d3bdSJoe Perches
41306061d949SJoe Perches# Check for __attribute__ format(scanf, prefer __scanf
41312b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
41322b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
4133d5e616fcSJoe Perches			if (WARN("PREFER_SCANF",
4134d5e616fcSJoe Perches				 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) &&
4135d5e616fcSJoe Perches			    $fix) {
4136d5e616fcSJoe Perches				$fixed[$linenr - 1] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\s*,\s*(.*)\)\s*\)\s*\)/"__scanf(" . trim($1) . ")"/ex;
4137d5e616fcSJoe Perches			}
41386061d949SJoe Perches		}
41396061d949SJoe Perches
41408f53a9b8SJoe Perches# check for sizeof(&)
41418f53a9b8SJoe Perches		if ($line =~ /\bsizeof\s*\(\s*\&/) {
4142000d1cc1SJoe Perches			WARN("SIZEOF_ADDRESS",
4143000d1cc1SJoe Perches			     "sizeof(& should be avoided\n" . $herecurr);
41448f53a9b8SJoe Perches		}
41458f53a9b8SJoe Perches
414666c80b60SJoe Perches# check for sizeof without parenthesis
414766c80b60SJoe Perches		if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
4148d5e616fcSJoe Perches			if (WARN("SIZEOF_PARENTHESIS",
4149d5e616fcSJoe Perches				 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
4150d5e616fcSJoe Perches			    $fix) {
4151d5e616fcSJoe Perches				$fixed[$linenr - 1] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
4152d5e616fcSJoe Perches			}
415366c80b60SJoe Perches		}
415466c80b60SJoe Perches
4155428e2fdcSJoe Perches# check for line continuations in quoted strings with odd counts of "
4156428e2fdcSJoe Perches		if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) {
4157000d1cc1SJoe Perches			WARN("LINE_CONTINUATIONS",
4158000d1cc1SJoe Perches			     "Avoid line continuations in quoted strings\n" . $herecurr);
4159428e2fdcSJoe Perches		}
4160428e2fdcSJoe Perches
416188982feaSJoe Perches# check for struct spinlock declarations
416288982feaSJoe Perches		if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
416388982feaSJoe Perches			WARN("USE_SPINLOCK_T",
416488982feaSJoe Perches			     "struct spinlock should be spinlock_t\n" . $herecurr);
416588982feaSJoe Perches		}
416688982feaSJoe Perches
4167a6962d72SJoe Perches# check for seq_printf uses that could be seq_puts
416806668727SJoe Perches		if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
4169a6962d72SJoe Perches			my $fmt = get_quoted_string($line, $rawline);
417006668727SJoe Perches			if ($fmt ne "" && $fmt !~ /[^\\]\%/) {
4171d5e616fcSJoe Perches				if (WARN("PREFER_SEQ_PUTS",
4172d5e616fcSJoe Perches					 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
4173d5e616fcSJoe Perches				    $fix) {
4174d5e616fcSJoe Perches					$fixed[$linenr - 1] =~ s/\bseq_printf\b/seq_puts/;
4175d5e616fcSJoe Perches				}
4176a6962d72SJoe Perches			}
4177a6962d72SJoe Perches		}
4178a6962d72SJoe Perches
4179554e165cSAndy Whitcroft# Check for misused memsets
4180d1fe9c09SJoe Perches		if ($^V && $^V ge 5.10.0 &&
4181d1fe9c09SJoe Perches		    defined $stat &&
4182d7c76ba7SJoe Perches		    $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/s) {
4183554e165cSAndy Whitcroft
4184d7c76ba7SJoe Perches			my $ms_addr = $2;
4185d1fe9c09SJoe Perches			my $ms_val = $7;
4186d1fe9c09SJoe Perches			my $ms_size = $12;
4187d7c76ba7SJoe Perches
4188554e165cSAndy Whitcroft			if ($ms_size =~ /^(0x|)0$/i) {
4189554e165cSAndy Whitcroft				ERROR("MEMSET",
4190d7c76ba7SJoe Perches				      "memset to 0's uses 0 as the 2nd argument, not the 3rd\n" . "$here\n$stat\n");
4191554e165cSAndy Whitcroft			} elsif ($ms_size =~ /^(0x|)1$/i) {
4192554e165cSAndy Whitcroft				WARN("MEMSET",
4193d7c76ba7SJoe Perches				     "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
4194d7c76ba7SJoe Perches			}
4195d7c76ba7SJoe Perches		}
4196d7c76ba7SJoe Perches
419798a9bba5SJoe Perches# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar)
419898a9bba5SJoe Perches		if ($^V && $^V ge 5.10.0 &&
419998a9bba5SJoe Perches		    $line =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/s) {
420098a9bba5SJoe Perches			if (WARN("PREFER_ETHER_ADDR_COPY",
420198a9bba5SJoe Perches				 "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . $herecurr) &&
420298a9bba5SJoe Perches			    $fix) {
420398a9bba5SJoe Perches				$fixed[$linenr - 1] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/;
420498a9bba5SJoe Perches			}
420598a9bba5SJoe Perches		}
420698a9bba5SJoe Perches
4207d7c76ba7SJoe Perches# typecasts on min/max could be min_t/max_t
4208d1fe9c09SJoe Perches		if ($^V && $^V ge 5.10.0 &&
4209d1fe9c09SJoe Perches		    defined $stat &&
4210d7c76ba7SJoe Perches		    $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
4211d1fe9c09SJoe Perches			if (defined $2 || defined $7) {
4212d7c76ba7SJoe Perches				my $call = $1;
4213d7c76ba7SJoe Perches				my $cast1 = deparenthesize($2);
4214d7c76ba7SJoe Perches				my $arg1 = $3;
4215d1fe9c09SJoe Perches				my $cast2 = deparenthesize($7);
4216d1fe9c09SJoe Perches				my $arg2 = $8;
4217d7c76ba7SJoe Perches				my $cast;
4218d7c76ba7SJoe Perches
4219d1fe9c09SJoe Perches				if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
4220d7c76ba7SJoe Perches					$cast = "$cast1 or $cast2";
4221d7c76ba7SJoe Perches				} elsif ($cast1 ne "") {
4222d7c76ba7SJoe Perches					$cast = $cast1;
4223d7c76ba7SJoe Perches				} else {
4224d7c76ba7SJoe Perches					$cast = $cast2;
4225d7c76ba7SJoe Perches				}
4226d7c76ba7SJoe Perches				WARN("MINMAX",
4227d7c76ba7SJoe Perches				     "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
4228554e165cSAndy Whitcroft			}
4229554e165cSAndy Whitcroft		}
4230554e165cSAndy Whitcroft
42314a273195SJoe Perches# check usleep_range arguments
42324a273195SJoe Perches		if ($^V && $^V ge 5.10.0 &&
42334a273195SJoe Perches		    defined $stat &&
42344a273195SJoe Perches		    $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
42354a273195SJoe Perches			my $min = $1;
42364a273195SJoe Perches			my $max = $7;
42374a273195SJoe Perches			if ($min eq $max) {
42384a273195SJoe Perches				WARN("USLEEP_RANGE",
42394a273195SJoe Perches				     "usleep_range should not use min == max args; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
42404a273195SJoe Perches			} elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
42414a273195SJoe Perches				 $min > $max) {
42424a273195SJoe Perches				WARN("USLEEP_RANGE",
42434a273195SJoe Perches				     "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
42444a273195SJoe Perches			}
42454a273195SJoe Perches		}
42464a273195SJoe Perches
4247823b794cSJoe Perches# check for naked sscanf
4248823b794cSJoe Perches		if ($^V && $^V ge 5.10.0 &&
4249823b794cSJoe Perches		    defined $stat &&
42506c8bd707SJoe Perches		    $line =~ /\bsscanf\b/ &&
4251823b794cSJoe Perches		    ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
4252823b794cSJoe Perches		     $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ &&
4253823b794cSJoe Perches		     $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) {
4254823b794cSJoe Perches			my $lc = $stat =~ tr@\n@@;
4255823b794cSJoe Perches			$lc = $lc + $linenr;
4256823b794cSJoe Perches			my $stat_real = raw_line($linenr, 0);
4257823b794cSJoe Perches		        for (my $count = $linenr + 1; $count <= $lc; $count++) {
4258823b794cSJoe Perches				$stat_real = $stat_real . "\n" . raw_line($count, 0);
4259823b794cSJoe Perches			}
4260823b794cSJoe Perches			WARN("NAKED_SSCANF",
4261823b794cSJoe Perches			     "unchecked sscanf return value\n" . "$here\n$stat_real\n");
4262823b794cSJoe Perches		}
4263823b794cSJoe Perches
426470dc8a48SJoe Perches# check for new externs in .h files.
426570dc8a48SJoe Perches		if ($realfile =~ /\.h$/ &&
426670dc8a48SJoe Perches		    $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
4267d1d85780SJoe Perches			if (CHK("AVOID_EXTERNS",
426870dc8a48SJoe Perches				"extern prototypes should be avoided in .h files\n" . $herecurr) &&
426970dc8a48SJoe Perches			    $fix) {
427070dc8a48SJoe Perches				$fixed[$linenr - 1] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
427170dc8a48SJoe Perches			}
427270dc8a48SJoe Perches		}
427370dc8a48SJoe Perches
4274de7d4f0eSAndy Whitcroft# check for new externs in .c files.
4275171ae1a4SAndy Whitcroft		if ($realfile =~ /\.c$/ && defined $stat &&
4276c45dcabdSAndy Whitcroft		    $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
4277171ae1a4SAndy Whitcroft		{
4278c45dcabdSAndy Whitcroft			my $function_name = $1;
4279c45dcabdSAndy Whitcroft			my $paren_space = $2;
4280171ae1a4SAndy Whitcroft
4281171ae1a4SAndy Whitcroft			my $s = $stat;
4282171ae1a4SAndy Whitcroft			if (defined $cond) {
4283171ae1a4SAndy Whitcroft				substr($s, 0, length($cond), '');
4284171ae1a4SAndy Whitcroft			}
4285c45dcabdSAndy Whitcroft			if ($s =~ /^\s*;/ &&
4286c45dcabdSAndy Whitcroft			    $function_name ne 'uninitialized_var')
4287c45dcabdSAndy Whitcroft			{
4288000d1cc1SJoe Perches				WARN("AVOID_EXTERNS",
4289000d1cc1SJoe Perches				     "externs should be avoided in .c files\n" .  $herecurr);
4290de7d4f0eSAndy Whitcroft			}
4291de7d4f0eSAndy Whitcroft
4292171ae1a4SAndy Whitcroft			if ($paren_space =~ /\n/) {
4293000d1cc1SJoe Perches				WARN("FUNCTION_ARGUMENTS",
4294000d1cc1SJoe Perches				     "arguments for function declarations should follow identifier\n" . $herecurr);
4295171ae1a4SAndy Whitcroft			}
42969c9ba34eSAndy Whitcroft
42979c9ba34eSAndy Whitcroft		} elsif ($realfile =~ /\.c$/ && defined $stat &&
42989c9ba34eSAndy Whitcroft		    $stat =~ /^.\s*extern\s+/)
42999c9ba34eSAndy Whitcroft		{
4300000d1cc1SJoe Perches			WARN("AVOID_EXTERNS",
4301000d1cc1SJoe Perches			     "externs should be avoided in .c files\n" .  $herecurr);
4302171ae1a4SAndy Whitcroft		}
4303171ae1a4SAndy Whitcroft
4304de7d4f0eSAndy Whitcroft# checks for new __setup's
4305de7d4f0eSAndy Whitcroft		if ($rawline =~ /\b__setup\("([^"]*)"/) {
4306de7d4f0eSAndy Whitcroft			my $name = $1;
4307de7d4f0eSAndy Whitcroft
4308de7d4f0eSAndy Whitcroft			if (!grep(/$name/, @setup_docs)) {
4309000d1cc1SJoe Perches				CHK("UNDOCUMENTED_SETUP",
4310000d1cc1SJoe Perches				    "__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
4311de7d4f0eSAndy Whitcroft			}
4312653d4876SAndy Whitcroft		}
43139c0ca6f9SAndy Whitcroft
43149c0ca6f9SAndy Whitcroft# check for pointless casting of kmalloc return
4315caf2a54fSJoe Perches		if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) {
4316000d1cc1SJoe Perches			WARN("UNNECESSARY_CASTS",
4317000d1cc1SJoe Perches			     "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
43189c0ca6f9SAndy Whitcroft		}
431913214adfSAndy Whitcroft
4320a640d25cSJoe Perches# alloc style
4321a640d25cSJoe Perches# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
4322a640d25cSJoe Perches		if ($^V && $^V ge 5.10.0 &&
4323a640d25cSJoe Perches		    $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*([kv][mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
4324a640d25cSJoe Perches			CHK("ALLOC_SIZEOF_STRUCT",
4325a640d25cSJoe Perches			    "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
4326a640d25cSJoe Perches		}
4327a640d25cSJoe Perches
4328972fdea2SJoe Perches# check for krealloc arg reuse
4329972fdea2SJoe Perches		if ($^V && $^V ge 5.10.0 &&
4330972fdea2SJoe Perches		    $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*\1\s*,/) {
4331972fdea2SJoe Perches			WARN("KREALLOC_ARG_REUSE",
4332972fdea2SJoe Perches			     "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
4333972fdea2SJoe Perches		}
4334972fdea2SJoe Perches
43355ce59ae0SJoe Perches# check for alloc argument mismatch
43365ce59ae0SJoe Perches		if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
43375ce59ae0SJoe Perches			WARN("ALLOC_ARRAY_ARGS",
43385ce59ae0SJoe Perches			     "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
43395ce59ae0SJoe Perches		}
43405ce59ae0SJoe Perches
4341caf2a54fSJoe Perches# check for multiple semicolons
4342caf2a54fSJoe Perches		if ($line =~ /;\s*;\s*$/) {
4343d5e616fcSJoe Perches			if (WARN("ONE_SEMICOLON",
4344d5e616fcSJoe Perches				 "Statements terminations use 1 semicolon\n" . $herecurr) &&
4345d5e616fcSJoe Perches			    $fix) {
4346d5e616fcSJoe Perches				$fixed[$linenr - 1] =~ s/(\s*;\s*){2,}$/;/g;
4347d5e616fcSJoe Perches			}
4348d1e2ad07SJoe Perches		}
4349d1e2ad07SJoe Perches
4350c34c09a8SJoe Perches# check for case / default statements not preceeded by break/fallthrough/switch
4351c34c09a8SJoe Perches		if ($line =~ /^.\s*(?:case\s+(?:$Ident|$Constant)\s*|default):/) {
4352c34c09a8SJoe Perches			my $has_break = 0;
4353c34c09a8SJoe Perches			my $has_statement = 0;
4354c34c09a8SJoe Perches			my $count = 0;
4355c34c09a8SJoe Perches			my $prevline = $linenr;
4356c34c09a8SJoe Perches			while ($prevline > 1 && $count < 3 && !$has_break) {
4357c34c09a8SJoe Perches				$prevline--;
4358c34c09a8SJoe Perches				my $rline = $rawlines[$prevline - 1];
4359c34c09a8SJoe Perches				my $fline = $lines[$prevline - 1];
4360c34c09a8SJoe Perches				last if ($fline =~ /^\@\@/);
4361c34c09a8SJoe Perches				next if ($fline =~ /^\-/);
4362c34c09a8SJoe Perches				next if ($fline =~ /^.(?:\s*(?:case\s+(?:$Ident|$Constant)[\s$;]*|default):[\s$;]*)*$/);
4363c34c09a8SJoe Perches				$has_break = 1 if ($rline =~ /fall[\s_-]*(through|thru)/i);
4364c34c09a8SJoe Perches				next if ($fline =~ /^.[\s$;]*$/);
4365c34c09a8SJoe Perches				$has_statement = 1;
4366c34c09a8SJoe Perches				$count++;
4367c34c09a8SJoe Perches				$has_break = 1 if ($fline =~ /\bswitch\b|\b(?:break\s*;[\s$;]*$|return\b|goto\b|continue\b)/);
4368c34c09a8SJoe Perches			}
4369c34c09a8SJoe Perches			if (!$has_break && $has_statement) {
4370c34c09a8SJoe Perches				WARN("MISSING_BREAK",
4371c34c09a8SJoe Perches				     "Possible switch case/default not preceeded by break or fallthrough comment\n" . $herecurr);
4372c34c09a8SJoe Perches			}
4373c34c09a8SJoe Perches		}
4374c34c09a8SJoe Perches
4375d1e2ad07SJoe Perches# check for switch/default statements without a break;
4376d1e2ad07SJoe Perches		if ($^V && $^V ge 5.10.0 &&
4377d1e2ad07SJoe Perches		    defined $stat &&
4378d1e2ad07SJoe Perches		    $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
4379d1e2ad07SJoe Perches			my $ctx = '';
4380d1e2ad07SJoe Perches			my $herectx = $here . "\n";
4381d1e2ad07SJoe Perches			my $cnt = statement_rawlines($stat);
4382d1e2ad07SJoe Perches			for (my $n = 0; $n < $cnt; $n++) {
4383d1e2ad07SJoe Perches				$herectx .= raw_line($linenr, $n) . "\n";
4384d1e2ad07SJoe Perches			}
4385d1e2ad07SJoe Perches			WARN("DEFAULT_NO_BREAK",
4386d1e2ad07SJoe Perches			     "switch default: should use break\n" . $herectx);
4387caf2a54fSJoe Perches		}
4388caf2a54fSJoe Perches
438913214adfSAndy Whitcroft# check for gcc specific __FUNCTION__
4390d5e616fcSJoe Perches		if ($line =~ /\b__FUNCTION__\b/) {
4391d5e616fcSJoe Perches			if (WARN("USE_FUNC",
4392d5e616fcSJoe Perches				 "__func__ should be used instead of gcc specific __FUNCTION__\n"  . $herecurr) &&
4393d5e616fcSJoe Perches			    $fix) {
4394d5e616fcSJoe Perches				$fixed[$linenr - 1] =~ s/\b__FUNCTION__\b/__func__/g;
4395d5e616fcSJoe Perches			}
439613214adfSAndy Whitcroft		}
4397773647a0SAndy Whitcroft
43982c92488aSJoe Perches# check for use of yield()
43992c92488aSJoe Perches		if ($line =~ /\byield\s*\(\s*\)/) {
44002c92488aSJoe Perches			WARN("YIELD",
44012c92488aSJoe Perches			     "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n"  . $herecurr);
44022c92488aSJoe Perches		}
44032c92488aSJoe Perches
4404179f8f40SJoe Perches# check for comparisons against true and false
4405179f8f40SJoe Perches		if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
4406179f8f40SJoe Perches			my $lead = $1;
4407179f8f40SJoe Perches			my $arg = $2;
4408179f8f40SJoe Perches			my $test = $3;
4409179f8f40SJoe Perches			my $otype = $4;
4410179f8f40SJoe Perches			my $trail = $5;
4411179f8f40SJoe Perches			my $op = "!";
4412179f8f40SJoe Perches
4413179f8f40SJoe Perches			($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
4414179f8f40SJoe Perches
4415179f8f40SJoe Perches			my $type = lc($otype);
4416179f8f40SJoe Perches			if ($type =~ /^(?:true|false)$/) {
4417179f8f40SJoe Perches				if (("$test" eq "==" && "$type" eq "true") ||
4418179f8f40SJoe Perches				    ("$test" eq "!=" && "$type" eq "false")) {
4419179f8f40SJoe Perches					$op = "";
4420179f8f40SJoe Perches				}
4421179f8f40SJoe Perches
4422179f8f40SJoe Perches				CHK("BOOL_COMPARISON",
4423179f8f40SJoe Perches				    "Using comparison to $otype is error prone\n" . $herecurr);
4424179f8f40SJoe Perches
4425179f8f40SJoe Perches## maybe suggesting a correct construct would better
4426179f8f40SJoe Perches##				    "Using comparison to $otype is error prone.  Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
4427179f8f40SJoe Perches
4428179f8f40SJoe Perches			}
4429179f8f40SJoe Perches		}
4430179f8f40SJoe Perches
44314882720bSThomas Gleixner# check for semaphores initialized locked
44324882720bSThomas Gleixner		if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
4433000d1cc1SJoe Perches			WARN("CONSIDER_COMPLETION",
4434000d1cc1SJoe Perches			     "consider using a completion\n" . $herecurr);
4435773647a0SAndy Whitcroft		}
44366712d858SJoe Perches
443767d0a075SJoe Perches# recommend kstrto* over simple_strto* and strict_strto*
443867d0a075SJoe Perches		if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
4439000d1cc1SJoe Perches			WARN("CONSIDER_KSTRTO",
444067d0a075SJoe Perches			     "$1 is obsolete, use k$3 instead\n" . $herecurr);
4441773647a0SAndy Whitcroft		}
44426712d858SJoe Perches
4443f3db6639SMichael Ellerman# check for __initcall(), use device_initcall() explicitly please
4444f3db6639SMichael Ellerman		if ($line =~ /^.\s*__initcall\s*\(/) {
4445000d1cc1SJoe Perches			WARN("USE_DEVICE_INITCALL",
4446000d1cc1SJoe Perches			     "please use device_initcall() instead of __initcall()\n" . $herecurr);
4447f3db6639SMichael Ellerman		}
44486712d858SJoe Perches
444979404849SEmese Revfy# check for various ops structs, ensure they are const.
445079404849SEmese Revfy		my $struct_ops = qr{acpi_dock_ops|
445179404849SEmese Revfy				address_space_operations|
445279404849SEmese Revfy				backlight_ops|
445379404849SEmese Revfy				block_device_operations|
445479404849SEmese Revfy				dentry_operations|
445579404849SEmese Revfy				dev_pm_ops|
445679404849SEmese Revfy				dma_map_ops|
445779404849SEmese Revfy				extent_io_ops|
445879404849SEmese Revfy				file_lock_operations|
445979404849SEmese Revfy				file_operations|
446079404849SEmese Revfy				hv_ops|
446179404849SEmese Revfy				ide_dma_ops|
446279404849SEmese Revfy				intel_dvo_dev_ops|
446379404849SEmese Revfy				item_operations|
446479404849SEmese Revfy				iwl_ops|
446579404849SEmese Revfy				kgdb_arch|
446679404849SEmese Revfy				kgdb_io|
446779404849SEmese Revfy				kset_uevent_ops|
446879404849SEmese Revfy				lock_manager_operations|
446979404849SEmese Revfy				microcode_ops|
447079404849SEmese Revfy				mtrr_ops|
447179404849SEmese Revfy				neigh_ops|
447279404849SEmese Revfy				nlmsvc_binding|
447379404849SEmese Revfy				pci_raw_ops|
447479404849SEmese Revfy				pipe_buf_operations|
447579404849SEmese Revfy				platform_hibernation_ops|
447679404849SEmese Revfy				platform_suspend_ops|
447779404849SEmese Revfy				proto_ops|
447879404849SEmese Revfy				rpc_pipe_ops|
447979404849SEmese Revfy				seq_operations|
448079404849SEmese Revfy				snd_ac97_build_ops|
448179404849SEmese Revfy				soc_pcmcia_socket_ops|
448279404849SEmese Revfy				stacktrace_ops|
448379404849SEmese Revfy				sysfs_ops|
448479404849SEmese Revfy				tty_operations|
448579404849SEmese Revfy				usb_mon_operations|
448679404849SEmese Revfy				wd_ops}x;
44876903ffb2SAndy Whitcroft		if ($line !~ /\bconst\b/ &&
448879404849SEmese Revfy		    $line =~ /\bstruct\s+($struct_ops)\b/) {
4489000d1cc1SJoe Perches			WARN("CONST_STRUCT",
4490000d1cc1SJoe Perches			     "struct $1 should normally be const\n" .
44916903ffb2SAndy Whitcroft				$herecurr);
44922b6db5cbSAndy Whitcroft		}
4493773647a0SAndy Whitcroft
4494773647a0SAndy Whitcroft# use of NR_CPUS is usually wrong
4495773647a0SAndy Whitcroft# ignore definitions of NR_CPUS and usage to define arrays as likely right
4496773647a0SAndy Whitcroft		if ($line =~ /\bNR_CPUS\b/ &&
4497c45dcabdSAndy Whitcroft		    $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
4498c45dcabdSAndy Whitcroft		    $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
4499171ae1a4SAndy Whitcroft		    $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
4500171ae1a4SAndy Whitcroft		    $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
4501171ae1a4SAndy Whitcroft		    $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
4502773647a0SAndy Whitcroft		{
4503000d1cc1SJoe Perches			WARN("NR_CPUS",
4504000d1cc1SJoe Perches			     "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
4505773647a0SAndy Whitcroft		}
45069c9ba34eSAndy Whitcroft
450752ea8506SJoe Perches# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
450852ea8506SJoe Perches		if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
450952ea8506SJoe Perches			ERROR("DEFINE_ARCH_HAS",
451052ea8506SJoe Perches			      "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
451152ea8506SJoe Perches		}
451252ea8506SJoe Perches
45139c9ba34eSAndy Whitcroft# check for %L{u,d,i} in strings
45149c9ba34eSAndy Whitcroft		my $string;
45159c9ba34eSAndy Whitcroft		while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
45169c9ba34eSAndy Whitcroft			$string = substr($rawline, $-[1], $+[1] - $-[1]);
45172a1bc5d5SAndy Whitcroft			$string =~ s/%%/__/g;
45189c9ba34eSAndy Whitcroft			if ($string =~ /(?<!%)%L[udi]/) {
4519000d1cc1SJoe Perches				WARN("PRINTF_L",
4520000d1cc1SJoe Perches				     "\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
45219c9ba34eSAndy Whitcroft				last;
45229c9ba34eSAndy Whitcroft			}
45239c9ba34eSAndy Whitcroft		}
4524691d77b6SAndy Whitcroft
4525691d77b6SAndy Whitcroft# whine mightly about in_atomic
4526691d77b6SAndy Whitcroft		if ($line =~ /\bin_atomic\s*\(/) {
4527691d77b6SAndy Whitcroft			if ($realfile =~ m@^drivers/@) {
4528000d1cc1SJoe Perches				ERROR("IN_ATOMIC",
4529000d1cc1SJoe Perches				      "do not use in_atomic in drivers\n" . $herecurr);
4530f4a87736SAndy Whitcroft			} elsif ($realfile !~ m@^kernel/@) {
4531000d1cc1SJoe Perches				WARN("IN_ATOMIC",
4532000d1cc1SJoe Perches				     "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
4533691d77b6SAndy Whitcroft			}
4534691d77b6SAndy Whitcroft		}
45351704f47bSPeter Zijlstra
45361704f47bSPeter Zijlstra# check for lockdep_set_novalidate_class
45371704f47bSPeter Zijlstra		if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
45381704f47bSPeter Zijlstra		    $line =~ /__lockdep_no_validate__\s*\)/ ) {
45391704f47bSPeter Zijlstra			if ($realfile !~ m@^kernel/lockdep@ &&
45401704f47bSPeter Zijlstra			    $realfile !~ m@^include/linux/lockdep@ &&
45411704f47bSPeter Zijlstra			    $realfile !~ m@^drivers/base/core@) {
4542000d1cc1SJoe Perches				ERROR("LOCKDEP",
4543000d1cc1SJoe Perches				      "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
45441704f47bSPeter Zijlstra			}
45451704f47bSPeter Zijlstra		}
454688f8831cSDave Jones
454788f8831cSDave Jones		if ($line =~ /debugfs_create_file.*S_IWUGO/ ||
454888f8831cSDave Jones		    $line =~ /DEVICE_ATTR.*S_IWUGO/ ) {
4549000d1cc1SJoe Perches			WARN("EXPORTED_WORLD_WRITABLE",
4550000d1cc1SJoe Perches			     "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
455188f8831cSDave Jones		}
45522435880fSJoe Perches
4553515a235eSJoe Perches# Mode permission misuses where it seems decimal should be octal
4554515a235eSJoe Perches# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop
4555515a235eSJoe Perches		if ($^V && $^V ge 5.10.0 &&
4556515a235eSJoe Perches		    $line =~ /$mode_perms_search/) {
45572435880fSJoe Perches			foreach my $entry (@mode_permission_funcs) {
45582435880fSJoe Perches				my $func = $entry->[0];
45592435880fSJoe Perches				my $arg_pos = $entry->[1];
45602435880fSJoe Perches
45612435880fSJoe Perches				my $skip_args = "";
45622435880fSJoe Perches				if ($arg_pos > 1) {
45632435880fSJoe Perches					$arg_pos--;
45642435880fSJoe Perches					$skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}";
45652435880fSJoe Perches				}
45662435880fSJoe Perches				my $test = "\\b$func\\s*\\(${skip_args}([\\d]+)\\s*[,\\)]";
4567515a235eSJoe Perches				if ($line =~ /$test/) {
45682435880fSJoe Perches					my $val = $1;
45692435880fSJoe Perches					$val = $6 if ($skip_args ne "");
45702435880fSJoe Perches
45711727cc70SJoe Perches					if ($val !~ /^0$/ &&
45721727cc70SJoe Perches					    (($val =~ /^$Int$/ && $val !~ /^$Octal$/) ||
45731727cc70SJoe Perches					     length($val) ne 4)) {
45742435880fSJoe Perches						ERROR("NON_OCTAL_PERMISSIONS",
45751727cc70SJoe Perches						      "Use 4 digit octal (0777) not decimal permissions\n" . $herecurr);
45762435880fSJoe Perches					}
45772435880fSJoe Perches				}
45782435880fSJoe Perches			}
457913214adfSAndy Whitcroft		}
4580515a235eSJoe Perches	}
458113214adfSAndy Whitcroft
458213214adfSAndy Whitcroft	# If we have no input at all, then there is nothing to report on
458313214adfSAndy Whitcroft	# so just keep quiet.
458413214adfSAndy Whitcroft	if ($#rawlines == -1) {
458513214adfSAndy Whitcroft		exit(0);
45860a920b5bSAndy Whitcroft	}
45870a920b5bSAndy Whitcroft
45888905a67cSAndy Whitcroft	# In mailback mode only produce a report in the negative, for
45898905a67cSAndy Whitcroft	# things that appear to be patches.
45908905a67cSAndy Whitcroft	if ($mailback && ($clean == 1 || !$is_patch)) {
45918905a67cSAndy Whitcroft		exit(0);
45928905a67cSAndy Whitcroft	}
45938905a67cSAndy Whitcroft
45948905a67cSAndy Whitcroft	# This is not a patch, and we are are in 'no-patch' mode so
45958905a67cSAndy Whitcroft	# just keep quiet.
45968905a67cSAndy Whitcroft	if (!$chk_patch && !$is_patch) {
45978905a67cSAndy Whitcroft		exit(0);
45988905a67cSAndy Whitcroft	}
45998905a67cSAndy Whitcroft
46008905a67cSAndy Whitcroft	if (!$is_patch) {
4601000d1cc1SJoe Perches		ERROR("NOT_UNIFIED_DIFF",
4602000d1cc1SJoe Perches		      "Does not appear to be a unified-diff format patch\n");
46030a920b5bSAndy Whitcroft	}
46040a920b5bSAndy Whitcroft	if ($is_patch && $chk_signoff && $signoff == 0) {
4605000d1cc1SJoe Perches		ERROR("MISSING_SIGN_OFF",
4606000d1cc1SJoe Perches		      "Missing Signed-off-by: line(s)\n");
46070a920b5bSAndy Whitcroft	}
46080a920b5bSAndy Whitcroft
4609f0a594c1SAndy Whitcroft	print report_dump();
461013214adfSAndy Whitcroft	if ($summary && !($clean == 1 && $quiet == 1)) {
461113214adfSAndy Whitcroft		print "$filename " if ($summary_file);
46126c72ffaaSAndy Whitcroft		print "total: $cnt_error errors, $cnt_warn warnings, " .
46136c72ffaaSAndy Whitcroft			(($check)? "$cnt_chk checks, " : "") .
46146c72ffaaSAndy Whitcroft			"$cnt_lines lines checked\n";
46158905a67cSAndy Whitcroft		print "\n" if ($quiet == 0);
46166c72ffaaSAndy Whitcroft	}
46178905a67cSAndy Whitcroft
4618d2c0a235SAndy Whitcroft	if ($quiet == 0) {
4619d1fe9c09SJoe Perches
4620d1fe9c09SJoe Perches		if ($^V lt 5.10.0) {
4621d1fe9c09SJoe Perches			print("NOTE: perl $^V is not modern enough to detect all possible issues.\n");
4622d1fe9c09SJoe Perches			print("An upgrade to at least perl v5.10.0 is suggested.\n\n");
4623d1fe9c09SJoe Perches		}
4624d1fe9c09SJoe Perches
4625d2c0a235SAndy Whitcroft		# If there were whitespace errors which cleanpatch can fix
4626d2c0a235SAndy Whitcroft		# then suggest that.
4627d2c0a235SAndy Whitcroft		if ($rpt_cleaners) {
4628d2c0a235SAndy Whitcroft			print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n";
4629d2c0a235SAndy Whitcroft			print "      scripts/cleanfile\n\n";
4630b0781216SMike Frysinger			$rpt_cleaners = 0;
4631d2c0a235SAndy Whitcroft		}
4632d2c0a235SAndy Whitcroft	}
4633d2c0a235SAndy Whitcroft
463491bfe484SJoe Perches	hash_show_words(\%use_type, "Used");
463591bfe484SJoe Perches	hash_show_words(\%ignore_type, "Ignored");
4636000d1cc1SJoe Perches
46373705ce5bSJoe Perches	if ($clean == 0 && $fix && "@rawlines" ne "@fixed") {
46389624b8d6SJoe Perches		my $newfile = $filename;
46399624b8d6SJoe Perches		$newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace);
46403705ce5bSJoe Perches		my $linecount = 0;
46413705ce5bSJoe Perches		my $f;
46423705ce5bSJoe Perches
46433705ce5bSJoe Perches		open($f, '>', $newfile)
46443705ce5bSJoe Perches		    or die "$P: Can't open $newfile for write\n";
46453705ce5bSJoe Perches		foreach my $fixed_line (@fixed) {
46463705ce5bSJoe Perches			$linecount++;
46473705ce5bSJoe Perches			if ($file) {
46483705ce5bSJoe Perches				if ($linecount > 3) {
46493705ce5bSJoe Perches					$fixed_line =~ s/^\+//;
46503705ce5bSJoe Perches					print $f $fixed_line. "\n";
46513705ce5bSJoe Perches				}
46523705ce5bSJoe Perches			} else {
46533705ce5bSJoe Perches				print $f $fixed_line . "\n";
46543705ce5bSJoe Perches			}
46553705ce5bSJoe Perches		}
46563705ce5bSJoe Perches		close($f);
46573705ce5bSJoe Perches
46583705ce5bSJoe Perches		if (!$quiet) {
46593705ce5bSJoe Perches			print << "EOM";
46603705ce5bSJoe PerchesWrote EXPERIMENTAL --fix correction(s) to '$newfile'
46613705ce5bSJoe Perches
46623705ce5bSJoe PerchesDo _NOT_ trust the results written to this file.
46633705ce5bSJoe PerchesDo _NOT_ submit these changes without inspecting them for correctness.
46643705ce5bSJoe Perches
46653705ce5bSJoe PerchesThis EXPERIMENTAL file is simply a convenience to help rewrite patches.
46663705ce5bSJoe PerchesNo warranties, expressed or implied...
46673705ce5bSJoe Perches
46683705ce5bSJoe PerchesEOM
46693705ce5bSJoe Perches		}
46703705ce5bSJoe Perches	}
46713705ce5bSJoe Perches
46720a920b5bSAndy Whitcroft	if ($clean == 1 && $quiet == 0) {
4673c2fdda0dSAndy Whitcroft		print "$vname has no obvious style problems and is ready for submission.\n"
46740a920b5bSAndy Whitcroft	}
46750a920b5bSAndy Whitcroft	if ($clean == 0 && $quiet == 0) {
4676000d1cc1SJoe Perches		print << "EOM";
4677000d1cc1SJoe Perches$vname has style problems, please review.
4678000d1cc1SJoe Perches
4679000d1cc1SJoe PerchesIf any of these errors are false positives, please report
4680000d1cc1SJoe Perchesthem to the maintainer, see CHECKPATCH in MAINTAINERS.
4681000d1cc1SJoe PerchesEOM
46820a920b5bSAndy Whitcroft	}
468313214adfSAndy Whitcroft
46840a920b5bSAndy Whitcroft	return $clean;
46850a920b5bSAndy Whitcroft}
4686