xref: /linux-6.15/scripts/checkpatch.pl (revision 66b47b4a)
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;
12*66b47b4aSKees Cook$P =~ s@(.*)/@@g;
13*66b47b4aSKees Cookmy $D = $1;
140a920b5bSAndy Whitcroft
15000d1cc1SJoe Perchesmy $V = '0.32';
160a920b5bSAndy Whitcroft
170a920b5bSAndy Whitcroftuse Getopt::Long qw(:config no_auto_abbrev);
180a920b5bSAndy Whitcroft
190a920b5bSAndy Whitcroftmy $quiet = 0;
200a920b5bSAndy Whitcroftmy $tree = 1;
210a920b5bSAndy Whitcroftmy $chk_signoff = 1;
220a920b5bSAndy Whitcroftmy $chk_patch = 1;
23773647a0SAndy Whitcroftmy $tst_only;
246c72ffaaSAndy Whitcroftmy $emacs = 0;
258905a67cSAndy Whitcroftmy $terse = 0;
266c72ffaaSAndy Whitcroftmy $file = 0;
276c72ffaaSAndy Whitcroftmy $check = 0;
282ac73b4fSJoe Perchesmy $check_orig = 0;
298905a67cSAndy Whitcroftmy $summary = 1;
308905a67cSAndy Whitcroftmy $mailback = 0;
3113214adfSAndy Whitcroftmy $summary_file = 0;
32000d1cc1SJoe Perchesmy $show_types = 0;
333705ce5bSJoe Perchesmy $fix = 0;
349624b8d6SJoe Perchesmy $fix_inplace = 0;
356c72ffaaSAndy Whitcroftmy $root;
36c2fdda0dSAndy Whitcroftmy %debug;
373445686aSJoe Perchesmy %camelcase = ();
3891bfe484SJoe Perchesmy %use_type = ();
3991bfe484SJoe Perchesmy @use = ();
4091bfe484SJoe Perchesmy %ignore_type = ();
41000d1cc1SJoe Perchesmy @ignore = ();
4277f5b10aSHannes Edermy $help = 0;
43000d1cc1SJoe Perchesmy $configuration_file = ".checkpatch.conf";
446cd7f386SJoe Perchesmy $max_line_length = 80;
45d62a201fSDave Hansenmy $ignore_perl_version = 0;
46d62a201fSDave Hansenmy $minimum_perl_version = 5.10.0;
4756193274SVadim Bendeburymy $min_conf_desc_length = 4;
48*66b47b4aSKees Cookmy $spelling_file = "$D/spelling.txt";
4977f5b10aSHannes Eder
5077f5b10aSHannes Edersub help {
5177f5b10aSHannes Eder	my ($exitcode) = @_;
5277f5b10aSHannes Eder
5377f5b10aSHannes Eder	print << "EOM";
5477f5b10aSHannes EderUsage: $P [OPTION]... [FILE]...
5577f5b10aSHannes EderVersion: $V
5677f5b10aSHannes Eder
5777f5b10aSHannes EderOptions:
5877f5b10aSHannes Eder  -q, --quiet                quiet
5977f5b10aSHannes Eder  --no-tree                  run without a kernel tree
6077f5b10aSHannes Eder  --no-signoff               do not check for 'Signed-off-by' line
6177f5b10aSHannes Eder  --patch                    treat FILE as patchfile (default)
6277f5b10aSHannes Eder  --emacs                    emacs compile window format
6377f5b10aSHannes Eder  --terse                    one line per report
6477f5b10aSHannes Eder  -f, --file                 treat FILE as regular source file
6577f5b10aSHannes Eder  --subjective, --strict     enable more subjective tests
6691bfe484SJoe Perches  --types TYPE(,TYPE2...)    show only these comma separated message types
67000d1cc1SJoe Perches  --ignore TYPE(,TYPE2...)   ignore various comma separated message types
686cd7f386SJoe Perches  --max-line-length=n        set the maximum line length, if exceeded, warn
6956193274SVadim Bendebury  --min-conf-desc-length=n   set the min description length, if shorter, warn
70000d1cc1SJoe Perches  --show-types               show the message "types" in the output
7177f5b10aSHannes Eder  --root=PATH                PATH to the kernel tree root
7277f5b10aSHannes Eder  --no-summary               suppress the per-file summary
7377f5b10aSHannes Eder  --mailback                 only produce a report in case of warnings/errors
7477f5b10aSHannes Eder  --summary-file             include the filename in summary
7577f5b10aSHannes Eder  --debug KEY=[0|1]          turn on/off debugging of KEY, where KEY is one of
7677f5b10aSHannes Eder                             'values', 'possible', 'type', and 'attr' (default
7777f5b10aSHannes Eder                             is all off)
7877f5b10aSHannes Eder  --test-only=WORD           report only warnings/errors containing WORD
7977f5b10aSHannes Eder                             literally
803705ce5bSJoe Perches  --fix                      EXPERIMENTAL - may create horrible results
813705ce5bSJoe Perches                             If correctable single-line errors exist, create
823705ce5bSJoe Perches                             "<inputfile>.EXPERIMENTAL-checkpatch-fixes"
833705ce5bSJoe Perches                             with potential errors corrected to the preferred
843705ce5bSJoe Perches                             checkpatch style
859624b8d6SJoe Perches  --fix-inplace              EXPERIMENTAL - may create horrible results
869624b8d6SJoe Perches                             Is the same as --fix, but overwrites the input
879624b8d6SJoe Perches                             file.  It's your fault if there's no backup or git
88d62a201fSDave Hansen  --ignore-perl-version      override checking of perl version.  expect
89d62a201fSDave Hansen                             runtime errors.
9077f5b10aSHannes Eder  -h, --help, --version      display this help and exit
9177f5b10aSHannes Eder
9277f5b10aSHannes EderWhen FILE is - read standard input.
9377f5b10aSHannes EderEOM
9477f5b10aSHannes Eder
9577f5b10aSHannes Eder	exit($exitcode);
9677f5b10aSHannes Eder}
9777f5b10aSHannes Eder
98000d1cc1SJoe Perchesmy $conf = which_conf($configuration_file);
99000d1cc1SJoe Perchesif (-f $conf) {
100000d1cc1SJoe Perches	my @conf_args;
101000d1cc1SJoe Perches	open(my $conffile, '<', "$conf")
102000d1cc1SJoe Perches	    or warn "$P: Can't find a readable $configuration_file file $!\n";
103000d1cc1SJoe Perches
104000d1cc1SJoe Perches	while (<$conffile>) {
105000d1cc1SJoe Perches		my $line = $_;
106000d1cc1SJoe Perches
107000d1cc1SJoe Perches		$line =~ s/\s*\n?$//g;
108000d1cc1SJoe Perches		$line =~ s/^\s*//g;
109000d1cc1SJoe Perches		$line =~ s/\s+/ /g;
110000d1cc1SJoe Perches
111000d1cc1SJoe Perches		next if ($line =~ m/^\s*#/);
112000d1cc1SJoe Perches		next if ($line =~ m/^\s*$/);
113000d1cc1SJoe Perches
114000d1cc1SJoe Perches		my @words = split(" ", $line);
115000d1cc1SJoe Perches		foreach my $word (@words) {
116000d1cc1SJoe Perches			last if ($word =~ m/^#/);
117000d1cc1SJoe Perches			push (@conf_args, $word);
118000d1cc1SJoe Perches		}
119000d1cc1SJoe Perches	}
120000d1cc1SJoe Perches	close($conffile);
121000d1cc1SJoe Perches	unshift(@ARGV, @conf_args) if @conf_args;
122000d1cc1SJoe Perches}
123000d1cc1SJoe Perches
1240a920b5bSAndy WhitcroftGetOptions(
1256c72ffaaSAndy Whitcroft	'q|quiet+'	=> \$quiet,
1260a920b5bSAndy Whitcroft	'tree!'		=> \$tree,
1270a920b5bSAndy Whitcroft	'signoff!'	=> \$chk_signoff,
1280a920b5bSAndy Whitcroft	'patch!'	=> \$chk_patch,
1296c72ffaaSAndy Whitcroft	'emacs!'	=> \$emacs,
1308905a67cSAndy Whitcroft	'terse!'	=> \$terse,
13177f5b10aSHannes Eder	'f|file!'	=> \$file,
1326c72ffaaSAndy Whitcroft	'subjective!'	=> \$check,
1336c72ffaaSAndy Whitcroft	'strict!'	=> \$check,
134000d1cc1SJoe Perches	'ignore=s'	=> \@ignore,
13591bfe484SJoe Perches	'types=s'	=> \@use,
136000d1cc1SJoe Perches	'show-types!'	=> \$show_types,
1376cd7f386SJoe Perches	'max-line-length=i' => \$max_line_length,
13856193274SVadim Bendebury	'min-conf-desc-length=i' => \$min_conf_desc_length,
1396c72ffaaSAndy Whitcroft	'root=s'	=> \$root,
1408905a67cSAndy Whitcroft	'summary!'	=> \$summary,
1418905a67cSAndy Whitcroft	'mailback!'	=> \$mailback,
14213214adfSAndy Whitcroft	'summary-file!'	=> \$summary_file,
1433705ce5bSJoe Perches	'fix!'		=> \$fix,
1449624b8d6SJoe Perches	'fix-inplace!'	=> \$fix_inplace,
145d62a201fSDave Hansen	'ignore-perl-version!' => \$ignore_perl_version,
146c2fdda0dSAndy Whitcroft	'debug=s'	=> \%debug,
147773647a0SAndy Whitcroft	'test-only=s'	=> \$tst_only,
14877f5b10aSHannes Eder	'h|help'	=> \$help,
14977f5b10aSHannes Eder	'version'	=> \$help
15077f5b10aSHannes Eder) or help(1);
15177f5b10aSHannes Eder
15277f5b10aSHannes Ederhelp(0) if ($help);
1530a920b5bSAndy Whitcroft
1549624b8d6SJoe Perches$fix = 1 if ($fix_inplace);
1552ac73b4fSJoe Perches$check_orig = $check;
1569624b8d6SJoe Perches
1570a920b5bSAndy Whitcroftmy $exit = 0;
1580a920b5bSAndy Whitcroft
159d62a201fSDave Hansenif ($^V && $^V lt $minimum_perl_version) {
160d62a201fSDave Hansen	printf "$P: requires at least perl version %vd\n", $minimum_perl_version;
161d62a201fSDave Hansen	if (!$ignore_perl_version) {
162d62a201fSDave Hansen		exit(1);
163d62a201fSDave Hansen	}
164d62a201fSDave Hansen}
165d62a201fSDave Hansen
1660a920b5bSAndy Whitcroftif ($#ARGV < 0) {
16777f5b10aSHannes Eder	print "$P: no input files\n";
1680a920b5bSAndy Whitcroft	exit(1);
1690a920b5bSAndy Whitcroft}
1700a920b5bSAndy Whitcroft
17191bfe484SJoe Perchessub hash_save_array_words {
17291bfe484SJoe Perches	my ($hashRef, $arrayRef) = @_;
17391bfe484SJoe Perches
17491bfe484SJoe Perches	my @array = split(/,/, join(',', @$arrayRef));
17591bfe484SJoe Perches	foreach my $word (@array) {
176000d1cc1SJoe Perches		$word =~ s/\s*\n?$//g;
177000d1cc1SJoe Perches		$word =~ s/^\s*//g;
178000d1cc1SJoe Perches		$word =~ s/\s+/ /g;
179000d1cc1SJoe Perches		$word =~ tr/[a-z]/[A-Z]/;
180000d1cc1SJoe Perches
181000d1cc1SJoe Perches		next if ($word =~ m/^\s*#/);
182000d1cc1SJoe Perches		next if ($word =~ m/^\s*$/);
183000d1cc1SJoe Perches
18491bfe484SJoe Perches		$hashRef->{$word}++;
185000d1cc1SJoe Perches	}
18691bfe484SJoe Perches}
18791bfe484SJoe Perches
18891bfe484SJoe Perchessub hash_show_words {
18991bfe484SJoe Perches	my ($hashRef, $prefix) = @_;
19091bfe484SJoe Perches
19158cb3cf6SJoe Perches	if ($quiet == 0 && keys %$hashRef) {
19291bfe484SJoe Perches		print "NOTE: $prefix message types:";
19358cb3cf6SJoe Perches		foreach my $word (sort keys %$hashRef) {
19491bfe484SJoe Perches			print " $word";
19591bfe484SJoe Perches		}
19691bfe484SJoe Perches		print "\n\n";
19791bfe484SJoe Perches	}
19891bfe484SJoe Perches}
19991bfe484SJoe Perches
20091bfe484SJoe Percheshash_save_array_words(\%ignore_type, \@ignore);
20191bfe484SJoe Percheshash_save_array_words(\%use_type, \@use);
202000d1cc1SJoe Perches
203c2fdda0dSAndy Whitcroftmy $dbg_values = 0;
204c2fdda0dSAndy Whitcroftmy $dbg_possible = 0;
2057429c690SAndy Whitcroftmy $dbg_type = 0;
206a1ef277eSAndy Whitcroftmy $dbg_attr = 0;
207c2fdda0dSAndy Whitcroftfor my $key (keys %debug) {
20821caa13cSAndy Whitcroft	## no critic
20921caa13cSAndy Whitcroft	eval "\${dbg_$key} = '$debug{$key}';";
21021caa13cSAndy Whitcroft	die "$@" if ($@);
211c2fdda0dSAndy Whitcroft}
212c2fdda0dSAndy Whitcroft
213d2c0a235SAndy Whitcroftmy $rpt_cleaners = 0;
214d2c0a235SAndy Whitcroft
2158905a67cSAndy Whitcroftif ($terse) {
2168905a67cSAndy Whitcroft	$emacs = 1;
2178905a67cSAndy Whitcroft	$quiet++;
2188905a67cSAndy Whitcroft}
2198905a67cSAndy Whitcroft
2206c72ffaaSAndy Whitcroftif ($tree) {
2216c72ffaaSAndy Whitcroft	if (defined $root) {
2226c72ffaaSAndy Whitcroft		if (!top_of_kernel_tree($root)) {
2236c72ffaaSAndy Whitcroft			die "$P: $root: --root does not point at a valid tree\n";
2246c72ffaaSAndy Whitcroft		}
2256c72ffaaSAndy Whitcroft	} else {
2266c72ffaaSAndy Whitcroft		if (top_of_kernel_tree('.')) {
2276c72ffaaSAndy Whitcroft			$root = '.';
2286c72ffaaSAndy Whitcroft		} elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
2296c72ffaaSAndy Whitcroft						top_of_kernel_tree($1)) {
2306c72ffaaSAndy Whitcroft			$root = $1;
2316c72ffaaSAndy Whitcroft		}
2326c72ffaaSAndy Whitcroft	}
2336c72ffaaSAndy Whitcroft
2346c72ffaaSAndy Whitcroft	if (!defined $root) {
2350a920b5bSAndy Whitcroft		print "Must be run from the top-level dir. of a kernel tree\n";
2360a920b5bSAndy Whitcroft		exit(2);
2370a920b5bSAndy Whitcroft	}
2386c72ffaaSAndy Whitcroft}
2396c72ffaaSAndy Whitcroft
2406c72ffaaSAndy Whitcroftmy $emitted_corrupt = 0;
2416c72ffaaSAndy Whitcroft
2422ceb532bSAndy Whitcroftour $Ident	= qr{
2432ceb532bSAndy Whitcroft			[A-Za-z_][A-Za-z\d_]*
2442ceb532bSAndy Whitcroft			(?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
2452ceb532bSAndy Whitcroft		}x;
2466c72ffaaSAndy Whitcroftour $Storage	= qr{extern|static|asmlinkage};
2476c72ffaaSAndy Whitcroftour $Sparse	= qr{
2486c72ffaaSAndy Whitcroft			__user|
2496c72ffaaSAndy Whitcroft			__kernel|
2506c72ffaaSAndy Whitcroft			__force|
2516c72ffaaSAndy Whitcroft			__iomem|
2526c72ffaaSAndy Whitcroft			__must_check|
2536c72ffaaSAndy Whitcroft			__init_refok|
254417495edSAndy Whitcroft			__kprobes|
255165e72a6SSven Eckelmann			__ref|
256165e72a6SSven Eckelmann			__rcu
2576c72ffaaSAndy Whitcroft		}x;
258e970b884SJoe Perchesour $InitAttributePrefix = qr{__(?:mem|cpu|dev|net_|)};
259e970b884SJoe Perchesour $InitAttributeData = qr{$InitAttributePrefix(?:initdata\b)};
260e970b884SJoe Perchesour $InitAttributeConst = qr{$InitAttributePrefix(?:initconst\b)};
261e970b884SJoe Perchesour $InitAttributeInit = qr{$InitAttributePrefix(?:init\b)};
262e970b884SJoe Perchesour $InitAttribute = qr{$InitAttributeData|$InitAttributeConst|$InitAttributeInit};
2638716de38SJoe Perches
26452131292SWolfram Sang# Notes to $Attribute:
26552131292SWolfram Sang# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
2666c72ffaaSAndy Whitcroftour $Attribute	= qr{
2676c72ffaaSAndy Whitcroft			const|
26803f1df7dSJoe Perches			__percpu|
26903f1df7dSJoe Perches			__nocast|
27003f1df7dSJoe Perches			__safe|
27103f1df7dSJoe Perches			__bitwise__|
27203f1df7dSJoe Perches			__packed__|
27303f1df7dSJoe Perches			__packed2__|
27403f1df7dSJoe Perches			__naked|
27503f1df7dSJoe Perches			__maybe_unused|
27603f1df7dSJoe Perches			__always_unused|
27703f1df7dSJoe Perches			__noreturn|
27803f1df7dSJoe Perches			__used|
27903f1df7dSJoe Perches			__cold|
28003f1df7dSJoe Perches			__noclone|
28103f1df7dSJoe Perches			__deprecated|
2826c72ffaaSAndy Whitcroft			__read_mostly|
2836c72ffaaSAndy Whitcroft			__kprobes|
2848716de38SJoe Perches			$InitAttribute|
28524e1d81aSAndy Whitcroft			____cacheline_aligned|
28624e1d81aSAndy Whitcroft			____cacheline_aligned_in_smp|
2875fe3af11SAndy Whitcroft			____cacheline_internodealigned_in_smp|
2885fe3af11SAndy Whitcroft			__weak
2896c72ffaaSAndy Whitcroft		  }x;
290c45dcabdSAndy Whitcroftour $Modifier;
29191cb5195SJoe Perchesour $Inline	= qr{inline|__always_inline|noinline|__inline|__inline__};
2926c72ffaaSAndy Whitcroftour $Member	= qr{->$Ident|\.$Ident|\[[^]]*\]};
2936c72ffaaSAndy Whitcroftour $Lval	= qr{$Ident(?:$Member)*};
2946c72ffaaSAndy Whitcroft
29595e2c602SJoe Perchesour $Int_type	= qr{(?i)llu|ull|ll|lu|ul|l|u};
29695e2c602SJoe Perchesour $Binary	= qr{(?i)0b[01]+$Int_type?};
29795e2c602SJoe Perchesour $Hex	= qr{(?i)0x[0-9a-f]+$Int_type?};
29895e2c602SJoe Perchesour $Int	= qr{[0-9]+$Int_type?};
2992435880fSJoe Perchesour $Octal	= qr{0[0-7]+$Int_type?};
300326b1ffcSJoe Perchesour $Float_hex	= qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?};
301326b1ffcSJoe Perchesour $Float_dec	= qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?};
302326b1ffcSJoe Perchesour $Float_int	= qr{(?i)[0-9]+e-?[0-9]+[fl]?};
30374349bccSJoe Perchesour $Float	= qr{$Float_hex|$Float_dec|$Float_int};
3042435880fSJoe Perchesour $Constant	= qr{$Float|$Binary|$Octal|$Hex|$Int};
305326b1ffcSJoe Perchesour $Assignment	= qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=};
306447432f3SJoe Perchesour $Compare    = qr{<=|>=|==|!=|<|(?<!-)>};
30723f780c9SJoe Perchesour $Arithmetic = qr{\+|-|\*|\/|%};
3086c72ffaaSAndy Whitcroftour $Operators	= qr{
3096c72ffaaSAndy Whitcroft			<=|>=|==|!=|
3106c72ffaaSAndy Whitcroft			=>|->|<<|>>|<|>|!|~|
31123f780c9SJoe Perches			&&|\|\||,|\^|\+\+|--|&|\||$Arithmetic
3126c72ffaaSAndy Whitcroft		  }x;
3136c72ffaaSAndy Whitcroft
31491cb5195SJoe Perchesour $c90_Keywords = qr{do|for|while|if|else|return|goto|continue|switch|default|case|break}x;
31591cb5195SJoe Perches
3168905a67cSAndy Whitcroftour $NonptrType;
3171813087dSJoe Perchesour $NonptrTypeMisordered;
3188716de38SJoe Perchesour $NonptrTypeWithAttr;
3198905a67cSAndy Whitcroftour $Type;
3201813087dSJoe Perchesour $TypeMisordered;
3218905a67cSAndy Whitcroftour $Declare;
3221813087dSJoe Perchesour $DeclareMisordered;
3238905a67cSAndy Whitcroft
32415662b3eSJoe Perchesour $NON_ASCII_UTF8	= qr{
32515662b3eSJoe Perches	[\xC2-\xDF][\x80-\xBF]               # non-overlong 2-byte
326171ae1a4SAndy Whitcroft	|  \xE0[\xA0-\xBF][\x80-\xBF]        # excluding overlongs
327171ae1a4SAndy Whitcroft	| [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}  # straight 3-byte
328171ae1a4SAndy Whitcroft	|  \xED[\x80-\x9F][\x80-\xBF]        # excluding surrogates
329171ae1a4SAndy Whitcroft	|  \xF0[\x90-\xBF][\x80-\xBF]{2}     # planes 1-3
330171ae1a4SAndy Whitcroft	| [\xF1-\xF3][\x80-\xBF]{3}          # planes 4-15
331171ae1a4SAndy Whitcroft	|  \xF4[\x80-\x8F][\x80-\xBF]{2}     # plane 16
332171ae1a4SAndy Whitcroft}x;
333171ae1a4SAndy Whitcroft
33415662b3eSJoe Perchesour $UTF8	= qr{
33515662b3eSJoe Perches	[\x09\x0A\x0D\x20-\x7E]              # ASCII
33615662b3eSJoe Perches	| $NON_ASCII_UTF8
33715662b3eSJoe Perches}x;
33815662b3eSJoe Perches
3398ed22cadSAndy Whitcroftour $typeTypedefs = qr{(?x:
340fb9e9096SAndy Whitcroft	(?:__)?(?:u|s|be|le)(?:8|16|32|64)|
3418ed22cadSAndy Whitcroft	atomic_t
3428ed22cadSAndy Whitcroft)};
3438ed22cadSAndy Whitcroft
344691e669bSJoe Perchesour $logFunctions = qr{(?x:
3456e60c02eSJoe Perches	printk(?:_ratelimited|_once|)|
3467d0b6594SJacob Keller	(?:[a-z0-9]+_){1,2}(?:printk|emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)(?:_ratelimited|_once|)|
3476e60c02eSJoe Perches	WARN(?:_RATELIMIT|_ONCE|)|
348b0531722SJoe Perches	panic|
34906668727SJoe Perches	MODULE_[A-Z_]+|
35006668727SJoe Perches	seq_vprintf|seq_printf|seq_puts
351691e669bSJoe Perches)};
352691e669bSJoe Perches
35320112475SJoe Perchesour $signature_tags = qr{(?xi:
35420112475SJoe Perches	Signed-off-by:|
35520112475SJoe Perches	Acked-by:|
35620112475SJoe Perches	Tested-by:|
35720112475SJoe Perches	Reviewed-by:|
35820112475SJoe Perches	Reported-by:|
3598543ae12SMugunthan V N	Suggested-by:|
36020112475SJoe Perches	To:|
36120112475SJoe Perches	Cc:
36220112475SJoe Perches)};
36320112475SJoe Perches
3641813087dSJoe Perchesour @typeListMisordered = (
3651813087dSJoe Perches	qr{char\s+(?:un)?signed},
3661813087dSJoe Perches	qr{int\s+(?:(?:un)?signed\s+)?short\s},
3671813087dSJoe Perches	qr{int\s+short(?:\s+(?:un)?signed)},
3681813087dSJoe Perches	qr{short\s+int(?:\s+(?:un)?signed)},
3691813087dSJoe Perches	qr{(?:un)?signed\s+int\s+short},
3701813087dSJoe Perches	qr{short\s+(?:un)?signed},
3711813087dSJoe Perches	qr{long\s+int\s+(?:un)?signed},
3721813087dSJoe Perches	qr{int\s+long\s+(?:un)?signed},
3731813087dSJoe Perches	qr{long\s+(?:un)?signed\s+int},
3741813087dSJoe Perches	qr{int\s+(?:un)?signed\s+long},
3751813087dSJoe Perches	qr{int\s+(?:un)?signed},
3761813087dSJoe Perches	qr{int\s+long\s+long\s+(?:un)?signed},
3771813087dSJoe Perches	qr{long\s+long\s+int\s+(?:un)?signed},
3781813087dSJoe Perches	qr{long\s+long\s+(?:un)?signed\s+int},
3791813087dSJoe Perches	qr{long\s+long\s+(?:un)?signed},
3801813087dSJoe Perches	qr{long\s+(?:un)?signed},
3811813087dSJoe Perches);
3821813087dSJoe Perches
3838905a67cSAndy Whitcroftour @typeList = (
3848905a67cSAndy Whitcroft	qr{void},
3850c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?char},
3860c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?short\s+int},
3870c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?short},
3880c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?int},
3890c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?long\s+int},
3900c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?long\s+long\s+int},
3910c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?long\s+long},
3920c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?long},
3930c773d9dSJoe Perches	qr{(?:un)?signed},
3948905a67cSAndy Whitcroft	qr{float},
3958905a67cSAndy Whitcroft	qr{double},
3968905a67cSAndy Whitcroft	qr{bool},
3978905a67cSAndy Whitcroft	qr{struct\s+$Ident},
3988905a67cSAndy Whitcroft	qr{union\s+$Ident},
3998905a67cSAndy Whitcroft	qr{enum\s+$Ident},
4008905a67cSAndy Whitcroft	qr{${Ident}_t},
4018905a67cSAndy Whitcroft	qr{${Ident}_handler},
4028905a67cSAndy Whitcroft	qr{${Ident}_handler_fn},
4031813087dSJoe Perches	@typeListMisordered,
4048905a67cSAndy Whitcroft);
4058716de38SJoe Perchesour @typeListWithAttr = (
4068716de38SJoe Perches	@typeList,
4078716de38SJoe Perches	qr{struct\s+$InitAttribute\s+$Ident},
4088716de38SJoe Perches	qr{union\s+$InitAttribute\s+$Ident},
4098716de38SJoe Perches);
4108716de38SJoe Perches
411c45dcabdSAndy Whitcroftour @modifierList = (
412c45dcabdSAndy Whitcroft	qr{fastcall},
413c45dcabdSAndy Whitcroft);
4148905a67cSAndy Whitcroft
4152435880fSJoe Perchesour @mode_permission_funcs = (
4162435880fSJoe Perches	["module_param", 3],
4172435880fSJoe Perches	["module_param_(?:array|named|string)", 4],
4182435880fSJoe Perches	["module_param_array_named", 5],
4192435880fSJoe Perches	["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2],
4202435880fSJoe Perches	["proc_create(?:_data|)", 2],
4212435880fSJoe Perches	["(?:CLASS|DEVICE|SENSOR)_ATTR", 2],
4222435880fSJoe Perches);
4232435880fSJoe Perches
424515a235eSJoe Perches#Create a search pattern for all these functions to speed up a loop below
425515a235eSJoe Perchesour $mode_perms_search = "";
426515a235eSJoe Perchesforeach my $entry (@mode_permission_funcs) {
427515a235eSJoe Perches	$mode_perms_search .= '|' if ($mode_perms_search ne "");
428515a235eSJoe Perches	$mode_perms_search .= $entry->[0];
429515a235eSJoe Perches}
430515a235eSJoe Perches
4317840a94cSWolfram Sangour $allowed_asm_includes = qr{(?x:
4327840a94cSWolfram Sang	irq|
433cdcee686SSergey Ryazanov	memory|
434cdcee686SSergey Ryazanov	time|
435cdcee686SSergey Ryazanov	reboot
4367840a94cSWolfram Sang)};
4377840a94cSWolfram Sang# memory.h: ARM has a custom one
4387840a94cSWolfram Sang
439*66b47b4aSKees Cook# Load common spelling mistakes and build regular expression list.
440*66b47b4aSKees Cookmy $misspellings;
441*66b47b4aSKees Cookmy @spelling_list;
442*66b47b4aSKees Cookmy %spelling_fix;
443*66b47b4aSKees Cookopen(my $spelling, '<', $spelling_file)
444*66b47b4aSKees Cook    or die "$P: Can't open $spelling_file for reading: $!\n";
445*66b47b4aSKees Cookwhile (<$spelling>) {
446*66b47b4aSKees Cook	my $line = $_;
447*66b47b4aSKees Cook
448*66b47b4aSKees Cook	$line =~ s/\s*\n?$//g;
449*66b47b4aSKees Cook	$line =~ s/^\s*//g;
450*66b47b4aSKees Cook
451*66b47b4aSKees Cook	next if ($line =~ m/^\s*#/);
452*66b47b4aSKees Cook	next if ($line =~ m/^\s*$/);
453*66b47b4aSKees Cook
454*66b47b4aSKees Cook	my ($suspect, $fix) = split(/\|\|/, $line);
455*66b47b4aSKees Cook
456*66b47b4aSKees Cook	push(@spelling_list, $suspect);
457*66b47b4aSKees Cook	$spelling_fix{$suspect} = $fix;
458*66b47b4aSKees Cook}
459*66b47b4aSKees Cookclose($spelling);
460*66b47b4aSKees Cook$misspellings = join("|", @spelling_list);
461*66b47b4aSKees Cook
4628905a67cSAndy Whitcroftsub build_types {
463d2172eb5SAndy Whitcroft	my $mods = "(?x:  \n" . join("|\n  ", @modifierList) . "\n)";
464d2172eb5SAndy Whitcroft	my $all = "(?x:  \n" . join("|\n  ", @typeList) . "\n)";
4651813087dSJoe Perches	my $Misordered = "(?x:  \n" . join("|\n  ", @typeListMisordered) . "\n)";
4668716de38SJoe Perches	my $allWithAttr = "(?x:  \n" . join("|\n  ", @typeListWithAttr) . "\n)";
467c8cb2ca3SAndy Whitcroft	$Modifier	= qr{(?:$Attribute|$Sparse|$mods)};
4688905a67cSAndy Whitcroft	$NonptrType	= qr{
469d2172eb5SAndy Whitcroft			(?:$Modifier\s+|const\s+)*
470cf655043SAndy Whitcroft			(?:
4716b48db24SAndy Whitcroft				(?:typeof|__typeof__)\s*\([^\)]*\)|
4728ed22cadSAndy Whitcroft				(?:$typeTypedefs\b)|
473c45dcabdSAndy Whitcroft				(?:${all}\b)
474cf655043SAndy Whitcroft			)
475c8cb2ca3SAndy Whitcroft			(?:\s+$Modifier|\s+const)*
4768905a67cSAndy Whitcroft		  }x;
4771813087dSJoe Perches	$NonptrTypeMisordered	= qr{
4781813087dSJoe Perches			(?:$Modifier\s+|const\s+)*
4791813087dSJoe Perches			(?:
4801813087dSJoe Perches				(?:${Misordered}\b)
4811813087dSJoe Perches			)
4821813087dSJoe Perches			(?:\s+$Modifier|\s+const)*
4831813087dSJoe Perches		  }x;
4848716de38SJoe Perches	$NonptrTypeWithAttr	= qr{
4858716de38SJoe Perches			(?:$Modifier\s+|const\s+)*
4868716de38SJoe Perches			(?:
4878716de38SJoe Perches				(?:typeof|__typeof__)\s*\([^\)]*\)|
4888716de38SJoe Perches				(?:$typeTypedefs\b)|
4898716de38SJoe Perches				(?:${allWithAttr}\b)
4908716de38SJoe Perches			)
4918716de38SJoe Perches			(?:\s+$Modifier|\s+const)*
4928716de38SJoe Perches		  }x;
4938905a67cSAndy Whitcroft	$Type	= qr{
494c45dcabdSAndy Whitcroft			$NonptrType
4951574a29fSJoe Perches			(?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
496c8cb2ca3SAndy Whitcroft			(?:\s+$Inline|\s+$Modifier)*
4978905a67cSAndy Whitcroft		  }x;
4981813087dSJoe Perches	$TypeMisordered	= qr{
4991813087dSJoe Perches			$NonptrTypeMisordered
5001813087dSJoe Perches			(?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
5011813087dSJoe Perches			(?:\s+$Inline|\s+$Modifier)*
5021813087dSJoe Perches		  }x;
50391cb5195SJoe Perches	$Declare	= qr{(?:$Storage\s+(?:$Inline\s+)?)?$Type};
5041813087dSJoe Perches	$DeclareMisordered	= qr{(?:$Storage\s+(?:$Inline\s+)?)?$TypeMisordered};
5058905a67cSAndy Whitcroft}
5068905a67cSAndy Whitcroftbuild_types();
5076c72ffaaSAndy Whitcroft
5087d2367afSJoe Perchesour $Typecast	= qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
509d1fe9c09SJoe Perches
510d1fe9c09SJoe Perches# Using $balanced_parens, $LvalOrFunc, or $FuncArg
511d1fe9c09SJoe Perches# requires at least perl version v5.10.0
512d1fe9c09SJoe Perches# Any use must be runtime checked with $^V
513d1fe9c09SJoe Perches
514d1fe9c09SJoe Perchesour $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
5152435880fSJoe Perchesour $LvalOrFunc	= qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*};
516d7c76ba7SJoe Perchesour $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant)};
5177d2367afSJoe Perches
518f8422308SJoe Perchesour $declaration_macros = qr{(?x:
519f8422308SJoe Perches	(?:$Storage\s+)?(?:[A-Z_][A-Z0-9]*_){0,2}(?:DEFINE|DECLARE)(?:_[A-Z0-9]+){1,2}\s*\(|
520f8422308SJoe Perches	(?:$Storage\s+)?LIST_HEAD\s*\(|
521f8422308SJoe Perches	(?:$Storage\s+)?${Type}\s+uninitialized_var\s*\(
522f8422308SJoe Perches)};
523f8422308SJoe Perches
5247d2367afSJoe Perchessub deparenthesize {
5257d2367afSJoe Perches	my ($string) = @_;
5267d2367afSJoe Perches	return "" if (!defined($string));
5275b9553abSJoe Perches
5285b9553abSJoe Perches	while ($string =~ /^\s*\(.*\)\s*$/) {
5295b9553abSJoe Perches		$string =~ s@^\s*\(\s*@@;
5305b9553abSJoe Perches		$string =~ s@\s*\)\s*$@@;
5315b9553abSJoe Perches	}
5325b9553abSJoe Perches
5337d2367afSJoe Perches	$string =~ s@\s+@ @g;
5345b9553abSJoe Perches
5357d2367afSJoe Perches	return $string;
5367d2367afSJoe Perches}
5377d2367afSJoe Perches
5383445686aSJoe Perchessub seed_camelcase_file {
5393445686aSJoe Perches	my ($file) = @_;
5403445686aSJoe Perches
5413445686aSJoe Perches	return if (!(-f $file));
5423445686aSJoe Perches
5433445686aSJoe Perches	local $/;
5443445686aSJoe Perches
5453445686aSJoe Perches	open(my $include_file, '<', "$file")
5463445686aSJoe Perches	    or warn "$P: Can't read '$file' $!\n";
5473445686aSJoe Perches	my $text = <$include_file>;
5483445686aSJoe Perches	close($include_file);
5493445686aSJoe Perches
5503445686aSJoe Perches	my @lines = split('\n', $text);
5513445686aSJoe Perches
5523445686aSJoe Perches	foreach my $line (@lines) {
5533445686aSJoe Perches		next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/);
5543445686aSJoe Perches		if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) {
5553445686aSJoe Perches			$camelcase{$1} = 1;
55611ea516aSJoe Perches		} elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) {
55711ea516aSJoe Perches			$camelcase{$1} = 1;
55811ea516aSJoe Perches		} elsif ($line =~ /^\s*(?:union|struct|enum)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[;\{]/) {
5593445686aSJoe Perches			$camelcase{$1} = 1;
5603445686aSJoe Perches		}
5613445686aSJoe Perches	}
5623445686aSJoe Perches}
5633445686aSJoe Perches
5643445686aSJoe Perchesmy $camelcase_seeded = 0;
5653445686aSJoe Perchessub seed_camelcase_includes {
5663445686aSJoe Perches	return if ($camelcase_seeded);
5673445686aSJoe Perches
5683445686aSJoe Perches	my $files;
569c707a81dSJoe Perches	my $camelcase_cache = "";
570c707a81dSJoe Perches	my @include_files = ();
571c707a81dSJoe Perches
572c707a81dSJoe Perches	$camelcase_seeded = 1;
573351b2a1fSJoe Perches
5743645e328SRichard Genoud	if (-e ".git") {
575351b2a1fSJoe Perches		my $git_last_include_commit = `git log --no-merges --pretty=format:"%h%n" -1 -- include`;
576351b2a1fSJoe Perches		chomp $git_last_include_commit;
577c707a81dSJoe Perches		$camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit";
578c707a81dSJoe Perches	} else {
579c707a81dSJoe Perches		my $last_mod_date = 0;
580c707a81dSJoe Perches		$files = `find $root/include -name "*.h"`;
581c707a81dSJoe Perches		@include_files = split('\n', $files);
582c707a81dSJoe Perches		foreach my $file (@include_files) {
583c707a81dSJoe Perches			my $date = POSIX::strftime("%Y%m%d%H%M",
584c707a81dSJoe Perches						   localtime((stat $file)[9]));
585c707a81dSJoe Perches			$last_mod_date = $date if ($last_mod_date < $date);
586c707a81dSJoe Perches		}
587c707a81dSJoe Perches		$camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date";
588c707a81dSJoe Perches	}
589c707a81dSJoe Perches
590c707a81dSJoe Perches	if ($camelcase_cache ne "" && -f $camelcase_cache) {
591c707a81dSJoe Perches		open(my $camelcase_file, '<', "$camelcase_cache")
592c707a81dSJoe Perches		    or warn "$P: Can't read '$camelcase_cache' $!\n";
593351b2a1fSJoe Perches		while (<$camelcase_file>) {
594351b2a1fSJoe Perches			chomp;
595351b2a1fSJoe Perches			$camelcase{$_} = 1;
596351b2a1fSJoe Perches		}
597351b2a1fSJoe Perches		close($camelcase_file);
598351b2a1fSJoe Perches
599351b2a1fSJoe Perches		return;
600351b2a1fSJoe Perches	}
601c707a81dSJoe Perches
6023645e328SRichard Genoud	if (-e ".git") {
603c707a81dSJoe Perches		$files = `git ls-files "include/*.h"`;
604c707a81dSJoe Perches		@include_files = split('\n', $files);
6053445686aSJoe Perches	}
606c707a81dSJoe Perches
6073445686aSJoe Perches	foreach my $file (@include_files) {
6083445686aSJoe Perches		seed_camelcase_file($file);
6093445686aSJoe Perches	}
610351b2a1fSJoe Perches
611c707a81dSJoe Perches	if ($camelcase_cache ne "") {
612351b2a1fSJoe Perches		unlink glob ".checkpatch-camelcase.*";
613c707a81dSJoe Perches		open(my $camelcase_file, '>', "$camelcase_cache")
614c707a81dSJoe Perches		    or warn "$P: Can't write '$camelcase_cache' $!\n";
615351b2a1fSJoe Perches		foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) {
616351b2a1fSJoe Perches			print $camelcase_file ("$_\n");
617351b2a1fSJoe Perches		}
618351b2a1fSJoe Perches		close($camelcase_file);
619351b2a1fSJoe Perches	}
6203445686aSJoe Perches}
6213445686aSJoe Perches
622d311cd44SJoe Perchessub git_commit_info {
623d311cd44SJoe Perches	my ($commit, $id, $desc) = @_;
624d311cd44SJoe Perches
625d311cd44SJoe Perches	return ($id, $desc) if ((which("git") eq "") || !(-e ".git"));
626d311cd44SJoe Perches
627d311cd44SJoe Perches	my $output = `git log --no-color --format='%H %s' -1 $commit 2>&1`;
628d311cd44SJoe Perches	$output =~ s/^\s*//gm;
629d311cd44SJoe Perches	my @lines = split("\n", $output);
630d311cd44SJoe Perches
631d311cd44SJoe Perches	if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous\./) {
632d311cd44SJoe Perches# Maybe one day convert this block of bash into something that returns
633d311cd44SJoe Perches# all matching commit ids, but it's very slow...
634d311cd44SJoe Perches#
635d311cd44SJoe Perches#		echo "checking commits $1..."
636d311cd44SJoe Perches#		git rev-list --remotes | grep -i "^$1" |
637d311cd44SJoe Perches#		while read line ; do
638d311cd44SJoe Perches#		    git log --format='%H %s' -1 $line |
639d311cd44SJoe Perches#		    echo "commit $(cut -c 1-12,41-)"
640d311cd44SJoe Perches#		done
641d311cd44SJoe Perches	} elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./) {
642d311cd44SJoe Perches	} else {
643d311cd44SJoe Perches		$id = substr($lines[0], 0, 12);
644d311cd44SJoe Perches		$desc = substr($lines[0], 41);
645d311cd44SJoe Perches	}
646d311cd44SJoe Perches
647d311cd44SJoe Perches	return ($id, $desc);
648d311cd44SJoe Perches}
649d311cd44SJoe Perches
6506c72ffaaSAndy Whitcroft$chk_signoff = 0 if ($file);
6510a920b5bSAndy Whitcroft
65200df344fSAndy Whitcroftmy @rawlines = ();
653c2fdda0dSAndy Whitcroftmy @lines = ();
6543705ce5bSJoe Perchesmy @fixed = ();
655d752fcc8SJoe Perchesmy @fixed_inserted = ();
656d752fcc8SJoe Perchesmy @fixed_deleted = ();
657194f66fcSJoe Perchesmy $fixlinenr = -1;
658194f66fcSJoe Perches
659c2fdda0dSAndy Whitcroftmy $vname;
6606c72ffaaSAndy Whitcroftfor my $filename (@ARGV) {
66121caa13cSAndy Whitcroft	my $FILE;
6626c72ffaaSAndy Whitcroft	if ($file) {
66321caa13cSAndy Whitcroft		open($FILE, '-|', "diff -u /dev/null $filename") ||
6646c72ffaaSAndy Whitcroft			die "$P: $filename: diff failed - $!\n";
66521caa13cSAndy Whitcroft	} elsif ($filename eq '-') {
66621caa13cSAndy Whitcroft		open($FILE, '<&STDIN');
6676c72ffaaSAndy Whitcroft	} else {
66821caa13cSAndy Whitcroft		open($FILE, '<', "$filename") ||
6696c72ffaaSAndy Whitcroft			die "$P: $filename: open failed - $!\n";
6706c72ffaaSAndy Whitcroft	}
671c2fdda0dSAndy Whitcroft	if ($filename eq '-') {
672c2fdda0dSAndy Whitcroft		$vname = 'Your patch';
673c2fdda0dSAndy Whitcroft	} else {
674c2fdda0dSAndy Whitcroft		$vname = $filename;
675c2fdda0dSAndy Whitcroft	}
67621caa13cSAndy Whitcroft	while (<$FILE>) {
6770a920b5bSAndy Whitcroft		chomp;
67800df344fSAndy Whitcroft		push(@rawlines, $_);
6796c72ffaaSAndy Whitcroft	}
68021caa13cSAndy Whitcroft	close($FILE);
681c2fdda0dSAndy Whitcroft	if (!process($filename)) {
6820a920b5bSAndy Whitcroft		$exit = 1;
6830a920b5bSAndy Whitcroft	}
68400df344fSAndy Whitcroft	@rawlines = ();
68513214adfSAndy Whitcroft	@lines = ();
6863705ce5bSJoe Perches	@fixed = ();
687d752fcc8SJoe Perches	@fixed_inserted = ();
688d752fcc8SJoe Perches	@fixed_deleted = ();
689194f66fcSJoe Perches	$fixlinenr = -1;
6900a920b5bSAndy Whitcroft}
6910a920b5bSAndy Whitcroft
6920a920b5bSAndy Whitcroftexit($exit);
6930a920b5bSAndy Whitcroft
6940a920b5bSAndy Whitcroftsub top_of_kernel_tree {
6956c72ffaaSAndy Whitcroft	my ($root) = @_;
6966c72ffaaSAndy Whitcroft
6976c72ffaaSAndy Whitcroft	my @tree_check = (
6986c72ffaaSAndy Whitcroft		"COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
6996c72ffaaSAndy Whitcroft		"README", "Documentation", "arch", "include", "drivers",
7006c72ffaaSAndy Whitcroft		"fs", "init", "ipc", "kernel", "lib", "scripts",
7016c72ffaaSAndy Whitcroft	);
7026c72ffaaSAndy Whitcroft
7036c72ffaaSAndy Whitcroft	foreach my $check (@tree_check) {
7046c72ffaaSAndy Whitcroft		if (! -e $root . '/' . $check) {
7050a920b5bSAndy Whitcroft			return 0;
7060a920b5bSAndy Whitcroft		}
7076c72ffaaSAndy Whitcroft	}
7086c72ffaaSAndy Whitcroft	return 1;
7096c72ffaaSAndy Whitcroft}
7100a920b5bSAndy Whitcroft
71120112475SJoe Perchessub parse_email {
71220112475SJoe Perches	my ($formatted_email) = @_;
71320112475SJoe Perches
71420112475SJoe Perches	my $name = "";
71520112475SJoe Perches	my $address = "";
71620112475SJoe Perches	my $comment = "";
71720112475SJoe Perches
71820112475SJoe Perches	if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
71920112475SJoe Perches		$name = $1;
72020112475SJoe Perches		$address = $2;
72120112475SJoe Perches		$comment = $3 if defined $3;
72220112475SJoe Perches	} elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
72320112475SJoe Perches		$address = $1;
72420112475SJoe Perches		$comment = $2 if defined $2;
72520112475SJoe Perches	} elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
72620112475SJoe Perches		$address = $1;
72720112475SJoe Perches		$comment = $2 if defined $2;
72820112475SJoe Perches		$formatted_email =~ s/$address.*$//;
72920112475SJoe Perches		$name = $formatted_email;
7303705ce5bSJoe Perches		$name = trim($name);
73120112475SJoe Perches		$name =~ s/^\"|\"$//g;
73220112475SJoe Perches		# If there's a name left after stripping spaces and
73320112475SJoe Perches		# leading quotes, and the address doesn't have both
73420112475SJoe Perches		# leading and trailing angle brackets, the address
73520112475SJoe Perches		# is invalid. ie:
73620112475SJoe Perches		#   "joe smith [email protected]" bad
73720112475SJoe Perches		#   "joe smith <[email protected]" bad
73820112475SJoe Perches		if ($name ne "" && $address !~ /^<[^>]+>$/) {
73920112475SJoe Perches			$name = "";
74020112475SJoe Perches			$address = "";
74120112475SJoe Perches			$comment = "";
74220112475SJoe Perches		}
74320112475SJoe Perches	}
74420112475SJoe Perches
7453705ce5bSJoe Perches	$name = trim($name);
74620112475SJoe Perches	$name =~ s/^\"|\"$//g;
7473705ce5bSJoe Perches	$address = trim($address);
74820112475SJoe Perches	$address =~ s/^\<|\>$//g;
74920112475SJoe Perches
75020112475SJoe Perches	if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
75120112475SJoe Perches		$name =~ s/(?<!\\)"/\\"/g; ##escape quotes
75220112475SJoe Perches		$name = "\"$name\"";
75320112475SJoe Perches	}
75420112475SJoe Perches
75520112475SJoe Perches	return ($name, $address, $comment);
75620112475SJoe Perches}
75720112475SJoe Perches
75820112475SJoe Perchessub format_email {
75920112475SJoe Perches	my ($name, $address) = @_;
76020112475SJoe Perches
76120112475SJoe Perches	my $formatted_email;
76220112475SJoe Perches
7633705ce5bSJoe Perches	$name = trim($name);
76420112475SJoe Perches	$name =~ s/^\"|\"$//g;
7653705ce5bSJoe Perches	$address = trim($address);
76620112475SJoe Perches
76720112475SJoe Perches	if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
76820112475SJoe Perches		$name =~ s/(?<!\\)"/\\"/g; ##escape quotes
76920112475SJoe Perches		$name = "\"$name\"";
77020112475SJoe Perches	}
77120112475SJoe Perches
77220112475SJoe Perches	if ("$name" eq "") {
77320112475SJoe Perches		$formatted_email = "$address";
77420112475SJoe Perches	} else {
77520112475SJoe Perches		$formatted_email = "$name <$address>";
77620112475SJoe Perches	}
77720112475SJoe Perches
77820112475SJoe Perches	return $formatted_email;
77920112475SJoe Perches}
78020112475SJoe Perches
781d311cd44SJoe Perchessub which {
782d311cd44SJoe Perches	my ($bin) = @_;
783d311cd44SJoe Perches
784d311cd44SJoe Perches	foreach my $path (split(/:/, $ENV{PATH})) {
785d311cd44SJoe Perches		if (-e "$path/$bin") {
786d311cd44SJoe Perches			return "$path/$bin";
787d311cd44SJoe Perches		}
788d311cd44SJoe Perches	}
789d311cd44SJoe Perches
790d311cd44SJoe Perches	return "";
791d311cd44SJoe Perches}
792d311cd44SJoe Perches
793000d1cc1SJoe Perchessub which_conf {
794000d1cc1SJoe Perches	my ($conf) = @_;
795000d1cc1SJoe Perches
796000d1cc1SJoe Perches	foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
797000d1cc1SJoe Perches		if (-e "$path/$conf") {
798000d1cc1SJoe Perches			return "$path/$conf";
799000d1cc1SJoe Perches		}
800000d1cc1SJoe Perches	}
801000d1cc1SJoe Perches
802000d1cc1SJoe Perches	return "";
803000d1cc1SJoe Perches}
804000d1cc1SJoe Perches
8050a920b5bSAndy Whitcroftsub expand_tabs {
8060a920b5bSAndy Whitcroft	my ($str) = @_;
8070a920b5bSAndy Whitcroft
8080a920b5bSAndy Whitcroft	my $res = '';
8090a920b5bSAndy Whitcroft	my $n = 0;
8100a920b5bSAndy Whitcroft	for my $c (split(//, $str)) {
8110a920b5bSAndy Whitcroft		if ($c eq "\t") {
8120a920b5bSAndy Whitcroft			$res .= ' ';
8130a920b5bSAndy Whitcroft			$n++;
8140a920b5bSAndy Whitcroft			for (; ($n % 8) != 0; $n++) {
8150a920b5bSAndy Whitcroft				$res .= ' ';
8160a920b5bSAndy Whitcroft			}
8170a920b5bSAndy Whitcroft			next;
8180a920b5bSAndy Whitcroft		}
8190a920b5bSAndy Whitcroft		$res .= $c;
8200a920b5bSAndy Whitcroft		$n++;
8210a920b5bSAndy Whitcroft	}
8220a920b5bSAndy Whitcroft
8230a920b5bSAndy Whitcroft	return $res;
8240a920b5bSAndy Whitcroft}
8256c72ffaaSAndy Whitcroftsub copy_spacing {
826773647a0SAndy Whitcroft	(my $res = shift) =~ tr/\t/ /c;
8276c72ffaaSAndy Whitcroft	return $res;
8286c72ffaaSAndy Whitcroft}
8290a920b5bSAndy Whitcroft
8304a0df2efSAndy Whitcroftsub line_stats {
8314a0df2efSAndy Whitcroft	my ($line) = @_;
8324a0df2efSAndy Whitcroft
8334a0df2efSAndy Whitcroft	# Drop the diff line leader and expand tabs
8344a0df2efSAndy Whitcroft	$line =~ s/^.//;
8354a0df2efSAndy Whitcroft	$line = expand_tabs($line);
8364a0df2efSAndy Whitcroft
8374a0df2efSAndy Whitcroft	# Pick the indent from the front of the line.
8384a0df2efSAndy Whitcroft	my ($white) = ($line =~ /^(\s*)/);
8394a0df2efSAndy Whitcroft
8404a0df2efSAndy Whitcroft	return (length($line), length($white));
8414a0df2efSAndy Whitcroft}
8424a0df2efSAndy Whitcroft
843773647a0SAndy Whitcroftmy $sanitise_quote = '';
844773647a0SAndy Whitcroft
845773647a0SAndy Whitcroftsub sanitise_line_reset {
846773647a0SAndy Whitcroft	my ($in_comment) = @_;
847773647a0SAndy Whitcroft
848773647a0SAndy Whitcroft	if ($in_comment) {
849773647a0SAndy Whitcroft		$sanitise_quote = '*/';
850773647a0SAndy Whitcroft	} else {
851773647a0SAndy Whitcroft		$sanitise_quote = '';
852773647a0SAndy Whitcroft	}
853773647a0SAndy Whitcroft}
85400df344fSAndy Whitcroftsub sanitise_line {
85500df344fSAndy Whitcroft	my ($line) = @_;
85600df344fSAndy Whitcroft
85700df344fSAndy Whitcroft	my $res = '';
85800df344fSAndy Whitcroft	my $l = '';
85900df344fSAndy Whitcroft
860c2fdda0dSAndy Whitcroft	my $qlen = 0;
861773647a0SAndy Whitcroft	my $off = 0;
862773647a0SAndy Whitcroft	my $c;
86300df344fSAndy Whitcroft
864773647a0SAndy Whitcroft	# Always copy over the diff marker.
865773647a0SAndy Whitcroft	$res = substr($line, 0, 1);
866773647a0SAndy Whitcroft
867773647a0SAndy Whitcroft	for ($off = 1; $off < length($line); $off++) {
868773647a0SAndy Whitcroft		$c = substr($line, $off, 1);
869773647a0SAndy Whitcroft
870773647a0SAndy Whitcroft		# Comments we are wacking completly including the begin
871773647a0SAndy Whitcroft		# and end, all to $;.
872773647a0SAndy Whitcroft		if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
873773647a0SAndy Whitcroft			$sanitise_quote = '*/';
874773647a0SAndy Whitcroft
875773647a0SAndy Whitcroft			substr($res, $off, 2, "$;$;");
876773647a0SAndy Whitcroft			$off++;
87700df344fSAndy Whitcroft			next;
878773647a0SAndy Whitcroft		}
87981bc0e02SAndy Whitcroft		if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
880773647a0SAndy Whitcroft			$sanitise_quote = '';
881773647a0SAndy Whitcroft			substr($res, $off, 2, "$;$;");
882773647a0SAndy Whitcroft			$off++;
883773647a0SAndy Whitcroft			next;
884773647a0SAndy Whitcroft		}
885113f04a8SDaniel Walker		if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
886113f04a8SDaniel Walker			$sanitise_quote = '//';
887113f04a8SDaniel Walker
888113f04a8SDaniel Walker			substr($res, $off, 2, $sanitise_quote);
889113f04a8SDaniel Walker			$off++;
890113f04a8SDaniel Walker			next;
891113f04a8SDaniel Walker		}
892773647a0SAndy Whitcroft
893773647a0SAndy Whitcroft		# A \ in a string means ignore the next character.
894773647a0SAndy Whitcroft		if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
895773647a0SAndy Whitcroft		    $c eq "\\") {
896773647a0SAndy Whitcroft			substr($res, $off, 2, 'XX');
897773647a0SAndy Whitcroft			$off++;
898773647a0SAndy Whitcroft			next;
899773647a0SAndy Whitcroft		}
900773647a0SAndy Whitcroft		# Regular quotes.
901773647a0SAndy Whitcroft		if ($c eq "'" || $c eq '"') {
902773647a0SAndy Whitcroft			if ($sanitise_quote eq '') {
903773647a0SAndy Whitcroft				$sanitise_quote = $c;
904773647a0SAndy Whitcroft
905773647a0SAndy Whitcroft				substr($res, $off, 1, $c);
906773647a0SAndy Whitcroft				next;
907773647a0SAndy Whitcroft			} elsif ($sanitise_quote eq $c) {
908773647a0SAndy Whitcroft				$sanitise_quote = '';
90900df344fSAndy Whitcroft			}
91000df344fSAndy Whitcroft		}
911773647a0SAndy Whitcroft
912fae17daeSAndy Whitcroft		#print "c<$c> SQ<$sanitise_quote>\n";
913773647a0SAndy Whitcroft		if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
914773647a0SAndy Whitcroft			substr($res, $off, 1, $;);
915113f04a8SDaniel Walker		} elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
916113f04a8SDaniel Walker			substr($res, $off, 1, $;);
917773647a0SAndy Whitcroft		} elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
918773647a0SAndy Whitcroft			substr($res, $off, 1, 'X');
91900df344fSAndy Whitcroft		} else {
920773647a0SAndy Whitcroft			substr($res, $off, 1, $c);
92100df344fSAndy Whitcroft		}
922c2fdda0dSAndy Whitcroft	}
923c2fdda0dSAndy Whitcroft
924113f04a8SDaniel Walker	if ($sanitise_quote eq '//') {
925113f04a8SDaniel Walker		$sanitise_quote = '';
926113f04a8SDaniel Walker	}
927113f04a8SDaniel Walker
928c2fdda0dSAndy Whitcroft	# The pathname on a #include may be surrounded by '<' and '>'.
929c45dcabdSAndy Whitcroft	if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
930c2fdda0dSAndy Whitcroft		my $clean = 'X' x length($1);
931c2fdda0dSAndy Whitcroft		$res =~ s@\<.*\>@<$clean>@;
932c2fdda0dSAndy Whitcroft
933c2fdda0dSAndy Whitcroft	# The whole of a #error is a string.
934c45dcabdSAndy Whitcroft	} elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
935c2fdda0dSAndy Whitcroft		my $clean = 'X' x length($1);
936c45dcabdSAndy Whitcroft		$res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
937c2fdda0dSAndy Whitcroft	}
938c2fdda0dSAndy Whitcroft
93900df344fSAndy Whitcroft	return $res;
94000df344fSAndy Whitcroft}
94100df344fSAndy Whitcroft
942a6962d72SJoe Perchessub get_quoted_string {
943a6962d72SJoe Perches	my ($line, $rawline) = @_;
944a6962d72SJoe Perches
945a6962d72SJoe Perches	return "" if ($line !~ m/(\"[X]+\")/g);
946a6962d72SJoe Perches	return substr($rawline, $-[0], $+[0] - $-[0]);
947a6962d72SJoe Perches}
948a6962d72SJoe Perches
9498905a67cSAndy Whitcroftsub ctx_statement_block {
9508905a67cSAndy Whitcroft	my ($linenr, $remain, $off) = @_;
9518905a67cSAndy Whitcroft	my $line = $linenr - 1;
9528905a67cSAndy Whitcroft	my $blk = '';
9538905a67cSAndy Whitcroft	my $soff = $off;
9548905a67cSAndy Whitcroft	my $coff = $off - 1;
955773647a0SAndy Whitcroft	my $coff_set = 0;
9568905a67cSAndy Whitcroft
95713214adfSAndy Whitcroft	my $loff = 0;
95813214adfSAndy Whitcroft
9598905a67cSAndy Whitcroft	my $type = '';
9608905a67cSAndy Whitcroft	my $level = 0;
961a2750645SAndy Whitcroft	my @stack = ();
962cf655043SAndy Whitcroft	my $p;
9638905a67cSAndy Whitcroft	my $c;
9648905a67cSAndy Whitcroft	my $len = 0;
96513214adfSAndy Whitcroft
96613214adfSAndy Whitcroft	my $remainder;
9678905a67cSAndy Whitcroft	while (1) {
968a2750645SAndy Whitcroft		@stack = (['', 0]) if ($#stack == -1);
969a2750645SAndy Whitcroft
970773647a0SAndy Whitcroft		#warn "CSB: blk<$blk> remain<$remain>\n";
9718905a67cSAndy Whitcroft		# If we are about to drop off the end, pull in more
9728905a67cSAndy Whitcroft		# context.
9738905a67cSAndy Whitcroft		if ($off >= $len) {
9748905a67cSAndy Whitcroft			for (; $remain > 0; $line++) {
975dea33496SAndy Whitcroft				last if (!defined $lines[$line]);
976c2fdda0dSAndy Whitcroft				next if ($lines[$line] =~ /^-/);
9778905a67cSAndy Whitcroft				$remain--;
97813214adfSAndy Whitcroft				$loff = $len;
979c2fdda0dSAndy Whitcroft				$blk .= $lines[$line] . "\n";
9808905a67cSAndy Whitcroft				$len = length($blk);
9818905a67cSAndy Whitcroft				$line++;
9828905a67cSAndy Whitcroft				last;
9838905a67cSAndy Whitcroft			}
9848905a67cSAndy Whitcroft			# Bail if there is no further context.
9858905a67cSAndy Whitcroft			#warn "CSB: blk<$blk> off<$off> len<$len>\n";
98613214adfSAndy Whitcroft			if ($off >= $len) {
9878905a67cSAndy Whitcroft				last;
9888905a67cSAndy Whitcroft			}
989f74bd194SAndy Whitcroft			if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
990f74bd194SAndy Whitcroft				$level++;
991f74bd194SAndy Whitcroft				$type = '#';
992f74bd194SAndy Whitcroft			}
9938905a67cSAndy Whitcroft		}
994cf655043SAndy Whitcroft		$p = $c;
9958905a67cSAndy Whitcroft		$c = substr($blk, $off, 1);
99613214adfSAndy Whitcroft		$remainder = substr($blk, $off);
9978905a67cSAndy Whitcroft
998773647a0SAndy Whitcroft		#warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
9994635f4fbSAndy Whitcroft
10004635f4fbSAndy Whitcroft		# Handle nested #if/#else.
10014635f4fbSAndy Whitcroft		if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
10024635f4fbSAndy Whitcroft			push(@stack, [ $type, $level ]);
10034635f4fbSAndy Whitcroft		} elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
10044635f4fbSAndy Whitcroft			($type, $level) = @{$stack[$#stack - 1]};
10054635f4fbSAndy Whitcroft		} elsif ($remainder =~ /^#\s*endif\b/) {
10064635f4fbSAndy Whitcroft			($type, $level) = @{pop(@stack)};
10074635f4fbSAndy Whitcroft		}
10084635f4fbSAndy Whitcroft
10098905a67cSAndy Whitcroft		# Statement ends at the ';' or a close '}' at the
10108905a67cSAndy Whitcroft		# outermost level.
10118905a67cSAndy Whitcroft		if ($level == 0 && $c eq ';') {
10128905a67cSAndy Whitcroft			last;
10138905a67cSAndy Whitcroft		}
10148905a67cSAndy Whitcroft
101513214adfSAndy Whitcroft		# An else is really a conditional as long as its not else if
1016773647a0SAndy Whitcroft		if ($level == 0 && $coff_set == 0 &&
1017773647a0SAndy Whitcroft				(!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
1018773647a0SAndy Whitcroft				$remainder =~ /^(else)(?:\s|{)/ &&
1019773647a0SAndy Whitcroft				$remainder !~ /^else\s+if\b/) {
1020773647a0SAndy Whitcroft			$coff = $off + length($1) - 1;
1021773647a0SAndy Whitcroft			$coff_set = 1;
1022773647a0SAndy Whitcroft			#warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
1023773647a0SAndy Whitcroft			#warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
102413214adfSAndy Whitcroft		}
102513214adfSAndy Whitcroft
10268905a67cSAndy Whitcroft		if (($type eq '' || $type eq '(') && $c eq '(') {
10278905a67cSAndy Whitcroft			$level++;
10288905a67cSAndy Whitcroft			$type = '(';
10298905a67cSAndy Whitcroft		}
10308905a67cSAndy Whitcroft		if ($type eq '(' && $c eq ')') {
10318905a67cSAndy Whitcroft			$level--;
10328905a67cSAndy Whitcroft			$type = ($level != 0)? '(' : '';
10338905a67cSAndy Whitcroft
10348905a67cSAndy Whitcroft			if ($level == 0 && $coff < $soff) {
10358905a67cSAndy Whitcroft				$coff = $off;
1036773647a0SAndy Whitcroft				$coff_set = 1;
1037773647a0SAndy Whitcroft				#warn "CSB: mark coff<$coff>\n";
10388905a67cSAndy Whitcroft			}
10398905a67cSAndy Whitcroft		}
10408905a67cSAndy Whitcroft		if (($type eq '' || $type eq '{') && $c eq '{') {
10418905a67cSAndy Whitcroft			$level++;
10428905a67cSAndy Whitcroft			$type = '{';
10438905a67cSAndy Whitcroft		}
10448905a67cSAndy Whitcroft		if ($type eq '{' && $c eq '}') {
10458905a67cSAndy Whitcroft			$level--;
10468905a67cSAndy Whitcroft			$type = ($level != 0)? '{' : '';
10478905a67cSAndy Whitcroft
10488905a67cSAndy Whitcroft			if ($level == 0) {
1049b998e001SPatrick Pannuto				if (substr($blk, $off + 1, 1) eq ';') {
1050b998e001SPatrick Pannuto					$off++;
1051b998e001SPatrick Pannuto				}
10528905a67cSAndy Whitcroft				last;
10538905a67cSAndy Whitcroft			}
10548905a67cSAndy Whitcroft		}
1055f74bd194SAndy Whitcroft		# Preprocessor commands end at the newline unless escaped.
1056f74bd194SAndy Whitcroft		if ($type eq '#' && $c eq "\n" && $p ne "\\") {
1057f74bd194SAndy Whitcroft			$level--;
1058f74bd194SAndy Whitcroft			$type = '';
1059f74bd194SAndy Whitcroft			$off++;
1060f74bd194SAndy Whitcroft			last;
1061f74bd194SAndy Whitcroft		}
10628905a67cSAndy Whitcroft		$off++;
10638905a67cSAndy Whitcroft	}
1064a3bb97a7SAndy Whitcroft	# We are truly at the end, so shuffle to the next line.
106513214adfSAndy Whitcroft	if ($off == $len) {
1066a3bb97a7SAndy Whitcroft		$loff = $len + 1;
106713214adfSAndy Whitcroft		$line++;
106813214adfSAndy Whitcroft		$remain--;
106913214adfSAndy Whitcroft	}
10708905a67cSAndy Whitcroft
10718905a67cSAndy Whitcroft	my $statement = substr($blk, $soff, $off - $soff + 1);
10728905a67cSAndy Whitcroft	my $condition = substr($blk, $soff, $coff - $soff + 1);
10738905a67cSAndy Whitcroft
10748905a67cSAndy Whitcroft	#warn "STATEMENT<$statement>\n";
10758905a67cSAndy Whitcroft	#warn "CONDITION<$condition>\n";
10768905a67cSAndy Whitcroft
1077773647a0SAndy Whitcroft	#print "coff<$coff> soff<$off> loff<$loff>\n";
107813214adfSAndy Whitcroft
107913214adfSAndy Whitcroft	return ($statement, $condition,
108013214adfSAndy Whitcroft			$line, $remain + 1, $off - $loff + 1, $level);
108113214adfSAndy Whitcroft}
108213214adfSAndy Whitcroft
1083cf655043SAndy Whitcroftsub statement_lines {
1084cf655043SAndy Whitcroft	my ($stmt) = @_;
1085cf655043SAndy Whitcroft
1086cf655043SAndy Whitcroft	# Strip the diff line prefixes and rip blank lines at start and end.
1087cf655043SAndy Whitcroft	$stmt =~ s/(^|\n)./$1/g;
1088cf655043SAndy Whitcroft	$stmt =~ s/^\s*//;
1089cf655043SAndy Whitcroft	$stmt =~ s/\s*$//;
1090cf655043SAndy Whitcroft
1091cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
1092cf655043SAndy Whitcroft
1093cf655043SAndy Whitcroft	return $#stmt_lines + 2;
1094cf655043SAndy Whitcroft}
1095cf655043SAndy Whitcroft
1096cf655043SAndy Whitcroftsub statement_rawlines {
1097cf655043SAndy Whitcroft	my ($stmt) = @_;
1098cf655043SAndy Whitcroft
1099cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
1100cf655043SAndy Whitcroft
1101cf655043SAndy Whitcroft	return $#stmt_lines + 2;
1102cf655043SAndy Whitcroft}
1103cf655043SAndy Whitcroft
1104cf655043SAndy Whitcroftsub statement_block_size {
1105cf655043SAndy Whitcroft	my ($stmt) = @_;
1106cf655043SAndy Whitcroft
1107cf655043SAndy Whitcroft	$stmt =~ s/(^|\n)./$1/g;
1108cf655043SAndy Whitcroft	$stmt =~ s/^\s*{//;
1109cf655043SAndy Whitcroft	$stmt =~ s/}\s*$//;
1110cf655043SAndy Whitcroft	$stmt =~ s/^\s*//;
1111cf655043SAndy Whitcroft	$stmt =~ s/\s*$//;
1112cf655043SAndy Whitcroft
1113cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
1114cf655043SAndy Whitcroft	my @stmt_statements = ($stmt =~ /;/g);
1115cf655043SAndy Whitcroft
1116cf655043SAndy Whitcroft	my $stmt_lines = $#stmt_lines + 2;
1117cf655043SAndy Whitcroft	my $stmt_statements = $#stmt_statements + 1;
1118cf655043SAndy Whitcroft
1119cf655043SAndy Whitcroft	if ($stmt_lines > $stmt_statements) {
1120cf655043SAndy Whitcroft		return $stmt_lines;
1121cf655043SAndy Whitcroft	} else {
1122cf655043SAndy Whitcroft		return $stmt_statements;
1123cf655043SAndy Whitcroft	}
1124cf655043SAndy Whitcroft}
1125cf655043SAndy Whitcroft
112613214adfSAndy Whitcroftsub ctx_statement_full {
112713214adfSAndy Whitcroft	my ($linenr, $remain, $off) = @_;
112813214adfSAndy Whitcroft	my ($statement, $condition, $level);
112913214adfSAndy Whitcroft
113013214adfSAndy Whitcroft	my (@chunks);
113113214adfSAndy Whitcroft
1132cf655043SAndy Whitcroft	# Grab the first conditional/block pair.
113313214adfSAndy Whitcroft	($statement, $condition, $linenr, $remain, $off, $level) =
113413214adfSAndy Whitcroft				ctx_statement_block($linenr, $remain, $off);
1135773647a0SAndy Whitcroft	#print "F: c<$condition> s<$statement> remain<$remain>\n";
113613214adfSAndy Whitcroft	push(@chunks, [ $condition, $statement ]);
1137cf655043SAndy Whitcroft	if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
1138cf655043SAndy Whitcroft		return ($level, $linenr, @chunks);
1139cf655043SAndy Whitcroft	}
1140cf655043SAndy Whitcroft
1141cf655043SAndy Whitcroft	# Pull in the following conditional/block pairs and see if they
1142cf655043SAndy Whitcroft	# could continue the statement.
1143cf655043SAndy Whitcroft	for (;;) {
114413214adfSAndy Whitcroft		($statement, $condition, $linenr, $remain, $off, $level) =
114513214adfSAndy Whitcroft				ctx_statement_block($linenr, $remain, $off);
1146cf655043SAndy Whitcroft		#print "C: c<$condition> s<$statement> remain<$remain>\n";
1147773647a0SAndy Whitcroft		last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
1148cf655043SAndy Whitcroft		#print "C: push\n";
1149cf655043SAndy Whitcroft		push(@chunks, [ $condition, $statement ]);
115013214adfSAndy Whitcroft	}
115113214adfSAndy Whitcroft
115213214adfSAndy Whitcroft	return ($level, $linenr, @chunks);
11538905a67cSAndy Whitcroft}
11548905a67cSAndy Whitcroft
11554a0df2efSAndy Whitcroftsub ctx_block_get {
1156f0a594c1SAndy Whitcroft	my ($linenr, $remain, $outer, $open, $close, $off) = @_;
11574a0df2efSAndy Whitcroft	my $line;
11584a0df2efSAndy Whitcroft	my $start = $linenr - 1;
11594a0df2efSAndy Whitcroft	my $blk = '';
11604a0df2efSAndy Whitcroft	my @o;
11614a0df2efSAndy Whitcroft	my @c;
11624a0df2efSAndy Whitcroft	my @res = ();
11634a0df2efSAndy Whitcroft
1164f0a594c1SAndy Whitcroft	my $level = 0;
11654635f4fbSAndy Whitcroft	my @stack = ($level);
116600df344fSAndy Whitcroft	for ($line = $start; $remain > 0; $line++) {
116700df344fSAndy Whitcroft		next if ($rawlines[$line] =~ /^-/);
116800df344fSAndy Whitcroft		$remain--;
116900df344fSAndy Whitcroft
117000df344fSAndy Whitcroft		$blk .= $rawlines[$line];
11714635f4fbSAndy Whitcroft
11724635f4fbSAndy Whitcroft		# Handle nested #if/#else.
117301464f30SAndy Whitcroft		if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
11744635f4fbSAndy Whitcroft			push(@stack, $level);
117501464f30SAndy Whitcroft		} elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
11764635f4fbSAndy Whitcroft			$level = $stack[$#stack - 1];
117701464f30SAndy Whitcroft		} elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
11784635f4fbSAndy Whitcroft			$level = pop(@stack);
11794635f4fbSAndy Whitcroft		}
11804635f4fbSAndy Whitcroft
118101464f30SAndy Whitcroft		foreach my $c (split(//, $lines[$line])) {
1182f0a594c1SAndy Whitcroft			##print "C<$c>L<$level><$open$close>O<$off>\n";
1183f0a594c1SAndy Whitcroft			if ($off > 0) {
1184f0a594c1SAndy Whitcroft				$off--;
1185f0a594c1SAndy Whitcroft				next;
1186f0a594c1SAndy Whitcroft			}
11874a0df2efSAndy Whitcroft
1188f0a594c1SAndy Whitcroft			if ($c eq $close && $level > 0) {
1189f0a594c1SAndy Whitcroft				$level--;
1190f0a594c1SAndy Whitcroft				last if ($level == 0);
1191f0a594c1SAndy Whitcroft			} elsif ($c eq $open) {
1192f0a594c1SAndy Whitcroft				$level++;
1193f0a594c1SAndy Whitcroft			}
1194f0a594c1SAndy Whitcroft		}
11954a0df2efSAndy Whitcroft
1196f0a594c1SAndy Whitcroft		if (!$outer || $level <= 1) {
119700df344fSAndy Whitcroft			push(@res, $rawlines[$line]);
11984a0df2efSAndy Whitcroft		}
11994a0df2efSAndy Whitcroft
1200f0a594c1SAndy Whitcroft		last if ($level == 0);
12014a0df2efSAndy Whitcroft	}
12024a0df2efSAndy Whitcroft
1203f0a594c1SAndy Whitcroft	return ($level, @res);
12044a0df2efSAndy Whitcroft}
12054a0df2efSAndy Whitcroftsub ctx_block_outer {
12064a0df2efSAndy Whitcroft	my ($linenr, $remain) = @_;
12074a0df2efSAndy Whitcroft
1208f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
1209f0a594c1SAndy Whitcroft	return @r;
12104a0df2efSAndy Whitcroft}
12114a0df2efSAndy Whitcroftsub ctx_block {
12124a0df2efSAndy Whitcroft	my ($linenr, $remain) = @_;
12134a0df2efSAndy Whitcroft
1214f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1215f0a594c1SAndy Whitcroft	return @r;
1216653d4876SAndy Whitcroft}
1217653d4876SAndy Whitcroftsub ctx_statement {
1218f0a594c1SAndy Whitcroft	my ($linenr, $remain, $off) = @_;
1219f0a594c1SAndy Whitcroft
1220f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1221f0a594c1SAndy Whitcroft	return @r;
1222f0a594c1SAndy Whitcroft}
1223f0a594c1SAndy Whitcroftsub ctx_block_level {
1224653d4876SAndy Whitcroft	my ($linenr, $remain) = @_;
1225653d4876SAndy Whitcroft
1226f0a594c1SAndy Whitcroft	return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
12274a0df2efSAndy Whitcroft}
12289c0ca6f9SAndy Whitcroftsub ctx_statement_level {
12299c0ca6f9SAndy Whitcroft	my ($linenr, $remain, $off) = @_;
12309c0ca6f9SAndy Whitcroft
12319c0ca6f9SAndy Whitcroft	return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
12329c0ca6f9SAndy Whitcroft}
12334a0df2efSAndy Whitcroft
12344a0df2efSAndy Whitcroftsub ctx_locate_comment {
12354a0df2efSAndy Whitcroft	my ($first_line, $end_line) = @_;
12364a0df2efSAndy Whitcroft
12374a0df2efSAndy Whitcroft	# Catch a comment on the end of the line itself.
1238beae6332SAndy Whitcroft	my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
12394a0df2efSAndy Whitcroft	return $current_comment if (defined $current_comment);
12404a0df2efSAndy Whitcroft
12414a0df2efSAndy Whitcroft	# Look through the context and try and figure out if there is a
12424a0df2efSAndy Whitcroft	# comment.
12434a0df2efSAndy Whitcroft	my $in_comment = 0;
12444a0df2efSAndy Whitcroft	$current_comment = '';
12454a0df2efSAndy Whitcroft	for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
124600df344fSAndy Whitcroft		my $line = $rawlines[$linenr - 1];
124700df344fSAndy Whitcroft		#warn "           $line\n";
12484a0df2efSAndy Whitcroft		if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
12494a0df2efSAndy Whitcroft			$in_comment = 1;
12504a0df2efSAndy Whitcroft		}
12514a0df2efSAndy Whitcroft		if ($line =~ m@/\*@) {
12524a0df2efSAndy Whitcroft			$in_comment = 1;
12534a0df2efSAndy Whitcroft		}
12544a0df2efSAndy Whitcroft		if (!$in_comment && $current_comment ne '') {
12554a0df2efSAndy Whitcroft			$current_comment = '';
12564a0df2efSAndy Whitcroft		}
12574a0df2efSAndy Whitcroft		$current_comment .= $line . "\n" if ($in_comment);
12584a0df2efSAndy Whitcroft		if ($line =~ m@\*/@) {
12594a0df2efSAndy Whitcroft			$in_comment = 0;
12604a0df2efSAndy Whitcroft		}
12614a0df2efSAndy Whitcroft	}
12624a0df2efSAndy Whitcroft
12634a0df2efSAndy Whitcroft	chomp($current_comment);
12644a0df2efSAndy Whitcroft	return($current_comment);
12654a0df2efSAndy Whitcroft}
12664a0df2efSAndy Whitcroftsub ctx_has_comment {
12674a0df2efSAndy Whitcroft	my ($first_line, $end_line) = @_;
12684a0df2efSAndy Whitcroft	my $cmt = ctx_locate_comment($first_line, $end_line);
12694a0df2efSAndy Whitcroft
127000df344fSAndy Whitcroft	##print "LINE: $rawlines[$end_line - 1 ]\n";
12714a0df2efSAndy Whitcroft	##print "CMMT: $cmt\n";
12724a0df2efSAndy Whitcroft
12734a0df2efSAndy Whitcroft	return ($cmt ne '');
12744a0df2efSAndy Whitcroft}
12754a0df2efSAndy Whitcroft
12764d001e4dSAndy Whitcroftsub raw_line {
12774d001e4dSAndy Whitcroft	my ($linenr, $cnt) = @_;
12784d001e4dSAndy Whitcroft
12794d001e4dSAndy Whitcroft	my $offset = $linenr - 1;
12804d001e4dSAndy Whitcroft	$cnt++;
12814d001e4dSAndy Whitcroft
12824d001e4dSAndy Whitcroft	my $line;
12834d001e4dSAndy Whitcroft	while ($cnt) {
12844d001e4dSAndy Whitcroft		$line = $rawlines[$offset++];
12854d001e4dSAndy Whitcroft		next if (defined($line) && $line =~ /^-/);
12864d001e4dSAndy Whitcroft		$cnt--;
12874d001e4dSAndy Whitcroft	}
12884d001e4dSAndy Whitcroft
12894d001e4dSAndy Whitcroft	return $line;
12904d001e4dSAndy Whitcroft}
12914d001e4dSAndy Whitcroft
12920a920b5bSAndy Whitcroftsub cat_vet {
12930a920b5bSAndy Whitcroft	my ($vet) = @_;
12949c0ca6f9SAndy Whitcroft	my ($res, $coded);
12950a920b5bSAndy Whitcroft
12969c0ca6f9SAndy Whitcroft	$res = '';
12976c72ffaaSAndy Whitcroft	while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
12986c72ffaaSAndy Whitcroft		$res .= $1;
12996c72ffaaSAndy Whitcroft		if ($2 ne '') {
13009c0ca6f9SAndy Whitcroft			$coded = sprintf("^%c", unpack('C', $2) + 64);
13016c72ffaaSAndy Whitcroft			$res .= $coded;
13026c72ffaaSAndy Whitcroft		}
13039c0ca6f9SAndy Whitcroft	}
13049c0ca6f9SAndy Whitcroft	$res =~ s/$/\$/;
13050a920b5bSAndy Whitcroft
13069c0ca6f9SAndy Whitcroft	return $res;
13070a920b5bSAndy Whitcroft}
13080a920b5bSAndy Whitcroft
1309c2fdda0dSAndy Whitcroftmy $av_preprocessor = 0;
1310cf655043SAndy Whitcroftmy $av_pending;
1311c2fdda0dSAndy Whitcroftmy @av_paren_type;
13121f65f947SAndy Whitcroftmy $av_pend_colon;
1313c2fdda0dSAndy Whitcroft
1314c2fdda0dSAndy Whitcroftsub annotate_reset {
1315c2fdda0dSAndy Whitcroft	$av_preprocessor = 0;
1316cf655043SAndy Whitcroft	$av_pending = '_';
1317cf655043SAndy Whitcroft	@av_paren_type = ('E');
13181f65f947SAndy Whitcroft	$av_pend_colon = 'O';
1319c2fdda0dSAndy Whitcroft}
1320c2fdda0dSAndy Whitcroft
13216c72ffaaSAndy Whitcroftsub annotate_values {
13226c72ffaaSAndy Whitcroft	my ($stream, $type) = @_;
13236c72ffaaSAndy Whitcroft
13246c72ffaaSAndy Whitcroft	my $res;
13251f65f947SAndy Whitcroft	my $var = '_' x length($stream);
13266c72ffaaSAndy Whitcroft	my $cur = $stream;
13276c72ffaaSAndy Whitcroft
1328c2fdda0dSAndy Whitcroft	print "$stream\n" if ($dbg_values > 1);
13296c72ffaaSAndy Whitcroft
13306c72ffaaSAndy Whitcroft	while (length($cur)) {
1331773647a0SAndy Whitcroft		@av_paren_type = ('E') if ($#av_paren_type < 0);
1332cf655043SAndy Whitcroft		print " <" . join('', @av_paren_type) .
1333171ae1a4SAndy Whitcroft				"> <$type> <$av_pending>" if ($dbg_values > 1);
13346c72ffaaSAndy Whitcroft		if ($cur =~ /^(\s+)/o) {
1335c2fdda0dSAndy Whitcroft			print "WS($1)\n" if ($dbg_values > 1);
1336c2fdda0dSAndy Whitcroft			if ($1 =~ /\n/ && $av_preprocessor) {
1337cf655043SAndy Whitcroft				$type = pop(@av_paren_type);
1338c2fdda0dSAndy Whitcroft				$av_preprocessor = 0;
13396c72ffaaSAndy Whitcroft			}
13406c72ffaaSAndy Whitcroft
1341c023e473SFlorian Mickler		} elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
13429446ef56SAndy Whitcroft			print "CAST($1)\n" if ($dbg_values > 1);
13439446ef56SAndy Whitcroft			push(@av_paren_type, $type);
1344addcdceaSAndy Whitcroft			$type = 'c';
13459446ef56SAndy Whitcroft
1346e91b6e26SAndy Whitcroft		} elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
1347c2fdda0dSAndy Whitcroft			print "DECLARE($1)\n" if ($dbg_values > 1);
13486c72ffaaSAndy Whitcroft			$type = 'T';
13496c72ffaaSAndy Whitcroft
1350389a2fe5SAndy Whitcroft		} elsif ($cur =~ /^($Modifier)\s*/) {
1351389a2fe5SAndy Whitcroft			print "MODIFIER($1)\n" if ($dbg_values > 1);
1352389a2fe5SAndy Whitcroft			$type = 'T';
1353389a2fe5SAndy Whitcroft
1354c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
1355171ae1a4SAndy Whitcroft			print "DEFINE($1,$2)\n" if ($dbg_values > 1);
1356c2fdda0dSAndy Whitcroft			$av_preprocessor = 1;
1357171ae1a4SAndy Whitcroft			push(@av_paren_type, $type);
1358171ae1a4SAndy Whitcroft			if ($2 ne '') {
1359cf655043SAndy Whitcroft				$av_pending = 'N';
1360171ae1a4SAndy Whitcroft			}
1361171ae1a4SAndy Whitcroft			$type = 'E';
1362171ae1a4SAndy Whitcroft
1363c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
1364171ae1a4SAndy Whitcroft			print "UNDEF($1)\n" if ($dbg_values > 1);
1365171ae1a4SAndy Whitcroft			$av_preprocessor = 1;
1366171ae1a4SAndy Whitcroft			push(@av_paren_type, $type);
13676c72ffaaSAndy Whitcroft
1368c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
1369cf655043SAndy Whitcroft			print "PRE_START($1)\n" if ($dbg_values > 1);
1370c2fdda0dSAndy Whitcroft			$av_preprocessor = 1;
1371cf655043SAndy Whitcroft
1372cf655043SAndy Whitcroft			push(@av_paren_type, $type);
1373cf655043SAndy Whitcroft			push(@av_paren_type, $type);
1374171ae1a4SAndy Whitcroft			$type = 'E';
1375cf655043SAndy Whitcroft
1376c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
1377cf655043SAndy Whitcroft			print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1378cf655043SAndy Whitcroft			$av_preprocessor = 1;
1379cf655043SAndy Whitcroft
1380cf655043SAndy Whitcroft			push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1381cf655043SAndy Whitcroft
1382171ae1a4SAndy Whitcroft			$type = 'E';
1383cf655043SAndy Whitcroft
1384c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:endif))/o) {
1385cf655043SAndy Whitcroft			print "PRE_END($1)\n" if ($dbg_values > 1);
1386cf655043SAndy Whitcroft
1387cf655043SAndy Whitcroft			$av_preprocessor = 1;
1388cf655043SAndy Whitcroft
1389cf655043SAndy Whitcroft			# Assume all arms of the conditional end as this
1390cf655043SAndy Whitcroft			# one does, and continue as if the #endif was not here.
1391cf655043SAndy Whitcroft			pop(@av_paren_type);
1392cf655043SAndy Whitcroft			push(@av_paren_type, $type);
1393171ae1a4SAndy Whitcroft			$type = 'E';
13946c72ffaaSAndy Whitcroft
13956c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\\\n)/o) {
1396c2fdda0dSAndy Whitcroft			print "PRECONT($1)\n" if ($dbg_values > 1);
13976c72ffaaSAndy Whitcroft
1398171ae1a4SAndy Whitcroft		} elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1399171ae1a4SAndy Whitcroft			print "ATTR($1)\n" if ($dbg_values > 1);
1400171ae1a4SAndy Whitcroft			$av_pending = $type;
1401171ae1a4SAndy Whitcroft			$type = 'N';
1402171ae1a4SAndy Whitcroft
14036c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
1404c2fdda0dSAndy Whitcroft			print "SIZEOF($1)\n" if ($dbg_values > 1);
14056c72ffaaSAndy Whitcroft			if (defined $2) {
1406cf655043SAndy Whitcroft				$av_pending = 'V';
14076c72ffaaSAndy Whitcroft			}
14086c72ffaaSAndy Whitcroft			$type = 'N';
14096c72ffaaSAndy Whitcroft
141014b111c1SAndy Whitcroft		} elsif ($cur =~ /^(if|while|for)\b/o) {
1411c2fdda0dSAndy Whitcroft			print "COND($1)\n" if ($dbg_values > 1);
141214b111c1SAndy Whitcroft			$av_pending = 'E';
14136c72ffaaSAndy Whitcroft			$type = 'N';
14146c72ffaaSAndy Whitcroft
14151f65f947SAndy Whitcroft		} elsif ($cur =~/^(case)/o) {
14161f65f947SAndy Whitcroft			print "CASE($1)\n" if ($dbg_values > 1);
14171f65f947SAndy Whitcroft			$av_pend_colon = 'C';
14181f65f947SAndy Whitcroft			$type = 'N';
14191f65f947SAndy Whitcroft
142014b111c1SAndy Whitcroft		} elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
1421c2fdda0dSAndy Whitcroft			print "KEYWORD($1)\n" if ($dbg_values > 1);
14226c72ffaaSAndy Whitcroft			$type = 'N';
14236c72ffaaSAndy Whitcroft
14246c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\()/o) {
1425c2fdda0dSAndy Whitcroft			print "PAREN('$1')\n" if ($dbg_values > 1);
1426cf655043SAndy Whitcroft			push(@av_paren_type, $av_pending);
1427cf655043SAndy Whitcroft			$av_pending = '_';
14286c72ffaaSAndy Whitcroft			$type = 'N';
14296c72ffaaSAndy Whitcroft
14306c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\))/o) {
1431cf655043SAndy Whitcroft			my $new_type = pop(@av_paren_type);
1432cf655043SAndy Whitcroft			if ($new_type ne '_') {
1433cf655043SAndy Whitcroft				$type = $new_type;
1434c2fdda0dSAndy Whitcroft				print "PAREN('$1') -> $type\n"
1435c2fdda0dSAndy Whitcroft							if ($dbg_values > 1);
14366c72ffaaSAndy Whitcroft			} else {
1437c2fdda0dSAndy Whitcroft				print "PAREN('$1')\n" if ($dbg_values > 1);
14386c72ffaaSAndy Whitcroft			}
14396c72ffaaSAndy Whitcroft
1440c8cb2ca3SAndy Whitcroft		} elsif ($cur =~ /^($Ident)\s*\(/o) {
1441c2fdda0dSAndy Whitcroft			print "FUNC($1)\n" if ($dbg_values > 1);
1442c8cb2ca3SAndy Whitcroft			$type = 'V';
1443cf655043SAndy Whitcroft			$av_pending = 'V';
14446c72ffaaSAndy Whitcroft
14458e761b04SAndy Whitcroft		} elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
14468e761b04SAndy Whitcroft			if (defined $2 && $type eq 'C' || $type eq 'T') {
14471f65f947SAndy Whitcroft				$av_pend_colon = 'B';
14488e761b04SAndy Whitcroft			} elsif ($type eq 'E') {
14498e761b04SAndy Whitcroft				$av_pend_colon = 'L';
14501f65f947SAndy Whitcroft			}
14511f65f947SAndy Whitcroft			print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
14521f65f947SAndy Whitcroft			$type = 'V';
14531f65f947SAndy Whitcroft
14546c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Ident|$Constant)/o) {
1455c2fdda0dSAndy Whitcroft			print "IDENT($1)\n" if ($dbg_values > 1);
14566c72ffaaSAndy Whitcroft			$type = 'V';
14576c72ffaaSAndy Whitcroft
14586c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Assignment)/o) {
1459c2fdda0dSAndy Whitcroft			print "ASSIGN($1)\n" if ($dbg_values > 1);
14606c72ffaaSAndy Whitcroft			$type = 'N';
14616c72ffaaSAndy Whitcroft
1462cf655043SAndy Whitcroft		} elsif ($cur =~/^(;|{|})/) {
1463c2fdda0dSAndy Whitcroft			print "END($1)\n" if ($dbg_values > 1);
146413214adfSAndy Whitcroft			$type = 'E';
14651f65f947SAndy Whitcroft			$av_pend_colon = 'O';
146613214adfSAndy Whitcroft
14678e761b04SAndy Whitcroft		} elsif ($cur =~/^(,)/) {
14688e761b04SAndy Whitcroft			print "COMMA($1)\n" if ($dbg_values > 1);
14698e761b04SAndy Whitcroft			$type = 'C';
14708e761b04SAndy Whitcroft
14711f65f947SAndy Whitcroft		} elsif ($cur =~ /^(\?)/o) {
14721f65f947SAndy Whitcroft			print "QUESTION($1)\n" if ($dbg_values > 1);
14731f65f947SAndy Whitcroft			$type = 'N';
14741f65f947SAndy Whitcroft
14751f65f947SAndy Whitcroft		} elsif ($cur =~ /^(:)/o) {
14761f65f947SAndy Whitcroft			print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
14771f65f947SAndy Whitcroft
14781f65f947SAndy Whitcroft			substr($var, length($res), 1, $av_pend_colon);
14791f65f947SAndy Whitcroft			if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
14801f65f947SAndy Whitcroft				$type = 'E';
14811f65f947SAndy Whitcroft			} else {
14821f65f947SAndy Whitcroft				$type = 'N';
14831f65f947SAndy Whitcroft			}
14841f65f947SAndy Whitcroft			$av_pend_colon = 'O';
14851f65f947SAndy Whitcroft
14868e761b04SAndy Whitcroft		} elsif ($cur =~ /^(\[)/o) {
148713214adfSAndy Whitcroft			print "CLOSE($1)\n" if ($dbg_values > 1);
14886c72ffaaSAndy Whitcroft			$type = 'N';
14896c72ffaaSAndy Whitcroft
14900d413866SAndy Whitcroft		} elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
149174048ed8SAndy Whitcroft			my $variant;
149274048ed8SAndy Whitcroft
149374048ed8SAndy Whitcroft			print "OPV($1)\n" if ($dbg_values > 1);
149474048ed8SAndy Whitcroft			if ($type eq 'V') {
149574048ed8SAndy Whitcroft				$variant = 'B';
149674048ed8SAndy Whitcroft			} else {
149774048ed8SAndy Whitcroft				$variant = 'U';
149874048ed8SAndy Whitcroft			}
149974048ed8SAndy Whitcroft
150074048ed8SAndy Whitcroft			substr($var, length($res), 1, $variant);
150174048ed8SAndy Whitcroft			$type = 'N';
150274048ed8SAndy Whitcroft
15036c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Operators)/o) {
1504c2fdda0dSAndy Whitcroft			print "OP($1)\n" if ($dbg_values > 1);
15056c72ffaaSAndy Whitcroft			if ($1 ne '++' && $1 ne '--') {
15066c72ffaaSAndy Whitcroft				$type = 'N';
15076c72ffaaSAndy Whitcroft			}
15086c72ffaaSAndy Whitcroft
15096c72ffaaSAndy Whitcroft		} elsif ($cur =~ /(^.)/o) {
1510c2fdda0dSAndy Whitcroft			print "C($1)\n" if ($dbg_values > 1);
15116c72ffaaSAndy Whitcroft		}
15126c72ffaaSAndy Whitcroft		if (defined $1) {
15136c72ffaaSAndy Whitcroft			$cur = substr($cur, length($1));
15146c72ffaaSAndy Whitcroft			$res .= $type x length($1);
15156c72ffaaSAndy Whitcroft		}
15166c72ffaaSAndy Whitcroft	}
15176c72ffaaSAndy Whitcroft
15181f65f947SAndy Whitcroft	return ($res, $var);
15196c72ffaaSAndy Whitcroft}
15206c72ffaaSAndy Whitcroft
15218905a67cSAndy Whitcroftsub possible {
152213214adfSAndy Whitcroft	my ($possible, $line) = @_;
15239a974fdbSAndy Whitcroft	my $notPermitted = qr{(?:
15240776e594SAndy Whitcroft		^(?:
15250776e594SAndy Whitcroft			$Modifier|
15260776e594SAndy Whitcroft			$Storage|
15270776e594SAndy Whitcroft			$Type|
15289a974fdbSAndy Whitcroft			DEFINE_\S+
15299a974fdbSAndy Whitcroft		)$|
15309a974fdbSAndy Whitcroft		^(?:
15310776e594SAndy Whitcroft			goto|
15320776e594SAndy Whitcroft			return|
15330776e594SAndy Whitcroft			case|
15340776e594SAndy Whitcroft			else|
15350776e594SAndy Whitcroft			asm|__asm__|
153689a88353SAndy Whitcroft			do|
153789a88353SAndy Whitcroft			\#|
153889a88353SAndy Whitcroft			\#\#|
15399a974fdbSAndy Whitcroft		)(?:\s|$)|
15400776e594SAndy Whitcroft		^(?:typedef|struct|enum)\b
15419a974fdbSAndy Whitcroft	    )}x;
15429a974fdbSAndy Whitcroft	warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
15439a974fdbSAndy Whitcroft	if ($possible !~ $notPermitted) {
1544c45dcabdSAndy Whitcroft		# Check for modifiers.
1545c45dcabdSAndy Whitcroft		$possible =~ s/\s*$Storage\s*//g;
1546c45dcabdSAndy Whitcroft		$possible =~ s/\s*$Sparse\s*//g;
1547c45dcabdSAndy Whitcroft		if ($possible =~ /^\s*$/) {
1548c45dcabdSAndy Whitcroft
1549c45dcabdSAndy Whitcroft		} elsif ($possible =~ /\s/) {
1550c45dcabdSAndy Whitcroft			$possible =~ s/\s*$Type\s*//g;
1551d2506586SAndy Whitcroft			for my $modifier (split(' ', $possible)) {
15529a974fdbSAndy Whitcroft				if ($modifier !~ $notPermitted) {
1553d2506586SAndy Whitcroft					warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1554d2506586SAndy Whitcroft					push(@modifierList, $modifier);
1555d2506586SAndy Whitcroft				}
15569a974fdbSAndy Whitcroft			}
1557c45dcabdSAndy Whitcroft
1558c45dcabdSAndy Whitcroft		} else {
155913214adfSAndy Whitcroft			warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
15608905a67cSAndy Whitcroft			push(@typeList, $possible);
1561c45dcabdSAndy Whitcroft		}
15628905a67cSAndy Whitcroft		build_types();
15630776e594SAndy Whitcroft	} else {
15640776e594SAndy Whitcroft		warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
15658905a67cSAndy Whitcroft	}
15668905a67cSAndy Whitcroft}
15678905a67cSAndy Whitcroft
15686c72ffaaSAndy Whitcroftmy $prefix = '';
15696c72ffaaSAndy Whitcroft
1570000d1cc1SJoe Perchessub show_type {
1571cbec18afSJoe Perches	my ($type) = @_;
157291bfe484SJoe Perches
1573cbec18afSJoe Perches	return defined $use_type{$type} if (scalar keys %use_type > 0);
1574cbec18afSJoe Perches
1575cbec18afSJoe Perches	return !defined $ignore_type{$type};
1576000d1cc1SJoe Perches}
1577000d1cc1SJoe Perches
1578f0a594c1SAndy Whitcroftsub report {
1579cbec18afSJoe Perches	my ($level, $type, $msg) = @_;
1580cbec18afSJoe Perches
1581cbec18afSJoe Perches	if (!show_type($type) ||
1582cbec18afSJoe Perches	    (defined $tst_only && $msg !~ /\Q$tst_only\E/)) {
1583773647a0SAndy Whitcroft		return 0;
1584773647a0SAndy Whitcroft	}
1585000d1cc1SJoe Perches	my $line;
1586000d1cc1SJoe Perches	if ($show_types) {
1587cbec18afSJoe Perches		$line = "$prefix$level:$type: $msg\n";
1588000d1cc1SJoe Perches	} else {
1589cbec18afSJoe Perches		$line = "$prefix$level: $msg\n";
1590000d1cc1SJoe Perches	}
15918905a67cSAndy Whitcroft	$line = (split('\n', $line))[0] . "\n" if ($terse);
15928905a67cSAndy Whitcroft
159313214adfSAndy Whitcroft	push(our @report, $line);
1594773647a0SAndy Whitcroft
1595773647a0SAndy Whitcroft	return 1;
1596f0a594c1SAndy Whitcroft}
1597cbec18afSJoe Perches
1598f0a594c1SAndy Whitcroftsub report_dump {
159913214adfSAndy Whitcroft	our @report;
1600f0a594c1SAndy Whitcroft}
1601000d1cc1SJoe Perches
1602d752fcc8SJoe Perchessub fixup_current_range {
1603d752fcc8SJoe Perches	my ($lineRef, $offset, $length) = @_;
1604d752fcc8SJoe Perches
1605d752fcc8SJoe Perches	if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) {
1606d752fcc8SJoe Perches		my $o = $1;
1607d752fcc8SJoe Perches		my $l = $2;
1608d752fcc8SJoe Perches		my $no = $o + $offset;
1609d752fcc8SJoe Perches		my $nl = $l + $length;
1610d752fcc8SJoe Perches		$$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/;
1611d752fcc8SJoe Perches	}
1612d752fcc8SJoe Perches}
1613d752fcc8SJoe Perches
1614d752fcc8SJoe Perchessub fix_inserted_deleted_lines {
1615d752fcc8SJoe Perches	my ($linesRef, $insertedRef, $deletedRef) = @_;
1616d752fcc8SJoe Perches
1617d752fcc8SJoe Perches	my $range_last_linenr = 0;
1618d752fcc8SJoe Perches	my $delta_offset = 0;
1619d752fcc8SJoe Perches
1620d752fcc8SJoe Perches	my $old_linenr = 0;
1621d752fcc8SJoe Perches	my $new_linenr = 0;
1622d752fcc8SJoe Perches
1623d752fcc8SJoe Perches	my $next_insert = 0;
1624d752fcc8SJoe Perches	my $next_delete = 0;
1625d752fcc8SJoe Perches
1626d752fcc8SJoe Perches	my @lines = ();
1627d752fcc8SJoe Perches
1628d752fcc8SJoe Perches	my $inserted = @{$insertedRef}[$next_insert++];
1629d752fcc8SJoe Perches	my $deleted = @{$deletedRef}[$next_delete++];
1630d752fcc8SJoe Perches
1631d752fcc8SJoe Perches	foreach my $old_line (@{$linesRef}) {
1632d752fcc8SJoe Perches		my $save_line = 1;
1633d752fcc8SJoe Perches		my $line = $old_line;	#don't modify the array
1634d752fcc8SJoe Perches		if ($line =~ /^(?:\+\+\+\|\-\-\-)\s+\S+/) {	#new filename
1635d752fcc8SJoe Perches			$delta_offset = 0;
1636d752fcc8SJoe Perches		} elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) {	#new hunk
1637d752fcc8SJoe Perches			$range_last_linenr = $new_linenr;
1638d752fcc8SJoe Perches			fixup_current_range(\$line, $delta_offset, 0);
1639d752fcc8SJoe Perches		}
1640d752fcc8SJoe Perches
1641d752fcc8SJoe Perches		while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) {
1642d752fcc8SJoe Perches			$deleted = @{$deletedRef}[$next_delete++];
1643d752fcc8SJoe Perches			$save_line = 0;
1644d752fcc8SJoe Perches			fixup_current_range(\$lines[$range_last_linenr], $delta_offset--, -1);
1645d752fcc8SJoe Perches		}
1646d752fcc8SJoe Perches
1647d752fcc8SJoe Perches		while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) {
1648d752fcc8SJoe Perches			push(@lines, ${$inserted}{'LINE'});
1649d752fcc8SJoe Perches			$inserted = @{$insertedRef}[$next_insert++];
1650d752fcc8SJoe Perches			$new_linenr++;
1651d752fcc8SJoe Perches			fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1);
1652d752fcc8SJoe Perches		}
1653d752fcc8SJoe Perches
1654d752fcc8SJoe Perches		if ($save_line) {
1655d752fcc8SJoe Perches			push(@lines, $line);
1656d752fcc8SJoe Perches			$new_linenr++;
1657d752fcc8SJoe Perches		}
1658d752fcc8SJoe Perches
1659d752fcc8SJoe Perches		$old_linenr++;
1660d752fcc8SJoe Perches	}
1661d752fcc8SJoe Perches
1662d752fcc8SJoe Perches	return @lines;
1663d752fcc8SJoe Perches}
1664d752fcc8SJoe Perches
1665f2d7e4d4SJoe Perchessub fix_insert_line {
1666f2d7e4d4SJoe Perches	my ($linenr, $line) = @_;
1667f2d7e4d4SJoe Perches
1668f2d7e4d4SJoe Perches	my $inserted = {
1669f2d7e4d4SJoe Perches		LINENR => $linenr,
1670f2d7e4d4SJoe Perches		LINE => $line,
1671f2d7e4d4SJoe Perches	};
1672f2d7e4d4SJoe Perches	push(@fixed_inserted, $inserted);
1673f2d7e4d4SJoe Perches}
1674f2d7e4d4SJoe Perches
1675f2d7e4d4SJoe Perchessub fix_delete_line {
1676f2d7e4d4SJoe Perches	my ($linenr, $line) = @_;
1677f2d7e4d4SJoe Perches
1678f2d7e4d4SJoe Perches	my $deleted = {
1679f2d7e4d4SJoe Perches		LINENR => $linenr,
1680f2d7e4d4SJoe Perches		LINE => $line,
1681f2d7e4d4SJoe Perches	};
1682f2d7e4d4SJoe Perches
1683f2d7e4d4SJoe Perches	push(@fixed_deleted, $deleted);
1684f2d7e4d4SJoe Perches}
1685f2d7e4d4SJoe Perches
1686de7d4f0eSAndy Whitcroftsub ERROR {
1687cbec18afSJoe Perches	my ($type, $msg) = @_;
1688cbec18afSJoe Perches
1689cbec18afSJoe Perches	if (report("ERROR", $type, $msg)) {
1690de7d4f0eSAndy Whitcroft		our $clean = 0;
16916c72ffaaSAndy Whitcroft		our $cnt_error++;
16923705ce5bSJoe Perches		return 1;
1693de7d4f0eSAndy Whitcroft	}
16943705ce5bSJoe Perches	return 0;
1695773647a0SAndy Whitcroft}
1696de7d4f0eSAndy Whitcroftsub WARN {
1697cbec18afSJoe Perches	my ($type, $msg) = @_;
1698cbec18afSJoe Perches
1699cbec18afSJoe Perches	if (report("WARNING", $type, $msg)) {
1700de7d4f0eSAndy Whitcroft		our $clean = 0;
17016c72ffaaSAndy Whitcroft		our $cnt_warn++;
17023705ce5bSJoe Perches		return 1;
1703de7d4f0eSAndy Whitcroft	}
17043705ce5bSJoe Perches	return 0;
1705773647a0SAndy Whitcroft}
1706de7d4f0eSAndy Whitcroftsub CHK {
1707cbec18afSJoe Perches	my ($type, $msg) = @_;
1708cbec18afSJoe Perches
1709cbec18afSJoe Perches	if ($check && report("CHECK", $type, $msg)) {
1710de7d4f0eSAndy Whitcroft		our $clean = 0;
17116c72ffaaSAndy Whitcroft		our $cnt_chk++;
17123705ce5bSJoe Perches		return 1;
17136c72ffaaSAndy Whitcroft	}
17143705ce5bSJoe Perches	return 0;
1715de7d4f0eSAndy Whitcroft}
1716de7d4f0eSAndy Whitcroft
17176ecd9674SAndy Whitcroftsub check_absolute_file {
17186ecd9674SAndy Whitcroft	my ($absolute, $herecurr) = @_;
17196ecd9674SAndy Whitcroft	my $file = $absolute;
17206ecd9674SAndy Whitcroft
17216ecd9674SAndy Whitcroft	##print "absolute<$absolute>\n";
17226ecd9674SAndy Whitcroft
17236ecd9674SAndy Whitcroft	# See if any suffix of this path is a path within the tree.
17246ecd9674SAndy Whitcroft	while ($file =~ s@^[^/]*/@@) {
17256ecd9674SAndy Whitcroft		if (-f "$root/$file") {
17266ecd9674SAndy Whitcroft			##print "file<$file>\n";
17276ecd9674SAndy Whitcroft			last;
17286ecd9674SAndy Whitcroft		}
17296ecd9674SAndy Whitcroft	}
17306ecd9674SAndy Whitcroft	if (! -f _)  {
17316ecd9674SAndy Whitcroft		return 0;
17326ecd9674SAndy Whitcroft	}
17336ecd9674SAndy Whitcroft
17346ecd9674SAndy Whitcroft	# It is, so see if the prefix is acceptable.
17356ecd9674SAndy Whitcroft	my $prefix = $absolute;
17366ecd9674SAndy Whitcroft	substr($prefix, -length($file)) = '';
17376ecd9674SAndy Whitcroft
17386ecd9674SAndy Whitcroft	##print "prefix<$prefix>\n";
17396ecd9674SAndy Whitcroft	if ($prefix ne ".../") {
1740000d1cc1SJoe Perches		WARN("USE_RELATIVE_PATH",
1741000d1cc1SJoe Perches		     "use relative pathname instead of absolute in changelog text\n" . $herecurr);
17426ecd9674SAndy Whitcroft	}
17436ecd9674SAndy Whitcroft}
17446ecd9674SAndy Whitcroft
17453705ce5bSJoe Perchessub trim {
17463705ce5bSJoe Perches	my ($string) = @_;
17473705ce5bSJoe Perches
1748b34c648bSJoe Perches	$string =~ s/^\s+|\s+$//g;
1749b34c648bSJoe Perches
1750b34c648bSJoe Perches	return $string;
1751b34c648bSJoe Perches}
1752b34c648bSJoe Perches
1753b34c648bSJoe Perchessub ltrim {
1754b34c648bSJoe Perches	my ($string) = @_;
1755b34c648bSJoe Perches
1756b34c648bSJoe Perches	$string =~ s/^\s+//;
1757b34c648bSJoe Perches
1758b34c648bSJoe Perches	return $string;
1759b34c648bSJoe Perches}
1760b34c648bSJoe Perches
1761b34c648bSJoe Perchessub rtrim {
1762b34c648bSJoe Perches	my ($string) = @_;
1763b34c648bSJoe Perches
1764b34c648bSJoe Perches	$string =~ s/\s+$//;
17653705ce5bSJoe Perches
17663705ce5bSJoe Perches	return $string;
17673705ce5bSJoe Perches}
17683705ce5bSJoe Perches
176952ea8506SJoe Perchessub string_find_replace {
177052ea8506SJoe Perches	my ($string, $find, $replace) = @_;
177152ea8506SJoe Perches
177252ea8506SJoe Perches	$string =~ s/$find/$replace/g;
177352ea8506SJoe Perches
177452ea8506SJoe Perches	return $string;
177552ea8506SJoe Perches}
177652ea8506SJoe Perches
17773705ce5bSJoe Perchessub tabify {
17783705ce5bSJoe Perches	my ($leading) = @_;
17793705ce5bSJoe Perches
17803705ce5bSJoe Perches	my $source_indent = 8;
17813705ce5bSJoe Perches	my $max_spaces_before_tab = $source_indent - 1;
17823705ce5bSJoe Perches	my $spaces_to_tab = " " x $source_indent;
17833705ce5bSJoe Perches
17843705ce5bSJoe Perches	#convert leading spaces to tabs
17853705ce5bSJoe Perches	1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
17863705ce5bSJoe Perches	#Remove spaces before a tab
17873705ce5bSJoe Perches	1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
17883705ce5bSJoe Perches
17893705ce5bSJoe Perches	return "$leading";
17903705ce5bSJoe Perches}
17913705ce5bSJoe Perches
1792d1fe9c09SJoe Perchessub pos_last_openparen {
1793d1fe9c09SJoe Perches	my ($line) = @_;
1794d1fe9c09SJoe Perches
1795d1fe9c09SJoe Perches	my $pos = 0;
1796d1fe9c09SJoe Perches
1797d1fe9c09SJoe Perches	my $opens = $line =~ tr/\(/\(/;
1798d1fe9c09SJoe Perches	my $closes = $line =~ tr/\)/\)/;
1799d1fe9c09SJoe Perches
1800d1fe9c09SJoe Perches	my $last_openparen = 0;
1801d1fe9c09SJoe Perches
1802d1fe9c09SJoe Perches	if (($opens == 0) || ($closes >= $opens)) {
1803d1fe9c09SJoe Perches		return -1;
1804d1fe9c09SJoe Perches	}
1805d1fe9c09SJoe Perches
1806d1fe9c09SJoe Perches	my $len = length($line);
1807d1fe9c09SJoe Perches
1808d1fe9c09SJoe Perches	for ($pos = 0; $pos < $len; $pos++) {
1809d1fe9c09SJoe Perches		my $string = substr($line, $pos);
1810d1fe9c09SJoe Perches		if ($string =~ /^($FuncArg|$balanced_parens)/) {
1811d1fe9c09SJoe Perches			$pos += length($1) - 1;
1812d1fe9c09SJoe Perches		} elsif (substr($line, $pos, 1) eq '(') {
1813d1fe9c09SJoe Perches			$last_openparen = $pos;
1814d1fe9c09SJoe Perches		} elsif (index($string, '(') == -1) {
1815d1fe9c09SJoe Perches			last;
1816d1fe9c09SJoe Perches		}
1817d1fe9c09SJoe Perches	}
1818d1fe9c09SJoe Perches
181991cb5195SJoe Perches	return length(expand_tabs(substr($line, 0, $last_openparen))) + 1;
1820d1fe9c09SJoe Perches}
1821d1fe9c09SJoe Perches
18220a920b5bSAndy Whitcroftsub process {
18230a920b5bSAndy Whitcroft	my $filename = shift;
18240a920b5bSAndy Whitcroft
18250a920b5bSAndy Whitcroft	my $linenr=0;
18260a920b5bSAndy Whitcroft	my $prevline="";
1827c2fdda0dSAndy Whitcroft	my $prevrawline="";
18280a920b5bSAndy Whitcroft	my $stashline="";
1829c2fdda0dSAndy Whitcroft	my $stashrawline="";
18300a920b5bSAndy Whitcroft
18314a0df2efSAndy Whitcroft	my $length;
18320a920b5bSAndy Whitcroft	my $indent;
18330a920b5bSAndy Whitcroft	my $previndent=0;
18340a920b5bSAndy Whitcroft	my $stashindent=0;
18350a920b5bSAndy Whitcroft
1836de7d4f0eSAndy Whitcroft	our $clean = 1;
18370a920b5bSAndy Whitcroft	my $signoff = 0;
18380a920b5bSAndy Whitcroft	my $is_patch = 0;
18390a920b5bSAndy Whitcroft
184029ee1b0cSJoe Perches	my $in_header_lines = $file ? 0 : 1;
184115662b3eSJoe Perches	my $in_commit_log = 0;		#Scanning lines before patch
184213f1937eSJoe Perches	my $reported_maintainer_file = 0;
1843fa64205dSPasi Savanainen	my $non_utf8_charset = 0;
1844fa64205dSPasi Savanainen
1845365dd4eaSJoe Perches	my $last_blank_line = 0;
1846365dd4eaSJoe Perches
184713214adfSAndy Whitcroft	our @report = ();
18486c72ffaaSAndy Whitcroft	our $cnt_lines = 0;
18496c72ffaaSAndy Whitcroft	our $cnt_error = 0;
18506c72ffaaSAndy Whitcroft	our $cnt_warn = 0;
18516c72ffaaSAndy Whitcroft	our $cnt_chk = 0;
18526c72ffaaSAndy Whitcroft
18530a920b5bSAndy Whitcroft	# Trace the real file/line as we go.
18540a920b5bSAndy Whitcroft	my $realfile = '';
18550a920b5bSAndy Whitcroft	my $realline = 0;
18560a920b5bSAndy Whitcroft	my $realcnt = 0;
18570a920b5bSAndy Whitcroft	my $here = '';
18580a920b5bSAndy Whitcroft	my $in_comment = 0;
1859c2fdda0dSAndy Whitcroft	my $comment_edge = 0;
18600a920b5bSAndy Whitcroft	my $first_line = 0;
18611e855726SWolfram Sang	my $p1_prefix = '';
18620a920b5bSAndy Whitcroft
186313214adfSAndy Whitcroft	my $prev_values = 'E';
186413214adfSAndy Whitcroft
186513214adfSAndy Whitcroft	# suppression flags
1866773647a0SAndy Whitcroft	my %suppress_ifbraces;
1867170d3a22SAndy Whitcroft	my %suppress_whiletrailers;
18682b474a1aSAndy Whitcroft	my %suppress_export;
18693e469cdcSAndy Whitcroft	my $suppress_statement = 0;
1870653d4876SAndy Whitcroft
18717e51f197SJoe Perches	my %signatures = ();
1872323c1260SJoe Perches
1873c2fdda0dSAndy Whitcroft	# Pre-scan the patch sanitizing the lines.
1874de7d4f0eSAndy Whitcroft	# Pre-scan the patch looking for any __setup documentation.
1875c2fdda0dSAndy Whitcroft	#
1876de7d4f0eSAndy Whitcroft	my @setup_docs = ();
1877de7d4f0eSAndy Whitcroft	my $setup_docs = 0;
1878773647a0SAndy Whitcroft
1879d8b07710SJoe Perches	my $camelcase_file_seeded = 0;
1880d8b07710SJoe Perches
1881773647a0SAndy Whitcroft	sanitise_line_reset();
1882c2fdda0dSAndy Whitcroft	my $line;
1883c2fdda0dSAndy Whitcroft	foreach my $rawline (@rawlines) {
1884773647a0SAndy Whitcroft		$linenr++;
1885773647a0SAndy Whitcroft		$line = $rawline;
1886c2fdda0dSAndy Whitcroft
18873705ce5bSJoe Perches		push(@fixed, $rawline) if ($fix);
18883705ce5bSJoe Perches
1889773647a0SAndy Whitcroft		if ($rawline=~/^\+\+\+\s+(\S+)/) {
1890de7d4f0eSAndy Whitcroft			$setup_docs = 0;
1891de7d4f0eSAndy Whitcroft			if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
1892de7d4f0eSAndy Whitcroft				$setup_docs = 1;
1893de7d4f0eSAndy Whitcroft			}
1894773647a0SAndy Whitcroft			#next;
1895de7d4f0eSAndy Whitcroft		}
1896773647a0SAndy Whitcroft		if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1897773647a0SAndy Whitcroft			$realline=$1-1;
1898773647a0SAndy Whitcroft			if (defined $2) {
1899773647a0SAndy Whitcroft				$realcnt=$3+1;
1900773647a0SAndy Whitcroft			} else {
1901773647a0SAndy Whitcroft				$realcnt=1+1;
1902773647a0SAndy Whitcroft			}
1903c45dcabdSAndy Whitcroft			$in_comment = 0;
1904773647a0SAndy Whitcroft
1905773647a0SAndy Whitcroft			# Guestimate if this is a continuing comment.  Run
1906773647a0SAndy Whitcroft			# the context looking for a comment "edge".  If this
1907773647a0SAndy Whitcroft			# edge is a close comment then we must be in a comment
1908773647a0SAndy Whitcroft			# at context start.
1909773647a0SAndy Whitcroft			my $edge;
191001fa9147SAndy Whitcroft			my $cnt = $realcnt;
191101fa9147SAndy Whitcroft			for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
191201fa9147SAndy Whitcroft				next if (defined $rawlines[$ln - 1] &&
191301fa9147SAndy Whitcroft					 $rawlines[$ln - 1] =~ /^-/);
191401fa9147SAndy Whitcroft				$cnt--;
191501fa9147SAndy Whitcroft				#print "RAW<$rawlines[$ln - 1]>\n";
1916721c1cb6SAndy Whitcroft				last if (!defined $rawlines[$ln - 1]);
1917fae17daeSAndy Whitcroft				if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
1918fae17daeSAndy Whitcroft				    $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
1919fae17daeSAndy Whitcroft					($edge) = $1;
1920fae17daeSAndy Whitcroft					last;
1921fae17daeSAndy Whitcroft				}
1922773647a0SAndy Whitcroft			}
1923773647a0SAndy Whitcroft			if (defined $edge && $edge eq '*/') {
1924773647a0SAndy Whitcroft				$in_comment = 1;
1925773647a0SAndy Whitcroft			}
1926773647a0SAndy Whitcroft
1927773647a0SAndy Whitcroft			# Guestimate if this is a continuing comment.  If this
1928773647a0SAndy Whitcroft			# is the start of a diff block and this line starts
1929773647a0SAndy Whitcroft			# ' *' then it is very likely a comment.
1930773647a0SAndy Whitcroft			if (!defined $edge &&
193183242e0cSAndy Whitcroft			    $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
1932773647a0SAndy Whitcroft			{
1933773647a0SAndy Whitcroft				$in_comment = 1;
1934773647a0SAndy Whitcroft			}
1935773647a0SAndy Whitcroft
1936773647a0SAndy Whitcroft			##print "COMMENT:$in_comment edge<$edge> $rawline\n";
1937773647a0SAndy Whitcroft			sanitise_line_reset($in_comment);
1938773647a0SAndy Whitcroft
1939171ae1a4SAndy Whitcroft		} elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
1940773647a0SAndy Whitcroft			# Standardise the strings and chars within the input to
1941171ae1a4SAndy Whitcroft			# simplify matching -- only bother with positive lines.
1942773647a0SAndy Whitcroft			$line = sanitise_line($rawline);
1943773647a0SAndy Whitcroft		}
1944773647a0SAndy Whitcroft		push(@lines, $line);
1945773647a0SAndy Whitcroft
1946773647a0SAndy Whitcroft		if ($realcnt > 1) {
1947773647a0SAndy Whitcroft			$realcnt-- if ($line =~ /^(?:\+| |$)/);
1948773647a0SAndy Whitcroft		} else {
1949773647a0SAndy Whitcroft			$realcnt = 0;
1950773647a0SAndy Whitcroft		}
1951773647a0SAndy Whitcroft
1952773647a0SAndy Whitcroft		#print "==>$rawline\n";
1953773647a0SAndy Whitcroft		#print "-->$line\n";
1954de7d4f0eSAndy Whitcroft
1955de7d4f0eSAndy Whitcroft		if ($setup_docs && $line =~ /^\+/) {
1956de7d4f0eSAndy Whitcroft			push(@setup_docs, $line);
1957de7d4f0eSAndy Whitcroft		}
1958de7d4f0eSAndy Whitcroft	}
1959de7d4f0eSAndy Whitcroft
19606c72ffaaSAndy Whitcroft	$prefix = '';
19616c72ffaaSAndy Whitcroft
1962773647a0SAndy Whitcroft	$realcnt = 0;
1963773647a0SAndy Whitcroft	$linenr = 0;
1964194f66fcSJoe Perches	$fixlinenr = -1;
19650a920b5bSAndy Whitcroft	foreach my $line (@lines) {
19660a920b5bSAndy Whitcroft		$linenr++;
1967194f66fcSJoe Perches		$fixlinenr++;
19681b5539b1SJoe Perches		my $sline = $line;	#copy of $line
19691b5539b1SJoe Perches		$sline =~ s/$;/ /g;	#with comments as spaces
19700a920b5bSAndy Whitcroft
1971c2fdda0dSAndy Whitcroft		my $rawline = $rawlines[$linenr - 1];
19726c72ffaaSAndy Whitcroft
19730a920b5bSAndy Whitcroft#extract the line range in the file after the patch is applied
19746c72ffaaSAndy Whitcroft		if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
19750a920b5bSAndy Whitcroft			$is_patch = 1;
19764a0df2efSAndy Whitcroft			$first_line = $linenr + 1;
19770a920b5bSAndy Whitcroft			$realline=$1-1;
19780a920b5bSAndy Whitcroft			if (defined $2) {
19790a920b5bSAndy Whitcroft				$realcnt=$3+1;
19800a920b5bSAndy Whitcroft			} else {
19810a920b5bSAndy Whitcroft				$realcnt=1+1;
19820a920b5bSAndy Whitcroft			}
1983c2fdda0dSAndy Whitcroft			annotate_reset();
198413214adfSAndy Whitcroft			$prev_values = 'E';
198513214adfSAndy Whitcroft
1986773647a0SAndy Whitcroft			%suppress_ifbraces = ();
1987170d3a22SAndy Whitcroft			%suppress_whiletrailers = ();
19882b474a1aSAndy Whitcroft			%suppress_export = ();
19893e469cdcSAndy Whitcroft			$suppress_statement = 0;
19900a920b5bSAndy Whitcroft			next;
19910a920b5bSAndy Whitcroft
19924a0df2efSAndy Whitcroft# track the line number as we move through the hunk, note that
19934a0df2efSAndy Whitcroft# new versions of GNU diff omit the leading space on completely
19944a0df2efSAndy Whitcroft# blank context lines so we need to count that too.
1995773647a0SAndy Whitcroft		} elsif ($line =~ /^( |\+|$)/) {
19960a920b5bSAndy Whitcroft			$realline++;
1997d8aaf121SAndy Whitcroft			$realcnt-- if ($realcnt != 0);
19980a920b5bSAndy Whitcroft
19994a0df2efSAndy Whitcroft			# Measure the line length and indent.
2000c2fdda0dSAndy Whitcroft			($length, $indent) = line_stats($rawline);
20010a920b5bSAndy Whitcroft
20020a920b5bSAndy Whitcroft			# Track the previous line.
20030a920b5bSAndy Whitcroft			($prevline, $stashline) = ($stashline, $line);
20040a920b5bSAndy Whitcroft			($previndent, $stashindent) = ($stashindent, $indent);
2005c2fdda0dSAndy Whitcroft			($prevrawline, $stashrawline) = ($stashrawline, $rawline);
2006c2fdda0dSAndy Whitcroft
2007773647a0SAndy Whitcroft			#warn "line<$line>\n";
20086c72ffaaSAndy Whitcroft
2009d8aaf121SAndy Whitcroft		} elsif ($realcnt == 1) {
2010d8aaf121SAndy Whitcroft			$realcnt--;
20110a920b5bSAndy Whitcroft		}
20120a920b5bSAndy Whitcroft
2013cc77cdcaSAndy Whitcroft		my $hunk_line = ($realcnt != 0);
2014cc77cdcaSAndy Whitcroft
20150a920b5bSAndy Whitcroft#make up the handle for any error we report on this line
2016773647a0SAndy Whitcroft		$prefix = "$filename:$realline: " if ($emacs && $file);
2017773647a0SAndy Whitcroft		$prefix = "$filename:$linenr: " if ($emacs && !$file);
2018773647a0SAndy Whitcroft
20196c72ffaaSAndy Whitcroft		$here = "#$linenr: " if (!$file);
20206c72ffaaSAndy Whitcroft		$here = "#$realline: " if ($file);
2021773647a0SAndy Whitcroft
20222ac73b4fSJoe Perches		my $found_file = 0;
2023773647a0SAndy Whitcroft		# extract the filename as it passes
20243bf9a009SRabin Vincent		if ($line =~ /^diff --git.*?(\S+)$/) {
20253bf9a009SRabin Vincent			$realfile = $1;
20262b7ab453SJoe Perches			$realfile =~ s@^([^/]*)/@@ if (!$file);
2027270c49a0SJoe Perches			$in_commit_log = 0;
20282ac73b4fSJoe Perches			$found_file = 1;
20293bf9a009SRabin Vincent		} elsif ($line =~ /^\+\+\+\s+(\S+)/) {
2030773647a0SAndy Whitcroft			$realfile = $1;
20312b7ab453SJoe Perches			$realfile =~ s@^([^/]*)/@@ if (!$file);
2032270c49a0SJoe Perches			$in_commit_log = 0;
20331e855726SWolfram Sang
20341e855726SWolfram Sang			$p1_prefix = $1;
2035e2f7aa4bSAndy Whitcroft			if (!$file && $tree && $p1_prefix ne '' &&
2036e2f7aa4bSAndy Whitcroft			    -e "$root/$p1_prefix") {
2037000d1cc1SJoe Perches				WARN("PATCH_PREFIX",
2038000d1cc1SJoe Perches				     "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
20391e855726SWolfram Sang			}
2040773647a0SAndy Whitcroft
2041c1ab3326SAndy Whitcroft			if ($realfile =~ m@^include/asm/@) {
2042000d1cc1SJoe Perches				ERROR("MODIFIED_INCLUDE_ASM",
2043000d1cc1SJoe Perches				      "do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
2044773647a0SAndy Whitcroft			}
20452ac73b4fSJoe Perches			$found_file = 1;
20462ac73b4fSJoe Perches		}
20472ac73b4fSJoe Perches
20482ac73b4fSJoe Perches		if ($found_file) {
20492ac73b4fSJoe Perches			if ($realfile =~ m@^(drivers/net/|net/)@) {
20502ac73b4fSJoe Perches				$check = 1;
20512ac73b4fSJoe Perches			} else {
20522ac73b4fSJoe Perches				$check = $check_orig;
20532ac73b4fSJoe Perches			}
2054773647a0SAndy Whitcroft			next;
2055773647a0SAndy Whitcroft		}
2056773647a0SAndy Whitcroft
2057389834b6SRandy Dunlap		$here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
20580a920b5bSAndy Whitcroft
2059c2fdda0dSAndy Whitcroft		my $hereline = "$here\n$rawline\n";
2060c2fdda0dSAndy Whitcroft		my $herecurr = "$here\n$rawline\n";
2061c2fdda0dSAndy Whitcroft		my $hereprev = "$here\n$prevrawline\n$rawline\n";
20620a920b5bSAndy Whitcroft
20636c72ffaaSAndy Whitcroft		$cnt_lines++ if ($realcnt != 0);
20646c72ffaaSAndy Whitcroft
20653bf9a009SRabin Vincent# Check for incorrect file permissions
20663bf9a009SRabin Vincent		if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
20673bf9a009SRabin Vincent			my $permhere = $here . "FILE: $realfile\n";
206804db4d25SJoe Perches			if ($realfile !~ m@scripts/@ &&
206904db4d25SJoe Perches			    $realfile !~ /\.(py|pl|awk|sh)$/) {
2070000d1cc1SJoe Perches				ERROR("EXECUTE_PERMISSIONS",
2071000d1cc1SJoe Perches				      "do not set execute permissions for source files\n" . $permhere);
20723bf9a009SRabin Vincent			}
20733bf9a009SRabin Vincent		}
20743bf9a009SRabin Vincent
207520112475SJoe Perches# Check the patch for a signoff:
2076d8aaf121SAndy Whitcroft		if ($line =~ /^\s*signed-off-by:/i) {
20774a0df2efSAndy Whitcroft			$signoff++;
207815662b3eSJoe Perches			$in_commit_log = 0;
20790a920b5bSAndy Whitcroft		}
208020112475SJoe Perches
208120112475SJoe Perches# Check signature styles
2082270c49a0SJoe Perches		if (!$in_header_lines &&
2083ce0338dfSJoe Perches		    $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
208420112475SJoe Perches			my $space_before = $1;
208520112475SJoe Perches			my $sign_off = $2;
208620112475SJoe Perches			my $space_after = $3;
208720112475SJoe Perches			my $email = $4;
208820112475SJoe Perches			my $ucfirst_sign_off = ucfirst(lc($sign_off));
208920112475SJoe Perches
2090ce0338dfSJoe Perches			if ($sign_off !~ /$signature_tags/) {
2091ce0338dfSJoe Perches				WARN("BAD_SIGN_OFF",
2092ce0338dfSJoe Perches				     "Non-standard signature: $sign_off\n" . $herecurr);
2093ce0338dfSJoe Perches			}
209420112475SJoe Perches			if (defined $space_before && $space_before ne "") {
20953705ce5bSJoe Perches				if (WARN("BAD_SIGN_OFF",
20963705ce5bSJoe Perches					 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
20973705ce5bSJoe Perches				    $fix) {
2098194f66fcSJoe Perches					$fixed[$fixlinenr] =
20993705ce5bSJoe Perches					    "$ucfirst_sign_off $email";
21003705ce5bSJoe Perches				}
210120112475SJoe Perches			}
210220112475SJoe Perches			if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
21033705ce5bSJoe Perches				if (WARN("BAD_SIGN_OFF",
21043705ce5bSJoe Perches					 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
21053705ce5bSJoe Perches				    $fix) {
2106194f66fcSJoe Perches					$fixed[$fixlinenr] =
21073705ce5bSJoe Perches					    "$ucfirst_sign_off $email";
21083705ce5bSJoe Perches				}
21093705ce5bSJoe Perches
211020112475SJoe Perches			}
211120112475SJoe Perches			if (!defined $space_after || $space_after ne " ") {
21123705ce5bSJoe Perches				if (WARN("BAD_SIGN_OFF",
21133705ce5bSJoe Perches					 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
21143705ce5bSJoe Perches				    $fix) {
2115194f66fcSJoe Perches					$fixed[$fixlinenr] =
21163705ce5bSJoe Perches					    "$ucfirst_sign_off $email";
21173705ce5bSJoe Perches				}
211820112475SJoe Perches			}
211920112475SJoe Perches
212020112475SJoe Perches			my ($email_name, $email_address, $comment) = parse_email($email);
212120112475SJoe Perches			my $suggested_email = format_email(($email_name, $email_address));
212220112475SJoe Perches			if ($suggested_email eq "") {
2123000d1cc1SJoe Perches				ERROR("BAD_SIGN_OFF",
2124000d1cc1SJoe Perches				      "Unrecognized email address: '$email'\n" . $herecurr);
212520112475SJoe Perches			} else {
212620112475SJoe Perches				my $dequoted = $suggested_email;
212720112475SJoe Perches				$dequoted =~ s/^"//;
212820112475SJoe Perches				$dequoted =~ s/" </ </;
212920112475SJoe Perches				# Don't force email to have quotes
213020112475SJoe Perches				# Allow just an angle bracketed address
213120112475SJoe Perches				if ("$dequoted$comment" ne $email &&
213220112475SJoe Perches				    "<$email_address>$comment" ne $email &&
213320112475SJoe Perches				    "$suggested_email$comment" ne $email) {
2134000d1cc1SJoe Perches					WARN("BAD_SIGN_OFF",
2135000d1cc1SJoe Perches					     "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
213620112475SJoe Perches				}
21370a920b5bSAndy Whitcroft			}
21387e51f197SJoe Perches
21397e51f197SJoe Perches# Check for duplicate signatures
21407e51f197SJoe Perches			my $sig_nospace = $line;
21417e51f197SJoe Perches			$sig_nospace =~ s/\s//g;
21427e51f197SJoe Perches			$sig_nospace = lc($sig_nospace);
21437e51f197SJoe Perches			if (defined $signatures{$sig_nospace}) {
21447e51f197SJoe Perches				WARN("BAD_SIGN_OFF",
21457e51f197SJoe Perches				     "Duplicate signature\n" . $herecurr);
21467e51f197SJoe Perches			} else {
21477e51f197SJoe Perches				$signatures{$sig_nospace} = 1;
21487e51f197SJoe Perches			}
21490a920b5bSAndy Whitcroft		}
21500a920b5bSAndy Whitcroft
21519b3189ebSJoe Perches# Check for old stable address
21529b3189ebSJoe Perches		if ($line =~ /^\s*cc:\s*.*<?\bstable\@kernel\.org\b>?.*$/i) {
21539b3189ebSJoe Perches			ERROR("STABLE_ADDRESS",
21549b3189ebSJoe Perches			      "The 'stable' address should be 'stable\@vger.kernel.org'\n" . $herecurr);
21559b3189ebSJoe Perches		}
21569b3189ebSJoe Perches
21577ebd05efSChristopher Covington# Check for unwanted Gerrit info
21587ebd05efSChristopher Covington		if ($in_commit_log && $line =~ /^\s*change-id:/i) {
21597ebd05efSChristopher Covington			ERROR("GERRIT_CHANGE_ID",
21607ebd05efSChristopher Covington			      "Remove Gerrit Change-Id's before submitting upstream.\n" . $herecurr);
21617ebd05efSChristopher Covington		}
21627ebd05efSChristopher Covington
2163d311cd44SJoe Perches# Check for improperly formed commit descriptions
2164d311cd44SJoe Perches		if ($in_commit_log &&
2165d311cd44SJoe Perches		    $line =~ /\bcommit\s+[0-9a-f]{5,}/i &&
216666881735SJoe Perches		    !($line =~ /\b[Cc]ommit [0-9a-f]{12,40} \("/ ||
216766881735SJoe Perches		      ($line =~ /\b[Cc]ommit [0-9a-f]{12,40}\s*$/ &&
216866881735SJoe Perches		       defined $rawlines[$linenr] &&
216966881735SJoe Perches		       $rawlines[$linenr] =~ /^\s*\("/))) {
2170d311cd44SJoe Perches			$line =~ /\b(c)ommit\s+([0-9a-f]{5,})/i;
2171d311cd44SJoe Perches			my $init_char = $1;
2172d311cd44SJoe Perches			my $orig_commit = lc($2);
2173d311cd44SJoe Perches			my $id = '01234567890ab';
2174d311cd44SJoe Perches			my $desc = 'commit description';
2175d311cd44SJoe Perches		        ($id, $desc) = git_commit_info($orig_commit, $id, $desc);
2176d311cd44SJoe Perches			ERROR("GIT_COMMIT_ID",
21773f6316b4SJoe Perches			      "Please use 12 or more chars for the git commit ID like: '${init_char}ommit $id (\"$desc\")'\n" . $herecurr);
2178d311cd44SJoe Perches		}
2179d311cd44SJoe Perches
218013f1937eSJoe Perches# Check for added, moved or deleted files
218113f1937eSJoe Perches		if (!$reported_maintainer_file && !$in_commit_log &&
218213f1937eSJoe Perches		    ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ ||
218313f1937eSJoe Perches		     $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ ||
218413f1937eSJoe Perches		     ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ &&
218513f1937eSJoe Perches		      (defined($1) || defined($2))))) {
218613f1937eSJoe Perches			$reported_maintainer_file = 1;
218713f1937eSJoe Perches			WARN("FILE_PATH_CHANGES",
218813f1937eSJoe Perches			     "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr);
218913f1937eSJoe Perches		}
219013f1937eSJoe Perches
219100df344fSAndy Whitcroft# Check for wrappage within a valid hunk of the file
21928905a67cSAndy Whitcroft		if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
2193000d1cc1SJoe Perches			ERROR("CORRUPTED_PATCH",
2194000d1cc1SJoe Perches			      "patch seems to be corrupt (line wrapped?)\n" .
21956c72ffaaSAndy Whitcroft				$herecurr) if (!$emitted_corrupt++);
2196de7d4f0eSAndy Whitcroft		}
2197de7d4f0eSAndy Whitcroft
21986ecd9674SAndy Whitcroft# Check for absolute kernel paths.
21996ecd9674SAndy Whitcroft		if ($tree) {
22006ecd9674SAndy Whitcroft			while ($line =~ m{(?:^|\s)(/\S*)}g) {
22016ecd9674SAndy Whitcroft				my $file = $1;
22026ecd9674SAndy Whitcroft
22036ecd9674SAndy Whitcroft				if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
22046ecd9674SAndy Whitcroft				    check_absolute_file($1, $herecurr)) {
22056ecd9674SAndy Whitcroft					#
22066ecd9674SAndy Whitcroft				} else {
22076ecd9674SAndy Whitcroft					check_absolute_file($file, $herecurr);
22086ecd9674SAndy Whitcroft				}
22096ecd9674SAndy Whitcroft			}
22106ecd9674SAndy Whitcroft		}
22116ecd9674SAndy Whitcroft
2212de7d4f0eSAndy Whitcroft# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
2213de7d4f0eSAndy Whitcroft		if (($realfile =~ /^$/ || $line =~ /^\+/) &&
2214171ae1a4SAndy Whitcroft		    $rawline !~ m/^$UTF8*$/) {
2215171ae1a4SAndy Whitcroft			my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
2216171ae1a4SAndy Whitcroft
2217171ae1a4SAndy Whitcroft			my $blank = copy_spacing($rawline);
2218171ae1a4SAndy Whitcroft			my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
2219171ae1a4SAndy Whitcroft			my $hereptr = "$hereline$ptr\n";
2220171ae1a4SAndy Whitcroft
222134d99219SJoe Perches			CHK("INVALID_UTF8",
2222000d1cc1SJoe Perches			    "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
222300df344fSAndy Whitcroft		}
22240a920b5bSAndy Whitcroft
222515662b3eSJoe Perches# Check if it's the start of a commit log
222615662b3eSJoe Perches# (not a header line and we haven't seen the patch filename)
222715662b3eSJoe Perches		if ($in_header_lines && $realfile =~ /^$/ &&
222829ee1b0cSJoe Perches		    !($rawline =~ /^\s+\S/ ||
222929ee1b0cSJoe Perches		      $rawline =~ /^(commit\b|from\b|[\w-]+:).*$/i)) {
223015662b3eSJoe Perches			$in_header_lines = 0;
223115662b3eSJoe Perches			$in_commit_log = 1;
223215662b3eSJoe Perches		}
223315662b3eSJoe Perches
2234fa64205dSPasi Savanainen# Check if there is UTF-8 in a commit log when a mail header has explicitly
2235fa64205dSPasi Savanainen# declined it, i.e defined some charset where it is missing.
2236fa64205dSPasi Savanainen		if ($in_header_lines &&
2237fa64205dSPasi Savanainen		    $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
2238fa64205dSPasi Savanainen		    $1 !~ /utf-8/i) {
2239fa64205dSPasi Savanainen			$non_utf8_charset = 1;
2240fa64205dSPasi Savanainen		}
2241fa64205dSPasi Savanainen
2242fa64205dSPasi Savanainen		if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
224315662b3eSJoe Perches		    $rawline =~ /$NON_ASCII_UTF8/) {
2244fa64205dSPasi Savanainen			WARN("UTF8_BEFORE_PATCH",
224515662b3eSJoe Perches			    "8-bit UTF-8 used in possible commit log\n" . $herecurr);
224615662b3eSJoe Perches		}
224715662b3eSJoe Perches
2248*66b47b4aSKees Cook# Check for various typo / spelling mistakes
2249*66b47b4aSKees Cook		if ($in_commit_log || $line =~ /^\+/) {
2250*66b47b4aSKees Cook			while ($rawline =~ /(?:^|[^a-z@])($misspellings)(?:$|[^a-z@])/gi) {
2251*66b47b4aSKees Cook				my $typo = $1;
2252*66b47b4aSKees Cook				my $typo_fix = $spelling_fix{lc($typo)};
2253*66b47b4aSKees Cook				$typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/);
2254*66b47b4aSKees Cook				$typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/);
2255*66b47b4aSKees Cook				my $msg_type = \&WARN;
2256*66b47b4aSKees Cook				$msg_type = \&CHK if ($file);
2257*66b47b4aSKees Cook				if (&{$msg_type}("TYPO_SPELLING",
2258*66b47b4aSKees Cook						 "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $herecurr) &&
2259*66b47b4aSKees Cook				    $fix) {
2260*66b47b4aSKees Cook					$fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/;
2261*66b47b4aSKees Cook				}
2262*66b47b4aSKees Cook			}
2263*66b47b4aSKees Cook		}
2264*66b47b4aSKees Cook
226530670854SAndy Whitcroft# ignore non-hunk lines and lines being removed
226630670854SAndy Whitcroft		next if (!$hunk_line || $line =~ /^-/);
226700df344fSAndy Whitcroft
22680a920b5bSAndy Whitcroft#trailing whitespace
22699c0ca6f9SAndy Whitcroft		if ($line =~ /^\+.*\015/) {
2270c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2271d5e616fcSJoe Perches			if (ERROR("DOS_LINE_ENDINGS",
2272d5e616fcSJoe Perches				  "DOS line endings\n" . $herevet) &&
2273d5e616fcSJoe Perches			    $fix) {
2274194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/[\s\015]+$//;
2275d5e616fcSJoe Perches			}
2276c2fdda0dSAndy Whitcroft		} elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
2277c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
22783705ce5bSJoe Perches			if (ERROR("TRAILING_WHITESPACE",
22793705ce5bSJoe Perches				  "trailing whitespace\n" . $herevet) &&
22803705ce5bSJoe Perches			    $fix) {
2281194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\s+$//;
22823705ce5bSJoe Perches			}
22833705ce5bSJoe Perches
2284d2c0a235SAndy Whitcroft			$rpt_cleaners = 1;
22850a920b5bSAndy Whitcroft		}
22865368df20SAndy Whitcroft
22874783f894SJosh Triplett# Check for FSF mailing addresses.
2288109d8cb2SAlexander Duyck		if ($rawline =~ /\bwrite to the Free/i ||
22893e2232f2SJoe Perches		    $rawline =~ /\b59\s+Temple\s+Pl/i ||
22903e2232f2SJoe Perches		    $rawline =~ /\b51\s+Franklin\s+St/i) {
22914783f894SJosh Triplett			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
22924783f894SJosh Triplett			my $msg_type = \&ERROR;
22934783f894SJosh Triplett			$msg_type = \&CHK if ($file);
22944783f894SJosh Triplett			&{$msg_type}("FSF_MAILING_ADDRESS",
22954783f894SJosh 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)
22964783f894SJosh Triplett		}
22974783f894SJosh Triplett
22983354957aSAndi Kleen# check for Kconfig help text having a real description
22999fe287d7SAndy Whitcroft# Only applies when adding the entry originally, after that we do not have
23009fe287d7SAndy Whitcroft# sufficient context to determine whether it is indeed long enough.
23013354957aSAndi Kleen		if ($realfile =~ /Kconfig/ &&
23028d73e0e7SJoe Perches		    $line =~ /^\+\s*config\s+/) {
23033354957aSAndi Kleen			my $length = 0;
23049fe287d7SAndy Whitcroft			my $cnt = $realcnt;
23059fe287d7SAndy Whitcroft			my $ln = $linenr + 1;
23069fe287d7SAndy Whitcroft			my $f;
2307a1385803SAndy Whitcroft			my $is_start = 0;
23089fe287d7SAndy Whitcroft			my $is_end = 0;
2309a1385803SAndy Whitcroft			for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
23109fe287d7SAndy Whitcroft				$f = $lines[$ln - 1];
23119fe287d7SAndy Whitcroft				$cnt-- if ($lines[$ln - 1] !~ /^-/);
23129fe287d7SAndy Whitcroft				$is_end = $lines[$ln - 1] =~ /^\+/;
23139fe287d7SAndy Whitcroft
23149fe287d7SAndy Whitcroft				next if ($f =~ /^-/);
23158d73e0e7SJoe Perches				last if (!$file && $f =~ /^\@\@/);
2316a1385803SAndy Whitcroft
23178d73e0e7SJoe Perches				if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate)\s*\"/) {
2318a1385803SAndy Whitcroft					$is_start = 1;
23198d73e0e7SJoe Perches				} elsif ($lines[$ln - 1] =~ /^\+\s*(?:---)?help(?:---)?$/) {
2320a1385803SAndy Whitcroft					$length = -1;
2321a1385803SAndy Whitcroft				}
2322a1385803SAndy Whitcroft
23239fe287d7SAndy Whitcroft				$f =~ s/^.//;
23243354957aSAndi Kleen				$f =~ s/#.*//;
23253354957aSAndi Kleen				$f =~ s/^\s+//;
23263354957aSAndi Kleen				next if ($f =~ /^$/);
23279fe287d7SAndy Whitcroft				if ($f =~ /^\s*config\s/) {
23289fe287d7SAndy Whitcroft					$is_end = 1;
23299fe287d7SAndy Whitcroft					last;
23309fe287d7SAndy Whitcroft				}
23313354957aSAndi Kleen				$length++;
23323354957aSAndi Kleen			}
233356193274SVadim Bendebury			if ($is_start && $is_end && $length < $min_conf_desc_length) {
2334000d1cc1SJoe Perches				WARN("CONFIG_DESCRIPTION",
233556193274SVadim Bendebury				     "please write a paragraph that describes the config symbol fully\n" . $herecurr);
233656193274SVadim Bendebury			}
2337a1385803SAndy Whitcroft			#print "is_start<$is_start> is_end<$is_end> length<$length>\n";
23383354957aSAndi Kleen		}
23393354957aSAndi Kleen
23401ba8dfd1SKees Cook# discourage the addition of CONFIG_EXPERIMENTAL in Kconfig.
23411ba8dfd1SKees Cook		if ($realfile =~ /Kconfig/ &&
23421ba8dfd1SKees Cook		    $line =~ /.\s*depends on\s+.*\bEXPERIMENTAL\b/) {
23431ba8dfd1SKees Cook			WARN("CONFIG_EXPERIMENTAL",
23441ba8dfd1SKees Cook			     "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
23451ba8dfd1SKees Cook		}
23461ba8dfd1SKees Cook
2347c68e5878SArnaud Lacombe		if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
2348c68e5878SArnaud Lacombe		    ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
2349c68e5878SArnaud Lacombe			my $flag = $1;
2350c68e5878SArnaud Lacombe			my $replacement = {
2351c68e5878SArnaud Lacombe				'EXTRA_AFLAGS' =>   'asflags-y',
2352c68e5878SArnaud Lacombe				'EXTRA_CFLAGS' =>   'ccflags-y',
2353c68e5878SArnaud Lacombe				'EXTRA_CPPFLAGS' => 'cppflags-y',
2354c68e5878SArnaud Lacombe				'EXTRA_LDFLAGS' =>  'ldflags-y',
2355c68e5878SArnaud Lacombe			};
2356c68e5878SArnaud Lacombe
2357c68e5878SArnaud Lacombe			WARN("DEPRECATED_VARIABLE",
2358c68e5878SArnaud Lacombe			     "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
2359c68e5878SArnaud Lacombe		}
2360c68e5878SArnaud Lacombe
2361bff5da43SRob Herring# check for DT compatible documentation
23627dd05b38SFlorian Vaussard		if (defined $root &&
23637dd05b38SFlorian Vaussard			(($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) ||
23647dd05b38SFlorian Vaussard			 ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) {
23657dd05b38SFlorian Vaussard
2366bff5da43SRob Herring			my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g;
2367bff5da43SRob Herring
2368cc93319bSFlorian Vaussard			my $dt_path = $root . "/Documentation/devicetree/bindings/";
2369cc93319bSFlorian Vaussard			my $vp_file = $dt_path . "vendor-prefixes.txt";
2370cc93319bSFlorian Vaussard
2371bff5da43SRob Herring			foreach my $compat (@compats) {
2372bff5da43SRob Herring				my $compat2 = $compat;
2373185d566bSRob Herring				$compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/;
2374185d566bSRob Herring				my $compat3 = $compat;
2375185d566bSRob Herring				$compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/;
2376185d566bSRob Herring				`grep -Erq "$compat|$compat2|$compat3" $dt_path`;
2377bff5da43SRob Herring				if ( $? >> 8 ) {
2378bff5da43SRob Herring					WARN("UNDOCUMENTED_DT_STRING",
2379bff5da43SRob Herring					     "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr);
2380bff5da43SRob Herring				}
2381bff5da43SRob Herring
23824fbf32a6SFlorian Vaussard				next if $compat !~ /^([a-zA-Z0-9\-]+)\,/;
23834fbf32a6SFlorian Vaussard				my $vendor = $1;
2384cc93319bSFlorian Vaussard				`grep -Eq "^$vendor\\b" $vp_file`;
2385bff5da43SRob Herring				if ( $? >> 8 ) {
2386bff5da43SRob Herring					WARN("UNDOCUMENTED_DT_STRING",
2387cc93319bSFlorian Vaussard					     "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr);
2388bff5da43SRob Herring				}
2389bff5da43SRob Herring			}
2390bff5da43SRob Herring		}
2391bff5da43SRob Herring
23925368df20SAndy Whitcroft# check we are in a valid source file if not then ignore this hunk
2393de4c924cSGeert Uytterhoeven		next if ($realfile !~ /\.(h|c|s|S|pl|sh|dtsi|dts)$/);
23945368df20SAndy Whitcroft
23956cd7f386SJoe Perches#line length limit
2396c45dcabdSAndy Whitcroft		if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ &&
2397f4c014c0SAndy Whitcroft		    $rawline !~ /^.\s*\*\s*\@$Ident\s/ &&
23980fccc622SJoe Perches		    !($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(KERN_\S+\s*|[^"]*))?"[X\t]*"\s*(?:|,|\)\s*;)\s*$/ ||
23998bbea968SJoe Perches		    $line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) &&
24006cd7f386SJoe Perches		    $length > $max_line_length)
2401c45dcabdSAndy Whitcroft		{
2402000d1cc1SJoe Perches			WARN("LONG_LINE",
24036cd7f386SJoe Perches			     "line over $max_line_length characters\n" . $herecurr);
24040a920b5bSAndy Whitcroft		}
24050a920b5bSAndy Whitcroft
2406ca56dc09SJosh Triplett# Check for user-visible strings broken across lines, which breaks the ability
24078c5fcd24SJoe Perches# to grep for the string.  Make exceptions when the previous string ends in a
24088c5fcd24SJoe Perches# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{'
24098c5fcd24SJoe Perches# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value
2410ca56dc09SJosh Triplett		if ($line =~ /^\+\s*"/ &&
2411ca56dc09SJosh Triplett		    $prevline =~ /"\s*$/ &&
24128c5fcd24SJoe Perches		    $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) {
2413ca56dc09SJosh Triplett			WARN("SPLIT_STRING",
2414ca56dc09SJosh Triplett			     "quoted string split across lines\n" . $hereprev);
2415ca56dc09SJosh Triplett		}
2416ca56dc09SJosh Triplett
2417ece9659fSDan Carpenter# check for missing a space in a string concatination
2418ece9659fSDan Carpenter		if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) {
2419ece9659fSDan Carpenter			WARN('MISSING_SPACE',
2420ece9659fSDan Carpenter			     "break quoted strings at a space character\n" . $hereprev);
2421ece9659fSDan Carpenter		}
2422ece9659fSDan Carpenter
24235e79d96eSJoe Perches# check for spaces before a quoted newline
24245e79d96eSJoe Perches		if ($rawline =~ /^.*\".*\s\\n/) {
24253705ce5bSJoe Perches			if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
24263705ce5bSJoe Perches				 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
24273705ce5bSJoe Perches			    $fix) {
2428194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
24293705ce5bSJoe Perches			}
24303705ce5bSJoe Perches
24315e79d96eSJoe Perches		}
24325e79d96eSJoe Perches
24338905a67cSAndy Whitcroft# check for adding lines without a newline.
24348905a67cSAndy Whitcroft		if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
2435000d1cc1SJoe Perches			WARN("MISSING_EOF_NEWLINE",
2436000d1cc1SJoe Perches			     "adding a line without newline at end of file\n" . $herecurr);
24378905a67cSAndy Whitcroft		}
24388905a67cSAndy Whitcroft
243942e41c54SMike Frysinger# Blackfin: use hi/lo macros
244042e41c54SMike Frysinger		if ($realfile =~ m@arch/blackfin/.*\.S$@) {
244142e41c54SMike Frysinger			if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
244242e41c54SMike Frysinger				my $herevet = "$here\n" . cat_vet($line) . "\n";
2443000d1cc1SJoe Perches				ERROR("LO_MACRO",
2444000d1cc1SJoe Perches				      "use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
244542e41c54SMike Frysinger			}
244642e41c54SMike Frysinger			if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
244742e41c54SMike Frysinger				my $herevet = "$here\n" . cat_vet($line) . "\n";
2448000d1cc1SJoe Perches				ERROR("HI_MACRO",
2449000d1cc1SJoe Perches				      "use the HI() macro, not (... >> 16)\n" . $herevet);
245042e41c54SMike Frysinger			}
245142e41c54SMike Frysinger		}
245242e41c54SMike Frysinger
2453b9ea10d6SAndy Whitcroft# check we are in a valid source file C or perl if not then ignore this hunk
2454de4c924cSGeert Uytterhoeven		next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/);
24550a920b5bSAndy Whitcroft
24560a920b5bSAndy Whitcroft# at the beginning of a line any tabs must come first and anything
24570a920b5bSAndy Whitcroft# more than 8 must use tabs.
2458c2fdda0dSAndy Whitcroft		if ($rawline =~ /^\+\s* \t\s*\S/ ||
2459c2fdda0dSAndy Whitcroft		    $rawline =~ /^\+\s*        \s*/) {
2460c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2461d2c0a235SAndy Whitcroft			$rpt_cleaners = 1;
24623705ce5bSJoe Perches			if (ERROR("CODE_INDENT",
24633705ce5bSJoe Perches				  "code indent should use tabs where possible\n" . $herevet) &&
24643705ce5bSJoe Perches			    $fix) {
2465194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
24663705ce5bSJoe Perches			}
24670a920b5bSAndy Whitcroft		}
24680a920b5bSAndy Whitcroft
246908e44365SAlberto Panizzo# check for space before tabs.
247008e44365SAlberto Panizzo		if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
247108e44365SAlberto Panizzo			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
24723705ce5bSJoe Perches			if (WARN("SPACE_BEFORE_TAB",
24733705ce5bSJoe Perches				"please, no space before tabs\n" . $herevet) &&
24743705ce5bSJoe Perches			    $fix) {
2475194f66fcSJoe Perches				while ($fixed[$fixlinenr] =~
2476d2207ccbSJoe Perches					   s/(^\+.*) {8,8}\t/$1\t\t/) {}
2477194f66fcSJoe Perches				while ($fixed[$fixlinenr] =~
2478c76f4cb3SJoe Perches					   s/(^\+.*) +\t/$1\t/) {}
24793705ce5bSJoe Perches			}
248008e44365SAlberto Panizzo		}
248108e44365SAlberto Panizzo
2482d1fe9c09SJoe Perches# check for && or || at the start of a line
2483d1fe9c09SJoe Perches		if ($rawline =~ /^\+\s*(&&|\|\|)/) {
2484d1fe9c09SJoe Perches			CHK("LOGICAL_CONTINUATIONS",
2485d1fe9c09SJoe Perches			    "Logical continuations should be on the previous line\n" . $hereprev);
2486d1fe9c09SJoe Perches		}
2487d1fe9c09SJoe Perches
2488d1fe9c09SJoe Perches# check multi-line statement indentation matches previous line
2489d1fe9c09SJoe Perches		if ($^V && $^V ge 5.10.0 &&
249091cb5195SJoe Perches		    $prevline =~ /^\+([ \t]*)((?:$c90_Keywords(?:\s+if)\s*)|(?:$Declare\s*)?(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*|$Ident\s*=\s*$Ident\s*)\(.*(\&\&|\|\||,)\s*$/) {
2491d1fe9c09SJoe Perches			$prevline =~ /^\+(\t*)(.*)$/;
2492d1fe9c09SJoe Perches			my $oldindent = $1;
2493d1fe9c09SJoe Perches			my $rest = $2;
2494d1fe9c09SJoe Perches
2495d1fe9c09SJoe Perches			my $pos = pos_last_openparen($rest);
2496d1fe9c09SJoe Perches			if ($pos >= 0) {
2497b34a26f3SJoe Perches				$line =~ /^(\+| )([ \t]*)/;
2498b34a26f3SJoe Perches				my $newindent = $2;
2499d1fe9c09SJoe Perches
2500d1fe9c09SJoe Perches				my $goodtabindent = $oldindent .
2501d1fe9c09SJoe Perches					"\t" x ($pos / 8) .
2502d1fe9c09SJoe Perches					" "  x ($pos % 8);
2503d1fe9c09SJoe Perches				my $goodspaceindent = $oldindent . " "  x $pos;
2504d1fe9c09SJoe Perches
2505d1fe9c09SJoe Perches				if ($newindent ne $goodtabindent &&
2506d1fe9c09SJoe Perches				    $newindent ne $goodspaceindent) {
25073705ce5bSJoe Perches
25083705ce5bSJoe Perches					if (CHK("PARENTHESIS_ALIGNMENT",
25093705ce5bSJoe Perches						"Alignment should match open parenthesis\n" . $hereprev) &&
25103705ce5bSJoe Perches					    $fix && $line =~ /^\+/) {
2511194f66fcSJoe Perches						$fixed[$fixlinenr] =~
25123705ce5bSJoe Perches						    s/^\+[ \t]*/\+$goodtabindent/;
25133705ce5bSJoe Perches					}
2514d1fe9c09SJoe Perches				}
2515d1fe9c09SJoe Perches			}
2516d1fe9c09SJoe Perches		}
2517d1fe9c09SJoe Perches
2518308cc8d8SJoe Perches		if ($line =~ /^\+.*\(\s*$Type\s*\)[ \t]+(?!$Assignment|$Arithmetic|{)/) {
25193705ce5bSJoe Perches			if (CHK("SPACING",
2520f27c95dbSJoe Perches				"No space is necessary after a cast\n" . $herecurr) &&
25213705ce5bSJoe Perches			    $fix) {
2522194f66fcSJoe Perches				$fixed[$fixlinenr] =~
2523f27c95dbSJoe Perches				    s/(\(\s*$Type\s*\))[ \t]+/$1/;
25243705ce5bSJoe Perches			}
2525aad4f614SJoe Perches		}
2526aad4f614SJoe Perches
252705880600SJoe Perches		if ($realfile =~ m@^(drivers/net/|net/)@ &&
2528fdb4bcd6SJoe Perches		    $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
252985ad978cSJoe Perches		    $rawline =~ /^\+[ \t]*\*/ &&
253085ad978cSJoe Perches		    $realline > 2) {
253105880600SJoe Perches			WARN("NETWORKING_BLOCK_COMMENT_STYLE",
253205880600SJoe Perches			     "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
253305880600SJoe Perches		}
253405880600SJoe Perches
253505880600SJoe Perches		if ($realfile =~ m@^(drivers/net/|net/)@ &&
2536a605e32eSJoe Perches		    $prevrawline =~ /^\+[ \t]*\/\*/ &&		#starting /*
2537a605e32eSJoe Perches		    $prevrawline !~ /\*\/[ \t]*$/ &&		#no trailing */
253861135e96SJoe Perches		    $rawline =~ /^\+/ &&			#line is new
2539a605e32eSJoe Perches		    $rawline !~ /^\+[ \t]*\*/) {		#no leading *
2540a605e32eSJoe Perches			WARN("NETWORKING_BLOCK_COMMENT_STYLE",
2541a605e32eSJoe Perches			     "networking block comments start with * on subsequent lines\n" . $hereprev);
2542a605e32eSJoe Perches		}
2543a605e32eSJoe Perches
2544a605e32eSJoe Perches		if ($realfile =~ m@^(drivers/net/|net/)@ &&
2545c24f9f19SJoe Perches		    $rawline !~ m@^\+[ \t]*\*/[ \t]*$@ &&	#trailing */
2546c24f9f19SJoe Perches		    $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ &&	#inline /*...*/
2547c24f9f19SJoe Perches		    $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ &&	#trailing **/
2548c24f9f19SJoe Perches		    $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) {	#non blank */
254905880600SJoe Perches			WARN("NETWORKING_BLOCK_COMMENT_STYLE",
255005880600SJoe Perches			     "networking block comments put the trailing */ on a separate line\n" . $herecurr);
255105880600SJoe Perches		}
255205880600SJoe Perches
25537f619191SJoe Perches# check for missing blank lines after struct/union declarations
25547f619191SJoe Perches# with exceptions for various attributes and macros
25557f619191SJoe Perches		if ($prevline =~ /^[\+ ]};?\s*$/ &&
25567f619191SJoe Perches		    $line =~ /^\+/ &&
25577f619191SJoe Perches		    !($line =~ /^\+\s*$/ ||
25587f619191SJoe Perches		      $line =~ /^\+\s*EXPORT_SYMBOL/ ||
25597f619191SJoe Perches		      $line =~ /^\+\s*MODULE_/i ||
25607f619191SJoe Perches		      $line =~ /^\+\s*\#\s*(?:end|elif|else)/ ||
25617f619191SJoe Perches		      $line =~ /^\+[a-z_]*init/ ||
25627f619191SJoe Perches		      $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ ||
25637f619191SJoe Perches		      $line =~ /^\+\s*DECLARE/ ||
25647f619191SJoe Perches		      $line =~ /^\+\s*__setup/)) {
2565d752fcc8SJoe Perches			if (CHK("LINE_SPACING",
2566d752fcc8SJoe Perches				"Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) &&
2567d752fcc8SJoe Perches			    $fix) {
2568f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, "\+");
2569d752fcc8SJoe Perches			}
25707f619191SJoe Perches		}
25717f619191SJoe Perches
2572365dd4eaSJoe Perches# check for multiple consecutive blank lines
2573365dd4eaSJoe Perches		if ($prevline =~ /^[\+ ]\s*$/ &&
2574365dd4eaSJoe Perches		    $line =~ /^\+\s*$/ &&
2575365dd4eaSJoe Perches		    $last_blank_line != ($linenr - 1)) {
2576d752fcc8SJoe Perches			if (CHK("LINE_SPACING",
2577d752fcc8SJoe Perches				"Please don't use multiple blank lines\n" . $hereprev) &&
2578d752fcc8SJoe Perches			    $fix) {
2579f2d7e4d4SJoe Perches				fix_delete_line($fixlinenr, $rawline);
2580d752fcc8SJoe Perches			}
2581d752fcc8SJoe Perches
2582365dd4eaSJoe Perches			$last_blank_line = $linenr;
2583365dd4eaSJoe Perches		}
2584365dd4eaSJoe Perches
25853b617e3bSJoe Perches# check for missing blank lines after declarations
25863f7bac03SJoe Perches		if ($sline =~ /^\+\s+\S/ &&			#Not at char 1
25873f7bac03SJoe Perches			# actual declarations
25883f7bac03SJoe Perches		    ($prevline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
25895a4e1fd3SJoe Perches			# function pointer declarations
25905a4e1fd3SJoe Perches		     $prevline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
25913f7bac03SJoe Perches			# foo bar; where foo is some local typedef or #define
25923f7bac03SJoe Perches		     $prevline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
25933f7bac03SJoe Perches			# known declaration macros
25943f7bac03SJoe Perches		     $prevline =~ /^\+\s+$declaration_macros/) &&
25953f7bac03SJoe Perches			# for "else if" which can look like "$Ident $Ident"
25963f7bac03SJoe Perches		    !($prevline =~ /^\+\s+$c90_Keywords\b/ ||
25973f7bac03SJoe Perches			# other possible extensions of declaration lines
25983f7bac03SJoe Perches		      $prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ ||
25993f7bac03SJoe Perches			# not starting a section or a macro "\" extended line
26003f7bac03SJoe Perches		      $prevline =~ /(?:\{\s*|\\)$/) &&
26013f7bac03SJoe Perches			# looks like a declaration
26023f7bac03SJoe Perches		    !($sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
26035a4e1fd3SJoe Perches			# function pointer declarations
26045a4e1fd3SJoe Perches		      $sline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
26053f7bac03SJoe Perches			# foo bar; where foo is some local typedef or #define
26063f7bac03SJoe Perches		      $sline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
26073f7bac03SJoe Perches			# known declaration macros
26083f7bac03SJoe Perches		      $sline =~ /^\+\s+$declaration_macros/ ||
26093f7bac03SJoe Perches			# start of struct or union or enum
26103b617e3bSJoe Perches		      $sline =~ /^\+\s+(?:union|struct|enum|typedef)\b/ ||
26113f7bac03SJoe Perches			# start or end of block or continuation of declaration
26123f7bac03SJoe Perches		      $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ ||
26133f7bac03SJoe Perches			# bitfield continuation
26143f7bac03SJoe Perches		      $sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ ||
26153f7bac03SJoe Perches			# other possible extensions of declaration lines
26163f7bac03SJoe Perches		      $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) &&
26173f7bac03SJoe Perches			# indentation of previous and current line are the same
26183f7bac03SJoe Perches		    (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) {
2619d752fcc8SJoe Perches			if (WARN("LINE_SPACING",
2620d752fcc8SJoe Perches				 "Missing a blank line after declarations\n" . $hereprev) &&
2621d752fcc8SJoe Perches			    $fix) {
2622f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, "\+");
2623d752fcc8SJoe Perches			}
26243b617e3bSJoe Perches		}
26253b617e3bSJoe Perches
26265f7ddae6SRaffaele Recalcati# check for spaces at the beginning of a line.
26276b4c5bebSAndy Whitcroft# Exceptions:
26286b4c5bebSAndy Whitcroft#  1) within comments
26296b4c5bebSAndy Whitcroft#  2) indented preprocessor commands
26306b4c5bebSAndy Whitcroft#  3) hanging labels
26313705ce5bSJoe Perches		if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/)  {
26325f7ddae6SRaffaele Recalcati			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
26333705ce5bSJoe Perches			if (WARN("LEADING_SPACE",
26343705ce5bSJoe Perches				 "please, no spaces at the start of a line\n" . $herevet) &&
26353705ce5bSJoe Perches			    $fix) {
2636194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
26373705ce5bSJoe Perches			}
26385f7ddae6SRaffaele Recalcati		}
26395f7ddae6SRaffaele Recalcati
2640b9ea10d6SAndy Whitcroft# check we are in a valid C source file if not then ignore this hunk
2641b9ea10d6SAndy Whitcroft		next if ($realfile !~ /\.(h|c)$/);
2642b9ea10d6SAndy Whitcroft
2643032a4c0fSJoe Perches# check indentation of any line with a bare else
2644032a4c0fSJoe Perches# if the previous line is a break or return and is indented 1 tab more...
2645032a4c0fSJoe Perches		if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {
2646032a4c0fSJoe Perches			my $tabs = length($1) + 1;
2647032a4c0fSJoe Perches			if ($prevline =~ /^\+\t{$tabs,$tabs}(?:break|return)\b/) {
2648032a4c0fSJoe Perches				WARN("UNNECESSARY_ELSE",
2649032a4c0fSJoe Perches				     "else is not generally useful after a break or return\n" . $hereprev);
2650032a4c0fSJoe Perches			}
2651032a4c0fSJoe Perches		}
2652032a4c0fSJoe Perches
2653c00df19aSJoe Perches# check indentation of a line with a break;
2654c00df19aSJoe Perches# if the previous line is a goto or return and is indented the same # of tabs
2655c00df19aSJoe Perches		if ($sline =~ /^\+([\t]+)break\s*;\s*$/) {
2656c00df19aSJoe Perches			my $tabs = $1;
2657c00df19aSJoe Perches			if ($prevline =~ /^\+$tabs(?:goto|return)\b/) {
2658c00df19aSJoe Perches				WARN("UNNECESSARY_BREAK",
2659c00df19aSJoe Perches				     "break is not useful after a goto or return\n" . $hereprev);
2660c00df19aSJoe Perches			}
2661c00df19aSJoe Perches		}
2662c00df19aSJoe Perches
26631ba8dfd1SKees Cook# discourage the addition of CONFIG_EXPERIMENTAL in #if(def).
26641ba8dfd1SKees Cook		if ($line =~ /^\+\s*\#\s*if.*\bCONFIG_EXPERIMENTAL\b/) {
26651ba8dfd1SKees Cook			WARN("CONFIG_EXPERIMENTAL",
26661ba8dfd1SKees Cook			     "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
26671ba8dfd1SKees Cook		}
26681ba8dfd1SKees Cook
2669c2fdda0dSAndy Whitcroft# check for RCS/CVS revision markers
2670cf655043SAndy Whitcroft		if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
2671000d1cc1SJoe Perches			WARN("CVS_KEYWORD",
2672000d1cc1SJoe Perches			     "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
2673c2fdda0dSAndy Whitcroft		}
267422f2a2efSAndy Whitcroft
267542e41c54SMike Frysinger# Blackfin: don't use __builtin_bfin_[cs]sync
267642e41c54SMike Frysinger		if ($line =~ /__builtin_bfin_csync/) {
267742e41c54SMike Frysinger			my $herevet = "$here\n" . cat_vet($line) . "\n";
2678000d1cc1SJoe Perches			ERROR("CSYNC",
2679000d1cc1SJoe Perches			      "use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
268042e41c54SMike Frysinger		}
268142e41c54SMike Frysinger		if ($line =~ /__builtin_bfin_ssync/) {
268242e41c54SMike Frysinger			my $herevet = "$here\n" . cat_vet($line) . "\n";
2683000d1cc1SJoe Perches			ERROR("SSYNC",
2684000d1cc1SJoe Perches			      "use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
268542e41c54SMike Frysinger		}
268642e41c54SMike Frysinger
268756e77d70SJoe Perches# check for old HOTPLUG __dev<foo> section markings
268856e77d70SJoe Perches		if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
268956e77d70SJoe Perches			WARN("HOTPLUG_SECTION",
269056e77d70SJoe Perches			     "Using $1 is unnecessary\n" . $herecurr);
269156e77d70SJoe Perches		}
269256e77d70SJoe Perches
26939c0ca6f9SAndy Whitcroft# Check for potential 'bare' types
26942b474a1aSAndy Whitcroft		my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
26952b474a1aSAndy Whitcroft		    $realline_next);
26963e469cdcSAndy Whitcroft#print "LINE<$line>\n";
26973e469cdcSAndy Whitcroft		if ($linenr >= $suppress_statement &&
26981b5539b1SJoe Perches		    $realcnt && $sline =~ /.\s*\S/) {
2699170d3a22SAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
2700f5fe35ddSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0);
2701171ae1a4SAndy Whitcroft			$stat =~ s/\n./\n /g;
2702171ae1a4SAndy Whitcroft			$cond =~ s/\n./\n /g;
2703171ae1a4SAndy Whitcroft
27043e469cdcSAndy Whitcroft#print "linenr<$linenr> <$stat>\n";
27053e469cdcSAndy Whitcroft			# If this statement has no statement boundaries within
27063e469cdcSAndy Whitcroft			# it there is no point in retrying a statement scan
27073e469cdcSAndy Whitcroft			# until we hit end of it.
27083e469cdcSAndy Whitcroft			my $frag = $stat; $frag =~ s/;+\s*$//;
27093e469cdcSAndy Whitcroft			if ($frag !~ /(?:{|;)/) {
27103e469cdcSAndy Whitcroft#print "skip<$line_nr_next>\n";
27113e469cdcSAndy Whitcroft				$suppress_statement = $line_nr_next;
27123e469cdcSAndy Whitcroft			}
2713f74bd194SAndy Whitcroft
27142b474a1aSAndy Whitcroft			# Find the real next line.
27152b474a1aSAndy Whitcroft			$realline_next = $line_nr_next;
27162b474a1aSAndy Whitcroft			if (defined $realline_next &&
27172b474a1aSAndy Whitcroft			    (!defined $lines[$realline_next - 1] ||
27182b474a1aSAndy Whitcroft			     substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
27192b474a1aSAndy Whitcroft				$realline_next++;
27202b474a1aSAndy Whitcroft			}
27212b474a1aSAndy Whitcroft
2722171ae1a4SAndy Whitcroft			my $s = $stat;
2723171ae1a4SAndy Whitcroft			$s =~ s/{.*$//s;
2724cf655043SAndy Whitcroft
2725c2fdda0dSAndy Whitcroft			# Ignore goto labels.
2726171ae1a4SAndy Whitcroft			if ($s =~ /$Ident:\*$/s) {
2727c2fdda0dSAndy Whitcroft
2728c2fdda0dSAndy Whitcroft			# Ignore functions being called
2729171ae1a4SAndy Whitcroft			} elsif ($s =~ /^.\s*$Ident\s*\(/s) {
2730c2fdda0dSAndy Whitcroft
2731463f2864SAndy Whitcroft			} elsif ($s =~ /^.\s*else\b/s) {
2732463f2864SAndy Whitcroft
2733c45dcabdSAndy Whitcroft			# declarations always start with types
2734d2506586SAndy 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) {
2735c45dcabdSAndy Whitcroft				my $type = $1;
2736c45dcabdSAndy Whitcroft				$type =~ s/\s+/ /g;
2737c45dcabdSAndy Whitcroft				possible($type, "A:" . $s);
2738c45dcabdSAndy Whitcroft
27396c72ffaaSAndy Whitcroft			# definitions in global scope can only start with types
2740a6a84062SAndy Whitcroft			} elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
2741c45dcabdSAndy Whitcroft				possible($1, "B:" . $s);
2742c2fdda0dSAndy Whitcroft			}
27438905a67cSAndy Whitcroft
27446c72ffaaSAndy Whitcroft			# any (foo ... *) is a pointer cast, and foo is a type
274565863862SAndy Whitcroft			while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
2746c45dcabdSAndy Whitcroft				possible($1, "C:" . $s);
27479c0ca6f9SAndy Whitcroft			}
27488905a67cSAndy Whitcroft
27498905a67cSAndy Whitcroft			# Check for any sort of function declaration.
27508905a67cSAndy Whitcroft			# int foo(something bar, other baz);
27518905a67cSAndy Whitcroft			# void (*store_gdt)(x86_descr_ptr *);
2752171ae1a4SAndy Whitcroft			if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
27538905a67cSAndy Whitcroft				my ($name_len) = length($1);
27548905a67cSAndy Whitcroft
2755cf655043SAndy Whitcroft				my $ctx = $s;
2756773647a0SAndy Whitcroft				substr($ctx, 0, $name_len + 1, '');
27578905a67cSAndy Whitcroft				$ctx =~ s/\)[^\)]*$//;
2758cf655043SAndy Whitcroft
27598905a67cSAndy Whitcroft				for my $arg (split(/\s*,\s*/, $ctx)) {
2760c45dcabdSAndy Whitcroft					if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
27618905a67cSAndy Whitcroft
2762c45dcabdSAndy Whitcroft						possible($1, "D:" . $s);
27638905a67cSAndy Whitcroft					}
27648905a67cSAndy Whitcroft				}
27658905a67cSAndy Whitcroft			}
27668905a67cSAndy Whitcroft
27679c0ca6f9SAndy Whitcroft		}
27689c0ca6f9SAndy Whitcroft
276900df344fSAndy Whitcroft#
277000df344fSAndy Whitcroft# Checks which may be anchored in the context.
277100df344fSAndy Whitcroft#
277200df344fSAndy Whitcroft
277300df344fSAndy Whitcroft# Check for switch () and associated case and default
277400df344fSAndy Whitcroft# statements should be at the same indent.
277500df344fSAndy Whitcroft		if ($line=~/\bswitch\s*\(.*\)/) {
277600df344fSAndy Whitcroft			my $err = '';
277700df344fSAndy Whitcroft			my $sep = '';
277800df344fSAndy Whitcroft			my @ctx = ctx_block_outer($linenr, $realcnt);
277900df344fSAndy Whitcroft			shift(@ctx);
278000df344fSAndy Whitcroft			for my $ctx (@ctx) {
278100df344fSAndy Whitcroft				my ($clen, $cindent) = line_stats($ctx);
278200df344fSAndy Whitcroft				if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
278300df344fSAndy Whitcroft							$indent != $cindent) {
278400df344fSAndy Whitcroft					$err .= "$sep$ctx\n";
278500df344fSAndy Whitcroft					$sep = '';
278600df344fSAndy Whitcroft				} else {
278700df344fSAndy Whitcroft					$sep = "[...]\n";
278800df344fSAndy Whitcroft				}
278900df344fSAndy Whitcroft			}
279000df344fSAndy Whitcroft			if ($err ne '') {
2791000d1cc1SJoe Perches				ERROR("SWITCH_CASE_INDENT_LEVEL",
2792000d1cc1SJoe Perches				      "switch and case should be at the same indent\n$hereline$err");
2793de7d4f0eSAndy Whitcroft			}
2794de7d4f0eSAndy Whitcroft		}
2795de7d4f0eSAndy Whitcroft
2796de7d4f0eSAndy Whitcroft# if/while/etc brace do not go on next line, unless defining a do while loop,
2797de7d4f0eSAndy Whitcroft# or if that brace on the next line is for something else
27980fe3dc2bSJoe Perches		if ($line =~ /(.*)\b((?:if|while|for|switch|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
2799773647a0SAndy Whitcroft			my $pre_ctx = "$1$2";
2800773647a0SAndy Whitcroft
28019c0ca6f9SAndy Whitcroft			my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
28028eef05ddSJoe Perches
28038eef05ddSJoe Perches			if ($line =~ /^\+\t{6,}/) {
28048eef05ddSJoe Perches				WARN("DEEP_INDENTATION",
28058eef05ddSJoe Perches				     "Too many leading tabs - consider code refactoring\n" . $herecurr);
28068eef05ddSJoe Perches			}
28078eef05ddSJoe Perches
2808de7d4f0eSAndy Whitcroft			my $ctx_cnt = $realcnt - $#ctx - 1;
2809de7d4f0eSAndy Whitcroft			my $ctx = join("\n", @ctx);
2810de7d4f0eSAndy Whitcroft
2811548596d5SAndy Whitcroft			my $ctx_ln = $linenr;
2812548596d5SAndy Whitcroft			my $ctx_skip = $realcnt;
2813de7d4f0eSAndy Whitcroft
2814548596d5SAndy Whitcroft			while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
2815548596d5SAndy Whitcroft					defined $lines[$ctx_ln - 1] &&
2816548596d5SAndy Whitcroft					$lines[$ctx_ln - 1] =~ /^-/)) {
2817548596d5SAndy Whitcroft				##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
2818548596d5SAndy Whitcroft				$ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
2819773647a0SAndy Whitcroft				$ctx_ln++;
2820773647a0SAndy Whitcroft			}
2821548596d5SAndy Whitcroft
282253210168SAndy Whitcroft			#print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
282353210168SAndy Whitcroft			#print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
2824773647a0SAndy Whitcroft
2825773647a0SAndy Whitcroft			if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
2826000d1cc1SJoe Perches				ERROR("OPEN_BRACE",
2827000d1cc1SJoe Perches				      "that open brace { should be on the previous line\n" .
282801464f30SAndy Whitcroft					"$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
282900df344fSAndy Whitcroft			}
2830773647a0SAndy Whitcroft			if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
2831773647a0SAndy Whitcroft			    $ctx =~ /\)\s*\;\s*$/ &&
2832773647a0SAndy Whitcroft			    defined $lines[$ctx_ln - 1])
2833773647a0SAndy Whitcroft			{
28349c0ca6f9SAndy Whitcroft				my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
28359c0ca6f9SAndy Whitcroft				if ($nindent > $indent) {
2836000d1cc1SJoe Perches					WARN("TRAILING_SEMICOLON",
2837000d1cc1SJoe Perches					     "trailing semicolon indicates no statements, indent implies otherwise\n" .
283801464f30SAndy Whitcroft						"$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
28399c0ca6f9SAndy Whitcroft				}
28409c0ca6f9SAndy Whitcroft			}
284100df344fSAndy Whitcroft		}
284200df344fSAndy Whitcroft
28434d001e4dSAndy Whitcroft# Check relative indent for conditionals and blocks.
28440fe3dc2bSJoe Perches		if ($line =~ /\b(?:(?:if|while|for|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
28453e469cdcSAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
28463e469cdcSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0)
28473e469cdcSAndy Whitcroft					if (!defined $stat);
28484d001e4dSAndy Whitcroft			my ($s, $c) = ($stat, $cond);
28494d001e4dSAndy Whitcroft
28504d001e4dSAndy Whitcroft			substr($s, 0, length($c), '');
28514d001e4dSAndy Whitcroft
28524d001e4dSAndy Whitcroft			# Make sure we remove the line prefixes as we have
28534d001e4dSAndy Whitcroft			# none on the first line, and are going to readd them
28544d001e4dSAndy Whitcroft			# where necessary.
28554d001e4dSAndy Whitcroft			$s =~ s/\n./\n/gs;
28564d001e4dSAndy Whitcroft
28574d001e4dSAndy Whitcroft			# Find out how long the conditional actually is.
28586f779c18SAndy Whitcroft			my @newlines = ($c =~ /\n/gs);
28596f779c18SAndy Whitcroft			my $cond_lines = 1 + $#newlines;
28604d001e4dSAndy Whitcroft
28614d001e4dSAndy Whitcroft			# We want to check the first line inside the block
28624d001e4dSAndy Whitcroft			# starting at the end of the conditional, so remove:
28634d001e4dSAndy Whitcroft			#  1) any blank line termination
28644d001e4dSAndy Whitcroft			#  2) any opening brace { on end of the line
28654d001e4dSAndy Whitcroft			#  3) any do (...) {
28664d001e4dSAndy Whitcroft			my $continuation = 0;
28674d001e4dSAndy Whitcroft			my $check = 0;
28684d001e4dSAndy Whitcroft			$s =~ s/^.*\bdo\b//;
28694d001e4dSAndy Whitcroft			$s =~ s/^\s*{//;
28704d001e4dSAndy Whitcroft			if ($s =~ s/^\s*\\//) {
28714d001e4dSAndy Whitcroft				$continuation = 1;
28724d001e4dSAndy Whitcroft			}
28739bd49efeSAndy Whitcroft			if ($s =~ s/^\s*?\n//) {
28744d001e4dSAndy Whitcroft				$check = 1;
28754d001e4dSAndy Whitcroft				$cond_lines++;
28764d001e4dSAndy Whitcroft			}
28774d001e4dSAndy Whitcroft
28784d001e4dSAndy Whitcroft			# Also ignore a loop construct at the end of a
28794d001e4dSAndy Whitcroft			# preprocessor statement.
28804d001e4dSAndy Whitcroft			if (($prevline =~ /^.\s*#\s*define\s/ ||
28814d001e4dSAndy Whitcroft			    $prevline =~ /\\\s*$/) && $continuation == 0) {
28824d001e4dSAndy Whitcroft				$check = 0;
28834d001e4dSAndy Whitcroft			}
28844d001e4dSAndy Whitcroft
28859bd49efeSAndy Whitcroft			my $cond_ptr = -1;
2886740504c6SAndy Whitcroft			$continuation = 0;
28879bd49efeSAndy Whitcroft			while ($cond_ptr != $cond_lines) {
28889bd49efeSAndy Whitcroft				$cond_ptr = $cond_lines;
28894d001e4dSAndy Whitcroft
2890f16fa28fSAndy Whitcroft				# If we see an #else/#elif then the code
2891f16fa28fSAndy Whitcroft				# is not linear.
2892f16fa28fSAndy Whitcroft				if ($s =~ /^\s*\#\s*(?:else|elif)/) {
2893f16fa28fSAndy Whitcroft					$check = 0;
2894f16fa28fSAndy Whitcroft				}
2895f16fa28fSAndy Whitcroft
28969bd49efeSAndy Whitcroft				# Ignore:
28979bd49efeSAndy Whitcroft				#  1) blank lines, they should be at 0,
28989bd49efeSAndy Whitcroft				#  2) preprocessor lines, and
28999bd49efeSAndy Whitcroft				#  3) labels.
2900740504c6SAndy Whitcroft				if ($continuation ||
2901740504c6SAndy Whitcroft				    $s =~ /^\s*?\n/ ||
29029bd49efeSAndy Whitcroft				    $s =~ /^\s*#\s*?/ ||
29039bd49efeSAndy Whitcroft				    $s =~ /^\s*$Ident\s*:/) {
2904740504c6SAndy Whitcroft					$continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
290530dad6ebSAndy Whitcroft					if ($s =~ s/^.*?\n//) {
29069bd49efeSAndy Whitcroft						$cond_lines++;
29079bd49efeSAndy Whitcroft					}
29084d001e4dSAndy Whitcroft				}
290930dad6ebSAndy Whitcroft			}
29104d001e4dSAndy Whitcroft
29114d001e4dSAndy Whitcroft			my (undef, $sindent) = line_stats("+" . $s);
29124d001e4dSAndy Whitcroft			my $stat_real = raw_line($linenr, $cond_lines);
29134d001e4dSAndy Whitcroft
29144d001e4dSAndy Whitcroft			# Check if either of these lines are modified, else
29154d001e4dSAndy Whitcroft			# this is not this patch's fault.
29164d001e4dSAndy Whitcroft			if (!defined($stat_real) ||
29174d001e4dSAndy Whitcroft			    $stat !~ /^\+/ && $stat_real !~ /^\+/) {
29184d001e4dSAndy Whitcroft				$check = 0;
29194d001e4dSAndy Whitcroft			}
29204d001e4dSAndy Whitcroft			if (defined($stat_real) && $cond_lines > 1) {
29214d001e4dSAndy Whitcroft				$stat_real = "[...]\n$stat_real";
29224d001e4dSAndy Whitcroft			}
29234d001e4dSAndy Whitcroft
29249bd49efeSAndy 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";
29254d001e4dSAndy Whitcroft
29264d001e4dSAndy Whitcroft			if ($check && (($sindent % 8) != 0 ||
29274d001e4dSAndy Whitcroft			    ($sindent <= $indent && $s ne ''))) {
2928000d1cc1SJoe Perches				WARN("SUSPECT_CODE_INDENT",
2929000d1cc1SJoe Perches				     "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
29304d001e4dSAndy Whitcroft			}
29314d001e4dSAndy Whitcroft		}
29324d001e4dSAndy Whitcroft
29336c72ffaaSAndy Whitcroft		# Track the 'values' across context and added lines.
29346c72ffaaSAndy Whitcroft		my $opline = $line; $opline =~ s/^./ /;
29351f65f947SAndy Whitcroft		my ($curr_values, $curr_vars) =
29361f65f947SAndy Whitcroft				annotate_values($opline . "\n", $prev_values);
29376c72ffaaSAndy Whitcroft		$curr_values = $prev_values . $curr_values;
2938c2fdda0dSAndy Whitcroft		if ($dbg_values) {
2939c2fdda0dSAndy Whitcroft			my $outline = $opline; $outline =~ s/\t/ /g;
2940cf655043SAndy Whitcroft			print "$linenr > .$outline\n";
2941cf655043SAndy Whitcroft			print "$linenr > $curr_values\n";
29421f65f947SAndy Whitcroft			print "$linenr >  $curr_vars\n";
2943c2fdda0dSAndy Whitcroft		}
29446c72ffaaSAndy Whitcroft		$prev_values = substr($curr_values, -1);
29456c72ffaaSAndy Whitcroft
294600df344fSAndy Whitcroft#ignore lines not being added
29473705ce5bSJoe Perches		next if ($line =~ /^[^\+]/);
294800df344fSAndy Whitcroft
2949653d4876SAndy Whitcroft# TEST: allow direct testing of the type matcher.
29507429c690SAndy Whitcroft		if ($dbg_type) {
29517429c690SAndy Whitcroft			if ($line =~ /^.\s*$Declare\s*$/) {
2952000d1cc1SJoe Perches				ERROR("TEST_TYPE",
2953000d1cc1SJoe Perches				      "TEST: is type\n" . $herecurr);
29547429c690SAndy Whitcroft			} elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
2955000d1cc1SJoe Perches				ERROR("TEST_NOT_TYPE",
2956000d1cc1SJoe Perches				      "TEST: is not type ($1 is)\n". $herecurr);
29577429c690SAndy Whitcroft			}
2958653d4876SAndy Whitcroft			next;
2959653d4876SAndy Whitcroft		}
2960a1ef277eSAndy Whitcroft# TEST: allow direct testing of the attribute matcher.
2961a1ef277eSAndy Whitcroft		if ($dbg_attr) {
29629360b0e5SAndy Whitcroft			if ($line =~ /^.\s*$Modifier\s*$/) {
2963000d1cc1SJoe Perches				ERROR("TEST_ATTR",
2964000d1cc1SJoe Perches				      "TEST: is attr\n" . $herecurr);
29659360b0e5SAndy Whitcroft			} elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
2966000d1cc1SJoe Perches				ERROR("TEST_NOT_ATTR",
2967000d1cc1SJoe Perches				      "TEST: is not attr ($1 is)\n". $herecurr);
2968a1ef277eSAndy Whitcroft			}
2969a1ef277eSAndy Whitcroft			next;
2970a1ef277eSAndy Whitcroft		}
2971653d4876SAndy Whitcroft
2972f0a594c1SAndy Whitcroft# check for initialisation to aggregates open brace on the next line
297399423c20SAndy Whitcroft		if ($line =~ /^.\s*{/ &&
297499423c20SAndy Whitcroft		    $prevline =~ /(?:^|[^=])=\s*$/) {
2975d752fcc8SJoe Perches			if (ERROR("OPEN_BRACE",
2976d752fcc8SJoe Perches				  "that open brace { should be on the previous line\n" . $hereprev) &&
2977f2d7e4d4SJoe Perches			    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
2978f2d7e4d4SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
2979f2d7e4d4SJoe Perches				fix_delete_line($fixlinenr, $rawline);
2980d752fcc8SJoe Perches				my $fixedline = $prevrawline;
2981d752fcc8SJoe Perches				$fixedline =~ s/\s*=\s*$/ = {/;
2982f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
2983d752fcc8SJoe Perches				$fixedline = $line;
2984d752fcc8SJoe Perches				$fixedline =~ s/^(.\s*){\s*/$1/;
2985f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
2986d752fcc8SJoe Perches			}
2987f0a594c1SAndy Whitcroft		}
2988f0a594c1SAndy Whitcroft
298900df344fSAndy Whitcroft#
299000df344fSAndy Whitcroft# Checks which are anchored on the added line.
299100df344fSAndy Whitcroft#
299200df344fSAndy Whitcroft
2993653d4876SAndy Whitcroft# check for malformed paths in #include statements (uses RAW line)
2994c45dcabdSAndy Whitcroft		if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
2995653d4876SAndy Whitcroft			my $path = $1;
2996653d4876SAndy Whitcroft			if ($path =~ m{//}) {
2997000d1cc1SJoe Perches				ERROR("MALFORMED_INCLUDE",
2998495e9d84SJoe Perches				      "malformed #include filename\n" . $herecurr);
2999495e9d84SJoe Perches			}
3000495e9d84SJoe Perches			if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
3001495e9d84SJoe Perches				ERROR("UAPI_INCLUDE",
3002495e9d84SJoe Perches				      "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
3003653d4876SAndy Whitcroft			}
3004653d4876SAndy Whitcroft		}
3005653d4876SAndy Whitcroft
300600df344fSAndy Whitcroft# no C99 // comments
300700df344fSAndy Whitcroft		if ($line =~ m{//}) {
30083705ce5bSJoe Perches			if (ERROR("C99_COMMENTS",
30093705ce5bSJoe Perches				  "do not use C99 // comments\n" . $herecurr) &&
30103705ce5bSJoe Perches			    $fix) {
3011194f66fcSJoe Perches				my $line = $fixed[$fixlinenr];
30123705ce5bSJoe Perches				if ($line =~ /\/\/(.*)$/) {
30133705ce5bSJoe Perches					my $comment = trim($1);
3014194f66fcSJoe Perches					$fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@;
30153705ce5bSJoe Perches				}
30163705ce5bSJoe Perches			}
301700df344fSAndy Whitcroft		}
301800df344fSAndy Whitcroft		# Remove C99 comments.
30190a920b5bSAndy Whitcroft		$line =~ s@//.*@@;
30206c72ffaaSAndy Whitcroft		$opline =~ s@//.*@@;
30210a920b5bSAndy Whitcroft
30222b474a1aSAndy Whitcroft# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
30232b474a1aSAndy Whitcroft# the whole statement.
30242b474a1aSAndy Whitcroft#print "APW <$lines[$realline_next - 1]>\n";
30252b474a1aSAndy Whitcroft		if (defined $realline_next &&
30262b474a1aSAndy Whitcroft		    exists $lines[$realline_next - 1] &&
30272b474a1aSAndy Whitcroft		    !defined $suppress_export{$realline_next} &&
30282b474a1aSAndy Whitcroft		    ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
30292b474a1aSAndy Whitcroft		     $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
30303cbf62dfSAndy Whitcroft			# Handle definitions which produce identifiers with
30313cbf62dfSAndy Whitcroft			# a prefix:
30323cbf62dfSAndy Whitcroft			#   XXX(foo);
30333cbf62dfSAndy Whitcroft			#   EXPORT_SYMBOL(something_foo);
3034653d4876SAndy Whitcroft			my $name = $1;
303587a53877SAndy Whitcroft			if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
30363cbf62dfSAndy Whitcroft			    $name =~ /^${Ident}_$2/) {
30373cbf62dfSAndy Whitcroft#print "FOO C name<$name>\n";
30383cbf62dfSAndy Whitcroft				$suppress_export{$realline_next} = 1;
30393cbf62dfSAndy Whitcroft
30403cbf62dfSAndy Whitcroft			} elsif ($stat !~ /(?:
30412b474a1aSAndy Whitcroft				\n.}\s*$|
304248012058SAndy Whitcroft				^.DEFINE_$Ident\(\Q$name\E\)|
304348012058SAndy Whitcroft				^.DECLARE_$Ident\(\Q$name\E\)|
304448012058SAndy Whitcroft				^.LIST_HEAD\(\Q$name\E\)|
30452b474a1aSAndy Whitcroft				^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
30462b474a1aSAndy Whitcroft				\b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
304748012058SAndy Whitcroft			    )/x) {
30482b474a1aSAndy Whitcroft#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
30492b474a1aSAndy Whitcroft				$suppress_export{$realline_next} = 2;
30502b474a1aSAndy Whitcroft			} else {
30512b474a1aSAndy Whitcroft				$suppress_export{$realline_next} = 1;
30520a920b5bSAndy Whitcroft			}
30530a920b5bSAndy Whitcroft		}
30542b474a1aSAndy Whitcroft		if (!defined $suppress_export{$linenr} &&
30552b474a1aSAndy Whitcroft		    $prevline =~ /^.\s*$/ &&
30562b474a1aSAndy Whitcroft		    ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
30572b474a1aSAndy Whitcroft		     $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
30582b474a1aSAndy Whitcroft#print "FOO B <$lines[$linenr - 1]>\n";
30592b474a1aSAndy Whitcroft			$suppress_export{$linenr} = 2;
30602b474a1aSAndy Whitcroft		}
30612b474a1aSAndy Whitcroft		if (defined $suppress_export{$linenr} &&
30622b474a1aSAndy Whitcroft		    $suppress_export{$linenr} == 2) {
3063000d1cc1SJoe Perches			WARN("EXPORT_SYMBOL",
3064000d1cc1SJoe Perches			     "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
30652b474a1aSAndy Whitcroft		}
30660a920b5bSAndy Whitcroft
30675150bda4SJoe Eloff# check for global initialisers.
3068d5e616fcSJoe Perches		if ($line =~ /^\+(\s*$Type\s*$Ident\s*(?:\s+$Modifier))*\s*=\s*(0|NULL|false)\s*;/) {
3069d5e616fcSJoe Perches			if (ERROR("GLOBAL_INITIALISERS",
3070000d1cc1SJoe Perches				  "do not initialise globals to 0 or NULL\n" .
3071d5e616fcSJoe Perches				      $herecurr) &&
3072d5e616fcSJoe Perches			    $fix) {
3073194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/($Type\s*$Ident\s*(?:\s+$Modifier))*\s*=\s*(0|NULL|false)\s*;/$1;/;
3074d5e616fcSJoe Perches			}
3075f0a594c1SAndy Whitcroft		}
30760a920b5bSAndy Whitcroft# check for static initialisers.
3077d5e616fcSJoe Perches		if ($line =~ /^\+.*\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
3078d5e616fcSJoe Perches			if (ERROR("INITIALISED_STATIC",
3079000d1cc1SJoe Perches				  "do not initialise statics to 0 or NULL\n" .
3080d5e616fcSJoe Perches				      $herecurr) &&
3081d5e616fcSJoe Perches			    $fix) {
3082194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*(0|NULL|false)\s*;/$1;/;
3083d5e616fcSJoe Perches			}
30840a920b5bSAndy Whitcroft		}
30850a920b5bSAndy Whitcroft
30861813087dSJoe Perches# check for misordered declarations of char/short/int/long with signed/unsigned
30871813087dSJoe Perches		while ($sline =~ m{(\b$TypeMisordered\b)}g) {
30881813087dSJoe Perches			my $tmp = trim($1);
30891813087dSJoe Perches			WARN("MISORDERED_TYPE",
30901813087dSJoe Perches			     "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr);
30911813087dSJoe Perches		}
30921813087dSJoe Perches
3093cb710ecaSJoe Perches# check for static const char * arrays.
3094cb710ecaSJoe Perches		if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
3095000d1cc1SJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
3096000d1cc1SJoe Perches			     "static const char * array should probably be static const char * const\n" .
3097cb710ecaSJoe Perches				$herecurr);
3098cb710ecaSJoe Perches               }
3099cb710ecaSJoe Perches
3100cb710ecaSJoe Perches# check for static char foo[] = "bar" declarations.
3101cb710ecaSJoe Perches		if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
3102000d1cc1SJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
3103000d1cc1SJoe Perches			     "static char array declaration should probably be static const char\n" .
3104cb710ecaSJoe Perches				$herecurr);
3105cb710ecaSJoe Perches               }
3106cb710ecaSJoe Perches
31079b0fa60dSJoe Perches# check for non-global char *foo[] = {"bar", ...} declarations.
31089b0fa60dSJoe Perches		if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) {
31099b0fa60dSJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
31109b0fa60dSJoe Perches			     "char * array declaration might be better as static const\n" .
31119b0fa60dSJoe Perches				$herecurr);
31129b0fa60dSJoe Perches               }
31139b0fa60dSJoe Perches
3114b36190c5SJoe Perches# check for function declarations without arguments like "int foo()"
3115b36190c5SJoe Perches		if ($line =~ /(\b$Type\s+$Ident)\s*\(\s*\)/) {
3116b36190c5SJoe Perches			if (ERROR("FUNCTION_WITHOUT_ARGS",
3117b36190c5SJoe Perches				  "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) &&
3118b36190c5SJoe Perches			    $fix) {
3119194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/;
3120b36190c5SJoe Perches			}
3121b36190c5SJoe Perches		}
3122b36190c5SJoe Perches
312392e112fdSJoe Perches# check for uses of DEFINE_PCI_DEVICE_TABLE
312492e112fdSJoe Perches		if ($line =~ /\bDEFINE_PCI_DEVICE_TABLE\s*\(\s*(\w+)\s*\)\s*=/) {
312592e112fdSJoe Perches			if (WARN("DEFINE_PCI_DEVICE_TABLE",
312692e112fdSJoe Perches				 "Prefer struct pci_device_id over deprecated DEFINE_PCI_DEVICE_TABLE\n" . $herecurr) &&
312792e112fdSJoe Perches			    $fix) {
3128194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b(?:static\s+|)DEFINE_PCI_DEVICE_TABLE\s*\(\s*(\w+)\s*\)\s*=\s*/static const struct pci_device_id $1\[\] = /;
312992e112fdSJoe Perches			}
313093ed0e2dSJoe Perches		}
313193ed0e2dSJoe Perches
3132653d4876SAndy Whitcroft# check for new typedefs, only function parameters and sparse annotations
3133653d4876SAndy Whitcroft# make sense.
3134653d4876SAndy Whitcroft		if ($line =~ /\btypedef\s/ &&
31358054576dSAndy Whitcroft		    $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
3136c45dcabdSAndy Whitcroft		    $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
31378ed22cadSAndy Whitcroft		    $line !~ /\b$typeTypedefs\b/ &&
3138653d4876SAndy Whitcroft		    $line !~ /\b__bitwise(?:__|)\b/) {
3139000d1cc1SJoe Perches			WARN("NEW_TYPEDEFS",
3140000d1cc1SJoe Perches			     "do not add new typedefs\n" . $herecurr);
31410a920b5bSAndy Whitcroft		}
31420a920b5bSAndy Whitcroft
31430a920b5bSAndy Whitcroft# * goes on variable not on type
314465863862SAndy Whitcroft		# (char*[ const])
3145bfcb2cc7SAndy Whitcroft		while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
3146bfcb2cc7SAndy Whitcroft			#print "AA<$1>\n";
31473705ce5bSJoe Perches			my ($ident, $from, $to) = ($1, $2, $2);
3148d8aaf121SAndy Whitcroft
314965863862SAndy Whitcroft			# Should start with a space.
315065863862SAndy Whitcroft			$to =~ s/^(\S)/ $1/;
315165863862SAndy Whitcroft			# Should not end with a space.
315265863862SAndy Whitcroft			$to =~ s/\s+$//;
315365863862SAndy Whitcroft			# '*'s should not have spaces between.
3154f9a0b3d1SAndy Whitcroft			while ($to =~ s/\*\s+\*/\*\*/) {
315565863862SAndy Whitcroft			}
3156d8aaf121SAndy Whitcroft
31573705ce5bSJoe Perches##			print "1: from<$from> to<$to> ident<$ident>\n";
315865863862SAndy Whitcroft			if ($from ne $to) {
31593705ce5bSJoe Perches				if (ERROR("POINTER_LOCATION",
31603705ce5bSJoe Perches					  "\"(foo$from)\" should be \"(foo$to)\"\n" .  $herecurr) &&
31613705ce5bSJoe Perches				    $fix) {
31623705ce5bSJoe Perches					my $sub_from = $ident;
31633705ce5bSJoe Perches					my $sub_to = $ident;
31643705ce5bSJoe Perches					$sub_to =~ s/\Q$from\E/$to/;
3165194f66fcSJoe Perches					$fixed[$fixlinenr] =~
31663705ce5bSJoe Perches					    s@\Q$sub_from\E@$sub_to@;
31673705ce5bSJoe Perches				}
316865863862SAndy Whitcroft			}
3169bfcb2cc7SAndy Whitcroft		}
3170bfcb2cc7SAndy Whitcroft		while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
3171bfcb2cc7SAndy Whitcroft			#print "BB<$1>\n";
31723705ce5bSJoe Perches			my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
3173d8aaf121SAndy Whitcroft
317465863862SAndy Whitcroft			# Should start with a space.
317565863862SAndy Whitcroft			$to =~ s/^(\S)/ $1/;
317665863862SAndy Whitcroft			# Should not end with a space.
317765863862SAndy Whitcroft			$to =~ s/\s+$//;
317865863862SAndy Whitcroft			# '*'s should not have spaces between.
3179f9a0b3d1SAndy Whitcroft			while ($to =~ s/\*\s+\*/\*\*/) {
318065863862SAndy Whitcroft			}
318165863862SAndy Whitcroft			# Modifiers should have spaces.
318265863862SAndy Whitcroft			$to =~ s/(\b$Modifier$)/$1 /;
318365863862SAndy Whitcroft
31843705ce5bSJoe Perches##			print "2: from<$from> to<$to> ident<$ident>\n";
3185667026e7SAndy Whitcroft			if ($from ne $to && $ident !~ /^$Modifier$/) {
31863705ce5bSJoe Perches				if (ERROR("POINTER_LOCATION",
31873705ce5bSJoe Perches					  "\"foo${from}bar\" should be \"foo${to}bar\"\n" .  $herecurr) &&
31883705ce5bSJoe Perches				    $fix) {
31893705ce5bSJoe Perches
31903705ce5bSJoe Perches					my $sub_from = $match;
31913705ce5bSJoe Perches					my $sub_to = $match;
31923705ce5bSJoe Perches					$sub_to =~ s/\Q$from\E/$to/;
3193194f66fcSJoe Perches					$fixed[$fixlinenr] =~
31943705ce5bSJoe Perches					    s@\Q$sub_from\E@$sub_to@;
31953705ce5bSJoe Perches				}
319665863862SAndy Whitcroft			}
31970a920b5bSAndy Whitcroft		}
31980a920b5bSAndy Whitcroft
31990a920b5bSAndy Whitcroft# # no BUG() or BUG_ON()
32000a920b5bSAndy Whitcroft# 		if ($line =~ /\b(BUG|BUG_ON)\b/) {
32010a920b5bSAndy Whitcroft# 			print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
32020a920b5bSAndy Whitcroft# 			print "$herecurr";
32030a920b5bSAndy Whitcroft# 			$clean = 0;
32040a920b5bSAndy Whitcroft# 		}
32050a920b5bSAndy Whitcroft
32068905a67cSAndy Whitcroft		if ($line =~ /\bLINUX_VERSION_CODE\b/) {
3207000d1cc1SJoe Perches			WARN("LINUX_VERSION_CODE",
3208000d1cc1SJoe Perches			     "LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
32098905a67cSAndy Whitcroft		}
32108905a67cSAndy Whitcroft
321117441227SJoe Perches# check for uses of printk_ratelimit
321217441227SJoe Perches		if ($line =~ /\bprintk_ratelimit\s*\(/) {
3213000d1cc1SJoe Perches			WARN("PRINTK_RATELIMITED",
3214000d1cc1SJoe Perches"Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
321517441227SJoe Perches		}
321617441227SJoe Perches
321700df344fSAndy Whitcroft# printk should use KERN_* levels.  Note that follow on printk's on the
321800df344fSAndy Whitcroft# same line do not need a level, so we use the current block context
321900df344fSAndy Whitcroft# to try and find and validate the current printk.  In summary the current
322025985edcSLucas De Marchi# printk includes all preceding printk's which have no newline on the end.
322100df344fSAndy Whitcroft# we assume the first bad printk is the one to report.
3222f0a594c1SAndy Whitcroft		if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
322300df344fSAndy Whitcroft			my $ok = 0;
322400df344fSAndy Whitcroft			for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
322500df344fSAndy Whitcroft				#print "CHECK<$lines[$ln - 1]\n";
322625985edcSLucas De Marchi				# we have a preceding printk if it ends
322700df344fSAndy Whitcroft				# with "\n" ignore it, else it is to blame
322800df344fSAndy Whitcroft				if ($lines[$ln - 1] =~ m{\bprintk\(}) {
322900df344fSAndy Whitcroft					if ($rawlines[$ln - 1] !~ m{\\n"}) {
323000df344fSAndy Whitcroft						$ok = 1;
323100df344fSAndy Whitcroft					}
323200df344fSAndy Whitcroft					last;
323300df344fSAndy Whitcroft				}
323400df344fSAndy Whitcroft			}
323500df344fSAndy Whitcroft			if ($ok == 0) {
3236000d1cc1SJoe Perches				WARN("PRINTK_WITHOUT_KERN_LEVEL",
3237000d1cc1SJoe Perches				     "printk() should include KERN_ facility level\n" . $herecurr);
32380a920b5bSAndy Whitcroft			}
323900df344fSAndy Whitcroft		}
32400a920b5bSAndy Whitcroft
3241243f3803SJoe Perches		if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
3242243f3803SJoe Perches			my $orig = $1;
3243243f3803SJoe Perches			my $level = lc($orig);
3244243f3803SJoe Perches			$level = "warn" if ($level eq "warning");
32458f26b837SJoe Perches			my $level2 = $level;
32468f26b837SJoe Perches			$level2 = "dbg" if ($level eq "debug");
3247243f3803SJoe Perches			WARN("PREFER_PR_LEVEL",
3248daa8b059SYogesh Chaudhari			     "Prefer [subsystem eg: netdev]_$level2([subsystem]dev, ... then dev_$level2(dev, ... then pr_$level(...  to printk(KERN_$orig ...\n" . $herecurr);
3249243f3803SJoe Perches		}
3250243f3803SJoe Perches
3251243f3803SJoe Perches		if ($line =~ /\bpr_warning\s*\(/) {
3252d5e616fcSJoe Perches			if (WARN("PREFER_PR_LEVEL",
3253d5e616fcSJoe Perches				 "Prefer pr_warn(... to pr_warning(...\n" . $herecurr) &&
3254d5e616fcSJoe Perches			    $fix) {
3255194f66fcSJoe Perches				$fixed[$fixlinenr] =~
3256d5e616fcSJoe Perches				    s/\bpr_warning\b/pr_warn/;
3257d5e616fcSJoe Perches			}
3258243f3803SJoe Perches		}
3259243f3803SJoe Perches
3260dc139313SJoe Perches		if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
3261dc139313SJoe Perches			my $orig = $1;
3262dc139313SJoe Perches			my $level = lc($orig);
3263dc139313SJoe Perches			$level = "warn" if ($level eq "warning");
3264dc139313SJoe Perches			$level = "dbg" if ($level eq "debug");
3265dc139313SJoe Perches			WARN("PREFER_DEV_LEVEL",
3266dc139313SJoe Perches			     "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
3267dc139313SJoe Perches		}
3268dc139313SJoe Perches
3269653d4876SAndy Whitcroft# function brace can't be on same line, except for #defines of do while,
3270653d4876SAndy Whitcroft# or if closed on same line
32718d182478SJoe Perches		if (($line=~/$Type\s*$Ident\(.*\).*\s*{/) and
3272c45dcabdSAndy Whitcroft		    !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) {
32738d182478SJoe Perches			if (ERROR("OPEN_BRACE",
32748d182478SJoe Perches				  "open brace '{' following function declarations go on the next line\n" . $herecurr) &&
32758d182478SJoe Perches			    $fix) {
32768d182478SJoe Perches				fix_delete_line($fixlinenr, $rawline);
32778d182478SJoe Perches				my $fixed_line = $rawline;
32788d182478SJoe Perches				$fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*){(.*)$/;
32798d182478SJoe Perches				my $line1 = $1;
32808d182478SJoe Perches				my $line2 = $2;
32818d182478SJoe Perches				fix_insert_line($fixlinenr, ltrim($line1));
32828d182478SJoe Perches				fix_insert_line($fixlinenr, "\+{");
32838d182478SJoe Perches				if ($line2 !~ /^\s*$/) {
32848d182478SJoe Perches					fix_insert_line($fixlinenr, "\+\t" . trim($line2));
32858d182478SJoe Perches				}
32868d182478SJoe Perches			}
32870a920b5bSAndy Whitcroft		}
3288653d4876SAndy Whitcroft
32898905a67cSAndy Whitcroft# open braces for enum, union and struct go on the same line.
32908905a67cSAndy Whitcroft		if ($line =~ /^.\s*{/ &&
32918905a67cSAndy Whitcroft		    $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
32928d182478SJoe Perches			if (ERROR("OPEN_BRACE",
32938d182478SJoe Perches				  "open brace '{' following $1 go on the same line\n" . $hereprev) &&
32948d182478SJoe Perches			    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
32958d182478SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
32968d182478SJoe Perches				fix_delete_line($fixlinenr, $rawline);
32978d182478SJoe Perches				my $fixedline = rtrim($prevrawline) . " {";
32988d182478SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
32998d182478SJoe Perches				$fixedline = $rawline;
33008d182478SJoe Perches				$fixedline =~ s/^(.\s*){\s*/$1\t/;
33018d182478SJoe Perches				if ($fixedline !~ /^\+\s*$/) {
33028d182478SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
33038d182478SJoe Perches				}
33048d182478SJoe Perches			}
33058905a67cSAndy Whitcroft		}
33068905a67cSAndy Whitcroft
33070c73b4ebSAndy Whitcroft# missing space after union, struct or enum definition
33083705ce5bSJoe Perches		if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
33093705ce5bSJoe Perches			if (WARN("SPACING",
33103705ce5bSJoe Perches				 "missing space after $1 definition\n" . $herecurr) &&
33113705ce5bSJoe Perches			    $fix) {
3312194f66fcSJoe Perches				$fixed[$fixlinenr] =~
33133705ce5bSJoe Perches				    s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
33143705ce5bSJoe Perches			}
33150c73b4ebSAndy Whitcroft		}
33160c73b4ebSAndy Whitcroft
331731070b5dSJoe Perches# Function pointer declarations
331831070b5dSJoe Perches# check spacing between type, funcptr, and args
331931070b5dSJoe Perches# canonical declaration is "type (*funcptr)(args...)"
332091f72e9cSJoe Perches		if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) {
332131070b5dSJoe Perches			my $declare = $1;
332231070b5dSJoe Perches			my $pre_pointer_space = $2;
332331070b5dSJoe Perches			my $post_pointer_space = $3;
332431070b5dSJoe Perches			my $funcname = $4;
332531070b5dSJoe Perches			my $post_funcname_space = $5;
332631070b5dSJoe Perches			my $pre_args_space = $6;
332731070b5dSJoe Perches
332891f72e9cSJoe Perches# the $Declare variable will capture all spaces after the type
332991f72e9cSJoe Perches# so check it for a missing trailing missing space but pointer return types
333091f72e9cSJoe Perches# don't need a space so don't warn for those.
333191f72e9cSJoe Perches			my $post_declare_space = "";
333291f72e9cSJoe Perches			if ($declare =~ /(\s+)$/) {
333391f72e9cSJoe Perches				$post_declare_space = $1;
333491f72e9cSJoe Perches				$declare = rtrim($declare);
333591f72e9cSJoe Perches			}
333691f72e9cSJoe Perches			if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) {
333731070b5dSJoe Perches				WARN("SPACING",
333831070b5dSJoe Perches				     "missing space after return type\n" . $herecurr);
333991f72e9cSJoe Perches				$post_declare_space = " ";
334031070b5dSJoe Perches			}
334131070b5dSJoe Perches
334231070b5dSJoe Perches# unnecessary space "type  (*funcptr)(args...)"
334391f72e9cSJoe Perches# This test is not currently implemented because these declarations are
334491f72e9cSJoe Perches# equivalent to
334591f72e9cSJoe Perches#	int  foo(int bar, ...)
334691f72e9cSJoe Perches# and this is form shouldn't/doesn't generate a checkpatch warning.
334791f72e9cSJoe Perches#
334891f72e9cSJoe Perches#			elsif ($declare =~ /\s{2,}$/) {
334991f72e9cSJoe Perches#				WARN("SPACING",
335091f72e9cSJoe Perches#				     "Multiple spaces after return type\n" . $herecurr);
335191f72e9cSJoe Perches#			}
335231070b5dSJoe Perches
335331070b5dSJoe Perches# unnecessary space "type ( *funcptr)(args...)"
335431070b5dSJoe Perches			if (defined $pre_pointer_space &&
335531070b5dSJoe Perches			    $pre_pointer_space =~ /^\s/) {
335631070b5dSJoe Perches				WARN("SPACING",
335731070b5dSJoe Perches				     "Unnecessary space after function pointer open parenthesis\n" . $herecurr);
335831070b5dSJoe Perches			}
335931070b5dSJoe Perches
336031070b5dSJoe Perches# unnecessary space "type (* funcptr)(args...)"
336131070b5dSJoe Perches			if (defined $post_pointer_space &&
336231070b5dSJoe Perches			    $post_pointer_space =~ /^\s/) {
336331070b5dSJoe Perches				WARN("SPACING",
336431070b5dSJoe Perches				     "Unnecessary space before function pointer name\n" . $herecurr);
336531070b5dSJoe Perches			}
336631070b5dSJoe Perches
336731070b5dSJoe Perches# unnecessary space "type (*funcptr )(args...)"
336831070b5dSJoe Perches			if (defined $post_funcname_space &&
336931070b5dSJoe Perches			    $post_funcname_space =~ /^\s/) {
337031070b5dSJoe Perches				WARN("SPACING",
337131070b5dSJoe Perches				     "Unnecessary space after function pointer name\n" . $herecurr);
337231070b5dSJoe Perches			}
337331070b5dSJoe Perches
337431070b5dSJoe Perches# unnecessary space "type (*funcptr) (args...)"
337531070b5dSJoe Perches			if (defined $pre_args_space &&
337631070b5dSJoe Perches			    $pre_args_space =~ /^\s/) {
337731070b5dSJoe Perches				WARN("SPACING",
337831070b5dSJoe Perches				     "Unnecessary space before function pointer arguments\n" . $herecurr);
337931070b5dSJoe Perches			}
338031070b5dSJoe Perches
338131070b5dSJoe Perches			if (show_type("SPACING") && $fix) {
3382194f66fcSJoe Perches				$fixed[$fixlinenr] =~
338391f72e9cSJoe Perches				    s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex;
338431070b5dSJoe Perches			}
338531070b5dSJoe Perches		}
338631070b5dSJoe Perches
33878d31cfceSAndy Whitcroft# check for spacing round square brackets; allowed:
33888d31cfceSAndy Whitcroft#  1. with a type on the left -- int [] a;
3389fe2a7dbcSAndy Whitcroft#  2. at the beginning of a line for slice initialisers -- [0...10] = 5,
3390fe2a7dbcSAndy Whitcroft#  3. inside a curly brace -- = { [0...10] = 5 }
33918d31cfceSAndy Whitcroft		while ($line =~ /(.*?\s)\[/g) {
33928d31cfceSAndy Whitcroft			my ($where, $prefix) = ($-[1], $1);
33938d31cfceSAndy Whitcroft			if ($prefix !~ /$Type\s+$/ &&
3394fe2a7dbcSAndy Whitcroft			    ($where != 0 || $prefix !~ /^.\s+$/) &&
3395daebc534SAndy Whitcroft			    $prefix !~ /[{,]\s+$/) {
33963705ce5bSJoe Perches				if (ERROR("BRACKET_SPACE",
33973705ce5bSJoe Perches					  "space prohibited before open square bracket '['\n" . $herecurr) &&
33983705ce5bSJoe Perches				    $fix) {
3399194f66fcSJoe Perches				    $fixed[$fixlinenr] =~
34003705ce5bSJoe Perches					s/^(\+.*?)\s+\[/$1\[/;
34013705ce5bSJoe Perches				}
34028d31cfceSAndy Whitcroft			}
34038d31cfceSAndy Whitcroft		}
34048d31cfceSAndy Whitcroft
3405f0a594c1SAndy Whitcroft# check for spaces between functions and their parentheses.
34066c72ffaaSAndy Whitcroft		while ($line =~ /($Ident)\s+\(/g) {
3407c2fdda0dSAndy Whitcroft			my $name = $1;
3408773647a0SAndy Whitcroft			my $ctx_before = substr($line, 0, $-[1]);
3409773647a0SAndy Whitcroft			my $ctx = "$ctx_before$name";
3410c2fdda0dSAndy Whitcroft
3411c2fdda0dSAndy Whitcroft			# Ignore those directives where spaces _are_ permitted.
3412773647a0SAndy Whitcroft			if ($name =~ /^(?:
3413773647a0SAndy Whitcroft				if|for|while|switch|return|case|
3414773647a0SAndy Whitcroft				volatile|__volatile__|
3415773647a0SAndy Whitcroft				__attribute__|format|__extension__|
3416773647a0SAndy Whitcroft				asm|__asm__)$/x)
3417773647a0SAndy Whitcroft			{
3418c2fdda0dSAndy Whitcroft			# cpp #define statements have non-optional spaces, ie
3419c2fdda0dSAndy Whitcroft			# if there is a space between the name and the open
3420c2fdda0dSAndy Whitcroft			# parenthesis it is simply not a parameter group.
3421c45dcabdSAndy Whitcroft			} elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
3422773647a0SAndy Whitcroft
3423773647a0SAndy Whitcroft			# cpp #elif statement condition may start with a (
3424c45dcabdSAndy Whitcroft			} elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
3425c2fdda0dSAndy Whitcroft
3426c2fdda0dSAndy Whitcroft			# If this whole things ends with a type its most
3427c2fdda0dSAndy Whitcroft			# likely a typedef for a function.
3428773647a0SAndy Whitcroft			} elsif ($ctx =~ /$Type$/) {
3429c2fdda0dSAndy Whitcroft
3430c2fdda0dSAndy Whitcroft			} else {
34313705ce5bSJoe Perches				if (WARN("SPACING",
34323705ce5bSJoe Perches					 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
34333705ce5bSJoe Perches					     $fix) {
3434194f66fcSJoe Perches					$fixed[$fixlinenr] =~
34353705ce5bSJoe Perches					    s/\b$name\s+\(/$name\(/;
34363705ce5bSJoe Perches				}
3437f0a594c1SAndy Whitcroft			}
34386c72ffaaSAndy Whitcroft		}
34399a4cad4eSEric Nelson
3440653d4876SAndy Whitcroft# Check operator spacing.
34410a920b5bSAndy Whitcroft		if (!($line=~/\#\s*include/)) {
34423705ce5bSJoe Perches			my $fixed_line = "";
34433705ce5bSJoe Perches			my $line_fixed = 0;
34443705ce5bSJoe Perches
34459c0ca6f9SAndy Whitcroft			my $ops = qr{
34469c0ca6f9SAndy Whitcroft				<<=|>>=|<=|>=|==|!=|
34479c0ca6f9SAndy Whitcroft				\+=|-=|\*=|\/=|%=|\^=|\|=|&=|
34489c0ca6f9SAndy Whitcroft				=>|->|<<|>>|<|>|=|!|~|
34491f65f947SAndy Whitcroft				&&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
345084731623SJoe Perches				\?:|\?|:
34519c0ca6f9SAndy Whitcroft			}x;
3452cf655043SAndy Whitcroft			my @elements = split(/($ops|;)/, $opline);
34533705ce5bSJoe Perches
34543705ce5bSJoe Perches##			print("element count: <" . $#elements . ">\n");
34553705ce5bSJoe Perches##			foreach my $el (@elements) {
34563705ce5bSJoe Perches##				print("el: <$el>\n");
34573705ce5bSJoe Perches##			}
34583705ce5bSJoe Perches
34593705ce5bSJoe Perches			my @fix_elements = ();
346000df344fSAndy Whitcroft			my $off = 0;
34616c72ffaaSAndy Whitcroft
34623705ce5bSJoe Perches			foreach my $el (@elements) {
34633705ce5bSJoe Perches				push(@fix_elements, substr($rawline, $off, length($el)));
34643705ce5bSJoe Perches				$off += length($el);
34653705ce5bSJoe Perches			}
34663705ce5bSJoe Perches
34673705ce5bSJoe Perches			$off = 0;
34683705ce5bSJoe Perches
34696c72ffaaSAndy Whitcroft			my $blank = copy_spacing($opline);
3470b34c648bSJoe Perches			my $last_after = -1;
34716c72ffaaSAndy Whitcroft
34720a920b5bSAndy Whitcroft			for (my $n = 0; $n < $#elements; $n += 2) {
34733705ce5bSJoe Perches
34743705ce5bSJoe Perches				my $good = $fix_elements[$n] . $fix_elements[$n + 1];
34753705ce5bSJoe Perches
34763705ce5bSJoe Perches##				print("n: <$n> good: <$good>\n");
34773705ce5bSJoe Perches
34784a0df2efSAndy Whitcroft				$off += length($elements[$n]);
34794a0df2efSAndy Whitcroft
348025985edcSLucas De Marchi				# Pick up the preceding and succeeding characters.
3481773647a0SAndy Whitcroft				my $ca = substr($opline, 0, $off);
3482773647a0SAndy Whitcroft				my $cc = '';
3483773647a0SAndy Whitcroft				if (length($opline) >= ($off + length($elements[$n + 1]))) {
3484773647a0SAndy Whitcroft					$cc = substr($opline, $off + length($elements[$n + 1]));
3485773647a0SAndy Whitcroft				}
3486773647a0SAndy Whitcroft				my $cb = "$ca$;$cc";
3487773647a0SAndy Whitcroft
34884a0df2efSAndy Whitcroft				my $a = '';
34894a0df2efSAndy Whitcroft				$a = 'V' if ($elements[$n] ne '');
34904a0df2efSAndy Whitcroft				$a = 'W' if ($elements[$n] =~ /\s$/);
3491cf655043SAndy Whitcroft				$a = 'C' if ($elements[$n] =~ /$;$/);
34924a0df2efSAndy Whitcroft				$a = 'B' if ($elements[$n] =~ /(\[|\()$/);
34934a0df2efSAndy Whitcroft				$a = 'O' if ($elements[$n] eq '');
3494773647a0SAndy Whitcroft				$a = 'E' if ($ca =~ /^\s*$/);
34954a0df2efSAndy Whitcroft
34960a920b5bSAndy Whitcroft				my $op = $elements[$n + 1];
34974a0df2efSAndy Whitcroft
34984a0df2efSAndy Whitcroft				my $c = '';
34990a920b5bSAndy Whitcroft				if (defined $elements[$n + 2]) {
35004a0df2efSAndy Whitcroft					$c = 'V' if ($elements[$n + 2] ne '');
35014a0df2efSAndy Whitcroft					$c = 'W' if ($elements[$n + 2] =~ /^\s/);
3502cf655043SAndy Whitcroft					$c = 'C' if ($elements[$n + 2] =~ /^$;/);
35034a0df2efSAndy Whitcroft					$c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
35044a0df2efSAndy Whitcroft					$c = 'O' if ($elements[$n + 2] eq '');
35058b1b3378SAndy Whitcroft					$c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
35064a0df2efSAndy Whitcroft				} else {
35074a0df2efSAndy Whitcroft					$c = 'E';
35080a920b5bSAndy Whitcroft				}
35090a920b5bSAndy Whitcroft
35104a0df2efSAndy Whitcroft				my $ctx = "${a}x${c}";
35114a0df2efSAndy Whitcroft
35124a0df2efSAndy Whitcroft				my $at = "(ctx:$ctx)";
35134a0df2efSAndy Whitcroft
35146c72ffaaSAndy Whitcroft				my $ptr = substr($blank, 0, $off) . "^";
3515de7d4f0eSAndy Whitcroft				my $hereptr = "$hereline$ptr\n";
35160a920b5bSAndy Whitcroft
351774048ed8SAndy Whitcroft				# Pull out the value of this operator.
35186c72ffaaSAndy Whitcroft				my $op_type = substr($curr_values, $off + 1, 1);
35190a920b5bSAndy Whitcroft
35201f65f947SAndy Whitcroft				# Get the full operator variant.
35211f65f947SAndy Whitcroft				my $opv = $op . substr($curr_vars, $off, 1);
35221f65f947SAndy Whitcroft
352313214adfSAndy Whitcroft				# Ignore operators passed as parameters.
352413214adfSAndy Whitcroft				if ($op_type ne 'V' &&
352513214adfSAndy Whitcroft				    $ca =~ /\s$/ && $cc =~ /^\s*,/) {
352613214adfSAndy Whitcroft
3527cf655043SAndy Whitcroft#				# Ignore comments
3528cf655043SAndy Whitcroft#				} elsif ($op =~ /^$;+$/) {
352913214adfSAndy Whitcroft
3530d8aaf121SAndy Whitcroft				# ; should have either the end of line or a space or \ after it
353113214adfSAndy Whitcroft				} elsif ($op eq ';') {
3532cf655043SAndy Whitcroft					if ($ctx !~ /.x[WEBC]/ &&
3533cf655043SAndy Whitcroft					    $cc !~ /^\\/ && $cc !~ /^;/) {
35343705ce5bSJoe Perches						if (ERROR("SPACING",
35353705ce5bSJoe Perches							  "space required after that '$op' $at\n" . $hereptr)) {
3536b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
35373705ce5bSJoe Perches							$line_fixed = 1;
35383705ce5bSJoe Perches						}
3539d8aaf121SAndy Whitcroft					}
3540d8aaf121SAndy Whitcroft
3541d8aaf121SAndy Whitcroft				# // is a comment
3542d8aaf121SAndy Whitcroft				} elsif ($op eq '//') {
35430a920b5bSAndy Whitcroft
3544b00e4814SJoe Perches				#   :   when part of a bitfield
3545b00e4814SJoe Perches				} elsif ($opv eq ':B') {
3546b00e4814SJoe Perches					# skip the bitfield test for now
3547b00e4814SJoe Perches
35481f65f947SAndy Whitcroft				# No spaces for:
35491f65f947SAndy Whitcroft				#   ->
3550b00e4814SJoe Perches				} elsif ($op eq '->') {
35514a0df2efSAndy Whitcroft					if ($ctx =~ /Wx.|.xW/) {
35523705ce5bSJoe Perches						if (ERROR("SPACING",
35533705ce5bSJoe Perches							  "spaces prohibited around that '$op' $at\n" . $hereptr)) {
3554b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
35553705ce5bSJoe Perches							if (defined $fix_elements[$n + 2]) {
35563705ce5bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
35573705ce5bSJoe Perches							}
3558b34c648bSJoe Perches							$line_fixed = 1;
35593705ce5bSJoe Perches						}
35600a920b5bSAndy Whitcroft					}
35610a920b5bSAndy Whitcroft
35620a920b5bSAndy Whitcroft				# , must have a space on the right.
35630a920b5bSAndy Whitcroft				} elsif ($op eq ',') {
3564cf655043SAndy Whitcroft					if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
35653705ce5bSJoe Perches						if (ERROR("SPACING",
35663705ce5bSJoe Perches							  "space required after that '$op' $at\n" . $hereptr)) {
3567b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
35683705ce5bSJoe Perches							$line_fixed = 1;
3569b34c648bSJoe Perches							$last_after = $n;
35703705ce5bSJoe Perches						}
35710a920b5bSAndy Whitcroft					}
35720a920b5bSAndy Whitcroft
35739c0ca6f9SAndy Whitcroft				# '*' as part of a type definition -- reported already.
357474048ed8SAndy Whitcroft				} elsif ($opv eq '*_') {
35759c0ca6f9SAndy Whitcroft					#warn "'*' is part of type\n";
35769c0ca6f9SAndy Whitcroft
35779c0ca6f9SAndy Whitcroft				# unary operators should have a space before and
35789c0ca6f9SAndy Whitcroft				# none after.  May be left adjacent to another
35799c0ca6f9SAndy Whitcroft				# unary operator, or a cast
35809c0ca6f9SAndy Whitcroft				} elsif ($op eq '!' || $op eq '~' ||
358174048ed8SAndy Whitcroft					 $opv eq '*U' || $opv eq '-U' ||
35820d413866SAndy Whitcroft					 $opv eq '&U' || $opv eq '&&U') {
3583cf655043SAndy Whitcroft					if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
35843705ce5bSJoe Perches						if (ERROR("SPACING",
35853705ce5bSJoe Perches							  "space required before that '$op' $at\n" . $hereptr)) {
3586b34c648bSJoe Perches							if ($n != $last_after + 2) {
3587b34c648bSJoe Perches								$good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]);
35883705ce5bSJoe Perches								$line_fixed = 1;
35893705ce5bSJoe Perches							}
35900a920b5bSAndy Whitcroft						}
3591b34c648bSJoe Perches					}
3592a3340b35SAndy Whitcroft					if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
3593171ae1a4SAndy Whitcroft						# A unary '*' may be const
3594171ae1a4SAndy Whitcroft
3595171ae1a4SAndy Whitcroft					} elsif ($ctx =~ /.xW/) {
35963705ce5bSJoe Perches						if (ERROR("SPACING",
35973705ce5bSJoe Perches							  "space prohibited after that '$op' $at\n" . $hereptr)) {
3598b34c648bSJoe Perches							$good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]);
35993705ce5bSJoe Perches							if (defined $fix_elements[$n + 2]) {
36003705ce5bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
36013705ce5bSJoe Perches							}
3602b34c648bSJoe Perches							$line_fixed = 1;
36033705ce5bSJoe Perches						}
36040a920b5bSAndy Whitcroft					}
36050a920b5bSAndy Whitcroft
36060a920b5bSAndy Whitcroft				# unary ++ and unary -- are allowed no space on one side.
36070a920b5bSAndy Whitcroft				} elsif ($op eq '++' or $op eq '--') {
3608773647a0SAndy Whitcroft					if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
36093705ce5bSJoe Perches						if (ERROR("SPACING",
36103705ce5bSJoe Perches							  "space required one side of that '$op' $at\n" . $hereptr)) {
3611b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
36123705ce5bSJoe Perches							$line_fixed = 1;
36133705ce5bSJoe Perches						}
36140a920b5bSAndy Whitcroft					}
3615773647a0SAndy Whitcroft					if ($ctx =~ /Wx[BE]/ ||
3616773647a0SAndy Whitcroft					    ($ctx =~ /Wx./ && $cc =~ /^;/)) {
36173705ce5bSJoe Perches						if (ERROR("SPACING",
36183705ce5bSJoe Perches							  "space prohibited before that '$op' $at\n" . $hereptr)) {
3619b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
36203705ce5bSJoe Perches							$line_fixed = 1;
36213705ce5bSJoe Perches						}
3622653d4876SAndy Whitcroft					}
3623773647a0SAndy Whitcroft					if ($ctx =~ /ExW/) {
36243705ce5bSJoe Perches						if (ERROR("SPACING",
36253705ce5bSJoe Perches							  "space prohibited after that '$op' $at\n" . $hereptr)) {
3626b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
36273705ce5bSJoe Perches							if (defined $fix_elements[$n + 2]) {
36283705ce5bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
3629773647a0SAndy Whitcroft							}
3630b34c648bSJoe Perches							$line_fixed = 1;
36313705ce5bSJoe Perches						}
36323705ce5bSJoe Perches					}
36330a920b5bSAndy Whitcroft
36340a920b5bSAndy Whitcroft				# << and >> may either have or not have spaces both sides
36359c0ca6f9SAndy Whitcroft				} elsif ($op eq '<<' or $op eq '>>' or
36369c0ca6f9SAndy Whitcroft					 $op eq '&' or $op eq '^' or $op eq '|' or
36379c0ca6f9SAndy Whitcroft					 $op eq '+' or $op eq '-' or
3638c2fdda0dSAndy Whitcroft					 $op eq '*' or $op eq '/' or
3639c2fdda0dSAndy Whitcroft					 $op eq '%')
36400a920b5bSAndy Whitcroft				{
3641773647a0SAndy Whitcroft					if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
36423705ce5bSJoe Perches						if (ERROR("SPACING",
36433705ce5bSJoe Perches							  "need consistent spacing around '$op' $at\n" . $hereptr)) {
3644b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
3645b34c648bSJoe Perches							if (defined $fix_elements[$n + 2]) {
3646b34c648bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
3647b34c648bSJoe Perches							}
36483705ce5bSJoe Perches							$line_fixed = 1;
36493705ce5bSJoe Perches						}
36500a920b5bSAndy Whitcroft					}
36510a920b5bSAndy Whitcroft
36521f65f947SAndy Whitcroft				# A colon needs no spaces before when it is
36531f65f947SAndy Whitcroft				# terminating a case value or a label.
36541f65f947SAndy Whitcroft				} elsif ($opv eq ':C' || $opv eq ':L') {
36551f65f947SAndy Whitcroft					if ($ctx =~ /Wx./) {
36563705ce5bSJoe Perches						if (ERROR("SPACING",
36573705ce5bSJoe Perches							  "space prohibited before that '$op' $at\n" . $hereptr)) {
3658b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
36593705ce5bSJoe Perches							$line_fixed = 1;
36603705ce5bSJoe Perches						}
36611f65f947SAndy Whitcroft					}
36621f65f947SAndy Whitcroft
36630a920b5bSAndy Whitcroft				# All the others need spaces both sides.
3664cf655043SAndy Whitcroft				} elsif ($ctx !~ /[EWC]x[CWE]/) {
36651f65f947SAndy Whitcroft					my $ok = 0;
36661f65f947SAndy Whitcroft
366722f2a2efSAndy Whitcroft					# Ignore email addresses <foo@bar>
36681f65f947SAndy Whitcroft					if (($op eq '<' &&
36691f65f947SAndy Whitcroft					     $cc =~ /^\S+\@\S+>/) ||
36701f65f947SAndy Whitcroft					    ($op eq '>' &&
36711f65f947SAndy Whitcroft					     $ca =~ /<\S+\@\S+$/))
36721f65f947SAndy Whitcroft					{
36731f65f947SAndy Whitcroft					    	$ok = 1;
36741f65f947SAndy Whitcroft					}
36751f65f947SAndy Whitcroft
367684731623SJoe Perches					# messages are ERROR, but ?: are CHK
36771f65f947SAndy Whitcroft					if ($ok == 0) {
367884731623SJoe Perches						my $msg_type = \&ERROR;
367984731623SJoe Perches						$msg_type = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
368084731623SJoe Perches
368184731623SJoe Perches						if (&{$msg_type}("SPACING",
36823705ce5bSJoe Perches								 "spaces required around that '$op' $at\n" . $hereptr)) {
3683b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
3684b34c648bSJoe Perches							if (defined $fix_elements[$n + 2]) {
3685b34c648bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
3686b34c648bSJoe Perches							}
36873705ce5bSJoe Perches							$line_fixed = 1;
36883705ce5bSJoe Perches						}
36890a920b5bSAndy Whitcroft					}
369022f2a2efSAndy Whitcroft				}
36914a0df2efSAndy Whitcroft				$off += length($elements[$n + 1]);
36923705ce5bSJoe Perches
36933705ce5bSJoe Perches##				print("n: <$n> GOOD: <$good>\n");
36943705ce5bSJoe Perches
36953705ce5bSJoe Perches				$fixed_line = $fixed_line . $good;
36960a920b5bSAndy Whitcroft			}
36973705ce5bSJoe Perches
36983705ce5bSJoe Perches			if (($#elements % 2) == 0) {
36993705ce5bSJoe Perches				$fixed_line = $fixed_line . $fix_elements[$#elements];
37003705ce5bSJoe Perches			}
37013705ce5bSJoe Perches
3702194f66fcSJoe Perches			if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) {
3703194f66fcSJoe Perches				$fixed[$fixlinenr] = $fixed_line;
37043705ce5bSJoe Perches			}
37053705ce5bSJoe Perches
37063705ce5bSJoe Perches
37070a920b5bSAndy Whitcroft		}
37080a920b5bSAndy Whitcroft
3709786b6326SJoe Perches# check for whitespace before a non-naked semicolon
3710d2e248e7SJoe Perches		if ($line =~ /^\+.*\S\s+;\s*$/) {
3711786b6326SJoe Perches			if (WARN("SPACING",
3712786b6326SJoe Perches				 "space prohibited before semicolon\n" . $herecurr) &&
3713786b6326SJoe Perches			    $fix) {
3714194f66fcSJoe Perches				1 while $fixed[$fixlinenr] =~
3715786b6326SJoe Perches				    s/^(\+.*\S)\s+;/$1;/;
3716786b6326SJoe Perches			}
3717786b6326SJoe Perches		}
3718786b6326SJoe Perches
3719f0a594c1SAndy Whitcroft# check for multiple assignments
3720f0a594c1SAndy Whitcroft		if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
3721000d1cc1SJoe Perches			CHK("MULTIPLE_ASSIGNMENTS",
3722000d1cc1SJoe Perches			    "multiple assignments should be avoided\n" . $herecurr);
3723f0a594c1SAndy Whitcroft		}
3724f0a594c1SAndy Whitcroft
372522f2a2efSAndy Whitcroft## # check for multiple declarations, allowing for a function declaration
372622f2a2efSAndy Whitcroft## # continuation.
372722f2a2efSAndy Whitcroft## 		if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
372822f2a2efSAndy Whitcroft## 		    $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
372922f2a2efSAndy Whitcroft##
373022f2a2efSAndy Whitcroft## 			# Remove any bracketed sections to ensure we do not
373122f2a2efSAndy Whitcroft## 			# falsly report the parameters of functions.
373222f2a2efSAndy Whitcroft## 			my $ln = $line;
373322f2a2efSAndy Whitcroft## 			while ($ln =~ s/\([^\(\)]*\)//g) {
373422f2a2efSAndy Whitcroft## 			}
373522f2a2efSAndy Whitcroft## 			if ($ln =~ /,/) {
3736000d1cc1SJoe Perches## 				WARN("MULTIPLE_DECLARATION",
3737000d1cc1SJoe Perches##				     "declaring multiple variables together should be avoided\n" . $herecurr);
373822f2a2efSAndy Whitcroft## 			}
373922f2a2efSAndy Whitcroft## 		}
3740f0a594c1SAndy Whitcroft
37410a920b5bSAndy Whitcroft#need space before brace following if, while, etc
374222f2a2efSAndy Whitcroft		if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
374322f2a2efSAndy Whitcroft		    $line =~ /do{/) {
37443705ce5bSJoe Perches			if (ERROR("SPACING",
37453705ce5bSJoe Perches				  "space required before the open brace '{'\n" . $herecurr) &&
37463705ce5bSJoe Perches			    $fix) {
3747194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/^(\+.*(?:do|\))){/$1 {/;
37483705ce5bSJoe Perches			}
3749de7d4f0eSAndy Whitcroft		}
3750de7d4f0eSAndy Whitcroft
3751c4a62ef9SJoe Perches## # check for blank lines before declarations
3752c4a62ef9SJoe Perches##		if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
3753c4a62ef9SJoe Perches##		    $prevrawline =~ /^.\s*$/) {
3754c4a62ef9SJoe Perches##			WARN("SPACING",
3755c4a62ef9SJoe Perches##			     "No blank lines before declarations\n" . $hereprev);
3756c4a62ef9SJoe Perches##		}
3757c4a62ef9SJoe Perches##
3758c4a62ef9SJoe Perches
3759de7d4f0eSAndy Whitcroft# closing brace should have a space following it when it has anything
3760de7d4f0eSAndy Whitcroft# on the line
3761de7d4f0eSAndy Whitcroft		if ($line =~ /}(?!(?:,|;|\)))\S/) {
3762d5e616fcSJoe Perches			if (ERROR("SPACING",
3763d5e616fcSJoe Perches				  "space required after that close brace '}'\n" . $herecurr) &&
3764d5e616fcSJoe Perches			    $fix) {
3765194f66fcSJoe Perches				$fixed[$fixlinenr] =~
3766d5e616fcSJoe Perches				    s/}((?!(?:,|;|\)))\S)/} $1/;
3767d5e616fcSJoe Perches			}
37680a920b5bSAndy Whitcroft		}
37690a920b5bSAndy Whitcroft
377022f2a2efSAndy Whitcroft# check spacing on square brackets
377122f2a2efSAndy Whitcroft		if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
37723705ce5bSJoe Perches			if (ERROR("SPACING",
37733705ce5bSJoe Perches				  "space prohibited after that open square bracket '['\n" . $herecurr) &&
37743705ce5bSJoe Perches			    $fix) {
3775194f66fcSJoe Perches				$fixed[$fixlinenr] =~
37763705ce5bSJoe Perches				    s/\[\s+/\[/;
37773705ce5bSJoe Perches			}
377822f2a2efSAndy Whitcroft		}
377922f2a2efSAndy Whitcroft		if ($line =~ /\s\]/) {
37803705ce5bSJoe Perches			if (ERROR("SPACING",
37813705ce5bSJoe Perches				  "space prohibited before that close square bracket ']'\n" . $herecurr) &&
37823705ce5bSJoe Perches			    $fix) {
3783194f66fcSJoe Perches				$fixed[$fixlinenr] =~
37843705ce5bSJoe Perches				    s/\s+\]/\]/;
37853705ce5bSJoe Perches			}
378622f2a2efSAndy Whitcroft		}
378722f2a2efSAndy Whitcroft
3788c45dcabdSAndy Whitcroft# check spacing on parentheses
37899c0ca6f9SAndy Whitcroft		if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
37909c0ca6f9SAndy Whitcroft		    $line !~ /for\s*\(\s+;/) {
37913705ce5bSJoe Perches			if (ERROR("SPACING",
37923705ce5bSJoe Perches				  "space prohibited after that open parenthesis '('\n" . $herecurr) &&
37933705ce5bSJoe Perches			    $fix) {
3794194f66fcSJoe Perches				$fixed[$fixlinenr] =~
37953705ce5bSJoe Perches				    s/\(\s+/\(/;
37963705ce5bSJoe Perches			}
379722f2a2efSAndy Whitcroft		}
379813214adfSAndy Whitcroft		if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
3799c45dcabdSAndy Whitcroft		    $line !~ /for\s*\(.*;\s+\)/ &&
3800c45dcabdSAndy Whitcroft		    $line !~ /:\s+\)/) {
38013705ce5bSJoe Perches			if (ERROR("SPACING",
38023705ce5bSJoe Perches				  "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
38033705ce5bSJoe Perches			    $fix) {
3804194f66fcSJoe Perches				$fixed[$fixlinenr] =~
38053705ce5bSJoe Perches				    s/\s+\)/\)/;
38063705ce5bSJoe Perches			}
380722f2a2efSAndy Whitcroft		}
380822f2a2efSAndy Whitcroft
3809e2826fd0SJoe Perches# check unnecessary parentheses around addressof/dereference single $Lvals
3810e2826fd0SJoe Perches# ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar
3811e2826fd0SJoe Perches
3812e2826fd0SJoe Perches		while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) {
3813e2826fd0SJoe Perches			CHK("UNNECESSARY_PARENTHESES",
3814e2826fd0SJoe Perches			    "Unnecessary parentheses around $1\n" . $herecurr);
3815e2826fd0SJoe Perches		    }
3816e2826fd0SJoe Perches
38170a920b5bSAndy Whitcroft#goto labels aren't indented, allow a single space however
38184a0df2efSAndy Whitcroft		if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
38190a920b5bSAndy Whitcroft		   !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
38203705ce5bSJoe Perches			if (WARN("INDENTED_LABEL",
38213705ce5bSJoe Perches				 "labels should not be indented\n" . $herecurr) &&
38223705ce5bSJoe Perches			    $fix) {
3823194f66fcSJoe Perches				$fixed[$fixlinenr] =~
38243705ce5bSJoe Perches				    s/^(.)\s+/$1/;
38253705ce5bSJoe Perches			}
38260a920b5bSAndy Whitcroft		}
38270a920b5bSAndy Whitcroft
38285b9553abSJoe Perches# return is not a function
3829507e5141SJoe Perches		if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {
3830c45dcabdSAndy Whitcroft			my $spacing = $1;
3831507e5141SJoe Perches			if ($^V && $^V ge 5.10.0 &&
38325b9553abSJoe Perches			    $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) {
38335b9553abSJoe Perches				my $value = $1;
38345b9553abSJoe Perches				$value = deparenthesize($value);
38355b9553abSJoe Perches				if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) {
3836000d1cc1SJoe Perches					ERROR("RETURN_PARENTHESES",
3837000d1cc1SJoe Perches					      "return is not a function, parentheses are not required\n" . $herecurr);
38385b9553abSJoe Perches				}
3839c45dcabdSAndy Whitcroft			} elsif ($spacing !~ /\s+/) {
3840000d1cc1SJoe Perches				ERROR("SPACING",
3841000d1cc1SJoe Perches				      "space required before the open parenthesis '('\n" . $herecurr);
3842c45dcabdSAndy Whitcroft			}
3843c45dcabdSAndy Whitcroft		}
3844507e5141SJoe Perches
3845b43ae21bSJoe Perches# unnecessary return in a void function
3846b43ae21bSJoe Perches# at end-of-function, with the previous line a single leading tab, then return;
3847b43ae21bSJoe Perches# and the line before that not a goto label target like "out:"
3848b43ae21bSJoe Perches		if ($sline =~ /^[ \+]}\s*$/ &&
3849b43ae21bSJoe Perches		    $prevline =~ /^\+\treturn\s*;\s*$/ &&
3850b43ae21bSJoe Perches		    $linenr >= 3 &&
3851b43ae21bSJoe Perches		    $lines[$linenr - 3] =~ /^[ +]/ &&
3852b43ae21bSJoe Perches		    $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) {
38539819cf25SJoe Perches			WARN("RETURN_VOID",
3854b43ae21bSJoe Perches			     "void function return statements are not generally useful\n" . $hereprev);
38559819cf25SJoe Perches               }
38569819cf25SJoe Perches
3857189248d8SJoe Perches# if statements using unnecessary parentheses - ie: if ((foo == bar))
3858189248d8SJoe Perches		if ($^V && $^V ge 5.10.0 &&
3859189248d8SJoe Perches		    $line =~ /\bif\s*((?:\(\s*){2,})/) {
3860189248d8SJoe Perches			my $openparens = $1;
3861189248d8SJoe Perches			my $count = $openparens =~ tr@\(@\(@;
3862189248d8SJoe Perches			my $msg = "";
3863189248d8SJoe Perches			if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) {
3864189248d8SJoe Perches				my $comp = $4;	#Not $1 because of $LvalOrFunc
3865189248d8SJoe Perches				$msg = " - maybe == should be = ?" if ($comp eq "==");
3866189248d8SJoe Perches				WARN("UNNECESSARY_PARENTHESES",
3867189248d8SJoe Perches				     "Unnecessary parentheses$msg\n" . $herecurr);
3868189248d8SJoe Perches			}
3869189248d8SJoe Perches		}
3870189248d8SJoe Perches
387153a3c448SAndy Whitcroft# Return of what appears to be an errno should normally be -'ve
387253a3c448SAndy Whitcroft		if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) {
387353a3c448SAndy Whitcroft			my $name = $1;
387453a3c448SAndy Whitcroft			if ($name ne 'EOF' && $name ne 'ERROR') {
3875000d1cc1SJoe Perches				WARN("USE_NEGATIVE_ERRNO",
3876000d1cc1SJoe Perches				     "return of an errno should typically be -ve (return -$1)\n" . $herecurr);
387753a3c448SAndy Whitcroft			}
387853a3c448SAndy Whitcroft		}
3879c45dcabdSAndy Whitcroft
38800a920b5bSAndy Whitcroft# Need a space before open parenthesis after if, while etc
38814a0df2efSAndy Whitcroft		if ($line =~ /\b(if|while|for|switch)\(/) {
38823705ce5bSJoe Perches			if (ERROR("SPACING",
38833705ce5bSJoe Perches				  "space required before the open parenthesis '('\n" . $herecurr) &&
38843705ce5bSJoe Perches			    $fix) {
3885194f66fcSJoe Perches				$fixed[$fixlinenr] =~
38863705ce5bSJoe Perches				    s/\b(if|while|for|switch)\(/$1 \(/;
38873705ce5bSJoe Perches			}
38880a920b5bSAndy Whitcroft		}
38890a920b5bSAndy Whitcroft
3890f5fe35ddSAndy Whitcroft# Check for illegal assignment in if conditional -- and check for trailing
3891f5fe35ddSAndy Whitcroft# statements after the conditional.
3892170d3a22SAndy Whitcroft		if ($line =~ /do\s*(?!{)/) {
38933e469cdcSAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
38943e469cdcSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0)
38953e469cdcSAndy Whitcroft					if (!defined $stat);
3896170d3a22SAndy Whitcroft			my ($stat_next) = ctx_statement_block($line_nr_next,
3897170d3a22SAndy Whitcroft						$remain_next, $off_next);
3898170d3a22SAndy Whitcroft			$stat_next =~ s/\n./\n /g;
3899170d3a22SAndy Whitcroft			##print "stat<$stat> stat_next<$stat_next>\n";
3900170d3a22SAndy Whitcroft
3901170d3a22SAndy Whitcroft			if ($stat_next =~ /^\s*while\b/) {
3902170d3a22SAndy Whitcroft				# If the statement carries leading newlines,
3903170d3a22SAndy Whitcroft				# then count those as offsets.
3904170d3a22SAndy Whitcroft				my ($whitespace) =
3905170d3a22SAndy Whitcroft					($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
3906170d3a22SAndy Whitcroft				my $offset =
3907170d3a22SAndy Whitcroft					statement_rawlines($whitespace) - 1;
3908170d3a22SAndy Whitcroft
3909170d3a22SAndy Whitcroft				$suppress_whiletrailers{$line_nr_next +
3910170d3a22SAndy Whitcroft								$offset} = 1;
3911170d3a22SAndy Whitcroft			}
3912170d3a22SAndy Whitcroft		}
3913170d3a22SAndy Whitcroft		if (!defined $suppress_whiletrailers{$linenr} &&
3914c11230f4SJoe Perches		    defined($stat) && defined($cond) &&
3915170d3a22SAndy Whitcroft		    $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
3916171ae1a4SAndy Whitcroft			my ($s, $c) = ($stat, $cond);
39178905a67cSAndy Whitcroft
3918b53c8e10SAndy Whitcroft			if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
3919000d1cc1SJoe Perches				ERROR("ASSIGN_IN_IF",
3920000d1cc1SJoe Perches				      "do not use assignment in if condition\n" . $herecurr);
39218905a67cSAndy Whitcroft			}
39228905a67cSAndy Whitcroft
39238905a67cSAndy Whitcroft			# Find out what is on the end of the line after the
39248905a67cSAndy Whitcroft			# conditional.
3925773647a0SAndy Whitcroft			substr($s, 0, length($c), '');
39268905a67cSAndy Whitcroft			$s =~ s/\n.*//g;
392713214adfSAndy Whitcroft			$s =~ s/$;//g; 	# Remove any comments
392853210168SAndy Whitcroft			if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
392953210168SAndy Whitcroft			    $c !~ /}\s*while\s*/)
3930773647a0SAndy Whitcroft			{
3931bb44ad39SAndy Whitcroft				# Find out how long the conditional actually is.
3932bb44ad39SAndy Whitcroft				my @newlines = ($c =~ /\n/gs);
3933bb44ad39SAndy Whitcroft				my $cond_lines = 1 + $#newlines;
393442bdf74cSHidetoshi Seto				my $stat_real = '';
3935bb44ad39SAndy Whitcroft
393642bdf74cSHidetoshi Seto				$stat_real = raw_line($linenr, $cond_lines)
393742bdf74cSHidetoshi Seto							. "\n" if ($cond_lines);
3938bb44ad39SAndy Whitcroft				if (defined($stat_real) && $cond_lines > 1) {
3939bb44ad39SAndy Whitcroft					$stat_real = "[...]\n$stat_real";
3940bb44ad39SAndy Whitcroft				}
3941bb44ad39SAndy Whitcroft
3942000d1cc1SJoe Perches				ERROR("TRAILING_STATEMENTS",
3943000d1cc1SJoe Perches				      "trailing statements should be on next line\n" . $herecurr . $stat_real);
39448905a67cSAndy Whitcroft			}
39458905a67cSAndy Whitcroft		}
39468905a67cSAndy Whitcroft
394713214adfSAndy Whitcroft# Check for bitwise tests written as boolean
394813214adfSAndy Whitcroft		if ($line =~ /
394913214adfSAndy Whitcroft			(?:
395013214adfSAndy Whitcroft				(?:\[|\(|\&\&|\|\|)
395113214adfSAndy Whitcroft				\s*0[xX][0-9]+\s*
395213214adfSAndy Whitcroft				(?:\&\&|\|\|)
395313214adfSAndy Whitcroft			|
395413214adfSAndy Whitcroft				(?:\&\&|\|\|)
395513214adfSAndy Whitcroft				\s*0[xX][0-9]+\s*
395613214adfSAndy Whitcroft				(?:\&\&|\|\||\)|\])
395713214adfSAndy Whitcroft			)/x)
395813214adfSAndy Whitcroft		{
3959000d1cc1SJoe Perches			WARN("HEXADECIMAL_BOOLEAN_TEST",
3960000d1cc1SJoe Perches			     "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
396113214adfSAndy Whitcroft		}
396213214adfSAndy Whitcroft
39638905a67cSAndy Whitcroft# if and else should not have general statements after it
396413214adfSAndy Whitcroft		if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
396513214adfSAndy Whitcroft			my $s = $1;
396613214adfSAndy Whitcroft			$s =~ s/$;//g; 	# Remove any comments
396713214adfSAndy Whitcroft			if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
3968000d1cc1SJoe Perches				ERROR("TRAILING_STATEMENTS",
3969000d1cc1SJoe Perches				      "trailing statements should be on next line\n" . $herecurr);
39700a920b5bSAndy Whitcroft			}
397113214adfSAndy Whitcroft		}
397239667782SAndy Whitcroft# if should not continue a brace
397339667782SAndy Whitcroft		if ($line =~ /}\s*if\b/) {
3974000d1cc1SJoe Perches			ERROR("TRAILING_STATEMENTS",
3975048b123fSRasmus Villemoes			      "trailing statements should be on next line (or did you mean 'else if'?)\n" .
397639667782SAndy Whitcroft				$herecurr);
397739667782SAndy Whitcroft		}
3978a1080bf8SAndy Whitcroft# case and default should not have general statements after them
3979a1080bf8SAndy Whitcroft		if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
3980a1080bf8SAndy Whitcroft		    $line !~ /\G(?:
39813fef12d6SAndy Whitcroft			(?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
3982a1080bf8SAndy Whitcroft			\s*return\s+
3983a1080bf8SAndy Whitcroft		    )/xg)
3984a1080bf8SAndy Whitcroft		{
3985000d1cc1SJoe Perches			ERROR("TRAILING_STATEMENTS",
3986000d1cc1SJoe Perches			      "trailing statements should be on next line\n" . $herecurr);
3987a1080bf8SAndy Whitcroft		}
39880a920b5bSAndy Whitcroft
39890a920b5bSAndy Whitcroft		# Check for }<nl>else {, these must be at the same
39900a920b5bSAndy Whitcroft		# indent level to be relevant to each other.
39918b8856f4SJoe Perches		if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ &&
39920a920b5bSAndy Whitcroft		    $previndent == $indent) {
39938b8856f4SJoe Perches			if (ERROR("ELSE_AFTER_BRACE",
39948b8856f4SJoe Perches				  "else should follow close brace '}'\n" . $hereprev) &&
39958b8856f4SJoe Perches			    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
39968b8856f4SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
39978b8856f4SJoe Perches				fix_delete_line($fixlinenr, $rawline);
39988b8856f4SJoe Perches				my $fixedline = $prevrawline;
39998b8856f4SJoe Perches				$fixedline =~ s/}\s*$//;
40008b8856f4SJoe Perches				if ($fixedline !~ /^\+\s*$/) {
40018b8856f4SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
40028b8856f4SJoe Perches				}
40038b8856f4SJoe Perches				$fixedline = $rawline;
40048b8856f4SJoe Perches				$fixedline =~ s/^(.\s*)else/$1} else/;
40058b8856f4SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
40068b8856f4SJoe Perches			}
40070a920b5bSAndy Whitcroft		}
40080a920b5bSAndy Whitcroft
40098b8856f4SJoe Perches		if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ &&
4010c2fdda0dSAndy Whitcroft		    $previndent == $indent) {
4011c2fdda0dSAndy Whitcroft			my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
4012c2fdda0dSAndy Whitcroft
4013c2fdda0dSAndy Whitcroft			# Find out what is on the end of the line after the
4014c2fdda0dSAndy Whitcroft			# conditional.
4015773647a0SAndy Whitcroft			substr($s, 0, length($c), '');
4016c2fdda0dSAndy Whitcroft			$s =~ s/\n.*//g;
4017c2fdda0dSAndy Whitcroft
4018c2fdda0dSAndy Whitcroft			if ($s =~ /^\s*;/) {
40198b8856f4SJoe Perches				if (ERROR("WHILE_AFTER_BRACE",
40208b8856f4SJoe Perches					  "while should follow close brace '}'\n" . $hereprev) &&
40218b8856f4SJoe Perches				    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
40228b8856f4SJoe Perches					fix_delete_line($fixlinenr - 1, $prevrawline);
40238b8856f4SJoe Perches					fix_delete_line($fixlinenr, $rawline);
40248b8856f4SJoe Perches					my $fixedline = $prevrawline;
40258b8856f4SJoe Perches					my $trailing = $rawline;
40268b8856f4SJoe Perches					$trailing =~ s/^\+//;
40278b8856f4SJoe Perches					$trailing = trim($trailing);
40288b8856f4SJoe Perches					$fixedline =~ s/}\s*$/} $trailing/;
40298b8856f4SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
40308b8856f4SJoe Perches				}
4031c2fdda0dSAndy Whitcroft			}
4032c2fdda0dSAndy Whitcroft		}
4033c2fdda0dSAndy Whitcroft
403495e2c602SJoe Perches#Specific variable tests
4035323c1260SJoe Perches		while ($line =~ m{($Constant|$Lval)}g) {
4036323c1260SJoe Perches			my $var = $1;
403795e2c602SJoe Perches
403895e2c602SJoe Perches#gcc binary extension
403995e2c602SJoe Perches			if ($var =~ /^$Binary$/) {
4040d5e616fcSJoe Perches				if (WARN("GCC_BINARY_CONSTANT",
4041d5e616fcSJoe Perches					 "Avoid gcc v4.3+ binary constant extension: <$var>\n" . $herecurr) &&
4042d5e616fcSJoe Perches				    $fix) {
4043d5e616fcSJoe Perches					my $hexval = sprintf("0x%x", oct($var));
4044194f66fcSJoe Perches					$fixed[$fixlinenr] =~
4045d5e616fcSJoe Perches					    s/\b$var\b/$hexval/;
4046d5e616fcSJoe Perches				}
404795e2c602SJoe Perches			}
404895e2c602SJoe Perches
404995e2c602SJoe Perches#CamelCase
4050807bd26cSJoe Perches			if ($var !~ /^$Constant$/ &&
4051be79794bSJoe Perches			    $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
405222735ce8SJoe Perches#Ignore Page<foo> variants
4053807bd26cSJoe Perches			    $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
405422735ce8SJoe Perches#Ignore SI style variants like nS, mV and dB (ie: max_uV, regulator_min_uA_show)
40553445686aSJoe Perches			    $var !~ /^(?:[a-z_]*?)_?[a-z][A-Z](?:_[a-z_]+)?$/) {
40567e781f67SJoe Perches				while ($var =~ m{($Ident)}g) {
40577e781f67SJoe Perches					my $word = $1;
40587e781f67SJoe Perches					next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
4059d8b07710SJoe Perches					if ($check) {
4060d8b07710SJoe Perches						seed_camelcase_includes();
4061d8b07710SJoe Perches						if (!$file && !$camelcase_file_seeded) {
4062d8b07710SJoe Perches							seed_camelcase_file($realfile);
4063d8b07710SJoe Perches							$camelcase_file_seeded = 1;
4064d8b07710SJoe Perches						}
4065d8b07710SJoe Perches					}
40667e781f67SJoe Perches					if (!defined $camelcase{$word}) {
40677e781f67SJoe Perches						$camelcase{$word} = 1;
4068be79794bSJoe Perches						CHK("CAMELCASE",
40697e781f67SJoe Perches						    "Avoid CamelCase: <$word>\n" . $herecurr);
40707e781f67SJoe Perches					}
4071323c1260SJoe Perches				}
4072323c1260SJoe Perches			}
40733445686aSJoe Perches		}
40740a920b5bSAndy Whitcroft
40750a920b5bSAndy Whitcroft#no spaces allowed after \ in define
4076d5e616fcSJoe Perches		if ($line =~ /\#\s*define.*\\\s+$/) {
4077d5e616fcSJoe Perches			if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
4078d5e616fcSJoe Perches				 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
4079d5e616fcSJoe Perches			    $fix) {
4080194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\s+$//;
4081d5e616fcSJoe Perches			}
40820a920b5bSAndy Whitcroft		}
40830a920b5bSAndy Whitcroft
4084653d4876SAndy Whitcroft#warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
4085c45dcabdSAndy Whitcroft		if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
4086e09dec48SAndy Whitcroft			my $file = "$1.h";
4087e09dec48SAndy Whitcroft			my $checkfile = "include/linux/$file";
4088e09dec48SAndy Whitcroft			if (-f "$root/$checkfile" &&
4089e09dec48SAndy Whitcroft			    $realfile ne $checkfile &&
40907840a94cSWolfram Sang			    $1 !~ /$allowed_asm_includes/)
4091c45dcabdSAndy Whitcroft			{
4092e09dec48SAndy Whitcroft				if ($realfile =~ m{^arch/}) {
4093000d1cc1SJoe Perches					CHK("ARCH_INCLUDE_LINUX",
4094000d1cc1SJoe Perches					    "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
4095e09dec48SAndy Whitcroft				} else {
4096000d1cc1SJoe Perches					WARN("INCLUDE_LINUX",
4097000d1cc1SJoe Perches					     "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
4098e09dec48SAndy Whitcroft				}
40990a920b5bSAndy Whitcroft			}
41000a920b5bSAndy Whitcroft		}
41010a920b5bSAndy Whitcroft
4102653d4876SAndy Whitcroft# multi-statement macros should be enclosed in a do while loop, grab the
4103653d4876SAndy Whitcroft# first statement and ensure its the whole macro if its not enclosed
4104cf655043SAndy Whitcroft# in a known good container
4105b8f96a31SAndy Whitcroft		if ($realfile !~ m@/vmlinux.lds.h$@ &&
4106b8f96a31SAndy Whitcroft		    $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
4107d8aaf121SAndy Whitcroft			my $ln = $linenr;
4108d8aaf121SAndy Whitcroft			my $cnt = $realcnt;
4109c45dcabdSAndy Whitcroft			my ($off, $dstat, $dcond, $rest);
4110c45dcabdSAndy Whitcroft			my $ctx = '';
411108a2843eSJoe Perches			my $has_flow_statement = 0;
411208a2843eSJoe Perches			my $has_arg_concat = 0;
4113c45dcabdSAndy Whitcroft			($dstat, $dcond, $ln, $cnt, $off) =
4114f74bd194SAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0);
4115f74bd194SAndy Whitcroft			$ctx = $dstat;
4116c45dcabdSAndy Whitcroft			#print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
4117a3bb97a7SAndy Whitcroft			#print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
4118c45dcabdSAndy Whitcroft
411908a2843eSJoe Perches			$has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/);
412008a2843eSJoe Perches			$has_arg_concat = 1 if ($ctx =~ /\#\#/);
412108a2843eSJoe Perches
4122f74bd194SAndy Whitcroft			$dstat =~ s/^.\s*\#\s*define\s+$Ident(?:\([^\)]*\))?\s*//;
4123292f1a9bSAndy Whitcroft			$dstat =~ s/$;//g;
4124c45dcabdSAndy Whitcroft			$dstat =~ s/\\\n.//g;
4125c45dcabdSAndy Whitcroft			$dstat =~ s/^\s*//s;
4126c45dcabdSAndy Whitcroft			$dstat =~ s/\s*$//s;
4127c45dcabdSAndy Whitcroft
4128c45dcabdSAndy Whitcroft			# Flatten any parentheses and braces
4129bf30d6edSAndy Whitcroft			while ($dstat =~ s/\([^\(\)]*\)/1/ ||
4130bf30d6edSAndy Whitcroft			       $dstat =~ s/\{[^\{\}]*\}/1/ ||
4131c81769fdSAndy Whitcroft			       $dstat =~ s/\[[^\[\]]*\]/1/)
4132bf30d6edSAndy Whitcroft			{
4133c45dcabdSAndy Whitcroft			}
4134c45dcabdSAndy Whitcroft
4135e45bab8eSAndy Whitcroft			# Flatten any obvious string concatentation.
4136e45bab8eSAndy Whitcroft			while ($dstat =~ s/("X*")\s*$Ident/$1/ ||
4137e45bab8eSAndy Whitcroft			       $dstat =~ s/$Ident\s*("X*")/$1/)
4138e45bab8eSAndy Whitcroft			{
4139e45bab8eSAndy Whitcroft			}
4140e45bab8eSAndy Whitcroft
4141c45dcabdSAndy Whitcroft			my $exceptions = qr{
4142c45dcabdSAndy Whitcroft				$Declare|
4143c45dcabdSAndy Whitcroft				module_param_named|
4144a0a0a7a9SKees Cook				MODULE_PARM_DESC|
4145c45dcabdSAndy Whitcroft				DECLARE_PER_CPU|
4146c45dcabdSAndy Whitcroft				DEFINE_PER_CPU|
4147383099fdSAndy Whitcroft				__typeof__\(|
414822fd2d3eSStefani Seibold				union|
414922fd2d3eSStefani Seibold				struct|
4150ea71a0a0SAndy Whitcroft				\.$Ident\s*=\s*|
4151ea71a0a0SAndy Whitcroft				^\"|\"$
4152c45dcabdSAndy Whitcroft			}x;
41535eaa20b9SAndy Whitcroft			#print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
4154f74bd194SAndy Whitcroft			if ($dstat ne '' &&
4155f74bd194SAndy Whitcroft			    $dstat !~ /^(?:$Ident|-?$Constant),$/ &&			# 10, // foo(),
4156f74bd194SAndy Whitcroft			    $dstat !~ /^(?:$Ident|-?$Constant);$/ &&			# foo();
41573cc4b1c3SJoe Perches			    $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ &&		# 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
4158356fd398SJoe Perches			    $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ &&			# character constants
4159f74bd194SAndy Whitcroft			    $dstat !~ /$exceptions/ &&
4160f74bd194SAndy Whitcroft			    $dstat !~ /^\.$Ident\s*=/ &&				# .foo =
4161e942e2c3SJoe Perches			    $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ &&		# stringification #foo
416272f115f9SAndy Whitcroft			    $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ &&	# do {...} while (...); // do {...} while (...)
4163f74bd194SAndy Whitcroft			    $dstat !~ /^for\s*$Constant$/ &&				# for (...)
4164f74bd194SAndy Whitcroft			    $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ &&	# for (...) bar()
4165f74bd194SAndy Whitcroft			    $dstat !~ /^do\s*{/ &&					# do {...
4166f95a7e6aSJoe Perches			    $dstat !~ /^\({/ &&						# ({...
4167f95a7e6aSJoe Perches			    $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
4168c45dcabdSAndy Whitcroft			{
4169f74bd194SAndy Whitcroft				$ctx =~ s/\n*$//;
4170f74bd194SAndy Whitcroft				my $herectx = $here . "\n";
4171f74bd194SAndy Whitcroft				my $cnt = statement_rawlines($ctx);
4172f74bd194SAndy Whitcroft
4173f74bd194SAndy Whitcroft				for (my $n = 0; $n < $cnt; $n++) {
4174f74bd194SAndy Whitcroft					$herectx .= raw_line($linenr, $n) . "\n";
4175c45dcabdSAndy Whitcroft				}
4176c45dcabdSAndy Whitcroft
4177f74bd194SAndy Whitcroft				if ($dstat =~ /;/) {
4178f74bd194SAndy Whitcroft					ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
4179f74bd194SAndy Whitcroft					      "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
4180f74bd194SAndy Whitcroft				} else {
4181000d1cc1SJoe Perches					ERROR("COMPLEX_MACRO",
4182388982b5SAndrew Morton					      "Macros with complex values should be enclosed in parentheses\n" . "$herectx");
4183d8aaf121SAndy Whitcroft				}
41840a920b5bSAndy Whitcroft			}
41855023d347SJoe Perches
418608a2843eSJoe Perches# check for macros with flow control, but without ## concatenation
418708a2843eSJoe Perches# ## concatenation is commonly a macro that defines a function so ignore those
418808a2843eSJoe Perches			if ($has_flow_statement && !$has_arg_concat) {
418908a2843eSJoe Perches				my $herectx = $here . "\n";
419008a2843eSJoe Perches				my $cnt = statement_rawlines($ctx);
419108a2843eSJoe Perches
419208a2843eSJoe Perches				for (my $n = 0; $n < $cnt; $n++) {
419308a2843eSJoe Perches					$herectx .= raw_line($linenr, $n) . "\n";
419408a2843eSJoe Perches				}
419508a2843eSJoe Perches				WARN("MACRO_WITH_FLOW_CONTROL",
419608a2843eSJoe Perches				     "Macros with flow control statements should be avoided\n" . "$herectx");
419708a2843eSJoe Perches			}
419808a2843eSJoe Perches
4199481eb486SJoe Perches# check for line continuations outside of #defines, preprocessor #, and asm
42005023d347SJoe Perches
42015023d347SJoe Perches		} else {
42025023d347SJoe Perches			if ($prevline !~ /^..*\\$/ &&
4203481eb486SJoe Perches			    $line !~ /^\+\s*\#.*\\$/ &&		# preprocessor
4204481eb486SJoe Perches			    $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ &&	# asm
42055023d347SJoe Perches			    $line =~ /^\+.*\\$/) {
42065023d347SJoe Perches				WARN("LINE_CONTINUATIONS",
42075023d347SJoe Perches				     "Avoid unnecessary line continuations\n" . $herecurr);
42085023d347SJoe Perches			}
4209653d4876SAndy Whitcroft		}
42100a920b5bSAndy Whitcroft
4211b13edf7fSJoe Perches# do {} while (0) macro tests:
4212b13edf7fSJoe Perches# single-statement macros do not need to be enclosed in do while (0) loop,
4213b13edf7fSJoe Perches# macro should not end with a semicolon
4214b13edf7fSJoe Perches		if ($^V && $^V ge 5.10.0 &&
4215b13edf7fSJoe Perches		    $realfile !~ m@/vmlinux.lds.h$@ &&
4216b13edf7fSJoe Perches		    $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
4217b13edf7fSJoe Perches			my $ln = $linenr;
4218b13edf7fSJoe Perches			my $cnt = $realcnt;
4219b13edf7fSJoe Perches			my ($off, $dstat, $dcond, $rest);
4220b13edf7fSJoe Perches			my $ctx = '';
4221b13edf7fSJoe Perches			($dstat, $dcond, $ln, $cnt, $off) =
4222b13edf7fSJoe Perches				ctx_statement_block($linenr, $realcnt, 0);
4223b13edf7fSJoe Perches			$ctx = $dstat;
4224b13edf7fSJoe Perches
4225b13edf7fSJoe Perches			$dstat =~ s/\\\n.//g;
4226b13edf7fSJoe Perches
4227b13edf7fSJoe Perches			if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
4228b13edf7fSJoe Perches				my $stmts = $2;
4229b13edf7fSJoe Perches				my $semis = $3;
4230b13edf7fSJoe Perches
4231b13edf7fSJoe Perches				$ctx =~ s/\n*$//;
4232b13edf7fSJoe Perches				my $cnt = statement_rawlines($ctx);
4233b13edf7fSJoe Perches				my $herectx = $here . "\n";
4234b13edf7fSJoe Perches
4235b13edf7fSJoe Perches				for (my $n = 0; $n < $cnt; $n++) {
4236b13edf7fSJoe Perches					$herectx .= raw_line($linenr, $n) . "\n";
4237b13edf7fSJoe Perches				}
4238b13edf7fSJoe Perches
4239ac8e97f8SJoe Perches				if (($stmts =~ tr/;/;/) == 1 &&
4240ac8e97f8SJoe Perches				    $stmts !~ /^\s*(if|while|for|switch)\b/) {
4241b13edf7fSJoe Perches					WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
4242b13edf7fSJoe Perches					     "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
4243b13edf7fSJoe Perches				}
4244b13edf7fSJoe Perches				if (defined $semis && $semis ne "") {
4245b13edf7fSJoe Perches					WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
4246b13edf7fSJoe Perches					     "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
4247b13edf7fSJoe Perches				}
4248f5ef95b1SJoe Perches			} elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) {
4249f5ef95b1SJoe Perches				$ctx =~ s/\n*$//;
4250f5ef95b1SJoe Perches				my $cnt = statement_rawlines($ctx);
4251f5ef95b1SJoe Perches				my $herectx = $here . "\n";
4252f5ef95b1SJoe Perches
4253f5ef95b1SJoe Perches				for (my $n = 0; $n < $cnt; $n++) {
4254f5ef95b1SJoe Perches					$herectx .= raw_line($linenr, $n) . "\n";
4255f5ef95b1SJoe Perches				}
4256f5ef95b1SJoe Perches
4257f5ef95b1SJoe Perches				WARN("TRAILING_SEMICOLON",
4258f5ef95b1SJoe Perches				     "macros should not use a trailing semicolon\n" . "$herectx");
4259b13edf7fSJoe Perches			}
4260b13edf7fSJoe Perches		}
4261b13edf7fSJoe Perches
4262080ba929SMike Frysinger# make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
4263080ba929SMike Frysinger# all assignments may have only one of the following with an assignment:
4264080ba929SMike Frysinger#	.
4265080ba929SMike Frysinger#	ALIGN(...)
4266080ba929SMike Frysinger#	VMLINUX_SYMBOL(...)
4267080ba929SMike Frysinger		if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
4268000d1cc1SJoe Perches			WARN("MISSING_VMLINUX_SYMBOL",
4269000d1cc1SJoe Perches			     "vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
4270080ba929SMike Frysinger		}
4271080ba929SMike Frysinger
4272f0a594c1SAndy Whitcroft# check for redundant bracing round if etc
427313214adfSAndy Whitcroft		if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
427413214adfSAndy Whitcroft			my ($level, $endln, @chunks) =
4275cf655043SAndy Whitcroft				ctx_statement_full($linenr, $realcnt, 1);
427613214adfSAndy Whitcroft			#print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
4277cf655043SAndy Whitcroft			#print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
4278cf655043SAndy Whitcroft			if ($#chunks > 0 && $level == 0) {
4279aad4f614SJoe Perches				my @allowed = ();
4280aad4f614SJoe Perches				my $allow = 0;
428113214adfSAndy Whitcroft				my $seen = 0;
4282773647a0SAndy Whitcroft				my $herectx = $here . "\n";
4283cf655043SAndy Whitcroft				my $ln = $linenr - 1;
428413214adfSAndy Whitcroft				for my $chunk (@chunks) {
428513214adfSAndy Whitcroft					my ($cond, $block) = @{$chunk};
428613214adfSAndy Whitcroft
4287773647a0SAndy Whitcroft					# If the condition carries leading newlines, then count those as offsets.
4288773647a0SAndy Whitcroft					my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
4289773647a0SAndy Whitcroft					my $offset = statement_rawlines($whitespace) - 1;
4290773647a0SAndy Whitcroft
4291aad4f614SJoe Perches					$allowed[$allow] = 0;
4292773647a0SAndy Whitcroft					#print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
4293773647a0SAndy Whitcroft
4294773647a0SAndy Whitcroft					# We have looked at and allowed this specific line.
4295773647a0SAndy Whitcroft					$suppress_ifbraces{$ln + $offset} = 1;
4296773647a0SAndy Whitcroft
4297773647a0SAndy Whitcroft					$herectx .= "$rawlines[$ln + $offset]\n[...]\n";
4298cf655043SAndy Whitcroft					$ln += statement_rawlines($block) - 1;
4299cf655043SAndy Whitcroft
4300773647a0SAndy Whitcroft					substr($block, 0, length($cond), '');
430113214adfSAndy Whitcroft
430213214adfSAndy Whitcroft					$seen++ if ($block =~ /^\s*{/);
430313214adfSAndy Whitcroft
4304aad4f614SJoe Perches					#print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
4305cf655043SAndy Whitcroft					if (statement_lines($cond) > 1) {
4306cf655043SAndy Whitcroft						#print "APW: ALLOWED: cond<$cond>\n";
4307aad4f614SJoe Perches						$allowed[$allow] = 1;
430813214adfSAndy Whitcroft					}
430913214adfSAndy Whitcroft					if ($block =~/\b(?:if|for|while)\b/) {
4310cf655043SAndy Whitcroft						#print "APW: ALLOWED: block<$block>\n";
4311aad4f614SJoe Perches						$allowed[$allow] = 1;
431213214adfSAndy Whitcroft					}
4313cf655043SAndy Whitcroft					if (statement_block_size($block) > 1) {
4314cf655043SAndy Whitcroft						#print "APW: ALLOWED: lines block<$block>\n";
4315aad4f614SJoe Perches						$allowed[$allow] = 1;
431613214adfSAndy Whitcroft					}
4317aad4f614SJoe Perches					$allow++;
431813214adfSAndy Whitcroft				}
4319aad4f614SJoe Perches				if ($seen) {
4320aad4f614SJoe Perches					my $sum_allowed = 0;
4321aad4f614SJoe Perches					foreach (@allowed) {
4322aad4f614SJoe Perches						$sum_allowed += $_;
4323aad4f614SJoe Perches					}
4324aad4f614SJoe Perches					if ($sum_allowed == 0) {
4325000d1cc1SJoe Perches						WARN("BRACES",
4326000d1cc1SJoe Perches						     "braces {} are not necessary for any arm of this statement\n" . $herectx);
4327aad4f614SJoe Perches					} elsif ($sum_allowed != $allow &&
4328aad4f614SJoe Perches						 $seen != $allow) {
4329aad4f614SJoe Perches						CHK("BRACES",
4330aad4f614SJoe Perches						    "braces {} should be used on all arms of this statement\n" . $herectx);
4331aad4f614SJoe Perches					}
433213214adfSAndy Whitcroft				}
433313214adfSAndy Whitcroft			}
433413214adfSAndy Whitcroft		}
4335773647a0SAndy Whitcroft		if (!defined $suppress_ifbraces{$linenr - 1} &&
433613214adfSAndy Whitcroft					$line =~ /\b(if|while|for|else)\b/) {
4337cf655043SAndy Whitcroft			my $allowed = 0;
4338f0a594c1SAndy Whitcroft
4339cf655043SAndy Whitcroft			# Check the pre-context.
4340cf655043SAndy Whitcroft			if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
4341cf655043SAndy Whitcroft				#print "APW: ALLOWED: pre<$1>\n";
4342cf655043SAndy Whitcroft				$allowed = 1;
4343f0a594c1SAndy Whitcroft			}
4344773647a0SAndy Whitcroft
4345773647a0SAndy Whitcroft			my ($level, $endln, @chunks) =
4346773647a0SAndy Whitcroft				ctx_statement_full($linenr, $realcnt, $-[0]);
4347773647a0SAndy Whitcroft
4348cf655043SAndy Whitcroft			# Check the condition.
4349cf655043SAndy Whitcroft			my ($cond, $block) = @{$chunks[0]};
4350773647a0SAndy Whitcroft			#print "CHECKING<$linenr> cond<$cond> block<$block>\n";
4351cf655043SAndy Whitcroft			if (defined $cond) {
4352773647a0SAndy Whitcroft				substr($block, 0, length($cond), '');
4353cf655043SAndy Whitcroft			}
4354cf655043SAndy Whitcroft			if (statement_lines($cond) > 1) {
4355cf655043SAndy Whitcroft				#print "APW: ALLOWED: cond<$cond>\n";
4356cf655043SAndy Whitcroft				$allowed = 1;
4357cf655043SAndy Whitcroft			}
4358cf655043SAndy Whitcroft			if ($block =~/\b(?:if|for|while)\b/) {
4359cf655043SAndy Whitcroft				#print "APW: ALLOWED: block<$block>\n";
4360cf655043SAndy Whitcroft				$allowed = 1;
4361cf655043SAndy Whitcroft			}
4362cf655043SAndy Whitcroft			if (statement_block_size($block) > 1) {
4363cf655043SAndy Whitcroft				#print "APW: ALLOWED: lines block<$block>\n";
4364cf655043SAndy Whitcroft				$allowed = 1;
4365cf655043SAndy Whitcroft			}
4366cf655043SAndy Whitcroft			# Check the post-context.
4367cf655043SAndy Whitcroft			if (defined $chunks[1]) {
4368cf655043SAndy Whitcroft				my ($cond, $block) = @{$chunks[1]};
4369cf655043SAndy Whitcroft				if (defined $cond) {
4370773647a0SAndy Whitcroft					substr($block, 0, length($cond), '');
4371cf655043SAndy Whitcroft				}
4372cf655043SAndy Whitcroft				if ($block =~ /^\s*\{/) {
4373cf655043SAndy Whitcroft					#print "APW: ALLOWED: chunk-1 block<$block>\n";
4374cf655043SAndy Whitcroft					$allowed = 1;
4375cf655043SAndy Whitcroft				}
4376cf655043SAndy Whitcroft			}
4377cf655043SAndy Whitcroft			if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
437869932487SJustin P. Mattock				my $herectx = $here . "\n";
4379f055663cSAndy Whitcroft				my $cnt = statement_rawlines($block);
4380cf655043SAndy Whitcroft
4381f055663cSAndy Whitcroft				for (my $n = 0; $n < $cnt; $n++) {
438269932487SJustin P. Mattock					$herectx .= raw_line($linenr, $n) . "\n";
4383cf655043SAndy Whitcroft				}
4384cf655043SAndy Whitcroft
4385000d1cc1SJoe Perches				WARN("BRACES",
4386000d1cc1SJoe Perches				     "braces {} are not necessary for single statement blocks\n" . $herectx);
4387f0a594c1SAndy Whitcroft			}
4388f0a594c1SAndy Whitcroft		}
4389f0a594c1SAndy Whitcroft
43900979ae66SJoe Perches# check for unnecessary blank lines around braces
439177b9a53aSJoe Perches		if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
43920979ae66SJoe Perches			CHK("BRACES",
43930979ae66SJoe Perches			    "Blank lines aren't necessary before a close brace '}'\n" . $hereprev);
43940979ae66SJoe Perches		}
439577b9a53aSJoe Perches		if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
43960979ae66SJoe Perches			CHK("BRACES",
43970979ae66SJoe Perches			    "Blank lines aren't necessary after an open brace '{'\n" . $hereprev);
43980979ae66SJoe Perches		}
43990979ae66SJoe Perches
44004a0df2efSAndy Whitcroft# no volatiles please
44016c72ffaaSAndy Whitcroft		my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
44026c72ffaaSAndy Whitcroft		if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
4403000d1cc1SJoe Perches			WARN("VOLATILE",
4404000d1cc1SJoe Perches			     "Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
44054a0df2efSAndy Whitcroft		}
44064a0df2efSAndy Whitcroft
4407f17dba4fSJoe Perches# concatenated string without spaces between elements
4408f17dba4fSJoe Perches		if ($line =~ /"X+"[A-Z_]+/ || $line =~ /[A-Z_]+"X+"/) {
4409f17dba4fSJoe Perches			CHK("CONCATENATED_STRING",
4410f17dba4fSJoe Perches			    "Concatenated strings should use spaces between elements\n" . $herecurr);
4411f17dba4fSJoe Perches		}
4412f17dba4fSJoe Perches
441300df344fSAndy Whitcroft# warn about #if 0
4414c45dcabdSAndy Whitcroft		if ($line =~ /^.\s*\#\s*if\s+0\b/) {
4415000d1cc1SJoe Perches			CHK("REDUNDANT_CODE",
4416000d1cc1SJoe Perches			    "if this code is redundant consider removing it\n" .
4417de7d4f0eSAndy Whitcroft				$herecurr);
44184a0df2efSAndy Whitcroft		}
44194a0df2efSAndy Whitcroft
442003df4b51SAndy Whitcroft# check for needless "if (<foo>) fn(<foo>)" uses
442103df4b51SAndy Whitcroft		if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
442203df4b51SAndy Whitcroft			my $expr = '\s*\(\s*' . quotemeta($1) . '\s*\)\s*;';
442303df4b51SAndy Whitcroft			if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?)$expr/) {
442403df4b51SAndy Whitcroft				WARN('NEEDLESS_IF',
442503df4b51SAndy Whitcroft				     "$1(NULL) is safe this check is probably not required\n" . $hereprev);
44264c432a8fSGreg Kroah-Hartman			}
44274c432a8fSGreg Kroah-Hartman		}
4428f0a594c1SAndy Whitcroft
4429ebfdc409SJoe Perches# check for unnecessary "Out of Memory" messages
4430ebfdc409SJoe Perches		if ($line =~ /^\+.*\b$logFunctions\s*\(/ &&
4431ebfdc409SJoe Perches		    $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ &&
4432ebfdc409SJoe Perches		    (defined $1 || defined $3) &&
4433ebfdc409SJoe Perches		    $linenr > 3) {
4434ebfdc409SJoe Perches			my $testval = $2;
4435ebfdc409SJoe Perches			my $testline = $lines[$linenr - 3];
4436ebfdc409SJoe Perches
4437ebfdc409SJoe Perches			my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0);
4438ebfdc409SJoe Perches#			print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n");
4439ebfdc409SJoe Perches
4440ebfdc409SJoe 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)/) {
4441ebfdc409SJoe Perches				WARN("OOM_MESSAGE",
4442ebfdc409SJoe Perches				     "Possible unnecessary 'out of memory' message\n" . $hereprev);
4443ebfdc409SJoe Perches			}
4444ebfdc409SJoe Perches		}
4445ebfdc409SJoe Perches
44468716de38SJoe Perches# check for bad placement of section $InitAttribute (e.g.: __initdata)
44478716de38SJoe Perches		if ($line =~ /(\b$InitAttribute\b)/) {
44488716de38SJoe Perches			my $attr = $1;
44498716de38SJoe Perches			if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
44508716de38SJoe Perches				my $ptr = $1;
44518716de38SJoe Perches				my $var = $2;
44528716de38SJoe Perches				if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
44538716de38SJoe Perches				      ERROR("MISPLACED_INIT",
44548716de38SJoe Perches					    "$attr should be placed after $var\n" . $herecurr)) ||
44558716de38SJoe Perches				     ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
44568716de38SJoe Perches				      WARN("MISPLACED_INIT",
44578716de38SJoe Perches					   "$attr should be placed after $var\n" . $herecurr))) &&
44588716de38SJoe Perches				    $fix) {
4459194f66fcSJoe 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;
44608716de38SJoe Perches				}
44618716de38SJoe Perches			}
44628716de38SJoe Perches		}
44638716de38SJoe Perches
4464e970b884SJoe Perches# check for $InitAttributeData (ie: __initdata) with const
4465e970b884SJoe Perches		if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
4466e970b884SJoe Perches			my $attr = $1;
4467e970b884SJoe Perches			$attr =~ /($InitAttributePrefix)(.*)/;
4468e970b884SJoe Perches			my $attr_prefix = $1;
4469e970b884SJoe Perches			my $attr_type = $2;
4470e970b884SJoe Perches			if (ERROR("INIT_ATTRIBUTE",
4471e970b884SJoe Perches				  "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
4472e970b884SJoe Perches			    $fix) {
4473194f66fcSJoe Perches				$fixed[$fixlinenr] =~
4474e970b884SJoe Perches				    s/$InitAttributeData/${attr_prefix}initconst/;
4475e970b884SJoe Perches			}
4476e970b884SJoe Perches		}
4477e970b884SJoe Perches
4478e970b884SJoe Perches# check for $InitAttributeConst (ie: __initconst) without const
4479e970b884SJoe Perches		if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
4480e970b884SJoe Perches			my $attr = $1;
4481e970b884SJoe Perches			if (ERROR("INIT_ATTRIBUTE",
4482e970b884SJoe Perches				  "Use of $attr requires a separate use of const\n" . $herecurr) &&
4483e970b884SJoe Perches			    $fix) {
4484194f66fcSJoe Perches				my $lead = $fixed[$fixlinenr] =~
4485e970b884SJoe Perches				    /(^\+\s*(?:static\s+))/;
4486e970b884SJoe Perches				$lead = rtrim($1);
4487e970b884SJoe Perches				$lead = "$lead " if ($lead !~ /^\+$/);
4488e970b884SJoe Perches				$lead = "${lead}const ";
4489194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/;
4490e970b884SJoe Perches			}
4491e970b884SJoe Perches		}
4492e970b884SJoe Perches
4493fbdb8138SJoe Perches# don't use __constant_<foo> functions outside of include/uapi/
4494fbdb8138SJoe Perches		if ($realfile !~ m@^include/uapi/@ &&
4495fbdb8138SJoe Perches		    $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) {
4496fbdb8138SJoe Perches			my $constant_func = $1;
4497fbdb8138SJoe Perches			my $func = $constant_func;
4498fbdb8138SJoe Perches			$func =~ s/^__constant_//;
4499fbdb8138SJoe Perches			if (WARN("CONSTANT_CONVERSION",
4500fbdb8138SJoe Perches				 "$constant_func should be $func\n" . $herecurr) &&
4501fbdb8138SJoe Perches			    $fix) {
4502194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g;
4503fbdb8138SJoe Perches			}
4504fbdb8138SJoe Perches		}
4505fbdb8138SJoe Perches
45061a15a250SPatrick Pannuto# prefer usleep_range over udelay
450737581c28SBruce Allan		if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
450843c1d77cSJoe Perches			my $delay = $1;
45091a15a250SPatrick Pannuto			# ignore udelay's < 10, however
451043c1d77cSJoe Perches			if (! ($delay < 10) ) {
4511000d1cc1SJoe Perches				CHK("USLEEP_RANGE",
451243c1d77cSJoe Perches				    "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $herecurr);
451343c1d77cSJoe Perches			}
451443c1d77cSJoe Perches			if ($delay > 2000) {
451543c1d77cSJoe Perches				WARN("LONG_UDELAY",
451643c1d77cSJoe Perches				     "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr);
45171a15a250SPatrick Pannuto			}
45181a15a250SPatrick Pannuto		}
45191a15a250SPatrick Pannuto
452009ef8725SPatrick Pannuto# warn about unexpectedly long msleep's
452109ef8725SPatrick Pannuto		if ($line =~ /\bmsleep\s*\((\d+)\);/) {
452209ef8725SPatrick Pannuto			if ($1 < 20) {
4523000d1cc1SJoe Perches				WARN("MSLEEP",
452443c1d77cSJoe Perches				     "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $herecurr);
452509ef8725SPatrick Pannuto			}
452609ef8725SPatrick Pannuto		}
452709ef8725SPatrick Pannuto
452836ec1939SJoe Perches# check for comparisons of jiffies
452936ec1939SJoe Perches		if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
453036ec1939SJoe Perches			WARN("JIFFIES_COMPARISON",
453136ec1939SJoe Perches			     "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
453236ec1939SJoe Perches		}
453336ec1939SJoe Perches
45349d7a34a5SJoe Perches# check for comparisons of get_jiffies_64()
45359d7a34a5SJoe Perches		if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
45369d7a34a5SJoe Perches			WARN("JIFFIES_COMPARISON",
45379d7a34a5SJoe Perches			     "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
45389d7a34a5SJoe Perches		}
45399d7a34a5SJoe Perches
454000df344fSAndy Whitcroft# warn about #ifdefs in C files
4541c45dcabdSAndy Whitcroft#		if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
454200df344fSAndy Whitcroft#			print "#ifdef in C files should be avoided\n";
454300df344fSAndy Whitcroft#			print "$herecurr";
454400df344fSAndy Whitcroft#			$clean = 0;
454500df344fSAndy Whitcroft#		}
454600df344fSAndy Whitcroft
454722f2a2efSAndy Whitcroft# warn about spacing in #ifdefs
4548c45dcabdSAndy Whitcroft		if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
45493705ce5bSJoe Perches			if (ERROR("SPACING",
45503705ce5bSJoe Perches				  "exactly one space required after that #$1\n" . $herecurr) &&
45513705ce5bSJoe Perches			    $fix) {
4552194f66fcSJoe Perches				$fixed[$fixlinenr] =~
45533705ce5bSJoe Perches				    s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
45543705ce5bSJoe Perches			}
45553705ce5bSJoe Perches
455622f2a2efSAndy Whitcroft		}
455722f2a2efSAndy Whitcroft
45584a0df2efSAndy Whitcroft# check for spinlock_t definitions without a comment.
4559171ae1a4SAndy Whitcroft		if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
4560171ae1a4SAndy Whitcroft		    $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
45614a0df2efSAndy Whitcroft			my $which = $1;
45624a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
4563000d1cc1SJoe Perches				CHK("UNCOMMENTED_DEFINITION",
4564000d1cc1SJoe Perches				    "$1 definition without comment\n" . $herecurr);
45654a0df2efSAndy Whitcroft			}
45664a0df2efSAndy Whitcroft		}
45674a0df2efSAndy Whitcroft# check for memory barriers without a comment.
45684a0df2efSAndy Whitcroft		if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
45694a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
4570c1fd7bb9SJoe Perches				WARN("MEMORY_BARRIER",
4571000d1cc1SJoe Perches				     "memory barrier without comment\n" . $herecurr);
45724a0df2efSAndy Whitcroft			}
45734a0df2efSAndy Whitcroft		}
45744a0df2efSAndy Whitcroft# check of hardware specific defines
4575c45dcabdSAndy Whitcroft		if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
4576000d1cc1SJoe Perches			CHK("ARCH_DEFINES",
4577000d1cc1SJoe Perches			    "architecture specific defines should be avoided\n" .  $herecurr);
45780a920b5bSAndy Whitcroft		}
4579653d4876SAndy Whitcroft
4580d4977c78STobias Klauser# Check that the storage class is at the beginning of a declaration
4581d4977c78STobias Klauser		if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
4582000d1cc1SJoe Perches			WARN("STORAGE_CLASS",
4583000d1cc1SJoe Perches			     "storage class should be at the beginning of the declaration\n" . $herecurr)
4584d4977c78STobias Klauser		}
4585d4977c78STobias Klauser
4586de7d4f0eSAndy Whitcroft# check the location of the inline attribute, that it is between
4587de7d4f0eSAndy Whitcroft# storage class and type.
45889c0ca6f9SAndy Whitcroft		if ($line =~ /\b$Type\s+$Inline\b/ ||
45899c0ca6f9SAndy Whitcroft		    $line =~ /\b$Inline\s+$Storage\b/) {
4590000d1cc1SJoe Perches			ERROR("INLINE_LOCATION",
4591000d1cc1SJoe Perches			      "inline keyword should sit between storage class and type\n" . $herecurr);
4592de7d4f0eSAndy Whitcroft		}
4593de7d4f0eSAndy Whitcroft
45948905a67cSAndy Whitcroft# Check for __inline__ and __inline, prefer inline
45952b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
45962b7ab453SJoe Perches		    $line =~ /\b(__inline__|__inline)\b/) {
4597d5e616fcSJoe Perches			if (WARN("INLINE",
4598d5e616fcSJoe Perches				 "plain inline is preferred over $1\n" . $herecurr) &&
4599d5e616fcSJoe Perches			    $fix) {
4600194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/;
4601d5e616fcSJoe Perches
4602d5e616fcSJoe Perches			}
46038905a67cSAndy Whitcroft		}
46048905a67cSAndy Whitcroft
46053d130fd0SJoe Perches# Check for __attribute__ packed, prefer __packed
46062b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
46072b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
4608000d1cc1SJoe Perches			WARN("PREFER_PACKED",
4609000d1cc1SJoe Perches			     "__packed is preferred over __attribute__((packed))\n" . $herecurr);
46103d130fd0SJoe Perches		}
46113d130fd0SJoe Perches
461239b7e287SJoe Perches# Check for __attribute__ aligned, prefer __aligned
46132b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
46142b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
4615000d1cc1SJoe Perches			WARN("PREFER_ALIGNED",
4616000d1cc1SJoe Perches			     "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
461739b7e287SJoe Perches		}
461839b7e287SJoe Perches
46195f14d3bdSJoe Perches# Check for __attribute__ format(printf, prefer __printf
46202b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
46212b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
4622d5e616fcSJoe Perches			if (WARN("PREFER_PRINTF",
4623d5e616fcSJoe Perches				 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) &&
4624d5e616fcSJoe Perches			    $fix) {
4625194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf\s*,\s*(.*)\)\s*\)\s*\)/"__printf(" . trim($1) . ")"/ex;
4626d5e616fcSJoe Perches
4627d5e616fcSJoe Perches			}
46285f14d3bdSJoe Perches		}
46295f14d3bdSJoe Perches
46306061d949SJoe Perches# Check for __attribute__ format(scanf, prefer __scanf
46312b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
46322b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
4633d5e616fcSJoe Perches			if (WARN("PREFER_SCANF",
4634d5e616fcSJoe Perches				 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) &&
4635d5e616fcSJoe Perches			    $fix) {
4636194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\s*,\s*(.*)\)\s*\)\s*\)/"__scanf(" . trim($1) . ")"/ex;
4637d5e616fcSJoe Perches			}
46386061d949SJoe Perches		}
46396061d949SJoe Perches
46408f53a9b8SJoe Perches# check for sizeof(&)
46418f53a9b8SJoe Perches		if ($line =~ /\bsizeof\s*\(\s*\&/) {
4642000d1cc1SJoe Perches			WARN("SIZEOF_ADDRESS",
4643000d1cc1SJoe Perches			     "sizeof(& should be avoided\n" . $herecurr);
46448f53a9b8SJoe Perches		}
46458f53a9b8SJoe Perches
464666c80b60SJoe Perches# check for sizeof without parenthesis
464766c80b60SJoe Perches		if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
4648d5e616fcSJoe Perches			if (WARN("SIZEOF_PARENTHESIS",
4649d5e616fcSJoe Perches				 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
4650d5e616fcSJoe Perches			    $fix) {
4651194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
4652d5e616fcSJoe Perches			}
465366c80b60SJoe Perches		}
465466c80b60SJoe Perches
4655428e2fdcSJoe Perches# check for line continuations in quoted strings with odd counts of "
4656428e2fdcSJoe Perches		if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) {
4657000d1cc1SJoe Perches			WARN("LINE_CONTINUATIONS",
4658000d1cc1SJoe Perches			     "Avoid line continuations in quoted strings\n" . $herecurr);
4659428e2fdcSJoe Perches		}
4660428e2fdcSJoe Perches
466188982feaSJoe Perches# check for struct spinlock declarations
466288982feaSJoe Perches		if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
466388982feaSJoe Perches			WARN("USE_SPINLOCK_T",
466488982feaSJoe Perches			     "struct spinlock should be spinlock_t\n" . $herecurr);
466588982feaSJoe Perches		}
466688982feaSJoe Perches
4667a6962d72SJoe Perches# check for seq_printf uses that could be seq_puts
466806668727SJoe Perches		if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
4669a6962d72SJoe Perches			my $fmt = get_quoted_string($line, $rawline);
467006668727SJoe Perches			if ($fmt ne "" && $fmt !~ /[^\\]\%/) {
4671d5e616fcSJoe Perches				if (WARN("PREFER_SEQ_PUTS",
4672d5e616fcSJoe Perches					 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
4673d5e616fcSJoe Perches				    $fix) {
4674194f66fcSJoe Perches					$fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/;
4675d5e616fcSJoe Perches				}
4676a6962d72SJoe Perches			}
4677a6962d72SJoe Perches		}
4678a6962d72SJoe Perches
4679554e165cSAndy Whitcroft# Check for misused memsets
4680d1fe9c09SJoe Perches		if ($^V && $^V ge 5.10.0 &&
4681d1fe9c09SJoe Perches		    defined $stat &&
4682d7c76ba7SJoe Perches		    $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/s) {
4683554e165cSAndy Whitcroft
4684d7c76ba7SJoe Perches			my $ms_addr = $2;
4685d1fe9c09SJoe Perches			my $ms_val = $7;
4686d1fe9c09SJoe Perches			my $ms_size = $12;
4687d7c76ba7SJoe Perches
4688554e165cSAndy Whitcroft			if ($ms_size =~ /^(0x|)0$/i) {
4689554e165cSAndy Whitcroft				ERROR("MEMSET",
4690d7c76ba7SJoe Perches				      "memset to 0's uses 0 as the 2nd argument, not the 3rd\n" . "$here\n$stat\n");
4691554e165cSAndy Whitcroft			} elsif ($ms_size =~ /^(0x|)1$/i) {
4692554e165cSAndy Whitcroft				WARN("MEMSET",
4693d7c76ba7SJoe Perches				     "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
4694d7c76ba7SJoe Perches			}
4695d7c76ba7SJoe Perches		}
4696d7c76ba7SJoe Perches
469798a9bba5SJoe Perches# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar)
469898a9bba5SJoe Perches		if ($^V && $^V ge 5.10.0 &&
469998a9bba5SJoe Perches		    $line =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/s) {
470098a9bba5SJoe Perches			if (WARN("PREFER_ETHER_ADDR_COPY",
470198a9bba5SJoe Perches				 "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . $herecurr) &&
470298a9bba5SJoe Perches			    $fix) {
4703194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/;
470498a9bba5SJoe Perches			}
470598a9bba5SJoe Perches		}
470698a9bba5SJoe Perches
4707d7c76ba7SJoe Perches# typecasts on min/max could be min_t/max_t
4708d1fe9c09SJoe Perches		if ($^V && $^V ge 5.10.0 &&
4709d1fe9c09SJoe Perches		    defined $stat &&
4710d7c76ba7SJoe Perches		    $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
4711d1fe9c09SJoe Perches			if (defined $2 || defined $7) {
4712d7c76ba7SJoe Perches				my $call = $1;
4713d7c76ba7SJoe Perches				my $cast1 = deparenthesize($2);
4714d7c76ba7SJoe Perches				my $arg1 = $3;
4715d1fe9c09SJoe Perches				my $cast2 = deparenthesize($7);
4716d1fe9c09SJoe Perches				my $arg2 = $8;
4717d7c76ba7SJoe Perches				my $cast;
4718d7c76ba7SJoe Perches
4719d1fe9c09SJoe Perches				if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
4720d7c76ba7SJoe Perches					$cast = "$cast1 or $cast2";
4721d7c76ba7SJoe Perches				} elsif ($cast1 ne "") {
4722d7c76ba7SJoe Perches					$cast = $cast1;
4723d7c76ba7SJoe Perches				} else {
4724d7c76ba7SJoe Perches					$cast = $cast2;
4725d7c76ba7SJoe Perches				}
4726d7c76ba7SJoe Perches				WARN("MINMAX",
4727d7c76ba7SJoe Perches				     "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
4728554e165cSAndy Whitcroft			}
4729554e165cSAndy Whitcroft		}
4730554e165cSAndy Whitcroft
47314a273195SJoe Perches# check usleep_range arguments
47324a273195SJoe Perches		if ($^V && $^V ge 5.10.0 &&
47334a273195SJoe Perches		    defined $stat &&
47344a273195SJoe Perches		    $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
47354a273195SJoe Perches			my $min = $1;
47364a273195SJoe Perches			my $max = $7;
47374a273195SJoe Perches			if ($min eq $max) {
47384a273195SJoe Perches				WARN("USLEEP_RANGE",
47394a273195SJoe Perches				     "usleep_range should not use min == max args; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
47404a273195SJoe Perches			} elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
47414a273195SJoe Perches				 $min > $max) {
47424a273195SJoe Perches				WARN("USLEEP_RANGE",
47434a273195SJoe Perches				     "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
47444a273195SJoe Perches			}
47454a273195SJoe Perches		}
47464a273195SJoe Perches
4747823b794cSJoe Perches# check for naked sscanf
4748823b794cSJoe Perches		if ($^V && $^V ge 5.10.0 &&
4749823b794cSJoe Perches		    defined $stat &&
47506c8bd707SJoe Perches		    $line =~ /\bsscanf\b/ &&
4751823b794cSJoe Perches		    ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
4752823b794cSJoe Perches		     $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ &&
4753823b794cSJoe Perches		     $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) {
4754823b794cSJoe Perches			my $lc = $stat =~ tr@\n@@;
4755823b794cSJoe Perches			$lc = $lc + $linenr;
4756823b794cSJoe Perches			my $stat_real = raw_line($linenr, 0);
4757823b794cSJoe Perches		        for (my $count = $linenr + 1; $count <= $lc; $count++) {
4758823b794cSJoe Perches				$stat_real = $stat_real . "\n" . raw_line($count, 0);
4759823b794cSJoe Perches			}
4760823b794cSJoe Perches			WARN("NAKED_SSCANF",
4761823b794cSJoe Perches			     "unchecked sscanf return value\n" . "$here\n$stat_real\n");
4762823b794cSJoe Perches		}
4763823b794cSJoe Perches
4764afc819abSJoe Perches# check for simple sscanf that should be kstrto<foo>
4765afc819abSJoe Perches		if ($^V && $^V ge 5.10.0 &&
4766afc819abSJoe Perches		    defined $stat &&
4767afc819abSJoe Perches		    $line =~ /\bsscanf\b/) {
4768afc819abSJoe Perches			my $lc = $stat =~ tr@\n@@;
4769afc819abSJoe Perches			$lc = $lc + $linenr;
4770afc819abSJoe Perches			my $stat_real = raw_line($linenr, 0);
4771afc819abSJoe Perches		        for (my $count = $linenr + 1; $count <= $lc; $count++) {
4772afc819abSJoe Perches				$stat_real = $stat_real . "\n" . raw_line($count, 0);
4773afc819abSJoe Perches			}
4774afc819abSJoe Perches			if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) {
4775afc819abSJoe Perches				my $format = $6;
4776afc819abSJoe Perches				my $count = $format =~ tr@%@%@;
4777afc819abSJoe Perches				if ($count == 1 &&
4778afc819abSJoe Perches				    $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) {
4779afc819abSJoe Perches					WARN("SSCANF_TO_KSTRTO",
4780afc819abSJoe Perches					     "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n");
4781afc819abSJoe Perches				}
4782afc819abSJoe Perches			}
4783afc819abSJoe Perches		}
4784afc819abSJoe Perches
478570dc8a48SJoe Perches# check for new externs in .h files.
478670dc8a48SJoe Perches		if ($realfile =~ /\.h$/ &&
478770dc8a48SJoe Perches		    $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
4788d1d85780SJoe Perches			if (CHK("AVOID_EXTERNS",
478970dc8a48SJoe Perches				"extern prototypes should be avoided in .h files\n" . $herecurr) &&
479070dc8a48SJoe Perches			    $fix) {
4791194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
479270dc8a48SJoe Perches			}
479370dc8a48SJoe Perches		}
479470dc8a48SJoe Perches
4795de7d4f0eSAndy Whitcroft# check for new externs in .c files.
4796171ae1a4SAndy Whitcroft		if ($realfile =~ /\.c$/ && defined $stat &&
4797c45dcabdSAndy Whitcroft		    $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
4798171ae1a4SAndy Whitcroft		{
4799c45dcabdSAndy Whitcroft			my $function_name = $1;
4800c45dcabdSAndy Whitcroft			my $paren_space = $2;
4801171ae1a4SAndy Whitcroft
4802171ae1a4SAndy Whitcroft			my $s = $stat;
4803171ae1a4SAndy Whitcroft			if (defined $cond) {
4804171ae1a4SAndy Whitcroft				substr($s, 0, length($cond), '');
4805171ae1a4SAndy Whitcroft			}
4806c45dcabdSAndy Whitcroft			if ($s =~ /^\s*;/ &&
4807c45dcabdSAndy Whitcroft			    $function_name ne 'uninitialized_var')
4808c45dcabdSAndy Whitcroft			{
4809000d1cc1SJoe Perches				WARN("AVOID_EXTERNS",
4810000d1cc1SJoe Perches				     "externs should be avoided in .c files\n" .  $herecurr);
4811de7d4f0eSAndy Whitcroft			}
4812de7d4f0eSAndy Whitcroft
4813171ae1a4SAndy Whitcroft			if ($paren_space =~ /\n/) {
4814000d1cc1SJoe Perches				WARN("FUNCTION_ARGUMENTS",
4815000d1cc1SJoe Perches				     "arguments for function declarations should follow identifier\n" . $herecurr);
4816171ae1a4SAndy Whitcroft			}
48179c9ba34eSAndy Whitcroft
48189c9ba34eSAndy Whitcroft		} elsif ($realfile =~ /\.c$/ && defined $stat &&
48199c9ba34eSAndy Whitcroft		    $stat =~ /^.\s*extern\s+/)
48209c9ba34eSAndy Whitcroft		{
4821000d1cc1SJoe Perches			WARN("AVOID_EXTERNS",
4822000d1cc1SJoe Perches			     "externs should be avoided in .c files\n" .  $herecurr);
4823171ae1a4SAndy Whitcroft		}
4824171ae1a4SAndy Whitcroft
4825de7d4f0eSAndy Whitcroft# checks for new __setup's
4826de7d4f0eSAndy Whitcroft		if ($rawline =~ /\b__setup\("([^"]*)"/) {
4827de7d4f0eSAndy Whitcroft			my $name = $1;
4828de7d4f0eSAndy Whitcroft
4829de7d4f0eSAndy Whitcroft			if (!grep(/$name/, @setup_docs)) {
4830000d1cc1SJoe Perches				CHK("UNDOCUMENTED_SETUP",
4831000d1cc1SJoe Perches				    "__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
4832de7d4f0eSAndy Whitcroft			}
4833653d4876SAndy Whitcroft		}
48349c0ca6f9SAndy Whitcroft
48359c0ca6f9SAndy Whitcroft# check for pointless casting of kmalloc return
4836caf2a54fSJoe Perches		if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) {
4837000d1cc1SJoe Perches			WARN("UNNECESSARY_CASTS",
4838000d1cc1SJoe Perches			     "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
48399c0ca6f9SAndy Whitcroft		}
484013214adfSAndy Whitcroft
4841a640d25cSJoe Perches# alloc style
4842a640d25cSJoe Perches# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
4843a640d25cSJoe Perches		if ($^V && $^V ge 5.10.0 &&
4844a640d25cSJoe Perches		    $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*([kv][mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
4845a640d25cSJoe Perches			CHK("ALLOC_SIZEOF_STRUCT",
4846a640d25cSJoe Perches			    "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
4847a640d25cSJoe Perches		}
4848a640d25cSJoe Perches
484960a55369SJoe Perches# check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc
485060a55369SJoe Perches		if ($^V && $^V ge 5.10.0 &&
4851e367455aSJoe Perches		    $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*(k[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)\s*,/) {
485260a55369SJoe Perches			my $oldfunc = $3;
485360a55369SJoe Perches			my $a1 = $4;
485460a55369SJoe Perches			my $a2 = $10;
485560a55369SJoe Perches			my $newfunc = "kmalloc_array";
485660a55369SJoe Perches			$newfunc = "kcalloc" if ($oldfunc eq "kzalloc");
485760a55369SJoe Perches			my $r1 = $a1;
485860a55369SJoe Perches			my $r2 = $a2;
485960a55369SJoe Perches			if ($a1 =~ /^sizeof\s*\S/) {
486060a55369SJoe Perches				$r1 = $a2;
486160a55369SJoe Perches				$r2 = $a1;
486260a55369SJoe Perches			}
4863e367455aSJoe Perches			if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ &&
4864e367455aSJoe Perches			    !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) {
4865e367455aSJoe Perches				if (WARN("ALLOC_WITH_MULTIPLY",
4866e367455aSJoe Perches					 "Prefer $newfunc over $oldfunc with multiply\n" . $herecurr) &&
4867e367455aSJoe Perches				    $fix) {
4868194f66fcSJoe 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;
486960a55369SJoe Perches
487060a55369SJoe Perches				}
487160a55369SJoe Perches			}
487260a55369SJoe Perches		}
487360a55369SJoe Perches
4874972fdea2SJoe Perches# check for krealloc arg reuse
4875972fdea2SJoe Perches		if ($^V && $^V ge 5.10.0 &&
4876972fdea2SJoe Perches		    $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*\1\s*,/) {
4877972fdea2SJoe Perches			WARN("KREALLOC_ARG_REUSE",
4878972fdea2SJoe Perches			     "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
4879972fdea2SJoe Perches		}
4880972fdea2SJoe Perches
48815ce59ae0SJoe Perches# check for alloc argument mismatch
48825ce59ae0SJoe Perches		if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
48835ce59ae0SJoe Perches			WARN("ALLOC_ARRAY_ARGS",
48845ce59ae0SJoe Perches			     "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
48855ce59ae0SJoe Perches		}
48865ce59ae0SJoe Perches
4887caf2a54fSJoe Perches# check for multiple semicolons
4888caf2a54fSJoe Perches		if ($line =~ /;\s*;\s*$/) {
4889d5e616fcSJoe Perches			if (WARN("ONE_SEMICOLON",
4890d5e616fcSJoe Perches				 "Statements terminations use 1 semicolon\n" . $herecurr) &&
4891d5e616fcSJoe Perches			    $fix) {
4892194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g;
4893d5e616fcSJoe Perches			}
4894d1e2ad07SJoe Perches		}
4895d1e2ad07SJoe Perches
4896e81f239bSJoe Perches# check for case / default statements not preceded by break/fallthrough/switch
4897c34c09a8SJoe Perches		if ($line =~ /^.\s*(?:case\s+(?:$Ident|$Constant)\s*|default):/) {
4898c34c09a8SJoe Perches			my $has_break = 0;
4899c34c09a8SJoe Perches			my $has_statement = 0;
4900c34c09a8SJoe Perches			my $count = 0;
4901c34c09a8SJoe Perches			my $prevline = $linenr;
4902e81f239bSJoe Perches			while ($prevline > 1 && ($file || $count < 3) && !$has_break) {
4903c34c09a8SJoe Perches				$prevline--;
4904c34c09a8SJoe Perches				my $rline = $rawlines[$prevline - 1];
4905c34c09a8SJoe Perches				my $fline = $lines[$prevline - 1];
4906c34c09a8SJoe Perches				last if ($fline =~ /^\@\@/);
4907c34c09a8SJoe Perches				next if ($fline =~ /^\-/);
4908c34c09a8SJoe Perches				next if ($fline =~ /^.(?:\s*(?:case\s+(?:$Ident|$Constant)[\s$;]*|default):[\s$;]*)*$/);
4909c34c09a8SJoe Perches				$has_break = 1 if ($rline =~ /fall[\s_-]*(through|thru)/i);
4910c34c09a8SJoe Perches				next if ($fline =~ /^.[\s$;]*$/);
4911c34c09a8SJoe Perches				$has_statement = 1;
4912c34c09a8SJoe Perches				$count++;
4913c34c09a8SJoe Perches				$has_break = 1 if ($fline =~ /\bswitch\b|\b(?:break\s*;[\s$;]*$|return\b|goto\b|continue\b)/);
4914c34c09a8SJoe Perches			}
4915c34c09a8SJoe Perches			if (!$has_break && $has_statement) {
4916c34c09a8SJoe Perches				WARN("MISSING_BREAK",
4917c34c09a8SJoe Perches				     "Possible switch case/default not preceeded by break or fallthrough comment\n" . $herecurr);
4918c34c09a8SJoe Perches			}
4919c34c09a8SJoe Perches		}
4920c34c09a8SJoe Perches
4921d1e2ad07SJoe Perches# check for switch/default statements without a break;
4922d1e2ad07SJoe Perches		if ($^V && $^V ge 5.10.0 &&
4923d1e2ad07SJoe Perches		    defined $stat &&
4924d1e2ad07SJoe Perches		    $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
4925d1e2ad07SJoe Perches			my $ctx = '';
4926d1e2ad07SJoe Perches			my $herectx = $here . "\n";
4927d1e2ad07SJoe Perches			my $cnt = statement_rawlines($stat);
4928d1e2ad07SJoe Perches			for (my $n = 0; $n < $cnt; $n++) {
4929d1e2ad07SJoe Perches				$herectx .= raw_line($linenr, $n) . "\n";
4930d1e2ad07SJoe Perches			}
4931d1e2ad07SJoe Perches			WARN("DEFAULT_NO_BREAK",
4932d1e2ad07SJoe Perches			     "switch default: should use break\n" . $herectx);
4933caf2a54fSJoe Perches		}
4934caf2a54fSJoe Perches
493513214adfSAndy Whitcroft# check for gcc specific __FUNCTION__
4936d5e616fcSJoe Perches		if ($line =~ /\b__FUNCTION__\b/) {
4937d5e616fcSJoe Perches			if (WARN("USE_FUNC",
4938d5e616fcSJoe Perches				 "__func__ should be used instead of gcc specific __FUNCTION__\n"  . $herecurr) &&
4939d5e616fcSJoe Perches			    $fix) {
4940194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g;
4941d5e616fcSJoe Perches			}
494213214adfSAndy Whitcroft		}
4943773647a0SAndy Whitcroft
49442c92488aSJoe Perches# check for use of yield()
49452c92488aSJoe Perches		if ($line =~ /\byield\s*\(\s*\)/) {
49462c92488aSJoe Perches			WARN("YIELD",
49472c92488aSJoe Perches			     "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n"  . $herecurr);
49482c92488aSJoe Perches		}
49492c92488aSJoe Perches
4950179f8f40SJoe Perches# check for comparisons against true and false
4951179f8f40SJoe Perches		if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
4952179f8f40SJoe Perches			my $lead = $1;
4953179f8f40SJoe Perches			my $arg = $2;
4954179f8f40SJoe Perches			my $test = $3;
4955179f8f40SJoe Perches			my $otype = $4;
4956179f8f40SJoe Perches			my $trail = $5;
4957179f8f40SJoe Perches			my $op = "!";
4958179f8f40SJoe Perches
4959179f8f40SJoe Perches			($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
4960179f8f40SJoe Perches
4961179f8f40SJoe Perches			my $type = lc($otype);
4962179f8f40SJoe Perches			if ($type =~ /^(?:true|false)$/) {
4963179f8f40SJoe Perches				if (("$test" eq "==" && "$type" eq "true") ||
4964179f8f40SJoe Perches				    ("$test" eq "!=" && "$type" eq "false")) {
4965179f8f40SJoe Perches					$op = "";
4966179f8f40SJoe Perches				}
4967179f8f40SJoe Perches
4968179f8f40SJoe Perches				CHK("BOOL_COMPARISON",
4969179f8f40SJoe Perches				    "Using comparison to $otype is error prone\n" . $herecurr);
4970179f8f40SJoe Perches
4971179f8f40SJoe Perches## maybe suggesting a correct construct would better
4972179f8f40SJoe Perches##				    "Using comparison to $otype is error prone.  Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
4973179f8f40SJoe Perches
4974179f8f40SJoe Perches			}
4975179f8f40SJoe Perches		}
4976179f8f40SJoe Perches
49774882720bSThomas Gleixner# check for semaphores initialized locked
49784882720bSThomas Gleixner		if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
4979000d1cc1SJoe Perches			WARN("CONSIDER_COMPLETION",
4980000d1cc1SJoe Perches			     "consider using a completion\n" . $herecurr);
4981773647a0SAndy Whitcroft		}
49826712d858SJoe Perches
498367d0a075SJoe Perches# recommend kstrto* over simple_strto* and strict_strto*
498467d0a075SJoe Perches		if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
4985000d1cc1SJoe Perches			WARN("CONSIDER_KSTRTO",
498667d0a075SJoe Perches			     "$1 is obsolete, use k$3 instead\n" . $herecurr);
4987773647a0SAndy Whitcroft		}
49886712d858SJoe Perches
4989ae3ccc46SFabian Frederick# check for __initcall(), use device_initcall() explicitly or more appropriate function please
4990f3db6639SMichael Ellerman		if ($line =~ /^.\s*__initcall\s*\(/) {
4991000d1cc1SJoe Perches			WARN("USE_DEVICE_INITCALL",
4992ae3ccc46SFabian Frederick			     "please use device_initcall() or more appropriate function instead of __initcall() (see include/linux/init.h)\n" . $herecurr);
4993f3db6639SMichael Ellerman		}
49946712d858SJoe Perches
499579404849SEmese Revfy# check for various ops structs, ensure they are const.
499679404849SEmese Revfy		my $struct_ops = qr{acpi_dock_ops|
499779404849SEmese Revfy				address_space_operations|
499879404849SEmese Revfy				backlight_ops|
499979404849SEmese Revfy				block_device_operations|
500079404849SEmese Revfy				dentry_operations|
500179404849SEmese Revfy				dev_pm_ops|
500279404849SEmese Revfy				dma_map_ops|
500379404849SEmese Revfy				extent_io_ops|
500479404849SEmese Revfy				file_lock_operations|
500579404849SEmese Revfy				file_operations|
500679404849SEmese Revfy				hv_ops|
500779404849SEmese Revfy				ide_dma_ops|
500879404849SEmese Revfy				intel_dvo_dev_ops|
500979404849SEmese Revfy				item_operations|
501079404849SEmese Revfy				iwl_ops|
501179404849SEmese Revfy				kgdb_arch|
501279404849SEmese Revfy				kgdb_io|
501379404849SEmese Revfy				kset_uevent_ops|
501479404849SEmese Revfy				lock_manager_operations|
501579404849SEmese Revfy				microcode_ops|
501679404849SEmese Revfy				mtrr_ops|
501779404849SEmese Revfy				neigh_ops|
501879404849SEmese Revfy				nlmsvc_binding|
501979404849SEmese Revfy				pci_raw_ops|
502079404849SEmese Revfy				pipe_buf_operations|
502179404849SEmese Revfy				platform_hibernation_ops|
502279404849SEmese Revfy				platform_suspend_ops|
502379404849SEmese Revfy				proto_ops|
502479404849SEmese Revfy				rpc_pipe_ops|
502579404849SEmese Revfy				seq_operations|
502679404849SEmese Revfy				snd_ac97_build_ops|
502779404849SEmese Revfy				soc_pcmcia_socket_ops|
502879404849SEmese Revfy				stacktrace_ops|
502979404849SEmese Revfy				sysfs_ops|
503079404849SEmese Revfy				tty_operations|
503179404849SEmese Revfy				usb_mon_operations|
503279404849SEmese Revfy				wd_ops}x;
50336903ffb2SAndy Whitcroft		if ($line !~ /\bconst\b/ &&
503479404849SEmese Revfy		    $line =~ /\bstruct\s+($struct_ops)\b/) {
5035000d1cc1SJoe Perches			WARN("CONST_STRUCT",
5036000d1cc1SJoe Perches			     "struct $1 should normally be const\n" .
50376903ffb2SAndy Whitcroft				$herecurr);
50382b6db5cbSAndy Whitcroft		}
5039773647a0SAndy Whitcroft
5040773647a0SAndy Whitcroft# use of NR_CPUS is usually wrong
5041773647a0SAndy Whitcroft# ignore definitions of NR_CPUS and usage to define arrays as likely right
5042773647a0SAndy Whitcroft		if ($line =~ /\bNR_CPUS\b/ &&
5043c45dcabdSAndy Whitcroft		    $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
5044c45dcabdSAndy Whitcroft		    $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
5045171ae1a4SAndy Whitcroft		    $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
5046171ae1a4SAndy Whitcroft		    $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
5047171ae1a4SAndy Whitcroft		    $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
5048773647a0SAndy Whitcroft		{
5049000d1cc1SJoe Perches			WARN("NR_CPUS",
5050000d1cc1SJoe Perches			     "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
5051773647a0SAndy Whitcroft		}
50529c9ba34eSAndy Whitcroft
505352ea8506SJoe Perches# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
505452ea8506SJoe Perches		if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
505552ea8506SJoe Perches			ERROR("DEFINE_ARCH_HAS",
505652ea8506SJoe Perches			      "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
505752ea8506SJoe Perches		}
505852ea8506SJoe Perches
50599c9ba34eSAndy Whitcroft# check for %L{u,d,i} in strings
50609c9ba34eSAndy Whitcroft		my $string;
50619c9ba34eSAndy Whitcroft		while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
50629c9ba34eSAndy Whitcroft			$string = substr($rawline, $-[1], $+[1] - $-[1]);
50632a1bc5d5SAndy Whitcroft			$string =~ s/%%/__/g;
50649c9ba34eSAndy Whitcroft			if ($string =~ /(?<!%)%L[udi]/) {
5065000d1cc1SJoe Perches				WARN("PRINTF_L",
5066000d1cc1SJoe Perches				     "\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
50679c9ba34eSAndy Whitcroft				last;
50689c9ba34eSAndy Whitcroft			}
50699c9ba34eSAndy Whitcroft		}
5070691d77b6SAndy Whitcroft
5071691d77b6SAndy Whitcroft# whine mightly about in_atomic
5072691d77b6SAndy Whitcroft		if ($line =~ /\bin_atomic\s*\(/) {
5073691d77b6SAndy Whitcroft			if ($realfile =~ m@^drivers/@) {
5074000d1cc1SJoe Perches				ERROR("IN_ATOMIC",
5075000d1cc1SJoe Perches				      "do not use in_atomic in drivers\n" . $herecurr);
5076f4a87736SAndy Whitcroft			} elsif ($realfile !~ m@^kernel/@) {
5077000d1cc1SJoe Perches				WARN("IN_ATOMIC",
5078000d1cc1SJoe Perches				     "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
5079691d77b6SAndy Whitcroft			}
5080691d77b6SAndy Whitcroft		}
50811704f47bSPeter Zijlstra
50821704f47bSPeter Zijlstra# check for lockdep_set_novalidate_class
50831704f47bSPeter Zijlstra		if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
50841704f47bSPeter Zijlstra		    $line =~ /__lockdep_no_validate__\s*\)/ ) {
50851704f47bSPeter Zijlstra			if ($realfile !~ m@^kernel/lockdep@ &&
50861704f47bSPeter Zijlstra			    $realfile !~ m@^include/linux/lockdep@ &&
50871704f47bSPeter Zijlstra			    $realfile !~ m@^drivers/base/core@) {
5088000d1cc1SJoe Perches				ERROR("LOCKDEP",
5089000d1cc1SJoe Perches				      "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
50901704f47bSPeter Zijlstra			}
50911704f47bSPeter Zijlstra		}
509288f8831cSDave Jones
509388f8831cSDave Jones		if ($line =~ /debugfs_create_file.*S_IWUGO/ ||
509488f8831cSDave Jones		    $line =~ /DEVICE_ATTR.*S_IWUGO/ ) {
5095000d1cc1SJoe Perches			WARN("EXPORTED_WORLD_WRITABLE",
5096000d1cc1SJoe Perches			     "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
509788f8831cSDave Jones		}
50982435880fSJoe Perches
5099515a235eSJoe Perches# Mode permission misuses where it seems decimal should be octal
5100515a235eSJoe Perches# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop
5101515a235eSJoe Perches		if ($^V && $^V ge 5.10.0 &&
5102515a235eSJoe Perches		    $line =~ /$mode_perms_search/) {
51032435880fSJoe Perches			foreach my $entry (@mode_permission_funcs) {
51042435880fSJoe Perches				my $func = $entry->[0];
51052435880fSJoe Perches				my $arg_pos = $entry->[1];
51062435880fSJoe Perches
51072435880fSJoe Perches				my $skip_args = "";
51082435880fSJoe Perches				if ($arg_pos > 1) {
51092435880fSJoe Perches					$arg_pos--;
51102435880fSJoe Perches					$skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}";
51112435880fSJoe Perches				}
51122435880fSJoe Perches				my $test = "\\b$func\\s*\\(${skip_args}([\\d]+)\\s*[,\\)]";
5113515a235eSJoe Perches				if ($line =~ /$test/) {
51142435880fSJoe Perches					my $val = $1;
51152435880fSJoe Perches					$val = $6 if ($skip_args ne "");
51162435880fSJoe Perches
51171727cc70SJoe Perches					if ($val !~ /^0$/ &&
51181727cc70SJoe Perches					    (($val =~ /^$Int$/ && $val !~ /^$Octal$/) ||
51191727cc70SJoe Perches					     length($val) ne 4)) {
51202435880fSJoe Perches						ERROR("NON_OCTAL_PERMISSIONS",
51211727cc70SJoe Perches						      "Use 4 digit octal (0777) not decimal permissions\n" . $herecurr);
51222435880fSJoe Perches					}
51232435880fSJoe Perches				}
51242435880fSJoe Perches			}
512513214adfSAndy Whitcroft		}
5126515a235eSJoe Perches	}
512713214adfSAndy Whitcroft
512813214adfSAndy Whitcroft	# If we have no input at all, then there is nothing to report on
512913214adfSAndy Whitcroft	# so just keep quiet.
513013214adfSAndy Whitcroft	if ($#rawlines == -1) {
513113214adfSAndy Whitcroft		exit(0);
51320a920b5bSAndy Whitcroft	}
51330a920b5bSAndy Whitcroft
51348905a67cSAndy Whitcroft	# In mailback mode only produce a report in the negative, for
51358905a67cSAndy Whitcroft	# things that appear to be patches.
51368905a67cSAndy Whitcroft	if ($mailback && ($clean == 1 || !$is_patch)) {
51378905a67cSAndy Whitcroft		exit(0);
51388905a67cSAndy Whitcroft	}
51398905a67cSAndy Whitcroft
51408905a67cSAndy Whitcroft	# This is not a patch, and we are are in 'no-patch' mode so
51418905a67cSAndy Whitcroft	# just keep quiet.
51428905a67cSAndy Whitcroft	if (!$chk_patch && !$is_patch) {
51438905a67cSAndy Whitcroft		exit(0);
51448905a67cSAndy Whitcroft	}
51458905a67cSAndy Whitcroft
51468905a67cSAndy Whitcroft	if (!$is_patch) {
5147000d1cc1SJoe Perches		ERROR("NOT_UNIFIED_DIFF",
5148000d1cc1SJoe Perches		      "Does not appear to be a unified-diff format patch\n");
51490a920b5bSAndy Whitcroft	}
51500a920b5bSAndy Whitcroft	if ($is_patch && $chk_signoff && $signoff == 0) {
5151000d1cc1SJoe Perches		ERROR("MISSING_SIGN_OFF",
5152000d1cc1SJoe Perches		      "Missing Signed-off-by: line(s)\n");
51530a920b5bSAndy Whitcroft	}
51540a920b5bSAndy Whitcroft
5155f0a594c1SAndy Whitcroft	print report_dump();
515613214adfSAndy Whitcroft	if ($summary && !($clean == 1 && $quiet == 1)) {
515713214adfSAndy Whitcroft		print "$filename " if ($summary_file);
51586c72ffaaSAndy Whitcroft		print "total: $cnt_error errors, $cnt_warn warnings, " .
51596c72ffaaSAndy Whitcroft			(($check)? "$cnt_chk checks, " : "") .
51606c72ffaaSAndy Whitcroft			"$cnt_lines lines checked\n";
51618905a67cSAndy Whitcroft		print "\n" if ($quiet == 0);
51626c72ffaaSAndy Whitcroft	}
51638905a67cSAndy Whitcroft
5164d2c0a235SAndy Whitcroft	if ($quiet == 0) {
5165d1fe9c09SJoe Perches
5166d1fe9c09SJoe Perches		if ($^V lt 5.10.0) {
5167d1fe9c09SJoe Perches			print("NOTE: perl $^V is not modern enough to detect all possible issues.\n");
5168d1fe9c09SJoe Perches			print("An upgrade to at least perl v5.10.0 is suggested.\n\n");
5169d1fe9c09SJoe Perches		}
5170d1fe9c09SJoe Perches
5171d2c0a235SAndy Whitcroft		# If there were whitespace errors which cleanpatch can fix
5172d2c0a235SAndy Whitcroft		# then suggest that.
5173d2c0a235SAndy Whitcroft		if ($rpt_cleaners) {
5174d2c0a235SAndy Whitcroft			print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n";
5175d2c0a235SAndy Whitcroft			print "      scripts/cleanfile\n\n";
5176b0781216SMike Frysinger			$rpt_cleaners = 0;
5177d2c0a235SAndy Whitcroft		}
5178d2c0a235SAndy Whitcroft	}
5179d2c0a235SAndy Whitcroft
518091bfe484SJoe Perches	hash_show_words(\%use_type, "Used");
518191bfe484SJoe Perches	hash_show_words(\%ignore_type, "Ignored");
5182000d1cc1SJoe Perches
5183d752fcc8SJoe Perches	if ($clean == 0 && $fix &&
5184d752fcc8SJoe Perches	    ("@rawlines" ne "@fixed" ||
5185d752fcc8SJoe Perches	     $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) {
51869624b8d6SJoe Perches		my $newfile = $filename;
51879624b8d6SJoe Perches		$newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace);
51883705ce5bSJoe Perches		my $linecount = 0;
51893705ce5bSJoe Perches		my $f;
51903705ce5bSJoe Perches
5191d752fcc8SJoe Perches		@fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted);
5192d752fcc8SJoe Perches
51933705ce5bSJoe Perches		open($f, '>', $newfile)
51943705ce5bSJoe Perches		    or die "$P: Can't open $newfile for write\n";
51953705ce5bSJoe Perches		foreach my $fixed_line (@fixed) {
51963705ce5bSJoe Perches			$linecount++;
51973705ce5bSJoe Perches			if ($file) {
51983705ce5bSJoe Perches				if ($linecount > 3) {
51993705ce5bSJoe Perches					$fixed_line =~ s/^\+//;
52003705ce5bSJoe Perches					print $f $fixed_line . "\n";
52013705ce5bSJoe Perches				}
52023705ce5bSJoe Perches			} else {
52033705ce5bSJoe Perches				print $f $fixed_line . "\n";
52043705ce5bSJoe Perches			}
52053705ce5bSJoe Perches		}
52063705ce5bSJoe Perches		close($f);
52073705ce5bSJoe Perches
52083705ce5bSJoe Perches		if (!$quiet) {
52093705ce5bSJoe Perches			print << "EOM";
52103705ce5bSJoe PerchesWrote EXPERIMENTAL --fix correction(s) to '$newfile'
52113705ce5bSJoe Perches
52123705ce5bSJoe PerchesDo _NOT_ trust the results written to this file.
52133705ce5bSJoe PerchesDo _NOT_ submit these changes without inspecting them for correctness.
52143705ce5bSJoe Perches
52153705ce5bSJoe PerchesThis EXPERIMENTAL file is simply a convenience to help rewrite patches.
52163705ce5bSJoe PerchesNo warranties, expressed or implied...
52173705ce5bSJoe Perches
52183705ce5bSJoe PerchesEOM
52193705ce5bSJoe Perches		}
52203705ce5bSJoe Perches	}
52213705ce5bSJoe Perches
52220a920b5bSAndy Whitcroft	if ($clean == 1 && $quiet == 0) {
5223c2fdda0dSAndy Whitcroft		print "$vname has no obvious style problems and is ready for submission.\n"
52240a920b5bSAndy Whitcroft	}
52250a920b5bSAndy Whitcroft	if ($clean == 0 && $quiet == 0) {
5226000d1cc1SJoe Perches		print << "EOM";
5227000d1cc1SJoe Perches$vname has style problems, please review.
5228000d1cc1SJoe Perches
5229000d1cc1SJoe PerchesIf any of these errors are false positives, please report
5230000d1cc1SJoe Perchesthem to the maintainer, see CHECKPATCH in MAINTAINERS.
5231000d1cc1SJoe PerchesEOM
52320a920b5bSAndy Whitcroft	}
523313214adfSAndy Whitcroft
52340a920b5bSAndy Whitcroft	return $clean;
52350a920b5bSAndy Whitcroft}
5236