xref: /linux-6.15/scripts/checkpatch.pl (revision 1813087d)
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;
312*1813087dSJoe Perchesour $NonptrTypeMisordered;
3138716de38SJoe Perchesour $NonptrTypeWithAttr;
3148905a67cSAndy Whitcroftour $Type;
315*1813087dSJoe Perchesour $TypeMisordered;
3168905a67cSAndy Whitcroftour $Declare;
317*1813087dSJoe Perchesour $DeclareMisordered;
3188905a67cSAndy Whitcroft
31915662b3eSJoe Perchesour $NON_ASCII_UTF8	= qr{
32015662b3eSJoe Perches	[\xC2-\xDF][\x80-\xBF]               # non-overlong 2-byte
321171ae1a4SAndy Whitcroft	|  \xE0[\xA0-\xBF][\x80-\xBF]        # excluding overlongs
322171ae1a4SAndy Whitcroft	| [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}  # straight 3-byte
323171ae1a4SAndy Whitcroft	|  \xED[\x80-\x9F][\x80-\xBF]        # excluding surrogates
324171ae1a4SAndy Whitcroft	|  \xF0[\x90-\xBF][\x80-\xBF]{2}     # planes 1-3
325171ae1a4SAndy Whitcroft	| [\xF1-\xF3][\x80-\xBF]{3}          # planes 4-15
326171ae1a4SAndy Whitcroft	|  \xF4[\x80-\x8F][\x80-\xBF]{2}     # plane 16
327171ae1a4SAndy Whitcroft}x;
328171ae1a4SAndy Whitcroft
32915662b3eSJoe Perchesour $UTF8	= qr{
33015662b3eSJoe Perches	[\x09\x0A\x0D\x20-\x7E]              # ASCII
33115662b3eSJoe Perches	| $NON_ASCII_UTF8
33215662b3eSJoe Perches}x;
33315662b3eSJoe Perches
3348ed22cadSAndy Whitcroftour $typeTypedefs = qr{(?x:
335fb9e9096SAndy Whitcroft	(?:__)?(?:u|s|be|le)(?:8|16|32|64)|
3368ed22cadSAndy Whitcroft	atomic_t
3378ed22cadSAndy Whitcroft)};
3388ed22cadSAndy Whitcroft
339691e669bSJoe Perchesour $logFunctions = qr{(?x:
3406e60c02eSJoe Perches	printk(?:_ratelimited|_once|)|
3417d0b6594SJacob Keller	(?:[a-z0-9]+_){1,2}(?:printk|emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)(?:_ratelimited|_once|)|
3426e60c02eSJoe Perches	WARN(?:_RATELIMIT|_ONCE|)|
343b0531722SJoe Perches	panic|
34406668727SJoe Perches	MODULE_[A-Z_]+|
34506668727SJoe Perches	seq_vprintf|seq_printf|seq_puts
346691e669bSJoe Perches)};
347691e669bSJoe Perches
34820112475SJoe Perchesour $signature_tags = qr{(?xi:
34920112475SJoe Perches	Signed-off-by:|
35020112475SJoe Perches	Acked-by:|
35120112475SJoe Perches	Tested-by:|
35220112475SJoe Perches	Reviewed-by:|
35320112475SJoe Perches	Reported-by:|
3548543ae12SMugunthan V N	Suggested-by:|
35520112475SJoe Perches	To:|
35620112475SJoe Perches	Cc:
35720112475SJoe Perches)};
35820112475SJoe Perches
359*1813087dSJoe Perchesour @typeListMisordered = (
360*1813087dSJoe Perches	qr{char\s+(?:un)?signed},
361*1813087dSJoe Perches	qr{int\s+(?:(?:un)?signed\s+)?short\s},
362*1813087dSJoe Perches	qr{int\s+short(?:\s+(?:un)?signed)},
363*1813087dSJoe Perches	qr{short\s+int(?:\s+(?:un)?signed)},
364*1813087dSJoe Perches	qr{(?:un)?signed\s+int\s+short},
365*1813087dSJoe Perches	qr{short\s+(?:un)?signed},
366*1813087dSJoe Perches	qr{long\s+int\s+(?:un)?signed},
367*1813087dSJoe Perches	qr{int\s+long\s+(?:un)?signed},
368*1813087dSJoe Perches	qr{long\s+(?:un)?signed\s+int},
369*1813087dSJoe Perches	qr{int\s+(?:un)?signed\s+long},
370*1813087dSJoe Perches	qr{int\s+(?:un)?signed},
371*1813087dSJoe Perches	qr{int\s+long\s+long\s+(?:un)?signed},
372*1813087dSJoe Perches	qr{long\s+long\s+int\s+(?:un)?signed},
373*1813087dSJoe Perches	qr{long\s+long\s+(?:un)?signed\s+int},
374*1813087dSJoe Perches	qr{long\s+long\s+(?:un)?signed},
375*1813087dSJoe Perches	qr{long\s+(?:un)?signed},
376*1813087dSJoe Perches);
377*1813087dSJoe Perches
3788905a67cSAndy Whitcroftour @typeList = (
3798905a67cSAndy Whitcroft	qr{void},
3800c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?char},
3810c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?short\s+int},
3820c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?short},
3830c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?int},
3840c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?long\s+int},
3850c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?long\s+long\s+int},
3860c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?long\s+long},
3870c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?long},
3880c773d9dSJoe Perches	qr{(?:un)?signed},
3898905a67cSAndy Whitcroft	qr{float},
3908905a67cSAndy Whitcroft	qr{double},
3918905a67cSAndy Whitcroft	qr{bool},
3928905a67cSAndy Whitcroft	qr{struct\s+$Ident},
3938905a67cSAndy Whitcroft	qr{union\s+$Ident},
3948905a67cSAndy Whitcroft	qr{enum\s+$Ident},
3958905a67cSAndy Whitcroft	qr{${Ident}_t},
3968905a67cSAndy Whitcroft	qr{${Ident}_handler},
3978905a67cSAndy Whitcroft	qr{${Ident}_handler_fn},
398*1813087dSJoe Perches	@typeListMisordered,
3998905a67cSAndy Whitcroft);
4008716de38SJoe Perchesour @typeListWithAttr = (
4018716de38SJoe Perches	@typeList,
4028716de38SJoe Perches	qr{struct\s+$InitAttribute\s+$Ident},
4038716de38SJoe Perches	qr{union\s+$InitAttribute\s+$Ident},
4048716de38SJoe Perches);
4058716de38SJoe Perches
406c45dcabdSAndy Whitcroftour @modifierList = (
407c45dcabdSAndy Whitcroft	qr{fastcall},
408c45dcabdSAndy Whitcroft);
4098905a67cSAndy Whitcroft
4102435880fSJoe Perchesour @mode_permission_funcs = (
4112435880fSJoe Perches	["module_param", 3],
4122435880fSJoe Perches	["module_param_(?:array|named|string)", 4],
4132435880fSJoe Perches	["module_param_array_named", 5],
4142435880fSJoe Perches	["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2],
4152435880fSJoe Perches	["proc_create(?:_data|)", 2],
4162435880fSJoe Perches	["(?:CLASS|DEVICE|SENSOR)_ATTR", 2],
4172435880fSJoe Perches);
4182435880fSJoe Perches
419515a235eSJoe Perches#Create a search pattern for all these functions to speed up a loop below
420515a235eSJoe Perchesour $mode_perms_search = "";
421515a235eSJoe Perchesforeach my $entry (@mode_permission_funcs) {
422515a235eSJoe Perches	$mode_perms_search .= '|' if ($mode_perms_search ne "");
423515a235eSJoe Perches	$mode_perms_search .= $entry->[0];
424515a235eSJoe Perches}
425515a235eSJoe Perches
4263f7bac03SJoe Perchesour $declaration_macros = qr{(?x:
4273f7bac03SJoe Perches	(?:$Storage\s+)?(?:DECLARE|DEFINE)_[A-Z]+\s*\(|
4283f7bac03SJoe Perches	(?:$Storage\s+)?LIST_HEAD\s*\(
4293f7bac03SJoe Perches)};
4303f7bac03SJoe Perches
4317840a94cSWolfram Sangour $allowed_asm_includes = qr{(?x:
4327840a94cSWolfram Sang	irq|
4337840a94cSWolfram Sang	memory
4347840a94cSWolfram Sang)};
4357840a94cSWolfram Sang# memory.h: ARM has a custom one
4367840a94cSWolfram Sang
4378905a67cSAndy Whitcroftsub build_types {
438d2172eb5SAndy Whitcroft	my $mods = "(?x:  \n" . join("|\n  ", @modifierList) . "\n)";
439d2172eb5SAndy Whitcroft	my $all = "(?x:  \n" . join("|\n  ", @typeList) . "\n)";
440*1813087dSJoe Perches	my $Misordered = "(?x:  \n" . join("|\n  ", @typeListMisordered) . "\n)";
4418716de38SJoe Perches	my $allWithAttr = "(?x:  \n" . join("|\n  ", @typeListWithAttr) . "\n)";
442c8cb2ca3SAndy Whitcroft	$Modifier	= qr{(?:$Attribute|$Sparse|$mods)};
4438905a67cSAndy Whitcroft	$NonptrType	= qr{
444d2172eb5SAndy Whitcroft			(?:$Modifier\s+|const\s+)*
445cf655043SAndy Whitcroft			(?:
4466b48db24SAndy Whitcroft				(?:typeof|__typeof__)\s*\([^\)]*\)|
4478ed22cadSAndy Whitcroft				(?:$typeTypedefs\b)|
448c45dcabdSAndy Whitcroft				(?:${all}\b)
449cf655043SAndy Whitcroft			)
450c8cb2ca3SAndy Whitcroft			(?:\s+$Modifier|\s+const)*
4518905a67cSAndy Whitcroft		  }x;
452*1813087dSJoe Perches	$NonptrTypeMisordered	= qr{
453*1813087dSJoe Perches			(?:$Modifier\s+|const\s+)*
454*1813087dSJoe Perches			(?:
455*1813087dSJoe Perches				(?:${Misordered}\b)
456*1813087dSJoe Perches			)
457*1813087dSJoe Perches			(?:\s+$Modifier|\s+const)*
458*1813087dSJoe Perches		  }x;
4598716de38SJoe Perches	$NonptrTypeWithAttr	= qr{
4608716de38SJoe Perches			(?:$Modifier\s+|const\s+)*
4618716de38SJoe Perches			(?:
4628716de38SJoe Perches				(?:typeof|__typeof__)\s*\([^\)]*\)|
4638716de38SJoe Perches				(?:$typeTypedefs\b)|
4648716de38SJoe Perches				(?:${allWithAttr}\b)
4658716de38SJoe Perches			)
4668716de38SJoe Perches			(?:\s+$Modifier|\s+const)*
4678716de38SJoe Perches		  }x;
4688905a67cSAndy Whitcroft	$Type	= qr{
469c45dcabdSAndy Whitcroft			$NonptrType
4701574a29fSJoe Perches			(?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
471c8cb2ca3SAndy Whitcroft			(?:\s+$Inline|\s+$Modifier)*
4728905a67cSAndy Whitcroft		  }x;
473*1813087dSJoe Perches	$TypeMisordered	= qr{
474*1813087dSJoe Perches			$NonptrTypeMisordered
475*1813087dSJoe Perches			(?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
476*1813087dSJoe Perches			(?:\s+$Inline|\s+$Modifier)*
477*1813087dSJoe Perches		  }x;
47891cb5195SJoe Perches	$Declare	= qr{(?:$Storage\s+(?:$Inline\s+)?)?$Type};
479*1813087dSJoe Perches	$DeclareMisordered	= qr{(?:$Storage\s+(?:$Inline\s+)?)?$TypeMisordered};
4808905a67cSAndy Whitcroft}
4818905a67cSAndy Whitcroftbuild_types();
4826c72ffaaSAndy Whitcroft
4837d2367afSJoe Perchesour $Typecast	= qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
484d1fe9c09SJoe Perches
485d1fe9c09SJoe Perches# Using $balanced_parens, $LvalOrFunc, or $FuncArg
486d1fe9c09SJoe Perches# requires at least perl version v5.10.0
487d1fe9c09SJoe Perches# Any use must be runtime checked with $^V
488d1fe9c09SJoe Perches
489d1fe9c09SJoe Perchesour $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
4902435880fSJoe Perchesour $LvalOrFunc	= qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*};
491d7c76ba7SJoe Perchesour $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant)};
4927d2367afSJoe Perches
4937d2367afSJoe Perchessub deparenthesize {
4947d2367afSJoe Perches	my ($string) = @_;
4957d2367afSJoe Perches	return "" if (!defined($string));
4965b9553abSJoe Perches
4975b9553abSJoe Perches	while ($string =~ /^\s*\(.*\)\s*$/) {
4985b9553abSJoe Perches		$string =~ s@^\s*\(\s*@@;
4995b9553abSJoe Perches		$string =~ s@\s*\)\s*$@@;
5005b9553abSJoe Perches	}
5015b9553abSJoe Perches
5027d2367afSJoe Perches	$string =~ s@\s+@ @g;
5035b9553abSJoe Perches
5047d2367afSJoe Perches	return $string;
5057d2367afSJoe Perches}
5067d2367afSJoe Perches
5073445686aSJoe Perchessub seed_camelcase_file {
5083445686aSJoe Perches	my ($file) = @_;
5093445686aSJoe Perches
5103445686aSJoe Perches	return if (!(-f $file));
5113445686aSJoe Perches
5123445686aSJoe Perches	local $/;
5133445686aSJoe Perches
5143445686aSJoe Perches	open(my $include_file, '<', "$file")
5153445686aSJoe Perches	    or warn "$P: Can't read '$file' $!\n";
5163445686aSJoe Perches	my $text = <$include_file>;
5173445686aSJoe Perches	close($include_file);
5183445686aSJoe Perches
5193445686aSJoe Perches	my @lines = split('\n', $text);
5203445686aSJoe Perches
5213445686aSJoe Perches	foreach my $line (@lines) {
5223445686aSJoe Perches		next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/);
5233445686aSJoe Perches		if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) {
5243445686aSJoe Perches			$camelcase{$1} = 1;
52511ea516aSJoe Perches		} elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) {
52611ea516aSJoe Perches			$camelcase{$1} = 1;
52711ea516aSJoe Perches		} elsif ($line =~ /^\s*(?:union|struct|enum)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[;\{]/) {
5283445686aSJoe Perches			$camelcase{$1} = 1;
5293445686aSJoe Perches		}
5303445686aSJoe Perches	}
5313445686aSJoe Perches}
5323445686aSJoe Perches
5333445686aSJoe Perchesmy $camelcase_seeded = 0;
5343445686aSJoe Perchessub seed_camelcase_includes {
5353445686aSJoe Perches	return if ($camelcase_seeded);
5363445686aSJoe Perches
5373445686aSJoe Perches	my $files;
538c707a81dSJoe Perches	my $camelcase_cache = "";
539c707a81dSJoe Perches	my @include_files = ();
540c707a81dSJoe Perches
541c707a81dSJoe Perches	$camelcase_seeded = 1;
542351b2a1fSJoe Perches
5433645e328SRichard Genoud	if (-e ".git") {
544351b2a1fSJoe Perches		my $git_last_include_commit = `git log --no-merges --pretty=format:"%h%n" -1 -- include`;
545351b2a1fSJoe Perches		chomp $git_last_include_commit;
546c707a81dSJoe Perches		$camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit";
547c707a81dSJoe Perches	} else {
548c707a81dSJoe Perches		my $last_mod_date = 0;
549c707a81dSJoe Perches		$files = `find $root/include -name "*.h"`;
550c707a81dSJoe Perches		@include_files = split('\n', $files);
551c707a81dSJoe Perches		foreach my $file (@include_files) {
552c707a81dSJoe Perches			my $date = POSIX::strftime("%Y%m%d%H%M",
553c707a81dSJoe Perches						   localtime((stat $file)[9]));
554c707a81dSJoe Perches			$last_mod_date = $date if ($last_mod_date < $date);
555c707a81dSJoe Perches		}
556c707a81dSJoe Perches		$camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date";
557c707a81dSJoe Perches	}
558c707a81dSJoe Perches
559c707a81dSJoe Perches	if ($camelcase_cache ne "" && -f $camelcase_cache) {
560c707a81dSJoe Perches		open(my $camelcase_file, '<', "$camelcase_cache")
561c707a81dSJoe Perches		    or warn "$P: Can't read '$camelcase_cache' $!\n";
562351b2a1fSJoe Perches		while (<$camelcase_file>) {
563351b2a1fSJoe Perches			chomp;
564351b2a1fSJoe Perches			$camelcase{$_} = 1;
565351b2a1fSJoe Perches		}
566351b2a1fSJoe Perches		close($camelcase_file);
567351b2a1fSJoe Perches
568351b2a1fSJoe Perches		return;
569351b2a1fSJoe Perches	}
570c707a81dSJoe Perches
5713645e328SRichard Genoud	if (-e ".git") {
572c707a81dSJoe Perches		$files = `git ls-files "include/*.h"`;
573c707a81dSJoe Perches		@include_files = split('\n', $files);
5743445686aSJoe Perches	}
575c707a81dSJoe Perches
5763445686aSJoe Perches	foreach my $file (@include_files) {
5773445686aSJoe Perches		seed_camelcase_file($file);
5783445686aSJoe Perches	}
579351b2a1fSJoe Perches
580c707a81dSJoe Perches	if ($camelcase_cache ne "") {
581351b2a1fSJoe Perches		unlink glob ".checkpatch-camelcase.*";
582c707a81dSJoe Perches		open(my $camelcase_file, '>', "$camelcase_cache")
583c707a81dSJoe Perches		    or warn "$P: Can't write '$camelcase_cache' $!\n";
584351b2a1fSJoe Perches		foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) {
585351b2a1fSJoe Perches			print $camelcase_file ("$_\n");
586351b2a1fSJoe Perches		}
587351b2a1fSJoe Perches		close($camelcase_file);
588351b2a1fSJoe Perches	}
5893445686aSJoe Perches}
5903445686aSJoe Perches
591d311cd44SJoe Perchessub git_commit_info {
592d311cd44SJoe Perches	my ($commit, $id, $desc) = @_;
593d311cd44SJoe Perches
594d311cd44SJoe Perches	return ($id, $desc) if ((which("git") eq "") || !(-e ".git"));
595d311cd44SJoe Perches
596d311cd44SJoe Perches	my $output = `git log --no-color --format='%H %s' -1 $commit 2>&1`;
597d311cd44SJoe Perches	$output =~ s/^\s*//gm;
598d311cd44SJoe Perches	my @lines = split("\n", $output);
599d311cd44SJoe Perches
600d311cd44SJoe Perches	if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous\./) {
601d311cd44SJoe Perches# Maybe one day convert this block of bash into something that returns
602d311cd44SJoe Perches# all matching commit ids, but it's very slow...
603d311cd44SJoe Perches#
604d311cd44SJoe Perches#		echo "checking commits $1..."
605d311cd44SJoe Perches#		git rev-list --remotes | grep -i "^$1" |
606d311cd44SJoe Perches#		while read line ; do
607d311cd44SJoe Perches#		    git log --format='%H %s' -1 $line |
608d311cd44SJoe Perches#		    echo "commit $(cut -c 1-12,41-)"
609d311cd44SJoe Perches#		done
610d311cd44SJoe Perches	} elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./) {
611d311cd44SJoe Perches	} else {
612d311cd44SJoe Perches		$id = substr($lines[0], 0, 12);
613d311cd44SJoe Perches		$desc = substr($lines[0], 41);
614d311cd44SJoe Perches	}
615d311cd44SJoe Perches
616d311cd44SJoe Perches	return ($id, $desc);
617d311cd44SJoe Perches}
618d311cd44SJoe Perches
6196c72ffaaSAndy Whitcroft$chk_signoff = 0 if ($file);
6200a920b5bSAndy Whitcroft
62100df344fSAndy Whitcroftmy @rawlines = ();
622c2fdda0dSAndy Whitcroftmy @lines = ();
6233705ce5bSJoe Perchesmy @fixed = ();
624d752fcc8SJoe Perchesmy @fixed_inserted = ();
625d752fcc8SJoe Perchesmy @fixed_deleted = ();
626194f66fcSJoe Perchesmy $fixlinenr = -1;
627194f66fcSJoe Perches
628c2fdda0dSAndy Whitcroftmy $vname;
6296c72ffaaSAndy Whitcroftfor my $filename (@ARGV) {
63021caa13cSAndy Whitcroft	my $FILE;
6316c72ffaaSAndy Whitcroft	if ($file) {
63221caa13cSAndy Whitcroft		open($FILE, '-|', "diff -u /dev/null $filename") ||
6336c72ffaaSAndy Whitcroft			die "$P: $filename: diff failed - $!\n";
63421caa13cSAndy Whitcroft	} elsif ($filename eq '-') {
63521caa13cSAndy Whitcroft		open($FILE, '<&STDIN');
6366c72ffaaSAndy Whitcroft	} else {
63721caa13cSAndy Whitcroft		open($FILE, '<', "$filename") ||
6386c72ffaaSAndy Whitcroft			die "$P: $filename: open failed - $!\n";
6396c72ffaaSAndy Whitcroft	}
640c2fdda0dSAndy Whitcroft	if ($filename eq '-') {
641c2fdda0dSAndy Whitcroft		$vname = 'Your patch';
642c2fdda0dSAndy Whitcroft	} else {
643c2fdda0dSAndy Whitcroft		$vname = $filename;
644c2fdda0dSAndy Whitcroft	}
64521caa13cSAndy Whitcroft	while (<$FILE>) {
6460a920b5bSAndy Whitcroft		chomp;
64700df344fSAndy Whitcroft		push(@rawlines, $_);
6486c72ffaaSAndy Whitcroft	}
64921caa13cSAndy Whitcroft	close($FILE);
650c2fdda0dSAndy Whitcroft	if (!process($filename)) {
6510a920b5bSAndy Whitcroft		$exit = 1;
6520a920b5bSAndy Whitcroft	}
65300df344fSAndy Whitcroft	@rawlines = ();
65413214adfSAndy Whitcroft	@lines = ();
6553705ce5bSJoe Perches	@fixed = ();
656d752fcc8SJoe Perches	@fixed_inserted = ();
657d752fcc8SJoe Perches	@fixed_deleted = ();
658194f66fcSJoe Perches	$fixlinenr = -1;
6590a920b5bSAndy Whitcroft}
6600a920b5bSAndy Whitcroft
6610a920b5bSAndy Whitcroftexit($exit);
6620a920b5bSAndy Whitcroft
6630a920b5bSAndy Whitcroftsub top_of_kernel_tree {
6646c72ffaaSAndy Whitcroft	my ($root) = @_;
6656c72ffaaSAndy Whitcroft
6666c72ffaaSAndy Whitcroft	my @tree_check = (
6676c72ffaaSAndy Whitcroft		"COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
6686c72ffaaSAndy Whitcroft		"README", "Documentation", "arch", "include", "drivers",
6696c72ffaaSAndy Whitcroft		"fs", "init", "ipc", "kernel", "lib", "scripts",
6706c72ffaaSAndy Whitcroft	);
6716c72ffaaSAndy Whitcroft
6726c72ffaaSAndy Whitcroft	foreach my $check (@tree_check) {
6736c72ffaaSAndy Whitcroft		if (! -e $root . '/' . $check) {
6740a920b5bSAndy Whitcroft			return 0;
6750a920b5bSAndy Whitcroft		}
6766c72ffaaSAndy Whitcroft	}
6776c72ffaaSAndy Whitcroft	return 1;
6786c72ffaaSAndy Whitcroft}
6790a920b5bSAndy Whitcroft
68020112475SJoe Perchessub parse_email {
68120112475SJoe Perches	my ($formatted_email) = @_;
68220112475SJoe Perches
68320112475SJoe Perches	my $name = "";
68420112475SJoe Perches	my $address = "";
68520112475SJoe Perches	my $comment = "";
68620112475SJoe Perches
68720112475SJoe Perches	if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
68820112475SJoe Perches		$name = $1;
68920112475SJoe Perches		$address = $2;
69020112475SJoe Perches		$comment = $3 if defined $3;
69120112475SJoe Perches	} elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
69220112475SJoe Perches		$address = $1;
69320112475SJoe Perches		$comment = $2 if defined $2;
69420112475SJoe Perches	} elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
69520112475SJoe Perches		$address = $1;
69620112475SJoe Perches		$comment = $2 if defined $2;
69720112475SJoe Perches		$formatted_email =~ s/$address.*$//;
69820112475SJoe Perches		$name = $formatted_email;
6993705ce5bSJoe Perches		$name = trim($name);
70020112475SJoe Perches		$name =~ s/^\"|\"$//g;
70120112475SJoe Perches		# If there's a name left after stripping spaces and
70220112475SJoe Perches		# leading quotes, and the address doesn't have both
70320112475SJoe Perches		# leading and trailing angle brackets, the address
70420112475SJoe Perches		# is invalid. ie:
70520112475SJoe Perches		#   "joe smith [email protected]" bad
70620112475SJoe Perches		#   "joe smith <[email protected]" bad
70720112475SJoe Perches		if ($name ne "" && $address !~ /^<[^>]+>$/) {
70820112475SJoe Perches			$name = "";
70920112475SJoe Perches			$address = "";
71020112475SJoe Perches			$comment = "";
71120112475SJoe Perches		}
71220112475SJoe Perches	}
71320112475SJoe Perches
7143705ce5bSJoe Perches	$name = trim($name);
71520112475SJoe Perches	$name =~ s/^\"|\"$//g;
7163705ce5bSJoe Perches	$address = trim($address);
71720112475SJoe Perches	$address =~ s/^\<|\>$//g;
71820112475SJoe Perches
71920112475SJoe Perches	if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
72020112475SJoe Perches		$name =~ s/(?<!\\)"/\\"/g; ##escape quotes
72120112475SJoe Perches		$name = "\"$name\"";
72220112475SJoe Perches	}
72320112475SJoe Perches
72420112475SJoe Perches	return ($name, $address, $comment);
72520112475SJoe Perches}
72620112475SJoe Perches
72720112475SJoe Perchessub format_email {
72820112475SJoe Perches	my ($name, $address) = @_;
72920112475SJoe Perches
73020112475SJoe Perches	my $formatted_email;
73120112475SJoe Perches
7323705ce5bSJoe Perches	$name = trim($name);
73320112475SJoe Perches	$name =~ s/^\"|\"$//g;
7343705ce5bSJoe Perches	$address = trim($address);
73520112475SJoe Perches
73620112475SJoe Perches	if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
73720112475SJoe Perches		$name =~ s/(?<!\\)"/\\"/g; ##escape quotes
73820112475SJoe Perches		$name = "\"$name\"";
73920112475SJoe Perches	}
74020112475SJoe Perches
74120112475SJoe Perches	if ("$name" eq "") {
74220112475SJoe Perches		$formatted_email = "$address";
74320112475SJoe Perches	} else {
74420112475SJoe Perches		$formatted_email = "$name <$address>";
74520112475SJoe Perches	}
74620112475SJoe Perches
74720112475SJoe Perches	return $formatted_email;
74820112475SJoe Perches}
74920112475SJoe Perches
750d311cd44SJoe Perchessub which {
751d311cd44SJoe Perches	my ($bin) = @_;
752d311cd44SJoe Perches
753d311cd44SJoe Perches	foreach my $path (split(/:/, $ENV{PATH})) {
754d311cd44SJoe Perches		if (-e "$path/$bin") {
755d311cd44SJoe Perches			return "$path/$bin";
756d311cd44SJoe Perches		}
757d311cd44SJoe Perches	}
758d311cd44SJoe Perches
759d311cd44SJoe Perches	return "";
760d311cd44SJoe Perches}
761d311cd44SJoe Perches
762000d1cc1SJoe Perchessub which_conf {
763000d1cc1SJoe Perches	my ($conf) = @_;
764000d1cc1SJoe Perches
765000d1cc1SJoe Perches	foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
766000d1cc1SJoe Perches		if (-e "$path/$conf") {
767000d1cc1SJoe Perches			return "$path/$conf";
768000d1cc1SJoe Perches		}
769000d1cc1SJoe Perches	}
770000d1cc1SJoe Perches
771000d1cc1SJoe Perches	return "";
772000d1cc1SJoe Perches}
773000d1cc1SJoe Perches
7740a920b5bSAndy Whitcroftsub expand_tabs {
7750a920b5bSAndy Whitcroft	my ($str) = @_;
7760a920b5bSAndy Whitcroft
7770a920b5bSAndy Whitcroft	my $res = '';
7780a920b5bSAndy Whitcroft	my $n = 0;
7790a920b5bSAndy Whitcroft	for my $c (split(//, $str)) {
7800a920b5bSAndy Whitcroft		if ($c eq "\t") {
7810a920b5bSAndy Whitcroft			$res .= ' ';
7820a920b5bSAndy Whitcroft			$n++;
7830a920b5bSAndy Whitcroft			for (; ($n % 8) != 0; $n++) {
7840a920b5bSAndy Whitcroft				$res .= ' ';
7850a920b5bSAndy Whitcroft			}
7860a920b5bSAndy Whitcroft			next;
7870a920b5bSAndy Whitcroft		}
7880a920b5bSAndy Whitcroft		$res .= $c;
7890a920b5bSAndy Whitcroft		$n++;
7900a920b5bSAndy Whitcroft	}
7910a920b5bSAndy Whitcroft
7920a920b5bSAndy Whitcroft	return $res;
7930a920b5bSAndy Whitcroft}
7946c72ffaaSAndy Whitcroftsub copy_spacing {
795773647a0SAndy Whitcroft	(my $res = shift) =~ tr/\t/ /c;
7966c72ffaaSAndy Whitcroft	return $res;
7976c72ffaaSAndy Whitcroft}
7980a920b5bSAndy Whitcroft
7994a0df2efSAndy Whitcroftsub line_stats {
8004a0df2efSAndy Whitcroft	my ($line) = @_;
8014a0df2efSAndy Whitcroft
8024a0df2efSAndy Whitcroft	# Drop the diff line leader and expand tabs
8034a0df2efSAndy Whitcroft	$line =~ s/^.//;
8044a0df2efSAndy Whitcroft	$line = expand_tabs($line);
8054a0df2efSAndy Whitcroft
8064a0df2efSAndy Whitcroft	# Pick the indent from the front of the line.
8074a0df2efSAndy Whitcroft	my ($white) = ($line =~ /^(\s*)/);
8084a0df2efSAndy Whitcroft
8094a0df2efSAndy Whitcroft	return (length($line), length($white));
8104a0df2efSAndy Whitcroft}
8114a0df2efSAndy Whitcroft
812773647a0SAndy Whitcroftmy $sanitise_quote = '';
813773647a0SAndy Whitcroft
814773647a0SAndy Whitcroftsub sanitise_line_reset {
815773647a0SAndy Whitcroft	my ($in_comment) = @_;
816773647a0SAndy Whitcroft
817773647a0SAndy Whitcroft	if ($in_comment) {
818773647a0SAndy Whitcroft		$sanitise_quote = '*/';
819773647a0SAndy Whitcroft	} else {
820773647a0SAndy Whitcroft		$sanitise_quote = '';
821773647a0SAndy Whitcroft	}
822773647a0SAndy Whitcroft}
82300df344fSAndy Whitcroftsub sanitise_line {
82400df344fSAndy Whitcroft	my ($line) = @_;
82500df344fSAndy Whitcroft
82600df344fSAndy Whitcroft	my $res = '';
82700df344fSAndy Whitcroft	my $l = '';
82800df344fSAndy Whitcroft
829c2fdda0dSAndy Whitcroft	my $qlen = 0;
830773647a0SAndy Whitcroft	my $off = 0;
831773647a0SAndy Whitcroft	my $c;
83200df344fSAndy Whitcroft
833773647a0SAndy Whitcroft	# Always copy over the diff marker.
834773647a0SAndy Whitcroft	$res = substr($line, 0, 1);
835773647a0SAndy Whitcroft
836773647a0SAndy Whitcroft	for ($off = 1; $off < length($line); $off++) {
837773647a0SAndy Whitcroft		$c = substr($line, $off, 1);
838773647a0SAndy Whitcroft
839773647a0SAndy Whitcroft		# Comments we are wacking completly including the begin
840773647a0SAndy Whitcroft		# and end, all to $;.
841773647a0SAndy Whitcroft		if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
842773647a0SAndy Whitcroft			$sanitise_quote = '*/';
843773647a0SAndy Whitcroft
844773647a0SAndy Whitcroft			substr($res, $off, 2, "$;$;");
845773647a0SAndy Whitcroft			$off++;
84600df344fSAndy Whitcroft			next;
847773647a0SAndy Whitcroft		}
84881bc0e02SAndy Whitcroft		if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
849773647a0SAndy Whitcroft			$sanitise_quote = '';
850773647a0SAndy Whitcroft			substr($res, $off, 2, "$;$;");
851773647a0SAndy Whitcroft			$off++;
852773647a0SAndy Whitcroft			next;
853773647a0SAndy Whitcroft		}
854113f04a8SDaniel Walker		if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
855113f04a8SDaniel Walker			$sanitise_quote = '//';
856113f04a8SDaniel Walker
857113f04a8SDaniel Walker			substr($res, $off, 2, $sanitise_quote);
858113f04a8SDaniel Walker			$off++;
859113f04a8SDaniel Walker			next;
860113f04a8SDaniel Walker		}
861773647a0SAndy Whitcroft
862773647a0SAndy Whitcroft		# A \ in a string means ignore the next character.
863773647a0SAndy Whitcroft		if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
864773647a0SAndy Whitcroft		    $c eq "\\") {
865773647a0SAndy Whitcroft			substr($res, $off, 2, 'XX');
866773647a0SAndy Whitcroft			$off++;
867773647a0SAndy Whitcroft			next;
868773647a0SAndy Whitcroft		}
869773647a0SAndy Whitcroft		# Regular quotes.
870773647a0SAndy Whitcroft		if ($c eq "'" || $c eq '"') {
871773647a0SAndy Whitcroft			if ($sanitise_quote eq '') {
872773647a0SAndy Whitcroft				$sanitise_quote = $c;
873773647a0SAndy Whitcroft
874773647a0SAndy Whitcroft				substr($res, $off, 1, $c);
875773647a0SAndy Whitcroft				next;
876773647a0SAndy Whitcroft			} elsif ($sanitise_quote eq $c) {
877773647a0SAndy Whitcroft				$sanitise_quote = '';
87800df344fSAndy Whitcroft			}
87900df344fSAndy Whitcroft		}
880773647a0SAndy Whitcroft
881fae17daeSAndy Whitcroft		#print "c<$c> SQ<$sanitise_quote>\n";
882773647a0SAndy Whitcroft		if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
883773647a0SAndy Whitcroft			substr($res, $off, 1, $;);
884113f04a8SDaniel Walker		} elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
885113f04a8SDaniel Walker			substr($res, $off, 1, $;);
886773647a0SAndy Whitcroft		} elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
887773647a0SAndy Whitcroft			substr($res, $off, 1, 'X');
88800df344fSAndy Whitcroft		} else {
889773647a0SAndy Whitcroft			substr($res, $off, 1, $c);
89000df344fSAndy Whitcroft		}
891c2fdda0dSAndy Whitcroft	}
892c2fdda0dSAndy Whitcroft
893113f04a8SDaniel Walker	if ($sanitise_quote eq '//') {
894113f04a8SDaniel Walker		$sanitise_quote = '';
895113f04a8SDaniel Walker	}
896113f04a8SDaniel Walker
897c2fdda0dSAndy Whitcroft	# The pathname on a #include may be surrounded by '<' and '>'.
898c45dcabdSAndy Whitcroft	if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
899c2fdda0dSAndy Whitcroft		my $clean = 'X' x length($1);
900c2fdda0dSAndy Whitcroft		$res =~ s@\<.*\>@<$clean>@;
901c2fdda0dSAndy Whitcroft
902c2fdda0dSAndy Whitcroft	# The whole of a #error is a string.
903c45dcabdSAndy Whitcroft	} elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
904c2fdda0dSAndy Whitcroft		my $clean = 'X' x length($1);
905c45dcabdSAndy Whitcroft		$res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
906c2fdda0dSAndy Whitcroft	}
907c2fdda0dSAndy Whitcroft
90800df344fSAndy Whitcroft	return $res;
90900df344fSAndy Whitcroft}
91000df344fSAndy Whitcroft
911a6962d72SJoe Perchessub get_quoted_string {
912a6962d72SJoe Perches	my ($line, $rawline) = @_;
913a6962d72SJoe Perches
914a6962d72SJoe Perches	return "" if ($line !~ m/(\"[X]+\")/g);
915a6962d72SJoe Perches	return substr($rawline, $-[0], $+[0] - $-[0]);
916a6962d72SJoe Perches}
917a6962d72SJoe Perches
9188905a67cSAndy Whitcroftsub ctx_statement_block {
9198905a67cSAndy Whitcroft	my ($linenr, $remain, $off) = @_;
9208905a67cSAndy Whitcroft	my $line = $linenr - 1;
9218905a67cSAndy Whitcroft	my $blk = '';
9228905a67cSAndy Whitcroft	my $soff = $off;
9238905a67cSAndy Whitcroft	my $coff = $off - 1;
924773647a0SAndy Whitcroft	my $coff_set = 0;
9258905a67cSAndy Whitcroft
92613214adfSAndy Whitcroft	my $loff = 0;
92713214adfSAndy Whitcroft
9288905a67cSAndy Whitcroft	my $type = '';
9298905a67cSAndy Whitcroft	my $level = 0;
930a2750645SAndy Whitcroft	my @stack = ();
931cf655043SAndy Whitcroft	my $p;
9328905a67cSAndy Whitcroft	my $c;
9338905a67cSAndy Whitcroft	my $len = 0;
93413214adfSAndy Whitcroft
93513214adfSAndy Whitcroft	my $remainder;
9368905a67cSAndy Whitcroft	while (1) {
937a2750645SAndy Whitcroft		@stack = (['', 0]) if ($#stack == -1);
938a2750645SAndy Whitcroft
939773647a0SAndy Whitcroft		#warn "CSB: blk<$blk> remain<$remain>\n";
9408905a67cSAndy Whitcroft		# If we are about to drop off the end, pull in more
9418905a67cSAndy Whitcroft		# context.
9428905a67cSAndy Whitcroft		if ($off >= $len) {
9438905a67cSAndy Whitcroft			for (; $remain > 0; $line++) {
944dea33496SAndy Whitcroft				last if (!defined $lines[$line]);
945c2fdda0dSAndy Whitcroft				next if ($lines[$line] =~ /^-/);
9468905a67cSAndy Whitcroft				$remain--;
94713214adfSAndy Whitcroft				$loff = $len;
948c2fdda0dSAndy Whitcroft				$blk .= $lines[$line] . "\n";
9498905a67cSAndy Whitcroft				$len = length($blk);
9508905a67cSAndy Whitcroft				$line++;
9518905a67cSAndy Whitcroft				last;
9528905a67cSAndy Whitcroft			}
9538905a67cSAndy Whitcroft			# Bail if there is no further context.
9548905a67cSAndy Whitcroft			#warn "CSB: blk<$blk> off<$off> len<$len>\n";
95513214adfSAndy Whitcroft			if ($off >= $len) {
9568905a67cSAndy Whitcroft				last;
9578905a67cSAndy Whitcroft			}
958f74bd194SAndy Whitcroft			if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
959f74bd194SAndy Whitcroft				$level++;
960f74bd194SAndy Whitcroft				$type = '#';
961f74bd194SAndy Whitcroft			}
9628905a67cSAndy Whitcroft		}
963cf655043SAndy Whitcroft		$p = $c;
9648905a67cSAndy Whitcroft		$c = substr($blk, $off, 1);
96513214adfSAndy Whitcroft		$remainder = substr($blk, $off);
9668905a67cSAndy Whitcroft
967773647a0SAndy Whitcroft		#warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
9684635f4fbSAndy Whitcroft
9694635f4fbSAndy Whitcroft		# Handle nested #if/#else.
9704635f4fbSAndy Whitcroft		if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
9714635f4fbSAndy Whitcroft			push(@stack, [ $type, $level ]);
9724635f4fbSAndy Whitcroft		} elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
9734635f4fbSAndy Whitcroft			($type, $level) = @{$stack[$#stack - 1]};
9744635f4fbSAndy Whitcroft		} elsif ($remainder =~ /^#\s*endif\b/) {
9754635f4fbSAndy Whitcroft			($type, $level) = @{pop(@stack)};
9764635f4fbSAndy Whitcroft		}
9774635f4fbSAndy Whitcroft
9788905a67cSAndy Whitcroft		# Statement ends at the ';' or a close '}' at the
9798905a67cSAndy Whitcroft		# outermost level.
9808905a67cSAndy Whitcroft		if ($level == 0 && $c eq ';') {
9818905a67cSAndy Whitcroft			last;
9828905a67cSAndy Whitcroft		}
9838905a67cSAndy Whitcroft
98413214adfSAndy Whitcroft		# An else is really a conditional as long as its not else if
985773647a0SAndy Whitcroft		if ($level == 0 && $coff_set == 0 &&
986773647a0SAndy Whitcroft				(!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
987773647a0SAndy Whitcroft				$remainder =~ /^(else)(?:\s|{)/ &&
988773647a0SAndy Whitcroft				$remainder !~ /^else\s+if\b/) {
989773647a0SAndy Whitcroft			$coff = $off + length($1) - 1;
990773647a0SAndy Whitcroft			$coff_set = 1;
991773647a0SAndy Whitcroft			#warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
992773647a0SAndy Whitcroft			#warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
99313214adfSAndy Whitcroft		}
99413214adfSAndy Whitcroft
9958905a67cSAndy Whitcroft		if (($type eq '' || $type eq '(') && $c eq '(') {
9968905a67cSAndy Whitcroft			$level++;
9978905a67cSAndy Whitcroft			$type = '(';
9988905a67cSAndy Whitcroft		}
9998905a67cSAndy Whitcroft		if ($type eq '(' && $c eq ')') {
10008905a67cSAndy Whitcroft			$level--;
10018905a67cSAndy Whitcroft			$type = ($level != 0)? '(' : '';
10028905a67cSAndy Whitcroft
10038905a67cSAndy Whitcroft			if ($level == 0 && $coff < $soff) {
10048905a67cSAndy Whitcroft				$coff = $off;
1005773647a0SAndy Whitcroft				$coff_set = 1;
1006773647a0SAndy Whitcroft				#warn "CSB: mark coff<$coff>\n";
10078905a67cSAndy Whitcroft			}
10088905a67cSAndy Whitcroft		}
10098905a67cSAndy Whitcroft		if (($type eq '' || $type eq '{') && $c eq '{') {
10108905a67cSAndy Whitcroft			$level++;
10118905a67cSAndy Whitcroft			$type = '{';
10128905a67cSAndy Whitcroft		}
10138905a67cSAndy Whitcroft		if ($type eq '{' && $c eq '}') {
10148905a67cSAndy Whitcroft			$level--;
10158905a67cSAndy Whitcroft			$type = ($level != 0)? '{' : '';
10168905a67cSAndy Whitcroft
10178905a67cSAndy Whitcroft			if ($level == 0) {
1018b998e001SPatrick Pannuto				if (substr($blk, $off + 1, 1) eq ';') {
1019b998e001SPatrick Pannuto					$off++;
1020b998e001SPatrick Pannuto				}
10218905a67cSAndy Whitcroft				last;
10228905a67cSAndy Whitcroft			}
10238905a67cSAndy Whitcroft		}
1024f74bd194SAndy Whitcroft		# Preprocessor commands end at the newline unless escaped.
1025f74bd194SAndy Whitcroft		if ($type eq '#' && $c eq "\n" && $p ne "\\") {
1026f74bd194SAndy Whitcroft			$level--;
1027f74bd194SAndy Whitcroft			$type = '';
1028f74bd194SAndy Whitcroft			$off++;
1029f74bd194SAndy Whitcroft			last;
1030f74bd194SAndy Whitcroft		}
10318905a67cSAndy Whitcroft		$off++;
10328905a67cSAndy Whitcroft	}
1033a3bb97a7SAndy Whitcroft	# We are truly at the end, so shuffle to the next line.
103413214adfSAndy Whitcroft	if ($off == $len) {
1035a3bb97a7SAndy Whitcroft		$loff = $len + 1;
103613214adfSAndy Whitcroft		$line++;
103713214adfSAndy Whitcroft		$remain--;
103813214adfSAndy Whitcroft	}
10398905a67cSAndy Whitcroft
10408905a67cSAndy Whitcroft	my $statement = substr($blk, $soff, $off - $soff + 1);
10418905a67cSAndy Whitcroft	my $condition = substr($blk, $soff, $coff - $soff + 1);
10428905a67cSAndy Whitcroft
10438905a67cSAndy Whitcroft	#warn "STATEMENT<$statement>\n";
10448905a67cSAndy Whitcroft	#warn "CONDITION<$condition>\n";
10458905a67cSAndy Whitcroft
1046773647a0SAndy Whitcroft	#print "coff<$coff> soff<$off> loff<$loff>\n";
104713214adfSAndy Whitcroft
104813214adfSAndy Whitcroft	return ($statement, $condition,
104913214adfSAndy Whitcroft			$line, $remain + 1, $off - $loff + 1, $level);
105013214adfSAndy Whitcroft}
105113214adfSAndy Whitcroft
1052cf655043SAndy Whitcroftsub statement_lines {
1053cf655043SAndy Whitcroft	my ($stmt) = @_;
1054cf655043SAndy Whitcroft
1055cf655043SAndy Whitcroft	# Strip the diff line prefixes and rip blank lines at start and end.
1056cf655043SAndy Whitcroft	$stmt =~ s/(^|\n)./$1/g;
1057cf655043SAndy Whitcroft	$stmt =~ s/^\s*//;
1058cf655043SAndy Whitcroft	$stmt =~ s/\s*$//;
1059cf655043SAndy Whitcroft
1060cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
1061cf655043SAndy Whitcroft
1062cf655043SAndy Whitcroft	return $#stmt_lines + 2;
1063cf655043SAndy Whitcroft}
1064cf655043SAndy Whitcroft
1065cf655043SAndy Whitcroftsub statement_rawlines {
1066cf655043SAndy Whitcroft	my ($stmt) = @_;
1067cf655043SAndy Whitcroft
1068cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
1069cf655043SAndy Whitcroft
1070cf655043SAndy Whitcroft	return $#stmt_lines + 2;
1071cf655043SAndy Whitcroft}
1072cf655043SAndy Whitcroft
1073cf655043SAndy Whitcroftsub statement_block_size {
1074cf655043SAndy Whitcroft	my ($stmt) = @_;
1075cf655043SAndy Whitcroft
1076cf655043SAndy Whitcroft	$stmt =~ s/(^|\n)./$1/g;
1077cf655043SAndy Whitcroft	$stmt =~ s/^\s*{//;
1078cf655043SAndy Whitcroft	$stmt =~ s/}\s*$//;
1079cf655043SAndy Whitcroft	$stmt =~ s/^\s*//;
1080cf655043SAndy Whitcroft	$stmt =~ s/\s*$//;
1081cf655043SAndy Whitcroft
1082cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
1083cf655043SAndy Whitcroft	my @stmt_statements = ($stmt =~ /;/g);
1084cf655043SAndy Whitcroft
1085cf655043SAndy Whitcroft	my $stmt_lines = $#stmt_lines + 2;
1086cf655043SAndy Whitcroft	my $stmt_statements = $#stmt_statements + 1;
1087cf655043SAndy Whitcroft
1088cf655043SAndy Whitcroft	if ($stmt_lines > $stmt_statements) {
1089cf655043SAndy Whitcroft		return $stmt_lines;
1090cf655043SAndy Whitcroft	} else {
1091cf655043SAndy Whitcroft		return $stmt_statements;
1092cf655043SAndy Whitcroft	}
1093cf655043SAndy Whitcroft}
1094cf655043SAndy Whitcroft
109513214adfSAndy Whitcroftsub ctx_statement_full {
109613214adfSAndy Whitcroft	my ($linenr, $remain, $off) = @_;
109713214adfSAndy Whitcroft	my ($statement, $condition, $level);
109813214adfSAndy Whitcroft
109913214adfSAndy Whitcroft	my (@chunks);
110013214adfSAndy Whitcroft
1101cf655043SAndy Whitcroft	# Grab the first conditional/block pair.
110213214adfSAndy Whitcroft	($statement, $condition, $linenr, $remain, $off, $level) =
110313214adfSAndy Whitcroft				ctx_statement_block($linenr, $remain, $off);
1104773647a0SAndy Whitcroft	#print "F: c<$condition> s<$statement> remain<$remain>\n";
110513214adfSAndy Whitcroft	push(@chunks, [ $condition, $statement ]);
1106cf655043SAndy Whitcroft	if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
1107cf655043SAndy Whitcroft		return ($level, $linenr, @chunks);
1108cf655043SAndy Whitcroft	}
1109cf655043SAndy Whitcroft
1110cf655043SAndy Whitcroft	# Pull in the following conditional/block pairs and see if they
1111cf655043SAndy Whitcroft	# could continue the statement.
1112cf655043SAndy Whitcroft	for (;;) {
111313214adfSAndy Whitcroft		($statement, $condition, $linenr, $remain, $off, $level) =
111413214adfSAndy Whitcroft				ctx_statement_block($linenr, $remain, $off);
1115cf655043SAndy Whitcroft		#print "C: c<$condition> s<$statement> remain<$remain>\n";
1116773647a0SAndy Whitcroft		last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
1117cf655043SAndy Whitcroft		#print "C: push\n";
1118cf655043SAndy Whitcroft		push(@chunks, [ $condition, $statement ]);
111913214adfSAndy Whitcroft	}
112013214adfSAndy Whitcroft
112113214adfSAndy Whitcroft	return ($level, $linenr, @chunks);
11228905a67cSAndy Whitcroft}
11238905a67cSAndy Whitcroft
11244a0df2efSAndy Whitcroftsub ctx_block_get {
1125f0a594c1SAndy Whitcroft	my ($linenr, $remain, $outer, $open, $close, $off) = @_;
11264a0df2efSAndy Whitcroft	my $line;
11274a0df2efSAndy Whitcroft	my $start = $linenr - 1;
11284a0df2efSAndy Whitcroft	my $blk = '';
11294a0df2efSAndy Whitcroft	my @o;
11304a0df2efSAndy Whitcroft	my @c;
11314a0df2efSAndy Whitcroft	my @res = ();
11324a0df2efSAndy Whitcroft
1133f0a594c1SAndy Whitcroft	my $level = 0;
11344635f4fbSAndy Whitcroft	my @stack = ($level);
113500df344fSAndy Whitcroft	for ($line = $start; $remain > 0; $line++) {
113600df344fSAndy Whitcroft		next if ($rawlines[$line] =~ /^-/);
113700df344fSAndy Whitcroft		$remain--;
113800df344fSAndy Whitcroft
113900df344fSAndy Whitcroft		$blk .= $rawlines[$line];
11404635f4fbSAndy Whitcroft
11414635f4fbSAndy Whitcroft		# Handle nested #if/#else.
114201464f30SAndy Whitcroft		if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
11434635f4fbSAndy Whitcroft			push(@stack, $level);
114401464f30SAndy Whitcroft		} elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
11454635f4fbSAndy Whitcroft			$level = $stack[$#stack - 1];
114601464f30SAndy Whitcroft		} elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
11474635f4fbSAndy Whitcroft			$level = pop(@stack);
11484635f4fbSAndy Whitcroft		}
11494635f4fbSAndy Whitcroft
115001464f30SAndy Whitcroft		foreach my $c (split(//, $lines[$line])) {
1151f0a594c1SAndy Whitcroft			##print "C<$c>L<$level><$open$close>O<$off>\n";
1152f0a594c1SAndy Whitcroft			if ($off > 0) {
1153f0a594c1SAndy Whitcroft				$off--;
1154f0a594c1SAndy Whitcroft				next;
1155f0a594c1SAndy Whitcroft			}
11564a0df2efSAndy Whitcroft
1157f0a594c1SAndy Whitcroft			if ($c eq $close && $level > 0) {
1158f0a594c1SAndy Whitcroft				$level--;
1159f0a594c1SAndy Whitcroft				last if ($level == 0);
1160f0a594c1SAndy Whitcroft			} elsif ($c eq $open) {
1161f0a594c1SAndy Whitcroft				$level++;
1162f0a594c1SAndy Whitcroft			}
1163f0a594c1SAndy Whitcroft		}
11644a0df2efSAndy Whitcroft
1165f0a594c1SAndy Whitcroft		if (!$outer || $level <= 1) {
116600df344fSAndy Whitcroft			push(@res, $rawlines[$line]);
11674a0df2efSAndy Whitcroft		}
11684a0df2efSAndy Whitcroft
1169f0a594c1SAndy Whitcroft		last if ($level == 0);
11704a0df2efSAndy Whitcroft	}
11714a0df2efSAndy Whitcroft
1172f0a594c1SAndy Whitcroft	return ($level, @res);
11734a0df2efSAndy Whitcroft}
11744a0df2efSAndy Whitcroftsub ctx_block_outer {
11754a0df2efSAndy Whitcroft	my ($linenr, $remain) = @_;
11764a0df2efSAndy Whitcroft
1177f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
1178f0a594c1SAndy Whitcroft	return @r;
11794a0df2efSAndy Whitcroft}
11804a0df2efSAndy Whitcroftsub ctx_block {
11814a0df2efSAndy Whitcroft	my ($linenr, $remain) = @_;
11824a0df2efSAndy Whitcroft
1183f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1184f0a594c1SAndy Whitcroft	return @r;
1185653d4876SAndy Whitcroft}
1186653d4876SAndy Whitcroftsub ctx_statement {
1187f0a594c1SAndy Whitcroft	my ($linenr, $remain, $off) = @_;
1188f0a594c1SAndy Whitcroft
1189f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1190f0a594c1SAndy Whitcroft	return @r;
1191f0a594c1SAndy Whitcroft}
1192f0a594c1SAndy Whitcroftsub ctx_block_level {
1193653d4876SAndy Whitcroft	my ($linenr, $remain) = @_;
1194653d4876SAndy Whitcroft
1195f0a594c1SAndy Whitcroft	return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
11964a0df2efSAndy Whitcroft}
11979c0ca6f9SAndy Whitcroftsub ctx_statement_level {
11989c0ca6f9SAndy Whitcroft	my ($linenr, $remain, $off) = @_;
11999c0ca6f9SAndy Whitcroft
12009c0ca6f9SAndy Whitcroft	return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
12019c0ca6f9SAndy Whitcroft}
12024a0df2efSAndy Whitcroft
12034a0df2efSAndy Whitcroftsub ctx_locate_comment {
12044a0df2efSAndy Whitcroft	my ($first_line, $end_line) = @_;
12054a0df2efSAndy Whitcroft
12064a0df2efSAndy Whitcroft	# Catch a comment on the end of the line itself.
1207beae6332SAndy Whitcroft	my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
12084a0df2efSAndy Whitcroft	return $current_comment if (defined $current_comment);
12094a0df2efSAndy Whitcroft
12104a0df2efSAndy Whitcroft	# Look through the context and try and figure out if there is a
12114a0df2efSAndy Whitcroft	# comment.
12124a0df2efSAndy Whitcroft	my $in_comment = 0;
12134a0df2efSAndy Whitcroft	$current_comment = '';
12144a0df2efSAndy Whitcroft	for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
121500df344fSAndy Whitcroft		my $line = $rawlines[$linenr - 1];
121600df344fSAndy Whitcroft		#warn "           $line\n";
12174a0df2efSAndy Whitcroft		if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
12184a0df2efSAndy Whitcroft			$in_comment = 1;
12194a0df2efSAndy Whitcroft		}
12204a0df2efSAndy Whitcroft		if ($line =~ m@/\*@) {
12214a0df2efSAndy Whitcroft			$in_comment = 1;
12224a0df2efSAndy Whitcroft		}
12234a0df2efSAndy Whitcroft		if (!$in_comment && $current_comment ne '') {
12244a0df2efSAndy Whitcroft			$current_comment = '';
12254a0df2efSAndy Whitcroft		}
12264a0df2efSAndy Whitcroft		$current_comment .= $line . "\n" if ($in_comment);
12274a0df2efSAndy Whitcroft		if ($line =~ m@\*/@) {
12284a0df2efSAndy Whitcroft			$in_comment = 0;
12294a0df2efSAndy Whitcroft		}
12304a0df2efSAndy Whitcroft	}
12314a0df2efSAndy Whitcroft
12324a0df2efSAndy Whitcroft	chomp($current_comment);
12334a0df2efSAndy Whitcroft	return($current_comment);
12344a0df2efSAndy Whitcroft}
12354a0df2efSAndy Whitcroftsub ctx_has_comment {
12364a0df2efSAndy Whitcroft	my ($first_line, $end_line) = @_;
12374a0df2efSAndy Whitcroft	my $cmt = ctx_locate_comment($first_line, $end_line);
12384a0df2efSAndy Whitcroft
123900df344fSAndy Whitcroft	##print "LINE: $rawlines[$end_line - 1 ]\n";
12404a0df2efSAndy Whitcroft	##print "CMMT: $cmt\n";
12414a0df2efSAndy Whitcroft
12424a0df2efSAndy Whitcroft	return ($cmt ne '');
12434a0df2efSAndy Whitcroft}
12444a0df2efSAndy Whitcroft
12454d001e4dSAndy Whitcroftsub raw_line {
12464d001e4dSAndy Whitcroft	my ($linenr, $cnt) = @_;
12474d001e4dSAndy Whitcroft
12484d001e4dSAndy Whitcroft	my $offset = $linenr - 1;
12494d001e4dSAndy Whitcroft	$cnt++;
12504d001e4dSAndy Whitcroft
12514d001e4dSAndy Whitcroft	my $line;
12524d001e4dSAndy Whitcroft	while ($cnt) {
12534d001e4dSAndy Whitcroft		$line = $rawlines[$offset++];
12544d001e4dSAndy Whitcroft		next if (defined($line) && $line =~ /^-/);
12554d001e4dSAndy Whitcroft		$cnt--;
12564d001e4dSAndy Whitcroft	}
12574d001e4dSAndy Whitcroft
12584d001e4dSAndy Whitcroft	return $line;
12594d001e4dSAndy Whitcroft}
12604d001e4dSAndy Whitcroft
12610a920b5bSAndy Whitcroftsub cat_vet {
12620a920b5bSAndy Whitcroft	my ($vet) = @_;
12639c0ca6f9SAndy Whitcroft	my ($res, $coded);
12640a920b5bSAndy Whitcroft
12659c0ca6f9SAndy Whitcroft	$res = '';
12666c72ffaaSAndy Whitcroft	while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
12676c72ffaaSAndy Whitcroft		$res .= $1;
12686c72ffaaSAndy Whitcroft		if ($2 ne '') {
12699c0ca6f9SAndy Whitcroft			$coded = sprintf("^%c", unpack('C', $2) + 64);
12706c72ffaaSAndy Whitcroft			$res .= $coded;
12716c72ffaaSAndy Whitcroft		}
12729c0ca6f9SAndy Whitcroft	}
12739c0ca6f9SAndy Whitcroft	$res =~ s/$/\$/;
12740a920b5bSAndy Whitcroft
12759c0ca6f9SAndy Whitcroft	return $res;
12760a920b5bSAndy Whitcroft}
12770a920b5bSAndy Whitcroft
1278c2fdda0dSAndy Whitcroftmy $av_preprocessor = 0;
1279cf655043SAndy Whitcroftmy $av_pending;
1280c2fdda0dSAndy Whitcroftmy @av_paren_type;
12811f65f947SAndy Whitcroftmy $av_pend_colon;
1282c2fdda0dSAndy Whitcroft
1283c2fdda0dSAndy Whitcroftsub annotate_reset {
1284c2fdda0dSAndy Whitcroft	$av_preprocessor = 0;
1285cf655043SAndy Whitcroft	$av_pending = '_';
1286cf655043SAndy Whitcroft	@av_paren_type = ('E');
12871f65f947SAndy Whitcroft	$av_pend_colon = 'O';
1288c2fdda0dSAndy Whitcroft}
1289c2fdda0dSAndy Whitcroft
12906c72ffaaSAndy Whitcroftsub annotate_values {
12916c72ffaaSAndy Whitcroft	my ($stream, $type) = @_;
12926c72ffaaSAndy Whitcroft
12936c72ffaaSAndy Whitcroft	my $res;
12941f65f947SAndy Whitcroft	my $var = '_' x length($stream);
12956c72ffaaSAndy Whitcroft	my $cur = $stream;
12966c72ffaaSAndy Whitcroft
1297c2fdda0dSAndy Whitcroft	print "$stream\n" if ($dbg_values > 1);
12986c72ffaaSAndy Whitcroft
12996c72ffaaSAndy Whitcroft	while (length($cur)) {
1300773647a0SAndy Whitcroft		@av_paren_type = ('E') if ($#av_paren_type < 0);
1301cf655043SAndy Whitcroft		print " <" . join('', @av_paren_type) .
1302171ae1a4SAndy Whitcroft				"> <$type> <$av_pending>" if ($dbg_values > 1);
13036c72ffaaSAndy Whitcroft		if ($cur =~ /^(\s+)/o) {
1304c2fdda0dSAndy Whitcroft			print "WS($1)\n" if ($dbg_values > 1);
1305c2fdda0dSAndy Whitcroft			if ($1 =~ /\n/ && $av_preprocessor) {
1306cf655043SAndy Whitcroft				$type = pop(@av_paren_type);
1307c2fdda0dSAndy Whitcroft				$av_preprocessor = 0;
13086c72ffaaSAndy Whitcroft			}
13096c72ffaaSAndy Whitcroft
1310c023e473SFlorian Mickler		} elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
13119446ef56SAndy Whitcroft			print "CAST($1)\n" if ($dbg_values > 1);
13129446ef56SAndy Whitcroft			push(@av_paren_type, $type);
1313addcdceaSAndy Whitcroft			$type = 'c';
13149446ef56SAndy Whitcroft
1315e91b6e26SAndy Whitcroft		} elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
1316c2fdda0dSAndy Whitcroft			print "DECLARE($1)\n" if ($dbg_values > 1);
13176c72ffaaSAndy Whitcroft			$type = 'T';
13186c72ffaaSAndy Whitcroft
1319389a2fe5SAndy Whitcroft		} elsif ($cur =~ /^($Modifier)\s*/) {
1320389a2fe5SAndy Whitcroft			print "MODIFIER($1)\n" if ($dbg_values > 1);
1321389a2fe5SAndy Whitcroft			$type = 'T';
1322389a2fe5SAndy Whitcroft
1323c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
1324171ae1a4SAndy Whitcroft			print "DEFINE($1,$2)\n" if ($dbg_values > 1);
1325c2fdda0dSAndy Whitcroft			$av_preprocessor = 1;
1326171ae1a4SAndy Whitcroft			push(@av_paren_type, $type);
1327171ae1a4SAndy Whitcroft			if ($2 ne '') {
1328cf655043SAndy Whitcroft				$av_pending = 'N';
1329171ae1a4SAndy Whitcroft			}
1330171ae1a4SAndy Whitcroft			$type = 'E';
1331171ae1a4SAndy Whitcroft
1332c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
1333171ae1a4SAndy Whitcroft			print "UNDEF($1)\n" if ($dbg_values > 1);
1334171ae1a4SAndy Whitcroft			$av_preprocessor = 1;
1335171ae1a4SAndy Whitcroft			push(@av_paren_type, $type);
13366c72ffaaSAndy Whitcroft
1337c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
1338cf655043SAndy Whitcroft			print "PRE_START($1)\n" if ($dbg_values > 1);
1339c2fdda0dSAndy Whitcroft			$av_preprocessor = 1;
1340cf655043SAndy Whitcroft
1341cf655043SAndy Whitcroft			push(@av_paren_type, $type);
1342cf655043SAndy Whitcroft			push(@av_paren_type, $type);
1343171ae1a4SAndy Whitcroft			$type = 'E';
1344cf655043SAndy Whitcroft
1345c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
1346cf655043SAndy Whitcroft			print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1347cf655043SAndy Whitcroft			$av_preprocessor = 1;
1348cf655043SAndy Whitcroft
1349cf655043SAndy Whitcroft			push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1350cf655043SAndy Whitcroft
1351171ae1a4SAndy Whitcroft			$type = 'E';
1352cf655043SAndy Whitcroft
1353c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:endif))/o) {
1354cf655043SAndy Whitcroft			print "PRE_END($1)\n" if ($dbg_values > 1);
1355cf655043SAndy Whitcroft
1356cf655043SAndy Whitcroft			$av_preprocessor = 1;
1357cf655043SAndy Whitcroft
1358cf655043SAndy Whitcroft			# Assume all arms of the conditional end as this
1359cf655043SAndy Whitcroft			# one does, and continue as if the #endif was not here.
1360cf655043SAndy Whitcroft			pop(@av_paren_type);
1361cf655043SAndy Whitcroft			push(@av_paren_type, $type);
1362171ae1a4SAndy Whitcroft			$type = 'E';
13636c72ffaaSAndy Whitcroft
13646c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\\\n)/o) {
1365c2fdda0dSAndy Whitcroft			print "PRECONT($1)\n" if ($dbg_values > 1);
13666c72ffaaSAndy Whitcroft
1367171ae1a4SAndy Whitcroft		} elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1368171ae1a4SAndy Whitcroft			print "ATTR($1)\n" if ($dbg_values > 1);
1369171ae1a4SAndy Whitcroft			$av_pending = $type;
1370171ae1a4SAndy Whitcroft			$type = 'N';
1371171ae1a4SAndy Whitcroft
13726c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
1373c2fdda0dSAndy Whitcroft			print "SIZEOF($1)\n" if ($dbg_values > 1);
13746c72ffaaSAndy Whitcroft			if (defined $2) {
1375cf655043SAndy Whitcroft				$av_pending = 'V';
13766c72ffaaSAndy Whitcroft			}
13776c72ffaaSAndy Whitcroft			$type = 'N';
13786c72ffaaSAndy Whitcroft
137914b111c1SAndy Whitcroft		} elsif ($cur =~ /^(if|while|for)\b/o) {
1380c2fdda0dSAndy Whitcroft			print "COND($1)\n" if ($dbg_values > 1);
138114b111c1SAndy Whitcroft			$av_pending = 'E';
13826c72ffaaSAndy Whitcroft			$type = 'N';
13836c72ffaaSAndy Whitcroft
13841f65f947SAndy Whitcroft		} elsif ($cur =~/^(case)/o) {
13851f65f947SAndy Whitcroft			print "CASE($1)\n" if ($dbg_values > 1);
13861f65f947SAndy Whitcroft			$av_pend_colon = 'C';
13871f65f947SAndy Whitcroft			$type = 'N';
13881f65f947SAndy Whitcroft
138914b111c1SAndy Whitcroft		} elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
1390c2fdda0dSAndy Whitcroft			print "KEYWORD($1)\n" if ($dbg_values > 1);
13916c72ffaaSAndy Whitcroft			$type = 'N';
13926c72ffaaSAndy Whitcroft
13936c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\()/o) {
1394c2fdda0dSAndy Whitcroft			print "PAREN('$1')\n" if ($dbg_values > 1);
1395cf655043SAndy Whitcroft			push(@av_paren_type, $av_pending);
1396cf655043SAndy Whitcroft			$av_pending = '_';
13976c72ffaaSAndy Whitcroft			$type = 'N';
13986c72ffaaSAndy Whitcroft
13996c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\))/o) {
1400cf655043SAndy Whitcroft			my $new_type = pop(@av_paren_type);
1401cf655043SAndy Whitcroft			if ($new_type ne '_') {
1402cf655043SAndy Whitcroft				$type = $new_type;
1403c2fdda0dSAndy Whitcroft				print "PAREN('$1') -> $type\n"
1404c2fdda0dSAndy Whitcroft							if ($dbg_values > 1);
14056c72ffaaSAndy Whitcroft			} else {
1406c2fdda0dSAndy Whitcroft				print "PAREN('$1')\n" if ($dbg_values > 1);
14076c72ffaaSAndy Whitcroft			}
14086c72ffaaSAndy Whitcroft
1409c8cb2ca3SAndy Whitcroft		} elsif ($cur =~ /^($Ident)\s*\(/o) {
1410c2fdda0dSAndy Whitcroft			print "FUNC($1)\n" if ($dbg_values > 1);
1411c8cb2ca3SAndy Whitcroft			$type = 'V';
1412cf655043SAndy Whitcroft			$av_pending = 'V';
14136c72ffaaSAndy Whitcroft
14148e761b04SAndy Whitcroft		} elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
14158e761b04SAndy Whitcroft			if (defined $2 && $type eq 'C' || $type eq 'T') {
14161f65f947SAndy Whitcroft				$av_pend_colon = 'B';
14178e761b04SAndy Whitcroft			} elsif ($type eq 'E') {
14188e761b04SAndy Whitcroft				$av_pend_colon = 'L';
14191f65f947SAndy Whitcroft			}
14201f65f947SAndy Whitcroft			print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
14211f65f947SAndy Whitcroft			$type = 'V';
14221f65f947SAndy Whitcroft
14236c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Ident|$Constant)/o) {
1424c2fdda0dSAndy Whitcroft			print "IDENT($1)\n" if ($dbg_values > 1);
14256c72ffaaSAndy Whitcroft			$type = 'V';
14266c72ffaaSAndy Whitcroft
14276c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Assignment)/o) {
1428c2fdda0dSAndy Whitcroft			print "ASSIGN($1)\n" if ($dbg_values > 1);
14296c72ffaaSAndy Whitcroft			$type = 'N';
14306c72ffaaSAndy Whitcroft
1431cf655043SAndy Whitcroft		} elsif ($cur =~/^(;|{|})/) {
1432c2fdda0dSAndy Whitcroft			print "END($1)\n" if ($dbg_values > 1);
143313214adfSAndy Whitcroft			$type = 'E';
14341f65f947SAndy Whitcroft			$av_pend_colon = 'O';
143513214adfSAndy Whitcroft
14368e761b04SAndy Whitcroft		} elsif ($cur =~/^(,)/) {
14378e761b04SAndy Whitcroft			print "COMMA($1)\n" if ($dbg_values > 1);
14388e761b04SAndy Whitcroft			$type = 'C';
14398e761b04SAndy Whitcroft
14401f65f947SAndy Whitcroft		} elsif ($cur =~ /^(\?)/o) {
14411f65f947SAndy Whitcroft			print "QUESTION($1)\n" if ($dbg_values > 1);
14421f65f947SAndy Whitcroft			$type = 'N';
14431f65f947SAndy Whitcroft
14441f65f947SAndy Whitcroft		} elsif ($cur =~ /^(:)/o) {
14451f65f947SAndy Whitcroft			print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
14461f65f947SAndy Whitcroft
14471f65f947SAndy Whitcroft			substr($var, length($res), 1, $av_pend_colon);
14481f65f947SAndy Whitcroft			if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
14491f65f947SAndy Whitcroft				$type = 'E';
14501f65f947SAndy Whitcroft			} else {
14511f65f947SAndy Whitcroft				$type = 'N';
14521f65f947SAndy Whitcroft			}
14531f65f947SAndy Whitcroft			$av_pend_colon = 'O';
14541f65f947SAndy Whitcroft
14558e761b04SAndy Whitcroft		} elsif ($cur =~ /^(\[)/o) {
145613214adfSAndy Whitcroft			print "CLOSE($1)\n" if ($dbg_values > 1);
14576c72ffaaSAndy Whitcroft			$type = 'N';
14586c72ffaaSAndy Whitcroft
14590d413866SAndy Whitcroft		} elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
146074048ed8SAndy Whitcroft			my $variant;
146174048ed8SAndy Whitcroft
146274048ed8SAndy Whitcroft			print "OPV($1)\n" if ($dbg_values > 1);
146374048ed8SAndy Whitcroft			if ($type eq 'V') {
146474048ed8SAndy Whitcroft				$variant = 'B';
146574048ed8SAndy Whitcroft			} else {
146674048ed8SAndy Whitcroft				$variant = 'U';
146774048ed8SAndy Whitcroft			}
146874048ed8SAndy Whitcroft
146974048ed8SAndy Whitcroft			substr($var, length($res), 1, $variant);
147074048ed8SAndy Whitcroft			$type = 'N';
147174048ed8SAndy Whitcroft
14726c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Operators)/o) {
1473c2fdda0dSAndy Whitcroft			print "OP($1)\n" if ($dbg_values > 1);
14746c72ffaaSAndy Whitcroft			if ($1 ne '++' && $1 ne '--') {
14756c72ffaaSAndy Whitcroft				$type = 'N';
14766c72ffaaSAndy Whitcroft			}
14776c72ffaaSAndy Whitcroft
14786c72ffaaSAndy Whitcroft		} elsif ($cur =~ /(^.)/o) {
1479c2fdda0dSAndy Whitcroft			print "C($1)\n" if ($dbg_values > 1);
14806c72ffaaSAndy Whitcroft		}
14816c72ffaaSAndy Whitcroft		if (defined $1) {
14826c72ffaaSAndy Whitcroft			$cur = substr($cur, length($1));
14836c72ffaaSAndy Whitcroft			$res .= $type x length($1);
14846c72ffaaSAndy Whitcroft		}
14856c72ffaaSAndy Whitcroft	}
14866c72ffaaSAndy Whitcroft
14871f65f947SAndy Whitcroft	return ($res, $var);
14886c72ffaaSAndy Whitcroft}
14896c72ffaaSAndy Whitcroft
14908905a67cSAndy Whitcroftsub possible {
149113214adfSAndy Whitcroft	my ($possible, $line) = @_;
14929a974fdbSAndy Whitcroft	my $notPermitted = qr{(?:
14930776e594SAndy Whitcroft		^(?:
14940776e594SAndy Whitcroft			$Modifier|
14950776e594SAndy Whitcroft			$Storage|
14960776e594SAndy Whitcroft			$Type|
14979a974fdbSAndy Whitcroft			DEFINE_\S+
14989a974fdbSAndy Whitcroft		)$|
14999a974fdbSAndy Whitcroft		^(?:
15000776e594SAndy Whitcroft			goto|
15010776e594SAndy Whitcroft			return|
15020776e594SAndy Whitcroft			case|
15030776e594SAndy Whitcroft			else|
15040776e594SAndy Whitcroft			asm|__asm__|
150589a88353SAndy Whitcroft			do|
150689a88353SAndy Whitcroft			\#|
150789a88353SAndy Whitcroft			\#\#|
15089a974fdbSAndy Whitcroft		)(?:\s|$)|
15090776e594SAndy Whitcroft		^(?:typedef|struct|enum)\b
15109a974fdbSAndy Whitcroft	    )}x;
15119a974fdbSAndy Whitcroft	warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
15129a974fdbSAndy Whitcroft	if ($possible !~ $notPermitted) {
1513c45dcabdSAndy Whitcroft		# Check for modifiers.
1514c45dcabdSAndy Whitcroft		$possible =~ s/\s*$Storage\s*//g;
1515c45dcabdSAndy Whitcroft		$possible =~ s/\s*$Sparse\s*//g;
1516c45dcabdSAndy Whitcroft		if ($possible =~ /^\s*$/) {
1517c45dcabdSAndy Whitcroft
1518c45dcabdSAndy Whitcroft		} elsif ($possible =~ /\s/) {
1519c45dcabdSAndy Whitcroft			$possible =~ s/\s*$Type\s*//g;
1520d2506586SAndy Whitcroft			for my $modifier (split(' ', $possible)) {
15219a974fdbSAndy Whitcroft				if ($modifier !~ $notPermitted) {
1522d2506586SAndy Whitcroft					warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1523d2506586SAndy Whitcroft					push(@modifierList, $modifier);
1524d2506586SAndy Whitcroft				}
15259a974fdbSAndy Whitcroft			}
1526c45dcabdSAndy Whitcroft
1527c45dcabdSAndy Whitcroft		} else {
152813214adfSAndy Whitcroft			warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
15298905a67cSAndy Whitcroft			push(@typeList, $possible);
1530c45dcabdSAndy Whitcroft		}
15318905a67cSAndy Whitcroft		build_types();
15320776e594SAndy Whitcroft	} else {
15330776e594SAndy Whitcroft		warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
15348905a67cSAndy Whitcroft	}
15358905a67cSAndy Whitcroft}
15368905a67cSAndy Whitcroft
15376c72ffaaSAndy Whitcroftmy $prefix = '';
15386c72ffaaSAndy Whitcroft
1539000d1cc1SJoe Perchessub show_type {
1540cbec18afSJoe Perches	my ($type) = @_;
154191bfe484SJoe Perches
1542cbec18afSJoe Perches	return defined $use_type{$type} if (scalar keys %use_type > 0);
1543cbec18afSJoe Perches
1544cbec18afSJoe Perches	return !defined $ignore_type{$type};
1545000d1cc1SJoe Perches}
1546000d1cc1SJoe Perches
1547f0a594c1SAndy Whitcroftsub report {
1548cbec18afSJoe Perches	my ($level, $type, $msg) = @_;
1549cbec18afSJoe Perches
1550cbec18afSJoe Perches	if (!show_type($type) ||
1551cbec18afSJoe Perches	    (defined $tst_only && $msg !~ /\Q$tst_only\E/)) {
1552773647a0SAndy Whitcroft		return 0;
1553773647a0SAndy Whitcroft	}
1554000d1cc1SJoe Perches	my $line;
1555000d1cc1SJoe Perches	if ($show_types) {
1556cbec18afSJoe Perches		$line = "$prefix$level:$type: $msg\n";
1557000d1cc1SJoe Perches	} else {
1558cbec18afSJoe Perches		$line = "$prefix$level: $msg\n";
1559000d1cc1SJoe Perches	}
15608905a67cSAndy Whitcroft	$line = (split('\n', $line))[0] . "\n" if ($terse);
15618905a67cSAndy Whitcroft
156213214adfSAndy Whitcroft	push(our @report, $line);
1563773647a0SAndy Whitcroft
1564773647a0SAndy Whitcroft	return 1;
1565f0a594c1SAndy Whitcroft}
1566cbec18afSJoe Perches
1567f0a594c1SAndy Whitcroftsub report_dump {
156813214adfSAndy Whitcroft	our @report;
1569f0a594c1SAndy Whitcroft}
1570000d1cc1SJoe Perches
1571d752fcc8SJoe Perchessub fixup_current_range {
1572d752fcc8SJoe Perches	my ($lineRef, $offset, $length) = @_;
1573d752fcc8SJoe Perches
1574d752fcc8SJoe Perches	if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) {
1575d752fcc8SJoe Perches		my $o = $1;
1576d752fcc8SJoe Perches		my $l = $2;
1577d752fcc8SJoe Perches		my $no = $o + $offset;
1578d752fcc8SJoe Perches		my $nl = $l + $length;
1579d752fcc8SJoe Perches		$$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/;
1580d752fcc8SJoe Perches	}
1581d752fcc8SJoe Perches}
1582d752fcc8SJoe Perches
1583d752fcc8SJoe Perchessub fix_inserted_deleted_lines {
1584d752fcc8SJoe Perches	my ($linesRef, $insertedRef, $deletedRef) = @_;
1585d752fcc8SJoe Perches
1586d752fcc8SJoe Perches	my $range_last_linenr = 0;
1587d752fcc8SJoe Perches	my $delta_offset = 0;
1588d752fcc8SJoe Perches
1589d752fcc8SJoe Perches	my $old_linenr = 0;
1590d752fcc8SJoe Perches	my $new_linenr = 0;
1591d752fcc8SJoe Perches
1592d752fcc8SJoe Perches	my $next_insert = 0;
1593d752fcc8SJoe Perches	my $next_delete = 0;
1594d752fcc8SJoe Perches
1595d752fcc8SJoe Perches	my @lines = ();
1596d752fcc8SJoe Perches
1597d752fcc8SJoe Perches	my $inserted = @{$insertedRef}[$next_insert++];
1598d752fcc8SJoe Perches	my $deleted = @{$deletedRef}[$next_delete++];
1599d752fcc8SJoe Perches
1600d752fcc8SJoe Perches	foreach my $old_line (@{$linesRef}) {
1601d752fcc8SJoe Perches		my $save_line = 1;
1602d752fcc8SJoe Perches		my $line = $old_line;	#don't modify the array
1603d752fcc8SJoe Perches		if ($line =~ /^(?:\+\+\+\|\-\-\-)\s+\S+/) {	#new filename
1604d752fcc8SJoe Perches			$delta_offset = 0;
1605d752fcc8SJoe Perches		} elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) {	#new hunk
1606d752fcc8SJoe Perches			$range_last_linenr = $new_linenr;
1607d752fcc8SJoe Perches			fixup_current_range(\$line, $delta_offset, 0);
1608d752fcc8SJoe Perches		}
1609d752fcc8SJoe Perches
1610d752fcc8SJoe Perches		while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) {
1611d752fcc8SJoe Perches			$deleted = @{$deletedRef}[$next_delete++];
1612d752fcc8SJoe Perches			$save_line = 0;
1613d752fcc8SJoe Perches			fixup_current_range(\$lines[$range_last_linenr], $delta_offset--, -1);
1614d752fcc8SJoe Perches		}
1615d752fcc8SJoe Perches
1616d752fcc8SJoe Perches		while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) {
1617d752fcc8SJoe Perches			push(@lines, ${$inserted}{'LINE'});
1618d752fcc8SJoe Perches			$inserted = @{$insertedRef}[$next_insert++];
1619d752fcc8SJoe Perches			$new_linenr++;
1620d752fcc8SJoe Perches			fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1);
1621d752fcc8SJoe Perches		}
1622d752fcc8SJoe Perches
1623d752fcc8SJoe Perches		if ($save_line) {
1624d752fcc8SJoe Perches			push(@lines, $line);
1625d752fcc8SJoe Perches			$new_linenr++;
1626d752fcc8SJoe Perches		}
1627d752fcc8SJoe Perches
1628d752fcc8SJoe Perches		$old_linenr++;
1629d752fcc8SJoe Perches	}
1630d752fcc8SJoe Perches
1631d752fcc8SJoe Perches	return @lines;
1632d752fcc8SJoe Perches}
1633d752fcc8SJoe Perches
1634f2d7e4d4SJoe Perchessub fix_insert_line {
1635f2d7e4d4SJoe Perches	my ($linenr, $line) = @_;
1636f2d7e4d4SJoe Perches
1637f2d7e4d4SJoe Perches	my $inserted = {
1638f2d7e4d4SJoe Perches		LINENR => $linenr,
1639f2d7e4d4SJoe Perches		LINE => $line,
1640f2d7e4d4SJoe Perches	};
1641f2d7e4d4SJoe Perches	push(@fixed_inserted, $inserted);
1642f2d7e4d4SJoe Perches}
1643f2d7e4d4SJoe Perches
1644f2d7e4d4SJoe Perchessub fix_delete_line {
1645f2d7e4d4SJoe Perches	my ($linenr, $line) = @_;
1646f2d7e4d4SJoe Perches
1647f2d7e4d4SJoe Perches	my $deleted = {
1648f2d7e4d4SJoe Perches		LINENR => $linenr,
1649f2d7e4d4SJoe Perches		LINE => $line,
1650f2d7e4d4SJoe Perches	};
1651f2d7e4d4SJoe Perches
1652f2d7e4d4SJoe Perches	push(@fixed_deleted, $deleted);
1653f2d7e4d4SJoe Perches}
1654f2d7e4d4SJoe Perches
1655de7d4f0eSAndy Whitcroftsub ERROR {
1656cbec18afSJoe Perches	my ($type, $msg) = @_;
1657cbec18afSJoe Perches
1658cbec18afSJoe Perches	if (report("ERROR", $type, $msg)) {
1659de7d4f0eSAndy Whitcroft		our $clean = 0;
16606c72ffaaSAndy Whitcroft		our $cnt_error++;
16613705ce5bSJoe Perches		return 1;
1662de7d4f0eSAndy Whitcroft	}
16633705ce5bSJoe Perches	return 0;
1664773647a0SAndy Whitcroft}
1665de7d4f0eSAndy Whitcroftsub WARN {
1666cbec18afSJoe Perches	my ($type, $msg) = @_;
1667cbec18afSJoe Perches
1668cbec18afSJoe Perches	if (report("WARNING", $type, $msg)) {
1669de7d4f0eSAndy Whitcroft		our $clean = 0;
16706c72ffaaSAndy Whitcroft		our $cnt_warn++;
16713705ce5bSJoe Perches		return 1;
1672de7d4f0eSAndy Whitcroft	}
16733705ce5bSJoe Perches	return 0;
1674773647a0SAndy Whitcroft}
1675de7d4f0eSAndy Whitcroftsub CHK {
1676cbec18afSJoe Perches	my ($type, $msg) = @_;
1677cbec18afSJoe Perches
1678cbec18afSJoe Perches	if ($check && report("CHECK", $type, $msg)) {
1679de7d4f0eSAndy Whitcroft		our $clean = 0;
16806c72ffaaSAndy Whitcroft		our $cnt_chk++;
16813705ce5bSJoe Perches		return 1;
16826c72ffaaSAndy Whitcroft	}
16833705ce5bSJoe Perches	return 0;
1684de7d4f0eSAndy Whitcroft}
1685de7d4f0eSAndy Whitcroft
16866ecd9674SAndy Whitcroftsub check_absolute_file {
16876ecd9674SAndy Whitcroft	my ($absolute, $herecurr) = @_;
16886ecd9674SAndy Whitcroft	my $file = $absolute;
16896ecd9674SAndy Whitcroft
16906ecd9674SAndy Whitcroft	##print "absolute<$absolute>\n";
16916ecd9674SAndy Whitcroft
16926ecd9674SAndy Whitcroft	# See if any suffix of this path is a path within the tree.
16936ecd9674SAndy Whitcroft	while ($file =~ s@^[^/]*/@@) {
16946ecd9674SAndy Whitcroft		if (-f "$root/$file") {
16956ecd9674SAndy Whitcroft			##print "file<$file>\n";
16966ecd9674SAndy Whitcroft			last;
16976ecd9674SAndy Whitcroft		}
16986ecd9674SAndy Whitcroft	}
16996ecd9674SAndy Whitcroft	if (! -f _)  {
17006ecd9674SAndy Whitcroft		return 0;
17016ecd9674SAndy Whitcroft	}
17026ecd9674SAndy Whitcroft
17036ecd9674SAndy Whitcroft	# It is, so see if the prefix is acceptable.
17046ecd9674SAndy Whitcroft	my $prefix = $absolute;
17056ecd9674SAndy Whitcroft	substr($prefix, -length($file)) = '';
17066ecd9674SAndy Whitcroft
17076ecd9674SAndy Whitcroft	##print "prefix<$prefix>\n";
17086ecd9674SAndy Whitcroft	if ($prefix ne ".../") {
1709000d1cc1SJoe Perches		WARN("USE_RELATIVE_PATH",
1710000d1cc1SJoe Perches		     "use relative pathname instead of absolute in changelog text\n" . $herecurr);
17116ecd9674SAndy Whitcroft	}
17126ecd9674SAndy Whitcroft}
17136ecd9674SAndy Whitcroft
17143705ce5bSJoe Perchessub trim {
17153705ce5bSJoe Perches	my ($string) = @_;
17163705ce5bSJoe Perches
1717b34c648bSJoe Perches	$string =~ s/^\s+|\s+$//g;
1718b34c648bSJoe Perches
1719b34c648bSJoe Perches	return $string;
1720b34c648bSJoe Perches}
1721b34c648bSJoe Perches
1722b34c648bSJoe Perchessub ltrim {
1723b34c648bSJoe Perches	my ($string) = @_;
1724b34c648bSJoe Perches
1725b34c648bSJoe Perches	$string =~ s/^\s+//;
1726b34c648bSJoe Perches
1727b34c648bSJoe Perches	return $string;
1728b34c648bSJoe Perches}
1729b34c648bSJoe Perches
1730b34c648bSJoe Perchessub rtrim {
1731b34c648bSJoe Perches	my ($string) = @_;
1732b34c648bSJoe Perches
1733b34c648bSJoe Perches	$string =~ s/\s+$//;
17343705ce5bSJoe Perches
17353705ce5bSJoe Perches	return $string;
17363705ce5bSJoe Perches}
17373705ce5bSJoe Perches
173852ea8506SJoe Perchessub string_find_replace {
173952ea8506SJoe Perches	my ($string, $find, $replace) = @_;
174052ea8506SJoe Perches
174152ea8506SJoe Perches	$string =~ s/$find/$replace/g;
174252ea8506SJoe Perches
174352ea8506SJoe Perches	return $string;
174452ea8506SJoe Perches}
174552ea8506SJoe Perches
17463705ce5bSJoe Perchessub tabify {
17473705ce5bSJoe Perches	my ($leading) = @_;
17483705ce5bSJoe Perches
17493705ce5bSJoe Perches	my $source_indent = 8;
17503705ce5bSJoe Perches	my $max_spaces_before_tab = $source_indent - 1;
17513705ce5bSJoe Perches	my $spaces_to_tab = " " x $source_indent;
17523705ce5bSJoe Perches
17533705ce5bSJoe Perches	#convert leading spaces to tabs
17543705ce5bSJoe Perches	1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
17553705ce5bSJoe Perches	#Remove spaces before a tab
17563705ce5bSJoe Perches	1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
17573705ce5bSJoe Perches
17583705ce5bSJoe Perches	return "$leading";
17593705ce5bSJoe Perches}
17603705ce5bSJoe Perches
1761d1fe9c09SJoe Perchessub pos_last_openparen {
1762d1fe9c09SJoe Perches	my ($line) = @_;
1763d1fe9c09SJoe Perches
1764d1fe9c09SJoe Perches	my $pos = 0;
1765d1fe9c09SJoe Perches
1766d1fe9c09SJoe Perches	my $opens = $line =~ tr/\(/\(/;
1767d1fe9c09SJoe Perches	my $closes = $line =~ tr/\)/\)/;
1768d1fe9c09SJoe Perches
1769d1fe9c09SJoe Perches	my $last_openparen = 0;
1770d1fe9c09SJoe Perches
1771d1fe9c09SJoe Perches	if (($opens == 0) || ($closes >= $opens)) {
1772d1fe9c09SJoe Perches		return -1;
1773d1fe9c09SJoe Perches	}
1774d1fe9c09SJoe Perches
1775d1fe9c09SJoe Perches	my $len = length($line);
1776d1fe9c09SJoe Perches
1777d1fe9c09SJoe Perches	for ($pos = 0; $pos < $len; $pos++) {
1778d1fe9c09SJoe Perches		my $string = substr($line, $pos);
1779d1fe9c09SJoe Perches		if ($string =~ /^($FuncArg|$balanced_parens)/) {
1780d1fe9c09SJoe Perches			$pos += length($1) - 1;
1781d1fe9c09SJoe Perches		} elsif (substr($line, $pos, 1) eq '(') {
1782d1fe9c09SJoe Perches			$last_openparen = $pos;
1783d1fe9c09SJoe Perches		} elsif (index($string, '(') == -1) {
1784d1fe9c09SJoe Perches			last;
1785d1fe9c09SJoe Perches		}
1786d1fe9c09SJoe Perches	}
1787d1fe9c09SJoe Perches
178891cb5195SJoe Perches	return length(expand_tabs(substr($line, 0, $last_openparen))) + 1;
1789d1fe9c09SJoe Perches}
1790d1fe9c09SJoe Perches
17910a920b5bSAndy Whitcroftsub process {
17920a920b5bSAndy Whitcroft	my $filename = shift;
17930a920b5bSAndy Whitcroft
17940a920b5bSAndy Whitcroft	my $linenr=0;
17950a920b5bSAndy Whitcroft	my $prevline="";
1796c2fdda0dSAndy Whitcroft	my $prevrawline="";
17970a920b5bSAndy Whitcroft	my $stashline="";
1798c2fdda0dSAndy Whitcroft	my $stashrawline="";
17990a920b5bSAndy Whitcroft
18004a0df2efSAndy Whitcroft	my $length;
18010a920b5bSAndy Whitcroft	my $indent;
18020a920b5bSAndy Whitcroft	my $previndent=0;
18030a920b5bSAndy Whitcroft	my $stashindent=0;
18040a920b5bSAndy Whitcroft
1805de7d4f0eSAndy Whitcroft	our $clean = 1;
18060a920b5bSAndy Whitcroft	my $signoff = 0;
18070a920b5bSAndy Whitcroft	my $is_patch = 0;
18080a920b5bSAndy Whitcroft
180929ee1b0cSJoe Perches	my $in_header_lines = $file ? 0 : 1;
181015662b3eSJoe Perches	my $in_commit_log = 0;		#Scanning lines before patch
181113f1937eSJoe Perches	my $reported_maintainer_file = 0;
1812fa64205dSPasi Savanainen	my $non_utf8_charset = 0;
1813fa64205dSPasi Savanainen
1814365dd4eaSJoe Perches	my $last_blank_line = 0;
1815365dd4eaSJoe Perches
181613214adfSAndy Whitcroft	our @report = ();
18176c72ffaaSAndy Whitcroft	our $cnt_lines = 0;
18186c72ffaaSAndy Whitcroft	our $cnt_error = 0;
18196c72ffaaSAndy Whitcroft	our $cnt_warn = 0;
18206c72ffaaSAndy Whitcroft	our $cnt_chk = 0;
18216c72ffaaSAndy Whitcroft
18220a920b5bSAndy Whitcroft	# Trace the real file/line as we go.
18230a920b5bSAndy Whitcroft	my $realfile = '';
18240a920b5bSAndy Whitcroft	my $realline = 0;
18250a920b5bSAndy Whitcroft	my $realcnt = 0;
18260a920b5bSAndy Whitcroft	my $here = '';
18270a920b5bSAndy Whitcroft	my $in_comment = 0;
1828c2fdda0dSAndy Whitcroft	my $comment_edge = 0;
18290a920b5bSAndy Whitcroft	my $first_line = 0;
18301e855726SWolfram Sang	my $p1_prefix = '';
18310a920b5bSAndy Whitcroft
183213214adfSAndy Whitcroft	my $prev_values = 'E';
183313214adfSAndy Whitcroft
183413214adfSAndy Whitcroft	# suppression flags
1835773647a0SAndy Whitcroft	my %suppress_ifbraces;
1836170d3a22SAndy Whitcroft	my %suppress_whiletrailers;
18372b474a1aSAndy Whitcroft	my %suppress_export;
18383e469cdcSAndy Whitcroft	my $suppress_statement = 0;
1839653d4876SAndy Whitcroft
18407e51f197SJoe Perches	my %signatures = ();
1841323c1260SJoe Perches
1842c2fdda0dSAndy Whitcroft	# Pre-scan the patch sanitizing the lines.
1843de7d4f0eSAndy Whitcroft	# Pre-scan the patch looking for any __setup documentation.
1844c2fdda0dSAndy Whitcroft	#
1845de7d4f0eSAndy Whitcroft	my @setup_docs = ();
1846de7d4f0eSAndy Whitcroft	my $setup_docs = 0;
1847773647a0SAndy Whitcroft
1848d8b07710SJoe Perches	my $camelcase_file_seeded = 0;
1849d8b07710SJoe Perches
1850773647a0SAndy Whitcroft	sanitise_line_reset();
1851c2fdda0dSAndy Whitcroft	my $line;
1852c2fdda0dSAndy Whitcroft	foreach my $rawline (@rawlines) {
1853773647a0SAndy Whitcroft		$linenr++;
1854773647a0SAndy Whitcroft		$line = $rawline;
1855c2fdda0dSAndy Whitcroft
18563705ce5bSJoe Perches		push(@fixed, $rawline) if ($fix);
18573705ce5bSJoe Perches
1858773647a0SAndy Whitcroft		if ($rawline=~/^\+\+\+\s+(\S+)/) {
1859de7d4f0eSAndy Whitcroft			$setup_docs = 0;
1860de7d4f0eSAndy Whitcroft			if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
1861de7d4f0eSAndy Whitcroft				$setup_docs = 1;
1862de7d4f0eSAndy Whitcroft			}
1863773647a0SAndy Whitcroft			#next;
1864de7d4f0eSAndy Whitcroft		}
1865773647a0SAndy Whitcroft		if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1866773647a0SAndy Whitcroft			$realline=$1-1;
1867773647a0SAndy Whitcroft			if (defined $2) {
1868773647a0SAndy Whitcroft				$realcnt=$3+1;
1869773647a0SAndy Whitcroft			} else {
1870773647a0SAndy Whitcroft				$realcnt=1+1;
1871773647a0SAndy Whitcroft			}
1872c45dcabdSAndy Whitcroft			$in_comment = 0;
1873773647a0SAndy Whitcroft
1874773647a0SAndy Whitcroft			# Guestimate if this is a continuing comment.  Run
1875773647a0SAndy Whitcroft			# the context looking for a comment "edge".  If this
1876773647a0SAndy Whitcroft			# edge is a close comment then we must be in a comment
1877773647a0SAndy Whitcroft			# at context start.
1878773647a0SAndy Whitcroft			my $edge;
187901fa9147SAndy Whitcroft			my $cnt = $realcnt;
188001fa9147SAndy Whitcroft			for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
188101fa9147SAndy Whitcroft				next if (defined $rawlines[$ln - 1] &&
188201fa9147SAndy Whitcroft					 $rawlines[$ln - 1] =~ /^-/);
188301fa9147SAndy Whitcroft				$cnt--;
188401fa9147SAndy Whitcroft				#print "RAW<$rawlines[$ln - 1]>\n";
1885721c1cb6SAndy Whitcroft				last if (!defined $rawlines[$ln - 1]);
1886fae17daeSAndy Whitcroft				if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
1887fae17daeSAndy Whitcroft				    $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
1888fae17daeSAndy Whitcroft					($edge) = $1;
1889fae17daeSAndy Whitcroft					last;
1890fae17daeSAndy Whitcroft				}
1891773647a0SAndy Whitcroft			}
1892773647a0SAndy Whitcroft			if (defined $edge && $edge eq '*/') {
1893773647a0SAndy Whitcroft				$in_comment = 1;
1894773647a0SAndy Whitcroft			}
1895773647a0SAndy Whitcroft
1896773647a0SAndy Whitcroft			# Guestimate if this is a continuing comment.  If this
1897773647a0SAndy Whitcroft			# is the start of a diff block and this line starts
1898773647a0SAndy Whitcroft			# ' *' then it is very likely a comment.
1899773647a0SAndy Whitcroft			if (!defined $edge &&
190083242e0cSAndy Whitcroft			    $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
1901773647a0SAndy Whitcroft			{
1902773647a0SAndy Whitcroft				$in_comment = 1;
1903773647a0SAndy Whitcroft			}
1904773647a0SAndy Whitcroft
1905773647a0SAndy Whitcroft			##print "COMMENT:$in_comment edge<$edge> $rawline\n";
1906773647a0SAndy Whitcroft			sanitise_line_reset($in_comment);
1907773647a0SAndy Whitcroft
1908171ae1a4SAndy Whitcroft		} elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
1909773647a0SAndy Whitcroft			# Standardise the strings and chars within the input to
1910171ae1a4SAndy Whitcroft			# simplify matching -- only bother with positive lines.
1911773647a0SAndy Whitcroft			$line = sanitise_line($rawline);
1912773647a0SAndy Whitcroft		}
1913773647a0SAndy Whitcroft		push(@lines, $line);
1914773647a0SAndy Whitcroft
1915773647a0SAndy Whitcroft		if ($realcnt > 1) {
1916773647a0SAndy Whitcroft			$realcnt-- if ($line =~ /^(?:\+| |$)/);
1917773647a0SAndy Whitcroft		} else {
1918773647a0SAndy Whitcroft			$realcnt = 0;
1919773647a0SAndy Whitcroft		}
1920773647a0SAndy Whitcroft
1921773647a0SAndy Whitcroft		#print "==>$rawline\n";
1922773647a0SAndy Whitcroft		#print "-->$line\n";
1923de7d4f0eSAndy Whitcroft
1924de7d4f0eSAndy Whitcroft		if ($setup_docs && $line =~ /^\+/) {
1925de7d4f0eSAndy Whitcroft			push(@setup_docs, $line);
1926de7d4f0eSAndy Whitcroft		}
1927de7d4f0eSAndy Whitcroft	}
1928de7d4f0eSAndy Whitcroft
19296c72ffaaSAndy Whitcroft	$prefix = '';
19306c72ffaaSAndy Whitcroft
1931773647a0SAndy Whitcroft	$realcnt = 0;
1932773647a0SAndy Whitcroft	$linenr = 0;
1933194f66fcSJoe Perches	$fixlinenr = -1;
19340a920b5bSAndy Whitcroft	foreach my $line (@lines) {
19350a920b5bSAndy Whitcroft		$linenr++;
1936194f66fcSJoe Perches		$fixlinenr++;
19371b5539b1SJoe Perches		my $sline = $line;	#copy of $line
19381b5539b1SJoe Perches		$sline =~ s/$;/ /g;	#with comments as spaces
19390a920b5bSAndy Whitcroft
1940c2fdda0dSAndy Whitcroft		my $rawline = $rawlines[$linenr - 1];
19416c72ffaaSAndy Whitcroft
19420a920b5bSAndy Whitcroft#extract the line range in the file after the patch is applied
19436c72ffaaSAndy Whitcroft		if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
19440a920b5bSAndy Whitcroft			$is_patch = 1;
19454a0df2efSAndy Whitcroft			$first_line = $linenr + 1;
19460a920b5bSAndy Whitcroft			$realline=$1-1;
19470a920b5bSAndy Whitcroft			if (defined $2) {
19480a920b5bSAndy Whitcroft				$realcnt=$3+1;
19490a920b5bSAndy Whitcroft			} else {
19500a920b5bSAndy Whitcroft				$realcnt=1+1;
19510a920b5bSAndy Whitcroft			}
1952c2fdda0dSAndy Whitcroft			annotate_reset();
195313214adfSAndy Whitcroft			$prev_values = 'E';
195413214adfSAndy Whitcroft
1955773647a0SAndy Whitcroft			%suppress_ifbraces = ();
1956170d3a22SAndy Whitcroft			%suppress_whiletrailers = ();
19572b474a1aSAndy Whitcroft			%suppress_export = ();
19583e469cdcSAndy Whitcroft			$suppress_statement = 0;
19590a920b5bSAndy Whitcroft			next;
19600a920b5bSAndy Whitcroft
19614a0df2efSAndy Whitcroft# track the line number as we move through the hunk, note that
19624a0df2efSAndy Whitcroft# new versions of GNU diff omit the leading space on completely
19634a0df2efSAndy Whitcroft# blank context lines so we need to count that too.
1964773647a0SAndy Whitcroft		} elsif ($line =~ /^( |\+|$)/) {
19650a920b5bSAndy Whitcroft			$realline++;
1966d8aaf121SAndy Whitcroft			$realcnt-- if ($realcnt != 0);
19670a920b5bSAndy Whitcroft
19684a0df2efSAndy Whitcroft			# Measure the line length and indent.
1969c2fdda0dSAndy Whitcroft			($length, $indent) = line_stats($rawline);
19700a920b5bSAndy Whitcroft
19710a920b5bSAndy Whitcroft			# Track the previous line.
19720a920b5bSAndy Whitcroft			($prevline, $stashline) = ($stashline, $line);
19730a920b5bSAndy Whitcroft			($previndent, $stashindent) = ($stashindent, $indent);
1974c2fdda0dSAndy Whitcroft			($prevrawline, $stashrawline) = ($stashrawline, $rawline);
1975c2fdda0dSAndy Whitcroft
1976773647a0SAndy Whitcroft			#warn "line<$line>\n";
19776c72ffaaSAndy Whitcroft
1978d8aaf121SAndy Whitcroft		} elsif ($realcnt == 1) {
1979d8aaf121SAndy Whitcroft			$realcnt--;
19800a920b5bSAndy Whitcroft		}
19810a920b5bSAndy Whitcroft
1982cc77cdcaSAndy Whitcroft		my $hunk_line = ($realcnt != 0);
1983cc77cdcaSAndy Whitcroft
19840a920b5bSAndy Whitcroft#make up the handle for any error we report on this line
1985773647a0SAndy Whitcroft		$prefix = "$filename:$realline: " if ($emacs && $file);
1986773647a0SAndy Whitcroft		$prefix = "$filename:$linenr: " if ($emacs && !$file);
1987773647a0SAndy Whitcroft
19886c72ffaaSAndy Whitcroft		$here = "#$linenr: " if (!$file);
19896c72ffaaSAndy Whitcroft		$here = "#$realline: " if ($file);
1990773647a0SAndy Whitcroft
19912ac73b4fSJoe Perches		my $found_file = 0;
1992773647a0SAndy Whitcroft		# extract the filename as it passes
19933bf9a009SRabin Vincent		if ($line =~ /^diff --git.*?(\S+)$/) {
19943bf9a009SRabin Vincent			$realfile = $1;
19952b7ab453SJoe Perches			$realfile =~ s@^([^/]*)/@@ if (!$file);
1996270c49a0SJoe Perches			$in_commit_log = 0;
19972ac73b4fSJoe Perches			$found_file = 1;
19983bf9a009SRabin Vincent		} elsif ($line =~ /^\+\+\+\s+(\S+)/) {
1999773647a0SAndy Whitcroft			$realfile = $1;
20002b7ab453SJoe Perches			$realfile =~ s@^([^/]*)/@@ if (!$file);
2001270c49a0SJoe Perches			$in_commit_log = 0;
20021e855726SWolfram Sang
20031e855726SWolfram Sang			$p1_prefix = $1;
2004e2f7aa4bSAndy Whitcroft			if (!$file && $tree && $p1_prefix ne '' &&
2005e2f7aa4bSAndy Whitcroft			    -e "$root/$p1_prefix") {
2006000d1cc1SJoe Perches				WARN("PATCH_PREFIX",
2007000d1cc1SJoe Perches				     "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
20081e855726SWolfram Sang			}
2009773647a0SAndy Whitcroft
2010c1ab3326SAndy Whitcroft			if ($realfile =~ m@^include/asm/@) {
2011000d1cc1SJoe Perches				ERROR("MODIFIED_INCLUDE_ASM",
2012000d1cc1SJoe Perches				      "do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
2013773647a0SAndy Whitcroft			}
20142ac73b4fSJoe Perches			$found_file = 1;
20152ac73b4fSJoe Perches		}
20162ac73b4fSJoe Perches
20172ac73b4fSJoe Perches		if ($found_file) {
20182ac73b4fSJoe Perches			if ($realfile =~ m@^(drivers/net/|net/)@) {
20192ac73b4fSJoe Perches				$check = 1;
20202ac73b4fSJoe Perches			} else {
20212ac73b4fSJoe Perches				$check = $check_orig;
20222ac73b4fSJoe Perches			}
2023773647a0SAndy Whitcroft			next;
2024773647a0SAndy Whitcroft		}
2025773647a0SAndy Whitcroft
2026389834b6SRandy Dunlap		$here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
20270a920b5bSAndy Whitcroft
2028c2fdda0dSAndy Whitcroft		my $hereline = "$here\n$rawline\n";
2029c2fdda0dSAndy Whitcroft		my $herecurr = "$here\n$rawline\n";
2030c2fdda0dSAndy Whitcroft		my $hereprev = "$here\n$prevrawline\n$rawline\n";
20310a920b5bSAndy Whitcroft
20326c72ffaaSAndy Whitcroft		$cnt_lines++ if ($realcnt != 0);
20336c72ffaaSAndy Whitcroft
20343bf9a009SRabin Vincent# Check for incorrect file permissions
20353bf9a009SRabin Vincent		if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
20363bf9a009SRabin Vincent			my $permhere = $here . "FILE: $realfile\n";
203704db4d25SJoe Perches			if ($realfile !~ m@scripts/@ &&
203804db4d25SJoe Perches			    $realfile !~ /\.(py|pl|awk|sh)$/) {
2039000d1cc1SJoe Perches				ERROR("EXECUTE_PERMISSIONS",
2040000d1cc1SJoe Perches				      "do not set execute permissions for source files\n" . $permhere);
20413bf9a009SRabin Vincent			}
20423bf9a009SRabin Vincent		}
20433bf9a009SRabin Vincent
204420112475SJoe Perches# Check the patch for a signoff:
2045d8aaf121SAndy Whitcroft		if ($line =~ /^\s*signed-off-by:/i) {
20464a0df2efSAndy Whitcroft			$signoff++;
204715662b3eSJoe Perches			$in_commit_log = 0;
20480a920b5bSAndy Whitcroft		}
204920112475SJoe Perches
205020112475SJoe Perches# Check signature styles
2051270c49a0SJoe Perches		if (!$in_header_lines &&
2052ce0338dfSJoe Perches		    $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
205320112475SJoe Perches			my $space_before = $1;
205420112475SJoe Perches			my $sign_off = $2;
205520112475SJoe Perches			my $space_after = $3;
205620112475SJoe Perches			my $email = $4;
205720112475SJoe Perches			my $ucfirst_sign_off = ucfirst(lc($sign_off));
205820112475SJoe Perches
2059ce0338dfSJoe Perches			if ($sign_off !~ /$signature_tags/) {
2060ce0338dfSJoe Perches				WARN("BAD_SIGN_OFF",
2061ce0338dfSJoe Perches				     "Non-standard signature: $sign_off\n" . $herecurr);
2062ce0338dfSJoe Perches			}
206320112475SJoe Perches			if (defined $space_before && $space_before ne "") {
20643705ce5bSJoe Perches				if (WARN("BAD_SIGN_OFF",
20653705ce5bSJoe Perches					 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
20663705ce5bSJoe Perches				    $fix) {
2067194f66fcSJoe Perches					$fixed[$fixlinenr] =
20683705ce5bSJoe Perches					    "$ucfirst_sign_off $email";
20693705ce5bSJoe Perches				}
207020112475SJoe Perches			}
207120112475SJoe Perches			if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
20723705ce5bSJoe Perches				if (WARN("BAD_SIGN_OFF",
20733705ce5bSJoe Perches					 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
20743705ce5bSJoe Perches				    $fix) {
2075194f66fcSJoe Perches					$fixed[$fixlinenr] =
20763705ce5bSJoe Perches					    "$ucfirst_sign_off $email";
20773705ce5bSJoe Perches				}
20783705ce5bSJoe Perches
207920112475SJoe Perches			}
208020112475SJoe Perches			if (!defined $space_after || $space_after ne " ") {
20813705ce5bSJoe Perches				if (WARN("BAD_SIGN_OFF",
20823705ce5bSJoe Perches					 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
20833705ce5bSJoe Perches				    $fix) {
2084194f66fcSJoe Perches					$fixed[$fixlinenr] =
20853705ce5bSJoe Perches					    "$ucfirst_sign_off $email";
20863705ce5bSJoe Perches				}
208720112475SJoe Perches			}
208820112475SJoe Perches
208920112475SJoe Perches			my ($email_name, $email_address, $comment) = parse_email($email);
209020112475SJoe Perches			my $suggested_email = format_email(($email_name, $email_address));
209120112475SJoe Perches			if ($suggested_email eq "") {
2092000d1cc1SJoe Perches				ERROR("BAD_SIGN_OFF",
2093000d1cc1SJoe Perches				      "Unrecognized email address: '$email'\n" . $herecurr);
209420112475SJoe Perches			} else {
209520112475SJoe Perches				my $dequoted = $suggested_email;
209620112475SJoe Perches				$dequoted =~ s/^"//;
209720112475SJoe Perches				$dequoted =~ s/" </ </;
209820112475SJoe Perches				# Don't force email to have quotes
209920112475SJoe Perches				# Allow just an angle bracketed address
210020112475SJoe Perches				if ("$dequoted$comment" ne $email &&
210120112475SJoe Perches				    "<$email_address>$comment" ne $email &&
210220112475SJoe Perches				    "$suggested_email$comment" ne $email) {
2103000d1cc1SJoe Perches					WARN("BAD_SIGN_OFF",
2104000d1cc1SJoe Perches					     "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
210520112475SJoe Perches				}
21060a920b5bSAndy Whitcroft			}
21077e51f197SJoe Perches
21087e51f197SJoe Perches# Check for duplicate signatures
21097e51f197SJoe Perches			my $sig_nospace = $line;
21107e51f197SJoe Perches			$sig_nospace =~ s/\s//g;
21117e51f197SJoe Perches			$sig_nospace = lc($sig_nospace);
21127e51f197SJoe Perches			if (defined $signatures{$sig_nospace}) {
21137e51f197SJoe Perches				WARN("BAD_SIGN_OFF",
21147e51f197SJoe Perches				     "Duplicate signature\n" . $herecurr);
21157e51f197SJoe Perches			} else {
21167e51f197SJoe Perches				$signatures{$sig_nospace} = 1;
21177e51f197SJoe Perches			}
21180a920b5bSAndy Whitcroft		}
21190a920b5bSAndy Whitcroft
21209b3189ebSJoe Perches# Check for old stable address
21219b3189ebSJoe Perches		if ($line =~ /^\s*cc:\s*.*<?\bstable\@kernel\.org\b>?.*$/i) {
21229b3189ebSJoe Perches			ERROR("STABLE_ADDRESS",
21239b3189ebSJoe Perches			      "The 'stable' address should be 'stable\@vger.kernel.org'\n" . $herecurr);
21249b3189ebSJoe Perches		}
21259b3189ebSJoe Perches
21267ebd05efSChristopher Covington# Check for unwanted Gerrit info
21277ebd05efSChristopher Covington		if ($in_commit_log && $line =~ /^\s*change-id:/i) {
21287ebd05efSChristopher Covington			ERROR("GERRIT_CHANGE_ID",
21297ebd05efSChristopher Covington			      "Remove Gerrit Change-Id's before submitting upstream.\n" . $herecurr);
21307ebd05efSChristopher Covington		}
21317ebd05efSChristopher Covington
2132d311cd44SJoe Perches# Check for improperly formed commit descriptions
2133d311cd44SJoe Perches		if ($in_commit_log &&
2134d311cd44SJoe Perches		    $line =~ /\bcommit\s+[0-9a-f]{5,}/i &&
2135d311cd44SJoe Perches		    $line !~ /\b[Cc]ommit [0-9a-f]{12,16} \("/) {
2136d311cd44SJoe Perches			$line =~ /\b(c)ommit\s+([0-9a-f]{5,})/i;
2137d311cd44SJoe Perches			my $init_char = $1;
2138d311cd44SJoe Perches			my $orig_commit = lc($2);
2139d311cd44SJoe Perches			my $id = '01234567890ab';
2140d311cd44SJoe Perches			my $desc = 'commit description';
2141d311cd44SJoe Perches		        ($id, $desc) = git_commit_info($orig_commit, $id, $desc);
2142d311cd44SJoe Perches			ERROR("GIT_COMMIT_ID",
2143d311cd44SJoe Perches			      "Please use 12 to 16 chars for the git commit ID like: '${init_char}ommit $id (\"$desc\")'\n" . $herecurr);
2144d311cd44SJoe Perches		}
2145d311cd44SJoe Perches
214613f1937eSJoe Perches# Check for added, moved or deleted files
214713f1937eSJoe Perches		if (!$reported_maintainer_file && !$in_commit_log &&
214813f1937eSJoe Perches		    ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ ||
214913f1937eSJoe Perches		     $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ ||
215013f1937eSJoe Perches		     ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ &&
215113f1937eSJoe Perches		      (defined($1) || defined($2))))) {
215213f1937eSJoe Perches			$reported_maintainer_file = 1;
215313f1937eSJoe Perches			WARN("FILE_PATH_CHANGES",
215413f1937eSJoe Perches			     "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr);
215513f1937eSJoe Perches		}
215613f1937eSJoe Perches
215700df344fSAndy Whitcroft# Check for wrappage within a valid hunk of the file
21588905a67cSAndy Whitcroft		if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
2159000d1cc1SJoe Perches			ERROR("CORRUPTED_PATCH",
2160000d1cc1SJoe Perches			      "patch seems to be corrupt (line wrapped?)\n" .
21616c72ffaaSAndy Whitcroft				$herecurr) if (!$emitted_corrupt++);
2162de7d4f0eSAndy Whitcroft		}
2163de7d4f0eSAndy Whitcroft
21646ecd9674SAndy Whitcroft# Check for absolute kernel paths.
21656ecd9674SAndy Whitcroft		if ($tree) {
21666ecd9674SAndy Whitcroft			while ($line =~ m{(?:^|\s)(/\S*)}g) {
21676ecd9674SAndy Whitcroft				my $file = $1;
21686ecd9674SAndy Whitcroft
21696ecd9674SAndy Whitcroft				if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
21706ecd9674SAndy Whitcroft				    check_absolute_file($1, $herecurr)) {
21716ecd9674SAndy Whitcroft					#
21726ecd9674SAndy Whitcroft				} else {
21736ecd9674SAndy Whitcroft					check_absolute_file($file, $herecurr);
21746ecd9674SAndy Whitcroft				}
21756ecd9674SAndy Whitcroft			}
21766ecd9674SAndy Whitcroft		}
21776ecd9674SAndy Whitcroft
2178de7d4f0eSAndy Whitcroft# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
2179de7d4f0eSAndy Whitcroft		if (($realfile =~ /^$/ || $line =~ /^\+/) &&
2180171ae1a4SAndy Whitcroft		    $rawline !~ m/^$UTF8*$/) {
2181171ae1a4SAndy Whitcroft			my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
2182171ae1a4SAndy Whitcroft
2183171ae1a4SAndy Whitcroft			my $blank = copy_spacing($rawline);
2184171ae1a4SAndy Whitcroft			my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
2185171ae1a4SAndy Whitcroft			my $hereptr = "$hereline$ptr\n";
2186171ae1a4SAndy Whitcroft
218734d99219SJoe Perches			CHK("INVALID_UTF8",
2188000d1cc1SJoe Perches			    "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
218900df344fSAndy Whitcroft		}
21900a920b5bSAndy Whitcroft
219115662b3eSJoe Perches# Check if it's the start of a commit log
219215662b3eSJoe Perches# (not a header line and we haven't seen the patch filename)
219315662b3eSJoe Perches		if ($in_header_lines && $realfile =~ /^$/ &&
219429ee1b0cSJoe Perches		    !($rawline =~ /^\s+\S/ ||
219529ee1b0cSJoe Perches		      $rawline =~ /^(commit\b|from\b|[\w-]+:).*$/i)) {
219615662b3eSJoe Perches			$in_header_lines = 0;
219715662b3eSJoe Perches			$in_commit_log = 1;
219815662b3eSJoe Perches		}
219915662b3eSJoe Perches
2200fa64205dSPasi Savanainen# Check if there is UTF-8 in a commit log when a mail header has explicitly
2201fa64205dSPasi Savanainen# declined it, i.e defined some charset where it is missing.
2202fa64205dSPasi Savanainen		if ($in_header_lines &&
2203fa64205dSPasi Savanainen		    $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
2204fa64205dSPasi Savanainen		    $1 !~ /utf-8/i) {
2205fa64205dSPasi Savanainen			$non_utf8_charset = 1;
2206fa64205dSPasi Savanainen		}
2207fa64205dSPasi Savanainen
2208fa64205dSPasi Savanainen		if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
220915662b3eSJoe Perches		    $rawline =~ /$NON_ASCII_UTF8/) {
2210fa64205dSPasi Savanainen			WARN("UTF8_BEFORE_PATCH",
221115662b3eSJoe Perches			    "8-bit UTF-8 used in possible commit log\n" . $herecurr);
221215662b3eSJoe Perches		}
221315662b3eSJoe Perches
221430670854SAndy Whitcroft# ignore non-hunk lines and lines being removed
221530670854SAndy Whitcroft		next if (!$hunk_line || $line =~ /^-/);
221600df344fSAndy Whitcroft
22170a920b5bSAndy Whitcroft#trailing whitespace
22189c0ca6f9SAndy Whitcroft		if ($line =~ /^\+.*\015/) {
2219c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2220d5e616fcSJoe Perches			if (ERROR("DOS_LINE_ENDINGS",
2221d5e616fcSJoe Perches				  "DOS line endings\n" . $herevet) &&
2222d5e616fcSJoe Perches			    $fix) {
2223194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/[\s\015]+$//;
2224d5e616fcSJoe Perches			}
2225c2fdda0dSAndy Whitcroft		} elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
2226c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
22273705ce5bSJoe Perches			if (ERROR("TRAILING_WHITESPACE",
22283705ce5bSJoe Perches				  "trailing whitespace\n" . $herevet) &&
22293705ce5bSJoe Perches			    $fix) {
2230194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\s+$//;
22313705ce5bSJoe Perches			}
22323705ce5bSJoe Perches
2233d2c0a235SAndy Whitcroft			$rpt_cleaners = 1;
22340a920b5bSAndy Whitcroft		}
22355368df20SAndy Whitcroft
22364783f894SJosh Triplett# Check for FSF mailing addresses.
2237109d8cb2SAlexander Duyck		if ($rawline =~ /\bwrite to the Free/i ||
22383e2232f2SJoe Perches		    $rawline =~ /\b59\s+Temple\s+Pl/i ||
22393e2232f2SJoe Perches		    $rawline =~ /\b51\s+Franklin\s+St/i) {
22404783f894SJosh Triplett			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
22414783f894SJosh Triplett			my $msg_type = \&ERROR;
22424783f894SJosh Triplett			$msg_type = \&CHK if ($file);
22434783f894SJosh Triplett			&{$msg_type}("FSF_MAILING_ADDRESS",
22444783f894SJosh 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)
22454783f894SJosh Triplett		}
22464783f894SJosh Triplett
22473354957aSAndi Kleen# check for Kconfig help text having a real description
22489fe287d7SAndy Whitcroft# Only applies when adding the entry originally, after that we do not have
22499fe287d7SAndy Whitcroft# sufficient context to determine whether it is indeed long enough.
22503354957aSAndi Kleen		if ($realfile =~ /Kconfig/ &&
22518d73e0e7SJoe Perches		    $line =~ /^\+\s*config\s+/) {
22523354957aSAndi Kleen			my $length = 0;
22539fe287d7SAndy Whitcroft			my $cnt = $realcnt;
22549fe287d7SAndy Whitcroft			my $ln = $linenr + 1;
22559fe287d7SAndy Whitcroft			my $f;
2256a1385803SAndy Whitcroft			my $is_start = 0;
22579fe287d7SAndy Whitcroft			my $is_end = 0;
2258a1385803SAndy Whitcroft			for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
22599fe287d7SAndy Whitcroft				$f = $lines[$ln - 1];
22609fe287d7SAndy Whitcroft				$cnt-- if ($lines[$ln - 1] !~ /^-/);
22619fe287d7SAndy Whitcroft				$is_end = $lines[$ln - 1] =~ /^\+/;
22629fe287d7SAndy Whitcroft
22639fe287d7SAndy Whitcroft				next if ($f =~ /^-/);
22648d73e0e7SJoe Perches				last if (!$file && $f =~ /^\@\@/);
2265a1385803SAndy Whitcroft
22668d73e0e7SJoe Perches				if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate)\s*\"/) {
2267a1385803SAndy Whitcroft					$is_start = 1;
22688d73e0e7SJoe Perches				} elsif ($lines[$ln - 1] =~ /^\+\s*(?:---)?help(?:---)?$/) {
2269a1385803SAndy Whitcroft					$length = -1;
2270a1385803SAndy Whitcroft				}
2271a1385803SAndy Whitcroft
22729fe287d7SAndy Whitcroft				$f =~ s/^.//;
22733354957aSAndi Kleen				$f =~ s/#.*//;
22743354957aSAndi Kleen				$f =~ s/^\s+//;
22753354957aSAndi Kleen				next if ($f =~ /^$/);
22769fe287d7SAndy Whitcroft				if ($f =~ /^\s*config\s/) {
22779fe287d7SAndy Whitcroft					$is_end = 1;
22789fe287d7SAndy Whitcroft					last;
22799fe287d7SAndy Whitcroft				}
22803354957aSAndi Kleen				$length++;
22813354957aSAndi Kleen			}
2282000d1cc1SJoe Perches			WARN("CONFIG_DESCRIPTION",
2283a1385803SAndy Whitcroft			     "please write a paragraph that describes the config symbol fully\n" . $herecurr) if ($is_start && $is_end && $length < 4);
2284a1385803SAndy Whitcroft			#print "is_start<$is_start> is_end<$is_end> length<$length>\n";
22853354957aSAndi Kleen		}
22863354957aSAndi Kleen
22871ba8dfd1SKees Cook# discourage the addition of CONFIG_EXPERIMENTAL in Kconfig.
22881ba8dfd1SKees Cook		if ($realfile =~ /Kconfig/ &&
22891ba8dfd1SKees Cook		    $line =~ /.\s*depends on\s+.*\bEXPERIMENTAL\b/) {
22901ba8dfd1SKees Cook			WARN("CONFIG_EXPERIMENTAL",
22911ba8dfd1SKees Cook			     "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
22921ba8dfd1SKees Cook		}
22931ba8dfd1SKees Cook
2294c68e5878SArnaud Lacombe		if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
2295c68e5878SArnaud Lacombe		    ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
2296c68e5878SArnaud Lacombe			my $flag = $1;
2297c68e5878SArnaud Lacombe			my $replacement = {
2298c68e5878SArnaud Lacombe				'EXTRA_AFLAGS' =>   'asflags-y',
2299c68e5878SArnaud Lacombe				'EXTRA_CFLAGS' =>   'ccflags-y',
2300c68e5878SArnaud Lacombe				'EXTRA_CPPFLAGS' => 'cppflags-y',
2301c68e5878SArnaud Lacombe				'EXTRA_LDFLAGS' =>  'ldflags-y',
2302c68e5878SArnaud Lacombe			};
2303c68e5878SArnaud Lacombe
2304c68e5878SArnaud Lacombe			WARN("DEPRECATED_VARIABLE",
2305c68e5878SArnaud Lacombe			     "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
2306c68e5878SArnaud Lacombe		}
2307c68e5878SArnaud Lacombe
2308bff5da43SRob Herring# check for DT compatible documentation
23097dd05b38SFlorian Vaussard		if (defined $root &&
23107dd05b38SFlorian Vaussard			(($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) ||
23117dd05b38SFlorian Vaussard			 ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) {
23127dd05b38SFlorian Vaussard
2313bff5da43SRob Herring			my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g;
2314bff5da43SRob Herring
2315cc93319bSFlorian Vaussard			my $dt_path = $root . "/Documentation/devicetree/bindings/";
2316cc93319bSFlorian Vaussard			my $vp_file = $dt_path . "vendor-prefixes.txt";
2317cc93319bSFlorian Vaussard
2318bff5da43SRob Herring			foreach my $compat (@compats) {
2319bff5da43SRob Herring				my $compat2 = $compat;
2320185d566bSRob Herring				$compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/;
2321185d566bSRob Herring				my $compat3 = $compat;
2322185d566bSRob Herring				$compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/;
2323185d566bSRob Herring				`grep -Erq "$compat|$compat2|$compat3" $dt_path`;
2324bff5da43SRob Herring				if ( $? >> 8 ) {
2325bff5da43SRob Herring					WARN("UNDOCUMENTED_DT_STRING",
2326bff5da43SRob Herring					     "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr);
2327bff5da43SRob Herring				}
2328bff5da43SRob Herring
23294fbf32a6SFlorian Vaussard				next if $compat !~ /^([a-zA-Z0-9\-]+)\,/;
23304fbf32a6SFlorian Vaussard				my $vendor = $1;
2331cc93319bSFlorian Vaussard				`grep -Eq "^$vendor\\b" $vp_file`;
2332bff5da43SRob Herring				if ( $? >> 8 ) {
2333bff5da43SRob Herring					WARN("UNDOCUMENTED_DT_STRING",
2334cc93319bSFlorian Vaussard					     "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr);
2335bff5da43SRob Herring				}
2336bff5da43SRob Herring			}
2337bff5da43SRob Herring		}
2338bff5da43SRob Herring
23395368df20SAndy Whitcroft# check we are in a valid source file if not then ignore this hunk
23405368df20SAndy Whitcroft		next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
23415368df20SAndy Whitcroft
23426cd7f386SJoe Perches#line length limit
2343c45dcabdSAndy Whitcroft		if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ &&
2344f4c014c0SAndy Whitcroft		    $rawline !~ /^.\s*\*\s*\@$Ident\s/ &&
23450fccc622SJoe Perches		    !($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(KERN_\S+\s*|[^"]*))?"[X\t]*"\s*(?:|,|\)\s*;)\s*$/ ||
23468bbea968SJoe Perches		    $line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) &&
23476cd7f386SJoe Perches		    $length > $max_line_length)
2348c45dcabdSAndy Whitcroft		{
2349000d1cc1SJoe Perches			WARN("LONG_LINE",
23506cd7f386SJoe Perches			     "line over $max_line_length characters\n" . $herecurr);
23510a920b5bSAndy Whitcroft		}
23520a920b5bSAndy Whitcroft
2353ca56dc09SJosh Triplett# Check for user-visible strings broken across lines, which breaks the ability
23548c5fcd24SJoe Perches# to grep for the string.  Make exceptions when the previous string ends in a
23558c5fcd24SJoe Perches# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{'
23568c5fcd24SJoe Perches# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value
2357ca56dc09SJosh Triplett		if ($line =~ /^\+\s*"/ &&
2358ca56dc09SJosh Triplett		    $prevline =~ /"\s*$/ &&
23598c5fcd24SJoe Perches		    $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) {
2360ca56dc09SJosh Triplett			WARN("SPLIT_STRING",
2361ca56dc09SJosh Triplett			     "quoted string split across lines\n" . $hereprev);
2362ca56dc09SJosh Triplett		}
2363ca56dc09SJosh Triplett
23645e79d96eSJoe Perches# check for spaces before a quoted newline
23655e79d96eSJoe Perches		if ($rawline =~ /^.*\".*\s\\n/) {
23663705ce5bSJoe Perches			if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
23673705ce5bSJoe Perches				 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
23683705ce5bSJoe Perches			    $fix) {
2369194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
23703705ce5bSJoe Perches			}
23713705ce5bSJoe Perches
23725e79d96eSJoe Perches		}
23735e79d96eSJoe Perches
23748905a67cSAndy Whitcroft# check for adding lines without a newline.
23758905a67cSAndy Whitcroft		if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
2376000d1cc1SJoe Perches			WARN("MISSING_EOF_NEWLINE",
2377000d1cc1SJoe Perches			     "adding a line without newline at end of file\n" . $herecurr);
23788905a67cSAndy Whitcroft		}
23798905a67cSAndy Whitcroft
238042e41c54SMike Frysinger# Blackfin: use hi/lo macros
238142e41c54SMike Frysinger		if ($realfile =~ m@arch/blackfin/.*\.S$@) {
238242e41c54SMike Frysinger			if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
238342e41c54SMike Frysinger				my $herevet = "$here\n" . cat_vet($line) . "\n";
2384000d1cc1SJoe Perches				ERROR("LO_MACRO",
2385000d1cc1SJoe Perches				      "use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
238642e41c54SMike Frysinger			}
238742e41c54SMike Frysinger			if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
238842e41c54SMike Frysinger				my $herevet = "$here\n" . cat_vet($line) . "\n";
2389000d1cc1SJoe Perches				ERROR("HI_MACRO",
2390000d1cc1SJoe Perches				      "use the HI() macro, not (... >> 16)\n" . $herevet);
239142e41c54SMike Frysinger			}
239242e41c54SMike Frysinger		}
239342e41c54SMike Frysinger
2394b9ea10d6SAndy Whitcroft# check we are in a valid source file C or perl if not then ignore this hunk
2395b9ea10d6SAndy Whitcroft		next if ($realfile !~ /\.(h|c|pl)$/);
23960a920b5bSAndy Whitcroft
23970a920b5bSAndy Whitcroft# at the beginning of a line any tabs must come first and anything
23980a920b5bSAndy Whitcroft# more than 8 must use tabs.
2399c2fdda0dSAndy Whitcroft		if ($rawline =~ /^\+\s* \t\s*\S/ ||
2400c2fdda0dSAndy Whitcroft		    $rawline =~ /^\+\s*        \s*/) {
2401c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2402d2c0a235SAndy Whitcroft			$rpt_cleaners = 1;
24033705ce5bSJoe Perches			if (ERROR("CODE_INDENT",
24043705ce5bSJoe Perches				  "code indent should use tabs where possible\n" . $herevet) &&
24053705ce5bSJoe Perches			    $fix) {
2406194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
24073705ce5bSJoe Perches			}
24080a920b5bSAndy Whitcroft		}
24090a920b5bSAndy Whitcroft
241008e44365SAlberto Panizzo# check for space before tabs.
241108e44365SAlberto Panizzo		if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
241208e44365SAlberto Panizzo			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
24133705ce5bSJoe Perches			if (WARN("SPACE_BEFORE_TAB",
24143705ce5bSJoe Perches				"please, no space before tabs\n" . $herevet) &&
24153705ce5bSJoe Perches			    $fix) {
2416194f66fcSJoe Perches				while ($fixed[$fixlinenr] =~
2417c76f4cb3SJoe Perches					   s/(^\+.*) {8,8}+\t/$1\t\t/) {}
2418194f66fcSJoe Perches				while ($fixed[$fixlinenr] =~
2419c76f4cb3SJoe Perches					   s/(^\+.*) +\t/$1\t/) {}
24203705ce5bSJoe Perches			}
242108e44365SAlberto Panizzo		}
242208e44365SAlberto Panizzo
2423d1fe9c09SJoe Perches# check for && or || at the start of a line
2424d1fe9c09SJoe Perches		if ($rawline =~ /^\+\s*(&&|\|\|)/) {
2425d1fe9c09SJoe Perches			CHK("LOGICAL_CONTINUATIONS",
2426d1fe9c09SJoe Perches			    "Logical continuations should be on the previous line\n" . $hereprev);
2427d1fe9c09SJoe Perches		}
2428d1fe9c09SJoe Perches
2429d1fe9c09SJoe Perches# check multi-line statement indentation matches previous line
2430d1fe9c09SJoe Perches		if ($^V && $^V ge 5.10.0 &&
243191cb5195SJoe Perches		    $prevline =~ /^\+([ \t]*)((?:$c90_Keywords(?:\s+if)\s*)|(?:$Declare\s*)?(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*|$Ident\s*=\s*$Ident\s*)\(.*(\&\&|\|\||,)\s*$/) {
2432d1fe9c09SJoe Perches			$prevline =~ /^\+(\t*)(.*)$/;
2433d1fe9c09SJoe Perches			my $oldindent = $1;
2434d1fe9c09SJoe Perches			my $rest = $2;
2435d1fe9c09SJoe Perches
2436d1fe9c09SJoe Perches			my $pos = pos_last_openparen($rest);
2437d1fe9c09SJoe Perches			if ($pos >= 0) {
2438b34a26f3SJoe Perches				$line =~ /^(\+| )([ \t]*)/;
2439b34a26f3SJoe Perches				my $newindent = $2;
2440d1fe9c09SJoe Perches
2441d1fe9c09SJoe Perches				my $goodtabindent = $oldindent .
2442d1fe9c09SJoe Perches					"\t" x ($pos / 8) .
2443d1fe9c09SJoe Perches					" "  x ($pos % 8);
2444d1fe9c09SJoe Perches				my $goodspaceindent = $oldindent . " "  x $pos;
2445d1fe9c09SJoe Perches
2446d1fe9c09SJoe Perches				if ($newindent ne $goodtabindent &&
2447d1fe9c09SJoe Perches				    $newindent ne $goodspaceindent) {
24483705ce5bSJoe Perches
24493705ce5bSJoe Perches					if (CHK("PARENTHESIS_ALIGNMENT",
24503705ce5bSJoe Perches						"Alignment should match open parenthesis\n" . $hereprev) &&
24513705ce5bSJoe Perches					    $fix && $line =~ /^\+/) {
2452194f66fcSJoe Perches						$fixed[$fixlinenr] =~
24533705ce5bSJoe Perches						    s/^\+[ \t]*/\+$goodtabindent/;
24543705ce5bSJoe Perches					}
2455d1fe9c09SJoe Perches				}
2456d1fe9c09SJoe Perches			}
2457d1fe9c09SJoe Perches		}
2458d1fe9c09SJoe Perches
2459f27c95dbSJoe Perches		if ($line =~ /^\+.*\(\s*$Type\s*\)[ \t]+(?!$Assignment|$Arithmetic)/) {
24603705ce5bSJoe Perches			if (CHK("SPACING",
2461f27c95dbSJoe Perches				"No space is necessary after a cast\n" . $herecurr) &&
24623705ce5bSJoe Perches			    $fix) {
2463194f66fcSJoe Perches				$fixed[$fixlinenr] =~
2464f27c95dbSJoe Perches				    s/(\(\s*$Type\s*\))[ \t]+/$1/;
24653705ce5bSJoe Perches			}
2466aad4f614SJoe Perches		}
2467aad4f614SJoe Perches
246805880600SJoe Perches		if ($realfile =~ m@^(drivers/net/|net/)@ &&
2469fdb4bcd6SJoe Perches		    $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
247085ad978cSJoe Perches		    $rawline =~ /^\+[ \t]*\*/ &&
247185ad978cSJoe Perches		    $realline > 2) {
247205880600SJoe Perches			WARN("NETWORKING_BLOCK_COMMENT_STYLE",
247305880600SJoe Perches			     "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
247405880600SJoe Perches		}
247505880600SJoe Perches
247605880600SJoe Perches		if ($realfile =~ m@^(drivers/net/|net/)@ &&
2477a605e32eSJoe Perches		    $prevrawline =~ /^\+[ \t]*\/\*/ &&		#starting /*
2478a605e32eSJoe Perches		    $prevrawline !~ /\*\/[ \t]*$/ &&		#no trailing */
247961135e96SJoe Perches		    $rawline =~ /^\+/ &&			#line is new
2480a605e32eSJoe Perches		    $rawline !~ /^\+[ \t]*\*/) {		#no leading *
2481a605e32eSJoe Perches			WARN("NETWORKING_BLOCK_COMMENT_STYLE",
2482a605e32eSJoe Perches			     "networking block comments start with * on subsequent lines\n" . $hereprev);
2483a605e32eSJoe Perches		}
2484a605e32eSJoe Perches
2485a605e32eSJoe Perches		if ($realfile =~ m@^(drivers/net/|net/)@ &&
2486c24f9f19SJoe Perches		    $rawline !~ m@^\+[ \t]*\*/[ \t]*$@ &&	#trailing */
2487c24f9f19SJoe Perches		    $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ &&	#inline /*...*/
2488c24f9f19SJoe Perches		    $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ &&	#trailing **/
2489c24f9f19SJoe Perches		    $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) {	#non blank */
249005880600SJoe Perches			WARN("NETWORKING_BLOCK_COMMENT_STYLE",
249105880600SJoe Perches			     "networking block comments put the trailing */ on a separate line\n" . $herecurr);
249205880600SJoe Perches		}
249305880600SJoe Perches
24947f619191SJoe Perches# check for missing blank lines after struct/union declarations
24957f619191SJoe Perches# with exceptions for various attributes and macros
24967f619191SJoe Perches		if ($prevline =~ /^[\+ ]};?\s*$/ &&
24977f619191SJoe Perches		    $line =~ /^\+/ &&
24987f619191SJoe Perches		    !($line =~ /^\+\s*$/ ||
24997f619191SJoe Perches		      $line =~ /^\+\s*EXPORT_SYMBOL/ ||
25007f619191SJoe Perches		      $line =~ /^\+\s*MODULE_/i ||
25017f619191SJoe Perches		      $line =~ /^\+\s*\#\s*(?:end|elif|else)/ ||
25027f619191SJoe Perches		      $line =~ /^\+[a-z_]*init/ ||
25037f619191SJoe Perches		      $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ ||
25047f619191SJoe Perches		      $line =~ /^\+\s*DECLARE/ ||
25057f619191SJoe Perches		      $line =~ /^\+\s*__setup/)) {
2506d752fcc8SJoe Perches			if (CHK("LINE_SPACING",
2507d752fcc8SJoe Perches				"Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) &&
2508d752fcc8SJoe Perches			    $fix) {
2509f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, "\+");
2510d752fcc8SJoe Perches			}
25117f619191SJoe Perches		}
25127f619191SJoe Perches
2513365dd4eaSJoe Perches# check for multiple consecutive blank lines
2514365dd4eaSJoe Perches		if ($prevline =~ /^[\+ ]\s*$/ &&
2515365dd4eaSJoe Perches		    $line =~ /^\+\s*$/ &&
2516365dd4eaSJoe Perches		    $last_blank_line != ($linenr - 1)) {
2517d752fcc8SJoe Perches			if (CHK("LINE_SPACING",
2518d752fcc8SJoe Perches				"Please don't use multiple blank lines\n" . $hereprev) &&
2519d752fcc8SJoe Perches			    $fix) {
2520f2d7e4d4SJoe Perches				fix_delete_line($fixlinenr, $rawline);
2521d752fcc8SJoe Perches			}
2522d752fcc8SJoe Perches
2523365dd4eaSJoe Perches			$last_blank_line = $linenr;
2524365dd4eaSJoe Perches		}
2525365dd4eaSJoe Perches
25263b617e3bSJoe Perches# check for missing blank lines after declarations
25273f7bac03SJoe Perches		if ($sline =~ /^\+\s+\S/ &&			#Not at char 1
25283f7bac03SJoe Perches			# actual declarations
25293f7bac03SJoe Perches		    ($prevline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
25305a4e1fd3SJoe Perches			# function pointer declarations
25315a4e1fd3SJoe Perches		     $prevline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
25323f7bac03SJoe Perches			# foo bar; where foo is some local typedef or #define
25333f7bac03SJoe Perches		     $prevline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
25343f7bac03SJoe Perches			# known declaration macros
25353f7bac03SJoe Perches		     $prevline =~ /^\+\s+$declaration_macros/) &&
25363f7bac03SJoe Perches			# for "else if" which can look like "$Ident $Ident"
25373f7bac03SJoe Perches		    !($prevline =~ /^\+\s+$c90_Keywords\b/ ||
25383f7bac03SJoe Perches			# other possible extensions of declaration lines
25393f7bac03SJoe Perches		      $prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ ||
25403f7bac03SJoe Perches			# not starting a section or a macro "\" extended line
25413f7bac03SJoe Perches		      $prevline =~ /(?:\{\s*|\\)$/) &&
25423f7bac03SJoe Perches			# looks like a declaration
25433f7bac03SJoe Perches		    !($sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
25445a4e1fd3SJoe Perches			# function pointer declarations
25455a4e1fd3SJoe Perches		      $sline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
25463f7bac03SJoe Perches			# foo bar; where foo is some local typedef or #define
25473f7bac03SJoe Perches		      $sline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
25483f7bac03SJoe Perches			# known declaration macros
25493f7bac03SJoe Perches		      $sline =~ /^\+\s+$declaration_macros/ ||
25503f7bac03SJoe Perches			# start of struct or union or enum
25513b617e3bSJoe Perches		      $sline =~ /^\+\s+(?:union|struct|enum|typedef)\b/ ||
25523f7bac03SJoe Perches			# start or end of block or continuation of declaration
25533f7bac03SJoe Perches		      $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ ||
25543f7bac03SJoe Perches			# bitfield continuation
25553f7bac03SJoe Perches		      $sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ ||
25563f7bac03SJoe Perches			# other possible extensions of declaration lines
25573f7bac03SJoe Perches		      $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) &&
25583f7bac03SJoe Perches			# indentation of previous and current line are the same
25593f7bac03SJoe Perches		    (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) {
2560d752fcc8SJoe Perches			if (WARN("LINE_SPACING",
2561d752fcc8SJoe Perches				 "Missing a blank line after declarations\n" . $hereprev) &&
2562d752fcc8SJoe Perches			    $fix) {
2563f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, "\+");
2564d752fcc8SJoe Perches			}
25653b617e3bSJoe Perches		}
25663b617e3bSJoe Perches
25675f7ddae6SRaffaele Recalcati# check for spaces at the beginning of a line.
25686b4c5bebSAndy Whitcroft# Exceptions:
25696b4c5bebSAndy Whitcroft#  1) within comments
25706b4c5bebSAndy Whitcroft#  2) indented preprocessor commands
25716b4c5bebSAndy Whitcroft#  3) hanging labels
25723705ce5bSJoe Perches		if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/)  {
25735f7ddae6SRaffaele Recalcati			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
25743705ce5bSJoe Perches			if (WARN("LEADING_SPACE",
25753705ce5bSJoe Perches				 "please, no spaces at the start of a line\n" . $herevet) &&
25763705ce5bSJoe Perches			    $fix) {
2577194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
25783705ce5bSJoe Perches			}
25795f7ddae6SRaffaele Recalcati		}
25805f7ddae6SRaffaele Recalcati
2581b9ea10d6SAndy Whitcroft# check we are in a valid C source file if not then ignore this hunk
2582b9ea10d6SAndy Whitcroft		next if ($realfile !~ /\.(h|c)$/);
2583b9ea10d6SAndy Whitcroft
2584032a4c0fSJoe Perches# check indentation of any line with a bare else
2585032a4c0fSJoe Perches# if the previous line is a break or return and is indented 1 tab more...
2586032a4c0fSJoe Perches		if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {
2587032a4c0fSJoe Perches			my $tabs = length($1) + 1;
2588032a4c0fSJoe Perches			if ($prevline =~ /^\+\t{$tabs,$tabs}(?:break|return)\b/) {
2589032a4c0fSJoe Perches				WARN("UNNECESSARY_ELSE",
2590032a4c0fSJoe Perches				     "else is not generally useful after a break or return\n" . $hereprev);
2591032a4c0fSJoe Perches			}
2592032a4c0fSJoe Perches		}
2593032a4c0fSJoe Perches
2594c00df19aSJoe Perches# check indentation of a line with a break;
2595c00df19aSJoe Perches# if the previous line is a goto or return and is indented the same # of tabs
2596c00df19aSJoe Perches		if ($sline =~ /^\+([\t]+)break\s*;\s*$/) {
2597c00df19aSJoe Perches			my $tabs = $1;
2598c00df19aSJoe Perches			if ($prevline =~ /^\+$tabs(?:goto|return)\b/) {
2599c00df19aSJoe Perches				WARN("UNNECESSARY_BREAK",
2600c00df19aSJoe Perches				     "break is not useful after a goto or return\n" . $hereprev);
2601c00df19aSJoe Perches			}
2602c00df19aSJoe Perches		}
2603c00df19aSJoe Perches
26041ba8dfd1SKees Cook# discourage the addition of CONFIG_EXPERIMENTAL in #if(def).
26051ba8dfd1SKees Cook		if ($line =~ /^\+\s*\#\s*if.*\bCONFIG_EXPERIMENTAL\b/) {
26061ba8dfd1SKees Cook			WARN("CONFIG_EXPERIMENTAL",
26071ba8dfd1SKees Cook			     "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
26081ba8dfd1SKees Cook		}
26091ba8dfd1SKees Cook
2610c2fdda0dSAndy Whitcroft# check for RCS/CVS revision markers
2611cf655043SAndy Whitcroft		if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
2612000d1cc1SJoe Perches			WARN("CVS_KEYWORD",
2613000d1cc1SJoe Perches			     "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
2614c2fdda0dSAndy Whitcroft		}
261522f2a2efSAndy Whitcroft
261642e41c54SMike Frysinger# Blackfin: don't use __builtin_bfin_[cs]sync
261742e41c54SMike Frysinger		if ($line =~ /__builtin_bfin_csync/) {
261842e41c54SMike Frysinger			my $herevet = "$here\n" . cat_vet($line) . "\n";
2619000d1cc1SJoe Perches			ERROR("CSYNC",
2620000d1cc1SJoe Perches			      "use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
262142e41c54SMike Frysinger		}
262242e41c54SMike Frysinger		if ($line =~ /__builtin_bfin_ssync/) {
262342e41c54SMike Frysinger			my $herevet = "$here\n" . cat_vet($line) . "\n";
2624000d1cc1SJoe Perches			ERROR("SSYNC",
2625000d1cc1SJoe Perches			      "use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
262642e41c54SMike Frysinger		}
262742e41c54SMike Frysinger
262856e77d70SJoe Perches# check for old HOTPLUG __dev<foo> section markings
262956e77d70SJoe Perches		if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
263056e77d70SJoe Perches			WARN("HOTPLUG_SECTION",
263156e77d70SJoe Perches			     "Using $1 is unnecessary\n" . $herecurr);
263256e77d70SJoe Perches		}
263356e77d70SJoe Perches
26349c0ca6f9SAndy Whitcroft# Check for potential 'bare' types
26352b474a1aSAndy Whitcroft		my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
26362b474a1aSAndy Whitcroft		    $realline_next);
26373e469cdcSAndy Whitcroft#print "LINE<$line>\n";
26383e469cdcSAndy Whitcroft		if ($linenr >= $suppress_statement &&
26391b5539b1SJoe Perches		    $realcnt && $sline =~ /.\s*\S/) {
2640170d3a22SAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
2641f5fe35ddSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0);
2642171ae1a4SAndy Whitcroft			$stat =~ s/\n./\n /g;
2643171ae1a4SAndy Whitcroft			$cond =~ s/\n./\n /g;
2644171ae1a4SAndy Whitcroft
26453e469cdcSAndy Whitcroft#print "linenr<$linenr> <$stat>\n";
26463e469cdcSAndy Whitcroft			# If this statement has no statement boundaries within
26473e469cdcSAndy Whitcroft			# it there is no point in retrying a statement scan
26483e469cdcSAndy Whitcroft			# until we hit end of it.
26493e469cdcSAndy Whitcroft			my $frag = $stat; $frag =~ s/;+\s*$//;
26503e469cdcSAndy Whitcroft			if ($frag !~ /(?:{|;)/) {
26513e469cdcSAndy Whitcroft#print "skip<$line_nr_next>\n";
26523e469cdcSAndy Whitcroft				$suppress_statement = $line_nr_next;
26533e469cdcSAndy Whitcroft			}
2654f74bd194SAndy Whitcroft
26552b474a1aSAndy Whitcroft			# Find the real next line.
26562b474a1aSAndy Whitcroft			$realline_next = $line_nr_next;
26572b474a1aSAndy Whitcroft			if (defined $realline_next &&
26582b474a1aSAndy Whitcroft			    (!defined $lines[$realline_next - 1] ||
26592b474a1aSAndy Whitcroft			     substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
26602b474a1aSAndy Whitcroft				$realline_next++;
26612b474a1aSAndy Whitcroft			}
26622b474a1aSAndy Whitcroft
2663171ae1a4SAndy Whitcroft			my $s = $stat;
2664171ae1a4SAndy Whitcroft			$s =~ s/{.*$//s;
2665cf655043SAndy Whitcroft
2666c2fdda0dSAndy Whitcroft			# Ignore goto labels.
2667171ae1a4SAndy Whitcroft			if ($s =~ /$Ident:\*$/s) {
2668c2fdda0dSAndy Whitcroft
2669c2fdda0dSAndy Whitcroft			# Ignore functions being called
2670171ae1a4SAndy Whitcroft			} elsif ($s =~ /^.\s*$Ident\s*\(/s) {
2671c2fdda0dSAndy Whitcroft
2672463f2864SAndy Whitcroft			} elsif ($s =~ /^.\s*else\b/s) {
2673463f2864SAndy Whitcroft
2674c45dcabdSAndy Whitcroft			# declarations always start with types
2675d2506586SAndy 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) {
2676c45dcabdSAndy Whitcroft				my $type = $1;
2677c45dcabdSAndy Whitcroft				$type =~ s/\s+/ /g;
2678c45dcabdSAndy Whitcroft				possible($type, "A:" . $s);
2679c45dcabdSAndy Whitcroft
26806c72ffaaSAndy Whitcroft			# definitions in global scope can only start with types
2681a6a84062SAndy Whitcroft			} elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
2682c45dcabdSAndy Whitcroft				possible($1, "B:" . $s);
2683c2fdda0dSAndy Whitcroft			}
26848905a67cSAndy Whitcroft
26856c72ffaaSAndy Whitcroft			# any (foo ... *) is a pointer cast, and foo is a type
268665863862SAndy Whitcroft			while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
2687c45dcabdSAndy Whitcroft				possible($1, "C:" . $s);
26889c0ca6f9SAndy Whitcroft			}
26898905a67cSAndy Whitcroft
26908905a67cSAndy Whitcroft			# Check for any sort of function declaration.
26918905a67cSAndy Whitcroft			# int foo(something bar, other baz);
26928905a67cSAndy Whitcroft			# void (*store_gdt)(x86_descr_ptr *);
2693171ae1a4SAndy Whitcroft			if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
26948905a67cSAndy Whitcroft				my ($name_len) = length($1);
26958905a67cSAndy Whitcroft
2696cf655043SAndy Whitcroft				my $ctx = $s;
2697773647a0SAndy Whitcroft				substr($ctx, 0, $name_len + 1, '');
26988905a67cSAndy Whitcroft				$ctx =~ s/\)[^\)]*$//;
2699cf655043SAndy Whitcroft
27008905a67cSAndy Whitcroft				for my $arg (split(/\s*,\s*/, $ctx)) {
2701c45dcabdSAndy Whitcroft					if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
27028905a67cSAndy Whitcroft
2703c45dcabdSAndy Whitcroft						possible($1, "D:" . $s);
27048905a67cSAndy Whitcroft					}
27058905a67cSAndy Whitcroft				}
27068905a67cSAndy Whitcroft			}
27078905a67cSAndy Whitcroft
27089c0ca6f9SAndy Whitcroft		}
27099c0ca6f9SAndy Whitcroft
271000df344fSAndy Whitcroft#
271100df344fSAndy Whitcroft# Checks which may be anchored in the context.
271200df344fSAndy Whitcroft#
271300df344fSAndy Whitcroft
271400df344fSAndy Whitcroft# Check for switch () and associated case and default
271500df344fSAndy Whitcroft# statements should be at the same indent.
271600df344fSAndy Whitcroft		if ($line=~/\bswitch\s*\(.*\)/) {
271700df344fSAndy Whitcroft			my $err = '';
271800df344fSAndy Whitcroft			my $sep = '';
271900df344fSAndy Whitcroft			my @ctx = ctx_block_outer($linenr, $realcnt);
272000df344fSAndy Whitcroft			shift(@ctx);
272100df344fSAndy Whitcroft			for my $ctx (@ctx) {
272200df344fSAndy Whitcroft				my ($clen, $cindent) = line_stats($ctx);
272300df344fSAndy Whitcroft				if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
272400df344fSAndy Whitcroft							$indent != $cindent) {
272500df344fSAndy Whitcroft					$err .= "$sep$ctx\n";
272600df344fSAndy Whitcroft					$sep = '';
272700df344fSAndy Whitcroft				} else {
272800df344fSAndy Whitcroft					$sep = "[...]\n";
272900df344fSAndy Whitcroft				}
273000df344fSAndy Whitcroft			}
273100df344fSAndy Whitcroft			if ($err ne '') {
2732000d1cc1SJoe Perches				ERROR("SWITCH_CASE_INDENT_LEVEL",
2733000d1cc1SJoe Perches				      "switch and case should be at the same indent\n$hereline$err");
2734de7d4f0eSAndy Whitcroft			}
2735de7d4f0eSAndy Whitcroft		}
2736de7d4f0eSAndy Whitcroft
2737de7d4f0eSAndy Whitcroft# if/while/etc brace do not go on next line, unless defining a do while loop,
2738de7d4f0eSAndy Whitcroft# or if that brace on the next line is for something else
27390fe3dc2bSJoe Perches		if ($line =~ /(.*)\b((?:if|while|for|switch|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
2740773647a0SAndy Whitcroft			my $pre_ctx = "$1$2";
2741773647a0SAndy Whitcroft
27429c0ca6f9SAndy Whitcroft			my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
27438eef05ddSJoe Perches
27448eef05ddSJoe Perches			if ($line =~ /^\+\t{6,}/) {
27458eef05ddSJoe Perches				WARN("DEEP_INDENTATION",
27468eef05ddSJoe Perches				     "Too many leading tabs - consider code refactoring\n" . $herecurr);
27478eef05ddSJoe Perches			}
27488eef05ddSJoe Perches
2749de7d4f0eSAndy Whitcroft			my $ctx_cnt = $realcnt - $#ctx - 1;
2750de7d4f0eSAndy Whitcroft			my $ctx = join("\n", @ctx);
2751de7d4f0eSAndy Whitcroft
2752548596d5SAndy Whitcroft			my $ctx_ln = $linenr;
2753548596d5SAndy Whitcroft			my $ctx_skip = $realcnt;
2754de7d4f0eSAndy Whitcroft
2755548596d5SAndy Whitcroft			while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
2756548596d5SAndy Whitcroft					defined $lines[$ctx_ln - 1] &&
2757548596d5SAndy Whitcroft					$lines[$ctx_ln - 1] =~ /^-/)) {
2758548596d5SAndy Whitcroft				##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
2759548596d5SAndy Whitcroft				$ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
2760773647a0SAndy Whitcroft				$ctx_ln++;
2761773647a0SAndy Whitcroft			}
2762548596d5SAndy Whitcroft
276353210168SAndy Whitcroft			#print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
276453210168SAndy Whitcroft			#print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
2765773647a0SAndy Whitcroft
2766773647a0SAndy Whitcroft			if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
2767000d1cc1SJoe Perches				ERROR("OPEN_BRACE",
2768000d1cc1SJoe Perches				      "that open brace { should be on the previous line\n" .
276901464f30SAndy Whitcroft					"$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
277000df344fSAndy Whitcroft			}
2771773647a0SAndy Whitcroft			if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
2772773647a0SAndy Whitcroft			    $ctx =~ /\)\s*\;\s*$/ &&
2773773647a0SAndy Whitcroft			    defined $lines[$ctx_ln - 1])
2774773647a0SAndy Whitcroft			{
27759c0ca6f9SAndy Whitcroft				my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
27769c0ca6f9SAndy Whitcroft				if ($nindent > $indent) {
2777000d1cc1SJoe Perches					WARN("TRAILING_SEMICOLON",
2778000d1cc1SJoe Perches					     "trailing semicolon indicates no statements, indent implies otherwise\n" .
277901464f30SAndy Whitcroft						"$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
27809c0ca6f9SAndy Whitcroft				}
27819c0ca6f9SAndy Whitcroft			}
278200df344fSAndy Whitcroft		}
278300df344fSAndy Whitcroft
27844d001e4dSAndy Whitcroft# Check relative indent for conditionals and blocks.
27850fe3dc2bSJoe Perches		if ($line =~ /\b(?:(?:if|while|for|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
27863e469cdcSAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
27873e469cdcSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0)
27883e469cdcSAndy Whitcroft					if (!defined $stat);
27894d001e4dSAndy Whitcroft			my ($s, $c) = ($stat, $cond);
27904d001e4dSAndy Whitcroft
27914d001e4dSAndy Whitcroft			substr($s, 0, length($c), '');
27924d001e4dSAndy Whitcroft
27934d001e4dSAndy Whitcroft			# Make sure we remove the line prefixes as we have
27944d001e4dSAndy Whitcroft			# none on the first line, and are going to readd them
27954d001e4dSAndy Whitcroft			# where necessary.
27964d001e4dSAndy Whitcroft			$s =~ s/\n./\n/gs;
27974d001e4dSAndy Whitcroft
27984d001e4dSAndy Whitcroft			# Find out how long the conditional actually is.
27996f779c18SAndy Whitcroft			my @newlines = ($c =~ /\n/gs);
28006f779c18SAndy Whitcroft			my $cond_lines = 1 + $#newlines;
28014d001e4dSAndy Whitcroft
28024d001e4dSAndy Whitcroft			# We want to check the first line inside the block
28034d001e4dSAndy Whitcroft			# starting at the end of the conditional, so remove:
28044d001e4dSAndy Whitcroft			#  1) any blank line termination
28054d001e4dSAndy Whitcroft			#  2) any opening brace { on end of the line
28064d001e4dSAndy Whitcroft			#  3) any do (...) {
28074d001e4dSAndy Whitcroft			my $continuation = 0;
28084d001e4dSAndy Whitcroft			my $check = 0;
28094d001e4dSAndy Whitcroft			$s =~ s/^.*\bdo\b//;
28104d001e4dSAndy Whitcroft			$s =~ s/^\s*{//;
28114d001e4dSAndy Whitcroft			if ($s =~ s/^\s*\\//) {
28124d001e4dSAndy Whitcroft				$continuation = 1;
28134d001e4dSAndy Whitcroft			}
28149bd49efeSAndy Whitcroft			if ($s =~ s/^\s*?\n//) {
28154d001e4dSAndy Whitcroft				$check = 1;
28164d001e4dSAndy Whitcroft				$cond_lines++;
28174d001e4dSAndy Whitcroft			}
28184d001e4dSAndy Whitcroft
28194d001e4dSAndy Whitcroft			# Also ignore a loop construct at the end of a
28204d001e4dSAndy Whitcroft			# preprocessor statement.
28214d001e4dSAndy Whitcroft			if (($prevline =~ /^.\s*#\s*define\s/ ||
28224d001e4dSAndy Whitcroft			    $prevline =~ /\\\s*$/) && $continuation == 0) {
28234d001e4dSAndy Whitcroft				$check = 0;
28244d001e4dSAndy Whitcroft			}
28254d001e4dSAndy Whitcroft
28269bd49efeSAndy Whitcroft			my $cond_ptr = -1;
2827740504c6SAndy Whitcroft			$continuation = 0;
28289bd49efeSAndy Whitcroft			while ($cond_ptr != $cond_lines) {
28299bd49efeSAndy Whitcroft				$cond_ptr = $cond_lines;
28304d001e4dSAndy Whitcroft
2831f16fa28fSAndy Whitcroft				# If we see an #else/#elif then the code
2832f16fa28fSAndy Whitcroft				# is not linear.
2833f16fa28fSAndy Whitcroft				if ($s =~ /^\s*\#\s*(?:else|elif)/) {
2834f16fa28fSAndy Whitcroft					$check = 0;
2835f16fa28fSAndy Whitcroft				}
2836f16fa28fSAndy Whitcroft
28379bd49efeSAndy Whitcroft				# Ignore:
28389bd49efeSAndy Whitcroft				#  1) blank lines, they should be at 0,
28399bd49efeSAndy Whitcroft				#  2) preprocessor lines, and
28409bd49efeSAndy Whitcroft				#  3) labels.
2841740504c6SAndy Whitcroft				if ($continuation ||
2842740504c6SAndy Whitcroft				    $s =~ /^\s*?\n/ ||
28439bd49efeSAndy Whitcroft				    $s =~ /^\s*#\s*?/ ||
28449bd49efeSAndy Whitcroft				    $s =~ /^\s*$Ident\s*:/) {
2845740504c6SAndy Whitcroft					$continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
284630dad6ebSAndy Whitcroft					if ($s =~ s/^.*?\n//) {
28479bd49efeSAndy Whitcroft						$cond_lines++;
28489bd49efeSAndy Whitcroft					}
28494d001e4dSAndy Whitcroft				}
285030dad6ebSAndy Whitcroft			}
28514d001e4dSAndy Whitcroft
28524d001e4dSAndy Whitcroft			my (undef, $sindent) = line_stats("+" . $s);
28534d001e4dSAndy Whitcroft			my $stat_real = raw_line($linenr, $cond_lines);
28544d001e4dSAndy Whitcroft
28554d001e4dSAndy Whitcroft			# Check if either of these lines are modified, else
28564d001e4dSAndy Whitcroft			# this is not this patch's fault.
28574d001e4dSAndy Whitcroft			if (!defined($stat_real) ||
28584d001e4dSAndy Whitcroft			    $stat !~ /^\+/ && $stat_real !~ /^\+/) {
28594d001e4dSAndy Whitcroft				$check = 0;
28604d001e4dSAndy Whitcroft			}
28614d001e4dSAndy Whitcroft			if (defined($stat_real) && $cond_lines > 1) {
28624d001e4dSAndy Whitcroft				$stat_real = "[...]\n$stat_real";
28634d001e4dSAndy Whitcroft			}
28644d001e4dSAndy Whitcroft
28659bd49efeSAndy 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";
28664d001e4dSAndy Whitcroft
28674d001e4dSAndy Whitcroft			if ($check && (($sindent % 8) != 0 ||
28684d001e4dSAndy Whitcroft			    ($sindent <= $indent && $s ne ''))) {
2869000d1cc1SJoe Perches				WARN("SUSPECT_CODE_INDENT",
2870000d1cc1SJoe Perches				     "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
28714d001e4dSAndy Whitcroft			}
28724d001e4dSAndy Whitcroft		}
28734d001e4dSAndy Whitcroft
28746c72ffaaSAndy Whitcroft		# Track the 'values' across context and added lines.
28756c72ffaaSAndy Whitcroft		my $opline = $line; $opline =~ s/^./ /;
28761f65f947SAndy Whitcroft		my ($curr_values, $curr_vars) =
28771f65f947SAndy Whitcroft				annotate_values($opline . "\n", $prev_values);
28786c72ffaaSAndy Whitcroft		$curr_values = $prev_values . $curr_values;
2879c2fdda0dSAndy Whitcroft		if ($dbg_values) {
2880c2fdda0dSAndy Whitcroft			my $outline = $opline; $outline =~ s/\t/ /g;
2881cf655043SAndy Whitcroft			print "$linenr > .$outline\n";
2882cf655043SAndy Whitcroft			print "$linenr > $curr_values\n";
28831f65f947SAndy Whitcroft			print "$linenr >  $curr_vars\n";
2884c2fdda0dSAndy Whitcroft		}
28856c72ffaaSAndy Whitcroft		$prev_values = substr($curr_values, -1);
28866c72ffaaSAndy Whitcroft
288700df344fSAndy Whitcroft#ignore lines not being added
28883705ce5bSJoe Perches		next if ($line =~ /^[^\+]/);
288900df344fSAndy Whitcroft
2890653d4876SAndy Whitcroft# TEST: allow direct testing of the type matcher.
28917429c690SAndy Whitcroft		if ($dbg_type) {
28927429c690SAndy Whitcroft			if ($line =~ /^.\s*$Declare\s*$/) {
2893000d1cc1SJoe Perches				ERROR("TEST_TYPE",
2894000d1cc1SJoe Perches				      "TEST: is type\n" . $herecurr);
28957429c690SAndy Whitcroft			} elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
2896000d1cc1SJoe Perches				ERROR("TEST_NOT_TYPE",
2897000d1cc1SJoe Perches				      "TEST: is not type ($1 is)\n". $herecurr);
28987429c690SAndy Whitcroft			}
2899653d4876SAndy Whitcroft			next;
2900653d4876SAndy Whitcroft		}
2901a1ef277eSAndy Whitcroft# TEST: allow direct testing of the attribute matcher.
2902a1ef277eSAndy Whitcroft		if ($dbg_attr) {
29039360b0e5SAndy Whitcroft			if ($line =~ /^.\s*$Modifier\s*$/) {
2904000d1cc1SJoe Perches				ERROR("TEST_ATTR",
2905000d1cc1SJoe Perches				      "TEST: is attr\n" . $herecurr);
29069360b0e5SAndy Whitcroft			} elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
2907000d1cc1SJoe Perches				ERROR("TEST_NOT_ATTR",
2908000d1cc1SJoe Perches				      "TEST: is not attr ($1 is)\n". $herecurr);
2909a1ef277eSAndy Whitcroft			}
2910a1ef277eSAndy Whitcroft			next;
2911a1ef277eSAndy Whitcroft		}
2912653d4876SAndy Whitcroft
2913f0a594c1SAndy Whitcroft# check for initialisation to aggregates open brace on the next line
291499423c20SAndy Whitcroft		if ($line =~ /^.\s*{/ &&
291599423c20SAndy Whitcroft		    $prevline =~ /(?:^|[^=])=\s*$/) {
2916d752fcc8SJoe Perches			if (ERROR("OPEN_BRACE",
2917d752fcc8SJoe Perches				  "that open brace { should be on the previous line\n" . $hereprev) &&
2918f2d7e4d4SJoe Perches			    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
2919f2d7e4d4SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
2920f2d7e4d4SJoe Perches				fix_delete_line($fixlinenr, $rawline);
2921d752fcc8SJoe Perches				my $fixedline = $prevrawline;
2922d752fcc8SJoe Perches				$fixedline =~ s/\s*=\s*$/ = {/;
2923f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
2924d752fcc8SJoe Perches				$fixedline = $line;
2925d752fcc8SJoe Perches				$fixedline =~ s/^(.\s*){\s*/$1/;
2926f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
2927d752fcc8SJoe Perches			}
2928f0a594c1SAndy Whitcroft		}
2929f0a594c1SAndy Whitcroft
293000df344fSAndy Whitcroft#
293100df344fSAndy Whitcroft# Checks which are anchored on the added line.
293200df344fSAndy Whitcroft#
293300df344fSAndy Whitcroft
2934653d4876SAndy Whitcroft# check for malformed paths in #include statements (uses RAW line)
2935c45dcabdSAndy Whitcroft		if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
2936653d4876SAndy Whitcroft			my $path = $1;
2937653d4876SAndy Whitcroft			if ($path =~ m{//}) {
2938000d1cc1SJoe Perches				ERROR("MALFORMED_INCLUDE",
2939495e9d84SJoe Perches				      "malformed #include filename\n" . $herecurr);
2940495e9d84SJoe Perches			}
2941495e9d84SJoe Perches			if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
2942495e9d84SJoe Perches				ERROR("UAPI_INCLUDE",
2943495e9d84SJoe Perches				      "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
2944653d4876SAndy Whitcroft			}
2945653d4876SAndy Whitcroft		}
2946653d4876SAndy Whitcroft
294700df344fSAndy Whitcroft# no C99 // comments
294800df344fSAndy Whitcroft		if ($line =~ m{//}) {
29493705ce5bSJoe Perches			if (ERROR("C99_COMMENTS",
29503705ce5bSJoe Perches				  "do not use C99 // comments\n" . $herecurr) &&
29513705ce5bSJoe Perches			    $fix) {
2952194f66fcSJoe Perches				my $line = $fixed[$fixlinenr];
29533705ce5bSJoe Perches				if ($line =~ /\/\/(.*)$/) {
29543705ce5bSJoe Perches					my $comment = trim($1);
2955194f66fcSJoe Perches					$fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@;
29563705ce5bSJoe Perches				}
29573705ce5bSJoe Perches			}
295800df344fSAndy Whitcroft		}
295900df344fSAndy Whitcroft		# Remove C99 comments.
29600a920b5bSAndy Whitcroft		$line =~ s@//.*@@;
29616c72ffaaSAndy Whitcroft		$opline =~ s@//.*@@;
29620a920b5bSAndy Whitcroft
29632b474a1aSAndy Whitcroft# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
29642b474a1aSAndy Whitcroft# the whole statement.
29652b474a1aSAndy Whitcroft#print "APW <$lines[$realline_next - 1]>\n";
29662b474a1aSAndy Whitcroft		if (defined $realline_next &&
29672b474a1aSAndy Whitcroft		    exists $lines[$realline_next - 1] &&
29682b474a1aSAndy Whitcroft		    !defined $suppress_export{$realline_next} &&
29692b474a1aSAndy Whitcroft		    ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
29702b474a1aSAndy Whitcroft		     $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
29713cbf62dfSAndy Whitcroft			# Handle definitions which produce identifiers with
29723cbf62dfSAndy Whitcroft			# a prefix:
29733cbf62dfSAndy Whitcroft			#   XXX(foo);
29743cbf62dfSAndy Whitcroft			#   EXPORT_SYMBOL(something_foo);
2975653d4876SAndy Whitcroft			my $name = $1;
297687a53877SAndy Whitcroft			if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
29773cbf62dfSAndy Whitcroft			    $name =~ /^${Ident}_$2/) {
29783cbf62dfSAndy Whitcroft#print "FOO C name<$name>\n";
29793cbf62dfSAndy Whitcroft				$suppress_export{$realline_next} = 1;
29803cbf62dfSAndy Whitcroft
29813cbf62dfSAndy Whitcroft			} elsif ($stat !~ /(?:
29822b474a1aSAndy Whitcroft				\n.}\s*$|
298348012058SAndy Whitcroft				^.DEFINE_$Ident\(\Q$name\E\)|
298448012058SAndy Whitcroft				^.DECLARE_$Ident\(\Q$name\E\)|
298548012058SAndy Whitcroft				^.LIST_HEAD\(\Q$name\E\)|
29862b474a1aSAndy Whitcroft				^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
29872b474a1aSAndy Whitcroft				\b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
298848012058SAndy Whitcroft			    )/x) {
29892b474a1aSAndy Whitcroft#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
29902b474a1aSAndy Whitcroft				$suppress_export{$realline_next} = 2;
29912b474a1aSAndy Whitcroft			} else {
29922b474a1aSAndy Whitcroft				$suppress_export{$realline_next} = 1;
29930a920b5bSAndy Whitcroft			}
29940a920b5bSAndy Whitcroft		}
29952b474a1aSAndy Whitcroft		if (!defined $suppress_export{$linenr} &&
29962b474a1aSAndy Whitcroft		    $prevline =~ /^.\s*$/ &&
29972b474a1aSAndy Whitcroft		    ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
29982b474a1aSAndy Whitcroft		     $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
29992b474a1aSAndy Whitcroft#print "FOO B <$lines[$linenr - 1]>\n";
30002b474a1aSAndy Whitcroft			$suppress_export{$linenr} = 2;
30012b474a1aSAndy Whitcroft		}
30022b474a1aSAndy Whitcroft		if (defined $suppress_export{$linenr} &&
30032b474a1aSAndy Whitcroft		    $suppress_export{$linenr} == 2) {
3004000d1cc1SJoe Perches			WARN("EXPORT_SYMBOL",
3005000d1cc1SJoe Perches			     "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
30062b474a1aSAndy Whitcroft		}
30070a920b5bSAndy Whitcroft
30085150bda4SJoe Eloff# check for global initialisers.
3009d5e616fcSJoe Perches		if ($line =~ /^\+(\s*$Type\s*$Ident\s*(?:\s+$Modifier))*\s*=\s*(0|NULL|false)\s*;/) {
3010d5e616fcSJoe Perches			if (ERROR("GLOBAL_INITIALISERS",
3011000d1cc1SJoe Perches				  "do not initialise globals to 0 or NULL\n" .
3012d5e616fcSJoe Perches				      $herecurr) &&
3013d5e616fcSJoe Perches			    $fix) {
3014194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/($Type\s*$Ident\s*(?:\s+$Modifier))*\s*=\s*(0|NULL|false)\s*;/$1;/;
3015d5e616fcSJoe Perches			}
3016f0a594c1SAndy Whitcroft		}
30170a920b5bSAndy Whitcroft# check for static initialisers.
3018d5e616fcSJoe Perches		if ($line =~ /^\+.*\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
3019d5e616fcSJoe Perches			if (ERROR("INITIALISED_STATIC",
3020000d1cc1SJoe Perches				  "do not initialise statics to 0 or NULL\n" .
3021d5e616fcSJoe Perches				      $herecurr) &&
3022d5e616fcSJoe Perches			    $fix) {
3023194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*(0|NULL|false)\s*;/$1;/;
3024d5e616fcSJoe Perches			}
30250a920b5bSAndy Whitcroft		}
30260a920b5bSAndy Whitcroft
3027*1813087dSJoe Perches# check for misordered declarations of char/short/int/long with signed/unsigned
3028*1813087dSJoe Perches		while ($sline =~ m{(\b$TypeMisordered\b)}g) {
3029*1813087dSJoe Perches			my $tmp = trim($1);
3030*1813087dSJoe Perches			WARN("MISORDERED_TYPE",
3031*1813087dSJoe Perches			     "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr);
3032*1813087dSJoe Perches		}
3033*1813087dSJoe Perches
3034cb710ecaSJoe Perches# check for static const char * arrays.
3035cb710ecaSJoe Perches		if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
3036000d1cc1SJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
3037000d1cc1SJoe Perches			     "static const char * array should probably be static const char * const\n" .
3038cb710ecaSJoe Perches				$herecurr);
3039cb710ecaSJoe Perches               }
3040cb710ecaSJoe Perches
3041cb710ecaSJoe Perches# check for static char foo[] = "bar" declarations.
3042cb710ecaSJoe Perches		if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
3043000d1cc1SJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
3044000d1cc1SJoe Perches			     "static char array declaration should probably be static const char\n" .
3045cb710ecaSJoe Perches				$herecurr);
3046cb710ecaSJoe Perches               }
3047cb710ecaSJoe Perches
30489b0fa60dSJoe Perches# check for non-global char *foo[] = {"bar", ...} declarations.
30499b0fa60dSJoe Perches		if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) {
30509b0fa60dSJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
30519b0fa60dSJoe Perches			     "char * array declaration might be better as static const\n" .
30529b0fa60dSJoe Perches				$herecurr);
30539b0fa60dSJoe Perches               }
30549b0fa60dSJoe Perches
3055b36190c5SJoe Perches# check for function declarations without arguments like "int foo()"
3056b36190c5SJoe Perches		if ($line =~ /(\b$Type\s+$Ident)\s*\(\s*\)/) {
3057b36190c5SJoe Perches			if (ERROR("FUNCTION_WITHOUT_ARGS",
3058b36190c5SJoe Perches				  "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) &&
3059b36190c5SJoe Perches			    $fix) {
3060194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/;
3061b36190c5SJoe Perches			}
3062b36190c5SJoe Perches		}
3063b36190c5SJoe Perches
306492e112fdSJoe Perches# check for uses of DEFINE_PCI_DEVICE_TABLE
306592e112fdSJoe Perches		if ($line =~ /\bDEFINE_PCI_DEVICE_TABLE\s*\(\s*(\w+)\s*\)\s*=/) {
306692e112fdSJoe Perches			if (WARN("DEFINE_PCI_DEVICE_TABLE",
306792e112fdSJoe Perches				 "Prefer struct pci_device_id over deprecated DEFINE_PCI_DEVICE_TABLE\n" . $herecurr) &&
306892e112fdSJoe Perches			    $fix) {
3069194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b(?:static\s+|)DEFINE_PCI_DEVICE_TABLE\s*\(\s*(\w+)\s*\)\s*=\s*/static const struct pci_device_id $1\[\] = /;
307092e112fdSJoe Perches			}
307193ed0e2dSJoe Perches		}
307293ed0e2dSJoe Perches
3073653d4876SAndy Whitcroft# check for new typedefs, only function parameters and sparse annotations
3074653d4876SAndy Whitcroft# make sense.
3075653d4876SAndy Whitcroft		if ($line =~ /\btypedef\s/ &&
30768054576dSAndy Whitcroft		    $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
3077c45dcabdSAndy Whitcroft		    $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
30788ed22cadSAndy Whitcroft		    $line !~ /\b$typeTypedefs\b/ &&
3079653d4876SAndy Whitcroft		    $line !~ /\b__bitwise(?:__|)\b/) {
3080000d1cc1SJoe Perches			WARN("NEW_TYPEDEFS",
3081000d1cc1SJoe Perches			     "do not add new typedefs\n" . $herecurr);
30820a920b5bSAndy Whitcroft		}
30830a920b5bSAndy Whitcroft
30840a920b5bSAndy Whitcroft# * goes on variable not on type
308565863862SAndy Whitcroft		# (char*[ const])
3086bfcb2cc7SAndy Whitcroft		while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
3087bfcb2cc7SAndy Whitcroft			#print "AA<$1>\n";
30883705ce5bSJoe Perches			my ($ident, $from, $to) = ($1, $2, $2);
3089d8aaf121SAndy Whitcroft
309065863862SAndy Whitcroft			# Should start with a space.
309165863862SAndy Whitcroft			$to =~ s/^(\S)/ $1/;
309265863862SAndy Whitcroft			# Should not end with a space.
309365863862SAndy Whitcroft			$to =~ s/\s+$//;
309465863862SAndy Whitcroft			# '*'s should not have spaces between.
3095f9a0b3d1SAndy Whitcroft			while ($to =~ s/\*\s+\*/\*\*/) {
309665863862SAndy Whitcroft			}
3097d8aaf121SAndy Whitcroft
30983705ce5bSJoe Perches##			print "1: from<$from> to<$to> ident<$ident>\n";
309965863862SAndy Whitcroft			if ($from ne $to) {
31003705ce5bSJoe Perches				if (ERROR("POINTER_LOCATION",
31013705ce5bSJoe Perches					  "\"(foo$from)\" should be \"(foo$to)\"\n" .  $herecurr) &&
31023705ce5bSJoe Perches				    $fix) {
31033705ce5bSJoe Perches					my $sub_from = $ident;
31043705ce5bSJoe Perches					my $sub_to = $ident;
31053705ce5bSJoe Perches					$sub_to =~ s/\Q$from\E/$to/;
3106194f66fcSJoe Perches					$fixed[$fixlinenr] =~
31073705ce5bSJoe Perches					    s@\Q$sub_from\E@$sub_to@;
31083705ce5bSJoe Perches				}
310965863862SAndy Whitcroft			}
3110bfcb2cc7SAndy Whitcroft		}
3111bfcb2cc7SAndy Whitcroft		while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
3112bfcb2cc7SAndy Whitcroft			#print "BB<$1>\n";
31133705ce5bSJoe Perches			my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
3114d8aaf121SAndy Whitcroft
311565863862SAndy Whitcroft			# Should start with a space.
311665863862SAndy Whitcroft			$to =~ s/^(\S)/ $1/;
311765863862SAndy Whitcroft			# Should not end with a space.
311865863862SAndy Whitcroft			$to =~ s/\s+$//;
311965863862SAndy Whitcroft			# '*'s should not have spaces between.
3120f9a0b3d1SAndy Whitcroft			while ($to =~ s/\*\s+\*/\*\*/) {
312165863862SAndy Whitcroft			}
312265863862SAndy Whitcroft			# Modifiers should have spaces.
312365863862SAndy Whitcroft			$to =~ s/(\b$Modifier$)/$1 /;
312465863862SAndy Whitcroft
31253705ce5bSJoe Perches##			print "2: from<$from> to<$to> ident<$ident>\n";
3126667026e7SAndy Whitcroft			if ($from ne $to && $ident !~ /^$Modifier$/) {
31273705ce5bSJoe Perches				if (ERROR("POINTER_LOCATION",
31283705ce5bSJoe Perches					  "\"foo${from}bar\" should be \"foo${to}bar\"\n" .  $herecurr) &&
31293705ce5bSJoe Perches				    $fix) {
31303705ce5bSJoe Perches
31313705ce5bSJoe Perches					my $sub_from = $match;
31323705ce5bSJoe Perches					my $sub_to = $match;
31333705ce5bSJoe Perches					$sub_to =~ s/\Q$from\E/$to/;
3134194f66fcSJoe Perches					$fixed[$fixlinenr] =~
31353705ce5bSJoe Perches					    s@\Q$sub_from\E@$sub_to@;
31363705ce5bSJoe Perches				}
313765863862SAndy Whitcroft			}
31380a920b5bSAndy Whitcroft		}
31390a920b5bSAndy Whitcroft
31400a920b5bSAndy Whitcroft# # no BUG() or BUG_ON()
31410a920b5bSAndy Whitcroft# 		if ($line =~ /\b(BUG|BUG_ON)\b/) {
31420a920b5bSAndy Whitcroft# 			print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
31430a920b5bSAndy Whitcroft# 			print "$herecurr";
31440a920b5bSAndy Whitcroft# 			$clean = 0;
31450a920b5bSAndy Whitcroft# 		}
31460a920b5bSAndy Whitcroft
31478905a67cSAndy Whitcroft		if ($line =~ /\bLINUX_VERSION_CODE\b/) {
3148000d1cc1SJoe Perches			WARN("LINUX_VERSION_CODE",
3149000d1cc1SJoe Perches			     "LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
31508905a67cSAndy Whitcroft		}
31518905a67cSAndy Whitcroft
315217441227SJoe Perches# check for uses of printk_ratelimit
315317441227SJoe Perches		if ($line =~ /\bprintk_ratelimit\s*\(/) {
3154000d1cc1SJoe Perches			WARN("PRINTK_RATELIMITED",
3155000d1cc1SJoe Perches"Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
315617441227SJoe Perches		}
315717441227SJoe Perches
315800df344fSAndy Whitcroft# printk should use KERN_* levels.  Note that follow on printk's on the
315900df344fSAndy Whitcroft# same line do not need a level, so we use the current block context
316000df344fSAndy Whitcroft# to try and find and validate the current printk.  In summary the current
316125985edcSLucas De Marchi# printk includes all preceding printk's which have no newline on the end.
316200df344fSAndy Whitcroft# we assume the first bad printk is the one to report.
3163f0a594c1SAndy Whitcroft		if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
316400df344fSAndy Whitcroft			my $ok = 0;
316500df344fSAndy Whitcroft			for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
316600df344fSAndy Whitcroft				#print "CHECK<$lines[$ln - 1]\n";
316725985edcSLucas De Marchi				# we have a preceding printk if it ends
316800df344fSAndy Whitcroft				# with "\n" ignore it, else it is to blame
316900df344fSAndy Whitcroft				if ($lines[$ln - 1] =~ m{\bprintk\(}) {
317000df344fSAndy Whitcroft					if ($rawlines[$ln - 1] !~ m{\\n"}) {
317100df344fSAndy Whitcroft						$ok = 1;
317200df344fSAndy Whitcroft					}
317300df344fSAndy Whitcroft					last;
317400df344fSAndy Whitcroft				}
317500df344fSAndy Whitcroft			}
317600df344fSAndy Whitcroft			if ($ok == 0) {
3177000d1cc1SJoe Perches				WARN("PRINTK_WITHOUT_KERN_LEVEL",
3178000d1cc1SJoe Perches				     "printk() should include KERN_ facility level\n" . $herecurr);
31790a920b5bSAndy Whitcroft			}
318000df344fSAndy Whitcroft		}
31810a920b5bSAndy Whitcroft
3182243f3803SJoe Perches		if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
3183243f3803SJoe Perches			my $orig = $1;
3184243f3803SJoe Perches			my $level = lc($orig);
3185243f3803SJoe Perches			$level = "warn" if ($level eq "warning");
31868f26b837SJoe Perches			my $level2 = $level;
31878f26b837SJoe Perches			$level2 = "dbg" if ($level eq "debug");
3188243f3803SJoe Perches			WARN("PREFER_PR_LEVEL",
3189daa8b059SYogesh Chaudhari			     "Prefer [subsystem eg: netdev]_$level2([subsystem]dev, ... then dev_$level2(dev, ... then pr_$level(...  to printk(KERN_$orig ...\n" . $herecurr);
3190243f3803SJoe Perches		}
3191243f3803SJoe Perches
3192243f3803SJoe Perches		if ($line =~ /\bpr_warning\s*\(/) {
3193d5e616fcSJoe Perches			if (WARN("PREFER_PR_LEVEL",
3194d5e616fcSJoe Perches				 "Prefer pr_warn(... to pr_warning(...\n" . $herecurr) &&
3195d5e616fcSJoe Perches			    $fix) {
3196194f66fcSJoe Perches				$fixed[$fixlinenr] =~
3197d5e616fcSJoe Perches				    s/\bpr_warning\b/pr_warn/;
3198d5e616fcSJoe Perches			}
3199243f3803SJoe Perches		}
3200243f3803SJoe Perches
3201dc139313SJoe Perches		if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
3202dc139313SJoe Perches			my $orig = $1;
3203dc139313SJoe Perches			my $level = lc($orig);
3204dc139313SJoe Perches			$level = "warn" if ($level eq "warning");
3205dc139313SJoe Perches			$level = "dbg" if ($level eq "debug");
3206dc139313SJoe Perches			WARN("PREFER_DEV_LEVEL",
3207dc139313SJoe Perches			     "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
3208dc139313SJoe Perches		}
3209dc139313SJoe Perches
3210653d4876SAndy Whitcroft# function brace can't be on same line, except for #defines of do while,
3211653d4876SAndy Whitcroft# or if closed on same line
32128d182478SJoe Perches		if (($line=~/$Type\s*$Ident\(.*\).*\s*{/) and
3213c45dcabdSAndy Whitcroft		    !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) {
32148d182478SJoe Perches			if (ERROR("OPEN_BRACE",
32158d182478SJoe Perches				  "open brace '{' following function declarations go on the next line\n" . $herecurr) &&
32168d182478SJoe Perches			    $fix) {
32178d182478SJoe Perches				fix_delete_line($fixlinenr, $rawline);
32188d182478SJoe Perches				my $fixed_line = $rawline;
32198d182478SJoe Perches				$fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*){(.*)$/;
32208d182478SJoe Perches				my $line1 = $1;
32218d182478SJoe Perches				my $line2 = $2;
32228d182478SJoe Perches				fix_insert_line($fixlinenr, ltrim($line1));
32238d182478SJoe Perches				fix_insert_line($fixlinenr, "\+{");
32248d182478SJoe Perches				if ($line2 !~ /^\s*$/) {
32258d182478SJoe Perches					fix_insert_line($fixlinenr, "\+\t" . trim($line2));
32268d182478SJoe Perches				}
32278d182478SJoe Perches			}
32280a920b5bSAndy Whitcroft		}
3229653d4876SAndy Whitcroft
32308905a67cSAndy Whitcroft# open braces for enum, union and struct go on the same line.
32318905a67cSAndy Whitcroft		if ($line =~ /^.\s*{/ &&
32328905a67cSAndy Whitcroft		    $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
32338d182478SJoe Perches			if (ERROR("OPEN_BRACE",
32348d182478SJoe Perches				  "open brace '{' following $1 go on the same line\n" . $hereprev) &&
32358d182478SJoe Perches			    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
32368d182478SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
32378d182478SJoe Perches				fix_delete_line($fixlinenr, $rawline);
32388d182478SJoe Perches				my $fixedline = rtrim($prevrawline) . " {";
32398d182478SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
32408d182478SJoe Perches				$fixedline = $rawline;
32418d182478SJoe Perches				$fixedline =~ s/^(.\s*){\s*/$1\t/;
32428d182478SJoe Perches				if ($fixedline !~ /^\+\s*$/) {
32438d182478SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
32448d182478SJoe Perches				}
32458d182478SJoe Perches			}
32468905a67cSAndy Whitcroft		}
32478905a67cSAndy Whitcroft
32480c73b4ebSAndy Whitcroft# missing space after union, struct or enum definition
32493705ce5bSJoe Perches		if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
32503705ce5bSJoe Perches			if (WARN("SPACING",
32513705ce5bSJoe Perches				 "missing space after $1 definition\n" . $herecurr) &&
32523705ce5bSJoe Perches			    $fix) {
3253194f66fcSJoe Perches				$fixed[$fixlinenr] =~
32543705ce5bSJoe Perches				    s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
32553705ce5bSJoe Perches			}
32560c73b4ebSAndy Whitcroft		}
32570c73b4ebSAndy Whitcroft
325831070b5dSJoe Perches# Function pointer declarations
325931070b5dSJoe Perches# check spacing between type, funcptr, and args
326031070b5dSJoe Perches# canonical declaration is "type (*funcptr)(args...)"
326191f72e9cSJoe Perches		if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) {
326231070b5dSJoe Perches			my $declare = $1;
326331070b5dSJoe Perches			my $pre_pointer_space = $2;
326431070b5dSJoe Perches			my $post_pointer_space = $3;
326531070b5dSJoe Perches			my $funcname = $4;
326631070b5dSJoe Perches			my $post_funcname_space = $5;
326731070b5dSJoe Perches			my $pre_args_space = $6;
326831070b5dSJoe Perches
326991f72e9cSJoe Perches# the $Declare variable will capture all spaces after the type
327091f72e9cSJoe Perches# so check it for a missing trailing missing space but pointer return types
327191f72e9cSJoe Perches# don't need a space so don't warn for those.
327291f72e9cSJoe Perches			my $post_declare_space = "";
327391f72e9cSJoe Perches			if ($declare =~ /(\s+)$/) {
327491f72e9cSJoe Perches				$post_declare_space = $1;
327591f72e9cSJoe Perches				$declare = rtrim($declare);
327691f72e9cSJoe Perches			}
327791f72e9cSJoe Perches			if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) {
327831070b5dSJoe Perches				WARN("SPACING",
327931070b5dSJoe Perches				     "missing space after return type\n" . $herecurr);
328091f72e9cSJoe Perches				$post_declare_space = " ";
328131070b5dSJoe Perches			}
328231070b5dSJoe Perches
328331070b5dSJoe Perches# unnecessary space "type  (*funcptr)(args...)"
328491f72e9cSJoe Perches# This test is not currently implemented because these declarations are
328591f72e9cSJoe Perches# equivalent to
328691f72e9cSJoe Perches#	int  foo(int bar, ...)
328791f72e9cSJoe Perches# and this is form shouldn't/doesn't generate a checkpatch warning.
328891f72e9cSJoe Perches#
328991f72e9cSJoe Perches#			elsif ($declare =~ /\s{2,}$/) {
329091f72e9cSJoe Perches#				WARN("SPACING",
329191f72e9cSJoe Perches#				     "Multiple spaces after return type\n" . $herecurr);
329291f72e9cSJoe Perches#			}
329331070b5dSJoe Perches
329431070b5dSJoe Perches# unnecessary space "type ( *funcptr)(args...)"
329531070b5dSJoe Perches			if (defined $pre_pointer_space &&
329631070b5dSJoe Perches			    $pre_pointer_space =~ /^\s/) {
329731070b5dSJoe Perches				WARN("SPACING",
329831070b5dSJoe Perches				     "Unnecessary space after function pointer open parenthesis\n" . $herecurr);
329931070b5dSJoe Perches			}
330031070b5dSJoe Perches
330131070b5dSJoe Perches# unnecessary space "type (* funcptr)(args...)"
330231070b5dSJoe Perches			if (defined $post_pointer_space &&
330331070b5dSJoe Perches			    $post_pointer_space =~ /^\s/) {
330431070b5dSJoe Perches				WARN("SPACING",
330531070b5dSJoe Perches				     "Unnecessary space before function pointer name\n" . $herecurr);
330631070b5dSJoe Perches			}
330731070b5dSJoe Perches
330831070b5dSJoe Perches# unnecessary space "type (*funcptr )(args...)"
330931070b5dSJoe Perches			if (defined $post_funcname_space &&
331031070b5dSJoe Perches			    $post_funcname_space =~ /^\s/) {
331131070b5dSJoe Perches				WARN("SPACING",
331231070b5dSJoe Perches				     "Unnecessary space after function pointer name\n" . $herecurr);
331331070b5dSJoe Perches			}
331431070b5dSJoe Perches
331531070b5dSJoe Perches# unnecessary space "type (*funcptr) (args...)"
331631070b5dSJoe Perches			if (defined $pre_args_space &&
331731070b5dSJoe Perches			    $pre_args_space =~ /^\s/) {
331831070b5dSJoe Perches				WARN("SPACING",
331931070b5dSJoe Perches				     "Unnecessary space before function pointer arguments\n" . $herecurr);
332031070b5dSJoe Perches			}
332131070b5dSJoe Perches
332231070b5dSJoe Perches			if (show_type("SPACING") && $fix) {
3323194f66fcSJoe Perches				$fixed[$fixlinenr] =~
332491f72e9cSJoe Perches				    s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex;
332531070b5dSJoe Perches			}
332631070b5dSJoe Perches		}
332731070b5dSJoe Perches
33288d31cfceSAndy Whitcroft# check for spacing round square brackets; allowed:
33298d31cfceSAndy Whitcroft#  1. with a type on the left -- int [] a;
3330fe2a7dbcSAndy Whitcroft#  2. at the beginning of a line for slice initialisers -- [0...10] = 5,
3331fe2a7dbcSAndy Whitcroft#  3. inside a curly brace -- = { [0...10] = 5 }
33328d31cfceSAndy Whitcroft		while ($line =~ /(.*?\s)\[/g) {
33338d31cfceSAndy Whitcroft			my ($where, $prefix) = ($-[1], $1);
33348d31cfceSAndy Whitcroft			if ($prefix !~ /$Type\s+$/ &&
3335fe2a7dbcSAndy Whitcroft			    ($where != 0 || $prefix !~ /^.\s+$/) &&
3336daebc534SAndy Whitcroft			    $prefix !~ /[{,]\s+$/) {
33373705ce5bSJoe Perches				if (ERROR("BRACKET_SPACE",
33383705ce5bSJoe Perches					  "space prohibited before open square bracket '['\n" . $herecurr) &&
33393705ce5bSJoe Perches				    $fix) {
3340194f66fcSJoe Perches				    $fixed[$fixlinenr] =~
33413705ce5bSJoe Perches					s/^(\+.*?)\s+\[/$1\[/;
33423705ce5bSJoe Perches				}
33438d31cfceSAndy Whitcroft			}
33448d31cfceSAndy Whitcroft		}
33458d31cfceSAndy Whitcroft
3346f0a594c1SAndy Whitcroft# check for spaces between functions and their parentheses.
33476c72ffaaSAndy Whitcroft		while ($line =~ /($Ident)\s+\(/g) {
3348c2fdda0dSAndy Whitcroft			my $name = $1;
3349773647a0SAndy Whitcroft			my $ctx_before = substr($line, 0, $-[1]);
3350773647a0SAndy Whitcroft			my $ctx = "$ctx_before$name";
3351c2fdda0dSAndy Whitcroft
3352c2fdda0dSAndy Whitcroft			# Ignore those directives where spaces _are_ permitted.
3353773647a0SAndy Whitcroft			if ($name =~ /^(?:
3354773647a0SAndy Whitcroft				if|for|while|switch|return|case|
3355773647a0SAndy Whitcroft				volatile|__volatile__|
3356773647a0SAndy Whitcroft				__attribute__|format|__extension__|
3357773647a0SAndy Whitcroft				asm|__asm__)$/x)
3358773647a0SAndy Whitcroft			{
3359c2fdda0dSAndy Whitcroft			# cpp #define statements have non-optional spaces, ie
3360c2fdda0dSAndy Whitcroft			# if there is a space between the name and the open
3361c2fdda0dSAndy Whitcroft			# parenthesis it is simply not a parameter group.
3362c45dcabdSAndy Whitcroft			} elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
3363773647a0SAndy Whitcroft
3364773647a0SAndy Whitcroft			# cpp #elif statement condition may start with a (
3365c45dcabdSAndy Whitcroft			} elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
3366c2fdda0dSAndy Whitcroft
3367c2fdda0dSAndy Whitcroft			# If this whole things ends with a type its most
3368c2fdda0dSAndy Whitcroft			# likely a typedef for a function.
3369773647a0SAndy Whitcroft			} elsif ($ctx =~ /$Type$/) {
3370c2fdda0dSAndy Whitcroft
3371c2fdda0dSAndy Whitcroft			} else {
33723705ce5bSJoe Perches				if (WARN("SPACING",
33733705ce5bSJoe Perches					 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
33743705ce5bSJoe Perches					     $fix) {
3375194f66fcSJoe Perches					$fixed[$fixlinenr] =~
33763705ce5bSJoe Perches					    s/\b$name\s+\(/$name\(/;
33773705ce5bSJoe Perches				}
3378f0a594c1SAndy Whitcroft			}
33796c72ffaaSAndy Whitcroft		}
33809a4cad4eSEric Nelson
3381653d4876SAndy Whitcroft# Check operator spacing.
33820a920b5bSAndy Whitcroft		if (!($line=~/\#\s*include/)) {
33833705ce5bSJoe Perches			my $fixed_line = "";
33843705ce5bSJoe Perches			my $line_fixed = 0;
33853705ce5bSJoe Perches
33869c0ca6f9SAndy Whitcroft			my $ops = qr{
33879c0ca6f9SAndy Whitcroft				<<=|>>=|<=|>=|==|!=|
33889c0ca6f9SAndy Whitcroft				\+=|-=|\*=|\/=|%=|\^=|\|=|&=|
33899c0ca6f9SAndy Whitcroft				=>|->|<<|>>|<|>|=|!|~|
33901f65f947SAndy Whitcroft				&&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
339184731623SJoe Perches				\?:|\?|:
33929c0ca6f9SAndy Whitcroft			}x;
3393cf655043SAndy Whitcroft			my @elements = split(/($ops|;)/, $opline);
33943705ce5bSJoe Perches
33953705ce5bSJoe Perches##			print("element count: <" . $#elements . ">\n");
33963705ce5bSJoe Perches##			foreach my $el (@elements) {
33973705ce5bSJoe Perches##				print("el: <$el>\n");
33983705ce5bSJoe Perches##			}
33993705ce5bSJoe Perches
34003705ce5bSJoe Perches			my @fix_elements = ();
340100df344fSAndy Whitcroft			my $off = 0;
34026c72ffaaSAndy Whitcroft
34033705ce5bSJoe Perches			foreach my $el (@elements) {
34043705ce5bSJoe Perches				push(@fix_elements, substr($rawline, $off, length($el)));
34053705ce5bSJoe Perches				$off += length($el);
34063705ce5bSJoe Perches			}
34073705ce5bSJoe Perches
34083705ce5bSJoe Perches			$off = 0;
34093705ce5bSJoe Perches
34106c72ffaaSAndy Whitcroft			my $blank = copy_spacing($opline);
3411b34c648bSJoe Perches			my $last_after = -1;
34126c72ffaaSAndy Whitcroft
34130a920b5bSAndy Whitcroft			for (my $n = 0; $n < $#elements; $n += 2) {
34143705ce5bSJoe Perches
34153705ce5bSJoe Perches				my $good = $fix_elements[$n] . $fix_elements[$n + 1];
34163705ce5bSJoe Perches
34173705ce5bSJoe Perches##				print("n: <$n> good: <$good>\n");
34183705ce5bSJoe Perches
34194a0df2efSAndy Whitcroft				$off += length($elements[$n]);
34204a0df2efSAndy Whitcroft
342125985edcSLucas De Marchi				# Pick up the preceding and succeeding characters.
3422773647a0SAndy Whitcroft				my $ca = substr($opline, 0, $off);
3423773647a0SAndy Whitcroft				my $cc = '';
3424773647a0SAndy Whitcroft				if (length($opline) >= ($off + length($elements[$n + 1]))) {
3425773647a0SAndy Whitcroft					$cc = substr($opline, $off + length($elements[$n + 1]));
3426773647a0SAndy Whitcroft				}
3427773647a0SAndy Whitcroft				my $cb = "$ca$;$cc";
3428773647a0SAndy Whitcroft
34294a0df2efSAndy Whitcroft				my $a = '';
34304a0df2efSAndy Whitcroft				$a = 'V' if ($elements[$n] ne '');
34314a0df2efSAndy Whitcroft				$a = 'W' if ($elements[$n] =~ /\s$/);
3432cf655043SAndy Whitcroft				$a = 'C' if ($elements[$n] =~ /$;$/);
34334a0df2efSAndy Whitcroft				$a = 'B' if ($elements[$n] =~ /(\[|\()$/);
34344a0df2efSAndy Whitcroft				$a = 'O' if ($elements[$n] eq '');
3435773647a0SAndy Whitcroft				$a = 'E' if ($ca =~ /^\s*$/);
34364a0df2efSAndy Whitcroft
34370a920b5bSAndy Whitcroft				my $op = $elements[$n + 1];
34384a0df2efSAndy Whitcroft
34394a0df2efSAndy Whitcroft				my $c = '';
34400a920b5bSAndy Whitcroft				if (defined $elements[$n + 2]) {
34414a0df2efSAndy Whitcroft					$c = 'V' if ($elements[$n + 2] ne '');
34424a0df2efSAndy Whitcroft					$c = 'W' if ($elements[$n + 2] =~ /^\s/);
3443cf655043SAndy Whitcroft					$c = 'C' if ($elements[$n + 2] =~ /^$;/);
34444a0df2efSAndy Whitcroft					$c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
34454a0df2efSAndy Whitcroft					$c = 'O' if ($elements[$n + 2] eq '');
34468b1b3378SAndy Whitcroft					$c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
34474a0df2efSAndy Whitcroft				} else {
34484a0df2efSAndy Whitcroft					$c = 'E';
34490a920b5bSAndy Whitcroft				}
34500a920b5bSAndy Whitcroft
34514a0df2efSAndy Whitcroft				my $ctx = "${a}x${c}";
34524a0df2efSAndy Whitcroft
34534a0df2efSAndy Whitcroft				my $at = "(ctx:$ctx)";
34544a0df2efSAndy Whitcroft
34556c72ffaaSAndy Whitcroft				my $ptr = substr($blank, 0, $off) . "^";
3456de7d4f0eSAndy Whitcroft				my $hereptr = "$hereline$ptr\n";
34570a920b5bSAndy Whitcroft
345874048ed8SAndy Whitcroft				# Pull out the value of this operator.
34596c72ffaaSAndy Whitcroft				my $op_type = substr($curr_values, $off + 1, 1);
34600a920b5bSAndy Whitcroft
34611f65f947SAndy Whitcroft				# Get the full operator variant.
34621f65f947SAndy Whitcroft				my $opv = $op . substr($curr_vars, $off, 1);
34631f65f947SAndy Whitcroft
346413214adfSAndy Whitcroft				# Ignore operators passed as parameters.
346513214adfSAndy Whitcroft				if ($op_type ne 'V' &&
346613214adfSAndy Whitcroft				    $ca =~ /\s$/ && $cc =~ /^\s*,/) {
346713214adfSAndy Whitcroft
3468cf655043SAndy Whitcroft#				# Ignore comments
3469cf655043SAndy Whitcroft#				} elsif ($op =~ /^$;+$/) {
347013214adfSAndy Whitcroft
3471d8aaf121SAndy Whitcroft				# ; should have either the end of line or a space or \ after it
347213214adfSAndy Whitcroft				} elsif ($op eq ';') {
3473cf655043SAndy Whitcroft					if ($ctx !~ /.x[WEBC]/ &&
3474cf655043SAndy Whitcroft					    $cc !~ /^\\/ && $cc !~ /^;/) {
34753705ce5bSJoe Perches						if (ERROR("SPACING",
34763705ce5bSJoe Perches							  "space required after that '$op' $at\n" . $hereptr)) {
3477b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
34783705ce5bSJoe Perches							$line_fixed = 1;
34793705ce5bSJoe Perches						}
3480d8aaf121SAndy Whitcroft					}
3481d8aaf121SAndy Whitcroft
3482d8aaf121SAndy Whitcroft				# // is a comment
3483d8aaf121SAndy Whitcroft				} elsif ($op eq '//') {
34840a920b5bSAndy Whitcroft
3485b00e4814SJoe Perches				#   :   when part of a bitfield
3486b00e4814SJoe Perches				} elsif ($opv eq ':B') {
3487b00e4814SJoe Perches					# skip the bitfield test for now
3488b00e4814SJoe Perches
34891f65f947SAndy Whitcroft				# No spaces for:
34901f65f947SAndy Whitcroft				#   ->
3491b00e4814SJoe Perches				} elsif ($op eq '->') {
34924a0df2efSAndy Whitcroft					if ($ctx =~ /Wx.|.xW/) {
34933705ce5bSJoe Perches						if (ERROR("SPACING",
34943705ce5bSJoe Perches							  "spaces prohibited around that '$op' $at\n" . $hereptr)) {
3495b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($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				# , must have a space on the right.
35040a920b5bSAndy Whitcroft				} elsif ($op eq ',') {
3505cf655043SAndy Whitcroft					if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
35063705ce5bSJoe Perches						if (ERROR("SPACING",
35073705ce5bSJoe Perches							  "space required after that '$op' $at\n" . $hereptr)) {
3508b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
35093705ce5bSJoe Perches							$line_fixed = 1;
3510b34c648bSJoe Perches							$last_after = $n;
35113705ce5bSJoe Perches						}
35120a920b5bSAndy Whitcroft					}
35130a920b5bSAndy Whitcroft
35149c0ca6f9SAndy Whitcroft				# '*' as part of a type definition -- reported already.
351574048ed8SAndy Whitcroft				} elsif ($opv eq '*_') {
35169c0ca6f9SAndy Whitcroft					#warn "'*' is part of type\n";
35179c0ca6f9SAndy Whitcroft
35189c0ca6f9SAndy Whitcroft				# unary operators should have a space before and
35199c0ca6f9SAndy Whitcroft				# none after.  May be left adjacent to another
35209c0ca6f9SAndy Whitcroft				# unary operator, or a cast
35219c0ca6f9SAndy Whitcroft				} elsif ($op eq '!' || $op eq '~' ||
352274048ed8SAndy Whitcroft					 $opv eq '*U' || $opv eq '-U' ||
35230d413866SAndy Whitcroft					 $opv eq '&U' || $opv eq '&&U') {
3524cf655043SAndy Whitcroft					if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
35253705ce5bSJoe Perches						if (ERROR("SPACING",
35263705ce5bSJoe Perches							  "space required before that '$op' $at\n" . $hereptr)) {
3527b34c648bSJoe Perches							if ($n != $last_after + 2) {
3528b34c648bSJoe Perches								$good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]);
35293705ce5bSJoe Perches								$line_fixed = 1;
35303705ce5bSJoe Perches							}
35310a920b5bSAndy Whitcroft						}
3532b34c648bSJoe Perches					}
3533a3340b35SAndy Whitcroft					if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
3534171ae1a4SAndy Whitcroft						# A unary '*' may be const
3535171ae1a4SAndy Whitcroft
3536171ae1a4SAndy Whitcroft					} elsif ($ctx =~ /.xW/) {
35373705ce5bSJoe Perches						if (ERROR("SPACING",
35383705ce5bSJoe Perches							  "space prohibited after that '$op' $at\n" . $hereptr)) {
3539b34c648bSJoe Perches							$good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]);
35403705ce5bSJoe Perches							if (defined $fix_elements[$n + 2]) {
35413705ce5bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
35423705ce5bSJoe Perches							}
3543b34c648bSJoe Perches							$line_fixed = 1;
35443705ce5bSJoe Perches						}
35450a920b5bSAndy Whitcroft					}
35460a920b5bSAndy Whitcroft
35470a920b5bSAndy Whitcroft				# unary ++ and unary -- are allowed no space on one side.
35480a920b5bSAndy Whitcroft				} elsif ($op eq '++' or $op eq '--') {
3549773647a0SAndy Whitcroft					if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
35503705ce5bSJoe Perches						if (ERROR("SPACING",
35513705ce5bSJoe Perches							  "space required one side of that '$op' $at\n" . $hereptr)) {
3552b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
35533705ce5bSJoe Perches							$line_fixed = 1;
35543705ce5bSJoe Perches						}
35550a920b5bSAndy Whitcroft					}
3556773647a0SAndy Whitcroft					if ($ctx =~ /Wx[BE]/ ||
3557773647a0SAndy Whitcroft					    ($ctx =~ /Wx./ && $cc =~ /^;/)) {
35583705ce5bSJoe Perches						if (ERROR("SPACING",
35593705ce5bSJoe Perches							  "space prohibited before that '$op' $at\n" . $hereptr)) {
3560b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
35613705ce5bSJoe Perches							$line_fixed = 1;
35623705ce5bSJoe Perches						}
3563653d4876SAndy Whitcroft					}
3564773647a0SAndy Whitcroft					if ($ctx =~ /ExW/) {
35653705ce5bSJoe Perches						if (ERROR("SPACING",
35663705ce5bSJoe Perches							  "space prohibited after that '$op' $at\n" . $hereptr)) {
3567b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
35683705ce5bSJoe Perches							if (defined $fix_elements[$n + 2]) {
35693705ce5bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
3570773647a0SAndy Whitcroft							}
3571b34c648bSJoe Perches							$line_fixed = 1;
35723705ce5bSJoe Perches						}
35733705ce5bSJoe Perches					}
35740a920b5bSAndy Whitcroft
35750a920b5bSAndy Whitcroft				# << and >> may either have or not have spaces both sides
35769c0ca6f9SAndy Whitcroft				} elsif ($op eq '<<' or $op eq '>>' or
35779c0ca6f9SAndy Whitcroft					 $op eq '&' or $op eq '^' or $op eq '|' or
35789c0ca6f9SAndy Whitcroft					 $op eq '+' or $op eq '-' or
3579c2fdda0dSAndy Whitcroft					 $op eq '*' or $op eq '/' or
3580c2fdda0dSAndy Whitcroft					 $op eq '%')
35810a920b5bSAndy Whitcroft				{
3582773647a0SAndy Whitcroft					if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
35833705ce5bSJoe Perches						if (ERROR("SPACING",
35843705ce5bSJoe Perches							  "need consistent spacing around '$op' $at\n" . $hereptr)) {
3585b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
3586b34c648bSJoe Perches							if (defined $fix_elements[$n + 2]) {
3587b34c648bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
3588b34c648bSJoe Perches							}
35893705ce5bSJoe Perches							$line_fixed = 1;
35903705ce5bSJoe Perches						}
35910a920b5bSAndy Whitcroft					}
35920a920b5bSAndy Whitcroft
35931f65f947SAndy Whitcroft				# A colon needs no spaces before when it is
35941f65f947SAndy Whitcroft				# terminating a case value or a label.
35951f65f947SAndy Whitcroft				} elsif ($opv eq ':C' || $opv eq ':L') {
35961f65f947SAndy Whitcroft					if ($ctx =~ /Wx./) {
35973705ce5bSJoe Perches						if (ERROR("SPACING",
35983705ce5bSJoe Perches							  "space prohibited before that '$op' $at\n" . $hereptr)) {
3599b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
36003705ce5bSJoe Perches							$line_fixed = 1;
36013705ce5bSJoe Perches						}
36021f65f947SAndy Whitcroft					}
36031f65f947SAndy Whitcroft
36040a920b5bSAndy Whitcroft				# All the others need spaces both sides.
3605cf655043SAndy Whitcroft				} elsif ($ctx !~ /[EWC]x[CWE]/) {
36061f65f947SAndy Whitcroft					my $ok = 0;
36071f65f947SAndy Whitcroft
360822f2a2efSAndy Whitcroft					# Ignore email addresses <foo@bar>
36091f65f947SAndy Whitcroft					if (($op eq '<' &&
36101f65f947SAndy Whitcroft					     $cc =~ /^\S+\@\S+>/) ||
36111f65f947SAndy Whitcroft					    ($op eq '>' &&
36121f65f947SAndy Whitcroft					     $ca =~ /<\S+\@\S+$/))
36131f65f947SAndy Whitcroft					{
36141f65f947SAndy Whitcroft					    	$ok = 1;
36151f65f947SAndy Whitcroft					}
36161f65f947SAndy Whitcroft
361784731623SJoe Perches					# messages are ERROR, but ?: are CHK
36181f65f947SAndy Whitcroft					if ($ok == 0) {
361984731623SJoe Perches						my $msg_type = \&ERROR;
362084731623SJoe Perches						$msg_type = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
362184731623SJoe Perches
362284731623SJoe Perches						if (&{$msg_type}("SPACING",
36233705ce5bSJoe Perches								 "spaces required around that '$op' $at\n" . $hereptr)) {
3624b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
3625b34c648bSJoe Perches							if (defined $fix_elements[$n + 2]) {
3626b34c648bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
3627b34c648bSJoe Perches							}
36283705ce5bSJoe Perches							$line_fixed = 1;
36293705ce5bSJoe Perches						}
36300a920b5bSAndy Whitcroft					}
363122f2a2efSAndy Whitcroft				}
36324a0df2efSAndy Whitcroft				$off += length($elements[$n + 1]);
36333705ce5bSJoe Perches
36343705ce5bSJoe Perches##				print("n: <$n> GOOD: <$good>\n");
36353705ce5bSJoe Perches
36363705ce5bSJoe Perches				$fixed_line = $fixed_line . $good;
36370a920b5bSAndy Whitcroft			}
36383705ce5bSJoe Perches
36393705ce5bSJoe Perches			if (($#elements % 2) == 0) {
36403705ce5bSJoe Perches				$fixed_line = $fixed_line . $fix_elements[$#elements];
36413705ce5bSJoe Perches			}
36423705ce5bSJoe Perches
3643194f66fcSJoe Perches			if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) {
3644194f66fcSJoe Perches				$fixed[$fixlinenr] = $fixed_line;
36453705ce5bSJoe Perches			}
36463705ce5bSJoe Perches
36473705ce5bSJoe Perches
36480a920b5bSAndy Whitcroft		}
36490a920b5bSAndy Whitcroft
3650786b6326SJoe Perches# check for whitespace before a non-naked semicolon
3651d2e248e7SJoe Perches		if ($line =~ /^\+.*\S\s+;\s*$/) {
3652786b6326SJoe Perches			if (WARN("SPACING",
3653786b6326SJoe Perches				 "space prohibited before semicolon\n" . $herecurr) &&
3654786b6326SJoe Perches			    $fix) {
3655194f66fcSJoe Perches				1 while $fixed[$fixlinenr] =~
3656786b6326SJoe Perches				    s/^(\+.*\S)\s+;/$1;/;
3657786b6326SJoe Perches			}
3658786b6326SJoe Perches		}
3659786b6326SJoe Perches
3660f0a594c1SAndy Whitcroft# check for multiple assignments
3661f0a594c1SAndy Whitcroft		if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
3662000d1cc1SJoe Perches			CHK("MULTIPLE_ASSIGNMENTS",
3663000d1cc1SJoe Perches			    "multiple assignments should be avoided\n" . $herecurr);
3664f0a594c1SAndy Whitcroft		}
3665f0a594c1SAndy Whitcroft
366622f2a2efSAndy Whitcroft## # check for multiple declarations, allowing for a function declaration
366722f2a2efSAndy Whitcroft## # continuation.
366822f2a2efSAndy Whitcroft## 		if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
366922f2a2efSAndy Whitcroft## 		    $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
367022f2a2efSAndy Whitcroft##
367122f2a2efSAndy Whitcroft## 			# Remove any bracketed sections to ensure we do not
367222f2a2efSAndy Whitcroft## 			# falsly report the parameters of functions.
367322f2a2efSAndy Whitcroft## 			my $ln = $line;
367422f2a2efSAndy Whitcroft## 			while ($ln =~ s/\([^\(\)]*\)//g) {
367522f2a2efSAndy Whitcroft## 			}
367622f2a2efSAndy Whitcroft## 			if ($ln =~ /,/) {
3677000d1cc1SJoe Perches## 				WARN("MULTIPLE_DECLARATION",
3678000d1cc1SJoe Perches##				     "declaring multiple variables together should be avoided\n" . $herecurr);
367922f2a2efSAndy Whitcroft## 			}
368022f2a2efSAndy Whitcroft## 		}
3681f0a594c1SAndy Whitcroft
36820a920b5bSAndy Whitcroft#need space before brace following if, while, etc
368322f2a2efSAndy Whitcroft		if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
368422f2a2efSAndy Whitcroft		    $line =~ /do{/) {
36853705ce5bSJoe Perches			if (ERROR("SPACING",
36863705ce5bSJoe Perches				  "space required before the open brace '{'\n" . $herecurr) &&
36873705ce5bSJoe Perches			    $fix) {
3688194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/^(\+.*(?:do|\))){/$1 {/;
36893705ce5bSJoe Perches			}
3690de7d4f0eSAndy Whitcroft		}
3691de7d4f0eSAndy Whitcroft
3692c4a62ef9SJoe Perches## # check for blank lines before declarations
3693c4a62ef9SJoe Perches##		if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
3694c4a62ef9SJoe Perches##		    $prevrawline =~ /^.\s*$/) {
3695c4a62ef9SJoe Perches##			WARN("SPACING",
3696c4a62ef9SJoe Perches##			     "No blank lines before declarations\n" . $hereprev);
3697c4a62ef9SJoe Perches##		}
3698c4a62ef9SJoe Perches##
3699c4a62ef9SJoe Perches
3700de7d4f0eSAndy Whitcroft# closing brace should have a space following it when it has anything
3701de7d4f0eSAndy Whitcroft# on the line
3702de7d4f0eSAndy Whitcroft		if ($line =~ /}(?!(?:,|;|\)))\S/) {
3703d5e616fcSJoe Perches			if (ERROR("SPACING",
3704d5e616fcSJoe Perches				  "space required after that close brace '}'\n" . $herecurr) &&
3705d5e616fcSJoe Perches			    $fix) {
3706194f66fcSJoe Perches				$fixed[$fixlinenr] =~
3707d5e616fcSJoe Perches				    s/}((?!(?:,|;|\)))\S)/} $1/;
3708d5e616fcSJoe Perches			}
37090a920b5bSAndy Whitcroft		}
37100a920b5bSAndy Whitcroft
371122f2a2efSAndy Whitcroft# check spacing on square brackets
371222f2a2efSAndy Whitcroft		if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
37133705ce5bSJoe Perches			if (ERROR("SPACING",
37143705ce5bSJoe Perches				  "space prohibited after that open square bracket '['\n" . $herecurr) &&
37153705ce5bSJoe Perches			    $fix) {
3716194f66fcSJoe Perches				$fixed[$fixlinenr] =~
37173705ce5bSJoe Perches				    s/\[\s+/\[/;
37183705ce5bSJoe Perches			}
371922f2a2efSAndy Whitcroft		}
372022f2a2efSAndy Whitcroft		if ($line =~ /\s\]/) {
37213705ce5bSJoe Perches			if (ERROR("SPACING",
37223705ce5bSJoe Perches				  "space prohibited before that close square bracket ']'\n" . $herecurr) &&
37233705ce5bSJoe Perches			    $fix) {
3724194f66fcSJoe Perches				$fixed[$fixlinenr] =~
37253705ce5bSJoe Perches				    s/\s+\]/\]/;
37263705ce5bSJoe Perches			}
372722f2a2efSAndy Whitcroft		}
372822f2a2efSAndy Whitcroft
3729c45dcabdSAndy Whitcroft# check spacing on parentheses
37309c0ca6f9SAndy Whitcroft		if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
37319c0ca6f9SAndy Whitcroft		    $line !~ /for\s*\(\s+;/) {
37323705ce5bSJoe Perches			if (ERROR("SPACING",
37333705ce5bSJoe Perches				  "space prohibited after that open parenthesis '('\n" . $herecurr) &&
37343705ce5bSJoe Perches			    $fix) {
3735194f66fcSJoe Perches				$fixed[$fixlinenr] =~
37363705ce5bSJoe Perches				    s/\(\s+/\(/;
37373705ce5bSJoe Perches			}
373822f2a2efSAndy Whitcroft		}
373913214adfSAndy Whitcroft		if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
3740c45dcabdSAndy Whitcroft		    $line !~ /for\s*\(.*;\s+\)/ &&
3741c45dcabdSAndy Whitcroft		    $line !~ /:\s+\)/) {
37423705ce5bSJoe Perches			if (ERROR("SPACING",
37433705ce5bSJoe Perches				  "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
37443705ce5bSJoe Perches			    $fix) {
3745194f66fcSJoe Perches				print("fixlinenr: <$fixlinenr> fixed[fixlinenr]: <$fixed[$fixlinenr]>\n");
3746194f66fcSJoe Perches				$fixed[$fixlinenr] =~
37473705ce5bSJoe Perches				    s/\s+\)/\)/;
37483705ce5bSJoe Perches			}
374922f2a2efSAndy Whitcroft		}
375022f2a2efSAndy Whitcroft
3751e2826fd0SJoe Perches# check unnecessary parentheses around addressof/dereference single $Lvals
3752e2826fd0SJoe Perches# ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar
3753e2826fd0SJoe Perches
3754e2826fd0SJoe Perches		while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) {
3755e2826fd0SJoe Perches			CHK("UNNECESSARY_PARENTHESES",
3756e2826fd0SJoe Perches			    "Unnecessary parentheses around $1\n" . $herecurr);
3757e2826fd0SJoe Perches		    }
3758e2826fd0SJoe Perches
37590a920b5bSAndy Whitcroft#goto labels aren't indented, allow a single space however
37604a0df2efSAndy Whitcroft		if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
37610a920b5bSAndy Whitcroft		   !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
37623705ce5bSJoe Perches			if (WARN("INDENTED_LABEL",
37633705ce5bSJoe Perches				 "labels should not be indented\n" . $herecurr) &&
37643705ce5bSJoe Perches			    $fix) {
3765194f66fcSJoe Perches				$fixed[$fixlinenr] =~
37663705ce5bSJoe Perches				    s/^(.)\s+/$1/;
37673705ce5bSJoe Perches			}
37680a920b5bSAndy Whitcroft		}
37690a920b5bSAndy Whitcroft
37705b9553abSJoe Perches# return is not a function
3771507e5141SJoe Perches		if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {
3772c45dcabdSAndy Whitcroft			my $spacing = $1;
3773507e5141SJoe Perches			if ($^V && $^V ge 5.10.0 &&
37745b9553abSJoe Perches			    $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) {
37755b9553abSJoe Perches				my $value = $1;
37765b9553abSJoe Perches				$value = deparenthesize($value);
37775b9553abSJoe Perches				if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) {
3778000d1cc1SJoe Perches					ERROR("RETURN_PARENTHESES",
3779000d1cc1SJoe Perches					      "return is not a function, parentheses are not required\n" . $herecurr);
37805b9553abSJoe Perches				}
3781c45dcabdSAndy Whitcroft			} elsif ($spacing !~ /\s+/) {
3782000d1cc1SJoe Perches				ERROR("SPACING",
3783000d1cc1SJoe Perches				      "space required before the open parenthesis '('\n" . $herecurr);
3784c45dcabdSAndy Whitcroft			}
3785c45dcabdSAndy Whitcroft		}
3786507e5141SJoe Perches
3787b43ae21bSJoe Perches# unnecessary return in a void function
3788b43ae21bSJoe Perches# at end-of-function, with the previous line a single leading tab, then return;
3789b43ae21bSJoe Perches# and the line before that not a goto label target like "out:"
3790b43ae21bSJoe Perches		if ($sline =~ /^[ \+]}\s*$/ &&
3791b43ae21bSJoe Perches		    $prevline =~ /^\+\treturn\s*;\s*$/ &&
3792b43ae21bSJoe Perches		    $linenr >= 3 &&
3793b43ae21bSJoe Perches		    $lines[$linenr - 3] =~ /^[ +]/ &&
3794b43ae21bSJoe Perches		    $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) {
37959819cf25SJoe Perches			WARN("RETURN_VOID",
3796b43ae21bSJoe Perches			     "void function return statements are not generally useful\n" . $hereprev);
37979819cf25SJoe Perches               }
37989819cf25SJoe Perches
3799189248d8SJoe Perches# if statements using unnecessary parentheses - ie: if ((foo == bar))
3800189248d8SJoe Perches		if ($^V && $^V ge 5.10.0 &&
3801189248d8SJoe Perches		    $line =~ /\bif\s*((?:\(\s*){2,})/) {
3802189248d8SJoe Perches			my $openparens = $1;
3803189248d8SJoe Perches			my $count = $openparens =~ tr@\(@\(@;
3804189248d8SJoe Perches			my $msg = "";
3805189248d8SJoe Perches			if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) {
3806189248d8SJoe Perches				my $comp = $4;	#Not $1 because of $LvalOrFunc
3807189248d8SJoe Perches				$msg = " - maybe == should be = ?" if ($comp eq "==");
3808189248d8SJoe Perches				WARN("UNNECESSARY_PARENTHESES",
3809189248d8SJoe Perches				     "Unnecessary parentheses$msg\n" . $herecurr);
3810189248d8SJoe Perches			}
3811189248d8SJoe Perches		}
3812189248d8SJoe Perches
381353a3c448SAndy Whitcroft# Return of what appears to be an errno should normally be -'ve
381453a3c448SAndy Whitcroft		if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) {
381553a3c448SAndy Whitcroft			my $name = $1;
381653a3c448SAndy Whitcroft			if ($name ne 'EOF' && $name ne 'ERROR') {
3817000d1cc1SJoe Perches				WARN("USE_NEGATIVE_ERRNO",
3818000d1cc1SJoe Perches				     "return of an errno should typically be -ve (return -$1)\n" . $herecurr);
381953a3c448SAndy Whitcroft			}
382053a3c448SAndy Whitcroft		}
3821c45dcabdSAndy Whitcroft
38220a920b5bSAndy Whitcroft# Need a space before open parenthesis after if, while etc
38234a0df2efSAndy Whitcroft		if ($line =~ /\b(if|while|for|switch)\(/) {
38243705ce5bSJoe Perches			if (ERROR("SPACING",
38253705ce5bSJoe Perches				  "space required before the open parenthesis '('\n" . $herecurr) &&
38263705ce5bSJoe Perches			    $fix) {
3827194f66fcSJoe Perches				$fixed[$fixlinenr] =~
38283705ce5bSJoe Perches				    s/\b(if|while|for|switch)\(/$1 \(/;
38293705ce5bSJoe Perches			}
38300a920b5bSAndy Whitcroft		}
38310a920b5bSAndy Whitcroft
3832f5fe35ddSAndy Whitcroft# Check for illegal assignment in if conditional -- and check for trailing
3833f5fe35ddSAndy Whitcroft# statements after the conditional.
3834170d3a22SAndy Whitcroft		if ($line =~ /do\s*(?!{)/) {
38353e469cdcSAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
38363e469cdcSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0)
38373e469cdcSAndy Whitcroft					if (!defined $stat);
3838170d3a22SAndy Whitcroft			my ($stat_next) = ctx_statement_block($line_nr_next,
3839170d3a22SAndy Whitcroft						$remain_next, $off_next);
3840170d3a22SAndy Whitcroft			$stat_next =~ s/\n./\n /g;
3841170d3a22SAndy Whitcroft			##print "stat<$stat> stat_next<$stat_next>\n";
3842170d3a22SAndy Whitcroft
3843170d3a22SAndy Whitcroft			if ($stat_next =~ /^\s*while\b/) {
3844170d3a22SAndy Whitcroft				# If the statement carries leading newlines,
3845170d3a22SAndy Whitcroft				# then count those as offsets.
3846170d3a22SAndy Whitcroft				my ($whitespace) =
3847170d3a22SAndy Whitcroft					($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
3848170d3a22SAndy Whitcroft				my $offset =
3849170d3a22SAndy Whitcroft					statement_rawlines($whitespace) - 1;
3850170d3a22SAndy Whitcroft
3851170d3a22SAndy Whitcroft				$suppress_whiletrailers{$line_nr_next +
3852170d3a22SAndy Whitcroft								$offset} = 1;
3853170d3a22SAndy Whitcroft			}
3854170d3a22SAndy Whitcroft		}
3855170d3a22SAndy Whitcroft		if (!defined $suppress_whiletrailers{$linenr} &&
3856c11230f4SJoe Perches		    defined($stat) && defined($cond) &&
3857170d3a22SAndy Whitcroft		    $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
3858171ae1a4SAndy Whitcroft			my ($s, $c) = ($stat, $cond);
38598905a67cSAndy Whitcroft
3860b53c8e10SAndy Whitcroft			if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
3861000d1cc1SJoe Perches				ERROR("ASSIGN_IN_IF",
3862000d1cc1SJoe Perches				      "do not use assignment in if condition\n" . $herecurr);
38638905a67cSAndy Whitcroft			}
38648905a67cSAndy Whitcroft
38658905a67cSAndy Whitcroft			# Find out what is on the end of the line after the
38668905a67cSAndy Whitcroft			# conditional.
3867773647a0SAndy Whitcroft			substr($s, 0, length($c), '');
38688905a67cSAndy Whitcroft			$s =~ s/\n.*//g;
386913214adfSAndy Whitcroft			$s =~ s/$;//g; 	# Remove any comments
387053210168SAndy Whitcroft			if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
387153210168SAndy Whitcroft			    $c !~ /}\s*while\s*/)
3872773647a0SAndy Whitcroft			{
3873bb44ad39SAndy Whitcroft				# Find out how long the conditional actually is.
3874bb44ad39SAndy Whitcroft				my @newlines = ($c =~ /\n/gs);
3875bb44ad39SAndy Whitcroft				my $cond_lines = 1 + $#newlines;
387642bdf74cSHidetoshi Seto				my $stat_real = '';
3877bb44ad39SAndy Whitcroft
387842bdf74cSHidetoshi Seto				$stat_real = raw_line($linenr, $cond_lines)
387942bdf74cSHidetoshi Seto							. "\n" if ($cond_lines);
3880bb44ad39SAndy Whitcroft				if (defined($stat_real) && $cond_lines > 1) {
3881bb44ad39SAndy Whitcroft					$stat_real = "[...]\n$stat_real";
3882bb44ad39SAndy Whitcroft				}
3883bb44ad39SAndy Whitcroft
3884000d1cc1SJoe Perches				ERROR("TRAILING_STATEMENTS",
3885000d1cc1SJoe Perches				      "trailing statements should be on next line\n" . $herecurr . $stat_real);
38868905a67cSAndy Whitcroft			}
38878905a67cSAndy Whitcroft		}
38888905a67cSAndy Whitcroft
388913214adfSAndy Whitcroft# Check for bitwise tests written as boolean
389013214adfSAndy Whitcroft		if ($line =~ /
389113214adfSAndy Whitcroft			(?:
389213214adfSAndy Whitcroft				(?:\[|\(|\&\&|\|\|)
389313214adfSAndy Whitcroft				\s*0[xX][0-9]+\s*
389413214adfSAndy Whitcroft				(?:\&\&|\|\|)
389513214adfSAndy Whitcroft			|
389613214adfSAndy Whitcroft				(?:\&\&|\|\|)
389713214adfSAndy Whitcroft				\s*0[xX][0-9]+\s*
389813214adfSAndy Whitcroft				(?:\&\&|\|\||\)|\])
389913214adfSAndy Whitcroft			)/x)
390013214adfSAndy Whitcroft		{
3901000d1cc1SJoe Perches			WARN("HEXADECIMAL_BOOLEAN_TEST",
3902000d1cc1SJoe Perches			     "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
390313214adfSAndy Whitcroft		}
390413214adfSAndy Whitcroft
39058905a67cSAndy Whitcroft# if and else should not have general statements after it
390613214adfSAndy Whitcroft		if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
390713214adfSAndy Whitcroft			my $s = $1;
390813214adfSAndy Whitcroft			$s =~ s/$;//g; 	# Remove any comments
390913214adfSAndy Whitcroft			if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
3910000d1cc1SJoe Perches				ERROR("TRAILING_STATEMENTS",
3911000d1cc1SJoe Perches				      "trailing statements should be on next line\n" . $herecurr);
39120a920b5bSAndy Whitcroft			}
391313214adfSAndy Whitcroft		}
391439667782SAndy Whitcroft# if should not continue a brace
391539667782SAndy Whitcroft		if ($line =~ /}\s*if\b/) {
3916000d1cc1SJoe Perches			ERROR("TRAILING_STATEMENTS",
3917048b123fSRasmus Villemoes			      "trailing statements should be on next line (or did you mean 'else if'?)\n" .
391839667782SAndy Whitcroft				$herecurr);
391939667782SAndy Whitcroft		}
3920a1080bf8SAndy Whitcroft# case and default should not have general statements after them
3921a1080bf8SAndy Whitcroft		if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
3922a1080bf8SAndy Whitcroft		    $line !~ /\G(?:
39233fef12d6SAndy Whitcroft			(?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
3924a1080bf8SAndy Whitcroft			\s*return\s+
3925a1080bf8SAndy Whitcroft		    )/xg)
3926a1080bf8SAndy Whitcroft		{
3927000d1cc1SJoe Perches			ERROR("TRAILING_STATEMENTS",
3928000d1cc1SJoe Perches			      "trailing statements should be on next line\n" . $herecurr);
3929a1080bf8SAndy Whitcroft		}
39300a920b5bSAndy Whitcroft
39310a920b5bSAndy Whitcroft		# Check for }<nl>else {, these must be at the same
39320a920b5bSAndy Whitcroft		# indent level to be relevant to each other.
39338b8856f4SJoe Perches		if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ &&
39340a920b5bSAndy Whitcroft		    $previndent == $indent) {
39358b8856f4SJoe Perches			if (ERROR("ELSE_AFTER_BRACE",
39368b8856f4SJoe Perches				  "else should follow close brace '}'\n" . $hereprev) &&
39378b8856f4SJoe Perches			    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
39388b8856f4SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
39398b8856f4SJoe Perches				fix_delete_line($fixlinenr, $rawline);
39408b8856f4SJoe Perches				my $fixedline = $prevrawline;
39418b8856f4SJoe Perches				$fixedline =~ s/}\s*$//;
39428b8856f4SJoe Perches				if ($fixedline !~ /^\+\s*$/) {
39438b8856f4SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
39448b8856f4SJoe Perches				}
39458b8856f4SJoe Perches				$fixedline = $rawline;
39468b8856f4SJoe Perches				$fixedline =~ s/^(.\s*)else/$1} else/;
39478b8856f4SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
39488b8856f4SJoe Perches			}
39490a920b5bSAndy Whitcroft		}
39500a920b5bSAndy Whitcroft
39518b8856f4SJoe Perches		if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ &&
3952c2fdda0dSAndy Whitcroft		    $previndent == $indent) {
3953c2fdda0dSAndy Whitcroft			my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
3954c2fdda0dSAndy Whitcroft
3955c2fdda0dSAndy Whitcroft			# Find out what is on the end of the line after the
3956c2fdda0dSAndy Whitcroft			# conditional.
3957773647a0SAndy Whitcroft			substr($s, 0, length($c), '');
3958c2fdda0dSAndy Whitcroft			$s =~ s/\n.*//g;
3959c2fdda0dSAndy Whitcroft
3960c2fdda0dSAndy Whitcroft			if ($s =~ /^\s*;/) {
39618b8856f4SJoe Perches				if (ERROR("WHILE_AFTER_BRACE",
39628b8856f4SJoe Perches					  "while should follow close brace '}'\n" . $hereprev) &&
39638b8856f4SJoe Perches				    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
39648b8856f4SJoe Perches					fix_delete_line($fixlinenr - 1, $prevrawline);
39658b8856f4SJoe Perches					fix_delete_line($fixlinenr, $rawline);
39668b8856f4SJoe Perches					my $fixedline = $prevrawline;
39678b8856f4SJoe Perches					my $trailing = $rawline;
39688b8856f4SJoe Perches					$trailing =~ s/^\+//;
39698b8856f4SJoe Perches					$trailing = trim($trailing);
39708b8856f4SJoe Perches					$fixedline =~ s/}\s*$/} $trailing/;
39718b8856f4SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
39728b8856f4SJoe Perches				}
3973c2fdda0dSAndy Whitcroft			}
3974c2fdda0dSAndy Whitcroft		}
3975c2fdda0dSAndy Whitcroft
397695e2c602SJoe Perches#Specific variable tests
3977323c1260SJoe Perches		while ($line =~ m{($Constant|$Lval)}g) {
3978323c1260SJoe Perches			my $var = $1;
397995e2c602SJoe Perches
398095e2c602SJoe Perches#gcc binary extension
398195e2c602SJoe Perches			if ($var =~ /^$Binary$/) {
3982d5e616fcSJoe Perches				if (WARN("GCC_BINARY_CONSTANT",
3983d5e616fcSJoe Perches					 "Avoid gcc v4.3+ binary constant extension: <$var>\n" . $herecurr) &&
3984d5e616fcSJoe Perches				    $fix) {
3985d5e616fcSJoe Perches					my $hexval = sprintf("0x%x", oct($var));
3986194f66fcSJoe Perches					$fixed[$fixlinenr] =~
3987d5e616fcSJoe Perches					    s/\b$var\b/$hexval/;
3988d5e616fcSJoe Perches				}
398995e2c602SJoe Perches			}
399095e2c602SJoe Perches
399195e2c602SJoe Perches#CamelCase
3992807bd26cSJoe Perches			if ($var !~ /^$Constant$/ &&
3993be79794bSJoe Perches			    $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
399422735ce8SJoe Perches#Ignore Page<foo> variants
3995807bd26cSJoe Perches			    $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
399622735ce8SJoe Perches#Ignore SI style variants like nS, mV and dB (ie: max_uV, regulator_min_uA_show)
39973445686aSJoe Perches			    $var !~ /^(?:[a-z_]*?)_?[a-z][A-Z](?:_[a-z_]+)?$/) {
39987e781f67SJoe Perches				while ($var =~ m{($Ident)}g) {
39997e781f67SJoe Perches					my $word = $1;
40007e781f67SJoe Perches					next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
4001d8b07710SJoe Perches					if ($check) {
4002d8b07710SJoe Perches						seed_camelcase_includes();
4003d8b07710SJoe Perches						if (!$file && !$camelcase_file_seeded) {
4004d8b07710SJoe Perches							seed_camelcase_file($realfile);
4005d8b07710SJoe Perches							$camelcase_file_seeded = 1;
4006d8b07710SJoe Perches						}
4007d8b07710SJoe Perches					}
40087e781f67SJoe Perches					if (!defined $camelcase{$word}) {
40097e781f67SJoe Perches						$camelcase{$word} = 1;
4010be79794bSJoe Perches						CHK("CAMELCASE",
40117e781f67SJoe Perches						    "Avoid CamelCase: <$word>\n" . $herecurr);
40127e781f67SJoe Perches					}
4013323c1260SJoe Perches				}
4014323c1260SJoe Perches			}
40153445686aSJoe Perches		}
40160a920b5bSAndy Whitcroft
40170a920b5bSAndy Whitcroft#no spaces allowed after \ in define
4018d5e616fcSJoe Perches		if ($line =~ /\#\s*define.*\\\s+$/) {
4019d5e616fcSJoe Perches			if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
4020d5e616fcSJoe Perches				 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
4021d5e616fcSJoe Perches			    $fix) {
4022194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\s+$//;
4023d5e616fcSJoe Perches			}
40240a920b5bSAndy Whitcroft		}
40250a920b5bSAndy Whitcroft
4026653d4876SAndy Whitcroft#warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
4027c45dcabdSAndy Whitcroft		if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
4028e09dec48SAndy Whitcroft			my $file = "$1.h";
4029e09dec48SAndy Whitcroft			my $checkfile = "include/linux/$file";
4030e09dec48SAndy Whitcroft			if (-f "$root/$checkfile" &&
4031e09dec48SAndy Whitcroft			    $realfile ne $checkfile &&
40327840a94cSWolfram Sang			    $1 !~ /$allowed_asm_includes/)
4033c45dcabdSAndy Whitcroft			{
4034e09dec48SAndy Whitcroft				if ($realfile =~ m{^arch/}) {
4035000d1cc1SJoe Perches					CHK("ARCH_INCLUDE_LINUX",
4036000d1cc1SJoe Perches					    "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
4037e09dec48SAndy Whitcroft				} else {
4038000d1cc1SJoe Perches					WARN("INCLUDE_LINUX",
4039000d1cc1SJoe Perches					     "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
4040e09dec48SAndy Whitcroft				}
40410a920b5bSAndy Whitcroft			}
40420a920b5bSAndy Whitcroft		}
40430a920b5bSAndy Whitcroft
4044653d4876SAndy Whitcroft# multi-statement macros should be enclosed in a do while loop, grab the
4045653d4876SAndy Whitcroft# first statement and ensure its the whole macro if its not enclosed
4046cf655043SAndy Whitcroft# in a known good container
4047b8f96a31SAndy Whitcroft		if ($realfile !~ m@/vmlinux.lds.h$@ &&
4048b8f96a31SAndy Whitcroft		    $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
4049d8aaf121SAndy Whitcroft			my $ln = $linenr;
4050d8aaf121SAndy Whitcroft			my $cnt = $realcnt;
4051c45dcabdSAndy Whitcroft			my ($off, $dstat, $dcond, $rest);
4052c45dcabdSAndy Whitcroft			my $ctx = '';
4053c45dcabdSAndy Whitcroft			($dstat, $dcond, $ln, $cnt, $off) =
4054f74bd194SAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0);
4055f74bd194SAndy Whitcroft			$ctx = $dstat;
4056c45dcabdSAndy Whitcroft			#print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
4057a3bb97a7SAndy Whitcroft			#print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
4058c45dcabdSAndy Whitcroft
4059f74bd194SAndy Whitcroft			$dstat =~ s/^.\s*\#\s*define\s+$Ident(?:\([^\)]*\))?\s*//;
4060292f1a9bSAndy Whitcroft			$dstat =~ s/$;//g;
4061c45dcabdSAndy Whitcroft			$dstat =~ s/\\\n.//g;
4062c45dcabdSAndy Whitcroft			$dstat =~ s/^\s*//s;
4063c45dcabdSAndy Whitcroft			$dstat =~ s/\s*$//s;
4064c45dcabdSAndy Whitcroft
4065c45dcabdSAndy Whitcroft			# Flatten any parentheses and braces
4066bf30d6edSAndy Whitcroft			while ($dstat =~ s/\([^\(\)]*\)/1/ ||
4067bf30d6edSAndy Whitcroft			       $dstat =~ s/\{[^\{\}]*\}/1/ ||
4068c81769fdSAndy Whitcroft			       $dstat =~ s/\[[^\[\]]*\]/1/)
4069bf30d6edSAndy Whitcroft			{
4070c45dcabdSAndy Whitcroft			}
4071c45dcabdSAndy Whitcroft
4072e45bab8eSAndy Whitcroft			# Flatten any obvious string concatentation.
4073e45bab8eSAndy Whitcroft			while ($dstat =~ s/("X*")\s*$Ident/$1/ ||
4074e45bab8eSAndy Whitcroft			       $dstat =~ s/$Ident\s*("X*")/$1/)
4075e45bab8eSAndy Whitcroft			{
4076e45bab8eSAndy Whitcroft			}
4077e45bab8eSAndy Whitcroft
4078c45dcabdSAndy Whitcroft			my $exceptions = qr{
4079c45dcabdSAndy Whitcroft				$Declare|
4080c45dcabdSAndy Whitcroft				module_param_named|
4081a0a0a7a9SKees Cook				MODULE_PARM_DESC|
4082c45dcabdSAndy Whitcroft				DECLARE_PER_CPU|
4083c45dcabdSAndy Whitcroft				DEFINE_PER_CPU|
4084383099fdSAndy Whitcroft				__typeof__\(|
408522fd2d3eSStefani Seibold				union|
408622fd2d3eSStefani Seibold				struct|
4087ea71a0a0SAndy Whitcroft				\.$Ident\s*=\s*|
4088ea71a0a0SAndy Whitcroft				^\"|\"$
4089c45dcabdSAndy Whitcroft			}x;
40905eaa20b9SAndy Whitcroft			#print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
4091f74bd194SAndy Whitcroft			if ($dstat ne '' &&
4092f74bd194SAndy Whitcroft			    $dstat !~ /^(?:$Ident|-?$Constant),$/ &&			# 10, // foo(),
4093f74bd194SAndy Whitcroft			    $dstat !~ /^(?:$Ident|-?$Constant);$/ &&			# foo();
40943cc4b1c3SJoe Perches			    $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ &&		# 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
4095356fd398SJoe Perches			    $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ &&			# character constants
4096f74bd194SAndy Whitcroft			    $dstat !~ /$exceptions/ &&
4097f74bd194SAndy Whitcroft			    $dstat !~ /^\.$Ident\s*=/ &&				# .foo =
4098e942e2c3SJoe Perches			    $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ &&		# stringification #foo
409972f115f9SAndy Whitcroft			    $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ &&	# do {...} while (...); // do {...} while (...)
4100f74bd194SAndy Whitcroft			    $dstat !~ /^for\s*$Constant$/ &&				# for (...)
4101f74bd194SAndy Whitcroft			    $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ &&	# for (...) bar()
4102f74bd194SAndy Whitcroft			    $dstat !~ /^do\s*{/ &&					# do {...
4103f95a7e6aSJoe Perches			    $dstat !~ /^\({/ &&						# ({...
4104f95a7e6aSJoe Perches			    $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
4105c45dcabdSAndy Whitcroft			{
4106f74bd194SAndy Whitcroft				$ctx =~ s/\n*$//;
4107f74bd194SAndy Whitcroft				my $herectx = $here . "\n";
4108f74bd194SAndy Whitcroft				my $cnt = statement_rawlines($ctx);
4109f74bd194SAndy Whitcroft
4110f74bd194SAndy Whitcroft				for (my $n = 0; $n < $cnt; $n++) {
4111f74bd194SAndy Whitcroft					$herectx .= raw_line($linenr, $n) . "\n";
4112c45dcabdSAndy Whitcroft				}
4113c45dcabdSAndy Whitcroft
4114f74bd194SAndy Whitcroft				if ($dstat =~ /;/) {
4115f74bd194SAndy Whitcroft					ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
4116f74bd194SAndy Whitcroft					      "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
4117f74bd194SAndy Whitcroft				} else {
4118000d1cc1SJoe Perches					ERROR("COMPLEX_MACRO",
4119f74bd194SAndy Whitcroft					      "Macros with complex values should be enclosed in parenthesis\n" . "$herectx");
4120d8aaf121SAndy Whitcroft				}
41210a920b5bSAndy Whitcroft			}
41225023d347SJoe Perches
4123481eb486SJoe Perches# check for line continuations outside of #defines, preprocessor #, and asm
41245023d347SJoe Perches
41255023d347SJoe Perches		} else {
41265023d347SJoe Perches			if ($prevline !~ /^..*\\$/ &&
4127481eb486SJoe Perches			    $line !~ /^\+\s*\#.*\\$/ &&		# preprocessor
4128481eb486SJoe Perches			    $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ &&	# asm
41295023d347SJoe Perches			    $line =~ /^\+.*\\$/) {
41305023d347SJoe Perches				WARN("LINE_CONTINUATIONS",
41315023d347SJoe Perches				     "Avoid unnecessary line continuations\n" . $herecurr);
41325023d347SJoe Perches			}
4133653d4876SAndy Whitcroft		}
41340a920b5bSAndy Whitcroft
4135b13edf7fSJoe Perches# do {} while (0) macro tests:
4136b13edf7fSJoe Perches# single-statement macros do not need to be enclosed in do while (0) loop,
4137b13edf7fSJoe Perches# macro should not end with a semicolon
4138b13edf7fSJoe Perches		if ($^V && $^V ge 5.10.0 &&
4139b13edf7fSJoe Perches		    $realfile !~ m@/vmlinux.lds.h$@ &&
4140b13edf7fSJoe Perches		    $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
4141b13edf7fSJoe Perches			my $ln = $linenr;
4142b13edf7fSJoe Perches			my $cnt = $realcnt;
4143b13edf7fSJoe Perches			my ($off, $dstat, $dcond, $rest);
4144b13edf7fSJoe Perches			my $ctx = '';
4145b13edf7fSJoe Perches			($dstat, $dcond, $ln, $cnt, $off) =
4146b13edf7fSJoe Perches				ctx_statement_block($linenr, $realcnt, 0);
4147b13edf7fSJoe Perches			$ctx = $dstat;
4148b13edf7fSJoe Perches
4149b13edf7fSJoe Perches			$dstat =~ s/\\\n.//g;
4150b13edf7fSJoe Perches
4151b13edf7fSJoe Perches			if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
4152b13edf7fSJoe Perches				my $stmts = $2;
4153b13edf7fSJoe Perches				my $semis = $3;
4154b13edf7fSJoe Perches
4155b13edf7fSJoe Perches				$ctx =~ s/\n*$//;
4156b13edf7fSJoe Perches				my $cnt = statement_rawlines($ctx);
4157b13edf7fSJoe Perches				my $herectx = $here . "\n";
4158b13edf7fSJoe Perches
4159b13edf7fSJoe Perches				for (my $n = 0; $n < $cnt; $n++) {
4160b13edf7fSJoe Perches					$herectx .= raw_line($linenr, $n) . "\n";
4161b13edf7fSJoe Perches				}
4162b13edf7fSJoe Perches
4163ac8e97f8SJoe Perches				if (($stmts =~ tr/;/;/) == 1 &&
4164ac8e97f8SJoe Perches				    $stmts !~ /^\s*(if|while|for|switch)\b/) {
4165b13edf7fSJoe Perches					WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
4166b13edf7fSJoe Perches					     "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
4167b13edf7fSJoe Perches				}
4168b13edf7fSJoe Perches				if (defined $semis && $semis ne "") {
4169b13edf7fSJoe Perches					WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
4170b13edf7fSJoe Perches					     "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
4171b13edf7fSJoe Perches				}
4172f5ef95b1SJoe Perches			} elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) {
4173f5ef95b1SJoe Perches				$ctx =~ s/\n*$//;
4174f5ef95b1SJoe Perches				my $cnt = statement_rawlines($ctx);
4175f5ef95b1SJoe Perches				my $herectx = $here . "\n";
4176f5ef95b1SJoe Perches
4177f5ef95b1SJoe Perches				for (my $n = 0; $n < $cnt; $n++) {
4178f5ef95b1SJoe Perches					$herectx .= raw_line($linenr, $n) . "\n";
4179f5ef95b1SJoe Perches				}
4180f5ef95b1SJoe Perches
4181f5ef95b1SJoe Perches				WARN("TRAILING_SEMICOLON",
4182f5ef95b1SJoe Perches				     "macros should not use a trailing semicolon\n" . "$herectx");
4183b13edf7fSJoe Perches			}
4184b13edf7fSJoe Perches		}
4185b13edf7fSJoe Perches
4186080ba929SMike Frysinger# make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
4187080ba929SMike Frysinger# all assignments may have only one of the following with an assignment:
4188080ba929SMike Frysinger#	.
4189080ba929SMike Frysinger#	ALIGN(...)
4190080ba929SMike Frysinger#	VMLINUX_SYMBOL(...)
4191080ba929SMike Frysinger		if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
4192000d1cc1SJoe Perches			WARN("MISSING_VMLINUX_SYMBOL",
4193000d1cc1SJoe Perches			     "vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
4194080ba929SMike Frysinger		}
4195080ba929SMike Frysinger
4196f0a594c1SAndy Whitcroft# check for redundant bracing round if etc
419713214adfSAndy Whitcroft		if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
419813214adfSAndy Whitcroft			my ($level, $endln, @chunks) =
4199cf655043SAndy Whitcroft				ctx_statement_full($linenr, $realcnt, 1);
420013214adfSAndy Whitcroft			#print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
4201cf655043SAndy Whitcroft			#print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
4202cf655043SAndy Whitcroft			if ($#chunks > 0 && $level == 0) {
4203aad4f614SJoe Perches				my @allowed = ();
4204aad4f614SJoe Perches				my $allow = 0;
420513214adfSAndy Whitcroft				my $seen = 0;
4206773647a0SAndy Whitcroft				my $herectx = $here . "\n";
4207cf655043SAndy Whitcroft				my $ln = $linenr - 1;
420813214adfSAndy Whitcroft				for my $chunk (@chunks) {
420913214adfSAndy Whitcroft					my ($cond, $block) = @{$chunk};
421013214adfSAndy Whitcroft
4211773647a0SAndy Whitcroft					# If the condition carries leading newlines, then count those as offsets.
4212773647a0SAndy Whitcroft					my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
4213773647a0SAndy Whitcroft					my $offset = statement_rawlines($whitespace) - 1;
4214773647a0SAndy Whitcroft
4215aad4f614SJoe Perches					$allowed[$allow] = 0;
4216773647a0SAndy Whitcroft					#print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
4217773647a0SAndy Whitcroft
4218773647a0SAndy Whitcroft					# We have looked at and allowed this specific line.
4219773647a0SAndy Whitcroft					$suppress_ifbraces{$ln + $offset} = 1;
4220773647a0SAndy Whitcroft
4221773647a0SAndy Whitcroft					$herectx .= "$rawlines[$ln + $offset]\n[...]\n";
4222cf655043SAndy Whitcroft					$ln += statement_rawlines($block) - 1;
4223cf655043SAndy Whitcroft
4224773647a0SAndy Whitcroft					substr($block, 0, length($cond), '');
422513214adfSAndy Whitcroft
422613214adfSAndy Whitcroft					$seen++ if ($block =~ /^\s*{/);
422713214adfSAndy Whitcroft
4228aad4f614SJoe Perches					#print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
4229cf655043SAndy Whitcroft					if (statement_lines($cond) > 1) {
4230cf655043SAndy Whitcroft						#print "APW: ALLOWED: cond<$cond>\n";
4231aad4f614SJoe Perches						$allowed[$allow] = 1;
423213214adfSAndy Whitcroft					}
423313214adfSAndy Whitcroft					if ($block =~/\b(?:if|for|while)\b/) {
4234cf655043SAndy Whitcroft						#print "APW: ALLOWED: block<$block>\n";
4235aad4f614SJoe Perches						$allowed[$allow] = 1;
423613214adfSAndy Whitcroft					}
4237cf655043SAndy Whitcroft					if (statement_block_size($block) > 1) {
4238cf655043SAndy Whitcroft						#print "APW: ALLOWED: lines block<$block>\n";
4239aad4f614SJoe Perches						$allowed[$allow] = 1;
424013214adfSAndy Whitcroft					}
4241aad4f614SJoe Perches					$allow++;
424213214adfSAndy Whitcroft				}
4243aad4f614SJoe Perches				if ($seen) {
4244aad4f614SJoe Perches					my $sum_allowed = 0;
4245aad4f614SJoe Perches					foreach (@allowed) {
4246aad4f614SJoe Perches						$sum_allowed += $_;
4247aad4f614SJoe Perches					}
4248aad4f614SJoe Perches					if ($sum_allowed == 0) {
4249000d1cc1SJoe Perches						WARN("BRACES",
4250000d1cc1SJoe Perches						     "braces {} are not necessary for any arm of this statement\n" . $herectx);
4251aad4f614SJoe Perches					} elsif ($sum_allowed != $allow &&
4252aad4f614SJoe Perches						 $seen != $allow) {
4253aad4f614SJoe Perches						CHK("BRACES",
4254aad4f614SJoe Perches						    "braces {} should be used on all arms of this statement\n" . $herectx);
4255aad4f614SJoe Perches					}
425613214adfSAndy Whitcroft				}
425713214adfSAndy Whitcroft			}
425813214adfSAndy Whitcroft		}
4259773647a0SAndy Whitcroft		if (!defined $suppress_ifbraces{$linenr - 1} &&
426013214adfSAndy Whitcroft					$line =~ /\b(if|while|for|else)\b/) {
4261cf655043SAndy Whitcroft			my $allowed = 0;
4262f0a594c1SAndy Whitcroft
4263cf655043SAndy Whitcroft			# Check the pre-context.
4264cf655043SAndy Whitcroft			if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
4265cf655043SAndy Whitcroft				#print "APW: ALLOWED: pre<$1>\n";
4266cf655043SAndy Whitcroft				$allowed = 1;
4267f0a594c1SAndy Whitcroft			}
4268773647a0SAndy Whitcroft
4269773647a0SAndy Whitcroft			my ($level, $endln, @chunks) =
4270773647a0SAndy Whitcroft				ctx_statement_full($linenr, $realcnt, $-[0]);
4271773647a0SAndy Whitcroft
4272cf655043SAndy Whitcroft			# Check the condition.
4273cf655043SAndy Whitcroft			my ($cond, $block) = @{$chunks[0]};
4274773647a0SAndy Whitcroft			#print "CHECKING<$linenr> cond<$cond> block<$block>\n";
4275cf655043SAndy Whitcroft			if (defined $cond) {
4276773647a0SAndy Whitcroft				substr($block, 0, length($cond), '');
4277cf655043SAndy Whitcroft			}
4278cf655043SAndy Whitcroft			if (statement_lines($cond) > 1) {
4279cf655043SAndy Whitcroft				#print "APW: ALLOWED: cond<$cond>\n";
4280cf655043SAndy Whitcroft				$allowed = 1;
4281cf655043SAndy Whitcroft			}
4282cf655043SAndy Whitcroft			if ($block =~/\b(?:if|for|while)\b/) {
4283cf655043SAndy Whitcroft				#print "APW: ALLOWED: block<$block>\n";
4284cf655043SAndy Whitcroft				$allowed = 1;
4285cf655043SAndy Whitcroft			}
4286cf655043SAndy Whitcroft			if (statement_block_size($block) > 1) {
4287cf655043SAndy Whitcroft				#print "APW: ALLOWED: lines block<$block>\n";
4288cf655043SAndy Whitcroft				$allowed = 1;
4289cf655043SAndy Whitcroft			}
4290cf655043SAndy Whitcroft			# Check the post-context.
4291cf655043SAndy Whitcroft			if (defined $chunks[1]) {
4292cf655043SAndy Whitcroft				my ($cond, $block) = @{$chunks[1]};
4293cf655043SAndy Whitcroft				if (defined $cond) {
4294773647a0SAndy Whitcroft					substr($block, 0, length($cond), '');
4295cf655043SAndy Whitcroft				}
4296cf655043SAndy Whitcroft				if ($block =~ /^\s*\{/) {
4297cf655043SAndy Whitcroft					#print "APW: ALLOWED: chunk-1 block<$block>\n";
4298cf655043SAndy Whitcroft					$allowed = 1;
4299cf655043SAndy Whitcroft				}
4300cf655043SAndy Whitcroft			}
4301cf655043SAndy Whitcroft			if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
430269932487SJustin P. Mattock				my $herectx = $here . "\n";
4303f055663cSAndy Whitcroft				my $cnt = statement_rawlines($block);
4304cf655043SAndy Whitcroft
4305f055663cSAndy Whitcroft				for (my $n = 0; $n < $cnt; $n++) {
430669932487SJustin P. Mattock					$herectx .= raw_line($linenr, $n) . "\n";
4307cf655043SAndy Whitcroft				}
4308cf655043SAndy Whitcroft
4309000d1cc1SJoe Perches				WARN("BRACES",
4310000d1cc1SJoe Perches				     "braces {} are not necessary for single statement blocks\n" . $herectx);
4311f0a594c1SAndy Whitcroft			}
4312f0a594c1SAndy Whitcroft		}
4313f0a594c1SAndy Whitcroft
43140979ae66SJoe Perches# check for unnecessary blank lines around braces
431577b9a53aSJoe Perches		if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
43160979ae66SJoe Perches			CHK("BRACES",
43170979ae66SJoe Perches			    "Blank lines aren't necessary before a close brace '}'\n" . $hereprev);
43180979ae66SJoe Perches		}
431977b9a53aSJoe Perches		if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
43200979ae66SJoe Perches			CHK("BRACES",
43210979ae66SJoe Perches			    "Blank lines aren't necessary after an open brace '{'\n" . $hereprev);
43220979ae66SJoe Perches		}
43230979ae66SJoe Perches
43244a0df2efSAndy Whitcroft# no volatiles please
43256c72ffaaSAndy Whitcroft		my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
43266c72ffaaSAndy Whitcroft		if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
4327000d1cc1SJoe Perches			WARN("VOLATILE",
4328000d1cc1SJoe Perches			     "Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
43294a0df2efSAndy Whitcroft		}
43304a0df2efSAndy Whitcroft
433100df344fSAndy Whitcroft# warn about #if 0
4332c45dcabdSAndy Whitcroft		if ($line =~ /^.\s*\#\s*if\s+0\b/) {
4333000d1cc1SJoe Perches			CHK("REDUNDANT_CODE",
4334000d1cc1SJoe Perches			    "if this code is redundant consider removing it\n" .
4335de7d4f0eSAndy Whitcroft				$herecurr);
43364a0df2efSAndy Whitcroft		}
43374a0df2efSAndy Whitcroft
433803df4b51SAndy Whitcroft# check for needless "if (<foo>) fn(<foo>)" uses
433903df4b51SAndy Whitcroft		if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
434003df4b51SAndy Whitcroft			my $expr = '\s*\(\s*' . quotemeta($1) . '\s*\)\s*;';
434103df4b51SAndy Whitcroft			if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?)$expr/) {
434203df4b51SAndy Whitcroft				WARN('NEEDLESS_IF',
434303df4b51SAndy Whitcroft				     "$1(NULL) is safe this check is probably not required\n" . $hereprev);
43444c432a8fSGreg Kroah-Hartman			}
43454c432a8fSGreg Kroah-Hartman		}
4346f0a594c1SAndy Whitcroft
4347ebfdc409SJoe Perches# check for unnecessary "Out of Memory" messages
4348ebfdc409SJoe Perches		if ($line =~ /^\+.*\b$logFunctions\s*\(/ &&
4349ebfdc409SJoe Perches		    $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ &&
4350ebfdc409SJoe Perches		    (defined $1 || defined $3) &&
4351ebfdc409SJoe Perches		    $linenr > 3) {
4352ebfdc409SJoe Perches			my $testval = $2;
4353ebfdc409SJoe Perches			my $testline = $lines[$linenr - 3];
4354ebfdc409SJoe Perches
4355ebfdc409SJoe Perches			my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0);
4356ebfdc409SJoe Perches#			print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n");
4357ebfdc409SJoe Perches
4358ebfdc409SJoe 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)/) {
4359ebfdc409SJoe Perches				WARN("OOM_MESSAGE",
4360ebfdc409SJoe Perches				     "Possible unnecessary 'out of memory' message\n" . $hereprev);
4361ebfdc409SJoe Perches			}
4362ebfdc409SJoe Perches		}
4363ebfdc409SJoe Perches
43648716de38SJoe Perches# check for bad placement of section $InitAttribute (e.g.: __initdata)
43658716de38SJoe Perches		if ($line =~ /(\b$InitAttribute\b)/) {
43668716de38SJoe Perches			my $attr = $1;
43678716de38SJoe Perches			if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
43688716de38SJoe Perches				my $ptr = $1;
43698716de38SJoe Perches				my $var = $2;
43708716de38SJoe Perches				if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
43718716de38SJoe Perches				      ERROR("MISPLACED_INIT",
43728716de38SJoe Perches					    "$attr should be placed after $var\n" . $herecurr)) ||
43738716de38SJoe Perches				     ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
43748716de38SJoe Perches				      WARN("MISPLACED_INIT",
43758716de38SJoe Perches					   "$attr should be placed after $var\n" . $herecurr))) &&
43768716de38SJoe Perches				    $fix) {
4377194f66fcSJoe 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;
43788716de38SJoe Perches				}
43798716de38SJoe Perches			}
43808716de38SJoe Perches		}
43818716de38SJoe Perches
4382e970b884SJoe Perches# check for $InitAttributeData (ie: __initdata) with const
4383e970b884SJoe Perches		if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
4384e970b884SJoe Perches			my $attr = $1;
4385e970b884SJoe Perches			$attr =~ /($InitAttributePrefix)(.*)/;
4386e970b884SJoe Perches			my $attr_prefix = $1;
4387e970b884SJoe Perches			my $attr_type = $2;
4388e970b884SJoe Perches			if (ERROR("INIT_ATTRIBUTE",
4389e970b884SJoe Perches				  "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
4390e970b884SJoe Perches			    $fix) {
4391194f66fcSJoe Perches				$fixed[$fixlinenr] =~
4392e970b884SJoe Perches				    s/$InitAttributeData/${attr_prefix}initconst/;
4393e970b884SJoe Perches			}
4394e970b884SJoe Perches		}
4395e970b884SJoe Perches
4396e970b884SJoe Perches# check for $InitAttributeConst (ie: __initconst) without const
4397e970b884SJoe Perches		if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
4398e970b884SJoe Perches			my $attr = $1;
4399e970b884SJoe Perches			if (ERROR("INIT_ATTRIBUTE",
4400e970b884SJoe Perches				  "Use of $attr requires a separate use of const\n" . $herecurr) &&
4401e970b884SJoe Perches			    $fix) {
4402194f66fcSJoe Perches				my $lead = $fixed[$fixlinenr] =~
4403e970b884SJoe Perches				    /(^\+\s*(?:static\s+))/;
4404e970b884SJoe Perches				$lead = rtrim($1);
4405e970b884SJoe Perches				$lead = "$lead " if ($lead !~ /^\+$/);
4406e970b884SJoe Perches				$lead = "${lead}const ";
4407194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/;
4408e970b884SJoe Perches			}
4409e970b884SJoe Perches		}
4410e970b884SJoe Perches
4411fbdb8138SJoe Perches# don't use __constant_<foo> functions outside of include/uapi/
4412fbdb8138SJoe Perches		if ($realfile !~ m@^include/uapi/@ &&
4413fbdb8138SJoe Perches		    $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) {
4414fbdb8138SJoe Perches			my $constant_func = $1;
4415fbdb8138SJoe Perches			my $func = $constant_func;
4416fbdb8138SJoe Perches			$func =~ s/^__constant_//;
4417fbdb8138SJoe Perches			if (WARN("CONSTANT_CONVERSION",
4418fbdb8138SJoe Perches				 "$constant_func should be $func\n" . $herecurr) &&
4419fbdb8138SJoe Perches			    $fix) {
4420194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g;
4421fbdb8138SJoe Perches			}
4422fbdb8138SJoe Perches		}
4423fbdb8138SJoe Perches
44241a15a250SPatrick Pannuto# prefer usleep_range over udelay
442537581c28SBruce Allan		if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
442643c1d77cSJoe Perches			my $delay = $1;
44271a15a250SPatrick Pannuto			# ignore udelay's < 10, however
442843c1d77cSJoe Perches			if (! ($delay < 10) ) {
4429000d1cc1SJoe Perches				CHK("USLEEP_RANGE",
443043c1d77cSJoe Perches				    "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $herecurr);
443143c1d77cSJoe Perches			}
443243c1d77cSJoe Perches			if ($delay > 2000) {
443343c1d77cSJoe Perches				WARN("LONG_UDELAY",
443443c1d77cSJoe Perches				     "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr);
44351a15a250SPatrick Pannuto			}
44361a15a250SPatrick Pannuto		}
44371a15a250SPatrick Pannuto
443809ef8725SPatrick Pannuto# warn about unexpectedly long msleep's
443909ef8725SPatrick Pannuto		if ($line =~ /\bmsleep\s*\((\d+)\);/) {
444009ef8725SPatrick Pannuto			if ($1 < 20) {
4441000d1cc1SJoe Perches				WARN("MSLEEP",
444243c1d77cSJoe Perches				     "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $herecurr);
444309ef8725SPatrick Pannuto			}
444409ef8725SPatrick Pannuto		}
444509ef8725SPatrick Pannuto
444636ec1939SJoe Perches# check for comparisons of jiffies
444736ec1939SJoe Perches		if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
444836ec1939SJoe Perches			WARN("JIFFIES_COMPARISON",
444936ec1939SJoe Perches			     "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
445036ec1939SJoe Perches		}
445136ec1939SJoe Perches
44529d7a34a5SJoe Perches# check for comparisons of get_jiffies_64()
44539d7a34a5SJoe Perches		if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
44549d7a34a5SJoe Perches			WARN("JIFFIES_COMPARISON",
44559d7a34a5SJoe Perches			     "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
44569d7a34a5SJoe Perches		}
44579d7a34a5SJoe Perches
445800df344fSAndy Whitcroft# warn about #ifdefs in C files
4459c45dcabdSAndy Whitcroft#		if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
446000df344fSAndy Whitcroft#			print "#ifdef in C files should be avoided\n";
446100df344fSAndy Whitcroft#			print "$herecurr";
446200df344fSAndy Whitcroft#			$clean = 0;
446300df344fSAndy Whitcroft#		}
446400df344fSAndy Whitcroft
446522f2a2efSAndy Whitcroft# warn about spacing in #ifdefs
4466c45dcabdSAndy Whitcroft		if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
44673705ce5bSJoe Perches			if (ERROR("SPACING",
44683705ce5bSJoe Perches				  "exactly one space required after that #$1\n" . $herecurr) &&
44693705ce5bSJoe Perches			    $fix) {
4470194f66fcSJoe Perches				$fixed[$fixlinenr] =~
44713705ce5bSJoe Perches				    s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
44723705ce5bSJoe Perches			}
44733705ce5bSJoe Perches
447422f2a2efSAndy Whitcroft		}
447522f2a2efSAndy Whitcroft
44764a0df2efSAndy Whitcroft# check for spinlock_t definitions without a comment.
4477171ae1a4SAndy Whitcroft		if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
4478171ae1a4SAndy Whitcroft		    $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
44794a0df2efSAndy Whitcroft			my $which = $1;
44804a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
4481000d1cc1SJoe Perches				CHK("UNCOMMENTED_DEFINITION",
4482000d1cc1SJoe Perches				    "$1 definition without comment\n" . $herecurr);
44834a0df2efSAndy Whitcroft			}
44844a0df2efSAndy Whitcroft		}
44854a0df2efSAndy Whitcroft# check for memory barriers without a comment.
44864a0df2efSAndy Whitcroft		if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
44874a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
4488c1fd7bb9SJoe Perches				WARN("MEMORY_BARRIER",
4489000d1cc1SJoe Perches				     "memory barrier without comment\n" . $herecurr);
44904a0df2efSAndy Whitcroft			}
44914a0df2efSAndy Whitcroft		}
44924a0df2efSAndy Whitcroft# check of hardware specific defines
4493c45dcabdSAndy Whitcroft		if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
4494000d1cc1SJoe Perches			CHK("ARCH_DEFINES",
4495000d1cc1SJoe Perches			    "architecture specific defines should be avoided\n" .  $herecurr);
44960a920b5bSAndy Whitcroft		}
4497653d4876SAndy Whitcroft
4498d4977c78STobias Klauser# Check that the storage class is at the beginning of a declaration
4499d4977c78STobias Klauser		if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
4500000d1cc1SJoe Perches			WARN("STORAGE_CLASS",
4501000d1cc1SJoe Perches			     "storage class should be at the beginning of the declaration\n" . $herecurr)
4502d4977c78STobias Klauser		}
4503d4977c78STobias Klauser
4504de7d4f0eSAndy Whitcroft# check the location of the inline attribute, that it is between
4505de7d4f0eSAndy Whitcroft# storage class and type.
45069c0ca6f9SAndy Whitcroft		if ($line =~ /\b$Type\s+$Inline\b/ ||
45079c0ca6f9SAndy Whitcroft		    $line =~ /\b$Inline\s+$Storage\b/) {
4508000d1cc1SJoe Perches			ERROR("INLINE_LOCATION",
4509000d1cc1SJoe Perches			      "inline keyword should sit between storage class and type\n" . $herecurr);
4510de7d4f0eSAndy Whitcroft		}
4511de7d4f0eSAndy Whitcroft
45128905a67cSAndy Whitcroft# Check for __inline__ and __inline, prefer inline
45132b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
45142b7ab453SJoe Perches		    $line =~ /\b(__inline__|__inline)\b/) {
4515d5e616fcSJoe Perches			if (WARN("INLINE",
4516d5e616fcSJoe Perches				 "plain inline is preferred over $1\n" . $herecurr) &&
4517d5e616fcSJoe Perches			    $fix) {
4518194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/;
4519d5e616fcSJoe Perches
4520d5e616fcSJoe Perches			}
45218905a67cSAndy Whitcroft		}
45228905a67cSAndy Whitcroft
45233d130fd0SJoe Perches# Check for __attribute__ packed, prefer __packed
45242b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
45252b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
4526000d1cc1SJoe Perches			WARN("PREFER_PACKED",
4527000d1cc1SJoe Perches			     "__packed is preferred over __attribute__((packed))\n" . $herecurr);
45283d130fd0SJoe Perches		}
45293d130fd0SJoe Perches
453039b7e287SJoe Perches# Check for __attribute__ aligned, prefer __aligned
45312b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
45322b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
4533000d1cc1SJoe Perches			WARN("PREFER_ALIGNED",
4534000d1cc1SJoe Perches			     "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
453539b7e287SJoe Perches		}
453639b7e287SJoe Perches
45375f14d3bdSJoe Perches# Check for __attribute__ format(printf, prefer __printf
45382b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
45392b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
4540d5e616fcSJoe Perches			if (WARN("PREFER_PRINTF",
4541d5e616fcSJoe Perches				 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) &&
4542d5e616fcSJoe Perches			    $fix) {
4543194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf\s*,\s*(.*)\)\s*\)\s*\)/"__printf(" . trim($1) . ")"/ex;
4544d5e616fcSJoe Perches
4545d5e616fcSJoe Perches			}
45465f14d3bdSJoe Perches		}
45475f14d3bdSJoe Perches
45486061d949SJoe Perches# Check for __attribute__ format(scanf, prefer __scanf
45492b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
45502b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
4551d5e616fcSJoe Perches			if (WARN("PREFER_SCANF",
4552d5e616fcSJoe Perches				 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) &&
4553d5e616fcSJoe Perches			    $fix) {
4554194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\s*,\s*(.*)\)\s*\)\s*\)/"__scanf(" . trim($1) . ")"/ex;
4555d5e616fcSJoe Perches			}
45566061d949SJoe Perches		}
45576061d949SJoe Perches
45588f53a9b8SJoe Perches# check for sizeof(&)
45598f53a9b8SJoe Perches		if ($line =~ /\bsizeof\s*\(\s*\&/) {
4560000d1cc1SJoe Perches			WARN("SIZEOF_ADDRESS",
4561000d1cc1SJoe Perches			     "sizeof(& should be avoided\n" . $herecurr);
45628f53a9b8SJoe Perches		}
45638f53a9b8SJoe Perches
456466c80b60SJoe Perches# check for sizeof without parenthesis
456566c80b60SJoe Perches		if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
4566d5e616fcSJoe Perches			if (WARN("SIZEOF_PARENTHESIS",
4567d5e616fcSJoe Perches				 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
4568d5e616fcSJoe Perches			    $fix) {
4569194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
4570d5e616fcSJoe Perches			}
457166c80b60SJoe Perches		}
457266c80b60SJoe Perches
4573428e2fdcSJoe Perches# check for line continuations in quoted strings with odd counts of "
4574428e2fdcSJoe Perches		if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) {
4575000d1cc1SJoe Perches			WARN("LINE_CONTINUATIONS",
4576000d1cc1SJoe Perches			     "Avoid line continuations in quoted strings\n" . $herecurr);
4577428e2fdcSJoe Perches		}
4578428e2fdcSJoe Perches
457988982feaSJoe Perches# check for struct spinlock declarations
458088982feaSJoe Perches		if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
458188982feaSJoe Perches			WARN("USE_SPINLOCK_T",
458288982feaSJoe Perches			     "struct spinlock should be spinlock_t\n" . $herecurr);
458388982feaSJoe Perches		}
458488982feaSJoe Perches
4585a6962d72SJoe Perches# check for seq_printf uses that could be seq_puts
458606668727SJoe Perches		if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
4587a6962d72SJoe Perches			my $fmt = get_quoted_string($line, $rawline);
458806668727SJoe Perches			if ($fmt ne "" && $fmt !~ /[^\\]\%/) {
4589d5e616fcSJoe Perches				if (WARN("PREFER_SEQ_PUTS",
4590d5e616fcSJoe Perches					 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
4591d5e616fcSJoe Perches				    $fix) {
4592194f66fcSJoe Perches					$fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/;
4593d5e616fcSJoe Perches				}
4594a6962d72SJoe Perches			}
4595a6962d72SJoe Perches		}
4596a6962d72SJoe Perches
4597554e165cSAndy Whitcroft# Check for misused memsets
4598d1fe9c09SJoe Perches		if ($^V && $^V ge 5.10.0 &&
4599d1fe9c09SJoe Perches		    defined $stat &&
4600d7c76ba7SJoe Perches		    $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/s) {
4601554e165cSAndy Whitcroft
4602d7c76ba7SJoe Perches			my $ms_addr = $2;
4603d1fe9c09SJoe Perches			my $ms_val = $7;
4604d1fe9c09SJoe Perches			my $ms_size = $12;
4605d7c76ba7SJoe Perches
4606554e165cSAndy Whitcroft			if ($ms_size =~ /^(0x|)0$/i) {
4607554e165cSAndy Whitcroft				ERROR("MEMSET",
4608d7c76ba7SJoe Perches				      "memset to 0's uses 0 as the 2nd argument, not the 3rd\n" . "$here\n$stat\n");
4609554e165cSAndy Whitcroft			} elsif ($ms_size =~ /^(0x|)1$/i) {
4610554e165cSAndy Whitcroft				WARN("MEMSET",
4611d7c76ba7SJoe Perches				     "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
4612d7c76ba7SJoe Perches			}
4613d7c76ba7SJoe Perches		}
4614d7c76ba7SJoe Perches
461598a9bba5SJoe Perches# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar)
461698a9bba5SJoe Perches		if ($^V && $^V ge 5.10.0 &&
461798a9bba5SJoe Perches		    $line =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/s) {
461898a9bba5SJoe Perches			if (WARN("PREFER_ETHER_ADDR_COPY",
461998a9bba5SJoe Perches				 "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . $herecurr) &&
462098a9bba5SJoe Perches			    $fix) {
4621194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/;
462298a9bba5SJoe Perches			}
462398a9bba5SJoe Perches		}
462498a9bba5SJoe Perches
4625d7c76ba7SJoe Perches# typecasts on min/max could be min_t/max_t
4626d1fe9c09SJoe Perches		if ($^V && $^V ge 5.10.0 &&
4627d1fe9c09SJoe Perches		    defined $stat &&
4628d7c76ba7SJoe Perches		    $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
4629d1fe9c09SJoe Perches			if (defined $2 || defined $7) {
4630d7c76ba7SJoe Perches				my $call = $1;
4631d7c76ba7SJoe Perches				my $cast1 = deparenthesize($2);
4632d7c76ba7SJoe Perches				my $arg1 = $3;
4633d1fe9c09SJoe Perches				my $cast2 = deparenthesize($7);
4634d1fe9c09SJoe Perches				my $arg2 = $8;
4635d7c76ba7SJoe Perches				my $cast;
4636d7c76ba7SJoe Perches
4637d1fe9c09SJoe Perches				if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
4638d7c76ba7SJoe Perches					$cast = "$cast1 or $cast2";
4639d7c76ba7SJoe Perches				} elsif ($cast1 ne "") {
4640d7c76ba7SJoe Perches					$cast = $cast1;
4641d7c76ba7SJoe Perches				} else {
4642d7c76ba7SJoe Perches					$cast = $cast2;
4643d7c76ba7SJoe Perches				}
4644d7c76ba7SJoe Perches				WARN("MINMAX",
4645d7c76ba7SJoe Perches				     "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
4646554e165cSAndy Whitcroft			}
4647554e165cSAndy Whitcroft		}
4648554e165cSAndy Whitcroft
46494a273195SJoe Perches# check usleep_range arguments
46504a273195SJoe Perches		if ($^V && $^V ge 5.10.0 &&
46514a273195SJoe Perches		    defined $stat &&
46524a273195SJoe Perches		    $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
46534a273195SJoe Perches			my $min = $1;
46544a273195SJoe Perches			my $max = $7;
46554a273195SJoe Perches			if ($min eq $max) {
46564a273195SJoe Perches				WARN("USLEEP_RANGE",
46574a273195SJoe Perches				     "usleep_range should not use min == max args; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
46584a273195SJoe Perches			} elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
46594a273195SJoe Perches				 $min > $max) {
46604a273195SJoe Perches				WARN("USLEEP_RANGE",
46614a273195SJoe Perches				     "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
46624a273195SJoe Perches			}
46634a273195SJoe Perches		}
46644a273195SJoe Perches
4665823b794cSJoe Perches# check for naked sscanf
4666823b794cSJoe Perches		if ($^V && $^V ge 5.10.0 &&
4667823b794cSJoe Perches		    defined $stat &&
46686c8bd707SJoe Perches		    $line =~ /\bsscanf\b/ &&
4669823b794cSJoe Perches		    ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
4670823b794cSJoe Perches		     $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ &&
4671823b794cSJoe Perches		     $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) {
4672823b794cSJoe Perches			my $lc = $stat =~ tr@\n@@;
4673823b794cSJoe Perches			$lc = $lc + $linenr;
4674823b794cSJoe Perches			my $stat_real = raw_line($linenr, 0);
4675823b794cSJoe Perches		        for (my $count = $linenr + 1; $count <= $lc; $count++) {
4676823b794cSJoe Perches				$stat_real = $stat_real . "\n" . raw_line($count, 0);
4677823b794cSJoe Perches			}
4678823b794cSJoe Perches			WARN("NAKED_SSCANF",
4679823b794cSJoe Perches			     "unchecked sscanf return value\n" . "$here\n$stat_real\n");
4680823b794cSJoe Perches		}
4681823b794cSJoe Perches
4682afc819abSJoe Perches# check for simple sscanf that should be kstrto<foo>
4683afc819abSJoe Perches		if ($^V && $^V ge 5.10.0 &&
4684afc819abSJoe Perches		    defined $stat &&
4685afc819abSJoe Perches		    $line =~ /\bsscanf\b/) {
4686afc819abSJoe Perches			my $lc = $stat =~ tr@\n@@;
4687afc819abSJoe Perches			$lc = $lc + $linenr;
4688afc819abSJoe Perches			my $stat_real = raw_line($linenr, 0);
4689afc819abSJoe Perches		        for (my $count = $linenr + 1; $count <= $lc; $count++) {
4690afc819abSJoe Perches				$stat_real = $stat_real . "\n" . raw_line($count, 0);
4691afc819abSJoe Perches			}
4692afc819abSJoe Perches			if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) {
4693afc819abSJoe Perches				my $format = $6;
4694afc819abSJoe Perches				my $count = $format =~ tr@%@%@;
4695afc819abSJoe Perches				if ($count == 1 &&
4696afc819abSJoe Perches				    $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) {
4697afc819abSJoe Perches					WARN("SSCANF_TO_KSTRTO",
4698afc819abSJoe Perches					     "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n");
4699afc819abSJoe Perches				}
4700afc819abSJoe Perches			}
4701afc819abSJoe Perches		}
4702afc819abSJoe Perches
470370dc8a48SJoe Perches# check for new externs in .h files.
470470dc8a48SJoe Perches		if ($realfile =~ /\.h$/ &&
470570dc8a48SJoe Perches		    $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
4706d1d85780SJoe Perches			if (CHK("AVOID_EXTERNS",
470770dc8a48SJoe Perches				"extern prototypes should be avoided in .h files\n" . $herecurr) &&
470870dc8a48SJoe Perches			    $fix) {
4709194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
471070dc8a48SJoe Perches			}
471170dc8a48SJoe Perches		}
471270dc8a48SJoe Perches
4713de7d4f0eSAndy Whitcroft# check for new externs in .c files.
4714171ae1a4SAndy Whitcroft		if ($realfile =~ /\.c$/ && defined $stat &&
4715c45dcabdSAndy Whitcroft		    $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
4716171ae1a4SAndy Whitcroft		{
4717c45dcabdSAndy Whitcroft			my $function_name = $1;
4718c45dcabdSAndy Whitcroft			my $paren_space = $2;
4719171ae1a4SAndy Whitcroft
4720171ae1a4SAndy Whitcroft			my $s = $stat;
4721171ae1a4SAndy Whitcroft			if (defined $cond) {
4722171ae1a4SAndy Whitcroft				substr($s, 0, length($cond), '');
4723171ae1a4SAndy Whitcroft			}
4724c45dcabdSAndy Whitcroft			if ($s =~ /^\s*;/ &&
4725c45dcabdSAndy Whitcroft			    $function_name ne 'uninitialized_var')
4726c45dcabdSAndy Whitcroft			{
4727000d1cc1SJoe Perches				WARN("AVOID_EXTERNS",
4728000d1cc1SJoe Perches				     "externs should be avoided in .c files\n" .  $herecurr);
4729de7d4f0eSAndy Whitcroft			}
4730de7d4f0eSAndy Whitcroft
4731171ae1a4SAndy Whitcroft			if ($paren_space =~ /\n/) {
4732000d1cc1SJoe Perches				WARN("FUNCTION_ARGUMENTS",
4733000d1cc1SJoe Perches				     "arguments for function declarations should follow identifier\n" . $herecurr);
4734171ae1a4SAndy Whitcroft			}
47359c9ba34eSAndy Whitcroft
47369c9ba34eSAndy Whitcroft		} elsif ($realfile =~ /\.c$/ && defined $stat &&
47379c9ba34eSAndy Whitcroft		    $stat =~ /^.\s*extern\s+/)
47389c9ba34eSAndy Whitcroft		{
4739000d1cc1SJoe Perches			WARN("AVOID_EXTERNS",
4740000d1cc1SJoe Perches			     "externs should be avoided in .c files\n" .  $herecurr);
4741171ae1a4SAndy Whitcroft		}
4742171ae1a4SAndy Whitcroft
4743de7d4f0eSAndy Whitcroft# checks for new __setup's
4744de7d4f0eSAndy Whitcroft		if ($rawline =~ /\b__setup\("([^"]*)"/) {
4745de7d4f0eSAndy Whitcroft			my $name = $1;
4746de7d4f0eSAndy Whitcroft
4747de7d4f0eSAndy Whitcroft			if (!grep(/$name/, @setup_docs)) {
4748000d1cc1SJoe Perches				CHK("UNDOCUMENTED_SETUP",
4749000d1cc1SJoe Perches				    "__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
4750de7d4f0eSAndy Whitcroft			}
4751653d4876SAndy Whitcroft		}
47529c0ca6f9SAndy Whitcroft
47539c0ca6f9SAndy Whitcroft# check for pointless casting of kmalloc return
4754caf2a54fSJoe Perches		if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) {
4755000d1cc1SJoe Perches			WARN("UNNECESSARY_CASTS",
4756000d1cc1SJoe Perches			     "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
47579c0ca6f9SAndy Whitcroft		}
475813214adfSAndy Whitcroft
4759a640d25cSJoe Perches# alloc style
4760a640d25cSJoe Perches# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
4761a640d25cSJoe Perches		if ($^V && $^V ge 5.10.0 &&
4762a640d25cSJoe Perches		    $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*([kv][mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
4763a640d25cSJoe Perches			CHK("ALLOC_SIZEOF_STRUCT",
4764a640d25cSJoe Perches			    "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
4765a640d25cSJoe Perches		}
4766a640d25cSJoe Perches
476760a55369SJoe Perches# check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc
476860a55369SJoe Perches		if ($^V && $^V ge 5.10.0 &&
4769e367455aSJoe Perches		    $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*(k[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)\s*,/) {
477060a55369SJoe Perches			my $oldfunc = $3;
477160a55369SJoe Perches			my $a1 = $4;
477260a55369SJoe Perches			my $a2 = $10;
477360a55369SJoe Perches			my $newfunc = "kmalloc_array";
477460a55369SJoe Perches			$newfunc = "kcalloc" if ($oldfunc eq "kzalloc");
477560a55369SJoe Perches			my $r1 = $a1;
477660a55369SJoe Perches			my $r2 = $a2;
477760a55369SJoe Perches			if ($a1 =~ /^sizeof\s*\S/) {
477860a55369SJoe Perches				$r1 = $a2;
477960a55369SJoe Perches				$r2 = $a1;
478060a55369SJoe Perches			}
4781e367455aSJoe Perches			if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ &&
4782e367455aSJoe Perches			    !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) {
4783e367455aSJoe Perches				if (WARN("ALLOC_WITH_MULTIPLY",
4784e367455aSJoe Perches					 "Prefer $newfunc over $oldfunc with multiply\n" . $herecurr) &&
4785e367455aSJoe Perches				    $fix) {
4786194f66fcSJoe 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;
478760a55369SJoe Perches
478860a55369SJoe Perches				}
478960a55369SJoe Perches			}
479060a55369SJoe Perches		}
479160a55369SJoe Perches
4792972fdea2SJoe Perches# check for krealloc arg reuse
4793972fdea2SJoe Perches		if ($^V && $^V ge 5.10.0 &&
4794972fdea2SJoe Perches		    $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*\1\s*,/) {
4795972fdea2SJoe Perches			WARN("KREALLOC_ARG_REUSE",
4796972fdea2SJoe Perches			     "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
4797972fdea2SJoe Perches		}
4798972fdea2SJoe Perches
47995ce59ae0SJoe Perches# check for alloc argument mismatch
48005ce59ae0SJoe Perches		if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
48015ce59ae0SJoe Perches			WARN("ALLOC_ARRAY_ARGS",
48025ce59ae0SJoe Perches			     "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
48035ce59ae0SJoe Perches		}
48045ce59ae0SJoe Perches
4805caf2a54fSJoe Perches# check for multiple semicolons
4806caf2a54fSJoe Perches		if ($line =~ /;\s*;\s*$/) {
4807d5e616fcSJoe Perches			if (WARN("ONE_SEMICOLON",
4808d5e616fcSJoe Perches				 "Statements terminations use 1 semicolon\n" . $herecurr) &&
4809d5e616fcSJoe Perches			    $fix) {
4810194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g;
4811d5e616fcSJoe Perches			}
4812d1e2ad07SJoe Perches		}
4813d1e2ad07SJoe Perches
4814c34c09a8SJoe Perches# check for case / default statements not preceeded by break/fallthrough/switch
4815c34c09a8SJoe Perches		if ($line =~ /^.\s*(?:case\s+(?:$Ident|$Constant)\s*|default):/) {
4816c34c09a8SJoe Perches			my $has_break = 0;
4817c34c09a8SJoe Perches			my $has_statement = 0;
4818c34c09a8SJoe Perches			my $count = 0;
4819c34c09a8SJoe Perches			my $prevline = $linenr;
4820c34c09a8SJoe Perches			while ($prevline > 1 && $count < 3 && !$has_break) {
4821c34c09a8SJoe Perches				$prevline--;
4822c34c09a8SJoe Perches				my $rline = $rawlines[$prevline - 1];
4823c34c09a8SJoe Perches				my $fline = $lines[$prevline - 1];
4824c34c09a8SJoe Perches				last if ($fline =~ /^\@\@/);
4825c34c09a8SJoe Perches				next if ($fline =~ /^\-/);
4826c34c09a8SJoe Perches				next if ($fline =~ /^.(?:\s*(?:case\s+(?:$Ident|$Constant)[\s$;]*|default):[\s$;]*)*$/);
4827c34c09a8SJoe Perches				$has_break = 1 if ($rline =~ /fall[\s_-]*(through|thru)/i);
4828c34c09a8SJoe Perches				next if ($fline =~ /^.[\s$;]*$/);
4829c34c09a8SJoe Perches				$has_statement = 1;
4830c34c09a8SJoe Perches				$count++;
4831c34c09a8SJoe Perches				$has_break = 1 if ($fline =~ /\bswitch\b|\b(?:break\s*;[\s$;]*$|return\b|goto\b|continue\b)/);
4832c34c09a8SJoe Perches			}
4833c34c09a8SJoe Perches			if (!$has_break && $has_statement) {
4834c34c09a8SJoe Perches				WARN("MISSING_BREAK",
4835c34c09a8SJoe Perches				     "Possible switch case/default not preceeded by break or fallthrough comment\n" . $herecurr);
4836c34c09a8SJoe Perches			}
4837c34c09a8SJoe Perches		}
4838c34c09a8SJoe Perches
4839d1e2ad07SJoe Perches# check for switch/default statements without a break;
4840d1e2ad07SJoe Perches		if ($^V && $^V ge 5.10.0 &&
4841d1e2ad07SJoe Perches		    defined $stat &&
4842d1e2ad07SJoe Perches		    $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
4843d1e2ad07SJoe Perches			my $ctx = '';
4844d1e2ad07SJoe Perches			my $herectx = $here . "\n";
4845d1e2ad07SJoe Perches			my $cnt = statement_rawlines($stat);
4846d1e2ad07SJoe Perches			for (my $n = 0; $n < $cnt; $n++) {
4847d1e2ad07SJoe Perches				$herectx .= raw_line($linenr, $n) . "\n";
4848d1e2ad07SJoe Perches			}
4849d1e2ad07SJoe Perches			WARN("DEFAULT_NO_BREAK",
4850d1e2ad07SJoe Perches			     "switch default: should use break\n" . $herectx);
4851caf2a54fSJoe Perches		}
4852caf2a54fSJoe Perches
485313214adfSAndy Whitcroft# check for gcc specific __FUNCTION__
4854d5e616fcSJoe Perches		if ($line =~ /\b__FUNCTION__\b/) {
4855d5e616fcSJoe Perches			if (WARN("USE_FUNC",
4856d5e616fcSJoe Perches				 "__func__ should be used instead of gcc specific __FUNCTION__\n"  . $herecurr) &&
4857d5e616fcSJoe Perches			    $fix) {
4858194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g;
4859d5e616fcSJoe Perches			}
486013214adfSAndy Whitcroft		}
4861773647a0SAndy Whitcroft
48622c92488aSJoe Perches# check for use of yield()
48632c92488aSJoe Perches		if ($line =~ /\byield\s*\(\s*\)/) {
48642c92488aSJoe Perches			WARN("YIELD",
48652c92488aSJoe Perches			     "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n"  . $herecurr);
48662c92488aSJoe Perches		}
48672c92488aSJoe Perches
4868179f8f40SJoe Perches# check for comparisons against true and false
4869179f8f40SJoe Perches		if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
4870179f8f40SJoe Perches			my $lead = $1;
4871179f8f40SJoe Perches			my $arg = $2;
4872179f8f40SJoe Perches			my $test = $3;
4873179f8f40SJoe Perches			my $otype = $4;
4874179f8f40SJoe Perches			my $trail = $5;
4875179f8f40SJoe Perches			my $op = "!";
4876179f8f40SJoe Perches
4877179f8f40SJoe Perches			($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
4878179f8f40SJoe Perches
4879179f8f40SJoe Perches			my $type = lc($otype);
4880179f8f40SJoe Perches			if ($type =~ /^(?:true|false)$/) {
4881179f8f40SJoe Perches				if (("$test" eq "==" && "$type" eq "true") ||
4882179f8f40SJoe Perches				    ("$test" eq "!=" && "$type" eq "false")) {
4883179f8f40SJoe Perches					$op = "";
4884179f8f40SJoe Perches				}
4885179f8f40SJoe Perches
4886179f8f40SJoe Perches				CHK("BOOL_COMPARISON",
4887179f8f40SJoe Perches				    "Using comparison to $otype is error prone\n" . $herecurr);
4888179f8f40SJoe Perches
4889179f8f40SJoe Perches## maybe suggesting a correct construct would better
4890179f8f40SJoe Perches##				    "Using comparison to $otype is error prone.  Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
4891179f8f40SJoe Perches
4892179f8f40SJoe Perches			}
4893179f8f40SJoe Perches		}
4894179f8f40SJoe Perches
48954882720bSThomas Gleixner# check for semaphores initialized locked
48964882720bSThomas Gleixner		if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
4897000d1cc1SJoe Perches			WARN("CONSIDER_COMPLETION",
4898000d1cc1SJoe Perches			     "consider using a completion\n" . $herecurr);
4899773647a0SAndy Whitcroft		}
49006712d858SJoe Perches
490167d0a075SJoe Perches# recommend kstrto* over simple_strto* and strict_strto*
490267d0a075SJoe Perches		if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
4903000d1cc1SJoe Perches			WARN("CONSIDER_KSTRTO",
490467d0a075SJoe Perches			     "$1 is obsolete, use k$3 instead\n" . $herecurr);
4905773647a0SAndy Whitcroft		}
49066712d858SJoe Perches
4907ae3ccc46SFabian Frederick# check for __initcall(), use device_initcall() explicitly or more appropriate function please
4908f3db6639SMichael Ellerman		if ($line =~ /^.\s*__initcall\s*\(/) {
4909000d1cc1SJoe Perches			WARN("USE_DEVICE_INITCALL",
4910ae3ccc46SFabian Frederick			     "please use device_initcall() or more appropriate function instead of __initcall() (see include/linux/init.h)\n" . $herecurr);
4911f3db6639SMichael Ellerman		}
49126712d858SJoe Perches
491379404849SEmese Revfy# check for various ops structs, ensure they are const.
491479404849SEmese Revfy		my $struct_ops = qr{acpi_dock_ops|
491579404849SEmese Revfy				address_space_operations|
491679404849SEmese Revfy				backlight_ops|
491779404849SEmese Revfy				block_device_operations|
491879404849SEmese Revfy				dentry_operations|
491979404849SEmese Revfy				dev_pm_ops|
492079404849SEmese Revfy				dma_map_ops|
492179404849SEmese Revfy				extent_io_ops|
492279404849SEmese Revfy				file_lock_operations|
492379404849SEmese Revfy				file_operations|
492479404849SEmese Revfy				hv_ops|
492579404849SEmese Revfy				ide_dma_ops|
492679404849SEmese Revfy				intel_dvo_dev_ops|
492779404849SEmese Revfy				item_operations|
492879404849SEmese Revfy				iwl_ops|
492979404849SEmese Revfy				kgdb_arch|
493079404849SEmese Revfy				kgdb_io|
493179404849SEmese Revfy				kset_uevent_ops|
493279404849SEmese Revfy				lock_manager_operations|
493379404849SEmese Revfy				microcode_ops|
493479404849SEmese Revfy				mtrr_ops|
493579404849SEmese Revfy				neigh_ops|
493679404849SEmese Revfy				nlmsvc_binding|
493779404849SEmese Revfy				pci_raw_ops|
493879404849SEmese Revfy				pipe_buf_operations|
493979404849SEmese Revfy				platform_hibernation_ops|
494079404849SEmese Revfy				platform_suspend_ops|
494179404849SEmese Revfy				proto_ops|
494279404849SEmese Revfy				rpc_pipe_ops|
494379404849SEmese Revfy				seq_operations|
494479404849SEmese Revfy				snd_ac97_build_ops|
494579404849SEmese Revfy				soc_pcmcia_socket_ops|
494679404849SEmese Revfy				stacktrace_ops|
494779404849SEmese Revfy				sysfs_ops|
494879404849SEmese Revfy				tty_operations|
494979404849SEmese Revfy				usb_mon_operations|
495079404849SEmese Revfy				wd_ops}x;
49516903ffb2SAndy Whitcroft		if ($line !~ /\bconst\b/ &&
495279404849SEmese Revfy		    $line =~ /\bstruct\s+($struct_ops)\b/) {
4953000d1cc1SJoe Perches			WARN("CONST_STRUCT",
4954000d1cc1SJoe Perches			     "struct $1 should normally be const\n" .
49556903ffb2SAndy Whitcroft				$herecurr);
49562b6db5cbSAndy Whitcroft		}
4957773647a0SAndy Whitcroft
4958773647a0SAndy Whitcroft# use of NR_CPUS is usually wrong
4959773647a0SAndy Whitcroft# ignore definitions of NR_CPUS and usage to define arrays as likely right
4960773647a0SAndy Whitcroft		if ($line =~ /\bNR_CPUS\b/ &&
4961c45dcabdSAndy Whitcroft		    $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
4962c45dcabdSAndy Whitcroft		    $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
4963171ae1a4SAndy Whitcroft		    $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
4964171ae1a4SAndy Whitcroft		    $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
4965171ae1a4SAndy Whitcroft		    $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
4966773647a0SAndy Whitcroft		{
4967000d1cc1SJoe Perches			WARN("NR_CPUS",
4968000d1cc1SJoe Perches			     "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
4969773647a0SAndy Whitcroft		}
49709c9ba34eSAndy Whitcroft
497152ea8506SJoe Perches# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
497252ea8506SJoe Perches		if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
497352ea8506SJoe Perches			ERROR("DEFINE_ARCH_HAS",
497452ea8506SJoe Perches			      "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
497552ea8506SJoe Perches		}
497652ea8506SJoe Perches
49779c9ba34eSAndy Whitcroft# check for %L{u,d,i} in strings
49789c9ba34eSAndy Whitcroft		my $string;
49799c9ba34eSAndy Whitcroft		while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
49809c9ba34eSAndy Whitcroft			$string = substr($rawline, $-[1], $+[1] - $-[1]);
49812a1bc5d5SAndy Whitcroft			$string =~ s/%%/__/g;
49829c9ba34eSAndy Whitcroft			if ($string =~ /(?<!%)%L[udi]/) {
4983000d1cc1SJoe Perches				WARN("PRINTF_L",
4984000d1cc1SJoe Perches				     "\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
49859c9ba34eSAndy Whitcroft				last;
49869c9ba34eSAndy Whitcroft			}
49879c9ba34eSAndy Whitcroft		}
4988691d77b6SAndy Whitcroft
4989691d77b6SAndy Whitcroft# whine mightly about in_atomic
4990691d77b6SAndy Whitcroft		if ($line =~ /\bin_atomic\s*\(/) {
4991691d77b6SAndy Whitcroft			if ($realfile =~ m@^drivers/@) {
4992000d1cc1SJoe Perches				ERROR("IN_ATOMIC",
4993000d1cc1SJoe Perches				      "do not use in_atomic in drivers\n" . $herecurr);
4994f4a87736SAndy Whitcroft			} elsif ($realfile !~ m@^kernel/@) {
4995000d1cc1SJoe Perches				WARN("IN_ATOMIC",
4996000d1cc1SJoe Perches				     "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
4997691d77b6SAndy Whitcroft			}
4998691d77b6SAndy Whitcroft		}
49991704f47bSPeter Zijlstra
50001704f47bSPeter Zijlstra# check for lockdep_set_novalidate_class
50011704f47bSPeter Zijlstra		if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
50021704f47bSPeter Zijlstra		    $line =~ /__lockdep_no_validate__\s*\)/ ) {
50031704f47bSPeter Zijlstra			if ($realfile !~ m@^kernel/lockdep@ &&
50041704f47bSPeter Zijlstra			    $realfile !~ m@^include/linux/lockdep@ &&
50051704f47bSPeter Zijlstra			    $realfile !~ m@^drivers/base/core@) {
5006000d1cc1SJoe Perches				ERROR("LOCKDEP",
5007000d1cc1SJoe Perches				      "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
50081704f47bSPeter Zijlstra			}
50091704f47bSPeter Zijlstra		}
501088f8831cSDave Jones
501188f8831cSDave Jones		if ($line =~ /debugfs_create_file.*S_IWUGO/ ||
501288f8831cSDave Jones		    $line =~ /DEVICE_ATTR.*S_IWUGO/ ) {
5013000d1cc1SJoe Perches			WARN("EXPORTED_WORLD_WRITABLE",
5014000d1cc1SJoe Perches			     "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
501588f8831cSDave Jones		}
50162435880fSJoe Perches
5017515a235eSJoe Perches# Mode permission misuses where it seems decimal should be octal
5018515a235eSJoe Perches# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop
5019515a235eSJoe Perches		if ($^V && $^V ge 5.10.0 &&
5020515a235eSJoe Perches		    $line =~ /$mode_perms_search/) {
50212435880fSJoe Perches			foreach my $entry (@mode_permission_funcs) {
50222435880fSJoe Perches				my $func = $entry->[0];
50232435880fSJoe Perches				my $arg_pos = $entry->[1];
50242435880fSJoe Perches
50252435880fSJoe Perches				my $skip_args = "";
50262435880fSJoe Perches				if ($arg_pos > 1) {
50272435880fSJoe Perches					$arg_pos--;
50282435880fSJoe Perches					$skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}";
50292435880fSJoe Perches				}
50302435880fSJoe Perches				my $test = "\\b$func\\s*\\(${skip_args}([\\d]+)\\s*[,\\)]";
5031515a235eSJoe Perches				if ($line =~ /$test/) {
50322435880fSJoe Perches					my $val = $1;
50332435880fSJoe Perches					$val = $6 if ($skip_args ne "");
50342435880fSJoe Perches
50351727cc70SJoe Perches					if ($val !~ /^0$/ &&
50361727cc70SJoe Perches					    (($val =~ /^$Int$/ && $val !~ /^$Octal$/) ||
50371727cc70SJoe Perches					     length($val) ne 4)) {
50382435880fSJoe Perches						ERROR("NON_OCTAL_PERMISSIONS",
50391727cc70SJoe Perches						      "Use 4 digit octal (0777) not decimal permissions\n" . $herecurr);
50402435880fSJoe Perches					}
50412435880fSJoe Perches				}
50422435880fSJoe Perches			}
504313214adfSAndy Whitcroft		}
5044515a235eSJoe Perches	}
504513214adfSAndy Whitcroft
504613214adfSAndy Whitcroft	# If we have no input at all, then there is nothing to report on
504713214adfSAndy Whitcroft	# so just keep quiet.
504813214adfSAndy Whitcroft	if ($#rawlines == -1) {
504913214adfSAndy Whitcroft		exit(0);
50500a920b5bSAndy Whitcroft	}
50510a920b5bSAndy Whitcroft
50528905a67cSAndy Whitcroft	# In mailback mode only produce a report in the negative, for
50538905a67cSAndy Whitcroft	# things that appear to be patches.
50548905a67cSAndy Whitcroft	if ($mailback && ($clean == 1 || !$is_patch)) {
50558905a67cSAndy Whitcroft		exit(0);
50568905a67cSAndy Whitcroft	}
50578905a67cSAndy Whitcroft
50588905a67cSAndy Whitcroft	# This is not a patch, and we are are in 'no-patch' mode so
50598905a67cSAndy Whitcroft	# just keep quiet.
50608905a67cSAndy Whitcroft	if (!$chk_patch && !$is_patch) {
50618905a67cSAndy Whitcroft		exit(0);
50628905a67cSAndy Whitcroft	}
50638905a67cSAndy Whitcroft
50648905a67cSAndy Whitcroft	if (!$is_patch) {
5065000d1cc1SJoe Perches		ERROR("NOT_UNIFIED_DIFF",
5066000d1cc1SJoe Perches		      "Does not appear to be a unified-diff format patch\n");
50670a920b5bSAndy Whitcroft	}
50680a920b5bSAndy Whitcroft	if ($is_patch && $chk_signoff && $signoff == 0) {
5069000d1cc1SJoe Perches		ERROR("MISSING_SIGN_OFF",
5070000d1cc1SJoe Perches		      "Missing Signed-off-by: line(s)\n");
50710a920b5bSAndy Whitcroft	}
50720a920b5bSAndy Whitcroft
5073f0a594c1SAndy Whitcroft	print report_dump();
507413214adfSAndy Whitcroft	if ($summary && !($clean == 1 && $quiet == 1)) {
507513214adfSAndy Whitcroft		print "$filename " if ($summary_file);
50766c72ffaaSAndy Whitcroft		print "total: $cnt_error errors, $cnt_warn warnings, " .
50776c72ffaaSAndy Whitcroft			(($check)? "$cnt_chk checks, " : "") .
50786c72ffaaSAndy Whitcroft			"$cnt_lines lines checked\n";
50798905a67cSAndy Whitcroft		print "\n" if ($quiet == 0);
50806c72ffaaSAndy Whitcroft	}
50818905a67cSAndy Whitcroft
5082d2c0a235SAndy Whitcroft	if ($quiet == 0) {
5083d1fe9c09SJoe Perches
5084d1fe9c09SJoe Perches		if ($^V lt 5.10.0) {
5085d1fe9c09SJoe Perches			print("NOTE: perl $^V is not modern enough to detect all possible issues.\n");
5086d1fe9c09SJoe Perches			print("An upgrade to at least perl v5.10.0 is suggested.\n\n");
5087d1fe9c09SJoe Perches		}
5088d1fe9c09SJoe Perches
5089d2c0a235SAndy Whitcroft		# If there were whitespace errors which cleanpatch can fix
5090d2c0a235SAndy Whitcroft		# then suggest that.
5091d2c0a235SAndy Whitcroft		if ($rpt_cleaners) {
5092d2c0a235SAndy Whitcroft			print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n";
5093d2c0a235SAndy Whitcroft			print "      scripts/cleanfile\n\n";
5094b0781216SMike Frysinger			$rpt_cleaners = 0;
5095d2c0a235SAndy Whitcroft		}
5096d2c0a235SAndy Whitcroft	}
5097d2c0a235SAndy Whitcroft
509891bfe484SJoe Perches	hash_show_words(\%use_type, "Used");
509991bfe484SJoe Perches	hash_show_words(\%ignore_type, "Ignored");
5100000d1cc1SJoe Perches
5101d752fcc8SJoe Perches	if ($clean == 0 && $fix &&
5102d752fcc8SJoe Perches	    ("@rawlines" ne "@fixed" ||
5103d752fcc8SJoe Perches	     $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) {
51049624b8d6SJoe Perches		my $newfile = $filename;
51059624b8d6SJoe Perches		$newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace);
51063705ce5bSJoe Perches		my $linecount = 0;
51073705ce5bSJoe Perches		my $f;
51083705ce5bSJoe Perches
5109d752fcc8SJoe Perches		@fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted);
5110d752fcc8SJoe Perches
51113705ce5bSJoe Perches		open($f, '>', $newfile)
51123705ce5bSJoe Perches		    or die "$P: Can't open $newfile for write\n";
51133705ce5bSJoe Perches		foreach my $fixed_line (@fixed) {
51143705ce5bSJoe Perches			$linecount++;
51153705ce5bSJoe Perches			if ($file) {
51163705ce5bSJoe Perches				if ($linecount > 3) {
51173705ce5bSJoe Perches					$fixed_line =~ s/^\+//;
51183705ce5bSJoe Perches					print $f $fixed_line . "\n";
51193705ce5bSJoe Perches				}
51203705ce5bSJoe Perches			} else {
51213705ce5bSJoe Perches				print $f $fixed_line . "\n";
51223705ce5bSJoe Perches			}
51233705ce5bSJoe Perches		}
51243705ce5bSJoe Perches		close($f);
51253705ce5bSJoe Perches
51263705ce5bSJoe Perches		if (!$quiet) {
51273705ce5bSJoe Perches			print << "EOM";
51283705ce5bSJoe PerchesWrote EXPERIMENTAL --fix correction(s) to '$newfile'
51293705ce5bSJoe Perches
51303705ce5bSJoe PerchesDo _NOT_ trust the results written to this file.
51313705ce5bSJoe PerchesDo _NOT_ submit these changes without inspecting them for correctness.
51323705ce5bSJoe Perches
51333705ce5bSJoe PerchesThis EXPERIMENTAL file is simply a convenience to help rewrite patches.
51343705ce5bSJoe PerchesNo warranties, expressed or implied...
51353705ce5bSJoe Perches
51363705ce5bSJoe PerchesEOM
51373705ce5bSJoe Perches		}
51383705ce5bSJoe Perches	}
51393705ce5bSJoe Perches
51400a920b5bSAndy Whitcroft	if ($clean == 1 && $quiet == 0) {
5141c2fdda0dSAndy Whitcroft		print "$vname has no obvious style problems and is ready for submission.\n"
51420a920b5bSAndy Whitcroft	}
51430a920b5bSAndy Whitcroft	if ($clean == 0 && $quiet == 0) {
5144000d1cc1SJoe Perches		print << "EOM";
5145000d1cc1SJoe Perches$vname has style problems, please review.
5146000d1cc1SJoe Perches
5147000d1cc1SJoe PerchesIf any of these errors are false positives, please report
5148000d1cc1SJoe Perchesthem to the maintainer, see CHECKPATCH in MAINTAINERS.
5149000d1cc1SJoe PerchesEOM
51500a920b5bSAndy Whitcroft	}
515113214adfSAndy Whitcroft
51520a920b5bSAndy Whitcroft	return $clean;
51530a920b5bSAndy Whitcroft}
5154