xref: /linux-6.15/scripts/checkpatch.pl (revision 481aea5c)
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|
272ad315455SBoqun Feng			__rcu|
273ad315455SBoqun Feng			__private
2746c72ffaaSAndy Whitcroft		}x;
275e970b884SJoe Perchesour $InitAttributePrefix = qr{__(?:mem|cpu|dev|net_|)};
276e970b884SJoe Perchesour $InitAttributeData = qr{$InitAttributePrefix(?:initdata\b)};
277e970b884SJoe Perchesour $InitAttributeConst = qr{$InitAttributePrefix(?:initconst\b)};
278e970b884SJoe Perchesour $InitAttributeInit = qr{$InitAttributePrefix(?:init\b)};
279e970b884SJoe Perchesour $InitAttribute = qr{$InitAttributeData|$InitAttributeConst|$InitAttributeInit};
2808716de38SJoe Perches
28152131292SWolfram Sang# Notes to $Attribute:
28252131292SWolfram Sang# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
2836c72ffaaSAndy Whitcroftour $Attribute	= qr{
2846c72ffaaSAndy Whitcroft			const|
28503f1df7dSJoe Perches			__percpu|
28603f1df7dSJoe Perches			__nocast|
28703f1df7dSJoe Perches			__safe|
28803f1df7dSJoe Perches			__bitwise__|
28903f1df7dSJoe Perches			__packed__|
29003f1df7dSJoe Perches			__packed2__|
29103f1df7dSJoe Perches			__naked|
29203f1df7dSJoe Perches			__maybe_unused|
29303f1df7dSJoe Perches			__always_unused|
29403f1df7dSJoe Perches			__noreturn|
29503f1df7dSJoe Perches			__used|
29603f1df7dSJoe Perches			__cold|
297e23ef1f3SJoe Perches			__pure|
29803f1df7dSJoe Perches			__noclone|
29903f1df7dSJoe Perches			__deprecated|
3006c72ffaaSAndy Whitcroft			__read_mostly|
3016c72ffaaSAndy Whitcroft			__kprobes|
3028716de38SJoe Perches			$InitAttribute|
30324e1d81aSAndy Whitcroft			____cacheline_aligned|
30424e1d81aSAndy Whitcroft			____cacheline_aligned_in_smp|
3055fe3af11SAndy Whitcroft			____cacheline_internodealigned_in_smp|
3065fe3af11SAndy Whitcroft			__weak
3076c72ffaaSAndy Whitcroft		  }x;
308c45dcabdSAndy Whitcroftour $Modifier;
30991cb5195SJoe Perchesour $Inline	= qr{inline|__always_inline|noinline|__inline|__inline__};
3106c72ffaaSAndy Whitcroftour $Member	= qr{->$Ident|\.$Ident|\[[^]]*\]};
3116c72ffaaSAndy Whitcroftour $Lval	= qr{$Ident(?:$Member)*};
3126c72ffaaSAndy Whitcroft
31395e2c602SJoe Perchesour $Int_type	= qr{(?i)llu|ull|ll|lu|ul|l|u};
31495e2c602SJoe Perchesour $Binary	= qr{(?i)0b[01]+$Int_type?};
31595e2c602SJoe Perchesour $Hex	= qr{(?i)0x[0-9a-f]+$Int_type?};
31695e2c602SJoe Perchesour $Int	= qr{[0-9]+$Int_type?};
3172435880fSJoe Perchesour $Octal	= qr{0[0-7]+$Int_type?};
318c0a5c898SJoe Perchesour $String	= qr{"[X\t]*"};
319326b1ffcSJoe Perchesour $Float_hex	= qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?};
320326b1ffcSJoe Perchesour $Float_dec	= qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?};
321326b1ffcSJoe Perchesour $Float_int	= qr{(?i)[0-9]+e-?[0-9]+[fl]?};
32274349bccSJoe Perchesour $Float	= qr{$Float_hex|$Float_dec|$Float_int};
3232435880fSJoe Perchesour $Constant	= qr{$Float|$Binary|$Octal|$Hex|$Int};
324326b1ffcSJoe Perchesour $Assignment	= qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=};
325447432f3SJoe Perchesour $Compare    = qr{<=|>=|==|!=|<|(?<!-)>};
32623f780c9SJoe Perchesour $Arithmetic = qr{\+|-|\*|\/|%};
3276c72ffaaSAndy Whitcroftour $Operators	= qr{
3286c72ffaaSAndy Whitcroft			<=|>=|==|!=|
3296c72ffaaSAndy Whitcroft			=>|->|<<|>>|<|>|!|~|
33023f780c9SJoe Perches			&&|\|\||,|\^|\+\+|--|&|\||$Arithmetic
3316c72ffaaSAndy Whitcroft		  }x;
3326c72ffaaSAndy Whitcroft
33391cb5195SJoe Perchesour $c90_Keywords = qr{do|for|while|if|else|return|goto|continue|switch|default|case|break}x;
33491cb5195SJoe Perches
335ab7e23f3SJoe Perchesour $BasicType;
3368905a67cSAndy Whitcroftour $NonptrType;
3371813087dSJoe Perchesour $NonptrTypeMisordered;
3388716de38SJoe Perchesour $NonptrTypeWithAttr;
3398905a67cSAndy Whitcroftour $Type;
3401813087dSJoe Perchesour $TypeMisordered;
3418905a67cSAndy Whitcroftour $Declare;
3421813087dSJoe Perchesour $DeclareMisordered;
3438905a67cSAndy Whitcroft
34415662b3eSJoe Perchesour $NON_ASCII_UTF8	= qr{
34515662b3eSJoe Perches	[\xC2-\xDF][\x80-\xBF]               # non-overlong 2-byte
346171ae1a4SAndy Whitcroft	|  \xE0[\xA0-\xBF][\x80-\xBF]        # excluding overlongs
347171ae1a4SAndy Whitcroft	| [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}  # straight 3-byte
348171ae1a4SAndy Whitcroft	|  \xED[\x80-\x9F][\x80-\xBF]        # excluding surrogates
349171ae1a4SAndy Whitcroft	|  \xF0[\x90-\xBF][\x80-\xBF]{2}     # planes 1-3
350171ae1a4SAndy Whitcroft	| [\xF1-\xF3][\x80-\xBF]{3}          # planes 4-15
351171ae1a4SAndy Whitcroft	|  \xF4[\x80-\x8F][\x80-\xBF]{2}     # plane 16
352171ae1a4SAndy Whitcroft}x;
353171ae1a4SAndy Whitcroft
35415662b3eSJoe Perchesour $UTF8	= qr{
35515662b3eSJoe Perches	[\x09\x0A\x0D\x20-\x7E]              # ASCII
35615662b3eSJoe Perches	| $NON_ASCII_UTF8
35715662b3eSJoe Perches}x;
35815662b3eSJoe Perches
359e6176fa4SJoe Perchesour $typeC99Typedefs = qr{(?:__)?(?:[us]_?)?int_?(?:8|16|32|64)_t};
360021158b4SJoe Perchesour $typeOtherOSTypedefs = qr{(?x:
361021158b4SJoe Perches	u_(?:char|short|int|long) |          # bsd
362021158b4SJoe Perches	u(?:nchar|short|int|long)            # sysv
363021158b4SJoe Perches)};
364e6176fa4SJoe Perchesour $typeKernelTypedefs = qr{(?x:
365fb9e9096SAndy Whitcroft	(?:__)?(?:u|s|be|le)(?:8|16|32|64)|
3668ed22cadSAndy Whitcroft	atomic_t
3678ed22cadSAndy Whitcroft)};
368e6176fa4SJoe Perchesour $typeTypedefs = qr{(?x:
369e6176fa4SJoe Perches	$typeC99Typedefs\b|
370e6176fa4SJoe Perches	$typeOtherOSTypedefs\b|
371e6176fa4SJoe Perches	$typeKernelTypedefs\b
372e6176fa4SJoe Perches)};
3738ed22cadSAndy Whitcroft
3746d32f7a3SJoe Perchesour $zero_initializer = qr{(?:(?:0[xX])?0+$Int_type?|NULL|false)\b};
3756d32f7a3SJoe Perches
376691e669bSJoe Perchesour $logFunctions = qr{(?x:
3776e60c02eSJoe Perches	printk(?:_ratelimited|_once|)|
3787d0b6594SJacob Keller	(?:[a-z0-9]+_){1,2}(?:printk|emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)(?:_ratelimited|_once|)|
3796e60c02eSJoe Perches	WARN(?:_RATELIMIT|_ONCE|)|
380b0531722SJoe Perches	panic|
38106668727SJoe Perches	MODULE_[A-Z_]+|
38206668727SJoe Perches	seq_vprintf|seq_printf|seq_puts
383691e669bSJoe Perches)};
384691e669bSJoe Perches
38520112475SJoe Perchesour $signature_tags = qr{(?xi:
38620112475SJoe Perches	Signed-off-by:|
38720112475SJoe Perches	Acked-by:|
38820112475SJoe Perches	Tested-by:|
38920112475SJoe Perches	Reviewed-by:|
39020112475SJoe Perches	Reported-by:|
3918543ae12SMugunthan V N	Suggested-by:|
39220112475SJoe Perches	To:|
39320112475SJoe Perches	Cc:
39420112475SJoe Perches)};
39520112475SJoe Perches
3961813087dSJoe Perchesour @typeListMisordered = (
3971813087dSJoe Perches	qr{char\s+(?:un)?signed},
3981813087dSJoe Perches	qr{int\s+(?:(?:un)?signed\s+)?short\s},
3991813087dSJoe Perches	qr{int\s+short(?:\s+(?:un)?signed)},
4001813087dSJoe Perches	qr{short\s+int(?:\s+(?:un)?signed)},
4011813087dSJoe Perches	qr{(?:un)?signed\s+int\s+short},
4021813087dSJoe Perches	qr{short\s+(?:un)?signed},
4031813087dSJoe Perches	qr{long\s+int\s+(?:un)?signed},
4041813087dSJoe Perches	qr{int\s+long\s+(?:un)?signed},
4051813087dSJoe Perches	qr{long\s+(?:un)?signed\s+int},
4061813087dSJoe Perches	qr{int\s+(?:un)?signed\s+long},
4071813087dSJoe Perches	qr{int\s+(?:un)?signed},
4081813087dSJoe Perches	qr{int\s+long\s+long\s+(?:un)?signed},
4091813087dSJoe Perches	qr{long\s+long\s+int\s+(?:un)?signed},
4101813087dSJoe Perches	qr{long\s+long\s+(?:un)?signed\s+int},
4111813087dSJoe Perches	qr{long\s+long\s+(?:un)?signed},
4121813087dSJoe Perches	qr{long\s+(?:un)?signed},
4131813087dSJoe Perches);
4141813087dSJoe Perches
4158905a67cSAndy Whitcroftour @typeList = (
4168905a67cSAndy Whitcroft	qr{void},
4170c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?char},
4180c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?short\s+int},
4190c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?short},
4200c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?int},
4210c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?long\s+int},
4220c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?long\s+long\s+int},
4230c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?long\s+long},
4240c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?long},
4250c773d9dSJoe Perches	qr{(?:un)?signed},
4268905a67cSAndy Whitcroft	qr{float},
4278905a67cSAndy Whitcroft	qr{double},
4288905a67cSAndy Whitcroft	qr{bool},
4298905a67cSAndy Whitcroft	qr{struct\s+$Ident},
4308905a67cSAndy Whitcroft	qr{union\s+$Ident},
4318905a67cSAndy Whitcroft	qr{enum\s+$Ident},
4328905a67cSAndy Whitcroft	qr{${Ident}_t},
4338905a67cSAndy Whitcroft	qr{${Ident}_handler},
4348905a67cSAndy Whitcroft	qr{${Ident}_handler_fn},
4351813087dSJoe Perches	@typeListMisordered,
4368905a67cSAndy Whitcroft);
437938224b5SJoe Perches
438938224b5SJoe Perchesour $C90_int_types = qr{(?x:
439938224b5SJoe Perches	long\s+long\s+int\s+(?:un)?signed|
440938224b5SJoe Perches	long\s+long\s+(?:un)?signed\s+int|
441938224b5SJoe Perches	long\s+long\s+(?:un)?signed|
442938224b5SJoe Perches	(?:(?:un)?signed\s+)?long\s+long\s+int|
443938224b5SJoe Perches	(?:(?:un)?signed\s+)?long\s+long|
444938224b5SJoe Perches	int\s+long\s+long\s+(?:un)?signed|
445938224b5SJoe Perches	int\s+(?:(?:un)?signed\s+)?long\s+long|
446938224b5SJoe Perches
447938224b5SJoe Perches	long\s+int\s+(?:un)?signed|
448938224b5SJoe Perches	long\s+(?:un)?signed\s+int|
449938224b5SJoe Perches	long\s+(?:un)?signed|
450938224b5SJoe Perches	(?:(?:un)?signed\s+)?long\s+int|
451938224b5SJoe Perches	(?:(?:un)?signed\s+)?long|
452938224b5SJoe Perches	int\s+long\s+(?:un)?signed|
453938224b5SJoe Perches	int\s+(?:(?:un)?signed\s+)?long|
454938224b5SJoe Perches
455938224b5SJoe Perches	int\s+(?:un)?signed|
456938224b5SJoe Perches	(?:(?:un)?signed\s+)?int
457938224b5SJoe Perches)};
458938224b5SJoe Perches
459485ff23eSAlex Dowadour @typeListFile = ();
4608716de38SJoe Perchesour @typeListWithAttr = (
4618716de38SJoe Perches	@typeList,
4628716de38SJoe Perches	qr{struct\s+$InitAttribute\s+$Ident},
4638716de38SJoe Perches	qr{union\s+$InitAttribute\s+$Ident},
4648716de38SJoe Perches);
4658716de38SJoe Perches
466c45dcabdSAndy Whitcroftour @modifierList = (
467c45dcabdSAndy Whitcroft	qr{fastcall},
468c45dcabdSAndy Whitcroft);
469485ff23eSAlex Dowadour @modifierListFile = ();
4708905a67cSAndy Whitcroft
4712435880fSJoe Perchesour @mode_permission_funcs = (
4722435880fSJoe Perches	["module_param", 3],
4732435880fSJoe Perches	["module_param_(?:array|named|string)", 4],
4742435880fSJoe Perches	["module_param_array_named", 5],
4752435880fSJoe Perches	["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2],
4762435880fSJoe Perches	["proc_create(?:_data|)", 2],
4772435880fSJoe Perches	["(?:CLASS|DEVICE|SENSOR)_ATTR", 2],
4782435880fSJoe Perches);
4792435880fSJoe Perches
480515a235eSJoe Perches#Create a search pattern for all these functions to speed up a loop below
481515a235eSJoe Perchesour $mode_perms_search = "";
482515a235eSJoe Perchesforeach my $entry (@mode_permission_funcs) {
483515a235eSJoe Perches	$mode_perms_search .= '|' if ($mode_perms_search ne "");
484515a235eSJoe Perches	$mode_perms_search .= $entry->[0];
485515a235eSJoe Perches}
486515a235eSJoe Perches
487b392c64fSJoe Perchesour $mode_perms_world_writable = qr{
488b392c64fSJoe Perches	S_IWUGO		|
489b392c64fSJoe Perches	S_IWOTH		|
490b392c64fSJoe Perches	S_IRWXUGO	|
491b392c64fSJoe Perches	S_IALLUGO	|
492b392c64fSJoe Perches	0[0-7][0-7][2367]
493b392c64fSJoe Perches}x;
494b392c64fSJoe Perches
4957840a94cSWolfram Sangour $allowed_asm_includes = qr{(?x:
4967840a94cSWolfram Sang	irq|
497cdcee686SSergey Ryazanov	memory|
498cdcee686SSergey Ryazanov	time|
499cdcee686SSergey Ryazanov	reboot
5007840a94cSWolfram Sang)};
5017840a94cSWolfram Sang# memory.h: ARM has a custom one
5027840a94cSWolfram Sang
50366b47b4aSKees Cook# Load common spelling mistakes and build regular expression list.
50466b47b4aSKees Cookmy $misspellings;
50566b47b4aSKees Cookmy %spelling_fix;
50636061e38SJoe Perches
50736061e38SJoe Perchesif (open(my $spelling, '<', $spelling_file)) {
50866b47b4aSKees Cook	while (<$spelling>) {
50966b47b4aSKees Cook		my $line = $_;
51066b47b4aSKees Cook
51166b47b4aSKees Cook		$line =~ s/\s*\n?$//g;
51266b47b4aSKees Cook		$line =~ s/^\s*//g;
51366b47b4aSKees Cook
51466b47b4aSKees Cook		next if ($line =~ m/^\s*#/);
51566b47b4aSKees Cook		next if ($line =~ m/^\s*$/);
51666b47b4aSKees Cook
51766b47b4aSKees Cook		my ($suspect, $fix) = split(/\|\|/, $line);
51866b47b4aSKees Cook
51966b47b4aSKees Cook		$spelling_fix{$suspect} = $fix;
52066b47b4aSKees Cook	}
52166b47b4aSKees Cook	close($spelling);
52236061e38SJoe Perches} else {
52336061e38SJoe Perches	warn "No typos will be found - file '$spelling_file': $!\n";
52436061e38SJoe Perches}
52566b47b4aSKees Cook
526ebfd7d62SJoe Perchesif ($codespell) {
527ebfd7d62SJoe Perches	if (open(my $spelling, '<', $codespellfile)) {
528ebfd7d62SJoe Perches		while (<$spelling>) {
529ebfd7d62SJoe Perches			my $line = $_;
530ebfd7d62SJoe Perches
531ebfd7d62SJoe Perches			$line =~ s/\s*\n?$//g;
532ebfd7d62SJoe Perches			$line =~ s/^\s*//g;
533ebfd7d62SJoe Perches
534ebfd7d62SJoe Perches			next if ($line =~ m/^\s*#/);
535ebfd7d62SJoe Perches			next if ($line =~ m/^\s*$/);
536ebfd7d62SJoe Perches			next if ($line =~ m/, disabled/i);
537ebfd7d62SJoe Perches
538ebfd7d62SJoe Perches			$line =~ s/,.*$//;
539ebfd7d62SJoe Perches
540ebfd7d62SJoe Perches			my ($suspect, $fix) = split(/->/, $line);
541ebfd7d62SJoe Perches
542ebfd7d62SJoe Perches			$spelling_fix{$suspect} = $fix;
543ebfd7d62SJoe Perches		}
544ebfd7d62SJoe Perches		close($spelling);
545ebfd7d62SJoe Perches	} else {
546ebfd7d62SJoe Perches		warn "No codespell typos will be found - file '$codespellfile': $!\n";
547ebfd7d62SJoe Perches	}
548ebfd7d62SJoe Perches}
549ebfd7d62SJoe Perches
550ebfd7d62SJoe Perches$misspellings = join("|", sort keys %spelling_fix) if keys %spelling_fix;
551ebfd7d62SJoe Perches
5528905a67cSAndy Whitcroftsub build_types {
553485ff23eSAlex Dowad	my $mods = "(?x:  \n" . join("|\n  ", (@modifierList, @modifierListFile)) . "\n)";
554485ff23eSAlex Dowad	my $all = "(?x:  \n" . join("|\n  ", (@typeList, @typeListFile)) . "\n)";
5551813087dSJoe Perches	my $Misordered = "(?x:  \n" . join("|\n  ", @typeListMisordered) . "\n)";
5568716de38SJoe Perches	my $allWithAttr = "(?x:  \n" . join("|\n  ", @typeListWithAttr) . "\n)";
557c8cb2ca3SAndy Whitcroft	$Modifier	= qr{(?:$Attribute|$Sparse|$mods)};
558ab7e23f3SJoe Perches	$BasicType	= qr{
559ab7e23f3SJoe Perches				(?:$typeTypedefs\b)|
560ab7e23f3SJoe Perches				(?:${all}\b)
561ab7e23f3SJoe Perches		}x;
5628905a67cSAndy Whitcroft	$NonptrType	= qr{
563d2172eb5SAndy Whitcroft			(?:$Modifier\s+|const\s+)*
564cf655043SAndy Whitcroft			(?:
5656b48db24SAndy Whitcroft				(?:typeof|__typeof__)\s*\([^\)]*\)|
5668ed22cadSAndy Whitcroft				(?:$typeTypedefs\b)|
567c45dcabdSAndy Whitcroft				(?:${all}\b)
568cf655043SAndy Whitcroft			)
569c8cb2ca3SAndy Whitcroft			(?:\s+$Modifier|\s+const)*
5708905a67cSAndy Whitcroft		  }x;
5711813087dSJoe Perches	$NonptrTypeMisordered	= qr{
5721813087dSJoe Perches			(?:$Modifier\s+|const\s+)*
5731813087dSJoe Perches			(?:
5741813087dSJoe Perches				(?:${Misordered}\b)
5751813087dSJoe Perches			)
5761813087dSJoe Perches			(?:\s+$Modifier|\s+const)*
5771813087dSJoe Perches		  }x;
5788716de38SJoe Perches	$NonptrTypeWithAttr	= qr{
5798716de38SJoe Perches			(?:$Modifier\s+|const\s+)*
5808716de38SJoe Perches			(?:
5818716de38SJoe Perches				(?:typeof|__typeof__)\s*\([^\)]*\)|
5828716de38SJoe Perches				(?:$typeTypedefs\b)|
5838716de38SJoe Perches				(?:${allWithAttr}\b)
5848716de38SJoe Perches			)
5858716de38SJoe Perches			(?:\s+$Modifier|\s+const)*
5868716de38SJoe Perches		  }x;
5878905a67cSAndy Whitcroft	$Type	= qr{
588c45dcabdSAndy Whitcroft			$NonptrType
5891574a29fSJoe Perches			(?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
590c8cb2ca3SAndy Whitcroft			(?:\s+$Inline|\s+$Modifier)*
5918905a67cSAndy Whitcroft		  }x;
5921813087dSJoe Perches	$TypeMisordered	= qr{
5931813087dSJoe Perches			$NonptrTypeMisordered
5941813087dSJoe Perches			(?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
5951813087dSJoe Perches			(?:\s+$Inline|\s+$Modifier)*
5961813087dSJoe Perches		  }x;
59791cb5195SJoe Perches	$Declare	= qr{(?:$Storage\s+(?:$Inline\s+)?)?$Type};
5981813087dSJoe Perches	$DeclareMisordered	= qr{(?:$Storage\s+(?:$Inline\s+)?)?$TypeMisordered};
5998905a67cSAndy Whitcroft}
6008905a67cSAndy Whitcroftbuild_types();
6016c72ffaaSAndy Whitcroft
6027d2367afSJoe Perchesour $Typecast	= qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
603d1fe9c09SJoe Perches
604d1fe9c09SJoe Perches# Using $balanced_parens, $LvalOrFunc, or $FuncArg
605d1fe9c09SJoe Perches# requires at least perl version v5.10.0
606d1fe9c09SJoe Perches# Any use must be runtime checked with $^V
607d1fe9c09SJoe Perches
608d1fe9c09SJoe Perchesour $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
6092435880fSJoe Perchesour $LvalOrFunc	= qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*};
610c0a5c898SJoe Perchesour $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant|$String)};
6117d2367afSJoe Perches
612f8422308SJoe Perchesour $declaration_macros = qr{(?x:
6133e838b6cSJoe Perches	(?:$Storage\s+)?(?:[A-Z_][A-Z0-9]*_){0,2}(?:DEFINE|DECLARE)(?:_[A-Z0-9]+){1,6}\s*\(|
614f8422308SJoe Perches	(?:$Storage\s+)?LIST_HEAD\s*\(|
615f8422308SJoe Perches	(?:$Storage\s+)?${Type}\s+uninitialized_var\s*\(
616f8422308SJoe Perches)};
617f8422308SJoe Perches
6187d2367afSJoe Perchessub deparenthesize {
6197d2367afSJoe Perches	my ($string) = @_;
6207d2367afSJoe Perches	return "" if (!defined($string));
6215b9553abSJoe Perches
6225b9553abSJoe Perches	while ($string =~ /^\s*\(.*\)\s*$/) {
6235b9553abSJoe Perches		$string =~ s@^\s*\(\s*@@;
6245b9553abSJoe Perches		$string =~ s@\s*\)\s*$@@;
6255b9553abSJoe Perches	}
6265b9553abSJoe Perches
6277d2367afSJoe Perches	$string =~ s@\s+@ @g;
6285b9553abSJoe Perches
6297d2367afSJoe Perches	return $string;
6307d2367afSJoe Perches}
6317d2367afSJoe Perches
6323445686aSJoe Perchessub seed_camelcase_file {
6333445686aSJoe Perches	my ($file) = @_;
6343445686aSJoe Perches
6353445686aSJoe Perches	return if (!(-f $file));
6363445686aSJoe Perches
6373445686aSJoe Perches	local $/;
6383445686aSJoe Perches
6393445686aSJoe Perches	open(my $include_file, '<', "$file")
6403445686aSJoe Perches	    or warn "$P: Can't read '$file' $!\n";
6413445686aSJoe Perches	my $text = <$include_file>;
6423445686aSJoe Perches	close($include_file);
6433445686aSJoe Perches
6443445686aSJoe Perches	my @lines = split('\n', $text);
6453445686aSJoe Perches
6463445686aSJoe Perches	foreach my $line (@lines) {
6473445686aSJoe Perches		next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/);
6483445686aSJoe Perches		if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) {
6493445686aSJoe Perches			$camelcase{$1} = 1;
65011ea516aSJoe Perches		} elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) {
65111ea516aSJoe Perches			$camelcase{$1} = 1;
65211ea516aSJoe Perches		} elsif ($line =~ /^\s*(?:union|struct|enum)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[;\{]/) {
6533445686aSJoe Perches			$camelcase{$1} = 1;
6543445686aSJoe Perches		}
6553445686aSJoe Perches	}
6563445686aSJoe Perches}
6573445686aSJoe Perches
6583445686aSJoe Perchesmy $camelcase_seeded = 0;
6593445686aSJoe Perchessub seed_camelcase_includes {
6603445686aSJoe Perches	return if ($camelcase_seeded);
6613445686aSJoe Perches
6623445686aSJoe Perches	my $files;
663c707a81dSJoe Perches	my $camelcase_cache = "";
664c707a81dSJoe Perches	my @include_files = ();
665c707a81dSJoe Perches
666c707a81dSJoe Perches	$camelcase_seeded = 1;
667351b2a1fSJoe Perches
6683645e328SRichard Genoud	if (-e ".git") {
669351b2a1fSJoe Perches		my $git_last_include_commit = `git log --no-merges --pretty=format:"%h%n" -1 -- include`;
670351b2a1fSJoe Perches		chomp $git_last_include_commit;
671c707a81dSJoe Perches		$camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit";
672c707a81dSJoe Perches	} else {
673c707a81dSJoe Perches		my $last_mod_date = 0;
674c707a81dSJoe Perches		$files = `find $root/include -name "*.h"`;
675c707a81dSJoe Perches		@include_files = split('\n', $files);
676c707a81dSJoe Perches		foreach my $file (@include_files) {
677c707a81dSJoe Perches			my $date = POSIX::strftime("%Y%m%d%H%M",
678c707a81dSJoe Perches						   localtime((stat $file)[9]));
679c707a81dSJoe Perches			$last_mod_date = $date if ($last_mod_date < $date);
680c707a81dSJoe Perches		}
681c707a81dSJoe Perches		$camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date";
682c707a81dSJoe Perches	}
683c707a81dSJoe Perches
684c707a81dSJoe Perches	if ($camelcase_cache ne "" && -f $camelcase_cache) {
685c707a81dSJoe Perches		open(my $camelcase_file, '<', "$camelcase_cache")
686c707a81dSJoe Perches		    or warn "$P: Can't read '$camelcase_cache' $!\n";
687351b2a1fSJoe Perches		while (<$camelcase_file>) {
688351b2a1fSJoe Perches			chomp;
689351b2a1fSJoe Perches			$camelcase{$_} = 1;
690351b2a1fSJoe Perches		}
691351b2a1fSJoe Perches		close($camelcase_file);
692351b2a1fSJoe Perches
693351b2a1fSJoe Perches		return;
694351b2a1fSJoe Perches	}
695c707a81dSJoe Perches
6963645e328SRichard Genoud	if (-e ".git") {
697c707a81dSJoe Perches		$files = `git ls-files "include/*.h"`;
698c707a81dSJoe Perches		@include_files = split('\n', $files);
6993445686aSJoe Perches	}
700c707a81dSJoe Perches
7013445686aSJoe Perches	foreach my $file (@include_files) {
7023445686aSJoe Perches		seed_camelcase_file($file);
7033445686aSJoe Perches	}
704351b2a1fSJoe Perches
705c707a81dSJoe Perches	if ($camelcase_cache ne "") {
706351b2a1fSJoe Perches		unlink glob ".checkpatch-camelcase.*";
707c707a81dSJoe Perches		open(my $camelcase_file, '>', "$camelcase_cache")
708c707a81dSJoe Perches		    or warn "$P: Can't write '$camelcase_cache' $!\n";
709351b2a1fSJoe Perches		foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) {
710351b2a1fSJoe Perches			print $camelcase_file ("$_\n");
711351b2a1fSJoe Perches		}
712351b2a1fSJoe Perches		close($camelcase_file);
713351b2a1fSJoe Perches	}
7143445686aSJoe Perches}
7153445686aSJoe Perches
716d311cd44SJoe Perchessub git_commit_info {
717d311cd44SJoe Perches	my ($commit, $id, $desc) = @_;
718d311cd44SJoe Perches
719d311cd44SJoe Perches	return ($id, $desc) if ((which("git") eq "") || !(-e ".git"));
720d311cd44SJoe Perches
721d311cd44SJoe Perches	my $output = `git log --no-color --format='%H %s' -1 $commit 2>&1`;
722d311cd44SJoe Perches	$output =~ s/^\s*//gm;
723d311cd44SJoe Perches	my @lines = split("\n", $output);
724d311cd44SJoe Perches
7250d7835fcSJoe Perches	return ($id, $desc) if ($#lines < 0);
7260d7835fcSJoe Perches
727d311cd44SJoe Perches	if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous\./) {
728d311cd44SJoe Perches# Maybe one day convert this block of bash into something that returns
729d311cd44SJoe Perches# all matching commit ids, but it's very slow...
730d311cd44SJoe Perches#
731d311cd44SJoe Perches#		echo "checking commits $1..."
732d311cd44SJoe Perches#		git rev-list --remotes | grep -i "^$1" |
733d311cd44SJoe Perches#		while read line ; do
734d311cd44SJoe Perches#		    git log --format='%H %s' -1 $line |
735d311cd44SJoe Perches#		    echo "commit $(cut -c 1-12,41-)"
736d311cd44SJoe Perches#		done
737d311cd44SJoe Perches	} elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./) {
738d311cd44SJoe Perches	} else {
739d311cd44SJoe Perches		$id = substr($lines[0], 0, 12);
740d311cd44SJoe Perches		$desc = substr($lines[0], 41);
741d311cd44SJoe Perches	}
742d311cd44SJoe Perches
743d311cd44SJoe Perches	return ($id, $desc);
744d311cd44SJoe Perches}
745d311cd44SJoe Perches
7466c72ffaaSAndy Whitcroft$chk_signoff = 0 if ($file);
7470a920b5bSAndy Whitcroft
74800df344fSAndy Whitcroftmy @rawlines = ();
749c2fdda0dSAndy Whitcroftmy @lines = ();
7503705ce5bSJoe Perchesmy @fixed = ();
751d752fcc8SJoe Perchesmy @fixed_inserted = ();
752d752fcc8SJoe Perchesmy @fixed_deleted = ();
753194f66fcSJoe Perchesmy $fixlinenr = -1;
754194f66fcSJoe Perches
755c2fdda0dSAndy Whitcroftmy $vname;
7566c72ffaaSAndy Whitcroftfor my $filename (@ARGV) {
75721caa13cSAndy Whitcroft	my $FILE;
7586c72ffaaSAndy Whitcroft	if ($file) {
75921caa13cSAndy Whitcroft		open($FILE, '-|', "diff -u /dev/null $filename") ||
7606c72ffaaSAndy Whitcroft			die "$P: $filename: diff failed - $!\n";
76121caa13cSAndy Whitcroft	} elsif ($filename eq '-') {
76221caa13cSAndy Whitcroft		open($FILE, '<&STDIN');
7636c72ffaaSAndy Whitcroft	} else {
76421caa13cSAndy Whitcroft		open($FILE, '<', "$filename") ||
7656c72ffaaSAndy Whitcroft			die "$P: $filename: open failed - $!\n";
7666c72ffaaSAndy Whitcroft	}
767c2fdda0dSAndy Whitcroft	if ($filename eq '-') {
768c2fdda0dSAndy Whitcroft		$vname = 'Your patch';
769c2fdda0dSAndy Whitcroft	} else {
770c2fdda0dSAndy Whitcroft		$vname = $filename;
771c2fdda0dSAndy Whitcroft	}
77221caa13cSAndy Whitcroft	while (<$FILE>) {
7730a920b5bSAndy Whitcroft		chomp;
77400df344fSAndy Whitcroft		push(@rawlines, $_);
7756c72ffaaSAndy Whitcroft	}
77621caa13cSAndy Whitcroft	close($FILE);
777d8469f16SJoe Perches
778d8469f16SJoe Perches	if ($#ARGV > 0 && $quiet == 0) {
779d8469f16SJoe Perches		print '-' x length($vname) . "\n";
780d8469f16SJoe Perches		print "$vname\n";
781d8469f16SJoe Perches		print '-' x length($vname) . "\n";
782d8469f16SJoe Perches	}
783d8469f16SJoe Perches
784c2fdda0dSAndy Whitcroft	if (!process($filename)) {
7850a920b5bSAndy Whitcroft		$exit = 1;
7860a920b5bSAndy Whitcroft	}
78700df344fSAndy Whitcroft	@rawlines = ();
78813214adfSAndy Whitcroft	@lines = ();
7893705ce5bSJoe Perches	@fixed = ();
790d752fcc8SJoe Perches	@fixed_inserted = ();
791d752fcc8SJoe Perches	@fixed_deleted = ();
792194f66fcSJoe Perches	$fixlinenr = -1;
793485ff23eSAlex Dowad	@modifierListFile = ();
794485ff23eSAlex Dowad	@typeListFile = ();
795485ff23eSAlex Dowad	build_types();
7960a920b5bSAndy Whitcroft}
7970a920b5bSAndy Whitcroft
798d8469f16SJoe Perchesif (!$quiet) {
7993c816e49SJoe Perches	hash_show_words(\%use_type, "Used");
8003c816e49SJoe Perches	hash_show_words(\%ignore_type, "Ignored");
8013c816e49SJoe Perches
802d8469f16SJoe Perches	if ($^V lt 5.10.0) {
803d8469f16SJoe Perches		print << "EOM"
804d8469f16SJoe Perches
805d8469f16SJoe PerchesNOTE: perl $^V is not modern enough to detect all possible issues.
806d8469f16SJoe Perches      An upgrade to at least perl v5.10.0 is suggested.
807d8469f16SJoe PerchesEOM
808d8469f16SJoe Perches	}
809d8469f16SJoe Perches	if ($exit) {
810d8469f16SJoe Perches		print << "EOM"
811d8469f16SJoe Perches
812d8469f16SJoe PerchesNOTE: If any of the errors are false positives, please report
813d8469f16SJoe Perches      them to the maintainer, see CHECKPATCH in MAINTAINERS.
814d8469f16SJoe PerchesEOM
815d8469f16SJoe Perches	}
816d8469f16SJoe Perches}
817d8469f16SJoe Perches
8180a920b5bSAndy Whitcroftexit($exit);
8190a920b5bSAndy Whitcroft
8200a920b5bSAndy Whitcroftsub top_of_kernel_tree {
8216c72ffaaSAndy Whitcroft	my ($root) = @_;
8226c72ffaaSAndy Whitcroft
8236c72ffaaSAndy Whitcroft	my @tree_check = (
8246c72ffaaSAndy Whitcroft		"COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
8256c72ffaaSAndy Whitcroft		"README", "Documentation", "arch", "include", "drivers",
8266c72ffaaSAndy Whitcroft		"fs", "init", "ipc", "kernel", "lib", "scripts",
8276c72ffaaSAndy Whitcroft	);
8286c72ffaaSAndy Whitcroft
8296c72ffaaSAndy Whitcroft	foreach my $check (@tree_check) {
8306c72ffaaSAndy Whitcroft		if (! -e $root . '/' . $check) {
8310a920b5bSAndy Whitcroft			return 0;
8320a920b5bSAndy Whitcroft		}
8336c72ffaaSAndy Whitcroft	}
8346c72ffaaSAndy Whitcroft	return 1;
8356c72ffaaSAndy Whitcroft}
8360a920b5bSAndy Whitcroft
83720112475SJoe Perchessub parse_email {
83820112475SJoe Perches	my ($formatted_email) = @_;
83920112475SJoe Perches
84020112475SJoe Perches	my $name = "";
84120112475SJoe Perches	my $address = "";
84220112475SJoe Perches	my $comment = "";
84320112475SJoe Perches
84420112475SJoe Perches	if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
84520112475SJoe Perches		$name = $1;
84620112475SJoe Perches		$address = $2;
84720112475SJoe Perches		$comment = $3 if defined $3;
84820112475SJoe Perches	} elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
84920112475SJoe Perches		$address = $1;
85020112475SJoe Perches		$comment = $2 if defined $2;
85120112475SJoe Perches	} elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
85220112475SJoe Perches		$address = $1;
85320112475SJoe Perches		$comment = $2 if defined $2;
85420112475SJoe Perches		$formatted_email =~ s/$address.*$//;
85520112475SJoe Perches		$name = $formatted_email;
8563705ce5bSJoe Perches		$name = trim($name);
85720112475SJoe Perches		$name =~ s/^\"|\"$//g;
85820112475SJoe Perches		# If there's a name left after stripping spaces and
85920112475SJoe Perches		# leading quotes, and the address doesn't have both
86020112475SJoe Perches		# leading and trailing angle brackets, the address
86120112475SJoe Perches		# is invalid. ie:
86220112475SJoe Perches		#   "joe smith [email protected]" bad
86320112475SJoe Perches		#   "joe smith <[email protected]" bad
86420112475SJoe Perches		if ($name ne "" && $address !~ /^<[^>]+>$/) {
86520112475SJoe Perches			$name = "";
86620112475SJoe Perches			$address = "";
86720112475SJoe Perches			$comment = "";
86820112475SJoe Perches		}
86920112475SJoe Perches	}
87020112475SJoe Perches
8713705ce5bSJoe Perches	$name = trim($name);
87220112475SJoe Perches	$name =~ s/^\"|\"$//g;
8733705ce5bSJoe Perches	$address = trim($address);
87420112475SJoe Perches	$address =~ s/^\<|\>$//g;
87520112475SJoe Perches
87620112475SJoe Perches	if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
87720112475SJoe Perches		$name =~ s/(?<!\\)"/\\"/g; ##escape quotes
87820112475SJoe Perches		$name = "\"$name\"";
87920112475SJoe Perches	}
88020112475SJoe Perches
88120112475SJoe Perches	return ($name, $address, $comment);
88220112475SJoe Perches}
88320112475SJoe Perches
88420112475SJoe Perchessub format_email {
88520112475SJoe Perches	my ($name, $address) = @_;
88620112475SJoe Perches
88720112475SJoe Perches	my $formatted_email;
88820112475SJoe Perches
8893705ce5bSJoe Perches	$name = trim($name);
89020112475SJoe Perches	$name =~ s/^\"|\"$//g;
8913705ce5bSJoe Perches	$address = trim($address);
89220112475SJoe Perches
89320112475SJoe Perches	if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
89420112475SJoe Perches		$name =~ s/(?<!\\)"/\\"/g; ##escape quotes
89520112475SJoe Perches		$name = "\"$name\"";
89620112475SJoe Perches	}
89720112475SJoe Perches
89820112475SJoe Perches	if ("$name" eq "") {
89920112475SJoe Perches		$formatted_email = "$address";
90020112475SJoe Perches	} else {
90120112475SJoe Perches		$formatted_email = "$name <$address>";
90220112475SJoe Perches	}
90320112475SJoe Perches
90420112475SJoe Perches	return $formatted_email;
90520112475SJoe Perches}
90620112475SJoe Perches
907d311cd44SJoe Perchessub which {
908d311cd44SJoe Perches	my ($bin) = @_;
909d311cd44SJoe Perches
910d311cd44SJoe Perches	foreach my $path (split(/:/, $ENV{PATH})) {
911d311cd44SJoe Perches		if (-e "$path/$bin") {
912d311cd44SJoe Perches			return "$path/$bin";
913d311cd44SJoe Perches		}
914d311cd44SJoe Perches	}
915d311cd44SJoe Perches
916d311cd44SJoe Perches	return "";
917d311cd44SJoe Perches}
918d311cd44SJoe Perches
919000d1cc1SJoe Perchessub which_conf {
920000d1cc1SJoe Perches	my ($conf) = @_;
921000d1cc1SJoe Perches
922000d1cc1SJoe Perches	foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
923000d1cc1SJoe Perches		if (-e "$path/$conf") {
924000d1cc1SJoe Perches			return "$path/$conf";
925000d1cc1SJoe Perches		}
926000d1cc1SJoe Perches	}
927000d1cc1SJoe Perches
928000d1cc1SJoe Perches	return "";
929000d1cc1SJoe Perches}
930000d1cc1SJoe Perches
9310a920b5bSAndy Whitcroftsub expand_tabs {
9320a920b5bSAndy Whitcroft	my ($str) = @_;
9330a920b5bSAndy Whitcroft
9340a920b5bSAndy Whitcroft	my $res = '';
9350a920b5bSAndy Whitcroft	my $n = 0;
9360a920b5bSAndy Whitcroft	for my $c (split(//, $str)) {
9370a920b5bSAndy Whitcroft		if ($c eq "\t") {
9380a920b5bSAndy Whitcroft			$res .= ' ';
9390a920b5bSAndy Whitcroft			$n++;
9400a920b5bSAndy Whitcroft			for (; ($n % 8) != 0; $n++) {
9410a920b5bSAndy Whitcroft				$res .= ' ';
9420a920b5bSAndy Whitcroft			}
9430a920b5bSAndy Whitcroft			next;
9440a920b5bSAndy Whitcroft		}
9450a920b5bSAndy Whitcroft		$res .= $c;
9460a920b5bSAndy Whitcroft		$n++;
9470a920b5bSAndy Whitcroft	}
9480a920b5bSAndy Whitcroft
9490a920b5bSAndy Whitcroft	return $res;
9500a920b5bSAndy Whitcroft}
9516c72ffaaSAndy Whitcroftsub copy_spacing {
952773647a0SAndy Whitcroft	(my $res = shift) =~ tr/\t/ /c;
9536c72ffaaSAndy Whitcroft	return $res;
9546c72ffaaSAndy Whitcroft}
9550a920b5bSAndy Whitcroft
9564a0df2efSAndy Whitcroftsub line_stats {
9574a0df2efSAndy Whitcroft	my ($line) = @_;
9584a0df2efSAndy Whitcroft
9594a0df2efSAndy Whitcroft	# Drop the diff line leader and expand tabs
9604a0df2efSAndy Whitcroft	$line =~ s/^.//;
9614a0df2efSAndy Whitcroft	$line = expand_tabs($line);
9624a0df2efSAndy Whitcroft
9634a0df2efSAndy Whitcroft	# Pick the indent from the front of the line.
9644a0df2efSAndy Whitcroft	my ($white) = ($line =~ /^(\s*)/);
9654a0df2efSAndy Whitcroft
9664a0df2efSAndy Whitcroft	return (length($line), length($white));
9674a0df2efSAndy Whitcroft}
9684a0df2efSAndy Whitcroft
969773647a0SAndy Whitcroftmy $sanitise_quote = '';
970773647a0SAndy Whitcroft
971773647a0SAndy Whitcroftsub sanitise_line_reset {
972773647a0SAndy Whitcroft	my ($in_comment) = @_;
973773647a0SAndy Whitcroft
974773647a0SAndy Whitcroft	if ($in_comment) {
975773647a0SAndy Whitcroft		$sanitise_quote = '*/';
976773647a0SAndy Whitcroft	} else {
977773647a0SAndy Whitcroft		$sanitise_quote = '';
978773647a0SAndy Whitcroft	}
979773647a0SAndy Whitcroft}
98000df344fSAndy Whitcroftsub sanitise_line {
98100df344fSAndy Whitcroft	my ($line) = @_;
98200df344fSAndy Whitcroft
98300df344fSAndy Whitcroft	my $res = '';
98400df344fSAndy Whitcroft	my $l = '';
98500df344fSAndy Whitcroft
986c2fdda0dSAndy Whitcroft	my $qlen = 0;
987773647a0SAndy Whitcroft	my $off = 0;
988773647a0SAndy Whitcroft	my $c;
98900df344fSAndy Whitcroft
990773647a0SAndy Whitcroft	# Always copy over the diff marker.
991773647a0SAndy Whitcroft	$res = substr($line, 0, 1);
992773647a0SAndy Whitcroft
993773647a0SAndy Whitcroft	for ($off = 1; $off < length($line); $off++) {
994773647a0SAndy Whitcroft		$c = substr($line, $off, 1);
995773647a0SAndy Whitcroft
996773647a0SAndy Whitcroft		# Comments we are wacking completly including the begin
997773647a0SAndy Whitcroft		# and end, all to $;.
998773647a0SAndy Whitcroft		if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
999773647a0SAndy Whitcroft			$sanitise_quote = '*/';
1000773647a0SAndy Whitcroft
1001773647a0SAndy Whitcroft			substr($res, $off, 2, "$;$;");
1002773647a0SAndy Whitcroft			$off++;
100300df344fSAndy Whitcroft			next;
1004773647a0SAndy Whitcroft		}
100581bc0e02SAndy Whitcroft		if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
1006773647a0SAndy Whitcroft			$sanitise_quote = '';
1007773647a0SAndy Whitcroft			substr($res, $off, 2, "$;$;");
1008773647a0SAndy Whitcroft			$off++;
1009773647a0SAndy Whitcroft			next;
1010773647a0SAndy Whitcroft		}
1011113f04a8SDaniel Walker		if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
1012113f04a8SDaniel Walker			$sanitise_quote = '//';
1013113f04a8SDaniel Walker
1014113f04a8SDaniel Walker			substr($res, $off, 2, $sanitise_quote);
1015113f04a8SDaniel Walker			$off++;
1016113f04a8SDaniel Walker			next;
1017113f04a8SDaniel Walker		}
1018773647a0SAndy Whitcroft
1019773647a0SAndy Whitcroft		# A \ in a string means ignore the next character.
1020773647a0SAndy Whitcroft		if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
1021773647a0SAndy Whitcroft		    $c eq "\\") {
1022773647a0SAndy Whitcroft			substr($res, $off, 2, 'XX');
1023773647a0SAndy Whitcroft			$off++;
1024773647a0SAndy Whitcroft			next;
1025773647a0SAndy Whitcroft		}
1026773647a0SAndy Whitcroft		# Regular quotes.
1027773647a0SAndy Whitcroft		if ($c eq "'" || $c eq '"') {
1028773647a0SAndy Whitcroft			if ($sanitise_quote eq '') {
1029773647a0SAndy Whitcroft				$sanitise_quote = $c;
1030773647a0SAndy Whitcroft
1031773647a0SAndy Whitcroft				substr($res, $off, 1, $c);
1032773647a0SAndy Whitcroft				next;
1033773647a0SAndy Whitcroft			} elsif ($sanitise_quote eq $c) {
1034773647a0SAndy Whitcroft				$sanitise_quote = '';
103500df344fSAndy Whitcroft			}
103600df344fSAndy Whitcroft		}
1037773647a0SAndy Whitcroft
1038fae17daeSAndy Whitcroft		#print "c<$c> SQ<$sanitise_quote>\n";
1039773647a0SAndy Whitcroft		if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
1040773647a0SAndy Whitcroft			substr($res, $off, 1, $;);
1041113f04a8SDaniel Walker		} elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
1042113f04a8SDaniel Walker			substr($res, $off, 1, $;);
1043773647a0SAndy Whitcroft		} elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
1044773647a0SAndy Whitcroft			substr($res, $off, 1, 'X');
104500df344fSAndy Whitcroft		} else {
1046773647a0SAndy Whitcroft			substr($res, $off, 1, $c);
104700df344fSAndy Whitcroft		}
1048c2fdda0dSAndy Whitcroft	}
1049c2fdda0dSAndy Whitcroft
1050113f04a8SDaniel Walker	if ($sanitise_quote eq '//') {
1051113f04a8SDaniel Walker		$sanitise_quote = '';
1052113f04a8SDaniel Walker	}
1053113f04a8SDaniel Walker
1054c2fdda0dSAndy Whitcroft	# The pathname on a #include may be surrounded by '<' and '>'.
1055c45dcabdSAndy Whitcroft	if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
1056c2fdda0dSAndy Whitcroft		my $clean = 'X' x length($1);
1057c2fdda0dSAndy Whitcroft		$res =~ s@\<.*\>@<$clean>@;
1058c2fdda0dSAndy Whitcroft
1059c2fdda0dSAndy Whitcroft	# The whole of a #error is a string.
1060c45dcabdSAndy Whitcroft	} elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
1061c2fdda0dSAndy Whitcroft		my $clean = 'X' x length($1);
1062c45dcabdSAndy Whitcroft		$res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
1063c2fdda0dSAndy Whitcroft	}
1064c2fdda0dSAndy Whitcroft
106500df344fSAndy Whitcroft	return $res;
106600df344fSAndy Whitcroft}
106700df344fSAndy Whitcroft
1068a6962d72SJoe Perchessub get_quoted_string {
1069a6962d72SJoe Perches	my ($line, $rawline) = @_;
1070a6962d72SJoe Perches
107133acb54aSJoe Perches	return "" if ($line !~ m/($String)/g);
1072a6962d72SJoe Perches	return substr($rawline, $-[0], $+[0] - $-[0]);
1073a6962d72SJoe Perches}
1074a6962d72SJoe Perches
10758905a67cSAndy Whitcroftsub ctx_statement_block {
10768905a67cSAndy Whitcroft	my ($linenr, $remain, $off) = @_;
10778905a67cSAndy Whitcroft	my $line = $linenr - 1;
10788905a67cSAndy Whitcroft	my $blk = '';
10798905a67cSAndy Whitcroft	my $soff = $off;
10808905a67cSAndy Whitcroft	my $coff = $off - 1;
1081773647a0SAndy Whitcroft	my $coff_set = 0;
10828905a67cSAndy Whitcroft
108313214adfSAndy Whitcroft	my $loff = 0;
108413214adfSAndy Whitcroft
10858905a67cSAndy Whitcroft	my $type = '';
10868905a67cSAndy Whitcroft	my $level = 0;
1087a2750645SAndy Whitcroft	my @stack = ();
1088cf655043SAndy Whitcroft	my $p;
10898905a67cSAndy Whitcroft	my $c;
10908905a67cSAndy Whitcroft	my $len = 0;
109113214adfSAndy Whitcroft
109213214adfSAndy Whitcroft	my $remainder;
10938905a67cSAndy Whitcroft	while (1) {
1094a2750645SAndy Whitcroft		@stack = (['', 0]) if ($#stack == -1);
1095a2750645SAndy Whitcroft
1096773647a0SAndy Whitcroft		#warn "CSB: blk<$blk> remain<$remain>\n";
10978905a67cSAndy Whitcroft		# If we are about to drop off the end, pull in more
10988905a67cSAndy Whitcroft		# context.
10998905a67cSAndy Whitcroft		if ($off >= $len) {
11008905a67cSAndy Whitcroft			for (; $remain > 0; $line++) {
1101dea33496SAndy Whitcroft				last if (!defined $lines[$line]);
1102c2fdda0dSAndy Whitcroft				next if ($lines[$line] =~ /^-/);
11038905a67cSAndy Whitcroft				$remain--;
110413214adfSAndy Whitcroft				$loff = $len;
1105c2fdda0dSAndy Whitcroft				$blk .= $lines[$line] . "\n";
11068905a67cSAndy Whitcroft				$len = length($blk);
11078905a67cSAndy Whitcroft				$line++;
11088905a67cSAndy Whitcroft				last;
11098905a67cSAndy Whitcroft			}
11108905a67cSAndy Whitcroft			# Bail if there is no further context.
11118905a67cSAndy Whitcroft			#warn "CSB: blk<$blk> off<$off> len<$len>\n";
111213214adfSAndy Whitcroft			if ($off >= $len) {
11138905a67cSAndy Whitcroft				last;
11148905a67cSAndy Whitcroft			}
1115f74bd194SAndy Whitcroft			if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
1116f74bd194SAndy Whitcroft				$level++;
1117f74bd194SAndy Whitcroft				$type = '#';
1118f74bd194SAndy Whitcroft			}
11198905a67cSAndy Whitcroft		}
1120cf655043SAndy Whitcroft		$p = $c;
11218905a67cSAndy Whitcroft		$c = substr($blk, $off, 1);
112213214adfSAndy Whitcroft		$remainder = substr($blk, $off);
11238905a67cSAndy Whitcroft
1124773647a0SAndy Whitcroft		#warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
11254635f4fbSAndy Whitcroft
11264635f4fbSAndy Whitcroft		# Handle nested #if/#else.
11274635f4fbSAndy Whitcroft		if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
11284635f4fbSAndy Whitcroft			push(@stack, [ $type, $level ]);
11294635f4fbSAndy Whitcroft		} elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
11304635f4fbSAndy Whitcroft			($type, $level) = @{$stack[$#stack - 1]};
11314635f4fbSAndy Whitcroft		} elsif ($remainder =~ /^#\s*endif\b/) {
11324635f4fbSAndy Whitcroft			($type, $level) = @{pop(@stack)};
11334635f4fbSAndy Whitcroft		}
11344635f4fbSAndy Whitcroft
11358905a67cSAndy Whitcroft		# Statement ends at the ';' or a close '}' at the
11368905a67cSAndy Whitcroft		# outermost level.
11378905a67cSAndy Whitcroft		if ($level == 0 && $c eq ';') {
11388905a67cSAndy Whitcroft			last;
11398905a67cSAndy Whitcroft		}
11408905a67cSAndy Whitcroft
114113214adfSAndy Whitcroft		# An else is really a conditional as long as its not else if
1142773647a0SAndy Whitcroft		if ($level == 0 && $coff_set == 0 &&
1143773647a0SAndy Whitcroft				(!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
1144773647a0SAndy Whitcroft				$remainder =~ /^(else)(?:\s|{)/ &&
1145773647a0SAndy Whitcroft				$remainder !~ /^else\s+if\b/) {
1146773647a0SAndy Whitcroft			$coff = $off + length($1) - 1;
1147773647a0SAndy Whitcroft			$coff_set = 1;
1148773647a0SAndy Whitcroft			#warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
1149773647a0SAndy Whitcroft			#warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
115013214adfSAndy Whitcroft		}
115113214adfSAndy Whitcroft
11528905a67cSAndy Whitcroft		if (($type eq '' || $type eq '(') && $c eq '(') {
11538905a67cSAndy Whitcroft			$level++;
11548905a67cSAndy Whitcroft			$type = '(';
11558905a67cSAndy Whitcroft		}
11568905a67cSAndy Whitcroft		if ($type eq '(' && $c eq ')') {
11578905a67cSAndy Whitcroft			$level--;
11588905a67cSAndy Whitcroft			$type = ($level != 0)? '(' : '';
11598905a67cSAndy Whitcroft
11608905a67cSAndy Whitcroft			if ($level == 0 && $coff < $soff) {
11618905a67cSAndy Whitcroft				$coff = $off;
1162773647a0SAndy Whitcroft				$coff_set = 1;
1163773647a0SAndy Whitcroft				#warn "CSB: mark coff<$coff>\n";
11648905a67cSAndy Whitcroft			}
11658905a67cSAndy Whitcroft		}
11668905a67cSAndy Whitcroft		if (($type eq '' || $type eq '{') && $c eq '{') {
11678905a67cSAndy Whitcroft			$level++;
11688905a67cSAndy Whitcroft			$type = '{';
11698905a67cSAndy Whitcroft		}
11708905a67cSAndy Whitcroft		if ($type eq '{' && $c eq '}') {
11718905a67cSAndy Whitcroft			$level--;
11728905a67cSAndy Whitcroft			$type = ($level != 0)? '{' : '';
11738905a67cSAndy Whitcroft
11748905a67cSAndy Whitcroft			if ($level == 0) {
1175b998e001SPatrick Pannuto				if (substr($blk, $off + 1, 1) eq ';') {
1176b998e001SPatrick Pannuto					$off++;
1177b998e001SPatrick Pannuto				}
11788905a67cSAndy Whitcroft				last;
11798905a67cSAndy Whitcroft			}
11808905a67cSAndy Whitcroft		}
1181f74bd194SAndy Whitcroft		# Preprocessor commands end at the newline unless escaped.
1182f74bd194SAndy Whitcroft		if ($type eq '#' && $c eq "\n" && $p ne "\\") {
1183f74bd194SAndy Whitcroft			$level--;
1184f74bd194SAndy Whitcroft			$type = '';
1185f74bd194SAndy Whitcroft			$off++;
1186f74bd194SAndy Whitcroft			last;
1187f74bd194SAndy Whitcroft		}
11888905a67cSAndy Whitcroft		$off++;
11898905a67cSAndy Whitcroft	}
1190a3bb97a7SAndy Whitcroft	# We are truly at the end, so shuffle to the next line.
119113214adfSAndy Whitcroft	if ($off == $len) {
1192a3bb97a7SAndy Whitcroft		$loff = $len + 1;
119313214adfSAndy Whitcroft		$line++;
119413214adfSAndy Whitcroft		$remain--;
119513214adfSAndy Whitcroft	}
11968905a67cSAndy Whitcroft
11978905a67cSAndy Whitcroft	my $statement = substr($blk, $soff, $off - $soff + 1);
11988905a67cSAndy Whitcroft	my $condition = substr($blk, $soff, $coff - $soff + 1);
11998905a67cSAndy Whitcroft
12008905a67cSAndy Whitcroft	#warn "STATEMENT<$statement>\n";
12018905a67cSAndy Whitcroft	#warn "CONDITION<$condition>\n";
12028905a67cSAndy Whitcroft
1203773647a0SAndy Whitcroft	#print "coff<$coff> soff<$off> loff<$loff>\n";
120413214adfSAndy Whitcroft
120513214adfSAndy Whitcroft	return ($statement, $condition,
120613214adfSAndy Whitcroft			$line, $remain + 1, $off - $loff + 1, $level);
120713214adfSAndy Whitcroft}
120813214adfSAndy Whitcroft
1209cf655043SAndy Whitcroftsub statement_lines {
1210cf655043SAndy Whitcroft	my ($stmt) = @_;
1211cf655043SAndy Whitcroft
1212cf655043SAndy Whitcroft	# Strip the diff line prefixes and rip blank lines at start and end.
1213cf655043SAndy Whitcroft	$stmt =~ s/(^|\n)./$1/g;
1214cf655043SAndy Whitcroft	$stmt =~ s/^\s*//;
1215cf655043SAndy Whitcroft	$stmt =~ s/\s*$//;
1216cf655043SAndy Whitcroft
1217cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
1218cf655043SAndy Whitcroft
1219cf655043SAndy Whitcroft	return $#stmt_lines + 2;
1220cf655043SAndy Whitcroft}
1221cf655043SAndy Whitcroft
1222cf655043SAndy Whitcroftsub statement_rawlines {
1223cf655043SAndy Whitcroft	my ($stmt) = @_;
1224cf655043SAndy Whitcroft
1225cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
1226cf655043SAndy Whitcroft
1227cf655043SAndy Whitcroft	return $#stmt_lines + 2;
1228cf655043SAndy Whitcroft}
1229cf655043SAndy Whitcroft
1230cf655043SAndy Whitcroftsub statement_block_size {
1231cf655043SAndy Whitcroft	my ($stmt) = @_;
1232cf655043SAndy Whitcroft
1233cf655043SAndy Whitcroft	$stmt =~ s/(^|\n)./$1/g;
1234cf655043SAndy Whitcroft	$stmt =~ s/^\s*{//;
1235cf655043SAndy Whitcroft	$stmt =~ s/}\s*$//;
1236cf655043SAndy Whitcroft	$stmt =~ s/^\s*//;
1237cf655043SAndy Whitcroft	$stmt =~ s/\s*$//;
1238cf655043SAndy Whitcroft
1239cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
1240cf655043SAndy Whitcroft	my @stmt_statements = ($stmt =~ /;/g);
1241cf655043SAndy Whitcroft
1242cf655043SAndy Whitcroft	my $stmt_lines = $#stmt_lines + 2;
1243cf655043SAndy Whitcroft	my $stmt_statements = $#stmt_statements + 1;
1244cf655043SAndy Whitcroft
1245cf655043SAndy Whitcroft	if ($stmt_lines > $stmt_statements) {
1246cf655043SAndy Whitcroft		return $stmt_lines;
1247cf655043SAndy Whitcroft	} else {
1248cf655043SAndy Whitcroft		return $stmt_statements;
1249cf655043SAndy Whitcroft	}
1250cf655043SAndy Whitcroft}
1251cf655043SAndy Whitcroft
125213214adfSAndy Whitcroftsub ctx_statement_full {
125313214adfSAndy Whitcroft	my ($linenr, $remain, $off) = @_;
125413214adfSAndy Whitcroft	my ($statement, $condition, $level);
125513214adfSAndy Whitcroft
125613214adfSAndy Whitcroft	my (@chunks);
125713214adfSAndy Whitcroft
1258cf655043SAndy Whitcroft	# Grab the first conditional/block pair.
125913214adfSAndy Whitcroft	($statement, $condition, $linenr, $remain, $off, $level) =
126013214adfSAndy Whitcroft				ctx_statement_block($linenr, $remain, $off);
1261773647a0SAndy Whitcroft	#print "F: c<$condition> s<$statement> remain<$remain>\n";
126213214adfSAndy Whitcroft	push(@chunks, [ $condition, $statement ]);
1263cf655043SAndy Whitcroft	if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
1264cf655043SAndy Whitcroft		return ($level, $linenr, @chunks);
1265cf655043SAndy Whitcroft	}
1266cf655043SAndy Whitcroft
1267cf655043SAndy Whitcroft	# Pull in the following conditional/block pairs and see if they
1268cf655043SAndy Whitcroft	# could continue the statement.
1269cf655043SAndy Whitcroft	for (;;) {
127013214adfSAndy Whitcroft		($statement, $condition, $linenr, $remain, $off, $level) =
127113214adfSAndy Whitcroft				ctx_statement_block($linenr, $remain, $off);
1272cf655043SAndy Whitcroft		#print "C: c<$condition> s<$statement> remain<$remain>\n";
1273773647a0SAndy Whitcroft		last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
1274cf655043SAndy Whitcroft		#print "C: push\n";
1275cf655043SAndy Whitcroft		push(@chunks, [ $condition, $statement ]);
127613214adfSAndy Whitcroft	}
127713214adfSAndy Whitcroft
127813214adfSAndy Whitcroft	return ($level, $linenr, @chunks);
12798905a67cSAndy Whitcroft}
12808905a67cSAndy Whitcroft
12814a0df2efSAndy Whitcroftsub ctx_block_get {
1282f0a594c1SAndy Whitcroft	my ($linenr, $remain, $outer, $open, $close, $off) = @_;
12834a0df2efSAndy Whitcroft	my $line;
12844a0df2efSAndy Whitcroft	my $start = $linenr - 1;
12854a0df2efSAndy Whitcroft	my $blk = '';
12864a0df2efSAndy Whitcroft	my @o;
12874a0df2efSAndy Whitcroft	my @c;
12884a0df2efSAndy Whitcroft	my @res = ();
12894a0df2efSAndy Whitcroft
1290f0a594c1SAndy Whitcroft	my $level = 0;
12914635f4fbSAndy Whitcroft	my @stack = ($level);
129200df344fSAndy Whitcroft	for ($line = $start; $remain > 0; $line++) {
129300df344fSAndy Whitcroft		next if ($rawlines[$line] =~ /^-/);
129400df344fSAndy Whitcroft		$remain--;
129500df344fSAndy Whitcroft
129600df344fSAndy Whitcroft		$blk .= $rawlines[$line];
12974635f4fbSAndy Whitcroft
12984635f4fbSAndy Whitcroft		# Handle nested #if/#else.
129901464f30SAndy Whitcroft		if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
13004635f4fbSAndy Whitcroft			push(@stack, $level);
130101464f30SAndy Whitcroft		} elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
13024635f4fbSAndy Whitcroft			$level = $stack[$#stack - 1];
130301464f30SAndy Whitcroft		} elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
13044635f4fbSAndy Whitcroft			$level = pop(@stack);
13054635f4fbSAndy Whitcroft		}
13064635f4fbSAndy Whitcroft
130701464f30SAndy Whitcroft		foreach my $c (split(//, $lines[$line])) {
1308f0a594c1SAndy Whitcroft			##print "C<$c>L<$level><$open$close>O<$off>\n";
1309f0a594c1SAndy Whitcroft			if ($off > 0) {
1310f0a594c1SAndy Whitcroft				$off--;
1311f0a594c1SAndy Whitcroft				next;
1312f0a594c1SAndy Whitcroft			}
13134a0df2efSAndy Whitcroft
1314f0a594c1SAndy Whitcroft			if ($c eq $close && $level > 0) {
1315f0a594c1SAndy Whitcroft				$level--;
1316f0a594c1SAndy Whitcroft				last if ($level == 0);
1317f0a594c1SAndy Whitcroft			} elsif ($c eq $open) {
1318f0a594c1SAndy Whitcroft				$level++;
1319f0a594c1SAndy Whitcroft			}
1320f0a594c1SAndy Whitcroft		}
13214a0df2efSAndy Whitcroft
1322f0a594c1SAndy Whitcroft		if (!$outer || $level <= 1) {
132300df344fSAndy Whitcroft			push(@res, $rawlines[$line]);
13244a0df2efSAndy Whitcroft		}
13254a0df2efSAndy Whitcroft
1326f0a594c1SAndy Whitcroft		last if ($level == 0);
13274a0df2efSAndy Whitcroft	}
13284a0df2efSAndy Whitcroft
1329f0a594c1SAndy Whitcroft	return ($level, @res);
13304a0df2efSAndy Whitcroft}
13314a0df2efSAndy Whitcroftsub ctx_block_outer {
13324a0df2efSAndy Whitcroft	my ($linenr, $remain) = @_;
13334a0df2efSAndy Whitcroft
1334f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
1335f0a594c1SAndy Whitcroft	return @r;
13364a0df2efSAndy Whitcroft}
13374a0df2efSAndy Whitcroftsub ctx_block {
13384a0df2efSAndy Whitcroft	my ($linenr, $remain) = @_;
13394a0df2efSAndy Whitcroft
1340f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1341f0a594c1SAndy Whitcroft	return @r;
1342653d4876SAndy Whitcroft}
1343653d4876SAndy Whitcroftsub ctx_statement {
1344f0a594c1SAndy Whitcroft	my ($linenr, $remain, $off) = @_;
1345f0a594c1SAndy Whitcroft
1346f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1347f0a594c1SAndy Whitcroft	return @r;
1348f0a594c1SAndy Whitcroft}
1349f0a594c1SAndy Whitcroftsub ctx_block_level {
1350653d4876SAndy Whitcroft	my ($linenr, $remain) = @_;
1351653d4876SAndy Whitcroft
1352f0a594c1SAndy Whitcroft	return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
13534a0df2efSAndy Whitcroft}
13549c0ca6f9SAndy Whitcroftsub ctx_statement_level {
13559c0ca6f9SAndy Whitcroft	my ($linenr, $remain, $off) = @_;
13569c0ca6f9SAndy Whitcroft
13579c0ca6f9SAndy Whitcroft	return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
13589c0ca6f9SAndy Whitcroft}
13594a0df2efSAndy Whitcroft
13604a0df2efSAndy Whitcroftsub ctx_locate_comment {
13614a0df2efSAndy Whitcroft	my ($first_line, $end_line) = @_;
13624a0df2efSAndy Whitcroft
13634a0df2efSAndy Whitcroft	# Catch a comment on the end of the line itself.
1364beae6332SAndy Whitcroft	my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
13654a0df2efSAndy Whitcroft	return $current_comment if (defined $current_comment);
13664a0df2efSAndy Whitcroft
13674a0df2efSAndy Whitcroft	# Look through the context and try and figure out if there is a
13684a0df2efSAndy Whitcroft	# comment.
13694a0df2efSAndy Whitcroft	my $in_comment = 0;
13704a0df2efSAndy Whitcroft	$current_comment = '';
13714a0df2efSAndy Whitcroft	for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
137200df344fSAndy Whitcroft		my $line = $rawlines[$linenr - 1];
137300df344fSAndy Whitcroft		#warn "           $line\n";
13744a0df2efSAndy Whitcroft		if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
13754a0df2efSAndy Whitcroft			$in_comment = 1;
13764a0df2efSAndy Whitcroft		}
13774a0df2efSAndy Whitcroft		if ($line =~ m@/\*@) {
13784a0df2efSAndy Whitcroft			$in_comment = 1;
13794a0df2efSAndy Whitcroft		}
13804a0df2efSAndy Whitcroft		if (!$in_comment && $current_comment ne '') {
13814a0df2efSAndy Whitcroft			$current_comment = '';
13824a0df2efSAndy Whitcroft		}
13834a0df2efSAndy Whitcroft		$current_comment .= $line . "\n" if ($in_comment);
13844a0df2efSAndy Whitcroft		if ($line =~ m@\*/@) {
13854a0df2efSAndy Whitcroft			$in_comment = 0;
13864a0df2efSAndy Whitcroft		}
13874a0df2efSAndy Whitcroft	}
13884a0df2efSAndy Whitcroft
13894a0df2efSAndy Whitcroft	chomp($current_comment);
13904a0df2efSAndy Whitcroft	return($current_comment);
13914a0df2efSAndy Whitcroft}
13924a0df2efSAndy Whitcroftsub ctx_has_comment {
13934a0df2efSAndy Whitcroft	my ($first_line, $end_line) = @_;
13944a0df2efSAndy Whitcroft	my $cmt = ctx_locate_comment($first_line, $end_line);
13954a0df2efSAndy Whitcroft
139600df344fSAndy Whitcroft	##print "LINE: $rawlines[$end_line - 1 ]\n";
13974a0df2efSAndy Whitcroft	##print "CMMT: $cmt\n";
13984a0df2efSAndy Whitcroft
13994a0df2efSAndy Whitcroft	return ($cmt ne '');
14004a0df2efSAndy Whitcroft}
14014a0df2efSAndy Whitcroft
14024d001e4dSAndy Whitcroftsub raw_line {
14034d001e4dSAndy Whitcroft	my ($linenr, $cnt) = @_;
14044d001e4dSAndy Whitcroft
14054d001e4dSAndy Whitcroft	my $offset = $linenr - 1;
14064d001e4dSAndy Whitcroft	$cnt++;
14074d001e4dSAndy Whitcroft
14084d001e4dSAndy Whitcroft	my $line;
14094d001e4dSAndy Whitcroft	while ($cnt) {
14104d001e4dSAndy Whitcroft		$line = $rawlines[$offset++];
14114d001e4dSAndy Whitcroft		next if (defined($line) && $line =~ /^-/);
14124d001e4dSAndy Whitcroft		$cnt--;
14134d001e4dSAndy Whitcroft	}
14144d001e4dSAndy Whitcroft
14154d001e4dSAndy Whitcroft	return $line;
14164d001e4dSAndy Whitcroft}
14174d001e4dSAndy Whitcroft
14180a920b5bSAndy Whitcroftsub cat_vet {
14190a920b5bSAndy Whitcroft	my ($vet) = @_;
14209c0ca6f9SAndy Whitcroft	my ($res, $coded);
14210a920b5bSAndy Whitcroft
14229c0ca6f9SAndy Whitcroft	$res = '';
14236c72ffaaSAndy Whitcroft	while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
14246c72ffaaSAndy Whitcroft		$res .= $1;
14256c72ffaaSAndy Whitcroft		if ($2 ne '') {
14269c0ca6f9SAndy Whitcroft			$coded = sprintf("^%c", unpack('C', $2) + 64);
14276c72ffaaSAndy Whitcroft			$res .= $coded;
14286c72ffaaSAndy Whitcroft		}
14299c0ca6f9SAndy Whitcroft	}
14309c0ca6f9SAndy Whitcroft	$res =~ s/$/\$/;
14310a920b5bSAndy Whitcroft
14329c0ca6f9SAndy Whitcroft	return $res;
14330a920b5bSAndy Whitcroft}
14340a920b5bSAndy Whitcroft
1435c2fdda0dSAndy Whitcroftmy $av_preprocessor = 0;
1436cf655043SAndy Whitcroftmy $av_pending;
1437c2fdda0dSAndy Whitcroftmy @av_paren_type;
14381f65f947SAndy Whitcroftmy $av_pend_colon;
1439c2fdda0dSAndy Whitcroft
1440c2fdda0dSAndy Whitcroftsub annotate_reset {
1441c2fdda0dSAndy Whitcroft	$av_preprocessor = 0;
1442cf655043SAndy Whitcroft	$av_pending = '_';
1443cf655043SAndy Whitcroft	@av_paren_type = ('E');
14441f65f947SAndy Whitcroft	$av_pend_colon = 'O';
1445c2fdda0dSAndy Whitcroft}
1446c2fdda0dSAndy Whitcroft
14476c72ffaaSAndy Whitcroftsub annotate_values {
14486c72ffaaSAndy Whitcroft	my ($stream, $type) = @_;
14496c72ffaaSAndy Whitcroft
14506c72ffaaSAndy Whitcroft	my $res;
14511f65f947SAndy Whitcroft	my $var = '_' x length($stream);
14526c72ffaaSAndy Whitcroft	my $cur = $stream;
14536c72ffaaSAndy Whitcroft
1454c2fdda0dSAndy Whitcroft	print "$stream\n" if ($dbg_values > 1);
14556c72ffaaSAndy Whitcroft
14566c72ffaaSAndy Whitcroft	while (length($cur)) {
1457773647a0SAndy Whitcroft		@av_paren_type = ('E') if ($#av_paren_type < 0);
1458cf655043SAndy Whitcroft		print " <" . join('', @av_paren_type) .
1459171ae1a4SAndy Whitcroft				"> <$type> <$av_pending>" if ($dbg_values > 1);
14606c72ffaaSAndy Whitcroft		if ($cur =~ /^(\s+)/o) {
1461c2fdda0dSAndy Whitcroft			print "WS($1)\n" if ($dbg_values > 1);
1462c2fdda0dSAndy Whitcroft			if ($1 =~ /\n/ && $av_preprocessor) {
1463cf655043SAndy Whitcroft				$type = pop(@av_paren_type);
1464c2fdda0dSAndy Whitcroft				$av_preprocessor = 0;
14656c72ffaaSAndy Whitcroft			}
14666c72ffaaSAndy Whitcroft
1467c023e473SFlorian Mickler		} elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
14689446ef56SAndy Whitcroft			print "CAST($1)\n" if ($dbg_values > 1);
14699446ef56SAndy Whitcroft			push(@av_paren_type, $type);
1470addcdceaSAndy Whitcroft			$type = 'c';
14719446ef56SAndy Whitcroft
1472e91b6e26SAndy Whitcroft		} elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
1473c2fdda0dSAndy Whitcroft			print "DECLARE($1)\n" if ($dbg_values > 1);
14746c72ffaaSAndy Whitcroft			$type = 'T';
14756c72ffaaSAndy Whitcroft
1476389a2fe5SAndy Whitcroft		} elsif ($cur =~ /^($Modifier)\s*/) {
1477389a2fe5SAndy Whitcroft			print "MODIFIER($1)\n" if ($dbg_values > 1);
1478389a2fe5SAndy Whitcroft			$type = 'T';
1479389a2fe5SAndy Whitcroft
1480c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
1481171ae1a4SAndy Whitcroft			print "DEFINE($1,$2)\n" if ($dbg_values > 1);
1482c2fdda0dSAndy Whitcroft			$av_preprocessor = 1;
1483171ae1a4SAndy Whitcroft			push(@av_paren_type, $type);
1484171ae1a4SAndy Whitcroft			if ($2 ne '') {
1485cf655043SAndy Whitcroft				$av_pending = 'N';
1486171ae1a4SAndy Whitcroft			}
1487171ae1a4SAndy Whitcroft			$type = 'E';
1488171ae1a4SAndy Whitcroft
1489c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
1490171ae1a4SAndy Whitcroft			print "UNDEF($1)\n" if ($dbg_values > 1);
1491171ae1a4SAndy Whitcroft			$av_preprocessor = 1;
1492171ae1a4SAndy Whitcroft			push(@av_paren_type, $type);
14936c72ffaaSAndy Whitcroft
1494c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
1495cf655043SAndy Whitcroft			print "PRE_START($1)\n" if ($dbg_values > 1);
1496c2fdda0dSAndy Whitcroft			$av_preprocessor = 1;
1497cf655043SAndy Whitcroft
1498cf655043SAndy Whitcroft			push(@av_paren_type, $type);
1499cf655043SAndy Whitcroft			push(@av_paren_type, $type);
1500171ae1a4SAndy Whitcroft			$type = 'E';
1501cf655043SAndy Whitcroft
1502c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
1503cf655043SAndy Whitcroft			print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1504cf655043SAndy Whitcroft			$av_preprocessor = 1;
1505cf655043SAndy Whitcroft
1506cf655043SAndy Whitcroft			push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1507cf655043SAndy Whitcroft
1508171ae1a4SAndy Whitcroft			$type = 'E';
1509cf655043SAndy Whitcroft
1510c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:endif))/o) {
1511cf655043SAndy Whitcroft			print "PRE_END($1)\n" if ($dbg_values > 1);
1512cf655043SAndy Whitcroft
1513cf655043SAndy Whitcroft			$av_preprocessor = 1;
1514cf655043SAndy Whitcroft
1515cf655043SAndy Whitcroft			# Assume all arms of the conditional end as this
1516cf655043SAndy Whitcroft			# one does, and continue as if the #endif was not here.
1517cf655043SAndy Whitcroft			pop(@av_paren_type);
1518cf655043SAndy Whitcroft			push(@av_paren_type, $type);
1519171ae1a4SAndy Whitcroft			$type = 'E';
15206c72ffaaSAndy Whitcroft
15216c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\\\n)/o) {
1522c2fdda0dSAndy Whitcroft			print "PRECONT($1)\n" if ($dbg_values > 1);
15236c72ffaaSAndy Whitcroft
1524171ae1a4SAndy Whitcroft		} elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1525171ae1a4SAndy Whitcroft			print "ATTR($1)\n" if ($dbg_values > 1);
1526171ae1a4SAndy Whitcroft			$av_pending = $type;
1527171ae1a4SAndy Whitcroft			$type = 'N';
1528171ae1a4SAndy Whitcroft
15296c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
1530c2fdda0dSAndy Whitcroft			print "SIZEOF($1)\n" if ($dbg_values > 1);
15316c72ffaaSAndy Whitcroft			if (defined $2) {
1532cf655043SAndy Whitcroft				$av_pending = 'V';
15336c72ffaaSAndy Whitcroft			}
15346c72ffaaSAndy Whitcroft			$type = 'N';
15356c72ffaaSAndy Whitcroft
153614b111c1SAndy Whitcroft		} elsif ($cur =~ /^(if|while|for)\b/o) {
1537c2fdda0dSAndy Whitcroft			print "COND($1)\n" if ($dbg_values > 1);
153814b111c1SAndy Whitcroft			$av_pending = 'E';
15396c72ffaaSAndy Whitcroft			$type = 'N';
15406c72ffaaSAndy Whitcroft
15411f65f947SAndy Whitcroft		} elsif ($cur =~/^(case)/o) {
15421f65f947SAndy Whitcroft			print "CASE($1)\n" if ($dbg_values > 1);
15431f65f947SAndy Whitcroft			$av_pend_colon = 'C';
15441f65f947SAndy Whitcroft			$type = 'N';
15451f65f947SAndy Whitcroft
154614b111c1SAndy Whitcroft		} elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
1547c2fdda0dSAndy Whitcroft			print "KEYWORD($1)\n" if ($dbg_values > 1);
15486c72ffaaSAndy Whitcroft			$type = 'N';
15496c72ffaaSAndy Whitcroft
15506c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\()/o) {
1551c2fdda0dSAndy Whitcroft			print "PAREN('$1')\n" if ($dbg_values > 1);
1552cf655043SAndy Whitcroft			push(@av_paren_type, $av_pending);
1553cf655043SAndy Whitcroft			$av_pending = '_';
15546c72ffaaSAndy Whitcroft			$type = 'N';
15556c72ffaaSAndy Whitcroft
15566c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\))/o) {
1557cf655043SAndy Whitcroft			my $new_type = pop(@av_paren_type);
1558cf655043SAndy Whitcroft			if ($new_type ne '_') {
1559cf655043SAndy Whitcroft				$type = $new_type;
1560c2fdda0dSAndy Whitcroft				print "PAREN('$1') -> $type\n"
1561c2fdda0dSAndy Whitcroft							if ($dbg_values > 1);
15626c72ffaaSAndy Whitcroft			} else {
1563c2fdda0dSAndy Whitcroft				print "PAREN('$1')\n" if ($dbg_values > 1);
15646c72ffaaSAndy Whitcroft			}
15656c72ffaaSAndy Whitcroft
1566c8cb2ca3SAndy Whitcroft		} elsif ($cur =~ /^($Ident)\s*\(/o) {
1567c2fdda0dSAndy Whitcroft			print "FUNC($1)\n" if ($dbg_values > 1);
1568c8cb2ca3SAndy Whitcroft			$type = 'V';
1569cf655043SAndy Whitcroft			$av_pending = 'V';
15706c72ffaaSAndy Whitcroft
15718e761b04SAndy Whitcroft		} elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
15728e761b04SAndy Whitcroft			if (defined $2 && $type eq 'C' || $type eq 'T') {
15731f65f947SAndy Whitcroft				$av_pend_colon = 'B';
15748e761b04SAndy Whitcroft			} elsif ($type eq 'E') {
15758e761b04SAndy Whitcroft				$av_pend_colon = 'L';
15761f65f947SAndy Whitcroft			}
15771f65f947SAndy Whitcroft			print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
15781f65f947SAndy Whitcroft			$type = 'V';
15791f65f947SAndy Whitcroft
15806c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Ident|$Constant)/o) {
1581c2fdda0dSAndy Whitcroft			print "IDENT($1)\n" if ($dbg_values > 1);
15826c72ffaaSAndy Whitcroft			$type = 'V';
15836c72ffaaSAndy Whitcroft
15846c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Assignment)/o) {
1585c2fdda0dSAndy Whitcroft			print "ASSIGN($1)\n" if ($dbg_values > 1);
15866c72ffaaSAndy Whitcroft			$type = 'N';
15876c72ffaaSAndy Whitcroft
1588cf655043SAndy Whitcroft		} elsif ($cur =~/^(;|{|})/) {
1589c2fdda0dSAndy Whitcroft			print "END($1)\n" if ($dbg_values > 1);
159013214adfSAndy Whitcroft			$type = 'E';
15911f65f947SAndy Whitcroft			$av_pend_colon = 'O';
159213214adfSAndy Whitcroft
15938e761b04SAndy Whitcroft		} elsif ($cur =~/^(,)/) {
15948e761b04SAndy Whitcroft			print "COMMA($1)\n" if ($dbg_values > 1);
15958e761b04SAndy Whitcroft			$type = 'C';
15968e761b04SAndy Whitcroft
15971f65f947SAndy Whitcroft		} elsif ($cur =~ /^(\?)/o) {
15981f65f947SAndy Whitcroft			print "QUESTION($1)\n" if ($dbg_values > 1);
15991f65f947SAndy Whitcroft			$type = 'N';
16001f65f947SAndy Whitcroft
16011f65f947SAndy Whitcroft		} elsif ($cur =~ /^(:)/o) {
16021f65f947SAndy Whitcroft			print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
16031f65f947SAndy Whitcroft
16041f65f947SAndy Whitcroft			substr($var, length($res), 1, $av_pend_colon);
16051f65f947SAndy Whitcroft			if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
16061f65f947SAndy Whitcroft				$type = 'E';
16071f65f947SAndy Whitcroft			} else {
16081f65f947SAndy Whitcroft				$type = 'N';
16091f65f947SAndy Whitcroft			}
16101f65f947SAndy Whitcroft			$av_pend_colon = 'O';
16111f65f947SAndy Whitcroft
16128e761b04SAndy Whitcroft		} elsif ($cur =~ /^(\[)/o) {
161313214adfSAndy Whitcroft			print "CLOSE($1)\n" if ($dbg_values > 1);
16146c72ffaaSAndy Whitcroft			$type = 'N';
16156c72ffaaSAndy Whitcroft
16160d413866SAndy Whitcroft		} elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
161774048ed8SAndy Whitcroft			my $variant;
161874048ed8SAndy Whitcroft
161974048ed8SAndy Whitcroft			print "OPV($1)\n" if ($dbg_values > 1);
162074048ed8SAndy Whitcroft			if ($type eq 'V') {
162174048ed8SAndy Whitcroft				$variant = 'B';
162274048ed8SAndy Whitcroft			} else {
162374048ed8SAndy Whitcroft				$variant = 'U';
162474048ed8SAndy Whitcroft			}
162574048ed8SAndy Whitcroft
162674048ed8SAndy Whitcroft			substr($var, length($res), 1, $variant);
162774048ed8SAndy Whitcroft			$type = 'N';
162874048ed8SAndy Whitcroft
16296c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Operators)/o) {
1630c2fdda0dSAndy Whitcroft			print "OP($1)\n" if ($dbg_values > 1);
16316c72ffaaSAndy Whitcroft			if ($1 ne '++' && $1 ne '--') {
16326c72ffaaSAndy Whitcroft				$type = 'N';
16336c72ffaaSAndy Whitcroft			}
16346c72ffaaSAndy Whitcroft
16356c72ffaaSAndy Whitcroft		} elsif ($cur =~ /(^.)/o) {
1636c2fdda0dSAndy Whitcroft			print "C($1)\n" if ($dbg_values > 1);
16376c72ffaaSAndy Whitcroft		}
16386c72ffaaSAndy Whitcroft		if (defined $1) {
16396c72ffaaSAndy Whitcroft			$cur = substr($cur, length($1));
16406c72ffaaSAndy Whitcroft			$res .= $type x length($1);
16416c72ffaaSAndy Whitcroft		}
16426c72ffaaSAndy Whitcroft	}
16436c72ffaaSAndy Whitcroft
16441f65f947SAndy Whitcroft	return ($res, $var);
16456c72ffaaSAndy Whitcroft}
16466c72ffaaSAndy Whitcroft
16478905a67cSAndy Whitcroftsub possible {
164813214adfSAndy Whitcroft	my ($possible, $line) = @_;
16499a974fdbSAndy Whitcroft	my $notPermitted = qr{(?:
16500776e594SAndy Whitcroft		^(?:
16510776e594SAndy Whitcroft			$Modifier|
16520776e594SAndy Whitcroft			$Storage|
16530776e594SAndy Whitcroft			$Type|
16549a974fdbSAndy Whitcroft			DEFINE_\S+
16559a974fdbSAndy Whitcroft		)$|
16569a974fdbSAndy Whitcroft		^(?:
16570776e594SAndy Whitcroft			goto|
16580776e594SAndy Whitcroft			return|
16590776e594SAndy Whitcroft			case|
16600776e594SAndy Whitcroft			else|
16610776e594SAndy Whitcroft			asm|__asm__|
166289a88353SAndy Whitcroft			do|
166389a88353SAndy Whitcroft			\#|
166489a88353SAndy Whitcroft			\#\#|
16659a974fdbSAndy Whitcroft		)(?:\s|$)|
16660776e594SAndy Whitcroft		^(?:typedef|struct|enum)\b
16679a974fdbSAndy Whitcroft	    )}x;
16689a974fdbSAndy Whitcroft	warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
16699a974fdbSAndy Whitcroft	if ($possible !~ $notPermitted) {
1670c45dcabdSAndy Whitcroft		# Check for modifiers.
1671c45dcabdSAndy Whitcroft		$possible =~ s/\s*$Storage\s*//g;
1672c45dcabdSAndy Whitcroft		$possible =~ s/\s*$Sparse\s*//g;
1673c45dcabdSAndy Whitcroft		if ($possible =~ /^\s*$/) {
1674c45dcabdSAndy Whitcroft
1675c45dcabdSAndy Whitcroft		} elsif ($possible =~ /\s/) {
1676c45dcabdSAndy Whitcroft			$possible =~ s/\s*$Type\s*//g;
1677d2506586SAndy Whitcroft			for my $modifier (split(' ', $possible)) {
16789a974fdbSAndy Whitcroft				if ($modifier !~ $notPermitted) {
1679d2506586SAndy Whitcroft					warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1680485ff23eSAlex Dowad					push(@modifierListFile, $modifier);
1681d2506586SAndy Whitcroft				}
16829a974fdbSAndy Whitcroft			}
1683c45dcabdSAndy Whitcroft
1684c45dcabdSAndy Whitcroft		} else {
168513214adfSAndy Whitcroft			warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
1686485ff23eSAlex Dowad			push(@typeListFile, $possible);
1687c45dcabdSAndy Whitcroft		}
16888905a67cSAndy Whitcroft		build_types();
16890776e594SAndy Whitcroft	} else {
16900776e594SAndy Whitcroft		warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
16918905a67cSAndy Whitcroft	}
16928905a67cSAndy Whitcroft}
16938905a67cSAndy Whitcroft
16946c72ffaaSAndy Whitcroftmy $prefix = '';
16956c72ffaaSAndy Whitcroft
1696000d1cc1SJoe Perchessub show_type {
1697cbec18afSJoe Perches	my ($type) = @_;
169891bfe484SJoe Perches
1699cbec18afSJoe Perches	return defined $use_type{$type} if (scalar keys %use_type > 0);
1700cbec18afSJoe Perches
1701cbec18afSJoe Perches	return !defined $ignore_type{$type};
1702000d1cc1SJoe Perches}
1703000d1cc1SJoe Perches
1704f0a594c1SAndy Whitcroftsub report {
1705cbec18afSJoe Perches	my ($level, $type, $msg) = @_;
1706cbec18afSJoe Perches
1707cbec18afSJoe Perches	if (!show_type($type) ||
1708cbec18afSJoe Perches	    (defined $tst_only && $msg !~ /\Q$tst_only\E/)) {
1709773647a0SAndy Whitcroft		return 0;
1710773647a0SAndy Whitcroft	}
171157230297SJoe Perches	my $output = '';
171257230297SJoe Perches	if (-t STDOUT && $color) {
171357230297SJoe Perches		if ($level eq 'ERROR') {
171457230297SJoe Perches			$output .= RED;
171557230297SJoe Perches		} elsif ($level eq 'WARNING') {
171657230297SJoe Perches			$output .= YELLOW;
1717000d1cc1SJoe Perches		} else {
171857230297SJoe Perches			$output .= GREEN;
1719000d1cc1SJoe Perches		}
172057230297SJoe Perches	}
172157230297SJoe Perches	$output .= $prefix . $level . ':';
172257230297SJoe Perches	if ($show_types) {
172357230297SJoe Perches		$output .= BLUE if (-t STDOUT && $color);
172457230297SJoe Perches		$output .= "$type:";
172557230297SJoe Perches	}
172657230297SJoe Perches	$output .= RESET if (-t STDOUT && $color);
172757230297SJoe Perches	$output .= ' ' . $msg . "\n";
172834d8815fSJoe Perches
172934d8815fSJoe Perches	if ($showfile) {
173034d8815fSJoe Perches		my @lines = split("\n", $output, -1);
173134d8815fSJoe Perches		splice(@lines, 1, 1);
173234d8815fSJoe Perches		$output = join("\n", @lines);
173334d8815fSJoe Perches	}
173457230297SJoe Perches	$output = (split('\n', $output))[0] . "\n" if ($terse);
17358905a67cSAndy Whitcroft
173657230297SJoe Perches	push(our @report, $output);
1737773647a0SAndy Whitcroft
1738773647a0SAndy Whitcroft	return 1;
1739f0a594c1SAndy Whitcroft}
1740cbec18afSJoe Perches
1741f0a594c1SAndy Whitcroftsub report_dump {
174213214adfSAndy Whitcroft	our @report;
1743f0a594c1SAndy Whitcroft}
1744000d1cc1SJoe Perches
1745d752fcc8SJoe Perchessub fixup_current_range {
1746d752fcc8SJoe Perches	my ($lineRef, $offset, $length) = @_;
1747d752fcc8SJoe Perches
1748d752fcc8SJoe Perches	if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) {
1749d752fcc8SJoe Perches		my $o = $1;
1750d752fcc8SJoe Perches		my $l = $2;
1751d752fcc8SJoe Perches		my $no = $o + $offset;
1752d752fcc8SJoe Perches		my $nl = $l + $length;
1753d752fcc8SJoe Perches		$$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/;
1754d752fcc8SJoe Perches	}
1755d752fcc8SJoe Perches}
1756d752fcc8SJoe Perches
1757d752fcc8SJoe Perchessub fix_inserted_deleted_lines {
1758d752fcc8SJoe Perches	my ($linesRef, $insertedRef, $deletedRef) = @_;
1759d752fcc8SJoe Perches
1760d752fcc8SJoe Perches	my $range_last_linenr = 0;
1761d752fcc8SJoe Perches	my $delta_offset = 0;
1762d752fcc8SJoe Perches
1763d752fcc8SJoe Perches	my $old_linenr = 0;
1764d752fcc8SJoe Perches	my $new_linenr = 0;
1765d752fcc8SJoe Perches
1766d752fcc8SJoe Perches	my $next_insert = 0;
1767d752fcc8SJoe Perches	my $next_delete = 0;
1768d752fcc8SJoe Perches
1769d752fcc8SJoe Perches	my @lines = ();
1770d752fcc8SJoe Perches
1771d752fcc8SJoe Perches	my $inserted = @{$insertedRef}[$next_insert++];
1772d752fcc8SJoe Perches	my $deleted = @{$deletedRef}[$next_delete++];
1773d752fcc8SJoe Perches
1774d752fcc8SJoe Perches	foreach my $old_line (@{$linesRef}) {
1775d752fcc8SJoe Perches		my $save_line = 1;
1776d752fcc8SJoe Perches		my $line = $old_line;	#don't modify the array
1777323b267fSJoe Perches		if ($line =~ /^(?:\+\+\+|\-\-\-)\s+\S+/) {	#new filename
1778d752fcc8SJoe Perches			$delta_offset = 0;
1779d752fcc8SJoe Perches		} elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) {	#new hunk
1780d752fcc8SJoe Perches			$range_last_linenr = $new_linenr;
1781d752fcc8SJoe Perches			fixup_current_range(\$line, $delta_offset, 0);
1782d752fcc8SJoe Perches		}
1783d752fcc8SJoe Perches
1784d752fcc8SJoe Perches		while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) {
1785d752fcc8SJoe Perches			$deleted = @{$deletedRef}[$next_delete++];
1786d752fcc8SJoe Perches			$save_line = 0;
1787d752fcc8SJoe Perches			fixup_current_range(\$lines[$range_last_linenr], $delta_offset--, -1);
1788d752fcc8SJoe Perches		}
1789d752fcc8SJoe Perches
1790d752fcc8SJoe Perches		while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) {
1791d752fcc8SJoe Perches			push(@lines, ${$inserted}{'LINE'});
1792d752fcc8SJoe Perches			$inserted = @{$insertedRef}[$next_insert++];
1793d752fcc8SJoe Perches			$new_linenr++;
1794d752fcc8SJoe Perches			fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1);
1795d752fcc8SJoe Perches		}
1796d752fcc8SJoe Perches
1797d752fcc8SJoe Perches		if ($save_line) {
1798d752fcc8SJoe Perches			push(@lines, $line);
1799d752fcc8SJoe Perches			$new_linenr++;
1800d752fcc8SJoe Perches		}
1801d752fcc8SJoe Perches
1802d752fcc8SJoe Perches		$old_linenr++;
1803d752fcc8SJoe Perches	}
1804d752fcc8SJoe Perches
1805d752fcc8SJoe Perches	return @lines;
1806d752fcc8SJoe Perches}
1807d752fcc8SJoe Perches
1808f2d7e4d4SJoe Perchessub fix_insert_line {
1809f2d7e4d4SJoe Perches	my ($linenr, $line) = @_;
1810f2d7e4d4SJoe Perches
1811f2d7e4d4SJoe Perches	my $inserted = {
1812f2d7e4d4SJoe Perches		LINENR => $linenr,
1813f2d7e4d4SJoe Perches		LINE => $line,
1814f2d7e4d4SJoe Perches	};
1815f2d7e4d4SJoe Perches	push(@fixed_inserted, $inserted);
1816f2d7e4d4SJoe Perches}
1817f2d7e4d4SJoe Perches
1818f2d7e4d4SJoe Perchessub fix_delete_line {
1819f2d7e4d4SJoe Perches	my ($linenr, $line) = @_;
1820f2d7e4d4SJoe Perches
1821f2d7e4d4SJoe Perches	my $deleted = {
1822f2d7e4d4SJoe Perches		LINENR => $linenr,
1823f2d7e4d4SJoe Perches		LINE => $line,
1824f2d7e4d4SJoe Perches	};
1825f2d7e4d4SJoe Perches
1826f2d7e4d4SJoe Perches	push(@fixed_deleted, $deleted);
1827f2d7e4d4SJoe Perches}
1828f2d7e4d4SJoe Perches
1829de7d4f0eSAndy Whitcroftsub ERROR {
1830cbec18afSJoe Perches	my ($type, $msg) = @_;
1831cbec18afSJoe Perches
1832cbec18afSJoe Perches	if (report("ERROR", $type, $msg)) {
1833de7d4f0eSAndy Whitcroft		our $clean = 0;
18346c72ffaaSAndy Whitcroft		our $cnt_error++;
18353705ce5bSJoe Perches		return 1;
1836de7d4f0eSAndy Whitcroft	}
18373705ce5bSJoe Perches	return 0;
1838773647a0SAndy Whitcroft}
1839de7d4f0eSAndy Whitcroftsub WARN {
1840cbec18afSJoe Perches	my ($type, $msg) = @_;
1841cbec18afSJoe Perches
1842cbec18afSJoe Perches	if (report("WARNING", $type, $msg)) {
1843de7d4f0eSAndy Whitcroft		our $clean = 0;
18446c72ffaaSAndy Whitcroft		our $cnt_warn++;
18453705ce5bSJoe Perches		return 1;
1846de7d4f0eSAndy Whitcroft	}
18473705ce5bSJoe Perches	return 0;
1848773647a0SAndy Whitcroft}
1849de7d4f0eSAndy Whitcroftsub CHK {
1850cbec18afSJoe Perches	my ($type, $msg) = @_;
1851cbec18afSJoe Perches
1852cbec18afSJoe Perches	if ($check && report("CHECK", $type, $msg)) {
1853de7d4f0eSAndy Whitcroft		our $clean = 0;
18546c72ffaaSAndy Whitcroft		our $cnt_chk++;
18553705ce5bSJoe Perches		return 1;
18566c72ffaaSAndy Whitcroft	}
18573705ce5bSJoe Perches	return 0;
1858de7d4f0eSAndy Whitcroft}
1859de7d4f0eSAndy Whitcroft
18606ecd9674SAndy Whitcroftsub check_absolute_file {
18616ecd9674SAndy Whitcroft	my ($absolute, $herecurr) = @_;
18626ecd9674SAndy Whitcroft	my $file = $absolute;
18636ecd9674SAndy Whitcroft
18646ecd9674SAndy Whitcroft	##print "absolute<$absolute>\n";
18656ecd9674SAndy Whitcroft
18666ecd9674SAndy Whitcroft	# See if any suffix of this path is a path within the tree.
18676ecd9674SAndy Whitcroft	while ($file =~ s@^[^/]*/@@) {
18686ecd9674SAndy Whitcroft		if (-f "$root/$file") {
18696ecd9674SAndy Whitcroft			##print "file<$file>\n";
18706ecd9674SAndy Whitcroft			last;
18716ecd9674SAndy Whitcroft		}
18726ecd9674SAndy Whitcroft	}
18736ecd9674SAndy Whitcroft	if (! -f _)  {
18746ecd9674SAndy Whitcroft		return 0;
18756ecd9674SAndy Whitcroft	}
18766ecd9674SAndy Whitcroft
18776ecd9674SAndy Whitcroft	# It is, so see if the prefix is acceptable.
18786ecd9674SAndy Whitcroft	my $prefix = $absolute;
18796ecd9674SAndy Whitcroft	substr($prefix, -length($file)) = '';
18806ecd9674SAndy Whitcroft
18816ecd9674SAndy Whitcroft	##print "prefix<$prefix>\n";
18826ecd9674SAndy Whitcroft	if ($prefix ne ".../") {
1883000d1cc1SJoe Perches		WARN("USE_RELATIVE_PATH",
1884000d1cc1SJoe Perches		     "use relative pathname instead of absolute in changelog text\n" . $herecurr);
18856ecd9674SAndy Whitcroft	}
18866ecd9674SAndy Whitcroft}
18876ecd9674SAndy Whitcroft
18883705ce5bSJoe Perchessub trim {
18893705ce5bSJoe Perches	my ($string) = @_;
18903705ce5bSJoe Perches
1891b34c648bSJoe Perches	$string =~ s/^\s+|\s+$//g;
1892b34c648bSJoe Perches
1893b34c648bSJoe Perches	return $string;
1894b34c648bSJoe Perches}
1895b34c648bSJoe Perches
1896b34c648bSJoe Perchessub ltrim {
1897b34c648bSJoe Perches	my ($string) = @_;
1898b34c648bSJoe Perches
1899b34c648bSJoe Perches	$string =~ s/^\s+//;
1900b34c648bSJoe Perches
1901b34c648bSJoe Perches	return $string;
1902b34c648bSJoe Perches}
1903b34c648bSJoe Perches
1904b34c648bSJoe Perchessub rtrim {
1905b34c648bSJoe Perches	my ($string) = @_;
1906b34c648bSJoe Perches
1907b34c648bSJoe Perches	$string =~ s/\s+$//;
19083705ce5bSJoe Perches
19093705ce5bSJoe Perches	return $string;
19103705ce5bSJoe Perches}
19113705ce5bSJoe Perches
191252ea8506SJoe Perchessub string_find_replace {
191352ea8506SJoe Perches	my ($string, $find, $replace) = @_;
191452ea8506SJoe Perches
191552ea8506SJoe Perches	$string =~ s/$find/$replace/g;
191652ea8506SJoe Perches
191752ea8506SJoe Perches	return $string;
191852ea8506SJoe Perches}
191952ea8506SJoe Perches
19203705ce5bSJoe Perchessub tabify {
19213705ce5bSJoe Perches	my ($leading) = @_;
19223705ce5bSJoe Perches
19233705ce5bSJoe Perches	my $source_indent = 8;
19243705ce5bSJoe Perches	my $max_spaces_before_tab = $source_indent - 1;
19253705ce5bSJoe Perches	my $spaces_to_tab = " " x $source_indent;
19263705ce5bSJoe Perches
19273705ce5bSJoe Perches	#convert leading spaces to tabs
19283705ce5bSJoe Perches	1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
19293705ce5bSJoe Perches	#Remove spaces before a tab
19303705ce5bSJoe Perches	1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
19313705ce5bSJoe Perches
19323705ce5bSJoe Perches	return "$leading";
19333705ce5bSJoe Perches}
19343705ce5bSJoe Perches
1935d1fe9c09SJoe Perchessub pos_last_openparen {
1936d1fe9c09SJoe Perches	my ($line) = @_;
1937d1fe9c09SJoe Perches
1938d1fe9c09SJoe Perches	my $pos = 0;
1939d1fe9c09SJoe Perches
1940d1fe9c09SJoe Perches	my $opens = $line =~ tr/\(/\(/;
1941d1fe9c09SJoe Perches	my $closes = $line =~ tr/\)/\)/;
1942d1fe9c09SJoe Perches
1943d1fe9c09SJoe Perches	my $last_openparen = 0;
1944d1fe9c09SJoe Perches
1945d1fe9c09SJoe Perches	if (($opens == 0) || ($closes >= $opens)) {
1946d1fe9c09SJoe Perches		return -1;
1947d1fe9c09SJoe Perches	}
1948d1fe9c09SJoe Perches
1949d1fe9c09SJoe Perches	my $len = length($line);
1950d1fe9c09SJoe Perches
1951d1fe9c09SJoe Perches	for ($pos = 0; $pos < $len; $pos++) {
1952d1fe9c09SJoe Perches		my $string = substr($line, $pos);
1953d1fe9c09SJoe Perches		if ($string =~ /^($FuncArg|$balanced_parens)/) {
1954d1fe9c09SJoe Perches			$pos += length($1) - 1;
1955d1fe9c09SJoe Perches		} elsif (substr($line, $pos, 1) eq '(') {
1956d1fe9c09SJoe Perches			$last_openparen = $pos;
1957d1fe9c09SJoe Perches		} elsif (index($string, '(') == -1) {
1958d1fe9c09SJoe Perches			last;
1959d1fe9c09SJoe Perches		}
1960d1fe9c09SJoe Perches	}
1961d1fe9c09SJoe Perches
196291cb5195SJoe Perches	return length(expand_tabs(substr($line, 0, $last_openparen))) + 1;
1963d1fe9c09SJoe Perches}
1964d1fe9c09SJoe Perches
19650a920b5bSAndy Whitcroftsub process {
19660a920b5bSAndy Whitcroft	my $filename = shift;
19670a920b5bSAndy Whitcroft
19680a920b5bSAndy Whitcroft	my $linenr=0;
19690a920b5bSAndy Whitcroft	my $prevline="";
1970c2fdda0dSAndy Whitcroft	my $prevrawline="";
19710a920b5bSAndy Whitcroft	my $stashline="";
1972c2fdda0dSAndy Whitcroft	my $stashrawline="";
19730a920b5bSAndy Whitcroft
19744a0df2efSAndy Whitcroft	my $length;
19750a920b5bSAndy Whitcroft	my $indent;
19760a920b5bSAndy Whitcroft	my $previndent=0;
19770a920b5bSAndy Whitcroft	my $stashindent=0;
19780a920b5bSAndy Whitcroft
1979de7d4f0eSAndy Whitcroft	our $clean = 1;
19800a920b5bSAndy Whitcroft	my $signoff = 0;
19810a920b5bSAndy Whitcroft	my $is_patch = 0;
198229ee1b0cSJoe Perches	my $in_header_lines = $file ? 0 : 1;
198315662b3eSJoe Perches	my $in_commit_log = 0;		#Scanning lines before patch
1984bf4daf12SJoe Perches       my $commit_log_possible_stack_dump = 0;
19852a076f40SJoe Perches	my $commit_log_long_line = 0;
1986e518e9a5SJoe Perches	my $commit_log_has_diff = 0;
198713f1937eSJoe Perches	my $reported_maintainer_file = 0;
1988fa64205dSPasi Savanainen	my $non_utf8_charset = 0;
1989fa64205dSPasi Savanainen
1990365dd4eaSJoe Perches	my $last_blank_line = 0;
19915e4f6ba5SJoe Perches	my $last_coalesced_string_linenr = -1;
1992365dd4eaSJoe Perches
199313214adfSAndy Whitcroft	our @report = ();
19946c72ffaaSAndy Whitcroft	our $cnt_lines = 0;
19956c72ffaaSAndy Whitcroft	our $cnt_error = 0;
19966c72ffaaSAndy Whitcroft	our $cnt_warn = 0;
19976c72ffaaSAndy Whitcroft	our $cnt_chk = 0;
19986c72ffaaSAndy Whitcroft
19990a920b5bSAndy Whitcroft	# Trace the real file/line as we go.
20000a920b5bSAndy Whitcroft	my $realfile = '';
20010a920b5bSAndy Whitcroft	my $realline = 0;
20020a920b5bSAndy Whitcroft	my $realcnt = 0;
20030a920b5bSAndy Whitcroft	my $here = '';
20040a920b5bSAndy Whitcroft	my $in_comment = 0;
2005c2fdda0dSAndy Whitcroft	my $comment_edge = 0;
20060a920b5bSAndy Whitcroft	my $first_line = 0;
20071e855726SWolfram Sang	my $p1_prefix = '';
20080a920b5bSAndy Whitcroft
200913214adfSAndy Whitcroft	my $prev_values = 'E';
201013214adfSAndy Whitcroft
201113214adfSAndy Whitcroft	# suppression flags
2012773647a0SAndy Whitcroft	my %suppress_ifbraces;
2013170d3a22SAndy Whitcroft	my %suppress_whiletrailers;
20142b474a1aSAndy Whitcroft	my %suppress_export;
20153e469cdcSAndy Whitcroft	my $suppress_statement = 0;
2016653d4876SAndy Whitcroft
20177e51f197SJoe Perches	my %signatures = ();
2018323c1260SJoe Perches
2019c2fdda0dSAndy Whitcroft	# Pre-scan the patch sanitizing the lines.
2020de7d4f0eSAndy Whitcroft	# Pre-scan the patch looking for any __setup documentation.
2021c2fdda0dSAndy Whitcroft	#
2022de7d4f0eSAndy Whitcroft	my @setup_docs = ();
2023de7d4f0eSAndy Whitcroft	my $setup_docs = 0;
2024773647a0SAndy Whitcroft
2025d8b07710SJoe Perches	my $camelcase_file_seeded = 0;
2026d8b07710SJoe Perches
2027773647a0SAndy Whitcroft	sanitise_line_reset();
2028c2fdda0dSAndy Whitcroft	my $line;
2029c2fdda0dSAndy Whitcroft	foreach my $rawline (@rawlines) {
2030773647a0SAndy Whitcroft		$linenr++;
2031773647a0SAndy Whitcroft		$line = $rawline;
2032c2fdda0dSAndy Whitcroft
20333705ce5bSJoe Perches		push(@fixed, $rawline) if ($fix);
20343705ce5bSJoe Perches
2035773647a0SAndy Whitcroft		if ($rawline=~/^\+\+\+\s+(\S+)/) {
2036de7d4f0eSAndy Whitcroft			$setup_docs = 0;
2037de7d4f0eSAndy Whitcroft			if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
2038de7d4f0eSAndy Whitcroft				$setup_docs = 1;
2039de7d4f0eSAndy Whitcroft			}
2040773647a0SAndy Whitcroft			#next;
2041de7d4f0eSAndy Whitcroft		}
2042773647a0SAndy Whitcroft		if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
2043773647a0SAndy Whitcroft			$realline=$1-1;
2044773647a0SAndy Whitcroft			if (defined $2) {
2045773647a0SAndy Whitcroft				$realcnt=$3+1;
2046773647a0SAndy Whitcroft			} else {
2047773647a0SAndy Whitcroft				$realcnt=1+1;
2048773647a0SAndy Whitcroft			}
2049c45dcabdSAndy Whitcroft			$in_comment = 0;
2050773647a0SAndy Whitcroft
2051773647a0SAndy Whitcroft			# Guestimate if this is a continuing comment.  Run
2052773647a0SAndy Whitcroft			# the context looking for a comment "edge".  If this
2053773647a0SAndy Whitcroft			# edge is a close comment then we must be in a comment
2054773647a0SAndy Whitcroft			# at context start.
2055773647a0SAndy Whitcroft			my $edge;
205601fa9147SAndy Whitcroft			my $cnt = $realcnt;
205701fa9147SAndy Whitcroft			for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
205801fa9147SAndy Whitcroft				next if (defined $rawlines[$ln - 1] &&
205901fa9147SAndy Whitcroft					 $rawlines[$ln - 1] =~ /^-/);
206001fa9147SAndy Whitcroft				$cnt--;
206101fa9147SAndy Whitcroft				#print "RAW<$rawlines[$ln - 1]>\n";
2062721c1cb6SAndy Whitcroft				last if (!defined $rawlines[$ln - 1]);
2063fae17daeSAndy Whitcroft				if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
2064fae17daeSAndy Whitcroft				    $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
2065fae17daeSAndy Whitcroft					($edge) = $1;
2066fae17daeSAndy Whitcroft					last;
2067fae17daeSAndy Whitcroft				}
2068773647a0SAndy Whitcroft			}
2069773647a0SAndy Whitcroft			if (defined $edge && $edge eq '*/') {
2070773647a0SAndy Whitcroft				$in_comment = 1;
2071773647a0SAndy Whitcroft			}
2072773647a0SAndy Whitcroft
2073773647a0SAndy Whitcroft			# Guestimate if this is a continuing comment.  If this
2074773647a0SAndy Whitcroft			# is the start of a diff block and this line starts
2075773647a0SAndy Whitcroft			# ' *' then it is very likely a comment.
2076773647a0SAndy Whitcroft			if (!defined $edge &&
207783242e0cSAndy Whitcroft			    $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
2078773647a0SAndy Whitcroft			{
2079773647a0SAndy Whitcroft				$in_comment = 1;
2080773647a0SAndy Whitcroft			}
2081773647a0SAndy Whitcroft
2082773647a0SAndy Whitcroft			##print "COMMENT:$in_comment edge<$edge> $rawline\n";
2083773647a0SAndy Whitcroft			sanitise_line_reset($in_comment);
2084773647a0SAndy Whitcroft
2085171ae1a4SAndy Whitcroft		} elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
2086773647a0SAndy Whitcroft			# Standardise the strings and chars within the input to
2087171ae1a4SAndy Whitcroft			# simplify matching -- only bother with positive lines.
2088773647a0SAndy Whitcroft			$line = sanitise_line($rawline);
2089773647a0SAndy Whitcroft		}
2090773647a0SAndy Whitcroft		push(@lines, $line);
2091773647a0SAndy Whitcroft
2092773647a0SAndy Whitcroft		if ($realcnt > 1) {
2093773647a0SAndy Whitcroft			$realcnt-- if ($line =~ /^(?:\+| |$)/);
2094773647a0SAndy Whitcroft		} else {
2095773647a0SAndy Whitcroft			$realcnt = 0;
2096773647a0SAndy Whitcroft		}
2097773647a0SAndy Whitcroft
2098773647a0SAndy Whitcroft		#print "==>$rawline\n";
2099773647a0SAndy Whitcroft		#print "-->$line\n";
2100de7d4f0eSAndy Whitcroft
2101de7d4f0eSAndy Whitcroft		if ($setup_docs && $line =~ /^\+/) {
2102de7d4f0eSAndy Whitcroft			push(@setup_docs, $line);
2103de7d4f0eSAndy Whitcroft		}
2104de7d4f0eSAndy Whitcroft	}
2105de7d4f0eSAndy Whitcroft
21066c72ffaaSAndy Whitcroft	$prefix = '';
21076c72ffaaSAndy Whitcroft
2108773647a0SAndy Whitcroft	$realcnt = 0;
2109773647a0SAndy Whitcroft	$linenr = 0;
2110194f66fcSJoe Perches	$fixlinenr = -1;
21110a920b5bSAndy Whitcroft	foreach my $line (@lines) {
21120a920b5bSAndy Whitcroft		$linenr++;
2113194f66fcSJoe Perches		$fixlinenr++;
21141b5539b1SJoe Perches		my $sline = $line;	#copy of $line
21151b5539b1SJoe Perches		$sline =~ s/$;/ /g;	#with comments as spaces
21160a920b5bSAndy Whitcroft
2117c2fdda0dSAndy Whitcroft		my $rawline = $rawlines[$linenr - 1];
21186c72ffaaSAndy Whitcroft
21190a920b5bSAndy Whitcroft#extract the line range in the file after the patch is applied
2120e518e9a5SJoe Perches		if (!$in_commit_log &&
2121e518e9a5SJoe Perches		    $line =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
21220a920b5bSAndy Whitcroft			$is_patch = 1;
21234a0df2efSAndy Whitcroft			$first_line = $linenr + 1;
21240a920b5bSAndy Whitcroft			$realline=$1-1;
21250a920b5bSAndy Whitcroft			if (defined $2) {
21260a920b5bSAndy Whitcroft				$realcnt=$3+1;
21270a920b5bSAndy Whitcroft			} else {
21280a920b5bSAndy Whitcroft				$realcnt=1+1;
21290a920b5bSAndy Whitcroft			}
2130c2fdda0dSAndy Whitcroft			annotate_reset();
213113214adfSAndy Whitcroft			$prev_values = 'E';
213213214adfSAndy Whitcroft
2133773647a0SAndy Whitcroft			%suppress_ifbraces = ();
2134170d3a22SAndy Whitcroft			%suppress_whiletrailers = ();
21352b474a1aSAndy Whitcroft			%suppress_export = ();
21363e469cdcSAndy Whitcroft			$suppress_statement = 0;
21370a920b5bSAndy Whitcroft			next;
21380a920b5bSAndy Whitcroft
21394a0df2efSAndy Whitcroft# track the line number as we move through the hunk, note that
21404a0df2efSAndy Whitcroft# new versions of GNU diff omit the leading space on completely
21414a0df2efSAndy Whitcroft# blank context lines so we need to count that too.
2142773647a0SAndy Whitcroft		} elsif ($line =~ /^( |\+|$)/) {
21430a920b5bSAndy Whitcroft			$realline++;
2144d8aaf121SAndy Whitcroft			$realcnt-- if ($realcnt != 0);
21450a920b5bSAndy Whitcroft
21464a0df2efSAndy Whitcroft			# Measure the line length and indent.
2147c2fdda0dSAndy Whitcroft			($length, $indent) = line_stats($rawline);
21480a920b5bSAndy Whitcroft
21490a920b5bSAndy Whitcroft			# Track the previous line.
21500a920b5bSAndy Whitcroft			($prevline, $stashline) = ($stashline, $line);
21510a920b5bSAndy Whitcroft			($previndent, $stashindent) = ($stashindent, $indent);
2152c2fdda0dSAndy Whitcroft			($prevrawline, $stashrawline) = ($stashrawline, $rawline);
2153c2fdda0dSAndy Whitcroft
2154773647a0SAndy Whitcroft			#warn "line<$line>\n";
21556c72ffaaSAndy Whitcroft
2156d8aaf121SAndy Whitcroft		} elsif ($realcnt == 1) {
2157d8aaf121SAndy Whitcroft			$realcnt--;
21580a920b5bSAndy Whitcroft		}
21590a920b5bSAndy Whitcroft
2160cc77cdcaSAndy Whitcroft		my $hunk_line = ($realcnt != 0);
2161cc77cdcaSAndy Whitcroft
21626c72ffaaSAndy Whitcroft		$here = "#$linenr: " if (!$file);
21636c72ffaaSAndy Whitcroft		$here = "#$realline: " if ($file);
2164773647a0SAndy Whitcroft
21652ac73b4fSJoe Perches		my $found_file = 0;
2166773647a0SAndy Whitcroft		# extract the filename as it passes
21673bf9a009SRabin Vincent		if ($line =~ /^diff --git.*?(\S+)$/) {
21683bf9a009SRabin Vincent			$realfile = $1;
21692b7ab453SJoe Perches			$realfile =~ s@^([^/]*)/@@ if (!$file);
2170270c49a0SJoe Perches			$in_commit_log = 0;
21712ac73b4fSJoe Perches			$found_file = 1;
21723bf9a009SRabin Vincent		} elsif ($line =~ /^\+\+\+\s+(\S+)/) {
2173773647a0SAndy Whitcroft			$realfile = $1;
21742b7ab453SJoe Perches			$realfile =~ s@^([^/]*)/@@ if (!$file);
2175270c49a0SJoe Perches			$in_commit_log = 0;
21761e855726SWolfram Sang
21771e855726SWolfram Sang			$p1_prefix = $1;
2178e2f7aa4bSAndy Whitcroft			if (!$file && $tree && $p1_prefix ne '' &&
2179e2f7aa4bSAndy Whitcroft			    -e "$root/$p1_prefix") {
2180000d1cc1SJoe Perches				WARN("PATCH_PREFIX",
2181000d1cc1SJoe Perches				     "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
21821e855726SWolfram Sang			}
2183773647a0SAndy Whitcroft
2184c1ab3326SAndy Whitcroft			if ($realfile =~ m@^include/asm/@) {
2185000d1cc1SJoe Perches				ERROR("MODIFIED_INCLUDE_ASM",
2186000d1cc1SJoe Perches				      "do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
2187773647a0SAndy Whitcroft			}
21882ac73b4fSJoe Perches			$found_file = 1;
21892ac73b4fSJoe Perches		}
21902ac73b4fSJoe Perches
219134d8815fSJoe Perches#make up the handle for any error we report on this line
219234d8815fSJoe Perches		if ($showfile) {
219334d8815fSJoe Perches			$prefix = "$realfile:$realline: "
219434d8815fSJoe Perches		} elsif ($emacs) {
21957d3a9f67SJoe Perches			if ($file) {
21967d3a9f67SJoe Perches				$prefix = "$filename:$realline: ";
21977d3a9f67SJoe Perches			} else {
219834d8815fSJoe Perches				$prefix = "$filename:$linenr: ";
219934d8815fSJoe Perches			}
22007d3a9f67SJoe Perches		}
220134d8815fSJoe Perches
22022ac73b4fSJoe Perches		if ($found_file) {
22037bd7e483SJoe Perches			if ($realfile =~ m@^(?:drivers/net/|net/|drivers/staging/)@) {
22042ac73b4fSJoe Perches				$check = 1;
22052ac73b4fSJoe Perches			} else {
22062ac73b4fSJoe Perches				$check = $check_orig;
22072ac73b4fSJoe Perches			}
2208773647a0SAndy Whitcroft			next;
2209773647a0SAndy Whitcroft		}
2210773647a0SAndy Whitcroft
2211389834b6SRandy Dunlap		$here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
22120a920b5bSAndy Whitcroft
2213c2fdda0dSAndy Whitcroft		my $hereline = "$here\n$rawline\n";
2214c2fdda0dSAndy Whitcroft		my $herecurr = "$here\n$rawline\n";
2215c2fdda0dSAndy Whitcroft		my $hereprev = "$here\n$prevrawline\n$rawline\n";
22160a920b5bSAndy Whitcroft
22176c72ffaaSAndy Whitcroft		$cnt_lines++ if ($realcnt != 0);
22186c72ffaaSAndy Whitcroft
2219e518e9a5SJoe Perches# Check if the commit log has what seems like a diff which can confuse patch
2220e518e9a5SJoe Perches		if ($in_commit_log && !$commit_log_has_diff &&
2221e518e9a5SJoe Perches		    (($line =~ m@^\s+diff\b.*a/[\w/]+@ &&
2222e518e9a5SJoe Perches		      $line =~ m@^\s+diff\b.*a/([\w/]+)\s+b/$1\b@) ||
2223e518e9a5SJoe Perches		     $line =~ m@^\s*(?:\-\-\-\s+a/|\+\+\+\s+b/)@ ||
2224e518e9a5SJoe Perches		     $line =~ m/^\s*\@\@ \-\d+,\d+ \+\d+,\d+ \@\@/)) {
2225e518e9a5SJoe Perches			ERROR("DIFF_IN_COMMIT_MSG",
2226e518e9a5SJoe Perches			      "Avoid using diff content in the commit message - patch(1) might not work\n" . $herecurr);
2227e518e9a5SJoe Perches			$commit_log_has_diff = 1;
2228e518e9a5SJoe Perches		}
2229e518e9a5SJoe Perches
22303bf9a009SRabin Vincent# Check for incorrect file permissions
22313bf9a009SRabin Vincent		if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
22323bf9a009SRabin Vincent			my $permhere = $here . "FILE: $realfile\n";
223304db4d25SJoe Perches			if ($realfile !~ m@scripts/@ &&
223404db4d25SJoe Perches			    $realfile !~ /\.(py|pl|awk|sh)$/) {
2235000d1cc1SJoe Perches				ERROR("EXECUTE_PERMISSIONS",
2236000d1cc1SJoe Perches				      "do not set execute permissions for source files\n" . $permhere);
22373bf9a009SRabin Vincent			}
22383bf9a009SRabin Vincent		}
22393bf9a009SRabin Vincent
224020112475SJoe Perches# Check the patch for a signoff:
2241d8aaf121SAndy Whitcroft		if ($line =~ /^\s*signed-off-by:/i) {
22424a0df2efSAndy Whitcroft			$signoff++;
224315662b3eSJoe Perches			$in_commit_log = 0;
22440a920b5bSAndy Whitcroft		}
224520112475SJoe Perches
2246e0d975b1SJoe Perches# Check if MAINTAINERS is being updated.  If so, there's probably no need to
2247e0d975b1SJoe Perches# emit the "does MAINTAINERS need updating?" message on file add/move/delete
2248e0d975b1SJoe Perches		if ($line =~ /^\s*MAINTAINERS\s*\|/) {
2249e0d975b1SJoe Perches			$reported_maintainer_file = 1;
2250e0d975b1SJoe Perches		}
2251e0d975b1SJoe Perches
225220112475SJoe Perches# Check signature styles
2253270c49a0SJoe Perches		if (!$in_header_lines &&
2254ce0338dfSJoe Perches		    $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
225520112475SJoe Perches			my $space_before = $1;
225620112475SJoe Perches			my $sign_off = $2;
225720112475SJoe Perches			my $space_after = $3;
225820112475SJoe Perches			my $email = $4;
225920112475SJoe Perches			my $ucfirst_sign_off = ucfirst(lc($sign_off));
226020112475SJoe Perches
2261ce0338dfSJoe Perches			if ($sign_off !~ /$signature_tags/) {
2262ce0338dfSJoe Perches				WARN("BAD_SIGN_OFF",
2263ce0338dfSJoe Perches				     "Non-standard signature: $sign_off\n" . $herecurr);
2264ce0338dfSJoe Perches			}
226520112475SJoe Perches			if (defined $space_before && $space_before ne "") {
22663705ce5bSJoe Perches				if (WARN("BAD_SIGN_OFF",
22673705ce5bSJoe Perches					 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
22683705ce5bSJoe Perches				    $fix) {
2269194f66fcSJoe Perches					$fixed[$fixlinenr] =
22703705ce5bSJoe Perches					    "$ucfirst_sign_off $email";
22713705ce5bSJoe Perches				}
227220112475SJoe Perches			}
227320112475SJoe Perches			if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
22743705ce5bSJoe Perches				if (WARN("BAD_SIGN_OFF",
22753705ce5bSJoe Perches					 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
22763705ce5bSJoe Perches				    $fix) {
2277194f66fcSJoe Perches					$fixed[$fixlinenr] =
22783705ce5bSJoe Perches					    "$ucfirst_sign_off $email";
22793705ce5bSJoe Perches				}
22803705ce5bSJoe Perches
228120112475SJoe Perches			}
228220112475SJoe Perches			if (!defined $space_after || $space_after ne " ") {
22833705ce5bSJoe Perches				if (WARN("BAD_SIGN_OFF",
22843705ce5bSJoe Perches					 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
22853705ce5bSJoe Perches				    $fix) {
2286194f66fcSJoe Perches					$fixed[$fixlinenr] =
22873705ce5bSJoe Perches					    "$ucfirst_sign_off $email";
22883705ce5bSJoe Perches				}
228920112475SJoe Perches			}
229020112475SJoe Perches
229120112475SJoe Perches			my ($email_name, $email_address, $comment) = parse_email($email);
229220112475SJoe Perches			my $suggested_email = format_email(($email_name, $email_address));
229320112475SJoe Perches			if ($suggested_email eq "") {
2294000d1cc1SJoe Perches				ERROR("BAD_SIGN_OFF",
2295000d1cc1SJoe Perches				      "Unrecognized email address: '$email'\n" . $herecurr);
229620112475SJoe Perches			} else {
229720112475SJoe Perches				my $dequoted = $suggested_email;
229820112475SJoe Perches				$dequoted =~ s/^"//;
229920112475SJoe Perches				$dequoted =~ s/" </ </;
230020112475SJoe Perches				# Don't force email to have quotes
230120112475SJoe Perches				# Allow just an angle bracketed address
230220112475SJoe Perches				if ("$dequoted$comment" ne $email &&
230320112475SJoe Perches				    "<$email_address>$comment" ne $email &&
230420112475SJoe Perches				    "$suggested_email$comment" ne $email) {
2305000d1cc1SJoe Perches					WARN("BAD_SIGN_OFF",
2306000d1cc1SJoe Perches					     "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
230720112475SJoe Perches				}
23080a920b5bSAndy Whitcroft			}
23097e51f197SJoe Perches
23107e51f197SJoe Perches# Check for duplicate signatures
23117e51f197SJoe Perches			my $sig_nospace = $line;
23127e51f197SJoe Perches			$sig_nospace =~ s/\s//g;
23137e51f197SJoe Perches			$sig_nospace = lc($sig_nospace);
23147e51f197SJoe Perches			if (defined $signatures{$sig_nospace}) {
23157e51f197SJoe Perches				WARN("BAD_SIGN_OFF",
23167e51f197SJoe Perches				     "Duplicate signature\n" . $herecurr);
23177e51f197SJoe Perches			} else {
23187e51f197SJoe Perches				$signatures{$sig_nospace} = 1;
23197e51f197SJoe Perches			}
23200a920b5bSAndy Whitcroft		}
23210a920b5bSAndy Whitcroft
2322a2fe16b9SJoe Perches# Check email subject for common tools that don't need to be mentioned
2323a2fe16b9SJoe Perches		if ($in_header_lines &&
2324a2fe16b9SJoe Perches		    $line =~ /^Subject:.*\b(?:checkpatch|sparse|smatch)\b[^:]/i) {
2325a2fe16b9SJoe Perches			WARN("EMAIL_SUBJECT",
2326a2fe16b9SJoe Perches			     "A patch subject line should describe the change not the tool that found it\n" . $herecurr);
2327a2fe16b9SJoe Perches		}
2328a2fe16b9SJoe Perches
23299b3189ebSJoe Perches# Check for old stable address
23309b3189ebSJoe Perches		if ($line =~ /^\s*cc:\s*.*<?\bstable\@kernel\.org\b>?.*$/i) {
23319b3189ebSJoe Perches			ERROR("STABLE_ADDRESS",
23329b3189ebSJoe Perches			      "The 'stable' address should be 'stable\@vger.kernel.org'\n" . $herecurr);
23339b3189ebSJoe Perches		}
23349b3189ebSJoe Perches
23357ebd05efSChristopher Covington# Check for unwanted Gerrit info
23367ebd05efSChristopher Covington		if ($in_commit_log && $line =~ /^\s*change-id:/i) {
23377ebd05efSChristopher Covington			ERROR("GERRIT_CHANGE_ID",
23387ebd05efSChristopher Covington			      "Remove Gerrit Change-Id's before submitting upstream.\n" . $herecurr);
23397ebd05efSChristopher Covington		}
23407ebd05efSChristopher Covington
2341369c8dd3SJoe Perches# Check if the commit log is in a possible stack dump
2342369c8dd3SJoe Perches		if ($in_commit_log && !$commit_log_possible_stack_dump &&
2343369c8dd3SJoe Perches		    ($line =~ /^\s*(?:WARNING:|BUG:)/ ||
2344369c8dd3SJoe Perches		     $line =~ /^\s*\[\s*\d+\.\d{6,6}\s*\]/ ||
2345369c8dd3SJoe Perches					# timestamp
2346369c8dd3SJoe Perches		     $line =~ /^\s*\[\<[0-9a-fA-F]{8,}\>\]/)) {
2347369c8dd3SJoe Perches					# stack dump address
2348369c8dd3SJoe Perches			$commit_log_possible_stack_dump = 1;
2349369c8dd3SJoe Perches		}
2350369c8dd3SJoe Perches
23512a076f40SJoe Perches# Check for line lengths > 75 in commit log, warn once
23522a076f40SJoe Perches		if ($in_commit_log && !$commit_log_long_line &&
2353bf4daf12SJoe Perches		    length($line) > 75 &&
2354bf4daf12SJoe Perches		    !($line =~ /^\s*[a-zA-Z0-9_\/\.]+\s+\|\s+\d+/ ||
2355bf4daf12SJoe Perches					# file delta changes
2356bf4daf12SJoe Perches		      $line =~ /^\s*(?:[\w\.\-]+\/)++[\w\.\-]+:/ ||
2357bf4daf12SJoe Perches					# filename then :
2358bf4daf12SJoe Perches		      $line =~ /^\s*(?:Fixes:|Link:)/i ||
2359bf4daf12SJoe Perches					# A Fixes: or Link: line
2360bf4daf12SJoe Perches		      $commit_log_possible_stack_dump)) {
23612a076f40SJoe Perches			WARN("COMMIT_LOG_LONG_LINE",
23622a076f40SJoe Perches			     "Possible unwrapped commit description (prefer a maximum 75 chars per line)\n" . $herecurr);
23632a076f40SJoe Perches			$commit_log_long_line = 1;
23642a076f40SJoe Perches		}
23652a076f40SJoe Perches
2366bf4daf12SJoe Perches# Reset possible stack dump if a blank line is found
2367bf4daf12SJoe Perches		if ($in_commit_log && $commit_log_possible_stack_dump &&
2368bf4daf12SJoe Perches		    $line =~ /^\s*$/) {
2369bf4daf12SJoe Perches			$commit_log_possible_stack_dump = 0;
2370bf4daf12SJoe Perches		}
2371bf4daf12SJoe Perches
23720d7835fcSJoe Perches# Check for git id commit length and improperly formed commit descriptions
2373369c8dd3SJoe Perches		if ($in_commit_log && !$commit_log_possible_stack_dump &&
2374fe043ea1SJoe Perches		    ($line =~ /\bcommit\s+[0-9a-f]{5,}\b/i ||
2375bf4daf12SJoe Perches		     ($line =~ /\b[0-9a-f]{12,40}\b/i &&
2376369c8dd3SJoe Perches		      $line !~ /[\<\[][0-9a-f]{12,40}[\>\]]/i &&
2377bf4daf12SJoe Perches		      $line !~ /\bfixes:\s*[0-9a-f]{12,40}/i))) {
2378fe043ea1SJoe Perches			my $init_char = "c";
2379fe043ea1SJoe Perches			my $orig_commit = "";
23800d7835fcSJoe Perches			my $short = 1;
23810d7835fcSJoe Perches			my $long = 0;
23820d7835fcSJoe Perches			my $case = 1;
23830d7835fcSJoe Perches			my $space = 1;
23840d7835fcSJoe Perches			my $hasdesc = 0;
238519c146a6SJoe Perches			my $hasparens = 0;
23860d7835fcSJoe Perches			my $id = '0123456789ab';
23870d7835fcSJoe Perches			my $orig_desc = "commit description";
23880d7835fcSJoe Perches			my $description = "";
23890d7835fcSJoe Perches
2390fe043ea1SJoe Perches			if ($line =~ /\b(c)ommit\s+([0-9a-f]{5,})\b/i) {
2391fe043ea1SJoe Perches				$init_char = $1;
2392fe043ea1SJoe Perches				$orig_commit = lc($2);
2393fe043ea1SJoe Perches			} elsif ($line =~ /\b([0-9a-f]{12,40})\b/i) {
2394fe043ea1SJoe Perches				$orig_commit = lc($1);
2395fe043ea1SJoe Perches			}
2396fe043ea1SJoe Perches
23970d7835fcSJoe Perches			$short = 0 if ($line =~ /\bcommit\s+[0-9a-f]{12,40}/i);
23980d7835fcSJoe Perches			$long = 1 if ($line =~ /\bcommit\s+[0-9a-f]{41,}/i);
23990d7835fcSJoe Perches			$space = 0 if ($line =~ /\bcommit [0-9a-f]/i);
24000d7835fcSJoe Perches			$case = 0 if ($line =~ /\b[Cc]ommit\s+[0-9a-f]{5,40}[^A-F]/);
24010d7835fcSJoe Perches			if ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)"\)/i) {
24020d7835fcSJoe Perches				$orig_desc = $1;
240319c146a6SJoe Perches				$hasparens = 1;
24040d7835fcSJoe Perches			} elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s*$/i &&
24050d7835fcSJoe Perches				 defined $rawlines[$linenr] &&
24060d7835fcSJoe Perches				 $rawlines[$linenr] =~ /^\s*\("([^"]+)"\)/) {
24070d7835fcSJoe Perches				$orig_desc = $1;
240819c146a6SJoe Perches				$hasparens = 1;
2409b671fde0SJoe Perches			} elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("[^"]+$/i &&
2410b671fde0SJoe Perches				 defined $rawlines[$linenr] &&
2411b671fde0SJoe Perches				 $rawlines[$linenr] =~ /^\s*[^"]+"\)/) {
2412b671fde0SJoe Perches				$line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)$/i;
2413b671fde0SJoe Perches				$orig_desc = $1;
2414b671fde0SJoe Perches				$rawlines[$linenr] =~ /^\s*([^"]+)"\)/;
2415b671fde0SJoe Perches				$orig_desc .= " " . $1;
241619c146a6SJoe Perches				$hasparens = 1;
24170d7835fcSJoe Perches			}
24180d7835fcSJoe Perches
24190d7835fcSJoe Perches			($id, $description) = git_commit_info($orig_commit,
24200d7835fcSJoe Perches							      $id, $orig_desc);
24210d7835fcSJoe Perches
242219c146a6SJoe Perches			if ($short || $long || $space || $case || ($orig_desc ne $description) || !$hasparens) {
2423d311cd44SJoe Perches				ERROR("GIT_COMMIT_ID",
24240d7835fcSJoe Perches				      "Please use git commit description style 'commit <12+ chars of sha1> (\"<title line>\")' - ie: '${init_char}ommit $id (\"$description\")'\n" . $herecurr);
24250d7835fcSJoe Perches			}
2426d311cd44SJoe Perches		}
2427d311cd44SJoe Perches
242813f1937eSJoe Perches# Check for added, moved or deleted files
242913f1937eSJoe Perches		if (!$reported_maintainer_file && !$in_commit_log &&
243013f1937eSJoe Perches		    ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ ||
243113f1937eSJoe Perches		     $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ ||
243213f1937eSJoe Perches		     ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ &&
243313f1937eSJoe Perches		      (defined($1) || defined($2))))) {
243413f1937eSJoe Perches			$reported_maintainer_file = 1;
243513f1937eSJoe Perches			WARN("FILE_PATH_CHANGES",
243613f1937eSJoe Perches			     "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr);
243713f1937eSJoe Perches		}
243813f1937eSJoe Perches
243900df344fSAndy Whitcroft# Check for wrappage within a valid hunk of the file
24408905a67cSAndy Whitcroft		if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
2441000d1cc1SJoe Perches			ERROR("CORRUPTED_PATCH",
2442000d1cc1SJoe Perches			      "patch seems to be corrupt (line wrapped?)\n" .
24436c72ffaaSAndy Whitcroft				$herecurr) if (!$emitted_corrupt++);
2444de7d4f0eSAndy Whitcroft		}
2445de7d4f0eSAndy Whitcroft
24466ecd9674SAndy Whitcroft# Check for absolute kernel paths.
24476ecd9674SAndy Whitcroft		if ($tree) {
24486ecd9674SAndy Whitcroft			while ($line =~ m{(?:^|\s)(/\S*)}g) {
24496ecd9674SAndy Whitcroft				my $file = $1;
24506ecd9674SAndy Whitcroft
24516ecd9674SAndy Whitcroft				if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
24526ecd9674SAndy Whitcroft				    check_absolute_file($1, $herecurr)) {
24536ecd9674SAndy Whitcroft					#
24546ecd9674SAndy Whitcroft				} else {
24556ecd9674SAndy Whitcroft					check_absolute_file($file, $herecurr);
24566ecd9674SAndy Whitcroft				}
24576ecd9674SAndy Whitcroft			}
24586ecd9674SAndy Whitcroft		}
24596ecd9674SAndy Whitcroft
2460de7d4f0eSAndy Whitcroft# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
2461de7d4f0eSAndy Whitcroft		if (($realfile =~ /^$/ || $line =~ /^\+/) &&
2462171ae1a4SAndy Whitcroft		    $rawline !~ m/^$UTF8*$/) {
2463171ae1a4SAndy Whitcroft			my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
2464171ae1a4SAndy Whitcroft
2465171ae1a4SAndy Whitcroft			my $blank = copy_spacing($rawline);
2466171ae1a4SAndy Whitcroft			my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
2467171ae1a4SAndy Whitcroft			my $hereptr = "$hereline$ptr\n";
2468171ae1a4SAndy Whitcroft
246934d99219SJoe Perches			CHK("INVALID_UTF8",
2470000d1cc1SJoe Perches			    "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
247100df344fSAndy Whitcroft		}
24720a920b5bSAndy Whitcroft
247315662b3eSJoe Perches# Check if it's the start of a commit log
247415662b3eSJoe Perches# (not a header line and we haven't seen the patch filename)
247515662b3eSJoe Perches		if ($in_header_lines && $realfile =~ /^$/ &&
247629ee1b0cSJoe Perches		    !($rawline =~ /^\s+\S/ ||
247729ee1b0cSJoe Perches		      $rawline =~ /^(commit\b|from\b|[\w-]+:).*$/i)) {
247815662b3eSJoe Perches			$in_header_lines = 0;
247915662b3eSJoe Perches			$in_commit_log = 1;
248015662b3eSJoe Perches		}
248115662b3eSJoe Perches
2482fa64205dSPasi Savanainen# Check if there is UTF-8 in a commit log when a mail header has explicitly
2483fa64205dSPasi Savanainen# declined it, i.e defined some charset where it is missing.
2484fa64205dSPasi Savanainen		if ($in_header_lines &&
2485fa64205dSPasi Savanainen		    $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
2486fa64205dSPasi Savanainen		    $1 !~ /utf-8/i) {
2487fa64205dSPasi Savanainen			$non_utf8_charset = 1;
2488fa64205dSPasi Savanainen		}
2489fa64205dSPasi Savanainen
2490fa64205dSPasi Savanainen		if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
249115662b3eSJoe Perches		    $rawline =~ /$NON_ASCII_UTF8/) {
2492fa64205dSPasi Savanainen			WARN("UTF8_BEFORE_PATCH",
249315662b3eSJoe Perches			    "8-bit UTF-8 used in possible commit log\n" . $herecurr);
249415662b3eSJoe Perches		}
249515662b3eSJoe Perches
249666b47b4aSKees Cook# Check for various typo / spelling mistakes
249766d7a382SJoe Perches		if (defined($misspellings) &&
249866d7a382SJoe Perches		    ($in_commit_log || $line =~ /^(?:\+|Subject:)/i)) {
2499ebfd7d62SJoe Perches			while ($rawline =~ /(?:^|[^a-z@])($misspellings)(?:\b|$|[^a-z@])/gi) {
250066b47b4aSKees Cook				my $typo = $1;
250166b47b4aSKees Cook				my $typo_fix = $spelling_fix{lc($typo)};
250266b47b4aSKees Cook				$typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/);
250366b47b4aSKees Cook				$typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/);
250466b47b4aSKees Cook				my $msg_type = \&WARN;
250566b47b4aSKees Cook				$msg_type = \&CHK if ($file);
250666b47b4aSKees Cook				if (&{$msg_type}("TYPO_SPELLING",
250766b47b4aSKees Cook						 "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $herecurr) &&
250866b47b4aSKees Cook				    $fix) {
250966b47b4aSKees Cook					$fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/;
251066b47b4aSKees Cook				}
251166b47b4aSKees Cook			}
251266b47b4aSKees Cook		}
251366b47b4aSKees Cook
251430670854SAndy Whitcroft# ignore non-hunk lines and lines being removed
251530670854SAndy Whitcroft		next if (!$hunk_line || $line =~ /^-/);
251600df344fSAndy Whitcroft
25170a920b5bSAndy Whitcroft#trailing whitespace
25189c0ca6f9SAndy Whitcroft		if ($line =~ /^\+.*\015/) {
2519c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2520d5e616fcSJoe Perches			if (ERROR("DOS_LINE_ENDINGS",
2521d5e616fcSJoe Perches				  "DOS line endings\n" . $herevet) &&
2522d5e616fcSJoe Perches			    $fix) {
2523194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/[\s\015]+$//;
2524d5e616fcSJoe Perches			}
2525c2fdda0dSAndy Whitcroft		} elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
2526c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
25273705ce5bSJoe Perches			if (ERROR("TRAILING_WHITESPACE",
25283705ce5bSJoe Perches				  "trailing whitespace\n" . $herevet) &&
25293705ce5bSJoe Perches			    $fix) {
2530194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\s+$//;
25313705ce5bSJoe Perches			}
25323705ce5bSJoe Perches
2533d2c0a235SAndy Whitcroft			$rpt_cleaners = 1;
25340a920b5bSAndy Whitcroft		}
25355368df20SAndy Whitcroft
25364783f894SJosh Triplett# Check for FSF mailing addresses.
2537109d8cb2SAlexander Duyck		if ($rawline =~ /\bwrite to the Free/i ||
25383e2232f2SJoe Perches		    $rawline =~ /\b59\s+Temple\s+Pl/i ||
25393e2232f2SJoe Perches		    $rawline =~ /\b51\s+Franklin\s+St/i) {
25404783f894SJosh Triplett			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
25414783f894SJosh Triplett			my $msg_type = \&ERROR;
25424783f894SJosh Triplett			$msg_type = \&CHK if ($file);
25434783f894SJosh Triplett			&{$msg_type}("FSF_MAILING_ADDRESS",
25444783f894SJosh 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)
25454783f894SJosh Triplett		}
25464783f894SJosh Triplett
25473354957aSAndi Kleen# check for Kconfig help text having a real description
25489fe287d7SAndy Whitcroft# Only applies when adding the entry originally, after that we do not have
25499fe287d7SAndy Whitcroft# sufficient context to determine whether it is indeed long enough.
25503354957aSAndi Kleen		if ($realfile =~ /Kconfig/ &&
25518d73e0e7SJoe Perches		    $line =~ /^\+\s*config\s+/) {
25523354957aSAndi Kleen			my $length = 0;
25539fe287d7SAndy Whitcroft			my $cnt = $realcnt;
25549fe287d7SAndy Whitcroft			my $ln = $linenr + 1;
25559fe287d7SAndy Whitcroft			my $f;
2556a1385803SAndy Whitcroft			my $is_start = 0;
25579fe287d7SAndy Whitcroft			my $is_end = 0;
2558a1385803SAndy Whitcroft			for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
25599fe287d7SAndy Whitcroft				$f = $lines[$ln - 1];
25609fe287d7SAndy Whitcroft				$cnt-- if ($lines[$ln - 1] !~ /^-/);
25619fe287d7SAndy Whitcroft				$is_end = $lines[$ln - 1] =~ /^\+/;
25629fe287d7SAndy Whitcroft
25639fe287d7SAndy Whitcroft				next if ($f =~ /^-/);
25648d73e0e7SJoe Perches				last if (!$file && $f =~ /^\@\@/);
2565a1385803SAndy Whitcroft
25668d73e0e7SJoe Perches				if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate)\s*\"/) {
2567a1385803SAndy Whitcroft					$is_start = 1;
25688d73e0e7SJoe Perches				} elsif ($lines[$ln - 1] =~ /^\+\s*(?:---)?help(?:---)?$/) {
2569a1385803SAndy Whitcroft					$length = -1;
2570a1385803SAndy Whitcroft				}
2571a1385803SAndy Whitcroft
25729fe287d7SAndy Whitcroft				$f =~ s/^.//;
25733354957aSAndi Kleen				$f =~ s/#.*//;
25743354957aSAndi Kleen				$f =~ s/^\s+//;
25753354957aSAndi Kleen				next if ($f =~ /^$/);
25769fe287d7SAndy Whitcroft				if ($f =~ /^\s*config\s/) {
25779fe287d7SAndy Whitcroft					$is_end = 1;
25789fe287d7SAndy Whitcroft					last;
25799fe287d7SAndy Whitcroft				}
25803354957aSAndi Kleen				$length++;
25813354957aSAndi Kleen			}
258256193274SVadim Bendebury			if ($is_start && $is_end && $length < $min_conf_desc_length) {
2583000d1cc1SJoe Perches				WARN("CONFIG_DESCRIPTION",
258456193274SVadim Bendebury				     "please write a paragraph that describes the config symbol fully\n" . $herecurr);
258556193274SVadim Bendebury			}
2586a1385803SAndy Whitcroft			#print "is_start<$is_start> is_end<$is_end> length<$length>\n";
25873354957aSAndi Kleen		}
25883354957aSAndi Kleen
25891ba8dfd1SKees Cook# discourage the addition of CONFIG_EXPERIMENTAL in Kconfig.
25901ba8dfd1SKees Cook		if ($realfile =~ /Kconfig/ &&
25911ba8dfd1SKees Cook		    $line =~ /.\s*depends on\s+.*\bEXPERIMENTAL\b/) {
25921ba8dfd1SKees Cook			WARN("CONFIG_EXPERIMENTAL",
25931ba8dfd1SKees Cook			     "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
25941ba8dfd1SKees Cook		}
25951ba8dfd1SKees Cook
2596327953e9SChristoph Jaeger# discourage the use of boolean for type definition attributes of Kconfig options
2597327953e9SChristoph Jaeger		if ($realfile =~ /Kconfig/ &&
2598327953e9SChristoph Jaeger		    $line =~ /^\+\s*\bboolean\b/) {
2599327953e9SChristoph Jaeger			WARN("CONFIG_TYPE_BOOLEAN",
2600327953e9SChristoph Jaeger			     "Use of boolean is deprecated, please use bool instead.\n" . $herecurr);
2601327953e9SChristoph Jaeger		}
2602327953e9SChristoph Jaeger
2603c68e5878SArnaud Lacombe		if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
2604c68e5878SArnaud Lacombe		    ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
2605c68e5878SArnaud Lacombe			my $flag = $1;
2606c68e5878SArnaud Lacombe			my $replacement = {
2607c68e5878SArnaud Lacombe				'EXTRA_AFLAGS' =>   'asflags-y',
2608c68e5878SArnaud Lacombe				'EXTRA_CFLAGS' =>   'ccflags-y',
2609c68e5878SArnaud Lacombe				'EXTRA_CPPFLAGS' => 'cppflags-y',
2610c68e5878SArnaud Lacombe				'EXTRA_LDFLAGS' =>  'ldflags-y',
2611c68e5878SArnaud Lacombe			};
2612c68e5878SArnaud Lacombe
2613c68e5878SArnaud Lacombe			WARN("DEPRECATED_VARIABLE",
2614c68e5878SArnaud Lacombe			     "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
2615c68e5878SArnaud Lacombe		}
2616c68e5878SArnaud Lacombe
2617bff5da43SRob Herring# check for DT compatible documentation
26187dd05b38SFlorian Vaussard		if (defined $root &&
26197dd05b38SFlorian Vaussard			(($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) ||
26207dd05b38SFlorian Vaussard			 ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) {
26217dd05b38SFlorian Vaussard
2622bff5da43SRob Herring			my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g;
2623bff5da43SRob Herring
2624cc93319bSFlorian Vaussard			my $dt_path = $root . "/Documentation/devicetree/bindings/";
2625cc93319bSFlorian Vaussard			my $vp_file = $dt_path . "vendor-prefixes.txt";
2626cc93319bSFlorian Vaussard
2627bff5da43SRob Herring			foreach my $compat (@compats) {
2628bff5da43SRob Herring				my $compat2 = $compat;
2629185d566bSRob Herring				$compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/;
2630185d566bSRob Herring				my $compat3 = $compat;
2631185d566bSRob Herring				$compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/;
2632185d566bSRob Herring				`grep -Erq "$compat|$compat2|$compat3" $dt_path`;
2633bff5da43SRob Herring				if ( $? >> 8 ) {
2634bff5da43SRob Herring					WARN("UNDOCUMENTED_DT_STRING",
2635bff5da43SRob Herring					     "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr);
2636bff5da43SRob Herring				}
2637bff5da43SRob Herring
26384fbf32a6SFlorian Vaussard				next if $compat !~ /^([a-zA-Z0-9\-]+)\,/;
26394fbf32a6SFlorian Vaussard				my $vendor = $1;
2640cc93319bSFlorian Vaussard				`grep -Eq "^$vendor\\b" $vp_file`;
2641bff5da43SRob Herring				if ( $? >> 8 ) {
2642bff5da43SRob Herring					WARN("UNDOCUMENTED_DT_STRING",
2643cc93319bSFlorian Vaussard					     "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr);
2644bff5da43SRob Herring				}
2645bff5da43SRob Herring			}
2646bff5da43SRob Herring		}
2647bff5da43SRob Herring
26485368df20SAndy Whitcroft# check we are in a valid source file if not then ignore this hunk
2649de4c924cSGeert Uytterhoeven		next if ($realfile !~ /\.(h|c|s|S|pl|sh|dtsi|dts)$/);
26505368df20SAndy Whitcroft
265147e0c88bSJoe Perches# line length limit (with some exclusions)
265247e0c88bSJoe Perches#
265347e0c88bSJoe Perches# There are a few types of lines that may extend beyond $max_line_length:
265447e0c88bSJoe Perches#	logging functions like pr_info that end in a string
265547e0c88bSJoe Perches#	lines with a single string
265647e0c88bSJoe Perches#	#defines that are a single string
265747e0c88bSJoe Perches#
265847e0c88bSJoe Perches# There are 3 different line length message types:
265947e0c88bSJoe Perches# LONG_LINE_COMMENT	a comment starts before but extends beyond $max_linelength
266047e0c88bSJoe Perches# LONG_LINE_STRING	a string starts before but extends beyond $max_line_length
266147e0c88bSJoe Perches# LONG_LINE		all other lines longer than $max_line_length
266247e0c88bSJoe Perches#
266347e0c88bSJoe Perches# if LONG_LINE is ignored, the other 2 types are also ignored
266447e0c88bSJoe Perches#
266547e0c88bSJoe Perches
2666b4749e96SJoe Perches		if ($line =~ /^\+/ && $length > $max_line_length) {
266747e0c88bSJoe Perches			my $msg_type = "LONG_LINE";
266847e0c88bSJoe Perches
266947e0c88bSJoe Perches			# Check the allowed long line types first
267047e0c88bSJoe Perches
267147e0c88bSJoe Perches			# logging functions that end in a string that starts
267247e0c88bSJoe Perches			# before $max_line_length
267347e0c88bSJoe Perches			if ($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(?:KERN_\S+\s*|[^"]*))?($String\s*(?:|,|\)\s*;)\s*)$/ &&
267447e0c88bSJoe Perches			    length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
267547e0c88bSJoe Perches				$msg_type = "";
267647e0c88bSJoe Perches
267747e0c88bSJoe Perches			# lines with only strings (w/ possible termination)
267847e0c88bSJoe Perches			# #defines with only strings
267947e0c88bSJoe Perches			} elsif ($line =~ /^\+\s*$String\s*(?:\s*|,|\)\s*;)\s*$/ ||
268047e0c88bSJoe Perches				 $line =~ /^\+\s*#\s*define\s+\w+\s+$String$/) {
268147e0c88bSJoe Perches				$msg_type = "";
268247e0c88bSJoe Perches
268347e0c88bSJoe Perches			# Otherwise set the alternate message types
268447e0c88bSJoe Perches
268547e0c88bSJoe Perches			# a comment starts before $max_line_length
268647e0c88bSJoe Perches			} elsif ($line =~ /($;[\s$;]*)$/ &&
268747e0c88bSJoe Perches				 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
268847e0c88bSJoe Perches				$msg_type = "LONG_LINE_COMMENT"
268947e0c88bSJoe Perches
269047e0c88bSJoe Perches			# a quoted string starts before $max_line_length
269147e0c88bSJoe Perches			} elsif ($sline =~ /\s*($String(?:\s*(?:\\|,\s*|\)\s*;\s*))?)$/ &&
269247e0c88bSJoe Perches				 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
269347e0c88bSJoe Perches				$msg_type = "LONG_LINE_STRING"
269447e0c88bSJoe Perches			}
269547e0c88bSJoe Perches
269647e0c88bSJoe Perches			if ($msg_type ne "" &&
269747e0c88bSJoe Perches			    (show_type("LONG_LINE") || show_type($msg_type))) {
269847e0c88bSJoe Perches				WARN($msg_type,
26996cd7f386SJoe Perches				     "line over $max_line_length characters\n" . $herecurr);
27000a920b5bSAndy Whitcroft			}
270147e0c88bSJoe Perches		}
27020a920b5bSAndy Whitcroft
27038905a67cSAndy Whitcroft# check for adding lines without a newline.
27048905a67cSAndy Whitcroft		if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
2705000d1cc1SJoe Perches			WARN("MISSING_EOF_NEWLINE",
2706000d1cc1SJoe Perches			     "adding a line without newline at end of file\n" . $herecurr);
27078905a67cSAndy Whitcroft		}
27088905a67cSAndy Whitcroft
270942e41c54SMike Frysinger# Blackfin: use hi/lo macros
271042e41c54SMike Frysinger		if ($realfile =~ m@arch/blackfin/.*\.S$@) {
271142e41c54SMike Frysinger			if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
271242e41c54SMike Frysinger				my $herevet = "$here\n" . cat_vet($line) . "\n";
2713000d1cc1SJoe Perches				ERROR("LO_MACRO",
2714000d1cc1SJoe Perches				      "use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
271542e41c54SMike Frysinger			}
271642e41c54SMike Frysinger			if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
271742e41c54SMike Frysinger				my $herevet = "$here\n" . cat_vet($line) . "\n";
2718000d1cc1SJoe Perches				ERROR("HI_MACRO",
2719000d1cc1SJoe Perches				      "use the HI() macro, not (... >> 16)\n" . $herevet);
272042e41c54SMike Frysinger			}
272142e41c54SMike Frysinger		}
272242e41c54SMike Frysinger
2723b9ea10d6SAndy Whitcroft# check we are in a valid source file C or perl if not then ignore this hunk
2724de4c924cSGeert Uytterhoeven		next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/);
27250a920b5bSAndy Whitcroft
27260a920b5bSAndy Whitcroft# at the beginning of a line any tabs must come first and anything
27270a920b5bSAndy Whitcroft# more than 8 must use tabs.
2728c2fdda0dSAndy Whitcroft		if ($rawline =~ /^\+\s* \t\s*\S/ ||
2729c2fdda0dSAndy Whitcroft		    $rawline =~ /^\+\s*        \s*/) {
2730c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2731d2c0a235SAndy Whitcroft			$rpt_cleaners = 1;
27323705ce5bSJoe Perches			if (ERROR("CODE_INDENT",
27333705ce5bSJoe Perches				  "code indent should use tabs where possible\n" . $herevet) &&
27343705ce5bSJoe Perches			    $fix) {
2735194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
27363705ce5bSJoe Perches			}
27370a920b5bSAndy Whitcroft		}
27380a920b5bSAndy Whitcroft
273908e44365SAlberto Panizzo# check for space before tabs.
274008e44365SAlberto Panizzo		if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
274108e44365SAlberto Panizzo			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
27423705ce5bSJoe Perches			if (WARN("SPACE_BEFORE_TAB",
27433705ce5bSJoe Perches				"please, no space before tabs\n" . $herevet) &&
27443705ce5bSJoe Perches			    $fix) {
2745194f66fcSJoe Perches				while ($fixed[$fixlinenr] =~
2746d2207ccbSJoe Perches					   s/(^\+.*) {8,8}\t/$1\t\t/) {}
2747194f66fcSJoe Perches				while ($fixed[$fixlinenr] =~
2748c76f4cb3SJoe Perches					   s/(^\+.*) +\t/$1\t/) {}
27493705ce5bSJoe Perches			}
275008e44365SAlberto Panizzo		}
275108e44365SAlberto Panizzo
2752d1fe9c09SJoe Perches# check for && or || at the start of a line
2753d1fe9c09SJoe Perches		if ($rawline =~ /^\+\s*(&&|\|\|)/) {
2754d1fe9c09SJoe Perches			CHK("LOGICAL_CONTINUATIONS",
2755d1fe9c09SJoe Perches			    "Logical continuations should be on the previous line\n" . $hereprev);
2756d1fe9c09SJoe Perches		}
2757d1fe9c09SJoe Perches
2758a91e8994SJoe Perches# check indentation starts on a tab stop
2759a91e8994SJoe Perches		if ($^V && $^V ge 5.10.0 &&
2760a91e8994SJoe Perches		    $sline =~ /^\+\t+( +)(?:$c90_Keywords\b|\{\s*$|\}\s*(?:else\b|while\b|\s*$))/) {
2761a91e8994SJoe Perches			my $indent = length($1);
2762a91e8994SJoe Perches			if ($indent % 8) {
2763a91e8994SJoe Perches				if (WARN("TABSTOP",
2764a91e8994SJoe Perches					 "Statements should start on a tabstop\n" . $herecurr) &&
2765a91e8994SJoe Perches				    $fix) {
2766a91e8994SJoe Perches					$fixed[$fixlinenr] =~ s@(^\+\t+) +@$1 . "\t" x ($indent/8)@e;
2767a91e8994SJoe Perches				}
2768a91e8994SJoe Perches			}
2769a91e8994SJoe Perches		}
2770a91e8994SJoe Perches
2771d1fe9c09SJoe Perches# check multi-line statement indentation matches previous line
2772d1fe9c09SJoe Perches		if ($^V && $^V ge 5.10.0 &&
277391cb5195SJoe Perches		    $prevline =~ /^\+([ \t]*)((?:$c90_Keywords(?:\s+if)\s*)|(?:$Declare\s*)?(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*|$Ident\s*=\s*$Ident\s*)\(.*(\&\&|\|\||,)\s*$/) {
2774d1fe9c09SJoe Perches			$prevline =~ /^\+(\t*)(.*)$/;
2775d1fe9c09SJoe Perches			my $oldindent = $1;
2776d1fe9c09SJoe Perches			my $rest = $2;
2777d1fe9c09SJoe Perches
2778d1fe9c09SJoe Perches			my $pos = pos_last_openparen($rest);
2779d1fe9c09SJoe Perches			if ($pos >= 0) {
2780b34a26f3SJoe Perches				$line =~ /^(\+| )([ \t]*)/;
2781b34a26f3SJoe Perches				my $newindent = $2;
2782d1fe9c09SJoe Perches
2783d1fe9c09SJoe Perches				my $goodtabindent = $oldindent .
2784d1fe9c09SJoe Perches					"\t" x ($pos / 8) .
2785d1fe9c09SJoe Perches					" "  x ($pos % 8);
2786d1fe9c09SJoe Perches				my $goodspaceindent = $oldindent . " "  x $pos;
2787d1fe9c09SJoe Perches
2788d1fe9c09SJoe Perches				if ($newindent ne $goodtabindent &&
2789d1fe9c09SJoe Perches				    $newindent ne $goodspaceindent) {
27903705ce5bSJoe Perches
27913705ce5bSJoe Perches					if (CHK("PARENTHESIS_ALIGNMENT",
27923705ce5bSJoe Perches						"Alignment should match open parenthesis\n" . $hereprev) &&
27933705ce5bSJoe Perches					    $fix && $line =~ /^\+/) {
2794194f66fcSJoe Perches						$fixed[$fixlinenr] =~
27953705ce5bSJoe Perches						    s/^\+[ \t]*/\+$goodtabindent/;
27963705ce5bSJoe Perches					}
2797d1fe9c09SJoe Perches				}
2798d1fe9c09SJoe Perches			}
2799d1fe9c09SJoe Perches		}
2800d1fe9c09SJoe Perches
28016ab3a970SJoe Perches# check for space after cast like "(int) foo" or "(struct foo) bar"
28026ab3a970SJoe Perches# avoid checking a few false positives:
28036ab3a970SJoe Perches#   "sizeof(<type>)" or "__alignof__(<type>)"
28046ab3a970SJoe Perches#   function pointer declarations like "(*foo)(int) = bar;"
28056ab3a970SJoe Perches#   structure definitions like "(struct foo) { 0 };"
28066ab3a970SJoe Perches#   multiline macros that define functions
28076ab3a970SJoe Perches#   known attributes or the __attribute__ keyword
28086ab3a970SJoe Perches		if ($line =~ /^\+(.*)\(\s*$Type\s*\)([ \t]++)((?![={]|\\$|$Attribute|__attribute__))/ &&
28096ab3a970SJoe Perches		    (!defined($1) || $1 !~ /\b(?:sizeof|__alignof__)\s*$/)) {
28103705ce5bSJoe Perches			if (CHK("SPACING",
2811f27c95dbSJoe Perches				"No space is necessary after a cast\n" . $herecurr) &&
28123705ce5bSJoe Perches			    $fix) {
2813194f66fcSJoe Perches				$fixed[$fixlinenr] =~
2814f27c95dbSJoe Perches				    s/(\(\s*$Type\s*\))[ \t]+/$1/;
28153705ce5bSJoe Perches			}
2816aad4f614SJoe Perches		}
2817aad4f614SJoe Perches
281886406b1cSJoe Perches# Block comment styles
281986406b1cSJoe Perches# Networking with an initial /*
282005880600SJoe Perches		if ($realfile =~ m@^(drivers/net/|net/)@ &&
2821fdb4bcd6SJoe Perches		    $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
282285ad978cSJoe Perches		    $rawline =~ /^\+[ \t]*\*/ &&
282385ad978cSJoe Perches		    $realline > 2) {
282405880600SJoe Perches			WARN("NETWORKING_BLOCK_COMMENT_STYLE",
282505880600SJoe Perches			     "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
282605880600SJoe Perches		}
282705880600SJoe Perches
282886406b1cSJoe Perches# Block comments use * on subsequent lines
282986406b1cSJoe Perches		if ($prevline =~ /$;[ \t]*$/ &&			#ends in comment
283086406b1cSJoe Perches		    $prevrawline =~ /^\+.*?\/\*/ &&		#starting /*
2831a605e32eSJoe Perches		    $prevrawline !~ /\*\/[ \t]*$/ &&		#no trailing */
283261135e96SJoe Perches		    $rawline =~ /^\+/ &&			#line is new
2833a605e32eSJoe Perches		    $rawline !~ /^\+[ \t]*\*/) {		#no leading *
283486406b1cSJoe Perches			WARN("BLOCK_COMMENT_STYLE",
283586406b1cSJoe Perches			     "Block comments use * on subsequent lines\n" . $hereprev);
2836a605e32eSJoe Perches		}
2837a605e32eSJoe Perches
283886406b1cSJoe Perches# Block comments use */ on trailing lines
283986406b1cSJoe Perches		if ($rawline !~ m@^\+[ \t]*\*/[ \t]*$@ &&	#trailing */
2840c24f9f19SJoe Perches		    $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ &&	#inline /*...*/
2841c24f9f19SJoe Perches		    $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ &&	#trailing **/
2842c24f9f19SJoe Perches		    $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) {	#non blank */
284386406b1cSJoe Perches			WARN("BLOCK_COMMENT_STYLE",
284486406b1cSJoe Perches			     "Block comments use a trailing */ on a separate line\n" . $herecurr);
284505880600SJoe Perches		}
284605880600SJoe Perches
28477f619191SJoe Perches# check for missing blank lines after struct/union declarations
28487f619191SJoe Perches# with exceptions for various attributes and macros
28497f619191SJoe Perches		if ($prevline =~ /^[\+ ]};?\s*$/ &&
28507f619191SJoe Perches		    $line =~ /^\+/ &&
28517f619191SJoe Perches		    !($line =~ /^\+\s*$/ ||
28527f619191SJoe Perches		      $line =~ /^\+\s*EXPORT_SYMBOL/ ||
28537f619191SJoe Perches		      $line =~ /^\+\s*MODULE_/i ||
28547f619191SJoe Perches		      $line =~ /^\+\s*\#\s*(?:end|elif|else)/ ||
28557f619191SJoe Perches		      $line =~ /^\+[a-z_]*init/ ||
28567f619191SJoe Perches		      $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ ||
28577f619191SJoe Perches		      $line =~ /^\+\s*DECLARE/ ||
28587f619191SJoe Perches		      $line =~ /^\+\s*__setup/)) {
2859d752fcc8SJoe Perches			if (CHK("LINE_SPACING",
2860d752fcc8SJoe Perches				"Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) &&
2861d752fcc8SJoe Perches			    $fix) {
2862f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, "\+");
2863d752fcc8SJoe Perches			}
28647f619191SJoe Perches		}
28657f619191SJoe Perches
2866365dd4eaSJoe Perches# check for multiple consecutive blank lines
2867365dd4eaSJoe Perches		if ($prevline =~ /^[\+ ]\s*$/ &&
2868365dd4eaSJoe Perches		    $line =~ /^\+\s*$/ &&
2869365dd4eaSJoe Perches		    $last_blank_line != ($linenr - 1)) {
2870d752fcc8SJoe Perches			if (CHK("LINE_SPACING",
2871d752fcc8SJoe Perches				"Please don't use multiple blank lines\n" . $hereprev) &&
2872d752fcc8SJoe Perches			    $fix) {
2873f2d7e4d4SJoe Perches				fix_delete_line($fixlinenr, $rawline);
2874d752fcc8SJoe Perches			}
2875d752fcc8SJoe Perches
2876365dd4eaSJoe Perches			$last_blank_line = $linenr;
2877365dd4eaSJoe Perches		}
2878365dd4eaSJoe Perches
28793b617e3bSJoe Perches# check for missing blank lines after declarations
28803f7bac03SJoe Perches		if ($sline =~ /^\+\s+\S/ &&			#Not at char 1
28813f7bac03SJoe Perches			# actual declarations
28823f7bac03SJoe Perches		    ($prevline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
28835a4e1fd3SJoe Perches			# function pointer declarations
28845a4e1fd3SJoe Perches		     $prevline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
28853f7bac03SJoe Perches			# foo bar; where foo is some local typedef or #define
28863f7bac03SJoe Perches		     $prevline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
28873f7bac03SJoe Perches			# known declaration macros
28883f7bac03SJoe Perches		     $prevline =~ /^\+\s+$declaration_macros/) &&
28893f7bac03SJoe Perches			# for "else if" which can look like "$Ident $Ident"
28903f7bac03SJoe Perches		    !($prevline =~ /^\+\s+$c90_Keywords\b/ ||
28913f7bac03SJoe Perches			# other possible extensions of declaration lines
28923f7bac03SJoe Perches		      $prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ ||
28933f7bac03SJoe Perches			# not starting a section or a macro "\" extended line
28943f7bac03SJoe Perches		      $prevline =~ /(?:\{\s*|\\)$/) &&
28953f7bac03SJoe Perches			# looks like a declaration
28963f7bac03SJoe Perches		    !($sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
28975a4e1fd3SJoe Perches			# function pointer declarations
28985a4e1fd3SJoe Perches		      $sline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
28993f7bac03SJoe Perches			# foo bar; where foo is some local typedef or #define
29003f7bac03SJoe Perches		      $sline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
29013f7bac03SJoe Perches			# known declaration macros
29023f7bac03SJoe Perches		      $sline =~ /^\+\s+$declaration_macros/ ||
29033f7bac03SJoe Perches			# start of struct or union or enum
29043b617e3bSJoe Perches		      $sline =~ /^\+\s+(?:union|struct|enum|typedef)\b/ ||
29053f7bac03SJoe Perches			# start or end of block or continuation of declaration
29063f7bac03SJoe Perches		      $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ ||
29073f7bac03SJoe Perches			# bitfield continuation
29083f7bac03SJoe Perches		      $sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ ||
29093f7bac03SJoe Perches			# other possible extensions of declaration lines
29103f7bac03SJoe Perches		      $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) &&
29113f7bac03SJoe Perches			# indentation of previous and current line are the same
29123f7bac03SJoe Perches		    (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) {
2913d752fcc8SJoe Perches			if (WARN("LINE_SPACING",
2914d752fcc8SJoe Perches				 "Missing a blank line after declarations\n" . $hereprev) &&
2915d752fcc8SJoe Perches			    $fix) {
2916f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, "\+");
2917d752fcc8SJoe Perches			}
29183b617e3bSJoe Perches		}
29193b617e3bSJoe Perches
29205f7ddae6SRaffaele Recalcati# check for spaces at the beginning of a line.
29216b4c5bebSAndy Whitcroft# Exceptions:
29226b4c5bebSAndy Whitcroft#  1) within comments
29236b4c5bebSAndy Whitcroft#  2) indented preprocessor commands
29246b4c5bebSAndy Whitcroft#  3) hanging labels
29253705ce5bSJoe Perches		if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/)  {
29265f7ddae6SRaffaele Recalcati			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
29273705ce5bSJoe Perches			if (WARN("LEADING_SPACE",
29283705ce5bSJoe Perches				 "please, no spaces at the start of a line\n" . $herevet) &&
29293705ce5bSJoe Perches			    $fix) {
2930194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
29313705ce5bSJoe Perches			}
29325f7ddae6SRaffaele Recalcati		}
29335f7ddae6SRaffaele Recalcati
2934b9ea10d6SAndy Whitcroft# check we are in a valid C source file if not then ignore this hunk
2935b9ea10d6SAndy Whitcroft		next if ($realfile !~ /\.(h|c)$/);
2936b9ea10d6SAndy Whitcroft
2937032a4c0fSJoe Perches# check indentation of any line with a bare else
2938840080a0SJoe Perches# (but not if it is a multiple line "if (foo) return bar; else return baz;")
2939032a4c0fSJoe Perches# if the previous line is a break or return and is indented 1 tab more...
2940032a4c0fSJoe Perches		if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {
2941032a4c0fSJoe Perches			my $tabs = length($1) + 1;
2942840080a0SJoe Perches			if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ ||
2943840080a0SJoe Perches			    ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ &&
2944840080a0SJoe Perches			     defined $lines[$linenr] &&
2945840080a0SJoe Perches			     $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) {
2946032a4c0fSJoe Perches				WARN("UNNECESSARY_ELSE",
2947032a4c0fSJoe Perches				     "else is not generally useful after a break or return\n" . $hereprev);
2948032a4c0fSJoe Perches			}
2949032a4c0fSJoe Perches		}
2950032a4c0fSJoe Perches
2951c00df19aSJoe Perches# check indentation of a line with a break;
2952c00df19aSJoe Perches# if the previous line is a goto or return and is indented the same # of tabs
2953c00df19aSJoe Perches		if ($sline =~ /^\+([\t]+)break\s*;\s*$/) {
2954c00df19aSJoe Perches			my $tabs = $1;
2955c00df19aSJoe Perches			if ($prevline =~ /^\+$tabs(?:goto|return)\b/) {
2956c00df19aSJoe Perches				WARN("UNNECESSARY_BREAK",
2957c00df19aSJoe Perches				     "break is not useful after a goto or return\n" . $hereprev);
2958c00df19aSJoe Perches			}
2959c00df19aSJoe Perches		}
2960c00df19aSJoe Perches
29611ba8dfd1SKees Cook# discourage the addition of CONFIG_EXPERIMENTAL in #if(def).
29621ba8dfd1SKees Cook		if ($line =~ /^\+\s*\#\s*if.*\bCONFIG_EXPERIMENTAL\b/) {
29631ba8dfd1SKees Cook			WARN("CONFIG_EXPERIMENTAL",
29641ba8dfd1SKees Cook			     "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
29651ba8dfd1SKees Cook		}
29661ba8dfd1SKees Cook
2967c2fdda0dSAndy Whitcroft# check for RCS/CVS revision markers
2968cf655043SAndy Whitcroft		if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
2969000d1cc1SJoe Perches			WARN("CVS_KEYWORD",
2970000d1cc1SJoe Perches			     "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
2971c2fdda0dSAndy Whitcroft		}
297222f2a2efSAndy Whitcroft
297342e41c54SMike Frysinger# Blackfin: don't use __builtin_bfin_[cs]sync
297442e41c54SMike Frysinger		if ($line =~ /__builtin_bfin_csync/) {
297542e41c54SMike Frysinger			my $herevet = "$here\n" . cat_vet($line) . "\n";
2976000d1cc1SJoe Perches			ERROR("CSYNC",
2977000d1cc1SJoe Perches			      "use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
297842e41c54SMike Frysinger		}
297942e41c54SMike Frysinger		if ($line =~ /__builtin_bfin_ssync/) {
298042e41c54SMike Frysinger			my $herevet = "$here\n" . cat_vet($line) . "\n";
2981000d1cc1SJoe Perches			ERROR("SSYNC",
2982000d1cc1SJoe Perches			      "use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
298342e41c54SMike Frysinger		}
298442e41c54SMike Frysinger
298556e77d70SJoe Perches# check for old HOTPLUG __dev<foo> section markings
298656e77d70SJoe Perches		if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
298756e77d70SJoe Perches			WARN("HOTPLUG_SECTION",
298856e77d70SJoe Perches			     "Using $1 is unnecessary\n" . $herecurr);
298956e77d70SJoe Perches		}
299056e77d70SJoe Perches
29919c0ca6f9SAndy Whitcroft# Check for potential 'bare' types
29922b474a1aSAndy Whitcroft		my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
29932b474a1aSAndy Whitcroft		    $realline_next);
29943e469cdcSAndy Whitcroft#print "LINE<$line>\n";
29953e469cdcSAndy Whitcroft		if ($linenr >= $suppress_statement &&
29961b5539b1SJoe Perches		    $realcnt && $sline =~ /.\s*\S/) {
2997170d3a22SAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
2998f5fe35ddSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0);
2999171ae1a4SAndy Whitcroft			$stat =~ s/\n./\n /g;
3000171ae1a4SAndy Whitcroft			$cond =~ s/\n./\n /g;
3001171ae1a4SAndy Whitcroft
30023e469cdcSAndy Whitcroft#print "linenr<$linenr> <$stat>\n";
30033e469cdcSAndy Whitcroft			# If this statement has no statement boundaries within
30043e469cdcSAndy Whitcroft			# it there is no point in retrying a statement scan
30053e469cdcSAndy Whitcroft			# until we hit end of it.
30063e469cdcSAndy Whitcroft			my $frag = $stat; $frag =~ s/;+\s*$//;
30073e469cdcSAndy Whitcroft			if ($frag !~ /(?:{|;)/) {
30083e469cdcSAndy Whitcroft#print "skip<$line_nr_next>\n";
30093e469cdcSAndy Whitcroft				$suppress_statement = $line_nr_next;
30103e469cdcSAndy Whitcroft			}
3011f74bd194SAndy Whitcroft
30122b474a1aSAndy Whitcroft			# Find the real next line.
30132b474a1aSAndy Whitcroft			$realline_next = $line_nr_next;
30142b474a1aSAndy Whitcroft			if (defined $realline_next &&
30152b474a1aSAndy Whitcroft			    (!defined $lines[$realline_next - 1] ||
30162b474a1aSAndy Whitcroft			     substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
30172b474a1aSAndy Whitcroft				$realline_next++;
30182b474a1aSAndy Whitcroft			}
30192b474a1aSAndy Whitcroft
3020171ae1a4SAndy Whitcroft			my $s = $stat;
3021171ae1a4SAndy Whitcroft			$s =~ s/{.*$//s;
3022cf655043SAndy Whitcroft
3023c2fdda0dSAndy Whitcroft			# Ignore goto labels.
3024171ae1a4SAndy Whitcroft			if ($s =~ /$Ident:\*$/s) {
3025c2fdda0dSAndy Whitcroft
3026c2fdda0dSAndy Whitcroft			# Ignore functions being called
3027171ae1a4SAndy Whitcroft			} elsif ($s =~ /^.\s*$Ident\s*\(/s) {
3028c2fdda0dSAndy Whitcroft
3029463f2864SAndy Whitcroft			} elsif ($s =~ /^.\s*else\b/s) {
3030463f2864SAndy Whitcroft
3031c45dcabdSAndy Whitcroft			# declarations always start with types
3032d2506586SAndy 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) {
3033c45dcabdSAndy Whitcroft				my $type = $1;
3034c45dcabdSAndy Whitcroft				$type =~ s/\s+/ /g;
3035c45dcabdSAndy Whitcroft				possible($type, "A:" . $s);
3036c45dcabdSAndy Whitcroft
30376c72ffaaSAndy Whitcroft			# definitions in global scope can only start with types
3038a6a84062SAndy Whitcroft			} elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
3039c45dcabdSAndy Whitcroft				possible($1, "B:" . $s);
3040c2fdda0dSAndy Whitcroft			}
30418905a67cSAndy Whitcroft
30426c72ffaaSAndy Whitcroft			# any (foo ... *) is a pointer cast, and foo is a type
304365863862SAndy Whitcroft			while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
3044c45dcabdSAndy Whitcroft				possible($1, "C:" . $s);
30459c0ca6f9SAndy Whitcroft			}
30468905a67cSAndy Whitcroft
30478905a67cSAndy Whitcroft			# Check for any sort of function declaration.
30488905a67cSAndy Whitcroft			# int foo(something bar, other baz);
30498905a67cSAndy Whitcroft			# void (*store_gdt)(x86_descr_ptr *);
3050171ae1a4SAndy Whitcroft			if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
30518905a67cSAndy Whitcroft				my ($name_len) = length($1);
30528905a67cSAndy Whitcroft
3053cf655043SAndy Whitcroft				my $ctx = $s;
3054773647a0SAndy Whitcroft				substr($ctx, 0, $name_len + 1, '');
30558905a67cSAndy Whitcroft				$ctx =~ s/\)[^\)]*$//;
3056cf655043SAndy Whitcroft
30578905a67cSAndy Whitcroft				for my $arg (split(/\s*,\s*/, $ctx)) {
3058c45dcabdSAndy Whitcroft					if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
30598905a67cSAndy Whitcroft
3060c45dcabdSAndy Whitcroft						possible($1, "D:" . $s);
30618905a67cSAndy Whitcroft					}
30628905a67cSAndy Whitcroft				}
30638905a67cSAndy Whitcroft			}
30648905a67cSAndy Whitcroft
30659c0ca6f9SAndy Whitcroft		}
30669c0ca6f9SAndy Whitcroft
306700df344fSAndy Whitcroft#
306800df344fSAndy Whitcroft# Checks which may be anchored in the context.
306900df344fSAndy Whitcroft#
307000df344fSAndy Whitcroft
307100df344fSAndy Whitcroft# Check for switch () and associated case and default
307200df344fSAndy Whitcroft# statements should be at the same indent.
307300df344fSAndy Whitcroft		if ($line=~/\bswitch\s*\(.*\)/) {
307400df344fSAndy Whitcroft			my $err = '';
307500df344fSAndy Whitcroft			my $sep = '';
307600df344fSAndy Whitcroft			my @ctx = ctx_block_outer($linenr, $realcnt);
307700df344fSAndy Whitcroft			shift(@ctx);
307800df344fSAndy Whitcroft			for my $ctx (@ctx) {
307900df344fSAndy Whitcroft				my ($clen, $cindent) = line_stats($ctx);
308000df344fSAndy Whitcroft				if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
308100df344fSAndy Whitcroft							$indent != $cindent) {
308200df344fSAndy Whitcroft					$err .= "$sep$ctx\n";
308300df344fSAndy Whitcroft					$sep = '';
308400df344fSAndy Whitcroft				} else {
308500df344fSAndy Whitcroft					$sep = "[...]\n";
308600df344fSAndy Whitcroft				}
308700df344fSAndy Whitcroft			}
308800df344fSAndy Whitcroft			if ($err ne '') {
3089000d1cc1SJoe Perches				ERROR("SWITCH_CASE_INDENT_LEVEL",
3090000d1cc1SJoe Perches				      "switch and case should be at the same indent\n$hereline$err");
3091de7d4f0eSAndy Whitcroft			}
3092de7d4f0eSAndy Whitcroft		}
3093de7d4f0eSAndy Whitcroft
3094de7d4f0eSAndy Whitcroft# if/while/etc brace do not go on next line, unless defining a do while loop,
3095de7d4f0eSAndy Whitcroft# or if that brace on the next line is for something else
30960fe3dc2bSJoe Perches		if ($line =~ /(.*)\b((?:if|while|for|switch|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
3097773647a0SAndy Whitcroft			my $pre_ctx = "$1$2";
3098773647a0SAndy Whitcroft
30999c0ca6f9SAndy Whitcroft			my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
31008eef05ddSJoe Perches
31018eef05ddSJoe Perches			if ($line =~ /^\+\t{6,}/) {
31028eef05ddSJoe Perches				WARN("DEEP_INDENTATION",
31038eef05ddSJoe Perches				     "Too many leading tabs - consider code refactoring\n" . $herecurr);
31048eef05ddSJoe Perches			}
31058eef05ddSJoe Perches
3106de7d4f0eSAndy Whitcroft			my $ctx_cnt = $realcnt - $#ctx - 1;
3107de7d4f0eSAndy Whitcroft			my $ctx = join("\n", @ctx);
3108de7d4f0eSAndy Whitcroft
3109548596d5SAndy Whitcroft			my $ctx_ln = $linenr;
3110548596d5SAndy Whitcroft			my $ctx_skip = $realcnt;
3111de7d4f0eSAndy Whitcroft
3112548596d5SAndy Whitcroft			while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
3113548596d5SAndy Whitcroft					defined $lines[$ctx_ln - 1] &&
3114548596d5SAndy Whitcroft					$lines[$ctx_ln - 1] =~ /^-/)) {
3115548596d5SAndy Whitcroft				##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
3116548596d5SAndy Whitcroft				$ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
3117773647a0SAndy Whitcroft				$ctx_ln++;
3118773647a0SAndy Whitcroft			}
3119548596d5SAndy Whitcroft
312053210168SAndy Whitcroft			#print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
312153210168SAndy Whitcroft			#print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
3122773647a0SAndy Whitcroft
3123773647a0SAndy Whitcroft			if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
3124000d1cc1SJoe Perches				ERROR("OPEN_BRACE",
3125000d1cc1SJoe Perches				      "that open brace { should be on the previous line\n" .
312601464f30SAndy Whitcroft					"$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
312700df344fSAndy Whitcroft			}
3128773647a0SAndy Whitcroft			if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
3129773647a0SAndy Whitcroft			    $ctx =~ /\)\s*\;\s*$/ &&
3130773647a0SAndy Whitcroft			    defined $lines[$ctx_ln - 1])
3131773647a0SAndy Whitcroft			{
31329c0ca6f9SAndy Whitcroft				my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
31339c0ca6f9SAndy Whitcroft				if ($nindent > $indent) {
3134000d1cc1SJoe Perches					WARN("TRAILING_SEMICOLON",
3135000d1cc1SJoe Perches					     "trailing semicolon indicates no statements, indent implies otherwise\n" .
313601464f30SAndy Whitcroft						"$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
31379c0ca6f9SAndy Whitcroft				}
31389c0ca6f9SAndy Whitcroft			}
313900df344fSAndy Whitcroft		}
314000df344fSAndy Whitcroft
31414d001e4dSAndy Whitcroft# Check relative indent for conditionals and blocks.
31420fe3dc2bSJoe Perches		if ($line =~ /\b(?:(?:if|while|for|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
31433e469cdcSAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
31443e469cdcSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0)
31453e469cdcSAndy Whitcroft					if (!defined $stat);
31464d001e4dSAndy Whitcroft			my ($s, $c) = ($stat, $cond);
31474d001e4dSAndy Whitcroft
31484d001e4dSAndy Whitcroft			substr($s, 0, length($c), '');
31494d001e4dSAndy Whitcroft
31509f5af480SJoe Perches			# remove inline comments
31519f5af480SJoe Perches			$s =~ s/$;/ /g;
31529f5af480SJoe Perches			$c =~ s/$;/ /g;
31534d001e4dSAndy Whitcroft
31544d001e4dSAndy Whitcroft			# Find out how long the conditional actually is.
31556f779c18SAndy Whitcroft			my @newlines = ($c =~ /\n/gs);
31566f779c18SAndy Whitcroft			my $cond_lines = 1 + $#newlines;
31574d001e4dSAndy Whitcroft
31589f5af480SJoe Perches			# Make sure we remove the line prefixes as we have
31599f5af480SJoe Perches			# none on the first line, and are going to readd them
31609f5af480SJoe Perches			# where necessary.
31619f5af480SJoe Perches			$s =~ s/\n./\n/gs;
31629f5af480SJoe Perches			while ($s =~ /\n\s+\\\n/) {
31639f5af480SJoe Perches				$cond_lines += $s =~ s/\n\s+\\\n/\n/g;
31649f5af480SJoe Perches			}
31659f5af480SJoe Perches
31664d001e4dSAndy Whitcroft			# We want to check the first line inside the block
31674d001e4dSAndy Whitcroft			# starting at the end of the conditional, so remove:
31684d001e4dSAndy Whitcroft			#  1) any blank line termination
31694d001e4dSAndy Whitcroft			#  2) any opening brace { on end of the line
31704d001e4dSAndy Whitcroft			#  3) any do (...) {
31714d001e4dSAndy Whitcroft			my $continuation = 0;
31724d001e4dSAndy Whitcroft			my $check = 0;
31734d001e4dSAndy Whitcroft			$s =~ s/^.*\bdo\b//;
31744d001e4dSAndy Whitcroft			$s =~ s/^\s*{//;
31754d001e4dSAndy Whitcroft			if ($s =~ s/^\s*\\//) {
31764d001e4dSAndy Whitcroft				$continuation = 1;
31774d001e4dSAndy Whitcroft			}
31789bd49efeSAndy Whitcroft			if ($s =~ s/^\s*?\n//) {
31794d001e4dSAndy Whitcroft				$check = 1;
31804d001e4dSAndy Whitcroft				$cond_lines++;
31814d001e4dSAndy Whitcroft			}
31824d001e4dSAndy Whitcroft
31834d001e4dSAndy Whitcroft			# Also ignore a loop construct at the end of a
31844d001e4dSAndy Whitcroft			# preprocessor statement.
31854d001e4dSAndy Whitcroft			if (($prevline =~ /^.\s*#\s*define\s/ ||
31864d001e4dSAndy Whitcroft			    $prevline =~ /\\\s*$/) && $continuation == 0) {
31874d001e4dSAndy Whitcroft				$check = 0;
31884d001e4dSAndy Whitcroft			}
31894d001e4dSAndy Whitcroft
31909bd49efeSAndy Whitcroft			my $cond_ptr = -1;
3191740504c6SAndy Whitcroft			$continuation = 0;
31929bd49efeSAndy Whitcroft			while ($cond_ptr != $cond_lines) {
31939bd49efeSAndy Whitcroft				$cond_ptr = $cond_lines;
31944d001e4dSAndy Whitcroft
3195f16fa28fSAndy Whitcroft				# If we see an #else/#elif then the code
3196f16fa28fSAndy Whitcroft				# is not linear.
3197f16fa28fSAndy Whitcroft				if ($s =~ /^\s*\#\s*(?:else|elif)/) {
3198f16fa28fSAndy Whitcroft					$check = 0;
3199f16fa28fSAndy Whitcroft				}
3200f16fa28fSAndy Whitcroft
32019bd49efeSAndy Whitcroft				# Ignore:
32029bd49efeSAndy Whitcroft				#  1) blank lines, they should be at 0,
32039bd49efeSAndy Whitcroft				#  2) preprocessor lines, and
32049bd49efeSAndy Whitcroft				#  3) labels.
3205740504c6SAndy Whitcroft				if ($continuation ||
3206740504c6SAndy Whitcroft				    $s =~ /^\s*?\n/ ||
32079bd49efeSAndy Whitcroft				    $s =~ /^\s*#\s*?/ ||
32089bd49efeSAndy Whitcroft				    $s =~ /^\s*$Ident\s*:/) {
3209740504c6SAndy Whitcroft					$continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
321030dad6ebSAndy Whitcroft					if ($s =~ s/^.*?\n//) {
32119bd49efeSAndy Whitcroft						$cond_lines++;
32129bd49efeSAndy Whitcroft					}
32134d001e4dSAndy Whitcroft				}
321430dad6ebSAndy Whitcroft			}
32154d001e4dSAndy Whitcroft
32164d001e4dSAndy Whitcroft			my (undef, $sindent) = line_stats("+" . $s);
32174d001e4dSAndy Whitcroft			my $stat_real = raw_line($linenr, $cond_lines);
32184d001e4dSAndy Whitcroft
32194d001e4dSAndy Whitcroft			# Check if either of these lines are modified, else
32204d001e4dSAndy Whitcroft			# this is not this patch's fault.
32214d001e4dSAndy Whitcroft			if (!defined($stat_real) ||
32224d001e4dSAndy Whitcroft			    $stat !~ /^\+/ && $stat_real !~ /^\+/) {
32234d001e4dSAndy Whitcroft				$check = 0;
32244d001e4dSAndy Whitcroft			}
32254d001e4dSAndy Whitcroft			if (defined($stat_real) && $cond_lines > 1) {
32264d001e4dSAndy Whitcroft				$stat_real = "[...]\n$stat_real";
32274d001e4dSAndy Whitcroft			}
32284d001e4dSAndy Whitcroft
32299bd49efeSAndy 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";
32304d001e4dSAndy Whitcroft
32319f5af480SJoe Perches			if ($check && $s ne '' &&
32329f5af480SJoe Perches			    (($sindent % 8) != 0 ||
32339f5af480SJoe Perches			     ($sindent < $indent) ||
32349f5af480SJoe Perches			     ($sindent > $indent + 8))) {
3235000d1cc1SJoe Perches				WARN("SUSPECT_CODE_INDENT",
3236000d1cc1SJoe Perches				     "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
32374d001e4dSAndy Whitcroft			}
32384d001e4dSAndy Whitcroft		}
32394d001e4dSAndy Whitcroft
32406c72ffaaSAndy Whitcroft		# Track the 'values' across context and added lines.
32416c72ffaaSAndy Whitcroft		my $opline = $line; $opline =~ s/^./ /;
32421f65f947SAndy Whitcroft		my ($curr_values, $curr_vars) =
32431f65f947SAndy Whitcroft				annotate_values($opline . "\n", $prev_values);
32446c72ffaaSAndy Whitcroft		$curr_values = $prev_values . $curr_values;
3245c2fdda0dSAndy Whitcroft		if ($dbg_values) {
3246c2fdda0dSAndy Whitcroft			my $outline = $opline; $outline =~ s/\t/ /g;
3247cf655043SAndy Whitcroft			print "$linenr > .$outline\n";
3248cf655043SAndy Whitcroft			print "$linenr > $curr_values\n";
32491f65f947SAndy Whitcroft			print "$linenr >  $curr_vars\n";
3250c2fdda0dSAndy Whitcroft		}
32516c72ffaaSAndy Whitcroft		$prev_values = substr($curr_values, -1);
32526c72ffaaSAndy Whitcroft
325300df344fSAndy Whitcroft#ignore lines not being added
32543705ce5bSJoe Perches		next if ($line =~ /^[^\+]/);
325500df344fSAndy Whitcroft
3256a1ce18e4SJoe Perches# check for declarations of signed or unsigned without int
3257207a8e84SJoe Perches		while ($line =~ m{($Declare)\s*(?!char\b|short\b|int\b|long\b)\s*($Ident)?\s*[=,;\[\)\(]}g) {
3258a1ce18e4SJoe Perches			my $type = $1;
3259a1ce18e4SJoe Perches			my $var = $2;
3260207a8e84SJoe Perches			$var = "" if (!defined $var);
3261207a8e84SJoe Perches			if ($type =~ /^(?:(?:$Storage|$Inline|$Attribute)\s+)*((?:un)?signed)((?:\s*\*)*)\s*$/) {
3262a1ce18e4SJoe Perches				my $sign = $1;
3263a1ce18e4SJoe Perches				my $pointer = $2;
3264a1ce18e4SJoe Perches
3265a1ce18e4SJoe Perches				$pointer = "" if (!defined $pointer);
3266a1ce18e4SJoe Perches
3267a1ce18e4SJoe Perches				if (WARN("UNSPECIFIED_INT",
3268a1ce18e4SJoe Perches					 "Prefer '" . trim($sign) . " int" . rtrim($pointer) . "' to bare use of '$sign" . rtrim($pointer) . "'\n" . $herecurr) &&
3269a1ce18e4SJoe Perches				    $fix) {
3270a1ce18e4SJoe Perches					my $decl = trim($sign) . " int ";
3271207a8e84SJoe Perches					my $comp_pointer = $pointer;
3272207a8e84SJoe Perches					$comp_pointer =~ s/\s//g;
3273207a8e84SJoe Perches					$decl .= $comp_pointer;
3274207a8e84SJoe Perches					$decl = rtrim($decl) if ($var eq "");
3275207a8e84SJoe Perches					$fixed[$fixlinenr] =~ s@\b$sign\s*\Q$pointer\E\s*$var\b@$decl$var@;
3276a1ce18e4SJoe Perches				}
3277a1ce18e4SJoe Perches			}
3278a1ce18e4SJoe Perches		}
3279a1ce18e4SJoe Perches
3280653d4876SAndy Whitcroft# TEST: allow direct testing of the type matcher.
32817429c690SAndy Whitcroft		if ($dbg_type) {
32827429c690SAndy Whitcroft			if ($line =~ /^.\s*$Declare\s*$/) {
3283000d1cc1SJoe Perches				ERROR("TEST_TYPE",
3284000d1cc1SJoe Perches				      "TEST: is type\n" . $herecurr);
32857429c690SAndy Whitcroft			} elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
3286000d1cc1SJoe Perches				ERROR("TEST_NOT_TYPE",
3287000d1cc1SJoe Perches				      "TEST: is not type ($1 is)\n". $herecurr);
32887429c690SAndy Whitcroft			}
3289653d4876SAndy Whitcroft			next;
3290653d4876SAndy Whitcroft		}
3291a1ef277eSAndy Whitcroft# TEST: allow direct testing of the attribute matcher.
3292a1ef277eSAndy Whitcroft		if ($dbg_attr) {
32939360b0e5SAndy Whitcroft			if ($line =~ /^.\s*$Modifier\s*$/) {
3294000d1cc1SJoe Perches				ERROR("TEST_ATTR",
3295000d1cc1SJoe Perches				      "TEST: is attr\n" . $herecurr);
32969360b0e5SAndy Whitcroft			} elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
3297000d1cc1SJoe Perches				ERROR("TEST_NOT_ATTR",
3298000d1cc1SJoe Perches				      "TEST: is not attr ($1 is)\n". $herecurr);
3299a1ef277eSAndy Whitcroft			}
3300a1ef277eSAndy Whitcroft			next;
3301a1ef277eSAndy Whitcroft		}
3302653d4876SAndy Whitcroft
3303f0a594c1SAndy Whitcroft# check for initialisation to aggregates open brace on the next line
330499423c20SAndy Whitcroft		if ($line =~ /^.\s*{/ &&
330599423c20SAndy Whitcroft		    $prevline =~ /(?:^|[^=])=\s*$/) {
3306d752fcc8SJoe Perches			if (ERROR("OPEN_BRACE",
3307d752fcc8SJoe Perches				  "that open brace { should be on the previous line\n" . $hereprev) &&
3308f2d7e4d4SJoe Perches			    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
3309f2d7e4d4SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
3310f2d7e4d4SJoe Perches				fix_delete_line($fixlinenr, $rawline);
3311d752fcc8SJoe Perches				my $fixedline = $prevrawline;
3312d752fcc8SJoe Perches				$fixedline =~ s/\s*=\s*$/ = {/;
3313f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
3314d752fcc8SJoe Perches				$fixedline = $line;
3315d752fcc8SJoe Perches				$fixedline =~ s/^(.\s*){\s*/$1/;
3316f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
3317d752fcc8SJoe Perches			}
3318f0a594c1SAndy Whitcroft		}
3319f0a594c1SAndy Whitcroft
332000df344fSAndy Whitcroft#
332100df344fSAndy Whitcroft# Checks which are anchored on the added line.
332200df344fSAndy Whitcroft#
332300df344fSAndy Whitcroft
3324653d4876SAndy Whitcroft# check for malformed paths in #include statements (uses RAW line)
3325c45dcabdSAndy Whitcroft		if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
3326653d4876SAndy Whitcroft			my $path = $1;
3327653d4876SAndy Whitcroft			if ($path =~ m{//}) {
3328000d1cc1SJoe Perches				ERROR("MALFORMED_INCLUDE",
3329495e9d84SJoe Perches				      "malformed #include filename\n" . $herecurr);
3330495e9d84SJoe Perches			}
3331495e9d84SJoe Perches			if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
3332495e9d84SJoe Perches				ERROR("UAPI_INCLUDE",
3333495e9d84SJoe Perches				      "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
3334653d4876SAndy Whitcroft			}
3335653d4876SAndy Whitcroft		}
3336653d4876SAndy Whitcroft
333700df344fSAndy Whitcroft# no C99 // comments
333800df344fSAndy Whitcroft		if ($line =~ m{//}) {
33393705ce5bSJoe Perches			if (ERROR("C99_COMMENTS",
33403705ce5bSJoe Perches				  "do not use C99 // comments\n" . $herecurr) &&
33413705ce5bSJoe Perches			    $fix) {
3342194f66fcSJoe Perches				my $line = $fixed[$fixlinenr];
33433705ce5bSJoe Perches				if ($line =~ /\/\/(.*)$/) {
33443705ce5bSJoe Perches					my $comment = trim($1);
3345194f66fcSJoe Perches					$fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@;
33463705ce5bSJoe Perches				}
33473705ce5bSJoe Perches			}
334800df344fSAndy Whitcroft		}
334900df344fSAndy Whitcroft		# Remove C99 comments.
33500a920b5bSAndy Whitcroft		$line =~ s@//.*@@;
33516c72ffaaSAndy Whitcroft		$opline =~ s@//.*@@;
33520a920b5bSAndy Whitcroft
33532b474a1aSAndy Whitcroft# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
33542b474a1aSAndy Whitcroft# the whole statement.
33552b474a1aSAndy Whitcroft#print "APW <$lines[$realline_next - 1]>\n";
33562b474a1aSAndy Whitcroft		if (defined $realline_next &&
33572b474a1aSAndy Whitcroft		    exists $lines[$realline_next - 1] &&
33582b474a1aSAndy Whitcroft		    !defined $suppress_export{$realline_next} &&
33592b474a1aSAndy Whitcroft		    ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
33602b474a1aSAndy Whitcroft		     $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
33613cbf62dfSAndy Whitcroft			# Handle definitions which produce identifiers with
33623cbf62dfSAndy Whitcroft			# a prefix:
33633cbf62dfSAndy Whitcroft			#   XXX(foo);
33643cbf62dfSAndy Whitcroft			#   EXPORT_SYMBOL(something_foo);
3365653d4876SAndy Whitcroft			my $name = $1;
336687a53877SAndy Whitcroft			if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
33673cbf62dfSAndy Whitcroft			    $name =~ /^${Ident}_$2/) {
33683cbf62dfSAndy Whitcroft#print "FOO C name<$name>\n";
33693cbf62dfSAndy Whitcroft				$suppress_export{$realline_next} = 1;
33703cbf62dfSAndy Whitcroft
33713cbf62dfSAndy Whitcroft			} elsif ($stat !~ /(?:
33722b474a1aSAndy Whitcroft				\n.}\s*$|
337348012058SAndy Whitcroft				^.DEFINE_$Ident\(\Q$name\E\)|
337448012058SAndy Whitcroft				^.DECLARE_$Ident\(\Q$name\E\)|
337548012058SAndy Whitcroft				^.LIST_HEAD\(\Q$name\E\)|
33762b474a1aSAndy Whitcroft				^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
33772b474a1aSAndy Whitcroft				\b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
337848012058SAndy Whitcroft			    )/x) {
33792b474a1aSAndy Whitcroft#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
33802b474a1aSAndy Whitcroft				$suppress_export{$realline_next} = 2;
33812b474a1aSAndy Whitcroft			} else {
33822b474a1aSAndy Whitcroft				$suppress_export{$realline_next} = 1;
33830a920b5bSAndy Whitcroft			}
33840a920b5bSAndy Whitcroft		}
33852b474a1aSAndy Whitcroft		if (!defined $suppress_export{$linenr} &&
33862b474a1aSAndy Whitcroft		    $prevline =~ /^.\s*$/ &&
33872b474a1aSAndy Whitcroft		    ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
33882b474a1aSAndy Whitcroft		     $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
33892b474a1aSAndy Whitcroft#print "FOO B <$lines[$linenr - 1]>\n";
33902b474a1aSAndy Whitcroft			$suppress_export{$linenr} = 2;
33912b474a1aSAndy Whitcroft		}
33922b474a1aSAndy Whitcroft		if (defined $suppress_export{$linenr} &&
33932b474a1aSAndy Whitcroft		    $suppress_export{$linenr} == 2) {
3394000d1cc1SJoe Perches			WARN("EXPORT_SYMBOL",
3395000d1cc1SJoe Perches			     "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
33962b474a1aSAndy Whitcroft		}
33970a920b5bSAndy Whitcroft
33985150bda4SJoe Eloff# check for global initialisers.
33996d32f7a3SJoe Perches		if ($line =~ /^\+$Type\s*$Ident(?:\s+$Modifier)*\s*=\s*($zero_initializer)\s*;/) {
3400d5e616fcSJoe Perches			if (ERROR("GLOBAL_INITIALISERS",
34016d32f7a3SJoe Perches				  "do not initialise globals to $1\n" . $herecurr) &&
3402d5e616fcSJoe Perches			    $fix) {
34036d32f7a3SJoe Perches				$fixed[$fixlinenr] =~ s/(^.$Type\s*$Ident(?:\s+$Modifier)*)\s*=\s*$zero_initializer\s*;/$1;/;
3404d5e616fcSJoe Perches			}
3405f0a594c1SAndy Whitcroft		}
34060a920b5bSAndy Whitcroft# check for static initialisers.
34076d32f7a3SJoe Perches		if ($line =~ /^\+.*\bstatic\s.*=\s*($zero_initializer)\s*;/) {
3408d5e616fcSJoe Perches			if (ERROR("INITIALISED_STATIC",
34096d32f7a3SJoe Perches				  "do not initialise statics to $1\n" .
3410d5e616fcSJoe Perches				      $herecurr) &&
3411d5e616fcSJoe Perches			    $fix) {
34126d32f7a3SJoe Perches				$fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*$zero_initializer\s*;/$1;/;
3413d5e616fcSJoe Perches			}
34140a920b5bSAndy Whitcroft		}
34150a920b5bSAndy Whitcroft
34161813087dSJoe Perches# check for misordered declarations of char/short/int/long with signed/unsigned
34171813087dSJoe Perches		while ($sline =~ m{(\b$TypeMisordered\b)}g) {
34181813087dSJoe Perches			my $tmp = trim($1);
34191813087dSJoe Perches			WARN("MISORDERED_TYPE",
34201813087dSJoe Perches			     "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr);
34211813087dSJoe Perches		}
34221813087dSJoe Perches
3423cb710ecaSJoe Perches# check for static const char * arrays.
3424cb710ecaSJoe Perches		if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
3425000d1cc1SJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
3426000d1cc1SJoe Perches			     "static const char * array should probably be static const char * const\n" .
3427cb710ecaSJoe Perches				$herecurr);
3428cb710ecaSJoe Perches               }
3429cb710ecaSJoe Perches
3430cb710ecaSJoe Perches# check for static char foo[] = "bar" declarations.
3431cb710ecaSJoe Perches		if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
3432000d1cc1SJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
3433000d1cc1SJoe Perches			     "static char array declaration should probably be static const char\n" .
3434cb710ecaSJoe Perches				$herecurr);
3435cb710ecaSJoe Perches               }
3436cb710ecaSJoe Perches
3437ab7e23f3SJoe Perches# check for const <foo> const where <foo> is not a pointer or array type
3438ab7e23f3SJoe Perches		if ($sline =~ /\bconst\s+($BasicType)\s+const\b/) {
3439ab7e23f3SJoe Perches			my $found = $1;
3440ab7e23f3SJoe Perches			if ($sline =~ /\bconst\s+\Q$found\E\s+const\b\s*\*/) {
3441ab7e23f3SJoe Perches				WARN("CONST_CONST",
3442ab7e23f3SJoe Perches				     "'const $found const *' should probably be 'const $found * const'\n" . $herecurr);
3443ab7e23f3SJoe Perches			} elsif ($sline !~ /\bconst\s+\Q$found\E\s+const\s+\w+\s*\[/) {
3444ab7e23f3SJoe Perches				WARN("CONST_CONST",
3445ab7e23f3SJoe Perches				     "'const $found const' should probably be 'const $found'\n" . $herecurr);
3446ab7e23f3SJoe Perches			}
3447ab7e23f3SJoe Perches		}
3448ab7e23f3SJoe Perches
34499b0fa60dSJoe Perches# check for non-global char *foo[] = {"bar", ...} declarations.
34509b0fa60dSJoe Perches		if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) {
34519b0fa60dSJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
34529b0fa60dSJoe Perches			     "char * array declaration might be better as static const\n" .
34539b0fa60dSJoe Perches				$herecurr);
34549b0fa60dSJoe Perches               }
34559b0fa60dSJoe Perches
3456b598b670SJoe Perches# check for sizeof(foo)/sizeof(foo[0]) that could be ARRAY_SIZE(foo)
3457b598b670SJoe Perches		if ($line =~ m@\bsizeof\s*\(\s*($Lval)\s*\)@) {
3458b598b670SJoe Perches			my $array = $1;
3459b598b670SJoe 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*\))@) {
3460b598b670SJoe Perches				my $array_div = $1;
3461b598b670SJoe Perches				if (WARN("ARRAY_SIZE",
3462b598b670SJoe Perches					 "Prefer ARRAY_SIZE($array)\n" . $herecurr) &&
3463b598b670SJoe Perches				    $fix) {
3464b598b670SJoe Perches					$fixed[$fixlinenr] =~ s/\Q$array_div\E/ARRAY_SIZE($array)/;
3465b598b670SJoe Perches				}
3466b598b670SJoe Perches			}
3467b598b670SJoe Perches		}
3468b598b670SJoe Perches
3469b36190c5SJoe Perches# check for function declarations without arguments like "int foo()"
3470b36190c5SJoe Perches		if ($line =~ /(\b$Type\s+$Ident)\s*\(\s*\)/) {
3471b36190c5SJoe Perches			if (ERROR("FUNCTION_WITHOUT_ARGS",
3472b36190c5SJoe Perches				  "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) &&
3473b36190c5SJoe Perches			    $fix) {
3474194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/;
3475b36190c5SJoe Perches			}
3476b36190c5SJoe Perches		}
3477b36190c5SJoe Perches
347892e112fdSJoe Perches# check for uses of DEFINE_PCI_DEVICE_TABLE
347992e112fdSJoe Perches		if ($line =~ /\bDEFINE_PCI_DEVICE_TABLE\s*\(\s*(\w+)\s*\)\s*=/) {
348092e112fdSJoe Perches			if (WARN("DEFINE_PCI_DEVICE_TABLE",
348192e112fdSJoe Perches				 "Prefer struct pci_device_id over deprecated DEFINE_PCI_DEVICE_TABLE\n" . $herecurr) &&
348292e112fdSJoe Perches			    $fix) {
3483194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b(?:static\s+|)DEFINE_PCI_DEVICE_TABLE\s*\(\s*(\w+)\s*\)\s*=\s*/static const struct pci_device_id $1\[\] = /;
348492e112fdSJoe Perches			}
348593ed0e2dSJoe Perches		}
348693ed0e2dSJoe Perches
3487653d4876SAndy Whitcroft# check for new typedefs, only function parameters and sparse annotations
3488653d4876SAndy Whitcroft# make sense.
3489653d4876SAndy Whitcroft		if ($line =~ /\btypedef\s/ &&
34908054576dSAndy Whitcroft		    $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
3491c45dcabdSAndy Whitcroft		    $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
34928ed22cadSAndy Whitcroft		    $line !~ /\b$typeTypedefs\b/ &&
3493653d4876SAndy Whitcroft		    $line !~ /\b__bitwise(?:__|)\b/) {
3494000d1cc1SJoe Perches			WARN("NEW_TYPEDEFS",
3495000d1cc1SJoe Perches			     "do not add new typedefs\n" . $herecurr);
34960a920b5bSAndy Whitcroft		}
34970a920b5bSAndy Whitcroft
34980a920b5bSAndy Whitcroft# * goes on variable not on type
349965863862SAndy Whitcroft		# (char*[ const])
3500bfcb2cc7SAndy Whitcroft		while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
3501bfcb2cc7SAndy Whitcroft			#print "AA<$1>\n";
35023705ce5bSJoe Perches			my ($ident, $from, $to) = ($1, $2, $2);
3503d8aaf121SAndy Whitcroft
350465863862SAndy Whitcroft			# Should start with a space.
350565863862SAndy Whitcroft			$to =~ s/^(\S)/ $1/;
350665863862SAndy Whitcroft			# Should not end with a space.
350765863862SAndy Whitcroft			$to =~ s/\s+$//;
350865863862SAndy Whitcroft			# '*'s should not have spaces between.
3509f9a0b3d1SAndy Whitcroft			while ($to =~ s/\*\s+\*/\*\*/) {
351065863862SAndy Whitcroft			}
3511d8aaf121SAndy Whitcroft
35123705ce5bSJoe Perches##			print "1: from<$from> to<$to> ident<$ident>\n";
351365863862SAndy Whitcroft			if ($from ne $to) {
35143705ce5bSJoe Perches				if (ERROR("POINTER_LOCATION",
35153705ce5bSJoe Perches					  "\"(foo$from)\" should be \"(foo$to)\"\n" .  $herecurr) &&
35163705ce5bSJoe Perches				    $fix) {
35173705ce5bSJoe Perches					my $sub_from = $ident;
35183705ce5bSJoe Perches					my $sub_to = $ident;
35193705ce5bSJoe Perches					$sub_to =~ s/\Q$from\E/$to/;
3520194f66fcSJoe Perches					$fixed[$fixlinenr] =~
35213705ce5bSJoe Perches					    s@\Q$sub_from\E@$sub_to@;
35223705ce5bSJoe Perches				}
352365863862SAndy Whitcroft			}
3524bfcb2cc7SAndy Whitcroft		}
3525bfcb2cc7SAndy Whitcroft		while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
3526bfcb2cc7SAndy Whitcroft			#print "BB<$1>\n";
35273705ce5bSJoe Perches			my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
3528d8aaf121SAndy Whitcroft
352965863862SAndy Whitcroft			# Should start with a space.
353065863862SAndy Whitcroft			$to =~ s/^(\S)/ $1/;
353165863862SAndy Whitcroft			# Should not end with a space.
353265863862SAndy Whitcroft			$to =~ s/\s+$//;
353365863862SAndy Whitcroft			# '*'s should not have spaces between.
3534f9a0b3d1SAndy Whitcroft			while ($to =~ s/\*\s+\*/\*\*/) {
353565863862SAndy Whitcroft			}
353665863862SAndy Whitcroft			# Modifiers should have spaces.
353765863862SAndy Whitcroft			$to =~ s/(\b$Modifier$)/$1 /;
353865863862SAndy Whitcroft
35393705ce5bSJoe Perches##			print "2: from<$from> to<$to> ident<$ident>\n";
3540667026e7SAndy Whitcroft			if ($from ne $to && $ident !~ /^$Modifier$/) {
35413705ce5bSJoe Perches				if (ERROR("POINTER_LOCATION",
35423705ce5bSJoe Perches					  "\"foo${from}bar\" should be \"foo${to}bar\"\n" .  $herecurr) &&
35433705ce5bSJoe Perches				    $fix) {
35443705ce5bSJoe Perches
35453705ce5bSJoe Perches					my $sub_from = $match;
35463705ce5bSJoe Perches					my $sub_to = $match;
35473705ce5bSJoe Perches					$sub_to =~ s/\Q$from\E/$to/;
3548194f66fcSJoe Perches					$fixed[$fixlinenr] =~
35493705ce5bSJoe Perches					    s@\Q$sub_from\E@$sub_to@;
35503705ce5bSJoe Perches				}
355165863862SAndy Whitcroft			}
35520a920b5bSAndy Whitcroft		}
35530a920b5bSAndy Whitcroft
35549d3e3c70SJoe Perches# avoid BUG() or BUG_ON()
35559d3e3c70SJoe Perches		if ($line =~ /\b(?:BUG|BUG_ON)\b/) {
35569d3e3c70SJoe Perches			my $msg_type = \&WARN;
35579d3e3c70SJoe Perches			$msg_type = \&CHK if ($file);
35589d3e3c70SJoe Perches			&{$msg_type}("AVOID_BUG",
35599d3e3c70SJoe Perches				     "Avoid crashing the kernel - try using WARN_ON & recovery code rather than BUG() or BUG_ON()\n" . $herecurr);
35609d3e3c70SJoe Perches		}
35610a920b5bSAndy Whitcroft
35629d3e3c70SJoe Perches# avoid LINUX_VERSION_CODE
35638905a67cSAndy Whitcroft		if ($line =~ /\bLINUX_VERSION_CODE\b/) {
3564000d1cc1SJoe Perches			WARN("LINUX_VERSION_CODE",
3565000d1cc1SJoe Perches			     "LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
35668905a67cSAndy Whitcroft		}
35678905a67cSAndy Whitcroft
356817441227SJoe Perches# check for uses of printk_ratelimit
356917441227SJoe Perches		if ($line =~ /\bprintk_ratelimit\s*\(/) {
3570000d1cc1SJoe Perches			WARN("PRINTK_RATELIMITED",
3571000d1cc1SJoe Perches			     "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
357217441227SJoe Perches		}
357317441227SJoe Perches
357400df344fSAndy Whitcroft# printk should use KERN_* levels.  Note that follow on printk's on the
357500df344fSAndy Whitcroft# same line do not need a level, so we use the current block context
357600df344fSAndy Whitcroft# to try and find and validate the current printk.  In summary the current
357725985edcSLucas De Marchi# printk includes all preceding printk's which have no newline on the end.
357800df344fSAndy Whitcroft# we assume the first bad printk is the one to report.
3579f0a594c1SAndy Whitcroft		if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
358000df344fSAndy Whitcroft			my $ok = 0;
358100df344fSAndy Whitcroft			for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
358200df344fSAndy Whitcroft				#print "CHECK<$lines[$ln - 1]\n";
358325985edcSLucas De Marchi				# we have a preceding printk if it ends
358400df344fSAndy Whitcroft				# with "\n" ignore it, else it is to blame
358500df344fSAndy Whitcroft				if ($lines[$ln - 1] =~ m{\bprintk\(}) {
358600df344fSAndy Whitcroft					if ($rawlines[$ln - 1] !~ m{\\n"}) {
358700df344fSAndy Whitcroft						$ok = 1;
358800df344fSAndy Whitcroft					}
358900df344fSAndy Whitcroft					last;
359000df344fSAndy Whitcroft				}
359100df344fSAndy Whitcroft			}
359200df344fSAndy Whitcroft			if ($ok == 0) {
3593000d1cc1SJoe Perches				WARN("PRINTK_WITHOUT_KERN_LEVEL",
3594000d1cc1SJoe Perches				     "printk() should include KERN_ facility level\n" . $herecurr);
35950a920b5bSAndy Whitcroft			}
359600df344fSAndy Whitcroft		}
35970a920b5bSAndy Whitcroft
3598243f3803SJoe Perches		if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
3599243f3803SJoe Perches			my $orig = $1;
3600243f3803SJoe Perches			my $level = lc($orig);
3601243f3803SJoe Perches			$level = "warn" if ($level eq "warning");
36028f26b837SJoe Perches			my $level2 = $level;
36038f26b837SJoe Perches			$level2 = "dbg" if ($level eq "debug");
3604243f3803SJoe Perches			WARN("PREFER_PR_LEVEL",
3605daa8b059SYogesh Chaudhari			     "Prefer [subsystem eg: netdev]_$level2([subsystem]dev, ... then dev_$level2(dev, ... then pr_$level(...  to printk(KERN_$orig ...\n" . $herecurr);
3606243f3803SJoe Perches		}
3607243f3803SJoe Perches
3608243f3803SJoe Perches		if ($line =~ /\bpr_warning\s*\(/) {
3609d5e616fcSJoe Perches			if (WARN("PREFER_PR_LEVEL",
3610d5e616fcSJoe Perches				 "Prefer pr_warn(... to pr_warning(...\n" . $herecurr) &&
3611d5e616fcSJoe Perches			    $fix) {
3612194f66fcSJoe Perches				$fixed[$fixlinenr] =~
3613d5e616fcSJoe Perches				    s/\bpr_warning\b/pr_warn/;
3614d5e616fcSJoe Perches			}
3615243f3803SJoe Perches		}
3616243f3803SJoe Perches
3617dc139313SJoe Perches		if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
3618dc139313SJoe Perches			my $orig = $1;
3619dc139313SJoe Perches			my $level = lc($orig);
3620dc139313SJoe Perches			$level = "warn" if ($level eq "warning");
3621dc139313SJoe Perches			$level = "dbg" if ($level eq "debug");
3622dc139313SJoe Perches			WARN("PREFER_DEV_LEVEL",
3623dc139313SJoe Perches			     "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
3624dc139313SJoe Perches		}
3625dc139313SJoe Perches
362691c9afafSAndy Lutomirski# ENOSYS means "bad syscall nr" and nothing else.  This will have a small
362791c9afafSAndy Lutomirski# number of false positives, but assembly files are not checked, so at
362891c9afafSAndy Lutomirski# least the arch entry code will not trigger this warning.
362991c9afafSAndy Lutomirski		if ($line =~ /\bENOSYS\b/) {
363091c9afafSAndy Lutomirski			WARN("ENOSYS",
363191c9afafSAndy Lutomirski			     "ENOSYS means 'invalid syscall nr' and nothing else\n" . $herecurr);
363291c9afafSAndy Lutomirski		}
363391c9afafSAndy Lutomirski
3634653d4876SAndy Whitcroft# function brace can't be on same line, except for #defines of do while,
3635653d4876SAndy Whitcroft# or if closed on same line
36368d182478SJoe Perches		if (($line=~/$Type\s*$Ident\(.*\).*\s*{/) and
36374e5d56bdSEddie Kovsky		    !($line=~/\#\s*define.*do\s\{/) and !($line=~/}/)) {
36388d182478SJoe Perches			if (ERROR("OPEN_BRACE",
36398d182478SJoe Perches				  "open brace '{' following function declarations go on the next line\n" . $herecurr) &&
36408d182478SJoe Perches			    $fix) {
36418d182478SJoe Perches				fix_delete_line($fixlinenr, $rawline);
36428d182478SJoe Perches				my $fixed_line = $rawline;
36438d182478SJoe Perches				$fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*){(.*)$/;
36448d182478SJoe Perches				my $line1 = $1;
36458d182478SJoe Perches				my $line2 = $2;
36468d182478SJoe Perches				fix_insert_line($fixlinenr, ltrim($line1));
36478d182478SJoe Perches				fix_insert_line($fixlinenr, "\+{");
36488d182478SJoe Perches				if ($line2 !~ /^\s*$/) {
36498d182478SJoe Perches					fix_insert_line($fixlinenr, "\+\t" . trim($line2));
36508d182478SJoe Perches				}
36518d182478SJoe Perches			}
36520a920b5bSAndy Whitcroft		}
3653653d4876SAndy Whitcroft
36548905a67cSAndy Whitcroft# open braces for enum, union and struct go on the same line.
36558905a67cSAndy Whitcroft		if ($line =~ /^.\s*{/ &&
36568905a67cSAndy Whitcroft		    $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
36578d182478SJoe Perches			if (ERROR("OPEN_BRACE",
36588d182478SJoe Perches				  "open brace '{' following $1 go on the same line\n" . $hereprev) &&
36598d182478SJoe Perches			    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
36608d182478SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
36618d182478SJoe Perches				fix_delete_line($fixlinenr, $rawline);
36628d182478SJoe Perches				my $fixedline = rtrim($prevrawline) . " {";
36638d182478SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
36648d182478SJoe Perches				$fixedline = $rawline;
36658d182478SJoe Perches				$fixedline =~ s/^(.\s*){\s*/$1\t/;
36668d182478SJoe Perches				if ($fixedline !~ /^\+\s*$/) {
36678d182478SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
36688d182478SJoe Perches				}
36698d182478SJoe Perches			}
36708905a67cSAndy Whitcroft		}
36718905a67cSAndy Whitcroft
36720c73b4ebSAndy Whitcroft# missing space after union, struct or enum definition
36733705ce5bSJoe Perches		if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
36743705ce5bSJoe Perches			if (WARN("SPACING",
36753705ce5bSJoe Perches				 "missing space after $1 definition\n" . $herecurr) &&
36763705ce5bSJoe Perches			    $fix) {
3677194f66fcSJoe Perches				$fixed[$fixlinenr] =~
36783705ce5bSJoe Perches				    s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
36793705ce5bSJoe Perches			}
36800c73b4ebSAndy Whitcroft		}
36810c73b4ebSAndy Whitcroft
368231070b5dSJoe Perches# Function pointer declarations
368331070b5dSJoe Perches# check spacing between type, funcptr, and args
368431070b5dSJoe Perches# canonical declaration is "type (*funcptr)(args...)"
368591f72e9cSJoe Perches		if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) {
368631070b5dSJoe Perches			my $declare = $1;
368731070b5dSJoe Perches			my $pre_pointer_space = $2;
368831070b5dSJoe Perches			my $post_pointer_space = $3;
368931070b5dSJoe Perches			my $funcname = $4;
369031070b5dSJoe Perches			my $post_funcname_space = $5;
369131070b5dSJoe Perches			my $pre_args_space = $6;
369231070b5dSJoe Perches
369391f72e9cSJoe Perches# the $Declare variable will capture all spaces after the type
369491f72e9cSJoe Perches# so check it for a missing trailing missing space but pointer return types
369591f72e9cSJoe Perches# don't need a space so don't warn for those.
369691f72e9cSJoe Perches			my $post_declare_space = "";
369791f72e9cSJoe Perches			if ($declare =~ /(\s+)$/) {
369891f72e9cSJoe Perches				$post_declare_space = $1;
369991f72e9cSJoe Perches				$declare = rtrim($declare);
370091f72e9cSJoe Perches			}
370191f72e9cSJoe Perches			if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) {
370231070b5dSJoe Perches				WARN("SPACING",
370331070b5dSJoe Perches				     "missing space after return type\n" . $herecurr);
370491f72e9cSJoe Perches				$post_declare_space = " ";
370531070b5dSJoe Perches			}
370631070b5dSJoe Perches
370731070b5dSJoe Perches# unnecessary space "type  (*funcptr)(args...)"
370891f72e9cSJoe Perches# This test is not currently implemented because these declarations are
370991f72e9cSJoe Perches# equivalent to
371091f72e9cSJoe Perches#	int  foo(int bar, ...)
371191f72e9cSJoe Perches# and this is form shouldn't/doesn't generate a checkpatch warning.
371291f72e9cSJoe Perches#
371391f72e9cSJoe Perches#			elsif ($declare =~ /\s{2,}$/) {
371491f72e9cSJoe Perches#				WARN("SPACING",
371591f72e9cSJoe Perches#				     "Multiple spaces after return type\n" . $herecurr);
371691f72e9cSJoe Perches#			}
371731070b5dSJoe Perches
371831070b5dSJoe Perches# unnecessary space "type ( *funcptr)(args...)"
371931070b5dSJoe Perches			if (defined $pre_pointer_space &&
372031070b5dSJoe Perches			    $pre_pointer_space =~ /^\s/) {
372131070b5dSJoe Perches				WARN("SPACING",
372231070b5dSJoe Perches				     "Unnecessary space after function pointer open parenthesis\n" . $herecurr);
372331070b5dSJoe Perches			}
372431070b5dSJoe Perches
372531070b5dSJoe Perches# unnecessary space "type (* funcptr)(args...)"
372631070b5dSJoe Perches			if (defined $post_pointer_space &&
372731070b5dSJoe Perches			    $post_pointer_space =~ /^\s/) {
372831070b5dSJoe Perches				WARN("SPACING",
372931070b5dSJoe Perches				     "Unnecessary space before function pointer name\n" . $herecurr);
373031070b5dSJoe Perches			}
373131070b5dSJoe Perches
373231070b5dSJoe Perches# unnecessary space "type (*funcptr )(args...)"
373331070b5dSJoe Perches			if (defined $post_funcname_space &&
373431070b5dSJoe Perches			    $post_funcname_space =~ /^\s/) {
373531070b5dSJoe Perches				WARN("SPACING",
373631070b5dSJoe Perches				     "Unnecessary space after function pointer name\n" . $herecurr);
373731070b5dSJoe Perches			}
373831070b5dSJoe Perches
373931070b5dSJoe Perches# unnecessary space "type (*funcptr) (args...)"
374031070b5dSJoe Perches			if (defined $pre_args_space &&
374131070b5dSJoe Perches			    $pre_args_space =~ /^\s/) {
374231070b5dSJoe Perches				WARN("SPACING",
374331070b5dSJoe Perches				     "Unnecessary space before function pointer arguments\n" . $herecurr);
374431070b5dSJoe Perches			}
374531070b5dSJoe Perches
374631070b5dSJoe Perches			if (show_type("SPACING") && $fix) {
3747194f66fcSJoe Perches				$fixed[$fixlinenr] =~
374891f72e9cSJoe Perches				    s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex;
374931070b5dSJoe Perches			}
375031070b5dSJoe Perches		}
375131070b5dSJoe Perches
37528d31cfceSAndy Whitcroft# check for spacing round square brackets; allowed:
37538d31cfceSAndy Whitcroft#  1. with a type on the left -- int [] a;
3754fe2a7dbcSAndy Whitcroft#  2. at the beginning of a line for slice initialisers -- [0...10] = 5,
3755fe2a7dbcSAndy Whitcroft#  3. inside a curly brace -- = { [0...10] = 5 }
37568d31cfceSAndy Whitcroft		while ($line =~ /(.*?\s)\[/g) {
37578d31cfceSAndy Whitcroft			my ($where, $prefix) = ($-[1], $1);
37588d31cfceSAndy Whitcroft			if ($prefix !~ /$Type\s+$/ &&
3759fe2a7dbcSAndy Whitcroft			    ($where != 0 || $prefix !~ /^.\s+$/) &&
3760daebc534SAndy Whitcroft			    $prefix !~ /[{,]\s+$/) {
37613705ce5bSJoe Perches				if (ERROR("BRACKET_SPACE",
37623705ce5bSJoe Perches					  "space prohibited before open square bracket '['\n" . $herecurr) &&
37633705ce5bSJoe Perches				    $fix) {
3764194f66fcSJoe Perches				    $fixed[$fixlinenr] =~
37653705ce5bSJoe Perches					s/^(\+.*?)\s+\[/$1\[/;
37663705ce5bSJoe Perches				}
37678d31cfceSAndy Whitcroft			}
37688d31cfceSAndy Whitcroft		}
37698d31cfceSAndy Whitcroft
3770f0a594c1SAndy Whitcroft# check for spaces between functions and their parentheses.
37716c72ffaaSAndy Whitcroft		while ($line =~ /($Ident)\s+\(/g) {
3772c2fdda0dSAndy Whitcroft			my $name = $1;
3773773647a0SAndy Whitcroft			my $ctx_before = substr($line, 0, $-[1]);
3774773647a0SAndy Whitcroft			my $ctx = "$ctx_before$name";
3775c2fdda0dSAndy Whitcroft
3776c2fdda0dSAndy Whitcroft			# Ignore those directives where spaces _are_ permitted.
3777773647a0SAndy Whitcroft			if ($name =~ /^(?:
3778773647a0SAndy Whitcroft				if|for|while|switch|return|case|
3779773647a0SAndy Whitcroft				volatile|__volatile__|
3780773647a0SAndy Whitcroft				__attribute__|format|__extension__|
3781773647a0SAndy Whitcroft				asm|__asm__)$/x)
3782773647a0SAndy Whitcroft			{
3783c2fdda0dSAndy Whitcroft			# cpp #define statements have non-optional spaces, ie
3784c2fdda0dSAndy Whitcroft			# if there is a space between the name and the open
3785c2fdda0dSAndy Whitcroft			# parenthesis it is simply not a parameter group.
3786c45dcabdSAndy Whitcroft			} elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
3787773647a0SAndy Whitcroft
3788773647a0SAndy Whitcroft			# cpp #elif statement condition may start with a (
3789c45dcabdSAndy Whitcroft			} elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
3790c2fdda0dSAndy Whitcroft
3791c2fdda0dSAndy Whitcroft			# If this whole things ends with a type its most
3792c2fdda0dSAndy Whitcroft			# likely a typedef for a function.
3793773647a0SAndy Whitcroft			} elsif ($ctx =~ /$Type$/) {
3794c2fdda0dSAndy Whitcroft
3795c2fdda0dSAndy Whitcroft			} else {
37963705ce5bSJoe Perches				if (WARN("SPACING",
37973705ce5bSJoe Perches					 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
37983705ce5bSJoe Perches					     $fix) {
3799194f66fcSJoe Perches					$fixed[$fixlinenr] =~
38003705ce5bSJoe Perches					    s/\b$name\s+\(/$name\(/;
38013705ce5bSJoe Perches				}
3802f0a594c1SAndy Whitcroft			}
38036c72ffaaSAndy Whitcroft		}
38049a4cad4eSEric Nelson
3805653d4876SAndy Whitcroft# Check operator spacing.
38060a920b5bSAndy Whitcroft		if (!($line=~/\#\s*include/)) {
38073705ce5bSJoe Perches			my $fixed_line = "";
38083705ce5bSJoe Perches			my $line_fixed = 0;
38093705ce5bSJoe Perches
38109c0ca6f9SAndy Whitcroft			my $ops = qr{
38119c0ca6f9SAndy Whitcroft				<<=|>>=|<=|>=|==|!=|
38129c0ca6f9SAndy Whitcroft				\+=|-=|\*=|\/=|%=|\^=|\|=|&=|
38139c0ca6f9SAndy Whitcroft				=>|->|<<|>>|<|>|=|!|~|
38141f65f947SAndy Whitcroft				&&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
381584731623SJoe Perches				\?:|\?|:
38169c0ca6f9SAndy Whitcroft			}x;
3817cf655043SAndy Whitcroft			my @elements = split(/($ops|;)/, $opline);
38183705ce5bSJoe Perches
38193705ce5bSJoe Perches##			print("element count: <" . $#elements . ">\n");
38203705ce5bSJoe Perches##			foreach my $el (@elements) {
38213705ce5bSJoe Perches##				print("el: <$el>\n");
38223705ce5bSJoe Perches##			}
38233705ce5bSJoe Perches
38243705ce5bSJoe Perches			my @fix_elements = ();
382500df344fSAndy Whitcroft			my $off = 0;
38266c72ffaaSAndy Whitcroft
38273705ce5bSJoe Perches			foreach my $el (@elements) {
38283705ce5bSJoe Perches				push(@fix_elements, substr($rawline, $off, length($el)));
38293705ce5bSJoe Perches				$off += length($el);
38303705ce5bSJoe Perches			}
38313705ce5bSJoe Perches
38323705ce5bSJoe Perches			$off = 0;
38333705ce5bSJoe Perches
38346c72ffaaSAndy Whitcroft			my $blank = copy_spacing($opline);
3835b34c648bSJoe Perches			my $last_after = -1;
38366c72ffaaSAndy Whitcroft
38370a920b5bSAndy Whitcroft			for (my $n = 0; $n < $#elements; $n += 2) {
38383705ce5bSJoe Perches
38393705ce5bSJoe Perches				my $good = $fix_elements[$n] . $fix_elements[$n + 1];
38403705ce5bSJoe Perches
38413705ce5bSJoe Perches##				print("n: <$n> good: <$good>\n");
38423705ce5bSJoe Perches
38434a0df2efSAndy Whitcroft				$off += length($elements[$n]);
38444a0df2efSAndy Whitcroft
384525985edcSLucas De Marchi				# Pick up the preceding and succeeding characters.
3846773647a0SAndy Whitcroft				my $ca = substr($opline, 0, $off);
3847773647a0SAndy Whitcroft				my $cc = '';
3848773647a0SAndy Whitcroft				if (length($opline) >= ($off + length($elements[$n + 1]))) {
3849773647a0SAndy Whitcroft					$cc = substr($opline, $off + length($elements[$n + 1]));
3850773647a0SAndy Whitcroft				}
3851773647a0SAndy Whitcroft				my $cb = "$ca$;$cc";
3852773647a0SAndy Whitcroft
38534a0df2efSAndy Whitcroft				my $a = '';
38544a0df2efSAndy Whitcroft				$a = 'V' if ($elements[$n] ne '');
38554a0df2efSAndy Whitcroft				$a = 'W' if ($elements[$n] =~ /\s$/);
3856cf655043SAndy Whitcroft				$a = 'C' if ($elements[$n] =~ /$;$/);
38574a0df2efSAndy Whitcroft				$a = 'B' if ($elements[$n] =~ /(\[|\()$/);
38584a0df2efSAndy Whitcroft				$a = 'O' if ($elements[$n] eq '');
3859773647a0SAndy Whitcroft				$a = 'E' if ($ca =~ /^\s*$/);
38604a0df2efSAndy Whitcroft
38610a920b5bSAndy Whitcroft				my $op = $elements[$n + 1];
38624a0df2efSAndy Whitcroft
38634a0df2efSAndy Whitcroft				my $c = '';
38640a920b5bSAndy Whitcroft				if (defined $elements[$n + 2]) {
38654a0df2efSAndy Whitcroft					$c = 'V' if ($elements[$n + 2] ne '');
38664a0df2efSAndy Whitcroft					$c = 'W' if ($elements[$n + 2] =~ /^\s/);
3867cf655043SAndy Whitcroft					$c = 'C' if ($elements[$n + 2] =~ /^$;/);
38684a0df2efSAndy Whitcroft					$c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
38694a0df2efSAndy Whitcroft					$c = 'O' if ($elements[$n + 2] eq '');
38708b1b3378SAndy Whitcroft					$c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
38714a0df2efSAndy Whitcroft				} else {
38724a0df2efSAndy Whitcroft					$c = 'E';
38730a920b5bSAndy Whitcroft				}
38740a920b5bSAndy Whitcroft
38754a0df2efSAndy Whitcroft				my $ctx = "${a}x${c}";
38764a0df2efSAndy Whitcroft
38774a0df2efSAndy Whitcroft				my $at = "(ctx:$ctx)";
38784a0df2efSAndy Whitcroft
38796c72ffaaSAndy Whitcroft				my $ptr = substr($blank, 0, $off) . "^";
3880de7d4f0eSAndy Whitcroft				my $hereptr = "$hereline$ptr\n";
38810a920b5bSAndy Whitcroft
388274048ed8SAndy Whitcroft				# Pull out the value of this operator.
38836c72ffaaSAndy Whitcroft				my $op_type = substr($curr_values, $off + 1, 1);
38840a920b5bSAndy Whitcroft
38851f65f947SAndy Whitcroft				# Get the full operator variant.
38861f65f947SAndy Whitcroft				my $opv = $op . substr($curr_vars, $off, 1);
38871f65f947SAndy Whitcroft
388813214adfSAndy Whitcroft				# Ignore operators passed as parameters.
388913214adfSAndy Whitcroft				if ($op_type ne 'V' &&
3890d7fe8065SSam Bobroff				    $ca =~ /\s$/ && $cc =~ /^\s*[,\)]/) {
389113214adfSAndy Whitcroft
3892cf655043SAndy Whitcroft#				# Ignore comments
3893cf655043SAndy Whitcroft#				} elsif ($op =~ /^$;+$/) {
389413214adfSAndy Whitcroft
3895d8aaf121SAndy Whitcroft				# ; should have either the end of line or a space or \ after it
389613214adfSAndy Whitcroft				} elsif ($op eq ';') {
3897cf655043SAndy Whitcroft					if ($ctx !~ /.x[WEBC]/ &&
3898cf655043SAndy Whitcroft					    $cc !~ /^\\/ && $cc !~ /^;/) {
38993705ce5bSJoe Perches						if (ERROR("SPACING",
39003705ce5bSJoe Perches							  "space required after that '$op' $at\n" . $hereptr)) {
3901b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
39023705ce5bSJoe Perches							$line_fixed = 1;
39033705ce5bSJoe Perches						}
3904d8aaf121SAndy Whitcroft					}
3905d8aaf121SAndy Whitcroft
3906d8aaf121SAndy Whitcroft				# // is a comment
3907d8aaf121SAndy Whitcroft				} elsif ($op eq '//') {
39080a920b5bSAndy Whitcroft
3909b00e4814SJoe Perches				#   :   when part of a bitfield
3910b00e4814SJoe Perches				} elsif ($opv eq ':B') {
3911b00e4814SJoe Perches					# skip the bitfield test for now
3912b00e4814SJoe Perches
39131f65f947SAndy Whitcroft				# No spaces for:
39141f65f947SAndy Whitcroft				#   ->
3915b00e4814SJoe Perches				} elsif ($op eq '->') {
39164a0df2efSAndy Whitcroft					if ($ctx =~ /Wx.|.xW/) {
39173705ce5bSJoe Perches						if (ERROR("SPACING",
39183705ce5bSJoe Perches							  "spaces prohibited around that '$op' $at\n" . $hereptr)) {
3919b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
39203705ce5bSJoe Perches							if (defined $fix_elements[$n + 2]) {
39213705ce5bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
39223705ce5bSJoe Perches							}
3923b34c648bSJoe Perches							$line_fixed = 1;
39243705ce5bSJoe Perches						}
39250a920b5bSAndy Whitcroft					}
39260a920b5bSAndy Whitcroft
39272381097bSJoe Perches				# , must not have a space before and must have a space on the right.
39280a920b5bSAndy Whitcroft				} elsif ($op eq ',') {
39292381097bSJoe Perches					my $rtrim_before = 0;
39302381097bSJoe Perches					my $space_after = 0;
39312381097bSJoe Perches					if ($ctx =~ /Wx./) {
39322381097bSJoe Perches						if (ERROR("SPACING",
39332381097bSJoe Perches							  "space prohibited before that '$op' $at\n" . $hereptr)) {
39342381097bSJoe Perches							$line_fixed = 1;
39352381097bSJoe Perches							$rtrim_before = 1;
39362381097bSJoe Perches						}
39372381097bSJoe Perches					}
3938cf655043SAndy Whitcroft					if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
39393705ce5bSJoe Perches						if (ERROR("SPACING",
39403705ce5bSJoe Perches							  "space required after that '$op' $at\n" . $hereptr)) {
39413705ce5bSJoe Perches							$line_fixed = 1;
3942b34c648bSJoe Perches							$last_after = $n;
39432381097bSJoe Perches							$space_after = 1;
39442381097bSJoe Perches						}
39452381097bSJoe Perches					}
39462381097bSJoe Perches					if ($rtrim_before || $space_after) {
39472381097bSJoe Perches						if ($rtrim_before) {
39482381097bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
39492381097bSJoe Perches						} else {
39502381097bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
39512381097bSJoe Perches						}
39522381097bSJoe Perches						if ($space_after) {
39532381097bSJoe Perches							$good .= " ";
39543705ce5bSJoe Perches						}
39550a920b5bSAndy Whitcroft					}
39560a920b5bSAndy Whitcroft
39579c0ca6f9SAndy Whitcroft				# '*' as part of a type definition -- reported already.
395874048ed8SAndy Whitcroft				} elsif ($opv eq '*_') {
39599c0ca6f9SAndy Whitcroft					#warn "'*' is part of type\n";
39609c0ca6f9SAndy Whitcroft
39619c0ca6f9SAndy Whitcroft				# unary operators should have a space before and
39629c0ca6f9SAndy Whitcroft				# none after.  May be left adjacent to another
39639c0ca6f9SAndy Whitcroft				# unary operator, or a cast
39649c0ca6f9SAndy Whitcroft				} elsif ($op eq '!' || $op eq '~' ||
396574048ed8SAndy Whitcroft					 $opv eq '*U' || $opv eq '-U' ||
39660d413866SAndy Whitcroft					 $opv eq '&U' || $opv eq '&&U') {
3967cf655043SAndy Whitcroft					if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
39683705ce5bSJoe Perches						if (ERROR("SPACING",
39693705ce5bSJoe Perches							  "space required before that '$op' $at\n" . $hereptr)) {
3970b34c648bSJoe Perches							if ($n != $last_after + 2) {
3971b34c648bSJoe Perches								$good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]);
39723705ce5bSJoe Perches								$line_fixed = 1;
39733705ce5bSJoe Perches							}
39740a920b5bSAndy Whitcroft						}
3975b34c648bSJoe Perches					}
3976a3340b35SAndy Whitcroft					if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
3977171ae1a4SAndy Whitcroft						# A unary '*' may be const
3978171ae1a4SAndy Whitcroft
3979171ae1a4SAndy Whitcroft					} elsif ($ctx =~ /.xW/) {
39803705ce5bSJoe Perches						if (ERROR("SPACING",
39813705ce5bSJoe Perches							  "space prohibited after that '$op' $at\n" . $hereptr)) {
3982b34c648bSJoe Perches							$good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]);
39833705ce5bSJoe Perches							if (defined $fix_elements[$n + 2]) {
39843705ce5bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
39853705ce5bSJoe Perches							}
3986b34c648bSJoe Perches							$line_fixed = 1;
39873705ce5bSJoe Perches						}
39880a920b5bSAndy Whitcroft					}
39890a920b5bSAndy Whitcroft
39900a920b5bSAndy Whitcroft				# unary ++ and unary -- are allowed no space on one side.
39910a920b5bSAndy Whitcroft				} elsif ($op eq '++' or $op eq '--') {
3992773647a0SAndy Whitcroft					if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
39933705ce5bSJoe Perches						if (ERROR("SPACING",
39943705ce5bSJoe Perches							  "space required one side of that '$op' $at\n" . $hereptr)) {
3995b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
39963705ce5bSJoe Perches							$line_fixed = 1;
39973705ce5bSJoe Perches						}
39980a920b5bSAndy Whitcroft					}
3999773647a0SAndy Whitcroft					if ($ctx =~ /Wx[BE]/ ||
4000773647a0SAndy Whitcroft					    ($ctx =~ /Wx./ && $cc =~ /^;/)) {
40013705ce5bSJoe Perches						if (ERROR("SPACING",
40023705ce5bSJoe Perches							  "space prohibited before that '$op' $at\n" . $hereptr)) {
4003b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
40043705ce5bSJoe Perches							$line_fixed = 1;
40053705ce5bSJoe Perches						}
4006653d4876SAndy Whitcroft					}
4007773647a0SAndy Whitcroft					if ($ctx =~ /ExW/) {
40083705ce5bSJoe Perches						if (ERROR("SPACING",
40093705ce5bSJoe Perches							  "space prohibited after that '$op' $at\n" . $hereptr)) {
4010b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
40113705ce5bSJoe Perches							if (defined $fix_elements[$n + 2]) {
40123705ce5bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
4013773647a0SAndy Whitcroft							}
4014b34c648bSJoe Perches							$line_fixed = 1;
40153705ce5bSJoe Perches						}
40163705ce5bSJoe Perches					}
40170a920b5bSAndy Whitcroft
40180a920b5bSAndy Whitcroft				# << and >> may either have or not have spaces both sides
40199c0ca6f9SAndy Whitcroft				} elsif ($op eq '<<' or $op eq '>>' or
40209c0ca6f9SAndy Whitcroft					 $op eq '&' or $op eq '^' or $op eq '|' or
40219c0ca6f9SAndy Whitcroft					 $op eq '+' or $op eq '-' or
4022c2fdda0dSAndy Whitcroft					 $op eq '*' or $op eq '/' or
4023c2fdda0dSAndy Whitcroft					 $op eq '%')
40240a920b5bSAndy Whitcroft				{
4025d2e025f3SJoe Perches					if ($check) {
4026d2e025f3SJoe Perches						if (defined $fix_elements[$n + 2] && $ctx !~ /[EW]x[EW]/) {
4027d2e025f3SJoe Perches							if (CHK("SPACING",
4028d2e025f3SJoe Perches								"spaces preferred around that '$op' $at\n" . $hereptr)) {
4029d2e025f3SJoe Perches								$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4030d2e025f3SJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
4031d2e025f3SJoe Perches								$line_fixed = 1;
4032d2e025f3SJoe Perches							}
4033d2e025f3SJoe Perches						} elsif (!defined $fix_elements[$n + 2] && $ctx !~ /Wx[OE]/) {
4034d2e025f3SJoe Perches							if (CHK("SPACING",
4035d2e025f3SJoe Perches								"space preferred before that '$op' $at\n" . $hereptr)) {
4036d2e025f3SJoe Perches								$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]);
4037d2e025f3SJoe Perches								$line_fixed = 1;
4038d2e025f3SJoe Perches							}
4039d2e025f3SJoe Perches						}
4040d2e025f3SJoe Perches					} elsif ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
40413705ce5bSJoe Perches						if (ERROR("SPACING",
40423705ce5bSJoe Perches							  "need consistent spacing around '$op' $at\n" . $hereptr)) {
4043b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4044b34c648bSJoe Perches							if (defined $fix_elements[$n + 2]) {
4045b34c648bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
4046b34c648bSJoe Perches							}
40473705ce5bSJoe Perches							$line_fixed = 1;
40483705ce5bSJoe Perches						}
40490a920b5bSAndy Whitcroft					}
40500a920b5bSAndy Whitcroft
40511f65f947SAndy Whitcroft				# A colon needs no spaces before when it is
40521f65f947SAndy Whitcroft				# terminating a case value or a label.
40531f65f947SAndy Whitcroft				} elsif ($opv eq ':C' || $opv eq ':L') {
40541f65f947SAndy Whitcroft					if ($ctx =~ /Wx./) {
40553705ce5bSJoe Perches						if (ERROR("SPACING",
40563705ce5bSJoe Perches							  "space prohibited before that '$op' $at\n" . $hereptr)) {
4057b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
40583705ce5bSJoe Perches							$line_fixed = 1;
40593705ce5bSJoe Perches						}
40601f65f947SAndy Whitcroft					}
40611f65f947SAndy Whitcroft
40620a920b5bSAndy Whitcroft				# All the others need spaces both sides.
4063cf655043SAndy Whitcroft				} elsif ($ctx !~ /[EWC]x[CWE]/) {
40641f65f947SAndy Whitcroft					my $ok = 0;
40651f65f947SAndy Whitcroft
406622f2a2efSAndy Whitcroft					# Ignore email addresses <foo@bar>
40671f65f947SAndy Whitcroft					if (($op eq '<' &&
40681f65f947SAndy Whitcroft					     $cc =~ /^\S+\@\S+>/) ||
40691f65f947SAndy Whitcroft					    ($op eq '>' &&
40701f65f947SAndy Whitcroft					     $ca =~ /<\S+\@\S+$/))
40711f65f947SAndy Whitcroft					{
40721f65f947SAndy Whitcroft					    	$ok = 1;
40731f65f947SAndy Whitcroft					}
40741f65f947SAndy Whitcroft
4075e0df7e1fSJoe Perches					# for asm volatile statements
4076e0df7e1fSJoe Perches					# ignore a colon with another
4077e0df7e1fSJoe Perches					# colon immediately before or after
4078e0df7e1fSJoe Perches					if (($op eq ':') &&
4079e0df7e1fSJoe Perches					    ($ca =~ /:$/ || $cc =~ /^:/)) {
4080e0df7e1fSJoe Perches						$ok = 1;
4081e0df7e1fSJoe Perches					}
4082e0df7e1fSJoe Perches
408384731623SJoe Perches					# messages are ERROR, but ?: are CHK
40841f65f947SAndy Whitcroft					if ($ok == 0) {
408584731623SJoe Perches						my $msg_type = \&ERROR;
408684731623SJoe Perches						$msg_type = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
408784731623SJoe Perches
408884731623SJoe Perches						if (&{$msg_type}("SPACING",
40893705ce5bSJoe Perches								 "spaces required around that '$op' $at\n" . $hereptr)) {
4090b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4091b34c648bSJoe Perches							if (defined $fix_elements[$n + 2]) {
4092b34c648bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
4093b34c648bSJoe Perches							}
40943705ce5bSJoe Perches							$line_fixed = 1;
40953705ce5bSJoe Perches						}
40960a920b5bSAndy Whitcroft					}
409722f2a2efSAndy Whitcroft				}
40984a0df2efSAndy Whitcroft				$off += length($elements[$n + 1]);
40993705ce5bSJoe Perches
41003705ce5bSJoe Perches##				print("n: <$n> GOOD: <$good>\n");
41013705ce5bSJoe Perches
41023705ce5bSJoe Perches				$fixed_line = $fixed_line . $good;
41030a920b5bSAndy Whitcroft			}
41043705ce5bSJoe Perches
41053705ce5bSJoe Perches			if (($#elements % 2) == 0) {
41063705ce5bSJoe Perches				$fixed_line = $fixed_line . $fix_elements[$#elements];
41073705ce5bSJoe Perches			}
41083705ce5bSJoe Perches
4109194f66fcSJoe Perches			if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) {
4110194f66fcSJoe Perches				$fixed[$fixlinenr] = $fixed_line;
41113705ce5bSJoe Perches			}
41123705ce5bSJoe Perches
41133705ce5bSJoe Perches
41140a920b5bSAndy Whitcroft		}
41150a920b5bSAndy Whitcroft
4116786b6326SJoe Perches# check for whitespace before a non-naked semicolon
4117d2e248e7SJoe Perches		if ($line =~ /^\+.*\S\s+;\s*$/) {
4118786b6326SJoe Perches			if (WARN("SPACING",
4119786b6326SJoe Perches				 "space prohibited before semicolon\n" . $herecurr) &&
4120786b6326SJoe Perches			    $fix) {
4121194f66fcSJoe Perches				1 while $fixed[$fixlinenr] =~
4122786b6326SJoe Perches				    s/^(\+.*\S)\s+;/$1;/;
4123786b6326SJoe Perches			}
4124786b6326SJoe Perches		}
4125786b6326SJoe Perches
4126f0a594c1SAndy Whitcroft# check for multiple assignments
4127f0a594c1SAndy Whitcroft		if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
4128000d1cc1SJoe Perches			CHK("MULTIPLE_ASSIGNMENTS",
4129000d1cc1SJoe Perches			    "multiple assignments should be avoided\n" . $herecurr);
4130f0a594c1SAndy Whitcroft		}
4131f0a594c1SAndy Whitcroft
413222f2a2efSAndy Whitcroft## # check for multiple declarations, allowing for a function declaration
413322f2a2efSAndy Whitcroft## # continuation.
413422f2a2efSAndy Whitcroft## 		if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
413522f2a2efSAndy Whitcroft## 		    $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
413622f2a2efSAndy Whitcroft##
413722f2a2efSAndy Whitcroft## 			# Remove any bracketed sections to ensure we do not
413822f2a2efSAndy Whitcroft## 			# falsly report the parameters of functions.
413922f2a2efSAndy Whitcroft## 			my $ln = $line;
414022f2a2efSAndy Whitcroft## 			while ($ln =~ s/\([^\(\)]*\)//g) {
414122f2a2efSAndy Whitcroft## 			}
414222f2a2efSAndy Whitcroft## 			if ($ln =~ /,/) {
4143000d1cc1SJoe Perches## 				WARN("MULTIPLE_DECLARATION",
4144000d1cc1SJoe Perches##				     "declaring multiple variables together should be avoided\n" . $herecurr);
414522f2a2efSAndy Whitcroft## 			}
414622f2a2efSAndy Whitcroft## 		}
4147f0a594c1SAndy Whitcroft
41480a920b5bSAndy Whitcroft#need space before brace following if, while, etc
41496b8c69e4SGeyslan G. Bem		if (($line =~ /\(.*\)\{/ && $line !~ /\($Type\)\{/) ||
41504e5d56bdSEddie Kovsky		    $line =~ /do\{/) {
41513705ce5bSJoe Perches			if (ERROR("SPACING",
41523705ce5bSJoe Perches				  "space required before the open brace '{'\n" . $herecurr) &&
41533705ce5bSJoe Perches			    $fix) {
4154194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/^(\+.*(?:do|\))){/$1 {/;
41553705ce5bSJoe Perches			}
4156de7d4f0eSAndy Whitcroft		}
4157de7d4f0eSAndy Whitcroft
4158c4a62ef9SJoe Perches## # check for blank lines before declarations
4159c4a62ef9SJoe Perches##		if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
4160c4a62ef9SJoe Perches##		    $prevrawline =~ /^.\s*$/) {
4161c4a62ef9SJoe Perches##			WARN("SPACING",
4162c4a62ef9SJoe Perches##			     "No blank lines before declarations\n" . $hereprev);
4163c4a62ef9SJoe Perches##		}
4164c4a62ef9SJoe Perches##
4165c4a62ef9SJoe Perches
4166de7d4f0eSAndy Whitcroft# closing brace should have a space following it when it has anything
4167de7d4f0eSAndy Whitcroft# on the line
4168de7d4f0eSAndy Whitcroft		if ($line =~ /}(?!(?:,|;|\)))\S/) {
4169d5e616fcSJoe Perches			if (ERROR("SPACING",
4170d5e616fcSJoe Perches				  "space required after that close brace '}'\n" . $herecurr) &&
4171d5e616fcSJoe Perches			    $fix) {
4172194f66fcSJoe Perches				$fixed[$fixlinenr] =~
4173d5e616fcSJoe Perches				    s/}((?!(?:,|;|\)))\S)/} $1/;
4174d5e616fcSJoe Perches			}
41750a920b5bSAndy Whitcroft		}
41760a920b5bSAndy Whitcroft
417722f2a2efSAndy Whitcroft# check spacing on square brackets
417822f2a2efSAndy Whitcroft		if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
41793705ce5bSJoe Perches			if (ERROR("SPACING",
41803705ce5bSJoe Perches				  "space prohibited after that open square bracket '['\n" . $herecurr) &&
41813705ce5bSJoe Perches			    $fix) {
4182194f66fcSJoe Perches				$fixed[$fixlinenr] =~
41833705ce5bSJoe Perches				    s/\[\s+/\[/;
41843705ce5bSJoe Perches			}
418522f2a2efSAndy Whitcroft		}
418622f2a2efSAndy Whitcroft		if ($line =~ /\s\]/) {
41873705ce5bSJoe Perches			if (ERROR("SPACING",
41883705ce5bSJoe Perches				  "space prohibited before that close square bracket ']'\n" . $herecurr) &&
41893705ce5bSJoe Perches			    $fix) {
4190194f66fcSJoe Perches				$fixed[$fixlinenr] =~
41913705ce5bSJoe Perches				    s/\s+\]/\]/;
41923705ce5bSJoe Perches			}
419322f2a2efSAndy Whitcroft		}
419422f2a2efSAndy Whitcroft
4195c45dcabdSAndy Whitcroft# check spacing on parentheses
41969c0ca6f9SAndy Whitcroft		if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
41979c0ca6f9SAndy Whitcroft		    $line !~ /for\s*\(\s+;/) {
41983705ce5bSJoe Perches			if (ERROR("SPACING",
41993705ce5bSJoe Perches				  "space prohibited after that open parenthesis '('\n" . $herecurr) &&
42003705ce5bSJoe Perches			    $fix) {
4201194f66fcSJoe Perches				$fixed[$fixlinenr] =~
42023705ce5bSJoe Perches				    s/\(\s+/\(/;
42033705ce5bSJoe Perches			}
420422f2a2efSAndy Whitcroft		}
420513214adfSAndy Whitcroft		if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
4206c45dcabdSAndy Whitcroft		    $line !~ /for\s*\(.*;\s+\)/ &&
4207c45dcabdSAndy Whitcroft		    $line !~ /:\s+\)/) {
42083705ce5bSJoe Perches			if (ERROR("SPACING",
42093705ce5bSJoe Perches				  "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
42103705ce5bSJoe Perches			    $fix) {
4211194f66fcSJoe Perches				$fixed[$fixlinenr] =~
42123705ce5bSJoe Perches				    s/\s+\)/\)/;
42133705ce5bSJoe Perches			}
421422f2a2efSAndy Whitcroft		}
421522f2a2efSAndy Whitcroft
4216e2826fd0SJoe Perches# check unnecessary parentheses around addressof/dereference single $Lvals
4217e2826fd0SJoe Perches# ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar
4218e2826fd0SJoe Perches
4219e2826fd0SJoe Perches		while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) {
4220ea4acbb1SJoe Perches			my $var = $1;
4221ea4acbb1SJoe Perches			if (CHK("UNNECESSARY_PARENTHESES",
4222ea4acbb1SJoe Perches				"Unnecessary parentheses around $var\n" . $herecurr) &&
4223ea4acbb1SJoe Perches			    $fix) {
4224ea4acbb1SJoe Perches				$fixed[$fixlinenr] =~ s/\(\s*\Q$var\E\s*\)/$var/;
4225ea4acbb1SJoe Perches			}
4226ea4acbb1SJoe Perches		}
4227ea4acbb1SJoe Perches
4228ea4acbb1SJoe Perches# check for unnecessary parentheses around function pointer uses
4229ea4acbb1SJoe Perches# ie: (foo->bar)(); should be foo->bar();
4230ea4acbb1SJoe Perches# but not "if (foo->bar) (" to avoid some false positives
4231ea4acbb1SJoe Perches		if ($line =~ /(\bif\s*|)(\(\s*$Ident\s*(?:$Member\s*)+\))[ \t]*\(/ && $1 !~ /^if/) {
4232ea4acbb1SJoe Perches			my $var = $2;
4233ea4acbb1SJoe Perches			if (CHK("UNNECESSARY_PARENTHESES",
4234ea4acbb1SJoe Perches				"Unnecessary parentheses around function pointer $var\n" . $herecurr) &&
4235ea4acbb1SJoe Perches			    $fix) {
4236ea4acbb1SJoe Perches				my $var2 = deparenthesize($var);
4237ea4acbb1SJoe Perches				$var2 =~ s/\s//g;
4238ea4acbb1SJoe Perches				$fixed[$fixlinenr] =~ s/\Q$var\E/$var2/;
4239ea4acbb1SJoe Perches			}
4240e2826fd0SJoe Perches		}
4241e2826fd0SJoe Perches
42420a920b5bSAndy Whitcroft#goto labels aren't indented, allow a single space however
42434a0df2efSAndy Whitcroft		if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
42440a920b5bSAndy Whitcroft		   !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
42453705ce5bSJoe Perches			if (WARN("INDENTED_LABEL",
42463705ce5bSJoe Perches				 "labels should not be indented\n" . $herecurr) &&
42473705ce5bSJoe Perches			    $fix) {
4248194f66fcSJoe Perches				$fixed[$fixlinenr] =~
42493705ce5bSJoe Perches				    s/^(.)\s+/$1/;
42503705ce5bSJoe Perches			}
42510a920b5bSAndy Whitcroft		}
42520a920b5bSAndy Whitcroft
42535b9553abSJoe Perches# return is not a function
4254507e5141SJoe Perches		if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {
4255c45dcabdSAndy Whitcroft			my $spacing = $1;
4256507e5141SJoe Perches			if ($^V && $^V ge 5.10.0 &&
42575b9553abSJoe Perches			    $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) {
42585b9553abSJoe Perches				my $value = $1;
42595b9553abSJoe Perches				$value = deparenthesize($value);
42605b9553abSJoe Perches				if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) {
4261000d1cc1SJoe Perches					ERROR("RETURN_PARENTHESES",
4262000d1cc1SJoe Perches					      "return is not a function, parentheses are not required\n" . $herecurr);
42635b9553abSJoe Perches				}
4264c45dcabdSAndy Whitcroft			} elsif ($spacing !~ /\s+/) {
4265000d1cc1SJoe Perches				ERROR("SPACING",
4266000d1cc1SJoe Perches				      "space required before the open parenthesis '('\n" . $herecurr);
4267c45dcabdSAndy Whitcroft			}
4268c45dcabdSAndy Whitcroft		}
4269507e5141SJoe Perches
4270b43ae21bSJoe Perches# unnecessary return in a void function
4271b43ae21bSJoe Perches# at end-of-function, with the previous line a single leading tab, then return;
4272b43ae21bSJoe Perches# and the line before that not a goto label target like "out:"
4273b43ae21bSJoe Perches		if ($sline =~ /^[ \+]}\s*$/ &&
4274b43ae21bSJoe Perches		    $prevline =~ /^\+\treturn\s*;\s*$/ &&
4275b43ae21bSJoe Perches		    $linenr >= 3 &&
4276b43ae21bSJoe Perches		    $lines[$linenr - 3] =~ /^[ +]/ &&
4277b43ae21bSJoe Perches		    $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) {
42789819cf25SJoe Perches			WARN("RETURN_VOID",
4279b43ae21bSJoe Perches			     "void function return statements are not generally useful\n" . $hereprev);
42809819cf25SJoe Perches               }
42819819cf25SJoe Perches
4282189248d8SJoe Perches# if statements using unnecessary parentheses - ie: if ((foo == bar))
4283189248d8SJoe Perches		if ($^V && $^V ge 5.10.0 &&
4284189248d8SJoe Perches		    $line =~ /\bif\s*((?:\(\s*){2,})/) {
4285189248d8SJoe Perches			my $openparens = $1;
4286189248d8SJoe Perches			my $count = $openparens =~ tr@\(@\(@;
4287189248d8SJoe Perches			my $msg = "";
4288189248d8SJoe Perches			if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) {
4289189248d8SJoe Perches				my $comp = $4;	#Not $1 because of $LvalOrFunc
4290189248d8SJoe Perches				$msg = " - maybe == should be = ?" if ($comp eq "==");
4291189248d8SJoe Perches				WARN("UNNECESSARY_PARENTHESES",
4292189248d8SJoe Perches				     "Unnecessary parentheses$msg\n" . $herecurr);
4293189248d8SJoe Perches			}
4294189248d8SJoe Perches		}
4295189248d8SJoe Perches
4296c5595fa2SJoe Perches# comparisons with a constant or upper case identifier on the left
4297c5595fa2SJoe Perches#	avoid cases like "foo + BAR < baz"
4298c5595fa2SJoe Perches#	only fix matches surrounded by parentheses to avoid incorrect
4299c5595fa2SJoe Perches#	conversions like "FOO < baz() + 5" being "misfixed" to "baz() > FOO + 5"
4300c5595fa2SJoe Perches		if ($^V && $^V ge 5.10.0 &&
4301c5595fa2SJoe Perches		    $line =~ /^\+(.*)\b($Constant|[A-Z_][A-Z0-9_]*)\s*($Compare)\s*($LvalOrFunc)/) {
4302c5595fa2SJoe Perches			my $lead = $1;
4303c5595fa2SJoe Perches			my $const = $2;
4304c5595fa2SJoe Perches			my $comp = $3;
4305c5595fa2SJoe Perches			my $to = $4;
4306c5595fa2SJoe Perches			my $newcomp = $comp;
4307f39e1769SJoe Perches			if ($lead !~ /(?:$Operators|\.)\s*$/ &&
4308c5595fa2SJoe Perches			    $to !~ /^(?:Constant|[A-Z_][A-Z0-9_]*)$/ &&
4309c5595fa2SJoe Perches			    WARN("CONSTANT_COMPARISON",
4310c5595fa2SJoe Perches				 "Comparisons should place the constant on the right side of the test\n" . $herecurr) &&
4311c5595fa2SJoe Perches			    $fix) {
4312c5595fa2SJoe Perches				if ($comp eq "<") {
4313c5595fa2SJoe Perches					$newcomp = ">";
4314c5595fa2SJoe Perches				} elsif ($comp eq "<=") {
4315c5595fa2SJoe Perches					$newcomp = ">=";
4316c5595fa2SJoe Perches				} elsif ($comp eq ">") {
4317c5595fa2SJoe Perches					$newcomp = "<";
4318c5595fa2SJoe Perches				} elsif ($comp eq ">=") {
4319c5595fa2SJoe Perches					$newcomp = "<=";
4320c5595fa2SJoe Perches				}
4321c5595fa2SJoe Perches				$fixed[$fixlinenr] =~ s/\(\s*\Q$const\E\s*$Compare\s*\Q$to\E\s*\)/($to $newcomp $const)/;
4322c5595fa2SJoe Perches			}
4323c5595fa2SJoe Perches		}
4324c5595fa2SJoe Perches
4325f34e4a4fSJoe Perches# Return of what appears to be an errno should normally be negative
4326f34e4a4fSJoe Perches		if ($sline =~ /\breturn(?:\s*\(+\s*|\s+)(E[A-Z]+)(?:\s*\)+\s*|\s*)[;:,]/) {
432753a3c448SAndy Whitcroft			my $name = $1;
432853a3c448SAndy Whitcroft			if ($name ne 'EOF' && $name ne 'ERROR') {
4329000d1cc1SJoe Perches				WARN("USE_NEGATIVE_ERRNO",
4330f34e4a4fSJoe Perches				     "return of an errno should typically be negative (ie: return -$1)\n" . $herecurr);
433153a3c448SAndy Whitcroft			}
433253a3c448SAndy Whitcroft		}
4333c45dcabdSAndy Whitcroft
43340a920b5bSAndy Whitcroft# Need a space before open parenthesis after if, while etc
43354a0df2efSAndy Whitcroft		if ($line =~ /\b(if|while|for|switch)\(/) {
43363705ce5bSJoe Perches			if (ERROR("SPACING",
43373705ce5bSJoe Perches				  "space required before the open parenthesis '('\n" . $herecurr) &&
43383705ce5bSJoe Perches			    $fix) {
4339194f66fcSJoe Perches				$fixed[$fixlinenr] =~
43403705ce5bSJoe Perches				    s/\b(if|while|for|switch)\(/$1 \(/;
43413705ce5bSJoe Perches			}
43420a920b5bSAndy Whitcroft		}
43430a920b5bSAndy Whitcroft
4344f5fe35ddSAndy Whitcroft# Check for illegal assignment in if conditional -- and check for trailing
4345f5fe35ddSAndy Whitcroft# statements after the conditional.
4346170d3a22SAndy Whitcroft		if ($line =~ /do\s*(?!{)/) {
43473e469cdcSAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
43483e469cdcSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0)
43493e469cdcSAndy Whitcroft					if (!defined $stat);
4350170d3a22SAndy Whitcroft			my ($stat_next) = ctx_statement_block($line_nr_next,
4351170d3a22SAndy Whitcroft						$remain_next, $off_next);
4352170d3a22SAndy Whitcroft			$stat_next =~ s/\n./\n /g;
4353170d3a22SAndy Whitcroft			##print "stat<$stat> stat_next<$stat_next>\n";
4354170d3a22SAndy Whitcroft
4355170d3a22SAndy Whitcroft			if ($stat_next =~ /^\s*while\b/) {
4356170d3a22SAndy Whitcroft				# If the statement carries leading newlines,
4357170d3a22SAndy Whitcroft				# then count those as offsets.
4358170d3a22SAndy Whitcroft				my ($whitespace) =
4359170d3a22SAndy Whitcroft					($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
4360170d3a22SAndy Whitcroft				my $offset =
4361170d3a22SAndy Whitcroft					statement_rawlines($whitespace) - 1;
4362170d3a22SAndy Whitcroft
4363170d3a22SAndy Whitcroft				$suppress_whiletrailers{$line_nr_next +
4364170d3a22SAndy Whitcroft								$offset} = 1;
4365170d3a22SAndy Whitcroft			}
4366170d3a22SAndy Whitcroft		}
4367170d3a22SAndy Whitcroft		if (!defined $suppress_whiletrailers{$linenr} &&
4368c11230f4SJoe Perches		    defined($stat) && defined($cond) &&
4369170d3a22SAndy Whitcroft		    $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
4370171ae1a4SAndy Whitcroft			my ($s, $c) = ($stat, $cond);
43718905a67cSAndy Whitcroft
4372b53c8e10SAndy Whitcroft			if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
4373000d1cc1SJoe Perches				ERROR("ASSIGN_IN_IF",
4374000d1cc1SJoe Perches				      "do not use assignment in if condition\n" . $herecurr);
43758905a67cSAndy Whitcroft			}
43768905a67cSAndy Whitcroft
43778905a67cSAndy Whitcroft			# Find out what is on the end of the line after the
43788905a67cSAndy Whitcroft			# conditional.
4379773647a0SAndy Whitcroft			substr($s, 0, length($c), '');
43808905a67cSAndy Whitcroft			$s =~ s/\n.*//g;
438113214adfSAndy Whitcroft			$s =~ s/$;//g; 	# Remove any comments
438253210168SAndy Whitcroft			if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
438353210168SAndy Whitcroft			    $c !~ /}\s*while\s*/)
4384773647a0SAndy Whitcroft			{
4385bb44ad39SAndy Whitcroft				# Find out how long the conditional actually is.
4386bb44ad39SAndy Whitcroft				my @newlines = ($c =~ /\n/gs);
4387bb44ad39SAndy Whitcroft				my $cond_lines = 1 + $#newlines;
438842bdf74cSHidetoshi Seto				my $stat_real = '';
4389bb44ad39SAndy Whitcroft
439042bdf74cSHidetoshi Seto				$stat_real = raw_line($linenr, $cond_lines)
439142bdf74cSHidetoshi Seto							. "\n" if ($cond_lines);
4392bb44ad39SAndy Whitcroft				if (defined($stat_real) && $cond_lines > 1) {
4393bb44ad39SAndy Whitcroft					$stat_real = "[...]\n$stat_real";
4394bb44ad39SAndy Whitcroft				}
4395bb44ad39SAndy Whitcroft
4396000d1cc1SJoe Perches				ERROR("TRAILING_STATEMENTS",
4397000d1cc1SJoe Perches				      "trailing statements should be on next line\n" . $herecurr . $stat_real);
43988905a67cSAndy Whitcroft			}
43998905a67cSAndy Whitcroft		}
44008905a67cSAndy Whitcroft
440113214adfSAndy Whitcroft# Check for bitwise tests written as boolean
440213214adfSAndy Whitcroft		if ($line =~ /
440313214adfSAndy Whitcroft			(?:
440413214adfSAndy Whitcroft				(?:\[|\(|\&\&|\|\|)
440513214adfSAndy Whitcroft				\s*0[xX][0-9]+\s*
440613214adfSAndy Whitcroft				(?:\&\&|\|\|)
440713214adfSAndy Whitcroft			|
440813214adfSAndy Whitcroft				(?:\&\&|\|\|)
440913214adfSAndy Whitcroft				\s*0[xX][0-9]+\s*
441013214adfSAndy Whitcroft				(?:\&\&|\|\||\)|\])
441113214adfSAndy Whitcroft			)/x)
441213214adfSAndy Whitcroft		{
4413000d1cc1SJoe Perches			WARN("HEXADECIMAL_BOOLEAN_TEST",
4414000d1cc1SJoe Perches			     "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
441513214adfSAndy Whitcroft		}
441613214adfSAndy Whitcroft
44178905a67cSAndy Whitcroft# if and else should not have general statements after it
441813214adfSAndy Whitcroft		if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
441913214adfSAndy Whitcroft			my $s = $1;
442013214adfSAndy Whitcroft			$s =~ s/$;//g; 	# Remove any comments
442113214adfSAndy Whitcroft			if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
4422000d1cc1SJoe Perches				ERROR("TRAILING_STATEMENTS",
4423000d1cc1SJoe Perches				      "trailing statements should be on next line\n" . $herecurr);
44240a920b5bSAndy Whitcroft			}
442513214adfSAndy Whitcroft		}
442639667782SAndy Whitcroft# if should not continue a brace
442739667782SAndy Whitcroft		if ($line =~ /}\s*if\b/) {
4428000d1cc1SJoe Perches			ERROR("TRAILING_STATEMENTS",
4429048b123fSRasmus Villemoes			      "trailing statements should be on next line (or did you mean 'else if'?)\n" .
443039667782SAndy Whitcroft				$herecurr);
443139667782SAndy Whitcroft		}
4432a1080bf8SAndy Whitcroft# case and default should not have general statements after them
4433a1080bf8SAndy Whitcroft		if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
4434a1080bf8SAndy Whitcroft		    $line !~ /\G(?:
44353fef12d6SAndy Whitcroft			(?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
4436a1080bf8SAndy Whitcroft			\s*return\s+
4437a1080bf8SAndy Whitcroft		    )/xg)
4438a1080bf8SAndy Whitcroft		{
4439000d1cc1SJoe Perches			ERROR("TRAILING_STATEMENTS",
4440000d1cc1SJoe Perches			      "trailing statements should be on next line\n" . $herecurr);
4441a1080bf8SAndy Whitcroft		}
44420a920b5bSAndy Whitcroft
44430a920b5bSAndy Whitcroft		# Check for }<nl>else {, these must be at the same
44440a920b5bSAndy Whitcroft		# indent level to be relevant to each other.
44458b8856f4SJoe Perches		if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ &&
44460a920b5bSAndy Whitcroft		    $previndent == $indent) {
44478b8856f4SJoe Perches			if (ERROR("ELSE_AFTER_BRACE",
44488b8856f4SJoe Perches				  "else should follow close brace '}'\n" . $hereprev) &&
44498b8856f4SJoe Perches			    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
44508b8856f4SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
44518b8856f4SJoe Perches				fix_delete_line($fixlinenr, $rawline);
44528b8856f4SJoe Perches				my $fixedline = $prevrawline;
44538b8856f4SJoe Perches				$fixedline =~ s/}\s*$//;
44548b8856f4SJoe Perches				if ($fixedline !~ /^\+\s*$/) {
44558b8856f4SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
44568b8856f4SJoe Perches				}
44578b8856f4SJoe Perches				$fixedline = $rawline;
44588b8856f4SJoe Perches				$fixedline =~ s/^(.\s*)else/$1} else/;
44598b8856f4SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
44608b8856f4SJoe Perches			}
44610a920b5bSAndy Whitcroft		}
44620a920b5bSAndy Whitcroft
44638b8856f4SJoe Perches		if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ &&
4464c2fdda0dSAndy Whitcroft		    $previndent == $indent) {
4465c2fdda0dSAndy Whitcroft			my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
4466c2fdda0dSAndy Whitcroft
4467c2fdda0dSAndy Whitcroft			# Find out what is on the end of the line after the
4468c2fdda0dSAndy Whitcroft			# conditional.
4469773647a0SAndy Whitcroft			substr($s, 0, length($c), '');
4470c2fdda0dSAndy Whitcroft			$s =~ s/\n.*//g;
4471c2fdda0dSAndy Whitcroft
4472c2fdda0dSAndy Whitcroft			if ($s =~ /^\s*;/) {
44738b8856f4SJoe Perches				if (ERROR("WHILE_AFTER_BRACE",
44748b8856f4SJoe Perches					  "while should follow close brace '}'\n" . $hereprev) &&
44758b8856f4SJoe Perches				    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
44768b8856f4SJoe Perches					fix_delete_line($fixlinenr - 1, $prevrawline);
44778b8856f4SJoe Perches					fix_delete_line($fixlinenr, $rawline);
44788b8856f4SJoe Perches					my $fixedline = $prevrawline;
44798b8856f4SJoe Perches					my $trailing = $rawline;
44808b8856f4SJoe Perches					$trailing =~ s/^\+//;
44818b8856f4SJoe Perches					$trailing = trim($trailing);
44828b8856f4SJoe Perches					$fixedline =~ s/}\s*$/} $trailing/;
44838b8856f4SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
44848b8856f4SJoe Perches				}
4485c2fdda0dSAndy Whitcroft			}
4486c2fdda0dSAndy Whitcroft		}
4487c2fdda0dSAndy Whitcroft
448895e2c602SJoe Perches#Specific variable tests
4489323c1260SJoe Perches		while ($line =~ m{($Constant|$Lval)}g) {
4490323c1260SJoe Perches			my $var = $1;
449195e2c602SJoe Perches
449295e2c602SJoe Perches#gcc binary extension
449395e2c602SJoe Perches			if ($var =~ /^$Binary$/) {
4494d5e616fcSJoe Perches				if (WARN("GCC_BINARY_CONSTANT",
4495d5e616fcSJoe Perches					 "Avoid gcc v4.3+ binary constant extension: <$var>\n" . $herecurr) &&
4496d5e616fcSJoe Perches				    $fix) {
4497d5e616fcSJoe Perches					my $hexval = sprintf("0x%x", oct($var));
4498194f66fcSJoe Perches					$fixed[$fixlinenr] =~
4499d5e616fcSJoe Perches					    s/\b$var\b/$hexval/;
4500d5e616fcSJoe Perches				}
450195e2c602SJoe Perches			}
450295e2c602SJoe Perches
450395e2c602SJoe Perches#CamelCase
4504807bd26cSJoe Perches			if ($var !~ /^$Constant$/ &&
4505be79794bSJoe Perches			    $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
450622735ce8SJoe Perches#Ignore Page<foo> variants
4507807bd26cSJoe Perches			    $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
450822735ce8SJoe Perches#Ignore SI style variants like nS, mV and dB (ie: max_uV, regulator_min_uA_show)
4509f5123576SJulius Werner			    $var !~ /^(?:[a-z_]*?)_?[a-z][A-Z](?:_[a-z_]+)?$/ &&
4510f5123576SJulius Werner#Ignore some three character SI units explicitly, like MiB and KHz
4511f5123576SJulius Werner			    $var !~ /^(?:[a-z_]*?)_?(?:[KMGT]iB|[KMGT]?Hz)(?:_[a-z_]+)?$/) {
45127e781f67SJoe Perches				while ($var =~ m{($Ident)}g) {
45137e781f67SJoe Perches					my $word = $1;
45147e781f67SJoe Perches					next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
4515d8b07710SJoe Perches					if ($check) {
4516d8b07710SJoe Perches						seed_camelcase_includes();
4517d8b07710SJoe Perches						if (!$file && !$camelcase_file_seeded) {
4518d8b07710SJoe Perches							seed_camelcase_file($realfile);
4519d8b07710SJoe Perches							$camelcase_file_seeded = 1;
4520d8b07710SJoe Perches						}
4521d8b07710SJoe Perches					}
45227e781f67SJoe Perches					if (!defined $camelcase{$word}) {
45237e781f67SJoe Perches						$camelcase{$word} = 1;
4524be79794bSJoe Perches						CHK("CAMELCASE",
45257e781f67SJoe Perches						    "Avoid CamelCase: <$word>\n" . $herecurr);
45267e781f67SJoe Perches					}
4527323c1260SJoe Perches				}
4528323c1260SJoe Perches			}
45293445686aSJoe Perches		}
45300a920b5bSAndy Whitcroft
45310a920b5bSAndy Whitcroft#no spaces allowed after \ in define
4532d5e616fcSJoe Perches		if ($line =~ /\#\s*define.*\\\s+$/) {
4533d5e616fcSJoe Perches			if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
4534d5e616fcSJoe Perches				 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
4535d5e616fcSJoe Perches			    $fix) {
4536194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\s+$//;
4537d5e616fcSJoe Perches			}
45380a920b5bSAndy Whitcroft		}
45390a920b5bSAndy Whitcroft
45400e212e0aSFabian Frederick# warn if <asm/foo.h> is #included and <linux/foo.h> is available and includes
45410e212e0aSFabian Frederick# itself <asm/foo.h> (uses RAW line)
4542c45dcabdSAndy Whitcroft		if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
4543e09dec48SAndy Whitcroft			my $file = "$1.h";
4544e09dec48SAndy Whitcroft			my $checkfile = "include/linux/$file";
4545e09dec48SAndy Whitcroft			if (-f "$root/$checkfile" &&
4546e09dec48SAndy Whitcroft			    $realfile ne $checkfile &&
45477840a94cSWolfram Sang			    $1 !~ /$allowed_asm_includes/)
4548c45dcabdSAndy Whitcroft			{
45490e212e0aSFabian Frederick				my $asminclude = `grep -Ec "#include\\s+<asm/$file>" $root/$checkfile`;
45500e212e0aSFabian Frederick				if ($asminclude > 0) {
4551e09dec48SAndy Whitcroft					if ($realfile =~ m{^arch/}) {
4552000d1cc1SJoe Perches						CHK("ARCH_INCLUDE_LINUX",
4553000d1cc1SJoe Perches						    "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
4554e09dec48SAndy Whitcroft					} else {
4555000d1cc1SJoe Perches						WARN("INCLUDE_LINUX",
4556000d1cc1SJoe Perches						     "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
4557e09dec48SAndy Whitcroft					}
45580a920b5bSAndy Whitcroft				}
45590a920b5bSAndy Whitcroft			}
45600e212e0aSFabian Frederick		}
45610a920b5bSAndy Whitcroft
4562653d4876SAndy Whitcroft# multi-statement macros should be enclosed in a do while loop, grab the
4563653d4876SAndy Whitcroft# first statement and ensure its the whole macro if its not enclosed
4564cf655043SAndy Whitcroft# in a known good container
4565b8f96a31SAndy Whitcroft		if ($realfile !~ m@/vmlinux.lds.h$@ &&
4566b8f96a31SAndy Whitcroft		    $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
4567d8aaf121SAndy Whitcroft			my $ln = $linenr;
4568d8aaf121SAndy Whitcroft			my $cnt = $realcnt;
4569c45dcabdSAndy Whitcroft			my ($off, $dstat, $dcond, $rest);
4570c45dcabdSAndy Whitcroft			my $ctx = '';
457108a2843eSJoe Perches			my $has_flow_statement = 0;
457208a2843eSJoe Perches			my $has_arg_concat = 0;
4573c45dcabdSAndy Whitcroft			($dstat, $dcond, $ln, $cnt, $off) =
4574f74bd194SAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0);
4575f74bd194SAndy Whitcroft			$ctx = $dstat;
4576c45dcabdSAndy Whitcroft			#print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
4577a3bb97a7SAndy Whitcroft			#print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
4578c45dcabdSAndy Whitcroft
457908a2843eSJoe Perches			$has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/);
458062e15a6dSJoe Perches			$has_arg_concat = 1 if ($ctx =~ /\#\#/ && $ctx !~ /\#\#\s*(?:__VA_ARGS__|args)\b/);
458108a2843eSJoe Perches
4582f74bd194SAndy Whitcroft			$dstat =~ s/^.\s*\#\s*define\s+$Ident(?:\([^\)]*\))?\s*//;
4583292f1a9bSAndy Whitcroft			$dstat =~ s/$;//g;
4584c45dcabdSAndy Whitcroft			$dstat =~ s/\\\n.//g;
4585c45dcabdSAndy Whitcroft			$dstat =~ s/^\s*//s;
4586c45dcabdSAndy Whitcroft			$dstat =~ s/\s*$//s;
4587c45dcabdSAndy Whitcroft
4588c45dcabdSAndy Whitcroft			# Flatten any parentheses and braces
4589bf30d6edSAndy Whitcroft			while ($dstat =~ s/\([^\(\)]*\)/1/ ||
4590bf30d6edSAndy Whitcroft			       $dstat =~ s/\{[^\{\}]*\}/1/ ||
45916b10df42SVladimir Zapolskiy			       $dstat =~ s/.\[[^\[\]]*\]/1/)
4592bf30d6edSAndy Whitcroft			{
4593c45dcabdSAndy Whitcroft			}
4594c45dcabdSAndy Whitcroft
4595e45bab8eSAndy Whitcroft			# Flatten any obvious string concatentation.
459633acb54aSJoe Perches			while ($dstat =~ s/($String)\s*$Ident/$1/ ||
459733acb54aSJoe Perches			       $dstat =~ s/$Ident\s*($String)/$1/)
4598e45bab8eSAndy Whitcroft			{
4599e45bab8eSAndy Whitcroft			}
4600e45bab8eSAndy Whitcroft
460142e15293SJoe Perches			# Make asm volatile uses seem like a generic function
460242e15293SJoe Perches			$dstat =~ s/\b_*asm_*\s+_*volatile_*\b/asm_volatile/g;
460342e15293SJoe Perches
4604c45dcabdSAndy Whitcroft			my $exceptions = qr{
4605c45dcabdSAndy Whitcroft				$Declare|
4606c45dcabdSAndy Whitcroft				module_param_named|
4607a0a0a7a9SKees Cook				MODULE_PARM_DESC|
4608c45dcabdSAndy Whitcroft				DECLARE_PER_CPU|
4609c45dcabdSAndy Whitcroft				DEFINE_PER_CPU|
4610383099fdSAndy Whitcroft				__typeof__\(|
461122fd2d3eSStefani Seibold				union|
461222fd2d3eSStefani Seibold				struct|
4613ea71a0a0SAndy Whitcroft				\.$Ident\s*=\s*|
46146b10df42SVladimir Zapolskiy				^\"|\"$|
46156b10df42SVladimir Zapolskiy				^\[
4616c45dcabdSAndy Whitcroft			}x;
46175eaa20b9SAndy Whitcroft			#print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
4618f74bd194SAndy Whitcroft			if ($dstat ne '' &&
4619f74bd194SAndy Whitcroft			    $dstat !~ /^(?:$Ident|-?$Constant),$/ &&			# 10, // foo(),
4620f74bd194SAndy Whitcroft			    $dstat !~ /^(?:$Ident|-?$Constant);$/ &&			# foo();
46213cc4b1c3SJoe Perches			    $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ &&		# 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
4622356fd398SJoe Perches			    $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ &&			# character constants
4623f74bd194SAndy Whitcroft			    $dstat !~ /$exceptions/ &&
4624f74bd194SAndy Whitcroft			    $dstat !~ /^\.$Ident\s*=/ &&				# .foo =
4625e942e2c3SJoe Perches			    $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ &&		# stringification #foo
462672f115f9SAndy Whitcroft			    $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ &&	# do {...} while (...); // do {...} while (...)
4627f74bd194SAndy Whitcroft			    $dstat !~ /^for\s*$Constant$/ &&				# for (...)
4628f74bd194SAndy Whitcroft			    $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ &&	# for (...) bar()
4629f74bd194SAndy Whitcroft			    $dstat !~ /^do\s*{/ &&					# do {...
46304e5d56bdSEddie Kovsky			    $dstat !~ /^\(\{/ &&						# ({...
4631f95a7e6aSJoe Perches			    $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
4632c45dcabdSAndy Whitcroft			{
4633f74bd194SAndy Whitcroft				$ctx =~ s/\n*$//;
4634f74bd194SAndy Whitcroft				my $herectx = $here . "\n";
4635f74bd194SAndy Whitcroft				my $cnt = statement_rawlines($ctx);
4636f74bd194SAndy Whitcroft
4637f74bd194SAndy Whitcroft				for (my $n = 0; $n < $cnt; $n++) {
4638f74bd194SAndy Whitcroft					$herectx .= raw_line($linenr, $n) . "\n";
4639c45dcabdSAndy Whitcroft				}
4640c45dcabdSAndy Whitcroft
4641f74bd194SAndy Whitcroft				if ($dstat =~ /;/) {
4642f74bd194SAndy Whitcroft					ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
4643f74bd194SAndy Whitcroft					      "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
4644f74bd194SAndy Whitcroft				} else {
4645000d1cc1SJoe Perches					ERROR("COMPLEX_MACRO",
4646388982b5SAndrew Morton					      "Macros with complex values should be enclosed in parentheses\n" . "$herectx");
4647d8aaf121SAndy Whitcroft				}
46480a920b5bSAndy Whitcroft			}
46495023d347SJoe Perches
465008a2843eSJoe Perches# check for macros with flow control, but without ## concatenation
465108a2843eSJoe Perches# ## concatenation is commonly a macro that defines a function so ignore those
465208a2843eSJoe Perches			if ($has_flow_statement && !$has_arg_concat) {
465308a2843eSJoe Perches				my $herectx = $here . "\n";
465408a2843eSJoe Perches				my $cnt = statement_rawlines($ctx);
465508a2843eSJoe Perches
465608a2843eSJoe Perches				for (my $n = 0; $n < $cnt; $n++) {
465708a2843eSJoe Perches					$herectx .= raw_line($linenr, $n) . "\n";
465808a2843eSJoe Perches				}
465908a2843eSJoe Perches				WARN("MACRO_WITH_FLOW_CONTROL",
466008a2843eSJoe Perches				     "Macros with flow control statements should be avoided\n" . "$herectx");
466108a2843eSJoe Perches			}
466208a2843eSJoe Perches
4663481eb486SJoe Perches# check for line continuations outside of #defines, preprocessor #, and asm
46645023d347SJoe Perches
46655023d347SJoe Perches		} else {
46665023d347SJoe Perches			if ($prevline !~ /^..*\\$/ &&
4667481eb486SJoe Perches			    $line !~ /^\+\s*\#.*\\$/ &&		# preprocessor
4668481eb486SJoe Perches			    $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ &&	# asm
46695023d347SJoe Perches			    $line =~ /^\+.*\\$/) {
46705023d347SJoe Perches				WARN("LINE_CONTINUATIONS",
46715023d347SJoe Perches				     "Avoid unnecessary line continuations\n" . $herecurr);
46725023d347SJoe Perches			}
4673653d4876SAndy Whitcroft		}
46740a920b5bSAndy Whitcroft
4675b13edf7fSJoe Perches# do {} while (0) macro tests:
4676b13edf7fSJoe Perches# single-statement macros do not need to be enclosed in do while (0) loop,
4677b13edf7fSJoe Perches# macro should not end with a semicolon
4678b13edf7fSJoe Perches		if ($^V && $^V ge 5.10.0 &&
4679b13edf7fSJoe Perches		    $realfile !~ m@/vmlinux.lds.h$@ &&
4680b13edf7fSJoe Perches		    $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
4681b13edf7fSJoe Perches			my $ln = $linenr;
4682b13edf7fSJoe Perches			my $cnt = $realcnt;
4683b13edf7fSJoe Perches			my ($off, $dstat, $dcond, $rest);
4684b13edf7fSJoe Perches			my $ctx = '';
4685b13edf7fSJoe Perches			($dstat, $dcond, $ln, $cnt, $off) =
4686b13edf7fSJoe Perches				ctx_statement_block($linenr, $realcnt, 0);
4687b13edf7fSJoe Perches			$ctx = $dstat;
4688b13edf7fSJoe Perches
4689b13edf7fSJoe Perches			$dstat =~ s/\\\n.//g;
46901b36b201SJoe Perches			$dstat =~ s/$;/ /g;
4691b13edf7fSJoe Perches
4692b13edf7fSJoe Perches			if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
4693b13edf7fSJoe Perches				my $stmts = $2;
4694b13edf7fSJoe Perches				my $semis = $3;
4695b13edf7fSJoe Perches
4696b13edf7fSJoe Perches				$ctx =~ s/\n*$//;
4697b13edf7fSJoe Perches				my $cnt = statement_rawlines($ctx);
4698b13edf7fSJoe Perches				my $herectx = $here . "\n";
4699b13edf7fSJoe Perches
4700b13edf7fSJoe Perches				for (my $n = 0; $n < $cnt; $n++) {
4701b13edf7fSJoe Perches					$herectx .= raw_line($linenr, $n) . "\n";
4702b13edf7fSJoe Perches				}
4703b13edf7fSJoe Perches
4704ac8e97f8SJoe Perches				if (($stmts =~ tr/;/;/) == 1 &&
4705ac8e97f8SJoe Perches				    $stmts !~ /^\s*(if|while|for|switch)\b/) {
4706b13edf7fSJoe Perches					WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
4707b13edf7fSJoe Perches					     "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
4708b13edf7fSJoe Perches				}
4709b13edf7fSJoe Perches				if (defined $semis && $semis ne "") {
4710b13edf7fSJoe Perches					WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
4711b13edf7fSJoe Perches					     "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
4712b13edf7fSJoe Perches				}
4713f5ef95b1SJoe Perches			} elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) {
4714f5ef95b1SJoe Perches				$ctx =~ s/\n*$//;
4715f5ef95b1SJoe Perches				my $cnt = statement_rawlines($ctx);
4716f5ef95b1SJoe Perches				my $herectx = $here . "\n";
4717f5ef95b1SJoe Perches
4718f5ef95b1SJoe Perches				for (my $n = 0; $n < $cnt; $n++) {
4719f5ef95b1SJoe Perches					$herectx .= raw_line($linenr, $n) . "\n";
4720f5ef95b1SJoe Perches				}
4721f5ef95b1SJoe Perches
4722f5ef95b1SJoe Perches				WARN("TRAILING_SEMICOLON",
4723f5ef95b1SJoe Perches				     "macros should not use a trailing semicolon\n" . "$herectx");
4724b13edf7fSJoe Perches			}
4725b13edf7fSJoe Perches		}
4726b13edf7fSJoe Perches
4727080ba929SMike Frysinger# make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
4728080ba929SMike Frysinger# all assignments may have only one of the following with an assignment:
4729080ba929SMike Frysinger#	.
4730080ba929SMike Frysinger#	ALIGN(...)
4731080ba929SMike Frysinger#	VMLINUX_SYMBOL(...)
4732080ba929SMike Frysinger		if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
4733000d1cc1SJoe Perches			WARN("MISSING_VMLINUX_SYMBOL",
4734000d1cc1SJoe Perches			     "vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
4735080ba929SMike Frysinger		}
4736080ba929SMike Frysinger
4737f0a594c1SAndy Whitcroft# check for redundant bracing round if etc
473813214adfSAndy Whitcroft		if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
473913214adfSAndy Whitcroft			my ($level, $endln, @chunks) =
4740cf655043SAndy Whitcroft				ctx_statement_full($linenr, $realcnt, 1);
474113214adfSAndy Whitcroft			#print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
4742cf655043SAndy Whitcroft			#print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
4743cf655043SAndy Whitcroft			if ($#chunks > 0 && $level == 0) {
4744aad4f614SJoe Perches				my @allowed = ();
4745aad4f614SJoe Perches				my $allow = 0;
474613214adfSAndy Whitcroft				my $seen = 0;
4747773647a0SAndy Whitcroft				my $herectx = $here . "\n";
4748cf655043SAndy Whitcroft				my $ln = $linenr - 1;
474913214adfSAndy Whitcroft				for my $chunk (@chunks) {
475013214adfSAndy Whitcroft					my ($cond, $block) = @{$chunk};
475113214adfSAndy Whitcroft
4752773647a0SAndy Whitcroft					# If the condition carries leading newlines, then count those as offsets.
4753773647a0SAndy Whitcroft					my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
4754773647a0SAndy Whitcroft					my $offset = statement_rawlines($whitespace) - 1;
4755773647a0SAndy Whitcroft
4756aad4f614SJoe Perches					$allowed[$allow] = 0;
4757773647a0SAndy Whitcroft					#print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
4758773647a0SAndy Whitcroft
4759773647a0SAndy Whitcroft					# We have looked at and allowed this specific line.
4760773647a0SAndy Whitcroft					$suppress_ifbraces{$ln + $offset} = 1;
4761773647a0SAndy Whitcroft
4762773647a0SAndy Whitcroft					$herectx .= "$rawlines[$ln + $offset]\n[...]\n";
4763cf655043SAndy Whitcroft					$ln += statement_rawlines($block) - 1;
4764cf655043SAndy Whitcroft
4765773647a0SAndy Whitcroft					substr($block, 0, length($cond), '');
476613214adfSAndy Whitcroft
476713214adfSAndy Whitcroft					$seen++ if ($block =~ /^\s*{/);
476813214adfSAndy Whitcroft
4769aad4f614SJoe Perches					#print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
4770cf655043SAndy Whitcroft					if (statement_lines($cond) > 1) {
4771cf655043SAndy Whitcroft						#print "APW: ALLOWED: cond<$cond>\n";
4772aad4f614SJoe Perches						$allowed[$allow] = 1;
477313214adfSAndy Whitcroft					}
477413214adfSAndy Whitcroft					if ($block =~/\b(?:if|for|while)\b/) {
4775cf655043SAndy Whitcroft						#print "APW: ALLOWED: block<$block>\n";
4776aad4f614SJoe Perches						$allowed[$allow] = 1;
477713214adfSAndy Whitcroft					}
4778cf655043SAndy Whitcroft					if (statement_block_size($block) > 1) {
4779cf655043SAndy Whitcroft						#print "APW: ALLOWED: lines block<$block>\n";
4780aad4f614SJoe Perches						$allowed[$allow] = 1;
478113214adfSAndy Whitcroft					}
4782aad4f614SJoe Perches					$allow++;
478313214adfSAndy Whitcroft				}
4784aad4f614SJoe Perches				if ($seen) {
4785aad4f614SJoe Perches					my $sum_allowed = 0;
4786aad4f614SJoe Perches					foreach (@allowed) {
4787aad4f614SJoe Perches						$sum_allowed += $_;
4788aad4f614SJoe Perches					}
4789aad4f614SJoe Perches					if ($sum_allowed == 0) {
4790000d1cc1SJoe Perches						WARN("BRACES",
4791000d1cc1SJoe Perches						     "braces {} are not necessary for any arm of this statement\n" . $herectx);
4792aad4f614SJoe Perches					} elsif ($sum_allowed != $allow &&
4793aad4f614SJoe Perches						 $seen != $allow) {
4794aad4f614SJoe Perches						CHK("BRACES",
4795aad4f614SJoe Perches						    "braces {} should be used on all arms of this statement\n" . $herectx);
4796aad4f614SJoe Perches					}
479713214adfSAndy Whitcroft				}
479813214adfSAndy Whitcroft			}
479913214adfSAndy Whitcroft		}
4800773647a0SAndy Whitcroft		if (!defined $suppress_ifbraces{$linenr - 1} &&
480113214adfSAndy Whitcroft					$line =~ /\b(if|while|for|else)\b/) {
4802cf655043SAndy Whitcroft			my $allowed = 0;
4803f0a594c1SAndy Whitcroft
4804cf655043SAndy Whitcroft			# Check the pre-context.
4805cf655043SAndy Whitcroft			if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
4806cf655043SAndy Whitcroft				#print "APW: ALLOWED: pre<$1>\n";
4807cf655043SAndy Whitcroft				$allowed = 1;
4808f0a594c1SAndy Whitcroft			}
4809773647a0SAndy Whitcroft
4810773647a0SAndy Whitcroft			my ($level, $endln, @chunks) =
4811773647a0SAndy Whitcroft				ctx_statement_full($linenr, $realcnt, $-[0]);
4812773647a0SAndy Whitcroft
4813cf655043SAndy Whitcroft			# Check the condition.
4814cf655043SAndy Whitcroft			my ($cond, $block) = @{$chunks[0]};
4815773647a0SAndy Whitcroft			#print "CHECKING<$linenr> cond<$cond> block<$block>\n";
4816cf655043SAndy Whitcroft			if (defined $cond) {
4817773647a0SAndy Whitcroft				substr($block, 0, length($cond), '');
4818cf655043SAndy Whitcroft			}
4819cf655043SAndy Whitcroft			if (statement_lines($cond) > 1) {
4820cf655043SAndy Whitcroft				#print "APW: ALLOWED: cond<$cond>\n";
4821cf655043SAndy Whitcroft				$allowed = 1;
4822cf655043SAndy Whitcroft			}
4823cf655043SAndy Whitcroft			if ($block =~/\b(?:if|for|while)\b/) {
4824cf655043SAndy Whitcroft				#print "APW: ALLOWED: block<$block>\n";
4825cf655043SAndy Whitcroft				$allowed = 1;
4826cf655043SAndy Whitcroft			}
4827cf655043SAndy Whitcroft			if (statement_block_size($block) > 1) {
4828cf655043SAndy Whitcroft				#print "APW: ALLOWED: lines block<$block>\n";
4829cf655043SAndy Whitcroft				$allowed = 1;
4830cf655043SAndy Whitcroft			}
4831cf655043SAndy Whitcroft			# Check the post-context.
4832cf655043SAndy Whitcroft			if (defined $chunks[1]) {
4833cf655043SAndy Whitcroft				my ($cond, $block) = @{$chunks[1]};
4834cf655043SAndy Whitcroft				if (defined $cond) {
4835773647a0SAndy Whitcroft					substr($block, 0, length($cond), '');
4836cf655043SAndy Whitcroft				}
4837cf655043SAndy Whitcroft				if ($block =~ /^\s*\{/) {
4838cf655043SAndy Whitcroft					#print "APW: ALLOWED: chunk-1 block<$block>\n";
4839cf655043SAndy Whitcroft					$allowed = 1;
4840cf655043SAndy Whitcroft				}
4841cf655043SAndy Whitcroft			}
4842cf655043SAndy Whitcroft			if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
484369932487SJustin P. Mattock				my $herectx = $here . "\n";
4844f055663cSAndy Whitcroft				my $cnt = statement_rawlines($block);
4845cf655043SAndy Whitcroft
4846f055663cSAndy Whitcroft				for (my $n = 0; $n < $cnt; $n++) {
484769932487SJustin P. Mattock					$herectx .= raw_line($linenr, $n) . "\n";
4848cf655043SAndy Whitcroft				}
4849cf655043SAndy Whitcroft
4850000d1cc1SJoe Perches				WARN("BRACES",
4851000d1cc1SJoe Perches				     "braces {} are not necessary for single statement blocks\n" . $herectx);
4852f0a594c1SAndy Whitcroft			}
4853f0a594c1SAndy Whitcroft		}
4854f0a594c1SAndy Whitcroft
48550979ae66SJoe Perches# check for unnecessary blank lines around braces
485677b9a53aSJoe Perches		if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
4857f8e58219SJoe Perches			if (CHK("BRACES",
4858f8e58219SJoe Perches				"Blank lines aren't necessary before a close brace '}'\n" . $hereprev) &&
4859f8e58219SJoe Perches			    $fix && $prevrawline =~ /^\+/) {
4860f8e58219SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
4861f8e58219SJoe Perches			}
48620979ae66SJoe Perches		}
486377b9a53aSJoe Perches		if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
4864f8e58219SJoe Perches			if (CHK("BRACES",
4865f8e58219SJoe Perches				"Blank lines aren't necessary after an open brace '{'\n" . $hereprev) &&
4866f8e58219SJoe Perches			    $fix) {
4867f8e58219SJoe Perches				fix_delete_line($fixlinenr, $rawline);
4868f8e58219SJoe Perches			}
48690979ae66SJoe Perches		}
48700979ae66SJoe Perches
48714a0df2efSAndy Whitcroft# no volatiles please
48726c72ffaaSAndy Whitcroft		my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
48736c72ffaaSAndy Whitcroft		if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
4874000d1cc1SJoe Perches			WARN("VOLATILE",
4875000d1cc1SJoe Perches			     "Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
48764a0df2efSAndy Whitcroft		}
48774a0df2efSAndy Whitcroft
48785e4f6ba5SJoe Perches# Check for user-visible strings broken across lines, which breaks the ability
48795e4f6ba5SJoe Perches# to grep for the string.  Make exceptions when the previous string ends in a
48805e4f6ba5SJoe Perches# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{'
48815e4f6ba5SJoe Perches# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value
488233acb54aSJoe Perches		if ($line =~ /^\+\s*$String/ &&
48835e4f6ba5SJoe Perches		    $prevline =~ /"\s*$/ &&
48845e4f6ba5SJoe Perches		    $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) {
48855e4f6ba5SJoe Perches			if (WARN("SPLIT_STRING",
48865e4f6ba5SJoe Perches				 "quoted string split across lines\n" . $hereprev) &&
48875e4f6ba5SJoe Perches				     $fix &&
48885e4f6ba5SJoe Perches				     $prevrawline =~ /^\+.*"\s*$/ &&
48895e4f6ba5SJoe Perches				     $last_coalesced_string_linenr != $linenr - 1) {
48905e4f6ba5SJoe Perches				my $extracted_string = get_quoted_string($line, $rawline);
48915e4f6ba5SJoe Perches				my $comma_close = "";
48925e4f6ba5SJoe Perches				if ($rawline =~ /\Q$extracted_string\E(\s*\)\s*;\s*$|\s*,\s*)/) {
48935e4f6ba5SJoe Perches					$comma_close = $1;
48945e4f6ba5SJoe Perches				}
48955e4f6ba5SJoe Perches
48965e4f6ba5SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
48975e4f6ba5SJoe Perches				fix_delete_line($fixlinenr, $rawline);
48985e4f6ba5SJoe Perches				my $fixedline = $prevrawline;
48995e4f6ba5SJoe Perches				$fixedline =~ s/"\s*$//;
49005e4f6ba5SJoe Perches				$fixedline .= substr($extracted_string, 1) . trim($comma_close);
49015e4f6ba5SJoe Perches				fix_insert_line($fixlinenr - 1, $fixedline);
49025e4f6ba5SJoe Perches				$fixedline = $rawline;
49035e4f6ba5SJoe Perches				$fixedline =~ s/\Q$extracted_string\E\Q$comma_close\E//;
49045e4f6ba5SJoe Perches				if ($fixedline !~ /\+\s*$/) {
49055e4f6ba5SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
49065e4f6ba5SJoe Perches				}
49075e4f6ba5SJoe Perches				$last_coalesced_string_linenr = $linenr;
49085e4f6ba5SJoe Perches			}
49095e4f6ba5SJoe Perches		}
49105e4f6ba5SJoe Perches
49115e4f6ba5SJoe Perches# check for missing a space in a string concatenation
49125e4f6ba5SJoe Perches		if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) {
49135e4f6ba5SJoe Perches			WARN('MISSING_SPACE',
49145e4f6ba5SJoe Perches			     "break quoted strings at a space character\n" . $hereprev);
49155e4f6ba5SJoe Perches		}
49165e4f6ba5SJoe Perches
49175e4f6ba5SJoe Perches# check for spaces before a quoted newline
49185e4f6ba5SJoe Perches		if ($rawline =~ /^.*\".*\s\\n/) {
49195e4f6ba5SJoe Perches			if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
49205e4f6ba5SJoe Perches				 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
49215e4f6ba5SJoe Perches			    $fix) {
49225e4f6ba5SJoe Perches				$fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
49235e4f6ba5SJoe Perches			}
49245e4f6ba5SJoe Perches
49255e4f6ba5SJoe Perches		}
49265e4f6ba5SJoe Perches
4927f17dba4fSJoe Perches# concatenated string without spaces between elements
492833acb54aSJoe Perches		if ($line =~ /$String[A-Z_]/ || $line =~ /[A-Za-z0-9_]$String/) {
4929f17dba4fSJoe Perches			CHK("CONCATENATED_STRING",
4930f17dba4fSJoe Perches			    "Concatenated strings should use spaces between elements\n" . $herecurr);
4931f17dba4fSJoe Perches		}
4932f17dba4fSJoe Perches
493390ad30e5SJoe Perches# uncoalesced string fragments
493433acb54aSJoe Perches		if ($line =~ /$String\s*"/) {
493590ad30e5SJoe Perches			WARN("STRING_FRAGMENTS",
493690ad30e5SJoe Perches			     "Consecutive strings are generally better as a single string\n" . $herecurr);
493790ad30e5SJoe Perches		}
493890ad30e5SJoe Perches
49396e300757SJoe Perches# check for %L{u,d,i} and 0x%[udi] in strings
49405e4f6ba5SJoe Perches		my $string;
49415e4f6ba5SJoe Perches		while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
49425e4f6ba5SJoe Perches			$string = substr($rawline, $-[1], $+[1] - $-[1]);
49435e4f6ba5SJoe Perches			$string =~ s/%%/__/g;
49446e300757SJoe Perches			if ($string =~ /(?<!%)%[\*\d\.\$]*L[udi]/) {
49455e4f6ba5SJoe Perches				WARN("PRINTF_L",
49465e4f6ba5SJoe Perches				     "\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
49475e4f6ba5SJoe Perches				last;
49485e4f6ba5SJoe Perches			}
49496e300757SJoe Perches			if ($string =~ /0x%[\*\d\.\$\Llzth]*[udi]/) {
49506e300757SJoe Perches				ERROR("PRINTF_0xDECIMAL",
49516e300757SJoe Perches				      "Prefixing 0x with decimal output is defective\n" . $herecurr);
49526e300757SJoe Perches			}
49535e4f6ba5SJoe Perches		}
49545e4f6ba5SJoe Perches
49555e4f6ba5SJoe Perches# check for line continuations in quoted strings with odd counts of "
49565e4f6ba5SJoe Perches		if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) {
49575e4f6ba5SJoe Perches			WARN("LINE_CONTINUATIONS",
49585e4f6ba5SJoe Perches			     "Avoid line continuations in quoted strings\n" . $herecurr);
49595e4f6ba5SJoe Perches		}
49605e4f6ba5SJoe Perches
496100df344fSAndy Whitcroft# warn about #if 0
4962c45dcabdSAndy Whitcroft		if ($line =~ /^.\s*\#\s*if\s+0\b/) {
4963000d1cc1SJoe Perches			CHK("REDUNDANT_CODE",
4964000d1cc1SJoe Perches			    "if this code is redundant consider removing it\n" .
4965de7d4f0eSAndy Whitcroft				$herecurr);
49664a0df2efSAndy Whitcroft		}
49674a0df2efSAndy Whitcroft
496803df4b51SAndy Whitcroft# check for needless "if (<foo>) fn(<foo>)" uses
496903df4b51SAndy Whitcroft		if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
4970100425deSJoe Perches			my $tested = quotemeta($1);
4971100425deSJoe Perches			my $expr = '\s*\(\s*' . $tested . '\s*\)\s*;';
4972100425deSJoe Perches			if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?|(?:kmem_cache|mempool|dma_pool)_destroy)$expr/) {
4973100425deSJoe Perches				my $func = $1;
4974100425deSJoe Perches				if (WARN('NEEDLESS_IF',
4975100425deSJoe Perches					 "$func(NULL) is safe and this check is probably not required\n" . $hereprev) &&
4976100425deSJoe Perches				    $fix) {
4977100425deSJoe Perches					my $do_fix = 1;
4978100425deSJoe Perches					my $leading_tabs = "";
4979100425deSJoe Perches					my $new_leading_tabs = "";
4980100425deSJoe Perches					if ($lines[$linenr - 2] =~ /^\+(\t*)if\s*\(\s*$tested\s*\)\s*$/) {
4981100425deSJoe Perches						$leading_tabs = $1;
4982100425deSJoe Perches					} else {
4983100425deSJoe Perches						$do_fix = 0;
4984100425deSJoe Perches					}
4985100425deSJoe Perches					if ($lines[$linenr - 1] =~ /^\+(\t+)$func\s*\(\s*$tested\s*\)\s*;\s*$/) {
4986100425deSJoe Perches						$new_leading_tabs = $1;
4987100425deSJoe Perches						if (length($leading_tabs) + 1 ne length($new_leading_tabs)) {
4988100425deSJoe Perches							$do_fix = 0;
4989100425deSJoe Perches						}
4990100425deSJoe Perches					} else {
4991100425deSJoe Perches						$do_fix = 0;
4992100425deSJoe Perches					}
4993100425deSJoe Perches					if ($do_fix) {
4994100425deSJoe Perches						fix_delete_line($fixlinenr - 1, $prevrawline);
4995100425deSJoe Perches						$fixed[$fixlinenr] =~ s/^\+$new_leading_tabs/\+$leading_tabs/;
4996100425deSJoe Perches					}
4997100425deSJoe Perches				}
49984c432a8fSGreg Kroah-Hartman			}
49994c432a8fSGreg Kroah-Hartman		}
5000f0a594c1SAndy Whitcroft
5001ebfdc409SJoe Perches# check for unnecessary "Out of Memory" messages
5002ebfdc409SJoe Perches		if ($line =~ /^\+.*\b$logFunctions\s*\(/ &&
5003ebfdc409SJoe Perches		    $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ &&
5004ebfdc409SJoe Perches		    (defined $1 || defined $3) &&
5005ebfdc409SJoe Perches		    $linenr > 3) {
5006ebfdc409SJoe Perches			my $testval = $2;
5007ebfdc409SJoe Perches			my $testline = $lines[$linenr - 3];
5008ebfdc409SJoe Perches
5009ebfdc409SJoe Perches			my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0);
5010ebfdc409SJoe Perches#			print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n");
5011ebfdc409SJoe Perches
5012ebfdc409SJoe 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)/) {
5013ebfdc409SJoe Perches				WARN("OOM_MESSAGE",
5014ebfdc409SJoe Perches				     "Possible unnecessary 'out of memory' message\n" . $hereprev);
5015ebfdc409SJoe Perches			}
5016ebfdc409SJoe Perches		}
5017ebfdc409SJoe Perches
5018f78d98f6SJoe Perches# check for logging functions with KERN_<LEVEL>
5019dcaf1123SPaolo Bonzini		if ($line !~ /printk(?:_ratelimited|_once)?\s*\(/ &&
5020f78d98f6SJoe Perches		    $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) {
5021f78d98f6SJoe Perches			my $level = $1;
5022f78d98f6SJoe Perches			if (WARN("UNNECESSARY_KERN_LEVEL",
5023f78d98f6SJoe Perches				 "Possible unnecessary $level\n" . $herecurr) &&
5024f78d98f6SJoe Perches			    $fix) {
5025f78d98f6SJoe Perches				$fixed[$fixlinenr] =~ s/\s*$level\s*//;
5026f78d98f6SJoe Perches			}
5027f78d98f6SJoe Perches		}
5028f78d98f6SJoe Perches
5029abb08a53SJoe Perches# check for mask then right shift without a parentheses
5030abb08a53SJoe Perches		if ($^V && $^V ge 5.10.0 &&
5031abb08a53SJoe Perches		    $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ &&
5032abb08a53SJoe Perches		    $4 !~ /^\&/) { # $LvalOrFunc may be &foo, ignore if so
5033abb08a53SJoe Perches			WARN("MASK_THEN_SHIFT",
5034abb08a53SJoe Perches			     "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr);
5035abb08a53SJoe Perches		}
5036abb08a53SJoe Perches
5037b75ac618SJoe Perches# check for pointer comparisons to NULL
5038b75ac618SJoe Perches		if ($^V && $^V ge 5.10.0) {
5039b75ac618SJoe Perches			while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) {
5040b75ac618SJoe Perches				my $val = $1;
5041b75ac618SJoe Perches				my $equal = "!";
5042b75ac618SJoe Perches				$equal = "" if ($4 eq "!=");
5043b75ac618SJoe Perches				if (CHK("COMPARISON_TO_NULL",
5044b75ac618SJoe Perches					"Comparison to NULL could be written \"${equal}${val}\"\n" . $herecurr) &&
5045b75ac618SJoe Perches					    $fix) {
5046b75ac618SJoe Perches					$fixed[$fixlinenr] =~ s/\b\Q$val\E\s*(?:==|\!=)\s*NULL\b/$equal$val/;
5047b75ac618SJoe Perches				}
5048b75ac618SJoe Perches			}
5049b75ac618SJoe Perches		}
5050b75ac618SJoe Perches
50518716de38SJoe Perches# check for bad placement of section $InitAttribute (e.g.: __initdata)
50528716de38SJoe Perches		if ($line =~ /(\b$InitAttribute\b)/) {
50538716de38SJoe Perches			my $attr = $1;
50548716de38SJoe Perches			if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
50558716de38SJoe Perches				my $ptr = $1;
50568716de38SJoe Perches				my $var = $2;
50578716de38SJoe Perches				if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
50588716de38SJoe Perches				      ERROR("MISPLACED_INIT",
50598716de38SJoe Perches					    "$attr should be placed after $var\n" . $herecurr)) ||
50608716de38SJoe Perches				     ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
50618716de38SJoe Perches				      WARN("MISPLACED_INIT",
50628716de38SJoe Perches					   "$attr should be placed after $var\n" . $herecurr))) &&
50638716de38SJoe Perches				    $fix) {
5064194f66fcSJoe 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;
50658716de38SJoe Perches				}
50668716de38SJoe Perches			}
50678716de38SJoe Perches		}
50688716de38SJoe Perches
5069e970b884SJoe Perches# check for $InitAttributeData (ie: __initdata) with const
5070e970b884SJoe Perches		if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
5071e970b884SJoe Perches			my $attr = $1;
5072e970b884SJoe Perches			$attr =~ /($InitAttributePrefix)(.*)/;
5073e970b884SJoe Perches			my $attr_prefix = $1;
5074e970b884SJoe Perches			my $attr_type = $2;
5075e970b884SJoe Perches			if (ERROR("INIT_ATTRIBUTE",
5076e970b884SJoe Perches				  "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
5077e970b884SJoe Perches			    $fix) {
5078194f66fcSJoe Perches				$fixed[$fixlinenr] =~
5079e970b884SJoe Perches				    s/$InitAttributeData/${attr_prefix}initconst/;
5080e970b884SJoe Perches			}
5081e970b884SJoe Perches		}
5082e970b884SJoe Perches
5083e970b884SJoe Perches# check for $InitAttributeConst (ie: __initconst) without const
5084e970b884SJoe Perches		if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
5085e970b884SJoe Perches			my $attr = $1;
5086e970b884SJoe Perches			if (ERROR("INIT_ATTRIBUTE",
5087e970b884SJoe Perches				  "Use of $attr requires a separate use of const\n" . $herecurr) &&
5088e970b884SJoe Perches			    $fix) {
5089194f66fcSJoe Perches				my $lead = $fixed[$fixlinenr] =~
5090e970b884SJoe Perches				    /(^\+\s*(?:static\s+))/;
5091e970b884SJoe Perches				$lead = rtrim($1);
5092e970b884SJoe Perches				$lead = "$lead " if ($lead !~ /^\+$/);
5093e970b884SJoe Perches				$lead = "${lead}const ";
5094194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/;
5095e970b884SJoe Perches			}
5096e970b884SJoe Perches		}
5097e970b884SJoe Perches
5098c17893c7SJoe Perches# check for __read_mostly with const non-pointer (should just be const)
5099c17893c7SJoe Perches		if ($line =~ /\b__read_mostly\b/ &&
5100c17893c7SJoe Perches		    $line =~ /($Type)\s*$Ident/ && $1 !~ /\*\s*$/ && $1 =~ /\bconst\b/) {
5101c17893c7SJoe Perches			if (ERROR("CONST_READ_MOSTLY",
5102c17893c7SJoe Perches				  "Invalid use of __read_mostly with const type\n" . $herecurr) &&
5103c17893c7SJoe Perches			    $fix) {
5104c17893c7SJoe Perches				$fixed[$fixlinenr] =~ s/\s+__read_mostly\b//;
5105c17893c7SJoe Perches			}
5106c17893c7SJoe Perches		}
5107c17893c7SJoe Perches
5108fbdb8138SJoe Perches# don't use __constant_<foo> functions outside of include/uapi/
5109fbdb8138SJoe Perches		if ($realfile !~ m@^include/uapi/@ &&
5110fbdb8138SJoe Perches		    $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) {
5111fbdb8138SJoe Perches			my $constant_func = $1;
5112fbdb8138SJoe Perches			my $func = $constant_func;
5113fbdb8138SJoe Perches			$func =~ s/^__constant_//;
5114fbdb8138SJoe Perches			if (WARN("CONSTANT_CONVERSION",
5115fbdb8138SJoe Perches				 "$constant_func should be $func\n" . $herecurr) &&
5116fbdb8138SJoe Perches			    $fix) {
5117194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g;
5118fbdb8138SJoe Perches			}
5119fbdb8138SJoe Perches		}
5120fbdb8138SJoe Perches
51211a15a250SPatrick Pannuto# prefer usleep_range over udelay
512237581c28SBruce Allan		if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
512343c1d77cSJoe Perches			my $delay = $1;
51241a15a250SPatrick Pannuto			# ignore udelay's < 10, however
512543c1d77cSJoe Perches			if (! ($delay < 10) ) {
5126000d1cc1SJoe Perches				CHK("USLEEP_RANGE",
512743c1d77cSJoe Perches				    "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $herecurr);
512843c1d77cSJoe Perches			}
512943c1d77cSJoe Perches			if ($delay > 2000) {
513043c1d77cSJoe Perches				WARN("LONG_UDELAY",
513143c1d77cSJoe Perches				     "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr);
51321a15a250SPatrick Pannuto			}
51331a15a250SPatrick Pannuto		}
51341a15a250SPatrick Pannuto
513509ef8725SPatrick Pannuto# warn about unexpectedly long msleep's
513609ef8725SPatrick Pannuto		if ($line =~ /\bmsleep\s*\((\d+)\);/) {
513709ef8725SPatrick Pannuto			if ($1 < 20) {
5138000d1cc1SJoe Perches				WARN("MSLEEP",
513943c1d77cSJoe Perches				     "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $herecurr);
514009ef8725SPatrick Pannuto			}
514109ef8725SPatrick Pannuto		}
514209ef8725SPatrick Pannuto
514336ec1939SJoe Perches# check for comparisons of jiffies
514436ec1939SJoe Perches		if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
514536ec1939SJoe Perches			WARN("JIFFIES_COMPARISON",
514636ec1939SJoe Perches			     "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
514736ec1939SJoe Perches		}
514836ec1939SJoe Perches
51499d7a34a5SJoe Perches# check for comparisons of get_jiffies_64()
51509d7a34a5SJoe Perches		if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
51519d7a34a5SJoe Perches			WARN("JIFFIES_COMPARISON",
51529d7a34a5SJoe Perches			     "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
51539d7a34a5SJoe Perches		}
51549d7a34a5SJoe Perches
515500df344fSAndy Whitcroft# warn about #ifdefs in C files
5156c45dcabdSAndy Whitcroft#		if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
515700df344fSAndy Whitcroft#			print "#ifdef in C files should be avoided\n";
515800df344fSAndy Whitcroft#			print "$herecurr";
515900df344fSAndy Whitcroft#			$clean = 0;
516000df344fSAndy Whitcroft#		}
516100df344fSAndy Whitcroft
516222f2a2efSAndy Whitcroft# warn about spacing in #ifdefs
5163c45dcabdSAndy Whitcroft		if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
51643705ce5bSJoe Perches			if (ERROR("SPACING",
51653705ce5bSJoe Perches				  "exactly one space required after that #$1\n" . $herecurr) &&
51663705ce5bSJoe Perches			    $fix) {
5167194f66fcSJoe Perches				$fixed[$fixlinenr] =~
51683705ce5bSJoe Perches				    s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
51693705ce5bSJoe Perches			}
51703705ce5bSJoe Perches
517122f2a2efSAndy Whitcroft		}
517222f2a2efSAndy Whitcroft
51734a0df2efSAndy Whitcroft# check for spinlock_t definitions without a comment.
5174171ae1a4SAndy Whitcroft		if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
5175171ae1a4SAndy Whitcroft		    $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
51764a0df2efSAndy Whitcroft			my $which = $1;
51774a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
5178000d1cc1SJoe Perches				CHK("UNCOMMENTED_DEFINITION",
5179000d1cc1SJoe Perches				    "$1 definition without comment\n" . $herecurr);
51804a0df2efSAndy Whitcroft			}
51814a0df2efSAndy Whitcroft		}
51824a0df2efSAndy Whitcroft# check for memory barriers without a comment.
5183402c2553SMichael S. Tsirkin
5184402c2553SMichael S. Tsirkin		my $barriers = qr{
5185402c2553SMichael S. Tsirkin			mb|
5186402c2553SMichael S. Tsirkin			rmb|
5187402c2553SMichael S. Tsirkin			wmb|
5188402c2553SMichael S. Tsirkin			read_barrier_depends
5189402c2553SMichael S. Tsirkin		}x;
5190402c2553SMichael S. Tsirkin		my $barrier_stems = qr{
5191402c2553SMichael S. Tsirkin			mb__before_atomic|
5192402c2553SMichael S. Tsirkin			mb__after_atomic|
5193402c2553SMichael S. Tsirkin			store_release|
5194402c2553SMichael S. Tsirkin			load_acquire|
5195402c2553SMichael S. Tsirkin			store_mb|
5196402c2553SMichael S. Tsirkin			(?:$barriers)
5197402c2553SMichael S. Tsirkin		}x;
5198402c2553SMichael S. Tsirkin		my $all_barriers = qr{
5199402c2553SMichael S. Tsirkin			(?:$barriers)|
520043e361f2SMichael S. Tsirkin			smp_(?:$barrier_stems)|
520143e361f2SMichael S. Tsirkin			virt_(?:$barrier_stems)
5202402c2553SMichael S. Tsirkin		}x;
5203402c2553SMichael S. Tsirkin
5204402c2553SMichael S. Tsirkin		if ($line =~ /\b(?:$all_barriers)\s*\(/) {
52054a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
5206c1fd7bb9SJoe Perches				WARN("MEMORY_BARRIER",
5207000d1cc1SJoe Perches				     "memory barrier without comment\n" . $herecurr);
52084a0df2efSAndy Whitcroft			}
52094a0df2efSAndy Whitcroft		}
52103ad81779SPaul E. McKenney
5211f4073b0fSMichael S. Tsirkin		my $underscore_smp_barriers = qr{__smp_(?:$barrier_stems)}x;
5212f4073b0fSMichael S. Tsirkin
5213f4073b0fSMichael S. Tsirkin		if ($realfile !~ m@^include/asm-generic/@ &&
5214f4073b0fSMichael S. Tsirkin		    $realfile !~ m@/barrier\.h$@ &&
5215f4073b0fSMichael S. Tsirkin		    $line =~ m/\b(?:$underscore_smp_barriers)\s*\(/ &&
5216f4073b0fSMichael S. Tsirkin		    $line !~ m/^.\s*\#\s*define\s+(?:$underscore_smp_barriers)\s*\(/) {
5217f4073b0fSMichael S. Tsirkin			WARN("MEMORY_BARRIER",
5218f4073b0fSMichael S. Tsirkin			     "__smp memory barriers shouldn't be used outside barrier.h and asm-generic\n" . $herecurr);
5219f4073b0fSMichael S. Tsirkin		}
5220f4073b0fSMichael S. Tsirkin
5221cb426e99SJoe Perches# check for waitqueue_active without a comment.
5222cb426e99SJoe Perches		if ($line =~ /\bwaitqueue_active\s*\(/) {
5223cb426e99SJoe Perches			if (!ctx_has_comment($first_line, $linenr)) {
5224cb426e99SJoe Perches				WARN("WAITQUEUE_ACTIVE",
5225cb426e99SJoe Perches				     "waitqueue_active without comment\n" . $herecurr);
5226cb426e99SJoe Perches			}
5227cb426e99SJoe Perches		}
52283ad81779SPaul E. McKenney
52293ad81779SPaul E. McKenney# Check for expedited grace periods that interrupt non-idle non-nohz
52303ad81779SPaul E. McKenney# online CPUs.  These expedited can therefore degrade real-time response
52313ad81779SPaul E. McKenney# if used carelessly, and should be avoided where not absolutely
52323ad81779SPaul E. McKenney# needed.  It is always OK to use synchronize_rcu_expedited() and
52333ad81779SPaul E. McKenney# synchronize_sched_expedited() at boot time (before real-time applications
52343ad81779SPaul E. McKenney# start) and in error situations where real-time response is compromised in
52353ad81779SPaul E. McKenney# any case.  Note that synchronize_srcu_expedited() does -not- interrupt
52363ad81779SPaul E. McKenney# other CPUs, so don't warn on uses of synchronize_srcu_expedited().
52373ad81779SPaul E. McKenney# Of course, nothing comes for free, and srcu_read_lock() and
52383ad81779SPaul E. McKenney# srcu_read_unlock() do contain full memory barriers in payment for
52393ad81779SPaul E. McKenney# synchronize_srcu_expedited() non-interruption properties.
52403ad81779SPaul E. McKenney		if ($line =~ /\b(synchronize_rcu_expedited|synchronize_sched_expedited)\(/) {
52413ad81779SPaul E. McKenney			WARN("EXPEDITED_RCU_GRACE_PERIOD",
52423ad81779SPaul E. McKenney			     "expedited RCU grace periods should be avoided where they can degrade real-time response\n" . $herecurr);
52433ad81779SPaul E. McKenney
52443ad81779SPaul E. McKenney		}
52453ad81779SPaul E. McKenney
52464a0df2efSAndy Whitcroft# check of hardware specific defines
5247c45dcabdSAndy Whitcroft		if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
5248000d1cc1SJoe Perches			CHK("ARCH_DEFINES",
5249000d1cc1SJoe Perches			    "architecture specific defines should be avoided\n" .  $herecurr);
52500a920b5bSAndy Whitcroft		}
5251653d4876SAndy Whitcroft
5252d4977c78STobias Klauser# Check that the storage class is at the beginning of a declaration
5253d4977c78STobias Klauser		if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
5254000d1cc1SJoe Perches			WARN("STORAGE_CLASS",
5255000d1cc1SJoe Perches			     "storage class should be at the beginning of the declaration\n" . $herecurr)
5256d4977c78STobias Klauser		}
5257d4977c78STobias Klauser
5258de7d4f0eSAndy Whitcroft# check the location of the inline attribute, that it is between
5259de7d4f0eSAndy Whitcroft# storage class and type.
52609c0ca6f9SAndy Whitcroft		if ($line =~ /\b$Type\s+$Inline\b/ ||
52619c0ca6f9SAndy Whitcroft		    $line =~ /\b$Inline\s+$Storage\b/) {
5262000d1cc1SJoe Perches			ERROR("INLINE_LOCATION",
5263000d1cc1SJoe Perches			      "inline keyword should sit between storage class and type\n" . $herecurr);
5264de7d4f0eSAndy Whitcroft		}
5265de7d4f0eSAndy Whitcroft
52668905a67cSAndy Whitcroft# Check for __inline__ and __inline, prefer inline
52672b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
52682b7ab453SJoe Perches		    $line =~ /\b(__inline__|__inline)\b/) {
5269d5e616fcSJoe Perches			if (WARN("INLINE",
5270d5e616fcSJoe Perches				 "plain inline is preferred over $1\n" . $herecurr) &&
5271d5e616fcSJoe Perches			    $fix) {
5272194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/;
5273d5e616fcSJoe Perches
5274d5e616fcSJoe Perches			}
52758905a67cSAndy Whitcroft		}
52768905a67cSAndy Whitcroft
52773d130fd0SJoe Perches# Check for __attribute__ packed, prefer __packed
52782b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
52792b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
5280000d1cc1SJoe Perches			WARN("PREFER_PACKED",
5281000d1cc1SJoe Perches			     "__packed is preferred over __attribute__((packed))\n" . $herecurr);
52823d130fd0SJoe Perches		}
52833d130fd0SJoe Perches
528439b7e287SJoe Perches# Check for __attribute__ aligned, prefer __aligned
52852b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
52862b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
5287000d1cc1SJoe Perches			WARN("PREFER_ALIGNED",
5288000d1cc1SJoe Perches			     "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
528939b7e287SJoe Perches		}
529039b7e287SJoe Perches
52915f14d3bdSJoe Perches# Check for __attribute__ format(printf, prefer __printf
52922b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
52932b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
5294d5e616fcSJoe Perches			if (WARN("PREFER_PRINTF",
5295d5e616fcSJoe Perches				 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) &&
5296d5e616fcSJoe Perches			    $fix) {
5297194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf\s*,\s*(.*)\)\s*\)\s*\)/"__printf(" . trim($1) . ")"/ex;
5298d5e616fcSJoe Perches
5299d5e616fcSJoe Perches			}
53005f14d3bdSJoe Perches		}
53015f14d3bdSJoe Perches
53026061d949SJoe Perches# Check for __attribute__ format(scanf, prefer __scanf
53032b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
53042b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
5305d5e616fcSJoe Perches			if (WARN("PREFER_SCANF",
5306d5e616fcSJoe Perches				 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) &&
5307d5e616fcSJoe Perches			    $fix) {
5308194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\s*,\s*(.*)\)\s*\)\s*\)/"__scanf(" . trim($1) . ")"/ex;
5309d5e616fcSJoe Perches			}
53106061d949SJoe Perches		}
53116061d949SJoe Perches
5312619a908aSJoe Perches# Check for __attribute__ weak, or __weak declarations (may have link issues)
5313619a908aSJoe Perches		if ($^V && $^V ge 5.10.0 &&
5314619a908aSJoe Perches		    $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ &&
5315619a908aSJoe Perches		    ($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ ||
5316619a908aSJoe Perches		     $line =~ /\b__weak\b/)) {
5317619a908aSJoe Perches			ERROR("WEAK_DECLARATION",
5318619a908aSJoe Perches			      "Using weak declarations can have unintended link defects\n" . $herecurr);
5319619a908aSJoe Perches		}
5320619a908aSJoe Perches
5321e6176fa4SJoe Perches# check for c99 types like uint8_t used outside of uapi/
5322e6176fa4SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
5323e6176fa4SJoe Perches		    $line =~ /\b($Declare)\s*$Ident\s*[=;,\[]/) {
5324e6176fa4SJoe Perches			my $type = $1;
5325e6176fa4SJoe Perches			if ($type =~ /\b($typeC99Typedefs)\b/) {
5326e6176fa4SJoe Perches				$type = $1;
5327e6176fa4SJoe Perches				my $kernel_type = 'u';
5328e6176fa4SJoe Perches				$kernel_type = 's' if ($type =~ /^_*[si]/);
5329e6176fa4SJoe Perches				$type =~ /(\d+)/;
5330e6176fa4SJoe Perches				$kernel_type .= $1;
5331e6176fa4SJoe Perches				if (CHK("PREFER_KERNEL_TYPES",
5332e6176fa4SJoe Perches					"Prefer kernel type '$kernel_type' over '$type'\n" . $herecurr) &&
5333e6176fa4SJoe Perches				    $fix) {
5334e6176fa4SJoe Perches					$fixed[$fixlinenr] =~ s/\b$type\b/$kernel_type/;
5335e6176fa4SJoe Perches				}
5336e6176fa4SJoe Perches			}
5337e6176fa4SJoe Perches		}
5338e6176fa4SJoe Perches
5339938224b5SJoe Perches# check for cast of C90 native int or longer types constants
5340938224b5SJoe Perches		if ($line =~ /(\(\s*$C90_int_types\s*\)\s*)($Constant)\b/) {
5341938224b5SJoe Perches			my $cast = $1;
5342938224b5SJoe Perches			my $const = $2;
5343938224b5SJoe Perches			if (WARN("TYPECAST_INT_CONSTANT",
5344938224b5SJoe Perches				 "Unnecessary typecast of c90 int constant\n" . $herecurr) &&
5345938224b5SJoe Perches			    $fix) {
5346938224b5SJoe Perches				my $suffix = "";
5347938224b5SJoe Perches				my $newconst = $const;
5348938224b5SJoe Perches				$newconst =~ s/${Int_type}$//;
5349938224b5SJoe Perches				$suffix .= 'U' if ($cast =~ /\bunsigned\b/);
5350938224b5SJoe Perches				if ($cast =~ /\blong\s+long\b/) {
5351938224b5SJoe Perches					$suffix .= 'LL';
5352938224b5SJoe Perches				} elsif ($cast =~ /\blong\b/) {
5353938224b5SJoe Perches					$suffix .= 'L';
5354938224b5SJoe Perches				}
5355938224b5SJoe Perches				$fixed[$fixlinenr] =~ s/\Q$cast\E$const\b/$newconst$suffix/;
5356938224b5SJoe Perches			}
5357938224b5SJoe Perches		}
5358938224b5SJoe Perches
53598f53a9b8SJoe Perches# check for sizeof(&)
53608f53a9b8SJoe Perches		if ($line =~ /\bsizeof\s*\(\s*\&/) {
5361000d1cc1SJoe Perches			WARN("SIZEOF_ADDRESS",
5362000d1cc1SJoe Perches			     "sizeof(& should be avoided\n" . $herecurr);
53638f53a9b8SJoe Perches		}
53648f53a9b8SJoe Perches
536566c80b60SJoe Perches# check for sizeof without parenthesis
536666c80b60SJoe Perches		if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
5367d5e616fcSJoe Perches			if (WARN("SIZEOF_PARENTHESIS",
5368d5e616fcSJoe Perches				 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
5369d5e616fcSJoe Perches			    $fix) {
5370194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
5371d5e616fcSJoe Perches			}
537266c80b60SJoe Perches		}
537366c80b60SJoe Perches
537488982feaSJoe Perches# check for struct spinlock declarations
537588982feaSJoe Perches		if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
537688982feaSJoe Perches			WARN("USE_SPINLOCK_T",
537788982feaSJoe Perches			     "struct spinlock should be spinlock_t\n" . $herecurr);
537888982feaSJoe Perches		}
537988982feaSJoe Perches
5380a6962d72SJoe Perches# check for seq_printf uses that could be seq_puts
538106668727SJoe Perches		if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
5382a6962d72SJoe Perches			my $fmt = get_quoted_string($line, $rawline);
5383caac1d5fSHeba Aamer			$fmt =~ s/%%//g;
5384caac1d5fSHeba Aamer			if ($fmt !~ /%/) {
5385d5e616fcSJoe Perches				if (WARN("PREFER_SEQ_PUTS",
5386d5e616fcSJoe Perches					 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
5387d5e616fcSJoe Perches				    $fix) {
5388194f66fcSJoe Perches					$fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/;
5389d5e616fcSJoe Perches				}
5390a6962d72SJoe Perches			}
5391a6962d72SJoe Perches		}
5392a6962d72SJoe Perches
5393554e165cSAndy Whitcroft# Check for misused memsets
5394d1fe9c09SJoe Perches		if ($^V && $^V ge 5.10.0 &&
5395d1fe9c09SJoe Perches		    defined $stat &&
53969e20a853SMateusz Kulikowski		    $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/) {
5397554e165cSAndy Whitcroft
5398d7c76ba7SJoe Perches			my $ms_addr = $2;
5399d1fe9c09SJoe Perches			my $ms_val = $7;
5400d1fe9c09SJoe Perches			my $ms_size = $12;
5401d7c76ba7SJoe Perches
5402554e165cSAndy Whitcroft			if ($ms_size =~ /^(0x|)0$/i) {
5403554e165cSAndy Whitcroft				ERROR("MEMSET",
5404d7c76ba7SJoe Perches				      "memset to 0's uses 0 as the 2nd argument, not the 3rd\n" . "$here\n$stat\n");
5405554e165cSAndy Whitcroft			} elsif ($ms_size =~ /^(0x|)1$/i) {
5406554e165cSAndy Whitcroft				WARN("MEMSET",
5407d7c76ba7SJoe Perches				     "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
5408d7c76ba7SJoe Perches			}
5409d7c76ba7SJoe Perches		}
5410d7c76ba7SJoe Perches
541198a9bba5SJoe Perches# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar)
541298a9bba5SJoe Perches		if ($^V && $^V ge 5.10.0 &&
541310895d2cSMateusz Kulikowski		    defined $stat &&
541410895d2cSMateusz Kulikowski		    $stat =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
541598a9bba5SJoe Perches			if (WARN("PREFER_ETHER_ADDR_COPY",
541610895d2cSMateusz Kulikowski				 "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . "$here\n$stat\n") &&
541798a9bba5SJoe Perches			    $fix) {
5418194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/;
541998a9bba5SJoe Perches			}
542098a9bba5SJoe Perches		}
542198a9bba5SJoe Perches
5422b6117d17SMateusz Kulikowski# Check for memcmp(foo, bar, ETH_ALEN) that could be ether_addr_equal*(foo, bar)
5423b6117d17SMateusz Kulikowski		if ($^V && $^V ge 5.10.0 &&
5424b6117d17SMateusz Kulikowski		    defined $stat &&
5425b6117d17SMateusz Kulikowski		    $stat =~ /^\+(?:.*?)\bmemcmp\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
5426b6117d17SMateusz Kulikowski			WARN("PREFER_ETHER_ADDR_EQUAL",
5427b6117d17SMateusz Kulikowski			     "Prefer ether_addr_equal() or ether_addr_equal_unaligned() over memcmp()\n" . "$here\n$stat\n")
5428b6117d17SMateusz Kulikowski		}
5429b6117d17SMateusz Kulikowski
54308617cd09SMateusz Kulikowski# check for memset(foo, 0x0, ETH_ALEN) that could be eth_zero_addr
54318617cd09SMateusz Kulikowski# check for memset(foo, 0xFF, ETH_ALEN) that could be eth_broadcast_addr
54328617cd09SMateusz Kulikowski		if ($^V && $^V ge 5.10.0 &&
54338617cd09SMateusz Kulikowski		    defined $stat &&
54348617cd09SMateusz Kulikowski		    $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
54358617cd09SMateusz Kulikowski
54368617cd09SMateusz Kulikowski			my $ms_val = $7;
54378617cd09SMateusz Kulikowski
54388617cd09SMateusz Kulikowski			if ($ms_val =~ /^(?:0x|)0+$/i) {
54398617cd09SMateusz Kulikowski				if (WARN("PREFER_ETH_ZERO_ADDR",
54408617cd09SMateusz Kulikowski					 "Prefer eth_zero_addr over memset()\n" . "$here\n$stat\n") &&
54418617cd09SMateusz Kulikowski				    $fix) {
54428617cd09SMateusz Kulikowski					$fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_zero_addr($2)/;
54438617cd09SMateusz Kulikowski				}
54448617cd09SMateusz Kulikowski			} elsif ($ms_val =~ /^(?:0xff|255)$/i) {
54458617cd09SMateusz Kulikowski				if (WARN("PREFER_ETH_BROADCAST_ADDR",
54468617cd09SMateusz Kulikowski					 "Prefer eth_broadcast_addr() over memset()\n" . "$here\n$stat\n") &&
54478617cd09SMateusz Kulikowski				    $fix) {
54488617cd09SMateusz Kulikowski					$fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_broadcast_addr($2)/;
54498617cd09SMateusz Kulikowski				}
54508617cd09SMateusz Kulikowski			}
54518617cd09SMateusz Kulikowski		}
54528617cd09SMateusz Kulikowski
5453d7c76ba7SJoe Perches# typecasts on min/max could be min_t/max_t
5454d1fe9c09SJoe Perches		if ($^V && $^V ge 5.10.0 &&
5455d1fe9c09SJoe Perches		    defined $stat &&
5456d7c76ba7SJoe Perches		    $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
5457d1fe9c09SJoe Perches			if (defined $2 || defined $7) {
5458d7c76ba7SJoe Perches				my $call = $1;
5459d7c76ba7SJoe Perches				my $cast1 = deparenthesize($2);
5460d7c76ba7SJoe Perches				my $arg1 = $3;
5461d1fe9c09SJoe Perches				my $cast2 = deparenthesize($7);
5462d1fe9c09SJoe Perches				my $arg2 = $8;
5463d7c76ba7SJoe Perches				my $cast;
5464d7c76ba7SJoe Perches
5465d1fe9c09SJoe Perches				if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
5466d7c76ba7SJoe Perches					$cast = "$cast1 or $cast2";
5467d7c76ba7SJoe Perches				} elsif ($cast1 ne "") {
5468d7c76ba7SJoe Perches					$cast = $cast1;
5469d7c76ba7SJoe Perches				} else {
5470d7c76ba7SJoe Perches					$cast = $cast2;
5471d7c76ba7SJoe Perches				}
5472d7c76ba7SJoe Perches				WARN("MINMAX",
5473d7c76ba7SJoe Perches				     "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
5474554e165cSAndy Whitcroft			}
5475554e165cSAndy Whitcroft		}
5476554e165cSAndy Whitcroft
54774a273195SJoe Perches# check usleep_range arguments
54784a273195SJoe Perches		if ($^V && $^V ge 5.10.0 &&
54794a273195SJoe Perches		    defined $stat &&
54804a273195SJoe Perches		    $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
54814a273195SJoe Perches			my $min = $1;
54824a273195SJoe Perches			my $max = $7;
54834a273195SJoe Perches			if ($min eq $max) {
54844a273195SJoe Perches				WARN("USLEEP_RANGE",
54854a273195SJoe Perches				     "usleep_range should not use min == max args; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
54864a273195SJoe Perches			} elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
54874a273195SJoe Perches				 $min > $max) {
54884a273195SJoe Perches				WARN("USLEEP_RANGE",
54894a273195SJoe Perches				     "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
54904a273195SJoe Perches			}
54914a273195SJoe Perches		}
54924a273195SJoe Perches
5493823b794cSJoe Perches# check for naked sscanf
5494823b794cSJoe Perches		if ($^V && $^V ge 5.10.0 &&
5495823b794cSJoe Perches		    defined $stat &&
54966c8bd707SJoe Perches		    $line =~ /\bsscanf\b/ &&
5497823b794cSJoe Perches		    ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
5498823b794cSJoe Perches		     $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ &&
5499823b794cSJoe Perches		     $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) {
5500823b794cSJoe Perches			my $lc = $stat =~ tr@\n@@;
5501823b794cSJoe Perches			$lc = $lc + $linenr;
5502823b794cSJoe Perches			my $stat_real = raw_line($linenr, 0);
5503823b794cSJoe Perches		        for (my $count = $linenr + 1; $count <= $lc; $count++) {
5504823b794cSJoe Perches				$stat_real = $stat_real . "\n" . raw_line($count, 0);
5505823b794cSJoe Perches			}
5506823b794cSJoe Perches			WARN("NAKED_SSCANF",
5507823b794cSJoe Perches			     "unchecked sscanf return value\n" . "$here\n$stat_real\n");
5508823b794cSJoe Perches		}
5509823b794cSJoe Perches
5510afc819abSJoe Perches# check for simple sscanf that should be kstrto<foo>
5511afc819abSJoe Perches		if ($^V && $^V ge 5.10.0 &&
5512afc819abSJoe Perches		    defined $stat &&
5513afc819abSJoe Perches		    $line =~ /\bsscanf\b/) {
5514afc819abSJoe Perches			my $lc = $stat =~ tr@\n@@;
5515afc819abSJoe Perches			$lc = $lc + $linenr;
5516afc819abSJoe Perches			my $stat_real = raw_line($linenr, 0);
5517afc819abSJoe Perches		        for (my $count = $linenr + 1; $count <= $lc; $count++) {
5518afc819abSJoe Perches				$stat_real = $stat_real . "\n" . raw_line($count, 0);
5519afc819abSJoe Perches			}
5520afc819abSJoe Perches			if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) {
5521afc819abSJoe Perches				my $format = $6;
5522afc819abSJoe Perches				my $count = $format =~ tr@%@%@;
5523afc819abSJoe Perches				if ($count == 1 &&
5524afc819abSJoe Perches				    $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) {
5525afc819abSJoe Perches					WARN("SSCANF_TO_KSTRTO",
5526afc819abSJoe Perches					     "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n");
5527afc819abSJoe Perches				}
5528afc819abSJoe Perches			}
5529afc819abSJoe Perches		}
5530afc819abSJoe Perches
553170dc8a48SJoe Perches# check for new externs in .h files.
553270dc8a48SJoe Perches		if ($realfile =~ /\.h$/ &&
553370dc8a48SJoe Perches		    $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
5534d1d85780SJoe Perches			if (CHK("AVOID_EXTERNS",
553570dc8a48SJoe Perches				"extern prototypes should be avoided in .h files\n" . $herecurr) &&
553670dc8a48SJoe Perches			    $fix) {
5537194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
553870dc8a48SJoe Perches			}
553970dc8a48SJoe Perches		}
554070dc8a48SJoe Perches
5541de7d4f0eSAndy Whitcroft# check for new externs in .c files.
5542171ae1a4SAndy Whitcroft		if ($realfile =~ /\.c$/ && defined $stat &&
5543c45dcabdSAndy Whitcroft		    $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
5544171ae1a4SAndy Whitcroft		{
5545c45dcabdSAndy Whitcroft			my $function_name = $1;
5546c45dcabdSAndy Whitcroft			my $paren_space = $2;
5547171ae1a4SAndy Whitcroft
5548171ae1a4SAndy Whitcroft			my $s = $stat;
5549171ae1a4SAndy Whitcroft			if (defined $cond) {
5550171ae1a4SAndy Whitcroft				substr($s, 0, length($cond), '');
5551171ae1a4SAndy Whitcroft			}
5552c45dcabdSAndy Whitcroft			if ($s =~ /^\s*;/ &&
5553c45dcabdSAndy Whitcroft			    $function_name ne 'uninitialized_var')
5554c45dcabdSAndy Whitcroft			{
5555000d1cc1SJoe Perches				WARN("AVOID_EXTERNS",
5556000d1cc1SJoe Perches				     "externs should be avoided in .c files\n" .  $herecurr);
5557de7d4f0eSAndy Whitcroft			}
5558de7d4f0eSAndy Whitcroft
5559171ae1a4SAndy Whitcroft			if ($paren_space =~ /\n/) {
5560000d1cc1SJoe Perches				WARN("FUNCTION_ARGUMENTS",
5561000d1cc1SJoe Perches				     "arguments for function declarations should follow identifier\n" . $herecurr);
5562171ae1a4SAndy Whitcroft			}
55639c9ba34eSAndy Whitcroft
55649c9ba34eSAndy Whitcroft		} elsif ($realfile =~ /\.c$/ && defined $stat &&
55659c9ba34eSAndy Whitcroft		    $stat =~ /^.\s*extern\s+/)
55669c9ba34eSAndy Whitcroft		{
5567000d1cc1SJoe Perches			WARN("AVOID_EXTERNS",
5568000d1cc1SJoe Perches			     "externs should be avoided in .c files\n" .  $herecurr);
5569171ae1a4SAndy Whitcroft		}
5570171ae1a4SAndy Whitcroft
5571de7d4f0eSAndy Whitcroft# checks for new __setup's
5572de7d4f0eSAndy Whitcroft		if ($rawline =~ /\b__setup\("([^"]*)"/) {
5573de7d4f0eSAndy Whitcroft			my $name = $1;
5574de7d4f0eSAndy Whitcroft
5575de7d4f0eSAndy Whitcroft			if (!grep(/$name/, @setup_docs)) {
5576000d1cc1SJoe Perches				CHK("UNDOCUMENTED_SETUP",
5577000d1cc1SJoe Perches				    "__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
5578de7d4f0eSAndy Whitcroft			}
5579653d4876SAndy Whitcroft		}
55809c0ca6f9SAndy Whitcroft
55819c0ca6f9SAndy Whitcroft# check for pointless casting of kmalloc return
5582caf2a54fSJoe Perches		if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) {
5583000d1cc1SJoe Perches			WARN("UNNECESSARY_CASTS",
5584000d1cc1SJoe Perches			     "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
55859c0ca6f9SAndy Whitcroft		}
558613214adfSAndy Whitcroft
5587a640d25cSJoe Perches# alloc style
5588a640d25cSJoe Perches# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
5589a640d25cSJoe Perches		if ($^V && $^V ge 5.10.0 &&
5590a640d25cSJoe Perches		    $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*([kv][mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
5591a640d25cSJoe Perches			CHK("ALLOC_SIZEOF_STRUCT",
5592a640d25cSJoe Perches			    "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
5593a640d25cSJoe Perches		}
5594a640d25cSJoe Perches
559560a55369SJoe Perches# check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc
559660a55369SJoe Perches		if ($^V && $^V ge 5.10.0 &&
5597e367455aSJoe Perches		    $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*(k[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)\s*,/) {
559860a55369SJoe Perches			my $oldfunc = $3;
559960a55369SJoe Perches			my $a1 = $4;
560060a55369SJoe Perches			my $a2 = $10;
560160a55369SJoe Perches			my $newfunc = "kmalloc_array";
560260a55369SJoe Perches			$newfunc = "kcalloc" if ($oldfunc eq "kzalloc");
560360a55369SJoe Perches			my $r1 = $a1;
560460a55369SJoe Perches			my $r2 = $a2;
560560a55369SJoe Perches			if ($a1 =~ /^sizeof\s*\S/) {
560660a55369SJoe Perches				$r1 = $a2;
560760a55369SJoe Perches				$r2 = $a1;
560860a55369SJoe Perches			}
5609e367455aSJoe Perches			if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ &&
5610e367455aSJoe Perches			    !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) {
5611e367455aSJoe Perches				if (WARN("ALLOC_WITH_MULTIPLY",
5612e367455aSJoe Perches					 "Prefer $newfunc over $oldfunc with multiply\n" . $herecurr) &&
5613e367455aSJoe Perches				    $fix) {
5614194f66fcSJoe 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;
561560a55369SJoe Perches
561660a55369SJoe Perches				}
561760a55369SJoe Perches			}
561860a55369SJoe Perches		}
561960a55369SJoe Perches
5620972fdea2SJoe Perches# check for krealloc arg reuse
5621972fdea2SJoe Perches		if ($^V && $^V ge 5.10.0 &&
5622972fdea2SJoe Perches		    $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*\1\s*,/) {
5623972fdea2SJoe Perches			WARN("KREALLOC_ARG_REUSE",
5624972fdea2SJoe Perches			     "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
5625972fdea2SJoe Perches		}
5626972fdea2SJoe Perches
56275ce59ae0SJoe Perches# check for alloc argument mismatch
56285ce59ae0SJoe Perches		if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
56295ce59ae0SJoe Perches			WARN("ALLOC_ARRAY_ARGS",
56305ce59ae0SJoe Perches			     "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
56315ce59ae0SJoe Perches		}
56325ce59ae0SJoe Perches
5633caf2a54fSJoe Perches# check for multiple semicolons
5634caf2a54fSJoe Perches		if ($line =~ /;\s*;\s*$/) {
5635d5e616fcSJoe Perches			if (WARN("ONE_SEMICOLON",
5636d5e616fcSJoe Perches				 "Statements terminations use 1 semicolon\n" . $herecurr) &&
5637d5e616fcSJoe Perches			    $fix) {
5638194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g;
5639d5e616fcSJoe Perches			}
5640d1e2ad07SJoe Perches		}
5641d1e2ad07SJoe Perches
56420ab90191SJoe Perches# check for #defines like: 1 << <digit> that could be BIT(digit)
56430ab90191SJoe Perches		if ($line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) {
56440ab90191SJoe Perches			my $ull = "";
56450ab90191SJoe Perches			$ull = "_ULL" if (defined($1) && $1 =~ /ll/i);
56460ab90191SJoe Perches			if (CHK("BIT_MACRO",
56470ab90191SJoe Perches				"Prefer using the BIT$ull macro\n" . $herecurr) &&
56480ab90191SJoe Perches			    $fix) {
56490ab90191SJoe Perches				$fixed[$fixlinenr] =~ s/\(?\s*1\s*[ulUL]*\s*<<\s*(\d+|$Ident)\s*\)?/BIT${ull}($1)/;
56500ab90191SJoe Perches			}
56510ab90191SJoe Perches		}
56520ab90191SJoe Perches
56532d632745SJoe Perches# check for #if defined CONFIG_<FOO> || defined CONFIG_<FOO>_MODULE
56542d632745SJoe Perches		if ($line =~ /^\+\s*#\s*if\s+defined(?:\s*\(?\s*|\s+)(CONFIG_[A-Z_]+)\s*\)?\s*\|\|\s*defined(?:\s*\(?\s*|\s+)\1_MODULE\s*\)?\s*$/) {
56552d632745SJoe Perches			my $config = $1;
56562d632745SJoe Perches			if (WARN("PREFER_IS_ENABLED",
56572d632745SJoe Perches				 "Prefer IS_ENABLED(<FOO>) to CONFIG_<FOO> || CONFIG_<FOO>_MODULE\n" . $herecurr) &&
56582d632745SJoe Perches			    $fix) {
56592d632745SJoe Perches				$fixed[$fixlinenr] = "\+#if IS_ENABLED($config)";
56602d632745SJoe Perches			}
56612d632745SJoe Perches		}
56622d632745SJoe Perches
5663e81f239bSJoe Perches# check for case / default statements not preceded by break/fallthrough/switch
5664c34c09a8SJoe Perches		if ($line =~ /^.\s*(?:case\s+(?:$Ident|$Constant)\s*|default):/) {
5665c34c09a8SJoe Perches			my $has_break = 0;
5666c34c09a8SJoe Perches			my $has_statement = 0;
5667c34c09a8SJoe Perches			my $count = 0;
5668c34c09a8SJoe Perches			my $prevline = $linenr;
5669e81f239bSJoe Perches			while ($prevline > 1 && ($file || $count < 3) && !$has_break) {
5670c34c09a8SJoe Perches				$prevline--;
5671c34c09a8SJoe Perches				my $rline = $rawlines[$prevline - 1];
5672c34c09a8SJoe Perches				my $fline = $lines[$prevline - 1];
5673c34c09a8SJoe Perches				last if ($fline =~ /^\@\@/);
5674c34c09a8SJoe Perches				next if ($fline =~ /^\-/);
5675c34c09a8SJoe Perches				next if ($fline =~ /^.(?:\s*(?:case\s+(?:$Ident|$Constant)[\s$;]*|default):[\s$;]*)*$/);
5676c34c09a8SJoe Perches				$has_break = 1 if ($rline =~ /fall[\s_-]*(through|thru)/i);
5677c34c09a8SJoe Perches				next if ($fline =~ /^.[\s$;]*$/);
5678c34c09a8SJoe Perches				$has_statement = 1;
5679c34c09a8SJoe Perches				$count++;
5680c34c09a8SJoe Perches				$has_break = 1 if ($fline =~ /\bswitch\b|\b(?:break\s*;[\s$;]*$|return\b|goto\b|continue\b)/);
5681c34c09a8SJoe Perches			}
5682c34c09a8SJoe Perches			if (!$has_break && $has_statement) {
5683c34c09a8SJoe Perches				WARN("MISSING_BREAK",
5684c34c09a8SJoe Perches				     "Possible switch case/default not preceeded by break or fallthrough comment\n" . $herecurr);
5685c34c09a8SJoe Perches			}
5686c34c09a8SJoe Perches		}
5687c34c09a8SJoe Perches
5688d1e2ad07SJoe Perches# check for switch/default statements without a break;
5689d1e2ad07SJoe Perches		if ($^V && $^V ge 5.10.0 &&
5690d1e2ad07SJoe Perches		    defined $stat &&
5691d1e2ad07SJoe Perches		    $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
5692d1e2ad07SJoe Perches			my $ctx = '';
5693d1e2ad07SJoe Perches			my $herectx = $here . "\n";
5694d1e2ad07SJoe Perches			my $cnt = statement_rawlines($stat);
5695d1e2ad07SJoe Perches			for (my $n = 0; $n < $cnt; $n++) {
5696d1e2ad07SJoe Perches				$herectx .= raw_line($linenr, $n) . "\n";
5697d1e2ad07SJoe Perches			}
5698d1e2ad07SJoe Perches			WARN("DEFAULT_NO_BREAK",
5699d1e2ad07SJoe Perches			     "switch default: should use break\n" . $herectx);
5700caf2a54fSJoe Perches		}
5701caf2a54fSJoe Perches
570213214adfSAndy Whitcroft# check for gcc specific __FUNCTION__
5703d5e616fcSJoe Perches		if ($line =~ /\b__FUNCTION__\b/) {
5704d5e616fcSJoe Perches			if (WARN("USE_FUNC",
5705d5e616fcSJoe Perches				 "__func__ should be used instead of gcc specific __FUNCTION__\n"  . $herecurr) &&
5706d5e616fcSJoe Perches			    $fix) {
5707194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g;
5708d5e616fcSJoe Perches			}
570913214adfSAndy Whitcroft		}
5710773647a0SAndy Whitcroft
571162ec818fSJoe Perches# check for uses of __DATE__, __TIME__, __TIMESTAMP__
571262ec818fSJoe Perches		while ($line =~ /\b(__(?:DATE|TIME|TIMESTAMP)__)\b/g) {
571362ec818fSJoe Perches			ERROR("DATE_TIME",
571462ec818fSJoe Perches			      "Use of the '$1' macro makes the build non-deterministic\n" . $herecurr);
571562ec818fSJoe Perches		}
571662ec818fSJoe Perches
57172c92488aSJoe Perches# check for use of yield()
57182c92488aSJoe Perches		if ($line =~ /\byield\s*\(\s*\)/) {
57192c92488aSJoe Perches			WARN("YIELD",
57202c92488aSJoe Perches			     "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n"  . $herecurr);
57212c92488aSJoe Perches		}
57222c92488aSJoe Perches
5723179f8f40SJoe Perches# check for comparisons against true and false
5724179f8f40SJoe Perches		if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
5725179f8f40SJoe Perches			my $lead = $1;
5726179f8f40SJoe Perches			my $arg = $2;
5727179f8f40SJoe Perches			my $test = $3;
5728179f8f40SJoe Perches			my $otype = $4;
5729179f8f40SJoe Perches			my $trail = $5;
5730179f8f40SJoe Perches			my $op = "!";
5731179f8f40SJoe Perches
5732179f8f40SJoe Perches			($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
5733179f8f40SJoe Perches
5734179f8f40SJoe Perches			my $type = lc($otype);
5735179f8f40SJoe Perches			if ($type =~ /^(?:true|false)$/) {
5736179f8f40SJoe Perches				if (("$test" eq "==" && "$type" eq "true") ||
5737179f8f40SJoe Perches				    ("$test" eq "!=" && "$type" eq "false")) {
5738179f8f40SJoe Perches					$op = "";
5739179f8f40SJoe Perches				}
5740179f8f40SJoe Perches
5741179f8f40SJoe Perches				CHK("BOOL_COMPARISON",
5742179f8f40SJoe Perches				    "Using comparison to $otype is error prone\n" . $herecurr);
5743179f8f40SJoe Perches
5744179f8f40SJoe Perches## maybe suggesting a correct construct would better
5745179f8f40SJoe Perches##				    "Using comparison to $otype is error prone.  Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
5746179f8f40SJoe Perches
5747179f8f40SJoe Perches			}
5748179f8f40SJoe Perches		}
5749179f8f40SJoe Perches
57504882720bSThomas Gleixner# check for semaphores initialized locked
57514882720bSThomas Gleixner		if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
5752000d1cc1SJoe Perches			WARN("CONSIDER_COMPLETION",
5753000d1cc1SJoe Perches			     "consider using a completion\n" . $herecurr);
5754773647a0SAndy Whitcroft		}
57556712d858SJoe Perches
575667d0a075SJoe Perches# recommend kstrto* over simple_strto* and strict_strto*
575767d0a075SJoe Perches		if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
5758000d1cc1SJoe Perches			WARN("CONSIDER_KSTRTO",
575967d0a075SJoe Perches			     "$1 is obsolete, use k$3 instead\n" . $herecurr);
5760773647a0SAndy Whitcroft		}
57616712d858SJoe Perches
5762ae3ccc46SFabian Frederick# check for __initcall(), use device_initcall() explicitly or more appropriate function please
5763f3db6639SMichael Ellerman		if ($line =~ /^.\s*__initcall\s*\(/) {
5764000d1cc1SJoe Perches			WARN("USE_DEVICE_INITCALL",
5765ae3ccc46SFabian Frederick			     "please use device_initcall() or more appropriate function instead of __initcall() (see include/linux/init.h)\n" . $herecurr);
5766f3db6639SMichael Ellerman		}
57676712d858SJoe Perches
57680f3c5aabSJoe Perches# check for various structs that are normally const (ops, kgdb, device_tree)
57690f3c5aabSJoe Perches		my $const_structs = qr{
57700f3c5aabSJoe Perches				acpi_dock_ops|
577179404849SEmese Revfy				address_space_operations|
577279404849SEmese Revfy				backlight_ops|
577379404849SEmese Revfy				block_device_operations|
577479404849SEmese Revfy				dentry_operations|
577579404849SEmese Revfy				dev_pm_ops|
577679404849SEmese Revfy				dma_map_ops|
577779404849SEmese Revfy				extent_io_ops|
577879404849SEmese Revfy				file_lock_operations|
577979404849SEmese Revfy				file_operations|
578079404849SEmese Revfy				hv_ops|
578179404849SEmese Revfy				ide_dma_ops|
578279404849SEmese Revfy				intel_dvo_dev_ops|
578379404849SEmese Revfy				item_operations|
578479404849SEmese Revfy				iwl_ops|
578579404849SEmese Revfy				kgdb_arch|
578679404849SEmese Revfy				kgdb_io|
578779404849SEmese Revfy				kset_uevent_ops|
578879404849SEmese Revfy				lock_manager_operations|
578979404849SEmese Revfy				microcode_ops|
579079404849SEmese Revfy				mtrr_ops|
579179404849SEmese Revfy				neigh_ops|
579279404849SEmese Revfy				nlmsvc_binding|
57930f3c5aabSJoe Perches				of_device_id|
579479404849SEmese Revfy				pci_raw_ops|
579579404849SEmese Revfy				pipe_buf_operations|
579679404849SEmese Revfy				platform_hibernation_ops|
579779404849SEmese Revfy				platform_suspend_ops|
579879404849SEmese Revfy				proto_ops|
579979404849SEmese Revfy				rpc_pipe_ops|
580079404849SEmese Revfy				seq_operations|
580179404849SEmese Revfy				snd_ac97_build_ops|
580279404849SEmese Revfy				soc_pcmcia_socket_ops|
580379404849SEmese Revfy				stacktrace_ops|
580479404849SEmese Revfy				sysfs_ops|
580579404849SEmese Revfy				tty_operations|
58066d07d01bSJoe Perches				uart_ops|
580779404849SEmese Revfy				usb_mon_operations|
580879404849SEmese Revfy				wd_ops}x;
58096903ffb2SAndy Whitcroft		if ($line !~ /\bconst\b/ &&
58100f3c5aabSJoe Perches		    $line =~ /\bstruct\s+($const_structs)\b/) {
5811000d1cc1SJoe Perches			WARN("CONST_STRUCT",
5812000d1cc1SJoe Perches			     "struct $1 should normally be const\n" .
58136903ffb2SAndy Whitcroft				$herecurr);
58142b6db5cbSAndy Whitcroft		}
5815773647a0SAndy Whitcroft
5816773647a0SAndy Whitcroft# use of NR_CPUS is usually wrong
5817773647a0SAndy Whitcroft# ignore definitions of NR_CPUS and usage to define arrays as likely right
5818773647a0SAndy Whitcroft		if ($line =~ /\bNR_CPUS\b/ &&
5819c45dcabdSAndy Whitcroft		    $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
5820c45dcabdSAndy Whitcroft		    $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
5821171ae1a4SAndy Whitcroft		    $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
5822171ae1a4SAndy Whitcroft		    $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
5823171ae1a4SAndy Whitcroft		    $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
5824773647a0SAndy Whitcroft		{
5825000d1cc1SJoe Perches			WARN("NR_CPUS",
5826000d1cc1SJoe Perches			     "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
5827773647a0SAndy Whitcroft		}
58289c9ba34eSAndy Whitcroft
582952ea8506SJoe Perches# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
583052ea8506SJoe Perches		if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
583152ea8506SJoe Perches			ERROR("DEFINE_ARCH_HAS",
583252ea8506SJoe Perches			      "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
583352ea8506SJoe Perches		}
583452ea8506SJoe Perches
5835acd9362cSJoe Perches# likely/unlikely comparisons similar to "(likely(foo) > 0)"
5836acd9362cSJoe Perches		if ($^V && $^V ge 5.10.0 &&
5837acd9362cSJoe Perches		    $line =~ /\b((?:un)?likely)\s*\(\s*$FuncArg\s*\)\s*$Compare/) {
5838acd9362cSJoe Perches			WARN("LIKELY_MISUSE",
5839acd9362cSJoe Perches			     "Using $1 should generally have parentheses around the comparison\n" . $herecurr);
5840acd9362cSJoe Perches		}
5841acd9362cSJoe Perches
5842691d77b6SAndy Whitcroft# whine mightly about in_atomic
5843691d77b6SAndy Whitcroft		if ($line =~ /\bin_atomic\s*\(/) {
5844691d77b6SAndy Whitcroft			if ($realfile =~ m@^drivers/@) {
5845000d1cc1SJoe Perches				ERROR("IN_ATOMIC",
5846000d1cc1SJoe Perches				      "do not use in_atomic in drivers\n" . $herecurr);
5847f4a87736SAndy Whitcroft			} elsif ($realfile !~ m@^kernel/@) {
5848000d1cc1SJoe Perches				WARN("IN_ATOMIC",
5849000d1cc1SJoe Perches				     "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
5850691d77b6SAndy Whitcroft			}
5851691d77b6SAndy Whitcroft		}
58521704f47bSPeter Zijlstra
5853*481aea5cSJoe Perches# whine about ACCESS_ONCE
5854*481aea5cSJoe Perches		if ($^V && $^V ge 5.10.0 &&
5855*481aea5cSJoe Perches		    $line =~ /\bACCESS_ONCE\s*$balanced_parens\s*(=(?!=))?\s*($FuncArg)?/) {
5856*481aea5cSJoe Perches			my $par = $1;
5857*481aea5cSJoe Perches			my $eq = $2;
5858*481aea5cSJoe Perches			my $fun = $3;
5859*481aea5cSJoe Perches			$par =~ s/^\(\s*(.*)\s*\)$/$1/;
5860*481aea5cSJoe Perches			if (defined($eq)) {
5861*481aea5cSJoe Perches				if (WARN("PREFER_WRITE_ONCE",
5862*481aea5cSJoe Perches					 "Prefer WRITE_ONCE(<FOO>, <BAR>) over ACCESS_ONCE(<FOO>) = <BAR>\n" . $herecurr) &&
5863*481aea5cSJoe Perches				    $fix) {
5864*481aea5cSJoe Perches					$fixed[$fixlinenr] =~ s/\bACCESS_ONCE\s*\(\s*\Q$par\E\s*\)\s*$eq\s*\Q$fun\E/WRITE_ONCE($par, $fun)/;
5865*481aea5cSJoe Perches				}
5866*481aea5cSJoe Perches			} else {
5867*481aea5cSJoe Perches				if (WARN("PREFER_READ_ONCE",
5868*481aea5cSJoe Perches					 "Prefer READ_ONCE(<FOO>) over ACCESS_ONCE(<FOO>)\n" . $herecurr) &&
5869*481aea5cSJoe Perches				    $fix) {
5870*481aea5cSJoe Perches					$fixed[$fixlinenr] =~ s/\bACCESS_ONCE\s*\(\s*\Q$par\E\s*\)/READ_ONCE($par)/;
5871*481aea5cSJoe Perches				}
5872*481aea5cSJoe Perches			}
5873*481aea5cSJoe Perches		}
5874*481aea5cSJoe Perches
58751704f47bSPeter Zijlstra# check for lockdep_set_novalidate_class
58761704f47bSPeter Zijlstra		if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
58771704f47bSPeter Zijlstra		    $line =~ /__lockdep_no_validate__\s*\)/ ) {
58781704f47bSPeter Zijlstra			if ($realfile !~ m@^kernel/lockdep@ &&
58791704f47bSPeter Zijlstra			    $realfile !~ m@^include/linux/lockdep@ &&
58801704f47bSPeter Zijlstra			    $realfile !~ m@^drivers/base/core@) {
5881000d1cc1SJoe Perches				ERROR("LOCKDEP",
5882000d1cc1SJoe Perches				      "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
58831704f47bSPeter Zijlstra			}
58841704f47bSPeter Zijlstra		}
588588f8831cSDave Jones
5886b392c64fSJoe Perches		if ($line =~ /debugfs_create_\w+.*\b$mode_perms_world_writable\b/ ||
5887b392c64fSJoe Perches		    $line =~ /DEVICE_ATTR.*\b$mode_perms_world_writable\b/) {
5888000d1cc1SJoe Perches			WARN("EXPORTED_WORLD_WRITABLE",
5889000d1cc1SJoe Perches			     "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
589088f8831cSDave Jones		}
58912435880fSJoe Perches
5892515a235eSJoe Perches# Mode permission misuses where it seems decimal should be octal
5893515a235eSJoe Perches# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop
5894515a235eSJoe Perches		if ($^V && $^V ge 5.10.0 &&
5895515a235eSJoe Perches		    $line =~ /$mode_perms_search/) {
58962435880fSJoe Perches			foreach my $entry (@mode_permission_funcs) {
58972435880fSJoe Perches				my $func = $entry->[0];
58982435880fSJoe Perches				my $arg_pos = $entry->[1];
58992435880fSJoe Perches
59002435880fSJoe Perches				my $skip_args = "";
59012435880fSJoe Perches				if ($arg_pos > 1) {
59022435880fSJoe Perches					$arg_pos--;
59032435880fSJoe Perches					$skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}";
59042435880fSJoe Perches				}
59052435880fSJoe Perches				my $test = "\\b$func\\s*\\(${skip_args}([\\d]+)\\s*[,\\)]";
5906515a235eSJoe Perches				if ($line =~ /$test/) {
59072435880fSJoe Perches					my $val = $1;
59082435880fSJoe Perches					$val = $6 if ($skip_args ne "");
59092435880fSJoe Perches
59101727cc70SJoe Perches					if ($val !~ /^0$/ &&
59111727cc70SJoe Perches					    (($val =~ /^$Int$/ && $val !~ /^$Octal$/) ||
59121727cc70SJoe Perches					     length($val) ne 4)) {
59132435880fSJoe Perches						ERROR("NON_OCTAL_PERMISSIONS",
59141727cc70SJoe Perches						      "Use 4 digit octal (0777) not decimal permissions\n" . $herecurr);
5915c0a5c898SJoe Perches					} elsif ($val =~ /^$Octal$/ && (oct($val) & 02)) {
5916c0a5c898SJoe Perches						ERROR("EXPORTED_WORLD_WRITABLE",
5917c0a5c898SJoe Perches						      "Exporting writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
59182435880fSJoe Perches					}
59192435880fSJoe Perches				}
59202435880fSJoe Perches			}
592113214adfSAndy Whitcroft		}
59225a6d20ceSBjorn Andersson
59235a6d20ceSBjorn Andersson# validate content of MODULE_LICENSE against list from include/linux/module.h
59245a6d20ceSBjorn Andersson		if ($line =~ /\bMODULE_LICENSE\s*\(\s*($String)\s*\)/) {
59255a6d20ceSBjorn Andersson			my $extracted_string = get_quoted_string($line, $rawline);
59265a6d20ceSBjorn Andersson			my $valid_licenses = qr{
59275a6d20ceSBjorn Andersson						GPL|
59285a6d20ceSBjorn Andersson						GPL\ v2|
59295a6d20ceSBjorn Andersson						GPL\ and\ additional\ rights|
59305a6d20ceSBjorn Andersson						Dual\ BSD/GPL|
59315a6d20ceSBjorn Andersson						Dual\ MIT/GPL|
59325a6d20ceSBjorn Andersson						Dual\ MPL/GPL|
59335a6d20ceSBjorn Andersson						Proprietary
59345a6d20ceSBjorn Andersson					}x;
59355a6d20ceSBjorn Andersson			if ($extracted_string !~ /^"(?:$valid_licenses)"$/x) {
59365a6d20ceSBjorn Andersson				WARN("MODULE_LICENSE",
59375a6d20ceSBjorn Andersson				     "unknown module license " . $extracted_string . "\n" . $herecurr);
59385a6d20ceSBjorn Andersson			}
59395a6d20ceSBjorn Andersson		}
5940515a235eSJoe Perches	}
594113214adfSAndy Whitcroft
594213214adfSAndy Whitcroft	# If we have no input at all, then there is nothing to report on
594313214adfSAndy Whitcroft	# so just keep quiet.
594413214adfSAndy Whitcroft	if ($#rawlines == -1) {
594513214adfSAndy Whitcroft		exit(0);
59460a920b5bSAndy Whitcroft	}
59470a920b5bSAndy Whitcroft
59488905a67cSAndy Whitcroft	# In mailback mode only produce a report in the negative, for
59498905a67cSAndy Whitcroft	# things that appear to be patches.
59508905a67cSAndy Whitcroft	if ($mailback && ($clean == 1 || !$is_patch)) {
59518905a67cSAndy Whitcroft		exit(0);
59528905a67cSAndy Whitcroft	}
59538905a67cSAndy Whitcroft
59548905a67cSAndy Whitcroft	# This is not a patch, and we are are in 'no-patch' mode so
59558905a67cSAndy Whitcroft	# just keep quiet.
59568905a67cSAndy Whitcroft	if (!$chk_patch && !$is_patch) {
59578905a67cSAndy Whitcroft		exit(0);
59588905a67cSAndy Whitcroft	}
59598905a67cSAndy Whitcroft
596006330fc4SJoe Perches	if (!$is_patch && $file !~ /cover-letter\.patch$/) {
5961000d1cc1SJoe Perches		ERROR("NOT_UNIFIED_DIFF",
5962000d1cc1SJoe Perches		      "Does not appear to be a unified-diff format patch\n");
59630a920b5bSAndy Whitcroft	}
596434d8815fSJoe Perches	if ($is_patch && $filename ne '-' && $chk_signoff && $signoff == 0) {
5965000d1cc1SJoe Perches		ERROR("MISSING_SIGN_OFF",
5966000d1cc1SJoe Perches		      "Missing Signed-off-by: line(s)\n");
59670a920b5bSAndy Whitcroft	}
59680a920b5bSAndy Whitcroft
5969f0a594c1SAndy Whitcroft	print report_dump();
597013214adfSAndy Whitcroft	if ($summary && !($clean == 1 && $quiet == 1)) {
597113214adfSAndy Whitcroft		print "$filename " if ($summary_file);
59726c72ffaaSAndy Whitcroft		print "total: $cnt_error errors, $cnt_warn warnings, " .
59736c72ffaaSAndy Whitcroft			(($check)? "$cnt_chk checks, " : "") .
59746c72ffaaSAndy Whitcroft			"$cnt_lines lines checked\n";
59756c72ffaaSAndy Whitcroft	}
59768905a67cSAndy Whitcroft
5977d2c0a235SAndy Whitcroft	if ($quiet == 0) {
5978d2c0a235SAndy Whitcroft		# If there were whitespace errors which cleanpatch can fix
5979d2c0a235SAndy Whitcroft		# then suggest that.
5980d2c0a235SAndy Whitcroft		if ($rpt_cleaners) {
5981b0781216SMike Frysinger			$rpt_cleaners = 0;
5982d8469f16SJoe Perches			print << "EOM"
5983d8469f16SJoe Perches
5984d8469f16SJoe PerchesNOTE: Whitespace errors detected.
5985d8469f16SJoe Perches      You may wish to use scripts/cleanpatch or scripts/cleanfile
5986d8469f16SJoe PerchesEOM
5987d2c0a235SAndy Whitcroft		}
5988d2c0a235SAndy Whitcroft	}
5989d2c0a235SAndy Whitcroft
5990d752fcc8SJoe Perches	if ($clean == 0 && $fix &&
5991d752fcc8SJoe Perches	    ("@rawlines" ne "@fixed" ||
5992d752fcc8SJoe Perches	     $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) {
59939624b8d6SJoe Perches		my $newfile = $filename;
59949624b8d6SJoe Perches		$newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace);
59953705ce5bSJoe Perches		my $linecount = 0;
59963705ce5bSJoe Perches		my $f;
59973705ce5bSJoe Perches
5998d752fcc8SJoe Perches		@fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted);
5999d752fcc8SJoe Perches
60003705ce5bSJoe Perches		open($f, '>', $newfile)
60013705ce5bSJoe Perches		    or die "$P: Can't open $newfile for write\n";
60023705ce5bSJoe Perches		foreach my $fixed_line (@fixed) {
60033705ce5bSJoe Perches			$linecount++;
60043705ce5bSJoe Perches			if ($file) {
60053705ce5bSJoe Perches				if ($linecount > 3) {
60063705ce5bSJoe Perches					$fixed_line =~ s/^\+//;
60073705ce5bSJoe Perches					print $f $fixed_line . "\n";
60083705ce5bSJoe Perches				}
60093705ce5bSJoe Perches			} else {
60103705ce5bSJoe Perches				print $f $fixed_line . "\n";
60113705ce5bSJoe Perches			}
60123705ce5bSJoe Perches		}
60133705ce5bSJoe Perches		close($f);
60143705ce5bSJoe Perches
60153705ce5bSJoe Perches		if (!$quiet) {
60163705ce5bSJoe Perches			print << "EOM";
6017d8469f16SJoe Perches
60183705ce5bSJoe PerchesWrote EXPERIMENTAL --fix correction(s) to '$newfile'
60193705ce5bSJoe Perches
60203705ce5bSJoe PerchesDo _NOT_ trust the results written to this file.
60213705ce5bSJoe PerchesDo _NOT_ submit these changes without inspecting them for correctness.
60223705ce5bSJoe Perches
60233705ce5bSJoe PerchesThis EXPERIMENTAL file is simply a convenience to help rewrite patches.
60243705ce5bSJoe PerchesNo warranties, expressed or implied...
60253705ce5bSJoe PerchesEOM
60263705ce5bSJoe Perches		}
60273705ce5bSJoe Perches	}
60283705ce5bSJoe Perches
6029d8469f16SJoe Perches	if ($quiet == 0) {
6030d8469f16SJoe Perches		print "\n";
6031d8469f16SJoe Perches		if ($clean == 1) {
6032d8469f16SJoe Perches			print "$vname has no obvious style problems and is ready for submission.\n";
6033d8469f16SJoe Perches		} else {
6034d8469f16SJoe Perches			print "$vname has style problems, please review.\n";
60350a920b5bSAndy Whitcroft		}
60360a920b5bSAndy Whitcroft	}
60370a920b5bSAndy Whitcroft	return $clean;
60380a920b5bSAndy Whitcroft}
6039