xref: /linux-6.15/scripts/checkpatch.pl (revision 8d73e0e7)
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;
272ac73b4fSJoe Perchesmy $check_orig = 0;
288905a67cSAndy Whitcroftmy $summary = 1;
298905a67cSAndy Whitcroftmy $mailback = 0;
3013214adfSAndy Whitcroftmy $summary_file = 0;
31000d1cc1SJoe Perchesmy $show_types = 0;
323705ce5bSJoe Perchesmy $fix = 0;
339624b8d6SJoe Perchesmy $fix_inplace = 0;
346c72ffaaSAndy Whitcroftmy $root;
35c2fdda0dSAndy Whitcroftmy %debug;
363445686aSJoe Perchesmy %camelcase = ();
3791bfe484SJoe Perchesmy %use_type = ();
3891bfe484SJoe Perchesmy @use = ();
3991bfe484SJoe Perchesmy %ignore_type = ();
40000d1cc1SJoe Perchesmy @ignore = ();
4177f5b10aSHannes Edermy $help = 0;
42000d1cc1SJoe Perchesmy $configuration_file = ".checkpatch.conf";
436cd7f386SJoe Perchesmy $max_line_length = 80;
44d62a201fSDave Hansenmy $ignore_perl_version = 0;
45d62a201fSDave Hansenmy $minimum_perl_version = 5.10.0;
4677f5b10aSHannes Eder
4777f5b10aSHannes Edersub help {
4877f5b10aSHannes Eder	my ($exitcode) = @_;
4977f5b10aSHannes Eder
5077f5b10aSHannes Eder	print << "EOM";
5177f5b10aSHannes EderUsage: $P [OPTION]... [FILE]...
5277f5b10aSHannes EderVersion: $V
5377f5b10aSHannes Eder
5477f5b10aSHannes EderOptions:
5577f5b10aSHannes Eder  -q, --quiet                quiet
5677f5b10aSHannes Eder  --no-tree                  run without a kernel tree
5777f5b10aSHannes Eder  --no-signoff               do not check for 'Signed-off-by' line
5877f5b10aSHannes Eder  --patch                    treat FILE as patchfile (default)
5977f5b10aSHannes Eder  --emacs                    emacs compile window format
6077f5b10aSHannes Eder  --terse                    one line per report
6177f5b10aSHannes Eder  -f, --file                 treat FILE as regular source file
6277f5b10aSHannes Eder  --subjective, --strict     enable more subjective tests
6391bfe484SJoe Perches  --types TYPE(,TYPE2...)    show only these comma separated message types
64000d1cc1SJoe Perches  --ignore TYPE(,TYPE2...)   ignore various comma separated message types
656cd7f386SJoe Perches  --max-line-length=n        set the maximum line length, if exceeded, warn
66000d1cc1SJoe Perches  --show-types               show the message "types" in the output
6777f5b10aSHannes Eder  --root=PATH                PATH to the kernel tree root
6877f5b10aSHannes Eder  --no-summary               suppress the per-file summary
6977f5b10aSHannes Eder  --mailback                 only produce a report in case of warnings/errors
7077f5b10aSHannes Eder  --summary-file             include the filename in summary
7177f5b10aSHannes Eder  --debug KEY=[0|1]          turn on/off debugging of KEY, where KEY is one of
7277f5b10aSHannes Eder                             'values', 'possible', 'type', and 'attr' (default
7377f5b10aSHannes Eder                             is all off)
7477f5b10aSHannes Eder  --test-only=WORD           report only warnings/errors containing WORD
7577f5b10aSHannes Eder                             literally
763705ce5bSJoe Perches  --fix                      EXPERIMENTAL - may create horrible results
773705ce5bSJoe Perches                             If correctable single-line errors exist, create
783705ce5bSJoe Perches                             "<inputfile>.EXPERIMENTAL-checkpatch-fixes"
793705ce5bSJoe Perches                             with potential errors corrected to the preferred
803705ce5bSJoe Perches                             checkpatch style
819624b8d6SJoe Perches  --fix-inplace              EXPERIMENTAL - may create horrible results
829624b8d6SJoe Perches                             Is the same as --fix, but overwrites the input
839624b8d6SJoe Perches                             file.  It's your fault if there's no backup or git
84d62a201fSDave Hansen  --ignore-perl-version      override checking of perl version.  expect
85d62a201fSDave Hansen                             runtime errors.
8677f5b10aSHannes Eder  -h, --help, --version      display this help and exit
8777f5b10aSHannes Eder
8877f5b10aSHannes EderWhen FILE is - read standard input.
8977f5b10aSHannes EderEOM
9077f5b10aSHannes Eder
9177f5b10aSHannes Eder	exit($exitcode);
9277f5b10aSHannes Eder}
9377f5b10aSHannes Eder
94000d1cc1SJoe Perchesmy $conf = which_conf($configuration_file);
95000d1cc1SJoe Perchesif (-f $conf) {
96000d1cc1SJoe Perches	my @conf_args;
97000d1cc1SJoe Perches	open(my $conffile, '<', "$conf")
98000d1cc1SJoe Perches	    or warn "$P: Can't find a readable $configuration_file file $!\n";
99000d1cc1SJoe Perches
100000d1cc1SJoe Perches	while (<$conffile>) {
101000d1cc1SJoe Perches		my $line = $_;
102000d1cc1SJoe Perches
103000d1cc1SJoe Perches		$line =~ s/\s*\n?$//g;
104000d1cc1SJoe Perches		$line =~ s/^\s*//g;
105000d1cc1SJoe Perches		$line =~ s/\s+/ /g;
106000d1cc1SJoe Perches
107000d1cc1SJoe Perches		next if ($line =~ m/^\s*#/);
108000d1cc1SJoe Perches		next if ($line =~ m/^\s*$/);
109000d1cc1SJoe Perches
110000d1cc1SJoe Perches		my @words = split(" ", $line);
111000d1cc1SJoe Perches		foreach my $word (@words) {
112000d1cc1SJoe Perches			last if ($word =~ m/^#/);
113000d1cc1SJoe Perches			push (@conf_args, $word);
114000d1cc1SJoe Perches		}
115000d1cc1SJoe Perches	}
116000d1cc1SJoe Perches	close($conffile);
117000d1cc1SJoe Perches	unshift(@ARGV, @conf_args) if @conf_args;
118000d1cc1SJoe Perches}
119000d1cc1SJoe Perches
1200a920b5bSAndy WhitcroftGetOptions(
1216c72ffaaSAndy Whitcroft	'q|quiet+'	=> \$quiet,
1220a920b5bSAndy Whitcroft	'tree!'		=> \$tree,
1230a920b5bSAndy Whitcroft	'signoff!'	=> \$chk_signoff,
1240a920b5bSAndy Whitcroft	'patch!'	=> \$chk_patch,
1256c72ffaaSAndy Whitcroft	'emacs!'	=> \$emacs,
1268905a67cSAndy Whitcroft	'terse!'	=> \$terse,
12777f5b10aSHannes Eder	'f|file!'	=> \$file,
1286c72ffaaSAndy Whitcroft	'subjective!'	=> \$check,
1296c72ffaaSAndy Whitcroft	'strict!'	=> \$check,
130000d1cc1SJoe Perches	'ignore=s'	=> \@ignore,
13191bfe484SJoe Perches	'types=s'	=> \@use,
132000d1cc1SJoe Perches	'show-types!'	=> \$show_types,
1336cd7f386SJoe Perches	'max-line-length=i' => \$max_line_length,
1346c72ffaaSAndy Whitcroft	'root=s'	=> \$root,
1358905a67cSAndy Whitcroft	'summary!'	=> \$summary,
1368905a67cSAndy Whitcroft	'mailback!'	=> \$mailback,
13713214adfSAndy Whitcroft	'summary-file!'	=> \$summary_file,
1383705ce5bSJoe Perches	'fix!'		=> \$fix,
1399624b8d6SJoe Perches	'fix-inplace!'	=> \$fix_inplace,
140d62a201fSDave Hansen	'ignore-perl-version!' => \$ignore_perl_version,
141c2fdda0dSAndy Whitcroft	'debug=s'	=> \%debug,
142773647a0SAndy Whitcroft	'test-only=s'	=> \$tst_only,
14377f5b10aSHannes Eder	'h|help'	=> \$help,
14477f5b10aSHannes Eder	'version'	=> \$help
14577f5b10aSHannes Eder) or help(1);
14677f5b10aSHannes Eder
14777f5b10aSHannes Ederhelp(0) if ($help);
1480a920b5bSAndy Whitcroft
1499624b8d6SJoe Perches$fix = 1 if ($fix_inplace);
1502ac73b4fSJoe Perches$check_orig = $check;
1519624b8d6SJoe Perches
1520a920b5bSAndy Whitcroftmy $exit = 0;
1530a920b5bSAndy Whitcroft
154d62a201fSDave Hansenif ($^V && $^V lt $minimum_perl_version) {
155d62a201fSDave Hansen	printf "$P: requires at least perl version %vd\n", $minimum_perl_version;
156d62a201fSDave Hansen	if (!$ignore_perl_version) {
157d62a201fSDave Hansen		exit(1);
158d62a201fSDave Hansen	}
159d62a201fSDave Hansen}
160d62a201fSDave Hansen
1610a920b5bSAndy Whitcroftif ($#ARGV < 0) {
16277f5b10aSHannes Eder	print "$P: no input files\n";
1630a920b5bSAndy Whitcroft	exit(1);
1640a920b5bSAndy Whitcroft}
1650a920b5bSAndy Whitcroft
16691bfe484SJoe Perchessub hash_save_array_words {
16791bfe484SJoe Perches	my ($hashRef, $arrayRef) = @_;
16891bfe484SJoe Perches
16991bfe484SJoe Perches	my @array = split(/,/, join(',', @$arrayRef));
17091bfe484SJoe Perches	foreach my $word (@array) {
171000d1cc1SJoe Perches		$word =~ s/\s*\n?$//g;
172000d1cc1SJoe Perches		$word =~ s/^\s*//g;
173000d1cc1SJoe Perches		$word =~ s/\s+/ /g;
174000d1cc1SJoe Perches		$word =~ tr/[a-z]/[A-Z]/;
175000d1cc1SJoe Perches
176000d1cc1SJoe Perches		next if ($word =~ m/^\s*#/);
177000d1cc1SJoe Perches		next if ($word =~ m/^\s*$/);
178000d1cc1SJoe Perches
17991bfe484SJoe Perches		$hashRef->{$word}++;
180000d1cc1SJoe Perches	}
18191bfe484SJoe Perches}
18291bfe484SJoe Perches
18391bfe484SJoe Perchessub hash_show_words {
18491bfe484SJoe Perches	my ($hashRef, $prefix) = @_;
18591bfe484SJoe Perches
18658cb3cf6SJoe Perches	if ($quiet == 0 && keys %$hashRef) {
18791bfe484SJoe Perches		print "NOTE: $prefix message types:";
18858cb3cf6SJoe Perches		foreach my $word (sort keys %$hashRef) {
18991bfe484SJoe Perches			print " $word";
19091bfe484SJoe Perches		}
19191bfe484SJoe Perches		print "\n\n";
19291bfe484SJoe Perches	}
19391bfe484SJoe Perches}
19491bfe484SJoe Perches
19591bfe484SJoe Percheshash_save_array_words(\%ignore_type, \@ignore);
19691bfe484SJoe Percheshash_save_array_words(\%use_type, \@use);
197000d1cc1SJoe Perches
198c2fdda0dSAndy Whitcroftmy $dbg_values = 0;
199c2fdda0dSAndy Whitcroftmy $dbg_possible = 0;
2007429c690SAndy Whitcroftmy $dbg_type = 0;
201a1ef277eSAndy Whitcroftmy $dbg_attr = 0;
202c2fdda0dSAndy Whitcroftfor my $key (keys %debug) {
20321caa13cSAndy Whitcroft	## no critic
20421caa13cSAndy Whitcroft	eval "\${dbg_$key} = '$debug{$key}';";
20521caa13cSAndy Whitcroft	die "$@" if ($@);
206c2fdda0dSAndy Whitcroft}
207c2fdda0dSAndy Whitcroft
208d2c0a235SAndy Whitcroftmy $rpt_cleaners = 0;
209d2c0a235SAndy Whitcroft
2108905a67cSAndy Whitcroftif ($terse) {
2118905a67cSAndy Whitcroft	$emacs = 1;
2128905a67cSAndy Whitcroft	$quiet++;
2138905a67cSAndy Whitcroft}
2148905a67cSAndy Whitcroft
2156c72ffaaSAndy Whitcroftif ($tree) {
2166c72ffaaSAndy Whitcroft	if (defined $root) {
2176c72ffaaSAndy Whitcroft		if (!top_of_kernel_tree($root)) {
2186c72ffaaSAndy Whitcroft			die "$P: $root: --root does not point at a valid tree\n";
2196c72ffaaSAndy Whitcroft		}
2206c72ffaaSAndy Whitcroft	} else {
2216c72ffaaSAndy Whitcroft		if (top_of_kernel_tree('.')) {
2226c72ffaaSAndy Whitcroft			$root = '.';
2236c72ffaaSAndy Whitcroft		} elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
2246c72ffaaSAndy Whitcroft						top_of_kernel_tree($1)) {
2256c72ffaaSAndy Whitcroft			$root = $1;
2266c72ffaaSAndy Whitcroft		}
2276c72ffaaSAndy Whitcroft	}
2286c72ffaaSAndy Whitcroft
2296c72ffaaSAndy Whitcroft	if (!defined $root) {
2300a920b5bSAndy Whitcroft		print "Must be run from the top-level dir. of a kernel tree\n";
2310a920b5bSAndy Whitcroft		exit(2);
2320a920b5bSAndy Whitcroft	}
2336c72ffaaSAndy Whitcroft}
2346c72ffaaSAndy Whitcroft
2356c72ffaaSAndy Whitcroftmy $emitted_corrupt = 0;
2366c72ffaaSAndy Whitcroft
2372ceb532bSAndy Whitcroftour $Ident	= qr{
2382ceb532bSAndy Whitcroft			[A-Za-z_][A-Za-z\d_]*
2392ceb532bSAndy Whitcroft			(?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
2402ceb532bSAndy Whitcroft		}x;
2416c72ffaaSAndy Whitcroftour $Storage	= qr{extern|static|asmlinkage};
2426c72ffaaSAndy Whitcroftour $Sparse	= qr{
2436c72ffaaSAndy Whitcroft			__user|
2446c72ffaaSAndy Whitcroft			__kernel|
2456c72ffaaSAndy Whitcroft			__force|
2466c72ffaaSAndy Whitcroft			__iomem|
2476c72ffaaSAndy Whitcroft			__must_check|
2486c72ffaaSAndy Whitcroft			__init_refok|
249417495edSAndy Whitcroft			__kprobes|
250165e72a6SSven Eckelmann			__ref|
251165e72a6SSven Eckelmann			__rcu
2526c72ffaaSAndy Whitcroft		}x;
253e970b884SJoe Perchesour $InitAttributePrefix = qr{__(?:mem|cpu|dev|net_|)};
254e970b884SJoe Perchesour $InitAttributeData = qr{$InitAttributePrefix(?:initdata\b)};
255e970b884SJoe Perchesour $InitAttributeConst = qr{$InitAttributePrefix(?:initconst\b)};
256e970b884SJoe Perchesour $InitAttributeInit = qr{$InitAttributePrefix(?:init\b)};
257e970b884SJoe Perchesour $InitAttribute = qr{$InitAttributeData|$InitAttributeConst|$InitAttributeInit};
2588716de38SJoe Perches
25952131292SWolfram Sang# Notes to $Attribute:
26052131292SWolfram Sang# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
2616c72ffaaSAndy Whitcroftour $Attribute	= qr{
2626c72ffaaSAndy Whitcroft			const|
26303f1df7dSJoe Perches			__percpu|
26403f1df7dSJoe Perches			__nocast|
26503f1df7dSJoe Perches			__safe|
26603f1df7dSJoe Perches			__bitwise__|
26703f1df7dSJoe Perches			__packed__|
26803f1df7dSJoe Perches			__packed2__|
26903f1df7dSJoe Perches			__naked|
27003f1df7dSJoe Perches			__maybe_unused|
27103f1df7dSJoe Perches			__always_unused|
27203f1df7dSJoe Perches			__noreturn|
27303f1df7dSJoe Perches			__used|
27403f1df7dSJoe Perches			__cold|
27503f1df7dSJoe Perches			__noclone|
27603f1df7dSJoe Perches			__deprecated|
2776c72ffaaSAndy Whitcroft			__read_mostly|
2786c72ffaaSAndy Whitcroft			__kprobes|
2798716de38SJoe Perches			$InitAttribute|
28024e1d81aSAndy Whitcroft			____cacheline_aligned|
28124e1d81aSAndy Whitcroft			____cacheline_aligned_in_smp|
2825fe3af11SAndy Whitcroft			____cacheline_internodealigned_in_smp|
2835fe3af11SAndy Whitcroft			__weak
2846c72ffaaSAndy Whitcroft		  }x;
285c45dcabdSAndy Whitcroftour $Modifier;
28691cb5195SJoe Perchesour $Inline	= qr{inline|__always_inline|noinline|__inline|__inline__};
2876c72ffaaSAndy Whitcroftour $Member	= qr{->$Ident|\.$Ident|\[[^]]*\]};
2886c72ffaaSAndy Whitcroftour $Lval	= qr{$Ident(?:$Member)*};
2896c72ffaaSAndy Whitcroft
29095e2c602SJoe Perchesour $Int_type	= qr{(?i)llu|ull|ll|lu|ul|l|u};
29195e2c602SJoe Perchesour $Binary	= qr{(?i)0b[01]+$Int_type?};
29295e2c602SJoe Perchesour $Hex	= qr{(?i)0x[0-9a-f]+$Int_type?};
29395e2c602SJoe Perchesour $Int	= qr{[0-9]+$Int_type?};
2942435880fSJoe Perchesour $Octal	= qr{0[0-7]+$Int_type?};
295326b1ffcSJoe Perchesour $Float_hex	= qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?};
296326b1ffcSJoe Perchesour $Float_dec	= qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?};
297326b1ffcSJoe Perchesour $Float_int	= qr{(?i)[0-9]+e-?[0-9]+[fl]?};
29874349bccSJoe Perchesour $Float	= qr{$Float_hex|$Float_dec|$Float_int};
2992435880fSJoe Perchesour $Constant	= qr{$Float|$Binary|$Octal|$Hex|$Int};
300326b1ffcSJoe Perchesour $Assignment	= qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=};
301447432f3SJoe Perchesour $Compare    = qr{<=|>=|==|!=|<|(?<!-)>};
30223f780c9SJoe Perchesour $Arithmetic = qr{\+|-|\*|\/|%};
3036c72ffaaSAndy Whitcroftour $Operators	= qr{
3046c72ffaaSAndy Whitcroft			<=|>=|==|!=|
3056c72ffaaSAndy Whitcroft			=>|->|<<|>>|<|>|!|~|
30623f780c9SJoe Perches			&&|\|\||,|\^|\+\+|--|&|\||$Arithmetic
3076c72ffaaSAndy Whitcroft		  }x;
3086c72ffaaSAndy Whitcroft
30991cb5195SJoe Perchesour $c90_Keywords = qr{do|for|while|if|else|return|goto|continue|switch|default|case|break}x;
31091cb5195SJoe Perches
3118905a67cSAndy Whitcroftour $NonptrType;
3128716de38SJoe Perchesour $NonptrTypeWithAttr;
3138905a67cSAndy Whitcroftour $Type;
3148905a67cSAndy Whitcroftour $Declare;
3158905a67cSAndy Whitcroft
31615662b3eSJoe Perchesour $NON_ASCII_UTF8	= qr{
31715662b3eSJoe Perches	[\xC2-\xDF][\x80-\xBF]               # non-overlong 2-byte
318171ae1a4SAndy Whitcroft	|  \xE0[\xA0-\xBF][\x80-\xBF]        # excluding overlongs
319171ae1a4SAndy Whitcroft	| [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}  # straight 3-byte
320171ae1a4SAndy Whitcroft	|  \xED[\x80-\x9F][\x80-\xBF]        # excluding surrogates
321171ae1a4SAndy Whitcroft	|  \xF0[\x90-\xBF][\x80-\xBF]{2}     # planes 1-3
322171ae1a4SAndy Whitcroft	| [\xF1-\xF3][\x80-\xBF]{3}          # planes 4-15
323171ae1a4SAndy Whitcroft	|  \xF4[\x80-\x8F][\x80-\xBF]{2}     # plane 16
324171ae1a4SAndy Whitcroft}x;
325171ae1a4SAndy Whitcroft
32615662b3eSJoe Perchesour $UTF8	= qr{
32715662b3eSJoe Perches	[\x09\x0A\x0D\x20-\x7E]              # ASCII
32815662b3eSJoe Perches	| $NON_ASCII_UTF8
32915662b3eSJoe Perches}x;
33015662b3eSJoe Perches
3318ed22cadSAndy Whitcroftour $typeTypedefs = qr{(?x:
332fb9e9096SAndy Whitcroft	(?:__)?(?:u|s|be|le)(?:8|16|32|64)|
3338ed22cadSAndy Whitcroft	atomic_t
3348ed22cadSAndy Whitcroft)};
3358ed22cadSAndy Whitcroft
336691e669bSJoe Perchesour $logFunctions = qr{(?x:
3376e60c02eSJoe Perches	printk(?:_ratelimited|_once|)|
3387d0b6594SJacob Keller	(?:[a-z0-9]+_){1,2}(?:printk|emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)(?:_ratelimited|_once|)|
3396e60c02eSJoe Perches	WARN(?:_RATELIMIT|_ONCE|)|
340b0531722SJoe Perches	panic|
34106668727SJoe Perches	MODULE_[A-Z_]+|
34206668727SJoe Perches	seq_vprintf|seq_printf|seq_puts
343691e669bSJoe Perches)};
344691e669bSJoe Perches
34520112475SJoe Perchesour $signature_tags = qr{(?xi:
34620112475SJoe Perches	Signed-off-by:|
34720112475SJoe Perches	Acked-by:|
34820112475SJoe Perches	Tested-by:|
34920112475SJoe Perches	Reviewed-by:|
35020112475SJoe Perches	Reported-by:|
3518543ae12SMugunthan V N	Suggested-by:|
35220112475SJoe Perches	To:|
35320112475SJoe Perches	Cc:
35420112475SJoe Perches)};
35520112475SJoe Perches
3568905a67cSAndy Whitcroftour @typeList = (
3578905a67cSAndy Whitcroft	qr{void},
358c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?char},
359c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?short},
360c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?int},
361c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?long},
362c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?long\s+int},
363c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?long\s+long},
364c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?long\s+long\s+int},
3658905a67cSAndy Whitcroft	qr{unsigned},
3668905a67cSAndy Whitcroft	qr{float},
3678905a67cSAndy Whitcroft	qr{double},
3688905a67cSAndy Whitcroft	qr{bool},
3698905a67cSAndy Whitcroft	qr{struct\s+$Ident},
3708905a67cSAndy Whitcroft	qr{union\s+$Ident},
3718905a67cSAndy Whitcroft	qr{enum\s+$Ident},
3728905a67cSAndy Whitcroft	qr{${Ident}_t},
3738905a67cSAndy Whitcroft	qr{${Ident}_handler},
3748905a67cSAndy Whitcroft	qr{${Ident}_handler_fn},
3758905a67cSAndy Whitcroft);
3768716de38SJoe Perchesour @typeListWithAttr = (
3778716de38SJoe Perches	@typeList,
3788716de38SJoe Perches	qr{struct\s+$InitAttribute\s+$Ident},
3798716de38SJoe Perches	qr{union\s+$InitAttribute\s+$Ident},
3808716de38SJoe Perches);
3818716de38SJoe Perches
382c45dcabdSAndy Whitcroftour @modifierList = (
383c45dcabdSAndy Whitcroft	qr{fastcall},
384c45dcabdSAndy Whitcroft);
3858905a67cSAndy Whitcroft
3862435880fSJoe Perchesour @mode_permission_funcs = (
3872435880fSJoe Perches	["module_param", 3],
3882435880fSJoe Perches	["module_param_(?:array|named|string)", 4],
3892435880fSJoe Perches	["module_param_array_named", 5],
3902435880fSJoe Perches	["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2],
3912435880fSJoe Perches	["proc_create(?:_data|)", 2],
3922435880fSJoe Perches	["(?:CLASS|DEVICE|SENSOR)_ATTR", 2],
3932435880fSJoe Perches);
3942435880fSJoe Perches
395515a235eSJoe Perches#Create a search pattern for all these functions to speed up a loop below
396515a235eSJoe Perchesour $mode_perms_search = "";
397515a235eSJoe Perchesforeach my $entry (@mode_permission_funcs) {
398515a235eSJoe Perches	$mode_perms_search .= '|' if ($mode_perms_search ne "");
399515a235eSJoe Perches	$mode_perms_search .= $entry->[0];
400515a235eSJoe Perches}
401515a235eSJoe Perches
4023f7bac03SJoe Perchesour $declaration_macros = qr{(?x:
4033f7bac03SJoe Perches	(?:$Storage\s+)?(?:DECLARE|DEFINE)_[A-Z]+\s*\(|
4043f7bac03SJoe Perches	(?:$Storage\s+)?LIST_HEAD\s*\(
4053f7bac03SJoe Perches)};
4063f7bac03SJoe Perches
4077840a94cSWolfram Sangour $allowed_asm_includes = qr{(?x:
4087840a94cSWolfram Sang	irq|
4097840a94cSWolfram Sang	memory
4107840a94cSWolfram Sang)};
4117840a94cSWolfram Sang# memory.h: ARM has a custom one
4127840a94cSWolfram Sang
4138905a67cSAndy Whitcroftsub build_types {
414d2172eb5SAndy Whitcroft	my $mods = "(?x:  \n" . join("|\n  ", @modifierList) . "\n)";
415d2172eb5SAndy Whitcroft	my $all = "(?x:  \n" . join("|\n  ", @typeList) . "\n)";
4168716de38SJoe Perches	my $allWithAttr = "(?x:  \n" . join("|\n  ", @typeListWithAttr) . "\n)";
417c8cb2ca3SAndy Whitcroft	$Modifier	= qr{(?:$Attribute|$Sparse|$mods)};
4188905a67cSAndy Whitcroft	$NonptrType	= qr{
419d2172eb5SAndy Whitcroft			(?:$Modifier\s+|const\s+)*
420cf655043SAndy Whitcroft			(?:
4216b48db24SAndy Whitcroft				(?:typeof|__typeof__)\s*\([^\)]*\)|
4228ed22cadSAndy Whitcroft				(?:$typeTypedefs\b)|
423c45dcabdSAndy Whitcroft				(?:${all}\b)
424cf655043SAndy Whitcroft			)
425c8cb2ca3SAndy Whitcroft			(?:\s+$Modifier|\s+const)*
4268905a67cSAndy Whitcroft		  }x;
4278716de38SJoe Perches	$NonptrTypeWithAttr	= qr{
4288716de38SJoe Perches			(?:$Modifier\s+|const\s+)*
4298716de38SJoe Perches			(?:
4308716de38SJoe Perches				(?:typeof|__typeof__)\s*\([^\)]*\)|
4318716de38SJoe Perches				(?:$typeTypedefs\b)|
4328716de38SJoe Perches				(?:${allWithAttr}\b)
4338716de38SJoe Perches			)
4348716de38SJoe Perches			(?:\s+$Modifier|\s+const)*
4358716de38SJoe Perches		  }x;
4368905a67cSAndy Whitcroft	$Type	= qr{
437c45dcabdSAndy Whitcroft			$NonptrType
438b337d8b8SAndy Whitcroft			(?:(?:\s|\*|\[\])+\s*const|(?:\s|\*|\[\])+|(?:\s*\[\s*\])+)?
439c8cb2ca3SAndy Whitcroft			(?:\s+$Inline|\s+$Modifier)*
4408905a67cSAndy Whitcroft		  }x;
44191cb5195SJoe Perches	$Declare	= qr{(?:$Storage\s+(?:$Inline\s+)?)?$Type};
4428905a67cSAndy Whitcroft}
4438905a67cSAndy Whitcroftbuild_types();
4446c72ffaaSAndy Whitcroft
4457d2367afSJoe Perchesour $Typecast	= qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
446d1fe9c09SJoe Perches
447d1fe9c09SJoe Perches# Using $balanced_parens, $LvalOrFunc, or $FuncArg
448d1fe9c09SJoe Perches# requires at least perl version v5.10.0
449d1fe9c09SJoe Perches# Any use must be runtime checked with $^V
450d1fe9c09SJoe Perches
451d1fe9c09SJoe Perchesour $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
4522435880fSJoe Perchesour $LvalOrFunc	= qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*};
453d7c76ba7SJoe Perchesour $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant)};
4547d2367afSJoe Perches
4557d2367afSJoe Perchessub deparenthesize {
4567d2367afSJoe Perches	my ($string) = @_;
4577d2367afSJoe Perches	return "" if (!defined($string));
4585b9553abSJoe Perches
4595b9553abSJoe Perches	while ($string =~ /^\s*\(.*\)\s*$/) {
4605b9553abSJoe Perches		$string =~ s@^\s*\(\s*@@;
4615b9553abSJoe Perches		$string =~ s@\s*\)\s*$@@;
4625b9553abSJoe Perches	}
4635b9553abSJoe Perches
4647d2367afSJoe Perches	$string =~ s@\s+@ @g;
4655b9553abSJoe Perches
4667d2367afSJoe Perches	return $string;
4677d2367afSJoe Perches}
4687d2367afSJoe Perches
4693445686aSJoe Perchessub seed_camelcase_file {
4703445686aSJoe Perches	my ($file) = @_;
4713445686aSJoe Perches
4723445686aSJoe Perches	return if (!(-f $file));
4733445686aSJoe Perches
4743445686aSJoe Perches	local $/;
4753445686aSJoe Perches
4763445686aSJoe Perches	open(my $include_file, '<', "$file")
4773445686aSJoe Perches	    or warn "$P: Can't read '$file' $!\n";
4783445686aSJoe Perches	my $text = <$include_file>;
4793445686aSJoe Perches	close($include_file);
4803445686aSJoe Perches
4813445686aSJoe Perches	my @lines = split('\n', $text);
4823445686aSJoe Perches
4833445686aSJoe Perches	foreach my $line (@lines) {
4843445686aSJoe Perches		next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/);
4853445686aSJoe Perches		if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) {
4863445686aSJoe Perches			$camelcase{$1} = 1;
48711ea516aSJoe Perches		} elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) {
48811ea516aSJoe Perches			$camelcase{$1} = 1;
48911ea516aSJoe Perches		} elsif ($line =~ /^\s*(?:union|struct|enum)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[;\{]/) {
4903445686aSJoe Perches			$camelcase{$1} = 1;
4913445686aSJoe Perches		}
4923445686aSJoe Perches	}
4933445686aSJoe Perches}
4943445686aSJoe Perches
4953445686aSJoe Perchesmy $camelcase_seeded = 0;
4963445686aSJoe Perchessub seed_camelcase_includes {
4973445686aSJoe Perches	return if ($camelcase_seeded);
4983445686aSJoe Perches
4993445686aSJoe Perches	my $files;
500c707a81dSJoe Perches	my $camelcase_cache = "";
501c707a81dSJoe Perches	my @include_files = ();
502c707a81dSJoe Perches
503c707a81dSJoe Perches	$camelcase_seeded = 1;
504351b2a1fSJoe Perches
5053645e328SRichard Genoud	if (-e ".git") {
506351b2a1fSJoe Perches		my $git_last_include_commit = `git log --no-merges --pretty=format:"%h%n" -1 -- include`;
507351b2a1fSJoe Perches		chomp $git_last_include_commit;
508c707a81dSJoe Perches		$camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit";
509c707a81dSJoe Perches	} else {
510c707a81dSJoe Perches		my $last_mod_date = 0;
511c707a81dSJoe Perches		$files = `find $root/include -name "*.h"`;
512c707a81dSJoe Perches		@include_files = split('\n', $files);
513c707a81dSJoe Perches		foreach my $file (@include_files) {
514c707a81dSJoe Perches			my $date = POSIX::strftime("%Y%m%d%H%M",
515c707a81dSJoe Perches						   localtime((stat $file)[9]));
516c707a81dSJoe Perches			$last_mod_date = $date if ($last_mod_date < $date);
517c707a81dSJoe Perches		}
518c707a81dSJoe Perches		$camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date";
519c707a81dSJoe Perches	}
520c707a81dSJoe Perches
521c707a81dSJoe Perches	if ($camelcase_cache ne "" && -f $camelcase_cache) {
522c707a81dSJoe Perches		open(my $camelcase_file, '<', "$camelcase_cache")
523c707a81dSJoe Perches		    or warn "$P: Can't read '$camelcase_cache' $!\n";
524351b2a1fSJoe Perches		while (<$camelcase_file>) {
525351b2a1fSJoe Perches			chomp;
526351b2a1fSJoe Perches			$camelcase{$_} = 1;
527351b2a1fSJoe Perches		}
528351b2a1fSJoe Perches		close($camelcase_file);
529351b2a1fSJoe Perches
530351b2a1fSJoe Perches		return;
531351b2a1fSJoe Perches	}
532c707a81dSJoe Perches
5333645e328SRichard Genoud	if (-e ".git") {
534c707a81dSJoe Perches		$files = `git ls-files "include/*.h"`;
535c707a81dSJoe Perches		@include_files = split('\n', $files);
5363445686aSJoe Perches	}
537c707a81dSJoe Perches
5383445686aSJoe Perches	foreach my $file (@include_files) {
5393445686aSJoe Perches		seed_camelcase_file($file);
5403445686aSJoe Perches	}
541351b2a1fSJoe Perches
542c707a81dSJoe Perches	if ($camelcase_cache ne "") {
543351b2a1fSJoe Perches		unlink glob ".checkpatch-camelcase.*";
544c707a81dSJoe Perches		open(my $camelcase_file, '>', "$camelcase_cache")
545c707a81dSJoe Perches		    or warn "$P: Can't write '$camelcase_cache' $!\n";
546351b2a1fSJoe Perches		foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) {
547351b2a1fSJoe Perches			print $camelcase_file ("$_\n");
548351b2a1fSJoe Perches		}
549351b2a1fSJoe Perches		close($camelcase_file);
550351b2a1fSJoe Perches	}
5513445686aSJoe Perches}
5523445686aSJoe Perches
5536c72ffaaSAndy Whitcroft$chk_signoff = 0 if ($file);
5540a920b5bSAndy Whitcroft
55500df344fSAndy Whitcroftmy @rawlines = ();
556c2fdda0dSAndy Whitcroftmy @lines = ();
5573705ce5bSJoe Perchesmy @fixed = ();
558c2fdda0dSAndy Whitcroftmy $vname;
5596c72ffaaSAndy Whitcroftfor my $filename (@ARGV) {
56021caa13cSAndy Whitcroft	my $FILE;
5616c72ffaaSAndy Whitcroft	if ($file) {
56221caa13cSAndy Whitcroft		open($FILE, '-|', "diff -u /dev/null $filename") ||
5636c72ffaaSAndy Whitcroft			die "$P: $filename: diff failed - $!\n";
56421caa13cSAndy Whitcroft	} elsif ($filename eq '-') {
56521caa13cSAndy Whitcroft		open($FILE, '<&STDIN');
5666c72ffaaSAndy Whitcroft	} else {
56721caa13cSAndy Whitcroft		open($FILE, '<', "$filename") ||
5686c72ffaaSAndy Whitcroft			die "$P: $filename: open failed - $!\n";
5696c72ffaaSAndy Whitcroft	}
570c2fdda0dSAndy Whitcroft	if ($filename eq '-') {
571c2fdda0dSAndy Whitcroft		$vname = 'Your patch';
572c2fdda0dSAndy Whitcroft	} else {
573c2fdda0dSAndy Whitcroft		$vname = $filename;
574c2fdda0dSAndy Whitcroft	}
57521caa13cSAndy Whitcroft	while (<$FILE>) {
5760a920b5bSAndy Whitcroft		chomp;
57700df344fSAndy Whitcroft		push(@rawlines, $_);
5786c72ffaaSAndy Whitcroft	}
57921caa13cSAndy Whitcroft	close($FILE);
580c2fdda0dSAndy Whitcroft	if (!process($filename)) {
5810a920b5bSAndy Whitcroft		$exit = 1;
5820a920b5bSAndy Whitcroft	}
58300df344fSAndy Whitcroft	@rawlines = ();
58413214adfSAndy Whitcroft	@lines = ();
5853705ce5bSJoe Perches	@fixed = ();
5860a920b5bSAndy Whitcroft}
5870a920b5bSAndy Whitcroft
5880a920b5bSAndy Whitcroftexit($exit);
5890a920b5bSAndy Whitcroft
5900a920b5bSAndy Whitcroftsub top_of_kernel_tree {
5916c72ffaaSAndy Whitcroft	my ($root) = @_;
5926c72ffaaSAndy Whitcroft
5936c72ffaaSAndy Whitcroft	my @tree_check = (
5946c72ffaaSAndy Whitcroft		"COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
5956c72ffaaSAndy Whitcroft		"README", "Documentation", "arch", "include", "drivers",
5966c72ffaaSAndy Whitcroft		"fs", "init", "ipc", "kernel", "lib", "scripts",
5976c72ffaaSAndy Whitcroft	);
5986c72ffaaSAndy Whitcroft
5996c72ffaaSAndy Whitcroft	foreach my $check (@tree_check) {
6006c72ffaaSAndy Whitcroft		if (! -e $root . '/' . $check) {
6010a920b5bSAndy Whitcroft			return 0;
6020a920b5bSAndy Whitcroft		}
6036c72ffaaSAndy Whitcroft	}
6046c72ffaaSAndy Whitcroft	return 1;
6056c72ffaaSAndy Whitcroft}
6060a920b5bSAndy Whitcroft
60720112475SJoe Perchessub parse_email {
60820112475SJoe Perches	my ($formatted_email) = @_;
60920112475SJoe Perches
61020112475SJoe Perches	my $name = "";
61120112475SJoe Perches	my $address = "";
61220112475SJoe Perches	my $comment = "";
61320112475SJoe Perches
61420112475SJoe Perches	if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
61520112475SJoe Perches		$name = $1;
61620112475SJoe Perches		$address = $2;
61720112475SJoe Perches		$comment = $3 if defined $3;
61820112475SJoe Perches	} elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
61920112475SJoe Perches		$address = $1;
62020112475SJoe Perches		$comment = $2 if defined $2;
62120112475SJoe Perches	} elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
62220112475SJoe Perches		$address = $1;
62320112475SJoe Perches		$comment = $2 if defined $2;
62420112475SJoe Perches		$formatted_email =~ s/$address.*$//;
62520112475SJoe Perches		$name = $formatted_email;
6263705ce5bSJoe Perches		$name = trim($name);
62720112475SJoe Perches		$name =~ s/^\"|\"$//g;
62820112475SJoe Perches		# If there's a name left after stripping spaces and
62920112475SJoe Perches		# leading quotes, and the address doesn't have both
63020112475SJoe Perches		# leading and trailing angle brackets, the address
63120112475SJoe Perches		# is invalid. ie:
63220112475SJoe Perches		#   "joe smith [email protected]" bad
63320112475SJoe Perches		#   "joe smith <[email protected]" bad
63420112475SJoe Perches		if ($name ne "" && $address !~ /^<[^>]+>$/) {
63520112475SJoe Perches			$name = "";
63620112475SJoe Perches			$address = "";
63720112475SJoe Perches			$comment = "";
63820112475SJoe Perches		}
63920112475SJoe Perches	}
64020112475SJoe Perches
6413705ce5bSJoe Perches	$name = trim($name);
64220112475SJoe Perches	$name =~ s/^\"|\"$//g;
6433705ce5bSJoe Perches	$address = trim($address);
64420112475SJoe Perches	$address =~ s/^\<|\>$//g;
64520112475SJoe Perches
64620112475SJoe Perches	if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
64720112475SJoe Perches		$name =~ s/(?<!\\)"/\\"/g; ##escape quotes
64820112475SJoe Perches		$name = "\"$name\"";
64920112475SJoe Perches	}
65020112475SJoe Perches
65120112475SJoe Perches	return ($name, $address, $comment);
65220112475SJoe Perches}
65320112475SJoe Perches
65420112475SJoe Perchessub format_email {
65520112475SJoe Perches	my ($name, $address) = @_;
65620112475SJoe Perches
65720112475SJoe Perches	my $formatted_email;
65820112475SJoe Perches
6593705ce5bSJoe Perches	$name = trim($name);
66020112475SJoe Perches	$name =~ s/^\"|\"$//g;
6613705ce5bSJoe Perches	$address = trim($address);
66220112475SJoe Perches
66320112475SJoe Perches	if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
66420112475SJoe Perches		$name =~ s/(?<!\\)"/\\"/g; ##escape quotes
66520112475SJoe Perches		$name = "\"$name\"";
66620112475SJoe Perches	}
66720112475SJoe Perches
66820112475SJoe Perches	if ("$name" eq "") {
66920112475SJoe Perches		$formatted_email = "$address";
67020112475SJoe Perches	} else {
67120112475SJoe Perches		$formatted_email = "$name <$address>";
67220112475SJoe Perches	}
67320112475SJoe Perches
67420112475SJoe Perches	return $formatted_email;
67520112475SJoe Perches}
67620112475SJoe Perches
677000d1cc1SJoe Perchessub which_conf {
678000d1cc1SJoe Perches	my ($conf) = @_;
679000d1cc1SJoe Perches
680000d1cc1SJoe Perches	foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
681000d1cc1SJoe Perches		if (-e "$path/$conf") {
682000d1cc1SJoe Perches			return "$path/$conf";
683000d1cc1SJoe Perches		}
684000d1cc1SJoe Perches	}
685000d1cc1SJoe Perches
686000d1cc1SJoe Perches	return "";
687000d1cc1SJoe Perches}
688000d1cc1SJoe Perches
6890a920b5bSAndy Whitcroftsub expand_tabs {
6900a920b5bSAndy Whitcroft	my ($str) = @_;
6910a920b5bSAndy Whitcroft
6920a920b5bSAndy Whitcroft	my $res = '';
6930a920b5bSAndy Whitcroft	my $n = 0;
6940a920b5bSAndy Whitcroft	for my $c (split(//, $str)) {
6950a920b5bSAndy Whitcroft		if ($c eq "\t") {
6960a920b5bSAndy Whitcroft			$res .= ' ';
6970a920b5bSAndy Whitcroft			$n++;
6980a920b5bSAndy Whitcroft			for (; ($n % 8) != 0; $n++) {
6990a920b5bSAndy Whitcroft				$res .= ' ';
7000a920b5bSAndy Whitcroft			}
7010a920b5bSAndy Whitcroft			next;
7020a920b5bSAndy Whitcroft		}
7030a920b5bSAndy Whitcroft		$res .= $c;
7040a920b5bSAndy Whitcroft		$n++;
7050a920b5bSAndy Whitcroft	}
7060a920b5bSAndy Whitcroft
7070a920b5bSAndy Whitcroft	return $res;
7080a920b5bSAndy Whitcroft}
7096c72ffaaSAndy Whitcroftsub copy_spacing {
710773647a0SAndy Whitcroft	(my $res = shift) =~ tr/\t/ /c;
7116c72ffaaSAndy Whitcroft	return $res;
7126c72ffaaSAndy Whitcroft}
7130a920b5bSAndy Whitcroft
7144a0df2efSAndy Whitcroftsub line_stats {
7154a0df2efSAndy Whitcroft	my ($line) = @_;
7164a0df2efSAndy Whitcroft
7174a0df2efSAndy Whitcroft	# Drop the diff line leader and expand tabs
7184a0df2efSAndy Whitcroft	$line =~ s/^.//;
7194a0df2efSAndy Whitcroft	$line = expand_tabs($line);
7204a0df2efSAndy Whitcroft
7214a0df2efSAndy Whitcroft	# Pick the indent from the front of the line.
7224a0df2efSAndy Whitcroft	my ($white) = ($line =~ /^(\s*)/);
7234a0df2efSAndy Whitcroft
7244a0df2efSAndy Whitcroft	return (length($line), length($white));
7254a0df2efSAndy Whitcroft}
7264a0df2efSAndy Whitcroft
727773647a0SAndy Whitcroftmy $sanitise_quote = '';
728773647a0SAndy Whitcroft
729773647a0SAndy Whitcroftsub sanitise_line_reset {
730773647a0SAndy Whitcroft	my ($in_comment) = @_;
731773647a0SAndy Whitcroft
732773647a0SAndy Whitcroft	if ($in_comment) {
733773647a0SAndy Whitcroft		$sanitise_quote = '*/';
734773647a0SAndy Whitcroft	} else {
735773647a0SAndy Whitcroft		$sanitise_quote = '';
736773647a0SAndy Whitcroft	}
737773647a0SAndy Whitcroft}
73800df344fSAndy Whitcroftsub sanitise_line {
73900df344fSAndy Whitcroft	my ($line) = @_;
74000df344fSAndy Whitcroft
74100df344fSAndy Whitcroft	my $res = '';
74200df344fSAndy Whitcroft	my $l = '';
74300df344fSAndy Whitcroft
744c2fdda0dSAndy Whitcroft	my $qlen = 0;
745773647a0SAndy Whitcroft	my $off = 0;
746773647a0SAndy Whitcroft	my $c;
74700df344fSAndy Whitcroft
748773647a0SAndy Whitcroft	# Always copy over the diff marker.
749773647a0SAndy Whitcroft	$res = substr($line, 0, 1);
750773647a0SAndy Whitcroft
751773647a0SAndy Whitcroft	for ($off = 1; $off < length($line); $off++) {
752773647a0SAndy Whitcroft		$c = substr($line, $off, 1);
753773647a0SAndy Whitcroft
754773647a0SAndy Whitcroft		# Comments we are wacking completly including the begin
755773647a0SAndy Whitcroft		# and end, all to $;.
756773647a0SAndy Whitcroft		if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
757773647a0SAndy Whitcroft			$sanitise_quote = '*/';
758773647a0SAndy Whitcroft
759773647a0SAndy Whitcroft			substr($res, $off, 2, "$;$;");
760773647a0SAndy Whitcroft			$off++;
76100df344fSAndy Whitcroft			next;
762773647a0SAndy Whitcroft		}
76381bc0e02SAndy Whitcroft		if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
764773647a0SAndy Whitcroft			$sanitise_quote = '';
765773647a0SAndy Whitcroft			substr($res, $off, 2, "$;$;");
766773647a0SAndy Whitcroft			$off++;
767773647a0SAndy Whitcroft			next;
768773647a0SAndy Whitcroft		}
769113f04a8SDaniel Walker		if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
770113f04a8SDaniel Walker			$sanitise_quote = '//';
771113f04a8SDaniel Walker
772113f04a8SDaniel Walker			substr($res, $off, 2, $sanitise_quote);
773113f04a8SDaniel Walker			$off++;
774113f04a8SDaniel Walker			next;
775113f04a8SDaniel Walker		}
776773647a0SAndy Whitcroft
777773647a0SAndy Whitcroft		# A \ in a string means ignore the next character.
778773647a0SAndy Whitcroft		if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
779773647a0SAndy Whitcroft		    $c eq "\\") {
780773647a0SAndy Whitcroft			substr($res, $off, 2, 'XX');
781773647a0SAndy Whitcroft			$off++;
782773647a0SAndy Whitcroft			next;
783773647a0SAndy Whitcroft		}
784773647a0SAndy Whitcroft		# Regular quotes.
785773647a0SAndy Whitcroft		if ($c eq "'" || $c eq '"') {
786773647a0SAndy Whitcroft			if ($sanitise_quote eq '') {
787773647a0SAndy Whitcroft				$sanitise_quote = $c;
788773647a0SAndy Whitcroft
789773647a0SAndy Whitcroft				substr($res, $off, 1, $c);
790773647a0SAndy Whitcroft				next;
791773647a0SAndy Whitcroft			} elsif ($sanitise_quote eq $c) {
792773647a0SAndy Whitcroft				$sanitise_quote = '';
79300df344fSAndy Whitcroft			}
79400df344fSAndy Whitcroft		}
795773647a0SAndy Whitcroft
796fae17daeSAndy Whitcroft		#print "c<$c> SQ<$sanitise_quote>\n";
797773647a0SAndy Whitcroft		if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
798773647a0SAndy Whitcroft			substr($res, $off, 1, $;);
799113f04a8SDaniel Walker		} elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
800113f04a8SDaniel Walker			substr($res, $off, 1, $;);
801773647a0SAndy Whitcroft		} elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
802773647a0SAndy Whitcroft			substr($res, $off, 1, 'X');
80300df344fSAndy Whitcroft		} else {
804773647a0SAndy Whitcroft			substr($res, $off, 1, $c);
80500df344fSAndy Whitcroft		}
806c2fdda0dSAndy Whitcroft	}
807c2fdda0dSAndy Whitcroft
808113f04a8SDaniel Walker	if ($sanitise_quote eq '//') {
809113f04a8SDaniel Walker		$sanitise_quote = '';
810113f04a8SDaniel Walker	}
811113f04a8SDaniel Walker
812c2fdda0dSAndy Whitcroft	# The pathname on a #include may be surrounded by '<' and '>'.
813c45dcabdSAndy Whitcroft	if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
814c2fdda0dSAndy Whitcroft		my $clean = 'X' x length($1);
815c2fdda0dSAndy Whitcroft		$res =~ s@\<.*\>@<$clean>@;
816c2fdda0dSAndy Whitcroft
817c2fdda0dSAndy Whitcroft	# The whole of a #error is a string.
818c45dcabdSAndy Whitcroft	} elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
819c2fdda0dSAndy Whitcroft		my $clean = 'X' x length($1);
820c45dcabdSAndy Whitcroft		$res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
821c2fdda0dSAndy Whitcroft	}
822c2fdda0dSAndy Whitcroft
82300df344fSAndy Whitcroft	return $res;
82400df344fSAndy Whitcroft}
82500df344fSAndy Whitcroft
826a6962d72SJoe Perchessub get_quoted_string {
827a6962d72SJoe Perches	my ($line, $rawline) = @_;
828a6962d72SJoe Perches
829a6962d72SJoe Perches	return "" if ($line !~ m/(\"[X]+\")/g);
830a6962d72SJoe Perches	return substr($rawline, $-[0], $+[0] - $-[0]);
831a6962d72SJoe Perches}
832a6962d72SJoe Perches
8338905a67cSAndy Whitcroftsub ctx_statement_block {
8348905a67cSAndy Whitcroft	my ($linenr, $remain, $off) = @_;
8358905a67cSAndy Whitcroft	my $line = $linenr - 1;
8368905a67cSAndy Whitcroft	my $blk = '';
8378905a67cSAndy Whitcroft	my $soff = $off;
8388905a67cSAndy Whitcroft	my $coff = $off - 1;
839773647a0SAndy Whitcroft	my $coff_set = 0;
8408905a67cSAndy Whitcroft
84113214adfSAndy Whitcroft	my $loff = 0;
84213214adfSAndy Whitcroft
8438905a67cSAndy Whitcroft	my $type = '';
8448905a67cSAndy Whitcroft	my $level = 0;
845a2750645SAndy Whitcroft	my @stack = ();
846cf655043SAndy Whitcroft	my $p;
8478905a67cSAndy Whitcroft	my $c;
8488905a67cSAndy Whitcroft	my $len = 0;
84913214adfSAndy Whitcroft
85013214adfSAndy Whitcroft	my $remainder;
8518905a67cSAndy Whitcroft	while (1) {
852a2750645SAndy Whitcroft		@stack = (['', 0]) if ($#stack == -1);
853a2750645SAndy Whitcroft
854773647a0SAndy Whitcroft		#warn "CSB: blk<$blk> remain<$remain>\n";
8558905a67cSAndy Whitcroft		# If we are about to drop off the end, pull in more
8568905a67cSAndy Whitcroft		# context.
8578905a67cSAndy Whitcroft		if ($off >= $len) {
8588905a67cSAndy Whitcroft			for (; $remain > 0; $line++) {
859dea33496SAndy Whitcroft				last if (!defined $lines[$line]);
860c2fdda0dSAndy Whitcroft				next if ($lines[$line] =~ /^-/);
8618905a67cSAndy Whitcroft				$remain--;
86213214adfSAndy Whitcroft				$loff = $len;
863c2fdda0dSAndy Whitcroft				$blk .= $lines[$line] . "\n";
8648905a67cSAndy Whitcroft				$len = length($blk);
8658905a67cSAndy Whitcroft				$line++;
8668905a67cSAndy Whitcroft				last;
8678905a67cSAndy Whitcroft			}
8688905a67cSAndy Whitcroft			# Bail if there is no further context.
8698905a67cSAndy Whitcroft			#warn "CSB: blk<$blk> off<$off> len<$len>\n";
87013214adfSAndy Whitcroft			if ($off >= $len) {
8718905a67cSAndy Whitcroft				last;
8728905a67cSAndy Whitcroft			}
873f74bd194SAndy Whitcroft			if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
874f74bd194SAndy Whitcroft				$level++;
875f74bd194SAndy Whitcroft				$type = '#';
876f74bd194SAndy Whitcroft			}
8778905a67cSAndy Whitcroft		}
878cf655043SAndy Whitcroft		$p = $c;
8798905a67cSAndy Whitcroft		$c = substr($blk, $off, 1);
88013214adfSAndy Whitcroft		$remainder = substr($blk, $off);
8818905a67cSAndy Whitcroft
882773647a0SAndy Whitcroft		#warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
8834635f4fbSAndy Whitcroft
8844635f4fbSAndy Whitcroft		# Handle nested #if/#else.
8854635f4fbSAndy Whitcroft		if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
8864635f4fbSAndy Whitcroft			push(@stack, [ $type, $level ]);
8874635f4fbSAndy Whitcroft		} elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
8884635f4fbSAndy Whitcroft			($type, $level) = @{$stack[$#stack - 1]};
8894635f4fbSAndy Whitcroft		} elsif ($remainder =~ /^#\s*endif\b/) {
8904635f4fbSAndy Whitcroft			($type, $level) = @{pop(@stack)};
8914635f4fbSAndy Whitcroft		}
8924635f4fbSAndy Whitcroft
8938905a67cSAndy Whitcroft		# Statement ends at the ';' or a close '}' at the
8948905a67cSAndy Whitcroft		# outermost level.
8958905a67cSAndy Whitcroft		if ($level == 0 && $c eq ';') {
8968905a67cSAndy Whitcroft			last;
8978905a67cSAndy Whitcroft		}
8988905a67cSAndy Whitcroft
89913214adfSAndy Whitcroft		# An else is really a conditional as long as its not else if
900773647a0SAndy Whitcroft		if ($level == 0 && $coff_set == 0 &&
901773647a0SAndy Whitcroft				(!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
902773647a0SAndy Whitcroft				$remainder =~ /^(else)(?:\s|{)/ &&
903773647a0SAndy Whitcroft				$remainder !~ /^else\s+if\b/) {
904773647a0SAndy Whitcroft			$coff = $off + length($1) - 1;
905773647a0SAndy Whitcroft			$coff_set = 1;
906773647a0SAndy Whitcroft			#warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
907773647a0SAndy Whitcroft			#warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
90813214adfSAndy Whitcroft		}
90913214adfSAndy Whitcroft
9108905a67cSAndy Whitcroft		if (($type eq '' || $type eq '(') && $c eq '(') {
9118905a67cSAndy Whitcroft			$level++;
9128905a67cSAndy Whitcroft			$type = '(';
9138905a67cSAndy Whitcroft		}
9148905a67cSAndy Whitcroft		if ($type eq '(' && $c eq ')') {
9158905a67cSAndy Whitcroft			$level--;
9168905a67cSAndy Whitcroft			$type = ($level != 0)? '(' : '';
9178905a67cSAndy Whitcroft
9188905a67cSAndy Whitcroft			if ($level == 0 && $coff < $soff) {
9198905a67cSAndy Whitcroft				$coff = $off;
920773647a0SAndy Whitcroft				$coff_set = 1;
921773647a0SAndy Whitcroft				#warn "CSB: mark coff<$coff>\n";
9228905a67cSAndy Whitcroft			}
9238905a67cSAndy Whitcroft		}
9248905a67cSAndy Whitcroft		if (($type eq '' || $type eq '{') && $c eq '{') {
9258905a67cSAndy Whitcroft			$level++;
9268905a67cSAndy Whitcroft			$type = '{';
9278905a67cSAndy Whitcroft		}
9288905a67cSAndy Whitcroft		if ($type eq '{' && $c eq '}') {
9298905a67cSAndy Whitcroft			$level--;
9308905a67cSAndy Whitcroft			$type = ($level != 0)? '{' : '';
9318905a67cSAndy Whitcroft
9328905a67cSAndy Whitcroft			if ($level == 0) {
933b998e001SPatrick Pannuto				if (substr($blk, $off + 1, 1) eq ';') {
934b998e001SPatrick Pannuto					$off++;
935b998e001SPatrick Pannuto				}
9368905a67cSAndy Whitcroft				last;
9378905a67cSAndy Whitcroft			}
9388905a67cSAndy Whitcroft		}
939f74bd194SAndy Whitcroft		# Preprocessor commands end at the newline unless escaped.
940f74bd194SAndy Whitcroft		if ($type eq '#' && $c eq "\n" && $p ne "\\") {
941f74bd194SAndy Whitcroft			$level--;
942f74bd194SAndy Whitcroft			$type = '';
943f74bd194SAndy Whitcroft			$off++;
944f74bd194SAndy Whitcroft			last;
945f74bd194SAndy Whitcroft		}
9468905a67cSAndy Whitcroft		$off++;
9478905a67cSAndy Whitcroft	}
948a3bb97a7SAndy Whitcroft	# We are truly at the end, so shuffle to the next line.
94913214adfSAndy Whitcroft	if ($off == $len) {
950a3bb97a7SAndy Whitcroft		$loff = $len + 1;
95113214adfSAndy Whitcroft		$line++;
95213214adfSAndy Whitcroft		$remain--;
95313214adfSAndy Whitcroft	}
9548905a67cSAndy Whitcroft
9558905a67cSAndy Whitcroft	my $statement = substr($blk, $soff, $off - $soff + 1);
9568905a67cSAndy Whitcroft	my $condition = substr($blk, $soff, $coff - $soff + 1);
9578905a67cSAndy Whitcroft
9588905a67cSAndy Whitcroft	#warn "STATEMENT<$statement>\n";
9598905a67cSAndy Whitcroft	#warn "CONDITION<$condition>\n";
9608905a67cSAndy Whitcroft
961773647a0SAndy Whitcroft	#print "coff<$coff> soff<$off> loff<$loff>\n";
96213214adfSAndy Whitcroft
96313214adfSAndy Whitcroft	return ($statement, $condition,
96413214adfSAndy Whitcroft			$line, $remain + 1, $off - $loff + 1, $level);
96513214adfSAndy Whitcroft}
96613214adfSAndy Whitcroft
967cf655043SAndy Whitcroftsub statement_lines {
968cf655043SAndy Whitcroft	my ($stmt) = @_;
969cf655043SAndy Whitcroft
970cf655043SAndy Whitcroft	# Strip the diff line prefixes and rip blank lines at start and end.
971cf655043SAndy Whitcroft	$stmt =~ s/(^|\n)./$1/g;
972cf655043SAndy Whitcroft	$stmt =~ s/^\s*//;
973cf655043SAndy Whitcroft	$stmt =~ s/\s*$//;
974cf655043SAndy Whitcroft
975cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
976cf655043SAndy Whitcroft
977cf655043SAndy Whitcroft	return $#stmt_lines + 2;
978cf655043SAndy Whitcroft}
979cf655043SAndy Whitcroft
980cf655043SAndy Whitcroftsub statement_rawlines {
981cf655043SAndy Whitcroft	my ($stmt) = @_;
982cf655043SAndy Whitcroft
983cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
984cf655043SAndy Whitcroft
985cf655043SAndy Whitcroft	return $#stmt_lines + 2;
986cf655043SAndy Whitcroft}
987cf655043SAndy Whitcroft
988cf655043SAndy Whitcroftsub statement_block_size {
989cf655043SAndy Whitcroft	my ($stmt) = @_;
990cf655043SAndy Whitcroft
991cf655043SAndy Whitcroft	$stmt =~ s/(^|\n)./$1/g;
992cf655043SAndy Whitcroft	$stmt =~ s/^\s*{//;
993cf655043SAndy Whitcroft	$stmt =~ s/}\s*$//;
994cf655043SAndy Whitcroft	$stmt =~ s/^\s*//;
995cf655043SAndy Whitcroft	$stmt =~ s/\s*$//;
996cf655043SAndy Whitcroft
997cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
998cf655043SAndy Whitcroft	my @stmt_statements = ($stmt =~ /;/g);
999cf655043SAndy Whitcroft
1000cf655043SAndy Whitcroft	my $stmt_lines = $#stmt_lines + 2;
1001cf655043SAndy Whitcroft	my $stmt_statements = $#stmt_statements + 1;
1002cf655043SAndy Whitcroft
1003cf655043SAndy Whitcroft	if ($stmt_lines > $stmt_statements) {
1004cf655043SAndy Whitcroft		return $stmt_lines;
1005cf655043SAndy Whitcroft	} else {
1006cf655043SAndy Whitcroft		return $stmt_statements;
1007cf655043SAndy Whitcroft	}
1008cf655043SAndy Whitcroft}
1009cf655043SAndy Whitcroft
101013214adfSAndy Whitcroftsub ctx_statement_full {
101113214adfSAndy Whitcroft	my ($linenr, $remain, $off) = @_;
101213214adfSAndy Whitcroft	my ($statement, $condition, $level);
101313214adfSAndy Whitcroft
101413214adfSAndy Whitcroft	my (@chunks);
101513214adfSAndy Whitcroft
1016cf655043SAndy Whitcroft	# Grab the first conditional/block pair.
101713214adfSAndy Whitcroft	($statement, $condition, $linenr, $remain, $off, $level) =
101813214adfSAndy Whitcroft				ctx_statement_block($linenr, $remain, $off);
1019773647a0SAndy Whitcroft	#print "F: c<$condition> s<$statement> remain<$remain>\n";
102013214adfSAndy Whitcroft	push(@chunks, [ $condition, $statement ]);
1021cf655043SAndy Whitcroft	if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
1022cf655043SAndy Whitcroft		return ($level, $linenr, @chunks);
1023cf655043SAndy Whitcroft	}
1024cf655043SAndy Whitcroft
1025cf655043SAndy Whitcroft	# Pull in the following conditional/block pairs and see if they
1026cf655043SAndy Whitcroft	# could continue the statement.
1027cf655043SAndy Whitcroft	for (;;) {
102813214adfSAndy Whitcroft		($statement, $condition, $linenr, $remain, $off, $level) =
102913214adfSAndy Whitcroft				ctx_statement_block($linenr, $remain, $off);
1030cf655043SAndy Whitcroft		#print "C: c<$condition> s<$statement> remain<$remain>\n";
1031773647a0SAndy Whitcroft		last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
1032cf655043SAndy Whitcroft		#print "C: push\n";
1033cf655043SAndy Whitcroft		push(@chunks, [ $condition, $statement ]);
103413214adfSAndy Whitcroft	}
103513214adfSAndy Whitcroft
103613214adfSAndy Whitcroft	return ($level, $linenr, @chunks);
10378905a67cSAndy Whitcroft}
10388905a67cSAndy Whitcroft
10394a0df2efSAndy Whitcroftsub ctx_block_get {
1040f0a594c1SAndy Whitcroft	my ($linenr, $remain, $outer, $open, $close, $off) = @_;
10414a0df2efSAndy Whitcroft	my $line;
10424a0df2efSAndy Whitcroft	my $start = $linenr - 1;
10434a0df2efSAndy Whitcroft	my $blk = '';
10444a0df2efSAndy Whitcroft	my @o;
10454a0df2efSAndy Whitcroft	my @c;
10464a0df2efSAndy Whitcroft	my @res = ();
10474a0df2efSAndy Whitcroft
1048f0a594c1SAndy Whitcroft	my $level = 0;
10494635f4fbSAndy Whitcroft	my @stack = ($level);
105000df344fSAndy Whitcroft	for ($line = $start; $remain > 0; $line++) {
105100df344fSAndy Whitcroft		next if ($rawlines[$line] =~ /^-/);
105200df344fSAndy Whitcroft		$remain--;
105300df344fSAndy Whitcroft
105400df344fSAndy Whitcroft		$blk .= $rawlines[$line];
10554635f4fbSAndy Whitcroft
10564635f4fbSAndy Whitcroft		# Handle nested #if/#else.
105701464f30SAndy Whitcroft		if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
10584635f4fbSAndy Whitcroft			push(@stack, $level);
105901464f30SAndy Whitcroft		} elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
10604635f4fbSAndy Whitcroft			$level = $stack[$#stack - 1];
106101464f30SAndy Whitcroft		} elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
10624635f4fbSAndy Whitcroft			$level = pop(@stack);
10634635f4fbSAndy Whitcroft		}
10644635f4fbSAndy Whitcroft
106501464f30SAndy Whitcroft		foreach my $c (split(//, $lines[$line])) {
1066f0a594c1SAndy Whitcroft			##print "C<$c>L<$level><$open$close>O<$off>\n";
1067f0a594c1SAndy Whitcroft			if ($off > 0) {
1068f0a594c1SAndy Whitcroft				$off--;
1069f0a594c1SAndy Whitcroft				next;
1070f0a594c1SAndy Whitcroft			}
10714a0df2efSAndy Whitcroft
1072f0a594c1SAndy Whitcroft			if ($c eq $close && $level > 0) {
1073f0a594c1SAndy Whitcroft				$level--;
1074f0a594c1SAndy Whitcroft				last if ($level == 0);
1075f0a594c1SAndy Whitcroft			} elsif ($c eq $open) {
1076f0a594c1SAndy Whitcroft				$level++;
1077f0a594c1SAndy Whitcroft			}
1078f0a594c1SAndy Whitcroft		}
10794a0df2efSAndy Whitcroft
1080f0a594c1SAndy Whitcroft		if (!$outer || $level <= 1) {
108100df344fSAndy Whitcroft			push(@res, $rawlines[$line]);
10824a0df2efSAndy Whitcroft		}
10834a0df2efSAndy Whitcroft
1084f0a594c1SAndy Whitcroft		last if ($level == 0);
10854a0df2efSAndy Whitcroft	}
10864a0df2efSAndy Whitcroft
1087f0a594c1SAndy Whitcroft	return ($level, @res);
10884a0df2efSAndy Whitcroft}
10894a0df2efSAndy Whitcroftsub ctx_block_outer {
10904a0df2efSAndy Whitcroft	my ($linenr, $remain) = @_;
10914a0df2efSAndy Whitcroft
1092f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
1093f0a594c1SAndy Whitcroft	return @r;
10944a0df2efSAndy Whitcroft}
10954a0df2efSAndy Whitcroftsub ctx_block {
10964a0df2efSAndy Whitcroft	my ($linenr, $remain) = @_;
10974a0df2efSAndy Whitcroft
1098f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1099f0a594c1SAndy Whitcroft	return @r;
1100653d4876SAndy Whitcroft}
1101653d4876SAndy Whitcroftsub ctx_statement {
1102f0a594c1SAndy Whitcroft	my ($linenr, $remain, $off) = @_;
1103f0a594c1SAndy Whitcroft
1104f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1105f0a594c1SAndy Whitcroft	return @r;
1106f0a594c1SAndy Whitcroft}
1107f0a594c1SAndy Whitcroftsub ctx_block_level {
1108653d4876SAndy Whitcroft	my ($linenr, $remain) = @_;
1109653d4876SAndy Whitcroft
1110f0a594c1SAndy Whitcroft	return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
11114a0df2efSAndy Whitcroft}
11129c0ca6f9SAndy Whitcroftsub ctx_statement_level {
11139c0ca6f9SAndy Whitcroft	my ($linenr, $remain, $off) = @_;
11149c0ca6f9SAndy Whitcroft
11159c0ca6f9SAndy Whitcroft	return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
11169c0ca6f9SAndy Whitcroft}
11174a0df2efSAndy Whitcroft
11184a0df2efSAndy Whitcroftsub ctx_locate_comment {
11194a0df2efSAndy Whitcroft	my ($first_line, $end_line) = @_;
11204a0df2efSAndy Whitcroft
11214a0df2efSAndy Whitcroft	# Catch a comment on the end of the line itself.
1122beae6332SAndy Whitcroft	my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
11234a0df2efSAndy Whitcroft	return $current_comment if (defined $current_comment);
11244a0df2efSAndy Whitcroft
11254a0df2efSAndy Whitcroft	# Look through the context and try and figure out if there is a
11264a0df2efSAndy Whitcroft	# comment.
11274a0df2efSAndy Whitcroft	my $in_comment = 0;
11284a0df2efSAndy Whitcroft	$current_comment = '';
11294a0df2efSAndy Whitcroft	for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
113000df344fSAndy Whitcroft		my $line = $rawlines[$linenr - 1];
113100df344fSAndy Whitcroft		#warn "           $line\n";
11324a0df2efSAndy Whitcroft		if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
11334a0df2efSAndy Whitcroft			$in_comment = 1;
11344a0df2efSAndy Whitcroft		}
11354a0df2efSAndy Whitcroft		if ($line =~ m@/\*@) {
11364a0df2efSAndy Whitcroft			$in_comment = 1;
11374a0df2efSAndy Whitcroft		}
11384a0df2efSAndy Whitcroft		if (!$in_comment && $current_comment ne '') {
11394a0df2efSAndy Whitcroft			$current_comment = '';
11404a0df2efSAndy Whitcroft		}
11414a0df2efSAndy Whitcroft		$current_comment .= $line . "\n" if ($in_comment);
11424a0df2efSAndy Whitcroft		if ($line =~ m@\*/@) {
11434a0df2efSAndy Whitcroft			$in_comment = 0;
11444a0df2efSAndy Whitcroft		}
11454a0df2efSAndy Whitcroft	}
11464a0df2efSAndy Whitcroft
11474a0df2efSAndy Whitcroft	chomp($current_comment);
11484a0df2efSAndy Whitcroft	return($current_comment);
11494a0df2efSAndy Whitcroft}
11504a0df2efSAndy Whitcroftsub ctx_has_comment {
11514a0df2efSAndy Whitcroft	my ($first_line, $end_line) = @_;
11524a0df2efSAndy Whitcroft	my $cmt = ctx_locate_comment($first_line, $end_line);
11534a0df2efSAndy Whitcroft
115400df344fSAndy Whitcroft	##print "LINE: $rawlines[$end_line - 1 ]\n";
11554a0df2efSAndy Whitcroft	##print "CMMT: $cmt\n";
11564a0df2efSAndy Whitcroft
11574a0df2efSAndy Whitcroft	return ($cmt ne '');
11584a0df2efSAndy Whitcroft}
11594a0df2efSAndy Whitcroft
11604d001e4dSAndy Whitcroftsub raw_line {
11614d001e4dSAndy Whitcroft	my ($linenr, $cnt) = @_;
11624d001e4dSAndy Whitcroft
11634d001e4dSAndy Whitcroft	my $offset = $linenr - 1;
11644d001e4dSAndy Whitcroft	$cnt++;
11654d001e4dSAndy Whitcroft
11664d001e4dSAndy Whitcroft	my $line;
11674d001e4dSAndy Whitcroft	while ($cnt) {
11684d001e4dSAndy Whitcroft		$line = $rawlines[$offset++];
11694d001e4dSAndy Whitcroft		next if (defined($line) && $line =~ /^-/);
11704d001e4dSAndy Whitcroft		$cnt--;
11714d001e4dSAndy Whitcroft	}
11724d001e4dSAndy Whitcroft
11734d001e4dSAndy Whitcroft	return $line;
11744d001e4dSAndy Whitcroft}
11754d001e4dSAndy Whitcroft
11760a920b5bSAndy Whitcroftsub cat_vet {
11770a920b5bSAndy Whitcroft	my ($vet) = @_;
11789c0ca6f9SAndy Whitcroft	my ($res, $coded);
11790a920b5bSAndy Whitcroft
11809c0ca6f9SAndy Whitcroft	$res = '';
11816c72ffaaSAndy Whitcroft	while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
11826c72ffaaSAndy Whitcroft		$res .= $1;
11836c72ffaaSAndy Whitcroft		if ($2 ne '') {
11849c0ca6f9SAndy Whitcroft			$coded = sprintf("^%c", unpack('C', $2) + 64);
11856c72ffaaSAndy Whitcroft			$res .= $coded;
11866c72ffaaSAndy Whitcroft		}
11879c0ca6f9SAndy Whitcroft	}
11889c0ca6f9SAndy Whitcroft	$res =~ s/$/\$/;
11890a920b5bSAndy Whitcroft
11909c0ca6f9SAndy Whitcroft	return $res;
11910a920b5bSAndy Whitcroft}
11920a920b5bSAndy Whitcroft
1193c2fdda0dSAndy Whitcroftmy $av_preprocessor = 0;
1194cf655043SAndy Whitcroftmy $av_pending;
1195c2fdda0dSAndy Whitcroftmy @av_paren_type;
11961f65f947SAndy Whitcroftmy $av_pend_colon;
1197c2fdda0dSAndy Whitcroft
1198c2fdda0dSAndy Whitcroftsub annotate_reset {
1199c2fdda0dSAndy Whitcroft	$av_preprocessor = 0;
1200cf655043SAndy Whitcroft	$av_pending = '_';
1201cf655043SAndy Whitcroft	@av_paren_type = ('E');
12021f65f947SAndy Whitcroft	$av_pend_colon = 'O';
1203c2fdda0dSAndy Whitcroft}
1204c2fdda0dSAndy Whitcroft
12056c72ffaaSAndy Whitcroftsub annotate_values {
12066c72ffaaSAndy Whitcroft	my ($stream, $type) = @_;
12076c72ffaaSAndy Whitcroft
12086c72ffaaSAndy Whitcroft	my $res;
12091f65f947SAndy Whitcroft	my $var = '_' x length($stream);
12106c72ffaaSAndy Whitcroft	my $cur = $stream;
12116c72ffaaSAndy Whitcroft
1212c2fdda0dSAndy Whitcroft	print "$stream\n" if ($dbg_values > 1);
12136c72ffaaSAndy Whitcroft
12146c72ffaaSAndy Whitcroft	while (length($cur)) {
1215773647a0SAndy Whitcroft		@av_paren_type = ('E') if ($#av_paren_type < 0);
1216cf655043SAndy Whitcroft		print " <" . join('', @av_paren_type) .
1217171ae1a4SAndy Whitcroft				"> <$type> <$av_pending>" if ($dbg_values > 1);
12186c72ffaaSAndy Whitcroft		if ($cur =~ /^(\s+)/o) {
1219c2fdda0dSAndy Whitcroft			print "WS($1)\n" if ($dbg_values > 1);
1220c2fdda0dSAndy Whitcroft			if ($1 =~ /\n/ && $av_preprocessor) {
1221cf655043SAndy Whitcroft				$type = pop(@av_paren_type);
1222c2fdda0dSAndy Whitcroft				$av_preprocessor = 0;
12236c72ffaaSAndy Whitcroft			}
12246c72ffaaSAndy Whitcroft
1225c023e473SFlorian Mickler		} elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
12269446ef56SAndy Whitcroft			print "CAST($1)\n" if ($dbg_values > 1);
12279446ef56SAndy Whitcroft			push(@av_paren_type, $type);
1228addcdceaSAndy Whitcroft			$type = 'c';
12299446ef56SAndy Whitcroft
1230e91b6e26SAndy Whitcroft		} elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
1231c2fdda0dSAndy Whitcroft			print "DECLARE($1)\n" if ($dbg_values > 1);
12326c72ffaaSAndy Whitcroft			$type = 'T';
12336c72ffaaSAndy Whitcroft
1234389a2fe5SAndy Whitcroft		} elsif ($cur =~ /^($Modifier)\s*/) {
1235389a2fe5SAndy Whitcroft			print "MODIFIER($1)\n" if ($dbg_values > 1);
1236389a2fe5SAndy Whitcroft			$type = 'T';
1237389a2fe5SAndy Whitcroft
1238c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
1239171ae1a4SAndy Whitcroft			print "DEFINE($1,$2)\n" if ($dbg_values > 1);
1240c2fdda0dSAndy Whitcroft			$av_preprocessor = 1;
1241171ae1a4SAndy Whitcroft			push(@av_paren_type, $type);
1242171ae1a4SAndy Whitcroft			if ($2 ne '') {
1243cf655043SAndy Whitcroft				$av_pending = 'N';
1244171ae1a4SAndy Whitcroft			}
1245171ae1a4SAndy Whitcroft			$type = 'E';
1246171ae1a4SAndy Whitcroft
1247c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
1248171ae1a4SAndy Whitcroft			print "UNDEF($1)\n" if ($dbg_values > 1);
1249171ae1a4SAndy Whitcroft			$av_preprocessor = 1;
1250171ae1a4SAndy Whitcroft			push(@av_paren_type, $type);
12516c72ffaaSAndy Whitcroft
1252c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
1253cf655043SAndy Whitcroft			print "PRE_START($1)\n" if ($dbg_values > 1);
1254c2fdda0dSAndy Whitcroft			$av_preprocessor = 1;
1255cf655043SAndy Whitcroft
1256cf655043SAndy Whitcroft			push(@av_paren_type, $type);
1257cf655043SAndy Whitcroft			push(@av_paren_type, $type);
1258171ae1a4SAndy Whitcroft			$type = 'E';
1259cf655043SAndy Whitcroft
1260c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
1261cf655043SAndy Whitcroft			print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1262cf655043SAndy Whitcroft			$av_preprocessor = 1;
1263cf655043SAndy Whitcroft
1264cf655043SAndy Whitcroft			push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1265cf655043SAndy Whitcroft
1266171ae1a4SAndy Whitcroft			$type = 'E';
1267cf655043SAndy Whitcroft
1268c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:endif))/o) {
1269cf655043SAndy Whitcroft			print "PRE_END($1)\n" if ($dbg_values > 1);
1270cf655043SAndy Whitcroft
1271cf655043SAndy Whitcroft			$av_preprocessor = 1;
1272cf655043SAndy Whitcroft
1273cf655043SAndy Whitcroft			# Assume all arms of the conditional end as this
1274cf655043SAndy Whitcroft			# one does, and continue as if the #endif was not here.
1275cf655043SAndy Whitcroft			pop(@av_paren_type);
1276cf655043SAndy Whitcroft			push(@av_paren_type, $type);
1277171ae1a4SAndy Whitcroft			$type = 'E';
12786c72ffaaSAndy Whitcroft
12796c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\\\n)/o) {
1280c2fdda0dSAndy Whitcroft			print "PRECONT($1)\n" if ($dbg_values > 1);
12816c72ffaaSAndy Whitcroft
1282171ae1a4SAndy Whitcroft		} elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1283171ae1a4SAndy Whitcroft			print "ATTR($1)\n" if ($dbg_values > 1);
1284171ae1a4SAndy Whitcroft			$av_pending = $type;
1285171ae1a4SAndy Whitcroft			$type = 'N';
1286171ae1a4SAndy Whitcroft
12876c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
1288c2fdda0dSAndy Whitcroft			print "SIZEOF($1)\n" if ($dbg_values > 1);
12896c72ffaaSAndy Whitcroft			if (defined $2) {
1290cf655043SAndy Whitcroft				$av_pending = 'V';
12916c72ffaaSAndy Whitcroft			}
12926c72ffaaSAndy Whitcroft			$type = 'N';
12936c72ffaaSAndy Whitcroft
129414b111c1SAndy Whitcroft		} elsif ($cur =~ /^(if|while|for)\b/o) {
1295c2fdda0dSAndy Whitcroft			print "COND($1)\n" if ($dbg_values > 1);
129614b111c1SAndy Whitcroft			$av_pending = 'E';
12976c72ffaaSAndy Whitcroft			$type = 'N';
12986c72ffaaSAndy Whitcroft
12991f65f947SAndy Whitcroft		} elsif ($cur =~/^(case)/o) {
13001f65f947SAndy Whitcroft			print "CASE($1)\n" if ($dbg_values > 1);
13011f65f947SAndy Whitcroft			$av_pend_colon = 'C';
13021f65f947SAndy Whitcroft			$type = 'N';
13031f65f947SAndy Whitcroft
130414b111c1SAndy Whitcroft		} elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
1305c2fdda0dSAndy Whitcroft			print "KEYWORD($1)\n" if ($dbg_values > 1);
13066c72ffaaSAndy Whitcroft			$type = 'N';
13076c72ffaaSAndy Whitcroft
13086c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\()/o) {
1309c2fdda0dSAndy Whitcroft			print "PAREN('$1')\n" if ($dbg_values > 1);
1310cf655043SAndy Whitcroft			push(@av_paren_type, $av_pending);
1311cf655043SAndy Whitcroft			$av_pending = '_';
13126c72ffaaSAndy Whitcroft			$type = 'N';
13136c72ffaaSAndy Whitcroft
13146c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\))/o) {
1315cf655043SAndy Whitcroft			my $new_type = pop(@av_paren_type);
1316cf655043SAndy Whitcroft			if ($new_type ne '_') {
1317cf655043SAndy Whitcroft				$type = $new_type;
1318c2fdda0dSAndy Whitcroft				print "PAREN('$1') -> $type\n"
1319c2fdda0dSAndy Whitcroft							if ($dbg_values > 1);
13206c72ffaaSAndy Whitcroft			} else {
1321c2fdda0dSAndy Whitcroft				print "PAREN('$1')\n" if ($dbg_values > 1);
13226c72ffaaSAndy Whitcroft			}
13236c72ffaaSAndy Whitcroft
1324c8cb2ca3SAndy Whitcroft		} elsif ($cur =~ /^($Ident)\s*\(/o) {
1325c2fdda0dSAndy Whitcroft			print "FUNC($1)\n" if ($dbg_values > 1);
1326c8cb2ca3SAndy Whitcroft			$type = 'V';
1327cf655043SAndy Whitcroft			$av_pending = 'V';
13286c72ffaaSAndy Whitcroft
13298e761b04SAndy Whitcroft		} elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
13308e761b04SAndy Whitcroft			if (defined $2 && $type eq 'C' || $type eq 'T') {
13311f65f947SAndy Whitcroft				$av_pend_colon = 'B';
13328e761b04SAndy Whitcroft			} elsif ($type eq 'E') {
13338e761b04SAndy Whitcroft				$av_pend_colon = 'L';
13341f65f947SAndy Whitcroft			}
13351f65f947SAndy Whitcroft			print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
13361f65f947SAndy Whitcroft			$type = 'V';
13371f65f947SAndy Whitcroft
13386c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Ident|$Constant)/o) {
1339c2fdda0dSAndy Whitcroft			print "IDENT($1)\n" if ($dbg_values > 1);
13406c72ffaaSAndy Whitcroft			$type = 'V';
13416c72ffaaSAndy Whitcroft
13426c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Assignment)/o) {
1343c2fdda0dSAndy Whitcroft			print "ASSIGN($1)\n" if ($dbg_values > 1);
13446c72ffaaSAndy Whitcroft			$type = 'N';
13456c72ffaaSAndy Whitcroft
1346cf655043SAndy Whitcroft		} elsif ($cur =~/^(;|{|})/) {
1347c2fdda0dSAndy Whitcroft			print "END($1)\n" if ($dbg_values > 1);
134813214adfSAndy Whitcroft			$type = 'E';
13491f65f947SAndy Whitcroft			$av_pend_colon = 'O';
135013214adfSAndy Whitcroft
13518e761b04SAndy Whitcroft		} elsif ($cur =~/^(,)/) {
13528e761b04SAndy Whitcroft			print "COMMA($1)\n" if ($dbg_values > 1);
13538e761b04SAndy Whitcroft			$type = 'C';
13548e761b04SAndy Whitcroft
13551f65f947SAndy Whitcroft		} elsif ($cur =~ /^(\?)/o) {
13561f65f947SAndy Whitcroft			print "QUESTION($1)\n" if ($dbg_values > 1);
13571f65f947SAndy Whitcroft			$type = 'N';
13581f65f947SAndy Whitcroft
13591f65f947SAndy Whitcroft		} elsif ($cur =~ /^(:)/o) {
13601f65f947SAndy Whitcroft			print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
13611f65f947SAndy Whitcroft
13621f65f947SAndy Whitcroft			substr($var, length($res), 1, $av_pend_colon);
13631f65f947SAndy Whitcroft			if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
13641f65f947SAndy Whitcroft				$type = 'E';
13651f65f947SAndy Whitcroft			} else {
13661f65f947SAndy Whitcroft				$type = 'N';
13671f65f947SAndy Whitcroft			}
13681f65f947SAndy Whitcroft			$av_pend_colon = 'O';
13691f65f947SAndy Whitcroft
13708e761b04SAndy Whitcroft		} elsif ($cur =~ /^(\[)/o) {
137113214adfSAndy Whitcroft			print "CLOSE($1)\n" if ($dbg_values > 1);
13726c72ffaaSAndy Whitcroft			$type = 'N';
13736c72ffaaSAndy Whitcroft
13740d413866SAndy Whitcroft		} elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
137574048ed8SAndy Whitcroft			my $variant;
137674048ed8SAndy Whitcroft
137774048ed8SAndy Whitcroft			print "OPV($1)\n" if ($dbg_values > 1);
137874048ed8SAndy Whitcroft			if ($type eq 'V') {
137974048ed8SAndy Whitcroft				$variant = 'B';
138074048ed8SAndy Whitcroft			} else {
138174048ed8SAndy Whitcroft				$variant = 'U';
138274048ed8SAndy Whitcroft			}
138374048ed8SAndy Whitcroft
138474048ed8SAndy Whitcroft			substr($var, length($res), 1, $variant);
138574048ed8SAndy Whitcroft			$type = 'N';
138674048ed8SAndy Whitcroft
13876c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Operators)/o) {
1388c2fdda0dSAndy Whitcroft			print "OP($1)\n" if ($dbg_values > 1);
13896c72ffaaSAndy Whitcroft			if ($1 ne '++' && $1 ne '--') {
13906c72ffaaSAndy Whitcroft				$type = 'N';
13916c72ffaaSAndy Whitcroft			}
13926c72ffaaSAndy Whitcroft
13936c72ffaaSAndy Whitcroft		} elsif ($cur =~ /(^.)/o) {
1394c2fdda0dSAndy Whitcroft			print "C($1)\n" if ($dbg_values > 1);
13956c72ffaaSAndy Whitcroft		}
13966c72ffaaSAndy Whitcroft		if (defined $1) {
13976c72ffaaSAndy Whitcroft			$cur = substr($cur, length($1));
13986c72ffaaSAndy Whitcroft			$res .= $type x length($1);
13996c72ffaaSAndy Whitcroft		}
14006c72ffaaSAndy Whitcroft	}
14016c72ffaaSAndy Whitcroft
14021f65f947SAndy Whitcroft	return ($res, $var);
14036c72ffaaSAndy Whitcroft}
14046c72ffaaSAndy Whitcroft
14058905a67cSAndy Whitcroftsub possible {
140613214adfSAndy Whitcroft	my ($possible, $line) = @_;
14079a974fdbSAndy Whitcroft	my $notPermitted = qr{(?:
14080776e594SAndy Whitcroft		^(?:
14090776e594SAndy Whitcroft			$Modifier|
14100776e594SAndy Whitcroft			$Storage|
14110776e594SAndy Whitcroft			$Type|
14129a974fdbSAndy Whitcroft			DEFINE_\S+
14139a974fdbSAndy Whitcroft		)$|
14149a974fdbSAndy Whitcroft		^(?:
14150776e594SAndy Whitcroft			goto|
14160776e594SAndy Whitcroft			return|
14170776e594SAndy Whitcroft			case|
14180776e594SAndy Whitcroft			else|
14190776e594SAndy Whitcroft			asm|__asm__|
142089a88353SAndy Whitcroft			do|
142189a88353SAndy Whitcroft			\#|
142289a88353SAndy Whitcroft			\#\#|
14239a974fdbSAndy Whitcroft		)(?:\s|$)|
14240776e594SAndy Whitcroft		^(?:typedef|struct|enum)\b
14259a974fdbSAndy Whitcroft	    )}x;
14269a974fdbSAndy Whitcroft	warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
14279a974fdbSAndy Whitcroft	if ($possible !~ $notPermitted) {
1428c45dcabdSAndy Whitcroft		# Check for modifiers.
1429c45dcabdSAndy Whitcroft		$possible =~ s/\s*$Storage\s*//g;
1430c45dcabdSAndy Whitcroft		$possible =~ s/\s*$Sparse\s*//g;
1431c45dcabdSAndy Whitcroft		if ($possible =~ /^\s*$/) {
1432c45dcabdSAndy Whitcroft
1433c45dcabdSAndy Whitcroft		} elsif ($possible =~ /\s/) {
1434c45dcabdSAndy Whitcroft			$possible =~ s/\s*$Type\s*//g;
1435d2506586SAndy Whitcroft			for my $modifier (split(' ', $possible)) {
14369a974fdbSAndy Whitcroft				if ($modifier !~ $notPermitted) {
1437d2506586SAndy Whitcroft					warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1438d2506586SAndy Whitcroft					push(@modifierList, $modifier);
1439d2506586SAndy Whitcroft				}
14409a974fdbSAndy Whitcroft			}
1441c45dcabdSAndy Whitcroft
1442c45dcabdSAndy Whitcroft		} else {
144313214adfSAndy Whitcroft			warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
14448905a67cSAndy Whitcroft			push(@typeList, $possible);
1445c45dcabdSAndy Whitcroft		}
14468905a67cSAndy Whitcroft		build_types();
14470776e594SAndy Whitcroft	} else {
14480776e594SAndy Whitcroft		warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
14498905a67cSAndy Whitcroft	}
14508905a67cSAndy Whitcroft}
14518905a67cSAndy Whitcroft
14526c72ffaaSAndy Whitcroftmy $prefix = '';
14536c72ffaaSAndy Whitcroft
1454000d1cc1SJoe Perchessub show_type {
1455cbec18afSJoe Perches	my ($type) = @_;
145691bfe484SJoe Perches
1457cbec18afSJoe Perches	return defined $use_type{$type} if (scalar keys %use_type > 0);
1458cbec18afSJoe Perches
1459cbec18afSJoe Perches	return !defined $ignore_type{$type};
1460000d1cc1SJoe Perches}
1461000d1cc1SJoe Perches
1462f0a594c1SAndy Whitcroftsub report {
1463cbec18afSJoe Perches	my ($level, $type, $msg) = @_;
1464cbec18afSJoe Perches
1465cbec18afSJoe Perches	if (!show_type($type) ||
1466cbec18afSJoe Perches	    (defined $tst_only && $msg !~ /\Q$tst_only\E/)) {
1467773647a0SAndy Whitcroft		return 0;
1468773647a0SAndy Whitcroft	}
1469000d1cc1SJoe Perches	my $line;
1470000d1cc1SJoe Perches	if ($show_types) {
1471cbec18afSJoe Perches		$line = "$prefix$level:$type: $msg\n";
1472000d1cc1SJoe Perches	} else {
1473cbec18afSJoe Perches		$line = "$prefix$level: $msg\n";
1474000d1cc1SJoe Perches	}
14758905a67cSAndy Whitcroft	$line = (split('\n', $line))[0] . "\n" if ($terse);
14768905a67cSAndy Whitcroft
147713214adfSAndy Whitcroft	push(our @report, $line);
1478773647a0SAndy Whitcroft
1479773647a0SAndy Whitcroft	return 1;
1480f0a594c1SAndy Whitcroft}
1481cbec18afSJoe Perches
1482f0a594c1SAndy Whitcroftsub report_dump {
148313214adfSAndy Whitcroft	our @report;
1484f0a594c1SAndy Whitcroft}
1485000d1cc1SJoe Perches
1486de7d4f0eSAndy Whitcroftsub ERROR {
1487cbec18afSJoe Perches	my ($type, $msg) = @_;
1488cbec18afSJoe Perches
1489cbec18afSJoe Perches	if (report("ERROR", $type, $msg)) {
1490de7d4f0eSAndy Whitcroft		our $clean = 0;
14916c72ffaaSAndy Whitcroft		our $cnt_error++;
14923705ce5bSJoe Perches		return 1;
1493de7d4f0eSAndy Whitcroft	}
14943705ce5bSJoe Perches	return 0;
1495773647a0SAndy Whitcroft}
1496de7d4f0eSAndy Whitcroftsub WARN {
1497cbec18afSJoe Perches	my ($type, $msg) = @_;
1498cbec18afSJoe Perches
1499cbec18afSJoe Perches	if (report("WARNING", $type, $msg)) {
1500de7d4f0eSAndy Whitcroft		our $clean = 0;
15016c72ffaaSAndy Whitcroft		our $cnt_warn++;
15023705ce5bSJoe Perches		return 1;
1503de7d4f0eSAndy Whitcroft	}
15043705ce5bSJoe Perches	return 0;
1505773647a0SAndy Whitcroft}
1506de7d4f0eSAndy Whitcroftsub CHK {
1507cbec18afSJoe Perches	my ($type, $msg) = @_;
1508cbec18afSJoe Perches
1509cbec18afSJoe Perches	if ($check && report("CHECK", $type, $msg)) {
1510de7d4f0eSAndy Whitcroft		our $clean = 0;
15116c72ffaaSAndy Whitcroft		our $cnt_chk++;
15123705ce5bSJoe Perches		return 1;
15136c72ffaaSAndy Whitcroft	}
15143705ce5bSJoe Perches	return 0;
1515de7d4f0eSAndy Whitcroft}
1516de7d4f0eSAndy Whitcroft
15176ecd9674SAndy Whitcroftsub check_absolute_file {
15186ecd9674SAndy Whitcroft	my ($absolute, $herecurr) = @_;
15196ecd9674SAndy Whitcroft	my $file = $absolute;
15206ecd9674SAndy Whitcroft
15216ecd9674SAndy Whitcroft	##print "absolute<$absolute>\n";
15226ecd9674SAndy Whitcroft
15236ecd9674SAndy Whitcroft	# See if any suffix of this path is a path within the tree.
15246ecd9674SAndy Whitcroft	while ($file =~ s@^[^/]*/@@) {
15256ecd9674SAndy Whitcroft		if (-f "$root/$file") {
15266ecd9674SAndy Whitcroft			##print "file<$file>\n";
15276ecd9674SAndy Whitcroft			last;
15286ecd9674SAndy Whitcroft		}
15296ecd9674SAndy Whitcroft	}
15306ecd9674SAndy Whitcroft	if (! -f _)  {
15316ecd9674SAndy Whitcroft		return 0;
15326ecd9674SAndy Whitcroft	}
15336ecd9674SAndy Whitcroft
15346ecd9674SAndy Whitcroft	# It is, so see if the prefix is acceptable.
15356ecd9674SAndy Whitcroft	my $prefix = $absolute;
15366ecd9674SAndy Whitcroft	substr($prefix, -length($file)) = '';
15376ecd9674SAndy Whitcroft
15386ecd9674SAndy Whitcroft	##print "prefix<$prefix>\n";
15396ecd9674SAndy Whitcroft	if ($prefix ne ".../") {
1540000d1cc1SJoe Perches		WARN("USE_RELATIVE_PATH",
1541000d1cc1SJoe Perches		     "use relative pathname instead of absolute in changelog text\n" . $herecurr);
15426ecd9674SAndy Whitcroft	}
15436ecd9674SAndy Whitcroft}
15446ecd9674SAndy Whitcroft
15453705ce5bSJoe Perchessub trim {
15463705ce5bSJoe Perches	my ($string) = @_;
15473705ce5bSJoe Perches
1548b34c648bSJoe Perches	$string =~ s/^\s+|\s+$//g;
1549b34c648bSJoe Perches
1550b34c648bSJoe Perches	return $string;
1551b34c648bSJoe Perches}
1552b34c648bSJoe Perches
1553b34c648bSJoe Perchessub ltrim {
1554b34c648bSJoe Perches	my ($string) = @_;
1555b34c648bSJoe Perches
1556b34c648bSJoe Perches	$string =~ s/^\s+//;
1557b34c648bSJoe Perches
1558b34c648bSJoe Perches	return $string;
1559b34c648bSJoe Perches}
1560b34c648bSJoe Perches
1561b34c648bSJoe Perchessub rtrim {
1562b34c648bSJoe Perches	my ($string) = @_;
1563b34c648bSJoe Perches
1564b34c648bSJoe Perches	$string =~ s/\s+$//;
15653705ce5bSJoe Perches
15663705ce5bSJoe Perches	return $string;
15673705ce5bSJoe Perches}
15683705ce5bSJoe Perches
156952ea8506SJoe Perchessub string_find_replace {
157052ea8506SJoe Perches	my ($string, $find, $replace) = @_;
157152ea8506SJoe Perches
157252ea8506SJoe Perches	$string =~ s/$find/$replace/g;
157352ea8506SJoe Perches
157452ea8506SJoe Perches	return $string;
157552ea8506SJoe Perches}
157652ea8506SJoe Perches
15773705ce5bSJoe Perchessub tabify {
15783705ce5bSJoe Perches	my ($leading) = @_;
15793705ce5bSJoe Perches
15803705ce5bSJoe Perches	my $source_indent = 8;
15813705ce5bSJoe Perches	my $max_spaces_before_tab = $source_indent - 1;
15823705ce5bSJoe Perches	my $spaces_to_tab = " " x $source_indent;
15833705ce5bSJoe Perches
15843705ce5bSJoe Perches	#convert leading spaces to tabs
15853705ce5bSJoe Perches	1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
15863705ce5bSJoe Perches	#Remove spaces before a tab
15873705ce5bSJoe Perches	1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
15883705ce5bSJoe Perches
15893705ce5bSJoe Perches	return "$leading";
15903705ce5bSJoe Perches}
15913705ce5bSJoe Perches
1592d1fe9c09SJoe Perchessub pos_last_openparen {
1593d1fe9c09SJoe Perches	my ($line) = @_;
1594d1fe9c09SJoe Perches
1595d1fe9c09SJoe Perches	my $pos = 0;
1596d1fe9c09SJoe Perches
1597d1fe9c09SJoe Perches	my $opens = $line =~ tr/\(/\(/;
1598d1fe9c09SJoe Perches	my $closes = $line =~ tr/\)/\)/;
1599d1fe9c09SJoe Perches
1600d1fe9c09SJoe Perches	my $last_openparen = 0;
1601d1fe9c09SJoe Perches
1602d1fe9c09SJoe Perches	if (($opens == 0) || ($closes >= $opens)) {
1603d1fe9c09SJoe Perches		return -1;
1604d1fe9c09SJoe Perches	}
1605d1fe9c09SJoe Perches
1606d1fe9c09SJoe Perches	my $len = length($line);
1607d1fe9c09SJoe Perches
1608d1fe9c09SJoe Perches	for ($pos = 0; $pos < $len; $pos++) {
1609d1fe9c09SJoe Perches		my $string = substr($line, $pos);
1610d1fe9c09SJoe Perches		if ($string =~ /^($FuncArg|$balanced_parens)/) {
1611d1fe9c09SJoe Perches			$pos += length($1) - 1;
1612d1fe9c09SJoe Perches		} elsif (substr($line, $pos, 1) eq '(') {
1613d1fe9c09SJoe Perches			$last_openparen = $pos;
1614d1fe9c09SJoe Perches		} elsif (index($string, '(') == -1) {
1615d1fe9c09SJoe Perches			last;
1616d1fe9c09SJoe Perches		}
1617d1fe9c09SJoe Perches	}
1618d1fe9c09SJoe Perches
161991cb5195SJoe Perches	return length(expand_tabs(substr($line, 0, $last_openparen))) + 1;
1620d1fe9c09SJoe Perches}
1621d1fe9c09SJoe Perches
16220a920b5bSAndy Whitcroftsub process {
16230a920b5bSAndy Whitcroft	my $filename = shift;
16240a920b5bSAndy Whitcroft
16250a920b5bSAndy Whitcroft	my $linenr=0;
16260a920b5bSAndy Whitcroft	my $prevline="";
1627c2fdda0dSAndy Whitcroft	my $prevrawline="";
16280a920b5bSAndy Whitcroft	my $stashline="";
1629c2fdda0dSAndy Whitcroft	my $stashrawline="";
16300a920b5bSAndy Whitcroft
16314a0df2efSAndy Whitcroft	my $length;
16320a920b5bSAndy Whitcroft	my $indent;
16330a920b5bSAndy Whitcroft	my $previndent=0;
16340a920b5bSAndy Whitcroft	my $stashindent=0;
16350a920b5bSAndy Whitcroft
1636de7d4f0eSAndy Whitcroft	our $clean = 1;
16370a920b5bSAndy Whitcroft	my $signoff = 0;
16380a920b5bSAndy Whitcroft	my $is_patch = 0;
16390a920b5bSAndy Whitcroft
164029ee1b0cSJoe Perches	my $in_header_lines = $file ? 0 : 1;
164115662b3eSJoe Perches	my $in_commit_log = 0;		#Scanning lines before patch
164215662b3eSJoe Perches
1643fa64205dSPasi Savanainen	my $non_utf8_charset = 0;
1644fa64205dSPasi Savanainen
1645365dd4eaSJoe Perches	my $last_blank_line = 0;
1646365dd4eaSJoe Perches
164713214adfSAndy Whitcroft	our @report = ();
16486c72ffaaSAndy Whitcroft	our $cnt_lines = 0;
16496c72ffaaSAndy Whitcroft	our $cnt_error = 0;
16506c72ffaaSAndy Whitcroft	our $cnt_warn = 0;
16516c72ffaaSAndy Whitcroft	our $cnt_chk = 0;
16526c72ffaaSAndy Whitcroft
16530a920b5bSAndy Whitcroft	# Trace the real file/line as we go.
16540a920b5bSAndy Whitcroft	my $realfile = '';
16550a920b5bSAndy Whitcroft	my $realline = 0;
16560a920b5bSAndy Whitcroft	my $realcnt = 0;
16570a920b5bSAndy Whitcroft	my $here = '';
16580a920b5bSAndy Whitcroft	my $in_comment = 0;
1659c2fdda0dSAndy Whitcroft	my $comment_edge = 0;
16600a920b5bSAndy Whitcroft	my $first_line = 0;
16611e855726SWolfram Sang	my $p1_prefix = '';
16620a920b5bSAndy Whitcroft
166313214adfSAndy Whitcroft	my $prev_values = 'E';
166413214adfSAndy Whitcroft
166513214adfSAndy Whitcroft	# suppression flags
1666773647a0SAndy Whitcroft	my %suppress_ifbraces;
1667170d3a22SAndy Whitcroft	my %suppress_whiletrailers;
16682b474a1aSAndy Whitcroft	my %suppress_export;
16693e469cdcSAndy Whitcroft	my $suppress_statement = 0;
1670653d4876SAndy Whitcroft
16717e51f197SJoe Perches	my %signatures = ();
1672323c1260SJoe Perches
1673c2fdda0dSAndy Whitcroft	# Pre-scan the patch sanitizing the lines.
1674de7d4f0eSAndy Whitcroft	# Pre-scan the patch looking for any __setup documentation.
1675c2fdda0dSAndy Whitcroft	#
1676de7d4f0eSAndy Whitcroft	my @setup_docs = ();
1677de7d4f0eSAndy Whitcroft	my $setup_docs = 0;
1678773647a0SAndy Whitcroft
1679d8b07710SJoe Perches	my $camelcase_file_seeded = 0;
1680d8b07710SJoe Perches
1681773647a0SAndy Whitcroft	sanitise_line_reset();
1682c2fdda0dSAndy Whitcroft	my $line;
1683c2fdda0dSAndy Whitcroft	foreach my $rawline (@rawlines) {
1684773647a0SAndy Whitcroft		$linenr++;
1685773647a0SAndy Whitcroft		$line = $rawline;
1686c2fdda0dSAndy Whitcroft
16873705ce5bSJoe Perches		push(@fixed, $rawline) if ($fix);
16883705ce5bSJoe Perches
1689773647a0SAndy Whitcroft		if ($rawline=~/^\+\+\+\s+(\S+)/) {
1690de7d4f0eSAndy Whitcroft			$setup_docs = 0;
1691de7d4f0eSAndy Whitcroft			if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
1692de7d4f0eSAndy Whitcroft				$setup_docs = 1;
1693de7d4f0eSAndy Whitcroft			}
1694773647a0SAndy Whitcroft			#next;
1695de7d4f0eSAndy Whitcroft		}
1696773647a0SAndy Whitcroft		if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1697773647a0SAndy Whitcroft			$realline=$1-1;
1698773647a0SAndy Whitcroft			if (defined $2) {
1699773647a0SAndy Whitcroft				$realcnt=$3+1;
1700773647a0SAndy Whitcroft			} else {
1701773647a0SAndy Whitcroft				$realcnt=1+1;
1702773647a0SAndy Whitcroft			}
1703c45dcabdSAndy Whitcroft			$in_comment = 0;
1704773647a0SAndy Whitcroft
1705773647a0SAndy Whitcroft			# Guestimate if this is a continuing comment.  Run
1706773647a0SAndy Whitcroft			# the context looking for a comment "edge".  If this
1707773647a0SAndy Whitcroft			# edge is a close comment then we must be in a comment
1708773647a0SAndy Whitcroft			# at context start.
1709773647a0SAndy Whitcroft			my $edge;
171001fa9147SAndy Whitcroft			my $cnt = $realcnt;
171101fa9147SAndy Whitcroft			for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
171201fa9147SAndy Whitcroft				next if (defined $rawlines[$ln - 1] &&
171301fa9147SAndy Whitcroft					 $rawlines[$ln - 1] =~ /^-/);
171401fa9147SAndy Whitcroft				$cnt--;
171501fa9147SAndy Whitcroft				#print "RAW<$rawlines[$ln - 1]>\n";
1716721c1cb6SAndy Whitcroft				last if (!defined $rawlines[$ln - 1]);
1717fae17daeSAndy Whitcroft				if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
1718fae17daeSAndy Whitcroft				    $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
1719fae17daeSAndy Whitcroft					($edge) = $1;
1720fae17daeSAndy Whitcroft					last;
1721fae17daeSAndy Whitcroft				}
1722773647a0SAndy Whitcroft			}
1723773647a0SAndy Whitcroft			if (defined $edge && $edge eq '*/') {
1724773647a0SAndy Whitcroft				$in_comment = 1;
1725773647a0SAndy Whitcroft			}
1726773647a0SAndy Whitcroft
1727773647a0SAndy Whitcroft			# Guestimate if this is a continuing comment.  If this
1728773647a0SAndy Whitcroft			# is the start of a diff block and this line starts
1729773647a0SAndy Whitcroft			# ' *' then it is very likely a comment.
1730773647a0SAndy Whitcroft			if (!defined $edge &&
173183242e0cSAndy Whitcroft			    $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
1732773647a0SAndy Whitcroft			{
1733773647a0SAndy Whitcroft				$in_comment = 1;
1734773647a0SAndy Whitcroft			}
1735773647a0SAndy Whitcroft
1736773647a0SAndy Whitcroft			##print "COMMENT:$in_comment edge<$edge> $rawline\n";
1737773647a0SAndy Whitcroft			sanitise_line_reset($in_comment);
1738773647a0SAndy Whitcroft
1739171ae1a4SAndy Whitcroft		} elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
1740773647a0SAndy Whitcroft			# Standardise the strings and chars within the input to
1741171ae1a4SAndy Whitcroft			# simplify matching -- only bother with positive lines.
1742773647a0SAndy Whitcroft			$line = sanitise_line($rawline);
1743773647a0SAndy Whitcroft		}
1744773647a0SAndy Whitcroft		push(@lines, $line);
1745773647a0SAndy Whitcroft
1746773647a0SAndy Whitcroft		if ($realcnt > 1) {
1747773647a0SAndy Whitcroft			$realcnt-- if ($line =~ /^(?:\+| |$)/);
1748773647a0SAndy Whitcroft		} else {
1749773647a0SAndy Whitcroft			$realcnt = 0;
1750773647a0SAndy Whitcroft		}
1751773647a0SAndy Whitcroft
1752773647a0SAndy Whitcroft		#print "==>$rawline\n";
1753773647a0SAndy Whitcroft		#print "-->$line\n";
1754de7d4f0eSAndy Whitcroft
1755de7d4f0eSAndy Whitcroft		if ($setup_docs && $line =~ /^\+/) {
1756de7d4f0eSAndy Whitcroft			push(@setup_docs, $line);
1757de7d4f0eSAndy Whitcroft		}
1758de7d4f0eSAndy Whitcroft	}
1759de7d4f0eSAndy Whitcroft
17606c72ffaaSAndy Whitcroft	$prefix = '';
17616c72ffaaSAndy Whitcroft
1762773647a0SAndy Whitcroft	$realcnt = 0;
1763773647a0SAndy Whitcroft	$linenr = 0;
17640a920b5bSAndy Whitcroft	foreach my $line (@lines) {
17650a920b5bSAndy Whitcroft		$linenr++;
17661b5539b1SJoe Perches		my $sline = $line;	#copy of $line
17671b5539b1SJoe Perches		$sline =~ s/$;/ /g;	#with comments as spaces
17680a920b5bSAndy Whitcroft
1769c2fdda0dSAndy Whitcroft		my $rawline = $rawlines[$linenr - 1];
17706c72ffaaSAndy Whitcroft
17710a920b5bSAndy Whitcroft#extract the line range in the file after the patch is applied
17726c72ffaaSAndy Whitcroft		if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
17730a920b5bSAndy Whitcroft			$is_patch = 1;
17744a0df2efSAndy Whitcroft			$first_line = $linenr + 1;
17750a920b5bSAndy Whitcroft			$realline=$1-1;
17760a920b5bSAndy Whitcroft			if (defined $2) {
17770a920b5bSAndy Whitcroft				$realcnt=$3+1;
17780a920b5bSAndy Whitcroft			} else {
17790a920b5bSAndy Whitcroft				$realcnt=1+1;
17800a920b5bSAndy Whitcroft			}
1781c2fdda0dSAndy Whitcroft			annotate_reset();
178213214adfSAndy Whitcroft			$prev_values = 'E';
178313214adfSAndy Whitcroft
1784773647a0SAndy Whitcroft			%suppress_ifbraces = ();
1785170d3a22SAndy Whitcroft			%suppress_whiletrailers = ();
17862b474a1aSAndy Whitcroft			%suppress_export = ();
17873e469cdcSAndy Whitcroft			$suppress_statement = 0;
17880a920b5bSAndy Whitcroft			next;
17890a920b5bSAndy Whitcroft
17904a0df2efSAndy Whitcroft# track the line number as we move through the hunk, note that
17914a0df2efSAndy Whitcroft# new versions of GNU diff omit the leading space on completely
17924a0df2efSAndy Whitcroft# blank context lines so we need to count that too.
1793773647a0SAndy Whitcroft		} elsif ($line =~ /^( |\+|$)/) {
17940a920b5bSAndy Whitcroft			$realline++;
1795d8aaf121SAndy Whitcroft			$realcnt-- if ($realcnt != 0);
17960a920b5bSAndy Whitcroft
17974a0df2efSAndy Whitcroft			# Measure the line length and indent.
1798c2fdda0dSAndy Whitcroft			($length, $indent) = line_stats($rawline);
17990a920b5bSAndy Whitcroft
18000a920b5bSAndy Whitcroft			# Track the previous line.
18010a920b5bSAndy Whitcroft			($prevline, $stashline) = ($stashline, $line);
18020a920b5bSAndy Whitcroft			($previndent, $stashindent) = ($stashindent, $indent);
1803c2fdda0dSAndy Whitcroft			($prevrawline, $stashrawline) = ($stashrawline, $rawline);
1804c2fdda0dSAndy Whitcroft
1805773647a0SAndy Whitcroft			#warn "line<$line>\n";
18066c72ffaaSAndy Whitcroft
1807d8aaf121SAndy Whitcroft		} elsif ($realcnt == 1) {
1808d8aaf121SAndy Whitcroft			$realcnt--;
18090a920b5bSAndy Whitcroft		}
18100a920b5bSAndy Whitcroft
1811cc77cdcaSAndy Whitcroft		my $hunk_line = ($realcnt != 0);
1812cc77cdcaSAndy Whitcroft
18130a920b5bSAndy Whitcroft#make up the handle for any error we report on this line
1814773647a0SAndy Whitcroft		$prefix = "$filename:$realline: " if ($emacs && $file);
1815773647a0SAndy Whitcroft		$prefix = "$filename:$linenr: " if ($emacs && !$file);
1816773647a0SAndy Whitcroft
18176c72ffaaSAndy Whitcroft		$here = "#$linenr: " if (!$file);
18186c72ffaaSAndy Whitcroft		$here = "#$realline: " if ($file);
1819773647a0SAndy Whitcroft
18202ac73b4fSJoe Perches		my $found_file = 0;
1821773647a0SAndy Whitcroft		# extract the filename as it passes
18223bf9a009SRabin Vincent		if ($line =~ /^diff --git.*?(\S+)$/) {
18233bf9a009SRabin Vincent			$realfile = $1;
18242b7ab453SJoe Perches			$realfile =~ s@^([^/]*)/@@ if (!$file);
1825270c49a0SJoe Perches			$in_commit_log = 0;
18262ac73b4fSJoe Perches			$found_file = 1;
18273bf9a009SRabin Vincent		} elsif ($line =~ /^\+\+\+\s+(\S+)/) {
1828773647a0SAndy Whitcroft			$realfile = $1;
18292b7ab453SJoe Perches			$realfile =~ s@^([^/]*)/@@ if (!$file);
1830270c49a0SJoe Perches			$in_commit_log = 0;
18311e855726SWolfram Sang
18321e855726SWolfram Sang			$p1_prefix = $1;
1833e2f7aa4bSAndy Whitcroft			if (!$file && $tree && $p1_prefix ne '' &&
1834e2f7aa4bSAndy Whitcroft			    -e "$root/$p1_prefix") {
1835000d1cc1SJoe Perches				WARN("PATCH_PREFIX",
1836000d1cc1SJoe Perches				     "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
18371e855726SWolfram Sang			}
1838773647a0SAndy Whitcroft
1839c1ab3326SAndy Whitcroft			if ($realfile =~ m@^include/asm/@) {
1840000d1cc1SJoe Perches				ERROR("MODIFIED_INCLUDE_ASM",
1841000d1cc1SJoe Perches				      "do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
1842773647a0SAndy Whitcroft			}
18432ac73b4fSJoe Perches			$found_file = 1;
18442ac73b4fSJoe Perches		}
18452ac73b4fSJoe Perches
18462ac73b4fSJoe Perches		if ($found_file) {
18472ac73b4fSJoe Perches			if ($realfile =~ m@^(drivers/net/|net/)@) {
18482ac73b4fSJoe Perches				$check = 1;
18492ac73b4fSJoe Perches			} else {
18502ac73b4fSJoe Perches				$check = $check_orig;
18512ac73b4fSJoe Perches			}
1852773647a0SAndy Whitcroft			next;
1853773647a0SAndy Whitcroft		}
1854773647a0SAndy Whitcroft
1855389834b6SRandy Dunlap		$here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
18560a920b5bSAndy Whitcroft
1857c2fdda0dSAndy Whitcroft		my $hereline = "$here\n$rawline\n";
1858c2fdda0dSAndy Whitcroft		my $herecurr = "$here\n$rawline\n";
1859c2fdda0dSAndy Whitcroft		my $hereprev = "$here\n$prevrawline\n$rawline\n";
18600a920b5bSAndy Whitcroft
18616c72ffaaSAndy Whitcroft		$cnt_lines++ if ($realcnt != 0);
18626c72ffaaSAndy Whitcroft
18633bf9a009SRabin Vincent# Check for incorrect file permissions
18643bf9a009SRabin Vincent		if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
18653bf9a009SRabin Vincent			my $permhere = $here . "FILE: $realfile\n";
186604db4d25SJoe Perches			if ($realfile !~ m@scripts/@ &&
186704db4d25SJoe Perches			    $realfile !~ /\.(py|pl|awk|sh)$/) {
1868000d1cc1SJoe Perches				ERROR("EXECUTE_PERMISSIONS",
1869000d1cc1SJoe Perches				      "do not set execute permissions for source files\n" . $permhere);
18703bf9a009SRabin Vincent			}
18713bf9a009SRabin Vincent		}
18723bf9a009SRabin Vincent
187320112475SJoe Perches# Check the patch for a signoff:
1874d8aaf121SAndy Whitcroft		if ($line =~ /^\s*signed-off-by:/i) {
18754a0df2efSAndy Whitcroft			$signoff++;
187615662b3eSJoe Perches			$in_commit_log = 0;
18770a920b5bSAndy Whitcroft		}
187820112475SJoe Perches
187920112475SJoe Perches# Check signature styles
1880270c49a0SJoe Perches		if (!$in_header_lines &&
1881ce0338dfSJoe Perches		    $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
188220112475SJoe Perches			my $space_before = $1;
188320112475SJoe Perches			my $sign_off = $2;
188420112475SJoe Perches			my $space_after = $3;
188520112475SJoe Perches			my $email = $4;
188620112475SJoe Perches			my $ucfirst_sign_off = ucfirst(lc($sign_off));
188720112475SJoe Perches
1888ce0338dfSJoe Perches			if ($sign_off !~ /$signature_tags/) {
1889ce0338dfSJoe Perches				WARN("BAD_SIGN_OFF",
1890ce0338dfSJoe Perches				     "Non-standard signature: $sign_off\n" . $herecurr);
1891ce0338dfSJoe Perches			}
189220112475SJoe Perches			if (defined $space_before && $space_before ne "") {
18933705ce5bSJoe Perches				if (WARN("BAD_SIGN_OFF",
18943705ce5bSJoe Perches					 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
18953705ce5bSJoe Perches				    $fix) {
18963705ce5bSJoe Perches					$fixed[$linenr - 1] =
18973705ce5bSJoe Perches					    "$ucfirst_sign_off $email";
18983705ce5bSJoe Perches				}
189920112475SJoe Perches			}
190020112475SJoe Perches			if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
19013705ce5bSJoe Perches				if (WARN("BAD_SIGN_OFF",
19023705ce5bSJoe Perches					 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
19033705ce5bSJoe Perches				    $fix) {
19043705ce5bSJoe Perches					$fixed[$linenr - 1] =
19053705ce5bSJoe Perches					    "$ucfirst_sign_off $email";
19063705ce5bSJoe Perches				}
19073705ce5bSJoe Perches
190820112475SJoe Perches			}
190920112475SJoe Perches			if (!defined $space_after || $space_after ne " ") {
19103705ce5bSJoe Perches				if (WARN("BAD_SIGN_OFF",
19113705ce5bSJoe Perches					 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
19123705ce5bSJoe Perches				    $fix) {
19133705ce5bSJoe Perches					$fixed[$linenr - 1] =
19143705ce5bSJoe Perches					    "$ucfirst_sign_off $email";
19153705ce5bSJoe Perches				}
191620112475SJoe Perches			}
191720112475SJoe Perches
191820112475SJoe Perches			my ($email_name, $email_address, $comment) = parse_email($email);
191920112475SJoe Perches			my $suggested_email = format_email(($email_name, $email_address));
192020112475SJoe Perches			if ($suggested_email eq "") {
1921000d1cc1SJoe Perches				ERROR("BAD_SIGN_OFF",
1922000d1cc1SJoe Perches				      "Unrecognized email address: '$email'\n" . $herecurr);
192320112475SJoe Perches			} else {
192420112475SJoe Perches				my $dequoted = $suggested_email;
192520112475SJoe Perches				$dequoted =~ s/^"//;
192620112475SJoe Perches				$dequoted =~ s/" </ </;
192720112475SJoe Perches				# Don't force email to have quotes
192820112475SJoe Perches				# Allow just an angle bracketed address
192920112475SJoe Perches				if ("$dequoted$comment" ne $email &&
193020112475SJoe Perches				    "<$email_address>$comment" ne $email &&
193120112475SJoe Perches				    "$suggested_email$comment" ne $email) {
1932000d1cc1SJoe Perches					WARN("BAD_SIGN_OFF",
1933000d1cc1SJoe Perches					     "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
193420112475SJoe Perches				}
19350a920b5bSAndy Whitcroft			}
19367e51f197SJoe Perches
19377e51f197SJoe Perches# Check for duplicate signatures
19387e51f197SJoe Perches			my $sig_nospace = $line;
19397e51f197SJoe Perches			$sig_nospace =~ s/\s//g;
19407e51f197SJoe Perches			$sig_nospace = lc($sig_nospace);
19417e51f197SJoe Perches			if (defined $signatures{$sig_nospace}) {
19427e51f197SJoe Perches				WARN("BAD_SIGN_OFF",
19437e51f197SJoe Perches				     "Duplicate signature\n" . $herecurr);
19447e51f197SJoe Perches			} else {
19457e51f197SJoe Perches				$signatures{$sig_nospace} = 1;
19467e51f197SJoe Perches			}
19470a920b5bSAndy Whitcroft		}
19480a920b5bSAndy Whitcroft
19499b3189ebSJoe Perches# Check for old stable address
19509b3189ebSJoe Perches		if ($line =~ /^\s*cc:\s*.*<?\bstable\@kernel\.org\b>?.*$/i) {
19519b3189ebSJoe Perches			ERROR("STABLE_ADDRESS",
19529b3189ebSJoe Perches			      "The 'stable' address should be 'stable\@vger.kernel.org'\n" . $herecurr);
19539b3189ebSJoe Perches		}
19549b3189ebSJoe Perches
19557ebd05efSChristopher Covington# Check for unwanted Gerrit info
19567ebd05efSChristopher Covington		if ($in_commit_log && $line =~ /^\s*change-id:/i) {
19577ebd05efSChristopher Covington			ERROR("GERRIT_CHANGE_ID",
19587ebd05efSChristopher Covington			      "Remove Gerrit Change-Id's before submitting upstream.\n" . $herecurr);
19597ebd05efSChristopher Covington		}
19607ebd05efSChristopher Covington
196100df344fSAndy Whitcroft# Check for wrappage within a valid hunk of the file
19628905a67cSAndy Whitcroft		if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
1963000d1cc1SJoe Perches			ERROR("CORRUPTED_PATCH",
1964000d1cc1SJoe Perches			      "patch seems to be corrupt (line wrapped?)\n" .
19656c72ffaaSAndy Whitcroft				$herecurr) if (!$emitted_corrupt++);
1966de7d4f0eSAndy Whitcroft		}
1967de7d4f0eSAndy Whitcroft
19686ecd9674SAndy Whitcroft# Check for absolute kernel paths.
19696ecd9674SAndy Whitcroft		if ($tree) {
19706ecd9674SAndy Whitcroft			while ($line =~ m{(?:^|\s)(/\S*)}g) {
19716ecd9674SAndy Whitcroft				my $file = $1;
19726ecd9674SAndy Whitcroft
19736ecd9674SAndy Whitcroft				if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
19746ecd9674SAndy Whitcroft				    check_absolute_file($1, $herecurr)) {
19756ecd9674SAndy Whitcroft					#
19766ecd9674SAndy Whitcroft				} else {
19776ecd9674SAndy Whitcroft					check_absolute_file($file, $herecurr);
19786ecd9674SAndy Whitcroft				}
19796ecd9674SAndy Whitcroft			}
19806ecd9674SAndy Whitcroft		}
19816ecd9674SAndy Whitcroft
1982de7d4f0eSAndy Whitcroft# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
1983de7d4f0eSAndy Whitcroft		if (($realfile =~ /^$/ || $line =~ /^\+/) &&
1984171ae1a4SAndy Whitcroft		    $rawline !~ m/^$UTF8*$/) {
1985171ae1a4SAndy Whitcroft			my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
1986171ae1a4SAndy Whitcroft
1987171ae1a4SAndy Whitcroft			my $blank = copy_spacing($rawline);
1988171ae1a4SAndy Whitcroft			my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
1989171ae1a4SAndy Whitcroft			my $hereptr = "$hereline$ptr\n";
1990171ae1a4SAndy Whitcroft
199134d99219SJoe Perches			CHK("INVALID_UTF8",
1992000d1cc1SJoe Perches			    "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
199300df344fSAndy Whitcroft		}
19940a920b5bSAndy Whitcroft
199515662b3eSJoe Perches# Check if it's the start of a commit log
199615662b3eSJoe Perches# (not a header line and we haven't seen the patch filename)
199715662b3eSJoe Perches		if ($in_header_lines && $realfile =~ /^$/ &&
199829ee1b0cSJoe Perches		    !($rawline =~ /^\s+\S/ ||
199929ee1b0cSJoe Perches		      $rawline =~ /^(commit\b|from\b|[\w-]+:).*$/i)) {
200015662b3eSJoe Perches			$in_header_lines = 0;
200115662b3eSJoe Perches			$in_commit_log = 1;
200215662b3eSJoe Perches		}
200315662b3eSJoe Perches
2004fa64205dSPasi Savanainen# Check if there is UTF-8 in a commit log when a mail header has explicitly
2005fa64205dSPasi Savanainen# declined it, i.e defined some charset where it is missing.
2006fa64205dSPasi Savanainen		if ($in_header_lines &&
2007fa64205dSPasi Savanainen		    $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
2008fa64205dSPasi Savanainen		    $1 !~ /utf-8/i) {
2009fa64205dSPasi Savanainen			$non_utf8_charset = 1;
2010fa64205dSPasi Savanainen		}
2011fa64205dSPasi Savanainen
2012fa64205dSPasi Savanainen		if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
201315662b3eSJoe Perches		    $rawline =~ /$NON_ASCII_UTF8/) {
2014fa64205dSPasi Savanainen			WARN("UTF8_BEFORE_PATCH",
201515662b3eSJoe Perches			    "8-bit UTF-8 used in possible commit log\n" . $herecurr);
201615662b3eSJoe Perches		}
201715662b3eSJoe Perches
201830670854SAndy Whitcroft# ignore non-hunk lines and lines being removed
201930670854SAndy Whitcroft		next if (!$hunk_line || $line =~ /^-/);
202000df344fSAndy Whitcroft
20210a920b5bSAndy Whitcroft#trailing whitespace
20229c0ca6f9SAndy Whitcroft		if ($line =~ /^\+.*\015/) {
2023c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2024d5e616fcSJoe Perches			if (ERROR("DOS_LINE_ENDINGS",
2025d5e616fcSJoe Perches				  "DOS line endings\n" . $herevet) &&
2026d5e616fcSJoe Perches			    $fix) {
2027d5e616fcSJoe Perches				$fixed[$linenr - 1] =~ s/[\s\015]+$//;
2028d5e616fcSJoe Perches			}
2029c2fdda0dSAndy Whitcroft		} elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
2030c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
20313705ce5bSJoe Perches			if (ERROR("TRAILING_WHITESPACE",
20323705ce5bSJoe Perches				  "trailing whitespace\n" . $herevet) &&
20333705ce5bSJoe Perches			    $fix) {
2034d5e616fcSJoe Perches				$fixed[$linenr - 1] =~ s/\s+$//;
20353705ce5bSJoe Perches			}
20363705ce5bSJoe Perches
2037d2c0a235SAndy Whitcroft			$rpt_cleaners = 1;
20380a920b5bSAndy Whitcroft		}
20395368df20SAndy Whitcroft
20404783f894SJosh Triplett# Check for FSF mailing addresses.
2041109d8cb2SAlexander Duyck		if ($rawline =~ /\bwrite to the Free/i ||
20423e2232f2SJoe Perches		    $rawline =~ /\b59\s+Temple\s+Pl/i ||
20433e2232f2SJoe Perches		    $rawline =~ /\b51\s+Franklin\s+St/i) {
20444783f894SJosh Triplett			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
20454783f894SJosh Triplett			my $msg_type = \&ERROR;
20464783f894SJosh Triplett			$msg_type = \&CHK if ($file);
20474783f894SJosh Triplett			&{$msg_type}("FSF_MAILING_ADDRESS",
20484783f894SJosh 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)
20494783f894SJosh Triplett		}
20504783f894SJosh Triplett
20513354957aSAndi Kleen# check for Kconfig help text having a real description
20529fe287d7SAndy Whitcroft# Only applies when adding the entry originally, after that we do not have
20539fe287d7SAndy Whitcroft# sufficient context to determine whether it is indeed long enough.
20543354957aSAndi Kleen		if ($realfile =~ /Kconfig/ &&
2055*8d73e0e7SJoe Perches		    $line =~ /^\+\s*config\s+/) {
20563354957aSAndi Kleen			my $length = 0;
20579fe287d7SAndy Whitcroft			my $cnt = $realcnt;
20589fe287d7SAndy Whitcroft			my $ln = $linenr + 1;
20599fe287d7SAndy Whitcroft			my $f;
2060a1385803SAndy Whitcroft			my $is_start = 0;
20619fe287d7SAndy Whitcroft			my $is_end = 0;
2062a1385803SAndy Whitcroft			for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
20639fe287d7SAndy Whitcroft				$f = $lines[$ln - 1];
20649fe287d7SAndy Whitcroft				$cnt-- if ($lines[$ln - 1] !~ /^-/);
20659fe287d7SAndy Whitcroft				$is_end = $lines[$ln - 1] =~ /^\+/;
20669fe287d7SAndy Whitcroft
20679fe287d7SAndy Whitcroft				next if ($f =~ /^-/);
2068*8d73e0e7SJoe Perches				last if (!$file && $f =~ /^\@\@/);
2069a1385803SAndy Whitcroft
2070*8d73e0e7SJoe Perches				if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate)\s*\"/) {
2071a1385803SAndy Whitcroft					$is_start = 1;
2072*8d73e0e7SJoe Perches				} elsif ($lines[$ln - 1] =~ /^\+\s*(?:---)?help(?:---)?$/) {
2073a1385803SAndy Whitcroft					$length = -1;
2074a1385803SAndy Whitcroft				}
2075a1385803SAndy Whitcroft
20769fe287d7SAndy Whitcroft				$f =~ s/^.//;
20773354957aSAndi Kleen				$f =~ s/#.*//;
20783354957aSAndi Kleen				$f =~ s/^\s+//;
20793354957aSAndi Kleen				next if ($f =~ /^$/);
20809fe287d7SAndy Whitcroft				if ($f =~ /^\s*config\s/) {
20819fe287d7SAndy Whitcroft					$is_end = 1;
20829fe287d7SAndy Whitcroft					last;
20839fe287d7SAndy Whitcroft				}
20843354957aSAndi Kleen				$length++;
20853354957aSAndi Kleen			}
2086000d1cc1SJoe Perches			WARN("CONFIG_DESCRIPTION",
2087a1385803SAndy Whitcroft			     "please write a paragraph that describes the config symbol fully\n" . $herecurr) if ($is_start && $is_end && $length < 4);
2088a1385803SAndy Whitcroft			#print "is_start<$is_start> is_end<$is_end> length<$length>\n";
20893354957aSAndi Kleen		}
20903354957aSAndi Kleen
20911ba8dfd1SKees Cook# discourage the addition of CONFIG_EXPERIMENTAL in Kconfig.
20921ba8dfd1SKees Cook		if ($realfile =~ /Kconfig/ &&
20931ba8dfd1SKees Cook		    $line =~ /.\s*depends on\s+.*\bEXPERIMENTAL\b/) {
20941ba8dfd1SKees Cook			WARN("CONFIG_EXPERIMENTAL",
20951ba8dfd1SKees Cook			     "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
20961ba8dfd1SKees Cook		}
20971ba8dfd1SKees Cook
2098c68e5878SArnaud Lacombe		if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
2099c68e5878SArnaud Lacombe		    ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
2100c68e5878SArnaud Lacombe			my $flag = $1;
2101c68e5878SArnaud Lacombe			my $replacement = {
2102c68e5878SArnaud Lacombe				'EXTRA_AFLAGS' =>   'asflags-y',
2103c68e5878SArnaud Lacombe				'EXTRA_CFLAGS' =>   'ccflags-y',
2104c68e5878SArnaud Lacombe				'EXTRA_CPPFLAGS' => 'cppflags-y',
2105c68e5878SArnaud Lacombe				'EXTRA_LDFLAGS' =>  'ldflags-y',
2106c68e5878SArnaud Lacombe			};
2107c68e5878SArnaud Lacombe
2108c68e5878SArnaud Lacombe			WARN("DEPRECATED_VARIABLE",
2109c68e5878SArnaud Lacombe			     "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
2110c68e5878SArnaud Lacombe		}
2111c68e5878SArnaud Lacombe
2112bff5da43SRob Herring# check for DT compatible documentation
21137dd05b38SFlorian Vaussard		if (defined $root &&
21147dd05b38SFlorian Vaussard			(($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) ||
21157dd05b38SFlorian Vaussard			 ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) {
21167dd05b38SFlorian Vaussard
2117bff5da43SRob Herring			my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g;
2118bff5da43SRob Herring
2119cc93319bSFlorian Vaussard			my $dt_path = $root . "/Documentation/devicetree/bindings/";
2120cc93319bSFlorian Vaussard			my $vp_file = $dt_path . "vendor-prefixes.txt";
2121cc93319bSFlorian Vaussard
2122bff5da43SRob Herring			foreach my $compat (@compats) {
2123bff5da43SRob Herring				my $compat2 = $compat;
2124185d566bSRob Herring				$compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/;
2125185d566bSRob Herring				my $compat3 = $compat;
2126185d566bSRob Herring				$compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/;
2127185d566bSRob Herring				`grep -Erq "$compat|$compat2|$compat3" $dt_path`;
2128bff5da43SRob Herring				if ( $? >> 8 ) {
2129bff5da43SRob Herring					WARN("UNDOCUMENTED_DT_STRING",
2130bff5da43SRob Herring					     "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr);
2131bff5da43SRob Herring				}
2132bff5da43SRob Herring
21334fbf32a6SFlorian Vaussard				next if $compat !~ /^([a-zA-Z0-9\-]+)\,/;
21344fbf32a6SFlorian Vaussard				my $vendor = $1;
2135cc93319bSFlorian Vaussard				`grep -Eq "^$vendor\\b" $vp_file`;
2136bff5da43SRob Herring				if ( $? >> 8 ) {
2137bff5da43SRob Herring					WARN("UNDOCUMENTED_DT_STRING",
2138cc93319bSFlorian Vaussard					     "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr);
2139bff5da43SRob Herring				}
2140bff5da43SRob Herring			}
2141bff5da43SRob Herring		}
2142bff5da43SRob Herring
21435368df20SAndy Whitcroft# check we are in a valid source file if not then ignore this hunk
21445368df20SAndy Whitcroft		next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
21455368df20SAndy Whitcroft
21466cd7f386SJoe Perches#line length limit
2147c45dcabdSAndy Whitcroft		if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ &&
2148f4c014c0SAndy Whitcroft		    $rawline !~ /^.\s*\*\s*\@$Ident\s/ &&
21490fccc622SJoe Perches		    !($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(KERN_\S+\s*|[^"]*))?"[X\t]*"\s*(?:|,|\)\s*;)\s*$/ ||
21508bbea968SJoe Perches		    $line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) &&
21516cd7f386SJoe Perches		    $length > $max_line_length)
2152c45dcabdSAndy Whitcroft		{
2153000d1cc1SJoe Perches			WARN("LONG_LINE",
21546cd7f386SJoe Perches			     "line over $max_line_length characters\n" . $herecurr);
21550a920b5bSAndy Whitcroft		}
21560a920b5bSAndy Whitcroft
2157ca56dc09SJosh Triplett# Check for user-visible strings broken across lines, which breaks the ability
21588c5fcd24SJoe Perches# to grep for the string.  Make exceptions when the previous string ends in a
21598c5fcd24SJoe Perches# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{'
21608c5fcd24SJoe Perches# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value
2161ca56dc09SJosh Triplett		if ($line =~ /^\+\s*"/ &&
2162ca56dc09SJosh Triplett		    $prevline =~ /"\s*$/ &&
21638c5fcd24SJoe Perches		    $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) {
2164ca56dc09SJosh Triplett			WARN("SPLIT_STRING",
2165ca56dc09SJosh Triplett			     "quoted string split across lines\n" . $hereprev);
2166ca56dc09SJosh Triplett		}
2167ca56dc09SJosh Triplett
21685e79d96eSJoe Perches# check for spaces before a quoted newline
21695e79d96eSJoe Perches		if ($rawline =~ /^.*\".*\s\\n/) {
21703705ce5bSJoe Perches			if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
21713705ce5bSJoe Perches				 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
21723705ce5bSJoe Perches			    $fix) {
21733705ce5bSJoe Perches				$fixed[$linenr - 1] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
21743705ce5bSJoe Perches			}
21753705ce5bSJoe Perches
21765e79d96eSJoe Perches		}
21775e79d96eSJoe Perches
21788905a67cSAndy Whitcroft# check for adding lines without a newline.
21798905a67cSAndy Whitcroft		if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
2180000d1cc1SJoe Perches			WARN("MISSING_EOF_NEWLINE",
2181000d1cc1SJoe Perches			     "adding a line without newline at end of file\n" . $herecurr);
21828905a67cSAndy Whitcroft		}
21838905a67cSAndy Whitcroft
218442e41c54SMike Frysinger# Blackfin: use hi/lo macros
218542e41c54SMike Frysinger		if ($realfile =~ m@arch/blackfin/.*\.S$@) {
218642e41c54SMike Frysinger			if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
218742e41c54SMike Frysinger				my $herevet = "$here\n" . cat_vet($line) . "\n";
2188000d1cc1SJoe Perches				ERROR("LO_MACRO",
2189000d1cc1SJoe Perches				      "use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
219042e41c54SMike Frysinger			}
219142e41c54SMike Frysinger			if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
219242e41c54SMike Frysinger				my $herevet = "$here\n" . cat_vet($line) . "\n";
2193000d1cc1SJoe Perches				ERROR("HI_MACRO",
2194000d1cc1SJoe Perches				      "use the HI() macro, not (... >> 16)\n" . $herevet);
219542e41c54SMike Frysinger			}
219642e41c54SMike Frysinger		}
219742e41c54SMike Frysinger
2198b9ea10d6SAndy Whitcroft# check we are in a valid source file C or perl if not then ignore this hunk
2199b9ea10d6SAndy Whitcroft		next if ($realfile !~ /\.(h|c|pl)$/);
22000a920b5bSAndy Whitcroft
22010a920b5bSAndy Whitcroft# at the beginning of a line any tabs must come first and anything
22020a920b5bSAndy Whitcroft# more than 8 must use tabs.
2203c2fdda0dSAndy Whitcroft		if ($rawline =~ /^\+\s* \t\s*\S/ ||
2204c2fdda0dSAndy Whitcroft		    $rawline =~ /^\+\s*        \s*/) {
2205c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2206d2c0a235SAndy Whitcroft			$rpt_cleaners = 1;
22073705ce5bSJoe Perches			if (ERROR("CODE_INDENT",
22083705ce5bSJoe Perches				  "code indent should use tabs where possible\n" . $herevet) &&
22093705ce5bSJoe Perches			    $fix) {
22103705ce5bSJoe Perches				$fixed[$linenr - 1] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
22113705ce5bSJoe Perches			}
22120a920b5bSAndy Whitcroft		}
22130a920b5bSAndy Whitcroft
221408e44365SAlberto Panizzo# check for space before tabs.
221508e44365SAlberto Panizzo		if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
221608e44365SAlberto Panizzo			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
22173705ce5bSJoe Perches			if (WARN("SPACE_BEFORE_TAB",
22183705ce5bSJoe Perches				"please, no space before tabs\n" . $herevet) &&
22193705ce5bSJoe Perches			    $fix) {
2220c76f4cb3SJoe Perches				while ($fixed[$linenr - 1] =~
2221c76f4cb3SJoe Perches					   s/(^\+.*) {8,8}+\t/$1\t\t/) {}
2222c76f4cb3SJoe Perches				while ($fixed[$linenr - 1] =~
2223c76f4cb3SJoe Perches					   s/(^\+.*) +\t/$1\t/) {}
22243705ce5bSJoe Perches			}
222508e44365SAlberto Panizzo		}
222608e44365SAlberto Panizzo
2227d1fe9c09SJoe Perches# check for && or || at the start of a line
2228d1fe9c09SJoe Perches		if ($rawline =~ /^\+\s*(&&|\|\|)/) {
2229d1fe9c09SJoe Perches			CHK("LOGICAL_CONTINUATIONS",
2230d1fe9c09SJoe Perches			    "Logical continuations should be on the previous line\n" . $hereprev);
2231d1fe9c09SJoe Perches		}
2232d1fe9c09SJoe Perches
2233d1fe9c09SJoe Perches# check multi-line statement indentation matches previous line
2234d1fe9c09SJoe Perches		if ($^V && $^V ge 5.10.0 &&
223591cb5195SJoe Perches		    $prevline =~ /^\+([ \t]*)((?:$c90_Keywords(?:\s+if)\s*)|(?:$Declare\s*)?(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*|$Ident\s*=\s*$Ident\s*)\(.*(\&\&|\|\||,)\s*$/) {
2236d1fe9c09SJoe Perches			$prevline =~ /^\+(\t*)(.*)$/;
2237d1fe9c09SJoe Perches			my $oldindent = $1;
2238d1fe9c09SJoe Perches			my $rest = $2;
2239d1fe9c09SJoe Perches
2240d1fe9c09SJoe Perches			my $pos = pos_last_openparen($rest);
2241d1fe9c09SJoe Perches			if ($pos >= 0) {
2242b34a26f3SJoe Perches				$line =~ /^(\+| )([ \t]*)/;
2243b34a26f3SJoe Perches				my $newindent = $2;
2244d1fe9c09SJoe Perches
2245d1fe9c09SJoe Perches				my $goodtabindent = $oldindent .
2246d1fe9c09SJoe Perches					"\t" x ($pos / 8) .
2247d1fe9c09SJoe Perches					" "  x ($pos % 8);
2248d1fe9c09SJoe Perches				my $goodspaceindent = $oldindent . " "  x $pos;
2249d1fe9c09SJoe Perches
2250d1fe9c09SJoe Perches				if ($newindent ne $goodtabindent &&
2251d1fe9c09SJoe Perches				    $newindent ne $goodspaceindent) {
22523705ce5bSJoe Perches
22533705ce5bSJoe Perches					if (CHK("PARENTHESIS_ALIGNMENT",
22543705ce5bSJoe Perches						"Alignment should match open parenthesis\n" . $hereprev) &&
22553705ce5bSJoe Perches					    $fix && $line =~ /^\+/) {
22563705ce5bSJoe Perches						$fixed[$linenr - 1] =~
22573705ce5bSJoe Perches						    s/^\+[ \t]*/\+$goodtabindent/;
22583705ce5bSJoe Perches					}
2259d1fe9c09SJoe Perches				}
2260d1fe9c09SJoe Perches			}
2261d1fe9c09SJoe Perches		}
2262d1fe9c09SJoe Perches
226323f780c9SJoe Perches		if ($line =~ /^\+.*\*[ \t]*\)[ \t]+(?!$Assignment|$Arithmetic)/) {
22643705ce5bSJoe Perches			if (CHK("SPACING",
22653705ce5bSJoe Perches				"No space is necessary after a cast\n" . $hereprev) &&
22663705ce5bSJoe Perches			    $fix) {
22673705ce5bSJoe Perches				$fixed[$linenr - 1] =~
22683705ce5bSJoe Perches				    s/^(\+.*\*[ \t]*\))[ \t]+/$1/;
22693705ce5bSJoe Perches			}
2270aad4f614SJoe Perches		}
2271aad4f614SJoe Perches
227205880600SJoe Perches		if ($realfile =~ m@^(drivers/net/|net/)@ &&
2273fdb4bcd6SJoe Perches		    $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
227485ad978cSJoe Perches		    $rawline =~ /^\+[ \t]*\*/ &&
227585ad978cSJoe Perches		    $realline > 2) {
227605880600SJoe Perches			WARN("NETWORKING_BLOCK_COMMENT_STYLE",
227705880600SJoe Perches			     "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
227805880600SJoe Perches		}
227905880600SJoe Perches
228005880600SJoe Perches		if ($realfile =~ m@^(drivers/net/|net/)@ &&
2281a605e32eSJoe Perches		    $prevrawline =~ /^\+[ \t]*\/\*/ &&		#starting /*
2282a605e32eSJoe Perches		    $prevrawline !~ /\*\/[ \t]*$/ &&		#no trailing */
228361135e96SJoe Perches		    $rawline =~ /^\+/ &&			#line is new
2284a605e32eSJoe Perches		    $rawline !~ /^\+[ \t]*\*/) {		#no leading *
2285a605e32eSJoe Perches			WARN("NETWORKING_BLOCK_COMMENT_STYLE",
2286a605e32eSJoe Perches			     "networking block comments start with * on subsequent lines\n" . $hereprev);
2287a605e32eSJoe Perches		}
2288a605e32eSJoe Perches
2289a605e32eSJoe Perches		if ($realfile =~ m@^(drivers/net/|net/)@ &&
2290c24f9f19SJoe Perches		    $rawline !~ m@^\+[ \t]*\*/[ \t]*$@ &&	#trailing */
2291c24f9f19SJoe Perches		    $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ &&	#inline /*...*/
2292c24f9f19SJoe Perches		    $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ &&	#trailing **/
2293c24f9f19SJoe Perches		    $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) {	#non blank */
229405880600SJoe Perches			WARN("NETWORKING_BLOCK_COMMENT_STYLE",
229505880600SJoe Perches			     "networking block comments put the trailing */ on a separate line\n" . $herecurr);
229605880600SJoe Perches		}
229705880600SJoe Perches
22987f619191SJoe Perches# check for missing blank lines after struct/union declarations
22997f619191SJoe Perches# with exceptions for various attributes and macros
23007f619191SJoe Perches		if ($prevline =~ /^[\+ ]};?\s*$/ &&
23017f619191SJoe Perches		    $line =~ /^\+/ &&
23027f619191SJoe Perches		    !($line =~ /^\+\s*$/ ||
23037f619191SJoe Perches		      $line =~ /^\+\s*EXPORT_SYMBOL/ ||
23047f619191SJoe Perches		      $line =~ /^\+\s*MODULE_/i ||
23057f619191SJoe Perches		      $line =~ /^\+\s*\#\s*(?:end|elif|else)/ ||
23067f619191SJoe Perches		      $line =~ /^\+[a-z_]*init/ ||
23077f619191SJoe Perches		      $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ ||
23087f619191SJoe Perches		      $line =~ /^\+\s*DECLARE/ ||
23097f619191SJoe Perches		      $line =~ /^\+\s*__setup/)) {
23107f619191SJoe Perches			CHK("LINE_SPACING",
23117f619191SJoe Perches			    "Please use a blank line after function/struct/union/enum declarations\n" . $hereprev);
23127f619191SJoe Perches		}
23137f619191SJoe Perches
2314365dd4eaSJoe Perches# check for multiple consecutive blank lines
2315365dd4eaSJoe Perches		if ($prevline =~ /^[\+ ]\s*$/ &&
2316365dd4eaSJoe Perches		    $line =~ /^\+\s*$/ &&
2317365dd4eaSJoe Perches		    $last_blank_line != ($linenr - 1)) {
2318365dd4eaSJoe Perches			CHK("LINE_SPACING",
2319365dd4eaSJoe Perches			    "Please don't use multiple blank lines\n" . $hereprev);
2320365dd4eaSJoe Perches			$last_blank_line = $linenr;
2321365dd4eaSJoe Perches		}
2322365dd4eaSJoe Perches
23233b617e3bSJoe Perches# check for missing blank lines after declarations
23243f7bac03SJoe Perches		if ($sline =~ /^\+\s+\S/ &&			#Not at char 1
23253f7bac03SJoe Perches			# actual declarations
23263f7bac03SJoe Perches		    ($prevline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
23275a4e1fd3SJoe Perches			# function pointer declarations
23285a4e1fd3SJoe Perches		     $prevline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
23293f7bac03SJoe Perches			# foo bar; where foo is some local typedef or #define
23303f7bac03SJoe Perches		     $prevline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
23313f7bac03SJoe Perches			# known declaration macros
23323f7bac03SJoe Perches		     $prevline =~ /^\+\s+$declaration_macros/) &&
23333f7bac03SJoe Perches			# for "else if" which can look like "$Ident $Ident"
23343f7bac03SJoe Perches		    !($prevline =~ /^\+\s+$c90_Keywords\b/ ||
23353f7bac03SJoe Perches			# other possible extensions of declaration lines
23363f7bac03SJoe Perches		      $prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ ||
23373f7bac03SJoe Perches			# not starting a section or a macro "\" extended line
23383f7bac03SJoe Perches		      $prevline =~ /(?:\{\s*|\\)$/) &&
23393f7bac03SJoe Perches			# looks like a declaration
23403f7bac03SJoe Perches		    !($sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
23415a4e1fd3SJoe Perches			# function pointer declarations
23425a4e1fd3SJoe Perches		      $sline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
23433f7bac03SJoe Perches			# foo bar; where foo is some local typedef or #define
23443f7bac03SJoe Perches		      $sline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
23453f7bac03SJoe Perches			# known declaration macros
23463f7bac03SJoe Perches		      $sline =~ /^\+\s+$declaration_macros/ ||
23473f7bac03SJoe Perches			# start of struct or union or enum
23483b617e3bSJoe Perches		      $sline =~ /^\+\s+(?:union|struct|enum|typedef)\b/ ||
23493f7bac03SJoe Perches			# start or end of block or continuation of declaration
23503f7bac03SJoe Perches		      $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ ||
23513f7bac03SJoe Perches			# bitfield continuation
23523f7bac03SJoe Perches		      $sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ ||
23533f7bac03SJoe Perches			# other possible extensions of declaration lines
23543f7bac03SJoe Perches		      $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) &&
23553f7bac03SJoe Perches			# indentation of previous and current line are the same
23563f7bac03SJoe Perches		    (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) {
2357fee0aa83SJoe Perches			WARN("LINE_SPACING",
23583f7bac03SJoe Perches			     "Missing a blank line after declarations\n" . $hereprev);
23593b617e3bSJoe Perches		}
23603b617e3bSJoe Perches
23615f7ddae6SRaffaele Recalcati# check for spaces at the beginning of a line.
23626b4c5bebSAndy Whitcroft# Exceptions:
23636b4c5bebSAndy Whitcroft#  1) within comments
23646b4c5bebSAndy Whitcroft#  2) indented preprocessor commands
23656b4c5bebSAndy Whitcroft#  3) hanging labels
23663705ce5bSJoe Perches		if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/)  {
23675f7ddae6SRaffaele Recalcati			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
23683705ce5bSJoe Perches			if (WARN("LEADING_SPACE",
23693705ce5bSJoe Perches				 "please, no spaces at the start of a line\n" . $herevet) &&
23703705ce5bSJoe Perches			    $fix) {
23713705ce5bSJoe Perches				$fixed[$linenr - 1] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
23723705ce5bSJoe Perches			}
23735f7ddae6SRaffaele Recalcati		}
23745f7ddae6SRaffaele Recalcati
2375b9ea10d6SAndy Whitcroft# check we are in a valid C source file if not then ignore this hunk
2376b9ea10d6SAndy Whitcroft		next if ($realfile !~ /\.(h|c)$/);
2377b9ea10d6SAndy Whitcroft
2378032a4c0fSJoe Perches# check indentation of any line with a bare else
2379032a4c0fSJoe Perches# if the previous line is a break or return and is indented 1 tab more...
2380032a4c0fSJoe Perches		if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {
2381032a4c0fSJoe Perches			my $tabs = length($1) + 1;
2382032a4c0fSJoe Perches			if ($prevline =~ /^\+\t{$tabs,$tabs}(?:break|return)\b/) {
2383032a4c0fSJoe Perches				WARN("UNNECESSARY_ELSE",
2384032a4c0fSJoe Perches				     "else is not generally useful after a break or return\n" . $hereprev);
2385032a4c0fSJoe Perches			}
2386032a4c0fSJoe Perches		}
2387032a4c0fSJoe Perches
23881ba8dfd1SKees Cook# discourage the addition of CONFIG_EXPERIMENTAL in #if(def).
23891ba8dfd1SKees Cook		if ($line =~ /^\+\s*\#\s*if.*\bCONFIG_EXPERIMENTAL\b/) {
23901ba8dfd1SKees Cook			WARN("CONFIG_EXPERIMENTAL",
23911ba8dfd1SKees Cook			     "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
23921ba8dfd1SKees Cook		}
23931ba8dfd1SKees Cook
2394c2fdda0dSAndy Whitcroft# check for RCS/CVS revision markers
2395cf655043SAndy Whitcroft		if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
2396000d1cc1SJoe Perches			WARN("CVS_KEYWORD",
2397000d1cc1SJoe Perches			     "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
2398c2fdda0dSAndy Whitcroft		}
239922f2a2efSAndy Whitcroft
240042e41c54SMike Frysinger# Blackfin: don't use __builtin_bfin_[cs]sync
240142e41c54SMike Frysinger		if ($line =~ /__builtin_bfin_csync/) {
240242e41c54SMike Frysinger			my $herevet = "$here\n" . cat_vet($line) . "\n";
2403000d1cc1SJoe Perches			ERROR("CSYNC",
2404000d1cc1SJoe Perches			      "use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
240542e41c54SMike Frysinger		}
240642e41c54SMike Frysinger		if ($line =~ /__builtin_bfin_ssync/) {
240742e41c54SMike Frysinger			my $herevet = "$here\n" . cat_vet($line) . "\n";
2408000d1cc1SJoe Perches			ERROR("SSYNC",
2409000d1cc1SJoe Perches			      "use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
241042e41c54SMike Frysinger		}
241142e41c54SMike Frysinger
241256e77d70SJoe Perches# check for old HOTPLUG __dev<foo> section markings
241356e77d70SJoe Perches		if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
241456e77d70SJoe Perches			WARN("HOTPLUG_SECTION",
241556e77d70SJoe Perches			     "Using $1 is unnecessary\n" . $herecurr);
241656e77d70SJoe Perches		}
241756e77d70SJoe Perches
24189c0ca6f9SAndy Whitcroft# Check for potential 'bare' types
24192b474a1aSAndy Whitcroft		my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
24202b474a1aSAndy Whitcroft		    $realline_next);
24213e469cdcSAndy Whitcroft#print "LINE<$line>\n";
24223e469cdcSAndy Whitcroft		if ($linenr >= $suppress_statement &&
24231b5539b1SJoe Perches		    $realcnt && $sline =~ /.\s*\S/) {
2424170d3a22SAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
2425f5fe35ddSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0);
2426171ae1a4SAndy Whitcroft			$stat =~ s/\n./\n /g;
2427171ae1a4SAndy Whitcroft			$cond =~ s/\n./\n /g;
2428171ae1a4SAndy Whitcroft
24293e469cdcSAndy Whitcroft#print "linenr<$linenr> <$stat>\n";
24303e469cdcSAndy Whitcroft			# If this statement has no statement boundaries within
24313e469cdcSAndy Whitcroft			# it there is no point in retrying a statement scan
24323e469cdcSAndy Whitcroft			# until we hit end of it.
24333e469cdcSAndy Whitcroft			my $frag = $stat; $frag =~ s/;+\s*$//;
24343e469cdcSAndy Whitcroft			if ($frag !~ /(?:{|;)/) {
24353e469cdcSAndy Whitcroft#print "skip<$line_nr_next>\n";
24363e469cdcSAndy Whitcroft				$suppress_statement = $line_nr_next;
24373e469cdcSAndy Whitcroft			}
2438f74bd194SAndy Whitcroft
24392b474a1aSAndy Whitcroft			# Find the real next line.
24402b474a1aSAndy Whitcroft			$realline_next = $line_nr_next;
24412b474a1aSAndy Whitcroft			if (defined $realline_next &&
24422b474a1aSAndy Whitcroft			    (!defined $lines[$realline_next - 1] ||
24432b474a1aSAndy Whitcroft			     substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
24442b474a1aSAndy Whitcroft				$realline_next++;
24452b474a1aSAndy Whitcroft			}
24462b474a1aSAndy Whitcroft
2447171ae1a4SAndy Whitcroft			my $s = $stat;
2448171ae1a4SAndy Whitcroft			$s =~ s/{.*$//s;
2449cf655043SAndy Whitcroft
2450c2fdda0dSAndy Whitcroft			# Ignore goto labels.
2451171ae1a4SAndy Whitcroft			if ($s =~ /$Ident:\*$/s) {
2452c2fdda0dSAndy Whitcroft
2453c2fdda0dSAndy Whitcroft			# Ignore functions being called
2454171ae1a4SAndy Whitcroft			} elsif ($s =~ /^.\s*$Ident\s*\(/s) {
2455c2fdda0dSAndy Whitcroft
2456463f2864SAndy Whitcroft			} elsif ($s =~ /^.\s*else\b/s) {
2457463f2864SAndy Whitcroft
2458c45dcabdSAndy Whitcroft			# declarations always start with types
2459d2506586SAndy 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) {
2460c45dcabdSAndy Whitcroft				my $type = $1;
2461c45dcabdSAndy Whitcroft				$type =~ s/\s+/ /g;
2462c45dcabdSAndy Whitcroft				possible($type, "A:" . $s);
2463c45dcabdSAndy Whitcroft
24646c72ffaaSAndy Whitcroft			# definitions in global scope can only start with types
2465a6a84062SAndy Whitcroft			} elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
2466c45dcabdSAndy Whitcroft				possible($1, "B:" . $s);
2467c2fdda0dSAndy Whitcroft			}
24688905a67cSAndy Whitcroft
24696c72ffaaSAndy Whitcroft			# any (foo ... *) is a pointer cast, and foo is a type
247065863862SAndy Whitcroft			while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
2471c45dcabdSAndy Whitcroft				possible($1, "C:" . $s);
24729c0ca6f9SAndy Whitcroft			}
24738905a67cSAndy Whitcroft
24748905a67cSAndy Whitcroft			# Check for any sort of function declaration.
24758905a67cSAndy Whitcroft			# int foo(something bar, other baz);
24768905a67cSAndy Whitcroft			# void (*store_gdt)(x86_descr_ptr *);
2477171ae1a4SAndy Whitcroft			if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
24788905a67cSAndy Whitcroft				my ($name_len) = length($1);
24798905a67cSAndy Whitcroft
2480cf655043SAndy Whitcroft				my $ctx = $s;
2481773647a0SAndy Whitcroft				substr($ctx, 0, $name_len + 1, '');
24828905a67cSAndy Whitcroft				$ctx =~ s/\)[^\)]*$//;
2483cf655043SAndy Whitcroft
24848905a67cSAndy Whitcroft				for my $arg (split(/\s*,\s*/, $ctx)) {
2485c45dcabdSAndy Whitcroft					if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
24868905a67cSAndy Whitcroft
2487c45dcabdSAndy Whitcroft						possible($1, "D:" . $s);
24888905a67cSAndy Whitcroft					}
24898905a67cSAndy Whitcroft				}
24908905a67cSAndy Whitcroft			}
24918905a67cSAndy Whitcroft
24929c0ca6f9SAndy Whitcroft		}
24939c0ca6f9SAndy Whitcroft
249400df344fSAndy Whitcroft#
249500df344fSAndy Whitcroft# Checks which may be anchored in the context.
249600df344fSAndy Whitcroft#
249700df344fSAndy Whitcroft
249800df344fSAndy Whitcroft# Check for switch () and associated case and default
249900df344fSAndy Whitcroft# statements should be at the same indent.
250000df344fSAndy Whitcroft		if ($line=~/\bswitch\s*\(.*\)/) {
250100df344fSAndy Whitcroft			my $err = '';
250200df344fSAndy Whitcroft			my $sep = '';
250300df344fSAndy Whitcroft			my @ctx = ctx_block_outer($linenr, $realcnt);
250400df344fSAndy Whitcroft			shift(@ctx);
250500df344fSAndy Whitcroft			for my $ctx (@ctx) {
250600df344fSAndy Whitcroft				my ($clen, $cindent) = line_stats($ctx);
250700df344fSAndy Whitcroft				if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
250800df344fSAndy Whitcroft							$indent != $cindent) {
250900df344fSAndy Whitcroft					$err .= "$sep$ctx\n";
251000df344fSAndy Whitcroft					$sep = '';
251100df344fSAndy Whitcroft				} else {
251200df344fSAndy Whitcroft					$sep = "[...]\n";
251300df344fSAndy Whitcroft				}
251400df344fSAndy Whitcroft			}
251500df344fSAndy Whitcroft			if ($err ne '') {
2516000d1cc1SJoe Perches				ERROR("SWITCH_CASE_INDENT_LEVEL",
2517000d1cc1SJoe Perches				      "switch and case should be at the same indent\n$hereline$err");
2518de7d4f0eSAndy Whitcroft			}
2519de7d4f0eSAndy Whitcroft		}
2520de7d4f0eSAndy Whitcroft
2521de7d4f0eSAndy Whitcroft# if/while/etc brace do not go on next line, unless defining a do while loop,
2522de7d4f0eSAndy Whitcroft# or if that brace on the next line is for something else
2523c45dcabdSAndy Whitcroft		if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
2524773647a0SAndy Whitcroft			my $pre_ctx = "$1$2";
2525773647a0SAndy Whitcroft
25269c0ca6f9SAndy Whitcroft			my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
25278eef05ddSJoe Perches
25288eef05ddSJoe Perches			if ($line =~ /^\+\t{6,}/) {
25298eef05ddSJoe Perches				WARN("DEEP_INDENTATION",
25308eef05ddSJoe Perches				     "Too many leading tabs - consider code refactoring\n" . $herecurr);
25318eef05ddSJoe Perches			}
25328eef05ddSJoe Perches
2533de7d4f0eSAndy Whitcroft			my $ctx_cnt = $realcnt - $#ctx - 1;
2534de7d4f0eSAndy Whitcroft			my $ctx = join("\n", @ctx);
2535de7d4f0eSAndy Whitcroft
2536548596d5SAndy Whitcroft			my $ctx_ln = $linenr;
2537548596d5SAndy Whitcroft			my $ctx_skip = $realcnt;
2538de7d4f0eSAndy Whitcroft
2539548596d5SAndy Whitcroft			while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
2540548596d5SAndy Whitcroft					defined $lines[$ctx_ln - 1] &&
2541548596d5SAndy Whitcroft					$lines[$ctx_ln - 1] =~ /^-/)) {
2542548596d5SAndy Whitcroft				##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
2543548596d5SAndy Whitcroft				$ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
2544773647a0SAndy Whitcroft				$ctx_ln++;
2545773647a0SAndy Whitcroft			}
2546548596d5SAndy Whitcroft
254753210168SAndy Whitcroft			#print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
254853210168SAndy Whitcroft			#print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
2549773647a0SAndy Whitcroft
2550773647a0SAndy Whitcroft			if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln -1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
2551000d1cc1SJoe Perches				ERROR("OPEN_BRACE",
2552000d1cc1SJoe Perches				      "that open brace { should be on the previous line\n" .
255301464f30SAndy Whitcroft					"$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
255400df344fSAndy Whitcroft			}
2555773647a0SAndy Whitcroft			if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
2556773647a0SAndy Whitcroft			    $ctx =~ /\)\s*\;\s*$/ &&
2557773647a0SAndy Whitcroft			    defined $lines[$ctx_ln - 1])
2558773647a0SAndy Whitcroft			{
25599c0ca6f9SAndy Whitcroft				my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
25609c0ca6f9SAndy Whitcroft				if ($nindent > $indent) {
2561000d1cc1SJoe Perches					WARN("TRAILING_SEMICOLON",
2562000d1cc1SJoe Perches					     "trailing semicolon indicates no statements, indent implies otherwise\n" .
256301464f30SAndy Whitcroft						"$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
25649c0ca6f9SAndy Whitcroft				}
25659c0ca6f9SAndy Whitcroft			}
256600df344fSAndy Whitcroft		}
256700df344fSAndy Whitcroft
25684d001e4dSAndy Whitcroft# Check relative indent for conditionals and blocks.
25694d001e4dSAndy Whitcroft		if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
25703e469cdcSAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
25713e469cdcSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0)
25723e469cdcSAndy Whitcroft					if (!defined $stat);
25734d001e4dSAndy Whitcroft			my ($s, $c) = ($stat, $cond);
25744d001e4dSAndy Whitcroft
25754d001e4dSAndy Whitcroft			substr($s, 0, length($c), '');
25764d001e4dSAndy Whitcroft
25774d001e4dSAndy Whitcroft			# Make sure we remove the line prefixes as we have
25784d001e4dSAndy Whitcroft			# none on the first line, and are going to readd them
25794d001e4dSAndy Whitcroft			# where necessary.
25804d001e4dSAndy Whitcroft			$s =~ s/\n./\n/gs;
25814d001e4dSAndy Whitcroft
25824d001e4dSAndy Whitcroft			# Find out how long the conditional actually is.
25836f779c18SAndy Whitcroft			my @newlines = ($c =~ /\n/gs);
25846f779c18SAndy Whitcroft			my $cond_lines = 1 + $#newlines;
25854d001e4dSAndy Whitcroft
25864d001e4dSAndy Whitcroft			# We want to check the first line inside the block
25874d001e4dSAndy Whitcroft			# starting at the end of the conditional, so remove:
25884d001e4dSAndy Whitcroft			#  1) any blank line termination
25894d001e4dSAndy Whitcroft			#  2) any opening brace { on end of the line
25904d001e4dSAndy Whitcroft			#  3) any do (...) {
25914d001e4dSAndy Whitcroft			my $continuation = 0;
25924d001e4dSAndy Whitcroft			my $check = 0;
25934d001e4dSAndy Whitcroft			$s =~ s/^.*\bdo\b//;
25944d001e4dSAndy Whitcroft			$s =~ s/^\s*{//;
25954d001e4dSAndy Whitcroft			if ($s =~ s/^\s*\\//) {
25964d001e4dSAndy Whitcroft				$continuation = 1;
25974d001e4dSAndy Whitcroft			}
25989bd49efeSAndy Whitcroft			if ($s =~ s/^\s*?\n//) {
25994d001e4dSAndy Whitcroft				$check = 1;
26004d001e4dSAndy Whitcroft				$cond_lines++;
26014d001e4dSAndy Whitcroft			}
26024d001e4dSAndy Whitcroft
26034d001e4dSAndy Whitcroft			# Also ignore a loop construct at the end of a
26044d001e4dSAndy Whitcroft			# preprocessor statement.
26054d001e4dSAndy Whitcroft			if (($prevline =~ /^.\s*#\s*define\s/ ||
26064d001e4dSAndy Whitcroft			    $prevline =~ /\\\s*$/) && $continuation == 0) {
26074d001e4dSAndy Whitcroft				$check = 0;
26084d001e4dSAndy Whitcroft			}
26094d001e4dSAndy Whitcroft
26109bd49efeSAndy Whitcroft			my $cond_ptr = -1;
2611740504c6SAndy Whitcroft			$continuation = 0;
26129bd49efeSAndy Whitcroft			while ($cond_ptr != $cond_lines) {
26139bd49efeSAndy Whitcroft				$cond_ptr = $cond_lines;
26144d001e4dSAndy Whitcroft
2615f16fa28fSAndy Whitcroft				# If we see an #else/#elif then the code
2616f16fa28fSAndy Whitcroft				# is not linear.
2617f16fa28fSAndy Whitcroft				if ($s =~ /^\s*\#\s*(?:else|elif)/) {
2618f16fa28fSAndy Whitcroft					$check = 0;
2619f16fa28fSAndy Whitcroft				}
2620f16fa28fSAndy Whitcroft
26219bd49efeSAndy Whitcroft				# Ignore:
26229bd49efeSAndy Whitcroft				#  1) blank lines, they should be at 0,
26239bd49efeSAndy Whitcroft				#  2) preprocessor lines, and
26249bd49efeSAndy Whitcroft				#  3) labels.
2625740504c6SAndy Whitcroft				if ($continuation ||
2626740504c6SAndy Whitcroft				    $s =~ /^\s*?\n/ ||
26279bd49efeSAndy Whitcroft				    $s =~ /^\s*#\s*?/ ||
26289bd49efeSAndy Whitcroft				    $s =~ /^\s*$Ident\s*:/) {
2629740504c6SAndy Whitcroft					$continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
263030dad6ebSAndy Whitcroft					if ($s =~ s/^.*?\n//) {
26319bd49efeSAndy Whitcroft						$cond_lines++;
26329bd49efeSAndy Whitcroft					}
26334d001e4dSAndy Whitcroft				}
263430dad6ebSAndy Whitcroft			}
26354d001e4dSAndy Whitcroft
26364d001e4dSAndy Whitcroft			my (undef, $sindent) = line_stats("+" . $s);
26374d001e4dSAndy Whitcroft			my $stat_real = raw_line($linenr, $cond_lines);
26384d001e4dSAndy Whitcroft
26394d001e4dSAndy Whitcroft			# Check if either of these lines are modified, else
26404d001e4dSAndy Whitcroft			# this is not this patch's fault.
26414d001e4dSAndy Whitcroft			if (!defined($stat_real) ||
26424d001e4dSAndy Whitcroft			    $stat !~ /^\+/ && $stat_real !~ /^\+/) {
26434d001e4dSAndy Whitcroft				$check = 0;
26444d001e4dSAndy Whitcroft			}
26454d001e4dSAndy Whitcroft			if (defined($stat_real) && $cond_lines > 1) {
26464d001e4dSAndy Whitcroft				$stat_real = "[...]\n$stat_real";
26474d001e4dSAndy Whitcroft			}
26484d001e4dSAndy Whitcroft
26499bd49efeSAndy 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";
26504d001e4dSAndy Whitcroft
26514d001e4dSAndy Whitcroft			if ($check && (($sindent % 8) != 0 ||
26524d001e4dSAndy Whitcroft			    ($sindent <= $indent && $s ne ''))) {
2653000d1cc1SJoe Perches				WARN("SUSPECT_CODE_INDENT",
2654000d1cc1SJoe Perches				     "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
26554d001e4dSAndy Whitcroft			}
26564d001e4dSAndy Whitcroft		}
26574d001e4dSAndy Whitcroft
26586c72ffaaSAndy Whitcroft		# Track the 'values' across context and added lines.
26596c72ffaaSAndy Whitcroft		my $opline = $line; $opline =~ s/^./ /;
26601f65f947SAndy Whitcroft		my ($curr_values, $curr_vars) =
26611f65f947SAndy Whitcroft				annotate_values($opline . "\n", $prev_values);
26626c72ffaaSAndy Whitcroft		$curr_values = $prev_values . $curr_values;
2663c2fdda0dSAndy Whitcroft		if ($dbg_values) {
2664c2fdda0dSAndy Whitcroft			my $outline = $opline; $outline =~ s/\t/ /g;
2665cf655043SAndy Whitcroft			print "$linenr > .$outline\n";
2666cf655043SAndy Whitcroft			print "$linenr > $curr_values\n";
26671f65f947SAndy Whitcroft			print "$linenr >  $curr_vars\n";
2668c2fdda0dSAndy Whitcroft		}
26696c72ffaaSAndy Whitcroft		$prev_values = substr($curr_values, -1);
26706c72ffaaSAndy Whitcroft
267100df344fSAndy Whitcroft#ignore lines not being added
26723705ce5bSJoe Perches		next if ($line =~ /^[^\+]/);
267300df344fSAndy Whitcroft
2674653d4876SAndy Whitcroft# TEST: allow direct testing of the type matcher.
26757429c690SAndy Whitcroft		if ($dbg_type) {
26767429c690SAndy Whitcroft			if ($line =~ /^.\s*$Declare\s*$/) {
2677000d1cc1SJoe Perches				ERROR("TEST_TYPE",
2678000d1cc1SJoe Perches				      "TEST: is type\n" . $herecurr);
26797429c690SAndy Whitcroft			} elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
2680000d1cc1SJoe Perches				ERROR("TEST_NOT_TYPE",
2681000d1cc1SJoe Perches				      "TEST: is not type ($1 is)\n". $herecurr);
26827429c690SAndy Whitcroft			}
2683653d4876SAndy Whitcroft			next;
2684653d4876SAndy Whitcroft		}
2685a1ef277eSAndy Whitcroft# TEST: allow direct testing of the attribute matcher.
2686a1ef277eSAndy Whitcroft		if ($dbg_attr) {
26879360b0e5SAndy Whitcroft			if ($line =~ /^.\s*$Modifier\s*$/) {
2688000d1cc1SJoe Perches				ERROR("TEST_ATTR",
2689000d1cc1SJoe Perches				      "TEST: is attr\n" . $herecurr);
26909360b0e5SAndy Whitcroft			} elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
2691000d1cc1SJoe Perches				ERROR("TEST_NOT_ATTR",
2692000d1cc1SJoe Perches				      "TEST: is not attr ($1 is)\n". $herecurr);
2693a1ef277eSAndy Whitcroft			}
2694a1ef277eSAndy Whitcroft			next;
2695a1ef277eSAndy Whitcroft		}
2696653d4876SAndy Whitcroft
2697f0a594c1SAndy Whitcroft# check for initialisation to aggregates open brace on the next line
269899423c20SAndy Whitcroft		if ($line =~ /^.\s*{/ &&
269999423c20SAndy Whitcroft		    $prevline =~ /(?:^|[^=])=\s*$/) {
2700000d1cc1SJoe Perches			ERROR("OPEN_BRACE",
2701000d1cc1SJoe Perches			      "that open brace { should be on the previous line\n" . $hereprev);
2702f0a594c1SAndy Whitcroft		}
2703f0a594c1SAndy Whitcroft
270400df344fSAndy Whitcroft#
270500df344fSAndy Whitcroft# Checks which are anchored on the added line.
270600df344fSAndy Whitcroft#
270700df344fSAndy Whitcroft
2708653d4876SAndy Whitcroft# check for malformed paths in #include statements (uses RAW line)
2709c45dcabdSAndy Whitcroft		if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
2710653d4876SAndy Whitcroft			my $path = $1;
2711653d4876SAndy Whitcroft			if ($path =~ m{//}) {
2712000d1cc1SJoe Perches				ERROR("MALFORMED_INCLUDE",
2713495e9d84SJoe Perches				      "malformed #include filename\n" . $herecurr);
2714495e9d84SJoe Perches			}
2715495e9d84SJoe Perches			if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
2716495e9d84SJoe Perches				ERROR("UAPI_INCLUDE",
2717495e9d84SJoe Perches				      "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
2718653d4876SAndy Whitcroft			}
2719653d4876SAndy Whitcroft		}
2720653d4876SAndy Whitcroft
272100df344fSAndy Whitcroft# no C99 // comments
272200df344fSAndy Whitcroft		if ($line =~ m{//}) {
27233705ce5bSJoe Perches			if (ERROR("C99_COMMENTS",
27243705ce5bSJoe Perches				  "do not use C99 // comments\n" . $herecurr) &&
27253705ce5bSJoe Perches			    $fix) {
27263705ce5bSJoe Perches				my $line = $fixed[$linenr - 1];
27273705ce5bSJoe Perches				if ($line =~ /\/\/(.*)$/) {
27283705ce5bSJoe Perches					my $comment = trim($1);
27293705ce5bSJoe Perches					$fixed[$linenr - 1] =~ s@\/\/(.*)$@/\* $comment \*/@;
27303705ce5bSJoe Perches				}
27313705ce5bSJoe Perches			}
273200df344fSAndy Whitcroft		}
273300df344fSAndy Whitcroft		# Remove C99 comments.
27340a920b5bSAndy Whitcroft		$line =~ s@//.*@@;
27356c72ffaaSAndy Whitcroft		$opline =~ s@//.*@@;
27360a920b5bSAndy Whitcroft
27372b474a1aSAndy Whitcroft# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
27382b474a1aSAndy Whitcroft# the whole statement.
27392b474a1aSAndy Whitcroft#print "APW <$lines[$realline_next - 1]>\n";
27402b474a1aSAndy Whitcroft		if (defined $realline_next &&
27412b474a1aSAndy Whitcroft		    exists $lines[$realline_next - 1] &&
27422b474a1aSAndy Whitcroft		    !defined $suppress_export{$realline_next} &&
27432b474a1aSAndy Whitcroft		    ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
27442b474a1aSAndy Whitcroft		     $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
27453cbf62dfSAndy Whitcroft			# Handle definitions which produce identifiers with
27463cbf62dfSAndy Whitcroft			# a prefix:
27473cbf62dfSAndy Whitcroft			#   XXX(foo);
27483cbf62dfSAndy Whitcroft			#   EXPORT_SYMBOL(something_foo);
2749653d4876SAndy Whitcroft			my $name = $1;
275087a53877SAndy Whitcroft			if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
27513cbf62dfSAndy Whitcroft			    $name =~ /^${Ident}_$2/) {
27523cbf62dfSAndy Whitcroft#print "FOO C name<$name>\n";
27533cbf62dfSAndy Whitcroft				$suppress_export{$realline_next} = 1;
27543cbf62dfSAndy Whitcroft
27553cbf62dfSAndy Whitcroft			} elsif ($stat !~ /(?:
27562b474a1aSAndy Whitcroft				\n.}\s*$|
275748012058SAndy Whitcroft				^.DEFINE_$Ident\(\Q$name\E\)|
275848012058SAndy Whitcroft				^.DECLARE_$Ident\(\Q$name\E\)|
275948012058SAndy Whitcroft				^.LIST_HEAD\(\Q$name\E\)|
27602b474a1aSAndy Whitcroft				^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
27612b474a1aSAndy Whitcroft				\b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
276248012058SAndy Whitcroft			    )/x) {
27632b474a1aSAndy Whitcroft#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
27642b474a1aSAndy Whitcroft				$suppress_export{$realline_next} = 2;
27652b474a1aSAndy Whitcroft			} else {
27662b474a1aSAndy Whitcroft				$suppress_export{$realline_next} = 1;
27670a920b5bSAndy Whitcroft			}
27680a920b5bSAndy Whitcroft		}
27692b474a1aSAndy Whitcroft		if (!defined $suppress_export{$linenr} &&
27702b474a1aSAndy Whitcroft		    $prevline =~ /^.\s*$/ &&
27712b474a1aSAndy Whitcroft		    ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
27722b474a1aSAndy Whitcroft		     $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
27732b474a1aSAndy Whitcroft#print "FOO B <$lines[$linenr - 1]>\n";
27742b474a1aSAndy Whitcroft			$suppress_export{$linenr} = 2;
27752b474a1aSAndy Whitcroft		}
27762b474a1aSAndy Whitcroft		if (defined $suppress_export{$linenr} &&
27772b474a1aSAndy Whitcroft		    $suppress_export{$linenr} == 2) {
2778000d1cc1SJoe Perches			WARN("EXPORT_SYMBOL",
2779000d1cc1SJoe Perches			     "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
27802b474a1aSAndy Whitcroft		}
27810a920b5bSAndy Whitcroft
27825150bda4SJoe Eloff# check for global initialisers.
2783d5e616fcSJoe Perches		if ($line =~ /^\+(\s*$Type\s*$Ident\s*(?:\s+$Modifier))*\s*=\s*(0|NULL|false)\s*;/) {
2784d5e616fcSJoe Perches			if (ERROR("GLOBAL_INITIALISERS",
2785000d1cc1SJoe Perches				  "do not initialise globals to 0 or NULL\n" .
2786d5e616fcSJoe Perches				      $herecurr) &&
2787d5e616fcSJoe Perches			    $fix) {
2788d5e616fcSJoe Perches				$fixed[$linenr - 1] =~ s/($Type\s*$Ident\s*(?:\s+$Modifier))*\s*=\s*(0|NULL|false)\s*;/$1;/;
2789d5e616fcSJoe Perches			}
2790f0a594c1SAndy Whitcroft		}
27910a920b5bSAndy Whitcroft# check for static initialisers.
2792d5e616fcSJoe Perches		if ($line =~ /^\+.*\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
2793d5e616fcSJoe Perches			if (ERROR("INITIALISED_STATIC",
2794000d1cc1SJoe Perches				  "do not initialise statics to 0 or NULL\n" .
2795d5e616fcSJoe Perches				      $herecurr) &&
2796d5e616fcSJoe Perches			    $fix) {
2797d5e616fcSJoe Perches				$fixed[$linenr - 1] =~ s/(\bstatic\s.*?)\s*=\s*(0|NULL|false)\s*;/$1;/;
2798d5e616fcSJoe Perches			}
27990a920b5bSAndy Whitcroft		}
28000a920b5bSAndy Whitcroft
2801cb710ecaSJoe Perches# check for static const char * arrays.
2802cb710ecaSJoe Perches		if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
2803000d1cc1SJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
2804000d1cc1SJoe Perches			     "static const char * array should probably be static const char * const\n" .
2805cb710ecaSJoe Perches				$herecurr);
2806cb710ecaSJoe Perches               }
2807cb710ecaSJoe Perches
2808cb710ecaSJoe Perches# check for static char foo[] = "bar" declarations.
2809cb710ecaSJoe Perches		if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
2810000d1cc1SJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
2811000d1cc1SJoe Perches			     "static char array declaration should probably be static const char\n" .
2812cb710ecaSJoe Perches				$herecurr);
2813cb710ecaSJoe Perches               }
2814cb710ecaSJoe Perches
28159b0fa60dSJoe Perches# check for non-global char *foo[] = {"bar", ...} declarations.
28169b0fa60dSJoe Perches		if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) {
28179b0fa60dSJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
28189b0fa60dSJoe Perches			     "char * array declaration might be better as static const\n" .
28199b0fa60dSJoe Perches				$herecurr);
28209b0fa60dSJoe Perches               }
28219b0fa60dSJoe Perches
2822b36190c5SJoe Perches# check for function declarations without arguments like "int foo()"
2823b36190c5SJoe Perches		if ($line =~ /(\b$Type\s+$Ident)\s*\(\s*\)/) {
2824b36190c5SJoe Perches			if (ERROR("FUNCTION_WITHOUT_ARGS",
2825b36190c5SJoe Perches				  "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) &&
2826b36190c5SJoe Perches			    $fix) {
2827b36190c5SJoe Perches				$fixed[$linenr - 1] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/;
2828b36190c5SJoe Perches			}
2829b36190c5SJoe Perches		}
2830b36190c5SJoe Perches
283192e112fdSJoe Perches# check for uses of DEFINE_PCI_DEVICE_TABLE
283292e112fdSJoe Perches		if ($line =~ /\bDEFINE_PCI_DEVICE_TABLE\s*\(\s*(\w+)\s*\)\s*=/) {
283392e112fdSJoe Perches			if (WARN("DEFINE_PCI_DEVICE_TABLE",
283492e112fdSJoe Perches				 "Prefer struct pci_device_id over deprecated DEFINE_PCI_DEVICE_TABLE\n" . $herecurr) &&
283592e112fdSJoe Perches			    $fix) {
283692e112fdSJoe 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\[\] = /;
283792e112fdSJoe Perches			}
283893ed0e2dSJoe Perches		}
283993ed0e2dSJoe Perches
2840653d4876SAndy Whitcroft# check for new typedefs, only function parameters and sparse annotations
2841653d4876SAndy Whitcroft# make sense.
2842653d4876SAndy Whitcroft		if ($line =~ /\btypedef\s/ &&
28438054576dSAndy Whitcroft		    $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
2844c45dcabdSAndy Whitcroft		    $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
28458ed22cadSAndy Whitcroft		    $line !~ /\b$typeTypedefs\b/ &&
2846653d4876SAndy Whitcroft		    $line !~ /\b__bitwise(?:__|)\b/) {
2847000d1cc1SJoe Perches			WARN("NEW_TYPEDEFS",
2848000d1cc1SJoe Perches			     "do not add new typedefs\n" . $herecurr);
28490a920b5bSAndy Whitcroft		}
28500a920b5bSAndy Whitcroft
28510a920b5bSAndy Whitcroft# * goes on variable not on type
285265863862SAndy Whitcroft		# (char*[ const])
2853bfcb2cc7SAndy Whitcroft		while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
2854bfcb2cc7SAndy Whitcroft			#print "AA<$1>\n";
28553705ce5bSJoe Perches			my ($ident, $from, $to) = ($1, $2, $2);
2856d8aaf121SAndy Whitcroft
285765863862SAndy Whitcroft			# Should start with a space.
285865863862SAndy Whitcroft			$to =~ s/^(\S)/ $1/;
285965863862SAndy Whitcroft			# Should not end with a space.
286065863862SAndy Whitcroft			$to =~ s/\s+$//;
286165863862SAndy Whitcroft			# '*'s should not have spaces between.
2862f9a0b3d1SAndy Whitcroft			while ($to =~ s/\*\s+\*/\*\*/) {
286365863862SAndy Whitcroft			}
2864d8aaf121SAndy Whitcroft
28653705ce5bSJoe Perches##			print "1: from<$from> to<$to> ident<$ident>\n";
286665863862SAndy Whitcroft			if ($from ne $to) {
28673705ce5bSJoe Perches				if (ERROR("POINTER_LOCATION",
28683705ce5bSJoe Perches					  "\"(foo$from)\" should be \"(foo$to)\"\n" .  $herecurr) &&
28693705ce5bSJoe Perches				    $fix) {
28703705ce5bSJoe Perches					my $sub_from = $ident;
28713705ce5bSJoe Perches					my $sub_to = $ident;
28723705ce5bSJoe Perches					$sub_to =~ s/\Q$from\E/$to/;
28733705ce5bSJoe Perches					$fixed[$linenr - 1] =~
28743705ce5bSJoe Perches					    s@\Q$sub_from\E@$sub_to@;
28753705ce5bSJoe Perches				}
287665863862SAndy Whitcroft			}
2877bfcb2cc7SAndy Whitcroft		}
2878bfcb2cc7SAndy Whitcroft		while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
2879bfcb2cc7SAndy Whitcroft			#print "BB<$1>\n";
28803705ce5bSJoe Perches			my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
2881d8aaf121SAndy Whitcroft
288265863862SAndy Whitcroft			# Should start with a space.
288365863862SAndy Whitcroft			$to =~ s/^(\S)/ $1/;
288465863862SAndy Whitcroft			# Should not end with a space.
288565863862SAndy Whitcroft			$to =~ s/\s+$//;
288665863862SAndy Whitcroft			# '*'s should not have spaces between.
2887f9a0b3d1SAndy Whitcroft			while ($to =~ s/\*\s+\*/\*\*/) {
288865863862SAndy Whitcroft			}
288965863862SAndy Whitcroft			# Modifiers should have spaces.
289065863862SAndy Whitcroft			$to =~ s/(\b$Modifier$)/$1 /;
289165863862SAndy Whitcroft
28923705ce5bSJoe Perches##			print "2: from<$from> to<$to> ident<$ident>\n";
2893667026e7SAndy Whitcroft			if ($from ne $to && $ident !~ /^$Modifier$/) {
28943705ce5bSJoe Perches				if (ERROR("POINTER_LOCATION",
28953705ce5bSJoe Perches					  "\"foo${from}bar\" should be \"foo${to}bar\"\n" .  $herecurr) &&
28963705ce5bSJoe Perches				    $fix) {
28973705ce5bSJoe Perches
28983705ce5bSJoe Perches					my $sub_from = $match;
28993705ce5bSJoe Perches					my $sub_to = $match;
29003705ce5bSJoe Perches					$sub_to =~ s/\Q$from\E/$to/;
29013705ce5bSJoe Perches					$fixed[$linenr - 1] =~
29023705ce5bSJoe Perches					    s@\Q$sub_from\E@$sub_to@;
29033705ce5bSJoe Perches				}
290465863862SAndy Whitcroft			}
29050a920b5bSAndy Whitcroft		}
29060a920b5bSAndy Whitcroft
29070a920b5bSAndy Whitcroft# # no BUG() or BUG_ON()
29080a920b5bSAndy Whitcroft# 		if ($line =~ /\b(BUG|BUG_ON)\b/) {
29090a920b5bSAndy Whitcroft# 			print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
29100a920b5bSAndy Whitcroft# 			print "$herecurr";
29110a920b5bSAndy Whitcroft# 			$clean = 0;
29120a920b5bSAndy Whitcroft# 		}
29130a920b5bSAndy Whitcroft
29148905a67cSAndy Whitcroft		if ($line =~ /\bLINUX_VERSION_CODE\b/) {
2915000d1cc1SJoe Perches			WARN("LINUX_VERSION_CODE",
2916000d1cc1SJoe Perches			     "LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
29178905a67cSAndy Whitcroft		}
29188905a67cSAndy Whitcroft
291917441227SJoe Perches# check for uses of printk_ratelimit
292017441227SJoe Perches		if ($line =~ /\bprintk_ratelimit\s*\(/) {
2921000d1cc1SJoe Perches			WARN("PRINTK_RATELIMITED",
2922000d1cc1SJoe Perches"Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
292317441227SJoe Perches		}
292417441227SJoe Perches
292500df344fSAndy Whitcroft# printk should use KERN_* levels.  Note that follow on printk's on the
292600df344fSAndy Whitcroft# same line do not need a level, so we use the current block context
292700df344fSAndy Whitcroft# to try and find and validate the current printk.  In summary the current
292825985edcSLucas De Marchi# printk includes all preceding printk's which have no newline on the end.
292900df344fSAndy Whitcroft# we assume the first bad printk is the one to report.
2930f0a594c1SAndy Whitcroft		if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
293100df344fSAndy Whitcroft			my $ok = 0;
293200df344fSAndy Whitcroft			for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
293300df344fSAndy Whitcroft				#print "CHECK<$lines[$ln - 1]\n";
293425985edcSLucas De Marchi				# we have a preceding printk if it ends
293500df344fSAndy Whitcroft				# with "\n" ignore it, else it is to blame
293600df344fSAndy Whitcroft				if ($lines[$ln - 1] =~ m{\bprintk\(}) {
293700df344fSAndy Whitcroft					if ($rawlines[$ln - 1] !~ m{\\n"}) {
293800df344fSAndy Whitcroft						$ok = 1;
293900df344fSAndy Whitcroft					}
294000df344fSAndy Whitcroft					last;
294100df344fSAndy Whitcroft				}
294200df344fSAndy Whitcroft			}
294300df344fSAndy Whitcroft			if ($ok == 0) {
2944000d1cc1SJoe Perches				WARN("PRINTK_WITHOUT_KERN_LEVEL",
2945000d1cc1SJoe Perches				     "printk() should include KERN_ facility level\n" . $herecurr);
29460a920b5bSAndy Whitcroft			}
294700df344fSAndy Whitcroft		}
29480a920b5bSAndy Whitcroft
2949243f3803SJoe Perches		if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
2950243f3803SJoe Perches			my $orig = $1;
2951243f3803SJoe Perches			my $level = lc($orig);
2952243f3803SJoe Perches			$level = "warn" if ($level eq "warning");
29538f26b837SJoe Perches			my $level2 = $level;
29548f26b837SJoe Perches			$level2 = "dbg" if ($level eq "debug");
2955243f3803SJoe Perches			WARN("PREFER_PR_LEVEL",
2956daa8b059SYogesh Chaudhari			     "Prefer [subsystem eg: netdev]_$level2([subsystem]dev, ... then dev_$level2(dev, ... then pr_$level(...  to printk(KERN_$orig ...\n" . $herecurr);
2957243f3803SJoe Perches		}
2958243f3803SJoe Perches
2959243f3803SJoe Perches		if ($line =~ /\bpr_warning\s*\(/) {
2960d5e616fcSJoe Perches			if (WARN("PREFER_PR_LEVEL",
2961d5e616fcSJoe Perches				 "Prefer pr_warn(... to pr_warning(...\n" . $herecurr) &&
2962d5e616fcSJoe Perches			    $fix) {
2963d5e616fcSJoe Perches				$fixed[$linenr - 1] =~
2964d5e616fcSJoe Perches				    s/\bpr_warning\b/pr_warn/;
2965d5e616fcSJoe Perches			}
2966243f3803SJoe Perches		}
2967243f3803SJoe Perches
2968dc139313SJoe Perches		if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
2969dc139313SJoe Perches			my $orig = $1;
2970dc139313SJoe Perches			my $level = lc($orig);
2971dc139313SJoe Perches			$level = "warn" if ($level eq "warning");
2972dc139313SJoe Perches			$level = "dbg" if ($level eq "debug");
2973dc139313SJoe Perches			WARN("PREFER_DEV_LEVEL",
2974dc139313SJoe Perches			     "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
2975dc139313SJoe Perches		}
2976dc139313SJoe Perches
2977653d4876SAndy Whitcroft# function brace can't be on same line, except for #defines of do while,
2978653d4876SAndy Whitcroft# or if closed on same line
2979c45dcabdSAndy Whitcroft		if (($line=~/$Type\s*$Ident\(.*\).*\s{/) and
2980c45dcabdSAndy Whitcroft		    !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) {
2981000d1cc1SJoe Perches			ERROR("OPEN_BRACE",
2982000d1cc1SJoe Perches			      "open brace '{' following function declarations go on the next line\n" . $herecurr);
29830a920b5bSAndy Whitcroft		}
2984653d4876SAndy Whitcroft
29858905a67cSAndy Whitcroft# open braces for enum, union and struct go on the same line.
29868905a67cSAndy Whitcroft		if ($line =~ /^.\s*{/ &&
29878905a67cSAndy Whitcroft		    $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
2988000d1cc1SJoe Perches			ERROR("OPEN_BRACE",
2989000d1cc1SJoe Perches			      "open brace '{' following $1 go on the same line\n" . $hereprev);
29908905a67cSAndy Whitcroft		}
29918905a67cSAndy Whitcroft
29920c73b4ebSAndy Whitcroft# missing space after union, struct or enum definition
29933705ce5bSJoe Perches		if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
29943705ce5bSJoe Perches			if (WARN("SPACING",
29953705ce5bSJoe Perches				 "missing space after $1 definition\n" . $herecurr) &&
29963705ce5bSJoe Perches			    $fix) {
29973705ce5bSJoe Perches				$fixed[$linenr - 1] =~
29983705ce5bSJoe Perches				    s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
29993705ce5bSJoe Perches			}
30000c73b4ebSAndy Whitcroft		}
30010c73b4ebSAndy Whitcroft
300231070b5dSJoe Perches# Function pointer declarations
300331070b5dSJoe Perches# check spacing between type, funcptr, and args
300431070b5dSJoe Perches# canonical declaration is "type (*funcptr)(args...)"
300591f72e9cSJoe Perches		if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) {
300631070b5dSJoe Perches			my $declare = $1;
300731070b5dSJoe Perches			my $pre_pointer_space = $2;
300831070b5dSJoe Perches			my $post_pointer_space = $3;
300931070b5dSJoe Perches			my $funcname = $4;
301031070b5dSJoe Perches			my $post_funcname_space = $5;
301131070b5dSJoe Perches			my $pre_args_space = $6;
301231070b5dSJoe Perches
301391f72e9cSJoe Perches# the $Declare variable will capture all spaces after the type
301491f72e9cSJoe Perches# so check it for a missing trailing missing space but pointer return types
301591f72e9cSJoe Perches# don't need a space so don't warn for those.
301691f72e9cSJoe Perches			my $post_declare_space = "";
301791f72e9cSJoe Perches			if ($declare =~ /(\s+)$/) {
301891f72e9cSJoe Perches				$post_declare_space = $1;
301991f72e9cSJoe Perches				$declare = rtrim($declare);
302091f72e9cSJoe Perches			}
302191f72e9cSJoe Perches			if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) {
302231070b5dSJoe Perches				WARN("SPACING",
302331070b5dSJoe Perches				     "missing space after return type\n" . $herecurr);
302491f72e9cSJoe Perches				$post_declare_space = " ";
302531070b5dSJoe Perches			}
302631070b5dSJoe Perches
302731070b5dSJoe Perches# unnecessary space "type  (*funcptr)(args...)"
302891f72e9cSJoe Perches# This test is not currently implemented because these declarations are
302991f72e9cSJoe Perches# equivalent to
303091f72e9cSJoe Perches#	int  foo(int bar, ...)
303191f72e9cSJoe Perches# and this is form shouldn't/doesn't generate a checkpatch warning.
303291f72e9cSJoe Perches#
303391f72e9cSJoe Perches#			elsif ($declare =~ /\s{2,}$/) {
303491f72e9cSJoe Perches#				WARN("SPACING",
303591f72e9cSJoe Perches#				     "Multiple spaces after return type\n" . $herecurr);
303691f72e9cSJoe Perches#			}
303731070b5dSJoe Perches
303831070b5dSJoe Perches# unnecessary space "type ( *funcptr)(args...)"
303931070b5dSJoe Perches			if (defined $pre_pointer_space &&
304031070b5dSJoe Perches			    $pre_pointer_space =~ /^\s/) {
304131070b5dSJoe Perches				WARN("SPACING",
304231070b5dSJoe Perches				     "Unnecessary space after function pointer open parenthesis\n" . $herecurr);
304331070b5dSJoe Perches			}
304431070b5dSJoe Perches
304531070b5dSJoe Perches# unnecessary space "type (* funcptr)(args...)"
304631070b5dSJoe Perches			if (defined $post_pointer_space &&
304731070b5dSJoe Perches			    $post_pointer_space =~ /^\s/) {
304831070b5dSJoe Perches				WARN("SPACING",
304931070b5dSJoe Perches				     "Unnecessary space before function pointer name\n" . $herecurr);
305031070b5dSJoe Perches			}
305131070b5dSJoe Perches
305231070b5dSJoe Perches# unnecessary space "type (*funcptr )(args...)"
305331070b5dSJoe Perches			if (defined $post_funcname_space &&
305431070b5dSJoe Perches			    $post_funcname_space =~ /^\s/) {
305531070b5dSJoe Perches				WARN("SPACING",
305631070b5dSJoe Perches				     "Unnecessary space after function pointer name\n" . $herecurr);
305731070b5dSJoe Perches			}
305831070b5dSJoe Perches
305931070b5dSJoe Perches# unnecessary space "type (*funcptr) (args...)"
306031070b5dSJoe Perches			if (defined $pre_args_space &&
306131070b5dSJoe Perches			    $pre_args_space =~ /^\s/) {
306231070b5dSJoe Perches				WARN("SPACING",
306331070b5dSJoe Perches				     "Unnecessary space before function pointer arguments\n" . $herecurr);
306431070b5dSJoe Perches			}
306531070b5dSJoe Perches
306631070b5dSJoe Perches			if (show_type("SPACING") && $fix) {
306731070b5dSJoe Perches				$fixed[$linenr - 1] =~
306891f72e9cSJoe Perches				    s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex;
306931070b5dSJoe Perches			}
307031070b5dSJoe Perches		}
307131070b5dSJoe Perches
30728d31cfceSAndy Whitcroft# check for spacing round square brackets; allowed:
30738d31cfceSAndy Whitcroft#  1. with a type on the left -- int [] a;
3074fe2a7dbcSAndy Whitcroft#  2. at the beginning of a line for slice initialisers -- [0...10] = 5,
3075fe2a7dbcSAndy Whitcroft#  3. inside a curly brace -- = { [0...10] = 5 }
30768d31cfceSAndy Whitcroft		while ($line =~ /(.*?\s)\[/g) {
30778d31cfceSAndy Whitcroft			my ($where, $prefix) = ($-[1], $1);
30788d31cfceSAndy Whitcroft			if ($prefix !~ /$Type\s+$/ &&
3079fe2a7dbcSAndy Whitcroft			    ($where != 0 || $prefix !~ /^.\s+$/) &&
3080daebc534SAndy Whitcroft			    $prefix !~ /[{,]\s+$/) {
30813705ce5bSJoe Perches				if (ERROR("BRACKET_SPACE",
30823705ce5bSJoe Perches					  "space prohibited before open square bracket '['\n" . $herecurr) &&
30833705ce5bSJoe Perches				    $fix) {
30843705ce5bSJoe Perches				    $fixed[$linenr - 1] =~
30853705ce5bSJoe Perches					s/^(\+.*?)\s+\[/$1\[/;
30863705ce5bSJoe Perches				}
30878d31cfceSAndy Whitcroft			}
30888d31cfceSAndy Whitcroft		}
30898d31cfceSAndy Whitcroft
3090f0a594c1SAndy Whitcroft# check for spaces between functions and their parentheses.
30916c72ffaaSAndy Whitcroft		while ($line =~ /($Ident)\s+\(/g) {
3092c2fdda0dSAndy Whitcroft			my $name = $1;
3093773647a0SAndy Whitcroft			my $ctx_before = substr($line, 0, $-[1]);
3094773647a0SAndy Whitcroft			my $ctx = "$ctx_before$name";
3095c2fdda0dSAndy Whitcroft
3096c2fdda0dSAndy Whitcroft			# Ignore those directives where spaces _are_ permitted.
3097773647a0SAndy Whitcroft			if ($name =~ /^(?:
3098773647a0SAndy Whitcroft				if|for|while|switch|return|case|
3099773647a0SAndy Whitcroft				volatile|__volatile__|
3100773647a0SAndy Whitcroft				__attribute__|format|__extension__|
3101773647a0SAndy Whitcroft				asm|__asm__)$/x)
3102773647a0SAndy Whitcroft			{
3103c2fdda0dSAndy Whitcroft			# cpp #define statements have non-optional spaces, ie
3104c2fdda0dSAndy Whitcroft			# if there is a space between the name and the open
3105c2fdda0dSAndy Whitcroft			# parenthesis it is simply not a parameter group.
3106c45dcabdSAndy Whitcroft			} elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
3107773647a0SAndy Whitcroft
3108773647a0SAndy Whitcroft			# cpp #elif statement condition may start with a (
3109c45dcabdSAndy Whitcroft			} elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
3110c2fdda0dSAndy Whitcroft
3111c2fdda0dSAndy Whitcroft			# If this whole things ends with a type its most
3112c2fdda0dSAndy Whitcroft			# likely a typedef for a function.
3113773647a0SAndy Whitcroft			} elsif ($ctx =~ /$Type$/) {
3114c2fdda0dSAndy Whitcroft
3115c2fdda0dSAndy Whitcroft			} else {
31163705ce5bSJoe Perches				if (WARN("SPACING",
31173705ce5bSJoe Perches					 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
31183705ce5bSJoe Perches					     $fix) {
31193705ce5bSJoe Perches					$fixed[$linenr - 1] =~
31203705ce5bSJoe Perches					    s/\b$name\s+\(/$name\(/;
31213705ce5bSJoe Perches				}
3122f0a594c1SAndy Whitcroft			}
31236c72ffaaSAndy Whitcroft		}
31249a4cad4eSEric Nelson
3125653d4876SAndy Whitcroft# Check operator spacing.
31260a920b5bSAndy Whitcroft		if (!($line=~/\#\s*include/)) {
31273705ce5bSJoe Perches			my $fixed_line = "";
31283705ce5bSJoe Perches			my $line_fixed = 0;
31293705ce5bSJoe Perches
31309c0ca6f9SAndy Whitcroft			my $ops = qr{
31319c0ca6f9SAndy Whitcroft				<<=|>>=|<=|>=|==|!=|
31329c0ca6f9SAndy Whitcroft				\+=|-=|\*=|\/=|%=|\^=|\|=|&=|
31339c0ca6f9SAndy Whitcroft				=>|->|<<|>>|<|>|=|!|~|
31341f65f947SAndy Whitcroft				&&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
313584731623SJoe Perches				\?:|\?|:
31369c0ca6f9SAndy Whitcroft			}x;
3137cf655043SAndy Whitcroft			my @elements = split(/($ops|;)/, $opline);
31383705ce5bSJoe Perches
31393705ce5bSJoe Perches##			print("element count: <" . $#elements . ">\n");
31403705ce5bSJoe Perches##			foreach my $el (@elements) {
31413705ce5bSJoe Perches##				print("el: <$el>\n");
31423705ce5bSJoe Perches##			}
31433705ce5bSJoe Perches
31443705ce5bSJoe Perches			my @fix_elements = ();
314500df344fSAndy Whitcroft			my $off = 0;
31466c72ffaaSAndy Whitcroft
31473705ce5bSJoe Perches			foreach my $el (@elements) {
31483705ce5bSJoe Perches				push(@fix_elements, substr($rawline, $off, length($el)));
31493705ce5bSJoe Perches				$off += length($el);
31503705ce5bSJoe Perches			}
31513705ce5bSJoe Perches
31523705ce5bSJoe Perches			$off = 0;
31533705ce5bSJoe Perches
31546c72ffaaSAndy Whitcroft			my $blank = copy_spacing($opline);
3155b34c648bSJoe Perches			my $last_after = -1;
31566c72ffaaSAndy Whitcroft
31570a920b5bSAndy Whitcroft			for (my $n = 0; $n < $#elements; $n += 2) {
31583705ce5bSJoe Perches
31593705ce5bSJoe Perches				my $good = $fix_elements[$n] . $fix_elements[$n + 1];
31603705ce5bSJoe Perches
31613705ce5bSJoe Perches##				print("n: <$n> good: <$good>\n");
31623705ce5bSJoe Perches
31634a0df2efSAndy Whitcroft				$off += length($elements[$n]);
31644a0df2efSAndy Whitcroft
316525985edcSLucas De Marchi				# Pick up the preceding and succeeding characters.
3166773647a0SAndy Whitcroft				my $ca = substr($opline, 0, $off);
3167773647a0SAndy Whitcroft				my $cc = '';
3168773647a0SAndy Whitcroft				if (length($opline) >= ($off + length($elements[$n + 1]))) {
3169773647a0SAndy Whitcroft					$cc = substr($opline, $off + length($elements[$n + 1]));
3170773647a0SAndy Whitcroft				}
3171773647a0SAndy Whitcroft				my $cb = "$ca$;$cc";
3172773647a0SAndy Whitcroft
31734a0df2efSAndy Whitcroft				my $a = '';
31744a0df2efSAndy Whitcroft				$a = 'V' if ($elements[$n] ne '');
31754a0df2efSAndy Whitcroft				$a = 'W' if ($elements[$n] =~ /\s$/);
3176cf655043SAndy Whitcroft				$a = 'C' if ($elements[$n] =~ /$;$/);
31774a0df2efSAndy Whitcroft				$a = 'B' if ($elements[$n] =~ /(\[|\()$/);
31784a0df2efSAndy Whitcroft				$a = 'O' if ($elements[$n] eq '');
3179773647a0SAndy Whitcroft				$a = 'E' if ($ca =~ /^\s*$/);
31804a0df2efSAndy Whitcroft
31810a920b5bSAndy Whitcroft				my $op = $elements[$n + 1];
31824a0df2efSAndy Whitcroft
31834a0df2efSAndy Whitcroft				my $c = '';
31840a920b5bSAndy Whitcroft				if (defined $elements[$n + 2]) {
31854a0df2efSAndy Whitcroft					$c = 'V' if ($elements[$n + 2] ne '');
31864a0df2efSAndy Whitcroft					$c = 'W' if ($elements[$n + 2] =~ /^\s/);
3187cf655043SAndy Whitcroft					$c = 'C' if ($elements[$n + 2] =~ /^$;/);
31884a0df2efSAndy Whitcroft					$c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
31894a0df2efSAndy Whitcroft					$c = 'O' if ($elements[$n + 2] eq '');
31908b1b3378SAndy Whitcroft					$c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
31914a0df2efSAndy Whitcroft				} else {
31924a0df2efSAndy Whitcroft					$c = 'E';
31930a920b5bSAndy Whitcroft				}
31940a920b5bSAndy Whitcroft
31954a0df2efSAndy Whitcroft				my $ctx = "${a}x${c}";
31964a0df2efSAndy Whitcroft
31974a0df2efSAndy Whitcroft				my $at = "(ctx:$ctx)";
31984a0df2efSAndy Whitcroft
31996c72ffaaSAndy Whitcroft				my $ptr = substr($blank, 0, $off) . "^";
3200de7d4f0eSAndy Whitcroft				my $hereptr = "$hereline$ptr\n";
32010a920b5bSAndy Whitcroft
320274048ed8SAndy Whitcroft				# Pull out the value of this operator.
32036c72ffaaSAndy Whitcroft				my $op_type = substr($curr_values, $off + 1, 1);
32040a920b5bSAndy Whitcroft
32051f65f947SAndy Whitcroft				# Get the full operator variant.
32061f65f947SAndy Whitcroft				my $opv = $op . substr($curr_vars, $off, 1);
32071f65f947SAndy Whitcroft
320813214adfSAndy Whitcroft				# Ignore operators passed as parameters.
320913214adfSAndy Whitcroft				if ($op_type ne 'V' &&
321013214adfSAndy Whitcroft				    $ca =~ /\s$/ && $cc =~ /^\s*,/) {
321113214adfSAndy Whitcroft
3212cf655043SAndy Whitcroft#				# Ignore comments
3213cf655043SAndy Whitcroft#				} elsif ($op =~ /^$;+$/) {
321413214adfSAndy Whitcroft
3215d8aaf121SAndy Whitcroft				# ; should have either the end of line or a space or \ after it
321613214adfSAndy Whitcroft				} elsif ($op eq ';') {
3217cf655043SAndy Whitcroft					if ($ctx !~ /.x[WEBC]/ &&
3218cf655043SAndy Whitcroft					    $cc !~ /^\\/ && $cc !~ /^;/) {
32193705ce5bSJoe Perches						if (ERROR("SPACING",
32203705ce5bSJoe Perches							  "space required after that '$op' $at\n" . $hereptr)) {
3221b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
32223705ce5bSJoe Perches							$line_fixed = 1;
32233705ce5bSJoe Perches						}
3224d8aaf121SAndy Whitcroft					}
3225d8aaf121SAndy Whitcroft
3226d8aaf121SAndy Whitcroft				# // is a comment
3227d8aaf121SAndy Whitcroft				} elsif ($op eq '//') {
32280a920b5bSAndy Whitcroft
3229b00e4814SJoe Perches				#   :   when part of a bitfield
3230b00e4814SJoe Perches				} elsif ($opv eq ':B') {
3231b00e4814SJoe Perches					# skip the bitfield test for now
3232b00e4814SJoe Perches
32331f65f947SAndy Whitcroft				# No spaces for:
32341f65f947SAndy Whitcroft				#   ->
3235b00e4814SJoe Perches				} elsif ($op eq '->') {
32364a0df2efSAndy Whitcroft					if ($ctx =~ /Wx.|.xW/) {
32373705ce5bSJoe Perches						if (ERROR("SPACING",
32383705ce5bSJoe Perches							  "spaces prohibited around that '$op' $at\n" . $hereptr)) {
3239b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
32403705ce5bSJoe Perches							if (defined $fix_elements[$n + 2]) {
32413705ce5bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
32423705ce5bSJoe Perches							}
3243b34c648bSJoe Perches							$line_fixed = 1;
32443705ce5bSJoe Perches						}
32450a920b5bSAndy Whitcroft					}
32460a920b5bSAndy Whitcroft
32470a920b5bSAndy Whitcroft				# , must have a space on the right.
32480a920b5bSAndy Whitcroft				} elsif ($op eq ',') {
3249cf655043SAndy Whitcroft					if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
32503705ce5bSJoe Perches						if (ERROR("SPACING",
32513705ce5bSJoe Perches							  "space required after that '$op' $at\n" . $hereptr)) {
3252b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
32533705ce5bSJoe Perches							$line_fixed = 1;
3254b34c648bSJoe Perches							$last_after = $n;
32553705ce5bSJoe Perches						}
32560a920b5bSAndy Whitcroft					}
32570a920b5bSAndy Whitcroft
32589c0ca6f9SAndy Whitcroft				# '*' as part of a type definition -- reported already.
325974048ed8SAndy Whitcroft				} elsif ($opv eq '*_') {
32609c0ca6f9SAndy Whitcroft					#warn "'*' is part of type\n";
32619c0ca6f9SAndy Whitcroft
32629c0ca6f9SAndy Whitcroft				# unary operators should have a space before and
32639c0ca6f9SAndy Whitcroft				# none after.  May be left adjacent to another
32649c0ca6f9SAndy Whitcroft				# unary operator, or a cast
32659c0ca6f9SAndy Whitcroft				} elsif ($op eq '!' || $op eq '~' ||
326674048ed8SAndy Whitcroft					 $opv eq '*U' || $opv eq '-U' ||
32670d413866SAndy Whitcroft					 $opv eq '&U' || $opv eq '&&U') {
3268cf655043SAndy Whitcroft					if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
32693705ce5bSJoe Perches						if (ERROR("SPACING",
32703705ce5bSJoe Perches							  "space required before that '$op' $at\n" . $hereptr)) {
3271b34c648bSJoe Perches							if ($n != $last_after + 2) {
3272b34c648bSJoe Perches								$good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]);
32733705ce5bSJoe Perches								$line_fixed = 1;
32743705ce5bSJoe Perches							}
32750a920b5bSAndy Whitcroft						}
3276b34c648bSJoe Perches					}
3277a3340b35SAndy Whitcroft					if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
3278171ae1a4SAndy Whitcroft						# A unary '*' may be const
3279171ae1a4SAndy Whitcroft
3280171ae1a4SAndy Whitcroft					} elsif ($ctx =~ /.xW/) {
32813705ce5bSJoe Perches						if (ERROR("SPACING",
32823705ce5bSJoe Perches							  "space prohibited after that '$op' $at\n" . $hereptr)) {
3283b34c648bSJoe Perches							$good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]);
32843705ce5bSJoe Perches							if (defined $fix_elements[$n + 2]) {
32853705ce5bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
32863705ce5bSJoe Perches							}
3287b34c648bSJoe Perches							$line_fixed = 1;
32883705ce5bSJoe Perches						}
32890a920b5bSAndy Whitcroft					}
32900a920b5bSAndy Whitcroft
32910a920b5bSAndy Whitcroft				# unary ++ and unary -- are allowed no space on one side.
32920a920b5bSAndy Whitcroft				} elsif ($op eq '++' or $op eq '--') {
3293773647a0SAndy Whitcroft					if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
32943705ce5bSJoe Perches						if (ERROR("SPACING",
32953705ce5bSJoe Perches							  "space required one side of that '$op' $at\n" . $hereptr)) {
3296b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
32973705ce5bSJoe Perches							$line_fixed = 1;
32983705ce5bSJoe Perches						}
32990a920b5bSAndy Whitcroft					}
3300773647a0SAndy Whitcroft					if ($ctx =~ /Wx[BE]/ ||
3301773647a0SAndy Whitcroft					    ($ctx =~ /Wx./ && $cc =~ /^;/)) {
33023705ce5bSJoe Perches						if (ERROR("SPACING",
33033705ce5bSJoe Perches							  "space prohibited before that '$op' $at\n" . $hereptr)) {
3304b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
33053705ce5bSJoe Perches							$line_fixed = 1;
33063705ce5bSJoe Perches						}
3307653d4876SAndy Whitcroft					}
3308773647a0SAndy Whitcroft					if ($ctx =~ /ExW/) {
33093705ce5bSJoe Perches						if (ERROR("SPACING",
33103705ce5bSJoe Perches							  "space prohibited after that '$op' $at\n" . $hereptr)) {
3311b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
33123705ce5bSJoe Perches							if (defined $fix_elements[$n + 2]) {
33133705ce5bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
3314773647a0SAndy Whitcroft							}
3315b34c648bSJoe Perches							$line_fixed = 1;
33163705ce5bSJoe Perches						}
33173705ce5bSJoe Perches					}
33180a920b5bSAndy Whitcroft
33190a920b5bSAndy Whitcroft				# << and >> may either have or not have spaces both sides
33209c0ca6f9SAndy Whitcroft				} elsif ($op eq '<<' or $op eq '>>' or
33219c0ca6f9SAndy Whitcroft					 $op eq '&' or $op eq '^' or $op eq '|' or
33229c0ca6f9SAndy Whitcroft					 $op eq '+' or $op eq '-' or
3323c2fdda0dSAndy Whitcroft					 $op eq '*' or $op eq '/' or
3324c2fdda0dSAndy Whitcroft					 $op eq '%')
33250a920b5bSAndy Whitcroft				{
3326773647a0SAndy Whitcroft					if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
33273705ce5bSJoe Perches						if (ERROR("SPACING",
33283705ce5bSJoe Perches							  "need consistent spacing around '$op' $at\n" . $hereptr)) {
3329b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
3330b34c648bSJoe Perches							if (defined $fix_elements[$n + 2]) {
3331b34c648bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
3332b34c648bSJoe Perches							}
33333705ce5bSJoe Perches							$line_fixed = 1;
33343705ce5bSJoe Perches						}
33350a920b5bSAndy Whitcroft					}
33360a920b5bSAndy Whitcroft
33371f65f947SAndy Whitcroft				# A colon needs no spaces before when it is
33381f65f947SAndy Whitcroft				# terminating a case value or a label.
33391f65f947SAndy Whitcroft				} elsif ($opv eq ':C' || $opv eq ':L') {
33401f65f947SAndy Whitcroft					if ($ctx =~ /Wx./) {
33413705ce5bSJoe Perches						if (ERROR("SPACING",
33423705ce5bSJoe Perches							  "space prohibited before that '$op' $at\n" . $hereptr)) {
3343b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
33443705ce5bSJoe Perches							$line_fixed = 1;
33453705ce5bSJoe Perches						}
33461f65f947SAndy Whitcroft					}
33471f65f947SAndy Whitcroft
33480a920b5bSAndy Whitcroft				# All the others need spaces both sides.
3349cf655043SAndy Whitcroft				} elsif ($ctx !~ /[EWC]x[CWE]/) {
33501f65f947SAndy Whitcroft					my $ok = 0;
33511f65f947SAndy Whitcroft
335222f2a2efSAndy Whitcroft					# Ignore email addresses <foo@bar>
33531f65f947SAndy Whitcroft					if (($op eq '<' &&
33541f65f947SAndy Whitcroft					     $cc =~ /^\S+\@\S+>/) ||
33551f65f947SAndy Whitcroft					    ($op eq '>' &&
33561f65f947SAndy Whitcroft					     $ca =~ /<\S+\@\S+$/))
33571f65f947SAndy Whitcroft					{
33581f65f947SAndy Whitcroft					    	$ok = 1;
33591f65f947SAndy Whitcroft					}
33601f65f947SAndy Whitcroft
336184731623SJoe Perches					# messages are ERROR, but ?: are CHK
33621f65f947SAndy Whitcroft					if ($ok == 0) {
336384731623SJoe Perches						my $msg_type = \&ERROR;
336484731623SJoe Perches						$msg_type = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
336584731623SJoe Perches
336684731623SJoe Perches						if (&{$msg_type}("SPACING",
33673705ce5bSJoe Perches								 "spaces required around that '$op' $at\n" . $hereptr)) {
3368b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
3369b34c648bSJoe Perches							if (defined $fix_elements[$n + 2]) {
3370b34c648bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
3371b34c648bSJoe Perches							}
33723705ce5bSJoe Perches							$line_fixed = 1;
33733705ce5bSJoe Perches						}
33740a920b5bSAndy Whitcroft					}
337522f2a2efSAndy Whitcroft				}
33764a0df2efSAndy Whitcroft				$off += length($elements[$n + 1]);
33773705ce5bSJoe Perches
33783705ce5bSJoe Perches##				print("n: <$n> GOOD: <$good>\n");
33793705ce5bSJoe Perches
33803705ce5bSJoe Perches				$fixed_line = $fixed_line . $good;
33810a920b5bSAndy Whitcroft			}
33823705ce5bSJoe Perches
33833705ce5bSJoe Perches			if (($#elements % 2) == 0) {
33843705ce5bSJoe Perches				$fixed_line = $fixed_line . $fix_elements[$#elements];
33853705ce5bSJoe Perches			}
33863705ce5bSJoe Perches
33873705ce5bSJoe Perches			if ($fix && $line_fixed && $fixed_line ne $fixed[$linenr - 1]) {
33883705ce5bSJoe Perches				$fixed[$linenr - 1] = $fixed_line;
33893705ce5bSJoe Perches			}
33903705ce5bSJoe Perches
33913705ce5bSJoe Perches
33920a920b5bSAndy Whitcroft		}
33930a920b5bSAndy Whitcroft
3394786b6326SJoe Perches# check for whitespace before a non-naked semicolon
3395d2e248e7SJoe Perches		if ($line =~ /^\+.*\S\s+;\s*$/) {
3396786b6326SJoe Perches			if (WARN("SPACING",
3397786b6326SJoe Perches				 "space prohibited before semicolon\n" . $herecurr) &&
3398786b6326SJoe Perches			    $fix) {
3399786b6326SJoe Perches				1 while $fixed[$linenr - 1] =~
3400786b6326SJoe Perches				    s/^(\+.*\S)\s+;/$1;/;
3401786b6326SJoe Perches			}
3402786b6326SJoe Perches		}
3403786b6326SJoe Perches
3404f0a594c1SAndy Whitcroft# check for multiple assignments
3405f0a594c1SAndy Whitcroft		if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
3406000d1cc1SJoe Perches			CHK("MULTIPLE_ASSIGNMENTS",
3407000d1cc1SJoe Perches			    "multiple assignments should be avoided\n" . $herecurr);
3408f0a594c1SAndy Whitcroft		}
3409f0a594c1SAndy Whitcroft
341022f2a2efSAndy Whitcroft## # check for multiple declarations, allowing for a function declaration
341122f2a2efSAndy Whitcroft## # continuation.
341222f2a2efSAndy Whitcroft## 		if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
341322f2a2efSAndy Whitcroft## 		    $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
341422f2a2efSAndy Whitcroft##
341522f2a2efSAndy Whitcroft## 			# Remove any bracketed sections to ensure we do not
341622f2a2efSAndy Whitcroft## 			# falsly report the parameters of functions.
341722f2a2efSAndy Whitcroft## 			my $ln = $line;
341822f2a2efSAndy Whitcroft## 			while ($ln =~ s/\([^\(\)]*\)//g) {
341922f2a2efSAndy Whitcroft## 			}
342022f2a2efSAndy Whitcroft## 			if ($ln =~ /,/) {
3421000d1cc1SJoe Perches## 				WARN("MULTIPLE_DECLARATION",
3422000d1cc1SJoe Perches##				     "declaring multiple variables together should be avoided\n" . $herecurr);
342322f2a2efSAndy Whitcroft## 			}
342422f2a2efSAndy Whitcroft## 		}
3425f0a594c1SAndy Whitcroft
34260a920b5bSAndy Whitcroft#need space before brace following if, while, etc
342722f2a2efSAndy Whitcroft		if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
342822f2a2efSAndy Whitcroft		    $line =~ /do{/) {
34293705ce5bSJoe Perches			if (ERROR("SPACING",
34303705ce5bSJoe Perches				  "space required before the open brace '{'\n" . $herecurr) &&
34313705ce5bSJoe Perches			    $fix) {
3432d5e616fcSJoe Perches				$fixed[$linenr - 1] =~ s/^(\+.*(?:do|\))){/$1 {/;
34333705ce5bSJoe Perches			}
3434de7d4f0eSAndy Whitcroft		}
3435de7d4f0eSAndy Whitcroft
3436c4a62ef9SJoe Perches## # check for blank lines before declarations
3437c4a62ef9SJoe Perches##		if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
3438c4a62ef9SJoe Perches##		    $prevrawline =~ /^.\s*$/) {
3439c4a62ef9SJoe Perches##			WARN("SPACING",
3440c4a62ef9SJoe Perches##			     "No blank lines before declarations\n" . $hereprev);
3441c4a62ef9SJoe Perches##		}
3442c4a62ef9SJoe Perches##
3443c4a62ef9SJoe Perches
3444de7d4f0eSAndy Whitcroft# closing brace should have a space following it when it has anything
3445de7d4f0eSAndy Whitcroft# on the line
3446de7d4f0eSAndy Whitcroft		if ($line =~ /}(?!(?:,|;|\)))\S/) {
3447d5e616fcSJoe Perches			if (ERROR("SPACING",
3448d5e616fcSJoe Perches				  "space required after that close brace '}'\n" . $herecurr) &&
3449d5e616fcSJoe Perches			    $fix) {
3450d5e616fcSJoe Perches				$fixed[$linenr - 1] =~
3451d5e616fcSJoe Perches				    s/}((?!(?:,|;|\)))\S)/} $1/;
3452d5e616fcSJoe Perches			}
34530a920b5bSAndy Whitcroft		}
34540a920b5bSAndy Whitcroft
345522f2a2efSAndy Whitcroft# check spacing on square brackets
345622f2a2efSAndy Whitcroft		if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
34573705ce5bSJoe Perches			if (ERROR("SPACING",
34583705ce5bSJoe Perches				  "space prohibited after that open square bracket '['\n" . $herecurr) &&
34593705ce5bSJoe Perches			    $fix) {
34603705ce5bSJoe Perches				$fixed[$linenr - 1] =~
34613705ce5bSJoe Perches				    s/\[\s+/\[/;
34623705ce5bSJoe Perches			}
346322f2a2efSAndy Whitcroft		}
346422f2a2efSAndy Whitcroft		if ($line =~ /\s\]/) {
34653705ce5bSJoe Perches			if (ERROR("SPACING",
34663705ce5bSJoe Perches				  "space prohibited before that close square bracket ']'\n" . $herecurr) &&
34673705ce5bSJoe Perches			    $fix) {
34683705ce5bSJoe Perches				$fixed[$linenr - 1] =~
34693705ce5bSJoe Perches				    s/\s+\]/\]/;
34703705ce5bSJoe Perches			}
347122f2a2efSAndy Whitcroft		}
347222f2a2efSAndy Whitcroft
3473c45dcabdSAndy Whitcroft# check spacing on parentheses
34749c0ca6f9SAndy Whitcroft		if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
34759c0ca6f9SAndy Whitcroft		    $line !~ /for\s*\(\s+;/) {
34763705ce5bSJoe Perches			if (ERROR("SPACING",
34773705ce5bSJoe Perches				  "space prohibited after that open parenthesis '('\n" . $herecurr) &&
34783705ce5bSJoe Perches			    $fix) {
34793705ce5bSJoe Perches				$fixed[$linenr - 1] =~
34803705ce5bSJoe Perches				    s/\(\s+/\(/;
34813705ce5bSJoe Perches			}
348222f2a2efSAndy Whitcroft		}
348313214adfSAndy Whitcroft		if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
3484c45dcabdSAndy Whitcroft		    $line !~ /for\s*\(.*;\s+\)/ &&
3485c45dcabdSAndy Whitcroft		    $line !~ /:\s+\)/) {
34863705ce5bSJoe Perches			if (ERROR("SPACING",
34873705ce5bSJoe Perches				  "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
34883705ce5bSJoe Perches			    $fix) {
34893705ce5bSJoe Perches				$fixed[$linenr - 1] =~
34903705ce5bSJoe Perches				    s/\s+\)/\)/;
34913705ce5bSJoe Perches			}
349222f2a2efSAndy Whitcroft		}
349322f2a2efSAndy Whitcroft
34940a920b5bSAndy Whitcroft#goto labels aren't indented, allow a single space however
34954a0df2efSAndy Whitcroft		if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
34960a920b5bSAndy Whitcroft		   !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
34973705ce5bSJoe Perches			if (WARN("INDENTED_LABEL",
34983705ce5bSJoe Perches				 "labels should not be indented\n" . $herecurr) &&
34993705ce5bSJoe Perches			    $fix) {
35003705ce5bSJoe Perches				$fixed[$linenr - 1] =~
35013705ce5bSJoe Perches				    s/^(.)\s+/$1/;
35023705ce5bSJoe Perches			}
35030a920b5bSAndy Whitcroft		}
35040a920b5bSAndy Whitcroft
35055b9553abSJoe Perches# return is not a function
3506507e5141SJoe Perches		if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {
3507c45dcabdSAndy Whitcroft			my $spacing = $1;
3508507e5141SJoe Perches			if ($^V && $^V ge 5.10.0 &&
35095b9553abSJoe Perches			    $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) {
35105b9553abSJoe Perches				my $value = $1;
35115b9553abSJoe Perches				$value = deparenthesize($value);
35125b9553abSJoe Perches				if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) {
3513000d1cc1SJoe Perches					ERROR("RETURN_PARENTHESES",
3514000d1cc1SJoe Perches					      "return is not a function, parentheses are not required\n" . $herecurr);
35155b9553abSJoe Perches				}
3516c45dcabdSAndy Whitcroft			} elsif ($spacing !~ /\s+/) {
3517000d1cc1SJoe Perches				ERROR("SPACING",
3518000d1cc1SJoe Perches				      "space required before the open parenthesis '('\n" . $herecurr);
3519c45dcabdSAndy Whitcroft			}
3520c45dcabdSAndy Whitcroft		}
3521507e5141SJoe Perches
3522b43ae21bSJoe Perches# unnecessary return in a void function
3523b43ae21bSJoe Perches# at end-of-function, with the previous line a single leading tab, then return;
3524b43ae21bSJoe Perches# and the line before that not a goto label target like "out:"
3525b43ae21bSJoe Perches		if ($sline =~ /^[ \+]}\s*$/ &&
3526b43ae21bSJoe Perches		    $prevline =~ /^\+\treturn\s*;\s*$/ &&
3527b43ae21bSJoe Perches		    $linenr >= 3 &&
3528b43ae21bSJoe Perches		    $lines[$linenr - 3] =~ /^[ +]/ &&
3529b43ae21bSJoe Perches		    $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) {
35309819cf25SJoe Perches			WARN("RETURN_VOID",
3531b43ae21bSJoe Perches			     "void function return statements are not generally useful\n" . $hereprev);
35329819cf25SJoe Perches               }
35339819cf25SJoe Perches
3534189248d8SJoe Perches# if statements using unnecessary parentheses - ie: if ((foo == bar))
3535189248d8SJoe Perches		if ($^V && $^V ge 5.10.0 &&
3536189248d8SJoe Perches		    $line =~ /\bif\s*((?:\(\s*){2,})/) {
3537189248d8SJoe Perches			my $openparens = $1;
3538189248d8SJoe Perches			my $count = $openparens =~ tr@\(@\(@;
3539189248d8SJoe Perches			my $msg = "";
3540189248d8SJoe Perches			if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) {
3541189248d8SJoe Perches				my $comp = $4;	#Not $1 because of $LvalOrFunc
3542189248d8SJoe Perches				$msg = " - maybe == should be = ?" if ($comp eq "==");
3543189248d8SJoe Perches				WARN("UNNECESSARY_PARENTHESES",
3544189248d8SJoe Perches				     "Unnecessary parentheses$msg\n" . $herecurr);
3545189248d8SJoe Perches			}
3546189248d8SJoe Perches		}
3547189248d8SJoe Perches
354853a3c448SAndy Whitcroft# Return of what appears to be an errno should normally be -'ve
354953a3c448SAndy Whitcroft		if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) {
355053a3c448SAndy Whitcroft			my $name = $1;
355153a3c448SAndy Whitcroft			if ($name ne 'EOF' && $name ne 'ERROR') {
3552000d1cc1SJoe Perches				WARN("USE_NEGATIVE_ERRNO",
3553000d1cc1SJoe Perches				     "return of an errno should typically be -ve (return -$1)\n" . $herecurr);
355453a3c448SAndy Whitcroft			}
355553a3c448SAndy Whitcroft		}
3556c45dcabdSAndy Whitcroft
35570a920b5bSAndy Whitcroft# Need a space before open parenthesis after if, while etc
35584a0df2efSAndy Whitcroft		if ($line =~ /\b(if|while|for|switch)\(/) {
35593705ce5bSJoe Perches			if (ERROR("SPACING",
35603705ce5bSJoe Perches				  "space required before the open parenthesis '('\n" . $herecurr) &&
35613705ce5bSJoe Perches			    $fix) {
35623705ce5bSJoe Perches				$fixed[$linenr - 1] =~
35633705ce5bSJoe Perches				    s/\b(if|while|for|switch)\(/$1 \(/;
35643705ce5bSJoe Perches			}
35650a920b5bSAndy Whitcroft		}
35660a920b5bSAndy Whitcroft
3567f5fe35ddSAndy Whitcroft# Check for illegal assignment in if conditional -- and check for trailing
3568f5fe35ddSAndy Whitcroft# statements after the conditional.
3569170d3a22SAndy Whitcroft		if ($line =~ /do\s*(?!{)/) {
35703e469cdcSAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
35713e469cdcSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0)
35723e469cdcSAndy Whitcroft					if (!defined $stat);
3573170d3a22SAndy Whitcroft			my ($stat_next) = ctx_statement_block($line_nr_next,
3574170d3a22SAndy Whitcroft						$remain_next, $off_next);
3575170d3a22SAndy Whitcroft			$stat_next =~ s/\n./\n /g;
3576170d3a22SAndy Whitcroft			##print "stat<$stat> stat_next<$stat_next>\n";
3577170d3a22SAndy Whitcroft
3578170d3a22SAndy Whitcroft			if ($stat_next =~ /^\s*while\b/) {
3579170d3a22SAndy Whitcroft				# If the statement carries leading newlines,
3580170d3a22SAndy Whitcroft				# then count those as offsets.
3581170d3a22SAndy Whitcroft				my ($whitespace) =
3582170d3a22SAndy Whitcroft					($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
3583170d3a22SAndy Whitcroft				my $offset =
3584170d3a22SAndy Whitcroft					statement_rawlines($whitespace) - 1;
3585170d3a22SAndy Whitcroft
3586170d3a22SAndy Whitcroft				$suppress_whiletrailers{$line_nr_next +
3587170d3a22SAndy Whitcroft								$offset} = 1;
3588170d3a22SAndy Whitcroft			}
3589170d3a22SAndy Whitcroft		}
3590170d3a22SAndy Whitcroft		if (!defined $suppress_whiletrailers{$linenr} &&
3591c11230f4SJoe Perches		    defined($stat) && defined($cond) &&
3592170d3a22SAndy Whitcroft		    $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
3593171ae1a4SAndy Whitcroft			my ($s, $c) = ($stat, $cond);
35948905a67cSAndy Whitcroft
3595b53c8e10SAndy Whitcroft			if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
3596000d1cc1SJoe Perches				ERROR("ASSIGN_IN_IF",
3597000d1cc1SJoe Perches				      "do not use assignment in if condition\n" . $herecurr);
35988905a67cSAndy Whitcroft			}
35998905a67cSAndy Whitcroft
36008905a67cSAndy Whitcroft			# Find out what is on the end of the line after the
36018905a67cSAndy Whitcroft			# conditional.
3602773647a0SAndy Whitcroft			substr($s, 0, length($c), '');
36038905a67cSAndy Whitcroft			$s =~ s/\n.*//g;
360413214adfSAndy Whitcroft			$s =~ s/$;//g; 	# Remove any comments
360553210168SAndy Whitcroft			if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
360653210168SAndy Whitcroft			    $c !~ /}\s*while\s*/)
3607773647a0SAndy Whitcroft			{
3608bb44ad39SAndy Whitcroft				# Find out how long the conditional actually is.
3609bb44ad39SAndy Whitcroft				my @newlines = ($c =~ /\n/gs);
3610bb44ad39SAndy Whitcroft				my $cond_lines = 1 + $#newlines;
361142bdf74cSHidetoshi Seto				my $stat_real = '';
3612bb44ad39SAndy Whitcroft
361342bdf74cSHidetoshi Seto				$stat_real = raw_line($linenr, $cond_lines)
361442bdf74cSHidetoshi Seto							. "\n" if ($cond_lines);
3615bb44ad39SAndy Whitcroft				if (defined($stat_real) && $cond_lines > 1) {
3616bb44ad39SAndy Whitcroft					$stat_real = "[...]\n$stat_real";
3617bb44ad39SAndy Whitcroft				}
3618bb44ad39SAndy Whitcroft
3619000d1cc1SJoe Perches				ERROR("TRAILING_STATEMENTS",
3620000d1cc1SJoe Perches				      "trailing statements should be on next line\n" . $herecurr . $stat_real);
36218905a67cSAndy Whitcroft			}
36228905a67cSAndy Whitcroft		}
36238905a67cSAndy Whitcroft
362413214adfSAndy Whitcroft# Check for bitwise tests written as boolean
362513214adfSAndy Whitcroft		if ($line =~ /
362613214adfSAndy Whitcroft			(?:
362713214adfSAndy Whitcroft				(?:\[|\(|\&\&|\|\|)
362813214adfSAndy Whitcroft				\s*0[xX][0-9]+\s*
362913214adfSAndy Whitcroft				(?:\&\&|\|\|)
363013214adfSAndy Whitcroft			|
363113214adfSAndy Whitcroft				(?:\&\&|\|\|)
363213214adfSAndy Whitcroft				\s*0[xX][0-9]+\s*
363313214adfSAndy Whitcroft				(?:\&\&|\|\||\)|\])
363413214adfSAndy Whitcroft			)/x)
363513214adfSAndy Whitcroft		{
3636000d1cc1SJoe Perches			WARN("HEXADECIMAL_BOOLEAN_TEST",
3637000d1cc1SJoe Perches			     "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
363813214adfSAndy Whitcroft		}
363913214adfSAndy Whitcroft
36408905a67cSAndy Whitcroft# if and else should not have general statements after it
364113214adfSAndy Whitcroft		if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
364213214adfSAndy Whitcroft			my $s = $1;
364313214adfSAndy Whitcroft			$s =~ s/$;//g; 	# Remove any comments
364413214adfSAndy Whitcroft			if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
3645000d1cc1SJoe Perches				ERROR("TRAILING_STATEMENTS",
3646000d1cc1SJoe Perches				      "trailing statements should be on next line\n" . $herecurr);
36470a920b5bSAndy Whitcroft			}
364813214adfSAndy Whitcroft		}
364939667782SAndy Whitcroft# if should not continue a brace
365039667782SAndy Whitcroft		if ($line =~ /}\s*if\b/) {
3651000d1cc1SJoe Perches			ERROR("TRAILING_STATEMENTS",
3652048b123fSRasmus Villemoes			      "trailing statements should be on next line (or did you mean 'else if'?)\n" .
365339667782SAndy Whitcroft				$herecurr);
365439667782SAndy Whitcroft		}
3655a1080bf8SAndy Whitcroft# case and default should not have general statements after them
3656a1080bf8SAndy Whitcroft		if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
3657a1080bf8SAndy Whitcroft		    $line !~ /\G(?:
36583fef12d6SAndy Whitcroft			(?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
3659a1080bf8SAndy Whitcroft			\s*return\s+
3660a1080bf8SAndy Whitcroft		    )/xg)
3661a1080bf8SAndy Whitcroft		{
3662000d1cc1SJoe Perches			ERROR("TRAILING_STATEMENTS",
3663000d1cc1SJoe Perches			      "trailing statements should be on next line\n" . $herecurr);
3664a1080bf8SAndy Whitcroft		}
36650a920b5bSAndy Whitcroft
36660a920b5bSAndy Whitcroft		# Check for }<nl>else {, these must be at the same
36670a920b5bSAndy Whitcroft		# indent level to be relevant to each other.
36680a920b5bSAndy Whitcroft		if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
36690a920b5bSAndy Whitcroft						$previndent == $indent) {
3670000d1cc1SJoe Perches			ERROR("ELSE_AFTER_BRACE",
3671000d1cc1SJoe Perches			      "else should follow close brace '}'\n" . $hereprev);
36720a920b5bSAndy Whitcroft		}
36730a920b5bSAndy Whitcroft
3674c2fdda0dSAndy Whitcroft		if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and
3675c2fdda0dSAndy Whitcroft						$previndent == $indent) {
3676c2fdda0dSAndy Whitcroft			my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
3677c2fdda0dSAndy Whitcroft
3678c2fdda0dSAndy Whitcroft			# Find out what is on the end of the line after the
3679c2fdda0dSAndy Whitcroft			# conditional.
3680773647a0SAndy Whitcroft			substr($s, 0, length($c), '');
3681c2fdda0dSAndy Whitcroft			$s =~ s/\n.*//g;
3682c2fdda0dSAndy Whitcroft
3683c2fdda0dSAndy Whitcroft			if ($s =~ /^\s*;/) {
3684000d1cc1SJoe Perches				ERROR("WHILE_AFTER_BRACE",
3685000d1cc1SJoe Perches				      "while should follow close brace '}'\n" . $hereprev);
3686c2fdda0dSAndy Whitcroft			}
3687c2fdda0dSAndy Whitcroft		}
3688c2fdda0dSAndy Whitcroft
368995e2c602SJoe Perches#Specific variable tests
3690323c1260SJoe Perches		while ($line =~ m{($Constant|$Lval)}g) {
3691323c1260SJoe Perches			my $var = $1;
369295e2c602SJoe Perches
369395e2c602SJoe Perches#gcc binary extension
369495e2c602SJoe Perches			if ($var =~ /^$Binary$/) {
3695d5e616fcSJoe Perches				if (WARN("GCC_BINARY_CONSTANT",
3696d5e616fcSJoe Perches					 "Avoid gcc v4.3+ binary constant extension: <$var>\n" . $herecurr) &&
3697d5e616fcSJoe Perches				    $fix) {
3698d5e616fcSJoe Perches					my $hexval = sprintf("0x%x", oct($var));
3699d5e616fcSJoe Perches					$fixed[$linenr - 1] =~
3700d5e616fcSJoe Perches					    s/\b$var\b/$hexval/;
3701d5e616fcSJoe Perches				}
370295e2c602SJoe Perches			}
370395e2c602SJoe Perches
370495e2c602SJoe Perches#CamelCase
3705807bd26cSJoe Perches			if ($var !~ /^$Constant$/ &&
3706be79794bSJoe Perches			    $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
370722735ce8SJoe Perches#Ignore Page<foo> variants
3708807bd26cSJoe Perches			    $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
370922735ce8SJoe Perches#Ignore SI style variants like nS, mV and dB (ie: max_uV, regulator_min_uA_show)
37103445686aSJoe Perches			    $var !~ /^(?:[a-z_]*?)_?[a-z][A-Z](?:_[a-z_]+)?$/) {
37117e781f67SJoe Perches				while ($var =~ m{($Ident)}g) {
37127e781f67SJoe Perches					my $word = $1;
37137e781f67SJoe Perches					next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
3714d8b07710SJoe Perches					if ($check) {
3715d8b07710SJoe Perches						seed_camelcase_includes();
3716d8b07710SJoe Perches						if (!$file && !$camelcase_file_seeded) {
3717d8b07710SJoe Perches							seed_camelcase_file($realfile);
3718d8b07710SJoe Perches							$camelcase_file_seeded = 1;
3719d8b07710SJoe Perches						}
3720d8b07710SJoe Perches					}
37217e781f67SJoe Perches					if (!defined $camelcase{$word}) {
37227e781f67SJoe Perches						$camelcase{$word} = 1;
3723be79794bSJoe Perches						CHK("CAMELCASE",
37247e781f67SJoe Perches						    "Avoid CamelCase: <$word>\n" . $herecurr);
37257e781f67SJoe Perches					}
3726323c1260SJoe Perches				}
3727323c1260SJoe Perches			}
37283445686aSJoe Perches		}
37290a920b5bSAndy Whitcroft
37300a920b5bSAndy Whitcroft#no spaces allowed after \ in define
3731d5e616fcSJoe Perches		if ($line =~ /\#\s*define.*\\\s+$/) {
3732d5e616fcSJoe Perches			if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
3733d5e616fcSJoe Perches				 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
3734d5e616fcSJoe Perches			    $fix) {
3735d5e616fcSJoe Perches				$fixed[$linenr - 1] =~ s/\s+$//;
3736d5e616fcSJoe Perches			}
37370a920b5bSAndy Whitcroft		}
37380a920b5bSAndy Whitcroft
3739653d4876SAndy Whitcroft#warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
3740c45dcabdSAndy Whitcroft		if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
3741e09dec48SAndy Whitcroft			my $file = "$1.h";
3742e09dec48SAndy Whitcroft			my $checkfile = "include/linux/$file";
3743e09dec48SAndy Whitcroft			if (-f "$root/$checkfile" &&
3744e09dec48SAndy Whitcroft			    $realfile ne $checkfile &&
37457840a94cSWolfram Sang			    $1 !~ /$allowed_asm_includes/)
3746c45dcabdSAndy Whitcroft			{
3747e09dec48SAndy Whitcroft				if ($realfile =~ m{^arch/}) {
3748000d1cc1SJoe Perches					CHK("ARCH_INCLUDE_LINUX",
3749000d1cc1SJoe Perches					    "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
3750e09dec48SAndy Whitcroft				} else {
3751000d1cc1SJoe Perches					WARN("INCLUDE_LINUX",
3752000d1cc1SJoe Perches					     "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
3753e09dec48SAndy Whitcroft				}
37540a920b5bSAndy Whitcroft			}
37550a920b5bSAndy Whitcroft		}
37560a920b5bSAndy Whitcroft
3757653d4876SAndy Whitcroft# multi-statement macros should be enclosed in a do while loop, grab the
3758653d4876SAndy Whitcroft# first statement and ensure its the whole macro if its not enclosed
3759cf655043SAndy Whitcroft# in a known good container
3760b8f96a31SAndy Whitcroft		if ($realfile !~ m@/vmlinux.lds.h$@ &&
3761b8f96a31SAndy Whitcroft		    $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
3762d8aaf121SAndy Whitcroft			my $ln = $linenr;
3763d8aaf121SAndy Whitcroft			my $cnt = $realcnt;
3764c45dcabdSAndy Whitcroft			my ($off, $dstat, $dcond, $rest);
3765c45dcabdSAndy Whitcroft			my $ctx = '';
3766c45dcabdSAndy Whitcroft			($dstat, $dcond, $ln, $cnt, $off) =
3767f74bd194SAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0);
3768f74bd194SAndy Whitcroft			$ctx = $dstat;
3769c45dcabdSAndy Whitcroft			#print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
3770a3bb97a7SAndy Whitcroft			#print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
3771c45dcabdSAndy Whitcroft
3772f74bd194SAndy Whitcroft			$dstat =~ s/^.\s*\#\s*define\s+$Ident(?:\([^\)]*\))?\s*//;
3773292f1a9bSAndy Whitcroft			$dstat =~ s/$;//g;
3774c45dcabdSAndy Whitcroft			$dstat =~ s/\\\n.//g;
3775c45dcabdSAndy Whitcroft			$dstat =~ s/^\s*//s;
3776c45dcabdSAndy Whitcroft			$dstat =~ s/\s*$//s;
3777c45dcabdSAndy Whitcroft
3778c45dcabdSAndy Whitcroft			# Flatten any parentheses and braces
3779bf30d6edSAndy Whitcroft			while ($dstat =~ s/\([^\(\)]*\)/1/ ||
3780bf30d6edSAndy Whitcroft			       $dstat =~ s/\{[^\{\}]*\}/1/ ||
3781c81769fdSAndy Whitcroft			       $dstat =~ s/\[[^\[\]]*\]/1/)
3782bf30d6edSAndy Whitcroft			{
3783c45dcabdSAndy Whitcroft			}
3784c45dcabdSAndy Whitcroft
3785e45bab8eSAndy Whitcroft			# Flatten any obvious string concatentation.
3786e45bab8eSAndy Whitcroft			while ($dstat =~ s/("X*")\s*$Ident/$1/ ||
3787e45bab8eSAndy Whitcroft			       $dstat =~ s/$Ident\s*("X*")/$1/)
3788e45bab8eSAndy Whitcroft			{
3789e45bab8eSAndy Whitcroft			}
3790e45bab8eSAndy Whitcroft
3791c45dcabdSAndy Whitcroft			my $exceptions = qr{
3792c45dcabdSAndy Whitcroft				$Declare|
3793c45dcabdSAndy Whitcroft				module_param_named|
3794a0a0a7a9SKees Cook				MODULE_PARM_DESC|
3795c45dcabdSAndy Whitcroft				DECLARE_PER_CPU|
3796c45dcabdSAndy Whitcroft				DEFINE_PER_CPU|
3797383099fdSAndy Whitcroft				__typeof__\(|
379822fd2d3eSStefani Seibold				union|
379922fd2d3eSStefani Seibold				struct|
3800ea71a0a0SAndy Whitcroft				\.$Ident\s*=\s*|
3801ea71a0a0SAndy Whitcroft				^\"|\"$
3802c45dcabdSAndy Whitcroft			}x;
38035eaa20b9SAndy Whitcroft			#print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
3804f74bd194SAndy Whitcroft			if ($dstat ne '' &&
3805f74bd194SAndy Whitcroft			    $dstat !~ /^(?:$Ident|-?$Constant),$/ &&			# 10, // foo(),
3806f74bd194SAndy Whitcroft			    $dstat !~ /^(?:$Ident|-?$Constant);$/ &&			# foo();
38073cc4b1c3SJoe Perches			    $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ &&		# 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
3808356fd398SJoe Perches			    $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ &&			# character constants
3809f74bd194SAndy Whitcroft			    $dstat !~ /$exceptions/ &&
3810f74bd194SAndy Whitcroft			    $dstat !~ /^\.$Ident\s*=/ &&				# .foo =
3811e942e2c3SJoe Perches			    $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ &&		# stringification #foo
381272f115f9SAndy Whitcroft			    $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ &&	# do {...} while (...); // do {...} while (...)
3813f74bd194SAndy Whitcroft			    $dstat !~ /^for\s*$Constant$/ &&				# for (...)
3814f74bd194SAndy Whitcroft			    $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ &&	# for (...) bar()
3815f74bd194SAndy Whitcroft			    $dstat !~ /^do\s*{/ &&					# do {...
3816f95a7e6aSJoe Perches			    $dstat !~ /^\({/ &&						# ({...
3817f95a7e6aSJoe Perches			    $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
3818c45dcabdSAndy Whitcroft			{
3819f74bd194SAndy Whitcroft				$ctx =~ s/\n*$//;
3820f74bd194SAndy Whitcroft				my $herectx = $here . "\n";
3821f74bd194SAndy Whitcroft				my $cnt = statement_rawlines($ctx);
3822f74bd194SAndy Whitcroft
3823f74bd194SAndy Whitcroft				for (my $n = 0; $n < $cnt; $n++) {
3824f74bd194SAndy Whitcroft					$herectx .= raw_line($linenr, $n) . "\n";
3825c45dcabdSAndy Whitcroft				}
3826c45dcabdSAndy Whitcroft
3827f74bd194SAndy Whitcroft				if ($dstat =~ /;/) {
3828f74bd194SAndy Whitcroft					ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
3829f74bd194SAndy Whitcroft					      "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
3830f74bd194SAndy Whitcroft				} else {
3831000d1cc1SJoe Perches					ERROR("COMPLEX_MACRO",
3832f74bd194SAndy Whitcroft					      "Macros with complex values should be enclosed in parenthesis\n" . "$herectx");
3833d8aaf121SAndy Whitcroft				}
38340a920b5bSAndy Whitcroft			}
38355023d347SJoe Perches
3836481eb486SJoe Perches# check for line continuations outside of #defines, preprocessor #, and asm
38375023d347SJoe Perches
38385023d347SJoe Perches		} else {
38395023d347SJoe Perches			if ($prevline !~ /^..*\\$/ &&
3840481eb486SJoe Perches			    $line !~ /^\+\s*\#.*\\$/ &&		# preprocessor
3841481eb486SJoe Perches			    $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ &&	# asm
38425023d347SJoe Perches			    $line =~ /^\+.*\\$/) {
38435023d347SJoe Perches				WARN("LINE_CONTINUATIONS",
38445023d347SJoe Perches				     "Avoid unnecessary line continuations\n" . $herecurr);
38455023d347SJoe Perches			}
3846653d4876SAndy Whitcroft		}
38470a920b5bSAndy Whitcroft
3848b13edf7fSJoe Perches# do {} while (0) macro tests:
3849b13edf7fSJoe Perches# single-statement macros do not need to be enclosed in do while (0) loop,
3850b13edf7fSJoe Perches# macro should not end with a semicolon
3851b13edf7fSJoe Perches		if ($^V && $^V ge 5.10.0 &&
3852b13edf7fSJoe Perches		    $realfile !~ m@/vmlinux.lds.h$@ &&
3853b13edf7fSJoe Perches		    $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
3854b13edf7fSJoe Perches			my $ln = $linenr;
3855b13edf7fSJoe Perches			my $cnt = $realcnt;
3856b13edf7fSJoe Perches			my ($off, $dstat, $dcond, $rest);
3857b13edf7fSJoe Perches			my $ctx = '';
3858b13edf7fSJoe Perches			($dstat, $dcond, $ln, $cnt, $off) =
3859b13edf7fSJoe Perches				ctx_statement_block($linenr, $realcnt, 0);
3860b13edf7fSJoe Perches			$ctx = $dstat;
3861b13edf7fSJoe Perches
3862b13edf7fSJoe Perches			$dstat =~ s/\\\n.//g;
3863b13edf7fSJoe Perches
3864b13edf7fSJoe Perches			if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
3865b13edf7fSJoe Perches				my $stmts = $2;
3866b13edf7fSJoe Perches				my $semis = $3;
3867b13edf7fSJoe Perches
3868b13edf7fSJoe Perches				$ctx =~ s/\n*$//;
3869b13edf7fSJoe Perches				my $cnt = statement_rawlines($ctx);
3870b13edf7fSJoe Perches				my $herectx = $here . "\n";
3871b13edf7fSJoe Perches
3872b13edf7fSJoe Perches				for (my $n = 0; $n < $cnt; $n++) {
3873b13edf7fSJoe Perches					$herectx .= raw_line($linenr, $n) . "\n";
3874b13edf7fSJoe Perches				}
3875b13edf7fSJoe Perches
3876ac8e97f8SJoe Perches				if (($stmts =~ tr/;/;/) == 1 &&
3877ac8e97f8SJoe Perches				    $stmts !~ /^\s*(if|while|for|switch)\b/) {
3878b13edf7fSJoe Perches					WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
3879b13edf7fSJoe Perches					     "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
3880b13edf7fSJoe Perches				}
3881b13edf7fSJoe Perches				if (defined $semis && $semis ne "") {
3882b13edf7fSJoe Perches					WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
3883b13edf7fSJoe Perches					     "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
3884b13edf7fSJoe Perches				}
3885f5ef95b1SJoe Perches			} elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) {
3886f5ef95b1SJoe Perches				$ctx =~ s/\n*$//;
3887f5ef95b1SJoe Perches				my $cnt = statement_rawlines($ctx);
3888f5ef95b1SJoe Perches				my $herectx = $here . "\n";
3889f5ef95b1SJoe Perches
3890f5ef95b1SJoe Perches				for (my $n = 0; $n < $cnt; $n++) {
3891f5ef95b1SJoe Perches					$herectx .= raw_line($linenr, $n) . "\n";
3892f5ef95b1SJoe Perches				}
3893f5ef95b1SJoe Perches
3894f5ef95b1SJoe Perches				WARN("TRAILING_SEMICOLON",
3895f5ef95b1SJoe Perches				     "macros should not use a trailing semicolon\n" . "$herectx");
3896b13edf7fSJoe Perches			}
3897b13edf7fSJoe Perches		}
3898b13edf7fSJoe Perches
3899080ba929SMike Frysinger# make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
3900080ba929SMike Frysinger# all assignments may have only one of the following with an assignment:
3901080ba929SMike Frysinger#	.
3902080ba929SMike Frysinger#	ALIGN(...)
3903080ba929SMike Frysinger#	VMLINUX_SYMBOL(...)
3904080ba929SMike Frysinger		if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
3905000d1cc1SJoe Perches			WARN("MISSING_VMLINUX_SYMBOL",
3906000d1cc1SJoe Perches			     "vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
3907080ba929SMike Frysinger		}
3908080ba929SMike Frysinger
3909f0a594c1SAndy Whitcroft# check for redundant bracing round if etc
391013214adfSAndy Whitcroft		if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
391113214adfSAndy Whitcroft			my ($level, $endln, @chunks) =
3912cf655043SAndy Whitcroft				ctx_statement_full($linenr, $realcnt, 1);
391313214adfSAndy Whitcroft			#print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
3914cf655043SAndy Whitcroft			#print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
3915cf655043SAndy Whitcroft			if ($#chunks > 0 && $level == 0) {
3916aad4f614SJoe Perches				my @allowed = ();
3917aad4f614SJoe Perches				my $allow = 0;
391813214adfSAndy Whitcroft				my $seen = 0;
3919773647a0SAndy Whitcroft				my $herectx = $here . "\n";
3920cf655043SAndy Whitcroft				my $ln = $linenr - 1;
392113214adfSAndy Whitcroft				for my $chunk (@chunks) {
392213214adfSAndy Whitcroft					my ($cond, $block) = @{$chunk};
392313214adfSAndy Whitcroft
3924773647a0SAndy Whitcroft					# If the condition carries leading newlines, then count those as offsets.
3925773647a0SAndy Whitcroft					my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
3926773647a0SAndy Whitcroft					my $offset = statement_rawlines($whitespace) - 1;
3927773647a0SAndy Whitcroft
3928aad4f614SJoe Perches					$allowed[$allow] = 0;
3929773647a0SAndy Whitcroft					#print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
3930773647a0SAndy Whitcroft
3931773647a0SAndy Whitcroft					# We have looked at and allowed this specific line.
3932773647a0SAndy Whitcroft					$suppress_ifbraces{$ln + $offset} = 1;
3933773647a0SAndy Whitcroft
3934773647a0SAndy Whitcroft					$herectx .= "$rawlines[$ln + $offset]\n[...]\n";
3935cf655043SAndy Whitcroft					$ln += statement_rawlines($block) - 1;
3936cf655043SAndy Whitcroft
3937773647a0SAndy Whitcroft					substr($block, 0, length($cond), '');
393813214adfSAndy Whitcroft
393913214adfSAndy Whitcroft					$seen++ if ($block =~ /^\s*{/);
394013214adfSAndy Whitcroft
3941aad4f614SJoe Perches					#print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
3942cf655043SAndy Whitcroft					if (statement_lines($cond) > 1) {
3943cf655043SAndy Whitcroft						#print "APW: ALLOWED: cond<$cond>\n";
3944aad4f614SJoe Perches						$allowed[$allow] = 1;
394513214adfSAndy Whitcroft					}
394613214adfSAndy Whitcroft					if ($block =~/\b(?:if|for|while)\b/) {
3947cf655043SAndy Whitcroft						#print "APW: ALLOWED: block<$block>\n";
3948aad4f614SJoe Perches						$allowed[$allow] = 1;
394913214adfSAndy Whitcroft					}
3950cf655043SAndy Whitcroft					if (statement_block_size($block) > 1) {
3951cf655043SAndy Whitcroft						#print "APW: ALLOWED: lines block<$block>\n";
3952aad4f614SJoe Perches						$allowed[$allow] = 1;
395313214adfSAndy Whitcroft					}
3954aad4f614SJoe Perches					$allow++;
395513214adfSAndy Whitcroft				}
3956aad4f614SJoe Perches				if ($seen) {
3957aad4f614SJoe Perches					my $sum_allowed = 0;
3958aad4f614SJoe Perches					foreach (@allowed) {
3959aad4f614SJoe Perches						$sum_allowed += $_;
3960aad4f614SJoe Perches					}
3961aad4f614SJoe Perches					if ($sum_allowed == 0) {
3962000d1cc1SJoe Perches						WARN("BRACES",
3963000d1cc1SJoe Perches						     "braces {} are not necessary for any arm of this statement\n" . $herectx);
3964aad4f614SJoe Perches					} elsif ($sum_allowed != $allow &&
3965aad4f614SJoe Perches						 $seen != $allow) {
3966aad4f614SJoe Perches						CHK("BRACES",
3967aad4f614SJoe Perches						    "braces {} should be used on all arms of this statement\n" . $herectx);
3968aad4f614SJoe Perches					}
396913214adfSAndy Whitcroft				}
397013214adfSAndy Whitcroft			}
397113214adfSAndy Whitcroft		}
3972773647a0SAndy Whitcroft		if (!defined $suppress_ifbraces{$linenr - 1} &&
397313214adfSAndy Whitcroft					$line =~ /\b(if|while|for|else)\b/) {
3974cf655043SAndy Whitcroft			my $allowed = 0;
3975f0a594c1SAndy Whitcroft
3976cf655043SAndy Whitcroft			# Check the pre-context.
3977cf655043SAndy Whitcroft			if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
3978cf655043SAndy Whitcroft				#print "APW: ALLOWED: pre<$1>\n";
3979cf655043SAndy Whitcroft				$allowed = 1;
3980f0a594c1SAndy Whitcroft			}
3981773647a0SAndy Whitcroft
3982773647a0SAndy Whitcroft			my ($level, $endln, @chunks) =
3983773647a0SAndy Whitcroft				ctx_statement_full($linenr, $realcnt, $-[0]);
3984773647a0SAndy Whitcroft
3985cf655043SAndy Whitcroft			# Check the condition.
3986cf655043SAndy Whitcroft			my ($cond, $block) = @{$chunks[0]};
3987773647a0SAndy Whitcroft			#print "CHECKING<$linenr> cond<$cond> block<$block>\n";
3988cf655043SAndy Whitcroft			if (defined $cond) {
3989773647a0SAndy Whitcroft				substr($block, 0, length($cond), '');
3990cf655043SAndy Whitcroft			}
3991cf655043SAndy Whitcroft			if (statement_lines($cond) > 1) {
3992cf655043SAndy Whitcroft				#print "APW: ALLOWED: cond<$cond>\n";
3993cf655043SAndy Whitcroft				$allowed = 1;
3994cf655043SAndy Whitcroft			}
3995cf655043SAndy Whitcroft			if ($block =~/\b(?:if|for|while)\b/) {
3996cf655043SAndy Whitcroft				#print "APW: ALLOWED: block<$block>\n";
3997cf655043SAndy Whitcroft				$allowed = 1;
3998cf655043SAndy Whitcroft			}
3999cf655043SAndy Whitcroft			if (statement_block_size($block) > 1) {
4000cf655043SAndy Whitcroft				#print "APW: ALLOWED: lines block<$block>\n";
4001cf655043SAndy Whitcroft				$allowed = 1;
4002cf655043SAndy Whitcroft			}
4003cf655043SAndy Whitcroft			# Check the post-context.
4004cf655043SAndy Whitcroft			if (defined $chunks[1]) {
4005cf655043SAndy Whitcroft				my ($cond, $block) = @{$chunks[1]};
4006cf655043SAndy Whitcroft				if (defined $cond) {
4007773647a0SAndy Whitcroft					substr($block, 0, length($cond), '');
4008cf655043SAndy Whitcroft				}
4009cf655043SAndy Whitcroft				if ($block =~ /^\s*\{/) {
4010cf655043SAndy Whitcroft					#print "APW: ALLOWED: chunk-1 block<$block>\n";
4011cf655043SAndy Whitcroft					$allowed = 1;
4012cf655043SAndy Whitcroft				}
4013cf655043SAndy Whitcroft			}
4014cf655043SAndy Whitcroft			if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
401569932487SJustin P. Mattock				my $herectx = $here . "\n";
4016f055663cSAndy Whitcroft				my $cnt = statement_rawlines($block);
4017cf655043SAndy Whitcroft
4018f055663cSAndy Whitcroft				for (my $n = 0; $n < $cnt; $n++) {
401969932487SJustin P. Mattock					$herectx .= raw_line($linenr, $n) . "\n";
4020cf655043SAndy Whitcroft				}
4021cf655043SAndy Whitcroft
4022000d1cc1SJoe Perches				WARN("BRACES",
4023000d1cc1SJoe Perches				     "braces {} are not necessary for single statement blocks\n" . $herectx);
4024f0a594c1SAndy Whitcroft			}
4025f0a594c1SAndy Whitcroft		}
4026f0a594c1SAndy Whitcroft
40270979ae66SJoe Perches# check for unnecessary blank lines around braces
402877b9a53aSJoe Perches		if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
40290979ae66SJoe Perches			CHK("BRACES",
40300979ae66SJoe Perches			    "Blank lines aren't necessary before a close brace '}'\n" . $hereprev);
40310979ae66SJoe Perches		}
403277b9a53aSJoe Perches		if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
40330979ae66SJoe Perches			CHK("BRACES",
40340979ae66SJoe Perches			    "Blank lines aren't necessary after an open brace '{'\n" . $hereprev);
40350979ae66SJoe Perches		}
40360979ae66SJoe Perches
40374a0df2efSAndy Whitcroft# no volatiles please
40386c72ffaaSAndy Whitcroft		my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
40396c72ffaaSAndy Whitcroft		if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
4040000d1cc1SJoe Perches			WARN("VOLATILE",
4041000d1cc1SJoe Perches			     "Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
40424a0df2efSAndy Whitcroft		}
40434a0df2efSAndy Whitcroft
404400df344fSAndy Whitcroft# warn about #if 0
4045c45dcabdSAndy Whitcroft		if ($line =~ /^.\s*\#\s*if\s+0\b/) {
4046000d1cc1SJoe Perches			CHK("REDUNDANT_CODE",
4047000d1cc1SJoe Perches			    "if this code is redundant consider removing it\n" .
4048de7d4f0eSAndy Whitcroft				$herecurr);
40494a0df2efSAndy Whitcroft		}
40504a0df2efSAndy Whitcroft
405103df4b51SAndy Whitcroft# check for needless "if (<foo>) fn(<foo>)" uses
405203df4b51SAndy Whitcroft		if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
405303df4b51SAndy Whitcroft			my $expr = '\s*\(\s*' . quotemeta($1) . '\s*\)\s*;';
405403df4b51SAndy Whitcroft			if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?)$expr/) {
405503df4b51SAndy Whitcroft				WARN('NEEDLESS_IF',
405603df4b51SAndy Whitcroft				     "$1(NULL) is safe this check is probably not required\n" . $hereprev);
40574c432a8fSGreg Kroah-Hartman			}
40584c432a8fSGreg Kroah-Hartman		}
4059f0a594c1SAndy Whitcroft
4060ebfdc409SJoe Perches# check for unnecessary "Out of Memory" messages
4061ebfdc409SJoe Perches		if ($line =~ /^\+.*\b$logFunctions\s*\(/ &&
4062ebfdc409SJoe Perches		    $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ &&
4063ebfdc409SJoe Perches		    (defined $1 || defined $3) &&
4064ebfdc409SJoe Perches		    $linenr > 3) {
4065ebfdc409SJoe Perches			my $testval = $2;
4066ebfdc409SJoe Perches			my $testline = $lines[$linenr - 3];
4067ebfdc409SJoe Perches
4068ebfdc409SJoe Perches			my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0);
4069ebfdc409SJoe Perches#			print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n");
4070ebfdc409SJoe Perches
4071ebfdc409SJoe Perches			if ($c =~ /(?:^|\n)[ \+]\s*(?:$Type\s*)?\Q$testval\E\s*=\s*(?:\([^\)]*\)\s*)?\s*(?:devm_)?(?:[kv][czm]alloc(?:_node|_array)?\b|kstrdup|(?:dev_)?alloc_skb)/) {
4072ebfdc409SJoe Perches				WARN("OOM_MESSAGE",
4073ebfdc409SJoe Perches				     "Possible unnecessary 'out of memory' message\n" . $hereprev);
4074ebfdc409SJoe Perches			}
4075ebfdc409SJoe Perches		}
4076ebfdc409SJoe Perches
40778716de38SJoe Perches# check for bad placement of section $InitAttribute (e.g.: __initdata)
40788716de38SJoe Perches		if ($line =~ /(\b$InitAttribute\b)/) {
40798716de38SJoe Perches			my $attr = $1;
40808716de38SJoe Perches			if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
40818716de38SJoe Perches				my $ptr = $1;
40828716de38SJoe Perches				my $var = $2;
40838716de38SJoe Perches				if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
40848716de38SJoe Perches				      ERROR("MISPLACED_INIT",
40858716de38SJoe Perches					    "$attr should be placed after $var\n" . $herecurr)) ||
40868716de38SJoe Perches				     ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
40878716de38SJoe Perches				      WARN("MISPLACED_INIT",
40888716de38SJoe Perches					   "$attr should be placed after $var\n" . $herecurr))) &&
40898716de38SJoe Perches				    $fix) {
40908716de38SJoe 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;
40918716de38SJoe Perches				}
40928716de38SJoe Perches			}
40938716de38SJoe Perches		}
40948716de38SJoe Perches
4095e970b884SJoe Perches# check for $InitAttributeData (ie: __initdata) with const
4096e970b884SJoe Perches		if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
4097e970b884SJoe Perches			my $attr = $1;
4098e970b884SJoe Perches			$attr =~ /($InitAttributePrefix)(.*)/;
4099e970b884SJoe Perches			my $attr_prefix = $1;
4100e970b884SJoe Perches			my $attr_type = $2;
4101e970b884SJoe Perches			if (ERROR("INIT_ATTRIBUTE",
4102e970b884SJoe Perches				  "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
4103e970b884SJoe Perches			    $fix) {
4104e970b884SJoe Perches				$fixed[$linenr - 1] =~
4105e970b884SJoe Perches				    s/$InitAttributeData/${attr_prefix}initconst/;
4106e970b884SJoe Perches			}
4107e970b884SJoe Perches		}
4108e970b884SJoe Perches
4109e970b884SJoe Perches# check for $InitAttributeConst (ie: __initconst) without const
4110e970b884SJoe Perches		if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
4111e970b884SJoe Perches			my $attr = $1;
4112e970b884SJoe Perches			if (ERROR("INIT_ATTRIBUTE",
4113e970b884SJoe Perches				  "Use of $attr requires a separate use of const\n" . $herecurr) &&
4114e970b884SJoe Perches			    $fix) {
4115e970b884SJoe Perches				my $lead = $fixed[$linenr - 1] =~
4116e970b884SJoe Perches				    /(^\+\s*(?:static\s+))/;
4117e970b884SJoe Perches				$lead = rtrim($1);
4118e970b884SJoe Perches				$lead = "$lead " if ($lead !~ /^\+$/);
4119e970b884SJoe Perches				$lead = "${lead}const ";
4120e970b884SJoe Perches				$fixed[$linenr - 1] =~ s/(^\+\s*(?:static\s+))/$lead/;
4121e970b884SJoe Perches			}
4122e970b884SJoe Perches		}
4123e970b884SJoe Perches
4124fbdb8138SJoe Perches# don't use __constant_<foo> functions outside of include/uapi/
4125fbdb8138SJoe Perches		if ($realfile !~ m@^include/uapi/@ &&
4126fbdb8138SJoe Perches		    $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) {
4127fbdb8138SJoe Perches			my $constant_func = $1;
4128fbdb8138SJoe Perches			my $func = $constant_func;
4129fbdb8138SJoe Perches			$func =~ s/^__constant_//;
4130fbdb8138SJoe Perches			if (WARN("CONSTANT_CONVERSION",
4131fbdb8138SJoe Perches				 "$constant_func should be $func\n" . $herecurr) &&
4132fbdb8138SJoe Perches			    $fix) {
4133fbdb8138SJoe Perches				$fixed[$linenr - 1] =~ s/\b$constant_func\b/$func/g;
4134fbdb8138SJoe Perches			}
4135fbdb8138SJoe Perches		}
4136fbdb8138SJoe Perches
41371a15a250SPatrick Pannuto# prefer usleep_range over udelay
413837581c28SBruce Allan		if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
413943c1d77cSJoe Perches			my $delay = $1;
41401a15a250SPatrick Pannuto			# ignore udelay's < 10, however
414143c1d77cSJoe Perches			if (! ($delay < 10) ) {
4142000d1cc1SJoe Perches				CHK("USLEEP_RANGE",
414343c1d77cSJoe Perches				    "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $herecurr);
414443c1d77cSJoe Perches			}
414543c1d77cSJoe Perches			if ($delay > 2000) {
414643c1d77cSJoe Perches				WARN("LONG_UDELAY",
414743c1d77cSJoe Perches				     "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr);
41481a15a250SPatrick Pannuto			}
41491a15a250SPatrick Pannuto		}
41501a15a250SPatrick Pannuto
415109ef8725SPatrick Pannuto# warn about unexpectedly long msleep's
415209ef8725SPatrick Pannuto		if ($line =~ /\bmsleep\s*\((\d+)\);/) {
415309ef8725SPatrick Pannuto			if ($1 < 20) {
4154000d1cc1SJoe Perches				WARN("MSLEEP",
415543c1d77cSJoe Perches				     "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $herecurr);
415609ef8725SPatrick Pannuto			}
415709ef8725SPatrick Pannuto		}
415809ef8725SPatrick Pannuto
415936ec1939SJoe Perches# check for comparisons of jiffies
416036ec1939SJoe Perches		if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
416136ec1939SJoe Perches			WARN("JIFFIES_COMPARISON",
416236ec1939SJoe Perches			     "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
416336ec1939SJoe Perches		}
416436ec1939SJoe Perches
41659d7a34a5SJoe Perches# check for comparisons of get_jiffies_64()
41669d7a34a5SJoe Perches		if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
41679d7a34a5SJoe Perches			WARN("JIFFIES_COMPARISON",
41689d7a34a5SJoe Perches			     "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
41699d7a34a5SJoe Perches		}
41709d7a34a5SJoe Perches
417100df344fSAndy Whitcroft# warn about #ifdefs in C files
4172c45dcabdSAndy Whitcroft#		if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
417300df344fSAndy Whitcroft#			print "#ifdef in C files should be avoided\n";
417400df344fSAndy Whitcroft#			print "$herecurr";
417500df344fSAndy Whitcroft#			$clean = 0;
417600df344fSAndy Whitcroft#		}
417700df344fSAndy Whitcroft
417822f2a2efSAndy Whitcroft# warn about spacing in #ifdefs
4179c45dcabdSAndy Whitcroft		if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
41803705ce5bSJoe Perches			if (ERROR("SPACING",
41813705ce5bSJoe Perches				  "exactly one space required after that #$1\n" . $herecurr) &&
41823705ce5bSJoe Perches			    $fix) {
41833705ce5bSJoe Perches				$fixed[$linenr - 1] =~
41843705ce5bSJoe Perches				    s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
41853705ce5bSJoe Perches			}
41863705ce5bSJoe Perches
418722f2a2efSAndy Whitcroft		}
418822f2a2efSAndy Whitcroft
41894a0df2efSAndy Whitcroft# check for spinlock_t definitions without a comment.
4190171ae1a4SAndy Whitcroft		if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
4191171ae1a4SAndy Whitcroft		    $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
41924a0df2efSAndy Whitcroft			my $which = $1;
41934a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
4194000d1cc1SJoe Perches				CHK("UNCOMMENTED_DEFINITION",
4195000d1cc1SJoe Perches				    "$1 definition without comment\n" . $herecurr);
41964a0df2efSAndy Whitcroft			}
41974a0df2efSAndy Whitcroft		}
41984a0df2efSAndy Whitcroft# check for memory barriers without a comment.
41994a0df2efSAndy Whitcroft		if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
42004a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
4201c1fd7bb9SJoe Perches				WARN("MEMORY_BARRIER",
4202000d1cc1SJoe Perches				     "memory barrier without comment\n" . $herecurr);
42034a0df2efSAndy Whitcroft			}
42044a0df2efSAndy Whitcroft		}
42054a0df2efSAndy Whitcroft# check of hardware specific defines
4206c45dcabdSAndy Whitcroft		if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
4207000d1cc1SJoe Perches			CHK("ARCH_DEFINES",
4208000d1cc1SJoe Perches			    "architecture specific defines should be avoided\n" .  $herecurr);
42090a920b5bSAndy Whitcroft		}
4210653d4876SAndy Whitcroft
4211d4977c78STobias Klauser# Check that the storage class is at the beginning of a declaration
4212d4977c78STobias Klauser		if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
4213000d1cc1SJoe Perches			WARN("STORAGE_CLASS",
4214000d1cc1SJoe Perches			     "storage class should be at the beginning of the declaration\n" . $herecurr)
4215d4977c78STobias Klauser		}
4216d4977c78STobias Klauser
4217de7d4f0eSAndy Whitcroft# check the location of the inline attribute, that it is between
4218de7d4f0eSAndy Whitcroft# storage class and type.
42199c0ca6f9SAndy Whitcroft		if ($line =~ /\b$Type\s+$Inline\b/ ||
42209c0ca6f9SAndy Whitcroft		    $line =~ /\b$Inline\s+$Storage\b/) {
4221000d1cc1SJoe Perches			ERROR("INLINE_LOCATION",
4222000d1cc1SJoe Perches			      "inline keyword should sit between storage class and type\n" . $herecurr);
4223de7d4f0eSAndy Whitcroft		}
4224de7d4f0eSAndy Whitcroft
42258905a67cSAndy Whitcroft# Check for __inline__ and __inline, prefer inline
42262b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
42272b7ab453SJoe Perches		    $line =~ /\b(__inline__|__inline)\b/) {
4228d5e616fcSJoe Perches			if (WARN("INLINE",
4229d5e616fcSJoe Perches				 "plain inline is preferred over $1\n" . $herecurr) &&
4230d5e616fcSJoe Perches			    $fix) {
4231d5e616fcSJoe Perches				$fixed[$linenr - 1] =~ s/\b(__inline__|__inline)\b/inline/;
4232d5e616fcSJoe Perches
4233d5e616fcSJoe Perches			}
42348905a67cSAndy Whitcroft		}
42358905a67cSAndy Whitcroft
42363d130fd0SJoe Perches# Check for __attribute__ packed, prefer __packed
42372b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
42382b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
4239000d1cc1SJoe Perches			WARN("PREFER_PACKED",
4240000d1cc1SJoe Perches			     "__packed is preferred over __attribute__((packed))\n" . $herecurr);
42413d130fd0SJoe Perches		}
42423d130fd0SJoe Perches
424339b7e287SJoe Perches# Check for __attribute__ aligned, prefer __aligned
42442b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
42452b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
4246000d1cc1SJoe Perches			WARN("PREFER_ALIGNED",
4247000d1cc1SJoe Perches			     "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
424839b7e287SJoe Perches		}
424939b7e287SJoe Perches
42505f14d3bdSJoe Perches# Check for __attribute__ format(printf, prefer __printf
42512b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
42522b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
4253d5e616fcSJoe Perches			if (WARN("PREFER_PRINTF",
4254d5e616fcSJoe Perches				 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) &&
4255d5e616fcSJoe Perches			    $fix) {
4256d5e616fcSJoe Perches				$fixed[$linenr - 1] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf\s*,\s*(.*)\)\s*\)\s*\)/"__printf(" . trim($1) . ")"/ex;
4257d5e616fcSJoe Perches
4258d5e616fcSJoe Perches			}
42595f14d3bdSJoe Perches		}
42605f14d3bdSJoe Perches
42616061d949SJoe Perches# Check for __attribute__ format(scanf, prefer __scanf
42622b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
42632b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
4264d5e616fcSJoe Perches			if (WARN("PREFER_SCANF",
4265d5e616fcSJoe Perches				 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) &&
4266d5e616fcSJoe Perches			    $fix) {
4267d5e616fcSJoe Perches				$fixed[$linenr - 1] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\s*,\s*(.*)\)\s*\)\s*\)/"__scanf(" . trim($1) . ")"/ex;
4268d5e616fcSJoe Perches			}
42696061d949SJoe Perches		}
42706061d949SJoe Perches
42718f53a9b8SJoe Perches# check for sizeof(&)
42728f53a9b8SJoe Perches		if ($line =~ /\bsizeof\s*\(\s*\&/) {
4273000d1cc1SJoe Perches			WARN("SIZEOF_ADDRESS",
4274000d1cc1SJoe Perches			     "sizeof(& should be avoided\n" . $herecurr);
42758f53a9b8SJoe Perches		}
42768f53a9b8SJoe Perches
427766c80b60SJoe Perches# check for sizeof without parenthesis
427866c80b60SJoe Perches		if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
4279d5e616fcSJoe Perches			if (WARN("SIZEOF_PARENTHESIS",
4280d5e616fcSJoe Perches				 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
4281d5e616fcSJoe Perches			    $fix) {
4282d5e616fcSJoe Perches				$fixed[$linenr - 1] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
4283d5e616fcSJoe Perches			}
428466c80b60SJoe Perches		}
428566c80b60SJoe Perches
4286428e2fdcSJoe Perches# check for line continuations in quoted strings with odd counts of "
4287428e2fdcSJoe Perches		if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) {
4288000d1cc1SJoe Perches			WARN("LINE_CONTINUATIONS",
4289000d1cc1SJoe Perches			     "Avoid line continuations in quoted strings\n" . $herecurr);
4290428e2fdcSJoe Perches		}
4291428e2fdcSJoe Perches
429288982feaSJoe Perches# check for struct spinlock declarations
429388982feaSJoe Perches		if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
429488982feaSJoe Perches			WARN("USE_SPINLOCK_T",
429588982feaSJoe Perches			     "struct spinlock should be spinlock_t\n" . $herecurr);
429688982feaSJoe Perches		}
429788982feaSJoe Perches
4298a6962d72SJoe Perches# check for seq_printf uses that could be seq_puts
429906668727SJoe Perches		if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
4300a6962d72SJoe Perches			my $fmt = get_quoted_string($line, $rawline);
430106668727SJoe Perches			if ($fmt ne "" && $fmt !~ /[^\\]\%/) {
4302d5e616fcSJoe Perches				if (WARN("PREFER_SEQ_PUTS",
4303d5e616fcSJoe Perches					 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
4304d5e616fcSJoe Perches				    $fix) {
4305d5e616fcSJoe Perches					$fixed[$linenr - 1] =~ s/\bseq_printf\b/seq_puts/;
4306d5e616fcSJoe Perches				}
4307a6962d72SJoe Perches			}
4308a6962d72SJoe Perches		}
4309a6962d72SJoe Perches
4310554e165cSAndy Whitcroft# Check for misused memsets
4311d1fe9c09SJoe Perches		if ($^V && $^V ge 5.10.0 &&
4312d1fe9c09SJoe Perches		    defined $stat &&
4313d7c76ba7SJoe Perches		    $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/s) {
4314554e165cSAndy Whitcroft
4315d7c76ba7SJoe Perches			my $ms_addr = $2;
4316d1fe9c09SJoe Perches			my $ms_val = $7;
4317d1fe9c09SJoe Perches			my $ms_size = $12;
4318d7c76ba7SJoe Perches
4319554e165cSAndy Whitcroft			if ($ms_size =~ /^(0x|)0$/i) {
4320554e165cSAndy Whitcroft				ERROR("MEMSET",
4321d7c76ba7SJoe Perches				      "memset to 0's uses 0 as the 2nd argument, not the 3rd\n" . "$here\n$stat\n");
4322554e165cSAndy Whitcroft			} elsif ($ms_size =~ /^(0x|)1$/i) {
4323554e165cSAndy Whitcroft				WARN("MEMSET",
4324d7c76ba7SJoe Perches				     "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
4325d7c76ba7SJoe Perches			}
4326d7c76ba7SJoe Perches		}
4327d7c76ba7SJoe Perches
432898a9bba5SJoe Perches# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar)
432998a9bba5SJoe Perches		if ($^V && $^V ge 5.10.0 &&
433098a9bba5SJoe Perches		    $line =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/s) {
433198a9bba5SJoe Perches			if (WARN("PREFER_ETHER_ADDR_COPY",
433298a9bba5SJoe Perches				 "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . $herecurr) &&
433398a9bba5SJoe Perches			    $fix) {
433498a9bba5SJoe Perches				$fixed[$linenr - 1] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/;
433598a9bba5SJoe Perches			}
433698a9bba5SJoe Perches		}
433798a9bba5SJoe Perches
4338d7c76ba7SJoe Perches# typecasts on min/max could be min_t/max_t
4339d1fe9c09SJoe Perches		if ($^V && $^V ge 5.10.0 &&
4340d1fe9c09SJoe Perches		    defined $stat &&
4341d7c76ba7SJoe Perches		    $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
4342d1fe9c09SJoe Perches			if (defined $2 || defined $7) {
4343d7c76ba7SJoe Perches				my $call = $1;
4344d7c76ba7SJoe Perches				my $cast1 = deparenthesize($2);
4345d7c76ba7SJoe Perches				my $arg1 = $3;
4346d1fe9c09SJoe Perches				my $cast2 = deparenthesize($7);
4347d1fe9c09SJoe Perches				my $arg2 = $8;
4348d7c76ba7SJoe Perches				my $cast;
4349d7c76ba7SJoe Perches
4350d1fe9c09SJoe Perches				if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
4351d7c76ba7SJoe Perches					$cast = "$cast1 or $cast2";
4352d7c76ba7SJoe Perches				} elsif ($cast1 ne "") {
4353d7c76ba7SJoe Perches					$cast = $cast1;
4354d7c76ba7SJoe Perches				} else {
4355d7c76ba7SJoe Perches					$cast = $cast2;
4356d7c76ba7SJoe Perches				}
4357d7c76ba7SJoe Perches				WARN("MINMAX",
4358d7c76ba7SJoe Perches				     "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
4359554e165cSAndy Whitcroft			}
4360554e165cSAndy Whitcroft		}
4361554e165cSAndy Whitcroft
43624a273195SJoe Perches# check usleep_range arguments
43634a273195SJoe Perches		if ($^V && $^V ge 5.10.0 &&
43644a273195SJoe Perches		    defined $stat &&
43654a273195SJoe Perches		    $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
43664a273195SJoe Perches			my $min = $1;
43674a273195SJoe Perches			my $max = $7;
43684a273195SJoe Perches			if ($min eq $max) {
43694a273195SJoe Perches				WARN("USLEEP_RANGE",
43704a273195SJoe Perches				     "usleep_range should not use min == max args; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
43714a273195SJoe Perches			} elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
43724a273195SJoe Perches				 $min > $max) {
43734a273195SJoe Perches				WARN("USLEEP_RANGE",
43744a273195SJoe Perches				     "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
43754a273195SJoe Perches			}
43764a273195SJoe Perches		}
43774a273195SJoe Perches
4378823b794cSJoe Perches# check for naked sscanf
4379823b794cSJoe Perches		if ($^V && $^V ge 5.10.0 &&
4380823b794cSJoe Perches		    defined $stat &&
43816c8bd707SJoe Perches		    $line =~ /\bsscanf\b/ &&
4382823b794cSJoe Perches		    ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
4383823b794cSJoe Perches		     $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ &&
4384823b794cSJoe Perches		     $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) {
4385823b794cSJoe Perches			my $lc = $stat =~ tr@\n@@;
4386823b794cSJoe Perches			$lc = $lc + $linenr;
4387823b794cSJoe Perches			my $stat_real = raw_line($linenr, 0);
4388823b794cSJoe Perches		        for (my $count = $linenr + 1; $count <= $lc; $count++) {
4389823b794cSJoe Perches				$stat_real = $stat_real . "\n" . raw_line($count, 0);
4390823b794cSJoe Perches			}
4391823b794cSJoe Perches			WARN("NAKED_SSCANF",
4392823b794cSJoe Perches			     "unchecked sscanf return value\n" . "$here\n$stat_real\n");
4393823b794cSJoe Perches		}
4394823b794cSJoe Perches
4395afc819abSJoe Perches# check for simple sscanf that should be kstrto<foo>
4396afc819abSJoe Perches		if ($^V && $^V ge 5.10.0 &&
4397afc819abSJoe Perches		    defined $stat &&
4398afc819abSJoe Perches		    $line =~ /\bsscanf\b/) {
4399afc819abSJoe Perches			my $lc = $stat =~ tr@\n@@;
4400afc819abSJoe Perches			$lc = $lc + $linenr;
4401afc819abSJoe Perches			my $stat_real = raw_line($linenr, 0);
4402afc819abSJoe Perches		        for (my $count = $linenr + 1; $count <= $lc; $count++) {
4403afc819abSJoe Perches				$stat_real = $stat_real . "\n" . raw_line($count, 0);
4404afc819abSJoe Perches			}
4405afc819abSJoe Perches			if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) {
4406afc819abSJoe Perches				my $format = $6;
4407afc819abSJoe Perches				my $count = $format =~ tr@%@%@;
4408afc819abSJoe Perches				if ($count == 1 &&
4409afc819abSJoe Perches				    $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) {
4410afc819abSJoe Perches					WARN("SSCANF_TO_KSTRTO",
4411afc819abSJoe Perches					     "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n");
4412afc819abSJoe Perches				}
4413afc819abSJoe Perches			}
4414afc819abSJoe Perches		}
4415afc819abSJoe Perches
441670dc8a48SJoe Perches# check for new externs in .h files.
441770dc8a48SJoe Perches		if ($realfile =~ /\.h$/ &&
441870dc8a48SJoe Perches		    $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
4419d1d85780SJoe Perches			if (CHK("AVOID_EXTERNS",
442070dc8a48SJoe Perches				"extern prototypes should be avoided in .h files\n" . $herecurr) &&
442170dc8a48SJoe Perches			    $fix) {
442270dc8a48SJoe Perches				$fixed[$linenr - 1] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
442370dc8a48SJoe Perches			}
442470dc8a48SJoe Perches		}
442570dc8a48SJoe Perches
4426de7d4f0eSAndy Whitcroft# check for new externs in .c files.
4427171ae1a4SAndy Whitcroft		if ($realfile =~ /\.c$/ && defined $stat &&
4428c45dcabdSAndy Whitcroft		    $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
4429171ae1a4SAndy Whitcroft		{
4430c45dcabdSAndy Whitcroft			my $function_name = $1;
4431c45dcabdSAndy Whitcroft			my $paren_space = $2;
4432171ae1a4SAndy Whitcroft
4433171ae1a4SAndy Whitcroft			my $s = $stat;
4434171ae1a4SAndy Whitcroft			if (defined $cond) {
4435171ae1a4SAndy Whitcroft				substr($s, 0, length($cond), '');
4436171ae1a4SAndy Whitcroft			}
4437c45dcabdSAndy Whitcroft			if ($s =~ /^\s*;/ &&
4438c45dcabdSAndy Whitcroft			    $function_name ne 'uninitialized_var')
4439c45dcabdSAndy Whitcroft			{
4440000d1cc1SJoe Perches				WARN("AVOID_EXTERNS",
4441000d1cc1SJoe Perches				     "externs should be avoided in .c files\n" .  $herecurr);
4442de7d4f0eSAndy Whitcroft			}
4443de7d4f0eSAndy Whitcroft
4444171ae1a4SAndy Whitcroft			if ($paren_space =~ /\n/) {
4445000d1cc1SJoe Perches				WARN("FUNCTION_ARGUMENTS",
4446000d1cc1SJoe Perches				     "arguments for function declarations should follow identifier\n" . $herecurr);
4447171ae1a4SAndy Whitcroft			}
44489c9ba34eSAndy Whitcroft
44499c9ba34eSAndy Whitcroft		} elsif ($realfile =~ /\.c$/ && defined $stat &&
44509c9ba34eSAndy Whitcroft		    $stat =~ /^.\s*extern\s+/)
44519c9ba34eSAndy Whitcroft		{
4452000d1cc1SJoe Perches			WARN("AVOID_EXTERNS",
4453000d1cc1SJoe Perches			     "externs should be avoided in .c files\n" .  $herecurr);
4454171ae1a4SAndy Whitcroft		}
4455171ae1a4SAndy Whitcroft
4456de7d4f0eSAndy Whitcroft# checks for new __setup's
4457de7d4f0eSAndy Whitcroft		if ($rawline =~ /\b__setup\("([^"]*)"/) {
4458de7d4f0eSAndy Whitcroft			my $name = $1;
4459de7d4f0eSAndy Whitcroft
4460de7d4f0eSAndy Whitcroft			if (!grep(/$name/, @setup_docs)) {
4461000d1cc1SJoe Perches				CHK("UNDOCUMENTED_SETUP",
4462000d1cc1SJoe Perches				    "__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
4463de7d4f0eSAndy Whitcroft			}
4464653d4876SAndy Whitcroft		}
44659c0ca6f9SAndy Whitcroft
44669c0ca6f9SAndy Whitcroft# check for pointless casting of kmalloc return
4467caf2a54fSJoe Perches		if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) {
4468000d1cc1SJoe Perches			WARN("UNNECESSARY_CASTS",
4469000d1cc1SJoe Perches			     "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
44709c0ca6f9SAndy Whitcroft		}
447113214adfSAndy Whitcroft
4472a640d25cSJoe Perches# alloc style
4473a640d25cSJoe Perches# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
4474a640d25cSJoe Perches		if ($^V && $^V ge 5.10.0 &&
4475a640d25cSJoe Perches		    $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*([kv][mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
4476a640d25cSJoe Perches			CHK("ALLOC_SIZEOF_STRUCT",
4477a640d25cSJoe Perches			    "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
4478a640d25cSJoe Perches		}
4479a640d25cSJoe Perches
448060a55369SJoe Perches# check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc
448160a55369SJoe Perches		if ($^V && $^V ge 5.10.0 &&
448260a55369SJoe Perches		    $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*(k[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)/) {
448360a55369SJoe Perches			my $oldfunc = $3;
448460a55369SJoe Perches			my $a1 = $4;
448560a55369SJoe Perches			my $a2 = $10;
448660a55369SJoe Perches			my $newfunc = "kmalloc_array";
448760a55369SJoe Perches			$newfunc = "kcalloc" if ($oldfunc eq "kzalloc");
448860a55369SJoe Perches			if ($a1 =~ /^sizeof\s*\S/ || $a2 =~ /^sizeof\s*\S/) {
448960a55369SJoe Perches				if (WARN("ALLOC_WITH_MULTIPLY",
449060a55369SJoe Perches					 "Prefer $newfunc over $oldfunc with multiply\n" . $herecurr) &&
449160a55369SJoe Perches				    $fix) {
449260a55369SJoe Perches					my $r1 = $a1;
449360a55369SJoe Perches					my $r2 = $a2;
449460a55369SJoe Perches					if ($a1 =~ /^sizeof\s*\S/) {
449560a55369SJoe Perches						$r1 = $a2;
449660a55369SJoe Perches						$r2 = $a1;
449760a55369SJoe Perches					}
449860a55369SJoe Perches					$fixed[$linenr - 1] =~ s/\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*(k[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)/$1 . ' = ' . "$newfunc(" . trim($r1) . ', ' . trim($r2)/e;
449960a55369SJoe Perches
450060a55369SJoe Perches				}
450160a55369SJoe Perches			}
450260a55369SJoe Perches		}
450360a55369SJoe Perches
4504972fdea2SJoe Perches# check for krealloc arg reuse
4505972fdea2SJoe Perches		if ($^V && $^V ge 5.10.0 &&
4506972fdea2SJoe Perches		    $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*\1\s*,/) {
4507972fdea2SJoe Perches			WARN("KREALLOC_ARG_REUSE",
4508972fdea2SJoe Perches			     "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
4509972fdea2SJoe Perches		}
4510972fdea2SJoe Perches
45115ce59ae0SJoe Perches# check for alloc argument mismatch
45125ce59ae0SJoe Perches		if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
45135ce59ae0SJoe Perches			WARN("ALLOC_ARRAY_ARGS",
45145ce59ae0SJoe Perches			     "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
45155ce59ae0SJoe Perches		}
45165ce59ae0SJoe Perches
4517caf2a54fSJoe Perches# check for multiple semicolons
4518caf2a54fSJoe Perches		if ($line =~ /;\s*;\s*$/) {
4519d5e616fcSJoe Perches			if (WARN("ONE_SEMICOLON",
4520d5e616fcSJoe Perches				 "Statements terminations use 1 semicolon\n" . $herecurr) &&
4521d5e616fcSJoe Perches			    $fix) {
4522d5e616fcSJoe Perches				$fixed[$linenr - 1] =~ s/(\s*;\s*){2,}$/;/g;
4523d5e616fcSJoe Perches			}
4524d1e2ad07SJoe Perches		}
4525d1e2ad07SJoe Perches
4526c34c09a8SJoe Perches# check for case / default statements not preceeded by break/fallthrough/switch
4527c34c09a8SJoe Perches		if ($line =~ /^.\s*(?:case\s+(?:$Ident|$Constant)\s*|default):/) {
4528c34c09a8SJoe Perches			my $has_break = 0;
4529c34c09a8SJoe Perches			my $has_statement = 0;
4530c34c09a8SJoe Perches			my $count = 0;
4531c34c09a8SJoe Perches			my $prevline = $linenr;
4532c34c09a8SJoe Perches			while ($prevline > 1 && $count < 3 && !$has_break) {
4533c34c09a8SJoe Perches				$prevline--;
4534c34c09a8SJoe Perches				my $rline = $rawlines[$prevline - 1];
4535c34c09a8SJoe Perches				my $fline = $lines[$prevline - 1];
4536c34c09a8SJoe Perches				last if ($fline =~ /^\@\@/);
4537c34c09a8SJoe Perches				next if ($fline =~ /^\-/);
4538c34c09a8SJoe Perches				next if ($fline =~ /^.(?:\s*(?:case\s+(?:$Ident|$Constant)[\s$;]*|default):[\s$;]*)*$/);
4539c34c09a8SJoe Perches				$has_break = 1 if ($rline =~ /fall[\s_-]*(through|thru)/i);
4540c34c09a8SJoe Perches				next if ($fline =~ /^.[\s$;]*$/);
4541c34c09a8SJoe Perches				$has_statement = 1;
4542c34c09a8SJoe Perches				$count++;
4543c34c09a8SJoe Perches				$has_break = 1 if ($fline =~ /\bswitch\b|\b(?:break\s*;[\s$;]*$|return\b|goto\b|continue\b)/);
4544c34c09a8SJoe Perches			}
4545c34c09a8SJoe Perches			if (!$has_break && $has_statement) {
4546c34c09a8SJoe Perches				WARN("MISSING_BREAK",
4547c34c09a8SJoe Perches				     "Possible switch case/default not preceeded by break or fallthrough comment\n" . $herecurr);
4548c34c09a8SJoe Perches			}
4549c34c09a8SJoe Perches		}
4550c34c09a8SJoe Perches
4551d1e2ad07SJoe Perches# check for switch/default statements without a break;
4552d1e2ad07SJoe Perches		if ($^V && $^V ge 5.10.0 &&
4553d1e2ad07SJoe Perches		    defined $stat &&
4554d1e2ad07SJoe Perches		    $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
4555d1e2ad07SJoe Perches			my $ctx = '';
4556d1e2ad07SJoe Perches			my $herectx = $here . "\n";
4557d1e2ad07SJoe Perches			my $cnt = statement_rawlines($stat);
4558d1e2ad07SJoe Perches			for (my $n = 0; $n < $cnt; $n++) {
4559d1e2ad07SJoe Perches				$herectx .= raw_line($linenr, $n) . "\n";
4560d1e2ad07SJoe Perches			}
4561d1e2ad07SJoe Perches			WARN("DEFAULT_NO_BREAK",
4562d1e2ad07SJoe Perches			     "switch default: should use break\n" . $herectx);
4563caf2a54fSJoe Perches		}
4564caf2a54fSJoe Perches
456513214adfSAndy Whitcroft# check for gcc specific __FUNCTION__
4566d5e616fcSJoe Perches		if ($line =~ /\b__FUNCTION__\b/) {
4567d5e616fcSJoe Perches			if (WARN("USE_FUNC",
4568d5e616fcSJoe Perches				 "__func__ should be used instead of gcc specific __FUNCTION__\n"  . $herecurr) &&
4569d5e616fcSJoe Perches			    $fix) {
4570d5e616fcSJoe Perches				$fixed[$linenr - 1] =~ s/\b__FUNCTION__\b/__func__/g;
4571d5e616fcSJoe Perches			}
457213214adfSAndy Whitcroft		}
4573773647a0SAndy Whitcroft
45742c92488aSJoe Perches# check for use of yield()
45752c92488aSJoe Perches		if ($line =~ /\byield\s*\(\s*\)/) {
45762c92488aSJoe Perches			WARN("YIELD",
45772c92488aSJoe Perches			     "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n"  . $herecurr);
45782c92488aSJoe Perches		}
45792c92488aSJoe Perches
4580179f8f40SJoe Perches# check for comparisons against true and false
4581179f8f40SJoe Perches		if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
4582179f8f40SJoe Perches			my $lead = $1;
4583179f8f40SJoe Perches			my $arg = $2;
4584179f8f40SJoe Perches			my $test = $3;
4585179f8f40SJoe Perches			my $otype = $4;
4586179f8f40SJoe Perches			my $trail = $5;
4587179f8f40SJoe Perches			my $op = "!";
4588179f8f40SJoe Perches
4589179f8f40SJoe Perches			($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
4590179f8f40SJoe Perches
4591179f8f40SJoe Perches			my $type = lc($otype);
4592179f8f40SJoe Perches			if ($type =~ /^(?:true|false)$/) {
4593179f8f40SJoe Perches				if (("$test" eq "==" && "$type" eq "true") ||
4594179f8f40SJoe Perches				    ("$test" eq "!=" && "$type" eq "false")) {
4595179f8f40SJoe Perches					$op = "";
4596179f8f40SJoe Perches				}
4597179f8f40SJoe Perches
4598179f8f40SJoe Perches				CHK("BOOL_COMPARISON",
4599179f8f40SJoe Perches				    "Using comparison to $otype is error prone\n" . $herecurr);
4600179f8f40SJoe Perches
4601179f8f40SJoe Perches## maybe suggesting a correct construct would better
4602179f8f40SJoe Perches##				    "Using comparison to $otype is error prone.  Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
4603179f8f40SJoe Perches
4604179f8f40SJoe Perches			}
4605179f8f40SJoe Perches		}
4606179f8f40SJoe Perches
46074882720bSThomas Gleixner# check for semaphores initialized locked
46084882720bSThomas Gleixner		if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
4609000d1cc1SJoe Perches			WARN("CONSIDER_COMPLETION",
4610000d1cc1SJoe Perches			     "consider using a completion\n" . $herecurr);
4611773647a0SAndy Whitcroft		}
46126712d858SJoe Perches
461367d0a075SJoe Perches# recommend kstrto* over simple_strto* and strict_strto*
461467d0a075SJoe Perches		if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
4615000d1cc1SJoe Perches			WARN("CONSIDER_KSTRTO",
461667d0a075SJoe Perches			     "$1 is obsolete, use k$3 instead\n" . $herecurr);
4617773647a0SAndy Whitcroft		}
46186712d858SJoe Perches
4619ae3ccc46SFabian Frederick# check for __initcall(), use device_initcall() explicitly or more appropriate function please
4620f3db6639SMichael Ellerman		if ($line =~ /^.\s*__initcall\s*\(/) {
4621000d1cc1SJoe Perches			WARN("USE_DEVICE_INITCALL",
4622ae3ccc46SFabian Frederick			     "please use device_initcall() or more appropriate function instead of __initcall() (see include/linux/init.h)\n" . $herecurr);
4623f3db6639SMichael Ellerman		}
46246712d858SJoe Perches
462579404849SEmese Revfy# check for various ops structs, ensure they are const.
462679404849SEmese Revfy		my $struct_ops = qr{acpi_dock_ops|
462779404849SEmese Revfy				address_space_operations|
462879404849SEmese Revfy				backlight_ops|
462979404849SEmese Revfy				block_device_operations|
463079404849SEmese Revfy				dentry_operations|
463179404849SEmese Revfy				dev_pm_ops|
463279404849SEmese Revfy				dma_map_ops|
463379404849SEmese Revfy				extent_io_ops|
463479404849SEmese Revfy				file_lock_operations|
463579404849SEmese Revfy				file_operations|
463679404849SEmese Revfy				hv_ops|
463779404849SEmese Revfy				ide_dma_ops|
463879404849SEmese Revfy				intel_dvo_dev_ops|
463979404849SEmese Revfy				item_operations|
464079404849SEmese Revfy				iwl_ops|
464179404849SEmese Revfy				kgdb_arch|
464279404849SEmese Revfy				kgdb_io|
464379404849SEmese Revfy				kset_uevent_ops|
464479404849SEmese Revfy				lock_manager_operations|
464579404849SEmese Revfy				microcode_ops|
464679404849SEmese Revfy				mtrr_ops|
464779404849SEmese Revfy				neigh_ops|
464879404849SEmese Revfy				nlmsvc_binding|
464979404849SEmese Revfy				pci_raw_ops|
465079404849SEmese Revfy				pipe_buf_operations|
465179404849SEmese Revfy				platform_hibernation_ops|
465279404849SEmese Revfy				platform_suspend_ops|
465379404849SEmese Revfy				proto_ops|
465479404849SEmese Revfy				rpc_pipe_ops|
465579404849SEmese Revfy				seq_operations|
465679404849SEmese Revfy				snd_ac97_build_ops|
465779404849SEmese Revfy				soc_pcmcia_socket_ops|
465879404849SEmese Revfy				stacktrace_ops|
465979404849SEmese Revfy				sysfs_ops|
466079404849SEmese Revfy				tty_operations|
466179404849SEmese Revfy				usb_mon_operations|
466279404849SEmese Revfy				wd_ops}x;
46636903ffb2SAndy Whitcroft		if ($line !~ /\bconst\b/ &&
466479404849SEmese Revfy		    $line =~ /\bstruct\s+($struct_ops)\b/) {
4665000d1cc1SJoe Perches			WARN("CONST_STRUCT",
4666000d1cc1SJoe Perches			     "struct $1 should normally be const\n" .
46676903ffb2SAndy Whitcroft				$herecurr);
46682b6db5cbSAndy Whitcroft		}
4669773647a0SAndy Whitcroft
4670773647a0SAndy Whitcroft# use of NR_CPUS is usually wrong
4671773647a0SAndy Whitcroft# ignore definitions of NR_CPUS and usage to define arrays as likely right
4672773647a0SAndy Whitcroft		if ($line =~ /\bNR_CPUS\b/ &&
4673c45dcabdSAndy Whitcroft		    $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
4674c45dcabdSAndy Whitcroft		    $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
4675171ae1a4SAndy Whitcroft		    $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
4676171ae1a4SAndy Whitcroft		    $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
4677171ae1a4SAndy Whitcroft		    $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
4678773647a0SAndy Whitcroft		{
4679000d1cc1SJoe Perches			WARN("NR_CPUS",
4680000d1cc1SJoe Perches			     "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
4681773647a0SAndy Whitcroft		}
46829c9ba34eSAndy Whitcroft
468352ea8506SJoe Perches# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
468452ea8506SJoe Perches		if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
468552ea8506SJoe Perches			ERROR("DEFINE_ARCH_HAS",
468652ea8506SJoe Perches			      "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
468752ea8506SJoe Perches		}
468852ea8506SJoe Perches
46899c9ba34eSAndy Whitcroft# check for %L{u,d,i} in strings
46909c9ba34eSAndy Whitcroft		my $string;
46919c9ba34eSAndy Whitcroft		while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
46929c9ba34eSAndy Whitcroft			$string = substr($rawline, $-[1], $+[1] - $-[1]);
46932a1bc5d5SAndy Whitcroft			$string =~ s/%%/__/g;
46949c9ba34eSAndy Whitcroft			if ($string =~ /(?<!%)%L[udi]/) {
4695000d1cc1SJoe Perches				WARN("PRINTF_L",
4696000d1cc1SJoe Perches				     "\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
46979c9ba34eSAndy Whitcroft				last;
46989c9ba34eSAndy Whitcroft			}
46999c9ba34eSAndy Whitcroft		}
4700691d77b6SAndy Whitcroft
4701691d77b6SAndy Whitcroft# whine mightly about in_atomic
4702691d77b6SAndy Whitcroft		if ($line =~ /\bin_atomic\s*\(/) {
4703691d77b6SAndy Whitcroft			if ($realfile =~ m@^drivers/@) {
4704000d1cc1SJoe Perches				ERROR("IN_ATOMIC",
4705000d1cc1SJoe Perches				      "do not use in_atomic in drivers\n" . $herecurr);
4706f4a87736SAndy Whitcroft			} elsif ($realfile !~ m@^kernel/@) {
4707000d1cc1SJoe Perches				WARN("IN_ATOMIC",
4708000d1cc1SJoe Perches				     "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
4709691d77b6SAndy Whitcroft			}
4710691d77b6SAndy Whitcroft		}
47111704f47bSPeter Zijlstra
47121704f47bSPeter Zijlstra# check for lockdep_set_novalidate_class
47131704f47bSPeter Zijlstra		if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
47141704f47bSPeter Zijlstra		    $line =~ /__lockdep_no_validate__\s*\)/ ) {
47151704f47bSPeter Zijlstra			if ($realfile !~ m@^kernel/lockdep@ &&
47161704f47bSPeter Zijlstra			    $realfile !~ m@^include/linux/lockdep@ &&
47171704f47bSPeter Zijlstra			    $realfile !~ m@^drivers/base/core@) {
4718000d1cc1SJoe Perches				ERROR("LOCKDEP",
4719000d1cc1SJoe Perches				      "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
47201704f47bSPeter Zijlstra			}
47211704f47bSPeter Zijlstra		}
472288f8831cSDave Jones
472388f8831cSDave Jones		if ($line =~ /debugfs_create_file.*S_IWUGO/ ||
472488f8831cSDave Jones		    $line =~ /DEVICE_ATTR.*S_IWUGO/ ) {
4725000d1cc1SJoe Perches			WARN("EXPORTED_WORLD_WRITABLE",
4726000d1cc1SJoe Perches			     "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
472788f8831cSDave Jones		}
47282435880fSJoe Perches
4729515a235eSJoe Perches# Mode permission misuses where it seems decimal should be octal
4730515a235eSJoe Perches# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop
4731515a235eSJoe Perches		if ($^V && $^V ge 5.10.0 &&
4732515a235eSJoe Perches		    $line =~ /$mode_perms_search/) {
47332435880fSJoe Perches			foreach my $entry (@mode_permission_funcs) {
47342435880fSJoe Perches				my $func = $entry->[0];
47352435880fSJoe Perches				my $arg_pos = $entry->[1];
47362435880fSJoe Perches
47372435880fSJoe Perches				my $skip_args = "";
47382435880fSJoe Perches				if ($arg_pos > 1) {
47392435880fSJoe Perches					$arg_pos--;
47402435880fSJoe Perches					$skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}";
47412435880fSJoe Perches				}
47422435880fSJoe Perches				my $test = "\\b$func\\s*\\(${skip_args}([\\d]+)\\s*[,\\)]";
4743515a235eSJoe Perches				if ($line =~ /$test/) {
47442435880fSJoe Perches					my $val = $1;
47452435880fSJoe Perches					$val = $6 if ($skip_args ne "");
47462435880fSJoe Perches
47471727cc70SJoe Perches					if ($val !~ /^0$/ &&
47481727cc70SJoe Perches					    (($val =~ /^$Int$/ && $val !~ /^$Octal$/) ||
47491727cc70SJoe Perches					     length($val) ne 4)) {
47502435880fSJoe Perches						ERROR("NON_OCTAL_PERMISSIONS",
47511727cc70SJoe Perches						      "Use 4 digit octal (0777) not decimal permissions\n" . $herecurr);
47522435880fSJoe Perches					}
47532435880fSJoe Perches				}
47542435880fSJoe Perches			}
475513214adfSAndy Whitcroft		}
4756515a235eSJoe Perches	}
475713214adfSAndy Whitcroft
475813214adfSAndy Whitcroft	# If we have no input at all, then there is nothing to report on
475913214adfSAndy Whitcroft	# so just keep quiet.
476013214adfSAndy Whitcroft	if ($#rawlines == -1) {
476113214adfSAndy Whitcroft		exit(0);
47620a920b5bSAndy Whitcroft	}
47630a920b5bSAndy Whitcroft
47648905a67cSAndy Whitcroft	# In mailback mode only produce a report in the negative, for
47658905a67cSAndy Whitcroft	# things that appear to be patches.
47668905a67cSAndy Whitcroft	if ($mailback && ($clean == 1 || !$is_patch)) {
47678905a67cSAndy Whitcroft		exit(0);
47688905a67cSAndy Whitcroft	}
47698905a67cSAndy Whitcroft
47708905a67cSAndy Whitcroft	# This is not a patch, and we are are in 'no-patch' mode so
47718905a67cSAndy Whitcroft	# just keep quiet.
47728905a67cSAndy Whitcroft	if (!$chk_patch && !$is_patch) {
47738905a67cSAndy Whitcroft		exit(0);
47748905a67cSAndy Whitcroft	}
47758905a67cSAndy Whitcroft
47768905a67cSAndy Whitcroft	if (!$is_patch) {
4777000d1cc1SJoe Perches		ERROR("NOT_UNIFIED_DIFF",
4778000d1cc1SJoe Perches		      "Does not appear to be a unified-diff format patch\n");
47790a920b5bSAndy Whitcroft	}
47800a920b5bSAndy Whitcroft	if ($is_patch && $chk_signoff && $signoff == 0) {
4781000d1cc1SJoe Perches		ERROR("MISSING_SIGN_OFF",
4782000d1cc1SJoe Perches		      "Missing Signed-off-by: line(s)\n");
47830a920b5bSAndy Whitcroft	}
47840a920b5bSAndy Whitcroft
4785f0a594c1SAndy Whitcroft	print report_dump();
478613214adfSAndy Whitcroft	if ($summary && !($clean == 1 && $quiet == 1)) {
478713214adfSAndy Whitcroft		print "$filename " if ($summary_file);
47886c72ffaaSAndy Whitcroft		print "total: $cnt_error errors, $cnt_warn warnings, " .
47896c72ffaaSAndy Whitcroft			(($check)? "$cnt_chk checks, " : "") .
47906c72ffaaSAndy Whitcroft			"$cnt_lines lines checked\n";
47918905a67cSAndy Whitcroft		print "\n" if ($quiet == 0);
47926c72ffaaSAndy Whitcroft	}
47938905a67cSAndy Whitcroft
4794d2c0a235SAndy Whitcroft	if ($quiet == 0) {
4795d1fe9c09SJoe Perches
4796d1fe9c09SJoe Perches		if ($^V lt 5.10.0) {
4797d1fe9c09SJoe Perches			print("NOTE: perl $^V is not modern enough to detect all possible issues.\n");
4798d1fe9c09SJoe Perches			print("An upgrade to at least perl v5.10.0 is suggested.\n\n");
4799d1fe9c09SJoe Perches		}
4800d1fe9c09SJoe Perches
4801d2c0a235SAndy Whitcroft		# If there were whitespace errors which cleanpatch can fix
4802d2c0a235SAndy Whitcroft		# then suggest that.
4803d2c0a235SAndy Whitcroft		if ($rpt_cleaners) {
4804d2c0a235SAndy Whitcroft			print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n";
4805d2c0a235SAndy Whitcroft			print "      scripts/cleanfile\n\n";
4806b0781216SMike Frysinger			$rpt_cleaners = 0;
4807d2c0a235SAndy Whitcroft		}
4808d2c0a235SAndy Whitcroft	}
4809d2c0a235SAndy Whitcroft
481091bfe484SJoe Perches	hash_show_words(\%use_type, "Used");
481191bfe484SJoe Perches	hash_show_words(\%ignore_type, "Ignored");
4812000d1cc1SJoe Perches
48133705ce5bSJoe Perches	if ($clean == 0 && $fix && "@rawlines" ne "@fixed") {
48149624b8d6SJoe Perches		my $newfile = $filename;
48159624b8d6SJoe Perches		$newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace);
48163705ce5bSJoe Perches		my $linecount = 0;
48173705ce5bSJoe Perches		my $f;
48183705ce5bSJoe Perches
48193705ce5bSJoe Perches		open($f, '>', $newfile)
48203705ce5bSJoe Perches		    or die "$P: Can't open $newfile for write\n";
48213705ce5bSJoe Perches		foreach my $fixed_line (@fixed) {
48223705ce5bSJoe Perches			$linecount++;
48233705ce5bSJoe Perches			if ($file) {
48243705ce5bSJoe Perches				if ($linecount > 3) {
48253705ce5bSJoe Perches					$fixed_line =~ s/^\+//;
48263705ce5bSJoe Perches					print $f $fixed_line. "\n";
48273705ce5bSJoe Perches				}
48283705ce5bSJoe Perches			} else {
48293705ce5bSJoe Perches				print $f $fixed_line . "\n";
48303705ce5bSJoe Perches			}
48313705ce5bSJoe Perches		}
48323705ce5bSJoe Perches		close($f);
48333705ce5bSJoe Perches
48343705ce5bSJoe Perches		if (!$quiet) {
48353705ce5bSJoe Perches			print << "EOM";
48363705ce5bSJoe PerchesWrote EXPERIMENTAL --fix correction(s) to '$newfile'
48373705ce5bSJoe Perches
48383705ce5bSJoe PerchesDo _NOT_ trust the results written to this file.
48393705ce5bSJoe PerchesDo _NOT_ submit these changes without inspecting them for correctness.
48403705ce5bSJoe Perches
48413705ce5bSJoe PerchesThis EXPERIMENTAL file is simply a convenience to help rewrite patches.
48423705ce5bSJoe PerchesNo warranties, expressed or implied...
48433705ce5bSJoe Perches
48443705ce5bSJoe PerchesEOM
48453705ce5bSJoe Perches		}
48463705ce5bSJoe Perches	}
48473705ce5bSJoe Perches
48480a920b5bSAndy Whitcroft	if ($clean == 1 && $quiet == 0) {
4849c2fdda0dSAndy Whitcroft		print "$vname has no obvious style problems and is ready for submission.\n"
48500a920b5bSAndy Whitcroft	}
48510a920b5bSAndy Whitcroft	if ($clean == 0 && $quiet == 0) {
4852000d1cc1SJoe Perches		print << "EOM";
4853000d1cc1SJoe Perches$vname has style problems, please review.
4854000d1cc1SJoe Perches
4855000d1cc1SJoe PerchesIf any of these errors are false positives, please report
4856000d1cc1SJoe Perchesthem to the maintainer, see CHECKPATCH in MAINTAINERS.
4857000d1cc1SJoe PerchesEOM
48580a920b5bSAndy Whitcroft	}
485913214adfSAndy Whitcroft
48600a920b5bSAndy Whitcroft	return $clean;
48610a920b5bSAndy Whitcroft}
4862