xref: /linux-6.15/scripts/checkpatch.pl (revision d2207ccb)
10a920b5bSAndy Whitcroft#!/usr/bin/perl -w
2dbf004d7SDave Jones# (c) 2001, Dave Jones. (the file handling bit)
300df344fSAndy Whitcroft# (c) 2005, Joel Schopp <[email protected]> (the ugly bit)
42a5a2c25SAndy Whitcroft# (c) 2007,2008, Andy Whitcroft <[email protected]> (new conditions, test suite)
5015830beSAndy Whitcroft# (c) 2008-2010 Andy Whitcroft <[email protected]>
60a920b5bSAndy Whitcroft# Licensed under the terms of the GNU GPL License version 2
70a920b5bSAndy Whitcroft
80a920b5bSAndy Whitcroftuse strict;
9c707a81dSJoe Perchesuse POSIX;
100a920b5bSAndy Whitcroft
110a920b5bSAndy Whitcroftmy $P = $0;
1200df344fSAndy Whitcroft$P =~ s@.*/@@g;
130a920b5bSAndy Whitcroft
14000d1cc1SJoe Perchesmy $V = '0.32';
150a920b5bSAndy Whitcroft
160a920b5bSAndy Whitcroftuse Getopt::Long qw(:config no_auto_abbrev);
170a920b5bSAndy Whitcroft
180a920b5bSAndy Whitcroftmy $quiet = 0;
190a920b5bSAndy Whitcroftmy $tree = 1;
200a920b5bSAndy Whitcroftmy $chk_signoff = 1;
210a920b5bSAndy Whitcroftmy $chk_patch = 1;
22773647a0SAndy Whitcroftmy $tst_only;
236c72ffaaSAndy Whitcroftmy $emacs = 0;
248905a67cSAndy Whitcroftmy $terse = 0;
256c72ffaaSAndy Whitcroftmy $file = 0;
266c72ffaaSAndy Whitcroftmy $check = 0;
272ac73b4fSJoe Perchesmy $check_orig = 0;
288905a67cSAndy Whitcroftmy $summary = 1;
298905a67cSAndy Whitcroftmy $mailback = 0;
3013214adfSAndy Whitcroftmy $summary_file = 0;
31000d1cc1SJoe Perchesmy $show_types = 0;
323705ce5bSJoe Perchesmy $fix = 0;
339624b8d6SJoe Perchesmy $fix_inplace = 0;
346c72ffaaSAndy Whitcroftmy $root;
35c2fdda0dSAndy Whitcroftmy %debug;
363445686aSJoe Perchesmy %camelcase = ();
3791bfe484SJoe Perchesmy %use_type = ();
3891bfe484SJoe Perchesmy @use = ();
3991bfe484SJoe Perchesmy %ignore_type = ();
40000d1cc1SJoe Perchesmy @ignore = ();
4177f5b10aSHannes Edermy $help = 0;
42000d1cc1SJoe Perchesmy $configuration_file = ".checkpatch.conf";
436cd7f386SJoe Perchesmy $max_line_length = 80;
44d62a201fSDave Hansenmy $ignore_perl_version = 0;
45d62a201fSDave Hansenmy $minimum_perl_version = 5.10.0;
4656193274SVadim Bendeburymy $min_conf_desc_length = 4;
4777f5b10aSHannes Eder
4877f5b10aSHannes Edersub help {
4977f5b10aSHannes Eder	my ($exitcode) = @_;
5077f5b10aSHannes Eder
5177f5b10aSHannes Eder	print << "EOM";
5277f5b10aSHannes EderUsage: $P [OPTION]... [FILE]...
5377f5b10aSHannes EderVersion: $V
5477f5b10aSHannes Eder
5577f5b10aSHannes EderOptions:
5677f5b10aSHannes Eder  -q, --quiet                quiet
5777f5b10aSHannes Eder  --no-tree                  run without a kernel tree
5877f5b10aSHannes Eder  --no-signoff               do not check for 'Signed-off-by' line
5977f5b10aSHannes Eder  --patch                    treat FILE as patchfile (default)
6077f5b10aSHannes Eder  --emacs                    emacs compile window format
6177f5b10aSHannes Eder  --terse                    one line per report
6277f5b10aSHannes Eder  -f, --file                 treat FILE as regular source file
6377f5b10aSHannes Eder  --subjective, --strict     enable more subjective tests
6491bfe484SJoe Perches  --types TYPE(,TYPE2...)    show only these comma separated message types
65000d1cc1SJoe Perches  --ignore TYPE(,TYPE2...)   ignore various comma separated message types
666cd7f386SJoe Perches  --max-line-length=n        set the maximum line length, if exceeded, warn
6756193274SVadim Bendebury  --min-conf-desc-length=n   set the min description length, if shorter, warn
68000d1cc1SJoe Perches  --show-types               show the message "types" in the output
6977f5b10aSHannes Eder  --root=PATH                PATH to the kernel tree root
7077f5b10aSHannes Eder  --no-summary               suppress the per-file summary
7177f5b10aSHannes Eder  --mailback                 only produce a report in case of warnings/errors
7277f5b10aSHannes Eder  --summary-file             include the filename in summary
7377f5b10aSHannes Eder  --debug KEY=[0|1]          turn on/off debugging of KEY, where KEY is one of
7477f5b10aSHannes Eder                             'values', 'possible', 'type', and 'attr' (default
7577f5b10aSHannes Eder                             is all off)
7677f5b10aSHannes Eder  --test-only=WORD           report only warnings/errors containing WORD
7777f5b10aSHannes Eder                             literally
783705ce5bSJoe Perches  --fix                      EXPERIMENTAL - may create horrible results
793705ce5bSJoe Perches                             If correctable single-line errors exist, create
803705ce5bSJoe Perches                             "<inputfile>.EXPERIMENTAL-checkpatch-fixes"
813705ce5bSJoe Perches                             with potential errors corrected to the preferred
823705ce5bSJoe Perches                             checkpatch style
839624b8d6SJoe Perches  --fix-inplace              EXPERIMENTAL - may create horrible results
849624b8d6SJoe Perches                             Is the same as --fix, but overwrites the input
859624b8d6SJoe Perches                             file.  It's your fault if there's no backup or git
86d62a201fSDave Hansen  --ignore-perl-version      override checking of perl version.  expect
87d62a201fSDave Hansen                             runtime errors.
8877f5b10aSHannes Eder  -h, --help, --version      display this help and exit
8977f5b10aSHannes Eder
9077f5b10aSHannes EderWhen FILE is - read standard input.
9177f5b10aSHannes EderEOM
9277f5b10aSHannes Eder
9377f5b10aSHannes Eder	exit($exitcode);
9477f5b10aSHannes Eder}
9577f5b10aSHannes Eder
96000d1cc1SJoe Perchesmy $conf = which_conf($configuration_file);
97000d1cc1SJoe Perchesif (-f $conf) {
98000d1cc1SJoe Perches	my @conf_args;
99000d1cc1SJoe Perches	open(my $conffile, '<', "$conf")
100000d1cc1SJoe Perches	    or warn "$P: Can't find a readable $configuration_file file $!\n";
101000d1cc1SJoe Perches
102000d1cc1SJoe Perches	while (<$conffile>) {
103000d1cc1SJoe Perches		my $line = $_;
104000d1cc1SJoe Perches
105000d1cc1SJoe Perches		$line =~ s/\s*\n?$//g;
106000d1cc1SJoe Perches		$line =~ s/^\s*//g;
107000d1cc1SJoe Perches		$line =~ s/\s+/ /g;
108000d1cc1SJoe Perches
109000d1cc1SJoe Perches		next if ($line =~ m/^\s*#/);
110000d1cc1SJoe Perches		next if ($line =~ m/^\s*$/);
111000d1cc1SJoe Perches
112000d1cc1SJoe Perches		my @words = split(" ", $line);
113000d1cc1SJoe Perches		foreach my $word (@words) {
114000d1cc1SJoe Perches			last if ($word =~ m/^#/);
115000d1cc1SJoe Perches			push (@conf_args, $word);
116000d1cc1SJoe Perches		}
117000d1cc1SJoe Perches	}
118000d1cc1SJoe Perches	close($conffile);
119000d1cc1SJoe Perches	unshift(@ARGV, @conf_args) if @conf_args;
120000d1cc1SJoe Perches}
121000d1cc1SJoe Perches
1220a920b5bSAndy WhitcroftGetOptions(
1236c72ffaaSAndy Whitcroft	'q|quiet+'	=> \$quiet,
1240a920b5bSAndy Whitcroft	'tree!'		=> \$tree,
1250a920b5bSAndy Whitcroft	'signoff!'	=> \$chk_signoff,
1260a920b5bSAndy Whitcroft	'patch!'	=> \$chk_patch,
1276c72ffaaSAndy Whitcroft	'emacs!'	=> \$emacs,
1288905a67cSAndy Whitcroft	'terse!'	=> \$terse,
12977f5b10aSHannes Eder	'f|file!'	=> \$file,
1306c72ffaaSAndy Whitcroft	'subjective!'	=> \$check,
1316c72ffaaSAndy Whitcroft	'strict!'	=> \$check,
132000d1cc1SJoe Perches	'ignore=s'	=> \@ignore,
13391bfe484SJoe Perches	'types=s'	=> \@use,
134000d1cc1SJoe Perches	'show-types!'	=> \$show_types,
1356cd7f386SJoe Perches	'max-line-length=i' => \$max_line_length,
13656193274SVadim Bendebury	'min-conf-desc-length=i' => \$min_conf_desc_length,
1376c72ffaaSAndy Whitcroft	'root=s'	=> \$root,
1388905a67cSAndy Whitcroft	'summary!'	=> \$summary,
1398905a67cSAndy Whitcroft	'mailback!'	=> \$mailback,
14013214adfSAndy Whitcroft	'summary-file!'	=> \$summary_file,
1413705ce5bSJoe Perches	'fix!'		=> \$fix,
1429624b8d6SJoe Perches	'fix-inplace!'	=> \$fix_inplace,
143d62a201fSDave Hansen	'ignore-perl-version!' => \$ignore_perl_version,
144c2fdda0dSAndy Whitcroft	'debug=s'	=> \%debug,
145773647a0SAndy Whitcroft	'test-only=s'	=> \$tst_only,
14677f5b10aSHannes Eder	'h|help'	=> \$help,
14777f5b10aSHannes Eder	'version'	=> \$help
14877f5b10aSHannes Eder) or help(1);
14977f5b10aSHannes Eder
15077f5b10aSHannes Ederhelp(0) if ($help);
1510a920b5bSAndy Whitcroft
1529624b8d6SJoe Perches$fix = 1 if ($fix_inplace);
1532ac73b4fSJoe Perches$check_orig = $check;
1549624b8d6SJoe Perches
1550a920b5bSAndy Whitcroftmy $exit = 0;
1560a920b5bSAndy Whitcroft
157d62a201fSDave Hansenif ($^V && $^V lt $minimum_perl_version) {
158d62a201fSDave Hansen	printf "$P: requires at least perl version %vd\n", $minimum_perl_version;
159d62a201fSDave Hansen	if (!$ignore_perl_version) {
160d62a201fSDave Hansen		exit(1);
161d62a201fSDave Hansen	}
162d62a201fSDave Hansen}
163d62a201fSDave Hansen
1640a920b5bSAndy Whitcroftif ($#ARGV < 0) {
16577f5b10aSHannes Eder	print "$P: no input files\n";
1660a920b5bSAndy Whitcroft	exit(1);
1670a920b5bSAndy Whitcroft}
1680a920b5bSAndy Whitcroft
16991bfe484SJoe Perchessub hash_save_array_words {
17091bfe484SJoe Perches	my ($hashRef, $arrayRef) = @_;
17191bfe484SJoe Perches
17291bfe484SJoe Perches	my @array = split(/,/, join(',', @$arrayRef));
17391bfe484SJoe Perches	foreach my $word (@array) {
174000d1cc1SJoe Perches		$word =~ s/\s*\n?$//g;
175000d1cc1SJoe Perches		$word =~ s/^\s*//g;
176000d1cc1SJoe Perches		$word =~ s/\s+/ /g;
177000d1cc1SJoe Perches		$word =~ tr/[a-z]/[A-Z]/;
178000d1cc1SJoe Perches
179000d1cc1SJoe Perches		next if ($word =~ m/^\s*#/);
180000d1cc1SJoe Perches		next if ($word =~ m/^\s*$/);
181000d1cc1SJoe Perches
18291bfe484SJoe Perches		$hashRef->{$word}++;
183000d1cc1SJoe Perches	}
18491bfe484SJoe Perches}
18591bfe484SJoe Perches
18691bfe484SJoe Perchessub hash_show_words {
18791bfe484SJoe Perches	my ($hashRef, $prefix) = @_;
18891bfe484SJoe Perches
18958cb3cf6SJoe Perches	if ($quiet == 0 && keys %$hashRef) {
19091bfe484SJoe Perches		print "NOTE: $prefix message types:";
19158cb3cf6SJoe Perches		foreach my $word (sort keys %$hashRef) {
19291bfe484SJoe Perches			print " $word";
19391bfe484SJoe Perches		}
19491bfe484SJoe Perches		print "\n\n";
19591bfe484SJoe Perches	}
19691bfe484SJoe Perches}
19791bfe484SJoe Perches
19891bfe484SJoe Percheshash_save_array_words(\%ignore_type, \@ignore);
19991bfe484SJoe Percheshash_save_array_words(\%use_type, \@use);
200000d1cc1SJoe Perches
201c2fdda0dSAndy Whitcroftmy $dbg_values = 0;
202c2fdda0dSAndy Whitcroftmy $dbg_possible = 0;
2037429c690SAndy Whitcroftmy $dbg_type = 0;
204a1ef277eSAndy Whitcroftmy $dbg_attr = 0;
205c2fdda0dSAndy Whitcroftfor my $key (keys %debug) {
20621caa13cSAndy Whitcroft	## no critic
20721caa13cSAndy Whitcroft	eval "\${dbg_$key} = '$debug{$key}';";
20821caa13cSAndy Whitcroft	die "$@" if ($@);
209c2fdda0dSAndy Whitcroft}
210c2fdda0dSAndy Whitcroft
211d2c0a235SAndy Whitcroftmy $rpt_cleaners = 0;
212d2c0a235SAndy Whitcroft
2138905a67cSAndy Whitcroftif ($terse) {
2148905a67cSAndy Whitcroft	$emacs = 1;
2158905a67cSAndy Whitcroft	$quiet++;
2168905a67cSAndy Whitcroft}
2178905a67cSAndy Whitcroft
2186c72ffaaSAndy Whitcroftif ($tree) {
2196c72ffaaSAndy Whitcroft	if (defined $root) {
2206c72ffaaSAndy Whitcroft		if (!top_of_kernel_tree($root)) {
2216c72ffaaSAndy Whitcroft			die "$P: $root: --root does not point at a valid tree\n";
2226c72ffaaSAndy Whitcroft		}
2236c72ffaaSAndy Whitcroft	} else {
2246c72ffaaSAndy Whitcroft		if (top_of_kernel_tree('.')) {
2256c72ffaaSAndy Whitcroft			$root = '.';
2266c72ffaaSAndy Whitcroft		} elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
2276c72ffaaSAndy Whitcroft						top_of_kernel_tree($1)) {
2286c72ffaaSAndy Whitcroft			$root = $1;
2296c72ffaaSAndy Whitcroft		}
2306c72ffaaSAndy Whitcroft	}
2316c72ffaaSAndy Whitcroft
2326c72ffaaSAndy Whitcroft	if (!defined $root) {
2330a920b5bSAndy Whitcroft		print "Must be run from the top-level dir. of a kernel tree\n";
2340a920b5bSAndy Whitcroft		exit(2);
2350a920b5bSAndy Whitcroft	}
2366c72ffaaSAndy Whitcroft}
2376c72ffaaSAndy Whitcroft
2386c72ffaaSAndy Whitcroftmy $emitted_corrupt = 0;
2396c72ffaaSAndy Whitcroft
2402ceb532bSAndy Whitcroftour $Ident	= qr{
2412ceb532bSAndy Whitcroft			[A-Za-z_][A-Za-z\d_]*
2422ceb532bSAndy Whitcroft			(?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
2432ceb532bSAndy Whitcroft		}x;
2446c72ffaaSAndy Whitcroftour $Storage	= qr{extern|static|asmlinkage};
2456c72ffaaSAndy Whitcroftour $Sparse	= qr{
2466c72ffaaSAndy Whitcroft			__user|
2476c72ffaaSAndy Whitcroft			__kernel|
2486c72ffaaSAndy Whitcroft			__force|
2496c72ffaaSAndy Whitcroft			__iomem|
2506c72ffaaSAndy Whitcroft			__must_check|
2516c72ffaaSAndy Whitcroft			__init_refok|
252417495edSAndy Whitcroft			__kprobes|
253165e72a6SSven Eckelmann			__ref|
254165e72a6SSven Eckelmann			__rcu
2556c72ffaaSAndy Whitcroft		}x;
256e970b884SJoe Perchesour $InitAttributePrefix = qr{__(?:mem|cpu|dev|net_|)};
257e970b884SJoe Perchesour $InitAttributeData = qr{$InitAttributePrefix(?:initdata\b)};
258e970b884SJoe Perchesour $InitAttributeConst = qr{$InitAttributePrefix(?:initconst\b)};
259e970b884SJoe Perchesour $InitAttributeInit = qr{$InitAttributePrefix(?:init\b)};
260e970b884SJoe Perchesour $InitAttribute = qr{$InitAttributeData|$InitAttributeConst|$InitAttributeInit};
2618716de38SJoe Perches
26252131292SWolfram Sang# Notes to $Attribute:
26352131292SWolfram Sang# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
2646c72ffaaSAndy Whitcroftour $Attribute	= qr{
2656c72ffaaSAndy Whitcroft			const|
26603f1df7dSJoe Perches			__percpu|
26703f1df7dSJoe Perches			__nocast|
26803f1df7dSJoe Perches			__safe|
26903f1df7dSJoe Perches			__bitwise__|
27003f1df7dSJoe Perches			__packed__|
27103f1df7dSJoe Perches			__packed2__|
27203f1df7dSJoe Perches			__naked|
27303f1df7dSJoe Perches			__maybe_unused|
27403f1df7dSJoe Perches			__always_unused|
27503f1df7dSJoe Perches			__noreturn|
27603f1df7dSJoe Perches			__used|
27703f1df7dSJoe Perches			__cold|
27803f1df7dSJoe Perches			__noclone|
27903f1df7dSJoe Perches			__deprecated|
2806c72ffaaSAndy Whitcroft			__read_mostly|
2816c72ffaaSAndy Whitcroft			__kprobes|
2828716de38SJoe Perches			$InitAttribute|
28324e1d81aSAndy Whitcroft			____cacheline_aligned|
28424e1d81aSAndy Whitcroft			____cacheline_aligned_in_smp|
2855fe3af11SAndy Whitcroft			____cacheline_internodealigned_in_smp|
2865fe3af11SAndy Whitcroft			__weak
2876c72ffaaSAndy Whitcroft		  }x;
288c45dcabdSAndy Whitcroftour $Modifier;
28991cb5195SJoe Perchesour $Inline	= qr{inline|__always_inline|noinline|__inline|__inline__};
2906c72ffaaSAndy Whitcroftour $Member	= qr{->$Ident|\.$Ident|\[[^]]*\]};
2916c72ffaaSAndy Whitcroftour $Lval	= qr{$Ident(?:$Member)*};
2926c72ffaaSAndy Whitcroft
29395e2c602SJoe Perchesour $Int_type	= qr{(?i)llu|ull|ll|lu|ul|l|u};
29495e2c602SJoe Perchesour $Binary	= qr{(?i)0b[01]+$Int_type?};
29595e2c602SJoe Perchesour $Hex	= qr{(?i)0x[0-9a-f]+$Int_type?};
29695e2c602SJoe Perchesour $Int	= qr{[0-9]+$Int_type?};
2972435880fSJoe Perchesour $Octal	= qr{0[0-7]+$Int_type?};
298326b1ffcSJoe Perchesour $Float_hex	= qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?};
299326b1ffcSJoe Perchesour $Float_dec	= qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?};
300326b1ffcSJoe Perchesour $Float_int	= qr{(?i)[0-9]+e-?[0-9]+[fl]?};
30174349bccSJoe Perchesour $Float	= qr{$Float_hex|$Float_dec|$Float_int};
3022435880fSJoe Perchesour $Constant	= qr{$Float|$Binary|$Octal|$Hex|$Int};
303326b1ffcSJoe Perchesour $Assignment	= qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=};
304447432f3SJoe Perchesour $Compare    = qr{<=|>=|==|!=|<|(?<!-)>};
30523f780c9SJoe Perchesour $Arithmetic = qr{\+|-|\*|\/|%};
3066c72ffaaSAndy Whitcroftour $Operators	= qr{
3076c72ffaaSAndy Whitcroft			<=|>=|==|!=|
3086c72ffaaSAndy Whitcroft			=>|->|<<|>>|<|>|!|~|
30923f780c9SJoe Perches			&&|\|\||,|\^|\+\+|--|&|\||$Arithmetic
3106c72ffaaSAndy Whitcroft		  }x;
3116c72ffaaSAndy Whitcroft
31291cb5195SJoe Perchesour $c90_Keywords = qr{do|for|while|if|else|return|goto|continue|switch|default|case|break}x;
31391cb5195SJoe Perches
3148905a67cSAndy Whitcroftour $NonptrType;
3151813087dSJoe Perchesour $NonptrTypeMisordered;
3168716de38SJoe Perchesour $NonptrTypeWithAttr;
3178905a67cSAndy Whitcroftour $Type;
3181813087dSJoe Perchesour $TypeMisordered;
3198905a67cSAndy Whitcroftour $Declare;
3201813087dSJoe Perchesour $DeclareMisordered;
3218905a67cSAndy Whitcroft
32215662b3eSJoe Perchesour $NON_ASCII_UTF8	= qr{
32315662b3eSJoe Perches	[\xC2-\xDF][\x80-\xBF]               # non-overlong 2-byte
324171ae1a4SAndy Whitcroft	|  \xE0[\xA0-\xBF][\x80-\xBF]        # excluding overlongs
325171ae1a4SAndy Whitcroft	| [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}  # straight 3-byte
326171ae1a4SAndy Whitcroft	|  \xED[\x80-\x9F][\x80-\xBF]        # excluding surrogates
327171ae1a4SAndy Whitcroft	|  \xF0[\x90-\xBF][\x80-\xBF]{2}     # planes 1-3
328171ae1a4SAndy Whitcroft	| [\xF1-\xF3][\x80-\xBF]{3}          # planes 4-15
329171ae1a4SAndy Whitcroft	|  \xF4[\x80-\x8F][\x80-\xBF]{2}     # plane 16
330171ae1a4SAndy Whitcroft}x;
331171ae1a4SAndy Whitcroft
33215662b3eSJoe Perchesour $UTF8	= qr{
33315662b3eSJoe Perches	[\x09\x0A\x0D\x20-\x7E]              # ASCII
33415662b3eSJoe Perches	| $NON_ASCII_UTF8
33515662b3eSJoe Perches}x;
33615662b3eSJoe Perches
3378ed22cadSAndy Whitcroftour $typeTypedefs = qr{(?x:
338fb9e9096SAndy Whitcroft	(?:__)?(?:u|s|be|le)(?:8|16|32|64)|
3398ed22cadSAndy Whitcroft	atomic_t
3408ed22cadSAndy Whitcroft)};
3418ed22cadSAndy Whitcroft
342691e669bSJoe Perchesour $logFunctions = qr{(?x:
3436e60c02eSJoe Perches	printk(?:_ratelimited|_once|)|
3447d0b6594SJacob Keller	(?:[a-z0-9]+_){1,2}(?:printk|emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)(?:_ratelimited|_once|)|
3456e60c02eSJoe Perches	WARN(?:_RATELIMIT|_ONCE|)|
346b0531722SJoe Perches	panic|
34706668727SJoe Perches	MODULE_[A-Z_]+|
34806668727SJoe Perches	seq_vprintf|seq_printf|seq_puts
349691e669bSJoe Perches)};
350691e669bSJoe Perches
35120112475SJoe Perchesour $signature_tags = qr{(?xi:
35220112475SJoe Perches	Signed-off-by:|
35320112475SJoe Perches	Acked-by:|
35420112475SJoe Perches	Tested-by:|
35520112475SJoe Perches	Reviewed-by:|
35620112475SJoe Perches	Reported-by:|
3578543ae12SMugunthan V N	Suggested-by:|
35820112475SJoe Perches	To:|
35920112475SJoe Perches	Cc:
36020112475SJoe Perches)};
36120112475SJoe Perches
3621813087dSJoe Perchesour @typeListMisordered = (
3631813087dSJoe Perches	qr{char\s+(?:un)?signed},
3641813087dSJoe Perches	qr{int\s+(?:(?:un)?signed\s+)?short\s},
3651813087dSJoe Perches	qr{int\s+short(?:\s+(?:un)?signed)},
3661813087dSJoe Perches	qr{short\s+int(?:\s+(?:un)?signed)},
3671813087dSJoe Perches	qr{(?:un)?signed\s+int\s+short},
3681813087dSJoe Perches	qr{short\s+(?:un)?signed},
3691813087dSJoe Perches	qr{long\s+int\s+(?:un)?signed},
3701813087dSJoe Perches	qr{int\s+long\s+(?:un)?signed},
3711813087dSJoe Perches	qr{long\s+(?:un)?signed\s+int},
3721813087dSJoe Perches	qr{int\s+(?:un)?signed\s+long},
3731813087dSJoe Perches	qr{int\s+(?:un)?signed},
3741813087dSJoe Perches	qr{int\s+long\s+long\s+(?:un)?signed},
3751813087dSJoe Perches	qr{long\s+long\s+int\s+(?:un)?signed},
3761813087dSJoe Perches	qr{long\s+long\s+(?:un)?signed\s+int},
3771813087dSJoe Perches	qr{long\s+long\s+(?:un)?signed},
3781813087dSJoe Perches	qr{long\s+(?:un)?signed},
3791813087dSJoe Perches);
3801813087dSJoe Perches
3818905a67cSAndy Whitcroftour @typeList = (
3828905a67cSAndy Whitcroft	qr{void},
3830c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?char},
3840c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?short\s+int},
3850c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?short},
3860c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?int},
3870c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?long\s+int},
3880c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?long\s+long\s+int},
3890c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?long\s+long},
3900c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?long},
3910c773d9dSJoe Perches	qr{(?:un)?signed},
3928905a67cSAndy Whitcroft	qr{float},
3938905a67cSAndy Whitcroft	qr{double},
3948905a67cSAndy Whitcroft	qr{bool},
3958905a67cSAndy Whitcroft	qr{struct\s+$Ident},
3968905a67cSAndy Whitcroft	qr{union\s+$Ident},
3978905a67cSAndy Whitcroft	qr{enum\s+$Ident},
3988905a67cSAndy Whitcroft	qr{${Ident}_t},
3998905a67cSAndy Whitcroft	qr{${Ident}_handler},
4008905a67cSAndy Whitcroft	qr{${Ident}_handler_fn},
4011813087dSJoe Perches	@typeListMisordered,
4028905a67cSAndy Whitcroft);
4038716de38SJoe Perchesour @typeListWithAttr = (
4048716de38SJoe Perches	@typeList,
4058716de38SJoe Perches	qr{struct\s+$InitAttribute\s+$Ident},
4068716de38SJoe Perches	qr{union\s+$InitAttribute\s+$Ident},
4078716de38SJoe Perches);
4088716de38SJoe Perches
409c45dcabdSAndy Whitcroftour @modifierList = (
410c45dcabdSAndy Whitcroft	qr{fastcall},
411c45dcabdSAndy Whitcroft);
4128905a67cSAndy Whitcroft
4132435880fSJoe Perchesour @mode_permission_funcs = (
4142435880fSJoe Perches	["module_param", 3],
4152435880fSJoe Perches	["module_param_(?:array|named|string)", 4],
4162435880fSJoe Perches	["module_param_array_named", 5],
4172435880fSJoe Perches	["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2],
4182435880fSJoe Perches	["proc_create(?:_data|)", 2],
4192435880fSJoe Perches	["(?:CLASS|DEVICE|SENSOR)_ATTR", 2],
4202435880fSJoe Perches);
4212435880fSJoe Perches
422515a235eSJoe Perches#Create a search pattern for all these functions to speed up a loop below
423515a235eSJoe Perchesour $mode_perms_search = "";
424515a235eSJoe Perchesforeach my $entry (@mode_permission_funcs) {
425515a235eSJoe Perches	$mode_perms_search .= '|' if ($mode_perms_search ne "");
426515a235eSJoe Perches	$mode_perms_search .= $entry->[0];
427515a235eSJoe Perches}
428515a235eSJoe Perches
4297840a94cSWolfram Sangour $allowed_asm_includes = qr{(?x:
4307840a94cSWolfram Sang	irq|
431cdcee686SSergey Ryazanov	memory|
432cdcee686SSergey Ryazanov	time|
433cdcee686SSergey Ryazanov	reboot
4347840a94cSWolfram Sang)};
4357840a94cSWolfram Sang# memory.h: ARM has a custom one
4367840a94cSWolfram Sang
4378905a67cSAndy Whitcroftsub build_types {
438d2172eb5SAndy Whitcroft	my $mods = "(?x:  \n" . join("|\n  ", @modifierList) . "\n)";
439d2172eb5SAndy Whitcroft	my $all = "(?x:  \n" . join("|\n  ", @typeList) . "\n)";
4401813087dSJoe Perches	my $Misordered = "(?x:  \n" . join("|\n  ", @typeListMisordered) . "\n)";
4418716de38SJoe Perches	my $allWithAttr = "(?x:  \n" . join("|\n  ", @typeListWithAttr) . "\n)";
442c8cb2ca3SAndy Whitcroft	$Modifier	= qr{(?:$Attribute|$Sparse|$mods)};
4438905a67cSAndy Whitcroft	$NonptrType	= qr{
444d2172eb5SAndy Whitcroft			(?:$Modifier\s+|const\s+)*
445cf655043SAndy Whitcroft			(?:
4466b48db24SAndy Whitcroft				(?:typeof|__typeof__)\s*\([^\)]*\)|
4478ed22cadSAndy Whitcroft				(?:$typeTypedefs\b)|
448c45dcabdSAndy Whitcroft				(?:${all}\b)
449cf655043SAndy Whitcroft			)
450c8cb2ca3SAndy Whitcroft			(?:\s+$Modifier|\s+const)*
4518905a67cSAndy Whitcroft		  }x;
4521813087dSJoe Perches	$NonptrTypeMisordered	= qr{
4531813087dSJoe Perches			(?:$Modifier\s+|const\s+)*
4541813087dSJoe Perches			(?:
4551813087dSJoe Perches				(?:${Misordered}\b)
4561813087dSJoe Perches			)
4571813087dSJoe Perches			(?:\s+$Modifier|\s+const)*
4581813087dSJoe Perches		  }x;
4598716de38SJoe Perches	$NonptrTypeWithAttr	= qr{
4608716de38SJoe Perches			(?:$Modifier\s+|const\s+)*
4618716de38SJoe Perches			(?:
4628716de38SJoe Perches				(?:typeof|__typeof__)\s*\([^\)]*\)|
4638716de38SJoe Perches				(?:$typeTypedefs\b)|
4648716de38SJoe Perches				(?:${allWithAttr}\b)
4658716de38SJoe Perches			)
4668716de38SJoe Perches			(?:\s+$Modifier|\s+const)*
4678716de38SJoe Perches		  }x;
4688905a67cSAndy Whitcroft	$Type	= qr{
469c45dcabdSAndy Whitcroft			$NonptrType
4701574a29fSJoe Perches			(?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
471c8cb2ca3SAndy Whitcroft			(?:\s+$Inline|\s+$Modifier)*
4728905a67cSAndy Whitcroft		  }x;
4731813087dSJoe Perches	$TypeMisordered	= qr{
4741813087dSJoe Perches			$NonptrTypeMisordered
4751813087dSJoe Perches			(?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
4761813087dSJoe Perches			(?:\s+$Inline|\s+$Modifier)*
4771813087dSJoe Perches		  }x;
47891cb5195SJoe Perches	$Declare	= qr{(?:$Storage\s+(?:$Inline\s+)?)?$Type};
4791813087dSJoe Perches	$DeclareMisordered	= qr{(?:$Storage\s+(?:$Inline\s+)?)?$TypeMisordered};
4808905a67cSAndy Whitcroft}
4818905a67cSAndy Whitcroftbuild_types();
4826c72ffaaSAndy Whitcroft
4837d2367afSJoe Perchesour $Typecast	= qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
484d1fe9c09SJoe Perches
485d1fe9c09SJoe Perches# Using $balanced_parens, $LvalOrFunc, or $FuncArg
486d1fe9c09SJoe Perches# requires at least perl version v5.10.0
487d1fe9c09SJoe Perches# Any use must be runtime checked with $^V
488d1fe9c09SJoe Perches
489d1fe9c09SJoe Perchesour $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
4902435880fSJoe Perchesour $LvalOrFunc	= qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*};
491d7c76ba7SJoe Perchesour $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant)};
4927d2367afSJoe Perches
493f8422308SJoe Perchesour $declaration_macros = qr{(?x:
494f8422308SJoe Perches	(?:$Storage\s+)?(?:[A-Z_][A-Z0-9]*_){0,2}(?:DEFINE|DECLARE)(?:_[A-Z0-9]+){1,2}\s*\(|
495f8422308SJoe Perches	(?:$Storage\s+)?LIST_HEAD\s*\(|
496f8422308SJoe Perches	(?:$Storage\s+)?${Type}\s+uninitialized_var\s*\(
497f8422308SJoe Perches)};
498f8422308SJoe Perches
4997d2367afSJoe Perchessub deparenthesize {
5007d2367afSJoe Perches	my ($string) = @_;
5017d2367afSJoe Perches	return "" if (!defined($string));
5025b9553abSJoe Perches
5035b9553abSJoe Perches	while ($string =~ /^\s*\(.*\)\s*$/) {
5045b9553abSJoe Perches		$string =~ s@^\s*\(\s*@@;
5055b9553abSJoe Perches		$string =~ s@\s*\)\s*$@@;
5065b9553abSJoe Perches	}
5075b9553abSJoe Perches
5087d2367afSJoe Perches	$string =~ s@\s+@ @g;
5095b9553abSJoe Perches
5107d2367afSJoe Perches	return $string;
5117d2367afSJoe Perches}
5127d2367afSJoe Perches
5133445686aSJoe Perchessub seed_camelcase_file {
5143445686aSJoe Perches	my ($file) = @_;
5153445686aSJoe Perches
5163445686aSJoe Perches	return if (!(-f $file));
5173445686aSJoe Perches
5183445686aSJoe Perches	local $/;
5193445686aSJoe Perches
5203445686aSJoe Perches	open(my $include_file, '<', "$file")
5213445686aSJoe Perches	    or warn "$P: Can't read '$file' $!\n";
5223445686aSJoe Perches	my $text = <$include_file>;
5233445686aSJoe Perches	close($include_file);
5243445686aSJoe Perches
5253445686aSJoe Perches	my @lines = split('\n', $text);
5263445686aSJoe Perches
5273445686aSJoe Perches	foreach my $line (@lines) {
5283445686aSJoe Perches		next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/);
5293445686aSJoe Perches		if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) {
5303445686aSJoe Perches			$camelcase{$1} = 1;
53111ea516aSJoe Perches		} elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) {
53211ea516aSJoe Perches			$camelcase{$1} = 1;
53311ea516aSJoe Perches		} elsif ($line =~ /^\s*(?:union|struct|enum)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[;\{]/) {
5343445686aSJoe Perches			$camelcase{$1} = 1;
5353445686aSJoe Perches		}
5363445686aSJoe Perches	}
5373445686aSJoe Perches}
5383445686aSJoe Perches
5393445686aSJoe Perchesmy $camelcase_seeded = 0;
5403445686aSJoe Perchessub seed_camelcase_includes {
5413445686aSJoe Perches	return if ($camelcase_seeded);
5423445686aSJoe Perches
5433445686aSJoe Perches	my $files;
544c707a81dSJoe Perches	my $camelcase_cache = "";
545c707a81dSJoe Perches	my @include_files = ();
546c707a81dSJoe Perches
547c707a81dSJoe Perches	$camelcase_seeded = 1;
548351b2a1fSJoe Perches
5493645e328SRichard Genoud	if (-e ".git") {
550351b2a1fSJoe Perches		my $git_last_include_commit = `git log --no-merges --pretty=format:"%h%n" -1 -- include`;
551351b2a1fSJoe Perches		chomp $git_last_include_commit;
552c707a81dSJoe Perches		$camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit";
553c707a81dSJoe Perches	} else {
554c707a81dSJoe Perches		my $last_mod_date = 0;
555c707a81dSJoe Perches		$files = `find $root/include -name "*.h"`;
556c707a81dSJoe Perches		@include_files = split('\n', $files);
557c707a81dSJoe Perches		foreach my $file (@include_files) {
558c707a81dSJoe Perches			my $date = POSIX::strftime("%Y%m%d%H%M",
559c707a81dSJoe Perches						   localtime((stat $file)[9]));
560c707a81dSJoe Perches			$last_mod_date = $date if ($last_mod_date < $date);
561c707a81dSJoe Perches		}
562c707a81dSJoe Perches		$camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date";
563c707a81dSJoe Perches	}
564c707a81dSJoe Perches
565c707a81dSJoe Perches	if ($camelcase_cache ne "" && -f $camelcase_cache) {
566c707a81dSJoe Perches		open(my $camelcase_file, '<', "$camelcase_cache")
567c707a81dSJoe Perches		    or warn "$P: Can't read '$camelcase_cache' $!\n";
568351b2a1fSJoe Perches		while (<$camelcase_file>) {
569351b2a1fSJoe Perches			chomp;
570351b2a1fSJoe Perches			$camelcase{$_} = 1;
571351b2a1fSJoe Perches		}
572351b2a1fSJoe Perches		close($camelcase_file);
573351b2a1fSJoe Perches
574351b2a1fSJoe Perches		return;
575351b2a1fSJoe Perches	}
576c707a81dSJoe Perches
5773645e328SRichard Genoud	if (-e ".git") {
578c707a81dSJoe Perches		$files = `git ls-files "include/*.h"`;
579c707a81dSJoe Perches		@include_files = split('\n', $files);
5803445686aSJoe Perches	}
581c707a81dSJoe Perches
5823445686aSJoe Perches	foreach my $file (@include_files) {
5833445686aSJoe Perches		seed_camelcase_file($file);
5843445686aSJoe Perches	}
585351b2a1fSJoe Perches
586c707a81dSJoe Perches	if ($camelcase_cache ne "") {
587351b2a1fSJoe Perches		unlink glob ".checkpatch-camelcase.*";
588c707a81dSJoe Perches		open(my $camelcase_file, '>', "$camelcase_cache")
589c707a81dSJoe Perches		    or warn "$P: Can't write '$camelcase_cache' $!\n";
590351b2a1fSJoe Perches		foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) {
591351b2a1fSJoe Perches			print $camelcase_file ("$_\n");
592351b2a1fSJoe Perches		}
593351b2a1fSJoe Perches		close($camelcase_file);
594351b2a1fSJoe Perches	}
5953445686aSJoe Perches}
5963445686aSJoe Perches
597d311cd44SJoe Perchessub git_commit_info {
598d311cd44SJoe Perches	my ($commit, $id, $desc) = @_;
599d311cd44SJoe Perches
600d311cd44SJoe Perches	return ($id, $desc) if ((which("git") eq "") || !(-e ".git"));
601d311cd44SJoe Perches
602d311cd44SJoe Perches	my $output = `git log --no-color --format='%H %s' -1 $commit 2>&1`;
603d311cd44SJoe Perches	$output =~ s/^\s*//gm;
604d311cd44SJoe Perches	my @lines = split("\n", $output);
605d311cd44SJoe Perches
606d311cd44SJoe Perches	if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous\./) {
607d311cd44SJoe Perches# Maybe one day convert this block of bash into something that returns
608d311cd44SJoe Perches# all matching commit ids, but it's very slow...
609d311cd44SJoe Perches#
610d311cd44SJoe Perches#		echo "checking commits $1..."
611d311cd44SJoe Perches#		git rev-list --remotes | grep -i "^$1" |
612d311cd44SJoe Perches#		while read line ; do
613d311cd44SJoe Perches#		    git log --format='%H %s' -1 $line |
614d311cd44SJoe Perches#		    echo "commit $(cut -c 1-12,41-)"
615d311cd44SJoe Perches#		done
616d311cd44SJoe Perches	} elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./) {
617d311cd44SJoe Perches	} else {
618d311cd44SJoe Perches		$id = substr($lines[0], 0, 12);
619d311cd44SJoe Perches		$desc = substr($lines[0], 41);
620d311cd44SJoe Perches	}
621d311cd44SJoe Perches
622d311cd44SJoe Perches	return ($id, $desc);
623d311cd44SJoe Perches}
624d311cd44SJoe Perches
6256c72ffaaSAndy Whitcroft$chk_signoff = 0 if ($file);
6260a920b5bSAndy Whitcroft
62700df344fSAndy Whitcroftmy @rawlines = ();
628c2fdda0dSAndy Whitcroftmy @lines = ();
6293705ce5bSJoe Perchesmy @fixed = ();
630d752fcc8SJoe Perchesmy @fixed_inserted = ();
631d752fcc8SJoe Perchesmy @fixed_deleted = ();
632194f66fcSJoe Perchesmy $fixlinenr = -1;
633194f66fcSJoe Perches
634c2fdda0dSAndy Whitcroftmy $vname;
6356c72ffaaSAndy Whitcroftfor my $filename (@ARGV) {
63621caa13cSAndy Whitcroft	my $FILE;
6376c72ffaaSAndy Whitcroft	if ($file) {
63821caa13cSAndy Whitcroft		open($FILE, '-|', "diff -u /dev/null $filename") ||
6396c72ffaaSAndy Whitcroft			die "$P: $filename: diff failed - $!\n";
64021caa13cSAndy Whitcroft	} elsif ($filename eq '-') {
64121caa13cSAndy Whitcroft		open($FILE, '<&STDIN');
6426c72ffaaSAndy Whitcroft	} else {
64321caa13cSAndy Whitcroft		open($FILE, '<', "$filename") ||
6446c72ffaaSAndy Whitcroft			die "$P: $filename: open failed - $!\n";
6456c72ffaaSAndy Whitcroft	}
646c2fdda0dSAndy Whitcroft	if ($filename eq '-') {
647c2fdda0dSAndy Whitcroft		$vname = 'Your patch';
648c2fdda0dSAndy Whitcroft	} else {
649c2fdda0dSAndy Whitcroft		$vname = $filename;
650c2fdda0dSAndy Whitcroft	}
65121caa13cSAndy Whitcroft	while (<$FILE>) {
6520a920b5bSAndy Whitcroft		chomp;
65300df344fSAndy Whitcroft		push(@rawlines, $_);
6546c72ffaaSAndy Whitcroft	}
65521caa13cSAndy Whitcroft	close($FILE);
656c2fdda0dSAndy Whitcroft	if (!process($filename)) {
6570a920b5bSAndy Whitcroft		$exit = 1;
6580a920b5bSAndy Whitcroft	}
65900df344fSAndy Whitcroft	@rawlines = ();
66013214adfSAndy Whitcroft	@lines = ();
6613705ce5bSJoe Perches	@fixed = ();
662d752fcc8SJoe Perches	@fixed_inserted = ();
663d752fcc8SJoe Perches	@fixed_deleted = ();
664194f66fcSJoe Perches	$fixlinenr = -1;
6650a920b5bSAndy Whitcroft}
6660a920b5bSAndy Whitcroft
6670a920b5bSAndy Whitcroftexit($exit);
6680a920b5bSAndy Whitcroft
6690a920b5bSAndy Whitcroftsub top_of_kernel_tree {
6706c72ffaaSAndy Whitcroft	my ($root) = @_;
6716c72ffaaSAndy Whitcroft
6726c72ffaaSAndy Whitcroft	my @tree_check = (
6736c72ffaaSAndy Whitcroft		"COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
6746c72ffaaSAndy Whitcroft		"README", "Documentation", "arch", "include", "drivers",
6756c72ffaaSAndy Whitcroft		"fs", "init", "ipc", "kernel", "lib", "scripts",
6766c72ffaaSAndy Whitcroft	);
6776c72ffaaSAndy Whitcroft
6786c72ffaaSAndy Whitcroft	foreach my $check (@tree_check) {
6796c72ffaaSAndy Whitcroft		if (! -e $root . '/' . $check) {
6800a920b5bSAndy Whitcroft			return 0;
6810a920b5bSAndy Whitcroft		}
6826c72ffaaSAndy Whitcroft	}
6836c72ffaaSAndy Whitcroft	return 1;
6846c72ffaaSAndy Whitcroft}
6850a920b5bSAndy Whitcroft
68620112475SJoe Perchessub parse_email {
68720112475SJoe Perches	my ($formatted_email) = @_;
68820112475SJoe Perches
68920112475SJoe Perches	my $name = "";
69020112475SJoe Perches	my $address = "";
69120112475SJoe Perches	my $comment = "";
69220112475SJoe Perches
69320112475SJoe Perches	if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
69420112475SJoe Perches		$name = $1;
69520112475SJoe Perches		$address = $2;
69620112475SJoe Perches		$comment = $3 if defined $3;
69720112475SJoe Perches	} elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
69820112475SJoe Perches		$address = $1;
69920112475SJoe Perches		$comment = $2 if defined $2;
70020112475SJoe Perches	} elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
70120112475SJoe Perches		$address = $1;
70220112475SJoe Perches		$comment = $2 if defined $2;
70320112475SJoe Perches		$formatted_email =~ s/$address.*$//;
70420112475SJoe Perches		$name = $formatted_email;
7053705ce5bSJoe Perches		$name = trim($name);
70620112475SJoe Perches		$name =~ s/^\"|\"$//g;
70720112475SJoe Perches		# If there's a name left after stripping spaces and
70820112475SJoe Perches		# leading quotes, and the address doesn't have both
70920112475SJoe Perches		# leading and trailing angle brackets, the address
71020112475SJoe Perches		# is invalid. ie:
71120112475SJoe Perches		#   "joe smith [email protected]" bad
71220112475SJoe Perches		#   "joe smith <[email protected]" bad
71320112475SJoe Perches		if ($name ne "" && $address !~ /^<[^>]+>$/) {
71420112475SJoe Perches			$name = "";
71520112475SJoe Perches			$address = "";
71620112475SJoe Perches			$comment = "";
71720112475SJoe Perches		}
71820112475SJoe Perches	}
71920112475SJoe Perches
7203705ce5bSJoe Perches	$name = trim($name);
72120112475SJoe Perches	$name =~ s/^\"|\"$//g;
7223705ce5bSJoe Perches	$address = trim($address);
72320112475SJoe Perches	$address =~ s/^\<|\>$//g;
72420112475SJoe Perches
72520112475SJoe Perches	if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
72620112475SJoe Perches		$name =~ s/(?<!\\)"/\\"/g; ##escape quotes
72720112475SJoe Perches		$name = "\"$name\"";
72820112475SJoe Perches	}
72920112475SJoe Perches
73020112475SJoe Perches	return ($name, $address, $comment);
73120112475SJoe Perches}
73220112475SJoe Perches
73320112475SJoe Perchessub format_email {
73420112475SJoe Perches	my ($name, $address) = @_;
73520112475SJoe Perches
73620112475SJoe Perches	my $formatted_email;
73720112475SJoe Perches
7383705ce5bSJoe Perches	$name = trim($name);
73920112475SJoe Perches	$name =~ s/^\"|\"$//g;
7403705ce5bSJoe Perches	$address = trim($address);
74120112475SJoe Perches
74220112475SJoe Perches	if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
74320112475SJoe Perches		$name =~ s/(?<!\\)"/\\"/g; ##escape quotes
74420112475SJoe Perches		$name = "\"$name\"";
74520112475SJoe Perches	}
74620112475SJoe Perches
74720112475SJoe Perches	if ("$name" eq "") {
74820112475SJoe Perches		$formatted_email = "$address";
74920112475SJoe Perches	} else {
75020112475SJoe Perches		$formatted_email = "$name <$address>";
75120112475SJoe Perches	}
75220112475SJoe Perches
75320112475SJoe Perches	return $formatted_email;
75420112475SJoe Perches}
75520112475SJoe Perches
756d311cd44SJoe Perchessub which {
757d311cd44SJoe Perches	my ($bin) = @_;
758d311cd44SJoe Perches
759d311cd44SJoe Perches	foreach my $path (split(/:/, $ENV{PATH})) {
760d311cd44SJoe Perches		if (-e "$path/$bin") {
761d311cd44SJoe Perches			return "$path/$bin";
762d311cd44SJoe Perches		}
763d311cd44SJoe Perches	}
764d311cd44SJoe Perches
765d311cd44SJoe Perches	return "";
766d311cd44SJoe Perches}
767d311cd44SJoe Perches
768000d1cc1SJoe Perchessub which_conf {
769000d1cc1SJoe Perches	my ($conf) = @_;
770000d1cc1SJoe Perches
771000d1cc1SJoe Perches	foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
772000d1cc1SJoe Perches		if (-e "$path/$conf") {
773000d1cc1SJoe Perches			return "$path/$conf";
774000d1cc1SJoe Perches		}
775000d1cc1SJoe Perches	}
776000d1cc1SJoe Perches
777000d1cc1SJoe Perches	return "";
778000d1cc1SJoe Perches}
779000d1cc1SJoe Perches
7800a920b5bSAndy Whitcroftsub expand_tabs {
7810a920b5bSAndy Whitcroft	my ($str) = @_;
7820a920b5bSAndy Whitcroft
7830a920b5bSAndy Whitcroft	my $res = '';
7840a920b5bSAndy Whitcroft	my $n = 0;
7850a920b5bSAndy Whitcroft	for my $c (split(//, $str)) {
7860a920b5bSAndy Whitcroft		if ($c eq "\t") {
7870a920b5bSAndy Whitcroft			$res .= ' ';
7880a920b5bSAndy Whitcroft			$n++;
7890a920b5bSAndy Whitcroft			for (; ($n % 8) != 0; $n++) {
7900a920b5bSAndy Whitcroft				$res .= ' ';
7910a920b5bSAndy Whitcroft			}
7920a920b5bSAndy Whitcroft			next;
7930a920b5bSAndy Whitcroft		}
7940a920b5bSAndy Whitcroft		$res .= $c;
7950a920b5bSAndy Whitcroft		$n++;
7960a920b5bSAndy Whitcroft	}
7970a920b5bSAndy Whitcroft
7980a920b5bSAndy Whitcroft	return $res;
7990a920b5bSAndy Whitcroft}
8006c72ffaaSAndy Whitcroftsub copy_spacing {
801773647a0SAndy Whitcroft	(my $res = shift) =~ tr/\t/ /c;
8026c72ffaaSAndy Whitcroft	return $res;
8036c72ffaaSAndy Whitcroft}
8040a920b5bSAndy Whitcroft
8054a0df2efSAndy Whitcroftsub line_stats {
8064a0df2efSAndy Whitcroft	my ($line) = @_;
8074a0df2efSAndy Whitcroft
8084a0df2efSAndy Whitcroft	# Drop the diff line leader and expand tabs
8094a0df2efSAndy Whitcroft	$line =~ s/^.//;
8104a0df2efSAndy Whitcroft	$line = expand_tabs($line);
8114a0df2efSAndy Whitcroft
8124a0df2efSAndy Whitcroft	# Pick the indent from the front of the line.
8134a0df2efSAndy Whitcroft	my ($white) = ($line =~ /^(\s*)/);
8144a0df2efSAndy Whitcroft
8154a0df2efSAndy Whitcroft	return (length($line), length($white));
8164a0df2efSAndy Whitcroft}
8174a0df2efSAndy Whitcroft
818773647a0SAndy Whitcroftmy $sanitise_quote = '';
819773647a0SAndy Whitcroft
820773647a0SAndy Whitcroftsub sanitise_line_reset {
821773647a0SAndy Whitcroft	my ($in_comment) = @_;
822773647a0SAndy Whitcroft
823773647a0SAndy Whitcroft	if ($in_comment) {
824773647a0SAndy Whitcroft		$sanitise_quote = '*/';
825773647a0SAndy Whitcroft	} else {
826773647a0SAndy Whitcroft		$sanitise_quote = '';
827773647a0SAndy Whitcroft	}
828773647a0SAndy Whitcroft}
82900df344fSAndy Whitcroftsub sanitise_line {
83000df344fSAndy Whitcroft	my ($line) = @_;
83100df344fSAndy Whitcroft
83200df344fSAndy Whitcroft	my $res = '';
83300df344fSAndy Whitcroft	my $l = '';
83400df344fSAndy Whitcroft
835c2fdda0dSAndy Whitcroft	my $qlen = 0;
836773647a0SAndy Whitcroft	my $off = 0;
837773647a0SAndy Whitcroft	my $c;
83800df344fSAndy Whitcroft
839773647a0SAndy Whitcroft	# Always copy over the diff marker.
840773647a0SAndy Whitcroft	$res = substr($line, 0, 1);
841773647a0SAndy Whitcroft
842773647a0SAndy Whitcroft	for ($off = 1; $off < length($line); $off++) {
843773647a0SAndy Whitcroft		$c = substr($line, $off, 1);
844773647a0SAndy Whitcroft
845773647a0SAndy Whitcroft		# Comments we are wacking completly including the begin
846773647a0SAndy Whitcroft		# and end, all to $;.
847773647a0SAndy Whitcroft		if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
848773647a0SAndy Whitcroft			$sanitise_quote = '*/';
849773647a0SAndy Whitcroft
850773647a0SAndy Whitcroft			substr($res, $off, 2, "$;$;");
851773647a0SAndy Whitcroft			$off++;
85200df344fSAndy Whitcroft			next;
853773647a0SAndy Whitcroft		}
85481bc0e02SAndy Whitcroft		if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
855773647a0SAndy Whitcroft			$sanitise_quote = '';
856773647a0SAndy Whitcroft			substr($res, $off, 2, "$;$;");
857773647a0SAndy Whitcroft			$off++;
858773647a0SAndy Whitcroft			next;
859773647a0SAndy Whitcroft		}
860113f04a8SDaniel Walker		if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
861113f04a8SDaniel Walker			$sanitise_quote = '//';
862113f04a8SDaniel Walker
863113f04a8SDaniel Walker			substr($res, $off, 2, $sanitise_quote);
864113f04a8SDaniel Walker			$off++;
865113f04a8SDaniel Walker			next;
866113f04a8SDaniel Walker		}
867773647a0SAndy Whitcroft
868773647a0SAndy Whitcroft		# A \ in a string means ignore the next character.
869773647a0SAndy Whitcroft		if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
870773647a0SAndy Whitcroft		    $c eq "\\") {
871773647a0SAndy Whitcroft			substr($res, $off, 2, 'XX');
872773647a0SAndy Whitcroft			$off++;
873773647a0SAndy Whitcroft			next;
874773647a0SAndy Whitcroft		}
875773647a0SAndy Whitcroft		# Regular quotes.
876773647a0SAndy Whitcroft		if ($c eq "'" || $c eq '"') {
877773647a0SAndy Whitcroft			if ($sanitise_quote eq '') {
878773647a0SAndy Whitcroft				$sanitise_quote = $c;
879773647a0SAndy Whitcroft
880773647a0SAndy Whitcroft				substr($res, $off, 1, $c);
881773647a0SAndy Whitcroft				next;
882773647a0SAndy Whitcroft			} elsif ($sanitise_quote eq $c) {
883773647a0SAndy Whitcroft				$sanitise_quote = '';
88400df344fSAndy Whitcroft			}
88500df344fSAndy Whitcroft		}
886773647a0SAndy Whitcroft
887fae17daeSAndy Whitcroft		#print "c<$c> SQ<$sanitise_quote>\n";
888773647a0SAndy Whitcroft		if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
889773647a0SAndy Whitcroft			substr($res, $off, 1, $;);
890113f04a8SDaniel Walker		} elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
891113f04a8SDaniel Walker			substr($res, $off, 1, $;);
892773647a0SAndy Whitcroft		} elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
893773647a0SAndy Whitcroft			substr($res, $off, 1, 'X');
89400df344fSAndy Whitcroft		} else {
895773647a0SAndy Whitcroft			substr($res, $off, 1, $c);
89600df344fSAndy Whitcroft		}
897c2fdda0dSAndy Whitcroft	}
898c2fdda0dSAndy Whitcroft
899113f04a8SDaniel Walker	if ($sanitise_quote eq '//') {
900113f04a8SDaniel Walker		$sanitise_quote = '';
901113f04a8SDaniel Walker	}
902113f04a8SDaniel Walker
903c2fdda0dSAndy Whitcroft	# The pathname on a #include may be surrounded by '<' and '>'.
904c45dcabdSAndy Whitcroft	if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
905c2fdda0dSAndy Whitcroft		my $clean = 'X' x length($1);
906c2fdda0dSAndy Whitcroft		$res =~ s@\<.*\>@<$clean>@;
907c2fdda0dSAndy Whitcroft
908c2fdda0dSAndy Whitcroft	# The whole of a #error is a string.
909c45dcabdSAndy Whitcroft	} elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
910c2fdda0dSAndy Whitcroft		my $clean = 'X' x length($1);
911c45dcabdSAndy Whitcroft		$res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
912c2fdda0dSAndy Whitcroft	}
913c2fdda0dSAndy Whitcroft
91400df344fSAndy Whitcroft	return $res;
91500df344fSAndy Whitcroft}
91600df344fSAndy Whitcroft
917a6962d72SJoe Perchessub get_quoted_string {
918a6962d72SJoe Perches	my ($line, $rawline) = @_;
919a6962d72SJoe Perches
920a6962d72SJoe Perches	return "" if ($line !~ m/(\"[X]+\")/g);
921a6962d72SJoe Perches	return substr($rawline, $-[0], $+[0] - $-[0]);
922a6962d72SJoe Perches}
923a6962d72SJoe Perches
9248905a67cSAndy Whitcroftsub ctx_statement_block {
9258905a67cSAndy Whitcroft	my ($linenr, $remain, $off) = @_;
9268905a67cSAndy Whitcroft	my $line = $linenr - 1;
9278905a67cSAndy Whitcroft	my $blk = '';
9288905a67cSAndy Whitcroft	my $soff = $off;
9298905a67cSAndy Whitcroft	my $coff = $off - 1;
930773647a0SAndy Whitcroft	my $coff_set = 0;
9318905a67cSAndy Whitcroft
93213214adfSAndy Whitcroft	my $loff = 0;
93313214adfSAndy Whitcroft
9348905a67cSAndy Whitcroft	my $type = '';
9358905a67cSAndy Whitcroft	my $level = 0;
936a2750645SAndy Whitcroft	my @stack = ();
937cf655043SAndy Whitcroft	my $p;
9388905a67cSAndy Whitcroft	my $c;
9398905a67cSAndy Whitcroft	my $len = 0;
94013214adfSAndy Whitcroft
94113214adfSAndy Whitcroft	my $remainder;
9428905a67cSAndy Whitcroft	while (1) {
943a2750645SAndy Whitcroft		@stack = (['', 0]) if ($#stack == -1);
944a2750645SAndy Whitcroft
945773647a0SAndy Whitcroft		#warn "CSB: blk<$blk> remain<$remain>\n";
9468905a67cSAndy Whitcroft		# If we are about to drop off the end, pull in more
9478905a67cSAndy Whitcroft		# context.
9488905a67cSAndy Whitcroft		if ($off >= $len) {
9498905a67cSAndy Whitcroft			for (; $remain > 0; $line++) {
950dea33496SAndy Whitcroft				last if (!defined $lines[$line]);
951c2fdda0dSAndy Whitcroft				next if ($lines[$line] =~ /^-/);
9528905a67cSAndy Whitcroft				$remain--;
95313214adfSAndy Whitcroft				$loff = $len;
954c2fdda0dSAndy Whitcroft				$blk .= $lines[$line] . "\n";
9558905a67cSAndy Whitcroft				$len = length($blk);
9568905a67cSAndy Whitcroft				$line++;
9578905a67cSAndy Whitcroft				last;
9588905a67cSAndy Whitcroft			}
9598905a67cSAndy Whitcroft			# Bail if there is no further context.
9608905a67cSAndy Whitcroft			#warn "CSB: blk<$blk> off<$off> len<$len>\n";
96113214adfSAndy Whitcroft			if ($off >= $len) {
9628905a67cSAndy Whitcroft				last;
9638905a67cSAndy Whitcroft			}
964f74bd194SAndy Whitcroft			if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
965f74bd194SAndy Whitcroft				$level++;
966f74bd194SAndy Whitcroft				$type = '#';
967f74bd194SAndy Whitcroft			}
9688905a67cSAndy Whitcroft		}
969cf655043SAndy Whitcroft		$p = $c;
9708905a67cSAndy Whitcroft		$c = substr($blk, $off, 1);
97113214adfSAndy Whitcroft		$remainder = substr($blk, $off);
9728905a67cSAndy Whitcroft
973773647a0SAndy Whitcroft		#warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
9744635f4fbSAndy Whitcroft
9754635f4fbSAndy Whitcroft		# Handle nested #if/#else.
9764635f4fbSAndy Whitcroft		if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
9774635f4fbSAndy Whitcroft			push(@stack, [ $type, $level ]);
9784635f4fbSAndy Whitcroft		} elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
9794635f4fbSAndy Whitcroft			($type, $level) = @{$stack[$#stack - 1]};
9804635f4fbSAndy Whitcroft		} elsif ($remainder =~ /^#\s*endif\b/) {
9814635f4fbSAndy Whitcroft			($type, $level) = @{pop(@stack)};
9824635f4fbSAndy Whitcroft		}
9834635f4fbSAndy Whitcroft
9848905a67cSAndy Whitcroft		# Statement ends at the ';' or a close '}' at the
9858905a67cSAndy Whitcroft		# outermost level.
9868905a67cSAndy Whitcroft		if ($level == 0 && $c eq ';') {
9878905a67cSAndy Whitcroft			last;
9888905a67cSAndy Whitcroft		}
9898905a67cSAndy Whitcroft
99013214adfSAndy Whitcroft		# An else is really a conditional as long as its not else if
991773647a0SAndy Whitcroft		if ($level == 0 && $coff_set == 0 &&
992773647a0SAndy Whitcroft				(!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
993773647a0SAndy Whitcroft				$remainder =~ /^(else)(?:\s|{)/ &&
994773647a0SAndy Whitcroft				$remainder !~ /^else\s+if\b/) {
995773647a0SAndy Whitcroft			$coff = $off + length($1) - 1;
996773647a0SAndy Whitcroft			$coff_set = 1;
997773647a0SAndy Whitcroft			#warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
998773647a0SAndy Whitcroft			#warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
99913214adfSAndy Whitcroft		}
100013214adfSAndy Whitcroft
10018905a67cSAndy Whitcroft		if (($type eq '' || $type eq '(') && $c eq '(') {
10028905a67cSAndy Whitcroft			$level++;
10038905a67cSAndy Whitcroft			$type = '(';
10048905a67cSAndy Whitcroft		}
10058905a67cSAndy Whitcroft		if ($type eq '(' && $c eq ')') {
10068905a67cSAndy Whitcroft			$level--;
10078905a67cSAndy Whitcroft			$type = ($level != 0)? '(' : '';
10088905a67cSAndy Whitcroft
10098905a67cSAndy Whitcroft			if ($level == 0 && $coff < $soff) {
10108905a67cSAndy Whitcroft				$coff = $off;
1011773647a0SAndy Whitcroft				$coff_set = 1;
1012773647a0SAndy Whitcroft				#warn "CSB: mark coff<$coff>\n";
10138905a67cSAndy Whitcroft			}
10148905a67cSAndy Whitcroft		}
10158905a67cSAndy Whitcroft		if (($type eq '' || $type eq '{') && $c eq '{') {
10168905a67cSAndy Whitcroft			$level++;
10178905a67cSAndy Whitcroft			$type = '{';
10188905a67cSAndy Whitcroft		}
10198905a67cSAndy Whitcroft		if ($type eq '{' && $c eq '}') {
10208905a67cSAndy Whitcroft			$level--;
10218905a67cSAndy Whitcroft			$type = ($level != 0)? '{' : '';
10228905a67cSAndy Whitcroft
10238905a67cSAndy Whitcroft			if ($level == 0) {
1024b998e001SPatrick Pannuto				if (substr($blk, $off + 1, 1) eq ';') {
1025b998e001SPatrick Pannuto					$off++;
1026b998e001SPatrick Pannuto				}
10278905a67cSAndy Whitcroft				last;
10288905a67cSAndy Whitcroft			}
10298905a67cSAndy Whitcroft		}
1030f74bd194SAndy Whitcroft		# Preprocessor commands end at the newline unless escaped.
1031f74bd194SAndy Whitcroft		if ($type eq '#' && $c eq "\n" && $p ne "\\") {
1032f74bd194SAndy Whitcroft			$level--;
1033f74bd194SAndy Whitcroft			$type = '';
1034f74bd194SAndy Whitcroft			$off++;
1035f74bd194SAndy Whitcroft			last;
1036f74bd194SAndy Whitcroft		}
10378905a67cSAndy Whitcroft		$off++;
10388905a67cSAndy Whitcroft	}
1039a3bb97a7SAndy Whitcroft	# We are truly at the end, so shuffle to the next line.
104013214adfSAndy Whitcroft	if ($off == $len) {
1041a3bb97a7SAndy Whitcroft		$loff = $len + 1;
104213214adfSAndy Whitcroft		$line++;
104313214adfSAndy Whitcroft		$remain--;
104413214adfSAndy Whitcroft	}
10458905a67cSAndy Whitcroft
10468905a67cSAndy Whitcroft	my $statement = substr($blk, $soff, $off - $soff + 1);
10478905a67cSAndy Whitcroft	my $condition = substr($blk, $soff, $coff - $soff + 1);
10488905a67cSAndy Whitcroft
10498905a67cSAndy Whitcroft	#warn "STATEMENT<$statement>\n";
10508905a67cSAndy Whitcroft	#warn "CONDITION<$condition>\n";
10518905a67cSAndy Whitcroft
1052773647a0SAndy Whitcroft	#print "coff<$coff> soff<$off> loff<$loff>\n";
105313214adfSAndy Whitcroft
105413214adfSAndy Whitcroft	return ($statement, $condition,
105513214adfSAndy Whitcroft			$line, $remain + 1, $off - $loff + 1, $level);
105613214adfSAndy Whitcroft}
105713214adfSAndy Whitcroft
1058cf655043SAndy Whitcroftsub statement_lines {
1059cf655043SAndy Whitcroft	my ($stmt) = @_;
1060cf655043SAndy Whitcroft
1061cf655043SAndy Whitcroft	# Strip the diff line prefixes and rip blank lines at start and end.
1062cf655043SAndy Whitcroft	$stmt =~ s/(^|\n)./$1/g;
1063cf655043SAndy Whitcroft	$stmt =~ s/^\s*//;
1064cf655043SAndy Whitcroft	$stmt =~ s/\s*$//;
1065cf655043SAndy Whitcroft
1066cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
1067cf655043SAndy Whitcroft
1068cf655043SAndy Whitcroft	return $#stmt_lines + 2;
1069cf655043SAndy Whitcroft}
1070cf655043SAndy Whitcroft
1071cf655043SAndy Whitcroftsub statement_rawlines {
1072cf655043SAndy Whitcroft	my ($stmt) = @_;
1073cf655043SAndy Whitcroft
1074cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
1075cf655043SAndy Whitcroft
1076cf655043SAndy Whitcroft	return $#stmt_lines + 2;
1077cf655043SAndy Whitcroft}
1078cf655043SAndy Whitcroft
1079cf655043SAndy Whitcroftsub statement_block_size {
1080cf655043SAndy Whitcroft	my ($stmt) = @_;
1081cf655043SAndy Whitcroft
1082cf655043SAndy Whitcroft	$stmt =~ s/(^|\n)./$1/g;
1083cf655043SAndy Whitcroft	$stmt =~ s/^\s*{//;
1084cf655043SAndy Whitcroft	$stmt =~ s/}\s*$//;
1085cf655043SAndy Whitcroft	$stmt =~ s/^\s*//;
1086cf655043SAndy Whitcroft	$stmt =~ s/\s*$//;
1087cf655043SAndy Whitcroft
1088cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
1089cf655043SAndy Whitcroft	my @stmt_statements = ($stmt =~ /;/g);
1090cf655043SAndy Whitcroft
1091cf655043SAndy Whitcroft	my $stmt_lines = $#stmt_lines + 2;
1092cf655043SAndy Whitcroft	my $stmt_statements = $#stmt_statements + 1;
1093cf655043SAndy Whitcroft
1094cf655043SAndy Whitcroft	if ($stmt_lines > $stmt_statements) {
1095cf655043SAndy Whitcroft		return $stmt_lines;
1096cf655043SAndy Whitcroft	} else {
1097cf655043SAndy Whitcroft		return $stmt_statements;
1098cf655043SAndy Whitcroft	}
1099cf655043SAndy Whitcroft}
1100cf655043SAndy Whitcroft
110113214adfSAndy Whitcroftsub ctx_statement_full {
110213214adfSAndy Whitcroft	my ($linenr, $remain, $off) = @_;
110313214adfSAndy Whitcroft	my ($statement, $condition, $level);
110413214adfSAndy Whitcroft
110513214adfSAndy Whitcroft	my (@chunks);
110613214adfSAndy Whitcroft
1107cf655043SAndy Whitcroft	# Grab the first conditional/block pair.
110813214adfSAndy Whitcroft	($statement, $condition, $linenr, $remain, $off, $level) =
110913214adfSAndy Whitcroft				ctx_statement_block($linenr, $remain, $off);
1110773647a0SAndy Whitcroft	#print "F: c<$condition> s<$statement> remain<$remain>\n";
111113214adfSAndy Whitcroft	push(@chunks, [ $condition, $statement ]);
1112cf655043SAndy Whitcroft	if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
1113cf655043SAndy Whitcroft		return ($level, $linenr, @chunks);
1114cf655043SAndy Whitcroft	}
1115cf655043SAndy Whitcroft
1116cf655043SAndy Whitcroft	# Pull in the following conditional/block pairs and see if they
1117cf655043SAndy Whitcroft	# could continue the statement.
1118cf655043SAndy Whitcroft	for (;;) {
111913214adfSAndy Whitcroft		($statement, $condition, $linenr, $remain, $off, $level) =
112013214adfSAndy Whitcroft				ctx_statement_block($linenr, $remain, $off);
1121cf655043SAndy Whitcroft		#print "C: c<$condition> s<$statement> remain<$remain>\n";
1122773647a0SAndy Whitcroft		last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
1123cf655043SAndy Whitcroft		#print "C: push\n";
1124cf655043SAndy Whitcroft		push(@chunks, [ $condition, $statement ]);
112513214adfSAndy Whitcroft	}
112613214adfSAndy Whitcroft
112713214adfSAndy Whitcroft	return ($level, $linenr, @chunks);
11288905a67cSAndy Whitcroft}
11298905a67cSAndy Whitcroft
11304a0df2efSAndy Whitcroftsub ctx_block_get {
1131f0a594c1SAndy Whitcroft	my ($linenr, $remain, $outer, $open, $close, $off) = @_;
11324a0df2efSAndy Whitcroft	my $line;
11334a0df2efSAndy Whitcroft	my $start = $linenr - 1;
11344a0df2efSAndy Whitcroft	my $blk = '';
11354a0df2efSAndy Whitcroft	my @o;
11364a0df2efSAndy Whitcroft	my @c;
11374a0df2efSAndy Whitcroft	my @res = ();
11384a0df2efSAndy Whitcroft
1139f0a594c1SAndy Whitcroft	my $level = 0;
11404635f4fbSAndy Whitcroft	my @stack = ($level);
114100df344fSAndy Whitcroft	for ($line = $start; $remain > 0; $line++) {
114200df344fSAndy Whitcroft		next if ($rawlines[$line] =~ /^-/);
114300df344fSAndy Whitcroft		$remain--;
114400df344fSAndy Whitcroft
114500df344fSAndy Whitcroft		$blk .= $rawlines[$line];
11464635f4fbSAndy Whitcroft
11474635f4fbSAndy Whitcroft		# Handle nested #if/#else.
114801464f30SAndy Whitcroft		if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
11494635f4fbSAndy Whitcroft			push(@stack, $level);
115001464f30SAndy Whitcroft		} elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
11514635f4fbSAndy Whitcroft			$level = $stack[$#stack - 1];
115201464f30SAndy Whitcroft		} elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
11534635f4fbSAndy Whitcroft			$level = pop(@stack);
11544635f4fbSAndy Whitcroft		}
11554635f4fbSAndy Whitcroft
115601464f30SAndy Whitcroft		foreach my $c (split(//, $lines[$line])) {
1157f0a594c1SAndy Whitcroft			##print "C<$c>L<$level><$open$close>O<$off>\n";
1158f0a594c1SAndy Whitcroft			if ($off > 0) {
1159f0a594c1SAndy Whitcroft				$off--;
1160f0a594c1SAndy Whitcroft				next;
1161f0a594c1SAndy Whitcroft			}
11624a0df2efSAndy Whitcroft
1163f0a594c1SAndy Whitcroft			if ($c eq $close && $level > 0) {
1164f0a594c1SAndy Whitcroft				$level--;
1165f0a594c1SAndy Whitcroft				last if ($level == 0);
1166f0a594c1SAndy Whitcroft			} elsif ($c eq $open) {
1167f0a594c1SAndy Whitcroft				$level++;
1168f0a594c1SAndy Whitcroft			}
1169f0a594c1SAndy Whitcroft		}
11704a0df2efSAndy Whitcroft
1171f0a594c1SAndy Whitcroft		if (!$outer || $level <= 1) {
117200df344fSAndy Whitcroft			push(@res, $rawlines[$line]);
11734a0df2efSAndy Whitcroft		}
11744a0df2efSAndy Whitcroft
1175f0a594c1SAndy Whitcroft		last if ($level == 0);
11764a0df2efSAndy Whitcroft	}
11774a0df2efSAndy Whitcroft
1178f0a594c1SAndy Whitcroft	return ($level, @res);
11794a0df2efSAndy Whitcroft}
11804a0df2efSAndy Whitcroftsub ctx_block_outer {
11814a0df2efSAndy Whitcroft	my ($linenr, $remain) = @_;
11824a0df2efSAndy Whitcroft
1183f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
1184f0a594c1SAndy Whitcroft	return @r;
11854a0df2efSAndy Whitcroft}
11864a0df2efSAndy Whitcroftsub ctx_block {
11874a0df2efSAndy Whitcroft	my ($linenr, $remain) = @_;
11884a0df2efSAndy Whitcroft
1189f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1190f0a594c1SAndy Whitcroft	return @r;
1191653d4876SAndy Whitcroft}
1192653d4876SAndy Whitcroftsub ctx_statement {
1193f0a594c1SAndy Whitcroft	my ($linenr, $remain, $off) = @_;
1194f0a594c1SAndy Whitcroft
1195f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1196f0a594c1SAndy Whitcroft	return @r;
1197f0a594c1SAndy Whitcroft}
1198f0a594c1SAndy Whitcroftsub ctx_block_level {
1199653d4876SAndy Whitcroft	my ($linenr, $remain) = @_;
1200653d4876SAndy Whitcroft
1201f0a594c1SAndy Whitcroft	return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
12024a0df2efSAndy Whitcroft}
12039c0ca6f9SAndy Whitcroftsub ctx_statement_level {
12049c0ca6f9SAndy Whitcroft	my ($linenr, $remain, $off) = @_;
12059c0ca6f9SAndy Whitcroft
12069c0ca6f9SAndy Whitcroft	return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
12079c0ca6f9SAndy Whitcroft}
12084a0df2efSAndy Whitcroft
12094a0df2efSAndy Whitcroftsub ctx_locate_comment {
12104a0df2efSAndy Whitcroft	my ($first_line, $end_line) = @_;
12114a0df2efSAndy Whitcroft
12124a0df2efSAndy Whitcroft	# Catch a comment on the end of the line itself.
1213beae6332SAndy Whitcroft	my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
12144a0df2efSAndy Whitcroft	return $current_comment if (defined $current_comment);
12154a0df2efSAndy Whitcroft
12164a0df2efSAndy Whitcroft	# Look through the context and try and figure out if there is a
12174a0df2efSAndy Whitcroft	# comment.
12184a0df2efSAndy Whitcroft	my $in_comment = 0;
12194a0df2efSAndy Whitcroft	$current_comment = '';
12204a0df2efSAndy Whitcroft	for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
122100df344fSAndy Whitcroft		my $line = $rawlines[$linenr - 1];
122200df344fSAndy Whitcroft		#warn "           $line\n";
12234a0df2efSAndy Whitcroft		if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
12244a0df2efSAndy Whitcroft			$in_comment = 1;
12254a0df2efSAndy Whitcroft		}
12264a0df2efSAndy Whitcroft		if ($line =~ m@/\*@) {
12274a0df2efSAndy Whitcroft			$in_comment = 1;
12284a0df2efSAndy Whitcroft		}
12294a0df2efSAndy Whitcroft		if (!$in_comment && $current_comment ne '') {
12304a0df2efSAndy Whitcroft			$current_comment = '';
12314a0df2efSAndy Whitcroft		}
12324a0df2efSAndy Whitcroft		$current_comment .= $line . "\n" if ($in_comment);
12334a0df2efSAndy Whitcroft		if ($line =~ m@\*/@) {
12344a0df2efSAndy Whitcroft			$in_comment = 0;
12354a0df2efSAndy Whitcroft		}
12364a0df2efSAndy Whitcroft	}
12374a0df2efSAndy Whitcroft
12384a0df2efSAndy Whitcroft	chomp($current_comment);
12394a0df2efSAndy Whitcroft	return($current_comment);
12404a0df2efSAndy Whitcroft}
12414a0df2efSAndy Whitcroftsub ctx_has_comment {
12424a0df2efSAndy Whitcroft	my ($first_line, $end_line) = @_;
12434a0df2efSAndy Whitcroft	my $cmt = ctx_locate_comment($first_line, $end_line);
12444a0df2efSAndy Whitcroft
124500df344fSAndy Whitcroft	##print "LINE: $rawlines[$end_line - 1 ]\n";
12464a0df2efSAndy Whitcroft	##print "CMMT: $cmt\n";
12474a0df2efSAndy Whitcroft
12484a0df2efSAndy Whitcroft	return ($cmt ne '');
12494a0df2efSAndy Whitcroft}
12504a0df2efSAndy Whitcroft
12514d001e4dSAndy Whitcroftsub raw_line {
12524d001e4dSAndy Whitcroft	my ($linenr, $cnt) = @_;
12534d001e4dSAndy Whitcroft
12544d001e4dSAndy Whitcroft	my $offset = $linenr - 1;
12554d001e4dSAndy Whitcroft	$cnt++;
12564d001e4dSAndy Whitcroft
12574d001e4dSAndy Whitcroft	my $line;
12584d001e4dSAndy Whitcroft	while ($cnt) {
12594d001e4dSAndy Whitcroft		$line = $rawlines[$offset++];
12604d001e4dSAndy Whitcroft		next if (defined($line) && $line =~ /^-/);
12614d001e4dSAndy Whitcroft		$cnt--;
12624d001e4dSAndy Whitcroft	}
12634d001e4dSAndy Whitcroft
12644d001e4dSAndy Whitcroft	return $line;
12654d001e4dSAndy Whitcroft}
12664d001e4dSAndy Whitcroft
12670a920b5bSAndy Whitcroftsub cat_vet {
12680a920b5bSAndy Whitcroft	my ($vet) = @_;
12699c0ca6f9SAndy Whitcroft	my ($res, $coded);
12700a920b5bSAndy Whitcroft
12719c0ca6f9SAndy Whitcroft	$res = '';
12726c72ffaaSAndy Whitcroft	while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
12736c72ffaaSAndy Whitcroft		$res .= $1;
12746c72ffaaSAndy Whitcroft		if ($2 ne '') {
12759c0ca6f9SAndy Whitcroft			$coded = sprintf("^%c", unpack('C', $2) + 64);
12766c72ffaaSAndy Whitcroft			$res .= $coded;
12776c72ffaaSAndy Whitcroft		}
12789c0ca6f9SAndy Whitcroft	}
12799c0ca6f9SAndy Whitcroft	$res =~ s/$/\$/;
12800a920b5bSAndy Whitcroft
12819c0ca6f9SAndy Whitcroft	return $res;
12820a920b5bSAndy Whitcroft}
12830a920b5bSAndy Whitcroft
1284c2fdda0dSAndy Whitcroftmy $av_preprocessor = 0;
1285cf655043SAndy Whitcroftmy $av_pending;
1286c2fdda0dSAndy Whitcroftmy @av_paren_type;
12871f65f947SAndy Whitcroftmy $av_pend_colon;
1288c2fdda0dSAndy Whitcroft
1289c2fdda0dSAndy Whitcroftsub annotate_reset {
1290c2fdda0dSAndy Whitcroft	$av_preprocessor = 0;
1291cf655043SAndy Whitcroft	$av_pending = '_';
1292cf655043SAndy Whitcroft	@av_paren_type = ('E');
12931f65f947SAndy Whitcroft	$av_pend_colon = 'O';
1294c2fdda0dSAndy Whitcroft}
1295c2fdda0dSAndy Whitcroft
12966c72ffaaSAndy Whitcroftsub annotate_values {
12976c72ffaaSAndy Whitcroft	my ($stream, $type) = @_;
12986c72ffaaSAndy Whitcroft
12996c72ffaaSAndy Whitcroft	my $res;
13001f65f947SAndy Whitcroft	my $var = '_' x length($stream);
13016c72ffaaSAndy Whitcroft	my $cur = $stream;
13026c72ffaaSAndy Whitcroft
1303c2fdda0dSAndy Whitcroft	print "$stream\n" if ($dbg_values > 1);
13046c72ffaaSAndy Whitcroft
13056c72ffaaSAndy Whitcroft	while (length($cur)) {
1306773647a0SAndy Whitcroft		@av_paren_type = ('E') if ($#av_paren_type < 0);
1307cf655043SAndy Whitcroft		print " <" . join('', @av_paren_type) .
1308171ae1a4SAndy Whitcroft				"> <$type> <$av_pending>" if ($dbg_values > 1);
13096c72ffaaSAndy Whitcroft		if ($cur =~ /^(\s+)/o) {
1310c2fdda0dSAndy Whitcroft			print "WS($1)\n" if ($dbg_values > 1);
1311c2fdda0dSAndy Whitcroft			if ($1 =~ /\n/ && $av_preprocessor) {
1312cf655043SAndy Whitcroft				$type = pop(@av_paren_type);
1313c2fdda0dSAndy Whitcroft				$av_preprocessor = 0;
13146c72ffaaSAndy Whitcroft			}
13156c72ffaaSAndy Whitcroft
1316c023e473SFlorian Mickler		} elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
13179446ef56SAndy Whitcroft			print "CAST($1)\n" if ($dbg_values > 1);
13189446ef56SAndy Whitcroft			push(@av_paren_type, $type);
1319addcdceaSAndy Whitcroft			$type = 'c';
13209446ef56SAndy Whitcroft
1321e91b6e26SAndy Whitcroft		} elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
1322c2fdda0dSAndy Whitcroft			print "DECLARE($1)\n" if ($dbg_values > 1);
13236c72ffaaSAndy Whitcroft			$type = 'T';
13246c72ffaaSAndy Whitcroft
1325389a2fe5SAndy Whitcroft		} elsif ($cur =~ /^($Modifier)\s*/) {
1326389a2fe5SAndy Whitcroft			print "MODIFIER($1)\n" if ($dbg_values > 1);
1327389a2fe5SAndy Whitcroft			$type = 'T';
1328389a2fe5SAndy Whitcroft
1329c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
1330171ae1a4SAndy Whitcroft			print "DEFINE($1,$2)\n" if ($dbg_values > 1);
1331c2fdda0dSAndy Whitcroft			$av_preprocessor = 1;
1332171ae1a4SAndy Whitcroft			push(@av_paren_type, $type);
1333171ae1a4SAndy Whitcroft			if ($2 ne '') {
1334cf655043SAndy Whitcroft				$av_pending = 'N';
1335171ae1a4SAndy Whitcroft			}
1336171ae1a4SAndy Whitcroft			$type = 'E';
1337171ae1a4SAndy Whitcroft
1338c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
1339171ae1a4SAndy Whitcroft			print "UNDEF($1)\n" if ($dbg_values > 1);
1340171ae1a4SAndy Whitcroft			$av_preprocessor = 1;
1341171ae1a4SAndy Whitcroft			push(@av_paren_type, $type);
13426c72ffaaSAndy Whitcroft
1343c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
1344cf655043SAndy Whitcroft			print "PRE_START($1)\n" if ($dbg_values > 1);
1345c2fdda0dSAndy Whitcroft			$av_preprocessor = 1;
1346cf655043SAndy Whitcroft
1347cf655043SAndy Whitcroft			push(@av_paren_type, $type);
1348cf655043SAndy Whitcroft			push(@av_paren_type, $type);
1349171ae1a4SAndy Whitcroft			$type = 'E';
1350cf655043SAndy Whitcroft
1351c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
1352cf655043SAndy Whitcroft			print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1353cf655043SAndy Whitcroft			$av_preprocessor = 1;
1354cf655043SAndy Whitcroft
1355cf655043SAndy Whitcroft			push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1356cf655043SAndy Whitcroft
1357171ae1a4SAndy Whitcroft			$type = 'E';
1358cf655043SAndy Whitcroft
1359c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:endif))/o) {
1360cf655043SAndy Whitcroft			print "PRE_END($1)\n" if ($dbg_values > 1);
1361cf655043SAndy Whitcroft
1362cf655043SAndy Whitcroft			$av_preprocessor = 1;
1363cf655043SAndy Whitcroft
1364cf655043SAndy Whitcroft			# Assume all arms of the conditional end as this
1365cf655043SAndy Whitcroft			# one does, and continue as if the #endif was not here.
1366cf655043SAndy Whitcroft			pop(@av_paren_type);
1367cf655043SAndy Whitcroft			push(@av_paren_type, $type);
1368171ae1a4SAndy Whitcroft			$type = 'E';
13696c72ffaaSAndy Whitcroft
13706c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\\\n)/o) {
1371c2fdda0dSAndy Whitcroft			print "PRECONT($1)\n" if ($dbg_values > 1);
13726c72ffaaSAndy Whitcroft
1373171ae1a4SAndy Whitcroft		} elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1374171ae1a4SAndy Whitcroft			print "ATTR($1)\n" if ($dbg_values > 1);
1375171ae1a4SAndy Whitcroft			$av_pending = $type;
1376171ae1a4SAndy Whitcroft			$type = 'N';
1377171ae1a4SAndy Whitcroft
13786c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
1379c2fdda0dSAndy Whitcroft			print "SIZEOF($1)\n" if ($dbg_values > 1);
13806c72ffaaSAndy Whitcroft			if (defined $2) {
1381cf655043SAndy Whitcroft				$av_pending = 'V';
13826c72ffaaSAndy Whitcroft			}
13836c72ffaaSAndy Whitcroft			$type = 'N';
13846c72ffaaSAndy Whitcroft
138514b111c1SAndy Whitcroft		} elsif ($cur =~ /^(if|while|for)\b/o) {
1386c2fdda0dSAndy Whitcroft			print "COND($1)\n" if ($dbg_values > 1);
138714b111c1SAndy Whitcroft			$av_pending = 'E';
13886c72ffaaSAndy Whitcroft			$type = 'N';
13896c72ffaaSAndy Whitcroft
13901f65f947SAndy Whitcroft		} elsif ($cur =~/^(case)/o) {
13911f65f947SAndy Whitcroft			print "CASE($1)\n" if ($dbg_values > 1);
13921f65f947SAndy Whitcroft			$av_pend_colon = 'C';
13931f65f947SAndy Whitcroft			$type = 'N';
13941f65f947SAndy Whitcroft
139514b111c1SAndy Whitcroft		} elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
1396c2fdda0dSAndy Whitcroft			print "KEYWORD($1)\n" if ($dbg_values > 1);
13976c72ffaaSAndy Whitcroft			$type = 'N';
13986c72ffaaSAndy Whitcroft
13996c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\()/o) {
1400c2fdda0dSAndy Whitcroft			print "PAREN('$1')\n" if ($dbg_values > 1);
1401cf655043SAndy Whitcroft			push(@av_paren_type, $av_pending);
1402cf655043SAndy Whitcroft			$av_pending = '_';
14036c72ffaaSAndy Whitcroft			$type = 'N';
14046c72ffaaSAndy Whitcroft
14056c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\))/o) {
1406cf655043SAndy Whitcroft			my $new_type = pop(@av_paren_type);
1407cf655043SAndy Whitcroft			if ($new_type ne '_') {
1408cf655043SAndy Whitcroft				$type = $new_type;
1409c2fdda0dSAndy Whitcroft				print "PAREN('$1') -> $type\n"
1410c2fdda0dSAndy Whitcroft							if ($dbg_values > 1);
14116c72ffaaSAndy Whitcroft			} else {
1412c2fdda0dSAndy Whitcroft				print "PAREN('$1')\n" if ($dbg_values > 1);
14136c72ffaaSAndy Whitcroft			}
14146c72ffaaSAndy Whitcroft
1415c8cb2ca3SAndy Whitcroft		} elsif ($cur =~ /^($Ident)\s*\(/o) {
1416c2fdda0dSAndy Whitcroft			print "FUNC($1)\n" if ($dbg_values > 1);
1417c8cb2ca3SAndy Whitcroft			$type = 'V';
1418cf655043SAndy Whitcroft			$av_pending = 'V';
14196c72ffaaSAndy Whitcroft
14208e761b04SAndy Whitcroft		} elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
14218e761b04SAndy Whitcroft			if (defined $2 && $type eq 'C' || $type eq 'T') {
14221f65f947SAndy Whitcroft				$av_pend_colon = 'B';
14238e761b04SAndy Whitcroft			} elsif ($type eq 'E') {
14248e761b04SAndy Whitcroft				$av_pend_colon = 'L';
14251f65f947SAndy Whitcroft			}
14261f65f947SAndy Whitcroft			print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
14271f65f947SAndy Whitcroft			$type = 'V';
14281f65f947SAndy Whitcroft
14296c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Ident|$Constant)/o) {
1430c2fdda0dSAndy Whitcroft			print "IDENT($1)\n" if ($dbg_values > 1);
14316c72ffaaSAndy Whitcroft			$type = 'V';
14326c72ffaaSAndy Whitcroft
14336c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Assignment)/o) {
1434c2fdda0dSAndy Whitcroft			print "ASSIGN($1)\n" if ($dbg_values > 1);
14356c72ffaaSAndy Whitcroft			$type = 'N';
14366c72ffaaSAndy Whitcroft
1437cf655043SAndy Whitcroft		} elsif ($cur =~/^(;|{|})/) {
1438c2fdda0dSAndy Whitcroft			print "END($1)\n" if ($dbg_values > 1);
143913214adfSAndy Whitcroft			$type = 'E';
14401f65f947SAndy Whitcroft			$av_pend_colon = 'O';
144113214adfSAndy Whitcroft
14428e761b04SAndy Whitcroft		} elsif ($cur =~/^(,)/) {
14438e761b04SAndy Whitcroft			print "COMMA($1)\n" if ($dbg_values > 1);
14448e761b04SAndy Whitcroft			$type = 'C';
14458e761b04SAndy Whitcroft
14461f65f947SAndy Whitcroft		} elsif ($cur =~ /^(\?)/o) {
14471f65f947SAndy Whitcroft			print "QUESTION($1)\n" if ($dbg_values > 1);
14481f65f947SAndy Whitcroft			$type = 'N';
14491f65f947SAndy Whitcroft
14501f65f947SAndy Whitcroft		} elsif ($cur =~ /^(:)/o) {
14511f65f947SAndy Whitcroft			print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
14521f65f947SAndy Whitcroft
14531f65f947SAndy Whitcroft			substr($var, length($res), 1, $av_pend_colon);
14541f65f947SAndy Whitcroft			if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
14551f65f947SAndy Whitcroft				$type = 'E';
14561f65f947SAndy Whitcroft			} else {
14571f65f947SAndy Whitcroft				$type = 'N';
14581f65f947SAndy Whitcroft			}
14591f65f947SAndy Whitcroft			$av_pend_colon = 'O';
14601f65f947SAndy Whitcroft
14618e761b04SAndy Whitcroft		} elsif ($cur =~ /^(\[)/o) {
146213214adfSAndy Whitcroft			print "CLOSE($1)\n" if ($dbg_values > 1);
14636c72ffaaSAndy Whitcroft			$type = 'N';
14646c72ffaaSAndy Whitcroft
14650d413866SAndy Whitcroft		} elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
146674048ed8SAndy Whitcroft			my $variant;
146774048ed8SAndy Whitcroft
146874048ed8SAndy Whitcroft			print "OPV($1)\n" if ($dbg_values > 1);
146974048ed8SAndy Whitcroft			if ($type eq 'V') {
147074048ed8SAndy Whitcroft				$variant = 'B';
147174048ed8SAndy Whitcroft			} else {
147274048ed8SAndy Whitcroft				$variant = 'U';
147374048ed8SAndy Whitcroft			}
147474048ed8SAndy Whitcroft
147574048ed8SAndy Whitcroft			substr($var, length($res), 1, $variant);
147674048ed8SAndy Whitcroft			$type = 'N';
147774048ed8SAndy Whitcroft
14786c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Operators)/o) {
1479c2fdda0dSAndy Whitcroft			print "OP($1)\n" if ($dbg_values > 1);
14806c72ffaaSAndy Whitcroft			if ($1 ne '++' && $1 ne '--') {
14816c72ffaaSAndy Whitcroft				$type = 'N';
14826c72ffaaSAndy Whitcroft			}
14836c72ffaaSAndy Whitcroft
14846c72ffaaSAndy Whitcroft		} elsif ($cur =~ /(^.)/o) {
1485c2fdda0dSAndy Whitcroft			print "C($1)\n" if ($dbg_values > 1);
14866c72ffaaSAndy Whitcroft		}
14876c72ffaaSAndy Whitcroft		if (defined $1) {
14886c72ffaaSAndy Whitcroft			$cur = substr($cur, length($1));
14896c72ffaaSAndy Whitcroft			$res .= $type x length($1);
14906c72ffaaSAndy Whitcroft		}
14916c72ffaaSAndy Whitcroft	}
14926c72ffaaSAndy Whitcroft
14931f65f947SAndy Whitcroft	return ($res, $var);
14946c72ffaaSAndy Whitcroft}
14956c72ffaaSAndy Whitcroft
14968905a67cSAndy Whitcroftsub possible {
149713214adfSAndy Whitcroft	my ($possible, $line) = @_;
14989a974fdbSAndy Whitcroft	my $notPermitted = qr{(?:
14990776e594SAndy Whitcroft		^(?:
15000776e594SAndy Whitcroft			$Modifier|
15010776e594SAndy Whitcroft			$Storage|
15020776e594SAndy Whitcroft			$Type|
15039a974fdbSAndy Whitcroft			DEFINE_\S+
15049a974fdbSAndy Whitcroft		)$|
15059a974fdbSAndy Whitcroft		^(?:
15060776e594SAndy Whitcroft			goto|
15070776e594SAndy Whitcroft			return|
15080776e594SAndy Whitcroft			case|
15090776e594SAndy Whitcroft			else|
15100776e594SAndy Whitcroft			asm|__asm__|
151189a88353SAndy Whitcroft			do|
151289a88353SAndy Whitcroft			\#|
151389a88353SAndy Whitcroft			\#\#|
15149a974fdbSAndy Whitcroft		)(?:\s|$)|
15150776e594SAndy Whitcroft		^(?:typedef|struct|enum)\b
15169a974fdbSAndy Whitcroft	    )}x;
15179a974fdbSAndy Whitcroft	warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
15189a974fdbSAndy Whitcroft	if ($possible !~ $notPermitted) {
1519c45dcabdSAndy Whitcroft		# Check for modifiers.
1520c45dcabdSAndy Whitcroft		$possible =~ s/\s*$Storage\s*//g;
1521c45dcabdSAndy Whitcroft		$possible =~ s/\s*$Sparse\s*//g;
1522c45dcabdSAndy Whitcroft		if ($possible =~ /^\s*$/) {
1523c45dcabdSAndy Whitcroft
1524c45dcabdSAndy Whitcroft		} elsif ($possible =~ /\s/) {
1525c45dcabdSAndy Whitcroft			$possible =~ s/\s*$Type\s*//g;
1526d2506586SAndy Whitcroft			for my $modifier (split(' ', $possible)) {
15279a974fdbSAndy Whitcroft				if ($modifier !~ $notPermitted) {
1528d2506586SAndy Whitcroft					warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1529d2506586SAndy Whitcroft					push(@modifierList, $modifier);
1530d2506586SAndy Whitcroft				}
15319a974fdbSAndy Whitcroft			}
1532c45dcabdSAndy Whitcroft
1533c45dcabdSAndy Whitcroft		} else {
153413214adfSAndy Whitcroft			warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
15358905a67cSAndy Whitcroft			push(@typeList, $possible);
1536c45dcabdSAndy Whitcroft		}
15378905a67cSAndy Whitcroft		build_types();
15380776e594SAndy Whitcroft	} else {
15390776e594SAndy Whitcroft		warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
15408905a67cSAndy Whitcroft	}
15418905a67cSAndy Whitcroft}
15428905a67cSAndy Whitcroft
15436c72ffaaSAndy Whitcroftmy $prefix = '';
15446c72ffaaSAndy Whitcroft
1545000d1cc1SJoe Perchessub show_type {
1546cbec18afSJoe Perches	my ($type) = @_;
154791bfe484SJoe Perches
1548cbec18afSJoe Perches	return defined $use_type{$type} if (scalar keys %use_type > 0);
1549cbec18afSJoe Perches
1550cbec18afSJoe Perches	return !defined $ignore_type{$type};
1551000d1cc1SJoe Perches}
1552000d1cc1SJoe Perches
1553f0a594c1SAndy Whitcroftsub report {
1554cbec18afSJoe Perches	my ($level, $type, $msg) = @_;
1555cbec18afSJoe Perches
1556cbec18afSJoe Perches	if (!show_type($type) ||
1557cbec18afSJoe Perches	    (defined $tst_only && $msg !~ /\Q$tst_only\E/)) {
1558773647a0SAndy Whitcroft		return 0;
1559773647a0SAndy Whitcroft	}
1560000d1cc1SJoe Perches	my $line;
1561000d1cc1SJoe Perches	if ($show_types) {
1562cbec18afSJoe Perches		$line = "$prefix$level:$type: $msg\n";
1563000d1cc1SJoe Perches	} else {
1564cbec18afSJoe Perches		$line = "$prefix$level: $msg\n";
1565000d1cc1SJoe Perches	}
15668905a67cSAndy Whitcroft	$line = (split('\n', $line))[0] . "\n" if ($terse);
15678905a67cSAndy Whitcroft
156813214adfSAndy Whitcroft	push(our @report, $line);
1569773647a0SAndy Whitcroft
1570773647a0SAndy Whitcroft	return 1;
1571f0a594c1SAndy Whitcroft}
1572cbec18afSJoe Perches
1573f0a594c1SAndy Whitcroftsub report_dump {
157413214adfSAndy Whitcroft	our @report;
1575f0a594c1SAndy Whitcroft}
1576000d1cc1SJoe Perches
1577d752fcc8SJoe Perchessub fixup_current_range {
1578d752fcc8SJoe Perches	my ($lineRef, $offset, $length) = @_;
1579d752fcc8SJoe Perches
1580d752fcc8SJoe Perches	if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) {
1581d752fcc8SJoe Perches		my $o = $1;
1582d752fcc8SJoe Perches		my $l = $2;
1583d752fcc8SJoe Perches		my $no = $o + $offset;
1584d752fcc8SJoe Perches		my $nl = $l + $length;
1585d752fcc8SJoe Perches		$$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/;
1586d752fcc8SJoe Perches	}
1587d752fcc8SJoe Perches}
1588d752fcc8SJoe Perches
1589d752fcc8SJoe Perchessub fix_inserted_deleted_lines {
1590d752fcc8SJoe Perches	my ($linesRef, $insertedRef, $deletedRef) = @_;
1591d752fcc8SJoe Perches
1592d752fcc8SJoe Perches	my $range_last_linenr = 0;
1593d752fcc8SJoe Perches	my $delta_offset = 0;
1594d752fcc8SJoe Perches
1595d752fcc8SJoe Perches	my $old_linenr = 0;
1596d752fcc8SJoe Perches	my $new_linenr = 0;
1597d752fcc8SJoe Perches
1598d752fcc8SJoe Perches	my $next_insert = 0;
1599d752fcc8SJoe Perches	my $next_delete = 0;
1600d752fcc8SJoe Perches
1601d752fcc8SJoe Perches	my @lines = ();
1602d752fcc8SJoe Perches
1603d752fcc8SJoe Perches	my $inserted = @{$insertedRef}[$next_insert++];
1604d752fcc8SJoe Perches	my $deleted = @{$deletedRef}[$next_delete++];
1605d752fcc8SJoe Perches
1606d752fcc8SJoe Perches	foreach my $old_line (@{$linesRef}) {
1607d752fcc8SJoe Perches		my $save_line = 1;
1608d752fcc8SJoe Perches		my $line = $old_line;	#don't modify the array
1609d752fcc8SJoe Perches		if ($line =~ /^(?:\+\+\+\|\-\-\-)\s+\S+/) {	#new filename
1610d752fcc8SJoe Perches			$delta_offset = 0;
1611d752fcc8SJoe Perches		} elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) {	#new hunk
1612d752fcc8SJoe Perches			$range_last_linenr = $new_linenr;
1613d752fcc8SJoe Perches			fixup_current_range(\$line, $delta_offset, 0);
1614d752fcc8SJoe Perches		}
1615d752fcc8SJoe Perches
1616d752fcc8SJoe Perches		while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) {
1617d752fcc8SJoe Perches			$deleted = @{$deletedRef}[$next_delete++];
1618d752fcc8SJoe Perches			$save_line = 0;
1619d752fcc8SJoe Perches			fixup_current_range(\$lines[$range_last_linenr], $delta_offset--, -1);
1620d752fcc8SJoe Perches		}
1621d752fcc8SJoe Perches
1622d752fcc8SJoe Perches		while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) {
1623d752fcc8SJoe Perches			push(@lines, ${$inserted}{'LINE'});
1624d752fcc8SJoe Perches			$inserted = @{$insertedRef}[$next_insert++];
1625d752fcc8SJoe Perches			$new_linenr++;
1626d752fcc8SJoe Perches			fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1);
1627d752fcc8SJoe Perches		}
1628d752fcc8SJoe Perches
1629d752fcc8SJoe Perches		if ($save_line) {
1630d752fcc8SJoe Perches			push(@lines, $line);
1631d752fcc8SJoe Perches			$new_linenr++;
1632d752fcc8SJoe Perches		}
1633d752fcc8SJoe Perches
1634d752fcc8SJoe Perches		$old_linenr++;
1635d752fcc8SJoe Perches	}
1636d752fcc8SJoe Perches
1637d752fcc8SJoe Perches	return @lines;
1638d752fcc8SJoe Perches}
1639d752fcc8SJoe Perches
1640f2d7e4d4SJoe Perchessub fix_insert_line {
1641f2d7e4d4SJoe Perches	my ($linenr, $line) = @_;
1642f2d7e4d4SJoe Perches
1643f2d7e4d4SJoe Perches	my $inserted = {
1644f2d7e4d4SJoe Perches		LINENR => $linenr,
1645f2d7e4d4SJoe Perches		LINE => $line,
1646f2d7e4d4SJoe Perches	};
1647f2d7e4d4SJoe Perches	push(@fixed_inserted, $inserted);
1648f2d7e4d4SJoe Perches}
1649f2d7e4d4SJoe Perches
1650f2d7e4d4SJoe Perchessub fix_delete_line {
1651f2d7e4d4SJoe Perches	my ($linenr, $line) = @_;
1652f2d7e4d4SJoe Perches
1653f2d7e4d4SJoe Perches	my $deleted = {
1654f2d7e4d4SJoe Perches		LINENR => $linenr,
1655f2d7e4d4SJoe Perches		LINE => $line,
1656f2d7e4d4SJoe Perches	};
1657f2d7e4d4SJoe Perches
1658f2d7e4d4SJoe Perches	push(@fixed_deleted, $deleted);
1659f2d7e4d4SJoe Perches}
1660f2d7e4d4SJoe Perches
1661de7d4f0eSAndy Whitcroftsub ERROR {
1662cbec18afSJoe Perches	my ($type, $msg) = @_;
1663cbec18afSJoe Perches
1664cbec18afSJoe Perches	if (report("ERROR", $type, $msg)) {
1665de7d4f0eSAndy Whitcroft		our $clean = 0;
16666c72ffaaSAndy Whitcroft		our $cnt_error++;
16673705ce5bSJoe Perches		return 1;
1668de7d4f0eSAndy Whitcroft	}
16693705ce5bSJoe Perches	return 0;
1670773647a0SAndy Whitcroft}
1671de7d4f0eSAndy Whitcroftsub WARN {
1672cbec18afSJoe Perches	my ($type, $msg) = @_;
1673cbec18afSJoe Perches
1674cbec18afSJoe Perches	if (report("WARNING", $type, $msg)) {
1675de7d4f0eSAndy Whitcroft		our $clean = 0;
16766c72ffaaSAndy Whitcroft		our $cnt_warn++;
16773705ce5bSJoe Perches		return 1;
1678de7d4f0eSAndy Whitcroft	}
16793705ce5bSJoe Perches	return 0;
1680773647a0SAndy Whitcroft}
1681de7d4f0eSAndy Whitcroftsub CHK {
1682cbec18afSJoe Perches	my ($type, $msg) = @_;
1683cbec18afSJoe Perches
1684cbec18afSJoe Perches	if ($check && report("CHECK", $type, $msg)) {
1685de7d4f0eSAndy Whitcroft		our $clean = 0;
16866c72ffaaSAndy Whitcroft		our $cnt_chk++;
16873705ce5bSJoe Perches		return 1;
16886c72ffaaSAndy Whitcroft	}
16893705ce5bSJoe Perches	return 0;
1690de7d4f0eSAndy Whitcroft}
1691de7d4f0eSAndy Whitcroft
16926ecd9674SAndy Whitcroftsub check_absolute_file {
16936ecd9674SAndy Whitcroft	my ($absolute, $herecurr) = @_;
16946ecd9674SAndy Whitcroft	my $file = $absolute;
16956ecd9674SAndy Whitcroft
16966ecd9674SAndy Whitcroft	##print "absolute<$absolute>\n";
16976ecd9674SAndy Whitcroft
16986ecd9674SAndy Whitcroft	# See if any suffix of this path is a path within the tree.
16996ecd9674SAndy Whitcroft	while ($file =~ s@^[^/]*/@@) {
17006ecd9674SAndy Whitcroft		if (-f "$root/$file") {
17016ecd9674SAndy Whitcroft			##print "file<$file>\n";
17026ecd9674SAndy Whitcroft			last;
17036ecd9674SAndy Whitcroft		}
17046ecd9674SAndy Whitcroft	}
17056ecd9674SAndy Whitcroft	if (! -f _)  {
17066ecd9674SAndy Whitcroft		return 0;
17076ecd9674SAndy Whitcroft	}
17086ecd9674SAndy Whitcroft
17096ecd9674SAndy Whitcroft	# It is, so see if the prefix is acceptable.
17106ecd9674SAndy Whitcroft	my $prefix = $absolute;
17116ecd9674SAndy Whitcroft	substr($prefix, -length($file)) = '';
17126ecd9674SAndy Whitcroft
17136ecd9674SAndy Whitcroft	##print "prefix<$prefix>\n";
17146ecd9674SAndy Whitcroft	if ($prefix ne ".../") {
1715000d1cc1SJoe Perches		WARN("USE_RELATIVE_PATH",
1716000d1cc1SJoe Perches		     "use relative pathname instead of absolute in changelog text\n" . $herecurr);
17176ecd9674SAndy Whitcroft	}
17186ecd9674SAndy Whitcroft}
17196ecd9674SAndy Whitcroft
17203705ce5bSJoe Perchessub trim {
17213705ce5bSJoe Perches	my ($string) = @_;
17223705ce5bSJoe Perches
1723b34c648bSJoe Perches	$string =~ s/^\s+|\s+$//g;
1724b34c648bSJoe Perches
1725b34c648bSJoe Perches	return $string;
1726b34c648bSJoe Perches}
1727b34c648bSJoe Perches
1728b34c648bSJoe Perchessub ltrim {
1729b34c648bSJoe Perches	my ($string) = @_;
1730b34c648bSJoe Perches
1731b34c648bSJoe Perches	$string =~ s/^\s+//;
1732b34c648bSJoe Perches
1733b34c648bSJoe Perches	return $string;
1734b34c648bSJoe Perches}
1735b34c648bSJoe Perches
1736b34c648bSJoe Perchessub rtrim {
1737b34c648bSJoe Perches	my ($string) = @_;
1738b34c648bSJoe Perches
1739b34c648bSJoe Perches	$string =~ s/\s+$//;
17403705ce5bSJoe Perches
17413705ce5bSJoe Perches	return $string;
17423705ce5bSJoe Perches}
17433705ce5bSJoe Perches
174452ea8506SJoe Perchessub string_find_replace {
174552ea8506SJoe Perches	my ($string, $find, $replace) = @_;
174652ea8506SJoe Perches
174752ea8506SJoe Perches	$string =~ s/$find/$replace/g;
174852ea8506SJoe Perches
174952ea8506SJoe Perches	return $string;
175052ea8506SJoe Perches}
175152ea8506SJoe Perches
17523705ce5bSJoe Perchessub tabify {
17533705ce5bSJoe Perches	my ($leading) = @_;
17543705ce5bSJoe Perches
17553705ce5bSJoe Perches	my $source_indent = 8;
17563705ce5bSJoe Perches	my $max_spaces_before_tab = $source_indent - 1;
17573705ce5bSJoe Perches	my $spaces_to_tab = " " x $source_indent;
17583705ce5bSJoe Perches
17593705ce5bSJoe Perches	#convert leading spaces to tabs
17603705ce5bSJoe Perches	1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
17613705ce5bSJoe Perches	#Remove spaces before a tab
17623705ce5bSJoe Perches	1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
17633705ce5bSJoe Perches
17643705ce5bSJoe Perches	return "$leading";
17653705ce5bSJoe Perches}
17663705ce5bSJoe Perches
1767d1fe9c09SJoe Perchessub pos_last_openparen {
1768d1fe9c09SJoe Perches	my ($line) = @_;
1769d1fe9c09SJoe Perches
1770d1fe9c09SJoe Perches	my $pos = 0;
1771d1fe9c09SJoe Perches
1772d1fe9c09SJoe Perches	my $opens = $line =~ tr/\(/\(/;
1773d1fe9c09SJoe Perches	my $closes = $line =~ tr/\)/\)/;
1774d1fe9c09SJoe Perches
1775d1fe9c09SJoe Perches	my $last_openparen = 0;
1776d1fe9c09SJoe Perches
1777d1fe9c09SJoe Perches	if (($opens == 0) || ($closes >= $opens)) {
1778d1fe9c09SJoe Perches		return -1;
1779d1fe9c09SJoe Perches	}
1780d1fe9c09SJoe Perches
1781d1fe9c09SJoe Perches	my $len = length($line);
1782d1fe9c09SJoe Perches
1783d1fe9c09SJoe Perches	for ($pos = 0; $pos < $len; $pos++) {
1784d1fe9c09SJoe Perches		my $string = substr($line, $pos);
1785d1fe9c09SJoe Perches		if ($string =~ /^($FuncArg|$balanced_parens)/) {
1786d1fe9c09SJoe Perches			$pos += length($1) - 1;
1787d1fe9c09SJoe Perches		} elsif (substr($line, $pos, 1) eq '(') {
1788d1fe9c09SJoe Perches			$last_openparen = $pos;
1789d1fe9c09SJoe Perches		} elsif (index($string, '(') == -1) {
1790d1fe9c09SJoe Perches			last;
1791d1fe9c09SJoe Perches		}
1792d1fe9c09SJoe Perches	}
1793d1fe9c09SJoe Perches
179491cb5195SJoe Perches	return length(expand_tabs(substr($line, 0, $last_openparen))) + 1;
1795d1fe9c09SJoe Perches}
1796d1fe9c09SJoe Perches
17970a920b5bSAndy Whitcroftsub process {
17980a920b5bSAndy Whitcroft	my $filename = shift;
17990a920b5bSAndy Whitcroft
18000a920b5bSAndy Whitcroft	my $linenr=0;
18010a920b5bSAndy Whitcroft	my $prevline="";
1802c2fdda0dSAndy Whitcroft	my $prevrawline="";
18030a920b5bSAndy Whitcroft	my $stashline="";
1804c2fdda0dSAndy Whitcroft	my $stashrawline="";
18050a920b5bSAndy Whitcroft
18064a0df2efSAndy Whitcroft	my $length;
18070a920b5bSAndy Whitcroft	my $indent;
18080a920b5bSAndy Whitcroft	my $previndent=0;
18090a920b5bSAndy Whitcroft	my $stashindent=0;
18100a920b5bSAndy Whitcroft
1811de7d4f0eSAndy Whitcroft	our $clean = 1;
18120a920b5bSAndy Whitcroft	my $signoff = 0;
18130a920b5bSAndy Whitcroft	my $is_patch = 0;
18140a920b5bSAndy Whitcroft
181529ee1b0cSJoe Perches	my $in_header_lines = $file ? 0 : 1;
181615662b3eSJoe Perches	my $in_commit_log = 0;		#Scanning lines before patch
181713f1937eSJoe Perches	my $reported_maintainer_file = 0;
1818fa64205dSPasi Savanainen	my $non_utf8_charset = 0;
1819fa64205dSPasi Savanainen
1820365dd4eaSJoe Perches	my $last_blank_line = 0;
1821365dd4eaSJoe Perches
182213214adfSAndy Whitcroft	our @report = ();
18236c72ffaaSAndy Whitcroft	our $cnt_lines = 0;
18246c72ffaaSAndy Whitcroft	our $cnt_error = 0;
18256c72ffaaSAndy Whitcroft	our $cnt_warn = 0;
18266c72ffaaSAndy Whitcroft	our $cnt_chk = 0;
18276c72ffaaSAndy Whitcroft
18280a920b5bSAndy Whitcroft	# Trace the real file/line as we go.
18290a920b5bSAndy Whitcroft	my $realfile = '';
18300a920b5bSAndy Whitcroft	my $realline = 0;
18310a920b5bSAndy Whitcroft	my $realcnt = 0;
18320a920b5bSAndy Whitcroft	my $here = '';
18330a920b5bSAndy Whitcroft	my $in_comment = 0;
1834c2fdda0dSAndy Whitcroft	my $comment_edge = 0;
18350a920b5bSAndy Whitcroft	my $first_line = 0;
18361e855726SWolfram Sang	my $p1_prefix = '';
18370a920b5bSAndy Whitcroft
183813214adfSAndy Whitcroft	my $prev_values = 'E';
183913214adfSAndy Whitcroft
184013214adfSAndy Whitcroft	# suppression flags
1841773647a0SAndy Whitcroft	my %suppress_ifbraces;
1842170d3a22SAndy Whitcroft	my %suppress_whiletrailers;
18432b474a1aSAndy Whitcroft	my %suppress_export;
18443e469cdcSAndy Whitcroft	my $suppress_statement = 0;
1845653d4876SAndy Whitcroft
18467e51f197SJoe Perches	my %signatures = ();
1847323c1260SJoe Perches
1848c2fdda0dSAndy Whitcroft	# Pre-scan the patch sanitizing the lines.
1849de7d4f0eSAndy Whitcroft	# Pre-scan the patch looking for any __setup documentation.
1850c2fdda0dSAndy Whitcroft	#
1851de7d4f0eSAndy Whitcroft	my @setup_docs = ();
1852de7d4f0eSAndy Whitcroft	my $setup_docs = 0;
1853773647a0SAndy Whitcroft
1854d8b07710SJoe Perches	my $camelcase_file_seeded = 0;
1855d8b07710SJoe Perches
1856773647a0SAndy Whitcroft	sanitise_line_reset();
1857c2fdda0dSAndy Whitcroft	my $line;
1858c2fdda0dSAndy Whitcroft	foreach my $rawline (@rawlines) {
1859773647a0SAndy Whitcroft		$linenr++;
1860773647a0SAndy Whitcroft		$line = $rawline;
1861c2fdda0dSAndy Whitcroft
18623705ce5bSJoe Perches		push(@fixed, $rawline) if ($fix);
18633705ce5bSJoe Perches
1864773647a0SAndy Whitcroft		if ($rawline=~/^\+\+\+\s+(\S+)/) {
1865de7d4f0eSAndy Whitcroft			$setup_docs = 0;
1866de7d4f0eSAndy Whitcroft			if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
1867de7d4f0eSAndy Whitcroft				$setup_docs = 1;
1868de7d4f0eSAndy Whitcroft			}
1869773647a0SAndy Whitcroft			#next;
1870de7d4f0eSAndy Whitcroft		}
1871773647a0SAndy Whitcroft		if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1872773647a0SAndy Whitcroft			$realline=$1-1;
1873773647a0SAndy Whitcroft			if (defined $2) {
1874773647a0SAndy Whitcroft				$realcnt=$3+1;
1875773647a0SAndy Whitcroft			} else {
1876773647a0SAndy Whitcroft				$realcnt=1+1;
1877773647a0SAndy Whitcroft			}
1878c45dcabdSAndy Whitcroft			$in_comment = 0;
1879773647a0SAndy Whitcroft
1880773647a0SAndy Whitcroft			# Guestimate if this is a continuing comment.  Run
1881773647a0SAndy Whitcroft			# the context looking for a comment "edge".  If this
1882773647a0SAndy Whitcroft			# edge is a close comment then we must be in a comment
1883773647a0SAndy Whitcroft			# at context start.
1884773647a0SAndy Whitcroft			my $edge;
188501fa9147SAndy Whitcroft			my $cnt = $realcnt;
188601fa9147SAndy Whitcroft			for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
188701fa9147SAndy Whitcroft				next if (defined $rawlines[$ln - 1] &&
188801fa9147SAndy Whitcroft					 $rawlines[$ln - 1] =~ /^-/);
188901fa9147SAndy Whitcroft				$cnt--;
189001fa9147SAndy Whitcroft				#print "RAW<$rawlines[$ln - 1]>\n";
1891721c1cb6SAndy Whitcroft				last if (!defined $rawlines[$ln - 1]);
1892fae17daeSAndy Whitcroft				if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
1893fae17daeSAndy Whitcroft				    $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
1894fae17daeSAndy Whitcroft					($edge) = $1;
1895fae17daeSAndy Whitcroft					last;
1896fae17daeSAndy Whitcroft				}
1897773647a0SAndy Whitcroft			}
1898773647a0SAndy Whitcroft			if (defined $edge && $edge eq '*/') {
1899773647a0SAndy Whitcroft				$in_comment = 1;
1900773647a0SAndy Whitcroft			}
1901773647a0SAndy Whitcroft
1902773647a0SAndy Whitcroft			# Guestimate if this is a continuing comment.  If this
1903773647a0SAndy Whitcroft			# is the start of a diff block and this line starts
1904773647a0SAndy Whitcroft			# ' *' then it is very likely a comment.
1905773647a0SAndy Whitcroft			if (!defined $edge &&
190683242e0cSAndy Whitcroft			    $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
1907773647a0SAndy Whitcroft			{
1908773647a0SAndy Whitcroft				$in_comment = 1;
1909773647a0SAndy Whitcroft			}
1910773647a0SAndy Whitcroft
1911773647a0SAndy Whitcroft			##print "COMMENT:$in_comment edge<$edge> $rawline\n";
1912773647a0SAndy Whitcroft			sanitise_line_reset($in_comment);
1913773647a0SAndy Whitcroft
1914171ae1a4SAndy Whitcroft		} elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
1915773647a0SAndy Whitcroft			# Standardise the strings and chars within the input to
1916171ae1a4SAndy Whitcroft			# simplify matching -- only bother with positive lines.
1917773647a0SAndy Whitcroft			$line = sanitise_line($rawline);
1918773647a0SAndy Whitcroft		}
1919773647a0SAndy Whitcroft		push(@lines, $line);
1920773647a0SAndy Whitcroft
1921773647a0SAndy Whitcroft		if ($realcnt > 1) {
1922773647a0SAndy Whitcroft			$realcnt-- if ($line =~ /^(?:\+| |$)/);
1923773647a0SAndy Whitcroft		} else {
1924773647a0SAndy Whitcroft			$realcnt = 0;
1925773647a0SAndy Whitcroft		}
1926773647a0SAndy Whitcroft
1927773647a0SAndy Whitcroft		#print "==>$rawline\n";
1928773647a0SAndy Whitcroft		#print "-->$line\n";
1929de7d4f0eSAndy Whitcroft
1930de7d4f0eSAndy Whitcroft		if ($setup_docs && $line =~ /^\+/) {
1931de7d4f0eSAndy Whitcroft			push(@setup_docs, $line);
1932de7d4f0eSAndy Whitcroft		}
1933de7d4f0eSAndy Whitcroft	}
1934de7d4f0eSAndy Whitcroft
19356c72ffaaSAndy Whitcroft	$prefix = '';
19366c72ffaaSAndy Whitcroft
1937773647a0SAndy Whitcroft	$realcnt = 0;
1938773647a0SAndy Whitcroft	$linenr = 0;
1939194f66fcSJoe Perches	$fixlinenr = -1;
19400a920b5bSAndy Whitcroft	foreach my $line (@lines) {
19410a920b5bSAndy Whitcroft		$linenr++;
1942194f66fcSJoe Perches		$fixlinenr++;
19431b5539b1SJoe Perches		my $sline = $line;	#copy of $line
19441b5539b1SJoe Perches		$sline =~ s/$;/ /g;	#with comments as spaces
19450a920b5bSAndy Whitcroft
1946c2fdda0dSAndy Whitcroft		my $rawline = $rawlines[$linenr - 1];
19476c72ffaaSAndy Whitcroft
19480a920b5bSAndy Whitcroft#extract the line range in the file after the patch is applied
19496c72ffaaSAndy Whitcroft		if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
19500a920b5bSAndy Whitcroft			$is_patch = 1;
19514a0df2efSAndy Whitcroft			$first_line = $linenr + 1;
19520a920b5bSAndy Whitcroft			$realline=$1-1;
19530a920b5bSAndy Whitcroft			if (defined $2) {
19540a920b5bSAndy Whitcroft				$realcnt=$3+1;
19550a920b5bSAndy Whitcroft			} else {
19560a920b5bSAndy Whitcroft				$realcnt=1+1;
19570a920b5bSAndy Whitcroft			}
1958c2fdda0dSAndy Whitcroft			annotate_reset();
195913214adfSAndy Whitcroft			$prev_values = 'E';
196013214adfSAndy Whitcroft
1961773647a0SAndy Whitcroft			%suppress_ifbraces = ();
1962170d3a22SAndy Whitcroft			%suppress_whiletrailers = ();
19632b474a1aSAndy Whitcroft			%suppress_export = ();
19643e469cdcSAndy Whitcroft			$suppress_statement = 0;
19650a920b5bSAndy Whitcroft			next;
19660a920b5bSAndy Whitcroft
19674a0df2efSAndy Whitcroft# track the line number as we move through the hunk, note that
19684a0df2efSAndy Whitcroft# new versions of GNU diff omit the leading space on completely
19694a0df2efSAndy Whitcroft# blank context lines so we need to count that too.
1970773647a0SAndy Whitcroft		} elsif ($line =~ /^( |\+|$)/) {
19710a920b5bSAndy Whitcroft			$realline++;
1972d8aaf121SAndy Whitcroft			$realcnt-- if ($realcnt != 0);
19730a920b5bSAndy Whitcroft
19744a0df2efSAndy Whitcroft			# Measure the line length and indent.
1975c2fdda0dSAndy Whitcroft			($length, $indent) = line_stats($rawline);
19760a920b5bSAndy Whitcroft
19770a920b5bSAndy Whitcroft			# Track the previous line.
19780a920b5bSAndy Whitcroft			($prevline, $stashline) = ($stashline, $line);
19790a920b5bSAndy Whitcroft			($previndent, $stashindent) = ($stashindent, $indent);
1980c2fdda0dSAndy Whitcroft			($prevrawline, $stashrawline) = ($stashrawline, $rawline);
1981c2fdda0dSAndy Whitcroft
1982773647a0SAndy Whitcroft			#warn "line<$line>\n";
19836c72ffaaSAndy Whitcroft
1984d8aaf121SAndy Whitcroft		} elsif ($realcnt == 1) {
1985d8aaf121SAndy Whitcroft			$realcnt--;
19860a920b5bSAndy Whitcroft		}
19870a920b5bSAndy Whitcroft
1988cc77cdcaSAndy Whitcroft		my $hunk_line = ($realcnt != 0);
1989cc77cdcaSAndy Whitcroft
19900a920b5bSAndy Whitcroft#make up the handle for any error we report on this line
1991773647a0SAndy Whitcroft		$prefix = "$filename:$realline: " if ($emacs && $file);
1992773647a0SAndy Whitcroft		$prefix = "$filename:$linenr: " if ($emacs && !$file);
1993773647a0SAndy Whitcroft
19946c72ffaaSAndy Whitcroft		$here = "#$linenr: " if (!$file);
19956c72ffaaSAndy Whitcroft		$here = "#$realline: " if ($file);
1996773647a0SAndy Whitcroft
19972ac73b4fSJoe Perches		my $found_file = 0;
1998773647a0SAndy Whitcroft		# extract the filename as it passes
19993bf9a009SRabin Vincent		if ($line =~ /^diff --git.*?(\S+)$/) {
20003bf9a009SRabin Vincent			$realfile = $1;
20012b7ab453SJoe Perches			$realfile =~ s@^([^/]*)/@@ if (!$file);
2002270c49a0SJoe Perches			$in_commit_log = 0;
20032ac73b4fSJoe Perches			$found_file = 1;
20043bf9a009SRabin Vincent		} elsif ($line =~ /^\+\+\+\s+(\S+)/) {
2005773647a0SAndy Whitcroft			$realfile = $1;
20062b7ab453SJoe Perches			$realfile =~ s@^([^/]*)/@@ if (!$file);
2007270c49a0SJoe Perches			$in_commit_log = 0;
20081e855726SWolfram Sang
20091e855726SWolfram Sang			$p1_prefix = $1;
2010e2f7aa4bSAndy Whitcroft			if (!$file && $tree && $p1_prefix ne '' &&
2011e2f7aa4bSAndy Whitcroft			    -e "$root/$p1_prefix") {
2012000d1cc1SJoe Perches				WARN("PATCH_PREFIX",
2013000d1cc1SJoe Perches				     "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
20141e855726SWolfram Sang			}
2015773647a0SAndy Whitcroft
2016c1ab3326SAndy Whitcroft			if ($realfile =~ m@^include/asm/@) {
2017000d1cc1SJoe Perches				ERROR("MODIFIED_INCLUDE_ASM",
2018000d1cc1SJoe Perches				      "do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
2019773647a0SAndy Whitcroft			}
20202ac73b4fSJoe Perches			$found_file = 1;
20212ac73b4fSJoe Perches		}
20222ac73b4fSJoe Perches
20232ac73b4fSJoe Perches		if ($found_file) {
20242ac73b4fSJoe Perches			if ($realfile =~ m@^(drivers/net/|net/)@) {
20252ac73b4fSJoe Perches				$check = 1;
20262ac73b4fSJoe Perches			} else {
20272ac73b4fSJoe Perches				$check = $check_orig;
20282ac73b4fSJoe Perches			}
2029773647a0SAndy Whitcroft			next;
2030773647a0SAndy Whitcroft		}
2031773647a0SAndy Whitcroft
2032389834b6SRandy Dunlap		$here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
20330a920b5bSAndy Whitcroft
2034c2fdda0dSAndy Whitcroft		my $hereline = "$here\n$rawline\n";
2035c2fdda0dSAndy Whitcroft		my $herecurr = "$here\n$rawline\n";
2036c2fdda0dSAndy Whitcroft		my $hereprev = "$here\n$prevrawline\n$rawline\n";
20370a920b5bSAndy Whitcroft
20386c72ffaaSAndy Whitcroft		$cnt_lines++ if ($realcnt != 0);
20396c72ffaaSAndy Whitcroft
20403bf9a009SRabin Vincent# Check for incorrect file permissions
20413bf9a009SRabin Vincent		if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
20423bf9a009SRabin Vincent			my $permhere = $here . "FILE: $realfile\n";
204304db4d25SJoe Perches			if ($realfile !~ m@scripts/@ &&
204404db4d25SJoe Perches			    $realfile !~ /\.(py|pl|awk|sh)$/) {
2045000d1cc1SJoe Perches				ERROR("EXECUTE_PERMISSIONS",
2046000d1cc1SJoe Perches				      "do not set execute permissions for source files\n" . $permhere);
20473bf9a009SRabin Vincent			}
20483bf9a009SRabin Vincent		}
20493bf9a009SRabin Vincent
205020112475SJoe Perches# Check the patch for a signoff:
2051d8aaf121SAndy Whitcroft		if ($line =~ /^\s*signed-off-by:/i) {
20524a0df2efSAndy Whitcroft			$signoff++;
205315662b3eSJoe Perches			$in_commit_log = 0;
20540a920b5bSAndy Whitcroft		}
205520112475SJoe Perches
205620112475SJoe Perches# Check signature styles
2057270c49a0SJoe Perches		if (!$in_header_lines &&
2058ce0338dfSJoe Perches		    $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
205920112475SJoe Perches			my $space_before = $1;
206020112475SJoe Perches			my $sign_off = $2;
206120112475SJoe Perches			my $space_after = $3;
206220112475SJoe Perches			my $email = $4;
206320112475SJoe Perches			my $ucfirst_sign_off = ucfirst(lc($sign_off));
206420112475SJoe Perches
2065ce0338dfSJoe Perches			if ($sign_off !~ /$signature_tags/) {
2066ce0338dfSJoe Perches				WARN("BAD_SIGN_OFF",
2067ce0338dfSJoe Perches				     "Non-standard signature: $sign_off\n" . $herecurr);
2068ce0338dfSJoe Perches			}
206920112475SJoe Perches			if (defined $space_before && $space_before ne "") {
20703705ce5bSJoe Perches				if (WARN("BAD_SIGN_OFF",
20713705ce5bSJoe Perches					 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
20723705ce5bSJoe Perches				    $fix) {
2073194f66fcSJoe Perches					$fixed[$fixlinenr] =
20743705ce5bSJoe Perches					    "$ucfirst_sign_off $email";
20753705ce5bSJoe Perches				}
207620112475SJoe Perches			}
207720112475SJoe Perches			if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
20783705ce5bSJoe Perches				if (WARN("BAD_SIGN_OFF",
20793705ce5bSJoe Perches					 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
20803705ce5bSJoe Perches				    $fix) {
2081194f66fcSJoe Perches					$fixed[$fixlinenr] =
20823705ce5bSJoe Perches					    "$ucfirst_sign_off $email";
20833705ce5bSJoe Perches				}
20843705ce5bSJoe Perches
208520112475SJoe Perches			}
208620112475SJoe Perches			if (!defined $space_after || $space_after ne " ") {
20873705ce5bSJoe Perches				if (WARN("BAD_SIGN_OFF",
20883705ce5bSJoe Perches					 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
20893705ce5bSJoe Perches				    $fix) {
2090194f66fcSJoe Perches					$fixed[$fixlinenr] =
20913705ce5bSJoe Perches					    "$ucfirst_sign_off $email";
20923705ce5bSJoe Perches				}
209320112475SJoe Perches			}
209420112475SJoe Perches
209520112475SJoe Perches			my ($email_name, $email_address, $comment) = parse_email($email);
209620112475SJoe Perches			my $suggested_email = format_email(($email_name, $email_address));
209720112475SJoe Perches			if ($suggested_email eq "") {
2098000d1cc1SJoe Perches				ERROR("BAD_SIGN_OFF",
2099000d1cc1SJoe Perches				      "Unrecognized email address: '$email'\n" . $herecurr);
210020112475SJoe Perches			} else {
210120112475SJoe Perches				my $dequoted = $suggested_email;
210220112475SJoe Perches				$dequoted =~ s/^"//;
210320112475SJoe Perches				$dequoted =~ s/" </ </;
210420112475SJoe Perches				# Don't force email to have quotes
210520112475SJoe Perches				# Allow just an angle bracketed address
210620112475SJoe Perches				if ("$dequoted$comment" ne $email &&
210720112475SJoe Perches				    "<$email_address>$comment" ne $email &&
210820112475SJoe Perches				    "$suggested_email$comment" ne $email) {
2109000d1cc1SJoe Perches					WARN("BAD_SIGN_OFF",
2110000d1cc1SJoe Perches					     "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
211120112475SJoe Perches				}
21120a920b5bSAndy Whitcroft			}
21137e51f197SJoe Perches
21147e51f197SJoe Perches# Check for duplicate signatures
21157e51f197SJoe Perches			my $sig_nospace = $line;
21167e51f197SJoe Perches			$sig_nospace =~ s/\s//g;
21177e51f197SJoe Perches			$sig_nospace = lc($sig_nospace);
21187e51f197SJoe Perches			if (defined $signatures{$sig_nospace}) {
21197e51f197SJoe Perches				WARN("BAD_SIGN_OFF",
21207e51f197SJoe Perches				     "Duplicate signature\n" . $herecurr);
21217e51f197SJoe Perches			} else {
21227e51f197SJoe Perches				$signatures{$sig_nospace} = 1;
21237e51f197SJoe Perches			}
21240a920b5bSAndy Whitcroft		}
21250a920b5bSAndy Whitcroft
21269b3189ebSJoe Perches# Check for old stable address
21279b3189ebSJoe Perches		if ($line =~ /^\s*cc:\s*.*<?\bstable\@kernel\.org\b>?.*$/i) {
21289b3189ebSJoe Perches			ERROR("STABLE_ADDRESS",
21299b3189ebSJoe Perches			      "The 'stable' address should be 'stable\@vger.kernel.org'\n" . $herecurr);
21309b3189ebSJoe Perches		}
21319b3189ebSJoe Perches
21327ebd05efSChristopher Covington# Check for unwanted Gerrit info
21337ebd05efSChristopher Covington		if ($in_commit_log && $line =~ /^\s*change-id:/i) {
21347ebd05efSChristopher Covington			ERROR("GERRIT_CHANGE_ID",
21357ebd05efSChristopher Covington			      "Remove Gerrit Change-Id's before submitting upstream.\n" . $herecurr);
21367ebd05efSChristopher Covington		}
21377ebd05efSChristopher Covington
2138d311cd44SJoe Perches# Check for improperly formed commit descriptions
2139d311cd44SJoe Perches		if ($in_commit_log &&
2140d311cd44SJoe Perches		    $line =~ /\bcommit\s+[0-9a-f]{5,}/i &&
214166881735SJoe Perches		    !($line =~ /\b[Cc]ommit [0-9a-f]{12,40} \("/ ||
214266881735SJoe Perches		      ($line =~ /\b[Cc]ommit [0-9a-f]{12,40}\s*$/ &&
214366881735SJoe Perches		       defined $rawlines[$linenr] &&
214466881735SJoe Perches		       $rawlines[$linenr] =~ /^\s*\("/))) {
2145d311cd44SJoe Perches			$line =~ /\b(c)ommit\s+([0-9a-f]{5,})/i;
2146d311cd44SJoe Perches			my $init_char = $1;
2147d311cd44SJoe Perches			my $orig_commit = lc($2);
2148d311cd44SJoe Perches			my $id = '01234567890ab';
2149d311cd44SJoe Perches			my $desc = 'commit description';
2150d311cd44SJoe Perches		        ($id, $desc) = git_commit_info($orig_commit, $id, $desc);
2151d311cd44SJoe Perches			ERROR("GIT_COMMIT_ID",
21523f6316b4SJoe Perches			      "Please use 12 or more chars for the git commit ID like: '${init_char}ommit $id (\"$desc\")'\n" . $herecurr);
2153d311cd44SJoe Perches		}
2154d311cd44SJoe Perches
215513f1937eSJoe Perches# Check for added, moved or deleted files
215613f1937eSJoe Perches		if (!$reported_maintainer_file && !$in_commit_log &&
215713f1937eSJoe Perches		    ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ ||
215813f1937eSJoe Perches		     $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ ||
215913f1937eSJoe Perches		     ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ &&
216013f1937eSJoe Perches		      (defined($1) || defined($2))))) {
216113f1937eSJoe Perches			$reported_maintainer_file = 1;
216213f1937eSJoe Perches			WARN("FILE_PATH_CHANGES",
216313f1937eSJoe Perches			     "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr);
216413f1937eSJoe Perches		}
216513f1937eSJoe Perches
216600df344fSAndy Whitcroft# Check for wrappage within a valid hunk of the file
21678905a67cSAndy Whitcroft		if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
2168000d1cc1SJoe Perches			ERROR("CORRUPTED_PATCH",
2169000d1cc1SJoe Perches			      "patch seems to be corrupt (line wrapped?)\n" .
21706c72ffaaSAndy Whitcroft				$herecurr) if (!$emitted_corrupt++);
2171de7d4f0eSAndy Whitcroft		}
2172de7d4f0eSAndy Whitcroft
21736ecd9674SAndy Whitcroft# Check for absolute kernel paths.
21746ecd9674SAndy Whitcroft		if ($tree) {
21756ecd9674SAndy Whitcroft			while ($line =~ m{(?:^|\s)(/\S*)}g) {
21766ecd9674SAndy Whitcroft				my $file = $1;
21776ecd9674SAndy Whitcroft
21786ecd9674SAndy Whitcroft				if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
21796ecd9674SAndy Whitcroft				    check_absolute_file($1, $herecurr)) {
21806ecd9674SAndy Whitcroft					#
21816ecd9674SAndy Whitcroft				} else {
21826ecd9674SAndy Whitcroft					check_absolute_file($file, $herecurr);
21836ecd9674SAndy Whitcroft				}
21846ecd9674SAndy Whitcroft			}
21856ecd9674SAndy Whitcroft		}
21866ecd9674SAndy Whitcroft
2187de7d4f0eSAndy Whitcroft# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
2188de7d4f0eSAndy Whitcroft		if (($realfile =~ /^$/ || $line =~ /^\+/) &&
2189171ae1a4SAndy Whitcroft		    $rawline !~ m/^$UTF8*$/) {
2190171ae1a4SAndy Whitcroft			my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
2191171ae1a4SAndy Whitcroft
2192171ae1a4SAndy Whitcroft			my $blank = copy_spacing($rawline);
2193171ae1a4SAndy Whitcroft			my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
2194171ae1a4SAndy Whitcroft			my $hereptr = "$hereline$ptr\n";
2195171ae1a4SAndy Whitcroft
219634d99219SJoe Perches			CHK("INVALID_UTF8",
2197000d1cc1SJoe Perches			    "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
219800df344fSAndy Whitcroft		}
21990a920b5bSAndy Whitcroft
220015662b3eSJoe Perches# Check if it's the start of a commit log
220115662b3eSJoe Perches# (not a header line and we haven't seen the patch filename)
220215662b3eSJoe Perches		if ($in_header_lines && $realfile =~ /^$/ &&
220329ee1b0cSJoe Perches		    !($rawline =~ /^\s+\S/ ||
220429ee1b0cSJoe Perches		      $rawline =~ /^(commit\b|from\b|[\w-]+:).*$/i)) {
220515662b3eSJoe Perches			$in_header_lines = 0;
220615662b3eSJoe Perches			$in_commit_log = 1;
220715662b3eSJoe Perches		}
220815662b3eSJoe Perches
2209fa64205dSPasi Savanainen# Check if there is UTF-8 in a commit log when a mail header has explicitly
2210fa64205dSPasi Savanainen# declined it, i.e defined some charset where it is missing.
2211fa64205dSPasi Savanainen		if ($in_header_lines &&
2212fa64205dSPasi Savanainen		    $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
2213fa64205dSPasi Savanainen		    $1 !~ /utf-8/i) {
2214fa64205dSPasi Savanainen			$non_utf8_charset = 1;
2215fa64205dSPasi Savanainen		}
2216fa64205dSPasi Savanainen
2217fa64205dSPasi Savanainen		if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
221815662b3eSJoe Perches		    $rawline =~ /$NON_ASCII_UTF8/) {
2219fa64205dSPasi Savanainen			WARN("UTF8_BEFORE_PATCH",
222015662b3eSJoe Perches			    "8-bit UTF-8 used in possible commit log\n" . $herecurr);
222115662b3eSJoe Perches		}
222215662b3eSJoe Perches
222330670854SAndy Whitcroft# ignore non-hunk lines and lines being removed
222430670854SAndy Whitcroft		next if (!$hunk_line || $line =~ /^-/);
222500df344fSAndy Whitcroft
22260a920b5bSAndy Whitcroft#trailing whitespace
22279c0ca6f9SAndy Whitcroft		if ($line =~ /^\+.*\015/) {
2228c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2229d5e616fcSJoe Perches			if (ERROR("DOS_LINE_ENDINGS",
2230d5e616fcSJoe Perches				  "DOS line endings\n" . $herevet) &&
2231d5e616fcSJoe Perches			    $fix) {
2232194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/[\s\015]+$//;
2233d5e616fcSJoe Perches			}
2234c2fdda0dSAndy Whitcroft		} elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
2235c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
22363705ce5bSJoe Perches			if (ERROR("TRAILING_WHITESPACE",
22373705ce5bSJoe Perches				  "trailing whitespace\n" . $herevet) &&
22383705ce5bSJoe Perches			    $fix) {
2239194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\s+$//;
22403705ce5bSJoe Perches			}
22413705ce5bSJoe Perches
2242d2c0a235SAndy Whitcroft			$rpt_cleaners = 1;
22430a920b5bSAndy Whitcroft		}
22445368df20SAndy Whitcroft
22454783f894SJosh Triplett# Check for FSF mailing addresses.
2246109d8cb2SAlexander Duyck		if ($rawline =~ /\bwrite to the Free/i ||
22473e2232f2SJoe Perches		    $rawline =~ /\b59\s+Temple\s+Pl/i ||
22483e2232f2SJoe Perches		    $rawline =~ /\b51\s+Franklin\s+St/i) {
22494783f894SJosh Triplett			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
22504783f894SJosh Triplett			my $msg_type = \&ERROR;
22514783f894SJosh Triplett			$msg_type = \&CHK if ($file);
22524783f894SJosh Triplett			&{$msg_type}("FSF_MAILING_ADDRESS",
22534783f894SJosh 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)
22544783f894SJosh Triplett		}
22554783f894SJosh Triplett
22563354957aSAndi Kleen# check for Kconfig help text having a real description
22579fe287d7SAndy Whitcroft# Only applies when adding the entry originally, after that we do not have
22589fe287d7SAndy Whitcroft# sufficient context to determine whether it is indeed long enough.
22593354957aSAndi Kleen		if ($realfile =~ /Kconfig/ &&
22608d73e0e7SJoe Perches		    $line =~ /^\+\s*config\s+/) {
22613354957aSAndi Kleen			my $length = 0;
22629fe287d7SAndy Whitcroft			my $cnt = $realcnt;
22639fe287d7SAndy Whitcroft			my $ln = $linenr + 1;
22649fe287d7SAndy Whitcroft			my $f;
2265a1385803SAndy Whitcroft			my $is_start = 0;
22669fe287d7SAndy Whitcroft			my $is_end = 0;
2267a1385803SAndy Whitcroft			for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
22689fe287d7SAndy Whitcroft				$f = $lines[$ln - 1];
22699fe287d7SAndy Whitcroft				$cnt-- if ($lines[$ln - 1] !~ /^-/);
22709fe287d7SAndy Whitcroft				$is_end = $lines[$ln - 1] =~ /^\+/;
22719fe287d7SAndy Whitcroft
22729fe287d7SAndy Whitcroft				next if ($f =~ /^-/);
22738d73e0e7SJoe Perches				last if (!$file && $f =~ /^\@\@/);
2274a1385803SAndy Whitcroft
22758d73e0e7SJoe Perches				if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate)\s*\"/) {
2276a1385803SAndy Whitcroft					$is_start = 1;
22778d73e0e7SJoe Perches				} elsif ($lines[$ln - 1] =~ /^\+\s*(?:---)?help(?:---)?$/) {
2278a1385803SAndy Whitcroft					$length = -1;
2279a1385803SAndy Whitcroft				}
2280a1385803SAndy Whitcroft
22819fe287d7SAndy Whitcroft				$f =~ s/^.//;
22823354957aSAndi Kleen				$f =~ s/#.*//;
22833354957aSAndi Kleen				$f =~ s/^\s+//;
22843354957aSAndi Kleen				next if ($f =~ /^$/);
22859fe287d7SAndy Whitcroft				if ($f =~ /^\s*config\s/) {
22869fe287d7SAndy Whitcroft					$is_end = 1;
22879fe287d7SAndy Whitcroft					last;
22889fe287d7SAndy Whitcroft				}
22893354957aSAndi Kleen				$length++;
22903354957aSAndi Kleen			}
229156193274SVadim Bendebury			if ($is_start && $is_end && $length < $min_conf_desc_length) {
2292000d1cc1SJoe Perches				WARN("CONFIG_DESCRIPTION",
229356193274SVadim Bendebury				     "please write a paragraph that describes the config symbol fully\n" . $herecurr);
229456193274SVadim Bendebury			}
2295a1385803SAndy Whitcroft			#print "is_start<$is_start> is_end<$is_end> length<$length>\n";
22963354957aSAndi Kleen		}
22973354957aSAndi Kleen
22981ba8dfd1SKees Cook# discourage the addition of CONFIG_EXPERIMENTAL in Kconfig.
22991ba8dfd1SKees Cook		if ($realfile =~ /Kconfig/ &&
23001ba8dfd1SKees Cook		    $line =~ /.\s*depends on\s+.*\bEXPERIMENTAL\b/) {
23011ba8dfd1SKees Cook			WARN("CONFIG_EXPERIMENTAL",
23021ba8dfd1SKees Cook			     "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
23031ba8dfd1SKees Cook		}
23041ba8dfd1SKees Cook
2305c68e5878SArnaud Lacombe		if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
2306c68e5878SArnaud Lacombe		    ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
2307c68e5878SArnaud Lacombe			my $flag = $1;
2308c68e5878SArnaud Lacombe			my $replacement = {
2309c68e5878SArnaud Lacombe				'EXTRA_AFLAGS' =>   'asflags-y',
2310c68e5878SArnaud Lacombe				'EXTRA_CFLAGS' =>   'ccflags-y',
2311c68e5878SArnaud Lacombe				'EXTRA_CPPFLAGS' => 'cppflags-y',
2312c68e5878SArnaud Lacombe				'EXTRA_LDFLAGS' =>  'ldflags-y',
2313c68e5878SArnaud Lacombe			};
2314c68e5878SArnaud Lacombe
2315c68e5878SArnaud Lacombe			WARN("DEPRECATED_VARIABLE",
2316c68e5878SArnaud Lacombe			     "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
2317c68e5878SArnaud Lacombe		}
2318c68e5878SArnaud Lacombe
2319bff5da43SRob Herring# check for DT compatible documentation
23207dd05b38SFlorian Vaussard		if (defined $root &&
23217dd05b38SFlorian Vaussard			(($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) ||
23227dd05b38SFlorian Vaussard			 ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) {
23237dd05b38SFlorian Vaussard
2324bff5da43SRob Herring			my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g;
2325bff5da43SRob Herring
2326cc93319bSFlorian Vaussard			my $dt_path = $root . "/Documentation/devicetree/bindings/";
2327cc93319bSFlorian Vaussard			my $vp_file = $dt_path . "vendor-prefixes.txt";
2328cc93319bSFlorian Vaussard
2329bff5da43SRob Herring			foreach my $compat (@compats) {
2330bff5da43SRob Herring				my $compat2 = $compat;
2331185d566bSRob Herring				$compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/;
2332185d566bSRob Herring				my $compat3 = $compat;
2333185d566bSRob Herring				$compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/;
2334185d566bSRob Herring				`grep -Erq "$compat|$compat2|$compat3" $dt_path`;
2335bff5da43SRob Herring				if ( $? >> 8 ) {
2336bff5da43SRob Herring					WARN("UNDOCUMENTED_DT_STRING",
2337bff5da43SRob Herring					     "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr);
2338bff5da43SRob Herring				}
2339bff5da43SRob Herring
23404fbf32a6SFlorian Vaussard				next if $compat !~ /^([a-zA-Z0-9\-]+)\,/;
23414fbf32a6SFlorian Vaussard				my $vendor = $1;
2342cc93319bSFlorian Vaussard				`grep -Eq "^$vendor\\b" $vp_file`;
2343bff5da43SRob Herring				if ( $? >> 8 ) {
2344bff5da43SRob Herring					WARN("UNDOCUMENTED_DT_STRING",
2345cc93319bSFlorian Vaussard					     "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr);
2346bff5da43SRob Herring				}
2347bff5da43SRob Herring			}
2348bff5da43SRob Herring		}
2349bff5da43SRob Herring
23505368df20SAndy Whitcroft# check we are in a valid source file if not then ignore this hunk
2351de4c924cSGeert Uytterhoeven		next if ($realfile !~ /\.(h|c|s|S|pl|sh|dtsi|dts)$/);
23525368df20SAndy Whitcroft
23536cd7f386SJoe Perches#line length limit
2354c45dcabdSAndy Whitcroft		if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ &&
2355f4c014c0SAndy Whitcroft		    $rawline !~ /^.\s*\*\s*\@$Ident\s/ &&
23560fccc622SJoe Perches		    !($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(KERN_\S+\s*|[^"]*))?"[X\t]*"\s*(?:|,|\)\s*;)\s*$/ ||
23578bbea968SJoe Perches		    $line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) &&
23586cd7f386SJoe Perches		    $length > $max_line_length)
2359c45dcabdSAndy Whitcroft		{
2360000d1cc1SJoe Perches			WARN("LONG_LINE",
23616cd7f386SJoe Perches			     "line over $max_line_length characters\n" . $herecurr);
23620a920b5bSAndy Whitcroft		}
23630a920b5bSAndy Whitcroft
2364ca56dc09SJosh Triplett# Check for user-visible strings broken across lines, which breaks the ability
23658c5fcd24SJoe Perches# to grep for the string.  Make exceptions when the previous string ends in a
23668c5fcd24SJoe Perches# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{'
23678c5fcd24SJoe Perches# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value
2368ca56dc09SJosh Triplett		if ($line =~ /^\+\s*"/ &&
2369ca56dc09SJosh Triplett		    $prevline =~ /"\s*$/ &&
23708c5fcd24SJoe Perches		    $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) {
2371ca56dc09SJosh Triplett			WARN("SPLIT_STRING",
2372ca56dc09SJosh Triplett			     "quoted string split across lines\n" . $hereprev);
2373ca56dc09SJosh Triplett		}
2374ca56dc09SJosh Triplett
2375ece9659fSDan Carpenter# check for missing a space in a string concatination
2376ece9659fSDan Carpenter		if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) {
2377ece9659fSDan Carpenter			WARN('MISSING_SPACE',
2378ece9659fSDan Carpenter			     "break quoted strings at a space character\n" . $hereprev);
2379ece9659fSDan Carpenter		}
2380ece9659fSDan Carpenter
23815e79d96eSJoe Perches# check for spaces before a quoted newline
23825e79d96eSJoe Perches		if ($rawline =~ /^.*\".*\s\\n/) {
23833705ce5bSJoe Perches			if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
23843705ce5bSJoe Perches				 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
23853705ce5bSJoe Perches			    $fix) {
2386194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
23873705ce5bSJoe Perches			}
23883705ce5bSJoe Perches
23895e79d96eSJoe Perches		}
23905e79d96eSJoe Perches
23918905a67cSAndy Whitcroft# check for adding lines without a newline.
23928905a67cSAndy Whitcroft		if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
2393000d1cc1SJoe Perches			WARN("MISSING_EOF_NEWLINE",
2394000d1cc1SJoe Perches			     "adding a line without newline at end of file\n" . $herecurr);
23958905a67cSAndy Whitcroft		}
23968905a67cSAndy Whitcroft
239742e41c54SMike Frysinger# Blackfin: use hi/lo macros
239842e41c54SMike Frysinger		if ($realfile =~ m@arch/blackfin/.*\.S$@) {
239942e41c54SMike Frysinger			if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
240042e41c54SMike Frysinger				my $herevet = "$here\n" . cat_vet($line) . "\n";
2401000d1cc1SJoe Perches				ERROR("LO_MACRO",
2402000d1cc1SJoe Perches				      "use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
240342e41c54SMike Frysinger			}
240442e41c54SMike Frysinger			if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
240542e41c54SMike Frysinger				my $herevet = "$here\n" . cat_vet($line) . "\n";
2406000d1cc1SJoe Perches				ERROR("HI_MACRO",
2407000d1cc1SJoe Perches				      "use the HI() macro, not (... >> 16)\n" . $herevet);
240842e41c54SMike Frysinger			}
240942e41c54SMike Frysinger		}
241042e41c54SMike Frysinger
2411b9ea10d6SAndy Whitcroft# check we are in a valid source file C or perl if not then ignore this hunk
2412de4c924cSGeert Uytterhoeven		next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/);
24130a920b5bSAndy Whitcroft
24140a920b5bSAndy Whitcroft# at the beginning of a line any tabs must come first and anything
24150a920b5bSAndy Whitcroft# more than 8 must use tabs.
2416c2fdda0dSAndy Whitcroft		if ($rawline =~ /^\+\s* \t\s*\S/ ||
2417c2fdda0dSAndy Whitcroft		    $rawline =~ /^\+\s*        \s*/) {
2418c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2419d2c0a235SAndy Whitcroft			$rpt_cleaners = 1;
24203705ce5bSJoe Perches			if (ERROR("CODE_INDENT",
24213705ce5bSJoe Perches				  "code indent should use tabs where possible\n" . $herevet) &&
24223705ce5bSJoe Perches			    $fix) {
2423194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
24243705ce5bSJoe Perches			}
24250a920b5bSAndy Whitcroft		}
24260a920b5bSAndy Whitcroft
242708e44365SAlberto Panizzo# check for space before tabs.
242808e44365SAlberto Panizzo		if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
242908e44365SAlberto Panizzo			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
24303705ce5bSJoe Perches			if (WARN("SPACE_BEFORE_TAB",
24313705ce5bSJoe Perches				"please, no space before tabs\n" . $herevet) &&
24323705ce5bSJoe Perches			    $fix) {
2433194f66fcSJoe Perches				while ($fixed[$fixlinenr] =~
2434*d2207ccbSJoe Perches					   s/(^\+.*) {8,8}\t/$1\t\t/) {}
2435194f66fcSJoe Perches				while ($fixed[$fixlinenr] =~
2436c76f4cb3SJoe Perches					   s/(^\+.*) +\t/$1\t/) {}
24373705ce5bSJoe Perches			}
243808e44365SAlberto Panizzo		}
243908e44365SAlberto Panizzo
2440d1fe9c09SJoe Perches# check for && or || at the start of a line
2441d1fe9c09SJoe Perches		if ($rawline =~ /^\+\s*(&&|\|\|)/) {
2442d1fe9c09SJoe Perches			CHK("LOGICAL_CONTINUATIONS",
2443d1fe9c09SJoe Perches			    "Logical continuations should be on the previous line\n" . $hereprev);
2444d1fe9c09SJoe Perches		}
2445d1fe9c09SJoe Perches
2446d1fe9c09SJoe Perches# check multi-line statement indentation matches previous line
2447d1fe9c09SJoe Perches		if ($^V && $^V ge 5.10.0 &&
244891cb5195SJoe Perches		    $prevline =~ /^\+([ \t]*)((?:$c90_Keywords(?:\s+if)\s*)|(?:$Declare\s*)?(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*|$Ident\s*=\s*$Ident\s*)\(.*(\&\&|\|\||,)\s*$/) {
2449d1fe9c09SJoe Perches			$prevline =~ /^\+(\t*)(.*)$/;
2450d1fe9c09SJoe Perches			my $oldindent = $1;
2451d1fe9c09SJoe Perches			my $rest = $2;
2452d1fe9c09SJoe Perches
2453d1fe9c09SJoe Perches			my $pos = pos_last_openparen($rest);
2454d1fe9c09SJoe Perches			if ($pos >= 0) {
2455b34a26f3SJoe Perches				$line =~ /^(\+| )([ \t]*)/;
2456b34a26f3SJoe Perches				my $newindent = $2;
2457d1fe9c09SJoe Perches
2458d1fe9c09SJoe Perches				my $goodtabindent = $oldindent .
2459d1fe9c09SJoe Perches					"\t" x ($pos / 8) .
2460d1fe9c09SJoe Perches					" "  x ($pos % 8);
2461d1fe9c09SJoe Perches				my $goodspaceindent = $oldindent . " "  x $pos;
2462d1fe9c09SJoe Perches
2463d1fe9c09SJoe Perches				if ($newindent ne $goodtabindent &&
2464d1fe9c09SJoe Perches				    $newindent ne $goodspaceindent) {
24653705ce5bSJoe Perches
24663705ce5bSJoe Perches					if (CHK("PARENTHESIS_ALIGNMENT",
24673705ce5bSJoe Perches						"Alignment should match open parenthesis\n" . $hereprev) &&
24683705ce5bSJoe Perches					    $fix && $line =~ /^\+/) {
2469194f66fcSJoe Perches						$fixed[$fixlinenr] =~
24703705ce5bSJoe Perches						    s/^\+[ \t]*/\+$goodtabindent/;
24713705ce5bSJoe Perches					}
2472d1fe9c09SJoe Perches				}
2473d1fe9c09SJoe Perches			}
2474d1fe9c09SJoe Perches		}
2475d1fe9c09SJoe Perches
2476308cc8d8SJoe Perches		if ($line =~ /^\+.*\(\s*$Type\s*\)[ \t]+(?!$Assignment|$Arithmetic|{)/) {
24773705ce5bSJoe Perches			if (CHK("SPACING",
2478f27c95dbSJoe Perches				"No space is necessary after a cast\n" . $herecurr) &&
24793705ce5bSJoe Perches			    $fix) {
2480194f66fcSJoe Perches				$fixed[$fixlinenr] =~
2481f27c95dbSJoe Perches				    s/(\(\s*$Type\s*\))[ \t]+/$1/;
24823705ce5bSJoe Perches			}
2483aad4f614SJoe Perches		}
2484aad4f614SJoe Perches
248505880600SJoe Perches		if ($realfile =~ m@^(drivers/net/|net/)@ &&
2486fdb4bcd6SJoe Perches		    $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
248785ad978cSJoe Perches		    $rawline =~ /^\+[ \t]*\*/ &&
248885ad978cSJoe Perches		    $realline > 2) {
248905880600SJoe Perches			WARN("NETWORKING_BLOCK_COMMENT_STYLE",
249005880600SJoe Perches			     "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
249105880600SJoe Perches		}
249205880600SJoe Perches
249305880600SJoe Perches		if ($realfile =~ m@^(drivers/net/|net/)@ &&
2494a605e32eSJoe Perches		    $prevrawline =~ /^\+[ \t]*\/\*/ &&		#starting /*
2495a605e32eSJoe Perches		    $prevrawline !~ /\*\/[ \t]*$/ &&		#no trailing */
249661135e96SJoe Perches		    $rawline =~ /^\+/ &&			#line is new
2497a605e32eSJoe Perches		    $rawline !~ /^\+[ \t]*\*/) {		#no leading *
2498a605e32eSJoe Perches			WARN("NETWORKING_BLOCK_COMMENT_STYLE",
2499a605e32eSJoe Perches			     "networking block comments start with * on subsequent lines\n" . $hereprev);
2500a605e32eSJoe Perches		}
2501a605e32eSJoe Perches
2502a605e32eSJoe Perches		if ($realfile =~ m@^(drivers/net/|net/)@ &&
2503c24f9f19SJoe Perches		    $rawline !~ m@^\+[ \t]*\*/[ \t]*$@ &&	#trailing */
2504c24f9f19SJoe Perches		    $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ &&	#inline /*...*/
2505c24f9f19SJoe Perches		    $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ &&	#trailing **/
2506c24f9f19SJoe Perches		    $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) {	#non blank */
250705880600SJoe Perches			WARN("NETWORKING_BLOCK_COMMENT_STYLE",
250805880600SJoe Perches			     "networking block comments put the trailing */ on a separate line\n" . $herecurr);
250905880600SJoe Perches		}
251005880600SJoe Perches
25117f619191SJoe Perches# check for missing blank lines after struct/union declarations
25127f619191SJoe Perches# with exceptions for various attributes and macros
25137f619191SJoe Perches		if ($prevline =~ /^[\+ ]};?\s*$/ &&
25147f619191SJoe Perches		    $line =~ /^\+/ &&
25157f619191SJoe Perches		    !($line =~ /^\+\s*$/ ||
25167f619191SJoe Perches		      $line =~ /^\+\s*EXPORT_SYMBOL/ ||
25177f619191SJoe Perches		      $line =~ /^\+\s*MODULE_/i ||
25187f619191SJoe Perches		      $line =~ /^\+\s*\#\s*(?:end|elif|else)/ ||
25197f619191SJoe Perches		      $line =~ /^\+[a-z_]*init/ ||
25207f619191SJoe Perches		      $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ ||
25217f619191SJoe Perches		      $line =~ /^\+\s*DECLARE/ ||
25227f619191SJoe Perches		      $line =~ /^\+\s*__setup/)) {
2523d752fcc8SJoe Perches			if (CHK("LINE_SPACING",
2524d752fcc8SJoe Perches				"Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) &&
2525d752fcc8SJoe Perches			    $fix) {
2526f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, "\+");
2527d752fcc8SJoe Perches			}
25287f619191SJoe Perches		}
25297f619191SJoe Perches
2530365dd4eaSJoe Perches# check for multiple consecutive blank lines
2531365dd4eaSJoe Perches		if ($prevline =~ /^[\+ ]\s*$/ &&
2532365dd4eaSJoe Perches		    $line =~ /^\+\s*$/ &&
2533365dd4eaSJoe Perches		    $last_blank_line != ($linenr - 1)) {
2534d752fcc8SJoe Perches			if (CHK("LINE_SPACING",
2535d752fcc8SJoe Perches				"Please don't use multiple blank lines\n" . $hereprev) &&
2536d752fcc8SJoe Perches			    $fix) {
2537f2d7e4d4SJoe Perches				fix_delete_line($fixlinenr, $rawline);
2538d752fcc8SJoe Perches			}
2539d752fcc8SJoe Perches
2540365dd4eaSJoe Perches			$last_blank_line = $linenr;
2541365dd4eaSJoe Perches		}
2542365dd4eaSJoe Perches
25433b617e3bSJoe Perches# check for missing blank lines after declarations
25443f7bac03SJoe Perches		if ($sline =~ /^\+\s+\S/ &&			#Not at char 1
25453f7bac03SJoe Perches			# actual declarations
25463f7bac03SJoe Perches		    ($prevline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
25475a4e1fd3SJoe Perches			# function pointer declarations
25485a4e1fd3SJoe Perches		     $prevline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
25493f7bac03SJoe Perches			# foo bar; where foo is some local typedef or #define
25503f7bac03SJoe Perches		     $prevline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
25513f7bac03SJoe Perches			# known declaration macros
25523f7bac03SJoe Perches		     $prevline =~ /^\+\s+$declaration_macros/) &&
25533f7bac03SJoe Perches			# for "else if" which can look like "$Ident $Ident"
25543f7bac03SJoe Perches		    !($prevline =~ /^\+\s+$c90_Keywords\b/ ||
25553f7bac03SJoe Perches			# other possible extensions of declaration lines
25563f7bac03SJoe Perches		      $prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ ||
25573f7bac03SJoe Perches			# not starting a section or a macro "\" extended line
25583f7bac03SJoe Perches		      $prevline =~ /(?:\{\s*|\\)$/) &&
25593f7bac03SJoe Perches			# looks like a declaration
25603f7bac03SJoe Perches		    !($sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
25615a4e1fd3SJoe Perches			# function pointer declarations
25625a4e1fd3SJoe Perches		      $sline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
25633f7bac03SJoe Perches			# foo bar; where foo is some local typedef or #define
25643f7bac03SJoe Perches		      $sline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
25653f7bac03SJoe Perches			# known declaration macros
25663f7bac03SJoe Perches		      $sline =~ /^\+\s+$declaration_macros/ ||
25673f7bac03SJoe Perches			# start of struct or union or enum
25683b617e3bSJoe Perches		      $sline =~ /^\+\s+(?:union|struct|enum|typedef)\b/ ||
25693f7bac03SJoe Perches			# start or end of block or continuation of declaration
25703f7bac03SJoe Perches		      $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ ||
25713f7bac03SJoe Perches			# bitfield continuation
25723f7bac03SJoe Perches		      $sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ ||
25733f7bac03SJoe Perches			# other possible extensions of declaration lines
25743f7bac03SJoe Perches		      $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) &&
25753f7bac03SJoe Perches			# indentation of previous and current line are the same
25763f7bac03SJoe Perches		    (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) {
2577d752fcc8SJoe Perches			if (WARN("LINE_SPACING",
2578d752fcc8SJoe Perches				 "Missing a blank line after declarations\n" . $hereprev) &&
2579d752fcc8SJoe Perches			    $fix) {
2580f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, "\+");
2581d752fcc8SJoe Perches			}
25823b617e3bSJoe Perches		}
25833b617e3bSJoe Perches
25845f7ddae6SRaffaele Recalcati# check for spaces at the beginning of a line.
25856b4c5bebSAndy Whitcroft# Exceptions:
25866b4c5bebSAndy Whitcroft#  1) within comments
25876b4c5bebSAndy Whitcroft#  2) indented preprocessor commands
25886b4c5bebSAndy Whitcroft#  3) hanging labels
25893705ce5bSJoe Perches		if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/)  {
25905f7ddae6SRaffaele Recalcati			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
25913705ce5bSJoe Perches			if (WARN("LEADING_SPACE",
25923705ce5bSJoe Perches				 "please, no spaces at the start of a line\n" . $herevet) &&
25933705ce5bSJoe Perches			    $fix) {
2594194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
25953705ce5bSJoe Perches			}
25965f7ddae6SRaffaele Recalcati		}
25975f7ddae6SRaffaele Recalcati
2598b9ea10d6SAndy Whitcroft# check we are in a valid C source file if not then ignore this hunk
2599b9ea10d6SAndy Whitcroft		next if ($realfile !~ /\.(h|c)$/);
2600b9ea10d6SAndy Whitcroft
2601032a4c0fSJoe Perches# check indentation of any line with a bare else
2602032a4c0fSJoe Perches# if the previous line is a break or return and is indented 1 tab more...
2603032a4c0fSJoe Perches		if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {
2604032a4c0fSJoe Perches			my $tabs = length($1) + 1;
2605032a4c0fSJoe Perches			if ($prevline =~ /^\+\t{$tabs,$tabs}(?:break|return)\b/) {
2606032a4c0fSJoe Perches				WARN("UNNECESSARY_ELSE",
2607032a4c0fSJoe Perches				     "else is not generally useful after a break or return\n" . $hereprev);
2608032a4c0fSJoe Perches			}
2609032a4c0fSJoe Perches		}
2610032a4c0fSJoe Perches
2611c00df19aSJoe Perches# check indentation of a line with a break;
2612c00df19aSJoe Perches# if the previous line is a goto or return and is indented the same # of tabs
2613c00df19aSJoe Perches		if ($sline =~ /^\+([\t]+)break\s*;\s*$/) {
2614c00df19aSJoe Perches			my $tabs = $1;
2615c00df19aSJoe Perches			if ($prevline =~ /^\+$tabs(?:goto|return)\b/) {
2616c00df19aSJoe Perches				WARN("UNNECESSARY_BREAK",
2617c00df19aSJoe Perches				     "break is not useful after a goto or return\n" . $hereprev);
2618c00df19aSJoe Perches			}
2619c00df19aSJoe Perches		}
2620c00df19aSJoe Perches
26211ba8dfd1SKees Cook# discourage the addition of CONFIG_EXPERIMENTAL in #if(def).
26221ba8dfd1SKees Cook		if ($line =~ /^\+\s*\#\s*if.*\bCONFIG_EXPERIMENTAL\b/) {
26231ba8dfd1SKees Cook			WARN("CONFIG_EXPERIMENTAL",
26241ba8dfd1SKees Cook			     "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
26251ba8dfd1SKees Cook		}
26261ba8dfd1SKees Cook
2627c2fdda0dSAndy Whitcroft# check for RCS/CVS revision markers
2628cf655043SAndy Whitcroft		if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
2629000d1cc1SJoe Perches			WARN("CVS_KEYWORD",
2630000d1cc1SJoe Perches			     "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
2631c2fdda0dSAndy Whitcroft		}
263222f2a2efSAndy Whitcroft
263342e41c54SMike Frysinger# Blackfin: don't use __builtin_bfin_[cs]sync
263442e41c54SMike Frysinger		if ($line =~ /__builtin_bfin_csync/) {
263542e41c54SMike Frysinger			my $herevet = "$here\n" . cat_vet($line) . "\n";
2636000d1cc1SJoe Perches			ERROR("CSYNC",
2637000d1cc1SJoe Perches			      "use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
263842e41c54SMike Frysinger		}
263942e41c54SMike Frysinger		if ($line =~ /__builtin_bfin_ssync/) {
264042e41c54SMike Frysinger			my $herevet = "$here\n" . cat_vet($line) . "\n";
2641000d1cc1SJoe Perches			ERROR("SSYNC",
2642000d1cc1SJoe Perches			      "use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
264342e41c54SMike Frysinger		}
264442e41c54SMike Frysinger
264556e77d70SJoe Perches# check for old HOTPLUG __dev<foo> section markings
264656e77d70SJoe Perches		if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
264756e77d70SJoe Perches			WARN("HOTPLUG_SECTION",
264856e77d70SJoe Perches			     "Using $1 is unnecessary\n" . $herecurr);
264956e77d70SJoe Perches		}
265056e77d70SJoe Perches
26519c0ca6f9SAndy Whitcroft# Check for potential 'bare' types
26522b474a1aSAndy Whitcroft		my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
26532b474a1aSAndy Whitcroft		    $realline_next);
26543e469cdcSAndy Whitcroft#print "LINE<$line>\n";
26553e469cdcSAndy Whitcroft		if ($linenr >= $suppress_statement &&
26561b5539b1SJoe Perches		    $realcnt && $sline =~ /.\s*\S/) {
2657170d3a22SAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
2658f5fe35ddSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0);
2659171ae1a4SAndy Whitcroft			$stat =~ s/\n./\n /g;
2660171ae1a4SAndy Whitcroft			$cond =~ s/\n./\n /g;
2661171ae1a4SAndy Whitcroft
26623e469cdcSAndy Whitcroft#print "linenr<$linenr> <$stat>\n";
26633e469cdcSAndy Whitcroft			# If this statement has no statement boundaries within
26643e469cdcSAndy Whitcroft			# it there is no point in retrying a statement scan
26653e469cdcSAndy Whitcroft			# until we hit end of it.
26663e469cdcSAndy Whitcroft			my $frag = $stat; $frag =~ s/;+\s*$//;
26673e469cdcSAndy Whitcroft			if ($frag !~ /(?:{|;)/) {
26683e469cdcSAndy Whitcroft#print "skip<$line_nr_next>\n";
26693e469cdcSAndy Whitcroft				$suppress_statement = $line_nr_next;
26703e469cdcSAndy Whitcroft			}
2671f74bd194SAndy Whitcroft
26722b474a1aSAndy Whitcroft			# Find the real next line.
26732b474a1aSAndy Whitcroft			$realline_next = $line_nr_next;
26742b474a1aSAndy Whitcroft			if (defined $realline_next &&
26752b474a1aSAndy Whitcroft			    (!defined $lines[$realline_next - 1] ||
26762b474a1aSAndy Whitcroft			     substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
26772b474a1aSAndy Whitcroft				$realline_next++;
26782b474a1aSAndy Whitcroft			}
26792b474a1aSAndy Whitcroft
2680171ae1a4SAndy Whitcroft			my $s = $stat;
2681171ae1a4SAndy Whitcroft			$s =~ s/{.*$//s;
2682cf655043SAndy Whitcroft
2683c2fdda0dSAndy Whitcroft			# Ignore goto labels.
2684171ae1a4SAndy Whitcroft			if ($s =~ /$Ident:\*$/s) {
2685c2fdda0dSAndy Whitcroft
2686c2fdda0dSAndy Whitcroft			# Ignore functions being called
2687171ae1a4SAndy Whitcroft			} elsif ($s =~ /^.\s*$Ident\s*\(/s) {
2688c2fdda0dSAndy Whitcroft
2689463f2864SAndy Whitcroft			} elsif ($s =~ /^.\s*else\b/s) {
2690463f2864SAndy Whitcroft
2691c45dcabdSAndy Whitcroft			# declarations always start with types
2692d2506586SAndy 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) {
2693c45dcabdSAndy Whitcroft				my $type = $1;
2694c45dcabdSAndy Whitcroft				$type =~ s/\s+/ /g;
2695c45dcabdSAndy Whitcroft				possible($type, "A:" . $s);
2696c45dcabdSAndy Whitcroft
26976c72ffaaSAndy Whitcroft			# definitions in global scope can only start with types
2698a6a84062SAndy Whitcroft			} elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
2699c45dcabdSAndy Whitcroft				possible($1, "B:" . $s);
2700c2fdda0dSAndy Whitcroft			}
27018905a67cSAndy Whitcroft
27026c72ffaaSAndy Whitcroft			# any (foo ... *) is a pointer cast, and foo is a type
270365863862SAndy Whitcroft			while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
2704c45dcabdSAndy Whitcroft				possible($1, "C:" . $s);
27059c0ca6f9SAndy Whitcroft			}
27068905a67cSAndy Whitcroft
27078905a67cSAndy Whitcroft			# Check for any sort of function declaration.
27088905a67cSAndy Whitcroft			# int foo(something bar, other baz);
27098905a67cSAndy Whitcroft			# void (*store_gdt)(x86_descr_ptr *);
2710171ae1a4SAndy Whitcroft			if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
27118905a67cSAndy Whitcroft				my ($name_len) = length($1);
27128905a67cSAndy Whitcroft
2713cf655043SAndy Whitcroft				my $ctx = $s;
2714773647a0SAndy Whitcroft				substr($ctx, 0, $name_len + 1, '');
27158905a67cSAndy Whitcroft				$ctx =~ s/\)[^\)]*$//;
2716cf655043SAndy Whitcroft
27178905a67cSAndy Whitcroft				for my $arg (split(/\s*,\s*/, $ctx)) {
2718c45dcabdSAndy Whitcroft					if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
27198905a67cSAndy Whitcroft
2720c45dcabdSAndy Whitcroft						possible($1, "D:" . $s);
27218905a67cSAndy Whitcroft					}
27228905a67cSAndy Whitcroft				}
27238905a67cSAndy Whitcroft			}
27248905a67cSAndy Whitcroft
27259c0ca6f9SAndy Whitcroft		}
27269c0ca6f9SAndy Whitcroft
272700df344fSAndy Whitcroft#
272800df344fSAndy Whitcroft# Checks which may be anchored in the context.
272900df344fSAndy Whitcroft#
273000df344fSAndy Whitcroft
273100df344fSAndy Whitcroft# Check for switch () and associated case and default
273200df344fSAndy Whitcroft# statements should be at the same indent.
273300df344fSAndy Whitcroft		if ($line=~/\bswitch\s*\(.*\)/) {
273400df344fSAndy Whitcroft			my $err = '';
273500df344fSAndy Whitcroft			my $sep = '';
273600df344fSAndy Whitcroft			my @ctx = ctx_block_outer($linenr, $realcnt);
273700df344fSAndy Whitcroft			shift(@ctx);
273800df344fSAndy Whitcroft			for my $ctx (@ctx) {
273900df344fSAndy Whitcroft				my ($clen, $cindent) = line_stats($ctx);
274000df344fSAndy Whitcroft				if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
274100df344fSAndy Whitcroft							$indent != $cindent) {
274200df344fSAndy Whitcroft					$err .= "$sep$ctx\n";
274300df344fSAndy Whitcroft					$sep = '';
274400df344fSAndy Whitcroft				} else {
274500df344fSAndy Whitcroft					$sep = "[...]\n";
274600df344fSAndy Whitcroft				}
274700df344fSAndy Whitcroft			}
274800df344fSAndy Whitcroft			if ($err ne '') {
2749000d1cc1SJoe Perches				ERROR("SWITCH_CASE_INDENT_LEVEL",
2750000d1cc1SJoe Perches				      "switch and case should be at the same indent\n$hereline$err");
2751de7d4f0eSAndy Whitcroft			}
2752de7d4f0eSAndy Whitcroft		}
2753de7d4f0eSAndy Whitcroft
2754de7d4f0eSAndy Whitcroft# if/while/etc brace do not go on next line, unless defining a do while loop,
2755de7d4f0eSAndy Whitcroft# or if that brace on the next line is for something else
27560fe3dc2bSJoe Perches		if ($line =~ /(.*)\b((?:if|while|for|switch|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
2757773647a0SAndy Whitcroft			my $pre_ctx = "$1$2";
2758773647a0SAndy Whitcroft
27599c0ca6f9SAndy Whitcroft			my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
27608eef05ddSJoe Perches
27618eef05ddSJoe Perches			if ($line =~ /^\+\t{6,}/) {
27628eef05ddSJoe Perches				WARN("DEEP_INDENTATION",
27638eef05ddSJoe Perches				     "Too many leading tabs - consider code refactoring\n" . $herecurr);
27648eef05ddSJoe Perches			}
27658eef05ddSJoe Perches
2766de7d4f0eSAndy Whitcroft			my $ctx_cnt = $realcnt - $#ctx - 1;
2767de7d4f0eSAndy Whitcroft			my $ctx = join("\n", @ctx);
2768de7d4f0eSAndy Whitcroft
2769548596d5SAndy Whitcroft			my $ctx_ln = $linenr;
2770548596d5SAndy Whitcroft			my $ctx_skip = $realcnt;
2771de7d4f0eSAndy Whitcroft
2772548596d5SAndy Whitcroft			while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
2773548596d5SAndy Whitcroft					defined $lines[$ctx_ln - 1] &&
2774548596d5SAndy Whitcroft					$lines[$ctx_ln - 1] =~ /^-/)) {
2775548596d5SAndy Whitcroft				##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
2776548596d5SAndy Whitcroft				$ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
2777773647a0SAndy Whitcroft				$ctx_ln++;
2778773647a0SAndy Whitcroft			}
2779548596d5SAndy Whitcroft
278053210168SAndy Whitcroft			#print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
278153210168SAndy Whitcroft			#print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
2782773647a0SAndy Whitcroft
2783773647a0SAndy Whitcroft			if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
2784000d1cc1SJoe Perches				ERROR("OPEN_BRACE",
2785000d1cc1SJoe Perches				      "that open brace { should be on the previous line\n" .
278601464f30SAndy Whitcroft					"$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
278700df344fSAndy Whitcroft			}
2788773647a0SAndy Whitcroft			if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
2789773647a0SAndy Whitcroft			    $ctx =~ /\)\s*\;\s*$/ &&
2790773647a0SAndy Whitcroft			    defined $lines[$ctx_ln - 1])
2791773647a0SAndy Whitcroft			{
27929c0ca6f9SAndy Whitcroft				my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
27939c0ca6f9SAndy Whitcroft				if ($nindent > $indent) {
2794000d1cc1SJoe Perches					WARN("TRAILING_SEMICOLON",
2795000d1cc1SJoe Perches					     "trailing semicolon indicates no statements, indent implies otherwise\n" .
279601464f30SAndy Whitcroft						"$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
27979c0ca6f9SAndy Whitcroft				}
27989c0ca6f9SAndy Whitcroft			}
279900df344fSAndy Whitcroft		}
280000df344fSAndy Whitcroft
28014d001e4dSAndy Whitcroft# Check relative indent for conditionals and blocks.
28020fe3dc2bSJoe Perches		if ($line =~ /\b(?:(?:if|while|for|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
28033e469cdcSAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
28043e469cdcSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0)
28053e469cdcSAndy Whitcroft					if (!defined $stat);
28064d001e4dSAndy Whitcroft			my ($s, $c) = ($stat, $cond);
28074d001e4dSAndy Whitcroft
28084d001e4dSAndy Whitcroft			substr($s, 0, length($c), '');
28094d001e4dSAndy Whitcroft
28104d001e4dSAndy Whitcroft			# Make sure we remove the line prefixes as we have
28114d001e4dSAndy Whitcroft			# none on the first line, and are going to readd them
28124d001e4dSAndy Whitcroft			# where necessary.
28134d001e4dSAndy Whitcroft			$s =~ s/\n./\n/gs;
28144d001e4dSAndy Whitcroft
28154d001e4dSAndy Whitcroft			# Find out how long the conditional actually is.
28166f779c18SAndy Whitcroft			my @newlines = ($c =~ /\n/gs);
28176f779c18SAndy Whitcroft			my $cond_lines = 1 + $#newlines;
28184d001e4dSAndy Whitcroft
28194d001e4dSAndy Whitcroft			# We want to check the first line inside the block
28204d001e4dSAndy Whitcroft			# starting at the end of the conditional, so remove:
28214d001e4dSAndy Whitcroft			#  1) any blank line termination
28224d001e4dSAndy Whitcroft			#  2) any opening brace { on end of the line
28234d001e4dSAndy Whitcroft			#  3) any do (...) {
28244d001e4dSAndy Whitcroft			my $continuation = 0;
28254d001e4dSAndy Whitcroft			my $check = 0;
28264d001e4dSAndy Whitcroft			$s =~ s/^.*\bdo\b//;
28274d001e4dSAndy Whitcroft			$s =~ s/^\s*{//;
28284d001e4dSAndy Whitcroft			if ($s =~ s/^\s*\\//) {
28294d001e4dSAndy Whitcroft				$continuation = 1;
28304d001e4dSAndy Whitcroft			}
28319bd49efeSAndy Whitcroft			if ($s =~ s/^\s*?\n//) {
28324d001e4dSAndy Whitcroft				$check = 1;
28334d001e4dSAndy Whitcroft				$cond_lines++;
28344d001e4dSAndy Whitcroft			}
28354d001e4dSAndy Whitcroft
28364d001e4dSAndy Whitcroft			# Also ignore a loop construct at the end of a
28374d001e4dSAndy Whitcroft			# preprocessor statement.
28384d001e4dSAndy Whitcroft			if (($prevline =~ /^.\s*#\s*define\s/ ||
28394d001e4dSAndy Whitcroft			    $prevline =~ /\\\s*$/) && $continuation == 0) {
28404d001e4dSAndy Whitcroft				$check = 0;
28414d001e4dSAndy Whitcroft			}
28424d001e4dSAndy Whitcroft
28439bd49efeSAndy Whitcroft			my $cond_ptr = -1;
2844740504c6SAndy Whitcroft			$continuation = 0;
28459bd49efeSAndy Whitcroft			while ($cond_ptr != $cond_lines) {
28469bd49efeSAndy Whitcroft				$cond_ptr = $cond_lines;
28474d001e4dSAndy Whitcroft
2848f16fa28fSAndy Whitcroft				# If we see an #else/#elif then the code
2849f16fa28fSAndy Whitcroft				# is not linear.
2850f16fa28fSAndy Whitcroft				if ($s =~ /^\s*\#\s*(?:else|elif)/) {
2851f16fa28fSAndy Whitcroft					$check = 0;
2852f16fa28fSAndy Whitcroft				}
2853f16fa28fSAndy Whitcroft
28549bd49efeSAndy Whitcroft				# Ignore:
28559bd49efeSAndy Whitcroft				#  1) blank lines, they should be at 0,
28569bd49efeSAndy Whitcroft				#  2) preprocessor lines, and
28579bd49efeSAndy Whitcroft				#  3) labels.
2858740504c6SAndy Whitcroft				if ($continuation ||
2859740504c6SAndy Whitcroft				    $s =~ /^\s*?\n/ ||
28609bd49efeSAndy Whitcroft				    $s =~ /^\s*#\s*?/ ||
28619bd49efeSAndy Whitcroft				    $s =~ /^\s*$Ident\s*:/) {
2862740504c6SAndy Whitcroft					$continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
286330dad6ebSAndy Whitcroft					if ($s =~ s/^.*?\n//) {
28649bd49efeSAndy Whitcroft						$cond_lines++;
28659bd49efeSAndy Whitcroft					}
28664d001e4dSAndy Whitcroft				}
286730dad6ebSAndy Whitcroft			}
28684d001e4dSAndy Whitcroft
28694d001e4dSAndy Whitcroft			my (undef, $sindent) = line_stats("+" . $s);
28704d001e4dSAndy Whitcroft			my $stat_real = raw_line($linenr, $cond_lines);
28714d001e4dSAndy Whitcroft
28724d001e4dSAndy Whitcroft			# Check if either of these lines are modified, else
28734d001e4dSAndy Whitcroft			# this is not this patch's fault.
28744d001e4dSAndy Whitcroft			if (!defined($stat_real) ||
28754d001e4dSAndy Whitcroft			    $stat !~ /^\+/ && $stat_real !~ /^\+/) {
28764d001e4dSAndy Whitcroft				$check = 0;
28774d001e4dSAndy Whitcroft			}
28784d001e4dSAndy Whitcroft			if (defined($stat_real) && $cond_lines > 1) {
28794d001e4dSAndy Whitcroft				$stat_real = "[...]\n$stat_real";
28804d001e4dSAndy Whitcroft			}
28814d001e4dSAndy Whitcroft
28829bd49efeSAndy 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";
28834d001e4dSAndy Whitcroft
28844d001e4dSAndy Whitcroft			if ($check && (($sindent % 8) != 0 ||
28854d001e4dSAndy Whitcroft			    ($sindent <= $indent && $s ne ''))) {
2886000d1cc1SJoe Perches				WARN("SUSPECT_CODE_INDENT",
2887000d1cc1SJoe Perches				     "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
28884d001e4dSAndy Whitcroft			}
28894d001e4dSAndy Whitcroft		}
28904d001e4dSAndy Whitcroft
28916c72ffaaSAndy Whitcroft		# Track the 'values' across context and added lines.
28926c72ffaaSAndy Whitcroft		my $opline = $line; $opline =~ s/^./ /;
28931f65f947SAndy Whitcroft		my ($curr_values, $curr_vars) =
28941f65f947SAndy Whitcroft				annotate_values($opline . "\n", $prev_values);
28956c72ffaaSAndy Whitcroft		$curr_values = $prev_values . $curr_values;
2896c2fdda0dSAndy Whitcroft		if ($dbg_values) {
2897c2fdda0dSAndy Whitcroft			my $outline = $opline; $outline =~ s/\t/ /g;
2898cf655043SAndy Whitcroft			print "$linenr > .$outline\n";
2899cf655043SAndy Whitcroft			print "$linenr > $curr_values\n";
29001f65f947SAndy Whitcroft			print "$linenr >  $curr_vars\n";
2901c2fdda0dSAndy Whitcroft		}
29026c72ffaaSAndy Whitcroft		$prev_values = substr($curr_values, -1);
29036c72ffaaSAndy Whitcroft
290400df344fSAndy Whitcroft#ignore lines not being added
29053705ce5bSJoe Perches		next if ($line =~ /^[^\+]/);
290600df344fSAndy Whitcroft
2907653d4876SAndy Whitcroft# TEST: allow direct testing of the type matcher.
29087429c690SAndy Whitcroft		if ($dbg_type) {
29097429c690SAndy Whitcroft			if ($line =~ /^.\s*$Declare\s*$/) {
2910000d1cc1SJoe Perches				ERROR("TEST_TYPE",
2911000d1cc1SJoe Perches				      "TEST: is type\n" . $herecurr);
29127429c690SAndy Whitcroft			} elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
2913000d1cc1SJoe Perches				ERROR("TEST_NOT_TYPE",
2914000d1cc1SJoe Perches				      "TEST: is not type ($1 is)\n". $herecurr);
29157429c690SAndy Whitcroft			}
2916653d4876SAndy Whitcroft			next;
2917653d4876SAndy Whitcroft		}
2918a1ef277eSAndy Whitcroft# TEST: allow direct testing of the attribute matcher.
2919a1ef277eSAndy Whitcroft		if ($dbg_attr) {
29209360b0e5SAndy Whitcroft			if ($line =~ /^.\s*$Modifier\s*$/) {
2921000d1cc1SJoe Perches				ERROR("TEST_ATTR",
2922000d1cc1SJoe Perches				      "TEST: is attr\n" . $herecurr);
29239360b0e5SAndy Whitcroft			} elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
2924000d1cc1SJoe Perches				ERROR("TEST_NOT_ATTR",
2925000d1cc1SJoe Perches				      "TEST: is not attr ($1 is)\n". $herecurr);
2926a1ef277eSAndy Whitcroft			}
2927a1ef277eSAndy Whitcroft			next;
2928a1ef277eSAndy Whitcroft		}
2929653d4876SAndy Whitcroft
2930f0a594c1SAndy Whitcroft# check for initialisation to aggregates open brace on the next line
293199423c20SAndy Whitcroft		if ($line =~ /^.\s*{/ &&
293299423c20SAndy Whitcroft		    $prevline =~ /(?:^|[^=])=\s*$/) {
2933d752fcc8SJoe Perches			if (ERROR("OPEN_BRACE",
2934d752fcc8SJoe Perches				  "that open brace { should be on the previous line\n" . $hereprev) &&
2935f2d7e4d4SJoe Perches			    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
2936f2d7e4d4SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
2937f2d7e4d4SJoe Perches				fix_delete_line($fixlinenr, $rawline);
2938d752fcc8SJoe Perches				my $fixedline = $prevrawline;
2939d752fcc8SJoe Perches				$fixedline =~ s/\s*=\s*$/ = {/;
2940f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
2941d752fcc8SJoe Perches				$fixedline = $line;
2942d752fcc8SJoe Perches				$fixedline =~ s/^(.\s*){\s*/$1/;
2943f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
2944d752fcc8SJoe Perches			}
2945f0a594c1SAndy Whitcroft		}
2946f0a594c1SAndy Whitcroft
294700df344fSAndy Whitcroft#
294800df344fSAndy Whitcroft# Checks which are anchored on the added line.
294900df344fSAndy Whitcroft#
295000df344fSAndy Whitcroft
2951653d4876SAndy Whitcroft# check for malformed paths in #include statements (uses RAW line)
2952c45dcabdSAndy Whitcroft		if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
2953653d4876SAndy Whitcroft			my $path = $1;
2954653d4876SAndy Whitcroft			if ($path =~ m{//}) {
2955000d1cc1SJoe Perches				ERROR("MALFORMED_INCLUDE",
2956495e9d84SJoe Perches				      "malformed #include filename\n" . $herecurr);
2957495e9d84SJoe Perches			}
2958495e9d84SJoe Perches			if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
2959495e9d84SJoe Perches				ERROR("UAPI_INCLUDE",
2960495e9d84SJoe Perches				      "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
2961653d4876SAndy Whitcroft			}
2962653d4876SAndy Whitcroft		}
2963653d4876SAndy Whitcroft
296400df344fSAndy Whitcroft# no C99 // comments
296500df344fSAndy Whitcroft		if ($line =~ m{//}) {
29663705ce5bSJoe Perches			if (ERROR("C99_COMMENTS",
29673705ce5bSJoe Perches				  "do not use C99 // comments\n" . $herecurr) &&
29683705ce5bSJoe Perches			    $fix) {
2969194f66fcSJoe Perches				my $line = $fixed[$fixlinenr];
29703705ce5bSJoe Perches				if ($line =~ /\/\/(.*)$/) {
29713705ce5bSJoe Perches					my $comment = trim($1);
2972194f66fcSJoe Perches					$fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@;
29733705ce5bSJoe Perches				}
29743705ce5bSJoe Perches			}
297500df344fSAndy Whitcroft		}
297600df344fSAndy Whitcroft		# Remove C99 comments.
29770a920b5bSAndy Whitcroft		$line =~ s@//.*@@;
29786c72ffaaSAndy Whitcroft		$opline =~ s@//.*@@;
29790a920b5bSAndy Whitcroft
29802b474a1aSAndy Whitcroft# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
29812b474a1aSAndy Whitcroft# the whole statement.
29822b474a1aSAndy Whitcroft#print "APW <$lines[$realline_next - 1]>\n";
29832b474a1aSAndy Whitcroft		if (defined $realline_next &&
29842b474a1aSAndy Whitcroft		    exists $lines[$realline_next - 1] &&
29852b474a1aSAndy Whitcroft		    !defined $suppress_export{$realline_next} &&
29862b474a1aSAndy Whitcroft		    ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
29872b474a1aSAndy Whitcroft		     $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
29883cbf62dfSAndy Whitcroft			# Handle definitions which produce identifiers with
29893cbf62dfSAndy Whitcroft			# a prefix:
29903cbf62dfSAndy Whitcroft			#   XXX(foo);
29913cbf62dfSAndy Whitcroft			#   EXPORT_SYMBOL(something_foo);
2992653d4876SAndy Whitcroft			my $name = $1;
299387a53877SAndy Whitcroft			if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
29943cbf62dfSAndy Whitcroft			    $name =~ /^${Ident}_$2/) {
29953cbf62dfSAndy Whitcroft#print "FOO C name<$name>\n";
29963cbf62dfSAndy Whitcroft				$suppress_export{$realline_next} = 1;
29973cbf62dfSAndy Whitcroft
29983cbf62dfSAndy Whitcroft			} elsif ($stat !~ /(?:
29992b474a1aSAndy Whitcroft				\n.}\s*$|
300048012058SAndy Whitcroft				^.DEFINE_$Ident\(\Q$name\E\)|
300148012058SAndy Whitcroft				^.DECLARE_$Ident\(\Q$name\E\)|
300248012058SAndy Whitcroft				^.LIST_HEAD\(\Q$name\E\)|
30032b474a1aSAndy Whitcroft				^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
30042b474a1aSAndy Whitcroft				\b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
300548012058SAndy Whitcroft			    )/x) {
30062b474a1aSAndy Whitcroft#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
30072b474a1aSAndy Whitcroft				$suppress_export{$realline_next} = 2;
30082b474a1aSAndy Whitcroft			} else {
30092b474a1aSAndy Whitcroft				$suppress_export{$realline_next} = 1;
30100a920b5bSAndy Whitcroft			}
30110a920b5bSAndy Whitcroft		}
30122b474a1aSAndy Whitcroft		if (!defined $suppress_export{$linenr} &&
30132b474a1aSAndy Whitcroft		    $prevline =~ /^.\s*$/ &&
30142b474a1aSAndy Whitcroft		    ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
30152b474a1aSAndy Whitcroft		     $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
30162b474a1aSAndy Whitcroft#print "FOO B <$lines[$linenr - 1]>\n";
30172b474a1aSAndy Whitcroft			$suppress_export{$linenr} = 2;
30182b474a1aSAndy Whitcroft		}
30192b474a1aSAndy Whitcroft		if (defined $suppress_export{$linenr} &&
30202b474a1aSAndy Whitcroft		    $suppress_export{$linenr} == 2) {
3021000d1cc1SJoe Perches			WARN("EXPORT_SYMBOL",
3022000d1cc1SJoe Perches			     "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
30232b474a1aSAndy Whitcroft		}
30240a920b5bSAndy Whitcroft
30255150bda4SJoe Eloff# check for global initialisers.
3026d5e616fcSJoe Perches		if ($line =~ /^\+(\s*$Type\s*$Ident\s*(?:\s+$Modifier))*\s*=\s*(0|NULL|false)\s*;/) {
3027d5e616fcSJoe Perches			if (ERROR("GLOBAL_INITIALISERS",
3028000d1cc1SJoe Perches				  "do not initialise globals to 0 or NULL\n" .
3029d5e616fcSJoe Perches				      $herecurr) &&
3030d5e616fcSJoe Perches			    $fix) {
3031194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/($Type\s*$Ident\s*(?:\s+$Modifier))*\s*=\s*(0|NULL|false)\s*;/$1;/;
3032d5e616fcSJoe Perches			}
3033f0a594c1SAndy Whitcroft		}
30340a920b5bSAndy Whitcroft# check for static initialisers.
3035d5e616fcSJoe Perches		if ($line =~ /^\+.*\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
3036d5e616fcSJoe Perches			if (ERROR("INITIALISED_STATIC",
3037000d1cc1SJoe Perches				  "do not initialise statics to 0 or NULL\n" .
3038d5e616fcSJoe Perches				      $herecurr) &&
3039d5e616fcSJoe Perches			    $fix) {
3040194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*(0|NULL|false)\s*;/$1;/;
3041d5e616fcSJoe Perches			}
30420a920b5bSAndy Whitcroft		}
30430a920b5bSAndy Whitcroft
30441813087dSJoe Perches# check for misordered declarations of char/short/int/long with signed/unsigned
30451813087dSJoe Perches		while ($sline =~ m{(\b$TypeMisordered\b)}g) {
30461813087dSJoe Perches			my $tmp = trim($1);
30471813087dSJoe Perches			WARN("MISORDERED_TYPE",
30481813087dSJoe Perches			     "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr);
30491813087dSJoe Perches		}
30501813087dSJoe Perches
3051cb710ecaSJoe Perches# check for static const char * arrays.
3052cb710ecaSJoe Perches		if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
3053000d1cc1SJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
3054000d1cc1SJoe Perches			     "static const char * array should probably be static const char * const\n" .
3055cb710ecaSJoe Perches				$herecurr);
3056cb710ecaSJoe Perches               }
3057cb710ecaSJoe Perches
3058cb710ecaSJoe Perches# check for static char foo[] = "bar" declarations.
3059cb710ecaSJoe Perches		if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
3060000d1cc1SJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
3061000d1cc1SJoe Perches			     "static char array declaration should probably be static const char\n" .
3062cb710ecaSJoe Perches				$herecurr);
3063cb710ecaSJoe Perches               }
3064cb710ecaSJoe Perches
30659b0fa60dSJoe Perches# check for non-global char *foo[] = {"bar", ...} declarations.
30669b0fa60dSJoe Perches		if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) {
30679b0fa60dSJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
30689b0fa60dSJoe Perches			     "char * array declaration might be better as static const\n" .
30699b0fa60dSJoe Perches				$herecurr);
30709b0fa60dSJoe Perches               }
30719b0fa60dSJoe Perches
3072b36190c5SJoe Perches# check for function declarations without arguments like "int foo()"
3073b36190c5SJoe Perches		if ($line =~ /(\b$Type\s+$Ident)\s*\(\s*\)/) {
3074b36190c5SJoe Perches			if (ERROR("FUNCTION_WITHOUT_ARGS",
3075b36190c5SJoe Perches				  "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) &&
3076b36190c5SJoe Perches			    $fix) {
3077194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/;
3078b36190c5SJoe Perches			}
3079b36190c5SJoe Perches		}
3080b36190c5SJoe Perches
308192e112fdSJoe Perches# check for uses of DEFINE_PCI_DEVICE_TABLE
308292e112fdSJoe Perches		if ($line =~ /\bDEFINE_PCI_DEVICE_TABLE\s*\(\s*(\w+)\s*\)\s*=/) {
308392e112fdSJoe Perches			if (WARN("DEFINE_PCI_DEVICE_TABLE",
308492e112fdSJoe Perches				 "Prefer struct pci_device_id over deprecated DEFINE_PCI_DEVICE_TABLE\n" . $herecurr) &&
308592e112fdSJoe Perches			    $fix) {
3086194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b(?:static\s+|)DEFINE_PCI_DEVICE_TABLE\s*\(\s*(\w+)\s*\)\s*=\s*/static const struct pci_device_id $1\[\] = /;
308792e112fdSJoe Perches			}
308893ed0e2dSJoe Perches		}
308993ed0e2dSJoe Perches
3090653d4876SAndy Whitcroft# check for new typedefs, only function parameters and sparse annotations
3091653d4876SAndy Whitcroft# make sense.
3092653d4876SAndy Whitcroft		if ($line =~ /\btypedef\s/ &&
30938054576dSAndy Whitcroft		    $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
3094c45dcabdSAndy Whitcroft		    $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
30958ed22cadSAndy Whitcroft		    $line !~ /\b$typeTypedefs\b/ &&
3096653d4876SAndy Whitcroft		    $line !~ /\b__bitwise(?:__|)\b/) {
3097000d1cc1SJoe Perches			WARN("NEW_TYPEDEFS",
3098000d1cc1SJoe Perches			     "do not add new typedefs\n" . $herecurr);
30990a920b5bSAndy Whitcroft		}
31000a920b5bSAndy Whitcroft
31010a920b5bSAndy Whitcroft# * goes on variable not on type
310265863862SAndy Whitcroft		# (char*[ const])
3103bfcb2cc7SAndy Whitcroft		while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
3104bfcb2cc7SAndy Whitcroft			#print "AA<$1>\n";
31053705ce5bSJoe Perches			my ($ident, $from, $to) = ($1, $2, $2);
3106d8aaf121SAndy Whitcroft
310765863862SAndy Whitcroft			# Should start with a space.
310865863862SAndy Whitcroft			$to =~ s/^(\S)/ $1/;
310965863862SAndy Whitcroft			# Should not end with a space.
311065863862SAndy Whitcroft			$to =~ s/\s+$//;
311165863862SAndy Whitcroft			# '*'s should not have spaces between.
3112f9a0b3d1SAndy Whitcroft			while ($to =~ s/\*\s+\*/\*\*/) {
311365863862SAndy Whitcroft			}
3114d8aaf121SAndy Whitcroft
31153705ce5bSJoe Perches##			print "1: from<$from> to<$to> ident<$ident>\n";
311665863862SAndy Whitcroft			if ($from ne $to) {
31173705ce5bSJoe Perches				if (ERROR("POINTER_LOCATION",
31183705ce5bSJoe Perches					  "\"(foo$from)\" should be \"(foo$to)\"\n" .  $herecurr) &&
31193705ce5bSJoe Perches				    $fix) {
31203705ce5bSJoe Perches					my $sub_from = $ident;
31213705ce5bSJoe Perches					my $sub_to = $ident;
31223705ce5bSJoe Perches					$sub_to =~ s/\Q$from\E/$to/;
3123194f66fcSJoe Perches					$fixed[$fixlinenr] =~
31243705ce5bSJoe Perches					    s@\Q$sub_from\E@$sub_to@;
31253705ce5bSJoe Perches				}
312665863862SAndy Whitcroft			}
3127bfcb2cc7SAndy Whitcroft		}
3128bfcb2cc7SAndy Whitcroft		while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
3129bfcb2cc7SAndy Whitcroft			#print "BB<$1>\n";
31303705ce5bSJoe Perches			my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
3131d8aaf121SAndy Whitcroft
313265863862SAndy Whitcroft			# Should start with a space.
313365863862SAndy Whitcroft			$to =~ s/^(\S)/ $1/;
313465863862SAndy Whitcroft			# Should not end with a space.
313565863862SAndy Whitcroft			$to =~ s/\s+$//;
313665863862SAndy Whitcroft			# '*'s should not have spaces between.
3137f9a0b3d1SAndy Whitcroft			while ($to =~ s/\*\s+\*/\*\*/) {
313865863862SAndy Whitcroft			}
313965863862SAndy Whitcroft			# Modifiers should have spaces.
314065863862SAndy Whitcroft			$to =~ s/(\b$Modifier$)/$1 /;
314165863862SAndy Whitcroft
31423705ce5bSJoe Perches##			print "2: from<$from> to<$to> ident<$ident>\n";
3143667026e7SAndy Whitcroft			if ($from ne $to && $ident !~ /^$Modifier$/) {
31443705ce5bSJoe Perches				if (ERROR("POINTER_LOCATION",
31453705ce5bSJoe Perches					  "\"foo${from}bar\" should be \"foo${to}bar\"\n" .  $herecurr) &&
31463705ce5bSJoe Perches				    $fix) {
31473705ce5bSJoe Perches
31483705ce5bSJoe Perches					my $sub_from = $match;
31493705ce5bSJoe Perches					my $sub_to = $match;
31503705ce5bSJoe Perches					$sub_to =~ s/\Q$from\E/$to/;
3151194f66fcSJoe Perches					$fixed[$fixlinenr] =~
31523705ce5bSJoe Perches					    s@\Q$sub_from\E@$sub_to@;
31533705ce5bSJoe Perches				}
315465863862SAndy Whitcroft			}
31550a920b5bSAndy Whitcroft		}
31560a920b5bSAndy Whitcroft
31570a920b5bSAndy Whitcroft# # no BUG() or BUG_ON()
31580a920b5bSAndy Whitcroft# 		if ($line =~ /\b(BUG|BUG_ON)\b/) {
31590a920b5bSAndy Whitcroft# 			print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
31600a920b5bSAndy Whitcroft# 			print "$herecurr";
31610a920b5bSAndy Whitcroft# 			$clean = 0;
31620a920b5bSAndy Whitcroft# 		}
31630a920b5bSAndy Whitcroft
31648905a67cSAndy Whitcroft		if ($line =~ /\bLINUX_VERSION_CODE\b/) {
3165000d1cc1SJoe Perches			WARN("LINUX_VERSION_CODE",
3166000d1cc1SJoe Perches			     "LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
31678905a67cSAndy Whitcroft		}
31688905a67cSAndy Whitcroft
316917441227SJoe Perches# check for uses of printk_ratelimit
317017441227SJoe Perches		if ($line =~ /\bprintk_ratelimit\s*\(/) {
3171000d1cc1SJoe Perches			WARN("PRINTK_RATELIMITED",
3172000d1cc1SJoe Perches"Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
317317441227SJoe Perches		}
317417441227SJoe Perches
317500df344fSAndy Whitcroft# printk should use KERN_* levels.  Note that follow on printk's on the
317600df344fSAndy Whitcroft# same line do not need a level, so we use the current block context
317700df344fSAndy Whitcroft# to try and find and validate the current printk.  In summary the current
317825985edcSLucas De Marchi# printk includes all preceding printk's which have no newline on the end.
317900df344fSAndy Whitcroft# we assume the first bad printk is the one to report.
3180f0a594c1SAndy Whitcroft		if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
318100df344fSAndy Whitcroft			my $ok = 0;
318200df344fSAndy Whitcroft			for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
318300df344fSAndy Whitcroft				#print "CHECK<$lines[$ln - 1]\n";
318425985edcSLucas De Marchi				# we have a preceding printk if it ends
318500df344fSAndy Whitcroft				# with "\n" ignore it, else it is to blame
318600df344fSAndy Whitcroft				if ($lines[$ln - 1] =~ m{\bprintk\(}) {
318700df344fSAndy Whitcroft					if ($rawlines[$ln - 1] !~ m{\\n"}) {
318800df344fSAndy Whitcroft						$ok = 1;
318900df344fSAndy Whitcroft					}
319000df344fSAndy Whitcroft					last;
319100df344fSAndy Whitcroft				}
319200df344fSAndy Whitcroft			}
319300df344fSAndy Whitcroft			if ($ok == 0) {
3194000d1cc1SJoe Perches				WARN("PRINTK_WITHOUT_KERN_LEVEL",
3195000d1cc1SJoe Perches				     "printk() should include KERN_ facility level\n" . $herecurr);
31960a920b5bSAndy Whitcroft			}
319700df344fSAndy Whitcroft		}
31980a920b5bSAndy Whitcroft
3199243f3803SJoe Perches		if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
3200243f3803SJoe Perches			my $orig = $1;
3201243f3803SJoe Perches			my $level = lc($orig);
3202243f3803SJoe Perches			$level = "warn" if ($level eq "warning");
32038f26b837SJoe Perches			my $level2 = $level;
32048f26b837SJoe Perches			$level2 = "dbg" if ($level eq "debug");
3205243f3803SJoe Perches			WARN("PREFER_PR_LEVEL",
3206daa8b059SYogesh Chaudhari			     "Prefer [subsystem eg: netdev]_$level2([subsystem]dev, ... then dev_$level2(dev, ... then pr_$level(...  to printk(KERN_$orig ...\n" . $herecurr);
3207243f3803SJoe Perches		}
3208243f3803SJoe Perches
3209243f3803SJoe Perches		if ($line =~ /\bpr_warning\s*\(/) {
3210d5e616fcSJoe Perches			if (WARN("PREFER_PR_LEVEL",
3211d5e616fcSJoe Perches				 "Prefer pr_warn(... to pr_warning(...\n" . $herecurr) &&
3212d5e616fcSJoe Perches			    $fix) {
3213194f66fcSJoe Perches				$fixed[$fixlinenr] =~
3214d5e616fcSJoe Perches				    s/\bpr_warning\b/pr_warn/;
3215d5e616fcSJoe Perches			}
3216243f3803SJoe Perches		}
3217243f3803SJoe Perches
3218dc139313SJoe Perches		if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
3219dc139313SJoe Perches			my $orig = $1;
3220dc139313SJoe Perches			my $level = lc($orig);
3221dc139313SJoe Perches			$level = "warn" if ($level eq "warning");
3222dc139313SJoe Perches			$level = "dbg" if ($level eq "debug");
3223dc139313SJoe Perches			WARN("PREFER_DEV_LEVEL",
3224dc139313SJoe Perches			     "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
3225dc139313SJoe Perches		}
3226dc139313SJoe Perches
3227653d4876SAndy Whitcroft# function brace can't be on same line, except for #defines of do while,
3228653d4876SAndy Whitcroft# or if closed on same line
32298d182478SJoe Perches		if (($line=~/$Type\s*$Ident\(.*\).*\s*{/) and
3230c45dcabdSAndy Whitcroft		    !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) {
32318d182478SJoe Perches			if (ERROR("OPEN_BRACE",
32328d182478SJoe Perches				  "open brace '{' following function declarations go on the next line\n" . $herecurr) &&
32338d182478SJoe Perches			    $fix) {
32348d182478SJoe Perches				fix_delete_line($fixlinenr, $rawline);
32358d182478SJoe Perches				my $fixed_line = $rawline;
32368d182478SJoe Perches				$fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*){(.*)$/;
32378d182478SJoe Perches				my $line1 = $1;
32388d182478SJoe Perches				my $line2 = $2;
32398d182478SJoe Perches				fix_insert_line($fixlinenr, ltrim($line1));
32408d182478SJoe Perches				fix_insert_line($fixlinenr, "\+{");
32418d182478SJoe Perches				if ($line2 !~ /^\s*$/) {
32428d182478SJoe Perches					fix_insert_line($fixlinenr, "\+\t" . trim($line2));
32438d182478SJoe Perches				}
32448d182478SJoe Perches			}
32450a920b5bSAndy Whitcroft		}
3246653d4876SAndy Whitcroft
32478905a67cSAndy Whitcroft# open braces for enum, union and struct go on the same line.
32488905a67cSAndy Whitcroft		if ($line =~ /^.\s*{/ &&
32498905a67cSAndy Whitcroft		    $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
32508d182478SJoe Perches			if (ERROR("OPEN_BRACE",
32518d182478SJoe Perches				  "open brace '{' following $1 go on the same line\n" . $hereprev) &&
32528d182478SJoe Perches			    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
32538d182478SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
32548d182478SJoe Perches				fix_delete_line($fixlinenr, $rawline);
32558d182478SJoe Perches				my $fixedline = rtrim($prevrawline) . " {";
32568d182478SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
32578d182478SJoe Perches				$fixedline = $rawline;
32588d182478SJoe Perches				$fixedline =~ s/^(.\s*){\s*/$1\t/;
32598d182478SJoe Perches				if ($fixedline !~ /^\+\s*$/) {
32608d182478SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
32618d182478SJoe Perches				}
32628d182478SJoe Perches			}
32638905a67cSAndy Whitcroft		}
32648905a67cSAndy Whitcroft
32650c73b4ebSAndy Whitcroft# missing space after union, struct or enum definition
32663705ce5bSJoe Perches		if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
32673705ce5bSJoe Perches			if (WARN("SPACING",
32683705ce5bSJoe Perches				 "missing space after $1 definition\n" . $herecurr) &&
32693705ce5bSJoe Perches			    $fix) {
3270194f66fcSJoe Perches				$fixed[$fixlinenr] =~
32713705ce5bSJoe Perches				    s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
32723705ce5bSJoe Perches			}
32730c73b4ebSAndy Whitcroft		}
32740c73b4ebSAndy Whitcroft
327531070b5dSJoe Perches# Function pointer declarations
327631070b5dSJoe Perches# check spacing between type, funcptr, and args
327731070b5dSJoe Perches# canonical declaration is "type (*funcptr)(args...)"
327891f72e9cSJoe Perches		if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) {
327931070b5dSJoe Perches			my $declare = $1;
328031070b5dSJoe Perches			my $pre_pointer_space = $2;
328131070b5dSJoe Perches			my $post_pointer_space = $3;
328231070b5dSJoe Perches			my $funcname = $4;
328331070b5dSJoe Perches			my $post_funcname_space = $5;
328431070b5dSJoe Perches			my $pre_args_space = $6;
328531070b5dSJoe Perches
328691f72e9cSJoe Perches# the $Declare variable will capture all spaces after the type
328791f72e9cSJoe Perches# so check it for a missing trailing missing space but pointer return types
328891f72e9cSJoe Perches# don't need a space so don't warn for those.
328991f72e9cSJoe Perches			my $post_declare_space = "";
329091f72e9cSJoe Perches			if ($declare =~ /(\s+)$/) {
329191f72e9cSJoe Perches				$post_declare_space = $1;
329291f72e9cSJoe Perches				$declare = rtrim($declare);
329391f72e9cSJoe Perches			}
329491f72e9cSJoe Perches			if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) {
329531070b5dSJoe Perches				WARN("SPACING",
329631070b5dSJoe Perches				     "missing space after return type\n" . $herecurr);
329791f72e9cSJoe Perches				$post_declare_space = " ";
329831070b5dSJoe Perches			}
329931070b5dSJoe Perches
330031070b5dSJoe Perches# unnecessary space "type  (*funcptr)(args...)"
330191f72e9cSJoe Perches# This test is not currently implemented because these declarations are
330291f72e9cSJoe Perches# equivalent to
330391f72e9cSJoe Perches#	int  foo(int bar, ...)
330491f72e9cSJoe Perches# and this is form shouldn't/doesn't generate a checkpatch warning.
330591f72e9cSJoe Perches#
330691f72e9cSJoe Perches#			elsif ($declare =~ /\s{2,}$/) {
330791f72e9cSJoe Perches#				WARN("SPACING",
330891f72e9cSJoe Perches#				     "Multiple spaces after return type\n" . $herecurr);
330991f72e9cSJoe Perches#			}
331031070b5dSJoe Perches
331131070b5dSJoe Perches# unnecessary space "type ( *funcptr)(args...)"
331231070b5dSJoe Perches			if (defined $pre_pointer_space &&
331331070b5dSJoe Perches			    $pre_pointer_space =~ /^\s/) {
331431070b5dSJoe Perches				WARN("SPACING",
331531070b5dSJoe Perches				     "Unnecessary space after function pointer open parenthesis\n" . $herecurr);
331631070b5dSJoe Perches			}
331731070b5dSJoe Perches
331831070b5dSJoe Perches# unnecessary space "type (* funcptr)(args...)"
331931070b5dSJoe Perches			if (defined $post_pointer_space &&
332031070b5dSJoe Perches			    $post_pointer_space =~ /^\s/) {
332131070b5dSJoe Perches				WARN("SPACING",
332231070b5dSJoe Perches				     "Unnecessary space before function pointer name\n" . $herecurr);
332331070b5dSJoe Perches			}
332431070b5dSJoe Perches
332531070b5dSJoe Perches# unnecessary space "type (*funcptr )(args...)"
332631070b5dSJoe Perches			if (defined $post_funcname_space &&
332731070b5dSJoe Perches			    $post_funcname_space =~ /^\s/) {
332831070b5dSJoe Perches				WARN("SPACING",
332931070b5dSJoe Perches				     "Unnecessary space after function pointer name\n" . $herecurr);
333031070b5dSJoe Perches			}
333131070b5dSJoe Perches
333231070b5dSJoe Perches# unnecessary space "type (*funcptr) (args...)"
333331070b5dSJoe Perches			if (defined $pre_args_space &&
333431070b5dSJoe Perches			    $pre_args_space =~ /^\s/) {
333531070b5dSJoe Perches				WARN("SPACING",
333631070b5dSJoe Perches				     "Unnecessary space before function pointer arguments\n" . $herecurr);
333731070b5dSJoe Perches			}
333831070b5dSJoe Perches
333931070b5dSJoe Perches			if (show_type("SPACING") && $fix) {
3340194f66fcSJoe Perches				$fixed[$fixlinenr] =~
334191f72e9cSJoe Perches				    s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex;
334231070b5dSJoe Perches			}
334331070b5dSJoe Perches		}
334431070b5dSJoe Perches
33458d31cfceSAndy Whitcroft# check for spacing round square brackets; allowed:
33468d31cfceSAndy Whitcroft#  1. with a type on the left -- int [] a;
3347fe2a7dbcSAndy Whitcroft#  2. at the beginning of a line for slice initialisers -- [0...10] = 5,
3348fe2a7dbcSAndy Whitcroft#  3. inside a curly brace -- = { [0...10] = 5 }
33498d31cfceSAndy Whitcroft		while ($line =~ /(.*?\s)\[/g) {
33508d31cfceSAndy Whitcroft			my ($where, $prefix) = ($-[1], $1);
33518d31cfceSAndy Whitcroft			if ($prefix !~ /$Type\s+$/ &&
3352fe2a7dbcSAndy Whitcroft			    ($where != 0 || $prefix !~ /^.\s+$/) &&
3353daebc534SAndy Whitcroft			    $prefix !~ /[{,]\s+$/) {
33543705ce5bSJoe Perches				if (ERROR("BRACKET_SPACE",
33553705ce5bSJoe Perches					  "space prohibited before open square bracket '['\n" . $herecurr) &&
33563705ce5bSJoe Perches				    $fix) {
3357194f66fcSJoe Perches				    $fixed[$fixlinenr] =~
33583705ce5bSJoe Perches					s/^(\+.*?)\s+\[/$1\[/;
33593705ce5bSJoe Perches				}
33608d31cfceSAndy Whitcroft			}
33618d31cfceSAndy Whitcroft		}
33628d31cfceSAndy Whitcroft
3363f0a594c1SAndy Whitcroft# check for spaces between functions and their parentheses.
33646c72ffaaSAndy Whitcroft		while ($line =~ /($Ident)\s+\(/g) {
3365c2fdda0dSAndy Whitcroft			my $name = $1;
3366773647a0SAndy Whitcroft			my $ctx_before = substr($line, 0, $-[1]);
3367773647a0SAndy Whitcroft			my $ctx = "$ctx_before$name";
3368c2fdda0dSAndy Whitcroft
3369c2fdda0dSAndy Whitcroft			# Ignore those directives where spaces _are_ permitted.
3370773647a0SAndy Whitcroft			if ($name =~ /^(?:
3371773647a0SAndy Whitcroft				if|for|while|switch|return|case|
3372773647a0SAndy Whitcroft				volatile|__volatile__|
3373773647a0SAndy Whitcroft				__attribute__|format|__extension__|
3374773647a0SAndy Whitcroft				asm|__asm__)$/x)
3375773647a0SAndy Whitcroft			{
3376c2fdda0dSAndy Whitcroft			# cpp #define statements have non-optional spaces, ie
3377c2fdda0dSAndy Whitcroft			# if there is a space between the name and the open
3378c2fdda0dSAndy Whitcroft			# parenthesis it is simply not a parameter group.
3379c45dcabdSAndy Whitcroft			} elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
3380773647a0SAndy Whitcroft
3381773647a0SAndy Whitcroft			# cpp #elif statement condition may start with a (
3382c45dcabdSAndy Whitcroft			} elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
3383c2fdda0dSAndy Whitcroft
3384c2fdda0dSAndy Whitcroft			# If this whole things ends with a type its most
3385c2fdda0dSAndy Whitcroft			# likely a typedef for a function.
3386773647a0SAndy Whitcroft			} elsif ($ctx =~ /$Type$/) {
3387c2fdda0dSAndy Whitcroft
3388c2fdda0dSAndy Whitcroft			} else {
33893705ce5bSJoe Perches				if (WARN("SPACING",
33903705ce5bSJoe Perches					 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
33913705ce5bSJoe Perches					     $fix) {
3392194f66fcSJoe Perches					$fixed[$fixlinenr] =~
33933705ce5bSJoe Perches					    s/\b$name\s+\(/$name\(/;
33943705ce5bSJoe Perches				}
3395f0a594c1SAndy Whitcroft			}
33966c72ffaaSAndy Whitcroft		}
33979a4cad4eSEric Nelson
3398653d4876SAndy Whitcroft# Check operator spacing.
33990a920b5bSAndy Whitcroft		if (!($line=~/\#\s*include/)) {
34003705ce5bSJoe Perches			my $fixed_line = "";
34013705ce5bSJoe Perches			my $line_fixed = 0;
34023705ce5bSJoe Perches
34039c0ca6f9SAndy Whitcroft			my $ops = qr{
34049c0ca6f9SAndy Whitcroft				<<=|>>=|<=|>=|==|!=|
34059c0ca6f9SAndy Whitcroft				\+=|-=|\*=|\/=|%=|\^=|\|=|&=|
34069c0ca6f9SAndy Whitcroft				=>|->|<<|>>|<|>|=|!|~|
34071f65f947SAndy Whitcroft				&&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
340884731623SJoe Perches				\?:|\?|:
34099c0ca6f9SAndy Whitcroft			}x;
3410cf655043SAndy Whitcroft			my @elements = split(/($ops|;)/, $opline);
34113705ce5bSJoe Perches
34123705ce5bSJoe Perches##			print("element count: <" . $#elements . ">\n");
34133705ce5bSJoe Perches##			foreach my $el (@elements) {
34143705ce5bSJoe Perches##				print("el: <$el>\n");
34153705ce5bSJoe Perches##			}
34163705ce5bSJoe Perches
34173705ce5bSJoe Perches			my @fix_elements = ();
341800df344fSAndy Whitcroft			my $off = 0;
34196c72ffaaSAndy Whitcroft
34203705ce5bSJoe Perches			foreach my $el (@elements) {
34213705ce5bSJoe Perches				push(@fix_elements, substr($rawline, $off, length($el)));
34223705ce5bSJoe Perches				$off += length($el);
34233705ce5bSJoe Perches			}
34243705ce5bSJoe Perches
34253705ce5bSJoe Perches			$off = 0;
34263705ce5bSJoe Perches
34276c72ffaaSAndy Whitcroft			my $blank = copy_spacing($opline);
3428b34c648bSJoe Perches			my $last_after = -1;
34296c72ffaaSAndy Whitcroft
34300a920b5bSAndy Whitcroft			for (my $n = 0; $n < $#elements; $n += 2) {
34313705ce5bSJoe Perches
34323705ce5bSJoe Perches				my $good = $fix_elements[$n] . $fix_elements[$n + 1];
34333705ce5bSJoe Perches
34343705ce5bSJoe Perches##				print("n: <$n> good: <$good>\n");
34353705ce5bSJoe Perches
34364a0df2efSAndy Whitcroft				$off += length($elements[$n]);
34374a0df2efSAndy Whitcroft
343825985edcSLucas De Marchi				# Pick up the preceding and succeeding characters.
3439773647a0SAndy Whitcroft				my $ca = substr($opline, 0, $off);
3440773647a0SAndy Whitcroft				my $cc = '';
3441773647a0SAndy Whitcroft				if (length($opline) >= ($off + length($elements[$n + 1]))) {
3442773647a0SAndy Whitcroft					$cc = substr($opline, $off + length($elements[$n + 1]));
3443773647a0SAndy Whitcroft				}
3444773647a0SAndy Whitcroft				my $cb = "$ca$;$cc";
3445773647a0SAndy Whitcroft
34464a0df2efSAndy Whitcroft				my $a = '';
34474a0df2efSAndy Whitcroft				$a = 'V' if ($elements[$n] ne '');
34484a0df2efSAndy Whitcroft				$a = 'W' if ($elements[$n] =~ /\s$/);
3449cf655043SAndy Whitcroft				$a = 'C' if ($elements[$n] =~ /$;$/);
34504a0df2efSAndy Whitcroft				$a = 'B' if ($elements[$n] =~ /(\[|\()$/);
34514a0df2efSAndy Whitcroft				$a = 'O' if ($elements[$n] eq '');
3452773647a0SAndy Whitcroft				$a = 'E' if ($ca =~ /^\s*$/);
34534a0df2efSAndy Whitcroft
34540a920b5bSAndy Whitcroft				my $op = $elements[$n + 1];
34554a0df2efSAndy Whitcroft
34564a0df2efSAndy Whitcroft				my $c = '';
34570a920b5bSAndy Whitcroft				if (defined $elements[$n + 2]) {
34584a0df2efSAndy Whitcroft					$c = 'V' if ($elements[$n + 2] ne '');
34594a0df2efSAndy Whitcroft					$c = 'W' if ($elements[$n + 2] =~ /^\s/);
3460cf655043SAndy Whitcroft					$c = 'C' if ($elements[$n + 2] =~ /^$;/);
34614a0df2efSAndy Whitcroft					$c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
34624a0df2efSAndy Whitcroft					$c = 'O' if ($elements[$n + 2] eq '');
34638b1b3378SAndy Whitcroft					$c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
34644a0df2efSAndy Whitcroft				} else {
34654a0df2efSAndy Whitcroft					$c = 'E';
34660a920b5bSAndy Whitcroft				}
34670a920b5bSAndy Whitcroft
34684a0df2efSAndy Whitcroft				my $ctx = "${a}x${c}";
34694a0df2efSAndy Whitcroft
34704a0df2efSAndy Whitcroft				my $at = "(ctx:$ctx)";
34714a0df2efSAndy Whitcroft
34726c72ffaaSAndy Whitcroft				my $ptr = substr($blank, 0, $off) . "^";
3473de7d4f0eSAndy Whitcroft				my $hereptr = "$hereline$ptr\n";
34740a920b5bSAndy Whitcroft
347574048ed8SAndy Whitcroft				# Pull out the value of this operator.
34766c72ffaaSAndy Whitcroft				my $op_type = substr($curr_values, $off + 1, 1);
34770a920b5bSAndy Whitcroft
34781f65f947SAndy Whitcroft				# Get the full operator variant.
34791f65f947SAndy Whitcroft				my $opv = $op . substr($curr_vars, $off, 1);
34801f65f947SAndy Whitcroft
348113214adfSAndy Whitcroft				# Ignore operators passed as parameters.
348213214adfSAndy Whitcroft				if ($op_type ne 'V' &&
348313214adfSAndy Whitcroft				    $ca =~ /\s$/ && $cc =~ /^\s*,/) {
348413214adfSAndy Whitcroft
3485cf655043SAndy Whitcroft#				# Ignore comments
3486cf655043SAndy Whitcroft#				} elsif ($op =~ /^$;+$/) {
348713214adfSAndy Whitcroft
3488d8aaf121SAndy Whitcroft				# ; should have either the end of line or a space or \ after it
348913214adfSAndy Whitcroft				} elsif ($op eq ';') {
3490cf655043SAndy Whitcroft					if ($ctx !~ /.x[WEBC]/ &&
3491cf655043SAndy Whitcroft					    $cc !~ /^\\/ && $cc !~ /^;/) {
34923705ce5bSJoe Perches						if (ERROR("SPACING",
34933705ce5bSJoe Perches							  "space required after that '$op' $at\n" . $hereptr)) {
3494b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
34953705ce5bSJoe Perches							$line_fixed = 1;
34963705ce5bSJoe Perches						}
3497d8aaf121SAndy Whitcroft					}
3498d8aaf121SAndy Whitcroft
3499d8aaf121SAndy Whitcroft				# // is a comment
3500d8aaf121SAndy Whitcroft				} elsif ($op eq '//') {
35010a920b5bSAndy Whitcroft
3502b00e4814SJoe Perches				#   :   when part of a bitfield
3503b00e4814SJoe Perches				} elsif ($opv eq ':B') {
3504b00e4814SJoe Perches					# skip the bitfield test for now
3505b00e4814SJoe Perches
35061f65f947SAndy Whitcroft				# No spaces for:
35071f65f947SAndy Whitcroft				#   ->
3508b00e4814SJoe Perches				} elsif ($op eq '->') {
35094a0df2efSAndy Whitcroft					if ($ctx =~ /Wx.|.xW/) {
35103705ce5bSJoe Perches						if (ERROR("SPACING",
35113705ce5bSJoe Perches							  "spaces prohibited around that '$op' $at\n" . $hereptr)) {
3512b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
35133705ce5bSJoe Perches							if (defined $fix_elements[$n + 2]) {
35143705ce5bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
35153705ce5bSJoe Perches							}
3516b34c648bSJoe Perches							$line_fixed = 1;
35173705ce5bSJoe Perches						}
35180a920b5bSAndy Whitcroft					}
35190a920b5bSAndy Whitcroft
35200a920b5bSAndy Whitcroft				# , must have a space on the right.
35210a920b5bSAndy Whitcroft				} elsif ($op eq ',') {
3522cf655043SAndy Whitcroft					if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
35233705ce5bSJoe Perches						if (ERROR("SPACING",
35243705ce5bSJoe Perches							  "space required after that '$op' $at\n" . $hereptr)) {
3525b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
35263705ce5bSJoe Perches							$line_fixed = 1;
3527b34c648bSJoe Perches							$last_after = $n;
35283705ce5bSJoe Perches						}
35290a920b5bSAndy Whitcroft					}
35300a920b5bSAndy Whitcroft
35319c0ca6f9SAndy Whitcroft				# '*' as part of a type definition -- reported already.
353274048ed8SAndy Whitcroft				} elsif ($opv eq '*_') {
35339c0ca6f9SAndy Whitcroft					#warn "'*' is part of type\n";
35349c0ca6f9SAndy Whitcroft
35359c0ca6f9SAndy Whitcroft				# unary operators should have a space before and
35369c0ca6f9SAndy Whitcroft				# none after.  May be left adjacent to another
35379c0ca6f9SAndy Whitcroft				# unary operator, or a cast
35389c0ca6f9SAndy Whitcroft				} elsif ($op eq '!' || $op eq '~' ||
353974048ed8SAndy Whitcroft					 $opv eq '*U' || $opv eq '-U' ||
35400d413866SAndy Whitcroft					 $opv eq '&U' || $opv eq '&&U') {
3541cf655043SAndy Whitcroft					if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
35423705ce5bSJoe Perches						if (ERROR("SPACING",
35433705ce5bSJoe Perches							  "space required before that '$op' $at\n" . $hereptr)) {
3544b34c648bSJoe Perches							if ($n != $last_after + 2) {
3545b34c648bSJoe Perches								$good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]);
35463705ce5bSJoe Perches								$line_fixed = 1;
35473705ce5bSJoe Perches							}
35480a920b5bSAndy Whitcroft						}
3549b34c648bSJoe Perches					}
3550a3340b35SAndy Whitcroft					if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
3551171ae1a4SAndy Whitcroft						# A unary '*' may be const
3552171ae1a4SAndy Whitcroft
3553171ae1a4SAndy Whitcroft					} elsif ($ctx =~ /.xW/) {
35543705ce5bSJoe Perches						if (ERROR("SPACING",
35553705ce5bSJoe Perches							  "space prohibited after that '$op' $at\n" . $hereptr)) {
3556b34c648bSJoe Perches							$good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]);
35573705ce5bSJoe Perches							if (defined $fix_elements[$n + 2]) {
35583705ce5bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
35593705ce5bSJoe Perches							}
3560b34c648bSJoe Perches							$line_fixed = 1;
35613705ce5bSJoe Perches						}
35620a920b5bSAndy Whitcroft					}
35630a920b5bSAndy Whitcroft
35640a920b5bSAndy Whitcroft				# unary ++ and unary -- are allowed no space on one side.
35650a920b5bSAndy Whitcroft				} elsif ($op eq '++' or $op eq '--') {
3566773647a0SAndy Whitcroft					if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
35673705ce5bSJoe Perches						if (ERROR("SPACING",
35683705ce5bSJoe Perches							  "space required one side of that '$op' $at\n" . $hereptr)) {
3569b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
35703705ce5bSJoe Perches							$line_fixed = 1;
35713705ce5bSJoe Perches						}
35720a920b5bSAndy Whitcroft					}
3573773647a0SAndy Whitcroft					if ($ctx =~ /Wx[BE]/ ||
3574773647a0SAndy Whitcroft					    ($ctx =~ /Wx./ && $cc =~ /^;/)) {
35753705ce5bSJoe Perches						if (ERROR("SPACING",
35763705ce5bSJoe Perches							  "space prohibited before that '$op' $at\n" . $hereptr)) {
3577b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
35783705ce5bSJoe Perches							$line_fixed = 1;
35793705ce5bSJoe Perches						}
3580653d4876SAndy Whitcroft					}
3581773647a0SAndy Whitcroft					if ($ctx =~ /ExW/) {
35823705ce5bSJoe Perches						if (ERROR("SPACING",
35833705ce5bSJoe Perches							  "space prohibited after that '$op' $at\n" . $hereptr)) {
3584b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
35853705ce5bSJoe Perches							if (defined $fix_elements[$n + 2]) {
35863705ce5bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
3587773647a0SAndy Whitcroft							}
3588b34c648bSJoe Perches							$line_fixed = 1;
35893705ce5bSJoe Perches						}
35903705ce5bSJoe Perches					}
35910a920b5bSAndy Whitcroft
35920a920b5bSAndy Whitcroft				# << and >> may either have or not have spaces both sides
35939c0ca6f9SAndy Whitcroft				} elsif ($op eq '<<' or $op eq '>>' or
35949c0ca6f9SAndy Whitcroft					 $op eq '&' or $op eq '^' or $op eq '|' or
35959c0ca6f9SAndy Whitcroft					 $op eq '+' or $op eq '-' or
3596c2fdda0dSAndy Whitcroft					 $op eq '*' or $op eq '/' or
3597c2fdda0dSAndy Whitcroft					 $op eq '%')
35980a920b5bSAndy Whitcroft				{
3599773647a0SAndy Whitcroft					if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
36003705ce5bSJoe Perches						if (ERROR("SPACING",
36013705ce5bSJoe Perches							  "need consistent spacing around '$op' $at\n" . $hereptr)) {
3602b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
3603b34c648bSJoe Perches							if (defined $fix_elements[$n + 2]) {
3604b34c648bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
3605b34c648bSJoe Perches							}
36063705ce5bSJoe Perches							$line_fixed = 1;
36073705ce5bSJoe Perches						}
36080a920b5bSAndy Whitcroft					}
36090a920b5bSAndy Whitcroft
36101f65f947SAndy Whitcroft				# A colon needs no spaces before when it is
36111f65f947SAndy Whitcroft				# terminating a case value or a label.
36121f65f947SAndy Whitcroft				} elsif ($opv eq ':C' || $opv eq ':L') {
36131f65f947SAndy Whitcroft					if ($ctx =~ /Wx./) {
36143705ce5bSJoe Perches						if (ERROR("SPACING",
36153705ce5bSJoe Perches							  "space prohibited before that '$op' $at\n" . $hereptr)) {
3616b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
36173705ce5bSJoe Perches							$line_fixed = 1;
36183705ce5bSJoe Perches						}
36191f65f947SAndy Whitcroft					}
36201f65f947SAndy Whitcroft
36210a920b5bSAndy Whitcroft				# All the others need spaces both sides.
3622cf655043SAndy Whitcroft				} elsif ($ctx !~ /[EWC]x[CWE]/) {
36231f65f947SAndy Whitcroft					my $ok = 0;
36241f65f947SAndy Whitcroft
362522f2a2efSAndy Whitcroft					# Ignore email addresses <foo@bar>
36261f65f947SAndy Whitcroft					if (($op eq '<' &&
36271f65f947SAndy Whitcroft					     $cc =~ /^\S+\@\S+>/) ||
36281f65f947SAndy Whitcroft					    ($op eq '>' &&
36291f65f947SAndy Whitcroft					     $ca =~ /<\S+\@\S+$/))
36301f65f947SAndy Whitcroft					{
36311f65f947SAndy Whitcroft					    	$ok = 1;
36321f65f947SAndy Whitcroft					}
36331f65f947SAndy Whitcroft
363484731623SJoe Perches					# messages are ERROR, but ?: are CHK
36351f65f947SAndy Whitcroft					if ($ok == 0) {
363684731623SJoe Perches						my $msg_type = \&ERROR;
363784731623SJoe Perches						$msg_type = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
363884731623SJoe Perches
363984731623SJoe Perches						if (&{$msg_type}("SPACING",
36403705ce5bSJoe Perches								 "spaces required around that '$op' $at\n" . $hereptr)) {
3641b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
3642b34c648bSJoe Perches							if (defined $fix_elements[$n + 2]) {
3643b34c648bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
3644b34c648bSJoe Perches							}
36453705ce5bSJoe Perches							$line_fixed = 1;
36463705ce5bSJoe Perches						}
36470a920b5bSAndy Whitcroft					}
364822f2a2efSAndy Whitcroft				}
36494a0df2efSAndy Whitcroft				$off += length($elements[$n + 1]);
36503705ce5bSJoe Perches
36513705ce5bSJoe Perches##				print("n: <$n> GOOD: <$good>\n");
36523705ce5bSJoe Perches
36533705ce5bSJoe Perches				$fixed_line = $fixed_line . $good;
36540a920b5bSAndy Whitcroft			}
36553705ce5bSJoe Perches
36563705ce5bSJoe Perches			if (($#elements % 2) == 0) {
36573705ce5bSJoe Perches				$fixed_line = $fixed_line . $fix_elements[$#elements];
36583705ce5bSJoe Perches			}
36593705ce5bSJoe Perches
3660194f66fcSJoe Perches			if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) {
3661194f66fcSJoe Perches				$fixed[$fixlinenr] = $fixed_line;
36623705ce5bSJoe Perches			}
36633705ce5bSJoe Perches
36643705ce5bSJoe Perches
36650a920b5bSAndy Whitcroft		}
36660a920b5bSAndy Whitcroft
3667786b6326SJoe Perches# check for whitespace before a non-naked semicolon
3668d2e248e7SJoe Perches		if ($line =~ /^\+.*\S\s+;\s*$/) {
3669786b6326SJoe Perches			if (WARN("SPACING",
3670786b6326SJoe Perches				 "space prohibited before semicolon\n" . $herecurr) &&
3671786b6326SJoe Perches			    $fix) {
3672194f66fcSJoe Perches				1 while $fixed[$fixlinenr] =~
3673786b6326SJoe Perches				    s/^(\+.*\S)\s+;/$1;/;
3674786b6326SJoe Perches			}
3675786b6326SJoe Perches		}
3676786b6326SJoe Perches
3677f0a594c1SAndy Whitcroft# check for multiple assignments
3678f0a594c1SAndy Whitcroft		if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
3679000d1cc1SJoe Perches			CHK("MULTIPLE_ASSIGNMENTS",
3680000d1cc1SJoe Perches			    "multiple assignments should be avoided\n" . $herecurr);
3681f0a594c1SAndy Whitcroft		}
3682f0a594c1SAndy Whitcroft
368322f2a2efSAndy Whitcroft## # check for multiple declarations, allowing for a function declaration
368422f2a2efSAndy Whitcroft## # continuation.
368522f2a2efSAndy Whitcroft## 		if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
368622f2a2efSAndy Whitcroft## 		    $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
368722f2a2efSAndy Whitcroft##
368822f2a2efSAndy Whitcroft## 			# Remove any bracketed sections to ensure we do not
368922f2a2efSAndy Whitcroft## 			# falsly report the parameters of functions.
369022f2a2efSAndy Whitcroft## 			my $ln = $line;
369122f2a2efSAndy Whitcroft## 			while ($ln =~ s/\([^\(\)]*\)//g) {
369222f2a2efSAndy Whitcroft## 			}
369322f2a2efSAndy Whitcroft## 			if ($ln =~ /,/) {
3694000d1cc1SJoe Perches## 				WARN("MULTIPLE_DECLARATION",
3695000d1cc1SJoe Perches##				     "declaring multiple variables together should be avoided\n" . $herecurr);
369622f2a2efSAndy Whitcroft## 			}
369722f2a2efSAndy Whitcroft## 		}
3698f0a594c1SAndy Whitcroft
36990a920b5bSAndy Whitcroft#need space before brace following if, while, etc
370022f2a2efSAndy Whitcroft		if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
370122f2a2efSAndy Whitcroft		    $line =~ /do{/) {
37023705ce5bSJoe Perches			if (ERROR("SPACING",
37033705ce5bSJoe Perches				  "space required before the open brace '{'\n" . $herecurr) &&
37043705ce5bSJoe Perches			    $fix) {
3705194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/^(\+.*(?:do|\))){/$1 {/;
37063705ce5bSJoe Perches			}
3707de7d4f0eSAndy Whitcroft		}
3708de7d4f0eSAndy Whitcroft
3709c4a62ef9SJoe Perches## # check for blank lines before declarations
3710c4a62ef9SJoe Perches##		if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
3711c4a62ef9SJoe Perches##		    $prevrawline =~ /^.\s*$/) {
3712c4a62ef9SJoe Perches##			WARN("SPACING",
3713c4a62ef9SJoe Perches##			     "No blank lines before declarations\n" . $hereprev);
3714c4a62ef9SJoe Perches##		}
3715c4a62ef9SJoe Perches##
3716c4a62ef9SJoe Perches
3717de7d4f0eSAndy Whitcroft# closing brace should have a space following it when it has anything
3718de7d4f0eSAndy Whitcroft# on the line
3719de7d4f0eSAndy Whitcroft		if ($line =~ /}(?!(?:,|;|\)))\S/) {
3720d5e616fcSJoe Perches			if (ERROR("SPACING",
3721d5e616fcSJoe Perches				  "space required after that close brace '}'\n" . $herecurr) &&
3722d5e616fcSJoe Perches			    $fix) {
3723194f66fcSJoe Perches				$fixed[$fixlinenr] =~
3724d5e616fcSJoe Perches				    s/}((?!(?:,|;|\)))\S)/} $1/;
3725d5e616fcSJoe Perches			}
37260a920b5bSAndy Whitcroft		}
37270a920b5bSAndy Whitcroft
372822f2a2efSAndy Whitcroft# check spacing on square brackets
372922f2a2efSAndy Whitcroft		if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
37303705ce5bSJoe Perches			if (ERROR("SPACING",
37313705ce5bSJoe Perches				  "space prohibited after that open square bracket '['\n" . $herecurr) &&
37323705ce5bSJoe Perches			    $fix) {
3733194f66fcSJoe Perches				$fixed[$fixlinenr] =~
37343705ce5bSJoe Perches				    s/\[\s+/\[/;
37353705ce5bSJoe Perches			}
373622f2a2efSAndy Whitcroft		}
373722f2a2efSAndy Whitcroft		if ($line =~ /\s\]/) {
37383705ce5bSJoe Perches			if (ERROR("SPACING",
37393705ce5bSJoe Perches				  "space prohibited before that close square bracket ']'\n" . $herecurr) &&
37403705ce5bSJoe Perches			    $fix) {
3741194f66fcSJoe Perches				$fixed[$fixlinenr] =~
37423705ce5bSJoe Perches				    s/\s+\]/\]/;
37433705ce5bSJoe Perches			}
374422f2a2efSAndy Whitcroft		}
374522f2a2efSAndy Whitcroft
3746c45dcabdSAndy Whitcroft# check spacing on parentheses
37479c0ca6f9SAndy Whitcroft		if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
37489c0ca6f9SAndy Whitcroft		    $line !~ /for\s*\(\s+;/) {
37493705ce5bSJoe Perches			if (ERROR("SPACING",
37503705ce5bSJoe Perches				  "space prohibited after that open parenthesis '('\n" . $herecurr) &&
37513705ce5bSJoe Perches			    $fix) {
3752194f66fcSJoe Perches				$fixed[$fixlinenr] =~
37533705ce5bSJoe Perches				    s/\(\s+/\(/;
37543705ce5bSJoe Perches			}
375522f2a2efSAndy Whitcroft		}
375613214adfSAndy Whitcroft		if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
3757c45dcabdSAndy Whitcroft		    $line !~ /for\s*\(.*;\s+\)/ &&
3758c45dcabdSAndy Whitcroft		    $line !~ /:\s+\)/) {
37593705ce5bSJoe Perches			if (ERROR("SPACING",
37603705ce5bSJoe Perches				  "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
37613705ce5bSJoe Perches			    $fix) {
3762194f66fcSJoe Perches				$fixed[$fixlinenr] =~
37633705ce5bSJoe Perches				    s/\s+\)/\)/;
37643705ce5bSJoe Perches			}
376522f2a2efSAndy Whitcroft		}
376622f2a2efSAndy Whitcroft
3767e2826fd0SJoe Perches# check unnecessary parentheses around addressof/dereference single $Lvals
3768e2826fd0SJoe Perches# ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar
3769e2826fd0SJoe Perches
3770e2826fd0SJoe Perches		while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) {
3771e2826fd0SJoe Perches			CHK("UNNECESSARY_PARENTHESES",
3772e2826fd0SJoe Perches			    "Unnecessary parentheses around $1\n" . $herecurr);
3773e2826fd0SJoe Perches		    }
3774e2826fd0SJoe Perches
37750a920b5bSAndy Whitcroft#goto labels aren't indented, allow a single space however
37764a0df2efSAndy Whitcroft		if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
37770a920b5bSAndy Whitcroft		   !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
37783705ce5bSJoe Perches			if (WARN("INDENTED_LABEL",
37793705ce5bSJoe Perches				 "labels should not be indented\n" . $herecurr) &&
37803705ce5bSJoe Perches			    $fix) {
3781194f66fcSJoe Perches				$fixed[$fixlinenr] =~
37823705ce5bSJoe Perches				    s/^(.)\s+/$1/;
37833705ce5bSJoe Perches			}
37840a920b5bSAndy Whitcroft		}
37850a920b5bSAndy Whitcroft
37865b9553abSJoe Perches# return is not a function
3787507e5141SJoe Perches		if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {
3788c45dcabdSAndy Whitcroft			my $spacing = $1;
3789507e5141SJoe Perches			if ($^V && $^V ge 5.10.0 &&
37905b9553abSJoe Perches			    $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) {
37915b9553abSJoe Perches				my $value = $1;
37925b9553abSJoe Perches				$value = deparenthesize($value);
37935b9553abSJoe Perches				if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) {
3794000d1cc1SJoe Perches					ERROR("RETURN_PARENTHESES",
3795000d1cc1SJoe Perches					      "return is not a function, parentheses are not required\n" . $herecurr);
37965b9553abSJoe Perches				}
3797c45dcabdSAndy Whitcroft			} elsif ($spacing !~ /\s+/) {
3798000d1cc1SJoe Perches				ERROR("SPACING",
3799000d1cc1SJoe Perches				      "space required before the open parenthesis '('\n" . $herecurr);
3800c45dcabdSAndy Whitcroft			}
3801c45dcabdSAndy Whitcroft		}
3802507e5141SJoe Perches
3803b43ae21bSJoe Perches# unnecessary return in a void function
3804b43ae21bSJoe Perches# at end-of-function, with the previous line a single leading tab, then return;
3805b43ae21bSJoe Perches# and the line before that not a goto label target like "out:"
3806b43ae21bSJoe Perches		if ($sline =~ /^[ \+]}\s*$/ &&
3807b43ae21bSJoe Perches		    $prevline =~ /^\+\treturn\s*;\s*$/ &&
3808b43ae21bSJoe Perches		    $linenr >= 3 &&
3809b43ae21bSJoe Perches		    $lines[$linenr - 3] =~ /^[ +]/ &&
3810b43ae21bSJoe Perches		    $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) {
38119819cf25SJoe Perches			WARN("RETURN_VOID",
3812b43ae21bSJoe Perches			     "void function return statements are not generally useful\n" . $hereprev);
38139819cf25SJoe Perches               }
38149819cf25SJoe Perches
3815189248d8SJoe Perches# if statements using unnecessary parentheses - ie: if ((foo == bar))
3816189248d8SJoe Perches		if ($^V && $^V ge 5.10.0 &&
3817189248d8SJoe Perches		    $line =~ /\bif\s*((?:\(\s*){2,})/) {
3818189248d8SJoe Perches			my $openparens = $1;
3819189248d8SJoe Perches			my $count = $openparens =~ tr@\(@\(@;
3820189248d8SJoe Perches			my $msg = "";
3821189248d8SJoe Perches			if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) {
3822189248d8SJoe Perches				my $comp = $4;	#Not $1 because of $LvalOrFunc
3823189248d8SJoe Perches				$msg = " - maybe == should be = ?" if ($comp eq "==");
3824189248d8SJoe Perches				WARN("UNNECESSARY_PARENTHESES",
3825189248d8SJoe Perches				     "Unnecessary parentheses$msg\n" . $herecurr);
3826189248d8SJoe Perches			}
3827189248d8SJoe Perches		}
3828189248d8SJoe Perches
382953a3c448SAndy Whitcroft# Return of what appears to be an errno should normally be -'ve
383053a3c448SAndy Whitcroft		if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) {
383153a3c448SAndy Whitcroft			my $name = $1;
383253a3c448SAndy Whitcroft			if ($name ne 'EOF' && $name ne 'ERROR') {
3833000d1cc1SJoe Perches				WARN("USE_NEGATIVE_ERRNO",
3834000d1cc1SJoe Perches				     "return of an errno should typically be -ve (return -$1)\n" . $herecurr);
383553a3c448SAndy Whitcroft			}
383653a3c448SAndy Whitcroft		}
3837c45dcabdSAndy Whitcroft
38380a920b5bSAndy Whitcroft# Need a space before open parenthesis after if, while etc
38394a0df2efSAndy Whitcroft		if ($line =~ /\b(if|while|for|switch)\(/) {
38403705ce5bSJoe Perches			if (ERROR("SPACING",
38413705ce5bSJoe Perches				  "space required before the open parenthesis '('\n" . $herecurr) &&
38423705ce5bSJoe Perches			    $fix) {
3843194f66fcSJoe Perches				$fixed[$fixlinenr] =~
38443705ce5bSJoe Perches				    s/\b(if|while|for|switch)\(/$1 \(/;
38453705ce5bSJoe Perches			}
38460a920b5bSAndy Whitcroft		}
38470a920b5bSAndy Whitcroft
3848f5fe35ddSAndy Whitcroft# Check for illegal assignment in if conditional -- and check for trailing
3849f5fe35ddSAndy Whitcroft# statements after the conditional.
3850170d3a22SAndy Whitcroft		if ($line =~ /do\s*(?!{)/) {
38513e469cdcSAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
38523e469cdcSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0)
38533e469cdcSAndy Whitcroft					if (!defined $stat);
3854170d3a22SAndy Whitcroft			my ($stat_next) = ctx_statement_block($line_nr_next,
3855170d3a22SAndy Whitcroft						$remain_next, $off_next);
3856170d3a22SAndy Whitcroft			$stat_next =~ s/\n./\n /g;
3857170d3a22SAndy Whitcroft			##print "stat<$stat> stat_next<$stat_next>\n";
3858170d3a22SAndy Whitcroft
3859170d3a22SAndy Whitcroft			if ($stat_next =~ /^\s*while\b/) {
3860170d3a22SAndy Whitcroft				# If the statement carries leading newlines,
3861170d3a22SAndy Whitcroft				# then count those as offsets.
3862170d3a22SAndy Whitcroft				my ($whitespace) =
3863170d3a22SAndy Whitcroft					($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
3864170d3a22SAndy Whitcroft				my $offset =
3865170d3a22SAndy Whitcroft					statement_rawlines($whitespace) - 1;
3866170d3a22SAndy Whitcroft
3867170d3a22SAndy Whitcroft				$suppress_whiletrailers{$line_nr_next +
3868170d3a22SAndy Whitcroft								$offset} = 1;
3869170d3a22SAndy Whitcroft			}
3870170d3a22SAndy Whitcroft		}
3871170d3a22SAndy Whitcroft		if (!defined $suppress_whiletrailers{$linenr} &&
3872c11230f4SJoe Perches		    defined($stat) && defined($cond) &&
3873170d3a22SAndy Whitcroft		    $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
3874171ae1a4SAndy Whitcroft			my ($s, $c) = ($stat, $cond);
38758905a67cSAndy Whitcroft
3876b53c8e10SAndy Whitcroft			if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
3877000d1cc1SJoe Perches				ERROR("ASSIGN_IN_IF",
3878000d1cc1SJoe Perches				      "do not use assignment in if condition\n" . $herecurr);
38798905a67cSAndy Whitcroft			}
38808905a67cSAndy Whitcroft
38818905a67cSAndy Whitcroft			# Find out what is on the end of the line after the
38828905a67cSAndy Whitcroft			# conditional.
3883773647a0SAndy Whitcroft			substr($s, 0, length($c), '');
38848905a67cSAndy Whitcroft			$s =~ s/\n.*//g;
388513214adfSAndy Whitcroft			$s =~ s/$;//g; 	# Remove any comments
388653210168SAndy Whitcroft			if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
388753210168SAndy Whitcroft			    $c !~ /}\s*while\s*/)
3888773647a0SAndy Whitcroft			{
3889bb44ad39SAndy Whitcroft				# Find out how long the conditional actually is.
3890bb44ad39SAndy Whitcroft				my @newlines = ($c =~ /\n/gs);
3891bb44ad39SAndy Whitcroft				my $cond_lines = 1 + $#newlines;
389242bdf74cSHidetoshi Seto				my $stat_real = '';
3893bb44ad39SAndy Whitcroft
389442bdf74cSHidetoshi Seto				$stat_real = raw_line($linenr, $cond_lines)
389542bdf74cSHidetoshi Seto							. "\n" if ($cond_lines);
3896bb44ad39SAndy Whitcroft				if (defined($stat_real) && $cond_lines > 1) {
3897bb44ad39SAndy Whitcroft					$stat_real = "[...]\n$stat_real";
3898bb44ad39SAndy Whitcroft				}
3899bb44ad39SAndy Whitcroft
3900000d1cc1SJoe Perches				ERROR("TRAILING_STATEMENTS",
3901000d1cc1SJoe Perches				      "trailing statements should be on next line\n" . $herecurr . $stat_real);
39028905a67cSAndy Whitcroft			}
39038905a67cSAndy Whitcroft		}
39048905a67cSAndy Whitcroft
390513214adfSAndy Whitcroft# Check for bitwise tests written as boolean
390613214adfSAndy Whitcroft		if ($line =~ /
390713214adfSAndy Whitcroft			(?:
390813214adfSAndy Whitcroft				(?:\[|\(|\&\&|\|\|)
390913214adfSAndy Whitcroft				\s*0[xX][0-9]+\s*
391013214adfSAndy Whitcroft				(?:\&\&|\|\|)
391113214adfSAndy Whitcroft			|
391213214adfSAndy Whitcroft				(?:\&\&|\|\|)
391313214adfSAndy Whitcroft				\s*0[xX][0-9]+\s*
391413214adfSAndy Whitcroft				(?:\&\&|\|\||\)|\])
391513214adfSAndy Whitcroft			)/x)
391613214adfSAndy Whitcroft		{
3917000d1cc1SJoe Perches			WARN("HEXADECIMAL_BOOLEAN_TEST",
3918000d1cc1SJoe Perches			     "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
391913214adfSAndy Whitcroft		}
392013214adfSAndy Whitcroft
39218905a67cSAndy Whitcroft# if and else should not have general statements after it
392213214adfSAndy Whitcroft		if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
392313214adfSAndy Whitcroft			my $s = $1;
392413214adfSAndy Whitcroft			$s =~ s/$;//g; 	# Remove any comments
392513214adfSAndy Whitcroft			if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
3926000d1cc1SJoe Perches				ERROR("TRAILING_STATEMENTS",
3927000d1cc1SJoe Perches				      "trailing statements should be on next line\n" . $herecurr);
39280a920b5bSAndy Whitcroft			}
392913214adfSAndy Whitcroft		}
393039667782SAndy Whitcroft# if should not continue a brace
393139667782SAndy Whitcroft		if ($line =~ /}\s*if\b/) {
3932000d1cc1SJoe Perches			ERROR("TRAILING_STATEMENTS",
3933048b123fSRasmus Villemoes			      "trailing statements should be on next line (or did you mean 'else if'?)\n" .
393439667782SAndy Whitcroft				$herecurr);
393539667782SAndy Whitcroft		}
3936a1080bf8SAndy Whitcroft# case and default should not have general statements after them
3937a1080bf8SAndy Whitcroft		if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
3938a1080bf8SAndy Whitcroft		    $line !~ /\G(?:
39393fef12d6SAndy Whitcroft			(?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
3940a1080bf8SAndy Whitcroft			\s*return\s+
3941a1080bf8SAndy Whitcroft		    )/xg)
3942a1080bf8SAndy Whitcroft		{
3943000d1cc1SJoe Perches			ERROR("TRAILING_STATEMENTS",
3944000d1cc1SJoe Perches			      "trailing statements should be on next line\n" . $herecurr);
3945a1080bf8SAndy Whitcroft		}
39460a920b5bSAndy Whitcroft
39470a920b5bSAndy Whitcroft		# Check for }<nl>else {, these must be at the same
39480a920b5bSAndy Whitcroft		# indent level to be relevant to each other.
39498b8856f4SJoe Perches		if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ &&
39500a920b5bSAndy Whitcroft		    $previndent == $indent) {
39518b8856f4SJoe Perches			if (ERROR("ELSE_AFTER_BRACE",
39528b8856f4SJoe Perches				  "else should follow close brace '}'\n" . $hereprev) &&
39538b8856f4SJoe Perches			    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
39548b8856f4SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
39558b8856f4SJoe Perches				fix_delete_line($fixlinenr, $rawline);
39568b8856f4SJoe Perches				my $fixedline = $prevrawline;
39578b8856f4SJoe Perches				$fixedline =~ s/}\s*$//;
39588b8856f4SJoe Perches				if ($fixedline !~ /^\+\s*$/) {
39598b8856f4SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
39608b8856f4SJoe Perches				}
39618b8856f4SJoe Perches				$fixedline = $rawline;
39628b8856f4SJoe Perches				$fixedline =~ s/^(.\s*)else/$1} else/;
39638b8856f4SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
39648b8856f4SJoe Perches			}
39650a920b5bSAndy Whitcroft		}
39660a920b5bSAndy Whitcroft
39678b8856f4SJoe Perches		if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ &&
3968c2fdda0dSAndy Whitcroft		    $previndent == $indent) {
3969c2fdda0dSAndy Whitcroft			my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
3970c2fdda0dSAndy Whitcroft
3971c2fdda0dSAndy Whitcroft			# Find out what is on the end of the line after the
3972c2fdda0dSAndy Whitcroft			# conditional.
3973773647a0SAndy Whitcroft			substr($s, 0, length($c), '');
3974c2fdda0dSAndy Whitcroft			$s =~ s/\n.*//g;
3975c2fdda0dSAndy Whitcroft
3976c2fdda0dSAndy Whitcroft			if ($s =~ /^\s*;/) {
39778b8856f4SJoe Perches				if (ERROR("WHILE_AFTER_BRACE",
39788b8856f4SJoe Perches					  "while should follow close brace '}'\n" . $hereprev) &&
39798b8856f4SJoe Perches				    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
39808b8856f4SJoe Perches					fix_delete_line($fixlinenr - 1, $prevrawline);
39818b8856f4SJoe Perches					fix_delete_line($fixlinenr, $rawline);
39828b8856f4SJoe Perches					my $fixedline = $prevrawline;
39838b8856f4SJoe Perches					my $trailing = $rawline;
39848b8856f4SJoe Perches					$trailing =~ s/^\+//;
39858b8856f4SJoe Perches					$trailing = trim($trailing);
39868b8856f4SJoe Perches					$fixedline =~ s/}\s*$/} $trailing/;
39878b8856f4SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
39888b8856f4SJoe Perches				}
3989c2fdda0dSAndy Whitcroft			}
3990c2fdda0dSAndy Whitcroft		}
3991c2fdda0dSAndy Whitcroft
399295e2c602SJoe Perches#Specific variable tests
3993323c1260SJoe Perches		while ($line =~ m{($Constant|$Lval)}g) {
3994323c1260SJoe Perches			my $var = $1;
399595e2c602SJoe Perches
399695e2c602SJoe Perches#gcc binary extension
399795e2c602SJoe Perches			if ($var =~ /^$Binary$/) {
3998d5e616fcSJoe Perches				if (WARN("GCC_BINARY_CONSTANT",
3999d5e616fcSJoe Perches					 "Avoid gcc v4.3+ binary constant extension: <$var>\n" . $herecurr) &&
4000d5e616fcSJoe Perches				    $fix) {
4001d5e616fcSJoe Perches					my $hexval = sprintf("0x%x", oct($var));
4002194f66fcSJoe Perches					$fixed[$fixlinenr] =~
4003d5e616fcSJoe Perches					    s/\b$var\b/$hexval/;
4004d5e616fcSJoe Perches				}
400595e2c602SJoe Perches			}
400695e2c602SJoe Perches
400795e2c602SJoe Perches#CamelCase
4008807bd26cSJoe Perches			if ($var !~ /^$Constant$/ &&
4009be79794bSJoe Perches			    $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
401022735ce8SJoe Perches#Ignore Page<foo> variants
4011807bd26cSJoe Perches			    $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
401222735ce8SJoe Perches#Ignore SI style variants like nS, mV and dB (ie: max_uV, regulator_min_uA_show)
40133445686aSJoe Perches			    $var !~ /^(?:[a-z_]*?)_?[a-z][A-Z](?:_[a-z_]+)?$/) {
40147e781f67SJoe Perches				while ($var =~ m{($Ident)}g) {
40157e781f67SJoe Perches					my $word = $1;
40167e781f67SJoe Perches					next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
4017d8b07710SJoe Perches					if ($check) {
4018d8b07710SJoe Perches						seed_camelcase_includes();
4019d8b07710SJoe Perches						if (!$file && !$camelcase_file_seeded) {
4020d8b07710SJoe Perches							seed_camelcase_file($realfile);
4021d8b07710SJoe Perches							$camelcase_file_seeded = 1;
4022d8b07710SJoe Perches						}
4023d8b07710SJoe Perches					}
40247e781f67SJoe Perches					if (!defined $camelcase{$word}) {
40257e781f67SJoe Perches						$camelcase{$word} = 1;
4026be79794bSJoe Perches						CHK("CAMELCASE",
40277e781f67SJoe Perches						    "Avoid CamelCase: <$word>\n" . $herecurr);
40287e781f67SJoe Perches					}
4029323c1260SJoe Perches				}
4030323c1260SJoe Perches			}
40313445686aSJoe Perches		}
40320a920b5bSAndy Whitcroft
40330a920b5bSAndy Whitcroft#no spaces allowed after \ in define
4034d5e616fcSJoe Perches		if ($line =~ /\#\s*define.*\\\s+$/) {
4035d5e616fcSJoe Perches			if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
4036d5e616fcSJoe Perches				 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
4037d5e616fcSJoe Perches			    $fix) {
4038194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\s+$//;
4039d5e616fcSJoe Perches			}
40400a920b5bSAndy Whitcroft		}
40410a920b5bSAndy Whitcroft
4042653d4876SAndy Whitcroft#warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
4043c45dcabdSAndy Whitcroft		if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
4044e09dec48SAndy Whitcroft			my $file = "$1.h";
4045e09dec48SAndy Whitcroft			my $checkfile = "include/linux/$file";
4046e09dec48SAndy Whitcroft			if (-f "$root/$checkfile" &&
4047e09dec48SAndy Whitcroft			    $realfile ne $checkfile &&
40487840a94cSWolfram Sang			    $1 !~ /$allowed_asm_includes/)
4049c45dcabdSAndy Whitcroft			{
4050e09dec48SAndy Whitcroft				if ($realfile =~ m{^arch/}) {
4051000d1cc1SJoe Perches					CHK("ARCH_INCLUDE_LINUX",
4052000d1cc1SJoe Perches					    "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
4053e09dec48SAndy Whitcroft				} else {
4054000d1cc1SJoe Perches					WARN("INCLUDE_LINUX",
4055000d1cc1SJoe Perches					     "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
4056e09dec48SAndy Whitcroft				}
40570a920b5bSAndy Whitcroft			}
40580a920b5bSAndy Whitcroft		}
40590a920b5bSAndy Whitcroft
4060653d4876SAndy Whitcroft# multi-statement macros should be enclosed in a do while loop, grab the
4061653d4876SAndy Whitcroft# first statement and ensure its the whole macro if its not enclosed
4062cf655043SAndy Whitcroft# in a known good container
4063b8f96a31SAndy Whitcroft		if ($realfile !~ m@/vmlinux.lds.h$@ &&
4064b8f96a31SAndy Whitcroft		    $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
4065d8aaf121SAndy Whitcroft			my $ln = $linenr;
4066d8aaf121SAndy Whitcroft			my $cnt = $realcnt;
4067c45dcabdSAndy Whitcroft			my ($off, $dstat, $dcond, $rest);
4068c45dcabdSAndy Whitcroft			my $ctx = '';
4069c45dcabdSAndy Whitcroft			($dstat, $dcond, $ln, $cnt, $off) =
4070f74bd194SAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0);
4071f74bd194SAndy Whitcroft			$ctx = $dstat;
4072c45dcabdSAndy Whitcroft			#print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
4073a3bb97a7SAndy Whitcroft			#print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
4074c45dcabdSAndy Whitcroft
4075f74bd194SAndy Whitcroft			$dstat =~ s/^.\s*\#\s*define\s+$Ident(?:\([^\)]*\))?\s*//;
4076292f1a9bSAndy Whitcroft			$dstat =~ s/$;//g;
4077c45dcabdSAndy Whitcroft			$dstat =~ s/\\\n.//g;
4078c45dcabdSAndy Whitcroft			$dstat =~ s/^\s*//s;
4079c45dcabdSAndy Whitcroft			$dstat =~ s/\s*$//s;
4080c45dcabdSAndy Whitcroft
4081c45dcabdSAndy Whitcroft			# Flatten any parentheses and braces
4082bf30d6edSAndy Whitcroft			while ($dstat =~ s/\([^\(\)]*\)/1/ ||
4083bf30d6edSAndy Whitcroft			       $dstat =~ s/\{[^\{\}]*\}/1/ ||
4084c81769fdSAndy Whitcroft			       $dstat =~ s/\[[^\[\]]*\]/1/)
4085bf30d6edSAndy Whitcroft			{
4086c45dcabdSAndy Whitcroft			}
4087c45dcabdSAndy Whitcroft
4088e45bab8eSAndy Whitcroft			# Flatten any obvious string concatentation.
4089e45bab8eSAndy Whitcroft			while ($dstat =~ s/("X*")\s*$Ident/$1/ ||
4090e45bab8eSAndy Whitcroft			       $dstat =~ s/$Ident\s*("X*")/$1/)
4091e45bab8eSAndy Whitcroft			{
4092e45bab8eSAndy Whitcroft			}
4093e45bab8eSAndy Whitcroft
4094c45dcabdSAndy Whitcroft			my $exceptions = qr{
4095c45dcabdSAndy Whitcroft				$Declare|
4096c45dcabdSAndy Whitcroft				module_param_named|
4097a0a0a7a9SKees Cook				MODULE_PARM_DESC|
4098c45dcabdSAndy Whitcroft				DECLARE_PER_CPU|
4099c45dcabdSAndy Whitcroft				DEFINE_PER_CPU|
4100383099fdSAndy Whitcroft				__typeof__\(|
410122fd2d3eSStefani Seibold				union|
410222fd2d3eSStefani Seibold				struct|
4103ea71a0a0SAndy Whitcroft				\.$Ident\s*=\s*|
4104ea71a0a0SAndy Whitcroft				^\"|\"$
4105c45dcabdSAndy Whitcroft			}x;
41065eaa20b9SAndy Whitcroft			#print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
4107f74bd194SAndy Whitcroft			if ($dstat ne '' &&
4108f74bd194SAndy Whitcroft			    $dstat !~ /^(?:$Ident|-?$Constant),$/ &&			# 10, // foo(),
4109f74bd194SAndy Whitcroft			    $dstat !~ /^(?:$Ident|-?$Constant);$/ &&			# foo();
41103cc4b1c3SJoe Perches			    $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ &&		# 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
4111356fd398SJoe Perches			    $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ &&			# character constants
4112f74bd194SAndy Whitcroft			    $dstat !~ /$exceptions/ &&
4113f74bd194SAndy Whitcroft			    $dstat !~ /^\.$Ident\s*=/ &&				# .foo =
4114e942e2c3SJoe Perches			    $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ &&		# stringification #foo
411572f115f9SAndy Whitcroft			    $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ &&	# do {...} while (...); // do {...} while (...)
4116f74bd194SAndy Whitcroft			    $dstat !~ /^for\s*$Constant$/ &&				# for (...)
4117f74bd194SAndy Whitcroft			    $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ &&	# for (...) bar()
4118f74bd194SAndy Whitcroft			    $dstat !~ /^do\s*{/ &&					# do {...
4119f95a7e6aSJoe Perches			    $dstat !~ /^\({/ &&						# ({...
4120f95a7e6aSJoe Perches			    $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
4121c45dcabdSAndy Whitcroft			{
4122f74bd194SAndy Whitcroft				$ctx =~ s/\n*$//;
4123f74bd194SAndy Whitcroft				my $herectx = $here . "\n";
4124f74bd194SAndy Whitcroft				my $cnt = statement_rawlines($ctx);
4125f74bd194SAndy Whitcroft
4126f74bd194SAndy Whitcroft				for (my $n = 0; $n < $cnt; $n++) {
4127f74bd194SAndy Whitcroft					$herectx .= raw_line($linenr, $n) . "\n";
4128c45dcabdSAndy Whitcroft				}
4129c45dcabdSAndy Whitcroft
4130f74bd194SAndy Whitcroft				if ($dstat =~ /;/) {
4131f74bd194SAndy Whitcroft					ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
4132f74bd194SAndy Whitcroft					      "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
4133f74bd194SAndy Whitcroft				} else {
4134000d1cc1SJoe Perches					ERROR("COMPLEX_MACRO",
4135388982b5SAndrew Morton					      "Macros with complex values should be enclosed in parentheses\n" . "$herectx");
4136d8aaf121SAndy Whitcroft				}
41370a920b5bSAndy Whitcroft			}
41385023d347SJoe Perches
4139481eb486SJoe Perches# check for line continuations outside of #defines, preprocessor #, and asm
41405023d347SJoe Perches
41415023d347SJoe Perches		} else {
41425023d347SJoe Perches			if ($prevline !~ /^..*\\$/ &&
4143481eb486SJoe Perches			    $line !~ /^\+\s*\#.*\\$/ &&		# preprocessor
4144481eb486SJoe Perches			    $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ &&	# asm
41455023d347SJoe Perches			    $line =~ /^\+.*\\$/) {
41465023d347SJoe Perches				WARN("LINE_CONTINUATIONS",
41475023d347SJoe Perches				     "Avoid unnecessary line continuations\n" . $herecurr);
41485023d347SJoe Perches			}
4149653d4876SAndy Whitcroft		}
41500a920b5bSAndy Whitcroft
4151b13edf7fSJoe Perches# do {} while (0) macro tests:
4152b13edf7fSJoe Perches# single-statement macros do not need to be enclosed in do while (0) loop,
4153b13edf7fSJoe Perches# macro should not end with a semicolon
4154b13edf7fSJoe Perches		if ($^V && $^V ge 5.10.0 &&
4155b13edf7fSJoe Perches		    $realfile !~ m@/vmlinux.lds.h$@ &&
4156b13edf7fSJoe Perches		    $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
4157b13edf7fSJoe Perches			my $ln = $linenr;
4158b13edf7fSJoe Perches			my $cnt = $realcnt;
4159b13edf7fSJoe Perches			my ($off, $dstat, $dcond, $rest);
4160b13edf7fSJoe Perches			my $ctx = '';
4161b13edf7fSJoe Perches			($dstat, $dcond, $ln, $cnt, $off) =
4162b13edf7fSJoe Perches				ctx_statement_block($linenr, $realcnt, 0);
4163b13edf7fSJoe Perches			$ctx = $dstat;
4164b13edf7fSJoe Perches
4165b13edf7fSJoe Perches			$dstat =~ s/\\\n.//g;
4166b13edf7fSJoe Perches
4167b13edf7fSJoe Perches			if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
4168b13edf7fSJoe Perches				my $stmts = $2;
4169b13edf7fSJoe Perches				my $semis = $3;
4170b13edf7fSJoe Perches
4171b13edf7fSJoe Perches				$ctx =~ s/\n*$//;
4172b13edf7fSJoe Perches				my $cnt = statement_rawlines($ctx);
4173b13edf7fSJoe Perches				my $herectx = $here . "\n";
4174b13edf7fSJoe Perches
4175b13edf7fSJoe Perches				for (my $n = 0; $n < $cnt; $n++) {
4176b13edf7fSJoe Perches					$herectx .= raw_line($linenr, $n) . "\n";
4177b13edf7fSJoe Perches				}
4178b13edf7fSJoe Perches
4179ac8e97f8SJoe Perches				if (($stmts =~ tr/;/;/) == 1 &&
4180ac8e97f8SJoe Perches				    $stmts !~ /^\s*(if|while|for|switch)\b/) {
4181b13edf7fSJoe Perches					WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
4182b13edf7fSJoe Perches					     "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
4183b13edf7fSJoe Perches				}
4184b13edf7fSJoe Perches				if (defined $semis && $semis ne "") {
4185b13edf7fSJoe Perches					WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
4186b13edf7fSJoe Perches					     "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
4187b13edf7fSJoe Perches				}
4188f5ef95b1SJoe Perches			} elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) {
4189f5ef95b1SJoe Perches				$ctx =~ s/\n*$//;
4190f5ef95b1SJoe Perches				my $cnt = statement_rawlines($ctx);
4191f5ef95b1SJoe Perches				my $herectx = $here . "\n";
4192f5ef95b1SJoe Perches
4193f5ef95b1SJoe Perches				for (my $n = 0; $n < $cnt; $n++) {
4194f5ef95b1SJoe Perches					$herectx .= raw_line($linenr, $n) . "\n";
4195f5ef95b1SJoe Perches				}
4196f5ef95b1SJoe Perches
4197f5ef95b1SJoe Perches				WARN("TRAILING_SEMICOLON",
4198f5ef95b1SJoe Perches				     "macros should not use a trailing semicolon\n" . "$herectx");
4199b13edf7fSJoe Perches			}
4200b13edf7fSJoe Perches		}
4201b13edf7fSJoe Perches
4202080ba929SMike Frysinger# make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
4203080ba929SMike Frysinger# all assignments may have only one of the following with an assignment:
4204080ba929SMike Frysinger#	.
4205080ba929SMike Frysinger#	ALIGN(...)
4206080ba929SMike Frysinger#	VMLINUX_SYMBOL(...)
4207080ba929SMike Frysinger		if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
4208000d1cc1SJoe Perches			WARN("MISSING_VMLINUX_SYMBOL",
4209000d1cc1SJoe Perches			     "vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
4210080ba929SMike Frysinger		}
4211080ba929SMike Frysinger
4212f0a594c1SAndy Whitcroft# check for redundant bracing round if etc
421313214adfSAndy Whitcroft		if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
421413214adfSAndy Whitcroft			my ($level, $endln, @chunks) =
4215cf655043SAndy Whitcroft				ctx_statement_full($linenr, $realcnt, 1);
421613214adfSAndy Whitcroft			#print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
4217cf655043SAndy Whitcroft			#print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
4218cf655043SAndy Whitcroft			if ($#chunks > 0 && $level == 0) {
4219aad4f614SJoe Perches				my @allowed = ();
4220aad4f614SJoe Perches				my $allow = 0;
422113214adfSAndy Whitcroft				my $seen = 0;
4222773647a0SAndy Whitcroft				my $herectx = $here . "\n";
4223cf655043SAndy Whitcroft				my $ln = $linenr - 1;
422413214adfSAndy Whitcroft				for my $chunk (@chunks) {
422513214adfSAndy Whitcroft					my ($cond, $block) = @{$chunk};
422613214adfSAndy Whitcroft
4227773647a0SAndy Whitcroft					# If the condition carries leading newlines, then count those as offsets.
4228773647a0SAndy Whitcroft					my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
4229773647a0SAndy Whitcroft					my $offset = statement_rawlines($whitespace) - 1;
4230773647a0SAndy Whitcroft
4231aad4f614SJoe Perches					$allowed[$allow] = 0;
4232773647a0SAndy Whitcroft					#print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
4233773647a0SAndy Whitcroft
4234773647a0SAndy Whitcroft					# We have looked at and allowed this specific line.
4235773647a0SAndy Whitcroft					$suppress_ifbraces{$ln + $offset} = 1;
4236773647a0SAndy Whitcroft
4237773647a0SAndy Whitcroft					$herectx .= "$rawlines[$ln + $offset]\n[...]\n";
4238cf655043SAndy Whitcroft					$ln += statement_rawlines($block) - 1;
4239cf655043SAndy Whitcroft
4240773647a0SAndy Whitcroft					substr($block, 0, length($cond), '');
424113214adfSAndy Whitcroft
424213214adfSAndy Whitcroft					$seen++ if ($block =~ /^\s*{/);
424313214adfSAndy Whitcroft
4244aad4f614SJoe Perches					#print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
4245cf655043SAndy Whitcroft					if (statement_lines($cond) > 1) {
4246cf655043SAndy Whitcroft						#print "APW: ALLOWED: cond<$cond>\n";
4247aad4f614SJoe Perches						$allowed[$allow] = 1;
424813214adfSAndy Whitcroft					}
424913214adfSAndy Whitcroft					if ($block =~/\b(?:if|for|while)\b/) {
4250cf655043SAndy Whitcroft						#print "APW: ALLOWED: block<$block>\n";
4251aad4f614SJoe Perches						$allowed[$allow] = 1;
425213214adfSAndy Whitcroft					}
4253cf655043SAndy Whitcroft					if (statement_block_size($block) > 1) {
4254cf655043SAndy Whitcroft						#print "APW: ALLOWED: lines block<$block>\n";
4255aad4f614SJoe Perches						$allowed[$allow] = 1;
425613214adfSAndy Whitcroft					}
4257aad4f614SJoe Perches					$allow++;
425813214adfSAndy Whitcroft				}
4259aad4f614SJoe Perches				if ($seen) {
4260aad4f614SJoe Perches					my $sum_allowed = 0;
4261aad4f614SJoe Perches					foreach (@allowed) {
4262aad4f614SJoe Perches						$sum_allowed += $_;
4263aad4f614SJoe Perches					}
4264aad4f614SJoe Perches					if ($sum_allowed == 0) {
4265000d1cc1SJoe Perches						WARN("BRACES",
4266000d1cc1SJoe Perches						     "braces {} are not necessary for any arm of this statement\n" . $herectx);
4267aad4f614SJoe Perches					} elsif ($sum_allowed != $allow &&
4268aad4f614SJoe Perches						 $seen != $allow) {
4269aad4f614SJoe Perches						CHK("BRACES",
4270aad4f614SJoe Perches						    "braces {} should be used on all arms of this statement\n" . $herectx);
4271aad4f614SJoe Perches					}
427213214adfSAndy Whitcroft				}
427313214adfSAndy Whitcroft			}
427413214adfSAndy Whitcroft		}
4275773647a0SAndy Whitcroft		if (!defined $suppress_ifbraces{$linenr - 1} &&
427613214adfSAndy Whitcroft					$line =~ /\b(if|while|for|else)\b/) {
4277cf655043SAndy Whitcroft			my $allowed = 0;
4278f0a594c1SAndy Whitcroft
4279cf655043SAndy Whitcroft			# Check the pre-context.
4280cf655043SAndy Whitcroft			if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
4281cf655043SAndy Whitcroft				#print "APW: ALLOWED: pre<$1>\n";
4282cf655043SAndy Whitcroft				$allowed = 1;
4283f0a594c1SAndy Whitcroft			}
4284773647a0SAndy Whitcroft
4285773647a0SAndy Whitcroft			my ($level, $endln, @chunks) =
4286773647a0SAndy Whitcroft				ctx_statement_full($linenr, $realcnt, $-[0]);
4287773647a0SAndy Whitcroft
4288cf655043SAndy Whitcroft			# Check the condition.
4289cf655043SAndy Whitcroft			my ($cond, $block) = @{$chunks[0]};
4290773647a0SAndy Whitcroft			#print "CHECKING<$linenr> cond<$cond> block<$block>\n";
4291cf655043SAndy Whitcroft			if (defined $cond) {
4292773647a0SAndy Whitcroft				substr($block, 0, length($cond), '');
4293cf655043SAndy Whitcroft			}
4294cf655043SAndy Whitcroft			if (statement_lines($cond) > 1) {
4295cf655043SAndy Whitcroft				#print "APW: ALLOWED: cond<$cond>\n";
4296cf655043SAndy Whitcroft				$allowed = 1;
4297cf655043SAndy Whitcroft			}
4298cf655043SAndy Whitcroft			if ($block =~/\b(?:if|for|while)\b/) {
4299cf655043SAndy Whitcroft				#print "APW: ALLOWED: block<$block>\n";
4300cf655043SAndy Whitcroft				$allowed = 1;
4301cf655043SAndy Whitcroft			}
4302cf655043SAndy Whitcroft			if (statement_block_size($block) > 1) {
4303cf655043SAndy Whitcroft				#print "APW: ALLOWED: lines block<$block>\n";
4304cf655043SAndy Whitcroft				$allowed = 1;
4305cf655043SAndy Whitcroft			}
4306cf655043SAndy Whitcroft			# Check the post-context.
4307cf655043SAndy Whitcroft			if (defined $chunks[1]) {
4308cf655043SAndy Whitcroft				my ($cond, $block) = @{$chunks[1]};
4309cf655043SAndy Whitcroft				if (defined $cond) {
4310773647a0SAndy Whitcroft					substr($block, 0, length($cond), '');
4311cf655043SAndy Whitcroft				}
4312cf655043SAndy Whitcroft				if ($block =~ /^\s*\{/) {
4313cf655043SAndy Whitcroft					#print "APW: ALLOWED: chunk-1 block<$block>\n";
4314cf655043SAndy Whitcroft					$allowed = 1;
4315cf655043SAndy Whitcroft				}
4316cf655043SAndy Whitcroft			}
4317cf655043SAndy Whitcroft			if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
431869932487SJustin P. Mattock				my $herectx = $here . "\n";
4319f055663cSAndy Whitcroft				my $cnt = statement_rawlines($block);
4320cf655043SAndy Whitcroft
4321f055663cSAndy Whitcroft				for (my $n = 0; $n < $cnt; $n++) {
432269932487SJustin P. Mattock					$herectx .= raw_line($linenr, $n) . "\n";
4323cf655043SAndy Whitcroft				}
4324cf655043SAndy Whitcroft
4325000d1cc1SJoe Perches				WARN("BRACES",
4326000d1cc1SJoe Perches				     "braces {} are not necessary for single statement blocks\n" . $herectx);
4327f0a594c1SAndy Whitcroft			}
4328f0a594c1SAndy Whitcroft		}
4329f0a594c1SAndy Whitcroft
43300979ae66SJoe Perches# check for unnecessary blank lines around braces
433177b9a53aSJoe Perches		if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
43320979ae66SJoe Perches			CHK("BRACES",
43330979ae66SJoe Perches			    "Blank lines aren't necessary before a close brace '}'\n" . $hereprev);
43340979ae66SJoe Perches		}
433577b9a53aSJoe Perches		if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
43360979ae66SJoe Perches			CHK("BRACES",
43370979ae66SJoe Perches			    "Blank lines aren't necessary after an open brace '{'\n" . $hereprev);
43380979ae66SJoe Perches		}
43390979ae66SJoe Perches
43404a0df2efSAndy Whitcroft# no volatiles please
43416c72ffaaSAndy Whitcroft		my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
43426c72ffaaSAndy Whitcroft		if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
4343000d1cc1SJoe Perches			WARN("VOLATILE",
4344000d1cc1SJoe Perches			     "Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
43454a0df2efSAndy Whitcroft		}
43464a0df2efSAndy Whitcroft
4347f17dba4fSJoe Perches# concatenated string without spaces between elements
4348f17dba4fSJoe Perches		if ($line =~ /"X+"[A-Z_]+/ || $line =~ /[A-Z_]+"X+"/) {
4349f17dba4fSJoe Perches			CHK("CONCATENATED_STRING",
4350f17dba4fSJoe Perches			    "Concatenated strings should use spaces between elements\n" . $herecurr);
4351f17dba4fSJoe Perches		}
4352f17dba4fSJoe Perches
435300df344fSAndy Whitcroft# warn about #if 0
4354c45dcabdSAndy Whitcroft		if ($line =~ /^.\s*\#\s*if\s+0\b/) {
4355000d1cc1SJoe Perches			CHK("REDUNDANT_CODE",
4356000d1cc1SJoe Perches			    "if this code is redundant consider removing it\n" .
4357de7d4f0eSAndy Whitcroft				$herecurr);
43584a0df2efSAndy Whitcroft		}
43594a0df2efSAndy Whitcroft
436003df4b51SAndy Whitcroft# check for needless "if (<foo>) fn(<foo>)" uses
436103df4b51SAndy Whitcroft		if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
436203df4b51SAndy Whitcroft			my $expr = '\s*\(\s*' . quotemeta($1) . '\s*\)\s*;';
436303df4b51SAndy Whitcroft			if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?)$expr/) {
436403df4b51SAndy Whitcroft				WARN('NEEDLESS_IF',
436503df4b51SAndy Whitcroft				     "$1(NULL) is safe this check is probably not required\n" . $hereprev);
43664c432a8fSGreg Kroah-Hartman			}
43674c432a8fSGreg Kroah-Hartman		}
4368f0a594c1SAndy Whitcroft
4369ebfdc409SJoe Perches# check for unnecessary "Out of Memory" messages
4370ebfdc409SJoe Perches		if ($line =~ /^\+.*\b$logFunctions\s*\(/ &&
4371ebfdc409SJoe Perches		    $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ &&
4372ebfdc409SJoe Perches		    (defined $1 || defined $3) &&
4373ebfdc409SJoe Perches		    $linenr > 3) {
4374ebfdc409SJoe Perches			my $testval = $2;
4375ebfdc409SJoe Perches			my $testline = $lines[$linenr - 3];
4376ebfdc409SJoe Perches
4377ebfdc409SJoe Perches			my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0);
4378ebfdc409SJoe Perches#			print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n");
4379ebfdc409SJoe Perches
4380ebfdc409SJoe 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)/) {
4381ebfdc409SJoe Perches				WARN("OOM_MESSAGE",
4382ebfdc409SJoe Perches				     "Possible unnecessary 'out of memory' message\n" . $hereprev);
4383ebfdc409SJoe Perches			}
4384ebfdc409SJoe Perches		}
4385ebfdc409SJoe Perches
43868716de38SJoe Perches# check for bad placement of section $InitAttribute (e.g.: __initdata)
43878716de38SJoe Perches		if ($line =~ /(\b$InitAttribute\b)/) {
43888716de38SJoe Perches			my $attr = $1;
43898716de38SJoe Perches			if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
43908716de38SJoe Perches				my $ptr = $1;
43918716de38SJoe Perches				my $var = $2;
43928716de38SJoe Perches				if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
43938716de38SJoe Perches				      ERROR("MISPLACED_INIT",
43948716de38SJoe Perches					    "$attr should be placed after $var\n" . $herecurr)) ||
43958716de38SJoe Perches				     ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
43968716de38SJoe Perches				      WARN("MISPLACED_INIT",
43978716de38SJoe Perches					   "$attr should be placed after $var\n" . $herecurr))) &&
43988716de38SJoe Perches				    $fix) {
4399194f66fcSJoe 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;
44008716de38SJoe Perches				}
44018716de38SJoe Perches			}
44028716de38SJoe Perches		}
44038716de38SJoe Perches
4404e970b884SJoe Perches# check for $InitAttributeData (ie: __initdata) with const
4405e970b884SJoe Perches		if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
4406e970b884SJoe Perches			my $attr = $1;
4407e970b884SJoe Perches			$attr =~ /($InitAttributePrefix)(.*)/;
4408e970b884SJoe Perches			my $attr_prefix = $1;
4409e970b884SJoe Perches			my $attr_type = $2;
4410e970b884SJoe Perches			if (ERROR("INIT_ATTRIBUTE",
4411e970b884SJoe Perches				  "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
4412e970b884SJoe Perches			    $fix) {
4413194f66fcSJoe Perches				$fixed[$fixlinenr] =~
4414e970b884SJoe Perches				    s/$InitAttributeData/${attr_prefix}initconst/;
4415e970b884SJoe Perches			}
4416e970b884SJoe Perches		}
4417e970b884SJoe Perches
4418e970b884SJoe Perches# check for $InitAttributeConst (ie: __initconst) without const
4419e970b884SJoe Perches		if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
4420e970b884SJoe Perches			my $attr = $1;
4421e970b884SJoe Perches			if (ERROR("INIT_ATTRIBUTE",
4422e970b884SJoe Perches				  "Use of $attr requires a separate use of const\n" . $herecurr) &&
4423e970b884SJoe Perches			    $fix) {
4424194f66fcSJoe Perches				my $lead = $fixed[$fixlinenr] =~
4425e970b884SJoe Perches				    /(^\+\s*(?:static\s+))/;
4426e970b884SJoe Perches				$lead = rtrim($1);
4427e970b884SJoe Perches				$lead = "$lead " if ($lead !~ /^\+$/);
4428e970b884SJoe Perches				$lead = "${lead}const ";
4429194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/;
4430e970b884SJoe Perches			}
4431e970b884SJoe Perches		}
4432e970b884SJoe Perches
4433fbdb8138SJoe Perches# don't use __constant_<foo> functions outside of include/uapi/
4434fbdb8138SJoe Perches		if ($realfile !~ m@^include/uapi/@ &&
4435fbdb8138SJoe Perches		    $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) {
4436fbdb8138SJoe Perches			my $constant_func = $1;
4437fbdb8138SJoe Perches			my $func = $constant_func;
4438fbdb8138SJoe Perches			$func =~ s/^__constant_//;
4439fbdb8138SJoe Perches			if (WARN("CONSTANT_CONVERSION",
4440fbdb8138SJoe Perches				 "$constant_func should be $func\n" . $herecurr) &&
4441fbdb8138SJoe Perches			    $fix) {
4442194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g;
4443fbdb8138SJoe Perches			}
4444fbdb8138SJoe Perches		}
4445fbdb8138SJoe Perches
44461a15a250SPatrick Pannuto# prefer usleep_range over udelay
444737581c28SBruce Allan		if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
444843c1d77cSJoe Perches			my $delay = $1;
44491a15a250SPatrick Pannuto			# ignore udelay's < 10, however
445043c1d77cSJoe Perches			if (! ($delay < 10) ) {
4451000d1cc1SJoe Perches				CHK("USLEEP_RANGE",
445243c1d77cSJoe Perches				    "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $herecurr);
445343c1d77cSJoe Perches			}
445443c1d77cSJoe Perches			if ($delay > 2000) {
445543c1d77cSJoe Perches				WARN("LONG_UDELAY",
445643c1d77cSJoe Perches				     "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr);
44571a15a250SPatrick Pannuto			}
44581a15a250SPatrick Pannuto		}
44591a15a250SPatrick Pannuto
446009ef8725SPatrick Pannuto# warn about unexpectedly long msleep's
446109ef8725SPatrick Pannuto		if ($line =~ /\bmsleep\s*\((\d+)\);/) {
446209ef8725SPatrick Pannuto			if ($1 < 20) {
4463000d1cc1SJoe Perches				WARN("MSLEEP",
446443c1d77cSJoe Perches				     "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $herecurr);
446509ef8725SPatrick Pannuto			}
446609ef8725SPatrick Pannuto		}
446709ef8725SPatrick Pannuto
446836ec1939SJoe Perches# check for comparisons of jiffies
446936ec1939SJoe Perches		if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
447036ec1939SJoe Perches			WARN("JIFFIES_COMPARISON",
447136ec1939SJoe Perches			     "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
447236ec1939SJoe Perches		}
447336ec1939SJoe Perches
44749d7a34a5SJoe Perches# check for comparisons of get_jiffies_64()
44759d7a34a5SJoe Perches		if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
44769d7a34a5SJoe Perches			WARN("JIFFIES_COMPARISON",
44779d7a34a5SJoe Perches			     "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
44789d7a34a5SJoe Perches		}
44799d7a34a5SJoe Perches
448000df344fSAndy Whitcroft# warn about #ifdefs in C files
4481c45dcabdSAndy Whitcroft#		if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
448200df344fSAndy Whitcroft#			print "#ifdef in C files should be avoided\n";
448300df344fSAndy Whitcroft#			print "$herecurr";
448400df344fSAndy Whitcroft#			$clean = 0;
448500df344fSAndy Whitcroft#		}
448600df344fSAndy Whitcroft
448722f2a2efSAndy Whitcroft# warn about spacing in #ifdefs
4488c45dcabdSAndy Whitcroft		if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
44893705ce5bSJoe Perches			if (ERROR("SPACING",
44903705ce5bSJoe Perches				  "exactly one space required after that #$1\n" . $herecurr) &&
44913705ce5bSJoe Perches			    $fix) {
4492194f66fcSJoe Perches				$fixed[$fixlinenr] =~
44933705ce5bSJoe Perches				    s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
44943705ce5bSJoe Perches			}
44953705ce5bSJoe Perches
449622f2a2efSAndy Whitcroft		}
449722f2a2efSAndy Whitcroft
44984a0df2efSAndy Whitcroft# check for spinlock_t definitions without a comment.
4499171ae1a4SAndy Whitcroft		if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
4500171ae1a4SAndy Whitcroft		    $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
45014a0df2efSAndy Whitcroft			my $which = $1;
45024a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
4503000d1cc1SJoe Perches				CHK("UNCOMMENTED_DEFINITION",
4504000d1cc1SJoe Perches				    "$1 definition without comment\n" . $herecurr);
45054a0df2efSAndy Whitcroft			}
45064a0df2efSAndy Whitcroft		}
45074a0df2efSAndy Whitcroft# check for memory barriers without a comment.
45084a0df2efSAndy Whitcroft		if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
45094a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
4510c1fd7bb9SJoe Perches				WARN("MEMORY_BARRIER",
4511000d1cc1SJoe Perches				     "memory barrier without comment\n" . $herecurr);
45124a0df2efSAndy Whitcroft			}
45134a0df2efSAndy Whitcroft		}
45144a0df2efSAndy Whitcroft# check of hardware specific defines
4515c45dcabdSAndy Whitcroft		if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
4516000d1cc1SJoe Perches			CHK("ARCH_DEFINES",
4517000d1cc1SJoe Perches			    "architecture specific defines should be avoided\n" .  $herecurr);
45180a920b5bSAndy Whitcroft		}
4519653d4876SAndy Whitcroft
4520d4977c78STobias Klauser# Check that the storage class is at the beginning of a declaration
4521d4977c78STobias Klauser		if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
4522000d1cc1SJoe Perches			WARN("STORAGE_CLASS",
4523000d1cc1SJoe Perches			     "storage class should be at the beginning of the declaration\n" . $herecurr)
4524d4977c78STobias Klauser		}
4525d4977c78STobias Klauser
4526de7d4f0eSAndy Whitcroft# check the location of the inline attribute, that it is between
4527de7d4f0eSAndy Whitcroft# storage class and type.
45289c0ca6f9SAndy Whitcroft		if ($line =~ /\b$Type\s+$Inline\b/ ||
45299c0ca6f9SAndy Whitcroft		    $line =~ /\b$Inline\s+$Storage\b/) {
4530000d1cc1SJoe Perches			ERROR("INLINE_LOCATION",
4531000d1cc1SJoe Perches			      "inline keyword should sit between storage class and type\n" . $herecurr);
4532de7d4f0eSAndy Whitcroft		}
4533de7d4f0eSAndy Whitcroft
45348905a67cSAndy Whitcroft# Check for __inline__ and __inline, prefer inline
45352b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
45362b7ab453SJoe Perches		    $line =~ /\b(__inline__|__inline)\b/) {
4537d5e616fcSJoe Perches			if (WARN("INLINE",
4538d5e616fcSJoe Perches				 "plain inline is preferred over $1\n" . $herecurr) &&
4539d5e616fcSJoe Perches			    $fix) {
4540194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/;
4541d5e616fcSJoe Perches
4542d5e616fcSJoe Perches			}
45438905a67cSAndy Whitcroft		}
45448905a67cSAndy Whitcroft
45453d130fd0SJoe Perches# Check for __attribute__ packed, prefer __packed
45462b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
45472b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
4548000d1cc1SJoe Perches			WARN("PREFER_PACKED",
4549000d1cc1SJoe Perches			     "__packed is preferred over __attribute__((packed))\n" . $herecurr);
45503d130fd0SJoe Perches		}
45513d130fd0SJoe Perches
455239b7e287SJoe Perches# Check for __attribute__ aligned, prefer __aligned
45532b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
45542b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
4555000d1cc1SJoe Perches			WARN("PREFER_ALIGNED",
4556000d1cc1SJoe Perches			     "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
455739b7e287SJoe Perches		}
455839b7e287SJoe Perches
45595f14d3bdSJoe Perches# Check for __attribute__ format(printf, prefer __printf
45602b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
45612b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
4562d5e616fcSJoe Perches			if (WARN("PREFER_PRINTF",
4563d5e616fcSJoe Perches				 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) &&
4564d5e616fcSJoe Perches			    $fix) {
4565194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf\s*,\s*(.*)\)\s*\)\s*\)/"__printf(" . trim($1) . ")"/ex;
4566d5e616fcSJoe Perches
4567d5e616fcSJoe Perches			}
45685f14d3bdSJoe Perches		}
45695f14d3bdSJoe Perches
45706061d949SJoe Perches# Check for __attribute__ format(scanf, prefer __scanf
45712b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
45722b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
4573d5e616fcSJoe Perches			if (WARN("PREFER_SCANF",
4574d5e616fcSJoe Perches				 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) &&
4575d5e616fcSJoe Perches			    $fix) {
4576194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\s*,\s*(.*)\)\s*\)\s*\)/"__scanf(" . trim($1) . ")"/ex;
4577d5e616fcSJoe Perches			}
45786061d949SJoe Perches		}
45796061d949SJoe Perches
45808f53a9b8SJoe Perches# check for sizeof(&)
45818f53a9b8SJoe Perches		if ($line =~ /\bsizeof\s*\(\s*\&/) {
4582000d1cc1SJoe Perches			WARN("SIZEOF_ADDRESS",
4583000d1cc1SJoe Perches			     "sizeof(& should be avoided\n" . $herecurr);
45848f53a9b8SJoe Perches		}
45858f53a9b8SJoe Perches
458666c80b60SJoe Perches# check for sizeof without parenthesis
458766c80b60SJoe Perches		if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
4588d5e616fcSJoe Perches			if (WARN("SIZEOF_PARENTHESIS",
4589d5e616fcSJoe Perches				 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
4590d5e616fcSJoe Perches			    $fix) {
4591194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
4592d5e616fcSJoe Perches			}
459366c80b60SJoe Perches		}
459466c80b60SJoe Perches
4595428e2fdcSJoe Perches# check for line continuations in quoted strings with odd counts of "
4596428e2fdcSJoe Perches		if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) {
4597000d1cc1SJoe Perches			WARN("LINE_CONTINUATIONS",
4598000d1cc1SJoe Perches			     "Avoid line continuations in quoted strings\n" . $herecurr);
4599428e2fdcSJoe Perches		}
4600428e2fdcSJoe Perches
460188982feaSJoe Perches# check for struct spinlock declarations
460288982feaSJoe Perches		if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
460388982feaSJoe Perches			WARN("USE_SPINLOCK_T",
460488982feaSJoe Perches			     "struct spinlock should be spinlock_t\n" . $herecurr);
460588982feaSJoe Perches		}
460688982feaSJoe Perches
4607a6962d72SJoe Perches# check for seq_printf uses that could be seq_puts
460806668727SJoe Perches		if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
4609a6962d72SJoe Perches			my $fmt = get_quoted_string($line, $rawline);
461006668727SJoe Perches			if ($fmt ne "" && $fmt !~ /[^\\]\%/) {
4611d5e616fcSJoe Perches				if (WARN("PREFER_SEQ_PUTS",
4612d5e616fcSJoe Perches					 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
4613d5e616fcSJoe Perches				    $fix) {
4614194f66fcSJoe Perches					$fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/;
4615d5e616fcSJoe Perches				}
4616a6962d72SJoe Perches			}
4617a6962d72SJoe Perches		}
4618a6962d72SJoe Perches
4619554e165cSAndy Whitcroft# Check for misused memsets
4620d1fe9c09SJoe Perches		if ($^V && $^V ge 5.10.0 &&
4621d1fe9c09SJoe Perches		    defined $stat &&
4622d7c76ba7SJoe Perches		    $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/s) {
4623554e165cSAndy Whitcroft
4624d7c76ba7SJoe Perches			my $ms_addr = $2;
4625d1fe9c09SJoe Perches			my $ms_val = $7;
4626d1fe9c09SJoe Perches			my $ms_size = $12;
4627d7c76ba7SJoe Perches
4628554e165cSAndy Whitcroft			if ($ms_size =~ /^(0x|)0$/i) {
4629554e165cSAndy Whitcroft				ERROR("MEMSET",
4630d7c76ba7SJoe Perches				      "memset to 0's uses 0 as the 2nd argument, not the 3rd\n" . "$here\n$stat\n");
4631554e165cSAndy Whitcroft			} elsif ($ms_size =~ /^(0x|)1$/i) {
4632554e165cSAndy Whitcroft				WARN("MEMSET",
4633d7c76ba7SJoe Perches				     "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
4634d7c76ba7SJoe Perches			}
4635d7c76ba7SJoe Perches		}
4636d7c76ba7SJoe Perches
463798a9bba5SJoe Perches# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar)
463898a9bba5SJoe Perches		if ($^V && $^V ge 5.10.0 &&
463998a9bba5SJoe Perches		    $line =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/s) {
464098a9bba5SJoe Perches			if (WARN("PREFER_ETHER_ADDR_COPY",
464198a9bba5SJoe Perches				 "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . $herecurr) &&
464298a9bba5SJoe Perches			    $fix) {
4643194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/;
464498a9bba5SJoe Perches			}
464598a9bba5SJoe Perches		}
464698a9bba5SJoe Perches
4647d7c76ba7SJoe Perches# typecasts on min/max could be min_t/max_t
4648d1fe9c09SJoe Perches		if ($^V && $^V ge 5.10.0 &&
4649d1fe9c09SJoe Perches		    defined $stat &&
4650d7c76ba7SJoe Perches		    $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
4651d1fe9c09SJoe Perches			if (defined $2 || defined $7) {
4652d7c76ba7SJoe Perches				my $call = $1;
4653d7c76ba7SJoe Perches				my $cast1 = deparenthesize($2);
4654d7c76ba7SJoe Perches				my $arg1 = $3;
4655d1fe9c09SJoe Perches				my $cast2 = deparenthesize($7);
4656d1fe9c09SJoe Perches				my $arg2 = $8;
4657d7c76ba7SJoe Perches				my $cast;
4658d7c76ba7SJoe Perches
4659d1fe9c09SJoe Perches				if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
4660d7c76ba7SJoe Perches					$cast = "$cast1 or $cast2";
4661d7c76ba7SJoe Perches				} elsif ($cast1 ne "") {
4662d7c76ba7SJoe Perches					$cast = $cast1;
4663d7c76ba7SJoe Perches				} else {
4664d7c76ba7SJoe Perches					$cast = $cast2;
4665d7c76ba7SJoe Perches				}
4666d7c76ba7SJoe Perches				WARN("MINMAX",
4667d7c76ba7SJoe Perches				     "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
4668554e165cSAndy Whitcroft			}
4669554e165cSAndy Whitcroft		}
4670554e165cSAndy Whitcroft
46714a273195SJoe Perches# check usleep_range arguments
46724a273195SJoe Perches		if ($^V && $^V ge 5.10.0 &&
46734a273195SJoe Perches		    defined $stat &&
46744a273195SJoe Perches		    $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
46754a273195SJoe Perches			my $min = $1;
46764a273195SJoe Perches			my $max = $7;
46774a273195SJoe Perches			if ($min eq $max) {
46784a273195SJoe Perches				WARN("USLEEP_RANGE",
46794a273195SJoe Perches				     "usleep_range should not use min == max args; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
46804a273195SJoe Perches			} elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
46814a273195SJoe Perches				 $min > $max) {
46824a273195SJoe Perches				WARN("USLEEP_RANGE",
46834a273195SJoe Perches				     "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
46844a273195SJoe Perches			}
46854a273195SJoe Perches		}
46864a273195SJoe Perches
4687823b794cSJoe Perches# check for naked sscanf
4688823b794cSJoe Perches		if ($^V && $^V ge 5.10.0 &&
4689823b794cSJoe Perches		    defined $stat &&
46906c8bd707SJoe Perches		    $line =~ /\bsscanf\b/ &&
4691823b794cSJoe Perches		    ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
4692823b794cSJoe Perches		     $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ &&
4693823b794cSJoe Perches		     $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) {
4694823b794cSJoe Perches			my $lc = $stat =~ tr@\n@@;
4695823b794cSJoe Perches			$lc = $lc + $linenr;
4696823b794cSJoe Perches			my $stat_real = raw_line($linenr, 0);
4697823b794cSJoe Perches		        for (my $count = $linenr + 1; $count <= $lc; $count++) {
4698823b794cSJoe Perches				$stat_real = $stat_real . "\n" . raw_line($count, 0);
4699823b794cSJoe Perches			}
4700823b794cSJoe Perches			WARN("NAKED_SSCANF",
4701823b794cSJoe Perches			     "unchecked sscanf return value\n" . "$here\n$stat_real\n");
4702823b794cSJoe Perches		}
4703823b794cSJoe Perches
4704afc819abSJoe Perches# check for simple sscanf that should be kstrto<foo>
4705afc819abSJoe Perches		if ($^V && $^V ge 5.10.0 &&
4706afc819abSJoe Perches		    defined $stat &&
4707afc819abSJoe Perches		    $line =~ /\bsscanf\b/) {
4708afc819abSJoe Perches			my $lc = $stat =~ tr@\n@@;
4709afc819abSJoe Perches			$lc = $lc + $linenr;
4710afc819abSJoe Perches			my $stat_real = raw_line($linenr, 0);
4711afc819abSJoe Perches		        for (my $count = $linenr + 1; $count <= $lc; $count++) {
4712afc819abSJoe Perches				$stat_real = $stat_real . "\n" . raw_line($count, 0);
4713afc819abSJoe Perches			}
4714afc819abSJoe Perches			if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) {
4715afc819abSJoe Perches				my $format = $6;
4716afc819abSJoe Perches				my $count = $format =~ tr@%@%@;
4717afc819abSJoe Perches				if ($count == 1 &&
4718afc819abSJoe Perches				    $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) {
4719afc819abSJoe Perches					WARN("SSCANF_TO_KSTRTO",
4720afc819abSJoe Perches					     "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n");
4721afc819abSJoe Perches				}
4722afc819abSJoe Perches			}
4723afc819abSJoe Perches		}
4724afc819abSJoe Perches
472570dc8a48SJoe Perches# check for new externs in .h files.
472670dc8a48SJoe Perches		if ($realfile =~ /\.h$/ &&
472770dc8a48SJoe Perches		    $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
4728d1d85780SJoe Perches			if (CHK("AVOID_EXTERNS",
472970dc8a48SJoe Perches				"extern prototypes should be avoided in .h files\n" . $herecurr) &&
473070dc8a48SJoe Perches			    $fix) {
4731194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
473270dc8a48SJoe Perches			}
473370dc8a48SJoe Perches		}
473470dc8a48SJoe Perches
4735de7d4f0eSAndy Whitcroft# check for new externs in .c files.
4736171ae1a4SAndy Whitcroft		if ($realfile =~ /\.c$/ && defined $stat &&
4737c45dcabdSAndy Whitcroft		    $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
4738171ae1a4SAndy Whitcroft		{
4739c45dcabdSAndy Whitcroft			my $function_name = $1;
4740c45dcabdSAndy Whitcroft			my $paren_space = $2;
4741171ae1a4SAndy Whitcroft
4742171ae1a4SAndy Whitcroft			my $s = $stat;
4743171ae1a4SAndy Whitcroft			if (defined $cond) {
4744171ae1a4SAndy Whitcroft				substr($s, 0, length($cond), '');
4745171ae1a4SAndy Whitcroft			}
4746c45dcabdSAndy Whitcroft			if ($s =~ /^\s*;/ &&
4747c45dcabdSAndy Whitcroft			    $function_name ne 'uninitialized_var')
4748c45dcabdSAndy Whitcroft			{
4749000d1cc1SJoe Perches				WARN("AVOID_EXTERNS",
4750000d1cc1SJoe Perches				     "externs should be avoided in .c files\n" .  $herecurr);
4751de7d4f0eSAndy Whitcroft			}
4752de7d4f0eSAndy Whitcroft
4753171ae1a4SAndy Whitcroft			if ($paren_space =~ /\n/) {
4754000d1cc1SJoe Perches				WARN("FUNCTION_ARGUMENTS",
4755000d1cc1SJoe Perches				     "arguments for function declarations should follow identifier\n" . $herecurr);
4756171ae1a4SAndy Whitcroft			}
47579c9ba34eSAndy Whitcroft
47589c9ba34eSAndy Whitcroft		} elsif ($realfile =~ /\.c$/ && defined $stat &&
47599c9ba34eSAndy Whitcroft		    $stat =~ /^.\s*extern\s+/)
47609c9ba34eSAndy Whitcroft		{
4761000d1cc1SJoe Perches			WARN("AVOID_EXTERNS",
4762000d1cc1SJoe Perches			     "externs should be avoided in .c files\n" .  $herecurr);
4763171ae1a4SAndy Whitcroft		}
4764171ae1a4SAndy Whitcroft
4765de7d4f0eSAndy Whitcroft# checks for new __setup's
4766de7d4f0eSAndy Whitcroft		if ($rawline =~ /\b__setup\("([^"]*)"/) {
4767de7d4f0eSAndy Whitcroft			my $name = $1;
4768de7d4f0eSAndy Whitcroft
4769de7d4f0eSAndy Whitcroft			if (!grep(/$name/, @setup_docs)) {
4770000d1cc1SJoe Perches				CHK("UNDOCUMENTED_SETUP",
4771000d1cc1SJoe Perches				    "__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
4772de7d4f0eSAndy Whitcroft			}
4773653d4876SAndy Whitcroft		}
47749c0ca6f9SAndy Whitcroft
47759c0ca6f9SAndy Whitcroft# check for pointless casting of kmalloc return
4776caf2a54fSJoe Perches		if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) {
4777000d1cc1SJoe Perches			WARN("UNNECESSARY_CASTS",
4778000d1cc1SJoe Perches			     "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
47799c0ca6f9SAndy Whitcroft		}
478013214adfSAndy Whitcroft
4781a640d25cSJoe Perches# alloc style
4782a640d25cSJoe Perches# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
4783a640d25cSJoe Perches		if ($^V && $^V ge 5.10.0 &&
4784a640d25cSJoe Perches		    $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*([kv][mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
4785a640d25cSJoe Perches			CHK("ALLOC_SIZEOF_STRUCT",
4786a640d25cSJoe Perches			    "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
4787a640d25cSJoe Perches		}
4788a640d25cSJoe Perches
478960a55369SJoe Perches# check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc
479060a55369SJoe Perches		if ($^V && $^V ge 5.10.0 &&
4791e367455aSJoe Perches		    $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*(k[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)\s*,/) {
479260a55369SJoe Perches			my $oldfunc = $3;
479360a55369SJoe Perches			my $a1 = $4;
479460a55369SJoe Perches			my $a2 = $10;
479560a55369SJoe Perches			my $newfunc = "kmalloc_array";
479660a55369SJoe Perches			$newfunc = "kcalloc" if ($oldfunc eq "kzalloc");
479760a55369SJoe Perches			my $r1 = $a1;
479860a55369SJoe Perches			my $r2 = $a2;
479960a55369SJoe Perches			if ($a1 =~ /^sizeof\s*\S/) {
480060a55369SJoe Perches				$r1 = $a2;
480160a55369SJoe Perches				$r2 = $a1;
480260a55369SJoe Perches			}
4803e367455aSJoe Perches			if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ &&
4804e367455aSJoe Perches			    !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) {
4805e367455aSJoe Perches				if (WARN("ALLOC_WITH_MULTIPLY",
4806e367455aSJoe Perches					 "Prefer $newfunc over $oldfunc with multiply\n" . $herecurr) &&
4807e367455aSJoe Perches				    $fix) {
4808194f66fcSJoe 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;
480960a55369SJoe Perches
481060a55369SJoe Perches				}
481160a55369SJoe Perches			}
481260a55369SJoe Perches		}
481360a55369SJoe Perches
4814972fdea2SJoe Perches# check for krealloc arg reuse
4815972fdea2SJoe Perches		if ($^V && $^V ge 5.10.0 &&
4816972fdea2SJoe Perches		    $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*\1\s*,/) {
4817972fdea2SJoe Perches			WARN("KREALLOC_ARG_REUSE",
4818972fdea2SJoe Perches			     "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
4819972fdea2SJoe Perches		}
4820972fdea2SJoe Perches
48215ce59ae0SJoe Perches# check for alloc argument mismatch
48225ce59ae0SJoe Perches		if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
48235ce59ae0SJoe Perches			WARN("ALLOC_ARRAY_ARGS",
48245ce59ae0SJoe Perches			     "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
48255ce59ae0SJoe Perches		}
48265ce59ae0SJoe Perches
4827caf2a54fSJoe Perches# check for multiple semicolons
4828caf2a54fSJoe Perches		if ($line =~ /;\s*;\s*$/) {
4829d5e616fcSJoe Perches			if (WARN("ONE_SEMICOLON",
4830d5e616fcSJoe Perches				 "Statements terminations use 1 semicolon\n" . $herecurr) &&
4831d5e616fcSJoe Perches			    $fix) {
4832194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g;
4833d5e616fcSJoe Perches			}
4834d1e2ad07SJoe Perches		}
4835d1e2ad07SJoe Perches
4836e81f239bSJoe Perches# check for case / default statements not preceded by break/fallthrough/switch
4837c34c09a8SJoe Perches		if ($line =~ /^.\s*(?:case\s+(?:$Ident|$Constant)\s*|default):/) {
4838c34c09a8SJoe Perches			my $has_break = 0;
4839c34c09a8SJoe Perches			my $has_statement = 0;
4840c34c09a8SJoe Perches			my $count = 0;
4841c34c09a8SJoe Perches			my $prevline = $linenr;
4842e81f239bSJoe Perches			while ($prevline > 1 && ($file || $count < 3) && !$has_break) {
4843c34c09a8SJoe Perches				$prevline--;
4844c34c09a8SJoe Perches				my $rline = $rawlines[$prevline - 1];
4845c34c09a8SJoe Perches				my $fline = $lines[$prevline - 1];
4846c34c09a8SJoe Perches				last if ($fline =~ /^\@\@/);
4847c34c09a8SJoe Perches				next if ($fline =~ /^\-/);
4848c34c09a8SJoe Perches				next if ($fline =~ /^.(?:\s*(?:case\s+(?:$Ident|$Constant)[\s$;]*|default):[\s$;]*)*$/);
4849c34c09a8SJoe Perches				$has_break = 1 if ($rline =~ /fall[\s_-]*(through|thru)/i);
4850c34c09a8SJoe Perches				next if ($fline =~ /^.[\s$;]*$/);
4851c34c09a8SJoe Perches				$has_statement = 1;
4852c34c09a8SJoe Perches				$count++;
4853c34c09a8SJoe Perches				$has_break = 1 if ($fline =~ /\bswitch\b|\b(?:break\s*;[\s$;]*$|return\b|goto\b|continue\b)/);
4854c34c09a8SJoe Perches			}
4855c34c09a8SJoe Perches			if (!$has_break && $has_statement) {
4856c34c09a8SJoe Perches				WARN("MISSING_BREAK",
4857c34c09a8SJoe Perches				     "Possible switch case/default not preceeded by break or fallthrough comment\n" . $herecurr);
4858c34c09a8SJoe Perches			}
4859c34c09a8SJoe Perches		}
4860c34c09a8SJoe Perches
4861d1e2ad07SJoe Perches# check for switch/default statements without a break;
4862d1e2ad07SJoe Perches		if ($^V && $^V ge 5.10.0 &&
4863d1e2ad07SJoe Perches		    defined $stat &&
4864d1e2ad07SJoe Perches		    $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
4865d1e2ad07SJoe Perches			my $ctx = '';
4866d1e2ad07SJoe Perches			my $herectx = $here . "\n";
4867d1e2ad07SJoe Perches			my $cnt = statement_rawlines($stat);
4868d1e2ad07SJoe Perches			for (my $n = 0; $n < $cnt; $n++) {
4869d1e2ad07SJoe Perches				$herectx .= raw_line($linenr, $n) . "\n";
4870d1e2ad07SJoe Perches			}
4871d1e2ad07SJoe Perches			WARN("DEFAULT_NO_BREAK",
4872d1e2ad07SJoe Perches			     "switch default: should use break\n" . $herectx);
4873caf2a54fSJoe Perches		}
4874caf2a54fSJoe Perches
487513214adfSAndy Whitcroft# check for gcc specific __FUNCTION__
4876d5e616fcSJoe Perches		if ($line =~ /\b__FUNCTION__\b/) {
4877d5e616fcSJoe Perches			if (WARN("USE_FUNC",
4878d5e616fcSJoe Perches				 "__func__ should be used instead of gcc specific __FUNCTION__\n"  . $herecurr) &&
4879d5e616fcSJoe Perches			    $fix) {
4880194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g;
4881d5e616fcSJoe Perches			}
488213214adfSAndy Whitcroft		}
4883773647a0SAndy Whitcroft
48842c92488aSJoe Perches# check for use of yield()
48852c92488aSJoe Perches		if ($line =~ /\byield\s*\(\s*\)/) {
48862c92488aSJoe Perches			WARN("YIELD",
48872c92488aSJoe Perches			     "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n"  . $herecurr);
48882c92488aSJoe Perches		}
48892c92488aSJoe Perches
4890179f8f40SJoe Perches# check for comparisons against true and false
4891179f8f40SJoe Perches		if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
4892179f8f40SJoe Perches			my $lead = $1;
4893179f8f40SJoe Perches			my $arg = $2;
4894179f8f40SJoe Perches			my $test = $3;
4895179f8f40SJoe Perches			my $otype = $4;
4896179f8f40SJoe Perches			my $trail = $5;
4897179f8f40SJoe Perches			my $op = "!";
4898179f8f40SJoe Perches
4899179f8f40SJoe Perches			($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
4900179f8f40SJoe Perches
4901179f8f40SJoe Perches			my $type = lc($otype);
4902179f8f40SJoe Perches			if ($type =~ /^(?:true|false)$/) {
4903179f8f40SJoe Perches				if (("$test" eq "==" && "$type" eq "true") ||
4904179f8f40SJoe Perches				    ("$test" eq "!=" && "$type" eq "false")) {
4905179f8f40SJoe Perches					$op = "";
4906179f8f40SJoe Perches				}
4907179f8f40SJoe Perches
4908179f8f40SJoe Perches				CHK("BOOL_COMPARISON",
4909179f8f40SJoe Perches				    "Using comparison to $otype is error prone\n" . $herecurr);
4910179f8f40SJoe Perches
4911179f8f40SJoe Perches## maybe suggesting a correct construct would better
4912179f8f40SJoe Perches##				    "Using comparison to $otype is error prone.  Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
4913179f8f40SJoe Perches
4914179f8f40SJoe Perches			}
4915179f8f40SJoe Perches		}
4916179f8f40SJoe Perches
49174882720bSThomas Gleixner# check for semaphores initialized locked
49184882720bSThomas Gleixner		if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
4919000d1cc1SJoe Perches			WARN("CONSIDER_COMPLETION",
4920000d1cc1SJoe Perches			     "consider using a completion\n" . $herecurr);
4921773647a0SAndy Whitcroft		}
49226712d858SJoe Perches
492367d0a075SJoe Perches# recommend kstrto* over simple_strto* and strict_strto*
492467d0a075SJoe Perches		if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
4925000d1cc1SJoe Perches			WARN("CONSIDER_KSTRTO",
492667d0a075SJoe Perches			     "$1 is obsolete, use k$3 instead\n" . $herecurr);
4927773647a0SAndy Whitcroft		}
49286712d858SJoe Perches
4929ae3ccc46SFabian Frederick# check for __initcall(), use device_initcall() explicitly or more appropriate function please
4930f3db6639SMichael Ellerman		if ($line =~ /^.\s*__initcall\s*\(/) {
4931000d1cc1SJoe Perches			WARN("USE_DEVICE_INITCALL",
4932ae3ccc46SFabian Frederick			     "please use device_initcall() or more appropriate function instead of __initcall() (see include/linux/init.h)\n" . $herecurr);
4933f3db6639SMichael Ellerman		}
49346712d858SJoe Perches
493579404849SEmese Revfy# check for various ops structs, ensure they are const.
493679404849SEmese Revfy		my $struct_ops = qr{acpi_dock_ops|
493779404849SEmese Revfy				address_space_operations|
493879404849SEmese Revfy				backlight_ops|
493979404849SEmese Revfy				block_device_operations|
494079404849SEmese Revfy				dentry_operations|
494179404849SEmese Revfy				dev_pm_ops|
494279404849SEmese Revfy				dma_map_ops|
494379404849SEmese Revfy				extent_io_ops|
494479404849SEmese Revfy				file_lock_operations|
494579404849SEmese Revfy				file_operations|
494679404849SEmese Revfy				hv_ops|
494779404849SEmese Revfy				ide_dma_ops|
494879404849SEmese Revfy				intel_dvo_dev_ops|
494979404849SEmese Revfy				item_operations|
495079404849SEmese Revfy				iwl_ops|
495179404849SEmese Revfy				kgdb_arch|
495279404849SEmese Revfy				kgdb_io|
495379404849SEmese Revfy				kset_uevent_ops|
495479404849SEmese Revfy				lock_manager_operations|
495579404849SEmese Revfy				microcode_ops|
495679404849SEmese Revfy				mtrr_ops|
495779404849SEmese Revfy				neigh_ops|
495879404849SEmese Revfy				nlmsvc_binding|
495979404849SEmese Revfy				pci_raw_ops|
496079404849SEmese Revfy				pipe_buf_operations|
496179404849SEmese Revfy				platform_hibernation_ops|
496279404849SEmese Revfy				platform_suspend_ops|
496379404849SEmese Revfy				proto_ops|
496479404849SEmese Revfy				rpc_pipe_ops|
496579404849SEmese Revfy				seq_operations|
496679404849SEmese Revfy				snd_ac97_build_ops|
496779404849SEmese Revfy				soc_pcmcia_socket_ops|
496879404849SEmese Revfy				stacktrace_ops|
496979404849SEmese Revfy				sysfs_ops|
497079404849SEmese Revfy				tty_operations|
497179404849SEmese Revfy				usb_mon_operations|
497279404849SEmese Revfy				wd_ops}x;
49736903ffb2SAndy Whitcroft		if ($line !~ /\bconst\b/ &&
497479404849SEmese Revfy		    $line =~ /\bstruct\s+($struct_ops)\b/) {
4975000d1cc1SJoe Perches			WARN("CONST_STRUCT",
4976000d1cc1SJoe Perches			     "struct $1 should normally be const\n" .
49776903ffb2SAndy Whitcroft				$herecurr);
49782b6db5cbSAndy Whitcroft		}
4979773647a0SAndy Whitcroft
4980773647a0SAndy Whitcroft# use of NR_CPUS is usually wrong
4981773647a0SAndy Whitcroft# ignore definitions of NR_CPUS and usage to define arrays as likely right
4982773647a0SAndy Whitcroft		if ($line =~ /\bNR_CPUS\b/ &&
4983c45dcabdSAndy Whitcroft		    $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
4984c45dcabdSAndy Whitcroft		    $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
4985171ae1a4SAndy Whitcroft		    $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
4986171ae1a4SAndy Whitcroft		    $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
4987171ae1a4SAndy Whitcroft		    $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
4988773647a0SAndy Whitcroft		{
4989000d1cc1SJoe Perches			WARN("NR_CPUS",
4990000d1cc1SJoe Perches			     "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
4991773647a0SAndy Whitcroft		}
49929c9ba34eSAndy Whitcroft
499352ea8506SJoe Perches# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
499452ea8506SJoe Perches		if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
499552ea8506SJoe Perches			ERROR("DEFINE_ARCH_HAS",
499652ea8506SJoe Perches			      "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
499752ea8506SJoe Perches		}
499852ea8506SJoe Perches
49999c9ba34eSAndy Whitcroft# check for %L{u,d,i} in strings
50009c9ba34eSAndy Whitcroft		my $string;
50019c9ba34eSAndy Whitcroft		while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
50029c9ba34eSAndy Whitcroft			$string = substr($rawline, $-[1], $+[1] - $-[1]);
50032a1bc5d5SAndy Whitcroft			$string =~ s/%%/__/g;
50049c9ba34eSAndy Whitcroft			if ($string =~ /(?<!%)%L[udi]/) {
5005000d1cc1SJoe Perches				WARN("PRINTF_L",
5006000d1cc1SJoe Perches				     "\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
50079c9ba34eSAndy Whitcroft				last;
50089c9ba34eSAndy Whitcroft			}
50099c9ba34eSAndy Whitcroft		}
5010691d77b6SAndy Whitcroft
5011691d77b6SAndy Whitcroft# whine mightly about in_atomic
5012691d77b6SAndy Whitcroft		if ($line =~ /\bin_atomic\s*\(/) {
5013691d77b6SAndy Whitcroft			if ($realfile =~ m@^drivers/@) {
5014000d1cc1SJoe Perches				ERROR("IN_ATOMIC",
5015000d1cc1SJoe Perches				      "do not use in_atomic in drivers\n" . $herecurr);
5016f4a87736SAndy Whitcroft			} elsif ($realfile !~ m@^kernel/@) {
5017000d1cc1SJoe Perches				WARN("IN_ATOMIC",
5018000d1cc1SJoe Perches				     "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
5019691d77b6SAndy Whitcroft			}
5020691d77b6SAndy Whitcroft		}
50211704f47bSPeter Zijlstra
50221704f47bSPeter Zijlstra# check for lockdep_set_novalidate_class
50231704f47bSPeter Zijlstra		if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
50241704f47bSPeter Zijlstra		    $line =~ /__lockdep_no_validate__\s*\)/ ) {
50251704f47bSPeter Zijlstra			if ($realfile !~ m@^kernel/lockdep@ &&
50261704f47bSPeter Zijlstra			    $realfile !~ m@^include/linux/lockdep@ &&
50271704f47bSPeter Zijlstra			    $realfile !~ m@^drivers/base/core@) {
5028000d1cc1SJoe Perches				ERROR("LOCKDEP",
5029000d1cc1SJoe Perches				      "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
50301704f47bSPeter Zijlstra			}
50311704f47bSPeter Zijlstra		}
503288f8831cSDave Jones
503388f8831cSDave Jones		if ($line =~ /debugfs_create_file.*S_IWUGO/ ||
503488f8831cSDave Jones		    $line =~ /DEVICE_ATTR.*S_IWUGO/ ) {
5035000d1cc1SJoe Perches			WARN("EXPORTED_WORLD_WRITABLE",
5036000d1cc1SJoe Perches			     "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
503788f8831cSDave Jones		}
50382435880fSJoe Perches
5039515a235eSJoe Perches# Mode permission misuses where it seems decimal should be octal
5040515a235eSJoe Perches# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop
5041515a235eSJoe Perches		if ($^V && $^V ge 5.10.0 &&
5042515a235eSJoe Perches		    $line =~ /$mode_perms_search/) {
50432435880fSJoe Perches			foreach my $entry (@mode_permission_funcs) {
50442435880fSJoe Perches				my $func = $entry->[0];
50452435880fSJoe Perches				my $arg_pos = $entry->[1];
50462435880fSJoe Perches
50472435880fSJoe Perches				my $skip_args = "";
50482435880fSJoe Perches				if ($arg_pos > 1) {
50492435880fSJoe Perches					$arg_pos--;
50502435880fSJoe Perches					$skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}";
50512435880fSJoe Perches				}
50522435880fSJoe Perches				my $test = "\\b$func\\s*\\(${skip_args}([\\d]+)\\s*[,\\)]";
5053515a235eSJoe Perches				if ($line =~ /$test/) {
50542435880fSJoe Perches					my $val = $1;
50552435880fSJoe Perches					$val = $6 if ($skip_args ne "");
50562435880fSJoe Perches
50571727cc70SJoe Perches					if ($val !~ /^0$/ &&
50581727cc70SJoe Perches					    (($val =~ /^$Int$/ && $val !~ /^$Octal$/) ||
50591727cc70SJoe Perches					     length($val) ne 4)) {
50602435880fSJoe Perches						ERROR("NON_OCTAL_PERMISSIONS",
50611727cc70SJoe Perches						      "Use 4 digit octal (0777) not decimal permissions\n" . $herecurr);
50622435880fSJoe Perches					}
50632435880fSJoe Perches				}
50642435880fSJoe Perches			}
506513214adfSAndy Whitcroft		}
5066515a235eSJoe Perches	}
506713214adfSAndy Whitcroft
506813214adfSAndy Whitcroft	# If we have no input at all, then there is nothing to report on
506913214adfSAndy Whitcroft	# so just keep quiet.
507013214adfSAndy Whitcroft	if ($#rawlines == -1) {
507113214adfSAndy Whitcroft		exit(0);
50720a920b5bSAndy Whitcroft	}
50730a920b5bSAndy Whitcroft
50748905a67cSAndy Whitcroft	# In mailback mode only produce a report in the negative, for
50758905a67cSAndy Whitcroft	# things that appear to be patches.
50768905a67cSAndy Whitcroft	if ($mailback && ($clean == 1 || !$is_patch)) {
50778905a67cSAndy Whitcroft		exit(0);
50788905a67cSAndy Whitcroft	}
50798905a67cSAndy Whitcroft
50808905a67cSAndy Whitcroft	# This is not a patch, and we are are in 'no-patch' mode so
50818905a67cSAndy Whitcroft	# just keep quiet.
50828905a67cSAndy Whitcroft	if (!$chk_patch && !$is_patch) {
50838905a67cSAndy Whitcroft		exit(0);
50848905a67cSAndy Whitcroft	}
50858905a67cSAndy Whitcroft
50868905a67cSAndy Whitcroft	if (!$is_patch) {
5087000d1cc1SJoe Perches		ERROR("NOT_UNIFIED_DIFF",
5088000d1cc1SJoe Perches		      "Does not appear to be a unified-diff format patch\n");
50890a920b5bSAndy Whitcroft	}
50900a920b5bSAndy Whitcroft	if ($is_patch && $chk_signoff && $signoff == 0) {
5091000d1cc1SJoe Perches		ERROR("MISSING_SIGN_OFF",
5092000d1cc1SJoe Perches		      "Missing Signed-off-by: line(s)\n");
50930a920b5bSAndy Whitcroft	}
50940a920b5bSAndy Whitcroft
5095f0a594c1SAndy Whitcroft	print report_dump();
509613214adfSAndy Whitcroft	if ($summary && !($clean == 1 && $quiet == 1)) {
509713214adfSAndy Whitcroft		print "$filename " if ($summary_file);
50986c72ffaaSAndy Whitcroft		print "total: $cnt_error errors, $cnt_warn warnings, " .
50996c72ffaaSAndy Whitcroft			(($check)? "$cnt_chk checks, " : "") .
51006c72ffaaSAndy Whitcroft			"$cnt_lines lines checked\n";
51018905a67cSAndy Whitcroft		print "\n" if ($quiet == 0);
51026c72ffaaSAndy Whitcroft	}
51038905a67cSAndy Whitcroft
5104d2c0a235SAndy Whitcroft	if ($quiet == 0) {
5105d1fe9c09SJoe Perches
5106d1fe9c09SJoe Perches		if ($^V lt 5.10.0) {
5107d1fe9c09SJoe Perches			print("NOTE: perl $^V is not modern enough to detect all possible issues.\n");
5108d1fe9c09SJoe Perches			print("An upgrade to at least perl v5.10.0 is suggested.\n\n");
5109d1fe9c09SJoe Perches		}
5110d1fe9c09SJoe Perches
5111d2c0a235SAndy Whitcroft		# If there were whitespace errors which cleanpatch can fix
5112d2c0a235SAndy Whitcroft		# then suggest that.
5113d2c0a235SAndy Whitcroft		if ($rpt_cleaners) {
5114d2c0a235SAndy Whitcroft			print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n";
5115d2c0a235SAndy Whitcroft			print "      scripts/cleanfile\n\n";
5116b0781216SMike Frysinger			$rpt_cleaners = 0;
5117d2c0a235SAndy Whitcroft		}
5118d2c0a235SAndy Whitcroft	}
5119d2c0a235SAndy Whitcroft
512091bfe484SJoe Perches	hash_show_words(\%use_type, "Used");
512191bfe484SJoe Perches	hash_show_words(\%ignore_type, "Ignored");
5122000d1cc1SJoe Perches
5123d752fcc8SJoe Perches	if ($clean == 0 && $fix &&
5124d752fcc8SJoe Perches	    ("@rawlines" ne "@fixed" ||
5125d752fcc8SJoe Perches	     $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) {
51269624b8d6SJoe Perches		my $newfile = $filename;
51279624b8d6SJoe Perches		$newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace);
51283705ce5bSJoe Perches		my $linecount = 0;
51293705ce5bSJoe Perches		my $f;
51303705ce5bSJoe Perches
5131d752fcc8SJoe Perches		@fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted);
5132d752fcc8SJoe Perches
51333705ce5bSJoe Perches		open($f, '>', $newfile)
51343705ce5bSJoe Perches		    or die "$P: Can't open $newfile for write\n";
51353705ce5bSJoe Perches		foreach my $fixed_line (@fixed) {
51363705ce5bSJoe Perches			$linecount++;
51373705ce5bSJoe Perches			if ($file) {
51383705ce5bSJoe Perches				if ($linecount > 3) {
51393705ce5bSJoe Perches					$fixed_line =~ s/^\+//;
51403705ce5bSJoe Perches					print $f $fixed_line . "\n";
51413705ce5bSJoe Perches				}
51423705ce5bSJoe Perches			} else {
51433705ce5bSJoe Perches				print $f $fixed_line . "\n";
51443705ce5bSJoe Perches			}
51453705ce5bSJoe Perches		}
51463705ce5bSJoe Perches		close($f);
51473705ce5bSJoe Perches
51483705ce5bSJoe Perches		if (!$quiet) {
51493705ce5bSJoe Perches			print << "EOM";
51503705ce5bSJoe PerchesWrote EXPERIMENTAL --fix correction(s) to '$newfile'
51513705ce5bSJoe Perches
51523705ce5bSJoe PerchesDo _NOT_ trust the results written to this file.
51533705ce5bSJoe PerchesDo _NOT_ submit these changes without inspecting them for correctness.
51543705ce5bSJoe Perches
51553705ce5bSJoe PerchesThis EXPERIMENTAL file is simply a convenience to help rewrite patches.
51563705ce5bSJoe PerchesNo warranties, expressed or implied...
51573705ce5bSJoe Perches
51583705ce5bSJoe PerchesEOM
51593705ce5bSJoe Perches		}
51603705ce5bSJoe Perches	}
51613705ce5bSJoe Perches
51620a920b5bSAndy Whitcroft	if ($clean == 1 && $quiet == 0) {
5163c2fdda0dSAndy Whitcroft		print "$vname has no obvious style problems and is ready for submission.\n"
51640a920b5bSAndy Whitcroft	}
51650a920b5bSAndy Whitcroft	if ($clean == 0 && $quiet == 0) {
5166000d1cc1SJoe Perches		print << "EOM";
5167000d1cc1SJoe Perches$vname has style problems, please review.
5168000d1cc1SJoe Perches
5169000d1cc1SJoe PerchesIf any of these errors are false positives, please report
5170000d1cc1SJoe Perchesthem to the maintainer, see CHECKPATCH in MAINTAINERS.
5171000d1cc1SJoe PerchesEOM
51720a920b5bSAndy Whitcroft	}
517313214adfSAndy Whitcroft
51740a920b5bSAndy Whitcroft	return $clean;
51750a920b5bSAndy Whitcroft}
5176