xref: /linux-6.15/scripts/checkpatch.pl (revision 0c773d9d)
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},
358*0c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?char},
359*0c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?short\s+int},
360*0c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?short},
361*0c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?int},
362*0c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?long\s+int},
363*0c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?long\s+long\s+int},
364*0c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?long\s+long},
365*0c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?long},
366*0c773d9dSJoe Perches	qr{(?:un)?signed},
3678905a67cSAndy Whitcroft	qr{float},
3688905a67cSAndy Whitcroft	qr{double},
3698905a67cSAndy Whitcroft	qr{bool},
3708905a67cSAndy Whitcroft	qr{struct\s+$Ident},
3718905a67cSAndy Whitcroft	qr{union\s+$Ident},
3728905a67cSAndy Whitcroft	qr{enum\s+$Ident},
3738905a67cSAndy Whitcroft	qr{${Ident}_t},
3748905a67cSAndy Whitcroft	qr{${Ident}_handler},
3758905a67cSAndy Whitcroft	qr{${Ident}_handler_fn},
3768905a67cSAndy Whitcroft);
3778716de38SJoe Perchesour @typeListWithAttr = (
3788716de38SJoe Perches	@typeList,
3798716de38SJoe Perches	qr{struct\s+$InitAttribute\s+$Ident},
3808716de38SJoe Perches	qr{union\s+$InitAttribute\s+$Ident},
3818716de38SJoe Perches);
3828716de38SJoe Perches
383c45dcabdSAndy Whitcroftour @modifierList = (
384c45dcabdSAndy Whitcroft	qr{fastcall},
385c45dcabdSAndy Whitcroft);
3868905a67cSAndy Whitcroft
3872435880fSJoe Perchesour @mode_permission_funcs = (
3882435880fSJoe Perches	["module_param", 3],
3892435880fSJoe Perches	["module_param_(?:array|named|string)", 4],
3902435880fSJoe Perches	["module_param_array_named", 5],
3912435880fSJoe Perches	["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2],
3922435880fSJoe Perches	["proc_create(?:_data|)", 2],
3932435880fSJoe Perches	["(?:CLASS|DEVICE|SENSOR)_ATTR", 2],
3942435880fSJoe Perches);
3952435880fSJoe Perches
396515a235eSJoe Perches#Create a search pattern for all these functions to speed up a loop below
397515a235eSJoe Perchesour $mode_perms_search = "";
398515a235eSJoe Perchesforeach my $entry (@mode_permission_funcs) {
399515a235eSJoe Perches	$mode_perms_search .= '|' if ($mode_perms_search ne "");
400515a235eSJoe Perches	$mode_perms_search .= $entry->[0];
401515a235eSJoe Perches}
402515a235eSJoe Perches
4033f7bac03SJoe Perchesour $declaration_macros = qr{(?x:
4043f7bac03SJoe Perches	(?:$Storage\s+)?(?:DECLARE|DEFINE)_[A-Z]+\s*\(|
4053f7bac03SJoe Perches	(?:$Storage\s+)?LIST_HEAD\s*\(
4063f7bac03SJoe Perches)};
4073f7bac03SJoe Perches
4087840a94cSWolfram Sangour $allowed_asm_includes = qr{(?x:
4097840a94cSWolfram Sang	irq|
4107840a94cSWolfram Sang	memory
4117840a94cSWolfram Sang)};
4127840a94cSWolfram Sang# memory.h: ARM has a custom one
4137840a94cSWolfram Sang
4148905a67cSAndy Whitcroftsub build_types {
415d2172eb5SAndy Whitcroft	my $mods = "(?x:  \n" . join("|\n  ", @modifierList) . "\n)";
416d2172eb5SAndy Whitcroft	my $all = "(?x:  \n" . join("|\n  ", @typeList) . "\n)";
4178716de38SJoe Perches	my $allWithAttr = "(?x:  \n" . join("|\n  ", @typeListWithAttr) . "\n)";
418c8cb2ca3SAndy Whitcroft	$Modifier	= qr{(?:$Attribute|$Sparse|$mods)};
4198905a67cSAndy Whitcroft	$NonptrType	= qr{
420d2172eb5SAndy Whitcroft			(?:$Modifier\s+|const\s+)*
421cf655043SAndy Whitcroft			(?:
4226b48db24SAndy Whitcroft				(?:typeof|__typeof__)\s*\([^\)]*\)|
4238ed22cadSAndy Whitcroft				(?:$typeTypedefs\b)|
424c45dcabdSAndy Whitcroft				(?:${all}\b)
425cf655043SAndy Whitcroft			)
426c8cb2ca3SAndy Whitcroft			(?:\s+$Modifier|\s+const)*
4278905a67cSAndy Whitcroft		  }x;
4288716de38SJoe Perches	$NonptrTypeWithAttr	= qr{
4298716de38SJoe Perches			(?:$Modifier\s+|const\s+)*
4308716de38SJoe Perches			(?:
4318716de38SJoe Perches				(?:typeof|__typeof__)\s*\([^\)]*\)|
4328716de38SJoe Perches				(?:$typeTypedefs\b)|
4338716de38SJoe Perches				(?:${allWithAttr}\b)
4348716de38SJoe Perches			)
4358716de38SJoe Perches			(?:\s+$Modifier|\s+const)*
4368716de38SJoe Perches		  }x;
4378905a67cSAndy Whitcroft	$Type	= qr{
438c45dcabdSAndy Whitcroft			$NonptrType
4391574a29fSJoe Perches			(?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
440c8cb2ca3SAndy Whitcroft			(?:\s+$Inline|\s+$Modifier)*
4418905a67cSAndy Whitcroft		  }x;
44291cb5195SJoe Perches	$Declare	= qr{(?:$Storage\s+(?:$Inline\s+)?)?$Type};
4438905a67cSAndy Whitcroft}
4448905a67cSAndy Whitcroftbuild_types();
4456c72ffaaSAndy Whitcroft
4467d2367afSJoe Perchesour $Typecast	= qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
447d1fe9c09SJoe Perches
448d1fe9c09SJoe Perches# Using $balanced_parens, $LvalOrFunc, or $FuncArg
449d1fe9c09SJoe Perches# requires at least perl version v5.10.0
450d1fe9c09SJoe Perches# Any use must be runtime checked with $^V
451d1fe9c09SJoe Perches
452d1fe9c09SJoe Perchesour $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
4532435880fSJoe Perchesour $LvalOrFunc	= qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*};
454d7c76ba7SJoe Perchesour $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant)};
4557d2367afSJoe Perches
4567d2367afSJoe Perchessub deparenthesize {
4577d2367afSJoe Perches	my ($string) = @_;
4587d2367afSJoe Perches	return "" if (!defined($string));
4595b9553abSJoe Perches
4605b9553abSJoe Perches	while ($string =~ /^\s*\(.*\)\s*$/) {
4615b9553abSJoe Perches		$string =~ s@^\s*\(\s*@@;
4625b9553abSJoe Perches		$string =~ s@\s*\)\s*$@@;
4635b9553abSJoe Perches	}
4645b9553abSJoe Perches
4657d2367afSJoe Perches	$string =~ s@\s+@ @g;
4665b9553abSJoe Perches
4677d2367afSJoe Perches	return $string;
4687d2367afSJoe Perches}
4697d2367afSJoe Perches
4703445686aSJoe Perchessub seed_camelcase_file {
4713445686aSJoe Perches	my ($file) = @_;
4723445686aSJoe Perches
4733445686aSJoe Perches	return if (!(-f $file));
4743445686aSJoe Perches
4753445686aSJoe Perches	local $/;
4763445686aSJoe Perches
4773445686aSJoe Perches	open(my $include_file, '<', "$file")
4783445686aSJoe Perches	    or warn "$P: Can't read '$file' $!\n";
4793445686aSJoe Perches	my $text = <$include_file>;
4803445686aSJoe Perches	close($include_file);
4813445686aSJoe Perches
4823445686aSJoe Perches	my @lines = split('\n', $text);
4833445686aSJoe Perches
4843445686aSJoe Perches	foreach my $line (@lines) {
4853445686aSJoe Perches		next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/);
4863445686aSJoe Perches		if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) {
4873445686aSJoe Perches			$camelcase{$1} = 1;
48811ea516aSJoe Perches		} elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) {
48911ea516aSJoe Perches			$camelcase{$1} = 1;
49011ea516aSJoe Perches		} elsif ($line =~ /^\s*(?:union|struct|enum)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[;\{]/) {
4913445686aSJoe Perches			$camelcase{$1} = 1;
4923445686aSJoe Perches		}
4933445686aSJoe Perches	}
4943445686aSJoe Perches}
4953445686aSJoe Perches
4963445686aSJoe Perchesmy $camelcase_seeded = 0;
4973445686aSJoe Perchessub seed_camelcase_includes {
4983445686aSJoe Perches	return if ($camelcase_seeded);
4993445686aSJoe Perches
5003445686aSJoe Perches	my $files;
501c707a81dSJoe Perches	my $camelcase_cache = "";
502c707a81dSJoe Perches	my @include_files = ();
503c707a81dSJoe Perches
504c707a81dSJoe Perches	$camelcase_seeded = 1;
505351b2a1fSJoe Perches
5063645e328SRichard Genoud	if (-e ".git") {
507351b2a1fSJoe Perches		my $git_last_include_commit = `git log --no-merges --pretty=format:"%h%n" -1 -- include`;
508351b2a1fSJoe Perches		chomp $git_last_include_commit;
509c707a81dSJoe Perches		$camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit";
510c707a81dSJoe Perches	} else {
511c707a81dSJoe Perches		my $last_mod_date = 0;
512c707a81dSJoe Perches		$files = `find $root/include -name "*.h"`;
513c707a81dSJoe Perches		@include_files = split('\n', $files);
514c707a81dSJoe Perches		foreach my $file (@include_files) {
515c707a81dSJoe Perches			my $date = POSIX::strftime("%Y%m%d%H%M",
516c707a81dSJoe Perches						   localtime((stat $file)[9]));
517c707a81dSJoe Perches			$last_mod_date = $date if ($last_mod_date < $date);
518c707a81dSJoe Perches		}
519c707a81dSJoe Perches		$camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date";
520c707a81dSJoe Perches	}
521c707a81dSJoe Perches
522c707a81dSJoe Perches	if ($camelcase_cache ne "" && -f $camelcase_cache) {
523c707a81dSJoe Perches		open(my $camelcase_file, '<', "$camelcase_cache")
524c707a81dSJoe Perches		    or warn "$P: Can't read '$camelcase_cache' $!\n";
525351b2a1fSJoe Perches		while (<$camelcase_file>) {
526351b2a1fSJoe Perches			chomp;
527351b2a1fSJoe Perches			$camelcase{$_} = 1;
528351b2a1fSJoe Perches		}
529351b2a1fSJoe Perches		close($camelcase_file);
530351b2a1fSJoe Perches
531351b2a1fSJoe Perches		return;
532351b2a1fSJoe Perches	}
533c707a81dSJoe Perches
5343645e328SRichard Genoud	if (-e ".git") {
535c707a81dSJoe Perches		$files = `git ls-files "include/*.h"`;
536c707a81dSJoe Perches		@include_files = split('\n', $files);
5373445686aSJoe Perches	}
538c707a81dSJoe Perches
5393445686aSJoe Perches	foreach my $file (@include_files) {
5403445686aSJoe Perches		seed_camelcase_file($file);
5413445686aSJoe Perches	}
542351b2a1fSJoe Perches
543c707a81dSJoe Perches	if ($camelcase_cache ne "") {
544351b2a1fSJoe Perches		unlink glob ".checkpatch-camelcase.*";
545c707a81dSJoe Perches		open(my $camelcase_file, '>', "$camelcase_cache")
546c707a81dSJoe Perches		    or warn "$P: Can't write '$camelcase_cache' $!\n";
547351b2a1fSJoe Perches		foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) {
548351b2a1fSJoe Perches			print $camelcase_file ("$_\n");
549351b2a1fSJoe Perches		}
550351b2a1fSJoe Perches		close($camelcase_file);
551351b2a1fSJoe Perches	}
5523445686aSJoe Perches}
5533445686aSJoe Perches
554d311cd44SJoe Perchessub git_commit_info {
555d311cd44SJoe Perches	my ($commit, $id, $desc) = @_;
556d311cd44SJoe Perches
557d311cd44SJoe Perches	return ($id, $desc) if ((which("git") eq "") || !(-e ".git"));
558d311cd44SJoe Perches
559d311cd44SJoe Perches	my $output = `git log --no-color --format='%H %s' -1 $commit 2>&1`;
560d311cd44SJoe Perches	$output =~ s/^\s*//gm;
561d311cd44SJoe Perches	my @lines = split("\n", $output);
562d311cd44SJoe Perches
563d311cd44SJoe Perches	if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous\./) {
564d311cd44SJoe Perches# Maybe one day convert this block of bash into something that returns
565d311cd44SJoe Perches# all matching commit ids, but it's very slow...
566d311cd44SJoe Perches#
567d311cd44SJoe Perches#		echo "checking commits $1..."
568d311cd44SJoe Perches#		git rev-list --remotes | grep -i "^$1" |
569d311cd44SJoe Perches#		while read line ; do
570d311cd44SJoe Perches#		    git log --format='%H %s' -1 $line |
571d311cd44SJoe Perches#		    echo "commit $(cut -c 1-12,41-)"
572d311cd44SJoe Perches#		done
573d311cd44SJoe Perches	} elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./) {
574d311cd44SJoe Perches	} else {
575d311cd44SJoe Perches		$id = substr($lines[0], 0, 12);
576d311cd44SJoe Perches		$desc = substr($lines[0], 41);
577d311cd44SJoe Perches	}
578d311cd44SJoe Perches
579d311cd44SJoe Perches	return ($id, $desc);
580d311cd44SJoe Perches}
581d311cd44SJoe Perches
5826c72ffaaSAndy Whitcroft$chk_signoff = 0 if ($file);
5830a920b5bSAndy Whitcroft
58400df344fSAndy Whitcroftmy @rawlines = ();
585c2fdda0dSAndy Whitcroftmy @lines = ();
5863705ce5bSJoe Perchesmy @fixed = ();
587d752fcc8SJoe Perchesmy @fixed_inserted = ();
588d752fcc8SJoe Perchesmy @fixed_deleted = ();
589194f66fcSJoe Perchesmy $fixlinenr = -1;
590194f66fcSJoe Perches
591c2fdda0dSAndy Whitcroftmy $vname;
5926c72ffaaSAndy Whitcroftfor my $filename (@ARGV) {
59321caa13cSAndy Whitcroft	my $FILE;
5946c72ffaaSAndy Whitcroft	if ($file) {
59521caa13cSAndy Whitcroft		open($FILE, '-|', "diff -u /dev/null $filename") ||
5966c72ffaaSAndy Whitcroft			die "$P: $filename: diff failed - $!\n";
59721caa13cSAndy Whitcroft	} elsif ($filename eq '-') {
59821caa13cSAndy Whitcroft		open($FILE, '<&STDIN');
5996c72ffaaSAndy Whitcroft	} else {
60021caa13cSAndy Whitcroft		open($FILE, '<', "$filename") ||
6016c72ffaaSAndy Whitcroft			die "$P: $filename: open failed - $!\n";
6026c72ffaaSAndy Whitcroft	}
603c2fdda0dSAndy Whitcroft	if ($filename eq '-') {
604c2fdda0dSAndy Whitcroft		$vname = 'Your patch';
605c2fdda0dSAndy Whitcroft	} else {
606c2fdda0dSAndy Whitcroft		$vname = $filename;
607c2fdda0dSAndy Whitcroft	}
60821caa13cSAndy Whitcroft	while (<$FILE>) {
6090a920b5bSAndy Whitcroft		chomp;
61000df344fSAndy Whitcroft		push(@rawlines, $_);
6116c72ffaaSAndy Whitcroft	}
61221caa13cSAndy Whitcroft	close($FILE);
613c2fdda0dSAndy Whitcroft	if (!process($filename)) {
6140a920b5bSAndy Whitcroft		$exit = 1;
6150a920b5bSAndy Whitcroft	}
61600df344fSAndy Whitcroft	@rawlines = ();
61713214adfSAndy Whitcroft	@lines = ();
6183705ce5bSJoe Perches	@fixed = ();
619d752fcc8SJoe Perches	@fixed_inserted = ();
620d752fcc8SJoe Perches	@fixed_deleted = ();
621194f66fcSJoe Perches	$fixlinenr = -1;
6220a920b5bSAndy Whitcroft}
6230a920b5bSAndy Whitcroft
6240a920b5bSAndy Whitcroftexit($exit);
6250a920b5bSAndy Whitcroft
6260a920b5bSAndy Whitcroftsub top_of_kernel_tree {
6276c72ffaaSAndy Whitcroft	my ($root) = @_;
6286c72ffaaSAndy Whitcroft
6296c72ffaaSAndy Whitcroft	my @tree_check = (
6306c72ffaaSAndy Whitcroft		"COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
6316c72ffaaSAndy Whitcroft		"README", "Documentation", "arch", "include", "drivers",
6326c72ffaaSAndy Whitcroft		"fs", "init", "ipc", "kernel", "lib", "scripts",
6336c72ffaaSAndy Whitcroft	);
6346c72ffaaSAndy Whitcroft
6356c72ffaaSAndy Whitcroft	foreach my $check (@tree_check) {
6366c72ffaaSAndy Whitcroft		if (! -e $root . '/' . $check) {
6370a920b5bSAndy Whitcroft			return 0;
6380a920b5bSAndy Whitcroft		}
6396c72ffaaSAndy Whitcroft	}
6406c72ffaaSAndy Whitcroft	return 1;
6416c72ffaaSAndy Whitcroft}
6420a920b5bSAndy Whitcroft
64320112475SJoe Perchessub parse_email {
64420112475SJoe Perches	my ($formatted_email) = @_;
64520112475SJoe Perches
64620112475SJoe Perches	my $name = "";
64720112475SJoe Perches	my $address = "";
64820112475SJoe Perches	my $comment = "";
64920112475SJoe Perches
65020112475SJoe Perches	if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
65120112475SJoe Perches		$name = $1;
65220112475SJoe Perches		$address = $2;
65320112475SJoe Perches		$comment = $3 if defined $3;
65420112475SJoe Perches	} elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
65520112475SJoe Perches		$address = $1;
65620112475SJoe Perches		$comment = $2 if defined $2;
65720112475SJoe Perches	} elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
65820112475SJoe Perches		$address = $1;
65920112475SJoe Perches		$comment = $2 if defined $2;
66020112475SJoe Perches		$formatted_email =~ s/$address.*$//;
66120112475SJoe Perches		$name = $formatted_email;
6623705ce5bSJoe Perches		$name = trim($name);
66320112475SJoe Perches		$name =~ s/^\"|\"$//g;
66420112475SJoe Perches		# If there's a name left after stripping spaces and
66520112475SJoe Perches		# leading quotes, and the address doesn't have both
66620112475SJoe Perches		# leading and trailing angle brackets, the address
66720112475SJoe Perches		# is invalid. ie:
66820112475SJoe Perches		#   "joe smith [email protected]" bad
66920112475SJoe Perches		#   "joe smith <[email protected]" bad
67020112475SJoe Perches		if ($name ne "" && $address !~ /^<[^>]+>$/) {
67120112475SJoe Perches			$name = "";
67220112475SJoe Perches			$address = "";
67320112475SJoe Perches			$comment = "";
67420112475SJoe Perches		}
67520112475SJoe Perches	}
67620112475SJoe Perches
6773705ce5bSJoe Perches	$name = trim($name);
67820112475SJoe Perches	$name =~ s/^\"|\"$//g;
6793705ce5bSJoe Perches	$address = trim($address);
68020112475SJoe Perches	$address =~ s/^\<|\>$//g;
68120112475SJoe Perches
68220112475SJoe Perches	if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
68320112475SJoe Perches		$name =~ s/(?<!\\)"/\\"/g; ##escape quotes
68420112475SJoe Perches		$name = "\"$name\"";
68520112475SJoe Perches	}
68620112475SJoe Perches
68720112475SJoe Perches	return ($name, $address, $comment);
68820112475SJoe Perches}
68920112475SJoe Perches
69020112475SJoe Perchessub format_email {
69120112475SJoe Perches	my ($name, $address) = @_;
69220112475SJoe Perches
69320112475SJoe Perches	my $formatted_email;
69420112475SJoe Perches
6953705ce5bSJoe Perches	$name = trim($name);
69620112475SJoe Perches	$name =~ s/^\"|\"$//g;
6973705ce5bSJoe Perches	$address = trim($address);
69820112475SJoe Perches
69920112475SJoe Perches	if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
70020112475SJoe Perches		$name =~ s/(?<!\\)"/\\"/g; ##escape quotes
70120112475SJoe Perches		$name = "\"$name\"";
70220112475SJoe Perches	}
70320112475SJoe Perches
70420112475SJoe Perches	if ("$name" eq "") {
70520112475SJoe Perches		$formatted_email = "$address";
70620112475SJoe Perches	} else {
70720112475SJoe Perches		$formatted_email = "$name <$address>";
70820112475SJoe Perches	}
70920112475SJoe Perches
71020112475SJoe Perches	return $formatted_email;
71120112475SJoe Perches}
71220112475SJoe Perches
713d311cd44SJoe Perchessub which {
714d311cd44SJoe Perches	my ($bin) = @_;
715d311cd44SJoe Perches
716d311cd44SJoe Perches	foreach my $path (split(/:/, $ENV{PATH})) {
717d311cd44SJoe Perches		if (-e "$path/$bin") {
718d311cd44SJoe Perches			return "$path/$bin";
719d311cd44SJoe Perches		}
720d311cd44SJoe Perches	}
721d311cd44SJoe Perches
722d311cd44SJoe Perches	return "";
723d311cd44SJoe Perches}
724d311cd44SJoe Perches
725000d1cc1SJoe Perchessub which_conf {
726000d1cc1SJoe Perches	my ($conf) = @_;
727000d1cc1SJoe Perches
728000d1cc1SJoe Perches	foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
729000d1cc1SJoe Perches		if (-e "$path/$conf") {
730000d1cc1SJoe Perches			return "$path/$conf";
731000d1cc1SJoe Perches		}
732000d1cc1SJoe Perches	}
733000d1cc1SJoe Perches
734000d1cc1SJoe Perches	return "";
735000d1cc1SJoe Perches}
736000d1cc1SJoe Perches
7370a920b5bSAndy Whitcroftsub expand_tabs {
7380a920b5bSAndy Whitcroft	my ($str) = @_;
7390a920b5bSAndy Whitcroft
7400a920b5bSAndy Whitcroft	my $res = '';
7410a920b5bSAndy Whitcroft	my $n = 0;
7420a920b5bSAndy Whitcroft	for my $c (split(//, $str)) {
7430a920b5bSAndy Whitcroft		if ($c eq "\t") {
7440a920b5bSAndy Whitcroft			$res .= ' ';
7450a920b5bSAndy Whitcroft			$n++;
7460a920b5bSAndy Whitcroft			for (; ($n % 8) != 0; $n++) {
7470a920b5bSAndy Whitcroft				$res .= ' ';
7480a920b5bSAndy Whitcroft			}
7490a920b5bSAndy Whitcroft			next;
7500a920b5bSAndy Whitcroft		}
7510a920b5bSAndy Whitcroft		$res .= $c;
7520a920b5bSAndy Whitcroft		$n++;
7530a920b5bSAndy Whitcroft	}
7540a920b5bSAndy Whitcroft
7550a920b5bSAndy Whitcroft	return $res;
7560a920b5bSAndy Whitcroft}
7576c72ffaaSAndy Whitcroftsub copy_spacing {
758773647a0SAndy Whitcroft	(my $res = shift) =~ tr/\t/ /c;
7596c72ffaaSAndy Whitcroft	return $res;
7606c72ffaaSAndy Whitcroft}
7610a920b5bSAndy Whitcroft
7624a0df2efSAndy Whitcroftsub line_stats {
7634a0df2efSAndy Whitcroft	my ($line) = @_;
7644a0df2efSAndy Whitcroft
7654a0df2efSAndy Whitcroft	# Drop the diff line leader and expand tabs
7664a0df2efSAndy Whitcroft	$line =~ s/^.//;
7674a0df2efSAndy Whitcroft	$line = expand_tabs($line);
7684a0df2efSAndy Whitcroft
7694a0df2efSAndy Whitcroft	# Pick the indent from the front of the line.
7704a0df2efSAndy Whitcroft	my ($white) = ($line =~ /^(\s*)/);
7714a0df2efSAndy Whitcroft
7724a0df2efSAndy Whitcroft	return (length($line), length($white));
7734a0df2efSAndy Whitcroft}
7744a0df2efSAndy Whitcroft
775773647a0SAndy Whitcroftmy $sanitise_quote = '';
776773647a0SAndy Whitcroft
777773647a0SAndy Whitcroftsub sanitise_line_reset {
778773647a0SAndy Whitcroft	my ($in_comment) = @_;
779773647a0SAndy Whitcroft
780773647a0SAndy Whitcroft	if ($in_comment) {
781773647a0SAndy Whitcroft		$sanitise_quote = '*/';
782773647a0SAndy Whitcroft	} else {
783773647a0SAndy Whitcroft		$sanitise_quote = '';
784773647a0SAndy Whitcroft	}
785773647a0SAndy Whitcroft}
78600df344fSAndy Whitcroftsub sanitise_line {
78700df344fSAndy Whitcroft	my ($line) = @_;
78800df344fSAndy Whitcroft
78900df344fSAndy Whitcroft	my $res = '';
79000df344fSAndy Whitcroft	my $l = '';
79100df344fSAndy Whitcroft
792c2fdda0dSAndy Whitcroft	my $qlen = 0;
793773647a0SAndy Whitcroft	my $off = 0;
794773647a0SAndy Whitcroft	my $c;
79500df344fSAndy Whitcroft
796773647a0SAndy Whitcroft	# Always copy over the diff marker.
797773647a0SAndy Whitcroft	$res = substr($line, 0, 1);
798773647a0SAndy Whitcroft
799773647a0SAndy Whitcroft	for ($off = 1; $off < length($line); $off++) {
800773647a0SAndy Whitcroft		$c = substr($line, $off, 1);
801773647a0SAndy Whitcroft
802773647a0SAndy Whitcroft		# Comments we are wacking completly including the begin
803773647a0SAndy Whitcroft		# and end, all to $;.
804773647a0SAndy Whitcroft		if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
805773647a0SAndy Whitcroft			$sanitise_quote = '*/';
806773647a0SAndy Whitcroft
807773647a0SAndy Whitcroft			substr($res, $off, 2, "$;$;");
808773647a0SAndy Whitcroft			$off++;
80900df344fSAndy Whitcroft			next;
810773647a0SAndy Whitcroft		}
81181bc0e02SAndy Whitcroft		if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
812773647a0SAndy Whitcroft			$sanitise_quote = '';
813773647a0SAndy Whitcroft			substr($res, $off, 2, "$;$;");
814773647a0SAndy Whitcroft			$off++;
815773647a0SAndy Whitcroft			next;
816773647a0SAndy Whitcroft		}
817113f04a8SDaniel Walker		if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
818113f04a8SDaniel Walker			$sanitise_quote = '//';
819113f04a8SDaniel Walker
820113f04a8SDaniel Walker			substr($res, $off, 2, $sanitise_quote);
821113f04a8SDaniel Walker			$off++;
822113f04a8SDaniel Walker			next;
823113f04a8SDaniel Walker		}
824773647a0SAndy Whitcroft
825773647a0SAndy Whitcroft		# A \ in a string means ignore the next character.
826773647a0SAndy Whitcroft		if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
827773647a0SAndy Whitcroft		    $c eq "\\") {
828773647a0SAndy Whitcroft			substr($res, $off, 2, 'XX');
829773647a0SAndy Whitcroft			$off++;
830773647a0SAndy Whitcroft			next;
831773647a0SAndy Whitcroft		}
832773647a0SAndy Whitcroft		# Regular quotes.
833773647a0SAndy Whitcroft		if ($c eq "'" || $c eq '"') {
834773647a0SAndy Whitcroft			if ($sanitise_quote eq '') {
835773647a0SAndy Whitcroft				$sanitise_quote = $c;
836773647a0SAndy Whitcroft
837773647a0SAndy Whitcroft				substr($res, $off, 1, $c);
838773647a0SAndy Whitcroft				next;
839773647a0SAndy Whitcroft			} elsif ($sanitise_quote eq $c) {
840773647a0SAndy Whitcroft				$sanitise_quote = '';
84100df344fSAndy Whitcroft			}
84200df344fSAndy Whitcroft		}
843773647a0SAndy Whitcroft
844fae17daeSAndy Whitcroft		#print "c<$c> SQ<$sanitise_quote>\n";
845773647a0SAndy Whitcroft		if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
846773647a0SAndy Whitcroft			substr($res, $off, 1, $;);
847113f04a8SDaniel Walker		} elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
848113f04a8SDaniel Walker			substr($res, $off, 1, $;);
849773647a0SAndy Whitcroft		} elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
850773647a0SAndy Whitcroft			substr($res, $off, 1, 'X');
85100df344fSAndy Whitcroft		} else {
852773647a0SAndy Whitcroft			substr($res, $off, 1, $c);
85300df344fSAndy Whitcroft		}
854c2fdda0dSAndy Whitcroft	}
855c2fdda0dSAndy Whitcroft
856113f04a8SDaniel Walker	if ($sanitise_quote eq '//') {
857113f04a8SDaniel Walker		$sanitise_quote = '';
858113f04a8SDaniel Walker	}
859113f04a8SDaniel Walker
860c2fdda0dSAndy Whitcroft	# The pathname on a #include may be surrounded by '<' and '>'.
861c45dcabdSAndy Whitcroft	if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
862c2fdda0dSAndy Whitcroft		my $clean = 'X' x length($1);
863c2fdda0dSAndy Whitcroft		$res =~ s@\<.*\>@<$clean>@;
864c2fdda0dSAndy Whitcroft
865c2fdda0dSAndy Whitcroft	# The whole of a #error is a string.
866c45dcabdSAndy Whitcroft	} elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
867c2fdda0dSAndy Whitcroft		my $clean = 'X' x length($1);
868c45dcabdSAndy Whitcroft		$res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
869c2fdda0dSAndy Whitcroft	}
870c2fdda0dSAndy Whitcroft
87100df344fSAndy Whitcroft	return $res;
87200df344fSAndy Whitcroft}
87300df344fSAndy Whitcroft
874a6962d72SJoe Perchessub get_quoted_string {
875a6962d72SJoe Perches	my ($line, $rawline) = @_;
876a6962d72SJoe Perches
877a6962d72SJoe Perches	return "" if ($line !~ m/(\"[X]+\")/g);
878a6962d72SJoe Perches	return substr($rawline, $-[0], $+[0] - $-[0]);
879a6962d72SJoe Perches}
880a6962d72SJoe Perches
8818905a67cSAndy Whitcroftsub ctx_statement_block {
8828905a67cSAndy Whitcroft	my ($linenr, $remain, $off) = @_;
8838905a67cSAndy Whitcroft	my $line = $linenr - 1;
8848905a67cSAndy Whitcroft	my $blk = '';
8858905a67cSAndy Whitcroft	my $soff = $off;
8868905a67cSAndy Whitcroft	my $coff = $off - 1;
887773647a0SAndy Whitcroft	my $coff_set = 0;
8888905a67cSAndy Whitcroft
88913214adfSAndy Whitcroft	my $loff = 0;
89013214adfSAndy Whitcroft
8918905a67cSAndy Whitcroft	my $type = '';
8928905a67cSAndy Whitcroft	my $level = 0;
893a2750645SAndy Whitcroft	my @stack = ();
894cf655043SAndy Whitcroft	my $p;
8958905a67cSAndy Whitcroft	my $c;
8968905a67cSAndy Whitcroft	my $len = 0;
89713214adfSAndy Whitcroft
89813214adfSAndy Whitcroft	my $remainder;
8998905a67cSAndy Whitcroft	while (1) {
900a2750645SAndy Whitcroft		@stack = (['', 0]) if ($#stack == -1);
901a2750645SAndy Whitcroft
902773647a0SAndy Whitcroft		#warn "CSB: blk<$blk> remain<$remain>\n";
9038905a67cSAndy Whitcroft		# If we are about to drop off the end, pull in more
9048905a67cSAndy Whitcroft		# context.
9058905a67cSAndy Whitcroft		if ($off >= $len) {
9068905a67cSAndy Whitcroft			for (; $remain > 0; $line++) {
907dea33496SAndy Whitcroft				last if (!defined $lines[$line]);
908c2fdda0dSAndy Whitcroft				next if ($lines[$line] =~ /^-/);
9098905a67cSAndy Whitcroft				$remain--;
91013214adfSAndy Whitcroft				$loff = $len;
911c2fdda0dSAndy Whitcroft				$blk .= $lines[$line] . "\n";
9128905a67cSAndy Whitcroft				$len = length($blk);
9138905a67cSAndy Whitcroft				$line++;
9148905a67cSAndy Whitcroft				last;
9158905a67cSAndy Whitcroft			}
9168905a67cSAndy Whitcroft			# Bail if there is no further context.
9178905a67cSAndy Whitcroft			#warn "CSB: blk<$blk> off<$off> len<$len>\n";
91813214adfSAndy Whitcroft			if ($off >= $len) {
9198905a67cSAndy Whitcroft				last;
9208905a67cSAndy Whitcroft			}
921f74bd194SAndy Whitcroft			if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
922f74bd194SAndy Whitcroft				$level++;
923f74bd194SAndy Whitcroft				$type = '#';
924f74bd194SAndy Whitcroft			}
9258905a67cSAndy Whitcroft		}
926cf655043SAndy Whitcroft		$p = $c;
9278905a67cSAndy Whitcroft		$c = substr($blk, $off, 1);
92813214adfSAndy Whitcroft		$remainder = substr($blk, $off);
9298905a67cSAndy Whitcroft
930773647a0SAndy Whitcroft		#warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
9314635f4fbSAndy Whitcroft
9324635f4fbSAndy Whitcroft		# Handle nested #if/#else.
9334635f4fbSAndy Whitcroft		if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
9344635f4fbSAndy Whitcroft			push(@stack, [ $type, $level ]);
9354635f4fbSAndy Whitcroft		} elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
9364635f4fbSAndy Whitcroft			($type, $level) = @{$stack[$#stack - 1]};
9374635f4fbSAndy Whitcroft		} elsif ($remainder =~ /^#\s*endif\b/) {
9384635f4fbSAndy Whitcroft			($type, $level) = @{pop(@stack)};
9394635f4fbSAndy Whitcroft		}
9404635f4fbSAndy Whitcroft
9418905a67cSAndy Whitcroft		# Statement ends at the ';' or a close '}' at the
9428905a67cSAndy Whitcroft		# outermost level.
9438905a67cSAndy Whitcroft		if ($level == 0 && $c eq ';') {
9448905a67cSAndy Whitcroft			last;
9458905a67cSAndy Whitcroft		}
9468905a67cSAndy Whitcroft
94713214adfSAndy Whitcroft		# An else is really a conditional as long as its not else if
948773647a0SAndy Whitcroft		if ($level == 0 && $coff_set == 0 &&
949773647a0SAndy Whitcroft				(!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
950773647a0SAndy Whitcroft				$remainder =~ /^(else)(?:\s|{)/ &&
951773647a0SAndy Whitcroft				$remainder !~ /^else\s+if\b/) {
952773647a0SAndy Whitcroft			$coff = $off + length($1) - 1;
953773647a0SAndy Whitcroft			$coff_set = 1;
954773647a0SAndy Whitcroft			#warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
955773647a0SAndy Whitcroft			#warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
95613214adfSAndy Whitcroft		}
95713214adfSAndy Whitcroft
9588905a67cSAndy Whitcroft		if (($type eq '' || $type eq '(') && $c eq '(') {
9598905a67cSAndy Whitcroft			$level++;
9608905a67cSAndy Whitcroft			$type = '(';
9618905a67cSAndy Whitcroft		}
9628905a67cSAndy Whitcroft		if ($type eq '(' && $c eq ')') {
9638905a67cSAndy Whitcroft			$level--;
9648905a67cSAndy Whitcroft			$type = ($level != 0)? '(' : '';
9658905a67cSAndy Whitcroft
9668905a67cSAndy Whitcroft			if ($level == 0 && $coff < $soff) {
9678905a67cSAndy Whitcroft				$coff = $off;
968773647a0SAndy Whitcroft				$coff_set = 1;
969773647a0SAndy Whitcroft				#warn "CSB: mark coff<$coff>\n";
9708905a67cSAndy Whitcroft			}
9718905a67cSAndy Whitcroft		}
9728905a67cSAndy Whitcroft		if (($type eq '' || $type eq '{') && $c eq '{') {
9738905a67cSAndy Whitcroft			$level++;
9748905a67cSAndy Whitcroft			$type = '{';
9758905a67cSAndy Whitcroft		}
9768905a67cSAndy Whitcroft		if ($type eq '{' && $c eq '}') {
9778905a67cSAndy Whitcroft			$level--;
9788905a67cSAndy Whitcroft			$type = ($level != 0)? '{' : '';
9798905a67cSAndy Whitcroft
9808905a67cSAndy Whitcroft			if ($level == 0) {
981b998e001SPatrick Pannuto				if (substr($blk, $off + 1, 1) eq ';') {
982b998e001SPatrick Pannuto					$off++;
983b998e001SPatrick Pannuto				}
9848905a67cSAndy Whitcroft				last;
9858905a67cSAndy Whitcroft			}
9868905a67cSAndy Whitcroft		}
987f74bd194SAndy Whitcroft		# Preprocessor commands end at the newline unless escaped.
988f74bd194SAndy Whitcroft		if ($type eq '#' && $c eq "\n" && $p ne "\\") {
989f74bd194SAndy Whitcroft			$level--;
990f74bd194SAndy Whitcroft			$type = '';
991f74bd194SAndy Whitcroft			$off++;
992f74bd194SAndy Whitcroft			last;
993f74bd194SAndy Whitcroft		}
9948905a67cSAndy Whitcroft		$off++;
9958905a67cSAndy Whitcroft	}
996a3bb97a7SAndy Whitcroft	# We are truly at the end, so shuffle to the next line.
99713214adfSAndy Whitcroft	if ($off == $len) {
998a3bb97a7SAndy Whitcroft		$loff = $len + 1;
99913214adfSAndy Whitcroft		$line++;
100013214adfSAndy Whitcroft		$remain--;
100113214adfSAndy Whitcroft	}
10028905a67cSAndy Whitcroft
10038905a67cSAndy Whitcroft	my $statement = substr($blk, $soff, $off - $soff + 1);
10048905a67cSAndy Whitcroft	my $condition = substr($blk, $soff, $coff - $soff + 1);
10058905a67cSAndy Whitcroft
10068905a67cSAndy Whitcroft	#warn "STATEMENT<$statement>\n";
10078905a67cSAndy Whitcroft	#warn "CONDITION<$condition>\n";
10088905a67cSAndy Whitcroft
1009773647a0SAndy Whitcroft	#print "coff<$coff> soff<$off> loff<$loff>\n";
101013214adfSAndy Whitcroft
101113214adfSAndy Whitcroft	return ($statement, $condition,
101213214adfSAndy Whitcroft			$line, $remain + 1, $off - $loff + 1, $level);
101313214adfSAndy Whitcroft}
101413214adfSAndy Whitcroft
1015cf655043SAndy Whitcroftsub statement_lines {
1016cf655043SAndy Whitcroft	my ($stmt) = @_;
1017cf655043SAndy Whitcroft
1018cf655043SAndy Whitcroft	# Strip the diff line prefixes and rip blank lines at start and end.
1019cf655043SAndy Whitcroft	$stmt =~ s/(^|\n)./$1/g;
1020cf655043SAndy Whitcroft	$stmt =~ s/^\s*//;
1021cf655043SAndy Whitcroft	$stmt =~ s/\s*$//;
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_rawlines {
1029cf655043SAndy Whitcroft	my ($stmt) = @_;
1030cf655043SAndy Whitcroft
1031cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
1032cf655043SAndy Whitcroft
1033cf655043SAndy Whitcroft	return $#stmt_lines + 2;
1034cf655043SAndy Whitcroft}
1035cf655043SAndy Whitcroft
1036cf655043SAndy Whitcroftsub statement_block_size {
1037cf655043SAndy Whitcroft	my ($stmt) = @_;
1038cf655043SAndy Whitcroft
1039cf655043SAndy Whitcroft	$stmt =~ s/(^|\n)./$1/g;
1040cf655043SAndy Whitcroft	$stmt =~ s/^\s*{//;
1041cf655043SAndy Whitcroft	$stmt =~ s/}\s*$//;
1042cf655043SAndy Whitcroft	$stmt =~ s/^\s*//;
1043cf655043SAndy Whitcroft	$stmt =~ s/\s*$//;
1044cf655043SAndy Whitcroft
1045cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
1046cf655043SAndy Whitcroft	my @stmt_statements = ($stmt =~ /;/g);
1047cf655043SAndy Whitcroft
1048cf655043SAndy Whitcroft	my $stmt_lines = $#stmt_lines + 2;
1049cf655043SAndy Whitcroft	my $stmt_statements = $#stmt_statements + 1;
1050cf655043SAndy Whitcroft
1051cf655043SAndy Whitcroft	if ($stmt_lines > $stmt_statements) {
1052cf655043SAndy Whitcroft		return $stmt_lines;
1053cf655043SAndy Whitcroft	} else {
1054cf655043SAndy Whitcroft		return $stmt_statements;
1055cf655043SAndy Whitcroft	}
1056cf655043SAndy Whitcroft}
1057cf655043SAndy Whitcroft
105813214adfSAndy Whitcroftsub ctx_statement_full {
105913214adfSAndy Whitcroft	my ($linenr, $remain, $off) = @_;
106013214adfSAndy Whitcroft	my ($statement, $condition, $level);
106113214adfSAndy Whitcroft
106213214adfSAndy Whitcroft	my (@chunks);
106313214adfSAndy Whitcroft
1064cf655043SAndy Whitcroft	# Grab the first conditional/block pair.
106513214adfSAndy Whitcroft	($statement, $condition, $linenr, $remain, $off, $level) =
106613214adfSAndy Whitcroft				ctx_statement_block($linenr, $remain, $off);
1067773647a0SAndy Whitcroft	#print "F: c<$condition> s<$statement> remain<$remain>\n";
106813214adfSAndy Whitcroft	push(@chunks, [ $condition, $statement ]);
1069cf655043SAndy Whitcroft	if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
1070cf655043SAndy Whitcroft		return ($level, $linenr, @chunks);
1071cf655043SAndy Whitcroft	}
1072cf655043SAndy Whitcroft
1073cf655043SAndy Whitcroft	# Pull in the following conditional/block pairs and see if they
1074cf655043SAndy Whitcroft	# could continue the statement.
1075cf655043SAndy Whitcroft	for (;;) {
107613214adfSAndy Whitcroft		($statement, $condition, $linenr, $remain, $off, $level) =
107713214adfSAndy Whitcroft				ctx_statement_block($linenr, $remain, $off);
1078cf655043SAndy Whitcroft		#print "C: c<$condition> s<$statement> remain<$remain>\n";
1079773647a0SAndy Whitcroft		last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
1080cf655043SAndy Whitcroft		#print "C: push\n";
1081cf655043SAndy Whitcroft		push(@chunks, [ $condition, $statement ]);
108213214adfSAndy Whitcroft	}
108313214adfSAndy Whitcroft
108413214adfSAndy Whitcroft	return ($level, $linenr, @chunks);
10858905a67cSAndy Whitcroft}
10868905a67cSAndy Whitcroft
10874a0df2efSAndy Whitcroftsub ctx_block_get {
1088f0a594c1SAndy Whitcroft	my ($linenr, $remain, $outer, $open, $close, $off) = @_;
10894a0df2efSAndy Whitcroft	my $line;
10904a0df2efSAndy Whitcroft	my $start = $linenr - 1;
10914a0df2efSAndy Whitcroft	my $blk = '';
10924a0df2efSAndy Whitcroft	my @o;
10934a0df2efSAndy Whitcroft	my @c;
10944a0df2efSAndy Whitcroft	my @res = ();
10954a0df2efSAndy Whitcroft
1096f0a594c1SAndy Whitcroft	my $level = 0;
10974635f4fbSAndy Whitcroft	my @stack = ($level);
109800df344fSAndy Whitcroft	for ($line = $start; $remain > 0; $line++) {
109900df344fSAndy Whitcroft		next if ($rawlines[$line] =~ /^-/);
110000df344fSAndy Whitcroft		$remain--;
110100df344fSAndy Whitcroft
110200df344fSAndy Whitcroft		$blk .= $rawlines[$line];
11034635f4fbSAndy Whitcroft
11044635f4fbSAndy Whitcroft		# Handle nested #if/#else.
110501464f30SAndy Whitcroft		if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
11064635f4fbSAndy Whitcroft			push(@stack, $level);
110701464f30SAndy Whitcroft		} elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
11084635f4fbSAndy Whitcroft			$level = $stack[$#stack - 1];
110901464f30SAndy Whitcroft		} elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
11104635f4fbSAndy Whitcroft			$level = pop(@stack);
11114635f4fbSAndy Whitcroft		}
11124635f4fbSAndy Whitcroft
111301464f30SAndy Whitcroft		foreach my $c (split(//, $lines[$line])) {
1114f0a594c1SAndy Whitcroft			##print "C<$c>L<$level><$open$close>O<$off>\n";
1115f0a594c1SAndy Whitcroft			if ($off > 0) {
1116f0a594c1SAndy Whitcroft				$off--;
1117f0a594c1SAndy Whitcroft				next;
1118f0a594c1SAndy Whitcroft			}
11194a0df2efSAndy Whitcroft
1120f0a594c1SAndy Whitcroft			if ($c eq $close && $level > 0) {
1121f0a594c1SAndy Whitcroft				$level--;
1122f0a594c1SAndy Whitcroft				last if ($level == 0);
1123f0a594c1SAndy Whitcroft			} elsif ($c eq $open) {
1124f0a594c1SAndy Whitcroft				$level++;
1125f0a594c1SAndy Whitcroft			}
1126f0a594c1SAndy Whitcroft		}
11274a0df2efSAndy Whitcroft
1128f0a594c1SAndy Whitcroft		if (!$outer || $level <= 1) {
112900df344fSAndy Whitcroft			push(@res, $rawlines[$line]);
11304a0df2efSAndy Whitcroft		}
11314a0df2efSAndy Whitcroft
1132f0a594c1SAndy Whitcroft		last if ($level == 0);
11334a0df2efSAndy Whitcroft	}
11344a0df2efSAndy Whitcroft
1135f0a594c1SAndy Whitcroft	return ($level, @res);
11364a0df2efSAndy Whitcroft}
11374a0df2efSAndy Whitcroftsub ctx_block_outer {
11384a0df2efSAndy Whitcroft	my ($linenr, $remain) = @_;
11394a0df2efSAndy Whitcroft
1140f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
1141f0a594c1SAndy Whitcroft	return @r;
11424a0df2efSAndy Whitcroft}
11434a0df2efSAndy Whitcroftsub ctx_block {
11444a0df2efSAndy Whitcroft	my ($linenr, $remain) = @_;
11454a0df2efSAndy Whitcroft
1146f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1147f0a594c1SAndy Whitcroft	return @r;
1148653d4876SAndy Whitcroft}
1149653d4876SAndy Whitcroftsub ctx_statement {
1150f0a594c1SAndy Whitcroft	my ($linenr, $remain, $off) = @_;
1151f0a594c1SAndy Whitcroft
1152f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1153f0a594c1SAndy Whitcroft	return @r;
1154f0a594c1SAndy Whitcroft}
1155f0a594c1SAndy Whitcroftsub ctx_block_level {
1156653d4876SAndy Whitcroft	my ($linenr, $remain) = @_;
1157653d4876SAndy Whitcroft
1158f0a594c1SAndy Whitcroft	return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
11594a0df2efSAndy Whitcroft}
11609c0ca6f9SAndy Whitcroftsub ctx_statement_level {
11619c0ca6f9SAndy Whitcroft	my ($linenr, $remain, $off) = @_;
11629c0ca6f9SAndy Whitcroft
11639c0ca6f9SAndy Whitcroft	return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
11649c0ca6f9SAndy Whitcroft}
11654a0df2efSAndy Whitcroft
11664a0df2efSAndy Whitcroftsub ctx_locate_comment {
11674a0df2efSAndy Whitcroft	my ($first_line, $end_line) = @_;
11684a0df2efSAndy Whitcroft
11694a0df2efSAndy Whitcroft	# Catch a comment on the end of the line itself.
1170beae6332SAndy Whitcroft	my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
11714a0df2efSAndy Whitcroft	return $current_comment if (defined $current_comment);
11724a0df2efSAndy Whitcroft
11734a0df2efSAndy Whitcroft	# Look through the context and try and figure out if there is a
11744a0df2efSAndy Whitcroft	# comment.
11754a0df2efSAndy Whitcroft	my $in_comment = 0;
11764a0df2efSAndy Whitcroft	$current_comment = '';
11774a0df2efSAndy Whitcroft	for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
117800df344fSAndy Whitcroft		my $line = $rawlines[$linenr - 1];
117900df344fSAndy Whitcroft		#warn "           $line\n";
11804a0df2efSAndy Whitcroft		if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
11814a0df2efSAndy Whitcroft			$in_comment = 1;
11824a0df2efSAndy Whitcroft		}
11834a0df2efSAndy Whitcroft		if ($line =~ m@/\*@) {
11844a0df2efSAndy Whitcroft			$in_comment = 1;
11854a0df2efSAndy Whitcroft		}
11864a0df2efSAndy Whitcroft		if (!$in_comment && $current_comment ne '') {
11874a0df2efSAndy Whitcroft			$current_comment = '';
11884a0df2efSAndy Whitcroft		}
11894a0df2efSAndy Whitcroft		$current_comment .= $line . "\n" if ($in_comment);
11904a0df2efSAndy Whitcroft		if ($line =~ m@\*/@) {
11914a0df2efSAndy Whitcroft			$in_comment = 0;
11924a0df2efSAndy Whitcroft		}
11934a0df2efSAndy Whitcroft	}
11944a0df2efSAndy Whitcroft
11954a0df2efSAndy Whitcroft	chomp($current_comment);
11964a0df2efSAndy Whitcroft	return($current_comment);
11974a0df2efSAndy Whitcroft}
11984a0df2efSAndy Whitcroftsub ctx_has_comment {
11994a0df2efSAndy Whitcroft	my ($first_line, $end_line) = @_;
12004a0df2efSAndy Whitcroft	my $cmt = ctx_locate_comment($first_line, $end_line);
12014a0df2efSAndy Whitcroft
120200df344fSAndy Whitcroft	##print "LINE: $rawlines[$end_line - 1 ]\n";
12034a0df2efSAndy Whitcroft	##print "CMMT: $cmt\n";
12044a0df2efSAndy Whitcroft
12054a0df2efSAndy Whitcroft	return ($cmt ne '');
12064a0df2efSAndy Whitcroft}
12074a0df2efSAndy Whitcroft
12084d001e4dSAndy Whitcroftsub raw_line {
12094d001e4dSAndy Whitcroft	my ($linenr, $cnt) = @_;
12104d001e4dSAndy Whitcroft
12114d001e4dSAndy Whitcroft	my $offset = $linenr - 1;
12124d001e4dSAndy Whitcroft	$cnt++;
12134d001e4dSAndy Whitcroft
12144d001e4dSAndy Whitcroft	my $line;
12154d001e4dSAndy Whitcroft	while ($cnt) {
12164d001e4dSAndy Whitcroft		$line = $rawlines[$offset++];
12174d001e4dSAndy Whitcroft		next if (defined($line) && $line =~ /^-/);
12184d001e4dSAndy Whitcroft		$cnt--;
12194d001e4dSAndy Whitcroft	}
12204d001e4dSAndy Whitcroft
12214d001e4dSAndy Whitcroft	return $line;
12224d001e4dSAndy Whitcroft}
12234d001e4dSAndy Whitcroft
12240a920b5bSAndy Whitcroftsub cat_vet {
12250a920b5bSAndy Whitcroft	my ($vet) = @_;
12269c0ca6f9SAndy Whitcroft	my ($res, $coded);
12270a920b5bSAndy Whitcroft
12289c0ca6f9SAndy Whitcroft	$res = '';
12296c72ffaaSAndy Whitcroft	while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
12306c72ffaaSAndy Whitcroft		$res .= $1;
12316c72ffaaSAndy Whitcroft		if ($2 ne '') {
12329c0ca6f9SAndy Whitcroft			$coded = sprintf("^%c", unpack('C', $2) + 64);
12336c72ffaaSAndy Whitcroft			$res .= $coded;
12346c72ffaaSAndy Whitcroft		}
12359c0ca6f9SAndy Whitcroft	}
12369c0ca6f9SAndy Whitcroft	$res =~ s/$/\$/;
12370a920b5bSAndy Whitcroft
12389c0ca6f9SAndy Whitcroft	return $res;
12390a920b5bSAndy Whitcroft}
12400a920b5bSAndy Whitcroft
1241c2fdda0dSAndy Whitcroftmy $av_preprocessor = 0;
1242cf655043SAndy Whitcroftmy $av_pending;
1243c2fdda0dSAndy Whitcroftmy @av_paren_type;
12441f65f947SAndy Whitcroftmy $av_pend_colon;
1245c2fdda0dSAndy Whitcroft
1246c2fdda0dSAndy Whitcroftsub annotate_reset {
1247c2fdda0dSAndy Whitcroft	$av_preprocessor = 0;
1248cf655043SAndy Whitcroft	$av_pending = '_';
1249cf655043SAndy Whitcroft	@av_paren_type = ('E');
12501f65f947SAndy Whitcroft	$av_pend_colon = 'O';
1251c2fdda0dSAndy Whitcroft}
1252c2fdda0dSAndy Whitcroft
12536c72ffaaSAndy Whitcroftsub annotate_values {
12546c72ffaaSAndy Whitcroft	my ($stream, $type) = @_;
12556c72ffaaSAndy Whitcroft
12566c72ffaaSAndy Whitcroft	my $res;
12571f65f947SAndy Whitcroft	my $var = '_' x length($stream);
12586c72ffaaSAndy Whitcroft	my $cur = $stream;
12596c72ffaaSAndy Whitcroft
1260c2fdda0dSAndy Whitcroft	print "$stream\n" if ($dbg_values > 1);
12616c72ffaaSAndy Whitcroft
12626c72ffaaSAndy Whitcroft	while (length($cur)) {
1263773647a0SAndy Whitcroft		@av_paren_type = ('E') if ($#av_paren_type < 0);
1264cf655043SAndy Whitcroft		print " <" . join('', @av_paren_type) .
1265171ae1a4SAndy Whitcroft				"> <$type> <$av_pending>" if ($dbg_values > 1);
12666c72ffaaSAndy Whitcroft		if ($cur =~ /^(\s+)/o) {
1267c2fdda0dSAndy Whitcroft			print "WS($1)\n" if ($dbg_values > 1);
1268c2fdda0dSAndy Whitcroft			if ($1 =~ /\n/ && $av_preprocessor) {
1269cf655043SAndy Whitcroft				$type = pop(@av_paren_type);
1270c2fdda0dSAndy Whitcroft				$av_preprocessor = 0;
12716c72ffaaSAndy Whitcroft			}
12726c72ffaaSAndy Whitcroft
1273c023e473SFlorian Mickler		} elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
12749446ef56SAndy Whitcroft			print "CAST($1)\n" if ($dbg_values > 1);
12759446ef56SAndy Whitcroft			push(@av_paren_type, $type);
1276addcdceaSAndy Whitcroft			$type = 'c';
12779446ef56SAndy Whitcroft
1278e91b6e26SAndy Whitcroft		} elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
1279c2fdda0dSAndy Whitcroft			print "DECLARE($1)\n" if ($dbg_values > 1);
12806c72ffaaSAndy Whitcroft			$type = 'T';
12816c72ffaaSAndy Whitcroft
1282389a2fe5SAndy Whitcroft		} elsif ($cur =~ /^($Modifier)\s*/) {
1283389a2fe5SAndy Whitcroft			print "MODIFIER($1)\n" if ($dbg_values > 1);
1284389a2fe5SAndy Whitcroft			$type = 'T';
1285389a2fe5SAndy Whitcroft
1286c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
1287171ae1a4SAndy Whitcroft			print "DEFINE($1,$2)\n" if ($dbg_values > 1);
1288c2fdda0dSAndy Whitcroft			$av_preprocessor = 1;
1289171ae1a4SAndy Whitcroft			push(@av_paren_type, $type);
1290171ae1a4SAndy Whitcroft			if ($2 ne '') {
1291cf655043SAndy Whitcroft				$av_pending = 'N';
1292171ae1a4SAndy Whitcroft			}
1293171ae1a4SAndy Whitcroft			$type = 'E';
1294171ae1a4SAndy Whitcroft
1295c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
1296171ae1a4SAndy Whitcroft			print "UNDEF($1)\n" if ($dbg_values > 1);
1297171ae1a4SAndy Whitcroft			$av_preprocessor = 1;
1298171ae1a4SAndy Whitcroft			push(@av_paren_type, $type);
12996c72ffaaSAndy Whitcroft
1300c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
1301cf655043SAndy Whitcroft			print "PRE_START($1)\n" if ($dbg_values > 1);
1302c2fdda0dSAndy Whitcroft			$av_preprocessor = 1;
1303cf655043SAndy Whitcroft
1304cf655043SAndy Whitcroft			push(@av_paren_type, $type);
1305cf655043SAndy Whitcroft			push(@av_paren_type, $type);
1306171ae1a4SAndy Whitcroft			$type = 'E';
1307cf655043SAndy Whitcroft
1308c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
1309cf655043SAndy Whitcroft			print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1310cf655043SAndy Whitcroft			$av_preprocessor = 1;
1311cf655043SAndy Whitcroft
1312cf655043SAndy Whitcroft			push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1313cf655043SAndy Whitcroft
1314171ae1a4SAndy Whitcroft			$type = 'E';
1315cf655043SAndy Whitcroft
1316c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:endif))/o) {
1317cf655043SAndy Whitcroft			print "PRE_END($1)\n" if ($dbg_values > 1);
1318cf655043SAndy Whitcroft
1319cf655043SAndy Whitcroft			$av_preprocessor = 1;
1320cf655043SAndy Whitcroft
1321cf655043SAndy Whitcroft			# Assume all arms of the conditional end as this
1322cf655043SAndy Whitcroft			# one does, and continue as if the #endif was not here.
1323cf655043SAndy Whitcroft			pop(@av_paren_type);
1324cf655043SAndy Whitcroft			push(@av_paren_type, $type);
1325171ae1a4SAndy Whitcroft			$type = 'E';
13266c72ffaaSAndy Whitcroft
13276c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\\\n)/o) {
1328c2fdda0dSAndy Whitcroft			print "PRECONT($1)\n" if ($dbg_values > 1);
13296c72ffaaSAndy Whitcroft
1330171ae1a4SAndy Whitcroft		} elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1331171ae1a4SAndy Whitcroft			print "ATTR($1)\n" if ($dbg_values > 1);
1332171ae1a4SAndy Whitcroft			$av_pending = $type;
1333171ae1a4SAndy Whitcroft			$type = 'N';
1334171ae1a4SAndy Whitcroft
13356c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
1336c2fdda0dSAndy Whitcroft			print "SIZEOF($1)\n" if ($dbg_values > 1);
13376c72ffaaSAndy Whitcroft			if (defined $2) {
1338cf655043SAndy Whitcroft				$av_pending = 'V';
13396c72ffaaSAndy Whitcroft			}
13406c72ffaaSAndy Whitcroft			$type = 'N';
13416c72ffaaSAndy Whitcroft
134214b111c1SAndy Whitcroft		} elsif ($cur =~ /^(if|while|for)\b/o) {
1343c2fdda0dSAndy Whitcroft			print "COND($1)\n" if ($dbg_values > 1);
134414b111c1SAndy Whitcroft			$av_pending = 'E';
13456c72ffaaSAndy Whitcroft			$type = 'N';
13466c72ffaaSAndy Whitcroft
13471f65f947SAndy Whitcroft		} elsif ($cur =~/^(case)/o) {
13481f65f947SAndy Whitcroft			print "CASE($1)\n" if ($dbg_values > 1);
13491f65f947SAndy Whitcroft			$av_pend_colon = 'C';
13501f65f947SAndy Whitcroft			$type = 'N';
13511f65f947SAndy Whitcroft
135214b111c1SAndy Whitcroft		} elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
1353c2fdda0dSAndy Whitcroft			print "KEYWORD($1)\n" if ($dbg_values > 1);
13546c72ffaaSAndy Whitcroft			$type = 'N';
13556c72ffaaSAndy Whitcroft
13566c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\()/o) {
1357c2fdda0dSAndy Whitcroft			print "PAREN('$1')\n" if ($dbg_values > 1);
1358cf655043SAndy Whitcroft			push(@av_paren_type, $av_pending);
1359cf655043SAndy Whitcroft			$av_pending = '_';
13606c72ffaaSAndy Whitcroft			$type = 'N';
13616c72ffaaSAndy Whitcroft
13626c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\))/o) {
1363cf655043SAndy Whitcroft			my $new_type = pop(@av_paren_type);
1364cf655043SAndy Whitcroft			if ($new_type ne '_') {
1365cf655043SAndy Whitcroft				$type = $new_type;
1366c2fdda0dSAndy Whitcroft				print "PAREN('$1') -> $type\n"
1367c2fdda0dSAndy Whitcroft							if ($dbg_values > 1);
13686c72ffaaSAndy Whitcroft			} else {
1369c2fdda0dSAndy Whitcroft				print "PAREN('$1')\n" if ($dbg_values > 1);
13706c72ffaaSAndy Whitcroft			}
13716c72ffaaSAndy Whitcroft
1372c8cb2ca3SAndy Whitcroft		} elsif ($cur =~ /^($Ident)\s*\(/o) {
1373c2fdda0dSAndy Whitcroft			print "FUNC($1)\n" if ($dbg_values > 1);
1374c8cb2ca3SAndy Whitcroft			$type = 'V';
1375cf655043SAndy Whitcroft			$av_pending = 'V';
13766c72ffaaSAndy Whitcroft
13778e761b04SAndy Whitcroft		} elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
13788e761b04SAndy Whitcroft			if (defined $2 && $type eq 'C' || $type eq 'T') {
13791f65f947SAndy Whitcroft				$av_pend_colon = 'B';
13808e761b04SAndy Whitcroft			} elsif ($type eq 'E') {
13818e761b04SAndy Whitcroft				$av_pend_colon = 'L';
13821f65f947SAndy Whitcroft			}
13831f65f947SAndy Whitcroft			print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
13841f65f947SAndy Whitcroft			$type = 'V';
13851f65f947SAndy Whitcroft
13866c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Ident|$Constant)/o) {
1387c2fdda0dSAndy Whitcroft			print "IDENT($1)\n" if ($dbg_values > 1);
13886c72ffaaSAndy Whitcroft			$type = 'V';
13896c72ffaaSAndy Whitcroft
13906c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Assignment)/o) {
1391c2fdda0dSAndy Whitcroft			print "ASSIGN($1)\n" if ($dbg_values > 1);
13926c72ffaaSAndy Whitcroft			$type = 'N';
13936c72ffaaSAndy Whitcroft
1394cf655043SAndy Whitcroft		} elsif ($cur =~/^(;|{|})/) {
1395c2fdda0dSAndy Whitcroft			print "END($1)\n" if ($dbg_values > 1);
139613214adfSAndy Whitcroft			$type = 'E';
13971f65f947SAndy Whitcroft			$av_pend_colon = 'O';
139813214adfSAndy Whitcroft
13998e761b04SAndy Whitcroft		} elsif ($cur =~/^(,)/) {
14008e761b04SAndy Whitcroft			print "COMMA($1)\n" if ($dbg_values > 1);
14018e761b04SAndy Whitcroft			$type = 'C';
14028e761b04SAndy Whitcroft
14031f65f947SAndy Whitcroft		} elsif ($cur =~ /^(\?)/o) {
14041f65f947SAndy Whitcroft			print "QUESTION($1)\n" if ($dbg_values > 1);
14051f65f947SAndy Whitcroft			$type = 'N';
14061f65f947SAndy Whitcroft
14071f65f947SAndy Whitcroft		} elsif ($cur =~ /^(:)/o) {
14081f65f947SAndy Whitcroft			print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
14091f65f947SAndy Whitcroft
14101f65f947SAndy Whitcroft			substr($var, length($res), 1, $av_pend_colon);
14111f65f947SAndy Whitcroft			if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
14121f65f947SAndy Whitcroft				$type = 'E';
14131f65f947SAndy Whitcroft			} else {
14141f65f947SAndy Whitcroft				$type = 'N';
14151f65f947SAndy Whitcroft			}
14161f65f947SAndy Whitcroft			$av_pend_colon = 'O';
14171f65f947SAndy Whitcroft
14188e761b04SAndy Whitcroft		} elsif ($cur =~ /^(\[)/o) {
141913214adfSAndy Whitcroft			print "CLOSE($1)\n" if ($dbg_values > 1);
14206c72ffaaSAndy Whitcroft			$type = 'N';
14216c72ffaaSAndy Whitcroft
14220d413866SAndy Whitcroft		} elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
142374048ed8SAndy Whitcroft			my $variant;
142474048ed8SAndy Whitcroft
142574048ed8SAndy Whitcroft			print "OPV($1)\n" if ($dbg_values > 1);
142674048ed8SAndy Whitcroft			if ($type eq 'V') {
142774048ed8SAndy Whitcroft				$variant = 'B';
142874048ed8SAndy Whitcroft			} else {
142974048ed8SAndy Whitcroft				$variant = 'U';
143074048ed8SAndy Whitcroft			}
143174048ed8SAndy Whitcroft
143274048ed8SAndy Whitcroft			substr($var, length($res), 1, $variant);
143374048ed8SAndy Whitcroft			$type = 'N';
143474048ed8SAndy Whitcroft
14356c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Operators)/o) {
1436c2fdda0dSAndy Whitcroft			print "OP($1)\n" if ($dbg_values > 1);
14376c72ffaaSAndy Whitcroft			if ($1 ne '++' && $1 ne '--') {
14386c72ffaaSAndy Whitcroft				$type = 'N';
14396c72ffaaSAndy Whitcroft			}
14406c72ffaaSAndy Whitcroft
14416c72ffaaSAndy Whitcroft		} elsif ($cur =~ /(^.)/o) {
1442c2fdda0dSAndy Whitcroft			print "C($1)\n" if ($dbg_values > 1);
14436c72ffaaSAndy Whitcroft		}
14446c72ffaaSAndy Whitcroft		if (defined $1) {
14456c72ffaaSAndy Whitcroft			$cur = substr($cur, length($1));
14466c72ffaaSAndy Whitcroft			$res .= $type x length($1);
14476c72ffaaSAndy Whitcroft		}
14486c72ffaaSAndy Whitcroft	}
14496c72ffaaSAndy Whitcroft
14501f65f947SAndy Whitcroft	return ($res, $var);
14516c72ffaaSAndy Whitcroft}
14526c72ffaaSAndy Whitcroft
14538905a67cSAndy Whitcroftsub possible {
145413214adfSAndy Whitcroft	my ($possible, $line) = @_;
14559a974fdbSAndy Whitcroft	my $notPermitted = qr{(?:
14560776e594SAndy Whitcroft		^(?:
14570776e594SAndy Whitcroft			$Modifier|
14580776e594SAndy Whitcroft			$Storage|
14590776e594SAndy Whitcroft			$Type|
14609a974fdbSAndy Whitcroft			DEFINE_\S+
14619a974fdbSAndy Whitcroft		)$|
14629a974fdbSAndy Whitcroft		^(?:
14630776e594SAndy Whitcroft			goto|
14640776e594SAndy Whitcroft			return|
14650776e594SAndy Whitcroft			case|
14660776e594SAndy Whitcroft			else|
14670776e594SAndy Whitcroft			asm|__asm__|
146889a88353SAndy Whitcroft			do|
146989a88353SAndy Whitcroft			\#|
147089a88353SAndy Whitcroft			\#\#|
14719a974fdbSAndy Whitcroft		)(?:\s|$)|
14720776e594SAndy Whitcroft		^(?:typedef|struct|enum)\b
14739a974fdbSAndy Whitcroft	    )}x;
14749a974fdbSAndy Whitcroft	warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
14759a974fdbSAndy Whitcroft	if ($possible !~ $notPermitted) {
1476c45dcabdSAndy Whitcroft		# Check for modifiers.
1477c45dcabdSAndy Whitcroft		$possible =~ s/\s*$Storage\s*//g;
1478c45dcabdSAndy Whitcroft		$possible =~ s/\s*$Sparse\s*//g;
1479c45dcabdSAndy Whitcroft		if ($possible =~ /^\s*$/) {
1480c45dcabdSAndy Whitcroft
1481c45dcabdSAndy Whitcroft		} elsif ($possible =~ /\s/) {
1482c45dcabdSAndy Whitcroft			$possible =~ s/\s*$Type\s*//g;
1483d2506586SAndy Whitcroft			for my $modifier (split(' ', $possible)) {
14849a974fdbSAndy Whitcroft				if ($modifier !~ $notPermitted) {
1485d2506586SAndy Whitcroft					warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1486d2506586SAndy Whitcroft					push(@modifierList, $modifier);
1487d2506586SAndy Whitcroft				}
14889a974fdbSAndy Whitcroft			}
1489c45dcabdSAndy Whitcroft
1490c45dcabdSAndy Whitcroft		} else {
149113214adfSAndy Whitcroft			warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
14928905a67cSAndy Whitcroft			push(@typeList, $possible);
1493c45dcabdSAndy Whitcroft		}
14948905a67cSAndy Whitcroft		build_types();
14950776e594SAndy Whitcroft	} else {
14960776e594SAndy Whitcroft		warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
14978905a67cSAndy Whitcroft	}
14988905a67cSAndy Whitcroft}
14998905a67cSAndy Whitcroft
15006c72ffaaSAndy Whitcroftmy $prefix = '';
15016c72ffaaSAndy Whitcroft
1502000d1cc1SJoe Perchessub show_type {
1503cbec18afSJoe Perches	my ($type) = @_;
150491bfe484SJoe Perches
1505cbec18afSJoe Perches	return defined $use_type{$type} if (scalar keys %use_type > 0);
1506cbec18afSJoe Perches
1507cbec18afSJoe Perches	return !defined $ignore_type{$type};
1508000d1cc1SJoe Perches}
1509000d1cc1SJoe Perches
1510f0a594c1SAndy Whitcroftsub report {
1511cbec18afSJoe Perches	my ($level, $type, $msg) = @_;
1512cbec18afSJoe Perches
1513cbec18afSJoe Perches	if (!show_type($type) ||
1514cbec18afSJoe Perches	    (defined $tst_only && $msg !~ /\Q$tst_only\E/)) {
1515773647a0SAndy Whitcroft		return 0;
1516773647a0SAndy Whitcroft	}
1517000d1cc1SJoe Perches	my $line;
1518000d1cc1SJoe Perches	if ($show_types) {
1519cbec18afSJoe Perches		$line = "$prefix$level:$type: $msg\n";
1520000d1cc1SJoe Perches	} else {
1521cbec18afSJoe Perches		$line = "$prefix$level: $msg\n";
1522000d1cc1SJoe Perches	}
15238905a67cSAndy Whitcroft	$line = (split('\n', $line))[0] . "\n" if ($terse);
15248905a67cSAndy Whitcroft
152513214adfSAndy Whitcroft	push(our @report, $line);
1526773647a0SAndy Whitcroft
1527773647a0SAndy Whitcroft	return 1;
1528f0a594c1SAndy Whitcroft}
1529cbec18afSJoe Perches
1530f0a594c1SAndy Whitcroftsub report_dump {
153113214adfSAndy Whitcroft	our @report;
1532f0a594c1SAndy Whitcroft}
1533000d1cc1SJoe Perches
1534d752fcc8SJoe Perchessub fixup_current_range {
1535d752fcc8SJoe Perches	my ($lineRef, $offset, $length) = @_;
1536d752fcc8SJoe Perches
1537d752fcc8SJoe Perches	if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) {
1538d752fcc8SJoe Perches		my $o = $1;
1539d752fcc8SJoe Perches		my $l = $2;
1540d752fcc8SJoe Perches		my $no = $o + $offset;
1541d752fcc8SJoe Perches		my $nl = $l + $length;
1542d752fcc8SJoe Perches		$$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/;
1543d752fcc8SJoe Perches	}
1544d752fcc8SJoe Perches}
1545d752fcc8SJoe Perches
1546d752fcc8SJoe Perchessub fix_inserted_deleted_lines {
1547d752fcc8SJoe Perches	my ($linesRef, $insertedRef, $deletedRef) = @_;
1548d752fcc8SJoe Perches
1549d752fcc8SJoe Perches	my $range_last_linenr = 0;
1550d752fcc8SJoe Perches	my $delta_offset = 0;
1551d752fcc8SJoe Perches
1552d752fcc8SJoe Perches	my $old_linenr = 0;
1553d752fcc8SJoe Perches	my $new_linenr = 0;
1554d752fcc8SJoe Perches
1555d752fcc8SJoe Perches	my $next_insert = 0;
1556d752fcc8SJoe Perches	my $next_delete = 0;
1557d752fcc8SJoe Perches
1558d752fcc8SJoe Perches	my @lines = ();
1559d752fcc8SJoe Perches
1560d752fcc8SJoe Perches	my $inserted = @{$insertedRef}[$next_insert++];
1561d752fcc8SJoe Perches	my $deleted = @{$deletedRef}[$next_delete++];
1562d752fcc8SJoe Perches
1563d752fcc8SJoe Perches	foreach my $old_line (@{$linesRef}) {
1564d752fcc8SJoe Perches		my $save_line = 1;
1565d752fcc8SJoe Perches		my $line = $old_line;	#don't modify the array
1566d752fcc8SJoe Perches		if ($line =~ /^(?:\+\+\+\|\-\-\-)\s+\S+/) {	#new filename
1567d752fcc8SJoe Perches			$delta_offset = 0;
1568d752fcc8SJoe Perches		} elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) {	#new hunk
1569d752fcc8SJoe Perches			$range_last_linenr = $new_linenr;
1570d752fcc8SJoe Perches			fixup_current_range(\$line, $delta_offset, 0);
1571d752fcc8SJoe Perches		}
1572d752fcc8SJoe Perches
1573d752fcc8SJoe Perches		while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) {
1574d752fcc8SJoe Perches			$deleted = @{$deletedRef}[$next_delete++];
1575d752fcc8SJoe Perches			$save_line = 0;
1576d752fcc8SJoe Perches			fixup_current_range(\$lines[$range_last_linenr], $delta_offset--, -1);
1577d752fcc8SJoe Perches		}
1578d752fcc8SJoe Perches
1579d752fcc8SJoe Perches		while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) {
1580d752fcc8SJoe Perches			push(@lines, ${$inserted}{'LINE'});
1581d752fcc8SJoe Perches			$inserted = @{$insertedRef}[$next_insert++];
1582d752fcc8SJoe Perches			$new_linenr++;
1583d752fcc8SJoe Perches			fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1);
1584d752fcc8SJoe Perches		}
1585d752fcc8SJoe Perches
1586d752fcc8SJoe Perches		if ($save_line) {
1587d752fcc8SJoe Perches			push(@lines, $line);
1588d752fcc8SJoe Perches			$new_linenr++;
1589d752fcc8SJoe Perches		}
1590d752fcc8SJoe Perches
1591d752fcc8SJoe Perches		$old_linenr++;
1592d752fcc8SJoe Perches	}
1593d752fcc8SJoe Perches
1594d752fcc8SJoe Perches	return @lines;
1595d752fcc8SJoe Perches}
1596d752fcc8SJoe Perches
1597f2d7e4d4SJoe Perchessub fix_insert_line {
1598f2d7e4d4SJoe Perches	my ($linenr, $line) = @_;
1599f2d7e4d4SJoe Perches
1600f2d7e4d4SJoe Perches	my $inserted = {
1601f2d7e4d4SJoe Perches		LINENR => $linenr,
1602f2d7e4d4SJoe Perches		LINE => $line,
1603f2d7e4d4SJoe Perches	};
1604f2d7e4d4SJoe Perches	push(@fixed_inserted, $inserted);
1605f2d7e4d4SJoe Perches}
1606f2d7e4d4SJoe Perches
1607f2d7e4d4SJoe Perchessub fix_delete_line {
1608f2d7e4d4SJoe Perches	my ($linenr, $line) = @_;
1609f2d7e4d4SJoe Perches
1610f2d7e4d4SJoe Perches	my $deleted = {
1611f2d7e4d4SJoe Perches		LINENR => $linenr,
1612f2d7e4d4SJoe Perches		LINE => $line,
1613f2d7e4d4SJoe Perches	};
1614f2d7e4d4SJoe Perches
1615f2d7e4d4SJoe Perches	push(@fixed_deleted, $deleted);
1616f2d7e4d4SJoe Perches}
1617f2d7e4d4SJoe Perches
1618de7d4f0eSAndy Whitcroftsub ERROR {
1619cbec18afSJoe Perches	my ($type, $msg) = @_;
1620cbec18afSJoe Perches
1621cbec18afSJoe Perches	if (report("ERROR", $type, $msg)) {
1622de7d4f0eSAndy Whitcroft		our $clean = 0;
16236c72ffaaSAndy Whitcroft		our $cnt_error++;
16243705ce5bSJoe Perches		return 1;
1625de7d4f0eSAndy Whitcroft	}
16263705ce5bSJoe Perches	return 0;
1627773647a0SAndy Whitcroft}
1628de7d4f0eSAndy Whitcroftsub WARN {
1629cbec18afSJoe Perches	my ($type, $msg) = @_;
1630cbec18afSJoe Perches
1631cbec18afSJoe Perches	if (report("WARNING", $type, $msg)) {
1632de7d4f0eSAndy Whitcroft		our $clean = 0;
16336c72ffaaSAndy Whitcroft		our $cnt_warn++;
16343705ce5bSJoe Perches		return 1;
1635de7d4f0eSAndy Whitcroft	}
16363705ce5bSJoe Perches	return 0;
1637773647a0SAndy Whitcroft}
1638de7d4f0eSAndy Whitcroftsub CHK {
1639cbec18afSJoe Perches	my ($type, $msg) = @_;
1640cbec18afSJoe Perches
1641cbec18afSJoe Perches	if ($check && report("CHECK", $type, $msg)) {
1642de7d4f0eSAndy Whitcroft		our $clean = 0;
16436c72ffaaSAndy Whitcroft		our $cnt_chk++;
16443705ce5bSJoe Perches		return 1;
16456c72ffaaSAndy Whitcroft	}
16463705ce5bSJoe Perches	return 0;
1647de7d4f0eSAndy Whitcroft}
1648de7d4f0eSAndy Whitcroft
16496ecd9674SAndy Whitcroftsub check_absolute_file {
16506ecd9674SAndy Whitcroft	my ($absolute, $herecurr) = @_;
16516ecd9674SAndy Whitcroft	my $file = $absolute;
16526ecd9674SAndy Whitcroft
16536ecd9674SAndy Whitcroft	##print "absolute<$absolute>\n";
16546ecd9674SAndy Whitcroft
16556ecd9674SAndy Whitcroft	# See if any suffix of this path is a path within the tree.
16566ecd9674SAndy Whitcroft	while ($file =~ s@^[^/]*/@@) {
16576ecd9674SAndy Whitcroft		if (-f "$root/$file") {
16586ecd9674SAndy Whitcroft			##print "file<$file>\n";
16596ecd9674SAndy Whitcroft			last;
16606ecd9674SAndy Whitcroft		}
16616ecd9674SAndy Whitcroft	}
16626ecd9674SAndy Whitcroft	if (! -f _)  {
16636ecd9674SAndy Whitcroft		return 0;
16646ecd9674SAndy Whitcroft	}
16656ecd9674SAndy Whitcroft
16666ecd9674SAndy Whitcroft	# It is, so see if the prefix is acceptable.
16676ecd9674SAndy Whitcroft	my $prefix = $absolute;
16686ecd9674SAndy Whitcroft	substr($prefix, -length($file)) = '';
16696ecd9674SAndy Whitcroft
16706ecd9674SAndy Whitcroft	##print "prefix<$prefix>\n";
16716ecd9674SAndy Whitcroft	if ($prefix ne ".../") {
1672000d1cc1SJoe Perches		WARN("USE_RELATIVE_PATH",
1673000d1cc1SJoe Perches		     "use relative pathname instead of absolute in changelog text\n" . $herecurr);
16746ecd9674SAndy Whitcroft	}
16756ecd9674SAndy Whitcroft}
16766ecd9674SAndy Whitcroft
16773705ce5bSJoe Perchessub trim {
16783705ce5bSJoe Perches	my ($string) = @_;
16793705ce5bSJoe Perches
1680b34c648bSJoe Perches	$string =~ s/^\s+|\s+$//g;
1681b34c648bSJoe Perches
1682b34c648bSJoe Perches	return $string;
1683b34c648bSJoe Perches}
1684b34c648bSJoe Perches
1685b34c648bSJoe Perchessub ltrim {
1686b34c648bSJoe Perches	my ($string) = @_;
1687b34c648bSJoe Perches
1688b34c648bSJoe Perches	$string =~ s/^\s+//;
1689b34c648bSJoe Perches
1690b34c648bSJoe Perches	return $string;
1691b34c648bSJoe Perches}
1692b34c648bSJoe Perches
1693b34c648bSJoe Perchessub rtrim {
1694b34c648bSJoe Perches	my ($string) = @_;
1695b34c648bSJoe Perches
1696b34c648bSJoe Perches	$string =~ s/\s+$//;
16973705ce5bSJoe Perches
16983705ce5bSJoe Perches	return $string;
16993705ce5bSJoe Perches}
17003705ce5bSJoe Perches
170152ea8506SJoe Perchessub string_find_replace {
170252ea8506SJoe Perches	my ($string, $find, $replace) = @_;
170352ea8506SJoe Perches
170452ea8506SJoe Perches	$string =~ s/$find/$replace/g;
170552ea8506SJoe Perches
170652ea8506SJoe Perches	return $string;
170752ea8506SJoe Perches}
170852ea8506SJoe Perches
17093705ce5bSJoe Perchessub tabify {
17103705ce5bSJoe Perches	my ($leading) = @_;
17113705ce5bSJoe Perches
17123705ce5bSJoe Perches	my $source_indent = 8;
17133705ce5bSJoe Perches	my $max_spaces_before_tab = $source_indent - 1;
17143705ce5bSJoe Perches	my $spaces_to_tab = " " x $source_indent;
17153705ce5bSJoe Perches
17163705ce5bSJoe Perches	#convert leading spaces to tabs
17173705ce5bSJoe Perches	1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
17183705ce5bSJoe Perches	#Remove spaces before a tab
17193705ce5bSJoe Perches	1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
17203705ce5bSJoe Perches
17213705ce5bSJoe Perches	return "$leading";
17223705ce5bSJoe Perches}
17233705ce5bSJoe Perches
1724d1fe9c09SJoe Perchessub pos_last_openparen {
1725d1fe9c09SJoe Perches	my ($line) = @_;
1726d1fe9c09SJoe Perches
1727d1fe9c09SJoe Perches	my $pos = 0;
1728d1fe9c09SJoe Perches
1729d1fe9c09SJoe Perches	my $opens = $line =~ tr/\(/\(/;
1730d1fe9c09SJoe Perches	my $closes = $line =~ tr/\)/\)/;
1731d1fe9c09SJoe Perches
1732d1fe9c09SJoe Perches	my $last_openparen = 0;
1733d1fe9c09SJoe Perches
1734d1fe9c09SJoe Perches	if (($opens == 0) || ($closes >= $opens)) {
1735d1fe9c09SJoe Perches		return -1;
1736d1fe9c09SJoe Perches	}
1737d1fe9c09SJoe Perches
1738d1fe9c09SJoe Perches	my $len = length($line);
1739d1fe9c09SJoe Perches
1740d1fe9c09SJoe Perches	for ($pos = 0; $pos < $len; $pos++) {
1741d1fe9c09SJoe Perches		my $string = substr($line, $pos);
1742d1fe9c09SJoe Perches		if ($string =~ /^($FuncArg|$balanced_parens)/) {
1743d1fe9c09SJoe Perches			$pos += length($1) - 1;
1744d1fe9c09SJoe Perches		} elsif (substr($line, $pos, 1) eq '(') {
1745d1fe9c09SJoe Perches			$last_openparen = $pos;
1746d1fe9c09SJoe Perches		} elsif (index($string, '(') == -1) {
1747d1fe9c09SJoe Perches			last;
1748d1fe9c09SJoe Perches		}
1749d1fe9c09SJoe Perches	}
1750d1fe9c09SJoe Perches
175191cb5195SJoe Perches	return length(expand_tabs(substr($line, 0, $last_openparen))) + 1;
1752d1fe9c09SJoe Perches}
1753d1fe9c09SJoe Perches
17540a920b5bSAndy Whitcroftsub process {
17550a920b5bSAndy Whitcroft	my $filename = shift;
17560a920b5bSAndy Whitcroft
17570a920b5bSAndy Whitcroft	my $linenr=0;
17580a920b5bSAndy Whitcroft	my $prevline="";
1759c2fdda0dSAndy Whitcroft	my $prevrawline="";
17600a920b5bSAndy Whitcroft	my $stashline="";
1761c2fdda0dSAndy Whitcroft	my $stashrawline="";
17620a920b5bSAndy Whitcroft
17634a0df2efSAndy Whitcroft	my $length;
17640a920b5bSAndy Whitcroft	my $indent;
17650a920b5bSAndy Whitcroft	my $previndent=0;
17660a920b5bSAndy Whitcroft	my $stashindent=0;
17670a920b5bSAndy Whitcroft
1768de7d4f0eSAndy Whitcroft	our $clean = 1;
17690a920b5bSAndy Whitcroft	my $signoff = 0;
17700a920b5bSAndy Whitcroft	my $is_patch = 0;
17710a920b5bSAndy Whitcroft
177229ee1b0cSJoe Perches	my $in_header_lines = $file ? 0 : 1;
177315662b3eSJoe Perches	my $in_commit_log = 0;		#Scanning lines before patch
177413f1937eSJoe Perches	my $reported_maintainer_file = 0;
1775fa64205dSPasi Savanainen	my $non_utf8_charset = 0;
1776fa64205dSPasi Savanainen
1777365dd4eaSJoe Perches	my $last_blank_line = 0;
1778365dd4eaSJoe Perches
177913214adfSAndy Whitcroft	our @report = ();
17806c72ffaaSAndy Whitcroft	our $cnt_lines = 0;
17816c72ffaaSAndy Whitcroft	our $cnt_error = 0;
17826c72ffaaSAndy Whitcroft	our $cnt_warn = 0;
17836c72ffaaSAndy Whitcroft	our $cnt_chk = 0;
17846c72ffaaSAndy Whitcroft
17850a920b5bSAndy Whitcroft	# Trace the real file/line as we go.
17860a920b5bSAndy Whitcroft	my $realfile = '';
17870a920b5bSAndy Whitcroft	my $realline = 0;
17880a920b5bSAndy Whitcroft	my $realcnt = 0;
17890a920b5bSAndy Whitcroft	my $here = '';
17900a920b5bSAndy Whitcroft	my $in_comment = 0;
1791c2fdda0dSAndy Whitcroft	my $comment_edge = 0;
17920a920b5bSAndy Whitcroft	my $first_line = 0;
17931e855726SWolfram Sang	my $p1_prefix = '';
17940a920b5bSAndy Whitcroft
179513214adfSAndy Whitcroft	my $prev_values = 'E';
179613214adfSAndy Whitcroft
179713214adfSAndy Whitcroft	# suppression flags
1798773647a0SAndy Whitcroft	my %suppress_ifbraces;
1799170d3a22SAndy Whitcroft	my %suppress_whiletrailers;
18002b474a1aSAndy Whitcroft	my %suppress_export;
18013e469cdcSAndy Whitcroft	my $suppress_statement = 0;
1802653d4876SAndy Whitcroft
18037e51f197SJoe Perches	my %signatures = ();
1804323c1260SJoe Perches
1805c2fdda0dSAndy Whitcroft	# Pre-scan the patch sanitizing the lines.
1806de7d4f0eSAndy Whitcroft	# Pre-scan the patch looking for any __setup documentation.
1807c2fdda0dSAndy Whitcroft	#
1808de7d4f0eSAndy Whitcroft	my @setup_docs = ();
1809de7d4f0eSAndy Whitcroft	my $setup_docs = 0;
1810773647a0SAndy Whitcroft
1811d8b07710SJoe Perches	my $camelcase_file_seeded = 0;
1812d8b07710SJoe Perches
1813773647a0SAndy Whitcroft	sanitise_line_reset();
1814c2fdda0dSAndy Whitcroft	my $line;
1815c2fdda0dSAndy Whitcroft	foreach my $rawline (@rawlines) {
1816773647a0SAndy Whitcroft		$linenr++;
1817773647a0SAndy Whitcroft		$line = $rawline;
1818c2fdda0dSAndy Whitcroft
18193705ce5bSJoe Perches		push(@fixed, $rawline) if ($fix);
18203705ce5bSJoe Perches
1821773647a0SAndy Whitcroft		if ($rawline=~/^\+\+\+\s+(\S+)/) {
1822de7d4f0eSAndy Whitcroft			$setup_docs = 0;
1823de7d4f0eSAndy Whitcroft			if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
1824de7d4f0eSAndy Whitcroft				$setup_docs = 1;
1825de7d4f0eSAndy Whitcroft			}
1826773647a0SAndy Whitcroft			#next;
1827de7d4f0eSAndy Whitcroft		}
1828773647a0SAndy Whitcroft		if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1829773647a0SAndy Whitcroft			$realline=$1-1;
1830773647a0SAndy Whitcroft			if (defined $2) {
1831773647a0SAndy Whitcroft				$realcnt=$3+1;
1832773647a0SAndy Whitcroft			} else {
1833773647a0SAndy Whitcroft				$realcnt=1+1;
1834773647a0SAndy Whitcroft			}
1835c45dcabdSAndy Whitcroft			$in_comment = 0;
1836773647a0SAndy Whitcroft
1837773647a0SAndy Whitcroft			# Guestimate if this is a continuing comment.  Run
1838773647a0SAndy Whitcroft			# the context looking for a comment "edge".  If this
1839773647a0SAndy Whitcroft			# edge is a close comment then we must be in a comment
1840773647a0SAndy Whitcroft			# at context start.
1841773647a0SAndy Whitcroft			my $edge;
184201fa9147SAndy Whitcroft			my $cnt = $realcnt;
184301fa9147SAndy Whitcroft			for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
184401fa9147SAndy Whitcroft				next if (defined $rawlines[$ln - 1] &&
184501fa9147SAndy Whitcroft					 $rawlines[$ln - 1] =~ /^-/);
184601fa9147SAndy Whitcroft				$cnt--;
184701fa9147SAndy Whitcroft				#print "RAW<$rawlines[$ln - 1]>\n";
1848721c1cb6SAndy Whitcroft				last if (!defined $rawlines[$ln - 1]);
1849fae17daeSAndy Whitcroft				if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
1850fae17daeSAndy Whitcroft				    $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
1851fae17daeSAndy Whitcroft					($edge) = $1;
1852fae17daeSAndy Whitcroft					last;
1853fae17daeSAndy Whitcroft				}
1854773647a0SAndy Whitcroft			}
1855773647a0SAndy Whitcroft			if (defined $edge && $edge eq '*/') {
1856773647a0SAndy Whitcroft				$in_comment = 1;
1857773647a0SAndy Whitcroft			}
1858773647a0SAndy Whitcroft
1859773647a0SAndy Whitcroft			# Guestimate if this is a continuing comment.  If this
1860773647a0SAndy Whitcroft			# is the start of a diff block and this line starts
1861773647a0SAndy Whitcroft			# ' *' then it is very likely a comment.
1862773647a0SAndy Whitcroft			if (!defined $edge &&
186383242e0cSAndy Whitcroft			    $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
1864773647a0SAndy Whitcroft			{
1865773647a0SAndy Whitcroft				$in_comment = 1;
1866773647a0SAndy Whitcroft			}
1867773647a0SAndy Whitcroft
1868773647a0SAndy Whitcroft			##print "COMMENT:$in_comment edge<$edge> $rawline\n";
1869773647a0SAndy Whitcroft			sanitise_line_reset($in_comment);
1870773647a0SAndy Whitcroft
1871171ae1a4SAndy Whitcroft		} elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
1872773647a0SAndy Whitcroft			# Standardise the strings and chars within the input to
1873171ae1a4SAndy Whitcroft			# simplify matching -- only bother with positive lines.
1874773647a0SAndy Whitcroft			$line = sanitise_line($rawline);
1875773647a0SAndy Whitcroft		}
1876773647a0SAndy Whitcroft		push(@lines, $line);
1877773647a0SAndy Whitcroft
1878773647a0SAndy Whitcroft		if ($realcnt > 1) {
1879773647a0SAndy Whitcroft			$realcnt-- if ($line =~ /^(?:\+| |$)/);
1880773647a0SAndy Whitcroft		} else {
1881773647a0SAndy Whitcroft			$realcnt = 0;
1882773647a0SAndy Whitcroft		}
1883773647a0SAndy Whitcroft
1884773647a0SAndy Whitcroft		#print "==>$rawline\n";
1885773647a0SAndy Whitcroft		#print "-->$line\n";
1886de7d4f0eSAndy Whitcroft
1887de7d4f0eSAndy Whitcroft		if ($setup_docs && $line =~ /^\+/) {
1888de7d4f0eSAndy Whitcroft			push(@setup_docs, $line);
1889de7d4f0eSAndy Whitcroft		}
1890de7d4f0eSAndy Whitcroft	}
1891de7d4f0eSAndy Whitcroft
18926c72ffaaSAndy Whitcroft	$prefix = '';
18936c72ffaaSAndy Whitcroft
1894773647a0SAndy Whitcroft	$realcnt = 0;
1895773647a0SAndy Whitcroft	$linenr = 0;
1896194f66fcSJoe Perches	$fixlinenr = -1;
18970a920b5bSAndy Whitcroft	foreach my $line (@lines) {
18980a920b5bSAndy Whitcroft		$linenr++;
1899194f66fcSJoe Perches		$fixlinenr++;
19001b5539b1SJoe Perches		my $sline = $line;	#copy of $line
19011b5539b1SJoe Perches		$sline =~ s/$;/ /g;	#with comments as spaces
19020a920b5bSAndy Whitcroft
1903c2fdda0dSAndy Whitcroft		my $rawline = $rawlines[$linenr - 1];
19046c72ffaaSAndy Whitcroft
19050a920b5bSAndy Whitcroft#extract the line range in the file after the patch is applied
19066c72ffaaSAndy Whitcroft		if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
19070a920b5bSAndy Whitcroft			$is_patch = 1;
19084a0df2efSAndy Whitcroft			$first_line = $linenr + 1;
19090a920b5bSAndy Whitcroft			$realline=$1-1;
19100a920b5bSAndy Whitcroft			if (defined $2) {
19110a920b5bSAndy Whitcroft				$realcnt=$3+1;
19120a920b5bSAndy Whitcroft			} else {
19130a920b5bSAndy Whitcroft				$realcnt=1+1;
19140a920b5bSAndy Whitcroft			}
1915c2fdda0dSAndy Whitcroft			annotate_reset();
191613214adfSAndy Whitcroft			$prev_values = 'E';
191713214adfSAndy Whitcroft
1918773647a0SAndy Whitcroft			%suppress_ifbraces = ();
1919170d3a22SAndy Whitcroft			%suppress_whiletrailers = ();
19202b474a1aSAndy Whitcroft			%suppress_export = ();
19213e469cdcSAndy Whitcroft			$suppress_statement = 0;
19220a920b5bSAndy Whitcroft			next;
19230a920b5bSAndy Whitcroft
19244a0df2efSAndy Whitcroft# track the line number as we move through the hunk, note that
19254a0df2efSAndy Whitcroft# new versions of GNU diff omit the leading space on completely
19264a0df2efSAndy Whitcroft# blank context lines so we need to count that too.
1927773647a0SAndy Whitcroft		} elsif ($line =~ /^( |\+|$)/) {
19280a920b5bSAndy Whitcroft			$realline++;
1929d8aaf121SAndy Whitcroft			$realcnt-- if ($realcnt != 0);
19300a920b5bSAndy Whitcroft
19314a0df2efSAndy Whitcroft			# Measure the line length and indent.
1932c2fdda0dSAndy Whitcroft			($length, $indent) = line_stats($rawline);
19330a920b5bSAndy Whitcroft
19340a920b5bSAndy Whitcroft			# Track the previous line.
19350a920b5bSAndy Whitcroft			($prevline, $stashline) = ($stashline, $line);
19360a920b5bSAndy Whitcroft			($previndent, $stashindent) = ($stashindent, $indent);
1937c2fdda0dSAndy Whitcroft			($prevrawline, $stashrawline) = ($stashrawline, $rawline);
1938c2fdda0dSAndy Whitcroft
1939773647a0SAndy Whitcroft			#warn "line<$line>\n";
19406c72ffaaSAndy Whitcroft
1941d8aaf121SAndy Whitcroft		} elsif ($realcnt == 1) {
1942d8aaf121SAndy Whitcroft			$realcnt--;
19430a920b5bSAndy Whitcroft		}
19440a920b5bSAndy Whitcroft
1945cc77cdcaSAndy Whitcroft		my $hunk_line = ($realcnt != 0);
1946cc77cdcaSAndy Whitcroft
19470a920b5bSAndy Whitcroft#make up the handle for any error we report on this line
1948773647a0SAndy Whitcroft		$prefix = "$filename:$realline: " if ($emacs && $file);
1949773647a0SAndy Whitcroft		$prefix = "$filename:$linenr: " if ($emacs && !$file);
1950773647a0SAndy Whitcroft
19516c72ffaaSAndy Whitcroft		$here = "#$linenr: " if (!$file);
19526c72ffaaSAndy Whitcroft		$here = "#$realline: " if ($file);
1953773647a0SAndy Whitcroft
19542ac73b4fSJoe Perches		my $found_file = 0;
1955773647a0SAndy Whitcroft		# extract the filename as it passes
19563bf9a009SRabin Vincent		if ($line =~ /^diff --git.*?(\S+)$/) {
19573bf9a009SRabin Vincent			$realfile = $1;
19582b7ab453SJoe Perches			$realfile =~ s@^([^/]*)/@@ if (!$file);
1959270c49a0SJoe Perches			$in_commit_log = 0;
19602ac73b4fSJoe Perches			$found_file = 1;
19613bf9a009SRabin Vincent		} elsif ($line =~ /^\+\+\+\s+(\S+)/) {
1962773647a0SAndy Whitcroft			$realfile = $1;
19632b7ab453SJoe Perches			$realfile =~ s@^([^/]*)/@@ if (!$file);
1964270c49a0SJoe Perches			$in_commit_log = 0;
19651e855726SWolfram Sang
19661e855726SWolfram Sang			$p1_prefix = $1;
1967e2f7aa4bSAndy Whitcroft			if (!$file && $tree && $p1_prefix ne '' &&
1968e2f7aa4bSAndy Whitcroft			    -e "$root/$p1_prefix") {
1969000d1cc1SJoe Perches				WARN("PATCH_PREFIX",
1970000d1cc1SJoe Perches				     "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
19711e855726SWolfram Sang			}
1972773647a0SAndy Whitcroft
1973c1ab3326SAndy Whitcroft			if ($realfile =~ m@^include/asm/@) {
1974000d1cc1SJoe Perches				ERROR("MODIFIED_INCLUDE_ASM",
1975000d1cc1SJoe Perches				      "do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
1976773647a0SAndy Whitcroft			}
19772ac73b4fSJoe Perches			$found_file = 1;
19782ac73b4fSJoe Perches		}
19792ac73b4fSJoe Perches
19802ac73b4fSJoe Perches		if ($found_file) {
19812ac73b4fSJoe Perches			if ($realfile =~ m@^(drivers/net/|net/)@) {
19822ac73b4fSJoe Perches				$check = 1;
19832ac73b4fSJoe Perches			} else {
19842ac73b4fSJoe Perches				$check = $check_orig;
19852ac73b4fSJoe Perches			}
1986773647a0SAndy Whitcroft			next;
1987773647a0SAndy Whitcroft		}
1988773647a0SAndy Whitcroft
1989389834b6SRandy Dunlap		$here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
19900a920b5bSAndy Whitcroft
1991c2fdda0dSAndy Whitcroft		my $hereline = "$here\n$rawline\n";
1992c2fdda0dSAndy Whitcroft		my $herecurr = "$here\n$rawline\n";
1993c2fdda0dSAndy Whitcroft		my $hereprev = "$here\n$prevrawline\n$rawline\n";
19940a920b5bSAndy Whitcroft
19956c72ffaaSAndy Whitcroft		$cnt_lines++ if ($realcnt != 0);
19966c72ffaaSAndy Whitcroft
19973bf9a009SRabin Vincent# Check for incorrect file permissions
19983bf9a009SRabin Vincent		if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
19993bf9a009SRabin Vincent			my $permhere = $here . "FILE: $realfile\n";
200004db4d25SJoe Perches			if ($realfile !~ m@scripts/@ &&
200104db4d25SJoe Perches			    $realfile !~ /\.(py|pl|awk|sh)$/) {
2002000d1cc1SJoe Perches				ERROR("EXECUTE_PERMISSIONS",
2003000d1cc1SJoe Perches				      "do not set execute permissions for source files\n" . $permhere);
20043bf9a009SRabin Vincent			}
20053bf9a009SRabin Vincent		}
20063bf9a009SRabin Vincent
200720112475SJoe Perches# Check the patch for a signoff:
2008d8aaf121SAndy Whitcroft		if ($line =~ /^\s*signed-off-by:/i) {
20094a0df2efSAndy Whitcroft			$signoff++;
201015662b3eSJoe Perches			$in_commit_log = 0;
20110a920b5bSAndy Whitcroft		}
201220112475SJoe Perches
201320112475SJoe Perches# Check signature styles
2014270c49a0SJoe Perches		if (!$in_header_lines &&
2015ce0338dfSJoe Perches		    $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
201620112475SJoe Perches			my $space_before = $1;
201720112475SJoe Perches			my $sign_off = $2;
201820112475SJoe Perches			my $space_after = $3;
201920112475SJoe Perches			my $email = $4;
202020112475SJoe Perches			my $ucfirst_sign_off = ucfirst(lc($sign_off));
202120112475SJoe Perches
2022ce0338dfSJoe Perches			if ($sign_off !~ /$signature_tags/) {
2023ce0338dfSJoe Perches				WARN("BAD_SIGN_OFF",
2024ce0338dfSJoe Perches				     "Non-standard signature: $sign_off\n" . $herecurr);
2025ce0338dfSJoe Perches			}
202620112475SJoe Perches			if (defined $space_before && $space_before ne "") {
20273705ce5bSJoe Perches				if (WARN("BAD_SIGN_OFF",
20283705ce5bSJoe Perches					 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
20293705ce5bSJoe Perches				    $fix) {
2030194f66fcSJoe Perches					$fixed[$fixlinenr] =
20313705ce5bSJoe Perches					    "$ucfirst_sign_off $email";
20323705ce5bSJoe Perches				}
203320112475SJoe Perches			}
203420112475SJoe Perches			if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
20353705ce5bSJoe Perches				if (WARN("BAD_SIGN_OFF",
20363705ce5bSJoe Perches					 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
20373705ce5bSJoe Perches				    $fix) {
2038194f66fcSJoe Perches					$fixed[$fixlinenr] =
20393705ce5bSJoe Perches					    "$ucfirst_sign_off $email";
20403705ce5bSJoe Perches				}
20413705ce5bSJoe Perches
204220112475SJoe Perches			}
204320112475SJoe Perches			if (!defined $space_after || $space_after ne " ") {
20443705ce5bSJoe Perches				if (WARN("BAD_SIGN_OFF",
20453705ce5bSJoe Perches					 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
20463705ce5bSJoe Perches				    $fix) {
2047194f66fcSJoe Perches					$fixed[$fixlinenr] =
20483705ce5bSJoe Perches					    "$ucfirst_sign_off $email";
20493705ce5bSJoe Perches				}
205020112475SJoe Perches			}
205120112475SJoe Perches
205220112475SJoe Perches			my ($email_name, $email_address, $comment) = parse_email($email);
205320112475SJoe Perches			my $suggested_email = format_email(($email_name, $email_address));
205420112475SJoe Perches			if ($suggested_email eq "") {
2055000d1cc1SJoe Perches				ERROR("BAD_SIGN_OFF",
2056000d1cc1SJoe Perches				      "Unrecognized email address: '$email'\n" . $herecurr);
205720112475SJoe Perches			} else {
205820112475SJoe Perches				my $dequoted = $suggested_email;
205920112475SJoe Perches				$dequoted =~ s/^"//;
206020112475SJoe Perches				$dequoted =~ s/" </ </;
206120112475SJoe Perches				# Don't force email to have quotes
206220112475SJoe Perches				# Allow just an angle bracketed address
206320112475SJoe Perches				if ("$dequoted$comment" ne $email &&
206420112475SJoe Perches				    "<$email_address>$comment" ne $email &&
206520112475SJoe Perches				    "$suggested_email$comment" ne $email) {
2066000d1cc1SJoe Perches					WARN("BAD_SIGN_OFF",
2067000d1cc1SJoe Perches					     "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
206820112475SJoe Perches				}
20690a920b5bSAndy Whitcroft			}
20707e51f197SJoe Perches
20717e51f197SJoe Perches# Check for duplicate signatures
20727e51f197SJoe Perches			my $sig_nospace = $line;
20737e51f197SJoe Perches			$sig_nospace =~ s/\s//g;
20747e51f197SJoe Perches			$sig_nospace = lc($sig_nospace);
20757e51f197SJoe Perches			if (defined $signatures{$sig_nospace}) {
20767e51f197SJoe Perches				WARN("BAD_SIGN_OFF",
20777e51f197SJoe Perches				     "Duplicate signature\n" . $herecurr);
20787e51f197SJoe Perches			} else {
20797e51f197SJoe Perches				$signatures{$sig_nospace} = 1;
20807e51f197SJoe Perches			}
20810a920b5bSAndy Whitcroft		}
20820a920b5bSAndy Whitcroft
20839b3189ebSJoe Perches# Check for old stable address
20849b3189ebSJoe Perches		if ($line =~ /^\s*cc:\s*.*<?\bstable\@kernel\.org\b>?.*$/i) {
20859b3189ebSJoe Perches			ERROR("STABLE_ADDRESS",
20869b3189ebSJoe Perches			      "The 'stable' address should be 'stable\@vger.kernel.org'\n" . $herecurr);
20879b3189ebSJoe Perches		}
20889b3189ebSJoe Perches
20897ebd05efSChristopher Covington# Check for unwanted Gerrit info
20907ebd05efSChristopher Covington		if ($in_commit_log && $line =~ /^\s*change-id:/i) {
20917ebd05efSChristopher Covington			ERROR("GERRIT_CHANGE_ID",
20927ebd05efSChristopher Covington			      "Remove Gerrit Change-Id's before submitting upstream.\n" . $herecurr);
20937ebd05efSChristopher Covington		}
20947ebd05efSChristopher Covington
2095d311cd44SJoe Perches# Check for improperly formed commit descriptions
2096d311cd44SJoe Perches		if ($in_commit_log &&
2097d311cd44SJoe Perches		    $line =~ /\bcommit\s+[0-9a-f]{5,}/i &&
2098d311cd44SJoe Perches		    $line !~ /\b[Cc]ommit [0-9a-f]{12,16} \("/) {
2099d311cd44SJoe Perches			$line =~ /\b(c)ommit\s+([0-9a-f]{5,})/i;
2100d311cd44SJoe Perches			my $init_char = $1;
2101d311cd44SJoe Perches			my $orig_commit = lc($2);
2102d311cd44SJoe Perches			my $id = '01234567890ab';
2103d311cd44SJoe Perches			my $desc = 'commit description';
2104d311cd44SJoe Perches		        ($id, $desc) = git_commit_info($orig_commit, $id, $desc);
2105d311cd44SJoe Perches			ERROR("GIT_COMMIT_ID",
2106d311cd44SJoe Perches			      "Please use 12 to 16 chars for the git commit ID like: '${init_char}ommit $id (\"$desc\")'\n" . $herecurr);
2107d311cd44SJoe Perches		}
2108d311cd44SJoe Perches
210913f1937eSJoe Perches# Check for added, moved or deleted files
211013f1937eSJoe Perches		if (!$reported_maintainer_file && !$in_commit_log &&
211113f1937eSJoe Perches		    ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ ||
211213f1937eSJoe Perches		     $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ ||
211313f1937eSJoe Perches		     ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ &&
211413f1937eSJoe Perches		      (defined($1) || defined($2))))) {
211513f1937eSJoe Perches			$reported_maintainer_file = 1;
211613f1937eSJoe Perches			WARN("FILE_PATH_CHANGES",
211713f1937eSJoe Perches			     "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr);
211813f1937eSJoe Perches		}
211913f1937eSJoe Perches
212000df344fSAndy Whitcroft# Check for wrappage within a valid hunk of the file
21218905a67cSAndy Whitcroft		if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
2122000d1cc1SJoe Perches			ERROR("CORRUPTED_PATCH",
2123000d1cc1SJoe Perches			      "patch seems to be corrupt (line wrapped?)\n" .
21246c72ffaaSAndy Whitcroft				$herecurr) if (!$emitted_corrupt++);
2125de7d4f0eSAndy Whitcroft		}
2126de7d4f0eSAndy Whitcroft
21276ecd9674SAndy Whitcroft# Check for absolute kernel paths.
21286ecd9674SAndy Whitcroft		if ($tree) {
21296ecd9674SAndy Whitcroft			while ($line =~ m{(?:^|\s)(/\S*)}g) {
21306ecd9674SAndy Whitcroft				my $file = $1;
21316ecd9674SAndy Whitcroft
21326ecd9674SAndy Whitcroft				if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
21336ecd9674SAndy Whitcroft				    check_absolute_file($1, $herecurr)) {
21346ecd9674SAndy Whitcroft					#
21356ecd9674SAndy Whitcroft				} else {
21366ecd9674SAndy Whitcroft					check_absolute_file($file, $herecurr);
21376ecd9674SAndy Whitcroft				}
21386ecd9674SAndy Whitcroft			}
21396ecd9674SAndy Whitcroft		}
21406ecd9674SAndy Whitcroft
2141de7d4f0eSAndy Whitcroft# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
2142de7d4f0eSAndy Whitcroft		if (($realfile =~ /^$/ || $line =~ /^\+/) &&
2143171ae1a4SAndy Whitcroft		    $rawline !~ m/^$UTF8*$/) {
2144171ae1a4SAndy Whitcroft			my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
2145171ae1a4SAndy Whitcroft
2146171ae1a4SAndy Whitcroft			my $blank = copy_spacing($rawline);
2147171ae1a4SAndy Whitcroft			my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
2148171ae1a4SAndy Whitcroft			my $hereptr = "$hereline$ptr\n";
2149171ae1a4SAndy Whitcroft
215034d99219SJoe Perches			CHK("INVALID_UTF8",
2151000d1cc1SJoe Perches			    "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
215200df344fSAndy Whitcroft		}
21530a920b5bSAndy Whitcroft
215415662b3eSJoe Perches# Check if it's the start of a commit log
215515662b3eSJoe Perches# (not a header line and we haven't seen the patch filename)
215615662b3eSJoe Perches		if ($in_header_lines && $realfile =~ /^$/ &&
215729ee1b0cSJoe Perches		    !($rawline =~ /^\s+\S/ ||
215829ee1b0cSJoe Perches		      $rawline =~ /^(commit\b|from\b|[\w-]+:).*$/i)) {
215915662b3eSJoe Perches			$in_header_lines = 0;
216015662b3eSJoe Perches			$in_commit_log = 1;
216115662b3eSJoe Perches		}
216215662b3eSJoe Perches
2163fa64205dSPasi Savanainen# Check if there is UTF-8 in a commit log when a mail header has explicitly
2164fa64205dSPasi Savanainen# declined it, i.e defined some charset where it is missing.
2165fa64205dSPasi Savanainen		if ($in_header_lines &&
2166fa64205dSPasi Savanainen		    $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
2167fa64205dSPasi Savanainen		    $1 !~ /utf-8/i) {
2168fa64205dSPasi Savanainen			$non_utf8_charset = 1;
2169fa64205dSPasi Savanainen		}
2170fa64205dSPasi Savanainen
2171fa64205dSPasi Savanainen		if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
217215662b3eSJoe Perches		    $rawline =~ /$NON_ASCII_UTF8/) {
2173fa64205dSPasi Savanainen			WARN("UTF8_BEFORE_PATCH",
217415662b3eSJoe Perches			    "8-bit UTF-8 used in possible commit log\n" . $herecurr);
217515662b3eSJoe Perches		}
217615662b3eSJoe Perches
217730670854SAndy Whitcroft# ignore non-hunk lines and lines being removed
217830670854SAndy Whitcroft		next if (!$hunk_line || $line =~ /^-/);
217900df344fSAndy Whitcroft
21800a920b5bSAndy Whitcroft#trailing whitespace
21819c0ca6f9SAndy Whitcroft		if ($line =~ /^\+.*\015/) {
2182c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2183d5e616fcSJoe Perches			if (ERROR("DOS_LINE_ENDINGS",
2184d5e616fcSJoe Perches				  "DOS line endings\n" . $herevet) &&
2185d5e616fcSJoe Perches			    $fix) {
2186194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/[\s\015]+$//;
2187d5e616fcSJoe Perches			}
2188c2fdda0dSAndy Whitcroft		} elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
2189c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
21903705ce5bSJoe Perches			if (ERROR("TRAILING_WHITESPACE",
21913705ce5bSJoe Perches				  "trailing whitespace\n" . $herevet) &&
21923705ce5bSJoe Perches			    $fix) {
2193194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\s+$//;
21943705ce5bSJoe Perches			}
21953705ce5bSJoe Perches
2196d2c0a235SAndy Whitcroft			$rpt_cleaners = 1;
21970a920b5bSAndy Whitcroft		}
21985368df20SAndy Whitcroft
21994783f894SJosh Triplett# Check for FSF mailing addresses.
2200109d8cb2SAlexander Duyck		if ($rawline =~ /\bwrite to the Free/i ||
22013e2232f2SJoe Perches		    $rawline =~ /\b59\s+Temple\s+Pl/i ||
22023e2232f2SJoe Perches		    $rawline =~ /\b51\s+Franklin\s+St/i) {
22034783f894SJosh Triplett			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
22044783f894SJosh Triplett			my $msg_type = \&ERROR;
22054783f894SJosh Triplett			$msg_type = \&CHK if ($file);
22064783f894SJosh Triplett			&{$msg_type}("FSF_MAILING_ADDRESS",
22074783f894SJosh 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)
22084783f894SJosh Triplett		}
22094783f894SJosh Triplett
22103354957aSAndi Kleen# check for Kconfig help text having a real description
22119fe287d7SAndy Whitcroft# Only applies when adding the entry originally, after that we do not have
22129fe287d7SAndy Whitcroft# sufficient context to determine whether it is indeed long enough.
22133354957aSAndi Kleen		if ($realfile =~ /Kconfig/ &&
22148d73e0e7SJoe Perches		    $line =~ /^\+\s*config\s+/) {
22153354957aSAndi Kleen			my $length = 0;
22169fe287d7SAndy Whitcroft			my $cnt = $realcnt;
22179fe287d7SAndy Whitcroft			my $ln = $linenr + 1;
22189fe287d7SAndy Whitcroft			my $f;
2219a1385803SAndy Whitcroft			my $is_start = 0;
22209fe287d7SAndy Whitcroft			my $is_end = 0;
2221a1385803SAndy Whitcroft			for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
22229fe287d7SAndy Whitcroft				$f = $lines[$ln - 1];
22239fe287d7SAndy Whitcroft				$cnt-- if ($lines[$ln - 1] !~ /^-/);
22249fe287d7SAndy Whitcroft				$is_end = $lines[$ln - 1] =~ /^\+/;
22259fe287d7SAndy Whitcroft
22269fe287d7SAndy Whitcroft				next if ($f =~ /^-/);
22278d73e0e7SJoe Perches				last if (!$file && $f =~ /^\@\@/);
2228a1385803SAndy Whitcroft
22298d73e0e7SJoe Perches				if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate)\s*\"/) {
2230a1385803SAndy Whitcroft					$is_start = 1;
22318d73e0e7SJoe Perches				} elsif ($lines[$ln - 1] =~ /^\+\s*(?:---)?help(?:---)?$/) {
2232a1385803SAndy Whitcroft					$length = -1;
2233a1385803SAndy Whitcroft				}
2234a1385803SAndy Whitcroft
22359fe287d7SAndy Whitcroft				$f =~ s/^.//;
22363354957aSAndi Kleen				$f =~ s/#.*//;
22373354957aSAndi Kleen				$f =~ s/^\s+//;
22383354957aSAndi Kleen				next if ($f =~ /^$/);
22399fe287d7SAndy Whitcroft				if ($f =~ /^\s*config\s/) {
22409fe287d7SAndy Whitcroft					$is_end = 1;
22419fe287d7SAndy Whitcroft					last;
22429fe287d7SAndy Whitcroft				}
22433354957aSAndi Kleen				$length++;
22443354957aSAndi Kleen			}
2245000d1cc1SJoe Perches			WARN("CONFIG_DESCRIPTION",
2246a1385803SAndy Whitcroft			     "please write a paragraph that describes the config symbol fully\n" . $herecurr) if ($is_start && $is_end && $length < 4);
2247a1385803SAndy Whitcroft			#print "is_start<$is_start> is_end<$is_end> length<$length>\n";
22483354957aSAndi Kleen		}
22493354957aSAndi Kleen
22501ba8dfd1SKees Cook# discourage the addition of CONFIG_EXPERIMENTAL in Kconfig.
22511ba8dfd1SKees Cook		if ($realfile =~ /Kconfig/ &&
22521ba8dfd1SKees Cook		    $line =~ /.\s*depends on\s+.*\bEXPERIMENTAL\b/) {
22531ba8dfd1SKees Cook			WARN("CONFIG_EXPERIMENTAL",
22541ba8dfd1SKees Cook			     "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
22551ba8dfd1SKees Cook		}
22561ba8dfd1SKees Cook
2257c68e5878SArnaud Lacombe		if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
2258c68e5878SArnaud Lacombe		    ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
2259c68e5878SArnaud Lacombe			my $flag = $1;
2260c68e5878SArnaud Lacombe			my $replacement = {
2261c68e5878SArnaud Lacombe				'EXTRA_AFLAGS' =>   'asflags-y',
2262c68e5878SArnaud Lacombe				'EXTRA_CFLAGS' =>   'ccflags-y',
2263c68e5878SArnaud Lacombe				'EXTRA_CPPFLAGS' => 'cppflags-y',
2264c68e5878SArnaud Lacombe				'EXTRA_LDFLAGS' =>  'ldflags-y',
2265c68e5878SArnaud Lacombe			};
2266c68e5878SArnaud Lacombe
2267c68e5878SArnaud Lacombe			WARN("DEPRECATED_VARIABLE",
2268c68e5878SArnaud Lacombe			     "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
2269c68e5878SArnaud Lacombe		}
2270c68e5878SArnaud Lacombe
2271bff5da43SRob Herring# check for DT compatible documentation
22727dd05b38SFlorian Vaussard		if (defined $root &&
22737dd05b38SFlorian Vaussard			(($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) ||
22747dd05b38SFlorian Vaussard			 ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) {
22757dd05b38SFlorian Vaussard
2276bff5da43SRob Herring			my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g;
2277bff5da43SRob Herring
2278cc93319bSFlorian Vaussard			my $dt_path = $root . "/Documentation/devicetree/bindings/";
2279cc93319bSFlorian Vaussard			my $vp_file = $dt_path . "vendor-prefixes.txt";
2280cc93319bSFlorian Vaussard
2281bff5da43SRob Herring			foreach my $compat (@compats) {
2282bff5da43SRob Herring				my $compat2 = $compat;
2283185d566bSRob Herring				$compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/;
2284185d566bSRob Herring				my $compat3 = $compat;
2285185d566bSRob Herring				$compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/;
2286185d566bSRob Herring				`grep -Erq "$compat|$compat2|$compat3" $dt_path`;
2287bff5da43SRob Herring				if ( $? >> 8 ) {
2288bff5da43SRob Herring					WARN("UNDOCUMENTED_DT_STRING",
2289bff5da43SRob Herring					     "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr);
2290bff5da43SRob Herring				}
2291bff5da43SRob Herring
22924fbf32a6SFlorian Vaussard				next if $compat !~ /^([a-zA-Z0-9\-]+)\,/;
22934fbf32a6SFlorian Vaussard				my $vendor = $1;
2294cc93319bSFlorian Vaussard				`grep -Eq "^$vendor\\b" $vp_file`;
2295bff5da43SRob Herring				if ( $? >> 8 ) {
2296bff5da43SRob Herring					WARN("UNDOCUMENTED_DT_STRING",
2297cc93319bSFlorian Vaussard					     "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr);
2298bff5da43SRob Herring				}
2299bff5da43SRob Herring			}
2300bff5da43SRob Herring		}
2301bff5da43SRob Herring
23025368df20SAndy Whitcroft# check we are in a valid source file if not then ignore this hunk
23035368df20SAndy Whitcroft		next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
23045368df20SAndy Whitcroft
23056cd7f386SJoe Perches#line length limit
2306c45dcabdSAndy Whitcroft		if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ &&
2307f4c014c0SAndy Whitcroft		    $rawline !~ /^.\s*\*\s*\@$Ident\s/ &&
23080fccc622SJoe Perches		    !($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(KERN_\S+\s*|[^"]*))?"[X\t]*"\s*(?:|,|\)\s*;)\s*$/ ||
23098bbea968SJoe Perches		    $line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) &&
23106cd7f386SJoe Perches		    $length > $max_line_length)
2311c45dcabdSAndy Whitcroft		{
2312000d1cc1SJoe Perches			WARN("LONG_LINE",
23136cd7f386SJoe Perches			     "line over $max_line_length characters\n" . $herecurr);
23140a920b5bSAndy Whitcroft		}
23150a920b5bSAndy Whitcroft
2316ca56dc09SJosh Triplett# Check for user-visible strings broken across lines, which breaks the ability
23178c5fcd24SJoe Perches# to grep for the string.  Make exceptions when the previous string ends in a
23188c5fcd24SJoe Perches# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{'
23198c5fcd24SJoe Perches# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value
2320ca56dc09SJosh Triplett		if ($line =~ /^\+\s*"/ &&
2321ca56dc09SJosh Triplett		    $prevline =~ /"\s*$/ &&
23228c5fcd24SJoe Perches		    $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) {
2323ca56dc09SJosh Triplett			WARN("SPLIT_STRING",
2324ca56dc09SJosh Triplett			     "quoted string split across lines\n" . $hereprev);
2325ca56dc09SJosh Triplett		}
2326ca56dc09SJosh Triplett
23275e79d96eSJoe Perches# check for spaces before a quoted newline
23285e79d96eSJoe Perches		if ($rawline =~ /^.*\".*\s\\n/) {
23293705ce5bSJoe Perches			if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
23303705ce5bSJoe Perches				 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
23313705ce5bSJoe Perches			    $fix) {
2332194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
23333705ce5bSJoe Perches			}
23343705ce5bSJoe Perches
23355e79d96eSJoe Perches		}
23365e79d96eSJoe Perches
23378905a67cSAndy Whitcroft# check for adding lines without a newline.
23388905a67cSAndy Whitcroft		if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
2339000d1cc1SJoe Perches			WARN("MISSING_EOF_NEWLINE",
2340000d1cc1SJoe Perches			     "adding a line without newline at end of file\n" . $herecurr);
23418905a67cSAndy Whitcroft		}
23428905a67cSAndy Whitcroft
234342e41c54SMike Frysinger# Blackfin: use hi/lo macros
234442e41c54SMike Frysinger		if ($realfile =~ m@arch/blackfin/.*\.S$@) {
234542e41c54SMike Frysinger			if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
234642e41c54SMike Frysinger				my $herevet = "$here\n" . cat_vet($line) . "\n";
2347000d1cc1SJoe Perches				ERROR("LO_MACRO",
2348000d1cc1SJoe Perches				      "use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
234942e41c54SMike Frysinger			}
235042e41c54SMike Frysinger			if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
235142e41c54SMike Frysinger				my $herevet = "$here\n" . cat_vet($line) . "\n";
2352000d1cc1SJoe Perches				ERROR("HI_MACRO",
2353000d1cc1SJoe Perches				      "use the HI() macro, not (... >> 16)\n" . $herevet);
235442e41c54SMike Frysinger			}
235542e41c54SMike Frysinger		}
235642e41c54SMike Frysinger
2357b9ea10d6SAndy Whitcroft# check we are in a valid source file C or perl if not then ignore this hunk
2358b9ea10d6SAndy Whitcroft		next if ($realfile !~ /\.(h|c|pl)$/);
23590a920b5bSAndy Whitcroft
23600a920b5bSAndy Whitcroft# at the beginning of a line any tabs must come first and anything
23610a920b5bSAndy Whitcroft# more than 8 must use tabs.
2362c2fdda0dSAndy Whitcroft		if ($rawline =~ /^\+\s* \t\s*\S/ ||
2363c2fdda0dSAndy Whitcroft		    $rawline =~ /^\+\s*        \s*/) {
2364c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2365d2c0a235SAndy Whitcroft			$rpt_cleaners = 1;
23663705ce5bSJoe Perches			if (ERROR("CODE_INDENT",
23673705ce5bSJoe Perches				  "code indent should use tabs where possible\n" . $herevet) &&
23683705ce5bSJoe Perches			    $fix) {
2369194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
23703705ce5bSJoe Perches			}
23710a920b5bSAndy Whitcroft		}
23720a920b5bSAndy Whitcroft
237308e44365SAlberto Panizzo# check for space before tabs.
237408e44365SAlberto Panizzo		if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
237508e44365SAlberto Panizzo			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
23763705ce5bSJoe Perches			if (WARN("SPACE_BEFORE_TAB",
23773705ce5bSJoe Perches				"please, no space before tabs\n" . $herevet) &&
23783705ce5bSJoe Perches			    $fix) {
2379194f66fcSJoe Perches				while ($fixed[$fixlinenr] =~
2380c76f4cb3SJoe Perches					   s/(^\+.*) {8,8}+\t/$1\t\t/) {}
2381194f66fcSJoe Perches				while ($fixed[$fixlinenr] =~
2382c76f4cb3SJoe Perches					   s/(^\+.*) +\t/$1\t/) {}
23833705ce5bSJoe Perches			}
238408e44365SAlberto Panizzo		}
238508e44365SAlberto Panizzo
2386d1fe9c09SJoe Perches# check for && or || at the start of a line
2387d1fe9c09SJoe Perches		if ($rawline =~ /^\+\s*(&&|\|\|)/) {
2388d1fe9c09SJoe Perches			CHK("LOGICAL_CONTINUATIONS",
2389d1fe9c09SJoe Perches			    "Logical continuations should be on the previous line\n" . $hereprev);
2390d1fe9c09SJoe Perches		}
2391d1fe9c09SJoe Perches
2392d1fe9c09SJoe Perches# check multi-line statement indentation matches previous line
2393d1fe9c09SJoe Perches		if ($^V && $^V ge 5.10.0 &&
239491cb5195SJoe Perches		    $prevline =~ /^\+([ \t]*)((?:$c90_Keywords(?:\s+if)\s*)|(?:$Declare\s*)?(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*|$Ident\s*=\s*$Ident\s*)\(.*(\&\&|\|\||,)\s*$/) {
2395d1fe9c09SJoe Perches			$prevline =~ /^\+(\t*)(.*)$/;
2396d1fe9c09SJoe Perches			my $oldindent = $1;
2397d1fe9c09SJoe Perches			my $rest = $2;
2398d1fe9c09SJoe Perches
2399d1fe9c09SJoe Perches			my $pos = pos_last_openparen($rest);
2400d1fe9c09SJoe Perches			if ($pos >= 0) {
2401b34a26f3SJoe Perches				$line =~ /^(\+| )([ \t]*)/;
2402b34a26f3SJoe Perches				my $newindent = $2;
2403d1fe9c09SJoe Perches
2404d1fe9c09SJoe Perches				my $goodtabindent = $oldindent .
2405d1fe9c09SJoe Perches					"\t" x ($pos / 8) .
2406d1fe9c09SJoe Perches					" "  x ($pos % 8);
2407d1fe9c09SJoe Perches				my $goodspaceindent = $oldindent . " "  x $pos;
2408d1fe9c09SJoe Perches
2409d1fe9c09SJoe Perches				if ($newindent ne $goodtabindent &&
2410d1fe9c09SJoe Perches				    $newindent ne $goodspaceindent) {
24113705ce5bSJoe Perches
24123705ce5bSJoe Perches					if (CHK("PARENTHESIS_ALIGNMENT",
24133705ce5bSJoe Perches						"Alignment should match open parenthesis\n" . $hereprev) &&
24143705ce5bSJoe Perches					    $fix && $line =~ /^\+/) {
2415194f66fcSJoe Perches						$fixed[$fixlinenr] =~
24163705ce5bSJoe Perches						    s/^\+[ \t]*/\+$goodtabindent/;
24173705ce5bSJoe Perches					}
2418d1fe9c09SJoe Perches				}
2419d1fe9c09SJoe Perches			}
2420d1fe9c09SJoe Perches		}
2421d1fe9c09SJoe Perches
2422f27c95dbSJoe Perches		if ($line =~ /^\+.*\(\s*$Type\s*\)[ \t]+(?!$Assignment|$Arithmetic)/) {
24233705ce5bSJoe Perches			if (CHK("SPACING",
2424f27c95dbSJoe Perches				"No space is necessary after a cast\n" . $herecurr) &&
24253705ce5bSJoe Perches			    $fix) {
2426194f66fcSJoe Perches				$fixed[$fixlinenr] =~
2427f27c95dbSJoe Perches				    s/(\(\s*$Type\s*\))[ \t]+/$1/;
24283705ce5bSJoe Perches			}
2429aad4f614SJoe Perches		}
2430aad4f614SJoe Perches
243105880600SJoe Perches		if ($realfile =~ m@^(drivers/net/|net/)@ &&
2432fdb4bcd6SJoe Perches		    $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
243385ad978cSJoe Perches		    $rawline =~ /^\+[ \t]*\*/ &&
243485ad978cSJoe Perches		    $realline > 2) {
243505880600SJoe Perches			WARN("NETWORKING_BLOCK_COMMENT_STYLE",
243605880600SJoe Perches			     "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
243705880600SJoe Perches		}
243805880600SJoe Perches
243905880600SJoe Perches		if ($realfile =~ m@^(drivers/net/|net/)@ &&
2440a605e32eSJoe Perches		    $prevrawline =~ /^\+[ \t]*\/\*/ &&		#starting /*
2441a605e32eSJoe Perches		    $prevrawline !~ /\*\/[ \t]*$/ &&		#no trailing */
244261135e96SJoe Perches		    $rawline =~ /^\+/ &&			#line is new
2443a605e32eSJoe Perches		    $rawline !~ /^\+[ \t]*\*/) {		#no leading *
2444a605e32eSJoe Perches			WARN("NETWORKING_BLOCK_COMMENT_STYLE",
2445a605e32eSJoe Perches			     "networking block comments start with * on subsequent lines\n" . $hereprev);
2446a605e32eSJoe Perches		}
2447a605e32eSJoe Perches
2448a605e32eSJoe Perches		if ($realfile =~ m@^(drivers/net/|net/)@ &&
2449c24f9f19SJoe Perches		    $rawline !~ m@^\+[ \t]*\*/[ \t]*$@ &&	#trailing */
2450c24f9f19SJoe Perches		    $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ &&	#inline /*...*/
2451c24f9f19SJoe Perches		    $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ &&	#trailing **/
2452c24f9f19SJoe Perches		    $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) {	#non blank */
245305880600SJoe Perches			WARN("NETWORKING_BLOCK_COMMENT_STYLE",
245405880600SJoe Perches			     "networking block comments put the trailing */ on a separate line\n" . $herecurr);
245505880600SJoe Perches		}
245605880600SJoe Perches
24577f619191SJoe Perches# check for missing blank lines after struct/union declarations
24587f619191SJoe Perches# with exceptions for various attributes and macros
24597f619191SJoe Perches		if ($prevline =~ /^[\+ ]};?\s*$/ &&
24607f619191SJoe Perches		    $line =~ /^\+/ &&
24617f619191SJoe Perches		    !($line =~ /^\+\s*$/ ||
24627f619191SJoe Perches		      $line =~ /^\+\s*EXPORT_SYMBOL/ ||
24637f619191SJoe Perches		      $line =~ /^\+\s*MODULE_/i ||
24647f619191SJoe Perches		      $line =~ /^\+\s*\#\s*(?:end|elif|else)/ ||
24657f619191SJoe Perches		      $line =~ /^\+[a-z_]*init/ ||
24667f619191SJoe Perches		      $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ ||
24677f619191SJoe Perches		      $line =~ /^\+\s*DECLARE/ ||
24687f619191SJoe Perches		      $line =~ /^\+\s*__setup/)) {
2469d752fcc8SJoe Perches			if (CHK("LINE_SPACING",
2470d752fcc8SJoe Perches				"Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) &&
2471d752fcc8SJoe Perches			    $fix) {
2472f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, "\+");
2473d752fcc8SJoe Perches			}
24747f619191SJoe Perches		}
24757f619191SJoe Perches
2476365dd4eaSJoe Perches# check for multiple consecutive blank lines
2477365dd4eaSJoe Perches		if ($prevline =~ /^[\+ ]\s*$/ &&
2478365dd4eaSJoe Perches		    $line =~ /^\+\s*$/ &&
2479365dd4eaSJoe Perches		    $last_blank_line != ($linenr - 1)) {
2480d752fcc8SJoe Perches			if (CHK("LINE_SPACING",
2481d752fcc8SJoe Perches				"Please don't use multiple blank lines\n" . $hereprev) &&
2482d752fcc8SJoe Perches			    $fix) {
2483f2d7e4d4SJoe Perches				fix_delete_line($fixlinenr, $rawline);
2484d752fcc8SJoe Perches			}
2485d752fcc8SJoe Perches
2486365dd4eaSJoe Perches			$last_blank_line = $linenr;
2487365dd4eaSJoe Perches		}
2488365dd4eaSJoe Perches
24893b617e3bSJoe Perches# check for missing blank lines after declarations
24903f7bac03SJoe Perches		if ($sline =~ /^\+\s+\S/ &&			#Not at char 1
24913f7bac03SJoe Perches			# actual declarations
24923f7bac03SJoe Perches		    ($prevline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
24935a4e1fd3SJoe Perches			# function pointer declarations
24945a4e1fd3SJoe Perches		     $prevline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
24953f7bac03SJoe Perches			# foo bar; where foo is some local typedef or #define
24963f7bac03SJoe Perches		     $prevline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
24973f7bac03SJoe Perches			# known declaration macros
24983f7bac03SJoe Perches		     $prevline =~ /^\+\s+$declaration_macros/) &&
24993f7bac03SJoe Perches			# for "else if" which can look like "$Ident $Ident"
25003f7bac03SJoe Perches		    !($prevline =~ /^\+\s+$c90_Keywords\b/ ||
25013f7bac03SJoe Perches			# other possible extensions of declaration lines
25023f7bac03SJoe Perches		      $prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ ||
25033f7bac03SJoe Perches			# not starting a section or a macro "\" extended line
25043f7bac03SJoe Perches		      $prevline =~ /(?:\{\s*|\\)$/) &&
25053f7bac03SJoe Perches			# looks like a declaration
25063f7bac03SJoe Perches		    !($sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
25075a4e1fd3SJoe Perches			# function pointer declarations
25085a4e1fd3SJoe Perches		      $sline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
25093f7bac03SJoe Perches			# foo bar; where foo is some local typedef or #define
25103f7bac03SJoe Perches		      $sline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
25113f7bac03SJoe Perches			# known declaration macros
25123f7bac03SJoe Perches		      $sline =~ /^\+\s+$declaration_macros/ ||
25133f7bac03SJoe Perches			# start of struct or union or enum
25143b617e3bSJoe Perches		      $sline =~ /^\+\s+(?:union|struct|enum|typedef)\b/ ||
25153f7bac03SJoe Perches			# start or end of block or continuation of declaration
25163f7bac03SJoe Perches		      $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ ||
25173f7bac03SJoe Perches			# bitfield continuation
25183f7bac03SJoe Perches		      $sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ ||
25193f7bac03SJoe Perches			# other possible extensions of declaration lines
25203f7bac03SJoe Perches		      $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) &&
25213f7bac03SJoe Perches			# indentation of previous and current line are the same
25223f7bac03SJoe Perches		    (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) {
2523d752fcc8SJoe Perches			if (WARN("LINE_SPACING",
2524d752fcc8SJoe Perches				 "Missing a blank line after declarations\n" . $hereprev) &&
2525d752fcc8SJoe Perches			    $fix) {
2526f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, "\+");
2527d752fcc8SJoe Perches			}
25283b617e3bSJoe Perches		}
25293b617e3bSJoe Perches
25305f7ddae6SRaffaele Recalcati# check for spaces at the beginning of a line.
25316b4c5bebSAndy Whitcroft# Exceptions:
25326b4c5bebSAndy Whitcroft#  1) within comments
25336b4c5bebSAndy Whitcroft#  2) indented preprocessor commands
25346b4c5bebSAndy Whitcroft#  3) hanging labels
25353705ce5bSJoe Perches		if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/)  {
25365f7ddae6SRaffaele Recalcati			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
25373705ce5bSJoe Perches			if (WARN("LEADING_SPACE",
25383705ce5bSJoe Perches				 "please, no spaces at the start of a line\n" . $herevet) &&
25393705ce5bSJoe Perches			    $fix) {
2540194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
25413705ce5bSJoe Perches			}
25425f7ddae6SRaffaele Recalcati		}
25435f7ddae6SRaffaele Recalcati
2544b9ea10d6SAndy Whitcroft# check we are in a valid C source file if not then ignore this hunk
2545b9ea10d6SAndy Whitcroft		next if ($realfile !~ /\.(h|c)$/);
2546b9ea10d6SAndy Whitcroft
2547032a4c0fSJoe Perches# check indentation of any line with a bare else
2548032a4c0fSJoe Perches# if the previous line is a break or return and is indented 1 tab more...
2549032a4c0fSJoe Perches		if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {
2550032a4c0fSJoe Perches			my $tabs = length($1) + 1;
2551032a4c0fSJoe Perches			if ($prevline =~ /^\+\t{$tabs,$tabs}(?:break|return)\b/) {
2552032a4c0fSJoe Perches				WARN("UNNECESSARY_ELSE",
2553032a4c0fSJoe Perches				     "else is not generally useful after a break or return\n" . $hereprev);
2554032a4c0fSJoe Perches			}
2555032a4c0fSJoe Perches		}
2556032a4c0fSJoe Perches
2557c00df19aSJoe Perches# check indentation of a line with a break;
2558c00df19aSJoe Perches# if the previous line is a goto or return and is indented the same # of tabs
2559c00df19aSJoe Perches		if ($sline =~ /^\+([\t]+)break\s*;\s*$/) {
2560c00df19aSJoe Perches			my $tabs = $1;
2561c00df19aSJoe Perches			if ($prevline =~ /^\+$tabs(?:goto|return)\b/) {
2562c00df19aSJoe Perches				WARN("UNNECESSARY_BREAK",
2563c00df19aSJoe Perches				     "break is not useful after a goto or return\n" . $hereprev);
2564c00df19aSJoe Perches			}
2565c00df19aSJoe Perches		}
2566c00df19aSJoe Perches
25671ba8dfd1SKees Cook# discourage the addition of CONFIG_EXPERIMENTAL in #if(def).
25681ba8dfd1SKees Cook		if ($line =~ /^\+\s*\#\s*if.*\bCONFIG_EXPERIMENTAL\b/) {
25691ba8dfd1SKees Cook			WARN("CONFIG_EXPERIMENTAL",
25701ba8dfd1SKees Cook			     "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
25711ba8dfd1SKees Cook		}
25721ba8dfd1SKees Cook
2573c2fdda0dSAndy Whitcroft# check for RCS/CVS revision markers
2574cf655043SAndy Whitcroft		if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
2575000d1cc1SJoe Perches			WARN("CVS_KEYWORD",
2576000d1cc1SJoe Perches			     "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
2577c2fdda0dSAndy Whitcroft		}
257822f2a2efSAndy Whitcroft
257942e41c54SMike Frysinger# Blackfin: don't use __builtin_bfin_[cs]sync
258042e41c54SMike Frysinger		if ($line =~ /__builtin_bfin_csync/) {
258142e41c54SMike Frysinger			my $herevet = "$here\n" . cat_vet($line) . "\n";
2582000d1cc1SJoe Perches			ERROR("CSYNC",
2583000d1cc1SJoe Perches			      "use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
258442e41c54SMike Frysinger		}
258542e41c54SMike Frysinger		if ($line =~ /__builtin_bfin_ssync/) {
258642e41c54SMike Frysinger			my $herevet = "$here\n" . cat_vet($line) . "\n";
2587000d1cc1SJoe Perches			ERROR("SSYNC",
2588000d1cc1SJoe Perches			      "use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
258942e41c54SMike Frysinger		}
259042e41c54SMike Frysinger
259156e77d70SJoe Perches# check for old HOTPLUG __dev<foo> section markings
259256e77d70SJoe Perches		if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
259356e77d70SJoe Perches			WARN("HOTPLUG_SECTION",
259456e77d70SJoe Perches			     "Using $1 is unnecessary\n" . $herecurr);
259556e77d70SJoe Perches		}
259656e77d70SJoe Perches
25979c0ca6f9SAndy Whitcroft# Check for potential 'bare' types
25982b474a1aSAndy Whitcroft		my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
25992b474a1aSAndy Whitcroft		    $realline_next);
26003e469cdcSAndy Whitcroft#print "LINE<$line>\n";
26013e469cdcSAndy Whitcroft		if ($linenr >= $suppress_statement &&
26021b5539b1SJoe Perches		    $realcnt && $sline =~ /.\s*\S/) {
2603170d3a22SAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
2604f5fe35ddSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0);
2605171ae1a4SAndy Whitcroft			$stat =~ s/\n./\n /g;
2606171ae1a4SAndy Whitcroft			$cond =~ s/\n./\n /g;
2607171ae1a4SAndy Whitcroft
26083e469cdcSAndy Whitcroft#print "linenr<$linenr> <$stat>\n";
26093e469cdcSAndy Whitcroft			# If this statement has no statement boundaries within
26103e469cdcSAndy Whitcroft			# it there is no point in retrying a statement scan
26113e469cdcSAndy Whitcroft			# until we hit end of it.
26123e469cdcSAndy Whitcroft			my $frag = $stat; $frag =~ s/;+\s*$//;
26133e469cdcSAndy Whitcroft			if ($frag !~ /(?:{|;)/) {
26143e469cdcSAndy Whitcroft#print "skip<$line_nr_next>\n";
26153e469cdcSAndy Whitcroft				$suppress_statement = $line_nr_next;
26163e469cdcSAndy Whitcroft			}
2617f74bd194SAndy Whitcroft
26182b474a1aSAndy Whitcroft			# Find the real next line.
26192b474a1aSAndy Whitcroft			$realline_next = $line_nr_next;
26202b474a1aSAndy Whitcroft			if (defined $realline_next &&
26212b474a1aSAndy Whitcroft			    (!defined $lines[$realline_next - 1] ||
26222b474a1aSAndy Whitcroft			     substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
26232b474a1aSAndy Whitcroft				$realline_next++;
26242b474a1aSAndy Whitcroft			}
26252b474a1aSAndy Whitcroft
2626171ae1a4SAndy Whitcroft			my $s = $stat;
2627171ae1a4SAndy Whitcroft			$s =~ s/{.*$//s;
2628cf655043SAndy Whitcroft
2629c2fdda0dSAndy Whitcroft			# Ignore goto labels.
2630171ae1a4SAndy Whitcroft			if ($s =~ /$Ident:\*$/s) {
2631c2fdda0dSAndy Whitcroft
2632c2fdda0dSAndy Whitcroft			# Ignore functions being called
2633171ae1a4SAndy Whitcroft			} elsif ($s =~ /^.\s*$Ident\s*\(/s) {
2634c2fdda0dSAndy Whitcroft
2635463f2864SAndy Whitcroft			} elsif ($s =~ /^.\s*else\b/s) {
2636463f2864SAndy Whitcroft
2637c45dcabdSAndy Whitcroft			# declarations always start with types
2638d2506586SAndy 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) {
2639c45dcabdSAndy Whitcroft				my $type = $1;
2640c45dcabdSAndy Whitcroft				$type =~ s/\s+/ /g;
2641c45dcabdSAndy Whitcroft				possible($type, "A:" . $s);
2642c45dcabdSAndy Whitcroft
26436c72ffaaSAndy Whitcroft			# definitions in global scope can only start with types
2644a6a84062SAndy Whitcroft			} elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
2645c45dcabdSAndy Whitcroft				possible($1, "B:" . $s);
2646c2fdda0dSAndy Whitcroft			}
26478905a67cSAndy Whitcroft
26486c72ffaaSAndy Whitcroft			# any (foo ... *) is a pointer cast, and foo is a type
264965863862SAndy Whitcroft			while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
2650c45dcabdSAndy Whitcroft				possible($1, "C:" . $s);
26519c0ca6f9SAndy Whitcroft			}
26528905a67cSAndy Whitcroft
26538905a67cSAndy Whitcroft			# Check for any sort of function declaration.
26548905a67cSAndy Whitcroft			# int foo(something bar, other baz);
26558905a67cSAndy Whitcroft			# void (*store_gdt)(x86_descr_ptr *);
2656171ae1a4SAndy Whitcroft			if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
26578905a67cSAndy Whitcroft				my ($name_len) = length($1);
26588905a67cSAndy Whitcroft
2659cf655043SAndy Whitcroft				my $ctx = $s;
2660773647a0SAndy Whitcroft				substr($ctx, 0, $name_len + 1, '');
26618905a67cSAndy Whitcroft				$ctx =~ s/\)[^\)]*$//;
2662cf655043SAndy Whitcroft
26638905a67cSAndy Whitcroft				for my $arg (split(/\s*,\s*/, $ctx)) {
2664c45dcabdSAndy Whitcroft					if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
26658905a67cSAndy Whitcroft
2666c45dcabdSAndy Whitcroft						possible($1, "D:" . $s);
26678905a67cSAndy Whitcroft					}
26688905a67cSAndy Whitcroft				}
26698905a67cSAndy Whitcroft			}
26708905a67cSAndy Whitcroft
26719c0ca6f9SAndy Whitcroft		}
26729c0ca6f9SAndy Whitcroft
267300df344fSAndy Whitcroft#
267400df344fSAndy Whitcroft# Checks which may be anchored in the context.
267500df344fSAndy Whitcroft#
267600df344fSAndy Whitcroft
267700df344fSAndy Whitcroft# Check for switch () and associated case and default
267800df344fSAndy Whitcroft# statements should be at the same indent.
267900df344fSAndy Whitcroft		if ($line=~/\bswitch\s*\(.*\)/) {
268000df344fSAndy Whitcroft			my $err = '';
268100df344fSAndy Whitcroft			my $sep = '';
268200df344fSAndy Whitcroft			my @ctx = ctx_block_outer($linenr, $realcnt);
268300df344fSAndy Whitcroft			shift(@ctx);
268400df344fSAndy Whitcroft			for my $ctx (@ctx) {
268500df344fSAndy Whitcroft				my ($clen, $cindent) = line_stats($ctx);
268600df344fSAndy Whitcroft				if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
268700df344fSAndy Whitcroft							$indent != $cindent) {
268800df344fSAndy Whitcroft					$err .= "$sep$ctx\n";
268900df344fSAndy Whitcroft					$sep = '';
269000df344fSAndy Whitcroft				} else {
269100df344fSAndy Whitcroft					$sep = "[...]\n";
269200df344fSAndy Whitcroft				}
269300df344fSAndy Whitcroft			}
269400df344fSAndy Whitcroft			if ($err ne '') {
2695000d1cc1SJoe Perches				ERROR("SWITCH_CASE_INDENT_LEVEL",
2696000d1cc1SJoe Perches				      "switch and case should be at the same indent\n$hereline$err");
2697de7d4f0eSAndy Whitcroft			}
2698de7d4f0eSAndy Whitcroft		}
2699de7d4f0eSAndy Whitcroft
2700de7d4f0eSAndy Whitcroft# if/while/etc brace do not go on next line, unless defining a do while loop,
2701de7d4f0eSAndy Whitcroft# or if that brace on the next line is for something else
27020fe3dc2bSJoe Perches		if ($line =~ /(.*)\b((?:if|while|for|switch|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
2703773647a0SAndy Whitcroft			my $pre_ctx = "$1$2";
2704773647a0SAndy Whitcroft
27059c0ca6f9SAndy Whitcroft			my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
27068eef05ddSJoe Perches
27078eef05ddSJoe Perches			if ($line =~ /^\+\t{6,}/) {
27088eef05ddSJoe Perches				WARN("DEEP_INDENTATION",
27098eef05ddSJoe Perches				     "Too many leading tabs - consider code refactoring\n" . $herecurr);
27108eef05ddSJoe Perches			}
27118eef05ddSJoe Perches
2712de7d4f0eSAndy Whitcroft			my $ctx_cnt = $realcnt - $#ctx - 1;
2713de7d4f0eSAndy Whitcroft			my $ctx = join("\n", @ctx);
2714de7d4f0eSAndy Whitcroft
2715548596d5SAndy Whitcroft			my $ctx_ln = $linenr;
2716548596d5SAndy Whitcroft			my $ctx_skip = $realcnt;
2717de7d4f0eSAndy Whitcroft
2718548596d5SAndy Whitcroft			while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
2719548596d5SAndy Whitcroft					defined $lines[$ctx_ln - 1] &&
2720548596d5SAndy Whitcroft					$lines[$ctx_ln - 1] =~ /^-/)) {
2721548596d5SAndy Whitcroft				##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
2722548596d5SAndy Whitcroft				$ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
2723773647a0SAndy Whitcroft				$ctx_ln++;
2724773647a0SAndy Whitcroft			}
2725548596d5SAndy Whitcroft
272653210168SAndy Whitcroft			#print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
272753210168SAndy Whitcroft			#print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
2728773647a0SAndy Whitcroft
2729773647a0SAndy Whitcroft			if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
2730000d1cc1SJoe Perches				ERROR("OPEN_BRACE",
2731000d1cc1SJoe Perches				      "that open brace { should be on the previous line\n" .
273201464f30SAndy Whitcroft					"$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
273300df344fSAndy Whitcroft			}
2734773647a0SAndy Whitcroft			if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
2735773647a0SAndy Whitcroft			    $ctx =~ /\)\s*\;\s*$/ &&
2736773647a0SAndy Whitcroft			    defined $lines[$ctx_ln - 1])
2737773647a0SAndy Whitcroft			{
27389c0ca6f9SAndy Whitcroft				my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
27399c0ca6f9SAndy Whitcroft				if ($nindent > $indent) {
2740000d1cc1SJoe Perches					WARN("TRAILING_SEMICOLON",
2741000d1cc1SJoe Perches					     "trailing semicolon indicates no statements, indent implies otherwise\n" .
274201464f30SAndy Whitcroft						"$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
27439c0ca6f9SAndy Whitcroft				}
27449c0ca6f9SAndy Whitcroft			}
274500df344fSAndy Whitcroft		}
274600df344fSAndy Whitcroft
27474d001e4dSAndy Whitcroft# Check relative indent for conditionals and blocks.
27480fe3dc2bSJoe Perches		if ($line =~ /\b(?:(?:if|while|for|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
27493e469cdcSAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
27503e469cdcSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0)
27513e469cdcSAndy Whitcroft					if (!defined $stat);
27524d001e4dSAndy Whitcroft			my ($s, $c) = ($stat, $cond);
27534d001e4dSAndy Whitcroft
27544d001e4dSAndy Whitcroft			substr($s, 0, length($c), '');
27554d001e4dSAndy Whitcroft
27564d001e4dSAndy Whitcroft			# Make sure we remove the line prefixes as we have
27574d001e4dSAndy Whitcroft			# none on the first line, and are going to readd them
27584d001e4dSAndy Whitcroft			# where necessary.
27594d001e4dSAndy Whitcroft			$s =~ s/\n./\n/gs;
27604d001e4dSAndy Whitcroft
27614d001e4dSAndy Whitcroft			# Find out how long the conditional actually is.
27626f779c18SAndy Whitcroft			my @newlines = ($c =~ /\n/gs);
27636f779c18SAndy Whitcroft			my $cond_lines = 1 + $#newlines;
27644d001e4dSAndy Whitcroft
27654d001e4dSAndy Whitcroft			# We want to check the first line inside the block
27664d001e4dSAndy Whitcroft			# starting at the end of the conditional, so remove:
27674d001e4dSAndy Whitcroft			#  1) any blank line termination
27684d001e4dSAndy Whitcroft			#  2) any opening brace { on end of the line
27694d001e4dSAndy Whitcroft			#  3) any do (...) {
27704d001e4dSAndy Whitcroft			my $continuation = 0;
27714d001e4dSAndy Whitcroft			my $check = 0;
27724d001e4dSAndy Whitcroft			$s =~ s/^.*\bdo\b//;
27734d001e4dSAndy Whitcroft			$s =~ s/^\s*{//;
27744d001e4dSAndy Whitcroft			if ($s =~ s/^\s*\\//) {
27754d001e4dSAndy Whitcroft				$continuation = 1;
27764d001e4dSAndy Whitcroft			}
27779bd49efeSAndy Whitcroft			if ($s =~ s/^\s*?\n//) {
27784d001e4dSAndy Whitcroft				$check = 1;
27794d001e4dSAndy Whitcroft				$cond_lines++;
27804d001e4dSAndy Whitcroft			}
27814d001e4dSAndy Whitcroft
27824d001e4dSAndy Whitcroft			# Also ignore a loop construct at the end of a
27834d001e4dSAndy Whitcroft			# preprocessor statement.
27844d001e4dSAndy Whitcroft			if (($prevline =~ /^.\s*#\s*define\s/ ||
27854d001e4dSAndy Whitcroft			    $prevline =~ /\\\s*$/) && $continuation == 0) {
27864d001e4dSAndy Whitcroft				$check = 0;
27874d001e4dSAndy Whitcroft			}
27884d001e4dSAndy Whitcroft
27899bd49efeSAndy Whitcroft			my $cond_ptr = -1;
2790740504c6SAndy Whitcroft			$continuation = 0;
27919bd49efeSAndy Whitcroft			while ($cond_ptr != $cond_lines) {
27929bd49efeSAndy Whitcroft				$cond_ptr = $cond_lines;
27934d001e4dSAndy Whitcroft
2794f16fa28fSAndy Whitcroft				# If we see an #else/#elif then the code
2795f16fa28fSAndy Whitcroft				# is not linear.
2796f16fa28fSAndy Whitcroft				if ($s =~ /^\s*\#\s*(?:else|elif)/) {
2797f16fa28fSAndy Whitcroft					$check = 0;
2798f16fa28fSAndy Whitcroft				}
2799f16fa28fSAndy Whitcroft
28009bd49efeSAndy Whitcroft				# Ignore:
28019bd49efeSAndy Whitcroft				#  1) blank lines, they should be at 0,
28029bd49efeSAndy Whitcroft				#  2) preprocessor lines, and
28039bd49efeSAndy Whitcroft				#  3) labels.
2804740504c6SAndy Whitcroft				if ($continuation ||
2805740504c6SAndy Whitcroft				    $s =~ /^\s*?\n/ ||
28069bd49efeSAndy Whitcroft				    $s =~ /^\s*#\s*?/ ||
28079bd49efeSAndy Whitcroft				    $s =~ /^\s*$Ident\s*:/) {
2808740504c6SAndy Whitcroft					$continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
280930dad6ebSAndy Whitcroft					if ($s =~ s/^.*?\n//) {
28109bd49efeSAndy Whitcroft						$cond_lines++;
28119bd49efeSAndy Whitcroft					}
28124d001e4dSAndy Whitcroft				}
281330dad6ebSAndy Whitcroft			}
28144d001e4dSAndy Whitcroft
28154d001e4dSAndy Whitcroft			my (undef, $sindent) = line_stats("+" . $s);
28164d001e4dSAndy Whitcroft			my $stat_real = raw_line($linenr, $cond_lines);
28174d001e4dSAndy Whitcroft
28184d001e4dSAndy Whitcroft			# Check if either of these lines are modified, else
28194d001e4dSAndy Whitcroft			# this is not this patch's fault.
28204d001e4dSAndy Whitcroft			if (!defined($stat_real) ||
28214d001e4dSAndy Whitcroft			    $stat !~ /^\+/ && $stat_real !~ /^\+/) {
28224d001e4dSAndy Whitcroft				$check = 0;
28234d001e4dSAndy Whitcroft			}
28244d001e4dSAndy Whitcroft			if (defined($stat_real) && $cond_lines > 1) {
28254d001e4dSAndy Whitcroft				$stat_real = "[...]\n$stat_real";
28264d001e4dSAndy Whitcroft			}
28274d001e4dSAndy Whitcroft
28289bd49efeSAndy 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";
28294d001e4dSAndy Whitcroft
28304d001e4dSAndy Whitcroft			if ($check && (($sindent % 8) != 0 ||
28314d001e4dSAndy Whitcroft			    ($sindent <= $indent && $s ne ''))) {
2832000d1cc1SJoe Perches				WARN("SUSPECT_CODE_INDENT",
2833000d1cc1SJoe Perches				     "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
28344d001e4dSAndy Whitcroft			}
28354d001e4dSAndy Whitcroft		}
28364d001e4dSAndy Whitcroft
28376c72ffaaSAndy Whitcroft		# Track the 'values' across context and added lines.
28386c72ffaaSAndy Whitcroft		my $opline = $line; $opline =~ s/^./ /;
28391f65f947SAndy Whitcroft		my ($curr_values, $curr_vars) =
28401f65f947SAndy Whitcroft				annotate_values($opline . "\n", $prev_values);
28416c72ffaaSAndy Whitcroft		$curr_values = $prev_values . $curr_values;
2842c2fdda0dSAndy Whitcroft		if ($dbg_values) {
2843c2fdda0dSAndy Whitcroft			my $outline = $opline; $outline =~ s/\t/ /g;
2844cf655043SAndy Whitcroft			print "$linenr > .$outline\n";
2845cf655043SAndy Whitcroft			print "$linenr > $curr_values\n";
28461f65f947SAndy Whitcroft			print "$linenr >  $curr_vars\n";
2847c2fdda0dSAndy Whitcroft		}
28486c72ffaaSAndy Whitcroft		$prev_values = substr($curr_values, -1);
28496c72ffaaSAndy Whitcroft
285000df344fSAndy Whitcroft#ignore lines not being added
28513705ce5bSJoe Perches		next if ($line =~ /^[^\+]/);
285200df344fSAndy Whitcroft
2853653d4876SAndy Whitcroft# TEST: allow direct testing of the type matcher.
28547429c690SAndy Whitcroft		if ($dbg_type) {
28557429c690SAndy Whitcroft			if ($line =~ /^.\s*$Declare\s*$/) {
2856000d1cc1SJoe Perches				ERROR("TEST_TYPE",
2857000d1cc1SJoe Perches				      "TEST: is type\n" . $herecurr);
28587429c690SAndy Whitcroft			} elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
2859000d1cc1SJoe Perches				ERROR("TEST_NOT_TYPE",
2860000d1cc1SJoe Perches				      "TEST: is not type ($1 is)\n". $herecurr);
28617429c690SAndy Whitcroft			}
2862653d4876SAndy Whitcroft			next;
2863653d4876SAndy Whitcroft		}
2864a1ef277eSAndy Whitcroft# TEST: allow direct testing of the attribute matcher.
2865a1ef277eSAndy Whitcroft		if ($dbg_attr) {
28669360b0e5SAndy Whitcroft			if ($line =~ /^.\s*$Modifier\s*$/) {
2867000d1cc1SJoe Perches				ERROR("TEST_ATTR",
2868000d1cc1SJoe Perches				      "TEST: is attr\n" . $herecurr);
28699360b0e5SAndy Whitcroft			} elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
2870000d1cc1SJoe Perches				ERROR("TEST_NOT_ATTR",
2871000d1cc1SJoe Perches				      "TEST: is not attr ($1 is)\n". $herecurr);
2872a1ef277eSAndy Whitcroft			}
2873a1ef277eSAndy Whitcroft			next;
2874a1ef277eSAndy Whitcroft		}
2875653d4876SAndy Whitcroft
2876f0a594c1SAndy Whitcroft# check for initialisation to aggregates open brace on the next line
287799423c20SAndy Whitcroft		if ($line =~ /^.\s*{/ &&
287899423c20SAndy Whitcroft		    $prevline =~ /(?:^|[^=])=\s*$/) {
2879d752fcc8SJoe Perches			if (ERROR("OPEN_BRACE",
2880d752fcc8SJoe Perches				  "that open brace { should be on the previous line\n" . $hereprev) &&
2881f2d7e4d4SJoe Perches			    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
2882f2d7e4d4SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
2883f2d7e4d4SJoe Perches				fix_delete_line($fixlinenr, $rawline);
2884d752fcc8SJoe Perches				my $fixedline = $prevrawline;
2885d752fcc8SJoe Perches				$fixedline =~ s/\s*=\s*$/ = {/;
2886f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
2887d752fcc8SJoe Perches				$fixedline = $line;
2888d752fcc8SJoe Perches				$fixedline =~ s/^(.\s*){\s*/$1/;
2889f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
2890d752fcc8SJoe Perches			}
2891f0a594c1SAndy Whitcroft		}
2892f0a594c1SAndy Whitcroft
289300df344fSAndy Whitcroft#
289400df344fSAndy Whitcroft# Checks which are anchored on the added line.
289500df344fSAndy Whitcroft#
289600df344fSAndy Whitcroft
2897653d4876SAndy Whitcroft# check for malformed paths in #include statements (uses RAW line)
2898c45dcabdSAndy Whitcroft		if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
2899653d4876SAndy Whitcroft			my $path = $1;
2900653d4876SAndy Whitcroft			if ($path =~ m{//}) {
2901000d1cc1SJoe Perches				ERROR("MALFORMED_INCLUDE",
2902495e9d84SJoe Perches				      "malformed #include filename\n" . $herecurr);
2903495e9d84SJoe Perches			}
2904495e9d84SJoe Perches			if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
2905495e9d84SJoe Perches				ERROR("UAPI_INCLUDE",
2906495e9d84SJoe Perches				      "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
2907653d4876SAndy Whitcroft			}
2908653d4876SAndy Whitcroft		}
2909653d4876SAndy Whitcroft
291000df344fSAndy Whitcroft# no C99 // comments
291100df344fSAndy Whitcroft		if ($line =~ m{//}) {
29123705ce5bSJoe Perches			if (ERROR("C99_COMMENTS",
29133705ce5bSJoe Perches				  "do not use C99 // comments\n" . $herecurr) &&
29143705ce5bSJoe Perches			    $fix) {
2915194f66fcSJoe Perches				my $line = $fixed[$fixlinenr];
29163705ce5bSJoe Perches				if ($line =~ /\/\/(.*)$/) {
29173705ce5bSJoe Perches					my $comment = trim($1);
2918194f66fcSJoe Perches					$fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@;
29193705ce5bSJoe Perches				}
29203705ce5bSJoe Perches			}
292100df344fSAndy Whitcroft		}
292200df344fSAndy Whitcroft		# Remove C99 comments.
29230a920b5bSAndy Whitcroft		$line =~ s@//.*@@;
29246c72ffaaSAndy Whitcroft		$opline =~ s@//.*@@;
29250a920b5bSAndy Whitcroft
29262b474a1aSAndy Whitcroft# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
29272b474a1aSAndy Whitcroft# the whole statement.
29282b474a1aSAndy Whitcroft#print "APW <$lines[$realline_next - 1]>\n";
29292b474a1aSAndy Whitcroft		if (defined $realline_next &&
29302b474a1aSAndy Whitcroft		    exists $lines[$realline_next - 1] &&
29312b474a1aSAndy Whitcroft		    !defined $suppress_export{$realline_next} &&
29322b474a1aSAndy Whitcroft		    ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
29332b474a1aSAndy Whitcroft		     $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
29343cbf62dfSAndy Whitcroft			# Handle definitions which produce identifiers with
29353cbf62dfSAndy Whitcroft			# a prefix:
29363cbf62dfSAndy Whitcroft			#   XXX(foo);
29373cbf62dfSAndy Whitcroft			#   EXPORT_SYMBOL(something_foo);
2938653d4876SAndy Whitcroft			my $name = $1;
293987a53877SAndy Whitcroft			if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
29403cbf62dfSAndy Whitcroft			    $name =~ /^${Ident}_$2/) {
29413cbf62dfSAndy Whitcroft#print "FOO C name<$name>\n";
29423cbf62dfSAndy Whitcroft				$suppress_export{$realline_next} = 1;
29433cbf62dfSAndy Whitcroft
29443cbf62dfSAndy Whitcroft			} elsif ($stat !~ /(?:
29452b474a1aSAndy Whitcroft				\n.}\s*$|
294648012058SAndy Whitcroft				^.DEFINE_$Ident\(\Q$name\E\)|
294748012058SAndy Whitcroft				^.DECLARE_$Ident\(\Q$name\E\)|
294848012058SAndy Whitcroft				^.LIST_HEAD\(\Q$name\E\)|
29492b474a1aSAndy Whitcroft				^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
29502b474a1aSAndy Whitcroft				\b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
295148012058SAndy Whitcroft			    )/x) {
29522b474a1aSAndy Whitcroft#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
29532b474a1aSAndy Whitcroft				$suppress_export{$realline_next} = 2;
29542b474a1aSAndy Whitcroft			} else {
29552b474a1aSAndy Whitcroft				$suppress_export{$realline_next} = 1;
29560a920b5bSAndy Whitcroft			}
29570a920b5bSAndy Whitcroft		}
29582b474a1aSAndy Whitcroft		if (!defined $suppress_export{$linenr} &&
29592b474a1aSAndy Whitcroft		    $prevline =~ /^.\s*$/ &&
29602b474a1aSAndy Whitcroft		    ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
29612b474a1aSAndy Whitcroft		     $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
29622b474a1aSAndy Whitcroft#print "FOO B <$lines[$linenr - 1]>\n";
29632b474a1aSAndy Whitcroft			$suppress_export{$linenr} = 2;
29642b474a1aSAndy Whitcroft		}
29652b474a1aSAndy Whitcroft		if (defined $suppress_export{$linenr} &&
29662b474a1aSAndy Whitcroft		    $suppress_export{$linenr} == 2) {
2967000d1cc1SJoe Perches			WARN("EXPORT_SYMBOL",
2968000d1cc1SJoe Perches			     "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
29692b474a1aSAndy Whitcroft		}
29700a920b5bSAndy Whitcroft
29715150bda4SJoe Eloff# check for global initialisers.
2972d5e616fcSJoe Perches		if ($line =~ /^\+(\s*$Type\s*$Ident\s*(?:\s+$Modifier))*\s*=\s*(0|NULL|false)\s*;/) {
2973d5e616fcSJoe Perches			if (ERROR("GLOBAL_INITIALISERS",
2974000d1cc1SJoe Perches				  "do not initialise globals to 0 or NULL\n" .
2975d5e616fcSJoe Perches				      $herecurr) &&
2976d5e616fcSJoe Perches			    $fix) {
2977194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/($Type\s*$Ident\s*(?:\s+$Modifier))*\s*=\s*(0|NULL|false)\s*;/$1;/;
2978d5e616fcSJoe Perches			}
2979f0a594c1SAndy Whitcroft		}
29800a920b5bSAndy Whitcroft# check for static initialisers.
2981d5e616fcSJoe Perches		if ($line =~ /^\+.*\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
2982d5e616fcSJoe Perches			if (ERROR("INITIALISED_STATIC",
2983000d1cc1SJoe Perches				  "do not initialise statics to 0 or NULL\n" .
2984d5e616fcSJoe Perches				      $herecurr) &&
2985d5e616fcSJoe Perches			    $fix) {
2986194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*(0|NULL|false)\s*;/$1;/;
2987d5e616fcSJoe Perches			}
29880a920b5bSAndy Whitcroft		}
29890a920b5bSAndy Whitcroft
2990cb710ecaSJoe Perches# check for static const char * arrays.
2991cb710ecaSJoe Perches		if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
2992000d1cc1SJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
2993000d1cc1SJoe Perches			     "static const char * array should probably be static const char * const\n" .
2994cb710ecaSJoe Perches				$herecurr);
2995cb710ecaSJoe Perches               }
2996cb710ecaSJoe Perches
2997cb710ecaSJoe Perches# check for static char foo[] = "bar" declarations.
2998cb710ecaSJoe Perches		if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
2999000d1cc1SJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
3000000d1cc1SJoe Perches			     "static char array declaration should probably be static const char\n" .
3001cb710ecaSJoe Perches				$herecurr);
3002cb710ecaSJoe Perches               }
3003cb710ecaSJoe Perches
30049b0fa60dSJoe Perches# check for non-global char *foo[] = {"bar", ...} declarations.
30059b0fa60dSJoe Perches		if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) {
30069b0fa60dSJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
30079b0fa60dSJoe Perches			     "char * array declaration might be better as static const\n" .
30089b0fa60dSJoe Perches				$herecurr);
30099b0fa60dSJoe Perches               }
30109b0fa60dSJoe Perches
3011b36190c5SJoe Perches# check for function declarations without arguments like "int foo()"
3012b36190c5SJoe Perches		if ($line =~ /(\b$Type\s+$Ident)\s*\(\s*\)/) {
3013b36190c5SJoe Perches			if (ERROR("FUNCTION_WITHOUT_ARGS",
3014b36190c5SJoe Perches				  "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) &&
3015b36190c5SJoe Perches			    $fix) {
3016194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/;
3017b36190c5SJoe Perches			}
3018b36190c5SJoe Perches		}
3019b36190c5SJoe Perches
302092e112fdSJoe Perches# check for uses of DEFINE_PCI_DEVICE_TABLE
302192e112fdSJoe Perches		if ($line =~ /\bDEFINE_PCI_DEVICE_TABLE\s*\(\s*(\w+)\s*\)\s*=/) {
302292e112fdSJoe Perches			if (WARN("DEFINE_PCI_DEVICE_TABLE",
302392e112fdSJoe Perches				 "Prefer struct pci_device_id over deprecated DEFINE_PCI_DEVICE_TABLE\n" . $herecurr) &&
302492e112fdSJoe Perches			    $fix) {
3025194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b(?:static\s+|)DEFINE_PCI_DEVICE_TABLE\s*\(\s*(\w+)\s*\)\s*=\s*/static const struct pci_device_id $1\[\] = /;
302692e112fdSJoe Perches			}
302793ed0e2dSJoe Perches		}
302893ed0e2dSJoe Perches
3029653d4876SAndy Whitcroft# check for new typedefs, only function parameters and sparse annotations
3030653d4876SAndy Whitcroft# make sense.
3031653d4876SAndy Whitcroft		if ($line =~ /\btypedef\s/ &&
30328054576dSAndy Whitcroft		    $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
3033c45dcabdSAndy Whitcroft		    $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
30348ed22cadSAndy Whitcroft		    $line !~ /\b$typeTypedefs\b/ &&
3035653d4876SAndy Whitcroft		    $line !~ /\b__bitwise(?:__|)\b/) {
3036000d1cc1SJoe Perches			WARN("NEW_TYPEDEFS",
3037000d1cc1SJoe Perches			     "do not add new typedefs\n" . $herecurr);
30380a920b5bSAndy Whitcroft		}
30390a920b5bSAndy Whitcroft
30400a920b5bSAndy Whitcroft# * goes on variable not on type
304165863862SAndy Whitcroft		# (char*[ const])
3042bfcb2cc7SAndy Whitcroft		while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
3043bfcb2cc7SAndy Whitcroft			#print "AA<$1>\n";
30443705ce5bSJoe Perches			my ($ident, $from, $to) = ($1, $2, $2);
3045d8aaf121SAndy Whitcroft
304665863862SAndy Whitcroft			# Should start with a space.
304765863862SAndy Whitcroft			$to =~ s/^(\S)/ $1/;
304865863862SAndy Whitcroft			# Should not end with a space.
304965863862SAndy Whitcroft			$to =~ s/\s+$//;
305065863862SAndy Whitcroft			# '*'s should not have spaces between.
3051f9a0b3d1SAndy Whitcroft			while ($to =~ s/\*\s+\*/\*\*/) {
305265863862SAndy Whitcroft			}
3053d8aaf121SAndy Whitcroft
30543705ce5bSJoe Perches##			print "1: from<$from> to<$to> ident<$ident>\n";
305565863862SAndy Whitcroft			if ($from ne $to) {
30563705ce5bSJoe Perches				if (ERROR("POINTER_LOCATION",
30573705ce5bSJoe Perches					  "\"(foo$from)\" should be \"(foo$to)\"\n" .  $herecurr) &&
30583705ce5bSJoe Perches				    $fix) {
30593705ce5bSJoe Perches					my $sub_from = $ident;
30603705ce5bSJoe Perches					my $sub_to = $ident;
30613705ce5bSJoe Perches					$sub_to =~ s/\Q$from\E/$to/;
3062194f66fcSJoe Perches					$fixed[$fixlinenr] =~
30633705ce5bSJoe Perches					    s@\Q$sub_from\E@$sub_to@;
30643705ce5bSJoe Perches				}
306565863862SAndy Whitcroft			}
3066bfcb2cc7SAndy Whitcroft		}
3067bfcb2cc7SAndy Whitcroft		while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
3068bfcb2cc7SAndy Whitcroft			#print "BB<$1>\n";
30693705ce5bSJoe Perches			my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
3070d8aaf121SAndy Whitcroft
307165863862SAndy Whitcroft			# Should start with a space.
307265863862SAndy Whitcroft			$to =~ s/^(\S)/ $1/;
307365863862SAndy Whitcroft			# Should not end with a space.
307465863862SAndy Whitcroft			$to =~ s/\s+$//;
307565863862SAndy Whitcroft			# '*'s should not have spaces between.
3076f9a0b3d1SAndy Whitcroft			while ($to =~ s/\*\s+\*/\*\*/) {
307765863862SAndy Whitcroft			}
307865863862SAndy Whitcroft			# Modifiers should have spaces.
307965863862SAndy Whitcroft			$to =~ s/(\b$Modifier$)/$1 /;
308065863862SAndy Whitcroft
30813705ce5bSJoe Perches##			print "2: from<$from> to<$to> ident<$ident>\n";
3082667026e7SAndy Whitcroft			if ($from ne $to && $ident !~ /^$Modifier$/) {
30833705ce5bSJoe Perches				if (ERROR("POINTER_LOCATION",
30843705ce5bSJoe Perches					  "\"foo${from}bar\" should be \"foo${to}bar\"\n" .  $herecurr) &&
30853705ce5bSJoe Perches				    $fix) {
30863705ce5bSJoe Perches
30873705ce5bSJoe Perches					my $sub_from = $match;
30883705ce5bSJoe Perches					my $sub_to = $match;
30893705ce5bSJoe Perches					$sub_to =~ s/\Q$from\E/$to/;
3090194f66fcSJoe Perches					$fixed[$fixlinenr] =~
30913705ce5bSJoe Perches					    s@\Q$sub_from\E@$sub_to@;
30923705ce5bSJoe Perches				}
309365863862SAndy Whitcroft			}
30940a920b5bSAndy Whitcroft		}
30950a920b5bSAndy Whitcroft
30960a920b5bSAndy Whitcroft# # no BUG() or BUG_ON()
30970a920b5bSAndy Whitcroft# 		if ($line =~ /\b(BUG|BUG_ON)\b/) {
30980a920b5bSAndy Whitcroft# 			print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
30990a920b5bSAndy Whitcroft# 			print "$herecurr";
31000a920b5bSAndy Whitcroft# 			$clean = 0;
31010a920b5bSAndy Whitcroft# 		}
31020a920b5bSAndy Whitcroft
31038905a67cSAndy Whitcroft		if ($line =~ /\bLINUX_VERSION_CODE\b/) {
3104000d1cc1SJoe Perches			WARN("LINUX_VERSION_CODE",
3105000d1cc1SJoe Perches			     "LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
31068905a67cSAndy Whitcroft		}
31078905a67cSAndy Whitcroft
310817441227SJoe Perches# check for uses of printk_ratelimit
310917441227SJoe Perches		if ($line =~ /\bprintk_ratelimit\s*\(/) {
3110000d1cc1SJoe Perches			WARN("PRINTK_RATELIMITED",
3111000d1cc1SJoe Perches"Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
311217441227SJoe Perches		}
311317441227SJoe Perches
311400df344fSAndy Whitcroft# printk should use KERN_* levels.  Note that follow on printk's on the
311500df344fSAndy Whitcroft# same line do not need a level, so we use the current block context
311600df344fSAndy Whitcroft# to try and find and validate the current printk.  In summary the current
311725985edcSLucas De Marchi# printk includes all preceding printk's which have no newline on the end.
311800df344fSAndy Whitcroft# we assume the first bad printk is the one to report.
3119f0a594c1SAndy Whitcroft		if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
312000df344fSAndy Whitcroft			my $ok = 0;
312100df344fSAndy Whitcroft			for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
312200df344fSAndy Whitcroft				#print "CHECK<$lines[$ln - 1]\n";
312325985edcSLucas De Marchi				# we have a preceding printk if it ends
312400df344fSAndy Whitcroft				# with "\n" ignore it, else it is to blame
312500df344fSAndy Whitcroft				if ($lines[$ln - 1] =~ m{\bprintk\(}) {
312600df344fSAndy Whitcroft					if ($rawlines[$ln - 1] !~ m{\\n"}) {
312700df344fSAndy Whitcroft						$ok = 1;
312800df344fSAndy Whitcroft					}
312900df344fSAndy Whitcroft					last;
313000df344fSAndy Whitcroft				}
313100df344fSAndy Whitcroft			}
313200df344fSAndy Whitcroft			if ($ok == 0) {
3133000d1cc1SJoe Perches				WARN("PRINTK_WITHOUT_KERN_LEVEL",
3134000d1cc1SJoe Perches				     "printk() should include KERN_ facility level\n" . $herecurr);
31350a920b5bSAndy Whitcroft			}
313600df344fSAndy Whitcroft		}
31370a920b5bSAndy Whitcroft
3138243f3803SJoe Perches		if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
3139243f3803SJoe Perches			my $orig = $1;
3140243f3803SJoe Perches			my $level = lc($orig);
3141243f3803SJoe Perches			$level = "warn" if ($level eq "warning");
31428f26b837SJoe Perches			my $level2 = $level;
31438f26b837SJoe Perches			$level2 = "dbg" if ($level eq "debug");
3144243f3803SJoe Perches			WARN("PREFER_PR_LEVEL",
3145daa8b059SYogesh Chaudhari			     "Prefer [subsystem eg: netdev]_$level2([subsystem]dev, ... then dev_$level2(dev, ... then pr_$level(...  to printk(KERN_$orig ...\n" . $herecurr);
3146243f3803SJoe Perches		}
3147243f3803SJoe Perches
3148243f3803SJoe Perches		if ($line =~ /\bpr_warning\s*\(/) {
3149d5e616fcSJoe Perches			if (WARN("PREFER_PR_LEVEL",
3150d5e616fcSJoe Perches				 "Prefer pr_warn(... to pr_warning(...\n" . $herecurr) &&
3151d5e616fcSJoe Perches			    $fix) {
3152194f66fcSJoe Perches				$fixed[$fixlinenr] =~
3153d5e616fcSJoe Perches				    s/\bpr_warning\b/pr_warn/;
3154d5e616fcSJoe Perches			}
3155243f3803SJoe Perches		}
3156243f3803SJoe Perches
3157dc139313SJoe Perches		if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
3158dc139313SJoe Perches			my $orig = $1;
3159dc139313SJoe Perches			my $level = lc($orig);
3160dc139313SJoe Perches			$level = "warn" if ($level eq "warning");
3161dc139313SJoe Perches			$level = "dbg" if ($level eq "debug");
3162dc139313SJoe Perches			WARN("PREFER_DEV_LEVEL",
3163dc139313SJoe Perches			     "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
3164dc139313SJoe Perches		}
3165dc139313SJoe Perches
3166653d4876SAndy Whitcroft# function brace can't be on same line, except for #defines of do while,
3167653d4876SAndy Whitcroft# or if closed on same line
31688d182478SJoe Perches		if (($line=~/$Type\s*$Ident\(.*\).*\s*{/) and
3169c45dcabdSAndy Whitcroft		    !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) {
31708d182478SJoe Perches			if (ERROR("OPEN_BRACE",
31718d182478SJoe Perches				  "open brace '{' following function declarations go on the next line\n" . $herecurr) &&
31728d182478SJoe Perches			    $fix) {
31738d182478SJoe Perches				fix_delete_line($fixlinenr, $rawline);
31748d182478SJoe Perches				my $fixed_line = $rawline;
31758d182478SJoe Perches				$fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*){(.*)$/;
31768d182478SJoe Perches				my $line1 = $1;
31778d182478SJoe Perches				my $line2 = $2;
31788d182478SJoe Perches				fix_insert_line($fixlinenr, ltrim($line1));
31798d182478SJoe Perches				fix_insert_line($fixlinenr, "\+{");
31808d182478SJoe Perches				if ($line2 !~ /^\s*$/) {
31818d182478SJoe Perches					fix_insert_line($fixlinenr, "\+\t" . trim($line2));
31828d182478SJoe Perches				}
31838d182478SJoe Perches			}
31840a920b5bSAndy Whitcroft		}
3185653d4876SAndy Whitcroft
31868905a67cSAndy Whitcroft# open braces for enum, union and struct go on the same line.
31878905a67cSAndy Whitcroft		if ($line =~ /^.\s*{/ &&
31888905a67cSAndy Whitcroft		    $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
31898d182478SJoe Perches			if (ERROR("OPEN_BRACE",
31908d182478SJoe Perches				  "open brace '{' following $1 go on the same line\n" . $hereprev) &&
31918d182478SJoe Perches			    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
31928d182478SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
31938d182478SJoe Perches				fix_delete_line($fixlinenr, $rawline);
31948d182478SJoe Perches				my $fixedline = rtrim($prevrawline) . " {";
31958d182478SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
31968d182478SJoe Perches				$fixedline = $rawline;
31978d182478SJoe Perches				$fixedline =~ s/^(.\s*){\s*/$1\t/;
31988d182478SJoe Perches				if ($fixedline !~ /^\+\s*$/) {
31998d182478SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
32008d182478SJoe Perches				}
32018d182478SJoe Perches			}
32028905a67cSAndy Whitcroft		}
32038905a67cSAndy Whitcroft
32040c73b4ebSAndy Whitcroft# missing space after union, struct or enum definition
32053705ce5bSJoe Perches		if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
32063705ce5bSJoe Perches			if (WARN("SPACING",
32073705ce5bSJoe Perches				 "missing space after $1 definition\n" . $herecurr) &&
32083705ce5bSJoe Perches			    $fix) {
3209194f66fcSJoe Perches				$fixed[$fixlinenr] =~
32103705ce5bSJoe Perches				    s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
32113705ce5bSJoe Perches			}
32120c73b4ebSAndy Whitcroft		}
32130c73b4ebSAndy Whitcroft
321431070b5dSJoe Perches# Function pointer declarations
321531070b5dSJoe Perches# check spacing between type, funcptr, and args
321631070b5dSJoe Perches# canonical declaration is "type (*funcptr)(args...)"
321791f72e9cSJoe Perches		if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) {
321831070b5dSJoe Perches			my $declare = $1;
321931070b5dSJoe Perches			my $pre_pointer_space = $2;
322031070b5dSJoe Perches			my $post_pointer_space = $3;
322131070b5dSJoe Perches			my $funcname = $4;
322231070b5dSJoe Perches			my $post_funcname_space = $5;
322331070b5dSJoe Perches			my $pre_args_space = $6;
322431070b5dSJoe Perches
322591f72e9cSJoe Perches# the $Declare variable will capture all spaces after the type
322691f72e9cSJoe Perches# so check it for a missing trailing missing space but pointer return types
322791f72e9cSJoe Perches# don't need a space so don't warn for those.
322891f72e9cSJoe Perches			my $post_declare_space = "";
322991f72e9cSJoe Perches			if ($declare =~ /(\s+)$/) {
323091f72e9cSJoe Perches				$post_declare_space = $1;
323191f72e9cSJoe Perches				$declare = rtrim($declare);
323291f72e9cSJoe Perches			}
323391f72e9cSJoe Perches			if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) {
323431070b5dSJoe Perches				WARN("SPACING",
323531070b5dSJoe Perches				     "missing space after return type\n" . $herecurr);
323691f72e9cSJoe Perches				$post_declare_space = " ";
323731070b5dSJoe Perches			}
323831070b5dSJoe Perches
323931070b5dSJoe Perches# unnecessary space "type  (*funcptr)(args...)"
324091f72e9cSJoe Perches# This test is not currently implemented because these declarations are
324191f72e9cSJoe Perches# equivalent to
324291f72e9cSJoe Perches#	int  foo(int bar, ...)
324391f72e9cSJoe Perches# and this is form shouldn't/doesn't generate a checkpatch warning.
324491f72e9cSJoe Perches#
324591f72e9cSJoe Perches#			elsif ($declare =~ /\s{2,}$/) {
324691f72e9cSJoe Perches#				WARN("SPACING",
324791f72e9cSJoe Perches#				     "Multiple spaces after return type\n" . $herecurr);
324891f72e9cSJoe Perches#			}
324931070b5dSJoe Perches
325031070b5dSJoe Perches# unnecessary space "type ( *funcptr)(args...)"
325131070b5dSJoe Perches			if (defined $pre_pointer_space &&
325231070b5dSJoe Perches			    $pre_pointer_space =~ /^\s/) {
325331070b5dSJoe Perches				WARN("SPACING",
325431070b5dSJoe Perches				     "Unnecessary space after function pointer open parenthesis\n" . $herecurr);
325531070b5dSJoe Perches			}
325631070b5dSJoe Perches
325731070b5dSJoe Perches# unnecessary space "type (* funcptr)(args...)"
325831070b5dSJoe Perches			if (defined $post_pointer_space &&
325931070b5dSJoe Perches			    $post_pointer_space =~ /^\s/) {
326031070b5dSJoe Perches				WARN("SPACING",
326131070b5dSJoe Perches				     "Unnecessary space before function pointer name\n" . $herecurr);
326231070b5dSJoe Perches			}
326331070b5dSJoe Perches
326431070b5dSJoe Perches# unnecessary space "type (*funcptr )(args...)"
326531070b5dSJoe Perches			if (defined $post_funcname_space &&
326631070b5dSJoe Perches			    $post_funcname_space =~ /^\s/) {
326731070b5dSJoe Perches				WARN("SPACING",
326831070b5dSJoe Perches				     "Unnecessary space after function pointer name\n" . $herecurr);
326931070b5dSJoe Perches			}
327031070b5dSJoe Perches
327131070b5dSJoe Perches# unnecessary space "type (*funcptr) (args...)"
327231070b5dSJoe Perches			if (defined $pre_args_space &&
327331070b5dSJoe Perches			    $pre_args_space =~ /^\s/) {
327431070b5dSJoe Perches				WARN("SPACING",
327531070b5dSJoe Perches				     "Unnecessary space before function pointer arguments\n" . $herecurr);
327631070b5dSJoe Perches			}
327731070b5dSJoe Perches
327831070b5dSJoe Perches			if (show_type("SPACING") && $fix) {
3279194f66fcSJoe Perches				$fixed[$fixlinenr] =~
328091f72e9cSJoe Perches				    s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex;
328131070b5dSJoe Perches			}
328231070b5dSJoe Perches		}
328331070b5dSJoe Perches
32848d31cfceSAndy Whitcroft# check for spacing round square brackets; allowed:
32858d31cfceSAndy Whitcroft#  1. with a type on the left -- int [] a;
3286fe2a7dbcSAndy Whitcroft#  2. at the beginning of a line for slice initialisers -- [0...10] = 5,
3287fe2a7dbcSAndy Whitcroft#  3. inside a curly brace -- = { [0...10] = 5 }
32888d31cfceSAndy Whitcroft		while ($line =~ /(.*?\s)\[/g) {
32898d31cfceSAndy Whitcroft			my ($where, $prefix) = ($-[1], $1);
32908d31cfceSAndy Whitcroft			if ($prefix !~ /$Type\s+$/ &&
3291fe2a7dbcSAndy Whitcroft			    ($where != 0 || $prefix !~ /^.\s+$/) &&
3292daebc534SAndy Whitcroft			    $prefix !~ /[{,]\s+$/) {
32933705ce5bSJoe Perches				if (ERROR("BRACKET_SPACE",
32943705ce5bSJoe Perches					  "space prohibited before open square bracket '['\n" . $herecurr) &&
32953705ce5bSJoe Perches				    $fix) {
3296194f66fcSJoe Perches				    $fixed[$fixlinenr] =~
32973705ce5bSJoe Perches					s/^(\+.*?)\s+\[/$1\[/;
32983705ce5bSJoe Perches				}
32998d31cfceSAndy Whitcroft			}
33008d31cfceSAndy Whitcroft		}
33018d31cfceSAndy Whitcroft
3302f0a594c1SAndy Whitcroft# check for spaces between functions and their parentheses.
33036c72ffaaSAndy Whitcroft		while ($line =~ /($Ident)\s+\(/g) {
3304c2fdda0dSAndy Whitcroft			my $name = $1;
3305773647a0SAndy Whitcroft			my $ctx_before = substr($line, 0, $-[1]);
3306773647a0SAndy Whitcroft			my $ctx = "$ctx_before$name";
3307c2fdda0dSAndy Whitcroft
3308c2fdda0dSAndy Whitcroft			# Ignore those directives where spaces _are_ permitted.
3309773647a0SAndy Whitcroft			if ($name =~ /^(?:
3310773647a0SAndy Whitcroft				if|for|while|switch|return|case|
3311773647a0SAndy Whitcroft				volatile|__volatile__|
3312773647a0SAndy Whitcroft				__attribute__|format|__extension__|
3313773647a0SAndy Whitcroft				asm|__asm__)$/x)
3314773647a0SAndy Whitcroft			{
3315c2fdda0dSAndy Whitcroft			# cpp #define statements have non-optional spaces, ie
3316c2fdda0dSAndy Whitcroft			# if there is a space between the name and the open
3317c2fdda0dSAndy Whitcroft			# parenthesis it is simply not a parameter group.
3318c45dcabdSAndy Whitcroft			} elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
3319773647a0SAndy Whitcroft
3320773647a0SAndy Whitcroft			# cpp #elif statement condition may start with a (
3321c45dcabdSAndy Whitcroft			} elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
3322c2fdda0dSAndy Whitcroft
3323c2fdda0dSAndy Whitcroft			# If this whole things ends with a type its most
3324c2fdda0dSAndy Whitcroft			# likely a typedef for a function.
3325773647a0SAndy Whitcroft			} elsif ($ctx =~ /$Type$/) {
3326c2fdda0dSAndy Whitcroft
3327c2fdda0dSAndy Whitcroft			} else {
33283705ce5bSJoe Perches				if (WARN("SPACING",
33293705ce5bSJoe Perches					 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
33303705ce5bSJoe Perches					     $fix) {
3331194f66fcSJoe Perches					$fixed[$fixlinenr] =~
33323705ce5bSJoe Perches					    s/\b$name\s+\(/$name\(/;
33333705ce5bSJoe Perches				}
3334f0a594c1SAndy Whitcroft			}
33356c72ffaaSAndy Whitcroft		}
33369a4cad4eSEric Nelson
3337653d4876SAndy Whitcroft# Check operator spacing.
33380a920b5bSAndy Whitcroft		if (!($line=~/\#\s*include/)) {
33393705ce5bSJoe Perches			my $fixed_line = "";
33403705ce5bSJoe Perches			my $line_fixed = 0;
33413705ce5bSJoe Perches
33429c0ca6f9SAndy Whitcroft			my $ops = qr{
33439c0ca6f9SAndy Whitcroft				<<=|>>=|<=|>=|==|!=|
33449c0ca6f9SAndy Whitcroft				\+=|-=|\*=|\/=|%=|\^=|\|=|&=|
33459c0ca6f9SAndy Whitcroft				=>|->|<<|>>|<|>|=|!|~|
33461f65f947SAndy Whitcroft				&&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
334784731623SJoe Perches				\?:|\?|:
33489c0ca6f9SAndy Whitcroft			}x;
3349cf655043SAndy Whitcroft			my @elements = split(/($ops|;)/, $opline);
33503705ce5bSJoe Perches
33513705ce5bSJoe Perches##			print("element count: <" . $#elements . ">\n");
33523705ce5bSJoe Perches##			foreach my $el (@elements) {
33533705ce5bSJoe Perches##				print("el: <$el>\n");
33543705ce5bSJoe Perches##			}
33553705ce5bSJoe Perches
33563705ce5bSJoe Perches			my @fix_elements = ();
335700df344fSAndy Whitcroft			my $off = 0;
33586c72ffaaSAndy Whitcroft
33593705ce5bSJoe Perches			foreach my $el (@elements) {
33603705ce5bSJoe Perches				push(@fix_elements, substr($rawline, $off, length($el)));
33613705ce5bSJoe Perches				$off += length($el);
33623705ce5bSJoe Perches			}
33633705ce5bSJoe Perches
33643705ce5bSJoe Perches			$off = 0;
33653705ce5bSJoe Perches
33666c72ffaaSAndy Whitcroft			my $blank = copy_spacing($opline);
3367b34c648bSJoe Perches			my $last_after = -1;
33686c72ffaaSAndy Whitcroft
33690a920b5bSAndy Whitcroft			for (my $n = 0; $n < $#elements; $n += 2) {
33703705ce5bSJoe Perches
33713705ce5bSJoe Perches				my $good = $fix_elements[$n] . $fix_elements[$n + 1];
33723705ce5bSJoe Perches
33733705ce5bSJoe Perches##				print("n: <$n> good: <$good>\n");
33743705ce5bSJoe Perches
33754a0df2efSAndy Whitcroft				$off += length($elements[$n]);
33764a0df2efSAndy Whitcroft
337725985edcSLucas De Marchi				# Pick up the preceding and succeeding characters.
3378773647a0SAndy Whitcroft				my $ca = substr($opline, 0, $off);
3379773647a0SAndy Whitcroft				my $cc = '';
3380773647a0SAndy Whitcroft				if (length($opline) >= ($off + length($elements[$n + 1]))) {
3381773647a0SAndy Whitcroft					$cc = substr($opline, $off + length($elements[$n + 1]));
3382773647a0SAndy Whitcroft				}
3383773647a0SAndy Whitcroft				my $cb = "$ca$;$cc";
3384773647a0SAndy Whitcroft
33854a0df2efSAndy Whitcroft				my $a = '';
33864a0df2efSAndy Whitcroft				$a = 'V' if ($elements[$n] ne '');
33874a0df2efSAndy Whitcroft				$a = 'W' if ($elements[$n] =~ /\s$/);
3388cf655043SAndy Whitcroft				$a = 'C' if ($elements[$n] =~ /$;$/);
33894a0df2efSAndy Whitcroft				$a = 'B' if ($elements[$n] =~ /(\[|\()$/);
33904a0df2efSAndy Whitcroft				$a = 'O' if ($elements[$n] eq '');
3391773647a0SAndy Whitcroft				$a = 'E' if ($ca =~ /^\s*$/);
33924a0df2efSAndy Whitcroft
33930a920b5bSAndy Whitcroft				my $op = $elements[$n + 1];
33944a0df2efSAndy Whitcroft
33954a0df2efSAndy Whitcroft				my $c = '';
33960a920b5bSAndy Whitcroft				if (defined $elements[$n + 2]) {
33974a0df2efSAndy Whitcroft					$c = 'V' if ($elements[$n + 2] ne '');
33984a0df2efSAndy Whitcroft					$c = 'W' if ($elements[$n + 2] =~ /^\s/);
3399cf655043SAndy Whitcroft					$c = 'C' if ($elements[$n + 2] =~ /^$;/);
34004a0df2efSAndy Whitcroft					$c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
34014a0df2efSAndy Whitcroft					$c = 'O' if ($elements[$n + 2] eq '');
34028b1b3378SAndy Whitcroft					$c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
34034a0df2efSAndy Whitcroft				} else {
34044a0df2efSAndy Whitcroft					$c = 'E';
34050a920b5bSAndy Whitcroft				}
34060a920b5bSAndy Whitcroft
34074a0df2efSAndy Whitcroft				my $ctx = "${a}x${c}";
34084a0df2efSAndy Whitcroft
34094a0df2efSAndy Whitcroft				my $at = "(ctx:$ctx)";
34104a0df2efSAndy Whitcroft
34116c72ffaaSAndy Whitcroft				my $ptr = substr($blank, 0, $off) . "^";
3412de7d4f0eSAndy Whitcroft				my $hereptr = "$hereline$ptr\n";
34130a920b5bSAndy Whitcroft
341474048ed8SAndy Whitcroft				# Pull out the value of this operator.
34156c72ffaaSAndy Whitcroft				my $op_type = substr($curr_values, $off + 1, 1);
34160a920b5bSAndy Whitcroft
34171f65f947SAndy Whitcroft				# Get the full operator variant.
34181f65f947SAndy Whitcroft				my $opv = $op . substr($curr_vars, $off, 1);
34191f65f947SAndy Whitcroft
342013214adfSAndy Whitcroft				# Ignore operators passed as parameters.
342113214adfSAndy Whitcroft				if ($op_type ne 'V' &&
342213214adfSAndy Whitcroft				    $ca =~ /\s$/ && $cc =~ /^\s*,/) {
342313214adfSAndy Whitcroft
3424cf655043SAndy Whitcroft#				# Ignore comments
3425cf655043SAndy Whitcroft#				} elsif ($op =~ /^$;+$/) {
342613214adfSAndy Whitcroft
3427d8aaf121SAndy Whitcroft				# ; should have either the end of line or a space or \ after it
342813214adfSAndy Whitcroft				} elsif ($op eq ';') {
3429cf655043SAndy Whitcroft					if ($ctx !~ /.x[WEBC]/ &&
3430cf655043SAndy Whitcroft					    $cc !~ /^\\/ && $cc !~ /^;/) {
34313705ce5bSJoe Perches						if (ERROR("SPACING",
34323705ce5bSJoe Perches							  "space required after that '$op' $at\n" . $hereptr)) {
3433b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
34343705ce5bSJoe Perches							$line_fixed = 1;
34353705ce5bSJoe Perches						}
3436d8aaf121SAndy Whitcroft					}
3437d8aaf121SAndy Whitcroft
3438d8aaf121SAndy Whitcroft				# // is a comment
3439d8aaf121SAndy Whitcroft				} elsif ($op eq '//') {
34400a920b5bSAndy Whitcroft
3441b00e4814SJoe Perches				#   :   when part of a bitfield
3442b00e4814SJoe Perches				} elsif ($opv eq ':B') {
3443b00e4814SJoe Perches					# skip the bitfield test for now
3444b00e4814SJoe Perches
34451f65f947SAndy Whitcroft				# No spaces for:
34461f65f947SAndy Whitcroft				#   ->
3447b00e4814SJoe Perches				} elsif ($op eq '->') {
34484a0df2efSAndy Whitcroft					if ($ctx =~ /Wx.|.xW/) {
34493705ce5bSJoe Perches						if (ERROR("SPACING",
34503705ce5bSJoe Perches							  "spaces prohibited around that '$op' $at\n" . $hereptr)) {
3451b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
34523705ce5bSJoe Perches							if (defined $fix_elements[$n + 2]) {
34533705ce5bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
34543705ce5bSJoe Perches							}
3455b34c648bSJoe Perches							$line_fixed = 1;
34563705ce5bSJoe Perches						}
34570a920b5bSAndy Whitcroft					}
34580a920b5bSAndy Whitcroft
34590a920b5bSAndy Whitcroft				# , must have a space on the right.
34600a920b5bSAndy Whitcroft				} elsif ($op eq ',') {
3461cf655043SAndy Whitcroft					if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
34623705ce5bSJoe Perches						if (ERROR("SPACING",
34633705ce5bSJoe Perches							  "space required after that '$op' $at\n" . $hereptr)) {
3464b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
34653705ce5bSJoe Perches							$line_fixed = 1;
3466b34c648bSJoe Perches							$last_after = $n;
34673705ce5bSJoe Perches						}
34680a920b5bSAndy Whitcroft					}
34690a920b5bSAndy Whitcroft
34709c0ca6f9SAndy Whitcroft				# '*' as part of a type definition -- reported already.
347174048ed8SAndy Whitcroft				} elsif ($opv eq '*_') {
34729c0ca6f9SAndy Whitcroft					#warn "'*' is part of type\n";
34739c0ca6f9SAndy Whitcroft
34749c0ca6f9SAndy Whitcroft				# unary operators should have a space before and
34759c0ca6f9SAndy Whitcroft				# none after.  May be left adjacent to another
34769c0ca6f9SAndy Whitcroft				# unary operator, or a cast
34779c0ca6f9SAndy Whitcroft				} elsif ($op eq '!' || $op eq '~' ||
347874048ed8SAndy Whitcroft					 $opv eq '*U' || $opv eq '-U' ||
34790d413866SAndy Whitcroft					 $opv eq '&U' || $opv eq '&&U') {
3480cf655043SAndy Whitcroft					if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
34813705ce5bSJoe Perches						if (ERROR("SPACING",
34823705ce5bSJoe Perches							  "space required before that '$op' $at\n" . $hereptr)) {
3483b34c648bSJoe Perches							if ($n != $last_after + 2) {
3484b34c648bSJoe Perches								$good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]);
34853705ce5bSJoe Perches								$line_fixed = 1;
34863705ce5bSJoe Perches							}
34870a920b5bSAndy Whitcroft						}
3488b34c648bSJoe Perches					}
3489a3340b35SAndy Whitcroft					if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
3490171ae1a4SAndy Whitcroft						# A unary '*' may be const
3491171ae1a4SAndy Whitcroft
3492171ae1a4SAndy Whitcroft					} elsif ($ctx =~ /.xW/) {
34933705ce5bSJoe Perches						if (ERROR("SPACING",
34943705ce5bSJoe Perches							  "space prohibited after that '$op' $at\n" . $hereptr)) {
3495b34c648bSJoe Perches							$good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]);
34963705ce5bSJoe Perches							if (defined $fix_elements[$n + 2]) {
34973705ce5bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
34983705ce5bSJoe Perches							}
3499b34c648bSJoe Perches							$line_fixed = 1;
35003705ce5bSJoe Perches						}
35010a920b5bSAndy Whitcroft					}
35020a920b5bSAndy Whitcroft
35030a920b5bSAndy Whitcroft				# unary ++ and unary -- are allowed no space on one side.
35040a920b5bSAndy Whitcroft				} elsif ($op eq '++' or $op eq '--') {
3505773647a0SAndy Whitcroft					if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
35063705ce5bSJoe Perches						if (ERROR("SPACING",
35073705ce5bSJoe Perches							  "space required one side of that '$op' $at\n" . $hereptr)) {
3508b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
35093705ce5bSJoe Perches							$line_fixed = 1;
35103705ce5bSJoe Perches						}
35110a920b5bSAndy Whitcroft					}
3512773647a0SAndy Whitcroft					if ($ctx =~ /Wx[BE]/ ||
3513773647a0SAndy Whitcroft					    ($ctx =~ /Wx./ && $cc =~ /^;/)) {
35143705ce5bSJoe Perches						if (ERROR("SPACING",
35153705ce5bSJoe Perches							  "space prohibited before that '$op' $at\n" . $hereptr)) {
3516b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
35173705ce5bSJoe Perches							$line_fixed = 1;
35183705ce5bSJoe Perches						}
3519653d4876SAndy Whitcroft					}
3520773647a0SAndy Whitcroft					if ($ctx =~ /ExW/) {
35213705ce5bSJoe Perches						if (ERROR("SPACING",
35223705ce5bSJoe Perches							  "space prohibited after that '$op' $at\n" . $hereptr)) {
3523b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
35243705ce5bSJoe Perches							if (defined $fix_elements[$n + 2]) {
35253705ce5bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
3526773647a0SAndy Whitcroft							}
3527b34c648bSJoe Perches							$line_fixed = 1;
35283705ce5bSJoe Perches						}
35293705ce5bSJoe Perches					}
35300a920b5bSAndy Whitcroft
35310a920b5bSAndy Whitcroft				# << and >> may either have or not have spaces both sides
35329c0ca6f9SAndy Whitcroft				} elsif ($op eq '<<' or $op eq '>>' or
35339c0ca6f9SAndy Whitcroft					 $op eq '&' or $op eq '^' or $op eq '|' or
35349c0ca6f9SAndy Whitcroft					 $op eq '+' or $op eq '-' or
3535c2fdda0dSAndy Whitcroft					 $op eq '*' or $op eq '/' or
3536c2fdda0dSAndy Whitcroft					 $op eq '%')
35370a920b5bSAndy Whitcroft				{
3538773647a0SAndy Whitcroft					if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
35393705ce5bSJoe Perches						if (ERROR("SPACING",
35403705ce5bSJoe Perches							  "need consistent spacing around '$op' $at\n" . $hereptr)) {
3541b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
3542b34c648bSJoe Perches							if (defined $fix_elements[$n + 2]) {
3543b34c648bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
3544b34c648bSJoe Perches							}
35453705ce5bSJoe Perches							$line_fixed = 1;
35463705ce5bSJoe Perches						}
35470a920b5bSAndy Whitcroft					}
35480a920b5bSAndy Whitcroft
35491f65f947SAndy Whitcroft				# A colon needs no spaces before when it is
35501f65f947SAndy Whitcroft				# terminating a case value or a label.
35511f65f947SAndy Whitcroft				} elsif ($opv eq ':C' || $opv eq ':L') {
35521f65f947SAndy Whitcroft					if ($ctx =~ /Wx./) {
35533705ce5bSJoe Perches						if (ERROR("SPACING",
35543705ce5bSJoe Perches							  "space prohibited before that '$op' $at\n" . $hereptr)) {
3555b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
35563705ce5bSJoe Perches							$line_fixed = 1;
35573705ce5bSJoe Perches						}
35581f65f947SAndy Whitcroft					}
35591f65f947SAndy Whitcroft
35600a920b5bSAndy Whitcroft				# All the others need spaces both sides.
3561cf655043SAndy Whitcroft				} elsif ($ctx !~ /[EWC]x[CWE]/) {
35621f65f947SAndy Whitcroft					my $ok = 0;
35631f65f947SAndy Whitcroft
356422f2a2efSAndy Whitcroft					# Ignore email addresses <foo@bar>
35651f65f947SAndy Whitcroft					if (($op eq '<' &&
35661f65f947SAndy Whitcroft					     $cc =~ /^\S+\@\S+>/) ||
35671f65f947SAndy Whitcroft					    ($op eq '>' &&
35681f65f947SAndy Whitcroft					     $ca =~ /<\S+\@\S+$/))
35691f65f947SAndy Whitcroft					{
35701f65f947SAndy Whitcroft					    	$ok = 1;
35711f65f947SAndy Whitcroft					}
35721f65f947SAndy Whitcroft
357384731623SJoe Perches					# messages are ERROR, but ?: are CHK
35741f65f947SAndy Whitcroft					if ($ok == 0) {
357584731623SJoe Perches						my $msg_type = \&ERROR;
357684731623SJoe Perches						$msg_type = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
357784731623SJoe Perches
357884731623SJoe Perches						if (&{$msg_type}("SPACING",
35793705ce5bSJoe Perches								 "spaces required around that '$op' $at\n" . $hereptr)) {
3580b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
3581b34c648bSJoe Perches							if (defined $fix_elements[$n + 2]) {
3582b34c648bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
3583b34c648bSJoe Perches							}
35843705ce5bSJoe Perches							$line_fixed = 1;
35853705ce5bSJoe Perches						}
35860a920b5bSAndy Whitcroft					}
358722f2a2efSAndy Whitcroft				}
35884a0df2efSAndy Whitcroft				$off += length($elements[$n + 1]);
35893705ce5bSJoe Perches
35903705ce5bSJoe Perches##				print("n: <$n> GOOD: <$good>\n");
35913705ce5bSJoe Perches
35923705ce5bSJoe Perches				$fixed_line = $fixed_line . $good;
35930a920b5bSAndy Whitcroft			}
35943705ce5bSJoe Perches
35953705ce5bSJoe Perches			if (($#elements % 2) == 0) {
35963705ce5bSJoe Perches				$fixed_line = $fixed_line . $fix_elements[$#elements];
35973705ce5bSJoe Perches			}
35983705ce5bSJoe Perches
3599194f66fcSJoe Perches			if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) {
3600194f66fcSJoe Perches				$fixed[$fixlinenr] = $fixed_line;
36013705ce5bSJoe Perches			}
36023705ce5bSJoe Perches
36033705ce5bSJoe Perches
36040a920b5bSAndy Whitcroft		}
36050a920b5bSAndy Whitcroft
3606786b6326SJoe Perches# check for whitespace before a non-naked semicolon
3607d2e248e7SJoe Perches		if ($line =~ /^\+.*\S\s+;\s*$/) {
3608786b6326SJoe Perches			if (WARN("SPACING",
3609786b6326SJoe Perches				 "space prohibited before semicolon\n" . $herecurr) &&
3610786b6326SJoe Perches			    $fix) {
3611194f66fcSJoe Perches				1 while $fixed[$fixlinenr] =~
3612786b6326SJoe Perches				    s/^(\+.*\S)\s+;/$1;/;
3613786b6326SJoe Perches			}
3614786b6326SJoe Perches		}
3615786b6326SJoe Perches
3616f0a594c1SAndy Whitcroft# check for multiple assignments
3617f0a594c1SAndy Whitcroft		if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
3618000d1cc1SJoe Perches			CHK("MULTIPLE_ASSIGNMENTS",
3619000d1cc1SJoe Perches			    "multiple assignments should be avoided\n" . $herecurr);
3620f0a594c1SAndy Whitcroft		}
3621f0a594c1SAndy Whitcroft
362222f2a2efSAndy Whitcroft## # check for multiple declarations, allowing for a function declaration
362322f2a2efSAndy Whitcroft## # continuation.
362422f2a2efSAndy Whitcroft## 		if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
362522f2a2efSAndy Whitcroft## 		    $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
362622f2a2efSAndy Whitcroft##
362722f2a2efSAndy Whitcroft## 			# Remove any bracketed sections to ensure we do not
362822f2a2efSAndy Whitcroft## 			# falsly report the parameters of functions.
362922f2a2efSAndy Whitcroft## 			my $ln = $line;
363022f2a2efSAndy Whitcroft## 			while ($ln =~ s/\([^\(\)]*\)//g) {
363122f2a2efSAndy Whitcroft## 			}
363222f2a2efSAndy Whitcroft## 			if ($ln =~ /,/) {
3633000d1cc1SJoe Perches## 				WARN("MULTIPLE_DECLARATION",
3634000d1cc1SJoe Perches##				     "declaring multiple variables together should be avoided\n" . $herecurr);
363522f2a2efSAndy Whitcroft## 			}
363622f2a2efSAndy Whitcroft## 		}
3637f0a594c1SAndy Whitcroft
36380a920b5bSAndy Whitcroft#need space before brace following if, while, etc
363922f2a2efSAndy Whitcroft		if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
364022f2a2efSAndy Whitcroft		    $line =~ /do{/) {
36413705ce5bSJoe Perches			if (ERROR("SPACING",
36423705ce5bSJoe Perches				  "space required before the open brace '{'\n" . $herecurr) &&
36433705ce5bSJoe Perches			    $fix) {
3644194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/^(\+.*(?:do|\))){/$1 {/;
36453705ce5bSJoe Perches			}
3646de7d4f0eSAndy Whitcroft		}
3647de7d4f0eSAndy Whitcroft
3648c4a62ef9SJoe Perches## # check for blank lines before declarations
3649c4a62ef9SJoe Perches##		if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
3650c4a62ef9SJoe Perches##		    $prevrawline =~ /^.\s*$/) {
3651c4a62ef9SJoe Perches##			WARN("SPACING",
3652c4a62ef9SJoe Perches##			     "No blank lines before declarations\n" . $hereprev);
3653c4a62ef9SJoe Perches##		}
3654c4a62ef9SJoe Perches##
3655c4a62ef9SJoe Perches
3656de7d4f0eSAndy Whitcroft# closing brace should have a space following it when it has anything
3657de7d4f0eSAndy Whitcroft# on the line
3658de7d4f0eSAndy Whitcroft		if ($line =~ /}(?!(?:,|;|\)))\S/) {
3659d5e616fcSJoe Perches			if (ERROR("SPACING",
3660d5e616fcSJoe Perches				  "space required after that close brace '}'\n" . $herecurr) &&
3661d5e616fcSJoe Perches			    $fix) {
3662194f66fcSJoe Perches				$fixed[$fixlinenr] =~
3663d5e616fcSJoe Perches				    s/}((?!(?:,|;|\)))\S)/} $1/;
3664d5e616fcSJoe Perches			}
36650a920b5bSAndy Whitcroft		}
36660a920b5bSAndy Whitcroft
366722f2a2efSAndy Whitcroft# check spacing on square brackets
366822f2a2efSAndy Whitcroft		if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
36693705ce5bSJoe Perches			if (ERROR("SPACING",
36703705ce5bSJoe Perches				  "space prohibited after that open square bracket '['\n" . $herecurr) &&
36713705ce5bSJoe Perches			    $fix) {
3672194f66fcSJoe Perches				$fixed[$fixlinenr] =~
36733705ce5bSJoe Perches				    s/\[\s+/\[/;
36743705ce5bSJoe Perches			}
367522f2a2efSAndy Whitcroft		}
367622f2a2efSAndy Whitcroft		if ($line =~ /\s\]/) {
36773705ce5bSJoe Perches			if (ERROR("SPACING",
36783705ce5bSJoe Perches				  "space prohibited before that close square bracket ']'\n" . $herecurr) &&
36793705ce5bSJoe Perches			    $fix) {
3680194f66fcSJoe Perches				$fixed[$fixlinenr] =~
36813705ce5bSJoe Perches				    s/\s+\]/\]/;
36823705ce5bSJoe Perches			}
368322f2a2efSAndy Whitcroft		}
368422f2a2efSAndy Whitcroft
3685c45dcabdSAndy Whitcroft# check spacing on parentheses
36869c0ca6f9SAndy Whitcroft		if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
36879c0ca6f9SAndy Whitcroft		    $line !~ /for\s*\(\s+;/) {
36883705ce5bSJoe Perches			if (ERROR("SPACING",
36893705ce5bSJoe Perches				  "space prohibited after that open parenthesis '('\n" . $herecurr) &&
36903705ce5bSJoe Perches			    $fix) {
3691194f66fcSJoe Perches				$fixed[$fixlinenr] =~
36923705ce5bSJoe Perches				    s/\(\s+/\(/;
36933705ce5bSJoe Perches			}
369422f2a2efSAndy Whitcroft		}
369513214adfSAndy Whitcroft		if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
3696c45dcabdSAndy Whitcroft		    $line !~ /for\s*\(.*;\s+\)/ &&
3697c45dcabdSAndy Whitcroft		    $line !~ /:\s+\)/) {
36983705ce5bSJoe Perches			if (ERROR("SPACING",
36993705ce5bSJoe Perches				  "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
37003705ce5bSJoe Perches			    $fix) {
3701194f66fcSJoe Perches				print("fixlinenr: <$fixlinenr> fixed[fixlinenr]: <$fixed[$fixlinenr]>\n");
3702194f66fcSJoe Perches				$fixed[$fixlinenr] =~
37033705ce5bSJoe Perches				    s/\s+\)/\)/;
37043705ce5bSJoe Perches			}
370522f2a2efSAndy Whitcroft		}
370622f2a2efSAndy Whitcroft
3707e2826fd0SJoe Perches# check unnecessary parentheses around addressof/dereference single $Lvals
3708e2826fd0SJoe Perches# ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar
3709e2826fd0SJoe Perches
3710e2826fd0SJoe Perches		while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) {
3711e2826fd0SJoe Perches			CHK("UNNECESSARY_PARENTHESES",
3712e2826fd0SJoe Perches			    "Unnecessary parentheses around $1\n" . $herecurr);
3713e2826fd0SJoe Perches		    }
3714e2826fd0SJoe Perches
37150a920b5bSAndy Whitcroft#goto labels aren't indented, allow a single space however
37164a0df2efSAndy Whitcroft		if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
37170a920b5bSAndy Whitcroft		   !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
37183705ce5bSJoe Perches			if (WARN("INDENTED_LABEL",
37193705ce5bSJoe Perches				 "labels should not be indented\n" . $herecurr) &&
37203705ce5bSJoe Perches			    $fix) {
3721194f66fcSJoe Perches				$fixed[$fixlinenr] =~
37223705ce5bSJoe Perches				    s/^(.)\s+/$1/;
37233705ce5bSJoe Perches			}
37240a920b5bSAndy Whitcroft		}
37250a920b5bSAndy Whitcroft
37265b9553abSJoe Perches# return is not a function
3727507e5141SJoe Perches		if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {
3728c45dcabdSAndy Whitcroft			my $spacing = $1;
3729507e5141SJoe Perches			if ($^V && $^V ge 5.10.0 &&
37305b9553abSJoe Perches			    $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) {
37315b9553abSJoe Perches				my $value = $1;
37325b9553abSJoe Perches				$value = deparenthesize($value);
37335b9553abSJoe Perches				if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) {
3734000d1cc1SJoe Perches					ERROR("RETURN_PARENTHESES",
3735000d1cc1SJoe Perches					      "return is not a function, parentheses are not required\n" . $herecurr);
37365b9553abSJoe Perches				}
3737c45dcabdSAndy Whitcroft			} elsif ($spacing !~ /\s+/) {
3738000d1cc1SJoe Perches				ERROR("SPACING",
3739000d1cc1SJoe Perches				      "space required before the open parenthesis '('\n" . $herecurr);
3740c45dcabdSAndy Whitcroft			}
3741c45dcabdSAndy Whitcroft		}
3742507e5141SJoe Perches
3743b43ae21bSJoe Perches# unnecessary return in a void function
3744b43ae21bSJoe Perches# at end-of-function, with the previous line a single leading tab, then return;
3745b43ae21bSJoe Perches# and the line before that not a goto label target like "out:"
3746b43ae21bSJoe Perches		if ($sline =~ /^[ \+]}\s*$/ &&
3747b43ae21bSJoe Perches		    $prevline =~ /^\+\treturn\s*;\s*$/ &&
3748b43ae21bSJoe Perches		    $linenr >= 3 &&
3749b43ae21bSJoe Perches		    $lines[$linenr - 3] =~ /^[ +]/ &&
3750b43ae21bSJoe Perches		    $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) {
37519819cf25SJoe Perches			WARN("RETURN_VOID",
3752b43ae21bSJoe Perches			     "void function return statements are not generally useful\n" . $hereprev);
37539819cf25SJoe Perches               }
37549819cf25SJoe Perches
3755189248d8SJoe Perches# if statements using unnecessary parentheses - ie: if ((foo == bar))
3756189248d8SJoe Perches		if ($^V && $^V ge 5.10.0 &&
3757189248d8SJoe Perches		    $line =~ /\bif\s*((?:\(\s*){2,})/) {
3758189248d8SJoe Perches			my $openparens = $1;
3759189248d8SJoe Perches			my $count = $openparens =~ tr@\(@\(@;
3760189248d8SJoe Perches			my $msg = "";
3761189248d8SJoe Perches			if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) {
3762189248d8SJoe Perches				my $comp = $4;	#Not $1 because of $LvalOrFunc
3763189248d8SJoe Perches				$msg = " - maybe == should be = ?" if ($comp eq "==");
3764189248d8SJoe Perches				WARN("UNNECESSARY_PARENTHESES",
3765189248d8SJoe Perches				     "Unnecessary parentheses$msg\n" . $herecurr);
3766189248d8SJoe Perches			}
3767189248d8SJoe Perches		}
3768189248d8SJoe Perches
376953a3c448SAndy Whitcroft# Return of what appears to be an errno should normally be -'ve
377053a3c448SAndy Whitcroft		if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) {
377153a3c448SAndy Whitcroft			my $name = $1;
377253a3c448SAndy Whitcroft			if ($name ne 'EOF' && $name ne 'ERROR') {
3773000d1cc1SJoe Perches				WARN("USE_NEGATIVE_ERRNO",
3774000d1cc1SJoe Perches				     "return of an errno should typically be -ve (return -$1)\n" . $herecurr);
377553a3c448SAndy Whitcroft			}
377653a3c448SAndy Whitcroft		}
3777c45dcabdSAndy Whitcroft
37780a920b5bSAndy Whitcroft# Need a space before open parenthesis after if, while etc
37794a0df2efSAndy Whitcroft		if ($line =~ /\b(if|while|for|switch)\(/) {
37803705ce5bSJoe Perches			if (ERROR("SPACING",
37813705ce5bSJoe Perches				  "space required before the open parenthesis '('\n" . $herecurr) &&
37823705ce5bSJoe Perches			    $fix) {
3783194f66fcSJoe Perches				$fixed[$fixlinenr] =~
37843705ce5bSJoe Perches				    s/\b(if|while|for|switch)\(/$1 \(/;
37853705ce5bSJoe Perches			}
37860a920b5bSAndy Whitcroft		}
37870a920b5bSAndy Whitcroft
3788f5fe35ddSAndy Whitcroft# Check for illegal assignment in if conditional -- and check for trailing
3789f5fe35ddSAndy Whitcroft# statements after the conditional.
3790170d3a22SAndy Whitcroft		if ($line =~ /do\s*(?!{)/) {
37913e469cdcSAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
37923e469cdcSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0)
37933e469cdcSAndy Whitcroft					if (!defined $stat);
3794170d3a22SAndy Whitcroft			my ($stat_next) = ctx_statement_block($line_nr_next,
3795170d3a22SAndy Whitcroft						$remain_next, $off_next);
3796170d3a22SAndy Whitcroft			$stat_next =~ s/\n./\n /g;
3797170d3a22SAndy Whitcroft			##print "stat<$stat> stat_next<$stat_next>\n";
3798170d3a22SAndy Whitcroft
3799170d3a22SAndy Whitcroft			if ($stat_next =~ /^\s*while\b/) {
3800170d3a22SAndy Whitcroft				# If the statement carries leading newlines,
3801170d3a22SAndy Whitcroft				# then count those as offsets.
3802170d3a22SAndy Whitcroft				my ($whitespace) =
3803170d3a22SAndy Whitcroft					($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
3804170d3a22SAndy Whitcroft				my $offset =
3805170d3a22SAndy Whitcroft					statement_rawlines($whitespace) - 1;
3806170d3a22SAndy Whitcroft
3807170d3a22SAndy Whitcroft				$suppress_whiletrailers{$line_nr_next +
3808170d3a22SAndy Whitcroft								$offset} = 1;
3809170d3a22SAndy Whitcroft			}
3810170d3a22SAndy Whitcroft		}
3811170d3a22SAndy Whitcroft		if (!defined $suppress_whiletrailers{$linenr} &&
3812c11230f4SJoe Perches		    defined($stat) && defined($cond) &&
3813170d3a22SAndy Whitcroft		    $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
3814171ae1a4SAndy Whitcroft			my ($s, $c) = ($stat, $cond);
38158905a67cSAndy Whitcroft
3816b53c8e10SAndy Whitcroft			if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
3817000d1cc1SJoe Perches				ERROR("ASSIGN_IN_IF",
3818000d1cc1SJoe Perches				      "do not use assignment in if condition\n" . $herecurr);
38198905a67cSAndy Whitcroft			}
38208905a67cSAndy Whitcroft
38218905a67cSAndy Whitcroft			# Find out what is on the end of the line after the
38228905a67cSAndy Whitcroft			# conditional.
3823773647a0SAndy Whitcroft			substr($s, 0, length($c), '');
38248905a67cSAndy Whitcroft			$s =~ s/\n.*//g;
382513214adfSAndy Whitcroft			$s =~ s/$;//g; 	# Remove any comments
382653210168SAndy Whitcroft			if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
382753210168SAndy Whitcroft			    $c !~ /}\s*while\s*/)
3828773647a0SAndy Whitcroft			{
3829bb44ad39SAndy Whitcroft				# Find out how long the conditional actually is.
3830bb44ad39SAndy Whitcroft				my @newlines = ($c =~ /\n/gs);
3831bb44ad39SAndy Whitcroft				my $cond_lines = 1 + $#newlines;
383242bdf74cSHidetoshi Seto				my $stat_real = '';
3833bb44ad39SAndy Whitcroft
383442bdf74cSHidetoshi Seto				$stat_real = raw_line($linenr, $cond_lines)
383542bdf74cSHidetoshi Seto							. "\n" if ($cond_lines);
3836bb44ad39SAndy Whitcroft				if (defined($stat_real) && $cond_lines > 1) {
3837bb44ad39SAndy Whitcroft					$stat_real = "[...]\n$stat_real";
3838bb44ad39SAndy Whitcroft				}
3839bb44ad39SAndy Whitcroft
3840000d1cc1SJoe Perches				ERROR("TRAILING_STATEMENTS",
3841000d1cc1SJoe Perches				      "trailing statements should be on next line\n" . $herecurr . $stat_real);
38428905a67cSAndy Whitcroft			}
38438905a67cSAndy Whitcroft		}
38448905a67cSAndy Whitcroft
384513214adfSAndy Whitcroft# Check for bitwise tests written as boolean
384613214adfSAndy Whitcroft		if ($line =~ /
384713214adfSAndy Whitcroft			(?:
384813214adfSAndy Whitcroft				(?:\[|\(|\&\&|\|\|)
384913214adfSAndy Whitcroft				\s*0[xX][0-9]+\s*
385013214adfSAndy Whitcroft				(?:\&\&|\|\|)
385113214adfSAndy Whitcroft			|
385213214adfSAndy Whitcroft				(?:\&\&|\|\|)
385313214adfSAndy Whitcroft				\s*0[xX][0-9]+\s*
385413214adfSAndy Whitcroft				(?:\&\&|\|\||\)|\])
385513214adfSAndy Whitcroft			)/x)
385613214adfSAndy Whitcroft		{
3857000d1cc1SJoe Perches			WARN("HEXADECIMAL_BOOLEAN_TEST",
3858000d1cc1SJoe Perches			     "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
385913214adfSAndy Whitcroft		}
386013214adfSAndy Whitcroft
38618905a67cSAndy Whitcroft# if and else should not have general statements after it
386213214adfSAndy Whitcroft		if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
386313214adfSAndy Whitcroft			my $s = $1;
386413214adfSAndy Whitcroft			$s =~ s/$;//g; 	# Remove any comments
386513214adfSAndy Whitcroft			if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
3866000d1cc1SJoe Perches				ERROR("TRAILING_STATEMENTS",
3867000d1cc1SJoe Perches				      "trailing statements should be on next line\n" . $herecurr);
38680a920b5bSAndy Whitcroft			}
386913214adfSAndy Whitcroft		}
387039667782SAndy Whitcroft# if should not continue a brace
387139667782SAndy Whitcroft		if ($line =~ /}\s*if\b/) {
3872000d1cc1SJoe Perches			ERROR("TRAILING_STATEMENTS",
3873048b123fSRasmus Villemoes			      "trailing statements should be on next line (or did you mean 'else if'?)\n" .
387439667782SAndy Whitcroft				$herecurr);
387539667782SAndy Whitcroft		}
3876a1080bf8SAndy Whitcroft# case and default should not have general statements after them
3877a1080bf8SAndy Whitcroft		if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
3878a1080bf8SAndy Whitcroft		    $line !~ /\G(?:
38793fef12d6SAndy Whitcroft			(?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
3880a1080bf8SAndy Whitcroft			\s*return\s+
3881a1080bf8SAndy Whitcroft		    )/xg)
3882a1080bf8SAndy Whitcroft		{
3883000d1cc1SJoe Perches			ERROR("TRAILING_STATEMENTS",
3884000d1cc1SJoe Perches			      "trailing statements should be on next line\n" . $herecurr);
3885a1080bf8SAndy Whitcroft		}
38860a920b5bSAndy Whitcroft
38870a920b5bSAndy Whitcroft		# Check for }<nl>else {, these must be at the same
38880a920b5bSAndy Whitcroft		# indent level to be relevant to each other.
38898b8856f4SJoe Perches		if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ &&
38900a920b5bSAndy Whitcroft		    $previndent == $indent) {
38918b8856f4SJoe Perches			if (ERROR("ELSE_AFTER_BRACE",
38928b8856f4SJoe Perches				  "else should follow close brace '}'\n" . $hereprev) &&
38938b8856f4SJoe Perches			    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
38948b8856f4SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
38958b8856f4SJoe Perches				fix_delete_line($fixlinenr, $rawline);
38968b8856f4SJoe Perches				my $fixedline = $prevrawline;
38978b8856f4SJoe Perches				$fixedline =~ s/}\s*$//;
38988b8856f4SJoe Perches				if ($fixedline !~ /^\+\s*$/) {
38998b8856f4SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
39008b8856f4SJoe Perches				}
39018b8856f4SJoe Perches				$fixedline = $rawline;
39028b8856f4SJoe Perches				$fixedline =~ s/^(.\s*)else/$1} else/;
39038b8856f4SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
39048b8856f4SJoe Perches			}
39050a920b5bSAndy Whitcroft		}
39060a920b5bSAndy Whitcroft
39078b8856f4SJoe Perches		if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ &&
3908c2fdda0dSAndy Whitcroft		    $previndent == $indent) {
3909c2fdda0dSAndy Whitcroft			my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
3910c2fdda0dSAndy Whitcroft
3911c2fdda0dSAndy Whitcroft			# Find out what is on the end of the line after the
3912c2fdda0dSAndy Whitcroft			# conditional.
3913773647a0SAndy Whitcroft			substr($s, 0, length($c), '');
3914c2fdda0dSAndy Whitcroft			$s =~ s/\n.*//g;
3915c2fdda0dSAndy Whitcroft
3916c2fdda0dSAndy Whitcroft			if ($s =~ /^\s*;/) {
39178b8856f4SJoe Perches				if (ERROR("WHILE_AFTER_BRACE",
39188b8856f4SJoe Perches					  "while should follow close brace '}'\n" . $hereprev) &&
39198b8856f4SJoe Perches				    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
39208b8856f4SJoe Perches					fix_delete_line($fixlinenr - 1, $prevrawline);
39218b8856f4SJoe Perches					fix_delete_line($fixlinenr, $rawline);
39228b8856f4SJoe Perches					my $fixedline = $prevrawline;
39238b8856f4SJoe Perches					my $trailing = $rawline;
39248b8856f4SJoe Perches					$trailing =~ s/^\+//;
39258b8856f4SJoe Perches					$trailing = trim($trailing);
39268b8856f4SJoe Perches					$fixedline =~ s/}\s*$/} $trailing/;
39278b8856f4SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
39288b8856f4SJoe Perches				}
3929c2fdda0dSAndy Whitcroft			}
3930c2fdda0dSAndy Whitcroft		}
3931c2fdda0dSAndy Whitcroft
393295e2c602SJoe Perches#Specific variable tests
3933323c1260SJoe Perches		while ($line =~ m{($Constant|$Lval)}g) {
3934323c1260SJoe Perches			my $var = $1;
393595e2c602SJoe Perches
393695e2c602SJoe Perches#gcc binary extension
393795e2c602SJoe Perches			if ($var =~ /^$Binary$/) {
3938d5e616fcSJoe Perches				if (WARN("GCC_BINARY_CONSTANT",
3939d5e616fcSJoe Perches					 "Avoid gcc v4.3+ binary constant extension: <$var>\n" . $herecurr) &&
3940d5e616fcSJoe Perches				    $fix) {
3941d5e616fcSJoe Perches					my $hexval = sprintf("0x%x", oct($var));
3942194f66fcSJoe Perches					$fixed[$fixlinenr] =~
3943d5e616fcSJoe Perches					    s/\b$var\b/$hexval/;
3944d5e616fcSJoe Perches				}
394595e2c602SJoe Perches			}
394695e2c602SJoe Perches
394795e2c602SJoe Perches#CamelCase
3948807bd26cSJoe Perches			if ($var !~ /^$Constant$/ &&
3949be79794bSJoe Perches			    $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
395022735ce8SJoe Perches#Ignore Page<foo> variants
3951807bd26cSJoe Perches			    $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
395222735ce8SJoe Perches#Ignore SI style variants like nS, mV and dB (ie: max_uV, regulator_min_uA_show)
39533445686aSJoe Perches			    $var !~ /^(?:[a-z_]*?)_?[a-z][A-Z](?:_[a-z_]+)?$/) {
39547e781f67SJoe Perches				while ($var =~ m{($Ident)}g) {
39557e781f67SJoe Perches					my $word = $1;
39567e781f67SJoe Perches					next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
3957d8b07710SJoe Perches					if ($check) {
3958d8b07710SJoe Perches						seed_camelcase_includes();
3959d8b07710SJoe Perches						if (!$file && !$camelcase_file_seeded) {
3960d8b07710SJoe Perches							seed_camelcase_file($realfile);
3961d8b07710SJoe Perches							$camelcase_file_seeded = 1;
3962d8b07710SJoe Perches						}
3963d8b07710SJoe Perches					}
39647e781f67SJoe Perches					if (!defined $camelcase{$word}) {
39657e781f67SJoe Perches						$camelcase{$word} = 1;
3966be79794bSJoe Perches						CHK("CAMELCASE",
39677e781f67SJoe Perches						    "Avoid CamelCase: <$word>\n" . $herecurr);
39687e781f67SJoe Perches					}
3969323c1260SJoe Perches				}
3970323c1260SJoe Perches			}
39713445686aSJoe Perches		}
39720a920b5bSAndy Whitcroft
39730a920b5bSAndy Whitcroft#no spaces allowed after \ in define
3974d5e616fcSJoe Perches		if ($line =~ /\#\s*define.*\\\s+$/) {
3975d5e616fcSJoe Perches			if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
3976d5e616fcSJoe Perches				 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
3977d5e616fcSJoe Perches			    $fix) {
3978194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\s+$//;
3979d5e616fcSJoe Perches			}
39800a920b5bSAndy Whitcroft		}
39810a920b5bSAndy Whitcroft
3982653d4876SAndy Whitcroft#warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
3983c45dcabdSAndy Whitcroft		if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
3984e09dec48SAndy Whitcroft			my $file = "$1.h";
3985e09dec48SAndy Whitcroft			my $checkfile = "include/linux/$file";
3986e09dec48SAndy Whitcroft			if (-f "$root/$checkfile" &&
3987e09dec48SAndy Whitcroft			    $realfile ne $checkfile &&
39887840a94cSWolfram Sang			    $1 !~ /$allowed_asm_includes/)
3989c45dcabdSAndy Whitcroft			{
3990e09dec48SAndy Whitcroft				if ($realfile =~ m{^arch/}) {
3991000d1cc1SJoe Perches					CHK("ARCH_INCLUDE_LINUX",
3992000d1cc1SJoe Perches					    "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
3993e09dec48SAndy Whitcroft				} else {
3994000d1cc1SJoe Perches					WARN("INCLUDE_LINUX",
3995000d1cc1SJoe Perches					     "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
3996e09dec48SAndy Whitcroft				}
39970a920b5bSAndy Whitcroft			}
39980a920b5bSAndy Whitcroft		}
39990a920b5bSAndy Whitcroft
4000653d4876SAndy Whitcroft# multi-statement macros should be enclosed in a do while loop, grab the
4001653d4876SAndy Whitcroft# first statement and ensure its the whole macro if its not enclosed
4002cf655043SAndy Whitcroft# in a known good container
4003b8f96a31SAndy Whitcroft		if ($realfile !~ m@/vmlinux.lds.h$@ &&
4004b8f96a31SAndy Whitcroft		    $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
4005d8aaf121SAndy Whitcroft			my $ln = $linenr;
4006d8aaf121SAndy Whitcroft			my $cnt = $realcnt;
4007c45dcabdSAndy Whitcroft			my ($off, $dstat, $dcond, $rest);
4008c45dcabdSAndy Whitcroft			my $ctx = '';
4009c45dcabdSAndy Whitcroft			($dstat, $dcond, $ln, $cnt, $off) =
4010f74bd194SAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0);
4011f74bd194SAndy Whitcroft			$ctx = $dstat;
4012c45dcabdSAndy Whitcroft			#print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
4013a3bb97a7SAndy Whitcroft			#print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
4014c45dcabdSAndy Whitcroft
4015f74bd194SAndy Whitcroft			$dstat =~ s/^.\s*\#\s*define\s+$Ident(?:\([^\)]*\))?\s*//;
4016292f1a9bSAndy Whitcroft			$dstat =~ s/$;//g;
4017c45dcabdSAndy Whitcroft			$dstat =~ s/\\\n.//g;
4018c45dcabdSAndy Whitcroft			$dstat =~ s/^\s*//s;
4019c45dcabdSAndy Whitcroft			$dstat =~ s/\s*$//s;
4020c45dcabdSAndy Whitcroft
4021c45dcabdSAndy Whitcroft			# Flatten any parentheses and braces
4022bf30d6edSAndy Whitcroft			while ($dstat =~ s/\([^\(\)]*\)/1/ ||
4023bf30d6edSAndy Whitcroft			       $dstat =~ s/\{[^\{\}]*\}/1/ ||
4024c81769fdSAndy Whitcroft			       $dstat =~ s/\[[^\[\]]*\]/1/)
4025bf30d6edSAndy Whitcroft			{
4026c45dcabdSAndy Whitcroft			}
4027c45dcabdSAndy Whitcroft
4028e45bab8eSAndy Whitcroft			# Flatten any obvious string concatentation.
4029e45bab8eSAndy Whitcroft			while ($dstat =~ s/("X*")\s*$Ident/$1/ ||
4030e45bab8eSAndy Whitcroft			       $dstat =~ s/$Ident\s*("X*")/$1/)
4031e45bab8eSAndy Whitcroft			{
4032e45bab8eSAndy Whitcroft			}
4033e45bab8eSAndy Whitcroft
4034c45dcabdSAndy Whitcroft			my $exceptions = qr{
4035c45dcabdSAndy Whitcroft				$Declare|
4036c45dcabdSAndy Whitcroft				module_param_named|
4037a0a0a7a9SKees Cook				MODULE_PARM_DESC|
4038c45dcabdSAndy Whitcroft				DECLARE_PER_CPU|
4039c45dcabdSAndy Whitcroft				DEFINE_PER_CPU|
4040383099fdSAndy Whitcroft				__typeof__\(|
404122fd2d3eSStefani Seibold				union|
404222fd2d3eSStefani Seibold				struct|
4043ea71a0a0SAndy Whitcroft				\.$Ident\s*=\s*|
4044ea71a0a0SAndy Whitcroft				^\"|\"$
4045c45dcabdSAndy Whitcroft			}x;
40465eaa20b9SAndy Whitcroft			#print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
4047f74bd194SAndy Whitcroft			if ($dstat ne '' &&
4048f74bd194SAndy Whitcroft			    $dstat !~ /^(?:$Ident|-?$Constant),$/ &&			# 10, // foo(),
4049f74bd194SAndy Whitcroft			    $dstat !~ /^(?:$Ident|-?$Constant);$/ &&			# foo();
40503cc4b1c3SJoe Perches			    $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ &&		# 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
4051356fd398SJoe Perches			    $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ &&			# character constants
4052f74bd194SAndy Whitcroft			    $dstat !~ /$exceptions/ &&
4053f74bd194SAndy Whitcroft			    $dstat !~ /^\.$Ident\s*=/ &&				# .foo =
4054e942e2c3SJoe Perches			    $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ &&		# stringification #foo
405572f115f9SAndy Whitcroft			    $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ &&	# do {...} while (...); // do {...} while (...)
4056f74bd194SAndy Whitcroft			    $dstat !~ /^for\s*$Constant$/ &&				# for (...)
4057f74bd194SAndy Whitcroft			    $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ &&	# for (...) bar()
4058f74bd194SAndy Whitcroft			    $dstat !~ /^do\s*{/ &&					# do {...
4059f95a7e6aSJoe Perches			    $dstat !~ /^\({/ &&						# ({...
4060f95a7e6aSJoe Perches			    $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
4061c45dcabdSAndy Whitcroft			{
4062f74bd194SAndy Whitcroft				$ctx =~ s/\n*$//;
4063f74bd194SAndy Whitcroft				my $herectx = $here . "\n";
4064f74bd194SAndy Whitcroft				my $cnt = statement_rawlines($ctx);
4065f74bd194SAndy Whitcroft
4066f74bd194SAndy Whitcroft				for (my $n = 0; $n < $cnt; $n++) {
4067f74bd194SAndy Whitcroft					$herectx .= raw_line($linenr, $n) . "\n";
4068c45dcabdSAndy Whitcroft				}
4069c45dcabdSAndy Whitcroft
4070f74bd194SAndy Whitcroft				if ($dstat =~ /;/) {
4071f74bd194SAndy Whitcroft					ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
4072f74bd194SAndy Whitcroft					      "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
4073f74bd194SAndy Whitcroft				} else {
4074000d1cc1SJoe Perches					ERROR("COMPLEX_MACRO",
4075f74bd194SAndy Whitcroft					      "Macros with complex values should be enclosed in parenthesis\n" . "$herectx");
4076d8aaf121SAndy Whitcroft				}
40770a920b5bSAndy Whitcroft			}
40785023d347SJoe Perches
4079481eb486SJoe Perches# check for line continuations outside of #defines, preprocessor #, and asm
40805023d347SJoe Perches
40815023d347SJoe Perches		} else {
40825023d347SJoe Perches			if ($prevline !~ /^..*\\$/ &&
4083481eb486SJoe Perches			    $line !~ /^\+\s*\#.*\\$/ &&		# preprocessor
4084481eb486SJoe Perches			    $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ &&	# asm
40855023d347SJoe Perches			    $line =~ /^\+.*\\$/) {
40865023d347SJoe Perches				WARN("LINE_CONTINUATIONS",
40875023d347SJoe Perches				     "Avoid unnecessary line continuations\n" . $herecurr);
40885023d347SJoe Perches			}
4089653d4876SAndy Whitcroft		}
40900a920b5bSAndy Whitcroft
4091b13edf7fSJoe Perches# do {} while (0) macro tests:
4092b13edf7fSJoe Perches# single-statement macros do not need to be enclosed in do while (0) loop,
4093b13edf7fSJoe Perches# macro should not end with a semicolon
4094b13edf7fSJoe Perches		if ($^V && $^V ge 5.10.0 &&
4095b13edf7fSJoe Perches		    $realfile !~ m@/vmlinux.lds.h$@ &&
4096b13edf7fSJoe Perches		    $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
4097b13edf7fSJoe Perches			my $ln = $linenr;
4098b13edf7fSJoe Perches			my $cnt = $realcnt;
4099b13edf7fSJoe Perches			my ($off, $dstat, $dcond, $rest);
4100b13edf7fSJoe Perches			my $ctx = '';
4101b13edf7fSJoe Perches			($dstat, $dcond, $ln, $cnt, $off) =
4102b13edf7fSJoe Perches				ctx_statement_block($linenr, $realcnt, 0);
4103b13edf7fSJoe Perches			$ctx = $dstat;
4104b13edf7fSJoe Perches
4105b13edf7fSJoe Perches			$dstat =~ s/\\\n.//g;
4106b13edf7fSJoe Perches
4107b13edf7fSJoe Perches			if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
4108b13edf7fSJoe Perches				my $stmts = $2;
4109b13edf7fSJoe Perches				my $semis = $3;
4110b13edf7fSJoe Perches
4111b13edf7fSJoe Perches				$ctx =~ s/\n*$//;
4112b13edf7fSJoe Perches				my $cnt = statement_rawlines($ctx);
4113b13edf7fSJoe Perches				my $herectx = $here . "\n";
4114b13edf7fSJoe Perches
4115b13edf7fSJoe Perches				for (my $n = 0; $n < $cnt; $n++) {
4116b13edf7fSJoe Perches					$herectx .= raw_line($linenr, $n) . "\n";
4117b13edf7fSJoe Perches				}
4118b13edf7fSJoe Perches
4119ac8e97f8SJoe Perches				if (($stmts =~ tr/;/;/) == 1 &&
4120ac8e97f8SJoe Perches				    $stmts !~ /^\s*(if|while|for|switch)\b/) {
4121b13edf7fSJoe Perches					WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
4122b13edf7fSJoe Perches					     "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
4123b13edf7fSJoe Perches				}
4124b13edf7fSJoe Perches				if (defined $semis && $semis ne "") {
4125b13edf7fSJoe Perches					WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
4126b13edf7fSJoe Perches					     "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
4127b13edf7fSJoe Perches				}
4128f5ef95b1SJoe Perches			} elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) {
4129f5ef95b1SJoe Perches				$ctx =~ s/\n*$//;
4130f5ef95b1SJoe Perches				my $cnt = statement_rawlines($ctx);
4131f5ef95b1SJoe Perches				my $herectx = $here . "\n";
4132f5ef95b1SJoe Perches
4133f5ef95b1SJoe Perches				for (my $n = 0; $n < $cnt; $n++) {
4134f5ef95b1SJoe Perches					$herectx .= raw_line($linenr, $n) . "\n";
4135f5ef95b1SJoe Perches				}
4136f5ef95b1SJoe Perches
4137f5ef95b1SJoe Perches				WARN("TRAILING_SEMICOLON",
4138f5ef95b1SJoe Perches				     "macros should not use a trailing semicolon\n" . "$herectx");
4139b13edf7fSJoe Perches			}
4140b13edf7fSJoe Perches		}
4141b13edf7fSJoe Perches
4142080ba929SMike Frysinger# make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
4143080ba929SMike Frysinger# all assignments may have only one of the following with an assignment:
4144080ba929SMike Frysinger#	.
4145080ba929SMike Frysinger#	ALIGN(...)
4146080ba929SMike Frysinger#	VMLINUX_SYMBOL(...)
4147080ba929SMike Frysinger		if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
4148000d1cc1SJoe Perches			WARN("MISSING_VMLINUX_SYMBOL",
4149000d1cc1SJoe Perches			     "vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
4150080ba929SMike Frysinger		}
4151080ba929SMike Frysinger
4152f0a594c1SAndy Whitcroft# check for redundant bracing round if etc
415313214adfSAndy Whitcroft		if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
415413214adfSAndy Whitcroft			my ($level, $endln, @chunks) =
4155cf655043SAndy Whitcroft				ctx_statement_full($linenr, $realcnt, 1);
415613214adfSAndy Whitcroft			#print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
4157cf655043SAndy Whitcroft			#print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
4158cf655043SAndy Whitcroft			if ($#chunks > 0 && $level == 0) {
4159aad4f614SJoe Perches				my @allowed = ();
4160aad4f614SJoe Perches				my $allow = 0;
416113214adfSAndy Whitcroft				my $seen = 0;
4162773647a0SAndy Whitcroft				my $herectx = $here . "\n";
4163cf655043SAndy Whitcroft				my $ln = $linenr - 1;
416413214adfSAndy Whitcroft				for my $chunk (@chunks) {
416513214adfSAndy Whitcroft					my ($cond, $block) = @{$chunk};
416613214adfSAndy Whitcroft
4167773647a0SAndy Whitcroft					# If the condition carries leading newlines, then count those as offsets.
4168773647a0SAndy Whitcroft					my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
4169773647a0SAndy Whitcroft					my $offset = statement_rawlines($whitespace) - 1;
4170773647a0SAndy Whitcroft
4171aad4f614SJoe Perches					$allowed[$allow] = 0;
4172773647a0SAndy Whitcroft					#print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
4173773647a0SAndy Whitcroft
4174773647a0SAndy Whitcroft					# We have looked at and allowed this specific line.
4175773647a0SAndy Whitcroft					$suppress_ifbraces{$ln + $offset} = 1;
4176773647a0SAndy Whitcroft
4177773647a0SAndy Whitcroft					$herectx .= "$rawlines[$ln + $offset]\n[...]\n";
4178cf655043SAndy Whitcroft					$ln += statement_rawlines($block) - 1;
4179cf655043SAndy Whitcroft
4180773647a0SAndy Whitcroft					substr($block, 0, length($cond), '');
418113214adfSAndy Whitcroft
418213214adfSAndy Whitcroft					$seen++ if ($block =~ /^\s*{/);
418313214adfSAndy Whitcroft
4184aad4f614SJoe Perches					#print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
4185cf655043SAndy Whitcroft					if (statement_lines($cond) > 1) {
4186cf655043SAndy Whitcroft						#print "APW: ALLOWED: cond<$cond>\n";
4187aad4f614SJoe Perches						$allowed[$allow] = 1;
418813214adfSAndy Whitcroft					}
418913214adfSAndy Whitcroft					if ($block =~/\b(?:if|for|while)\b/) {
4190cf655043SAndy Whitcroft						#print "APW: ALLOWED: block<$block>\n";
4191aad4f614SJoe Perches						$allowed[$allow] = 1;
419213214adfSAndy Whitcroft					}
4193cf655043SAndy Whitcroft					if (statement_block_size($block) > 1) {
4194cf655043SAndy Whitcroft						#print "APW: ALLOWED: lines block<$block>\n";
4195aad4f614SJoe Perches						$allowed[$allow] = 1;
419613214adfSAndy Whitcroft					}
4197aad4f614SJoe Perches					$allow++;
419813214adfSAndy Whitcroft				}
4199aad4f614SJoe Perches				if ($seen) {
4200aad4f614SJoe Perches					my $sum_allowed = 0;
4201aad4f614SJoe Perches					foreach (@allowed) {
4202aad4f614SJoe Perches						$sum_allowed += $_;
4203aad4f614SJoe Perches					}
4204aad4f614SJoe Perches					if ($sum_allowed == 0) {
4205000d1cc1SJoe Perches						WARN("BRACES",
4206000d1cc1SJoe Perches						     "braces {} are not necessary for any arm of this statement\n" . $herectx);
4207aad4f614SJoe Perches					} elsif ($sum_allowed != $allow &&
4208aad4f614SJoe Perches						 $seen != $allow) {
4209aad4f614SJoe Perches						CHK("BRACES",
4210aad4f614SJoe Perches						    "braces {} should be used on all arms of this statement\n" . $herectx);
4211aad4f614SJoe Perches					}
421213214adfSAndy Whitcroft				}
421313214adfSAndy Whitcroft			}
421413214adfSAndy Whitcroft		}
4215773647a0SAndy Whitcroft		if (!defined $suppress_ifbraces{$linenr - 1} &&
421613214adfSAndy Whitcroft					$line =~ /\b(if|while|for|else)\b/) {
4217cf655043SAndy Whitcroft			my $allowed = 0;
4218f0a594c1SAndy Whitcroft
4219cf655043SAndy Whitcroft			# Check the pre-context.
4220cf655043SAndy Whitcroft			if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
4221cf655043SAndy Whitcroft				#print "APW: ALLOWED: pre<$1>\n";
4222cf655043SAndy Whitcroft				$allowed = 1;
4223f0a594c1SAndy Whitcroft			}
4224773647a0SAndy Whitcroft
4225773647a0SAndy Whitcroft			my ($level, $endln, @chunks) =
4226773647a0SAndy Whitcroft				ctx_statement_full($linenr, $realcnt, $-[0]);
4227773647a0SAndy Whitcroft
4228cf655043SAndy Whitcroft			# Check the condition.
4229cf655043SAndy Whitcroft			my ($cond, $block) = @{$chunks[0]};
4230773647a0SAndy Whitcroft			#print "CHECKING<$linenr> cond<$cond> block<$block>\n";
4231cf655043SAndy Whitcroft			if (defined $cond) {
4232773647a0SAndy Whitcroft				substr($block, 0, length($cond), '');
4233cf655043SAndy Whitcroft			}
4234cf655043SAndy Whitcroft			if (statement_lines($cond) > 1) {
4235cf655043SAndy Whitcroft				#print "APW: ALLOWED: cond<$cond>\n";
4236cf655043SAndy Whitcroft				$allowed = 1;
4237cf655043SAndy Whitcroft			}
4238cf655043SAndy Whitcroft			if ($block =~/\b(?:if|for|while)\b/) {
4239cf655043SAndy Whitcroft				#print "APW: ALLOWED: block<$block>\n";
4240cf655043SAndy Whitcroft				$allowed = 1;
4241cf655043SAndy Whitcroft			}
4242cf655043SAndy Whitcroft			if (statement_block_size($block) > 1) {
4243cf655043SAndy Whitcroft				#print "APW: ALLOWED: lines block<$block>\n";
4244cf655043SAndy Whitcroft				$allowed = 1;
4245cf655043SAndy Whitcroft			}
4246cf655043SAndy Whitcroft			# Check the post-context.
4247cf655043SAndy Whitcroft			if (defined $chunks[1]) {
4248cf655043SAndy Whitcroft				my ($cond, $block) = @{$chunks[1]};
4249cf655043SAndy Whitcroft				if (defined $cond) {
4250773647a0SAndy Whitcroft					substr($block, 0, length($cond), '');
4251cf655043SAndy Whitcroft				}
4252cf655043SAndy Whitcroft				if ($block =~ /^\s*\{/) {
4253cf655043SAndy Whitcroft					#print "APW: ALLOWED: chunk-1 block<$block>\n";
4254cf655043SAndy Whitcroft					$allowed = 1;
4255cf655043SAndy Whitcroft				}
4256cf655043SAndy Whitcroft			}
4257cf655043SAndy Whitcroft			if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
425869932487SJustin P. Mattock				my $herectx = $here . "\n";
4259f055663cSAndy Whitcroft				my $cnt = statement_rawlines($block);
4260cf655043SAndy Whitcroft
4261f055663cSAndy Whitcroft				for (my $n = 0; $n < $cnt; $n++) {
426269932487SJustin P. Mattock					$herectx .= raw_line($linenr, $n) . "\n";
4263cf655043SAndy Whitcroft				}
4264cf655043SAndy Whitcroft
4265000d1cc1SJoe Perches				WARN("BRACES",
4266000d1cc1SJoe Perches				     "braces {} are not necessary for single statement blocks\n" . $herectx);
4267f0a594c1SAndy Whitcroft			}
4268f0a594c1SAndy Whitcroft		}
4269f0a594c1SAndy Whitcroft
42700979ae66SJoe Perches# check for unnecessary blank lines around braces
427177b9a53aSJoe Perches		if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
42720979ae66SJoe Perches			CHK("BRACES",
42730979ae66SJoe Perches			    "Blank lines aren't necessary before a close brace '}'\n" . $hereprev);
42740979ae66SJoe Perches		}
427577b9a53aSJoe Perches		if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
42760979ae66SJoe Perches			CHK("BRACES",
42770979ae66SJoe Perches			    "Blank lines aren't necessary after an open brace '{'\n" . $hereprev);
42780979ae66SJoe Perches		}
42790979ae66SJoe Perches
42804a0df2efSAndy Whitcroft# no volatiles please
42816c72ffaaSAndy Whitcroft		my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
42826c72ffaaSAndy Whitcroft		if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
4283000d1cc1SJoe Perches			WARN("VOLATILE",
4284000d1cc1SJoe Perches			     "Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
42854a0df2efSAndy Whitcroft		}
42864a0df2efSAndy Whitcroft
428700df344fSAndy Whitcroft# warn about #if 0
4288c45dcabdSAndy Whitcroft		if ($line =~ /^.\s*\#\s*if\s+0\b/) {
4289000d1cc1SJoe Perches			CHK("REDUNDANT_CODE",
4290000d1cc1SJoe Perches			    "if this code is redundant consider removing it\n" .
4291de7d4f0eSAndy Whitcroft				$herecurr);
42924a0df2efSAndy Whitcroft		}
42934a0df2efSAndy Whitcroft
429403df4b51SAndy Whitcroft# check for needless "if (<foo>) fn(<foo>)" uses
429503df4b51SAndy Whitcroft		if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
429603df4b51SAndy Whitcroft			my $expr = '\s*\(\s*' . quotemeta($1) . '\s*\)\s*;';
429703df4b51SAndy Whitcroft			if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?)$expr/) {
429803df4b51SAndy Whitcroft				WARN('NEEDLESS_IF',
429903df4b51SAndy Whitcroft				     "$1(NULL) is safe this check is probably not required\n" . $hereprev);
43004c432a8fSGreg Kroah-Hartman			}
43014c432a8fSGreg Kroah-Hartman		}
4302f0a594c1SAndy Whitcroft
4303ebfdc409SJoe Perches# check for unnecessary "Out of Memory" messages
4304ebfdc409SJoe Perches		if ($line =~ /^\+.*\b$logFunctions\s*\(/ &&
4305ebfdc409SJoe Perches		    $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ &&
4306ebfdc409SJoe Perches		    (defined $1 || defined $3) &&
4307ebfdc409SJoe Perches		    $linenr > 3) {
4308ebfdc409SJoe Perches			my $testval = $2;
4309ebfdc409SJoe Perches			my $testline = $lines[$linenr - 3];
4310ebfdc409SJoe Perches
4311ebfdc409SJoe Perches			my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0);
4312ebfdc409SJoe Perches#			print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n");
4313ebfdc409SJoe Perches
4314ebfdc409SJoe 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)/) {
4315ebfdc409SJoe Perches				WARN("OOM_MESSAGE",
4316ebfdc409SJoe Perches				     "Possible unnecessary 'out of memory' message\n" . $hereprev);
4317ebfdc409SJoe Perches			}
4318ebfdc409SJoe Perches		}
4319ebfdc409SJoe Perches
43208716de38SJoe Perches# check for bad placement of section $InitAttribute (e.g.: __initdata)
43218716de38SJoe Perches		if ($line =~ /(\b$InitAttribute\b)/) {
43228716de38SJoe Perches			my $attr = $1;
43238716de38SJoe Perches			if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
43248716de38SJoe Perches				my $ptr = $1;
43258716de38SJoe Perches				my $var = $2;
43268716de38SJoe Perches				if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
43278716de38SJoe Perches				      ERROR("MISPLACED_INIT",
43288716de38SJoe Perches					    "$attr should be placed after $var\n" . $herecurr)) ||
43298716de38SJoe Perches				     ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
43308716de38SJoe Perches				      WARN("MISPLACED_INIT",
43318716de38SJoe Perches					   "$attr should be placed after $var\n" . $herecurr))) &&
43328716de38SJoe Perches				    $fix) {
4333194f66fcSJoe Perches					$fixed[$fixlinenr] =~ 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;
43348716de38SJoe Perches				}
43358716de38SJoe Perches			}
43368716de38SJoe Perches		}
43378716de38SJoe Perches
4338e970b884SJoe Perches# check for $InitAttributeData (ie: __initdata) with const
4339e970b884SJoe Perches		if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
4340e970b884SJoe Perches			my $attr = $1;
4341e970b884SJoe Perches			$attr =~ /($InitAttributePrefix)(.*)/;
4342e970b884SJoe Perches			my $attr_prefix = $1;
4343e970b884SJoe Perches			my $attr_type = $2;
4344e970b884SJoe Perches			if (ERROR("INIT_ATTRIBUTE",
4345e970b884SJoe Perches				  "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
4346e970b884SJoe Perches			    $fix) {
4347194f66fcSJoe Perches				$fixed[$fixlinenr] =~
4348e970b884SJoe Perches				    s/$InitAttributeData/${attr_prefix}initconst/;
4349e970b884SJoe Perches			}
4350e970b884SJoe Perches		}
4351e970b884SJoe Perches
4352e970b884SJoe Perches# check for $InitAttributeConst (ie: __initconst) without const
4353e970b884SJoe Perches		if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
4354e970b884SJoe Perches			my $attr = $1;
4355e970b884SJoe Perches			if (ERROR("INIT_ATTRIBUTE",
4356e970b884SJoe Perches				  "Use of $attr requires a separate use of const\n" . $herecurr) &&
4357e970b884SJoe Perches			    $fix) {
4358194f66fcSJoe Perches				my $lead = $fixed[$fixlinenr] =~
4359e970b884SJoe Perches				    /(^\+\s*(?:static\s+))/;
4360e970b884SJoe Perches				$lead = rtrim($1);
4361e970b884SJoe Perches				$lead = "$lead " if ($lead !~ /^\+$/);
4362e970b884SJoe Perches				$lead = "${lead}const ";
4363194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/;
4364e970b884SJoe Perches			}
4365e970b884SJoe Perches		}
4366e970b884SJoe Perches
4367fbdb8138SJoe Perches# don't use __constant_<foo> functions outside of include/uapi/
4368fbdb8138SJoe Perches		if ($realfile !~ m@^include/uapi/@ &&
4369fbdb8138SJoe Perches		    $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) {
4370fbdb8138SJoe Perches			my $constant_func = $1;
4371fbdb8138SJoe Perches			my $func = $constant_func;
4372fbdb8138SJoe Perches			$func =~ s/^__constant_//;
4373fbdb8138SJoe Perches			if (WARN("CONSTANT_CONVERSION",
4374fbdb8138SJoe Perches				 "$constant_func should be $func\n" . $herecurr) &&
4375fbdb8138SJoe Perches			    $fix) {
4376194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g;
4377fbdb8138SJoe Perches			}
4378fbdb8138SJoe Perches		}
4379fbdb8138SJoe Perches
43801a15a250SPatrick Pannuto# prefer usleep_range over udelay
438137581c28SBruce Allan		if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
438243c1d77cSJoe Perches			my $delay = $1;
43831a15a250SPatrick Pannuto			# ignore udelay's < 10, however
438443c1d77cSJoe Perches			if (! ($delay < 10) ) {
4385000d1cc1SJoe Perches				CHK("USLEEP_RANGE",
438643c1d77cSJoe Perches				    "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $herecurr);
438743c1d77cSJoe Perches			}
438843c1d77cSJoe Perches			if ($delay > 2000) {
438943c1d77cSJoe Perches				WARN("LONG_UDELAY",
439043c1d77cSJoe Perches				     "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr);
43911a15a250SPatrick Pannuto			}
43921a15a250SPatrick Pannuto		}
43931a15a250SPatrick Pannuto
439409ef8725SPatrick Pannuto# warn about unexpectedly long msleep's
439509ef8725SPatrick Pannuto		if ($line =~ /\bmsleep\s*\((\d+)\);/) {
439609ef8725SPatrick Pannuto			if ($1 < 20) {
4397000d1cc1SJoe Perches				WARN("MSLEEP",
439843c1d77cSJoe Perches				     "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $herecurr);
439909ef8725SPatrick Pannuto			}
440009ef8725SPatrick Pannuto		}
440109ef8725SPatrick Pannuto
440236ec1939SJoe Perches# check for comparisons of jiffies
440336ec1939SJoe Perches		if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
440436ec1939SJoe Perches			WARN("JIFFIES_COMPARISON",
440536ec1939SJoe Perches			     "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
440636ec1939SJoe Perches		}
440736ec1939SJoe Perches
44089d7a34a5SJoe Perches# check for comparisons of get_jiffies_64()
44099d7a34a5SJoe Perches		if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
44109d7a34a5SJoe Perches			WARN("JIFFIES_COMPARISON",
44119d7a34a5SJoe Perches			     "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
44129d7a34a5SJoe Perches		}
44139d7a34a5SJoe Perches
441400df344fSAndy Whitcroft# warn about #ifdefs in C files
4415c45dcabdSAndy Whitcroft#		if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
441600df344fSAndy Whitcroft#			print "#ifdef in C files should be avoided\n";
441700df344fSAndy Whitcroft#			print "$herecurr";
441800df344fSAndy Whitcroft#			$clean = 0;
441900df344fSAndy Whitcroft#		}
442000df344fSAndy Whitcroft
442122f2a2efSAndy Whitcroft# warn about spacing in #ifdefs
4422c45dcabdSAndy Whitcroft		if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
44233705ce5bSJoe Perches			if (ERROR("SPACING",
44243705ce5bSJoe Perches				  "exactly one space required after that #$1\n" . $herecurr) &&
44253705ce5bSJoe Perches			    $fix) {
4426194f66fcSJoe Perches				$fixed[$fixlinenr] =~
44273705ce5bSJoe Perches				    s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
44283705ce5bSJoe Perches			}
44293705ce5bSJoe Perches
443022f2a2efSAndy Whitcroft		}
443122f2a2efSAndy Whitcroft
44324a0df2efSAndy Whitcroft# check for spinlock_t definitions without a comment.
4433171ae1a4SAndy Whitcroft		if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
4434171ae1a4SAndy Whitcroft		    $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
44354a0df2efSAndy Whitcroft			my $which = $1;
44364a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
4437000d1cc1SJoe Perches				CHK("UNCOMMENTED_DEFINITION",
4438000d1cc1SJoe Perches				    "$1 definition without comment\n" . $herecurr);
44394a0df2efSAndy Whitcroft			}
44404a0df2efSAndy Whitcroft		}
44414a0df2efSAndy Whitcroft# check for memory barriers without a comment.
44424a0df2efSAndy Whitcroft		if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
44434a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
4444c1fd7bb9SJoe Perches				WARN("MEMORY_BARRIER",
4445000d1cc1SJoe Perches				     "memory barrier without comment\n" . $herecurr);
44464a0df2efSAndy Whitcroft			}
44474a0df2efSAndy Whitcroft		}
44484a0df2efSAndy Whitcroft# check of hardware specific defines
4449c45dcabdSAndy Whitcroft		if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
4450000d1cc1SJoe Perches			CHK("ARCH_DEFINES",
4451000d1cc1SJoe Perches			    "architecture specific defines should be avoided\n" .  $herecurr);
44520a920b5bSAndy Whitcroft		}
4453653d4876SAndy Whitcroft
4454d4977c78STobias Klauser# Check that the storage class is at the beginning of a declaration
4455d4977c78STobias Klauser		if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
4456000d1cc1SJoe Perches			WARN("STORAGE_CLASS",
4457000d1cc1SJoe Perches			     "storage class should be at the beginning of the declaration\n" . $herecurr)
4458d4977c78STobias Klauser		}
4459d4977c78STobias Klauser
4460de7d4f0eSAndy Whitcroft# check the location of the inline attribute, that it is between
4461de7d4f0eSAndy Whitcroft# storage class and type.
44629c0ca6f9SAndy Whitcroft		if ($line =~ /\b$Type\s+$Inline\b/ ||
44639c0ca6f9SAndy Whitcroft		    $line =~ /\b$Inline\s+$Storage\b/) {
4464000d1cc1SJoe Perches			ERROR("INLINE_LOCATION",
4465000d1cc1SJoe Perches			      "inline keyword should sit between storage class and type\n" . $herecurr);
4466de7d4f0eSAndy Whitcroft		}
4467de7d4f0eSAndy Whitcroft
44688905a67cSAndy Whitcroft# Check for __inline__ and __inline, prefer inline
44692b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
44702b7ab453SJoe Perches		    $line =~ /\b(__inline__|__inline)\b/) {
4471d5e616fcSJoe Perches			if (WARN("INLINE",
4472d5e616fcSJoe Perches				 "plain inline is preferred over $1\n" . $herecurr) &&
4473d5e616fcSJoe Perches			    $fix) {
4474194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/;
4475d5e616fcSJoe Perches
4476d5e616fcSJoe Perches			}
44778905a67cSAndy Whitcroft		}
44788905a67cSAndy Whitcroft
44793d130fd0SJoe Perches# Check for __attribute__ packed, prefer __packed
44802b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
44812b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
4482000d1cc1SJoe Perches			WARN("PREFER_PACKED",
4483000d1cc1SJoe Perches			     "__packed is preferred over __attribute__((packed))\n" . $herecurr);
44843d130fd0SJoe Perches		}
44853d130fd0SJoe Perches
448639b7e287SJoe Perches# Check for __attribute__ aligned, prefer __aligned
44872b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
44882b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
4489000d1cc1SJoe Perches			WARN("PREFER_ALIGNED",
4490000d1cc1SJoe Perches			     "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
449139b7e287SJoe Perches		}
449239b7e287SJoe Perches
44935f14d3bdSJoe Perches# Check for __attribute__ format(printf, prefer __printf
44942b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
44952b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
4496d5e616fcSJoe Perches			if (WARN("PREFER_PRINTF",
4497d5e616fcSJoe Perches				 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) &&
4498d5e616fcSJoe Perches			    $fix) {
4499194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf\s*,\s*(.*)\)\s*\)\s*\)/"__printf(" . trim($1) . ")"/ex;
4500d5e616fcSJoe Perches
4501d5e616fcSJoe Perches			}
45025f14d3bdSJoe Perches		}
45035f14d3bdSJoe Perches
45046061d949SJoe Perches# Check for __attribute__ format(scanf, prefer __scanf
45052b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
45062b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
4507d5e616fcSJoe Perches			if (WARN("PREFER_SCANF",
4508d5e616fcSJoe Perches				 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) &&
4509d5e616fcSJoe Perches			    $fix) {
4510194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\s*,\s*(.*)\)\s*\)\s*\)/"__scanf(" . trim($1) . ")"/ex;
4511d5e616fcSJoe Perches			}
45126061d949SJoe Perches		}
45136061d949SJoe Perches
45148f53a9b8SJoe Perches# check for sizeof(&)
45158f53a9b8SJoe Perches		if ($line =~ /\bsizeof\s*\(\s*\&/) {
4516000d1cc1SJoe Perches			WARN("SIZEOF_ADDRESS",
4517000d1cc1SJoe Perches			     "sizeof(& should be avoided\n" . $herecurr);
45188f53a9b8SJoe Perches		}
45198f53a9b8SJoe Perches
452066c80b60SJoe Perches# check for sizeof without parenthesis
452166c80b60SJoe Perches		if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
4522d5e616fcSJoe Perches			if (WARN("SIZEOF_PARENTHESIS",
4523d5e616fcSJoe Perches				 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
4524d5e616fcSJoe Perches			    $fix) {
4525194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
4526d5e616fcSJoe Perches			}
452766c80b60SJoe Perches		}
452866c80b60SJoe Perches
4529428e2fdcSJoe Perches# check for line continuations in quoted strings with odd counts of "
4530428e2fdcSJoe Perches		if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) {
4531000d1cc1SJoe Perches			WARN("LINE_CONTINUATIONS",
4532000d1cc1SJoe Perches			     "Avoid line continuations in quoted strings\n" . $herecurr);
4533428e2fdcSJoe Perches		}
4534428e2fdcSJoe Perches
453588982feaSJoe Perches# check for struct spinlock declarations
453688982feaSJoe Perches		if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
453788982feaSJoe Perches			WARN("USE_SPINLOCK_T",
453888982feaSJoe Perches			     "struct spinlock should be spinlock_t\n" . $herecurr);
453988982feaSJoe Perches		}
454088982feaSJoe Perches
4541a6962d72SJoe Perches# check for seq_printf uses that could be seq_puts
454206668727SJoe Perches		if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
4543a6962d72SJoe Perches			my $fmt = get_quoted_string($line, $rawline);
454406668727SJoe Perches			if ($fmt ne "" && $fmt !~ /[^\\]\%/) {
4545d5e616fcSJoe Perches				if (WARN("PREFER_SEQ_PUTS",
4546d5e616fcSJoe Perches					 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
4547d5e616fcSJoe Perches				    $fix) {
4548194f66fcSJoe Perches					$fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/;
4549d5e616fcSJoe Perches				}
4550a6962d72SJoe Perches			}
4551a6962d72SJoe Perches		}
4552a6962d72SJoe Perches
4553554e165cSAndy Whitcroft# Check for misused memsets
4554d1fe9c09SJoe Perches		if ($^V && $^V ge 5.10.0 &&
4555d1fe9c09SJoe Perches		    defined $stat &&
4556d7c76ba7SJoe Perches		    $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/s) {
4557554e165cSAndy Whitcroft
4558d7c76ba7SJoe Perches			my $ms_addr = $2;
4559d1fe9c09SJoe Perches			my $ms_val = $7;
4560d1fe9c09SJoe Perches			my $ms_size = $12;
4561d7c76ba7SJoe Perches
4562554e165cSAndy Whitcroft			if ($ms_size =~ /^(0x|)0$/i) {
4563554e165cSAndy Whitcroft				ERROR("MEMSET",
4564d7c76ba7SJoe Perches				      "memset to 0's uses 0 as the 2nd argument, not the 3rd\n" . "$here\n$stat\n");
4565554e165cSAndy Whitcroft			} elsif ($ms_size =~ /^(0x|)1$/i) {
4566554e165cSAndy Whitcroft				WARN("MEMSET",
4567d7c76ba7SJoe Perches				     "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
4568d7c76ba7SJoe Perches			}
4569d7c76ba7SJoe Perches		}
4570d7c76ba7SJoe Perches
457198a9bba5SJoe Perches# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar)
457298a9bba5SJoe Perches		if ($^V && $^V ge 5.10.0 &&
457398a9bba5SJoe Perches		    $line =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/s) {
457498a9bba5SJoe Perches			if (WARN("PREFER_ETHER_ADDR_COPY",
457598a9bba5SJoe Perches				 "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . $herecurr) &&
457698a9bba5SJoe Perches			    $fix) {
4577194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/;
457898a9bba5SJoe Perches			}
457998a9bba5SJoe Perches		}
458098a9bba5SJoe Perches
4581d7c76ba7SJoe Perches# typecasts on min/max could be min_t/max_t
4582d1fe9c09SJoe Perches		if ($^V && $^V ge 5.10.0 &&
4583d1fe9c09SJoe Perches		    defined $stat &&
4584d7c76ba7SJoe Perches		    $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
4585d1fe9c09SJoe Perches			if (defined $2 || defined $7) {
4586d7c76ba7SJoe Perches				my $call = $1;
4587d7c76ba7SJoe Perches				my $cast1 = deparenthesize($2);
4588d7c76ba7SJoe Perches				my $arg1 = $3;
4589d1fe9c09SJoe Perches				my $cast2 = deparenthesize($7);
4590d1fe9c09SJoe Perches				my $arg2 = $8;
4591d7c76ba7SJoe Perches				my $cast;
4592d7c76ba7SJoe Perches
4593d1fe9c09SJoe Perches				if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
4594d7c76ba7SJoe Perches					$cast = "$cast1 or $cast2";
4595d7c76ba7SJoe Perches				} elsif ($cast1 ne "") {
4596d7c76ba7SJoe Perches					$cast = $cast1;
4597d7c76ba7SJoe Perches				} else {
4598d7c76ba7SJoe Perches					$cast = $cast2;
4599d7c76ba7SJoe Perches				}
4600d7c76ba7SJoe Perches				WARN("MINMAX",
4601d7c76ba7SJoe Perches				     "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
4602554e165cSAndy Whitcroft			}
4603554e165cSAndy Whitcroft		}
4604554e165cSAndy Whitcroft
46054a273195SJoe Perches# check usleep_range arguments
46064a273195SJoe Perches		if ($^V && $^V ge 5.10.0 &&
46074a273195SJoe Perches		    defined $stat &&
46084a273195SJoe Perches		    $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
46094a273195SJoe Perches			my $min = $1;
46104a273195SJoe Perches			my $max = $7;
46114a273195SJoe Perches			if ($min eq $max) {
46124a273195SJoe Perches				WARN("USLEEP_RANGE",
46134a273195SJoe Perches				     "usleep_range should not use min == max args; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
46144a273195SJoe Perches			} elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
46154a273195SJoe Perches				 $min > $max) {
46164a273195SJoe Perches				WARN("USLEEP_RANGE",
46174a273195SJoe Perches				     "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
46184a273195SJoe Perches			}
46194a273195SJoe Perches		}
46204a273195SJoe Perches
4621823b794cSJoe Perches# check for naked sscanf
4622823b794cSJoe Perches		if ($^V && $^V ge 5.10.0 &&
4623823b794cSJoe Perches		    defined $stat &&
46246c8bd707SJoe Perches		    $line =~ /\bsscanf\b/ &&
4625823b794cSJoe Perches		    ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
4626823b794cSJoe Perches		     $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ &&
4627823b794cSJoe Perches		     $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) {
4628823b794cSJoe Perches			my $lc = $stat =~ tr@\n@@;
4629823b794cSJoe Perches			$lc = $lc + $linenr;
4630823b794cSJoe Perches			my $stat_real = raw_line($linenr, 0);
4631823b794cSJoe Perches		        for (my $count = $linenr + 1; $count <= $lc; $count++) {
4632823b794cSJoe Perches				$stat_real = $stat_real . "\n" . raw_line($count, 0);
4633823b794cSJoe Perches			}
4634823b794cSJoe Perches			WARN("NAKED_SSCANF",
4635823b794cSJoe Perches			     "unchecked sscanf return value\n" . "$here\n$stat_real\n");
4636823b794cSJoe Perches		}
4637823b794cSJoe Perches
4638afc819abSJoe Perches# check for simple sscanf that should be kstrto<foo>
4639afc819abSJoe Perches		if ($^V && $^V ge 5.10.0 &&
4640afc819abSJoe Perches		    defined $stat &&
4641afc819abSJoe Perches		    $line =~ /\bsscanf\b/) {
4642afc819abSJoe Perches			my $lc = $stat =~ tr@\n@@;
4643afc819abSJoe Perches			$lc = $lc + $linenr;
4644afc819abSJoe Perches			my $stat_real = raw_line($linenr, 0);
4645afc819abSJoe Perches		        for (my $count = $linenr + 1; $count <= $lc; $count++) {
4646afc819abSJoe Perches				$stat_real = $stat_real . "\n" . raw_line($count, 0);
4647afc819abSJoe Perches			}
4648afc819abSJoe Perches			if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) {
4649afc819abSJoe Perches				my $format = $6;
4650afc819abSJoe Perches				my $count = $format =~ tr@%@%@;
4651afc819abSJoe Perches				if ($count == 1 &&
4652afc819abSJoe Perches				    $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) {
4653afc819abSJoe Perches					WARN("SSCANF_TO_KSTRTO",
4654afc819abSJoe Perches					     "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n");
4655afc819abSJoe Perches				}
4656afc819abSJoe Perches			}
4657afc819abSJoe Perches		}
4658afc819abSJoe Perches
465970dc8a48SJoe Perches# check for new externs in .h files.
466070dc8a48SJoe Perches		if ($realfile =~ /\.h$/ &&
466170dc8a48SJoe Perches		    $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
4662d1d85780SJoe Perches			if (CHK("AVOID_EXTERNS",
466370dc8a48SJoe Perches				"extern prototypes should be avoided in .h files\n" . $herecurr) &&
466470dc8a48SJoe Perches			    $fix) {
4665194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
466670dc8a48SJoe Perches			}
466770dc8a48SJoe Perches		}
466870dc8a48SJoe Perches
4669de7d4f0eSAndy Whitcroft# check for new externs in .c files.
4670171ae1a4SAndy Whitcroft		if ($realfile =~ /\.c$/ && defined $stat &&
4671c45dcabdSAndy Whitcroft		    $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
4672171ae1a4SAndy Whitcroft		{
4673c45dcabdSAndy Whitcroft			my $function_name = $1;
4674c45dcabdSAndy Whitcroft			my $paren_space = $2;
4675171ae1a4SAndy Whitcroft
4676171ae1a4SAndy Whitcroft			my $s = $stat;
4677171ae1a4SAndy Whitcroft			if (defined $cond) {
4678171ae1a4SAndy Whitcroft				substr($s, 0, length($cond), '');
4679171ae1a4SAndy Whitcroft			}
4680c45dcabdSAndy Whitcroft			if ($s =~ /^\s*;/ &&
4681c45dcabdSAndy Whitcroft			    $function_name ne 'uninitialized_var')
4682c45dcabdSAndy Whitcroft			{
4683000d1cc1SJoe Perches				WARN("AVOID_EXTERNS",
4684000d1cc1SJoe Perches				     "externs should be avoided in .c files\n" .  $herecurr);
4685de7d4f0eSAndy Whitcroft			}
4686de7d4f0eSAndy Whitcroft
4687171ae1a4SAndy Whitcroft			if ($paren_space =~ /\n/) {
4688000d1cc1SJoe Perches				WARN("FUNCTION_ARGUMENTS",
4689000d1cc1SJoe Perches				     "arguments for function declarations should follow identifier\n" . $herecurr);
4690171ae1a4SAndy Whitcroft			}
46919c9ba34eSAndy Whitcroft
46929c9ba34eSAndy Whitcroft		} elsif ($realfile =~ /\.c$/ && defined $stat &&
46939c9ba34eSAndy Whitcroft		    $stat =~ /^.\s*extern\s+/)
46949c9ba34eSAndy Whitcroft		{
4695000d1cc1SJoe Perches			WARN("AVOID_EXTERNS",
4696000d1cc1SJoe Perches			     "externs should be avoided in .c files\n" .  $herecurr);
4697171ae1a4SAndy Whitcroft		}
4698171ae1a4SAndy Whitcroft
4699de7d4f0eSAndy Whitcroft# checks for new __setup's
4700de7d4f0eSAndy Whitcroft		if ($rawline =~ /\b__setup\("([^"]*)"/) {
4701de7d4f0eSAndy Whitcroft			my $name = $1;
4702de7d4f0eSAndy Whitcroft
4703de7d4f0eSAndy Whitcroft			if (!grep(/$name/, @setup_docs)) {
4704000d1cc1SJoe Perches				CHK("UNDOCUMENTED_SETUP",
4705000d1cc1SJoe Perches				    "__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
4706de7d4f0eSAndy Whitcroft			}
4707653d4876SAndy Whitcroft		}
47089c0ca6f9SAndy Whitcroft
47099c0ca6f9SAndy Whitcroft# check for pointless casting of kmalloc return
4710caf2a54fSJoe Perches		if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) {
4711000d1cc1SJoe Perches			WARN("UNNECESSARY_CASTS",
4712000d1cc1SJoe Perches			     "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
47139c0ca6f9SAndy Whitcroft		}
471413214adfSAndy Whitcroft
4715a640d25cSJoe Perches# alloc style
4716a640d25cSJoe Perches# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
4717a640d25cSJoe Perches		if ($^V && $^V ge 5.10.0 &&
4718a640d25cSJoe Perches		    $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*([kv][mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
4719a640d25cSJoe Perches			CHK("ALLOC_SIZEOF_STRUCT",
4720a640d25cSJoe Perches			    "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
4721a640d25cSJoe Perches		}
4722a640d25cSJoe Perches
472360a55369SJoe Perches# check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc
472460a55369SJoe Perches		if ($^V && $^V ge 5.10.0 &&
4725e367455aSJoe Perches		    $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*(k[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)\s*,/) {
472660a55369SJoe Perches			my $oldfunc = $3;
472760a55369SJoe Perches			my $a1 = $4;
472860a55369SJoe Perches			my $a2 = $10;
472960a55369SJoe Perches			my $newfunc = "kmalloc_array";
473060a55369SJoe Perches			$newfunc = "kcalloc" if ($oldfunc eq "kzalloc");
473160a55369SJoe Perches			my $r1 = $a1;
473260a55369SJoe Perches			my $r2 = $a2;
473360a55369SJoe Perches			if ($a1 =~ /^sizeof\s*\S/) {
473460a55369SJoe Perches				$r1 = $a2;
473560a55369SJoe Perches				$r2 = $a1;
473660a55369SJoe Perches			}
4737e367455aSJoe Perches			if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ &&
4738e367455aSJoe Perches			    !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) {
4739e367455aSJoe Perches				if (WARN("ALLOC_WITH_MULTIPLY",
4740e367455aSJoe Perches					 "Prefer $newfunc over $oldfunc with multiply\n" . $herecurr) &&
4741e367455aSJoe Perches				    $fix) {
4742194f66fcSJoe Perches					$fixed[$fixlinenr] =~ s/\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*(k[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)/$1 . ' = ' . "$newfunc(" . trim($r1) . ', ' . trim($r2)/e;
474360a55369SJoe Perches
474460a55369SJoe Perches				}
474560a55369SJoe Perches			}
474660a55369SJoe Perches		}
474760a55369SJoe Perches
4748972fdea2SJoe Perches# check for krealloc arg reuse
4749972fdea2SJoe Perches		if ($^V && $^V ge 5.10.0 &&
4750972fdea2SJoe Perches		    $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*\1\s*,/) {
4751972fdea2SJoe Perches			WARN("KREALLOC_ARG_REUSE",
4752972fdea2SJoe Perches			     "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
4753972fdea2SJoe Perches		}
4754972fdea2SJoe Perches
47555ce59ae0SJoe Perches# check for alloc argument mismatch
47565ce59ae0SJoe Perches		if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
47575ce59ae0SJoe Perches			WARN("ALLOC_ARRAY_ARGS",
47585ce59ae0SJoe Perches			     "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
47595ce59ae0SJoe Perches		}
47605ce59ae0SJoe Perches
4761caf2a54fSJoe Perches# check for multiple semicolons
4762caf2a54fSJoe Perches		if ($line =~ /;\s*;\s*$/) {
4763d5e616fcSJoe Perches			if (WARN("ONE_SEMICOLON",
4764d5e616fcSJoe Perches				 "Statements terminations use 1 semicolon\n" . $herecurr) &&
4765d5e616fcSJoe Perches			    $fix) {
4766194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g;
4767d5e616fcSJoe Perches			}
4768d1e2ad07SJoe Perches		}
4769d1e2ad07SJoe Perches
4770c34c09a8SJoe Perches# check for case / default statements not preceeded by break/fallthrough/switch
4771c34c09a8SJoe Perches		if ($line =~ /^.\s*(?:case\s+(?:$Ident|$Constant)\s*|default):/) {
4772c34c09a8SJoe Perches			my $has_break = 0;
4773c34c09a8SJoe Perches			my $has_statement = 0;
4774c34c09a8SJoe Perches			my $count = 0;
4775c34c09a8SJoe Perches			my $prevline = $linenr;
4776c34c09a8SJoe Perches			while ($prevline > 1 && $count < 3 && !$has_break) {
4777c34c09a8SJoe Perches				$prevline--;
4778c34c09a8SJoe Perches				my $rline = $rawlines[$prevline - 1];
4779c34c09a8SJoe Perches				my $fline = $lines[$prevline - 1];
4780c34c09a8SJoe Perches				last if ($fline =~ /^\@\@/);
4781c34c09a8SJoe Perches				next if ($fline =~ /^\-/);
4782c34c09a8SJoe Perches				next if ($fline =~ /^.(?:\s*(?:case\s+(?:$Ident|$Constant)[\s$;]*|default):[\s$;]*)*$/);
4783c34c09a8SJoe Perches				$has_break = 1 if ($rline =~ /fall[\s_-]*(through|thru)/i);
4784c34c09a8SJoe Perches				next if ($fline =~ /^.[\s$;]*$/);
4785c34c09a8SJoe Perches				$has_statement = 1;
4786c34c09a8SJoe Perches				$count++;
4787c34c09a8SJoe Perches				$has_break = 1 if ($fline =~ /\bswitch\b|\b(?:break\s*;[\s$;]*$|return\b|goto\b|continue\b)/);
4788c34c09a8SJoe Perches			}
4789c34c09a8SJoe Perches			if (!$has_break && $has_statement) {
4790c34c09a8SJoe Perches				WARN("MISSING_BREAK",
4791c34c09a8SJoe Perches				     "Possible switch case/default not preceeded by break or fallthrough comment\n" . $herecurr);
4792c34c09a8SJoe Perches			}
4793c34c09a8SJoe Perches		}
4794c34c09a8SJoe Perches
4795d1e2ad07SJoe Perches# check for switch/default statements without a break;
4796d1e2ad07SJoe Perches		if ($^V && $^V ge 5.10.0 &&
4797d1e2ad07SJoe Perches		    defined $stat &&
4798d1e2ad07SJoe Perches		    $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
4799d1e2ad07SJoe Perches			my $ctx = '';
4800d1e2ad07SJoe Perches			my $herectx = $here . "\n";
4801d1e2ad07SJoe Perches			my $cnt = statement_rawlines($stat);
4802d1e2ad07SJoe Perches			for (my $n = 0; $n < $cnt; $n++) {
4803d1e2ad07SJoe Perches				$herectx .= raw_line($linenr, $n) . "\n";
4804d1e2ad07SJoe Perches			}
4805d1e2ad07SJoe Perches			WARN("DEFAULT_NO_BREAK",
4806d1e2ad07SJoe Perches			     "switch default: should use break\n" . $herectx);
4807caf2a54fSJoe Perches		}
4808caf2a54fSJoe Perches
480913214adfSAndy Whitcroft# check for gcc specific __FUNCTION__
4810d5e616fcSJoe Perches		if ($line =~ /\b__FUNCTION__\b/) {
4811d5e616fcSJoe Perches			if (WARN("USE_FUNC",
4812d5e616fcSJoe Perches				 "__func__ should be used instead of gcc specific __FUNCTION__\n"  . $herecurr) &&
4813d5e616fcSJoe Perches			    $fix) {
4814194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g;
4815d5e616fcSJoe Perches			}
481613214adfSAndy Whitcroft		}
4817773647a0SAndy Whitcroft
48182c92488aSJoe Perches# check for use of yield()
48192c92488aSJoe Perches		if ($line =~ /\byield\s*\(\s*\)/) {
48202c92488aSJoe Perches			WARN("YIELD",
48212c92488aSJoe Perches			     "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n"  . $herecurr);
48222c92488aSJoe Perches		}
48232c92488aSJoe Perches
4824179f8f40SJoe Perches# check for comparisons against true and false
4825179f8f40SJoe Perches		if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
4826179f8f40SJoe Perches			my $lead = $1;
4827179f8f40SJoe Perches			my $arg = $2;
4828179f8f40SJoe Perches			my $test = $3;
4829179f8f40SJoe Perches			my $otype = $4;
4830179f8f40SJoe Perches			my $trail = $5;
4831179f8f40SJoe Perches			my $op = "!";
4832179f8f40SJoe Perches
4833179f8f40SJoe Perches			($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
4834179f8f40SJoe Perches
4835179f8f40SJoe Perches			my $type = lc($otype);
4836179f8f40SJoe Perches			if ($type =~ /^(?:true|false)$/) {
4837179f8f40SJoe Perches				if (("$test" eq "==" && "$type" eq "true") ||
4838179f8f40SJoe Perches				    ("$test" eq "!=" && "$type" eq "false")) {
4839179f8f40SJoe Perches					$op = "";
4840179f8f40SJoe Perches				}
4841179f8f40SJoe Perches
4842179f8f40SJoe Perches				CHK("BOOL_COMPARISON",
4843179f8f40SJoe Perches				    "Using comparison to $otype is error prone\n" . $herecurr);
4844179f8f40SJoe Perches
4845179f8f40SJoe Perches## maybe suggesting a correct construct would better
4846179f8f40SJoe Perches##				    "Using comparison to $otype is error prone.  Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
4847179f8f40SJoe Perches
4848179f8f40SJoe Perches			}
4849179f8f40SJoe Perches		}
4850179f8f40SJoe Perches
48514882720bSThomas Gleixner# check for semaphores initialized locked
48524882720bSThomas Gleixner		if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
4853000d1cc1SJoe Perches			WARN("CONSIDER_COMPLETION",
4854000d1cc1SJoe Perches			     "consider using a completion\n" . $herecurr);
4855773647a0SAndy Whitcroft		}
48566712d858SJoe Perches
485767d0a075SJoe Perches# recommend kstrto* over simple_strto* and strict_strto*
485867d0a075SJoe Perches		if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
4859000d1cc1SJoe Perches			WARN("CONSIDER_KSTRTO",
486067d0a075SJoe Perches			     "$1 is obsolete, use k$3 instead\n" . $herecurr);
4861773647a0SAndy Whitcroft		}
48626712d858SJoe Perches
4863ae3ccc46SFabian Frederick# check for __initcall(), use device_initcall() explicitly or more appropriate function please
4864f3db6639SMichael Ellerman		if ($line =~ /^.\s*__initcall\s*\(/) {
4865000d1cc1SJoe Perches			WARN("USE_DEVICE_INITCALL",
4866ae3ccc46SFabian Frederick			     "please use device_initcall() or more appropriate function instead of __initcall() (see include/linux/init.h)\n" . $herecurr);
4867f3db6639SMichael Ellerman		}
48686712d858SJoe Perches
486979404849SEmese Revfy# check for various ops structs, ensure they are const.
487079404849SEmese Revfy		my $struct_ops = qr{acpi_dock_ops|
487179404849SEmese Revfy				address_space_operations|
487279404849SEmese Revfy				backlight_ops|
487379404849SEmese Revfy				block_device_operations|
487479404849SEmese Revfy				dentry_operations|
487579404849SEmese Revfy				dev_pm_ops|
487679404849SEmese Revfy				dma_map_ops|
487779404849SEmese Revfy				extent_io_ops|
487879404849SEmese Revfy				file_lock_operations|
487979404849SEmese Revfy				file_operations|
488079404849SEmese Revfy				hv_ops|
488179404849SEmese Revfy				ide_dma_ops|
488279404849SEmese Revfy				intel_dvo_dev_ops|
488379404849SEmese Revfy				item_operations|
488479404849SEmese Revfy				iwl_ops|
488579404849SEmese Revfy				kgdb_arch|
488679404849SEmese Revfy				kgdb_io|
488779404849SEmese Revfy				kset_uevent_ops|
488879404849SEmese Revfy				lock_manager_operations|
488979404849SEmese Revfy				microcode_ops|
489079404849SEmese Revfy				mtrr_ops|
489179404849SEmese Revfy				neigh_ops|
489279404849SEmese Revfy				nlmsvc_binding|
489379404849SEmese Revfy				pci_raw_ops|
489479404849SEmese Revfy				pipe_buf_operations|
489579404849SEmese Revfy				platform_hibernation_ops|
489679404849SEmese Revfy				platform_suspend_ops|
489779404849SEmese Revfy				proto_ops|
489879404849SEmese Revfy				rpc_pipe_ops|
489979404849SEmese Revfy				seq_operations|
490079404849SEmese Revfy				snd_ac97_build_ops|
490179404849SEmese Revfy				soc_pcmcia_socket_ops|
490279404849SEmese Revfy				stacktrace_ops|
490379404849SEmese Revfy				sysfs_ops|
490479404849SEmese Revfy				tty_operations|
490579404849SEmese Revfy				usb_mon_operations|
490679404849SEmese Revfy				wd_ops}x;
49076903ffb2SAndy Whitcroft		if ($line !~ /\bconst\b/ &&
490879404849SEmese Revfy		    $line =~ /\bstruct\s+($struct_ops)\b/) {
4909000d1cc1SJoe Perches			WARN("CONST_STRUCT",
4910000d1cc1SJoe Perches			     "struct $1 should normally be const\n" .
49116903ffb2SAndy Whitcroft				$herecurr);
49122b6db5cbSAndy Whitcroft		}
4913773647a0SAndy Whitcroft
4914773647a0SAndy Whitcroft# use of NR_CPUS is usually wrong
4915773647a0SAndy Whitcroft# ignore definitions of NR_CPUS and usage to define arrays as likely right
4916773647a0SAndy Whitcroft		if ($line =~ /\bNR_CPUS\b/ &&
4917c45dcabdSAndy Whitcroft		    $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
4918c45dcabdSAndy Whitcroft		    $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
4919171ae1a4SAndy Whitcroft		    $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
4920171ae1a4SAndy Whitcroft		    $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
4921171ae1a4SAndy Whitcroft		    $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
4922773647a0SAndy Whitcroft		{
4923000d1cc1SJoe Perches			WARN("NR_CPUS",
4924000d1cc1SJoe Perches			     "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
4925773647a0SAndy Whitcroft		}
49269c9ba34eSAndy Whitcroft
492752ea8506SJoe Perches# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
492852ea8506SJoe Perches		if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
492952ea8506SJoe Perches			ERROR("DEFINE_ARCH_HAS",
493052ea8506SJoe Perches			      "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
493152ea8506SJoe Perches		}
493252ea8506SJoe Perches
49339c9ba34eSAndy Whitcroft# check for %L{u,d,i} in strings
49349c9ba34eSAndy Whitcroft		my $string;
49359c9ba34eSAndy Whitcroft		while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
49369c9ba34eSAndy Whitcroft			$string = substr($rawline, $-[1], $+[1] - $-[1]);
49372a1bc5d5SAndy Whitcroft			$string =~ s/%%/__/g;
49389c9ba34eSAndy Whitcroft			if ($string =~ /(?<!%)%L[udi]/) {
4939000d1cc1SJoe Perches				WARN("PRINTF_L",
4940000d1cc1SJoe Perches				     "\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
49419c9ba34eSAndy Whitcroft				last;
49429c9ba34eSAndy Whitcroft			}
49439c9ba34eSAndy Whitcroft		}
4944691d77b6SAndy Whitcroft
4945691d77b6SAndy Whitcroft# whine mightly about in_atomic
4946691d77b6SAndy Whitcroft		if ($line =~ /\bin_atomic\s*\(/) {
4947691d77b6SAndy Whitcroft			if ($realfile =~ m@^drivers/@) {
4948000d1cc1SJoe Perches				ERROR("IN_ATOMIC",
4949000d1cc1SJoe Perches				      "do not use in_atomic in drivers\n" . $herecurr);
4950f4a87736SAndy Whitcroft			} elsif ($realfile !~ m@^kernel/@) {
4951000d1cc1SJoe Perches				WARN("IN_ATOMIC",
4952000d1cc1SJoe Perches				     "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
4953691d77b6SAndy Whitcroft			}
4954691d77b6SAndy Whitcroft		}
49551704f47bSPeter Zijlstra
49561704f47bSPeter Zijlstra# check for lockdep_set_novalidate_class
49571704f47bSPeter Zijlstra		if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
49581704f47bSPeter Zijlstra		    $line =~ /__lockdep_no_validate__\s*\)/ ) {
49591704f47bSPeter Zijlstra			if ($realfile !~ m@^kernel/lockdep@ &&
49601704f47bSPeter Zijlstra			    $realfile !~ m@^include/linux/lockdep@ &&
49611704f47bSPeter Zijlstra			    $realfile !~ m@^drivers/base/core@) {
4962000d1cc1SJoe Perches				ERROR("LOCKDEP",
4963000d1cc1SJoe Perches				      "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
49641704f47bSPeter Zijlstra			}
49651704f47bSPeter Zijlstra		}
496688f8831cSDave Jones
496788f8831cSDave Jones		if ($line =~ /debugfs_create_file.*S_IWUGO/ ||
496888f8831cSDave Jones		    $line =~ /DEVICE_ATTR.*S_IWUGO/ ) {
4969000d1cc1SJoe Perches			WARN("EXPORTED_WORLD_WRITABLE",
4970000d1cc1SJoe Perches			     "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
497188f8831cSDave Jones		}
49722435880fSJoe Perches
4973515a235eSJoe Perches# Mode permission misuses where it seems decimal should be octal
4974515a235eSJoe Perches# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop
4975515a235eSJoe Perches		if ($^V && $^V ge 5.10.0 &&
4976515a235eSJoe Perches		    $line =~ /$mode_perms_search/) {
49772435880fSJoe Perches			foreach my $entry (@mode_permission_funcs) {
49782435880fSJoe Perches				my $func = $entry->[0];
49792435880fSJoe Perches				my $arg_pos = $entry->[1];
49802435880fSJoe Perches
49812435880fSJoe Perches				my $skip_args = "";
49822435880fSJoe Perches				if ($arg_pos > 1) {
49832435880fSJoe Perches					$arg_pos--;
49842435880fSJoe Perches					$skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}";
49852435880fSJoe Perches				}
49862435880fSJoe Perches				my $test = "\\b$func\\s*\\(${skip_args}([\\d]+)\\s*[,\\)]";
4987515a235eSJoe Perches				if ($line =~ /$test/) {
49882435880fSJoe Perches					my $val = $1;
49892435880fSJoe Perches					$val = $6 if ($skip_args ne "");
49902435880fSJoe Perches
49911727cc70SJoe Perches					if ($val !~ /^0$/ &&
49921727cc70SJoe Perches					    (($val =~ /^$Int$/ && $val !~ /^$Octal$/) ||
49931727cc70SJoe Perches					     length($val) ne 4)) {
49942435880fSJoe Perches						ERROR("NON_OCTAL_PERMISSIONS",
49951727cc70SJoe Perches						      "Use 4 digit octal (0777) not decimal permissions\n" . $herecurr);
49962435880fSJoe Perches					}
49972435880fSJoe Perches				}
49982435880fSJoe Perches			}
499913214adfSAndy Whitcroft		}
5000515a235eSJoe Perches	}
500113214adfSAndy Whitcroft
500213214adfSAndy Whitcroft	# If we have no input at all, then there is nothing to report on
500313214adfSAndy Whitcroft	# so just keep quiet.
500413214adfSAndy Whitcroft	if ($#rawlines == -1) {
500513214adfSAndy Whitcroft		exit(0);
50060a920b5bSAndy Whitcroft	}
50070a920b5bSAndy Whitcroft
50088905a67cSAndy Whitcroft	# In mailback mode only produce a report in the negative, for
50098905a67cSAndy Whitcroft	# things that appear to be patches.
50108905a67cSAndy Whitcroft	if ($mailback && ($clean == 1 || !$is_patch)) {
50118905a67cSAndy Whitcroft		exit(0);
50128905a67cSAndy Whitcroft	}
50138905a67cSAndy Whitcroft
50148905a67cSAndy Whitcroft	# This is not a patch, and we are are in 'no-patch' mode so
50158905a67cSAndy Whitcroft	# just keep quiet.
50168905a67cSAndy Whitcroft	if (!$chk_patch && !$is_patch) {
50178905a67cSAndy Whitcroft		exit(0);
50188905a67cSAndy Whitcroft	}
50198905a67cSAndy Whitcroft
50208905a67cSAndy Whitcroft	if (!$is_patch) {
5021000d1cc1SJoe Perches		ERROR("NOT_UNIFIED_DIFF",
5022000d1cc1SJoe Perches		      "Does not appear to be a unified-diff format patch\n");
50230a920b5bSAndy Whitcroft	}
50240a920b5bSAndy Whitcroft	if ($is_patch && $chk_signoff && $signoff == 0) {
5025000d1cc1SJoe Perches		ERROR("MISSING_SIGN_OFF",
5026000d1cc1SJoe Perches		      "Missing Signed-off-by: line(s)\n");
50270a920b5bSAndy Whitcroft	}
50280a920b5bSAndy Whitcroft
5029f0a594c1SAndy Whitcroft	print report_dump();
503013214adfSAndy Whitcroft	if ($summary && !($clean == 1 && $quiet == 1)) {
503113214adfSAndy Whitcroft		print "$filename " if ($summary_file);
50326c72ffaaSAndy Whitcroft		print "total: $cnt_error errors, $cnt_warn warnings, " .
50336c72ffaaSAndy Whitcroft			(($check)? "$cnt_chk checks, " : "") .
50346c72ffaaSAndy Whitcroft			"$cnt_lines lines checked\n";
50358905a67cSAndy Whitcroft		print "\n" if ($quiet == 0);
50366c72ffaaSAndy Whitcroft	}
50378905a67cSAndy Whitcroft
5038d2c0a235SAndy Whitcroft	if ($quiet == 0) {
5039d1fe9c09SJoe Perches
5040d1fe9c09SJoe Perches		if ($^V lt 5.10.0) {
5041d1fe9c09SJoe Perches			print("NOTE: perl $^V is not modern enough to detect all possible issues.\n");
5042d1fe9c09SJoe Perches			print("An upgrade to at least perl v5.10.0 is suggested.\n\n");
5043d1fe9c09SJoe Perches		}
5044d1fe9c09SJoe Perches
5045d2c0a235SAndy Whitcroft		# If there were whitespace errors which cleanpatch can fix
5046d2c0a235SAndy Whitcroft		# then suggest that.
5047d2c0a235SAndy Whitcroft		if ($rpt_cleaners) {
5048d2c0a235SAndy Whitcroft			print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n";
5049d2c0a235SAndy Whitcroft			print "      scripts/cleanfile\n\n";
5050b0781216SMike Frysinger			$rpt_cleaners = 0;
5051d2c0a235SAndy Whitcroft		}
5052d2c0a235SAndy Whitcroft	}
5053d2c0a235SAndy Whitcroft
505491bfe484SJoe Perches	hash_show_words(\%use_type, "Used");
505591bfe484SJoe Perches	hash_show_words(\%ignore_type, "Ignored");
5056000d1cc1SJoe Perches
5057d752fcc8SJoe Perches	if ($clean == 0 && $fix &&
5058d752fcc8SJoe Perches	    ("@rawlines" ne "@fixed" ||
5059d752fcc8SJoe Perches	     $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) {
50609624b8d6SJoe Perches		my $newfile = $filename;
50619624b8d6SJoe Perches		$newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace);
50623705ce5bSJoe Perches		my $linecount = 0;
50633705ce5bSJoe Perches		my $f;
50643705ce5bSJoe Perches
5065d752fcc8SJoe Perches		@fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted);
5066d752fcc8SJoe Perches
50673705ce5bSJoe Perches		open($f, '>', $newfile)
50683705ce5bSJoe Perches		    or die "$P: Can't open $newfile for write\n";
50693705ce5bSJoe Perches		foreach my $fixed_line (@fixed) {
50703705ce5bSJoe Perches			$linecount++;
50713705ce5bSJoe Perches			if ($file) {
50723705ce5bSJoe Perches				if ($linecount > 3) {
50733705ce5bSJoe Perches					$fixed_line =~ s/^\+//;
50743705ce5bSJoe Perches					print $f $fixed_line . "\n";
50753705ce5bSJoe Perches				}
50763705ce5bSJoe Perches			} else {
50773705ce5bSJoe Perches				print $f $fixed_line . "\n";
50783705ce5bSJoe Perches			}
50793705ce5bSJoe Perches		}
50803705ce5bSJoe Perches		close($f);
50813705ce5bSJoe Perches
50823705ce5bSJoe Perches		if (!$quiet) {
50833705ce5bSJoe Perches			print << "EOM";
50843705ce5bSJoe PerchesWrote EXPERIMENTAL --fix correction(s) to '$newfile'
50853705ce5bSJoe Perches
50863705ce5bSJoe PerchesDo _NOT_ trust the results written to this file.
50873705ce5bSJoe PerchesDo _NOT_ submit these changes without inspecting them for correctness.
50883705ce5bSJoe Perches
50893705ce5bSJoe PerchesThis EXPERIMENTAL file is simply a convenience to help rewrite patches.
50903705ce5bSJoe PerchesNo warranties, expressed or implied...
50913705ce5bSJoe Perches
50923705ce5bSJoe PerchesEOM
50933705ce5bSJoe Perches		}
50943705ce5bSJoe Perches	}
50953705ce5bSJoe Perches
50960a920b5bSAndy Whitcroft	if ($clean == 1 && $quiet == 0) {
5097c2fdda0dSAndy Whitcroft		print "$vname has no obvious style problems and is ready for submission.\n"
50980a920b5bSAndy Whitcroft	}
50990a920b5bSAndy Whitcroft	if ($clean == 0 && $quiet == 0) {
5100000d1cc1SJoe Perches		print << "EOM";
5101000d1cc1SJoe Perches$vname has style problems, please review.
5102000d1cc1SJoe Perches
5103000d1cc1SJoe PerchesIf any of these errors are false positives, please report
5104000d1cc1SJoe Perchesthem to the maintainer, see CHECKPATCH in MAINTAINERS.
5105000d1cc1SJoe PerchesEOM
51060a920b5bSAndy Whitcroft	}
510713214adfSAndy Whitcroft
51080a920b5bSAndy Whitcroft	return $clean;
51090a920b5bSAndy Whitcroft}
5110