xref: /linux-6.15/scripts/checkpatch.pl (revision 369c8dd3)
10a920b5bSAndy Whitcroft#!/usr/bin/perl -w
2dbf004d7SDave Jones# (c) 2001, Dave Jones. (the file handling bit)
300df344fSAndy Whitcroft# (c) 2005, Joel Schopp <[email protected]> (the ugly bit)
42a5a2c25SAndy Whitcroft# (c) 2007,2008, Andy Whitcroft <[email protected]> (new conditions, test suite)
5015830beSAndy Whitcroft# (c) 2008-2010 Andy Whitcroft <[email protected]>
60a920b5bSAndy Whitcroft# Licensed under the terms of the GNU GPL License version 2
70a920b5bSAndy Whitcroft
80a920b5bSAndy Whitcroftuse strict;
9c707a81dSJoe Perchesuse POSIX;
1036061e38SJoe Perchesuse File::Basename;
1136061e38SJoe Perchesuse Cwd 'abs_path';
1257230297SJoe Perchesuse Term::ANSIColor qw(:constants);
130a920b5bSAndy Whitcroft
140a920b5bSAndy Whitcroftmy $P = $0;
1536061e38SJoe Perchesmy $D = dirname(abs_path($P));
160a920b5bSAndy Whitcroft
17000d1cc1SJoe Perchesmy $V = '0.32';
180a920b5bSAndy Whitcroft
190a920b5bSAndy Whitcroftuse Getopt::Long qw(:config no_auto_abbrev);
200a920b5bSAndy Whitcroft
210a920b5bSAndy Whitcroftmy $quiet = 0;
220a920b5bSAndy Whitcroftmy $tree = 1;
230a920b5bSAndy Whitcroftmy $chk_signoff = 1;
240a920b5bSAndy Whitcroftmy $chk_patch = 1;
25773647a0SAndy Whitcroftmy $tst_only;
266c72ffaaSAndy Whitcroftmy $emacs = 0;
278905a67cSAndy Whitcroftmy $terse = 0;
2834d8815fSJoe Perchesmy $showfile = 0;
296c72ffaaSAndy Whitcroftmy $file = 0;
306c72ffaaSAndy Whitcroftmy $check = 0;
312ac73b4fSJoe Perchesmy $check_orig = 0;
328905a67cSAndy Whitcroftmy $summary = 1;
338905a67cSAndy Whitcroftmy $mailback = 0;
3413214adfSAndy Whitcroftmy $summary_file = 0;
35000d1cc1SJoe Perchesmy $show_types = 0;
363705ce5bSJoe Perchesmy $fix = 0;
379624b8d6SJoe Perchesmy $fix_inplace = 0;
386c72ffaaSAndy Whitcroftmy $root;
39c2fdda0dSAndy Whitcroftmy %debug;
403445686aSJoe Perchesmy %camelcase = ();
4191bfe484SJoe Perchesmy %use_type = ();
4291bfe484SJoe Perchesmy @use = ();
4391bfe484SJoe Perchesmy %ignore_type = ();
44000d1cc1SJoe Perchesmy @ignore = ();
4577f5b10aSHannes Edermy $help = 0;
46000d1cc1SJoe Perchesmy $configuration_file = ".checkpatch.conf";
476cd7f386SJoe Perchesmy $max_line_length = 80;
48d62a201fSDave Hansenmy $ignore_perl_version = 0;
49d62a201fSDave Hansenmy $minimum_perl_version = 5.10.0;
5056193274SVadim Bendeburymy $min_conf_desc_length = 4;
5166b47b4aSKees Cookmy $spelling_file = "$D/spelling.txt";
52ebfd7d62SJoe Perchesmy $codespell = 0;
53f1a63678SMaxim Uvarovmy $codespellfile = "/usr/share/codespell/dictionary.txt";
5457230297SJoe Perchesmy $color = 1;
5577f5b10aSHannes Eder
5677f5b10aSHannes Edersub help {
5777f5b10aSHannes Eder	my ($exitcode) = @_;
5877f5b10aSHannes Eder
5977f5b10aSHannes Eder	print << "EOM";
6077f5b10aSHannes EderUsage: $P [OPTION]... [FILE]...
6177f5b10aSHannes EderVersion: $V
6277f5b10aSHannes Eder
6377f5b10aSHannes EderOptions:
6477f5b10aSHannes Eder  -q, --quiet                quiet
6577f5b10aSHannes Eder  --no-tree                  run without a kernel tree
6677f5b10aSHannes Eder  --no-signoff               do not check for 'Signed-off-by' line
6777f5b10aSHannes Eder  --patch                    treat FILE as patchfile (default)
6877f5b10aSHannes Eder  --emacs                    emacs compile window format
6977f5b10aSHannes Eder  --terse                    one line per report
7034d8815fSJoe Perches  --showfile                 emit diffed file position, not input file position
7177f5b10aSHannes Eder  -f, --file                 treat FILE as regular source file
7277f5b10aSHannes Eder  --subjective, --strict     enable more subjective tests
7391bfe484SJoe Perches  --types TYPE(,TYPE2...)    show only these comma separated message types
74000d1cc1SJoe Perches  --ignore TYPE(,TYPE2...)   ignore various comma separated message types
756cd7f386SJoe Perches  --max-line-length=n        set the maximum line length, if exceeded, warn
7656193274SVadim Bendebury  --min-conf-desc-length=n   set the min description length, if shorter, warn
77000d1cc1SJoe Perches  --show-types               show the message "types" in the output
7877f5b10aSHannes Eder  --root=PATH                PATH to the kernel tree root
7977f5b10aSHannes Eder  --no-summary               suppress the per-file summary
8077f5b10aSHannes Eder  --mailback                 only produce a report in case of warnings/errors
8177f5b10aSHannes Eder  --summary-file             include the filename in summary
8277f5b10aSHannes Eder  --debug KEY=[0|1]          turn on/off debugging of KEY, where KEY is one of
8377f5b10aSHannes Eder                             'values', 'possible', 'type', and 'attr' (default
8477f5b10aSHannes Eder                             is all off)
8577f5b10aSHannes Eder  --test-only=WORD           report only warnings/errors containing WORD
8677f5b10aSHannes Eder                             literally
873705ce5bSJoe Perches  --fix                      EXPERIMENTAL - may create horrible results
883705ce5bSJoe Perches                             If correctable single-line errors exist, create
893705ce5bSJoe Perches                             "<inputfile>.EXPERIMENTAL-checkpatch-fixes"
903705ce5bSJoe Perches                             with potential errors corrected to the preferred
913705ce5bSJoe Perches                             checkpatch style
929624b8d6SJoe Perches  --fix-inplace              EXPERIMENTAL - may create horrible results
939624b8d6SJoe Perches                             Is the same as --fix, but overwrites the input
949624b8d6SJoe Perches                             file.  It's your fault if there's no backup or git
95d62a201fSDave Hansen  --ignore-perl-version      override checking of perl version.  expect
96d62a201fSDave Hansen                             runtime errors.
97ebfd7d62SJoe Perches  --codespell                Use the codespell dictionary for spelling/typos
98f1a63678SMaxim Uvarov                             (default:/usr/share/codespell/dictionary.txt)
99ebfd7d62SJoe Perches  --codespellfile            Use this codespell dictionary
10057230297SJoe Perches  --color                    Use colors when output is STDOUT (default: on)
10177f5b10aSHannes Eder  -h, --help, --version      display this help and exit
10277f5b10aSHannes Eder
10377f5b10aSHannes EderWhen FILE is - read standard input.
10477f5b10aSHannes EderEOM
10577f5b10aSHannes Eder
10677f5b10aSHannes Eder	exit($exitcode);
10777f5b10aSHannes Eder}
10877f5b10aSHannes Eder
109000d1cc1SJoe Perchesmy $conf = which_conf($configuration_file);
110000d1cc1SJoe Perchesif (-f $conf) {
111000d1cc1SJoe Perches	my @conf_args;
112000d1cc1SJoe Perches	open(my $conffile, '<', "$conf")
113000d1cc1SJoe Perches	    or warn "$P: Can't find a readable $configuration_file file $!\n";
114000d1cc1SJoe Perches
115000d1cc1SJoe Perches	while (<$conffile>) {
116000d1cc1SJoe Perches		my $line = $_;
117000d1cc1SJoe Perches
118000d1cc1SJoe Perches		$line =~ s/\s*\n?$//g;
119000d1cc1SJoe Perches		$line =~ s/^\s*//g;
120000d1cc1SJoe Perches		$line =~ s/\s+/ /g;
121000d1cc1SJoe Perches
122000d1cc1SJoe Perches		next if ($line =~ m/^\s*#/);
123000d1cc1SJoe Perches		next if ($line =~ m/^\s*$/);
124000d1cc1SJoe Perches
125000d1cc1SJoe Perches		my @words = split(" ", $line);
126000d1cc1SJoe Perches		foreach my $word (@words) {
127000d1cc1SJoe Perches			last if ($word =~ m/^#/);
128000d1cc1SJoe Perches			push (@conf_args, $word);
129000d1cc1SJoe Perches		}
130000d1cc1SJoe Perches	}
131000d1cc1SJoe Perches	close($conffile);
132000d1cc1SJoe Perches	unshift(@ARGV, @conf_args) if @conf_args;
133000d1cc1SJoe Perches}
134000d1cc1SJoe Perches
1350a920b5bSAndy WhitcroftGetOptions(
1366c72ffaaSAndy Whitcroft	'q|quiet+'	=> \$quiet,
1370a920b5bSAndy Whitcroft	'tree!'		=> \$tree,
1380a920b5bSAndy Whitcroft	'signoff!'	=> \$chk_signoff,
1390a920b5bSAndy Whitcroft	'patch!'	=> \$chk_patch,
1406c72ffaaSAndy Whitcroft	'emacs!'	=> \$emacs,
1418905a67cSAndy Whitcroft	'terse!'	=> \$terse,
14234d8815fSJoe Perches	'showfile!'	=> \$showfile,
14377f5b10aSHannes Eder	'f|file!'	=> \$file,
1446c72ffaaSAndy Whitcroft	'subjective!'	=> \$check,
1456c72ffaaSAndy Whitcroft	'strict!'	=> \$check,
146000d1cc1SJoe Perches	'ignore=s'	=> \@ignore,
14791bfe484SJoe Perches	'types=s'	=> \@use,
148000d1cc1SJoe Perches	'show-types!'	=> \$show_types,
1496cd7f386SJoe Perches	'max-line-length=i' => \$max_line_length,
15056193274SVadim Bendebury	'min-conf-desc-length=i' => \$min_conf_desc_length,
1516c72ffaaSAndy Whitcroft	'root=s'	=> \$root,
1528905a67cSAndy Whitcroft	'summary!'	=> \$summary,
1538905a67cSAndy Whitcroft	'mailback!'	=> \$mailback,
15413214adfSAndy Whitcroft	'summary-file!'	=> \$summary_file,
1553705ce5bSJoe Perches	'fix!'		=> \$fix,
1569624b8d6SJoe Perches	'fix-inplace!'	=> \$fix_inplace,
157d62a201fSDave Hansen	'ignore-perl-version!' => \$ignore_perl_version,
158c2fdda0dSAndy Whitcroft	'debug=s'	=> \%debug,
159773647a0SAndy Whitcroft	'test-only=s'	=> \$tst_only,
160ebfd7d62SJoe Perches	'codespell!'	=> \$codespell,
161ebfd7d62SJoe Perches	'codespellfile=s'	=> \$codespellfile,
16257230297SJoe Perches	'color!'	=> \$color,
16377f5b10aSHannes Eder	'h|help'	=> \$help,
16477f5b10aSHannes Eder	'version'	=> \$help
16577f5b10aSHannes Eder) or help(1);
16677f5b10aSHannes Eder
16777f5b10aSHannes Ederhelp(0) if ($help);
1680a920b5bSAndy Whitcroft
1699624b8d6SJoe Perches$fix = 1 if ($fix_inplace);
1702ac73b4fSJoe Perches$check_orig = $check;
1719624b8d6SJoe Perches
1720a920b5bSAndy Whitcroftmy $exit = 0;
1730a920b5bSAndy Whitcroft
174d62a201fSDave Hansenif ($^V && $^V lt $minimum_perl_version) {
175d62a201fSDave Hansen	printf "$P: requires at least perl version %vd\n", $minimum_perl_version;
176d62a201fSDave Hansen	if (!$ignore_perl_version) {
177d62a201fSDave Hansen		exit(1);
178d62a201fSDave Hansen	}
179d62a201fSDave Hansen}
180d62a201fSDave Hansen
1810a920b5bSAndy Whitcroftif ($#ARGV < 0) {
18277f5b10aSHannes Eder	print "$P: no input files\n";
1830a920b5bSAndy Whitcroft	exit(1);
1840a920b5bSAndy Whitcroft}
1850a920b5bSAndy Whitcroft
18691bfe484SJoe Perchessub hash_save_array_words {
18791bfe484SJoe Perches	my ($hashRef, $arrayRef) = @_;
18891bfe484SJoe Perches
18991bfe484SJoe Perches	my @array = split(/,/, join(',', @$arrayRef));
19091bfe484SJoe Perches	foreach my $word (@array) {
191000d1cc1SJoe Perches		$word =~ s/\s*\n?$//g;
192000d1cc1SJoe Perches		$word =~ s/^\s*//g;
193000d1cc1SJoe Perches		$word =~ s/\s+/ /g;
194000d1cc1SJoe Perches		$word =~ tr/[a-z]/[A-Z]/;
195000d1cc1SJoe Perches
196000d1cc1SJoe Perches		next if ($word =~ m/^\s*#/);
197000d1cc1SJoe Perches		next if ($word =~ m/^\s*$/);
198000d1cc1SJoe Perches
19991bfe484SJoe Perches		$hashRef->{$word}++;
200000d1cc1SJoe Perches	}
20191bfe484SJoe Perches}
20291bfe484SJoe Perches
20391bfe484SJoe Perchessub hash_show_words {
20491bfe484SJoe Perches	my ($hashRef, $prefix) = @_;
20591bfe484SJoe Perches
2063c816e49SJoe Perches	if (keys %$hashRef) {
207d8469f16SJoe Perches		print "\nNOTE: $prefix message types:";
20858cb3cf6SJoe Perches		foreach my $word (sort keys %$hashRef) {
20991bfe484SJoe Perches			print " $word";
21091bfe484SJoe Perches		}
211d8469f16SJoe Perches		print "\n";
21291bfe484SJoe Perches	}
21391bfe484SJoe Perches}
21491bfe484SJoe Perches
21591bfe484SJoe Percheshash_save_array_words(\%ignore_type, \@ignore);
21691bfe484SJoe Percheshash_save_array_words(\%use_type, \@use);
217000d1cc1SJoe Perches
218c2fdda0dSAndy Whitcroftmy $dbg_values = 0;
219c2fdda0dSAndy Whitcroftmy $dbg_possible = 0;
2207429c690SAndy Whitcroftmy $dbg_type = 0;
221a1ef277eSAndy Whitcroftmy $dbg_attr = 0;
222c2fdda0dSAndy Whitcroftfor my $key (keys %debug) {
22321caa13cSAndy Whitcroft	## no critic
22421caa13cSAndy Whitcroft	eval "\${dbg_$key} = '$debug{$key}';";
22521caa13cSAndy Whitcroft	die "$@" if ($@);
226c2fdda0dSAndy Whitcroft}
227c2fdda0dSAndy Whitcroft
228d2c0a235SAndy Whitcroftmy $rpt_cleaners = 0;
229d2c0a235SAndy Whitcroft
2308905a67cSAndy Whitcroftif ($terse) {
2318905a67cSAndy Whitcroft	$emacs = 1;
2328905a67cSAndy Whitcroft	$quiet++;
2338905a67cSAndy Whitcroft}
2348905a67cSAndy Whitcroft
2356c72ffaaSAndy Whitcroftif ($tree) {
2366c72ffaaSAndy Whitcroft	if (defined $root) {
2376c72ffaaSAndy Whitcroft		if (!top_of_kernel_tree($root)) {
2386c72ffaaSAndy Whitcroft			die "$P: $root: --root does not point at a valid tree\n";
2396c72ffaaSAndy Whitcroft		}
2406c72ffaaSAndy Whitcroft	} else {
2416c72ffaaSAndy Whitcroft		if (top_of_kernel_tree('.')) {
2426c72ffaaSAndy Whitcroft			$root = '.';
2436c72ffaaSAndy Whitcroft		} elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
2446c72ffaaSAndy Whitcroft						top_of_kernel_tree($1)) {
2456c72ffaaSAndy Whitcroft			$root = $1;
2466c72ffaaSAndy Whitcroft		}
2476c72ffaaSAndy Whitcroft	}
2486c72ffaaSAndy Whitcroft
2496c72ffaaSAndy Whitcroft	if (!defined $root) {
2500a920b5bSAndy Whitcroft		print "Must be run from the top-level dir. of a kernel tree\n";
2510a920b5bSAndy Whitcroft		exit(2);
2520a920b5bSAndy Whitcroft	}
2536c72ffaaSAndy Whitcroft}
2546c72ffaaSAndy Whitcroft
2556c72ffaaSAndy Whitcroftmy $emitted_corrupt = 0;
2566c72ffaaSAndy Whitcroft
2572ceb532bSAndy Whitcroftour $Ident	= qr{
2582ceb532bSAndy Whitcroft			[A-Za-z_][A-Za-z\d_]*
2592ceb532bSAndy Whitcroft			(?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
2602ceb532bSAndy Whitcroft		}x;
2616c72ffaaSAndy Whitcroftour $Storage	= qr{extern|static|asmlinkage};
2626c72ffaaSAndy Whitcroftour $Sparse	= qr{
2636c72ffaaSAndy Whitcroft			__user|
2646c72ffaaSAndy Whitcroft			__kernel|
2656c72ffaaSAndy Whitcroft			__force|
2666c72ffaaSAndy Whitcroft			__iomem|
26754507b51SJoe Perches			__pmem|
2686c72ffaaSAndy Whitcroft			__must_check|
2696c72ffaaSAndy Whitcroft			__init_refok|
270417495edSAndy Whitcroft			__kprobes|
271165e72a6SSven Eckelmann			__ref|
272165e72a6SSven Eckelmann			__rcu
2736c72ffaaSAndy Whitcroft		}x;
274e970b884SJoe Perchesour $InitAttributePrefix = qr{__(?:mem|cpu|dev|net_|)};
275e970b884SJoe Perchesour $InitAttributeData = qr{$InitAttributePrefix(?:initdata\b)};
276e970b884SJoe Perchesour $InitAttributeConst = qr{$InitAttributePrefix(?:initconst\b)};
277e970b884SJoe Perchesour $InitAttributeInit = qr{$InitAttributePrefix(?:init\b)};
278e970b884SJoe Perchesour $InitAttribute = qr{$InitAttributeData|$InitAttributeConst|$InitAttributeInit};
2798716de38SJoe Perches
28052131292SWolfram Sang# Notes to $Attribute:
28152131292SWolfram Sang# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
2826c72ffaaSAndy Whitcroftour $Attribute	= qr{
2836c72ffaaSAndy Whitcroft			const|
28403f1df7dSJoe Perches			__percpu|
28503f1df7dSJoe Perches			__nocast|
28603f1df7dSJoe Perches			__safe|
28703f1df7dSJoe Perches			__bitwise__|
28803f1df7dSJoe Perches			__packed__|
28903f1df7dSJoe Perches			__packed2__|
29003f1df7dSJoe Perches			__naked|
29103f1df7dSJoe Perches			__maybe_unused|
29203f1df7dSJoe Perches			__always_unused|
29303f1df7dSJoe Perches			__noreturn|
29403f1df7dSJoe Perches			__used|
29503f1df7dSJoe Perches			__cold|
296e23ef1f3SJoe Perches			__pure|
29703f1df7dSJoe Perches			__noclone|
29803f1df7dSJoe Perches			__deprecated|
2996c72ffaaSAndy Whitcroft			__read_mostly|
3006c72ffaaSAndy Whitcroft			__kprobes|
3018716de38SJoe Perches			$InitAttribute|
30224e1d81aSAndy Whitcroft			____cacheline_aligned|
30324e1d81aSAndy Whitcroft			____cacheline_aligned_in_smp|
3045fe3af11SAndy Whitcroft			____cacheline_internodealigned_in_smp|
3055fe3af11SAndy Whitcroft			__weak
3066c72ffaaSAndy Whitcroft		  }x;
307c45dcabdSAndy Whitcroftour $Modifier;
30891cb5195SJoe Perchesour $Inline	= qr{inline|__always_inline|noinline|__inline|__inline__};
3096c72ffaaSAndy Whitcroftour $Member	= qr{->$Ident|\.$Ident|\[[^]]*\]};
3106c72ffaaSAndy Whitcroftour $Lval	= qr{$Ident(?:$Member)*};
3116c72ffaaSAndy Whitcroft
31295e2c602SJoe Perchesour $Int_type	= qr{(?i)llu|ull|ll|lu|ul|l|u};
31395e2c602SJoe Perchesour $Binary	= qr{(?i)0b[01]+$Int_type?};
31495e2c602SJoe Perchesour $Hex	= qr{(?i)0x[0-9a-f]+$Int_type?};
31595e2c602SJoe Perchesour $Int	= qr{[0-9]+$Int_type?};
3162435880fSJoe Perchesour $Octal	= qr{0[0-7]+$Int_type?};
317c0a5c898SJoe Perchesour $String	= qr{"[X\t]*"};
318326b1ffcSJoe Perchesour $Float_hex	= qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?};
319326b1ffcSJoe Perchesour $Float_dec	= qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?};
320326b1ffcSJoe Perchesour $Float_int	= qr{(?i)[0-9]+e-?[0-9]+[fl]?};
32174349bccSJoe Perchesour $Float	= qr{$Float_hex|$Float_dec|$Float_int};
3222435880fSJoe Perchesour $Constant	= qr{$Float|$Binary|$Octal|$Hex|$Int};
323326b1ffcSJoe Perchesour $Assignment	= qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=};
324447432f3SJoe Perchesour $Compare    = qr{<=|>=|==|!=|<|(?<!-)>};
32523f780c9SJoe Perchesour $Arithmetic = qr{\+|-|\*|\/|%};
3266c72ffaaSAndy Whitcroftour $Operators	= qr{
3276c72ffaaSAndy Whitcroft			<=|>=|==|!=|
3286c72ffaaSAndy Whitcroft			=>|->|<<|>>|<|>|!|~|
32923f780c9SJoe Perches			&&|\|\||,|\^|\+\+|--|&|\||$Arithmetic
3306c72ffaaSAndy Whitcroft		  }x;
3316c72ffaaSAndy Whitcroft
33291cb5195SJoe Perchesour $c90_Keywords = qr{do|for|while|if|else|return|goto|continue|switch|default|case|break}x;
33391cb5195SJoe Perches
334ab7e23f3SJoe Perchesour $BasicType;
3358905a67cSAndy Whitcroftour $NonptrType;
3361813087dSJoe Perchesour $NonptrTypeMisordered;
3378716de38SJoe Perchesour $NonptrTypeWithAttr;
3388905a67cSAndy Whitcroftour $Type;
3391813087dSJoe Perchesour $TypeMisordered;
3408905a67cSAndy Whitcroftour $Declare;
3411813087dSJoe Perchesour $DeclareMisordered;
3428905a67cSAndy Whitcroft
34315662b3eSJoe Perchesour $NON_ASCII_UTF8	= qr{
34415662b3eSJoe Perches	[\xC2-\xDF][\x80-\xBF]               # non-overlong 2-byte
345171ae1a4SAndy Whitcroft	|  \xE0[\xA0-\xBF][\x80-\xBF]        # excluding overlongs
346171ae1a4SAndy Whitcroft	| [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}  # straight 3-byte
347171ae1a4SAndy Whitcroft	|  \xED[\x80-\x9F][\x80-\xBF]        # excluding surrogates
348171ae1a4SAndy Whitcroft	|  \xF0[\x90-\xBF][\x80-\xBF]{2}     # planes 1-3
349171ae1a4SAndy Whitcroft	| [\xF1-\xF3][\x80-\xBF]{3}          # planes 4-15
350171ae1a4SAndy Whitcroft	|  \xF4[\x80-\x8F][\x80-\xBF]{2}     # plane 16
351171ae1a4SAndy Whitcroft}x;
352171ae1a4SAndy Whitcroft
35315662b3eSJoe Perchesour $UTF8	= qr{
35415662b3eSJoe Perches	[\x09\x0A\x0D\x20-\x7E]              # ASCII
35515662b3eSJoe Perches	| $NON_ASCII_UTF8
35615662b3eSJoe Perches}x;
35715662b3eSJoe Perches
358e6176fa4SJoe Perchesour $typeC99Typedefs = qr{(?:__)?(?:[us]_?)?int_?(?:8|16|32|64)_t};
359021158b4SJoe Perchesour $typeOtherOSTypedefs = qr{(?x:
360021158b4SJoe Perches	u_(?:char|short|int|long) |          # bsd
361021158b4SJoe Perches	u(?:nchar|short|int|long)            # sysv
362021158b4SJoe Perches)};
363e6176fa4SJoe Perchesour $typeKernelTypedefs = qr{(?x:
364fb9e9096SAndy Whitcroft	(?:__)?(?:u|s|be|le)(?:8|16|32|64)|
3658ed22cadSAndy Whitcroft	atomic_t
3668ed22cadSAndy Whitcroft)};
367e6176fa4SJoe Perchesour $typeTypedefs = qr{(?x:
368e6176fa4SJoe Perches	$typeC99Typedefs\b|
369e6176fa4SJoe Perches	$typeOtherOSTypedefs\b|
370e6176fa4SJoe Perches	$typeKernelTypedefs\b
371e6176fa4SJoe Perches)};
3728ed22cadSAndy Whitcroft
373691e669bSJoe Perchesour $logFunctions = qr{(?x:
3746e60c02eSJoe Perches	printk(?:_ratelimited|_once|)|
3757d0b6594SJacob Keller	(?:[a-z0-9]+_){1,2}(?:printk|emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)(?:_ratelimited|_once|)|
3766e60c02eSJoe Perches	WARN(?:_RATELIMIT|_ONCE|)|
377b0531722SJoe Perches	panic|
37806668727SJoe Perches	MODULE_[A-Z_]+|
37906668727SJoe Perches	seq_vprintf|seq_printf|seq_puts
380691e669bSJoe Perches)};
381691e669bSJoe Perches
38220112475SJoe Perchesour $signature_tags = qr{(?xi:
38320112475SJoe Perches	Signed-off-by:|
38420112475SJoe Perches	Acked-by:|
38520112475SJoe Perches	Tested-by:|
38620112475SJoe Perches	Reviewed-by:|
38720112475SJoe Perches	Reported-by:|
3888543ae12SMugunthan V N	Suggested-by:|
38920112475SJoe Perches	To:|
39020112475SJoe Perches	Cc:
39120112475SJoe Perches)};
39220112475SJoe Perches
3931813087dSJoe Perchesour @typeListMisordered = (
3941813087dSJoe Perches	qr{char\s+(?:un)?signed},
3951813087dSJoe Perches	qr{int\s+(?:(?:un)?signed\s+)?short\s},
3961813087dSJoe Perches	qr{int\s+short(?:\s+(?:un)?signed)},
3971813087dSJoe Perches	qr{short\s+int(?:\s+(?:un)?signed)},
3981813087dSJoe Perches	qr{(?:un)?signed\s+int\s+short},
3991813087dSJoe Perches	qr{short\s+(?:un)?signed},
4001813087dSJoe Perches	qr{long\s+int\s+(?:un)?signed},
4011813087dSJoe Perches	qr{int\s+long\s+(?:un)?signed},
4021813087dSJoe Perches	qr{long\s+(?:un)?signed\s+int},
4031813087dSJoe Perches	qr{int\s+(?:un)?signed\s+long},
4041813087dSJoe Perches	qr{int\s+(?:un)?signed},
4051813087dSJoe Perches	qr{int\s+long\s+long\s+(?:un)?signed},
4061813087dSJoe Perches	qr{long\s+long\s+int\s+(?:un)?signed},
4071813087dSJoe Perches	qr{long\s+long\s+(?:un)?signed\s+int},
4081813087dSJoe Perches	qr{long\s+long\s+(?:un)?signed},
4091813087dSJoe Perches	qr{long\s+(?:un)?signed},
4101813087dSJoe Perches);
4111813087dSJoe Perches
4128905a67cSAndy Whitcroftour @typeList = (
4138905a67cSAndy Whitcroft	qr{void},
4140c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?char},
4150c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?short\s+int},
4160c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?short},
4170c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?int},
4180c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?long\s+int},
4190c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?long\s+long\s+int},
4200c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?long\s+long},
4210c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?long},
4220c773d9dSJoe Perches	qr{(?:un)?signed},
4238905a67cSAndy Whitcroft	qr{float},
4248905a67cSAndy Whitcroft	qr{double},
4258905a67cSAndy Whitcroft	qr{bool},
4268905a67cSAndy Whitcroft	qr{struct\s+$Ident},
4278905a67cSAndy Whitcroft	qr{union\s+$Ident},
4288905a67cSAndy Whitcroft	qr{enum\s+$Ident},
4298905a67cSAndy Whitcroft	qr{${Ident}_t},
4308905a67cSAndy Whitcroft	qr{${Ident}_handler},
4318905a67cSAndy Whitcroft	qr{${Ident}_handler_fn},
4321813087dSJoe Perches	@typeListMisordered,
4338905a67cSAndy Whitcroft);
434485ff23eSAlex Dowadour @typeListFile = ();
4358716de38SJoe Perchesour @typeListWithAttr = (
4368716de38SJoe Perches	@typeList,
4378716de38SJoe Perches	qr{struct\s+$InitAttribute\s+$Ident},
4388716de38SJoe Perches	qr{union\s+$InitAttribute\s+$Ident},
4398716de38SJoe Perches);
4408716de38SJoe Perches
441c45dcabdSAndy Whitcroftour @modifierList = (
442c45dcabdSAndy Whitcroft	qr{fastcall},
443c45dcabdSAndy Whitcroft);
444485ff23eSAlex Dowadour @modifierListFile = ();
4458905a67cSAndy Whitcroft
4462435880fSJoe Perchesour @mode_permission_funcs = (
4472435880fSJoe Perches	["module_param", 3],
4482435880fSJoe Perches	["module_param_(?:array|named|string)", 4],
4492435880fSJoe Perches	["module_param_array_named", 5],
4502435880fSJoe Perches	["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2],
4512435880fSJoe Perches	["proc_create(?:_data|)", 2],
4522435880fSJoe Perches	["(?:CLASS|DEVICE|SENSOR)_ATTR", 2],
4532435880fSJoe Perches);
4542435880fSJoe Perches
455515a235eSJoe Perches#Create a search pattern for all these functions to speed up a loop below
456515a235eSJoe Perchesour $mode_perms_search = "";
457515a235eSJoe Perchesforeach my $entry (@mode_permission_funcs) {
458515a235eSJoe Perches	$mode_perms_search .= '|' if ($mode_perms_search ne "");
459515a235eSJoe Perches	$mode_perms_search .= $entry->[0];
460515a235eSJoe Perches}
461515a235eSJoe Perches
462b392c64fSJoe Perchesour $mode_perms_world_writable = qr{
463b392c64fSJoe Perches	S_IWUGO		|
464b392c64fSJoe Perches	S_IWOTH		|
465b392c64fSJoe Perches	S_IRWXUGO	|
466b392c64fSJoe Perches	S_IALLUGO	|
467b392c64fSJoe Perches	0[0-7][0-7][2367]
468b392c64fSJoe Perches}x;
469b392c64fSJoe Perches
4707840a94cSWolfram Sangour $allowed_asm_includes = qr{(?x:
4717840a94cSWolfram Sang	irq|
472cdcee686SSergey Ryazanov	memory|
473cdcee686SSergey Ryazanov	time|
474cdcee686SSergey Ryazanov	reboot
4757840a94cSWolfram Sang)};
4767840a94cSWolfram Sang# memory.h: ARM has a custom one
4777840a94cSWolfram Sang
47866b47b4aSKees Cook# Load common spelling mistakes and build regular expression list.
47966b47b4aSKees Cookmy $misspellings;
48066b47b4aSKees Cookmy %spelling_fix;
48136061e38SJoe Perches
48236061e38SJoe Perchesif (open(my $spelling, '<', $spelling_file)) {
48366b47b4aSKees Cook	while (<$spelling>) {
48466b47b4aSKees Cook		my $line = $_;
48566b47b4aSKees Cook
48666b47b4aSKees Cook		$line =~ s/\s*\n?$//g;
48766b47b4aSKees Cook		$line =~ s/^\s*//g;
48866b47b4aSKees Cook
48966b47b4aSKees Cook		next if ($line =~ m/^\s*#/);
49066b47b4aSKees Cook		next if ($line =~ m/^\s*$/);
49166b47b4aSKees Cook
49266b47b4aSKees Cook		my ($suspect, $fix) = split(/\|\|/, $line);
49366b47b4aSKees Cook
49466b47b4aSKees Cook		$spelling_fix{$suspect} = $fix;
49566b47b4aSKees Cook	}
49666b47b4aSKees Cook	close($spelling);
49736061e38SJoe Perches} else {
49836061e38SJoe Perches	warn "No typos will be found - file '$spelling_file': $!\n";
49936061e38SJoe Perches}
50066b47b4aSKees Cook
501ebfd7d62SJoe Perchesif ($codespell) {
502ebfd7d62SJoe Perches	if (open(my $spelling, '<', $codespellfile)) {
503ebfd7d62SJoe Perches		while (<$spelling>) {
504ebfd7d62SJoe Perches			my $line = $_;
505ebfd7d62SJoe Perches
506ebfd7d62SJoe Perches			$line =~ s/\s*\n?$//g;
507ebfd7d62SJoe Perches			$line =~ s/^\s*//g;
508ebfd7d62SJoe Perches
509ebfd7d62SJoe Perches			next if ($line =~ m/^\s*#/);
510ebfd7d62SJoe Perches			next if ($line =~ m/^\s*$/);
511ebfd7d62SJoe Perches			next if ($line =~ m/, disabled/i);
512ebfd7d62SJoe Perches
513ebfd7d62SJoe Perches			$line =~ s/,.*$//;
514ebfd7d62SJoe Perches
515ebfd7d62SJoe Perches			my ($suspect, $fix) = split(/->/, $line);
516ebfd7d62SJoe Perches
517ebfd7d62SJoe Perches			$spelling_fix{$suspect} = $fix;
518ebfd7d62SJoe Perches		}
519ebfd7d62SJoe Perches		close($spelling);
520ebfd7d62SJoe Perches	} else {
521ebfd7d62SJoe Perches		warn "No codespell typos will be found - file '$codespellfile': $!\n";
522ebfd7d62SJoe Perches	}
523ebfd7d62SJoe Perches}
524ebfd7d62SJoe Perches
525ebfd7d62SJoe Perches$misspellings = join("|", sort keys %spelling_fix) if keys %spelling_fix;
526ebfd7d62SJoe Perches
5278905a67cSAndy Whitcroftsub build_types {
528485ff23eSAlex Dowad	my $mods = "(?x:  \n" . join("|\n  ", (@modifierList, @modifierListFile)) . "\n)";
529485ff23eSAlex Dowad	my $all = "(?x:  \n" . join("|\n  ", (@typeList, @typeListFile)) . "\n)";
5301813087dSJoe Perches	my $Misordered = "(?x:  \n" . join("|\n  ", @typeListMisordered) . "\n)";
5318716de38SJoe Perches	my $allWithAttr = "(?x:  \n" . join("|\n  ", @typeListWithAttr) . "\n)";
532c8cb2ca3SAndy Whitcroft	$Modifier	= qr{(?:$Attribute|$Sparse|$mods)};
533ab7e23f3SJoe Perches	$BasicType	= qr{
534ab7e23f3SJoe Perches				(?:$typeTypedefs\b)|
535ab7e23f3SJoe Perches				(?:${all}\b)
536ab7e23f3SJoe Perches		}x;
5378905a67cSAndy Whitcroft	$NonptrType	= qr{
538d2172eb5SAndy Whitcroft			(?:$Modifier\s+|const\s+)*
539cf655043SAndy Whitcroft			(?:
5406b48db24SAndy Whitcroft				(?:typeof|__typeof__)\s*\([^\)]*\)|
5418ed22cadSAndy Whitcroft				(?:$typeTypedefs\b)|
542c45dcabdSAndy Whitcroft				(?:${all}\b)
543cf655043SAndy Whitcroft			)
544c8cb2ca3SAndy Whitcroft			(?:\s+$Modifier|\s+const)*
5458905a67cSAndy Whitcroft		  }x;
5461813087dSJoe Perches	$NonptrTypeMisordered	= qr{
5471813087dSJoe Perches			(?:$Modifier\s+|const\s+)*
5481813087dSJoe Perches			(?:
5491813087dSJoe Perches				(?:${Misordered}\b)
5501813087dSJoe Perches			)
5511813087dSJoe Perches			(?:\s+$Modifier|\s+const)*
5521813087dSJoe Perches		  }x;
5538716de38SJoe Perches	$NonptrTypeWithAttr	= qr{
5548716de38SJoe Perches			(?:$Modifier\s+|const\s+)*
5558716de38SJoe Perches			(?:
5568716de38SJoe Perches				(?:typeof|__typeof__)\s*\([^\)]*\)|
5578716de38SJoe Perches				(?:$typeTypedefs\b)|
5588716de38SJoe Perches				(?:${allWithAttr}\b)
5598716de38SJoe Perches			)
5608716de38SJoe Perches			(?:\s+$Modifier|\s+const)*
5618716de38SJoe Perches		  }x;
5628905a67cSAndy Whitcroft	$Type	= qr{
563c45dcabdSAndy Whitcroft			$NonptrType
5641574a29fSJoe Perches			(?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
565c8cb2ca3SAndy Whitcroft			(?:\s+$Inline|\s+$Modifier)*
5668905a67cSAndy Whitcroft		  }x;
5671813087dSJoe Perches	$TypeMisordered	= qr{
5681813087dSJoe Perches			$NonptrTypeMisordered
5691813087dSJoe Perches			(?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
5701813087dSJoe Perches			(?:\s+$Inline|\s+$Modifier)*
5711813087dSJoe Perches		  }x;
57291cb5195SJoe Perches	$Declare	= qr{(?:$Storage\s+(?:$Inline\s+)?)?$Type};
5731813087dSJoe Perches	$DeclareMisordered	= qr{(?:$Storage\s+(?:$Inline\s+)?)?$TypeMisordered};
5748905a67cSAndy Whitcroft}
5758905a67cSAndy Whitcroftbuild_types();
5766c72ffaaSAndy Whitcroft
5777d2367afSJoe Perchesour $Typecast	= qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
578d1fe9c09SJoe Perches
579d1fe9c09SJoe Perches# Using $balanced_parens, $LvalOrFunc, or $FuncArg
580d1fe9c09SJoe Perches# requires at least perl version v5.10.0
581d1fe9c09SJoe Perches# Any use must be runtime checked with $^V
582d1fe9c09SJoe Perches
583d1fe9c09SJoe Perchesour $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
5842435880fSJoe Perchesour $LvalOrFunc	= qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*};
585c0a5c898SJoe Perchesour $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant|$String)};
5867d2367afSJoe Perches
587f8422308SJoe Perchesour $declaration_macros = qr{(?x:
5883e838b6cSJoe Perches	(?:$Storage\s+)?(?:[A-Z_][A-Z0-9]*_){0,2}(?:DEFINE|DECLARE)(?:_[A-Z0-9]+){1,6}\s*\(|
589f8422308SJoe Perches	(?:$Storage\s+)?LIST_HEAD\s*\(|
590f8422308SJoe Perches	(?:$Storage\s+)?${Type}\s+uninitialized_var\s*\(
591f8422308SJoe Perches)};
592f8422308SJoe Perches
5937d2367afSJoe Perchessub deparenthesize {
5947d2367afSJoe Perches	my ($string) = @_;
5957d2367afSJoe Perches	return "" if (!defined($string));
5965b9553abSJoe Perches
5975b9553abSJoe Perches	while ($string =~ /^\s*\(.*\)\s*$/) {
5985b9553abSJoe Perches		$string =~ s@^\s*\(\s*@@;
5995b9553abSJoe Perches		$string =~ s@\s*\)\s*$@@;
6005b9553abSJoe Perches	}
6015b9553abSJoe Perches
6027d2367afSJoe Perches	$string =~ s@\s+@ @g;
6035b9553abSJoe Perches
6047d2367afSJoe Perches	return $string;
6057d2367afSJoe Perches}
6067d2367afSJoe Perches
6073445686aSJoe Perchessub seed_camelcase_file {
6083445686aSJoe Perches	my ($file) = @_;
6093445686aSJoe Perches
6103445686aSJoe Perches	return if (!(-f $file));
6113445686aSJoe Perches
6123445686aSJoe Perches	local $/;
6133445686aSJoe Perches
6143445686aSJoe Perches	open(my $include_file, '<', "$file")
6153445686aSJoe Perches	    or warn "$P: Can't read '$file' $!\n";
6163445686aSJoe Perches	my $text = <$include_file>;
6173445686aSJoe Perches	close($include_file);
6183445686aSJoe Perches
6193445686aSJoe Perches	my @lines = split('\n', $text);
6203445686aSJoe Perches
6213445686aSJoe Perches	foreach my $line (@lines) {
6223445686aSJoe Perches		next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/);
6233445686aSJoe Perches		if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) {
6243445686aSJoe Perches			$camelcase{$1} = 1;
62511ea516aSJoe Perches		} elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) {
62611ea516aSJoe Perches			$camelcase{$1} = 1;
62711ea516aSJoe Perches		} elsif ($line =~ /^\s*(?:union|struct|enum)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[;\{]/) {
6283445686aSJoe Perches			$camelcase{$1} = 1;
6293445686aSJoe Perches		}
6303445686aSJoe Perches	}
6313445686aSJoe Perches}
6323445686aSJoe Perches
6333445686aSJoe Perchesmy $camelcase_seeded = 0;
6343445686aSJoe Perchessub seed_camelcase_includes {
6353445686aSJoe Perches	return if ($camelcase_seeded);
6363445686aSJoe Perches
6373445686aSJoe Perches	my $files;
638c707a81dSJoe Perches	my $camelcase_cache = "";
639c707a81dSJoe Perches	my @include_files = ();
640c707a81dSJoe Perches
641c707a81dSJoe Perches	$camelcase_seeded = 1;
642351b2a1fSJoe Perches
6433645e328SRichard Genoud	if (-e ".git") {
644351b2a1fSJoe Perches		my $git_last_include_commit = `git log --no-merges --pretty=format:"%h%n" -1 -- include`;
645351b2a1fSJoe Perches		chomp $git_last_include_commit;
646c707a81dSJoe Perches		$camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit";
647c707a81dSJoe Perches	} else {
648c707a81dSJoe Perches		my $last_mod_date = 0;
649c707a81dSJoe Perches		$files = `find $root/include -name "*.h"`;
650c707a81dSJoe Perches		@include_files = split('\n', $files);
651c707a81dSJoe Perches		foreach my $file (@include_files) {
652c707a81dSJoe Perches			my $date = POSIX::strftime("%Y%m%d%H%M",
653c707a81dSJoe Perches						   localtime((stat $file)[9]));
654c707a81dSJoe Perches			$last_mod_date = $date if ($last_mod_date < $date);
655c707a81dSJoe Perches		}
656c707a81dSJoe Perches		$camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date";
657c707a81dSJoe Perches	}
658c707a81dSJoe Perches
659c707a81dSJoe Perches	if ($camelcase_cache ne "" && -f $camelcase_cache) {
660c707a81dSJoe Perches		open(my $camelcase_file, '<', "$camelcase_cache")
661c707a81dSJoe Perches		    or warn "$P: Can't read '$camelcase_cache' $!\n";
662351b2a1fSJoe Perches		while (<$camelcase_file>) {
663351b2a1fSJoe Perches			chomp;
664351b2a1fSJoe Perches			$camelcase{$_} = 1;
665351b2a1fSJoe Perches		}
666351b2a1fSJoe Perches		close($camelcase_file);
667351b2a1fSJoe Perches
668351b2a1fSJoe Perches		return;
669351b2a1fSJoe Perches	}
670c707a81dSJoe Perches
6713645e328SRichard Genoud	if (-e ".git") {
672c707a81dSJoe Perches		$files = `git ls-files "include/*.h"`;
673c707a81dSJoe Perches		@include_files = split('\n', $files);
6743445686aSJoe Perches	}
675c707a81dSJoe Perches
6763445686aSJoe Perches	foreach my $file (@include_files) {
6773445686aSJoe Perches		seed_camelcase_file($file);
6783445686aSJoe Perches	}
679351b2a1fSJoe Perches
680c707a81dSJoe Perches	if ($camelcase_cache ne "") {
681351b2a1fSJoe Perches		unlink glob ".checkpatch-camelcase.*";
682c707a81dSJoe Perches		open(my $camelcase_file, '>', "$camelcase_cache")
683c707a81dSJoe Perches		    or warn "$P: Can't write '$camelcase_cache' $!\n";
684351b2a1fSJoe Perches		foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) {
685351b2a1fSJoe Perches			print $camelcase_file ("$_\n");
686351b2a1fSJoe Perches		}
687351b2a1fSJoe Perches		close($camelcase_file);
688351b2a1fSJoe Perches	}
6893445686aSJoe Perches}
6903445686aSJoe Perches
691d311cd44SJoe Perchessub git_commit_info {
692d311cd44SJoe Perches	my ($commit, $id, $desc) = @_;
693d311cd44SJoe Perches
694d311cd44SJoe Perches	return ($id, $desc) if ((which("git") eq "") || !(-e ".git"));
695d311cd44SJoe Perches
696d311cd44SJoe Perches	my $output = `git log --no-color --format='%H %s' -1 $commit 2>&1`;
697d311cd44SJoe Perches	$output =~ s/^\s*//gm;
698d311cd44SJoe Perches	my @lines = split("\n", $output);
699d311cd44SJoe Perches
7000d7835fcSJoe Perches	return ($id, $desc) if ($#lines < 0);
7010d7835fcSJoe Perches
702d311cd44SJoe Perches	if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous\./) {
703d311cd44SJoe Perches# Maybe one day convert this block of bash into something that returns
704d311cd44SJoe Perches# all matching commit ids, but it's very slow...
705d311cd44SJoe Perches#
706d311cd44SJoe Perches#		echo "checking commits $1..."
707d311cd44SJoe Perches#		git rev-list --remotes | grep -i "^$1" |
708d311cd44SJoe Perches#		while read line ; do
709d311cd44SJoe Perches#		    git log --format='%H %s' -1 $line |
710d311cd44SJoe Perches#		    echo "commit $(cut -c 1-12,41-)"
711d311cd44SJoe Perches#		done
712d311cd44SJoe Perches	} elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./) {
713d311cd44SJoe Perches	} else {
714d311cd44SJoe Perches		$id = substr($lines[0], 0, 12);
715d311cd44SJoe Perches		$desc = substr($lines[0], 41);
716d311cd44SJoe Perches	}
717d311cd44SJoe Perches
718d311cd44SJoe Perches	return ($id, $desc);
719d311cd44SJoe Perches}
720d311cd44SJoe Perches
7216c72ffaaSAndy Whitcroft$chk_signoff = 0 if ($file);
7220a920b5bSAndy Whitcroft
72300df344fSAndy Whitcroftmy @rawlines = ();
724c2fdda0dSAndy Whitcroftmy @lines = ();
7253705ce5bSJoe Perchesmy @fixed = ();
726d752fcc8SJoe Perchesmy @fixed_inserted = ();
727d752fcc8SJoe Perchesmy @fixed_deleted = ();
728194f66fcSJoe Perchesmy $fixlinenr = -1;
729194f66fcSJoe Perches
730c2fdda0dSAndy Whitcroftmy $vname;
7316c72ffaaSAndy Whitcroftfor my $filename (@ARGV) {
73221caa13cSAndy Whitcroft	my $FILE;
7336c72ffaaSAndy Whitcroft	if ($file) {
73421caa13cSAndy Whitcroft		open($FILE, '-|', "diff -u /dev/null $filename") ||
7356c72ffaaSAndy Whitcroft			die "$P: $filename: diff failed - $!\n";
73621caa13cSAndy Whitcroft	} elsif ($filename eq '-') {
73721caa13cSAndy Whitcroft		open($FILE, '<&STDIN');
7386c72ffaaSAndy Whitcroft	} else {
73921caa13cSAndy Whitcroft		open($FILE, '<', "$filename") ||
7406c72ffaaSAndy Whitcroft			die "$P: $filename: open failed - $!\n";
7416c72ffaaSAndy Whitcroft	}
742c2fdda0dSAndy Whitcroft	if ($filename eq '-') {
743c2fdda0dSAndy Whitcroft		$vname = 'Your patch';
744c2fdda0dSAndy Whitcroft	} else {
745c2fdda0dSAndy Whitcroft		$vname = $filename;
746c2fdda0dSAndy Whitcroft	}
74721caa13cSAndy Whitcroft	while (<$FILE>) {
7480a920b5bSAndy Whitcroft		chomp;
74900df344fSAndy Whitcroft		push(@rawlines, $_);
7506c72ffaaSAndy Whitcroft	}
75121caa13cSAndy Whitcroft	close($FILE);
752d8469f16SJoe Perches
753d8469f16SJoe Perches	if ($#ARGV > 0 && $quiet == 0) {
754d8469f16SJoe Perches		print '-' x length($vname) . "\n";
755d8469f16SJoe Perches		print "$vname\n";
756d8469f16SJoe Perches		print '-' x length($vname) . "\n";
757d8469f16SJoe Perches	}
758d8469f16SJoe Perches
759c2fdda0dSAndy Whitcroft	if (!process($filename)) {
7600a920b5bSAndy Whitcroft		$exit = 1;
7610a920b5bSAndy Whitcroft	}
76200df344fSAndy Whitcroft	@rawlines = ();
76313214adfSAndy Whitcroft	@lines = ();
7643705ce5bSJoe Perches	@fixed = ();
765d752fcc8SJoe Perches	@fixed_inserted = ();
766d752fcc8SJoe Perches	@fixed_deleted = ();
767194f66fcSJoe Perches	$fixlinenr = -1;
768485ff23eSAlex Dowad	@modifierListFile = ();
769485ff23eSAlex Dowad	@typeListFile = ();
770485ff23eSAlex Dowad	build_types();
7710a920b5bSAndy Whitcroft}
7720a920b5bSAndy Whitcroft
773d8469f16SJoe Perchesif (!$quiet) {
7743c816e49SJoe Perches	hash_show_words(\%use_type, "Used");
7753c816e49SJoe Perches	hash_show_words(\%ignore_type, "Ignored");
7763c816e49SJoe Perches
777d8469f16SJoe Perches	if ($^V lt 5.10.0) {
778d8469f16SJoe Perches		print << "EOM"
779d8469f16SJoe Perches
780d8469f16SJoe PerchesNOTE: perl $^V is not modern enough to detect all possible issues.
781d8469f16SJoe Perches      An upgrade to at least perl v5.10.0 is suggested.
782d8469f16SJoe PerchesEOM
783d8469f16SJoe Perches	}
784d8469f16SJoe Perches	if ($exit) {
785d8469f16SJoe Perches		print << "EOM"
786d8469f16SJoe Perches
787d8469f16SJoe PerchesNOTE: If any of the errors are false positives, please report
788d8469f16SJoe Perches      them to the maintainer, see CHECKPATCH in MAINTAINERS.
789d8469f16SJoe PerchesEOM
790d8469f16SJoe Perches	}
791d8469f16SJoe Perches}
792d8469f16SJoe Perches
7930a920b5bSAndy Whitcroftexit($exit);
7940a920b5bSAndy Whitcroft
7950a920b5bSAndy Whitcroftsub top_of_kernel_tree {
7966c72ffaaSAndy Whitcroft	my ($root) = @_;
7976c72ffaaSAndy Whitcroft
7986c72ffaaSAndy Whitcroft	my @tree_check = (
7996c72ffaaSAndy Whitcroft		"COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
8006c72ffaaSAndy Whitcroft		"README", "Documentation", "arch", "include", "drivers",
8016c72ffaaSAndy Whitcroft		"fs", "init", "ipc", "kernel", "lib", "scripts",
8026c72ffaaSAndy Whitcroft	);
8036c72ffaaSAndy Whitcroft
8046c72ffaaSAndy Whitcroft	foreach my $check (@tree_check) {
8056c72ffaaSAndy Whitcroft		if (! -e $root . '/' . $check) {
8060a920b5bSAndy Whitcroft			return 0;
8070a920b5bSAndy Whitcroft		}
8086c72ffaaSAndy Whitcroft	}
8096c72ffaaSAndy Whitcroft	return 1;
8106c72ffaaSAndy Whitcroft}
8110a920b5bSAndy Whitcroft
81220112475SJoe Perchessub parse_email {
81320112475SJoe Perches	my ($formatted_email) = @_;
81420112475SJoe Perches
81520112475SJoe Perches	my $name = "";
81620112475SJoe Perches	my $address = "";
81720112475SJoe Perches	my $comment = "";
81820112475SJoe Perches
81920112475SJoe Perches	if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
82020112475SJoe Perches		$name = $1;
82120112475SJoe Perches		$address = $2;
82220112475SJoe Perches		$comment = $3 if defined $3;
82320112475SJoe Perches	} elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
82420112475SJoe Perches		$address = $1;
82520112475SJoe Perches		$comment = $2 if defined $2;
82620112475SJoe Perches	} elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
82720112475SJoe Perches		$address = $1;
82820112475SJoe Perches		$comment = $2 if defined $2;
82920112475SJoe Perches		$formatted_email =~ s/$address.*$//;
83020112475SJoe Perches		$name = $formatted_email;
8313705ce5bSJoe Perches		$name = trim($name);
83220112475SJoe Perches		$name =~ s/^\"|\"$//g;
83320112475SJoe Perches		# If there's a name left after stripping spaces and
83420112475SJoe Perches		# leading quotes, and the address doesn't have both
83520112475SJoe Perches		# leading and trailing angle brackets, the address
83620112475SJoe Perches		# is invalid. ie:
83720112475SJoe Perches		#   "joe smith [email protected]" bad
83820112475SJoe Perches		#   "joe smith <[email protected]" bad
83920112475SJoe Perches		if ($name ne "" && $address !~ /^<[^>]+>$/) {
84020112475SJoe Perches			$name = "";
84120112475SJoe Perches			$address = "";
84220112475SJoe Perches			$comment = "";
84320112475SJoe Perches		}
84420112475SJoe Perches	}
84520112475SJoe Perches
8463705ce5bSJoe Perches	$name = trim($name);
84720112475SJoe Perches	$name =~ s/^\"|\"$//g;
8483705ce5bSJoe Perches	$address = trim($address);
84920112475SJoe Perches	$address =~ s/^\<|\>$//g;
85020112475SJoe Perches
85120112475SJoe Perches	if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
85220112475SJoe Perches		$name =~ s/(?<!\\)"/\\"/g; ##escape quotes
85320112475SJoe Perches		$name = "\"$name\"";
85420112475SJoe Perches	}
85520112475SJoe Perches
85620112475SJoe Perches	return ($name, $address, $comment);
85720112475SJoe Perches}
85820112475SJoe Perches
85920112475SJoe Perchessub format_email {
86020112475SJoe Perches	my ($name, $address) = @_;
86120112475SJoe Perches
86220112475SJoe Perches	my $formatted_email;
86320112475SJoe Perches
8643705ce5bSJoe Perches	$name = trim($name);
86520112475SJoe Perches	$name =~ s/^\"|\"$//g;
8663705ce5bSJoe Perches	$address = trim($address);
86720112475SJoe Perches
86820112475SJoe Perches	if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
86920112475SJoe Perches		$name =~ s/(?<!\\)"/\\"/g; ##escape quotes
87020112475SJoe Perches		$name = "\"$name\"";
87120112475SJoe Perches	}
87220112475SJoe Perches
87320112475SJoe Perches	if ("$name" eq "") {
87420112475SJoe Perches		$formatted_email = "$address";
87520112475SJoe Perches	} else {
87620112475SJoe Perches		$formatted_email = "$name <$address>";
87720112475SJoe Perches	}
87820112475SJoe Perches
87920112475SJoe Perches	return $formatted_email;
88020112475SJoe Perches}
88120112475SJoe Perches
882d311cd44SJoe Perchessub which {
883d311cd44SJoe Perches	my ($bin) = @_;
884d311cd44SJoe Perches
885d311cd44SJoe Perches	foreach my $path (split(/:/, $ENV{PATH})) {
886d311cd44SJoe Perches		if (-e "$path/$bin") {
887d311cd44SJoe Perches			return "$path/$bin";
888d311cd44SJoe Perches		}
889d311cd44SJoe Perches	}
890d311cd44SJoe Perches
891d311cd44SJoe Perches	return "";
892d311cd44SJoe Perches}
893d311cd44SJoe Perches
894000d1cc1SJoe Perchessub which_conf {
895000d1cc1SJoe Perches	my ($conf) = @_;
896000d1cc1SJoe Perches
897000d1cc1SJoe Perches	foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
898000d1cc1SJoe Perches		if (-e "$path/$conf") {
899000d1cc1SJoe Perches			return "$path/$conf";
900000d1cc1SJoe Perches		}
901000d1cc1SJoe Perches	}
902000d1cc1SJoe Perches
903000d1cc1SJoe Perches	return "";
904000d1cc1SJoe Perches}
905000d1cc1SJoe Perches
9060a920b5bSAndy Whitcroftsub expand_tabs {
9070a920b5bSAndy Whitcroft	my ($str) = @_;
9080a920b5bSAndy Whitcroft
9090a920b5bSAndy Whitcroft	my $res = '';
9100a920b5bSAndy Whitcroft	my $n = 0;
9110a920b5bSAndy Whitcroft	for my $c (split(//, $str)) {
9120a920b5bSAndy Whitcroft		if ($c eq "\t") {
9130a920b5bSAndy Whitcroft			$res .= ' ';
9140a920b5bSAndy Whitcroft			$n++;
9150a920b5bSAndy Whitcroft			for (; ($n % 8) != 0; $n++) {
9160a920b5bSAndy Whitcroft				$res .= ' ';
9170a920b5bSAndy Whitcroft			}
9180a920b5bSAndy Whitcroft			next;
9190a920b5bSAndy Whitcroft		}
9200a920b5bSAndy Whitcroft		$res .= $c;
9210a920b5bSAndy Whitcroft		$n++;
9220a920b5bSAndy Whitcroft	}
9230a920b5bSAndy Whitcroft
9240a920b5bSAndy Whitcroft	return $res;
9250a920b5bSAndy Whitcroft}
9266c72ffaaSAndy Whitcroftsub copy_spacing {
927773647a0SAndy Whitcroft	(my $res = shift) =~ tr/\t/ /c;
9286c72ffaaSAndy Whitcroft	return $res;
9296c72ffaaSAndy Whitcroft}
9300a920b5bSAndy Whitcroft
9314a0df2efSAndy Whitcroftsub line_stats {
9324a0df2efSAndy Whitcroft	my ($line) = @_;
9334a0df2efSAndy Whitcroft
9344a0df2efSAndy Whitcroft	# Drop the diff line leader and expand tabs
9354a0df2efSAndy Whitcroft	$line =~ s/^.//;
9364a0df2efSAndy Whitcroft	$line = expand_tabs($line);
9374a0df2efSAndy Whitcroft
9384a0df2efSAndy Whitcroft	# Pick the indent from the front of the line.
9394a0df2efSAndy Whitcroft	my ($white) = ($line =~ /^(\s*)/);
9404a0df2efSAndy Whitcroft
9414a0df2efSAndy Whitcroft	return (length($line), length($white));
9424a0df2efSAndy Whitcroft}
9434a0df2efSAndy Whitcroft
944773647a0SAndy Whitcroftmy $sanitise_quote = '';
945773647a0SAndy Whitcroft
946773647a0SAndy Whitcroftsub sanitise_line_reset {
947773647a0SAndy Whitcroft	my ($in_comment) = @_;
948773647a0SAndy Whitcroft
949773647a0SAndy Whitcroft	if ($in_comment) {
950773647a0SAndy Whitcroft		$sanitise_quote = '*/';
951773647a0SAndy Whitcroft	} else {
952773647a0SAndy Whitcroft		$sanitise_quote = '';
953773647a0SAndy Whitcroft	}
954773647a0SAndy Whitcroft}
95500df344fSAndy Whitcroftsub sanitise_line {
95600df344fSAndy Whitcroft	my ($line) = @_;
95700df344fSAndy Whitcroft
95800df344fSAndy Whitcroft	my $res = '';
95900df344fSAndy Whitcroft	my $l = '';
96000df344fSAndy Whitcroft
961c2fdda0dSAndy Whitcroft	my $qlen = 0;
962773647a0SAndy Whitcroft	my $off = 0;
963773647a0SAndy Whitcroft	my $c;
96400df344fSAndy Whitcroft
965773647a0SAndy Whitcroft	# Always copy over the diff marker.
966773647a0SAndy Whitcroft	$res = substr($line, 0, 1);
967773647a0SAndy Whitcroft
968773647a0SAndy Whitcroft	for ($off = 1; $off < length($line); $off++) {
969773647a0SAndy Whitcroft		$c = substr($line, $off, 1);
970773647a0SAndy Whitcroft
971773647a0SAndy Whitcroft		# Comments we are wacking completly including the begin
972773647a0SAndy Whitcroft		# and end, all to $;.
973773647a0SAndy Whitcroft		if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
974773647a0SAndy Whitcroft			$sanitise_quote = '*/';
975773647a0SAndy Whitcroft
976773647a0SAndy Whitcroft			substr($res, $off, 2, "$;$;");
977773647a0SAndy Whitcroft			$off++;
97800df344fSAndy Whitcroft			next;
979773647a0SAndy Whitcroft		}
98081bc0e02SAndy Whitcroft		if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
981773647a0SAndy Whitcroft			$sanitise_quote = '';
982773647a0SAndy Whitcroft			substr($res, $off, 2, "$;$;");
983773647a0SAndy Whitcroft			$off++;
984773647a0SAndy Whitcroft			next;
985773647a0SAndy Whitcroft		}
986113f04a8SDaniel Walker		if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
987113f04a8SDaniel Walker			$sanitise_quote = '//';
988113f04a8SDaniel Walker
989113f04a8SDaniel Walker			substr($res, $off, 2, $sanitise_quote);
990113f04a8SDaniel Walker			$off++;
991113f04a8SDaniel Walker			next;
992113f04a8SDaniel Walker		}
993773647a0SAndy Whitcroft
994773647a0SAndy Whitcroft		# A \ in a string means ignore the next character.
995773647a0SAndy Whitcroft		if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
996773647a0SAndy Whitcroft		    $c eq "\\") {
997773647a0SAndy Whitcroft			substr($res, $off, 2, 'XX');
998773647a0SAndy Whitcroft			$off++;
999773647a0SAndy Whitcroft			next;
1000773647a0SAndy Whitcroft		}
1001773647a0SAndy Whitcroft		# Regular quotes.
1002773647a0SAndy Whitcroft		if ($c eq "'" || $c eq '"') {
1003773647a0SAndy Whitcroft			if ($sanitise_quote eq '') {
1004773647a0SAndy Whitcroft				$sanitise_quote = $c;
1005773647a0SAndy Whitcroft
1006773647a0SAndy Whitcroft				substr($res, $off, 1, $c);
1007773647a0SAndy Whitcroft				next;
1008773647a0SAndy Whitcroft			} elsif ($sanitise_quote eq $c) {
1009773647a0SAndy Whitcroft				$sanitise_quote = '';
101000df344fSAndy Whitcroft			}
101100df344fSAndy Whitcroft		}
1012773647a0SAndy Whitcroft
1013fae17daeSAndy Whitcroft		#print "c<$c> SQ<$sanitise_quote>\n";
1014773647a0SAndy Whitcroft		if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
1015773647a0SAndy Whitcroft			substr($res, $off, 1, $;);
1016113f04a8SDaniel Walker		} elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
1017113f04a8SDaniel Walker			substr($res, $off, 1, $;);
1018773647a0SAndy Whitcroft		} elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
1019773647a0SAndy Whitcroft			substr($res, $off, 1, 'X');
102000df344fSAndy Whitcroft		} else {
1021773647a0SAndy Whitcroft			substr($res, $off, 1, $c);
102200df344fSAndy Whitcroft		}
1023c2fdda0dSAndy Whitcroft	}
1024c2fdda0dSAndy Whitcroft
1025113f04a8SDaniel Walker	if ($sanitise_quote eq '//') {
1026113f04a8SDaniel Walker		$sanitise_quote = '';
1027113f04a8SDaniel Walker	}
1028113f04a8SDaniel Walker
1029c2fdda0dSAndy Whitcroft	# The pathname on a #include may be surrounded by '<' and '>'.
1030c45dcabdSAndy Whitcroft	if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
1031c2fdda0dSAndy Whitcroft		my $clean = 'X' x length($1);
1032c2fdda0dSAndy Whitcroft		$res =~ s@\<.*\>@<$clean>@;
1033c2fdda0dSAndy Whitcroft
1034c2fdda0dSAndy Whitcroft	# The whole of a #error is a string.
1035c45dcabdSAndy Whitcroft	} elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
1036c2fdda0dSAndy Whitcroft		my $clean = 'X' x length($1);
1037c45dcabdSAndy Whitcroft		$res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
1038c2fdda0dSAndy Whitcroft	}
1039c2fdda0dSAndy Whitcroft
104000df344fSAndy Whitcroft	return $res;
104100df344fSAndy Whitcroft}
104200df344fSAndy Whitcroft
1043a6962d72SJoe Perchessub get_quoted_string {
1044a6962d72SJoe Perches	my ($line, $rawline) = @_;
1045a6962d72SJoe Perches
104633acb54aSJoe Perches	return "" if ($line !~ m/($String)/g);
1047a6962d72SJoe Perches	return substr($rawline, $-[0], $+[0] - $-[0]);
1048a6962d72SJoe Perches}
1049a6962d72SJoe Perches
10508905a67cSAndy Whitcroftsub ctx_statement_block {
10518905a67cSAndy Whitcroft	my ($linenr, $remain, $off) = @_;
10528905a67cSAndy Whitcroft	my $line = $linenr - 1;
10538905a67cSAndy Whitcroft	my $blk = '';
10548905a67cSAndy Whitcroft	my $soff = $off;
10558905a67cSAndy Whitcroft	my $coff = $off - 1;
1056773647a0SAndy Whitcroft	my $coff_set = 0;
10578905a67cSAndy Whitcroft
105813214adfSAndy Whitcroft	my $loff = 0;
105913214adfSAndy Whitcroft
10608905a67cSAndy Whitcroft	my $type = '';
10618905a67cSAndy Whitcroft	my $level = 0;
1062a2750645SAndy Whitcroft	my @stack = ();
1063cf655043SAndy Whitcroft	my $p;
10648905a67cSAndy Whitcroft	my $c;
10658905a67cSAndy Whitcroft	my $len = 0;
106613214adfSAndy Whitcroft
106713214adfSAndy Whitcroft	my $remainder;
10688905a67cSAndy Whitcroft	while (1) {
1069a2750645SAndy Whitcroft		@stack = (['', 0]) if ($#stack == -1);
1070a2750645SAndy Whitcroft
1071773647a0SAndy Whitcroft		#warn "CSB: blk<$blk> remain<$remain>\n";
10728905a67cSAndy Whitcroft		# If we are about to drop off the end, pull in more
10738905a67cSAndy Whitcroft		# context.
10748905a67cSAndy Whitcroft		if ($off >= $len) {
10758905a67cSAndy Whitcroft			for (; $remain > 0; $line++) {
1076dea33496SAndy Whitcroft				last if (!defined $lines[$line]);
1077c2fdda0dSAndy Whitcroft				next if ($lines[$line] =~ /^-/);
10788905a67cSAndy Whitcroft				$remain--;
107913214adfSAndy Whitcroft				$loff = $len;
1080c2fdda0dSAndy Whitcroft				$blk .= $lines[$line] . "\n";
10818905a67cSAndy Whitcroft				$len = length($blk);
10828905a67cSAndy Whitcroft				$line++;
10838905a67cSAndy Whitcroft				last;
10848905a67cSAndy Whitcroft			}
10858905a67cSAndy Whitcroft			# Bail if there is no further context.
10868905a67cSAndy Whitcroft			#warn "CSB: blk<$blk> off<$off> len<$len>\n";
108713214adfSAndy Whitcroft			if ($off >= $len) {
10888905a67cSAndy Whitcroft				last;
10898905a67cSAndy Whitcroft			}
1090f74bd194SAndy Whitcroft			if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
1091f74bd194SAndy Whitcroft				$level++;
1092f74bd194SAndy Whitcroft				$type = '#';
1093f74bd194SAndy Whitcroft			}
10948905a67cSAndy Whitcroft		}
1095cf655043SAndy Whitcroft		$p = $c;
10968905a67cSAndy Whitcroft		$c = substr($blk, $off, 1);
109713214adfSAndy Whitcroft		$remainder = substr($blk, $off);
10988905a67cSAndy Whitcroft
1099773647a0SAndy Whitcroft		#warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
11004635f4fbSAndy Whitcroft
11014635f4fbSAndy Whitcroft		# Handle nested #if/#else.
11024635f4fbSAndy Whitcroft		if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
11034635f4fbSAndy Whitcroft			push(@stack, [ $type, $level ]);
11044635f4fbSAndy Whitcroft		} elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
11054635f4fbSAndy Whitcroft			($type, $level) = @{$stack[$#stack - 1]};
11064635f4fbSAndy Whitcroft		} elsif ($remainder =~ /^#\s*endif\b/) {
11074635f4fbSAndy Whitcroft			($type, $level) = @{pop(@stack)};
11084635f4fbSAndy Whitcroft		}
11094635f4fbSAndy Whitcroft
11108905a67cSAndy Whitcroft		# Statement ends at the ';' or a close '}' at the
11118905a67cSAndy Whitcroft		# outermost level.
11128905a67cSAndy Whitcroft		if ($level == 0 && $c eq ';') {
11138905a67cSAndy Whitcroft			last;
11148905a67cSAndy Whitcroft		}
11158905a67cSAndy Whitcroft
111613214adfSAndy Whitcroft		# An else is really a conditional as long as its not else if
1117773647a0SAndy Whitcroft		if ($level == 0 && $coff_set == 0 &&
1118773647a0SAndy Whitcroft				(!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
1119773647a0SAndy Whitcroft				$remainder =~ /^(else)(?:\s|{)/ &&
1120773647a0SAndy Whitcroft				$remainder !~ /^else\s+if\b/) {
1121773647a0SAndy Whitcroft			$coff = $off + length($1) - 1;
1122773647a0SAndy Whitcroft			$coff_set = 1;
1123773647a0SAndy Whitcroft			#warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
1124773647a0SAndy Whitcroft			#warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
112513214adfSAndy Whitcroft		}
112613214adfSAndy Whitcroft
11278905a67cSAndy Whitcroft		if (($type eq '' || $type eq '(') && $c eq '(') {
11288905a67cSAndy Whitcroft			$level++;
11298905a67cSAndy Whitcroft			$type = '(';
11308905a67cSAndy Whitcroft		}
11318905a67cSAndy Whitcroft		if ($type eq '(' && $c eq ')') {
11328905a67cSAndy Whitcroft			$level--;
11338905a67cSAndy Whitcroft			$type = ($level != 0)? '(' : '';
11348905a67cSAndy Whitcroft
11358905a67cSAndy Whitcroft			if ($level == 0 && $coff < $soff) {
11368905a67cSAndy Whitcroft				$coff = $off;
1137773647a0SAndy Whitcroft				$coff_set = 1;
1138773647a0SAndy Whitcroft				#warn "CSB: mark coff<$coff>\n";
11398905a67cSAndy Whitcroft			}
11408905a67cSAndy Whitcroft		}
11418905a67cSAndy Whitcroft		if (($type eq '' || $type eq '{') && $c eq '{') {
11428905a67cSAndy Whitcroft			$level++;
11438905a67cSAndy Whitcroft			$type = '{';
11448905a67cSAndy Whitcroft		}
11458905a67cSAndy Whitcroft		if ($type eq '{' && $c eq '}') {
11468905a67cSAndy Whitcroft			$level--;
11478905a67cSAndy Whitcroft			$type = ($level != 0)? '{' : '';
11488905a67cSAndy Whitcroft
11498905a67cSAndy Whitcroft			if ($level == 0) {
1150b998e001SPatrick Pannuto				if (substr($blk, $off + 1, 1) eq ';') {
1151b998e001SPatrick Pannuto					$off++;
1152b998e001SPatrick Pannuto				}
11538905a67cSAndy Whitcroft				last;
11548905a67cSAndy Whitcroft			}
11558905a67cSAndy Whitcroft		}
1156f74bd194SAndy Whitcroft		# Preprocessor commands end at the newline unless escaped.
1157f74bd194SAndy Whitcroft		if ($type eq '#' && $c eq "\n" && $p ne "\\") {
1158f74bd194SAndy Whitcroft			$level--;
1159f74bd194SAndy Whitcroft			$type = '';
1160f74bd194SAndy Whitcroft			$off++;
1161f74bd194SAndy Whitcroft			last;
1162f74bd194SAndy Whitcroft		}
11638905a67cSAndy Whitcroft		$off++;
11648905a67cSAndy Whitcroft	}
1165a3bb97a7SAndy Whitcroft	# We are truly at the end, so shuffle to the next line.
116613214adfSAndy Whitcroft	if ($off == $len) {
1167a3bb97a7SAndy Whitcroft		$loff = $len + 1;
116813214adfSAndy Whitcroft		$line++;
116913214adfSAndy Whitcroft		$remain--;
117013214adfSAndy Whitcroft	}
11718905a67cSAndy Whitcroft
11728905a67cSAndy Whitcroft	my $statement = substr($blk, $soff, $off - $soff + 1);
11738905a67cSAndy Whitcroft	my $condition = substr($blk, $soff, $coff - $soff + 1);
11748905a67cSAndy Whitcroft
11758905a67cSAndy Whitcroft	#warn "STATEMENT<$statement>\n";
11768905a67cSAndy Whitcroft	#warn "CONDITION<$condition>\n";
11778905a67cSAndy Whitcroft
1178773647a0SAndy Whitcroft	#print "coff<$coff> soff<$off> loff<$loff>\n";
117913214adfSAndy Whitcroft
118013214adfSAndy Whitcroft	return ($statement, $condition,
118113214adfSAndy Whitcroft			$line, $remain + 1, $off - $loff + 1, $level);
118213214adfSAndy Whitcroft}
118313214adfSAndy Whitcroft
1184cf655043SAndy Whitcroftsub statement_lines {
1185cf655043SAndy Whitcroft	my ($stmt) = @_;
1186cf655043SAndy Whitcroft
1187cf655043SAndy Whitcroft	# Strip the diff line prefixes and rip blank lines at start and end.
1188cf655043SAndy Whitcroft	$stmt =~ s/(^|\n)./$1/g;
1189cf655043SAndy Whitcroft	$stmt =~ s/^\s*//;
1190cf655043SAndy Whitcroft	$stmt =~ s/\s*$//;
1191cf655043SAndy Whitcroft
1192cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
1193cf655043SAndy Whitcroft
1194cf655043SAndy Whitcroft	return $#stmt_lines + 2;
1195cf655043SAndy Whitcroft}
1196cf655043SAndy Whitcroft
1197cf655043SAndy Whitcroftsub statement_rawlines {
1198cf655043SAndy Whitcroft	my ($stmt) = @_;
1199cf655043SAndy Whitcroft
1200cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
1201cf655043SAndy Whitcroft
1202cf655043SAndy Whitcroft	return $#stmt_lines + 2;
1203cf655043SAndy Whitcroft}
1204cf655043SAndy Whitcroft
1205cf655043SAndy Whitcroftsub statement_block_size {
1206cf655043SAndy Whitcroft	my ($stmt) = @_;
1207cf655043SAndy Whitcroft
1208cf655043SAndy Whitcroft	$stmt =~ s/(^|\n)./$1/g;
1209cf655043SAndy Whitcroft	$stmt =~ s/^\s*{//;
1210cf655043SAndy Whitcroft	$stmt =~ s/}\s*$//;
1211cf655043SAndy Whitcroft	$stmt =~ s/^\s*//;
1212cf655043SAndy Whitcroft	$stmt =~ s/\s*$//;
1213cf655043SAndy Whitcroft
1214cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
1215cf655043SAndy Whitcroft	my @stmt_statements = ($stmt =~ /;/g);
1216cf655043SAndy Whitcroft
1217cf655043SAndy Whitcroft	my $stmt_lines = $#stmt_lines + 2;
1218cf655043SAndy Whitcroft	my $stmt_statements = $#stmt_statements + 1;
1219cf655043SAndy Whitcroft
1220cf655043SAndy Whitcroft	if ($stmt_lines > $stmt_statements) {
1221cf655043SAndy Whitcroft		return $stmt_lines;
1222cf655043SAndy Whitcroft	} else {
1223cf655043SAndy Whitcroft		return $stmt_statements;
1224cf655043SAndy Whitcroft	}
1225cf655043SAndy Whitcroft}
1226cf655043SAndy Whitcroft
122713214adfSAndy Whitcroftsub ctx_statement_full {
122813214adfSAndy Whitcroft	my ($linenr, $remain, $off) = @_;
122913214adfSAndy Whitcroft	my ($statement, $condition, $level);
123013214adfSAndy Whitcroft
123113214adfSAndy Whitcroft	my (@chunks);
123213214adfSAndy Whitcroft
1233cf655043SAndy Whitcroft	# Grab the first conditional/block pair.
123413214adfSAndy Whitcroft	($statement, $condition, $linenr, $remain, $off, $level) =
123513214adfSAndy Whitcroft				ctx_statement_block($linenr, $remain, $off);
1236773647a0SAndy Whitcroft	#print "F: c<$condition> s<$statement> remain<$remain>\n";
123713214adfSAndy Whitcroft	push(@chunks, [ $condition, $statement ]);
1238cf655043SAndy Whitcroft	if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
1239cf655043SAndy Whitcroft		return ($level, $linenr, @chunks);
1240cf655043SAndy Whitcroft	}
1241cf655043SAndy Whitcroft
1242cf655043SAndy Whitcroft	# Pull in the following conditional/block pairs and see if they
1243cf655043SAndy Whitcroft	# could continue the statement.
1244cf655043SAndy Whitcroft	for (;;) {
124513214adfSAndy Whitcroft		($statement, $condition, $linenr, $remain, $off, $level) =
124613214adfSAndy Whitcroft				ctx_statement_block($linenr, $remain, $off);
1247cf655043SAndy Whitcroft		#print "C: c<$condition> s<$statement> remain<$remain>\n";
1248773647a0SAndy Whitcroft		last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
1249cf655043SAndy Whitcroft		#print "C: push\n";
1250cf655043SAndy Whitcroft		push(@chunks, [ $condition, $statement ]);
125113214adfSAndy Whitcroft	}
125213214adfSAndy Whitcroft
125313214adfSAndy Whitcroft	return ($level, $linenr, @chunks);
12548905a67cSAndy Whitcroft}
12558905a67cSAndy Whitcroft
12564a0df2efSAndy Whitcroftsub ctx_block_get {
1257f0a594c1SAndy Whitcroft	my ($linenr, $remain, $outer, $open, $close, $off) = @_;
12584a0df2efSAndy Whitcroft	my $line;
12594a0df2efSAndy Whitcroft	my $start = $linenr - 1;
12604a0df2efSAndy Whitcroft	my $blk = '';
12614a0df2efSAndy Whitcroft	my @o;
12624a0df2efSAndy Whitcroft	my @c;
12634a0df2efSAndy Whitcroft	my @res = ();
12644a0df2efSAndy Whitcroft
1265f0a594c1SAndy Whitcroft	my $level = 0;
12664635f4fbSAndy Whitcroft	my @stack = ($level);
126700df344fSAndy Whitcroft	for ($line = $start; $remain > 0; $line++) {
126800df344fSAndy Whitcroft		next if ($rawlines[$line] =~ /^-/);
126900df344fSAndy Whitcroft		$remain--;
127000df344fSAndy Whitcroft
127100df344fSAndy Whitcroft		$blk .= $rawlines[$line];
12724635f4fbSAndy Whitcroft
12734635f4fbSAndy Whitcroft		# Handle nested #if/#else.
127401464f30SAndy Whitcroft		if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
12754635f4fbSAndy Whitcroft			push(@stack, $level);
127601464f30SAndy Whitcroft		} elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
12774635f4fbSAndy Whitcroft			$level = $stack[$#stack - 1];
127801464f30SAndy Whitcroft		} elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
12794635f4fbSAndy Whitcroft			$level = pop(@stack);
12804635f4fbSAndy Whitcroft		}
12814635f4fbSAndy Whitcroft
128201464f30SAndy Whitcroft		foreach my $c (split(//, $lines[$line])) {
1283f0a594c1SAndy Whitcroft			##print "C<$c>L<$level><$open$close>O<$off>\n";
1284f0a594c1SAndy Whitcroft			if ($off > 0) {
1285f0a594c1SAndy Whitcroft				$off--;
1286f0a594c1SAndy Whitcroft				next;
1287f0a594c1SAndy Whitcroft			}
12884a0df2efSAndy Whitcroft
1289f0a594c1SAndy Whitcroft			if ($c eq $close && $level > 0) {
1290f0a594c1SAndy Whitcroft				$level--;
1291f0a594c1SAndy Whitcroft				last if ($level == 0);
1292f0a594c1SAndy Whitcroft			} elsif ($c eq $open) {
1293f0a594c1SAndy Whitcroft				$level++;
1294f0a594c1SAndy Whitcroft			}
1295f0a594c1SAndy Whitcroft		}
12964a0df2efSAndy Whitcroft
1297f0a594c1SAndy Whitcroft		if (!$outer || $level <= 1) {
129800df344fSAndy Whitcroft			push(@res, $rawlines[$line]);
12994a0df2efSAndy Whitcroft		}
13004a0df2efSAndy Whitcroft
1301f0a594c1SAndy Whitcroft		last if ($level == 0);
13024a0df2efSAndy Whitcroft	}
13034a0df2efSAndy Whitcroft
1304f0a594c1SAndy Whitcroft	return ($level, @res);
13054a0df2efSAndy Whitcroft}
13064a0df2efSAndy Whitcroftsub ctx_block_outer {
13074a0df2efSAndy Whitcroft	my ($linenr, $remain) = @_;
13084a0df2efSAndy Whitcroft
1309f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
1310f0a594c1SAndy Whitcroft	return @r;
13114a0df2efSAndy Whitcroft}
13124a0df2efSAndy Whitcroftsub ctx_block {
13134a0df2efSAndy Whitcroft	my ($linenr, $remain) = @_;
13144a0df2efSAndy Whitcroft
1315f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1316f0a594c1SAndy Whitcroft	return @r;
1317653d4876SAndy Whitcroft}
1318653d4876SAndy Whitcroftsub ctx_statement {
1319f0a594c1SAndy Whitcroft	my ($linenr, $remain, $off) = @_;
1320f0a594c1SAndy Whitcroft
1321f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1322f0a594c1SAndy Whitcroft	return @r;
1323f0a594c1SAndy Whitcroft}
1324f0a594c1SAndy Whitcroftsub ctx_block_level {
1325653d4876SAndy Whitcroft	my ($linenr, $remain) = @_;
1326653d4876SAndy Whitcroft
1327f0a594c1SAndy Whitcroft	return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
13284a0df2efSAndy Whitcroft}
13299c0ca6f9SAndy Whitcroftsub ctx_statement_level {
13309c0ca6f9SAndy Whitcroft	my ($linenr, $remain, $off) = @_;
13319c0ca6f9SAndy Whitcroft
13329c0ca6f9SAndy Whitcroft	return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
13339c0ca6f9SAndy Whitcroft}
13344a0df2efSAndy Whitcroft
13354a0df2efSAndy Whitcroftsub ctx_locate_comment {
13364a0df2efSAndy Whitcroft	my ($first_line, $end_line) = @_;
13374a0df2efSAndy Whitcroft
13384a0df2efSAndy Whitcroft	# Catch a comment on the end of the line itself.
1339beae6332SAndy Whitcroft	my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
13404a0df2efSAndy Whitcroft	return $current_comment if (defined $current_comment);
13414a0df2efSAndy Whitcroft
13424a0df2efSAndy Whitcroft	# Look through the context and try and figure out if there is a
13434a0df2efSAndy Whitcroft	# comment.
13444a0df2efSAndy Whitcroft	my $in_comment = 0;
13454a0df2efSAndy Whitcroft	$current_comment = '';
13464a0df2efSAndy Whitcroft	for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
134700df344fSAndy Whitcroft		my $line = $rawlines[$linenr - 1];
134800df344fSAndy Whitcroft		#warn "           $line\n";
13494a0df2efSAndy Whitcroft		if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
13504a0df2efSAndy Whitcroft			$in_comment = 1;
13514a0df2efSAndy Whitcroft		}
13524a0df2efSAndy Whitcroft		if ($line =~ m@/\*@) {
13534a0df2efSAndy Whitcroft			$in_comment = 1;
13544a0df2efSAndy Whitcroft		}
13554a0df2efSAndy Whitcroft		if (!$in_comment && $current_comment ne '') {
13564a0df2efSAndy Whitcroft			$current_comment = '';
13574a0df2efSAndy Whitcroft		}
13584a0df2efSAndy Whitcroft		$current_comment .= $line . "\n" if ($in_comment);
13594a0df2efSAndy Whitcroft		if ($line =~ m@\*/@) {
13604a0df2efSAndy Whitcroft			$in_comment = 0;
13614a0df2efSAndy Whitcroft		}
13624a0df2efSAndy Whitcroft	}
13634a0df2efSAndy Whitcroft
13644a0df2efSAndy Whitcroft	chomp($current_comment);
13654a0df2efSAndy Whitcroft	return($current_comment);
13664a0df2efSAndy Whitcroft}
13674a0df2efSAndy Whitcroftsub ctx_has_comment {
13684a0df2efSAndy Whitcroft	my ($first_line, $end_line) = @_;
13694a0df2efSAndy Whitcroft	my $cmt = ctx_locate_comment($first_line, $end_line);
13704a0df2efSAndy Whitcroft
137100df344fSAndy Whitcroft	##print "LINE: $rawlines[$end_line - 1 ]\n";
13724a0df2efSAndy Whitcroft	##print "CMMT: $cmt\n";
13734a0df2efSAndy Whitcroft
13744a0df2efSAndy Whitcroft	return ($cmt ne '');
13754a0df2efSAndy Whitcroft}
13764a0df2efSAndy Whitcroft
13774d001e4dSAndy Whitcroftsub raw_line {
13784d001e4dSAndy Whitcroft	my ($linenr, $cnt) = @_;
13794d001e4dSAndy Whitcroft
13804d001e4dSAndy Whitcroft	my $offset = $linenr - 1;
13814d001e4dSAndy Whitcroft	$cnt++;
13824d001e4dSAndy Whitcroft
13834d001e4dSAndy Whitcroft	my $line;
13844d001e4dSAndy Whitcroft	while ($cnt) {
13854d001e4dSAndy Whitcroft		$line = $rawlines[$offset++];
13864d001e4dSAndy Whitcroft		next if (defined($line) && $line =~ /^-/);
13874d001e4dSAndy Whitcroft		$cnt--;
13884d001e4dSAndy Whitcroft	}
13894d001e4dSAndy Whitcroft
13904d001e4dSAndy Whitcroft	return $line;
13914d001e4dSAndy Whitcroft}
13924d001e4dSAndy Whitcroft
13930a920b5bSAndy Whitcroftsub cat_vet {
13940a920b5bSAndy Whitcroft	my ($vet) = @_;
13959c0ca6f9SAndy Whitcroft	my ($res, $coded);
13960a920b5bSAndy Whitcroft
13979c0ca6f9SAndy Whitcroft	$res = '';
13986c72ffaaSAndy Whitcroft	while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
13996c72ffaaSAndy Whitcroft		$res .= $1;
14006c72ffaaSAndy Whitcroft		if ($2 ne '') {
14019c0ca6f9SAndy Whitcroft			$coded = sprintf("^%c", unpack('C', $2) + 64);
14026c72ffaaSAndy Whitcroft			$res .= $coded;
14036c72ffaaSAndy Whitcroft		}
14049c0ca6f9SAndy Whitcroft	}
14059c0ca6f9SAndy Whitcroft	$res =~ s/$/\$/;
14060a920b5bSAndy Whitcroft
14079c0ca6f9SAndy Whitcroft	return $res;
14080a920b5bSAndy Whitcroft}
14090a920b5bSAndy Whitcroft
1410c2fdda0dSAndy Whitcroftmy $av_preprocessor = 0;
1411cf655043SAndy Whitcroftmy $av_pending;
1412c2fdda0dSAndy Whitcroftmy @av_paren_type;
14131f65f947SAndy Whitcroftmy $av_pend_colon;
1414c2fdda0dSAndy Whitcroft
1415c2fdda0dSAndy Whitcroftsub annotate_reset {
1416c2fdda0dSAndy Whitcroft	$av_preprocessor = 0;
1417cf655043SAndy Whitcroft	$av_pending = '_';
1418cf655043SAndy Whitcroft	@av_paren_type = ('E');
14191f65f947SAndy Whitcroft	$av_pend_colon = 'O';
1420c2fdda0dSAndy Whitcroft}
1421c2fdda0dSAndy Whitcroft
14226c72ffaaSAndy Whitcroftsub annotate_values {
14236c72ffaaSAndy Whitcroft	my ($stream, $type) = @_;
14246c72ffaaSAndy Whitcroft
14256c72ffaaSAndy Whitcroft	my $res;
14261f65f947SAndy Whitcroft	my $var = '_' x length($stream);
14276c72ffaaSAndy Whitcroft	my $cur = $stream;
14286c72ffaaSAndy Whitcroft
1429c2fdda0dSAndy Whitcroft	print "$stream\n" if ($dbg_values > 1);
14306c72ffaaSAndy Whitcroft
14316c72ffaaSAndy Whitcroft	while (length($cur)) {
1432773647a0SAndy Whitcroft		@av_paren_type = ('E') if ($#av_paren_type < 0);
1433cf655043SAndy Whitcroft		print " <" . join('', @av_paren_type) .
1434171ae1a4SAndy Whitcroft				"> <$type> <$av_pending>" if ($dbg_values > 1);
14356c72ffaaSAndy Whitcroft		if ($cur =~ /^(\s+)/o) {
1436c2fdda0dSAndy Whitcroft			print "WS($1)\n" if ($dbg_values > 1);
1437c2fdda0dSAndy Whitcroft			if ($1 =~ /\n/ && $av_preprocessor) {
1438cf655043SAndy Whitcroft				$type = pop(@av_paren_type);
1439c2fdda0dSAndy Whitcroft				$av_preprocessor = 0;
14406c72ffaaSAndy Whitcroft			}
14416c72ffaaSAndy Whitcroft
1442c023e473SFlorian Mickler		} elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
14439446ef56SAndy Whitcroft			print "CAST($1)\n" if ($dbg_values > 1);
14449446ef56SAndy Whitcroft			push(@av_paren_type, $type);
1445addcdceaSAndy Whitcroft			$type = 'c';
14469446ef56SAndy Whitcroft
1447e91b6e26SAndy Whitcroft		} elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
1448c2fdda0dSAndy Whitcroft			print "DECLARE($1)\n" if ($dbg_values > 1);
14496c72ffaaSAndy Whitcroft			$type = 'T';
14506c72ffaaSAndy Whitcroft
1451389a2fe5SAndy Whitcroft		} elsif ($cur =~ /^($Modifier)\s*/) {
1452389a2fe5SAndy Whitcroft			print "MODIFIER($1)\n" if ($dbg_values > 1);
1453389a2fe5SAndy Whitcroft			$type = 'T';
1454389a2fe5SAndy Whitcroft
1455c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
1456171ae1a4SAndy Whitcroft			print "DEFINE($1,$2)\n" if ($dbg_values > 1);
1457c2fdda0dSAndy Whitcroft			$av_preprocessor = 1;
1458171ae1a4SAndy Whitcroft			push(@av_paren_type, $type);
1459171ae1a4SAndy Whitcroft			if ($2 ne '') {
1460cf655043SAndy Whitcroft				$av_pending = 'N';
1461171ae1a4SAndy Whitcroft			}
1462171ae1a4SAndy Whitcroft			$type = 'E';
1463171ae1a4SAndy Whitcroft
1464c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
1465171ae1a4SAndy Whitcroft			print "UNDEF($1)\n" if ($dbg_values > 1);
1466171ae1a4SAndy Whitcroft			$av_preprocessor = 1;
1467171ae1a4SAndy Whitcroft			push(@av_paren_type, $type);
14686c72ffaaSAndy Whitcroft
1469c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
1470cf655043SAndy Whitcroft			print "PRE_START($1)\n" if ($dbg_values > 1);
1471c2fdda0dSAndy Whitcroft			$av_preprocessor = 1;
1472cf655043SAndy Whitcroft
1473cf655043SAndy Whitcroft			push(@av_paren_type, $type);
1474cf655043SAndy Whitcroft			push(@av_paren_type, $type);
1475171ae1a4SAndy Whitcroft			$type = 'E';
1476cf655043SAndy Whitcroft
1477c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
1478cf655043SAndy Whitcroft			print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1479cf655043SAndy Whitcroft			$av_preprocessor = 1;
1480cf655043SAndy Whitcroft
1481cf655043SAndy Whitcroft			push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1482cf655043SAndy Whitcroft
1483171ae1a4SAndy Whitcroft			$type = 'E';
1484cf655043SAndy Whitcroft
1485c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:endif))/o) {
1486cf655043SAndy Whitcroft			print "PRE_END($1)\n" if ($dbg_values > 1);
1487cf655043SAndy Whitcroft
1488cf655043SAndy Whitcroft			$av_preprocessor = 1;
1489cf655043SAndy Whitcroft
1490cf655043SAndy Whitcroft			# Assume all arms of the conditional end as this
1491cf655043SAndy Whitcroft			# one does, and continue as if the #endif was not here.
1492cf655043SAndy Whitcroft			pop(@av_paren_type);
1493cf655043SAndy Whitcroft			push(@av_paren_type, $type);
1494171ae1a4SAndy Whitcroft			$type = 'E';
14956c72ffaaSAndy Whitcroft
14966c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\\\n)/o) {
1497c2fdda0dSAndy Whitcroft			print "PRECONT($1)\n" if ($dbg_values > 1);
14986c72ffaaSAndy Whitcroft
1499171ae1a4SAndy Whitcroft		} elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1500171ae1a4SAndy Whitcroft			print "ATTR($1)\n" if ($dbg_values > 1);
1501171ae1a4SAndy Whitcroft			$av_pending = $type;
1502171ae1a4SAndy Whitcroft			$type = 'N';
1503171ae1a4SAndy Whitcroft
15046c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
1505c2fdda0dSAndy Whitcroft			print "SIZEOF($1)\n" if ($dbg_values > 1);
15066c72ffaaSAndy Whitcroft			if (defined $2) {
1507cf655043SAndy Whitcroft				$av_pending = 'V';
15086c72ffaaSAndy Whitcroft			}
15096c72ffaaSAndy Whitcroft			$type = 'N';
15106c72ffaaSAndy Whitcroft
151114b111c1SAndy Whitcroft		} elsif ($cur =~ /^(if|while|for)\b/o) {
1512c2fdda0dSAndy Whitcroft			print "COND($1)\n" if ($dbg_values > 1);
151314b111c1SAndy Whitcroft			$av_pending = 'E';
15146c72ffaaSAndy Whitcroft			$type = 'N';
15156c72ffaaSAndy Whitcroft
15161f65f947SAndy Whitcroft		} elsif ($cur =~/^(case)/o) {
15171f65f947SAndy Whitcroft			print "CASE($1)\n" if ($dbg_values > 1);
15181f65f947SAndy Whitcroft			$av_pend_colon = 'C';
15191f65f947SAndy Whitcroft			$type = 'N';
15201f65f947SAndy Whitcroft
152114b111c1SAndy Whitcroft		} elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
1522c2fdda0dSAndy Whitcroft			print "KEYWORD($1)\n" if ($dbg_values > 1);
15236c72ffaaSAndy Whitcroft			$type = 'N';
15246c72ffaaSAndy Whitcroft
15256c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\()/o) {
1526c2fdda0dSAndy Whitcroft			print "PAREN('$1')\n" if ($dbg_values > 1);
1527cf655043SAndy Whitcroft			push(@av_paren_type, $av_pending);
1528cf655043SAndy Whitcroft			$av_pending = '_';
15296c72ffaaSAndy Whitcroft			$type = 'N';
15306c72ffaaSAndy Whitcroft
15316c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\))/o) {
1532cf655043SAndy Whitcroft			my $new_type = pop(@av_paren_type);
1533cf655043SAndy Whitcroft			if ($new_type ne '_') {
1534cf655043SAndy Whitcroft				$type = $new_type;
1535c2fdda0dSAndy Whitcroft				print "PAREN('$1') -> $type\n"
1536c2fdda0dSAndy Whitcroft							if ($dbg_values > 1);
15376c72ffaaSAndy Whitcroft			} else {
1538c2fdda0dSAndy Whitcroft				print "PAREN('$1')\n" if ($dbg_values > 1);
15396c72ffaaSAndy Whitcroft			}
15406c72ffaaSAndy Whitcroft
1541c8cb2ca3SAndy Whitcroft		} elsif ($cur =~ /^($Ident)\s*\(/o) {
1542c2fdda0dSAndy Whitcroft			print "FUNC($1)\n" if ($dbg_values > 1);
1543c8cb2ca3SAndy Whitcroft			$type = 'V';
1544cf655043SAndy Whitcroft			$av_pending = 'V';
15456c72ffaaSAndy Whitcroft
15468e761b04SAndy Whitcroft		} elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
15478e761b04SAndy Whitcroft			if (defined $2 && $type eq 'C' || $type eq 'T') {
15481f65f947SAndy Whitcroft				$av_pend_colon = 'B';
15498e761b04SAndy Whitcroft			} elsif ($type eq 'E') {
15508e761b04SAndy Whitcroft				$av_pend_colon = 'L';
15511f65f947SAndy Whitcroft			}
15521f65f947SAndy Whitcroft			print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
15531f65f947SAndy Whitcroft			$type = 'V';
15541f65f947SAndy Whitcroft
15556c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Ident|$Constant)/o) {
1556c2fdda0dSAndy Whitcroft			print "IDENT($1)\n" if ($dbg_values > 1);
15576c72ffaaSAndy Whitcroft			$type = 'V';
15586c72ffaaSAndy Whitcroft
15596c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Assignment)/o) {
1560c2fdda0dSAndy Whitcroft			print "ASSIGN($1)\n" if ($dbg_values > 1);
15616c72ffaaSAndy Whitcroft			$type = 'N';
15626c72ffaaSAndy Whitcroft
1563cf655043SAndy Whitcroft		} elsif ($cur =~/^(;|{|})/) {
1564c2fdda0dSAndy Whitcroft			print "END($1)\n" if ($dbg_values > 1);
156513214adfSAndy Whitcroft			$type = 'E';
15661f65f947SAndy Whitcroft			$av_pend_colon = 'O';
156713214adfSAndy Whitcroft
15688e761b04SAndy Whitcroft		} elsif ($cur =~/^(,)/) {
15698e761b04SAndy Whitcroft			print "COMMA($1)\n" if ($dbg_values > 1);
15708e761b04SAndy Whitcroft			$type = 'C';
15718e761b04SAndy Whitcroft
15721f65f947SAndy Whitcroft		} elsif ($cur =~ /^(\?)/o) {
15731f65f947SAndy Whitcroft			print "QUESTION($1)\n" if ($dbg_values > 1);
15741f65f947SAndy Whitcroft			$type = 'N';
15751f65f947SAndy Whitcroft
15761f65f947SAndy Whitcroft		} elsif ($cur =~ /^(:)/o) {
15771f65f947SAndy Whitcroft			print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
15781f65f947SAndy Whitcroft
15791f65f947SAndy Whitcroft			substr($var, length($res), 1, $av_pend_colon);
15801f65f947SAndy Whitcroft			if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
15811f65f947SAndy Whitcroft				$type = 'E';
15821f65f947SAndy Whitcroft			} else {
15831f65f947SAndy Whitcroft				$type = 'N';
15841f65f947SAndy Whitcroft			}
15851f65f947SAndy Whitcroft			$av_pend_colon = 'O';
15861f65f947SAndy Whitcroft
15878e761b04SAndy Whitcroft		} elsif ($cur =~ /^(\[)/o) {
158813214adfSAndy Whitcroft			print "CLOSE($1)\n" if ($dbg_values > 1);
15896c72ffaaSAndy Whitcroft			$type = 'N';
15906c72ffaaSAndy Whitcroft
15910d413866SAndy Whitcroft		} elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
159274048ed8SAndy Whitcroft			my $variant;
159374048ed8SAndy Whitcroft
159474048ed8SAndy Whitcroft			print "OPV($1)\n" if ($dbg_values > 1);
159574048ed8SAndy Whitcroft			if ($type eq 'V') {
159674048ed8SAndy Whitcroft				$variant = 'B';
159774048ed8SAndy Whitcroft			} else {
159874048ed8SAndy Whitcroft				$variant = 'U';
159974048ed8SAndy Whitcroft			}
160074048ed8SAndy Whitcroft
160174048ed8SAndy Whitcroft			substr($var, length($res), 1, $variant);
160274048ed8SAndy Whitcroft			$type = 'N';
160374048ed8SAndy Whitcroft
16046c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Operators)/o) {
1605c2fdda0dSAndy Whitcroft			print "OP($1)\n" if ($dbg_values > 1);
16066c72ffaaSAndy Whitcroft			if ($1 ne '++' && $1 ne '--') {
16076c72ffaaSAndy Whitcroft				$type = 'N';
16086c72ffaaSAndy Whitcroft			}
16096c72ffaaSAndy Whitcroft
16106c72ffaaSAndy Whitcroft		} elsif ($cur =~ /(^.)/o) {
1611c2fdda0dSAndy Whitcroft			print "C($1)\n" if ($dbg_values > 1);
16126c72ffaaSAndy Whitcroft		}
16136c72ffaaSAndy Whitcroft		if (defined $1) {
16146c72ffaaSAndy Whitcroft			$cur = substr($cur, length($1));
16156c72ffaaSAndy Whitcroft			$res .= $type x length($1);
16166c72ffaaSAndy Whitcroft		}
16176c72ffaaSAndy Whitcroft	}
16186c72ffaaSAndy Whitcroft
16191f65f947SAndy Whitcroft	return ($res, $var);
16206c72ffaaSAndy Whitcroft}
16216c72ffaaSAndy Whitcroft
16228905a67cSAndy Whitcroftsub possible {
162313214adfSAndy Whitcroft	my ($possible, $line) = @_;
16249a974fdbSAndy Whitcroft	my $notPermitted = qr{(?:
16250776e594SAndy Whitcroft		^(?:
16260776e594SAndy Whitcroft			$Modifier|
16270776e594SAndy Whitcroft			$Storage|
16280776e594SAndy Whitcroft			$Type|
16299a974fdbSAndy Whitcroft			DEFINE_\S+
16309a974fdbSAndy Whitcroft		)$|
16319a974fdbSAndy Whitcroft		^(?:
16320776e594SAndy Whitcroft			goto|
16330776e594SAndy Whitcroft			return|
16340776e594SAndy Whitcroft			case|
16350776e594SAndy Whitcroft			else|
16360776e594SAndy Whitcroft			asm|__asm__|
163789a88353SAndy Whitcroft			do|
163889a88353SAndy Whitcroft			\#|
163989a88353SAndy Whitcroft			\#\#|
16409a974fdbSAndy Whitcroft		)(?:\s|$)|
16410776e594SAndy Whitcroft		^(?:typedef|struct|enum)\b
16429a974fdbSAndy Whitcroft	    )}x;
16439a974fdbSAndy Whitcroft	warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
16449a974fdbSAndy Whitcroft	if ($possible !~ $notPermitted) {
1645c45dcabdSAndy Whitcroft		# Check for modifiers.
1646c45dcabdSAndy Whitcroft		$possible =~ s/\s*$Storage\s*//g;
1647c45dcabdSAndy Whitcroft		$possible =~ s/\s*$Sparse\s*//g;
1648c45dcabdSAndy Whitcroft		if ($possible =~ /^\s*$/) {
1649c45dcabdSAndy Whitcroft
1650c45dcabdSAndy Whitcroft		} elsif ($possible =~ /\s/) {
1651c45dcabdSAndy Whitcroft			$possible =~ s/\s*$Type\s*//g;
1652d2506586SAndy Whitcroft			for my $modifier (split(' ', $possible)) {
16539a974fdbSAndy Whitcroft				if ($modifier !~ $notPermitted) {
1654d2506586SAndy Whitcroft					warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1655485ff23eSAlex Dowad					push(@modifierListFile, $modifier);
1656d2506586SAndy Whitcroft				}
16579a974fdbSAndy Whitcroft			}
1658c45dcabdSAndy Whitcroft
1659c45dcabdSAndy Whitcroft		} else {
166013214adfSAndy Whitcroft			warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
1661485ff23eSAlex Dowad			push(@typeListFile, $possible);
1662c45dcabdSAndy Whitcroft		}
16638905a67cSAndy Whitcroft		build_types();
16640776e594SAndy Whitcroft	} else {
16650776e594SAndy Whitcroft		warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
16668905a67cSAndy Whitcroft	}
16678905a67cSAndy Whitcroft}
16688905a67cSAndy Whitcroft
16696c72ffaaSAndy Whitcroftmy $prefix = '';
16706c72ffaaSAndy Whitcroft
1671000d1cc1SJoe Perchessub show_type {
1672cbec18afSJoe Perches	my ($type) = @_;
167391bfe484SJoe Perches
1674cbec18afSJoe Perches	return defined $use_type{$type} if (scalar keys %use_type > 0);
1675cbec18afSJoe Perches
1676cbec18afSJoe Perches	return !defined $ignore_type{$type};
1677000d1cc1SJoe Perches}
1678000d1cc1SJoe Perches
1679f0a594c1SAndy Whitcroftsub report {
1680cbec18afSJoe Perches	my ($level, $type, $msg) = @_;
1681cbec18afSJoe Perches
1682cbec18afSJoe Perches	if (!show_type($type) ||
1683cbec18afSJoe Perches	    (defined $tst_only && $msg !~ /\Q$tst_only\E/)) {
1684773647a0SAndy Whitcroft		return 0;
1685773647a0SAndy Whitcroft	}
168657230297SJoe Perches	my $output = '';
168757230297SJoe Perches	if (-t STDOUT && $color) {
168857230297SJoe Perches		if ($level eq 'ERROR') {
168957230297SJoe Perches			$output .= RED;
169057230297SJoe Perches		} elsif ($level eq 'WARNING') {
169157230297SJoe Perches			$output .= YELLOW;
1692000d1cc1SJoe Perches		} else {
169357230297SJoe Perches			$output .= GREEN;
1694000d1cc1SJoe Perches		}
169557230297SJoe Perches	}
169657230297SJoe Perches	$output .= $prefix . $level . ':';
169757230297SJoe Perches	if ($show_types) {
169857230297SJoe Perches		$output .= BLUE if (-t STDOUT && $color);
169957230297SJoe Perches		$output .= "$type:";
170057230297SJoe Perches	}
170157230297SJoe Perches	$output .= RESET if (-t STDOUT && $color);
170257230297SJoe Perches	$output .= ' ' . $msg . "\n";
170334d8815fSJoe Perches
170434d8815fSJoe Perches	if ($showfile) {
170534d8815fSJoe Perches		my @lines = split("\n", $output, -1);
170634d8815fSJoe Perches		splice(@lines, 1, 1);
170734d8815fSJoe Perches		$output = join("\n", @lines);
170834d8815fSJoe Perches	}
170957230297SJoe Perches	$output = (split('\n', $output))[0] . "\n" if ($terse);
17108905a67cSAndy Whitcroft
171157230297SJoe Perches	push(our @report, $output);
1712773647a0SAndy Whitcroft
1713773647a0SAndy Whitcroft	return 1;
1714f0a594c1SAndy Whitcroft}
1715cbec18afSJoe Perches
1716f0a594c1SAndy Whitcroftsub report_dump {
171713214adfSAndy Whitcroft	our @report;
1718f0a594c1SAndy Whitcroft}
1719000d1cc1SJoe Perches
1720d752fcc8SJoe Perchessub fixup_current_range {
1721d752fcc8SJoe Perches	my ($lineRef, $offset, $length) = @_;
1722d752fcc8SJoe Perches
1723d752fcc8SJoe Perches	if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) {
1724d752fcc8SJoe Perches		my $o = $1;
1725d752fcc8SJoe Perches		my $l = $2;
1726d752fcc8SJoe Perches		my $no = $o + $offset;
1727d752fcc8SJoe Perches		my $nl = $l + $length;
1728d752fcc8SJoe Perches		$$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/;
1729d752fcc8SJoe Perches	}
1730d752fcc8SJoe Perches}
1731d752fcc8SJoe Perches
1732d752fcc8SJoe Perchessub fix_inserted_deleted_lines {
1733d752fcc8SJoe Perches	my ($linesRef, $insertedRef, $deletedRef) = @_;
1734d752fcc8SJoe Perches
1735d752fcc8SJoe Perches	my $range_last_linenr = 0;
1736d752fcc8SJoe Perches	my $delta_offset = 0;
1737d752fcc8SJoe Perches
1738d752fcc8SJoe Perches	my $old_linenr = 0;
1739d752fcc8SJoe Perches	my $new_linenr = 0;
1740d752fcc8SJoe Perches
1741d752fcc8SJoe Perches	my $next_insert = 0;
1742d752fcc8SJoe Perches	my $next_delete = 0;
1743d752fcc8SJoe Perches
1744d752fcc8SJoe Perches	my @lines = ();
1745d752fcc8SJoe Perches
1746d752fcc8SJoe Perches	my $inserted = @{$insertedRef}[$next_insert++];
1747d752fcc8SJoe Perches	my $deleted = @{$deletedRef}[$next_delete++];
1748d752fcc8SJoe Perches
1749d752fcc8SJoe Perches	foreach my $old_line (@{$linesRef}) {
1750d752fcc8SJoe Perches		my $save_line = 1;
1751d752fcc8SJoe Perches		my $line = $old_line;	#don't modify the array
1752323b267fSJoe Perches		if ($line =~ /^(?:\+\+\+|\-\-\-)\s+\S+/) {	#new filename
1753d752fcc8SJoe Perches			$delta_offset = 0;
1754d752fcc8SJoe Perches		} elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) {	#new hunk
1755d752fcc8SJoe Perches			$range_last_linenr = $new_linenr;
1756d752fcc8SJoe Perches			fixup_current_range(\$line, $delta_offset, 0);
1757d752fcc8SJoe Perches		}
1758d752fcc8SJoe Perches
1759d752fcc8SJoe Perches		while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) {
1760d752fcc8SJoe Perches			$deleted = @{$deletedRef}[$next_delete++];
1761d752fcc8SJoe Perches			$save_line = 0;
1762d752fcc8SJoe Perches			fixup_current_range(\$lines[$range_last_linenr], $delta_offset--, -1);
1763d752fcc8SJoe Perches		}
1764d752fcc8SJoe Perches
1765d752fcc8SJoe Perches		while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) {
1766d752fcc8SJoe Perches			push(@lines, ${$inserted}{'LINE'});
1767d752fcc8SJoe Perches			$inserted = @{$insertedRef}[$next_insert++];
1768d752fcc8SJoe Perches			$new_linenr++;
1769d752fcc8SJoe Perches			fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1);
1770d752fcc8SJoe Perches		}
1771d752fcc8SJoe Perches
1772d752fcc8SJoe Perches		if ($save_line) {
1773d752fcc8SJoe Perches			push(@lines, $line);
1774d752fcc8SJoe Perches			$new_linenr++;
1775d752fcc8SJoe Perches		}
1776d752fcc8SJoe Perches
1777d752fcc8SJoe Perches		$old_linenr++;
1778d752fcc8SJoe Perches	}
1779d752fcc8SJoe Perches
1780d752fcc8SJoe Perches	return @lines;
1781d752fcc8SJoe Perches}
1782d752fcc8SJoe Perches
1783f2d7e4d4SJoe Perchessub fix_insert_line {
1784f2d7e4d4SJoe Perches	my ($linenr, $line) = @_;
1785f2d7e4d4SJoe Perches
1786f2d7e4d4SJoe Perches	my $inserted = {
1787f2d7e4d4SJoe Perches		LINENR => $linenr,
1788f2d7e4d4SJoe Perches		LINE => $line,
1789f2d7e4d4SJoe Perches	};
1790f2d7e4d4SJoe Perches	push(@fixed_inserted, $inserted);
1791f2d7e4d4SJoe Perches}
1792f2d7e4d4SJoe Perches
1793f2d7e4d4SJoe Perchessub fix_delete_line {
1794f2d7e4d4SJoe Perches	my ($linenr, $line) = @_;
1795f2d7e4d4SJoe Perches
1796f2d7e4d4SJoe Perches	my $deleted = {
1797f2d7e4d4SJoe Perches		LINENR => $linenr,
1798f2d7e4d4SJoe Perches		LINE => $line,
1799f2d7e4d4SJoe Perches	};
1800f2d7e4d4SJoe Perches
1801f2d7e4d4SJoe Perches	push(@fixed_deleted, $deleted);
1802f2d7e4d4SJoe Perches}
1803f2d7e4d4SJoe Perches
1804de7d4f0eSAndy Whitcroftsub ERROR {
1805cbec18afSJoe Perches	my ($type, $msg) = @_;
1806cbec18afSJoe Perches
1807cbec18afSJoe Perches	if (report("ERROR", $type, $msg)) {
1808de7d4f0eSAndy Whitcroft		our $clean = 0;
18096c72ffaaSAndy Whitcroft		our $cnt_error++;
18103705ce5bSJoe Perches		return 1;
1811de7d4f0eSAndy Whitcroft	}
18123705ce5bSJoe Perches	return 0;
1813773647a0SAndy Whitcroft}
1814de7d4f0eSAndy Whitcroftsub WARN {
1815cbec18afSJoe Perches	my ($type, $msg) = @_;
1816cbec18afSJoe Perches
1817cbec18afSJoe Perches	if (report("WARNING", $type, $msg)) {
1818de7d4f0eSAndy Whitcroft		our $clean = 0;
18196c72ffaaSAndy Whitcroft		our $cnt_warn++;
18203705ce5bSJoe Perches		return 1;
1821de7d4f0eSAndy Whitcroft	}
18223705ce5bSJoe Perches	return 0;
1823773647a0SAndy Whitcroft}
1824de7d4f0eSAndy Whitcroftsub CHK {
1825cbec18afSJoe Perches	my ($type, $msg) = @_;
1826cbec18afSJoe Perches
1827cbec18afSJoe Perches	if ($check && report("CHECK", $type, $msg)) {
1828de7d4f0eSAndy Whitcroft		our $clean = 0;
18296c72ffaaSAndy Whitcroft		our $cnt_chk++;
18303705ce5bSJoe Perches		return 1;
18316c72ffaaSAndy Whitcroft	}
18323705ce5bSJoe Perches	return 0;
1833de7d4f0eSAndy Whitcroft}
1834de7d4f0eSAndy Whitcroft
18356ecd9674SAndy Whitcroftsub check_absolute_file {
18366ecd9674SAndy Whitcroft	my ($absolute, $herecurr) = @_;
18376ecd9674SAndy Whitcroft	my $file = $absolute;
18386ecd9674SAndy Whitcroft
18396ecd9674SAndy Whitcroft	##print "absolute<$absolute>\n";
18406ecd9674SAndy Whitcroft
18416ecd9674SAndy Whitcroft	# See if any suffix of this path is a path within the tree.
18426ecd9674SAndy Whitcroft	while ($file =~ s@^[^/]*/@@) {
18436ecd9674SAndy Whitcroft		if (-f "$root/$file") {
18446ecd9674SAndy Whitcroft			##print "file<$file>\n";
18456ecd9674SAndy Whitcroft			last;
18466ecd9674SAndy Whitcroft		}
18476ecd9674SAndy Whitcroft	}
18486ecd9674SAndy Whitcroft	if (! -f _)  {
18496ecd9674SAndy Whitcroft		return 0;
18506ecd9674SAndy Whitcroft	}
18516ecd9674SAndy Whitcroft
18526ecd9674SAndy Whitcroft	# It is, so see if the prefix is acceptable.
18536ecd9674SAndy Whitcroft	my $prefix = $absolute;
18546ecd9674SAndy Whitcroft	substr($prefix, -length($file)) = '';
18556ecd9674SAndy Whitcroft
18566ecd9674SAndy Whitcroft	##print "prefix<$prefix>\n";
18576ecd9674SAndy Whitcroft	if ($prefix ne ".../") {
1858000d1cc1SJoe Perches		WARN("USE_RELATIVE_PATH",
1859000d1cc1SJoe Perches		     "use relative pathname instead of absolute in changelog text\n" . $herecurr);
18606ecd9674SAndy Whitcroft	}
18616ecd9674SAndy Whitcroft}
18626ecd9674SAndy Whitcroft
18633705ce5bSJoe Perchessub trim {
18643705ce5bSJoe Perches	my ($string) = @_;
18653705ce5bSJoe Perches
1866b34c648bSJoe Perches	$string =~ s/^\s+|\s+$//g;
1867b34c648bSJoe Perches
1868b34c648bSJoe Perches	return $string;
1869b34c648bSJoe Perches}
1870b34c648bSJoe Perches
1871b34c648bSJoe Perchessub ltrim {
1872b34c648bSJoe Perches	my ($string) = @_;
1873b34c648bSJoe Perches
1874b34c648bSJoe Perches	$string =~ s/^\s+//;
1875b34c648bSJoe Perches
1876b34c648bSJoe Perches	return $string;
1877b34c648bSJoe Perches}
1878b34c648bSJoe Perches
1879b34c648bSJoe Perchessub rtrim {
1880b34c648bSJoe Perches	my ($string) = @_;
1881b34c648bSJoe Perches
1882b34c648bSJoe Perches	$string =~ s/\s+$//;
18833705ce5bSJoe Perches
18843705ce5bSJoe Perches	return $string;
18853705ce5bSJoe Perches}
18863705ce5bSJoe Perches
188752ea8506SJoe Perchessub string_find_replace {
188852ea8506SJoe Perches	my ($string, $find, $replace) = @_;
188952ea8506SJoe Perches
189052ea8506SJoe Perches	$string =~ s/$find/$replace/g;
189152ea8506SJoe Perches
189252ea8506SJoe Perches	return $string;
189352ea8506SJoe Perches}
189452ea8506SJoe Perches
18953705ce5bSJoe Perchessub tabify {
18963705ce5bSJoe Perches	my ($leading) = @_;
18973705ce5bSJoe Perches
18983705ce5bSJoe Perches	my $source_indent = 8;
18993705ce5bSJoe Perches	my $max_spaces_before_tab = $source_indent - 1;
19003705ce5bSJoe Perches	my $spaces_to_tab = " " x $source_indent;
19013705ce5bSJoe Perches
19023705ce5bSJoe Perches	#convert leading spaces to tabs
19033705ce5bSJoe Perches	1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
19043705ce5bSJoe Perches	#Remove spaces before a tab
19053705ce5bSJoe Perches	1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
19063705ce5bSJoe Perches
19073705ce5bSJoe Perches	return "$leading";
19083705ce5bSJoe Perches}
19093705ce5bSJoe Perches
1910d1fe9c09SJoe Perchessub pos_last_openparen {
1911d1fe9c09SJoe Perches	my ($line) = @_;
1912d1fe9c09SJoe Perches
1913d1fe9c09SJoe Perches	my $pos = 0;
1914d1fe9c09SJoe Perches
1915d1fe9c09SJoe Perches	my $opens = $line =~ tr/\(/\(/;
1916d1fe9c09SJoe Perches	my $closes = $line =~ tr/\)/\)/;
1917d1fe9c09SJoe Perches
1918d1fe9c09SJoe Perches	my $last_openparen = 0;
1919d1fe9c09SJoe Perches
1920d1fe9c09SJoe Perches	if (($opens == 0) || ($closes >= $opens)) {
1921d1fe9c09SJoe Perches		return -1;
1922d1fe9c09SJoe Perches	}
1923d1fe9c09SJoe Perches
1924d1fe9c09SJoe Perches	my $len = length($line);
1925d1fe9c09SJoe Perches
1926d1fe9c09SJoe Perches	for ($pos = 0; $pos < $len; $pos++) {
1927d1fe9c09SJoe Perches		my $string = substr($line, $pos);
1928d1fe9c09SJoe Perches		if ($string =~ /^($FuncArg|$balanced_parens)/) {
1929d1fe9c09SJoe Perches			$pos += length($1) - 1;
1930d1fe9c09SJoe Perches		} elsif (substr($line, $pos, 1) eq '(') {
1931d1fe9c09SJoe Perches			$last_openparen = $pos;
1932d1fe9c09SJoe Perches		} elsif (index($string, '(') == -1) {
1933d1fe9c09SJoe Perches			last;
1934d1fe9c09SJoe Perches		}
1935d1fe9c09SJoe Perches	}
1936d1fe9c09SJoe Perches
193791cb5195SJoe Perches	return length(expand_tabs(substr($line, 0, $last_openparen))) + 1;
1938d1fe9c09SJoe Perches}
1939d1fe9c09SJoe Perches
19400a920b5bSAndy Whitcroftsub process {
19410a920b5bSAndy Whitcroft	my $filename = shift;
19420a920b5bSAndy Whitcroft
19430a920b5bSAndy Whitcroft	my $linenr=0;
19440a920b5bSAndy Whitcroft	my $prevline="";
1945c2fdda0dSAndy Whitcroft	my $prevrawline="";
19460a920b5bSAndy Whitcroft	my $stashline="";
1947c2fdda0dSAndy Whitcroft	my $stashrawline="";
19480a920b5bSAndy Whitcroft
19494a0df2efSAndy Whitcroft	my $length;
19500a920b5bSAndy Whitcroft	my $indent;
19510a920b5bSAndy Whitcroft	my $previndent=0;
19520a920b5bSAndy Whitcroft	my $stashindent=0;
19530a920b5bSAndy Whitcroft
1954de7d4f0eSAndy Whitcroft	our $clean = 1;
19550a920b5bSAndy Whitcroft	my $signoff = 0;
19560a920b5bSAndy Whitcroft	my $is_patch = 0;
195729ee1b0cSJoe Perches	my $in_header_lines = $file ? 0 : 1;
195815662b3eSJoe Perches	my $in_commit_log = 0;		#Scanning lines before patch
1959bf4daf12SJoe Perches       my $commit_log_possible_stack_dump = 0;
19602a076f40SJoe Perches	my $commit_log_long_line = 0;
1961e518e9a5SJoe Perches	my $commit_log_has_diff = 0;
196213f1937eSJoe Perches	my $reported_maintainer_file = 0;
1963fa64205dSPasi Savanainen	my $non_utf8_charset = 0;
1964fa64205dSPasi Savanainen
1965365dd4eaSJoe Perches	my $last_blank_line = 0;
19665e4f6ba5SJoe Perches	my $last_coalesced_string_linenr = -1;
1967365dd4eaSJoe Perches
196813214adfSAndy Whitcroft	our @report = ();
19696c72ffaaSAndy Whitcroft	our $cnt_lines = 0;
19706c72ffaaSAndy Whitcroft	our $cnt_error = 0;
19716c72ffaaSAndy Whitcroft	our $cnt_warn = 0;
19726c72ffaaSAndy Whitcroft	our $cnt_chk = 0;
19736c72ffaaSAndy Whitcroft
19740a920b5bSAndy Whitcroft	# Trace the real file/line as we go.
19750a920b5bSAndy Whitcroft	my $realfile = '';
19760a920b5bSAndy Whitcroft	my $realline = 0;
19770a920b5bSAndy Whitcroft	my $realcnt = 0;
19780a920b5bSAndy Whitcroft	my $here = '';
19790a920b5bSAndy Whitcroft	my $in_comment = 0;
1980c2fdda0dSAndy Whitcroft	my $comment_edge = 0;
19810a920b5bSAndy Whitcroft	my $first_line = 0;
19821e855726SWolfram Sang	my $p1_prefix = '';
19830a920b5bSAndy Whitcroft
198413214adfSAndy Whitcroft	my $prev_values = 'E';
198513214adfSAndy Whitcroft
198613214adfSAndy Whitcroft	# suppression flags
1987773647a0SAndy Whitcroft	my %suppress_ifbraces;
1988170d3a22SAndy Whitcroft	my %suppress_whiletrailers;
19892b474a1aSAndy Whitcroft	my %suppress_export;
19903e469cdcSAndy Whitcroft	my $suppress_statement = 0;
1991653d4876SAndy Whitcroft
19927e51f197SJoe Perches	my %signatures = ();
1993323c1260SJoe Perches
1994c2fdda0dSAndy Whitcroft	# Pre-scan the patch sanitizing the lines.
1995de7d4f0eSAndy Whitcroft	# Pre-scan the patch looking for any __setup documentation.
1996c2fdda0dSAndy Whitcroft	#
1997de7d4f0eSAndy Whitcroft	my @setup_docs = ();
1998de7d4f0eSAndy Whitcroft	my $setup_docs = 0;
1999773647a0SAndy Whitcroft
2000d8b07710SJoe Perches	my $camelcase_file_seeded = 0;
2001d8b07710SJoe Perches
2002773647a0SAndy Whitcroft	sanitise_line_reset();
2003c2fdda0dSAndy Whitcroft	my $line;
2004c2fdda0dSAndy Whitcroft	foreach my $rawline (@rawlines) {
2005773647a0SAndy Whitcroft		$linenr++;
2006773647a0SAndy Whitcroft		$line = $rawline;
2007c2fdda0dSAndy Whitcroft
20083705ce5bSJoe Perches		push(@fixed, $rawline) if ($fix);
20093705ce5bSJoe Perches
2010773647a0SAndy Whitcroft		if ($rawline=~/^\+\+\+\s+(\S+)/) {
2011de7d4f0eSAndy Whitcroft			$setup_docs = 0;
2012de7d4f0eSAndy Whitcroft			if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
2013de7d4f0eSAndy Whitcroft				$setup_docs = 1;
2014de7d4f0eSAndy Whitcroft			}
2015773647a0SAndy Whitcroft			#next;
2016de7d4f0eSAndy Whitcroft		}
2017773647a0SAndy Whitcroft		if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
2018773647a0SAndy Whitcroft			$realline=$1-1;
2019773647a0SAndy Whitcroft			if (defined $2) {
2020773647a0SAndy Whitcroft				$realcnt=$3+1;
2021773647a0SAndy Whitcroft			} else {
2022773647a0SAndy Whitcroft				$realcnt=1+1;
2023773647a0SAndy Whitcroft			}
2024c45dcabdSAndy Whitcroft			$in_comment = 0;
2025773647a0SAndy Whitcroft
2026773647a0SAndy Whitcroft			# Guestimate if this is a continuing comment.  Run
2027773647a0SAndy Whitcroft			# the context looking for a comment "edge".  If this
2028773647a0SAndy Whitcroft			# edge is a close comment then we must be in a comment
2029773647a0SAndy Whitcroft			# at context start.
2030773647a0SAndy Whitcroft			my $edge;
203101fa9147SAndy Whitcroft			my $cnt = $realcnt;
203201fa9147SAndy Whitcroft			for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
203301fa9147SAndy Whitcroft				next if (defined $rawlines[$ln - 1] &&
203401fa9147SAndy Whitcroft					 $rawlines[$ln - 1] =~ /^-/);
203501fa9147SAndy Whitcroft				$cnt--;
203601fa9147SAndy Whitcroft				#print "RAW<$rawlines[$ln - 1]>\n";
2037721c1cb6SAndy Whitcroft				last if (!defined $rawlines[$ln - 1]);
2038fae17daeSAndy Whitcroft				if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
2039fae17daeSAndy Whitcroft				    $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
2040fae17daeSAndy Whitcroft					($edge) = $1;
2041fae17daeSAndy Whitcroft					last;
2042fae17daeSAndy Whitcroft				}
2043773647a0SAndy Whitcroft			}
2044773647a0SAndy Whitcroft			if (defined $edge && $edge eq '*/') {
2045773647a0SAndy Whitcroft				$in_comment = 1;
2046773647a0SAndy Whitcroft			}
2047773647a0SAndy Whitcroft
2048773647a0SAndy Whitcroft			# Guestimate if this is a continuing comment.  If this
2049773647a0SAndy Whitcroft			# is the start of a diff block and this line starts
2050773647a0SAndy Whitcroft			# ' *' then it is very likely a comment.
2051773647a0SAndy Whitcroft			if (!defined $edge &&
205283242e0cSAndy Whitcroft			    $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
2053773647a0SAndy Whitcroft			{
2054773647a0SAndy Whitcroft				$in_comment = 1;
2055773647a0SAndy Whitcroft			}
2056773647a0SAndy Whitcroft
2057773647a0SAndy Whitcroft			##print "COMMENT:$in_comment edge<$edge> $rawline\n";
2058773647a0SAndy Whitcroft			sanitise_line_reset($in_comment);
2059773647a0SAndy Whitcroft
2060171ae1a4SAndy Whitcroft		} elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
2061773647a0SAndy Whitcroft			# Standardise the strings and chars within the input to
2062171ae1a4SAndy Whitcroft			# simplify matching -- only bother with positive lines.
2063773647a0SAndy Whitcroft			$line = sanitise_line($rawline);
2064773647a0SAndy Whitcroft		}
2065773647a0SAndy Whitcroft		push(@lines, $line);
2066773647a0SAndy Whitcroft
2067773647a0SAndy Whitcroft		if ($realcnt > 1) {
2068773647a0SAndy Whitcroft			$realcnt-- if ($line =~ /^(?:\+| |$)/);
2069773647a0SAndy Whitcroft		} else {
2070773647a0SAndy Whitcroft			$realcnt = 0;
2071773647a0SAndy Whitcroft		}
2072773647a0SAndy Whitcroft
2073773647a0SAndy Whitcroft		#print "==>$rawline\n";
2074773647a0SAndy Whitcroft		#print "-->$line\n";
2075de7d4f0eSAndy Whitcroft
2076de7d4f0eSAndy Whitcroft		if ($setup_docs && $line =~ /^\+/) {
2077de7d4f0eSAndy Whitcroft			push(@setup_docs, $line);
2078de7d4f0eSAndy Whitcroft		}
2079de7d4f0eSAndy Whitcroft	}
2080de7d4f0eSAndy Whitcroft
20816c72ffaaSAndy Whitcroft	$prefix = '';
20826c72ffaaSAndy Whitcroft
2083773647a0SAndy Whitcroft	$realcnt = 0;
2084773647a0SAndy Whitcroft	$linenr = 0;
2085194f66fcSJoe Perches	$fixlinenr = -1;
20860a920b5bSAndy Whitcroft	foreach my $line (@lines) {
20870a920b5bSAndy Whitcroft		$linenr++;
2088194f66fcSJoe Perches		$fixlinenr++;
20891b5539b1SJoe Perches		my $sline = $line;	#copy of $line
20901b5539b1SJoe Perches		$sline =~ s/$;/ /g;	#with comments as spaces
20910a920b5bSAndy Whitcroft
2092c2fdda0dSAndy Whitcroft		my $rawline = $rawlines[$linenr - 1];
20936c72ffaaSAndy Whitcroft
20940a920b5bSAndy Whitcroft#extract the line range in the file after the patch is applied
2095e518e9a5SJoe Perches		if (!$in_commit_log &&
2096e518e9a5SJoe Perches		    $line =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
20970a920b5bSAndy Whitcroft			$is_patch = 1;
20984a0df2efSAndy Whitcroft			$first_line = $linenr + 1;
20990a920b5bSAndy Whitcroft			$realline=$1-1;
21000a920b5bSAndy Whitcroft			if (defined $2) {
21010a920b5bSAndy Whitcroft				$realcnt=$3+1;
21020a920b5bSAndy Whitcroft			} else {
21030a920b5bSAndy Whitcroft				$realcnt=1+1;
21040a920b5bSAndy Whitcroft			}
2105c2fdda0dSAndy Whitcroft			annotate_reset();
210613214adfSAndy Whitcroft			$prev_values = 'E';
210713214adfSAndy Whitcroft
2108773647a0SAndy Whitcroft			%suppress_ifbraces = ();
2109170d3a22SAndy Whitcroft			%suppress_whiletrailers = ();
21102b474a1aSAndy Whitcroft			%suppress_export = ();
21113e469cdcSAndy Whitcroft			$suppress_statement = 0;
21120a920b5bSAndy Whitcroft			next;
21130a920b5bSAndy Whitcroft
21144a0df2efSAndy Whitcroft# track the line number as we move through the hunk, note that
21154a0df2efSAndy Whitcroft# new versions of GNU diff omit the leading space on completely
21164a0df2efSAndy Whitcroft# blank context lines so we need to count that too.
2117773647a0SAndy Whitcroft		} elsif ($line =~ /^( |\+|$)/) {
21180a920b5bSAndy Whitcroft			$realline++;
2119d8aaf121SAndy Whitcroft			$realcnt-- if ($realcnt != 0);
21200a920b5bSAndy Whitcroft
21214a0df2efSAndy Whitcroft			# Measure the line length and indent.
2122c2fdda0dSAndy Whitcroft			($length, $indent) = line_stats($rawline);
21230a920b5bSAndy Whitcroft
21240a920b5bSAndy Whitcroft			# Track the previous line.
21250a920b5bSAndy Whitcroft			($prevline, $stashline) = ($stashline, $line);
21260a920b5bSAndy Whitcroft			($previndent, $stashindent) = ($stashindent, $indent);
2127c2fdda0dSAndy Whitcroft			($prevrawline, $stashrawline) = ($stashrawline, $rawline);
2128c2fdda0dSAndy Whitcroft
2129773647a0SAndy Whitcroft			#warn "line<$line>\n";
21306c72ffaaSAndy Whitcroft
2131d8aaf121SAndy Whitcroft		} elsif ($realcnt == 1) {
2132d8aaf121SAndy Whitcroft			$realcnt--;
21330a920b5bSAndy Whitcroft		}
21340a920b5bSAndy Whitcroft
2135cc77cdcaSAndy Whitcroft		my $hunk_line = ($realcnt != 0);
2136cc77cdcaSAndy Whitcroft
21376c72ffaaSAndy Whitcroft		$here = "#$linenr: " if (!$file);
21386c72ffaaSAndy Whitcroft		$here = "#$realline: " if ($file);
2139773647a0SAndy Whitcroft
21402ac73b4fSJoe Perches		my $found_file = 0;
2141773647a0SAndy Whitcroft		# extract the filename as it passes
21423bf9a009SRabin Vincent		if ($line =~ /^diff --git.*?(\S+)$/) {
21433bf9a009SRabin Vincent			$realfile = $1;
21442b7ab453SJoe Perches			$realfile =~ s@^([^/]*)/@@ if (!$file);
2145270c49a0SJoe Perches			$in_commit_log = 0;
21462ac73b4fSJoe Perches			$found_file = 1;
21473bf9a009SRabin Vincent		} elsif ($line =~ /^\+\+\+\s+(\S+)/) {
2148773647a0SAndy Whitcroft			$realfile = $1;
21492b7ab453SJoe Perches			$realfile =~ s@^([^/]*)/@@ if (!$file);
2150270c49a0SJoe Perches			$in_commit_log = 0;
21511e855726SWolfram Sang
21521e855726SWolfram Sang			$p1_prefix = $1;
2153e2f7aa4bSAndy Whitcroft			if (!$file && $tree && $p1_prefix ne '' &&
2154e2f7aa4bSAndy Whitcroft			    -e "$root/$p1_prefix") {
2155000d1cc1SJoe Perches				WARN("PATCH_PREFIX",
2156000d1cc1SJoe Perches				     "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
21571e855726SWolfram Sang			}
2158773647a0SAndy Whitcroft
2159c1ab3326SAndy Whitcroft			if ($realfile =~ m@^include/asm/@) {
2160000d1cc1SJoe Perches				ERROR("MODIFIED_INCLUDE_ASM",
2161000d1cc1SJoe Perches				      "do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
2162773647a0SAndy Whitcroft			}
21632ac73b4fSJoe Perches			$found_file = 1;
21642ac73b4fSJoe Perches		}
21652ac73b4fSJoe Perches
216634d8815fSJoe Perches#make up the handle for any error we report on this line
216734d8815fSJoe Perches		if ($showfile) {
216834d8815fSJoe Perches			$prefix = "$realfile:$realline: "
216934d8815fSJoe Perches		} elsif ($emacs) {
21707d3a9f67SJoe Perches			if ($file) {
21717d3a9f67SJoe Perches				$prefix = "$filename:$realline: ";
21727d3a9f67SJoe Perches			} else {
217334d8815fSJoe Perches				$prefix = "$filename:$linenr: ";
217434d8815fSJoe Perches			}
21757d3a9f67SJoe Perches		}
217634d8815fSJoe Perches
21772ac73b4fSJoe Perches		if ($found_file) {
21787bd7e483SJoe Perches			if ($realfile =~ m@^(?:drivers/net/|net/|drivers/staging/)@) {
21792ac73b4fSJoe Perches				$check = 1;
21802ac73b4fSJoe Perches			} else {
21812ac73b4fSJoe Perches				$check = $check_orig;
21822ac73b4fSJoe Perches			}
2183773647a0SAndy Whitcroft			next;
2184773647a0SAndy Whitcroft		}
2185773647a0SAndy Whitcroft
2186389834b6SRandy Dunlap		$here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
21870a920b5bSAndy Whitcroft
2188c2fdda0dSAndy Whitcroft		my $hereline = "$here\n$rawline\n";
2189c2fdda0dSAndy Whitcroft		my $herecurr = "$here\n$rawline\n";
2190c2fdda0dSAndy Whitcroft		my $hereprev = "$here\n$prevrawline\n$rawline\n";
21910a920b5bSAndy Whitcroft
21926c72ffaaSAndy Whitcroft		$cnt_lines++ if ($realcnt != 0);
21936c72ffaaSAndy Whitcroft
2194e518e9a5SJoe Perches# Check if the commit log has what seems like a diff which can confuse patch
2195e518e9a5SJoe Perches		if ($in_commit_log && !$commit_log_has_diff &&
2196e518e9a5SJoe Perches		    (($line =~ m@^\s+diff\b.*a/[\w/]+@ &&
2197e518e9a5SJoe Perches		      $line =~ m@^\s+diff\b.*a/([\w/]+)\s+b/$1\b@) ||
2198e518e9a5SJoe Perches		     $line =~ m@^\s*(?:\-\-\-\s+a/|\+\+\+\s+b/)@ ||
2199e518e9a5SJoe Perches		     $line =~ m/^\s*\@\@ \-\d+,\d+ \+\d+,\d+ \@\@/)) {
2200e518e9a5SJoe Perches			ERROR("DIFF_IN_COMMIT_MSG",
2201e518e9a5SJoe Perches			      "Avoid using diff content in the commit message - patch(1) might not work\n" . $herecurr);
2202e518e9a5SJoe Perches			$commit_log_has_diff = 1;
2203e518e9a5SJoe Perches		}
2204e518e9a5SJoe Perches
22053bf9a009SRabin Vincent# Check for incorrect file permissions
22063bf9a009SRabin Vincent		if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
22073bf9a009SRabin Vincent			my $permhere = $here . "FILE: $realfile\n";
220804db4d25SJoe Perches			if ($realfile !~ m@scripts/@ &&
220904db4d25SJoe Perches			    $realfile !~ /\.(py|pl|awk|sh)$/) {
2210000d1cc1SJoe Perches				ERROR("EXECUTE_PERMISSIONS",
2211000d1cc1SJoe Perches				      "do not set execute permissions for source files\n" . $permhere);
22123bf9a009SRabin Vincent			}
22133bf9a009SRabin Vincent		}
22143bf9a009SRabin Vincent
221520112475SJoe Perches# Check the patch for a signoff:
2216d8aaf121SAndy Whitcroft		if ($line =~ /^\s*signed-off-by:/i) {
22174a0df2efSAndy Whitcroft			$signoff++;
221815662b3eSJoe Perches			$in_commit_log = 0;
22190a920b5bSAndy Whitcroft		}
222020112475SJoe Perches
2221e0d975b1SJoe Perches# Check if MAINTAINERS is being updated.  If so, there's probably no need to
2222e0d975b1SJoe Perches# emit the "does MAINTAINERS need updating?" message on file add/move/delete
2223e0d975b1SJoe Perches		if ($line =~ /^\s*MAINTAINERS\s*\|/) {
2224e0d975b1SJoe Perches			$reported_maintainer_file = 1;
2225e0d975b1SJoe Perches		}
2226e0d975b1SJoe Perches
222720112475SJoe Perches# Check signature styles
2228270c49a0SJoe Perches		if (!$in_header_lines &&
2229ce0338dfSJoe Perches		    $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
223020112475SJoe Perches			my $space_before = $1;
223120112475SJoe Perches			my $sign_off = $2;
223220112475SJoe Perches			my $space_after = $3;
223320112475SJoe Perches			my $email = $4;
223420112475SJoe Perches			my $ucfirst_sign_off = ucfirst(lc($sign_off));
223520112475SJoe Perches
2236ce0338dfSJoe Perches			if ($sign_off !~ /$signature_tags/) {
2237ce0338dfSJoe Perches				WARN("BAD_SIGN_OFF",
2238ce0338dfSJoe Perches				     "Non-standard signature: $sign_off\n" . $herecurr);
2239ce0338dfSJoe Perches			}
224020112475SJoe Perches			if (defined $space_before && $space_before ne "") {
22413705ce5bSJoe Perches				if (WARN("BAD_SIGN_OFF",
22423705ce5bSJoe Perches					 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
22433705ce5bSJoe Perches				    $fix) {
2244194f66fcSJoe Perches					$fixed[$fixlinenr] =
22453705ce5bSJoe Perches					    "$ucfirst_sign_off $email";
22463705ce5bSJoe Perches				}
224720112475SJoe Perches			}
224820112475SJoe Perches			if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
22493705ce5bSJoe Perches				if (WARN("BAD_SIGN_OFF",
22503705ce5bSJoe Perches					 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
22513705ce5bSJoe Perches				    $fix) {
2252194f66fcSJoe Perches					$fixed[$fixlinenr] =
22533705ce5bSJoe Perches					    "$ucfirst_sign_off $email";
22543705ce5bSJoe Perches				}
22553705ce5bSJoe Perches
225620112475SJoe Perches			}
225720112475SJoe Perches			if (!defined $space_after || $space_after ne " ") {
22583705ce5bSJoe Perches				if (WARN("BAD_SIGN_OFF",
22593705ce5bSJoe Perches					 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
22603705ce5bSJoe Perches				    $fix) {
2261194f66fcSJoe Perches					$fixed[$fixlinenr] =
22623705ce5bSJoe Perches					    "$ucfirst_sign_off $email";
22633705ce5bSJoe Perches				}
226420112475SJoe Perches			}
226520112475SJoe Perches
226620112475SJoe Perches			my ($email_name, $email_address, $comment) = parse_email($email);
226720112475SJoe Perches			my $suggested_email = format_email(($email_name, $email_address));
226820112475SJoe Perches			if ($suggested_email eq "") {
2269000d1cc1SJoe Perches				ERROR("BAD_SIGN_OFF",
2270000d1cc1SJoe Perches				      "Unrecognized email address: '$email'\n" . $herecurr);
227120112475SJoe Perches			} else {
227220112475SJoe Perches				my $dequoted = $suggested_email;
227320112475SJoe Perches				$dequoted =~ s/^"//;
227420112475SJoe Perches				$dequoted =~ s/" </ </;
227520112475SJoe Perches				# Don't force email to have quotes
227620112475SJoe Perches				# Allow just an angle bracketed address
227720112475SJoe Perches				if ("$dequoted$comment" ne $email &&
227820112475SJoe Perches				    "<$email_address>$comment" ne $email &&
227920112475SJoe Perches				    "$suggested_email$comment" ne $email) {
2280000d1cc1SJoe Perches					WARN("BAD_SIGN_OFF",
2281000d1cc1SJoe Perches					     "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
228220112475SJoe Perches				}
22830a920b5bSAndy Whitcroft			}
22847e51f197SJoe Perches
22857e51f197SJoe Perches# Check for duplicate signatures
22867e51f197SJoe Perches			my $sig_nospace = $line;
22877e51f197SJoe Perches			$sig_nospace =~ s/\s//g;
22887e51f197SJoe Perches			$sig_nospace = lc($sig_nospace);
22897e51f197SJoe Perches			if (defined $signatures{$sig_nospace}) {
22907e51f197SJoe Perches				WARN("BAD_SIGN_OFF",
22917e51f197SJoe Perches				     "Duplicate signature\n" . $herecurr);
22927e51f197SJoe Perches			} else {
22937e51f197SJoe Perches				$signatures{$sig_nospace} = 1;
22947e51f197SJoe Perches			}
22950a920b5bSAndy Whitcroft		}
22960a920b5bSAndy Whitcroft
2297a2fe16b9SJoe Perches# Check email subject for common tools that don't need to be mentioned
2298a2fe16b9SJoe Perches		if ($in_header_lines &&
2299a2fe16b9SJoe Perches		    $line =~ /^Subject:.*\b(?:checkpatch|sparse|smatch)\b[^:]/i) {
2300a2fe16b9SJoe Perches			WARN("EMAIL_SUBJECT",
2301a2fe16b9SJoe Perches			     "A patch subject line should describe the change not the tool that found it\n" . $herecurr);
2302a2fe16b9SJoe Perches		}
2303a2fe16b9SJoe Perches
23049b3189ebSJoe Perches# Check for old stable address
23059b3189ebSJoe Perches		if ($line =~ /^\s*cc:\s*.*<?\bstable\@kernel\.org\b>?.*$/i) {
23069b3189ebSJoe Perches			ERROR("STABLE_ADDRESS",
23079b3189ebSJoe Perches			      "The 'stable' address should be 'stable\@vger.kernel.org'\n" . $herecurr);
23089b3189ebSJoe Perches		}
23099b3189ebSJoe Perches
23107ebd05efSChristopher Covington# Check for unwanted Gerrit info
23117ebd05efSChristopher Covington		if ($in_commit_log && $line =~ /^\s*change-id:/i) {
23127ebd05efSChristopher Covington			ERROR("GERRIT_CHANGE_ID",
23137ebd05efSChristopher Covington			      "Remove Gerrit Change-Id's before submitting upstream.\n" . $herecurr);
23147ebd05efSChristopher Covington		}
23157ebd05efSChristopher Covington
2316*369c8dd3SJoe Perches# Check if the commit log is in a possible stack dump
2317*369c8dd3SJoe Perches		if ($in_commit_log && !$commit_log_possible_stack_dump &&
2318*369c8dd3SJoe Perches		    ($line =~ /^\s*(?:WARNING:|BUG:)/ ||
2319*369c8dd3SJoe Perches		     $line =~ /^\s*\[\s*\d+\.\d{6,6}\s*\]/ ||
2320*369c8dd3SJoe Perches					# timestamp
2321*369c8dd3SJoe Perches		     $line =~ /^\s*\[\<[0-9a-fA-F]{8,}\>\]/)) {
2322*369c8dd3SJoe Perches					# stack dump address
2323*369c8dd3SJoe Perches			$commit_log_possible_stack_dump = 1;
2324*369c8dd3SJoe Perches		}
2325*369c8dd3SJoe Perches
23262a076f40SJoe Perches# Check for line lengths > 75 in commit log, warn once
23272a076f40SJoe Perches		if ($in_commit_log && !$commit_log_long_line &&
2328bf4daf12SJoe Perches		    length($line) > 75 &&
2329bf4daf12SJoe Perches		    !($line =~ /^\s*[a-zA-Z0-9_\/\.]+\s+\|\s+\d+/ ||
2330bf4daf12SJoe Perches					# file delta changes
2331bf4daf12SJoe Perches		      $line =~ /^\s*(?:[\w\.\-]+\/)++[\w\.\-]+:/ ||
2332bf4daf12SJoe Perches					# filename then :
2333bf4daf12SJoe Perches		      $line =~ /^\s*(?:Fixes:|Link:)/i ||
2334bf4daf12SJoe Perches					# A Fixes: or Link: line
2335bf4daf12SJoe Perches		      $commit_log_possible_stack_dump)) {
23362a076f40SJoe Perches			WARN("COMMIT_LOG_LONG_LINE",
23372a076f40SJoe Perches			     "Possible unwrapped commit description (prefer a maximum 75 chars per line)\n" . $herecurr);
23382a076f40SJoe Perches			$commit_log_long_line = 1;
23392a076f40SJoe Perches		}
23402a076f40SJoe Perches
2341bf4daf12SJoe Perches# Reset possible stack dump if a blank line is found
2342bf4daf12SJoe Perches		if ($in_commit_log && $commit_log_possible_stack_dump &&
2343bf4daf12SJoe Perches		    $line =~ /^\s*$/) {
2344bf4daf12SJoe Perches			$commit_log_possible_stack_dump = 0;
2345bf4daf12SJoe Perches		}
2346bf4daf12SJoe Perches
23470d7835fcSJoe Perches# Check for git id commit length and improperly formed commit descriptions
2348*369c8dd3SJoe Perches		if ($in_commit_log && !$commit_log_possible_stack_dump &&
2349fe043ea1SJoe Perches		    ($line =~ /\bcommit\s+[0-9a-f]{5,}\b/i ||
2350bf4daf12SJoe Perches		     ($line =~ /\b[0-9a-f]{12,40}\b/i &&
2351*369c8dd3SJoe Perches		      $line !~ /[\<\[][0-9a-f]{12,40}[\>\]]/i &&
2352bf4daf12SJoe Perches		      $line !~ /\bfixes:\s*[0-9a-f]{12,40}/i))) {
2353fe043ea1SJoe Perches			my $init_char = "c";
2354fe043ea1SJoe Perches			my $orig_commit = "";
23550d7835fcSJoe Perches			my $short = 1;
23560d7835fcSJoe Perches			my $long = 0;
23570d7835fcSJoe Perches			my $case = 1;
23580d7835fcSJoe Perches			my $space = 1;
23590d7835fcSJoe Perches			my $hasdesc = 0;
236019c146a6SJoe Perches			my $hasparens = 0;
23610d7835fcSJoe Perches			my $id = '0123456789ab';
23620d7835fcSJoe Perches			my $orig_desc = "commit description";
23630d7835fcSJoe Perches			my $description = "";
23640d7835fcSJoe Perches
2365fe043ea1SJoe Perches			if ($line =~ /\b(c)ommit\s+([0-9a-f]{5,})\b/i) {
2366fe043ea1SJoe Perches				$init_char = $1;
2367fe043ea1SJoe Perches				$orig_commit = lc($2);
2368fe043ea1SJoe Perches			} elsif ($line =~ /\b([0-9a-f]{12,40})\b/i) {
2369fe043ea1SJoe Perches				$orig_commit = lc($1);
2370fe043ea1SJoe Perches			}
2371fe043ea1SJoe Perches
23720d7835fcSJoe Perches			$short = 0 if ($line =~ /\bcommit\s+[0-9a-f]{12,40}/i);
23730d7835fcSJoe Perches			$long = 1 if ($line =~ /\bcommit\s+[0-9a-f]{41,}/i);
23740d7835fcSJoe Perches			$space = 0 if ($line =~ /\bcommit [0-9a-f]/i);
23750d7835fcSJoe Perches			$case = 0 if ($line =~ /\b[Cc]ommit\s+[0-9a-f]{5,40}[^A-F]/);
23760d7835fcSJoe Perches			if ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)"\)/i) {
23770d7835fcSJoe Perches				$orig_desc = $1;
237819c146a6SJoe Perches				$hasparens = 1;
23790d7835fcSJoe Perches			} elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s*$/i &&
23800d7835fcSJoe Perches				 defined $rawlines[$linenr] &&
23810d7835fcSJoe Perches				 $rawlines[$linenr] =~ /^\s*\("([^"]+)"\)/) {
23820d7835fcSJoe Perches				$orig_desc = $1;
238319c146a6SJoe Perches				$hasparens = 1;
2384b671fde0SJoe Perches			} elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("[^"]+$/i &&
2385b671fde0SJoe Perches				 defined $rawlines[$linenr] &&
2386b671fde0SJoe Perches				 $rawlines[$linenr] =~ /^\s*[^"]+"\)/) {
2387b671fde0SJoe Perches				$line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)$/i;
2388b671fde0SJoe Perches				$orig_desc = $1;
2389b671fde0SJoe Perches				$rawlines[$linenr] =~ /^\s*([^"]+)"\)/;
2390b671fde0SJoe Perches				$orig_desc .= " " . $1;
239119c146a6SJoe Perches				$hasparens = 1;
23920d7835fcSJoe Perches			}
23930d7835fcSJoe Perches
23940d7835fcSJoe Perches			($id, $description) = git_commit_info($orig_commit,
23950d7835fcSJoe Perches							      $id, $orig_desc);
23960d7835fcSJoe Perches
239719c146a6SJoe Perches			if ($short || $long || $space || $case || ($orig_desc ne $description) || !$hasparens) {
2398d311cd44SJoe Perches				ERROR("GIT_COMMIT_ID",
23990d7835fcSJoe Perches				      "Please use git commit description style 'commit <12+ chars of sha1> (\"<title line>\")' - ie: '${init_char}ommit $id (\"$description\")'\n" . $herecurr);
24000d7835fcSJoe Perches			}
2401d311cd44SJoe Perches		}
2402d311cd44SJoe Perches
240313f1937eSJoe Perches# Check for added, moved or deleted files
240413f1937eSJoe Perches		if (!$reported_maintainer_file && !$in_commit_log &&
240513f1937eSJoe Perches		    ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ ||
240613f1937eSJoe Perches		     $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ ||
240713f1937eSJoe Perches		     ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ &&
240813f1937eSJoe Perches		      (defined($1) || defined($2))))) {
240913f1937eSJoe Perches			$reported_maintainer_file = 1;
241013f1937eSJoe Perches			WARN("FILE_PATH_CHANGES",
241113f1937eSJoe Perches			     "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr);
241213f1937eSJoe Perches		}
241313f1937eSJoe Perches
241400df344fSAndy Whitcroft# Check for wrappage within a valid hunk of the file
24158905a67cSAndy Whitcroft		if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
2416000d1cc1SJoe Perches			ERROR("CORRUPTED_PATCH",
2417000d1cc1SJoe Perches			      "patch seems to be corrupt (line wrapped?)\n" .
24186c72ffaaSAndy Whitcroft				$herecurr) if (!$emitted_corrupt++);
2419de7d4f0eSAndy Whitcroft		}
2420de7d4f0eSAndy Whitcroft
24216ecd9674SAndy Whitcroft# Check for absolute kernel paths.
24226ecd9674SAndy Whitcroft		if ($tree) {
24236ecd9674SAndy Whitcroft			while ($line =~ m{(?:^|\s)(/\S*)}g) {
24246ecd9674SAndy Whitcroft				my $file = $1;
24256ecd9674SAndy Whitcroft
24266ecd9674SAndy Whitcroft				if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
24276ecd9674SAndy Whitcroft				    check_absolute_file($1, $herecurr)) {
24286ecd9674SAndy Whitcroft					#
24296ecd9674SAndy Whitcroft				} else {
24306ecd9674SAndy Whitcroft					check_absolute_file($file, $herecurr);
24316ecd9674SAndy Whitcroft				}
24326ecd9674SAndy Whitcroft			}
24336ecd9674SAndy Whitcroft		}
24346ecd9674SAndy Whitcroft
2435de7d4f0eSAndy Whitcroft# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
2436de7d4f0eSAndy Whitcroft		if (($realfile =~ /^$/ || $line =~ /^\+/) &&
2437171ae1a4SAndy Whitcroft		    $rawline !~ m/^$UTF8*$/) {
2438171ae1a4SAndy Whitcroft			my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
2439171ae1a4SAndy Whitcroft
2440171ae1a4SAndy Whitcroft			my $blank = copy_spacing($rawline);
2441171ae1a4SAndy Whitcroft			my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
2442171ae1a4SAndy Whitcroft			my $hereptr = "$hereline$ptr\n";
2443171ae1a4SAndy Whitcroft
244434d99219SJoe Perches			CHK("INVALID_UTF8",
2445000d1cc1SJoe Perches			    "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
244600df344fSAndy Whitcroft		}
24470a920b5bSAndy Whitcroft
244815662b3eSJoe Perches# Check if it's the start of a commit log
244915662b3eSJoe Perches# (not a header line and we haven't seen the patch filename)
245015662b3eSJoe Perches		if ($in_header_lines && $realfile =~ /^$/ &&
245129ee1b0cSJoe Perches		    !($rawline =~ /^\s+\S/ ||
245229ee1b0cSJoe Perches		      $rawline =~ /^(commit\b|from\b|[\w-]+:).*$/i)) {
245315662b3eSJoe Perches			$in_header_lines = 0;
245415662b3eSJoe Perches			$in_commit_log = 1;
245515662b3eSJoe Perches		}
245615662b3eSJoe Perches
2457fa64205dSPasi Savanainen# Check if there is UTF-8 in a commit log when a mail header has explicitly
2458fa64205dSPasi Savanainen# declined it, i.e defined some charset where it is missing.
2459fa64205dSPasi Savanainen		if ($in_header_lines &&
2460fa64205dSPasi Savanainen		    $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
2461fa64205dSPasi Savanainen		    $1 !~ /utf-8/i) {
2462fa64205dSPasi Savanainen			$non_utf8_charset = 1;
2463fa64205dSPasi Savanainen		}
2464fa64205dSPasi Savanainen
2465fa64205dSPasi Savanainen		if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
246615662b3eSJoe Perches		    $rawline =~ /$NON_ASCII_UTF8/) {
2467fa64205dSPasi Savanainen			WARN("UTF8_BEFORE_PATCH",
246815662b3eSJoe Perches			    "8-bit UTF-8 used in possible commit log\n" . $herecurr);
246915662b3eSJoe Perches		}
247015662b3eSJoe Perches
247166b47b4aSKees Cook# Check for various typo / spelling mistakes
247266d7a382SJoe Perches		if (defined($misspellings) &&
247366d7a382SJoe Perches		    ($in_commit_log || $line =~ /^(?:\+|Subject:)/i)) {
2474ebfd7d62SJoe Perches			while ($rawline =~ /(?:^|[^a-z@])($misspellings)(?:\b|$|[^a-z@])/gi) {
247566b47b4aSKees Cook				my $typo = $1;
247666b47b4aSKees Cook				my $typo_fix = $spelling_fix{lc($typo)};
247766b47b4aSKees Cook				$typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/);
247866b47b4aSKees Cook				$typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/);
247966b47b4aSKees Cook				my $msg_type = \&WARN;
248066b47b4aSKees Cook				$msg_type = \&CHK if ($file);
248166b47b4aSKees Cook				if (&{$msg_type}("TYPO_SPELLING",
248266b47b4aSKees Cook						 "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $herecurr) &&
248366b47b4aSKees Cook				    $fix) {
248466b47b4aSKees Cook					$fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/;
248566b47b4aSKees Cook				}
248666b47b4aSKees Cook			}
248766b47b4aSKees Cook		}
248866b47b4aSKees Cook
248930670854SAndy Whitcroft# ignore non-hunk lines and lines being removed
249030670854SAndy Whitcroft		next if (!$hunk_line || $line =~ /^-/);
249100df344fSAndy Whitcroft
24920a920b5bSAndy Whitcroft#trailing whitespace
24939c0ca6f9SAndy Whitcroft		if ($line =~ /^\+.*\015/) {
2494c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2495d5e616fcSJoe Perches			if (ERROR("DOS_LINE_ENDINGS",
2496d5e616fcSJoe Perches				  "DOS line endings\n" . $herevet) &&
2497d5e616fcSJoe Perches			    $fix) {
2498194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/[\s\015]+$//;
2499d5e616fcSJoe Perches			}
2500c2fdda0dSAndy Whitcroft		} elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
2501c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
25023705ce5bSJoe Perches			if (ERROR("TRAILING_WHITESPACE",
25033705ce5bSJoe Perches				  "trailing whitespace\n" . $herevet) &&
25043705ce5bSJoe Perches			    $fix) {
2505194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\s+$//;
25063705ce5bSJoe Perches			}
25073705ce5bSJoe Perches
2508d2c0a235SAndy Whitcroft			$rpt_cleaners = 1;
25090a920b5bSAndy Whitcroft		}
25105368df20SAndy Whitcroft
25114783f894SJosh Triplett# Check for FSF mailing addresses.
2512109d8cb2SAlexander Duyck		if ($rawline =~ /\bwrite to the Free/i ||
25133e2232f2SJoe Perches		    $rawline =~ /\b59\s+Temple\s+Pl/i ||
25143e2232f2SJoe Perches		    $rawline =~ /\b51\s+Franklin\s+St/i) {
25154783f894SJosh Triplett			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
25164783f894SJosh Triplett			my $msg_type = \&ERROR;
25174783f894SJosh Triplett			$msg_type = \&CHK if ($file);
25184783f894SJosh Triplett			&{$msg_type}("FSF_MAILING_ADDRESS",
25194783f894SJosh 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)
25204783f894SJosh Triplett		}
25214783f894SJosh Triplett
25223354957aSAndi Kleen# check for Kconfig help text having a real description
25239fe287d7SAndy Whitcroft# Only applies when adding the entry originally, after that we do not have
25249fe287d7SAndy Whitcroft# sufficient context to determine whether it is indeed long enough.
25253354957aSAndi Kleen		if ($realfile =~ /Kconfig/ &&
25268d73e0e7SJoe Perches		    $line =~ /^\+\s*config\s+/) {
25273354957aSAndi Kleen			my $length = 0;
25289fe287d7SAndy Whitcroft			my $cnt = $realcnt;
25299fe287d7SAndy Whitcroft			my $ln = $linenr + 1;
25309fe287d7SAndy Whitcroft			my $f;
2531a1385803SAndy Whitcroft			my $is_start = 0;
25329fe287d7SAndy Whitcroft			my $is_end = 0;
2533a1385803SAndy Whitcroft			for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
25349fe287d7SAndy Whitcroft				$f = $lines[$ln - 1];
25359fe287d7SAndy Whitcroft				$cnt-- if ($lines[$ln - 1] !~ /^-/);
25369fe287d7SAndy Whitcroft				$is_end = $lines[$ln - 1] =~ /^\+/;
25379fe287d7SAndy Whitcroft
25389fe287d7SAndy Whitcroft				next if ($f =~ /^-/);
25398d73e0e7SJoe Perches				last if (!$file && $f =~ /^\@\@/);
2540a1385803SAndy Whitcroft
25418d73e0e7SJoe Perches				if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate)\s*\"/) {
2542a1385803SAndy Whitcroft					$is_start = 1;
25438d73e0e7SJoe Perches				} elsif ($lines[$ln - 1] =~ /^\+\s*(?:---)?help(?:---)?$/) {
2544a1385803SAndy Whitcroft					$length = -1;
2545a1385803SAndy Whitcroft				}
2546a1385803SAndy Whitcroft
25479fe287d7SAndy Whitcroft				$f =~ s/^.//;
25483354957aSAndi Kleen				$f =~ s/#.*//;
25493354957aSAndi Kleen				$f =~ s/^\s+//;
25503354957aSAndi Kleen				next if ($f =~ /^$/);
25519fe287d7SAndy Whitcroft				if ($f =~ /^\s*config\s/) {
25529fe287d7SAndy Whitcroft					$is_end = 1;
25539fe287d7SAndy Whitcroft					last;
25549fe287d7SAndy Whitcroft				}
25553354957aSAndi Kleen				$length++;
25563354957aSAndi Kleen			}
255756193274SVadim Bendebury			if ($is_start && $is_end && $length < $min_conf_desc_length) {
2558000d1cc1SJoe Perches				WARN("CONFIG_DESCRIPTION",
255956193274SVadim Bendebury				     "please write a paragraph that describes the config symbol fully\n" . $herecurr);
256056193274SVadim Bendebury			}
2561a1385803SAndy Whitcroft			#print "is_start<$is_start> is_end<$is_end> length<$length>\n";
25623354957aSAndi Kleen		}
25633354957aSAndi Kleen
25641ba8dfd1SKees Cook# discourage the addition of CONFIG_EXPERIMENTAL in Kconfig.
25651ba8dfd1SKees Cook		if ($realfile =~ /Kconfig/ &&
25661ba8dfd1SKees Cook		    $line =~ /.\s*depends on\s+.*\bEXPERIMENTAL\b/) {
25671ba8dfd1SKees Cook			WARN("CONFIG_EXPERIMENTAL",
25681ba8dfd1SKees Cook			     "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
25691ba8dfd1SKees Cook		}
25701ba8dfd1SKees Cook
2571327953e9SChristoph Jaeger# discourage the use of boolean for type definition attributes of Kconfig options
2572327953e9SChristoph Jaeger		if ($realfile =~ /Kconfig/ &&
2573327953e9SChristoph Jaeger		    $line =~ /^\+\s*\bboolean\b/) {
2574327953e9SChristoph Jaeger			WARN("CONFIG_TYPE_BOOLEAN",
2575327953e9SChristoph Jaeger			     "Use of boolean is deprecated, please use bool instead.\n" . $herecurr);
2576327953e9SChristoph Jaeger		}
2577327953e9SChristoph Jaeger
2578c68e5878SArnaud Lacombe		if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
2579c68e5878SArnaud Lacombe		    ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
2580c68e5878SArnaud Lacombe			my $flag = $1;
2581c68e5878SArnaud Lacombe			my $replacement = {
2582c68e5878SArnaud Lacombe				'EXTRA_AFLAGS' =>   'asflags-y',
2583c68e5878SArnaud Lacombe				'EXTRA_CFLAGS' =>   'ccflags-y',
2584c68e5878SArnaud Lacombe				'EXTRA_CPPFLAGS' => 'cppflags-y',
2585c68e5878SArnaud Lacombe				'EXTRA_LDFLAGS' =>  'ldflags-y',
2586c68e5878SArnaud Lacombe			};
2587c68e5878SArnaud Lacombe
2588c68e5878SArnaud Lacombe			WARN("DEPRECATED_VARIABLE",
2589c68e5878SArnaud Lacombe			     "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
2590c68e5878SArnaud Lacombe		}
2591c68e5878SArnaud Lacombe
2592bff5da43SRob Herring# check for DT compatible documentation
25937dd05b38SFlorian Vaussard		if (defined $root &&
25947dd05b38SFlorian Vaussard			(($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) ||
25957dd05b38SFlorian Vaussard			 ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) {
25967dd05b38SFlorian Vaussard
2597bff5da43SRob Herring			my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g;
2598bff5da43SRob Herring
2599cc93319bSFlorian Vaussard			my $dt_path = $root . "/Documentation/devicetree/bindings/";
2600cc93319bSFlorian Vaussard			my $vp_file = $dt_path . "vendor-prefixes.txt";
2601cc93319bSFlorian Vaussard
2602bff5da43SRob Herring			foreach my $compat (@compats) {
2603bff5da43SRob Herring				my $compat2 = $compat;
2604185d566bSRob Herring				$compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/;
2605185d566bSRob Herring				my $compat3 = $compat;
2606185d566bSRob Herring				$compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/;
2607185d566bSRob Herring				`grep -Erq "$compat|$compat2|$compat3" $dt_path`;
2608bff5da43SRob Herring				if ( $? >> 8 ) {
2609bff5da43SRob Herring					WARN("UNDOCUMENTED_DT_STRING",
2610bff5da43SRob Herring					     "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr);
2611bff5da43SRob Herring				}
2612bff5da43SRob Herring
26134fbf32a6SFlorian Vaussard				next if $compat !~ /^([a-zA-Z0-9\-]+)\,/;
26144fbf32a6SFlorian Vaussard				my $vendor = $1;
2615cc93319bSFlorian Vaussard				`grep -Eq "^$vendor\\b" $vp_file`;
2616bff5da43SRob Herring				if ( $? >> 8 ) {
2617bff5da43SRob Herring					WARN("UNDOCUMENTED_DT_STRING",
2618cc93319bSFlorian Vaussard					     "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr);
2619bff5da43SRob Herring				}
2620bff5da43SRob Herring			}
2621bff5da43SRob Herring		}
2622bff5da43SRob Herring
26235368df20SAndy Whitcroft# check we are in a valid source file if not then ignore this hunk
2624de4c924cSGeert Uytterhoeven		next if ($realfile !~ /\.(h|c|s|S|pl|sh|dtsi|dts)$/);
26255368df20SAndy Whitcroft
262647e0c88bSJoe Perches# line length limit (with some exclusions)
262747e0c88bSJoe Perches#
262847e0c88bSJoe Perches# There are a few types of lines that may extend beyond $max_line_length:
262947e0c88bSJoe Perches#	logging functions like pr_info that end in a string
263047e0c88bSJoe Perches#	lines with a single string
263147e0c88bSJoe Perches#	#defines that are a single string
263247e0c88bSJoe Perches#
263347e0c88bSJoe Perches# There are 3 different line length message types:
263447e0c88bSJoe Perches# LONG_LINE_COMMENT	a comment starts before but extends beyond $max_linelength
263547e0c88bSJoe Perches# LONG_LINE_STRING	a string starts before but extends beyond $max_line_length
263647e0c88bSJoe Perches# LONG_LINE		all other lines longer than $max_line_length
263747e0c88bSJoe Perches#
263847e0c88bSJoe Perches# if LONG_LINE is ignored, the other 2 types are also ignored
263947e0c88bSJoe Perches#
264047e0c88bSJoe Perches
2641b4749e96SJoe Perches		if ($line =~ /^\+/ && $length > $max_line_length) {
264247e0c88bSJoe Perches			my $msg_type = "LONG_LINE";
264347e0c88bSJoe Perches
264447e0c88bSJoe Perches			# Check the allowed long line types first
264547e0c88bSJoe Perches
264647e0c88bSJoe Perches			# logging functions that end in a string that starts
264747e0c88bSJoe Perches			# before $max_line_length
264847e0c88bSJoe Perches			if ($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(?:KERN_\S+\s*|[^"]*))?($String\s*(?:|,|\)\s*;)\s*)$/ &&
264947e0c88bSJoe Perches			    length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
265047e0c88bSJoe Perches				$msg_type = "";
265147e0c88bSJoe Perches
265247e0c88bSJoe Perches			# lines with only strings (w/ possible termination)
265347e0c88bSJoe Perches			# #defines with only strings
265447e0c88bSJoe Perches			} elsif ($line =~ /^\+\s*$String\s*(?:\s*|,|\)\s*;)\s*$/ ||
265547e0c88bSJoe Perches				 $line =~ /^\+\s*#\s*define\s+\w+\s+$String$/) {
265647e0c88bSJoe Perches				$msg_type = "";
265747e0c88bSJoe Perches
265847e0c88bSJoe Perches			# Otherwise set the alternate message types
265947e0c88bSJoe Perches
266047e0c88bSJoe Perches			# a comment starts before $max_line_length
266147e0c88bSJoe Perches			} elsif ($line =~ /($;[\s$;]*)$/ &&
266247e0c88bSJoe Perches				 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
266347e0c88bSJoe Perches				$msg_type = "LONG_LINE_COMMENT"
266447e0c88bSJoe Perches
266547e0c88bSJoe Perches			# a quoted string starts before $max_line_length
266647e0c88bSJoe Perches			} elsif ($sline =~ /\s*($String(?:\s*(?:\\|,\s*|\)\s*;\s*))?)$/ &&
266747e0c88bSJoe Perches				 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
266847e0c88bSJoe Perches				$msg_type = "LONG_LINE_STRING"
266947e0c88bSJoe Perches			}
267047e0c88bSJoe Perches
267147e0c88bSJoe Perches			if ($msg_type ne "" &&
267247e0c88bSJoe Perches			    (show_type("LONG_LINE") || show_type($msg_type))) {
267347e0c88bSJoe Perches				WARN($msg_type,
26746cd7f386SJoe Perches				     "line over $max_line_length characters\n" . $herecurr);
26750a920b5bSAndy Whitcroft			}
267647e0c88bSJoe Perches		}
26770a920b5bSAndy Whitcroft
26788905a67cSAndy Whitcroft# check for adding lines without a newline.
26798905a67cSAndy Whitcroft		if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
2680000d1cc1SJoe Perches			WARN("MISSING_EOF_NEWLINE",
2681000d1cc1SJoe Perches			     "adding a line without newline at end of file\n" . $herecurr);
26828905a67cSAndy Whitcroft		}
26838905a67cSAndy Whitcroft
268442e41c54SMike Frysinger# Blackfin: use hi/lo macros
268542e41c54SMike Frysinger		if ($realfile =~ m@arch/blackfin/.*\.S$@) {
268642e41c54SMike Frysinger			if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
268742e41c54SMike Frysinger				my $herevet = "$here\n" . cat_vet($line) . "\n";
2688000d1cc1SJoe Perches				ERROR("LO_MACRO",
2689000d1cc1SJoe Perches				      "use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
269042e41c54SMike Frysinger			}
269142e41c54SMike Frysinger			if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
269242e41c54SMike Frysinger				my $herevet = "$here\n" . cat_vet($line) . "\n";
2693000d1cc1SJoe Perches				ERROR("HI_MACRO",
2694000d1cc1SJoe Perches				      "use the HI() macro, not (... >> 16)\n" . $herevet);
269542e41c54SMike Frysinger			}
269642e41c54SMike Frysinger		}
269742e41c54SMike Frysinger
2698b9ea10d6SAndy Whitcroft# check we are in a valid source file C or perl if not then ignore this hunk
2699de4c924cSGeert Uytterhoeven		next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/);
27000a920b5bSAndy Whitcroft
27010a920b5bSAndy Whitcroft# at the beginning of a line any tabs must come first and anything
27020a920b5bSAndy Whitcroft# more than 8 must use tabs.
2703c2fdda0dSAndy Whitcroft		if ($rawline =~ /^\+\s* \t\s*\S/ ||
2704c2fdda0dSAndy Whitcroft		    $rawline =~ /^\+\s*        \s*/) {
2705c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2706d2c0a235SAndy Whitcroft			$rpt_cleaners = 1;
27073705ce5bSJoe Perches			if (ERROR("CODE_INDENT",
27083705ce5bSJoe Perches				  "code indent should use tabs where possible\n" . $herevet) &&
27093705ce5bSJoe Perches			    $fix) {
2710194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
27113705ce5bSJoe Perches			}
27120a920b5bSAndy Whitcroft		}
27130a920b5bSAndy Whitcroft
271408e44365SAlberto Panizzo# check for space before tabs.
271508e44365SAlberto Panizzo		if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
271608e44365SAlberto Panizzo			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
27173705ce5bSJoe Perches			if (WARN("SPACE_BEFORE_TAB",
27183705ce5bSJoe Perches				"please, no space before tabs\n" . $herevet) &&
27193705ce5bSJoe Perches			    $fix) {
2720194f66fcSJoe Perches				while ($fixed[$fixlinenr] =~
2721d2207ccbSJoe Perches					   s/(^\+.*) {8,8}\t/$1\t\t/) {}
2722194f66fcSJoe Perches				while ($fixed[$fixlinenr] =~
2723c76f4cb3SJoe Perches					   s/(^\+.*) +\t/$1\t/) {}
27243705ce5bSJoe Perches			}
272508e44365SAlberto Panizzo		}
272608e44365SAlberto Panizzo
2727d1fe9c09SJoe Perches# check for && or || at the start of a line
2728d1fe9c09SJoe Perches		if ($rawline =~ /^\+\s*(&&|\|\|)/) {
2729d1fe9c09SJoe Perches			CHK("LOGICAL_CONTINUATIONS",
2730d1fe9c09SJoe Perches			    "Logical continuations should be on the previous line\n" . $hereprev);
2731d1fe9c09SJoe Perches		}
2732d1fe9c09SJoe Perches
2733d1fe9c09SJoe Perches# check multi-line statement indentation matches previous line
2734d1fe9c09SJoe Perches		if ($^V && $^V ge 5.10.0 &&
273591cb5195SJoe Perches		    $prevline =~ /^\+([ \t]*)((?:$c90_Keywords(?:\s+if)\s*)|(?:$Declare\s*)?(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*|$Ident\s*=\s*$Ident\s*)\(.*(\&\&|\|\||,)\s*$/) {
2736d1fe9c09SJoe Perches			$prevline =~ /^\+(\t*)(.*)$/;
2737d1fe9c09SJoe Perches			my $oldindent = $1;
2738d1fe9c09SJoe Perches			my $rest = $2;
2739d1fe9c09SJoe Perches
2740d1fe9c09SJoe Perches			my $pos = pos_last_openparen($rest);
2741d1fe9c09SJoe Perches			if ($pos >= 0) {
2742b34a26f3SJoe Perches				$line =~ /^(\+| )([ \t]*)/;
2743b34a26f3SJoe Perches				my $newindent = $2;
2744d1fe9c09SJoe Perches
2745d1fe9c09SJoe Perches				my $goodtabindent = $oldindent .
2746d1fe9c09SJoe Perches					"\t" x ($pos / 8) .
2747d1fe9c09SJoe Perches					" "  x ($pos % 8);
2748d1fe9c09SJoe Perches				my $goodspaceindent = $oldindent . " "  x $pos;
2749d1fe9c09SJoe Perches
2750d1fe9c09SJoe Perches				if ($newindent ne $goodtabindent &&
2751d1fe9c09SJoe Perches				    $newindent ne $goodspaceindent) {
27523705ce5bSJoe Perches
27533705ce5bSJoe Perches					if (CHK("PARENTHESIS_ALIGNMENT",
27543705ce5bSJoe Perches						"Alignment should match open parenthesis\n" . $hereprev) &&
27553705ce5bSJoe Perches					    $fix && $line =~ /^\+/) {
2756194f66fcSJoe Perches						$fixed[$fixlinenr] =~
27573705ce5bSJoe Perches						    s/^\+[ \t]*/\+$goodtabindent/;
27583705ce5bSJoe Perches					}
2759d1fe9c09SJoe Perches				}
2760d1fe9c09SJoe Perches			}
2761d1fe9c09SJoe Perches		}
2762d1fe9c09SJoe Perches
27636ab3a970SJoe Perches# check for space after cast like "(int) foo" or "(struct foo) bar"
27646ab3a970SJoe Perches# avoid checking a few false positives:
27656ab3a970SJoe Perches#   "sizeof(<type>)" or "__alignof__(<type>)"
27666ab3a970SJoe Perches#   function pointer declarations like "(*foo)(int) = bar;"
27676ab3a970SJoe Perches#   structure definitions like "(struct foo) { 0 };"
27686ab3a970SJoe Perches#   multiline macros that define functions
27696ab3a970SJoe Perches#   known attributes or the __attribute__ keyword
27706ab3a970SJoe Perches		if ($line =~ /^\+(.*)\(\s*$Type\s*\)([ \t]++)((?![={]|\\$|$Attribute|__attribute__))/ &&
27716ab3a970SJoe Perches		    (!defined($1) || $1 !~ /\b(?:sizeof|__alignof__)\s*$/)) {
27723705ce5bSJoe Perches			if (CHK("SPACING",
2773f27c95dbSJoe Perches				"No space is necessary after a cast\n" . $herecurr) &&
27743705ce5bSJoe Perches			    $fix) {
2775194f66fcSJoe Perches				$fixed[$fixlinenr] =~
2776f27c95dbSJoe Perches				    s/(\(\s*$Type\s*\))[ \t]+/$1/;
27773705ce5bSJoe Perches			}
2778aad4f614SJoe Perches		}
2779aad4f614SJoe Perches
278086406b1cSJoe Perches# Block comment styles
278186406b1cSJoe Perches# Networking with an initial /*
278205880600SJoe Perches		if ($realfile =~ m@^(drivers/net/|net/)@ &&
2783fdb4bcd6SJoe Perches		    $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
278485ad978cSJoe Perches		    $rawline =~ /^\+[ \t]*\*/ &&
278585ad978cSJoe Perches		    $realline > 2) {
278605880600SJoe Perches			WARN("NETWORKING_BLOCK_COMMENT_STYLE",
278705880600SJoe Perches			     "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
278805880600SJoe Perches		}
278905880600SJoe Perches
279086406b1cSJoe Perches# Block comments use * on subsequent lines
279186406b1cSJoe Perches		if ($prevline =~ /$;[ \t]*$/ &&			#ends in comment
279286406b1cSJoe Perches		    $prevrawline =~ /^\+.*?\/\*/ &&		#starting /*
2793a605e32eSJoe Perches		    $prevrawline !~ /\*\/[ \t]*$/ &&		#no trailing */
279461135e96SJoe Perches		    $rawline =~ /^\+/ &&			#line is new
2795a605e32eSJoe Perches		    $rawline !~ /^\+[ \t]*\*/) {		#no leading *
279686406b1cSJoe Perches			WARN("BLOCK_COMMENT_STYLE",
279786406b1cSJoe Perches			     "Block comments use * on subsequent lines\n" . $hereprev);
2798a605e32eSJoe Perches		}
2799a605e32eSJoe Perches
280086406b1cSJoe Perches# Block comments use */ on trailing lines
280186406b1cSJoe Perches		if ($rawline !~ m@^\+[ \t]*\*/[ \t]*$@ &&	#trailing */
2802c24f9f19SJoe Perches		    $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ &&	#inline /*...*/
2803c24f9f19SJoe Perches		    $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ &&	#trailing **/
2804c24f9f19SJoe Perches		    $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) {	#non blank */
280586406b1cSJoe Perches			WARN("BLOCK_COMMENT_STYLE",
280686406b1cSJoe Perches			     "Block comments use a trailing */ on a separate line\n" . $herecurr);
280705880600SJoe Perches		}
280805880600SJoe Perches
28097f619191SJoe Perches# check for missing blank lines after struct/union declarations
28107f619191SJoe Perches# with exceptions for various attributes and macros
28117f619191SJoe Perches		if ($prevline =~ /^[\+ ]};?\s*$/ &&
28127f619191SJoe Perches		    $line =~ /^\+/ &&
28137f619191SJoe Perches		    !($line =~ /^\+\s*$/ ||
28147f619191SJoe Perches		      $line =~ /^\+\s*EXPORT_SYMBOL/ ||
28157f619191SJoe Perches		      $line =~ /^\+\s*MODULE_/i ||
28167f619191SJoe Perches		      $line =~ /^\+\s*\#\s*(?:end|elif|else)/ ||
28177f619191SJoe Perches		      $line =~ /^\+[a-z_]*init/ ||
28187f619191SJoe Perches		      $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ ||
28197f619191SJoe Perches		      $line =~ /^\+\s*DECLARE/ ||
28207f619191SJoe Perches		      $line =~ /^\+\s*__setup/)) {
2821d752fcc8SJoe Perches			if (CHK("LINE_SPACING",
2822d752fcc8SJoe Perches				"Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) &&
2823d752fcc8SJoe Perches			    $fix) {
2824f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, "\+");
2825d752fcc8SJoe Perches			}
28267f619191SJoe Perches		}
28277f619191SJoe Perches
2828365dd4eaSJoe Perches# check for multiple consecutive blank lines
2829365dd4eaSJoe Perches		if ($prevline =~ /^[\+ ]\s*$/ &&
2830365dd4eaSJoe Perches		    $line =~ /^\+\s*$/ &&
2831365dd4eaSJoe Perches		    $last_blank_line != ($linenr - 1)) {
2832d752fcc8SJoe Perches			if (CHK("LINE_SPACING",
2833d752fcc8SJoe Perches				"Please don't use multiple blank lines\n" . $hereprev) &&
2834d752fcc8SJoe Perches			    $fix) {
2835f2d7e4d4SJoe Perches				fix_delete_line($fixlinenr, $rawline);
2836d752fcc8SJoe Perches			}
2837d752fcc8SJoe Perches
2838365dd4eaSJoe Perches			$last_blank_line = $linenr;
2839365dd4eaSJoe Perches		}
2840365dd4eaSJoe Perches
28413b617e3bSJoe Perches# check for missing blank lines after declarations
28423f7bac03SJoe Perches		if ($sline =~ /^\+\s+\S/ &&			#Not at char 1
28433f7bac03SJoe Perches			# actual declarations
28443f7bac03SJoe Perches		    ($prevline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
28455a4e1fd3SJoe Perches			# function pointer declarations
28465a4e1fd3SJoe Perches		     $prevline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
28473f7bac03SJoe Perches			# foo bar; where foo is some local typedef or #define
28483f7bac03SJoe Perches		     $prevline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
28493f7bac03SJoe Perches			# known declaration macros
28503f7bac03SJoe Perches		     $prevline =~ /^\+\s+$declaration_macros/) &&
28513f7bac03SJoe Perches			# for "else if" which can look like "$Ident $Ident"
28523f7bac03SJoe Perches		    !($prevline =~ /^\+\s+$c90_Keywords\b/ ||
28533f7bac03SJoe Perches			# other possible extensions of declaration lines
28543f7bac03SJoe Perches		      $prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ ||
28553f7bac03SJoe Perches			# not starting a section or a macro "\" extended line
28563f7bac03SJoe Perches		      $prevline =~ /(?:\{\s*|\\)$/) &&
28573f7bac03SJoe Perches			# looks like a declaration
28583f7bac03SJoe Perches		    !($sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
28595a4e1fd3SJoe Perches			# function pointer declarations
28605a4e1fd3SJoe Perches		      $sline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
28613f7bac03SJoe Perches			# foo bar; where foo is some local typedef or #define
28623f7bac03SJoe Perches		      $sline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
28633f7bac03SJoe Perches			# known declaration macros
28643f7bac03SJoe Perches		      $sline =~ /^\+\s+$declaration_macros/ ||
28653f7bac03SJoe Perches			# start of struct or union or enum
28663b617e3bSJoe Perches		      $sline =~ /^\+\s+(?:union|struct|enum|typedef)\b/ ||
28673f7bac03SJoe Perches			# start or end of block or continuation of declaration
28683f7bac03SJoe Perches		      $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ ||
28693f7bac03SJoe Perches			# bitfield continuation
28703f7bac03SJoe Perches		      $sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ ||
28713f7bac03SJoe Perches			# other possible extensions of declaration lines
28723f7bac03SJoe Perches		      $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) &&
28733f7bac03SJoe Perches			# indentation of previous and current line are the same
28743f7bac03SJoe Perches		    (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) {
2875d752fcc8SJoe Perches			if (WARN("LINE_SPACING",
2876d752fcc8SJoe Perches				 "Missing a blank line after declarations\n" . $hereprev) &&
2877d752fcc8SJoe Perches			    $fix) {
2878f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, "\+");
2879d752fcc8SJoe Perches			}
28803b617e3bSJoe Perches		}
28813b617e3bSJoe Perches
28825f7ddae6SRaffaele Recalcati# check for spaces at the beginning of a line.
28836b4c5bebSAndy Whitcroft# Exceptions:
28846b4c5bebSAndy Whitcroft#  1) within comments
28856b4c5bebSAndy Whitcroft#  2) indented preprocessor commands
28866b4c5bebSAndy Whitcroft#  3) hanging labels
28873705ce5bSJoe Perches		if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/)  {
28885f7ddae6SRaffaele Recalcati			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
28893705ce5bSJoe Perches			if (WARN("LEADING_SPACE",
28903705ce5bSJoe Perches				 "please, no spaces at the start of a line\n" . $herevet) &&
28913705ce5bSJoe Perches			    $fix) {
2892194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
28933705ce5bSJoe Perches			}
28945f7ddae6SRaffaele Recalcati		}
28955f7ddae6SRaffaele Recalcati
2896b9ea10d6SAndy Whitcroft# check we are in a valid C source file if not then ignore this hunk
2897b9ea10d6SAndy Whitcroft		next if ($realfile !~ /\.(h|c)$/);
2898b9ea10d6SAndy Whitcroft
2899032a4c0fSJoe Perches# check indentation of any line with a bare else
2900840080a0SJoe Perches# (but not if it is a multiple line "if (foo) return bar; else return baz;")
2901032a4c0fSJoe Perches# if the previous line is a break or return and is indented 1 tab more...
2902032a4c0fSJoe Perches		if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {
2903032a4c0fSJoe Perches			my $tabs = length($1) + 1;
2904840080a0SJoe Perches			if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ ||
2905840080a0SJoe Perches			    ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ &&
2906840080a0SJoe Perches			     defined $lines[$linenr] &&
2907840080a0SJoe Perches			     $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) {
2908032a4c0fSJoe Perches				WARN("UNNECESSARY_ELSE",
2909032a4c0fSJoe Perches				     "else is not generally useful after a break or return\n" . $hereprev);
2910032a4c0fSJoe Perches			}
2911032a4c0fSJoe Perches		}
2912032a4c0fSJoe Perches
2913c00df19aSJoe Perches# check indentation of a line with a break;
2914c00df19aSJoe Perches# if the previous line is a goto or return and is indented the same # of tabs
2915c00df19aSJoe Perches		if ($sline =~ /^\+([\t]+)break\s*;\s*$/) {
2916c00df19aSJoe Perches			my $tabs = $1;
2917c00df19aSJoe Perches			if ($prevline =~ /^\+$tabs(?:goto|return)\b/) {
2918c00df19aSJoe Perches				WARN("UNNECESSARY_BREAK",
2919c00df19aSJoe Perches				     "break is not useful after a goto or return\n" . $hereprev);
2920c00df19aSJoe Perches			}
2921c00df19aSJoe Perches		}
2922c00df19aSJoe Perches
29231ba8dfd1SKees Cook# discourage the addition of CONFIG_EXPERIMENTAL in #if(def).
29241ba8dfd1SKees Cook		if ($line =~ /^\+\s*\#\s*if.*\bCONFIG_EXPERIMENTAL\b/) {
29251ba8dfd1SKees Cook			WARN("CONFIG_EXPERIMENTAL",
29261ba8dfd1SKees Cook			     "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
29271ba8dfd1SKees Cook		}
29281ba8dfd1SKees Cook
2929c2fdda0dSAndy Whitcroft# check for RCS/CVS revision markers
2930cf655043SAndy Whitcroft		if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
2931000d1cc1SJoe Perches			WARN("CVS_KEYWORD",
2932000d1cc1SJoe Perches			     "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
2933c2fdda0dSAndy Whitcroft		}
293422f2a2efSAndy Whitcroft
293542e41c54SMike Frysinger# Blackfin: don't use __builtin_bfin_[cs]sync
293642e41c54SMike Frysinger		if ($line =~ /__builtin_bfin_csync/) {
293742e41c54SMike Frysinger			my $herevet = "$here\n" . cat_vet($line) . "\n";
2938000d1cc1SJoe Perches			ERROR("CSYNC",
2939000d1cc1SJoe Perches			      "use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
294042e41c54SMike Frysinger		}
294142e41c54SMike Frysinger		if ($line =~ /__builtin_bfin_ssync/) {
294242e41c54SMike Frysinger			my $herevet = "$here\n" . cat_vet($line) . "\n";
2943000d1cc1SJoe Perches			ERROR("SSYNC",
2944000d1cc1SJoe Perches			      "use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
294542e41c54SMike Frysinger		}
294642e41c54SMike Frysinger
294756e77d70SJoe Perches# check for old HOTPLUG __dev<foo> section markings
294856e77d70SJoe Perches		if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
294956e77d70SJoe Perches			WARN("HOTPLUG_SECTION",
295056e77d70SJoe Perches			     "Using $1 is unnecessary\n" . $herecurr);
295156e77d70SJoe Perches		}
295256e77d70SJoe Perches
29539c0ca6f9SAndy Whitcroft# Check for potential 'bare' types
29542b474a1aSAndy Whitcroft		my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
29552b474a1aSAndy Whitcroft		    $realline_next);
29563e469cdcSAndy Whitcroft#print "LINE<$line>\n";
29573e469cdcSAndy Whitcroft		if ($linenr >= $suppress_statement &&
29581b5539b1SJoe Perches		    $realcnt && $sline =~ /.\s*\S/) {
2959170d3a22SAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
2960f5fe35ddSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0);
2961171ae1a4SAndy Whitcroft			$stat =~ s/\n./\n /g;
2962171ae1a4SAndy Whitcroft			$cond =~ s/\n./\n /g;
2963171ae1a4SAndy Whitcroft
29643e469cdcSAndy Whitcroft#print "linenr<$linenr> <$stat>\n";
29653e469cdcSAndy Whitcroft			# If this statement has no statement boundaries within
29663e469cdcSAndy Whitcroft			# it there is no point in retrying a statement scan
29673e469cdcSAndy Whitcroft			# until we hit end of it.
29683e469cdcSAndy Whitcroft			my $frag = $stat; $frag =~ s/;+\s*$//;
29693e469cdcSAndy Whitcroft			if ($frag !~ /(?:{|;)/) {
29703e469cdcSAndy Whitcroft#print "skip<$line_nr_next>\n";
29713e469cdcSAndy Whitcroft				$suppress_statement = $line_nr_next;
29723e469cdcSAndy Whitcroft			}
2973f74bd194SAndy Whitcroft
29742b474a1aSAndy Whitcroft			# Find the real next line.
29752b474a1aSAndy Whitcroft			$realline_next = $line_nr_next;
29762b474a1aSAndy Whitcroft			if (defined $realline_next &&
29772b474a1aSAndy Whitcroft			    (!defined $lines[$realline_next - 1] ||
29782b474a1aSAndy Whitcroft			     substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
29792b474a1aSAndy Whitcroft				$realline_next++;
29802b474a1aSAndy Whitcroft			}
29812b474a1aSAndy Whitcroft
2982171ae1a4SAndy Whitcroft			my $s = $stat;
2983171ae1a4SAndy Whitcroft			$s =~ s/{.*$//s;
2984cf655043SAndy Whitcroft
2985c2fdda0dSAndy Whitcroft			# Ignore goto labels.
2986171ae1a4SAndy Whitcroft			if ($s =~ /$Ident:\*$/s) {
2987c2fdda0dSAndy Whitcroft
2988c2fdda0dSAndy Whitcroft			# Ignore functions being called
2989171ae1a4SAndy Whitcroft			} elsif ($s =~ /^.\s*$Ident\s*\(/s) {
2990c2fdda0dSAndy Whitcroft
2991463f2864SAndy Whitcroft			} elsif ($s =~ /^.\s*else\b/s) {
2992463f2864SAndy Whitcroft
2993c45dcabdSAndy Whitcroft			# declarations always start with types
2994d2506586SAndy 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) {
2995c45dcabdSAndy Whitcroft				my $type = $1;
2996c45dcabdSAndy Whitcroft				$type =~ s/\s+/ /g;
2997c45dcabdSAndy Whitcroft				possible($type, "A:" . $s);
2998c45dcabdSAndy Whitcroft
29996c72ffaaSAndy Whitcroft			# definitions in global scope can only start with types
3000a6a84062SAndy Whitcroft			} elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
3001c45dcabdSAndy Whitcroft				possible($1, "B:" . $s);
3002c2fdda0dSAndy Whitcroft			}
30038905a67cSAndy Whitcroft
30046c72ffaaSAndy Whitcroft			# any (foo ... *) is a pointer cast, and foo is a type
300565863862SAndy Whitcroft			while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
3006c45dcabdSAndy Whitcroft				possible($1, "C:" . $s);
30079c0ca6f9SAndy Whitcroft			}
30088905a67cSAndy Whitcroft
30098905a67cSAndy Whitcroft			# Check for any sort of function declaration.
30108905a67cSAndy Whitcroft			# int foo(something bar, other baz);
30118905a67cSAndy Whitcroft			# void (*store_gdt)(x86_descr_ptr *);
3012171ae1a4SAndy Whitcroft			if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
30138905a67cSAndy Whitcroft				my ($name_len) = length($1);
30148905a67cSAndy Whitcroft
3015cf655043SAndy Whitcroft				my $ctx = $s;
3016773647a0SAndy Whitcroft				substr($ctx, 0, $name_len + 1, '');
30178905a67cSAndy Whitcroft				$ctx =~ s/\)[^\)]*$//;
3018cf655043SAndy Whitcroft
30198905a67cSAndy Whitcroft				for my $arg (split(/\s*,\s*/, $ctx)) {
3020c45dcabdSAndy Whitcroft					if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
30218905a67cSAndy Whitcroft
3022c45dcabdSAndy Whitcroft						possible($1, "D:" . $s);
30238905a67cSAndy Whitcroft					}
30248905a67cSAndy Whitcroft				}
30258905a67cSAndy Whitcroft			}
30268905a67cSAndy Whitcroft
30279c0ca6f9SAndy Whitcroft		}
30289c0ca6f9SAndy Whitcroft
302900df344fSAndy Whitcroft#
303000df344fSAndy Whitcroft# Checks which may be anchored in the context.
303100df344fSAndy Whitcroft#
303200df344fSAndy Whitcroft
303300df344fSAndy Whitcroft# Check for switch () and associated case and default
303400df344fSAndy Whitcroft# statements should be at the same indent.
303500df344fSAndy Whitcroft		if ($line=~/\bswitch\s*\(.*\)/) {
303600df344fSAndy Whitcroft			my $err = '';
303700df344fSAndy Whitcroft			my $sep = '';
303800df344fSAndy Whitcroft			my @ctx = ctx_block_outer($linenr, $realcnt);
303900df344fSAndy Whitcroft			shift(@ctx);
304000df344fSAndy Whitcroft			for my $ctx (@ctx) {
304100df344fSAndy Whitcroft				my ($clen, $cindent) = line_stats($ctx);
304200df344fSAndy Whitcroft				if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
304300df344fSAndy Whitcroft							$indent != $cindent) {
304400df344fSAndy Whitcroft					$err .= "$sep$ctx\n";
304500df344fSAndy Whitcroft					$sep = '';
304600df344fSAndy Whitcroft				} else {
304700df344fSAndy Whitcroft					$sep = "[...]\n";
304800df344fSAndy Whitcroft				}
304900df344fSAndy Whitcroft			}
305000df344fSAndy Whitcroft			if ($err ne '') {
3051000d1cc1SJoe Perches				ERROR("SWITCH_CASE_INDENT_LEVEL",
3052000d1cc1SJoe Perches				      "switch and case should be at the same indent\n$hereline$err");
3053de7d4f0eSAndy Whitcroft			}
3054de7d4f0eSAndy Whitcroft		}
3055de7d4f0eSAndy Whitcroft
3056de7d4f0eSAndy Whitcroft# if/while/etc brace do not go on next line, unless defining a do while loop,
3057de7d4f0eSAndy Whitcroft# or if that brace on the next line is for something else
30580fe3dc2bSJoe Perches		if ($line =~ /(.*)\b((?:if|while|for|switch|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
3059773647a0SAndy Whitcroft			my $pre_ctx = "$1$2";
3060773647a0SAndy Whitcroft
30619c0ca6f9SAndy Whitcroft			my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
30628eef05ddSJoe Perches
30638eef05ddSJoe Perches			if ($line =~ /^\+\t{6,}/) {
30648eef05ddSJoe Perches				WARN("DEEP_INDENTATION",
30658eef05ddSJoe Perches				     "Too many leading tabs - consider code refactoring\n" . $herecurr);
30668eef05ddSJoe Perches			}
30678eef05ddSJoe Perches
3068de7d4f0eSAndy Whitcroft			my $ctx_cnt = $realcnt - $#ctx - 1;
3069de7d4f0eSAndy Whitcroft			my $ctx = join("\n", @ctx);
3070de7d4f0eSAndy Whitcroft
3071548596d5SAndy Whitcroft			my $ctx_ln = $linenr;
3072548596d5SAndy Whitcroft			my $ctx_skip = $realcnt;
3073de7d4f0eSAndy Whitcroft
3074548596d5SAndy Whitcroft			while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
3075548596d5SAndy Whitcroft					defined $lines[$ctx_ln - 1] &&
3076548596d5SAndy Whitcroft					$lines[$ctx_ln - 1] =~ /^-/)) {
3077548596d5SAndy Whitcroft				##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
3078548596d5SAndy Whitcroft				$ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
3079773647a0SAndy Whitcroft				$ctx_ln++;
3080773647a0SAndy Whitcroft			}
3081548596d5SAndy Whitcroft
308253210168SAndy Whitcroft			#print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
308353210168SAndy Whitcroft			#print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
3084773647a0SAndy Whitcroft
3085773647a0SAndy Whitcroft			if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
3086000d1cc1SJoe Perches				ERROR("OPEN_BRACE",
3087000d1cc1SJoe Perches				      "that open brace { should be on the previous line\n" .
308801464f30SAndy Whitcroft					"$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
308900df344fSAndy Whitcroft			}
3090773647a0SAndy Whitcroft			if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
3091773647a0SAndy Whitcroft			    $ctx =~ /\)\s*\;\s*$/ &&
3092773647a0SAndy Whitcroft			    defined $lines[$ctx_ln - 1])
3093773647a0SAndy Whitcroft			{
30949c0ca6f9SAndy Whitcroft				my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
30959c0ca6f9SAndy Whitcroft				if ($nindent > $indent) {
3096000d1cc1SJoe Perches					WARN("TRAILING_SEMICOLON",
3097000d1cc1SJoe Perches					     "trailing semicolon indicates no statements, indent implies otherwise\n" .
309801464f30SAndy Whitcroft						"$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
30999c0ca6f9SAndy Whitcroft				}
31009c0ca6f9SAndy Whitcroft			}
310100df344fSAndy Whitcroft		}
310200df344fSAndy Whitcroft
31034d001e4dSAndy Whitcroft# Check relative indent for conditionals and blocks.
31040fe3dc2bSJoe Perches		if ($line =~ /\b(?:(?:if|while|for|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
31053e469cdcSAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
31063e469cdcSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0)
31073e469cdcSAndy Whitcroft					if (!defined $stat);
31084d001e4dSAndy Whitcroft			my ($s, $c) = ($stat, $cond);
31094d001e4dSAndy Whitcroft
31104d001e4dSAndy Whitcroft			substr($s, 0, length($c), '');
31114d001e4dSAndy Whitcroft
31129f5af480SJoe Perches			# remove inline comments
31139f5af480SJoe Perches			$s =~ s/$;/ /g;
31149f5af480SJoe Perches			$c =~ s/$;/ /g;
31154d001e4dSAndy Whitcroft
31164d001e4dSAndy Whitcroft			# Find out how long the conditional actually is.
31176f779c18SAndy Whitcroft			my @newlines = ($c =~ /\n/gs);
31186f779c18SAndy Whitcroft			my $cond_lines = 1 + $#newlines;
31194d001e4dSAndy Whitcroft
31209f5af480SJoe Perches			# Make sure we remove the line prefixes as we have
31219f5af480SJoe Perches			# none on the first line, and are going to readd them
31229f5af480SJoe Perches			# where necessary.
31239f5af480SJoe Perches			$s =~ s/\n./\n/gs;
31249f5af480SJoe Perches			while ($s =~ /\n\s+\\\n/) {
31259f5af480SJoe Perches				$cond_lines += $s =~ s/\n\s+\\\n/\n/g;
31269f5af480SJoe Perches			}
31279f5af480SJoe Perches
31284d001e4dSAndy Whitcroft			# We want to check the first line inside the block
31294d001e4dSAndy Whitcroft			# starting at the end of the conditional, so remove:
31304d001e4dSAndy Whitcroft			#  1) any blank line termination
31314d001e4dSAndy Whitcroft			#  2) any opening brace { on end of the line
31324d001e4dSAndy Whitcroft			#  3) any do (...) {
31334d001e4dSAndy Whitcroft			my $continuation = 0;
31344d001e4dSAndy Whitcroft			my $check = 0;
31354d001e4dSAndy Whitcroft			$s =~ s/^.*\bdo\b//;
31364d001e4dSAndy Whitcroft			$s =~ s/^\s*{//;
31374d001e4dSAndy Whitcroft			if ($s =~ s/^\s*\\//) {
31384d001e4dSAndy Whitcroft				$continuation = 1;
31394d001e4dSAndy Whitcroft			}
31409bd49efeSAndy Whitcroft			if ($s =~ s/^\s*?\n//) {
31414d001e4dSAndy Whitcroft				$check = 1;
31424d001e4dSAndy Whitcroft				$cond_lines++;
31434d001e4dSAndy Whitcroft			}
31444d001e4dSAndy Whitcroft
31454d001e4dSAndy Whitcroft			# Also ignore a loop construct at the end of a
31464d001e4dSAndy Whitcroft			# preprocessor statement.
31474d001e4dSAndy Whitcroft			if (($prevline =~ /^.\s*#\s*define\s/ ||
31484d001e4dSAndy Whitcroft			    $prevline =~ /\\\s*$/) && $continuation == 0) {
31494d001e4dSAndy Whitcroft				$check = 0;
31504d001e4dSAndy Whitcroft			}
31514d001e4dSAndy Whitcroft
31529bd49efeSAndy Whitcroft			my $cond_ptr = -1;
3153740504c6SAndy Whitcroft			$continuation = 0;
31549bd49efeSAndy Whitcroft			while ($cond_ptr != $cond_lines) {
31559bd49efeSAndy Whitcroft				$cond_ptr = $cond_lines;
31564d001e4dSAndy Whitcroft
3157f16fa28fSAndy Whitcroft				# If we see an #else/#elif then the code
3158f16fa28fSAndy Whitcroft				# is not linear.
3159f16fa28fSAndy Whitcroft				if ($s =~ /^\s*\#\s*(?:else|elif)/) {
3160f16fa28fSAndy Whitcroft					$check = 0;
3161f16fa28fSAndy Whitcroft				}
3162f16fa28fSAndy Whitcroft
31639bd49efeSAndy Whitcroft				# Ignore:
31649bd49efeSAndy Whitcroft				#  1) blank lines, they should be at 0,
31659bd49efeSAndy Whitcroft				#  2) preprocessor lines, and
31669bd49efeSAndy Whitcroft				#  3) labels.
3167740504c6SAndy Whitcroft				if ($continuation ||
3168740504c6SAndy Whitcroft				    $s =~ /^\s*?\n/ ||
31699bd49efeSAndy Whitcroft				    $s =~ /^\s*#\s*?/ ||
31709bd49efeSAndy Whitcroft				    $s =~ /^\s*$Ident\s*:/) {
3171740504c6SAndy Whitcroft					$continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
317230dad6ebSAndy Whitcroft					if ($s =~ s/^.*?\n//) {
31739bd49efeSAndy Whitcroft						$cond_lines++;
31749bd49efeSAndy Whitcroft					}
31754d001e4dSAndy Whitcroft				}
317630dad6ebSAndy Whitcroft			}
31774d001e4dSAndy Whitcroft
31784d001e4dSAndy Whitcroft			my (undef, $sindent) = line_stats("+" . $s);
31794d001e4dSAndy Whitcroft			my $stat_real = raw_line($linenr, $cond_lines);
31804d001e4dSAndy Whitcroft
31814d001e4dSAndy Whitcroft			# Check if either of these lines are modified, else
31824d001e4dSAndy Whitcroft			# this is not this patch's fault.
31834d001e4dSAndy Whitcroft			if (!defined($stat_real) ||
31844d001e4dSAndy Whitcroft			    $stat !~ /^\+/ && $stat_real !~ /^\+/) {
31854d001e4dSAndy Whitcroft				$check = 0;
31864d001e4dSAndy Whitcroft			}
31874d001e4dSAndy Whitcroft			if (defined($stat_real) && $cond_lines > 1) {
31884d001e4dSAndy Whitcroft				$stat_real = "[...]\n$stat_real";
31894d001e4dSAndy Whitcroft			}
31904d001e4dSAndy Whitcroft
31919bd49efeSAndy 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";
31924d001e4dSAndy Whitcroft
31939f5af480SJoe Perches			if ($check && $s ne '' &&
31949f5af480SJoe Perches			    (($sindent % 8) != 0 ||
31959f5af480SJoe Perches			     ($sindent < $indent) ||
31969f5af480SJoe Perches			     ($sindent > $indent + 8))) {
3197000d1cc1SJoe Perches				WARN("SUSPECT_CODE_INDENT",
3198000d1cc1SJoe Perches				     "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
31994d001e4dSAndy Whitcroft			}
32004d001e4dSAndy Whitcroft		}
32014d001e4dSAndy Whitcroft
32026c72ffaaSAndy Whitcroft		# Track the 'values' across context and added lines.
32036c72ffaaSAndy Whitcroft		my $opline = $line; $opline =~ s/^./ /;
32041f65f947SAndy Whitcroft		my ($curr_values, $curr_vars) =
32051f65f947SAndy Whitcroft				annotate_values($opline . "\n", $prev_values);
32066c72ffaaSAndy Whitcroft		$curr_values = $prev_values . $curr_values;
3207c2fdda0dSAndy Whitcroft		if ($dbg_values) {
3208c2fdda0dSAndy Whitcroft			my $outline = $opline; $outline =~ s/\t/ /g;
3209cf655043SAndy Whitcroft			print "$linenr > .$outline\n";
3210cf655043SAndy Whitcroft			print "$linenr > $curr_values\n";
32111f65f947SAndy Whitcroft			print "$linenr >  $curr_vars\n";
3212c2fdda0dSAndy Whitcroft		}
32136c72ffaaSAndy Whitcroft		$prev_values = substr($curr_values, -1);
32146c72ffaaSAndy Whitcroft
321500df344fSAndy Whitcroft#ignore lines not being added
32163705ce5bSJoe Perches		next if ($line =~ /^[^\+]/);
321700df344fSAndy Whitcroft
3218653d4876SAndy Whitcroft# TEST: allow direct testing of the type matcher.
32197429c690SAndy Whitcroft		if ($dbg_type) {
32207429c690SAndy Whitcroft			if ($line =~ /^.\s*$Declare\s*$/) {
3221000d1cc1SJoe Perches				ERROR("TEST_TYPE",
3222000d1cc1SJoe Perches				      "TEST: is type\n" . $herecurr);
32237429c690SAndy Whitcroft			} elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
3224000d1cc1SJoe Perches				ERROR("TEST_NOT_TYPE",
3225000d1cc1SJoe Perches				      "TEST: is not type ($1 is)\n". $herecurr);
32267429c690SAndy Whitcroft			}
3227653d4876SAndy Whitcroft			next;
3228653d4876SAndy Whitcroft		}
3229a1ef277eSAndy Whitcroft# TEST: allow direct testing of the attribute matcher.
3230a1ef277eSAndy Whitcroft		if ($dbg_attr) {
32319360b0e5SAndy Whitcroft			if ($line =~ /^.\s*$Modifier\s*$/) {
3232000d1cc1SJoe Perches				ERROR("TEST_ATTR",
3233000d1cc1SJoe Perches				      "TEST: is attr\n" . $herecurr);
32349360b0e5SAndy Whitcroft			} elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
3235000d1cc1SJoe Perches				ERROR("TEST_NOT_ATTR",
3236000d1cc1SJoe Perches				      "TEST: is not attr ($1 is)\n". $herecurr);
3237a1ef277eSAndy Whitcroft			}
3238a1ef277eSAndy Whitcroft			next;
3239a1ef277eSAndy Whitcroft		}
3240653d4876SAndy Whitcroft
3241f0a594c1SAndy Whitcroft# check for initialisation to aggregates open brace on the next line
324299423c20SAndy Whitcroft		if ($line =~ /^.\s*{/ &&
324399423c20SAndy Whitcroft		    $prevline =~ /(?:^|[^=])=\s*$/) {
3244d752fcc8SJoe Perches			if (ERROR("OPEN_BRACE",
3245d752fcc8SJoe Perches				  "that open brace { should be on the previous line\n" . $hereprev) &&
3246f2d7e4d4SJoe Perches			    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
3247f2d7e4d4SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
3248f2d7e4d4SJoe Perches				fix_delete_line($fixlinenr, $rawline);
3249d752fcc8SJoe Perches				my $fixedline = $prevrawline;
3250d752fcc8SJoe Perches				$fixedline =~ s/\s*=\s*$/ = {/;
3251f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
3252d752fcc8SJoe Perches				$fixedline = $line;
3253d752fcc8SJoe Perches				$fixedline =~ s/^(.\s*){\s*/$1/;
3254f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
3255d752fcc8SJoe Perches			}
3256f0a594c1SAndy Whitcroft		}
3257f0a594c1SAndy Whitcroft
325800df344fSAndy Whitcroft#
325900df344fSAndy Whitcroft# Checks which are anchored on the added line.
326000df344fSAndy Whitcroft#
326100df344fSAndy Whitcroft
3262653d4876SAndy Whitcroft# check for malformed paths in #include statements (uses RAW line)
3263c45dcabdSAndy Whitcroft		if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
3264653d4876SAndy Whitcroft			my $path = $1;
3265653d4876SAndy Whitcroft			if ($path =~ m{//}) {
3266000d1cc1SJoe Perches				ERROR("MALFORMED_INCLUDE",
3267495e9d84SJoe Perches				      "malformed #include filename\n" . $herecurr);
3268495e9d84SJoe Perches			}
3269495e9d84SJoe Perches			if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
3270495e9d84SJoe Perches				ERROR("UAPI_INCLUDE",
3271495e9d84SJoe Perches				      "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
3272653d4876SAndy Whitcroft			}
3273653d4876SAndy Whitcroft		}
3274653d4876SAndy Whitcroft
327500df344fSAndy Whitcroft# no C99 // comments
327600df344fSAndy Whitcroft		if ($line =~ m{//}) {
32773705ce5bSJoe Perches			if (ERROR("C99_COMMENTS",
32783705ce5bSJoe Perches				  "do not use C99 // comments\n" . $herecurr) &&
32793705ce5bSJoe Perches			    $fix) {
3280194f66fcSJoe Perches				my $line = $fixed[$fixlinenr];
32813705ce5bSJoe Perches				if ($line =~ /\/\/(.*)$/) {
32823705ce5bSJoe Perches					my $comment = trim($1);
3283194f66fcSJoe Perches					$fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@;
32843705ce5bSJoe Perches				}
32853705ce5bSJoe Perches			}
328600df344fSAndy Whitcroft		}
328700df344fSAndy Whitcroft		# Remove C99 comments.
32880a920b5bSAndy Whitcroft		$line =~ s@//.*@@;
32896c72ffaaSAndy Whitcroft		$opline =~ s@//.*@@;
32900a920b5bSAndy Whitcroft
32912b474a1aSAndy Whitcroft# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
32922b474a1aSAndy Whitcroft# the whole statement.
32932b474a1aSAndy Whitcroft#print "APW <$lines[$realline_next - 1]>\n";
32942b474a1aSAndy Whitcroft		if (defined $realline_next &&
32952b474a1aSAndy Whitcroft		    exists $lines[$realline_next - 1] &&
32962b474a1aSAndy Whitcroft		    !defined $suppress_export{$realline_next} &&
32972b474a1aSAndy Whitcroft		    ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
32982b474a1aSAndy Whitcroft		     $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
32993cbf62dfSAndy Whitcroft			# Handle definitions which produce identifiers with
33003cbf62dfSAndy Whitcroft			# a prefix:
33013cbf62dfSAndy Whitcroft			#   XXX(foo);
33023cbf62dfSAndy Whitcroft			#   EXPORT_SYMBOL(something_foo);
3303653d4876SAndy Whitcroft			my $name = $1;
330487a53877SAndy Whitcroft			if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
33053cbf62dfSAndy Whitcroft			    $name =~ /^${Ident}_$2/) {
33063cbf62dfSAndy Whitcroft#print "FOO C name<$name>\n";
33073cbf62dfSAndy Whitcroft				$suppress_export{$realline_next} = 1;
33083cbf62dfSAndy Whitcroft
33093cbf62dfSAndy Whitcroft			} elsif ($stat !~ /(?:
33102b474a1aSAndy Whitcroft				\n.}\s*$|
331148012058SAndy Whitcroft				^.DEFINE_$Ident\(\Q$name\E\)|
331248012058SAndy Whitcroft				^.DECLARE_$Ident\(\Q$name\E\)|
331348012058SAndy Whitcroft				^.LIST_HEAD\(\Q$name\E\)|
33142b474a1aSAndy Whitcroft				^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
33152b474a1aSAndy Whitcroft				\b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
331648012058SAndy Whitcroft			    )/x) {
33172b474a1aSAndy Whitcroft#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
33182b474a1aSAndy Whitcroft				$suppress_export{$realline_next} = 2;
33192b474a1aSAndy Whitcroft			} else {
33202b474a1aSAndy Whitcroft				$suppress_export{$realline_next} = 1;
33210a920b5bSAndy Whitcroft			}
33220a920b5bSAndy Whitcroft		}
33232b474a1aSAndy Whitcroft		if (!defined $suppress_export{$linenr} &&
33242b474a1aSAndy Whitcroft		    $prevline =~ /^.\s*$/ &&
33252b474a1aSAndy Whitcroft		    ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
33262b474a1aSAndy Whitcroft		     $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
33272b474a1aSAndy Whitcroft#print "FOO B <$lines[$linenr - 1]>\n";
33282b474a1aSAndy Whitcroft			$suppress_export{$linenr} = 2;
33292b474a1aSAndy Whitcroft		}
33302b474a1aSAndy Whitcroft		if (defined $suppress_export{$linenr} &&
33312b474a1aSAndy Whitcroft		    $suppress_export{$linenr} == 2) {
3332000d1cc1SJoe Perches			WARN("EXPORT_SYMBOL",
3333000d1cc1SJoe Perches			     "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
33342b474a1aSAndy Whitcroft		}
33350a920b5bSAndy Whitcroft
33365150bda4SJoe Eloff# check for global initialisers.
33375129e87cSJoe Perches		if ($line =~ /^\+$Type\s*$Ident(?:\s+$Modifier)*\s*=\s*(?:0|NULL|false)\s*;/) {
3338d5e616fcSJoe Perches			if (ERROR("GLOBAL_INITIALISERS",
3339000d1cc1SJoe Perches				  "do not initialise globals to 0 or NULL\n" .
3340d5e616fcSJoe Perches				      $herecurr) &&
3341d5e616fcSJoe Perches			    $fix) {
33425129e87cSJoe Perches				$fixed[$fixlinenr] =~ s/(^.$Type\s*$Ident(?:\s+$Modifier)*)\s*=\s*(0|NULL|false)\s*;/$1;/;
3343d5e616fcSJoe Perches			}
3344f0a594c1SAndy Whitcroft		}
33450a920b5bSAndy Whitcroft# check for static initialisers.
3346d5e616fcSJoe Perches		if ($line =~ /^\+.*\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
3347d5e616fcSJoe Perches			if (ERROR("INITIALISED_STATIC",
3348000d1cc1SJoe Perches				  "do not initialise statics to 0 or NULL\n" .
3349d5e616fcSJoe Perches				      $herecurr) &&
3350d5e616fcSJoe Perches			    $fix) {
3351194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*(0|NULL|false)\s*;/$1;/;
3352d5e616fcSJoe Perches			}
33530a920b5bSAndy Whitcroft		}
33540a920b5bSAndy Whitcroft
33551813087dSJoe Perches# check for misordered declarations of char/short/int/long with signed/unsigned
33561813087dSJoe Perches		while ($sline =~ m{(\b$TypeMisordered\b)}g) {
33571813087dSJoe Perches			my $tmp = trim($1);
33581813087dSJoe Perches			WARN("MISORDERED_TYPE",
33591813087dSJoe Perches			     "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr);
33601813087dSJoe Perches		}
33611813087dSJoe Perches
3362cb710ecaSJoe Perches# check for static const char * arrays.
3363cb710ecaSJoe Perches		if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
3364000d1cc1SJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
3365000d1cc1SJoe Perches			     "static const char * array should probably be static const char * const\n" .
3366cb710ecaSJoe Perches				$herecurr);
3367cb710ecaSJoe Perches               }
3368cb710ecaSJoe Perches
3369cb710ecaSJoe Perches# check for static char foo[] = "bar" declarations.
3370cb710ecaSJoe Perches		if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
3371000d1cc1SJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
3372000d1cc1SJoe Perches			     "static char array declaration should probably be static const char\n" .
3373cb710ecaSJoe Perches				$herecurr);
3374cb710ecaSJoe Perches               }
3375cb710ecaSJoe Perches
3376ab7e23f3SJoe Perches# check for const <foo> const where <foo> is not a pointer or array type
3377ab7e23f3SJoe Perches		if ($sline =~ /\bconst\s+($BasicType)\s+const\b/) {
3378ab7e23f3SJoe Perches			my $found = $1;
3379ab7e23f3SJoe Perches			if ($sline =~ /\bconst\s+\Q$found\E\s+const\b\s*\*/) {
3380ab7e23f3SJoe Perches				WARN("CONST_CONST",
3381ab7e23f3SJoe Perches				     "'const $found const *' should probably be 'const $found * const'\n" . $herecurr);
3382ab7e23f3SJoe Perches			} elsif ($sline !~ /\bconst\s+\Q$found\E\s+const\s+\w+\s*\[/) {
3383ab7e23f3SJoe Perches				WARN("CONST_CONST",
3384ab7e23f3SJoe Perches				     "'const $found const' should probably be 'const $found'\n" . $herecurr);
3385ab7e23f3SJoe Perches			}
3386ab7e23f3SJoe Perches		}
3387ab7e23f3SJoe Perches
33889b0fa60dSJoe Perches# check for non-global char *foo[] = {"bar", ...} declarations.
33899b0fa60dSJoe Perches		if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) {
33909b0fa60dSJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
33919b0fa60dSJoe Perches			     "char * array declaration might be better as static const\n" .
33929b0fa60dSJoe Perches				$herecurr);
33939b0fa60dSJoe Perches               }
33949b0fa60dSJoe Perches
3395b598b670SJoe Perches# check for sizeof(foo)/sizeof(foo[0]) that could be ARRAY_SIZE(foo)
3396b598b670SJoe Perches		if ($line =~ m@\bsizeof\s*\(\s*($Lval)\s*\)@) {
3397b598b670SJoe Perches			my $array = $1;
3398b598b670SJoe Perches			if ($line =~ m@\b(sizeof\s*\(\s*\Q$array\E\s*\)\s*/\s*sizeof\s*\(\s*\Q$array\E\s*\[\s*0\s*\]\s*\))@) {
3399b598b670SJoe Perches				my $array_div = $1;
3400b598b670SJoe Perches				if (WARN("ARRAY_SIZE",
3401b598b670SJoe Perches					 "Prefer ARRAY_SIZE($array)\n" . $herecurr) &&
3402b598b670SJoe Perches				    $fix) {
3403b598b670SJoe Perches					$fixed[$fixlinenr] =~ s/\Q$array_div\E/ARRAY_SIZE($array)/;
3404b598b670SJoe Perches				}
3405b598b670SJoe Perches			}
3406b598b670SJoe Perches		}
3407b598b670SJoe Perches
3408b36190c5SJoe Perches# check for function declarations without arguments like "int foo()"
3409b36190c5SJoe Perches		if ($line =~ /(\b$Type\s+$Ident)\s*\(\s*\)/) {
3410b36190c5SJoe Perches			if (ERROR("FUNCTION_WITHOUT_ARGS",
3411b36190c5SJoe Perches				  "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) &&
3412b36190c5SJoe Perches			    $fix) {
3413194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/;
3414b36190c5SJoe Perches			}
3415b36190c5SJoe Perches		}
3416b36190c5SJoe Perches
341792e112fdSJoe Perches# check for uses of DEFINE_PCI_DEVICE_TABLE
341892e112fdSJoe Perches		if ($line =~ /\bDEFINE_PCI_DEVICE_TABLE\s*\(\s*(\w+)\s*\)\s*=/) {
341992e112fdSJoe Perches			if (WARN("DEFINE_PCI_DEVICE_TABLE",
342092e112fdSJoe Perches				 "Prefer struct pci_device_id over deprecated DEFINE_PCI_DEVICE_TABLE\n" . $herecurr) &&
342192e112fdSJoe Perches			    $fix) {
3422194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b(?:static\s+|)DEFINE_PCI_DEVICE_TABLE\s*\(\s*(\w+)\s*\)\s*=\s*/static const struct pci_device_id $1\[\] = /;
342392e112fdSJoe Perches			}
342493ed0e2dSJoe Perches		}
342593ed0e2dSJoe Perches
3426653d4876SAndy Whitcroft# check for new typedefs, only function parameters and sparse annotations
3427653d4876SAndy Whitcroft# make sense.
3428653d4876SAndy Whitcroft		if ($line =~ /\btypedef\s/ &&
34298054576dSAndy Whitcroft		    $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
3430c45dcabdSAndy Whitcroft		    $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
34318ed22cadSAndy Whitcroft		    $line !~ /\b$typeTypedefs\b/ &&
3432653d4876SAndy Whitcroft		    $line !~ /\b__bitwise(?:__|)\b/) {
3433000d1cc1SJoe Perches			WARN("NEW_TYPEDEFS",
3434000d1cc1SJoe Perches			     "do not add new typedefs\n" . $herecurr);
34350a920b5bSAndy Whitcroft		}
34360a920b5bSAndy Whitcroft
34370a920b5bSAndy Whitcroft# * goes on variable not on type
343865863862SAndy Whitcroft		# (char*[ const])
3439bfcb2cc7SAndy Whitcroft		while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
3440bfcb2cc7SAndy Whitcroft			#print "AA<$1>\n";
34413705ce5bSJoe Perches			my ($ident, $from, $to) = ($1, $2, $2);
3442d8aaf121SAndy Whitcroft
344365863862SAndy Whitcroft			# Should start with a space.
344465863862SAndy Whitcroft			$to =~ s/^(\S)/ $1/;
344565863862SAndy Whitcroft			# Should not end with a space.
344665863862SAndy Whitcroft			$to =~ s/\s+$//;
344765863862SAndy Whitcroft			# '*'s should not have spaces between.
3448f9a0b3d1SAndy Whitcroft			while ($to =~ s/\*\s+\*/\*\*/) {
344965863862SAndy Whitcroft			}
3450d8aaf121SAndy Whitcroft
34513705ce5bSJoe Perches##			print "1: from<$from> to<$to> ident<$ident>\n";
345265863862SAndy Whitcroft			if ($from ne $to) {
34533705ce5bSJoe Perches				if (ERROR("POINTER_LOCATION",
34543705ce5bSJoe Perches					  "\"(foo$from)\" should be \"(foo$to)\"\n" .  $herecurr) &&
34553705ce5bSJoe Perches				    $fix) {
34563705ce5bSJoe Perches					my $sub_from = $ident;
34573705ce5bSJoe Perches					my $sub_to = $ident;
34583705ce5bSJoe Perches					$sub_to =~ s/\Q$from\E/$to/;
3459194f66fcSJoe Perches					$fixed[$fixlinenr] =~
34603705ce5bSJoe Perches					    s@\Q$sub_from\E@$sub_to@;
34613705ce5bSJoe Perches				}
346265863862SAndy Whitcroft			}
3463bfcb2cc7SAndy Whitcroft		}
3464bfcb2cc7SAndy Whitcroft		while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
3465bfcb2cc7SAndy Whitcroft			#print "BB<$1>\n";
34663705ce5bSJoe Perches			my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
3467d8aaf121SAndy Whitcroft
346865863862SAndy Whitcroft			# Should start with a space.
346965863862SAndy Whitcroft			$to =~ s/^(\S)/ $1/;
347065863862SAndy Whitcroft			# Should not end with a space.
347165863862SAndy Whitcroft			$to =~ s/\s+$//;
347265863862SAndy Whitcroft			# '*'s should not have spaces between.
3473f9a0b3d1SAndy Whitcroft			while ($to =~ s/\*\s+\*/\*\*/) {
347465863862SAndy Whitcroft			}
347565863862SAndy Whitcroft			# Modifiers should have spaces.
347665863862SAndy Whitcroft			$to =~ s/(\b$Modifier$)/$1 /;
347765863862SAndy Whitcroft
34783705ce5bSJoe Perches##			print "2: from<$from> to<$to> ident<$ident>\n";
3479667026e7SAndy Whitcroft			if ($from ne $to && $ident !~ /^$Modifier$/) {
34803705ce5bSJoe Perches				if (ERROR("POINTER_LOCATION",
34813705ce5bSJoe Perches					  "\"foo${from}bar\" should be \"foo${to}bar\"\n" .  $herecurr) &&
34823705ce5bSJoe Perches				    $fix) {
34833705ce5bSJoe Perches
34843705ce5bSJoe Perches					my $sub_from = $match;
34853705ce5bSJoe Perches					my $sub_to = $match;
34863705ce5bSJoe Perches					$sub_to =~ s/\Q$from\E/$to/;
3487194f66fcSJoe Perches					$fixed[$fixlinenr] =~
34883705ce5bSJoe Perches					    s@\Q$sub_from\E@$sub_to@;
34893705ce5bSJoe Perches				}
349065863862SAndy Whitcroft			}
34910a920b5bSAndy Whitcroft		}
34920a920b5bSAndy Whitcroft
34939d3e3c70SJoe Perches# avoid BUG() or BUG_ON()
34949d3e3c70SJoe Perches		if ($line =~ /\b(?:BUG|BUG_ON)\b/) {
34959d3e3c70SJoe Perches			my $msg_type = \&WARN;
34969d3e3c70SJoe Perches			$msg_type = \&CHK if ($file);
34979d3e3c70SJoe Perches			&{$msg_type}("AVOID_BUG",
34989d3e3c70SJoe Perches				     "Avoid crashing the kernel - try using WARN_ON & recovery code rather than BUG() or BUG_ON()\n" . $herecurr);
34999d3e3c70SJoe Perches		}
35000a920b5bSAndy Whitcroft
35019d3e3c70SJoe Perches# avoid LINUX_VERSION_CODE
35028905a67cSAndy Whitcroft		if ($line =~ /\bLINUX_VERSION_CODE\b/) {
3503000d1cc1SJoe Perches			WARN("LINUX_VERSION_CODE",
3504000d1cc1SJoe Perches			     "LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
35058905a67cSAndy Whitcroft		}
35068905a67cSAndy Whitcroft
350717441227SJoe Perches# check for uses of printk_ratelimit
350817441227SJoe Perches		if ($line =~ /\bprintk_ratelimit\s*\(/) {
3509000d1cc1SJoe Perches			WARN("PRINTK_RATELIMITED",
3510000d1cc1SJoe Perches			     "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
351117441227SJoe Perches		}
351217441227SJoe Perches
351300df344fSAndy Whitcroft# printk should use KERN_* levels.  Note that follow on printk's on the
351400df344fSAndy Whitcroft# same line do not need a level, so we use the current block context
351500df344fSAndy Whitcroft# to try and find and validate the current printk.  In summary the current
351625985edcSLucas De Marchi# printk includes all preceding printk's which have no newline on the end.
351700df344fSAndy Whitcroft# we assume the first bad printk is the one to report.
3518f0a594c1SAndy Whitcroft		if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
351900df344fSAndy Whitcroft			my $ok = 0;
352000df344fSAndy Whitcroft			for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
352100df344fSAndy Whitcroft				#print "CHECK<$lines[$ln - 1]\n";
352225985edcSLucas De Marchi				# we have a preceding printk if it ends
352300df344fSAndy Whitcroft				# with "\n" ignore it, else it is to blame
352400df344fSAndy Whitcroft				if ($lines[$ln - 1] =~ m{\bprintk\(}) {
352500df344fSAndy Whitcroft					if ($rawlines[$ln - 1] !~ m{\\n"}) {
352600df344fSAndy Whitcroft						$ok = 1;
352700df344fSAndy Whitcroft					}
352800df344fSAndy Whitcroft					last;
352900df344fSAndy Whitcroft				}
353000df344fSAndy Whitcroft			}
353100df344fSAndy Whitcroft			if ($ok == 0) {
3532000d1cc1SJoe Perches				WARN("PRINTK_WITHOUT_KERN_LEVEL",
3533000d1cc1SJoe Perches				     "printk() should include KERN_ facility level\n" . $herecurr);
35340a920b5bSAndy Whitcroft			}
353500df344fSAndy Whitcroft		}
35360a920b5bSAndy Whitcroft
3537243f3803SJoe Perches		if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
3538243f3803SJoe Perches			my $orig = $1;
3539243f3803SJoe Perches			my $level = lc($orig);
3540243f3803SJoe Perches			$level = "warn" if ($level eq "warning");
35418f26b837SJoe Perches			my $level2 = $level;
35428f26b837SJoe Perches			$level2 = "dbg" if ($level eq "debug");
3543243f3803SJoe Perches			WARN("PREFER_PR_LEVEL",
3544daa8b059SYogesh Chaudhari			     "Prefer [subsystem eg: netdev]_$level2([subsystem]dev, ... then dev_$level2(dev, ... then pr_$level(...  to printk(KERN_$orig ...\n" . $herecurr);
3545243f3803SJoe Perches		}
3546243f3803SJoe Perches
3547243f3803SJoe Perches		if ($line =~ /\bpr_warning\s*\(/) {
3548d5e616fcSJoe Perches			if (WARN("PREFER_PR_LEVEL",
3549d5e616fcSJoe Perches				 "Prefer pr_warn(... to pr_warning(...\n" . $herecurr) &&
3550d5e616fcSJoe Perches			    $fix) {
3551194f66fcSJoe Perches				$fixed[$fixlinenr] =~
3552d5e616fcSJoe Perches				    s/\bpr_warning\b/pr_warn/;
3553d5e616fcSJoe Perches			}
3554243f3803SJoe Perches		}
3555243f3803SJoe Perches
3556dc139313SJoe Perches		if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
3557dc139313SJoe Perches			my $orig = $1;
3558dc139313SJoe Perches			my $level = lc($orig);
3559dc139313SJoe Perches			$level = "warn" if ($level eq "warning");
3560dc139313SJoe Perches			$level = "dbg" if ($level eq "debug");
3561dc139313SJoe Perches			WARN("PREFER_DEV_LEVEL",
3562dc139313SJoe Perches			     "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
3563dc139313SJoe Perches		}
3564dc139313SJoe Perches
356591c9afafSAndy Lutomirski# ENOSYS means "bad syscall nr" and nothing else.  This will have a small
356691c9afafSAndy Lutomirski# number of false positives, but assembly files are not checked, so at
356791c9afafSAndy Lutomirski# least the arch entry code will not trigger this warning.
356891c9afafSAndy Lutomirski		if ($line =~ /\bENOSYS\b/) {
356991c9afafSAndy Lutomirski			WARN("ENOSYS",
357091c9afafSAndy Lutomirski			     "ENOSYS means 'invalid syscall nr' and nothing else\n" . $herecurr);
357191c9afafSAndy Lutomirski		}
357291c9afafSAndy Lutomirski
3573653d4876SAndy Whitcroft# function brace can't be on same line, except for #defines of do while,
3574653d4876SAndy Whitcroft# or if closed on same line
35758d182478SJoe Perches		if (($line=~/$Type\s*$Ident\(.*\).*\s*{/) and
35764e5d56bdSEddie Kovsky		    !($line=~/\#\s*define.*do\s\{/) and !($line=~/}/)) {
35778d182478SJoe Perches			if (ERROR("OPEN_BRACE",
35788d182478SJoe Perches				  "open brace '{' following function declarations go on the next line\n" . $herecurr) &&
35798d182478SJoe Perches			    $fix) {
35808d182478SJoe Perches				fix_delete_line($fixlinenr, $rawline);
35818d182478SJoe Perches				my $fixed_line = $rawline;
35828d182478SJoe Perches				$fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*){(.*)$/;
35838d182478SJoe Perches				my $line1 = $1;
35848d182478SJoe Perches				my $line2 = $2;
35858d182478SJoe Perches				fix_insert_line($fixlinenr, ltrim($line1));
35868d182478SJoe Perches				fix_insert_line($fixlinenr, "\+{");
35878d182478SJoe Perches				if ($line2 !~ /^\s*$/) {
35888d182478SJoe Perches					fix_insert_line($fixlinenr, "\+\t" . trim($line2));
35898d182478SJoe Perches				}
35908d182478SJoe Perches			}
35910a920b5bSAndy Whitcroft		}
3592653d4876SAndy Whitcroft
35938905a67cSAndy Whitcroft# open braces for enum, union and struct go on the same line.
35948905a67cSAndy Whitcroft		if ($line =~ /^.\s*{/ &&
35958905a67cSAndy Whitcroft		    $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
35968d182478SJoe Perches			if (ERROR("OPEN_BRACE",
35978d182478SJoe Perches				  "open brace '{' following $1 go on the same line\n" . $hereprev) &&
35988d182478SJoe Perches			    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
35998d182478SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
36008d182478SJoe Perches				fix_delete_line($fixlinenr, $rawline);
36018d182478SJoe Perches				my $fixedline = rtrim($prevrawline) . " {";
36028d182478SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
36038d182478SJoe Perches				$fixedline = $rawline;
36048d182478SJoe Perches				$fixedline =~ s/^(.\s*){\s*/$1\t/;
36058d182478SJoe Perches				if ($fixedline !~ /^\+\s*$/) {
36068d182478SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
36078d182478SJoe Perches				}
36088d182478SJoe Perches			}
36098905a67cSAndy Whitcroft		}
36108905a67cSAndy Whitcroft
36110c73b4ebSAndy Whitcroft# missing space after union, struct or enum definition
36123705ce5bSJoe Perches		if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
36133705ce5bSJoe Perches			if (WARN("SPACING",
36143705ce5bSJoe Perches				 "missing space after $1 definition\n" . $herecurr) &&
36153705ce5bSJoe Perches			    $fix) {
3616194f66fcSJoe Perches				$fixed[$fixlinenr] =~
36173705ce5bSJoe Perches				    s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
36183705ce5bSJoe Perches			}
36190c73b4ebSAndy Whitcroft		}
36200c73b4ebSAndy Whitcroft
362131070b5dSJoe Perches# Function pointer declarations
362231070b5dSJoe Perches# check spacing between type, funcptr, and args
362331070b5dSJoe Perches# canonical declaration is "type (*funcptr)(args...)"
362491f72e9cSJoe Perches		if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) {
362531070b5dSJoe Perches			my $declare = $1;
362631070b5dSJoe Perches			my $pre_pointer_space = $2;
362731070b5dSJoe Perches			my $post_pointer_space = $3;
362831070b5dSJoe Perches			my $funcname = $4;
362931070b5dSJoe Perches			my $post_funcname_space = $5;
363031070b5dSJoe Perches			my $pre_args_space = $6;
363131070b5dSJoe Perches
363291f72e9cSJoe Perches# the $Declare variable will capture all spaces after the type
363391f72e9cSJoe Perches# so check it for a missing trailing missing space but pointer return types
363491f72e9cSJoe Perches# don't need a space so don't warn for those.
363591f72e9cSJoe Perches			my $post_declare_space = "";
363691f72e9cSJoe Perches			if ($declare =~ /(\s+)$/) {
363791f72e9cSJoe Perches				$post_declare_space = $1;
363891f72e9cSJoe Perches				$declare = rtrim($declare);
363991f72e9cSJoe Perches			}
364091f72e9cSJoe Perches			if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) {
364131070b5dSJoe Perches				WARN("SPACING",
364231070b5dSJoe Perches				     "missing space after return type\n" . $herecurr);
364391f72e9cSJoe Perches				$post_declare_space = " ";
364431070b5dSJoe Perches			}
364531070b5dSJoe Perches
364631070b5dSJoe Perches# unnecessary space "type  (*funcptr)(args...)"
364791f72e9cSJoe Perches# This test is not currently implemented because these declarations are
364891f72e9cSJoe Perches# equivalent to
364991f72e9cSJoe Perches#	int  foo(int bar, ...)
365091f72e9cSJoe Perches# and this is form shouldn't/doesn't generate a checkpatch warning.
365191f72e9cSJoe Perches#
365291f72e9cSJoe Perches#			elsif ($declare =~ /\s{2,}$/) {
365391f72e9cSJoe Perches#				WARN("SPACING",
365491f72e9cSJoe Perches#				     "Multiple spaces after return type\n" . $herecurr);
365591f72e9cSJoe Perches#			}
365631070b5dSJoe Perches
365731070b5dSJoe Perches# unnecessary space "type ( *funcptr)(args...)"
365831070b5dSJoe Perches			if (defined $pre_pointer_space &&
365931070b5dSJoe Perches			    $pre_pointer_space =~ /^\s/) {
366031070b5dSJoe Perches				WARN("SPACING",
366131070b5dSJoe Perches				     "Unnecessary space after function pointer open parenthesis\n" . $herecurr);
366231070b5dSJoe Perches			}
366331070b5dSJoe Perches
366431070b5dSJoe Perches# unnecessary space "type (* funcptr)(args...)"
366531070b5dSJoe Perches			if (defined $post_pointer_space &&
366631070b5dSJoe Perches			    $post_pointer_space =~ /^\s/) {
366731070b5dSJoe Perches				WARN("SPACING",
366831070b5dSJoe Perches				     "Unnecessary space before function pointer name\n" . $herecurr);
366931070b5dSJoe Perches			}
367031070b5dSJoe Perches
367131070b5dSJoe Perches# unnecessary space "type (*funcptr )(args...)"
367231070b5dSJoe Perches			if (defined $post_funcname_space &&
367331070b5dSJoe Perches			    $post_funcname_space =~ /^\s/) {
367431070b5dSJoe Perches				WARN("SPACING",
367531070b5dSJoe Perches				     "Unnecessary space after function pointer name\n" . $herecurr);
367631070b5dSJoe Perches			}
367731070b5dSJoe Perches
367831070b5dSJoe Perches# unnecessary space "type (*funcptr) (args...)"
367931070b5dSJoe Perches			if (defined $pre_args_space &&
368031070b5dSJoe Perches			    $pre_args_space =~ /^\s/) {
368131070b5dSJoe Perches				WARN("SPACING",
368231070b5dSJoe Perches				     "Unnecessary space before function pointer arguments\n" . $herecurr);
368331070b5dSJoe Perches			}
368431070b5dSJoe Perches
368531070b5dSJoe Perches			if (show_type("SPACING") && $fix) {
3686194f66fcSJoe Perches				$fixed[$fixlinenr] =~
368791f72e9cSJoe Perches				    s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex;
368831070b5dSJoe Perches			}
368931070b5dSJoe Perches		}
369031070b5dSJoe Perches
36918d31cfceSAndy Whitcroft# check for spacing round square brackets; allowed:
36928d31cfceSAndy Whitcroft#  1. with a type on the left -- int [] a;
3693fe2a7dbcSAndy Whitcroft#  2. at the beginning of a line for slice initialisers -- [0...10] = 5,
3694fe2a7dbcSAndy Whitcroft#  3. inside a curly brace -- = { [0...10] = 5 }
36958d31cfceSAndy Whitcroft		while ($line =~ /(.*?\s)\[/g) {
36968d31cfceSAndy Whitcroft			my ($where, $prefix) = ($-[1], $1);
36978d31cfceSAndy Whitcroft			if ($prefix !~ /$Type\s+$/ &&
3698fe2a7dbcSAndy Whitcroft			    ($where != 0 || $prefix !~ /^.\s+$/) &&
3699daebc534SAndy Whitcroft			    $prefix !~ /[{,]\s+$/) {
37003705ce5bSJoe Perches				if (ERROR("BRACKET_SPACE",
37013705ce5bSJoe Perches					  "space prohibited before open square bracket '['\n" . $herecurr) &&
37023705ce5bSJoe Perches				    $fix) {
3703194f66fcSJoe Perches				    $fixed[$fixlinenr] =~
37043705ce5bSJoe Perches					s/^(\+.*?)\s+\[/$1\[/;
37053705ce5bSJoe Perches				}
37068d31cfceSAndy Whitcroft			}
37078d31cfceSAndy Whitcroft		}
37088d31cfceSAndy Whitcroft
3709f0a594c1SAndy Whitcroft# check for spaces between functions and their parentheses.
37106c72ffaaSAndy Whitcroft		while ($line =~ /($Ident)\s+\(/g) {
3711c2fdda0dSAndy Whitcroft			my $name = $1;
3712773647a0SAndy Whitcroft			my $ctx_before = substr($line, 0, $-[1]);
3713773647a0SAndy Whitcroft			my $ctx = "$ctx_before$name";
3714c2fdda0dSAndy Whitcroft
3715c2fdda0dSAndy Whitcroft			# Ignore those directives where spaces _are_ permitted.
3716773647a0SAndy Whitcroft			if ($name =~ /^(?:
3717773647a0SAndy Whitcroft				if|for|while|switch|return|case|
3718773647a0SAndy Whitcroft				volatile|__volatile__|
3719773647a0SAndy Whitcroft				__attribute__|format|__extension__|
3720773647a0SAndy Whitcroft				asm|__asm__)$/x)
3721773647a0SAndy Whitcroft			{
3722c2fdda0dSAndy Whitcroft			# cpp #define statements have non-optional spaces, ie
3723c2fdda0dSAndy Whitcroft			# if there is a space between the name and the open
3724c2fdda0dSAndy Whitcroft			# parenthesis it is simply not a parameter group.
3725c45dcabdSAndy Whitcroft			} elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
3726773647a0SAndy Whitcroft
3727773647a0SAndy Whitcroft			# cpp #elif statement condition may start with a (
3728c45dcabdSAndy Whitcroft			} elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
3729c2fdda0dSAndy Whitcroft
3730c2fdda0dSAndy Whitcroft			# If this whole things ends with a type its most
3731c2fdda0dSAndy Whitcroft			# likely a typedef for a function.
3732773647a0SAndy Whitcroft			} elsif ($ctx =~ /$Type$/) {
3733c2fdda0dSAndy Whitcroft
3734c2fdda0dSAndy Whitcroft			} else {
37353705ce5bSJoe Perches				if (WARN("SPACING",
37363705ce5bSJoe Perches					 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
37373705ce5bSJoe Perches					     $fix) {
3738194f66fcSJoe Perches					$fixed[$fixlinenr] =~
37393705ce5bSJoe Perches					    s/\b$name\s+\(/$name\(/;
37403705ce5bSJoe Perches				}
3741f0a594c1SAndy Whitcroft			}
37426c72ffaaSAndy Whitcroft		}
37439a4cad4eSEric Nelson
3744653d4876SAndy Whitcroft# Check operator spacing.
37450a920b5bSAndy Whitcroft		if (!($line=~/\#\s*include/)) {
37463705ce5bSJoe Perches			my $fixed_line = "";
37473705ce5bSJoe Perches			my $line_fixed = 0;
37483705ce5bSJoe Perches
37499c0ca6f9SAndy Whitcroft			my $ops = qr{
37509c0ca6f9SAndy Whitcroft				<<=|>>=|<=|>=|==|!=|
37519c0ca6f9SAndy Whitcroft				\+=|-=|\*=|\/=|%=|\^=|\|=|&=|
37529c0ca6f9SAndy Whitcroft				=>|->|<<|>>|<|>|=|!|~|
37531f65f947SAndy Whitcroft				&&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
375484731623SJoe Perches				\?:|\?|:
37559c0ca6f9SAndy Whitcroft			}x;
3756cf655043SAndy Whitcroft			my @elements = split(/($ops|;)/, $opline);
37573705ce5bSJoe Perches
37583705ce5bSJoe Perches##			print("element count: <" . $#elements . ">\n");
37593705ce5bSJoe Perches##			foreach my $el (@elements) {
37603705ce5bSJoe Perches##				print("el: <$el>\n");
37613705ce5bSJoe Perches##			}
37623705ce5bSJoe Perches
37633705ce5bSJoe Perches			my @fix_elements = ();
376400df344fSAndy Whitcroft			my $off = 0;
37656c72ffaaSAndy Whitcroft
37663705ce5bSJoe Perches			foreach my $el (@elements) {
37673705ce5bSJoe Perches				push(@fix_elements, substr($rawline, $off, length($el)));
37683705ce5bSJoe Perches				$off += length($el);
37693705ce5bSJoe Perches			}
37703705ce5bSJoe Perches
37713705ce5bSJoe Perches			$off = 0;
37723705ce5bSJoe Perches
37736c72ffaaSAndy Whitcroft			my $blank = copy_spacing($opline);
3774b34c648bSJoe Perches			my $last_after = -1;
37756c72ffaaSAndy Whitcroft
37760a920b5bSAndy Whitcroft			for (my $n = 0; $n < $#elements; $n += 2) {
37773705ce5bSJoe Perches
37783705ce5bSJoe Perches				my $good = $fix_elements[$n] . $fix_elements[$n + 1];
37793705ce5bSJoe Perches
37803705ce5bSJoe Perches##				print("n: <$n> good: <$good>\n");
37813705ce5bSJoe Perches
37824a0df2efSAndy Whitcroft				$off += length($elements[$n]);
37834a0df2efSAndy Whitcroft
378425985edcSLucas De Marchi				# Pick up the preceding and succeeding characters.
3785773647a0SAndy Whitcroft				my $ca = substr($opline, 0, $off);
3786773647a0SAndy Whitcroft				my $cc = '';
3787773647a0SAndy Whitcroft				if (length($opline) >= ($off + length($elements[$n + 1]))) {
3788773647a0SAndy Whitcroft					$cc = substr($opline, $off + length($elements[$n + 1]));
3789773647a0SAndy Whitcroft				}
3790773647a0SAndy Whitcroft				my $cb = "$ca$;$cc";
3791773647a0SAndy Whitcroft
37924a0df2efSAndy Whitcroft				my $a = '';
37934a0df2efSAndy Whitcroft				$a = 'V' if ($elements[$n] ne '');
37944a0df2efSAndy Whitcroft				$a = 'W' if ($elements[$n] =~ /\s$/);
3795cf655043SAndy Whitcroft				$a = 'C' if ($elements[$n] =~ /$;$/);
37964a0df2efSAndy Whitcroft				$a = 'B' if ($elements[$n] =~ /(\[|\()$/);
37974a0df2efSAndy Whitcroft				$a = 'O' if ($elements[$n] eq '');
3798773647a0SAndy Whitcroft				$a = 'E' if ($ca =~ /^\s*$/);
37994a0df2efSAndy Whitcroft
38000a920b5bSAndy Whitcroft				my $op = $elements[$n + 1];
38014a0df2efSAndy Whitcroft
38024a0df2efSAndy Whitcroft				my $c = '';
38030a920b5bSAndy Whitcroft				if (defined $elements[$n + 2]) {
38044a0df2efSAndy Whitcroft					$c = 'V' if ($elements[$n + 2] ne '');
38054a0df2efSAndy Whitcroft					$c = 'W' if ($elements[$n + 2] =~ /^\s/);
3806cf655043SAndy Whitcroft					$c = 'C' if ($elements[$n + 2] =~ /^$;/);
38074a0df2efSAndy Whitcroft					$c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
38084a0df2efSAndy Whitcroft					$c = 'O' if ($elements[$n + 2] eq '');
38098b1b3378SAndy Whitcroft					$c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
38104a0df2efSAndy Whitcroft				} else {
38114a0df2efSAndy Whitcroft					$c = 'E';
38120a920b5bSAndy Whitcroft				}
38130a920b5bSAndy Whitcroft
38144a0df2efSAndy Whitcroft				my $ctx = "${a}x${c}";
38154a0df2efSAndy Whitcroft
38164a0df2efSAndy Whitcroft				my $at = "(ctx:$ctx)";
38174a0df2efSAndy Whitcroft
38186c72ffaaSAndy Whitcroft				my $ptr = substr($blank, 0, $off) . "^";
3819de7d4f0eSAndy Whitcroft				my $hereptr = "$hereline$ptr\n";
38200a920b5bSAndy Whitcroft
382174048ed8SAndy Whitcroft				# Pull out the value of this operator.
38226c72ffaaSAndy Whitcroft				my $op_type = substr($curr_values, $off + 1, 1);
38230a920b5bSAndy Whitcroft
38241f65f947SAndy Whitcroft				# Get the full operator variant.
38251f65f947SAndy Whitcroft				my $opv = $op . substr($curr_vars, $off, 1);
38261f65f947SAndy Whitcroft
382713214adfSAndy Whitcroft				# Ignore operators passed as parameters.
382813214adfSAndy Whitcroft				if ($op_type ne 'V' &&
3829d7fe8065SSam Bobroff				    $ca =~ /\s$/ && $cc =~ /^\s*[,\)]/) {
383013214adfSAndy Whitcroft
3831cf655043SAndy Whitcroft#				# Ignore comments
3832cf655043SAndy Whitcroft#				} elsif ($op =~ /^$;+$/) {
383313214adfSAndy Whitcroft
3834d8aaf121SAndy Whitcroft				# ; should have either the end of line or a space or \ after it
383513214adfSAndy Whitcroft				} elsif ($op eq ';') {
3836cf655043SAndy Whitcroft					if ($ctx !~ /.x[WEBC]/ &&
3837cf655043SAndy Whitcroft					    $cc !~ /^\\/ && $cc !~ /^;/) {
38383705ce5bSJoe Perches						if (ERROR("SPACING",
38393705ce5bSJoe Perches							  "space required after that '$op' $at\n" . $hereptr)) {
3840b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
38413705ce5bSJoe Perches							$line_fixed = 1;
38423705ce5bSJoe Perches						}
3843d8aaf121SAndy Whitcroft					}
3844d8aaf121SAndy Whitcroft
3845d8aaf121SAndy Whitcroft				# // is a comment
3846d8aaf121SAndy Whitcroft				} elsif ($op eq '//') {
38470a920b5bSAndy Whitcroft
3848b00e4814SJoe Perches				#   :   when part of a bitfield
3849b00e4814SJoe Perches				} elsif ($opv eq ':B') {
3850b00e4814SJoe Perches					# skip the bitfield test for now
3851b00e4814SJoe Perches
38521f65f947SAndy Whitcroft				# No spaces for:
38531f65f947SAndy Whitcroft				#   ->
3854b00e4814SJoe Perches				} elsif ($op eq '->') {
38554a0df2efSAndy Whitcroft					if ($ctx =~ /Wx.|.xW/) {
38563705ce5bSJoe Perches						if (ERROR("SPACING",
38573705ce5bSJoe Perches							  "spaces prohibited around that '$op' $at\n" . $hereptr)) {
3858b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
38593705ce5bSJoe Perches							if (defined $fix_elements[$n + 2]) {
38603705ce5bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
38613705ce5bSJoe Perches							}
3862b34c648bSJoe Perches							$line_fixed = 1;
38633705ce5bSJoe Perches						}
38640a920b5bSAndy Whitcroft					}
38650a920b5bSAndy Whitcroft
38662381097bSJoe Perches				# , must not have a space before and must have a space on the right.
38670a920b5bSAndy Whitcroft				} elsif ($op eq ',') {
38682381097bSJoe Perches					my $rtrim_before = 0;
38692381097bSJoe Perches					my $space_after = 0;
38702381097bSJoe Perches					if ($ctx =~ /Wx./) {
38712381097bSJoe Perches						if (ERROR("SPACING",
38722381097bSJoe Perches							  "space prohibited before that '$op' $at\n" . $hereptr)) {
38732381097bSJoe Perches							$line_fixed = 1;
38742381097bSJoe Perches							$rtrim_before = 1;
38752381097bSJoe Perches						}
38762381097bSJoe Perches					}
3877cf655043SAndy Whitcroft					if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
38783705ce5bSJoe Perches						if (ERROR("SPACING",
38793705ce5bSJoe Perches							  "space required after that '$op' $at\n" . $hereptr)) {
38803705ce5bSJoe Perches							$line_fixed = 1;
3881b34c648bSJoe Perches							$last_after = $n;
38822381097bSJoe Perches							$space_after = 1;
38832381097bSJoe Perches						}
38842381097bSJoe Perches					}
38852381097bSJoe Perches					if ($rtrim_before || $space_after) {
38862381097bSJoe Perches						if ($rtrim_before) {
38872381097bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
38882381097bSJoe Perches						} else {
38892381097bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
38902381097bSJoe Perches						}
38912381097bSJoe Perches						if ($space_after) {
38922381097bSJoe Perches							$good .= " ";
38933705ce5bSJoe Perches						}
38940a920b5bSAndy Whitcroft					}
38950a920b5bSAndy Whitcroft
38969c0ca6f9SAndy Whitcroft				# '*' as part of a type definition -- reported already.
389774048ed8SAndy Whitcroft				} elsif ($opv eq '*_') {
38989c0ca6f9SAndy Whitcroft					#warn "'*' is part of type\n";
38999c0ca6f9SAndy Whitcroft
39009c0ca6f9SAndy Whitcroft				# unary operators should have a space before and
39019c0ca6f9SAndy Whitcroft				# none after.  May be left adjacent to another
39029c0ca6f9SAndy Whitcroft				# unary operator, or a cast
39039c0ca6f9SAndy Whitcroft				} elsif ($op eq '!' || $op eq '~' ||
390474048ed8SAndy Whitcroft					 $opv eq '*U' || $opv eq '-U' ||
39050d413866SAndy Whitcroft					 $opv eq '&U' || $opv eq '&&U') {
3906cf655043SAndy Whitcroft					if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
39073705ce5bSJoe Perches						if (ERROR("SPACING",
39083705ce5bSJoe Perches							  "space required before that '$op' $at\n" . $hereptr)) {
3909b34c648bSJoe Perches							if ($n != $last_after + 2) {
3910b34c648bSJoe Perches								$good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]);
39113705ce5bSJoe Perches								$line_fixed = 1;
39123705ce5bSJoe Perches							}
39130a920b5bSAndy Whitcroft						}
3914b34c648bSJoe Perches					}
3915a3340b35SAndy Whitcroft					if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
3916171ae1a4SAndy Whitcroft						# A unary '*' may be const
3917171ae1a4SAndy Whitcroft
3918171ae1a4SAndy Whitcroft					} elsif ($ctx =~ /.xW/) {
39193705ce5bSJoe Perches						if (ERROR("SPACING",
39203705ce5bSJoe Perches							  "space prohibited after that '$op' $at\n" . $hereptr)) {
3921b34c648bSJoe Perches							$good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]);
39223705ce5bSJoe Perches							if (defined $fix_elements[$n + 2]) {
39233705ce5bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
39243705ce5bSJoe Perches							}
3925b34c648bSJoe Perches							$line_fixed = 1;
39263705ce5bSJoe Perches						}
39270a920b5bSAndy Whitcroft					}
39280a920b5bSAndy Whitcroft
39290a920b5bSAndy Whitcroft				# unary ++ and unary -- are allowed no space on one side.
39300a920b5bSAndy Whitcroft				} elsif ($op eq '++' or $op eq '--') {
3931773647a0SAndy Whitcroft					if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
39323705ce5bSJoe Perches						if (ERROR("SPACING",
39333705ce5bSJoe Perches							  "space required one side of that '$op' $at\n" . $hereptr)) {
3934b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
39353705ce5bSJoe Perches							$line_fixed = 1;
39363705ce5bSJoe Perches						}
39370a920b5bSAndy Whitcroft					}
3938773647a0SAndy Whitcroft					if ($ctx =~ /Wx[BE]/ ||
3939773647a0SAndy Whitcroft					    ($ctx =~ /Wx./ && $cc =~ /^;/)) {
39403705ce5bSJoe Perches						if (ERROR("SPACING",
39413705ce5bSJoe Perches							  "space prohibited before that '$op' $at\n" . $hereptr)) {
3942b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
39433705ce5bSJoe Perches							$line_fixed = 1;
39443705ce5bSJoe Perches						}
3945653d4876SAndy Whitcroft					}
3946773647a0SAndy Whitcroft					if ($ctx =~ /ExW/) {
39473705ce5bSJoe Perches						if (ERROR("SPACING",
39483705ce5bSJoe Perches							  "space prohibited after that '$op' $at\n" . $hereptr)) {
3949b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
39503705ce5bSJoe Perches							if (defined $fix_elements[$n + 2]) {
39513705ce5bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
3952773647a0SAndy Whitcroft							}
3953b34c648bSJoe Perches							$line_fixed = 1;
39543705ce5bSJoe Perches						}
39553705ce5bSJoe Perches					}
39560a920b5bSAndy Whitcroft
39570a920b5bSAndy Whitcroft				# << and >> may either have or not have spaces both sides
39589c0ca6f9SAndy Whitcroft				} elsif ($op eq '<<' or $op eq '>>' or
39599c0ca6f9SAndy Whitcroft					 $op eq '&' or $op eq '^' or $op eq '|' or
39609c0ca6f9SAndy Whitcroft					 $op eq '+' or $op eq '-' or
3961c2fdda0dSAndy Whitcroft					 $op eq '*' or $op eq '/' or
3962c2fdda0dSAndy Whitcroft					 $op eq '%')
39630a920b5bSAndy Whitcroft				{
3964d2e025f3SJoe Perches					if ($check) {
3965d2e025f3SJoe Perches						if (defined $fix_elements[$n + 2] && $ctx !~ /[EW]x[EW]/) {
3966d2e025f3SJoe Perches							if (CHK("SPACING",
3967d2e025f3SJoe Perches								"spaces preferred around that '$op' $at\n" . $hereptr)) {
3968d2e025f3SJoe Perches								$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
3969d2e025f3SJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
3970d2e025f3SJoe Perches								$line_fixed = 1;
3971d2e025f3SJoe Perches							}
3972d2e025f3SJoe Perches						} elsif (!defined $fix_elements[$n + 2] && $ctx !~ /Wx[OE]/) {
3973d2e025f3SJoe Perches							if (CHK("SPACING",
3974d2e025f3SJoe Perches								"space preferred before that '$op' $at\n" . $hereptr)) {
3975d2e025f3SJoe Perches								$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]);
3976d2e025f3SJoe Perches								$line_fixed = 1;
3977d2e025f3SJoe Perches							}
3978d2e025f3SJoe Perches						}
3979d2e025f3SJoe Perches					} elsif ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
39803705ce5bSJoe Perches						if (ERROR("SPACING",
39813705ce5bSJoe Perches							  "need consistent spacing around '$op' $at\n" . $hereptr)) {
3982b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
3983b34c648bSJoe Perches							if (defined $fix_elements[$n + 2]) {
3984b34c648bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
3985b34c648bSJoe Perches							}
39863705ce5bSJoe Perches							$line_fixed = 1;
39873705ce5bSJoe Perches						}
39880a920b5bSAndy Whitcroft					}
39890a920b5bSAndy Whitcroft
39901f65f947SAndy Whitcroft				# A colon needs no spaces before when it is
39911f65f947SAndy Whitcroft				# terminating a case value or a label.
39921f65f947SAndy Whitcroft				} elsif ($opv eq ':C' || $opv eq ':L') {
39931f65f947SAndy Whitcroft					if ($ctx =~ /Wx./) {
39943705ce5bSJoe Perches						if (ERROR("SPACING",
39953705ce5bSJoe Perches							  "space prohibited before that '$op' $at\n" . $hereptr)) {
3996b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
39973705ce5bSJoe Perches							$line_fixed = 1;
39983705ce5bSJoe Perches						}
39991f65f947SAndy Whitcroft					}
40001f65f947SAndy Whitcroft
40010a920b5bSAndy Whitcroft				# All the others need spaces both sides.
4002cf655043SAndy Whitcroft				} elsif ($ctx !~ /[EWC]x[CWE]/) {
40031f65f947SAndy Whitcroft					my $ok = 0;
40041f65f947SAndy Whitcroft
400522f2a2efSAndy Whitcroft					# Ignore email addresses <foo@bar>
40061f65f947SAndy Whitcroft					if (($op eq '<' &&
40071f65f947SAndy Whitcroft					     $cc =~ /^\S+\@\S+>/) ||
40081f65f947SAndy Whitcroft					    ($op eq '>' &&
40091f65f947SAndy Whitcroft					     $ca =~ /<\S+\@\S+$/))
40101f65f947SAndy Whitcroft					{
40111f65f947SAndy Whitcroft					    	$ok = 1;
40121f65f947SAndy Whitcroft					}
40131f65f947SAndy Whitcroft
4014e0df7e1fSJoe Perches					# for asm volatile statements
4015e0df7e1fSJoe Perches					# ignore a colon with another
4016e0df7e1fSJoe Perches					# colon immediately before or after
4017e0df7e1fSJoe Perches					if (($op eq ':') &&
4018e0df7e1fSJoe Perches					    ($ca =~ /:$/ || $cc =~ /^:/)) {
4019e0df7e1fSJoe Perches						$ok = 1;
4020e0df7e1fSJoe Perches					}
4021e0df7e1fSJoe Perches
402284731623SJoe Perches					# messages are ERROR, but ?: are CHK
40231f65f947SAndy Whitcroft					if ($ok == 0) {
402484731623SJoe Perches						my $msg_type = \&ERROR;
402584731623SJoe Perches						$msg_type = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
402684731623SJoe Perches
402784731623SJoe Perches						if (&{$msg_type}("SPACING",
40283705ce5bSJoe Perches								 "spaces required around that '$op' $at\n" . $hereptr)) {
4029b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4030b34c648bSJoe Perches							if (defined $fix_elements[$n + 2]) {
4031b34c648bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
4032b34c648bSJoe Perches							}
40333705ce5bSJoe Perches							$line_fixed = 1;
40343705ce5bSJoe Perches						}
40350a920b5bSAndy Whitcroft					}
403622f2a2efSAndy Whitcroft				}
40374a0df2efSAndy Whitcroft				$off += length($elements[$n + 1]);
40383705ce5bSJoe Perches
40393705ce5bSJoe Perches##				print("n: <$n> GOOD: <$good>\n");
40403705ce5bSJoe Perches
40413705ce5bSJoe Perches				$fixed_line = $fixed_line . $good;
40420a920b5bSAndy Whitcroft			}
40433705ce5bSJoe Perches
40443705ce5bSJoe Perches			if (($#elements % 2) == 0) {
40453705ce5bSJoe Perches				$fixed_line = $fixed_line . $fix_elements[$#elements];
40463705ce5bSJoe Perches			}
40473705ce5bSJoe Perches
4048194f66fcSJoe Perches			if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) {
4049194f66fcSJoe Perches				$fixed[$fixlinenr] = $fixed_line;
40503705ce5bSJoe Perches			}
40513705ce5bSJoe Perches
40523705ce5bSJoe Perches
40530a920b5bSAndy Whitcroft		}
40540a920b5bSAndy Whitcroft
4055786b6326SJoe Perches# check for whitespace before a non-naked semicolon
4056d2e248e7SJoe Perches		if ($line =~ /^\+.*\S\s+;\s*$/) {
4057786b6326SJoe Perches			if (WARN("SPACING",
4058786b6326SJoe Perches				 "space prohibited before semicolon\n" . $herecurr) &&
4059786b6326SJoe Perches			    $fix) {
4060194f66fcSJoe Perches				1 while $fixed[$fixlinenr] =~
4061786b6326SJoe Perches				    s/^(\+.*\S)\s+;/$1;/;
4062786b6326SJoe Perches			}
4063786b6326SJoe Perches		}
4064786b6326SJoe Perches
4065f0a594c1SAndy Whitcroft# check for multiple assignments
4066f0a594c1SAndy Whitcroft		if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
4067000d1cc1SJoe Perches			CHK("MULTIPLE_ASSIGNMENTS",
4068000d1cc1SJoe Perches			    "multiple assignments should be avoided\n" . $herecurr);
4069f0a594c1SAndy Whitcroft		}
4070f0a594c1SAndy Whitcroft
407122f2a2efSAndy Whitcroft## # check for multiple declarations, allowing for a function declaration
407222f2a2efSAndy Whitcroft## # continuation.
407322f2a2efSAndy Whitcroft## 		if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
407422f2a2efSAndy Whitcroft## 		    $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
407522f2a2efSAndy Whitcroft##
407622f2a2efSAndy Whitcroft## 			# Remove any bracketed sections to ensure we do not
407722f2a2efSAndy Whitcroft## 			# falsly report the parameters of functions.
407822f2a2efSAndy Whitcroft## 			my $ln = $line;
407922f2a2efSAndy Whitcroft## 			while ($ln =~ s/\([^\(\)]*\)//g) {
408022f2a2efSAndy Whitcroft## 			}
408122f2a2efSAndy Whitcroft## 			if ($ln =~ /,/) {
4082000d1cc1SJoe Perches## 				WARN("MULTIPLE_DECLARATION",
4083000d1cc1SJoe Perches##				     "declaring multiple variables together should be avoided\n" . $herecurr);
408422f2a2efSAndy Whitcroft## 			}
408522f2a2efSAndy Whitcroft## 		}
4086f0a594c1SAndy Whitcroft
40870a920b5bSAndy Whitcroft#need space before brace following if, while, etc
40884e5d56bdSEddie Kovsky		if (($line =~ /\(.*\)\{/ && $line !~ /\($Type\){/) ||
40894e5d56bdSEddie Kovsky		    $line =~ /do\{/) {
40903705ce5bSJoe Perches			if (ERROR("SPACING",
40913705ce5bSJoe Perches				  "space required before the open brace '{'\n" . $herecurr) &&
40923705ce5bSJoe Perches			    $fix) {
4093194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/^(\+.*(?:do|\))){/$1 {/;
40943705ce5bSJoe Perches			}
4095de7d4f0eSAndy Whitcroft		}
4096de7d4f0eSAndy Whitcroft
4097c4a62ef9SJoe Perches## # check for blank lines before declarations
4098c4a62ef9SJoe Perches##		if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
4099c4a62ef9SJoe Perches##		    $prevrawline =~ /^.\s*$/) {
4100c4a62ef9SJoe Perches##			WARN("SPACING",
4101c4a62ef9SJoe Perches##			     "No blank lines before declarations\n" . $hereprev);
4102c4a62ef9SJoe Perches##		}
4103c4a62ef9SJoe Perches##
4104c4a62ef9SJoe Perches
4105de7d4f0eSAndy Whitcroft# closing brace should have a space following it when it has anything
4106de7d4f0eSAndy Whitcroft# on the line
4107de7d4f0eSAndy Whitcroft		if ($line =~ /}(?!(?:,|;|\)))\S/) {
4108d5e616fcSJoe Perches			if (ERROR("SPACING",
4109d5e616fcSJoe Perches				  "space required after that close brace '}'\n" . $herecurr) &&
4110d5e616fcSJoe Perches			    $fix) {
4111194f66fcSJoe Perches				$fixed[$fixlinenr] =~
4112d5e616fcSJoe Perches				    s/}((?!(?:,|;|\)))\S)/} $1/;
4113d5e616fcSJoe Perches			}
41140a920b5bSAndy Whitcroft		}
41150a920b5bSAndy Whitcroft
411622f2a2efSAndy Whitcroft# check spacing on square brackets
411722f2a2efSAndy Whitcroft		if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
41183705ce5bSJoe Perches			if (ERROR("SPACING",
41193705ce5bSJoe Perches				  "space prohibited after that open square bracket '['\n" . $herecurr) &&
41203705ce5bSJoe Perches			    $fix) {
4121194f66fcSJoe Perches				$fixed[$fixlinenr] =~
41223705ce5bSJoe Perches				    s/\[\s+/\[/;
41233705ce5bSJoe Perches			}
412422f2a2efSAndy Whitcroft		}
412522f2a2efSAndy Whitcroft		if ($line =~ /\s\]/) {
41263705ce5bSJoe Perches			if (ERROR("SPACING",
41273705ce5bSJoe Perches				  "space prohibited before that close square bracket ']'\n" . $herecurr) &&
41283705ce5bSJoe Perches			    $fix) {
4129194f66fcSJoe Perches				$fixed[$fixlinenr] =~
41303705ce5bSJoe Perches				    s/\s+\]/\]/;
41313705ce5bSJoe Perches			}
413222f2a2efSAndy Whitcroft		}
413322f2a2efSAndy Whitcroft
4134c45dcabdSAndy Whitcroft# check spacing on parentheses
41359c0ca6f9SAndy Whitcroft		if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
41369c0ca6f9SAndy Whitcroft		    $line !~ /for\s*\(\s+;/) {
41373705ce5bSJoe Perches			if (ERROR("SPACING",
41383705ce5bSJoe Perches				  "space prohibited after that open parenthesis '('\n" . $herecurr) &&
41393705ce5bSJoe Perches			    $fix) {
4140194f66fcSJoe Perches				$fixed[$fixlinenr] =~
41413705ce5bSJoe Perches				    s/\(\s+/\(/;
41423705ce5bSJoe Perches			}
414322f2a2efSAndy Whitcroft		}
414413214adfSAndy Whitcroft		if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
4145c45dcabdSAndy Whitcroft		    $line !~ /for\s*\(.*;\s+\)/ &&
4146c45dcabdSAndy Whitcroft		    $line !~ /:\s+\)/) {
41473705ce5bSJoe Perches			if (ERROR("SPACING",
41483705ce5bSJoe Perches				  "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
41493705ce5bSJoe Perches			    $fix) {
4150194f66fcSJoe Perches				$fixed[$fixlinenr] =~
41513705ce5bSJoe Perches				    s/\s+\)/\)/;
41523705ce5bSJoe Perches			}
415322f2a2efSAndy Whitcroft		}
415422f2a2efSAndy Whitcroft
4155e2826fd0SJoe Perches# check unnecessary parentheses around addressof/dereference single $Lvals
4156e2826fd0SJoe Perches# ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar
4157e2826fd0SJoe Perches
4158e2826fd0SJoe Perches		while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) {
4159ea4acbb1SJoe Perches			my $var = $1;
4160ea4acbb1SJoe Perches			if (CHK("UNNECESSARY_PARENTHESES",
4161ea4acbb1SJoe Perches				"Unnecessary parentheses around $var\n" . $herecurr) &&
4162ea4acbb1SJoe Perches			    $fix) {
4163ea4acbb1SJoe Perches				$fixed[$fixlinenr] =~ s/\(\s*\Q$var\E\s*\)/$var/;
4164ea4acbb1SJoe Perches			}
4165ea4acbb1SJoe Perches		}
4166ea4acbb1SJoe Perches
4167ea4acbb1SJoe Perches# check for unnecessary parentheses around function pointer uses
4168ea4acbb1SJoe Perches# ie: (foo->bar)(); should be foo->bar();
4169ea4acbb1SJoe Perches# but not "if (foo->bar) (" to avoid some false positives
4170ea4acbb1SJoe Perches		if ($line =~ /(\bif\s*|)(\(\s*$Ident\s*(?:$Member\s*)+\))[ \t]*\(/ && $1 !~ /^if/) {
4171ea4acbb1SJoe Perches			my $var = $2;
4172ea4acbb1SJoe Perches			if (CHK("UNNECESSARY_PARENTHESES",
4173ea4acbb1SJoe Perches				"Unnecessary parentheses around function pointer $var\n" . $herecurr) &&
4174ea4acbb1SJoe Perches			    $fix) {
4175ea4acbb1SJoe Perches				my $var2 = deparenthesize($var);
4176ea4acbb1SJoe Perches				$var2 =~ s/\s//g;
4177ea4acbb1SJoe Perches				$fixed[$fixlinenr] =~ s/\Q$var\E/$var2/;
4178ea4acbb1SJoe Perches			}
4179e2826fd0SJoe Perches		}
4180e2826fd0SJoe Perches
41810a920b5bSAndy Whitcroft#goto labels aren't indented, allow a single space however
41824a0df2efSAndy Whitcroft		if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
41830a920b5bSAndy Whitcroft		   !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
41843705ce5bSJoe Perches			if (WARN("INDENTED_LABEL",
41853705ce5bSJoe Perches				 "labels should not be indented\n" . $herecurr) &&
41863705ce5bSJoe Perches			    $fix) {
4187194f66fcSJoe Perches				$fixed[$fixlinenr] =~
41883705ce5bSJoe Perches				    s/^(.)\s+/$1/;
41893705ce5bSJoe Perches			}
41900a920b5bSAndy Whitcroft		}
41910a920b5bSAndy Whitcroft
41925b9553abSJoe Perches# return is not a function
4193507e5141SJoe Perches		if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {
4194c45dcabdSAndy Whitcroft			my $spacing = $1;
4195507e5141SJoe Perches			if ($^V && $^V ge 5.10.0 &&
41965b9553abSJoe Perches			    $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) {
41975b9553abSJoe Perches				my $value = $1;
41985b9553abSJoe Perches				$value = deparenthesize($value);
41995b9553abSJoe Perches				if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) {
4200000d1cc1SJoe Perches					ERROR("RETURN_PARENTHESES",
4201000d1cc1SJoe Perches					      "return is not a function, parentheses are not required\n" . $herecurr);
42025b9553abSJoe Perches				}
4203c45dcabdSAndy Whitcroft			} elsif ($spacing !~ /\s+/) {
4204000d1cc1SJoe Perches				ERROR("SPACING",
4205000d1cc1SJoe Perches				      "space required before the open parenthesis '('\n" . $herecurr);
4206c45dcabdSAndy Whitcroft			}
4207c45dcabdSAndy Whitcroft		}
4208507e5141SJoe Perches
4209b43ae21bSJoe Perches# unnecessary return in a void function
4210b43ae21bSJoe Perches# at end-of-function, with the previous line a single leading tab, then return;
4211b43ae21bSJoe Perches# and the line before that not a goto label target like "out:"
4212b43ae21bSJoe Perches		if ($sline =~ /^[ \+]}\s*$/ &&
4213b43ae21bSJoe Perches		    $prevline =~ /^\+\treturn\s*;\s*$/ &&
4214b43ae21bSJoe Perches		    $linenr >= 3 &&
4215b43ae21bSJoe Perches		    $lines[$linenr - 3] =~ /^[ +]/ &&
4216b43ae21bSJoe Perches		    $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) {
42179819cf25SJoe Perches			WARN("RETURN_VOID",
4218b43ae21bSJoe Perches			     "void function return statements are not generally useful\n" . $hereprev);
42199819cf25SJoe Perches               }
42209819cf25SJoe Perches
4221189248d8SJoe Perches# if statements using unnecessary parentheses - ie: if ((foo == bar))
4222189248d8SJoe Perches		if ($^V && $^V ge 5.10.0 &&
4223189248d8SJoe Perches		    $line =~ /\bif\s*((?:\(\s*){2,})/) {
4224189248d8SJoe Perches			my $openparens = $1;
4225189248d8SJoe Perches			my $count = $openparens =~ tr@\(@\(@;
4226189248d8SJoe Perches			my $msg = "";
4227189248d8SJoe Perches			if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) {
4228189248d8SJoe Perches				my $comp = $4;	#Not $1 because of $LvalOrFunc
4229189248d8SJoe Perches				$msg = " - maybe == should be = ?" if ($comp eq "==");
4230189248d8SJoe Perches				WARN("UNNECESSARY_PARENTHESES",
4231189248d8SJoe Perches				     "Unnecessary parentheses$msg\n" . $herecurr);
4232189248d8SJoe Perches			}
4233189248d8SJoe Perches		}
4234189248d8SJoe Perches
4235c5595fa2SJoe Perches# comparisons with a constant or upper case identifier on the left
4236c5595fa2SJoe Perches#	avoid cases like "foo + BAR < baz"
4237c5595fa2SJoe Perches#	only fix matches surrounded by parentheses to avoid incorrect
4238c5595fa2SJoe Perches#	conversions like "FOO < baz() + 5" being "misfixed" to "baz() > FOO + 5"
4239c5595fa2SJoe Perches		if ($^V && $^V ge 5.10.0 &&
4240c5595fa2SJoe Perches		    $line =~ /^\+(.*)\b($Constant|[A-Z_][A-Z0-9_]*)\s*($Compare)\s*($LvalOrFunc)/) {
4241c5595fa2SJoe Perches			my $lead = $1;
4242c5595fa2SJoe Perches			my $const = $2;
4243c5595fa2SJoe Perches			my $comp = $3;
4244c5595fa2SJoe Perches			my $to = $4;
4245c5595fa2SJoe Perches			my $newcomp = $comp;
4246c5595fa2SJoe Perches			if ($lead !~ /$Operators\s*$/ &&
4247c5595fa2SJoe Perches			    $to !~ /^(?:Constant|[A-Z_][A-Z0-9_]*)$/ &&
4248c5595fa2SJoe Perches			    WARN("CONSTANT_COMPARISON",
4249c5595fa2SJoe Perches				 "Comparisons should place the constant on the right side of the test\n" . $herecurr) &&
4250c5595fa2SJoe Perches			    $fix) {
4251c5595fa2SJoe Perches				if ($comp eq "<") {
4252c5595fa2SJoe Perches					$newcomp = ">";
4253c5595fa2SJoe Perches				} elsif ($comp eq "<=") {
4254c5595fa2SJoe Perches					$newcomp = ">=";
4255c5595fa2SJoe Perches				} elsif ($comp eq ">") {
4256c5595fa2SJoe Perches					$newcomp = "<";
4257c5595fa2SJoe Perches				} elsif ($comp eq ">=") {
4258c5595fa2SJoe Perches					$newcomp = "<=";
4259c5595fa2SJoe Perches				}
4260c5595fa2SJoe Perches				$fixed[$fixlinenr] =~ s/\(\s*\Q$const\E\s*$Compare\s*\Q$to\E\s*\)/($to $newcomp $const)/;
4261c5595fa2SJoe Perches			}
4262c5595fa2SJoe Perches		}
4263c5595fa2SJoe Perches
4264f34e4a4fSJoe Perches# Return of what appears to be an errno should normally be negative
4265f34e4a4fSJoe Perches		if ($sline =~ /\breturn(?:\s*\(+\s*|\s+)(E[A-Z]+)(?:\s*\)+\s*|\s*)[;:,]/) {
426653a3c448SAndy Whitcroft			my $name = $1;
426753a3c448SAndy Whitcroft			if ($name ne 'EOF' && $name ne 'ERROR') {
4268000d1cc1SJoe Perches				WARN("USE_NEGATIVE_ERRNO",
4269f34e4a4fSJoe Perches				     "return of an errno should typically be negative (ie: return -$1)\n" . $herecurr);
427053a3c448SAndy Whitcroft			}
427153a3c448SAndy Whitcroft		}
4272c45dcabdSAndy Whitcroft
42730a920b5bSAndy Whitcroft# Need a space before open parenthesis after if, while etc
42744a0df2efSAndy Whitcroft		if ($line =~ /\b(if|while|for|switch)\(/) {
42753705ce5bSJoe Perches			if (ERROR("SPACING",
42763705ce5bSJoe Perches				  "space required before the open parenthesis '('\n" . $herecurr) &&
42773705ce5bSJoe Perches			    $fix) {
4278194f66fcSJoe Perches				$fixed[$fixlinenr] =~
42793705ce5bSJoe Perches				    s/\b(if|while|for|switch)\(/$1 \(/;
42803705ce5bSJoe Perches			}
42810a920b5bSAndy Whitcroft		}
42820a920b5bSAndy Whitcroft
4283f5fe35ddSAndy Whitcroft# Check for illegal assignment in if conditional -- and check for trailing
4284f5fe35ddSAndy Whitcroft# statements after the conditional.
4285170d3a22SAndy Whitcroft		if ($line =~ /do\s*(?!{)/) {
42863e469cdcSAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
42873e469cdcSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0)
42883e469cdcSAndy Whitcroft					if (!defined $stat);
4289170d3a22SAndy Whitcroft			my ($stat_next) = ctx_statement_block($line_nr_next,
4290170d3a22SAndy Whitcroft						$remain_next, $off_next);
4291170d3a22SAndy Whitcroft			$stat_next =~ s/\n./\n /g;
4292170d3a22SAndy Whitcroft			##print "stat<$stat> stat_next<$stat_next>\n";
4293170d3a22SAndy Whitcroft
4294170d3a22SAndy Whitcroft			if ($stat_next =~ /^\s*while\b/) {
4295170d3a22SAndy Whitcroft				# If the statement carries leading newlines,
4296170d3a22SAndy Whitcroft				# then count those as offsets.
4297170d3a22SAndy Whitcroft				my ($whitespace) =
4298170d3a22SAndy Whitcroft					($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
4299170d3a22SAndy Whitcroft				my $offset =
4300170d3a22SAndy Whitcroft					statement_rawlines($whitespace) - 1;
4301170d3a22SAndy Whitcroft
4302170d3a22SAndy Whitcroft				$suppress_whiletrailers{$line_nr_next +
4303170d3a22SAndy Whitcroft								$offset} = 1;
4304170d3a22SAndy Whitcroft			}
4305170d3a22SAndy Whitcroft		}
4306170d3a22SAndy Whitcroft		if (!defined $suppress_whiletrailers{$linenr} &&
4307c11230f4SJoe Perches		    defined($stat) && defined($cond) &&
4308170d3a22SAndy Whitcroft		    $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
4309171ae1a4SAndy Whitcroft			my ($s, $c) = ($stat, $cond);
43108905a67cSAndy Whitcroft
4311b53c8e10SAndy Whitcroft			if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
4312000d1cc1SJoe Perches				ERROR("ASSIGN_IN_IF",
4313000d1cc1SJoe Perches				      "do not use assignment in if condition\n" . $herecurr);
43148905a67cSAndy Whitcroft			}
43158905a67cSAndy Whitcroft
43168905a67cSAndy Whitcroft			# Find out what is on the end of the line after the
43178905a67cSAndy Whitcroft			# conditional.
4318773647a0SAndy Whitcroft			substr($s, 0, length($c), '');
43198905a67cSAndy Whitcroft			$s =~ s/\n.*//g;
432013214adfSAndy Whitcroft			$s =~ s/$;//g; 	# Remove any comments
432153210168SAndy Whitcroft			if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
432253210168SAndy Whitcroft			    $c !~ /}\s*while\s*/)
4323773647a0SAndy Whitcroft			{
4324bb44ad39SAndy Whitcroft				# Find out how long the conditional actually is.
4325bb44ad39SAndy Whitcroft				my @newlines = ($c =~ /\n/gs);
4326bb44ad39SAndy Whitcroft				my $cond_lines = 1 + $#newlines;
432742bdf74cSHidetoshi Seto				my $stat_real = '';
4328bb44ad39SAndy Whitcroft
432942bdf74cSHidetoshi Seto				$stat_real = raw_line($linenr, $cond_lines)
433042bdf74cSHidetoshi Seto							. "\n" if ($cond_lines);
4331bb44ad39SAndy Whitcroft				if (defined($stat_real) && $cond_lines > 1) {
4332bb44ad39SAndy Whitcroft					$stat_real = "[...]\n$stat_real";
4333bb44ad39SAndy Whitcroft				}
4334bb44ad39SAndy Whitcroft
4335000d1cc1SJoe Perches				ERROR("TRAILING_STATEMENTS",
4336000d1cc1SJoe Perches				      "trailing statements should be on next line\n" . $herecurr . $stat_real);
43378905a67cSAndy Whitcroft			}
43388905a67cSAndy Whitcroft		}
43398905a67cSAndy Whitcroft
434013214adfSAndy Whitcroft# Check for bitwise tests written as boolean
434113214adfSAndy Whitcroft		if ($line =~ /
434213214adfSAndy Whitcroft			(?:
434313214adfSAndy Whitcroft				(?:\[|\(|\&\&|\|\|)
434413214adfSAndy Whitcroft				\s*0[xX][0-9]+\s*
434513214adfSAndy Whitcroft				(?:\&\&|\|\|)
434613214adfSAndy Whitcroft			|
434713214adfSAndy Whitcroft				(?:\&\&|\|\|)
434813214adfSAndy Whitcroft				\s*0[xX][0-9]+\s*
434913214adfSAndy Whitcroft				(?:\&\&|\|\||\)|\])
435013214adfSAndy Whitcroft			)/x)
435113214adfSAndy Whitcroft		{
4352000d1cc1SJoe Perches			WARN("HEXADECIMAL_BOOLEAN_TEST",
4353000d1cc1SJoe Perches			     "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
435413214adfSAndy Whitcroft		}
435513214adfSAndy Whitcroft
43568905a67cSAndy Whitcroft# if and else should not have general statements after it
435713214adfSAndy Whitcroft		if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
435813214adfSAndy Whitcroft			my $s = $1;
435913214adfSAndy Whitcroft			$s =~ s/$;//g; 	# Remove any comments
436013214adfSAndy Whitcroft			if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
4361000d1cc1SJoe Perches				ERROR("TRAILING_STATEMENTS",
4362000d1cc1SJoe Perches				      "trailing statements should be on next line\n" . $herecurr);
43630a920b5bSAndy Whitcroft			}
436413214adfSAndy Whitcroft		}
436539667782SAndy Whitcroft# if should not continue a brace
436639667782SAndy Whitcroft		if ($line =~ /}\s*if\b/) {
4367000d1cc1SJoe Perches			ERROR("TRAILING_STATEMENTS",
4368048b123fSRasmus Villemoes			      "trailing statements should be on next line (or did you mean 'else if'?)\n" .
436939667782SAndy Whitcroft				$herecurr);
437039667782SAndy Whitcroft		}
4371a1080bf8SAndy Whitcroft# case and default should not have general statements after them
4372a1080bf8SAndy Whitcroft		if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
4373a1080bf8SAndy Whitcroft		    $line !~ /\G(?:
43743fef12d6SAndy Whitcroft			(?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
4375a1080bf8SAndy Whitcroft			\s*return\s+
4376a1080bf8SAndy Whitcroft		    )/xg)
4377a1080bf8SAndy Whitcroft		{
4378000d1cc1SJoe Perches			ERROR("TRAILING_STATEMENTS",
4379000d1cc1SJoe Perches			      "trailing statements should be on next line\n" . $herecurr);
4380a1080bf8SAndy Whitcroft		}
43810a920b5bSAndy Whitcroft
43820a920b5bSAndy Whitcroft		# Check for }<nl>else {, these must be at the same
43830a920b5bSAndy Whitcroft		# indent level to be relevant to each other.
43848b8856f4SJoe Perches		if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ &&
43850a920b5bSAndy Whitcroft		    $previndent == $indent) {
43868b8856f4SJoe Perches			if (ERROR("ELSE_AFTER_BRACE",
43878b8856f4SJoe Perches				  "else should follow close brace '}'\n" . $hereprev) &&
43888b8856f4SJoe Perches			    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
43898b8856f4SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
43908b8856f4SJoe Perches				fix_delete_line($fixlinenr, $rawline);
43918b8856f4SJoe Perches				my $fixedline = $prevrawline;
43928b8856f4SJoe Perches				$fixedline =~ s/}\s*$//;
43938b8856f4SJoe Perches				if ($fixedline !~ /^\+\s*$/) {
43948b8856f4SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
43958b8856f4SJoe Perches				}
43968b8856f4SJoe Perches				$fixedline = $rawline;
43978b8856f4SJoe Perches				$fixedline =~ s/^(.\s*)else/$1} else/;
43988b8856f4SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
43998b8856f4SJoe Perches			}
44000a920b5bSAndy Whitcroft		}
44010a920b5bSAndy Whitcroft
44028b8856f4SJoe Perches		if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ &&
4403c2fdda0dSAndy Whitcroft		    $previndent == $indent) {
4404c2fdda0dSAndy Whitcroft			my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
4405c2fdda0dSAndy Whitcroft
4406c2fdda0dSAndy Whitcroft			# Find out what is on the end of the line after the
4407c2fdda0dSAndy Whitcroft			# conditional.
4408773647a0SAndy Whitcroft			substr($s, 0, length($c), '');
4409c2fdda0dSAndy Whitcroft			$s =~ s/\n.*//g;
4410c2fdda0dSAndy Whitcroft
4411c2fdda0dSAndy Whitcroft			if ($s =~ /^\s*;/) {
44128b8856f4SJoe Perches				if (ERROR("WHILE_AFTER_BRACE",
44138b8856f4SJoe Perches					  "while should follow close brace '}'\n" . $hereprev) &&
44148b8856f4SJoe Perches				    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
44158b8856f4SJoe Perches					fix_delete_line($fixlinenr - 1, $prevrawline);
44168b8856f4SJoe Perches					fix_delete_line($fixlinenr, $rawline);
44178b8856f4SJoe Perches					my $fixedline = $prevrawline;
44188b8856f4SJoe Perches					my $trailing = $rawline;
44198b8856f4SJoe Perches					$trailing =~ s/^\+//;
44208b8856f4SJoe Perches					$trailing = trim($trailing);
44218b8856f4SJoe Perches					$fixedline =~ s/}\s*$/} $trailing/;
44228b8856f4SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
44238b8856f4SJoe Perches				}
4424c2fdda0dSAndy Whitcroft			}
4425c2fdda0dSAndy Whitcroft		}
4426c2fdda0dSAndy Whitcroft
442795e2c602SJoe Perches#Specific variable tests
4428323c1260SJoe Perches		while ($line =~ m{($Constant|$Lval)}g) {
4429323c1260SJoe Perches			my $var = $1;
443095e2c602SJoe Perches
443195e2c602SJoe Perches#gcc binary extension
443295e2c602SJoe Perches			if ($var =~ /^$Binary$/) {
4433d5e616fcSJoe Perches				if (WARN("GCC_BINARY_CONSTANT",
4434d5e616fcSJoe Perches					 "Avoid gcc v4.3+ binary constant extension: <$var>\n" . $herecurr) &&
4435d5e616fcSJoe Perches				    $fix) {
4436d5e616fcSJoe Perches					my $hexval = sprintf("0x%x", oct($var));
4437194f66fcSJoe Perches					$fixed[$fixlinenr] =~
4438d5e616fcSJoe Perches					    s/\b$var\b/$hexval/;
4439d5e616fcSJoe Perches				}
444095e2c602SJoe Perches			}
444195e2c602SJoe Perches
444295e2c602SJoe Perches#CamelCase
4443807bd26cSJoe Perches			if ($var !~ /^$Constant$/ &&
4444be79794bSJoe Perches			    $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
444522735ce8SJoe Perches#Ignore Page<foo> variants
4446807bd26cSJoe Perches			    $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
444722735ce8SJoe Perches#Ignore SI style variants like nS, mV and dB (ie: max_uV, regulator_min_uA_show)
4448f5123576SJulius Werner			    $var !~ /^(?:[a-z_]*?)_?[a-z][A-Z](?:_[a-z_]+)?$/ &&
4449f5123576SJulius Werner#Ignore some three character SI units explicitly, like MiB and KHz
4450f5123576SJulius Werner			    $var !~ /^(?:[a-z_]*?)_?(?:[KMGT]iB|[KMGT]?Hz)(?:_[a-z_]+)?$/) {
44517e781f67SJoe Perches				while ($var =~ m{($Ident)}g) {
44527e781f67SJoe Perches					my $word = $1;
44537e781f67SJoe Perches					next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
4454d8b07710SJoe Perches					if ($check) {
4455d8b07710SJoe Perches						seed_camelcase_includes();
4456d8b07710SJoe Perches						if (!$file && !$camelcase_file_seeded) {
4457d8b07710SJoe Perches							seed_camelcase_file($realfile);
4458d8b07710SJoe Perches							$camelcase_file_seeded = 1;
4459d8b07710SJoe Perches						}
4460d8b07710SJoe Perches					}
44617e781f67SJoe Perches					if (!defined $camelcase{$word}) {
44627e781f67SJoe Perches						$camelcase{$word} = 1;
4463be79794bSJoe Perches						CHK("CAMELCASE",
44647e781f67SJoe Perches						    "Avoid CamelCase: <$word>\n" . $herecurr);
44657e781f67SJoe Perches					}
4466323c1260SJoe Perches				}
4467323c1260SJoe Perches			}
44683445686aSJoe Perches		}
44690a920b5bSAndy Whitcroft
44700a920b5bSAndy Whitcroft#no spaces allowed after \ in define
4471d5e616fcSJoe Perches		if ($line =~ /\#\s*define.*\\\s+$/) {
4472d5e616fcSJoe Perches			if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
4473d5e616fcSJoe Perches				 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
4474d5e616fcSJoe Perches			    $fix) {
4475194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\s+$//;
4476d5e616fcSJoe Perches			}
44770a920b5bSAndy Whitcroft		}
44780a920b5bSAndy Whitcroft
44790e212e0aSFabian Frederick# warn if <asm/foo.h> is #included and <linux/foo.h> is available and includes
44800e212e0aSFabian Frederick# itself <asm/foo.h> (uses RAW line)
4481c45dcabdSAndy Whitcroft		if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
4482e09dec48SAndy Whitcroft			my $file = "$1.h";
4483e09dec48SAndy Whitcroft			my $checkfile = "include/linux/$file";
4484e09dec48SAndy Whitcroft			if (-f "$root/$checkfile" &&
4485e09dec48SAndy Whitcroft			    $realfile ne $checkfile &&
44867840a94cSWolfram Sang			    $1 !~ /$allowed_asm_includes/)
4487c45dcabdSAndy Whitcroft			{
44880e212e0aSFabian Frederick				my $asminclude = `grep -Ec "#include\\s+<asm/$file>" $root/$checkfile`;
44890e212e0aSFabian Frederick				if ($asminclude > 0) {
4490e09dec48SAndy Whitcroft					if ($realfile =~ m{^arch/}) {
4491000d1cc1SJoe Perches						CHK("ARCH_INCLUDE_LINUX",
4492000d1cc1SJoe Perches						    "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
4493e09dec48SAndy Whitcroft					} else {
4494000d1cc1SJoe Perches						WARN("INCLUDE_LINUX",
4495000d1cc1SJoe Perches						     "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
4496e09dec48SAndy Whitcroft					}
44970a920b5bSAndy Whitcroft				}
44980a920b5bSAndy Whitcroft			}
44990e212e0aSFabian Frederick		}
45000a920b5bSAndy Whitcroft
4501653d4876SAndy Whitcroft# multi-statement macros should be enclosed in a do while loop, grab the
4502653d4876SAndy Whitcroft# first statement and ensure its the whole macro if its not enclosed
4503cf655043SAndy Whitcroft# in a known good container
4504b8f96a31SAndy Whitcroft		if ($realfile !~ m@/vmlinux.lds.h$@ &&
4505b8f96a31SAndy Whitcroft		    $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
4506d8aaf121SAndy Whitcroft			my $ln = $linenr;
4507d8aaf121SAndy Whitcroft			my $cnt = $realcnt;
4508c45dcabdSAndy Whitcroft			my ($off, $dstat, $dcond, $rest);
4509c45dcabdSAndy Whitcroft			my $ctx = '';
451008a2843eSJoe Perches			my $has_flow_statement = 0;
451108a2843eSJoe Perches			my $has_arg_concat = 0;
4512c45dcabdSAndy Whitcroft			($dstat, $dcond, $ln, $cnt, $off) =
4513f74bd194SAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0);
4514f74bd194SAndy Whitcroft			$ctx = $dstat;
4515c45dcabdSAndy Whitcroft			#print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
4516a3bb97a7SAndy Whitcroft			#print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
4517c45dcabdSAndy Whitcroft
451808a2843eSJoe Perches			$has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/);
451908a2843eSJoe Perches			$has_arg_concat = 1 if ($ctx =~ /\#\#/);
452008a2843eSJoe Perches
4521f74bd194SAndy Whitcroft			$dstat =~ s/^.\s*\#\s*define\s+$Ident(?:\([^\)]*\))?\s*//;
4522292f1a9bSAndy Whitcroft			$dstat =~ s/$;//g;
4523c45dcabdSAndy Whitcroft			$dstat =~ s/\\\n.//g;
4524c45dcabdSAndy Whitcroft			$dstat =~ s/^\s*//s;
4525c45dcabdSAndy Whitcroft			$dstat =~ s/\s*$//s;
4526c45dcabdSAndy Whitcroft
4527c45dcabdSAndy Whitcroft			# Flatten any parentheses and braces
4528bf30d6edSAndy Whitcroft			while ($dstat =~ s/\([^\(\)]*\)/1/ ||
4529bf30d6edSAndy Whitcroft			       $dstat =~ s/\{[^\{\}]*\}/1/ ||
4530c81769fdSAndy Whitcroft			       $dstat =~ s/\[[^\[\]]*\]/1/)
4531bf30d6edSAndy Whitcroft			{
4532c45dcabdSAndy Whitcroft			}
4533c45dcabdSAndy Whitcroft
4534e45bab8eSAndy Whitcroft			# Flatten any obvious string concatentation.
453533acb54aSJoe Perches			while ($dstat =~ s/($String)\s*$Ident/$1/ ||
453633acb54aSJoe Perches			       $dstat =~ s/$Ident\s*($String)/$1/)
4537e45bab8eSAndy Whitcroft			{
4538e45bab8eSAndy Whitcroft			}
4539e45bab8eSAndy Whitcroft
4540c45dcabdSAndy Whitcroft			my $exceptions = qr{
4541c45dcabdSAndy Whitcroft				$Declare|
4542c45dcabdSAndy Whitcroft				module_param_named|
4543a0a0a7a9SKees Cook				MODULE_PARM_DESC|
4544c45dcabdSAndy Whitcroft				DECLARE_PER_CPU|
4545c45dcabdSAndy Whitcroft				DEFINE_PER_CPU|
4546383099fdSAndy Whitcroft				__typeof__\(|
454722fd2d3eSStefani Seibold				union|
454822fd2d3eSStefani Seibold				struct|
4549ea71a0a0SAndy Whitcroft				\.$Ident\s*=\s*|
4550ea71a0a0SAndy Whitcroft				^\"|\"$
4551c45dcabdSAndy Whitcroft			}x;
45525eaa20b9SAndy Whitcroft			#print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
4553f74bd194SAndy Whitcroft			if ($dstat ne '' &&
4554f74bd194SAndy Whitcroft			    $dstat !~ /^(?:$Ident|-?$Constant),$/ &&			# 10, // foo(),
4555f74bd194SAndy Whitcroft			    $dstat !~ /^(?:$Ident|-?$Constant);$/ &&			# foo();
45563cc4b1c3SJoe Perches			    $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ &&		# 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
4557356fd398SJoe Perches			    $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ &&			# character constants
4558f74bd194SAndy Whitcroft			    $dstat !~ /$exceptions/ &&
4559f74bd194SAndy Whitcroft			    $dstat !~ /^\.$Ident\s*=/ &&				# .foo =
4560e942e2c3SJoe Perches			    $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ &&		# stringification #foo
456172f115f9SAndy Whitcroft			    $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ &&	# do {...} while (...); // do {...} while (...)
4562f74bd194SAndy Whitcroft			    $dstat !~ /^for\s*$Constant$/ &&				# for (...)
4563f74bd194SAndy Whitcroft			    $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ &&	# for (...) bar()
4564f74bd194SAndy Whitcroft			    $dstat !~ /^do\s*{/ &&					# do {...
45654e5d56bdSEddie Kovsky			    $dstat !~ /^\(\{/ &&						# ({...
4566f95a7e6aSJoe Perches			    $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
4567c45dcabdSAndy Whitcroft			{
4568f74bd194SAndy Whitcroft				$ctx =~ s/\n*$//;
4569f74bd194SAndy Whitcroft				my $herectx = $here . "\n";
4570f74bd194SAndy Whitcroft				my $cnt = statement_rawlines($ctx);
4571f74bd194SAndy Whitcroft
4572f74bd194SAndy Whitcroft				for (my $n = 0; $n < $cnt; $n++) {
4573f74bd194SAndy Whitcroft					$herectx .= raw_line($linenr, $n) . "\n";
4574c45dcabdSAndy Whitcroft				}
4575c45dcabdSAndy Whitcroft
4576f74bd194SAndy Whitcroft				if ($dstat =~ /;/) {
4577f74bd194SAndy Whitcroft					ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
4578f74bd194SAndy Whitcroft					      "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
4579f74bd194SAndy Whitcroft				} else {
4580000d1cc1SJoe Perches					ERROR("COMPLEX_MACRO",
4581388982b5SAndrew Morton					      "Macros with complex values should be enclosed in parentheses\n" . "$herectx");
4582d8aaf121SAndy Whitcroft				}
45830a920b5bSAndy Whitcroft			}
45845023d347SJoe Perches
458508a2843eSJoe Perches# check for macros with flow control, but without ## concatenation
458608a2843eSJoe Perches# ## concatenation is commonly a macro that defines a function so ignore those
458708a2843eSJoe Perches			if ($has_flow_statement && !$has_arg_concat) {
458808a2843eSJoe Perches				my $herectx = $here . "\n";
458908a2843eSJoe Perches				my $cnt = statement_rawlines($ctx);
459008a2843eSJoe Perches
459108a2843eSJoe Perches				for (my $n = 0; $n < $cnt; $n++) {
459208a2843eSJoe Perches					$herectx .= raw_line($linenr, $n) . "\n";
459308a2843eSJoe Perches				}
459408a2843eSJoe Perches				WARN("MACRO_WITH_FLOW_CONTROL",
459508a2843eSJoe Perches				     "Macros with flow control statements should be avoided\n" . "$herectx");
459608a2843eSJoe Perches			}
459708a2843eSJoe Perches
4598481eb486SJoe Perches# check for line continuations outside of #defines, preprocessor #, and asm
45995023d347SJoe Perches
46005023d347SJoe Perches		} else {
46015023d347SJoe Perches			if ($prevline !~ /^..*\\$/ &&
4602481eb486SJoe Perches			    $line !~ /^\+\s*\#.*\\$/ &&		# preprocessor
4603481eb486SJoe Perches			    $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ &&	# asm
46045023d347SJoe Perches			    $line =~ /^\+.*\\$/) {
46055023d347SJoe Perches				WARN("LINE_CONTINUATIONS",
46065023d347SJoe Perches				     "Avoid unnecessary line continuations\n" . $herecurr);
46075023d347SJoe Perches			}
4608653d4876SAndy Whitcroft		}
46090a920b5bSAndy Whitcroft
4610b13edf7fSJoe Perches# do {} while (0) macro tests:
4611b13edf7fSJoe Perches# single-statement macros do not need to be enclosed in do while (0) loop,
4612b13edf7fSJoe Perches# macro should not end with a semicolon
4613b13edf7fSJoe Perches		if ($^V && $^V ge 5.10.0 &&
4614b13edf7fSJoe Perches		    $realfile !~ m@/vmlinux.lds.h$@ &&
4615b13edf7fSJoe Perches		    $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
4616b13edf7fSJoe Perches			my $ln = $linenr;
4617b13edf7fSJoe Perches			my $cnt = $realcnt;
4618b13edf7fSJoe Perches			my ($off, $dstat, $dcond, $rest);
4619b13edf7fSJoe Perches			my $ctx = '';
4620b13edf7fSJoe Perches			($dstat, $dcond, $ln, $cnt, $off) =
4621b13edf7fSJoe Perches				ctx_statement_block($linenr, $realcnt, 0);
4622b13edf7fSJoe Perches			$ctx = $dstat;
4623b13edf7fSJoe Perches
4624b13edf7fSJoe Perches			$dstat =~ s/\\\n.//g;
46251b36b201SJoe Perches			$dstat =~ s/$;/ /g;
4626b13edf7fSJoe Perches
4627b13edf7fSJoe Perches			if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
4628b13edf7fSJoe Perches				my $stmts = $2;
4629b13edf7fSJoe Perches				my $semis = $3;
4630b13edf7fSJoe Perches
4631b13edf7fSJoe Perches				$ctx =~ s/\n*$//;
4632b13edf7fSJoe Perches				my $cnt = statement_rawlines($ctx);
4633b13edf7fSJoe Perches				my $herectx = $here . "\n";
4634b13edf7fSJoe Perches
4635b13edf7fSJoe Perches				for (my $n = 0; $n < $cnt; $n++) {
4636b13edf7fSJoe Perches					$herectx .= raw_line($linenr, $n) . "\n";
4637b13edf7fSJoe Perches				}
4638b13edf7fSJoe Perches
4639ac8e97f8SJoe Perches				if (($stmts =~ tr/;/;/) == 1 &&
4640ac8e97f8SJoe Perches				    $stmts !~ /^\s*(if|while|for|switch)\b/) {
4641b13edf7fSJoe Perches					WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
4642b13edf7fSJoe Perches					     "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
4643b13edf7fSJoe Perches				}
4644b13edf7fSJoe Perches				if (defined $semis && $semis ne "") {
4645b13edf7fSJoe Perches					WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
4646b13edf7fSJoe Perches					     "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
4647b13edf7fSJoe Perches				}
4648f5ef95b1SJoe Perches			} elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) {
4649f5ef95b1SJoe Perches				$ctx =~ s/\n*$//;
4650f5ef95b1SJoe Perches				my $cnt = statement_rawlines($ctx);
4651f5ef95b1SJoe Perches				my $herectx = $here . "\n";
4652f5ef95b1SJoe Perches
4653f5ef95b1SJoe Perches				for (my $n = 0; $n < $cnt; $n++) {
4654f5ef95b1SJoe Perches					$herectx .= raw_line($linenr, $n) . "\n";
4655f5ef95b1SJoe Perches				}
4656f5ef95b1SJoe Perches
4657f5ef95b1SJoe Perches				WARN("TRAILING_SEMICOLON",
4658f5ef95b1SJoe Perches				     "macros should not use a trailing semicolon\n" . "$herectx");
4659b13edf7fSJoe Perches			}
4660b13edf7fSJoe Perches		}
4661b13edf7fSJoe Perches
4662080ba929SMike Frysinger# make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
4663080ba929SMike Frysinger# all assignments may have only one of the following with an assignment:
4664080ba929SMike Frysinger#	.
4665080ba929SMike Frysinger#	ALIGN(...)
4666080ba929SMike Frysinger#	VMLINUX_SYMBOL(...)
4667080ba929SMike Frysinger		if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
4668000d1cc1SJoe Perches			WARN("MISSING_VMLINUX_SYMBOL",
4669000d1cc1SJoe Perches			     "vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
4670080ba929SMike Frysinger		}
4671080ba929SMike Frysinger
4672f0a594c1SAndy Whitcroft# check for redundant bracing round if etc
467313214adfSAndy Whitcroft		if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
467413214adfSAndy Whitcroft			my ($level, $endln, @chunks) =
4675cf655043SAndy Whitcroft				ctx_statement_full($linenr, $realcnt, 1);
467613214adfSAndy Whitcroft			#print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
4677cf655043SAndy Whitcroft			#print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
4678cf655043SAndy Whitcroft			if ($#chunks > 0 && $level == 0) {
4679aad4f614SJoe Perches				my @allowed = ();
4680aad4f614SJoe Perches				my $allow = 0;
468113214adfSAndy Whitcroft				my $seen = 0;
4682773647a0SAndy Whitcroft				my $herectx = $here . "\n";
4683cf655043SAndy Whitcroft				my $ln = $linenr - 1;
468413214adfSAndy Whitcroft				for my $chunk (@chunks) {
468513214adfSAndy Whitcroft					my ($cond, $block) = @{$chunk};
468613214adfSAndy Whitcroft
4687773647a0SAndy Whitcroft					# If the condition carries leading newlines, then count those as offsets.
4688773647a0SAndy Whitcroft					my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
4689773647a0SAndy Whitcroft					my $offset = statement_rawlines($whitespace) - 1;
4690773647a0SAndy Whitcroft
4691aad4f614SJoe Perches					$allowed[$allow] = 0;
4692773647a0SAndy Whitcroft					#print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
4693773647a0SAndy Whitcroft
4694773647a0SAndy Whitcroft					# We have looked at and allowed this specific line.
4695773647a0SAndy Whitcroft					$suppress_ifbraces{$ln + $offset} = 1;
4696773647a0SAndy Whitcroft
4697773647a0SAndy Whitcroft					$herectx .= "$rawlines[$ln + $offset]\n[...]\n";
4698cf655043SAndy Whitcroft					$ln += statement_rawlines($block) - 1;
4699cf655043SAndy Whitcroft
4700773647a0SAndy Whitcroft					substr($block, 0, length($cond), '');
470113214adfSAndy Whitcroft
470213214adfSAndy Whitcroft					$seen++ if ($block =~ /^\s*{/);
470313214adfSAndy Whitcroft
4704aad4f614SJoe Perches					#print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
4705cf655043SAndy Whitcroft					if (statement_lines($cond) > 1) {
4706cf655043SAndy Whitcroft						#print "APW: ALLOWED: cond<$cond>\n";
4707aad4f614SJoe Perches						$allowed[$allow] = 1;
470813214adfSAndy Whitcroft					}
470913214adfSAndy Whitcroft					if ($block =~/\b(?:if|for|while)\b/) {
4710cf655043SAndy Whitcroft						#print "APW: ALLOWED: block<$block>\n";
4711aad4f614SJoe Perches						$allowed[$allow] = 1;
471213214adfSAndy Whitcroft					}
4713cf655043SAndy Whitcroft					if (statement_block_size($block) > 1) {
4714cf655043SAndy Whitcroft						#print "APW: ALLOWED: lines block<$block>\n";
4715aad4f614SJoe Perches						$allowed[$allow] = 1;
471613214adfSAndy Whitcroft					}
4717aad4f614SJoe Perches					$allow++;
471813214adfSAndy Whitcroft				}
4719aad4f614SJoe Perches				if ($seen) {
4720aad4f614SJoe Perches					my $sum_allowed = 0;
4721aad4f614SJoe Perches					foreach (@allowed) {
4722aad4f614SJoe Perches						$sum_allowed += $_;
4723aad4f614SJoe Perches					}
4724aad4f614SJoe Perches					if ($sum_allowed == 0) {
4725000d1cc1SJoe Perches						WARN("BRACES",
4726000d1cc1SJoe Perches						     "braces {} are not necessary for any arm of this statement\n" . $herectx);
4727aad4f614SJoe Perches					} elsif ($sum_allowed != $allow &&
4728aad4f614SJoe Perches						 $seen != $allow) {
4729aad4f614SJoe Perches						CHK("BRACES",
4730aad4f614SJoe Perches						    "braces {} should be used on all arms of this statement\n" . $herectx);
4731aad4f614SJoe Perches					}
473213214adfSAndy Whitcroft				}
473313214adfSAndy Whitcroft			}
473413214adfSAndy Whitcroft		}
4735773647a0SAndy Whitcroft		if (!defined $suppress_ifbraces{$linenr - 1} &&
473613214adfSAndy Whitcroft					$line =~ /\b(if|while|for|else)\b/) {
4737cf655043SAndy Whitcroft			my $allowed = 0;
4738f0a594c1SAndy Whitcroft
4739cf655043SAndy Whitcroft			# Check the pre-context.
4740cf655043SAndy Whitcroft			if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
4741cf655043SAndy Whitcroft				#print "APW: ALLOWED: pre<$1>\n";
4742cf655043SAndy Whitcroft				$allowed = 1;
4743f0a594c1SAndy Whitcroft			}
4744773647a0SAndy Whitcroft
4745773647a0SAndy Whitcroft			my ($level, $endln, @chunks) =
4746773647a0SAndy Whitcroft				ctx_statement_full($linenr, $realcnt, $-[0]);
4747773647a0SAndy Whitcroft
4748cf655043SAndy Whitcroft			# Check the condition.
4749cf655043SAndy Whitcroft			my ($cond, $block) = @{$chunks[0]};
4750773647a0SAndy Whitcroft			#print "CHECKING<$linenr> cond<$cond> block<$block>\n";
4751cf655043SAndy Whitcroft			if (defined $cond) {
4752773647a0SAndy Whitcroft				substr($block, 0, length($cond), '');
4753cf655043SAndy Whitcroft			}
4754cf655043SAndy Whitcroft			if (statement_lines($cond) > 1) {
4755cf655043SAndy Whitcroft				#print "APW: ALLOWED: cond<$cond>\n";
4756cf655043SAndy Whitcroft				$allowed = 1;
4757cf655043SAndy Whitcroft			}
4758cf655043SAndy Whitcroft			if ($block =~/\b(?:if|for|while)\b/) {
4759cf655043SAndy Whitcroft				#print "APW: ALLOWED: block<$block>\n";
4760cf655043SAndy Whitcroft				$allowed = 1;
4761cf655043SAndy Whitcroft			}
4762cf655043SAndy Whitcroft			if (statement_block_size($block) > 1) {
4763cf655043SAndy Whitcroft				#print "APW: ALLOWED: lines block<$block>\n";
4764cf655043SAndy Whitcroft				$allowed = 1;
4765cf655043SAndy Whitcroft			}
4766cf655043SAndy Whitcroft			# Check the post-context.
4767cf655043SAndy Whitcroft			if (defined $chunks[1]) {
4768cf655043SAndy Whitcroft				my ($cond, $block) = @{$chunks[1]};
4769cf655043SAndy Whitcroft				if (defined $cond) {
4770773647a0SAndy Whitcroft					substr($block, 0, length($cond), '');
4771cf655043SAndy Whitcroft				}
4772cf655043SAndy Whitcroft				if ($block =~ /^\s*\{/) {
4773cf655043SAndy Whitcroft					#print "APW: ALLOWED: chunk-1 block<$block>\n";
4774cf655043SAndy Whitcroft					$allowed = 1;
4775cf655043SAndy Whitcroft				}
4776cf655043SAndy Whitcroft			}
4777cf655043SAndy Whitcroft			if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
477869932487SJustin P. Mattock				my $herectx = $here . "\n";
4779f055663cSAndy Whitcroft				my $cnt = statement_rawlines($block);
4780cf655043SAndy Whitcroft
4781f055663cSAndy Whitcroft				for (my $n = 0; $n < $cnt; $n++) {
478269932487SJustin P. Mattock					$herectx .= raw_line($linenr, $n) . "\n";
4783cf655043SAndy Whitcroft				}
4784cf655043SAndy Whitcroft
4785000d1cc1SJoe Perches				WARN("BRACES",
4786000d1cc1SJoe Perches				     "braces {} are not necessary for single statement blocks\n" . $herectx);
4787f0a594c1SAndy Whitcroft			}
4788f0a594c1SAndy Whitcroft		}
4789f0a594c1SAndy Whitcroft
47900979ae66SJoe Perches# check for unnecessary blank lines around braces
479177b9a53aSJoe Perches		if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
4792f8e58219SJoe Perches			if (CHK("BRACES",
4793f8e58219SJoe Perches				"Blank lines aren't necessary before a close brace '}'\n" . $hereprev) &&
4794f8e58219SJoe Perches			    $fix && $prevrawline =~ /^\+/) {
4795f8e58219SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
4796f8e58219SJoe Perches			}
47970979ae66SJoe Perches		}
479877b9a53aSJoe Perches		if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
4799f8e58219SJoe Perches			if (CHK("BRACES",
4800f8e58219SJoe Perches				"Blank lines aren't necessary after an open brace '{'\n" . $hereprev) &&
4801f8e58219SJoe Perches			    $fix) {
4802f8e58219SJoe Perches				fix_delete_line($fixlinenr, $rawline);
4803f8e58219SJoe Perches			}
48040979ae66SJoe Perches		}
48050979ae66SJoe Perches
48064a0df2efSAndy Whitcroft# no volatiles please
48076c72ffaaSAndy Whitcroft		my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
48086c72ffaaSAndy Whitcroft		if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
4809000d1cc1SJoe Perches			WARN("VOLATILE",
4810000d1cc1SJoe Perches			     "Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
48114a0df2efSAndy Whitcroft		}
48124a0df2efSAndy Whitcroft
48135e4f6ba5SJoe Perches# Check for user-visible strings broken across lines, which breaks the ability
48145e4f6ba5SJoe Perches# to grep for the string.  Make exceptions when the previous string ends in a
48155e4f6ba5SJoe Perches# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{'
48165e4f6ba5SJoe Perches# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value
481733acb54aSJoe Perches		if ($line =~ /^\+\s*$String/ &&
48185e4f6ba5SJoe Perches		    $prevline =~ /"\s*$/ &&
48195e4f6ba5SJoe Perches		    $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) {
48205e4f6ba5SJoe Perches			if (WARN("SPLIT_STRING",
48215e4f6ba5SJoe Perches				 "quoted string split across lines\n" . $hereprev) &&
48225e4f6ba5SJoe Perches				     $fix &&
48235e4f6ba5SJoe Perches				     $prevrawline =~ /^\+.*"\s*$/ &&
48245e4f6ba5SJoe Perches				     $last_coalesced_string_linenr != $linenr - 1) {
48255e4f6ba5SJoe Perches				my $extracted_string = get_quoted_string($line, $rawline);
48265e4f6ba5SJoe Perches				my $comma_close = "";
48275e4f6ba5SJoe Perches				if ($rawline =~ /\Q$extracted_string\E(\s*\)\s*;\s*$|\s*,\s*)/) {
48285e4f6ba5SJoe Perches					$comma_close = $1;
48295e4f6ba5SJoe Perches				}
48305e4f6ba5SJoe Perches
48315e4f6ba5SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
48325e4f6ba5SJoe Perches				fix_delete_line($fixlinenr, $rawline);
48335e4f6ba5SJoe Perches				my $fixedline = $prevrawline;
48345e4f6ba5SJoe Perches				$fixedline =~ s/"\s*$//;
48355e4f6ba5SJoe Perches				$fixedline .= substr($extracted_string, 1) . trim($comma_close);
48365e4f6ba5SJoe Perches				fix_insert_line($fixlinenr - 1, $fixedline);
48375e4f6ba5SJoe Perches				$fixedline = $rawline;
48385e4f6ba5SJoe Perches				$fixedline =~ s/\Q$extracted_string\E\Q$comma_close\E//;
48395e4f6ba5SJoe Perches				if ($fixedline !~ /\+\s*$/) {
48405e4f6ba5SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
48415e4f6ba5SJoe Perches				}
48425e4f6ba5SJoe Perches				$last_coalesced_string_linenr = $linenr;
48435e4f6ba5SJoe Perches			}
48445e4f6ba5SJoe Perches		}
48455e4f6ba5SJoe Perches
48465e4f6ba5SJoe Perches# check for missing a space in a string concatenation
48475e4f6ba5SJoe Perches		if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) {
48485e4f6ba5SJoe Perches			WARN('MISSING_SPACE',
48495e4f6ba5SJoe Perches			     "break quoted strings at a space character\n" . $hereprev);
48505e4f6ba5SJoe Perches		}
48515e4f6ba5SJoe Perches
48525e4f6ba5SJoe Perches# check for spaces before a quoted newline
48535e4f6ba5SJoe Perches		if ($rawline =~ /^.*\".*\s\\n/) {
48545e4f6ba5SJoe Perches			if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
48555e4f6ba5SJoe Perches				 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
48565e4f6ba5SJoe Perches			    $fix) {
48575e4f6ba5SJoe Perches				$fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
48585e4f6ba5SJoe Perches			}
48595e4f6ba5SJoe Perches
48605e4f6ba5SJoe Perches		}
48615e4f6ba5SJoe Perches
4862f17dba4fSJoe Perches# concatenated string without spaces between elements
486333acb54aSJoe Perches		if ($line =~ /$String[A-Z_]/ || $line =~ /[A-Za-z0-9_]$String/) {
4864f17dba4fSJoe Perches			CHK("CONCATENATED_STRING",
4865f17dba4fSJoe Perches			    "Concatenated strings should use spaces between elements\n" . $herecurr);
4866f17dba4fSJoe Perches		}
4867f17dba4fSJoe Perches
486890ad30e5SJoe Perches# uncoalesced string fragments
486933acb54aSJoe Perches		if ($line =~ /$String\s*"/) {
487090ad30e5SJoe Perches			WARN("STRING_FRAGMENTS",
487190ad30e5SJoe Perches			     "Consecutive strings are generally better as a single string\n" . $herecurr);
487290ad30e5SJoe Perches		}
487390ad30e5SJoe Perches
48746e300757SJoe Perches# check for %L{u,d,i} and 0x%[udi] in strings
48755e4f6ba5SJoe Perches		my $string;
48765e4f6ba5SJoe Perches		while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
48775e4f6ba5SJoe Perches			$string = substr($rawline, $-[1], $+[1] - $-[1]);
48785e4f6ba5SJoe Perches			$string =~ s/%%/__/g;
48796e300757SJoe Perches			if ($string =~ /(?<!%)%[\*\d\.\$]*L[udi]/) {
48805e4f6ba5SJoe Perches				WARN("PRINTF_L",
48815e4f6ba5SJoe Perches				     "\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
48825e4f6ba5SJoe Perches				last;
48835e4f6ba5SJoe Perches			}
48846e300757SJoe Perches			if ($string =~ /0x%[\*\d\.\$\Llzth]*[udi]/) {
48856e300757SJoe Perches				ERROR("PRINTF_0xDECIMAL",
48866e300757SJoe Perches				      "Prefixing 0x with decimal output is defective\n" . $herecurr);
48876e300757SJoe Perches			}
48885e4f6ba5SJoe Perches		}
48895e4f6ba5SJoe Perches
48905e4f6ba5SJoe Perches# check for line continuations in quoted strings with odd counts of "
48915e4f6ba5SJoe Perches		if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) {
48925e4f6ba5SJoe Perches			WARN("LINE_CONTINUATIONS",
48935e4f6ba5SJoe Perches			     "Avoid line continuations in quoted strings\n" . $herecurr);
48945e4f6ba5SJoe Perches		}
48955e4f6ba5SJoe Perches
489600df344fSAndy Whitcroft# warn about #if 0
4897c45dcabdSAndy Whitcroft		if ($line =~ /^.\s*\#\s*if\s+0\b/) {
4898000d1cc1SJoe Perches			CHK("REDUNDANT_CODE",
4899000d1cc1SJoe Perches			    "if this code is redundant consider removing it\n" .
4900de7d4f0eSAndy Whitcroft				$herecurr);
49014a0df2efSAndy Whitcroft		}
49024a0df2efSAndy Whitcroft
490303df4b51SAndy Whitcroft# check for needless "if (<foo>) fn(<foo>)" uses
490403df4b51SAndy Whitcroft		if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
4905100425deSJoe Perches			my $tested = quotemeta($1);
4906100425deSJoe Perches			my $expr = '\s*\(\s*' . $tested . '\s*\)\s*;';
4907100425deSJoe Perches			if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?|(?:kmem_cache|mempool|dma_pool)_destroy)$expr/) {
4908100425deSJoe Perches				my $func = $1;
4909100425deSJoe Perches				if (WARN('NEEDLESS_IF',
4910100425deSJoe Perches					 "$func(NULL) is safe and this check is probably not required\n" . $hereprev) &&
4911100425deSJoe Perches				    $fix) {
4912100425deSJoe Perches					my $do_fix = 1;
4913100425deSJoe Perches					my $leading_tabs = "";
4914100425deSJoe Perches					my $new_leading_tabs = "";
4915100425deSJoe Perches					if ($lines[$linenr - 2] =~ /^\+(\t*)if\s*\(\s*$tested\s*\)\s*$/) {
4916100425deSJoe Perches						$leading_tabs = $1;
4917100425deSJoe Perches					} else {
4918100425deSJoe Perches						$do_fix = 0;
4919100425deSJoe Perches					}
4920100425deSJoe Perches					if ($lines[$linenr - 1] =~ /^\+(\t+)$func\s*\(\s*$tested\s*\)\s*;\s*$/) {
4921100425deSJoe Perches						$new_leading_tabs = $1;
4922100425deSJoe Perches						if (length($leading_tabs) + 1 ne length($new_leading_tabs)) {
4923100425deSJoe Perches							$do_fix = 0;
4924100425deSJoe Perches						}
4925100425deSJoe Perches					} else {
4926100425deSJoe Perches						$do_fix = 0;
4927100425deSJoe Perches					}
4928100425deSJoe Perches					if ($do_fix) {
4929100425deSJoe Perches						fix_delete_line($fixlinenr - 1, $prevrawline);
4930100425deSJoe Perches						$fixed[$fixlinenr] =~ s/^\+$new_leading_tabs/\+$leading_tabs/;
4931100425deSJoe Perches					}
4932100425deSJoe Perches				}
49334c432a8fSGreg Kroah-Hartman			}
49344c432a8fSGreg Kroah-Hartman		}
4935f0a594c1SAndy Whitcroft
4936ebfdc409SJoe Perches# check for unnecessary "Out of Memory" messages
4937ebfdc409SJoe Perches		if ($line =~ /^\+.*\b$logFunctions\s*\(/ &&
4938ebfdc409SJoe Perches		    $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ &&
4939ebfdc409SJoe Perches		    (defined $1 || defined $3) &&
4940ebfdc409SJoe Perches		    $linenr > 3) {
4941ebfdc409SJoe Perches			my $testval = $2;
4942ebfdc409SJoe Perches			my $testline = $lines[$linenr - 3];
4943ebfdc409SJoe Perches
4944ebfdc409SJoe Perches			my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0);
4945ebfdc409SJoe Perches#			print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n");
4946ebfdc409SJoe Perches
4947ebfdc409SJoe 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)/) {
4948ebfdc409SJoe Perches				WARN("OOM_MESSAGE",
4949ebfdc409SJoe Perches				     "Possible unnecessary 'out of memory' message\n" . $hereprev);
4950ebfdc409SJoe Perches			}
4951ebfdc409SJoe Perches		}
4952ebfdc409SJoe Perches
4953f78d98f6SJoe Perches# check for logging functions with KERN_<LEVEL>
4954dcaf1123SPaolo Bonzini		if ($line !~ /printk(?:_ratelimited|_once)?\s*\(/ &&
4955f78d98f6SJoe Perches		    $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) {
4956f78d98f6SJoe Perches			my $level = $1;
4957f78d98f6SJoe Perches			if (WARN("UNNECESSARY_KERN_LEVEL",
4958f78d98f6SJoe Perches				 "Possible unnecessary $level\n" . $herecurr) &&
4959f78d98f6SJoe Perches			    $fix) {
4960f78d98f6SJoe Perches				$fixed[$fixlinenr] =~ s/\s*$level\s*//;
4961f78d98f6SJoe Perches			}
4962f78d98f6SJoe Perches		}
4963f78d98f6SJoe Perches
4964abb08a53SJoe Perches# check for mask then right shift without a parentheses
4965abb08a53SJoe Perches		if ($^V && $^V ge 5.10.0 &&
4966abb08a53SJoe Perches		    $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ &&
4967abb08a53SJoe Perches		    $4 !~ /^\&/) { # $LvalOrFunc may be &foo, ignore if so
4968abb08a53SJoe Perches			WARN("MASK_THEN_SHIFT",
4969abb08a53SJoe Perches			     "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr);
4970abb08a53SJoe Perches		}
4971abb08a53SJoe Perches
4972b75ac618SJoe Perches# check for pointer comparisons to NULL
4973b75ac618SJoe Perches		if ($^V && $^V ge 5.10.0) {
4974b75ac618SJoe Perches			while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) {
4975b75ac618SJoe Perches				my $val = $1;
4976b75ac618SJoe Perches				my $equal = "!";
4977b75ac618SJoe Perches				$equal = "" if ($4 eq "!=");
4978b75ac618SJoe Perches				if (CHK("COMPARISON_TO_NULL",
4979b75ac618SJoe Perches					"Comparison to NULL could be written \"${equal}${val}\"\n" . $herecurr) &&
4980b75ac618SJoe Perches					    $fix) {
4981b75ac618SJoe Perches					$fixed[$fixlinenr] =~ s/\b\Q$val\E\s*(?:==|\!=)\s*NULL\b/$equal$val/;
4982b75ac618SJoe Perches				}
4983b75ac618SJoe Perches			}
4984b75ac618SJoe Perches		}
4985b75ac618SJoe Perches
49868716de38SJoe Perches# check for bad placement of section $InitAttribute (e.g.: __initdata)
49878716de38SJoe Perches		if ($line =~ /(\b$InitAttribute\b)/) {
49888716de38SJoe Perches			my $attr = $1;
49898716de38SJoe Perches			if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
49908716de38SJoe Perches				my $ptr = $1;
49918716de38SJoe Perches				my $var = $2;
49928716de38SJoe Perches				if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
49938716de38SJoe Perches				      ERROR("MISPLACED_INIT",
49948716de38SJoe Perches					    "$attr should be placed after $var\n" . $herecurr)) ||
49958716de38SJoe Perches				     ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
49968716de38SJoe Perches				      WARN("MISPLACED_INIT",
49978716de38SJoe Perches					   "$attr should be placed after $var\n" . $herecurr))) &&
49988716de38SJoe Perches				    $fix) {
4999194f66fcSJoe 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;
50008716de38SJoe Perches				}
50018716de38SJoe Perches			}
50028716de38SJoe Perches		}
50038716de38SJoe Perches
5004e970b884SJoe Perches# check for $InitAttributeData (ie: __initdata) with const
5005e970b884SJoe Perches		if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
5006e970b884SJoe Perches			my $attr = $1;
5007e970b884SJoe Perches			$attr =~ /($InitAttributePrefix)(.*)/;
5008e970b884SJoe Perches			my $attr_prefix = $1;
5009e970b884SJoe Perches			my $attr_type = $2;
5010e970b884SJoe Perches			if (ERROR("INIT_ATTRIBUTE",
5011e970b884SJoe Perches				  "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
5012e970b884SJoe Perches			    $fix) {
5013194f66fcSJoe Perches				$fixed[$fixlinenr] =~
5014e970b884SJoe Perches				    s/$InitAttributeData/${attr_prefix}initconst/;
5015e970b884SJoe Perches			}
5016e970b884SJoe Perches		}
5017e970b884SJoe Perches
5018e970b884SJoe Perches# check for $InitAttributeConst (ie: __initconst) without const
5019e970b884SJoe Perches		if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
5020e970b884SJoe Perches			my $attr = $1;
5021e970b884SJoe Perches			if (ERROR("INIT_ATTRIBUTE",
5022e970b884SJoe Perches				  "Use of $attr requires a separate use of const\n" . $herecurr) &&
5023e970b884SJoe Perches			    $fix) {
5024194f66fcSJoe Perches				my $lead = $fixed[$fixlinenr] =~
5025e970b884SJoe Perches				    /(^\+\s*(?:static\s+))/;
5026e970b884SJoe Perches				$lead = rtrim($1);
5027e970b884SJoe Perches				$lead = "$lead " if ($lead !~ /^\+$/);
5028e970b884SJoe Perches				$lead = "${lead}const ";
5029194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/;
5030e970b884SJoe Perches			}
5031e970b884SJoe Perches		}
5032e970b884SJoe Perches
5033c17893c7SJoe Perches# check for __read_mostly with const non-pointer (should just be const)
5034c17893c7SJoe Perches		if ($line =~ /\b__read_mostly\b/ &&
5035c17893c7SJoe Perches		    $line =~ /($Type)\s*$Ident/ && $1 !~ /\*\s*$/ && $1 =~ /\bconst\b/) {
5036c17893c7SJoe Perches			if (ERROR("CONST_READ_MOSTLY",
5037c17893c7SJoe Perches				  "Invalid use of __read_mostly with const type\n" . $herecurr) &&
5038c17893c7SJoe Perches			    $fix) {
5039c17893c7SJoe Perches				$fixed[$fixlinenr] =~ s/\s+__read_mostly\b//;
5040c17893c7SJoe Perches			}
5041c17893c7SJoe Perches		}
5042c17893c7SJoe Perches
5043fbdb8138SJoe Perches# don't use __constant_<foo> functions outside of include/uapi/
5044fbdb8138SJoe Perches		if ($realfile !~ m@^include/uapi/@ &&
5045fbdb8138SJoe Perches		    $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) {
5046fbdb8138SJoe Perches			my $constant_func = $1;
5047fbdb8138SJoe Perches			my $func = $constant_func;
5048fbdb8138SJoe Perches			$func =~ s/^__constant_//;
5049fbdb8138SJoe Perches			if (WARN("CONSTANT_CONVERSION",
5050fbdb8138SJoe Perches				 "$constant_func should be $func\n" . $herecurr) &&
5051fbdb8138SJoe Perches			    $fix) {
5052194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g;
5053fbdb8138SJoe Perches			}
5054fbdb8138SJoe Perches		}
5055fbdb8138SJoe Perches
50561a15a250SPatrick Pannuto# prefer usleep_range over udelay
505737581c28SBruce Allan		if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
505843c1d77cSJoe Perches			my $delay = $1;
50591a15a250SPatrick Pannuto			# ignore udelay's < 10, however
506043c1d77cSJoe Perches			if (! ($delay < 10) ) {
5061000d1cc1SJoe Perches				CHK("USLEEP_RANGE",
506243c1d77cSJoe Perches				    "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $herecurr);
506343c1d77cSJoe Perches			}
506443c1d77cSJoe Perches			if ($delay > 2000) {
506543c1d77cSJoe Perches				WARN("LONG_UDELAY",
506643c1d77cSJoe Perches				     "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr);
50671a15a250SPatrick Pannuto			}
50681a15a250SPatrick Pannuto		}
50691a15a250SPatrick Pannuto
507009ef8725SPatrick Pannuto# warn about unexpectedly long msleep's
507109ef8725SPatrick Pannuto		if ($line =~ /\bmsleep\s*\((\d+)\);/) {
507209ef8725SPatrick Pannuto			if ($1 < 20) {
5073000d1cc1SJoe Perches				WARN("MSLEEP",
507443c1d77cSJoe Perches				     "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $herecurr);
507509ef8725SPatrick Pannuto			}
507609ef8725SPatrick Pannuto		}
507709ef8725SPatrick Pannuto
507836ec1939SJoe Perches# check for comparisons of jiffies
507936ec1939SJoe Perches		if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
508036ec1939SJoe Perches			WARN("JIFFIES_COMPARISON",
508136ec1939SJoe Perches			     "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
508236ec1939SJoe Perches		}
508336ec1939SJoe Perches
50849d7a34a5SJoe Perches# check for comparisons of get_jiffies_64()
50859d7a34a5SJoe Perches		if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
50869d7a34a5SJoe Perches			WARN("JIFFIES_COMPARISON",
50879d7a34a5SJoe Perches			     "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
50889d7a34a5SJoe Perches		}
50899d7a34a5SJoe Perches
509000df344fSAndy Whitcroft# warn about #ifdefs in C files
5091c45dcabdSAndy Whitcroft#		if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
509200df344fSAndy Whitcroft#			print "#ifdef in C files should be avoided\n";
509300df344fSAndy Whitcroft#			print "$herecurr";
509400df344fSAndy Whitcroft#			$clean = 0;
509500df344fSAndy Whitcroft#		}
509600df344fSAndy Whitcroft
509722f2a2efSAndy Whitcroft# warn about spacing in #ifdefs
5098c45dcabdSAndy Whitcroft		if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
50993705ce5bSJoe Perches			if (ERROR("SPACING",
51003705ce5bSJoe Perches				  "exactly one space required after that #$1\n" . $herecurr) &&
51013705ce5bSJoe Perches			    $fix) {
5102194f66fcSJoe Perches				$fixed[$fixlinenr] =~
51033705ce5bSJoe Perches				    s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
51043705ce5bSJoe Perches			}
51053705ce5bSJoe Perches
510622f2a2efSAndy Whitcroft		}
510722f2a2efSAndy Whitcroft
51084a0df2efSAndy Whitcroft# check for spinlock_t definitions without a comment.
5109171ae1a4SAndy Whitcroft		if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
5110171ae1a4SAndy Whitcroft		    $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
51114a0df2efSAndy Whitcroft			my $which = $1;
51124a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
5113000d1cc1SJoe Perches				CHK("UNCOMMENTED_DEFINITION",
5114000d1cc1SJoe Perches				    "$1 definition without comment\n" . $herecurr);
51154a0df2efSAndy Whitcroft			}
51164a0df2efSAndy Whitcroft		}
51174a0df2efSAndy Whitcroft# check for memory barriers without a comment.
51184a0df2efSAndy Whitcroft		if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
51194a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
5120c1fd7bb9SJoe Perches				WARN("MEMORY_BARRIER",
5121000d1cc1SJoe Perches				     "memory barrier without comment\n" . $herecurr);
51224a0df2efSAndy Whitcroft			}
51234a0df2efSAndy Whitcroft		}
51243ad81779SPaul E. McKenney
5125cb426e99SJoe Perches# check for waitqueue_active without a comment.
5126cb426e99SJoe Perches		if ($line =~ /\bwaitqueue_active\s*\(/) {
5127cb426e99SJoe Perches			if (!ctx_has_comment($first_line, $linenr)) {
5128cb426e99SJoe Perches				WARN("WAITQUEUE_ACTIVE",
5129cb426e99SJoe Perches				     "waitqueue_active without comment\n" . $herecurr);
5130cb426e99SJoe Perches			}
5131cb426e99SJoe Perches		}
51323ad81779SPaul E. McKenney
51333ad81779SPaul E. McKenney# Check for expedited grace periods that interrupt non-idle non-nohz
51343ad81779SPaul E. McKenney# online CPUs.  These expedited can therefore degrade real-time response
51353ad81779SPaul E. McKenney# if used carelessly, and should be avoided where not absolutely
51363ad81779SPaul E. McKenney# needed.  It is always OK to use synchronize_rcu_expedited() and
51373ad81779SPaul E. McKenney# synchronize_sched_expedited() at boot time (before real-time applications
51383ad81779SPaul E. McKenney# start) and in error situations where real-time response is compromised in
51393ad81779SPaul E. McKenney# any case.  Note that synchronize_srcu_expedited() does -not- interrupt
51403ad81779SPaul E. McKenney# other CPUs, so don't warn on uses of synchronize_srcu_expedited().
51413ad81779SPaul E. McKenney# Of course, nothing comes for free, and srcu_read_lock() and
51423ad81779SPaul E. McKenney# srcu_read_unlock() do contain full memory barriers in payment for
51433ad81779SPaul E. McKenney# synchronize_srcu_expedited() non-interruption properties.
51443ad81779SPaul E. McKenney		if ($line =~ /\b(synchronize_rcu_expedited|synchronize_sched_expedited)\(/) {
51453ad81779SPaul E. McKenney			WARN("EXPEDITED_RCU_GRACE_PERIOD",
51463ad81779SPaul E. McKenney			     "expedited RCU grace periods should be avoided where they can degrade real-time response\n" . $herecurr);
51473ad81779SPaul E. McKenney
51483ad81779SPaul E. McKenney		}
51493ad81779SPaul E. McKenney
51504a0df2efSAndy Whitcroft# check of hardware specific defines
5151c45dcabdSAndy Whitcroft		if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
5152000d1cc1SJoe Perches			CHK("ARCH_DEFINES",
5153000d1cc1SJoe Perches			    "architecture specific defines should be avoided\n" .  $herecurr);
51540a920b5bSAndy Whitcroft		}
5155653d4876SAndy Whitcroft
5156d4977c78STobias Klauser# Check that the storage class is at the beginning of a declaration
5157d4977c78STobias Klauser		if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
5158000d1cc1SJoe Perches			WARN("STORAGE_CLASS",
5159000d1cc1SJoe Perches			     "storage class should be at the beginning of the declaration\n" . $herecurr)
5160d4977c78STobias Klauser		}
5161d4977c78STobias Klauser
5162de7d4f0eSAndy Whitcroft# check the location of the inline attribute, that it is between
5163de7d4f0eSAndy Whitcroft# storage class and type.
51649c0ca6f9SAndy Whitcroft		if ($line =~ /\b$Type\s+$Inline\b/ ||
51659c0ca6f9SAndy Whitcroft		    $line =~ /\b$Inline\s+$Storage\b/) {
5166000d1cc1SJoe Perches			ERROR("INLINE_LOCATION",
5167000d1cc1SJoe Perches			      "inline keyword should sit between storage class and type\n" . $herecurr);
5168de7d4f0eSAndy Whitcroft		}
5169de7d4f0eSAndy Whitcroft
51708905a67cSAndy Whitcroft# Check for __inline__ and __inline, prefer inline
51712b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
51722b7ab453SJoe Perches		    $line =~ /\b(__inline__|__inline)\b/) {
5173d5e616fcSJoe Perches			if (WARN("INLINE",
5174d5e616fcSJoe Perches				 "plain inline is preferred over $1\n" . $herecurr) &&
5175d5e616fcSJoe Perches			    $fix) {
5176194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/;
5177d5e616fcSJoe Perches
5178d5e616fcSJoe Perches			}
51798905a67cSAndy Whitcroft		}
51808905a67cSAndy Whitcroft
51813d130fd0SJoe Perches# Check for __attribute__ packed, prefer __packed
51822b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
51832b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
5184000d1cc1SJoe Perches			WARN("PREFER_PACKED",
5185000d1cc1SJoe Perches			     "__packed is preferred over __attribute__((packed))\n" . $herecurr);
51863d130fd0SJoe Perches		}
51873d130fd0SJoe Perches
518839b7e287SJoe Perches# Check for __attribute__ aligned, prefer __aligned
51892b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
51902b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
5191000d1cc1SJoe Perches			WARN("PREFER_ALIGNED",
5192000d1cc1SJoe Perches			     "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
519339b7e287SJoe Perches		}
519439b7e287SJoe Perches
51955f14d3bdSJoe Perches# Check for __attribute__ format(printf, prefer __printf
51962b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
51972b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
5198d5e616fcSJoe Perches			if (WARN("PREFER_PRINTF",
5199d5e616fcSJoe Perches				 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) &&
5200d5e616fcSJoe Perches			    $fix) {
5201194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf\s*,\s*(.*)\)\s*\)\s*\)/"__printf(" . trim($1) . ")"/ex;
5202d5e616fcSJoe Perches
5203d5e616fcSJoe Perches			}
52045f14d3bdSJoe Perches		}
52055f14d3bdSJoe Perches
52066061d949SJoe Perches# Check for __attribute__ format(scanf, prefer __scanf
52072b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
52082b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
5209d5e616fcSJoe Perches			if (WARN("PREFER_SCANF",
5210d5e616fcSJoe Perches				 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) &&
5211d5e616fcSJoe Perches			    $fix) {
5212194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\s*,\s*(.*)\)\s*\)\s*\)/"__scanf(" . trim($1) . ")"/ex;
5213d5e616fcSJoe Perches			}
52146061d949SJoe Perches		}
52156061d949SJoe Perches
5216619a908aSJoe Perches# Check for __attribute__ weak, or __weak declarations (may have link issues)
5217619a908aSJoe Perches		if ($^V && $^V ge 5.10.0 &&
5218619a908aSJoe Perches		    $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ &&
5219619a908aSJoe Perches		    ($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ ||
5220619a908aSJoe Perches		     $line =~ /\b__weak\b/)) {
5221619a908aSJoe Perches			ERROR("WEAK_DECLARATION",
5222619a908aSJoe Perches			      "Using weak declarations can have unintended link defects\n" . $herecurr);
5223619a908aSJoe Perches		}
5224619a908aSJoe Perches
5225e6176fa4SJoe Perches# check for c99 types like uint8_t used outside of uapi/
5226e6176fa4SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
5227e6176fa4SJoe Perches		    $line =~ /\b($Declare)\s*$Ident\s*[=;,\[]/) {
5228e6176fa4SJoe Perches			my $type = $1;
5229e6176fa4SJoe Perches			if ($type =~ /\b($typeC99Typedefs)\b/) {
5230e6176fa4SJoe Perches				$type = $1;
5231e6176fa4SJoe Perches				my $kernel_type = 'u';
5232e6176fa4SJoe Perches				$kernel_type = 's' if ($type =~ /^_*[si]/);
5233e6176fa4SJoe Perches				$type =~ /(\d+)/;
5234e6176fa4SJoe Perches				$kernel_type .= $1;
5235e6176fa4SJoe Perches				if (CHK("PREFER_KERNEL_TYPES",
5236e6176fa4SJoe Perches					"Prefer kernel type '$kernel_type' over '$type'\n" . $herecurr) &&
5237e6176fa4SJoe Perches				    $fix) {
5238e6176fa4SJoe Perches					$fixed[$fixlinenr] =~ s/\b$type\b/$kernel_type/;
5239e6176fa4SJoe Perches				}
5240e6176fa4SJoe Perches			}
5241e6176fa4SJoe Perches		}
5242e6176fa4SJoe Perches
52438f53a9b8SJoe Perches# check for sizeof(&)
52448f53a9b8SJoe Perches		if ($line =~ /\bsizeof\s*\(\s*\&/) {
5245000d1cc1SJoe Perches			WARN("SIZEOF_ADDRESS",
5246000d1cc1SJoe Perches			     "sizeof(& should be avoided\n" . $herecurr);
52478f53a9b8SJoe Perches		}
52488f53a9b8SJoe Perches
524966c80b60SJoe Perches# check for sizeof without parenthesis
525066c80b60SJoe Perches		if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
5251d5e616fcSJoe Perches			if (WARN("SIZEOF_PARENTHESIS",
5252d5e616fcSJoe Perches				 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
5253d5e616fcSJoe Perches			    $fix) {
5254194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
5255d5e616fcSJoe Perches			}
525666c80b60SJoe Perches		}
525766c80b60SJoe Perches
525888982feaSJoe Perches# check for struct spinlock declarations
525988982feaSJoe Perches		if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
526088982feaSJoe Perches			WARN("USE_SPINLOCK_T",
526188982feaSJoe Perches			     "struct spinlock should be spinlock_t\n" . $herecurr);
526288982feaSJoe Perches		}
526388982feaSJoe Perches
5264a6962d72SJoe Perches# check for seq_printf uses that could be seq_puts
526506668727SJoe Perches		if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
5266a6962d72SJoe Perches			my $fmt = get_quoted_string($line, $rawline);
5267caac1d5fSHeba Aamer			$fmt =~ s/%%//g;
5268caac1d5fSHeba Aamer			if ($fmt !~ /%/) {
5269d5e616fcSJoe Perches				if (WARN("PREFER_SEQ_PUTS",
5270d5e616fcSJoe Perches					 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
5271d5e616fcSJoe Perches				    $fix) {
5272194f66fcSJoe Perches					$fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/;
5273d5e616fcSJoe Perches				}
5274a6962d72SJoe Perches			}
5275a6962d72SJoe Perches		}
5276a6962d72SJoe Perches
5277554e165cSAndy Whitcroft# Check for misused memsets
5278d1fe9c09SJoe Perches		if ($^V && $^V ge 5.10.0 &&
5279d1fe9c09SJoe Perches		    defined $stat &&
52809e20a853SMateusz Kulikowski		    $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/) {
5281554e165cSAndy Whitcroft
5282d7c76ba7SJoe Perches			my $ms_addr = $2;
5283d1fe9c09SJoe Perches			my $ms_val = $7;
5284d1fe9c09SJoe Perches			my $ms_size = $12;
5285d7c76ba7SJoe Perches
5286554e165cSAndy Whitcroft			if ($ms_size =~ /^(0x|)0$/i) {
5287554e165cSAndy Whitcroft				ERROR("MEMSET",
5288d7c76ba7SJoe Perches				      "memset to 0's uses 0 as the 2nd argument, not the 3rd\n" . "$here\n$stat\n");
5289554e165cSAndy Whitcroft			} elsif ($ms_size =~ /^(0x|)1$/i) {
5290554e165cSAndy Whitcroft				WARN("MEMSET",
5291d7c76ba7SJoe Perches				     "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
5292d7c76ba7SJoe Perches			}
5293d7c76ba7SJoe Perches		}
5294d7c76ba7SJoe Perches
529598a9bba5SJoe Perches# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar)
529698a9bba5SJoe Perches		if ($^V && $^V ge 5.10.0 &&
529710895d2cSMateusz Kulikowski		    defined $stat &&
529810895d2cSMateusz Kulikowski		    $stat =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
529998a9bba5SJoe Perches			if (WARN("PREFER_ETHER_ADDR_COPY",
530010895d2cSMateusz Kulikowski				 "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . "$here\n$stat\n") &&
530198a9bba5SJoe Perches			    $fix) {
5302194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/;
530398a9bba5SJoe Perches			}
530498a9bba5SJoe Perches		}
530598a9bba5SJoe Perches
5306b6117d17SMateusz Kulikowski# Check for memcmp(foo, bar, ETH_ALEN) that could be ether_addr_equal*(foo, bar)
5307b6117d17SMateusz Kulikowski		if ($^V && $^V ge 5.10.0 &&
5308b6117d17SMateusz Kulikowski		    defined $stat &&
5309b6117d17SMateusz Kulikowski		    $stat =~ /^\+(?:.*?)\bmemcmp\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
5310b6117d17SMateusz Kulikowski			WARN("PREFER_ETHER_ADDR_EQUAL",
5311b6117d17SMateusz Kulikowski			     "Prefer ether_addr_equal() or ether_addr_equal_unaligned() over memcmp()\n" . "$here\n$stat\n")
5312b6117d17SMateusz Kulikowski		}
5313b6117d17SMateusz Kulikowski
53148617cd09SMateusz Kulikowski# check for memset(foo, 0x0, ETH_ALEN) that could be eth_zero_addr
53158617cd09SMateusz Kulikowski# check for memset(foo, 0xFF, ETH_ALEN) that could be eth_broadcast_addr
53168617cd09SMateusz Kulikowski		if ($^V && $^V ge 5.10.0 &&
53178617cd09SMateusz Kulikowski		    defined $stat &&
53188617cd09SMateusz Kulikowski		    $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
53198617cd09SMateusz Kulikowski
53208617cd09SMateusz Kulikowski			my $ms_val = $7;
53218617cd09SMateusz Kulikowski
53228617cd09SMateusz Kulikowski			if ($ms_val =~ /^(?:0x|)0+$/i) {
53238617cd09SMateusz Kulikowski				if (WARN("PREFER_ETH_ZERO_ADDR",
53248617cd09SMateusz Kulikowski					 "Prefer eth_zero_addr over memset()\n" . "$here\n$stat\n") &&
53258617cd09SMateusz Kulikowski				    $fix) {
53268617cd09SMateusz Kulikowski					$fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_zero_addr($2)/;
53278617cd09SMateusz Kulikowski				}
53288617cd09SMateusz Kulikowski			} elsif ($ms_val =~ /^(?:0xff|255)$/i) {
53298617cd09SMateusz Kulikowski				if (WARN("PREFER_ETH_BROADCAST_ADDR",
53308617cd09SMateusz Kulikowski					 "Prefer eth_broadcast_addr() over memset()\n" . "$here\n$stat\n") &&
53318617cd09SMateusz Kulikowski				    $fix) {
53328617cd09SMateusz Kulikowski					$fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_broadcast_addr($2)/;
53338617cd09SMateusz Kulikowski				}
53348617cd09SMateusz Kulikowski			}
53358617cd09SMateusz Kulikowski		}
53368617cd09SMateusz Kulikowski
5337d7c76ba7SJoe Perches# typecasts on min/max could be min_t/max_t
5338d1fe9c09SJoe Perches		if ($^V && $^V ge 5.10.0 &&
5339d1fe9c09SJoe Perches		    defined $stat &&
5340d7c76ba7SJoe Perches		    $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
5341d1fe9c09SJoe Perches			if (defined $2 || defined $7) {
5342d7c76ba7SJoe Perches				my $call = $1;
5343d7c76ba7SJoe Perches				my $cast1 = deparenthesize($2);
5344d7c76ba7SJoe Perches				my $arg1 = $3;
5345d1fe9c09SJoe Perches				my $cast2 = deparenthesize($7);
5346d1fe9c09SJoe Perches				my $arg2 = $8;
5347d7c76ba7SJoe Perches				my $cast;
5348d7c76ba7SJoe Perches
5349d1fe9c09SJoe Perches				if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
5350d7c76ba7SJoe Perches					$cast = "$cast1 or $cast2";
5351d7c76ba7SJoe Perches				} elsif ($cast1 ne "") {
5352d7c76ba7SJoe Perches					$cast = $cast1;
5353d7c76ba7SJoe Perches				} else {
5354d7c76ba7SJoe Perches					$cast = $cast2;
5355d7c76ba7SJoe Perches				}
5356d7c76ba7SJoe Perches				WARN("MINMAX",
5357d7c76ba7SJoe Perches				     "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
5358554e165cSAndy Whitcroft			}
5359554e165cSAndy Whitcroft		}
5360554e165cSAndy Whitcroft
53614a273195SJoe Perches# check usleep_range arguments
53624a273195SJoe Perches		if ($^V && $^V ge 5.10.0 &&
53634a273195SJoe Perches		    defined $stat &&
53644a273195SJoe Perches		    $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
53654a273195SJoe Perches			my $min = $1;
53664a273195SJoe Perches			my $max = $7;
53674a273195SJoe Perches			if ($min eq $max) {
53684a273195SJoe Perches				WARN("USLEEP_RANGE",
53694a273195SJoe Perches				     "usleep_range should not use min == max args; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
53704a273195SJoe Perches			} elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
53714a273195SJoe Perches				 $min > $max) {
53724a273195SJoe Perches				WARN("USLEEP_RANGE",
53734a273195SJoe Perches				     "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
53744a273195SJoe Perches			}
53754a273195SJoe Perches		}
53764a273195SJoe Perches
5377823b794cSJoe Perches# check for naked sscanf
5378823b794cSJoe Perches		if ($^V && $^V ge 5.10.0 &&
5379823b794cSJoe Perches		    defined $stat &&
53806c8bd707SJoe Perches		    $line =~ /\bsscanf\b/ &&
5381823b794cSJoe Perches		    ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
5382823b794cSJoe Perches		     $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ &&
5383823b794cSJoe Perches		     $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) {
5384823b794cSJoe Perches			my $lc = $stat =~ tr@\n@@;
5385823b794cSJoe Perches			$lc = $lc + $linenr;
5386823b794cSJoe Perches			my $stat_real = raw_line($linenr, 0);
5387823b794cSJoe Perches		        for (my $count = $linenr + 1; $count <= $lc; $count++) {
5388823b794cSJoe Perches				$stat_real = $stat_real . "\n" . raw_line($count, 0);
5389823b794cSJoe Perches			}
5390823b794cSJoe Perches			WARN("NAKED_SSCANF",
5391823b794cSJoe Perches			     "unchecked sscanf return value\n" . "$here\n$stat_real\n");
5392823b794cSJoe Perches		}
5393823b794cSJoe Perches
5394afc819abSJoe Perches# check for simple sscanf that should be kstrto<foo>
5395afc819abSJoe Perches		if ($^V && $^V ge 5.10.0 &&
5396afc819abSJoe Perches		    defined $stat &&
5397afc819abSJoe Perches		    $line =~ /\bsscanf\b/) {
5398afc819abSJoe Perches			my $lc = $stat =~ tr@\n@@;
5399afc819abSJoe Perches			$lc = $lc + $linenr;
5400afc819abSJoe Perches			my $stat_real = raw_line($linenr, 0);
5401afc819abSJoe Perches		        for (my $count = $linenr + 1; $count <= $lc; $count++) {
5402afc819abSJoe Perches				$stat_real = $stat_real . "\n" . raw_line($count, 0);
5403afc819abSJoe Perches			}
5404afc819abSJoe Perches			if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) {
5405afc819abSJoe Perches				my $format = $6;
5406afc819abSJoe Perches				my $count = $format =~ tr@%@%@;
5407afc819abSJoe Perches				if ($count == 1 &&
5408afc819abSJoe Perches				    $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) {
5409afc819abSJoe Perches					WARN("SSCANF_TO_KSTRTO",
5410afc819abSJoe Perches					     "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n");
5411afc819abSJoe Perches				}
5412afc819abSJoe Perches			}
5413afc819abSJoe Perches		}
5414afc819abSJoe Perches
541570dc8a48SJoe Perches# check for new externs in .h files.
541670dc8a48SJoe Perches		if ($realfile =~ /\.h$/ &&
541770dc8a48SJoe Perches		    $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
5418d1d85780SJoe Perches			if (CHK("AVOID_EXTERNS",
541970dc8a48SJoe Perches				"extern prototypes should be avoided in .h files\n" . $herecurr) &&
542070dc8a48SJoe Perches			    $fix) {
5421194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
542270dc8a48SJoe Perches			}
542370dc8a48SJoe Perches		}
542470dc8a48SJoe Perches
5425de7d4f0eSAndy Whitcroft# check for new externs in .c files.
5426171ae1a4SAndy Whitcroft		if ($realfile =~ /\.c$/ && defined $stat &&
5427c45dcabdSAndy Whitcroft		    $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
5428171ae1a4SAndy Whitcroft		{
5429c45dcabdSAndy Whitcroft			my $function_name = $1;
5430c45dcabdSAndy Whitcroft			my $paren_space = $2;
5431171ae1a4SAndy Whitcroft
5432171ae1a4SAndy Whitcroft			my $s = $stat;
5433171ae1a4SAndy Whitcroft			if (defined $cond) {
5434171ae1a4SAndy Whitcroft				substr($s, 0, length($cond), '');
5435171ae1a4SAndy Whitcroft			}
5436c45dcabdSAndy Whitcroft			if ($s =~ /^\s*;/ &&
5437c45dcabdSAndy Whitcroft			    $function_name ne 'uninitialized_var')
5438c45dcabdSAndy Whitcroft			{
5439000d1cc1SJoe Perches				WARN("AVOID_EXTERNS",
5440000d1cc1SJoe Perches				     "externs should be avoided in .c files\n" .  $herecurr);
5441de7d4f0eSAndy Whitcroft			}
5442de7d4f0eSAndy Whitcroft
5443171ae1a4SAndy Whitcroft			if ($paren_space =~ /\n/) {
5444000d1cc1SJoe Perches				WARN("FUNCTION_ARGUMENTS",
5445000d1cc1SJoe Perches				     "arguments for function declarations should follow identifier\n" . $herecurr);
5446171ae1a4SAndy Whitcroft			}
54479c9ba34eSAndy Whitcroft
54489c9ba34eSAndy Whitcroft		} elsif ($realfile =~ /\.c$/ && defined $stat &&
54499c9ba34eSAndy Whitcroft		    $stat =~ /^.\s*extern\s+/)
54509c9ba34eSAndy Whitcroft		{
5451000d1cc1SJoe Perches			WARN("AVOID_EXTERNS",
5452000d1cc1SJoe Perches			     "externs should be avoided in .c files\n" .  $herecurr);
5453171ae1a4SAndy Whitcroft		}
5454171ae1a4SAndy Whitcroft
5455de7d4f0eSAndy Whitcroft# checks for new __setup's
5456de7d4f0eSAndy Whitcroft		if ($rawline =~ /\b__setup\("([^"]*)"/) {
5457de7d4f0eSAndy Whitcroft			my $name = $1;
5458de7d4f0eSAndy Whitcroft
5459de7d4f0eSAndy Whitcroft			if (!grep(/$name/, @setup_docs)) {
5460000d1cc1SJoe Perches				CHK("UNDOCUMENTED_SETUP",
5461000d1cc1SJoe Perches				    "__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
5462de7d4f0eSAndy Whitcroft			}
5463653d4876SAndy Whitcroft		}
54649c0ca6f9SAndy Whitcroft
54659c0ca6f9SAndy Whitcroft# check for pointless casting of kmalloc return
5466caf2a54fSJoe Perches		if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) {
5467000d1cc1SJoe Perches			WARN("UNNECESSARY_CASTS",
5468000d1cc1SJoe Perches			     "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
54699c0ca6f9SAndy Whitcroft		}
547013214adfSAndy Whitcroft
5471a640d25cSJoe Perches# alloc style
5472a640d25cSJoe Perches# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
5473a640d25cSJoe Perches		if ($^V && $^V ge 5.10.0 &&
5474a640d25cSJoe Perches		    $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*([kv][mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
5475a640d25cSJoe Perches			CHK("ALLOC_SIZEOF_STRUCT",
5476a640d25cSJoe Perches			    "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
5477a640d25cSJoe Perches		}
5478a640d25cSJoe Perches
547960a55369SJoe Perches# check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc
548060a55369SJoe Perches		if ($^V && $^V ge 5.10.0 &&
5481e367455aSJoe Perches		    $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*(k[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)\s*,/) {
548260a55369SJoe Perches			my $oldfunc = $3;
548360a55369SJoe Perches			my $a1 = $4;
548460a55369SJoe Perches			my $a2 = $10;
548560a55369SJoe Perches			my $newfunc = "kmalloc_array";
548660a55369SJoe Perches			$newfunc = "kcalloc" if ($oldfunc eq "kzalloc");
548760a55369SJoe Perches			my $r1 = $a1;
548860a55369SJoe Perches			my $r2 = $a2;
548960a55369SJoe Perches			if ($a1 =~ /^sizeof\s*\S/) {
549060a55369SJoe Perches				$r1 = $a2;
549160a55369SJoe Perches				$r2 = $a1;
549260a55369SJoe Perches			}
5493e367455aSJoe Perches			if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ &&
5494e367455aSJoe Perches			    !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) {
5495e367455aSJoe Perches				if (WARN("ALLOC_WITH_MULTIPLY",
5496e367455aSJoe Perches					 "Prefer $newfunc over $oldfunc with multiply\n" . $herecurr) &&
5497e367455aSJoe Perches				    $fix) {
5498194f66fcSJoe 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;
549960a55369SJoe Perches
550060a55369SJoe Perches				}
550160a55369SJoe Perches			}
550260a55369SJoe Perches		}
550360a55369SJoe Perches
5504972fdea2SJoe Perches# check for krealloc arg reuse
5505972fdea2SJoe Perches		if ($^V && $^V ge 5.10.0 &&
5506972fdea2SJoe Perches		    $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*\1\s*,/) {
5507972fdea2SJoe Perches			WARN("KREALLOC_ARG_REUSE",
5508972fdea2SJoe Perches			     "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
5509972fdea2SJoe Perches		}
5510972fdea2SJoe Perches
55115ce59ae0SJoe Perches# check for alloc argument mismatch
55125ce59ae0SJoe Perches		if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
55135ce59ae0SJoe Perches			WARN("ALLOC_ARRAY_ARGS",
55145ce59ae0SJoe Perches			     "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
55155ce59ae0SJoe Perches		}
55165ce59ae0SJoe Perches
5517caf2a54fSJoe Perches# check for multiple semicolons
5518caf2a54fSJoe Perches		if ($line =~ /;\s*;\s*$/) {
5519d5e616fcSJoe Perches			if (WARN("ONE_SEMICOLON",
5520d5e616fcSJoe Perches				 "Statements terminations use 1 semicolon\n" . $herecurr) &&
5521d5e616fcSJoe Perches			    $fix) {
5522194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g;
5523d5e616fcSJoe Perches			}
5524d1e2ad07SJoe Perches		}
5525d1e2ad07SJoe Perches
55260ab90191SJoe Perches# check for #defines like: 1 << <digit> that could be BIT(digit)
55270ab90191SJoe Perches		if ($line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) {
55280ab90191SJoe Perches			my $ull = "";
55290ab90191SJoe Perches			$ull = "_ULL" if (defined($1) && $1 =~ /ll/i);
55300ab90191SJoe Perches			if (CHK("BIT_MACRO",
55310ab90191SJoe Perches				"Prefer using the BIT$ull macro\n" . $herecurr) &&
55320ab90191SJoe Perches			    $fix) {
55330ab90191SJoe Perches				$fixed[$fixlinenr] =~ s/\(?\s*1\s*[ulUL]*\s*<<\s*(\d+|$Ident)\s*\)?/BIT${ull}($1)/;
55340ab90191SJoe Perches			}
55350ab90191SJoe Perches		}
55360ab90191SJoe Perches
5537e81f239bSJoe Perches# check for case / default statements not preceded by break/fallthrough/switch
5538c34c09a8SJoe Perches		if ($line =~ /^.\s*(?:case\s+(?:$Ident|$Constant)\s*|default):/) {
5539c34c09a8SJoe Perches			my $has_break = 0;
5540c34c09a8SJoe Perches			my $has_statement = 0;
5541c34c09a8SJoe Perches			my $count = 0;
5542c34c09a8SJoe Perches			my $prevline = $linenr;
5543e81f239bSJoe Perches			while ($prevline > 1 && ($file || $count < 3) && !$has_break) {
5544c34c09a8SJoe Perches				$prevline--;
5545c34c09a8SJoe Perches				my $rline = $rawlines[$prevline - 1];
5546c34c09a8SJoe Perches				my $fline = $lines[$prevline - 1];
5547c34c09a8SJoe Perches				last if ($fline =~ /^\@\@/);
5548c34c09a8SJoe Perches				next if ($fline =~ /^\-/);
5549c34c09a8SJoe Perches				next if ($fline =~ /^.(?:\s*(?:case\s+(?:$Ident|$Constant)[\s$;]*|default):[\s$;]*)*$/);
5550c34c09a8SJoe Perches				$has_break = 1 if ($rline =~ /fall[\s_-]*(through|thru)/i);
5551c34c09a8SJoe Perches				next if ($fline =~ /^.[\s$;]*$/);
5552c34c09a8SJoe Perches				$has_statement = 1;
5553c34c09a8SJoe Perches				$count++;
5554c34c09a8SJoe Perches				$has_break = 1 if ($fline =~ /\bswitch\b|\b(?:break\s*;[\s$;]*$|return\b|goto\b|continue\b)/);
5555c34c09a8SJoe Perches			}
5556c34c09a8SJoe Perches			if (!$has_break && $has_statement) {
5557c34c09a8SJoe Perches				WARN("MISSING_BREAK",
5558c34c09a8SJoe Perches				     "Possible switch case/default not preceeded by break or fallthrough comment\n" . $herecurr);
5559c34c09a8SJoe Perches			}
5560c34c09a8SJoe Perches		}
5561c34c09a8SJoe Perches
5562d1e2ad07SJoe Perches# check for switch/default statements without a break;
5563d1e2ad07SJoe Perches		if ($^V && $^V ge 5.10.0 &&
5564d1e2ad07SJoe Perches		    defined $stat &&
5565d1e2ad07SJoe Perches		    $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
5566d1e2ad07SJoe Perches			my $ctx = '';
5567d1e2ad07SJoe Perches			my $herectx = $here . "\n";
5568d1e2ad07SJoe Perches			my $cnt = statement_rawlines($stat);
5569d1e2ad07SJoe Perches			for (my $n = 0; $n < $cnt; $n++) {
5570d1e2ad07SJoe Perches				$herectx .= raw_line($linenr, $n) . "\n";
5571d1e2ad07SJoe Perches			}
5572d1e2ad07SJoe Perches			WARN("DEFAULT_NO_BREAK",
5573d1e2ad07SJoe Perches			     "switch default: should use break\n" . $herectx);
5574caf2a54fSJoe Perches		}
5575caf2a54fSJoe Perches
557613214adfSAndy Whitcroft# check for gcc specific __FUNCTION__
5577d5e616fcSJoe Perches		if ($line =~ /\b__FUNCTION__\b/) {
5578d5e616fcSJoe Perches			if (WARN("USE_FUNC",
5579d5e616fcSJoe Perches				 "__func__ should be used instead of gcc specific __FUNCTION__\n"  . $herecurr) &&
5580d5e616fcSJoe Perches			    $fix) {
5581194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g;
5582d5e616fcSJoe Perches			}
558313214adfSAndy Whitcroft		}
5584773647a0SAndy Whitcroft
558562ec818fSJoe Perches# check for uses of __DATE__, __TIME__, __TIMESTAMP__
558662ec818fSJoe Perches		while ($line =~ /\b(__(?:DATE|TIME|TIMESTAMP)__)\b/g) {
558762ec818fSJoe Perches			ERROR("DATE_TIME",
558862ec818fSJoe Perches			      "Use of the '$1' macro makes the build non-deterministic\n" . $herecurr);
558962ec818fSJoe Perches		}
559062ec818fSJoe Perches
55912c92488aSJoe Perches# check for use of yield()
55922c92488aSJoe Perches		if ($line =~ /\byield\s*\(\s*\)/) {
55932c92488aSJoe Perches			WARN("YIELD",
55942c92488aSJoe Perches			     "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n"  . $herecurr);
55952c92488aSJoe Perches		}
55962c92488aSJoe Perches
5597179f8f40SJoe Perches# check for comparisons against true and false
5598179f8f40SJoe Perches		if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
5599179f8f40SJoe Perches			my $lead = $1;
5600179f8f40SJoe Perches			my $arg = $2;
5601179f8f40SJoe Perches			my $test = $3;
5602179f8f40SJoe Perches			my $otype = $4;
5603179f8f40SJoe Perches			my $trail = $5;
5604179f8f40SJoe Perches			my $op = "!";
5605179f8f40SJoe Perches
5606179f8f40SJoe Perches			($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
5607179f8f40SJoe Perches
5608179f8f40SJoe Perches			my $type = lc($otype);
5609179f8f40SJoe Perches			if ($type =~ /^(?:true|false)$/) {
5610179f8f40SJoe Perches				if (("$test" eq "==" && "$type" eq "true") ||
5611179f8f40SJoe Perches				    ("$test" eq "!=" && "$type" eq "false")) {
5612179f8f40SJoe Perches					$op = "";
5613179f8f40SJoe Perches				}
5614179f8f40SJoe Perches
5615179f8f40SJoe Perches				CHK("BOOL_COMPARISON",
5616179f8f40SJoe Perches				    "Using comparison to $otype is error prone\n" . $herecurr);
5617179f8f40SJoe Perches
5618179f8f40SJoe Perches## maybe suggesting a correct construct would better
5619179f8f40SJoe Perches##				    "Using comparison to $otype is error prone.  Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
5620179f8f40SJoe Perches
5621179f8f40SJoe Perches			}
5622179f8f40SJoe Perches		}
5623179f8f40SJoe Perches
56244882720bSThomas Gleixner# check for semaphores initialized locked
56254882720bSThomas Gleixner		if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
5626000d1cc1SJoe Perches			WARN("CONSIDER_COMPLETION",
5627000d1cc1SJoe Perches			     "consider using a completion\n" . $herecurr);
5628773647a0SAndy Whitcroft		}
56296712d858SJoe Perches
563067d0a075SJoe Perches# recommend kstrto* over simple_strto* and strict_strto*
563167d0a075SJoe Perches		if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
5632000d1cc1SJoe Perches			WARN("CONSIDER_KSTRTO",
563367d0a075SJoe Perches			     "$1 is obsolete, use k$3 instead\n" . $herecurr);
5634773647a0SAndy Whitcroft		}
56356712d858SJoe Perches
5636ae3ccc46SFabian Frederick# check for __initcall(), use device_initcall() explicitly or more appropriate function please
5637f3db6639SMichael Ellerman		if ($line =~ /^.\s*__initcall\s*\(/) {
5638000d1cc1SJoe Perches			WARN("USE_DEVICE_INITCALL",
5639ae3ccc46SFabian Frederick			     "please use device_initcall() or more appropriate function instead of __initcall() (see include/linux/init.h)\n" . $herecurr);
5640f3db6639SMichael Ellerman		}
56416712d858SJoe Perches
56420f3c5aabSJoe Perches# check for various structs that are normally const (ops, kgdb, device_tree)
56430f3c5aabSJoe Perches		my $const_structs = qr{
56440f3c5aabSJoe Perches				acpi_dock_ops|
564579404849SEmese Revfy				address_space_operations|
564679404849SEmese Revfy				backlight_ops|
564779404849SEmese Revfy				block_device_operations|
564879404849SEmese Revfy				dentry_operations|
564979404849SEmese Revfy				dev_pm_ops|
565079404849SEmese Revfy				dma_map_ops|
565179404849SEmese Revfy				extent_io_ops|
565279404849SEmese Revfy				file_lock_operations|
565379404849SEmese Revfy				file_operations|
565479404849SEmese Revfy				hv_ops|
565579404849SEmese Revfy				ide_dma_ops|
565679404849SEmese Revfy				intel_dvo_dev_ops|
565779404849SEmese Revfy				item_operations|
565879404849SEmese Revfy				iwl_ops|
565979404849SEmese Revfy				kgdb_arch|
566079404849SEmese Revfy				kgdb_io|
566179404849SEmese Revfy				kset_uevent_ops|
566279404849SEmese Revfy				lock_manager_operations|
566379404849SEmese Revfy				microcode_ops|
566479404849SEmese Revfy				mtrr_ops|
566579404849SEmese Revfy				neigh_ops|
566679404849SEmese Revfy				nlmsvc_binding|
56670f3c5aabSJoe Perches				of_device_id|
566879404849SEmese Revfy				pci_raw_ops|
566979404849SEmese Revfy				pipe_buf_operations|
567079404849SEmese Revfy				platform_hibernation_ops|
567179404849SEmese Revfy				platform_suspend_ops|
567279404849SEmese Revfy				proto_ops|
567379404849SEmese Revfy				rpc_pipe_ops|
567479404849SEmese Revfy				seq_operations|
567579404849SEmese Revfy				snd_ac97_build_ops|
567679404849SEmese Revfy				soc_pcmcia_socket_ops|
567779404849SEmese Revfy				stacktrace_ops|
567879404849SEmese Revfy				sysfs_ops|
567979404849SEmese Revfy				tty_operations|
56806d07d01bSJoe Perches				uart_ops|
568179404849SEmese Revfy				usb_mon_operations|
568279404849SEmese Revfy				wd_ops}x;
56836903ffb2SAndy Whitcroft		if ($line !~ /\bconst\b/ &&
56840f3c5aabSJoe Perches		    $line =~ /\bstruct\s+($const_structs)\b/) {
5685000d1cc1SJoe Perches			WARN("CONST_STRUCT",
5686000d1cc1SJoe Perches			     "struct $1 should normally be const\n" .
56876903ffb2SAndy Whitcroft				$herecurr);
56882b6db5cbSAndy Whitcroft		}
5689773647a0SAndy Whitcroft
5690773647a0SAndy Whitcroft# use of NR_CPUS is usually wrong
5691773647a0SAndy Whitcroft# ignore definitions of NR_CPUS and usage to define arrays as likely right
5692773647a0SAndy Whitcroft		if ($line =~ /\bNR_CPUS\b/ &&
5693c45dcabdSAndy Whitcroft		    $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
5694c45dcabdSAndy Whitcroft		    $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
5695171ae1a4SAndy Whitcroft		    $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
5696171ae1a4SAndy Whitcroft		    $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
5697171ae1a4SAndy Whitcroft		    $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
5698773647a0SAndy Whitcroft		{
5699000d1cc1SJoe Perches			WARN("NR_CPUS",
5700000d1cc1SJoe Perches			     "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
5701773647a0SAndy Whitcroft		}
57029c9ba34eSAndy Whitcroft
570352ea8506SJoe Perches# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
570452ea8506SJoe Perches		if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
570552ea8506SJoe Perches			ERROR("DEFINE_ARCH_HAS",
570652ea8506SJoe Perches			      "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
570752ea8506SJoe Perches		}
570852ea8506SJoe Perches
5709acd9362cSJoe Perches# likely/unlikely comparisons similar to "(likely(foo) > 0)"
5710acd9362cSJoe Perches		if ($^V && $^V ge 5.10.0 &&
5711acd9362cSJoe Perches		    $line =~ /\b((?:un)?likely)\s*\(\s*$FuncArg\s*\)\s*$Compare/) {
5712acd9362cSJoe Perches			WARN("LIKELY_MISUSE",
5713acd9362cSJoe Perches			     "Using $1 should generally have parentheses around the comparison\n" . $herecurr);
5714acd9362cSJoe Perches		}
5715acd9362cSJoe Perches
5716691d77b6SAndy Whitcroft# whine mightly about in_atomic
5717691d77b6SAndy Whitcroft		if ($line =~ /\bin_atomic\s*\(/) {
5718691d77b6SAndy Whitcroft			if ($realfile =~ m@^drivers/@) {
5719000d1cc1SJoe Perches				ERROR("IN_ATOMIC",
5720000d1cc1SJoe Perches				      "do not use in_atomic in drivers\n" . $herecurr);
5721f4a87736SAndy Whitcroft			} elsif ($realfile !~ m@^kernel/@) {
5722000d1cc1SJoe Perches				WARN("IN_ATOMIC",
5723000d1cc1SJoe Perches				     "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
5724691d77b6SAndy Whitcroft			}
5725691d77b6SAndy Whitcroft		}
57261704f47bSPeter Zijlstra
57271704f47bSPeter Zijlstra# check for lockdep_set_novalidate_class
57281704f47bSPeter Zijlstra		if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
57291704f47bSPeter Zijlstra		    $line =~ /__lockdep_no_validate__\s*\)/ ) {
57301704f47bSPeter Zijlstra			if ($realfile !~ m@^kernel/lockdep@ &&
57311704f47bSPeter Zijlstra			    $realfile !~ m@^include/linux/lockdep@ &&
57321704f47bSPeter Zijlstra			    $realfile !~ m@^drivers/base/core@) {
5733000d1cc1SJoe Perches				ERROR("LOCKDEP",
5734000d1cc1SJoe Perches				      "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
57351704f47bSPeter Zijlstra			}
57361704f47bSPeter Zijlstra		}
573788f8831cSDave Jones
5738b392c64fSJoe Perches		if ($line =~ /debugfs_create_\w+.*\b$mode_perms_world_writable\b/ ||
5739b392c64fSJoe Perches		    $line =~ /DEVICE_ATTR.*\b$mode_perms_world_writable\b/) {
5740000d1cc1SJoe Perches			WARN("EXPORTED_WORLD_WRITABLE",
5741000d1cc1SJoe Perches			     "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
574288f8831cSDave Jones		}
57432435880fSJoe Perches
5744515a235eSJoe Perches# Mode permission misuses where it seems decimal should be octal
5745515a235eSJoe Perches# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop
5746515a235eSJoe Perches		if ($^V && $^V ge 5.10.0 &&
5747515a235eSJoe Perches		    $line =~ /$mode_perms_search/) {
57482435880fSJoe Perches			foreach my $entry (@mode_permission_funcs) {
57492435880fSJoe Perches				my $func = $entry->[0];
57502435880fSJoe Perches				my $arg_pos = $entry->[1];
57512435880fSJoe Perches
57522435880fSJoe Perches				my $skip_args = "";
57532435880fSJoe Perches				if ($arg_pos > 1) {
57542435880fSJoe Perches					$arg_pos--;
57552435880fSJoe Perches					$skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}";
57562435880fSJoe Perches				}
57572435880fSJoe Perches				my $test = "\\b$func\\s*\\(${skip_args}([\\d]+)\\s*[,\\)]";
5758515a235eSJoe Perches				if ($line =~ /$test/) {
57592435880fSJoe Perches					my $val = $1;
57602435880fSJoe Perches					$val = $6 if ($skip_args ne "");
57612435880fSJoe Perches
57621727cc70SJoe Perches					if ($val !~ /^0$/ &&
57631727cc70SJoe Perches					    (($val =~ /^$Int$/ && $val !~ /^$Octal$/) ||
57641727cc70SJoe Perches					     length($val) ne 4)) {
57652435880fSJoe Perches						ERROR("NON_OCTAL_PERMISSIONS",
57661727cc70SJoe Perches						      "Use 4 digit octal (0777) not decimal permissions\n" . $herecurr);
5767c0a5c898SJoe Perches					} elsif ($val =~ /^$Octal$/ && (oct($val) & 02)) {
5768c0a5c898SJoe Perches						ERROR("EXPORTED_WORLD_WRITABLE",
5769c0a5c898SJoe Perches						      "Exporting writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
57702435880fSJoe Perches					}
57712435880fSJoe Perches				}
57722435880fSJoe Perches			}
577313214adfSAndy Whitcroft		}
57745a6d20ceSBjorn Andersson
57755a6d20ceSBjorn Andersson# validate content of MODULE_LICENSE against list from include/linux/module.h
57765a6d20ceSBjorn Andersson		if ($line =~ /\bMODULE_LICENSE\s*\(\s*($String)\s*\)/) {
57775a6d20ceSBjorn Andersson			my $extracted_string = get_quoted_string($line, $rawline);
57785a6d20ceSBjorn Andersson			my $valid_licenses = qr{
57795a6d20ceSBjorn Andersson						GPL|
57805a6d20ceSBjorn Andersson						GPL\ v2|
57815a6d20ceSBjorn Andersson						GPL\ and\ additional\ rights|
57825a6d20ceSBjorn Andersson						Dual\ BSD/GPL|
57835a6d20ceSBjorn Andersson						Dual\ MIT/GPL|
57845a6d20ceSBjorn Andersson						Dual\ MPL/GPL|
57855a6d20ceSBjorn Andersson						Proprietary
57865a6d20ceSBjorn Andersson					}x;
57875a6d20ceSBjorn Andersson			if ($extracted_string !~ /^"(?:$valid_licenses)"$/x) {
57885a6d20ceSBjorn Andersson				WARN("MODULE_LICENSE",
57895a6d20ceSBjorn Andersson				     "unknown module license " . $extracted_string . "\n" . $herecurr);
57905a6d20ceSBjorn Andersson			}
57915a6d20ceSBjorn Andersson		}
5792515a235eSJoe Perches	}
579313214adfSAndy Whitcroft
579413214adfSAndy Whitcroft	# If we have no input at all, then there is nothing to report on
579513214adfSAndy Whitcroft	# so just keep quiet.
579613214adfSAndy Whitcroft	if ($#rawlines == -1) {
579713214adfSAndy Whitcroft		exit(0);
57980a920b5bSAndy Whitcroft	}
57990a920b5bSAndy Whitcroft
58008905a67cSAndy Whitcroft	# In mailback mode only produce a report in the negative, for
58018905a67cSAndy Whitcroft	# things that appear to be patches.
58028905a67cSAndy Whitcroft	if ($mailback && ($clean == 1 || !$is_patch)) {
58038905a67cSAndy Whitcroft		exit(0);
58048905a67cSAndy Whitcroft	}
58058905a67cSAndy Whitcroft
58068905a67cSAndy Whitcroft	# This is not a patch, and we are are in 'no-patch' mode so
58078905a67cSAndy Whitcroft	# just keep quiet.
58088905a67cSAndy Whitcroft	if (!$chk_patch && !$is_patch) {
58098905a67cSAndy Whitcroft		exit(0);
58108905a67cSAndy Whitcroft	}
58118905a67cSAndy Whitcroft
581206330fc4SJoe Perches	if (!$is_patch && $file !~ /cover-letter\.patch$/) {
5813000d1cc1SJoe Perches		ERROR("NOT_UNIFIED_DIFF",
5814000d1cc1SJoe Perches		      "Does not appear to be a unified-diff format patch\n");
58150a920b5bSAndy Whitcroft	}
581634d8815fSJoe Perches	if ($is_patch && $filename ne '-' && $chk_signoff && $signoff == 0) {
5817000d1cc1SJoe Perches		ERROR("MISSING_SIGN_OFF",
5818000d1cc1SJoe Perches		      "Missing Signed-off-by: line(s)\n");
58190a920b5bSAndy Whitcroft	}
58200a920b5bSAndy Whitcroft
5821f0a594c1SAndy Whitcroft	print report_dump();
582213214adfSAndy Whitcroft	if ($summary && !($clean == 1 && $quiet == 1)) {
582313214adfSAndy Whitcroft		print "$filename " if ($summary_file);
58246c72ffaaSAndy Whitcroft		print "total: $cnt_error errors, $cnt_warn warnings, " .
58256c72ffaaSAndy Whitcroft			(($check)? "$cnt_chk checks, " : "") .
58266c72ffaaSAndy Whitcroft			"$cnt_lines lines checked\n";
58276c72ffaaSAndy Whitcroft	}
58288905a67cSAndy Whitcroft
5829d2c0a235SAndy Whitcroft	if ($quiet == 0) {
5830d2c0a235SAndy Whitcroft		# If there were whitespace errors which cleanpatch can fix
5831d2c0a235SAndy Whitcroft		# then suggest that.
5832d2c0a235SAndy Whitcroft		if ($rpt_cleaners) {
5833b0781216SMike Frysinger			$rpt_cleaners = 0;
5834d8469f16SJoe Perches			print << "EOM"
5835d8469f16SJoe Perches
5836d8469f16SJoe PerchesNOTE: Whitespace errors detected.
5837d8469f16SJoe Perches      You may wish to use scripts/cleanpatch or scripts/cleanfile
5838d8469f16SJoe PerchesEOM
5839d2c0a235SAndy Whitcroft		}
5840d2c0a235SAndy Whitcroft	}
5841d2c0a235SAndy Whitcroft
5842d752fcc8SJoe Perches	if ($clean == 0 && $fix &&
5843d752fcc8SJoe Perches	    ("@rawlines" ne "@fixed" ||
5844d752fcc8SJoe Perches	     $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) {
58459624b8d6SJoe Perches		my $newfile = $filename;
58469624b8d6SJoe Perches		$newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace);
58473705ce5bSJoe Perches		my $linecount = 0;
58483705ce5bSJoe Perches		my $f;
58493705ce5bSJoe Perches
5850d752fcc8SJoe Perches		@fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted);
5851d752fcc8SJoe Perches
58523705ce5bSJoe Perches		open($f, '>', $newfile)
58533705ce5bSJoe Perches		    or die "$P: Can't open $newfile for write\n";
58543705ce5bSJoe Perches		foreach my $fixed_line (@fixed) {
58553705ce5bSJoe Perches			$linecount++;
58563705ce5bSJoe Perches			if ($file) {
58573705ce5bSJoe Perches				if ($linecount > 3) {
58583705ce5bSJoe Perches					$fixed_line =~ s/^\+//;
58593705ce5bSJoe Perches					print $f $fixed_line . "\n";
58603705ce5bSJoe Perches				}
58613705ce5bSJoe Perches			} else {
58623705ce5bSJoe Perches				print $f $fixed_line . "\n";
58633705ce5bSJoe Perches			}
58643705ce5bSJoe Perches		}
58653705ce5bSJoe Perches		close($f);
58663705ce5bSJoe Perches
58673705ce5bSJoe Perches		if (!$quiet) {
58683705ce5bSJoe Perches			print << "EOM";
5869d8469f16SJoe Perches
58703705ce5bSJoe PerchesWrote EXPERIMENTAL --fix correction(s) to '$newfile'
58713705ce5bSJoe Perches
58723705ce5bSJoe PerchesDo _NOT_ trust the results written to this file.
58733705ce5bSJoe PerchesDo _NOT_ submit these changes without inspecting them for correctness.
58743705ce5bSJoe Perches
58753705ce5bSJoe PerchesThis EXPERIMENTAL file is simply a convenience to help rewrite patches.
58763705ce5bSJoe PerchesNo warranties, expressed or implied...
58773705ce5bSJoe PerchesEOM
58783705ce5bSJoe Perches		}
58793705ce5bSJoe Perches	}
58803705ce5bSJoe Perches
5881d8469f16SJoe Perches	if ($quiet == 0) {
5882d8469f16SJoe Perches		print "\n";
5883d8469f16SJoe Perches		if ($clean == 1) {
5884d8469f16SJoe Perches			print "$vname has no obvious style problems and is ready for submission.\n";
5885d8469f16SJoe Perches		} else {
5886d8469f16SJoe Perches			print "$vname has style problems, please review.\n";
58870a920b5bSAndy Whitcroft		}
58880a920b5bSAndy Whitcroft	}
58890a920b5bSAndy Whitcroft	return $clean;
58900a920b5bSAndy Whitcroft}
5891