xref: /linux-6.15/scripts/checkpatch.pl (revision e518e9a5)
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
20658cb3cf6SJoe Perches	if ($quiet == 0 && 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|
2676c72ffaaSAndy Whitcroft			__must_check|
2686c72ffaaSAndy Whitcroft			__init_refok|
269417495edSAndy Whitcroft			__kprobes|
270165e72a6SSven Eckelmann			__ref|
271165e72a6SSven Eckelmann			__rcu
2726c72ffaaSAndy Whitcroft		}x;
273e970b884SJoe Perchesour $InitAttributePrefix = qr{__(?:mem|cpu|dev|net_|)};
274e970b884SJoe Perchesour $InitAttributeData = qr{$InitAttributePrefix(?:initdata\b)};
275e970b884SJoe Perchesour $InitAttributeConst = qr{$InitAttributePrefix(?:initconst\b)};
276e970b884SJoe Perchesour $InitAttributeInit = qr{$InitAttributePrefix(?:init\b)};
277e970b884SJoe Perchesour $InitAttribute = qr{$InitAttributeData|$InitAttributeConst|$InitAttributeInit};
2788716de38SJoe Perches
27952131292SWolfram Sang# Notes to $Attribute:
28052131292SWolfram Sang# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
2816c72ffaaSAndy Whitcroftour $Attribute	= qr{
2826c72ffaaSAndy Whitcroft			const|
28303f1df7dSJoe Perches			__percpu|
28403f1df7dSJoe Perches			__nocast|
28503f1df7dSJoe Perches			__safe|
28603f1df7dSJoe Perches			__bitwise__|
28703f1df7dSJoe Perches			__packed__|
28803f1df7dSJoe Perches			__packed2__|
28903f1df7dSJoe Perches			__naked|
29003f1df7dSJoe Perches			__maybe_unused|
29103f1df7dSJoe Perches			__always_unused|
29203f1df7dSJoe Perches			__noreturn|
29303f1df7dSJoe Perches			__used|
29403f1df7dSJoe Perches			__cold|
295e23ef1f3SJoe Perches			__pure|
29603f1df7dSJoe Perches			__noclone|
29703f1df7dSJoe Perches			__deprecated|
2986c72ffaaSAndy Whitcroft			__read_mostly|
2996c72ffaaSAndy Whitcroft			__kprobes|
3008716de38SJoe Perches			$InitAttribute|
30124e1d81aSAndy Whitcroft			____cacheline_aligned|
30224e1d81aSAndy Whitcroft			____cacheline_aligned_in_smp|
3035fe3af11SAndy Whitcroft			____cacheline_internodealigned_in_smp|
3045fe3af11SAndy Whitcroft			__weak
3056c72ffaaSAndy Whitcroft		  }x;
306c45dcabdSAndy Whitcroftour $Modifier;
30791cb5195SJoe Perchesour $Inline	= qr{inline|__always_inline|noinline|__inline|__inline__};
3086c72ffaaSAndy Whitcroftour $Member	= qr{->$Ident|\.$Ident|\[[^]]*\]};
3096c72ffaaSAndy Whitcroftour $Lval	= qr{$Ident(?:$Member)*};
3106c72ffaaSAndy Whitcroft
31195e2c602SJoe Perchesour $Int_type	= qr{(?i)llu|ull|ll|lu|ul|l|u};
31295e2c602SJoe Perchesour $Binary	= qr{(?i)0b[01]+$Int_type?};
31395e2c602SJoe Perchesour $Hex	= qr{(?i)0x[0-9a-f]+$Int_type?};
31495e2c602SJoe Perchesour $Int	= qr{[0-9]+$Int_type?};
3152435880fSJoe Perchesour $Octal	= qr{0[0-7]+$Int_type?};
316c0a5c898SJoe Perchesour $String	= qr{"[X\t]*"};
317326b1ffcSJoe Perchesour $Float_hex	= qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?};
318326b1ffcSJoe Perchesour $Float_dec	= qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?};
319326b1ffcSJoe Perchesour $Float_int	= qr{(?i)[0-9]+e-?[0-9]+[fl]?};
32074349bccSJoe Perchesour $Float	= qr{$Float_hex|$Float_dec|$Float_int};
3212435880fSJoe Perchesour $Constant	= qr{$Float|$Binary|$Octal|$Hex|$Int};
322326b1ffcSJoe Perchesour $Assignment	= qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=};
323447432f3SJoe Perchesour $Compare    = qr{<=|>=|==|!=|<|(?<!-)>};
32423f780c9SJoe Perchesour $Arithmetic = qr{\+|-|\*|\/|%};
3256c72ffaaSAndy Whitcroftour $Operators	= qr{
3266c72ffaaSAndy Whitcroft			<=|>=|==|!=|
3276c72ffaaSAndy Whitcroft			=>|->|<<|>>|<|>|!|~|
32823f780c9SJoe Perches			&&|\|\||,|\^|\+\+|--|&|\||$Arithmetic
3296c72ffaaSAndy Whitcroft		  }x;
3306c72ffaaSAndy Whitcroft
33191cb5195SJoe Perchesour $c90_Keywords = qr{do|for|while|if|else|return|goto|continue|switch|default|case|break}x;
33291cb5195SJoe Perches
333ab7e23f3SJoe Perchesour $BasicType;
3348905a67cSAndy Whitcroftour $NonptrType;
3351813087dSJoe Perchesour $NonptrTypeMisordered;
3368716de38SJoe Perchesour $NonptrTypeWithAttr;
3378905a67cSAndy Whitcroftour $Type;
3381813087dSJoe Perchesour $TypeMisordered;
3398905a67cSAndy Whitcroftour $Declare;
3401813087dSJoe Perchesour $DeclareMisordered;
3418905a67cSAndy Whitcroft
34215662b3eSJoe Perchesour $NON_ASCII_UTF8	= qr{
34315662b3eSJoe Perches	[\xC2-\xDF][\x80-\xBF]               # non-overlong 2-byte
344171ae1a4SAndy Whitcroft	|  \xE0[\xA0-\xBF][\x80-\xBF]        # excluding overlongs
345171ae1a4SAndy Whitcroft	| [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}  # straight 3-byte
346171ae1a4SAndy Whitcroft	|  \xED[\x80-\x9F][\x80-\xBF]        # excluding surrogates
347171ae1a4SAndy Whitcroft	|  \xF0[\x90-\xBF][\x80-\xBF]{2}     # planes 1-3
348171ae1a4SAndy Whitcroft	| [\xF1-\xF3][\x80-\xBF]{3}          # planes 4-15
349171ae1a4SAndy Whitcroft	|  \xF4[\x80-\x8F][\x80-\xBF]{2}     # plane 16
350171ae1a4SAndy Whitcroft}x;
351171ae1a4SAndy Whitcroft
35215662b3eSJoe Perchesour $UTF8	= qr{
35315662b3eSJoe Perches	[\x09\x0A\x0D\x20-\x7E]              # ASCII
35415662b3eSJoe Perches	| $NON_ASCII_UTF8
35515662b3eSJoe Perches}x;
35615662b3eSJoe Perches
357e6176fa4SJoe Perchesour $typeC99Typedefs = qr{(?:__)?(?:[us]_?)?int_?(?:8|16|32|64)_t};
358021158b4SJoe Perchesour $typeOtherOSTypedefs = qr{(?x:
359021158b4SJoe Perches	u_(?:char|short|int|long) |          # bsd
360021158b4SJoe Perches	u(?:nchar|short|int|long)            # sysv
361021158b4SJoe Perches)};
362e6176fa4SJoe Perchesour $typeKernelTypedefs = qr{(?x:
363fb9e9096SAndy Whitcroft	(?:__)?(?:u|s|be|le)(?:8|16|32|64)|
3648ed22cadSAndy Whitcroft	atomic_t
3658ed22cadSAndy Whitcroft)};
366e6176fa4SJoe Perchesour $typeTypedefs = qr{(?x:
367e6176fa4SJoe Perches	$typeC99Typedefs\b|
368e6176fa4SJoe Perches	$typeOtherOSTypedefs\b|
369e6176fa4SJoe Perches	$typeKernelTypedefs\b
370e6176fa4SJoe Perches)};
3718ed22cadSAndy Whitcroft
372691e669bSJoe Perchesour $logFunctions = qr{(?x:
3736e60c02eSJoe Perches	printk(?:_ratelimited|_once|)|
3747d0b6594SJacob Keller	(?:[a-z0-9]+_){1,2}(?:printk|emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)(?:_ratelimited|_once|)|
3756e60c02eSJoe Perches	WARN(?:_RATELIMIT|_ONCE|)|
376b0531722SJoe Perches	panic|
37706668727SJoe Perches	MODULE_[A-Z_]+|
37806668727SJoe Perches	seq_vprintf|seq_printf|seq_puts
379691e669bSJoe Perches)};
380691e669bSJoe Perches
38120112475SJoe Perchesour $signature_tags = qr{(?xi:
38220112475SJoe Perches	Signed-off-by:|
38320112475SJoe Perches	Acked-by:|
38420112475SJoe Perches	Tested-by:|
38520112475SJoe Perches	Reviewed-by:|
38620112475SJoe Perches	Reported-by:|
3878543ae12SMugunthan V N	Suggested-by:|
38820112475SJoe Perches	To:|
38920112475SJoe Perches	Cc:
39020112475SJoe Perches)};
39120112475SJoe Perches
3921813087dSJoe Perchesour @typeListMisordered = (
3931813087dSJoe Perches	qr{char\s+(?:un)?signed},
3941813087dSJoe Perches	qr{int\s+(?:(?:un)?signed\s+)?short\s},
3951813087dSJoe Perches	qr{int\s+short(?:\s+(?:un)?signed)},
3961813087dSJoe Perches	qr{short\s+int(?:\s+(?:un)?signed)},
3971813087dSJoe Perches	qr{(?:un)?signed\s+int\s+short},
3981813087dSJoe Perches	qr{short\s+(?:un)?signed},
3991813087dSJoe Perches	qr{long\s+int\s+(?:un)?signed},
4001813087dSJoe Perches	qr{int\s+long\s+(?:un)?signed},
4011813087dSJoe Perches	qr{long\s+(?:un)?signed\s+int},
4021813087dSJoe Perches	qr{int\s+(?:un)?signed\s+long},
4031813087dSJoe Perches	qr{int\s+(?:un)?signed},
4041813087dSJoe Perches	qr{int\s+long\s+long\s+(?:un)?signed},
4051813087dSJoe Perches	qr{long\s+long\s+int\s+(?:un)?signed},
4061813087dSJoe Perches	qr{long\s+long\s+(?:un)?signed\s+int},
4071813087dSJoe Perches	qr{long\s+long\s+(?:un)?signed},
4081813087dSJoe Perches	qr{long\s+(?:un)?signed},
4091813087dSJoe Perches);
4101813087dSJoe Perches
4118905a67cSAndy Whitcroftour @typeList = (
4128905a67cSAndy Whitcroft	qr{void},
4130c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?char},
4140c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?short\s+int},
4150c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?short},
4160c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?int},
4170c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?long\s+int},
4180c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?long\s+long\s+int},
4190c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?long\s+long},
4200c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?long},
4210c773d9dSJoe Perches	qr{(?:un)?signed},
4228905a67cSAndy Whitcroft	qr{float},
4238905a67cSAndy Whitcroft	qr{double},
4248905a67cSAndy Whitcroft	qr{bool},
4258905a67cSAndy Whitcroft	qr{struct\s+$Ident},
4268905a67cSAndy Whitcroft	qr{union\s+$Ident},
4278905a67cSAndy Whitcroft	qr{enum\s+$Ident},
4288905a67cSAndy Whitcroft	qr{${Ident}_t},
4298905a67cSAndy Whitcroft	qr{${Ident}_handler},
4308905a67cSAndy Whitcroft	qr{${Ident}_handler_fn},
4311813087dSJoe Perches	@typeListMisordered,
4328905a67cSAndy Whitcroft);
433485ff23eSAlex Dowadour @typeListFile = ();
4348716de38SJoe Perchesour @typeListWithAttr = (
4358716de38SJoe Perches	@typeList,
4368716de38SJoe Perches	qr{struct\s+$InitAttribute\s+$Ident},
4378716de38SJoe Perches	qr{union\s+$InitAttribute\s+$Ident},
4388716de38SJoe Perches);
4398716de38SJoe Perches
440c45dcabdSAndy Whitcroftour @modifierList = (
441c45dcabdSAndy Whitcroft	qr{fastcall},
442c45dcabdSAndy Whitcroft);
443485ff23eSAlex Dowadour @modifierListFile = ();
4448905a67cSAndy Whitcroft
4452435880fSJoe Perchesour @mode_permission_funcs = (
4462435880fSJoe Perches	["module_param", 3],
4472435880fSJoe Perches	["module_param_(?:array|named|string)", 4],
4482435880fSJoe Perches	["module_param_array_named", 5],
4492435880fSJoe Perches	["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2],
4502435880fSJoe Perches	["proc_create(?:_data|)", 2],
4512435880fSJoe Perches	["(?:CLASS|DEVICE|SENSOR)_ATTR", 2],
4522435880fSJoe Perches);
4532435880fSJoe Perches
454515a235eSJoe Perches#Create a search pattern for all these functions to speed up a loop below
455515a235eSJoe Perchesour $mode_perms_search = "";
456515a235eSJoe Perchesforeach my $entry (@mode_permission_funcs) {
457515a235eSJoe Perches	$mode_perms_search .= '|' if ($mode_perms_search ne "");
458515a235eSJoe Perches	$mode_perms_search .= $entry->[0];
459515a235eSJoe Perches}
460515a235eSJoe Perches
461b392c64fSJoe Perchesour $mode_perms_world_writable = qr{
462b392c64fSJoe Perches	S_IWUGO		|
463b392c64fSJoe Perches	S_IWOTH		|
464b392c64fSJoe Perches	S_IRWXUGO	|
465b392c64fSJoe Perches	S_IALLUGO	|
466b392c64fSJoe Perches	0[0-7][0-7][2367]
467b392c64fSJoe Perches}x;
468b392c64fSJoe Perches
4697840a94cSWolfram Sangour $allowed_asm_includes = qr{(?x:
4707840a94cSWolfram Sang	irq|
471cdcee686SSergey Ryazanov	memory|
472cdcee686SSergey Ryazanov	time|
473cdcee686SSergey Ryazanov	reboot
4747840a94cSWolfram Sang)};
4757840a94cSWolfram Sang# memory.h: ARM has a custom one
4767840a94cSWolfram Sang
47766b47b4aSKees Cook# Load common spelling mistakes and build regular expression list.
47866b47b4aSKees Cookmy $misspellings;
47966b47b4aSKees Cookmy %spelling_fix;
48036061e38SJoe Perches
48136061e38SJoe Perchesif (open(my $spelling, '<', $spelling_file)) {
48266b47b4aSKees Cook	while (<$spelling>) {
48366b47b4aSKees Cook		my $line = $_;
48466b47b4aSKees Cook
48566b47b4aSKees Cook		$line =~ s/\s*\n?$//g;
48666b47b4aSKees Cook		$line =~ s/^\s*//g;
48766b47b4aSKees Cook
48866b47b4aSKees Cook		next if ($line =~ m/^\s*#/);
48966b47b4aSKees Cook		next if ($line =~ m/^\s*$/);
49066b47b4aSKees Cook
49166b47b4aSKees Cook		my ($suspect, $fix) = split(/\|\|/, $line);
49266b47b4aSKees Cook
49366b47b4aSKees Cook		$spelling_fix{$suspect} = $fix;
49466b47b4aSKees Cook	}
49566b47b4aSKees Cook	close($spelling);
49636061e38SJoe Perches} else {
49736061e38SJoe Perches	warn "No typos will be found - file '$spelling_file': $!\n";
49836061e38SJoe Perches}
49966b47b4aSKees Cook
500ebfd7d62SJoe Perchesif ($codespell) {
501ebfd7d62SJoe Perches	if (open(my $spelling, '<', $codespellfile)) {
502ebfd7d62SJoe Perches		while (<$spelling>) {
503ebfd7d62SJoe Perches			my $line = $_;
504ebfd7d62SJoe Perches
505ebfd7d62SJoe Perches			$line =~ s/\s*\n?$//g;
506ebfd7d62SJoe Perches			$line =~ s/^\s*//g;
507ebfd7d62SJoe Perches
508ebfd7d62SJoe Perches			next if ($line =~ m/^\s*#/);
509ebfd7d62SJoe Perches			next if ($line =~ m/^\s*$/);
510ebfd7d62SJoe Perches			next if ($line =~ m/, disabled/i);
511ebfd7d62SJoe Perches
512ebfd7d62SJoe Perches			$line =~ s/,.*$//;
513ebfd7d62SJoe Perches
514ebfd7d62SJoe Perches			my ($suspect, $fix) = split(/->/, $line);
515ebfd7d62SJoe Perches
516ebfd7d62SJoe Perches			$spelling_fix{$suspect} = $fix;
517ebfd7d62SJoe Perches		}
518ebfd7d62SJoe Perches		close($spelling);
519ebfd7d62SJoe Perches	} else {
520ebfd7d62SJoe Perches		warn "No codespell typos will be found - file '$codespellfile': $!\n";
521ebfd7d62SJoe Perches	}
522ebfd7d62SJoe Perches}
523ebfd7d62SJoe Perches
524ebfd7d62SJoe Perches$misspellings = join("|", sort keys %spelling_fix) if keys %spelling_fix;
525ebfd7d62SJoe Perches
5268905a67cSAndy Whitcroftsub build_types {
527485ff23eSAlex Dowad	my $mods = "(?x:  \n" . join("|\n  ", (@modifierList, @modifierListFile)) . "\n)";
528485ff23eSAlex Dowad	my $all = "(?x:  \n" . join("|\n  ", (@typeList, @typeListFile)) . "\n)";
5291813087dSJoe Perches	my $Misordered = "(?x:  \n" . join("|\n  ", @typeListMisordered) . "\n)";
5308716de38SJoe Perches	my $allWithAttr = "(?x:  \n" . join("|\n  ", @typeListWithAttr) . "\n)";
531c8cb2ca3SAndy Whitcroft	$Modifier	= qr{(?:$Attribute|$Sparse|$mods)};
532ab7e23f3SJoe Perches	$BasicType	= qr{
533ab7e23f3SJoe Perches				(?:$typeTypedefs\b)|
534ab7e23f3SJoe Perches				(?:${all}\b)
535ab7e23f3SJoe Perches		}x;
5368905a67cSAndy Whitcroft	$NonptrType	= qr{
537d2172eb5SAndy Whitcroft			(?:$Modifier\s+|const\s+)*
538cf655043SAndy Whitcroft			(?:
5396b48db24SAndy Whitcroft				(?:typeof|__typeof__)\s*\([^\)]*\)|
5408ed22cadSAndy Whitcroft				(?:$typeTypedefs\b)|
541c45dcabdSAndy Whitcroft				(?:${all}\b)
542cf655043SAndy Whitcroft			)
543c8cb2ca3SAndy Whitcroft			(?:\s+$Modifier|\s+const)*
5448905a67cSAndy Whitcroft		  }x;
5451813087dSJoe Perches	$NonptrTypeMisordered	= qr{
5461813087dSJoe Perches			(?:$Modifier\s+|const\s+)*
5471813087dSJoe Perches			(?:
5481813087dSJoe Perches				(?:${Misordered}\b)
5491813087dSJoe Perches			)
5501813087dSJoe Perches			(?:\s+$Modifier|\s+const)*
5511813087dSJoe Perches		  }x;
5528716de38SJoe Perches	$NonptrTypeWithAttr	= qr{
5538716de38SJoe Perches			(?:$Modifier\s+|const\s+)*
5548716de38SJoe Perches			(?:
5558716de38SJoe Perches				(?:typeof|__typeof__)\s*\([^\)]*\)|
5568716de38SJoe Perches				(?:$typeTypedefs\b)|
5578716de38SJoe Perches				(?:${allWithAttr}\b)
5588716de38SJoe Perches			)
5598716de38SJoe Perches			(?:\s+$Modifier|\s+const)*
5608716de38SJoe Perches		  }x;
5618905a67cSAndy Whitcroft	$Type	= qr{
562c45dcabdSAndy Whitcroft			$NonptrType
5631574a29fSJoe Perches			(?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
564c8cb2ca3SAndy Whitcroft			(?:\s+$Inline|\s+$Modifier)*
5658905a67cSAndy Whitcroft		  }x;
5661813087dSJoe Perches	$TypeMisordered	= qr{
5671813087dSJoe Perches			$NonptrTypeMisordered
5681813087dSJoe Perches			(?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
5691813087dSJoe Perches			(?:\s+$Inline|\s+$Modifier)*
5701813087dSJoe Perches		  }x;
57191cb5195SJoe Perches	$Declare	= qr{(?:$Storage\s+(?:$Inline\s+)?)?$Type};
5721813087dSJoe Perches	$DeclareMisordered	= qr{(?:$Storage\s+(?:$Inline\s+)?)?$TypeMisordered};
5738905a67cSAndy Whitcroft}
5748905a67cSAndy Whitcroftbuild_types();
5756c72ffaaSAndy Whitcroft
5767d2367afSJoe Perchesour $Typecast	= qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
577d1fe9c09SJoe Perches
578d1fe9c09SJoe Perches# Using $balanced_parens, $LvalOrFunc, or $FuncArg
579d1fe9c09SJoe Perches# requires at least perl version v5.10.0
580d1fe9c09SJoe Perches# Any use must be runtime checked with $^V
581d1fe9c09SJoe Perches
582d1fe9c09SJoe Perchesour $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
5832435880fSJoe Perchesour $LvalOrFunc	= qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*};
584c0a5c898SJoe Perchesour $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant|$String)};
5857d2367afSJoe Perches
586f8422308SJoe Perchesour $declaration_macros = qr{(?x:
587f8422308SJoe Perches	(?:$Storage\s+)?(?:[A-Z_][A-Z0-9]*_){0,2}(?:DEFINE|DECLARE)(?:_[A-Z0-9]+){1,2}\s*\(|
588f8422308SJoe Perches	(?:$Storage\s+)?LIST_HEAD\s*\(|
589f8422308SJoe Perches	(?:$Storage\s+)?${Type}\s+uninitialized_var\s*\(
590f8422308SJoe Perches)};
591f8422308SJoe Perches
5927d2367afSJoe Perchessub deparenthesize {
5937d2367afSJoe Perches	my ($string) = @_;
5947d2367afSJoe Perches	return "" if (!defined($string));
5955b9553abSJoe Perches
5965b9553abSJoe Perches	while ($string =~ /^\s*\(.*\)\s*$/) {
5975b9553abSJoe Perches		$string =~ s@^\s*\(\s*@@;
5985b9553abSJoe Perches		$string =~ s@\s*\)\s*$@@;
5995b9553abSJoe Perches	}
6005b9553abSJoe Perches
6017d2367afSJoe Perches	$string =~ s@\s+@ @g;
6025b9553abSJoe Perches
6037d2367afSJoe Perches	return $string;
6047d2367afSJoe Perches}
6057d2367afSJoe Perches
6063445686aSJoe Perchessub seed_camelcase_file {
6073445686aSJoe Perches	my ($file) = @_;
6083445686aSJoe Perches
6093445686aSJoe Perches	return if (!(-f $file));
6103445686aSJoe Perches
6113445686aSJoe Perches	local $/;
6123445686aSJoe Perches
6133445686aSJoe Perches	open(my $include_file, '<', "$file")
6143445686aSJoe Perches	    or warn "$P: Can't read '$file' $!\n";
6153445686aSJoe Perches	my $text = <$include_file>;
6163445686aSJoe Perches	close($include_file);
6173445686aSJoe Perches
6183445686aSJoe Perches	my @lines = split('\n', $text);
6193445686aSJoe Perches
6203445686aSJoe Perches	foreach my $line (@lines) {
6213445686aSJoe Perches		next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/);
6223445686aSJoe Perches		if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) {
6233445686aSJoe Perches			$camelcase{$1} = 1;
62411ea516aSJoe Perches		} elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) {
62511ea516aSJoe Perches			$camelcase{$1} = 1;
62611ea516aSJoe Perches		} elsif ($line =~ /^\s*(?:union|struct|enum)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[;\{]/) {
6273445686aSJoe Perches			$camelcase{$1} = 1;
6283445686aSJoe Perches		}
6293445686aSJoe Perches	}
6303445686aSJoe Perches}
6313445686aSJoe Perches
6323445686aSJoe Perchesmy $camelcase_seeded = 0;
6333445686aSJoe Perchessub seed_camelcase_includes {
6343445686aSJoe Perches	return if ($camelcase_seeded);
6353445686aSJoe Perches
6363445686aSJoe Perches	my $files;
637c707a81dSJoe Perches	my $camelcase_cache = "";
638c707a81dSJoe Perches	my @include_files = ();
639c707a81dSJoe Perches
640c707a81dSJoe Perches	$camelcase_seeded = 1;
641351b2a1fSJoe Perches
6423645e328SRichard Genoud	if (-e ".git") {
643351b2a1fSJoe Perches		my $git_last_include_commit = `git log --no-merges --pretty=format:"%h%n" -1 -- include`;
644351b2a1fSJoe Perches		chomp $git_last_include_commit;
645c707a81dSJoe Perches		$camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit";
646c707a81dSJoe Perches	} else {
647c707a81dSJoe Perches		my $last_mod_date = 0;
648c707a81dSJoe Perches		$files = `find $root/include -name "*.h"`;
649c707a81dSJoe Perches		@include_files = split('\n', $files);
650c707a81dSJoe Perches		foreach my $file (@include_files) {
651c707a81dSJoe Perches			my $date = POSIX::strftime("%Y%m%d%H%M",
652c707a81dSJoe Perches						   localtime((stat $file)[9]));
653c707a81dSJoe Perches			$last_mod_date = $date if ($last_mod_date < $date);
654c707a81dSJoe Perches		}
655c707a81dSJoe Perches		$camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date";
656c707a81dSJoe Perches	}
657c707a81dSJoe Perches
658c707a81dSJoe Perches	if ($camelcase_cache ne "" && -f $camelcase_cache) {
659c707a81dSJoe Perches		open(my $camelcase_file, '<', "$camelcase_cache")
660c707a81dSJoe Perches		    or warn "$P: Can't read '$camelcase_cache' $!\n";
661351b2a1fSJoe Perches		while (<$camelcase_file>) {
662351b2a1fSJoe Perches			chomp;
663351b2a1fSJoe Perches			$camelcase{$_} = 1;
664351b2a1fSJoe Perches		}
665351b2a1fSJoe Perches		close($camelcase_file);
666351b2a1fSJoe Perches
667351b2a1fSJoe Perches		return;
668351b2a1fSJoe Perches	}
669c707a81dSJoe Perches
6703645e328SRichard Genoud	if (-e ".git") {
671c707a81dSJoe Perches		$files = `git ls-files "include/*.h"`;
672c707a81dSJoe Perches		@include_files = split('\n', $files);
6733445686aSJoe Perches	}
674c707a81dSJoe Perches
6753445686aSJoe Perches	foreach my $file (@include_files) {
6763445686aSJoe Perches		seed_camelcase_file($file);
6773445686aSJoe Perches	}
678351b2a1fSJoe Perches
679c707a81dSJoe Perches	if ($camelcase_cache ne "") {
680351b2a1fSJoe Perches		unlink glob ".checkpatch-camelcase.*";
681c707a81dSJoe Perches		open(my $camelcase_file, '>', "$camelcase_cache")
682c707a81dSJoe Perches		    or warn "$P: Can't write '$camelcase_cache' $!\n";
683351b2a1fSJoe Perches		foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) {
684351b2a1fSJoe Perches			print $camelcase_file ("$_\n");
685351b2a1fSJoe Perches		}
686351b2a1fSJoe Perches		close($camelcase_file);
687351b2a1fSJoe Perches	}
6883445686aSJoe Perches}
6893445686aSJoe Perches
690d311cd44SJoe Perchessub git_commit_info {
691d311cd44SJoe Perches	my ($commit, $id, $desc) = @_;
692d311cd44SJoe Perches
693d311cd44SJoe Perches	return ($id, $desc) if ((which("git") eq "") || !(-e ".git"));
694d311cd44SJoe Perches
695d311cd44SJoe Perches	my $output = `git log --no-color --format='%H %s' -1 $commit 2>&1`;
696d311cd44SJoe Perches	$output =~ s/^\s*//gm;
697d311cd44SJoe Perches	my @lines = split("\n", $output);
698d311cd44SJoe Perches
6990d7835fcSJoe Perches	return ($id, $desc) if ($#lines < 0);
7000d7835fcSJoe Perches
701d311cd44SJoe Perches	if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous\./) {
702d311cd44SJoe Perches# Maybe one day convert this block of bash into something that returns
703d311cd44SJoe Perches# all matching commit ids, but it's very slow...
704d311cd44SJoe Perches#
705d311cd44SJoe Perches#		echo "checking commits $1..."
706d311cd44SJoe Perches#		git rev-list --remotes | grep -i "^$1" |
707d311cd44SJoe Perches#		while read line ; do
708d311cd44SJoe Perches#		    git log --format='%H %s' -1 $line |
709d311cd44SJoe Perches#		    echo "commit $(cut -c 1-12,41-)"
710d311cd44SJoe Perches#		done
711d311cd44SJoe Perches	} elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./) {
712d311cd44SJoe Perches	} else {
713d311cd44SJoe Perches		$id = substr($lines[0], 0, 12);
714d311cd44SJoe Perches		$desc = substr($lines[0], 41);
715d311cd44SJoe Perches	}
716d311cd44SJoe Perches
717d311cd44SJoe Perches	return ($id, $desc);
718d311cd44SJoe Perches}
719d311cd44SJoe Perches
7206c72ffaaSAndy Whitcroft$chk_signoff = 0 if ($file);
7210a920b5bSAndy Whitcroft
72200df344fSAndy Whitcroftmy @rawlines = ();
723c2fdda0dSAndy Whitcroftmy @lines = ();
7243705ce5bSJoe Perchesmy @fixed = ();
725d752fcc8SJoe Perchesmy @fixed_inserted = ();
726d752fcc8SJoe Perchesmy @fixed_deleted = ();
727194f66fcSJoe Perchesmy $fixlinenr = -1;
728194f66fcSJoe Perches
729c2fdda0dSAndy Whitcroftmy $vname;
7306c72ffaaSAndy Whitcroftfor my $filename (@ARGV) {
73121caa13cSAndy Whitcroft	my $FILE;
7326c72ffaaSAndy Whitcroft	if ($file) {
73321caa13cSAndy Whitcroft		open($FILE, '-|', "diff -u /dev/null $filename") ||
7346c72ffaaSAndy Whitcroft			die "$P: $filename: diff failed - $!\n";
73521caa13cSAndy Whitcroft	} elsif ($filename eq '-') {
73621caa13cSAndy Whitcroft		open($FILE, '<&STDIN');
7376c72ffaaSAndy Whitcroft	} else {
73821caa13cSAndy Whitcroft		open($FILE, '<', "$filename") ||
7396c72ffaaSAndy Whitcroft			die "$P: $filename: open failed - $!\n";
7406c72ffaaSAndy Whitcroft	}
741c2fdda0dSAndy Whitcroft	if ($filename eq '-') {
742c2fdda0dSAndy Whitcroft		$vname = 'Your patch';
743c2fdda0dSAndy Whitcroft	} else {
744c2fdda0dSAndy Whitcroft		$vname = $filename;
745c2fdda0dSAndy Whitcroft	}
74621caa13cSAndy Whitcroft	while (<$FILE>) {
7470a920b5bSAndy Whitcroft		chomp;
74800df344fSAndy Whitcroft		push(@rawlines, $_);
7496c72ffaaSAndy Whitcroft	}
75021caa13cSAndy Whitcroft	close($FILE);
751d8469f16SJoe Perches
752d8469f16SJoe Perches	if ($#ARGV > 0 && $quiet == 0) {
753d8469f16SJoe Perches		print '-' x length($vname) . "\n";
754d8469f16SJoe Perches		print "$vname\n";
755d8469f16SJoe Perches		print '-' x length($vname) . "\n";
756d8469f16SJoe Perches	}
757d8469f16SJoe Perches
758c2fdda0dSAndy Whitcroft	if (!process($filename)) {
7590a920b5bSAndy Whitcroft		$exit = 1;
7600a920b5bSAndy Whitcroft	}
76100df344fSAndy Whitcroft	@rawlines = ();
76213214adfSAndy Whitcroft	@lines = ();
7633705ce5bSJoe Perches	@fixed = ();
764d752fcc8SJoe Perches	@fixed_inserted = ();
765d752fcc8SJoe Perches	@fixed_deleted = ();
766194f66fcSJoe Perches	$fixlinenr = -1;
767485ff23eSAlex Dowad	@modifierListFile = ();
768485ff23eSAlex Dowad	@typeListFile = ();
769485ff23eSAlex Dowad	build_types();
7700a920b5bSAndy Whitcroft}
7710a920b5bSAndy Whitcroft
772d8469f16SJoe Perchesif (!$quiet) {
773d8469f16SJoe Perches	if ($^V lt 5.10.0) {
774d8469f16SJoe Perches		print << "EOM"
775d8469f16SJoe Perches
776d8469f16SJoe PerchesNOTE: perl $^V is not modern enough to detect all possible issues.
777d8469f16SJoe Perches      An upgrade to at least perl v5.10.0 is suggested.
778d8469f16SJoe PerchesEOM
779d8469f16SJoe Perches	}
780d8469f16SJoe Perches	if ($exit) {
781d8469f16SJoe Perches		print << "EOM"
782d8469f16SJoe Perches
783d8469f16SJoe PerchesNOTE: If any of the errors are false positives, please report
784d8469f16SJoe Perches      them to the maintainer, see CHECKPATCH in MAINTAINERS.
785d8469f16SJoe PerchesEOM
786d8469f16SJoe Perches	}
787d8469f16SJoe Perches}
788d8469f16SJoe Perches
7890a920b5bSAndy Whitcroftexit($exit);
7900a920b5bSAndy Whitcroft
7910a920b5bSAndy Whitcroftsub top_of_kernel_tree {
7926c72ffaaSAndy Whitcroft	my ($root) = @_;
7936c72ffaaSAndy Whitcroft
7946c72ffaaSAndy Whitcroft	my @tree_check = (
7956c72ffaaSAndy Whitcroft		"COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
7966c72ffaaSAndy Whitcroft		"README", "Documentation", "arch", "include", "drivers",
7976c72ffaaSAndy Whitcroft		"fs", "init", "ipc", "kernel", "lib", "scripts",
7986c72ffaaSAndy Whitcroft	);
7996c72ffaaSAndy Whitcroft
8006c72ffaaSAndy Whitcroft	foreach my $check (@tree_check) {
8016c72ffaaSAndy Whitcroft		if (! -e $root . '/' . $check) {
8020a920b5bSAndy Whitcroft			return 0;
8030a920b5bSAndy Whitcroft		}
8046c72ffaaSAndy Whitcroft	}
8056c72ffaaSAndy Whitcroft	return 1;
8066c72ffaaSAndy Whitcroft}
8070a920b5bSAndy Whitcroft
80820112475SJoe Perchessub parse_email {
80920112475SJoe Perches	my ($formatted_email) = @_;
81020112475SJoe Perches
81120112475SJoe Perches	my $name = "";
81220112475SJoe Perches	my $address = "";
81320112475SJoe Perches	my $comment = "";
81420112475SJoe Perches
81520112475SJoe Perches	if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
81620112475SJoe Perches		$name = $1;
81720112475SJoe Perches		$address = $2;
81820112475SJoe Perches		$comment = $3 if defined $3;
81920112475SJoe Perches	} elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
82020112475SJoe Perches		$address = $1;
82120112475SJoe Perches		$comment = $2 if defined $2;
82220112475SJoe Perches	} elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
82320112475SJoe Perches		$address = $1;
82420112475SJoe Perches		$comment = $2 if defined $2;
82520112475SJoe Perches		$formatted_email =~ s/$address.*$//;
82620112475SJoe Perches		$name = $formatted_email;
8273705ce5bSJoe Perches		$name = trim($name);
82820112475SJoe Perches		$name =~ s/^\"|\"$//g;
82920112475SJoe Perches		# If there's a name left after stripping spaces and
83020112475SJoe Perches		# leading quotes, and the address doesn't have both
83120112475SJoe Perches		# leading and trailing angle brackets, the address
83220112475SJoe Perches		# is invalid. ie:
83320112475SJoe Perches		#   "joe smith [email protected]" bad
83420112475SJoe Perches		#   "joe smith <[email protected]" bad
83520112475SJoe Perches		if ($name ne "" && $address !~ /^<[^>]+>$/) {
83620112475SJoe Perches			$name = "";
83720112475SJoe Perches			$address = "";
83820112475SJoe Perches			$comment = "";
83920112475SJoe Perches		}
84020112475SJoe Perches	}
84120112475SJoe Perches
8423705ce5bSJoe Perches	$name = trim($name);
84320112475SJoe Perches	$name =~ s/^\"|\"$//g;
8443705ce5bSJoe Perches	$address = trim($address);
84520112475SJoe Perches	$address =~ s/^\<|\>$//g;
84620112475SJoe Perches
84720112475SJoe Perches	if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
84820112475SJoe Perches		$name =~ s/(?<!\\)"/\\"/g; ##escape quotes
84920112475SJoe Perches		$name = "\"$name\"";
85020112475SJoe Perches	}
85120112475SJoe Perches
85220112475SJoe Perches	return ($name, $address, $comment);
85320112475SJoe Perches}
85420112475SJoe Perches
85520112475SJoe Perchessub format_email {
85620112475SJoe Perches	my ($name, $address) = @_;
85720112475SJoe Perches
85820112475SJoe Perches	my $formatted_email;
85920112475SJoe Perches
8603705ce5bSJoe Perches	$name = trim($name);
86120112475SJoe Perches	$name =~ s/^\"|\"$//g;
8623705ce5bSJoe Perches	$address = trim($address);
86320112475SJoe Perches
86420112475SJoe Perches	if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
86520112475SJoe Perches		$name =~ s/(?<!\\)"/\\"/g; ##escape quotes
86620112475SJoe Perches		$name = "\"$name\"";
86720112475SJoe Perches	}
86820112475SJoe Perches
86920112475SJoe Perches	if ("$name" eq "") {
87020112475SJoe Perches		$formatted_email = "$address";
87120112475SJoe Perches	} else {
87220112475SJoe Perches		$formatted_email = "$name <$address>";
87320112475SJoe Perches	}
87420112475SJoe Perches
87520112475SJoe Perches	return $formatted_email;
87620112475SJoe Perches}
87720112475SJoe Perches
878d311cd44SJoe Perchessub which {
879d311cd44SJoe Perches	my ($bin) = @_;
880d311cd44SJoe Perches
881d311cd44SJoe Perches	foreach my $path (split(/:/, $ENV{PATH})) {
882d311cd44SJoe Perches		if (-e "$path/$bin") {
883d311cd44SJoe Perches			return "$path/$bin";
884d311cd44SJoe Perches		}
885d311cd44SJoe Perches	}
886d311cd44SJoe Perches
887d311cd44SJoe Perches	return "";
888d311cd44SJoe Perches}
889d311cd44SJoe Perches
890000d1cc1SJoe Perchessub which_conf {
891000d1cc1SJoe Perches	my ($conf) = @_;
892000d1cc1SJoe Perches
893000d1cc1SJoe Perches	foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
894000d1cc1SJoe Perches		if (-e "$path/$conf") {
895000d1cc1SJoe Perches			return "$path/$conf";
896000d1cc1SJoe Perches		}
897000d1cc1SJoe Perches	}
898000d1cc1SJoe Perches
899000d1cc1SJoe Perches	return "";
900000d1cc1SJoe Perches}
901000d1cc1SJoe Perches
9020a920b5bSAndy Whitcroftsub expand_tabs {
9030a920b5bSAndy Whitcroft	my ($str) = @_;
9040a920b5bSAndy Whitcroft
9050a920b5bSAndy Whitcroft	my $res = '';
9060a920b5bSAndy Whitcroft	my $n = 0;
9070a920b5bSAndy Whitcroft	for my $c (split(//, $str)) {
9080a920b5bSAndy Whitcroft		if ($c eq "\t") {
9090a920b5bSAndy Whitcroft			$res .= ' ';
9100a920b5bSAndy Whitcroft			$n++;
9110a920b5bSAndy Whitcroft			for (; ($n % 8) != 0; $n++) {
9120a920b5bSAndy Whitcroft				$res .= ' ';
9130a920b5bSAndy Whitcroft			}
9140a920b5bSAndy Whitcroft			next;
9150a920b5bSAndy Whitcroft		}
9160a920b5bSAndy Whitcroft		$res .= $c;
9170a920b5bSAndy Whitcroft		$n++;
9180a920b5bSAndy Whitcroft	}
9190a920b5bSAndy Whitcroft
9200a920b5bSAndy Whitcroft	return $res;
9210a920b5bSAndy Whitcroft}
9226c72ffaaSAndy Whitcroftsub copy_spacing {
923773647a0SAndy Whitcroft	(my $res = shift) =~ tr/\t/ /c;
9246c72ffaaSAndy Whitcroft	return $res;
9256c72ffaaSAndy Whitcroft}
9260a920b5bSAndy Whitcroft
9274a0df2efSAndy Whitcroftsub line_stats {
9284a0df2efSAndy Whitcroft	my ($line) = @_;
9294a0df2efSAndy Whitcroft
9304a0df2efSAndy Whitcroft	# Drop the diff line leader and expand tabs
9314a0df2efSAndy Whitcroft	$line =~ s/^.//;
9324a0df2efSAndy Whitcroft	$line = expand_tabs($line);
9334a0df2efSAndy Whitcroft
9344a0df2efSAndy Whitcroft	# Pick the indent from the front of the line.
9354a0df2efSAndy Whitcroft	my ($white) = ($line =~ /^(\s*)/);
9364a0df2efSAndy Whitcroft
9374a0df2efSAndy Whitcroft	return (length($line), length($white));
9384a0df2efSAndy Whitcroft}
9394a0df2efSAndy Whitcroft
940773647a0SAndy Whitcroftmy $sanitise_quote = '';
941773647a0SAndy Whitcroft
942773647a0SAndy Whitcroftsub sanitise_line_reset {
943773647a0SAndy Whitcroft	my ($in_comment) = @_;
944773647a0SAndy Whitcroft
945773647a0SAndy Whitcroft	if ($in_comment) {
946773647a0SAndy Whitcroft		$sanitise_quote = '*/';
947773647a0SAndy Whitcroft	} else {
948773647a0SAndy Whitcroft		$sanitise_quote = '';
949773647a0SAndy Whitcroft	}
950773647a0SAndy Whitcroft}
95100df344fSAndy Whitcroftsub sanitise_line {
95200df344fSAndy Whitcroft	my ($line) = @_;
95300df344fSAndy Whitcroft
95400df344fSAndy Whitcroft	my $res = '';
95500df344fSAndy Whitcroft	my $l = '';
95600df344fSAndy Whitcroft
957c2fdda0dSAndy Whitcroft	my $qlen = 0;
958773647a0SAndy Whitcroft	my $off = 0;
959773647a0SAndy Whitcroft	my $c;
96000df344fSAndy Whitcroft
961773647a0SAndy Whitcroft	# Always copy over the diff marker.
962773647a0SAndy Whitcroft	$res = substr($line, 0, 1);
963773647a0SAndy Whitcroft
964773647a0SAndy Whitcroft	for ($off = 1; $off < length($line); $off++) {
965773647a0SAndy Whitcroft		$c = substr($line, $off, 1);
966773647a0SAndy Whitcroft
967773647a0SAndy Whitcroft		# Comments we are wacking completly including the begin
968773647a0SAndy Whitcroft		# and end, all to $;.
969773647a0SAndy Whitcroft		if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
970773647a0SAndy Whitcroft			$sanitise_quote = '*/';
971773647a0SAndy Whitcroft
972773647a0SAndy Whitcroft			substr($res, $off, 2, "$;$;");
973773647a0SAndy Whitcroft			$off++;
97400df344fSAndy Whitcroft			next;
975773647a0SAndy Whitcroft		}
97681bc0e02SAndy Whitcroft		if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
977773647a0SAndy Whitcroft			$sanitise_quote = '';
978773647a0SAndy Whitcroft			substr($res, $off, 2, "$;$;");
979773647a0SAndy Whitcroft			$off++;
980773647a0SAndy Whitcroft			next;
981773647a0SAndy Whitcroft		}
982113f04a8SDaniel Walker		if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
983113f04a8SDaniel Walker			$sanitise_quote = '//';
984113f04a8SDaniel Walker
985113f04a8SDaniel Walker			substr($res, $off, 2, $sanitise_quote);
986113f04a8SDaniel Walker			$off++;
987113f04a8SDaniel Walker			next;
988113f04a8SDaniel Walker		}
989773647a0SAndy Whitcroft
990773647a0SAndy Whitcroft		# A \ in a string means ignore the next character.
991773647a0SAndy Whitcroft		if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
992773647a0SAndy Whitcroft		    $c eq "\\") {
993773647a0SAndy Whitcroft			substr($res, $off, 2, 'XX');
994773647a0SAndy Whitcroft			$off++;
995773647a0SAndy Whitcroft			next;
996773647a0SAndy Whitcroft		}
997773647a0SAndy Whitcroft		# Regular quotes.
998773647a0SAndy Whitcroft		if ($c eq "'" || $c eq '"') {
999773647a0SAndy Whitcroft			if ($sanitise_quote eq '') {
1000773647a0SAndy Whitcroft				$sanitise_quote = $c;
1001773647a0SAndy Whitcroft
1002773647a0SAndy Whitcroft				substr($res, $off, 1, $c);
1003773647a0SAndy Whitcroft				next;
1004773647a0SAndy Whitcroft			} elsif ($sanitise_quote eq $c) {
1005773647a0SAndy Whitcroft				$sanitise_quote = '';
100600df344fSAndy Whitcroft			}
100700df344fSAndy Whitcroft		}
1008773647a0SAndy Whitcroft
1009fae17daeSAndy Whitcroft		#print "c<$c> SQ<$sanitise_quote>\n";
1010773647a0SAndy Whitcroft		if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
1011773647a0SAndy Whitcroft			substr($res, $off, 1, $;);
1012113f04a8SDaniel Walker		} elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
1013113f04a8SDaniel Walker			substr($res, $off, 1, $;);
1014773647a0SAndy Whitcroft		} elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
1015773647a0SAndy Whitcroft			substr($res, $off, 1, 'X');
101600df344fSAndy Whitcroft		} else {
1017773647a0SAndy Whitcroft			substr($res, $off, 1, $c);
101800df344fSAndy Whitcroft		}
1019c2fdda0dSAndy Whitcroft	}
1020c2fdda0dSAndy Whitcroft
1021113f04a8SDaniel Walker	if ($sanitise_quote eq '//') {
1022113f04a8SDaniel Walker		$sanitise_quote = '';
1023113f04a8SDaniel Walker	}
1024113f04a8SDaniel Walker
1025c2fdda0dSAndy Whitcroft	# The pathname on a #include may be surrounded by '<' and '>'.
1026c45dcabdSAndy Whitcroft	if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
1027c2fdda0dSAndy Whitcroft		my $clean = 'X' x length($1);
1028c2fdda0dSAndy Whitcroft		$res =~ s@\<.*\>@<$clean>@;
1029c2fdda0dSAndy Whitcroft
1030c2fdda0dSAndy Whitcroft	# The whole of a #error is a string.
1031c45dcabdSAndy Whitcroft	} elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
1032c2fdda0dSAndy Whitcroft		my $clean = 'X' x length($1);
1033c45dcabdSAndy Whitcroft		$res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
1034c2fdda0dSAndy Whitcroft	}
1035c2fdda0dSAndy Whitcroft
103600df344fSAndy Whitcroft	return $res;
103700df344fSAndy Whitcroft}
103800df344fSAndy Whitcroft
1039a6962d72SJoe Perchessub get_quoted_string {
1040a6962d72SJoe Perches	my ($line, $rawline) = @_;
1041a6962d72SJoe Perches
104233acb54aSJoe Perches	return "" if ($line !~ m/($String)/g);
1043a6962d72SJoe Perches	return substr($rawline, $-[0], $+[0] - $-[0]);
1044a6962d72SJoe Perches}
1045a6962d72SJoe Perches
10468905a67cSAndy Whitcroftsub ctx_statement_block {
10478905a67cSAndy Whitcroft	my ($linenr, $remain, $off) = @_;
10488905a67cSAndy Whitcroft	my $line = $linenr - 1;
10498905a67cSAndy Whitcroft	my $blk = '';
10508905a67cSAndy Whitcroft	my $soff = $off;
10518905a67cSAndy Whitcroft	my $coff = $off - 1;
1052773647a0SAndy Whitcroft	my $coff_set = 0;
10538905a67cSAndy Whitcroft
105413214adfSAndy Whitcroft	my $loff = 0;
105513214adfSAndy Whitcroft
10568905a67cSAndy Whitcroft	my $type = '';
10578905a67cSAndy Whitcroft	my $level = 0;
1058a2750645SAndy Whitcroft	my @stack = ();
1059cf655043SAndy Whitcroft	my $p;
10608905a67cSAndy Whitcroft	my $c;
10618905a67cSAndy Whitcroft	my $len = 0;
106213214adfSAndy Whitcroft
106313214adfSAndy Whitcroft	my $remainder;
10648905a67cSAndy Whitcroft	while (1) {
1065a2750645SAndy Whitcroft		@stack = (['', 0]) if ($#stack == -1);
1066a2750645SAndy Whitcroft
1067773647a0SAndy Whitcroft		#warn "CSB: blk<$blk> remain<$remain>\n";
10688905a67cSAndy Whitcroft		# If we are about to drop off the end, pull in more
10698905a67cSAndy Whitcroft		# context.
10708905a67cSAndy Whitcroft		if ($off >= $len) {
10718905a67cSAndy Whitcroft			for (; $remain > 0; $line++) {
1072dea33496SAndy Whitcroft				last if (!defined $lines[$line]);
1073c2fdda0dSAndy Whitcroft				next if ($lines[$line] =~ /^-/);
10748905a67cSAndy Whitcroft				$remain--;
107513214adfSAndy Whitcroft				$loff = $len;
1076c2fdda0dSAndy Whitcroft				$blk .= $lines[$line] . "\n";
10778905a67cSAndy Whitcroft				$len = length($blk);
10788905a67cSAndy Whitcroft				$line++;
10798905a67cSAndy Whitcroft				last;
10808905a67cSAndy Whitcroft			}
10818905a67cSAndy Whitcroft			# Bail if there is no further context.
10828905a67cSAndy Whitcroft			#warn "CSB: blk<$blk> off<$off> len<$len>\n";
108313214adfSAndy Whitcroft			if ($off >= $len) {
10848905a67cSAndy Whitcroft				last;
10858905a67cSAndy Whitcroft			}
1086f74bd194SAndy Whitcroft			if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
1087f74bd194SAndy Whitcroft				$level++;
1088f74bd194SAndy Whitcroft				$type = '#';
1089f74bd194SAndy Whitcroft			}
10908905a67cSAndy Whitcroft		}
1091cf655043SAndy Whitcroft		$p = $c;
10928905a67cSAndy Whitcroft		$c = substr($blk, $off, 1);
109313214adfSAndy Whitcroft		$remainder = substr($blk, $off);
10948905a67cSAndy Whitcroft
1095773647a0SAndy Whitcroft		#warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
10964635f4fbSAndy Whitcroft
10974635f4fbSAndy Whitcroft		# Handle nested #if/#else.
10984635f4fbSAndy Whitcroft		if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
10994635f4fbSAndy Whitcroft			push(@stack, [ $type, $level ]);
11004635f4fbSAndy Whitcroft		} elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
11014635f4fbSAndy Whitcroft			($type, $level) = @{$stack[$#stack - 1]};
11024635f4fbSAndy Whitcroft		} elsif ($remainder =~ /^#\s*endif\b/) {
11034635f4fbSAndy Whitcroft			($type, $level) = @{pop(@stack)};
11044635f4fbSAndy Whitcroft		}
11054635f4fbSAndy Whitcroft
11068905a67cSAndy Whitcroft		# Statement ends at the ';' or a close '}' at the
11078905a67cSAndy Whitcroft		# outermost level.
11088905a67cSAndy Whitcroft		if ($level == 0 && $c eq ';') {
11098905a67cSAndy Whitcroft			last;
11108905a67cSAndy Whitcroft		}
11118905a67cSAndy Whitcroft
111213214adfSAndy Whitcroft		# An else is really a conditional as long as its not else if
1113773647a0SAndy Whitcroft		if ($level == 0 && $coff_set == 0 &&
1114773647a0SAndy Whitcroft				(!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
1115773647a0SAndy Whitcroft				$remainder =~ /^(else)(?:\s|{)/ &&
1116773647a0SAndy Whitcroft				$remainder !~ /^else\s+if\b/) {
1117773647a0SAndy Whitcroft			$coff = $off + length($1) - 1;
1118773647a0SAndy Whitcroft			$coff_set = 1;
1119773647a0SAndy Whitcroft			#warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
1120773647a0SAndy Whitcroft			#warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
112113214adfSAndy Whitcroft		}
112213214adfSAndy Whitcroft
11238905a67cSAndy Whitcroft		if (($type eq '' || $type eq '(') && $c eq '(') {
11248905a67cSAndy Whitcroft			$level++;
11258905a67cSAndy Whitcroft			$type = '(';
11268905a67cSAndy Whitcroft		}
11278905a67cSAndy Whitcroft		if ($type eq '(' && $c eq ')') {
11288905a67cSAndy Whitcroft			$level--;
11298905a67cSAndy Whitcroft			$type = ($level != 0)? '(' : '';
11308905a67cSAndy Whitcroft
11318905a67cSAndy Whitcroft			if ($level == 0 && $coff < $soff) {
11328905a67cSAndy Whitcroft				$coff = $off;
1133773647a0SAndy Whitcroft				$coff_set = 1;
1134773647a0SAndy Whitcroft				#warn "CSB: mark coff<$coff>\n";
11358905a67cSAndy Whitcroft			}
11368905a67cSAndy Whitcroft		}
11378905a67cSAndy Whitcroft		if (($type eq '' || $type eq '{') && $c eq '{') {
11388905a67cSAndy Whitcroft			$level++;
11398905a67cSAndy Whitcroft			$type = '{';
11408905a67cSAndy Whitcroft		}
11418905a67cSAndy Whitcroft		if ($type eq '{' && $c eq '}') {
11428905a67cSAndy Whitcroft			$level--;
11438905a67cSAndy Whitcroft			$type = ($level != 0)? '{' : '';
11448905a67cSAndy Whitcroft
11458905a67cSAndy Whitcroft			if ($level == 0) {
1146b998e001SPatrick Pannuto				if (substr($blk, $off + 1, 1) eq ';') {
1147b998e001SPatrick Pannuto					$off++;
1148b998e001SPatrick Pannuto				}
11498905a67cSAndy Whitcroft				last;
11508905a67cSAndy Whitcroft			}
11518905a67cSAndy Whitcroft		}
1152f74bd194SAndy Whitcroft		# Preprocessor commands end at the newline unless escaped.
1153f74bd194SAndy Whitcroft		if ($type eq '#' && $c eq "\n" && $p ne "\\") {
1154f74bd194SAndy Whitcroft			$level--;
1155f74bd194SAndy Whitcroft			$type = '';
1156f74bd194SAndy Whitcroft			$off++;
1157f74bd194SAndy Whitcroft			last;
1158f74bd194SAndy Whitcroft		}
11598905a67cSAndy Whitcroft		$off++;
11608905a67cSAndy Whitcroft	}
1161a3bb97a7SAndy Whitcroft	# We are truly at the end, so shuffle to the next line.
116213214adfSAndy Whitcroft	if ($off == $len) {
1163a3bb97a7SAndy Whitcroft		$loff = $len + 1;
116413214adfSAndy Whitcroft		$line++;
116513214adfSAndy Whitcroft		$remain--;
116613214adfSAndy Whitcroft	}
11678905a67cSAndy Whitcroft
11688905a67cSAndy Whitcroft	my $statement = substr($blk, $soff, $off - $soff + 1);
11698905a67cSAndy Whitcroft	my $condition = substr($blk, $soff, $coff - $soff + 1);
11708905a67cSAndy Whitcroft
11718905a67cSAndy Whitcroft	#warn "STATEMENT<$statement>\n";
11728905a67cSAndy Whitcroft	#warn "CONDITION<$condition>\n";
11738905a67cSAndy Whitcroft
1174773647a0SAndy Whitcroft	#print "coff<$coff> soff<$off> loff<$loff>\n";
117513214adfSAndy Whitcroft
117613214adfSAndy Whitcroft	return ($statement, $condition,
117713214adfSAndy Whitcroft			$line, $remain + 1, $off - $loff + 1, $level);
117813214adfSAndy Whitcroft}
117913214adfSAndy Whitcroft
1180cf655043SAndy Whitcroftsub statement_lines {
1181cf655043SAndy Whitcroft	my ($stmt) = @_;
1182cf655043SAndy Whitcroft
1183cf655043SAndy Whitcroft	# Strip the diff line prefixes and rip blank lines at start and end.
1184cf655043SAndy Whitcroft	$stmt =~ s/(^|\n)./$1/g;
1185cf655043SAndy Whitcroft	$stmt =~ s/^\s*//;
1186cf655043SAndy Whitcroft	$stmt =~ s/\s*$//;
1187cf655043SAndy Whitcroft
1188cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
1189cf655043SAndy Whitcroft
1190cf655043SAndy Whitcroft	return $#stmt_lines + 2;
1191cf655043SAndy Whitcroft}
1192cf655043SAndy Whitcroft
1193cf655043SAndy Whitcroftsub statement_rawlines {
1194cf655043SAndy Whitcroft	my ($stmt) = @_;
1195cf655043SAndy Whitcroft
1196cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
1197cf655043SAndy Whitcroft
1198cf655043SAndy Whitcroft	return $#stmt_lines + 2;
1199cf655043SAndy Whitcroft}
1200cf655043SAndy Whitcroft
1201cf655043SAndy Whitcroftsub statement_block_size {
1202cf655043SAndy Whitcroft	my ($stmt) = @_;
1203cf655043SAndy Whitcroft
1204cf655043SAndy Whitcroft	$stmt =~ s/(^|\n)./$1/g;
1205cf655043SAndy Whitcroft	$stmt =~ s/^\s*{//;
1206cf655043SAndy Whitcroft	$stmt =~ s/}\s*$//;
1207cf655043SAndy Whitcroft	$stmt =~ s/^\s*//;
1208cf655043SAndy Whitcroft	$stmt =~ s/\s*$//;
1209cf655043SAndy Whitcroft
1210cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
1211cf655043SAndy Whitcroft	my @stmt_statements = ($stmt =~ /;/g);
1212cf655043SAndy Whitcroft
1213cf655043SAndy Whitcroft	my $stmt_lines = $#stmt_lines + 2;
1214cf655043SAndy Whitcroft	my $stmt_statements = $#stmt_statements + 1;
1215cf655043SAndy Whitcroft
1216cf655043SAndy Whitcroft	if ($stmt_lines > $stmt_statements) {
1217cf655043SAndy Whitcroft		return $stmt_lines;
1218cf655043SAndy Whitcroft	} else {
1219cf655043SAndy Whitcroft		return $stmt_statements;
1220cf655043SAndy Whitcroft	}
1221cf655043SAndy Whitcroft}
1222cf655043SAndy Whitcroft
122313214adfSAndy Whitcroftsub ctx_statement_full {
122413214adfSAndy Whitcroft	my ($linenr, $remain, $off) = @_;
122513214adfSAndy Whitcroft	my ($statement, $condition, $level);
122613214adfSAndy Whitcroft
122713214adfSAndy Whitcroft	my (@chunks);
122813214adfSAndy Whitcroft
1229cf655043SAndy Whitcroft	# Grab the first conditional/block pair.
123013214adfSAndy Whitcroft	($statement, $condition, $linenr, $remain, $off, $level) =
123113214adfSAndy Whitcroft				ctx_statement_block($linenr, $remain, $off);
1232773647a0SAndy Whitcroft	#print "F: c<$condition> s<$statement> remain<$remain>\n";
123313214adfSAndy Whitcroft	push(@chunks, [ $condition, $statement ]);
1234cf655043SAndy Whitcroft	if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
1235cf655043SAndy Whitcroft		return ($level, $linenr, @chunks);
1236cf655043SAndy Whitcroft	}
1237cf655043SAndy Whitcroft
1238cf655043SAndy Whitcroft	# Pull in the following conditional/block pairs and see if they
1239cf655043SAndy Whitcroft	# could continue the statement.
1240cf655043SAndy Whitcroft	for (;;) {
124113214adfSAndy Whitcroft		($statement, $condition, $linenr, $remain, $off, $level) =
124213214adfSAndy Whitcroft				ctx_statement_block($linenr, $remain, $off);
1243cf655043SAndy Whitcroft		#print "C: c<$condition> s<$statement> remain<$remain>\n";
1244773647a0SAndy Whitcroft		last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
1245cf655043SAndy Whitcroft		#print "C: push\n";
1246cf655043SAndy Whitcroft		push(@chunks, [ $condition, $statement ]);
124713214adfSAndy Whitcroft	}
124813214adfSAndy Whitcroft
124913214adfSAndy Whitcroft	return ($level, $linenr, @chunks);
12508905a67cSAndy Whitcroft}
12518905a67cSAndy Whitcroft
12524a0df2efSAndy Whitcroftsub ctx_block_get {
1253f0a594c1SAndy Whitcroft	my ($linenr, $remain, $outer, $open, $close, $off) = @_;
12544a0df2efSAndy Whitcroft	my $line;
12554a0df2efSAndy Whitcroft	my $start = $linenr - 1;
12564a0df2efSAndy Whitcroft	my $blk = '';
12574a0df2efSAndy Whitcroft	my @o;
12584a0df2efSAndy Whitcroft	my @c;
12594a0df2efSAndy Whitcroft	my @res = ();
12604a0df2efSAndy Whitcroft
1261f0a594c1SAndy Whitcroft	my $level = 0;
12624635f4fbSAndy Whitcroft	my @stack = ($level);
126300df344fSAndy Whitcroft	for ($line = $start; $remain > 0; $line++) {
126400df344fSAndy Whitcroft		next if ($rawlines[$line] =~ /^-/);
126500df344fSAndy Whitcroft		$remain--;
126600df344fSAndy Whitcroft
126700df344fSAndy Whitcroft		$blk .= $rawlines[$line];
12684635f4fbSAndy Whitcroft
12694635f4fbSAndy Whitcroft		# Handle nested #if/#else.
127001464f30SAndy Whitcroft		if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
12714635f4fbSAndy Whitcroft			push(@stack, $level);
127201464f30SAndy Whitcroft		} elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
12734635f4fbSAndy Whitcroft			$level = $stack[$#stack - 1];
127401464f30SAndy Whitcroft		} elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
12754635f4fbSAndy Whitcroft			$level = pop(@stack);
12764635f4fbSAndy Whitcroft		}
12774635f4fbSAndy Whitcroft
127801464f30SAndy Whitcroft		foreach my $c (split(//, $lines[$line])) {
1279f0a594c1SAndy Whitcroft			##print "C<$c>L<$level><$open$close>O<$off>\n";
1280f0a594c1SAndy Whitcroft			if ($off > 0) {
1281f0a594c1SAndy Whitcroft				$off--;
1282f0a594c1SAndy Whitcroft				next;
1283f0a594c1SAndy Whitcroft			}
12844a0df2efSAndy Whitcroft
1285f0a594c1SAndy Whitcroft			if ($c eq $close && $level > 0) {
1286f0a594c1SAndy Whitcroft				$level--;
1287f0a594c1SAndy Whitcroft				last if ($level == 0);
1288f0a594c1SAndy Whitcroft			} elsif ($c eq $open) {
1289f0a594c1SAndy Whitcroft				$level++;
1290f0a594c1SAndy Whitcroft			}
1291f0a594c1SAndy Whitcroft		}
12924a0df2efSAndy Whitcroft
1293f0a594c1SAndy Whitcroft		if (!$outer || $level <= 1) {
129400df344fSAndy Whitcroft			push(@res, $rawlines[$line]);
12954a0df2efSAndy Whitcroft		}
12964a0df2efSAndy Whitcroft
1297f0a594c1SAndy Whitcroft		last if ($level == 0);
12984a0df2efSAndy Whitcroft	}
12994a0df2efSAndy Whitcroft
1300f0a594c1SAndy Whitcroft	return ($level, @res);
13014a0df2efSAndy Whitcroft}
13024a0df2efSAndy Whitcroftsub ctx_block_outer {
13034a0df2efSAndy Whitcroft	my ($linenr, $remain) = @_;
13044a0df2efSAndy Whitcroft
1305f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
1306f0a594c1SAndy Whitcroft	return @r;
13074a0df2efSAndy Whitcroft}
13084a0df2efSAndy Whitcroftsub ctx_block {
13094a0df2efSAndy Whitcroft	my ($linenr, $remain) = @_;
13104a0df2efSAndy Whitcroft
1311f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1312f0a594c1SAndy Whitcroft	return @r;
1313653d4876SAndy Whitcroft}
1314653d4876SAndy Whitcroftsub ctx_statement {
1315f0a594c1SAndy Whitcroft	my ($linenr, $remain, $off) = @_;
1316f0a594c1SAndy Whitcroft
1317f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1318f0a594c1SAndy Whitcroft	return @r;
1319f0a594c1SAndy Whitcroft}
1320f0a594c1SAndy Whitcroftsub ctx_block_level {
1321653d4876SAndy Whitcroft	my ($linenr, $remain) = @_;
1322653d4876SAndy Whitcroft
1323f0a594c1SAndy Whitcroft	return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
13244a0df2efSAndy Whitcroft}
13259c0ca6f9SAndy Whitcroftsub ctx_statement_level {
13269c0ca6f9SAndy Whitcroft	my ($linenr, $remain, $off) = @_;
13279c0ca6f9SAndy Whitcroft
13289c0ca6f9SAndy Whitcroft	return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
13299c0ca6f9SAndy Whitcroft}
13304a0df2efSAndy Whitcroft
13314a0df2efSAndy Whitcroftsub ctx_locate_comment {
13324a0df2efSAndy Whitcroft	my ($first_line, $end_line) = @_;
13334a0df2efSAndy Whitcroft
13344a0df2efSAndy Whitcroft	# Catch a comment on the end of the line itself.
1335beae6332SAndy Whitcroft	my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
13364a0df2efSAndy Whitcroft	return $current_comment if (defined $current_comment);
13374a0df2efSAndy Whitcroft
13384a0df2efSAndy Whitcroft	# Look through the context and try and figure out if there is a
13394a0df2efSAndy Whitcroft	# comment.
13404a0df2efSAndy Whitcroft	my $in_comment = 0;
13414a0df2efSAndy Whitcroft	$current_comment = '';
13424a0df2efSAndy Whitcroft	for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
134300df344fSAndy Whitcroft		my $line = $rawlines[$linenr - 1];
134400df344fSAndy Whitcroft		#warn "           $line\n";
13454a0df2efSAndy Whitcroft		if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
13464a0df2efSAndy Whitcroft			$in_comment = 1;
13474a0df2efSAndy Whitcroft		}
13484a0df2efSAndy Whitcroft		if ($line =~ m@/\*@) {
13494a0df2efSAndy Whitcroft			$in_comment = 1;
13504a0df2efSAndy Whitcroft		}
13514a0df2efSAndy Whitcroft		if (!$in_comment && $current_comment ne '') {
13524a0df2efSAndy Whitcroft			$current_comment = '';
13534a0df2efSAndy Whitcroft		}
13544a0df2efSAndy Whitcroft		$current_comment .= $line . "\n" if ($in_comment);
13554a0df2efSAndy Whitcroft		if ($line =~ m@\*/@) {
13564a0df2efSAndy Whitcroft			$in_comment = 0;
13574a0df2efSAndy Whitcroft		}
13584a0df2efSAndy Whitcroft	}
13594a0df2efSAndy Whitcroft
13604a0df2efSAndy Whitcroft	chomp($current_comment);
13614a0df2efSAndy Whitcroft	return($current_comment);
13624a0df2efSAndy Whitcroft}
13634a0df2efSAndy Whitcroftsub ctx_has_comment {
13644a0df2efSAndy Whitcroft	my ($first_line, $end_line) = @_;
13654a0df2efSAndy Whitcroft	my $cmt = ctx_locate_comment($first_line, $end_line);
13664a0df2efSAndy Whitcroft
136700df344fSAndy Whitcroft	##print "LINE: $rawlines[$end_line - 1 ]\n";
13684a0df2efSAndy Whitcroft	##print "CMMT: $cmt\n";
13694a0df2efSAndy Whitcroft
13704a0df2efSAndy Whitcroft	return ($cmt ne '');
13714a0df2efSAndy Whitcroft}
13724a0df2efSAndy Whitcroft
13734d001e4dSAndy Whitcroftsub raw_line {
13744d001e4dSAndy Whitcroft	my ($linenr, $cnt) = @_;
13754d001e4dSAndy Whitcroft
13764d001e4dSAndy Whitcroft	my $offset = $linenr - 1;
13774d001e4dSAndy Whitcroft	$cnt++;
13784d001e4dSAndy Whitcroft
13794d001e4dSAndy Whitcroft	my $line;
13804d001e4dSAndy Whitcroft	while ($cnt) {
13814d001e4dSAndy Whitcroft		$line = $rawlines[$offset++];
13824d001e4dSAndy Whitcroft		next if (defined($line) && $line =~ /^-/);
13834d001e4dSAndy Whitcroft		$cnt--;
13844d001e4dSAndy Whitcroft	}
13854d001e4dSAndy Whitcroft
13864d001e4dSAndy Whitcroft	return $line;
13874d001e4dSAndy Whitcroft}
13884d001e4dSAndy Whitcroft
13890a920b5bSAndy Whitcroftsub cat_vet {
13900a920b5bSAndy Whitcroft	my ($vet) = @_;
13919c0ca6f9SAndy Whitcroft	my ($res, $coded);
13920a920b5bSAndy Whitcroft
13939c0ca6f9SAndy Whitcroft	$res = '';
13946c72ffaaSAndy Whitcroft	while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
13956c72ffaaSAndy Whitcroft		$res .= $1;
13966c72ffaaSAndy Whitcroft		if ($2 ne '') {
13979c0ca6f9SAndy Whitcroft			$coded = sprintf("^%c", unpack('C', $2) + 64);
13986c72ffaaSAndy Whitcroft			$res .= $coded;
13996c72ffaaSAndy Whitcroft		}
14009c0ca6f9SAndy Whitcroft	}
14019c0ca6f9SAndy Whitcroft	$res =~ s/$/\$/;
14020a920b5bSAndy Whitcroft
14039c0ca6f9SAndy Whitcroft	return $res;
14040a920b5bSAndy Whitcroft}
14050a920b5bSAndy Whitcroft
1406c2fdda0dSAndy Whitcroftmy $av_preprocessor = 0;
1407cf655043SAndy Whitcroftmy $av_pending;
1408c2fdda0dSAndy Whitcroftmy @av_paren_type;
14091f65f947SAndy Whitcroftmy $av_pend_colon;
1410c2fdda0dSAndy Whitcroft
1411c2fdda0dSAndy Whitcroftsub annotate_reset {
1412c2fdda0dSAndy Whitcroft	$av_preprocessor = 0;
1413cf655043SAndy Whitcroft	$av_pending = '_';
1414cf655043SAndy Whitcroft	@av_paren_type = ('E');
14151f65f947SAndy Whitcroft	$av_pend_colon = 'O';
1416c2fdda0dSAndy Whitcroft}
1417c2fdda0dSAndy Whitcroft
14186c72ffaaSAndy Whitcroftsub annotate_values {
14196c72ffaaSAndy Whitcroft	my ($stream, $type) = @_;
14206c72ffaaSAndy Whitcroft
14216c72ffaaSAndy Whitcroft	my $res;
14221f65f947SAndy Whitcroft	my $var = '_' x length($stream);
14236c72ffaaSAndy Whitcroft	my $cur = $stream;
14246c72ffaaSAndy Whitcroft
1425c2fdda0dSAndy Whitcroft	print "$stream\n" if ($dbg_values > 1);
14266c72ffaaSAndy Whitcroft
14276c72ffaaSAndy Whitcroft	while (length($cur)) {
1428773647a0SAndy Whitcroft		@av_paren_type = ('E') if ($#av_paren_type < 0);
1429cf655043SAndy Whitcroft		print " <" . join('', @av_paren_type) .
1430171ae1a4SAndy Whitcroft				"> <$type> <$av_pending>" if ($dbg_values > 1);
14316c72ffaaSAndy Whitcroft		if ($cur =~ /^(\s+)/o) {
1432c2fdda0dSAndy Whitcroft			print "WS($1)\n" if ($dbg_values > 1);
1433c2fdda0dSAndy Whitcroft			if ($1 =~ /\n/ && $av_preprocessor) {
1434cf655043SAndy Whitcroft				$type = pop(@av_paren_type);
1435c2fdda0dSAndy Whitcroft				$av_preprocessor = 0;
14366c72ffaaSAndy Whitcroft			}
14376c72ffaaSAndy Whitcroft
1438c023e473SFlorian Mickler		} elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
14399446ef56SAndy Whitcroft			print "CAST($1)\n" if ($dbg_values > 1);
14409446ef56SAndy Whitcroft			push(@av_paren_type, $type);
1441addcdceaSAndy Whitcroft			$type = 'c';
14429446ef56SAndy Whitcroft
1443e91b6e26SAndy Whitcroft		} elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
1444c2fdda0dSAndy Whitcroft			print "DECLARE($1)\n" if ($dbg_values > 1);
14456c72ffaaSAndy Whitcroft			$type = 'T';
14466c72ffaaSAndy Whitcroft
1447389a2fe5SAndy Whitcroft		} elsif ($cur =~ /^($Modifier)\s*/) {
1448389a2fe5SAndy Whitcroft			print "MODIFIER($1)\n" if ($dbg_values > 1);
1449389a2fe5SAndy Whitcroft			$type = 'T';
1450389a2fe5SAndy Whitcroft
1451c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
1452171ae1a4SAndy Whitcroft			print "DEFINE($1,$2)\n" if ($dbg_values > 1);
1453c2fdda0dSAndy Whitcroft			$av_preprocessor = 1;
1454171ae1a4SAndy Whitcroft			push(@av_paren_type, $type);
1455171ae1a4SAndy Whitcroft			if ($2 ne '') {
1456cf655043SAndy Whitcroft				$av_pending = 'N';
1457171ae1a4SAndy Whitcroft			}
1458171ae1a4SAndy Whitcroft			$type = 'E';
1459171ae1a4SAndy Whitcroft
1460c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
1461171ae1a4SAndy Whitcroft			print "UNDEF($1)\n" if ($dbg_values > 1);
1462171ae1a4SAndy Whitcroft			$av_preprocessor = 1;
1463171ae1a4SAndy Whitcroft			push(@av_paren_type, $type);
14646c72ffaaSAndy Whitcroft
1465c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
1466cf655043SAndy Whitcroft			print "PRE_START($1)\n" if ($dbg_values > 1);
1467c2fdda0dSAndy Whitcroft			$av_preprocessor = 1;
1468cf655043SAndy Whitcroft
1469cf655043SAndy Whitcroft			push(@av_paren_type, $type);
1470cf655043SAndy Whitcroft			push(@av_paren_type, $type);
1471171ae1a4SAndy Whitcroft			$type = 'E';
1472cf655043SAndy Whitcroft
1473c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
1474cf655043SAndy Whitcroft			print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1475cf655043SAndy Whitcroft			$av_preprocessor = 1;
1476cf655043SAndy Whitcroft
1477cf655043SAndy Whitcroft			push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1478cf655043SAndy Whitcroft
1479171ae1a4SAndy Whitcroft			$type = 'E';
1480cf655043SAndy Whitcroft
1481c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:endif))/o) {
1482cf655043SAndy Whitcroft			print "PRE_END($1)\n" if ($dbg_values > 1);
1483cf655043SAndy Whitcroft
1484cf655043SAndy Whitcroft			$av_preprocessor = 1;
1485cf655043SAndy Whitcroft
1486cf655043SAndy Whitcroft			# Assume all arms of the conditional end as this
1487cf655043SAndy Whitcroft			# one does, and continue as if the #endif was not here.
1488cf655043SAndy Whitcroft			pop(@av_paren_type);
1489cf655043SAndy Whitcroft			push(@av_paren_type, $type);
1490171ae1a4SAndy Whitcroft			$type = 'E';
14916c72ffaaSAndy Whitcroft
14926c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\\\n)/o) {
1493c2fdda0dSAndy Whitcroft			print "PRECONT($1)\n" if ($dbg_values > 1);
14946c72ffaaSAndy Whitcroft
1495171ae1a4SAndy Whitcroft		} elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1496171ae1a4SAndy Whitcroft			print "ATTR($1)\n" if ($dbg_values > 1);
1497171ae1a4SAndy Whitcroft			$av_pending = $type;
1498171ae1a4SAndy Whitcroft			$type = 'N';
1499171ae1a4SAndy Whitcroft
15006c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
1501c2fdda0dSAndy Whitcroft			print "SIZEOF($1)\n" if ($dbg_values > 1);
15026c72ffaaSAndy Whitcroft			if (defined $2) {
1503cf655043SAndy Whitcroft				$av_pending = 'V';
15046c72ffaaSAndy Whitcroft			}
15056c72ffaaSAndy Whitcroft			$type = 'N';
15066c72ffaaSAndy Whitcroft
150714b111c1SAndy Whitcroft		} elsif ($cur =~ /^(if|while|for)\b/o) {
1508c2fdda0dSAndy Whitcroft			print "COND($1)\n" if ($dbg_values > 1);
150914b111c1SAndy Whitcroft			$av_pending = 'E';
15106c72ffaaSAndy Whitcroft			$type = 'N';
15116c72ffaaSAndy Whitcroft
15121f65f947SAndy Whitcroft		} elsif ($cur =~/^(case)/o) {
15131f65f947SAndy Whitcroft			print "CASE($1)\n" if ($dbg_values > 1);
15141f65f947SAndy Whitcroft			$av_pend_colon = 'C';
15151f65f947SAndy Whitcroft			$type = 'N';
15161f65f947SAndy Whitcroft
151714b111c1SAndy Whitcroft		} elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
1518c2fdda0dSAndy Whitcroft			print "KEYWORD($1)\n" if ($dbg_values > 1);
15196c72ffaaSAndy Whitcroft			$type = 'N';
15206c72ffaaSAndy Whitcroft
15216c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\()/o) {
1522c2fdda0dSAndy Whitcroft			print "PAREN('$1')\n" if ($dbg_values > 1);
1523cf655043SAndy Whitcroft			push(@av_paren_type, $av_pending);
1524cf655043SAndy Whitcroft			$av_pending = '_';
15256c72ffaaSAndy Whitcroft			$type = 'N';
15266c72ffaaSAndy Whitcroft
15276c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\))/o) {
1528cf655043SAndy Whitcroft			my $new_type = pop(@av_paren_type);
1529cf655043SAndy Whitcroft			if ($new_type ne '_') {
1530cf655043SAndy Whitcroft				$type = $new_type;
1531c2fdda0dSAndy Whitcroft				print "PAREN('$1') -> $type\n"
1532c2fdda0dSAndy Whitcroft							if ($dbg_values > 1);
15336c72ffaaSAndy Whitcroft			} else {
1534c2fdda0dSAndy Whitcroft				print "PAREN('$1')\n" if ($dbg_values > 1);
15356c72ffaaSAndy Whitcroft			}
15366c72ffaaSAndy Whitcroft
1537c8cb2ca3SAndy Whitcroft		} elsif ($cur =~ /^($Ident)\s*\(/o) {
1538c2fdda0dSAndy Whitcroft			print "FUNC($1)\n" if ($dbg_values > 1);
1539c8cb2ca3SAndy Whitcroft			$type = 'V';
1540cf655043SAndy Whitcroft			$av_pending = 'V';
15416c72ffaaSAndy Whitcroft
15428e761b04SAndy Whitcroft		} elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
15438e761b04SAndy Whitcroft			if (defined $2 && $type eq 'C' || $type eq 'T') {
15441f65f947SAndy Whitcroft				$av_pend_colon = 'B';
15458e761b04SAndy Whitcroft			} elsif ($type eq 'E') {
15468e761b04SAndy Whitcroft				$av_pend_colon = 'L';
15471f65f947SAndy Whitcroft			}
15481f65f947SAndy Whitcroft			print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
15491f65f947SAndy Whitcroft			$type = 'V';
15501f65f947SAndy Whitcroft
15516c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Ident|$Constant)/o) {
1552c2fdda0dSAndy Whitcroft			print "IDENT($1)\n" if ($dbg_values > 1);
15536c72ffaaSAndy Whitcroft			$type = 'V';
15546c72ffaaSAndy Whitcroft
15556c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Assignment)/o) {
1556c2fdda0dSAndy Whitcroft			print "ASSIGN($1)\n" if ($dbg_values > 1);
15576c72ffaaSAndy Whitcroft			$type = 'N';
15586c72ffaaSAndy Whitcroft
1559cf655043SAndy Whitcroft		} elsif ($cur =~/^(;|{|})/) {
1560c2fdda0dSAndy Whitcroft			print "END($1)\n" if ($dbg_values > 1);
156113214adfSAndy Whitcroft			$type = 'E';
15621f65f947SAndy Whitcroft			$av_pend_colon = 'O';
156313214adfSAndy Whitcroft
15648e761b04SAndy Whitcroft		} elsif ($cur =~/^(,)/) {
15658e761b04SAndy Whitcroft			print "COMMA($1)\n" if ($dbg_values > 1);
15668e761b04SAndy Whitcroft			$type = 'C';
15678e761b04SAndy Whitcroft
15681f65f947SAndy Whitcroft		} elsif ($cur =~ /^(\?)/o) {
15691f65f947SAndy Whitcroft			print "QUESTION($1)\n" if ($dbg_values > 1);
15701f65f947SAndy Whitcroft			$type = 'N';
15711f65f947SAndy Whitcroft
15721f65f947SAndy Whitcroft		} elsif ($cur =~ /^(:)/o) {
15731f65f947SAndy Whitcroft			print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
15741f65f947SAndy Whitcroft
15751f65f947SAndy Whitcroft			substr($var, length($res), 1, $av_pend_colon);
15761f65f947SAndy Whitcroft			if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
15771f65f947SAndy Whitcroft				$type = 'E';
15781f65f947SAndy Whitcroft			} else {
15791f65f947SAndy Whitcroft				$type = 'N';
15801f65f947SAndy Whitcroft			}
15811f65f947SAndy Whitcroft			$av_pend_colon = 'O';
15821f65f947SAndy Whitcroft
15838e761b04SAndy Whitcroft		} elsif ($cur =~ /^(\[)/o) {
158413214adfSAndy Whitcroft			print "CLOSE($1)\n" if ($dbg_values > 1);
15856c72ffaaSAndy Whitcroft			$type = 'N';
15866c72ffaaSAndy Whitcroft
15870d413866SAndy Whitcroft		} elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
158874048ed8SAndy Whitcroft			my $variant;
158974048ed8SAndy Whitcroft
159074048ed8SAndy Whitcroft			print "OPV($1)\n" if ($dbg_values > 1);
159174048ed8SAndy Whitcroft			if ($type eq 'V') {
159274048ed8SAndy Whitcroft				$variant = 'B';
159374048ed8SAndy Whitcroft			} else {
159474048ed8SAndy Whitcroft				$variant = 'U';
159574048ed8SAndy Whitcroft			}
159674048ed8SAndy Whitcroft
159774048ed8SAndy Whitcroft			substr($var, length($res), 1, $variant);
159874048ed8SAndy Whitcroft			$type = 'N';
159974048ed8SAndy Whitcroft
16006c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Operators)/o) {
1601c2fdda0dSAndy Whitcroft			print "OP($1)\n" if ($dbg_values > 1);
16026c72ffaaSAndy Whitcroft			if ($1 ne '++' && $1 ne '--') {
16036c72ffaaSAndy Whitcroft				$type = 'N';
16046c72ffaaSAndy Whitcroft			}
16056c72ffaaSAndy Whitcroft
16066c72ffaaSAndy Whitcroft		} elsif ($cur =~ /(^.)/o) {
1607c2fdda0dSAndy Whitcroft			print "C($1)\n" if ($dbg_values > 1);
16086c72ffaaSAndy Whitcroft		}
16096c72ffaaSAndy Whitcroft		if (defined $1) {
16106c72ffaaSAndy Whitcroft			$cur = substr($cur, length($1));
16116c72ffaaSAndy Whitcroft			$res .= $type x length($1);
16126c72ffaaSAndy Whitcroft		}
16136c72ffaaSAndy Whitcroft	}
16146c72ffaaSAndy Whitcroft
16151f65f947SAndy Whitcroft	return ($res, $var);
16166c72ffaaSAndy Whitcroft}
16176c72ffaaSAndy Whitcroft
16188905a67cSAndy Whitcroftsub possible {
161913214adfSAndy Whitcroft	my ($possible, $line) = @_;
16209a974fdbSAndy Whitcroft	my $notPermitted = qr{(?:
16210776e594SAndy Whitcroft		^(?:
16220776e594SAndy Whitcroft			$Modifier|
16230776e594SAndy Whitcroft			$Storage|
16240776e594SAndy Whitcroft			$Type|
16259a974fdbSAndy Whitcroft			DEFINE_\S+
16269a974fdbSAndy Whitcroft		)$|
16279a974fdbSAndy Whitcroft		^(?:
16280776e594SAndy Whitcroft			goto|
16290776e594SAndy Whitcroft			return|
16300776e594SAndy Whitcroft			case|
16310776e594SAndy Whitcroft			else|
16320776e594SAndy Whitcroft			asm|__asm__|
163389a88353SAndy Whitcroft			do|
163489a88353SAndy Whitcroft			\#|
163589a88353SAndy Whitcroft			\#\#|
16369a974fdbSAndy Whitcroft		)(?:\s|$)|
16370776e594SAndy Whitcroft		^(?:typedef|struct|enum)\b
16389a974fdbSAndy Whitcroft	    )}x;
16399a974fdbSAndy Whitcroft	warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
16409a974fdbSAndy Whitcroft	if ($possible !~ $notPermitted) {
1641c45dcabdSAndy Whitcroft		# Check for modifiers.
1642c45dcabdSAndy Whitcroft		$possible =~ s/\s*$Storage\s*//g;
1643c45dcabdSAndy Whitcroft		$possible =~ s/\s*$Sparse\s*//g;
1644c45dcabdSAndy Whitcroft		if ($possible =~ /^\s*$/) {
1645c45dcabdSAndy Whitcroft
1646c45dcabdSAndy Whitcroft		} elsif ($possible =~ /\s/) {
1647c45dcabdSAndy Whitcroft			$possible =~ s/\s*$Type\s*//g;
1648d2506586SAndy Whitcroft			for my $modifier (split(' ', $possible)) {
16499a974fdbSAndy Whitcroft				if ($modifier !~ $notPermitted) {
1650d2506586SAndy Whitcroft					warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1651485ff23eSAlex Dowad					push(@modifierListFile, $modifier);
1652d2506586SAndy Whitcroft				}
16539a974fdbSAndy Whitcroft			}
1654c45dcabdSAndy Whitcroft
1655c45dcabdSAndy Whitcroft		} else {
165613214adfSAndy Whitcroft			warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
1657485ff23eSAlex Dowad			push(@typeListFile, $possible);
1658c45dcabdSAndy Whitcroft		}
16598905a67cSAndy Whitcroft		build_types();
16600776e594SAndy Whitcroft	} else {
16610776e594SAndy Whitcroft		warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
16628905a67cSAndy Whitcroft	}
16638905a67cSAndy Whitcroft}
16648905a67cSAndy Whitcroft
16656c72ffaaSAndy Whitcroftmy $prefix = '';
16666c72ffaaSAndy Whitcroft
1667000d1cc1SJoe Perchessub show_type {
1668cbec18afSJoe Perches	my ($type) = @_;
166991bfe484SJoe Perches
1670cbec18afSJoe Perches	return defined $use_type{$type} if (scalar keys %use_type > 0);
1671cbec18afSJoe Perches
1672cbec18afSJoe Perches	return !defined $ignore_type{$type};
1673000d1cc1SJoe Perches}
1674000d1cc1SJoe Perches
1675f0a594c1SAndy Whitcroftsub report {
1676cbec18afSJoe Perches	my ($level, $type, $msg) = @_;
1677cbec18afSJoe Perches
1678cbec18afSJoe Perches	if (!show_type($type) ||
1679cbec18afSJoe Perches	    (defined $tst_only && $msg !~ /\Q$tst_only\E/)) {
1680773647a0SAndy Whitcroft		return 0;
1681773647a0SAndy Whitcroft	}
168257230297SJoe Perches	my $output = '';
168357230297SJoe Perches	if (-t STDOUT && $color) {
168457230297SJoe Perches		if ($level eq 'ERROR') {
168557230297SJoe Perches			$output .= RED;
168657230297SJoe Perches		} elsif ($level eq 'WARNING') {
168757230297SJoe Perches			$output .= YELLOW;
1688000d1cc1SJoe Perches		} else {
168957230297SJoe Perches			$output .= GREEN;
1690000d1cc1SJoe Perches		}
169157230297SJoe Perches	}
169257230297SJoe Perches	$output .= $prefix . $level . ':';
169357230297SJoe Perches	if ($show_types) {
169457230297SJoe Perches		$output .= BLUE if (-t STDOUT && $color);
169557230297SJoe Perches		$output .= "$type:";
169657230297SJoe Perches	}
169757230297SJoe Perches	$output .= RESET if (-t STDOUT && $color);
169857230297SJoe Perches	$output .= ' ' . $msg . "\n";
169934d8815fSJoe Perches
170034d8815fSJoe Perches	if ($showfile) {
170134d8815fSJoe Perches		my @lines = split("\n", $output, -1);
170234d8815fSJoe Perches		splice(@lines, 1, 1);
170334d8815fSJoe Perches		$output = join("\n", @lines);
170434d8815fSJoe Perches	}
170557230297SJoe Perches	$output = (split('\n', $output))[0] . "\n" if ($terse);
17068905a67cSAndy Whitcroft
170757230297SJoe Perches	push(our @report, $output);
1708773647a0SAndy Whitcroft
1709773647a0SAndy Whitcroft	return 1;
1710f0a594c1SAndy Whitcroft}
1711cbec18afSJoe Perches
1712f0a594c1SAndy Whitcroftsub report_dump {
171313214adfSAndy Whitcroft	our @report;
1714f0a594c1SAndy Whitcroft}
1715000d1cc1SJoe Perches
1716d752fcc8SJoe Perchessub fixup_current_range {
1717d752fcc8SJoe Perches	my ($lineRef, $offset, $length) = @_;
1718d752fcc8SJoe Perches
1719d752fcc8SJoe Perches	if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) {
1720d752fcc8SJoe Perches		my $o = $1;
1721d752fcc8SJoe Perches		my $l = $2;
1722d752fcc8SJoe Perches		my $no = $o + $offset;
1723d752fcc8SJoe Perches		my $nl = $l + $length;
1724d752fcc8SJoe Perches		$$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/;
1725d752fcc8SJoe Perches	}
1726d752fcc8SJoe Perches}
1727d752fcc8SJoe Perches
1728d752fcc8SJoe Perchessub fix_inserted_deleted_lines {
1729d752fcc8SJoe Perches	my ($linesRef, $insertedRef, $deletedRef) = @_;
1730d752fcc8SJoe Perches
1731d752fcc8SJoe Perches	my $range_last_linenr = 0;
1732d752fcc8SJoe Perches	my $delta_offset = 0;
1733d752fcc8SJoe Perches
1734d752fcc8SJoe Perches	my $old_linenr = 0;
1735d752fcc8SJoe Perches	my $new_linenr = 0;
1736d752fcc8SJoe Perches
1737d752fcc8SJoe Perches	my $next_insert = 0;
1738d752fcc8SJoe Perches	my $next_delete = 0;
1739d752fcc8SJoe Perches
1740d752fcc8SJoe Perches	my @lines = ();
1741d752fcc8SJoe Perches
1742d752fcc8SJoe Perches	my $inserted = @{$insertedRef}[$next_insert++];
1743d752fcc8SJoe Perches	my $deleted = @{$deletedRef}[$next_delete++];
1744d752fcc8SJoe Perches
1745d752fcc8SJoe Perches	foreach my $old_line (@{$linesRef}) {
1746d752fcc8SJoe Perches		my $save_line = 1;
1747d752fcc8SJoe Perches		my $line = $old_line;	#don't modify the array
1748323b267fSJoe Perches		if ($line =~ /^(?:\+\+\+|\-\-\-)\s+\S+/) {	#new filename
1749d752fcc8SJoe Perches			$delta_offset = 0;
1750d752fcc8SJoe Perches		} elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) {	#new hunk
1751d752fcc8SJoe Perches			$range_last_linenr = $new_linenr;
1752d752fcc8SJoe Perches			fixup_current_range(\$line, $delta_offset, 0);
1753d752fcc8SJoe Perches		}
1754d752fcc8SJoe Perches
1755d752fcc8SJoe Perches		while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) {
1756d752fcc8SJoe Perches			$deleted = @{$deletedRef}[$next_delete++];
1757d752fcc8SJoe Perches			$save_line = 0;
1758d752fcc8SJoe Perches			fixup_current_range(\$lines[$range_last_linenr], $delta_offset--, -1);
1759d752fcc8SJoe Perches		}
1760d752fcc8SJoe Perches
1761d752fcc8SJoe Perches		while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) {
1762d752fcc8SJoe Perches			push(@lines, ${$inserted}{'LINE'});
1763d752fcc8SJoe Perches			$inserted = @{$insertedRef}[$next_insert++];
1764d752fcc8SJoe Perches			$new_linenr++;
1765d752fcc8SJoe Perches			fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1);
1766d752fcc8SJoe Perches		}
1767d752fcc8SJoe Perches
1768d752fcc8SJoe Perches		if ($save_line) {
1769d752fcc8SJoe Perches			push(@lines, $line);
1770d752fcc8SJoe Perches			$new_linenr++;
1771d752fcc8SJoe Perches		}
1772d752fcc8SJoe Perches
1773d752fcc8SJoe Perches		$old_linenr++;
1774d752fcc8SJoe Perches	}
1775d752fcc8SJoe Perches
1776d752fcc8SJoe Perches	return @lines;
1777d752fcc8SJoe Perches}
1778d752fcc8SJoe Perches
1779f2d7e4d4SJoe Perchessub fix_insert_line {
1780f2d7e4d4SJoe Perches	my ($linenr, $line) = @_;
1781f2d7e4d4SJoe Perches
1782f2d7e4d4SJoe Perches	my $inserted = {
1783f2d7e4d4SJoe Perches		LINENR => $linenr,
1784f2d7e4d4SJoe Perches		LINE => $line,
1785f2d7e4d4SJoe Perches	};
1786f2d7e4d4SJoe Perches	push(@fixed_inserted, $inserted);
1787f2d7e4d4SJoe Perches}
1788f2d7e4d4SJoe Perches
1789f2d7e4d4SJoe Perchessub fix_delete_line {
1790f2d7e4d4SJoe Perches	my ($linenr, $line) = @_;
1791f2d7e4d4SJoe Perches
1792f2d7e4d4SJoe Perches	my $deleted = {
1793f2d7e4d4SJoe Perches		LINENR => $linenr,
1794f2d7e4d4SJoe Perches		LINE => $line,
1795f2d7e4d4SJoe Perches	};
1796f2d7e4d4SJoe Perches
1797f2d7e4d4SJoe Perches	push(@fixed_deleted, $deleted);
1798f2d7e4d4SJoe Perches}
1799f2d7e4d4SJoe Perches
1800de7d4f0eSAndy Whitcroftsub ERROR {
1801cbec18afSJoe Perches	my ($type, $msg) = @_;
1802cbec18afSJoe Perches
1803cbec18afSJoe Perches	if (report("ERROR", $type, $msg)) {
1804de7d4f0eSAndy Whitcroft		our $clean = 0;
18056c72ffaaSAndy Whitcroft		our $cnt_error++;
18063705ce5bSJoe Perches		return 1;
1807de7d4f0eSAndy Whitcroft	}
18083705ce5bSJoe Perches	return 0;
1809773647a0SAndy Whitcroft}
1810de7d4f0eSAndy Whitcroftsub WARN {
1811cbec18afSJoe Perches	my ($type, $msg) = @_;
1812cbec18afSJoe Perches
1813cbec18afSJoe Perches	if (report("WARNING", $type, $msg)) {
1814de7d4f0eSAndy Whitcroft		our $clean = 0;
18156c72ffaaSAndy Whitcroft		our $cnt_warn++;
18163705ce5bSJoe Perches		return 1;
1817de7d4f0eSAndy Whitcroft	}
18183705ce5bSJoe Perches	return 0;
1819773647a0SAndy Whitcroft}
1820de7d4f0eSAndy Whitcroftsub CHK {
1821cbec18afSJoe Perches	my ($type, $msg) = @_;
1822cbec18afSJoe Perches
1823cbec18afSJoe Perches	if ($check && report("CHECK", $type, $msg)) {
1824de7d4f0eSAndy Whitcroft		our $clean = 0;
18256c72ffaaSAndy Whitcroft		our $cnt_chk++;
18263705ce5bSJoe Perches		return 1;
18276c72ffaaSAndy Whitcroft	}
18283705ce5bSJoe Perches	return 0;
1829de7d4f0eSAndy Whitcroft}
1830de7d4f0eSAndy Whitcroft
18316ecd9674SAndy Whitcroftsub check_absolute_file {
18326ecd9674SAndy Whitcroft	my ($absolute, $herecurr) = @_;
18336ecd9674SAndy Whitcroft	my $file = $absolute;
18346ecd9674SAndy Whitcroft
18356ecd9674SAndy Whitcroft	##print "absolute<$absolute>\n";
18366ecd9674SAndy Whitcroft
18376ecd9674SAndy Whitcroft	# See if any suffix of this path is a path within the tree.
18386ecd9674SAndy Whitcroft	while ($file =~ s@^[^/]*/@@) {
18396ecd9674SAndy Whitcroft		if (-f "$root/$file") {
18406ecd9674SAndy Whitcroft			##print "file<$file>\n";
18416ecd9674SAndy Whitcroft			last;
18426ecd9674SAndy Whitcroft		}
18436ecd9674SAndy Whitcroft	}
18446ecd9674SAndy Whitcroft	if (! -f _)  {
18456ecd9674SAndy Whitcroft		return 0;
18466ecd9674SAndy Whitcroft	}
18476ecd9674SAndy Whitcroft
18486ecd9674SAndy Whitcroft	# It is, so see if the prefix is acceptable.
18496ecd9674SAndy Whitcroft	my $prefix = $absolute;
18506ecd9674SAndy Whitcroft	substr($prefix, -length($file)) = '';
18516ecd9674SAndy Whitcroft
18526ecd9674SAndy Whitcroft	##print "prefix<$prefix>\n";
18536ecd9674SAndy Whitcroft	if ($prefix ne ".../") {
1854000d1cc1SJoe Perches		WARN("USE_RELATIVE_PATH",
1855000d1cc1SJoe Perches		     "use relative pathname instead of absolute in changelog text\n" . $herecurr);
18566ecd9674SAndy Whitcroft	}
18576ecd9674SAndy Whitcroft}
18586ecd9674SAndy Whitcroft
18593705ce5bSJoe Perchessub trim {
18603705ce5bSJoe Perches	my ($string) = @_;
18613705ce5bSJoe Perches
1862b34c648bSJoe Perches	$string =~ s/^\s+|\s+$//g;
1863b34c648bSJoe Perches
1864b34c648bSJoe Perches	return $string;
1865b34c648bSJoe Perches}
1866b34c648bSJoe Perches
1867b34c648bSJoe Perchessub ltrim {
1868b34c648bSJoe Perches	my ($string) = @_;
1869b34c648bSJoe Perches
1870b34c648bSJoe Perches	$string =~ s/^\s+//;
1871b34c648bSJoe Perches
1872b34c648bSJoe Perches	return $string;
1873b34c648bSJoe Perches}
1874b34c648bSJoe Perches
1875b34c648bSJoe Perchessub rtrim {
1876b34c648bSJoe Perches	my ($string) = @_;
1877b34c648bSJoe Perches
1878b34c648bSJoe Perches	$string =~ s/\s+$//;
18793705ce5bSJoe Perches
18803705ce5bSJoe Perches	return $string;
18813705ce5bSJoe Perches}
18823705ce5bSJoe Perches
188352ea8506SJoe Perchessub string_find_replace {
188452ea8506SJoe Perches	my ($string, $find, $replace) = @_;
188552ea8506SJoe Perches
188652ea8506SJoe Perches	$string =~ s/$find/$replace/g;
188752ea8506SJoe Perches
188852ea8506SJoe Perches	return $string;
188952ea8506SJoe Perches}
189052ea8506SJoe Perches
18913705ce5bSJoe Perchessub tabify {
18923705ce5bSJoe Perches	my ($leading) = @_;
18933705ce5bSJoe Perches
18943705ce5bSJoe Perches	my $source_indent = 8;
18953705ce5bSJoe Perches	my $max_spaces_before_tab = $source_indent - 1;
18963705ce5bSJoe Perches	my $spaces_to_tab = " " x $source_indent;
18973705ce5bSJoe Perches
18983705ce5bSJoe Perches	#convert leading spaces to tabs
18993705ce5bSJoe Perches	1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
19003705ce5bSJoe Perches	#Remove spaces before a tab
19013705ce5bSJoe Perches	1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
19023705ce5bSJoe Perches
19033705ce5bSJoe Perches	return "$leading";
19043705ce5bSJoe Perches}
19053705ce5bSJoe Perches
1906d1fe9c09SJoe Perchessub pos_last_openparen {
1907d1fe9c09SJoe Perches	my ($line) = @_;
1908d1fe9c09SJoe Perches
1909d1fe9c09SJoe Perches	my $pos = 0;
1910d1fe9c09SJoe Perches
1911d1fe9c09SJoe Perches	my $opens = $line =~ tr/\(/\(/;
1912d1fe9c09SJoe Perches	my $closes = $line =~ tr/\)/\)/;
1913d1fe9c09SJoe Perches
1914d1fe9c09SJoe Perches	my $last_openparen = 0;
1915d1fe9c09SJoe Perches
1916d1fe9c09SJoe Perches	if (($opens == 0) || ($closes >= $opens)) {
1917d1fe9c09SJoe Perches		return -1;
1918d1fe9c09SJoe Perches	}
1919d1fe9c09SJoe Perches
1920d1fe9c09SJoe Perches	my $len = length($line);
1921d1fe9c09SJoe Perches
1922d1fe9c09SJoe Perches	for ($pos = 0; $pos < $len; $pos++) {
1923d1fe9c09SJoe Perches		my $string = substr($line, $pos);
1924d1fe9c09SJoe Perches		if ($string =~ /^($FuncArg|$balanced_parens)/) {
1925d1fe9c09SJoe Perches			$pos += length($1) - 1;
1926d1fe9c09SJoe Perches		} elsif (substr($line, $pos, 1) eq '(') {
1927d1fe9c09SJoe Perches			$last_openparen = $pos;
1928d1fe9c09SJoe Perches		} elsif (index($string, '(') == -1) {
1929d1fe9c09SJoe Perches			last;
1930d1fe9c09SJoe Perches		}
1931d1fe9c09SJoe Perches	}
1932d1fe9c09SJoe Perches
193391cb5195SJoe Perches	return length(expand_tabs(substr($line, 0, $last_openparen))) + 1;
1934d1fe9c09SJoe Perches}
1935d1fe9c09SJoe Perches
19360a920b5bSAndy Whitcroftsub process {
19370a920b5bSAndy Whitcroft	my $filename = shift;
19380a920b5bSAndy Whitcroft
19390a920b5bSAndy Whitcroft	my $linenr=0;
19400a920b5bSAndy Whitcroft	my $prevline="";
1941c2fdda0dSAndy Whitcroft	my $prevrawline="";
19420a920b5bSAndy Whitcroft	my $stashline="";
1943c2fdda0dSAndy Whitcroft	my $stashrawline="";
19440a920b5bSAndy Whitcroft
19454a0df2efSAndy Whitcroft	my $length;
19460a920b5bSAndy Whitcroft	my $indent;
19470a920b5bSAndy Whitcroft	my $previndent=0;
19480a920b5bSAndy Whitcroft	my $stashindent=0;
19490a920b5bSAndy Whitcroft
1950de7d4f0eSAndy Whitcroft	our $clean = 1;
19510a920b5bSAndy Whitcroft	my $signoff = 0;
19520a920b5bSAndy Whitcroft	my $is_patch = 0;
19530a920b5bSAndy Whitcroft
195429ee1b0cSJoe Perches	my $in_header_lines = $file ? 0 : 1;
195515662b3eSJoe Perches	my $in_commit_log = 0;		#Scanning lines before patch
19562a076f40SJoe Perches	my $commit_log_long_line = 0;
1957*e518e9a5SJoe Perches	my $commit_log_has_diff = 0;
195813f1937eSJoe Perches	my $reported_maintainer_file = 0;
1959fa64205dSPasi Savanainen	my $non_utf8_charset = 0;
1960fa64205dSPasi Savanainen
1961365dd4eaSJoe Perches	my $last_blank_line = 0;
19625e4f6ba5SJoe Perches	my $last_coalesced_string_linenr = -1;
1963365dd4eaSJoe Perches
196413214adfSAndy Whitcroft	our @report = ();
19656c72ffaaSAndy Whitcroft	our $cnt_lines = 0;
19666c72ffaaSAndy Whitcroft	our $cnt_error = 0;
19676c72ffaaSAndy Whitcroft	our $cnt_warn = 0;
19686c72ffaaSAndy Whitcroft	our $cnt_chk = 0;
19696c72ffaaSAndy Whitcroft
19700a920b5bSAndy Whitcroft	# Trace the real file/line as we go.
19710a920b5bSAndy Whitcroft	my $realfile = '';
19720a920b5bSAndy Whitcroft	my $realline = 0;
19730a920b5bSAndy Whitcroft	my $realcnt = 0;
19740a920b5bSAndy Whitcroft	my $here = '';
19750a920b5bSAndy Whitcroft	my $in_comment = 0;
1976c2fdda0dSAndy Whitcroft	my $comment_edge = 0;
19770a920b5bSAndy Whitcroft	my $first_line = 0;
19781e855726SWolfram Sang	my $p1_prefix = '';
19790a920b5bSAndy Whitcroft
198013214adfSAndy Whitcroft	my $prev_values = 'E';
198113214adfSAndy Whitcroft
198213214adfSAndy Whitcroft	# suppression flags
1983773647a0SAndy Whitcroft	my %suppress_ifbraces;
1984170d3a22SAndy Whitcroft	my %suppress_whiletrailers;
19852b474a1aSAndy Whitcroft	my %suppress_export;
19863e469cdcSAndy Whitcroft	my $suppress_statement = 0;
1987653d4876SAndy Whitcroft
19887e51f197SJoe Perches	my %signatures = ();
1989323c1260SJoe Perches
1990c2fdda0dSAndy Whitcroft	# Pre-scan the patch sanitizing the lines.
1991de7d4f0eSAndy Whitcroft	# Pre-scan the patch looking for any __setup documentation.
1992c2fdda0dSAndy Whitcroft	#
1993de7d4f0eSAndy Whitcroft	my @setup_docs = ();
1994de7d4f0eSAndy Whitcroft	my $setup_docs = 0;
1995773647a0SAndy Whitcroft
1996d8b07710SJoe Perches	my $camelcase_file_seeded = 0;
1997d8b07710SJoe Perches
1998773647a0SAndy Whitcroft	sanitise_line_reset();
1999c2fdda0dSAndy Whitcroft	my $line;
2000c2fdda0dSAndy Whitcroft	foreach my $rawline (@rawlines) {
2001773647a0SAndy Whitcroft		$linenr++;
2002773647a0SAndy Whitcroft		$line = $rawline;
2003c2fdda0dSAndy Whitcroft
20043705ce5bSJoe Perches		push(@fixed, $rawline) if ($fix);
20053705ce5bSJoe Perches
2006773647a0SAndy Whitcroft		if ($rawline=~/^\+\+\+\s+(\S+)/) {
2007de7d4f0eSAndy Whitcroft			$setup_docs = 0;
2008de7d4f0eSAndy Whitcroft			if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
2009de7d4f0eSAndy Whitcroft				$setup_docs = 1;
2010de7d4f0eSAndy Whitcroft			}
2011773647a0SAndy Whitcroft			#next;
2012de7d4f0eSAndy Whitcroft		}
2013773647a0SAndy Whitcroft		if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
2014773647a0SAndy Whitcroft			$realline=$1-1;
2015773647a0SAndy Whitcroft			if (defined $2) {
2016773647a0SAndy Whitcroft				$realcnt=$3+1;
2017773647a0SAndy Whitcroft			} else {
2018773647a0SAndy Whitcroft				$realcnt=1+1;
2019773647a0SAndy Whitcroft			}
2020c45dcabdSAndy Whitcroft			$in_comment = 0;
2021773647a0SAndy Whitcroft
2022773647a0SAndy Whitcroft			# Guestimate if this is a continuing comment.  Run
2023773647a0SAndy Whitcroft			# the context looking for a comment "edge".  If this
2024773647a0SAndy Whitcroft			# edge is a close comment then we must be in a comment
2025773647a0SAndy Whitcroft			# at context start.
2026773647a0SAndy Whitcroft			my $edge;
202701fa9147SAndy Whitcroft			my $cnt = $realcnt;
202801fa9147SAndy Whitcroft			for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
202901fa9147SAndy Whitcroft				next if (defined $rawlines[$ln - 1] &&
203001fa9147SAndy Whitcroft					 $rawlines[$ln - 1] =~ /^-/);
203101fa9147SAndy Whitcroft				$cnt--;
203201fa9147SAndy Whitcroft				#print "RAW<$rawlines[$ln - 1]>\n";
2033721c1cb6SAndy Whitcroft				last if (!defined $rawlines[$ln - 1]);
2034fae17daeSAndy Whitcroft				if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
2035fae17daeSAndy Whitcroft				    $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
2036fae17daeSAndy Whitcroft					($edge) = $1;
2037fae17daeSAndy Whitcroft					last;
2038fae17daeSAndy Whitcroft				}
2039773647a0SAndy Whitcroft			}
2040773647a0SAndy Whitcroft			if (defined $edge && $edge eq '*/') {
2041773647a0SAndy Whitcroft				$in_comment = 1;
2042773647a0SAndy Whitcroft			}
2043773647a0SAndy Whitcroft
2044773647a0SAndy Whitcroft			# Guestimate if this is a continuing comment.  If this
2045773647a0SAndy Whitcroft			# is the start of a diff block and this line starts
2046773647a0SAndy Whitcroft			# ' *' then it is very likely a comment.
2047773647a0SAndy Whitcroft			if (!defined $edge &&
204883242e0cSAndy Whitcroft			    $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
2049773647a0SAndy Whitcroft			{
2050773647a0SAndy Whitcroft				$in_comment = 1;
2051773647a0SAndy Whitcroft			}
2052773647a0SAndy Whitcroft
2053773647a0SAndy Whitcroft			##print "COMMENT:$in_comment edge<$edge> $rawline\n";
2054773647a0SAndy Whitcroft			sanitise_line_reset($in_comment);
2055773647a0SAndy Whitcroft
2056171ae1a4SAndy Whitcroft		} elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
2057773647a0SAndy Whitcroft			# Standardise the strings and chars within the input to
2058171ae1a4SAndy Whitcroft			# simplify matching -- only bother with positive lines.
2059773647a0SAndy Whitcroft			$line = sanitise_line($rawline);
2060773647a0SAndy Whitcroft		}
2061773647a0SAndy Whitcroft		push(@lines, $line);
2062773647a0SAndy Whitcroft
2063773647a0SAndy Whitcroft		if ($realcnt > 1) {
2064773647a0SAndy Whitcroft			$realcnt-- if ($line =~ /^(?:\+| |$)/);
2065773647a0SAndy Whitcroft		} else {
2066773647a0SAndy Whitcroft			$realcnt = 0;
2067773647a0SAndy Whitcroft		}
2068773647a0SAndy Whitcroft
2069773647a0SAndy Whitcroft		#print "==>$rawline\n";
2070773647a0SAndy Whitcroft		#print "-->$line\n";
2071de7d4f0eSAndy Whitcroft
2072de7d4f0eSAndy Whitcroft		if ($setup_docs && $line =~ /^\+/) {
2073de7d4f0eSAndy Whitcroft			push(@setup_docs, $line);
2074de7d4f0eSAndy Whitcroft		}
2075de7d4f0eSAndy Whitcroft	}
2076de7d4f0eSAndy Whitcroft
20776c72ffaaSAndy Whitcroft	$prefix = '';
20786c72ffaaSAndy Whitcroft
2079773647a0SAndy Whitcroft	$realcnt = 0;
2080773647a0SAndy Whitcroft	$linenr = 0;
2081194f66fcSJoe Perches	$fixlinenr = -1;
20820a920b5bSAndy Whitcroft	foreach my $line (@lines) {
20830a920b5bSAndy Whitcroft		$linenr++;
2084194f66fcSJoe Perches		$fixlinenr++;
20851b5539b1SJoe Perches		my $sline = $line;	#copy of $line
20861b5539b1SJoe Perches		$sline =~ s/$;/ /g;	#with comments as spaces
20870a920b5bSAndy Whitcroft
2088c2fdda0dSAndy Whitcroft		my $rawline = $rawlines[$linenr - 1];
20896c72ffaaSAndy Whitcroft
20900a920b5bSAndy Whitcroft#extract the line range in the file after the patch is applied
2091*e518e9a5SJoe Perches		if (!$in_commit_log &&
2092*e518e9a5SJoe Perches		    $line =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
20930a920b5bSAndy Whitcroft			$is_patch = 1;
20944a0df2efSAndy Whitcroft			$first_line = $linenr + 1;
20950a920b5bSAndy Whitcroft			$realline=$1-1;
20960a920b5bSAndy Whitcroft			if (defined $2) {
20970a920b5bSAndy Whitcroft				$realcnt=$3+1;
20980a920b5bSAndy Whitcroft			} else {
20990a920b5bSAndy Whitcroft				$realcnt=1+1;
21000a920b5bSAndy Whitcroft			}
2101c2fdda0dSAndy Whitcroft			annotate_reset();
210213214adfSAndy Whitcroft			$prev_values = 'E';
210313214adfSAndy Whitcroft
2104773647a0SAndy Whitcroft			%suppress_ifbraces = ();
2105170d3a22SAndy Whitcroft			%suppress_whiletrailers = ();
21062b474a1aSAndy Whitcroft			%suppress_export = ();
21073e469cdcSAndy Whitcroft			$suppress_statement = 0;
21080a920b5bSAndy Whitcroft			next;
21090a920b5bSAndy Whitcroft
21104a0df2efSAndy Whitcroft# track the line number as we move through the hunk, note that
21114a0df2efSAndy Whitcroft# new versions of GNU diff omit the leading space on completely
21124a0df2efSAndy Whitcroft# blank context lines so we need to count that too.
2113773647a0SAndy Whitcroft		} elsif ($line =~ /^( |\+|$)/) {
21140a920b5bSAndy Whitcroft			$realline++;
2115d8aaf121SAndy Whitcroft			$realcnt-- if ($realcnt != 0);
21160a920b5bSAndy Whitcroft
21174a0df2efSAndy Whitcroft			# Measure the line length and indent.
2118c2fdda0dSAndy Whitcroft			($length, $indent) = line_stats($rawline);
21190a920b5bSAndy Whitcroft
21200a920b5bSAndy Whitcroft			# Track the previous line.
21210a920b5bSAndy Whitcroft			($prevline, $stashline) = ($stashline, $line);
21220a920b5bSAndy Whitcroft			($previndent, $stashindent) = ($stashindent, $indent);
2123c2fdda0dSAndy Whitcroft			($prevrawline, $stashrawline) = ($stashrawline, $rawline);
2124c2fdda0dSAndy Whitcroft
2125773647a0SAndy Whitcroft			#warn "line<$line>\n";
21266c72ffaaSAndy Whitcroft
2127d8aaf121SAndy Whitcroft		} elsif ($realcnt == 1) {
2128d8aaf121SAndy Whitcroft			$realcnt--;
21290a920b5bSAndy Whitcroft		}
21300a920b5bSAndy Whitcroft
2131cc77cdcaSAndy Whitcroft		my $hunk_line = ($realcnt != 0);
2132cc77cdcaSAndy Whitcroft
21336c72ffaaSAndy Whitcroft		$here = "#$linenr: " if (!$file);
21346c72ffaaSAndy Whitcroft		$here = "#$realline: " if ($file);
2135773647a0SAndy Whitcroft
21362ac73b4fSJoe Perches		my $found_file = 0;
2137773647a0SAndy Whitcroft		# extract the filename as it passes
21383bf9a009SRabin Vincent		if ($line =~ /^diff --git.*?(\S+)$/) {
21393bf9a009SRabin Vincent			$realfile = $1;
21402b7ab453SJoe Perches			$realfile =~ s@^([^/]*)/@@ if (!$file);
2141270c49a0SJoe Perches			$in_commit_log = 0;
21422ac73b4fSJoe Perches			$found_file = 1;
21433bf9a009SRabin Vincent		} elsif ($line =~ /^\+\+\+\s+(\S+)/) {
2144773647a0SAndy Whitcroft			$realfile = $1;
21452b7ab453SJoe Perches			$realfile =~ s@^([^/]*)/@@ if (!$file);
2146270c49a0SJoe Perches			$in_commit_log = 0;
21471e855726SWolfram Sang
21481e855726SWolfram Sang			$p1_prefix = $1;
2149e2f7aa4bSAndy Whitcroft			if (!$file && $tree && $p1_prefix ne '' &&
2150e2f7aa4bSAndy Whitcroft			    -e "$root/$p1_prefix") {
2151000d1cc1SJoe Perches				WARN("PATCH_PREFIX",
2152000d1cc1SJoe Perches				     "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
21531e855726SWolfram Sang			}
2154773647a0SAndy Whitcroft
2155c1ab3326SAndy Whitcroft			if ($realfile =~ m@^include/asm/@) {
2156000d1cc1SJoe Perches				ERROR("MODIFIED_INCLUDE_ASM",
2157000d1cc1SJoe Perches				      "do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
2158773647a0SAndy Whitcroft			}
21592ac73b4fSJoe Perches			$found_file = 1;
21602ac73b4fSJoe Perches		}
21612ac73b4fSJoe Perches
216234d8815fSJoe Perches#make up the handle for any error we report on this line
216334d8815fSJoe Perches		if ($showfile) {
216434d8815fSJoe Perches			$prefix = "$realfile:$realline: "
216534d8815fSJoe Perches		} elsif ($emacs) {
216634d8815fSJoe Perches			$prefix = "$filename:$linenr: ";
216734d8815fSJoe Perches		}
216834d8815fSJoe Perches
21692ac73b4fSJoe Perches		if ($found_file) {
21702ac73b4fSJoe Perches			if ($realfile =~ m@^(drivers/net/|net/)@) {
21712ac73b4fSJoe Perches				$check = 1;
21722ac73b4fSJoe Perches			} else {
21732ac73b4fSJoe Perches				$check = $check_orig;
21742ac73b4fSJoe Perches			}
2175773647a0SAndy Whitcroft			next;
2176773647a0SAndy Whitcroft		}
2177773647a0SAndy Whitcroft
2178389834b6SRandy Dunlap		$here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
21790a920b5bSAndy Whitcroft
2180c2fdda0dSAndy Whitcroft		my $hereline = "$here\n$rawline\n";
2181c2fdda0dSAndy Whitcroft		my $herecurr = "$here\n$rawline\n";
2182c2fdda0dSAndy Whitcroft		my $hereprev = "$here\n$prevrawline\n$rawline\n";
21830a920b5bSAndy Whitcroft
21846c72ffaaSAndy Whitcroft		$cnt_lines++ if ($realcnt != 0);
21856c72ffaaSAndy Whitcroft
2186*e518e9a5SJoe Perches# Check if the commit log has what seems like a diff which can confuse patch
2187*e518e9a5SJoe Perches		if ($in_commit_log && !$commit_log_has_diff &&
2188*e518e9a5SJoe Perches		    (($line =~ m@^\s+diff\b.*a/[\w/]+@ &&
2189*e518e9a5SJoe Perches		      $line =~ m@^\s+diff\b.*a/([\w/]+)\s+b/$1\b@) ||
2190*e518e9a5SJoe Perches		     $line =~ m@^\s*(?:\-\-\-\s+a/|\+\+\+\s+b/)@ ||
2191*e518e9a5SJoe Perches		     $line =~ m/^\s*\@\@ \-\d+,\d+ \+\d+,\d+ \@\@/)) {
2192*e518e9a5SJoe Perches			ERROR("DIFF_IN_COMMIT_MSG",
2193*e518e9a5SJoe Perches			      "Avoid using diff content in the commit message - patch(1) might not work\n" . $herecurr);
2194*e518e9a5SJoe Perches			$commit_log_has_diff = 1;
2195*e518e9a5SJoe Perches		}
2196*e518e9a5SJoe Perches
21973bf9a009SRabin Vincent# Check for incorrect file permissions
21983bf9a009SRabin Vincent		if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
21993bf9a009SRabin Vincent			my $permhere = $here . "FILE: $realfile\n";
220004db4d25SJoe Perches			if ($realfile !~ m@scripts/@ &&
220104db4d25SJoe Perches			    $realfile !~ /\.(py|pl|awk|sh)$/) {
2202000d1cc1SJoe Perches				ERROR("EXECUTE_PERMISSIONS",
2203000d1cc1SJoe Perches				      "do not set execute permissions for source files\n" . $permhere);
22043bf9a009SRabin Vincent			}
22053bf9a009SRabin Vincent		}
22063bf9a009SRabin Vincent
220720112475SJoe Perches# Check the patch for a signoff:
2208d8aaf121SAndy Whitcroft		if ($line =~ /^\s*signed-off-by:/i) {
22094a0df2efSAndy Whitcroft			$signoff++;
221015662b3eSJoe Perches			$in_commit_log = 0;
22110a920b5bSAndy Whitcroft		}
221220112475SJoe Perches
2213e0d975b1SJoe Perches# Check if MAINTAINERS is being updated.  If so, there's probably no need to
2214e0d975b1SJoe Perches# emit the "does MAINTAINERS need updating?" message on file add/move/delete
2215e0d975b1SJoe Perches		if ($line =~ /^\s*MAINTAINERS\s*\|/) {
2216e0d975b1SJoe Perches			$reported_maintainer_file = 1;
2217e0d975b1SJoe Perches		}
2218e0d975b1SJoe Perches
221920112475SJoe Perches# Check signature styles
2220270c49a0SJoe Perches		if (!$in_header_lines &&
2221ce0338dfSJoe Perches		    $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
222220112475SJoe Perches			my $space_before = $1;
222320112475SJoe Perches			my $sign_off = $2;
222420112475SJoe Perches			my $space_after = $3;
222520112475SJoe Perches			my $email = $4;
222620112475SJoe Perches			my $ucfirst_sign_off = ucfirst(lc($sign_off));
222720112475SJoe Perches
2228ce0338dfSJoe Perches			if ($sign_off !~ /$signature_tags/) {
2229ce0338dfSJoe Perches				WARN("BAD_SIGN_OFF",
2230ce0338dfSJoe Perches				     "Non-standard signature: $sign_off\n" . $herecurr);
2231ce0338dfSJoe Perches			}
223220112475SJoe Perches			if (defined $space_before && $space_before ne "") {
22333705ce5bSJoe Perches				if (WARN("BAD_SIGN_OFF",
22343705ce5bSJoe Perches					 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
22353705ce5bSJoe Perches				    $fix) {
2236194f66fcSJoe Perches					$fixed[$fixlinenr] =
22373705ce5bSJoe Perches					    "$ucfirst_sign_off $email";
22383705ce5bSJoe Perches				}
223920112475SJoe Perches			}
224020112475SJoe Perches			if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
22413705ce5bSJoe Perches				if (WARN("BAD_SIGN_OFF",
22423705ce5bSJoe Perches					 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
22433705ce5bSJoe Perches				    $fix) {
2244194f66fcSJoe Perches					$fixed[$fixlinenr] =
22453705ce5bSJoe Perches					    "$ucfirst_sign_off $email";
22463705ce5bSJoe Perches				}
22473705ce5bSJoe Perches
224820112475SJoe Perches			}
224920112475SJoe Perches			if (!defined $space_after || $space_after ne " ") {
22503705ce5bSJoe Perches				if (WARN("BAD_SIGN_OFF",
22513705ce5bSJoe Perches					 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
22523705ce5bSJoe Perches				    $fix) {
2253194f66fcSJoe Perches					$fixed[$fixlinenr] =
22543705ce5bSJoe Perches					    "$ucfirst_sign_off $email";
22553705ce5bSJoe Perches				}
225620112475SJoe Perches			}
225720112475SJoe Perches
225820112475SJoe Perches			my ($email_name, $email_address, $comment) = parse_email($email);
225920112475SJoe Perches			my $suggested_email = format_email(($email_name, $email_address));
226020112475SJoe Perches			if ($suggested_email eq "") {
2261000d1cc1SJoe Perches				ERROR("BAD_SIGN_OFF",
2262000d1cc1SJoe Perches				      "Unrecognized email address: '$email'\n" . $herecurr);
226320112475SJoe Perches			} else {
226420112475SJoe Perches				my $dequoted = $suggested_email;
226520112475SJoe Perches				$dequoted =~ s/^"//;
226620112475SJoe Perches				$dequoted =~ s/" </ </;
226720112475SJoe Perches				# Don't force email to have quotes
226820112475SJoe Perches				# Allow just an angle bracketed address
226920112475SJoe Perches				if ("$dequoted$comment" ne $email &&
227020112475SJoe Perches				    "<$email_address>$comment" ne $email &&
227120112475SJoe Perches				    "$suggested_email$comment" ne $email) {
2272000d1cc1SJoe Perches					WARN("BAD_SIGN_OFF",
2273000d1cc1SJoe Perches					     "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
227420112475SJoe Perches				}
22750a920b5bSAndy Whitcroft			}
22767e51f197SJoe Perches
22777e51f197SJoe Perches# Check for duplicate signatures
22787e51f197SJoe Perches			my $sig_nospace = $line;
22797e51f197SJoe Perches			$sig_nospace =~ s/\s//g;
22807e51f197SJoe Perches			$sig_nospace = lc($sig_nospace);
22817e51f197SJoe Perches			if (defined $signatures{$sig_nospace}) {
22827e51f197SJoe Perches				WARN("BAD_SIGN_OFF",
22837e51f197SJoe Perches				     "Duplicate signature\n" . $herecurr);
22847e51f197SJoe Perches			} else {
22857e51f197SJoe Perches				$signatures{$sig_nospace} = 1;
22867e51f197SJoe Perches			}
22870a920b5bSAndy Whitcroft		}
22880a920b5bSAndy Whitcroft
2289a2fe16b9SJoe Perches# Check email subject for common tools that don't need to be mentioned
2290a2fe16b9SJoe Perches		if ($in_header_lines &&
2291a2fe16b9SJoe Perches		    $line =~ /^Subject:.*\b(?:checkpatch|sparse|smatch)\b[^:]/i) {
2292a2fe16b9SJoe Perches			WARN("EMAIL_SUBJECT",
2293a2fe16b9SJoe Perches			     "A patch subject line should describe the change not the tool that found it\n" . $herecurr);
2294a2fe16b9SJoe Perches		}
2295a2fe16b9SJoe Perches
22969b3189ebSJoe Perches# Check for old stable address
22979b3189ebSJoe Perches		if ($line =~ /^\s*cc:\s*.*<?\bstable\@kernel\.org\b>?.*$/i) {
22989b3189ebSJoe Perches			ERROR("STABLE_ADDRESS",
22999b3189ebSJoe Perches			      "The 'stable' address should be 'stable\@vger.kernel.org'\n" . $herecurr);
23009b3189ebSJoe Perches		}
23019b3189ebSJoe Perches
23027ebd05efSChristopher Covington# Check for unwanted Gerrit info
23037ebd05efSChristopher Covington		if ($in_commit_log && $line =~ /^\s*change-id:/i) {
23047ebd05efSChristopher Covington			ERROR("GERRIT_CHANGE_ID",
23057ebd05efSChristopher Covington			      "Remove Gerrit Change-Id's before submitting upstream.\n" . $herecurr);
23067ebd05efSChristopher Covington		}
23077ebd05efSChristopher Covington
23082a076f40SJoe Perches# Check for line lengths > 75 in commit log, warn once
23092a076f40SJoe Perches		if ($in_commit_log && !$commit_log_long_line &&
23102a076f40SJoe Perches		    length($line) > 75) {
23112a076f40SJoe Perches			WARN("COMMIT_LOG_LONG_LINE",
23122a076f40SJoe Perches			     "Possible unwrapped commit description (prefer a maximum 75 chars per line)\n" . $herecurr);
23132a076f40SJoe Perches			$commit_log_long_line = 1;
23142a076f40SJoe Perches		}
23152a076f40SJoe Perches
23160d7835fcSJoe Perches# Check for git id commit length and improperly formed commit descriptions
23170d7835fcSJoe Perches		if ($in_commit_log && $line =~ /\b(c)ommit\s+([0-9a-f]{5,})/i) {
2318d311cd44SJoe Perches			my $init_char = $1;
2319d311cd44SJoe Perches			my $orig_commit = lc($2);
23200d7835fcSJoe Perches			my $short = 1;
23210d7835fcSJoe Perches			my $long = 0;
23220d7835fcSJoe Perches			my $case = 1;
23230d7835fcSJoe Perches			my $space = 1;
23240d7835fcSJoe Perches			my $hasdesc = 0;
232519c146a6SJoe Perches			my $hasparens = 0;
23260d7835fcSJoe Perches			my $id = '0123456789ab';
23270d7835fcSJoe Perches			my $orig_desc = "commit description";
23280d7835fcSJoe Perches			my $description = "";
23290d7835fcSJoe Perches
23300d7835fcSJoe Perches			$short = 0 if ($line =~ /\bcommit\s+[0-9a-f]{12,40}/i);
23310d7835fcSJoe Perches			$long = 1 if ($line =~ /\bcommit\s+[0-9a-f]{41,}/i);
23320d7835fcSJoe Perches			$space = 0 if ($line =~ /\bcommit [0-9a-f]/i);
23330d7835fcSJoe Perches			$case = 0 if ($line =~ /\b[Cc]ommit\s+[0-9a-f]{5,40}[^A-F]/);
23340d7835fcSJoe Perches			if ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)"\)/i) {
23350d7835fcSJoe Perches				$orig_desc = $1;
233619c146a6SJoe Perches				$hasparens = 1;
23370d7835fcSJoe Perches			} elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s*$/i &&
23380d7835fcSJoe Perches				 defined $rawlines[$linenr] &&
23390d7835fcSJoe Perches				 $rawlines[$linenr] =~ /^\s*\("([^"]+)"\)/) {
23400d7835fcSJoe Perches				$orig_desc = $1;
234119c146a6SJoe Perches				$hasparens = 1;
2342b671fde0SJoe Perches			} elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("[^"]+$/i &&
2343b671fde0SJoe Perches				 defined $rawlines[$linenr] &&
2344b671fde0SJoe Perches				 $rawlines[$linenr] =~ /^\s*[^"]+"\)/) {
2345b671fde0SJoe Perches				$line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)$/i;
2346b671fde0SJoe Perches				$orig_desc = $1;
2347b671fde0SJoe Perches				$rawlines[$linenr] =~ /^\s*([^"]+)"\)/;
2348b671fde0SJoe Perches				$orig_desc .= " " . $1;
234919c146a6SJoe Perches				$hasparens = 1;
23500d7835fcSJoe Perches			}
23510d7835fcSJoe Perches
23520d7835fcSJoe Perches			($id, $description) = git_commit_info($orig_commit,
23530d7835fcSJoe Perches							      $id, $orig_desc);
23540d7835fcSJoe Perches
235519c146a6SJoe Perches			if ($short || $long || $space || $case || ($orig_desc ne $description) || !$hasparens) {
2356d311cd44SJoe Perches				ERROR("GIT_COMMIT_ID",
23570d7835fcSJoe Perches				      "Please use git commit description style 'commit <12+ chars of sha1> (\"<title line>\")' - ie: '${init_char}ommit $id (\"$description\")'\n" . $herecurr);
23580d7835fcSJoe Perches			}
2359d311cd44SJoe Perches		}
2360d311cd44SJoe Perches
236113f1937eSJoe Perches# Check for added, moved or deleted files
236213f1937eSJoe Perches		if (!$reported_maintainer_file && !$in_commit_log &&
236313f1937eSJoe Perches		    ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ ||
236413f1937eSJoe Perches		     $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ ||
236513f1937eSJoe Perches		     ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ &&
236613f1937eSJoe Perches		      (defined($1) || defined($2))))) {
236713f1937eSJoe Perches			$reported_maintainer_file = 1;
236813f1937eSJoe Perches			WARN("FILE_PATH_CHANGES",
236913f1937eSJoe Perches			     "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr);
237013f1937eSJoe Perches		}
237113f1937eSJoe Perches
237200df344fSAndy Whitcroft# Check for wrappage within a valid hunk of the file
23738905a67cSAndy Whitcroft		if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
2374000d1cc1SJoe Perches			ERROR("CORRUPTED_PATCH",
2375000d1cc1SJoe Perches			      "patch seems to be corrupt (line wrapped?)\n" .
23766c72ffaaSAndy Whitcroft				$herecurr) if (!$emitted_corrupt++);
2377de7d4f0eSAndy Whitcroft		}
2378de7d4f0eSAndy Whitcroft
23796ecd9674SAndy Whitcroft# Check for absolute kernel paths.
23806ecd9674SAndy Whitcroft		if ($tree) {
23816ecd9674SAndy Whitcroft			while ($line =~ m{(?:^|\s)(/\S*)}g) {
23826ecd9674SAndy Whitcroft				my $file = $1;
23836ecd9674SAndy Whitcroft
23846ecd9674SAndy Whitcroft				if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
23856ecd9674SAndy Whitcroft				    check_absolute_file($1, $herecurr)) {
23866ecd9674SAndy Whitcroft					#
23876ecd9674SAndy Whitcroft				} else {
23886ecd9674SAndy Whitcroft					check_absolute_file($file, $herecurr);
23896ecd9674SAndy Whitcroft				}
23906ecd9674SAndy Whitcroft			}
23916ecd9674SAndy Whitcroft		}
23926ecd9674SAndy Whitcroft
2393de7d4f0eSAndy Whitcroft# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
2394de7d4f0eSAndy Whitcroft		if (($realfile =~ /^$/ || $line =~ /^\+/) &&
2395171ae1a4SAndy Whitcroft		    $rawline !~ m/^$UTF8*$/) {
2396171ae1a4SAndy Whitcroft			my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
2397171ae1a4SAndy Whitcroft
2398171ae1a4SAndy Whitcroft			my $blank = copy_spacing($rawline);
2399171ae1a4SAndy Whitcroft			my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
2400171ae1a4SAndy Whitcroft			my $hereptr = "$hereline$ptr\n";
2401171ae1a4SAndy Whitcroft
240234d99219SJoe Perches			CHK("INVALID_UTF8",
2403000d1cc1SJoe Perches			    "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
240400df344fSAndy Whitcroft		}
24050a920b5bSAndy Whitcroft
240615662b3eSJoe Perches# Check if it's the start of a commit log
240715662b3eSJoe Perches# (not a header line and we haven't seen the patch filename)
240815662b3eSJoe Perches		if ($in_header_lines && $realfile =~ /^$/ &&
240929ee1b0cSJoe Perches		    !($rawline =~ /^\s+\S/ ||
241029ee1b0cSJoe Perches		      $rawline =~ /^(commit\b|from\b|[\w-]+:).*$/i)) {
241115662b3eSJoe Perches			$in_header_lines = 0;
241215662b3eSJoe Perches			$in_commit_log = 1;
241315662b3eSJoe Perches		}
241415662b3eSJoe Perches
2415fa64205dSPasi Savanainen# Check if there is UTF-8 in a commit log when a mail header has explicitly
2416fa64205dSPasi Savanainen# declined it, i.e defined some charset where it is missing.
2417fa64205dSPasi Savanainen		if ($in_header_lines &&
2418fa64205dSPasi Savanainen		    $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
2419fa64205dSPasi Savanainen		    $1 !~ /utf-8/i) {
2420fa64205dSPasi Savanainen			$non_utf8_charset = 1;
2421fa64205dSPasi Savanainen		}
2422fa64205dSPasi Savanainen
2423fa64205dSPasi Savanainen		if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
242415662b3eSJoe Perches		    $rawline =~ /$NON_ASCII_UTF8/) {
2425fa64205dSPasi Savanainen			WARN("UTF8_BEFORE_PATCH",
242615662b3eSJoe Perches			    "8-bit UTF-8 used in possible commit log\n" . $herecurr);
242715662b3eSJoe Perches		}
242815662b3eSJoe Perches
242966b47b4aSKees Cook# Check for various typo / spelling mistakes
243066d7a382SJoe Perches		if (defined($misspellings) &&
243166d7a382SJoe Perches		    ($in_commit_log || $line =~ /^(?:\+|Subject:)/i)) {
2432ebfd7d62SJoe Perches			while ($rawline =~ /(?:^|[^a-z@])($misspellings)(?:\b|$|[^a-z@])/gi) {
243366b47b4aSKees Cook				my $typo = $1;
243466b47b4aSKees Cook				my $typo_fix = $spelling_fix{lc($typo)};
243566b47b4aSKees Cook				$typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/);
243666b47b4aSKees Cook				$typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/);
243766b47b4aSKees Cook				my $msg_type = \&WARN;
243866b47b4aSKees Cook				$msg_type = \&CHK if ($file);
243966b47b4aSKees Cook				if (&{$msg_type}("TYPO_SPELLING",
244066b47b4aSKees Cook						 "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $herecurr) &&
244166b47b4aSKees Cook				    $fix) {
244266b47b4aSKees Cook					$fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/;
244366b47b4aSKees Cook				}
244466b47b4aSKees Cook			}
244566b47b4aSKees Cook		}
244666b47b4aSKees Cook
244730670854SAndy Whitcroft# ignore non-hunk lines and lines being removed
244830670854SAndy Whitcroft		next if (!$hunk_line || $line =~ /^-/);
244900df344fSAndy Whitcroft
24500a920b5bSAndy Whitcroft#trailing whitespace
24519c0ca6f9SAndy Whitcroft		if ($line =~ /^\+.*\015/) {
2452c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2453d5e616fcSJoe Perches			if (ERROR("DOS_LINE_ENDINGS",
2454d5e616fcSJoe Perches				  "DOS line endings\n" . $herevet) &&
2455d5e616fcSJoe Perches			    $fix) {
2456194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/[\s\015]+$//;
2457d5e616fcSJoe Perches			}
2458c2fdda0dSAndy Whitcroft		} elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
2459c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
24603705ce5bSJoe Perches			if (ERROR("TRAILING_WHITESPACE",
24613705ce5bSJoe Perches				  "trailing whitespace\n" . $herevet) &&
24623705ce5bSJoe Perches			    $fix) {
2463194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\s+$//;
24643705ce5bSJoe Perches			}
24653705ce5bSJoe Perches
2466d2c0a235SAndy Whitcroft			$rpt_cleaners = 1;
24670a920b5bSAndy Whitcroft		}
24685368df20SAndy Whitcroft
24694783f894SJosh Triplett# Check for FSF mailing addresses.
2470109d8cb2SAlexander Duyck		if ($rawline =~ /\bwrite to the Free/i ||
24713e2232f2SJoe Perches		    $rawline =~ /\b59\s+Temple\s+Pl/i ||
24723e2232f2SJoe Perches		    $rawline =~ /\b51\s+Franklin\s+St/i) {
24734783f894SJosh Triplett			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
24744783f894SJosh Triplett			my $msg_type = \&ERROR;
24754783f894SJosh Triplett			$msg_type = \&CHK if ($file);
24764783f894SJosh Triplett			&{$msg_type}("FSF_MAILING_ADDRESS",
24774783f894SJosh 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)
24784783f894SJosh Triplett		}
24794783f894SJosh Triplett
24803354957aSAndi Kleen# check for Kconfig help text having a real description
24819fe287d7SAndy Whitcroft# Only applies when adding the entry originally, after that we do not have
24829fe287d7SAndy Whitcroft# sufficient context to determine whether it is indeed long enough.
24833354957aSAndi Kleen		if ($realfile =~ /Kconfig/ &&
24848d73e0e7SJoe Perches		    $line =~ /^\+\s*config\s+/) {
24853354957aSAndi Kleen			my $length = 0;
24869fe287d7SAndy Whitcroft			my $cnt = $realcnt;
24879fe287d7SAndy Whitcroft			my $ln = $linenr + 1;
24889fe287d7SAndy Whitcroft			my $f;
2489a1385803SAndy Whitcroft			my $is_start = 0;
24909fe287d7SAndy Whitcroft			my $is_end = 0;
2491a1385803SAndy Whitcroft			for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
24929fe287d7SAndy Whitcroft				$f = $lines[$ln - 1];
24939fe287d7SAndy Whitcroft				$cnt-- if ($lines[$ln - 1] !~ /^-/);
24949fe287d7SAndy Whitcroft				$is_end = $lines[$ln - 1] =~ /^\+/;
24959fe287d7SAndy Whitcroft
24969fe287d7SAndy Whitcroft				next if ($f =~ /^-/);
24978d73e0e7SJoe Perches				last if (!$file && $f =~ /^\@\@/);
2498a1385803SAndy Whitcroft
24998d73e0e7SJoe Perches				if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate)\s*\"/) {
2500a1385803SAndy Whitcroft					$is_start = 1;
25018d73e0e7SJoe Perches				} elsif ($lines[$ln - 1] =~ /^\+\s*(?:---)?help(?:---)?$/) {
2502a1385803SAndy Whitcroft					$length = -1;
2503a1385803SAndy Whitcroft				}
2504a1385803SAndy Whitcroft
25059fe287d7SAndy Whitcroft				$f =~ s/^.//;
25063354957aSAndi Kleen				$f =~ s/#.*//;
25073354957aSAndi Kleen				$f =~ s/^\s+//;
25083354957aSAndi Kleen				next if ($f =~ /^$/);
25099fe287d7SAndy Whitcroft				if ($f =~ /^\s*config\s/) {
25109fe287d7SAndy Whitcroft					$is_end = 1;
25119fe287d7SAndy Whitcroft					last;
25129fe287d7SAndy Whitcroft				}
25133354957aSAndi Kleen				$length++;
25143354957aSAndi Kleen			}
251556193274SVadim Bendebury			if ($is_start && $is_end && $length < $min_conf_desc_length) {
2516000d1cc1SJoe Perches				WARN("CONFIG_DESCRIPTION",
251756193274SVadim Bendebury				     "please write a paragraph that describes the config symbol fully\n" . $herecurr);
251856193274SVadim Bendebury			}
2519a1385803SAndy Whitcroft			#print "is_start<$is_start> is_end<$is_end> length<$length>\n";
25203354957aSAndi Kleen		}
25213354957aSAndi Kleen
25221ba8dfd1SKees Cook# discourage the addition of CONFIG_EXPERIMENTAL in Kconfig.
25231ba8dfd1SKees Cook		if ($realfile =~ /Kconfig/ &&
25241ba8dfd1SKees Cook		    $line =~ /.\s*depends on\s+.*\bEXPERIMENTAL\b/) {
25251ba8dfd1SKees Cook			WARN("CONFIG_EXPERIMENTAL",
25261ba8dfd1SKees Cook			     "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
25271ba8dfd1SKees Cook		}
25281ba8dfd1SKees Cook
2529327953e9SChristoph Jaeger# discourage the use of boolean for type definition attributes of Kconfig options
2530327953e9SChristoph Jaeger		if ($realfile =~ /Kconfig/ &&
2531327953e9SChristoph Jaeger		    $line =~ /^\+\s*\bboolean\b/) {
2532327953e9SChristoph Jaeger			WARN("CONFIG_TYPE_BOOLEAN",
2533327953e9SChristoph Jaeger			     "Use of boolean is deprecated, please use bool instead.\n" . $herecurr);
2534327953e9SChristoph Jaeger		}
2535327953e9SChristoph Jaeger
2536c68e5878SArnaud Lacombe		if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
2537c68e5878SArnaud Lacombe		    ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
2538c68e5878SArnaud Lacombe			my $flag = $1;
2539c68e5878SArnaud Lacombe			my $replacement = {
2540c68e5878SArnaud Lacombe				'EXTRA_AFLAGS' =>   'asflags-y',
2541c68e5878SArnaud Lacombe				'EXTRA_CFLAGS' =>   'ccflags-y',
2542c68e5878SArnaud Lacombe				'EXTRA_CPPFLAGS' => 'cppflags-y',
2543c68e5878SArnaud Lacombe				'EXTRA_LDFLAGS' =>  'ldflags-y',
2544c68e5878SArnaud Lacombe			};
2545c68e5878SArnaud Lacombe
2546c68e5878SArnaud Lacombe			WARN("DEPRECATED_VARIABLE",
2547c68e5878SArnaud Lacombe			     "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
2548c68e5878SArnaud Lacombe		}
2549c68e5878SArnaud Lacombe
2550bff5da43SRob Herring# check for DT compatible documentation
25517dd05b38SFlorian Vaussard		if (defined $root &&
25527dd05b38SFlorian Vaussard			(($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) ||
25537dd05b38SFlorian Vaussard			 ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) {
25547dd05b38SFlorian Vaussard
2555bff5da43SRob Herring			my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g;
2556bff5da43SRob Herring
2557cc93319bSFlorian Vaussard			my $dt_path = $root . "/Documentation/devicetree/bindings/";
2558cc93319bSFlorian Vaussard			my $vp_file = $dt_path . "vendor-prefixes.txt";
2559cc93319bSFlorian Vaussard
2560bff5da43SRob Herring			foreach my $compat (@compats) {
2561bff5da43SRob Herring				my $compat2 = $compat;
2562185d566bSRob Herring				$compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/;
2563185d566bSRob Herring				my $compat3 = $compat;
2564185d566bSRob Herring				$compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/;
2565185d566bSRob Herring				`grep -Erq "$compat|$compat2|$compat3" $dt_path`;
2566bff5da43SRob Herring				if ( $? >> 8 ) {
2567bff5da43SRob Herring					WARN("UNDOCUMENTED_DT_STRING",
2568bff5da43SRob Herring					     "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr);
2569bff5da43SRob Herring				}
2570bff5da43SRob Herring
25714fbf32a6SFlorian Vaussard				next if $compat !~ /^([a-zA-Z0-9\-]+)\,/;
25724fbf32a6SFlorian Vaussard				my $vendor = $1;
2573cc93319bSFlorian Vaussard				`grep -Eq "^$vendor\\b" $vp_file`;
2574bff5da43SRob Herring				if ( $? >> 8 ) {
2575bff5da43SRob Herring					WARN("UNDOCUMENTED_DT_STRING",
2576cc93319bSFlorian Vaussard					     "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr);
2577bff5da43SRob Herring				}
2578bff5da43SRob Herring			}
2579bff5da43SRob Herring		}
2580bff5da43SRob Herring
25815368df20SAndy Whitcroft# check we are in a valid source file if not then ignore this hunk
2582de4c924cSGeert Uytterhoeven		next if ($realfile !~ /\.(h|c|s|S|pl|sh|dtsi|dts)$/);
25835368df20SAndy Whitcroft
258447e0c88bSJoe Perches# line length limit (with some exclusions)
258547e0c88bSJoe Perches#
258647e0c88bSJoe Perches# There are a few types of lines that may extend beyond $max_line_length:
258747e0c88bSJoe Perches#	logging functions like pr_info that end in a string
258847e0c88bSJoe Perches#	lines with a single string
258947e0c88bSJoe Perches#	#defines that are a single string
259047e0c88bSJoe Perches#
259147e0c88bSJoe Perches# There are 3 different line length message types:
259247e0c88bSJoe Perches# LONG_LINE_COMMENT	a comment starts before but extends beyond $max_linelength
259347e0c88bSJoe Perches# LONG_LINE_STRING	a string starts before but extends beyond $max_line_length
259447e0c88bSJoe Perches# LONG_LINE		all other lines longer than $max_line_length
259547e0c88bSJoe Perches#
259647e0c88bSJoe Perches# if LONG_LINE is ignored, the other 2 types are also ignored
259747e0c88bSJoe Perches#
259847e0c88bSJoe Perches
259947e0c88bSJoe Perches		if ($length > $max_line_length) {
260047e0c88bSJoe Perches			my $msg_type = "LONG_LINE";
260147e0c88bSJoe Perches
260247e0c88bSJoe Perches			# Check the allowed long line types first
260347e0c88bSJoe Perches
260447e0c88bSJoe Perches			# logging functions that end in a string that starts
260547e0c88bSJoe Perches			# before $max_line_length
260647e0c88bSJoe Perches			if ($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(?:KERN_\S+\s*|[^"]*))?($String\s*(?:|,|\)\s*;)\s*)$/ &&
260747e0c88bSJoe Perches			    length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
260847e0c88bSJoe Perches				$msg_type = "";
260947e0c88bSJoe Perches
261047e0c88bSJoe Perches			# lines with only strings (w/ possible termination)
261147e0c88bSJoe Perches			# #defines with only strings
261247e0c88bSJoe Perches			} elsif ($line =~ /^\+\s*$String\s*(?:\s*|,|\)\s*;)\s*$/ ||
261347e0c88bSJoe Perches				 $line =~ /^\+\s*#\s*define\s+\w+\s+$String$/) {
261447e0c88bSJoe Perches				$msg_type = "";
261547e0c88bSJoe Perches
261647e0c88bSJoe Perches			# Otherwise set the alternate message types
261747e0c88bSJoe Perches
261847e0c88bSJoe Perches			# a comment starts before $max_line_length
261947e0c88bSJoe Perches			} elsif ($line =~ /($;[\s$;]*)$/ &&
262047e0c88bSJoe Perches				 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
262147e0c88bSJoe Perches				$msg_type = "LONG_LINE_COMMENT"
262247e0c88bSJoe Perches
262347e0c88bSJoe Perches			# a quoted string starts before $max_line_length
262447e0c88bSJoe Perches			} elsif ($sline =~ /\s*($String(?:\s*(?:\\|,\s*|\)\s*;\s*))?)$/ &&
262547e0c88bSJoe Perches				 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
262647e0c88bSJoe Perches				$msg_type = "LONG_LINE_STRING"
262747e0c88bSJoe Perches			}
262847e0c88bSJoe Perches
262947e0c88bSJoe Perches			if ($msg_type ne "" &&
263047e0c88bSJoe Perches			    (show_type("LONG_LINE") || show_type($msg_type))) {
263147e0c88bSJoe Perches				WARN($msg_type,
26326cd7f386SJoe Perches				     "line over $max_line_length characters\n" . $herecurr);
26330a920b5bSAndy Whitcroft			}
263447e0c88bSJoe Perches		}
26350a920b5bSAndy Whitcroft
26368905a67cSAndy Whitcroft# check for adding lines without a newline.
26378905a67cSAndy Whitcroft		if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
2638000d1cc1SJoe Perches			WARN("MISSING_EOF_NEWLINE",
2639000d1cc1SJoe Perches			     "adding a line without newline at end of file\n" . $herecurr);
26408905a67cSAndy Whitcroft		}
26418905a67cSAndy Whitcroft
264242e41c54SMike Frysinger# Blackfin: use hi/lo macros
264342e41c54SMike Frysinger		if ($realfile =~ m@arch/blackfin/.*\.S$@) {
264442e41c54SMike Frysinger			if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
264542e41c54SMike Frysinger				my $herevet = "$here\n" . cat_vet($line) . "\n";
2646000d1cc1SJoe Perches				ERROR("LO_MACRO",
2647000d1cc1SJoe Perches				      "use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
264842e41c54SMike Frysinger			}
264942e41c54SMike Frysinger			if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
265042e41c54SMike Frysinger				my $herevet = "$here\n" . cat_vet($line) . "\n";
2651000d1cc1SJoe Perches				ERROR("HI_MACRO",
2652000d1cc1SJoe Perches				      "use the HI() macro, not (... >> 16)\n" . $herevet);
265342e41c54SMike Frysinger			}
265442e41c54SMike Frysinger		}
265542e41c54SMike Frysinger
2656b9ea10d6SAndy Whitcroft# check we are in a valid source file C or perl if not then ignore this hunk
2657de4c924cSGeert Uytterhoeven		next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/);
26580a920b5bSAndy Whitcroft
26590a920b5bSAndy Whitcroft# at the beginning of a line any tabs must come first and anything
26600a920b5bSAndy Whitcroft# more than 8 must use tabs.
2661c2fdda0dSAndy Whitcroft		if ($rawline =~ /^\+\s* \t\s*\S/ ||
2662c2fdda0dSAndy Whitcroft		    $rawline =~ /^\+\s*        \s*/) {
2663c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2664d2c0a235SAndy Whitcroft			$rpt_cleaners = 1;
26653705ce5bSJoe Perches			if (ERROR("CODE_INDENT",
26663705ce5bSJoe Perches				  "code indent should use tabs where possible\n" . $herevet) &&
26673705ce5bSJoe Perches			    $fix) {
2668194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
26693705ce5bSJoe Perches			}
26700a920b5bSAndy Whitcroft		}
26710a920b5bSAndy Whitcroft
267208e44365SAlberto Panizzo# check for space before tabs.
267308e44365SAlberto Panizzo		if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
267408e44365SAlberto Panizzo			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
26753705ce5bSJoe Perches			if (WARN("SPACE_BEFORE_TAB",
26763705ce5bSJoe Perches				"please, no space before tabs\n" . $herevet) &&
26773705ce5bSJoe Perches			    $fix) {
2678194f66fcSJoe Perches				while ($fixed[$fixlinenr] =~
2679d2207ccbSJoe Perches					   s/(^\+.*) {8,8}\t/$1\t\t/) {}
2680194f66fcSJoe Perches				while ($fixed[$fixlinenr] =~
2681c76f4cb3SJoe Perches					   s/(^\+.*) +\t/$1\t/) {}
26823705ce5bSJoe Perches			}
268308e44365SAlberto Panizzo		}
268408e44365SAlberto Panizzo
2685d1fe9c09SJoe Perches# check for && or || at the start of a line
2686d1fe9c09SJoe Perches		if ($rawline =~ /^\+\s*(&&|\|\|)/) {
2687d1fe9c09SJoe Perches			CHK("LOGICAL_CONTINUATIONS",
2688d1fe9c09SJoe Perches			    "Logical continuations should be on the previous line\n" . $hereprev);
2689d1fe9c09SJoe Perches		}
2690d1fe9c09SJoe Perches
2691d1fe9c09SJoe Perches# check multi-line statement indentation matches previous line
2692d1fe9c09SJoe Perches		if ($^V && $^V ge 5.10.0 &&
269391cb5195SJoe Perches		    $prevline =~ /^\+([ \t]*)((?:$c90_Keywords(?:\s+if)\s*)|(?:$Declare\s*)?(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*|$Ident\s*=\s*$Ident\s*)\(.*(\&\&|\|\||,)\s*$/) {
2694d1fe9c09SJoe Perches			$prevline =~ /^\+(\t*)(.*)$/;
2695d1fe9c09SJoe Perches			my $oldindent = $1;
2696d1fe9c09SJoe Perches			my $rest = $2;
2697d1fe9c09SJoe Perches
2698d1fe9c09SJoe Perches			my $pos = pos_last_openparen($rest);
2699d1fe9c09SJoe Perches			if ($pos >= 0) {
2700b34a26f3SJoe Perches				$line =~ /^(\+| )([ \t]*)/;
2701b34a26f3SJoe Perches				my $newindent = $2;
2702d1fe9c09SJoe Perches
2703d1fe9c09SJoe Perches				my $goodtabindent = $oldindent .
2704d1fe9c09SJoe Perches					"\t" x ($pos / 8) .
2705d1fe9c09SJoe Perches					" "  x ($pos % 8);
2706d1fe9c09SJoe Perches				my $goodspaceindent = $oldindent . " "  x $pos;
2707d1fe9c09SJoe Perches
2708d1fe9c09SJoe Perches				if ($newindent ne $goodtabindent &&
2709d1fe9c09SJoe Perches				    $newindent ne $goodspaceindent) {
27103705ce5bSJoe Perches
27113705ce5bSJoe Perches					if (CHK("PARENTHESIS_ALIGNMENT",
27123705ce5bSJoe Perches						"Alignment should match open parenthesis\n" . $hereprev) &&
27133705ce5bSJoe Perches					    $fix && $line =~ /^\+/) {
2714194f66fcSJoe Perches						$fixed[$fixlinenr] =~
27153705ce5bSJoe Perches						    s/^\+[ \t]*/\+$goodtabindent/;
27163705ce5bSJoe Perches					}
2717d1fe9c09SJoe Perches				}
2718d1fe9c09SJoe Perches			}
2719d1fe9c09SJoe Perches		}
2720d1fe9c09SJoe Perches
27216ab3a970SJoe Perches# check for space after cast like "(int) foo" or "(struct foo) bar"
27226ab3a970SJoe Perches# avoid checking a few false positives:
27236ab3a970SJoe Perches#   "sizeof(<type>)" or "__alignof__(<type>)"
27246ab3a970SJoe Perches#   function pointer declarations like "(*foo)(int) = bar;"
27256ab3a970SJoe Perches#   structure definitions like "(struct foo) { 0 };"
27266ab3a970SJoe Perches#   multiline macros that define functions
27276ab3a970SJoe Perches#   known attributes or the __attribute__ keyword
27286ab3a970SJoe Perches		if ($line =~ /^\+(.*)\(\s*$Type\s*\)([ \t]++)((?![={]|\\$|$Attribute|__attribute__))/ &&
27296ab3a970SJoe Perches		    (!defined($1) || $1 !~ /\b(?:sizeof|__alignof__)\s*$/)) {
27303705ce5bSJoe Perches			if (CHK("SPACING",
2731f27c95dbSJoe Perches				"No space is necessary after a cast\n" . $herecurr) &&
27323705ce5bSJoe Perches			    $fix) {
2733194f66fcSJoe Perches				$fixed[$fixlinenr] =~
2734f27c95dbSJoe Perches				    s/(\(\s*$Type\s*\))[ \t]+/$1/;
27353705ce5bSJoe Perches			}
2736aad4f614SJoe Perches		}
2737aad4f614SJoe Perches
273805880600SJoe Perches		if ($realfile =~ m@^(drivers/net/|net/)@ &&
2739fdb4bcd6SJoe Perches		    $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
274085ad978cSJoe Perches		    $rawline =~ /^\+[ \t]*\*/ &&
274185ad978cSJoe Perches		    $realline > 2) {
274205880600SJoe Perches			WARN("NETWORKING_BLOCK_COMMENT_STYLE",
274305880600SJoe Perches			     "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
274405880600SJoe Perches		}
274505880600SJoe Perches
274605880600SJoe Perches		if ($realfile =~ m@^(drivers/net/|net/)@ &&
2747a605e32eSJoe Perches		    $prevrawline =~ /^\+[ \t]*\/\*/ &&		#starting /*
2748a605e32eSJoe Perches		    $prevrawline !~ /\*\/[ \t]*$/ &&		#no trailing */
274961135e96SJoe Perches		    $rawline =~ /^\+/ &&			#line is new
2750a605e32eSJoe Perches		    $rawline !~ /^\+[ \t]*\*/) {		#no leading *
2751a605e32eSJoe Perches			WARN("NETWORKING_BLOCK_COMMENT_STYLE",
2752a605e32eSJoe Perches			     "networking block comments start with * on subsequent lines\n" . $hereprev);
2753a605e32eSJoe Perches		}
2754a605e32eSJoe Perches
2755a605e32eSJoe Perches		if ($realfile =~ m@^(drivers/net/|net/)@ &&
2756c24f9f19SJoe Perches		    $rawline !~ m@^\+[ \t]*\*/[ \t]*$@ &&	#trailing */
2757c24f9f19SJoe Perches		    $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ &&	#inline /*...*/
2758c24f9f19SJoe Perches		    $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ &&	#trailing **/
2759c24f9f19SJoe Perches		    $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) {	#non blank */
276005880600SJoe Perches			WARN("NETWORKING_BLOCK_COMMENT_STYLE",
276105880600SJoe Perches			     "networking block comments put the trailing */ on a separate line\n" . $herecurr);
276205880600SJoe Perches		}
276305880600SJoe Perches
27647f619191SJoe Perches# check for missing blank lines after struct/union declarations
27657f619191SJoe Perches# with exceptions for various attributes and macros
27667f619191SJoe Perches		if ($prevline =~ /^[\+ ]};?\s*$/ &&
27677f619191SJoe Perches		    $line =~ /^\+/ &&
27687f619191SJoe Perches		    !($line =~ /^\+\s*$/ ||
27697f619191SJoe Perches		      $line =~ /^\+\s*EXPORT_SYMBOL/ ||
27707f619191SJoe Perches		      $line =~ /^\+\s*MODULE_/i ||
27717f619191SJoe Perches		      $line =~ /^\+\s*\#\s*(?:end|elif|else)/ ||
27727f619191SJoe Perches		      $line =~ /^\+[a-z_]*init/ ||
27737f619191SJoe Perches		      $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ ||
27747f619191SJoe Perches		      $line =~ /^\+\s*DECLARE/ ||
27757f619191SJoe Perches		      $line =~ /^\+\s*__setup/)) {
2776d752fcc8SJoe Perches			if (CHK("LINE_SPACING",
2777d752fcc8SJoe Perches				"Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) &&
2778d752fcc8SJoe Perches			    $fix) {
2779f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, "\+");
2780d752fcc8SJoe Perches			}
27817f619191SJoe Perches		}
27827f619191SJoe Perches
2783365dd4eaSJoe Perches# check for multiple consecutive blank lines
2784365dd4eaSJoe Perches		if ($prevline =~ /^[\+ ]\s*$/ &&
2785365dd4eaSJoe Perches		    $line =~ /^\+\s*$/ &&
2786365dd4eaSJoe Perches		    $last_blank_line != ($linenr - 1)) {
2787d752fcc8SJoe Perches			if (CHK("LINE_SPACING",
2788d752fcc8SJoe Perches				"Please don't use multiple blank lines\n" . $hereprev) &&
2789d752fcc8SJoe Perches			    $fix) {
2790f2d7e4d4SJoe Perches				fix_delete_line($fixlinenr, $rawline);
2791d752fcc8SJoe Perches			}
2792d752fcc8SJoe Perches
2793365dd4eaSJoe Perches			$last_blank_line = $linenr;
2794365dd4eaSJoe Perches		}
2795365dd4eaSJoe Perches
27963b617e3bSJoe Perches# check for missing blank lines after declarations
27973f7bac03SJoe Perches		if ($sline =~ /^\+\s+\S/ &&			#Not at char 1
27983f7bac03SJoe Perches			# actual declarations
27993f7bac03SJoe Perches		    ($prevline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
28005a4e1fd3SJoe Perches			# function pointer declarations
28015a4e1fd3SJoe Perches		     $prevline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
28023f7bac03SJoe Perches			# foo bar; where foo is some local typedef or #define
28033f7bac03SJoe Perches		     $prevline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
28043f7bac03SJoe Perches			# known declaration macros
28053f7bac03SJoe Perches		     $prevline =~ /^\+\s+$declaration_macros/) &&
28063f7bac03SJoe Perches			# for "else if" which can look like "$Ident $Ident"
28073f7bac03SJoe Perches		    !($prevline =~ /^\+\s+$c90_Keywords\b/ ||
28083f7bac03SJoe Perches			# other possible extensions of declaration lines
28093f7bac03SJoe Perches		      $prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ ||
28103f7bac03SJoe Perches			# not starting a section or a macro "\" extended line
28113f7bac03SJoe Perches		      $prevline =~ /(?:\{\s*|\\)$/) &&
28123f7bac03SJoe Perches			# looks like a declaration
28133f7bac03SJoe Perches		    !($sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
28145a4e1fd3SJoe Perches			# function pointer declarations
28155a4e1fd3SJoe Perches		      $sline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
28163f7bac03SJoe Perches			# foo bar; where foo is some local typedef or #define
28173f7bac03SJoe Perches		      $sline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
28183f7bac03SJoe Perches			# known declaration macros
28193f7bac03SJoe Perches		      $sline =~ /^\+\s+$declaration_macros/ ||
28203f7bac03SJoe Perches			# start of struct or union or enum
28213b617e3bSJoe Perches		      $sline =~ /^\+\s+(?:union|struct|enum|typedef)\b/ ||
28223f7bac03SJoe Perches			# start or end of block or continuation of declaration
28233f7bac03SJoe Perches		      $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ ||
28243f7bac03SJoe Perches			# bitfield continuation
28253f7bac03SJoe Perches		      $sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ ||
28263f7bac03SJoe Perches			# other possible extensions of declaration lines
28273f7bac03SJoe Perches		      $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) &&
28283f7bac03SJoe Perches			# indentation of previous and current line are the same
28293f7bac03SJoe Perches		    (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) {
2830d752fcc8SJoe Perches			if (WARN("LINE_SPACING",
2831d752fcc8SJoe Perches				 "Missing a blank line after declarations\n" . $hereprev) &&
2832d752fcc8SJoe Perches			    $fix) {
2833f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, "\+");
2834d752fcc8SJoe Perches			}
28353b617e3bSJoe Perches		}
28363b617e3bSJoe Perches
28375f7ddae6SRaffaele Recalcati# check for spaces at the beginning of a line.
28386b4c5bebSAndy Whitcroft# Exceptions:
28396b4c5bebSAndy Whitcroft#  1) within comments
28406b4c5bebSAndy Whitcroft#  2) indented preprocessor commands
28416b4c5bebSAndy Whitcroft#  3) hanging labels
28423705ce5bSJoe Perches		if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/)  {
28435f7ddae6SRaffaele Recalcati			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
28443705ce5bSJoe Perches			if (WARN("LEADING_SPACE",
28453705ce5bSJoe Perches				 "please, no spaces at the start of a line\n" . $herevet) &&
28463705ce5bSJoe Perches			    $fix) {
2847194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
28483705ce5bSJoe Perches			}
28495f7ddae6SRaffaele Recalcati		}
28505f7ddae6SRaffaele Recalcati
2851b9ea10d6SAndy Whitcroft# check we are in a valid C source file if not then ignore this hunk
2852b9ea10d6SAndy Whitcroft		next if ($realfile !~ /\.(h|c)$/);
2853b9ea10d6SAndy Whitcroft
2854032a4c0fSJoe Perches# check indentation of any line with a bare else
2855840080a0SJoe Perches# (but not if it is a multiple line "if (foo) return bar; else return baz;")
2856032a4c0fSJoe Perches# if the previous line is a break or return and is indented 1 tab more...
2857032a4c0fSJoe Perches		if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {
2858032a4c0fSJoe Perches			my $tabs = length($1) + 1;
2859840080a0SJoe Perches			if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ ||
2860840080a0SJoe Perches			    ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ &&
2861840080a0SJoe Perches			     defined $lines[$linenr] &&
2862840080a0SJoe Perches			     $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) {
2863032a4c0fSJoe Perches				WARN("UNNECESSARY_ELSE",
2864032a4c0fSJoe Perches				     "else is not generally useful after a break or return\n" . $hereprev);
2865032a4c0fSJoe Perches			}
2866032a4c0fSJoe Perches		}
2867032a4c0fSJoe Perches
2868c00df19aSJoe Perches# check indentation of a line with a break;
2869c00df19aSJoe Perches# if the previous line is a goto or return and is indented the same # of tabs
2870c00df19aSJoe Perches		if ($sline =~ /^\+([\t]+)break\s*;\s*$/) {
2871c00df19aSJoe Perches			my $tabs = $1;
2872c00df19aSJoe Perches			if ($prevline =~ /^\+$tabs(?:goto|return)\b/) {
2873c00df19aSJoe Perches				WARN("UNNECESSARY_BREAK",
2874c00df19aSJoe Perches				     "break is not useful after a goto or return\n" . $hereprev);
2875c00df19aSJoe Perches			}
2876c00df19aSJoe Perches		}
2877c00df19aSJoe Perches
28781ba8dfd1SKees Cook# discourage the addition of CONFIG_EXPERIMENTAL in #if(def).
28791ba8dfd1SKees Cook		if ($line =~ /^\+\s*\#\s*if.*\bCONFIG_EXPERIMENTAL\b/) {
28801ba8dfd1SKees Cook			WARN("CONFIG_EXPERIMENTAL",
28811ba8dfd1SKees Cook			     "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
28821ba8dfd1SKees Cook		}
28831ba8dfd1SKees Cook
2884c2fdda0dSAndy Whitcroft# check for RCS/CVS revision markers
2885cf655043SAndy Whitcroft		if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
2886000d1cc1SJoe Perches			WARN("CVS_KEYWORD",
2887000d1cc1SJoe Perches			     "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
2888c2fdda0dSAndy Whitcroft		}
288922f2a2efSAndy Whitcroft
289042e41c54SMike Frysinger# Blackfin: don't use __builtin_bfin_[cs]sync
289142e41c54SMike Frysinger		if ($line =~ /__builtin_bfin_csync/) {
289242e41c54SMike Frysinger			my $herevet = "$here\n" . cat_vet($line) . "\n";
2893000d1cc1SJoe Perches			ERROR("CSYNC",
2894000d1cc1SJoe Perches			      "use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
289542e41c54SMike Frysinger		}
289642e41c54SMike Frysinger		if ($line =~ /__builtin_bfin_ssync/) {
289742e41c54SMike Frysinger			my $herevet = "$here\n" . cat_vet($line) . "\n";
2898000d1cc1SJoe Perches			ERROR("SSYNC",
2899000d1cc1SJoe Perches			      "use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
290042e41c54SMike Frysinger		}
290142e41c54SMike Frysinger
290256e77d70SJoe Perches# check for old HOTPLUG __dev<foo> section markings
290356e77d70SJoe Perches		if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
290456e77d70SJoe Perches			WARN("HOTPLUG_SECTION",
290556e77d70SJoe Perches			     "Using $1 is unnecessary\n" . $herecurr);
290656e77d70SJoe Perches		}
290756e77d70SJoe Perches
29089c0ca6f9SAndy Whitcroft# Check for potential 'bare' types
29092b474a1aSAndy Whitcroft		my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
29102b474a1aSAndy Whitcroft		    $realline_next);
29113e469cdcSAndy Whitcroft#print "LINE<$line>\n";
29123e469cdcSAndy Whitcroft		if ($linenr >= $suppress_statement &&
29131b5539b1SJoe Perches		    $realcnt && $sline =~ /.\s*\S/) {
2914170d3a22SAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
2915f5fe35ddSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0);
2916171ae1a4SAndy Whitcroft			$stat =~ s/\n./\n /g;
2917171ae1a4SAndy Whitcroft			$cond =~ s/\n./\n /g;
2918171ae1a4SAndy Whitcroft
29193e469cdcSAndy Whitcroft#print "linenr<$linenr> <$stat>\n";
29203e469cdcSAndy Whitcroft			# If this statement has no statement boundaries within
29213e469cdcSAndy Whitcroft			# it there is no point in retrying a statement scan
29223e469cdcSAndy Whitcroft			# until we hit end of it.
29233e469cdcSAndy Whitcroft			my $frag = $stat; $frag =~ s/;+\s*$//;
29243e469cdcSAndy Whitcroft			if ($frag !~ /(?:{|;)/) {
29253e469cdcSAndy Whitcroft#print "skip<$line_nr_next>\n";
29263e469cdcSAndy Whitcroft				$suppress_statement = $line_nr_next;
29273e469cdcSAndy Whitcroft			}
2928f74bd194SAndy Whitcroft
29292b474a1aSAndy Whitcroft			# Find the real next line.
29302b474a1aSAndy Whitcroft			$realline_next = $line_nr_next;
29312b474a1aSAndy Whitcroft			if (defined $realline_next &&
29322b474a1aSAndy Whitcroft			    (!defined $lines[$realline_next - 1] ||
29332b474a1aSAndy Whitcroft			     substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
29342b474a1aSAndy Whitcroft				$realline_next++;
29352b474a1aSAndy Whitcroft			}
29362b474a1aSAndy Whitcroft
2937171ae1a4SAndy Whitcroft			my $s = $stat;
2938171ae1a4SAndy Whitcroft			$s =~ s/{.*$//s;
2939cf655043SAndy Whitcroft
2940c2fdda0dSAndy Whitcroft			# Ignore goto labels.
2941171ae1a4SAndy Whitcroft			if ($s =~ /$Ident:\*$/s) {
2942c2fdda0dSAndy Whitcroft
2943c2fdda0dSAndy Whitcroft			# Ignore functions being called
2944171ae1a4SAndy Whitcroft			} elsif ($s =~ /^.\s*$Ident\s*\(/s) {
2945c2fdda0dSAndy Whitcroft
2946463f2864SAndy Whitcroft			} elsif ($s =~ /^.\s*else\b/s) {
2947463f2864SAndy Whitcroft
2948c45dcabdSAndy Whitcroft			# declarations always start with types
2949d2506586SAndy 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) {
2950c45dcabdSAndy Whitcroft				my $type = $1;
2951c45dcabdSAndy Whitcroft				$type =~ s/\s+/ /g;
2952c45dcabdSAndy Whitcroft				possible($type, "A:" . $s);
2953c45dcabdSAndy Whitcroft
29546c72ffaaSAndy Whitcroft			# definitions in global scope can only start with types
2955a6a84062SAndy Whitcroft			} elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
2956c45dcabdSAndy Whitcroft				possible($1, "B:" . $s);
2957c2fdda0dSAndy Whitcroft			}
29588905a67cSAndy Whitcroft
29596c72ffaaSAndy Whitcroft			# any (foo ... *) is a pointer cast, and foo is a type
296065863862SAndy Whitcroft			while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
2961c45dcabdSAndy Whitcroft				possible($1, "C:" . $s);
29629c0ca6f9SAndy Whitcroft			}
29638905a67cSAndy Whitcroft
29648905a67cSAndy Whitcroft			# Check for any sort of function declaration.
29658905a67cSAndy Whitcroft			# int foo(something bar, other baz);
29668905a67cSAndy Whitcroft			# void (*store_gdt)(x86_descr_ptr *);
2967171ae1a4SAndy Whitcroft			if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
29688905a67cSAndy Whitcroft				my ($name_len) = length($1);
29698905a67cSAndy Whitcroft
2970cf655043SAndy Whitcroft				my $ctx = $s;
2971773647a0SAndy Whitcroft				substr($ctx, 0, $name_len + 1, '');
29728905a67cSAndy Whitcroft				$ctx =~ s/\)[^\)]*$//;
2973cf655043SAndy Whitcroft
29748905a67cSAndy Whitcroft				for my $arg (split(/\s*,\s*/, $ctx)) {
2975c45dcabdSAndy Whitcroft					if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
29768905a67cSAndy Whitcroft
2977c45dcabdSAndy Whitcroft						possible($1, "D:" . $s);
29788905a67cSAndy Whitcroft					}
29798905a67cSAndy Whitcroft				}
29808905a67cSAndy Whitcroft			}
29818905a67cSAndy Whitcroft
29829c0ca6f9SAndy Whitcroft		}
29839c0ca6f9SAndy Whitcroft
298400df344fSAndy Whitcroft#
298500df344fSAndy Whitcroft# Checks which may be anchored in the context.
298600df344fSAndy Whitcroft#
298700df344fSAndy Whitcroft
298800df344fSAndy Whitcroft# Check for switch () and associated case and default
298900df344fSAndy Whitcroft# statements should be at the same indent.
299000df344fSAndy Whitcroft		if ($line=~/\bswitch\s*\(.*\)/) {
299100df344fSAndy Whitcroft			my $err = '';
299200df344fSAndy Whitcroft			my $sep = '';
299300df344fSAndy Whitcroft			my @ctx = ctx_block_outer($linenr, $realcnt);
299400df344fSAndy Whitcroft			shift(@ctx);
299500df344fSAndy Whitcroft			for my $ctx (@ctx) {
299600df344fSAndy Whitcroft				my ($clen, $cindent) = line_stats($ctx);
299700df344fSAndy Whitcroft				if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
299800df344fSAndy Whitcroft							$indent != $cindent) {
299900df344fSAndy Whitcroft					$err .= "$sep$ctx\n";
300000df344fSAndy Whitcroft					$sep = '';
300100df344fSAndy Whitcroft				} else {
300200df344fSAndy Whitcroft					$sep = "[...]\n";
300300df344fSAndy Whitcroft				}
300400df344fSAndy Whitcroft			}
300500df344fSAndy Whitcroft			if ($err ne '') {
3006000d1cc1SJoe Perches				ERROR("SWITCH_CASE_INDENT_LEVEL",
3007000d1cc1SJoe Perches				      "switch and case should be at the same indent\n$hereline$err");
3008de7d4f0eSAndy Whitcroft			}
3009de7d4f0eSAndy Whitcroft		}
3010de7d4f0eSAndy Whitcroft
3011de7d4f0eSAndy Whitcroft# if/while/etc brace do not go on next line, unless defining a do while loop,
3012de7d4f0eSAndy Whitcroft# or if that brace on the next line is for something else
30130fe3dc2bSJoe Perches		if ($line =~ /(.*)\b((?:if|while|for|switch|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
3014773647a0SAndy Whitcroft			my $pre_ctx = "$1$2";
3015773647a0SAndy Whitcroft
30169c0ca6f9SAndy Whitcroft			my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
30178eef05ddSJoe Perches
30188eef05ddSJoe Perches			if ($line =~ /^\+\t{6,}/) {
30198eef05ddSJoe Perches				WARN("DEEP_INDENTATION",
30208eef05ddSJoe Perches				     "Too many leading tabs - consider code refactoring\n" . $herecurr);
30218eef05ddSJoe Perches			}
30228eef05ddSJoe Perches
3023de7d4f0eSAndy Whitcroft			my $ctx_cnt = $realcnt - $#ctx - 1;
3024de7d4f0eSAndy Whitcroft			my $ctx = join("\n", @ctx);
3025de7d4f0eSAndy Whitcroft
3026548596d5SAndy Whitcroft			my $ctx_ln = $linenr;
3027548596d5SAndy Whitcroft			my $ctx_skip = $realcnt;
3028de7d4f0eSAndy Whitcroft
3029548596d5SAndy Whitcroft			while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
3030548596d5SAndy Whitcroft					defined $lines[$ctx_ln - 1] &&
3031548596d5SAndy Whitcroft					$lines[$ctx_ln - 1] =~ /^-/)) {
3032548596d5SAndy Whitcroft				##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
3033548596d5SAndy Whitcroft				$ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
3034773647a0SAndy Whitcroft				$ctx_ln++;
3035773647a0SAndy Whitcroft			}
3036548596d5SAndy Whitcroft
303753210168SAndy Whitcroft			#print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
303853210168SAndy Whitcroft			#print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
3039773647a0SAndy Whitcroft
3040773647a0SAndy Whitcroft			if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
3041000d1cc1SJoe Perches				ERROR("OPEN_BRACE",
3042000d1cc1SJoe Perches				      "that open brace { should be on the previous line\n" .
304301464f30SAndy Whitcroft					"$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
304400df344fSAndy Whitcroft			}
3045773647a0SAndy Whitcroft			if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
3046773647a0SAndy Whitcroft			    $ctx =~ /\)\s*\;\s*$/ &&
3047773647a0SAndy Whitcroft			    defined $lines[$ctx_ln - 1])
3048773647a0SAndy Whitcroft			{
30499c0ca6f9SAndy Whitcroft				my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
30509c0ca6f9SAndy Whitcroft				if ($nindent > $indent) {
3051000d1cc1SJoe Perches					WARN("TRAILING_SEMICOLON",
3052000d1cc1SJoe Perches					     "trailing semicolon indicates no statements, indent implies otherwise\n" .
305301464f30SAndy Whitcroft						"$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
30549c0ca6f9SAndy Whitcroft				}
30559c0ca6f9SAndy Whitcroft			}
305600df344fSAndy Whitcroft		}
305700df344fSAndy Whitcroft
30584d001e4dSAndy Whitcroft# Check relative indent for conditionals and blocks.
30590fe3dc2bSJoe Perches		if ($line =~ /\b(?:(?:if|while|for|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
30603e469cdcSAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
30613e469cdcSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0)
30623e469cdcSAndy Whitcroft					if (!defined $stat);
30634d001e4dSAndy Whitcroft			my ($s, $c) = ($stat, $cond);
30644d001e4dSAndy Whitcroft
30654d001e4dSAndy Whitcroft			substr($s, 0, length($c), '');
30664d001e4dSAndy Whitcroft
30674d001e4dSAndy Whitcroft			# Make sure we remove the line prefixes as we have
30684d001e4dSAndy Whitcroft			# none on the first line, and are going to readd them
30694d001e4dSAndy Whitcroft			# where necessary.
30704d001e4dSAndy Whitcroft			$s =~ s/\n./\n/gs;
30714d001e4dSAndy Whitcroft
30724d001e4dSAndy Whitcroft			# Find out how long the conditional actually is.
30736f779c18SAndy Whitcroft			my @newlines = ($c =~ /\n/gs);
30746f779c18SAndy Whitcroft			my $cond_lines = 1 + $#newlines;
30754d001e4dSAndy Whitcroft
30764d001e4dSAndy Whitcroft			# We want to check the first line inside the block
30774d001e4dSAndy Whitcroft			# starting at the end of the conditional, so remove:
30784d001e4dSAndy Whitcroft			#  1) any blank line termination
30794d001e4dSAndy Whitcroft			#  2) any opening brace { on end of the line
30804d001e4dSAndy Whitcroft			#  3) any do (...) {
30814d001e4dSAndy Whitcroft			my $continuation = 0;
30824d001e4dSAndy Whitcroft			my $check = 0;
30834d001e4dSAndy Whitcroft			$s =~ s/^.*\bdo\b//;
30844d001e4dSAndy Whitcroft			$s =~ s/^\s*{//;
30854d001e4dSAndy Whitcroft			if ($s =~ s/^\s*\\//) {
30864d001e4dSAndy Whitcroft				$continuation = 1;
30874d001e4dSAndy Whitcroft			}
30889bd49efeSAndy Whitcroft			if ($s =~ s/^\s*?\n//) {
30894d001e4dSAndy Whitcroft				$check = 1;
30904d001e4dSAndy Whitcroft				$cond_lines++;
30914d001e4dSAndy Whitcroft			}
30924d001e4dSAndy Whitcroft
30934d001e4dSAndy Whitcroft			# Also ignore a loop construct at the end of a
30944d001e4dSAndy Whitcroft			# preprocessor statement.
30954d001e4dSAndy Whitcroft			if (($prevline =~ /^.\s*#\s*define\s/ ||
30964d001e4dSAndy Whitcroft			    $prevline =~ /\\\s*$/) && $continuation == 0) {
30974d001e4dSAndy Whitcroft				$check = 0;
30984d001e4dSAndy Whitcroft			}
30994d001e4dSAndy Whitcroft
31009bd49efeSAndy Whitcroft			my $cond_ptr = -1;
3101740504c6SAndy Whitcroft			$continuation = 0;
31029bd49efeSAndy Whitcroft			while ($cond_ptr != $cond_lines) {
31039bd49efeSAndy Whitcroft				$cond_ptr = $cond_lines;
31044d001e4dSAndy Whitcroft
3105f16fa28fSAndy Whitcroft				# If we see an #else/#elif then the code
3106f16fa28fSAndy Whitcroft				# is not linear.
3107f16fa28fSAndy Whitcroft				if ($s =~ /^\s*\#\s*(?:else|elif)/) {
3108f16fa28fSAndy Whitcroft					$check = 0;
3109f16fa28fSAndy Whitcroft				}
3110f16fa28fSAndy Whitcroft
31119bd49efeSAndy Whitcroft				# Ignore:
31129bd49efeSAndy Whitcroft				#  1) blank lines, they should be at 0,
31139bd49efeSAndy Whitcroft				#  2) preprocessor lines, and
31149bd49efeSAndy Whitcroft				#  3) labels.
3115740504c6SAndy Whitcroft				if ($continuation ||
3116740504c6SAndy Whitcroft				    $s =~ /^\s*?\n/ ||
31179bd49efeSAndy Whitcroft				    $s =~ /^\s*#\s*?/ ||
31189bd49efeSAndy Whitcroft				    $s =~ /^\s*$Ident\s*:/) {
3119740504c6SAndy Whitcroft					$continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
312030dad6ebSAndy Whitcroft					if ($s =~ s/^.*?\n//) {
31219bd49efeSAndy Whitcroft						$cond_lines++;
31229bd49efeSAndy Whitcroft					}
31234d001e4dSAndy Whitcroft				}
312430dad6ebSAndy Whitcroft			}
31254d001e4dSAndy Whitcroft
31264d001e4dSAndy Whitcroft			my (undef, $sindent) = line_stats("+" . $s);
31274d001e4dSAndy Whitcroft			my $stat_real = raw_line($linenr, $cond_lines);
31284d001e4dSAndy Whitcroft
31294d001e4dSAndy Whitcroft			# Check if either of these lines are modified, else
31304d001e4dSAndy Whitcroft			# this is not this patch's fault.
31314d001e4dSAndy Whitcroft			if (!defined($stat_real) ||
31324d001e4dSAndy Whitcroft			    $stat !~ /^\+/ && $stat_real !~ /^\+/) {
31334d001e4dSAndy Whitcroft				$check = 0;
31344d001e4dSAndy Whitcroft			}
31354d001e4dSAndy Whitcroft			if (defined($stat_real) && $cond_lines > 1) {
31364d001e4dSAndy Whitcroft				$stat_real = "[...]\n$stat_real";
31374d001e4dSAndy Whitcroft			}
31384d001e4dSAndy Whitcroft
31399bd49efeSAndy 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";
31404d001e4dSAndy Whitcroft
31414d001e4dSAndy Whitcroft			if ($check && (($sindent % 8) != 0 ||
31424d001e4dSAndy Whitcroft			    ($sindent <= $indent && $s ne ''))) {
3143000d1cc1SJoe Perches				WARN("SUSPECT_CODE_INDENT",
3144000d1cc1SJoe Perches				     "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
31454d001e4dSAndy Whitcroft			}
31464d001e4dSAndy Whitcroft		}
31474d001e4dSAndy Whitcroft
31486c72ffaaSAndy Whitcroft		# Track the 'values' across context and added lines.
31496c72ffaaSAndy Whitcroft		my $opline = $line; $opline =~ s/^./ /;
31501f65f947SAndy Whitcroft		my ($curr_values, $curr_vars) =
31511f65f947SAndy Whitcroft				annotate_values($opline . "\n", $prev_values);
31526c72ffaaSAndy Whitcroft		$curr_values = $prev_values . $curr_values;
3153c2fdda0dSAndy Whitcroft		if ($dbg_values) {
3154c2fdda0dSAndy Whitcroft			my $outline = $opline; $outline =~ s/\t/ /g;
3155cf655043SAndy Whitcroft			print "$linenr > .$outline\n";
3156cf655043SAndy Whitcroft			print "$linenr > $curr_values\n";
31571f65f947SAndy Whitcroft			print "$linenr >  $curr_vars\n";
3158c2fdda0dSAndy Whitcroft		}
31596c72ffaaSAndy Whitcroft		$prev_values = substr($curr_values, -1);
31606c72ffaaSAndy Whitcroft
316100df344fSAndy Whitcroft#ignore lines not being added
31623705ce5bSJoe Perches		next if ($line =~ /^[^\+]/);
316300df344fSAndy Whitcroft
3164653d4876SAndy Whitcroft# TEST: allow direct testing of the type matcher.
31657429c690SAndy Whitcroft		if ($dbg_type) {
31667429c690SAndy Whitcroft			if ($line =~ /^.\s*$Declare\s*$/) {
3167000d1cc1SJoe Perches				ERROR("TEST_TYPE",
3168000d1cc1SJoe Perches				      "TEST: is type\n" . $herecurr);
31697429c690SAndy Whitcroft			} elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
3170000d1cc1SJoe Perches				ERROR("TEST_NOT_TYPE",
3171000d1cc1SJoe Perches				      "TEST: is not type ($1 is)\n". $herecurr);
31727429c690SAndy Whitcroft			}
3173653d4876SAndy Whitcroft			next;
3174653d4876SAndy Whitcroft		}
3175a1ef277eSAndy Whitcroft# TEST: allow direct testing of the attribute matcher.
3176a1ef277eSAndy Whitcroft		if ($dbg_attr) {
31779360b0e5SAndy Whitcroft			if ($line =~ /^.\s*$Modifier\s*$/) {
3178000d1cc1SJoe Perches				ERROR("TEST_ATTR",
3179000d1cc1SJoe Perches				      "TEST: is attr\n" . $herecurr);
31809360b0e5SAndy Whitcroft			} elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
3181000d1cc1SJoe Perches				ERROR("TEST_NOT_ATTR",
3182000d1cc1SJoe Perches				      "TEST: is not attr ($1 is)\n". $herecurr);
3183a1ef277eSAndy Whitcroft			}
3184a1ef277eSAndy Whitcroft			next;
3185a1ef277eSAndy Whitcroft		}
3186653d4876SAndy Whitcroft
3187f0a594c1SAndy Whitcroft# check for initialisation to aggregates open brace on the next line
318899423c20SAndy Whitcroft		if ($line =~ /^.\s*{/ &&
318999423c20SAndy Whitcroft		    $prevline =~ /(?:^|[^=])=\s*$/) {
3190d752fcc8SJoe Perches			if (ERROR("OPEN_BRACE",
3191d752fcc8SJoe Perches				  "that open brace { should be on the previous line\n" . $hereprev) &&
3192f2d7e4d4SJoe Perches			    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
3193f2d7e4d4SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
3194f2d7e4d4SJoe Perches				fix_delete_line($fixlinenr, $rawline);
3195d752fcc8SJoe Perches				my $fixedline = $prevrawline;
3196d752fcc8SJoe Perches				$fixedline =~ s/\s*=\s*$/ = {/;
3197f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
3198d752fcc8SJoe Perches				$fixedline = $line;
3199d752fcc8SJoe Perches				$fixedline =~ s/^(.\s*){\s*/$1/;
3200f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
3201d752fcc8SJoe Perches			}
3202f0a594c1SAndy Whitcroft		}
3203f0a594c1SAndy Whitcroft
320400df344fSAndy Whitcroft#
320500df344fSAndy Whitcroft# Checks which are anchored on the added line.
320600df344fSAndy Whitcroft#
320700df344fSAndy Whitcroft
3208653d4876SAndy Whitcroft# check for malformed paths in #include statements (uses RAW line)
3209c45dcabdSAndy Whitcroft		if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
3210653d4876SAndy Whitcroft			my $path = $1;
3211653d4876SAndy Whitcroft			if ($path =~ m{//}) {
3212000d1cc1SJoe Perches				ERROR("MALFORMED_INCLUDE",
3213495e9d84SJoe Perches				      "malformed #include filename\n" . $herecurr);
3214495e9d84SJoe Perches			}
3215495e9d84SJoe Perches			if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
3216495e9d84SJoe Perches				ERROR("UAPI_INCLUDE",
3217495e9d84SJoe Perches				      "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
3218653d4876SAndy Whitcroft			}
3219653d4876SAndy Whitcroft		}
3220653d4876SAndy Whitcroft
322100df344fSAndy Whitcroft# no C99 // comments
322200df344fSAndy Whitcroft		if ($line =~ m{//}) {
32233705ce5bSJoe Perches			if (ERROR("C99_COMMENTS",
32243705ce5bSJoe Perches				  "do not use C99 // comments\n" . $herecurr) &&
32253705ce5bSJoe Perches			    $fix) {
3226194f66fcSJoe Perches				my $line = $fixed[$fixlinenr];
32273705ce5bSJoe Perches				if ($line =~ /\/\/(.*)$/) {
32283705ce5bSJoe Perches					my $comment = trim($1);
3229194f66fcSJoe Perches					$fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@;
32303705ce5bSJoe Perches				}
32313705ce5bSJoe Perches			}
323200df344fSAndy Whitcroft		}
323300df344fSAndy Whitcroft		# Remove C99 comments.
32340a920b5bSAndy Whitcroft		$line =~ s@//.*@@;
32356c72ffaaSAndy Whitcroft		$opline =~ s@//.*@@;
32360a920b5bSAndy Whitcroft
32372b474a1aSAndy Whitcroft# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
32382b474a1aSAndy Whitcroft# the whole statement.
32392b474a1aSAndy Whitcroft#print "APW <$lines[$realline_next - 1]>\n";
32402b474a1aSAndy Whitcroft		if (defined $realline_next &&
32412b474a1aSAndy Whitcroft		    exists $lines[$realline_next - 1] &&
32422b474a1aSAndy Whitcroft		    !defined $suppress_export{$realline_next} &&
32432b474a1aSAndy Whitcroft		    ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
32442b474a1aSAndy Whitcroft		     $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
32453cbf62dfSAndy Whitcroft			# Handle definitions which produce identifiers with
32463cbf62dfSAndy Whitcroft			# a prefix:
32473cbf62dfSAndy Whitcroft			#   XXX(foo);
32483cbf62dfSAndy Whitcroft			#   EXPORT_SYMBOL(something_foo);
3249653d4876SAndy Whitcroft			my $name = $1;
325087a53877SAndy Whitcroft			if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
32513cbf62dfSAndy Whitcroft			    $name =~ /^${Ident}_$2/) {
32523cbf62dfSAndy Whitcroft#print "FOO C name<$name>\n";
32533cbf62dfSAndy Whitcroft				$suppress_export{$realline_next} = 1;
32543cbf62dfSAndy Whitcroft
32553cbf62dfSAndy Whitcroft			} elsif ($stat !~ /(?:
32562b474a1aSAndy Whitcroft				\n.}\s*$|
325748012058SAndy Whitcroft				^.DEFINE_$Ident\(\Q$name\E\)|
325848012058SAndy Whitcroft				^.DECLARE_$Ident\(\Q$name\E\)|
325948012058SAndy Whitcroft				^.LIST_HEAD\(\Q$name\E\)|
32602b474a1aSAndy Whitcroft				^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
32612b474a1aSAndy Whitcroft				\b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
326248012058SAndy Whitcroft			    )/x) {
32632b474a1aSAndy Whitcroft#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
32642b474a1aSAndy Whitcroft				$suppress_export{$realline_next} = 2;
32652b474a1aSAndy Whitcroft			} else {
32662b474a1aSAndy Whitcroft				$suppress_export{$realline_next} = 1;
32670a920b5bSAndy Whitcroft			}
32680a920b5bSAndy Whitcroft		}
32692b474a1aSAndy Whitcroft		if (!defined $suppress_export{$linenr} &&
32702b474a1aSAndy Whitcroft		    $prevline =~ /^.\s*$/ &&
32712b474a1aSAndy Whitcroft		    ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
32722b474a1aSAndy Whitcroft		     $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
32732b474a1aSAndy Whitcroft#print "FOO B <$lines[$linenr - 1]>\n";
32742b474a1aSAndy Whitcroft			$suppress_export{$linenr} = 2;
32752b474a1aSAndy Whitcroft		}
32762b474a1aSAndy Whitcroft		if (defined $suppress_export{$linenr} &&
32772b474a1aSAndy Whitcroft		    $suppress_export{$linenr} == 2) {
3278000d1cc1SJoe Perches			WARN("EXPORT_SYMBOL",
3279000d1cc1SJoe Perches			     "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
32802b474a1aSAndy Whitcroft		}
32810a920b5bSAndy Whitcroft
32825150bda4SJoe Eloff# check for global initialisers.
32835129e87cSJoe Perches		if ($line =~ /^\+$Type\s*$Ident(?:\s+$Modifier)*\s*=\s*(?:0|NULL|false)\s*;/) {
3284d5e616fcSJoe Perches			if (ERROR("GLOBAL_INITIALISERS",
3285000d1cc1SJoe Perches				  "do not initialise globals to 0 or NULL\n" .
3286d5e616fcSJoe Perches				      $herecurr) &&
3287d5e616fcSJoe Perches			    $fix) {
32885129e87cSJoe Perches				$fixed[$fixlinenr] =~ s/(^.$Type\s*$Ident(?:\s+$Modifier)*)\s*=\s*(0|NULL|false)\s*;/$1;/;
3289d5e616fcSJoe Perches			}
3290f0a594c1SAndy Whitcroft		}
32910a920b5bSAndy Whitcroft# check for static initialisers.
3292d5e616fcSJoe Perches		if ($line =~ /^\+.*\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
3293d5e616fcSJoe Perches			if (ERROR("INITIALISED_STATIC",
3294000d1cc1SJoe Perches				  "do not initialise statics to 0 or NULL\n" .
3295d5e616fcSJoe Perches				      $herecurr) &&
3296d5e616fcSJoe Perches			    $fix) {
3297194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*(0|NULL|false)\s*;/$1;/;
3298d5e616fcSJoe Perches			}
32990a920b5bSAndy Whitcroft		}
33000a920b5bSAndy Whitcroft
33011813087dSJoe Perches# check for misordered declarations of char/short/int/long with signed/unsigned
33021813087dSJoe Perches		while ($sline =~ m{(\b$TypeMisordered\b)}g) {
33031813087dSJoe Perches			my $tmp = trim($1);
33041813087dSJoe Perches			WARN("MISORDERED_TYPE",
33051813087dSJoe Perches			     "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr);
33061813087dSJoe Perches		}
33071813087dSJoe Perches
3308cb710ecaSJoe Perches# check for static const char * arrays.
3309cb710ecaSJoe Perches		if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
3310000d1cc1SJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
3311000d1cc1SJoe Perches			     "static const char * array should probably be static const char * const\n" .
3312cb710ecaSJoe Perches				$herecurr);
3313cb710ecaSJoe Perches               }
3314cb710ecaSJoe Perches
3315cb710ecaSJoe Perches# check for static char foo[] = "bar" declarations.
3316cb710ecaSJoe Perches		if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
3317000d1cc1SJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
3318000d1cc1SJoe Perches			     "static char array declaration should probably be static const char\n" .
3319cb710ecaSJoe Perches				$herecurr);
3320cb710ecaSJoe Perches               }
3321cb710ecaSJoe Perches
3322ab7e23f3SJoe Perches# check for const <foo> const where <foo> is not a pointer or array type
3323ab7e23f3SJoe Perches		if ($sline =~ /\bconst\s+($BasicType)\s+const\b/) {
3324ab7e23f3SJoe Perches			my $found = $1;
3325ab7e23f3SJoe Perches			if ($sline =~ /\bconst\s+\Q$found\E\s+const\b\s*\*/) {
3326ab7e23f3SJoe Perches				WARN("CONST_CONST",
3327ab7e23f3SJoe Perches				     "'const $found const *' should probably be 'const $found * const'\n" . $herecurr);
3328ab7e23f3SJoe Perches			} elsif ($sline !~ /\bconst\s+\Q$found\E\s+const\s+\w+\s*\[/) {
3329ab7e23f3SJoe Perches				WARN("CONST_CONST",
3330ab7e23f3SJoe Perches				     "'const $found const' should probably be 'const $found'\n" . $herecurr);
3331ab7e23f3SJoe Perches			}
3332ab7e23f3SJoe Perches		}
3333ab7e23f3SJoe Perches
33349b0fa60dSJoe Perches# check for non-global char *foo[] = {"bar", ...} declarations.
33359b0fa60dSJoe Perches		if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) {
33369b0fa60dSJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
33379b0fa60dSJoe Perches			     "char * array declaration might be better as static const\n" .
33389b0fa60dSJoe Perches				$herecurr);
33399b0fa60dSJoe Perches               }
33409b0fa60dSJoe Perches
3341b598b670SJoe Perches# check for sizeof(foo)/sizeof(foo[0]) that could be ARRAY_SIZE(foo)
3342b598b670SJoe Perches		if ($line =~ m@\bsizeof\s*\(\s*($Lval)\s*\)@) {
3343b598b670SJoe Perches			my $array = $1;
3344b598b670SJoe 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*\))@) {
3345b598b670SJoe Perches				my $array_div = $1;
3346b598b670SJoe Perches				if (WARN("ARRAY_SIZE",
3347b598b670SJoe Perches					 "Prefer ARRAY_SIZE($array)\n" . $herecurr) &&
3348b598b670SJoe Perches				    $fix) {
3349b598b670SJoe Perches					$fixed[$fixlinenr] =~ s/\Q$array_div\E/ARRAY_SIZE($array)/;
3350b598b670SJoe Perches				}
3351b598b670SJoe Perches			}
3352b598b670SJoe Perches		}
3353b598b670SJoe Perches
3354b36190c5SJoe Perches# check for function declarations without arguments like "int foo()"
3355b36190c5SJoe Perches		if ($line =~ /(\b$Type\s+$Ident)\s*\(\s*\)/) {
3356b36190c5SJoe Perches			if (ERROR("FUNCTION_WITHOUT_ARGS",
3357b36190c5SJoe Perches				  "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) &&
3358b36190c5SJoe Perches			    $fix) {
3359194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/;
3360b36190c5SJoe Perches			}
3361b36190c5SJoe Perches		}
3362b36190c5SJoe Perches
336392e112fdSJoe Perches# check for uses of DEFINE_PCI_DEVICE_TABLE
336492e112fdSJoe Perches		if ($line =~ /\bDEFINE_PCI_DEVICE_TABLE\s*\(\s*(\w+)\s*\)\s*=/) {
336592e112fdSJoe Perches			if (WARN("DEFINE_PCI_DEVICE_TABLE",
336692e112fdSJoe Perches				 "Prefer struct pci_device_id over deprecated DEFINE_PCI_DEVICE_TABLE\n" . $herecurr) &&
336792e112fdSJoe Perches			    $fix) {
3368194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b(?:static\s+|)DEFINE_PCI_DEVICE_TABLE\s*\(\s*(\w+)\s*\)\s*=\s*/static const struct pci_device_id $1\[\] = /;
336992e112fdSJoe Perches			}
337093ed0e2dSJoe Perches		}
337193ed0e2dSJoe Perches
3372653d4876SAndy Whitcroft# check for new typedefs, only function parameters and sparse annotations
3373653d4876SAndy Whitcroft# make sense.
3374653d4876SAndy Whitcroft		if ($line =~ /\btypedef\s/ &&
33758054576dSAndy Whitcroft		    $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
3376c45dcabdSAndy Whitcroft		    $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
33778ed22cadSAndy Whitcroft		    $line !~ /\b$typeTypedefs\b/ &&
3378653d4876SAndy Whitcroft		    $line !~ /\b__bitwise(?:__|)\b/) {
3379000d1cc1SJoe Perches			WARN("NEW_TYPEDEFS",
3380000d1cc1SJoe Perches			     "do not add new typedefs\n" . $herecurr);
33810a920b5bSAndy Whitcroft		}
33820a920b5bSAndy Whitcroft
33830a920b5bSAndy Whitcroft# * goes on variable not on type
338465863862SAndy Whitcroft		# (char*[ const])
3385bfcb2cc7SAndy Whitcroft		while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
3386bfcb2cc7SAndy Whitcroft			#print "AA<$1>\n";
33873705ce5bSJoe Perches			my ($ident, $from, $to) = ($1, $2, $2);
3388d8aaf121SAndy Whitcroft
338965863862SAndy Whitcroft			# Should start with a space.
339065863862SAndy Whitcroft			$to =~ s/^(\S)/ $1/;
339165863862SAndy Whitcroft			# Should not end with a space.
339265863862SAndy Whitcroft			$to =~ s/\s+$//;
339365863862SAndy Whitcroft			# '*'s should not have spaces between.
3394f9a0b3d1SAndy Whitcroft			while ($to =~ s/\*\s+\*/\*\*/) {
339565863862SAndy Whitcroft			}
3396d8aaf121SAndy Whitcroft
33973705ce5bSJoe Perches##			print "1: from<$from> to<$to> ident<$ident>\n";
339865863862SAndy Whitcroft			if ($from ne $to) {
33993705ce5bSJoe Perches				if (ERROR("POINTER_LOCATION",
34003705ce5bSJoe Perches					  "\"(foo$from)\" should be \"(foo$to)\"\n" .  $herecurr) &&
34013705ce5bSJoe Perches				    $fix) {
34023705ce5bSJoe Perches					my $sub_from = $ident;
34033705ce5bSJoe Perches					my $sub_to = $ident;
34043705ce5bSJoe Perches					$sub_to =~ s/\Q$from\E/$to/;
3405194f66fcSJoe Perches					$fixed[$fixlinenr] =~
34063705ce5bSJoe Perches					    s@\Q$sub_from\E@$sub_to@;
34073705ce5bSJoe Perches				}
340865863862SAndy Whitcroft			}
3409bfcb2cc7SAndy Whitcroft		}
3410bfcb2cc7SAndy Whitcroft		while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
3411bfcb2cc7SAndy Whitcroft			#print "BB<$1>\n";
34123705ce5bSJoe Perches			my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
3413d8aaf121SAndy Whitcroft
341465863862SAndy Whitcroft			# Should start with a space.
341565863862SAndy Whitcroft			$to =~ s/^(\S)/ $1/;
341665863862SAndy Whitcroft			# Should not end with a space.
341765863862SAndy Whitcroft			$to =~ s/\s+$//;
341865863862SAndy Whitcroft			# '*'s should not have spaces between.
3419f9a0b3d1SAndy Whitcroft			while ($to =~ s/\*\s+\*/\*\*/) {
342065863862SAndy Whitcroft			}
342165863862SAndy Whitcroft			# Modifiers should have spaces.
342265863862SAndy Whitcroft			$to =~ s/(\b$Modifier$)/$1 /;
342365863862SAndy Whitcroft
34243705ce5bSJoe Perches##			print "2: from<$from> to<$to> ident<$ident>\n";
3425667026e7SAndy Whitcroft			if ($from ne $to && $ident !~ /^$Modifier$/) {
34263705ce5bSJoe Perches				if (ERROR("POINTER_LOCATION",
34273705ce5bSJoe Perches					  "\"foo${from}bar\" should be \"foo${to}bar\"\n" .  $herecurr) &&
34283705ce5bSJoe Perches				    $fix) {
34293705ce5bSJoe Perches
34303705ce5bSJoe Perches					my $sub_from = $match;
34313705ce5bSJoe Perches					my $sub_to = $match;
34323705ce5bSJoe Perches					$sub_to =~ s/\Q$from\E/$to/;
3433194f66fcSJoe Perches					$fixed[$fixlinenr] =~
34343705ce5bSJoe Perches					    s@\Q$sub_from\E@$sub_to@;
34353705ce5bSJoe Perches				}
343665863862SAndy Whitcroft			}
34370a920b5bSAndy Whitcroft		}
34380a920b5bSAndy Whitcroft
34390a920b5bSAndy Whitcroft# # no BUG() or BUG_ON()
34400a920b5bSAndy Whitcroft# 		if ($line =~ /\b(BUG|BUG_ON)\b/) {
34410a920b5bSAndy Whitcroft# 			print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
34420a920b5bSAndy Whitcroft# 			print "$herecurr";
34430a920b5bSAndy Whitcroft# 			$clean = 0;
34440a920b5bSAndy Whitcroft# 		}
34450a920b5bSAndy Whitcroft
34468905a67cSAndy Whitcroft		if ($line =~ /\bLINUX_VERSION_CODE\b/) {
3447000d1cc1SJoe Perches			WARN("LINUX_VERSION_CODE",
3448000d1cc1SJoe Perches			     "LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
34498905a67cSAndy Whitcroft		}
34508905a67cSAndy Whitcroft
345117441227SJoe Perches# check for uses of printk_ratelimit
345217441227SJoe Perches		if ($line =~ /\bprintk_ratelimit\s*\(/) {
3453000d1cc1SJoe Perches			WARN("PRINTK_RATELIMITED",
3454000d1cc1SJoe Perches			     "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
345517441227SJoe Perches		}
345617441227SJoe Perches
345700df344fSAndy Whitcroft# printk should use KERN_* levels.  Note that follow on printk's on the
345800df344fSAndy Whitcroft# same line do not need a level, so we use the current block context
345900df344fSAndy Whitcroft# to try and find and validate the current printk.  In summary the current
346025985edcSLucas De Marchi# printk includes all preceding printk's which have no newline on the end.
346100df344fSAndy Whitcroft# we assume the first bad printk is the one to report.
3462f0a594c1SAndy Whitcroft		if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
346300df344fSAndy Whitcroft			my $ok = 0;
346400df344fSAndy Whitcroft			for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
346500df344fSAndy Whitcroft				#print "CHECK<$lines[$ln - 1]\n";
346625985edcSLucas De Marchi				# we have a preceding printk if it ends
346700df344fSAndy Whitcroft				# with "\n" ignore it, else it is to blame
346800df344fSAndy Whitcroft				if ($lines[$ln - 1] =~ m{\bprintk\(}) {
346900df344fSAndy Whitcroft					if ($rawlines[$ln - 1] !~ m{\\n"}) {
347000df344fSAndy Whitcroft						$ok = 1;
347100df344fSAndy Whitcroft					}
347200df344fSAndy Whitcroft					last;
347300df344fSAndy Whitcroft				}
347400df344fSAndy Whitcroft			}
347500df344fSAndy Whitcroft			if ($ok == 0) {
3476000d1cc1SJoe Perches				WARN("PRINTK_WITHOUT_KERN_LEVEL",
3477000d1cc1SJoe Perches				     "printk() should include KERN_ facility level\n" . $herecurr);
34780a920b5bSAndy Whitcroft			}
347900df344fSAndy Whitcroft		}
34800a920b5bSAndy Whitcroft
3481243f3803SJoe Perches		if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
3482243f3803SJoe Perches			my $orig = $1;
3483243f3803SJoe Perches			my $level = lc($orig);
3484243f3803SJoe Perches			$level = "warn" if ($level eq "warning");
34858f26b837SJoe Perches			my $level2 = $level;
34868f26b837SJoe Perches			$level2 = "dbg" if ($level eq "debug");
3487243f3803SJoe Perches			WARN("PREFER_PR_LEVEL",
3488daa8b059SYogesh Chaudhari			     "Prefer [subsystem eg: netdev]_$level2([subsystem]dev, ... then dev_$level2(dev, ... then pr_$level(...  to printk(KERN_$orig ...\n" . $herecurr);
3489243f3803SJoe Perches		}
3490243f3803SJoe Perches
3491243f3803SJoe Perches		if ($line =~ /\bpr_warning\s*\(/) {
3492d5e616fcSJoe Perches			if (WARN("PREFER_PR_LEVEL",
3493d5e616fcSJoe Perches				 "Prefer pr_warn(... to pr_warning(...\n" . $herecurr) &&
3494d5e616fcSJoe Perches			    $fix) {
3495194f66fcSJoe Perches				$fixed[$fixlinenr] =~
3496d5e616fcSJoe Perches				    s/\bpr_warning\b/pr_warn/;
3497d5e616fcSJoe Perches			}
3498243f3803SJoe Perches		}
3499243f3803SJoe Perches
3500dc139313SJoe Perches		if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
3501dc139313SJoe Perches			my $orig = $1;
3502dc139313SJoe Perches			my $level = lc($orig);
3503dc139313SJoe Perches			$level = "warn" if ($level eq "warning");
3504dc139313SJoe Perches			$level = "dbg" if ($level eq "debug");
3505dc139313SJoe Perches			WARN("PREFER_DEV_LEVEL",
3506dc139313SJoe Perches			     "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
3507dc139313SJoe Perches		}
3508dc139313SJoe Perches
350991c9afafSAndy Lutomirski# ENOSYS means "bad syscall nr" and nothing else.  This will have a small
351091c9afafSAndy Lutomirski# number of false positives, but assembly files are not checked, so at
351191c9afafSAndy Lutomirski# least the arch entry code will not trigger this warning.
351291c9afafSAndy Lutomirski		if ($line =~ /\bENOSYS\b/) {
351391c9afafSAndy Lutomirski			WARN("ENOSYS",
351491c9afafSAndy Lutomirski			     "ENOSYS means 'invalid syscall nr' and nothing else\n" . $herecurr);
351591c9afafSAndy Lutomirski		}
351691c9afafSAndy Lutomirski
3517653d4876SAndy Whitcroft# function brace can't be on same line, except for #defines of do while,
3518653d4876SAndy Whitcroft# or if closed on same line
35198d182478SJoe Perches		if (($line=~/$Type\s*$Ident\(.*\).*\s*{/) and
3520c45dcabdSAndy Whitcroft		    !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) {
35218d182478SJoe Perches			if (ERROR("OPEN_BRACE",
35228d182478SJoe Perches				  "open brace '{' following function declarations go on the next line\n" . $herecurr) &&
35238d182478SJoe Perches			    $fix) {
35248d182478SJoe Perches				fix_delete_line($fixlinenr, $rawline);
35258d182478SJoe Perches				my $fixed_line = $rawline;
35268d182478SJoe Perches				$fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*){(.*)$/;
35278d182478SJoe Perches				my $line1 = $1;
35288d182478SJoe Perches				my $line2 = $2;
35298d182478SJoe Perches				fix_insert_line($fixlinenr, ltrim($line1));
35308d182478SJoe Perches				fix_insert_line($fixlinenr, "\+{");
35318d182478SJoe Perches				if ($line2 !~ /^\s*$/) {
35328d182478SJoe Perches					fix_insert_line($fixlinenr, "\+\t" . trim($line2));
35338d182478SJoe Perches				}
35348d182478SJoe Perches			}
35350a920b5bSAndy Whitcroft		}
3536653d4876SAndy Whitcroft
35378905a67cSAndy Whitcroft# open braces for enum, union and struct go on the same line.
35388905a67cSAndy Whitcroft		if ($line =~ /^.\s*{/ &&
35398905a67cSAndy Whitcroft		    $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
35408d182478SJoe Perches			if (ERROR("OPEN_BRACE",
35418d182478SJoe Perches				  "open brace '{' following $1 go on the same line\n" . $hereprev) &&
35428d182478SJoe Perches			    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
35438d182478SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
35448d182478SJoe Perches				fix_delete_line($fixlinenr, $rawline);
35458d182478SJoe Perches				my $fixedline = rtrim($prevrawline) . " {";
35468d182478SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
35478d182478SJoe Perches				$fixedline = $rawline;
35488d182478SJoe Perches				$fixedline =~ s/^(.\s*){\s*/$1\t/;
35498d182478SJoe Perches				if ($fixedline !~ /^\+\s*$/) {
35508d182478SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
35518d182478SJoe Perches				}
35528d182478SJoe Perches			}
35538905a67cSAndy Whitcroft		}
35548905a67cSAndy Whitcroft
35550c73b4ebSAndy Whitcroft# missing space after union, struct or enum definition
35563705ce5bSJoe Perches		if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
35573705ce5bSJoe Perches			if (WARN("SPACING",
35583705ce5bSJoe Perches				 "missing space after $1 definition\n" . $herecurr) &&
35593705ce5bSJoe Perches			    $fix) {
3560194f66fcSJoe Perches				$fixed[$fixlinenr] =~
35613705ce5bSJoe Perches				    s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
35623705ce5bSJoe Perches			}
35630c73b4ebSAndy Whitcroft		}
35640c73b4ebSAndy Whitcroft
356531070b5dSJoe Perches# Function pointer declarations
356631070b5dSJoe Perches# check spacing between type, funcptr, and args
356731070b5dSJoe Perches# canonical declaration is "type (*funcptr)(args...)"
356891f72e9cSJoe Perches		if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) {
356931070b5dSJoe Perches			my $declare = $1;
357031070b5dSJoe Perches			my $pre_pointer_space = $2;
357131070b5dSJoe Perches			my $post_pointer_space = $3;
357231070b5dSJoe Perches			my $funcname = $4;
357331070b5dSJoe Perches			my $post_funcname_space = $5;
357431070b5dSJoe Perches			my $pre_args_space = $6;
357531070b5dSJoe Perches
357691f72e9cSJoe Perches# the $Declare variable will capture all spaces after the type
357791f72e9cSJoe Perches# so check it for a missing trailing missing space but pointer return types
357891f72e9cSJoe Perches# don't need a space so don't warn for those.
357991f72e9cSJoe Perches			my $post_declare_space = "";
358091f72e9cSJoe Perches			if ($declare =~ /(\s+)$/) {
358191f72e9cSJoe Perches				$post_declare_space = $1;
358291f72e9cSJoe Perches				$declare = rtrim($declare);
358391f72e9cSJoe Perches			}
358491f72e9cSJoe Perches			if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) {
358531070b5dSJoe Perches				WARN("SPACING",
358631070b5dSJoe Perches				     "missing space after return type\n" . $herecurr);
358791f72e9cSJoe Perches				$post_declare_space = " ";
358831070b5dSJoe Perches			}
358931070b5dSJoe Perches
359031070b5dSJoe Perches# unnecessary space "type  (*funcptr)(args...)"
359191f72e9cSJoe Perches# This test is not currently implemented because these declarations are
359291f72e9cSJoe Perches# equivalent to
359391f72e9cSJoe Perches#	int  foo(int bar, ...)
359491f72e9cSJoe Perches# and this is form shouldn't/doesn't generate a checkpatch warning.
359591f72e9cSJoe Perches#
359691f72e9cSJoe Perches#			elsif ($declare =~ /\s{2,}$/) {
359791f72e9cSJoe Perches#				WARN("SPACING",
359891f72e9cSJoe Perches#				     "Multiple spaces after return type\n" . $herecurr);
359991f72e9cSJoe Perches#			}
360031070b5dSJoe Perches
360131070b5dSJoe Perches# unnecessary space "type ( *funcptr)(args...)"
360231070b5dSJoe Perches			if (defined $pre_pointer_space &&
360331070b5dSJoe Perches			    $pre_pointer_space =~ /^\s/) {
360431070b5dSJoe Perches				WARN("SPACING",
360531070b5dSJoe Perches				     "Unnecessary space after function pointer open parenthesis\n" . $herecurr);
360631070b5dSJoe Perches			}
360731070b5dSJoe Perches
360831070b5dSJoe Perches# unnecessary space "type (* funcptr)(args...)"
360931070b5dSJoe Perches			if (defined $post_pointer_space &&
361031070b5dSJoe Perches			    $post_pointer_space =~ /^\s/) {
361131070b5dSJoe Perches				WARN("SPACING",
361231070b5dSJoe Perches				     "Unnecessary space before function pointer name\n" . $herecurr);
361331070b5dSJoe Perches			}
361431070b5dSJoe Perches
361531070b5dSJoe Perches# unnecessary space "type (*funcptr )(args...)"
361631070b5dSJoe Perches			if (defined $post_funcname_space &&
361731070b5dSJoe Perches			    $post_funcname_space =~ /^\s/) {
361831070b5dSJoe Perches				WARN("SPACING",
361931070b5dSJoe Perches				     "Unnecessary space after function pointer name\n" . $herecurr);
362031070b5dSJoe Perches			}
362131070b5dSJoe Perches
362231070b5dSJoe Perches# unnecessary space "type (*funcptr) (args...)"
362331070b5dSJoe Perches			if (defined $pre_args_space &&
362431070b5dSJoe Perches			    $pre_args_space =~ /^\s/) {
362531070b5dSJoe Perches				WARN("SPACING",
362631070b5dSJoe Perches				     "Unnecessary space before function pointer arguments\n" . $herecurr);
362731070b5dSJoe Perches			}
362831070b5dSJoe Perches
362931070b5dSJoe Perches			if (show_type("SPACING") && $fix) {
3630194f66fcSJoe Perches				$fixed[$fixlinenr] =~
363191f72e9cSJoe Perches				    s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex;
363231070b5dSJoe Perches			}
363331070b5dSJoe Perches		}
363431070b5dSJoe Perches
36358d31cfceSAndy Whitcroft# check for spacing round square brackets; allowed:
36368d31cfceSAndy Whitcroft#  1. with a type on the left -- int [] a;
3637fe2a7dbcSAndy Whitcroft#  2. at the beginning of a line for slice initialisers -- [0...10] = 5,
3638fe2a7dbcSAndy Whitcroft#  3. inside a curly brace -- = { [0...10] = 5 }
36398d31cfceSAndy Whitcroft		while ($line =~ /(.*?\s)\[/g) {
36408d31cfceSAndy Whitcroft			my ($where, $prefix) = ($-[1], $1);
36418d31cfceSAndy Whitcroft			if ($prefix !~ /$Type\s+$/ &&
3642fe2a7dbcSAndy Whitcroft			    ($where != 0 || $prefix !~ /^.\s+$/) &&
3643daebc534SAndy Whitcroft			    $prefix !~ /[{,]\s+$/) {
36443705ce5bSJoe Perches				if (ERROR("BRACKET_SPACE",
36453705ce5bSJoe Perches					  "space prohibited before open square bracket '['\n" . $herecurr) &&
36463705ce5bSJoe Perches				    $fix) {
3647194f66fcSJoe Perches				    $fixed[$fixlinenr] =~
36483705ce5bSJoe Perches					s/^(\+.*?)\s+\[/$1\[/;
36493705ce5bSJoe Perches				}
36508d31cfceSAndy Whitcroft			}
36518d31cfceSAndy Whitcroft		}
36528d31cfceSAndy Whitcroft
3653f0a594c1SAndy Whitcroft# check for spaces between functions and their parentheses.
36546c72ffaaSAndy Whitcroft		while ($line =~ /($Ident)\s+\(/g) {
3655c2fdda0dSAndy Whitcroft			my $name = $1;
3656773647a0SAndy Whitcroft			my $ctx_before = substr($line, 0, $-[1]);
3657773647a0SAndy Whitcroft			my $ctx = "$ctx_before$name";
3658c2fdda0dSAndy Whitcroft
3659c2fdda0dSAndy Whitcroft			# Ignore those directives where spaces _are_ permitted.
3660773647a0SAndy Whitcroft			if ($name =~ /^(?:
3661773647a0SAndy Whitcroft				if|for|while|switch|return|case|
3662773647a0SAndy Whitcroft				volatile|__volatile__|
3663773647a0SAndy Whitcroft				__attribute__|format|__extension__|
3664773647a0SAndy Whitcroft				asm|__asm__)$/x)
3665773647a0SAndy Whitcroft			{
3666c2fdda0dSAndy Whitcroft			# cpp #define statements have non-optional spaces, ie
3667c2fdda0dSAndy Whitcroft			# if there is a space between the name and the open
3668c2fdda0dSAndy Whitcroft			# parenthesis it is simply not a parameter group.
3669c45dcabdSAndy Whitcroft			} elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
3670773647a0SAndy Whitcroft
3671773647a0SAndy Whitcroft			# cpp #elif statement condition may start with a (
3672c45dcabdSAndy Whitcroft			} elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
3673c2fdda0dSAndy Whitcroft
3674c2fdda0dSAndy Whitcroft			# If this whole things ends with a type its most
3675c2fdda0dSAndy Whitcroft			# likely a typedef for a function.
3676773647a0SAndy Whitcroft			} elsif ($ctx =~ /$Type$/) {
3677c2fdda0dSAndy Whitcroft
3678c2fdda0dSAndy Whitcroft			} else {
36793705ce5bSJoe Perches				if (WARN("SPACING",
36803705ce5bSJoe Perches					 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
36813705ce5bSJoe Perches					     $fix) {
3682194f66fcSJoe Perches					$fixed[$fixlinenr] =~
36833705ce5bSJoe Perches					    s/\b$name\s+\(/$name\(/;
36843705ce5bSJoe Perches				}
3685f0a594c1SAndy Whitcroft			}
36866c72ffaaSAndy Whitcroft		}
36879a4cad4eSEric Nelson
3688653d4876SAndy Whitcroft# Check operator spacing.
36890a920b5bSAndy Whitcroft		if (!($line=~/\#\s*include/)) {
36903705ce5bSJoe Perches			my $fixed_line = "";
36913705ce5bSJoe Perches			my $line_fixed = 0;
36923705ce5bSJoe Perches
36939c0ca6f9SAndy Whitcroft			my $ops = qr{
36949c0ca6f9SAndy Whitcroft				<<=|>>=|<=|>=|==|!=|
36959c0ca6f9SAndy Whitcroft				\+=|-=|\*=|\/=|%=|\^=|\|=|&=|
36969c0ca6f9SAndy Whitcroft				=>|->|<<|>>|<|>|=|!|~|
36971f65f947SAndy Whitcroft				&&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
369884731623SJoe Perches				\?:|\?|:
36999c0ca6f9SAndy Whitcroft			}x;
3700cf655043SAndy Whitcroft			my @elements = split(/($ops|;)/, $opline);
37013705ce5bSJoe Perches
37023705ce5bSJoe Perches##			print("element count: <" . $#elements . ">\n");
37033705ce5bSJoe Perches##			foreach my $el (@elements) {
37043705ce5bSJoe Perches##				print("el: <$el>\n");
37053705ce5bSJoe Perches##			}
37063705ce5bSJoe Perches
37073705ce5bSJoe Perches			my @fix_elements = ();
370800df344fSAndy Whitcroft			my $off = 0;
37096c72ffaaSAndy Whitcroft
37103705ce5bSJoe Perches			foreach my $el (@elements) {
37113705ce5bSJoe Perches				push(@fix_elements, substr($rawline, $off, length($el)));
37123705ce5bSJoe Perches				$off += length($el);
37133705ce5bSJoe Perches			}
37143705ce5bSJoe Perches
37153705ce5bSJoe Perches			$off = 0;
37163705ce5bSJoe Perches
37176c72ffaaSAndy Whitcroft			my $blank = copy_spacing($opline);
3718b34c648bSJoe Perches			my $last_after = -1;
37196c72ffaaSAndy Whitcroft
37200a920b5bSAndy Whitcroft			for (my $n = 0; $n < $#elements; $n += 2) {
37213705ce5bSJoe Perches
37223705ce5bSJoe Perches				my $good = $fix_elements[$n] . $fix_elements[$n + 1];
37233705ce5bSJoe Perches
37243705ce5bSJoe Perches##				print("n: <$n> good: <$good>\n");
37253705ce5bSJoe Perches
37264a0df2efSAndy Whitcroft				$off += length($elements[$n]);
37274a0df2efSAndy Whitcroft
372825985edcSLucas De Marchi				# Pick up the preceding and succeeding characters.
3729773647a0SAndy Whitcroft				my $ca = substr($opline, 0, $off);
3730773647a0SAndy Whitcroft				my $cc = '';
3731773647a0SAndy Whitcroft				if (length($opline) >= ($off + length($elements[$n + 1]))) {
3732773647a0SAndy Whitcroft					$cc = substr($opline, $off + length($elements[$n + 1]));
3733773647a0SAndy Whitcroft				}
3734773647a0SAndy Whitcroft				my $cb = "$ca$;$cc";
3735773647a0SAndy Whitcroft
37364a0df2efSAndy Whitcroft				my $a = '';
37374a0df2efSAndy Whitcroft				$a = 'V' if ($elements[$n] ne '');
37384a0df2efSAndy Whitcroft				$a = 'W' if ($elements[$n] =~ /\s$/);
3739cf655043SAndy Whitcroft				$a = 'C' if ($elements[$n] =~ /$;$/);
37404a0df2efSAndy Whitcroft				$a = 'B' if ($elements[$n] =~ /(\[|\()$/);
37414a0df2efSAndy Whitcroft				$a = 'O' if ($elements[$n] eq '');
3742773647a0SAndy Whitcroft				$a = 'E' if ($ca =~ /^\s*$/);
37434a0df2efSAndy Whitcroft
37440a920b5bSAndy Whitcroft				my $op = $elements[$n + 1];
37454a0df2efSAndy Whitcroft
37464a0df2efSAndy Whitcroft				my $c = '';
37470a920b5bSAndy Whitcroft				if (defined $elements[$n + 2]) {
37484a0df2efSAndy Whitcroft					$c = 'V' if ($elements[$n + 2] ne '');
37494a0df2efSAndy Whitcroft					$c = 'W' if ($elements[$n + 2] =~ /^\s/);
3750cf655043SAndy Whitcroft					$c = 'C' if ($elements[$n + 2] =~ /^$;/);
37514a0df2efSAndy Whitcroft					$c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
37524a0df2efSAndy Whitcroft					$c = 'O' if ($elements[$n + 2] eq '');
37538b1b3378SAndy Whitcroft					$c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
37544a0df2efSAndy Whitcroft				} else {
37554a0df2efSAndy Whitcroft					$c = 'E';
37560a920b5bSAndy Whitcroft				}
37570a920b5bSAndy Whitcroft
37584a0df2efSAndy Whitcroft				my $ctx = "${a}x${c}";
37594a0df2efSAndy Whitcroft
37604a0df2efSAndy Whitcroft				my $at = "(ctx:$ctx)";
37614a0df2efSAndy Whitcroft
37626c72ffaaSAndy Whitcroft				my $ptr = substr($blank, 0, $off) . "^";
3763de7d4f0eSAndy Whitcroft				my $hereptr = "$hereline$ptr\n";
37640a920b5bSAndy Whitcroft
376574048ed8SAndy Whitcroft				# Pull out the value of this operator.
37666c72ffaaSAndy Whitcroft				my $op_type = substr($curr_values, $off + 1, 1);
37670a920b5bSAndy Whitcroft
37681f65f947SAndy Whitcroft				# Get the full operator variant.
37691f65f947SAndy Whitcroft				my $opv = $op . substr($curr_vars, $off, 1);
37701f65f947SAndy Whitcroft
377113214adfSAndy Whitcroft				# Ignore operators passed as parameters.
377213214adfSAndy Whitcroft				if ($op_type ne 'V' &&
3773d7fe8065SSam Bobroff				    $ca =~ /\s$/ && $cc =~ /^\s*[,\)]/) {
377413214adfSAndy Whitcroft
3775cf655043SAndy Whitcroft#				# Ignore comments
3776cf655043SAndy Whitcroft#				} elsif ($op =~ /^$;+$/) {
377713214adfSAndy Whitcroft
3778d8aaf121SAndy Whitcroft				# ; should have either the end of line or a space or \ after it
377913214adfSAndy Whitcroft				} elsif ($op eq ';') {
3780cf655043SAndy Whitcroft					if ($ctx !~ /.x[WEBC]/ &&
3781cf655043SAndy Whitcroft					    $cc !~ /^\\/ && $cc !~ /^;/) {
37823705ce5bSJoe Perches						if (ERROR("SPACING",
37833705ce5bSJoe Perches							  "space required after that '$op' $at\n" . $hereptr)) {
3784b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
37853705ce5bSJoe Perches							$line_fixed = 1;
37863705ce5bSJoe Perches						}
3787d8aaf121SAndy Whitcroft					}
3788d8aaf121SAndy Whitcroft
3789d8aaf121SAndy Whitcroft				# // is a comment
3790d8aaf121SAndy Whitcroft				} elsif ($op eq '//') {
37910a920b5bSAndy Whitcroft
3792b00e4814SJoe Perches				#   :   when part of a bitfield
3793b00e4814SJoe Perches				} elsif ($opv eq ':B') {
3794b00e4814SJoe Perches					# skip the bitfield test for now
3795b00e4814SJoe Perches
37961f65f947SAndy Whitcroft				# No spaces for:
37971f65f947SAndy Whitcroft				#   ->
3798b00e4814SJoe Perches				} elsif ($op eq '->') {
37994a0df2efSAndy Whitcroft					if ($ctx =~ /Wx.|.xW/) {
38003705ce5bSJoe Perches						if (ERROR("SPACING",
38013705ce5bSJoe Perches							  "spaces prohibited around that '$op' $at\n" . $hereptr)) {
3802b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
38033705ce5bSJoe Perches							if (defined $fix_elements[$n + 2]) {
38043705ce5bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
38053705ce5bSJoe Perches							}
3806b34c648bSJoe Perches							$line_fixed = 1;
38073705ce5bSJoe Perches						}
38080a920b5bSAndy Whitcroft					}
38090a920b5bSAndy Whitcroft
38102381097bSJoe Perches				# , must not have a space before and must have a space on the right.
38110a920b5bSAndy Whitcroft				} elsif ($op eq ',') {
38122381097bSJoe Perches					my $rtrim_before = 0;
38132381097bSJoe Perches					my $space_after = 0;
38142381097bSJoe Perches					if ($ctx =~ /Wx./) {
38152381097bSJoe Perches						if (ERROR("SPACING",
38162381097bSJoe Perches							  "space prohibited before that '$op' $at\n" . $hereptr)) {
38172381097bSJoe Perches							$line_fixed = 1;
38182381097bSJoe Perches							$rtrim_before = 1;
38192381097bSJoe Perches						}
38202381097bSJoe Perches					}
3821cf655043SAndy Whitcroft					if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
38223705ce5bSJoe Perches						if (ERROR("SPACING",
38233705ce5bSJoe Perches							  "space required after that '$op' $at\n" . $hereptr)) {
38243705ce5bSJoe Perches							$line_fixed = 1;
3825b34c648bSJoe Perches							$last_after = $n;
38262381097bSJoe Perches							$space_after = 1;
38272381097bSJoe Perches						}
38282381097bSJoe Perches					}
38292381097bSJoe Perches					if ($rtrim_before || $space_after) {
38302381097bSJoe Perches						if ($rtrim_before) {
38312381097bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
38322381097bSJoe Perches						} else {
38332381097bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
38342381097bSJoe Perches						}
38352381097bSJoe Perches						if ($space_after) {
38362381097bSJoe Perches							$good .= " ";
38373705ce5bSJoe Perches						}
38380a920b5bSAndy Whitcroft					}
38390a920b5bSAndy Whitcroft
38409c0ca6f9SAndy Whitcroft				# '*' as part of a type definition -- reported already.
384174048ed8SAndy Whitcroft				} elsif ($opv eq '*_') {
38429c0ca6f9SAndy Whitcroft					#warn "'*' is part of type\n";
38439c0ca6f9SAndy Whitcroft
38449c0ca6f9SAndy Whitcroft				# unary operators should have a space before and
38459c0ca6f9SAndy Whitcroft				# none after.  May be left adjacent to another
38469c0ca6f9SAndy Whitcroft				# unary operator, or a cast
38479c0ca6f9SAndy Whitcroft				} elsif ($op eq '!' || $op eq '~' ||
384874048ed8SAndy Whitcroft					 $opv eq '*U' || $opv eq '-U' ||
38490d413866SAndy Whitcroft					 $opv eq '&U' || $opv eq '&&U') {
3850cf655043SAndy Whitcroft					if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
38513705ce5bSJoe Perches						if (ERROR("SPACING",
38523705ce5bSJoe Perches							  "space required before that '$op' $at\n" . $hereptr)) {
3853b34c648bSJoe Perches							if ($n != $last_after + 2) {
3854b34c648bSJoe Perches								$good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]);
38553705ce5bSJoe Perches								$line_fixed = 1;
38563705ce5bSJoe Perches							}
38570a920b5bSAndy Whitcroft						}
3858b34c648bSJoe Perches					}
3859a3340b35SAndy Whitcroft					if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
3860171ae1a4SAndy Whitcroft						# A unary '*' may be const
3861171ae1a4SAndy Whitcroft
3862171ae1a4SAndy Whitcroft					} elsif ($ctx =~ /.xW/) {
38633705ce5bSJoe Perches						if (ERROR("SPACING",
38643705ce5bSJoe Perches							  "space prohibited after that '$op' $at\n" . $hereptr)) {
3865b34c648bSJoe Perches							$good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]);
38663705ce5bSJoe Perches							if (defined $fix_elements[$n + 2]) {
38673705ce5bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
38683705ce5bSJoe Perches							}
3869b34c648bSJoe Perches							$line_fixed = 1;
38703705ce5bSJoe Perches						}
38710a920b5bSAndy Whitcroft					}
38720a920b5bSAndy Whitcroft
38730a920b5bSAndy Whitcroft				# unary ++ and unary -- are allowed no space on one side.
38740a920b5bSAndy Whitcroft				} elsif ($op eq '++' or $op eq '--') {
3875773647a0SAndy Whitcroft					if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
38763705ce5bSJoe Perches						if (ERROR("SPACING",
38773705ce5bSJoe Perches							  "space required one side of that '$op' $at\n" . $hereptr)) {
3878b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
38793705ce5bSJoe Perches							$line_fixed = 1;
38803705ce5bSJoe Perches						}
38810a920b5bSAndy Whitcroft					}
3882773647a0SAndy Whitcroft					if ($ctx =~ /Wx[BE]/ ||
3883773647a0SAndy Whitcroft					    ($ctx =~ /Wx./ && $cc =~ /^;/)) {
38843705ce5bSJoe Perches						if (ERROR("SPACING",
38853705ce5bSJoe Perches							  "space prohibited before that '$op' $at\n" . $hereptr)) {
3886b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
38873705ce5bSJoe Perches							$line_fixed = 1;
38883705ce5bSJoe Perches						}
3889653d4876SAndy Whitcroft					}
3890773647a0SAndy Whitcroft					if ($ctx =~ /ExW/) {
38913705ce5bSJoe Perches						if (ERROR("SPACING",
38923705ce5bSJoe Perches							  "space prohibited after that '$op' $at\n" . $hereptr)) {
3893b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
38943705ce5bSJoe Perches							if (defined $fix_elements[$n + 2]) {
38953705ce5bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
3896773647a0SAndy Whitcroft							}
3897b34c648bSJoe Perches							$line_fixed = 1;
38983705ce5bSJoe Perches						}
38993705ce5bSJoe Perches					}
39000a920b5bSAndy Whitcroft
39010a920b5bSAndy Whitcroft				# << and >> may either have or not have spaces both sides
39029c0ca6f9SAndy Whitcroft				} elsif ($op eq '<<' or $op eq '>>' or
39039c0ca6f9SAndy Whitcroft					 $op eq '&' or $op eq '^' or $op eq '|' or
39049c0ca6f9SAndy Whitcroft					 $op eq '+' or $op eq '-' or
3905c2fdda0dSAndy Whitcroft					 $op eq '*' or $op eq '/' or
3906c2fdda0dSAndy Whitcroft					 $op eq '%')
39070a920b5bSAndy Whitcroft				{
3908d2e025f3SJoe Perches					if ($check) {
3909d2e025f3SJoe Perches						if (defined $fix_elements[$n + 2] && $ctx !~ /[EW]x[EW]/) {
3910d2e025f3SJoe Perches							if (CHK("SPACING",
3911d2e025f3SJoe Perches								"spaces preferred around that '$op' $at\n" . $hereptr)) {
3912d2e025f3SJoe Perches								$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
3913d2e025f3SJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
3914d2e025f3SJoe Perches								$line_fixed = 1;
3915d2e025f3SJoe Perches							}
3916d2e025f3SJoe Perches						} elsif (!defined $fix_elements[$n + 2] && $ctx !~ /Wx[OE]/) {
3917d2e025f3SJoe Perches							if (CHK("SPACING",
3918d2e025f3SJoe Perches								"space preferred before that '$op' $at\n" . $hereptr)) {
3919d2e025f3SJoe Perches								$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]);
3920d2e025f3SJoe Perches								$line_fixed = 1;
3921d2e025f3SJoe Perches							}
3922d2e025f3SJoe Perches						}
3923d2e025f3SJoe Perches					} elsif ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
39243705ce5bSJoe Perches						if (ERROR("SPACING",
39253705ce5bSJoe Perches							  "need consistent spacing around '$op' $at\n" . $hereptr)) {
3926b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
3927b34c648bSJoe Perches							if (defined $fix_elements[$n + 2]) {
3928b34c648bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
3929b34c648bSJoe Perches							}
39303705ce5bSJoe Perches							$line_fixed = 1;
39313705ce5bSJoe Perches						}
39320a920b5bSAndy Whitcroft					}
39330a920b5bSAndy Whitcroft
39341f65f947SAndy Whitcroft				# A colon needs no spaces before when it is
39351f65f947SAndy Whitcroft				# terminating a case value or a label.
39361f65f947SAndy Whitcroft				} elsif ($opv eq ':C' || $opv eq ':L') {
39371f65f947SAndy Whitcroft					if ($ctx =~ /Wx./) {
39383705ce5bSJoe Perches						if (ERROR("SPACING",
39393705ce5bSJoe Perches							  "space prohibited before that '$op' $at\n" . $hereptr)) {
3940b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
39413705ce5bSJoe Perches							$line_fixed = 1;
39423705ce5bSJoe Perches						}
39431f65f947SAndy Whitcroft					}
39441f65f947SAndy Whitcroft
39450a920b5bSAndy Whitcroft				# All the others need spaces both sides.
3946cf655043SAndy Whitcroft				} elsif ($ctx !~ /[EWC]x[CWE]/) {
39471f65f947SAndy Whitcroft					my $ok = 0;
39481f65f947SAndy Whitcroft
394922f2a2efSAndy Whitcroft					# Ignore email addresses <foo@bar>
39501f65f947SAndy Whitcroft					if (($op eq '<' &&
39511f65f947SAndy Whitcroft					     $cc =~ /^\S+\@\S+>/) ||
39521f65f947SAndy Whitcroft					    ($op eq '>' &&
39531f65f947SAndy Whitcroft					     $ca =~ /<\S+\@\S+$/))
39541f65f947SAndy Whitcroft					{
39551f65f947SAndy Whitcroft					    	$ok = 1;
39561f65f947SAndy Whitcroft					}
39571f65f947SAndy Whitcroft
3958e0df7e1fSJoe Perches					# for asm volatile statements
3959e0df7e1fSJoe Perches					# ignore a colon with another
3960e0df7e1fSJoe Perches					# colon immediately before or after
3961e0df7e1fSJoe Perches					if (($op eq ':') &&
3962e0df7e1fSJoe Perches					    ($ca =~ /:$/ || $cc =~ /^:/)) {
3963e0df7e1fSJoe Perches						$ok = 1;
3964e0df7e1fSJoe Perches					}
3965e0df7e1fSJoe Perches
396684731623SJoe Perches					# messages are ERROR, but ?: are CHK
39671f65f947SAndy Whitcroft					if ($ok == 0) {
396884731623SJoe Perches						my $msg_type = \&ERROR;
396984731623SJoe Perches						$msg_type = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
397084731623SJoe Perches
397184731623SJoe Perches						if (&{$msg_type}("SPACING",
39723705ce5bSJoe Perches								 "spaces required around that '$op' $at\n" . $hereptr)) {
3973b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
3974b34c648bSJoe Perches							if (defined $fix_elements[$n + 2]) {
3975b34c648bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
3976b34c648bSJoe Perches							}
39773705ce5bSJoe Perches							$line_fixed = 1;
39783705ce5bSJoe Perches						}
39790a920b5bSAndy Whitcroft					}
398022f2a2efSAndy Whitcroft				}
39814a0df2efSAndy Whitcroft				$off += length($elements[$n + 1]);
39823705ce5bSJoe Perches
39833705ce5bSJoe Perches##				print("n: <$n> GOOD: <$good>\n");
39843705ce5bSJoe Perches
39853705ce5bSJoe Perches				$fixed_line = $fixed_line . $good;
39860a920b5bSAndy Whitcroft			}
39873705ce5bSJoe Perches
39883705ce5bSJoe Perches			if (($#elements % 2) == 0) {
39893705ce5bSJoe Perches				$fixed_line = $fixed_line . $fix_elements[$#elements];
39903705ce5bSJoe Perches			}
39913705ce5bSJoe Perches
3992194f66fcSJoe Perches			if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) {
3993194f66fcSJoe Perches				$fixed[$fixlinenr] = $fixed_line;
39943705ce5bSJoe Perches			}
39953705ce5bSJoe Perches
39963705ce5bSJoe Perches
39970a920b5bSAndy Whitcroft		}
39980a920b5bSAndy Whitcroft
3999786b6326SJoe Perches# check for whitespace before a non-naked semicolon
4000d2e248e7SJoe Perches		if ($line =~ /^\+.*\S\s+;\s*$/) {
4001786b6326SJoe Perches			if (WARN("SPACING",
4002786b6326SJoe Perches				 "space prohibited before semicolon\n" . $herecurr) &&
4003786b6326SJoe Perches			    $fix) {
4004194f66fcSJoe Perches				1 while $fixed[$fixlinenr] =~
4005786b6326SJoe Perches				    s/^(\+.*\S)\s+;/$1;/;
4006786b6326SJoe Perches			}
4007786b6326SJoe Perches		}
4008786b6326SJoe Perches
4009f0a594c1SAndy Whitcroft# check for multiple assignments
4010f0a594c1SAndy Whitcroft		if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
4011000d1cc1SJoe Perches			CHK("MULTIPLE_ASSIGNMENTS",
4012000d1cc1SJoe Perches			    "multiple assignments should be avoided\n" . $herecurr);
4013f0a594c1SAndy Whitcroft		}
4014f0a594c1SAndy Whitcroft
401522f2a2efSAndy Whitcroft## # check for multiple declarations, allowing for a function declaration
401622f2a2efSAndy Whitcroft## # continuation.
401722f2a2efSAndy Whitcroft## 		if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
401822f2a2efSAndy Whitcroft## 		    $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
401922f2a2efSAndy Whitcroft##
402022f2a2efSAndy Whitcroft## 			# Remove any bracketed sections to ensure we do not
402122f2a2efSAndy Whitcroft## 			# falsly report the parameters of functions.
402222f2a2efSAndy Whitcroft## 			my $ln = $line;
402322f2a2efSAndy Whitcroft## 			while ($ln =~ s/\([^\(\)]*\)//g) {
402422f2a2efSAndy Whitcroft## 			}
402522f2a2efSAndy Whitcroft## 			if ($ln =~ /,/) {
4026000d1cc1SJoe Perches## 				WARN("MULTIPLE_DECLARATION",
4027000d1cc1SJoe Perches##				     "declaring multiple variables together should be avoided\n" . $herecurr);
402822f2a2efSAndy Whitcroft## 			}
402922f2a2efSAndy Whitcroft## 		}
4030f0a594c1SAndy Whitcroft
40310a920b5bSAndy Whitcroft#need space before brace following if, while, etc
403222f2a2efSAndy Whitcroft		if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
403322f2a2efSAndy Whitcroft		    $line =~ /do{/) {
40343705ce5bSJoe Perches			if (ERROR("SPACING",
40353705ce5bSJoe Perches				  "space required before the open brace '{'\n" . $herecurr) &&
40363705ce5bSJoe Perches			    $fix) {
4037194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/^(\+.*(?:do|\))){/$1 {/;
40383705ce5bSJoe Perches			}
4039de7d4f0eSAndy Whitcroft		}
4040de7d4f0eSAndy Whitcroft
4041c4a62ef9SJoe Perches## # check for blank lines before declarations
4042c4a62ef9SJoe Perches##		if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
4043c4a62ef9SJoe Perches##		    $prevrawline =~ /^.\s*$/) {
4044c4a62ef9SJoe Perches##			WARN("SPACING",
4045c4a62ef9SJoe Perches##			     "No blank lines before declarations\n" . $hereprev);
4046c4a62ef9SJoe Perches##		}
4047c4a62ef9SJoe Perches##
4048c4a62ef9SJoe Perches
4049de7d4f0eSAndy Whitcroft# closing brace should have a space following it when it has anything
4050de7d4f0eSAndy Whitcroft# on the line
4051de7d4f0eSAndy Whitcroft		if ($line =~ /}(?!(?:,|;|\)))\S/) {
4052d5e616fcSJoe Perches			if (ERROR("SPACING",
4053d5e616fcSJoe Perches				  "space required after that close brace '}'\n" . $herecurr) &&
4054d5e616fcSJoe Perches			    $fix) {
4055194f66fcSJoe Perches				$fixed[$fixlinenr] =~
4056d5e616fcSJoe Perches				    s/}((?!(?:,|;|\)))\S)/} $1/;
4057d5e616fcSJoe Perches			}
40580a920b5bSAndy Whitcroft		}
40590a920b5bSAndy Whitcroft
406022f2a2efSAndy Whitcroft# check spacing on square brackets
406122f2a2efSAndy Whitcroft		if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
40623705ce5bSJoe Perches			if (ERROR("SPACING",
40633705ce5bSJoe Perches				  "space prohibited after that open square bracket '['\n" . $herecurr) &&
40643705ce5bSJoe Perches			    $fix) {
4065194f66fcSJoe Perches				$fixed[$fixlinenr] =~
40663705ce5bSJoe Perches				    s/\[\s+/\[/;
40673705ce5bSJoe Perches			}
406822f2a2efSAndy Whitcroft		}
406922f2a2efSAndy Whitcroft		if ($line =~ /\s\]/) {
40703705ce5bSJoe Perches			if (ERROR("SPACING",
40713705ce5bSJoe Perches				  "space prohibited before that close square bracket ']'\n" . $herecurr) &&
40723705ce5bSJoe Perches			    $fix) {
4073194f66fcSJoe Perches				$fixed[$fixlinenr] =~
40743705ce5bSJoe Perches				    s/\s+\]/\]/;
40753705ce5bSJoe Perches			}
407622f2a2efSAndy Whitcroft		}
407722f2a2efSAndy Whitcroft
4078c45dcabdSAndy Whitcroft# check spacing on parentheses
40799c0ca6f9SAndy Whitcroft		if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
40809c0ca6f9SAndy Whitcroft		    $line !~ /for\s*\(\s+;/) {
40813705ce5bSJoe Perches			if (ERROR("SPACING",
40823705ce5bSJoe Perches				  "space prohibited after that open parenthesis '('\n" . $herecurr) &&
40833705ce5bSJoe Perches			    $fix) {
4084194f66fcSJoe Perches				$fixed[$fixlinenr] =~
40853705ce5bSJoe Perches				    s/\(\s+/\(/;
40863705ce5bSJoe Perches			}
408722f2a2efSAndy Whitcroft		}
408813214adfSAndy Whitcroft		if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
4089c45dcabdSAndy Whitcroft		    $line !~ /for\s*\(.*;\s+\)/ &&
4090c45dcabdSAndy Whitcroft		    $line !~ /:\s+\)/) {
40913705ce5bSJoe Perches			if (ERROR("SPACING",
40923705ce5bSJoe Perches				  "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
40933705ce5bSJoe Perches			    $fix) {
4094194f66fcSJoe Perches				$fixed[$fixlinenr] =~
40953705ce5bSJoe Perches				    s/\s+\)/\)/;
40963705ce5bSJoe Perches			}
409722f2a2efSAndy Whitcroft		}
409822f2a2efSAndy Whitcroft
4099e2826fd0SJoe Perches# check unnecessary parentheses around addressof/dereference single $Lvals
4100e2826fd0SJoe Perches# ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar
4101e2826fd0SJoe Perches
4102e2826fd0SJoe Perches		while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) {
4103ea4acbb1SJoe Perches			my $var = $1;
4104ea4acbb1SJoe Perches			if (CHK("UNNECESSARY_PARENTHESES",
4105ea4acbb1SJoe Perches				"Unnecessary parentheses around $var\n" . $herecurr) &&
4106ea4acbb1SJoe Perches			    $fix) {
4107ea4acbb1SJoe Perches				$fixed[$fixlinenr] =~ s/\(\s*\Q$var\E\s*\)/$var/;
4108ea4acbb1SJoe Perches			}
4109ea4acbb1SJoe Perches		}
4110ea4acbb1SJoe Perches
4111ea4acbb1SJoe Perches# check for unnecessary parentheses around function pointer uses
4112ea4acbb1SJoe Perches# ie: (foo->bar)(); should be foo->bar();
4113ea4acbb1SJoe Perches# but not "if (foo->bar) (" to avoid some false positives
4114ea4acbb1SJoe Perches		if ($line =~ /(\bif\s*|)(\(\s*$Ident\s*(?:$Member\s*)+\))[ \t]*\(/ && $1 !~ /^if/) {
4115ea4acbb1SJoe Perches			my $var = $2;
4116ea4acbb1SJoe Perches			if (CHK("UNNECESSARY_PARENTHESES",
4117ea4acbb1SJoe Perches				"Unnecessary parentheses around function pointer $var\n" . $herecurr) &&
4118ea4acbb1SJoe Perches			    $fix) {
4119ea4acbb1SJoe Perches				my $var2 = deparenthesize($var);
4120ea4acbb1SJoe Perches				$var2 =~ s/\s//g;
4121ea4acbb1SJoe Perches				$fixed[$fixlinenr] =~ s/\Q$var\E/$var2/;
4122ea4acbb1SJoe Perches			}
4123e2826fd0SJoe Perches		}
4124e2826fd0SJoe Perches
41250a920b5bSAndy Whitcroft#goto labels aren't indented, allow a single space however
41264a0df2efSAndy Whitcroft		if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
41270a920b5bSAndy Whitcroft		   !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
41283705ce5bSJoe Perches			if (WARN("INDENTED_LABEL",
41293705ce5bSJoe Perches				 "labels should not be indented\n" . $herecurr) &&
41303705ce5bSJoe Perches			    $fix) {
4131194f66fcSJoe Perches				$fixed[$fixlinenr] =~
41323705ce5bSJoe Perches				    s/^(.)\s+/$1/;
41333705ce5bSJoe Perches			}
41340a920b5bSAndy Whitcroft		}
41350a920b5bSAndy Whitcroft
41365b9553abSJoe Perches# return is not a function
4137507e5141SJoe Perches		if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {
4138c45dcabdSAndy Whitcroft			my $spacing = $1;
4139507e5141SJoe Perches			if ($^V && $^V ge 5.10.0 &&
41405b9553abSJoe Perches			    $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) {
41415b9553abSJoe Perches				my $value = $1;
41425b9553abSJoe Perches				$value = deparenthesize($value);
41435b9553abSJoe Perches				if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) {
4144000d1cc1SJoe Perches					ERROR("RETURN_PARENTHESES",
4145000d1cc1SJoe Perches					      "return is not a function, parentheses are not required\n" . $herecurr);
41465b9553abSJoe Perches				}
4147c45dcabdSAndy Whitcroft			} elsif ($spacing !~ /\s+/) {
4148000d1cc1SJoe Perches				ERROR("SPACING",
4149000d1cc1SJoe Perches				      "space required before the open parenthesis '('\n" . $herecurr);
4150c45dcabdSAndy Whitcroft			}
4151c45dcabdSAndy Whitcroft		}
4152507e5141SJoe Perches
4153b43ae21bSJoe Perches# unnecessary return in a void function
4154b43ae21bSJoe Perches# at end-of-function, with the previous line a single leading tab, then return;
4155b43ae21bSJoe Perches# and the line before that not a goto label target like "out:"
4156b43ae21bSJoe Perches		if ($sline =~ /^[ \+]}\s*$/ &&
4157b43ae21bSJoe Perches		    $prevline =~ /^\+\treturn\s*;\s*$/ &&
4158b43ae21bSJoe Perches		    $linenr >= 3 &&
4159b43ae21bSJoe Perches		    $lines[$linenr - 3] =~ /^[ +]/ &&
4160b43ae21bSJoe Perches		    $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) {
41619819cf25SJoe Perches			WARN("RETURN_VOID",
4162b43ae21bSJoe Perches			     "void function return statements are not generally useful\n" . $hereprev);
41639819cf25SJoe Perches               }
41649819cf25SJoe Perches
4165189248d8SJoe Perches# if statements using unnecessary parentheses - ie: if ((foo == bar))
4166189248d8SJoe Perches		if ($^V && $^V ge 5.10.0 &&
4167189248d8SJoe Perches		    $line =~ /\bif\s*((?:\(\s*){2,})/) {
4168189248d8SJoe Perches			my $openparens = $1;
4169189248d8SJoe Perches			my $count = $openparens =~ tr@\(@\(@;
4170189248d8SJoe Perches			my $msg = "";
4171189248d8SJoe Perches			if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) {
4172189248d8SJoe Perches				my $comp = $4;	#Not $1 because of $LvalOrFunc
4173189248d8SJoe Perches				$msg = " - maybe == should be = ?" if ($comp eq "==");
4174189248d8SJoe Perches				WARN("UNNECESSARY_PARENTHESES",
4175189248d8SJoe Perches				     "Unnecessary parentheses$msg\n" . $herecurr);
4176189248d8SJoe Perches			}
4177189248d8SJoe Perches		}
4178189248d8SJoe Perches
4179f34e4a4fSJoe Perches# Return of what appears to be an errno should normally be negative
4180f34e4a4fSJoe Perches		if ($sline =~ /\breturn(?:\s*\(+\s*|\s+)(E[A-Z]+)(?:\s*\)+\s*|\s*)[;:,]/) {
418153a3c448SAndy Whitcroft			my $name = $1;
418253a3c448SAndy Whitcroft			if ($name ne 'EOF' && $name ne 'ERROR') {
4183000d1cc1SJoe Perches				WARN("USE_NEGATIVE_ERRNO",
4184f34e4a4fSJoe Perches				     "return of an errno should typically be negative (ie: return -$1)\n" . $herecurr);
418553a3c448SAndy Whitcroft			}
418653a3c448SAndy Whitcroft		}
4187c45dcabdSAndy Whitcroft
41880a920b5bSAndy Whitcroft# Need a space before open parenthesis after if, while etc
41894a0df2efSAndy Whitcroft		if ($line =~ /\b(if|while|for|switch)\(/) {
41903705ce5bSJoe Perches			if (ERROR("SPACING",
41913705ce5bSJoe Perches				  "space required before the open parenthesis '('\n" . $herecurr) &&
41923705ce5bSJoe Perches			    $fix) {
4193194f66fcSJoe Perches				$fixed[$fixlinenr] =~
41943705ce5bSJoe Perches				    s/\b(if|while|for|switch)\(/$1 \(/;
41953705ce5bSJoe Perches			}
41960a920b5bSAndy Whitcroft		}
41970a920b5bSAndy Whitcroft
4198f5fe35ddSAndy Whitcroft# Check for illegal assignment in if conditional -- and check for trailing
4199f5fe35ddSAndy Whitcroft# statements after the conditional.
4200170d3a22SAndy Whitcroft		if ($line =~ /do\s*(?!{)/) {
42013e469cdcSAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
42023e469cdcSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0)
42033e469cdcSAndy Whitcroft					if (!defined $stat);
4204170d3a22SAndy Whitcroft			my ($stat_next) = ctx_statement_block($line_nr_next,
4205170d3a22SAndy Whitcroft						$remain_next, $off_next);
4206170d3a22SAndy Whitcroft			$stat_next =~ s/\n./\n /g;
4207170d3a22SAndy Whitcroft			##print "stat<$stat> stat_next<$stat_next>\n";
4208170d3a22SAndy Whitcroft
4209170d3a22SAndy Whitcroft			if ($stat_next =~ /^\s*while\b/) {
4210170d3a22SAndy Whitcroft				# If the statement carries leading newlines,
4211170d3a22SAndy Whitcroft				# then count those as offsets.
4212170d3a22SAndy Whitcroft				my ($whitespace) =
4213170d3a22SAndy Whitcroft					($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
4214170d3a22SAndy Whitcroft				my $offset =
4215170d3a22SAndy Whitcroft					statement_rawlines($whitespace) - 1;
4216170d3a22SAndy Whitcroft
4217170d3a22SAndy Whitcroft				$suppress_whiletrailers{$line_nr_next +
4218170d3a22SAndy Whitcroft								$offset} = 1;
4219170d3a22SAndy Whitcroft			}
4220170d3a22SAndy Whitcroft		}
4221170d3a22SAndy Whitcroft		if (!defined $suppress_whiletrailers{$linenr} &&
4222c11230f4SJoe Perches		    defined($stat) && defined($cond) &&
4223170d3a22SAndy Whitcroft		    $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
4224171ae1a4SAndy Whitcroft			my ($s, $c) = ($stat, $cond);
42258905a67cSAndy Whitcroft
4226b53c8e10SAndy Whitcroft			if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
4227000d1cc1SJoe Perches				ERROR("ASSIGN_IN_IF",
4228000d1cc1SJoe Perches				      "do not use assignment in if condition\n" . $herecurr);
42298905a67cSAndy Whitcroft			}
42308905a67cSAndy Whitcroft
42318905a67cSAndy Whitcroft			# Find out what is on the end of the line after the
42328905a67cSAndy Whitcroft			# conditional.
4233773647a0SAndy Whitcroft			substr($s, 0, length($c), '');
42348905a67cSAndy Whitcroft			$s =~ s/\n.*//g;
423513214adfSAndy Whitcroft			$s =~ s/$;//g; 	# Remove any comments
423653210168SAndy Whitcroft			if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
423753210168SAndy Whitcroft			    $c !~ /}\s*while\s*/)
4238773647a0SAndy Whitcroft			{
4239bb44ad39SAndy Whitcroft				# Find out how long the conditional actually is.
4240bb44ad39SAndy Whitcroft				my @newlines = ($c =~ /\n/gs);
4241bb44ad39SAndy Whitcroft				my $cond_lines = 1 + $#newlines;
424242bdf74cSHidetoshi Seto				my $stat_real = '';
4243bb44ad39SAndy Whitcroft
424442bdf74cSHidetoshi Seto				$stat_real = raw_line($linenr, $cond_lines)
424542bdf74cSHidetoshi Seto							. "\n" if ($cond_lines);
4246bb44ad39SAndy Whitcroft				if (defined($stat_real) && $cond_lines > 1) {
4247bb44ad39SAndy Whitcroft					$stat_real = "[...]\n$stat_real";
4248bb44ad39SAndy Whitcroft				}
4249bb44ad39SAndy Whitcroft
4250000d1cc1SJoe Perches				ERROR("TRAILING_STATEMENTS",
4251000d1cc1SJoe Perches				      "trailing statements should be on next line\n" . $herecurr . $stat_real);
42528905a67cSAndy Whitcroft			}
42538905a67cSAndy Whitcroft		}
42548905a67cSAndy Whitcroft
425513214adfSAndy Whitcroft# Check for bitwise tests written as boolean
425613214adfSAndy Whitcroft		if ($line =~ /
425713214adfSAndy Whitcroft			(?:
425813214adfSAndy Whitcroft				(?:\[|\(|\&\&|\|\|)
425913214adfSAndy Whitcroft				\s*0[xX][0-9]+\s*
426013214adfSAndy Whitcroft				(?:\&\&|\|\|)
426113214adfSAndy Whitcroft			|
426213214adfSAndy Whitcroft				(?:\&\&|\|\|)
426313214adfSAndy Whitcroft				\s*0[xX][0-9]+\s*
426413214adfSAndy Whitcroft				(?:\&\&|\|\||\)|\])
426513214adfSAndy Whitcroft			)/x)
426613214adfSAndy Whitcroft		{
4267000d1cc1SJoe Perches			WARN("HEXADECIMAL_BOOLEAN_TEST",
4268000d1cc1SJoe Perches			     "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
426913214adfSAndy Whitcroft		}
427013214adfSAndy Whitcroft
42718905a67cSAndy Whitcroft# if and else should not have general statements after it
427213214adfSAndy Whitcroft		if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
427313214adfSAndy Whitcroft			my $s = $1;
427413214adfSAndy Whitcroft			$s =~ s/$;//g; 	# Remove any comments
427513214adfSAndy Whitcroft			if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
4276000d1cc1SJoe Perches				ERROR("TRAILING_STATEMENTS",
4277000d1cc1SJoe Perches				      "trailing statements should be on next line\n" . $herecurr);
42780a920b5bSAndy Whitcroft			}
427913214adfSAndy Whitcroft		}
428039667782SAndy Whitcroft# if should not continue a brace
428139667782SAndy Whitcroft		if ($line =~ /}\s*if\b/) {
4282000d1cc1SJoe Perches			ERROR("TRAILING_STATEMENTS",
4283048b123fSRasmus Villemoes			      "trailing statements should be on next line (or did you mean 'else if'?)\n" .
428439667782SAndy Whitcroft				$herecurr);
428539667782SAndy Whitcroft		}
4286a1080bf8SAndy Whitcroft# case and default should not have general statements after them
4287a1080bf8SAndy Whitcroft		if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
4288a1080bf8SAndy Whitcroft		    $line !~ /\G(?:
42893fef12d6SAndy Whitcroft			(?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
4290a1080bf8SAndy Whitcroft			\s*return\s+
4291a1080bf8SAndy Whitcroft		    )/xg)
4292a1080bf8SAndy Whitcroft		{
4293000d1cc1SJoe Perches			ERROR("TRAILING_STATEMENTS",
4294000d1cc1SJoe Perches			      "trailing statements should be on next line\n" . $herecurr);
4295a1080bf8SAndy Whitcroft		}
42960a920b5bSAndy Whitcroft
42970a920b5bSAndy Whitcroft		# Check for }<nl>else {, these must be at the same
42980a920b5bSAndy Whitcroft		# indent level to be relevant to each other.
42998b8856f4SJoe Perches		if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ &&
43000a920b5bSAndy Whitcroft		    $previndent == $indent) {
43018b8856f4SJoe Perches			if (ERROR("ELSE_AFTER_BRACE",
43028b8856f4SJoe Perches				  "else should follow close brace '}'\n" . $hereprev) &&
43038b8856f4SJoe Perches			    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
43048b8856f4SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
43058b8856f4SJoe Perches				fix_delete_line($fixlinenr, $rawline);
43068b8856f4SJoe Perches				my $fixedline = $prevrawline;
43078b8856f4SJoe Perches				$fixedline =~ s/}\s*$//;
43088b8856f4SJoe Perches				if ($fixedline !~ /^\+\s*$/) {
43098b8856f4SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
43108b8856f4SJoe Perches				}
43118b8856f4SJoe Perches				$fixedline = $rawline;
43128b8856f4SJoe Perches				$fixedline =~ s/^(.\s*)else/$1} else/;
43138b8856f4SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
43148b8856f4SJoe Perches			}
43150a920b5bSAndy Whitcroft		}
43160a920b5bSAndy Whitcroft
43178b8856f4SJoe Perches		if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ &&
4318c2fdda0dSAndy Whitcroft		    $previndent == $indent) {
4319c2fdda0dSAndy Whitcroft			my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
4320c2fdda0dSAndy Whitcroft
4321c2fdda0dSAndy Whitcroft			# Find out what is on the end of the line after the
4322c2fdda0dSAndy Whitcroft			# conditional.
4323773647a0SAndy Whitcroft			substr($s, 0, length($c), '');
4324c2fdda0dSAndy Whitcroft			$s =~ s/\n.*//g;
4325c2fdda0dSAndy Whitcroft
4326c2fdda0dSAndy Whitcroft			if ($s =~ /^\s*;/) {
43278b8856f4SJoe Perches				if (ERROR("WHILE_AFTER_BRACE",
43288b8856f4SJoe Perches					  "while should follow close brace '}'\n" . $hereprev) &&
43298b8856f4SJoe Perches				    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
43308b8856f4SJoe Perches					fix_delete_line($fixlinenr - 1, $prevrawline);
43318b8856f4SJoe Perches					fix_delete_line($fixlinenr, $rawline);
43328b8856f4SJoe Perches					my $fixedline = $prevrawline;
43338b8856f4SJoe Perches					my $trailing = $rawline;
43348b8856f4SJoe Perches					$trailing =~ s/^\+//;
43358b8856f4SJoe Perches					$trailing = trim($trailing);
43368b8856f4SJoe Perches					$fixedline =~ s/}\s*$/} $trailing/;
43378b8856f4SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
43388b8856f4SJoe Perches				}
4339c2fdda0dSAndy Whitcroft			}
4340c2fdda0dSAndy Whitcroft		}
4341c2fdda0dSAndy Whitcroft
434295e2c602SJoe Perches#Specific variable tests
4343323c1260SJoe Perches		while ($line =~ m{($Constant|$Lval)}g) {
4344323c1260SJoe Perches			my $var = $1;
434595e2c602SJoe Perches
434695e2c602SJoe Perches#gcc binary extension
434795e2c602SJoe Perches			if ($var =~ /^$Binary$/) {
4348d5e616fcSJoe Perches				if (WARN("GCC_BINARY_CONSTANT",
4349d5e616fcSJoe Perches					 "Avoid gcc v4.3+ binary constant extension: <$var>\n" . $herecurr) &&
4350d5e616fcSJoe Perches				    $fix) {
4351d5e616fcSJoe Perches					my $hexval = sprintf("0x%x", oct($var));
4352194f66fcSJoe Perches					$fixed[$fixlinenr] =~
4353d5e616fcSJoe Perches					    s/\b$var\b/$hexval/;
4354d5e616fcSJoe Perches				}
435595e2c602SJoe Perches			}
435695e2c602SJoe Perches
435795e2c602SJoe Perches#CamelCase
4358807bd26cSJoe Perches			if ($var !~ /^$Constant$/ &&
4359be79794bSJoe Perches			    $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
436022735ce8SJoe Perches#Ignore Page<foo> variants
4361807bd26cSJoe Perches			    $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
436222735ce8SJoe Perches#Ignore SI style variants like nS, mV and dB (ie: max_uV, regulator_min_uA_show)
4363f5123576SJulius Werner			    $var !~ /^(?:[a-z_]*?)_?[a-z][A-Z](?:_[a-z_]+)?$/ &&
4364f5123576SJulius Werner#Ignore some three character SI units explicitly, like MiB and KHz
4365f5123576SJulius Werner			    $var !~ /^(?:[a-z_]*?)_?(?:[KMGT]iB|[KMGT]?Hz)(?:_[a-z_]+)?$/) {
43667e781f67SJoe Perches				while ($var =~ m{($Ident)}g) {
43677e781f67SJoe Perches					my $word = $1;
43687e781f67SJoe Perches					next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
4369d8b07710SJoe Perches					if ($check) {
4370d8b07710SJoe Perches						seed_camelcase_includes();
4371d8b07710SJoe Perches						if (!$file && !$camelcase_file_seeded) {
4372d8b07710SJoe Perches							seed_camelcase_file($realfile);
4373d8b07710SJoe Perches							$camelcase_file_seeded = 1;
4374d8b07710SJoe Perches						}
4375d8b07710SJoe Perches					}
43767e781f67SJoe Perches					if (!defined $camelcase{$word}) {
43777e781f67SJoe Perches						$camelcase{$word} = 1;
4378be79794bSJoe Perches						CHK("CAMELCASE",
43797e781f67SJoe Perches						    "Avoid CamelCase: <$word>\n" . $herecurr);
43807e781f67SJoe Perches					}
4381323c1260SJoe Perches				}
4382323c1260SJoe Perches			}
43833445686aSJoe Perches		}
43840a920b5bSAndy Whitcroft
43850a920b5bSAndy Whitcroft#no spaces allowed after \ in define
4386d5e616fcSJoe Perches		if ($line =~ /\#\s*define.*\\\s+$/) {
4387d5e616fcSJoe Perches			if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
4388d5e616fcSJoe Perches				 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
4389d5e616fcSJoe Perches			    $fix) {
4390194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\s+$//;
4391d5e616fcSJoe Perches			}
43920a920b5bSAndy Whitcroft		}
43930a920b5bSAndy Whitcroft
43940e212e0aSFabian Frederick# warn if <asm/foo.h> is #included and <linux/foo.h> is available and includes
43950e212e0aSFabian Frederick# itself <asm/foo.h> (uses RAW line)
4396c45dcabdSAndy Whitcroft		if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
4397e09dec48SAndy Whitcroft			my $file = "$1.h";
4398e09dec48SAndy Whitcroft			my $checkfile = "include/linux/$file";
4399e09dec48SAndy Whitcroft			if (-f "$root/$checkfile" &&
4400e09dec48SAndy Whitcroft			    $realfile ne $checkfile &&
44017840a94cSWolfram Sang			    $1 !~ /$allowed_asm_includes/)
4402c45dcabdSAndy Whitcroft			{
44030e212e0aSFabian Frederick				my $asminclude = `grep -Ec "#include\\s+<asm/$file>" $root/$checkfile`;
44040e212e0aSFabian Frederick				if ($asminclude > 0) {
4405e09dec48SAndy Whitcroft					if ($realfile =~ m{^arch/}) {
4406000d1cc1SJoe Perches						CHK("ARCH_INCLUDE_LINUX",
4407000d1cc1SJoe Perches						    "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
4408e09dec48SAndy Whitcroft					} else {
4409000d1cc1SJoe Perches						WARN("INCLUDE_LINUX",
4410000d1cc1SJoe Perches						     "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
4411e09dec48SAndy Whitcroft					}
44120a920b5bSAndy Whitcroft				}
44130a920b5bSAndy Whitcroft			}
44140e212e0aSFabian Frederick		}
44150a920b5bSAndy Whitcroft
4416653d4876SAndy Whitcroft# multi-statement macros should be enclosed in a do while loop, grab the
4417653d4876SAndy Whitcroft# first statement and ensure its the whole macro if its not enclosed
4418cf655043SAndy Whitcroft# in a known good container
4419b8f96a31SAndy Whitcroft		if ($realfile !~ m@/vmlinux.lds.h$@ &&
4420b8f96a31SAndy Whitcroft		    $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
4421d8aaf121SAndy Whitcroft			my $ln = $linenr;
4422d8aaf121SAndy Whitcroft			my $cnt = $realcnt;
4423c45dcabdSAndy Whitcroft			my ($off, $dstat, $dcond, $rest);
4424c45dcabdSAndy Whitcroft			my $ctx = '';
442508a2843eSJoe Perches			my $has_flow_statement = 0;
442608a2843eSJoe Perches			my $has_arg_concat = 0;
4427c45dcabdSAndy Whitcroft			($dstat, $dcond, $ln, $cnt, $off) =
4428f74bd194SAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0);
4429f74bd194SAndy Whitcroft			$ctx = $dstat;
4430c45dcabdSAndy Whitcroft			#print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
4431a3bb97a7SAndy Whitcroft			#print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
4432c45dcabdSAndy Whitcroft
443308a2843eSJoe Perches			$has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/);
443408a2843eSJoe Perches			$has_arg_concat = 1 if ($ctx =~ /\#\#/);
443508a2843eSJoe Perches
4436f74bd194SAndy Whitcroft			$dstat =~ s/^.\s*\#\s*define\s+$Ident(?:\([^\)]*\))?\s*//;
4437292f1a9bSAndy Whitcroft			$dstat =~ s/$;//g;
4438c45dcabdSAndy Whitcroft			$dstat =~ s/\\\n.//g;
4439c45dcabdSAndy Whitcroft			$dstat =~ s/^\s*//s;
4440c45dcabdSAndy Whitcroft			$dstat =~ s/\s*$//s;
4441c45dcabdSAndy Whitcroft
4442c45dcabdSAndy Whitcroft			# Flatten any parentheses and braces
4443bf30d6edSAndy Whitcroft			while ($dstat =~ s/\([^\(\)]*\)/1/ ||
4444bf30d6edSAndy Whitcroft			       $dstat =~ s/\{[^\{\}]*\}/1/ ||
4445c81769fdSAndy Whitcroft			       $dstat =~ s/\[[^\[\]]*\]/1/)
4446bf30d6edSAndy Whitcroft			{
4447c45dcabdSAndy Whitcroft			}
4448c45dcabdSAndy Whitcroft
4449e45bab8eSAndy Whitcroft			# Flatten any obvious string concatentation.
445033acb54aSJoe Perches			while ($dstat =~ s/($String)\s*$Ident/$1/ ||
445133acb54aSJoe Perches			       $dstat =~ s/$Ident\s*($String)/$1/)
4452e45bab8eSAndy Whitcroft			{
4453e45bab8eSAndy Whitcroft			}
4454e45bab8eSAndy Whitcroft
4455c45dcabdSAndy Whitcroft			my $exceptions = qr{
4456c45dcabdSAndy Whitcroft				$Declare|
4457c45dcabdSAndy Whitcroft				module_param_named|
4458a0a0a7a9SKees Cook				MODULE_PARM_DESC|
4459c45dcabdSAndy Whitcroft				DECLARE_PER_CPU|
4460c45dcabdSAndy Whitcroft				DEFINE_PER_CPU|
4461383099fdSAndy Whitcroft				__typeof__\(|
446222fd2d3eSStefani Seibold				union|
446322fd2d3eSStefani Seibold				struct|
4464ea71a0a0SAndy Whitcroft				\.$Ident\s*=\s*|
4465ea71a0a0SAndy Whitcroft				^\"|\"$
4466c45dcabdSAndy Whitcroft			}x;
44675eaa20b9SAndy Whitcroft			#print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
4468f74bd194SAndy Whitcroft			if ($dstat ne '' &&
4469f74bd194SAndy Whitcroft			    $dstat !~ /^(?:$Ident|-?$Constant),$/ &&			# 10, // foo(),
4470f74bd194SAndy Whitcroft			    $dstat !~ /^(?:$Ident|-?$Constant);$/ &&			# foo();
44713cc4b1c3SJoe Perches			    $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ &&		# 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
4472356fd398SJoe Perches			    $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ &&			# character constants
4473f74bd194SAndy Whitcroft			    $dstat !~ /$exceptions/ &&
4474f74bd194SAndy Whitcroft			    $dstat !~ /^\.$Ident\s*=/ &&				# .foo =
4475e942e2c3SJoe Perches			    $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ &&		# stringification #foo
447672f115f9SAndy Whitcroft			    $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ &&	# do {...} while (...); // do {...} while (...)
4477f74bd194SAndy Whitcroft			    $dstat !~ /^for\s*$Constant$/ &&				# for (...)
4478f74bd194SAndy Whitcroft			    $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ &&	# for (...) bar()
4479f74bd194SAndy Whitcroft			    $dstat !~ /^do\s*{/ &&					# do {...
4480f95a7e6aSJoe Perches			    $dstat !~ /^\({/ &&						# ({...
4481f95a7e6aSJoe Perches			    $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
4482c45dcabdSAndy Whitcroft			{
4483f74bd194SAndy Whitcroft				$ctx =~ s/\n*$//;
4484f74bd194SAndy Whitcroft				my $herectx = $here . "\n";
4485f74bd194SAndy Whitcroft				my $cnt = statement_rawlines($ctx);
4486f74bd194SAndy Whitcroft
4487f74bd194SAndy Whitcroft				for (my $n = 0; $n < $cnt; $n++) {
4488f74bd194SAndy Whitcroft					$herectx .= raw_line($linenr, $n) . "\n";
4489c45dcabdSAndy Whitcroft				}
4490c45dcabdSAndy Whitcroft
4491f74bd194SAndy Whitcroft				if ($dstat =~ /;/) {
4492f74bd194SAndy Whitcroft					ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
4493f74bd194SAndy Whitcroft					      "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
4494f74bd194SAndy Whitcroft				} else {
4495000d1cc1SJoe Perches					ERROR("COMPLEX_MACRO",
4496388982b5SAndrew Morton					      "Macros with complex values should be enclosed in parentheses\n" . "$herectx");
4497d8aaf121SAndy Whitcroft				}
44980a920b5bSAndy Whitcroft			}
44995023d347SJoe Perches
450008a2843eSJoe Perches# check for macros with flow control, but without ## concatenation
450108a2843eSJoe Perches# ## concatenation is commonly a macro that defines a function so ignore those
450208a2843eSJoe Perches			if ($has_flow_statement && !$has_arg_concat) {
450308a2843eSJoe Perches				my $herectx = $here . "\n";
450408a2843eSJoe Perches				my $cnt = statement_rawlines($ctx);
450508a2843eSJoe Perches
450608a2843eSJoe Perches				for (my $n = 0; $n < $cnt; $n++) {
450708a2843eSJoe Perches					$herectx .= raw_line($linenr, $n) . "\n";
450808a2843eSJoe Perches				}
450908a2843eSJoe Perches				WARN("MACRO_WITH_FLOW_CONTROL",
451008a2843eSJoe Perches				     "Macros with flow control statements should be avoided\n" . "$herectx");
451108a2843eSJoe Perches			}
451208a2843eSJoe Perches
4513481eb486SJoe Perches# check for line continuations outside of #defines, preprocessor #, and asm
45145023d347SJoe Perches
45155023d347SJoe Perches		} else {
45165023d347SJoe Perches			if ($prevline !~ /^..*\\$/ &&
4517481eb486SJoe Perches			    $line !~ /^\+\s*\#.*\\$/ &&		# preprocessor
4518481eb486SJoe Perches			    $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ &&	# asm
45195023d347SJoe Perches			    $line =~ /^\+.*\\$/) {
45205023d347SJoe Perches				WARN("LINE_CONTINUATIONS",
45215023d347SJoe Perches				     "Avoid unnecessary line continuations\n" . $herecurr);
45225023d347SJoe Perches			}
4523653d4876SAndy Whitcroft		}
45240a920b5bSAndy Whitcroft
4525b13edf7fSJoe Perches# do {} while (0) macro tests:
4526b13edf7fSJoe Perches# single-statement macros do not need to be enclosed in do while (0) loop,
4527b13edf7fSJoe Perches# macro should not end with a semicolon
4528b13edf7fSJoe Perches		if ($^V && $^V ge 5.10.0 &&
4529b13edf7fSJoe Perches		    $realfile !~ m@/vmlinux.lds.h$@ &&
4530b13edf7fSJoe Perches		    $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
4531b13edf7fSJoe Perches			my $ln = $linenr;
4532b13edf7fSJoe Perches			my $cnt = $realcnt;
4533b13edf7fSJoe Perches			my ($off, $dstat, $dcond, $rest);
4534b13edf7fSJoe Perches			my $ctx = '';
4535b13edf7fSJoe Perches			($dstat, $dcond, $ln, $cnt, $off) =
4536b13edf7fSJoe Perches				ctx_statement_block($linenr, $realcnt, 0);
4537b13edf7fSJoe Perches			$ctx = $dstat;
4538b13edf7fSJoe Perches
4539b13edf7fSJoe Perches			$dstat =~ s/\\\n.//g;
45401b36b201SJoe Perches			$dstat =~ s/$;/ /g;
4541b13edf7fSJoe Perches
4542b13edf7fSJoe Perches			if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
4543b13edf7fSJoe Perches				my $stmts = $2;
4544b13edf7fSJoe Perches				my $semis = $3;
4545b13edf7fSJoe Perches
4546b13edf7fSJoe Perches				$ctx =~ s/\n*$//;
4547b13edf7fSJoe Perches				my $cnt = statement_rawlines($ctx);
4548b13edf7fSJoe Perches				my $herectx = $here . "\n";
4549b13edf7fSJoe Perches
4550b13edf7fSJoe Perches				for (my $n = 0; $n < $cnt; $n++) {
4551b13edf7fSJoe Perches					$herectx .= raw_line($linenr, $n) . "\n";
4552b13edf7fSJoe Perches				}
4553b13edf7fSJoe Perches
4554ac8e97f8SJoe Perches				if (($stmts =~ tr/;/;/) == 1 &&
4555ac8e97f8SJoe Perches				    $stmts !~ /^\s*(if|while|for|switch)\b/) {
4556b13edf7fSJoe Perches					WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
4557b13edf7fSJoe Perches					     "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
4558b13edf7fSJoe Perches				}
4559b13edf7fSJoe Perches				if (defined $semis && $semis ne "") {
4560b13edf7fSJoe Perches					WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
4561b13edf7fSJoe Perches					     "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
4562b13edf7fSJoe Perches				}
4563f5ef95b1SJoe Perches			} elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) {
4564f5ef95b1SJoe Perches				$ctx =~ s/\n*$//;
4565f5ef95b1SJoe Perches				my $cnt = statement_rawlines($ctx);
4566f5ef95b1SJoe Perches				my $herectx = $here . "\n";
4567f5ef95b1SJoe Perches
4568f5ef95b1SJoe Perches				for (my $n = 0; $n < $cnt; $n++) {
4569f5ef95b1SJoe Perches					$herectx .= raw_line($linenr, $n) . "\n";
4570f5ef95b1SJoe Perches				}
4571f5ef95b1SJoe Perches
4572f5ef95b1SJoe Perches				WARN("TRAILING_SEMICOLON",
4573f5ef95b1SJoe Perches				     "macros should not use a trailing semicolon\n" . "$herectx");
4574b13edf7fSJoe Perches			}
4575b13edf7fSJoe Perches		}
4576b13edf7fSJoe Perches
4577080ba929SMike Frysinger# make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
4578080ba929SMike Frysinger# all assignments may have only one of the following with an assignment:
4579080ba929SMike Frysinger#	.
4580080ba929SMike Frysinger#	ALIGN(...)
4581080ba929SMike Frysinger#	VMLINUX_SYMBOL(...)
4582080ba929SMike Frysinger		if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
4583000d1cc1SJoe Perches			WARN("MISSING_VMLINUX_SYMBOL",
4584000d1cc1SJoe Perches			     "vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
4585080ba929SMike Frysinger		}
4586080ba929SMike Frysinger
4587f0a594c1SAndy Whitcroft# check for redundant bracing round if etc
458813214adfSAndy Whitcroft		if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
458913214adfSAndy Whitcroft			my ($level, $endln, @chunks) =
4590cf655043SAndy Whitcroft				ctx_statement_full($linenr, $realcnt, 1);
459113214adfSAndy Whitcroft			#print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
4592cf655043SAndy Whitcroft			#print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
4593cf655043SAndy Whitcroft			if ($#chunks > 0 && $level == 0) {
4594aad4f614SJoe Perches				my @allowed = ();
4595aad4f614SJoe Perches				my $allow = 0;
459613214adfSAndy Whitcroft				my $seen = 0;
4597773647a0SAndy Whitcroft				my $herectx = $here . "\n";
4598cf655043SAndy Whitcroft				my $ln = $linenr - 1;
459913214adfSAndy Whitcroft				for my $chunk (@chunks) {
460013214adfSAndy Whitcroft					my ($cond, $block) = @{$chunk};
460113214adfSAndy Whitcroft
4602773647a0SAndy Whitcroft					# If the condition carries leading newlines, then count those as offsets.
4603773647a0SAndy Whitcroft					my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
4604773647a0SAndy Whitcroft					my $offset = statement_rawlines($whitespace) - 1;
4605773647a0SAndy Whitcroft
4606aad4f614SJoe Perches					$allowed[$allow] = 0;
4607773647a0SAndy Whitcroft					#print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
4608773647a0SAndy Whitcroft
4609773647a0SAndy Whitcroft					# We have looked at and allowed this specific line.
4610773647a0SAndy Whitcroft					$suppress_ifbraces{$ln + $offset} = 1;
4611773647a0SAndy Whitcroft
4612773647a0SAndy Whitcroft					$herectx .= "$rawlines[$ln + $offset]\n[...]\n";
4613cf655043SAndy Whitcroft					$ln += statement_rawlines($block) - 1;
4614cf655043SAndy Whitcroft
4615773647a0SAndy Whitcroft					substr($block, 0, length($cond), '');
461613214adfSAndy Whitcroft
461713214adfSAndy Whitcroft					$seen++ if ($block =~ /^\s*{/);
461813214adfSAndy Whitcroft
4619aad4f614SJoe Perches					#print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
4620cf655043SAndy Whitcroft					if (statement_lines($cond) > 1) {
4621cf655043SAndy Whitcroft						#print "APW: ALLOWED: cond<$cond>\n";
4622aad4f614SJoe Perches						$allowed[$allow] = 1;
462313214adfSAndy Whitcroft					}
462413214adfSAndy Whitcroft					if ($block =~/\b(?:if|for|while)\b/) {
4625cf655043SAndy Whitcroft						#print "APW: ALLOWED: block<$block>\n";
4626aad4f614SJoe Perches						$allowed[$allow] = 1;
462713214adfSAndy Whitcroft					}
4628cf655043SAndy Whitcroft					if (statement_block_size($block) > 1) {
4629cf655043SAndy Whitcroft						#print "APW: ALLOWED: lines block<$block>\n";
4630aad4f614SJoe Perches						$allowed[$allow] = 1;
463113214adfSAndy Whitcroft					}
4632aad4f614SJoe Perches					$allow++;
463313214adfSAndy Whitcroft				}
4634aad4f614SJoe Perches				if ($seen) {
4635aad4f614SJoe Perches					my $sum_allowed = 0;
4636aad4f614SJoe Perches					foreach (@allowed) {
4637aad4f614SJoe Perches						$sum_allowed += $_;
4638aad4f614SJoe Perches					}
4639aad4f614SJoe Perches					if ($sum_allowed == 0) {
4640000d1cc1SJoe Perches						WARN("BRACES",
4641000d1cc1SJoe Perches						     "braces {} are not necessary for any arm of this statement\n" . $herectx);
4642aad4f614SJoe Perches					} elsif ($sum_allowed != $allow &&
4643aad4f614SJoe Perches						 $seen != $allow) {
4644aad4f614SJoe Perches						CHK("BRACES",
4645aad4f614SJoe Perches						    "braces {} should be used on all arms of this statement\n" . $herectx);
4646aad4f614SJoe Perches					}
464713214adfSAndy Whitcroft				}
464813214adfSAndy Whitcroft			}
464913214adfSAndy Whitcroft		}
4650773647a0SAndy Whitcroft		if (!defined $suppress_ifbraces{$linenr - 1} &&
465113214adfSAndy Whitcroft					$line =~ /\b(if|while|for|else)\b/) {
4652cf655043SAndy Whitcroft			my $allowed = 0;
4653f0a594c1SAndy Whitcroft
4654cf655043SAndy Whitcroft			# Check the pre-context.
4655cf655043SAndy Whitcroft			if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
4656cf655043SAndy Whitcroft				#print "APW: ALLOWED: pre<$1>\n";
4657cf655043SAndy Whitcroft				$allowed = 1;
4658f0a594c1SAndy Whitcroft			}
4659773647a0SAndy Whitcroft
4660773647a0SAndy Whitcroft			my ($level, $endln, @chunks) =
4661773647a0SAndy Whitcroft				ctx_statement_full($linenr, $realcnt, $-[0]);
4662773647a0SAndy Whitcroft
4663cf655043SAndy Whitcroft			# Check the condition.
4664cf655043SAndy Whitcroft			my ($cond, $block) = @{$chunks[0]};
4665773647a0SAndy Whitcroft			#print "CHECKING<$linenr> cond<$cond> block<$block>\n";
4666cf655043SAndy Whitcroft			if (defined $cond) {
4667773647a0SAndy Whitcroft				substr($block, 0, length($cond), '');
4668cf655043SAndy Whitcroft			}
4669cf655043SAndy Whitcroft			if (statement_lines($cond) > 1) {
4670cf655043SAndy Whitcroft				#print "APW: ALLOWED: cond<$cond>\n";
4671cf655043SAndy Whitcroft				$allowed = 1;
4672cf655043SAndy Whitcroft			}
4673cf655043SAndy Whitcroft			if ($block =~/\b(?:if|for|while)\b/) {
4674cf655043SAndy Whitcroft				#print "APW: ALLOWED: block<$block>\n";
4675cf655043SAndy Whitcroft				$allowed = 1;
4676cf655043SAndy Whitcroft			}
4677cf655043SAndy Whitcroft			if (statement_block_size($block) > 1) {
4678cf655043SAndy Whitcroft				#print "APW: ALLOWED: lines block<$block>\n";
4679cf655043SAndy Whitcroft				$allowed = 1;
4680cf655043SAndy Whitcroft			}
4681cf655043SAndy Whitcroft			# Check the post-context.
4682cf655043SAndy Whitcroft			if (defined $chunks[1]) {
4683cf655043SAndy Whitcroft				my ($cond, $block) = @{$chunks[1]};
4684cf655043SAndy Whitcroft				if (defined $cond) {
4685773647a0SAndy Whitcroft					substr($block, 0, length($cond), '');
4686cf655043SAndy Whitcroft				}
4687cf655043SAndy Whitcroft				if ($block =~ /^\s*\{/) {
4688cf655043SAndy Whitcroft					#print "APW: ALLOWED: chunk-1 block<$block>\n";
4689cf655043SAndy Whitcroft					$allowed = 1;
4690cf655043SAndy Whitcroft				}
4691cf655043SAndy Whitcroft			}
4692cf655043SAndy Whitcroft			if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
469369932487SJustin P. Mattock				my $herectx = $here . "\n";
4694f055663cSAndy Whitcroft				my $cnt = statement_rawlines($block);
4695cf655043SAndy Whitcroft
4696f055663cSAndy Whitcroft				for (my $n = 0; $n < $cnt; $n++) {
469769932487SJustin P. Mattock					$herectx .= raw_line($linenr, $n) . "\n";
4698cf655043SAndy Whitcroft				}
4699cf655043SAndy Whitcroft
4700000d1cc1SJoe Perches				WARN("BRACES",
4701000d1cc1SJoe Perches				     "braces {} are not necessary for single statement blocks\n" . $herectx);
4702f0a594c1SAndy Whitcroft			}
4703f0a594c1SAndy Whitcroft		}
4704f0a594c1SAndy Whitcroft
47050979ae66SJoe Perches# check for unnecessary blank lines around braces
470677b9a53aSJoe Perches		if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
4707f8e58219SJoe Perches			if (CHK("BRACES",
4708f8e58219SJoe Perches				"Blank lines aren't necessary before a close brace '}'\n" . $hereprev) &&
4709f8e58219SJoe Perches			    $fix && $prevrawline =~ /^\+/) {
4710f8e58219SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
4711f8e58219SJoe Perches			}
47120979ae66SJoe Perches		}
471377b9a53aSJoe Perches		if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
4714f8e58219SJoe Perches			if (CHK("BRACES",
4715f8e58219SJoe Perches				"Blank lines aren't necessary after an open brace '{'\n" . $hereprev) &&
4716f8e58219SJoe Perches			    $fix) {
4717f8e58219SJoe Perches				fix_delete_line($fixlinenr, $rawline);
4718f8e58219SJoe Perches			}
47190979ae66SJoe Perches		}
47200979ae66SJoe Perches
47214a0df2efSAndy Whitcroft# no volatiles please
47226c72ffaaSAndy Whitcroft		my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
47236c72ffaaSAndy Whitcroft		if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
4724000d1cc1SJoe Perches			WARN("VOLATILE",
4725000d1cc1SJoe Perches			     "Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
47264a0df2efSAndy Whitcroft		}
47274a0df2efSAndy Whitcroft
47285e4f6ba5SJoe Perches# Check for user-visible strings broken across lines, which breaks the ability
47295e4f6ba5SJoe Perches# to grep for the string.  Make exceptions when the previous string ends in a
47305e4f6ba5SJoe Perches# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{'
47315e4f6ba5SJoe Perches# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value
473233acb54aSJoe Perches		if ($line =~ /^\+\s*$String/ &&
47335e4f6ba5SJoe Perches		    $prevline =~ /"\s*$/ &&
47345e4f6ba5SJoe Perches		    $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) {
47355e4f6ba5SJoe Perches			if (WARN("SPLIT_STRING",
47365e4f6ba5SJoe Perches				 "quoted string split across lines\n" . $hereprev) &&
47375e4f6ba5SJoe Perches				     $fix &&
47385e4f6ba5SJoe Perches				     $prevrawline =~ /^\+.*"\s*$/ &&
47395e4f6ba5SJoe Perches				     $last_coalesced_string_linenr != $linenr - 1) {
47405e4f6ba5SJoe Perches				my $extracted_string = get_quoted_string($line, $rawline);
47415e4f6ba5SJoe Perches				my $comma_close = "";
47425e4f6ba5SJoe Perches				if ($rawline =~ /\Q$extracted_string\E(\s*\)\s*;\s*$|\s*,\s*)/) {
47435e4f6ba5SJoe Perches					$comma_close = $1;
47445e4f6ba5SJoe Perches				}
47455e4f6ba5SJoe Perches
47465e4f6ba5SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
47475e4f6ba5SJoe Perches				fix_delete_line($fixlinenr, $rawline);
47485e4f6ba5SJoe Perches				my $fixedline = $prevrawline;
47495e4f6ba5SJoe Perches				$fixedline =~ s/"\s*$//;
47505e4f6ba5SJoe Perches				$fixedline .= substr($extracted_string, 1) . trim($comma_close);
47515e4f6ba5SJoe Perches				fix_insert_line($fixlinenr - 1, $fixedline);
47525e4f6ba5SJoe Perches				$fixedline = $rawline;
47535e4f6ba5SJoe Perches				$fixedline =~ s/\Q$extracted_string\E\Q$comma_close\E//;
47545e4f6ba5SJoe Perches				if ($fixedline !~ /\+\s*$/) {
47555e4f6ba5SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
47565e4f6ba5SJoe Perches				}
47575e4f6ba5SJoe Perches				$last_coalesced_string_linenr = $linenr;
47585e4f6ba5SJoe Perches			}
47595e4f6ba5SJoe Perches		}
47605e4f6ba5SJoe Perches
47615e4f6ba5SJoe Perches# check for missing a space in a string concatenation
47625e4f6ba5SJoe Perches		if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) {
47635e4f6ba5SJoe Perches			WARN('MISSING_SPACE',
47645e4f6ba5SJoe Perches			     "break quoted strings at a space character\n" . $hereprev);
47655e4f6ba5SJoe Perches		}
47665e4f6ba5SJoe Perches
47675e4f6ba5SJoe Perches# check for spaces before a quoted newline
47685e4f6ba5SJoe Perches		if ($rawline =~ /^.*\".*\s\\n/) {
47695e4f6ba5SJoe Perches			if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
47705e4f6ba5SJoe Perches				 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
47715e4f6ba5SJoe Perches			    $fix) {
47725e4f6ba5SJoe Perches				$fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
47735e4f6ba5SJoe Perches			}
47745e4f6ba5SJoe Perches
47755e4f6ba5SJoe Perches		}
47765e4f6ba5SJoe Perches
4777f17dba4fSJoe Perches# concatenated string without spaces between elements
477833acb54aSJoe Perches		if ($line =~ /$String[A-Z_]/ || $line =~ /[A-Za-z0-9_]$String/) {
4779f17dba4fSJoe Perches			CHK("CONCATENATED_STRING",
4780f17dba4fSJoe Perches			    "Concatenated strings should use spaces between elements\n" . $herecurr);
4781f17dba4fSJoe Perches		}
4782f17dba4fSJoe Perches
478390ad30e5SJoe Perches# uncoalesced string fragments
478433acb54aSJoe Perches		if ($line =~ /$String\s*"/) {
478590ad30e5SJoe Perches			WARN("STRING_FRAGMENTS",
478690ad30e5SJoe Perches			     "Consecutive strings are generally better as a single string\n" . $herecurr);
478790ad30e5SJoe Perches		}
478890ad30e5SJoe Perches
47895e4f6ba5SJoe Perches# check for %L{u,d,i} in strings
47905e4f6ba5SJoe Perches		my $string;
47915e4f6ba5SJoe Perches		while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
47925e4f6ba5SJoe Perches			$string = substr($rawline, $-[1], $+[1] - $-[1]);
47935e4f6ba5SJoe Perches			$string =~ s/%%/__/g;
47945e4f6ba5SJoe Perches			if ($string =~ /(?<!%)%L[udi]/) {
47955e4f6ba5SJoe Perches				WARN("PRINTF_L",
47965e4f6ba5SJoe Perches				     "\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
47975e4f6ba5SJoe Perches				last;
47985e4f6ba5SJoe Perches			}
47995e4f6ba5SJoe Perches		}
48005e4f6ba5SJoe Perches
48015e4f6ba5SJoe Perches# check for line continuations in quoted strings with odd counts of "
48025e4f6ba5SJoe Perches		if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) {
48035e4f6ba5SJoe Perches			WARN("LINE_CONTINUATIONS",
48045e4f6ba5SJoe Perches			     "Avoid line continuations in quoted strings\n" . $herecurr);
48055e4f6ba5SJoe Perches		}
48065e4f6ba5SJoe Perches
480700df344fSAndy Whitcroft# warn about #if 0
4808c45dcabdSAndy Whitcroft		if ($line =~ /^.\s*\#\s*if\s+0\b/) {
4809000d1cc1SJoe Perches			CHK("REDUNDANT_CODE",
4810000d1cc1SJoe Perches			    "if this code is redundant consider removing it\n" .
4811de7d4f0eSAndy Whitcroft				$herecurr);
48124a0df2efSAndy Whitcroft		}
48134a0df2efSAndy Whitcroft
481403df4b51SAndy Whitcroft# check for needless "if (<foo>) fn(<foo>)" uses
481503df4b51SAndy Whitcroft		if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
481603df4b51SAndy Whitcroft			my $expr = '\s*\(\s*' . quotemeta($1) . '\s*\)\s*;';
481703df4b51SAndy Whitcroft			if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?)$expr/) {
481803df4b51SAndy Whitcroft				WARN('NEEDLESS_IF',
481915160f90SFabio Estevam				     "$1(NULL) is safe and this check is probably not required\n" . $hereprev);
48204c432a8fSGreg Kroah-Hartman			}
48214c432a8fSGreg Kroah-Hartman		}
4822f0a594c1SAndy Whitcroft
4823ebfdc409SJoe Perches# check for unnecessary "Out of Memory" messages
4824ebfdc409SJoe Perches		if ($line =~ /^\+.*\b$logFunctions\s*\(/ &&
4825ebfdc409SJoe Perches		    $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ &&
4826ebfdc409SJoe Perches		    (defined $1 || defined $3) &&
4827ebfdc409SJoe Perches		    $linenr > 3) {
4828ebfdc409SJoe Perches			my $testval = $2;
4829ebfdc409SJoe Perches			my $testline = $lines[$linenr - 3];
4830ebfdc409SJoe Perches
4831ebfdc409SJoe Perches			my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0);
4832ebfdc409SJoe Perches#			print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n");
4833ebfdc409SJoe Perches
4834ebfdc409SJoe 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)/) {
4835ebfdc409SJoe Perches				WARN("OOM_MESSAGE",
4836ebfdc409SJoe Perches				     "Possible unnecessary 'out of memory' message\n" . $hereprev);
4837ebfdc409SJoe Perches			}
4838ebfdc409SJoe Perches		}
4839ebfdc409SJoe Perches
4840f78d98f6SJoe Perches# check for logging functions with KERN_<LEVEL>
4841dcaf1123SPaolo Bonzini		if ($line !~ /printk(?:_ratelimited|_once)?\s*\(/ &&
4842f78d98f6SJoe Perches		    $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) {
4843f78d98f6SJoe Perches			my $level = $1;
4844f78d98f6SJoe Perches			if (WARN("UNNECESSARY_KERN_LEVEL",
4845f78d98f6SJoe Perches				 "Possible unnecessary $level\n" . $herecurr) &&
4846f78d98f6SJoe Perches			    $fix) {
4847f78d98f6SJoe Perches				$fixed[$fixlinenr] =~ s/\s*$level\s*//;
4848f78d98f6SJoe Perches			}
4849f78d98f6SJoe Perches		}
4850f78d98f6SJoe Perches
4851abb08a53SJoe Perches# check for mask then right shift without a parentheses
4852abb08a53SJoe Perches		if ($^V && $^V ge 5.10.0 &&
4853abb08a53SJoe Perches		    $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ &&
4854abb08a53SJoe Perches		    $4 !~ /^\&/) { # $LvalOrFunc may be &foo, ignore if so
4855abb08a53SJoe Perches			WARN("MASK_THEN_SHIFT",
4856abb08a53SJoe Perches			     "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr);
4857abb08a53SJoe Perches		}
4858abb08a53SJoe Perches
4859b75ac618SJoe Perches# check for pointer comparisons to NULL
4860b75ac618SJoe Perches		if ($^V && $^V ge 5.10.0) {
4861b75ac618SJoe Perches			while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) {
4862b75ac618SJoe Perches				my $val = $1;
4863b75ac618SJoe Perches				my $equal = "!";
4864b75ac618SJoe Perches				$equal = "" if ($4 eq "!=");
4865b75ac618SJoe Perches				if (CHK("COMPARISON_TO_NULL",
4866b75ac618SJoe Perches					"Comparison to NULL could be written \"${equal}${val}\"\n" . $herecurr) &&
4867b75ac618SJoe Perches					    $fix) {
4868b75ac618SJoe Perches					$fixed[$fixlinenr] =~ s/\b\Q$val\E\s*(?:==|\!=)\s*NULL\b/$equal$val/;
4869b75ac618SJoe Perches				}
4870b75ac618SJoe Perches			}
4871b75ac618SJoe Perches		}
4872b75ac618SJoe Perches
48738716de38SJoe Perches# check for bad placement of section $InitAttribute (e.g.: __initdata)
48748716de38SJoe Perches		if ($line =~ /(\b$InitAttribute\b)/) {
48758716de38SJoe Perches			my $attr = $1;
48768716de38SJoe Perches			if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
48778716de38SJoe Perches				my $ptr = $1;
48788716de38SJoe Perches				my $var = $2;
48798716de38SJoe Perches				if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
48808716de38SJoe Perches				      ERROR("MISPLACED_INIT",
48818716de38SJoe Perches					    "$attr should be placed after $var\n" . $herecurr)) ||
48828716de38SJoe Perches				     ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
48838716de38SJoe Perches				      WARN("MISPLACED_INIT",
48848716de38SJoe Perches					   "$attr should be placed after $var\n" . $herecurr))) &&
48858716de38SJoe Perches				    $fix) {
4886194f66fcSJoe 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;
48878716de38SJoe Perches				}
48888716de38SJoe Perches			}
48898716de38SJoe Perches		}
48908716de38SJoe Perches
4891e970b884SJoe Perches# check for $InitAttributeData (ie: __initdata) with const
4892e970b884SJoe Perches		if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
4893e970b884SJoe Perches			my $attr = $1;
4894e970b884SJoe Perches			$attr =~ /($InitAttributePrefix)(.*)/;
4895e970b884SJoe Perches			my $attr_prefix = $1;
4896e970b884SJoe Perches			my $attr_type = $2;
4897e970b884SJoe Perches			if (ERROR("INIT_ATTRIBUTE",
4898e970b884SJoe Perches				  "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
4899e970b884SJoe Perches			    $fix) {
4900194f66fcSJoe Perches				$fixed[$fixlinenr] =~
4901e970b884SJoe Perches				    s/$InitAttributeData/${attr_prefix}initconst/;
4902e970b884SJoe Perches			}
4903e970b884SJoe Perches		}
4904e970b884SJoe Perches
4905e970b884SJoe Perches# check for $InitAttributeConst (ie: __initconst) without const
4906e970b884SJoe Perches		if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
4907e970b884SJoe Perches			my $attr = $1;
4908e970b884SJoe Perches			if (ERROR("INIT_ATTRIBUTE",
4909e970b884SJoe Perches				  "Use of $attr requires a separate use of const\n" . $herecurr) &&
4910e970b884SJoe Perches			    $fix) {
4911194f66fcSJoe Perches				my $lead = $fixed[$fixlinenr] =~
4912e970b884SJoe Perches				    /(^\+\s*(?:static\s+))/;
4913e970b884SJoe Perches				$lead = rtrim($1);
4914e970b884SJoe Perches				$lead = "$lead " if ($lead !~ /^\+$/);
4915e970b884SJoe Perches				$lead = "${lead}const ";
4916194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/;
4917e970b884SJoe Perches			}
4918e970b884SJoe Perches		}
4919e970b884SJoe Perches
4920c17893c7SJoe Perches# check for __read_mostly with const non-pointer (should just be const)
4921c17893c7SJoe Perches		if ($line =~ /\b__read_mostly\b/ &&
4922c17893c7SJoe Perches		    $line =~ /($Type)\s*$Ident/ && $1 !~ /\*\s*$/ && $1 =~ /\bconst\b/) {
4923c17893c7SJoe Perches			if (ERROR("CONST_READ_MOSTLY",
4924c17893c7SJoe Perches				  "Invalid use of __read_mostly with const type\n" . $herecurr) &&
4925c17893c7SJoe Perches			    $fix) {
4926c17893c7SJoe Perches				$fixed[$fixlinenr] =~ s/\s+__read_mostly\b//;
4927c17893c7SJoe Perches			}
4928c17893c7SJoe Perches		}
4929c17893c7SJoe Perches
4930fbdb8138SJoe Perches# don't use __constant_<foo> functions outside of include/uapi/
4931fbdb8138SJoe Perches		if ($realfile !~ m@^include/uapi/@ &&
4932fbdb8138SJoe Perches		    $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) {
4933fbdb8138SJoe Perches			my $constant_func = $1;
4934fbdb8138SJoe Perches			my $func = $constant_func;
4935fbdb8138SJoe Perches			$func =~ s/^__constant_//;
4936fbdb8138SJoe Perches			if (WARN("CONSTANT_CONVERSION",
4937fbdb8138SJoe Perches				 "$constant_func should be $func\n" . $herecurr) &&
4938fbdb8138SJoe Perches			    $fix) {
4939194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g;
4940fbdb8138SJoe Perches			}
4941fbdb8138SJoe Perches		}
4942fbdb8138SJoe Perches
49431a15a250SPatrick Pannuto# prefer usleep_range over udelay
494437581c28SBruce Allan		if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
494543c1d77cSJoe Perches			my $delay = $1;
49461a15a250SPatrick Pannuto			# ignore udelay's < 10, however
494743c1d77cSJoe Perches			if (! ($delay < 10) ) {
4948000d1cc1SJoe Perches				CHK("USLEEP_RANGE",
494943c1d77cSJoe Perches				    "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $herecurr);
495043c1d77cSJoe Perches			}
495143c1d77cSJoe Perches			if ($delay > 2000) {
495243c1d77cSJoe Perches				WARN("LONG_UDELAY",
495343c1d77cSJoe Perches				     "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr);
49541a15a250SPatrick Pannuto			}
49551a15a250SPatrick Pannuto		}
49561a15a250SPatrick Pannuto
495709ef8725SPatrick Pannuto# warn about unexpectedly long msleep's
495809ef8725SPatrick Pannuto		if ($line =~ /\bmsleep\s*\((\d+)\);/) {
495909ef8725SPatrick Pannuto			if ($1 < 20) {
4960000d1cc1SJoe Perches				WARN("MSLEEP",
496143c1d77cSJoe Perches				     "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $herecurr);
496209ef8725SPatrick Pannuto			}
496309ef8725SPatrick Pannuto		}
496409ef8725SPatrick Pannuto
496536ec1939SJoe Perches# check for comparisons of jiffies
496636ec1939SJoe Perches		if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
496736ec1939SJoe Perches			WARN("JIFFIES_COMPARISON",
496836ec1939SJoe Perches			     "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
496936ec1939SJoe Perches		}
497036ec1939SJoe Perches
49719d7a34a5SJoe Perches# check for comparisons of get_jiffies_64()
49729d7a34a5SJoe Perches		if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
49739d7a34a5SJoe Perches			WARN("JIFFIES_COMPARISON",
49749d7a34a5SJoe Perches			     "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
49759d7a34a5SJoe Perches		}
49769d7a34a5SJoe Perches
497700df344fSAndy Whitcroft# warn about #ifdefs in C files
4978c45dcabdSAndy Whitcroft#		if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
497900df344fSAndy Whitcroft#			print "#ifdef in C files should be avoided\n";
498000df344fSAndy Whitcroft#			print "$herecurr";
498100df344fSAndy Whitcroft#			$clean = 0;
498200df344fSAndy Whitcroft#		}
498300df344fSAndy Whitcroft
498422f2a2efSAndy Whitcroft# warn about spacing in #ifdefs
4985c45dcabdSAndy Whitcroft		if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
49863705ce5bSJoe Perches			if (ERROR("SPACING",
49873705ce5bSJoe Perches				  "exactly one space required after that #$1\n" . $herecurr) &&
49883705ce5bSJoe Perches			    $fix) {
4989194f66fcSJoe Perches				$fixed[$fixlinenr] =~
49903705ce5bSJoe Perches				    s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
49913705ce5bSJoe Perches			}
49923705ce5bSJoe Perches
499322f2a2efSAndy Whitcroft		}
499422f2a2efSAndy Whitcroft
49954a0df2efSAndy Whitcroft# check for spinlock_t definitions without a comment.
4996171ae1a4SAndy Whitcroft		if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
4997171ae1a4SAndy Whitcroft		    $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
49984a0df2efSAndy Whitcroft			my $which = $1;
49994a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
5000000d1cc1SJoe Perches				CHK("UNCOMMENTED_DEFINITION",
5001000d1cc1SJoe Perches				    "$1 definition without comment\n" . $herecurr);
50024a0df2efSAndy Whitcroft			}
50034a0df2efSAndy Whitcroft		}
50044a0df2efSAndy Whitcroft# check for memory barriers without a comment.
50054a0df2efSAndy Whitcroft		if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
50064a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
5007c1fd7bb9SJoe Perches				WARN("MEMORY_BARRIER",
5008000d1cc1SJoe Perches				     "memory barrier without comment\n" . $herecurr);
50094a0df2efSAndy Whitcroft			}
50104a0df2efSAndy Whitcroft		}
5011cb426e99SJoe Perches# check for waitqueue_active without a comment.
5012cb426e99SJoe Perches		if ($line =~ /\bwaitqueue_active\s*\(/) {
5013cb426e99SJoe Perches			if (!ctx_has_comment($first_line, $linenr)) {
5014cb426e99SJoe Perches				WARN("WAITQUEUE_ACTIVE",
5015cb426e99SJoe Perches				     "waitqueue_active without comment\n" . $herecurr);
5016cb426e99SJoe Perches			}
5017cb426e99SJoe Perches		}
50184a0df2efSAndy Whitcroft# check of hardware specific defines
5019c45dcabdSAndy Whitcroft		if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
5020000d1cc1SJoe Perches			CHK("ARCH_DEFINES",
5021000d1cc1SJoe Perches			    "architecture specific defines should be avoided\n" .  $herecurr);
50220a920b5bSAndy Whitcroft		}
5023653d4876SAndy Whitcroft
5024d4977c78STobias Klauser# Check that the storage class is at the beginning of a declaration
5025d4977c78STobias Klauser		if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
5026000d1cc1SJoe Perches			WARN("STORAGE_CLASS",
5027000d1cc1SJoe Perches			     "storage class should be at the beginning of the declaration\n" . $herecurr)
5028d4977c78STobias Klauser		}
5029d4977c78STobias Klauser
5030de7d4f0eSAndy Whitcroft# check the location of the inline attribute, that it is between
5031de7d4f0eSAndy Whitcroft# storage class and type.
50329c0ca6f9SAndy Whitcroft		if ($line =~ /\b$Type\s+$Inline\b/ ||
50339c0ca6f9SAndy Whitcroft		    $line =~ /\b$Inline\s+$Storage\b/) {
5034000d1cc1SJoe Perches			ERROR("INLINE_LOCATION",
5035000d1cc1SJoe Perches			      "inline keyword should sit between storage class and type\n" . $herecurr);
5036de7d4f0eSAndy Whitcroft		}
5037de7d4f0eSAndy Whitcroft
50388905a67cSAndy Whitcroft# Check for __inline__ and __inline, prefer inline
50392b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
50402b7ab453SJoe Perches		    $line =~ /\b(__inline__|__inline)\b/) {
5041d5e616fcSJoe Perches			if (WARN("INLINE",
5042d5e616fcSJoe Perches				 "plain inline is preferred over $1\n" . $herecurr) &&
5043d5e616fcSJoe Perches			    $fix) {
5044194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/;
5045d5e616fcSJoe Perches
5046d5e616fcSJoe Perches			}
50478905a67cSAndy Whitcroft		}
50488905a67cSAndy Whitcroft
50493d130fd0SJoe Perches# Check for __attribute__ packed, prefer __packed
50502b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
50512b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
5052000d1cc1SJoe Perches			WARN("PREFER_PACKED",
5053000d1cc1SJoe Perches			     "__packed is preferred over __attribute__((packed))\n" . $herecurr);
50543d130fd0SJoe Perches		}
50553d130fd0SJoe Perches
505639b7e287SJoe Perches# Check for __attribute__ aligned, prefer __aligned
50572b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
50582b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
5059000d1cc1SJoe Perches			WARN("PREFER_ALIGNED",
5060000d1cc1SJoe Perches			     "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
506139b7e287SJoe Perches		}
506239b7e287SJoe Perches
50635f14d3bdSJoe Perches# Check for __attribute__ format(printf, prefer __printf
50642b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
50652b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
5066d5e616fcSJoe Perches			if (WARN("PREFER_PRINTF",
5067d5e616fcSJoe Perches				 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) &&
5068d5e616fcSJoe Perches			    $fix) {
5069194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf\s*,\s*(.*)\)\s*\)\s*\)/"__printf(" . trim($1) . ")"/ex;
5070d5e616fcSJoe Perches
5071d5e616fcSJoe Perches			}
50725f14d3bdSJoe Perches		}
50735f14d3bdSJoe Perches
50746061d949SJoe Perches# Check for __attribute__ format(scanf, prefer __scanf
50752b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
50762b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
5077d5e616fcSJoe Perches			if (WARN("PREFER_SCANF",
5078d5e616fcSJoe Perches				 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) &&
5079d5e616fcSJoe Perches			    $fix) {
5080194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\s*,\s*(.*)\)\s*\)\s*\)/"__scanf(" . trim($1) . ")"/ex;
5081d5e616fcSJoe Perches			}
50826061d949SJoe Perches		}
50836061d949SJoe Perches
5084619a908aSJoe Perches# Check for __attribute__ weak, or __weak declarations (may have link issues)
5085619a908aSJoe Perches		if ($^V && $^V ge 5.10.0 &&
5086619a908aSJoe Perches		    $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ &&
5087619a908aSJoe Perches		    ($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ ||
5088619a908aSJoe Perches		     $line =~ /\b__weak\b/)) {
5089619a908aSJoe Perches			ERROR("WEAK_DECLARATION",
5090619a908aSJoe Perches			      "Using weak declarations can have unintended link defects\n" . $herecurr);
5091619a908aSJoe Perches		}
5092619a908aSJoe Perches
5093e6176fa4SJoe Perches# check for c99 types like uint8_t used outside of uapi/
5094e6176fa4SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
5095e6176fa4SJoe Perches		    $line =~ /\b($Declare)\s*$Ident\s*[=;,\[]/) {
5096e6176fa4SJoe Perches			my $type = $1;
5097e6176fa4SJoe Perches			if ($type =~ /\b($typeC99Typedefs)\b/) {
5098e6176fa4SJoe Perches				$type = $1;
5099e6176fa4SJoe Perches				my $kernel_type = 'u';
5100e6176fa4SJoe Perches				$kernel_type = 's' if ($type =~ /^_*[si]/);
5101e6176fa4SJoe Perches				$type =~ /(\d+)/;
5102e6176fa4SJoe Perches				$kernel_type .= $1;
5103e6176fa4SJoe Perches				if (CHK("PREFER_KERNEL_TYPES",
5104e6176fa4SJoe Perches					"Prefer kernel type '$kernel_type' over '$type'\n" . $herecurr) &&
5105e6176fa4SJoe Perches				    $fix) {
5106e6176fa4SJoe Perches					$fixed[$fixlinenr] =~ s/\b$type\b/$kernel_type/;
5107e6176fa4SJoe Perches				}
5108e6176fa4SJoe Perches			}
5109e6176fa4SJoe Perches		}
5110e6176fa4SJoe Perches
51118f53a9b8SJoe Perches# check for sizeof(&)
51128f53a9b8SJoe Perches		if ($line =~ /\bsizeof\s*\(\s*\&/) {
5113000d1cc1SJoe Perches			WARN("SIZEOF_ADDRESS",
5114000d1cc1SJoe Perches			     "sizeof(& should be avoided\n" . $herecurr);
51158f53a9b8SJoe Perches		}
51168f53a9b8SJoe Perches
511766c80b60SJoe Perches# check for sizeof without parenthesis
511866c80b60SJoe Perches		if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
5119d5e616fcSJoe Perches			if (WARN("SIZEOF_PARENTHESIS",
5120d5e616fcSJoe Perches				 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
5121d5e616fcSJoe Perches			    $fix) {
5122194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
5123d5e616fcSJoe Perches			}
512466c80b60SJoe Perches		}
512566c80b60SJoe Perches
512688982feaSJoe Perches# check for struct spinlock declarations
512788982feaSJoe Perches		if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
512888982feaSJoe Perches			WARN("USE_SPINLOCK_T",
512988982feaSJoe Perches			     "struct spinlock should be spinlock_t\n" . $herecurr);
513088982feaSJoe Perches		}
513188982feaSJoe Perches
5132a6962d72SJoe Perches# check for seq_printf uses that could be seq_puts
513306668727SJoe Perches		if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
5134a6962d72SJoe Perches			my $fmt = get_quoted_string($line, $rawline);
5135caac1d5fSHeba Aamer			$fmt =~ s/%%//g;
5136caac1d5fSHeba Aamer			if ($fmt !~ /%/) {
5137d5e616fcSJoe Perches				if (WARN("PREFER_SEQ_PUTS",
5138d5e616fcSJoe Perches					 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
5139d5e616fcSJoe Perches				    $fix) {
5140194f66fcSJoe Perches					$fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/;
5141d5e616fcSJoe Perches				}
5142a6962d72SJoe Perches			}
5143a6962d72SJoe Perches		}
5144a6962d72SJoe Perches
5145554e165cSAndy Whitcroft# Check for misused memsets
5146d1fe9c09SJoe Perches		if ($^V && $^V ge 5.10.0 &&
5147d1fe9c09SJoe Perches		    defined $stat &&
51489e20a853SMateusz Kulikowski		    $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/) {
5149554e165cSAndy Whitcroft
5150d7c76ba7SJoe Perches			my $ms_addr = $2;
5151d1fe9c09SJoe Perches			my $ms_val = $7;
5152d1fe9c09SJoe Perches			my $ms_size = $12;
5153d7c76ba7SJoe Perches
5154554e165cSAndy Whitcroft			if ($ms_size =~ /^(0x|)0$/i) {
5155554e165cSAndy Whitcroft				ERROR("MEMSET",
5156d7c76ba7SJoe Perches				      "memset to 0's uses 0 as the 2nd argument, not the 3rd\n" . "$here\n$stat\n");
5157554e165cSAndy Whitcroft			} elsif ($ms_size =~ /^(0x|)1$/i) {
5158554e165cSAndy Whitcroft				WARN("MEMSET",
5159d7c76ba7SJoe Perches				     "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
5160d7c76ba7SJoe Perches			}
5161d7c76ba7SJoe Perches		}
5162d7c76ba7SJoe Perches
516398a9bba5SJoe Perches# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar)
516498a9bba5SJoe Perches		if ($^V && $^V ge 5.10.0 &&
516510895d2cSMateusz Kulikowski		    defined $stat &&
516610895d2cSMateusz Kulikowski		    $stat =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
516798a9bba5SJoe Perches			if (WARN("PREFER_ETHER_ADDR_COPY",
516810895d2cSMateusz Kulikowski				 "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . "$here\n$stat\n") &&
516998a9bba5SJoe Perches			    $fix) {
5170194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/;
517198a9bba5SJoe Perches			}
517298a9bba5SJoe Perches		}
517398a9bba5SJoe Perches
5174b6117d17SMateusz Kulikowski# Check for memcmp(foo, bar, ETH_ALEN) that could be ether_addr_equal*(foo, bar)
5175b6117d17SMateusz Kulikowski		if ($^V && $^V ge 5.10.0 &&
5176b6117d17SMateusz Kulikowski		    defined $stat &&
5177b6117d17SMateusz Kulikowski		    $stat =~ /^\+(?:.*?)\bmemcmp\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
5178b6117d17SMateusz Kulikowski			WARN("PREFER_ETHER_ADDR_EQUAL",
5179b6117d17SMateusz Kulikowski			     "Prefer ether_addr_equal() or ether_addr_equal_unaligned() over memcmp()\n" . "$here\n$stat\n")
5180b6117d17SMateusz Kulikowski		}
5181b6117d17SMateusz Kulikowski
51828617cd09SMateusz Kulikowski# check for memset(foo, 0x0, ETH_ALEN) that could be eth_zero_addr
51838617cd09SMateusz Kulikowski# check for memset(foo, 0xFF, ETH_ALEN) that could be eth_broadcast_addr
51848617cd09SMateusz Kulikowski		if ($^V && $^V ge 5.10.0 &&
51858617cd09SMateusz Kulikowski		    defined $stat &&
51868617cd09SMateusz Kulikowski		    $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
51878617cd09SMateusz Kulikowski
51888617cd09SMateusz Kulikowski			my $ms_val = $7;
51898617cd09SMateusz Kulikowski
51908617cd09SMateusz Kulikowski			if ($ms_val =~ /^(?:0x|)0+$/i) {
51918617cd09SMateusz Kulikowski				if (WARN("PREFER_ETH_ZERO_ADDR",
51928617cd09SMateusz Kulikowski					 "Prefer eth_zero_addr over memset()\n" . "$here\n$stat\n") &&
51938617cd09SMateusz Kulikowski				    $fix) {
51948617cd09SMateusz Kulikowski					$fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_zero_addr($2)/;
51958617cd09SMateusz Kulikowski				}
51968617cd09SMateusz Kulikowski			} elsif ($ms_val =~ /^(?:0xff|255)$/i) {
51978617cd09SMateusz Kulikowski				if (WARN("PREFER_ETH_BROADCAST_ADDR",
51988617cd09SMateusz Kulikowski					 "Prefer eth_broadcast_addr() over memset()\n" . "$here\n$stat\n") &&
51998617cd09SMateusz Kulikowski				    $fix) {
52008617cd09SMateusz Kulikowski					$fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_broadcast_addr($2)/;
52018617cd09SMateusz Kulikowski				}
52028617cd09SMateusz Kulikowski			}
52038617cd09SMateusz Kulikowski		}
52048617cd09SMateusz Kulikowski
5205d7c76ba7SJoe Perches# typecasts on min/max could be min_t/max_t
5206d1fe9c09SJoe Perches		if ($^V && $^V ge 5.10.0 &&
5207d1fe9c09SJoe Perches		    defined $stat &&
5208d7c76ba7SJoe Perches		    $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
5209d1fe9c09SJoe Perches			if (defined $2 || defined $7) {
5210d7c76ba7SJoe Perches				my $call = $1;
5211d7c76ba7SJoe Perches				my $cast1 = deparenthesize($2);
5212d7c76ba7SJoe Perches				my $arg1 = $3;
5213d1fe9c09SJoe Perches				my $cast2 = deparenthesize($7);
5214d1fe9c09SJoe Perches				my $arg2 = $8;
5215d7c76ba7SJoe Perches				my $cast;
5216d7c76ba7SJoe Perches
5217d1fe9c09SJoe Perches				if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
5218d7c76ba7SJoe Perches					$cast = "$cast1 or $cast2";
5219d7c76ba7SJoe Perches				} elsif ($cast1 ne "") {
5220d7c76ba7SJoe Perches					$cast = $cast1;
5221d7c76ba7SJoe Perches				} else {
5222d7c76ba7SJoe Perches					$cast = $cast2;
5223d7c76ba7SJoe Perches				}
5224d7c76ba7SJoe Perches				WARN("MINMAX",
5225d7c76ba7SJoe Perches				     "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
5226554e165cSAndy Whitcroft			}
5227554e165cSAndy Whitcroft		}
5228554e165cSAndy Whitcroft
52294a273195SJoe Perches# check usleep_range arguments
52304a273195SJoe Perches		if ($^V && $^V ge 5.10.0 &&
52314a273195SJoe Perches		    defined $stat &&
52324a273195SJoe Perches		    $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
52334a273195SJoe Perches			my $min = $1;
52344a273195SJoe Perches			my $max = $7;
52354a273195SJoe Perches			if ($min eq $max) {
52364a273195SJoe Perches				WARN("USLEEP_RANGE",
52374a273195SJoe Perches				     "usleep_range should not use min == max args; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
52384a273195SJoe Perches			} elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
52394a273195SJoe Perches				 $min > $max) {
52404a273195SJoe Perches				WARN("USLEEP_RANGE",
52414a273195SJoe Perches				     "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
52424a273195SJoe Perches			}
52434a273195SJoe Perches		}
52444a273195SJoe Perches
5245823b794cSJoe Perches# check for naked sscanf
5246823b794cSJoe Perches		if ($^V && $^V ge 5.10.0 &&
5247823b794cSJoe Perches		    defined $stat &&
52486c8bd707SJoe Perches		    $line =~ /\bsscanf\b/ &&
5249823b794cSJoe Perches		    ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
5250823b794cSJoe Perches		     $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ &&
5251823b794cSJoe Perches		     $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) {
5252823b794cSJoe Perches			my $lc = $stat =~ tr@\n@@;
5253823b794cSJoe Perches			$lc = $lc + $linenr;
5254823b794cSJoe Perches			my $stat_real = raw_line($linenr, 0);
5255823b794cSJoe Perches		        for (my $count = $linenr + 1; $count <= $lc; $count++) {
5256823b794cSJoe Perches				$stat_real = $stat_real . "\n" . raw_line($count, 0);
5257823b794cSJoe Perches			}
5258823b794cSJoe Perches			WARN("NAKED_SSCANF",
5259823b794cSJoe Perches			     "unchecked sscanf return value\n" . "$here\n$stat_real\n");
5260823b794cSJoe Perches		}
5261823b794cSJoe Perches
5262afc819abSJoe Perches# check for simple sscanf that should be kstrto<foo>
5263afc819abSJoe Perches		if ($^V && $^V ge 5.10.0 &&
5264afc819abSJoe Perches		    defined $stat &&
5265afc819abSJoe Perches		    $line =~ /\bsscanf\b/) {
5266afc819abSJoe Perches			my $lc = $stat =~ tr@\n@@;
5267afc819abSJoe Perches			$lc = $lc + $linenr;
5268afc819abSJoe Perches			my $stat_real = raw_line($linenr, 0);
5269afc819abSJoe Perches		        for (my $count = $linenr + 1; $count <= $lc; $count++) {
5270afc819abSJoe Perches				$stat_real = $stat_real . "\n" . raw_line($count, 0);
5271afc819abSJoe Perches			}
5272afc819abSJoe Perches			if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) {
5273afc819abSJoe Perches				my $format = $6;
5274afc819abSJoe Perches				my $count = $format =~ tr@%@%@;
5275afc819abSJoe Perches				if ($count == 1 &&
5276afc819abSJoe Perches				    $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) {
5277afc819abSJoe Perches					WARN("SSCANF_TO_KSTRTO",
5278afc819abSJoe Perches					     "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n");
5279afc819abSJoe Perches				}
5280afc819abSJoe Perches			}
5281afc819abSJoe Perches		}
5282afc819abSJoe Perches
528370dc8a48SJoe Perches# check for new externs in .h files.
528470dc8a48SJoe Perches		if ($realfile =~ /\.h$/ &&
528570dc8a48SJoe Perches		    $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
5286d1d85780SJoe Perches			if (CHK("AVOID_EXTERNS",
528770dc8a48SJoe Perches				"extern prototypes should be avoided in .h files\n" . $herecurr) &&
528870dc8a48SJoe Perches			    $fix) {
5289194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
529070dc8a48SJoe Perches			}
529170dc8a48SJoe Perches		}
529270dc8a48SJoe Perches
5293de7d4f0eSAndy Whitcroft# check for new externs in .c files.
5294171ae1a4SAndy Whitcroft		if ($realfile =~ /\.c$/ && defined $stat &&
5295c45dcabdSAndy Whitcroft		    $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
5296171ae1a4SAndy Whitcroft		{
5297c45dcabdSAndy Whitcroft			my $function_name = $1;
5298c45dcabdSAndy Whitcroft			my $paren_space = $2;
5299171ae1a4SAndy Whitcroft
5300171ae1a4SAndy Whitcroft			my $s = $stat;
5301171ae1a4SAndy Whitcroft			if (defined $cond) {
5302171ae1a4SAndy Whitcroft				substr($s, 0, length($cond), '');
5303171ae1a4SAndy Whitcroft			}
5304c45dcabdSAndy Whitcroft			if ($s =~ /^\s*;/ &&
5305c45dcabdSAndy Whitcroft			    $function_name ne 'uninitialized_var')
5306c45dcabdSAndy Whitcroft			{
5307000d1cc1SJoe Perches				WARN("AVOID_EXTERNS",
5308000d1cc1SJoe Perches				     "externs should be avoided in .c files\n" .  $herecurr);
5309de7d4f0eSAndy Whitcroft			}
5310de7d4f0eSAndy Whitcroft
5311171ae1a4SAndy Whitcroft			if ($paren_space =~ /\n/) {
5312000d1cc1SJoe Perches				WARN("FUNCTION_ARGUMENTS",
5313000d1cc1SJoe Perches				     "arguments for function declarations should follow identifier\n" . $herecurr);
5314171ae1a4SAndy Whitcroft			}
53159c9ba34eSAndy Whitcroft
53169c9ba34eSAndy Whitcroft		} elsif ($realfile =~ /\.c$/ && defined $stat &&
53179c9ba34eSAndy Whitcroft		    $stat =~ /^.\s*extern\s+/)
53189c9ba34eSAndy Whitcroft		{
5319000d1cc1SJoe Perches			WARN("AVOID_EXTERNS",
5320000d1cc1SJoe Perches			     "externs should be avoided in .c files\n" .  $herecurr);
5321171ae1a4SAndy Whitcroft		}
5322171ae1a4SAndy Whitcroft
5323de7d4f0eSAndy Whitcroft# checks for new __setup's
5324de7d4f0eSAndy Whitcroft		if ($rawline =~ /\b__setup\("([^"]*)"/) {
5325de7d4f0eSAndy Whitcroft			my $name = $1;
5326de7d4f0eSAndy Whitcroft
5327de7d4f0eSAndy Whitcroft			if (!grep(/$name/, @setup_docs)) {
5328000d1cc1SJoe Perches				CHK("UNDOCUMENTED_SETUP",
5329000d1cc1SJoe Perches				    "__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
5330de7d4f0eSAndy Whitcroft			}
5331653d4876SAndy Whitcroft		}
53329c0ca6f9SAndy Whitcroft
53339c0ca6f9SAndy Whitcroft# check for pointless casting of kmalloc return
5334caf2a54fSJoe Perches		if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) {
5335000d1cc1SJoe Perches			WARN("UNNECESSARY_CASTS",
5336000d1cc1SJoe Perches			     "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
53379c0ca6f9SAndy Whitcroft		}
533813214adfSAndy Whitcroft
5339a640d25cSJoe Perches# alloc style
5340a640d25cSJoe Perches# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
5341a640d25cSJoe Perches		if ($^V && $^V ge 5.10.0 &&
5342a640d25cSJoe Perches		    $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*([kv][mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
5343a640d25cSJoe Perches			CHK("ALLOC_SIZEOF_STRUCT",
5344a640d25cSJoe Perches			    "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
5345a640d25cSJoe Perches		}
5346a640d25cSJoe Perches
534760a55369SJoe Perches# check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc
534860a55369SJoe Perches		if ($^V && $^V ge 5.10.0 &&
5349e367455aSJoe Perches		    $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*(k[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)\s*,/) {
535060a55369SJoe Perches			my $oldfunc = $3;
535160a55369SJoe Perches			my $a1 = $4;
535260a55369SJoe Perches			my $a2 = $10;
535360a55369SJoe Perches			my $newfunc = "kmalloc_array";
535460a55369SJoe Perches			$newfunc = "kcalloc" if ($oldfunc eq "kzalloc");
535560a55369SJoe Perches			my $r1 = $a1;
535660a55369SJoe Perches			my $r2 = $a2;
535760a55369SJoe Perches			if ($a1 =~ /^sizeof\s*\S/) {
535860a55369SJoe Perches				$r1 = $a2;
535960a55369SJoe Perches				$r2 = $a1;
536060a55369SJoe Perches			}
5361e367455aSJoe Perches			if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ &&
5362e367455aSJoe Perches			    !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) {
5363e367455aSJoe Perches				if (WARN("ALLOC_WITH_MULTIPLY",
5364e367455aSJoe Perches					 "Prefer $newfunc over $oldfunc with multiply\n" . $herecurr) &&
5365e367455aSJoe Perches				    $fix) {
5366194f66fcSJoe 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;
536760a55369SJoe Perches
536860a55369SJoe Perches				}
536960a55369SJoe Perches			}
537060a55369SJoe Perches		}
537160a55369SJoe Perches
5372972fdea2SJoe Perches# check for krealloc arg reuse
5373972fdea2SJoe Perches		if ($^V && $^V ge 5.10.0 &&
5374972fdea2SJoe Perches		    $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*\1\s*,/) {
5375972fdea2SJoe Perches			WARN("KREALLOC_ARG_REUSE",
5376972fdea2SJoe Perches			     "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
5377972fdea2SJoe Perches		}
5378972fdea2SJoe Perches
53795ce59ae0SJoe Perches# check for alloc argument mismatch
53805ce59ae0SJoe Perches		if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
53815ce59ae0SJoe Perches			WARN("ALLOC_ARRAY_ARGS",
53825ce59ae0SJoe Perches			     "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
53835ce59ae0SJoe Perches		}
53845ce59ae0SJoe Perches
5385caf2a54fSJoe Perches# check for multiple semicolons
5386caf2a54fSJoe Perches		if ($line =~ /;\s*;\s*$/) {
5387d5e616fcSJoe Perches			if (WARN("ONE_SEMICOLON",
5388d5e616fcSJoe Perches				 "Statements terminations use 1 semicolon\n" . $herecurr) &&
5389d5e616fcSJoe Perches			    $fix) {
5390194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g;
5391d5e616fcSJoe Perches			}
5392d1e2ad07SJoe Perches		}
5393d1e2ad07SJoe Perches
53940ab90191SJoe Perches# check for #defines like: 1 << <digit> that could be BIT(digit)
53950ab90191SJoe Perches		if ($line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) {
53960ab90191SJoe Perches			my $ull = "";
53970ab90191SJoe Perches			$ull = "_ULL" if (defined($1) && $1 =~ /ll/i);
53980ab90191SJoe Perches			if (CHK("BIT_MACRO",
53990ab90191SJoe Perches				"Prefer using the BIT$ull macro\n" . $herecurr) &&
54000ab90191SJoe Perches			    $fix) {
54010ab90191SJoe Perches				$fixed[$fixlinenr] =~ s/\(?\s*1\s*[ulUL]*\s*<<\s*(\d+|$Ident)\s*\)?/BIT${ull}($1)/;
54020ab90191SJoe Perches			}
54030ab90191SJoe Perches		}
54040ab90191SJoe Perches
5405e81f239bSJoe Perches# check for case / default statements not preceded by break/fallthrough/switch
5406c34c09a8SJoe Perches		if ($line =~ /^.\s*(?:case\s+(?:$Ident|$Constant)\s*|default):/) {
5407c34c09a8SJoe Perches			my $has_break = 0;
5408c34c09a8SJoe Perches			my $has_statement = 0;
5409c34c09a8SJoe Perches			my $count = 0;
5410c34c09a8SJoe Perches			my $prevline = $linenr;
5411e81f239bSJoe Perches			while ($prevline > 1 && ($file || $count < 3) && !$has_break) {
5412c34c09a8SJoe Perches				$prevline--;
5413c34c09a8SJoe Perches				my $rline = $rawlines[$prevline - 1];
5414c34c09a8SJoe Perches				my $fline = $lines[$prevline - 1];
5415c34c09a8SJoe Perches				last if ($fline =~ /^\@\@/);
5416c34c09a8SJoe Perches				next if ($fline =~ /^\-/);
5417c34c09a8SJoe Perches				next if ($fline =~ /^.(?:\s*(?:case\s+(?:$Ident|$Constant)[\s$;]*|default):[\s$;]*)*$/);
5418c34c09a8SJoe Perches				$has_break = 1 if ($rline =~ /fall[\s_-]*(through|thru)/i);
5419c34c09a8SJoe Perches				next if ($fline =~ /^.[\s$;]*$/);
5420c34c09a8SJoe Perches				$has_statement = 1;
5421c34c09a8SJoe Perches				$count++;
5422c34c09a8SJoe Perches				$has_break = 1 if ($fline =~ /\bswitch\b|\b(?:break\s*;[\s$;]*$|return\b|goto\b|continue\b)/);
5423c34c09a8SJoe Perches			}
5424c34c09a8SJoe Perches			if (!$has_break && $has_statement) {
5425c34c09a8SJoe Perches				WARN("MISSING_BREAK",
5426c34c09a8SJoe Perches				     "Possible switch case/default not preceeded by break or fallthrough comment\n" . $herecurr);
5427c34c09a8SJoe Perches			}
5428c34c09a8SJoe Perches		}
5429c34c09a8SJoe Perches
5430d1e2ad07SJoe Perches# check for switch/default statements without a break;
5431d1e2ad07SJoe Perches		if ($^V && $^V ge 5.10.0 &&
5432d1e2ad07SJoe Perches		    defined $stat &&
5433d1e2ad07SJoe Perches		    $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
5434d1e2ad07SJoe Perches			my $ctx = '';
5435d1e2ad07SJoe Perches			my $herectx = $here . "\n";
5436d1e2ad07SJoe Perches			my $cnt = statement_rawlines($stat);
5437d1e2ad07SJoe Perches			for (my $n = 0; $n < $cnt; $n++) {
5438d1e2ad07SJoe Perches				$herectx .= raw_line($linenr, $n) . "\n";
5439d1e2ad07SJoe Perches			}
5440d1e2ad07SJoe Perches			WARN("DEFAULT_NO_BREAK",
5441d1e2ad07SJoe Perches			     "switch default: should use break\n" . $herectx);
5442caf2a54fSJoe Perches		}
5443caf2a54fSJoe Perches
544413214adfSAndy Whitcroft# check for gcc specific __FUNCTION__
5445d5e616fcSJoe Perches		if ($line =~ /\b__FUNCTION__\b/) {
5446d5e616fcSJoe Perches			if (WARN("USE_FUNC",
5447d5e616fcSJoe Perches				 "__func__ should be used instead of gcc specific __FUNCTION__\n"  . $herecurr) &&
5448d5e616fcSJoe Perches			    $fix) {
5449194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g;
5450d5e616fcSJoe Perches			}
545113214adfSAndy Whitcroft		}
5452773647a0SAndy Whitcroft
545362ec818fSJoe Perches# check for uses of __DATE__, __TIME__, __TIMESTAMP__
545462ec818fSJoe Perches		while ($line =~ /\b(__(?:DATE|TIME|TIMESTAMP)__)\b/g) {
545562ec818fSJoe Perches			ERROR("DATE_TIME",
545662ec818fSJoe Perches			      "Use of the '$1' macro makes the build non-deterministic\n" . $herecurr);
545762ec818fSJoe Perches		}
545862ec818fSJoe Perches
54592c92488aSJoe Perches# check for use of yield()
54602c92488aSJoe Perches		if ($line =~ /\byield\s*\(\s*\)/) {
54612c92488aSJoe Perches			WARN("YIELD",
54622c92488aSJoe Perches			     "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n"  . $herecurr);
54632c92488aSJoe Perches		}
54642c92488aSJoe Perches
5465179f8f40SJoe Perches# check for comparisons against true and false
5466179f8f40SJoe Perches		if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
5467179f8f40SJoe Perches			my $lead = $1;
5468179f8f40SJoe Perches			my $arg = $2;
5469179f8f40SJoe Perches			my $test = $3;
5470179f8f40SJoe Perches			my $otype = $4;
5471179f8f40SJoe Perches			my $trail = $5;
5472179f8f40SJoe Perches			my $op = "!";
5473179f8f40SJoe Perches
5474179f8f40SJoe Perches			($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
5475179f8f40SJoe Perches
5476179f8f40SJoe Perches			my $type = lc($otype);
5477179f8f40SJoe Perches			if ($type =~ /^(?:true|false)$/) {
5478179f8f40SJoe Perches				if (("$test" eq "==" && "$type" eq "true") ||
5479179f8f40SJoe Perches				    ("$test" eq "!=" && "$type" eq "false")) {
5480179f8f40SJoe Perches					$op = "";
5481179f8f40SJoe Perches				}
5482179f8f40SJoe Perches
5483179f8f40SJoe Perches				CHK("BOOL_COMPARISON",
5484179f8f40SJoe Perches				    "Using comparison to $otype is error prone\n" . $herecurr);
5485179f8f40SJoe Perches
5486179f8f40SJoe Perches## maybe suggesting a correct construct would better
5487179f8f40SJoe Perches##				    "Using comparison to $otype is error prone.  Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
5488179f8f40SJoe Perches
5489179f8f40SJoe Perches			}
5490179f8f40SJoe Perches		}
5491179f8f40SJoe Perches
54924882720bSThomas Gleixner# check for semaphores initialized locked
54934882720bSThomas Gleixner		if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
5494000d1cc1SJoe Perches			WARN("CONSIDER_COMPLETION",
5495000d1cc1SJoe Perches			     "consider using a completion\n" . $herecurr);
5496773647a0SAndy Whitcroft		}
54976712d858SJoe Perches
549867d0a075SJoe Perches# recommend kstrto* over simple_strto* and strict_strto*
549967d0a075SJoe Perches		if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
5500000d1cc1SJoe Perches			WARN("CONSIDER_KSTRTO",
550167d0a075SJoe Perches			     "$1 is obsolete, use k$3 instead\n" . $herecurr);
5502773647a0SAndy Whitcroft		}
55036712d858SJoe Perches
5504ae3ccc46SFabian Frederick# check for __initcall(), use device_initcall() explicitly or more appropriate function please
5505f3db6639SMichael Ellerman		if ($line =~ /^.\s*__initcall\s*\(/) {
5506000d1cc1SJoe Perches			WARN("USE_DEVICE_INITCALL",
5507ae3ccc46SFabian Frederick			     "please use device_initcall() or more appropriate function instead of __initcall() (see include/linux/init.h)\n" . $herecurr);
5508f3db6639SMichael Ellerman		}
55096712d858SJoe Perches
55100f3c5aabSJoe Perches# check for various structs that are normally const (ops, kgdb, device_tree)
55110f3c5aabSJoe Perches		my $const_structs = qr{
55120f3c5aabSJoe Perches				acpi_dock_ops|
551379404849SEmese Revfy				address_space_operations|
551479404849SEmese Revfy				backlight_ops|
551579404849SEmese Revfy				block_device_operations|
551679404849SEmese Revfy				dentry_operations|
551779404849SEmese Revfy				dev_pm_ops|
551879404849SEmese Revfy				dma_map_ops|
551979404849SEmese Revfy				extent_io_ops|
552079404849SEmese Revfy				file_lock_operations|
552179404849SEmese Revfy				file_operations|
552279404849SEmese Revfy				hv_ops|
552379404849SEmese Revfy				ide_dma_ops|
552479404849SEmese Revfy				intel_dvo_dev_ops|
552579404849SEmese Revfy				item_operations|
552679404849SEmese Revfy				iwl_ops|
552779404849SEmese Revfy				kgdb_arch|
552879404849SEmese Revfy				kgdb_io|
552979404849SEmese Revfy				kset_uevent_ops|
553079404849SEmese Revfy				lock_manager_operations|
553179404849SEmese Revfy				microcode_ops|
553279404849SEmese Revfy				mtrr_ops|
553379404849SEmese Revfy				neigh_ops|
553479404849SEmese Revfy				nlmsvc_binding|
55350f3c5aabSJoe Perches				of_device_id|
553679404849SEmese Revfy				pci_raw_ops|
553779404849SEmese Revfy				pipe_buf_operations|
553879404849SEmese Revfy				platform_hibernation_ops|
553979404849SEmese Revfy				platform_suspend_ops|
554079404849SEmese Revfy				proto_ops|
554179404849SEmese Revfy				rpc_pipe_ops|
554279404849SEmese Revfy				seq_operations|
554379404849SEmese Revfy				snd_ac97_build_ops|
554479404849SEmese Revfy				soc_pcmcia_socket_ops|
554579404849SEmese Revfy				stacktrace_ops|
554679404849SEmese Revfy				sysfs_ops|
554779404849SEmese Revfy				tty_operations|
55486d07d01bSJoe Perches				uart_ops|
554979404849SEmese Revfy				usb_mon_operations|
555079404849SEmese Revfy				wd_ops}x;
55516903ffb2SAndy Whitcroft		if ($line !~ /\bconst\b/ &&
55520f3c5aabSJoe Perches		    $line =~ /\bstruct\s+($const_structs)\b/) {
5553000d1cc1SJoe Perches			WARN("CONST_STRUCT",
5554000d1cc1SJoe Perches			     "struct $1 should normally be const\n" .
55556903ffb2SAndy Whitcroft				$herecurr);
55562b6db5cbSAndy Whitcroft		}
5557773647a0SAndy Whitcroft
5558773647a0SAndy Whitcroft# use of NR_CPUS is usually wrong
5559773647a0SAndy Whitcroft# ignore definitions of NR_CPUS and usage to define arrays as likely right
5560773647a0SAndy Whitcroft		if ($line =~ /\bNR_CPUS\b/ &&
5561c45dcabdSAndy Whitcroft		    $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
5562c45dcabdSAndy Whitcroft		    $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
5563171ae1a4SAndy Whitcroft		    $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
5564171ae1a4SAndy Whitcroft		    $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
5565171ae1a4SAndy Whitcroft		    $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
5566773647a0SAndy Whitcroft		{
5567000d1cc1SJoe Perches			WARN("NR_CPUS",
5568000d1cc1SJoe Perches			     "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
5569773647a0SAndy Whitcroft		}
55709c9ba34eSAndy Whitcroft
557152ea8506SJoe Perches# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
557252ea8506SJoe Perches		if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
557352ea8506SJoe Perches			ERROR("DEFINE_ARCH_HAS",
557452ea8506SJoe Perches			      "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
557552ea8506SJoe Perches		}
557652ea8506SJoe Perches
5577acd9362cSJoe Perches# likely/unlikely comparisons similar to "(likely(foo) > 0)"
5578acd9362cSJoe Perches		if ($^V && $^V ge 5.10.0 &&
5579acd9362cSJoe Perches		    $line =~ /\b((?:un)?likely)\s*\(\s*$FuncArg\s*\)\s*$Compare/) {
5580acd9362cSJoe Perches			WARN("LIKELY_MISUSE",
5581acd9362cSJoe Perches			     "Using $1 should generally have parentheses around the comparison\n" . $herecurr);
5582acd9362cSJoe Perches		}
5583acd9362cSJoe Perches
5584691d77b6SAndy Whitcroft# whine mightly about in_atomic
5585691d77b6SAndy Whitcroft		if ($line =~ /\bin_atomic\s*\(/) {
5586691d77b6SAndy Whitcroft			if ($realfile =~ m@^drivers/@) {
5587000d1cc1SJoe Perches				ERROR("IN_ATOMIC",
5588000d1cc1SJoe Perches				      "do not use in_atomic in drivers\n" . $herecurr);
5589f4a87736SAndy Whitcroft			} elsif ($realfile !~ m@^kernel/@) {
5590000d1cc1SJoe Perches				WARN("IN_ATOMIC",
5591000d1cc1SJoe Perches				     "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
5592691d77b6SAndy Whitcroft			}
5593691d77b6SAndy Whitcroft		}
55941704f47bSPeter Zijlstra
55951704f47bSPeter Zijlstra# check for lockdep_set_novalidate_class
55961704f47bSPeter Zijlstra		if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
55971704f47bSPeter Zijlstra		    $line =~ /__lockdep_no_validate__\s*\)/ ) {
55981704f47bSPeter Zijlstra			if ($realfile !~ m@^kernel/lockdep@ &&
55991704f47bSPeter Zijlstra			    $realfile !~ m@^include/linux/lockdep@ &&
56001704f47bSPeter Zijlstra			    $realfile !~ m@^drivers/base/core@) {
5601000d1cc1SJoe Perches				ERROR("LOCKDEP",
5602000d1cc1SJoe Perches				      "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
56031704f47bSPeter Zijlstra			}
56041704f47bSPeter Zijlstra		}
560588f8831cSDave Jones
5606b392c64fSJoe Perches		if ($line =~ /debugfs_create_\w+.*\b$mode_perms_world_writable\b/ ||
5607b392c64fSJoe Perches		    $line =~ /DEVICE_ATTR.*\b$mode_perms_world_writable\b/) {
5608000d1cc1SJoe Perches			WARN("EXPORTED_WORLD_WRITABLE",
5609000d1cc1SJoe Perches			     "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
561088f8831cSDave Jones		}
56112435880fSJoe Perches
5612515a235eSJoe Perches# Mode permission misuses where it seems decimal should be octal
5613515a235eSJoe Perches# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop
5614515a235eSJoe Perches		if ($^V && $^V ge 5.10.0 &&
5615515a235eSJoe Perches		    $line =~ /$mode_perms_search/) {
56162435880fSJoe Perches			foreach my $entry (@mode_permission_funcs) {
56172435880fSJoe Perches				my $func = $entry->[0];
56182435880fSJoe Perches				my $arg_pos = $entry->[1];
56192435880fSJoe Perches
56202435880fSJoe Perches				my $skip_args = "";
56212435880fSJoe Perches				if ($arg_pos > 1) {
56222435880fSJoe Perches					$arg_pos--;
56232435880fSJoe Perches					$skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}";
56242435880fSJoe Perches				}
56252435880fSJoe Perches				my $test = "\\b$func\\s*\\(${skip_args}([\\d]+)\\s*[,\\)]";
5626515a235eSJoe Perches				if ($line =~ /$test/) {
56272435880fSJoe Perches					my $val = $1;
56282435880fSJoe Perches					$val = $6 if ($skip_args ne "");
56292435880fSJoe Perches
56301727cc70SJoe Perches					if ($val !~ /^0$/ &&
56311727cc70SJoe Perches					    (($val =~ /^$Int$/ && $val !~ /^$Octal$/) ||
56321727cc70SJoe Perches					     length($val) ne 4)) {
56332435880fSJoe Perches						ERROR("NON_OCTAL_PERMISSIONS",
56341727cc70SJoe Perches						      "Use 4 digit octal (0777) not decimal permissions\n" . $herecurr);
5635c0a5c898SJoe Perches					} elsif ($val =~ /^$Octal$/ && (oct($val) & 02)) {
5636c0a5c898SJoe Perches						ERROR("EXPORTED_WORLD_WRITABLE",
5637c0a5c898SJoe Perches						      "Exporting writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
56382435880fSJoe Perches					}
56392435880fSJoe Perches				}
56402435880fSJoe Perches			}
564113214adfSAndy Whitcroft		}
56425a6d20ceSBjorn Andersson
56435a6d20ceSBjorn Andersson# validate content of MODULE_LICENSE against list from include/linux/module.h
56445a6d20ceSBjorn Andersson		if ($line =~ /\bMODULE_LICENSE\s*\(\s*($String)\s*\)/) {
56455a6d20ceSBjorn Andersson			my $extracted_string = get_quoted_string($line, $rawline);
56465a6d20ceSBjorn Andersson			my $valid_licenses = qr{
56475a6d20ceSBjorn Andersson						GPL|
56485a6d20ceSBjorn Andersson						GPL\ v2|
56495a6d20ceSBjorn Andersson						GPL\ and\ additional\ rights|
56505a6d20ceSBjorn Andersson						Dual\ BSD/GPL|
56515a6d20ceSBjorn Andersson						Dual\ MIT/GPL|
56525a6d20ceSBjorn Andersson						Dual\ MPL/GPL|
56535a6d20ceSBjorn Andersson						Proprietary
56545a6d20ceSBjorn Andersson					}x;
56555a6d20ceSBjorn Andersson			if ($extracted_string !~ /^"(?:$valid_licenses)"$/x) {
56565a6d20ceSBjorn Andersson				WARN("MODULE_LICENSE",
56575a6d20ceSBjorn Andersson				     "unknown module license " . $extracted_string . "\n" . $herecurr);
56585a6d20ceSBjorn Andersson			}
56595a6d20ceSBjorn Andersson		}
5660515a235eSJoe Perches	}
566113214adfSAndy Whitcroft
566213214adfSAndy Whitcroft	# If we have no input at all, then there is nothing to report on
566313214adfSAndy Whitcroft	# so just keep quiet.
566413214adfSAndy Whitcroft	if ($#rawlines == -1) {
566513214adfSAndy Whitcroft		exit(0);
56660a920b5bSAndy Whitcroft	}
56670a920b5bSAndy Whitcroft
56688905a67cSAndy Whitcroft	# In mailback mode only produce a report in the negative, for
56698905a67cSAndy Whitcroft	# things that appear to be patches.
56708905a67cSAndy Whitcroft	if ($mailback && ($clean == 1 || !$is_patch)) {
56718905a67cSAndy Whitcroft		exit(0);
56728905a67cSAndy Whitcroft	}
56738905a67cSAndy Whitcroft
56748905a67cSAndy Whitcroft	# This is not a patch, and we are are in 'no-patch' mode so
56758905a67cSAndy Whitcroft	# just keep quiet.
56768905a67cSAndy Whitcroft	if (!$chk_patch && !$is_patch) {
56778905a67cSAndy Whitcroft		exit(0);
56788905a67cSAndy Whitcroft	}
56798905a67cSAndy Whitcroft
568006330fc4SJoe Perches	if (!$is_patch && $file !~ /cover-letter\.patch$/) {
5681000d1cc1SJoe Perches		ERROR("NOT_UNIFIED_DIFF",
5682000d1cc1SJoe Perches		      "Does not appear to be a unified-diff format patch\n");
56830a920b5bSAndy Whitcroft	}
568434d8815fSJoe Perches	if ($is_patch && $filename ne '-' && $chk_signoff && $signoff == 0) {
5685000d1cc1SJoe Perches		ERROR("MISSING_SIGN_OFF",
5686000d1cc1SJoe Perches		      "Missing Signed-off-by: line(s)\n");
56870a920b5bSAndy Whitcroft	}
56880a920b5bSAndy Whitcroft
5689f0a594c1SAndy Whitcroft	print report_dump();
569013214adfSAndy Whitcroft	if ($summary && !($clean == 1 && $quiet == 1)) {
569113214adfSAndy Whitcroft		print "$filename " if ($summary_file);
56926c72ffaaSAndy Whitcroft		print "total: $cnt_error errors, $cnt_warn warnings, " .
56936c72ffaaSAndy Whitcroft			(($check)? "$cnt_chk checks, " : "") .
56946c72ffaaSAndy Whitcroft			"$cnt_lines lines checked\n";
56956c72ffaaSAndy Whitcroft	}
56968905a67cSAndy Whitcroft
5697d2c0a235SAndy Whitcroft	if ($quiet == 0) {
5698d2c0a235SAndy Whitcroft		# If there were whitespace errors which cleanpatch can fix
5699d2c0a235SAndy Whitcroft		# then suggest that.
5700d2c0a235SAndy Whitcroft		if ($rpt_cleaners) {
5701b0781216SMike Frysinger			$rpt_cleaners = 0;
5702d8469f16SJoe Perches			print << "EOM"
5703d8469f16SJoe Perches
5704d8469f16SJoe PerchesNOTE: Whitespace errors detected.
5705d8469f16SJoe Perches      You may wish to use scripts/cleanpatch or scripts/cleanfile
5706d8469f16SJoe PerchesEOM
5707d2c0a235SAndy Whitcroft		}
5708d2c0a235SAndy Whitcroft	}
5709d2c0a235SAndy Whitcroft
571091bfe484SJoe Perches	hash_show_words(\%use_type, "Used");
571191bfe484SJoe Perches	hash_show_words(\%ignore_type, "Ignored");
5712000d1cc1SJoe Perches
5713d752fcc8SJoe Perches	if ($clean == 0 && $fix &&
5714d752fcc8SJoe Perches	    ("@rawlines" ne "@fixed" ||
5715d752fcc8SJoe Perches	     $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) {
57169624b8d6SJoe Perches		my $newfile = $filename;
57179624b8d6SJoe Perches		$newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace);
57183705ce5bSJoe Perches		my $linecount = 0;
57193705ce5bSJoe Perches		my $f;
57203705ce5bSJoe Perches
5721d752fcc8SJoe Perches		@fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted);
5722d752fcc8SJoe Perches
57233705ce5bSJoe Perches		open($f, '>', $newfile)
57243705ce5bSJoe Perches		    or die "$P: Can't open $newfile for write\n";
57253705ce5bSJoe Perches		foreach my $fixed_line (@fixed) {
57263705ce5bSJoe Perches			$linecount++;
57273705ce5bSJoe Perches			if ($file) {
57283705ce5bSJoe Perches				if ($linecount > 3) {
57293705ce5bSJoe Perches					$fixed_line =~ s/^\+//;
57303705ce5bSJoe Perches					print $f $fixed_line . "\n";
57313705ce5bSJoe Perches				}
57323705ce5bSJoe Perches			} else {
57333705ce5bSJoe Perches				print $f $fixed_line . "\n";
57343705ce5bSJoe Perches			}
57353705ce5bSJoe Perches		}
57363705ce5bSJoe Perches		close($f);
57373705ce5bSJoe Perches
57383705ce5bSJoe Perches		if (!$quiet) {
57393705ce5bSJoe Perches			print << "EOM";
5740d8469f16SJoe Perches
57413705ce5bSJoe PerchesWrote EXPERIMENTAL --fix correction(s) to '$newfile'
57423705ce5bSJoe Perches
57433705ce5bSJoe PerchesDo _NOT_ trust the results written to this file.
57443705ce5bSJoe PerchesDo _NOT_ submit these changes without inspecting them for correctness.
57453705ce5bSJoe Perches
57463705ce5bSJoe PerchesThis EXPERIMENTAL file is simply a convenience to help rewrite patches.
57473705ce5bSJoe PerchesNo warranties, expressed or implied...
57483705ce5bSJoe PerchesEOM
57493705ce5bSJoe Perches		}
57503705ce5bSJoe Perches	}
57513705ce5bSJoe Perches
5752d8469f16SJoe Perches	if ($quiet == 0) {
5753d8469f16SJoe Perches		print "\n";
5754d8469f16SJoe Perches		if ($clean == 1) {
5755d8469f16SJoe Perches			print "$vname has no obvious style problems and is ready for submission.\n";
5756d8469f16SJoe Perches		} else {
5757d8469f16SJoe Perches			print "$vname has style problems, please review.\n";
57580a920b5bSAndy Whitcroft		}
57590a920b5bSAndy Whitcroft	}
57600a920b5bSAndy Whitcroft	return $clean;
57610a920b5bSAndy Whitcroft}
5762