xref: /linux-6.15/scripts/checkpatch.pl (revision ab7e23f3)
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;
1036061e38SJoe Perchesuse File::Basename;
1136061e38SJoe Perchesuse Cwd 'abs_path';
120a920b5bSAndy Whitcroft
130a920b5bSAndy Whitcroftmy $P = $0;
1436061e38SJoe Perchesmy $D = dirname(abs_path($P));
150a920b5bSAndy Whitcroft
16000d1cc1SJoe Perchesmy $V = '0.32';
170a920b5bSAndy Whitcroft
180a920b5bSAndy Whitcroftuse Getopt::Long qw(:config no_auto_abbrev);
190a920b5bSAndy Whitcroft
200a920b5bSAndy Whitcroftmy $quiet = 0;
210a920b5bSAndy Whitcroftmy $tree = 1;
220a920b5bSAndy Whitcroftmy $chk_signoff = 1;
230a920b5bSAndy Whitcroftmy $chk_patch = 1;
24773647a0SAndy Whitcroftmy $tst_only;
256c72ffaaSAndy Whitcroftmy $emacs = 0;
268905a67cSAndy Whitcroftmy $terse = 0;
276c72ffaaSAndy Whitcroftmy $file = 0;
286c72ffaaSAndy Whitcroftmy $check = 0;
292ac73b4fSJoe Perchesmy $check_orig = 0;
308905a67cSAndy Whitcroftmy $summary = 1;
318905a67cSAndy Whitcroftmy $mailback = 0;
3213214adfSAndy Whitcroftmy $summary_file = 0;
33000d1cc1SJoe Perchesmy $show_types = 0;
343705ce5bSJoe Perchesmy $fix = 0;
359624b8d6SJoe Perchesmy $fix_inplace = 0;
366c72ffaaSAndy Whitcroftmy $root;
37c2fdda0dSAndy Whitcroftmy %debug;
383445686aSJoe Perchesmy %camelcase = ();
3991bfe484SJoe Perchesmy %use_type = ();
4091bfe484SJoe Perchesmy @use = ();
4191bfe484SJoe Perchesmy %ignore_type = ();
42000d1cc1SJoe Perchesmy @ignore = ();
4377f5b10aSHannes Edermy $help = 0;
44000d1cc1SJoe Perchesmy $configuration_file = ".checkpatch.conf";
456cd7f386SJoe Perchesmy $max_line_length = 80;
46d62a201fSDave Hansenmy $ignore_perl_version = 0;
47d62a201fSDave Hansenmy $minimum_perl_version = 5.10.0;
4856193274SVadim Bendeburymy $min_conf_desc_length = 4;
4966b47b4aSKees Cookmy $spelling_file = "$D/spelling.txt";
50ebfd7d62SJoe Perchesmy $codespell = 0;
51ebfd7d62SJoe Perchesmy $codespellfile = "/usr/local/share/codespell/dictionary.txt";
5277f5b10aSHannes Eder
5377f5b10aSHannes Edersub help {
5477f5b10aSHannes Eder	my ($exitcode) = @_;
5577f5b10aSHannes Eder
5677f5b10aSHannes Eder	print << "EOM";
5777f5b10aSHannes EderUsage: $P [OPTION]... [FILE]...
5877f5b10aSHannes EderVersion: $V
5977f5b10aSHannes Eder
6077f5b10aSHannes EderOptions:
6177f5b10aSHannes Eder  -q, --quiet                quiet
6277f5b10aSHannes Eder  --no-tree                  run without a kernel tree
6377f5b10aSHannes Eder  --no-signoff               do not check for 'Signed-off-by' line
6477f5b10aSHannes Eder  --patch                    treat FILE as patchfile (default)
6577f5b10aSHannes Eder  --emacs                    emacs compile window format
6677f5b10aSHannes Eder  --terse                    one line per report
6777f5b10aSHannes Eder  -f, --file                 treat FILE as regular source file
6877f5b10aSHannes Eder  --subjective, --strict     enable more subjective tests
6991bfe484SJoe Perches  --types TYPE(,TYPE2...)    show only these comma separated message types
70000d1cc1SJoe Perches  --ignore TYPE(,TYPE2...)   ignore various comma separated message types
716cd7f386SJoe Perches  --max-line-length=n        set the maximum line length, if exceeded, warn
7256193274SVadim Bendebury  --min-conf-desc-length=n   set the min description length, if shorter, warn
73000d1cc1SJoe Perches  --show-types               show the message "types" in the output
7477f5b10aSHannes Eder  --root=PATH                PATH to the kernel tree root
7577f5b10aSHannes Eder  --no-summary               suppress the per-file summary
7677f5b10aSHannes Eder  --mailback                 only produce a report in case of warnings/errors
7777f5b10aSHannes Eder  --summary-file             include the filename in summary
7877f5b10aSHannes Eder  --debug KEY=[0|1]          turn on/off debugging of KEY, where KEY is one of
7977f5b10aSHannes Eder                             'values', 'possible', 'type', and 'attr' (default
8077f5b10aSHannes Eder                             is all off)
8177f5b10aSHannes Eder  --test-only=WORD           report only warnings/errors containing WORD
8277f5b10aSHannes Eder                             literally
833705ce5bSJoe Perches  --fix                      EXPERIMENTAL - may create horrible results
843705ce5bSJoe Perches                             If correctable single-line errors exist, create
853705ce5bSJoe Perches                             "<inputfile>.EXPERIMENTAL-checkpatch-fixes"
863705ce5bSJoe Perches                             with potential errors corrected to the preferred
873705ce5bSJoe Perches                             checkpatch style
889624b8d6SJoe Perches  --fix-inplace              EXPERIMENTAL - may create horrible results
899624b8d6SJoe Perches                             Is the same as --fix, but overwrites the input
909624b8d6SJoe Perches                             file.  It's your fault if there's no backup or git
91d62a201fSDave Hansen  --ignore-perl-version      override checking of perl version.  expect
92d62a201fSDave Hansen                             runtime errors.
93ebfd7d62SJoe Perches  --codespell                Use the codespell dictionary for spelling/typos
94ebfd7d62SJoe Perches                             (default:/usr/local/share/codespell/dictionary.txt)
95ebfd7d62SJoe Perches  --codespellfile            Use this codespell dictionary
9677f5b10aSHannes Eder  -h, --help, --version      display this help and exit
9777f5b10aSHannes Eder
9877f5b10aSHannes EderWhen FILE is - read standard input.
9977f5b10aSHannes EderEOM
10077f5b10aSHannes Eder
10177f5b10aSHannes Eder	exit($exitcode);
10277f5b10aSHannes Eder}
10377f5b10aSHannes Eder
104000d1cc1SJoe Perchesmy $conf = which_conf($configuration_file);
105000d1cc1SJoe Perchesif (-f $conf) {
106000d1cc1SJoe Perches	my @conf_args;
107000d1cc1SJoe Perches	open(my $conffile, '<', "$conf")
108000d1cc1SJoe Perches	    or warn "$P: Can't find a readable $configuration_file file $!\n";
109000d1cc1SJoe Perches
110000d1cc1SJoe Perches	while (<$conffile>) {
111000d1cc1SJoe Perches		my $line = $_;
112000d1cc1SJoe Perches
113000d1cc1SJoe Perches		$line =~ s/\s*\n?$//g;
114000d1cc1SJoe Perches		$line =~ s/^\s*//g;
115000d1cc1SJoe Perches		$line =~ s/\s+/ /g;
116000d1cc1SJoe Perches
117000d1cc1SJoe Perches		next if ($line =~ m/^\s*#/);
118000d1cc1SJoe Perches		next if ($line =~ m/^\s*$/);
119000d1cc1SJoe Perches
120000d1cc1SJoe Perches		my @words = split(" ", $line);
121000d1cc1SJoe Perches		foreach my $word (@words) {
122000d1cc1SJoe Perches			last if ($word =~ m/^#/);
123000d1cc1SJoe Perches			push (@conf_args, $word);
124000d1cc1SJoe Perches		}
125000d1cc1SJoe Perches	}
126000d1cc1SJoe Perches	close($conffile);
127000d1cc1SJoe Perches	unshift(@ARGV, @conf_args) if @conf_args;
128000d1cc1SJoe Perches}
129000d1cc1SJoe Perches
1300a920b5bSAndy WhitcroftGetOptions(
1316c72ffaaSAndy Whitcroft	'q|quiet+'	=> \$quiet,
1320a920b5bSAndy Whitcroft	'tree!'		=> \$tree,
1330a920b5bSAndy Whitcroft	'signoff!'	=> \$chk_signoff,
1340a920b5bSAndy Whitcroft	'patch!'	=> \$chk_patch,
1356c72ffaaSAndy Whitcroft	'emacs!'	=> \$emacs,
1368905a67cSAndy Whitcroft	'terse!'	=> \$terse,
13777f5b10aSHannes Eder	'f|file!'	=> \$file,
1386c72ffaaSAndy Whitcroft	'subjective!'	=> \$check,
1396c72ffaaSAndy Whitcroft	'strict!'	=> \$check,
140000d1cc1SJoe Perches	'ignore=s'	=> \@ignore,
14191bfe484SJoe Perches	'types=s'	=> \@use,
142000d1cc1SJoe Perches	'show-types!'	=> \$show_types,
1436cd7f386SJoe Perches	'max-line-length=i' => \$max_line_length,
14456193274SVadim Bendebury	'min-conf-desc-length=i' => \$min_conf_desc_length,
1456c72ffaaSAndy Whitcroft	'root=s'	=> \$root,
1468905a67cSAndy Whitcroft	'summary!'	=> \$summary,
1478905a67cSAndy Whitcroft	'mailback!'	=> \$mailback,
14813214adfSAndy Whitcroft	'summary-file!'	=> \$summary_file,
1493705ce5bSJoe Perches	'fix!'		=> \$fix,
1509624b8d6SJoe Perches	'fix-inplace!'	=> \$fix_inplace,
151d62a201fSDave Hansen	'ignore-perl-version!' => \$ignore_perl_version,
152c2fdda0dSAndy Whitcroft	'debug=s'	=> \%debug,
153773647a0SAndy Whitcroft	'test-only=s'	=> \$tst_only,
154ebfd7d62SJoe Perches	'codespell!'	=> \$codespell,
155ebfd7d62SJoe Perches	'codespellfile=s'	=> \$codespellfile,
15677f5b10aSHannes Eder	'h|help'	=> \$help,
15777f5b10aSHannes Eder	'version'	=> \$help
15877f5b10aSHannes Eder) or help(1);
15977f5b10aSHannes Eder
16077f5b10aSHannes Ederhelp(0) if ($help);
1610a920b5bSAndy Whitcroft
1629624b8d6SJoe Perches$fix = 1 if ($fix_inplace);
1632ac73b4fSJoe Perches$check_orig = $check;
1649624b8d6SJoe Perches
1650a920b5bSAndy Whitcroftmy $exit = 0;
1660a920b5bSAndy Whitcroft
167d62a201fSDave Hansenif ($^V && $^V lt $minimum_perl_version) {
168d62a201fSDave Hansen	printf "$P: requires at least perl version %vd\n", $minimum_perl_version;
169d62a201fSDave Hansen	if (!$ignore_perl_version) {
170d62a201fSDave Hansen		exit(1);
171d62a201fSDave Hansen	}
172d62a201fSDave Hansen}
173d62a201fSDave Hansen
1740a920b5bSAndy Whitcroftif ($#ARGV < 0) {
17577f5b10aSHannes Eder	print "$P: no input files\n";
1760a920b5bSAndy Whitcroft	exit(1);
1770a920b5bSAndy Whitcroft}
1780a920b5bSAndy Whitcroft
17991bfe484SJoe Perchessub hash_save_array_words {
18091bfe484SJoe Perches	my ($hashRef, $arrayRef) = @_;
18191bfe484SJoe Perches
18291bfe484SJoe Perches	my @array = split(/,/, join(',', @$arrayRef));
18391bfe484SJoe Perches	foreach my $word (@array) {
184000d1cc1SJoe Perches		$word =~ s/\s*\n?$//g;
185000d1cc1SJoe Perches		$word =~ s/^\s*//g;
186000d1cc1SJoe Perches		$word =~ s/\s+/ /g;
187000d1cc1SJoe Perches		$word =~ tr/[a-z]/[A-Z]/;
188000d1cc1SJoe Perches
189000d1cc1SJoe Perches		next if ($word =~ m/^\s*#/);
190000d1cc1SJoe Perches		next if ($word =~ m/^\s*$/);
191000d1cc1SJoe Perches
19291bfe484SJoe Perches		$hashRef->{$word}++;
193000d1cc1SJoe Perches	}
19491bfe484SJoe Perches}
19591bfe484SJoe Perches
19691bfe484SJoe Perchessub hash_show_words {
19791bfe484SJoe Perches	my ($hashRef, $prefix) = @_;
19891bfe484SJoe Perches
19958cb3cf6SJoe Perches	if ($quiet == 0 && keys %$hashRef) {
20091bfe484SJoe Perches		print "NOTE: $prefix message types:";
20158cb3cf6SJoe Perches		foreach my $word (sort keys %$hashRef) {
20291bfe484SJoe Perches			print " $word";
20391bfe484SJoe Perches		}
20491bfe484SJoe Perches		print "\n\n";
20591bfe484SJoe Perches	}
20691bfe484SJoe Perches}
20791bfe484SJoe Perches
20891bfe484SJoe Percheshash_save_array_words(\%ignore_type, \@ignore);
20991bfe484SJoe Percheshash_save_array_words(\%use_type, \@use);
210000d1cc1SJoe Perches
211c2fdda0dSAndy Whitcroftmy $dbg_values = 0;
212c2fdda0dSAndy Whitcroftmy $dbg_possible = 0;
2137429c690SAndy Whitcroftmy $dbg_type = 0;
214a1ef277eSAndy Whitcroftmy $dbg_attr = 0;
215c2fdda0dSAndy Whitcroftfor my $key (keys %debug) {
21621caa13cSAndy Whitcroft	## no critic
21721caa13cSAndy Whitcroft	eval "\${dbg_$key} = '$debug{$key}';";
21821caa13cSAndy Whitcroft	die "$@" if ($@);
219c2fdda0dSAndy Whitcroft}
220c2fdda0dSAndy Whitcroft
221d2c0a235SAndy Whitcroftmy $rpt_cleaners = 0;
222d2c0a235SAndy Whitcroft
2238905a67cSAndy Whitcroftif ($terse) {
2248905a67cSAndy Whitcroft	$emacs = 1;
2258905a67cSAndy Whitcroft	$quiet++;
2268905a67cSAndy Whitcroft}
2278905a67cSAndy Whitcroft
2286c72ffaaSAndy Whitcroftif ($tree) {
2296c72ffaaSAndy Whitcroft	if (defined $root) {
2306c72ffaaSAndy Whitcroft		if (!top_of_kernel_tree($root)) {
2316c72ffaaSAndy Whitcroft			die "$P: $root: --root does not point at a valid tree\n";
2326c72ffaaSAndy Whitcroft		}
2336c72ffaaSAndy Whitcroft	} else {
2346c72ffaaSAndy Whitcroft		if (top_of_kernel_tree('.')) {
2356c72ffaaSAndy Whitcroft			$root = '.';
2366c72ffaaSAndy Whitcroft		} elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
2376c72ffaaSAndy Whitcroft						top_of_kernel_tree($1)) {
2386c72ffaaSAndy Whitcroft			$root = $1;
2396c72ffaaSAndy Whitcroft		}
2406c72ffaaSAndy Whitcroft	}
2416c72ffaaSAndy Whitcroft
2426c72ffaaSAndy Whitcroft	if (!defined $root) {
2430a920b5bSAndy Whitcroft		print "Must be run from the top-level dir. of a kernel tree\n";
2440a920b5bSAndy Whitcroft		exit(2);
2450a920b5bSAndy Whitcroft	}
2466c72ffaaSAndy Whitcroft}
2476c72ffaaSAndy Whitcroft
2486c72ffaaSAndy Whitcroftmy $emitted_corrupt = 0;
2496c72ffaaSAndy Whitcroft
2502ceb532bSAndy Whitcroftour $Ident	= qr{
2512ceb532bSAndy Whitcroft			[A-Za-z_][A-Za-z\d_]*
2522ceb532bSAndy Whitcroft			(?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
2532ceb532bSAndy Whitcroft		}x;
2546c72ffaaSAndy Whitcroftour $Storage	= qr{extern|static|asmlinkage};
2556c72ffaaSAndy Whitcroftour $Sparse	= qr{
2566c72ffaaSAndy Whitcroft			__user|
2576c72ffaaSAndy Whitcroft			__kernel|
2586c72ffaaSAndy Whitcroft			__force|
2596c72ffaaSAndy Whitcroft			__iomem|
2606c72ffaaSAndy Whitcroft			__must_check|
2616c72ffaaSAndy Whitcroft			__init_refok|
262417495edSAndy Whitcroft			__kprobes|
263165e72a6SSven Eckelmann			__ref|
264165e72a6SSven Eckelmann			__rcu
2656c72ffaaSAndy Whitcroft		}x;
266e970b884SJoe Perchesour $InitAttributePrefix = qr{__(?:mem|cpu|dev|net_|)};
267e970b884SJoe Perchesour $InitAttributeData = qr{$InitAttributePrefix(?:initdata\b)};
268e970b884SJoe Perchesour $InitAttributeConst = qr{$InitAttributePrefix(?:initconst\b)};
269e970b884SJoe Perchesour $InitAttributeInit = qr{$InitAttributePrefix(?:init\b)};
270e970b884SJoe Perchesour $InitAttribute = qr{$InitAttributeData|$InitAttributeConst|$InitAttributeInit};
2718716de38SJoe Perches
27252131292SWolfram Sang# Notes to $Attribute:
27352131292SWolfram Sang# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
2746c72ffaaSAndy Whitcroftour $Attribute	= qr{
2756c72ffaaSAndy Whitcroft			const|
27603f1df7dSJoe Perches			__percpu|
27703f1df7dSJoe Perches			__nocast|
27803f1df7dSJoe Perches			__safe|
27903f1df7dSJoe Perches			__bitwise__|
28003f1df7dSJoe Perches			__packed__|
28103f1df7dSJoe Perches			__packed2__|
28203f1df7dSJoe Perches			__naked|
28303f1df7dSJoe Perches			__maybe_unused|
28403f1df7dSJoe Perches			__always_unused|
28503f1df7dSJoe Perches			__noreturn|
28603f1df7dSJoe Perches			__used|
28703f1df7dSJoe Perches			__cold|
288e23ef1f3SJoe Perches			__pure|
28903f1df7dSJoe Perches			__noclone|
29003f1df7dSJoe Perches			__deprecated|
2916c72ffaaSAndy Whitcroft			__read_mostly|
2926c72ffaaSAndy Whitcroft			__kprobes|
2938716de38SJoe Perches			$InitAttribute|
29424e1d81aSAndy Whitcroft			____cacheline_aligned|
29524e1d81aSAndy Whitcroft			____cacheline_aligned_in_smp|
2965fe3af11SAndy Whitcroft			____cacheline_internodealigned_in_smp|
2975fe3af11SAndy Whitcroft			__weak
2986c72ffaaSAndy Whitcroft		  }x;
299c45dcabdSAndy Whitcroftour $Modifier;
30091cb5195SJoe Perchesour $Inline	= qr{inline|__always_inline|noinline|__inline|__inline__};
3016c72ffaaSAndy Whitcroftour $Member	= qr{->$Ident|\.$Ident|\[[^]]*\]};
3026c72ffaaSAndy Whitcroftour $Lval	= qr{$Ident(?:$Member)*};
3036c72ffaaSAndy Whitcroft
30495e2c602SJoe Perchesour $Int_type	= qr{(?i)llu|ull|ll|lu|ul|l|u};
30595e2c602SJoe Perchesour $Binary	= qr{(?i)0b[01]+$Int_type?};
30695e2c602SJoe Perchesour $Hex	= qr{(?i)0x[0-9a-f]+$Int_type?};
30795e2c602SJoe Perchesour $Int	= qr{[0-9]+$Int_type?};
3082435880fSJoe Perchesour $Octal	= qr{0[0-7]+$Int_type?};
309c0a5c898SJoe Perchesour $String	= qr{"[X\t]*"};
310326b1ffcSJoe Perchesour $Float_hex	= qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?};
311326b1ffcSJoe Perchesour $Float_dec	= qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?};
312326b1ffcSJoe Perchesour $Float_int	= qr{(?i)[0-9]+e-?[0-9]+[fl]?};
31374349bccSJoe Perchesour $Float	= qr{$Float_hex|$Float_dec|$Float_int};
3142435880fSJoe Perchesour $Constant	= qr{$Float|$Binary|$Octal|$Hex|$Int};
315326b1ffcSJoe Perchesour $Assignment	= qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=};
316447432f3SJoe Perchesour $Compare    = qr{<=|>=|==|!=|<|(?<!-)>};
31723f780c9SJoe Perchesour $Arithmetic = qr{\+|-|\*|\/|%};
3186c72ffaaSAndy Whitcroftour $Operators	= qr{
3196c72ffaaSAndy Whitcroft			<=|>=|==|!=|
3206c72ffaaSAndy Whitcroft			=>|->|<<|>>|<|>|!|~|
32123f780c9SJoe Perches			&&|\|\||,|\^|\+\+|--|&|\||$Arithmetic
3226c72ffaaSAndy Whitcroft		  }x;
3236c72ffaaSAndy Whitcroft
32491cb5195SJoe Perchesour $c90_Keywords = qr{do|for|while|if|else|return|goto|continue|switch|default|case|break}x;
32591cb5195SJoe Perches
326*ab7e23f3SJoe Perchesour $BasicType;
3278905a67cSAndy Whitcroftour $NonptrType;
3281813087dSJoe Perchesour $NonptrTypeMisordered;
3298716de38SJoe Perchesour $NonptrTypeWithAttr;
3308905a67cSAndy Whitcroftour $Type;
3311813087dSJoe Perchesour $TypeMisordered;
3328905a67cSAndy Whitcroftour $Declare;
3331813087dSJoe Perchesour $DeclareMisordered;
3348905a67cSAndy Whitcroft
33515662b3eSJoe Perchesour $NON_ASCII_UTF8	= qr{
33615662b3eSJoe Perches	[\xC2-\xDF][\x80-\xBF]               # non-overlong 2-byte
337171ae1a4SAndy Whitcroft	|  \xE0[\xA0-\xBF][\x80-\xBF]        # excluding overlongs
338171ae1a4SAndy Whitcroft	| [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}  # straight 3-byte
339171ae1a4SAndy Whitcroft	|  \xED[\x80-\x9F][\x80-\xBF]        # excluding surrogates
340171ae1a4SAndy Whitcroft	|  \xF0[\x90-\xBF][\x80-\xBF]{2}     # planes 1-3
341171ae1a4SAndy Whitcroft	| [\xF1-\xF3][\x80-\xBF]{3}          # planes 4-15
342171ae1a4SAndy Whitcroft	|  \xF4[\x80-\x8F][\x80-\xBF]{2}     # plane 16
343171ae1a4SAndy Whitcroft}x;
344171ae1a4SAndy Whitcroft
34515662b3eSJoe Perchesour $UTF8	= qr{
34615662b3eSJoe Perches	[\x09\x0A\x0D\x20-\x7E]              # ASCII
34715662b3eSJoe Perches	| $NON_ASCII_UTF8
34815662b3eSJoe Perches}x;
34915662b3eSJoe Perches
350021158b4SJoe Perchesour $typeOtherOSTypedefs = qr{(?x:
351021158b4SJoe Perches	u_(?:char|short|int|long) |          # bsd
352021158b4SJoe Perches	u(?:nchar|short|int|long)            # sysv
353021158b4SJoe Perches)};
354021158b4SJoe Perches
3558ed22cadSAndy Whitcroftour $typeTypedefs = qr{(?x:
356fb9e9096SAndy Whitcroft	(?:__)?(?:u|s|be|le)(?:8|16|32|64)|
3578ed22cadSAndy Whitcroft	atomic_t
3588ed22cadSAndy Whitcroft)};
3598ed22cadSAndy Whitcroft
360691e669bSJoe Perchesour $logFunctions = qr{(?x:
3616e60c02eSJoe Perches	printk(?:_ratelimited|_once|)|
3627d0b6594SJacob Keller	(?:[a-z0-9]+_){1,2}(?:printk|emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)(?:_ratelimited|_once|)|
3636e60c02eSJoe Perches	WARN(?:_RATELIMIT|_ONCE|)|
364b0531722SJoe Perches	panic|
36506668727SJoe Perches	MODULE_[A-Z_]+|
36606668727SJoe Perches	seq_vprintf|seq_printf|seq_puts
367691e669bSJoe Perches)};
368691e669bSJoe Perches
36920112475SJoe Perchesour $signature_tags = qr{(?xi:
37020112475SJoe Perches	Signed-off-by:|
37120112475SJoe Perches	Acked-by:|
37220112475SJoe Perches	Tested-by:|
37320112475SJoe Perches	Reviewed-by:|
37420112475SJoe Perches	Reported-by:|
3758543ae12SMugunthan V N	Suggested-by:|
37620112475SJoe Perches	To:|
37720112475SJoe Perches	Cc:
37820112475SJoe Perches)};
37920112475SJoe Perches
3801813087dSJoe Perchesour @typeListMisordered = (
3811813087dSJoe Perches	qr{char\s+(?:un)?signed},
3821813087dSJoe Perches	qr{int\s+(?:(?:un)?signed\s+)?short\s},
3831813087dSJoe Perches	qr{int\s+short(?:\s+(?:un)?signed)},
3841813087dSJoe Perches	qr{short\s+int(?:\s+(?:un)?signed)},
3851813087dSJoe Perches	qr{(?:un)?signed\s+int\s+short},
3861813087dSJoe Perches	qr{short\s+(?:un)?signed},
3871813087dSJoe Perches	qr{long\s+int\s+(?:un)?signed},
3881813087dSJoe Perches	qr{int\s+long\s+(?:un)?signed},
3891813087dSJoe Perches	qr{long\s+(?:un)?signed\s+int},
3901813087dSJoe Perches	qr{int\s+(?:un)?signed\s+long},
3911813087dSJoe Perches	qr{int\s+(?:un)?signed},
3921813087dSJoe Perches	qr{int\s+long\s+long\s+(?:un)?signed},
3931813087dSJoe Perches	qr{long\s+long\s+int\s+(?:un)?signed},
3941813087dSJoe Perches	qr{long\s+long\s+(?:un)?signed\s+int},
3951813087dSJoe Perches	qr{long\s+long\s+(?:un)?signed},
3961813087dSJoe Perches	qr{long\s+(?:un)?signed},
3971813087dSJoe Perches);
3981813087dSJoe Perches
3998905a67cSAndy Whitcroftour @typeList = (
4008905a67cSAndy Whitcroft	qr{void},
4010c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?char},
4020c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?short\s+int},
4030c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?short},
4040c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?int},
4050c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?long\s+int},
4060c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?long\s+long\s+int},
4070c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?long\s+long},
4080c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?long},
4090c773d9dSJoe Perches	qr{(?:un)?signed},
4108905a67cSAndy Whitcroft	qr{float},
4118905a67cSAndy Whitcroft	qr{double},
4128905a67cSAndy Whitcroft	qr{bool},
4138905a67cSAndy Whitcroft	qr{struct\s+$Ident},
4148905a67cSAndy Whitcroft	qr{union\s+$Ident},
4158905a67cSAndy Whitcroft	qr{enum\s+$Ident},
4168905a67cSAndy Whitcroft	qr{${Ident}_t},
4178905a67cSAndy Whitcroft	qr{${Ident}_handler},
4188905a67cSAndy Whitcroft	qr{${Ident}_handler_fn},
4191813087dSJoe Perches	@typeListMisordered,
4208905a67cSAndy Whitcroft);
4218716de38SJoe Perchesour @typeListWithAttr = (
4228716de38SJoe Perches	@typeList,
4238716de38SJoe Perches	qr{struct\s+$InitAttribute\s+$Ident},
4248716de38SJoe Perches	qr{union\s+$InitAttribute\s+$Ident},
4258716de38SJoe Perches);
4268716de38SJoe Perches
427c45dcabdSAndy Whitcroftour @modifierList = (
428c45dcabdSAndy Whitcroft	qr{fastcall},
429c45dcabdSAndy Whitcroft);
4308905a67cSAndy Whitcroft
4312435880fSJoe Perchesour @mode_permission_funcs = (
4322435880fSJoe Perches	["module_param", 3],
4332435880fSJoe Perches	["module_param_(?:array|named|string)", 4],
4342435880fSJoe Perches	["module_param_array_named", 5],
4352435880fSJoe Perches	["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2],
4362435880fSJoe Perches	["proc_create(?:_data|)", 2],
4372435880fSJoe Perches	["(?:CLASS|DEVICE|SENSOR)_ATTR", 2],
4382435880fSJoe Perches);
4392435880fSJoe Perches
440515a235eSJoe Perches#Create a search pattern for all these functions to speed up a loop below
441515a235eSJoe Perchesour $mode_perms_search = "";
442515a235eSJoe Perchesforeach my $entry (@mode_permission_funcs) {
443515a235eSJoe Perches	$mode_perms_search .= '|' if ($mode_perms_search ne "");
444515a235eSJoe Perches	$mode_perms_search .= $entry->[0];
445515a235eSJoe Perches}
446515a235eSJoe Perches
447b392c64fSJoe Perchesour $mode_perms_world_writable = qr{
448b392c64fSJoe Perches	S_IWUGO		|
449b392c64fSJoe Perches	S_IWOTH		|
450b392c64fSJoe Perches	S_IRWXUGO	|
451b392c64fSJoe Perches	S_IALLUGO	|
452b392c64fSJoe Perches	0[0-7][0-7][2367]
453b392c64fSJoe Perches}x;
454b392c64fSJoe Perches
4557840a94cSWolfram Sangour $allowed_asm_includes = qr{(?x:
4567840a94cSWolfram Sang	irq|
457cdcee686SSergey Ryazanov	memory|
458cdcee686SSergey Ryazanov	time|
459cdcee686SSergey Ryazanov	reboot
4607840a94cSWolfram Sang)};
4617840a94cSWolfram Sang# memory.h: ARM has a custom one
4627840a94cSWolfram Sang
46366b47b4aSKees Cook# Load common spelling mistakes and build regular expression list.
46466b47b4aSKees Cookmy $misspellings;
46566b47b4aSKees Cookmy %spelling_fix;
46636061e38SJoe Perches
46736061e38SJoe Perchesif (open(my $spelling, '<', $spelling_file)) {
46866b47b4aSKees Cook	while (<$spelling>) {
46966b47b4aSKees Cook		my $line = $_;
47066b47b4aSKees Cook
47166b47b4aSKees Cook		$line =~ s/\s*\n?$//g;
47266b47b4aSKees Cook		$line =~ s/^\s*//g;
47366b47b4aSKees Cook
47466b47b4aSKees Cook		next if ($line =~ m/^\s*#/);
47566b47b4aSKees Cook		next if ($line =~ m/^\s*$/);
47666b47b4aSKees Cook
47766b47b4aSKees Cook		my ($suspect, $fix) = split(/\|\|/, $line);
47866b47b4aSKees Cook
47966b47b4aSKees Cook		$spelling_fix{$suspect} = $fix;
48066b47b4aSKees Cook	}
48166b47b4aSKees Cook	close($spelling);
48236061e38SJoe Perches} else {
48336061e38SJoe Perches	warn "No typos will be found - file '$spelling_file': $!\n";
48436061e38SJoe Perches}
48566b47b4aSKees Cook
486ebfd7d62SJoe Perchesif ($codespell) {
487ebfd7d62SJoe Perches	if (open(my $spelling, '<', $codespellfile)) {
488ebfd7d62SJoe Perches		while (<$spelling>) {
489ebfd7d62SJoe Perches			my $line = $_;
490ebfd7d62SJoe Perches
491ebfd7d62SJoe Perches			$line =~ s/\s*\n?$//g;
492ebfd7d62SJoe Perches			$line =~ s/^\s*//g;
493ebfd7d62SJoe Perches
494ebfd7d62SJoe Perches			next if ($line =~ m/^\s*#/);
495ebfd7d62SJoe Perches			next if ($line =~ m/^\s*$/);
496ebfd7d62SJoe Perches			next if ($line =~ m/, disabled/i);
497ebfd7d62SJoe Perches
498ebfd7d62SJoe Perches			$line =~ s/,.*$//;
499ebfd7d62SJoe Perches
500ebfd7d62SJoe Perches			my ($suspect, $fix) = split(/->/, $line);
501ebfd7d62SJoe Perches
502ebfd7d62SJoe Perches			$spelling_fix{$suspect} = $fix;
503ebfd7d62SJoe Perches		}
504ebfd7d62SJoe Perches		close($spelling);
505ebfd7d62SJoe Perches	} else {
506ebfd7d62SJoe Perches		warn "No codespell typos will be found - file '$codespellfile': $!\n";
507ebfd7d62SJoe Perches	}
508ebfd7d62SJoe Perches}
509ebfd7d62SJoe Perches
510ebfd7d62SJoe Perches$misspellings = join("|", sort keys %spelling_fix) if keys %spelling_fix;
511ebfd7d62SJoe Perches
5128905a67cSAndy Whitcroftsub build_types {
513d2172eb5SAndy Whitcroft	my $mods = "(?x:  \n" . join("|\n  ", @modifierList) . "\n)";
514d2172eb5SAndy Whitcroft	my $all = "(?x:  \n" . join("|\n  ", @typeList) . "\n)";
5151813087dSJoe Perches	my $Misordered = "(?x:  \n" . join("|\n  ", @typeListMisordered) . "\n)";
5168716de38SJoe Perches	my $allWithAttr = "(?x:  \n" . join("|\n  ", @typeListWithAttr) . "\n)";
517c8cb2ca3SAndy Whitcroft	$Modifier	= qr{(?:$Attribute|$Sparse|$mods)};
518*ab7e23f3SJoe Perches	$BasicType	= qr{
519*ab7e23f3SJoe Perches				(?:$typeOtherOSTypedefs\b)|
520*ab7e23f3SJoe Perches				(?:$typeTypedefs\b)|
521*ab7e23f3SJoe Perches				(?:${all}\b)
522*ab7e23f3SJoe Perches		}x;
5238905a67cSAndy Whitcroft	$NonptrType	= qr{
524d2172eb5SAndy Whitcroft			(?:$Modifier\s+|const\s+)*
525cf655043SAndy Whitcroft			(?:
5266b48db24SAndy Whitcroft				(?:typeof|__typeof__)\s*\([^\)]*\)|
527021158b4SJoe Perches				(?:$typeOtherOSTypedefs\b)|
5288ed22cadSAndy Whitcroft				(?:$typeTypedefs\b)|
529c45dcabdSAndy Whitcroft				(?:${all}\b)
530cf655043SAndy Whitcroft			)
531c8cb2ca3SAndy Whitcroft			(?:\s+$Modifier|\s+const)*
5328905a67cSAndy Whitcroft		  }x;
5331813087dSJoe Perches	$NonptrTypeMisordered	= qr{
5341813087dSJoe Perches			(?:$Modifier\s+|const\s+)*
5351813087dSJoe Perches			(?:
5361813087dSJoe Perches				(?:${Misordered}\b)
5371813087dSJoe Perches			)
5381813087dSJoe Perches			(?:\s+$Modifier|\s+const)*
5391813087dSJoe Perches		  }x;
5408716de38SJoe Perches	$NonptrTypeWithAttr	= qr{
5418716de38SJoe Perches			(?:$Modifier\s+|const\s+)*
5428716de38SJoe Perches			(?:
5438716de38SJoe Perches				(?:typeof|__typeof__)\s*\([^\)]*\)|
5448716de38SJoe Perches				(?:$typeTypedefs\b)|
545021158b4SJoe Perches				(?:$typeOtherOSTypedefs\b)|
5468716de38SJoe Perches				(?:${allWithAttr}\b)
5478716de38SJoe Perches			)
5488716de38SJoe Perches			(?:\s+$Modifier|\s+const)*
5498716de38SJoe Perches		  }x;
5508905a67cSAndy Whitcroft	$Type	= qr{
551c45dcabdSAndy Whitcroft			$NonptrType
5521574a29fSJoe Perches			(?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
553c8cb2ca3SAndy Whitcroft			(?:\s+$Inline|\s+$Modifier)*
5548905a67cSAndy Whitcroft		  }x;
5551813087dSJoe Perches	$TypeMisordered	= qr{
5561813087dSJoe Perches			$NonptrTypeMisordered
5571813087dSJoe Perches			(?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
5581813087dSJoe Perches			(?:\s+$Inline|\s+$Modifier)*
5591813087dSJoe Perches		  }x;
56091cb5195SJoe Perches	$Declare	= qr{(?:$Storage\s+(?:$Inline\s+)?)?$Type};
5611813087dSJoe Perches	$DeclareMisordered	= qr{(?:$Storage\s+(?:$Inline\s+)?)?$TypeMisordered};
5628905a67cSAndy Whitcroft}
5638905a67cSAndy Whitcroftbuild_types();
5646c72ffaaSAndy Whitcroft
5657d2367afSJoe Perchesour $Typecast	= qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
566d1fe9c09SJoe Perches
567d1fe9c09SJoe Perches# Using $balanced_parens, $LvalOrFunc, or $FuncArg
568d1fe9c09SJoe Perches# requires at least perl version v5.10.0
569d1fe9c09SJoe Perches# Any use must be runtime checked with $^V
570d1fe9c09SJoe Perches
571d1fe9c09SJoe Perchesour $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
5722435880fSJoe Perchesour $LvalOrFunc	= qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*};
573c0a5c898SJoe Perchesour $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant|$String)};
5747d2367afSJoe Perches
575f8422308SJoe Perchesour $declaration_macros = qr{(?x:
576f8422308SJoe Perches	(?:$Storage\s+)?(?:[A-Z_][A-Z0-9]*_){0,2}(?:DEFINE|DECLARE)(?:_[A-Z0-9]+){1,2}\s*\(|
577f8422308SJoe Perches	(?:$Storage\s+)?LIST_HEAD\s*\(|
578f8422308SJoe Perches	(?:$Storage\s+)?${Type}\s+uninitialized_var\s*\(
579f8422308SJoe Perches)};
580f8422308SJoe Perches
5817d2367afSJoe Perchessub deparenthesize {
5827d2367afSJoe Perches	my ($string) = @_;
5837d2367afSJoe Perches	return "" if (!defined($string));
5845b9553abSJoe Perches
5855b9553abSJoe Perches	while ($string =~ /^\s*\(.*\)\s*$/) {
5865b9553abSJoe Perches		$string =~ s@^\s*\(\s*@@;
5875b9553abSJoe Perches		$string =~ s@\s*\)\s*$@@;
5885b9553abSJoe Perches	}
5895b9553abSJoe Perches
5907d2367afSJoe Perches	$string =~ s@\s+@ @g;
5915b9553abSJoe Perches
5927d2367afSJoe Perches	return $string;
5937d2367afSJoe Perches}
5947d2367afSJoe Perches
5953445686aSJoe Perchessub seed_camelcase_file {
5963445686aSJoe Perches	my ($file) = @_;
5973445686aSJoe Perches
5983445686aSJoe Perches	return if (!(-f $file));
5993445686aSJoe Perches
6003445686aSJoe Perches	local $/;
6013445686aSJoe Perches
6023445686aSJoe Perches	open(my $include_file, '<', "$file")
6033445686aSJoe Perches	    or warn "$P: Can't read '$file' $!\n";
6043445686aSJoe Perches	my $text = <$include_file>;
6053445686aSJoe Perches	close($include_file);
6063445686aSJoe Perches
6073445686aSJoe Perches	my @lines = split('\n', $text);
6083445686aSJoe Perches
6093445686aSJoe Perches	foreach my $line (@lines) {
6103445686aSJoe Perches		next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/);
6113445686aSJoe Perches		if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) {
6123445686aSJoe Perches			$camelcase{$1} = 1;
61311ea516aSJoe Perches		} elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) {
61411ea516aSJoe Perches			$camelcase{$1} = 1;
61511ea516aSJoe Perches		} elsif ($line =~ /^\s*(?:union|struct|enum)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[;\{]/) {
6163445686aSJoe Perches			$camelcase{$1} = 1;
6173445686aSJoe Perches		}
6183445686aSJoe Perches	}
6193445686aSJoe Perches}
6203445686aSJoe Perches
6213445686aSJoe Perchesmy $camelcase_seeded = 0;
6223445686aSJoe Perchessub seed_camelcase_includes {
6233445686aSJoe Perches	return if ($camelcase_seeded);
6243445686aSJoe Perches
6253445686aSJoe Perches	my $files;
626c707a81dSJoe Perches	my $camelcase_cache = "";
627c707a81dSJoe Perches	my @include_files = ();
628c707a81dSJoe Perches
629c707a81dSJoe Perches	$camelcase_seeded = 1;
630351b2a1fSJoe Perches
6313645e328SRichard Genoud	if (-e ".git") {
632351b2a1fSJoe Perches		my $git_last_include_commit = `git log --no-merges --pretty=format:"%h%n" -1 -- include`;
633351b2a1fSJoe Perches		chomp $git_last_include_commit;
634c707a81dSJoe Perches		$camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit";
635c707a81dSJoe Perches	} else {
636c707a81dSJoe Perches		my $last_mod_date = 0;
637c707a81dSJoe Perches		$files = `find $root/include -name "*.h"`;
638c707a81dSJoe Perches		@include_files = split('\n', $files);
639c707a81dSJoe Perches		foreach my $file (@include_files) {
640c707a81dSJoe Perches			my $date = POSIX::strftime("%Y%m%d%H%M",
641c707a81dSJoe Perches						   localtime((stat $file)[9]));
642c707a81dSJoe Perches			$last_mod_date = $date if ($last_mod_date < $date);
643c707a81dSJoe Perches		}
644c707a81dSJoe Perches		$camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date";
645c707a81dSJoe Perches	}
646c707a81dSJoe Perches
647c707a81dSJoe Perches	if ($camelcase_cache ne "" && -f $camelcase_cache) {
648c707a81dSJoe Perches		open(my $camelcase_file, '<', "$camelcase_cache")
649c707a81dSJoe Perches		    or warn "$P: Can't read '$camelcase_cache' $!\n";
650351b2a1fSJoe Perches		while (<$camelcase_file>) {
651351b2a1fSJoe Perches			chomp;
652351b2a1fSJoe Perches			$camelcase{$_} = 1;
653351b2a1fSJoe Perches		}
654351b2a1fSJoe Perches		close($camelcase_file);
655351b2a1fSJoe Perches
656351b2a1fSJoe Perches		return;
657351b2a1fSJoe Perches	}
658c707a81dSJoe Perches
6593645e328SRichard Genoud	if (-e ".git") {
660c707a81dSJoe Perches		$files = `git ls-files "include/*.h"`;
661c707a81dSJoe Perches		@include_files = split('\n', $files);
6623445686aSJoe Perches	}
663c707a81dSJoe Perches
6643445686aSJoe Perches	foreach my $file (@include_files) {
6653445686aSJoe Perches		seed_camelcase_file($file);
6663445686aSJoe Perches	}
667351b2a1fSJoe Perches
668c707a81dSJoe Perches	if ($camelcase_cache ne "") {
669351b2a1fSJoe Perches		unlink glob ".checkpatch-camelcase.*";
670c707a81dSJoe Perches		open(my $camelcase_file, '>', "$camelcase_cache")
671c707a81dSJoe Perches		    or warn "$P: Can't write '$camelcase_cache' $!\n";
672351b2a1fSJoe Perches		foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) {
673351b2a1fSJoe Perches			print $camelcase_file ("$_\n");
674351b2a1fSJoe Perches		}
675351b2a1fSJoe Perches		close($camelcase_file);
676351b2a1fSJoe Perches	}
6773445686aSJoe Perches}
6783445686aSJoe Perches
679d311cd44SJoe Perchessub git_commit_info {
680d311cd44SJoe Perches	my ($commit, $id, $desc) = @_;
681d311cd44SJoe Perches
682d311cd44SJoe Perches	return ($id, $desc) if ((which("git") eq "") || !(-e ".git"));
683d311cd44SJoe Perches
684d311cd44SJoe Perches	my $output = `git log --no-color --format='%H %s' -1 $commit 2>&1`;
685d311cd44SJoe Perches	$output =~ s/^\s*//gm;
686d311cd44SJoe Perches	my @lines = split("\n", $output);
687d311cd44SJoe Perches
6880d7835fcSJoe Perches	return ($id, $desc) if ($#lines < 0);
6890d7835fcSJoe Perches
690d311cd44SJoe Perches	if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous\./) {
691d311cd44SJoe Perches# Maybe one day convert this block of bash into something that returns
692d311cd44SJoe Perches# all matching commit ids, but it's very slow...
693d311cd44SJoe Perches#
694d311cd44SJoe Perches#		echo "checking commits $1..."
695d311cd44SJoe Perches#		git rev-list --remotes | grep -i "^$1" |
696d311cd44SJoe Perches#		while read line ; do
697d311cd44SJoe Perches#		    git log --format='%H %s' -1 $line |
698d311cd44SJoe Perches#		    echo "commit $(cut -c 1-12,41-)"
699d311cd44SJoe Perches#		done
700d311cd44SJoe Perches	} elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./) {
701d311cd44SJoe Perches	} else {
702d311cd44SJoe Perches		$id = substr($lines[0], 0, 12);
703d311cd44SJoe Perches		$desc = substr($lines[0], 41);
704d311cd44SJoe Perches	}
705d311cd44SJoe Perches
706d311cd44SJoe Perches	return ($id, $desc);
707d311cd44SJoe Perches}
708d311cd44SJoe Perches
7096c72ffaaSAndy Whitcroft$chk_signoff = 0 if ($file);
7100a920b5bSAndy Whitcroft
71100df344fSAndy Whitcroftmy @rawlines = ();
712c2fdda0dSAndy Whitcroftmy @lines = ();
7133705ce5bSJoe Perchesmy @fixed = ();
714d752fcc8SJoe Perchesmy @fixed_inserted = ();
715d752fcc8SJoe Perchesmy @fixed_deleted = ();
716194f66fcSJoe Perchesmy $fixlinenr = -1;
717194f66fcSJoe Perches
718c2fdda0dSAndy Whitcroftmy $vname;
7196c72ffaaSAndy Whitcroftfor my $filename (@ARGV) {
72021caa13cSAndy Whitcroft	my $FILE;
7216c72ffaaSAndy Whitcroft	if ($file) {
72221caa13cSAndy Whitcroft		open($FILE, '-|', "diff -u /dev/null $filename") ||
7236c72ffaaSAndy Whitcroft			die "$P: $filename: diff failed - $!\n";
72421caa13cSAndy Whitcroft	} elsif ($filename eq '-') {
72521caa13cSAndy Whitcroft		open($FILE, '<&STDIN');
7266c72ffaaSAndy Whitcroft	} else {
72721caa13cSAndy Whitcroft		open($FILE, '<', "$filename") ||
7286c72ffaaSAndy Whitcroft			die "$P: $filename: open failed - $!\n";
7296c72ffaaSAndy Whitcroft	}
730c2fdda0dSAndy Whitcroft	if ($filename eq '-') {
731c2fdda0dSAndy Whitcroft		$vname = 'Your patch';
732c2fdda0dSAndy Whitcroft	} else {
733c2fdda0dSAndy Whitcroft		$vname = $filename;
734c2fdda0dSAndy Whitcroft	}
73521caa13cSAndy Whitcroft	while (<$FILE>) {
7360a920b5bSAndy Whitcroft		chomp;
73700df344fSAndy Whitcroft		push(@rawlines, $_);
7386c72ffaaSAndy Whitcroft	}
73921caa13cSAndy Whitcroft	close($FILE);
740c2fdda0dSAndy Whitcroft	if (!process($filename)) {
7410a920b5bSAndy Whitcroft		$exit = 1;
7420a920b5bSAndy Whitcroft	}
74300df344fSAndy Whitcroft	@rawlines = ();
74413214adfSAndy Whitcroft	@lines = ();
7453705ce5bSJoe Perches	@fixed = ();
746d752fcc8SJoe Perches	@fixed_inserted = ();
747d752fcc8SJoe Perches	@fixed_deleted = ();
748194f66fcSJoe Perches	$fixlinenr = -1;
7490a920b5bSAndy Whitcroft}
7500a920b5bSAndy Whitcroft
7510a920b5bSAndy Whitcroftexit($exit);
7520a920b5bSAndy Whitcroft
7530a920b5bSAndy Whitcroftsub top_of_kernel_tree {
7546c72ffaaSAndy Whitcroft	my ($root) = @_;
7556c72ffaaSAndy Whitcroft
7566c72ffaaSAndy Whitcroft	my @tree_check = (
7576c72ffaaSAndy Whitcroft		"COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
7586c72ffaaSAndy Whitcroft		"README", "Documentation", "arch", "include", "drivers",
7596c72ffaaSAndy Whitcroft		"fs", "init", "ipc", "kernel", "lib", "scripts",
7606c72ffaaSAndy Whitcroft	);
7616c72ffaaSAndy Whitcroft
7626c72ffaaSAndy Whitcroft	foreach my $check (@tree_check) {
7636c72ffaaSAndy Whitcroft		if (! -e $root . '/' . $check) {
7640a920b5bSAndy Whitcroft			return 0;
7650a920b5bSAndy Whitcroft		}
7666c72ffaaSAndy Whitcroft	}
7676c72ffaaSAndy Whitcroft	return 1;
7686c72ffaaSAndy Whitcroft}
7690a920b5bSAndy Whitcroft
77020112475SJoe Perchessub parse_email {
77120112475SJoe Perches	my ($formatted_email) = @_;
77220112475SJoe Perches
77320112475SJoe Perches	my $name = "";
77420112475SJoe Perches	my $address = "";
77520112475SJoe Perches	my $comment = "";
77620112475SJoe Perches
77720112475SJoe Perches	if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
77820112475SJoe Perches		$name = $1;
77920112475SJoe Perches		$address = $2;
78020112475SJoe Perches		$comment = $3 if defined $3;
78120112475SJoe Perches	} elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
78220112475SJoe Perches		$address = $1;
78320112475SJoe Perches		$comment = $2 if defined $2;
78420112475SJoe Perches	} elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
78520112475SJoe Perches		$address = $1;
78620112475SJoe Perches		$comment = $2 if defined $2;
78720112475SJoe Perches		$formatted_email =~ s/$address.*$//;
78820112475SJoe Perches		$name = $formatted_email;
7893705ce5bSJoe Perches		$name = trim($name);
79020112475SJoe Perches		$name =~ s/^\"|\"$//g;
79120112475SJoe Perches		# If there's a name left after stripping spaces and
79220112475SJoe Perches		# leading quotes, and the address doesn't have both
79320112475SJoe Perches		# leading and trailing angle brackets, the address
79420112475SJoe Perches		# is invalid. ie:
79520112475SJoe Perches		#   "joe smith [email protected]" bad
79620112475SJoe Perches		#   "joe smith <[email protected]" bad
79720112475SJoe Perches		if ($name ne "" && $address !~ /^<[^>]+>$/) {
79820112475SJoe Perches			$name = "";
79920112475SJoe Perches			$address = "";
80020112475SJoe Perches			$comment = "";
80120112475SJoe Perches		}
80220112475SJoe Perches	}
80320112475SJoe Perches
8043705ce5bSJoe Perches	$name = trim($name);
80520112475SJoe Perches	$name =~ s/^\"|\"$//g;
8063705ce5bSJoe Perches	$address = trim($address);
80720112475SJoe Perches	$address =~ s/^\<|\>$//g;
80820112475SJoe Perches
80920112475SJoe Perches	if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
81020112475SJoe Perches		$name =~ s/(?<!\\)"/\\"/g; ##escape quotes
81120112475SJoe Perches		$name = "\"$name\"";
81220112475SJoe Perches	}
81320112475SJoe Perches
81420112475SJoe Perches	return ($name, $address, $comment);
81520112475SJoe Perches}
81620112475SJoe Perches
81720112475SJoe Perchessub format_email {
81820112475SJoe Perches	my ($name, $address) = @_;
81920112475SJoe Perches
82020112475SJoe Perches	my $formatted_email;
82120112475SJoe Perches
8223705ce5bSJoe Perches	$name = trim($name);
82320112475SJoe Perches	$name =~ s/^\"|\"$//g;
8243705ce5bSJoe Perches	$address = trim($address);
82520112475SJoe Perches
82620112475SJoe Perches	if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
82720112475SJoe Perches		$name =~ s/(?<!\\)"/\\"/g; ##escape quotes
82820112475SJoe Perches		$name = "\"$name\"";
82920112475SJoe Perches	}
83020112475SJoe Perches
83120112475SJoe Perches	if ("$name" eq "") {
83220112475SJoe Perches		$formatted_email = "$address";
83320112475SJoe Perches	} else {
83420112475SJoe Perches		$formatted_email = "$name <$address>";
83520112475SJoe Perches	}
83620112475SJoe Perches
83720112475SJoe Perches	return $formatted_email;
83820112475SJoe Perches}
83920112475SJoe Perches
840d311cd44SJoe Perchessub which {
841d311cd44SJoe Perches	my ($bin) = @_;
842d311cd44SJoe Perches
843d311cd44SJoe Perches	foreach my $path (split(/:/, $ENV{PATH})) {
844d311cd44SJoe Perches		if (-e "$path/$bin") {
845d311cd44SJoe Perches			return "$path/$bin";
846d311cd44SJoe Perches		}
847d311cd44SJoe Perches	}
848d311cd44SJoe Perches
849d311cd44SJoe Perches	return "";
850d311cd44SJoe Perches}
851d311cd44SJoe Perches
852000d1cc1SJoe Perchessub which_conf {
853000d1cc1SJoe Perches	my ($conf) = @_;
854000d1cc1SJoe Perches
855000d1cc1SJoe Perches	foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
856000d1cc1SJoe Perches		if (-e "$path/$conf") {
857000d1cc1SJoe Perches			return "$path/$conf";
858000d1cc1SJoe Perches		}
859000d1cc1SJoe Perches	}
860000d1cc1SJoe Perches
861000d1cc1SJoe Perches	return "";
862000d1cc1SJoe Perches}
863000d1cc1SJoe Perches
8640a920b5bSAndy Whitcroftsub expand_tabs {
8650a920b5bSAndy Whitcroft	my ($str) = @_;
8660a920b5bSAndy Whitcroft
8670a920b5bSAndy Whitcroft	my $res = '';
8680a920b5bSAndy Whitcroft	my $n = 0;
8690a920b5bSAndy Whitcroft	for my $c (split(//, $str)) {
8700a920b5bSAndy Whitcroft		if ($c eq "\t") {
8710a920b5bSAndy Whitcroft			$res .= ' ';
8720a920b5bSAndy Whitcroft			$n++;
8730a920b5bSAndy Whitcroft			for (; ($n % 8) != 0; $n++) {
8740a920b5bSAndy Whitcroft				$res .= ' ';
8750a920b5bSAndy Whitcroft			}
8760a920b5bSAndy Whitcroft			next;
8770a920b5bSAndy Whitcroft		}
8780a920b5bSAndy Whitcroft		$res .= $c;
8790a920b5bSAndy Whitcroft		$n++;
8800a920b5bSAndy Whitcroft	}
8810a920b5bSAndy Whitcroft
8820a920b5bSAndy Whitcroft	return $res;
8830a920b5bSAndy Whitcroft}
8846c72ffaaSAndy Whitcroftsub copy_spacing {
885773647a0SAndy Whitcroft	(my $res = shift) =~ tr/\t/ /c;
8866c72ffaaSAndy Whitcroft	return $res;
8876c72ffaaSAndy Whitcroft}
8880a920b5bSAndy Whitcroft
8894a0df2efSAndy Whitcroftsub line_stats {
8904a0df2efSAndy Whitcroft	my ($line) = @_;
8914a0df2efSAndy Whitcroft
8924a0df2efSAndy Whitcroft	# Drop the diff line leader and expand tabs
8934a0df2efSAndy Whitcroft	$line =~ s/^.//;
8944a0df2efSAndy Whitcroft	$line = expand_tabs($line);
8954a0df2efSAndy Whitcroft
8964a0df2efSAndy Whitcroft	# Pick the indent from the front of the line.
8974a0df2efSAndy Whitcroft	my ($white) = ($line =~ /^(\s*)/);
8984a0df2efSAndy Whitcroft
8994a0df2efSAndy Whitcroft	return (length($line), length($white));
9004a0df2efSAndy Whitcroft}
9014a0df2efSAndy Whitcroft
902773647a0SAndy Whitcroftmy $sanitise_quote = '';
903773647a0SAndy Whitcroft
904773647a0SAndy Whitcroftsub sanitise_line_reset {
905773647a0SAndy Whitcroft	my ($in_comment) = @_;
906773647a0SAndy Whitcroft
907773647a0SAndy Whitcroft	if ($in_comment) {
908773647a0SAndy Whitcroft		$sanitise_quote = '*/';
909773647a0SAndy Whitcroft	} else {
910773647a0SAndy Whitcroft		$sanitise_quote = '';
911773647a0SAndy Whitcroft	}
912773647a0SAndy Whitcroft}
91300df344fSAndy Whitcroftsub sanitise_line {
91400df344fSAndy Whitcroft	my ($line) = @_;
91500df344fSAndy Whitcroft
91600df344fSAndy Whitcroft	my $res = '';
91700df344fSAndy Whitcroft	my $l = '';
91800df344fSAndy Whitcroft
919c2fdda0dSAndy Whitcroft	my $qlen = 0;
920773647a0SAndy Whitcroft	my $off = 0;
921773647a0SAndy Whitcroft	my $c;
92200df344fSAndy Whitcroft
923773647a0SAndy Whitcroft	# Always copy over the diff marker.
924773647a0SAndy Whitcroft	$res = substr($line, 0, 1);
925773647a0SAndy Whitcroft
926773647a0SAndy Whitcroft	for ($off = 1; $off < length($line); $off++) {
927773647a0SAndy Whitcroft		$c = substr($line, $off, 1);
928773647a0SAndy Whitcroft
929773647a0SAndy Whitcroft		# Comments we are wacking completly including the begin
930773647a0SAndy Whitcroft		# and end, all to $;.
931773647a0SAndy Whitcroft		if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
932773647a0SAndy Whitcroft			$sanitise_quote = '*/';
933773647a0SAndy Whitcroft
934773647a0SAndy Whitcroft			substr($res, $off, 2, "$;$;");
935773647a0SAndy Whitcroft			$off++;
93600df344fSAndy Whitcroft			next;
937773647a0SAndy Whitcroft		}
93881bc0e02SAndy Whitcroft		if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
939773647a0SAndy Whitcroft			$sanitise_quote = '';
940773647a0SAndy Whitcroft			substr($res, $off, 2, "$;$;");
941773647a0SAndy Whitcroft			$off++;
942773647a0SAndy Whitcroft			next;
943773647a0SAndy Whitcroft		}
944113f04a8SDaniel Walker		if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
945113f04a8SDaniel Walker			$sanitise_quote = '//';
946113f04a8SDaniel Walker
947113f04a8SDaniel Walker			substr($res, $off, 2, $sanitise_quote);
948113f04a8SDaniel Walker			$off++;
949113f04a8SDaniel Walker			next;
950113f04a8SDaniel Walker		}
951773647a0SAndy Whitcroft
952773647a0SAndy Whitcroft		# A \ in a string means ignore the next character.
953773647a0SAndy Whitcroft		if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
954773647a0SAndy Whitcroft		    $c eq "\\") {
955773647a0SAndy Whitcroft			substr($res, $off, 2, 'XX');
956773647a0SAndy Whitcroft			$off++;
957773647a0SAndy Whitcroft			next;
958773647a0SAndy Whitcroft		}
959773647a0SAndy Whitcroft		# Regular quotes.
960773647a0SAndy Whitcroft		if ($c eq "'" || $c eq '"') {
961773647a0SAndy Whitcroft			if ($sanitise_quote eq '') {
962773647a0SAndy Whitcroft				$sanitise_quote = $c;
963773647a0SAndy Whitcroft
964773647a0SAndy Whitcroft				substr($res, $off, 1, $c);
965773647a0SAndy Whitcroft				next;
966773647a0SAndy Whitcroft			} elsif ($sanitise_quote eq $c) {
967773647a0SAndy Whitcroft				$sanitise_quote = '';
96800df344fSAndy Whitcroft			}
96900df344fSAndy Whitcroft		}
970773647a0SAndy Whitcroft
971fae17daeSAndy Whitcroft		#print "c<$c> SQ<$sanitise_quote>\n";
972773647a0SAndy Whitcroft		if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
973773647a0SAndy Whitcroft			substr($res, $off, 1, $;);
974113f04a8SDaniel Walker		} elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
975113f04a8SDaniel Walker			substr($res, $off, 1, $;);
976773647a0SAndy Whitcroft		} elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
977773647a0SAndy Whitcroft			substr($res, $off, 1, 'X');
97800df344fSAndy Whitcroft		} else {
979773647a0SAndy Whitcroft			substr($res, $off, 1, $c);
98000df344fSAndy Whitcroft		}
981c2fdda0dSAndy Whitcroft	}
982c2fdda0dSAndy Whitcroft
983113f04a8SDaniel Walker	if ($sanitise_quote eq '//') {
984113f04a8SDaniel Walker		$sanitise_quote = '';
985113f04a8SDaniel Walker	}
986113f04a8SDaniel Walker
987c2fdda0dSAndy Whitcroft	# The pathname on a #include may be surrounded by '<' and '>'.
988c45dcabdSAndy Whitcroft	if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
989c2fdda0dSAndy Whitcroft		my $clean = 'X' x length($1);
990c2fdda0dSAndy Whitcroft		$res =~ s@\<.*\>@<$clean>@;
991c2fdda0dSAndy Whitcroft
992c2fdda0dSAndy Whitcroft	# The whole of a #error is a string.
993c45dcabdSAndy Whitcroft	} elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
994c2fdda0dSAndy Whitcroft		my $clean = 'X' x length($1);
995c45dcabdSAndy Whitcroft		$res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
996c2fdda0dSAndy Whitcroft	}
997c2fdda0dSAndy Whitcroft
99800df344fSAndy Whitcroft	return $res;
99900df344fSAndy Whitcroft}
100000df344fSAndy Whitcroft
1001a6962d72SJoe Perchessub get_quoted_string {
1002a6962d72SJoe Perches	my ($line, $rawline) = @_;
1003a6962d72SJoe Perches
10045e4f6ba5SJoe Perches	return "" if ($line !~ m/(\"[X\t]+\")/g);
1005a6962d72SJoe Perches	return substr($rawline, $-[0], $+[0] - $-[0]);
1006a6962d72SJoe Perches}
1007a6962d72SJoe Perches
10088905a67cSAndy Whitcroftsub ctx_statement_block {
10098905a67cSAndy Whitcroft	my ($linenr, $remain, $off) = @_;
10108905a67cSAndy Whitcroft	my $line = $linenr - 1;
10118905a67cSAndy Whitcroft	my $blk = '';
10128905a67cSAndy Whitcroft	my $soff = $off;
10138905a67cSAndy Whitcroft	my $coff = $off - 1;
1014773647a0SAndy Whitcroft	my $coff_set = 0;
10158905a67cSAndy Whitcroft
101613214adfSAndy Whitcroft	my $loff = 0;
101713214adfSAndy Whitcroft
10188905a67cSAndy Whitcroft	my $type = '';
10198905a67cSAndy Whitcroft	my $level = 0;
1020a2750645SAndy Whitcroft	my @stack = ();
1021cf655043SAndy Whitcroft	my $p;
10228905a67cSAndy Whitcroft	my $c;
10238905a67cSAndy Whitcroft	my $len = 0;
102413214adfSAndy Whitcroft
102513214adfSAndy Whitcroft	my $remainder;
10268905a67cSAndy Whitcroft	while (1) {
1027a2750645SAndy Whitcroft		@stack = (['', 0]) if ($#stack == -1);
1028a2750645SAndy Whitcroft
1029773647a0SAndy Whitcroft		#warn "CSB: blk<$blk> remain<$remain>\n";
10308905a67cSAndy Whitcroft		# If we are about to drop off the end, pull in more
10318905a67cSAndy Whitcroft		# context.
10328905a67cSAndy Whitcroft		if ($off >= $len) {
10338905a67cSAndy Whitcroft			for (; $remain > 0; $line++) {
1034dea33496SAndy Whitcroft				last if (!defined $lines[$line]);
1035c2fdda0dSAndy Whitcroft				next if ($lines[$line] =~ /^-/);
10368905a67cSAndy Whitcroft				$remain--;
103713214adfSAndy Whitcroft				$loff = $len;
1038c2fdda0dSAndy Whitcroft				$blk .= $lines[$line] . "\n";
10398905a67cSAndy Whitcroft				$len = length($blk);
10408905a67cSAndy Whitcroft				$line++;
10418905a67cSAndy Whitcroft				last;
10428905a67cSAndy Whitcroft			}
10438905a67cSAndy Whitcroft			# Bail if there is no further context.
10448905a67cSAndy Whitcroft			#warn "CSB: blk<$blk> off<$off> len<$len>\n";
104513214adfSAndy Whitcroft			if ($off >= $len) {
10468905a67cSAndy Whitcroft				last;
10478905a67cSAndy Whitcroft			}
1048f74bd194SAndy Whitcroft			if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
1049f74bd194SAndy Whitcroft				$level++;
1050f74bd194SAndy Whitcroft				$type = '#';
1051f74bd194SAndy Whitcroft			}
10528905a67cSAndy Whitcroft		}
1053cf655043SAndy Whitcroft		$p = $c;
10548905a67cSAndy Whitcroft		$c = substr($blk, $off, 1);
105513214adfSAndy Whitcroft		$remainder = substr($blk, $off);
10568905a67cSAndy Whitcroft
1057773647a0SAndy Whitcroft		#warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
10584635f4fbSAndy Whitcroft
10594635f4fbSAndy Whitcroft		# Handle nested #if/#else.
10604635f4fbSAndy Whitcroft		if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
10614635f4fbSAndy Whitcroft			push(@stack, [ $type, $level ]);
10624635f4fbSAndy Whitcroft		} elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
10634635f4fbSAndy Whitcroft			($type, $level) = @{$stack[$#stack - 1]};
10644635f4fbSAndy Whitcroft		} elsif ($remainder =~ /^#\s*endif\b/) {
10654635f4fbSAndy Whitcroft			($type, $level) = @{pop(@stack)};
10664635f4fbSAndy Whitcroft		}
10674635f4fbSAndy Whitcroft
10688905a67cSAndy Whitcroft		# Statement ends at the ';' or a close '}' at the
10698905a67cSAndy Whitcroft		# outermost level.
10708905a67cSAndy Whitcroft		if ($level == 0 && $c eq ';') {
10718905a67cSAndy Whitcroft			last;
10728905a67cSAndy Whitcroft		}
10738905a67cSAndy Whitcroft
107413214adfSAndy Whitcroft		# An else is really a conditional as long as its not else if
1075773647a0SAndy Whitcroft		if ($level == 0 && $coff_set == 0 &&
1076773647a0SAndy Whitcroft				(!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
1077773647a0SAndy Whitcroft				$remainder =~ /^(else)(?:\s|{)/ &&
1078773647a0SAndy Whitcroft				$remainder !~ /^else\s+if\b/) {
1079773647a0SAndy Whitcroft			$coff = $off + length($1) - 1;
1080773647a0SAndy Whitcroft			$coff_set = 1;
1081773647a0SAndy Whitcroft			#warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
1082773647a0SAndy Whitcroft			#warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
108313214adfSAndy Whitcroft		}
108413214adfSAndy Whitcroft
10858905a67cSAndy Whitcroft		if (($type eq '' || $type eq '(') && $c eq '(') {
10868905a67cSAndy Whitcroft			$level++;
10878905a67cSAndy Whitcroft			$type = '(';
10888905a67cSAndy Whitcroft		}
10898905a67cSAndy Whitcroft		if ($type eq '(' && $c eq ')') {
10908905a67cSAndy Whitcroft			$level--;
10918905a67cSAndy Whitcroft			$type = ($level != 0)? '(' : '';
10928905a67cSAndy Whitcroft
10938905a67cSAndy Whitcroft			if ($level == 0 && $coff < $soff) {
10948905a67cSAndy Whitcroft				$coff = $off;
1095773647a0SAndy Whitcroft				$coff_set = 1;
1096773647a0SAndy Whitcroft				#warn "CSB: mark coff<$coff>\n";
10978905a67cSAndy Whitcroft			}
10988905a67cSAndy Whitcroft		}
10998905a67cSAndy Whitcroft		if (($type eq '' || $type eq '{') && $c eq '{') {
11008905a67cSAndy Whitcroft			$level++;
11018905a67cSAndy Whitcroft			$type = '{';
11028905a67cSAndy Whitcroft		}
11038905a67cSAndy Whitcroft		if ($type eq '{' && $c eq '}') {
11048905a67cSAndy Whitcroft			$level--;
11058905a67cSAndy Whitcroft			$type = ($level != 0)? '{' : '';
11068905a67cSAndy Whitcroft
11078905a67cSAndy Whitcroft			if ($level == 0) {
1108b998e001SPatrick Pannuto				if (substr($blk, $off + 1, 1) eq ';') {
1109b998e001SPatrick Pannuto					$off++;
1110b998e001SPatrick Pannuto				}
11118905a67cSAndy Whitcroft				last;
11128905a67cSAndy Whitcroft			}
11138905a67cSAndy Whitcroft		}
1114f74bd194SAndy Whitcroft		# Preprocessor commands end at the newline unless escaped.
1115f74bd194SAndy Whitcroft		if ($type eq '#' && $c eq "\n" && $p ne "\\") {
1116f74bd194SAndy Whitcroft			$level--;
1117f74bd194SAndy Whitcroft			$type = '';
1118f74bd194SAndy Whitcroft			$off++;
1119f74bd194SAndy Whitcroft			last;
1120f74bd194SAndy Whitcroft		}
11218905a67cSAndy Whitcroft		$off++;
11228905a67cSAndy Whitcroft	}
1123a3bb97a7SAndy Whitcroft	# We are truly at the end, so shuffle to the next line.
112413214adfSAndy Whitcroft	if ($off == $len) {
1125a3bb97a7SAndy Whitcroft		$loff = $len + 1;
112613214adfSAndy Whitcroft		$line++;
112713214adfSAndy Whitcroft		$remain--;
112813214adfSAndy Whitcroft	}
11298905a67cSAndy Whitcroft
11308905a67cSAndy Whitcroft	my $statement = substr($blk, $soff, $off - $soff + 1);
11318905a67cSAndy Whitcroft	my $condition = substr($blk, $soff, $coff - $soff + 1);
11328905a67cSAndy Whitcroft
11338905a67cSAndy Whitcroft	#warn "STATEMENT<$statement>\n";
11348905a67cSAndy Whitcroft	#warn "CONDITION<$condition>\n";
11358905a67cSAndy Whitcroft
1136773647a0SAndy Whitcroft	#print "coff<$coff> soff<$off> loff<$loff>\n";
113713214adfSAndy Whitcroft
113813214adfSAndy Whitcroft	return ($statement, $condition,
113913214adfSAndy Whitcroft			$line, $remain + 1, $off - $loff + 1, $level);
114013214adfSAndy Whitcroft}
114113214adfSAndy Whitcroft
1142cf655043SAndy Whitcroftsub statement_lines {
1143cf655043SAndy Whitcroft	my ($stmt) = @_;
1144cf655043SAndy Whitcroft
1145cf655043SAndy Whitcroft	# Strip the diff line prefixes and rip blank lines at start and end.
1146cf655043SAndy Whitcroft	$stmt =~ s/(^|\n)./$1/g;
1147cf655043SAndy Whitcroft	$stmt =~ s/^\s*//;
1148cf655043SAndy Whitcroft	$stmt =~ s/\s*$//;
1149cf655043SAndy Whitcroft
1150cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
1151cf655043SAndy Whitcroft
1152cf655043SAndy Whitcroft	return $#stmt_lines + 2;
1153cf655043SAndy Whitcroft}
1154cf655043SAndy Whitcroft
1155cf655043SAndy Whitcroftsub statement_rawlines {
1156cf655043SAndy Whitcroft	my ($stmt) = @_;
1157cf655043SAndy Whitcroft
1158cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
1159cf655043SAndy Whitcroft
1160cf655043SAndy Whitcroft	return $#stmt_lines + 2;
1161cf655043SAndy Whitcroft}
1162cf655043SAndy Whitcroft
1163cf655043SAndy Whitcroftsub statement_block_size {
1164cf655043SAndy Whitcroft	my ($stmt) = @_;
1165cf655043SAndy Whitcroft
1166cf655043SAndy Whitcroft	$stmt =~ s/(^|\n)./$1/g;
1167cf655043SAndy Whitcroft	$stmt =~ s/^\s*{//;
1168cf655043SAndy Whitcroft	$stmt =~ s/}\s*$//;
1169cf655043SAndy Whitcroft	$stmt =~ s/^\s*//;
1170cf655043SAndy Whitcroft	$stmt =~ s/\s*$//;
1171cf655043SAndy Whitcroft
1172cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
1173cf655043SAndy Whitcroft	my @stmt_statements = ($stmt =~ /;/g);
1174cf655043SAndy Whitcroft
1175cf655043SAndy Whitcroft	my $stmt_lines = $#stmt_lines + 2;
1176cf655043SAndy Whitcroft	my $stmt_statements = $#stmt_statements + 1;
1177cf655043SAndy Whitcroft
1178cf655043SAndy Whitcroft	if ($stmt_lines > $stmt_statements) {
1179cf655043SAndy Whitcroft		return $stmt_lines;
1180cf655043SAndy Whitcroft	} else {
1181cf655043SAndy Whitcroft		return $stmt_statements;
1182cf655043SAndy Whitcroft	}
1183cf655043SAndy Whitcroft}
1184cf655043SAndy Whitcroft
118513214adfSAndy Whitcroftsub ctx_statement_full {
118613214adfSAndy Whitcroft	my ($linenr, $remain, $off) = @_;
118713214adfSAndy Whitcroft	my ($statement, $condition, $level);
118813214adfSAndy Whitcroft
118913214adfSAndy Whitcroft	my (@chunks);
119013214adfSAndy Whitcroft
1191cf655043SAndy Whitcroft	# Grab the first conditional/block pair.
119213214adfSAndy Whitcroft	($statement, $condition, $linenr, $remain, $off, $level) =
119313214adfSAndy Whitcroft				ctx_statement_block($linenr, $remain, $off);
1194773647a0SAndy Whitcroft	#print "F: c<$condition> s<$statement> remain<$remain>\n";
119513214adfSAndy Whitcroft	push(@chunks, [ $condition, $statement ]);
1196cf655043SAndy Whitcroft	if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
1197cf655043SAndy Whitcroft		return ($level, $linenr, @chunks);
1198cf655043SAndy Whitcroft	}
1199cf655043SAndy Whitcroft
1200cf655043SAndy Whitcroft	# Pull in the following conditional/block pairs and see if they
1201cf655043SAndy Whitcroft	# could continue the statement.
1202cf655043SAndy Whitcroft	for (;;) {
120313214adfSAndy Whitcroft		($statement, $condition, $linenr, $remain, $off, $level) =
120413214adfSAndy Whitcroft				ctx_statement_block($linenr, $remain, $off);
1205cf655043SAndy Whitcroft		#print "C: c<$condition> s<$statement> remain<$remain>\n";
1206773647a0SAndy Whitcroft		last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
1207cf655043SAndy Whitcroft		#print "C: push\n";
1208cf655043SAndy Whitcroft		push(@chunks, [ $condition, $statement ]);
120913214adfSAndy Whitcroft	}
121013214adfSAndy Whitcroft
121113214adfSAndy Whitcroft	return ($level, $linenr, @chunks);
12128905a67cSAndy Whitcroft}
12138905a67cSAndy Whitcroft
12144a0df2efSAndy Whitcroftsub ctx_block_get {
1215f0a594c1SAndy Whitcroft	my ($linenr, $remain, $outer, $open, $close, $off) = @_;
12164a0df2efSAndy Whitcroft	my $line;
12174a0df2efSAndy Whitcroft	my $start = $linenr - 1;
12184a0df2efSAndy Whitcroft	my $blk = '';
12194a0df2efSAndy Whitcroft	my @o;
12204a0df2efSAndy Whitcroft	my @c;
12214a0df2efSAndy Whitcroft	my @res = ();
12224a0df2efSAndy Whitcroft
1223f0a594c1SAndy Whitcroft	my $level = 0;
12244635f4fbSAndy Whitcroft	my @stack = ($level);
122500df344fSAndy Whitcroft	for ($line = $start; $remain > 0; $line++) {
122600df344fSAndy Whitcroft		next if ($rawlines[$line] =~ /^-/);
122700df344fSAndy Whitcroft		$remain--;
122800df344fSAndy Whitcroft
122900df344fSAndy Whitcroft		$blk .= $rawlines[$line];
12304635f4fbSAndy Whitcroft
12314635f4fbSAndy Whitcroft		# Handle nested #if/#else.
123201464f30SAndy Whitcroft		if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
12334635f4fbSAndy Whitcroft			push(@stack, $level);
123401464f30SAndy Whitcroft		} elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
12354635f4fbSAndy Whitcroft			$level = $stack[$#stack - 1];
123601464f30SAndy Whitcroft		} elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
12374635f4fbSAndy Whitcroft			$level = pop(@stack);
12384635f4fbSAndy Whitcroft		}
12394635f4fbSAndy Whitcroft
124001464f30SAndy Whitcroft		foreach my $c (split(//, $lines[$line])) {
1241f0a594c1SAndy Whitcroft			##print "C<$c>L<$level><$open$close>O<$off>\n";
1242f0a594c1SAndy Whitcroft			if ($off > 0) {
1243f0a594c1SAndy Whitcroft				$off--;
1244f0a594c1SAndy Whitcroft				next;
1245f0a594c1SAndy Whitcroft			}
12464a0df2efSAndy Whitcroft
1247f0a594c1SAndy Whitcroft			if ($c eq $close && $level > 0) {
1248f0a594c1SAndy Whitcroft				$level--;
1249f0a594c1SAndy Whitcroft				last if ($level == 0);
1250f0a594c1SAndy Whitcroft			} elsif ($c eq $open) {
1251f0a594c1SAndy Whitcroft				$level++;
1252f0a594c1SAndy Whitcroft			}
1253f0a594c1SAndy Whitcroft		}
12544a0df2efSAndy Whitcroft
1255f0a594c1SAndy Whitcroft		if (!$outer || $level <= 1) {
125600df344fSAndy Whitcroft			push(@res, $rawlines[$line]);
12574a0df2efSAndy Whitcroft		}
12584a0df2efSAndy Whitcroft
1259f0a594c1SAndy Whitcroft		last if ($level == 0);
12604a0df2efSAndy Whitcroft	}
12614a0df2efSAndy Whitcroft
1262f0a594c1SAndy Whitcroft	return ($level, @res);
12634a0df2efSAndy Whitcroft}
12644a0df2efSAndy Whitcroftsub ctx_block_outer {
12654a0df2efSAndy Whitcroft	my ($linenr, $remain) = @_;
12664a0df2efSAndy Whitcroft
1267f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
1268f0a594c1SAndy Whitcroft	return @r;
12694a0df2efSAndy Whitcroft}
12704a0df2efSAndy Whitcroftsub ctx_block {
12714a0df2efSAndy Whitcroft	my ($linenr, $remain) = @_;
12724a0df2efSAndy Whitcroft
1273f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1274f0a594c1SAndy Whitcroft	return @r;
1275653d4876SAndy Whitcroft}
1276653d4876SAndy Whitcroftsub ctx_statement {
1277f0a594c1SAndy Whitcroft	my ($linenr, $remain, $off) = @_;
1278f0a594c1SAndy Whitcroft
1279f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1280f0a594c1SAndy Whitcroft	return @r;
1281f0a594c1SAndy Whitcroft}
1282f0a594c1SAndy Whitcroftsub ctx_block_level {
1283653d4876SAndy Whitcroft	my ($linenr, $remain) = @_;
1284653d4876SAndy Whitcroft
1285f0a594c1SAndy Whitcroft	return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
12864a0df2efSAndy Whitcroft}
12879c0ca6f9SAndy Whitcroftsub ctx_statement_level {
12889c0ca6f9SAndy Whitcroft	my ($linenr, $remain, $off) = @_;
12899c0ca6f9SAndy Whitcroft
12909c0ca6f9SAndy Whitcroft	return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
12919c0ca6f9SAndy Whitcroft}
12924a0df2efSAndy Whitcroft
12934a0df2efSAndy Whitcroftsub ctx_locate_comment {
12944a0df2efSAndy Whitcroft	my ($first_line, $end_line) = @_;
12954a0df2efSAndy Whitcroft
12964a0df2efSAndy Whitcroft	# Catch a comment on the end of the line itself.
1297beae6332SAndy Whitcroft	my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
12984a0df2efSAndy Whitcroft	return $current_comment if (defined $current_comment);
12994a0df2efSAndy Whitcroft
13004a0df2efSAndy Whitcroft	# Look through the context and try and figure out if there is a
13014a0df2efSAndy Whitcroft	# comment.
13024a0df2efSAndy Whitcroft	my $in_comment = 0;
13034a0df2efSAndy Whitcroft	$current_comment = '';
13044a0df2efSAndy Whitcroft	for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
130500df344fSAndy Whitcroft		my $line = $rawlines[$linenr - 1];
130600df344fSAndy Whitcroft		#warn "           $line\n";
13074a0df2efSAndy Whitcroft		if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
13084a0df2efSAndy Whitcroft			$in_comment = 1;
13094a0df2efSAndy Whitcroft		}
13104a0df2efSAndy Whitcroft		if ($line =~ m@/\*@) {
13114a0df2efSAndy Whitcroft			$in_comment = 1;
13124a0df2efSAndy Whitcroft		}
13134a0df2efSAndy Whitcroft		if (!$in_comment && $current_comment ne '') {
13144a0df2efSAndy Whitcroft			$current_comment = '';
13154a0df2efSAndy Whitcroft		}
13164a0df2efSAndy Whitcroft		$current_comment .= $line . "\n" if ($in_comment);
13174a0df2efSAndy Whitcroft		if ($line =~ m@\*/@) {
13184a0df2efSAndy Whitcroft			$in_comment = 0;
13194a0df2efSAndy Whitcroft		}
13204a0df2efSAndy Whitcroft	}
13214a0df2efSAndy Whitcroft
13224a0df2efSAndy Whitcroft	chomp($current_comment);
13234a0df2efSAndy Whitcroft	return($current_comment);
13244a0df2efSAndy Whitcroft}
13254a0df2efSAndy Whitcroftsub ctx_has_comment {
13264a0df2efSAndy Whitcroft	my ($first_line, $end_line) = @_;
13274a0df2efSAndy Whitcroft	my $cmt = ctx_locate_comment($first_line, $end_line);
13284a0df2efSAndy Whitcroft
132900df344fSAndy Whitcroft	##print "LINE: $rawlines[$end_line - 1 ]\n";
13304a0df2efSAndy Whitcroft	##print "CMMT: $cmt\n";
13314a0df2efSAndy Whitcroft
13324a0df2efSAndy Whitcroft	return ($cmt ne '');
13334a0df2efSAndy Whitcroft}
13344a0df2efSAndy Whitcroft
13354d001e4dSAndy Whitcroftsub raw_line {
13364d001e4dSAndy Whitcroft	my ($linenr, $cnt) = @_;
13374d001e4dSAndy Whitcroft
13384d001e4dSAndy Whitcroft	my $offset = $linenr - 1;
13394d001e4dSAndy Whitcroft	$cnt++;
13404d001e4dSAndy Whitcroft
13414d001e4dSAndy Whitcroft	my $line;
13424d001e4dSAndy Whitcroft	while ($cnt) {
13434d001e4dSAndy Whitcroft		$line = $rawlines[$offset++];
13444d001e4dSAndy Whitcroft		next if (defined($line) && $line =~ /^-/);
13454d001e4dSAndy Whitcroft		$cnt--;
13464d001e4dSAndy Whitcroft	}
13474d001e4dSAndy Whitcroft
13484d001e4dSAndy Whitcroft	return $line;
13494d001e4dSAndy Whitcroft}
13504d001e4dSAndy Whitcroft
13510a920b5bSAndy Whitcroftsub cat_vet {
13520a920b5bSAndy Whitcroft	my ($vet) = @_;
13539c0ca6f9SAndy Whitcroft	my ($res, $coded);
13540a920b5bSAndy Whitcroft
13559c0ca6f9SAndy Whitcroft	$res = '';
13566c72ffaaSAndy Whitcroft	while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
13576c72ffaaSAndy Whitcroft		$res .= $1;
13586c72ffaaSAndy Whitcroft		if ($2 ne '') {
13599c0ca6f9SAndy Whitcroft			$coded = sprintf("^%c", unpack('C', $2) + 64);
13606c72ffaaSAndy Whitcroft			$res .= $coded;
13616c72ffaaSAndy Whitcroft		}
13629c0ca6f9SAndy Whitcroft	}
13639c0ca6f9SAndy Whitcroft	$res =~ s/$/\$/;
13640a920b5bSAndy Whitcroft
13659c0ca6f9SAndy Whitcroft	return $res;
13660a920b5bSAndy Whitcroft}
13670a920b5bSAndy Whitcroft
1368c2fdda0dSAndy Whitcroftmy $av_preprocessor = 0;
1369cf655043SAndy Whitcroftmy $av_pending;
1370c2fdda0dSAndy Whitcroftmy @av_paren_type;
13711f65f947SAndy Whitcroftmy $av_pend_colon;
1372c2fdda0dSAndy Whitcroft
1373c2fdda0dSAndy Whitcroftsub annotate_reset {
1374c2fdda0dSAndy Whitcroft	$av_preprocessor = 0;
1375cf655043SAndy Whitcroft	$av_pending = '_';
1376cf655043SAndy Whitcroft	@av_paren_type = ('E');
13771f65f947SAndy Whitcroft	$av_pend_colon = 'O';
1378c2fdda0dSAndy Whitcroft}
1379c2fdda0dSAndy Whitcroft
13806c72ffaaSAndy Whitcroftsub annotate_values {
13816c72ffaaSAndy Whitcroft	my ($stream, $type) = @_;
13826c72ffaaSAndy Whitcroft
13836c72ffaaSAndy Whitcroft	my $res;
13841f65f947SAndy Whitcroft	my $var = '_' x length($stream);
13856c72ffaaSAndy Whitcroft	my $cur = $stream;
13866c72ffaaSAndy Whitcroft
1387c2fdda0dSAndy Whitcroft	print "$stream\n" if ($dbg_values > 1);
13886c72ffaaSAndy Whitcroft
13896c72ffaaSAndy Whitcroft	while (length($cur)) {
1390773647a0SAndy Whitcroft		@av_paren_type = ('E') if ($#av_paren_type < 0);
1391cf655043SAndy Whitcroft		print " <" . join('', @av_paren_type) .
1392171ae1a4SAndy Whitcroft				"> <$type> <$av_pending>" if ($dbg_values > 1);
13936c72ffaaSAndy Whitcroft		if ($cur =~ /^(\s+)/o) {
1394c2fdda0dSAndy Whitcroft			print "WS($1)\n" if ($dbg_values > 1);
1395c2fdda0dSAndy Whitcroft			if ($1 =~ /\n/ && $av_preprocessor) {
1396cf655043SAndy Whitcroft				$type = pop(@av_paren_type);
1397c2fdda0dSAndy Whitcroft				$av_preprocessor = 0;
13986c72ffaaSAndy Whitcroft			}
13996c72ffaaSAndy Whitcroft
1400c023e473SFlorian Mickler		} elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
14019446ef56SAndy Whitcroft			print "CAST($1)\n" if ($dbg_values > 1);
14029446ef56SAndy Whitcroft			push(@av_paren_type, $type);
1403addcdceaSAndy Whitcroft			$type = 'c';
14049446ef56SAndy Whitcroft
1405e91b6e26SAndy Whitcroft		} elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
1406c2fdda0dSAndy Whitcroft			print "DECLARE($1)\n" if ($dbg_values > 1);
14076c72ffaaSAndy Whitcroft			$type = 'T';
14086c72ffaaSAndy Whitcroft
1409389a2fe5SAndy Whitcroft		} elsif ($cur =~ /^($Modifier)\s*/) {
1410389a2fe5SAndy Whitcroft			print "MODIFIER($1)\n" if ($dbg_values > 1);
1411389a2fe5SAndy Whitcroft			$type = 'T';
1412389a2fe5SAndy Whitcroft
1413c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
1414171ae1a4SAndy Whitcroft			print "DEFINE($1,$2)\n" if ($dbg_values > 1);
1415c2fdda0dSAndy Whitcroft			$av_preprocessor = 1;
1416171ae1a4SAndy Whitcroft			push(@av_paren_type, $type);
1417171ae1a4SAndy Whitcroft			if ($2 ne '') {
1418cf655043SAndy Whitcroft				$av_pending = 'N';
1419171ae1a4SAndy Whitcroft			}
1420171ae1a4SAndy Whitcroft			$type = 'E';
1421171ae1a4SAndy Whitcroft
1422c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
1423171ae1a4SAndy Whitcroft			print "UNDEF($1)\n" if ($dbg_values > 1);
1424171ae1a4SAndy Whitcroft			$av_preprocessor = 1;
1425171ae1a4SAndy Whitcroft			push(@av_paren_type, $type);
14266c72ffaaSAndy Whitcroft
1427c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
1428cf655043SAndy Whitcroft			print "PRE_START($1)\n" if ($dbg_values > 1);
1429c2fdda0dSAndy Whitcroft			$av_preprocessor = 1;
1430cf655043SAndy Whitcroft
1431cf655043SAndy Whitcroft			push(@av_paren_type, $type);
1432cf655043SAndy Whitcroft			push(@av_paren_type, $type);
1433171ae1a4SAndy Whitcroft			$type = 'E';
1434cf655043SAndy Whitcroft
1435c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
1436cf655043SAndy Whitcroft			print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1437cf655043SAndy Whitcroft			$av_preprocessor = 1;
1438cf655043SAndy Whitcroft
1439cf655043SAndy Whitcroft			push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1440cf655043SAndy Whitcroft
1441171ae1a4SAndy Whitcroft			$type = 'E';
1442cf655043SAndy Whitcroft
1443c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:endif))/o) {
1444cf655043SAndy Whitcroft			print "PRE_END($1)\n" if ($dbg_values > 1);
1445cf655043SAndy Whitcroft
1446cf655043SAndy Whitcroft			$av_preprocessor = 1;
1447cf655043SAndy Whitcroft
1448cf655043SAndy Whitcroft			# Assume all arms of the conditional end as this
1449cf655043SAndy Whitcroft			# one does, and continue as if the #endif was not here.
1450cf655043SAndy Whitcroft			pop(@av_paren_type);
1451cf655043SAndy Whitcroft			push(@av_paren_type, $type);
1452171ae1a4SAndy Whitcroft			$type = 'E';
14536c72ffaaSAndy Whitcroft
14546c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\\\n)/o) {
1455c2fdda0dSAndy Whitcroft			print "PRECONT($1)\n" if ($dbg_values > 1);
14566c72ffaaSAndy Whitcroft
1457171ae1a4SAndy Whitcroft		} elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1458171ae1a4SAndy Whitcroft			print "ATTR($1)\n" if ($dbg_values > 1);
1459171ae1a4SAndy Whitcroft			$av_pending = $type;
1460171ae1a4SAndy Whitcroft			$type = 'N';
1461171ae1a4SAndy Whitcroft
14626c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
1463c2fdda0dSAndy Whitcroft			print "SIZEOF($1)\n" if ($dbg_values > 1);
14646c72ffaaSAndy Whitcroft			if (defined $2) {
1465cf655043SAndy Whitcroft				$av_pending = 'V';
14666c72ffaaSAndy Whitcroft			}
14676c72ffaaSAndy Whitcroft			$type = 'N';
14686c72ffaaSAndy Whitcroft
146914b111c1SAndy Whitcroft		} elsif ($cur =~ /^(if|while|for)\b/o) {
1470c2fdda0dSAndy Whitcroft			print "COND($1)\n" if ($dbg_values > 1);
147114b111c1SAndy Whitcroft			$av_pending = 'E';
14726c72ffaaSAndy Whitcroft			$type = 'N';
14736c72ffaaSAndy Whitcroft
14741f65f947SAndy Whitcroft		} elsif ($cur =~/^(case)/o) {
14751f65f947SAndy Whitcroft			print "CASE($1)\n" if ($dbg_values > 1);
14761f65f947SAndy Whitcroft			$av_pend_colon = 'C';
14771f65f947SAndy Whitcroft			$type = 'N';
14781f65f947SAndy Whitcroft
147914b111c1SAndy Whitcroft		} elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
1480c2fdda0dSAndy Whitcroft			print "KEYWORD($1)\n" if ($dbg_values > 1);
14816c72ffaaSAndy Whitcroft			$type = 'N';
14826c72ffaaSAndy Whitcroft
14836c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\()/o) {
1484c2fdda0dSAndy Whitcroft			print "PAREN('$1')\n" if ($dbg_values > 1);
1485cf655043SAndy Whitcroft			push(@av_paren_type, $av_pending);
1486cf655043SAndy Whitcroft			$av_pending = '_';
14876c72ffaaSAndy Whitcroft			$type = 'N';
14886c72ffaaSAndy Whitcroft
14896c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\))/o) {
1490cf655043SAndy Whitcroft			my $new_type = pop(@av_paren_type);
1491cf655043SAndy Whitcroft			if ($new_type ne '_') {
1492cf655043SAndy Whitcroft				$type = $new_type;
1493c2fdda0dSAndy Whitcroft				print "PAREN('$1') -> $type\n"
1494c2fdda0dSAndy Whitcroft							if ($dbg_values > 1);
14956c72ffaaSAndy Whitcroft			} else {
1496c2fdda0dSAndy Whitcroft				print "PAREN('$1')\n" if ($dbg_values > 1);
14976c72ffaaSAndy Whitcroft			}
14986c72ffaaSAndy Whitcroft
1499c8cb2ca3SAndy Whitcroft		} elsif ($cur =~ /^($Ident)\s*\(/o) {
1500c2fdda0dSAndy Whitcroft			print "FUNC($1)\n" if ($dbg_values > 1);
1501c8cb2ca3SAndy Whitcroft			$type = 'V';
1502cf655043SAndy Whitcroft			$av_pending = 'V';
15036c72ffaaSAndy Whitcroft
15048e761b04SAndy Whitcroft		} elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
15058e761b04SAndy Whitcroft			if (defined $2 && $type eq 'C' || $type eq 'T') {
15061f65f947SAndy Whitcroft				$av_pend_colon = 'B';
15078e761b04SAndy Whitcroft			} elsif ($type eq 'E') {
15088e761b04SAndy Whitcroft				$av_pend_colon = 'L';
15091f65f947SAndy Whitcroft			}
15101f65f947SAndy Whitcroft			print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
15111f65f947SAndy Whitcroft			$type = 'V';
15121f65f947SAndy Whitcroft
15136c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Ident|$Constant)/o) {
1514c2fdda0dSAndy Whitcroft			print "IDENT($1)\n" if ($dbg_values > 1);
15156c72ffaaSAndy Whitcroft			$type = 'V';
15166c72ffaaSAndy Whitcroft
15176c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Assignment)/o) {
1518c2fdda0dSAndy Whitcroft			print "ASSIGN($1)\n" if ($dbg_values > 1);
15196c72ffaaSAndy Whitcroft			$type = 'N';
15206c72ffaaSAndy Whitcroft
1521cf655043SAndy Whitcroft		} elsif ($cur =~/^(;|{|})/) {
1522c2fdda0dSAndy Whitcroft			print "END($1)\n" if ($dbg_values > 1);
152313214adfSAndy Whitcroft			$type = 'E';
15241f65f947SAndy Whitcroft			$av_pend_colon = 'O';
152513214adfSAndy Whitcroft
15268e761b04SAndy Whitcroft		} elsif ($cur =~/^(,)/) {
15278e761b04SAndy Whitcroft			print "COMMA($1)\n" if ($dbg_values > 1);
15288e761b04SAndy Whitcroft			$type = 'C';
15298e761b04SAndy Whitcroft
15301f65f947SAndy Whitcroft		} elsif ($cur =~ /^(\?)/o) {
15311f65f947SAndy Whitcroft			print "QUESTION($1)\n" if ($dbg_values > 1);
15321f65f947SAndy Whitcroft			$type = 'N';
15331f65f947SAndy Whitcroft
15341f65f947SAndy Whitcroft		} elsif ($cur =~ /^(:)/o) {
15351f65f947SAndy Whitcroft			print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
15361f65f947SAndy Whitcroft
15371f65f947SAndy Whitcroft			substr($var, length($res), 1, $av_pend_colon);
15381f65f947SAndy Whitcroft			if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
15391f65f947SAndy Whitcroft				$type = 'E';
15401f65f947SAndy Whitcroft			} else {
15411f65f947SAndy Whitcroft				$type = 'N';
15421f65f947SAndy Whitcroft			}
15431f65f947SAndy Whitcroft			$av_pend_colon = 'O';
15441f65f947SAndy Whitcroft
15458e761b04SAndy Whitcroft		} elsif ($cur =~ /^(\[)/o) {
154613214adfSAndy Whitcroft			print "CLOSE($1)\n" if ($dbg_values > 1);
15476c72ffaaSAndy Whitcroft			$type = 'N';
15486c72ffaaSAndy Whitcroft
15490d413866SAndy Whitcroft		} elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
155074048ed8SAndy Whitcroft			my $variant;
155174048ed8SAndy Whitcroft
155274048ed8SAndy Whitcroft			print "OPV($1)\n" if ($dbg_values > 1);
155374048ed8SAndy Whitcroft			if ($type eq 'V') {
155474048ed8SAndy Whitcroft				$variant = 'B';
155574048ed8SAndy Whitcroft			} else {
155674048ed8SAndy Whitcroft				$variant = 'U';
155774048ed8SAndy Whitcroft			}
155874048ed8SAndy Whitcroft
155974048ed8SAndy Whitcroft			substr($var, length($res), 1, $variant);
156074048ed8SAndy Whitcroft			$type = 'N';
156174048ed8SAndy Whitcroft
15626c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Operators)/o) {
1563c2fdda0dSAndy Whitcroft			print "OP($1)\n" if ($dbg_values > 1);
15646c72ffaaSAndy Whitcroft			if ($1 ne '++' && $1 ne '--') {
15656c72ffaaSAndy Whitcroft				$type = 'N';
15666c72ffaaSAndy Whitcroft			}
15676c72ffaaSAndy Whitcroft
15686c72ffaaSAndy Whitcroft		} elsif ($cur =~ /(^.)/o) {
1569c2fdda0dSAndy Whitcroft			print "C($1)\n" if ($dbg_values > 1);
15706c72ffaaSAndy Whitcroft		}
15716c72ffaaSAndy Whitcroft		if (defined $1) {
15726c72ffaaSAndy Whitcroft			$cur = substr($cur, length($1));
15736c72ffaaSAndy Whitcroft			$res .= $type x length($1);
15746c72ffaaSAndy Whitcroft		}
15756c72ffaaSAndy Whitcroft	}
15766c72ffaaSAndy Whitcroft
15771f65f947SAndy Whitcroft	return ($res, $var);
15786c72ffaaSAndy Whitcroft}
15796c72ffaaSAndy Whitcroft
15808905a67cSAndy Whitcroftsub possible {
158113214adfSAndy Whitcroft	my ($possible, $line) = @_;
15829a974fdbSAndy Whitcroft	my $notPermitted = qr{(?:
15830776e594SAndy Whitcroft		^(?:
15840776e594SAndy Whitcroft			$Modifier|
15850776e594SAndy Whitcroft			$Storage|
15860776e594SAndy Whitcroft			$Type|
15879a974fdbSAndy Whitcroft			DEFINE_\S+
15889a974fdbSAndy Whitcroft		)$|
15899a974fdbSAndy Whitcroft		^(?:
15900776e594SAndy Whitcroft			goto|
15910776e594SAndy Whitcroft			return|
15920776e594SAndy Whitcroft			case|
15930776e594SAndy Whitcroft			else|
15940776e594SAndy Whitcroft			asm|__asm__|
159589a88353SAndy Whitcroft			do|
159689a88353SAndy Whitcroft			\#|
159789a88353SAndy Whitcroft			\#\#|
15989a974fdbSAndy Whitcroft		)(?:\s|$)|
15990776e594SAndy Whitcroft		^(?:typedef|struct|enum)\b
16009a974fdbSAndy Whitcroft	    )}x;
16019a974fdbSAndy Whitcroft	warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
16029a974fdbSAndy Whitcroft	if ($possible !~ $notPermitted) {
1603c45dcabdSAndy Whitcroft		# Check for modifiers.
1604c45dcabdSAndy Whitcroft		$possible =~ s/\s*$Storage\s*//g;
1605c45dcabdSAndy Whitcroft		$possible =~ s/\s*$Sparse\s*//g;
1606c45dcabdSAndy Whitcroft		if ($possible =~ /^\s*$/) {
1607c45dcabdSAndy Whitcroft
1608c45dcabdSAndy Whitcroft		} elsif ($possible =~ /\s/) {
1609c45dcabdSAndy Whitcroft			$possible =~ s/\s*$Type\s*//g;
1610d2506586SAndy Whitcroft			for my $modifier (split(' ', $possible)) {
16119a974fdbSAndy Whitcroft				if ($modifier !~ $notPermitted) {
1612d2506586SAndy Whitcroft					warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1613d2506586SAndy Whitcroft					push(@modifierList, $modifier);
1614d2506586SAndy Whitcroft				}
16159a974fdbSAndy Whitcroft			}
1616c45dcabdSAndy Whitcroft
1617c45dcabdSAndy Whitcroft		} else {
161813214adfSAndy Whitcroft			warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
16198905a67cSAndy Whitcroft			push(@typeList, $possible);
1620c45dcabdSAndy Whitcroft		}
16218905a67cSAndy Whitcroft		build_types();
16220776e594SAndy Whitcroft	} else {
16230776e594SAndy Whitcroft		warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
16248905a67cSAndy Whitcroft	}
16258905a67cSAndy Whitcroft}
16268905a67cSAndy Whitcroft
16276c72ffaaSAndy Whitcroftmy $prefix = '';
16286c72ffaaSAndy Whitcroft
1629000d1cc1SJoe Perchessub show_type {
1630cbec18afSJoe Perches	my ($type) = @_;
163191bfe484SJoe Perches
1632cbec18afSJoe Perches	return defined $use_type{$type} if (scalar keys %use_type > 0);
1633cbec18afSJoe Perches
1634cbec18afSJoe Perches	return !defined $ignore_type{$type};
1635000d1cc1SJoe Perches}
1636000d1cc1SJoe Perches
1637f0a594c1SAndy Whitcroftsub report {
1638cbec18afSJoe Perches	my ($level, $type, $msg) = @_;
1639cbec18afSJoe Perches
1640cbec18afSJoe Perches	if (!show_type($type) ||
1641cbec18afSJoe Perches	    (defined $tst_only && $msg !~ /\Q$tst_only\E/)) {
1642773647a0SAndy Whitcroft		return 0;
1643773647a0SAndy Whitcroft	}
1644000d1cc1SJoe Perches	my $line;
1645000d1cc1SJoe Perches	if ($show_types) {
1646cbec18afSJoe Perches		$line = "$prefix$level:$type: $msg\n";
1647000d1cc1SJoe Perches	} else {
1648cbec18afSJoe Perches		$line = "$prefix$level: $msg\n";
1649000d1cc1SJoe Perches	}
16508905a67cSAndy Whitcroft	$line = (split('\n', $line))[0] . "\n" if ($terse);
16518905a67cSAndy Whitcroft
165213214adfSAndy Whitcroft	push(our @report, $line);
1653773647a0SAndy Whitcroft
1654773647a0SAndy Whitcroft	return 1;
1655f0a594c1SAndy Whitcroft}
1656cbec18afSJoe Perches
1657f0a594c1SAndy Whitcroftsub report_dump {
165813214adfSAndy Whitcroft	our @report;
1659f0a594c1SAndy Whitcroft}
1660000d1cc1SJoe Perches
1661d752fcc8SJoe Perchessub fixup_current_range {
1662d752fcc8SJoe Perches	my ($lineRef, $offset, $length) = @_;
1663d752fcc8SJoe Perches
1664d752fcc8SJoe Perches	if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) {
1665d752fcc8SJoe Perches		my $o = $1;
1666d752fcc8SJoe Perches		my $l = $2;
1667d752fcc8SJoe Perches		my $no = $o + $offset;
1668d752fcc8SJoe Perches		my $nl = $l + $length;
1669d752fcc8SJoe Perches		$$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/;
1670d752fcc8SJoe Perches	}
1671d752fcc8SJoe Perches}
1672d752fcc8SJoe Perches
1673d752fcc8SJoe Perchessub fix_inserted_deleted_lines {
1674d752fcc8SJoe Perches	my ($linesRef, $insertedRef, $deletedRef) = @_;
1675d752fcc8SJoe Perches
1676d752fcc8SJoe Perches	my $range_last_linenr = 0;
1677d752fcc8SJoe Perches	my $delta_offset = 0;
1678d752fcc8SJoe Perches
1679d752fcc8SJoe Perches	my $old_linenr = 0;
1680d752fcc8SJoe Perches	my $new_linenr = 0;
1681d752fcc8SJoe Perches
1682d752fcc8SJoe Perches	my $next_insert = 0;
1683d752fcc8SJoe Perches	my $next_delete = 0;
1684d752fcc8SJoe Perches
1685d752fcc8SJoe Perches	my @lines = ();
1686d752fcc8SJoe Perches
1687d752fcc8SJoe Perches	my $inserted = @{$insertedRef}[$next_insert++];
1688d752fcc8SJoe Perches	my $deleted = @{$deletedRef}[$next_delete++];
1689d752fcc8SJoe Perches
1690d752fcc8SJoe Perches	foreach my $old_line (@{$linesRef}) {
1691d752fcc8SJoe Perches		my $save_line = 1;
1692d752fcc8SJoe Perches		my $line = $old_line;	#don't modify the array
1693d752fcc8SJoe Perches		if ($line =~ /^(?:\+\+\+\|\-\-\-)\s+\S+/) {	#new filename
1694d752fcc8SJoe Perches			$delta_offset = 0;
1695d752fcc8SJoe Perches		} elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) {	#new hunk
1696d752fcc8SJoe Perches			$range_last_linenr = $new_linenr;
1697d752fcc8SJoe Perches			fixup_current_range(\$line, $delta_offset, 0);
1698d752fcc8SJoe Perches		}
1699d752fcc8SJoe Perches
1700d752fcc8SJoe Perches		while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) {
1701d752fcc8SJoe Perches			$deleted = @{$deletedRef}[$next_delete++];
1702d752fcc8SJoe Perches			$save_line = 0;
1703d752fcc8SJoe Perches			fixup_current_range(\$lines[$range_last_linenr], $delta_offset--, -1);
1704d752fcc8SJoe Perches		}
1705d752fcc8SJoe Perches
1706d752fcc8SJoe Perches		while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) {
1707d752fcc8SJoe Perches			push(@lines, ${$inserted}{'LINE'});
1708d752fcc8SJoe Perches			$inserted = @{$insertedRef}[$next_insert++];
1709d752fcc8SJoe Perches			$new_linenr++;
1710d752fcc8SJoe Perches			fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1);
1711d752fcc8SJoe Perches		}
1712d752fcc8SJoe Perches
1713d752fcc8SJoe Perches		if ($save_line) {
1714d752fcc8SJoe Perches			push(@lines, $line);
1715d752fcc8SJoe Perches			$new_linenr++;
1716d752fcc8SJoe Perches		}
1717d752fcc8SJoe Perches
1718d752fcc8SJoe Perches		$old_linenr++;
1719d752fcc8SJoe Perches	}
1720d752fcc8SJoe Perches
1721d752fcc8SJoe Perches	return @lines;
1722d752fcc8SJoe Perches}
1723d752fcc8SJoe Perches
1724f2d7e4d4SJoe Perchessub fix_insert_line {
1725f2d7e4d4SJoe Perches	my ($linenr, $line) = @_;
1726f2d7e4d4SJoe Perches
1727f2d7e4d4SJoe Perches	my $inserted = {
1728f2d7e4d4SJoe Perches		LINENR => $linenr,
1729f2d7e4d4SJoe Perches		LINE => $line,
1730f2d7e4d4SJoe Perches	};
1731f2d7e4d4SJoe Perches	push(@fixed_inserted, $inserted);
1732f2d7e4d4SJoe Perches}
1733f2d7e4d4SJoe Perches
1734f2d7e4d4SJoe Perchessub fix_delete_line {
1735f2d7e4d4SJoe Perches	my ($linenr, $line) = @_;
1736f2d7e4d4SJoe Perches
1737f2d7e4d4SJoe Perches	my $deleted = {
1738f2d7e4d4SJoe Perches		LINENR => $linenr,
1739f2d7e4d4SJoe Perches		LINE => $line,
1740f2d7e4d4SJoe Perches	};
1741f2d7e4d4SJoe Perches
1742f2d7e4d4SJoe Perches	push(@fixed_deleted, $deleted);
1743f2d7e4d4SJoe Perches}
1744f2d7e4d4SJoe Perches
1745de7d4f0eSAndy Whitcroftsub ERROR {
1746cbec18afSJoe Perches	my ($type, $msg) = @_;
1747cbec18afSJoe Perches
1748cbec18afSJoe Perches	if (report("ERROR", $type, $msg)) {
1749de7d4f0eSAndy Whitcroft		our $clean = 0;
17506c72ffaaSAndy Whitcroft		our $cnt_error++;
17513705ce5bSJoe Perches		return 1;
1752de7d4f0eSAndy Whitcroft	}
17533705ce5bSJoe Perches	return 0;
1754773647a0SAndy Whitcroft}
1755de7d4f0eSAndy Whitcroftsub WARN {
1756cbec18afSJoe Perches	my ($type, $msg) = @_;
1757cbec18afSJoe Perches
1758cbec18afSJoe Perches	if (report("WARNING", $type, $msg)) {
1759de7d4f0eSAndy Whitcroft		our $clean = 0;
17606c72ffaaSAndy Whitcroft		our $cnt_warn++;
17613705ce5bSJoe Perches		return 1;
1762de7d4f0eSAndy Whitcroft	}
17633705ce5bSJoe Perches	return 0;
1764773647a0SAndy Whitcroft}
1765de7d4f0eSAndy Whitcroftsub CHK {
1766cbec18afSJoe Perches	my ($type, $msg) = @_;
1767cbec18afSJoe Perches
1768cbec18afSJoe Perches	if ($check && report("CHECK", $type, $msg)) {
1769de7d4f0eSAndy Whitcroft		our $clean = 0;
17706c72ffaaSAndy Whitcroft		our $cnt_chk++;
17713705ce5bSJoe Perches		return 1;
17726c72ffaaSAndy Whitcroft	}
17733705ce5bSJoe Perches	return 0;
1774de7d4f0eSAndy Whitcroft}
1775de7d4f0eSAndy Whitcroft
17766ecd9674SAndy Whitcroftsub check_absolute_file {
17776ecd9674SAndy Whitcroft	my ($absolute, $herecurr) = @_;
17786ecd9674SAndy Whitcroft	my $file = $absolute;
17796ecd9674SAndy Whitcroft
17806ecd9674SAndy Whitcroft	##print "absolute<$absolute>\n";
17816ecd9674SAndy Whitcroft
17826ecd9674SAndy Whitcroft	# See if any suffix of this path is a path within the tree.
17836ecd9674SAndy Whitcroft	while ($file =~ s@^[^/]*/@@) {
17846ecd9674SAndy Whitcroft		if (-f "$root/$file") {
17856ecd9674SAndy Whitcroft			##print "file<$file>\n";
17866ecd9674SAndy Whitcroft			last;
17876ecd9674SAndy Whitcroft		}
17886ecd9674SAndy Whitcroft	}
17896ecd9674SAndy Whitcroft	if (! -f _)  {
17906ecd9674SAndy Whitcroft		return 0;
17916ecd9674SAndy Whitcroft	}
17926ecd9674SAndy Whitcroft
17936ecd9674SAndy Whitcroft	# It is, so see if the prefix is acceptable.
17946ecd9674SAndy Whitcroft	my $prefix = $absolute;
17956ecd9674SAndy Whitcroft	substr($prefix, -length($file)) = '';
17966ecd9674SAndy Whitcroft
17976ecd9674SAndy Whitcroft	##print "prefix<$prefix>\n";
17986ecd9674SAndy Whitcroft	if ($prefix ne ".../") {
1799000d1cc1SJoe Perches		WARN("USE_RELATIVE_PATH",
1800000d1cc1SJoe Perches		     "use relative pathname instead of absolute in changelog text\n" . $herecurr);
18016ecd9674SAndy Whitcroft	}
18026ecd9674SAndy Whitcroft}
18036ecd9674SAndy Whitcroft
18043705ce5bSJoe Perchessub trim {
18053705ce5bSJoe Perches	my ($string) = @_;
18063705ce5bSJoe Perches
1807b34c648bSJoe Perches	$string =~ s/^\s+|\s+$//g;
1808b34c648bSJoe Perches
1809b34c648bSJoe Perches	return $string;
1810b34c648bSJoe Perches}
1811b34c648bSJoe Perches
1812b34c648bSJoe Perchessub ltrim {
1813b34c648bSJoe Perches	my ($string) = @_;
1814b34c648bSJoe Perches
1815b34c648bSJoe Perches	$string =~ s/^\s+//;
1816b34c648bSJoe Perches
1817b34c648bSJoe Perches	return $string;
1818b34c648bSJoe Perches}
1819b34c648bSJoe Perches
1820b34c648bSJoe Perchessub rtrim {
1821b34c648bSJoe Perches	my ($string) = @_;
1822b34c648bSJoe Perches
1823b34c648bSJoe Perches	$string =~ s/\s+$//;
18243705ce5bSJoe Perches
18253705ce5bSJoe Perches	return $string;
18263705ce5bSJoe Perches}
18273705ce5bSJoe Perches
182852ea8506SJoe Perchessub string_find_replace {
182952ea8506SJoe Perches	my ($string, $find, $replace) = @_;
183052ea8506SJoe Perches
183152ea8506SJoe Perches	$string =~ s/$find/$replace/g;
183252ea8506SJoe Perches
183352ea8506SJoe Perches	return $string;
183452ea8506SJoe Perches}
183552ea8506SJoe Perches
18363705ce5bSJoe Perchessub tabify {
18373705ce5bSJoe Perches	my ($leading) = @_;
18383705ce5bSJoe Perches
18393705ce5bSJoe Perches	my $source_indent = 8;
18403705ce5bSJoe Perches	my $max_spaces_before_tab = $source_indent - 1;
18413705ce5bSJoe Perches	my $spaces_to_tab = " " x $source_indent;
18423705ce5bSJoe Perches
18433705ce5bSJoe Perches	#convert leading spaces to tabs
18443705ce5bSJoe Perches	1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
18453705ce5bSJoe Perches	#Remove spaces before a tab
18463705ce5bSJoe Perches	1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
18473705ce5bSJoe Perches
18483705ce5bSJoe Perches	return "$leading";
18493705ce5bSJoe Perches}
18503705ce5bSJoe Perches
1851d1fe9c09SJoe Perchessub pos_last_openparen {
1852d1fe9c09SJoe Perches	my ($line) = @_;
1853d1fe9c09SJoe Perches
1854d1fe9c09SJoe Perches	my $pos = 0;
1855d1fe9c09SJoe Perches
1856d1fe9c09SJoe Perches	my $opens = $line =~ tr/\(/\(/;
1857d1fe9c09SJoe Perches	my $closes = $line =~ tr/\)/\)/;
1858d1fe9c09SJoe Perches
1859d1fe9c09SJoe Perches	my $last_openparen = 0;
1860d1fe9c09SJoe Perches
1861d1fe9c09SJoe Perches	if (($opens == 0) || ($closes >= $opens)) {
1862d1fe9c09SJoe Perches		return -1;
1863d1fe9c09SJoe Perches	}
1864d1fe9c09SJoe Perches
1865d1fe9c09SJoe Perches	my $len = length($line);
1866d1fe9c09SJoe Perches
1867d1fe9c09SJoe Perches	for ($pos = 0; $pos < $len; $pos++) {
1868d1fe9c09SJoe Perches		my $string = substr($line, $pos);
1869d1fe9c09SJoe Perches		if ($string =~ /^($FuncArg|$balanced_parens)/) {
1870d1fe9c09SJoe Perches			$pos += length($1) - 1;
1871d1fe9c09SJoe Perches		} elsif (substr($line, $pos, 1) eq '(') {
1872d1fe9c09SJoe Perches			$last_openparen = $pos;
1873d1fe9c09SJoe Perches		} elsif (index($string, '(') == -1) {
1874d1fe9c09SJoe Perches			last;
1875d1fe9c09SJoe Perches		}
1876d1fe9c09SJoe Perches	}
1877d1fe9c09SJoe Perches
187891cb5195SJoe Perches	return length(expand_tabs(substr($line, 0, $last_openparen))) + 1;
1879d1fe9c09SJoe Perches}
1880d1fe9c09SJoe Perches
18810a920b5bSAndy Whitcroftsub process {
18820a920b5bSAndy Whitcroft	my $filename = shift;
18830a920b5bSAndy Whitcroft
18840a920b5bSAndy Whitcroft	my $linenr=0;
18850a920b5bSAndy Whitcroft	my $prevline="";
1886c2fdda0dSAndy Whitcroft	my $prevrawline="";
18870a920b5bSAndy Whitcroft	my $stashline="";
1888c2fdda0dSAndy Whitcroft	my $stashrawline="";
18890a920b5bSAndy Whitcroft
18904a0df2efSAndy Whitcroft	my $length;
18910a920b5bSAndy Whitcroft	my $indent;
18920a920b5bSAndy Whitcroft	my $previndent=0;
18930a920b5bSAndy Whitcroft	my $stashindent=0;
18940a920b5bSAndy Whitcroft
1895de7d4f0eSAndy Whitcroft	our $clean = 1;
18960a920b5bSAndy Whitcroft	my $signoff = 0;
18970a920b5bSAndy Whitcroft	my $is_patch = 0;
18980a920b5bSAndy Whitcroft
189929ee1b0cSJoe Perches	my $in_header_lines = $file ? 0 : 1;
190015662b3eSJoe Perches	my $in_commit_log = 0;		#Scanning lines before patch
190113f1937eSJoe Perches	my $reported_maintainer_file = 0;
1902fa64205dSPasi Savanainen	my $non_utf8_charset = 0;
1903fa64205dSPasi Savanainen
1904365dd4eaSJoe Perches	my $last_blank_line = 0;
19055e4f6ba5SJoe Perches	my $last_coalesced_string_linenr = -1;
1906365dd4eaSJoe Perches
190713214adfSAndy Whitcroft	our @report = ();
19086c72ffaaSAndy Whitcroft	our $cnt_lines = 0;
19096c72ffaaSAndy Whitcroft	our $cnt_error = 0;
19106c72ffaaSAndy Whitcroft	our $cnt_warn = 0;
19116c72ffaaSAndy Whitcroft	our $cnt_chk = 0;
19126c72ffaaSAndy Whitcroft
19130a920b5bSAndy Whitcroft	# Trace the real file/line as we go.
19140a920b5bSAndy Whitcroft	my $realfile = '';
19150a920b5bSAndy Whitcroft	my $realline = 0;
19160a920b5bSAndy Whitcroft	my $realcnt = 0;
19170a920b5bSAndy Whitcroft	my $here = '';
19180a920b5bSAndy Whitcroft	my $in_comment = 0;
1919c2fdda0dSAndy Whitcroft	my $comment_edge = 0;
19200a920b5bSAndy Whitcroft	my $first_line = 0;
19211e855726SWolfram Sang	my $p1_prefix = '';
19220a920b5bSAndy Whitcroft
192313214adfSAndy Whitcroft	my $prev_values = 'E';
192413214adfSAndy Whitcroft
192513214adfSAndy Whitcroft	# suppression flags
1926773647a0SAndy Whitcroft	my %suppress_ifbraces;
1927170d3a22SAndy Whitcroft	my %suppress_whiletrailers;
19282b474a1aSAndy Whitcroft	my %suppress_export;
19293e469cdcSAndy Whitcroft	my $suppress_statement = 0;
1930653d4876SAndy Whitcroft
19317e51f197SJoe Perches	my %signatures = ();
1932323c1260SJoe Perches
1933c2fdda0dSAndy Whitcroft	# Pre-scan the patch sanitizing the lines.
1934de7d4f0eSAndy Whitcroft	# Pre-scan the patch looking for any __setup documentation.
1935c2fdda0dSAndy Whitcroft	#
1936de7d4f0eSAndy Whitcroft	my @setup_docs = ();
1937de7d4f0eSAndy Whitcroft	my $setup_docs = 0;
1938773647a0SAndy Whitcroft
1939d8b07710SJoe Perches	my $camelcase_file_seeded = 0;
1940d8b07710SJoe Perches
1941773647a0SAndy Whitcroft	sanitise_line_reset();
1942c2fdda0dSAndy Whitcroft	my $line;
1943c2fdda0dSAndy Whitcroft	foreach my $rawline (@rawlines) {
1944773647a0SAndy Whitcroft		$linenr++;
1945773647a0SAndy Whitcroft		$line = $rawline;
1946c2fdda0dSAndy Whitcroft
19473705ce5bSJoe Perches		push(@fixed, $rawline) if ($fix);
19483705ce5bSJoe Perches
1949773647a0SAndy Whitcroft		if ($rawline=~/^\+\+\+\s+(\S+)/) {
1950de7d4f0eSAndy Whitcroft			$setup_docs = 0;
1951de7d4f0eSAndy Whitcroft			if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
1952de7d4f0eSAndy Whitcroft				$setup_docs = 1;
1953de7d4f0eSAndy Whitcroft			}
1954773647a0SAndy Whitcroft			#next;
1955de7d4f0eSAndy Whitcroft		}
1956773647a0SAndy Whitcroft		if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1957773647a0SAndy Whitcroft			$realline=$1-1;
1958773647a0SAndy Whitcroft			if (defined $2) {
1959773647a0SAndy Whitcroft				$realcnt=$3+1;
1960773647a0SAndy Whitcroft			} else {
1961773647a0SAndy Whitcroft				$realcnt=1+1;
1962773647a0SAndy Whitcroft			}
1963c45dcabdSAndy Whitcroft			$in_comment = 0;
1964773647a0SAndy Whitcroft
1965773647a0SAndy Whitcroft			# Guestimate if this is a continuing comment.  Run
1966773647a0SAndy Whitcroft			# the context looking for a comment "edge".  If this
1967773647a0SAndy Whitcroft			# edge is a close comment then we must be in a comment
1968773647a0SAndy Whitcroft			# at context start.
1969773647a0SAndy Whitcroft			my $edge;
197001fa9147SAndy Whitcroft			my $cnt = $realcnt;
197101fa9147SAndy Whitcroft			for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
197201fa9147SAndy Whitcroft				next if (defined $rawlines[$ln - 1] &&
197301fa9147SAndy Whitcroft					 $rawlines[$ln - 1] =~ /^-/);
197401fa9147SAndy Whitcroft				$cnt--;
197501fa9147SAndy Whitcroft				#print "RAW<$rawlines[$ln - 1]>\n";
1976721c1cb6SAndy Whitcroft				last if (!defined $rawlines[$ln - 1]);
1977fae17daeSAndy Whitcroft				if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
1978fae17daeSAndy Whitcroft				    $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
1979fae17daeSAndy Whitcroft					($edge) = $1;
1980fae17daeSAndy Whitcroft					last;
1981fae17daeSAndy Whitcroft				}
1982773647a0SAndy Whitcroft			}
1983773647a0SAndy Whitcroft			if (defined $edge && $edge eq '*/') {
1984773647a0SAndy Whitcroft				$in_comment = 1;
1985773647a0SAndy Whitcroft			}
1986773647a0SAndy Whitcroft
1987773647a0SAndy Whitcroft			# Guestimate if this is a continuing comment.  If this
1988773647a0SAndy Whitcroft			# is the start of a diff block and this line starts
1989773647a0SAndy Whitcroft			# ' *' then it is very likely a comment.
1990773647a0SAndy Whitcroft			if (!defined $edge &&
199183242e0cSAndy Whitcroft			    $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
1992773647a0SAndy Whitcroft			{
1993773647a0SAndy Whitcroft				$in_comment = 1;
1994773647a0SAndy Whitcroft			}
1995773647a0SAndy Whitcroft
1996773647a0SAndy Whitcroft			##print "COMMENT:$in_comment edge<$edge> $rawline\n";
1997773647a0SAndy Whitcroft			sanitise_line_reset($in_comment);
1998773647a0SAndy Whitcroft
1999171ae1a4SAndy Whitcroft		} elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
2000773647a0SAndy Whitcroft			# Standardise the strings and chars within the input to
2001171ae1a4SAndy Whitcroft			# simplify matching -- only bother with positive lines.
2002773647a0SAndy Whitcroft			$line = sanitise_line($rawline);
2003773647a0SAndy Whitcroft		}
2004773647a0SAndy Whitcroft		push(@lines, $line);
2005773647a0SAndy Whitcroft
2006773647a0SAndy Whitcroft		if ($realcnt > 1) {
2007773647a0SAndy Whitcroft			$realcnt-- if ($line =~ /^(?:\+| |$)/);
2008773647a0SAndy Whitcroft		} else {
2009773647a0SAndy Whitcroft			$realcnt = 0;
2010773647a0SAndy Whitcroft		}
2011773647a0SAndy Whitcroft
2012773647a0SAndy Whitcroft		#print "==>$rawline\n";
2013773647a0SAndy Whitcroft		#print "-->$line\n";
2014de7d4f0eSAndy Whitcroft
2015de7d4f0eSAndy Whitcroft		if ($setup_docs && $line =~ /^\+/) {
2016de7d4f0eSAndy Whitcroft			push(@setup_docs, $line);
2017de7d4f0eSAndy Whitcroft		}
2018de7d4f0eSAndy Whitcroft	}
2019de7d4f0eSAndy Whitcroft
20206c72ffaaSAndy Whitcroft	$prefix = '';
20216c72ffaaSAndy Whitcroft
2022773647a0SAndy Whitcroft	$realcnt = 0;
2023773647a0SAndy Whitcroft	$linenr = 0;
2024194f66fcSJoe Perches	$fixlinenr = -1;
20250a920b5bSAndy Whitcroft	foreach my $line (@lines) {
20260a920b5bSAndy Whitcroft		$linenr++;
2027194f66fcSJoe Perches		$fixlinenr++;
20281b5539b1SJoe Perches		my $sline = $line;	#copy of $line
20291b5539b1SJoe Perches		$sline =~ s/$;/ /g;	#with comments as spaces
20300a920b5bSAndy Whitcroft
2031c2fdda0dSAndy Whitcroft		my $rawline = $rawlines[$linenr - 1];
20326c72ffaaSAndy Whitcroft
20330a920b5bSAndy Whitcroft#extract the line range in the file after the patch is applied
20346c72ffaaSAndy Whitcroft		if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
20350a920b5bSAndy Whitcroft			$is_patch = 1;
20364a0df2efSAndy Whitcroft			$first_line = $linenr + 1;
20370a920b5bSAndy Whitcroft			$realline=$1-1;
20380a920b5bSAndy Whitcroft			if (defined $2) {
20390a920b5bSAndy Whitcroft				$realcnt=$3+1;
20400a920b5bSAndy Whitcroft			} else {
20410a920b5bSAndy Whitcroft				$realcnt=1+1;
20420a920b5bSAndy Whitcroft			}
2043c2fdda0dSAndy Whitcroft			annotate_reset();
204413214adfSAndy Whitcroft			$prev_values = 'E';
204513214adfSAndy Whitcroft
2046773647a0SAndy Whitcroft			%suppress_ifbraces = ();
2047170d3a22SAndy Whitcroft			%suppress_whiletrailers = ();
20482b474a1aSAndy Whitcroft			%suppress_export = ();
20493e469cdcSAndy Whitcroft			$suppress_statement = 0;
20500a920b5bSAndy Whitcroft			next;
20510a920b5bSAndy Whitcroft
20524a0df2efSAndy Whitcroft# track the line number as we move through the hunk, note that
20534a0df2efSAndy Whitcroft# new versions of GNU diff omit the leading space on completely
20544a0df2efSAndy Whitcroft# blank context lines so we need to count that too.
2055773647a0SAndy Whitcroft		} elsif ($line =~ /^( |\+|$)/) {
20560a920b5bSAndy Whitcroft			$realline++;
2057d8aaf121SAndy Whitcroft			$realcnt-- if ($realcnt != 0);
20580a920b5bSAndy Whitcroft
20594a0df2efSAndy Whitcroft			# Measure the line length and indent.
2060c2fdda0dSAndy Whitcroft			($length, $indent) = line_stats($rawline);
20610a920b5bSAndy Whitcroft
20620a920b5bSAndy Whitcroft			# Track the previous line.
20630a920b5bSAndy Whitcroft			($prevline, $stashline) = ($stashline, $line);
20640a920b5bSAndy Whitcroft			($previndent, $stashindent) = ($stashindent, $indent);
2065c2fdda0dSAndy Whitcroft			($prevrawline, $stashrawline) = ($stashrawline, $rawline);
2066c2fdda0dSAndy Whitcroft
2067773647a0SAndy Whitcroft			#warn "line<$line>\n";
20686c72ffaaSAndy Whitcroft
2069d8aaf121SAndy Whitcroft		} elsif ($realcnt == 1) {
2070d8aaf121SAndy Whitcroft			$realcnt--;
20710a920b5bSAndy Whitcroft		}
20720a920b5bSAndy Whitcroft
2073cc77cdcaSAndy Whitcroft		my $hunk_line = ($realcnt != 0);
2074cc77cdcaSAndy Whitcroft
20750a920b5bSAndy Whitcroft#make up the handle for any error we report on this line
2076773647a0SAndy Whitcroft		$prefix = "$filename:$realline: " if ($emacs && $file);
2077773647a0SAndy Whitcroft		$prefix = "$filename:$linenr: " if ($emacs && !$file);
2078773647a0SAndy Whitcroft
20796c72ffaaSAndy Whitcroft		$here = "#$linenr: " if (!$file);
20806c72ffaaSAndy Whitcroft		$here = "#$realline: " if ($file);
2081773647a0SAndy Whitcroft
20822ac73b4fSJoe Perches		my $found_file = 0;
2083773647a0SAndy Whitcroft		# extract the filename as it passes
20843bf9a009SRabin Vincent		if ($line =~ /^diff --git.*?(\S+)$/) {
20853bf9a009SRabin Vincent			$realfile = $1;
20862b7ab453SJoe Perches			$realfile =~ s@^([^/]*)/@@ if (!$file);
2087270c49a0SJoe Perches			$in_commit_log = 0;
20882ac73b4fSJoe Perches			$found_file = 1;
20893bf9a009SRabin Vincent		} elsif ($line =~ /^\+\+\+\s+(\S+)/) {
2090773647a0SAndy Whitcroft			$realfile = $1;
20912b7ab453SJoe Perches			$realfile =~ s@^([^/]*)/@@ if (!$file);
2092270c49a0SJoe Perches			$in_commit_log = 0;
20931e855726SWolfram Sang
20941e855726SWolfram Sang			$p1_prefix = $1;
2095e2f7aa4bSAndy Whitcroft			if (!$file && $tree && $p1_prefix ne '' &&
2096e2f7aa4bSAndy Whitcroft			    -e "$root/$p1_prefix") {
2097000d1cc1SJoe Perches				WARN("PATCH_PREFIX",
2098000d1cc1SJoe Perches				     "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
20991e855726SWolfram Sang			}
2100773647a0SAndy Whitcroft
2101c1ab3326SAndy Whitcroft			if ($realfile =~ m@^include/asm/@) {
2102000d1cc1SJoe Perches				ERROR("MODIFIED_INCLUDE_ASM",
2103000d1cc1SJoe Perches				      "do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
2104773647a0SAndy Whitcroft			}
21052ac73b4fSJoe Perches			$found_file = 1;
21062ac73b4fSJoe Perches		}
21072ac73b4fSJoe Perches
21082ac73b4fSJoe Perches		if ($found_file) {
21092ac73b4fSJoe Perches			if ($realfile =~ m@^(drivers/net/|net/)@) {
21102ac73b4fSJoe Perches				$check = 1;
21112ac73b4fSJoe Perches			} else {
21122ac73b4fSJoe Perches				$check = $check_orig;
21132ac73b4fSJoe Perches			}
2114773647a0SAndy Whitcroft			next;
2115773647a0SAndy Whitcroft		}
2116773647a0SAndy Whitcroft
2117389834b6SRandy Dunlap		$here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
21180a920b5bSAndy Whitcroft
2119c2fdda0dSAndy Whitcroft		my $hereline = "$here\n$rawline\n";
2120c2fdda0dSAndy Whitcroft		my $herecurr = "$here\n$rawline\n";
2121c2fdda0dSAndy Whitcroft		my $hereprev = "$here\n$prevrawline\n$rawline\n";
21220a920b5bSAndy Whitcroft
21236c72ffaaSAndy Whitcroft		$cnt_lines++ if ($realcnt != 0);
21246c72ffaaSAndy Whitcroft
21253bf9a009SRabin Vincent# Check for incorrect file permissions
21263bf9a009SRabin Vincent		if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
21273bf9a009SRabin Vincent			my $permhere = $here . "FILE: $realfile\n";
212804db4d25SJoe Perches			if ($realfile !~ m@scripts/@ &&
212904db4d25SJoe Perches			    $realfile !~ /\.(py|pl|awk|sh)$/) {
2130000d1cc1SJoe Perches				ERROR("EXECUTE_PERMISSIONS",
2131000d1cc1SJoe Perches				      "do not set execute permissions for source files\n" . $permhere);
21323bf9a009SRabin Vincent			}
21333bf9a009SRabin Vincent		}
21343bf9a009SRabin Vincent
213520112475SJoe Perches# Check the patch for a signoff:
2136d8aaf121SAndy Whitcroft		if ($line =~ /^\s*signed-off-by:/i) {
21374a0df2efSAndy Whitcroft			$signoff++;
213815662b3eSJoe Perches			$in_commit_log = 0;
21390a920b5bSAndy Whitcroft		}
214020112475SJoe Perches
2141e0d975b1SJoe Perches# Check if MAINTAINERS is being updated.  If so, there's probably no need to
2142e0d975b1SJoe Perches# emit the "does MAINTAINERS need updating?" message on file add/move/delete
2143e0d975b1SJoe Perches		if ($line =~ /^\s*MAINTAINERS\s*\|/) {
2144e0d975b1SJoe Perches			$reported_maintainer_file = 1;
2145e0d975b1SJoe Perches		}
2146e0d975b1SJoe Perches
214720112475SJoe Perches# Check signature styles
2148270c49a0SJoe Perches		if (!$in_header_lines &&
2149ce0338dfSJoe Perches		    $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
215020112475SJoe Perches			my $space_before = $1;
215120112475SJoe Perches			my $sign_off = $2;
215220112475SJoe Perches			my $space_after = $3;
215320112475SJoe Perches			my $email = $4;
215420112475SJoe Perches			my $ucfirst_sign_off = ucfirst(lc($sign_off));
215520112475SJoe Perches
2156ce0338dfSJoe Perches			if ($sign_off !~ /$signature_tags/) {
2157ce0338dfSJoe Perches				WARN("BAD_SIGN_OFF",
2158ce0338dfSJoe Perches				     "Non-standard signature: $sign_off\n" . $herecurr);
2159ce0338dfSJoe Perches			}
216020112475SJoe Perches			if (defined $space_before && $space_before ne "") {
21613705ce5bSJoe Perches				if (WARN("BAD_SIGN_OFF",
21623705ce5bSJoe Perches					 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
21633705ce5bSJoe Perches				    $fix) {
2164194f66fcSJoe Perches					$fixed[$fixlinenr] =
21653705ce5bSJoe Perches					    "$ucfirst_sign_off $email";
21663705ce5bSJoe Perches				}
216720112475SJoe Perches			}
216820112475SJoe Perches			if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
21693705ce5bSJoe Perches				if (WARN("BAD_SIGN_OFF",
21703705ce5bSJoe Perches					 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
21713705ce5bSJoe Perches				    $fix) {
2172194f66fcSJoe Perches					$fixed[$fixlinenr] =
21733705ce5bSJoe Perches					    "$ucfirst_sign_off $email";
21743705ce5bSJoe Perches				}
21753705ce5bSJoe Perches
217620112475SJoe Perches			}
217720112475SJoe Perches			if (!defined $space_after || $space_after ne " ") {
21783705ce5bSJoe Perches				if (WARN("BAD_SIGN_OFF",
21793705ce5bSJoe Perches					 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
21803705ce5bSJoe Perches				    $fix) {
2181194f66fcSJoe Perches					$fixed[$fixlinenr] =
21823705ce5bSJoe Perches					    "$ucfirst_sign_off $email";
21833705ce5bSJoe Perches				}
218420112475SJoe Perches			}
218520112475SJoe Perches
218620112475SJoe Perches			my ($email_name, $email_address, $comment) = parse_email($email);
218720112475SJoe Perches			my $suggested_email = format_email(($email_name, $email_address));
218820112475SJoe Perches			if ($suggested_email eq "") {
2189000d1cc1SJoe Perches				ERROR("BAD_SIGN_OFF",
2190000d1cc1SJoe Perches				      "Unrecognized email address: '$email'\n" . $herecurr);
219120112475SJoe Perches			} else {
219220112475SJoe Perches				my $dequoted = $suggested_email;
219320112475SJoe Perches				$dequoted =~ s/^"//;
219420112475SJoe Perches				$dequoted =~ s/" </ </;
219520112475SJoe Perches				# Don't force email to have quotes
219620112475SJoe Perches				# Allow just an angle bracketed address
219720112475SJoe Perches				if ("$dequoted$comment" ne $email &&
219820112475SJoe Perches				    "<$email_address>$comment" ne $email &&
219920112475SJoe Perches				    "$suggested_email$comment" ne $email) {
2200000d1cc1SJoe Perches					WARN("BAD_SIGN_OFF",
2201000d1cc1SJoe Perches					     "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
220220112475SJoe Perches				}
22030a920b5bSAndy Whitcroft			}
22047e51f197SJoe Perches
22057e51f197SJoe Perches# Check for duplicate signatures
22067e51f197SJoe Perches			my $sig_nospace = $line;
22077e51f197SJoe Perches			$sig_nospace =~ s/\s//g;
22087e51f197SJoe Perches			$sig_nospace = lc($sig_nospace);
22097e51f197SJoe Perches			if (defined $signatures{$sig_nospace}) {
22107e51f197SJoe Perches				WARN("BAD_SIGN_OFF",
22117e51f197SJoe Perches				     "Duplicate signature\n" . $herecurr);
22127e51f197SJoe Perches			} else {
22137e51f197SJoe Perches				$signatures{$sig_nospace} = 1;
22147e51f197SJoe Perches			}
22150a920b5bSAndy Whitcroft		}
22160a920b5bSAndy Whitcroft
2217a2fe16b9SJoe Perches# Check email subject for common tools that don't need to be mentioned
2218a2fe16b9SJoe Perches		if ($in_header_lines &&
2219a2fe16b9SJoe Perches		    $line =~ /^Subject:.*\b(?:checkpatch|sparse|smatch)\b[^:]/i) {
2220a2fe16b9SJoe Perches			WARN("EMAIL_SUBJECT",
2221a2fe16b9SJoe Perches			     "A patch subject line should describe the change not the tool that found it\n" . $herecurr);
2222a2fe16b9SJoe Perches		}
2223a2fe16b9SJoe Perches
22249b3189ebSJoe Perches# Check for old stable address
22259b3189ebSJoe Perches		if ($line =~ /^\s*cc:\s*.*<?\bstable\@kernel\.org\b>?.*$/i) {
22269b3189ebSJoe Perches			ERROR("STABLE_ADDRESS",
22279b3189ebSJoe Perches			      "The 'stable' address should be 'stable\@vger.kernel.org'\n" . $herecurr);
22289b3189ebSJoe Perches		}
22299b3189ebSJoe Perches
22307ebd05efSChristopher Covington# Check for unwanted Gerrit info
22317ebd05efSChristopher Covington		if ($in_commit_log && $line =~ /^\s*change-id:/i) {
22327ebd05efSChristopher Covington			ERROR("GERRIT_CHANGE_ID",
22337ebd05efSChristopher Covington			      "Remove Gerrit Change-Id's before submitting upstream.\n" . $herecurr);
22347ebd05efSChristopher Covington		}
22357ebd05efSChristopher Covington
22360d7835fcSJoe Perches# Check for git id commit length and improperly formed commit descriptions
22370d7835fcSJoe Perches		if ($in_commit_log && $line =~ /\b(c)ommit\s+([0-9a-f]{5,})/i) {
2238d311cd44SJoe Perches			my $init_char = $1;
2239d311cd44SJoe Perches			my $orig_commit = lc($2);
22400d7835fcSJoe Perches			my $short = 1;
22410d7835fcSJoe Perches			my $long = 0;
22420d7835fcSJoe Perches			my $case = 1;
22430d7835fcSJoe Perches			my $space = 1;
22440d7835fcSJoe Perches			my $hasdesc = 0;
224519c146a6SJoe Perches			my $hasparens = 0;
22460d7835fcSJoe Perches			my $id = '0123456789ab';
22470d7835fcSJoe Perches			my $orig_desc = "commit description";
22480d7835fcSJoe Perches			my $description = "";
22490d7835fcSJoe Perches
22500d7835fcSJoe Perches			$short = 0 if ($line =~ /\bcommit\s+[0-9a-f]{12,40}/i);
22510d7835fcSJoe Perches			$long = 1 if ($line =~ /\bcommit\s+[0-9a-f]{41,}/i);
22520d7835fcSJoe Perches			$space = 0 if ($line =~ /\bcommit [0-9a-f]/i);
22530d7835fcSJoe Perches			$case = 0 if ($line =~ /\b[Cc]ommit\s+[0-9a-f]{5,40}[^A-F]/);
22540d7835fcSJoe Perches			if ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)"\)/i) {
22550d7835fcSJoe Perches				$orig_desc = $1;
225619c146a6SJoe Perches				$hasparens = 1;
22570d7835fcSJoe Perches			} elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s*$/i &&
22580d7835fcSJoe Perches				 defined $rawlines[$linenr] &&
22590d7835fcSJoe Perches				 $rawlines[$linenr] =~ /^\s*\("([^"]+)"\)/) {
22600d7835fcSJoe Perches				$orig_desc = $1;
226119c146a6SJoe Perches				$hasparens = 1;
2262b671fde0SJoe Perches			} elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("[^"]+$/i &&
2263b671fde0SJoe Perches				 defined $rawlines[$linenr] &&
2264b671fde0SJoe Perches				 $rawlines[$linenr] =~ /^\s*[^"]+"\)/) {
2265b671fde0SJoe Perches				$line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)$/i;
2266b671fde0SJoe Perches				$orig_desc = $1;
2267b671fde0SJoe Perches				$rawlines[$linenr] =~ /^\s*([^"]+)"\)/;
2268b671fde0SJoe Perches				$orig_desc .= " " . $1;
226919c146a6SJoe Perches				$hasparens = 1;
22700d7835fcSJoe Perches			}
22710d7835fcSJoe Perches
22720d7835fcSJoe Perches			($id, $description) = git_commit_info($orig_commit,
22730d7835fcSJoe Perches							      $id, $orig_desc);
22740d7835fcSJoe Perches
227519c146a6SJoe Perches			if ($short || $long || $space || $case || ($orig_desc ne $description) || !$hasparens) {
2276d311cd44SJoe Perches				ERROR("GIT_COMMIT_ID",
22770d7835fcSJoe Perches				      "Please use git commit description style 'commit <12+ chars of sha1> (\"<title line>\")' - ie: '${init_char}ommit $id (\"$description\")'\n" . $herecurr);
22780d7835fcSJoe Perches			}
2279d311cd44SJoe Perches		}
2280d311cd44SJoe Perches
228113f1937eSJoe Perches# Check for added, moved or deleted files
228213f1937eSJoe Perches		if (!$reported_maintainer_file && !$in_commit_log &&
228313f1937eSJoe Perches		    ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ ||
228413f1937eSJoe Perches		     $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ ||
228513f1937eSJoe Perches		     ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ &&
228613f1937eSJoe Perches		      (defined($1) || defined($2))))) {
228713f1937eSJoe Perches			$reported_maintainer_file = 1;
228813f1937eSJoe Perches			WARN("FILE_PATH_CHANGES",
228913f1937eSJoe Perches			     "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr);
229013f1937eSJoe Perches		}
229113f1937eSJoe Perches
229200df344fSAndy Whitcroft# Check for wrappage within a valid hunk of the file
22938905a67cSAndy Whitcroft		if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
2294000d1cc1SJoe Perches			ERROR("CORRUPTED_PATCH",
2295000d1cc1SJoe Perches			      "patch seems to be corrupt (line wrapped?)\n" .
22966c72ffaaSAndy Whitcroft				$herecurr) if (!$emitted_corrupt++);
2297de7d4f0eSAndy Whitcroft		}
2298de7d4f0eSAndy Whitcroft
22996ecd9674SAndy Whitcroft# Check for absolute kernel paths.
23006ecd9674SAndy Whitcroft		if ($tree) {
23016ecd9674SAndy Whitcroft			while ($line =~ m{(?:^|\s)(/\S*)}g) {
23026ecd9674SAndy Whitcroft				my $file = $1;
23036ecd9674SAndy Whitcroft
23046ecd9674SAndy Whitcroft				if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
23056ecd9674SAndy Whitcroft				    check_absolute_file($1, $herecurr)) {
23066ecd9674SAndy Whitcroft					#
23076ecd9674SAndy Whitcroft				} else {
23086ecd9674SAndy Whitcroft					check_absolute_file($file, $herecurr);
23096ecd9674SAndy Whitcroft				}
23106ecd9674SAndy Whitcroft			}
23116ecd9674SAndy Whitcroft		}
23126ecd9674SAndy Whitcroft
2313de7d4f0eSAndy Whitcroft# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
2314de7d4f0eSAndy Whitcroft		if (($realfile =~ /^$/ || $line =~ /^\+/) &&
2315171ae1a4SAndy Whitcroft		    $rawline !~ m/^$UTF8*$/) {
2316171ae1a4SAndy Whitcroft			my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
2317171ae1a4SAndy Whitcroft
2318171ae1a4SAndy Whitcroft			my $blank = copy_spacing($rawline);
2319171ae1a4SAndy Whitcroft			my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
2320171ae1a4SAndy Whitcroft			my $hereptr = "$hereline$ptr\n";
2321171ae1a4SAndy Whitcroft
232234d99219SJoe Perches			CHK("INVALID_UTF8",
2323000d1cc1SJoe Perches			    "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
232400df344fSAndy Whitcroft		}
23250a920b5bSAndy Whitcroft
232615662b3eSJoe Perches# Check if it's the start of a commit log
232715662b3eSJoe Perches# (not a header line and we haven't seen the patch filename)
232815662b3eSJoe Perches		if ($in_header_lines && $realfile =~ /^$/ &&
232929ee1b0cSJoe Perches		    !($rawline =~ /^\s+\S/ ||
233029ee1b0cSJoe Perches		      $rawline =~ /^(commit\b|from\b|[\w-]+:).*$/i)) {
233115662b3eSJoe Perches			$in_header_lines = 0;
233215662b3eSJoe Perches			$in_commit_log = 1;
233315662b3eSJoe Perches		}
233415662b3eSJoe Perches
2335fa64205dSPasi Savanainen# Check if there is UTF-8 in a commit log when a mail header has explicitly
2336fa64205dSPasi Savanainen# declined it, i.e defined some charset where it is missing.
2337fa64205dSPasi Savanainen		if ($in_header_lines &&
2338fa64205dSPasi Savanainen		    $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
2339fa64205dSPasi Savanainen		    $1 !~ /utf-8/i) {
2340fa64205dSPasi Savanainen			$non_utf8_charset = 1;
2341fa64205dSPasi Savanainen		}
2342fa64205dSPasi Savanainen
2343fa64205dSPasi Savanainen		if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
234415662b3eSJoe Perches		    $rawline =~ /$NON_ASCII_UTF8/) {
2345fa64205dSPasi Savanainen			WARN("UTF8_BEFORE_PATCH",
234615662b3eSJoe Perches			    "8-bit UTF-8 used in possible commit log\n" . $herecurr);
234715662b3eSJoe Perches		}
234815662b3eSJoe Perches
234966b47b4aSKees Cook# Check for various typo / spelling mistakes
235066d7a382SJoe Perches		if (defined($misspellings) &&
235166d7a382SJoe Perches		    ($in_commit_log || $line =~ /^(?:\+|Subject:)/i)) {
2352ebfd7d62SJoe Perches			while ($rawline =~ /(?:^|[^a-z@])($misspellings)(?:\b|$|[^a-z@])/gi) {
235366b47b4aSKees Cook				my $typo = $1;
235466b47b4aSKees Cook				my $typo_fix = $spelling_fix{lc($typo)};
235566b47b4aSKees Cook				$typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/);
235666b47b4aSKees Cook				$typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/);
235766b47b4aSKees Cook				my $msg_type = \&WARN;
235866b47b4aSKees Cook				$msg_type = \&CHK if ($file);
235966b47b4aSKees Cook				if (&{$msg_type}("TYPO_SPELLING",
236066b47b4aSKees Cook						 "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $herecurr) &&
236166b47b4aSKees Cook				    $fix) {
236266b47b4aSKees Cook					$fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/;
236366b47b4aSKees Cook				}
236466b47b4aSKees Cook			}
236566b47b4aSKees Cook		}
236666b47b4aSKees Cook
236730670854SAndy Whitcroft# ignore non-hunk lines and lines being removed
236830670854SAndy Whitcroft		next if (!$hunk_line || $line =~ /^-/);
236900df344fSAndy Whitcroft
23700a920b5bSAndy Whitcroft#trailing whitespace
23719c0ca6f9SAndy Whitcroft		if ($line =~ /^\+.*\015/) {
2372c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2373d5e616fcSJoe Perches			if (ERROR("DOS_LINE_ENDINGS",
2374d5e616fcSJoe Perches				  "DOS line endings\n" . $herevet) &&
2375d5e616fcSJoe Perches			    $fix) {
2376194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/[\s\015]+$//;
2377d5e616fcSJoe Perches			}
2378c2fdda0dSAndy Whitcroft		} elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
2379c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
23803705ce5bSJoe Perches			if (ERROR("TRAILING_WHITESPACE",
23813705ce5bSJoe Perches				  "trailing whitespace\n" . $herevet) &&
23823705ce5bSJoe Perches			    $fix) {
2383194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\s+$//;
23843705ce5bSJoe Perches			}
23853705ce5bSJoe Perches
2386d2c0a235SAndy Whitcroft			$rpt_cleaners = 1;
23870a920b5bSAndy Whitcroft		}
23885368df20SAndy Whitcroft
23894783f894SJosh Triplett# Check for FSF mailing addresses.
2390109d8cb2SAlexander Duyck		if ($rawline =~ /\bwrite to the Free/i ||
23913e2232f2SJoe Perches		    $rawline =~ /\b59\s+Temple\s+Pl/i ||
23923e2232f2SJoe Perches		    $rawline =~ /\b51\s+Franklin\s+St/i) {
23934783f894SJosh Triplett			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
23944783f894SJosh Triplett			my $msg_type = \&ERROR;
23954783f894SJosh Triplett			$msg_type = \&CHK if ($file);
23964783f894SJosh Triplett			&{$msg_type}("FSF_MAILING_ADDRESS",
23974783f894SJosh 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)
23984783f894SJosh Triplett		}
23994783f894SJosh Triplett
24003354957aSAndi Kleen# check for Kconfig help text having a real description
24019fe287d7SAndy Whitcroft# Only applies when adding the entry originally, after that we do not have
24029fe287d7SAndy Whitcroft# sufficient context to determine whether it is indeed long enough.
24033354957aSAndi Kleen		if ($realfile =~ /Kconfig/ &&
24048d73e0e7SJoe Perches		    $line =~ /^\+\s*config\s+/) {
24053354957aSAndi Kleen			my $length = 0;
24069fe287d7SAndy Whitcroft			my $cnt = $realcnt;
24079fe287d7SAndy Whitcroft			my $ln = $linenr + 1;
24089fe287d7SAndy Whitcroft			my $f;
2409a1385803SAndy Whitcroft			my $is_start = 0;
24109fe287d7SAndy Whitcroft			my $is_end = 0;
2411a1385803SAndy Whitcroft			for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
24129fe287d7SAndy Whitcroft				$f = $lines[$ln - 1];
24139fe287d7SAndy Whitcroft				$cnt-- if ($lines[$ln - 1] !~ /^-/);
24149fe287d7SAndy Whitcroft				$is_end = $lines[$ln - 1] =~ /^\+/;
24159fe287d7SAndy Whitcroft
24169fe287d7SAndy Whitcroft				next if ($f =~ /^-/);
24178d73e0e7SJoe Perches				last if (!$file && $f =~ /^\@\@/);
2418a1385803SAndy Whitcroft
24198d73e0e7SJoe Perches				if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate)\s*\"/) {
2420a1385803SAndy Whitcroft					$is_start = 1;
24218d73e0e7SJoe Perches				} elsif ($lines[$ln - 1] =~ /^\+\s*(?:---)?help(?:---)?$/) {
2422a1385803SAndy Whitcroft					$length = -1;
2423a1385803SAndy Whitcroft				}
2424a1385803SAndy Whitcroft
24259fe287d7SAndy Whitcroft				$f =~ s/^.//;
24263354957aSAndi Kleen				$f =~ s/#.*//;
24273354957aSAndi Kleen				$f =~ s/^\s+//;
24283354957aSAndi Kleen				next if ($f =~ /^$/);
24299fe287d7SAndy Whitcroft				if ($f =~ /^\s*config\s/) {
24309fe287d7SAndy Whitcroft					$is_end = 1;
24319fe287d7SAndy Whitcroft					last;
24329fe287d7SAndy Whitcroft				}
24333354957aSAndi Kleen				$length++;
24343354957aSAndi Kleen			}
243556193274SVadim Bendebury			if ($is_start && $is_end && $length < $min_conf_desc_length) {
2436000d1cc1SJoe Perches				WARN("CONFIG_DESCRIPTION",
243756193274SVadim Bendebury				     "please write a paragraph that describes the config symbol fully\n" . $herecurr);
243856193274SVadim Bendebury			}
2439a1385803SAndy Whitcroft			#print "is_start<$is_start> is_end<$is_end> length<$length>\n";
24403354957aSAndi Kleen		}
24413354957aSAndi Kleen
24421ba8dfd1SKees Cook# discourage the addition of CONFIG_EXPERIMENTAL in Kconfig.
24431ba8dfd1SKees Cook		if ($realfile =~ /Kconfig/ &&
24441ba8dfd1SKees Cook		    $line =~ /.\s*depends on\s+.*\bEXPERIMENTAL\b/) {
24451ba8dfd1SKees Cook			WARN("CONFIG_EXPERIMENTAL",
24461ba8dfd1SKees Cook			     "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
24471ba8dfd1SKees Cook		}
24481ba8dfd1SKees Cook
2449327953e9SChristoph Jaeger# discourage the use of boolean for type definition attributes of Kconfig options
2450327953e9SChristoph Jaeger		if ($realfile =~ /Kconfig/ &&
2451327953e9SChristoph Jaeger		    $line =~ /^\+\s*\bboolean\b/) {
2452327953e9SChristoph Jaeger			WARN("CONFIG_TYPE_BOOLEAN",
2453327953e9SChristoph Jaeger			     "Use of boolean is deprecated, please use bool instead.\n" . $herecurr);
2454327953e9SChristoph Jaeger		}
2455327953e9SChristoph Jaeger
2456c68e5878SArnaud Lacombe		if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
2457c68e5878SArnaud Lacombe		    ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
2458c68e5878SArnaud Lacombe			my $flag = $1;
2459c68e5878SArnaud Lacombe			my $replacement = {
2460c68e5878SArnaud Lacombe				'EXTRA_AFLAGS' =>   'asflags-y',
2461c68e5878SArnaud Lacombe				'EXTRA_CFLAGS' =>   'ccflags-y',
2462c68e5878SArnaud Lacombe				'EXTRA_CPPFLAGS' => 'cppflags-y',
2463c68e5878SArnaud Lacombe				'EXTRA_LDFLAGS' =>  'ldflags-y',
2464c68e5878SArnaud Lacombe			};
2465c68e5878SArnaud Lacombe
2466c68e5878SArnaud Lacombe			WARN("DEPRECATED_VARIABLE",
2467c68e5878SArnaud Lacombe			     "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
2468c68e5878SArnaud Lacombe		}
2469c68e5878SArnaud Lacombe
2470bff5da43SRob Herring# check for DT compatible documentation
24717dd05b38SFlorian Vaussard		if (defined $root &&
24727dd05b38SFlorian Vaussard			(($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) ||
24737dd05b38SFlorian Vaussard			 ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) {
24747dd05b38SFlorian Vaussard
2475bff5da43SRob Herring			my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g;
2476bff5da43SRob Herring
2477cc93319bSFlorian Vaussard			my $dt_path = $root . "/Documentation/devicetree/bindings/";
2478cc93319bSFlorian Vaussard			my $vp_file = $dt_path . "vendor-prefixes.txt";
2479cc93319bSFlorian Vaussard
2480bff5da43SRob Herring			foreach my $compat (@compats) {
2481bff5da43SRob Herring				my $compat2 = $compat;
2482185d566bSRob Herring				$compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/;
2483185d566bSRob Herring				my $compat3 = $compat;
2484185d566bSRob Herring				$compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/;
2485185d566bSRob Herring				`grep -Erq "$compat|$compat2|$compat3" $dt_path`;
2486bff5da43SRob Herring				if ( $? >> 8 ) {
2487bff5da43SRob Herring					WARN("UNDOCUMENTED_DT_STRING",
2488bff5da43SRob Herring					     "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr);
2489bff5da43SRob Herring				}
2490bff5da43SRob Herring
24914fbf32a6SFlorian Vaussard				next if $compat !~ /^([a-zA-Z0-9\-]+)\,/;
24924fbf32a6SFlorian Vaussard				my $vendor = $1;
2493cc93319bSFlorian Vaussard				`grep -Eq "^$vendor\\b" $vp_file`;
2494bff5da43SRob Herring				if ( $? >> 8 ) {
2495bff5da43SRob Herring					WARN("UNDOCUMENTED_DT_STRING",
2496cc93319bSFlorian Vaussard					     "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr);
2497bff5da43SRob Herring				}
2498bff5da43SRob Herring			}
2499bff5da43SRob Herring		}
2500bff5da43SRob Herring
25015368df20SAndy Whitcroft# check we are in a valid source file if not then ignore this hunk
2502de4c924cSGeert Uytterhoeven		next if ($realfile !~ /\.(h|c|s|S|pl|sh|dtsi|dts)$/);
25035368df20SAndy Whitcroft
25046cd7f386SJoe Perches#line length limit
2505c45dcabdSAndy Whitcroft		if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ &&
2506f4c014c0SAndy Whitcroft		    $rawline !~ /^.\s*\*\s*\@$Ident\s/ &&
25070fccc622SJoe Perches		    !($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(KERN_\S+\s*|[^"]*))?"[X\t]*"\s*(?:|,|\)\s*;)\s*$/ ||
25088bbea968SJoe Perches		    $line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) &&
25096cd7f386SJoe Perches		    $length > $max_line_length)
2510c45dcabdSAndy Whitcroft		{
2511000d1cc1SJoe Perches			WARN("LONG_LINE",
25126cd7f386SJoe Perches			     "line over $max_line_length characters\n" . $herecurr);
25130a920b5bSAndy Whitcroft		}
25140a920b5bSAndy Whitcroft
25158905a67cSAndy Whitcroft# check for adding lines without a newline.
25168905a67cSAndy Whitcroft		if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
2517000d1cc1SJoe Perches			WARN("MISSING_EOF_NEWLINE",
2518000d1cc1SJoe Perches			     "adding a line without newline at end of file\n" . $herecurr);
25198905a67cSAndy Whitcroft		}
25208905a67cSAndy Whitcroft
252142e41c54SMike Frysinger# Blackfin: use hi/lo macros
252242e41c54SMike Frysinger		if ($realfile =~ m@arch/blackfin/.*\.S$@) {
252342e41c54SMike Frysinger			if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
252442e41c54SMike Frysinger				my $herevet = "$here\n" . cat_vet($line) . "\n";
2525000d1cc1SJoe Perches				ERROR("LO_MACRO",
2526000d1cc1SJoe Perches				      "use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
252742e41c54SMike Frysinger			}
252842e41c54SMike Frysinger			if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
252942e41c54SMike Frysinger				my $herevet = "$here\n" . cat_vet($line) . "\n";
2530000d1cc1SJoe Perches				ERROR("HI_MACRO",
2531000d1cc1SJoe Perches				      "use the HI() macro, not (... >> 16)\n" . $herevet);
253242e41c54SMike Frysinger			}
253342e41c54SMike Frysinger		}
253442e41c54SMike Frysinger
2535b9ea10d6SAndy Whitcroft# check we are in a valid source file C or perl if not then ignore this hunk
2536de4c924cSGeert Uytterhoeven		next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/);
25370a920b5bSAndy Whitcroft
25380a920b5bSAndy Whitcroft# at the beginning of a line any tabs must come first and anything
25390a920b5bSAndy Whitcroft# more than 8 must use tabs.
2540c2fdda0dSAndy Whitcroft		if ($rawline =~ /^\+\s* \t\s*\S/ ||
2541c2fdda0dSAndy Whitcroft		    $rawline =~ /^\+\s*        \s*/) {
2542c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2543d2c0a235SAndy Whitcroft			$rpt_cleaners = 1;
25443705ce5bSJoe Perches			if (ERROR("CODE_INDENT",
25453705ce5bSJoe Perches				  "code indent should use tabs where possible\n" . $herevet) &&
25463705ce5bSJoe Perches			    $fix) {
2547194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
25483705ce5bSJoe Perches			}
25490a920b5bSAndy Whitcroft		}
25500a920b5bSAndy Whitcroft
255108e44365SAlberto Panizzo# check for space before tabs.
255208e44365SAlberto Panizzo		if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
255308e44365SAlberto Panizzo			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
25543705ce5bSJoe Perches			if (WARN("SPACE_BEFORE_TAB",
25553705ce5bSJoe Perches				"please, no space before tabs\n" . $herevet) &&
25563705ce5bSJoe Perches			    $fix) {
2557194f66fcSJoe Perches				while ($fixed[$fixlinenr] =~
2558d2207ccbSJoe Perches					   s/(^\+.*) {8,8}\t/$1\t\t/) {}
2559194f66fcSJoe Perches				while ($fixed[$fixlinenr] =~
2560c76f4cb3SJoe Perches					   s/(^\+.*) +\t/$1\t/) {}
25613705ce5bSJoe Perches			}
256208e44365SAlberto Panizzo		}
256308e44365SAlberto Panizzo
2564d1fe9c09SJoe Perches# check for && or || at the start of a line
2565d1fe9c09SJoe Perches		if ($rawline =~ /^\+\s*(&&|\|\|)/) {
2566d1fe9c09SJoe Perches			CHK("LOGICAL_CONTINUATIONS",
2567d1fe9c09SJoe Perches			    "Logical continuations should be on the previous line\n" . $hereprev);
2568d1fe9c09SJoe Perches		}
2569d1fe9c09SJoe Perches
2570d1fe9c09SJoe Perches# check multi-line statement indentation matches previous line
2571d1fe9c09SJoe Perches		if ($^V && $^V ge 5.10.0 &&
257291cb5195SJoe Perches		    $prevline =~ /^\+([ \t]*)((?:$c90_Keywords(?:\s+if)\s*)|(?:$Declare\s*)?(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*|$Ident\s*=\s*$Ident\s*)\(.*(\&\&|\|\||,)\s*$/) {
2573d1fe9c09SJoe Perches			$prevline =~ /^\+(\t*)(.*)$/;
2574d1fe9c09SJoe Perches			my $oldindent = $1;
2575d1fe9c09SJoe Perches			my $rest = $2;
2576d1fe9c09SJoe Perches
2577d1fe9c09SJoe Perches			my $pos = pos_last_openparen($rest);
2578d1fe9c09SJoe Perches			if ($pos >= 0) {
2579b34a26f3SJoe Perches				$line =~ /^(\+| )([ \t]*)/;
2580b34a26f3SJoe Perches				my $newindent = $2;
2581d1fe9c09SJoe Perches
2582d1fe9c09SJoe Perches				my $goodtabindent = $oldindent .
2583d1fe9c09SJoe Perches					"\t" x ($pos / 8) .
2584d1fe9c09SJoe Perches					" "  x ($pos % 8);
2585d1fe9c09SJoe Perches				my $goodspaceindent = $oldindent . " "  x $pos;
2586d1fe9c09SJoe Perches
2587d1fe9c09SJoe Perches				if ($newindent ne $goodtabindent &&
2588d1fe9c09SJoe Perches				    $newindent ne $goodspaceindent) {
25893705ce5bSJoe Perches
25903705ce5bSJoe Perches					if (CHK("PARENTHESIS_ALIGNMENT",
25913705ce5bSJoe Perches						"Alignment should match open parenthesis\n" . $hereprev) &&
25923705ce5bSJoe Perches					    $fix && $line =~ /^\+/) {
2593194f66fcSJoe Perches						$fixed[$fixlinenr] =~
25943705ce5bSJoe Perches						    s/^\+[ \t]*/\+$goodtabindent/;
25953705ce5bSJoe Perches					}
2596d1fe9c09SJoe Perches				}
2597d1fe9c09SJoe Perches			}
2598d1fe9c09SJoe Perches		}
2599d1fe9c09SJoe Perches
26006ab3a970SJoe Perches# check for space after cast like "(int) foo" or "(struct foo) bar"
26016ab3a970SJoe Perches# avoid checking a few false positives:
26026ab3a970SJoe Perches#   "sizeof(<type>)" or "__alignof__(<type>)"
26036ab3a970SJoe Perches#   function pointer declarations like "(*foo)(int) = bar;"
26046ab3a970SJoe Perches#   structure definitions like "(struct foo) { 0 };"
26056ab3a970SJoe Perches#   multiline macros that define functions
26066ab3a970SJoe Perches#   known attributes or the __attribute__ keyword
26076ab3a970SJoe Perches		if ($line =~ /^\+(.*)\(\s*$Type\s*\)([ \t]++)((?![={]|\\$|$Attribute|__attribute__))/ &&
26086ab3a970SJoe Perches		    (!defined($1) || $1 !~ /\b(?:sizeof|__alignof__)\s*$/)) {
26093705ce5bSJoe Perches			if (CHK("SPACING",
2610f27c95dbSJoe Perches				"No space is necessary after a cast\n" . $herecurr) &&
26113705ce5bSJoe Perches			    $fix) {
2612194f66fcSJoe Perches				$fixed[$fixlinenr] =~
2613f27c95dbSJoe Perches				    s/(\(\s*$Type\s*\))[ \t]+/$1/;
26143705ce5bSJoe Perches			}
2615aad4f614SJoe Perches		}
2616aad4f614SJoe Perches
261705880600SJoe Perches		if ($realfile =~ m@^(drivers/net/|net/)@ &&
2618fdb4bcd6SJoe Perches		    $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
261985ad978cSJoe Perches		    $rawline =~ /^\+[ \t]*\*/ &&
262085ad978cSJoe Perches		    $realline > 2) {
262105880600SJoe Perches			WARN("NETWORKING_BLOCK_COMMENT_STYLE",
262205880600SJoe Perches			     "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
262305880600SJoe Perches		}
262405880600SJoe Perches
262505880600SJoe Perches		if ($realfile =~ m@^(drivers/net/|net/)@ &&
2626a605e32eSJoe Perches		    $prevrawline =~ /^\+[ \t]*\/\*/ &&		#starting /*
2627a605e32eSJoe Perches		    $prevrawline !~ /\*\/[ \t]*$/ &&		#no trailing */
262861135e96SJoe Perches		    $rawline =~ /^\+/ &&			#line is new
2629a605e32eSJoe Perches		    $rawline !~ /^\+[ \t]*\*/) {		#no leading *
2630a605e32eSJoe Perches			WARN("NETWORKING_BLOCK_COMMENT_STYLE",
2631a605e32eSJoe Perches			     "networking block comments start with * on subsequent lines\n" . $hereprev);
2632a605e32eSJoe Perches		}
2633a605e32eSJoe Perches
2634a605e32eSJoe Perches		if ($realfile =~ m@^(drivers/net/|net/)@ &&
2635c24f9f19SJoe Perches		    $rawline !~ m@^\+[ \t]*\*/[ \t]*$@ &&	#trailing */
2636c24f9f19SJoe Perches		    $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ &&	#inline /*...*/
2637c24f9f19SJoe Perches		    $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ &&	#trailing **/
2638c24f9f19SJoe Perches		    $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) {	#non blank */
263905880600SJoe Perches			WARN("NETWORKING_BLOCK_COMMENT_STYLE",
264005880600SJoe Perches			     "networking block comments put the trailing */ on a separate line\n" . $herecurr);
264105880600SJoe Perches		}
264205880600SJoe Perches
26437f619191SJoe Perches# check for missing blank lines after struct/union declarations
26447f619191SJoe Perches# with exceptions for various attributes and macros
26457f619191SJoe Perches		if ($prevline =~ /^[\+ ]};?\s*$/ &&
26467f619191SJoe Perches		    $line =~ /^\+/ &&
26477f619191SJoe Perches		    !($line =~ /^\+\s*$/ ||
26487f619191SJoe Perches		      $line =~ /^\+\s*EXPORT_SYMBOL/ ||
26497f619191SJoe Perches		      $line =~ /^\+\s*MODULE_/i ||
26507f619191SJoe Perches		      $line =~ /^\+\s*\#\s*(?:end|elif|else)/ ||
26517f619191SJoe Perches		      $line =~ /^\+[a-z_]*init/ ||
26527f619191SJoe Perches		      $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ ||
26537f619191SJoe Perches		      $line =~ /^\+\s*DECLARE/ ||
26547f619191SJoe Perches		      $line =~ /^\+\s*__setup/)) {
2655d752fcc8SJoe Perches			if (CHK("LINE_SPACING",
2656d752fcc8SJoe Perches				"Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) &&
2657d752fcc8SJoe Perches			    $fix) {
2658f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, "\+");
2659d752fcc8SJoe Perches			}
26607f619191SJoe Perches		}
26617f619191SJoe Perches
2662365dd4eaSJoe Perches# check for multiple consecutive blank lines
2663365dd4eaSJoe Perches		if ($prevline =~ /^[\+ ]\s*$/ &&
2664365dd4eaSJoe Perches		    $line =~ /^\+\s*$/ &&
2665365dd4eaSJoe Perches		    $last_blank_line != ($linenr - 1)) {
2666d752fcc8SJoe Perches			if (CHK("LINE_SPACING",
2667d752fcc8SJoe Perches				"Please don't use multiple blank lines\n" . $hereprev) &&
2668d752fcc8SJoe Perches			    $fix) {
2669f2d7e4d4SJoe Perches				fix_delete_line($fixlinenr, $rawline);
2670d752fcc8SJoe Perches			}
2671d752fcc8SJoe Perches
2672365dd4eaSJoe Perches			$last_blank_line = $linenr;
2673365dd4eaSJoe Perches		}
2674365dd4eaSJoe Perches
26753b617e3bSJoe Perches# check for missing blank lines after declarations
26763f7bac03SJoe Perches		if ($sline =~ /^\+\s+\S/ &&			#Not at char 1
26773f7bac03SJoe Perches			# actual declarations
26783f7bac03SJoe Perches		    ($prevline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
26795a4e1fd3SJoe Perches			# function pointer declarations
26805a4e1fd3SJoe Perches		     $prevline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
26813f7bac03SJoe Perches			# foo bar; where foo is some local typedef or #define
26823f7bac03SJoe Perches		     $prevline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
26833f7bac03SJoe Perches			# known declaration macros
26843f7bac03SJoe Perches		     $prevline =~ /^\+\s+$declaration_macros/) &&
26853f7bac03SJoe Perches			# for "else if" which can look like "$Ident $Ident"
26863f7bac03SJoe Perches		    !($prevline =~ /^\+\s+$c90_Keywords\b/ ||
26873f7bac03SJoe Perches			# other possible extensions of declaration lines
26883f7bac03SJoe Perches		      $prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ ||
26893f7bac03SJoe Perches			# not starting a section or a macro "\" extended line
26903f7bac03SJoe Perches		      $prevline =~ /(?:\{\s*|\\)$/) &&
26913f7bac03SJoe Perches			# looks like a declaration
26923f7bac03SJoe Perches		    !($sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
26935a4e1fd3SJoe Perches			# function pointer declarations
26945a4e1fd3SJoe Perches		      $sline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
26953f7bac03SJoe Perches			# foo bar; where foo is some local typedef or #define
26963f7bac03SJoe Perches		      $sline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
26973f7bac03SJoe Perches			# known declaration macros
26983f7bac03SJoe Perches		      $sline =~ /^\+\s+$declaration_macros/ ||
26993f7bac03SJoe Perches			# start of struct or union or enum
27003b617e3bSJoe Perches		      $sline =~ /^\+\s+(?:union|struct|enum|typedef)\b/ ||
27013f7bac03SJoe Perches			# start or end of block or continuation of declaration
27023f7bac03SJoe Perches		      $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ ||
27033f7bac03SJoe Perches			# bitfield continuation
27043f7bac03SJoe Perches		      $sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ ||
27053f7bac03SJoe Perches			# other possible extensions of declaration lines
27063f7bac03SJoe Perches		      $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) &&
27073f7bac03SJoe Perches			# indentation of previous and current line are the same
27083f7bac03SJoe Perches		    (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) {
2709d752fcc8SJoe Perches			if (WARN("LINE_SPACING",
2710d752fcc8SJoe Perches				 "Missing a blank line after declarations\n" . $hereprev) &&
2711d752fcc8SJoe Perches			    $fix) {
2712f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, "\+");
2713d752fcc8SJoe Perches			}
27143b617e3bSJoe Perches		}
27153b617e3bSJoe Perches
27165f7ddae6SRaffaele Recalcati# check for spaces at the beginning of a line.
27176b4c5bebSAndy Whitcroft# Exceptions:
27186b4c5bebSAndy Whitcroft#  1) within comments
27196b4c5bebSAndy Whitcroft#  2) indented preprocessor commands
27206b4c5bebSAndy Whitcroft#  3) hanging labels
27213705ce5bSJoe Perches		if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/)  {
27225f7ddae6SRaffaele Recalcati			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
27233705ce5bSJoe Perches			if (WARN("LEADING_SPACE",
27243705ce5bSJoe Perches				 "please, no spaces at the start of a line\n" . $herevet) &&
27253705ce5bSJoe Perches			    $fix) {
2726194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
27273705ce5bSJoe Perches			}
27285f7ddae6SRaffaele Recalcati		}
27295f7ddae6SRaffaele Recalcati
2730b9ea10d6SAndy Whitcroft# check we are in a valid C source file if not then ignore this hunk
2731b9ea10d6SAndy Whitcroft		next if ($realfile !~ /\.(h|c)$/);
2732b9ea10d6SAndy Whitcroft
2733032a4c0fSJoe Perches# check indentation of any line with a bare else
2734840080a0SJoe Perches# (but not if it is a multiple line "if (foo) return bar; else return baz;")
2735032a4c0fSJoe Perches# if the previous line is a break or return and is indented 1 tab more...
2736032a4c0fSJoe Perches		if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {
2737032a4c0fSJoe Perches			my $tabs = length($1) + 1;
2738840080a0SJoe Perches			if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ ||
2739840080a0SJoe Perches			    ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ &&
2740840080a0SJoe Perches			     defined $lines[$linenr] &&
2741840080a0SJoe Perches			     $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) {
2742032a4c0fSJoe Perches				WARN("UNNECESSARY_ELSE",
2743032a4c0fSJoe Perches				     "else is not generally useful after a break or return\n" . $hereprev);
2744032a4c0fSJoe Perches			}
2745032a4c0fSJoe Perches		}
2746032a4c0fSJoe Perches
2747c00df19aSJoe Perches# check indentation of a line with a break;
2748c00df19aSJoe Perches# if the previous line is a goto or return and is indented the same # of tabs
2749c00df19aSJoe Perches		if ($sline =~ /^\+([\t]+)break\s*;\s*$/) {
2750c00df19aSJoe Perches			my $tabs = $1;
2751c00df19aSJoe Perches			if ($prevline =~ /^\+$tabs(?:goto|return)\b/) {
2752c00df19aSJoe Perches				WARN("UNNECESSARY_BREAK",
2753c00df19aSJoe Perches				     "break is not useful after a goto or return\n" . $hereprev);
2754c00df19aSJoe Perches			}
2755c00df19aSJoe Perches		}
2756c00df19aSJoe Perches
27571ba8dfd1SKees Cook# discourage the addition of CONFIG_EXPERIMENTAL in #if(def).
27581ba8dfd1SKees Cook		if ($line =~ /^\+\s*\#\s*if.*\bCONFIG_EXPERIMENTAL\b/) {
27591ba8dfd1SKees Cook			WARN("CONFIG_EXPERIMENTAL",
27601ba8dfd1SKees Cook			     "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
27611ba8dfd1SKees Cook		}
27621ba8dfd1SKees Cook
2763c2fdda0dSAndy Whitcroft# check for RCS/CVS revision markers
2764cf655043SAndy Whitcroft		if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
2765000d1cc1SJoe Perches			WARN("CVS_KEYWORD",
2766000d1cc1SJoe Perches			     "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
2767c2fdda0dSAndy Whitcroft		}
276822f2a2efSAndy Whitcroft
276942e41c54SMike Frysinger# Blackfin: don't use __builtin_bfin_[cs]sync
277042e41c54SMike Frysinger		if ($line =~ /__builtin_bfin_csync/) {
277142e41c54SMike Frysinger			my $herevet = "$here\n" . cat_vet($line) . "\n";
2772000d1cc1SJoe Perches			ERROR("CSYNC",
2773000d1cc1SJoe Perches			      "use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
277442e41c54SMike Frysinger		}
277542e41c54SMike Frysinger		if ($line =~ /__builtin_bfin_ssync/) {
277642e41c54SMike Frysinger			my $herevet = "$here\n" . cat_vet($line) . "\n";
2777000d1cc1SJoe Perches			ERROR("SSYNC",
2778000d1cc1SJoe Perches			      "use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
277942e41c54SMike Frysinger		}
278042e41c54SMike Frysinger
278156e77d70SJoe Perches# check for old HOTPLUG __dev<foo> section markings
278256e77d70SJoe Perches		if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
278356e77d70SJoe Perches			WARN("HOTPLUG_SECTION",
278456e77d70SJoe Perches			     "Using $1 is unnecessary\n" . $herecurr);
278556e77d70SJoe Perches		}
278656e77d70SJoe Perches
27879c0ca6f9SAndy Whitcroft# Check for potential 'bare' types
27882b474a1aSAndy Whitcroft		my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
27892b474a1aSAndy Whitcroft		    $realline_next);
27903e469cdcSAndy Whitcroft#print "LINE<$line>\n";
27913e469cdcSAndy Whitcroft		if ($linenr >= $suppress_statement &&
27921b5539b1SJoe Perches		    $realcnt && $sline =~ /.\s*\S/) {
2793170d3a22SAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
2794f5fe35ddSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0);
2795171ae1a4SAndy Whitcroft			$stat =~ s/\n./\n /g;
2796171ae1a4SAndy Whitcroft			$cond =~ s/\n./\n /g;
2797171ae1a4SAndy Whitcroft
27983e469cdcSAndy Whitcroft#print "linenr<$linenr> <$stat>\n";
27993e469cdcSAndy Whitcroft			# If this statement has no statement boundaries within
28003e469cdcSAndy Whitcroft			# it there is no point in retrying a statement scan
28013e469cdcSAndy Whitcroft			# until we hit end of it.
28023e469cdcSAndy Whitcroft			my $frag = $stat; $frag =~ s/;+\s*$//;
28033e469cdcSAndy Whitcroft			if ($frag !~ /(?:{|;)/) {
28043e469cdcSAndy Whitcroft#print "skip<$line_nr_next>\n";
28053e469cdcSAndy Whitcroft				$suppress_statement = $line_nr_next;
28063e469cdcSAndy Whitcroft			}
2807f74bd194SAndy Whitcroft
28082b474a1aSAndy Whitcroft			# Find the real next line.
28092b474a1aSAndy Whitcroft			$realline_next = $line_nr_next;
28102b474a1aSAndy Whitcroft			if (defined $realline_next &&
28112b474a1aSAndy Whitcroft			    (!defined $lines[$realline_next - 1] ||
28122b474a1aSAndy Whitcroft			     substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
28132b474a1aSAndy Whitcroft				$realline_next++;
28142b474a1aSAndy Whitcroft			}
28152b474a1aSAndy Whitcroft
2816171ae1a4SAndy Whitcroft			my $s = $stat;
2817171ae1a4SAndy Whitcroft			$s =~ s/{.*$//s;
2818cf655043SAndy Whitcroft
2819c2fdda0dSAndy Whitcroft			# Ignore goto labels.
2820171ae1a4SAndy Whitcroft			if ($s =~ /$Ident:\*$/s) {
2821c2fdda0dSAndy Whitcroft
2822c2fdda0dSAndy Whitcroft			# Ignore functions being called
2823171ae1a4SAndy Whitcroft			} elsif ($s =~ /^.\s*$Ident\s*\(/s) {
2824c2fdda0dSAndy Whitcroft
2825463f2864SAndy Whitcroft			} elsif ($s =~ /^.\s*else\b/s) {
2826463f2864SAndy Whitcroft
2827c45dcabdSAndy Whitcroft			# declarations always start with types
2828d2506586SAndy 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) {
2829c45dcabdSAndy Whitcroft				my $type = $1;
2830c45dcabdSAndy Whitcroft				$type =~ s/\s+/ /g;
2831c45dcabdSAndy Whitcroft				possible($type, "A:" . $s);
2832c45dcabdSAndy Whitcroft
28336c72ffaaSAndy Whitcroft			# definitions in global scope can only start with types
2834a6a84062SAndy Whitcroft			} elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
2835c45dcabdSAndy Whitcroft				possible($1, "B:" . $s);
2836c2fdda0dSAndy Whitcroft			}
28378905a67cSAndy Whitcroft
28386c72ffaaSAndy Whitcroft			# any (foo ... *) is a pointer cast, and foo is a type
283965863862SAndy Whitcroft			while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
2840c45dcabdSAndy Whitcroft				possible($1, "C:" . $s);
28419c0ca6f9SAndy Whitcroft			}
28428905a67cSAndy Whitcroft
28438905a67cSAndy Whitcroft			# Check for any sort of function declaration.
28448905a67cSAndy Whitcroft			# int foo(something bar, other baz);
28458905a67cSAndy Whitcroft			# void (*store_gdt)(x86_descr_ptr *);
2846171ae1a4SAndy Whitcroft			if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
28478905a67cSAndy Whitcroft				my ($name_len) = length($1);
28488905a67cSAndy Whitcroft
2849cf655043SAndy Whitcroft				my $ctx = $s;
2850773647a0SAndy Whitcroft				substr($ctx, 0, $name_len + 1, '');
28518905a67cSAndy Whitcroft				$ctx =~ s/\)[^\)]*$//;
2852cf655043SAndy Whitcroft
28538905a67cSAndy Whitcroft				for my $arg (split(/\s*,\s*/, $ctx)) {
2854c45dcabdSAndy Whitcroft					if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
28558905a67cSAndy Whitcroft
2856c45dcabdSAndy Whitcroft						possible($1, "D:" . $s);
28578905a67cSAndy Whitcroft					}
28588905a67cSAndy Whitcroft				}
28598905a67cSAndy Whitcroft			}
28608905a67cSAndy Whitcroft
28619c0ca6f9SAndy Whitcroft		}
28629c0ca6f9SAndy Whitcroft
286300df344fSAndy Whitcroft#
286400df344fSAndy Whitcroft# Checks which may be anchored in the context.
286500df344fSAndy Whitcroft#
286600df344fSAndy Whitcroft
286700df344fSAndy Whitcroft# Check for switch () and associated case and default
286800df344fSAndy Whitcroft# statements should be at the same indent.
286900df344fSAndy Whitcroft		if ($line=~/\bswitch\s*\(.*\)/) {
287000df344fSAndy Whitcroft			my $err = '';
287100df344fSAndy Whitcroft			my $sep = '';
287200df344fSAndy Whitcroft			my @ctx = ctx_block_outer($linenr, $realcnt);
287300df344fSAndy Whitcroft			shift(@ctx);
287400df344fSAndy Whitcroft			for my $ctx (@ctx) {
287500df344fSAndy Whitcroft				my ($clen, $cindent) = line_stats($ctx);
287600df344fSAndy Whitcroft				if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
287700df344fSAndy Whitcroft							$indent != $cindent) {
287800df344fSAndy Whitcroft					$err .= "$sep$ctx\n";
287900df344fSAndy Whitcroft					$sep = '';
288000df344fSAndy Whitcroft				} else {
288100df344fSAndy Whitcroft					$sep = "[...]\n";
288200df344fSAndy Whitcroft				}
288300df344fSAndy Whitcroft			}
288400df344fSAndy Whitcroft			if ($err ne '') {
2885000d1cc1SJoe Perches				ERROR("SWITCH_CASE_INDENT_LEVEL",
2886000d1cc1SJoe Perches				      "switch and case should be at the same indent\n$hereline$err");
2887de7d4f0eSAndy Whitcroft			}
2888de7d4f0eSAndy Whitcroft		}
2889de7d4f0eSAndy Whitcroft
2890de7d4f0eSAndy Whitcroft# if/while/etc brace do not go on next line, unless defining a do while loop,
2891de7d4f0eSAndy Whitcroft# or if that brace on the next line is for something else
28920fe3dc2bSJoe Perches		if ($line =~ /(.*)\b((?:if|while|for|switch|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
2893773647a0SAndy Whitcroft			my $pre_ctx = "$1$2";
2894773647a0SAndy Whitcroft
28959c0ca6f9SAndy Whitcroft			my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
28968eef05ddSJoe Perches
28978eef05ddSJoe Perches			if ($line =~ /^\+\t{6,}/) {
28988eef05ddSJoe Perches				WARN("DEEP_INDENTATION",
28998eef05ddSJoe Perches				     "Too many leading tabs - consider code refactoring\n" . $herecurr);
29008eef05ddSJoe Perches			}
29018eef05ddSJoe Perches
2902de7d4f0eSAndy Whitcroft			my $ctx_cnt = $realcnt - $#ctx - 1;
2903de7d4f0eSAndy Whitcroft			my $ctx = join("\n", @ctx);
2904de7d4f0eSAndy Whitcroft
2905548596d5SAndy Whitcroft			my $ctx_ln = $linenr;
2906548596d5SAndy Whitcroft			my $ctx_skip = $realcnt;
2907de7d4f0eSAndy Whitcroft
2908548596d5SAndy Whitcroft			while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
2909548596d5SAndy Whitcroft					defined $lines[$ctx_ln - 1] &&
2910548596d5SAndy Whitcroft					$lines[$ctx_ln - 1] =~ /^-/)) {
2911548596d5SAndy Whitcroft				##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
2912548596d5SAndy Whitcroft				$ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
2913773647a0SAndy Whitcroft				$ctx_ln++;
2914773647a0SAndy Whitcroft			}
2915548596d5SAndy Whitcroft
291653210168SAndy Whitcroft			#print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
291753210168SAndy Whitcroft			#print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
2918773647a0SAndy Whitcroft
2919773647a0SAndy Whitcroft			if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
2920000d1cc1SJoe Perches				ERROR("OPEN_BRACE",
2921000d1cc1SJoe Perches				      "that open brace { should be on the previous line\n" .
292201464f30SAndy Whitcroft					"$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
292300df344fSAndy Whitcroft			}
2924773647a0SAndy Whitcroft			if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
2925773647a0SAndy Whitcroft			    $ctx =~ /\)\s*\;\s*$/ &&
2926773647a0SAndy Whitcroft			    defined $lines[$ctx_ln - 1])
2927773647a0SAndy Whitcroft			{
29289c0ca6f9SAndy Whitcroft				my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
29299c0ca6f9SAndy Whitcroft				if ($nindent > $indent) {
2930000d1cc1SJoe Perches					WARN("TRAILING_SEMICOLON",
2931000d1cc1SJoe Perches					     "trailing semicolon indicates no statements, indent implies otherwise\n" .
293201464f30SAndy Whitcroft						"$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
29339c0ca6f9SAndy Whitcroft				}
29349c0ca6f9SAndy Whitcroft			}
293500df344fSAndy Whitcroft		}
293600df344fSAndy Whitcroft
29374d001e4dSAndy Whitcroft# Check relative indent for conditionals and blocks.
29380fe3dc2bSJoe Perches		if ($line =~ /\b(?:(?:if|while|for|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
29393e469cdcSAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
29403e469cdcSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0)
29413e469cdcSAndy Whitcroft					if (!defined $stat);
29424d001e4dSAndy Whitcroft			my ($s, $c) = ($stat, $cond);
29434d001e4dSAndy Whitcroft
29444d001e4dSAndy Whitcroft			substr($s, 0, length($c), '');
29454d001e4dSAndy Whitcroft
29464d001e4dSAndy Whitcroft			# Make sure we remove the line prefixes as we have
29474d001e4dSAndy Whitcroft			# none on the first line, and are going to readd them
29484d001e4dSAndy Whitcroft			# where necessary.
29494d001e4dSAndy Whitcroft			$s =~ s/\n./\n/gs;
29504d001e4dSAndy Whitcroft
29514d001e4dSAndy Whitcroft			# Find out how long the conditional actually is.
29526f779c18SAndy Whitcroft			my @newlines = ($c =~ /\n/gs);
29536f779c18SAndy Whitcroft			my $cond_lines = 1 + $#newlines;
29544d001e4dSAndy Whitcroft
29554d001e4dSAndy Whitcroft			# We want to check the first line inside the block
29564d001e4dSAndy Whitcroft			# starting at the end of the conditional, so remove:
29574d001e4dSAndy Whitcroft			#  1) any blank line termination
29584d001e4dSAndy Whitcroft			#  2) any opening brace { on end of the line
29594d001e4dSAndy Whitcroft			#  3) any do (...) {
29604d001e4dSAndy Whitcroft			my $continuation = 0;
29614d001e4dSAndy Whitcroft			my $check = 0;
29624d001e4dSAndy Whitcroft			$s =~ s/^.*\bdo\b//;
29634d001e4dSAndy Whitcroft			$s =~ s/^\s*{//;
29644d001e4dSAndy Whitcroft			if ($s =~ s/^\s*\\//) {
29654d001e4dSAndy Whitcroft				$continuation = 1;
29664d001e4dSAndy Whitcroft			}
29679bd49efeSAndy Whitcroft			if ($s =~ s/^\s*?\n//) {
29684d001e4dSAndy Whitcroft				$check = 1;
29694d001e4dSAndy Whitcroft				$cond_lines++;
29704d001e4dSAndy Whitcroft			}
29714d001e4dSAndy Whitcroft
29724d001e4dSAndy Whitcroft			# Also ignore a loop construct at the end of a
29734d001e4dSAndy Whitcroft			# preprocessor statement.
29744d001e4dSAndy Whitcroft			if (($prevline =~ /^.\s*#\s*define\s/ ||
29754d001e4dSAndy Whitcroft			    $prevline =~ /\\\s*$/) && $continuation == 0) {
29764d001e4dSAndy Whitcroft				$check = 0;
29774d001e4dSAndy Whitcroft			}
29784d001e4dSAndy Whitcroft
29799bd49efeSAndy Whitcroft			my $cond_ptr = -1;
2980740504c6SAndy Whitcroft			$continuation = 0;
29819bd49efeSAndy Whitcroft			while ($cond_ptr != $cond_lines) {
29829bd49efeSAndy Whitcroft				$cond_ptr = $cond_lines;
29834d001e4dSAndy Whitcroft
2984f16fa28fSAndy Whitcroft				# If we see an #else/#elif then the code
2985f16fa28fSAndy Whitcroft				# is not linear.
2986f16fa28fSAndy Whitcroft				if ($s =~ /^\s*\#\s*(?:else|elif)/) {
2987f16fa28fSAndy Whitcroft					$check = 0;
2988f16fa28fSAndy Whitcroft				}
2989f16fa28fSAndy Whitcroft
29909bd49efeSAndy Whitcroft				# Ignore:
29919bd49efeSAndy Whitcroft				#  1) blank lines, they should be at 0,
29929bd49efeSAndy Whitcroft				#  2) preprocessor lines, and
29939bd49efeSAndy Whitcroft				#  3) labels.
2994740504c6SAndy Whitcroft				if ($continuation ||
2995740504c6SAndy Whitcroft				    $s =~ /^\s*?\n/ ||
29969bd49efeSAndy Whitcroft				    $s =~ /^\s*#\s*?/ ||
29979bd49efeSAndy Whitcroft				    $s =~ /^\s*$Ident\s*:/) {
2998740504c6SAndy Whitcroft					$continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
299930dad6ebSAndy Whitcroft					if ($s =~ s/^.*?\n//) {
30009bd49efeSAndy Whitcroft						$cond_lines++;
30019bd49efeSAndy Whitcroft					}
30024d001e4dSAndy Whitcroft				}
300330dad6ebSAndy Whitcroft			}
30044d001e4dSAndy Whitcroft
30054d001e4dSAndy Whitcroft			my (undef, $sindent) = line_stats("+" . $s);
30064d001e4dSAndy Whitcroft			my $stat_real = raw_line($linenr, $cond_lines);
30074d001e4dSAndy Whitcroft
30084d001e4dSAndy Whitcroft			# Check if either of these lines are modified, else
30094d001e4dSAndy Whitcroft			# this is not this patch's fault.
30104d001e4dSAndy Whitcroft			if (!defined($stat_real) ||
30114d001e4dSAndy Whitcroft			    $stat !~ /^\+/ && $stat_real !~ /^\+/) {
30124d001e4dSAndy Whitcroft				$check = 0;
30134d001e4dSAndy Whitcroft			}
30144d001e4dSAndy Whitcroft			if (defined($stat_real) && $cond_lines > 1) {
30154d001e4dSAndy Whitcroft				$stat_real = "[...]\n$stat_real";
30164d001e4dSAndy Whitcroft			}
30174d001e4dSAndy Whitcroft
30189bd49efeSAndy 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";
30194d001e4dSAndy Whitcroft
30204d001e4dSAndy Whitcroft			if ($check && (($sindent % 8) != 0 ||
30214d001e4dSAndy Whitcroft			    ($sindent <= $indent && $s ne ''))) {
3022000d1cc1SJoe Perches				WARN("SUSPECT_CODE_INDENT",
3023000d1cc1SJoe Perches				     "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
30244d001e4dSAndy Whitcroft			}
30254d001e4dSAndy Whitcroft		}
30264d001e4dSAndy Whitcroft
30276c72ffaaSAndy Whitcroft		# Track the 'values' across context and added lines.
30286c72ffaaSAndy Whitcroft		my $opline = $line; $opline =~ s/^./ /;
30291f65f947SAndy Whitcroft		my ($curr_values, $curr_vars) =
30301f65f947SAndy Whitcroft				annotate_values($opline . "\n", $prev_values);
30316c72ffaaSAndy Whitcroft		$curr_values = $prev_values . $curr_values;
3032c2fdda0dSAndy Whitcroft		if ($dbg_values) {
3033c2fdda0dSAndy Whitcroft			my $outline = $opline; $outline =~ s/\t/ /g;
3034cf655043SAndy Whitcroft			print "$linenr > .$outline\n";
3035cf655043SAndy Whitcroft			print "$linenr > $curr_values\n";
30361f65f947SAndy Whitcroft			print "$linenr >  $curr_vars\n";
3037c2fdda0dSAndy Whitcroft		}
30386c72ffaaSAndy Whitcroft		$prev_values = substr($curr_values, -1);
30396c72ffaaSAndy Whitcroft
304000df344fSAndy Whitcroft#ignore lines not being added
30413705ce5bSJoe Perches		next if ($line =~ /^[^\+]/);
304200df344fSAndy Whitcroft
3043653d4876SAndy Whitcroft# TEST: allow direct testing of the type matcher.
30447429c690SAndy Whitcroft		if ($dbg_type) {
30457429c690SAndy Whitcroft			if ($line =~ /^.\s*$Declare\s*$/) {
3046000d1cc1SJoe Perches				ERROR("TEST_TYPE",
3047000d1cc1SJoe Perches				      "TEST: is type\n" . $herecurr);
30487429c690SAndy Whitcroft			} elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
3049000d1cc1SJoe Perches				ERROR("TEST_NOT_TYPE",
3050000d1cc1SJoe Perches				      "TEST: is not type ($1 is)\n". $herecurr);
30517429c690SAndy Whitcroft			}
3052653d4876SAndy Whitcroft			next;
3053653d4876SAndy Whitcroft		}
3054a1ef277eSAndy Whitcroft# TEST: allow direct testing of the attribute matcher.
3055a1ef277eSAndy Whitcroft		if ($dbg_attr) {
30569360b0e5SAndy Whitcroft			if ($line =~ /^.\s*$Modifier\s*$/) {
3057000d1cc1SJoe Perches				ERROR("TEST_ATTR",
3058000d1cc1SJoe Perches				      "TEST: is attr\n" . $herecurr);
30599360b0e5SAndy Whitcroft			} elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
3060000d1cc1SJoe Perches				ERROR("TEST_NOT_ATTR",
3061000d1cc1SJoe Perches				      "TEST: is not attr ($1 is)\n". $herecurr);
3062a1ef277eSAndy Whitcroft			}
3063a1ef277eSAndy Whitcroft			next;
3064a1ef277eSAndy Whitcroft		}
3065653d4876SAndy Whitcroft
3066f0a594c1SAndy Whitcroft# check for initialisation to aggregates open brace on the next line
306799423c20SAndy Whitcroft		if ($line =~ /^.\s*{/ &&
306899423c20SAndy Whitcroft		    $prevline =~ /(?:^|[^=])=\s*$/) {
3069d752fcc8SJoe Perches			if (ERROR("OPEN_BRACE",
3070d752fcc8SJoe Perches				  "that open brace { should be on the previous line\n" . $hereprev) &&
3071f2d7e4d4SJoe Perches			    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
3072f2d7e4d4SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
3073f2d7e4d4SJoe Perches				fix_delete_line($fixlinenr, $rawline);
3074d752fcc8SJoe Perches				my $fixedline = $prevrawline;
3075d752fcc8SJoe Perches				$fixedline =~ s/\s*=\s*$/ = {/;
3076f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
3077d752fcc8SJoe Perches				$fixedline = $line;
3078d752fcc8SJoe Perches				$fixedline =~ s/^(.\s*){\s*/$1/;
3079f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
3080d752fcc8SJoe Perches			}
3081f0a594c1SAndy Whitcroft		}
3082f0a594c1SAndy Whitcroft
308300df344fSAndy Whitcroft#
308400df344fSAndy Whitcroft# Checks which are anchored on the added line.
308500df344fSAndy Whitcroft#
308600df344fSAndy Whitcroft
3087653d4876SAndy Whitcroft# check for malformed paths in #include statements (uses RAW line)
3088c45dcabdSAndy Whitcroft		if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
3089653d4876SAndy Whitcroft			my $path = $1;
3090653d4876SAndy Whitcroft			if ($path =~ m{//}) {
3091000d1cc1SJoe Perches				ERROR("MALFORMED_INCLUDE",
3092495e9d84SJoe Perches				      "malformed #include filename\n" . $herecurr);
3093495e9d84SJoe Perches			}
3094495e9d84SJoe Perches			if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
3095495e9d84SJoe Perches				ERROR("UAPI_INCLUDE",
3096495e9d84SJoe Perches				      "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
3097653d4876SAndy Whitcroft			}
3098653d4876SAndy Whitcroft		}
3099653d4876SAndy Whitcroft
310000df344fSAndy Whitcroft# no C99 // comments
310100df344fSAndy Whitcroft		if ($line =~ m{//}) {
31023705ce5bSJoe Perches			if (ERROR("C99_COMMENTS",
31033705ce5bSJoe Perches				  "do not use C99 // comments\n" . $herecurr) &&
31043705ce5bSJoe Perches			    $fix) {
3105194f66fcSJoe Perches				my $line = $fixed[$fixlinenr];
31063705ce5bSJoe Perches				if ($line =~ /\/\/(.*)$/) {
31073705ce5bSJoe Perches					my $comment = trim($1);
3108194f66fcSJoe Perches					$fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@;
31093705ce5bSJoe Perches				}
31103705ce5bSJoe Perches			}
311100df344fSAndy Whitcroft		}
311200df344fSAndy Whitcroft		# Remove C99 comments.
31130a920b5bSAndy Whitcroft		$line =~ s@//.*@@;
31146c72ffaaSAndy Whitcroft		$opline =~ s@//.*@@;
31150a920b5bSAndy Whitcroft
31162b474a1aSAndy Whitcroft# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
31172b474a1aSAndy Whitcroft# the whole statement.
31182b474a1aSAndy Whitcroft#print "APW <$lines[$realline_next - 1]>\n";
31192b474a1aSAndy Whitcroft		if (defined $realline_next &&
31202b474a1aSAndy Whitcroft		    exists $lines[$realline_next - 1] &&
31212b474a1aSAndy Whitcroft		    !defined $suppress_export{$realline_next} &&
31222b474a1aSAndy Whitcroft		    ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
31232b474a1aSAndy Whitcroft		     $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
31243cbf62dfSAndy Whitcroft			# Handle definitions which produce identifiers with
31253cbf62dfSAndy Whitcroft			# a prefix:
31263cbf62dfSAndy Whitcroft			#   XXX(foo);
31273cbf62dfSAndy Whitcroft			#   EXPORT_SYMBOL(something_foo);
3128653d4876SAndy Whitcroft			my $name = $1;
312987a53877SAndy Whitcroft			if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
31303cbf62dfSAndy Whitcroft			    $name =~ /^${Ident}_$2/) {
31313cbf62dfSAndy Whitcroft#print "FOO C name<$name>\n";
31323cbf62dfSAndy Whitcroft				$suppress_export{$realline_next} = 1;
31333cbf62dfSAndy Whitcroft
31343cbf62dfSAndy Whitcroft			} elsif ($stat !~ /(?:
31352b474a1aSAndy Whitcroft				\n.}\s*$|
313648012058SAndy Whitcroft				^.DEFINE_$Ident\(\Q$name\E\)|
313748012058SAndy Whitcroft				^.DECLARE_$Ident\(\Q$name\E\)|
313848012058SAndy Whitcroft				^.LIST_HEAD\(\Q$name\E\)|
31392b474a1aSAndy Whitcroft				^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
31402b474a1aSAndy Whitcroft				\b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
314148012058SAndy Whitcroft			    )/x) {
31422b474a1aSAndy Whitcroft#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
31432b474a1aSAndy Whitcroft				$suppress_export{$realline_next} = 2;
31442b474a1aSAndy Whitcroft			} else {
31452b474a1aSAndy Whitcroft				$suppress_export{$realline_next} = 1;
31460a920b5bSAndy Whitcroft			}
31470a920b5bSAndy Whitcroft		}
31482b474a1aSAndy Whitcroft		if (!defined $suppress_export{$linenr} &&
31492b474a1aSAndy Whitcroft		    $prevline =~ /^.\s*$/ &&
31502b474a1aSAndy Whitcroft		    ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
31512b474a1aSAndy Whitcroft		     $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
31522b474a1aSAndy Whitcroft#print "FOO B <$lines[$linenr - 1]>\n";
31532b474a1aSAndy Whitcroft			$suppress_export{$linenr} = 2;
31542b474a1aSAndy Whitcroft		}
31552b474a1aSAndy Whitcroft		if (defined $suppress_export{$linenr} &&
31562b474a1aSAndy Whitcroft		    $suppress_export{$linenr} == 2) {
3157000d1cc1SJoe Perches			WARN("EXPORT_SYMBOL",
3158000d1cc1SJoe Perches			     "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
31592b474a1aSAndy Whitcroft		}
31600a920b5bSAndy Whitcroft
31615150bda4SJoe Eloff# check for global initialisers.
3162d5e616fcSJoe Perches		if ($line =~ /^\+(\s*$Type\s*$Ident\s*(?:\s+$Modifier))*\s*=\s*(0|NULL|false)\s*;/) {
3163d5e616fcSJoe Perches			if (ERROR("GLOBAL_INITIALISERS",
3164000d1cc1SJoe Perches				  "do not initialise globals to 0 or NULL\n" .
3165d5e616fcSJoe Perches				      $herecurr) &&
3166d5e616fcSJoe Perches			    $fix) {
3167194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/($Type\s*$Ident\s*(?:\s+$Modifier))*\s*=\s*(0|NULL|false)\s*;/$1;/;
3168d5e616fcSJoe Perches			}
3169f0a594c1SAndy Whitcroft		}
31700a920b5bSAndy Whitcroft# check for static initialisers.
3171d5e616fcSJoe Perches		if ($line =~ /^\+.*\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
3172d5e616fcSJoe Perches			if (ERROR("INITIALISED_STATIC",
3173000d1cc1SJoe Perches				  "do not initialise statics to 0 or NULL\n" .
3174d5e616fcSJoe Perches				      $herecurr) &&
3175d5e616fcSJoe Perches			    $fix) {
3176194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*(0|NULL|false)\s*;/$1;/;
3177d5e616fcSJoe Perches			}
31780a920b5bSAndy Whitcroft		}
31790a920b5bSAndy Whitcroft
31801813087dSJoe Perches# check for misordered declarations of char/short/int/long with signed/unsigned
31811813087dSJoe Perches		while ($sline =~ m{(\b$TypeMisordered\b)}g) {
31821813087dSJoe Perches			my $tmp = trim($1);
31831813087dSJoe Perches			WARN("MISORDERED_TYPE",
31841813087dSJoe Perches			     "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr);
31851813087dSJoe Perches		}
31861813087dSJoe Perches
3187cb710ecaSJoe Perches# check for static const char * arrays.
3188cb710ecaSJoe Perches		if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
3189000d1cc1SJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
3190000d1cc1SJoe Perches			     "static const char * array should probably be static const char * const\n" .
3191cb710ecaSJoe Perches				$herecurr);
3192cb710ecaSJoe Perches               }
3193cb710ecaSJoe Perches
3194cb710ecaSJoe Perches# check for static char foo[] = "bar" declarations.
3195cb710ecaSJoe Perches		if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
3196000d1cc1SJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
3197000d1cc1SJoe Perches			     "static char array declaration should probably be static const char\n" .
3198cb710ecaSJoe Perches				$herecurr);
3199cb710ecaSJoe Perches               }
3200cb710ecaSJoe Perches
3201*ab7e23f3SJoe Perches# check for const <foo> const where <foo> is not a pointer or array type
3202*ab7e23f3SJoe Perches		if ($sline =~ /\bconst\s+($BasicType)\s+const\b/) {
3203*ab7e23f3SJoe Perches			my $found = $1;
3204*ab7e23f3SJoe Perches			if ($sline =~ /\bconst\s+\Q$found\E\s+const\b\s*\*/) {
3205*ab7e23f3SJoe Perches				WARN("CONST_CONST",
3206*ab7e23f3SJoe Perches				     "'const $found const *' should probably be 'const $found * const'\n" . $herecurr);
3207*ab7e23f3SJoe Perches			} elsif ($sline !~ /\bconst\s+\Q$found\E\s+const\s+\w+\s*\[/) {
3208*ab7e23f3SJoe Perches				WARN("CONST_CONST",
3209*ab7e23f3SJoe Perches				     "'const $found const' should probably be 'const $found'\n" . $herecurr);
3210*ab7e23f3SJoe Perches			}
3211*ab7e23f3SJoe Perches		}
3212*ab7e23f3SJoe Perches
32139b0fa60dSJoe Perches# check for non-global char *foo[] = {"bar", ...} declarations.
32149b0fa60dSJoe Perches		if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) {
32159b0fa60dSJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
32169b0fa60dSJoe Perches			     "char * array declaration might be better as static const\n" .
32179b0fa60dSJoe Perches				$herecurr);
32189b0fa60dSJoe Perches               }
32199b0fa60dSJoe Perches
3220b36190c5SJoe Perches# check for function declarations without arguments like "int foo()"
3221b36190c5SJoe Perches		if ($line =~ /(\b$Type\s+$Ident)\s*\(\s*\)/) {
3222b36190c5SJoe Perches			if (ERROR("FUNCTION_WITHOUT_ARGS",
3223b36190c5SJoe Perches				  "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) &&
3224b36190c5SJoe Perches			    $fix) {
3225194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/;
3226b36190c5SJoe Perches			}
3227b36190c5SJoe Perches		}
3228b36190c5SJoe Perches
322992e112fdSJoe Perches# check for uses of DEFINE_PCI_DEVICE_TABLE
323092e112fdSJoe Perches		if ($line =~ /\bDEFINE_PCI_DEVICE_TABLE\s*\(\s*(\w+)\s*\)\s*=/) {
323192e112fdSJoe Perches			if (WARN("DEFINE_PCI_DEVICE_TABLE",
323292e112fdSJoe Perches				 "Prefer struct pci_device_id over deprecated DEFINE_PCI_DEVICE_TABLE\n" . $herecurr) &&
323392e112fdSJoe Perches			    $fix) {
3234194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b(?:static\s+|)DEFINE_PCI_DEVICE_TABLE\s*\(\s*(\w+)\s*\)\s*=\s*/static const struct pci_device_id $1\[\] = /;
323592e112fdSJoe Perches			}
323693ed0e2dSJoe Perches		}
323793ed0e2dSJoe Perches
3238653d4876SAndy Whitcroft# check for new typedefs, only function parameters and sparse annotations
3239653d4876SAndy Whitcroft# make sense.
3240653d4876SAndy Whitcroft		if ($line =~ /\btypedef\s/ &&
32418054576dSAndy Whitcroft		    $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
3242c45dcabdSAndy Whitcroft		    $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
32438ed22cadSAndy Whitcroft		    $line !~ /\b$typeTypedefs\b/ &&
3244021158b4SJoe Perches		    $line !~ /\b$typeOtherOSTypedefs\b/ &&
3245653d4876SAndy Whitcroft		    $line !~ /\b__bitwise(?:__|)\b/) {
3246000d1cc1SJoe Perches			WARN("NEW_TYPEDEFS",
3247000d1cc1SJoe Perches			     "do not add new typedefs\n" . $herecurr);
32480a920b5bSAndy Whitcroft		}
32490a920b5bSAndy Whitcroft
32500a920b5bSAndy Whitcroft# * goes on variable not on type
325165863862SAndy Whitcroft		# (char*[ const])
3252bfcb2cc7SAndy Whitcroft		while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
3253bfcb2cc7SAndy Whitcroft			#print "AA<$1>\n";
32543705ce5bSJoe Perches			my ($ident, $from, $to) = ($1, $2, $2);
3255d8aaf121SAndy Whitcroft
325665863862SAndy Whitcroft			# Should start with a space.
325765863862SAndy Whitcroft			$to =~ s/^(\S)/ $1/;
325865863862SAndy Whitcroft			# Should not end with a space.
325965863862SAndy Whitcroft			$to =~ s/\s+$//;
326065863862SAndy Whitcroft			# '*'s should not have spaces between.
3261f9a0b3d1SAndy Whitcroft			while ($to =~ s/\*\s+\*/\*\*/) {
326265863862SAndy Whitcroft			}
3263d8aaf121SAndy Whitcroft
32643705ce5bSJoe Perches##			print "1: from<$from> to<$to> ident<$ident>\n";
326565863862SAndy Whitcroft			if ($from ne $to) {
32663705ce5bSJoe Perches				if (ERROR("POINTER_LOCATION",
32673705ce5bSJoe Perches					  "\"(foo$from)\" should be \"(foo$to)\"\n" .  $herecurr) &&
32683705ce5bSJoe Perches				    $fix) {
32693705ce5bSJoe Perches					my $sub_from = $ident;
32703705ce5bSJoe Perches					my $sub_to = $ident;
32713705ce5bSJoe Perches					$sub_to =~ s/\Q$from\E/$to/;
3272194f66fcSJoe Perches					$fixed[$fixlinenr] =~
32733705ce5bSJoe Perches					    s@\Q$sub_from\E@$sub_to@;
32743705ce5bSJoe Perches				}
327565863862SAndy Whitcroft			}
3276bfcb2cc7SAndy Whitcroft		}
3277bfcb2cc7SAndy Whitcroft		while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
3278bfcb2cc7SAndy Whitcroft			#print "BB<$1>\n";
32793705ce5bSJoe Perches			my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
3280d8aaf121SAndy Whitcroft
328165863862SAndy Whitcroft			# Should start with a space.
328265863862SAndy Whitcroft			$to =~ s/^(\S)/ $1/;
328365863862SAndy Whitcroft			# Should not end with a space.
328465863862SAndy Whitcroft			$to =~ s/\s+$//;
328565863862SAndy Whitcroft			# '*'s should not have spaces between.
3286f9a0b3d1SAndy Whitcroft			while ($to =~ s/\*\s+\*/\*\*/) {
328765863862SAndy Whitcroft			}
328865863862SAndy Whitcroft			# Modifiers should have spaces.
328965863862SAndy Whitcroft			$to =~ s/(\b$Modifier$)/$1 /;
329065863862SAndy Whitcroft
32913705ce5bSJoe Perches##			print "2: from<$from> to<$to> ident<$ident>\n";
3292667026e7SAndy Whitcroft			if ($from ne $to && $ident !~ /^$Modifier$/) {
32933705ce5bSJoe Perches				if (ERROR("POINTER_LOCATION",
32943705ce5bSJoe Perches					  "\"foo${from}bar\" should be \"foo${to}bar\"\n" .  $herecurr) &&
32953705ce5bSJoe Perches				    $fix) {
32963705ce5bSJoe Perches
32973705ce5bSJoe Perches					my $sub_from = $match;
32983705ce5bSJoe Perches					my $sub_to = $match;
32993705ce5bSJoe Perches					$sub_to =~ s/\Q$from\E/$to/;
3300194f66fcSJoe Perches					$fixed[$fixlinenr] =~
33013705ce5bSJoe Perches					    s@\Q$sub_from\E@$sub_to@;
33023705ce5bSJoe Perches				}
330365863862SAndy Whitcroft			}
33040a920b5bSAndy Whitcroft		}
33050a920b5bSAndy Whitcroft
33060a920b5bSAndy Whitcroft# # no BUG() or BUG_ON()
33070a920b5bSAndy Whitcroft# 		if ($line =~ /\b(BUG|BUG_ON)\b/) {
33080a920b5bSAndy Whitcroft# 			print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
33090a920b5bSAndy Whitcroft# 			print "$herecurr";
33100a920b5bSAndy Whitcroft# 			$clean = 0;
33110a920b5bSAndy Whitcroft# 		}
33120a920b5bSAndy Whitcroft
33138905a67cSAndy Whitcroft		if ($line =~ /\bLINUX_VERSION_CODE\b/) {
3314000d1cc1SJoe Perches			WARN("LINUX_VERSION_CODE",
3315000d1cc1SJoe Perches			     "LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
33168905a67cSAndy Whitcroft		}
33178905a67cSAndy Whitcroft
331817441227SJoe Perches# check for uses of printk_ratelimit
331917441227SJoe Perches		if ($line =~ /\bprintk_ratelimit\s*\(/) {
3320000d1cc1SJoe Perches			WARN("PRINTK_RATELIMITED",
3321000d1cc1SJoe Perches			     "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
332217441227SJoe Perches		}
332317441227SJoe Perches
332400df344fSAndy Whitcroft# printk should use KERN_* levels.  Note that follow on printk's on the
332500df344fSAndy Whitcroft# same line do not need a level, so we use the current block context
332600df344fSAndy Whitcroft# to try and find and validate the current printk.  In summary the current
332725985edcSLucas De Marchi# printk includes all preceding printk's which have no newline on the end.
332800df344fSAndy Whitcroft# we assume the first bad printk is the one to report.
3329f0a594c1SAndy Whitcroft		if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
333000df344fSAndy Whitcroft			my $ok = 0;
333100df344fSAndy Whitcroft			for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
333200df344fSAndy Whitcroft				#print "CHECK<$lines[$ln - 1]\n";
333325985edcSLucas De Marchi				# we have a preceding printk if it ends
333400df344fSAndy Whitcroft				# with "\n" ignore it, else it is to blame
333500df344fSAndy Whitcroft				if ($lines[$ln - 1] =~ m{\bprintk\(}) {
333600df344fSAndy Whitcroft					if ($rawlines[$ln - 1] !~ m{\\n"}) {
333700df344fSAndy Whitcroft						$ok = 1;
333800df344fSAndy Whitcroft					}
333900df344fSAndy Whitcroft					last;
334000df344fSAndy Whitcroft				}
334100df344fSAndy Whitcroft			}
334200df344fSAndy Whitcroft			if ($ok == 0) {
3343000d1cc1SJoe Perches				WARN("PRINTK_WITHOUT_KERN_LEVEL",
3344000d1cc1SJoe Perches				     "printk() should include KERN_ facility level\n" . $herecurr);
33450a920b5bSAndy Whitcroft			}
334600df344fSAndy Whitcroft		}
33470a920b5bSAndy Whitcroft
3348243f3803SJoe Perches		if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
3349243f3803SJoe Perches			my $orig = $1;
3350243f3803SJoe Perches			my $level = lc($orig);
3351243f3803SJoe Perches			$level = "warn" if ($level eq "warning");
33528f26b837SJoe Perches			my $level2 = $level;
33538f26b837SJoe Perches			$level2 = "dbg" if ($level eq "debug");
3354243f3803SJoe Perches			WARN("PREFER_PR_LEVEL",
3355daa8b059SYogesh Chaudhari			     "Prefer [subsystem eg: netdev]_$level2([subsystem]dev, ... then dev_$level2(dev, ... then pr_$level(...  to printk(KERN_$orig ...\n" . $herecurr);
3356243f3803SJoe Perches		}
3357243f3803SJoe Perches
3358243f3803SJoe Perches		if ($line =~ /\bpr_warning\s*\(/) {
3359d5e616fcSJoe Perches			if (WARN("PREFER_PR_LEVEL",
3360d5e616fcSJoe Perches				 "Prefer pr_warn(... to pr_warning(...\n" . $herecurr) &&
3361d5e616fcSJoe Perches			    $fix) {
3362194f66fcSJoe Perches				$fixed[$fixlinenr] =~
3363d5e616fcSJoe Perches				    s/\bpr_warning\b/pr_warn/;
3364d5e616fcSJoe Perches			}
3365243f3803SJoe Perches		}
3366243f3803SJoe Perches
3367dc139313SJoe Perches		if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
3368dc139313SJoe Perches			my $orig = $1;
3369dc139313SJoe Perches			my $level = lc($orig);
3370dc139313SJoe Perches			$level = "warn" if ($level eq "warning");
3371dc139313SJoe Perches			$level = "dbg" if ($level eq "debug");
3372dc139313SJoe Perches			WARN("PREFER_DEV_LEVEL",
3373dc139313SJoe Perches			     "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
3374dc139313SJoe Perches		}
3375dc139313SJoe Perches
3376653d4876SAndy Whitcroft# function brace can't be on same line, except for #defines of do while,
3377653d4876SAndy Whitcroft# or if closed on same line
33788d182478SJoe Perches		if (($line=~/$Type\s*$Ident\(.*\).*\s*{/) and
3379c45dcabdSAndy Whitcroft		    !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) {
33808d182478SJoe Perches			if (ERROR("OPEN_BRACE",
33818d182478SJoe Perches				  "open brace '{' following function declarations go on the next line\n" . $herecurr) &&
33828d182478SJoe Perches			    $fix) {
33838d182478SJoe Perches				fix_delete_line($fixlinenr, $rawline);
33848d182478SJoe Perches				my $fixed_line = $rawline;
33858d182478SJoe Perches				$fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*){(.*)$/;
33868d182478SJoe Perches				my $line1 = $1;
33878d182478SJoe Perches				my $line2 = $2;
33888d182478SJoe Perches				fix_insert_line($fixlinenr, ltrim($line1));
33898d182478SJoe Perches				fix_insert_line($fixlinenr, "\+{");
33908d182478SJoe Perches				if ($line2 !~ /^\s*$/) {
33918d182478SJoe Perches					fix_insert_line($fixlinenr, "\+\t" . trim($line2));
33928d182478SJoe Perches				}
33938d182478SJoe Perches			}
33940a920b5bSAndy Whitcroft		}
3395653d4876SAndy Whitcroft
33968905a67cSAndy Whitcroft# open braces for enum, union and struct go on the same line.
33978905a67cSAndy Whitcroft		if ($line =~ /^.\s*{/ &&
33988905a67cSAndy Whitcroft		    $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
33998d182478SJoe Perches			if (ERROR("OPEN_BRACE",
34008d182478SJoe Perches				  "open brace '{' following $1 go on the same line\n" . $hereprev) &&
34018d182478SJoe Perches			    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
34028d182478SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
34038d182478SJoe Perches				fix_delete_line($fixlinenr, $rawline);
34048d182478SJoe Perches				my $fixedline = rtrim($prevrawline) . " {";
34058d182478SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
34068d182478SJoe Perches				$fixedline = $rawline;
34078d182478SJoe Perches				$fixedline =~ s/^(.\s*){\s*/$1\t/;
34088d182478SJoe Perches				if ($fixedline !~ /^\+\s*$/) {
34098d182478SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
34108d182478SJoe Perches				}
34118d182478SJoe Perches			}
34128905a67cSAndy Whitcroft		}
34138905a67cSAndy Whitcroft
34140c73b4ebSAndy Whitcroft# missing space after union, struct or enum definition
34153705ce5bSJoe Perches		if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
34163705ce5bSJoe Perches			if (WARN("SPACING",
34173705ce5bSJoe Perches				 "missing space after $1 definition\n" . $herecurr) &&
34183705ce5bSJoe Perches			    $fix) {
3419194f66fcSJoe Perches				$fixed[$fixlinenr] =~
34203705ce5bSJoe Perches				    s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
34213705ce5bSJoe Perches			}
34220c73b4ebSAndy Whitcroft		}
34230c73b4ebSAndy Whitcroft
342431070b5dSJoe Perches# Function pointer declarations
342531070b5dSJoe Perches# check spacing between type, funcptr, and args
342631070b5dSJoe Perches# canonical declaration is "type (*funcptr)(args...)"
342791f72e9cSJoe Perches		if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) {
342831070b5dSJoe Perches			my $declare = $1;
342931070b5dSJoe Perches			my $pre_pointer_space = $2;
343031070b5dSJoe Perches			my $post_pointer_space = $3;
343131070b5dSJoe Perches			my $funcname = $4;
343231070b5dSJoe Perches			my $post_funcname_space = $5;
343331070b5dSJoe Perches			my $pre_args_space = $6;
343431070b5dSJoe Perches
343591f72e9cSJoe Perches# the $Declare variable will capture all spaces after the type
343691f72e9cSJoe Perches# so check it for a missing trailing missing space but pointer return types
343791f72e9cSJoe Perches# don't need a space so don't warn for those.
343891f72e9cSJoe Perches			my $post_declare_space = "";
343991f72e9cSJoe Perches			if ($declare =~ /(\s+)$/) {
344091f72e9cSJoe Perches				$post_declare_space = $1;
344191f72e9cSJoe Perches				$declare = rtrim($declare);
344291f72e9cSJoe Perches			}
344391f72e9cSJoe Perches			if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) {
344431070b5dSJoe Perches				WARN("SPACING",
344531070b5dSJoe Perches				     "missing space after return type\n" . $herecurr);
344691f72e9cSJoe Perches				$post_declare_space = " ";
344731070b5dSJoe Perches			}
344831070b5dSJoe Perches
344931070b5dSJoe Perches# unnecessary space "type  (*funcptr)(args...)"
345091f72e9cSJoe Perches# This test is not currently implemented because these declarations are
345191f72e9cSJoe Perches# equivalent to
345291f72e9cSJoe Perches#	int  foo(int bar, ...)
345391f72e9cSJoe Perches# and this is form shouldn't/doesn't generate a checkpatch warning.
345491f72e9cSJoe Perches#
345591f72e9cSJoe Perches#			elsif ($declare =~ /\s{2,}$/) {
345691f72e9cSJoe Perches#				WARN("SPACING",
345791f72e9cSJoe Perches#				     "Multiple spaces after return type\n" . $herecurr);
345891f72e9cSJoe Perches#			}
345931070b5dSJoe Perches
346031070b5dSJoe Perches# unnecessary space "type ( *funcptr)(args...)"
346131070b5dSJoe Perches			if (defined $pre_pointer_space &&
346231070b5dSJoe Perches			    $pre_pointer_space =~ /^\s/) {
346331070b5dSJoe Perches				WARN("SPACING",
346431070b5dSJoe Perches				     "Unnecessary space after function pointer open parenthesis\n" . $herecurr);
346531070b5dSJoe Perches			}
346631070b5dSJoe Perches
346731070b5dSJoe Perches# unnecessary space "type (* funcptr)(args...)"
346831070b5dSJoe Perches			if (defined $post_pointer_space &&
346931070b5dSJoe Perches			    $post_pointer_space =~ /^\s/) {
347031070b5dSJoe Perches				WARN("SPACING",
347131070b5dSJoe Perches				     "Unnecessary space before function pointer name\n" . $herecurr);
347231070b5dSJoe Perches			}
347331070b5dSJoe Perches
347431070b5dSJoe Perches# unnecessary space "type (*funcptr )(args...)"
347531070b5dSJoe Perches			if (defined $post_funcname_space &&
347631070b5dSJoe Perches			    $post_funcname_space =~ /^\s/) {
347731070b5dSJoe Perches				WARN("SPACING",
347831070b5dSJoe Perches				     "Unnecessary space after function pointer name\n" . $herecurr);
347931070b5dSJoe Perches			}
348031070b5dSJoe Perches
348131070b5dSJoe Perches# unnecessary space "type (*funcptr) (args...)"
348231070b5dSJoe Perches			if (defined $pre_args_space &&
348331070b5dSJoe Perches			    $pre_args_space =~ /^\s/) {
348431070b5dSJoe Perches				WARN("SPACING",
348531070b5dSJoe Perches				     "Unnecessary space before function pointer arguments\n" . $herecurr);
348631070b5dSJoe Perches			}
348731070b5dSJoe Perches
348831070b5dSJoe Perches			if (show_type("SPACING") && $fix) {
3489194f66fcSJoe Perches				$fixed[$fixlinenr] =~
349091f72e9cSJoe Perches				    s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex;
349131070b5dSJoe Perches			}
349231070b5dSJoe Perches		}
349331070b5dSJoe Perches
34948d31cfceSAndy Whitcroft# check for spacing round square brackets; allowed:
34958d31cfceSAndy Whitcroft#  1. with a type on the left -- int [] a;
3496fe2a7dbcSAndy Whitcroft#  2. at the beginning of a line for slice initialisers -- [0...10] = 5,
3497fe2a7dbcSAndy Whitcroft#  3. inside a curly brace -- = { [0...10] = 5 }
34988d31cfceSAndy Whitcroft		while ($line =~ /(.*?\s)\[/g) {
34998d31cfceSAndy Whitcroft			my ($where, $prefix) = ($-[1], $1);
35008d31cfceSAndy Whitcroft			if ($prefix !~ /$Type\s+$/ &&
3501fe2a7dbcSAndy Whitcroft			    ($where != 0 || $prefix !~ /^.\s+$/) &&
3502daebc534SAndy Whitcroft			    $prefix !~ /[{,]\s+$/) {
35033705ce5bSJoe Perches				if (ERROR("BRACKET_SPACE",
35043705ce5bSJoe Perches					  "space prohibited before open square bracket '['\n" . $herecurr) &&
35053705ce5bSJoe Perches				    $fix) {
3506194f66fcSJoe Perches				    $fixed[$fixlinenr] =~
35073705ce5bSJoe Perches					s/^(\+.*?)\s+\[/$1\[/;
35083705ce5bSJoe Perches				}
35098d31cfceSAndy Whitcroft			}
35108d31cfceSAndy Whitcroft		}
35118d31cfceSAndy Whitcroft
3512f0a594c1SAndy Whitcroft# check for spaces between functions and their parentheses.
35136c72ffaaSAndy Whitcroft		while ($line =~ /($Ident)\s+\(/g) {
3514c2fdda0dSAndy Whitcroft			my $name = $1;
3515773647a0SAndy Whitcroft			my $ctx_before = substr($line, 0, $-[1]);
3516773647a0SAndy Whitcroft			my $ctx = "$ctx_before$name";
3517c2fdda0dSAndy Whitcroft
3518c2fdda0dSAndy Whitcroft			# Ignore those directives where spaces _are_ permitted.
3519773647a0SAndy Whitcroft			if ($name =~ /^(?:
3520773647a0SAndy Whitcroft				if|for|while|switch|return|case|
3521773647a0SAndy Whitcroft				volatile|__volatile__|
3522773647a0SAndy Whitcroft				__attribute__|format|__extension__|
3523773647a0SAndy Whitcroft				asm|__asm__)$/x)
3524773647a0SAndy Whitcroft			{
3525c2fdda0dSAndy Whitcroft			# cpp #define statements have non-optional spaces, ie
3526c2fdda0dSAndy Whitcroft			# if there is a space between the name and the open
3527c2fdda0dSAndy Whitcroft			# parenthesis it is simply not a parameter group.
3528c45dcabdSAndy Whitcroft			} elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
3529773647a0SAndy Whitcroft
3530773647a0SAndy Whitcroft			# cpp #elif statement condition may start with a (
3531c45dcabdSAndy Whitcroft			} elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
3532c2fdda0dSAndy Whitcroft
3533c2fdda0dSAndy Whitcroft			# If this whole things ends with a type its most
3534c2fdda0dSAndy Whitcroft			# likely a typedef for a function.
3535773647a0SAndy Whitcroft			} elsif ($ctx =~ /$Type$/) {
3536c2fdda0dSAndy Whitcroft
3537c2fdda0dSAndy Whitcroft			} else {
35383705ce5bSJoe Perches				if (WARN("SPACING",
35393705ce5bSJoe Perches					 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
35403705ce5bSJoe Perches					     $fix) {
3541194f66fcSJoe Perches					$fixed[$fixlinenr] =~
35423705ce5bSJoe Perches					    s/\b$name\s+\(/$name\(/;
35433705ce5bSJoe Perches				}
3544f0a594c1SAndy Whitcroft			}
35456c72ffaaSAndy Whitcroft		}
35469a4cad4eSEric Nelson
3547653d4876SAndy Whitcroft# Check operator spacing.
35480a920b5bSAndy Whitcroft		if (!($line=~/\#\s*include/)) {
35493705ce5bSJoe Perches			my $fixed_line = "";
35503705ce5bSJoe Perches			my $line_fixed = 0;
35513705ce5bSJoe Perches
35529c0ca6f9SAndy Whitcroft			my $ops = qr{
35539c0ca6f9SAndy Whitcroft				<<=|>>=|<=|>=|==|!=|
35549c0ca6f9SAndy Whitcroft				\+=|-=|\*=|\/=|%=|\^=|\|=|&=|
35559c0ca6f9SAndy Whitcroft				=>|->|<<|>>|<|>|=|!|~|
35561f65f947SAndy Whitcroft				&&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
355784731623SJoe Perches				\?:|\?|:
35589c0ca6f9SAndy Whitcroft			}x;
3559cf655043SAndy Whitcroft			my @elements = split(/($ops|;)/, $opline);
35603705ce5bSJoe Perches
35613705ce5bSJoe Perches##			print("element count: <" . $#elements . ">\n");
35623705ce5bSJoe Perches##			foreach my $el (@elements) {
35633705ce5bSJoe Perches##				print("el: <$el>\n");
35643705ce5bSJoe Perches##			}
35653705ce5bSJoe Perches
35663705ce5bSJoe Perches			my @fix_elements = ();
356700df344fSAndy Whitcroft			my $off = 0;
35686c72ffaaSAndy Whitcroft
35693705ce5bSJoe Perches			foreach my $el (@elements) {
35703705ce5bSJoe Perches				push(@fix_elements, substr($rawline, $off, length($el)));
35713705ce5bSJoe Perches				$off += length($el);
35723705ce5bSJoe Perches			}
35733705ce5bSJoe Perches
35743705ce5bSJoe Perches			$off = 0;
35753705ce5bSJoe Perches
35766c72ffaaSAndy Whitcroft			my $blank = copy_spacing($opline);
3577b34c648bSJoe Perches			my $last_after = -1;
35786c72ffaaSAndy Whitcroft
35790a920b5bSAndy Whitcroft			for (my $n = 0; $n < $#elements; $n += 2) {
35803705ce5bSJoe Perches
35813705ce5bSJoe Perches				my $good = $fix_elements[$n] . $fix_elements[$n + 1];
35823705ce5bSJoe Perches
35833705ce5bSJoe Perches##				print("n: <$n> good: <$good>\n");
35843705ce5bSJoe Perches
35854a0df2efSAndy Whitcroft				$off += length($elements[$n]);
35864a0df2efSAndy Whitcroft
358725985edcSLucas De Marchi				# Pick up the preceding and succeeding characters.
3588773647a0SAndy Whitcroft				my $ca = substr($opline, 0, $off);
3589773647a0SAndy Whitcroft				my $cc = '';
3590773647a0SAndy Whitcroft				if (length($opline) >= ($off + length($elements[$n + 1]))) {
3591773647a0SAndy Whitcroft					$cc = substr($opline, $off + length($elements[$n + 1]));
3592773647a0SAndy Whitcroft				}
3593773647a0SAndy Whitcroft				my $cb = "$ca$;$cc";
3594773647a0SAndy Whitcroft
35954a0df2efSAndy Whitcroft				my $a = '';
35964a0df2efSAndy Whitcroft				$a = 'V' if ($elements[$n] ne '');
35974a0df2efSAndy Whitcroft				$a = 'W' if ($elements[$n] =~ /\s$/);
3598cf655043SAndy Whitcroft				$a = 'C' if ($elements[$n] =~ /$;$/);
35994a0df2efSAndy Whitcroft				$a = 'B' if ($elements[$n] =~ /(\[|\()$/);
36004a0df2efSAndy Whitcroft				$a = 'O' if ($elements[$n] eq '');
3601773647a0SAndy Whitcroft				$a = 'E' if ($ca =~ /^\s*$/);
36024a0df2efSAndy Whitcroft
36030a920b5bSAndy Whitcroft				my $op = $elements[$n + 1];
36044a0df2efSAndy Whitcroft
36054a0df2efSAndy Whitcroft				my $c = '';
36060a920b5bSAndy Whitcroft				if (defined $elements[$n + 2]) {
36074a0df2efSAndy Whitcroft					$c = 'V' if ($elements[$n + 2] ne '');
36084a0df2efSAndy Whitcroft					$c = 'W' if ($elements[$n + 2] =~ /^\s/);
3609cf655043SAndy Whitcroft					$c = 'C' if ($elements[$n + 2] =~ /^$;/);
36104a0df2efSAndy Whitcroft					$c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
36114a0df2efSAndy Whitcroft					$c = 'O' if ($elements[$n + 2] eq '');
36128b1b3378SAndy Whitcroft					$c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
36134a0df2efSAndy Whitcroft				} else {
36144a0df2efSAndy Whitcroft					$c = 'E';
36150a920b5bSAndy Whitcroft				}
36160a920b5bSAndy Whitcroft
36174a0df2efSAndy Whitcroft				my $ctx = "${a}x${c}";
36184a0df2efSAndy Whitcroft
36194a0df2efSAndy Whitcroft				my $at = "(ctx:$ctx)";
36204a0df2efSAndy Whitcroft
36216c72ffaaSAndy Whitcroft				my $ptr = substr($blank, 0, $off) . "^";
3622de7d4f0eSAndy Whitcroft				my $hereptr = "$hereline$ptr\n";
36230a920b5bSAndy Whitcroft
362474048ed8SAndy Whitcroft				# Pull out the value of this operator.
36256c72ffaaSAndy Whitcroft				my $op_type = substr($curr_values, $off + 1, 1);
36260a920b5bSAndy Whitcroft
36271f65f947SAndy Whitcroft				# Get the full operator variant.
36281f65f947SAndy Whitcroft				my $opv = $op . substr($curr_vars, $off, 1);
36291f65f947SAndy Whitcroft
363013214adfSAndy Whitcroft				# Ignore operators passed as parameters.
363113214adfSAndy Whitcroft				if ($op_type ne 'V' &&
363213214adfSAndy Whitcroft				    $ca =~ /\s$/ && $cc =~ /^\s*,/) {
363313214adfSAndy Whitcroft
3634cf655043SAndy Whitcroft#				# Ignore comments
3635cf655043SAndy Whitcroft#				} elsif ($op =~ /^$;+$/) {
363613214adfSAndy Whitcroft
3637d8aaf121SAndy Whitcroft				# ; should have either the end of line or a space or \ after it
363813214adfSAndy Whitcroft				} elsif ($op eq ';') {
3639cf655043SAndy Whitcroft					if ($ctx !~ /.x[WEBC]/ &&
3640cf655043SAndy Whitcroft					    $cc !~ /^\\/ && $cc !~ /^;/) {
36413705ce5bSJoe Perches						if (ERROR("SPACING",
36423705ce5bSJoe Perches							  "space required after that '$op' $at\n" . $hereptr)) {
3643b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
36443705ce5bSJoe Perches							$line_fixed = 1;
36453705ce5bSJoe Perches						}
3646d8aaf121SAndy Whitcroft					}
3647d8aaf121SAndy Whitcroft
3648d8aaf121SAndy Whitcroft				# // is a comment
3649d8aaf121SAndy Whitcroft				} elsif ($op eq '//') {
36500a920b5bSAndy Whitcroft
3651b00e4814SJoe Perches				#   :   when part of a bitfield
3652b00e4814SJoe Perches				} elsif ($opv eq ':B') {
3653b00e4814SJoe Perches					# skip the bitfield test for now
3654b00e4814SJoe Perches
36551f65f947SAndy Whitcroft				# No spaces for:
36561f65f947SAndy Whitcroft				#   ->
3657b00e4814SJoe Perches				} elsif ($op eq '->') {
36584a0df2efSAndy Whitcroft					if ($ctx =~ /Wx.|.xW/) {
36593705ce5bSJoe Perches						if (ERROR("SPACING",
36603705ce5bSJoe Perches							  "spaces prohibited around that '$op' $at\n" . $hereptr)) {
3661b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
36623705ce5bSJoe Perches							if (defined $fix_elements[$n + 2]) {
36633705ce5bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
36643705ce5bSJoe Perches							}
3665b34c648bSJoe Perches							$line_fixed = 1;
36663705ce5bSJoe Perches						}
36670a920b5bSAndy Whitcroft					}
36680a920b5bSAndy Whitcroft
36692381097bSJoe Perches				# , must not have a space before and must have a space on the right.
36700a920b5bSAndy Whitcroft				} elsif ($op eq ',') {
36712381097bSJoe Perches					my $rtrim_before = 0;
36722381097bSJoe Perches					my $space_after = 0;
36732381097bSJoe Perches					if ($ctx =~ /Wx./) {
36742381097bSJoe Perches						if (ERROR("SPACING",
36752381097bSJoe Perches							  "space prohibited before that '$op' $at\n" . $hereptr)) {
36762381097bSJoe Perches							$line_fixed = 1;
36772381097bSJoe Perches							$rtrim_before = 1;
36782381097bSJoe Perches						}
36792381097bSJoe Perches					}
3680cf655043SAndy Whitcroft					if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
36813705ce5bSJoe Perches						if (ERROR("SPACING",
36823705ce5bSJoe Perches							  "space required after that '$op' $at\n" . $hereptr)) {
36833705ce5bSJoe Perches							$line_fixed = 1;
3684b34c648bSJoe Perches							$last_after = $n;
36852381097bSJoe Perches							$space_after = 1;
36862381097bSJoe Perches						}
36872381097bSJoe Perches					}
36882381097bSJoe Perches					if ($rtrim_before || $space_after) {
36892381097bSJoe Perches						if ($rtrim_before) {
36902381097bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
36912381097bSJoe Perches						} else {
36922381097bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
36932381097bSJoe Perches						}
36942381097bSJoe Perches						if ($space_after) {
36952381097bSJoe Perches							$good .= " ";
36963705ce5bSJoe Perches						}
36970a920b5bSAndy Whitcroft					}
36980a920b5bSAndy Whitcroft
36999c0ca6f9SAndy Whitcroft				# '*' as part of a type definition -- reported already.
370074048ed8SAndy Whitcroft				} elsif ($opv eq '*_') {
37019c0ca6f9SAndy Whitcroft					#warn "'*' is part of type\n";
37029c0ca6f9SAndy Whitcroft
37039c0ca6f9SAndy Whitcroft				# unary operators should have a space before and
37049c0ca6f9SAndy Whitcroft				# none after.  May be left adjacent to another
37059c0ca6f9SAndy Whitcroft				# unary operator, or a cast
37069c0ca6f9SAndy Whitcroft				} elsif ($op eq '!' || $op eq '~' ||
370774048ed8SAndy Whitcroft					 $opv eq '*U' || $opv eq '-U' ||
37080d413866SAndy Whitcroft					 $opv eq '&U' || $opv eq '&&U') {
3709cf655043SAndy Whitcroft					if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
37103705ce5bSJoe Perches						if (ERROR("SPACING",
37113705ce5bSJoe Perches							  "space required before that '$op' $at\n" . $hereptr)) {
3712b34c648bSJoe Perches							if ($n != $last_after + 2) {
3713b34c648bSJoe Perches								$good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]);
37143705ce5bSJoe Perches								$line_fixed = 1;
37153705ce5bSJoe Perches							}
37160a920b5bSAndy Whitcroft						}
3717b34c648bSJoe Perches					}
3718a3340b35SAndy Whitcroft					if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
3719171ae1a4SAndy Whitcroft						# A unary '*' may be const
3720171ae1a4SAndy Whitcroft
3721171ae1a4SAndy Whitcroft					} elsif ($ctx =~ /.xW/) {
37223705ce5bSJoe Perches						if (ERROR("SPACING",
37233705ce5bSJoe Perches							  "space prohibited after that '$op' $at\n" . $hereptr)) {
3724b34c648bSJoe Perches							$good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]);
37253705ce5bSJoe Perches							if (defined $fix_elements[$n + 2]) {
37263705ce5bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
37273705ce5bSJoe Perches							}
3728b34c648bSJoe Perches							$line_fixed = 1;
37293705ce5bSJoe Perches						}
37300a920b5bSAndy Whitcroft					}
37310a920b5bSAndy Whitcroft
37320a920b5bSAndy Whitcroft				# unary ++ and unary -- are allowed no space on one side.
37330a920b5bSAndy Whitcroft				} elsif ($op eq '++' or $op eq '--') {
3734773647a0SAndy Whitcroft					if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
37353705ce5bSJoe Perches						if (ERROR("SPACING",
37363705ce5bSJoe Perches							  "space required one side of that '$op' $at\n" . $hereptr)) {
3737b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
37383705ce5bSJoe Perches							$line_fixed = 1;
37393705ce5bSJoe Perches						}
37400a920b5bSAndy Whitcroft					}
3741773647a0SAndy Whitcroft					if ($ctx =~ /Wx[BE]/ ||
3742773647a0SAndy Whitcroft					    ($ctx =~ /Wx./ && $cc =~ /^;/)) {
37433705ce5bSJoe Perches						if (ERROR("SPACING",
37443705ce5bSJoe Perches							  "space prohibited before that '$op' $at\n" . $hereptr)) {
3745b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
37463705ce5bSJoe Perches							$line_fixed = 1;
37473705ce5bSJoe Perches						}
3748653d4876SAndy Whitcroft					}
3749773647a0SAndy Whitcroft					if ($ctx =~ /ExW/) {
37503705ce5bSJoe Perches						if (ERROR("SPACING",
37513705ce5bSJoe Perches							  "space prohibited after that '$op' $at\n" . $hereptr)) {
3752b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
37533705ce5bSJoe Perches							if (defined $fix_elements[$n + 2]) {
37543705ce5bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
3755773647a0SAndy Whitcroft							}
3756b34c648bSJoe Perches							$line_fixed = 1;
37573705ce5bSJoe Perches						}
37583705ce5bSJoe Perches					}
37590a920b5bSAndy Whitcroft
37600a920b5bSAndy Whitcroft				# << and >> may either have or not have spaces both sides
37619c0ca6f9SAndy Whitcroft				} elsif ($op eq '<<' or $op eq '>>' or
37629c0ca6f9SAndy Whitcroft					 $op eq '&' or $op eq '^' or $op eq '|' or
37639c0ca6f9SAndy Whitcroft					 $op eq '+' or $op eq '-' or
3764c2fdda0dSAndy Whitcroft					 $op eq '*' or $op eq '/' or
3765c2fdda0dSAndy Whitcroft					 $op eq '%')
37660a920b5bSAndy Whitcroft				{
3767d2e025f3SJoe Perches					if ($check) {
3768d2e025f3SJoe Perches						if (defined $fix_elements[$n + 2] && $ctx !~ /[EW]x[EW]/) {
3769d2e025f3SJoe Perches							if (CHK("SPACING",
3770d2e025f3SJoe Perches								"spaces preferred around that '$op' $at\n" . $hereptr)) {
3771d2e025f3SJoe Perches								$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
3772d2e025f3SJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
3773d2e025f3SJoe Perches								$line_fixed = 1;
3774d2e025f3SJoe Perches							}
3775d2e025f3SJoe Perches						} elsif (!defined $fix_elements[$n + 2] && $ctx !~ /Wx[OE]/) {
3776d2e025f3SJoe Perches							if (CHK("SPACING",
3777d2e025f3SJoe Perches								"space preferred before that '$op' $at\n" . $hereptr)) {
3778d2e025f3SJoe Perches								$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]);
3779d2e025f3SJoe Perches								$line_fixed = 1;
3780d2e025f3SJoe Perches							}
3781d2e025f3SJoe Perches						}
3782d2e025f3SJoe Perches					} elsif ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
37833705ce5bSJoe Perches						if (ERROR("SPACING",
37843705ce5bSJoe Perches							  "need consistent spacing around '$op' $at\n" . $hereptr)) {
3785b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
3786b34c648bSJoe Perches							if (defined $fix_elements[$n + 2]) {
3787b34c648bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
3788b34c648bSJoe Perches							}
37893705ce5bSJoe Perches							$line_fixed = 1;
37903705ce5bSJoe Perches						}
37910a920b5bSAndy Whitcroft					}
37920a920b5bSAndy Whitcroft
37931f65f947SAndy Whitcroft				# A colon needs no spaces before when it is
37941f65f947SAndy Whitcroft				# terminating a case value or a label.
37951f65f947SAndy Whitcroft				} elsif ($opv eq ':C' || $opv eq ':L') {
37961f65f947SAndy Whitcroft					if ($ctx =~ /Wx./) {
37973705ce5bSJoe Perches						if (ERROR("SPACING",
37983705ce5bSJoe Perches							  "space prohibited before that '$op' $at\n" . $hereptr)) {
3799b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
38003705ce5bSJoe Perches							$line_fixed = 1;
38013705ce5bSJoe Perches						}
38021f65f947SAndy Whitcroft					}
38031f65f947SAndy Whitcroft
38040a920b5bSAndy Whitcroft				# All the others need spaces both sides.
3805cf655043SAndy Whitcroft				} elsif ($ctx !~ /[EWC]x[CWE]/) {
38061f65f947SAndy Whitcroft					my $ok = 0;
38071f65f947SAndy Whitcroft
380822f2a2efSAndy Whitcroft					# Ignore email addresses <foo@bar>
38091f65f947SAndy Whitcroft					if (($op eq '<' &&
38101f65f947SAndy Whitcroft					     $cc =~ /^\S+\@\S+>/) ||
38111f65f947SAndy Whitcroft					    ($op eq '>' &&
38121f65f947SAndy Whitcroft					     $ca =~ /<\S+\@\S+$/))
38131f65f947SAndy Whitcroft					{
38141f65f947SAndy Whitcroft					    	$ok = 1;
38151f65f947SAndy Whitcroft					}
38161f65f947SAndy Whitcroft
381784731623SJoe Perches					# messages are ERROR, but ?: are CHK
38181f65f947SAndy Whitcroft					if ($ok == 0) {
381984731623SJoe Perches						my $msg_type = \&ERROR;
382084731623SJoe Perches						$msg_type = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
382184731623SJoe Perches
382284731623SJoe Perches						if (&{$msg_type}("SPACING",
38233705ce5bSJoe Perches								 "spaces required around that '$op' $at\n" . $hereptr)) {
3824b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
3825b34c648bSJoe Perches							if (defined $fix_elements[$n + 2]) {
3826b34c648bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
3827b34c648bSJoe Perches							}
38283705ce5bSJoe Perches							$line_fixed = 1;
38293705ce5bSJoe Perches						}
38300a920b5bSAndy Whitcroft					}
383122f2a2efSAndy Whitcroft				}
38324a0df2efSAndy Whitcroft				$off += length($elements[$n + 1]);
38333705ce5bSJoe Perches
38343705ce5bSJoe Perches##				print("n: <$n> GOOD: <$good>\n");
38353705ce5bSJoe Perches
38363705ce5bSJoe Perches				$fixed_line = $fixed_line . $good;
38370a920b5bSAndy Whitcroft			}
38383705ce5bSJoe Perches
38393705ce5bSJoe Perches			if (($#elements % 2) == 0) {
38403705ce5bSJoe Perches				$fixed_line = $fixed_line . $fix_elements[$#elements];
38413705ce5bSJoe Perches			}
38423705ce5bSJoe Perches
3843194f66fcSJoe Perches			if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) {
3844194f66fcSJoe Perches				$fixed[$fixlinenr] = $fixed_line;
38453705ce5bSJoe Perches			}
38463705ce5bSJoe Perches
38473705ce5bSJoe Perches
38480a920b5bSAndy Whitcroft		}
38490a920b5bSAndy Whitcroft
3850786b6326SJoe Perches# check for whitespace before a non-naked semicolon
3851d2e248e7SJoe Perches		if ($line =~ /^\+.*\S\s+;\s*$/) {
3852786b6326SJoe Perches			if (WARN("SPACING",
3853786b6326SJoe Perches				 "space prohibited before semicolon\n" . $herecurr) &&
3854786b6326SJoe Perches			    $fix) {
3855194f66fcSJoe Perches				1 while $fixed[$fixlinenr] =~
3856786b6326SJoe Perches				    s/^(\+.*\S)\s+;/$1;/;
3857786b6326SJoe Perches			}
3858786b6326SJoe Perches		}
3859786b6326SJoe Perches
3860f0a594c1SAndy Whitcroft# check for multiple assignments
3861f0a594c1SAndy Whitcroft		if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
3862000d1cc1SJoe Perches			CHK("MULTIPLE_ASSIGNMENTS",
3863000d1cc1SJoe Perches			    "multiple assignments should be avoided\n" . $herecurr);
3864f0a594c1SAndy Whitcroft		}
3865f0a594c1SAndy Whitcroft
386622f2a2efSAndy Whitcroft## # check for multiple declarations, allowing for a function declaration
386722f2a2efSAndy Whitcroft## # continuation.
386822f2a2efSAndy Whitcroft## 		if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
386922f2a2efSAndy Whitcroft## 		    $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
387022f2a2efSAndy Whitcroft##
387122f2a2efSAndy Whitcroft## 			# Remove any bracketed sections to ensure we do not
387222f2a2efSAndy Whitcroft## 			# falsly report the parameters of functions.
387322f2a2efSAndy Whitcroft## 			my $ln = $line;
387422f2a2efSAndy Whitcroft## 			while ($ln =~ s/\([^\(\)]*\)//g) {
387522f2a2efSAndy Whitcroft## 			}
387622f2a2efSAndy Whitcroft## 			if ($ln =~ /,/) {
3877000d1cc1SJoe Perches## 				WARN("MULTIPLE_DECLARATION",
3878000d1cc1SJoe Perches##				     "declaring multiple variables together should be avoided\n" . $herecurr);
387922f2a2efSAndy Whitcroft## 			}
388022f2a2efSAndy Whitcroft## 		}
3881f0a594c1SAndy Whitcroft
38820a920b5bSAndy Whitcroft#need space before brace following if, while, etc
388322f2a2efSAndy Whitcroft		if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
388422f2a2efSAndy Whitcroft		    $line =~ /do{/) {
38853705ce5bSJoe Perches			if (ERROR("SPACING",
38863705ce5bSJoe Perches				  "space required before the open brace '{'\n" . $herecurr) &&
38873705ce5bSJoe Perches			    $fix) {
3888194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/^(\+.*(?:do|\))){/$1 {/;
38893705ce5bSJoe Perches			}
3890de7d4f0eSAndy Whitcroft		}
3891de7d4f0eSAndy Whitcroft
3892c4a62ef9SJoe Perches## # check for blank lines before declarations
3893c4a62ef9SJoe Perches##		if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
3894c4a62ef9SJoe Perches##		    $prevrawline =~ /^.\s*$/) {
3895c4a62ef9SJoe Perches##			WARN("SPACING",
3896c4a62ef9SJoe Perches##			     "No blank lines before declarations\n" . $hereprev);
3897c4a62ef9SJoe Perches##		}
3898c4a62ef9SJoe Perches##
3899c4a62ef9SJoe Perches
3900de7d4f0eSAndy Whitcroft# closing brace should have a space following it when it has anything
3901de7d4f0eSAndy Whitcroft# on the line
3902de7d4f0eSAndy Whitcroft		if ($line =~ /}(?!(?:,|;|\)))\S/) {
3903d5e616fcSJoe Perches			if (ERROR("SPACING",
3904d5e616fcSJoe Perches				  "space required after that close brace '}'\n" . $herecurr) &&
3905d5e616fcSJoe Perches			    $fix) {
3906194f66fcSJoe Perches				$fixed[$fixlinenr] =~
3907d5e616fcSJoe Perches				    s/}((?!(?:,|;|\)))\S)/} $1/;
3908d5e616fcSJoe Perches			}
39090a920b5bSAndy Whitcroft		}
39100a920b5bSAndy Whitcroft
391122f2a2efSAndy Whitcroft# check spacing on square brackets
391222f2a2efSAndy Whitcroft		if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
39133705ce5bSJoe Perches			if (ERROR("SPACING",
39143705ce5bSJoe Perches				  "space prohibited after that open square bracket '['\n" . $herecurr) &&
39153705ce5bSJoe Perches			    $fix) {
3916194f66fcSJoe Perches				$fixed[$fixlinenr] =~
39173705ce5bSJoe Perches				    s/\[\s+/\[/;
39183705ce5bSJoe Perches			}
391922f2a2efSAndy Whitcroft		}
392022f2a2efSAndy Whitcroft		if ($line =~ /\s\]/) {
39213705ce5bSJoe Perches			if (ERROR("SPACING",
39223705ce5bSJoe Perches				  "space prohibited before that close square bracket ']'\n" . $herecurr) &&
39233705ce5bSJoe Perches			    $fix) {
3924194f66fcSJoe Perches				$fixed[$fixlinenr] =~
39253705ce5bSJoe Perches				    s/\s+\]/\]/;
39263705ce5bSJoe Perches			}
392722f2a2efSAndy Whitcroft		}
392822f2a2efSAndy Whitcroft
3929c45dcabdSAndy Whitcroft# check spacing on parentheses
39309c0ca6f9SAndy Whitcroft		if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
39319c0ca6f9SAndy Whitcroft		    $line !~ /for\s*\(\s+;/) {
39323705ce5bSJoe Perches			if (ERROR("SPACING",
39333705ce5bSJoe Perches				  "space prohibited after that open parenthesis '('\n" . $herecurr) &&
39343705ce5bSJoe Perches			    $fix) {
3935194f66fcSJoe Perches				$fixed[$fixlinenr] =~
39363705ce5bSJoe Perches				    s/\(\s+/\(/;
39373705ce5bSJoe Perches			}
393822f2a2efSAndy Whitcroft		}
393913214adfSAndy Whitcroft		if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
3940c45dcabdSAndy Whitcroft		    $line !~ /for\s*\(.*;\s+\)/ &&
3941c45dcabdSAndy Whitcroft		    $line !~ /:\s+\)/) {
39423705ce5bSJoe Perches			if (ERROR("SPACING",
39433705ce5bSJoe Perches				  "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
39443705ce5bSJoe Perches			    $fix) {
3945194f66fcSJoe Perches				$fixed[$fixlinenr] =~
39463705ce5bSJoe Perches				    s/\s+\)/\)/;
39473705ce5bSJoe Perches			}
394822f2a2efSAndy Whitcroft		}
394922f2a2efSAndy Whitcroft
3950e2826fd0SJoe Perches# check unnecessary parentheses around addressof/dereference single $Lvals
3951e2826fd0SJoe Perches# ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar
3952e2826fd0SJoe Perches
3953e2826fd0SJoe Perches		while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) {
3954ea4acbb1SJoe Perches			my $var = $1;
3955ea4acbb1SJoe Perches			if (CHK("UNNECESSARY_PARENTHESES",
3956ea4acbb1SJoe Perches				"Unnecessary parentheses around $var\n" . $herecurr) &&
3957ea4acbb1SJoe Perches			    $fix) {
3958ea4acbb1SJoe Perches				$fixed[$fixlinenr] =~ s/\(\s*\Q$var\E\s*\)/$var/;
3959ea4acbb1SJoe Perches			}
3960ea4acbb1SJoe Perches		}
3961ea4acbb1SJoe Perches
3962ea4acbb1SJoe Perches# check for unnecessary parentheses around function pointer uses
3963ea4acbb1SJoe Perches# ie: (foo->bar)(); should be foo->bar();
3964ea4acbb1SJoe Perches# but not "if (foo->bar) (" to avoid some false positives
3965ea4acbb1SJoe Perches		if ($line =~ /(\bif\s*|)(\(\s*$Ident\s*(?:$Member\s*)+\))[ \t]*\(/ && $1 !~ /^if/) {
3966ea4acbb1SJoe Perches			my $var = $2;
3967ea4acbb1SJoe Perches			if (CHK("UNNECESSARY_PARENTHESES",
3968ea4acbb1SJoe Perches				"Unnecessary parentheses around function pointer $var\n" . $herecurr) &&
3969ea4acbb1SJoe Perches			    $fix) {
3970ea4acbb1SJoe Perches				my $var2 = deparenthesize($var);
3971ea4acbb1SJoe Perches				$var2 =~ s/\s//g;
3972ea4acbb1SJoe Perches				$fixed[$fixlinenr] =~ s/\Q$var\E/$var2/;
3973ea4acbb1SJoe Perches			}
3974e2826fd0SJoe Perches		}
3975e2826fd0SJoe Perches
39760a920b5bSAndy Whitcroft#goto labels aren't indented, allow a single space however
39774a0df2efSAndy Whitcroft		if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
39780a920b5bSAndy Whitcroft		   !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
39793705ce5bSJoe Perches			if (WARN("INDENTED_LABEL",
39803705ce5bSJoe Perches				 "labels should not be indented\n" . $herecurr) &&
39813705ce5bSJoe Perches			    $fix) {
3982194f66fcSJoe Perches				$fixed[$fixlinenr] =~
39833705ce5bSJoe Perches				    s/^(.)\s+/$1/;
39843705ce5bSJoe Perches			}
39850a920b5bSAndy Whitcroft		}
39860a920b5bSAndy Whitcroft
39875b9553abSJoe Perches# return is not a function
3988507e5141SJoe Perches		if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {
3989c45dcabdSAndy Whitcroft			my $spacing = $1;
3990507e5141SJoe Perches			if ($^V && $^V ge 5.10.0 &&
39915b9553abSJoe Perches			    $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) {
39925b9553abSJoe Perches				my $value = $1;
39935b9553abSJoe Perches				$value = deparenthesize($value);
39945b9553abSJoe Perches				if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) {
3995000d1cc1SJoe Perches					ERROR("RETURN_PARENTHESES",
3996000d1cc1SJoe Perches					      "return is not a function, parentheses are not required\n" . $herecurr);
39975b9553abSJoe Perches				}
3998c45dcabdSAndy Whitcroft			} elsif ($spacing !~ /\s+/) {
3999000d1cc1SJoe Perches				ERROR("SPACING",
4000000d1cc1SJoe Perches				      "space required before the open parenthesis '('\n" . $herecurr);
4001c45dcabdSAndy Whitcroft			}
4002c45dcabdSAndy Whitcroft		}
4003507e5141SJoe Perches
4004b43ae21bSJoe Perches# unnecessary return in a void function
4005b43ae21bSJoe Perches# at end-of-function, with the previous line a single leading tab, then return;
4006b43ae21bSJoe Perches# and the line before that not a goto label target like "out:"
4007b43ae21bSJoe Perches		if ($sline =~ /^[ \+]}\s*$/ &&
4008b43ae21bSJoe Perches		    $prevline =~ /^\+\treturn\s*;\s*$/ &&
4009b43ae21bSJoe Perches		    $linenr >= 3 &&
4010b43ae21bSJoe Perches		    $lines[$linenr - 3] =~ /^[ +]/ &&
4011b43ae21bSJoe Perches		    $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) {
40129819cf25SJoe Perches			WARN("RETURN_VOID",
4013b43ae21bSJoe Perches			     "void function return statements are not generally useful\n" . $hereprev);
40149819cf25SJoe Perches               }
40159819cf25SJoe Perches
4016189248d8SJoe Perches# if statements using unnecessary parentheses - ie: if ((foo == bar))
4017189248d8SJoe Perches		if ($^V && $^V ge 5.10.0 &&
4018189248d8SJoe Perches		    $line =~ /\bif\s*((?:\(\s*){2,})/) {
4019189248d8SJoe Perches			my $openparens = $1;
4020189248d8SJoe Perches			my $count = $openparens =~ tr@\(@\(@;
4021189248d8SJoe Perches			my $msg = "";
4022189248d8SJoe Perches			if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) {
4023189248d8SJoe Perches				my $comp = $4;	#Not $1 because of $LvalOrFunc
4024189248d8SJoe Perches				$msg = " - maybe == should be = ?" if ($comp eq "==");
4025189248d8SJoe Perches				WARN("UNNECESSARY_PARENTHESES",
4026189248d8SJoe Perches				     "Unnecessary parentheses$msg\n" . $herecurr);
4027189248d8SJoe Perches			}
4028189248d8SJoe Perches		}
4029189248d8SJoe Perches
4030f34e4a4fSJoe Perches# Return of what appears to be an errno should normally be negative
4031f34e4a4fSJoe Perches		if ($sline =~ /\breturn(?:\s*\(+\s*|\s+)(E[A-Z]+)(?:\s*\)+\s*|\s*)[;:,]/) {
403253a3c448SAndy Whitcroft			my $name = $1;
403353a3c448SAndy Whitcroft			if ($name ne 'EOF' && $name ne 'ERROR') {
4034000d1cc1SJoe Perches				WARN("USE_NEGATIVE_ERRNO",
4035f34e4a4fSJoe Perches				     "return of an errno should typically be negative (ie: return -$1)\n" . $herecurr);
403653a3c448SAndy Whitcroft			}
403753a3c448SAndy Whitcroft		}
4038c45dcabdSAndy Whitcroft
40390a920b5bSAndy Whitcroft# Need a space before open parenthesis after if, while etc
40404a0df2efSAndy Whitcroft		if ($line =~ /\b(if|while|for|switch)\(/) {
40413705ce5bSJoe Perches			if (ERROR("SPACING",
40423705ce5bSJoe Perches				  "space required before the open parenthesis '('\n" . $herecurr) &&
40433705ce5bSJoe Perches			    $fix) {
4044194f66fcSJoe Perches				$fixed[$fixlinenr] =~
40453705ce5bSJoe Perches				    s/\b(if|while|for|switch)\(/$1 \(/;
40463705ce5bSJoe Perches			}
40470a920b5bSAndy Whitcroft		}
40480a920b5bSAndy Whitcroft
4049f5fe35ddSAndy Whitcroft# Check for illegal assignment in if conditional -- and check for trailing
4050f5fe35ddSAndy Whitcroft# statements after the conditional.
4051170d3a22SAndy Whitcroft		if ($line =~ /do\s*(?!{)/) {
40523e469cdcSAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
40533e469cdcSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0)
40543e469cdcSAndy Whitcroft					if (!defined $stat);
4055170d3a22SAndy Whitcroft			my ($stat_next) = ctx_statement_block($line_nr_next,
4056170d3a22SAndy Whitcroft						$remain_next, $off_next);
4057170d3a22SAndy Whitcroft			$stat_next =~ s/\n./\n /g;
4058170d3a22SAndy Whitcroft			##print "stat<$stat> stat_next<$stat_next>\n";
4059170d3a22SAndy Whitcroft
4060170d3a22SAndy Whitcroft			if ($stat_next =~ /^\s*while\b/) {
4061170d3a22SAndy Whitcroft				# If the statement carries leading newlines,
4062170d3a22SAndy Whitcroft				# then count those as offsets.
4063170d3a22SAndy Whitcroft				my ($whitespace) =
4064170d3a22SAndy Whitcroft					($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
4065170d3a22SAndy Whitcroft				my $offset =
4066170d3a22SAndy Whitcroft					statement_rawlines($whitespace) - 1;
4067170d3a22SAndy Whitcroft
4068170d3a22SAndy Whitcroft				$suppress_whiletrailers{$line_nr_next +
4069170d3a22SAndy Whitcroft								$offset} = 1;
4070170d3a22SAndy Whitcroft			}
4071170d3a22SAndy Whitcroft		}
4072170d3a22SAndy Whitcroft		if (!defined $suppress_whiletrailers{$linenr} &&
4073c11230f4SJoe Perches		    defined($stat) && defined($cond) &&
4074170d3a22SAndy Whitcroft		    $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
4075171ae1a4SAndy Whitcroft			my ($s, $c) = ($stat, $cond);
40768905a67cSAndy Whitcroft
4077b53c8e10SAndy Whitcroft			if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
4078000d1cc1SJoe Perches				ERROR("ASSIGN_IN_IF",
4079000d1cc1SJoe Perches				      "do not use assignment in if condition\n" . $herecurr);
40808905a67cSAndy Whitcroft			}
40818905a67cSAndy Whitcroft
40828905a67cSAndy Whitcroft			# Find out what is on the end of the line after the
40838905a67cSAndy Whitcroft			# conditional.
4084773647a0SAndy Whitcroft			substr($s, 0, length($c), '');
40858905a67cSAndy Whitcroft			$s =~ s/\n.*//g;
408613214adfSAndy Whitcroft			$s =~ s/$;//g; 	# Remove any comments
408753210168SAndy Whitcroft			if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
408853210168SAndy Whitcroft			    $c !~ /}\s*while\s*/)
4089773647a0SAndy Whitcroft			{
4090bb44ad39SAndy Whitcroft				# Find out how long the conditional actually is.
4091bb44ad39SAndy Whitcroft				my @newlines = ($c =~ /\n/gs);
4092bb44ad39SAndy Whitcroft				my $cond_lines = 1 + $#newlines;
409342bdf74cSHidetoshi Seto				my $stat_real = '';
4094bb44ad39SAndy Whitcroft
409542bdf74cSHidetoshi Seto				$stat_real = raw_line($linenr, $cond_lines)
409642bdf74cSHidetoshi Seto							. "\n" if ($cond_lines);
4097bb44ad39SAndy Whitcroft				if (defined($stat_real) && $cond_lines > 1) {
4098bb44ad39SAndy Whitcroft					$stat_real = "[...]\n$stat_real";
4099bb44ad39SAndy Whitcroft				}
4100bb44ad39SAndy Whitcroft
4101000d1cc1SJoe Perches				ERROR("TRAILING_STATEMENTS",
4102000d1cc1SJoe Perches				      "trailing statements should be on next line\n" . $herecurr . $stat_real);
41038905a67cSAndy Whitcroft			}
41048905a67cSAndy Whitcroft		}
41058905a67cSAndy Whitcroft
410613214adfSAndy Whitcroft# Check for bitwise tests written as boolean
410713214adfSAndy Whitcroft		if ($line =~ /
410813214adfSAndy Whitcroft			(?:
410913214adfSAndy Whitcroft				(?:\[|\(|\&\&|\|\|)
411013214adfSAndy Whitcroft				\s*0[xX][0-9]+\s*
411113214adfSAndy Whitcroft				(?:\&\&|\|\|)
411213214adfSAndy Whitcroft			|
411313214adfSAndy Whitcroft				(?:\&\&|\|\|)
411413214adfSAndy Whitcroft				\s*0[xX][0-9]+\s*
411513214adfSAndy Whitcroft				(?:\&\&|\|\||\)|\])
411613214adfSAndy Whitcroft			)/x)
411713214adfSAndy Whitcroft		{
4118000d1cc1SJoe Perches			WARN("HEXADECIMAL_BOOLEAN_TEST",
4119000d1cc1SJoe Perches			     "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
412013214adfSAndy Whitcroft		}
412113214adfSAndy Whitcroft
41228905a67cSAndy Whitcroft# if and else should not have general statements after it
412313214adfSAndy Whitcroft		if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
412413214adfSAndy Whitcroft			my $s = $1;
412513214adfSAndy Whitcroft			$s =~ s/$;//g; 	# Remove any comments
412613214adfSAndy Whitcroft			if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
4127000d1cc1SJoe Perches				ERROR("TRAILING_STATEMENTS",
4128000d1cc1SJoe Perches				      "trailing statements should be on next line\n" . $herecurr);
41290a920b5bSAndy Whitcroft			}
413013214adfSAndy Whitcroft		}
413139667782SAndy Whitcroft# if should not continue a brace
413239667782SAndy Whitcroft		if ($line =~ /}\s*if\b/) {
4133000d1cc1SJoe Perches			ERROR("TRAILING_STATEMENTS",
4134048b123fSRasmus Villemoes			      "trailing statements should be on next line (or did you mean 'else if'?)\n" .
413539667782SAndy Whitcroft				$herecurr);
413639667782SAndy Whitcroft		}
4137a1080bf8SAndy Whitcroft# case and default should not have general statements after them
4138a1080bf8SAndy Whitcroft		if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
4139a1080bf8SAndy Whitcroft		    $line !~ /\G(?:
41403fef12d6SAndy Whitcroft			(?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
4141a1080bf8SAndy Whitcroft			\s*return\s+
4142a1080bf8SAndy Whitcroft		    )/xg)
4143a1080bf8SAndy Whitcroft		{
4144000d1cc1SJoe Perches			ERROR("TRAILING_STATEMENTS",
4145000d1cc1SJoe Perches			      "trailing statements should be on next line\n" . $herecurr);
4146a1080bf8SAndy Whitcroft		}
41470a920b5bSAndy Whitcroft
41480a920b5bSAndy Whitcroft		# Check for }<nl>else {, these must be at the same
41490a920b5bSAndy Whitcroft		# indent level to be relevant to each other.
41508b8856f4SJoe Perches		if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ &&
41510a920b5bSAndy Whitcroft		    $previndent == $indent) {
41528b8856f4SJoe Perches			if (ERROR("ELSE_AFTER_BRACE",
41538b8856f4SJoe Perches				  "else should follow close brace '}'\n" . $hereprev) &&
41548b8856f4SJoe Perches			    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
41558b8856f4SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
41568b8856f4SJoe Perches				fix_delete_line($fixlinenr, $rawline);
41578b8856f4SJoe Perches				my $fixedline = $prevrawline;
41588b8856f4SJoe Perches				$fixedline =~ s/}\s*$//;
41598b8856f4SJoe Perches				if ($fixedline !~ /^\+\s*$/) {
41608b8856f4SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
41618b8856f4SJoe Perches				}
41628b8856f4SJoe Perches				$fixedline = $rawline;
41638b8856f4SJoe Perches				$fixedline =~ s/^(.\s*)else/$1} else/;
41648b8856f4SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
41658b8856f4SJoe Perches			}
41660a920b5bSAndy Whitcroft		}
41670a920b5bSAndy Whitcroft
41688b8856f4SJoe Perches		if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ &&
4169c2fdda0dSAndy Whitcroft		    $previndent == $indent) {
4170c2fdda0dSAndy Whitcroft			my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
4171c2fdda0dSAndy Whitcroft
4172c2fdda0dSAndy Whitcroft			# Find out what is on the end of the line after the
4173c2fdda0dSAndy Whitcroft			# conditional.
4174773647a0SAndy Whitcroft			substr($s, 0, length($c), '');
4175c2fdda0dSAndy Whitcroft			$s =~ s/\n.*//g;
4176c2fdda0dSAndy Whitcroft
4177c2fdda0dSAndy Whitcroft			if ($s =~ /^\s*;/) {
41788b8856f4SJoe Perches				if (ERROR("WHILE_AFTER_BRACE",
41798b8856f4SJoe Perches					  "while should follow close brace '}'\n" . $hereprev) &&
41808b8856f4SJoe Perches				    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
41818b8856f4SJoe Perches					fix_delete_line($fixlinenr - 1, $prevrawline);
41828b8856f4SJoe Perches					fix_delete_line($fixlinenr, $rawline);
41838b8856f4SJoe Perches					my $fixedline = $prevrawline;
41848b8856f4SJoe Perches					my $trailing = $rawline;
41858b8856f4SJoe Perches					$trailing =~ s/^\+//;
41868b8856f4SJoe Perches					$trailing = trim($trailing);
41878b8856f4SJoe Perches					$fixedline =~ s/}\s*$/} $trailing/;
41888b8856f4SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
41898b8856f4SJoe Perches				}
4190c2fdda0dSAndy Whitcroft			}
4191c2fdda0dSAndy Whitcroft		}
4192c2fdda0dSAndy Whitcroft
419395e2c602SJoe Perches#Specific variable tests
4194323c1260SJoe Perches		while ($line =~ m{($Constant|$Lval)}g) {
4195323c1260SJoe Perches			my $var = $1;
419695e2c602SJoe Perches
419795e2c602SJoe Perches#gcc binary extension
419895e2c602SJoe Perches			if ($var =~ /^$Binary$/) {
4199d5e616fcSJoe Perches				if (WARN("GCC_BINARY_CONSTANT",
4200d5e616fcSJoe Perches					 "Avoid gcc v4.3+ binary constant extension: <$var>\n" . $herecurr) &&
4201d5e616fcSJoe Perches				    $fix) {
4202d5e616fcSJoe Perches					my $hexval = sprintf("0x%x", oct($var));
4203194f66fcSJoe Perches					$fixed[$fixlinenr] =~
4204d5e616fcSJoe Perches					    s/\b$var\b/$hexval/;
4205d5e616fcSJoe Perches				}
420695e2c602SJoe Perches			}
420795e2c602SJoe Perches
420895e2c602SJoe Perches#CamelCase
4209807bd26cSJoe Perches			if ($var !~ /^$Constant$/ &&
4210be79794bSJoe Perches			    $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
421122735ce8SJoe Perches#Ignore Page<foo> variants
4212807bd26cSJoe Perches			    $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
421322735ce8SJoe Perches#Ignore SI style variants like nS, mV and dB (ie: max_uV, regulator_min_uA_show)
4214f5123576SJulius Werner			    $var !~ /^(?:[a-z_]*?)_?[a-z][A-Z](?:_[a-z_]+)?$/ &&
4215f5123576SJulius Werner#Ignore some three character SI units explicitly, like MiB and KHz
4216f5123576SJulius Werner			    $var !~ /^(?:[a-z_]*?)_?(?:[KMGT]iB|[KMGT]?Hz)(?:_[a-z_]+)?$/) {
42177e781f67SJoe Perches				while ($var =~ m{($Ident)}g) {
42187e781f67SJoe Perches					my $word = $1;
42197e781f67SJoe Perches					next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
4220d8b07710SJoe Perches					if ($check) {
4221d8b07710SJoe Perches						seed_camelcase_includes();
4222d8b07710SJoe Perches						if (!$file && !$camelcase_file_seeded) {
4223d8b07710SJoe Perches							seed_camelcase_file($realfile);
4224d8b07710SJoe Perches							$camelcase_file_seeded = 1;
4225d8b07710SJoe Perches						}
4226d8b07710SJoe Perches					}
42277e781f67SJoe Perches					if (!defined $camelcase{$word}) {
42287e781f67SJoe Perches						$camelcase{$word} = 1;
4229be79794bSJoe Perches						CHK("CAMELCASE",
42307e781f67SJoe Perches						    "Avoid CamelCase: <$word>\n" . $herecurr);
42317e781f67SJoe Perches					}
4232323c1260SJoe Perches				}
4233323c1260SJoe Perches			}
42343445686aSJoe Perches		}
42350a920b5bSAndy Whitcroft
42360a920b5bSAndy Whitcroft#no spaces allowed after \ in define
4237d5e616fcSJoe Perches		if ($line =~ /\#\s*define.*\\\s+$/) {
4238d5e616fcSJoe Perches			if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
4239d5e616fcSJoe Perches				 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
4240d5e616fcSJoe Perches			    $fix) {
4241194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\s+$//;
4242d5e616fcSJoe Perches			}
42430a920b5bSAndy Whitcroft		}
42440a920b5bSAndy Whitcroft
4245653d4876SAndy Whitcroft#warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
4246c45dcabdSAndy Whitcroft		if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
4247e09dec48SAndy Whitcroft			my $file = "$1.h";
4248e09dec48SAndy Whitcroft			my $checkfile = "include/linux/$file";
4249e09dec48SAndy Whitcroft			if (-f "$root/$checkfile" &&
4250e09dec48SAndy Whitcroft			    $realfile ne $checkfile &&
42517840a94cSWolfram Sang			    $1 !~ /$allowed_asm_includes/)
4252c45dcabdSAndy Whitcroft			{
4253e09dec48SAndy Whitcroft				if ($realfile =~ m{^arch/}) {
4254000d1cc1SJoe Perches					CHK("ARCH_INCLUDE_LINUX",
4255000d1cc1SJoe Perches					    "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
4256e09dec48SAndy Whitcroft				} else {
4257000d1cc1SJoe Perches					WARN("INCLUDE_LINUX",
4258000d1cc1SJoe Perches					     "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
4259e09dec48SAndy Whitcroft				}
42600a920b5bSAndy Whitcroft			}
42610a920b5bSAndy Whitcroft		}
42620a920b5bSAndy Whitcroft
4263653d4876SAndy Whitcroft# multi-statement macros should be enclosed in a do while loop, grab the
4264653d4876SAndy Whitcroft# first statement and ensure its the whole macro if its not enclosed
4265cf655043SAndy Whitcroft# in a known good container
4266b8f96a31SAndy Whitcroft		if ($realfile !~ m@/vmlinux.lds.h$@ &&
4267b8f96a31SAndy Whitcroft		    $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
4268d8aaf121SAndy Whitcroft			my $ln = $linenr;
4269d8aaf121SAndy Whitcroft			my $cnt = $realcnt;
4270c45dcabdSAndy Whitcroft			my ($off, $dstat, $dcond, $rest);
4271c45dcabdSAndy Whitcroft			my $ctx = '';
427208a2843eSJoe Perches			my $has_flow_statement = 0;
427308a2843eSJoe Perches			my $has_arg_concat = 0;
4274c45dcabdSAndy Whitcroft			($dstat, $dcond, $ln, $cnt, $off) =
4275f74bd194SAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0);
4276f74bd194SAndy Whitcroft			$ctx = $dstat;
4277c45dcabdSAndy Whitcroft			#print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
4278a3bb97a7SAndy Whitcroft			#print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
4279c45dcabdSAndy Whitcroft
428008a2843eSJoe Perches			$has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/);
428108a2843eSJoe Perches			$has_arg_concat = 1 if ($ctx =~ /\#\#/);
428208a2843eSJoe Perches
4283f74bd194SAndy Whitcroft			$dstat =~ s/^.\s*\#\s*define\s+$Ident(?:\([^\)]*\))?\s*//;
4284292f1a9bSAndy Whitcroft			$dstat =~ s/$;//g;
4285c45dcabdSAndy Whitcroft			$dstat =~ s/\\\n.//g;
4286c45dcabdSAndy Whitcroft			$dstat =~ s/^\s*//s;
4287c45dcabdSAndy Whitcroft			$dstat =~ s/\s*$//s;
4288c45dcabdSAndy Whitcroft
4289c45dcabdSAndy Whitcroft			# Flatten any parentheses and braces
4290bf30d6edSAndy Whitcroft			while ($dstat =~ s/\([^\(\)]*\)/1/ ||
4291bf30d6edSAndy Whitcroft			       $dstat =~ s/\{[^\{\}]*\}/1/ ||
4292c81769fdSAndy Whitcroft			       $dstat =~ s/\[[^\[\]]*\]/1/)
4293bf30d6edSAndy Whitcroft			{
4294c45dcabdSAndy Whitcroft			}
4295c45dcabdSAndy Whitcroft
4296e45bab8eSAndy Whitcroft			# Flatten any obvious string concatentation.
4297e45bab8eSAndy Whitcroft			while ($dstat =~ s/("X*")\s*$Ident/$1/ ||
4298e45bab8eSAndy Whitcroft			       $dstat =~ s/$Ident\s*("X*")/$1/)
4299e45bab8eSAndy Whitcroft			{
4300e45bab8eSAndy Whitcroft			}
4301e45bab8eSAndy Whitcroft
4302c45dcabdSAndy Whitcroft			my $exceptions = qr{
4303c45dcabdSAndy Whitcroft				$Declare|
4304c45dcabdSAndy Whitcroft				module_param_named|
4305a0a0a7a9SKees Cook				MODULE_PARM_DESC|
4306c45dcabdSAndy Whitcroft				DECLARE_PER_CPU|
4307c45dcabdSAndy Whitcroft				DEFINE_PER_CPU|
4308383099fdSAndy Whitcroft				__typeof__\(|
430922fd2d3eSStefani Seibold				union|
431022fd2d3eSStefani Seibold				struct|
4311ea71a0a0SAndy Whitcroft				\.$Ident\s*=\s*|
4312ea71a0a0SAndy Whitcroft				^\"|\"$
4313c45dcabdSAndy Whitcroft			}x;
43145eaa20b9SAndy Whitcroft			#print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
4315f74bd194SAndy Whitcroft			if ($dstat ne '' &&
4316f74bd194SAndy Whitcroft			    $dstat !~ /^(?:$Ident|-?$Constant),$/ &&			# 10, // foo(),
4317f74bd194SAndy Whitcroft			    $dstat !~ /^(?:$Ident|-?$Constant);$/ &&			# foo();
43183cc4b1c3SJoe Perches			    $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ &&		# 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
4319356fd398SJoe Perches			    $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ &&			# character constants
4320f74bd194SAndy Whitcroft			    $dstat !~ /$exceptions/ &&
4321f74bd194SAndy Whitcroft			    $dstat !~ /^\.$Ident\s*=/ &&				# .foo =
4322e942e2c3SJoe Perches			    $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ &&		# stringification #foo
432372f115f9SAndy Whitcroft			    $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ &&	# do {...} while (...); // do {...} while (...)
4324f74bd194SAndy Whitcroft			    $dstat !~ /^for\s*$Constant$/ &&				# for (...)
4325f74bd194SAndy Whitcroft			    $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ &&	# for (...) bar()
4326f74bd194SAndy Whitcroft			    $dstat !~ /^do\s*{/ &&					# do {...
4327f95a7e6aSJoe Perches			    $dstat !~ /^\({/ &&						# ({...
4328f95a7e6aSJoe Perches			    $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
4329c45dcabdSAndy Whitcroft			{
4330f74bd194SAndy Whitcroft				$ctx =~ s/\n*$//;
4331f74bd194SAndy Whitcroft				my $herectx = $here . "\n";
4332f74bd194SAndy Whitcroft				my $cnt = statement_rawlines($ctx);
4333f74bd194SAndy Whitcroft
4334f74bd194SAndy Whitcroft				for (my $n = 0; $n < $cnt; $n++) {
4335f74bd194SAndy Whitcroft					$herectx .= raw_line($linenr, $n) . "\n";
4336c45dcabdSAndy Whitcroft				}
4337c45dcabdSAndy Whitcroft
4338f74bd194SAndy Whitcroft				if ($dstat =~ /;/) {
4339f74bd194SAndy Whitcroft					ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
4340f74bd194SAndy Whitcroft					      "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
4341f74bd194SAndy Whitcroft				} else {
4342000d1cc1SJoe Perches					ERROR("COMPLEX_MACRO",
4343388982b5SAndrew Morton					      "Macros with complex values should be enclosed in parentheses\n" . "$herectx");
4344d8aaf121SAndy Whitcroft				}
43450a920b5bSAndy Whitcroft			}
43465023d347SJoe Perches
434708a2843eSJoe Perches# check for macros with flow control, but without ## concatenation
434808a2843eSJoe Perches# ## concatenation is commonly a macro that defines a function so ignore those
434908a2843eSJoe Perches			if ($has_flow_statement && !$has_arg_concat) {
435008a2843eSJoe Perches				my $herectx = $here . "\n";
435108a2843eSJoe Perches				my $cnt = statement_rawlines($ctx);
435208a2843eSJoe Perches
435308a2843eSJoe Perches				for (my $n = 0; $n < $cnt; $n++) {
435408a2843eSJoe Perches					$herectx .= raw_line($linenr, $n) . "\n";
435508a2843eSJoe Perches				}
435608a2843eSJoe Perches				WARN("MACRO_WITH_FLOW_CONTROL",
435708a2843eSJoe Perches				     "Macros with flow control statements should be avoided\n" . "$herectx");
435808a2843eSJoe Perches			}
435908a2843eSJoe Perches
4360481eb486SJoe Perches# check for line continuations outside of #defines, preprocessor #, and asm
43615023d347SJoe Perches
43625023d347SJoe Perches		} else {
43635023d347SJoe Perches			if ($prevline !~ /^..*\\$/ &&
4364481eb486SJoe Perches			    $line !~ /^\+\s*\#.*\\$/ &&		# preprocessor
4365481eb486SJoe Perches			    $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ &&	# asm
43665023d347SJoe Perches			    $line =~ /^\+.*\\$/) {
43675023d347SJoe Perches				WARN("LINE_CONTINUATIONS",
43685023d347SJoe Perches				     "Avoid unnecessary line continuations\n" . $herecurr);
43695023d347SJoe Perches			}
4370653d4876SAndy Whitcroft		}
43710a920b5bSAndy Whitcroft
4372b13edf7fSJoe Perches# do {} while (0) macro tests:
4373b13edf7fSJoe Perches# single-statement macros do not need to be enclosed in do while (0) loop,
4374b13edf7fSJoe Perches# macro should not end with a semicolon
4375b13edf7fSJoe Perches		if ($^V && $^V ge 5.10.0 &&
4376b13edf7fSJoe Perches		    $realfile !~ m@/vmlinux.lds.h$@ &&
4377b13edf7fSJoe Perches		    $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
4378b13edf7fSJoe Perches			my $ln = $linenr;
4379b13edf7fSJoe Perches			my $cnt = $realcnt;
4380b13edf7fSJoe Perches			my ($off, $dstat, $dcond, $rest);
4381b13edf7fSJoe Perches			my $ctx = '';
4382b13edf7fSJoe Perches			($dstat, $dcond, $ln, $cnt, $off) =
4383b13edf7fSJoe Perches				ctx_statement_block($linenr, $realcnt, 0);
4384b13edf7fSJoe Perches			$ctx = $dstat;
4385b13edf7fSJoe Perches
4386b13edf7fSJoe Perches			$dstat =~ s/\\\n.//g;
43871b36b201SJoe Perches			$dstat =~ s/$;/ /g;
4388b13edf7fSJoe Perches
4389b13edf7fSJoe Perches			if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
4390b13edf7fSJoe Perches				my $stmts = $2;
4391b13edf7fSJoe Perches				my $semis = $3;
4392b13edf7fSJoe Perches
4393b13edf7fSJoe Perches				$ctx =~ s/\n*$//;
4394b13edf7fSJoe Perches				my $cnt = statement_rawlines($ctx);
4395b13edf7fSJoe Perches				my $herectx = $here . "\n";
4396b13edf7fSJoe Perches
4397b13edf7fSJoe Perches				for (my $n = 0; $n < $cnt; $n++) {
4398b13edf7fSJoe Perches					$herectx .= raw_line($linenr, $n) . "\n";
4399b13edf7fSJoe Perches				}
4400b13edf7fSJoe Perches
4401ac8e97f8SJoe Perches				if (($stmts =~ tr/;/;/) == 1 &&
4402ac8e97f8SJoe Perches				    $stmts !~ /^\s*(if|while|for|switch)\b/) {
4403b13edf7fSJoe Perches					WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
4404b13edf7fSJoe Perches					     "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
4405b13edf7fSJoe Perches				}
4406b13edf7fSJoe Perches				if (defined $semis && $semis ne "") {
4407b13edf7fSJoe Perches					WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
4408b13edf7fSJoe Perches					     "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
4409b13edf7fSJoe Perches				}
4410f5ef95b1SJoe Perches			} elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) {
4411f5ef95b1SJoe Perches				$ctx =~ s/\n*$//;
4412f5ef95b1SJoe Perches				my $cnt = statement_rawlines($ctx);
4413f5ef95b1SJoe Perches				my $herectx = $here . "\n";
4414f5ef95b1SJoe Perches
4415f5ef95b1SJoe Perches				for (my $n = 0; $n < $cnt; $n++) {
4416f5ef95b1SJoe Perches					$herectx .= raw_line($linenr, $n) . "\n";
4417f5ef95b1SJoe Perches				}
4418f5ef95b1SJoe Perches
4419f5ef95b1SJoe Perches				WARN("TRAILING_SEMICOLON",
4420f5ef95b1SJoe Perches				     "macros should not use a trailing semicolon\n" . "$herectx");
4421b13edf7fSJoe Perches			}
4422b13edf7fSJoe Perches		}
4423b13edf7fSJoe Perches
4424080ba929SMike Frysinger# make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
4425080ba929SMike Frysinger# all assignments may have only one of the following with an assignment:
4426080ba929SMike Frysinger#	.
4427080ba929SMike Frysinger#	ALIGN(...)
4428080ba929SMike Frysinger#	VMLINUX_SYMBOL(...)
4429080ba929SMike Frysinger		if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
4430000d1cc1SJoe Perches			WARN("MISSING_VMLINUX_SYMBOL",
4431000d1cc1SJoe Perches			     "vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
4432080ba929SMike Frysinger		}
4433080ba929SMike Frysinger
4434f0a594c1SAndy Whitcroft# check for redundant bracing round if etc
443513214adfSAndy Whitcroft		if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
443613214adfSAndy Whitcroft			my ($level, $endln, @chunks) =
4437cf655043SAndy Whitcroft				ctx_statement_full($linenr, $realcnt, 1);
443813214adfSAndy Whitcroft			#print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
4439cf655043SAndy Whitcroft			#print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
4440cf655043SAndy Whitcroft			if ($#chunks > 0 && $level == 0) {
4441aad4f614SJoe Perches				my @allowed = ();
4442aad4f614SJoe Perches				my $allow = 0;
444313214adfSAndy Whitcroft				my $seen = 0;
4444773647a0SAndy Whitcroft				my $herectx = $here . "\n";
4445cf655043SAndy Whitcroft				my $ln = $linenr - 1;
444613214adfSAndy Whitcroft				for my $chunk (@chunks) {
444713214adfSAndy Whitcroft					my ($cond, $block) = @{$chunk};
444813214adfSAndy Whitcroft
4449773647a0SAndy Whitcroft					# If the condition carries leading newlines, then count those as offsets.
4450773647a0SAndy Whitcroft					my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
4451773647a0SAndy Whitcroft					my $offset = statement_rawlines($whitespace) - 1;
4452773647a0SAndy Whitcroft
4453aad4f614SJoe Perches					$allowed[$allow] = 0;
4454773647a0SAndy Whitcroft					#print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
4455773647a0SAndy Whitcroft
4456773647a0SAndy Whitcroft					# We have looked at and allowed this specific line.
4457773647a0SAndy Whitcroft					$suppress_ifbraces{$ln + $offset} = 1;
4458773647a0SAndy Whitcroft
4459773647a0SAndy Whitcroft					$herectx .= "$rawlines[$ln + $offset]\n[...]\n";
4460cf655043SAndy Whitcroft					$ln += statement_rawlines($block) - 1;
4461cf655043SAndy Whitcroft
4462773647a0SAndy Whitcroft					substr($block, 0, length($cond), '');
446313214adfSAndy Whitcroft
446413214adfSAndy Whitcroft					$seen++ if ($block =~ /^\s*{/);
446513214adfSAndy Whitcroft
4466aad4f614SJoe Perches					#print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
4467cf655043SAndy Whitcroft					if (statement_lines($cond) > 1) {
4468cf655043SAndy Whitcroft						#print "APW: ALLOWED: cond<$cond>\n";
4469aad4f614SJoe Perches						$allowed[$allow] = 1;
447013214adfSAndy Whitcroft					}
447113214adfSAndy Whitcroft					if ($block =~/\b(?:if|for|while)\b/) {
4472cf655043SAndy Whitcroft						#print "APW: ALLOWED: block<$block>\n";
4473aad4f614SJoe Perches						$allowed[$allow] = 1;
447413214adfSAndy Whitcroft					}
4475cf655043SAndy Whitcroft					if (statement_block_size($block) > 1) {
4476cf655043SAndy Whitcroft						#print "APW: ALLOWED: lines block<$block>\n";
4477aad4f614SJoe Perches						$allowed[$allow] = 1;
447813214adfSAndy Whitcroft					}
4479aad4f614SJoe Perches					$allow++;
448013214adfSAndy Whitcroft				}
4481aad4f614SJoe Perches				if ($seen) {
4482aad4f614SJoe Perches					my $sum_allowed = 0;
4483aad4f614SJoe Perches					foreach (@allowed) {
4484aad4f614SJoe Perches						$sum_allowed += $_;
4485aad4f614SJoe Perches					}
4486aad4f614SJoe Perches					if ($sum_allowed == 0) {
4487000d1cc1SJoe Perches						WARN("BRACES",
4488000d1cc1SJoe Perches						     "braces {} are not necessary for any arm of this statement\n" . $herectx);
4489aad4f614SJoe Perches					} elsif ($sum_allowed != $allow &&
4490aad4f614SJoe Perches						 $seen != $allow) {
4491aad4f614SJoe Perches						CHK("BRACES",
4492aad4f614SJoe Perches						    "braces {} should be used on all arms of this statement\n" . $herectx);
4493aad4f614SJoe Perches					}
449413214adfSAndy Whitcroft				}
449513214adfSAndy Whitcroft			}
449613214adfSAndy Whitcroft		}
4497773647a0SAndy Whitcroft		if (!defined $suppress_ifbraces{$linenr - 1} &&
449813214adfSAndy Whitcroft					$line =~ /\b(if|while|for|else)\b/) {
4499cf655043SAndy Whitcroft			my $allowed = 0;
4500f0a594c1SAndy Whitcroft
4501cf655043SAndy Whitcroft			# Check the pre-context.
4502cf655043SAndy Whitcroft			if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
4503cf655043SAndy Whitcroft				#print "APW: ALLOWED: pre<$1>\n";
4504cf655043SAndy Whitcroft				$allowed = 1;
4505f0a594c1SAndy Whitcroft			}
4506773647a0SAndy Whitcroft
4507773647a0SAndy Whitcroft			my ($level, $endln, @chunks) =
4508773647a0SAndy Whitcroft				ctx_statement_full($linenr, $realcnt, $-[0]);
4509773647a0SAndy Whitcroft
4510cf655043SAndy Whitcroft			# Check the condition.
4511cf655043SAndy Whitcroft			my ($cond, $block) = @{$chunks[0]};
4512773647a0SAndy Whitcroft			#print "CHECKING<$linenr> cond<$cond> block<$block>\n";
4513cf655043SAndy Whitcroft			if (defined $cond) {
4514773647a0SAndy Whitcroft				substr($block, 0, length($cond), '');
4515cf655043SAndy Whitcroft			}
4516cf655043SAndy Whitcroft			if (statement_lines($cond) > 1) {
4517cf655043SAndy Whitcroft				#print "APW: ALLOWED: cond<$cond>\n";
4518cf655043SAndy Whitcroft				$allowed = 1;
4519cf655043SAndy Whitcroft			}
4520cf655043SAndy Whitcroft			if ($block =~/\b(?:if|for|while)\b/) {
4521cf655043SAndy Whitcroft				#print "APW: ALLOWED: block<$block>\n";
4522cf655043SAndy Whitcroft				$allowed = 1;
4523cf655043SAndy Whitcroft			}
4524cf655043SAndy Whitcroft			if (statement_block_size($block) > 1) {
4525cf655043SAndy Whitcroft				#print "APW: ALLOWED: lines block<$block>\n";
4526cf655043SAndy Whitcroft				$allowed = 1;
4527cf655043SAndy Whitcroft			}
4528cf655043SAndy Whitcroft			# Check the post-context.
4529cf655043SAndy Whitcroft			if (defined $chunks[1]) {
4530cf655043SAndy Whitcroft				my ($cond, $block) = @{$chunks[1]};
4531cf655043SAndy Whitcroft				if (defined $cond) {
4532773647a0SAndy Whitcroft					substr($block, 0, length($cond), '');
4533cf655043SAndy Whitcroft				}
4534cf655043SAndy Whitcroft				if ($block =~ /^\s*\{/) {
4535cf655043SAndy Whitcroft					#print "APW: ALLOWED: chunk-1 block<$block>\n";
4536cf655043SAndy Whitcroft					$allowed = 1;
4537cf655043SAndy Whitcroft				}
4538cf655043SAndy Whitcroft			}
4539cf655043SAndy Whitcroft			if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
454069932487SJustin P. Mattock				my $herectx = $here . "\n";
4541f055663cSAndy Whitcroft				my $cnt = statement_rawlines($block);
4542cf655043SAndy Whitcroft
4543f055663cSAndy Whitcroft				for (my $n = 0; $n < $cnt; $n++) {
454469932487SJustin P. Mattock					$herectx .= raw_line($linenr, $n) . "\n";
4545cf655043SAndy Whitcroft				}
4546cf655043SAndy Whitcroft
4547000d1cc1SJoe Perches				WARN("BRACES",
4548000d1cc1SJoe Perches				     "braces {} are not necessary for single statement blocks\n" . $herectx);
4549f0a594c1SAndy Whitcroft			}
4550f0a594c1SAndy Whitcroft		}
4551f0a594c1SAndy Whitcroft
45520979ae66SJoe Perches# check for unnecessary blank lines around braces
455377b9a53aSJoe Perches		if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
4554f8e58219SJoe Perches			if (CHK("BRACES",
4555f8e58219SJoe Perches				"Blank lines aren't necessary before a close brace '}'\n" . $hereprev) &&
4556f8e58219SJoe Perches			    $fix && $prevrawline =~ /^\+/) {
4557f8e58219SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
4558f8e58219SJoe Perches			}
45590979ae66SJoe Perches		}
456077b9a53aSJoe Perches		if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
4561f8e58219SJoe Perches			if (CHK("BRACES",
4562f8e58219SJoe Perches				"Blank lines aren't necessary after an open brace '{'\n" . $hereprev) &&
4563f8e58219SJoe Perches			    $fix) {
4564f8e58219SJoe Perches				fix_delete_line($fixlinenr, $rawline);
4565f8e58219SJoe Perches			}
45660979ae66SJoe Perches		}
45670979ae66SJoe Perches
45684a0df2efSAndy Whitcroft# no volatiles please
45696c72ffaaSAndy Whitcroft		my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
45706c72ffaaSAndy Whitcroft		if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
4571000d1cc1SJoe Perches			WARN("VOLATILE",
4572000d1cc1SJoe Perches			     "Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
45734a0df2efSAndy Whitcroft		}
45744a0df2efSAndy Whitcroft
45755e4f6ba5SJoe Perches# Check for user-visible strings broken across lines, which breaks the ability
45765e4f6ba5SJoe Perches# to grep for the string.  Make exceptions when the previous string ends in a
45775e4f6ba5SJoe Perches# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{'
45785e4f6ba5SJoe Perches# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value
45795e4f6ba5SJoe Perches		if ($line =~ /^\+\s*"[X\t]*"/ &&
45805e4f6ba5SJoe Perches		    $prevline =~ /"\s*$/ &&
45815e4f6ba5SJoe Perches		    $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) {
45825e4f6ba5SJoe Perches			if (WARN("SPLIT_STRING",
45835e4f6ba5SJoe Perches				 "quoted string split across lines\n" . $hereprev) &&
45845e4f6ba5SJoe Perches				     $fix &&
45855e4f6ba5SJoe Perches				     $prevrawline =~ /^\+.*"\s*$/ &&
45865e4f6ba5SJoe Perches				     $last_coalesced_string_linenr != $linenr - 1) {
45875e4f6ba5SJoe Perches				my $extracted_string = get_quoted_string($line, $rawline);
45885e4f6ba5SJoe Perches				my $comma_close = "";
45895e4f6ba5SJoe Perches				if ($rawline =~ /\Q$extracted_string\E(\s*\)\s*;\s*$|\s*,\s*)/) {
45905e4f6ba5SJoe Perches					$comma_close = $1;
45915e4f6ba5SJoe Perches				}
45925e4f6ba5SJoe Perches
45935e4f6ba5SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
45945e4f6ba5SJoe Perches				fix_delete_line($fixlinenr, $rawline);
45955e4f6ba5SJoe Perches				my $fixedline = $prevrawline;
45965e4f6ba5SJoe Perches				$fixedline =~ s/"\s*$//;
45975e4f6ba5SJoe Perches				$fixedline .= substr($extracted_string, 1) . trim($comma_close);
45985e4f6ba5SJoe Perches				fix_insert_line($fixlinenr - 1, $fixedline);
45995e4f6ba5SJoe Perches				$fixedline = $rawline;
46005e4f6ba5SJoe Perches				$fixedline =~ s/\Q$extracted_string\E\Q$comma_close\E//;
46015e4f6ba5SJoe Perches				if ($fixedline !~ /\+\s*$/) {
46025e4f6ba5SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
46035e4f6ba5SJoe Perches				}
46045e4f6ba5SJoe Perches				$last_coalesced_string_linenr = $linenr;
46055e4f6ba5SJoe Perches			}
46065e4f6ba5SJoe Perches		}
46075e4f6ba5SJoe Perches
46085e4f6ba5SJoe Perches# check for missing a space in a string concatenation
46095e4f6ba5SJoe Perches		if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) {
46105e4f6ba5SJoe Perches			WARN('MISSING_SPACE',
46115e4f6ba5SJoe Perches			     "break quoted strings at a space character\n" . $hereprev);
46125e4f6ba5SJoe Perches		}
46135e4f6ba5SJoe Perches
46145e4f6ba5SJoe Perches# check for spaces before a quoted newline
46155e4f6ba5SJoe Perches		if ($rawline =~ /^.*\".*\s\\n/) {
46165e4f6ba5SJoe Perches			if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
46175e4f6ba5SJoe Perches				 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
46185e4f6ba5SJoe Perches			    $fix) {
46195e4f6ba5SJoe Perches				$fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
46205e4f6ba5SJoe Perches			}
46215e4f6ba5SJoe Perches
46225e4f6ba5SJoe Perches		}
46235e4f6ba5SJoe Perches
4624f17dba4fSJoe Perches# concatenated string without spaces between elements
4625f17dba4fSJoe Perches		if ($line =~ /"X+"[A-Z_]+/ || $line =~ /[A-Z_]+"X+"/) {
4626f17dba4fSJoe Perches			CHK("CONCATENATED_STRING",
4627f17dba4fSJoe Perches			    "Concatenated strings should use spaces between elements\n" . $herecurr);
4628f17dba4fSJoe Perches		}
4629f17dba4fSJoe Perches
463090ad30e5SJoe Perches# uncoalesced string fragments
463190ad30e5SJoe Perches		if ($line =~ /"X*"\s*"/) {
463290ad30e5SJoe Perches			WARN("STRING_FRAGMENTS",
463390ad30e5SJoe Perches			     "Consecutive strings are generally better as a single string\n" . $herecurr);
463490ad30e5SJoe Perches		}
463590ad30e5SJoe Perches
46365e4f6ba5SJoe Perches# check for %L{u,d,i} in strings
46375e4f6ba5SJoe Perches		my $string;
46385e4f6ba5SJoe Perches		while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
46395e4f6ba5SJoe Perches			$string = substr($rawline, $-[1], $+[1] - $-[1]);
46405e4f6ba5SJoe Perches			$string =~ s/%%/__/g;
46415e4f6ba5SJoe Perches			if ($string =~ /(?<!%)%L[udi]/) {
46425e4f6ba5SJoe Perches				WARN("PRINTF_L",
46435e4f6ba5SJoe Perches				     "\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
46445e4f6ba5SJoe Perches				last;
46455e4f6ba5SJoe Perches			}
46465e4f6ba5SJoe Perches		}
46475e4f6ba5SJoe Perches
46485e4f6ba5SJoe Perches# check for line continuations in quoted strings with odd counts of "
46495e4f6ba5SJoe Perches		if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) {
46505e4f6ba5SJoe Perches			WARN("LINE_CONTINUATIONS",
46515e4f6ba5SJoe Perches			     "Avoid line continuations in quoted strings\n" . $herecurr);
46525e4f6ba5SJoe Perches		}
46535e4f6ba5SJoe Perches
465400df344fSAndy Whitcroft# warn about #if 0
4655c45dcabdSAndy Whitcroft		if ($line =~ /^.\s*\#\s*if\s+0\b/) {
4656000d1cc1SJoe Perches			CHK("REDUNDANT_CODE",
4657000d1cc1SJoe Perches			    "if this code is redundant consider removing it\n" .
4658de7d4f0eSAndy Whitcroft				$herecurr);
46594a0df2efSAndy Whitcroft		}
46604a0df2efSAndy Whitcroft
466103df4b51SAndy Whitcroft# check for needless "if (<foo>) fn(<foo>)" uses
466203df4b51SAndy Whitcroft		if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
466303df4b51SAndy Whitcroft			my $expr = '\s*\(\s*' . quotemeta($1) . '\s*\)\s*;';
466403df4b51SAndy Whitcroft			if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?)$expr/) {
466503df4b51SAndy Whitcroft				WARN('NEEDLESS_IF',
466615160f90SFabio Estevam				     "$1(NULL) is safe and this check is probably not required\n" . $hereprev);
46674c432a8fSGreg Kroah-Hartman			}
46684c432a8fSGreg Kroah-Hartman		}
4669f0a594c1SAndy Whitcroft
4670ebfdc409SJoe Perches# check for unnecessary "Out of Memory" messages
4671ebfdc409SJoe Perches		if ($line =~ /^\+.*\b$logFunctions\s*\(/ &&
4672ebfdc409SJoe Perches		    $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ &&
4673ebfdc409SJoe Perches		    (defined $1 || defined $3) &&
4674ebfdc409SJoe Perches		    $linenr > 3) {
4675ebfdc409SJoe Perches			my $testval = $2;
4676ebfdc409SJoe Perches			my $testline = $lines[$linenr - 3];
4677ebfdc409SJoe Perches
4678ebfdc409SJoe Perches			my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0);
4679ebfdc409SJoe Perches#			print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n");
4680ebfdc409SJoe Perches
4681ebfdc409SJoe 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)/) {
4682ebfdc409SJoe Perches				WARN("OOM_MESSAGE",
4683ebfdc409SJoe Perches				     "Possible unnecessary 'out of memory' message\n" . $hereprev);
4684ebfdc409SJoe Perches			}
4685ebfdc409SJoe Perches		}
4686ebfdc409SJoe Perches
4687f78d98f6SJoe Perches# check for logging functions with KERN_<LEVEL>
4688dcaf1123SPaolo Bonzini		if ($line !~ /printk(?:_ratelimited|_once)?\s*\(/ &&
4689f78d98f6SJoe Perches		    $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) {
4690f78d98f6SJoe Perches			my $level = $1;
4691f78d98f6SJoe Perches			if (WARN("UNNECESSARY_KERN_LEVEL",
4692f78d98f6SJoe Perches				 "Possible unnecessary $level\n" . $herecurr) &&
4693f78d98f6SJoe Perches			    $fix) {
4694f78d98f6SJoe Perches				$fixed[$fixlinenr] =~ s/\s*$level\s*//;
4695f78d98f6SJoe Perches			}
4696f78d98f6SJoe Perches		}
4697f78d98f6SJoe Perches
4698abb08a53SJoe Perches# check for mask then right shift without a parentheses
4699abb08a53SJoe Perches		if ($^V && $^V ge 5.10.0 &&
4700abb08a53SJoe Perches		    $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ &&
4701abb08a53SJoe Perches		    $4 !~ /^\&/) { # $LvalOrFunc may be &foo, ignore if so
4702abb08a53SJoe Perches			WARN("MASK_THEN_SHIFT",
4703abb08a53SJoe Perches			     "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr);
4704abb08a53SJoe Perches		}
4705abb08a53SJoe Perches
4706b75ac618SJoe Perches# check for pointer comparisons to NULL
4707b75ac618SJoe Perches		if ($^V && $^V ge 5.10.0) {
4708b75ac618SJoe Perches			while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) {
4709b75ac618SJoe Perches				my $val = $1;
4710b75ac618SJoe Perches				my $equal = "!";
4711b75ac618SJoe Perches				$equal = "" if ($4 eq "!=");
4712b75ac618SJoe Perches				if (CHK("COMPARISON_TO_NULL",
4713b75ac618SJoe Perches					"Comparison to NULL could be written \"${equal}${val}\"\n" . $herecurr) &&
4714b75ac618SJoe Perches					    $fix) {
4715b75ac618SJoe Perches					$fixed[$fixlinenr] =~ s/\b\Q$val\E\s*(?:==|\!=)\s*NULL\b/$equal$val/;
4716b75ac618SJoe Perches				}
4717b75ac618SJoe Perches			}
4718b75ac618SJoe Perches		}
4719b75ac618SJoe Perches
47208716de38SJoe Perches# check for bad placement of section $InitAttribute (e.g.: __initdata)
47218716de38SJoe Perches		if ($line =~ /(\b$InitAttribute\b)/) {
47228716de38SJoe Perches			my $attr = $1;
47238716de38SJoe Perches			if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
47248716de38SJoe Perches				my $ptr = $1;
47258716de38SJoe Perches				my $var = $2;
47268716de38SJoe Perches				if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
47278716de38SJoe Perches				      ERROR("MISPLACED_INIT",
47288716de38SJoe Perches					    "$attr should be placed after $var\n" . $herecurr)) ||
47298716de38SJoe Perches				     ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
47308716de38SJoe Perches				      WARN("MISPLACED_INIT",
47318716de38SJoe Perches					   "$attr should be placed after $var\n" . $herecurr))) &&
47328716de38SJoe Perches				    $fix) {
4733194f66fcSJoe 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;
47348716de38SJoe Perches				}
47358716de38SJoe Perches			}
47368716de38SJoe Perches		}
47378716de38SJoe Perches
4738e970b884SJoe Perches# check for $InitAttributeData (ie: __initdata) with const
4739e970b884SJoe Perches		if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
4740e970b884SJoe Perches			my $attr = $1;
4741e970b884SJoe Perches			$attr =~ /($InitAttributePrefix)(.*)/;
4742e970b884SJoe Perches			my $attr_prefix = $1;
4743e970b884SJoe Perches			my $attr_type = $2;
4744e970b884SJoe Perches			if (ERROR("INIT_ATTRIBUTE",
4745e970b884SJoe Perches				  "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
4746e970b884SJoe Perches			    $fix) {
4747194f66fcSJoe Perches				$fixed[$fixlinenr] =~
4748e970b884SJoe Perches				    s/$InitAttributeData/${attr_prefix}initconst/;
4749e970b884SJoe Perches			}
4750e970b884SJoe Perches		}
4751e970b884SJoe Perches
4752e970b884SJoe Perches# check for $InitAttributeConst (ie: __initconst) without const
4753e970b884SJoe Perches		if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
4754e970b884SJoe Perches			my $attr = $1;
4755e970b884SJoe Perches			if (ERROR("INIT_ATTRIBUTE",
4756e970b884SJoe Perches				  "Use of $attr requires a separate use of const\n" . $herecurr) &&
4757e970b884SJoe Perches			    $fix) {
4758194f66fcSJoe Perches				my $lead = $fixed[$fixlinenr] =~
4759e970b884SJoe Perches				    /(^\+\s*(?:static\s+))/;
4760e970b884SJoe Perches				$lead = rtrim($1);
4761e970b884SJoe Perches				$lead = "$lead " if ($lead !~ /^\+$/);
4762e970b884SJoe Perches				$lead = "${lead}const ";
4763194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/;
4764e970b884SJoe Perches			}
4765e970b884SJoe Perches		}
4766e970b884SJoe Perches
4767fbdb8138SJoe Perches# don't use __constant_<foo> functions outside of include/uapi/
4768fbdb8138SJoe Perches		if ($realfile !~ m@^include/uapi/@ &&
4769fbdb8138SJoe Perches		    $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) {
4770fbdb8138SJoe Perches			my $constant_func = $1;
4771fbdb8138SJoe Perches			my $func = $constant_func;
4772fbdb8138SJoe Perches			$func =~ s/^__constant_//;
4773fbdb8138SJoe Perches			if (WARN("CONSTANT_CONVERSION",
4774fbdb8138SJoe Perches				 "$constant_func should be $func\n" . $herecurr) &&
4775fbdb8138SJoe Perches			    $fix) {
4776194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g;
4777fbdb8138SJoe Perches			}
4778fbdb8138SJoe Perches		}
4779fbdb8138SJoe Perches
47801a15a250SPatrick Pannuto# prefer usleep_range over udelay
478137581c28SBruce Allan		if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
478243c1d77cSJoe Perches			my $delay = $1;
47831a15a250SPatrick Pannuto			# ignore udelay's < 10, however
478443c1d77cSJoe Perches			if (! ($delay < 10) ) {
4785000d1cc1SJoe Perches				CHK("USLEEP_RANGE",
478643c1d77cSJoe Perches				    "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $herecurr);
478743c1d77cSJoe Perches			}
478843c1d77cSJoe Perches			if ($delay > 2000) {
478943c1d77cSJoe Perches				WARN("LONG_UDELAY",
479043c1d77cSJoe Perches				     "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr);
47911a15a250SPatrick Pannuto			}
47921a15a250SPatrick Pannuto		}
47931a15a250SPatrick Pannuto
479409ef8725SPatrick Pannuto# warn about unexpectedly long msleep's
479509ef8725SPatrick Pannuto		if ($line =~ /\bmsleep\s*\((\d+)\);/) {
479609ef8725SPatrick Pannuto			if ($1 < 20) {
4797000d1cc1SJoe Perches				WARN("MSLEEP",
479843c1d77cSJoe Perches				     "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $herecurr);
479909ef8725SPatrick Pannuto			}
480009ef8725SPatrick Pannuto		}
480109ef8725SPatrick Pannuto
480236ec1939SJoe Perches# check for comparisons of jiffies
480336ec1939SJoe Perches		if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
480436ec1939SJoe Perches			WARN("JIFFIES_COMPARISON",
480536ec1939SJoe Perches			     "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
480636ec1939SJoe Perches		}
480736ec1939SJoe Perches
48089d7a34a5SJoe Perches# check for comparisons of get_jiffies_64()
48099d7a34a5SJoe Perches		if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
48109d7a34a5SJoe Perches			WARN("JIFFIES_COMPARISON",
48119d7a34a5SJoe Perches			     "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
48129d7a34a5SJoe Perches		}
48139d7a34a5SJoe Perches
481400df344fSAndy Whitcroft# warn about #ifdefs in C files
4815c45dcabdSAndy Whitcroft#		if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
481600df344fSAndy Whitcroft#			print "#ifdef in C files should be avoided\n";
481700df344fSAndy Whitcroft#			print "$herecurr";
481800df344fSAndy Whitcroft#			$clean = 0;
481900df344fSAndy Whitcroft#		}
482000df344fSAndy Whitcroft
482122f2a2efSAndy Whitcroft# warn about spacing in #ifdefs
4822c45dcabdSAndy Whitcroft		if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
48233705ce5bSJoe Perches			if (ERROR("SPACING",
48243705ce5bSJoe Perches				  "exactly one space required after that #$1\n" . $herecurr) &&
48253705ce5bSJoe Perches			    $fix) {
4826194f66fcSJoe Perches				$fixed[$fixlinenr] =~
48273705ce5bSJoe Perches				    s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
48283705ce5bSJoe Perches			}
48293705ce5bSJoe Perches
483022f2a2efSAndy Whitcroft		}
483122f2a2efSAndy Whitcroft
48324a0df2efSAndy Whitcroft# check for spinlock_t definitions without a comment.
4833171ae1a4SAndy Whitcroft		if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
4834171ae1a4SAndy Whitcroft		    $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
48354a0df2efSAndy Whitcroft			my $which = $1;
48364a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
4837000d1cc1SJoe Perches				CHK("UNCOMMENTED_DEFINITION",
4838000d1cc1SJoe Perches				    "$1 definition without comment\n" . $herecurr);
48394a0df2efSAndy Whitcroft			}
48404a0df2efSAndy Whitcroft		}
48414a0df2efSAndy Whitcroft# check for memory barriers without a comment.
48424a0df2efSAndy Whitcroft		if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
48434a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
4844c1fd7bb9SJoe Perches				WARN("MEMORY_BARRIER",
4845000d1cc1SJoe Perches				     "memory barrier without comment\n" . $herecurr);
48464a0df2efSAndy Whitcroft			}
48474a0df2efSAndy Whitcroft		}
48484a0df2efSAndy Whitcroft# check of hardware specific defines
4849c45dcabdSAndy Whitcroft		if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
4850000d1cc1SJoe Perches			CHK("ARCH_DEFINES",
4851000d1cc1SJoe Perches			    "architecture specific defines should be avoided\n" .  $herecurr);
48520a920b5bSAndy Whitcroft		}
4853653d4876SAndy Whitcroft
4854d4977c78STobias Klauser# Check that the storage class is at the beginning of a declaration
4855d4977c78STobias Klauser		if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
4856000d1cc1SJoe Perches			WARN("STORAGE_CLASS",
4857000d1cc1SJoe Perches			     "storage class should be at the beginning of the declaration\n" . $herecurr)
4858d4977c78STobias Klauser		}
4859d4977c78STobias Klauser
4860de7d4f0eSAndy Whitcroft# check the location of the inline attribute, that it is between
4861de7d4f0eSAndy Whitcroft# storage class and type.
48629c0ca6f9SAndy Whitcroft		if ($line =~ /\b$Type\s+$Inline\b/ ||
48639c0ca6f9SAndy Whitcroft		    $line =~ /\b$Inline\s+$Storage\b/) {
4864000d1cc1SJoe Perches			ERROR("INLINE_LOCATION",
4865000d1cc1SJoe Perches			      "inline keyword should sit between storage class and type\n" . $herecurr);
4866de7d4f0eSAndy Whitcroft		}
4867de7d4f0eSAndy Whitcroft
48688905a67cSAndy Whitcroft# Check for __inline__ and __inline, prefer inline
48692b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
48702b7ab453SJoe Perches		    $line =~ /\b(__inline__|__inline)\b/) {
4871d5e616fcSJoe Perches			if (WARN("INLINE",
4872d5e616fcSJoe Perches				 "plain inline is preferred over $1\n" . $herecurr) &&
4873d5e616fcSJoe Perches			    $fix) {
4874194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/;
4875d5e616fcSJoe Perches
4876d5e616fcSJoe Perches			}
48778905a67cSAndy Whitcroft		}
48788905a67cSAndy Whitcroft
48793d130fd0SJoe Perches# Check for __attribute__ packed, prefer __packed
48802b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
48812b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
4882000d1cc1SJoe Perches			WARN("PREFER_PACKED",
4883000d1cc1SJoe Perches			     "__packed is preferred over __attribute__((packed))\n" . $herecurr);
48843d130fd0SJoe Perches		}
48853d130fd0SJoe Perches
488639b7e287SJoe Perches# Check for __attribute__ aligned, prefer __aligned
48872b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
48882b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
4889000d1cc1SJoe Perches			WARN("PREFER_ALIGNED",
4890000d1cc1SJoe Perches			     "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
489139b7e287SJoe Perches		}
489239b7e287SJoe Perches
48935f14d3bdSJoe Perches# Check for __attribute__ format(printf, prefer __printf
48942b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
48952b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
4896d5e616fcSJoe Perches			if (WARN("PREFER_PRINTF",
4897d5e616fcSJoe Perches				 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) &&
4898d5e616fcSJoe Perches			    $fix) {
4899194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf\s*,\s*(.*)\)\s*\)\s*\)/"__printf(" . trim($1) . ")"/ex;
4900d5e616fcSJoe Perches
4901d5e616fcSJoe Perches			}
49025f14d3bdSJoe Perches		}
49035f14d3bdSJoe Perches
49046061d949SJoe Perches# Check for __attribute__ format(scanf, prefer __scanf
49052b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
49062b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
4907d5e616fcSJoe Perches			if (WARN("PREFER_SCANF",
4908d5e616fcSJoe Perches				 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) &&
4909d5e616fcSJoe Perches			    $fix) {
4910194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\s*,\s*(.*)\)\s*\)\s*\)/"__scanf(" . trim($1) . ")"/ex;
4911d5e616fcSJoe Perches			}
49126061d949SJoe Perches		}
49136061d949SJoe Perches
4914619a908aSJoe Perches# Check for __attribute__ weak, or __weak declarations (may have link issues)
4915619a908aSJoe Perches		if ($^V && $^V ge 5.10.0 &&
4916619a908aSJoe Perches		    $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ &&
4917619a908aSJoe Perches		    ($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ ||
4918619a908aSJoe Perches		     $line =~ /\b__weak\b/)) {
4919619a908aSJoe Perches			ERROR("WEAK_DECLARATION",
4920619a908aSJoe Perches			      "Using weak declarations can have unintended link defects\n" . $herecurr);
4921619a908aSJoe Perches		}
4922619a908aSJoe Perches
49238f53a9b8SJoe Perches# check for sizeof(&)
49248f53a9b8SJoe Perches		if ($line =~ /\bsizeof\s*\(\s*\&/) {
4925000d1cc1SJoe Perches			WARN("SIZEOF_ADDRESS",
4926000d1cc1SJoe Perches			     "sizeof(& should be avoided\n" . $herecurr);
49278f53a9b8SJoe Perches		}
49288f53a9b8SJoe Perches
492966c80b60SJoe Perches# check for sizeof without parenthesis
493066c80b60SJoe Perches		if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
4931d5e616fcSJoe Perches			if (WARN("SIZEOF_PARENTHESIS",
4932d5e616fcSJoe Perches				 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
4933d5e616fcSJoe Perches			    $fix) {
4934194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
4935d5e616fcSJoe Perches			}
493666c80b60SJoe Perches		}
493766c80b60SJoe Perches
493888982feaSJoe Perches# check for struct spinlock declarations
493988982feaSJoe Perches		if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
494088982feaSJoe Perches			WARN("USE_SPINLOCK_T",
494188982feaSJoe Perches			     "struct spinlock should be spinlock_t\n" . $herecurr);
494288982feaSJoe Perches		}
494388982feaSJoe Perches
4944a6962d72SJoe Perches# check for seq_printf uses that could be seq_puts
494506668727SJoe Perches		if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
4946a6962d72SJoe Perches			my $fmt = get_quoted_string($line, $rawline);
4947caac1d5fSHeba Aamer			$fmt =~ s/%%//g;
4948caac1d5fSHeba Aamer			if ($fmt !~ /%/) {
4949d5e616fcSJoe Perches				if (WARN("PREFER_SEQ_PUTS",
4950d5e616fcSJoe Perches					 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
4951d5e616fcSJoe Perches				    $fix) {
4952194f66fcSJoe Perches					$fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/;
4953d5e616fcSJoe Perches				}
4954a6962d72SJoe Perches			}
4955a6962d72SJoe Perches		}
4956a6962d72SJoe Perches
4957554e165cSAndy Whitcroft# Check for misused memsets
4958d1fe9c09SJoe Perches		if ($^V && $^V ge 5.10.0 &&
4959d1fe9c09SJoe Perches		    defined $stat &&
4960d7c76ba7SJoe Perches		    $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/s) {
4961554e165cSAndy Whitcroft
4962d7c76ba7SJoe Perches			my $ms_addr = $2;
4963d1fe9c09SJoe Perches			my $ms_val = $7;
4964d1fe9c09SJoe Perches			my $ms_size = $12;
4965d7c76ba7SJoe Perches
4966554e165cSAndy Whitcroft			if ($ms_size =~ /^(0x|)0$/i) {
4967554e165cSAndy Whitcroft				ERROR("MEMSET",
4968d7c76ba7SJoe Perches				      "memset to 0's uses 0 as the 2nd argument, not the 3rd\n" . "$here\n$stat\n");
4969554e165cSAndy Whitcroft			} elsif ($ms_size =~ /^(0x|)1$/i) {
4970554e165cSAndy Whitcroft				WARN("MEMSET",
4971d7c76ba7SJoe Perches				     "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
4972d7c76ba7SJoe Perches			}
4973d7c76ba7SJoe Perches		}
4974d7c76ba7SJoe Perches
497598a9bba5SJoe Perches# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar)
497698a9bba5SJoe Perches		if ($^V && $^V ge 5.10.0 &&
497798a9bba5SJoe Perches		    $line =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/s) {
497898a9bba5SJoe Perches			if (WARN("PREFER_ETHER_ADDR_COPY",
497998a9bba5SJoe Perches				 "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . $herecurr) &&
498098a9bba5SJoe Perches			    $fix) {
4981194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/;
498298a9bba5SJoe Perches			}
498398a9bba5SJoe Perches		}
498498a9bba5SJoe Perches
4985d7c76ba7SJoe Perches# typecasts on min/max could be min_t/max_t
4986d1fe9c09SJoe Perches		if ($^V && $^V ge 5.10.0 &&
4987d1fe9c09SJoe Perches		    defined $stat &&
4988d7c76ba7SJoe Perches		    $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
4989d1fe9c09SJoe Perches			if (defined $2 || defined $7) {
4990d7c76ba7SJoe Perches				my $call = $1;
4991d7c76ba7SJoe Perches				my $cast1 = deparenthesize($2);
4992d7c76ba7SJoe Perches				my $arg1 = $3;
4993d1fe9c09SJoe Perches				my $cast2 = deparenthesize($7);
4994d1fe9c09SJoe Perches				my $arg2 = $8;
4995d7c76ba7SJoe Perches				my $cast;
4996d7c76ba7SJoe Perches
4997d1fe9c09SJoe Perches				if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
4998d7c76ba7SJoe Perches					$cast = "$cast1 or $cast2";
4999d7c76ba7SJoe Perches				} elsif ($cast1 ne "") {
5000d7c76ba7SJoe Perches					$cast = $cast1;
5001d7c76ba7SJoe Perches				} else {
5002d7c76ba7SJoe Perches					$cast = $cast2;
5003d7c76ba7SJoe Perches				}
5004d7c76ba7SJoe Perches				WARN("MINMAX",
5005d7c76ba7SJoe Perches				     "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
5006554e165cSAndy Whitcroft			}
5007554e165cSAndy Whitcroft		}
5008554e165cSAndy Whitcroft
50094a273195SJoe Perches# check usleep_range arguments
50104a273195SJoe Perches		if ($^V && $^V ge 5.10.0 &&
50114a273195SJoe Perches		    defined $stat &&
50124a273195SJoe Perches		    $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
50134a273195SJoe Perches			my $min = $1;
50144a273195SJoe Perches			my $max = $7;
50154a273195SJoe Perches			if ($min eq $max) {
50164a273195SJoe Perches				WARN("USLEEP_RANGE",
50174a273195SJoe Perches				     "usleep_range should not use min == max args; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
50184a273195SJoe Perches			} elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
50194a273195SJoe Perches				 $min > $max) {
50204a273195SJoe Perches				WARN("USLEEP_RANGE",
50214a273195SJoe Perches				     "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
50224a273195SJoe Perches			}
50234a273195SJoe Perches		}
50244a273195SJoe Perches
5025823b794cSJoe Perches# check for naked sscanf
5026823b794cSJoe Perches		if ($^V && $^V ge 5.10.0 &&
5027823b794cSJoe Perches		    defined $stat &&
50286c8bd707SJoe Perches		    $line =~ /\bsscanf\b/ &&
5029823b794cSJoe Perches		    ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
5030823b794cSJoe Perches		     $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ &&
5031823b794cSJoe Perches		     $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) {
5032823b794cSJoe Perches			my $lc = $stat =~ tr@\n@@;
5033823b794cSJoe Perches			$lc = $lc + $linenr;
5034823b794cSJoe Perches			my $stat_real = raw_line($linenr, 0);
5035823b794cSJoe Perches		        for (my $count = $linenr + 1; $count <= $lc; $count++) {
5036823b794cSJoe Perches				$stat_real = $stat_real . "\n" . raw_line($count, 0);
5037823b794cSJoe Perches			}
5038823b794cSJoe Perches			WARN("NAKED_SSCANF",
5039823b794cSJoe Perches			     "unchecked sscanf return value\n" . "$here\n$stat_real\n");
5040823b794cSJoe Perches		}
5041823b794cSJoe Perches
5042afc819abSJoe Perches# check for simple sscanf that should be kstrto<foo>
5043afc819abSJoe Perches		if ($^V && $^V ge 5.10.0 &&
5044afc819abSJoe Perches		    defined $stat &&
5045afc819abSJoe Perches		    $line =~ /\bsscanf\b/) {
5046afc819abSJoe Perches			my $lc = $stat =~ tr@\n@@;
5047afc819abSJoe Perches			$lc = $lc + $linenr;
5048afc819abSJoe Perches			my $stat_real = raw_line($linenr, 0);
5049afc819abSJoe Perches		        for (my $count = $linenr + 1; $count <= $lc; $count++) {
5050afc819abSJoe Perches				$stat_real = $stat_real . "\n" . raw_line($count, 0);
5051afc819abSJoe Perches			}
5052afc819abSJoe Perches			if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) {
5053afc819abSJoe Perches				my $format = $6;
5054afc819abSJoe Perches				my $count = $format =~ tr@%@%@;
5055afc819abSJoe Perches				if ($count == 1 &&
5056afc819abSJoe Perches				    $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) {
5057afc819abSJoe Perches					WARN("SSCANF_TO_KSTRTO",
5058afc819abSJoe Perches					     "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n");
5059afc819abSJoe Perches				}
5060afc819abSJoe Perches			}
5061afc819abSJoe Perches		}
5062afc819abSJoe Perches
506370dc8a48SJoe Perches# check for new externs in .h files.
506470dc8a48SJoe Perches		if ($realfile =~ /\.h$/ &&
506570dc8a48SJoe Perches		    $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
5066d1d85780SJoe Perches			if (CHK("AVOID_EXTERNS",
506770dc8a48SJoe Perches				"extern prototypes should be avoided in .h files\n" . $herecurr) &&
506870dc8a48SJoe Perches			    $fix) {
5069194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
507070dc8a48SJoe Perches			}
507170dc8a48SJoe Perches		}
507270dc8a48SJoe Perches
5073de7d4f0eSAndy Whitcroft# check for new externs in .c files.
5074171ae1a4SAndy Whitcroft		if ($realfile =~ /\.c$/ && defined $stat &&
5075c45dcabdSAndy Whitcroft		    $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
5076171ae1a4SAndy Whitcroft		{
5077c45dcabdSAndy Whitcroft			my $function_name = $1;
5078c45dcabdSAndy Whitcroft			my $paren_space = $2;
5079171ae1a4SAndy Whitcroft
5080171ae1a4SAndy Whitcroft			my $s = $stat;
5081171ae1a4SAndy Whitcroft			if (defined $cond) {
5082171ae1a4SAndy Whitcroft				substr($s, 0, length($cond), '');
5083171ae1a4SAndy Whitcroft			}
5084c45dcabdSAndy Whitcroft			if ($s =~ /^\s*;/ &&
5085c45dcabdSAndy Whitcroft			    $function_name ne 'uninitialized_var')
5086c45dcabdSAndy Whitcroft			{
5087000d1cc1SJoe Perches				WARN("AVOID_EXTERNS",
5088000d1cc1SJoe Perches				     "externs should be avoided in .c files\n" .  $herecurr);
5089de7d4f0eSAndy Whitcroft			}
5090de7d4f0eSAndy Whitcroft
5091171ae1a4SAndy Whitcroft			if ($paren_space =~ /\n/) {
5092000d1cc1SJoe Perches				WARN("FUNCTION_ARGUMENTS",
5093000d1cc1SJoe Perches				     "arguments for function declarations should follow identifier\n" . $herecurr);
5094171ae1a4SAndy Whitcroft			}
50959c9ba34eSAndy Whitcroft
50969c9ba34eSAndy Whitcroft		} elsif ($realfile =~ /\.c$/ && defined $stat &&
50979c9ba34eSAndy Whitcroft		    $stat =~ /^.\s*extern\s+/)
50989c9ba34eSAndy Whitcroft		{
5099000d1cc1SJoe Perches			WARN("AVOID_EXTERNS",
5100000d1cc1SJoe Perches			     "externs should be avoided in .c files\n" .  $herecurr);
5101171ae1a4SAndy Whitcroft		}
5102171ae1a4SAndy Whitcroft
5103de7d4f0eSAndy Whitcroft# checks for new __setup's
5104de7d4f0eSAndy Whitcroft		if ($rawline =~ /\b__setup\("([^"]*)"/) {
5105de7d4f0eSAndy Whitcroft			my $name = $1;
5106de7d4f0eSAndy Whitcroft
5107de7d4f0eSAndy Whitcroft			if (!grep(/$name/, @setup_docs)) {
5108000d1cc1SJoe Perches				CHK("UNDOCUMENTED_SETUP",
5109000d1cc1SJoe Perches				    "__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
5110de7d4f0eSAndy Whitcroft			}
5111653d4876SAndy Whitcroft		}
51129c0ca6f9SAndy Whitcroft
51139c0ca6f9SAndy Whitcroft# check for pointless casting of kmalloc return
5114caf2a54fSJoe Perches		if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) {
5115000d1cc1SJoe Perches			WARN("UNNECESSARY_CASTS",
5116000d1cc1SJoe Perches			     "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
51179c0ca6f9SAndy Whitcroft		}
511813214adfSAndy Whitcroft
5119a640d25cSJoe Perches# alloc style
5120a640d25cSJoe Perches# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
5121a640d25cSJoe Perches		if ($^V && $^V ge 5.10.0 &&
5122a640d25cSJoe Perches		    $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*([kv][mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
5123a640d25cSJoe Perches			CHK("ALLOC_SIZEOF_STRUCT",
5124a640d25cSJoe Perches			    "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
5125a640d25cSJoe Perches		}
5126a640d25cSJoe Perches
512760a55369SJoe Perches# check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc
512860a55369SJoe Perches		if ($^V && $^V ge 5.10.0 &&
5129e367455aSJoe Perches		    $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*(k[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)\s*,/) {
513060a55369SJoe Perches			my $oldfunc = $3;
513160a55369SJoe Perches			my $a1 = $4;
513260a55369SJoe Perches			my $a2 = $10;
513360a55369SJoe Perches			my $newfunc = "kmalloc_array";
513460a55369SJoe Perches			$newfunc = "kcalloc" if ($oldfunc eq "kzalloc");
513560a55369SJoe Perches			my $r1 = $a1;
513660a55369SJoe Perches			my $r2 = $a2;
513760a55369SJoe Perches			if ($a1 =~ /^sizeof\s*\S/) {
513860a55369SJoe Perches				$r1 = $a2;
513960a55369SJoe Perches				$r2 = $a1;
514060a55369SJoe Perches			}
5141e367455aSJoe Perches			if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ &&
5142e367455aSJoe Perches			    !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) {
5143e367455aSJoe Perches				if (WARN("ALLOC_WITH_MULTIPLY",
5144e367455aSJoe Perches					 "Prefer $newfunc over $oldfunc with multiply\n" . $herecurr) &&
5145e367455aSJoe Perches				    $fix) {
5146194f66fcSJoe 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;
514760a55369SJoe Perches
514860a55369SJoe Perches				}
514960a55369SJoe Perches			}
515060a55369SJoe Perches		}
515160a55369SJoe Perches
5152972fdea2SJoe Perches# check for krealloc arg reuse
5153972fdea2SJoe Perches		if ($^V && $^V ge 5.10.0 &&
5154972fdea2SJoe Perches		    $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*\1\s*,/) {
5155972fdea2SJoe Perches			WARN("KREALLOC_ARG_REUSE",
5156972fdea2SJoe Perches			     "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
5157972fdea2SJoe Perches		}
5158972fdea2SJoe Perches
51595ce59ae0SJoe Perches# check for alloc argument mismatch
51605ce59ae0SJoe Perches		if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
51615ce59ae0SJoe Perches			WARN("ALLOC_ARRAY_ARGS",
51625ce59ae0SJoe Perches			     "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
51635ce59ae0SJoe Perches		}
51645ce59ae0SJoe Perches
5165caf2a54fSJoe Perches# check for multiple semicolons
5166caf2a54fSJoe Perches		if ($line =~ /;\s*;\s*$/) {
5167d5e616fcSJoe Perches			if (WARN("ONE_SEMICOLON",
5168d5e616fcSJoe Perches				 "Statements terminations use 1 semicolon\n" . $herecurr) &&
5169d5e616fcSJoe Perches			    $fix) {
5170194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g;
5171d5e616fcSJoe Perches			}
5172d1e2ad07SJoe Perches		}
5173d1e2ad07SJoe Perches
51740ab90191SJoe Perches# check for #defines like: 1 << <digit> that could be BIT(digit)
51750ab90191SJoe Perches		if ($line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) {
51760ab90191SJoe Perches			my $ull = "";
51770ab90191SJoe Perches			$ull = "_ULL" if (defined($1) && $1 =~ /ll/i);
51780ab90191SJoe Perches			if (CHK("BIT_MACRO",
51790ab90191SJoe Perches				"Prefer using the BIT$ull macro\n" . $herecurr) &&
51800ab90191SJoe Perches			    $fix) {
51810ab90191SJoe Perches				$fixed[$fixlinenr] =~ s/\(?\s*1\s*[ulUL]*\s*<<\s*(\d+|$Ident)\s*\)?/BIT${ull}($1)/;
51820ab90191SJoe Perches			}
51830ab90191SJoe Perches		}
51840ab90191SJoe Perches
5185e81f239bSJoe Perches# check for case / default statements not preceded by break/fallthrough/switch
5186c34c09a8SJoe Perches		if ($line =~ /^.\s*(?:case\s+(?:$Ident|$Constant)\s*|default):/) {
5187c34c09a8SJoe Perches			my $has_break = 0;
5188c34c09a8SJoe Perches			my $has_statement = 0;
5189c34c09a8SJoe Perches			my $count = 0;
5190c34c09a8SJoe Perches			my $prevline = $linenr;
5191e81f239bSJoe Perches			while ($prevline > 1 && ($file || $count < 3) && !$has_break) {
5192c34c09a8SJoe Perches				$prevline--;
5193c34c09a8SJoe Perches				my $rline = $rawlines[$prevline - 1];
5194c34c09a8SJoe Perches				my $fline = $lines[$prevline - 1];
5195c34c09a8SJoe Perches				last if ($fline =~ /^\@\@/);
5196c34c09a8SJoe Perches				next if ($fline =~ /^\-/);
5197c34c09a8SJoe Perches				next if ($fline =~ /^.(?:\s*(?:case\s+(?:$Ident|$Constant)[\s$;]*|default):[\s$;]*)*$/);
5198c34c09a8SJoe Perches				$has_break = 1 if ($rline =~ /fall[\s_-]*(through|thru)/i);
5199c34c09a8SJoe Perches				next if ($fline =~ /^.[\s$;]*$/);
5200c34c09a8SJoe Perches				$has_statement = 1;
5201c34c09a8SJoe Perches				$count++;
5202c34c09a8SJoe Perches				$has_break = 1 if ($fline =~ /\bswitch\b|\b(?:break\s*;[\s$;]*$|return\b|goto\b|continue\b)/);
5203c34c09a8SJoe Perches			}
5204c34c09a8SJoe Perches			if (!$has_break && $has_statement) {
5205c34c09a8SJoe Perches				WARN("MISSING_BREAK",
5206c34c09a8SJoe Perches				     "Possible switch case/default not preceeded by break or fallthrough comment\n" . $herecurr);
5207c34c09a8SJoe Perches			}
5208c34c09a8SJoe Perches		}
5209c34c09a8SJoe Perches
5210d1e2ad07SJoe Perches# check for switch/default statements without a break;
5211d1e2ad07SJoe Perches		if ($^V && $^V ge 5.10.0 &&
5212d1e2ad07SJoe Perches		    defined $stat &&
5213d1e2ad07SJoe Perches		    $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
5214d1e2ad07SJoe Perches			my $ctx = '';
5215d1e2ad07SJoe Perches			my $herectx = $here . "\n";
5216d1e2ad07SJoe Perches			my $cnt = statement_rawlines($stat);
5217d1e2ad07SJoe Perches			for (my $n = 0; $n < $cnt; $n++) {
5218d1e2ad07SJoe Perches				$herectx .= raw_line($linenr, $n) . "\n";
5219d1e2ad07SJoe Perches			}
5220d1e2ad07SJoe Perches			WARN("DEFAULT_NO_BREAK",
5221d1e2ad07SJoe Perches			     "switch default: should use break\n" . $herectx);
5222caf2a54fSJoe Perches		}
5223caf2a54fSJoe Perches
522413214adfSAndy Whitcroft# check for gcc specific __FUNCTION__
5225d5e616fcSJoe Perches		if ($line =~ /\b__FUNCTION__\b/) {
5226d5e616fcSJoe Perches			if (WARN("USE_FUNC",
5227d5e616fcSJoe Perches				 "__func__ should be used instead of gcc specific __FUNCTION__\n"  . $herecurr) &&
5228d5e616fcSJoe Perches			    $fix) {
5229194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g;
5230d5e616fcSJoe Perches			}
523113214adfSAndy Whitcroft		}
5232773647a0SAndy Whitcroft
523362ec818fSJoe Perches# check for uses of __DATE__, __TIME__, __TIMESTAMP__
523462ec818fSJoe Perches		while ($line =~ /\b(__(?:DATE|TIME|TIMESTAMP)__)\b/g) {
523562ec818fSJoe Perches			ERROR("DATE_TIME",
523662ec818fSJoe Perches			      "Use of the '$1' macro makes the build non-deterministic\n" . $herecurr);
523762ec818fSJoe Perches		}
523862ec818fSJoe Perches
52392c92488aSJoe Perches# check for use of yield()
52402c92488aSJoe Perches		if ($line =~ /\byield\s*\(\s*\)/) {
52412c92488aSJoe Perches			WARN("YIELD",
52422c92488aSJoe Perches			     "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n"  . $herecurr);
52432c92488aSJoe Perches		}
52442c92488aSJoe Perches
5245179f8f40SJoe Perches# check for comparisons against true and false
5246179f8f40SJoe Perches		if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
5247179f8f40SJoe Perches			my $lead = $1;
5248179f8f40SJoe Perches			my $arg = $2;
5249179f8f40SJoe Perches			my $test = $3;
5250179f8f40SJoe Perches			my $otype = $4;
5251179f8f40SJoe Perches			my $trail = $5;
5252179f8f40SJoe Perches			my $op = "!";
5253179f8f40SJoe Perches
5254179f8f40SJoe Perches			($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
5255179f8f40SJoe Perches
5256179f8f40SJoe Perches			my $type = lc($otype);
5257179f8f40SJoe Perches			if ($type =~ /^(?:true|false)$/) {
5258179f8f40SJoe Perches				if (("$test" eq "==" && "$type" eq "true") ||
5259179f8f40SJoe Perches				    ("$test" eq "!=" && "$type" eq "false")) {
5260179f8f40SJoe Perches					$op = "";
5261179f8f40SJoe Perches				}
5262179f8f40SJoe Perches
5263179f8f40SJoe Perches				CHK("BOOL_COMPARISON",
5264179f8f40SJoe Perches				    "Using comparison to $otype is error prone\n" . $herecurr);
5265179f8f40SJoe Perches
5266179f8f40SJoe Perches## maybe suggesting a correct construct would better
5267179f8f40SJoe Perches##				    "Using comparison to $otype is error prone.  Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
5268179f8f40SJoe Perches
5269179f8f40SJoe Perches			}
5270179f8f40SJoe Perches		}
5271179f8f40SJoe Perches
52724882720bSThomas Gleixner# check for semaphores initialized locked
52734882720bSThomas Gleixner		if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
5274000d1cc1SJoe Perches			WARN("CONSIDER_COMPLETION",
5275000d1cc1SJoe Perches			     "consider using a completion\n" . $herecurr);
5276773647a0SAndy Whitcroft		}
52776712d858SJoe Perches
527867d0a075SJoe Perches# recommend kstrto* over simple_strto* and strict_strto*
527967d0a075SJoe Perches		if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
5280000d1cc1SJoe Perches			WARN("CONSIDER_KSTRTO",
528167d0a075SJoe Perches			     "$1 is obsolete, use k$3 instead\n" . $herecurr);
5282773647a0SAndy Whitcroft		}
52836712d858SJoe Perches
5284ae3ccc46SFabian Frederick# check for __initcall(), use device_initcall() explicitly or more appropriate function please
5285f3db6639SMichael Ellerman		if ($line =~ /^.\s*__initcall\s*\(/) {
5286000d1cc1SJoe Perches			WARN("USE_DEVICE_INITCALL",
5287ae3ccc46SFabian Frederick			     "please use device_initcall() or more appropriate function instead of __initcall() (see include/linux/init.h)\n" . $herecurr);
5288f3db6639SMichael Ellerman		}
52896712d858SJoe Perches
52900f3c5aabSJoe Perches# check for various structs that are normally const (ops, kgdb, device_tree)
52910f3c5aabSJoe Perches		my $const_structs = qr{
52920f3c5aabSJoe Perches				acpi_dock_ops|
529379404849SEmese Revfy				address_space_operations|
529479404849SEmese Revfy				backlight_ops|
529579404849SEmese Revfy				block_device_operations|
529679404849SEmese Revfy				dentry_operations|
529779404849SEmese Revfy				dev_pm_ops|
529879404849SEmese Revfy				dma_map_ops|
529979404849SEmese Revfy				extent_io_ops|
530079404849SEmese Revfy				file_lock_operations|
530179404849SEmese Revfy				file_operations|
530279404849SEmese Revfy				hv_ops|
530379404849SEmese Revfy				ide_dma_ops|
530479404849SEmese Revfy				intel_dvo_dev_ops|
530579404849SEmese Revfy				item_operations|
530679404849SEmese Revfy				iwl_ops|
530779404849SEmese Revfy				kgdb_arch|
530879404849SEmese Revfy				kgdb_io|
530979404849SEmese Revfy				kset_uevent_ops|
531079404849SEmese Revfy				lock_manager_operations|
531179404849SEmese Revfy				microcode_ops|
531279404849SEmese Revfy				mtrr_ops|
531379404849SEmese Revfy				neigh_ops|
531479404849SEmese Revfy				nlmsvc_binding|
53150f3c5aabSJoe Perches				of_device_id|
531679404849SEmese Revfy				pci_raw_ops|
531779404849SEmese Revfy				pipe_buf_operations|
531879404849SEmese Revfy				platform_hibernation_ops|
531979404849SEmese Revfy				platform_suspend_ops|
532079404849SEmese Revfy				proto_ops|
532179404849SEmese Revfy				rpc_pipe_ops|
532279404849SEmese Revfy				seq_operations|
532379404849SEmese Revfy				snd_ac97_build_ops|
532479404849SEmese Revfy				soc_pcmcia_socket_ops|
532579404849SEmese Revfy				stacktrace_ops|
532679404849SEmese Revfy				sysfs_ops|
532779404849SEmese Revfy				tty_operations|
532879404849SEmese Revfy				usb_mon_operations|
532979404849SEmese Revfy				wd_ops}x;
53306903ffb2SAndy Whitcroft		if ($line !~ /\bconst\b/ &&
53310f3c5aabSJoe Perches		    $line =~ /\bstruct\s+($const_structs)\b/) {
5332000d1cc1SJoe Perches			WARN("CONST_STRUCT",
5333000d1cc1SJoe Perches			     "struct $1 should normally be const\n" .
53346903ffb2SAndy Whitcroft				$herecurr);
53352b6db5cbSAndy Whitcroft		}
5336773647a0SAndy Whitcroft
5337773647a0SAndy Whitcroft# use of NR_CPUS is usually wrong
5338773647a0SAndy Whitcroft# ignore definitions of NR_CPUS and usage to define arrays as likely right
5339773647a0SAndy Whitcroft		if ($line =~ /\bNR_CPUS\b/ &&
5340c45dcabdSAndy Whitcroft		    $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
5341c45dcabdSAndy Whitcroft		    $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
5342171ae1a4SAndy Whitcroft		    $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
5343171ae1a4SAndy Whitcroft		    $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
5344171ae1a4SAndy Whitcroft		    $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
5345773647a0SAndy Whitcroft		{
5346000d1cc1SJoe Perches			WARN("NR_CPUS",
5347000d1cc1SJoe Perches			     "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
5348773647a0SAndy Whitcroft		}
53499c9ba34eSAndy Whitcroft
535052ea8506SJoe Perches# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
535152ea8506SJoe Perches		if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
535252ea8506SJoe Perches			ERROR("DEFINE_ARCH_HAS",
535352ea8506SJoe Perches			      "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
535452ea8506SJoe Perches		}
535552ea8506SJoe Perches
5356acd9362cSJoe Perches# likely/unlikely comparisons similar to "(likely(foo) > 0)"
5357acd9362cSJoe Perches		if ($^V && $^V ge 5.10.0 &&
5358acd9362cSJoe Perches		    $line =~ /\b((?:un)?likely)\s*\(\s*$FuncArg\s*\)\s*$Compare/) {
5359acd9362cSJoe Perches			WARN("LIKELY_MISUSE",
5360acd9362cSJoe Perches			     "Using $1 should generally have parentheses around the comparison\n" . $herecurr);
5361acd9362cSJoe Perches		}
5362acd9362cSJoe Perches
5363691d77b6SAndy Whitcroft# whine mightly about in_atomic
5364691d77b6SAndy Whitcroft		if ($line =~ /\bin_atomic\s*\(/) {
5365691d77b6SAndy Whitcroft			if ($realfile =~ m@^drivers/@) {
5366000d1cc1SJoe Perches				ERROR("IN_ATOMIC",
5367000d1cc1SJoe Perches				      "do not use in_atomic in drivers\n" . $herecurr);
5368f4a87736SAndy Whitcroft			} elsif ($realfile !~ m@^kernel/@) {
5369000d1cc1SJoe Perches				WARN("IN_ATOMIC",
5370000d1cc1SJoe Perches				     "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
5371691d77b6SAndy Whitcroft			}
5372691d77b6SAndy Whitcroft		}
53731704f47bSPeter Zijlstra
53741704f47bSPeter Zijlstra# check for lockdep_set_novalidate_class
53751704f47bSPeter Zijlstra		if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
53761704f47bSPeter Zijlstra		    $line =~ /__lockdep_no_validate__\s*\)/ ) {
53771704f47bSPeter Zijlstra			if ($realfile !~ m@^kernel/lockdep@ &&
53781704f47bSPeter Zijlstra			    $realfile !~ m@^include/linux/lockdep@ &&
53791704f47bSPeter Zijlstra			    $realfile !~ m@^drivers/base/core@) {
5380000d1cc1SJoe Perches				ERROR("LOCKDEP",
5381000d1cc1SJoe Perches				      "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
53821704f47bSPeter Zijlstra			}
53831704f47bSPeter Zijlstra		}
538488f8831cSDave Jones
5385b392c64fSJoe Perches		if ($line =~ /debugfs_create_\w+.*\b$mode_perms_world_writable\b/ ||
5386b392c64fSJoe Perches		    $line =~ /DEVICE_ATTR.*\b$mode_perms_world_writable\b/) {
5387000d1cc1SJoe Perches			WARN("EXPORTED_WORLD_WRITABLE",
5388000d1cc1SJoe Perches			     "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
538988f8831cSDave Jones		}
53902435880fSJoe Perches
5391515a235eSJoe Perches# Mode permission misuses where it seems decimal should be octal
5392515a235eSJoe Perches# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop
5393515a235eSJoe Perches		if ($^V && $^V ge 5.10.0 &&
5394515a235eSJoe Perches		    $line =~ /$mode_perms_search/) {
53952435880fSJoe Perches			foreach my $entry (@mode_permission_funcs) {
53962435880fSJoe Perches				my $func = $entry->[0];
53972435880fSJoe Perches				my $arg_pos = $entry->[1];
53982435880fSJoe Perches
53992435880fSJoe Perches				my $skip_args = "";
54002435880fSJoe Perches				if ($arg_pos > 1) {
54012435880fSJoe Perches					$arg_pos--;
54022435880fSJoe Perches					$skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}";
54032435880fSJoe Perches				}
54042435880fSJoe Perches				my $test = "\\b$func\\s*\\(${skip_args}([\\d]+)\\s*[,\\)]";
5405515a235eSJoe Perches				if ($line =~ /$test/) {
54062435880fSJoe Perches					my $val = $1;
54072435880fSJoe Perches					$val = $6 if ($skip_args ne "");
54082435880fSJoe Perches
54091727cc70SJoe Perches					if ($val !~ /^0$/ &&
54101727cc70SJoe Perches					    (($val =~ /^$Int$/ && $val !~ /^$Octal$/) ||
54111727cc70SJoe Perches					     length($val) ne 4)) {
54122435880fSJoe Perches						ERROR("NON_OCTAL_PERMISSIONS",
54131727cc70SJoe Perches						      "Use 4 digit octal (0777) not decimal permissions\n" . $herecurr);
5414c0a5c898SJoe Perches					} elsif ($val =~ /^$Octal$/ && (oct($val) & 02)) {
5415c0a5c898SJoe Perches						ERROR("EXPORTED_WORLD_WRITABLE",
5416c0a5c898SJoe Perches						      "Exporting writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
54172435880fSJoe Perches					}
54182435880fSJoe Perches				}
54192435880fSJoe Perches			}
542013214adfSAndy Whitcroft		}
5421515a235eSJoe Perches	}
542213214adfSAndy Whitcroft
542313214adfSAndy Whitcroft	# If we have no input at all, then there is nothing to report on
542413214adfSAndy Whitcroft	# so just keep quiet.
542513214adfSAndy Whitcroft	if ($#rawlines == -1) {
542613214adfSAndy Whitcroft		exit(0);
54270a920b5bSAndy Whitcroft	}
54280a920b5bSAndy Whitcroft
54298905a67cSAndy Whitcroft	# In mailback mode only produce a report in the negative, for
54308905a67cSAndy Whitcroft	# things that appear to be patches.
54318905a67cSAndy Whitcroft	if ($mailback && ($clean == 1 || !$is_patch)) {
54328905a67cSAndy Whitcroft		exit(0);
54338905a67cSAndy Whitcroft	}
54348905a67cSAndy Whitcroft
54358905a67cSAndy Whitcroft	# This is not a patch, and we are are in 'no-patch' mode so
54368905a67cSAndy Whitcroft	# just keep quiet.
54378905a67cSAndy Whitcroft	if (!$chk_patch && !$is_patch) {
54388905a67cSAndy Whitcroft		exit(0);
54398905a67cSAndy Whitcroft	}
54408905a67cSAndy Whitcroft
54418905a67cSAndy Whitcroft	if (!$is_patch) {
5442000d1cc1SJoe Perches		ERROR("NOT_UNIFIED_DIFF",
5443000d1cc1SJoe Perches		      "Does not appear to be a unified-diff format patch\n");
54440a920b5bSAndy Whitcroft	}
54450a920b5bSAndy Whitcroft	if ($is_patch && $chk_signoff && $signoff == 0) {
5446000d1cc1SJoe Perches		ERROR("MISSING_SIGN_OFF",
5447000d1cc1SJoe Perches		      "Missing Signed-off-by: line(s)\n");
54480a920b5bSAndy Whitcroft	}
54490a920b5bSAndy Whitcroft
5450f0a594c1SAndy Whitcroft	print report_dump();
545113214adfSAndy Whitcroft	if ($summary && !($clean == 1 && $quiet == 1)) {
545213214adfSAndy Whitcroft		print "$filename " if ($summary_file);
54536c72ffaaSAndy Whitcroft		print "total: $cnt_error errors, $cnt_warn warnings, " .
54546c72ffaaSAndy Whitcroft			(($check)? "$cnt_chk checks, " : "") .
54556c72ffaaSAndy Whitcroft			"$cnt_lines lines checked\n";
54568905a67cSAndy Whitcroft		print "\n" if ($quiet == 0);
54576c72ffaaSAndy Whitcroft	}
54588905a67cSAndy Whitcroft
5459d2c0a235SAndy Whitcroft	if ($quiet == 0) {
5460d1fe9c09SJoe Perches
5461d1fe9c09SJoe Perches		if ($^V lt 5.10.0) {
5462d1fe9c09SJoe Perches			print("NOTE: perl $^V is not modern enough to detect all possible issues.\n");
5463d1fe9c09SJoe Perches			print("An upgrade to at least perl v5.10.0 is suggested.\n\n");
5464d1fe9c09SJoe Perches		}
5465d1fe9c09SJoe Perches
5466d2c0a235SAndy Whitcroft		# If there were whitespace errors which cleanpatch can fix
5467d2c0a235SAndy Whitcroft		# then suggest that.
5468d2c0a235SAndy Whitcroft		if ($rpt_cleaners) {
5469d2c0a235SAndy Whitcroft			print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n";
5470d2c0a235SAndy Whitcroft			print "      scripts/cleanfile\n\n";
5471b0781216SMike Frysinger			$rpt_cleaners = 0;
5472d2c0a235SAndy Whitcroft		}
5473d2c0a235SAndy Whitcroft	}
5474d2c0a235SAndy Whitcroft
547591bfe484SJoe Perches	hash_show_words(\%use_type, "Used");
547691bfe484SJoe Perches	hash_show_words(\%ignore_type, "Ignored");
5477000d1cc1SJoe Perches
5478d752fcc8SJoe Perches	if ($clean == 0 && $fix &&
5479d752fcc8SJoe Perches	    ("@rawlines" ne "@fixed" ||
5480d752fcc8SJoe Perches	     $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) {
54819624b8d6SJoe Perches		my $newfile = $filename;
54829624b8d6SJoe Perches		$newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace);
54833705ce5bSJoe Perches		my $linecount = 0;
54843705ce5bSJoe Perches		my $f;
54853705ce5bSJoe Perches
5486d752fcc8SJoe Perches		@fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted);
5487d752fcc8SJoe Perches
54883705ce5bSJoe Perches		open($f, '>', $newfile)
54893705ce5bSJoe Perches		    or die "$P: Can't open $newfile for write\n";
54903705ce5bSJoe Perches		foreach my $fixed_line (@fixed) {
54913705ce5bSJoe Perches			$linecount++;
54923705ce5bSJoe Perches			if ($file) {
54933705ce5bSJoe Perches				if ($linecount > 3) {
54943705ce5bSJoe Perches					$fixed_line =~ s/^\+//;
54953705ce5bSJoe Perches					print $f $fixed_line . "\n";
54963705ce5bSJoe Perches				}
54973705ce5bSJoe Perches			} else {
54983705ce5bSJoe Perches				print $f $fixed_line . "\n";
54993705ce5bSJoe Perches			}
55003705ce5bSJoe Perches		}
55013705ce5bSJoe Perches		close($f);
55023705ce5bSJoe Perches
55033705ce5bSJoe Perches		if (!$quiet) {
55043705ce5bSJoe Perches			print << "EOM";
55053705ce5bSJoe PerchesWrote EXPERIMENTAL --fix correction(s) to '$newfile'
55063705ce5bSJoe Perches
55073705ce5bSJoe PerchesDo _NOT_ trust the results written to this file.
55083705ce5bSJoe PerchesDo _NOT_ submit these changes without inspecting them for correctness.
55093705ce5bSJoe Perches
55103705ce5bSJoe PerchesThis EXPERIMENTAL file is simply a convenience to help rewrite patches.
55113705ce5bSJoe PerchesNo warranties, expressed or implied...
55123705ce5bSJoe Perches
55133705ce5bSJoe PerchesEOM
55143705ce5bSJoe Perches		}
55153705ce5bSJoe Perches	}
55163705ce5bSJoe Perches
55170a920b5bSAndy Whitcroft	if ($clean == 1 && $quiet == 0) {
5518c2fdda0dSAndy Whitcroft		print "$vname has no obvious style problems and is ready for submission.\n"
55190a920b5bSAndy Whitcroft	}
55200a920b5bSAndy Whitcroft	if ($clean == 0 && $quiet == 0) {
5521000d1cc1SJoe Perches		print << "EOM";
5522000d1cc1SJoe Perches$vname has style problems, please review.
5523000d1cc1SJoe Perches
5524000d1cc1SJoe PerchesIf any of these errors are false positives, please report
5525000d1cc1SJoe Perchesthem to the maintainer, see CHECKPATCH in MAINTAINERS.
5526000d1cc1SJoe PerchesEOM
55270a920b5bSAndy Whitcroft	}
552813214adfSAndy Whitcroft
55290a920b5bSAndy Whitcroft	return $clean;
55300a920b5bSAndy Whitcroft}
5531