xref: /linux-6.15/scripts/checkpatch.pl (revision 13f1937e)
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
4381574a29fSJoe Perches			(?:(?:\s|\*|\[\])+\s*const|(?:\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
553d311cd44SJoe Perchessub git_commit_info {
554d311cd44SJoe Perches	my ($commit, $id, $desc) = @_;
555d311cd44SJoe Perches
556d311cd44SJoe Perches	return ($id, $desc) if ((which("git") eq "") || !(-e ".git"));
557d311cd44SJoe Perches
558d311cd44SJoe Perches	my $output = `git log --no-color --format='%H %s' -1 $commit 2>&1`;
559d311cd44SJoe Perches	$output =~ s/^\s*//gm;
560d311cd44SJoe Perches	my @lines = split("\n", $output);
561d311cd44SJoe Perches
562d311cd44SJoe Perches	if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous\./) {
563d311cd44SJoe Perches# Maybe one day convert this block of bash into something that returns
564d311cd44SJoe Perches# all matching commit ids, but it's very slow...
565d311cd44SJoe Perches#
566d311cd44SJoe Perches#		echo "checking commits $1..."
567d311cd44SJoe Perches#		git rev-list --remotes | grep -i "^$1" |
568d311cd44SJoe Perches#		while read line ; do
569d311cd44SJoe Perches#		    git log --format='%H %s' -1 $line |
570d311cd44SJoe Perches#		    echo "commit $(cut -c 1-12,41-)"
571d311cd44SJoe Perches#		done
572d311cd44SJoe Perches	} elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./) {
573d311cd44SJoe Perches	} else {
574d311cd44SJoe Perches		$id = substr($lines[0], 0, 12);
575d311cd44SJoe Perches		$desc = substr($lines[0], 41);
576d311cd44SJoe Perches	}
577d311cd44SJoe Perches
578d311cd44SJoe Perches	return ($id, $desc);
579d311cd44SJoe Perches}
580d311cd44SJoe Perches
5816c72ffaaSAndy Whitcroft$chk_signoff = 0 if ($file);
5820a920b5bSAndy Whitcroft
58300df344fSAndy Whitcroftmy @rawlines = ();
584c2fdda0dSAndy Whitcroftmy @lines = ();
5853705ce5bSJoe Perchesmy @fixed = ();
586c2fdda0dSAndy Whitcroftmy $vname;
5876c72ffaaSAndy Whitcroftfor my $filename (@ARGV) {
58821caa13cSAndy Whitcroft	my $FILE;
5896c72ffaaSAndy Whitcroft	if ($file) {
59021caa13cSAndy Whitcroft		open($FILE, '-|', "diff -u /dev/null $filename") ||
5916c72ffaaSAndy Whitcroft			die "$P: $filename: diff failed - $!\n";
59221caa13cSAndy Whitcroft	} elsif ($filename eq '-') {
59321caa13cSAndy Whitcroft		open($FILE, '<&STDIN');
5946c72ffaaSAndy Whitcroft	} else {
59521caa13cSAndy Whitcroft		open($FILE, '<', "$filename") ||
5966c72ffaaSAndy Whitcroft			die "$P: $filename: open failed - $!\n";
5976c72ffaaSAndy Whitcroft	}
598c2fdda0dSAndy Whitcroft	if ($filename eq '-') {
599c2fdda0dSAndy Whitcroft		$vname = 'Your patch';
600c2fdda0dSAndy Whitcroft	} else {
601c2fdda0dSAndy Whitcroft		$vname = $filename;
602c2fdda0dSAndy Whitcroft	}
60321caa13cSAndy Whitcroft	while (<$FILE>) {
6040a920b5bSAndy Whitcroft		chomp;
60500df344fSAndy Whitcroft		push(@rawlines, $_);
6066c72ffaaSAndy Whitcroft	}
60721caa13cSAndy Whitcroft	close($FILE);
608c2fdda0dSAndy Whitcroft	if (!process($filename)) {
6090a920b5bSAndy Whitcroft		$exit = 1;
6100a920b5bSAndy Whitcroft	}
61100df344fSAndy Whitcroft	@rawlines = ();
61213214adfSAndy Whitcroft	@lines = ();
6133705ce5bSJoe Perches	@fixed = ();
6140a920b5bSAndy Whitcroft}
6150a920b5bSAndy Whitcroft
6160a920b5bSAndy Whitcroftexit($exit);
6170a920b5bSAndy Whitcroft
6180a920b5bSAndy Whitcroftsub top_of_kernel_tree {
6196c72ffaaSAndy Whitcroft	my ($root) = @_;
6206c72ffaaSAndy Whitcroft
6216c72ffaaSAndy Whitcroft	my @tree_check = (
6226c72ffaaSAndy Whitcroft		"COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
6236c72ffaaSAndy Whitcroft		"README", "Documentation", "arch", "include", "drivers",
6246c72ffaaSAndy Whitcroft		"fs", "init", "ipc", "kernel", "lib", "scripts",
6256c72ffaaSAndy Whitcroft	);
6266c72ffaaSAndy Whitcroft
6276c72ffaaSAndy Whitcroft	foreach my $check (@tree_check) {
6286c72ffaaSAndy Whitcroft		if (! -e $root . '/' . $check) {
6290a920b5bSAndy Whitcroft			return 0;
6300a920b5bSAndy Whitcroft		}
6316c72ffaaSAndy Whitcroft	}
6326c72ffaaSAndy Whitcroft	return 1;
6336c72ffaaSAndy Whitcroft}
6340a920b5bSAndy Whitcroft
63520112475SJoe Perchessub parse_email {
63620112475SJoe Perches	my ($formatted_email) = @_;
63720112475SJoe Perches
63820112475SJoe Perches	my $name = "";
63920112475SJoe Perches	my $address = "";
64020112475SJoe Perches	my $comment = "";
64120112475SJoe Perches
64220112475SJoe Perches	if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
64320112475SJoe Perches		$name = $1;
64420112475SJoe Perches		$address = $2;
64520112475SJoe Perches		$comment = $3 if defined $3;
64620112475SJoe Perches	} elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
64720112475SJoe Perches		$address = $1;
64820112475SJoe Perches		$comment = $2 if defined $2;
64920112475SJoe Perches	} elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
65020112475SJoe Perches		$address = $1;
65120112475SJoe Perches		$comment = $2 if defined $2;
65220112475SJoe Perches		$formatted_email =~ s/$address.*$//;
65320112475SJoe Perches		$name = $formatted_email;
6543705ce5bSJoe Perches		$name = trim($name);
65520112475SJoe Perches		$name =~ s/^\"|\"$//g;
65620112475SJoe Perches		# If there's a name left after stripping spaces and
65720112475SJoe Perches		# leading quotes, and the address doesn't have both
65820112475SJoe Perches		# leading and trailing angle brackets, the address
65920112475SJoe Perches		# is invalid. ie:
66020112475SJoe Perches		#   "joe smith [email protected]" bad
66120112475SJoe Perches		#   "joe smith <[email protected]" bad
66220112475SJoe Perches		if ($name ne "" && $address !~ /^<[^>]+>$/) {
66320112475SJoe Perches			$name = "";
66420112475SJoe Perches			$address = "";
66520112475SJoe Perches			$comment = "";
66620112475SJoe Perches		}
66720112475SJoe Perches	}
66820112475SJoe Perches
6693705ce5bSJoe Perches	$name = trim($name);
67020112475SJoe Perches	$name =~ s/^\"|\"$//g;
6713705ce5bSJoe Perches	$address = trim($address);
67220112475SJoe Perches	$address =~ s/^\<|\>$//g;
67320112475SJoe Perches
67420112475SJoe Perches	if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
67520112475SJoe Perches		$name =~ s/(?<!\\)"/\\"/g; ##escape quotes
67620112475SJoe Perches		$name = "\"$name\"";
67720112475SJoe Perches	}
67820112475SJoe Perches
67920112475SJoe Perches	return ($name, $address, $comment);
68020112475SJoe Perches}
68120112475SJoe Perches
68220112475SJoe Perchessub format_email {
68320112475SJoe Perches	my ($name, $address) = @_;
68420112475SJoe Perches
68520112475SJoe Perches	my $formatted_email;
68620112475SJoe Perches
6873705ce5bSJoe Perches	$name = trim($name);
68820112475SJoe Perches	$name =~ s/^\"|\"$//g;
6893705ce5bSJoe Perches	$address = trim($address);
69020112475SJoe Perches
69120112475SJoe Perches	if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
69220112475SJoe Perches		$name =~ s/(?<!\\)"/\\"/g; ##escape quotes
69320112475SJoe Perches		$name = "\"$name\"";
69420112475SJoe Perches	}
69520112475SJoe Perches
69620112475SJoe Perches	if ("$name" eq "") {
69720112475SJoe Perches		$formatted_email = "$address";
69820112475SJoe Perches	} else {
69920112475SJoe Perches		$formatted_email = "$name <$address>";
70020112475SJoe Perches	}
70120112475SJoe Perches
70220112475SJoe Perches	return $formatted_email;
70320112475SJoe Perches}
70420112475SJoe Perches
705d311cd44SJoe Perchessub which {
706d311cd44SJoe Perches    my ($bin) = @_;
707d311cd44SJoe Perches
708d311cd44SJoe Perches    foreach my $path (split(/:/, $ENV{PATH})) {
709d311cd44SJoe Perches	if (-e "$path/$bin") {
710d311cd44SJoe Perches	    return "$path/$bin";
711d311cd44SJoe Perches	}
712d311cd44SJoe Perches    }
713d311cd44SJoe Perches
714d311cd44SJoe Perches    return "";
715d311cd44SJoe Perches}
716d311cd44SJoe Perches
717000d1cc1SJoe Perchessub which_conf {
718000d1cc1SJoe Perches	my ($conf) = @_;
719000d1cc1SJoe Perches
720000d1cc1SJoe Perches	foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
721000d1cc1SJoe Perches		if (-e "$path/$conf") {
722000d1cc1SJoe Perches			return "$path/$conf";
723000d1cc1SJoe Perches		}
724000d1cc1SJoe Perches	}
725000d1cc1SJoe Perches
726000d1cc1SJoe Perches	return "";
727000d1cc1SJoe Perches}
728000d1cc1SJoe Perches
7290a920b5bSAndy Whitcroftsub expand_tabs {
7300a920b5bSAndy Whitcroft	my ($str) = @_;
7310a920b5bSAndy Whitcroft
7320a920b5bSAndy Whitcroft	my $res = '';
7330a920b5bSAndy Whitcroft	my $n = 0;
7340a920b5bSAndy Whitcroft	for my $c (split(//, $str)) {
7350a920b5bSAndy Whitcroft		if ($c eq "\t") {
7360a920b5bSAndy Whitcroft			$res .= ' ';
7370a920b5bSAndy Whitcroft			$n++;
7380a920b5bSAndy Whitcroft			for (; ($n % 8) != 0; $n++) {
7390a920b5bSAndy Whitcroft				$res .= ' ';
7400a920b5bSAndy Whitcroft			}
7410a920b5bSAndy Whitcroft			next;
7420a920b5bSAndy Whitcroft		}
7430a920b5bSAndy Whitcroft		$res .= $c;
7440a920b5bSAndy Whitcroft		$n++;
7450a920b5bSAndy Whitcroft	}
7460a920b5bSAndy Whitcroft
7470a920b5bSAndy Whitcroft	return $res;
7480a920b5bSAndy Whitcroft}
7496c72ffaaSAndy Whitcroftsub copy_spacing {
750773647a0SAndy Whitcroft	(my $res = shift) =~ tr/\t/ /c;
7516c72ffaaSAndy Whitcroft	return $res;
7526c72ffaaSAndy Whitcroft}
7530a920b5bSAndy Whitcroft
7544a0df2efSAndy Whitcroftsub line_stats {
7554a0df2efSAndy Whitcroft	my ($line) = @_;
7564a0df2efSAndy Whitcroft
7574a0df2efSAndy Whitcroft	# Drop the diff line leader and expand tabs
7584a0df2efSAndy Whitcroft	$line =~ s/^.//;
7594a0df2efSAndy Whitcroft	$line = expand_tabs($line);
7604a0df2efSAndy Whitcroft
7614a0df2efSAndy Whitcroft	# Pick the indent from the front of the line.
7624a0df2efSAndy Whitcroft	my ($white) = ($line =~ /^(\s*)/);
7634a0df2efSAndy Whitcroft
7644a0df2efSAndy Whitcroft	return (length($line), length($white));
7654a0df2efSAndy Whitcroft}
7664a0df2efSAndy Whitcroft
767773647a0SAndy Whitcroftmy $sanitise_quote = '';
768773647a0SAndy Whitcroft
769773647a0SAndy Whitcroftsub sanitise_line_reset {
770773647a0SAndy Whitcroft	my ($in_comment) = @_;
771773647a0SAndy Whitcroft
772773647a0SAndy Whitcroft	if ($in_comment) {
773773647a0SAndy Whitcroft		$sanitise_quote = '*/';
774773647a0SAndy Whitcroft	} else {
775773647a0SAndy Whitcroft		$sanitise_quote = '';
776773647a0SAndy Whitcroft	}
777773647a0SAndy Whitcroft}
77800df344fSAndy Whitcroftsub sanitise_line {
77900df344fSAndy Whitcroft	my ($line) = @_;
78000df344fSAndy Whitcroft
78100df344fSAndy Whitcroft	my $res = '';
78200df344fSAndy Whitcroft	my $l = '';
78300df344fSAndy Whitcroft
784c2fdda0dSAndy Whitcroft	my $qlen = 0;
785773647a0SAndy Whitcroft	my $off = 0;
786773647a0SAndy Whitcroft	my $c;
78700df344fSAndy Whitcroft
788773647a0SAndy Whitcroft	# Always copy over the diff marker.
789773647a0SAndy Whitcroft	$res = substr($line, 0, 1);
790773647a0SAndy Whitcroft
791773647a0SAndy Whitcroft	for ($off = 1; $off < length($line); $off++) {
792773647a0SAndy Whitcroft		$c = substr($line, $off, 1);
793773647a0SAndy Whitcroft
794773647a0SAndy Whitcroft		# Comments we are wacking completly including the begin
795773647a0SAndy Whitcroft		# and end, all to $;.
796773647a0SAndy Whitcroft		if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
797773647a0SAndy Whitcroft			$sanitise_quote = '*/';
798773647a0SAndy Whitcroft
799773647a0SAndy Whitcroft			substr($res, $off, 2, "$;$;");
800773647a0SAndy Whitcroft			$off++;
80100df344fSAndy Whitcroft			next;
802773647a0SAndy Whitcroft		}
80381bc0e02SAndy Whitcroft		if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
804773647a0SAndy Whitcroft			$sanitise_quote = '';
805773647a0SAndy Whitcroft			substr($res, $off, 2, "$;$;");
806773647a0SAndy Whitcroft			$off++;
807773647a0SAndy Whitcroft			next;
808773647a0SAndy Whitcroft		}
809113f04a8SDaniel Walker		if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
810113f04a8SDaniel Walker			$sanitise_quote = '//';
811113f04a8SDaniel Walker
812113f04a8SDaniel Walker			substr($res, $off, 2, $sanitise_quote);
813113f04a8SDaniel Walker			$off++;
814113f04a8SDaniel Walker			next;
815113f04a8SDaniel Walker		}
816773647a0SAndy Whitcroft
817773647a0SAndy Whitcroft		# A \ in a string means ignore the next character.
818773647a0SAndy Whitcroft		if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
819773647a0SAndy Whitcroft		    $c eq "\\") {
820773647a0SAndy Whitcroft			substr($res, $off, 2, 'XX');
821773647a0SAndy Whitcroft			$off++;
822773647a0SAndy Whitcroft			next;
823773647a0SAndy Whitcroft		}
824773647a0SAndy Whitcroft		# Regular quotes.
825773647a0SAndy Whitcroft		if ($c eq "'" || $c eq '"') {
826773647a0SAndy Whitcroft			if ($sanitise_quote eq '') {
827773647a0SAndy Whitcroft				$sanitise_quote = $c;
828773647a0SAndy Whitcroft
829773647a0SAndy Whitcroft				substr($res, $off, 1, $c);
830773647a0SAndy Whitcroft				next;
831773647a0SAndy Whitcroft			} elsif ($sanitise_quote eq $c) {
832773647a0SAndy Whitcroft				$sanitise_quote = '';
83300df344fSAndy Whitcroft			}
83400df344fSAndy Whitcroft		}
835773647a0SAndy Whitcroft
836fae17daeSAndy Whitcroft		#print "c<$c> SQ<$sanitise_quote>\n";
837773647a0SAndy Whitcroft		if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
838773647a0SAndy Whitcroft			substr($res, $off, 1, $;);
839113f04a8SDaniel Walker		} elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
840113f04a8SDaniel Walker			substr($res, $off, 1, $;);
841773647a0SAndy Whitcroft		} elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
842773647a0SAndy Whitcroft			substr($res, $off, 1, 'X');
84300df344fSAndy Whitcroft		} else {
844773647a0SAndy Whitcroft			substr($res, $off, 1, $c);
84500df344fSAndy Whitcroft		}
846c2fdda0dSAndy Whitcroft	}
847c2fdda0dSAndy Whitcroft
848113f04a8SDaniel Walker	if ($sanitise_quote eq '//') {
849113f04a8SDaniel Walker		$sanitise_quote = '';
850113f04a8SDaniel Walker	}
851113f04a8SDaniel Walker
852c2fdda0dSAndy Whitcroft	# The pathname on a #include may be surrounded by '<' and '>'.
853c45dcabdSAndy Whitcroft	if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
854c2fdda0dSAndy Whitcroft		my $clean = 'X' x length($1);
855c2fdda0dSAndy Whitcroft		$res =~ s@\<.*\>@<$clean>@;
856c2fdda0dSAndy Whitcroft
857c2fdda0dSAndy Whitcroft	# The whole of a #error is a string.
858c45dcabdSAndy Whitcroft	} elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
859c2fdda0dSAndy Whitcroft		my $clean = 'X' x length($1);
860c45dcabdSAndy Whitcroft		$res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
861c2fdda0dSAndy Whitcroft	}
862c2fdda0dSAndy Whitcroft
86300df344fSAndy Whitcroft	return $res;
86400df344fSAndy Whitcroft}
86500df344fSAndy Whitcroft
866a6962d72SJoe Perchessub get_quoted_string {
867a6962d72SJoe Perches	my ($line, $rawline) = @_;
868a6962d72SJoe Perches
869a6962d72SJoe Perches	return "" if ($line !~ m/(\"[X]+\")/g);
870a6962d72SJoe Perches	return substr($rawline, $-[0], $+[0] - $-[0]);
871a6962d72SJoe Perches}
872a6962d72SJoe Perches
8738905a67cSAndy Whitcroftsub ctx_statement_block {
8748905a67cSAndy Whitcroft	my ($linenr, $remain, $off) = @_;
8758905a67cSAndy Whitcroft	my $line = $linenr - 1;
8768905a67cSAndy Whitcroft	my $blk = '';
8778905a67cSAndy Whitcroft	my $soff = $off;
8788905a67cSAndy Whitcroft	my $coff = $off - 1;
879773647a0SAndy Whitcroft	my $coff_set = 0;
8808905a67cSAndy Whitcroft
88113214adfSAndy Whitcroft	my $loff = 0;
88213214adfSAndy Whitcroft
8838905a67cSAndy Whitcroft	my $type = '';
8848905a67cSAndy Whitcroft	my $level = 0;
885a2750645SAndy Whitcroft	my @stack = ();
886cf655043SAndy Whitcroft	my $p;
8878905a67cSAndy Whitcroft	my $c;
8888905a67cSAndy Whitcroft	my $len = 0;
88913214adfSAndy Whitcroft
89013214adfSAndy Whitcroft	my $remainder;
8918905a67cSAndy Whitcroft	while (1) {
892a2750645SAndy Whitcroft		@stack = (['', 0]) if ($#stack == -1);
893a2750645SAndy Whitcroft
894773647a0SAndy Whitcroft		#warn "CSB: blk<$blk> remain<$remain>\n";
8958905a67cSAndy Whitcroft		# If we are about to drop off the end, pull in more
8968905a67cSAndy Whitcroft		# context.
8978905a67cSAndy Whitcroft		if ($off >= $len) {
8988905a67cSAndy Whitcroft			for (; $remain > 0; $line++) {
899dea33496SAndy Whitcroft				last if (!defined $lines[$line]);
900c2fdda0dSAndy Whitcroft				next if ($lines[$line] =~ /^-/);
9018905a67cSAndy Whitcroft				$remain--;
90213214adfSAndy Whitcroft				$loff = $len;
903c2fdda0dSAndy Whitcroft				$blk .= $lines[$line] . "\n";
9048905a67cSAndy Whitcroft				$len = length($blk);
9058905a67cSAndy Whitcroft				$line++;
9068905a67cSAndy Whitcroft				last;
9078905a67cSAndy Whitcroft			}
9088905a67cSAndy Whitcroft			# Bail if there is no further context.
9098905a67cSAndy Whitcroft			#warn "CSB: blk<$blk> off<$off> len<$len>\n";
91013214adfSAndy Whitcroft			if ($off >= $len) {
9118905a67cSAndy Whitcroft				last;
9128905a67cSAndy Whitcroft			}
913f74bd194SAndy Whitcroft			if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
914f74bd194SAndy Whitcroft				$level++;
915f74bd194SAndy Whitcroft				$type = '#';
916f74bd194SAndy Whitcroft			}
9178905a67cSAndy Whitcroft		}
918cf655043SAndy Whitcroft		$p = $c;
9198905a67cSAndy Whitcroft		$c = substr($blk, $off, 1);
92013214adfSAndy Whitcroft		$remainder = substr($blk, $off);
9218905a67cSAndy Whitcroft
922773647a0SAndy Whitcroft		#warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
9234635f4fbSAndy Whitcroft
9244635f4fbSAndy Whitcroft		# Handle nested #if/#else.
9254635f4fbSAndy Whitcroft		if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
9264635f4fbSAndy Whitcroft			push(@stack, [ $type, $level ]);
9274635f4fbSAndy Whitcroft		} elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
9284635f4fbSAndy Whitcroft			($type, $level) = @{$stack[$#stack - 1]};
9294635f4fbSAndy Whitcroft		} elsif ($remainder =~ /^#\s*endif\b/) {
9304635f4fbSAndy Whitcroft			($type, $level) = @{pop(@stack)};
9314635f4fbSAndy Whitcroft		}
9324635f4fbSAndy Whitcroft
9338905a67cSAndy Whitcroft		# Statement ends at the ';' or a close '}' at the
9348905a67cSAndy Whitcroft		# outermost level.
9358905a67cSAndy Whitcroft		if ($level == 0 && $c eq ';') {
9368905a67cSAndy Whitcroft			last;
9378905a67cSAndy Whitcroft		}
9388905a67cSAndy Whitcroft
93913214adfSAndy Whitcroft		# An else is really a conditional as long as its not else if
940773647a0SAndy Whitcroft		if ($level == 0 && $coff_set == 0 &&
941773647a0SAndy Whitcroft				(!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
942773647a0SAndy Whitcroft				$remainder =~ /^(else)(?:\s|{)/ &&
943773647a0SAndy Whitcroft				$remainder !~ /^else\s+if\b/) {
944773647a0SAndy Whitcroft			$coff = $off + length($1) - 1;
945773647a0SAndy Whitcroft			$coff_set = 1;
946773647a0SAndy Whitcroft			#warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
947773647a0SAndy Whitcroft			#warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
94813214adfSAndy Whitcroft		}
94913214adfSAndy Whitcroft
9508905a67cSAndy Whitcroft		if (($type eq '' || $type eq '(') && $c eq '(') {
9518905a67cSAndy Whitcroft			$level++;
9528905a67cSAndy Whitcroft			$type = '(';
9538905a67cSAndy Whitcroft		}
9548905a67cSAndy Whitcroft		if ($type eq '(' && $c eq ')') {
9558905a67cSAndy Whitcroft			$level--;
9568905a67cSAndy Whitcroft			$type = ($level != 0)? '(' : '';
9578905a67cSAndy Whitcroft
9588905a67cSAndy Whitcroft			if ($level == 0 && $coff < $soff) {
9598905a67cSAndy Whitcroft				$coff = $off;
960773647a0SAndy Whitcroft				$coff_set = 1;
961773647a0SAndy Whitcroft				#warn "CSB: mark coff<$coff>\n";
9628905a67cSAndy Whitcroft			}
9638905a67cSAndy Whitcroft		}
9648905a67cSAndy Whitcroft		if (($type eq '' || $type eq '{') && $c eq '{') {
9658905a67cSAndy Whitcroft			$level++;
9668905a67cSAndy Whitcroft			$type = '{';
9678905a67cSAndy Whitcroft		}
9688905a67cSAndy Whitcroft		if ($type eq '{' && $c eq '}') {
9698905a67cSAndy Whitcroft			$level--;
9708905a67cSAndy Whitcroft			$type = ($level != 0)? '{' : '';
9718905a67cSAndy Whitcroft
9728905a67cSAndy Whitcroft			if ($level == 0) {
973b998e001SPatrick Pannuto				if (substr($blk, $off + 1, 1) eq ';') {
974b998e001SPatrick Pannuto					$off++;
975b998e001SPatrick Pannuto				}
9768905a67cSAndy Whitcroft				last;
9778905a67cSAndy Whitcroft			}
9788905a67cSAndy Whitcroft		}
979f74bd194SAndy Whitcroft		# Preprocessor commands end at the newline unless escaped.
980f74bd194SAndy Whitcroft		if ($type eq '#' && $c eq "\n" && $p ne "\\") {
981f74bd194SAndy Whitcroft			$level--;
982f74bd194SAndy Whitcroft			$type = '';
983f74bd194SAndy Whitcroft			$off++;
984f74bd194SAndy Whitcroft			last;
985f74bd194SAndy Whitcroft		}
9868905a67cSAndy Whitcroft		$off++;
9878905a67cSAndy Whitcroft	}
988a3bb97a7SAndy Whitcroft	# We are truly at the end, so shuffle to the next line.
98913214adfSAndy Whitcroft	if ($off == $len) {
990a3bb97a7SAndy Whitcroft		$loff = $len + 1;
99113214adfSAndy Whitcroft		$line++;
99213214adfSAndy Whitcroft		$remain--;
99313214adfSAndy Whitcroft	}
9948905a67cSAndy Whitcroft
9958905a67cSAndy Whitcroft	my $statement = substr($blk, $soff, $off - $soff + 1);
9968905a67cSAndy Whitcroft	my $condition = substr($blk, $soff, $coff - $soff + 1);
9978905a67cSAndy Whitcroft
9988905a67cSAndy Whitcroft	#warn "STATEMENT<$statement>\n";
9998905a67cSAndy Whitcroft	#warn "CONDITION<$condition>\n";
10008905a67cSAndy Whitcroft
1001773647a0SAndy Whitcroft	#print "coff<$coff> soff<$off> loff<$loff>\n";
100213214adfSAndy Whitcroft
100313214adfSAndy Whitcroft	return ($statement, $condition,
100413214adfSAndy Whitcroft			$line, $remain + 1, $off - $loff + 1, $level);
100513214adfSAndy Whitcroft}
100613214adfSAndy Whitcroft
1007cf655043SAndy Whitcroftsub statement_lines {
1008cf655043SAndy Whitcroft	my ($stmt) = @_;
1009cf655043SAndy Whitcroft
1010cf655043SAndy Whitcroft	# Strip the diff line prefixes and rip blank lines at start and end.
1011cf655043SAndy Whitcroft	$stmt =~ s/(^|\n)./$1/g;
1012cf655043SAndy Whitcroft	$stmt =~ s/^\s*//;
1013cf655043SAndy Whitcroft	$stmt =~ s/\s*$//;
1014cf655043SAndy Whitcroft
1015cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
1016cf655043SAndy Whitcroft
1017cf655043SAndy Whitcroft	return $#stmt_lines + 2;
1018cf655043SAndy Whitcroft}
1019cf655043SAndy Whitcroft
1020cf655043SAndy Whitcroftsub statement_rawlines {
1021cf655043SAndy Whitcroft	my ($stmt) = @_;
1022cf655043SAndy Whitcroft
1023cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
1024cf655043SAndy Whitcroft
1025cf655043SAndy Whitcroft	return $#stmt_lines + 2;
1026cf655043SAndy Whitcroft}
1027cf655043SAndy Whitcroft
1028cf655043SAndy Whitcroftsub statement_block_size {
1029cf655043SAndy Whitcroft	my ($stmt) = @_;
1030cf655043SAndy Whitcroft
1031cf655043SAndy Whitcroft	$stmt =~ s/(^|\n)./$1/g;
1032cf655043SAndy Whitcroft	$stmt =~ s/^\s*{//;
1033cf655043SAndy Whitcroft	$stmt =~ s/}\s*$//;
1034cf655043SAndy Whitcroft	$stmt =~ s/^\s*//;
1035cf655043SAndy Whitcroft	$stmt =~ s/\s*$//;
1036cf655043SAndy Whitcroft
1037cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
1038cf655043SAndy Whitcroft	my @stmt_statements = ($stmt =~ /;/g);
1039cf655043SAndy Whitcroft
1040cf655043SAndy Whitcroft	my $stmt_lines = $#stmt_lines + 2;
1041cf655043SAndy Whitcroft	my $stmt_statements = $#stmt_statements + 1;
1042cf655043SAndy Whitcroft
1043cf655043SAndy Whitcroft	if ($stmt_lines > $stmt_statements) {
1044cf655043SAndy Whitcroft		return $stmt_lines;
1045cf655043SAndy Whitcroft	} else {
1046cf655043SAndy Whitcroft		return $stmt_statements;
1047cf655043SAndy Whitcroft	}
1048cf655043SAndy Whitcroft}
1049cf655043SAndy Whitcroft
105013214adfSAndy Whitcroftsub ctx_statement_full {
105113214adfSAndy Whitcroft	my ($linenr, $remain, $off) = @_;
105213214adfSAndy Whitcroft	my ($statement, $condition, $level);
105313214adfSAndy Whitcroft
105413214adfSAndy Whitcroft	my (@chunks);
105513214adfSAndy Whitcroft
1056cf655043SAndy Whitcroft	# Grab the first conditional/block pair.
105713214adfSAndy Whitcroft	($statement, $condition, $linenr, $remain, $off, $level) =
105813214adfSAndy Whitcroft				ctx_statement_block($linenr, $remain, $off);
1059773647a0SAndy Whitcroft	#print "F: c<$condition> s<$statement> remain<$remain>\n";
106013214adfSAndy Whitcroft	push(@chunks, [ $condition, $statement ]);
1061cf655043SAndy Whitcroft	if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
1062cf655043SAndy Whitcroft		return ($level, $linenr, @chunks);
1063cf655043SAndy Whitcroft	}
1064cf655043SAndy Whitcroft
1065cf655043SAndy Whitcroft	# Pull in the following conditional/block pairs and see if they
1066cf655043SAndy Whitcroft	# could continue the statement.
1067cf655043SAndy Whitcroft	for (;;) {
106813214adfSAndy Whitcroft		($statement, $condition, $linenr, $remain, $off, $level) =
106913214adfSAndy Whitcroft				ctx_statement_block($linenr, $remain, $off);
1070cf655043SAndy Whitcroft		#print "C: c<$condition> s<$statement> remain<$remain>\n";
1071773647a0SAndy Whitcroft		last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
1072cf655043SAndy Whitcroft		#print "C: push\n";
1073cf655043SAndy Whitcroft		push(@chunks, [ $condition, $statement ]);
107413214adfSAndy Whitcroft	}
107513214adfSAndy Whitcroft
107613214adfSAndy Whitcroft	return ($level, $linenr, @chunks);
10778905a67cSAndy Whitcroft}
10788905a67cSAndy Whitcroft
10794a0df2efSAndy Whitcroftsub ctx_block_get {
1080f0a594c1SAndy Whitcroft	my ($linenr, $remain, $outer, $open, $close, $off) = @_;
10814a0df2efSAndy Whitcroft	my $line;
10824a0df2efSAndy Whitcroft	my $start = $linenr - 1;
10834a0df2efSAndy Whitcroft	my $blk = '';
10844a0df2efSAndy Whitcroft	my @o;
10854a0df2efSAndy Whitcroft	my @c;
10864a0df2efSAndy Whitcroft	my @res = ();
10874a0df2efSAndy Whitcroft
1088f0a594c1SAndy Whitcroft	my $level = 0;
10894635f4fbSAndy Whitcroft	my @stack = ($level);
109000df344fSAndy Whitcroft	for ($line = $start; $remain > 0; $line++) {
109100df344fSAndy Whitcroft		next if ($rawlines[$line] =~ /^-/);
109200df344fSAndy Whitcroft		$remain--;
109300df344fSAndy Whitcroft
109400df344fSAndy Whitcroft		$blk .= $rawlines[$line];
10954635f4fbSAndy Whitcroft
10964635f4fbSAndy Whitcroft		# Handle nested #if/#else.
109701464f30SAndy Whitcroft		if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
10984635f4fbSAndy Whitcroft			push(@stack, $level);
109901464f30SAndy Whitcroft		} elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
11004635f4fbSAndy Whitcroft			$level = $stack[$#stack - 1];
110101464f30SAndy Whitcroft		} elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
11024635f4fbSAndy Whitcroft			$level = pop(@stack);
11034635f4fbSAndy Whitcroft		}
11044635f4fbSAndy Whitcroft
110501464f30SAndy Whitcroft		foreach my $c (split(//, $lines[$line])) {
1106f0a594c1SAndy Whitcroft			##print "C<$c>L<$level><$open$close>O<$off>\n";
1107f0a594c1SAndy Whitcroft			if ($off > 0) {
1108f0a594c1SAndy Whitcroft				$off--;
1109f0a594c1SAndy Whitcroft				next;
1110f0a594c1SAndy Whitcroft			}
11114a0df2efSAndy Whitcroft
1112f0a594c1SAndy Whitcroft			if ($c eq $close && $level > 0) {
1113f0a594c1SAndy Whitcroft				$level--;
1114f0a594c1SAndy Whitcroft				last if ($level == 0);
1115f0a594c1SAndy Whitcroft			} elsif ($c eq $open) {
1116f0a594c1SAndy Whitcroft				$level++;
1117f0a594c1SAndy Whitcroft			}
1118f0a594c1SAndy Whitcroft		}
11194a0df2efSAndy Whitcroft
1120f0a594c1SAndy Whitcroft		if (!$outer || $level <= 1) {
112100df344fSAndy Whitcroft			push(@res, $rawlines[$line]);
11224a0df2efSAndy Whitcroft		}
11234a0df2efSAndy Whitcroft
1124f0a594c1SAndy Whitcroft		last if ($level == 0);
11254a0df2efSAndy Whitcroft	}
11264a0df2efSAndy Whitcroft
1127f0a594c1SAndy Whitcroft	return ($level, @res);
11284a0df2efSAndy Whitcroft}
11294a0df2efSAndy Whitcroftsub ctx_block_outer {
11304a0df2efSAndy Whitcroft	my ($linenr, $remain) = @_;
11314a0df2efSAndy Whitcroft
1132f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
1133f0a594c1SAndy Whitcroft	return @r;
11344a0df2efSAndy Whitcroft}
11354a0df2efSAndy Whitcroftsub ctx_block {
11364a0df2efSAndy Whitcroft	my ($linenr, $remain) = @_;
11374a0df2efSAndy Whitcroft
1138f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1139f0a594c1SAndy Whitcroft	return @r;
1140653d4876SAndy Whitcroft}
1141653d4876SAndy Whitcroftsub ctx_statement {
1142f0a594c1SAndy Whitcroft	my ($linenr, $remain, $off) = @_;
1143f0a594c1SAndy Whitcroft
1144f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1145f0a594c1SAndy Whitcroft	return @r;
1146f0a594c1SAndy Whitcroft}
1147f0a594c1SAndy Whitcroftsub ctx_block_level {
1148653d4876SAndy Whitcroft	my ($linenr, $remain) = @_;
1149653d4876SAndy Whitcroft
1150f0a594c1SAndy Whitcroft	return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
11514a0df2efSAndy Whitcroft}
11529c0ca6f9SAndy Whitcroftsub ctx_statement_level {
11539c0ca6f9SAndy Whitcroft	my ($linenr, $remain, $off) = @_;
11549c0ca6f9SAndy Whitcroft
11559c0ca6f9SAndy Whitcroft	return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
11569c0ca6f9SAndy Whitcroft}
11574a0df2efSAndy Whitcroft
11584a0df2efSAndy Whitcroftsub ctx_locate_comment {
11594a0df2efSAndy Whitcroft	my ($first_line, $end_line) = @_;
11604a0df2efSAndy Whitcroft
11614a0df2efSAndy Whitcroft	# Catch a comment on the end of the line itself.
1162beae6332SAndy Whitcroft	my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
11634a0df2efSAndy Whitcroft	return $current_comment if (defined $current_comment);
11644a0df2efSAndy Whitcroft
11654a0df2efSAndy Whitcroft	# Look through the context and try and figure out if there is a
11664a0df2efSAndy Whitcroft	# comment.
11674a0df2efSAndy Whitcroft	my $in_comment = 0;
11684a0df2efSAndy Whitcroft	$current_comment = '';
11694a0df2efSAndy Whitcroft	for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
117000df344fSAndy Whitcroft		my $line = $rawlines[$linenr - 1];
117100df344fSAndy Whitcroft		#warn "           $line\n";
11724a0df2efSAndy Whitcroft		if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
11734a0df2efSAndy Whitcroft			$in_comment = 1;
11744a0df2efSAndy Whitcroft		}
11754a0df2efSAndy Whitcroft		if ($line =~ m@/\*@) {
11764a0df2efSAndy Whitcroft			$in_comment = 1;
11774a0df2efSAndy Whitcroft		}
11784a0df2efSAndy Whitcroft		if (!$in_comment && $current_comment ne '') {
11794a0df2efSAndy Whitcroft			$current_comment = '';
11804a0df2efSAndy Whitcroft		}
11814a0df2efSAndy Whitcroft		$current_comment .= $line . "\n" if ($in_comment);
11824a0df2efSAndy Whitcroft		if ($line =~ m@\*/@) {
11834a0df2efSAndy Whitcroft			$in_comment = 0;
11844a0df2efSAndy Whitcroft		}
11854a0df2efSAndy Whitcroft	}
11864a0df2efSAndy Whitcroft
11874a0df2efSAndy Whitcroft	chomp($current_comment);
11884a0df2efSAndy Whitcroft	return($current_comment);
11894a0df2efSAndy Whitcroft}
11904a0df2efSAndy Whitcroftsub ctx_has_comment {
11914a0df2efSAndy Whitcroft	my ($first_line, $end_line) = @_;
11924a0df2efSAndy Whitcroft	my $cmt = ctx_locate_comment($first_line, $end_line);
11934a0df2efSAndy Whitcroft
119400df344fSAndy Whitcroft	##print "LINE: $rawlines[$end_line - 1 ]\n";
11954a0df2efSAndy Whitcroft	##print "CMMT: $cmt\n";
11964a0df2efSAndy Whitcroft
11974a0df2efSAndy Whitcroft	return ($cmt ne '');
11984a0df2efSAndy Whitcroft}
11994a0df2efSAndy Whitcroft
12004d001e4dSAndy Whitcroftsub raw_line {
12014d001e4dSAndy Whitcroft	my ($linenr, $cnt) = @_;
12024d001e4dSAndy Whitcroft
12034d001e4dSAndy Whitcroft	my $offset = $linenr - 1;
12044d001e4dSAndy Whitcroft	$cnt++;
12054d001e4dSAndy Whitcroft
12064d001e4dSAndy Whitcroft	my $line;
12074d001e4dSAndy Whitcroft	while ($cnt) {
12084d001e4dSAndy Whitcroft		$line = $rawlines[$offset++];
12094d001e4dSAndy Whitcroft		next if (defined($line) && $line =~ /^-/);
12104d001e4dSAndy Whitcroft		$cnt--;
12114d001e4dSAndy Whitcroft	}
12124d001e4dSAndy Whitcroft
12134d001e4dSAndy Whitcroft	return $line;
12144d001e4dSAndy Whitcroft}
12154d001e4dSAndy Whitcroft
12160a920b5bSAndy Whitcroftsub cat_vet {
12170a920b5bSAndy Whitcroft	my ($vet) = @_;
12189c0ca6f9SAndy Whitcroft	my ($res, $coded);
12190a920b5bSAndy Whitcroft
12209c0ca6f9SAndy Whitcroft	$res = '';
12216c72ffaaSAndy Whitcroft	while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
12226c72ffaaSAndy Whitcroft		$res .= $1;
12236c72ffaaSAndy Whitcroft		if ($2 ne '') {
12249c0ca6f9SAndy Whitcroft			$coded = sprintf("^%c", unpack('C', $2) + 64);
12256c72ffaaSAndy Whitcroft			$res .= $coded;
12266c72ffaaSAndy Whitcroft		}
12279c0ca6f9SAndy Whitcroft	}
12289c0ca6f9SAndy Whitcroft	$res =~ s/$/\$/;
12290a920b5bSAndy Whitcroft
12309c0ca6f9SAndy Whitcroft	return $res;
12310a920b5bSAndy Whitcroft}
12320a920b5bSAndy Whitcroft
1233c2fdda0dSAndy Whitcroftmy $av_preprocessor = 0;
1234cf655043SAndy Whitcroftmy $av_pending;
1235c2fdda0dSAndy Whitcroftmy @av_paren_type;
12361f65f947SAndy Whitcroftmy $av_pend_colon;
1237c2fdda0dSAndy Whitcroft
1238c2fdda0dSAndy Whitcroftsub annotate_reset {
1239c2fdda0dSAndy Whitcroft	$av_preprocessor = 0;
1240cf655043SAndy Whitcroft	$av_pending = '_';
1241cf655043SAndy Whitcroft	@av_paren_type = ('E');
12421f65f947SAndy Whitcroft	$av_pend_colon = 'O';
1243c2fdda0dSAndy Whitcroft}
1244c2fdda0dSAndy Whitcroft
12456c72ffaaSAndy Whitcroftsub annotate_values {
12466c72ffaaSAndy Whitcroft	my ($stream, $type) = @_;
12476c72ffaaSAndy Whitcroft
12486c72ffaaSAndy Whitcroft	my $res;
12491f65f947SAndy Whitcroft	my $var = '_' x length($stream);
12506c72ffaaSAndy Whitcroft	my $cur = $stream;
12516c72ffaaSAndy Whitcroft
1252c2fdda0dSAndy Whitcroft	print "$stream\n" if ($dbg_values > 1);
12536c72ffaaSAndy Whitcroft
12546c72ffaaSAndy Whitcroft	while (length($cur)) {
1255773647a0SAndy Whitcroft		@av_paren_type = ('E') if ($#av_paren_type < 0);
1256cf655043SAndy Whitcroft		print " <" . join('', @av_paren_type) .
1257171ae1a4SAndy Whitcroft				"> <$type> <$av_pending>" if ($dbg_values > 1);
12586c72ffaaSAndy Whitcroft		if ($cur =~ /^(\s+)/o) {
1259c2fdda0dSAndy Whitcroft			print "WS($1)\n" if ($dbg_values > 1);
1260c2fdda0dSAndy Whitcroft			if ($1 =~ /\n/ && $av_preprocessor) {
1261cf655043SAndy Whitcroft				$type = pop(@av_paren_type);
1262c2fdda0dSAndy Whitcroft				$av_preprocessor = 0;
12636c72ffaaSAndy Whitcroft			}
12646c72ffaaSAndy Whitcroft
1265c023e473SFlorian Mickler		} elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
12669446ef56SAndy Whitcroft			print "CAST($1)\n" if ($dbg_values > 1);
12679446ef56SAndy Whitcroft			push(@av_paren_type, $type);
1268addcdceaSAndy Whitcroft			$type = 'c';
12699446ef56SAndy Whitcroft
1270e91b6e26SAndy Whitcroft		} elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
1271c2fdda0dSAndy Whitcroft			print "DECLARE($1)\n" if ($dbg_values > 1);
12726c72ffaaSAndy Whitcroft			$type = 'T';
12736c72ffaaSAndy Whitcroft
1274389a2fe5SAndy Whitcroft		} elsif ($cur =~ /^($Modifier)\s*/) {
1275389a2fe5SAndy Whitcroft			print "MODIFIER($1)\n" if ($dbg_values > 1);
1276389a2fe5SAndy Whitcroft			$type = 'T';
1277389a2fe5SAndy Whitcroft
1278c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
1279171ae1a4SAndy Whitcroft			print "DEFINE($1,$2)\n" if ($dbg_values > 1);
1280c2fdda0dSAndy Whitcroft			$av_preprocessor = 1;
1281171ae1a4SAndy Whitcroft			push(@av_paren_type, $type);
1282171ae1a4SAndy Whitcroft			if ($2 ne '') {
1283cf655043SAndy Whitcroft				$av_pending = 'N';
1284171ae1a4SAndy Whitcroft			}
1285171ae1a4SAndy Whitcroft			$type = 'E';
1286171ae1a4SAndy Whitcroft
1287c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
1288171ae1a4SAndy Whitcroft			print "UNDEF($1)\n" if ($dbg_values > 1);
1289171ae1a4SAndy Whitcroft			$av_preprocessor = 1;
1290171ae1a4SAndy Whitcroft			push(@av_paren_type, $type);
12916c72ffaaSAndy Whitcroft
1292c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
1293cf655043SAndy Whitcroft			print "PRE_START($1)\n" if ($dbg_values > 1);
1294c2fdda0dSAndy Whitcroft			$av_preprocessor = 1;
1295cf655043SAndy Whitcroft
1296cf655043SAndy Whitcroft			push(@av_paren_type, $type);
1297cf655043SAndy Whitcroft			push(@av_paren_type, $type);
1298171ae1a4SAndy Whitcroft			$type = 'E';
1299cf655043SAndy Whitcroft
1300c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
1301cf655043SAndy Whitcroft			print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1302cf655043SAndy Whitcroft			$av_preprocessor = 1;
1303cf655043SAndy Whitcroft
1304cf655043SAndy Whitcroft			push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1305cf655043SAndy Whitcroft
1306171ae1a4SAndy Whitcroft			$type = 'E';
1307cf655043SAndy Whitcroft
1308c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:endif))/o) {
1309cf655043SAndy Whitcroft			print "PRE_END($1)\n" if ($dbg_values > 1);
1310cf655043SAndy Whitcroft
1311cf655043SAndy Whitcroft			$av_preprocessor = 1;
1312cf655043SAndy Whitcroft
1313cf655043SAndy Whitcroft			# Assume all arms of the conditional end as this
1314cf655043SAndy Whitcroft			# one does, and continue as if the #endif was not here.
1315cf655043SAndy Whitcroft			pop(@av_paren_type);
1316cf655043SAndy Whitcroft			push(@av_paren_type, $type);
1317171ae1a4SAndy Whitcroft			$type = 'E';
13186c72ffaaSAndy Whitcroft
13196c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\\\n)/o) {
1320c2fdda0dSAndy Whitcroft			print "PRECONT($1)\n" if ($dbg_values > 1);
13216c72ffaaSAndy Whitcroft
1322171ae1a4SAndy Whitcroft		} elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1323171ae1a4SAndy Whitcroft			print "ATTR($1)\n" if ($dbg_values > 1);
1324171ae1a4SAndy Whitcroft			$av_pending = $type;
1325171ae1a4SAndy Whitcroft			$type = 'N';
1326171ae1a4SAndy Whitcroft
13276c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
1328c2fdda0dSAndy Whitcroft			print "SIZEOF($1)\n" if ($dbg_values > 1);
13296c72ffaaSAndy Whitcroft			if (defined $2) {
1330cf655043SAndy Whitcroft				$av_pending = 'V';
13316c72ffaaSAndy Whitcroft			}
13326c72ffaaSAndy Whitcroft			$type = 'N';
13336c72ffaaSAndy Whitcroft
133414b111c1SAndy Whitcroft		} elsif ($cur =~ /^(if|while|for)\b/o) {
1335c2fdda0dSAndy Whitcroft			print "COND($1)\n" if ($dbg_values > 1);
133614b111c1SAndy Whitcroft			$av_pending = 'E';
13376c72ffaaSAndy Whitcroft			$type = 'N';
13386c72ffaaSAndy Whitcroft
13391f65f947SAndy Whitcroft		} elsif ($cur =~/^(case)/o) {
13401f65f947SAndy Whitcroft			print "CASE($1)\n" if ($dbg_values > 1);
13411f65f947SAndy Whitcroft			$av_pend_colon = 'C';
13421f65f947SAndy Whitcroft			$type = 'N';
13431f65f947SAndy Whitcroft
134414b111c1SAndy Whitcroft		} elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
1345c2fdda0dSAndy Whitcroft			print "KEYWORD($1)\n" if ($dbg_values > 1);
13466c72ffaaSAndy Whitcroft			$type = 'N';
13476c72ffaaSAndy Whitcroft
13486c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\()/o) {
1349c2fdda0dSAndy Whitcroft			print "PAREN('$1')\n" if ($dbg_values > 1);
1350cf655043SAndy Whitcroft			push(@av_paren_type, $av_pending);
1351cf655043SAndy Whitcroft			$av_pending = '_';
13526c72ffaaSAndy Whitcroft			$type = 'N';
13536c72ffaaSAndy Whitcroft
13546c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\))/o) {
1355cf655043SAndy Whitcroft			my $new_type = pop(@av_paren_type);
1356cf655043SAndy Whitcroft			if ($new_type ne '_') {
1357cf655043SAndy Whitcroft				$type = $new_type;
1358c2fdda0dSAndy Whitcroft				print "PAREN('$1') -> $type\n"
1359c2fdda0dSAndy Whitcroft							if ($dbg_values > 1);
13606c72ffaaSAndy Whitcroft			} else {
1361c2fdda0dSAndy Whitcroft				print "PAREN('$1')\n" if ($dbg_values > 1);
13626c72ffaaSAndy Whitcroft			}
13636c72ffaaSAndy Whitcroft
1364c8cb2ca3SAndy Whitcroft		} elsif ($cur =~ /^($Ident)\s*\(/o) {
1365c2fdda0dSAndy Whitcroft			print "FUNC($1)\n" if ($dbg_values > 1);
1366c8cb2ca3SAndy Whitcroft			$type = 'V';
1367cf655043SAndy Whitcroft			$av_pending = 'V';
13686c72ffaaSAndy Whitcroft
13698e761b04SAndy Whitcroft		} elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
13708e761b04SAndy Whitcroft			if (defined $2 && $type eq 'C' || $type eq 'T') {
13711f65f947SAndy Whitcroft				$av_pend_colon = 'B';
13728e761b04SAndy Whitcroft			} elsif ($type eq 'E') {
13738e761b04SAndy Whitcroft				$av_pend_colon = 'L';
13741f65f947SAndy Whitcroft			}
13751f65f947SAndy Whitcroft			print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
13761f65f947SAndy Whitcroft			$type = 'V';
13771f65f947SAndy Whitcroft
13786c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Ident|$Constant)/o) {
1379c2fdda0dSAndy Whitcroft			print "IDENT($1)\n" if ($dbg_values > 1);
13806c72ffaaSAndy Whitcroft			$type = 'V';
13816c72ffaaSAndy Whitcroft
13826c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Assignment)/o) {
1383c2fdda0dSAndy Whitcroft			print "ASSIGN($1)\n" if ($dbg_values > 1);
13846c72ffaaSAndy Whitcroft			$type = 'N';
13856c72ffaaSAndy Whitcroft
1386cf655043SAndy Whitcroft		} elsif ($cur =~/^(;|{|})/) {
1387c2fdda0dSAndy Whitcroft			print "END($1)\n" if ($dbg_values > 1);
138813214adfSAndy Whitcroft			$type = 'E';
13891f65f947SAndy Whitcroft			$av_pend_colon = 'O';
139013214adfSAndy Whitcroft
13918e761b04SAndy Whitcroft		} elsif ($cur =~/^(,)/) {
13928e761b04SAndy Whitcroft			print "COMMA($1)\n" if ($dbg_values > 1);
13938e761b04SAndy Whitcroft			$type = 'C';
13948e761b04SAndy Whitcroft
13951f65f947SAndy Whitcroft		} elsif ($cur =~ /^(\?)/o) {
13961f65f947SAndy Whitcroft			print "QUESTION($1)\n" if ($dbg_values > 1);
13971f65f947SAndy Whitcroft			$type = 'N';
13981f65f947SAndy Whitcroft
13991f65f947SAndy Whitcroft		} elsif ($cur =~ /^(:)/o) {
14001f65f947SAndy Whitcroft			print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
14011f65f947SAndy Whitcroft
14021f65f947SAndy Whitcroft			substr($var, length($res), 1, $av_pend_colon);
14031f65f947SAndy Whitcroft			if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
14041f65f947SAndy Whitcroft				$type = 'E';
14051f65f947SAndy Whitcroft			} else {
14061f65f947SAndy Whitcroft				$type = 'N';
14071f65f947SAndy Whitcroft			}
14081f65f947SAndy Whitcroft			$av_pend_colon = 'O';
14091f65f947SAndy Whitcroft
14108e761b04SAndy Whitcroft		} elsif ($cur =~ /^(\[)/o) {
141113214adfSAndy Whitcroft			print "CLOSE($1)\n" if ($dbg_values > 1);
14126c72ffaaSAndy Whitcroft			$type = 'N';
14136c72ffaaSAndy Whitcroft
14140d413866SAndy Whitcroft		} elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
141574048ed8SAndy Whitcroft			my $variant;
141674048ed8SAndy Whitcroft
141774048ed8SAndy Whitcroft			print "OPV($1)\n" if ($dbg_values > 1);
141874048ed8SAndy Whitcroft			if ($type eq 'V') {
141974048ed8SAndy Whitcroft				$variant = 'B';
142074048ed8SAndy Whitcroft			} else {
142174048ed8SAndy Whitcroft				$variant = 'U';
142274048ed8SAndy Whitcroft			}
142374048ed8SAndy Whitcroft
142474048ed8SAndy Whitcroft			substr($var, length($res), 1, $variant);
142574048ed8SAndy Whitcroft			$type = 'N';
142674048ed8SAndy Whitcroft
14276c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Operators)/o) {
1428c2fdda0dSAndy Whitcroft			print "OP($1)\n" if ($dbg_values > 1);
14296c72ffaaSAndy Whitcroft			if ($1 ne '++' && $1 ne '--') {
14306c72ffaaSAndy Whitcroft				$type = 'N';
14316c72ffaaSAndy Whitcroft			}
14326c72ffaaSAndy Whitcroft
14336c72ffaaSAndy Whitcroft		} elsif ($cur =~ /(^.)/o) {
1434c2fdda0dSAndy Whitcroft			print "C($1)\n" if ($dbg_values > 1);
14356c72ffaaSAndy Whitcroft		}
14366c72ffaaSAndy Whitcroft		if (defined $1) {
14376c72ffaaSAndy Whitcroft			$cur = substr($cur, length($1));
14386c72ffaaSAndy Whitcroft			$res .= $type x length($1);
14396c72ffaaSAndy Whitcroft		}
14406c72ffaaSAndy Whitcroft	}
14416c72ffaaSAndy Whitcroft
14421f65f947SAndy Whitcroft	return ($res, $var);
14436c72ffaaSAndy Whitcroft}
14446c72ffaaSAndy Whitcroft
14458905a67cSAndy Whitcroftsub possible {
144613214adfSAndy Whitcroft	my ($possible, $line) = @_;
14479a974fdbSAndy Whitcroft	my $notPermitted = qr{(?:
14480776e594SAndy Whitcroft		^(?:
14490776e594SAndy Whitcroft			$Modifier|
14500776e594SAndy Whitcroft			$Storage|
14510776e594SAndy Whitcroft			$Type|
14529a974fdbSAndy Whitcroft			DEFINE_\S+
14539a974fdbSAndy Whitcroft		)$|
14549a974fdbSAndy Whitcroft		^(?:
14550776e594SAndy Whitcroft			goto|
14560776e594SAndy Whitcroft			return|
14570776e594SAndy Whitcroft			case|
14580776e594SAndy Whitcroft			else|
14590776e594SAndy Whitcroft			asm|__asm__|
146089a88353SAndy Whitcroft			do|
146189a88353SAndy Whitcroft			\#|
146289a88353SAndy Whitcroft			\#\#|
14639a974fdbSAndy Whitcroft		)(?:\s|$)|
14640776e594SAndy Whitcroft		^(?:typedef|struct|enum)\b
14659a974fdbSAndy Whitcroft	    )}x;
14669a974fdbSAndy Whitcroft	warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
14679a974fdbSAndy Whitcroft	if ($possible !~ $notPermitted) {
1468c45dcabdSAndy Whitcroft		# Check for modifiers.
1469c45dcabdSAndy Whitcroft		$possible =~ s/\s*$Storage\s*//g;
1470c45dcabdSAndy Whitcroft		$possible =~ s/\s*$Sparse\s*//g;
1471c45dcabdSAndy Whitcroft		if ($possible =~ /^\s*$/) {
1472c45dcabdSAndy Whitcroft
1473c45dcabdSAndy Whitcroft		} elsif ($possible =~ /\s/) {
1474c45dcabdSAndy Whitcroft			$possible =~ s/\s*$Type\s*//g;
1475d2506586SAndy Whitcroft			for my $modifier (split(' ', $possible)) {
14769a974fdbSAndy Whitcroft				if ($modifier !~ $notPermitted) {
1477d2506586SAndy Whitcroft					warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1478d2506586SAndy Whitcroft					push(@modifierList, $modifier);
1479d2506586SAndy Whitcroft				}
14809a974fdbSAndy Whitcroft			}
1481c45dcabdSAndy Whitcroft
1482c45dcabdSAndy Whitcroft		} else {
148313214adfSAndy Whitcroft			warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
14848905a67cSAndy Whitcroft			push(@typeList, $possible);
1485c45dcabdSAndy Whitcroft		}
14868905a67cSAndy Whitcroft		build_types();
14870776e594SAndy Whitcroft	} else {
14880776e594SAndy Whitcroft		warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
14898905a67cSAndy Whitcroft	}
14908905a67cSAndy Whitcroft}
14918905a67cSAndy Whitcroft
14926c72ffaaSAndy Whitcroftmy $prefix = '';
14936c72ffaaSAndy Whitcroft
1494000d1cc1SJoe Perchessub show_type {
1495cbec18afSJoe Perches	my ($type) = @_;
149691bfe484SJoe Perches
1497cbec18afSJoe Perches	return defined $use_type{$type} if (scalar keys %use_type > 0);
1498cbec18afSJoe Perches
1499cbec18afSJoe Perches	return !defined $ignore_type{$type};
1500000d1cc1SJoe Perches}
1501000d1cc1SJoe Perches
1502f0a594c1SAndy Whitcroftsub report {
1503cbec18afSJoe Perches	my ($level, $type, $msg) = @_;
1504cbec18afSJoe Perches
1505cbec18afSJoe Perches	if (!show_type($type) ||
1506cbec18afSJoe Perches	    (defined $tst_only && $msg !~ /\Q$tst_only\E/)) {
1507773647a0SAndy Whitcroft		return 0;
1508773647a0SAndy Whitcroft	}
1509000d1cc1SJoe Perches	my $line;
1510000d1cc1SJoe Perches	if ($show_types) {
1511cbec18afSJoe Perches		$line = "$prefix$level:$type: $msg\n";
1512000d1cc1SJoe Perches	} else {
1513cbec18afSJoe Perches		$line = "$prefix$level: $msg\n";
1514000d1cc1SJoe Perches	}
15158905a67cSAndy Whitcroft	$line = (split('\n', $line))[0] . "\n" if ($terse);
15168905a67cSAndy Whitcroft
151713214adfSAndy Whitcroft	push(our @report, $line);
1518773647a0SAndy Whitcroft
1519773647a0SAndy Whitcroft	return 1;
1520f0a594c1SAndy Whitcroft}
1521cbec18afSJoe Perches
1522f0a594c1SAndy Whitcroftsub report_dump {
152313214adfSAndy Whitcroft	our @report;
1524f0a594c1SAndy Whitcroft}
1525000d1cc1SJoe Perches
1526de7d4f0eSAndy Whitcroftsub ERROR {
1527cbec18afSJoe Perches	my ($type, $msg) = @_;
1528cbec18afSJoe Perches
1529cbec18afSJoe Perches	if (report("ERROR", $type, $msg)) {
1530de7d4f0eSAndy Whitcroft		our $clean = 0;
15316c72ffaaSAndy Whitcroft		our $cnt_error++;
15323705ce5bSJoe Perches		return 1;
1533de7d4f0eSAndy Whitcroft	}
15343705ce5bSJoe Perches	return 0;
1535773647a0SAndy Whitcroft}
1536de7d4f0eSAndy Whitcroftsub WARN {
1537cbec18afSJoe Perches	my ($type, $msg) = @_;
1538cbec18afSJoe Perches
1539cbec18afSJoe Perches	if (report("WARNING", $type, $msg)) {
1540de7d4f0eSAndy Whitcroft		our $clean = 0;
15416c72ffaaSAndy Whitcroft		our $cnt_warn++;
15423705ce5bSJoe Perches		return 1;
1543de7d4f0eSAndy Whitcroft	}
15443705ce5bSJoe Perches	return 0;
1545773647a0SAndy Whitcroft}
1546de7d4f0eSAndy Whitcroftsub CHK {
1547cbec18afSJoe Perches	my ($type, $msg) = @_;
1548cbec18afSJoe Perches
1549cbec18afSJoe Perches	if ($check && report("CHECK", $type, $msg)) {
1550de7d4f0eSAndy Whitcroft		our $clean = 0;
15516c72ffaaSAndy Whitcroft		our $cnt_chk++;
15523705ce5bSJoe Perches		return 1;
15536c72ffaaSAndy Whitcroft	}
15543705ce5bSJoe Perches	return 0;
1555de7d4f0eSAndy Whitcroft}
1556de7d4f0eSAndy Whitcroft
15576ecd9674SAndy Whitcroftsub check_absolute_file {
15586ecd9674SAndy Whitcroft	my ($absolute, $herecurr) = @_;
15596ecd9674SAndy Whitcroft	my $file = $absolute;
15606ecd9674SAndy Whitcroft
15616ecd9674SAndy Whitcroft	##print "absolute<$absolute>\n";
15626ecd9674SAndy Whitcroft
15636ecd9674SAndy Whitcroft	# See if any suffix of this path is a path within the tree.
15646ecd9674SAndy Whitcroft	while ($file =~ s@^[^/]*/@@) {
15656ecd9674SAndy Whitcroft		if (-f "$root/$file") {
15666ecd9674SAndy Whitcroft			##print "file<$file>\n";
15676ecd9674SAndy Whitcroft			last;
15686ecd9674SAndy Whitcroft		}
15696ecd9674SAndy Whitcroft	}
15706ecd9674SAndy Whitcroft	if (! -f _)  {
15716ecd9674SAndy Whitcroft		return 0;
15726ecd9674SAndy Whitcroft	}
15736ecd9674SAndy Whitcroft
15746ecd9674SAndy Whitcroft	# It is, so see if the prefix is acceptable.
15756ecd9674SAndy Whitcroft	my $prefix = $absolute;
15766ecd9674SAndy Whitcroft	substr($prefix, -length($file)) = '';
15776ecd9674SAndy Whitcroft
15786ecd9674SAndy Whitcroft	##print "prefix<$prefix>\n";
15796ecd9674SAndy Whitcroft	if ($prefix ne ".../") {
1580000d1cc1SJoe Perches		WARN("USE_RELATIVE_PATH",
1581000d1cc1SJoe Perches		     "use relative pathname instead of absolute in changelog text\n" . $herecurr);
15826ecd9674SAndy Whitcroft	}
15836ecd9674SAndy Whitcroft}
15846ecd9674SAndy Whitcroft
15853705ce5bSJoe Perchessub trim {
15863705ce5bSJoe Perches	my ($string) = @_;
15873705ce5bSJoe Perches
1588b34c648bSJoe Perches	$string =~ s/^\s+|\s+$//g;
1589b34c648bSJoe Perches
1590b34c648bSJoe Perches	return $string;
1591b34c648bSJoe Perches}
1592b34c648bSJoe Perches
1593b34c648bSJoe Perchessub ltrim {
1594b34c648bSJoe Perches	my ($string) = @_;
1595b34c648bSJoe Perches
1596b34c648bSJoe Perches	$string =~ s/^\s+//;
1597b34c648bSJoe Perches
1598b34c648bSJoe Perches	return $string;
1599b34c648bSJoe Perches}
1600b34c648bSJoe Perches
1601b34c648bSJoe Perchessub rtrim {
1602b34c648bSJoe Perches	my ($string) = @_;
1603b34c648bSJoe Perches
1604b34c648bSJoe Perches	$string =~ s/\s+$//;
16053705ce5bSJoe Perches
16063705ce5bSJoe Perches	return $string;
16073705ce5bSJoe Perches}
16083705ce5bSJoe Perches
160952ea8506SJoe Perchessub string_find_replace {
161052ea8506SJoe Perches	my ($string, $find, $replace) = @_;
161152ea8506SJoe Perches
161252ea8506SJoe Perches	$string =~ s/$find/$replace/g;
161352ea8506SJoe Perches
161452ea8506SJoe Perches	return $string;
161552ea8506SJoe Perches}
161652ea8506SJoe Perches
16173705ce5bSJoe Perchessub tabify {
16183705ce5bSJoe Perches	my ($leading) = @_;
16193705ce5bSJoe Perches
16203705ce5bSJoe Perches	my $source_indent = 8;
16213705ce5bSJoe Perches	my $max_spaces_before_tab = $source_indent - 1;
16223705ce5bSJoe Perches	my $spaces_to_tab = " " x $source_indent;
16233705ce5bSJoe Perches
16243705ce5bSJoe Perches	#convert leading spaces to tabs
16253705ce5bSJoe Perches	1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
16263705ce5bSJoe Perches	#Remove spaces before a tab
16273705ce5bSJoe Perches	1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
16283705ce5bSJoe Perches
16293705ce5bSJoe Perches	return "$leading";
16303705ce5bSJoe Perches}
16313705ce5bSJoe Perches
1632d1fe9c09SJoe Perchessub pos_last_openparen {
1633d1fe9c09SJoe Perches	my ($line) = @_;
1634d1fe9c09SJoe Perches
1635d1fe9c09SJoe Perches	my $pos = 0;
1636d1fe9c09SJoe Perches
1637d1fe9c09SJoe Perches	my $opens = $line =~ tr/\(/\(/;
1638d1fe9c09SJoe Perches	my $closes = $line =~ tr/\)/\)/;
1639d1fe9c09SJoe Perches
1640d1fe9c09SJoe Perches	my $last_openparen = 0;
1641d1fe9c09SJoe Perches
1642d1fe9c09SJoe Perches	if (($opens == 0) || ($closes >= $opens)) {
1643d1fe9c09SJoe Perches		return -1;
1644d1fe9c09SJoe Perches	}
1645d1fe9c09SJoe Perches
1646d1fe9c09SJoe Perches	my $len = length($line);
1647d1fe9c09SJoe Perches
1648d1fe9c09SJoe Perches	for ($pos = 0; $pos < $len; $pos++) {
1649d1fe9c09SJoe Perches		my $string = substr($line, $pos);
1650d1fe9c09SJoe Perches		if ($string =~ /^($FuncArg|$balanced_parens)/) {
1651d1fe9c09SJoe Perches			$pos += length($1) - 1;
1652d1fe9c09SJoe Perches		} elsif (substr($line, $pos, 1) eq '(') {
1653d1fe9c09SJoe Perches			$last_openparen = $pos;
1654d1fe9c09SJoe Perches		} elsif (index($string, '(') == -1) {
1655d1fe9c09SJoe Perches			last;
1656d1fe9c09SJoe Perches		}
1657d1fe9c09SJoe Perches	}
1658d1fe9c09SJoe Perches
165991cb5195SJoe Perches	return length(expand_tabs(substr($line, 0, $last_openparen))) + 1;
1660d1fe9c09SJoe Perches}
1661d1fe9c09SJoe Perches
16620a920b5bSAndy Whitcroftsub process {
16630a920b5bSAndy Whitcroft	my $filename = shift;
16640a920b5bSAndy Whitcroft
16650a920b5bSAndy Whitcroft	my $linenr=0;
16660a920b5bSAndy Whitcroft	my $prevline="";
1667c2fdda0dSAndy Whitcroft	my $prevrawline="";
16680a920b5bSAndy Whitcroft	my $stashline="";
1669c2fdda0dSAndy Whitcroft	my $stashrawline="";
16700a920b5bSAndy Whitcroft
16714a0df2efSAndy Whitcroft	my $length;
16720a920b5bSAndy Whitcroft	my $indent;
16730a920b5bSAndy Whitcroft	my $previndent=0;
16740a920b5bSAndy Whitcroft	my $stashindent=0;
16750a920b5bSAndy Whitcroft
1676de7d4f0eSAndy Whitcroft	our $clean = 1;
16770a920b5bSAndy Whitcroft	my $signoff = 0;
16780a920b5bSAndy Whitcroft	my $is_patch = 0;
16790a920b5bSAndy Whitcroft
168029ee1b0cSJoe Perches	my $in_header_lines = $file ? 0 : 1;
168115662b3eSJoe Perches	my $in_commit_log = 0;		#Scanning lines before patch
1682*13f1937eSJoe Perches	my $reported_maintainer_file = 0;
1683fa64205dSPasi Savanainen	my $non_utf8_charset = 0;
1684fa64205dSPasi Savanainen
1685365dd4eaSJoe Perches	my $last_blank_line = 0;
1686365dd4eaSJoe Perches
168713214adfSAndy Whitcroft	our @report = ();
16886c72ffaaSAndy Whitcroft	our $cnt_lines = 0;
16896c72ffaaSAndy Whitcroft	our $cnt_error = 0;
16906c72ffaaSAndy Whitcroft	our $cnt_warn = 0;
16916c72ffaaSAndy Whitcroft	our $cnt_chk = 0;
16926c72ffaaSAndy Whitcroft
16930a920b5bSAndy Whitcroft	# Trace the real file/line as we go.
16940a920b5bSAndy Whitcroft	my $realfile = '';
16950a920b5bSAndy Whitcroft	my $realline = 0;
16960a920b5bSAndy Whitcroft	my $realcnt = 0;
16970a920b5bSAndy Whitcroft	my $here = '';
16980a920b5bSAndy Whitcroft	my $in_comment = 0;
1699c2fdda0dSAndy Whitcroft	my $comment_edge = 0;
17000a920b5bSAndy Whitcroft	my $first_line = 0;
17011e855726SWolfram Sang	my $p1_prefix = '';
17020a920b5bSAndy Whitcroft
170313214adfSAndy Whitcroft	my $prev_values = 'E';
170413214adfSAndy Whitcroft
170513214adfSAndy Whitcroft	# suppression flags
1706773647a0SAndy Whitcroft	my %suppress_ifbraces;
1707170d3a22SAndy Whitcroft	my %suppress_whiletrailers;
17082b474a1aSAndy Whitcroft	my %suppress_export;
17093e469cdcSAndy Whitcroft	my $suppress_statement = 0;
1710653d4876SAndy Whitcroft
17117e51f197SJoe Perches	my %signatures = ();
1712323c1260SJoe Perches
1713c2fdda0dSAndy Whitcroft	# Pre-scan the patch sanitizing the lines.
1714de7d4f0eSAndy Whitcroft	# Pre-scan the patch looking for any __setup documentation.
1715c2fdda0dSAndy Whitcroft	#
1716de7d4f0eSAndy Whitcroft	my @setup_docs = ();
1717de7d4f0eSAndy Whitcroft	my $setup_docs = 0;
1718773647a0SAndy Whitcroft
1719d8b07710SJoe Perches	my $camelcase_file_seeded = 0;
1720d8b07710SJoe Perches
1721773647a0SAndy Whitcroft	sanitise_line_reset();
1722c2fdda0dSAndy Whitcroft	my $line;
1723c2fdda0dSAndy Whitcroft	foreach my $rawline (@rawlines) {
1724773647a0SAndy Whitcroft		$linenr++;
1725773647a0SAndy Whitcroft		$line = $rawline;
1726c2fdda0dSAndy Whitcroft
17273705ce5bSJoe Perches		push(@fixed, $rawline) if ($fix);
17283705ce5bSJoe Perches
1729773647a0SAndy Whitcroft		if ($rawline=~/^\+\+\+\s+(\S+)/) {
1730de7d4f0eSAndy Whitcroft			$setup_docs = 0;
1731de7d4f0eSAndy Whitcroft			if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
1732de7d4f0eSAndy Whitcroft				$setup_docs = 1;
1733de7d4f0eSAndy Whitcroft			}
1734773647a0SAndy Whitcroft			#next;
1735de7d4f0eSAndy Whitcroft		}
1736773647a0SAndy Whitcroft		if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1737773647a0SAndy Whitcroft			$realline=$1-1;
1738773647a0SAndy Whitcroft			if (defined $2) {
1739773647a0SAndy Whitcroft				$realcnt=$3+1;
1740773647a0SAndy Whitcroft			} else {
1741773647a0SAndy Whitcroft				$realcnt=1+1;
1742773647a0SAndy Whitcroft			}
1743c45dcabdSAndy Whitcroft			$in_comment = 0;
1744773647a0SAndy Whitcroft
1745773647a0SAndy Whitcroft			# Guestimate if this is a continuing comment.  Run
1746773647a0SAndy Whitcroft			# the context looking for a comment "edge".  If this
1747773647a0SAndy Whitcroft			# edge is a close comment then we must be in a comment
1748773647a0SAndy Whitcroft			# at context start.
1749773647a0SAndy Whitcroft			my $edge;
175001fa9147SAndy Whitcroft			my $cnt = $realcnt;
175101fa9147SAndy Whitcroft			for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
175201fa9147SAndy Whitcroft				next if (defined $rawlines[$ln - 1] &&
175301fa9147SAndy Whitcroft					 $rawlines[$ln - 1] =~ /^-/);
175401fa9147SAndy Whitcroft				$cnt--;
175501fa9147SAndy Whitcroft				#print "RAW<$rawlines[$ln - 1]>\n";
1756721c1cb6SAndy Whitcroft				last if (!defined $rawlines[$ln - 1]);
1757fae17daeSAndy Whitcroft				if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
1758fae17daeSAndy Whitcroft				    $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
1759fae17daeSAndy Whitcroft					($edge) = $1;
1760fae17daeSAndy Whitcroft					last;
1761fae17daeSAndy Whitcroft				}
1762773647a0SAndy Whitcroft			}
1763773647a0SAndy Whitcroft			if (defined $edge && $edge eq '*/') {
1764773647a0SAndy Whitcroft				$in_comment = 1;
1765773647a0SAndy Whitcroft			}
1766773647a0SAndy Whitcroft
1767773647a0SAndy Whitcroft			# Guestimate if this is a continuing comment.  If this
1768773647a0SAndy Whitcroft			# is the start of a diff block and this line starts
1769773647a0SAndy Whitcroft			# ' *' then it is very likely a comment.
1770773647a0SAndy Whitcroft			if (!defined $edge &&
177183242e0cSAndy Whitcroft			    $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
1772773647a0SAndy Whitcroft			{
1773773647a0SAndy Whitcroft				$in_comment = 1;
1774773647a0SAndy Whitcroft			}
1775773647a0SAndy Whitcroft
1776773647a0SAndy Whitcroft			##print "COMMENT:$in_comment edge<$edge> $rawline\n";
1777773647a0SAndy Whitcroft			sanitise_line_reset($in_comment);
1778773647a0SAndy Whitcroft
1779171ae1a4SAndy Whitcroft		} elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
1780773647a0SAndy Whitcroft			# Standardise the strings and chars within the input to
1781171ae1a4SAndy Whitcroft			# simplify matching -- only bother with positive lines.
1782773647a0SAndy Whitcroft			$line = sanitise_line($rawline);
1783773647a0SAndy Whitcroft		}
1784773647a0SAndy Whitcroft		push(@lines, $line);
1785773647a0SAndy Whitcroft
1786773647a0SAndy Whitcroft		if ($realcnt > 1) {
1787773647a0SAndy Whitcroft			$realcnt-- if ($line =~ /^(?:\+| |$)/);
1788773647a0SAndy Whitcroft		} else {
1789773647a0SAndy Whitcroft			$realcnt = 0;
1790773647a0SAndy Whitcroft		}
1791773647a0SAndy Whitcroft
1792773647a0SAndy Whitcroft		#print "==>$rawline\n";
1793773647a0SAndy Whitcroft		#print "-->$line\n";
1794de7d4f0eSAndy Whitcroft
1795de7d4f0eSAndy Whitcroft		if ($setup_docs && $line =~ /^\+/) {
1796de7d4f0eSAndy Whitcroft			push(@setup_docs, $line);
1797de7d4f0eSAndy Whitcroft		}
1798de7d4f0eSAndy Whitcroft	}
1799de7d4f0eSAndy Whitcroft
18006c72ffaaSAndy Whitcroft	$prefix = '';
18016c72ffaaSAndy Whitcroft
1802773647a0SAndy Whitcroft	$realcnt = 0;
1803773647a0SAndy Whitcroft	$linenr = 0;
18040a920b5bSAndy Whitcroft	foreach my $line (@lines) {
18050a920b5bSAndy Whitcroft		$linenr++;
18061b5539b1SJoe Perches		my $sline = $line;	#copy of $line
18071b5539b1SJoe Perches		$sline =~ s/$;/ /g;	#with comments as spaces
18080a920b5bSAndy Whitcroft
1809c2fdda0dSAndy Whitcroft		my $rawline = $rawlines[$linenr - 1];
18106c72ffaaSAndy Whitcroft
18110a920b5bSAndy Whitcroft#extract the line range in the file after the patch is applied
18126c72ffaaSAndy Whitcroft		if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
18130a920b5bSAndy Whitcroft			$is_patch = 1;
18144a0df2efSAndy Whitcroft			$first_line = $linenr + 1;
18150a920b5bSAndy Whitcroft			$realline=$1-1;
18160a920b5bSAndy Whitcroft			if (defined $2) {
18170a920b5bSAndy Whitcroft				$realcnt=$3+1;
18180a920b5bSAndy Whitcroft			} else {
18190a920b5bSAndy Whitcroft				$realcnt=1+1;
18200a920b5bSAndy Whitcroft			}
1821c2fdda0dSAndy Whitcroft			annotate_reset();
182213214adfSAndy Whitcroft			$prev_values = 'E';
182313214adfSAndy Whitcroft
1824773647a0SAndy Whitcroft			%suppress_ifbraces = ();
1825170d3a22SAndy Whitcroft			%suppress_whiletrailers = ();
18262b474a1aSAndy Whitcroft			%suppress_export = ();
18273e469cdcSAndy Whitcroft			$suppress_statement = 0;
18280a920b5bSAndy Whitcroft			next;
18290a920b5bSAndy Whitcroft
18304a0df2efSAndy Whitcroft# track the line number as we move through the hunk, note that
18314a0df2efSAndy Whitcroft# new versions of GNU diff omit the leading space on completely
18324a0df2efSAndy Whitcroft# blank context lines so we need to count that too.
1833773647a0SAndy Whitcroft		} elsif ($line =~ /^( |\+|$)/) {
18340a920b5bSAndy Whitcroft			$realline++;
1835d8aaf121SAndy Whitcroft			$realcnt-- if ($realcnt != 0);
18360a920b5bSAndy Whitcroft
18374a0df2efSAndy Whitcroft			# Measure the line length and indent.
1838c2fdda0dSAndy Whitcroft			($length, $indent) = line_stats($rawline);
18390a920b5bSAndy Whitcroft
18400a920b5bSAndy Whitcroft			# Track the previous line.
18410a920b5bSAndy Whitcroft			($prevline, $stashline) = ($stashline, $line);
18420a920b5bSAndy Whitcroft			($previndent, $stashindent) = ($stashindent, $indent);
1843c2fdda0dSAndy Whitcroft			($prevrawline, $stashrawline) = ($stashrawline, $rawline);
1844c2fdda0dSAndy Whitcroft
1845773647a0SAndy Whitcroft			#warn "line<$line>\n";
18466c72ffaaSAndy Whitcroft
1847d8aaf121SAndy Whitcroft		} elsif ($realcnt == 1) {
1848d8aaf121SAndy Whitcroft			$realcnt--;
18490a920b5bSAndy Whitcroft		}
18500a920b5bSAndy Whitcroft
1851cc77cdcaSAndy Whitcroft		my $hunk_line = ($realcnt != 0);
1852cc77cdcaSAndy Whitcroft
18530a920b5bSAndy Whitcroft#make up the handle for any error we report on this line
1854773647a0SAndy Whitcroft		$prefix = "$filename:$realline: " if ($emacs && $file);
1855773647a0SAndy Whitcroft		$prefix = "$filename:$linenr: " if ($emacs && !$file);
1856773647a0SAndy Whitcroft
18576c72ffaaSAndy Whitcroft		$here = "#$linenr: " if (!$file);
18586c72ffaaSAndy Whitcroft		$here = "#$realline: " if ($file);
1859773647a0SAndy Whitcroft
18602ac73b4fSJoe Perches		my $found_file = 0;
1861773647a0SAndy Whitcroft		# extract the filename as it passes
18623bf9a009SRabin Vincent		if ($line =~ /^diff --git.*?(\S+)$/) {
18633bf9a009SRabin Vincent			$realfile = $1;
18642b7ab453SJoe Perches			$realfile =~ s@^([^/]*)/@@ if (!$file);
1865270c49a0SJoe Perches			$in_commit_log = 0;
18662ac73b4fSJoe Perches			$found_file = 1;
18673bf9a009SRabin Vincent		} elsif ($line =~ /^\+\+\+\s+(\S+)/) {
1868773647a0SAndy Whitcroft			$realfile = $1;
18692b7ab453SJoe Perches			$realfile =~ s@^([^/]*)/@@ if (!$file);
1870270c49a0SJoe Perches			$in_commit_log = 0;
18711e855726SWolfram Sang
18721e855726SWolfram Sang			$p1_prefix = $1;
1873e2f7aa4bSAndy Whitcroft			if (!$file && $tree && $p1_prefix ne '' &&
1874e2f7aa4bSAndy Whitcroft			    -e "$root/$p1_prefix") {
1875000d1cc1SJoe Perches				WARN("PATCH_PREFIX",
1876000d1cc1SJoe Perches				     "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
18771e855726SWolfram Sang			}
1878773647a0SAndy Whitcroft
1879c1ab3326SAndy Whitcroft			if ($realfile =~ m@^include/asm/@) {
1880000d1cc1SJoe Perches				ERROR("MODIFIED_INCLUDE_ASM",
1881000d1cc1SJoe Perches				      "do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
1882773647a0SAndy Whitcroft			}
18832ac73b4fSJoe Perches			$found_file = 1;
18842ac73b4fSJoe Perches		}
18852ac73b4fSJoe Perches
18862ac73b4fSJoe Perches		if ($found_file) {
18872ac73b4fSJoe Perches			if ($realfile =~ m@^(drivers/net/|net/)@) {
18882ac73b4fSJoe Perches				$check = 1;
18892ac73b4fSJoe Perches			} else {
18902ac73b4fSJoe Perches				$check = $check_orig;
18912ac73b4fSJoe Perches			}
1892773647a0SAndy Whitcroft			next;
1893773647a0SAndy Whitcroft		}
1894773647a0SAndy Whitcroft
1895389834b6SRandy Dunlap		$here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
18960a920b5bSAndy Whitcroft
1897c2fdda0dSAndy Whitcroft		my $hereline = "$here\n$rawline\n";
1898c2fdda0dSAndy Whitcroft		my $herecurr = "$here\n$rawline\n";
1899c2fdda0dSAndy Whitcroft		my $hereprev = "$here\n$prevrawline\n$rawline\n";
19000a920b5bSAndy Whitcroft
19016c72ffaaSAndy Whitcroft		$cnt_lines++ if ($realcnt != 0);
19026c72ffaaSAndy Whitcroft
19033bf9a009SRabin Vincent# Check for incorrect file permissions
19043bf9a009SRabin Vincent		if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
19053bf9a009SRabin Vincent			my $permhere = $here . "FILE: $realfile\n";
190604db4d25SJoe Perches			if ($realfile !~ m@scripts/@ &&
190704db4d25SJoe Perches			    $realfile !~ /\.(py|pl|awk|sh)$/) {
1908000d1cc1SJoe Perches				ERROR("EXECUTE_PERMISSIONS",
1909000d1cc1SJoe Perches				      "do not set execute permissions for source files\n" . $permhere);
19103bf9a009SRabin Vincent			}
19113bf9a009SRabin Vincent		}
19123bf9a009SRabin Vincent
191320112475SJoe Perches# Check the patch for a signoff:
1914d8aaf121SAndy Whitcroft		if ($line =~ /^\s*signed-off-by:/i) {
19154a0df2efSAndy Whitcroft			$signoff++;
191615662b3eSJoe Perches			$in_commit_log = 0;
19170a920b5bSAndy Whitcroft		}
191820112475SJoe Perches
191920112475SJoe Perches# Check signature styles
1920270c49a0SJoe Perches		if (!$in_header_lines &&
1921ce0338dfSJoe Perches		    $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
192220112475SJoe Perches			my $space_before = $1;
192320112475SJoe Perches			my $sign_off = $2;
192420112475SJoe Perches			my $space_after = $3;
192520112475SJoe Perches			my $email = $4;
192620112475SJoe Perches			my $ucfirst_sign_off = ucfirst(lc($sign_off));
192720112475SJoe Perches
1928ce0338dfSJoe Perches			if ($sign_off !~ /$signature_tags/) {
1929ce0338dfSJoe Perches				WARN("BAD_SIGN_OFF",
1930ce0338dfSJoe Perches				     "Non-standard signature: $sign_off\n" . $herecurr);
1931ce0338dfSJoe Perches			}
193220112475SJoe Perches			if (defined $space_before && $space_before ne "") {
19333705ce5bSJoe Perches				if (WARN("BAD_SIGN_OFF",
19343705ce5bSJoe Perches					 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
19353705ce5bSJoe Perches				    $fix) {
19363705ce5bSJoe Perches					$fixed[$linenr - 1] =
19373705ce5bSJoe Perches					    "$ucfirst_sign_off $email";
19383705ce5bSJoe Perches				}
193920112475SJoe Perches			}
194020112475SJoe Perches			if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
19413705ce5bSJoe Perches				if (WARN("BAD_SIGN_OFF",
19423705ce5bSJoe Perches					 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
19433705ce5bSJoe Perches				    $fix) {
19443705ce5bSJoe Perches					$fixed[$linenr - 1] =
19453705ce5bSJoe Perches					    "$ucfirst_sign_off $email";
19463705ce5bSJoe Perches				}
19473705ce5bSJoe Perches
194820112475SJoe Perches			}
194920112475SJoe Perches			if (!defined $space_after || $space_after ne " ") {
19503705ce5bSJoe Perches				if (WARN("BAD_SIGN_OFF",
19513705ce5bSJoe Perches					 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
19523705ce5bSJoe Perches				    $fix) {
19533705ce5bSJoe Perches					$fixed[$linenr - 1] =
19543705ce5bSJoe Perches					    "$ucfirst_sign_off $email";
19553705ce5bSJoe Perches				}
195620112475SJoe Perches			}
195720112475SJoe Perches
195820112475SJoe Perches			my ($email_name, $email_address, $comment) = parse_email($email);
195920112475SJoe Perches			my $suggested_email = format_email(($email_name, $email_address));
196020112475SJoe Perches			if ($suggested_email eq "") {
1961000d1cc1SJoe Perches				ERROR("BAD_SIGN_OFF",
1962000d1cc1SJoe Perches				      "Unrecognized email address: '$email'\n" . $herecurr);
196320112475SJoe Perches			} else {
196420112475SJoe Perches				my $dequoted = $suggested_email;
196520112475SJoe Perches				$dequoted =~ s/^"//;
196620112475SJoe Perches				$dequoted =~ s/" </ </;
196720112475SJoe Perches				# Don't force email to have quotes
196820112475SJoe Perches				# Allow just an angle bracketed address
196920112475SJoe Perches				if ("$dequoted$comment" ne $email &&
197020112475SJoe Perches				    "<$email_address>$comment" ne $email &&
197120112475SJoe Perches				    "$suggested_email$comment" ne $email) {
1972000d1cc1SJoe Perches					WARN("BAD_SIGN_OFF",
1973000d1cc1SJoe Perches					     "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
197420112475SJoe Perches				}
19750a920b5bSAndy Whitcroft			}
19767e51f197SJoe Perches
19777e51f197SJoe Perches# Check for duplicate signatures
19787e51f197SJoe Perches			my $sig_nospace = $line;
19797e51f197SJoe Perches			$sig_nospace =~ s/\s//g;
19807e51f197SJoe Perches			$sig_nospace = lc($sig_nospace);
19817e51f197SJoe Perches			if (defined $signatures{$sig_nospace}) {
19827e51f197SJoe Perches				WARN("BAD_SIGN_OFF",
19837e51f197SJoe Perches				     "Duplicate signature\n" . $herecurr);
19847e51f197SJoe Perches			} else {
19857e51f197SJoe Perches				$signatures{$sig_nospace} = 1;
19867e51f197SJoe Perches			}
19870a920b5bSAndy Whitcroft		}
19880a920b5bSAndy Whitcroft
19899b3189ebSJoe Perches# Check for old stable address
19909b3189ebSJoe Perches		if ($line =~ /^\s*cc:\s*.*<?\bstable\@kernel\.org\b>?.*$/i) {
19919b3189ebSJoe Perches			ERROR("STABLE_ADDRESS",
19929b3189ebSJoe Perches			      "The 'stable' address should be 'stable\@vger.kernel.org'\n" . $herecurr);
19939b3189ebSJoe Perches		}
19949b3189ebSJoe Perches
19957ebd05efSChristopher Covington# Check for unwanted Gerrit info
19967ebd05efSChristopher Covington		if ($in_commit_log && $line =~ /^\s*change-id:/i) {
19977ebd05efSChristopher Covington			ERROR("GERRIT_CHANGE_ID",
19987ebd05efSChristopher Covington			      "Remove Gerrit Change-Id's before submitting upstream.\n" . $herecurr);
19997ebd05efSChristopher Covington		}
20007ebd05efSChristopher Covington
2001d311cd44SJoe Perches# Check for improperly formed commit descriptions
2002d311cd44SJoe Perches		if ($in_commit_log &&
2003d311cd44SJoe Perches		    $line =~ /\bcommit\s+[0-9a-f]{5,}/i &&
2004d311cd44SJoe Perches		    $line !~ /\b[Cc]ommit [0-9a-f]{12,16} \("/) {
2005d311cd44SJoe Perches			$line =~ /\b(c)ommit\s+([0-9a-f]{5,})/i;
2006d311cd44SJoe Perches			my $init_char = $1;
2007d311cd44SJoe Perches			my $orig_commit = lc($2);
2008d311cd44SJoe Perches			my $id = '01234567890ab';
2009d311cd44SJoe Perches			my $desc = 'commit description';
2010d311cd44SJoe Perches		        ($id, $desc) = git_commit_info($orig_commit, $id, $desc);
2011d311cd44SJoe Perches			ERROR("GIT_COMMIT_ID",
2012d311cd44SJoe Perches			      "Please use 12 to 16 chars for the git commit ID like: '${init_char}ommit $id (\"$desc\")'\n" . $herecurr);
2013d311cd44SJoe Perches		}
2014d311cd44SJoe Perches
2015*13f1937eSJoe Perches# Check for added, moved or deleted files
2016*13f1937eSJoe Perches		if (!$reported_maintainer_file && !$in_commit_log &&
2017*13f1937eSJoe Perches		    ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ ||
2018*13f1937eSJoe Perches		     $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ ||
2019*13f1937eSJoe Perches		     ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ &&
2020*13f1937eSJoe Perches		      (defined($1) || defined($2))))) {
2021*13f1937eSJoe Perches			$reported_maintainer_file = 1;
2022*13f1937eSJoe Perches			WARN("FILE_PATH_CHANGES",
2023*13f1937eSJoe Perches			     "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr);
2024*13f1937eSJoe Perches		}
2025*13f1937eSJoe Perches
202600df344fSAndy Whitcroft# Check for wrappage within a valid hunk of the file
20278905a67cSAndy Whitcroft		if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
2028000d1cc1SJoe Perches			ERROR("CORRUPTED_PATCH",
2029000d1cc1SJoe Perches			      "patch seems to be corrupt (line wrapped?)\n" .
20306c72ffaaSAndy Whitcroft				$herecurr) if (!$emitted_corrupt++);
2031de7d4f0eSAndy Whitcroft		}
2032de7d4f0eSAndy Whitcroft
20336ecd9674SAndy Whitcroft# Check for absolute kernel paths.
20346ecd9674SAndy Whitcroft		if ($tree) {
20356ecd9674SAndy Whitcroft			while ($line =~ m{(?:^|\s)(/\S*)}g) {
20366ecd9674SAndy Whitcroft				my $file = $1;
20376ecd9674SAndy Whitcroft
20386ecd9674SAndy Whitcroft				if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
20396ecd9674SAndy Whitcroft				    check_absolute_file($1, $herecurr)) {
20406ecd9674SAndy Whitcroft					#
20416ecd9674SAndy Whitcroft				} else {
20426ecd9674SAndy Whitcroft					check_absolute_file($file, $herecurr);
20436ecd9674SAndy Whitcroft				}
20446ecd9674SAndy Whitcroft			}
20456ecd9674SAndy Whitcroft		}
20466ecd9674SAndy Whitcroft
2047de7d4f0eSAndy Whitcroft# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
2048de7d4f0eSAndy Whitcroft		if (($realfile =~ /^$/ || $line =~ /^\+/) &&
2049171ae1a4SAndy Whitcroft		    $rawline !~ m/^$UTF8*$/) {
2050171ae1a4SAndy Whitcroft			my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
2051171ae1a4SAndy Whitcroft
2052171ae1a4SAndy Whitcroft			my $blank = copy_spacing($rawline);
2053171ae1a4SAndy Whitcroft			my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
2054171ae1a4SAndy Whitcroft			my $hereptr = "$hereline$ptr\n";
2055171ae1a4SAndy Whitcroft
205634d99219SJoe Perches			CHK("INVALID_UTF8",
2057000d1cc1SJoe Perches			    "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
205800df344fSAndy Whitcroft		}
20590a920b5bSAndy Whitcroft
206015662b3eSJoe Perches# Check if it's the start of a commit log
206115662b3eSJoe Perches# (not a header line and we haven't seen the patch filename)
206215662b3eSJoe Perches		if ($in_header_lines && $realfile =~ /^$/ &&
206329ee1b0cSJoe Perches		    !($rawline =~ /^\s+\S/ ||
206429ee1b0cSJoe Perches		      $rawline =~ /^(commit\b|from\b|[\w-]+:).*$/i)) {
206515662b3eSJoe Perches			$in_header_lines = 0;
206615662b3eSJoe Perches			$in_commit_log = 1;
206715662b3eSJoe Perches		}
206815662b3eSJoe Perches
2069fa64205dSPasi Savanainen# Check if there is UTF-8 in a commit log when a mail header has explicitly
2070fa64205dSPasi Savanainen# declined it, i.e defined some charset where it is missing.
2071fa64205dSPasi Savanainen		if ($in_header_lines &&
2072fa64205dSPasi Savanainen		    $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
2073fa64205dSPasi Savanainen		    $1 !~ /utf-8/i) {
2074fa64205dSPasi Savanainen			$non_utf8_charset = 1;
2075fa64205dSPasi Savanainen		}
2076fa64205dSPasi Savanainen
2077fa64205dSPasi Savanainen		if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
207815662b3eSJoe Perches		    $rawline =~ /$NON_ASCII_UTF8/) {
2079fa64205dSPasi Savanainen			WARN("UTF8_BEFORE_PATCH",
208015662b3eSJoe Perches			    "8-bit UTF-8 used in possible commit log\n" . $herecurr);
208115662b3eSJoe Perches		}
208215662b3eSJoe Perches
208330670854SAndy Whitcroft# ignore non-hunk lines and lines being removed
208430670854SAndy Whitcroft		next if (!$hunk_line || $line =~ /^-/);
208500df344fSAndy Whitcroft
20860a920b5bSAndy Whitcroft#trailing whitespace
20879c0ca6f9SAndy Whitcroft		if ($line =~ /^\+.*\015/) {
2088c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2089d5e616fcSJoe Perches			if (ERROR("DOS_LINE_ENDINGS",
2090d5e616fcSJoe Perches				  "DOS line endings\n" . $herevet) &&
2091d5e616fcSJoe Perches			    $fix) {
2092d5e616fcSJoe Perches				$fixed[$linenr - 1] =~ s/[\s\015]+$//;
2093d5e616fcSJoe Perches			}
2094c2fdda0dSAndy Whitcroft		} elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
2095c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
20963705ce5bSJoe Perches			if (ERROR("TRAILING_WHITESPACE",
20973705ce5bSJoe Perches				  "trailing whitespace\n" . $herevet) &&
20983705ce5bSJoe Perches			    $fix) {
2099d5e616fcSJoe Perches				$fixed[$linenr - 1] =~ s/\s+$//;
21003705ce5bSJoe Perches			}
21013705ce5bSJoe Perches
2102d2c0a235SAndy Whitcroft			$rpt_cleaners = 1;
21030a920b5bSAndy Whitcroft		}
21045368df20SAndy Whitcroft
21054783f894SJosh Triplett# Check for FSF mailing addresses.
2106109d8cb2SAlexander Duyck		if ($rawline =~ /\bwrite to the Free/i ||
21073e2232f2SJoe Perches		    $rawline =~ /\b59\s+Temple\s+Pl/i ||
21083e2232f2SJoe Perches		    $rawline =~ /\b51\s+Franklin\s+St/i) {
21094783f894SJosh Triplett			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
21104783f894SJosh Triplett			my $msg_type = \&ERROR;
21114783f894SJosh Triplett			$msg_type = \&CHK if ($file);
21124783f894SJosh Triplett			&{$msg_type}("FSF_MAILING_ADDRESS",
21134783f894SJosh 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)
21144783f894SJosh Triplett		}
21154783f894SJosh Triplett
21163354957aSAndi Kleen# check for Kconfig help text having a real description
21179fe287d7SAndy Whitcroft# Only applies when adding the entry originally, after that we do not have
21189fe287d7SAndy Whitcroft# sufficient context to determine whether it is indeed long enough.
21193354957aSAndi Kleen		if ($realfile =~ /Kconfig/ &&
21208d73e0e7SJoe Perches		    $line =~ /^\+\s*config\s+/) {
21213354957aSAndi Kleen			my $length = 0;
21229fe287d7SAndy Whitcroft			my $cnt = $realcnt;
21239fe287d7SAndy Whitcroft			my $ln = $linenr + 1;
21249fe287d7SAndy Whitcroft			my $f;
2125a1385803SAndy Whitcroft			my $is_start = 0;
21269fe287d7SAndy Whitcroft			my $is_end = 0;
2127a1385803SAndy Whitcroft			for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
21289fe287d7SAndy Whitcroft				$f = $lines[$ln - 1];
21299fe287d7SAndy Whitcroft				$cnt-- if ($lines[$ln - 1] !~ /^-/);
21309fe287d7SAndy Whitcroft				$is_end = $lines[$ln - 1] =~ /^\+/;
21319fe287d7SAndy Whitcroft
21329fe287d7SAndy Whitcroft				next if ($f =~ /^-/);
21338d73e0e7SJoe Perches				last if (!$file && $f =~ /^\@\@/);
2134a1385803SAndy Whitcroft
21358d73e0e7SJoe Perches				if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate)\s*\"/) {
2136a1385803SAndy Whitcroft					$is_start = 1;
21378d73e0e7SJoe Perches				} elsif ($lines[$ln - 1] =~ /^\+\s*(?:---)?help(?:---)?$/) {
2138a1385803SAndy Whitcroft					$length = -1;
2139a1385803SAndy Whitcroft				}
2140a1385803SAndy Whitcroft
21419fe287d7SAndy Whitcroft				$f =~ s/^.//;
21423354957aSAndi Kleen				$f =~ s/#.*//;
21433354957aSAndi Kleen				$f =~ s/^\s+//;
21443354957aSAndi Kleen				next if ($f =~ /^$/);
21459fe287d7SAndy Whitcroft				if ($f =~ /^\s*config\s/) {
21469fe287d7SAndy Whitcroft					$is_end = 1;
21479fe287d7SAndy Whitcroft					last;
21489fe287d7SAndy Whitcroft				}
21493354957aSAndi Kleen				$length++;
21503354957aSAndi Kleen			}
2151000d1cc1SJoe Perches			WARN("CONFIG_DESCRIPTION",
2152a1385803SAndy Whitcroft			     "please write a paragraph that describes the config symbol fully\n" . $herecurr) if ($is_start && $is_end && $length < 4);
2153a1385803SAndy Whitcroft			#print "is_start<$is_start> is_end<$is_end> length<$length>\n";
21543354957aSAndi Kleen		}
21553354957aSAndi Kleen
21561ba8dfd1SKees Cook# discourage the addition of CONFIG_EXPERIMENTAL in Kconfig.
21571ba8dfd1SKees Cook		if ($realfile =~ /Kconfig/ &&
21581ba8dfd1SKees Cook		    $line =~ /.\s*depends on\s+.*\bEXPERIMENTAL\b/) {
21591ba8dfd1SKees Cook			WARN("CONFIG_EXPERIMENTAL",
21601ba8dfd1SKees Cook			     "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
21611ba8dfd1SKees Cook		}
21621ba8dfd1SKees Cook
2163c68e5878SArnaud Lacombe		if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
2164c68e5878SArnaud Lacombe		    ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
2165c68e5878SArnaud Lacombe			my $flag = $1;
2166c68e5878SArnaud Lacombe			my $replacement = {
2167c68e5878SArnaud Lacombe				'EXTRA_AFLAGS' =>   'asflags-y',
2168c68e5878SArnaud Lacombe				'EXTRA_CFLAGS' =>   'ccflags-y',
2169c68e5878SArnaud Lacombe				'EXTRA_CPPFLAGS' => 'cppflags-y',
2170c68e5878SArnaud Lacombe				'EXTRA_LDFLAGS' =>  'ldflags-y',
2171c68e5878SArnaud Lacombe			};
2172c68e5878SArnaud Lacombe
2173c68e5878SArnaud Lacombe			WARN("DEPRECATED_VARIABLE",
2174c68e5878SArnaud Lacombe			     "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
2175c68e5878SArnaud Lacombe		}
2176c68e5878SArnaud Lacombe
2177bff5da43SRob Herring# check for DT compatible documentation
21787dd05b38SFlorian Vaussard		if (defined $root &&
21797dd05b38SFlorian Vaussard			(($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) ||
21807dd05b38SFlorian Vaussard			 ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) {
21817dd05b38SFlorian Vaussard
2182bff5da43SRob Herring			my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g;
2183bff5da43SRob Herring
2184cc93319bSFlorian Vaussard			my $dt_path = $root . "/Documentation/devicetree/bindings/";
2185cc93319bSFlorian Vaussard			my $vp_file = $dt_path . "vendor-prefixes.txt";
2186cc93319bSFlorian Vaussard
2187bff5da43SRob Herring			foreach my $compat (@compats) {
2188bff5da43SRob Herring				my $compat2 = $compat;
2189185d566bSRob Herring				$compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/;
2190185d566bSRob Herring				my $compat3 = $compat;
2191185d566bSRob Herring				$compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/;
2192185d566bSRob Herring				`grep -Erq "$compat|$compat2|$compat3" $dt_path`;
2193bff5da43SRob Herring				if ( $? >> 8 ) {
2194bff5da43SRob Herring					WARN("UNDOCUMENTED_DT_STRING",
2195bff5da43SRob Herring					     "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr);
2196bff5da43SRob Herring				}
2197bff5da43SRob Herring
21984fbf32a6SFlorian Vaussard				next if $compat !~ /^([a-zA-Z0-9\-]+)\,/;
21994fbf32a6SFlorian Vaussard				my $vendor = $1;
2200cc93319bSFlorian Vaussard				`grep -Eq "^$vendor\\b" $vp_file`;
2201bff5da43SRob Herring				if ( $? >> 8 ) {
2202bff5da43SRob Herring					WARN("UNDOCUMENTED_DT_STRING",
2203cc93319bSFlorian Vaussard					     "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr);
2204bff5da43SRob Herring				}
2205bff5da43SRob Herring			}
2206bff5da43SRob Herring		}
2207bff5da43SRob Herring
22085368df20SAndy Whitcroft# check we are in a valid source file if not then ignore this hunk
22095368df20SAndy Whitcroft		next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
22105368df20SAndy Whitcroft
22116cd7f386SJoe Perches#line length limit
2212c45dcabdSAndy Whitcroft		if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ &&
2213f4c014c0SAndy Whitcroft		    $rawline !~ /^.\s*\*\s*\@$Ident\s/ &&
22140fccc622SJoe Perches		    !($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(KERN_\S+\s*|[^"]*))?"[X\t]*"\s*(?:|,|\)\s*;)\s*$/ ||
22158bbea968SJoe Perches		    $line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) &&
22166cd7f386SJoe Perches		    $length > $max_line_length)
2217c45dcabdSAndy Whitcroft		{
2218000d1cc1SJoe Perches			WARN("LONG_LINE",
22196cd7f386SJoe Perches			     "line over $max_line_length characters\n" . $herecurr);
22200a920b5bSAndy Whitcroft		}
22210a920b5bSAndy Whitcroft
2222ca56dc09SJosh Triplett# Check for user-visible strings broken across lines, which breaks the ability
22238c5fcd24SJoe Perches# to grep for the string.  Make exceptions when the previous string ends in a
22248c5fcd24SJoe Perches# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{'
22258c5fcd24SJoe Perches# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value
2226ca56dc09SJosh Triplett		if ($line =~ /^\+\s*"/ &&
2227ca56dc09SJosh Triplett		    $prevline =~ /"\s*$/ &&
22288c5fcd24SJoe Perches		    $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) {
2229ca56dc09SJosh Triplett			WARN("SPLIT_STRING",
2230ca56dc09SJosh Triplett			     "quoted string split across lines\n" . $hereprev);
2231ca56dc09SJosh Triplett		}
2232ca56dc09SJosh Triplett
22335e79d96eSJoe Perches# check for spaces before a quoted newline
22345e79d96eSJoe Perches		if ($rawline =~ /^.*\".*\s\\n/) {
22353705ce5bSJoe Perches			if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
22363705ce5bSJoe Perches				 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
22373705ce5bSJoe Perches			    $fix) {
22383705ce5bSJoe Perches				$fixed[$linenr - 1] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
22393705ce5bSJoe Perches			}
22403705ce5bSJoe Perches
22415e79d96eSJoe Perches		}
22425e79d96eSJoe Perches
22438905a67cSAndy Whitcroft# check for adding lines without a newline.
22448905a67cSAndy Whitcroft		if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
2245000d1cc1SJoe Perches			WARN("MISSING_EOF_NEWLINE",
2246000d1cc1SJoe Perches			     "adding a line without newline at end of file\n" . $herecurr);
22478905a67cSAndy Whitcroft		}
22488905a67cSAndy Whitcroft
224942e41c54SMike Frysinger# Blackfin: use hi/lo macros
225042e41c54SMike Frysinger		if ($realfile =~ m@arch/blackfin/.*\.S$@) {
225142e41c54SMike Frysinger			if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
225242e41c54SMike Frysinger				my $herevet = "$here\n" . cat_vet($line) . "\n";
2253000d1cc1SJoe Perches				ERROR("LO_MACRO",
2254000d1cc1SJoe Perches				      "use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
225542e41c54SMike Frysinger			}
225642e41c54SMike Frysinger			if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
225742e41c54SMike Frysinger				my $herevet = "$here\n" . cat_vet($line) . "\n";
2258000d1cc1SJoe Perches				ERROR("HI_MACRO",
2259000d1cc1SJoe Perches				      "use the HI() macro, not (... >> 16)\n" . $herevet);
226042e41c54SMike Frysinger			}
226142e41c54SMike Frysinger		}
226242e41c54SMike Frysinger
2263b9ea10d6SAndy Whitcroft# check we are in a valid source file C or perl if not then ignore this hunk
2264b9ea10d6SAndy Whitcroft		next if ($realfile !~ /\.(h|c|pl)$/);
22650a920b5bSAndy Whitcroft
22660a920b5bSAndy Whitcroft# at the beginning of a line any tabs must come first and anything
22670a920b5bSAndy Whitcroft# more than 8 must use tabs.
2268c2fdda0dSAndy Whitcroft		if ($rawline =~ /^\+\s* \t\s*\S/ ||
2269c2fdda0dSAndy Whitcroft		    $rawline =~ /^\+\s*        \s*/) {
2270c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2271d2c0a235SAndy Whitcroft			$rpt_cleaners = 1;
22723705ce5bSJoe Perches			if (ERROR("CODE_INDENT",
22733705ce5bSJoe Perches				  "code indent should use tabs where possible\n" . $herevet) &&
22743705ce5bSJoe Perches			    $fix) {
22753705ce5bSJoe Perches				$fixed[$linenr - 1] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
22763705ce5bSJoe Perches			}
22770a920b5bSAndy Whitcroft		}
22780a920b5bSAndy Whitcroft
227908e44365SAlberto Panizzo# check for space before tabs.
228008e44365SAlberto Panizzo		if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
228108e44365SAlberto Panizzo			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
22823705ce5bSJoe Perches			if (WARN("SPACE_BEFORE_TAB",
22833705ce5bSJoe Perches				"please, no space before tabs\n" . $herevet) &&
22843705ce5bSJoe Perches			    $fix) {
2285c76f4cb3SJoe Perches				while ($fixed[$linenr - 1] =~
2286c76f4cb3SJoe Perches					   s/(^\+.*) {8,8}+\t/$1\t\t/) {}
2287c76f4cb3SJoe Perches				while ($fixed[$linenr - 1] =~
2288c76f4cb3SJoe Perches					   s/(^\+.*) +\t/$1\t/) {}
22893705ce5bSJoe Perches			}
229008e44365SAlberto Panizzo		}
229108e44365SAlberto Panizzo
2292d1fe9c09SJoe Perches# check for && or || at the start of a line
2293d1fe9c09SJoe Perches		if ($rawline =~ /^\+\s*(&&|\|\|)/) {
2294d1fe9c09SJoe Perches			CHK("LOGICAL_CONTINUATIONS",
2295d1fe9c09SJoe Perches			    "Logical continuations should be on the previous line\n" . $hereprev);
2296d1fe9c09SJoe Perches		}
2297d1fe9c09SJoe Perches
2298d1fe9c09SJoe Perches# check multi-line statement indentation matches previous line
2299d1fe9c09SJoe Perches		if ($^V && $^V ge 5.10.0 &&
230091cb5195SJoe Perches		    $prevline =~ /^\+([ \t]*)((?:$c90_Keywords(?:\s+if)\s*)|(?:$Declare\s*)?(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*|$Ident\s*=\s*$Ident\s*)\(.*(\&\&|\|\||,)\s*$/) {
2301d1fe9c09SJoe Perches			$prevline =~ /^\+(\t*)(.*)$/;
2302d1fe9c09SJoe Perches			my $oldindent = $1;
2303d1fe9c09SJoe Perches			my $rest = $2;
2304d1fe9c09SJoe Perches
2305d1fe9c09SJoe Perches			my $pos = pos_last_openparen($rest);
2306d1fe9c09SJoe Perches			if ($pos >= 0) {
2307b34a26f3SJoe Perches				$line =~ /^(\+| )([ \t]*)/;
2308b34a26f3SJoe Perches				my $newindent = $2;
2309d1fe9c09SJoe Perches
2310d1fe9c09SJoe Perches				my $goodtabindent = $oldindent .
2311d1fe9c09SJoe Perches					"\t" x ($pos / 8) .
2312d1fe9c09SJoe Perches					" "  x ($pos % 8);
2313d1fe9c09SJoe Perches				my $goodspaceindent = $oldindent . " "  x $pos;
2314d1fe9c09SJoe Perches
2315d1fe9c09SJoe Perches				if ($newindent ne $goodtabindent &&
2316d1fe9c09SJoe Perches				    $newindent ne $goodspaceindent) {
23173705ce5bSJoe Perches
23183705ce5bSJoe Perches					if (CHK("PARENTHESIS_ALIGNMENT",
23193705ce5bSJoe Perches						"Alignment should match open parenthesis\n" . $hereprev) &&
23203705ce5bSJoe Perches					    $fix && $line =~ /^\+/) {
23213705ce5bSJoe Perches						$fixed[$linenr - 1] =~
23223705ce5bSJoe Perches						    s/^\+[ \t]*/\+$goodtabindent/;
23233705ce5bSJoe Perches					}
2324d1fe9c09SJoe Perches				}
2325d1fe9c09SJoe Perches			}
2326d1fe9c09SJoe Perches		}
2327d1fe9c09SJoe Perches
2328f27c95dbSJoe Perches		if ($line =~ /^\+.*\(\s*$Type\s*\)[ \t]+(?!$Assignment|$Arithmetic)/) {
23293705ce5bSJoe Perches			if (CHK("SPACING",
2330f27c95dbSJoe Perches				"No space is necessary after a cast\n" . $herecurr) &&
23313705ce5bSJoe Perches			    $fix) {
23323705ce5bSJoe Perches				$fixed[$linenr - 1] =~
2333f27c95dbSJoe Perches				    s/(\(\s*$Type\s*\))[ \t]+/$1/;
23343705ce5bSJoe Perches			}
2335aad4f614SJoe Perches		}
2336aad4f614SJoe Perches
233705880600SJoe Perches		if ($realfile =~ m@^(drivers/net/|net/)@ &&
2338fdb4bcd6SJoe Perches		    $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
233985ad978cSJoe Perches		    $rawline =~ /^\+[ \t]*\*/ &&
234085ad978cSJoe Perches		    $realline > 2) {
234105880600SJoe Perches			WARN("NETWORKING_BLOCK_COMMENT_STYLE",
234205880600SJoe Perches			     "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
234305880600SJoe Perches		}
234405880600SJoe Perches
234505880600SJoe Perches		if ($realfile =~ m@^(drivers/net/|net/)@ &&
2346a605e32eSJoe Perches		    $prevrawline =~ /^\+[ \t]*\/\*/ &&		#starting /*
2347a605e32eSJoe Perches		    $prevrawline !~ /\*\/[ \t]*$/ &&		#no trailing */
234861135e96SJoe Perches		    $rawline =~ /^\+/ &&			#line is new
2349a605e32eSJoe Perches		    $rawline !~ /^\+[ \t]*\*/) {		#no leading *
2350a605e32eSJoe Perches			WARN("NETWORKING_BLOCK_COMMENT_STYLE",
2351a605e32eSJoe Perches			     "networking block comments start with * on subsequent lines\n" . $hereprev);
2352a605e32eSJoe Perches		}
2353a605e32eSJoe Perches
2354a605e32eSJoe Perches		if ($realfile =~ m@^(drivers/net/|net/)@ &&
2355c24f9f19SJoe Perches		    $rawline !~ m@^\+[ \t]*\*/[ \t]*$@ &&	#trailing */
2356c24f9f19SJoe Perches		    $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ &&	#inline /*...*/
2357c24f9f19SJoe Perches		    $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ &&	#trailing **/
2358c24f9f19SJoe Perches		    $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) {	#non blank */
235905880600SJoe Perches			WARN("NETWORKING_BLOCK_COMMENT_STYLE",
236005880600SJoe Perches			     "networking block comments put the trailing */ on a separate line\n" . $herecurr);
236105880600SJoe Perches		}
236205880600SJoe Perches
23637f619191SJoe Perches# check for missing blank lines after struct/union declarations
23647f619191SJoe Perches# with exceptions for various attributes and macros
23657f619191SJoe Perches		if ($prevline =~ /^[\+ ]};?\s*$/ &&
23667f619191SJoe Perches		    $line =~ /^\+/ &&
23677f619191SJoe Perches		    !($line =~ /^\+\s*$/ ||
23687f619191SJoe Perches		      $line =~ /^\+\s*EXPORT_SYMBOL/ ||
23697f619191SJoe Perches		      $line =~ /^\+\s*MODULE_/i ||
23707f619191SJoe Perches		      $line =~ /^\+\s*\#\s*(?:end|elif|else)/ ||
23717f619191SJoe Perches		      $line =~ /^\+[a-z_]*init/ ||
23727f619191SJoe Perches		      $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ ||
23737f619191SJoe Perches		      $line =~ /^\+\s*DECLARE/ ||
23747f619191SJoe Perches		      $line =~ /^\+\s*__setup/)) {
23757f619191SJoe Perches			CHK("LINE_SPACING",
23767f619191SJoe Perches			    "Please use a blank line after function/struct/union/enum declarations\n" . $hereprev);
23777f619191SJoe Perches		}
23787f619191SJoe Perches
2379365dd4eaSJoe Perches# check for multiple consecutive blank lines
2380365dd4eaSJoe Perches		if ($prevline =~ /^[\+ ]\s*$/ &&
2381365dd4eaSJoe Perches		    $line =~ /^\+\s*$/ &&
2382365dd4eaSJoe Perches		    $last_blank_line != ($linenr - 1)) {
2383365dd4eaSJoe Perches			CHK("LINE_SPACING",
2384365dd4eaSJoe Perches			    "Please don't use multiple blank lines\n" . $hereprev);
2385365dd4eaSJoe Perches			$last_blank_line = $linenr;
2386365dd4eaSJoe Perches		}
2387365dd4eaSJoe Perches
23883b617e3bSJoe Perches# check for missing blank lines after declarations
23893f7bac03SJoe Perches		if ($sline =~ /^\+\s+\S/ &&			#Not at char 1
23903f7bac03SJoe Perches			# actual declarations
23913f7bac03SJoe Perches		    ($prevline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
23925a4e1fd3SJoe Perches			# function pointer declarations
23935a4e1fd3SJoe Perches		     $prevline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
23943f7bac03SJoe Perches			# foo bar; where foo is some local typedef or #define
23953f7bac03SJoe Perches		     $prevline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
23963f7bac03SJoe Perches			# known declaration macros
23973f7bac03SJoe Perches		     $prevline =~ /^\+\s+$declaration_macros/) &&
23983f7bac03SJoe Perches			# for "else if" which can look like "$Ident $Ident"
23993f7bac03SJoe Perches		    !($prevline =~ /^\+\s+$c90_Keywords\b/ ||
24003f7bac03SJoe Perches			# other possible extensions of declaration lines
24013f7bac03SJoe Perches		      $prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ ||
24023f7bac03SJoe Perches			# not starting a section or a macro "\" extended line
24033f7bac03SJoe Perches		      $prevline =~ /(?:\{\s*|\\)$/) &&
24043f7bac03SJoe Perches			# looks like a declaration
24053f7bac03SJoe Perches		    !($sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
24065a4e1fd3SJoe Perches			# function pointer declarations
24075a4e1fd3SJoe Perches		      $sline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
24083f7bac03SJoe Perches			# foo bar; where foo is some local typedef or #define
24093f7bac03SJoe Perches		      $sline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
24103f7bac03SJoe Perches			# known declaration macros
24113f7bac03SJoe Perches		      $sline =~ /^\+\s+$declaration_macros/ ||
24123f7bac03SJoe Perches			# start of struct or union or enum
24133b617e3bSJoe Perches		      $sline =~ /^\+\s+(?:union|struct|enum|typedef)\b/ ||
24143f7bac03SJoe Perches			# start or end of block or continuation of declaration
24153f7bac03SJoe Perches		      $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ ||
24163f7bac03SJoe Perches			# bitfield continuation
24173f7bac03SJoe Perches		      $sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ ||
24183f7bac03SJoe Perches			# other possible extensions of declaration lines
24193f7bac03SJoe Perches		      $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) &&
24203f7bac03SJoe Perches			# indentation of previous and current line are the same
24213f7bac03SJoe Perches		    (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) {
2422fee0aa83SJoe Perches			WARN("LINE_SPACING",
24233f7bac03SJoe Perches			     "Missing a blank line after declarations\n" . $hereprev);
24243b617e3bSJoe Perches		}
24253b617e3bSJoe Perches
24265f7ddae6SRaffaele Recalcati# check for spaces at the beginning of a line.
24276b4c5bebSAndy Whitcroft# Exceptions:
24286b4c5bebSAndy Whitcroft#  1) within comments
24296b4c5bebSAndy Whitcroft#  2) indented preprocessor commands
24306b4c5bebSAndy Whitcroft#  3) hanging labels
24313705ce5bSJoe Perches		if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/)  {
24325f7ddae6SRaffaele Recalcati			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
24333705ce5bSJoe Perches			if (WARN("LEADING_SPACE",
24343705ce5bSJoe Perches				 "please, no spaces at the start of a line\n" . $herevet) &&
24353705ce5bSJoe Perches			    $fix) {
24363705ce5bSJoe Perches				$fixed[$linenr - 1] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
24373705ce5bSJoe Perches			}
24385f7ddae6SRaffaele Recalcati		}
24395f7ddae6SRaffaele Recalcati
2440b9ea10d6SAndy Whitcroft# check we are in a valid C source file if not then ignore this hunk
2441b9ea10d6SAndy Whitcroft		next if ($realfile !~ /\.(h|c)$/);
2442b9ea10d6SAndy Whitcroft
2443032a4c0fSJoe Perches# check indentation of any line with a bare else
2444032a4c0fSJoe Perches# if the previous line is a break or return and is indented 1 tab more...
2445032a4c0fSJoe Perches		if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {
2446032a4c0fSJoe Perches			my $tabs = length($1) + 1;
2447032a4c0fSJoe Perches			if ($prevline =~ /^\+\t{$tabs,$tabs}(?:break|return)\b/) {
2448032a4c0fSJoe Perches				WARN("UNNECESSARY_ELSE",
2449032a4c0fSJoe Perches				     "else is not generally useful after a break or return\n" . $hereprev);
2450032a4c0fSJoe Perches			}
2451032a4c0fSJoe Perches		}
2452032a4c0fSJoe Perches
24531ba8dfd1SKees Cook# discourage the addition of CONFIG_EXPERIMENTAL in #if(def).
24541ba8dfd1SKees Cook		if ($line =~ /^\+\s*\#\s*if.*\bCONFIG_EXPERIMENTAL\b/) {
24551ba8dfd1SKees Cook			WARN("CONFIG_EXPERIMENTAL",
24561ba8dfd1SKees Cook			     "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
24571ba8dfd1SKees Cook		}
24581ba8dfd1SKees Cook
2459c2fdda0dSAndy Whitcroft# check for RCS/CVS revision markers
2460cf655043SAndy Whitcroft		if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
2461000d1cc1SJoe Perches			WARN("CVS_KEYWORD",
2462000d1cc1SJoe Perches			     "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
2463c2fdda0dSAndy Whitcroft		}
246422f2a2efSAndy Whitcroft
246542e41c54SMike Frysinger# Blackfin: don't use __builtin_bfin_[cs]sync
246642e41c54SMike Frysinger		if ($line =~ /__builtin_bfin_csync/) {
246742e41c54SMike Frysinger			my $herevet = "$here\n" . cat_vet($line) . "\n";
2468000d1cc1SJoe Perches			ERROR("CSYNC",
2469000d1cc1SJoe Perches			      "use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
247042e41c54SMike Frysinger		}
247142e41c54SMike Frysinger		if ($line =~ /__builtin_bfin_ssync/) {
247242e41c54SMike Frysinger			my $herevet = "$here\n" . cat_vet($line) . "\n";
2473000d1cc1SJoe Perches			ERROR("SSYNC",
2474000d1cc1SJoe Perches			      "use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
247542e41c54SMike Frysinger		}
247642e41c54SMike Frysinger
247756e77d70SJoe Perches# check for old HOTPLUG __dev<foo> section markings
247856e77d70SJoe Perches		if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
247956e77d70SJoe Perches			WARN("HOTPLUG_SECTION",
248056e77d70SJoe Perches			     "Using $1 is unnecessary\n" . $herecurr);
248156e77d70SJoe Perches		}
248256e77d70SJoe Perches
24839c0ca6f9SAndy Whitcroft# Check for potential 'bare' types
24842b474a1aSAndy Whitcroft		my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
24852b474a1aSAndy Whitcroft		    $realline_next);
24863e469cdcSAndy Whitcroft#print "LINE<$line>\n";
24873e469cdcSAndy Whitcroft		if ($linenr >= $suppress_statement &&
24881b5539b1SJoe Perches		    $realcnt && $sline =~ /.\s*\S/) {
2489170d3a22SAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
2490f5fe35ddSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0);
2491171ae1a4SAndy Whitcroft			$stat =~ s/\n./\n /g;
2492171ae1a4SAndy Whitcroft			$cond =~ s/\n./\n /g;
2493171ae1a4SAndy Whitcroft
24943e469cdcSAndy Whitcroft#print "linenr<$linenr> <$stat>\n";
24953e469cdcSAndy Whitcroft			# If this statement has no statement boundaries within
24963e469cdcSAndy Whitcroft			# it there is no point in retrying a statement scan
24973e469cdcSAndy Whitcroft			# until we hit end of it.
24983e469cdcSAndy Whitcroft			my $frag = $stat; $frag =~ s/;+\s*$//;
24993e469cdcSAndy Whitcroft			if ($frag !~ /(?:{|;)/) {
25003e469cdcSAndy Whitcroft#print "skip<$line_nr_next>\n";
25013e469cdcSAndy Whitcroft				$suppress_statement = $line_nr_next;
25023e469cdcSAndy Whitcroft			}
2503f74bd194SAndy Whitcroft
25042b474a1aSAndy Whitcroft			# Find the real next line.
25052b474a1aSAndy Whitcroft			$realline_next = $line_nr_next;
25062b474a1aSAndy Whitcroft			if (defined $realline_next &&
25072b474a1aSAndy Whitcroft			    (!defined $lines[$realline_next - 1] ||
25082b474a1aSAndy Whitcroft			     substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
25092b474a1aSAndy Whitcroft				$realline_next++;
25102b474a1aSAndy Whitcroft			}
25112b474a1aSAndy Whitcroft
2512171ae1a4SAndy Whitcroft			my $s = $stat;
2513171ae1a4SAndy Whitcroft			$s =~ s/{.*$//s;
2514cf655043SAndy Whitcroft
2515c2fdda0dSAndy Whitcroft			# Ignore goto labels.
2516171ae1a4SAndy Whitcroft			if ($s =~ /$Ident:\*$/s) {
2517c2fdda0dSAndy Whitcroft
2518c2fdda0dSAndy Whitcroft			# Ignore functions being called
2519171ae1a4SAndy Whitcroft			} elsif ($s =~ /^.\s*$Ident\s*\(/s) {
2520c2fdda0dSAndy Whitcroft
2521463f2864SAndy Whitcroft			} elsif ($s =~ /^.\s*else\b/s) {
2522463f2864SAndy Whitcroft
2523c45dcabdSAndy Whitcroft			# declarations always start with types
2524d2506586SAndy 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) {
2525c45dcabdSAndy Whitcroft				my $type = $1;
2526c45dcabdSAndy Whitcroft				$type =~ s/\s+/ /g;
2527c45dcabdSAndy Whitcroft				possible($type, "A:" . $s);
2528c45dcabdSAndy Whitcroft
25296c72ffaaSAndy Whitcroft			# definitions in global scope can only start with types
2530a6a84062SAndy Whitcroft			} elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
2531c45dcabdSAndy Whitcroft				possible($1, "B:" . $s);
2532c2fdda0dSAndy Whitcroft			}
25338905a67cSAndy Whitcroft
25346c72ffaaSAndy Whitcroft			# any (foo ... *) is a pointer cast, and foo is a type
253565863862SAndy Whitcroft			while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
2536c45dcabdSAndy Whitcroft				possible($1, "C:" . $s);
25379c0ca6f9SAndy Whitcroft			}
25388905a67cSAndy Whitcroft
25398905a67cSAndy Whitcroft			# Check for any sort of function declaration.
25408905a67cSAndy Whitcroft			# int foo(something bar, other baz);
25418905a67cSAndy Whitcroft			# void (*store_gdt)(x86_descr_ptr *);
2542171ae1a4SAndy Whitcroft			if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
25438905a67cSAndy Whitcroft				my ($name_len) = length($1);
25448905a67cSAndy Whitcroft
2545cf655043SAndy Whitcroft				my $ctx = $s;
2546773647a0SAndy Whitcroft				substr($ctx, 0, $name_len + 1, '');
25478905a67cSAndy Whitcroft				$ctx =~ s/\)[^\)]*$//;
2548cf655043SAndy Whitcroft
25498905a67cSAndy Whitcroft				for my $arg (split(/\s*,\s*/, $ctx)) {
2550c45dcabdSAndy Whitcroft					if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
25518905a67cSAndy Whitcroft
2552c45dcabdSAndy Whitcroft						possible($1, "D:" . $s);
25538905a67cSAndy Whitcroft					}
25548905a67cSAndy Whitcroft				}
25558905a67cSAndy Whitcroft			}
25568905a67cSAndy Whitcroft
25579c0ca6f9SAndy Whitcroft		}
25589c0ca6f9SAndy Whitcroft
255900df344fSAndy Whitcroft#
256000df344fSAndy Whitcroft# Checks which may be anchored in the context.
256100df344fSAndy Whitcroft#
256200df344fSAndy Whitcroft
256300df344fSAndy Whitcroft# Check for switch () and associated case and default
256400df344fSAndy Whitcroft# statements should be at the same indent.
256500df344fSAndy Whitcroft		if ($line=~/\bswitch\s*\(.*\)/) {
256600df344fSAndy Whitcroft			my $err = '';
256700df344fSAndy Whitcroft			my $sep = '';
256800df344fSAndy Whitcroft			my @ctx = ctx_block_outer($linenr, $realcnt);
256900df344fSAndy Whitcroft			shift(@ctx);
257000df344fSAndy Whitcroft			for my $ctx (@ctx) {
257100df344fSAndy Whitcroft				my ($clen, $cindent) = line_stats($ctx);
257200df344fSAndy Whitcroft				if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
257300df344fSAndy Whitcroft							$indent != $cindent) {
257400df344fSAndy Whitcroft					$err .= "$sep$ctx\n";
257500df344fSAndy Whitcroft					$sep = '';
257600df344fSAndy Whitcroft				} else {
257700df344fSAndy Whitcroft					$sep = "[...]\n";
257800df344fSAndy Whitcroft				}
257900df344fSAndy Whitcroft			}
258000df344fSAndy Whitcroft			if ($err ne '') {
2581000d1cc1SJoe Perches				ERROR("SWITCH_CASE_INDENT_LEVEL",
2582000d1cc1SJoe Perches				      "switch and case should be at the same indent\n$hereline$err");
2583de7d4f0eSAndy Whitcroft			}
2584de7d4f0eSAndy Whitcroft		}
2585de7d4f0eSAndy Whitcroft
2586de7d4f0eSAndy Whitcroft# if/while/etc brace do not go on next line, unless defining a do while loop,
2587de7d4f0eSAndy Whitcroft# or if that brace on the next line is for something else
2588c45dcabdSAndy Whitcroft		if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
2589773647a0SAndy Whitcroft			my $pre_ctx = "$1$2";
2590773647a0SAndy Whitcroft
25919c0ca6f9SAndy Whitcroft			my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
25928eef05ddSJoe Perches
25938eef05ddSJoe Perches			if ($line =~ /^\+\t{6,}/) {
25948eef05ddSJoe Perches				WARN("DEEP_INDENTATION",
25958eef05ddSJoe Perches				     "Too many leading tabs - consider code refactoring\n" . $herecurr);
25968eef05ddSJoe Perches			}
25978eef05ddSJoe Perches
2598de7d4f0eSAndy Whitcroft			my $ctx_cnt = $realcnt - $#ctx - 1;
2599de7d4f0eSAndy Whitcroft			my $ctx = join("\n", @ctx);
2600de7d4f0eSAndy Whitcroft
2601548596d5SAndy Whitcroft			my $ctx_ln = $linenr;
2602548596d5SAndy Whitcroft			my $ctx_skip = $realcnt;
2603de7d4f0eSAndy Whitcroft
2604548596d5SAndy Whitcroft			while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
2605548596d5SAndy Whitcroft					defined $lines[$ctx_ln - 1] &&
2606548596d5SAndy Whitcroft					$lines[$ctx_ln - 1] =~ /^-/)) {
2607548596d5SAndy Whitcroft				##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
2608548596d5SAndy Whitcroft				$ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
2609773647a0SAndy Whitcroft				$ctx_ln++;
2610773647a0SAndy Whitcroft			}
2611548596d5SAndy Whitcroft
261253210168SAndy Whitcroft			#print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
261353210168SAndy Whitcroft			#print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
2614773647a0SAndy Whitcroft
2615773647a0SAndy Whitcroft			if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln -1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
2616000d1cc1SJoe Perches				ERROR("OPEN_BRACE",
2617000d1cc1SJoe Perches				      "that open brace { should be on the previous line\n" .
261801464f30SAndy Whitcroft					"$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
261900df344fSAndy Whitcroft			}
2620773647a0SAndy Whitcroft			if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
2621773647a0SAndy Whitcroft			    $ctx =~ /\)\s*\;\s*$/ &&
2622773647a0SAndy Whitcroft			    defined $lines[$ctx_ln - 1])
2623773647a0SAndy Whitcroft			{
26249c0ca6f9SAndy Whitcroft				my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
26259c0ca6f9SAndy Whitcroft				if ($nindent > $indent) {
2626000d1cc1SJoe Perches					WARN("TRAILING_SEMICOLON",
2627000d1cc1SJoe Perches					     "trailing semicolon indicates no statements, indent implies otherwise\n" .
262801464f30SAndy Whitcroft						"$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
26299c0ca6f9SAndy Whitcroft				}
26309c0ca6f9SAndy Whitcroft			}
263100df344fSAndy Whitcroft		}
263200df344fSAndy Whitcroft
26334d001e4dSAndy Whitcroft# Check relative indent for conditionals and blocks.
26344d001e4dSAndy Whitcroft		if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
26353e469cdcSAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
26363e469cdcSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0)
26373e469cdcSAndy Whitcroft					if (!defined $stat);
26384d001e4dSAndy Whitcroft			my ($s, $c) = ($stat, $cond);
26394d001e4dSAndy Whitcroft
26404d001e4dSAndy Whitcroft			substr($s, 0, length($c), '');
26414d001e4dSAndy Whitcroft
26424d001e4dSAndy Whitcroft			# Make sure we remove the line prefixes as we have
26434d001e4dSAndy Whitcroft			# none on the first line, and are going to readd them
26444d001e4dSAndy Whitcroft			# where necessary.
26454d001e4dSAndy Whitcroft			$s =~ s/\n./\n/gs;
26464d001e4dSAndy Whitcroft
26474d001e4dSAndy Whitcroft			# Find out how long the conditional actually is.
26486f779c18SAndy Whitcroft			my @newlines = ($c =~ /\n/gs);
26496f779c18SAndy Whitcroft			my $cond_lines = 1 + $#newlines;
26504d001e4dSAndy Whitcroft
26514d001e4dSAndy Whitcroft			# We want to check the first line inside the block
26524d001e4dSAndy Whitcroft			# starting at the end of the conditional, so remove:
26534d001e4dSAndy Whitcroft			#  1) any blank line termination
26544d001e4dSAndy Whitcroft			#  2) any opening brace { on end of the line
26554d001e4dSAndy Whitcroft			#  3) any do (...) {
26564d001e4dSAndy Whitcroft			my $continuation = 0;
26574d001e4dSAndy Whitcroft			my $check = 0;
26584d001e4dSAndy Whitcroft			$s =~ s/^.*\bdo\b//;
26594d001e4dSAndy Whitcroft			$s =~ s/^\s*{//;
26604d001e4dSAndy Whitcroft			if ($s =~ s/^\s*\\//) {
26614d001e4dSAndy Whitcroft				$continuation = 1;
26624d001e4dSAndy Whitcroft			}
26639bd49efeSAndy Whitcroft			if ($s =~ s/^\s*?\n//) {
26644d001e4dSAndy Whitcroft				$check = 1;
26654d001e4dSAndy Whitcroft				$cond_lines++;
26664d001e4dSAndy Whitcroft			}
26674d001e4dSAndy Whitcroft
26684d001e4dSAndy Whitcroft			# Also ignore a loop construct at the end of a
26694d001e4dSAndy Whitcroft			# preprocessor statement.
26704d001e4dSAndy Whitcroft			if (($prevline =~ /^.\s*#\s*define\s/ ||
26714d001e4dSAndy Whitcroft			    $prevline =~ /\\\s*$/) && $continuation == 0) {
26724d001e4dSAndy Whitcroft				$check = 0;
26734d001e4dSAndy Whitcroft			}
26744d001e4dSAndy Whitcroft
26759bd49efeSAndy Whitcroft			my $cond_ptr = -1;
2676740504c6SAndy Whitcroft			$continuation = 0;
26779bd49efeSAndy Whitcroft			while ($cond_ptr != $cond_lines) {
26789bd49efeSAndy Whitcroft				$cond_ptr = $cond_lines;
26794d001e4dSAndy Whitcroft
2680f16fa28fSAndy Whitcroft				# If we see an #else/#elif then the code
2681f16fa28fSAndy Whitcroft				# is not linear.
2682f16fa28fSAndy Whitcroft				if ($s =~ /^\s*\#\s*(?:else|elif)/) {
2683f16fa28fSAndy Whitcroft					$check = 0;
2684f16fa28fSAndy Whitcroft				}
2685f16fa28fSAndy Whitcroft
26869bd49efeSAndy Whitcroft				# Ignore:
26879bd49efeSAndy Whitcroft				#  1) blank lines, they should be at 0,
26889bd49efeSAndy Whitcroft				#  2) preprocessor lines, and
26899bd49efeSAndy Whitcroft				#  3) labels.
2690740504c6SAndy Whitcroft				if ($continuation ||
2691740504c6SAndy Whitcroft				    $s =~ /^\s*?\n/ ||
26929bd49efeSAndy Whitcroft				    $s =~ /^\s*#\s*?/ ||
26939bd49efeSAndy Whitcroft				    $s =~ /^\s*$Ident\s*:/) {
2694740504c6SAndy Whitcroft					$continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
269530dad6ebSAndy Whitcroft					if ($s =~ s/^.*?\n//) {
26969bd49efeSAndy Whitcroft						$cond_lines++;
26979bd49efeSAndy Whitcroft					}
26984d001e4dSAndy Whitcroft				}
269930dad6ebSAndy Whitcroft			}
27004d001e4dSAndy Whitcroft
27014d001e4dSAndy Whitcroft			my (undef, $sindent) = line_stats("+" . $s);
27024d001e4dSAndy Whitcroft			my $stat_real = raw_line($linenr, $cond_lines);
27034d001e4dSAndy Whitcroft
27044d001e4dSAndy Whitcroft			# Check if either of these lines are modified, else
27054d001e4dSAndy Whitcroft			# this is not this patch's fault.
27064d001e4dSAndy Whitcroft			if (!defined($stat_real) ||
27074d001e4dSAndy Whitcroft			    $stat !~ /^\+/ && $stat_real !~ /^\+/) {
27084d001e4dSAndy Whitcroft				$check = 0;
27094d001e4dSAndy Whitcroft			}
27104d001e4dSAndy Whitcroft			if (defined($stat_real) && $cond_lines > 1) {
27114d001e4dSAndy Whitcroft				$stat_real = "[...]\n$stat_real";
27124d001e4dSAndy Whitcroft			}
27134d001e4dSAndy Whitcroft
27149bd49efeSAndy 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";
27154d001e4dSAndy Whitcroft
27164d001e4dSAndy Whitcroft			if ($check && (($sindent % 8) != 0 ||
27174d001e4dSAndy Whitcroft			    ($sindent <= $indent && $s ne ''))) {
2718000d1cc1SJoe Perches				WARN("SUSPECT_CODE_INDENT",
2719000d1cc1SJoe Perches				     "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
27204d001e4dSAndy Whitcroft			}
27214d001e4dSAndy Whitcroft		}
27224d001e4dSAndy Whitcroft
27236c72ffaaSAndy Whitcroft		# Track the 'values' across context and added lines.
27246c72ffaaSAndy Whitcroft		my $opline = $line; $opline =~ s/^./ /;
27251f65f947SAndy Whitcroft		my ($curr_values, $curr_vars) =
27261f65f947SAndy Whitcroft				annotate_values($opline . "\n", $prev_values);
27276c72ffaaSAndy Whitcroft		$curr_values = $prev_values . $curr_values;
2728c2fdda0dSAndy Whitcroft		if ($dbg_values) {
2729c2fdda0dSAndy Whitcroft			my $outline = $opline; $outline =~ s/\t/ /g;
2730cf655043SAndy Whitcroft			print "$linenr > .$outline\n";
2731cf655043SAndy Whitcroft			print "$linenr > $curr_values\n";
27321f65f947SAndy Whitcroft			print "$linenr >  $curr_vars\n";
2733c2fdda0dSAndy Whitcroft		}
27346c72ffaaSAndy Whitcroft		$prev_values = substr($curr_values, -1);
27356c72ffaaSAndy Whitcroft
273600df344fSAndy Whitcroft#ignore lines not being added
27373705ce5bSJoe Perches		next if ($line =~ /^[^\+]/);
273800df344fSAndy Whitcroft
2739653d4876SAndy Whitcroft# TEST: allow direct testing of the type matcher.
27407429c690SAndy Whitcroft		if ($dbg_type) {
27417429c690SAndy Whitcroft			if ($line =~ /^.\s*$Declare\s*$/) {
2742000d1cc1SJoe Perches				ERROR("TEST_TYPE",
2743000d1cc1SJoe Perches				      "TEST: is type\n" . $herecurr);
27447429c690SAndy Whitcroft			} elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
2745000d1cc1SJoe Perches				ERROR("TEST_NOT_TYPE",
2746000d1cc1SJoe Perches				      "TEST: is not type ($1 is)\n". $herecurr);
27477429c690SAndy Whitcroft			}
2748653d4876SAndy Whitcroft			next;
2749653d4876SAndy Whitcroft		}
2750a1ef277eSAndy Whitcroft# TEST: allow direct testing of the attribute matcher.
2751a1ef277eSAndy Whitcroft		if ($dbg_attr) {
27529360b0e5SAndy Whitcroft			if ($line =~ /^.\s*$Modifier\s*$/) {
2753000d1cc1SJoe Perches				ERROR("TEST_ATTR",
2754000d1cc1SJoe Perches				      "TEST: is attr\n" . $herecurr);
27559360b0e5SAndy Whitcroft			} elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
2756000d1cc1SJoe Perches				ERROR("TEST_NOT_ATTR",
2757000d1cc1SJoe Perches				      "TEST: is not attr ($1 is)\n". $herecurr);
2758a1ef277eSAndy Whitcroft			}
2759a1ef277eSAndy Whitcroft			next;
2760a1ef277eSAndy Whitcroft		}
2761653d4876SAndy Whitcroft
2762f0a594c1SAndy Whitcroft# check for initialisation to aggregates open brace on the next line
276399423c20SAndy Whitcroft		if ($line =~ /^.\s*{/ &&
276499423c20SAndy Whitcroft		    $prevline =~ /(?:^|[^=])=\s*$/) {
2765000d1cc1SJoe Perches			ERROR("OPEN_BRACE",
2766000d1cc1SJoe Perches			      "that open brace { should be on the previous line\n" . $hereprev);
2767f0a594c1SAndy Whitcroft		}
2768f0a594c1SAndy Whitcroft
276900df344fSAndy Whitcroft#
277000df344fSAndy Whitcroft# Checks which are anchored on the added line.
277100df344fSAndy Whitcroft#
277200df344fSAndy Whitcroft
2773653d4876SAndy Whitcroft# check for malformed paths in #include statements (uses RAW line)
2774c45dcabdSAndy Whitcroft		if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
2775653d4876SAndy Whitcroft			my $path = $1;
2776653d4876SAndy Whitcroft			if ($path =~ m{//}) {
2777000d1cc1SJoe Perches				ERROR("MALFORMED_INCLUDE",
2778495e9d84SJoe Perches				      "malformed #include filename\n" . $herecurr);
2779495e9d84SJoe Perches			}
2780495e9d84SJoe Perches			if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
2781495e9d84SJoe Perches				ERROR("UAPI_INCLUDE",
2782495e9d84SJoe Perches				      "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
2783653d4876SAndy Whitcroft			}
2784653d4876SAndy Whitcroft		}
2785653d4876SAndy Whitcroft
278600df344fSAndy Whitcroft# no C99 // comments
278700df344fSAndy Whitcroft		if ($line =~ m{//}) {
27883705ce5bSJoe Perches			if (ERROR("C99_COMMENTS",
27893705ce5bSJoe Perches				  "do not use C99 // comments\n" . $herecurr) &&
27903705ce5bSJoe Perches			    $fix) {
27913705ce5bSJoe Perches				my $line = $fixed[$linenr - 1];
27923705ce5bSJoe Perches				if ($line =~ /\/\/(.*)$/) {
27933705ce5bSJoe Perches					my $comment = trim($1);
27943705ce5bSJoe Perches					$fixed[$linenr - 1] =~ s@\/\/(.*)$@/\* $comment \*/@;
27953705ce5bSJoe Perches				}
27963705ce5bSJoe Perches			}
279700df344fSAndy Whitcroft		}
279800df344fSAndy Whitcroft		# Remove C99 comments.
27990a920b5bSAndy Whitcroft		$line =~ s@//.*@@;
28006c72ffaaSAndy Whitcroft		$opline =~ s@//.*@@;
28010a920b5bSAndy Whitcroft
28022b474a1aSAndy Whitcroft# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
28032b474a1aSAndy Whitcroft# the whole statement.
28042b474a1aSAndy Whitcroft#print "APW <$lines[$realline_next - 1]>\n";
28052b474a1aSAndy Whitcroft		if (defined $realline_next &&
28062b474a1aSAndy Whitcroft		    exists $lines[$realline_next - 1] &&
28072b474a1aSAndy Whitcroft		    !defined $suppress_export{$realline_next} &&
28082b474a1aSAndy Whitcroft		    ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
28092b474a1aSAndy Whitcroft		     $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
28103cbf62dfSAndy Whitcroft			# Handle definitions which produce identifiers with
28113cbf62dfSAndy Whitcroft			# a prefix:
28123cbf62dfSAndy Whitcroft			#   XXX(foo);
28133cbf62dfSAndy Whitcroft			#   EXPORT_SYMBOL(something_foo);
2814653d4876SAndy Whitcroft			my $name = $1;
281587a53877SAndy Whitcroft			if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
28163cbf62dfSAndy Whitcroft			    $name =~ /^${Ident}_$2/) {
28173cbf62dfSAndy Whitcroft#print "FOO C name<$name>\n";
28183cbf62dfSAndy Whitcroft				$suppress_export{$realline_next} = 1;
28193cbf62dfSAndy Whitcroft
28203cbf62dfSAndy Whitcroft			} elsif ($stat !~ /(?:
28212b474a1aSAndy Whitcroft				\n.}\s*$|
282248012058SAndy Whitcroft				^.DEFINE_$Ident\(\Q$name\E\)|
282348012058SAndy Whitcroft				^.DECLARE_$Ident\(\Q$name\E\)|
282448012058SAndy Whitcroft				^.LIST_HEAD\(\Q$name\E\)|
28252b474a1aSAndy Whitcroft				^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
28262b474a1aSAndy Whitcroft				\b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
282748012058SAndy Whitcroft			    )/x) {
28282b474a1aSAndy Whitcroft#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
28292b474a1aSAndy Whitcroft				$suppress_export{$realline_next} = 2;
28302b474a1aSAndy Whitcroft			} else {
28312b474a1aSAndy Whitcroft				$suppress_export{$realline_next} = 1;
28320a920b5bSAndy Whitcroft			}
28330a920b5bSAndy Whitcroft		}
28342b474a1aSAndy Whitcroft		if (!defined $suppress_export{$linenr} &&
28352b474a1aSAndy Whitcroft		    $prevline =~ /^.\s*$/ &&
28362b474a1aSAndy Whitcroft		    ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
28372b474a1aSAndy Whitcroft		     $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
28382b474a1aSAndy Whitcroft#print "FOO B <$lines[$linenr - 1]>\n";
28392b474a1aSAndy Whitcroft			$suppress_export{$linenr} = 2;
28402b474a1aSAndy Whitcroft		}
28412b474a1aSAndy Whitcroft		if (defined $suppress_export{$linenr} &&
28422b474a1aSAndy Whitcroft		    $suppress_export{$linenr} == 2) {
2843000d1cc1SJoe Perches			WARN("EXPORT_SYMBOL",
2844000d1cc1SJoe Perches			     "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
28452b474a1aSAndy Whitcroft		}
28460a920b5bSAndy Whitcroft
28475150bda4SJoe Eloff# check for global initialisers.
2848d5e616fcSJoe Perches		if ($line =~ /^\+(\s*$Type\s*$Ident\s*(?:\s+$Modifier))*\s*=\s*(0|NULL|false)\s*;/) {
2849d5e616fcSJoe Perches			if (ERROR("GLOBAL_INITIALISERS",
2850000d1cc1SJoe Perches				  "do not initialise globals to 0 or NULL\n" .
2851d5e616fcSJoe Perches				      $herecurr) &&
2852d5e616fcSJoe Perches			    $fix) {
2853d5e616fcSJoe Perches				$fixed[$linenr - 1] =~ s/($Type\s*$Ident\s*(?:\s+$Modifier))*\s*=\s*(0|NULL|false)\s*;/$1;/;
2854d5e616fcSJoe Perches			}
2855f0a594c1SAndy Whitcroft		}
28560a920b5bSAndy Whitcroft# check for static initialisers.
2857d5e616fcSJoe Perches		if ($line =~ /^\+.*\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
2858d5e616fcSJoe Perches			if (ERROR("INITIALISED_STATIC",
2859000d1cc1SJoe Perches				  "do not initialise statics to 0 or NULL\n" .
2860d5e616fcSJoe Perches				      $herecurr) &&
2861d5e616fcSJoe Perches			    $fix) {
2862d5e616fcSJoe Perches				$fixed[$linenr - 1] =~ s/(\bstatic\s.*?)\s*=\s*(0|NULL|false)\s*;/$1;/;
2863d5e616fcSJoe Perches			}
28640a920b5bSAndy Whitcroft		}
28650a920b5bSAndy Whitcroft
2866cb710ecaSJoe Perches# check for static const char * arrays.
2867cb710ecaSJoe Perches		if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
2868000d1cc1SJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
2869000d1cc1SJoe Perches			     "static const char * array should probably be static const char * const\n" .
2870cb710ecaSJoe Perches				$herecurr);
2871cb710ecaSJoe Perches               }
2872cb710ecaSJoe Perches
2873cb710ecaSJoe Perches# check for static char foo[] = "bar" declarations.
2874cb710ecaSJoe Perches		if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
2875000d1cc1SJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
2876000d1cc1SJoe Perches			     "static char array declaration should probably be static const char\n" .
2877cb710ecaSJoe Perches				$herecurr);
2878cb710ecaSJoe Perches               }
2879cb710ecaSJoe Perches
28809b0fa60dSJoe Perches# check for non-global char *foo[] = {"bar", ...} declarations.
28819b0fa60dSJoe Perches		if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) {
28829b0fa60dSJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
28839b0fa60dSJoe Perches			     "char * array declaration might be better as static const\n" .
28849b0fa60dSJoe Perches				$herecurr);
28859b0fa60dSJoe Perches               }
28869b0fa60dSJoe Perches
2887b36190c5SJoe Perches# check for function declarations without arguments like "int foo()"
2888b36190c5SJoe Perches		if ($line =~ /(\b$Type\s+$Ident)\s*\(\s*\)/) {
2889b36190c5SJoe Perches			if (ERROR("FUNCTION_WITHOUT_ARGS",
2890b36190c5SJoe Perches				  "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) &&
2891b36190c5SJoe Perches			    $fix) {
2892b36190c5SJoe Perches				$fixed[$linenr - 1] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/;
2893b36190c5SJoe Perches			}
2894b36190c5SJoe Perches		}
2895b36190c5SJoe Perches
289692e112fdSJoe Perches# check for uses of DEFINE_PCI_DEVICE_TABLE
289792e112fdSJoe Perches		if ($line =~ /\bDEFINE_PCI_DEVICE_TABLE\s*\(\s*(\w+)\s*\)\s*=/) {
289892e112fdSJoe Perches			if (WARN("DEFINE_PCI_DEVICE_TABLE",
289992e112fdSJoe Perches				 "Prefer struct pci_device_id over deprecated DEFINE_PCI_DEVICE_TABLE\n" . $herecurr) &&
290092e112fdSJoe Perches			    $fix) {
290192e112fdSJoe 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\[\] = /;
290292e112fdSJoe Perches			}
290393ed0e2dSJoe Perches		}
290493ed0e2dSJoe Perches
2905653d4876SAndy Whitcroft# check for new typedefs, only function parameters and sparse annotations
2906653d4876SAndy Whitcroft# make sense.
2907653d4876SAndy Whitcroft		if ($line =~ /\btypedef\s/ &&
29088054576dSAndy Whitcroft		    $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
2909c45dcabdSAndy Whitcroft		    $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
29108ed22cadSAndy Whitcroft		    $line !~ /\b$typeTypedefs\b/ &&
2911653d4876SAndy Whitcroft		    $line !~ /\b__bitwise(?:__|)\b/) {
2912000d1cc1SJoe Perches			WARN("NEW_TYPEDEFS",
2913000d1cc1SJoe Perches			     "do not add new typedefs\n" . $herecurr);
29140a920b5bSAndy Whitcroft		}
29150a920b5bSAndy Whitcroft
29160a920b5bSAndy Whitcroft# * goes on variable not on type
291765863862SAndy Whitcroft		# (char*[ const])
2918bfcb2cc7SAndy Whitcroft		while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
2919bfcb2cc7SAndy Whitcroft			#print "AA<$1>\n";
29203705ce5bSJoe Perches			my ($ident, $from, $to) = ($1, $2, $2);
2921d8aaf121SAndy Whitcroft
292265863862SAndy Whitcroft			# Should start with a space.
292365863862SAndy Whitcroft			$to =~ s/^(\S)/ $1/;
292465863862SAndy Whitcroft			# Should not end with a space.
292565863862SAndy Whitcroft			$to =~ s/\s+$//;
292665863862SAndy Whitcroft			# '*'s should not have spaces between.
2927f9a0b3d1SAndy Whitcroft			while ($to =~ s/\*\s+\*/\*\*/) {
292865863862SAndy Whitcroft			}
2929d8aaf121SAndy Whitcroft
29303705ce5bSJoe Perches##			print "1: from<$from> to<$to> ident<$ident>\n";
293165863862SAndy Whitcroft			if ($from ne $to) {
29323705ce5bSJoe Perches				if (ERROR("POINTER_LOCATION",
29333705ce5bSJoe Perches					  "\"(foo$from)\" should be \"(foo$to)\"\n" .  $herecurr) &&
29343705ce5bSJoe Perches				    $fix) {
29353705ce5bSJoe Perches					my $sub_from = $ident;
29363705ce5bSJoe Perches					my $sub_to = $ident;
29373705ce5bSJoe Perches					$sub_to =~ s/\Q$from\E/$to/;
29383705ce5bSJoe Perches					$fixed[$linenr - 1] =~
29393705ce5bSJoe Perches					    s@\Q$sub_from\E@$sub_to@;
29403705ce5bSJoe Perches				}
294165863862SAndy Whitcroft			}
2942bfcb2cc7SAndy Whitcroft		}
2943bfcb2cc7SAndy Whitcroft		while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
2944bfcb2cc7SAndy Whitcroft			#print "BB<$1>\n";
29453705ce5bSJoe Perches			my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
2946d8aaf121SAndy Whitcroft
294765863862SAndy Whitcroft			# Should start with a space.
294865863862SAndy Whitcroft			$to =~ s/^(\S)/ $1/;
294965863862SAndy Whitcroft			# Should not end with a space.
295065863862SAndy Whitcroft			$to =~ s/\s+$//;
295165863862SAndy Whitcroft			# '*'s should not have spaces between.
2952f9a0b3d1SAndy Whitcroft			while ($to =~ s/\*\s+\*/\*\*/) {
295365863862SAndy Whitcroft			}
295465863862SAndy Whitcroft			# Modifiers should have spaces.
295565863862SAndy Whitcroft			$to =~ s/(\b$Modifier$)/$1 /;
295665863862SAndy Whitcroft
29573705ce5bSJoe Perches##			print "2: from<$from> to<$to> ident<$ident>\n";
2958667026e7SAndy Whitcroft			if ($from ne $to && $ident !~ /^$Modifier$/) {
29593705ce5bSJoe Perches				if (ERROR("POINTER_LOCATION",
29603705ce5bSJoe Perches					  "\"foo${from}bar\" should be \"foo${to}bar\"\n" .  $herecurr) &&
29613705ce5bSJoe Perches				    $fix) {
29623705ce5bSJoe Perches
29633705ce5bSJoe Perches					my $sub_from = $match;
29643705ce5bSJoe Perches					my $sub_to = $match;
29653705ce5bSJoe Perches					$sub_to =~ s/\Q$from\E/$to/;
29663705ce5bSJoe Perches					$fixed[$linenr - 1] =~
29673705ce5bSJoe Perches					    s@\Q$sub_from\E@$sub_to@;
29683705ce5bSJoe Perches				}
296965863862SAndy Whitcroft			}
29700a920b5bSAndy Whitcroft		}
29710a920b5bSAndy Whitcroft
29720a920b5bSAndy Whitcroft# # no BUG() or BUG_ON()
29730a920b5bSAndy Whitcroft# 		if ($line =~ /\b(BUG|BUG_ON)\b/) {
29740a920b5bSAndy Whitcroft# 			print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
29750a920b5bSAndy Whitcroft# 			print "$herecurr";
29760a920b5bSAndy Whitcroft# 			$clean = 0;
29770a920b5bSAndy Whitcroft# 		}
29780a920b5bSAndy Whitcroft
29798905a67cSAndy Whitcroft		if ($line =~ /\bLINUX_VERSION_CODE\b/) {
2980000d1cc1SJoe Perches			WARN("LINUX_VERSION_CODE",
2981000d1cc1SJoe Perches			     "LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
29828905a67cSAndy Whitcroft		}
29838905a67cSAndy Whitcroft
298417441227SJoe Perches# check for uses of printk_ratelimit
298517441227SJoe Perches		if ($line =~ /\bprintk_ratelimit\s*\(/) {
2986000d1cc1SJoe Perches			WARN("PRINTK_RATELIMITED",
2987000d1cc1SJoe Perches"Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
298817441227SJoe Perches		}
298917441227SJoe Perches
299000df344fSAndy Whitcroft# printk should use KERN_* levels.  Note that follow on printk's on the
299100df344fSAndy Whitcroft# same line do not need a level, so we use the current block context
299200df344fSAndy Whitcroft# to try and find and validate the current printk.  In summary the current
299325985edcSLucas De Marchi# printk includes all preceding printk's which have no newline on the end.
299400df344fSAndy Whitcroft# we assume the first bad printk is the one to report.
2995f0a594c1SAndy Whitcroft		if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
299600df344fSAndy Whitcroft			my $ok = 0;
299700df344fSAndy Whitcroft			for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
299800df344fSAndy Whitcroft				#print "CHECK<$lines[$ln - 1]\n";
299925985edcSLucas De Marchi				# we have a preceding printk if it ends
300000df344fSAndy Whitcroft				# with "\n" ignore it, else it is to blame
300100df344fSAndy Whitcroft				if ($lines[$ln - 1] =~ m{\bprintk\(}) {
300200df344fSAndy Whitcroft					if ($rawlines[$ln - 1] !~ m{\\n"}) {
300300df344fSAndy Whitcroft						$ok = 1;
300400df344fSAndy Whitcroft					}
300500df344fSAndy Whitcroft					last;
300600df344fSAndy Whitcroft				}
300700df344fSAndy Whitcroft			}
300800df344fSAndy Whitcroft			if ($ok == 0) {
3009000d1cc1SJoe Perches				WARN("PRINTK_WITHOUT_KERN_LEVEL",
3010000d1cc1SJoe Perches				     "printk() should include KERN_ facility level\n" . $herecurr);
30110a920b5bSAndy Whitcroft			}
301200df344fSAndy Whitcroft		}
30130a920b5bSAndy Whitcroft
3014243f3803SJoe Perches		if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
3015243f3803SJoe Perches			my $orig = $1;
3016243f3803SJoe Perches			my $level = lc($orig);
3017243f3803SJoe Perches			$level = "warn" if ($level eq "warning");
30188f26b837SJoe Perches			my $level2 = $level;
30198f26b837SJoe Perches			$level2 = "dbg" if ($level eq "debug");
3020243f3803SJoe Perches			WARN("PREFER_PR_LEVEL",
3021daa8b059SYogesh Chaudhari			     "Prefer [subsystem eg: netdev]_$level2([subsystem]dev, ... then dev_$level2(dev, ... then pr_$level(...  to printk(KERN_$orig ...\n" . $herecurr);
3022243f3803SJoe Perches		}
3023243f3803SJoe Perches
3024243f3803SJoe Perches		if ($line =~ /\bpr_warning\s*\(/) {
3025d5e616fcSJoe Perches			if (WARN("PREFER_PR_LEVEL",
3026d5e616fcSJoe Perches				 "Prefer pr_warn(... to pr_warning(...\n" . $herecurr) &&
3027d5e616fcSJoe Perches			    $fix) {
3028d5e616fcSJoe Perches				$fixed[$linenr - 1] =~
3029d5e616fcSJoe Perches				    s/\bpr_warning\b/pr_warn/;
3030d5e616fcSJoe Perches			}
3031243f3803SJoe Perches		}
3032243f3803SJoe Perches
3033dc139313SJoe Perches		if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
3034dc139313SJoe Perches			my $orig = $1;
3035dc139313SJoe Perches			my $level = lc($orig);
3036dc139313SJoe Perches			$level = "warn" if ($level eq "warning");
3037dc139313SJoe Perches			$level = "dbg" if ($level eq "debug");
3038dc139313SJoe Perches			WARN("PREFER_DEV_LEVEL",
3039dc139313SJoe Perches			     "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
3040dc139313SJoe Perches		}
3041dc139313SJoe Perches
3042653d4876SAndy Whitcroft# function brace can't be on same line, except for #defines of do while,
3043653d4876SAndy Whitcroft# or if closed on same line
3044c45dcabdSAndy Whitcroft		if (($line=~/$Type\s*$Ident\(.*\).*\s{/) and
3045c45dcabdSAndy Whitcroft		    !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) {
3046000d1cc1SJoe Perches			ERROR("OPEN_BRACE",
3047000d1cc1SJoe Perches			      "open brace '{' following function declarations go on the next line\n" . $herecurr);
30480a920b5bSAndy Whitcroft		}
3049653d4876SAndy Whitcroft
30508905a67cSAndy Whitcroft# open braces for enum, union and struct go on the same line.
30518905a67cSAndy Whitcroft		if ($line =~ /^.\s*{/ &&
30528905a67cSAndy Whitcroft		    $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
3053000d1cc1SJoe Perches			ERROR("OPEN_BRACE",
3054000d1cc1SJoe Perches			      "open brace '{' following $1 go on the same line\n" . $hereprev);
30558905a67cSAndy Whitcroft		}
30568905a67cSAndy Whitcroft
30570c73b4ebSAndy Whitcroft# missing space after union, struct or enum definition
30583705ce5bSJoe Perches		if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
30593705ce5bSJoe Perches			if (WARN("SPACING",
30603705ce5bSJoe Perches				 "missing space after $1 definition\n" . $herecurr) &&
30613705ce5bSJoe Perches			    $fix) {
30623705ce5bSJoe Perches				$fixed[$linenr - 1] =~
30633705ce5bSJoe Perches				    s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
30643705ce5bSJoe Perches			}
30650c73b4ebSAndy Whitcroft		}
30660c73b4ebSAndy Whitcroft
306731070b5dSJoe Perches# Function pointer declarations
306831070b5dSJoe Perches# check spacing between type, funcptr, and args
306931070b5dSJoe Perches# canonical declaration is "type (*funcptr)(args...)"
307091f72e9cSJoe Perches		if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) {
307131070b5dSJoe Perches			my $declare = $1;
307231070b5dSJoe Perches			my $pre_pointer_space = $2;
307331070b5dSJoe Perches			my $post_pointer_space = $3;
307431070b5dSJoe Perches			my $funcname = $4;
307531070b5dSJoe Perches			my $post_funcname_space = $5;
307631070b5dSJoe Perches			my $pre_args_space = $6;
307731070b5dSJoe Perches
307891f72e9cSJoe Perches# the $Declare variable will capture all spaces after the type
307991f72e9cSJoe Perches# so check it for a missing trailing missing space but pointer return types
308091f72e9cSJoe Perches# don't need a space so don't warn for those.
308191f72e9cSJoe Perches			my $post_declare_space = "";
308291f72e9cSJoe Perches			if ($declare =~ /(\s+)$/) {
308391f72e9cSJoe Perches				$post_declare_space = $1;
308491f72e9cSJoe Perches				$declare = rtrim($declare);
308591f72e9cSJoe Perches			}
308691f72e9cSJoe Perches			if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) {
308731070b5dSJoe Perches				WARN("SPACING",
308831070b5dSJoe Perches				     "missing space after return type\n" . $herecurr);
308991f72e9cSJoe Perches				$post_declare_space = " ";
309031070b5dSJoe Perches			}
309131070b5dSJoe Perches
309231070b5dSJoe Perches# unnecessary space "type  (*funcptr)(args...)"
309391f72e9cSJoe Perches# This test is not currently implemented because these declarations are
309491f72e9cSJoe Perches# equivalent to
309591f72e9cSJoe Perches#	int  foo(int bar, ...)
309691f72e9cSJoe Perches# and this is form shouldn't/doesn't generate a checkpatch warning.
309791f72e9cSJoe Perches#
309891f72e9cSJoe Perches#			elsif ($declare =~ /\s{2,}$/) {
309991f72e9cSJoe Perches#				WARN("SPACING",
310091f72e9cSJoe Perches#				     "Multiple spaces after return type\n" . $herecurr);
310191f72e9cSJoe Perches#			}
310231070b5dSJoe Perches
310331070b5dSJoe Perches# unnecessary space "type ( *funcptr)(args...)"
310431070b5dSJoe Perches			if (defined $pre_pointer_space &&
310531070b5dSJoe Perches			    $pre_pointer_space =~ /^\s/) {
310631070b5dSJoe Perches				WARN("SPACING",
310731070b5dSJoe Perches				     "Unnecessary space after function pointer open parenthesis\n" . $herecurr);
310831070b5dSJoe Perches			}
310931070b5dSJoe Perches
311031070b5dSJoe Perches# unnecessary space "type (* funcptr)(args...)"
311131070b5dSJoe Perches			if (defined $post_pointer_space &&
311231070b5dSJoe Perches			    $post_pointer_space =~ /^\s/) {
311331070b5dSJoe Perches				WARN("SPACING",
311431070b5dSJoe Perches				     "Unnecessary space before function pointer name\n" . $herecurr);
311531070b5dSJoe Perches			}
311631070b5dSJoe Perches
311731070b5dSJoe Perches# unnecessary space "type (*funcptr )(args...)"
311831070b5dSJoe Perches			if (defined $post_funcname_space &&
311931070b5dSJoe Perches			    $post_funcname_space =~ /^\s/) {
312031070b5dSJoe Perches				WARN("SPACING",
312131070b5dSJoe Perches				     "Unnecessary space after function pointer name\n" . $herecurr);
312231070b5dSJoe Perches			}
312331070b5dSJoe Perches
312431070b5dSJoe Perches# unnecessary space "type (*funcptr) (args...)"
312531070b5dSJoe Perches			if (defined $pre_args_space &&
312631070b5dSJoe Perches			    $pre_args_space =~ /^\s/) {
312731070b5dSJoe Perches				WARN("SPACING",
312831070b5dSJoe Perches				     "Unnecessary space before function pointer arguments\n" . $herecurr);
312931070b5dSJoe Perches			}
313031070b5dSJoe Perches
313131070b5dSJoe Perches			if (show_type("SPACING") && $fix) {
313231070b5dSJoe Perches				$fixed[$linenr - 1] =~
313391f72e9cSJoe Perches				    s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex;
313431070b5dSJoe Perches			}
313531070b5dSJoe Perches		}
313631070b5dSJoe Perches
31378d31cfceSAndy Whitcroft# check for spacing round square brackets; allowed:
31388d31cfceSAndy Whitcroft#  1. with a type on the left -- int [] a;
3139fe2a7dbcSAndy Whitcroft#  2. at the beginning of a line for slice initialisers -- [0...10] = 5,
3140fe2a7dbcSAndy Whitcroft#  3. inside a curly brace -- = { [0...10] = 5 }
31418d31cfceSAndy Whitcroft		while ($line =~ /(.*?\s)\[/g) {
31428d31cfceSAndy Whitcroft			my ($where, $prefix) = ($-[1], $1);
31438d31cfceSAndy Whitcroft			if ($prefix !~ /$Type\s+$/ &&
3144fe2a7dbcSAndy Whitcroft			    ($where != 0 || $prefix !~ /^.\s+$/) &&
3145daebc534SAndy Whitcroft			    $prefix !~ /[{,]\s+$/) {
31463705ce5bSJoe Perches				if (ERROR("BRACKET_SPACE",
31473705ce5bSJoe Perches					  "space prohibited before open square bracket '['\n" . $herecurr) &&
31483705ce5bSJoe Perches				    $fix) {
31493705ce5bSJoe Perches				    $fixed[$linenr - 1] =~
31503705ce5bSJoe Perches					s/^(\+.*?)\s+\[/$1\[/;
31513705ce5bSJoe Perches				}
31528d31cfceSAndy Whitcroft			}
31538d31cfceSAndy Whitcroft		}
31548d31cfceSAndy Whitcroft
3155f0a594c1SAndy Whitcroft# check for spaces between functions and their parentheses.
31566c72ffaaSAndy Whitcroft		while ($line =~ /($Ident)\s+\(/g) {
3157c2fdda0dSAndy Whitcroft			my $name = $1;
3158773647a0SAndy Whitcroft			my $ctx_before = substr($line, 0, $-[1]);
3159773647a0SAndy Whitcroft			my $ctx = "$ctx_before$name";
3160c2fdda0dSAndy Whitcroft
3161c2fdda0dSAndy Whitcroft			# Ignore those directives where spaces _are_ permitted.
3162773647a0SAndy Whitcroft			if ($name =~ /^(?:
3163773647a0SAndy Whitcroft				if|for|while|switch|return|case|
3164773647a0SAndy Whitcroft				volatile|__volatile__|
3165773647a0SAndy Whitcroft				__attribute__|format|__extension__|
3166773647a0SAndy Whitcroft				asm|__asm__)$/x)
3167773647a0SAndy Whitcroft			{
3168c2fdda0dSAndy Whitcroft			# cpp #define statements have non-optional spaces, ie
3169c2fdda0dSAndy Whitcroft			# if there is a space between the name and the open
3170c2fdda0dSAndy Whitcroft			# parenthesis it is simply not a parameter group.
3171c45dcabdSAndy Whitcroft			} elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
3172773647a0SAndy Whitcroft
3173773647a0SAndy Whitcroft			# cpp #elif statement condition may start with a (
3174c45dcabdSAndy Whitcroft			} elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
3175c2fdda0dSAndy Whitcroft
3176c2fdda0dSAndy Whitcroft			# If this whole things ends with a type its most
3177c2fdda0dSAndy Whitcroft			# likely a typedef for a function.
3178773647a0SAndy Whitcroft			} elsif ($ctx =~ /$Type$/) {
3179c2fdda0dSAndy Whitcroft
3180c2fdda0dSAndy Whitcroft			} else {
31813705ce5bSJoe Perches				if (WARN("SPACING",
31823705ce5bSJoe Perches					 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
31833705ce5bSJoe Perches					     $fix) {
31843705ce5bSJoe Perches					$fixed[$linenr - 1] =~
31853705ce5bSJoe Perches					    s/\b$name\s+\(/$name\(/;
31863705ce5bSJoe Perches				}
3187f0a594c1SAndy Whitcroft			}
31886c72ffaaSAndy Whitcroft		}
31899a4cad4eSEric Nelson
3190653d4876SAndy Whitcroft# Check operator spacing.
31910a920b5bSAndy Whitcroft		if (!($line=~/\#\s*include/)) {
31923705ce5bSJoe Perches			my $fixed_line = "";
31933705ce5bSJoe Perches			my $line_fixed = 0;
31943705ce5bSJoe Perches
31959c0ca6f9SAndy Whitcroft			my $ops = qr{
31969c0ca6f9SAndy Whitcroft				<<=|>>=|<=|>=|==|!=|
31979c0ca6f9SAndy Whitcroft				\+=|-=|\*=|\/=|%=|\^=|\|=|&=|
31989c0ca6f9SAndy Whitcroft				=>|->|<<|>>|<|>|=|!|~|
31991f65f947SAndy Whitcroft				&&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
320084731623SJoe Perches				\?:|\?|:
32019c0ca6f9SAndy Whitcroft			}x;
3202cf655043SAndy Whitcroft			my @elements = split(/($ops|;)/, $opline);
32033705ce5bSJoe Perches
32043705ce5bSJoe Perches##			print("element count: <" . $#elements . ">\n");
32053705ce5bSJoe Perches##			foreach my $el (@elements) {
32063705ce5bSJoe Perches##				print("el: <$el>\n");
32073705ce5bSJoe Perches##			}
32083705ce5bSJoe Perches
32093705ce5bSJoe Perches			my @fix_elements = ();
321000df344fSAndy Whitcroft			my $off = 0;
32116c72ffaaSAndy Whitcroft
32123705ce5bSJoe Perches			foreach my $el (@elements) {
32133705ce5bSJoe Perches				push(@fix_elements, substr($rawline, $off, length($el)));
32143705ce5bSJoe Perches				$off += length($el);
32153705ce5bSJoe Perches			}
32163705ce5bSJoe Perches
32173705ce5bSJoe Perches			$off = 0;
32183705ce5bSJoe Perches
32196c72ffaaSAndy Whitcroft			my $blank = copy_spacing($opline);
3220b34c648bSJoe Perches			my $last_after = -1;
32216c72ffaaSAndy Whitcroft
32220a920b5bSAndy Whitcroft			for (my $n = 0; $n < $#elements; $n += 2) {
32233705ce5bSJoe Perches
32243705ce5bSJoe Perches				my $good = $fix_elements[$n] . $fix_elements[$n + 1];
32253705ce5bSJoe Perches
32263705ce5bSJoe Perches##				print("n: <$n> good: <$good>\n");
32273705ce5bSJoe Perches
32284a0df2efSAndy Whitcroft				$off += length($elements[$n]);
32294a0df2efSAndy Whitcroft
323025985edcSLucas De Marchi				# Pick up the preceding and succeeding characters.
3231773647a0SAndy Whitcroft				my $ca = substr($opline, 0, $off);
3232773647a0SAndy Whitcroft				my $cc = '';
3233773647a0SAndy Whitcroft				if (length($opline) >= ($off + length($elements[$n + 1]))) {
3234773647a0SAndy Whitcroft					$cc = substr($opline, $off + length($elements[$n + 1]));
3235773647a0SAndy Whitcroft				}
3236773647a0SAndy Whitcroft				my $cb = "$ca$;$cc";
3237773647a0SAndy Whitcroft
32384a0df2efSAndy Whitcroft				my $a = '';
32394a0df2efSAndy Whitcroft				$a = 'V' if ($elements[$n] ne '');
32404a0df2efSAndy Whitcroft				$a = 'W' if ($elements[$n] =~ /\s$/);
3241cf655043SAndy Whitcroft				$a = 'C' if ($elements[$n] =~ /$;$/);
32424a0df2efSAndy Whitcroft				$a = 'B' if ($elements[$n] =~ /(\[|\()$/);
32434a0df2efSAndy Whitcroft				$a = 'O' if ($elements[$n] eq '');
3244773647a0SAndy Whitcroft				$a = 'E' if ($ca =~ /^\s*$/);
32454a0df2efSAndy Whitcroft
32460a920b5bSAndy Whitcroft				my $op = $elements[$n + 1];
32474a0df2efSAndy Whitcroft
32484a0df2efSAndy Whitcroft				my $c = '';
32490a920b5bSAndy Whitcroft				if (defined $elements[$n + 2]) {
32504a0df2efSAndy Whitcroft					$c = 'V' if ($elements[$n + 2] ne '');
32514a0df2efSAndy Whitcroft					$c = 'W' if ($elements[$n + 2] =~ /^\s/);
3252cf655043SAndy Whitcroft					$c = 'C' if ($elements[$n + 2] =~ /^$;/);
32534a0df2efSAndy Whitcroft					$c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
32544a0df2efSAndy Whitcroft					$c = 'O' if ($elements[$n + 2] eq '');
32558b1b3378SAndy Whitcroft					$c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
32564a0df2efSAndy Whitcroft				} else {
32574a0df2efSAndy Whitcroft					$c = 'E';
32580a920b5bSAndy Whitcroft				}
32590a920b5bSAndy Whitcroft
32604a0df2efSAndy Whitcroft				my $ctx = "${a}x${c}";
32614a0df2efSAndy Whitcroft
32624a0df2efSAndy Whitcroft				my $at = "(ctx:$ctx)";
32634a0df2efSAndy Whitcroft
32646c72ffaaSAndy Whitcroft				my $ptr = substr($blank, 0, $off) . "^";
3265de7d4f0eSAndy Whitcroft				my $hereptr = "$hereline$ptr\n";
32660a920b5bSAndy Whitcroft
326774048ed8SAndy Whitcroft				# Pull out the value of this operator.
32686c72ffaaSAndy Whitcroft				my $op_type = substr($curr_values, $off + 1, 1);
32690a920b5bSAndy Whitcroft
32701f65f947SAndy Whitcroft				# Get the full operator variant.
32711f65f947SAndy Whitcroft				my $opv = $op . substr($curr_vars, $off, 1);
32721f65f947SAndy Whitcroft
327313214adfSAndy Whitcroft				# Ignore operators passed as parameters.
327413214adfSAndy Whitcroft				if ($op_type ne 'V' &&
327513214adfSAndy Whitcroft				    $ca =~ /\s$/ && $cc =~ /^\s*,/) {
327613214adfSAndy Whitcroft
3277cf655043SAndy Whitcroft#				# Ignore comments
3278cf655043SAndy Whitcroft#				} elsif ($op =~ /^$;+$/) {
327913214adfSAndy Whitcroft
3280d8aaf121SAndy Whitcroft				# ; should have either the end of line or a space or \ after it
328113214adfSAndy Whitcroft				} elsif ($op eq ';') {
3282cf655043SAndy Whitcroft					if ($ctx !~ /.x[WEBC]/ &&
3283cf655043SAndy Whitcroft					    $cc !~ /^\\/ && $cc !~ /^;/) {
32843705ce5bSJoe Perches						if (ERROR("SPACING",
32853705ce5bSJoe Perches							  "space required after that '$op' $at\n" . $hereptr)) {
3286b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
32873705ce5bSJoe Perches							$line_fixed = 1;
32883705ce5bSJoe Perches						}
3289d8aaf121SAndy Whitcroft					}
3290d8aaf121SAndy Whitcroft
3291d8aaf121SAndy Whitcroft				# // is a comment
3292d8aaf121SAndy Whitcroft				} elsif ($op eq '//') {
32930a920b5bSAndy Whitcroft
3294b00e4814SJoe Perches				#   :   when part of a bitfield
3295b00e4814SJoe Perches				} elsif ($opv eq ':B') {
3296b00e4814SJoe Perches					# skip the bitfield test for now
3297b00e4814SJoe Perches
32981f65f947SAndy Whitcroft				# No spaces for:
32991f65f947SAndy Whitcroft				#   ->
3300b00e4814SJoe Perches				} elsif ($op eq '->') {
33014a0df2efSAndy Whitcroft					if ($ctx =~ /Wx.|.xW/) {
33023705ce5bSJoe Perches						if (ERROR("SPACING",
33033705ce5bSJoe Perches							  "spaces prohibited around that '$op' $at\n" . $hereptr)) {
3304b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
33053705ce5bSJoe Perches							if (defined $fix_elements[$n + 2]) {
33063705ce5bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
33073705ce5bSJoe Perches							}
3308b34c648bSJoe Perches							$line_fixed = 1;
33093705ce5bSJoe Perches						}
33100a920b5bSAndy Whitcroft					}
33110a920b5bSAndy Whitcroft
33120a920b5bSAndy Whitcroft				# , must have a space on the right.
33130a920b5bSAndy Whitcroft				} elsif ($op eq ',') {
3314cf655043SAndy Whitcroft					if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
33153705ce5bSJoe Perches						if (ERROR("SPACING",
33163705ce5bSJoe Perches							  "space required after that '$op' $at\n" . $hereptr)) {
3317b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
33183705ce5bSJoe Perches							$line_fixed = 1;
3319b34c648bSJoe Perches							$last_after = $n;
33203705ce5bSJoe Perches						}
33210a920b5bSAndy Whitcroft					}
33220a920b5bSAndy Whitcroft
33239c0ca6f9SAndy Whitcroft				# '*' as part of a type definition -- reported already.
332474048ed8SAndy Whitcroft				} elsif ($opv eq '*_') {
33259c0ca6f9SAndy Whitcroft					#warn "'*' is part of type\n";
33269c0ca6f9SAndy Whitcroft
33279c0ca6f9SAndy Whitcroft				# unary operators should have a space before and
33289c0ca6f9SAndy Whitcroft				# none after.  May be left adjacent to another
33299c0ca6f9SAndy Whitcroft				# unary operator, or a cast
33309c0ca6f9SAndy Whitcroft				} elsif ($op eq '!' || $op eq '~' ||
333174048ed8SAndy Whitcroft					 $opv eq '*U' || $opv eq '-U' ||
33320d413866SAndy Whitcroft					 $opv eq '&U' || $opv eq '&&U') {
3333cf655043SAndy Whitcroft					if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
33343705ce5bSJoe Perches						if (ERROR("SPACING",
33353705ce5bSJoe Perches							  "space required before that '$op' $at\n" . $hereptr)) {
3336b34c648bSJoe Perches							if ($n != $last_after + 2) {
3337b34c648bSJoe Perches								$good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]);
33383705ce5bSJoe Perches								$line_fixed = 1;
33393705ce5bSJoe Perches							}
33400a920b5bSAndy Whitcroft						}
3341b34c648bSJoe Perches					}
3342a3340b35SAndy Whitcroft					if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
3343171ae1a4SAndy Whitcroft						# A unary '*' may be const
3344171ae1a4SAndy Whitcroft
3345171ae1a4SAndy Whitcroft					} elsif ($ctx =~ /.xW/) {
33463705ce5bSJoe Perches						if (ERROR("SPACING",
33473705ce5bSJoe Perches							  "space prohibited after that '$op' $at\n" . $hereptr)) {
3348b34c648bSJoe Perches							$good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]);
33493705ce5bSJoe Perches							if (defined $fix_elements[$n + 2]) {
33503705ce5bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
33513705ce5bSJoe Perches							}
3352b34c648bSJoe Perches							$line_fixed = 1;
33533705ce5bSJoe Perches						}
33540a920b5bSAndy Whitcroft					}
33550a920b5bSAndy Whitcroft
33560a920b5bSAndy Whitcroft				# unary ++ and unary -- are allowed no space on one side.
33570a920b5bSAndy Whitcroft				} elsif ($op eq '++' or $op eq '--') {
3358773647a0SAndy Whitcroft					if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
33593705ce5bSJoe Perches						if (ERROR("SPACING",
33603705ce5bSJoe Perches							  "space required one side of that '$op' $at\n" . $hereptr)) {
3361b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
33623705ce5bSJoe Perches							$line_fixed = 1;
33633705ce5bSJoe Perches						}
33640a920b5bSAndy Whitcroft					}
3365773647a0SAndy Whitcroft					if ($ctx =~ /Wx[BE]/ ||
3366773647a0SAndy Whitcroft					    ($ctx =~ /Wx./ && $cc =~ /^;/)) {
33673705ce5bSJoe Perches						if (ERROR("SPACING",
33683705ce5bSJoe Perches							  "space prohibited before that '$op' $at\n" . $hereptr)) {
3369b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
33703705ce5bSJoe Perches							$line_fixed = 1;
33713705ce5bSJoe Perches						}
3372653d4876SAndy Whitcroft					}
3373773647a0SAndy Whitcroft					if ($ctx =~ /ExW/) {
33743705ce5bSJoe Perches						if (ERROR("SPACING",
33753705ce5bSJoe Perches							  "space prohibited after that '$op' $at\n" . $hereptr)) {
3376b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
33773705ce5bSJoe Perches							if (defined $fix_elements[$n + 2]) {
33783705ce5bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
3379773647a0SAndy Whitcroft							}
3380b34c648bSJoe Perches							$line_fixed = 1;
33813705ce5bSJoe Perches						}
33823705ce5bSJoe Perches					}
33830a920b5bSAndy Whitcroft
33840a920b5bSAndy Whitcroft				# << and >> may either have or not have spaces both sides
33859c0ca6f9SAndy Whitcroft				} elsif ($op eq '<<' or $op eq '>>' or
33869c0ca6f9SAndy Whitcroft					 $op eq '&' or $op eq '^' or $op eq '|' or
33879c0ca6f9SAndy Whitcroft					 $op eq '+' or $op eq '-' or
3388c2fdda0dSAndy Whitcroft					 $op eq '*' or $op eq '/' or
3389c2fdda0dSAndy Whitcroft					 $op eq '%')
33900a920b5bSAndy Whitcroft				{
3391773647a0SAndy Whitcroft					if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
33923705ce5bSJoe Perches						if (ERROR("SPACING",
33933705ce5bSJoe Perches							  "need consistent spacing around '$op' $at\n" . $hereptr)) {
3394b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
3395b34c648bSJoe Perches							if (defined $fix_elements[$n + 2]) {
3396b34c648bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
3397b34c648bSJoe Perches							}
33983705ce5bSJoe Perches							$line_fixed = 1;
33993705ce5bSJoe Perches						}
34000a920b5bSAndy Whitcroft					}
34010a920b5bSAndy Whitcroft
34021f65f947SAndy Whitcroft				# A colon needs no spaces before when it is
34031f65f947SAndy Whitcroft				# terminating a case value or a label.
34041f65f947SAndy Whitcroft				} elsif ($opv eq ':C' || $opv eq ':L') {
34051f65f947SAndy Whitcroft					if ($ctx =~ /Wx./) {
34063705ce5bSJoe Perches						if (ERROR("SPACING",
34073705ce5bSJoe Perches							  "space prohibited before that '$op' $at\n" . $hereptr)) {
3408b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
34093705ce5bSJoe Perches							$line_fixed = 1;
34103705ce5bSJoe Perches						}
34111f65f947SAndy Whitcroft					}
34121f65f947SAndy Whitcroft
34130a920b5bSAndy Whitcroft				# All the others need spaces both sides.
3414cf655043SAndy Whitcroft				} elsif ($ctx !~ /[EWC]x[CWE]/) {
34151f65f947SAndy Whitcroft					my $ok = 0;
34161f65f947SAndy Whitcroft
341722f2a2efSAndy Whitcroft					# Ignore email addresses <foo@bar>
34181f65f947SAndy Whitcroft					if (($op eq '<' &&
34191f65f947SAndy Whitcroft					     $cc =~ /^\S+\@\S+>/) ||
34201f65f947SAndy Whitcroft					    ($op eq '>' &&
34211f65f947SAndy Whitcroft					     $ca =~ /<\S+\@\S+$/))
34221f65f947SAndy Whitcroft					{
34231f65f947SAndy Whitcroft					    	$ok = 1;
34241f65f947SAndy Whitcroft					}
34251f65f947SAndy Whitcroft
342684731623SJoe Perches					# messages are ERROR, but ?: are CHK
34271f65f947SAndy Whitcroft					if ($ok == 0) {
342884731623SJoe Perches						my $msg_type = \&ERROR;
342984731623SJoe Perches						$msg_type = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
343084731623SJoe Perches
343184731623SJoe Perches						if (&{$msg_type}("SPACING",
34323705ce5bSJoe Perches								 "spaces required around that '$op' $at\n" . $hereptr)) {
3433b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
3434b34c648bSJoe Perches							if (defined $fix_elements[$n + 2]) {
3435b34c648bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
3436b34c648bSJoe Perches							}
34373705ce5bSJoe Perches							$line_fixed = 1;
34383705ce5bSJoe Perches						}
34390a920b5bSAndy Whitcroft					}
344022f2a2efSAndy Whitcroft				}
34414a0df2efSAndy Whitcroft				$off += length($elements[$n + 1]);
34423705ce5bSJoe Perches
34433705ce5bSJoe Perches##				print("n: <$n> GOOD: <$good>\n");
34443705ce5bSJoe Perches
34453705ce5bSJoe Perches				$fixed_line = $fixed_line . $good;
34460a920b5bSAndy Whitcroft			}
34473705ce5bSJoe Perches
34483705ce5bSJoe Perches			if (($#elements % 2) == 0) {
34493705ce5bSJoe Perches				$fixed_line = $fixed_line . $fix_elements[$#elements];
34503705ce5bSJoe Perches			}
34513705ce5bSJoe Perches
34523705ce5bSJoe Perches			if ($fix && $line_fixed && $fixed_line ne $fixed[$linenr - 1]) {
34533705ce5bSJoe Perches				$fixed[$linenr - 1] = $fixed_line;
34543705ce5bSJoe Perches			}
34553705ce5bSJoe Perches
34563705ce5bSJoe Perches
34570a920b5bSAndy Whitcroft		}
34580a920b5bSAndy Whitcroft
3459786b6326SJoe Perches# check for whitespace before a non-naked semicolon
3460d2e248e7SJoe Perches		if ($line =~ /^\+.*\S\s+;\s*$/) {
3461786b6326SJoe Perches			if (WARN("SPACING",
3462786b6326SJoe Perches				 "space prohibited before semicolon\n" . $herecurr) &&
3463786b6326SJoe Perches			    $fix) {
3464786b6326SJoe Perches				1 while $fixed[$linenr - 1] =~
3465786b6326SJoe Perches				    s/^(\+.*\S)\s+;/$1;/;
3466786b6326SJoe Perches			}
3467786b6326SJoe Perches		}
3468786b6326SJoe Perches
3469f0a594c1SAndy Whitcroft# check for multiple assignments
3470f0a594c1SAndy Whitcroft		if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
3471000d1cc1SJoe Perches			CHK("MULTIPLE_ASSIGNMENTS",
3472000d1cc1SJoe Perches			    "multiple assignments should be avoided\n" . $herecurr);
3473f0a594c1SAndy Whitcroft		}
3474f0a594c1SAndy Whitcroft
347522f2a2efSAndy Whitcroft## # check for multiple declarations, allowing for a function declaration
347622f2a2efSAndy Whitcroft## # continuation.
347722f2a2efSAndy Whitcroft## 		if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
347822f2a2efSAndy Whitcroft## 		    $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
347922f2a2efSAndy Whitcroft##
348022f2a2efSAndy Whitcroft## 			# Remove any bracketed sections to ensure we do not
348122f2a2efSAndy Whitcroft## 			# falsly report the parameters of functions.
348222f2a2efSAndy Whitcroft## 			my $ln = $line;
348322f2a2efSAndy Whitcroft## 			while ($ln =~ s/\([^\(\)]*\)//g) {
348422f2a2efSAndy Whitcroft## 			}
348522f2a2efSAndy Whitcroft## 			if ($ln =~ /,/) {
3486000d1cc1SJoe Perches## 				WARN("MULTIPLE_DECLARATION",
3487000d1cc1SJoe Perches##				     "declaring multiple variables together should be avoided\n" . $herecurr);
348822f2a2efSAndy Whitcroft## 			}
348922f2a2efSAndy Whitcroft## 		}
3490f0a594c1SAndy Whitcroft
34910a920b5bSAndy Whitcroft#need space before brace following if, while, etc
349222f2a2efSAndy Whitcroft		if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
349322f2a2efSAndy Whitcroft		    $line =~ /do{/) {
34943705ce5bSJoe Perches			if (ERROR("SPACING",
34953705ce5bSJoe Perches				  "space required before the open brace '{'\n" . $herecurr) &&
34963705ce5bSJoe Perches			    $fix) {
3497d5e616fcSJoe Perches				$fixed[$linenr - 1] =~ s/^(\+.*(?:do|\))){/$1 {/;
34983705ce5bSJoe Perches			}
3499de7d4f0eSAndy Whitcroft		}
3500de7d4f0eSAndy Whitcroft
3501c4a62ef9SJoe Perches## # check for blank lines before declarations
3502c4a62ef9SJoe Perches##		if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
3503c4a62ef9SJoe Perches##		    $prevrawline =~ /^.\s*$/) {
3504c4a62ef9SJoe Perches##			WARN("SPACING",
3505c4a62ef9SJoe Perches##			     "No blank lines before declarations\n" . $hereprev);
3506c4a62ef9SJoe Perches##		}
3507c4a62ef9SJoe Perches##
3508c4a62ef9SJoe Perches
3509de7d4f0eSAndy Whitcroft# closing brace should have a space following it when it has anything
3510de7d4f0eSAndy Whitcroft# on the line
3511de7d4f0eSAndy Whitcroft		if ($line =~ /}(?!(?:,|;|\)))\S/) {
3512d5e616fcSJoe Perches			if (ERROR("SPACING",
3513d5e616fcSJoe Perches				  "space required after that close brace '}'\n" . $herecurr) &&
3514d5e616fcSJoe Perches			    $fix) {
3515d5e616fcSJoe Perches				$fixed[$linenr - 1] =~
3516d5e616fcSJoe Perches				    s/}((?!(?:,|;|\)))\S)/} $1/;
3517d5e616fcSJoe Perches			}
35180a920b5bSAndy Whitcroft		}
35190a920b5bSAndy Whitcroft
352022f2a2efSAndy Whitcroft# check spacing on square brackets
352122f2a2efSAndy Whitcroft		if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
35223705ce5bSJoe Perches			if (ERROR("SPACING",
35233705ce5bSJoe Perches				  "space prohibited after that open square bracket '['\n" . $herecurr) &&
35243705ce5bSJoe Perches			    $fix) {
35253705ce5bSJoe Perches				$fixed[$linenr - 1] =~
35263705ce5bSJoe Perches				    s/\[\s+/\[/;
35273705ce5bSJoe Perches			}
352822f2a2efSAndy Whitcroft		}
352922f2a2efSAndy Whitcroft		if ($line =~ /\s\]/) {
35303705ce5bSJoe Perches			if (ERROR("SPACING",
35313705ce5bSJoe Perches				  "space prohibited before that close square bracket ']'\n" . $herecurr) &&
35323705ce5bSJoe Perches			    $fix) {
35333705ce5bSJoe Perches				$fixed[$linenr - 1] =~
35343705ce5bSJoe Perches				    s/\s+\]/\]/;
35353705ce5bSJoe Perches			}
353622f2a2efSAndy Whitcroft		}
353722f2a2efSAndy Whitcroft
3538c45dcabdSAndy Whitcroft# check spacing on parentheses
35399c0ca6f9SAndy Whitcroft		if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
35409c0ca6f9SAndy Whitcroft		    $line !~ /for\s*\(\s+;/) {
35413705ce5bSJoe Perches			if (ERROR("SPACING",
35423705ce5bSJoe Perches				  "space prohibited after that open parenthesis '('\n" . $herecurr) &&
35433705ce5bSJoe Perches			    $fix) {
35443705ce5bSJoe Perches				$fixed[$linenr - 1] =~
35453705ce5bSJoe Perches				    s/\(\s+/\(/;
35463705ce5bSJoe Perches			}
354722f2a2efSAndy Whitcroft		}
354813214adfSAndy Whitcroft		if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
3549c45dcabdSAndy Whitcroft		    $line !~ /for\s*\(.*;\s+\)/ &&
3550c45dcabdSAndy Whitcroft		    $line !~ /:\s+\)/) {
35513705ce5bSJoe Perches			if (ERROR("SPACING",
35523705ce5bSJoe Perches				  "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
35533705ce5bSJoe Perches			    $fix) {
35543705ce5bSJoe Perches				$fixed[$linenr - 1] =~
35553705ce5bSJoe Perches				    s/\s+\)/\)/;
35563705ce5bSJoe Perches			}
355722f2a2efSAndy Whitcroft		}
355822f2a2efSAndy Whitcroft
3559e2826fd0SJoe Perches# check unnecessary parentheses around addressof/dereference single $Lvals
3560e2826fd0SJoe Perches# ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar
3561e2826fd0SJoe Perches
3562e2826fd0SJoe Perches		while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) {
3563e2826fd0SJoe Perches			CHK("UNNECESSARY_PARENTHESES",
3564e2826fd0SJoe Perches			    "Unnecessary parentheses around $1\n" . $herecurr);
3565e2826fd0SJoe Perches		    }
3566e2826fd0SJoe Perches
35670a920b5bSAndy Whitcroft#goto labels aren't indented, allow a single space however
35684a0df2efSAndy Whitcroft		if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
35690a920b5bSAndy Whitcroft		   !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
35703705ce5bSJoe Perches			if (WARN("INDENTED_LABEL",
35713705ce5bSJoe Perches				 "labels should not be indented\n" . $herecurr) &&
35723705ce5bSJoe Perches			    $fix) {
35733705ce5bSJoe Perches				$fixed[$linenr - 1] =~
35743705ce5bSJoe Perches				    s/^(.)\s+/$1/;
35753705ce5bSJoe Perches			}
35760a920b5bSAndy Whitcroft		}
35770a920b5bSAndy Whitcroft
35785b9553abSJoe Perches# return is not a function
3579507e5141SJoe Perches		if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {
3580c45dcabdSAndy Whitcroft			my $spacing = $1;
3581507e5141SJoe Perches			if ($^V && $^V ge 5.10.0 &&
35825b9553abSJoe Perches			    $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) {
35835b9553abSJoe Perches				my $value = $1;
35845b9553abSJoe Perches				$value = deparenthesize($value);
35855b9553abSJoe Perches				if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) {
3586000d1cc1SJoe Perches					ERROR("RETURN_PARENTHESES",
3587000d1cc1SJoe Perches					      "return is not a function, parentheses are not required\n" . $herecurr);
35885b9553abSJoe Perches				}
3589c45dcabdSAndy Whitcroft			} elsif ($spacing !~ /\s+/) {
3590000d1cc1SJoe Perches				ERROR("SPACING",
3591000d1cc1SJoe Perches				      "space required before the open parenthesis '('\n" . $herecurr);
3592c45dcabdSAndy Whitcroft			}
3593c45dcabdSAndy Whitcroft		}
3594507e5141SJoe Perches
3595b43ae21bSJoe Perches# unnecessary return in a void function
3596b43ae21bSJoe Perches# at end-of-function, with the previous line a single leading tab, then return;
3597b43ae21bSJoe Perches# and the line before that not a goto label target like "out:"
3598b43ae21bSJoe Perches		if ($sline =~ /^[ \+]}\s*$/ &&
3599b43ae21bSJoe Perches		    $prevline =~ /^\+\treturn\s*;\s*$/ &&
3600b43ae21bSJoe Perches		    $linenr >= 3 &&
3601b43ae21bSJoe Perches		    $lines[$linenr - 3] =~ /^[ +]/ &&
3602b43ae21bSJoe Perches		    $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) {
36039819cf25SJoe Perches			WARN("RETURN_VOID",
3604b43ae21bSJoe Perches			     "void function return statements are not generally useful\n" . $hereprev);
36059819cf25SJoe Perches               }
36069819cf25SJoe Perches
3607189248d8SJoe Perches# if statements using unnecessary parentheses - ie: if ((foo == bar))
3608189248d8SJoe Perches		if ($^V && $^V ge 5.10.0 &&
3609189248d8SJoe Perches		    $line =~ /\bif\s*((?:\(\s*){2,})/) {
3610189248d8SJoe Perches			my $openparens = $1;
3611189248d8SJoe Perches			my $count = $openparens =~ tr@\(@\(@;
3612189248d8SJoe Perches			my $msg = "";
3613189248d8SJoe Perches			if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) {
3614189248d8SJoe Perches				my $comp = $4;	#Not $1 because of $LvalOrFunc
3615189248d8SJoe Perches				$msg = " - maybe == should be = ?" if ($comp eq "==");
3616189248d8SJoe Perches				WARN("UNNECESSARY_PARENTHESES",
3617189248d8SJoe Perches				     "Unnecessary parentheses$msg\n" . $herecurr);
3618189248d8SJoe Perches			}
3619189248d8SJoe Perches		}
3620189248d8SJoe Perches
362153a3c448SAndy Whitcroft# Return of what appears to be an errno should normally be -'ve
362253a3c448SAndy Whitcroft		if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) {
362353a3c448SAndy Whitcroft			my $name = $1;
362453a3c448SAndy Whitcroft			if ($name ne 'EOF' && $name ne 'ERROR') {
3625000d1cc1SJoe Perches				WARN("USE_NEGATIVE_ERRNO",
3626000d1cc1SJoe Perches				     "return of an errno should typically be -ve (return -$1)\n" . $herecurr);
362753a3c448SAndy Whitcroft			}
362853a3c448SAndy Whitcroft		}
3629c45dcabdSAndy Whitcroft
36300a920b5bSAndy Whitcroft# Need a space before open parenthesis after if, while etc
36314a0df2efSAndy Whitcroft		if ($line =~ /\b(if|while|for|switch)\(/) {
36323705ce5bSJoe Perches			if (ERROR("SPACING",
36333705ce5bSJoe Perches				  "space required before the open parenthesis '('\n" . $herecurr) &&
36343705ce5bSJoe Perches			    $fix) {
36353705ce5bSJoe Perches				$fixed[$linenr - 1] =~
36363705ce5bSJoe Perches				    s/\b(if|while|for|switch)\(/$1 \(/;
36373705ce5bSJoe Perches			}
36380a920b5bSAndy Whitcroft		}
36390a920b5bSAndy Whitcroft
3640f5fe35ddSAndy Whitcroft# Check for illegal assignment in if conditional -- and check for trailing
3641f5fe35ddSAndy Whitcroft# statements after the conditional.
3642170d3a22SAndy Whitcroft		if ($line =~ /do\s*(?!{)/) {
36433e469cdcSAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
36443e469cdcSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0)
36453e469cdcSAndy Whitcroft					if (!defined $stat);
3646170d3a22SAndy Whitcroft			my ($stat_next) = ctx_statement_block($line_nr_next,
3647170d3a22SAndy Whitcroft						$remain_next, $off_next);
3648170d3a22SAndy Whitcroft			$stat_next =~ s/\n./\n /g;
3649170d3a22SAndy Whitcroft			##print "stat<$stat> stat_next<$stat_next>\n";
3650170d3a22SAndy Whitcroft
3651170d3a22SAndy Whitcroft			if ($stat_next =~ /^\s*while\b/) {
3652170d3a22SAndy Whitcroft				# If the statement carries leading newlines,
3653170d3a22SAndy Whitcroft				# then count those as offsets.
3654170d3a22SAndy Whitcroft				my ($whitespace) =
3655170d3a22SAndy Whitcroft					($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
3656170d3a22SAndy Whitcroft				my $offset =
3657170d3a22SAndy Whitcroft					statement_rawlines($whitespace) - 1;
3658170d3a22SAndy Whitcroft
3659170d3a22SAndy Whitcroft				$suppress_whiletrailers{$line_nr_next +
3660170d3a22SAndy Whitcroft								$offset} = 1;
3661170d3a22SAndy Whitcroft			}
3662170d3a22SAndy Whitcroft		}
3663170d3a22SAndy Whitcroft		if (!defined $suppress_whiletrailers{$linenr} &&
3664c11230f4SJoe Perches		    defined($stat) && defined($cond) &&
3665170d3a22SAndy Whitcroft		    $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
3666171ae1a4SAndy Whitcroft			my ($s, $c) = ($stat, $cond);
36678905a67cSAndy Whitcroft
3668b53c8e10SAndy Whitcroft			if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
3669000d1cc1SJoe Perches				ERROR("ASSIGN_IN_IF",
3670000d1cc1SJoe Perches				      "do not use assignment in if condition\n" . $herecurr);
36718905a67cSAndy Whitcroft			}
36728905a67cSAndy Whitcroft
36738905a67cSAndy Whitcroft			# Find out what is on the end of the line after the
36748905a67cSAndy Whitcroft			# conditional.
3675773647a0SAndy Whitcroft			substr($s, 0, length($c), '');
36768905a67cSAndy Whitcroft			$s =~ s/\n.*//g;
367713214adfSAndy Whitcroft			$s =~ s/$;//g; 	# Remove any comments
367853210168SAndy Whitcroft			if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
367953210168SAndy Whitcroft			    $c !~ /}\s*while\s*/)
3680773647a0SAndy Whitcroft			{
3681bb44ad39SAndy Whitcroft				# Find out how long the conditional actually is.
3682bb44ad39SAndy Whitcroft				my @newlines = ($c =~ /\n/gs);
3683bb44ad39SAndy Whitcroft				my $cond_lines = 1 + $#newlines;
368442bdf74cSHidetoshi Seto				my $stat_real = '';
3685bb44ad39SAndy Whitcroft
368642bdf74cSHidetoshi Seto				$stat_real = raw_line($linenr, $cond_lines)
368742bdf74cSHidetoshi Seto							. "\n" if ($cond_lines);
3688bb44ad39SAndy Whitcroft				if (defined($stat_real) && $cond_lines > 1) {
3689bb44ad39SAndy Whitcroft					$stat_real = "[...]\n$stat_real";
3690bb44ad39SAndy Whitcroft				}
3691bb44ad39SAndy Whitcroft
3692000d1cc1SJoe Perches				ERROR("TRAILING_STATEMENTS",
3693000d1cc1SJoe Perches				      "trailing statements should be on next line\n" . $herecurr . $stat_real);
36948905a67cSAndy Whitcroft			}
36958905a67cSAndy Whitcroft		}
36968905a67cSAndy Whitcroft
369713214adfSAndy Whitcroft# Check for bitwise tests written as boolean
369813214adfSAndy Whitcroft		if ($line =~ /
369913214adfSAndy Whitcroft			(?:
370013214adfSAndy Whitcroft				(?:\[|\(|\&\&|\|\|)
370113214adfSAndy Whitcroft				\s*0[xX][0-9]+\s*
370213214adfSAndy Whitcroft				(?:\&\&|\|\|)
370313214adfSAndy Whitcroft			|
370413214adfSAndy Whitcroft				(?:\&\&|\|\|)
370513214adfSAndy Whitcroft				\s*0[xX][0-9]+\s*
370613214adfSAndy Whitcroft				(?:\&\&|\|\||\)|\])
370713214adfSAndy Whitcroft			)/x)
370813214adfSAndy Whitcroft		{
3709000d1cc1SJoe Perches			WARN("HEXADECIMAL_BOOLEAN_TEST",
3710000d1cc1SJoe Perches			     "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
371113214adfSAndy Whitcroft		}
371213214adfSAndy Whitcroft
37138905a67cSAndy Whitcroft# if and else should not have general statements after it
371413214adfSAndy Whitcroft		if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
371513214adfSAndy Whitcroft			my $s = $1;
371613214adfSAndy Whitcroft			$s =~ s/$;//g; 	# Remove any comments
371713214adfSAndy Whitcroft			if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
3718000d1cc1SJoe Perches				ERROR("TRAILING_STATEMENTS",
3719000d1cc1SJoe Perches				      "trailing statements should be on next line\n" . $herecurr);
37200a920b5bSAndy Whitcroft			}
372113214adfSAndy Whitcroft		}
372239667782SAndy Whitcroft# if should not continue a brace
372339667782SAndy Whitcroft		if ($line =~ /}\s*if\b/) {
3724000d1cc1SJoe Perches			ERROR("TRAILING_STATEMENTS",
3725048b123fSRasmus Villemoes			      "trailing statements should be on next line (or did you mean 'else if'?)\n" .
372639667782SAndy Whitcroft				$herecurr);
372739667782SAndy Whitcroft		}
3728a1080bf8SAndy Whitcroft# case and default should not have general statements after them
3729a1080bf8SAndy Whitcroft		if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
3730a1080bf8SAndy Whitcroft		    $line !~ /\G(?:
37313fef12d6SAndy Whitcroft			(?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
3732a1080bf8SAndy Whitcroft			\s*return\s+
3733a1080bf8SAndy Whitcroft		    )/xg)
3734a1080bf8SAndy Whitcroft		{
3735000d1cc1SJoe Perches			ERROR("TRAILING_STATEMENTS",
3736000d1cc1SJoe Perches			      "trailing statements should be on next line\n" . $herecurr);
3737a1080bf8SAndy Whitcroft		}
37380a920b5bSAndy Whitcroft
37390a920b5bSAndy Whitcroft		# Check for }<nl>else {, these must be at the same
37400a920b5bSAndy Whitcroft		# indent level to be relevant to each other.
37410a920b5bSAndy Whitcroft		if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
37420a920b5bSAndy Whitcroft						$previndent == $indent) {
3743000d1cc1SJoe Perches			ERROR("ELSE_AFTER_BRACE",
3744000d1cc1SJoe Perches			      "else should follow close brace '}'\n" . $hereprev);
37450a920b5bSAndy Whitcroft		}
37460a920b5bSAndy Whitcroft
3747c2fdda0dSAndy Whitcroft		if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and
3748c2fdda0dSAndy Whitcroft						$previndent == $indent) {
3749c2fdda0dSAndy Whitcroft			my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
3750c2fdda0dSAndy Whitcroft
3751c2fdda0dSAndy Whitcroft			# Find out what is on the end of the line after the
3752c2fdda0dSAndy Whitcroft			# conditional.
3753773647a0SAndy Whitcroft			substr($s, 0, length($c), '');
3754c2fdda0dSAndy Whitcroft			$s =~ s/\n.*//g;
3755c2fdda0dSAndy Whitcroft
3756c2fdda0dSAndy Whitcroft			if ($s =~ /^\s*;/) {
3757000d1cc1SJoe Perches				ERROR("WHILE_AFTER_BRACE",
3758000d1cc1SJoe Perches				      "while should follow close brace '}'\n" . $hereprev);
3759c2fdda0dSAndy Whitcroft			}
3760c2fdda0dSAndy Whitcroft		}
3761c2fdda0dSAndy Whitcroft
376295e2c602SJoe Perches#Specific variable tests
3763323c1260SJoe Perches		while ($line =~ m{($Constant|$Lval)}g) {
3764323c1260SJoe Perches			my $var = $1;
376595e2c602SJoe Perches
376695e2c602SJoe Perches#gcc binary extension
376795e2c602SJoe Perches			if ($var =~ /^$Binary$/) {
3768d5e616fcSJoe Perches				if (WARN("GCC_BINARY_CONSTANT",
3769d5e616fcSJoe Perches					 "Avoid gcc v4.3+ binary constant extension: <$var>\n" . $herecurr) &&
3770d5e616fcSJoe Perches				    $fix) {
3771d5e616fcSJoe Perches					my $hexval = sprintf("0x%x", oct($var));
3772d5e616fcSJoe Perches					$fixed[$linenr - 1] =~
3773d5e616fcSJoe Perches					    s/\b$var\b/$hexval/;
3774d5e616fcSJoe Perches				}
377595e2c602SJoe Perches			}
377695e2c602SJoe Perches
377795e2c602SJoe Perches#CamelCase
3778807bd26cSJoe Perches			if ($var !~ /^$Constant$/ &&
3779be79794bSJoe Perches			    $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
378022735ce8SJoe Perches#Ignore Page<foo> variants
3781807bd26cSJoe Perches			    $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
378222735ce8SJoe Perches#Ignore SI style variants like nS, mV and dB (ie: max_uV, regulator_min_uA_show)
37833445686aSJoe Perches			    $var !~ /^(?:[a-z_]*?)_?[a-z][A-Z](?:_[a-z_]+)?$/) {
37847e781f67SJoe Perches				while ($var =~ m{($Ident)}g) {
37857e781f67SJoe Perches					my $word = $1;
37867e781f67SJoe Perches					next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
3787d8b07710SJoe Perches					if ($check) {
3788d8b07710SJoe Perches						seed_camelcase_includes();
3789d8b07710SJoe Perches						if (!$file && !$camelcase_file_seeded) {
3790d8b07710SJoe Perches							seed_camelcase_file($realfile);
3791d8b07710SJoe Perches							$camelcase_file_seeded = 1;
3792d8b07710SJoe Perches						}
3793d8b07710SJoe Perches					}
37947e781f67SJoe Perches					if (!defined $camelcase{$word}) {
37957e781f67SJoe Perches						$camelcase{$word} = 1;
3796be79794bSJoe Perches						CHK("CAMELCASE",
37977e781f67SJoe Perches						    "Avoid CamelCase: <$word>\n" . $herecurr);
37987e781f67SJoe Perches					}
3799323c1260SJoe Perches				}
3800323c1260SJoe Perches			}
38013445686aSJoe Perches		}
38020a920b5bSAndy Whitcroft
38030a920b5bSAndy Whitcroft#no spaces allowed after \ in define
3804d5e616fcSJoe Perches		if ($line =~ /\#\s*define.*\\\s+$/) {
3805d5e616fcSJoe Perches			if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
3806d5e616fcSJoe Perches				 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
3807d5e616fcSJoe Perches			    $fix) {
3808d5e616fcSJoe Perches				$fixed[$linenr - 1] =~ s/\s+$//;
3809d5e616fcSJoe Perches			}
38100a920b5bSAndy Whitcroft		}
38110a920b5bSAndy Whitcroft
3812653d4876SAndy Whitcroft#warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
3813c45dcabdSAndy Whitcroft		if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
3814e09dec48SAndy Whitcroft			my $file = "$1.h";
3815e09dec48SAndy Whitcroft			my $checkfile = "include/linux/$file";
3816e09dec48SAndy Whitcroft			if (-f "$root/$checkfile" &&
3817e09dec48SAndy Whitcroft			    $realfile ne $checkfile &&
38187840a94cSWolfram Sang			    $1 !~ /$allowed_asm_includes/)
3819c45dcabdSAndy Whitcroft			{
3820e09dec48SAndy Whitcroft				if ($realfile =~ m{^arch/}) {
3821000d1cc1SJoe Perches					CHK("ARCH_INCLUDE_LINUX",
3822000d1cc1SJoe Perches					    "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
3823e09dec48SAndy Whitcroft				} else {
3824000d1cc1SJoe Perches					WARN("INCLUDE_LINUX",
3825000d1cc1SJoe Perches					     "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
3826e09dec48SAndy Whitcroft				}
38270a920b5bSAndy Whitcroft			}
38280a920b5bSAndy Whitcroft		}
38290a920b5bSAndy Whitcroft
3830653d4876SAndy Whitcroft# multi-statement macros should be enclosed in a do while loop, grab the
3831653d4876SAndy Whitcroft# first statement and ensure its the whole macro if its not enclosed
3832cf655043SAndy Whitcroft# in a known good container
3833b8f96a31SAndy Whitcroft		if ($realfile !~ m@/vmlinux.lds.h$@ &&
3834b8f96a31SAndy Whitcroft		    $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
3835d8aaf121SAndy Whitcroft			my $ln = $linenr;
3836d8aaf121SAndy Whitcroft			my $cnt = $realcnt;
3837c45dcabdSAndy Whitcroft			my ($off, $dstat, $dcond, $rest);
3838c45dcabdSAndy Whitcroft			my $ctx = '';
3839c45dcabdSAndy Whitcroft			($dstat, $dcond, $ln, $cnt, $off) =
3840f74bd194SAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0);
3841f74bd194SAndy Whitcroft			$ctx = $dstat;
3842c45dcabdSAndy Whitcroft			#print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
3843a3bb97a7SAndy Whitcroft			#print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
3844c45dcabdSAndy Whitcroft
3845f74bd194SAndy Whitcroft			$dstat =~ s/^.\s*\#\s*define\s+$Ident(?:\([^\)]*\))?\s*//;
3846292f1a9bSAndy Whitcroft			$dstat =~ s/$;//g;
3847c45dcabdSAndy Whitcroft			$dstat =~ s/\\\n.//g;
3848c45dcabdSAndy Whitcroft			$dstat =~ s/^\s*//s;
3849c45dcabdSAndy Whitcroft			$dstat =~ s/\s*$//s;
3850c45dcabdSAndy Whitcroft
3851c45dcabdSAndy Whitcroft			# Flatten any parentheses and braces
3852bf30d6edSAndy Whitcroft			while ($dstat =~ s/\([^\(\)]*\)/1/ ||
3853bf30d6edSAndy Whitcroft			       $dstat =~ s/\{[^\{\}]*\}/1/ ||
3854c81769fdSAndy Whitcroft			       $dstat =~ s/\[[^\[\]]*\]/1/)
3855bf30d6edSAndy Whitcroft			{
3856c45dcabdSAndy Whitcroft			}
3857c45dcabdSAndy Whitcroft
3858e45bab8eSAndy Whitcroft			# Flatten any obvious string concatentation.
3859e45bab8eSAndy Whitcroft			while ($dstat =~ s/("X*")\s*$Ident/$1/ ||
3860e45bab8eSAndy Whitcroft			       $dstat =~ s/$Ident\s*("X*")/$1/)
3861e45bab8eSAndy Whitcroft			{
3862e45bab8eSAndy Whitcroft			}
3863e45bab8eSAndy Whitcroft
3864c45dcabdSAndy Whitcroft			my $exceptions = qr{
3865c45dcabdSAndy Whitcroft				$Declare|
3866c45dcabdSAndy Whitcroft				module_param_named|
3867a0a0a7a9SKees Cook				MODULE_PARM_DESC|
3868c45dcabdSAndy Whitcroft				DECLARE_PER_CPU|
3869c45dcabdSAndy Whitcroft				DEFINE_PER_CPU|
3870383099fdSAndy Whitcroft				__typeof__\(|
387122fd2d3eSStefani Seibold				union|
387222fd2d3eSStefani Seibold				struct|
3873ea71a0a0SAndy Whitcroft				\.$Ident\s*=\s*|
3874ea71a0a0SAndy Whitcroft				^\"|\"$
3875c45dcabdSAndy Whitcroft			}x;
38765eaa20b9SAndy Whitcroft			#print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
3877f74bd194SAndy Whitcroft			if ($dstat ne '' &&
3878f74bd194SAndy Whitcroft			    $dstat !~ /^(?:$Ident|-?$Constant),$/ &&			# 10, // foo(),
3879f74bd194SAndy Whitcroft			    $dstat !~ /^(?:$Ident|-?$Constant);$/ &&			# foo();
38803cc4b1c3SJoe Perches			    $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ &&		# 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
3881356fd398SJoe Perches			    $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ &&			# character constants
3882f74bd194SAndy Whitcroft			    $dstat !~ /$exceptions/ &&
3883f74bd194SAndy Whitcroft			    $dstat !~ /^\.$Ident\s*=/ &&				# .foo =
3884e942e2c3SJoe Perches			    $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ &&		# stringification #foo
388572f115f9SAndy Whitcroft			    $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ &&	# do {...} while (...); // do {...} while (...)
3886f74bd194SAndy Whitcroft			    $dstat !~ /^for\s*$Constant$/ &&				# for (...)
3887f74bd194SAndy Whitcroft			    $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ &&	# for (...) bar()
3888f74bd194SAndy Whitcroft			    $dstat !~ /^do\s*{/ &&					# do {...
3889f95a7e6aSJoe Perches			    $dstat !~ /^\({/ &&						# ({...
3890f95a7e6aSJoe Perches			    $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
3891c45dcabdSAndy Whitcroft			{
3892f74bd194SAndy Whitcroft				$ctx =~ s/\n*$//;
3893f74bd194SAndy Whitcroft				my $herectx = $here . "\n";
3894f74bd194SAndy Whitcroft				my $cnt = statement_rawlines($ctx);
3895f74bd194SAndy Whitcroft
3896f74bd194SAndy Whitcroft				for (my $n = 0; $n < $cnt; $n++) {
3897f74bd194SAndy Whitcroft					$herectx .= raw_line($linenr, $n) . "\n";
3898c45dcabdSAndy Whitcroft				}
3899c45dcabdSAndy Whitcroft
3900f74bd194SAndy Whitcroft				if ($dstat =~ /;/) {
3901f74bd194SAndy Whitcroft					ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
3902f74bd194SAndy Whitcroft					      "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
3903f74bd194SAndy Whitcroft				} else {
3904000d1cc1SJoe Perches					ERROR("COMPLEX_MACRO",
3905f74bd194SAndy Whitcroft					      "Macros with complex values should be enclosed in parenthesis\n" . "$herectx");
3906d8aaf121SAndy Whitcroft				}
39070a920b5bSAndy Whitcroft			}
39085023d347SJoe Perches
3909481eb486SJoe Perches# check for line continuations outside of #defines, preprocessor #, and asm
39105023d347SJoe Perches
39115023d347SJoe Perches		} else {
39125023d347SJoe Perches			if ($prevline !~ /^..*\\$/ &&
3913481eb486SJoe Perches			    $line !~ /^\+\s*\#.*\\$/ &&		# preprocessor
3914481eb486SJoe Perches			    $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ &&	# asm
39155023d347SJoe Perches			    $line =~ /^\+.*\\$/) {
39165023d347SJoe Perches				WARN("LINE_CONTINUATIONS",
39175023d347SJoe Perches				     "Avoid unnecessary line continuations\n" . $herecurr);
39185023d347SJoe Perches			}
3919653d4876SAndy Whitcroft		}
39200a920b5bSAndy Whitcroft
3921b13edf7fSJoe Perches# do {} while (0) macro tests:
3922b13edf7fSJoe Perches# single-statement macros do not need to be enclosed in do while (0) loop,
3923b13edf7fSJoe Perches# macro should not end with a semicolon
3924b13edf7fSJoe Perches		if ($^V && $^V ge 5.10.0 &&
3925b13edf7fSJoe Perches		    $realfile !~ m@/vmlinux.lds.h$@ &&
3926b13edf7fSJoe Perches		    $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
3927b13edf7fSJoe Perches			my $ln = $linenr;
3928b13edf7fSJoe Perches			my $cnt = $realcnt;
3929b13edf7fSJoe Perches			my ($off, $dstat, $dcond, $rest);
3930b13edf7fSJoe Perches			my $ctx = '';
3931b13edf7fSJoe Perches			($dstat, $dcond, $ln, $cnt, $off) =
3932b13edf7fSJoe Perches				ctx_statement_block($linenr, $realcnt, 0);
3933b13edf7fSJoe Perches			$ctx = $dstat;
3934b13edf7fSJoe Perches
3935b13edf7fSJoe Perches			$dstat =~ s/\\\n.//g;
3936b13edf7fSJoe Perches
3937b13edf7fSJoe Perches			if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
3938b13edf7fSJoe Perches				my $stmts = $2;
3939b13edf7fSJoe Perches				my $semis = $3;
3940b13edf7fSJoe Perches
3941b13edf7fSJoe Perches				$ctx =~ s/\n*$//;
3942b13edf7fSJoe Perches				my $cnt = statement_rawlines($ctx);
3943b13edf7fSJoe Perches				my $herectx = $here . "\n";
3944b13edf7fSJoe Perches
3945b13edf7fSJoe Perches				for (my $n = 0; $n < $cnt; $n++) {
3946b13edf7fSJoe Perches					$herectx .= raw_line($linenr, $n) . "\n";
3947b13edf7fSJoe Perches				}
3948b13edf7fSJoe Perches
3949ac8e97f8SJoe Perches				if (($stmts =~ tr/;/;/) == 1 &&
3950ac8e97f8SJoe Perches				    $stmts !~ /^\s*(if|while|for|switch)\b/) {
3951b13edf7fSJoe Perches					WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
3952b13edf7fSJoe Perches					     "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
3953b13edf7fSJoe Perches				}
3954b13edf7fSJoe Perches				if (defined $semis && $semis ne "") {
3955b13edf7fSJoe Perches					WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
3956b13edf7fSJoe Perches					     "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
3957b13edf7fSJoe Perches				}
3958f5ef95b1SJoe Perches			} elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) {
3959f5ef95b1SJoe Perches				$ctx =~ s/\n*$//;
3960f5ef95b1SJoe Perches				my $cnt = statement_rawlines($ctx);
3961f5ef95b1SJoe Perches				my $herectx = $here . "\n";
3962f5ef95b1SJoe Perches
3963f5ef95b1SJoe Perches				for (my $n = 0; $n < $cnt; $n++) {
3964f5ef95b1SJoe Perches					$herectx .= raw_line($linenr, $n) . "\n";
3965f5ef95b1SJoe Perches				}
3966f5ef95b1SJoe Perches
3967f5ef95b1SJoe Perches				WARN("TRAILING_SEMICOLON",
3968f5ef95b1SJoe Perches				     "macros should not use a trailing semicolon\n" . "$herectx");
3969b13edf7fSJoe Perches			}
3970b13edf7fSJoe Perches		}
3971b13edf7fSJoe Perches
3972080ba929SMike Frysinger# make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
3973080ba929SMike Frysinger# all assignments may have only one of the following with an assignment:
3974080ba929SMike Frysinger#	.
3975080ba929SMike Frysinger#	ALIGN(...)
3976080ba929SMike Frysinger#	VMLINUX_SYMBOL(...)
3977080ba929SMike Frysinger		if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
3978000d1cc1SJoe Perches			WARN("MISSING_VMLINUX_SYMBOL",
3979000d1cc1SJoe Perches			     "vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
3980080ba929SMike Frysinger		}
3981080ba929SMike Frysinger
3982f0a594c1SAndy Whitcroft# check for redundant bracing round if etc
398313214adfSAndy Whitcroft		if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
398413214adfSAndy Whitcroft			my ($level, $endln, @chunks) =
3985cf655043SAndy Whitcroft				ctx_statement_full($linenr, $realcnt, 1);
398613214adfSAndy Whitcroft			#print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
3987cf655043SAndy Whitcroft			#print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
3988cf655043SAndy Whitcroft			if ($#chunks > 0 && $level == 0) {
3989aad4f614SJoe Perches				my @allowed = ();
3990aad4f614SJoe Perches				my $allow = 0;
399113214adfSAndy Whitcroft				my $seen = 0;
3992773647a0SAndy Whitcroft				my $herectx = $here . "\n";
3993cf655043SAndy Whitcroft				my $ln = $linenr - 1;
399413214adfSAndy Whitcroft				for my $chunk (@chunks) {
399513214adfSAndy Whitcroft					my ($cond, $block) = @{$chunk};
399613214adfSAndy Whitcroft
3997773647a0SAndy Whitcroft					# If the condition carries leading newlines, then count those as offsets.
3998773647a0SAndy Whitcroft					my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
3999773647a0SAndy Whitcroft					my $offset = statement_rawlines($whitespace) - 1;
4000773647a0SAndy Whitcroft
4001aad4f614SJoe Perches					$allowed[$allow] = 0;
4002773647a0SAndy Whitcroft					#print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
4003773647a0SAndy Whitcroft
4004773647a0SAndy Whitcroft					# We have looked at and allowed this specific line.
4005773647a0SAndy Whitcroft					$suppress_ifbraces{$ln + $offset} = 1;
4006773647a0SAndy Whitcroft
4007773647a0SAndy Whitcroft					$herectx .= "$rawlines[$ln + $offset]\n[...]\n";
4008cf655043SAndy Whitcroft					$ln += statement_rawlines($block) - 1;
4009cf655043SAndy Whitcroft
4010773647a0SAndy Whitcroft					substr($block, 0, length($cond), '');
401113214adfSAndy Whitcroft
401213214adfSAndy Whitcroft					$seen++ if ($block =~ /^\s*{/);
401313214adfSAndy Whitcroft
4014aad4f614SJoe Perches					#print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
4015cf655043SAndy Whitcroft					if (statement_lines($cond) > 1) {
4016cf655043SAndy Whitcroft						#print "APW: ALLOWED: cond<$cond>\n";
4017aad4f614SJoe Perches						$allowed[$allow] = 1;
401813214adfSAndy Whitcroft					}
401913214adfSAndy Whitcroft					if ($block =~/\b(?:if|for|while)\b/) {
4020cf655043SAndy Whitcroft						#print "APW: ALLOWED: block<$block>\n";
4021aad4f614SJoe Perches						$allowed[$allow] = 1;
402213214adfSAndy Whitcroft					}
4023cf655043SAndy Whitcroft					if (statement_block_size($block) > 1) {
4024cf655043SAndy Whitcroft						#print "APW: ALLOWED: lines block<$block>\n";
4025aad4f614SJoe Perches						$allowed[$allow] = 1;
402613214adfSAndy Whitcroft					}
4027aad4f614SJoe Perches					$allow++;
402813214adfSAndy Whitcroft				}
4029aad4f614SJoe Perches				if ($seen) {
4030aad4f614SJoe Perches					my $sum_allowed = 0;
4031aad4f614SJoe Perches					foreach (@allowed) {
4032aad4f614SJoe Perches						$sum_allowed += $_;
4033aad4f614SJoe Perches					}
4034aad4f614SJoe Perches					if ($sum_allowed == 0) {
4035000d1cc1SJoe Perches						WARN("BRACES",
4036000d1cc1SJoe Perches						     "braces {} are not necessary for any arm of this statement\n" . $herectx);
4037aad4f614SJoe Perches					} elsif ($sum_allowed != $allow &&
4038aad4f614SJoe Perches						 $seen != $allow) {
4039aad4f614SJoe Perches						CHK("BRACES",
4040aad4f614SJoe Perches						    "braces {} should be used on all arms of this statement\n" . $herectx);
4041aad4f614SJoe Perches					}
404213214adfSAndy Whitcroft				}
404313214adfSAndy Whitcroft			}
404413214adfSAndy Whitcroft		}
4045773647a0SAndy Whitcroft		if (!defined $suppress_ifbraces{$linenr - 1} &&
404613214adfSAndy Whitcroft					$line =~ /\b(if|while|for|else)\b/) {
4047cf655043SAndy Whitcroft			my $allowed = 0;
4048f0a594c1SAndy Whitcroft
4049cf655043SAndy Whitcroft			# Check the pre-context.
4050cf655043SAndy Whitcroft			if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
4051cf655043SAndy Whitcroft				#print "APW: ALLOWED: pre<$1>\n";
4052cf655043SAndy Whitcroft				$allowed = 1;
4053f0a594c1SAndy Whitcroft			}
4054773647a0SAndy Whitcroft
4055773647a0SAndy Whitcroft			my ($level, $endln, @chunks) =
4056773647a0SAndy Whitcroft				ctx_statement_full($linenr, $realcnt, $-[0]);
4057773647a0SAndy Whitcroft
4058cf655043SAndy Whitcroft			# Check the condition.
4059cf655043SAndy Whitcroft			my ($cond, $block) = @{$chunks[0]};
4060773647a0SAndy Whitcroft			#print "CHECKING<$linenr> cond<$cond> block<$block>\n";
4061cf655043SAndy Whitcroft			if (defined $cond) {
4062773647a0SAndy Whitcroft				substr($block, 0, length($cond), '');
4063cf655043SAndy Whitcroft			}
4064cf655043SAndy Whitcroft			if (statement_lines($cond) > 1) {
4065cf655043SAndy Whitcroft				#print "APW: ALLOWED: cond<$cond>\n";
4066cf655043SAndy Whitcroft				$allowed = 1;
4067cf655043SAndy Whitcroft			}
4068cf655043SAndy Whitcroft			if ($block =~/\b(?:if|for|while)\b/) {
4069cf655043SAndy Whitcroft				#print "APW: ALLOWED: block<$block>\n";
4070cf655043SAndy Whitcroft				$allowed = 1;
4071cf655043SAndy Whitcroft			}
4072cf655043SAndy Whitcroft			if (statement_block_size($block) > 1) {
4073cf655043SAndy Whitcroft				#print "APW: ALLOWED: lines block<$block>\n";
4074cf655043SAndy Whitcroft				$allowed = 1;
4075cf655043SAndy Whitcroft			}
4076cf655043SAndy Whitcroft			# Check the post-context.
4077cf655043SAndy Whitcroft			if (defined $chunks[1]) {
4078cf655043SAndy Whitcroft				my ($cond, $block) = @{$chunks[1]};
4079cf655043SAndy Whitcroft				if (defined $cond) {
4080773647a0SAndy Whitcroft					substr($block, 0, length($cond), '');
4081cf655043SAndy Whitcroft				}
4082cf655043SAndy Whitcroft				if ($block =~ /^\s*\{/) {
4083cf655043SAndy Whitcroft					#print "APW: ALLOWED: chunk-1 block<$block>\n";
4084cf655043SAndy Whitcroft					$allowed = 1;
4085cf655043SAndy Whitcroft				}
4086cf655043SAndy Whitcroft			}
4087cf655043SAndy Whitcroft			if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
408869932487SJustin P. Mattock				my $herectx = $here . "\n";
4089f055663cSAndy Whitcroft				my $cnt = statement_rawlines($block);
4090cf655043SAndy Whitcroft
4091f055663cSAndy Whitcroft				for (my $n = 0; $n < $cnt; $n++) {
409269932487SJustin P. Mattock					$herectx .= raw_line($linenr, $n) . "\n";
4093cf655043SAndy Whitcroft				}
4094cf655043SAndy Whitcroft
4095000d1cc1SJoe Perches				WARN("BRACES",
4096000d1cc1SJoe Perches				     "braces {} are not necessary for single statement blocks\n" . $herectx);
4097f0a594c1SAndy Whitcroft			}
4098f0a594c1SAndy Whitcroft		}
4099f0a594c1SAndy Whitcroft
41000979ae66SJoe Perches# check for unnecessary blank lines around braces
410177b9a53aSJoe Perches		if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
41020979ae66SJoe Perches			CHK("BRACES",
41030979ae66SJoe Perches			    "Blank lines aren't necessary before a close brace '}'\n" . $hereprev);
41040979ae66SJoe Perches		}
410577b9a53aSJoe Perches		if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
41060979ae66SJoe Perches			CHK("BRACES",
41070979ae66SJoe Perches			    "Blank lines aren't necessary after an open brace '{'\n" . $hereprev);
41080979ae66SJoe Perches		}
41090979ae66SJoe Perches
41104a0df2efSAndy Whitcroft# no volatiles please
41116c72ffaaSAndy Whitcroft		my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
41126c72ffaaSAndy Whitcroft		if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
4113000d1cc1SJoe Perches			WARN("VOLATILE",
4114000d1cc1SJoe Perches			     "Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
41154a0df2efSAndy Whitcroft		}
41164a0df2efSAndy Whitcroft
411700df344fSAndy Whitcroft# warn about #if 0
4118c45dcabdSAndy Whitcroft		if ($line =~ /^.\s*\#\s*if\s+0\b/) {
4119000d1cc1SJoe Perches			CHK("REDUNDANT_CODE",
4120000d1cc1SJoe Perches			    "if this code is redundant consider removing it\n" .
4121de7d4f0eSAndy Whitcroft				$herecurr);
41224a0df2efSAndy Whitcroft		}
41234a0df2efSAndy Whitcroft
412403df4b51SAndy Whitcroft# check for needless "if (<foo>) fn(<foo>)" uses
412503df4b51SAndy Whitcroft		if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
412603df4b51SAndy Whitcroft			my $expr = '\s*\(\s*' . quotemeta($1) . '\s*\)\s*;';
412703df4b51SAndy Whitcroft			if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?)$expr/) {
412803df4b51SAndy Whitcroft				WARN('NEEDLESS_IF',
412903df4b51SAndy Whitcroft				     "$1(NULL) is safe this check is probably not required\n" . $hereprev);
41304c432a8fSGreg Kroah-Hartman			}
41314c432a8fSGreg Kroah-Hartman		}
4132f0a594c1SAndy Whitcroft
4133ebfdc409SJoe Perches# check for unnecessary "Out of Memory" messages
4134ebfdc409SJoe Perches		if ($line =~ /^\+.*\b$logFunctions\s*\(/ &&
4135ebfdc409SJoe Perches		    $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ &&
4136ebfdc409SJoe Perches		    (defined $1 || defined $3) &&
4137ebfdc409SJoe Perches		    $linenr > 3) {
4138ebfdc409SJoe Perches			my $testval = $2;
4139ebfdc409SJoe Perches			my $testline = $lines[$linenr - 3];
4140ebfdc409SJoe Perches
4141ebfdc409SJoe Perches			my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0);
4142ebfdc409SJoe Perches#			print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n");
4143ebfdc409SJoe Perches
4144ebfdc409SJoe 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)/) {
4145ebfdc409SJoe Perches				WARN("OOM_MESSAGE",
4146ebfdc409SJoe Perches				     "Possible unnecessary 'out of memory' message\n" . $hereprev);
4147ebfdc409SJoe Perches			}
4148ebfdc409SJoe Perches		}
4149ebfdc409SJoe Perches
41508716de38SJoe Perches# check for bad placement of section $InitAttribute (e.g.: __initdata)
41518716de38SJoe Perches		if ($line =~ /(\b$InitAttribute\b)/) {
41528716de38SJoe Perches			my $attr = $1;
41538716de38SJoe Perches			if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
41548716de38SJoe Perches				my $ptr = $1;
41558716de38SJoe Perches				my $var = $2;
41568716de38SJoe Perches				if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
41578716de38SJoe Perches				      ERROR("MISPLACED_INIT",
41588716de38SJoe Perches					    "$attr should be placed after $var\n" . $herecurr)) ||
41598716de38SJoe Perches				     ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
41608716de38SJoe Perches				      WARN("MISPLACED_INIT",
41618716de38SJoe Perches					   "$attr should be placed after $var\n" . $herecurr))) &&
41628716de38SJoe Perches				    $fix) {
41638716de38SJoe 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;
41648716de38SJoe Perches				}
41658716de38SJoe Perches			}
41668716de38SJoe Perches		}
41678716de38SJoe Perches
4168e970b884SJoe Perches# check for $InitAttributeData (ie: __initdata) with const
4169e970b884SJoe Perches		if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
4170e970b884SJoe Perches			my $attr = $1;
4171e970b884SJoe Perches			$attr =~ /($InitAttributePrefix)(.*)/;
4172e970b884SJoe Perches			my $attr_prefix = $1;
4173e970b884SJoe Perches			my $attr_type = $2;
4174e970b884SJoe Perches			if (ERROR("INIT_ATTRIBUTE",
4175e970b884SJoe Perches				  "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
4176e970b884SJoe Perches			    $fix) {
4177e970b884SJoe Perches				$fixed[$linenr - 1] =~
4178e970b884SJoe Perches				    s/$InitAttributeData/${attr_prefix}initconst/;
4179e970b884SJoe Perches			}
4180e970b884SJoe Perches		}
4181e970b884SJoe Perches
4182e970b884SJoe Perches# check for $InitAttributeConst (ie: __initconst) without const
4183e970b884SJoe Perches		if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
4184e970b884SJoe Perches			my $attr = $1;
4185e970b884SJoe Perches			if (ERROR("INIT_ATTRIBUTE",
4186e970b884SJoe Perches				  "Use of $attr requires a separate use of const\n" . $herecurr) &&
4187e970b884SJoe Perches			    $fix) {
4188e970b884SJoe Perches				my $lead = $fixed[$linenr - 1] =~
4189e970b884SJoe Perches				    /(^\+\s*(?:static\s+))/;
4190e970b884SJoe Perches				$lead = rtrim($1);
4191e970b884SJoe Perches				$lead = "$lead " if ($lead !~ /^\+$/);
4192e970b884SJoe Perches				$lead = "${lead}const ";
4193e970b884SJoe Perches				$fixed[$linenr - 1] =~ s/(^\+\s*(?:static\s+))/$lead/;
4194e970b884SJoe Perches			}
4195e970b884SJoe Perches		}
4196e970b884SJoe Perches
4197fbdb8138SJoe Perches# don't use __constant_<foo> functions outside of include/uapi/
4198fbdb8138SJoe Perches		if ($realfile !~ m@^include/uapi/@ &&
4199fbdb8138SJoe Perches		    $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) {
4200fbdb8138SJoe Perches			my $constant_func = $1;
4201fbdb8138SJoe Perches			my $func = $constant_func;
4202fbdb8138SJoe Perches			$func =~ s/^__constant_//;
4203fbdb8138SJoe Perches			if (WARN("CONSTANT_CONVERSION",
4204fbdb8138SJoe Perches				 "$constant_func should be $func\n" . $herecurr) &&
4205fbdb8138SJoe Perches			    $fix) {
4206fbdb8138SJoe Perches				$fixed[$linenr - 1] =~ s/\b$constant_func\b/$func/g;
4207fbdb8138SJoe Perches			}
4208fbdb8138SJoe Perches		}
4209fbdb8138SJoe Perches
42101a15a250SPatrick Pannuto# prefer usleep_range over udelay
421137581c28SBruce Allan		if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
421243c1d77cSJoe Perches			my $delay = $1;
42131a15a250SPatrick Pannuto			# ignore udelay's < 10, however
421443c1d77cSJoe Perches			if (! ($delay < 10) ) {
4215000d1cc1SJoe Perches				CHK("USLEEP_RANGE",
421643c1d77cSJoe Perches				    "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $herecurr);
421743c1d77cSJoe Perches			}
421843c1d77cSJoe Perches			if ($delay > 2000) {
421943c1d77cSJoe Perches				WARN("LONG_UDELAY",
422043c1d77cSJoe Perches				     "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr);
42211a15a250SPatrick Pannuto			}
42221a15a250SPatrick Pannuto		}
42231a15a250SPatrick Pannuto
422409ef8725SPatrick Pannuto# warn about unexpectedly long msleep's
422509ef8725SPatrick Pannuto		if ($line =~ /\bmsleep\s*\((\d+)\);/) {
422609ef8725SPatrick Pannuto			if ($1 < 20) {
4227000d1cc1SJoe Perches				WARN("MSLEEP",
422843c1d77cSJoe Perches				     "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $herecurr);
422909ef8725SPatrick Pannuto			}
423009ef8725SPatrick Pannuto		}
423109ef8725SPatrick Pannuto
423236ec1939SJoe Perches# check for comparisons of jiffies
423336ec1939SJoe Perches		if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
423436ec1939SJoe Perches			WARN("JIFFIES_COMPARISON",
423536ec1939SJoe Perches			     "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
423636ec1939SJoe Perches		}
423736ec1939SJoe Perches
42389d7a34a5SJoe Perches# check for comparisons of get_jiffies_64()
42399d7a34a5SJoe Perches		if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
42409d7a34a5SJoe Perches			WARN("JIFFIES_COMPARISON",
42419d7a34a5SJoe Perches			     "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
42429d7a34a5SJoe Perches		}
42439d7a34a5SJoe Perches
424400df344fSAndy Whitcroft# warn about #ifdefs in C files
4245c45dcabdSAndy Whitcroft#		if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
424600df344fSAndy Whitcroft#			print "#ifdef in C files should be avoided\n";
424700df344fSAndy Whitcroft#			print "$herecurr";
424800df344fSAndy Whitcroft#			$clean = 0;
424900df344fSAndy Whitcroft#		}
425000df344fSAndy Whitcroft
425122f2a2efSAndy Whitcroft# warn about spacing in #ifdefs
4252c45dcabdSAndy Whitcroft		if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
42533705ce5bSJoe Perches			if (ERROR("SPACING",
42543705ce5bSJoe Perches				  "exactly one space required after that #$1\n" . $herecurr) &&
42553705ce5bSJoe Perches			    $fix) {
42563705ce5bSJoe Perches				$fixed[$linenr - 1] =~
42573705ce5bSJoe Perches				    s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
42583705ce5bSJoe Perches			}
42593705ce5bSJoe Perches
426022f2a2efSAndy Whitcroft		}
426122f2a2efSAndy Whitcroft
42624a0df2efSAndy Whitcroft# check for spinlock_t definitions without a comment.
4263171ae1a4SAndy Whitcroft		if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
4264171ae1a4SAndy Whitcroft		    $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
42654a0df2efSAndy Whitcroft			my $which = $1;
42664a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
4267000d1cc1SJoe Perches				CHK("UNCOMMENTED_DEFINITION",
4268000d1cc1SJoe Perches				    "$1 definition without comment\n" . $herecurr);
42694a0df2efSAndy Whitcroft			}
42704a0df2efSAndy Whitcroft		}
42714a0df2efSAndy Whitcroft# check for memory barriers without a comment.
42724a0df2efSAndy Whitcroft		if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
42734a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
4274c1fd7bb9SJoe Perches				WARN("MEMORY_BARRIER",
4275000d1cc1SJoe Perches				     "memory barrier without comment\n" . $herecurr);
42764a0df2efSAndy Whitcroft			}
42774a0df2efSAndy Whitcroft		}
42784a0df2efSAndy Whitcroft# check of hardware specific defines
4279c45dcabdSAndy Whitcroft		if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
4280000d1cc1SJoe Perches			CHK("ARCH_DEFINES",
4281000d1cc1SJoe Perches			    "architecture specific defines should be avoided\n" .  $herecurr);
42820a920b5bSAndy Whitcroft		}
4283653d4876SAndy Whitcroft
4284d4977c78STobias Klauser# Check that the storage class is at the beginning of a declaration
4285d4977c78STobias Klauser		if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
4286000d1cc1SJoe Perches			WARN("STORAGE_CLASS",
4287000d1cc1SJoe Perches			     "storage class should be at the beginning of the declaration\n" . $herecurr)
4288d4977c78STobias Klauser		}
4289d4977c78STobias Klauser
4290de7d4f0eSAndy Whitcroft# check the location of the inline attribute, that it is between
4291de7d4f0eSAndy Whitcroft# storage class and type.
42929c0ca6f9SAndy Whitcroft		if ($line =~ /\b$Type\s+$Inline\b/ ||
42939c0ca6f9SAndy Whitcroft		    $line =~ /\b$Inline\s+$Storage\b/) {
4294000d1cc1SJoe Perches			ERROR("INLINE_LOCATION",
4295000d1cc1SJoe Perches			      "inline keyword should sit between storage class and type\n" . $herecurr);
4296de7d4f0eSAndy Whitcroft		}
4297de7d4f0eSAndy Whitcroft
42988905a67cSAndy Whitcroft# Check for __inline__ and __inline, prefer inline
42992b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
43002b7ab453SJoe Perches		    $line =~ /\b(__inline__|__inline)\b/) {
4301d5e616fcSJoe Perches			if (WARN("INLINE",
4302d5e616fcSJoe Perches				 "plain inline is preferred over $1\n" . $herecurr) &&
4303d5e616fcSJoe Perches			    $fix) {
4304d5e616fcSJoe Perches				$fixed[$linenr - 1] =~ s/\b(__inline__|__inline)\b/inline/;
4305d5e616fcSJoe Perches
4306d5e616fcSJoe Perches			}
43078905a67cSAndy Whitcroft		}
43088905a67cSAndy Whitcroft
43093d130fd0SJoe Perches# Check for __attribute__ packed, prefer __packed
43102b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
43112b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
4312000d1cc1SJoe Perches			WARN("PREFER_PACKED",
4313000d1cc1SJoe Perches			     "__packed is preferred over __attribute__((packed))\n" . $herecurr);
43143d130fd0SJoe Perches		}
43153d130fd0SJoe Perches
431639b7e287SJoe Perches# Check for __attribute__ aligned, prefer __aligned
43172b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
43182b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
4319000d1cc1SJoe Perches			WARN("PREFER_ALIGNED",
4320000d1cc1SJoe Perches			     "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
432139b7e287SJoe Perches		}
432239b7e287SJoe Perches
43235f14d3bdSJoe Perches# Check for __attribute__ format(printf, prefer __printf
43242b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
43252b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
4326d5e616fcSJoe Perches			if (WARN("PREFER_PRINTF",
4327d5e616fcSJoe Perches				 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) &&
4328d5e616fcSJoe Perches			    $fix) {
4329d5e616fcSJoe Perches				$fixed[$linenr - 1] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf\s*,\s*(.*)\)\s*\)\s*\)/"__printf(" . trim($1) . ")"/ex;
4330d5e616fcSJoe Perches
4331d5e616fcSJoe Perches			}
43325f14d3bdSJoe Perches		}
43335f14d3bdSJoe Perches
43346061d949SJoe Perches# Check for __attribute__ format(scanf, prefer __scanf
43352b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
43362b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
4337d5e616fcSJoe Perches			if (WARN("PREFER_SCANF",
4338d5e616fcSJoe Perches				 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) &&
4339d5e616fcSJoe Perches			    $fix) {
4340d5e616fcSJoe Perches				$fixed[$linenr - 1] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\s*,\s*(.*)\)\s*\)\s*\)/"__scanf(" . trim($1) . ")"/ex;
4341d5e616fcSJoe Perches			}
43426061d949SJoe Perches		}
43436061d949SJoe Perches
43448f53a9b8SJoe Perches# check for sizeof(&)
43458f53a9b8SJoe Perches		if ($line =~ /\bsizeof\s*\(\s*\&/) {
4346000d1cc1SJoe Perches			WARN("SIZEOF_ADDRESS",
4347000d1cc1SJoe Perches			     "sizeof(& should be avoided\n" . $herecurr);
43488f53a9b8SJoe Perches		}
43498f53a9b8SJoe Perches
435066c80b60SJoe Perches# check for sizeof without parenthesis
435166c80b60SJoe Perches		if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
4352d5e616fcSJoe Perches			if (WARN("SIZEOF_PARENTHESIS",
4353d5e616fcSJoe Perches				 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
4354d5e616fcSJoe Perches			    $fix) {
4355d5e616fcSJoe Perches				$fixed[$linenr - 1] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
4356d5e616fcSJoe Perches			}
435766c80b60SJoe Perches		}
435866c80b60SJoe Perches
4359428e2fdcSJoe Perches# check for line continuations in quoted strings with odd counts of "
4360428e2fdcSJoe Perches		if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) {
4361000d1cc1SJoe Perches			WARN("LINE_CONTINUATIONS",
4362000d1cc1SJoe Perches			     "Avoid line continuations in quoted strings\n" . $herecurr);
4363428e2fdcSJoe Perches		}
4364428e2fdcSJoe Perches
436588982feaSJoe Perches# check for struct spinlock declarations
436688982feaSJoe Perches		if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
436788982feaSJoe Perches			WARN("USE_SPINLOCK_T",
436888982feaSJoe Perches			     "struct spinlock should be spinlock_t\n" . $herecurr);
436988982feaSJoe Perches		}
437088982feaSJoe Perches
4371a6962d72SJoe Perches# check for seq_printf uses that could be seq_puts
437206668727SJoe Perches		if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
4373a6962d72SJoe Perches			my $fmt = get_quoted_string($line, $rawline);
437406668727SJoe Perches			if ($fmt ne "" && $fmt !~ /[^\\]\%/) {
4375d5e616fcSJoe Perches				if (WARN("PREFER_SEQ_PUTS",
4376d5e616fcSJoe Perches					 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
4377d5e616fcSJoe Perches				    $fix) {
4378d5e616fcSJoe Perches					$fixed[$linenr - 1] =~ s/\bseq_printf\b/seq_puts/;
4379d5e616fcSJoe Perches				}
4380a6962d72SJoe Perches			}
4381a6962d72SJoe Perches		}
4382a6962d72SJoe Perches
4383554e165cSAndy Whitcroft# Check for misused memsets
4384d1fe9c09SJoe Perches		if ($^V && $^V ge 5.10.0 &&
4385d1fe9c09SJoe Perches		    defined $stat &&
4386d7c76ba7SJoe Perches		    $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/s) {
4387554e165cSAndy Whitcroft
4388d7c76ba7SJoe Perches			my $ms_addr = $2;
4389d1fe9c09SJoe Perches			my $ms_val = $7;
4390d1fe9c09SJoe Perches			my $ms_size = $12;
4391d7c76ba7SJoe Perches
4392554e165cSAndy Whitcroft			if ($ms_size =~ /^(0x|)0$/i) {
4393554e165cSAndy Whitcroft				ERROR("MEMSET",
4394d7c76ba7SJoe Perches				      "memset to 0's uses 0 as the 2nd argument, not the 3rd\n" . "$here\n$stat\n");
4395554e165cSAndy Whitcroft			} elsif ($ms_size =~ /^(0x|)1$/i) {
4396554e165cSAndy Whitcroft				WARN("MEMSET",
4397d7c76ba7SJoe Perches				     "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
4398d7c76ba7SJoe Perches			}
4399d7c76ba7SJoe Perches		}
4400d7c76ba7SJoe Perches
440198a9bba5SJoe Perches# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar)
440298a9bba5SJoe Perches		if ($^V && $^V ge 5.10.0 &&
440398a9bba5SJoe Perches		    $line =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/s) {
440498a9bba5SJoe Perches			if (WARN("PREFER_ETHER_ADDR_COPY",
440598a9bba5SJoe Perches				 "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . $herecurr) &&
440698a9bba5SJoe Perches			    $fix) {
440798a9bba5SJoe Perches				$fixed[$linenr - 1] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/;
440898a9bba5SJoe Perches			}
440998a9bba5SJoe Perches		}
441098a9bba5SJoe Perches
4411d7c76ba7SJoe Perches# typecasts on min/max could be min_t/max_t
4412d1fe9c09SJoe Perches		if ($^V && $^V ge 5.10.0 &&
4413d1fe9c09SJoe Perches		    defined $stat &&
4414d7c76ba7SJoe Perches		    $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
4415d1fe9c09SJoe Perches			if (defined $2 || defined $7) {
4416d7c76ba7SJoe Perches				my $call = $1;
4417d7c76ba7SJoe Perches				my $cast1 = deparenthesize($2);
4418d7c76ba7SJoe Perches				my $arg1 = $3;
4419d1fe9c09SJoe Perches				my $cast2 = deparenthesize($7);
4420d1fe9c09SJoe Perches				my $arg2 = $8;
4421d7c76ba7SJoe Perches				my $cast;
4422d7c76ba7SJoe Perches
4423d1fe9c09SJoe Perches				if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
4424d7c76ba7SJoe Perches					$cast = "$cast1 or $cast2";
4425d7c76ba7SJoe Perches				} elsif ($cast1 ne "") {
4426d7c76ba7SJoe Perches					$cast = $cast1;
4427d7c76ba7SJoe Perches				} else {
4428d7c76ba7SJoe Perches					$cast = $cast2;
4429d7c76ba7SJoe Perches				}
4430d7c76ba7SJoe Perches				WARN("MINMAX",
4431d7c76ba7SJoe Perches				     "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
4432554e165cSAndy Whitcroft			}
4433554e165cSAndy Whitcroft		}
4434554e165cSAndy Whitcroft
44354a273195SJoe Perches# check usleep_range arguments
44364a273195SJoe Perches		if ($^V && $^V ge 5.10.0 &&
44374a273195SJoe Perches		    defined $stat &&
44384a273195SJoe Perches		    $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
44394a273195SJoe Perches			my $min = $1;
44404a273195SJoe Perches			my $max = $7;
44414a273195SJoe Perches			if ($min eq $max) {
44424a273195SJoe Perches				WARN("USLEEP_RANGE",
44434a273195SJoe Perches				     "usleep_range should not use min == max args; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
44444a273195SJoe Perches			} elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
44454a273195SJoe Perches				 $min > $max) {
44464a273195SJoe Perches				WARN("USLEEP_RANGE",
44474a273195SJoe Perches				     "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
44484a273195SJoe Perches			}
44494a273195SJoe Perches		}
44504a273195SJoe Perches
4451823b794cSJoe Perches# check for naked sscanf
4452823b794cSJoe Perches		if ($^V && $^V ge 5.10.0 &&
4453823b794cSJoe Perches		    defined $stat &&
44546c8bd707SJoe Perches		    $line =~ /\bsscanf\b/ &&
4455823b794cSJoe Perches		    ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
4456823b794cSJoe Perches		     $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ &&
4457823b794cSJoe Perches		     $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) {
4458823b794cSJoe Perches			my $lc = $stat =~ tr@\n@@;
4459823b794cSJoe Perches			$lc = $lc + $linenr;
4460823b794cSJoe Perches			my $stat_real = raw_line($linenr, 0);
4461823b794cSJoe Perches		        for (my $count = $linenr + 1; $count <= $lc; $count++) {
4462823b794cSJoe Perches				$stat_real = $stat_real . "\n" . raw_line($count, 0);
4463823b794cSJoe Perches			}
4464823b794cSJoe Perches			WARN("NAKED_SSCANF",
4465823b794cSJoe Perches			     "unchecked sscanf return value\n" . "$here\n$stat_real\n");
4466823b794cSJoe Perches		}
4467823b794cSJoe Perches
4468afc819abSJoe Perches# check for simple sscanf that should be kstrto<foo>
4469afc819abSJoe Perches		if ($^V && $^V ge 5.10.0 &&
4470afc819abSJoe Perches		    defined $stat &&
4471afc819abSJoe Perches		    $line =~ /\bsscanf\b/) {
4472afc819abSJoe Perches			my $lc = $stat =~ tr@\n@@;
4473afc819abSJoe Perches			$lc = $lc + $linenr;
4474afc819abSJoe Perches			my $stat_real = raw_line($linenr, 0);
4475afc819abSJoe Perches		        for (my $count = $linenr + 1; $count <= $lc; $count++) {
4476afc819abSJoe Perches				$stat_real = $stat_real . "\n" . raw_line($count, 0);
4477afc819abSJoe Perches			}
4478afc819abSJoe Perches			if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) {
4479afc819abSJoe Perches				my $format = $6;
4480afc819abSJoe Perches				my $count = $format =~ tr@%@%@;
4481afc819abSJoe Perches				if ($count == 1 &&
4482afc819abSJoe Perches				    $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) {
4483afc819abSJoe Perches					WARN("SSCANF_TO_KSTRTO",
4484afc819abSJoe Perches					     "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n");
4485afc819abSJoe Perches				}
4486afc819abSJoe Perches			}
4487afc819abSJoe Perches		}
4488afc819abSJoe Perches
448970dc8a48SJoe Perches# check for new externs in .h files.
449070dc8a48SJoe Perches		if ($realfile =~ /\.h$/ &&
449170dc8a48SJoe Perches		    $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
4492d1d85780SJoe Perches			if (CHK("AVOID_EXTERNS",
449370dc8a48SJoe Perches				"extern prototypes should be avoided in .h files\n" . $herecurr) &&
449470dc8a48SJoe Perches			    $fix) {
449570dc8a48SJoe Perches				$fixed[$linenr - 1] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
449670dc8a48SJoe Perches			}
449770dc8a48SJoe Perches		}
449870dc8a48SJoe Perches
4499de7d4f0eSAndy Whitcroft# check for new externs in .c files.
4500171ae1a4SAndy Whitcroft		if ($realfile =~ /\.c$/ && defined $stat &&
4501c45dcabdSAndy Whitcroft		    $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
4502171ae1a4SAndy Whitcroft		{
4503c45dcabdSAndy Whitcroft			my $function_name = $1;
4504c45dcabdSAndy Whitcroft			my $paren_space = $2;
4505171ae1a4SAndy Whitcroft
4506171ae1a4SAndy Whitcroft			my $s = $stat;
4507171ae1a4SAndy Whitcroft			if (defined $cond) {
4508171ae1a4SAndy Whitcroft				substr($s, 0, length($cond), '');
4509171ae1a4SAndy Whitcroft			}
4510c45dcabdSAndy Whitcroft			if ($s =~ /^\s*;/ &&
4511c45dcabdSAndy Whitcroft			    $function_name ne 'uninitialized_var')
4512c45dcabdSAndy Whitcroft			{
4513000d1cc1SJoe Perches				WARN("AVOID_EXTERNS",
4514000d1cc1SJoe Perches				     "externs should be avoided in .c files\n" .  $herecurr);
4515de7d4f0eSAndy Whitcroft			}
4516de7d4f0eSAndy Whitcroft
4517171ae1a4SAndy Whitcroft			if ($paren_space =~ /\n/) {
4518000d1cc1SJoe Perches				WARN("FUNCTION_ARGUMENTS",
4519000d1cc1SJoe Perches				     "arguments for function declarations should follow identifier\n" . $herecurr);
4520171ae1a4SAndy Whitcroft			}
45219c9ba34eSAndy Whitcroft
45229c9ba34eSAndy Whitcroft		} elsif ($realfile =~ /\.c$/ && defined $stat &&
45239c9ba34eSAndy Whitcroft		    $stat =~ /^.\s*extern\s+/)
45249c9ba34eSAndy Whitcroft		{
4525000d1cc1SJoe Perches			WARN("AVOID_EXTERNS",
4526000d1cc1SJoe Perches			     "externs should be avoided in .c files\n" .  $herecurr);
4527171ae1a4SAndy Whitcroft		}
4528171ae1a4SAndy Whitcroft
4529de7d4f0eSAndy Whitcroft# checks for new __setup's
4530de7d4f0eSAndy Whitcroft		if ($rawline =~ /\b__setup\("([^"]*)"/) {
4531de7d4f0eSAndy Whitcroft			my $name = $1;
4532de7d4f0eSAndy Whitcroft
4533de7d4f0eSAndy Whitcroft			if (!grep(/$name/, @setup_docs)) {
4534000d1cc1SJoe Perches				CHK("UNDOCUMENTED_SETUP",
4535000d1cc1SJoe Perches				    "__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
4536de7d4f0eSAndy Whitcroft			}
4537653d4876SAndy Whitcroft		}
45389c0ca6f9SAndy Whitcroft
45399c0ca6f9SAndy Whitcroft# check for pointless casting of kmalloc return
4540caf2a54fSJoe Perches		if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) {
4541000d1cc1SJoe Perches			WARN("UNNECESSARY_CASTS",
4542000d1cc1SJoe Perches			     "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
45439c0ca6f9SAndy Whitcroft		}
454413214adfSAndy Whitcroft
4545a640d25cSJoe Perches# alloc style
4546a640d25cSJoe Perches# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
4547a640d25cSJoe Perches		if ($^V && $^V ge 5.10.0 &&
4548a640d25cSJoe Perches		    $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*([kv][mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
4549a640d25cSJoe Perches			CHK("ALLOC_SIZEOF_STRUCT",
4550a640d25cSJoe Perches			    "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
4551a640d25cSJoe Perches		}
4552a640d25cSJoe Perches
455360a55369SJoe Perches# check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc
455460a55369SJoe Perches		if ($^V && $^V ge 5.10.0 &&
4555e367455aSJoe Perches		    $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*(k[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)\s*,/) {
455660a55369SJoe Perches			my $oldfunc = $3;
455760a55369SJoe Perches			my $a1 = $4;
455860a55369SJoe Perches			my $a2 = $10;
455960a55369SJoe Perches			my $newfunc = "kmalloc_array";
456060a55369SJoe Perches			$newfunc = "kcalloc" if ($oldfunc eq "kzalloc");
456160a55369SJoe Perches			my $r1 = $a1;
456260a55369SJoe Perches			my $r2 = $a2;
456360a55369SJoe Perches			if ($a1 =~ /^sizeof\s*\S/) {
456460a55369SJoe Perches				$r1 = $a2;
456560a55369SJoe Perches				$r2 = $a1;
456660a55369SJoe Perches			}
4567e367455aSJoe Perches			if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ &&
4568e367455aSJoe Perches			    !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) {
4569e367455aSJoe Perches				if (WARN("ALLOC_WITH_MULTIPLY",
4570e367455aSJoe Perches					 "Prefer $newfunc over $oldfunc with multiply\n" . $herecurr) &&
4571e367455aSJoe Perches				    $fix) {
457260a55369SJoe 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;
457360a55369SJoe Perches
457460a55369SJoe Perches				}
457560a55369SJoe Perches			}
457660a55369SJoe Perches		}
457760a55369SJoe Perches
4578972fdea2SJoe Perches# check for krealloc arg reuse
4579972fdea2SJoe Perches		if ($^V && $^V ge 5.10.0 &&
4580972fdea2SJoe Perches		    $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*\1\s*,/) {
4581972fdea2SJoe Perches			WARN("KREALLOC_ARG_REUSE",
4582972fdea2SJoe Perches			     "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
4583972fdea2SJoe Perches		}
4584972fdea2SJoe Perches
45855ce59ae0SJoe Perches# check for alloc argument mismatch
45865ce59ae0SJoe Perches		if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
45875ce59ae0SJoe Perches			WARN("ALLOC_ARRAY_ARGS",
45885ce59ae0SJoe Perches			     "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
45895ce59ae0SJoe Perches		}
45905ce59ae0SJoe Perches
4591caf2a54fSJoe Perches# check for multiple semicolons
4592caf2a54fSJoe Perches		if ($line =~ /;\s*;\s*$/) {
4593d5e616fcSJoe Perches			if (WARN("ONE_SEMICOLON",
4594d5e616fcSJoe Perches				 "Statements terminations use 1 semicolon\n" . $herecurr) &&
4595d5e616fcSJoe Perches			    $fix) {
4596d5e616fcSJoe Perches				$fixed[$linenr - 1] =~ s/(\s*;\s*){2,}$/;/g;
4597d5e616fcSJoe Perches			}
4598d1e2ad07SJoe Perches		}
4599d1e2ad07SJoe Perches
4600c34c09a8SJoe Perches# check for case / default statements not preceeded by break/fallthrough/switch
4601c34c09a8SJoe Perches		if ($line =~ /^.\s*(?:case\s+(?:$Ident|$Constant)\s*|default):/) {
4602c34c09a8SJoe Perches			my $has_break = 0;
4603c34c09a8SJoe Perches			my $has_statement = 0;
4604c34c09a8SJoe Perches			my $count = 0;
4605c34c09a8SJoe Perches			my $prevline = $linenr;
4606c34c09a8SJoe Perches			while ($prevline > 1 && $count < 3 && !$has_break) {
4607c34c09a8SJoe Perches				$prevline--;
4608c34c09a8SJoe Perches				my $rline = $rawlines[$prevline - 1];
4609c34c09a8SJoe Perches				my $fline = $lines[$prevline - 1];
4610c34c09a8SJoe Perches				last if ($fline =~ /^\@\@/);
4611c34c09a8SJoe Perches				next if ($fline =~ /^\-/);
4612c34c09a8SJoe Perches				next if ($fline =~ /^.(?:\s*(?:case\s+(?:$Ident|$Constant)[\s$;]*|default):[\s$;]*)*$/);
4613c34c09a8SJoe Perches				$has_break = 1 if ($rline =~ /fall[\s_-]*(through|thru)/i);
4614c34c09a8SJoe Perches				next if ($fline =~ /^.[\s$;]*$/);
4615c34c09a8SJoe Perches				$has_statement = 1;
4616c34c09a8SJoe Perches				$count++;
4617c34c09a8SJoe Perches				$has_break = 1 if ($fline =~ /\bswitch\b|\b(?:break\s*;[\s$;]*$|return\b|goto\b|continue\b)/);
4618c34c09a8SJoe Perches			}
4619c34c09a8SJoe Perches			if (!$has_break && $has_statement) {
4620c34c09a8SJoe Perches				WARN("MISSING_BREAK",
4621c34c09a8SJoe Perches				     "Possible switch case/default not preceeded by break or fallthrough comment\n" . $herecurr);
4622c34c09a8SJoe Perches			}
4623c34c09a8SJoe Perches		}
4624c34c09a8SJoe Perches
4625d1e2ad07SJoe Perches# check for switch/default statements without a break;
4626d1e2ad07SJoe Perches		if ($^V && $^V ge 5.10.0 &&
4627d1e2ad07SJoe Perches		    defined $stat &&
4628d1e2ad07SJoe Perches		    $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
4629d1e2ad07SJoe Perches			my $ctx = '';
4630d1e2ad07SJoe Perches			my $herectx = $here . "\n";
4631d1e2ad07SJoe Perches			my $cnt = statement_rawlines($stat);
4632d1e2ad07SJoe Perches			for (my $n = 0; $n < $cnt; $n++) {
4633d1e2ad07SJoe Perches				$herectx .= raw_line($linenr, $n) . "\n";
4634d1e2ad07SJoe Perches			}
4635d1e2ad07SJoe Perches			WARN("DEFAULT_NO_BREAK",
4636d1e2ad07SJoe Perches			     "switch default: should use break\n" . $herectx);
4637caf2a54fSJoe Perches		}
4638caf2a54fSJoe Perches
463913214adfSAndy Whitcroft# check for gcc specific __FUNCTION__
4640d5e616fcSJoe Perches		if ($line =~ /\b__FUNCTION__\b/) {
4641d5e616fcSJoe Perches			if (WARN("USE_FUNC",
4642d5e616fcSJoe Perches				 "__func__ should be used instead of gcc specific __FUNCTION__\n"  . $herecurr) &&
4643d5e616fcSJoe Perches			    $fix) {
4644d5e616fcSJoe Perches				$fixed[$linenr - 1] =~ s/\b__FUNCTION__\b/__func__/g;
4645d5e616fcSJoe Perches			}
464613214adfSAndy Whitcroft		}
4647773647a0SAndy Whitcroft
46482c92488aSJoe Perches# check for use of yield()
46492c92488aSJoe Perches		if ($line =~ /\byield\s*\(\s*\)/) {
46502c92488aSJoe Perches			WARN("YIELD",
46512c92488aSJoe Perches			     "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n"  . $herecurr);
46522c92488aSJoe Perches		}
46532c92488aSJoe Perches
4654179f8f40SJoe Perches# check for comparisons against true and false
4655179f8f40SJoe Perches		if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
4656179f8f40SJoe Perches			my $lead = $1;
4657179f8f40SJoe Perches			my $arg = $2;
4658179f8f40SJoe Perches			my $test = $3;
4659179f8f40SJoe Perches			my $otype = $4;
4660179f8f40SJoe Perches			my $trail = $5;
4661179f8f40SJoe Perches			my $op = "!";
4662179f8f40SJoe Perches
4663179f8f40SJoe Perches			($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
4664179f8f40SJoe Perches
4665179f8f40SJoe Perches			my $type = lc($otype);
4666179f8f40SJoe Perches			if ($type =~ /^(?:true|false)$/) {
4667179f8f40SJoe Perches				if (("$test" eq "==" && "$type" eq "true") ||
4668179f8f40SJoe Perches				    ("$test" eq "!=" && "$type" eq "false")) {
4669179f8f40SJoe Perches					$op = "";
4670179f8f40SJoe Perches				}
4671179f8f40SJoe Perches
4672179f8f40SJoe Perches				CHK("BOOL_COMPARISON",
4673179f8f40SJoe Perches				    "Using comparison to $otype is error prone\n" . $herecurr);
4674179f8f40SJoe Perches
4675179f8f40SJoe Perches## maybe suggesting a correct construct would better
4676179f8f40SJoe Perches##				    "Using comparison to $otype is error prone.  Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
4677179f8f40SJoe Perches
4678179f8f40SJoe Perches			}
4679179f8f40SJoe Perches		}
4680179f8f40SJoe Perches
46814882720bSThomas Gleixner# check for semaphores initialized locked
46824882720bSThomas Gleixner		if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
4683000d1cc1SJoe Perches			WARN("CONSIDER_COMPLETION",
4684000d1cc1SJoe Perches			     "consider using a completion\n" . $herecurr);
4685773647a0SAndy Whitcroft		}
46866712d858SJoe Perches
468767d0a075SJoe Perches# recommend kstrto* over simple_strto* and strict_strto*
468867d0a075SJoe Perches		if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
4689000d1cc1SJoe Perches			WARN("CONSIDER_KSTRTO",
469067d0a075SJoe Perches			     "$1 is obsolete, use k$3 instead\n" . $herecurr);
4691773647a0SAndy Whitcroft		}
46926712d858SJoe Perches
4693ae3ccc46SFabian Frederick# check for __initcall(), use device_initcall() explicitly or more appropriate function please
4694f3db6639SMichael Ellerman		if ($line =~ /^.\s*__initcall\s*\(/) {
4695000d1cc1SJoe Perches			WARN("USE_DEVICE_INITCALL",
4696ae3ccc46SFabian Frederick			     "please use device_initcall() or more appropriate function instead of __initcall() (see include/linux/init.h)\n" . $herecurr);
4697f3db6639SMichael Ellerman		}
46986712d858SJoe Perches
469979404849SEmese Revfy# check for various ops structs, ensure they are const.
470079404849SEmese Revfy		my $struct_ops = qr{acpi_dock_ops|
470179404849SEmese Revfy				address_space_operations|
470279404849SEmese Revfy				backlight_ops|
470379404849SEmese Revfy				block_device_operations|
470479404849SEmese Revfy				dentry_operations|
470579404849SEmese Revfy				dev_pm_ops|
470679404849SEmese Revfy				dma_map_ops|
470779404849SEmese Revfy				extent_io_ops|
470879404849SEmese Revfy				file_lock_operations|
470979404849SEmese Revfy				file_operations|
471079404849SEmese Revfy				hv_ops|
471179404849SEmese Revfy				ide_dma_ops|
471279404849SEmese Revfy				intel_dvo_dev_ops|
471379404849SEmese Revfy				item_operations|
471479404849SEmese Revfy				iwl_ops|
471579404849SEmese Revfy				kgdb_arch|
471679404849SEmese Revfy				kgdb_io|
471779404849SEmese Revfy				kset_uevent_ops|
471879404849SEmese Revfy				lock_manager_operations|
471979404849SEmese Revfy				microcode_ops|
472079404849SEmese Revfy				mtrr_ops|
472179404849SEmese Revfy				neigh_ops|
472279404849SEmese Revfy				nlmsvc_binding|
472379404849SEmese Revfy				pci_raw_ops|
472479404849SEmese Revfy				pipe_buf_operations|
472579404849SEmese Revfy				platform_hibernation_ops|
472679404849SEmese Revfy				platform_suspend_ops|
472779404849SEmese Revfy				proto_ops|
472879404849SEmese Revfy				rpc_pipe_ops|
472979404849SEmese Revfy				seq_operations|
473079404849SEmese Revfy				snd_ac97_build_ops|
473179404849SEmese Revfy				soc_pcmcia_socket_ops|
473279404849SEmese Revfy				stacktrace_ops|
473379404849SEmese Revfy				sysfs_ops|
473479404849SEmese Revfy				tty_operations|
473579404849SEmese Revfy				usb_mon_operations|
473679404849SEmese Revfy				wd_ops}x;
47376903ffb2SAndy Whitcroft		if ($line !~ /\bconst\b/ &&
473879404849SEmese Revfy		    $line =~ /\bstruct\s+($struct_ops)\b/) {
4739000d1cc1SJoe Perches			WARN("CONST_STRUCT",
4740000d1cc1SJoe Perches			     "struct $1 should normally be const\n" .
47416903ffb2SAndy Whitcroft				$herecurr);
47422b6db5cbSAndy Whitcroft		}
4743773647a0SAndy Whitcroft
4744773647a0SAndy Whitcroft# use of NR_CPUS is usually wrong
4745773647a0SAndy Whitcroft# ignore definitions of NR_CPUS and usage to define arrays as likely right
4746773647a0SAndy Whitcroft		if ($line =~ /\bNR_CPUS\b/ &&
4747c45dcabdSAndy Whitcroft		    $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
4748c45dcabdSAndy Whitcroft		    $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
4749171ae1a4SAndy Whitcroft		    $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
4750171ae1a4SAndy Whitcroft		    $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
4751171ae1a4SAndy Whitcroft		    $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
4752773647a0SAndy Whitcroft		{
4753000d1cc1SJoe Perches			WARN("NR_CPUS",
4754000d1cc1SJoe Perches			     "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
4755773647a0SAndy Whitcroft		}
47569c9ba34eSAndy Whitcroft
475752ea8506SJoe Perches# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
475852ea8506SJoe Perches		if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
475952ea8506SJoe Perches			ERROR("DEFINE_ARCH_HAS",
476052ea8506SJoe Perches			      "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
476152ea8506SJoe Perches		}
476252ea8506SJoe Perches
47639c9ba34eSAndy Whitcroft# check for %L{u,d,i} in strings
47649c9ba34eSAndy Whitcroft		my $string;
47659c9ba34eSAndy Whitcroft		while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
47669c9ba34eSAndy Whitcroft			$string = substr($rawline, $-[1], $+[1] - $-[1]);
47672a1bc5d5SAndy Whitcroft			$string =~ s/%%/__/g;
47689c9ba34eSAndy Whitcroft			if ($string =~ /(?<!%)%L[udi]/) {
4769000d1cc1SJoe Perches				WARN("PRINTF_L",
4770000d1cc1SJoe Perches				     "\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
47719c9ba34eSAndy Whitcroft				last;
47729c9ba34eSAndy Whitcroft			}
47739c9ba34eSAndy Whitcroft		}
4774691d77b6SAndy Whitcroft
4775691d77b6SAndy Whitcroft# whine mightly about in_atomic
4776691d77b6SAndy Whitcroft		if ($line =~ /\bin_atomic\s*\(/) {
4777691d77b6SAndy Whitcroft			if ($realfile =~ m@^drivers/@) {
4778000d1cc1SJoe Perches				ERROR("IN_ATOMIC",
4779000d1cc1SJoe Perches				      "do not use in_atomic in drivers\n" . $herecurr);
4780f4a87736SAndy Whitcroft			} elsif ($realfile !~ m@^kernel/@) {
4781000d1cc1SJoe Perches				WARN("IN_ATOMIC",
4782000d1cc1SJoe Perches				     "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
4783691d77b6SAndy Whitcroft			}
4784691d77b6SAndy Whitcroft		}
47851704f47bSPeter Zijlstra
47861704f47bSPeter Zijlstra# check for lockdep_set_novalidate_class
47871704f47bSPeter Zijlstra		if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
47881704f47bSPeter Zijlstra		    $line =~ /__lockdep_no_validate__\s*\)/ ) {
47891704f47bSPeter Zijlstra			if ($realfile !~ m@^kernel/lockdep@ &&
47901704f47bSPeter Zijlstra			    $realfile !~ m@^include/linux/lockdep@ &&
47911704f47bSPeter Zijlstra			    $realfile !~ m@^drivers/base/core@) {
4792000d1cc1SJoe Perches				ERROR("LOCKDEP",
4793000d1cc1SJoe Perches				      "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
47941704f47bSPeter Zijlstra			}
47951704f47bSPeter Zijlstra		}
479688f8831cSDave Jones
479788f8831cSDave Jones		if ($line =~ /debugfs_create_file.*S_IWUGO/ ||
479888f8831cSDave Jones		    $line =~ /DEVICE_ATTR.*S_IWUGO/ ) {
4799000d1cc1SJoe Perches			WARN("EXPORTED_WORLD_WRITABLE",
4800000d1cc1SJoe Perches			     "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
480188f8831cSDave Jones		}
48022435880fSJoe Perches
4803515a235eSJoe Perches# Mode permission misuses where it seems decimal should be octal
4804515a235eSJoe Perches# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop
4805515a235eSJoe Perches		if ($^V && $^V ge 5.10.0 &&
4806515a235eSJoe Perches		    $line =~ /$mode_perms_search/) {
48072435880fSJoe Perches			foreach my $entry (@mode_permission_funcs) {
48082435880fSJoe Perches				my $func = $entry->[0];
48092435880fSJoe Perches				my $arg_pos = $entry->[1];
48102435880fSJoe Perches
48112435880fSJoe Perches				my $skip_args = "";
48122435880fSJoe Perches				if ($arg_pos > 1) {
48132435880fSJoe Perches					$arg_pos--;
48142435880fSJoe Perches					$skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}";
48152435880fSJoe Perches				}
48162435880fSJoe Perches				my $test = "\\b$func\\s*\\(${skip_args}([\\d]+)\\s*[,\\)]";
4817515a235eSJoe Perches				if ($line =~ /$test/) {
48182435880fSJoe Perches					my $val = $1;
48192435880fSJoe Perches					$val = $6 if ($skip_args ne "");
48202435880fSJoe Perches
48211727cc70SJoe Perches					if ($val !~ /^0$/ &&
48221727cc70SJoe Perches					    (($val =~ /^$Int$/ && $val !~ /^$Octal$/) ||
48231727cc70SJoe Perches					     length($val) ne 4)) {
48242435880fSJoe Perches						ERROR("NON_OCTAL_PERMISSIONS",
48251727cc70SJoe Perches						      "Use 4 digit octal (0777) not decimal permissions\n" . $herecurr);
48262435880fSJoe Perches					}
48272435880fSJoe Perches				}
48282435880fSJoe Perches			}
482913214adfSAndy Whitcroft		}
4830515a235eSJoe Perches	}
483113214adfSAndy Whitcroft
483213214adfSAndy Whitcroft	# If we have no input at all, then there is nothing to report on
483313214adfSAndy Whitcroft	# so just keep quiet.
483413214adfSAndy Whitcroft	if ($#rawlines == -1) {
483513214adfSAndy Whitcroft		exit(0);
48360a920b5bSAndy Whitcroft	}
48370a920b5bSAndy Whitcroft
48388905a67cSAndy Whitcroft	# In mailback mode only produce a report in the negative, for
48398905a67cSAndy Whitcroft	# things that appear to be patches.
48408905a67cSAndy Whitcroft	if ($mailback && ($clean == 1 || !$is_patch)) {
48418905a67cSAndy Whitcroft		exit(0);
48428905a67cSAndy Whitcroft	}
48438905a67cSAndy Whitcroft
48448905a67cSAndy Whitcroft	# This is not a patch, and we are are in 'no-patch' mode so
48458905a67cSAndy Whitcroft	# just keep quiet.
48468905a67cSAndy Whitcroft	if (!$chk_patch && !$is_patch) {
48478905a67cSAndy Whitcroft		exit(0);
48488905a67cSAndy Whitcroft	}
48498905a67cSAndy Whitcroft
48508905a67cSAndy Whitcroft	if (!$is_patch) {
4851000d1cc1SJoe Perches		ERROR("NOT_UNIFIED_DIFF",
4852000d1cc1SJoe Perches		      "Does not appear to be a unified-diff format patch\n");
48530a920b5bSAndy Whitcroft	}
48540a920b5bSAndy Whitcroft	if ($is_patch && $chk_signoff && $signoff == 0) {
4855000d1cc1SJoe Perches		ERROR("MISSING_SIGN_OFF",
4856000d1cc1SJoe Perches		      "Missing Signed-off-by: line(s)\n");
48570a920b5bSAndy Whitcroft	}
48580a920b5bSAndy Whitcroft
4859f0a594c1SAndy Whitcroft	print report_dump();
486013214adfSAndy Whitcroft	if ($summary && !($clean == 1 && $quiet == 1)) {
486113214adfSAndy Whitcroft		print "$filename " if ($summary_file);
48626c72ffaaSAndy Whitcroft		print "total: $cnt_error errors, $cnt_warn warnings, " .
48636c72ffaaSAndy Whitcroft			(($check)? "$cnt_chk checks, " : "") .
48646c72ffaaSAndy Whitcroft			"$cnt_lines lines checked\n";
48658905a67cSAndy Whitcroft		print "\n" if ($quiet == 0);
48666c72ffaaSAndy Whitcroft	}
48678905a67cSAndy Whitcroft
4868d2c0a235SAndy Whitcroft	if ($quiet == 0) {
4869d1fe9c09SJoe Perches
4870d1fe9c09SJoe Perches		if ($^V lt 5.10.0) {
4871d1fe9c09SJoe Perches			print("NOTE: perl $^V is not modern enough to detect all possible issues.\n");
4872d1fe9c09SJoe Perches			print("An upgrade to at least perl v5.10.0 is suggested.\n\n");
4873d1fe9c09SJoe Perches		}
4874d1fe9c09SJoe Perches
4875d2c0a235SAndy Whitcroft		# If there were whitespace errors which cleanpatch can fix
4876d2c0a235SAndy Whitcroft		# then suggest that.
4877d2c0a235SAndy Whitcroft		if ($rpt_cleaners) {
4878d2c0a235SAndy Whitcroft			print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n";
4879d2c0a235SAndy Whitcroft			print "      scripts/cleanfile\n\n";
4880b0781216SMike Frysinger			$rpt_cleaners = 0;
4881d2c0a235SAndy Whitcroft		}
4882d2c0a235SAndy Whitcroft	}
4883d2c0a235SAndy Whitcroft
488491bfe484SJoe Perches	hash_show_words(\%use_type, "Used");
488591bfe484SJoe Perches	hash_show_words(\%ignore_type, "Ignored");
4886000d1cc1SJoe Perches
48873705ce5bSJoe Perches	if ($clean == 0 && $fix && "@rawlines" ne "@fixed") {
48889624b8d6SJoe Perches		my $newfile = $filename;
48899624b8d6SJoe Perches		$newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace);
48903705ce5bSJoe Perches		my $linecount = 0;
48913705ce5bSJoe Perches		my $f;
48923705ce5bSJoe Perches
48933705ce5bSJoe Perches		open($f, '>', $newfile)
48943705ce5bSJoe Perches		    or die "$P: Can't open $newfile for write\n";
48953705ce5bSJoe Perches		foreach my $fixed_line (@fixed) {
48963705ce5bSJoe Perches			$linecount++;
48973705ce5bSJoe Perches			if ($file) {
48983705ce5bSJoe Perches				if ($linecount > 3) {
48993705ce5bSJoe Perches					$fixed_line =~ s/^\+//;
49003705ce5bSJoe Perches					print $f $fixed_line. "\n";
49013705ce5bSJoe Perches				}
49023705ce5bSJoe Perches			} else {
49033705ce5bSJoe Perches				print $f $fixed_line . "\n";
49043705ce5bSJoe Perches			}
49053705ce5bSJoe Perches		}
49063705ce5bSJoe Perches		close($f);
49073705ce5bSJoe Perches
49083705ce5bSJoe Perches		if (!$quiet) {
49093705ce5bSJoe Perches			print << "EOM";
49103705ce5bSJoe PerchesWrote EXPERIMENTAL --fix correction(s) to '$newfile'
49113705ce5bSJoe Perches
49123705ce5bSJoe PerchesDo _NOT_ trust the results written to this file.
49133705ce5bSJoe PerchesDo _NOT_ submit these changes without inspecting them for correctness.
49143705ce5bSJoe Perches
49153705ce5bSJoe PerchesThis EXPERIMENTAL file is simply a convenience to help rewrite patches.
49163705ce5bSJoe PerchesNo warranties, expressed or implied...
49173705ce5bSJoe Perches
49183705ce5bSJoe PerchesEOM
49193705ce5bSJoe Perches		}
49203705ce5bSJoe Perches	}
49213705ce5bSJoe Perches
49220a920b5bSAndy Whitcroft	if ($clean == 1 && $quiet == 0) {
4923c2fdda0dSAndy Whitcroft		print "$vname has no obvious style problems and is ready for submission.\n"
49240a920b5bSAndy Whitcroft	}
49250a920b5bSAndy Whitcroft	if ($clean == 0 && $quiet == 0) {
4926000d1cc1SJoe Perches		print << "EOM";
4927000d1cc1SJoe Perches$vname has style problems, please review.
4928000d1cc1SJoe Perches
4929000d1cc1SJoe PerchesIf any of these errors are false positives, please report
4930000d1cc1SJoe Perchesthem to the maintainer, see CHECKPATCH in MAINTAINERS.
4931000d1cc1SJoe PerchesEOM
49320a920b5bSAndy Whitcroft	}
493313214adfSAndy Whitcroft
49340a920b5bSAndy Whitcroft	return $clean;
49350a920b5bSAndy Whitcroft}
4936