xref: /linux-6.15/scripts/checkpatch.pl (revision cdcee686)
10a920b5bSAndy Whitcroft#!/usr/bin/perl -w
2dbf004d7SDave Jones# (c) 2001, Dave Jones. (the file handling bit)
300df344fSAndy Whitcroft# (c) 2005, Joel Schopp <[email protected]> (the ugly bit)
42a5a2c25SAndy Whitcroft# (c) 2007,2008, Andy Whitcroft <[email protected]> (new conditions, test suite)
5015830beSAndy Whitcroft# (c) 2008-2010 Andy Whitcroft <[email protected]>
60a920b5bSAndy Whitcroft# Licensed under the terms of the GNU GPL License version 2
70a920b5bSAndy Whitcroft
80a920b5bSAndy Whitcroftuse strict;
9c707a81dSJoe Perchesuse POSIX;
100a920b5bSAndy Whitcroft
110a920b5bSAndy Whitcroftmy $P = $0;
1200df344fSAndy Whitcroft$P =~ s@.*/@@g;
130a920b5bSAndy Whitcroft
14000d1cc1SJoe Perchesmy $V = '0.32';
150a920b5bSAndy Whitcroft
160a920b5bSAndy Whitcroftuse Getopt::Long qw(:config no_auto_abbrev);
170a920b5bSAndy Whitcroft
180a920b5bSAndy Whitcroftmy $quiet = 0;
190a920b5bSAndy Whitcroftmy $tree = 1;
200a920b5bSAndy Whitcroftmy $chk_signoff = 1;
210a920b5bSAndy Whitcroftmy $chk_patch = 1;
22773647a0SAndy Whitcroftmy $tst_only;
236c72ffaaSAndy Whitcroftmy $emacs = 0;
248905a67cSAndy Whitcroftmy $terse = 0;
256c72ffaaSAndy Whitcroftmy $file = 0;
266c72ffaaSAndy Whitcroftmy $check = 0;
272ac73b4fSJoe Perchesmy $check_orig = 0;
288905a67cSAndy Whitcroftmy $summary = 1;
298905a67cSAndy Whitcroftmy $mailback = 0;
3013214adfSAndy Whitcroftmy $summary_file = 0;
31000d1cc1SJoe Perchesmy $show_types = 0;
323705ce5bSJoe Perchesmy $fix = 0;
339624b8d6SJoe Perchesmy $fix_inplace = 0;
346c72ffaaSAndy Whitcroftmy $root;
35c2fdda0dSAndy Whitcroftmy %debug;
363445686aSJoe Perchesmy %camelcase = ();
3791bfe484SJoe Perchesmy %use_type = ();
3891bfe484SJoe Perchesmy @use = ();
3991bfe484SJoe Perchesmy %ignore_type = ();
40000d1cc1SJoe Perchesmy @ignore = ();
4177f5b10aSHannes Edermy $help = 0;
42000d1cc1SJoe Perchesmy $configuration_file = ".checkpatch.conf";
436cd7f386SJoe Perchesmy $max_line_length = 80;
44d62a201fSDave Hansenmy $ignore_perl_version = 0;
45d62a201fSDave Hansenmy $minimum_perl_version = 5.10.0;
4677f5b10aSHannes Eder
4777f5b10aSHannes Edersub help {
4877f5b10aSHannes Eder	my ($exitcode) = @_;
4977f5b10aSHannes Eder
5077f5b10aSHannes Eder	print << "EOM";
5177f5b10aSHannes EderUsage: $P [OPTION]... [FILE]...
5277f5b10aSHannes EderVersion: $V
5377f5b10aSHannes Eder
5477f5b10aSHannes EderOptions:
5577f5b10aSHannes Eder  -q, --quiet                quiet
5677f5b10aSHannes Eder  --no-tree                  run without a kernel tree
5777f5b10aSHannes Eder  --no-signoff               do not check for 'Signed-off-by' line
5877f5b10aSHannes Eder  --patch                    treat FILE as patchfile (default)
5977f5b10aSHannes Eder  --emacs                    emacs compile window format
6077f5b10aSHannes Eder  --terse                    one line per report
6177f5b10aSHannes Eder  -f, --file                 treat FILE as regular source file
6277f5b10aSHannes Eder  --subjective, --strict     enable more subjective tests
6391bfe484SJoe Perches  --types TYPE(,TYPE2...)    show only these comma separated message types
64000d1cc1SJoe Perches  --ignore TYPE(,TYPE2...)   ignore various comma separated message types
656cd7f386SJoe Perches  --max-line-length=n        set the maximum line length, if exceeded, warn
66000d1cc1SJoe Perches  --show-types               show the message "types" in the output
6777f5b10aSHannes Eder  --root=PATH                PATH to the kernel tree root
6877f5b10aSHannes Eder  --no-summary               suppress the per-file summary
6977f5b10aSHannes Eder  --mailback                 only produce a report in case of warnings/errors
7077f5b10aSHannes Eder  --summary-file             include the filename in summary
7177f5b10aSHannes Eder  --debug KEY=[0|1]          turn on/off debugging of KEY, where KEY is one of
7277f5b10aSHannes Eder                             'values', 'possible', 'type', and 'attr' (default
7377f5b10aSHannes Eder                             is all off)
7477f5b10aSHannes Eder  --test-only=WORD           report only warnings/errors containing WORD
7577f5b10aSHannes Eder                             literally
763705ce5bSJoe Perches  --fix                      EXPERIMENTAL - may create horrible results
773705ce5bSJoe Perches                             If correctable single-line errors exist, create
783705ce5bSJoe Perches                             "<inputfile>.EXPERIMENTAL-checkpatch-fixes"
793705ce5bSJoe Perches                             with potential errors corrected to the preferred
803705ce5bSJoe Perches                             checkpatch style
819624b8d6SJoe Perches  --fix-inplace              EXPERIMENTAL - may create horrible results
829624b8d6SJoe Perches                             Is the same as --fix, but overwrites the input
839624b8d6SJoe Perches                             file.  It's your fault if there's no backup or git
84d62a201fSDave Hansen  --ignore-perl-version      override checking of perl version.  expect
85d62a201fSDave Hansen                             runtime errors.
8677f5b10aSHannes Eder  -h, --help, --version      display this help and exit
8777f5b10aSHannes Eder
8877f5b10aSHannes EderWhen FILE is - read standard input.
8977f5b10aSHannes EderEOM
9077f5b10aSHannes Eder
9177f5b10aSHannes Eder	exit($exitcode);
9277f5b10aSHannes Eder}
9377f5b10aSHannes Eder
94000d1cc1SJoe Perchesmy $conf = which_conf($configuration_file);
95000d1cc1SJoe Perchesif (-f $conf) {
96000d1cc1SJoe Perches	my @conf_args;
97000d1cc1SJoe Perches	open(my $conffile, '<', "$conf")
98000d1cc1SJoe Perches	    or warn "$P: Can't find a readable $configuration_file file $!\n";
99000d1cc1SJoe Perches
100000d1cc1SJoe Perches	while (<$conffile>) {
101000d1cc1SJoe Perches		my $line = $_;
102000d1cc1SJoe Perches
103000d1cc1SJoe Perches		$line =~ s/\s*\n?$//g;
104000d1cc1SJoe Perches		$line =~ s/^\s*//g;
105000d1cc1SJoe Perches		$line =~ s/\s+/ /g;
106000d1cc1SJoe Perches
107000d1cc1SJoe Perches		next if ($line =~ m/^\s*#/);
108000d1cc1SJoe Perches		next if ($line =~ m/^\s*$/);
109000d1cc1SJoe Perches
110000d1cc1SJoe Perches		my @words = split(" ", $line);
111000d1cc1SJoe Perches		foreach my $word (@words) {
112000d1cc1SJoe Perches			last if ($word =~ m/^#/);
113000d1cc1SJoe Perches			push (@conf_args, $word);
114000d1cc1SJoe Perches		}
115000d1cc1SJoe Perches	}
116000d1cc1SJoe Perches	close($conffile);
117000d1cc1SJoe Perches	unshift(@ARGV, @conf_args) if @conf_args;
118000d1cc1SJoe Perches}
119000d1cc1SJoe Perches
1200a920b5bSAndy WhitcroftGetOptions(
1216c72ffaaSAndy Whitcroft	'q|quiet+'	=> \$quiet,
1220a920b5bSAndy Whitcroft	'tree!'		=> \$tree,
1230a920b5bSAndy Whitcroft	'signoff!'	=> \$chk_signoff,
1240a920b5bSAndy Whitcroft	'patch!'	=> \$chk_patch,
1256c72ffaaSAndy Whitcroft	'emacs!'	=> \$emacs,
1268905a67cSAndy Whitcroft	'terse!'	=> \$terse,
12777f5b10aSHannes Eder	'f|file!'	=> \$file,
1286c72ffaaSAndy Whitcroft	'subjective!'	=> \$check,
1296c72ffaaSAndy Whitcroft	'strict!'	=> \$check,
130000d1cc1SJoe Perches	'ignore=s'	=> \@ignore,
13191bfe484SJoe Perches	'types=s'	=> \@use,
132000d1cc1SJoe Perches	'show-types!'	=> \$show_types,
1336cd7f386SJoe Perches	'max-line-length=i' => \$max_line_length,
1346c72ffaaSAndy Whitcroft	'root=s'	=> \$root,
1358905a67cSAndy Whitcroft	'summary!'	=> \$summary,
1368905a67cSAndy Whitcroft	'mailback!'	=> \$mailback,
13713214adfSAndy Whitcroft	'summary-file!'	=> \$summary_file,
1383705ce5bSJoe Perches	'fix!'		=> \$fix,
1399624b8d6SJoe Perches	'fix-inplace!'	=> \$fix_inplace,
140d62a201fSDave Hansen	'ignore-perl-version!' => \$ignore_perl_version,
141c2fdda0dSAndy Whitcroft	'debug=s'	=> \%debug,
142773647a0SAndy Whitcroft	'test-only=s'	=> \$tst_only,
14377f5b10aSHannes Eder	'h|help'	=> \$help,
14477f5b10aSHannes Eder	'version'	=> \$help
14577f5b10aSHannes Eder) or help(1);
14677f5b10aSHannes Eder
14777f5b10aSHannes Ederhelp(0) if ($help);
1480a920b5bSAndy Whitcroft
1499624b8d6SJoe Perches$fix = 1 if ($fix_inplace);
1502ac73b4fSJoe Perches$check_orig = $check;
1519624b8d6SJoe Perches
1520a920b5bSAndy Whitcroftmy $exit = 0;
1530a920b5bSAndy Whitcroft
154d62a201fSDave Hansenif ($^V && $^V lt $minimum_perl_version) {
155d62a201fSDave Hansen	printf "$P: requires at least perl version %vd\n", $minimum_perl_version;
156d62a201fSDave Hansen	if (!$ignore_perl_version) {
157d62a201fSDave Hansen		exit(1);
158d62a201fSDave Hansen	}
159d62a201fSDave Hansen}
160d62a201fSDave Hansen
1610a920b5bSAndy Whitcroftif ($#ARGV < 0) {
16277f5b10aSHannes Eder	print "$P: no input files\n";
1630a920b5bSAndy Whitcroft	exit(1);
1640a920b5bSAndy Whitcroft}
1650a920b5bSAndy Whitcroft
16691bfe484SJoe Perchessub hash_save_array_words {
16791bfe484SJoe Perches	my ($hashRef, $arrayRef) = @_;
16891bfe484SJoe Perches
16991bfe484SJoe Perches	my @array = split(/,/, join(',', @$arrayRef));
17091bfe484SJoe Perches	foreach my $word (@array) {
171000d1cc1SJoe Perches		$word =~ s/\s*\n?$//g;
172000d1cc1SJoe Perches		$word =~ s/^\s*//g;
173000d1cc1SJoe Perches		$word =~ s/\s+/ /g;
174000d1cc1SJoe Perches		$word =~ tr/[a-z]/[A-Z]/;
175000d1cc1SJoe Perches
176000d1cc1SJoe Perches		next if ($word =~ m/^\s*#/);
177000d1cc1SJoe Perches		next if ($word =~ m/^\s*$/);
178000d1cc1SJoe Perches
17991bfe484SJoe Perches		$hashRef->{$word}++;
180000d1cc1SJoe Perches	}
18191bfe484SJoe Perches}
18291bfe484SJoe Perches
18391bfe484SJoe Perchessub hash_show_words {
18491bfe484SJoe Perches	my ($hashRef, $prefix) = @_;
18591bfe484SJoe Perches
18658cb3cf6SJoe Perches	if ($quiet == 0 && keys %$hashRef) {
18791bfe484SJoe Perches		print "NOTE: $prefix message types:";
18858cb3cf6SJoe Perches		foreach my $word (sort keys %$hashRef) {
18991bfe484SJoe Perches			print " $word";
19091bfe484SJoe Perches		}
19191bfe484SJoe Perches		print "\n\n";
19291bfe484SJoe Perches	}
19391bfe484SJoe Perches}
19491bfe484SJoe Perches
19591bfe484SJoe Percheshash_save_array_words(\%ignore_type, \@ignore);
19691bfe484SJoe Percheshash_save_array_words(\%use_type, \@use);
197000d1cc1SJoe Perches
198c2fdda0dSAndy Whitcroftmy $dbg_values = 0;
199c2fdda0dSAndy Whitcroftmy $dbg_possible = 0;
2007429c690SAndy Whitcroftmy $dbg_type = 0;
201a1ef277eSAndy Whitcroftmy $dbg_attr = 0;
202c2fdda0dSAndy Whitcroftfor my $key (keys %debug) {
20321caa13cSAndy Whitcroft	## no critic
20421caa13cSAndy Whitcroft	eval "\${dbg_$key} = '$debug{$key}';";
20521caa13cSAndy Whitcroft	die "$@" if ($@);
206c2fdda0dSAndy Whitcroft}
207c2fdda0dSAndy Whitcroft
208d2c0a235SAndy Whitcroftmy $rpt_cleaners = 0;
209d2c0a235SAndy Whitcroft
2108905a67cSAndy Whitcroftif ($terse) {
2118905a67cSAndy Whitcroft	$emacs = 1;
2128905a67cSAndy Whitcroft	$quiet++;
2138905a67cSAndy Whitcroft}
2148905a67cSAndy Whitcroft
2156c72ffaaSAndy Whitcroftif ($tree) {
2166c72ffaaSAndy Whitcroft	if (defined $root) {
2176c72ffaaSAndy Whitcroft		if (!top_of_kernel_tree($root)) {
2186c72ffaaSAndy Whitcroft			die "$P: $root: --root does not point at a valid tree\n";
2196c72ffaaSAndy Whitcroft		}
2206c72ffaaSAndy Whitcroft	} else {
2216c72ffaaSAndy Whitcroft		if (top_of_kernel_tree('.')) {
2226c72ffaaSAndy Whitcroft			$root = '.';
2236c72ffaaSAndy Whitcroft		} elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
2246c72ffaaSAndy Whitcroft						top_of_kernel_tree($1)) {
2256c72ffaaSAndy Whitcroft			$root = $1;
2266c72ffaaSAndy Whitcroft		}
2276c72ffaaSAndy Whitcroft	}
2286c72ffaaSAndy Whitcroft
2296c72ffaaSAndy Whitcroft	if (!defined $root) {
2300a920b5bSAndy Whitcroft		print "Must be run from the top-level dir. of a kernel tree\n";
2310a920b5bSAndy Whitcroft		exit(2);
2320a920b5bSAndy Whitcroft	}
2336c72ffaaSAndy Whitcroft}
2346c72ffaaSAndy Whitcroft
2356c72ffaaSAndy Whitcroftmy $emitted_corrupt = 0;
2366c72ffaaSAndy Whitcroft
2372ceb532bSAndy Whitcroftour $Ident	= qr{
2382ceb532bSAndy Whitcroft			[A-Za-z_][A-Za-z\d_]*
2392ceb532bSAndy Whitcroft			(?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
2402ceb532bSAndy Whitcroft		}x;
2416c72ffaaSAndy Whitcroftour $Storage	= qr{extern|static|asmlinkage};
2426c72ffaaSAndy Whitcroftour $Sparse	= qr{
2436c72ffaaSAndy Whitcroft			__user|
2446c72ffaaSAndy Whitcroft			__kernel|
2456c72ffaaSAndy Whitcroft			__force|
2466c72ffaaSAndy Whitcroft			__iomem|
2476c72ffaaSAndy Whitcroft			__must_check|
2486c72ffaaSAndy Whitcroft			__init_refok|
249417495edSAndy Whitcroft			__kprobes|
250165e72a6SSven Eckelmann			__ref|
251165e72a6SSven Eckelmann			__rcu
2526c72ffaaSAndy Whitcroft		}x;
253e970b884SJoe Perchesour $InitAttributePrefix = qr{__(?:mem|cpu|dev|net_|)};
254e970b884SJoe Perchesour $InitAttributeData = qr{$InitAttributePrefix(?:initdata\b)};
255e970b884SJoe Perchesour $InitAttributeConst = qr{$InitAttributePrefix(?:initconst\b)};
256e970b884SJoe Perchesour $InitAttributeInit = qr{$InitAttributePrefix(?:init\b)};
257e970b884SJoe Perchesour $InitAttribute = qr{$InitAttributeData|$InitAttributeConst|$InitAttributeInit};
2588716de38SJoe Perches
25952131292SWolfram Sang# Notes to $Attribute:
26052131292SWolfram Sang# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
2616c72ffaaSAndy Whitcroftour $Attribute	= qr{
2626c72ffaaSAndy Whitcroft			const|
26303f1df7dSJoe Perches			__percpu|
26403f1df7dSJoe Perches			__nocast|
26503f1df7dSJoe Perches			__safe|
26603f1df7dSJoe Perches			__bitwise__|
26703f1df7dSJoe Perches			__packed__|
26803f1df7dSJoe Perches			__packed2__|
26903f1df7dSJoe Perches			__naked|
27003f1df7dSJoe Perches			__maybe_unused|
27103f1df7dSJoe Perches			__always_unused|
27203f1df7dSJoe Perches			__noreturn|
27303f1df7dSJoe Perches			__used|
27403f1df7dSJoe Perches			__cold|
27503f1df7dSJoe Perches			__noclone|
27603f1df7dSJoe Perches			__deprecated|
2776c72ffaaSAndy Whitcroft			__read_mostly|
2786c72ffaaSAndy Whitcroft			__kprobes|
2798716de38SJoe Perches			$InitAttribute|
28024e1d81aSAndy Whitcroft			____cacheline_aligned|
28124e1d81aSAndy Whitcroft			____cacheline_aligned_in_smp|
2825fe3af11SAndy Whitcroft			____cacheline_internodealigned_in_smp|
2835fe3af11SAndy Whitcroft			__weak
2846c72ffaaSAndy Whitcroft		  }x;
285c45dcabdSAndy Whitcroftour $Modifier;
28691cb5195SJoe Perchesour $Inline	= qr{inline|__always_inline|noinline|__inline|__inline__};
2876c72ffaaSAndy Whitcroftour $Member	= qr{->$Ident|\.$Ident|\[[^]]*\]};
2886c72ffaaSAndy Whitcroftour $Lval	= qr{$Ident(?:$Member)*};
2896c72ffaaSAndy Whitcroft
29095e2c602SJoe Perchesour $Int_type	= qr{(?i)llu|ull|ll|lu|ul|l|u};
29195e2c602SJoe Perchesour $Binary	= qr{(?i)0b[01]+$Int_type?};
29295e2c602SJoe Perchesour $Hex	= qr{(?i)0x[0-9a-f]+$Int_type?};
29395e2c602SJoe Perchesour $Int	= qr{[0-9]+$Int_type?};
2942435880fSJoe Perchesour $Octal	= qr{0[0-7]+$Int_type?};
295326b1ffcSJoe Perchesour $Float_hex	= qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?};
296326b1ffcSJoe Perchesour $Float_dec	= qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?};
297326b1ffcSJoe Perchesour $Float_int	= qr{(?i)[0-9]+e-?[0-9]+[fl]?};
29874349bccSJoe Perchesour $Float	= qr{$Float_hex|$Float_dec|$Float_int};
2992435880fSJoe Perchesour $Constant	= qr{$Float|$Binary|$Octal|$Hex|$Int};
300326b1ffcSJoe Perchesour $Assignment	= qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=};
301447432f3SJoe Perchesour $Compare    = qr{<=|>=|==|!=|<|(?<!-)>};
30223f780c9SJoe Perchesour $Arithmetic = qr{\+|-|\*|\/|%};
3036c72ffaaSAndy Whitcroftour $Operators	= qr{
3046c72ffaaSAndy Whitcroft			<=|>=|==|!=|
3056c72ffaaSAndy Whitcroft			=>|->|<<|>>|<|>|!|~|
30623f780c9SJoe Perches			&&|\|\||,|\^|\+\+|--|&|\||$Arithmetic
3076c72ffaaSAndy Whitcroft		  }x;
3086c72ffaaSAndy Whitcroft
30991cb5195SJoe Perchesour $c90_Keywords = qr{do|for|while|if|else|return|goto|continue|switch|default|case|break}x;
31091cb5195SJoe Perches
3118905a67cSAndy Whitcroftour $NonptrType;
3121813087dSJoe Perchesour $NonptrTypeMisordered;
3138716de38SJoe Perchesour $NonptrTypeWithAttr;
3148905a67cSAndy Whitcroftour $Type;
3151813087dSJoe Perchesour $TypeMisordered;
3168905a67cSAndy Whitcroftour $Declare;
3171813087dSJoe Perchesour $DeclareMisordered;
3188905a67cSAndy Whitcroft
31915662b3eSJoe Perchesour $NON_ASCII_UTF8	= qr{
32015662b3eSJoe Perches	[\xC2-\xDF][\x80-\xBF]               # non-overlong 2-byte
321171ae1a4SAndy Whitcroft	|  \xE0[\xA0-\xBF][\x80-\xBF]        # excluding overlongs
322171ae1a4SAndy Whitcroft	| [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}  # straight 3-byte
323171ae1a4SAndy Whitcroft	|  \xED[\x80-\x9F][\x80-\xBF]        # excluding surrogates
324171ae1a4SAndy Whitcroft	|  \xF0[\x90-\xBF][\x80-\xBF]{2}     # planes 1-3
325171ae1a4SAndy Whitcroft	| [\xF1-\xF3][\x80-\xBF]{3}          # planes 4-15
326171ae1a4SAndy Whitcroft	|  \xF4[\x80-\x8F][\x80-\xBF]{2}     # plane 16
327171ae1a4SAndy Whitcroft}x;
328171ae1a4SAndy Whitcroft
32915662b3eSJoe Perchesour $UTF8	= qr{
33015662b3eSJoe Perches	[\x09\x0A\x0D\x20-\x7E]              # ASCII
33115662b3eSJoe Perches	| $NON_ASCII_UTF8
33215662b3eSJoe Perches}x;
33315662b3eSJoe Perches
3348ed22cadSAndy Whitcroftour $typeTypedefs = qr{(?x:
335fb9e9096SAndy Whitcroft	(?:__)?(?:u|s|be|le)(?:8|16|32|64)|
3368ed22cadSAndy Whitcroft	atomic_t
3378ed22cadSAndy Whitcroft)};
3388ed22cadSAndy Whitcroft
339691e669bSJoe Perchesour $logFunctions = qr{(?x:
3406e60c02eSJoe Perches	printk(?:_ratelimited|_once|)|
3417d0b6594SJacob Keller	(?:[a-z0-9]+_){1,2}(?:printk|emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)(?:_ratelimited|_once|)|
3426e60c02eSJoe Perches	WARN(?:_RATELIMIT|_ONCE|)|
343b0531722SJoe Perches	panic|
34406668727SJoe Perches	MODULE_[A-Z_]+|
34506668727SJoe Perches	seq_vprintf|seq_printf|seq_puts
346691e669bSJoe Perches)};
347691e669bSJoe Perches
34820112475SJoe Perchesour $signature_tags = qr{(?xi:
34920112475SJoe Perches	Signed-off-by:|
35020112475SJoe Perches	Acked-by:|
35120112475SJoe Perches	Tested-by:|
35220112475SJoe Perches	Reviewed-by:|
35320112475SJoe Perches	Reported-by:|
3548543ae12SMugunthan V N	Suggested-by:|
35520112475SJoe Perches	To:|
35620112475SJoe Perches	Cc:
35720112475SJoe Perches)};
35820112475SJoe Perches
3591813087dSJoe Perchesour @typeListMisordered = (
3601813087dSJoe Perches	qr{char\s+(?:un)?signed},
3611813087dSJoe Perches	qr{int\s+(?:(?:un)?signed\s+)?short\s},
3621813087dSJoe Perches	qr{int\s+short(?:\s+(?:un)?signed)},
3631813087dSJoe Perches	qr{short\s+int(?:\s+(?:un)?signed)},
3641813087dSJoe Perches	qr{(?:un)?signed\s+int\s+short},
3651813087dSJoe Perches	qr{short\s+(?:un)?signed},
3661813087dSJoe Perches	qr{long\s+int\s+(?:un)?signed},
3671813087dSJoe Perches	qr{int\s+long\s+(?:un)?signed},
3681813087dSJoe Perches	qr{long\s+(?:un)?signed\s+int},
3691813087dSJoe Perches	qr{int\s+(?:un)?signed\s+long},
3701813087dSJoe Perches	qr{int\s+(?:un)?signed},
3711813087dSJoe Perches	qr{int\s+long\s+long\s+(?:un)?signed},
3721813087dSJoe Perches	qr{long\s+long\s+int\s+(?:un)?signed},
3731813087dSJoe Perches	qr{long\s+long\s+(?:un)?signed\s+int},
3741813087dSJoe Perches	qr{long\s+long\s+(?:un)?signed},
3751813087dSJoe Perches	qr{long\s+(?:un)?signed},
3761813087dSJoe Perches);
3771813087dSJoe Perches
3788905a67cSAndy Whitcroftour @typeList = (
3798905a67cSAndy Whitcroft	qr{void},
3800c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?char},
3810c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?short\s+int},
3820c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?short},
3830c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?int},
3840c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?long\s+int},
3850c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?long\s+long\s+int},
3860c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?long\s+long},
3870c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?long},
3880c773d9dSJoe Perches	qr{(?:un)?signed},
3898905a67cSAndy Whitcroft	qr{float},
3908905a67cSAndy Whitcroft	qr{double},
3918905a67cSAndy Whitcroft	qr{bool},
3928905a67cSAndy Whitcroft	qr{struct\s+$Ident},
3938905a67cSAndy Whitcroft	qr{union\s+$Ident},
3948905a67cSAndy Whitcroft	qr{enum\s+$Ident},
3958905a67cSAndy Whitcroft	qr{${Ident}_t},
3968905a67cSAndy Whitcroft	qr{${Ident}_handler},
3978905a67cSAndy Whitcroft	qr{${Ident}_handler_fn},
3981813087dSJoe Perches	@typeListMisordered,
3998905a67cSAndy Whitcroft);
4008716de38SJoe Perchesour @typeListWithAttr = (
4018716de38SJoe Perches	@typeList,
4028716de38SJoe Perches	qr{struct\s+$InitAttribute\s+$Ident},
4038716de38SJoe Perches	qr{union\s+$InitAttribute\s+$Ident},
4048716de38SJoe Perches);
4058716de38SJoe Perches
406c45dcabdSAndy Whitcroftour @modifierList = (
407c45dcabdSAndy Whitcroft	qr{fastcall},
408c45dcabdSAndy Whitcroft);
4098905a67cSAndy Whitcroft
4102435880fSJoe Perchesour @mode_permission_funcs = (
4112435880fSJoe Perches	["module_param", 3],
4122435880fSJoe Perches	["module_param_(?:array|named|string)", 4],
4132435880fSJoe Perches	["module_param_array_named", 5],
4142435880fSJoe Perches	["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2],
4152435880fSJoe Perches	["proc_create(?:_data|)", 2],
4162435880fSJoe Perches	["(?:CLASS|DEVICE|SENSOR)_ATTR", 2],
4172435880fSJoe Perches);
4182435880fSJoe Perches
419515a235eSJoe Perches#Create a search pattern for all these functions to speed up a loop below
420515a235eSJoe Perchesour $mode_perms_search = "";
421515a235eSJoe Perchesforeach my $entry (@mode_permission_funcs) {
422515a235eSJoe Perches	$mode_perms_search .= '|' if ($mode_perms_search ne "");
423515a235eSJoe Perches	$mode_perms_search .= $entry->[0];
424515a235eSJoe Perches}
425515a235eSJoe Perches
4267840a94cSWolfram Sangour $allowed_asm_includes = qr{(?x:
4277840a94cSWolfram Sang	irq|
428*cdcee686SSergey Ryazanov	memory|
429*cdcee686SSergey Ryazanov	time|
430*cdcee686SSergey Ryazanov	reboot
4317840a94cSWolfram Sang)};
4327840a94cSWolfram Sang# memory.h: ARM has a custom one
4337840a94cSWolfram Sang
4348905a67cSAndy Whitcroftsub build_types {
435d2172eb5SAndy Whitcroft	my $mods = "(?x:  \n" . join("|\n  ", @modifierList) . "\n)";
436d2172eb5SAndy Whitcroft	my $all = "(?x:  \n" . join("|\n  ", @typeList) . "\n)";
4371813087dSJoe Perches	my $Misordered = "(?x:  \n" . join("|\n  ", @typeListMisordered) . "\n)";
4388716de38SJoe Perches	my $allWithAttr = "(?x:  \n" . join("|\n  ", @typeListWithAttr) . "\n)";
439c8cb2ca3SAndy Whitcroft	$Modifier	= qr{(?:$Attribute|$Sparse|$mods)};
4408905a67cSAndy Whitcroft	$NonptrType	= qr{
441d2172eb5SAndy Whitcroft			(?:$Modifier\s+|const\s+)*
442cf655043SAndy Whitcroft			(?:
4436b48db24SAndy Whitcroft				(?:typeof|__typeof__)\s*\([^\)]*\)|
4448ed22cadSAndy Whitcroft				(?:$typeTypedefs\b)|
445c45dcabdSAndy Whitcroft				(?:${all}\b)
446cf655043SAndy Whitcroft			)
447c8cb2ca3SAndy Whitcroft			(?:\s+$Modifier|\s+const)*
4488905a67cSAndy Whitcroft		  }x;
4491813087dSJoe Perches	$NonptrTypeMisordered	= qr{
4501813087dSJoe Perches			(?:$Modifier\s+|const\s+)*
4511813087dSJoe Perches			(?:
4521813087dSJoe Perches				(?:${Misordered}\b)
4531813087dSJoe Perches			)
4541813087dSJoe Perches			(?:\s+$Modifier|\s+const)*
4551813087dSJoe Perches		  }x;
4568716de38SJoe Perches	$NonptrTypeWithAttr	= qr{
4578716de38SJoe Perches			(?:$Modifier\s+|const\s+)*
4588716de38SJoe Perches			(?:
4598716de38SJoe Perches				(?:typeof|__typeof__)\s*\([^\)]*\)|
4608716de38SJoe Perches				(?:$typeTypedefs\b)|
4618716de38SJoe Perches				(?:${allWithAttr}\b)
4628716de38SJoe Perches			)
4638716de38SJoe Perches			(?:\s+$Modifier|\s+const)*
4648716de38SJoe Perches		  }x;
4658905a67cSAndy Whitcroft	$Type	= qr{
466c45dcabdSAndy Whitcroft			$NonptrType
4671574a29fSJoe Perches			(?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
468c8cb2ca3SAndy Whitcroft			(?:\s+$Inline|\s+$Modifier)*
4698905a67cSAndy Whitcroft		  }x;
4701813087dSJoe Perches	$TypeMisordered	= qr{
4711813087dSJoe Perches			$NonptrTypeMisordered
4721813087dSJoe Perches			(?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
4731813087dSJoe Perches			(?:\s+$Inline|\s+$Modifier)*
4741813087dSJoe Perches		  }x;
47591cb5195SJoe Perches	$Declare	= qr{(?:$Storage\s+(?:$Inline\s+)?)?$Type};
4761813087dSJoe Perches	$DeclareMisordered	= qr{(?:$Storage\s+(?:$Inline\s+)?)?$TypeMisordered};
4778905a67cSAndy Whitcroft}
4788905a67cSAndy Whitcroftbuild_types();
4796c72ffaaSAndy Whitcroft
4807d2367afSJoe Perchesour $Typecast	= qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
481d1fe9c09SJoe Perches
482d1fe9c09SJoe Perches# Using $balanced_parens, $LvalOrFunc, or $FuncArg
483d1fe9c09SJoe Perches# requires at least perl version v5.10.0
484d1fe9c09SJoe Perches# Any use must be runtime checked with $^V
485d1fe9c09SJoe Perches
486d1fe9c09SJoe Perchesour $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
4872435880fSJoe Perchesour $LvalOrFunc	= qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*};
488d7c76ba7SJoe Perchesour $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant)};
4897d2367afSJoe Perches
490f8422308SJoe Perchesour $declaration_macros = qr{(?x:
491f8422308SJoe Perches	(?:$Storage\s+)?(?:[A-Z_][A-Z0-9]*_){0,2}(?:DEFINE|DECLARE)(?:_[A-Z0-9]+){1,2}\s*\(|
492f8422308SJoe Perches	(?:$Storage\s+)?LIST_HEAD\s*\(|
493f8422308SJoe Perches	(?:$Storage\s+)?${Type}\s+uninitialized_var\s*\(
494f8422308SJoe Perches)};
495f8422308SJoe Perches
4967d2367afSJoe Perchessub deparenthesize {
4977d2367afSJoe Perches	my ($string) = @_;
4987d2367afSJoe Perches	return "" if (!defined($string));
4995b9553abSJoe Perches
5005b9553abSJoe Perches	while ($string =~ /^\s*\(.*\)\s*$/) {
5015b9553abSJoe Perches		$string =~ s@^\s*\(\s*@@;
5025b9553abSJoe Perches		$string =~ s@\s*\)\s*$@@;
5035b9553abSJoe Perches	}
5045b9553abSJoe Perches
5057d2367afSJoe Perches	$string =~ s@\s+@ @g;
5065b9553abSJoe Perches
5077d2367afSJoe Perches	return $string;
5087d2367afSJoe Perches}
5097d2367afSJoe Perches
5103445686aSJoe Perchessub seed_camelcase_file {
5113445686aSJoe Perches	my ($file) = @_;
5123445686aSJoe Perches
5133445686aSJoe Perches	return if (!(-f $file));
5143445686aSJoe Perches
5153445686aSJoe Perches	local $/;
5163445686aSJoe Perches
5173445686aSJoe Perches	open(my $include_file, '<', "$file")
5183445686aSJoe Perches	    or warn "$P: Can't read '$file' $!\n";
5193445686aSJoe Perches	my $text = <$include_file>;
5203445686aSJoe Perches	close($include_file);
5213445686aSJoe Perches
5223445686aSJoe Perches	my @lines = split('\n', $text);
5233445686aSJoe Perches
5243445686aSJoe Perches	foreach my $line (@lines) {
5253445686aSJoe Perches		next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/);
5263445686aSJoe Perches		if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) {
5273445686aSJoe Perches			$camelcase{$1} = 1;
52811ea516aSJoe Perches		} elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) {
52911ea516aSJoe Perches			$camelcase{$1} = 1;
53011ea516aSJoe Perches		} elsif ($line =~ /^\s*(?:union|struct|enum)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[;\{]/) {
5313445686aSJoe Perches			$camelcase{$1} = 1;
5323445686aSJoe Perches		}
5333445686aSJoe Perches	}
5343445686aSJoe Perches}
5353445686aSJoe Perches
5363445686aSJoe Perchesmy $camelcase_seeded = 0;
5373445686aSJoe Perchessub seed_camelcase_includes {
5383445686aSJoe Perches	return if ($camelcase_seeded);
5393445686aSJoe Perches
5403445686aSJoe Perches	my $files;
541c707a81dSJoe Perches	my $camelcase_cache = "";
542c707a81dSJoe Perches	my @include_files = ();
543c707a81dSJoe Perches
544c707a81dSJoe Perches	$camelcase_seeded = 1;
545351b2a1fSJoe Perches
5463645e328SRichard Genoud	if (-e ".git") {
547351b2a1fSJoe Perches		my $git_last_include_commit = `git log --no-merges --pretty=format:"%h%n" -1 -- include`;
548351b2a1fSJoe Perches		chomp $git_last_include_commit;
549c707a81dSJoe Perches		$camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit";
550c707a81dSJoe Perches	} else {
551c707a81dSJoe Perches		my $last_mod_date = 0;
552c707a81dSJoe Perches		$files = `find $root/include -name "*.h"`;
553c707a81dSJoe Perches		@include_files = split('\n', $files);
554c707a81dSJoe Perches		foreach my $file (@include_files) {
555c707a81dSJoe Perches			my $date = POSIX::strftime("%Y%m%d%H%M",
556c707a81dSJoe Perches						   localtime((stat $file)[9]));
557c707a81dSJoe Perches			$last_mod_date = $date if ($last_mod_date < $date);
558c707a81dSJoe Perches		}
559c707a81dSJoe Perches		$camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date";
560c707a81dSJoe Perches	}
561c707a81dSJoe Perches
562c707a81dSJoe Perches	if ($camelcase_cache ne "" && -f $camelcase_cache) {
563c707a81dSJoe Perches		open(my $camelcase_file, '<', "$camelcase_cache")
564c707a81dSJoe Perches		    or warn "$P: Can't read '$camelcase_cache' $!\n";
565351b2a1fSJoe Perches		while (<$camelcase_file>) {
566351b2a1fSJoe Perches			chomp;
567351b2a1fSJoe Perches			$camelcase{$_} = 1;
568351b2a1fSJoe Perches		}
569351b2a1fSJoe Perches		close($camelcase_file);
570351b2a1fSJoe Perches
571351b2a1fSJoe Perches		return;
572351b2a1fSJoe Perches	}
573c707a81dSJoe Perches
5743645e328SRichard Genoud	if (-e ".git") {
575c707a81dSJoe Perches		$files = `git ls-files "include/*.h"`;
576c707a81dSJoe Perches		@include_files = split('\n', $files);
5773445686aSJoe Perches	}
578c707a81dSJoe Perches
5793445686aSJoe Perches	foreach my $file (@include_files) {
5803445686aSJoe Perches		seed_camelcase_file($file);
5813445686aSJoe Perches	}
582351b2a1fSJoe Perches
583c707a81dSJoe Perches	if ($camelcase_cache ne "") {
584351b2a1fSJoe Perches		unlink glob ".checkpatch-camelcase.*";
585c707a81dSJoe Perches		open(my $camelcase_file, '>', "$camelcase_cache")
586c707a81dSJoe Perches		    or warn "$P: Can't write '$camelcase_cache' $!\n";
587351b2a1fSJoe Perches		foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) {
588351b2a1fSJoe Perches			print $camelcase_file ("$_\n");
589351b2a1fSJoe Perches		}
590351b2a1fSJoe Perches		close($camelcase_file);
591351b2a1fSJoe Perches	}
5923445686aSJoe Perches}
5933445686aSJoe Perches
594d311cd44SJoe Perchessub git_commit_info {
595d311cd44SJoe Perches	my ($commit, $id, $desc) = @_;
596d311cd44SJoe Perches
597d311cd44SJoe Perches	return ($id, $desc) if ((which("git") eq "") || !(-e ".git"));
598d311cd44SJoe Perches
599d311cd44SJoe Perches	my $output = `git log --no-color --format='%H %s' -1 $commit 2>&1`;
600d311cd44SJoe Perches	$output =~ s/^\s*//gm;
601d311cd44SJoe Perches	my @lines = split("\n", $output);
602d311cd44SJoe Perches
603d311cd44SJoe Perches	if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous\./) {
604d311cd44SJoe Perches# Maybe one day convert this block of bash into something that returns
605d311cd44SJoe Perches# all matching commit ids, but it's very slow...
606d311cd44SJoe Perches#
607d311cd44SJoe Perches#		echo "checking commits $1..."
608d311cd44SJoe Perches#		git rev-list --remotes | grep -i "^$1" |
609d311cd44SJoe Perches#		while read line ; do
610d311cd44SJoe Perches#		    git log --format='%H %s' -1 $line |
611d311cd44SJoe Perches#		    echo "commit $(cut -c 1-12,41-)"
612d311cd44SJoe Perches#		done
613d311cd44SJoe Perches	} elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./) {
614d311cd44SJoe Perches	} else {
615d311cd44SJoe Perches		$id = substr($lines[0], 0, 12);
616d311cd44SJoe Perches		$desc = substr($lines[0], 41);
617d311cd44SJoe Perches	}
618d311cd44SJoe Perches
619d311cd44SJoe Perches	return ($id, $desc);
620d311cd44SJoe Perches}
621d311cd44SJoe Perches
6226c72ffaaSAndy Whitcroft$chk_signoff = 0 if ($file);
6230a920b5bSAndy Whitcroft
62400df344fSAndy Whitcroftmy @rawlines = ();
625c2fdda0dSAndy Whitcroftmy @lines = ();
6263705ce5bSJoe Perchesmy @fixed = ();
627d752fcc8SJoe Perchesmy @fixed_inserted = ();
628d752fcc8SJoe Perchesmy @fixed_deleted = ();
629194f66fcSJoe Perchesmy $fixlinenr = -1;
630194f66fcSJoe Perches
631c2fdda0dSAndy Whitcroftmy $vname;
6326c72ffaaSAndy Whitcroftfor my $filename (@ARGV) {
63321caa13cSAndy Whitcroft	my $FILE;
6346c72ffaaSAndy Whitcroft	if ($file) {
63521caa13cSAndy Whitcroft		open($FILE, '-|', "diff -u /dev/null $filename") ||
6366c72ffaaSAndy Whitcroft			die "$P: $filename: diff failed - $!\n";
63721caa13cSAndy Whitcroft	} elsif ($filename eq '-') {
63821caa13cSAndy Whitcroft		open($FILE, '<&STDIN');
6396c72ffaaSAndy Whitcroft	} else {
64021caa13cSAndy Whitcroft		open($FILE, '<', "$filename") ||
6416c72ffaaSAndy Whitcroft			die "$P: $filename: open failed - $!\n";
6426c72ffaaSAndy Whitcroft	}
643c2fdda0dSAndy Whitcroft	if ($filename eq '-') {
644c2fdda0dSAndy Whitcroft		$vname = 'Your patch';
645c2fdda0dSAndy Whitcroft	} else {
646c2fdda0dSAndy Whitcroft		$vname = $filename;
647c2fdda0dSAndy Whitcroft	}
64821caa13cSAndy Whitcroft	while (<$FILE>) {
6490a920b5bSAndy Whitcroft		chomp;
65000df344fSAndy Whitcroft		push(@rawlines, $_);
6516c72ffaaSAndy Whitcroft	}
65221caa13cSAndy Whitcroft	close($FILE);
653c2fdda0dSAndy Whitcroft	if (!process($filename)) {
6540a920b5bSAndy Whitcroft		$exit = 1;
6550a920b5bSAndy Whitcroft	}
65600df344fSAndy Whitcroft	@rawlines = ();
65713214adfSAndy Whitcroft	@lines = ();
6583705ce5bSJoe Perches	@fixed = ();
659d752fcc8SJoe Perches	@fixed_inserted = ();
660d752fcc8SJoe Perches	@fixed_deleted = ();
661194f66fcSJoe Perches	$fixlinenr = -1;
6620a920b5bSAndy Whitcroft}
6630a920b5bSAndy Whitcroft
6640a920b5bSAndy Whitcroftexit($exit);
6650a920b5bSAndy Whitcroft
6660a920b5bSAndy Whitcroftsub top_of_kernel_tree {
6676c72ffaaSAndy Whitcroft	my ($root) = @_;
6686c72ffaaSAndy Whitcroft
6696c72ffaaSAndy Whitcroft	my @tree_check = (
6706c72ffaaSAndy Whitcroft		"COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
6716c72ffaaSAndy Whitcroft		"README", "Documentation", "arch", "include", "drivers",
6726c72ffaaSAndy Whitcroft		"fs", "init", "ipc", "kernel", "lib", "scripts",
6736c72ffaaSAndy Whitcroft	);
6746c72ffaaSAndy Whitcroft
6756c72ffaaSAndy Whitcroft	foreach my $check (@tree_check) {
6766c72ffaaSAndy Whitcroft		if (! -e $root . '/' . $check) {
6770a920b5bSAndy Whitcroft			return 0;
6780a920b5bSAndy Whitcroft		}
6796c72ffaaSAndy Whitcroft	}
6806c72ffaaSAndy Whitcroft	return 1;
6816c72ffaaSAndy Whitcroft}
6820a920b5bSAndy Whitcroft
68320112475SJoe Perchessub parse_email {
68420112475SJoe Perches	my ($formatted_email) = @_;
68520112475SJoe Perches
68620112475SJoe Perches	my $name = "";
68720112475SJoe Perches	my $address = "";
68820112475SJoe Perches	my $comment = "";
68920112475SJoe Perches
69020112475SJoe Perches	if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
69120112475SJoe Perches		$name = $1;
69220112475SJoe Perches		$address = $2;
69320112475SJoe Perches		$comment = $3 if defined $3;
69420112475SJoe Perches	} elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
69520112475SJoe Perches		$address = $1;
69620112475SJoe Perches		$comment = $2 if defined $2;
69720112475SJoe Perches	} elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
69820112475SJoe Perches		$address = $1;
69920112475SJoe Perches		$comment = $2 if defined $2;
70020112475SJoe Perches		$formatted_email =~ s/$address.*$//;
70120112475SJoe Perches		$name = $formatted_email;
7023705ce5bSJoe Perches		$name = trim($name);
70320112475SJoe Perches		$name =~ s/^\"|\"$//g;
70420112475SJoe Perches		# If there's a name left after stripping spaces and
70520112475SJoe Perches		# leading quotes, and the address doesn't have both
70620112475SJoe Perches		# leading and trailing angle brackets, the address
70720112475SJoe Perches		# is invalid. ie:
70820112475SJoe Perches		#   "joe smith [email protected]" bad
70920112475SJoe Perches		#   "joe smith <[email protected]" bad
71020112475SJoe Perches		if ($name ne "" && $address !~ /^<[^>]+>$/) {
71120112475SJoe Perches			$name = "";
71220112475SJoe Perches			$address = "";
71320112475SJoe Perches			$comment = "";
71420112475SJoe Perches		}
71520112475SJoe Perches	}
71620112475SJoe Perches
7173705ce5bSJoe Perches	$name = trim($name);
71820112475SJoe Perches	$name =~ s/^\"|\"$//g;
7193705ce5bSJoe Perches	$address = trim($address);
72020112475SJoe Perches	$address =~ s/^\<|\>$//g;
72120112475SJoe Perches
72220112475SJoe Perches	if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
72320112475SJoe Perches		$name =~ s/(?<!\\)"/\\"/g; ##escape quotes
72420112475SJoe Perches		$name = "\"$name\"";
72520112475SJoe Perches	}
72620112475SJoe Perches
72720112475SJoe Perches	return ($name, $address, $comment);
72820112475SJoe Perches}
72920112475SJoe Perches
73020112475SJoe Perchessub format_email {
73120112475SJoe Perches	my ($name, $address) = @_;
73220112475SJoe Perches
73320112475SJoe Perches	my $formatted_email;
73420112475SJoe Perches
7353705ce5bSJoe Perches	$name = trim($name);
73620112475SJoe Perches	$name =~ s/^\"|\"$//g;
7373705ce5bSJoe Perches	$address = trim($address);
73820112475SJoe Perches
73920112475SJoe Perches	if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
74020112475SJoe Perches		$name =~ s/(?<!\\)"/\\"/g; ##escape quotes
74120112475SJoe Perches		$name = "\"$name\"";
74220112475SJoe Perches	}
74320112475SJoe Perches
74420112475SJoe Perches	if ("$name" eq "") {
74520112475SJoe Perches		$formatted_email = "$address";
74620112475SJoe Perches	} else {
74720112475SJoe Perches		$formatted_email = "$name <$address>";
74820112475SJoe Perches	}
74920112475SJoe Perches
75020112475SJoe Perches	return $formatted_email;
75120112475SJoe Perches}
75220112475SJoe Perches
753d311cd44SJoe Perchessub which {
754d311cd44SJoe Perches	my ($bin) = @_;
755d311cd44SJoe Perches
756d311cd44SJoe Perches	foreach my $path (split(/:/, $ENV{PATH})) {
757d311cd44SJoe Perches		if (-e "$path/$bin") {
758d311cd44SJoe Perches			return "$path/$bin";
759d311cd44SJoe Perches		}
760d311cd44SJoe Perches	}
761d311cd44SJoe Perches
762d311cd44SJoe Perches	return "";
763d311cd44SJoe Perches}
764d311cd44SJoe Perches
765000d1cc1SJoe Perchessub which_conf {
766000d1cc1SJoe Perches	my ($conf) = @_;
767000d1cc1SJoe Perches
768000d1cc1SJoe Perches	foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
769000d1cc1SJoe Perches		if (-e "$path/$conf") {
770000d1cc1SJoe Perches			return "$path/$conf";
771000d1cc1SJoe Perches		}
772000d1cc1SJoe Perches	}
773000d1cc1SJoe Perches
774000d1cc1SJoe Perches	return "";
775000d1cc1SJoe Perches}
776000d1cc1SJoe Perches
7770a920b5bSAndy Whitcroftsub expand_tabs {
7780a920b5bSAndy Whitcroft	my ($str) = @_;
7790a920b5bSAndy Whitcroft
7800a920b5bSAndy Whitcroft	my $res = '';
7810a920b5bSAndy Whitcroft	my $n = 0;
7820a920b5bSAndy Whitcroft	for my $c (split(//, $str)) {
7830a920b5bSAndy Whitcroft		if ($c eq "\t") {
7840a920b5bSAndy Whitcroft			$res .= ' ';
7850a920b5bSAndy Whitcroft			$n++;
7860a920b5bSAndy Whitcroft			for (; ($n % 8) != 0; $n++) {
7870a920b5bSAndy Whitcroft				$res .= ' ';
7880a920b5bSAndy Whitcroft			}
7890a920b5bSAndy Whitcroft			next;
7900a920b5bSAndy Whitcroft		}
7910a920b5bSAndy Whitcroft		$res .= $c;
7920a920b5bSAndy Whitcroft		$n++;
7930a920b5bSAndy Whitcroft	}
7940a920b5bSAndy Whitcroft
7950a920b5bSAndy Whitcroft	return $res;
7960a920b5bSAndy Whitcroft}
7976c72ffaaSAndy Whitcroftsub copy_spacing {
798773647a0SAndy Whitcroft	(my $res = shift) =~ tr/\t/ /c;
7996c72ffaaSAndy Whitcroft	return $res;
8006c72ffaaSAndy Whitcroft}
8010a920b5bSAndy Whitcroft
8024a0df2efSAndy Whitcroftsub line_stats {
8034a0df2efSAndy Whitcroft	my ($line) = @_;
8044a0df2efSAndy Whitcroft
8054a0df2efSAndy Whitcroft	# Drop the diff line leader and expand tabs
8064a0df2efSAndy Whitcroft	$line =~ s/^.//;
8074a0df2efSAndy Whitcroft	$line = expand_tabs($line);
8084a0df2efSAndy Whitcroft
8094a0df2efSAndy Whitcroft	# Pick the indent from the front of the line.
8104a0df2efSAndy Whitcroft	my ($white) = ($line =~ /^(\s*)/);
8114a0df2efSAndy Whitcroft
8124a0df2efSAndy Whitcroft	return (length($line), length($white));
8134a0df2efSAndy Whitcroft}
8144a0df2efSAndy Whitcroft
815773647a0SAndy Whitcroftmy $sanitise_quote = '';
816773647a0SAndy Whitcroft
817773647a0SAndy Whitcroftsub sanitise_line_reset {
818773647a0SAndy Whitcroft	my ($in_comment) = @_;
819773647a0SAndy Whitcroft
820773647a0SAndy Whitcroft	if ($in_comment) {
821773647a0SAndy Whitcroft		$sanitise_quote = '*/';
822773647a0SAndy Whitcroft	} else {
823773647a0SAndy Whitcroft		$sanitise_quote = '';
824773647a0SAndy Whitcroft	}
825773647a0SAndy Whitcroft}
82600df344fSAndy Whitcroftsub sanitise_line {
82700df344fSAndy Whitcroft	my ($line) = @_;
82800df344fSAndy Whitcroft
82900df344fSAndy Whitcroft	my $res = '';
83000df344fSAndy Whitcroft	my $l = '';
83100df344fSAndy Whitcroft
832c2fdda0dSAndy Whitcroft	my $qlen = 0;
833773647a0SAndy Whitcroft	my $off = 0;
834773647a0SAndy Whitcroft	my $c;
83500df344fSAndy Whitcroft
836773647a0SAndy Whitcroft	# Always copy over the diff marker.
837773647a0SAndy Whitcroft	$res = substr($line, 0, 1);
838773647a0SAndy Whitcroft
839773647a0SAndy Whitcroft	for ($off = 1; $off < length($line); $off++) {
840773647a0SAndy Whitcroft		$c = substr($line, $off, 1);
841773647a0SAndy Whitcroft
842773647a0SAndy Whitcroft		# Comments we are wacking completly including the begin
843773647a0SAndy Whitcroft		# and end, all to $;.
844773647a0SAndy Whitcroft		if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
845773647a0SAndy Whitcroft			$sanitise_quote = '*/';
846773647a0SAndy Whitcroft
847773647a0SAndy Whitcroft			substr($res, $off, 2, "$;$;");
848773647a0SAndy Whitcroft			$off++;
84900df344fSAndy Whitcroft			next;
850773647a0SAndy Whitcroft		}
85181bc0e02SAndy Whitcroft		if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
852773647a0SAndy Whitcroft			$sanitise_quote = '';
853773647a0SAndy Whitcroft			substr($res, $off, 2, "$;$;");
854773647a0SAndy Whitcroft			$off++;
855773647a0SAndy Whitcroft			next;
856773647a0SAndy Whitcroft		}
857113f04a8SDaniel Walker		if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
858113f04a8SDaniel Walker			$sanitise_quote = '//';
859113f04a8SDaniel Walker
860113f04a8SDaniel Walker			substr($res, $off, 2, $sanitise_quote);
861113f04a8SDaniel Walker			$off++;
862113f04a8SDaniel Walker			next;
863113f04a8SDaniel Walker		}
864773647a0SAndy Whitcroft
865773647a0SAndy Whitcroft		# A \ in a string means ignore the next character.
866773647a0SAndy Whitcroft		if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
867773647a0SAndy Whitcroft		    $c eq "\\") {
868773647a0SAndy Whitcroft			substr($res, $off, 2, 'XX');
869773647a0SAndy Whitcroft			$off++;
870773647a0SAndy Whitcroft			next;
871773647a0SAndy Whitcroft		}
872773647a0SAndy Whitcroft		# Regular quotes.
873773647a0SAndy Whitcroft		if ($c eq "'" || $c eq '"') {
874773647a0SAndy Whitcroft			if ($sanitise_quote eq '') {
875773647a0SAndy Whitcroft				$sanitise_quote = $c;
876773647a0SAndy Whitcroft
877773647a0SAndy Whitcroft				substr($res, $off, 1, $c);
878773647a0SAndy Whitcroft				next;
879773647a0SAndy Whitcroft			} elsif ($sanitise_quote eq $c) {
880773647a0SAndy Whitcroft				$sanitise_quote = '';
88100df344fSAndy Whitcroft			}
88200df344fSAndy Whitcroft		}
883773647a0SAndy Whitcroft
884fae17daeSAndy Whitcroft		#print "c<$c> SQ<$sanitise_quote>\n";
885773647a0SAndy Whitcroft		if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
886773647a0SAndy Whitcroft			substr($res, $off, 1, $;);
887113f04a8SDaniel Walker		} elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
888113f04a8SDaniel Walker			substr($res, $off, 1, $;);
889773647a0SAndy Whitcroft		} elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
890773647a0SAndy Whitcroft			substr($res, $off, 1, 'X');
89100df344fSAndy Whitcroft		} else {
892773647a0SAndy Whitcroft			substr($res, $off, 1, $c);
89300df344fSAndy Whitcroft		}
894c2fdda0dSAndy Whitcroft	}
895c2fdda0dSAndy Whitcroft
896113f04a8SDaniel Walker	if ($sanitise_quote eq '//') {
897113f04a8SDaniel Walker		$sanitise_quote = '';
898113f04a8SDaniel Walker	}
899113f04a8SDaniel Walker
900c2fdda0dSAndy Whitcroft	# The pathname on a #include may be surrounded by '<' and '>'.
901c45dcabdSAndy Whitcroft	if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
902c2fdda0dSAndy Whitcroft		my $clean = 'X' x length($1);
903c2fdda0dSAndy Whitcroft		$res =~ s@\<.*\>@<$clean>@;
904c2fdda0dSAndy Whitcroft
905c2fdda0dSAndy Whitcroft	# The whole of a #error is a string.
906c45dcabdSAndy Whitcroft	} elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
907c2fdda0dSAndy Whitcroft		my $clean = 'X' x length($1);
908c45dcabdSAndy Whitcroft		$res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
909c2fdda0dSAndy Whitcroft	}
910c2fdda0dSAndy Whitcroft
91100df344fSAndy Whitcroft	return $res;
91200df344fSAndy Whitcroft}
91300df344fSAndy Whitcroft
914a6962d72SJoe Perchessub get_quoted_string {
915a6962d72SJoe Perches	my ($line, $rawline) = @_;
916a6962d72SJoe Perches
917a6962d72SJoe Perches	return "" if ($line !~ m/(\"[X]+\")/g);
918a6962d72SJoe Perches	return substr($rawline, $-[0], $+[0] - $-[0]);
919a6962d72SJoe Perches}
920a6962d72SJoe Perches
9218905a67cSAndy Whitcroftsub ctx_statement_block {
9228905a67cSAndy Whitcroft	my ($linenr, $remain, $off) = @_;
9238905a67cSAndy Whitcroft	my $line = $linenr - 1;
9248905a67cSAndy Whitcroft	my $blk = '';
9258905a67cSAndy Whitcroft	my $soff = $off;
9268905a67cSAndy Whitcroft	my $coff = $off - 1;
927773647a0SAndy Whitcroft	my $coff_set = 0;
9288905a67cSAndy Whitcroft
92913214adfSAndy Whitcroft	my $loff = 0;
93013214adfSAndy Whitcroft
9318905a67cSAndy Whitcroft	my $type = '';
9328905a67cSAndy Whitcroft	my $level = 0;
933a2750645SAndy Whitcroft	my @stack = ();
934cf655043SAndy Whitcroft	my $p;
9358905a67cSAndy Whitcroft	my $c;
9368905a67cSAndy Whitcroft	my $len = 0;
93713214adfSAndy Whitcroft
93813214adfSAndy Whitcroft	my $remainder;
9398905a67cSAndy Whitcroft	while (1) {
940a2750645SAndy Whitcroft		@stack = (['', 0]) if ($#stack == -1);
941a2750645SAndy Whitcroft
942773647a0SAndy Whitcroft		#warn "CSB: blk<$blk> remain<$remain>\n";
9438905a67cSAndy Whitcroft		# If we are about to drop off the end, pull in more
9448905a67cSAndy Whitcroft		# context.
9458905a67cSAndy Whitcroft		if ($off >= $len) {
9468905a67cSAndy Whitcroft			for (; $remain > 0; $line++) {
947dea33496SAndy Whitcroft				last if (!defined $lines[$line]);
948c2fdda0dSAndy Whitcroft				next if ($lines[$line] =~ /^-/);
9498905a67cSAndy Whitcroft				$remain--;
95013214adfSAndy Whitcroft				$loff = $len;
951c2fdda0dSAndy Whitcroft				$blk .= $lines[$line] . "\n";
9528905a67cSAndy Whitcroft				$len = length($blk);
9538905a67cSAndy Whitcroft				$line++;
9548905a67cSAndy Whitcroft				last;
9558905a67cSAndy Whitcroft			}
9568905a67cSAndy Whitcroft			# Bail if there is no further context.
9578905a67cSAndy Whitcroft			#warn "CSB: blk<$blk> off<$off> len<$len>\n";
95813214adfSAndy Whitcroft			if ($off >= $len) {
9598905a67cSAndy Whitcroft				last;
9608905a67cSAndy Whitcroft			}
961f74bd194SAndy Whitcroft			if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
962f74bd194SAndy Whitcroft				$level++;
963f74bd194SAndy Whitcroft				$type = '#';
964f74bd194SAndy Whitcroft			}
9658905a67cSAndy Whitcroft		}
966cf655043SAndy Whitcroft		$p = $c;
9678905a67cSAndy Whitcroft		$c = substr($blk, $off, 1);
96813214adfSAndy Whitcroft		$remainder = substr($blk, $off);
9698905a67cSAndy Whitcroft
970773647a0SAndy Whitcroft		#warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
9714635f4fbSAndy Whitcroft
9724635f4fbSAndy Whitcroft		# Handle nested #if/#else.
9734635f4fbSAndy Whitcroft		if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
9744635f4fbSAndy Whitcroft			push(@stack, [ $type, $level ]);
9754635f4fbSAndy Whitcroft		} elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
9764635f4fbSAndy Whitcroft			($type, $level) = @{$stack[$#stack - 1]};
9774635f4fbSAndy Whitcroft		} elsif ($remainder =~ /^#\s*endif\b/) {
9784635f4fbSAndy Whitcroft			($type, $level) = @{pop(@stack)};
9794635f4fbSAndy Whitcroft		}
9804635f4fbSAndy Whitcroft
9818905a67cSAndy Whitcroft		# Statement ends at the ';' or a close '}' at the
9828905a67cSAndy Whitcroft		# outermost level.
9838905a67cSAndy Whitcroft		if ($level == 0 && $c eq ';') {
9848905a67cSAndy Whitcroft			last;
9858905a67cSAndy Whitcroft		}
9868905a67cSAndy Whitcroft
98713214adfSAndy Whitcroft		# An else is really a conditional as long as its not else if
988773647a0SAndy Whitcroft		if ($level == 0 && $coff_set == 0 &&
989773647a0SAndy Whitcroft				(!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
990773647a0SAndy Whitcroft				$remainder =~ /^(else)(?:\s|{)/ &&
991773647a0SAndy Whitcroft				$remainder !~ /^else\s+if\b/) {
992773647a0SAndy Whitcroft			$coff = $off + length($1) - 1;
993773647a0SAndy Whitcroft			$coff_set = 1;
994773647a0SAndy Whitcroft			#warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
995773647a0SAndy Whitcroft			#warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
99613214adfSAndy Whitcroft		}
99713214adfSAndy Whitcroft
9988905a67cSAndy Whitcroft		if (($type eq '' || $type eq '(') && $c eq '(') {
9998905a67cSAndy Whitcroft			$level++;
10008905a67cSAndy Whitcroft			$type = '(';
10018905a67cSAndy Whitcroft		}
10028905a67cSAndy Whitcroft		if ($type eq '(' && $c eq ')') {
10038905a67cSAndy Whitcroft			$level--;
10048905a67cSAndy Whitcroft			$type = ($level != 0)? '(' : '';
10058905a67cSAndy Whitcroft
10068905a67cSAndy Whitcroft			if ($level == 0 && $coff < $soff) {
10078905a67cSAndy Whitcroft				$coff = $off;
1008773647a0SAndy Whitcroft				$coff_set = 1;
1009773647a0SAndy Whitcroft				#warn "CSB: mark coff<$coff>\n";
10108905a67cSAndy Whitcroft			}
10118905a67cSAndy Whitcroft		}
10128905a67cSAndy Whitcroft		if (($type eq '' || $type eq '{') && $c eq '{') {
10138905a67cSAndy Whitcroft			$level++;
10148905a67cSAndy Whitcroft			$type = '{';
10158905a67cSAndy Whitcroft		}
10168905a67cSAndy Whitcroft		if ($type eq '{' && $c eq '}') {
10178905a67cSAndy Whitcroft			$level--;
10188905a67cSAndy Whitcroft			$type = ($level != 0)? '{' : '';
10198905a67cSAndy Whitcroft
10208905a67cSAndy Whitcroft			if ($level == 0) {
1021b998e001SPatrick Pannuto				if (substr($blk, $off + 1, 1) eq ';') {
1022b998e001SPatrick Pannuto					$off++;
1023b998e001SPatrick Pannuto				}
10248905a67cSAndy Whitcroft				last;
10258905a67cSAndy Whitcroft			}
10268905a67cSAndy Whitcroft		}
1027f74bd194SAndy Whitcroft		# Preprocessor commands end at the newline unless escaped.
1028f74bd194SAndy Whitcroft		if ($type eq '#' && $c eq "\n" && $p ne "\\") {
1029f74bd194SAndy Whitcroft			$level--;
1030f74bd194SAndy Whitcroft			$type = '';
1031f74bd194SAndy Whitcroft			$off++;
1032f74bd194SAndy Whitcroft			last;
1033f74bd194SAndy Whitcroft		}
10348905a67cSAndy Whitcroft		$off++;
10358905a67cSAndy Whitcroft	}
1036a3bb97a7SAndy Whitcroft	# We are truly at the end, so shuffle to the next line.
103713214adfSAndy Whitcroft	if ($off == $len) {
1038a3bb97a7SAndy Whitcroft		$loff = $len + 1;
103913214adfSAndy Whitcroft		$line++;
104013214adfSAndy Whitcroft		$remain--;
104113214adfSAndy Whitcroft	}
10428905a67cSAndy Whitcroft
10438905a67cSAndy Whitcroft	my $statement = substr($blk, $soff, $off - $soff + 1);
10448905a67cSAndy Whitcroft	my $condition = substr($blk, $soff, $coff - $soff + 1);
10458905a67cSAndy Whitcroft
10468905a67cSAndy Whitcroft	#warn "STATEMENT<$statement>\n";
10478905a67cSAndy Whitcroft	#warn "CONDITION<$condition>\n";
10488905a67cSAndy Whitcroft
1049773647a0SAndy Whitcroft	#print "coff<$coff> soff<$off> loff<$loff>\n";
105013214adfSAndy Whitcroft
105113214adfSAndy Whitcroft	return ($statement, $condition,
105213214adfSAndy Whitcroft			$line, $remain + 1, $off - $loff + 1, $level);
105313214adfSAndy Whitcroft}
105413214adfSAndy Whitcroft
1055cf655043SAndy Whitcroftsub statement_lines {
1056cf655043SAndy Whitcroft	my ($stmt) = @_;
1057cf655043SAndy Whitcroft
1058cf655043SAndy Whitcroft	# Strip the diff line prefixes and rip blank lines at start and end.
1059cf655043SAndy Whitcroft	$stmt =~ s/(^|\n)./$1/g;
1060cf655043SAndy Whitcroft	$stmt =~ s/^\s*//;
1061cf655043SAndy Whitcroft	$stmt =~ s/\s*$//;
1062cf655043SAndy Whitcroft
1063cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
1064cf655043SAndy Whitcroft
1065cf655043SAndy Whitcroft	return $#stmt_lines + 2;
1066cf655043SAndy Whitcroft}
1067cf655043SAndy Whitcroft
1068cf655043SAndy Whitcroftsub statement_rawlines {
1069cf655043SAndy Whitcroft	my ($stmt) = @_;
1070cf655043SAndy Whitcroft
1071cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
1072cf655043SAndy Whitcroft
1073cf655043SAndy Whitcroft	return $#stmt_lines + 2;
1074cf655043SAndy Whitcroft}
1075cf655043SAndy Whitcroft
1076cf655043SAndy Whitcroftsub statement_block_size {
1077cf655043SAndy Whitcroft	my ($stmt) = @_;
1078cf655043SAndy Whitcroft
1079cf655043SAndy Whitcroft	$stmt =~ s/(^|\n)./$1/g;
1080cf655043SAndy Whitcroft	$stmt =~ s/^\s*{//;
1081cf655043SAndy Whitcroft	$stmt =~ s/}\s*$//;
1082cf655043SAndy Whitcroft	$stmt =~ s/^\s*//;
1083cf655043SAndy Whitcroft	$stmt =~ s/\s*$//;
1084cf655043SAndy Whitcroft
1085cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
1086cf655043SAndy Whitcroft	my @stmt_statements = ($stmt =~ /;/g);
1087cf655043SAndy Whitcroft
1088cf655043SAndy Whitcroft	my $stmt_lines = $#stmt_lines + 2;
1089cf655043SAndy Whitcroft	my $stmt_statements = $#stmt_statements + 1;
1090cf655043SAndy Whitcroft
1091cf655043SAndy Whitcroft	if ($stmt_lines > $stmt_statements) {
1092cf655043SAndy Whitcroft		return $stmt_lines;
1093cf655043SAndy Whitcroft	} else {
1094cf655043SAndy Whitcroft		return $stmt_statements;
1095cf655043SAndy Whitcroft	}
1096cf655043SAndy Whitcroft}
1097cf655043SAndy Whitcroft
109813214adfSAndy Whitcroftsub ctx_statement_full {
109913214adfSAndy Whitcroft	my ($linenr, $remain, $off) = @_;
110013214adfSAndy Whitcroft	my ($statement, $condition, $level);
110113214adfSAndy Whitcroft
110213214adfSAndy Whitcroft	my (@chunks);
110313214adfSAndy Whitcroft
1104cf655043SAndy Whitcroft	# Grab the first conditional/block pair.
110513214adfSAndy Whitcroft	($statement, $condition, $linenr, $remain, $off, $level) =
110613214adfSAndy Whitcroft				ctx_statement_block($linenr, $remain, $off);
1107773647a0SAndy Whitcroft	#print "F: c<$condition> s<$statement> remain<$remain>\n";
110813214adfSAndy Whitcroft	push(@chunks, [ $condition, $statement ]);
1109cf655043SAndy Whitcroft	if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
1110cf655043SAndy Whitcroft		return ($level, $linenr, @chunks);
1111cf655043SAndy Whitcroft	}
1112cf655043SAndy Whitcroft
1113cf655043SAndy Whitcroft	# Pull in the following conditional/block pairs and see if they
1114cf655043SAndy Whitcroft	# could continue the statement.
1115cf655043SAndy Whitcroft	for (;;) {
111613214adfSAndy Whitcroft		($statement, $condition, $linenr, $remain, $off, $level) =
111713214adfSAndy Whitcroft				ctx_statement_block($linenr, $remain, $off);
1118cf655043SAndy Whitcroft		#print "C: c<$condition> s<$statement> remain<$remain>\n";
1119773647a0SAndy Whitcroft		last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
1120cf655043SAndy Whitcroft		#print "C: push\n";
1121cf655043SAndy Whitcroft		push(@chunks, [ $condition, $statement ]);
112213214adfSAndy Whitcroft	}
112313214adfSAndy Whitcroft
112413214adfSAndy Whitcroft	return ($level, $linenr, @chunks);
11258905a67cSAndy Whitcroft}
11268905a67cSAndy Whitcroft
11274a0df2efSAndy Whitcroftsub ctx_block_get {
1128f0a594c1SAndy Whitcroft	my ($linenr, $remain, $outer, $open, $close, $off) = @_;
11294a0df2efSAndy Whitcroft	my $line;
11304a0df2efSAndy Whitcroft	my $start = $linenr - 1;
11314a0df2efSAndy Whitcroft	my $blk = '';
11324a0df2efSAndy Whitcroft	my @o;
11334a0df2efSAndy Whitcroft	my @c;
11344a0df2efSAndy Whitcroft	my @res = ();
11354a0df2efSAndy Whitcroft
1136f0a594c1SAndy Whitcroft	my $level = 0;
11374635f4fbSAndy Whitcroft	my @stack = ($level);
113800df344fSAndy Whitcroft	for ($line = $start; $remain > 0; $line++) {
113900df344fSAndy Whitcroft		next if ($rawlines[$line] =~ /^-/);
114000df344fSAndy Whitcroft		$remain--;
114100df344fSAndy Whitcroft
114200df344fSAndy Whitcroft		$blk .= $rawlines[$line];
11434635f4fbSAndy Whitcroft
11444635f4fbSAndy Whitcroft		# Handle nested #if/#else.
114501464f30SAndy Whitcroft		if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
11464635f4fbSAndy Whitcroft			push(@stack, $level);
114701464f30SAndy Whitcroft		} elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
11484635f4fbSAndy Whitcroft			$level = $stack[$#stack - 1];
114901464f30SAndy Whitcroft		} elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
11504635f4fbSAndy Whitcroft			$level = pop(@stack);
11514635f4fbSAndy Whitcroft		}
11524635f4fbSAndy Whitcroft
115301464f30SAndy Whitcroft		foreach my $c (split(//, $lines[$line])) {
1154f0a594c1SAndy Whitcroft			##print "C<$c>L<$level><$open$close>O<$off>\n";
1155f0a594c1SAndy Whitcroft			if ($off > 0) {
1156f0a594c1SAndy Whitcroft				$off--;
1157f0a594c1SAndy Whitcroft				next;
1158f0a594c1SAndy Whitcroft			}
11594a0df2efSAndy Whitcroft
1160f0a594c1SAndy Whitcroft			if ($c eq $close && $level > 0) {
1161f0a594c1SAndy Whitcroft				$level--;
1162f0a594c1SAndy Whitcroft				last if ($level == 0);
1163f0a594c1SAndy Whitcroft			} elsif ($c eq $open) {
1164f0a594c1SAndy Whitcroft				$level++;
1165f0a594c1SAndy Whitcroft			}
1166f0a594c1SAndy Whitcroft		}
11674a0df2efSAndy Whitcroft
1168f0a594c1SAndy Whitcroft		if (!$outer || $level <= 1) {
116900df344fSAndy Whitcroft			push(@res, $rawlines[$line]);
11704a0df2efSAndy Whitcroft		}
11714a0df2efSAndy Whitcroft
1172f0a594c1SAndy Whitcroft		last if ($level == 0);
11734a0df2efSAndy Whitcroft	}
11744a0df2efSAndy Whitcroft
1175f0a594c1SAndy Whitcroft	return ($level, @res);
11764a0df2efSAndy Whitcroft}
11774a0df2efSAndy Whitcroftsub ctx_block_outer {
11784a0df2efSAndy Whitcroft	my ($linenr, $remain) = @_;
11794a0df2efSAndy Whitcroft
1180f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
1181f0a594c1SAndy Whitcroft	return @r;
11824a0df2efSAndy Whitcroft}
11834a0df2efSAndy Whitcroftsub ctx_block {
11844a0df2efSAndy Whitcroft	my ($linenr, $remain) = @_;
11854a0df2efSAndy Whitcroft
1186f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1187f0a594c1SAndy Whitcroft	return @r;
1188653d4876SAndy Whitcroft}
1189653d4876SAndy Whitcroftsub ctx_statement {
1190f0a594c1SAndy Whitcroft	my ($linenr, $remain, $off) = @_;
1191f0a594c1SAndy Whitcroft
1192f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1193f0a594c1SAndy Whitcroft	return @r;
1194f0a594c1SAndy Whitcroft}
1195f0a594c1SAndy Whitcroftsub ctx_block_level {
1196653d4876SAndy Whitcroft	my ($linenr, $remain) = @_;
1197653d4876SAndy Whitcroft
1198f0a594c1SAndy Whitcroft	return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
11994a0df2efSAndy Whitcroft}
12009c0ca6f9SAndy Whitcroftsub ctx_statement_level {
12019c0ca6f9SAndy Whitcroft	my ($linenr, $remain, $off) = @_;
12029c0ca6f9SAndy Whitcroft
12039c0ca6f9SAndy Whitcroft	return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
12049c0ca6f9SAndy Whitcroft}
12054a0df2efSAndy Whitcroft
12064a0df2efSAndy Whitcroftsub ctx_locate_comment {
12074a0df2efSAndy Whitcroft	my ($first_line, $end_line) = @_;
12084a0df2efSAndy Whitcroft
12094a0df2efSAndy Whitcroft	# Catch a comment on the end of the line itself.
1210beae6332SAndy Whitcroft	my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
12114a0df2efSAndy Whitcroft	return $current_comment if (defined $current_comment);
12124a0df2efSAndy Whitcroft
12134a0df2efSAndy Whitcroft	# Look through the context and try and figure out if there is a
12144a0df2efSAndy Whitcroft	# comment.
12154a0df2efSAndy Whitcroft	my $in_comment = 0;
12164a0df2efSAndy Whitcroft	$current_comment = '';
12174a0df2efSAndy Whitcroft	for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
121800df344fSAndy Whitcroft		my $line = $rawlines[$linenr - 1];
121900df344fSAndy Whitcroft		#warn "           $line\n";
12204a0df2efSAndy Whitcroft		if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
12214a0df2efSAndy Whitcroft			$in_comment = 1;
12224a0df2efSAndy Whitcroft		}
12234a0df2efSAndy Whitcroft		if ($line =~ m@/\*@) {
12244a0df2efSAndy Whitcroft			$in_comment = 1;
12254a0df2efSAndy Whitcroft		}
12264a0df2efSAndy Whitcroft		if (!$in_comment && $current_comment ne '') {
12274a0df2efSAndy Whitcroft			$current_comment = '';
12284a0df2efSAndy Whitcroft		}
12294a0df2efSAndy Whitcroft		$current_comment .= $line . "\n" if ($in_comment);
12304a0df2efSAndy Whitcroft		if ($line =~ m@\*/@) {
12314a0df2efSAndy Whitcroft			$in_comment = 0;
12324a0df2efSAndy Whitcroft		}
12334a0df2efSAndy Whitcroft	}
12344a0df2efSAndy Whitcroft
12354a0df2efSAndy Whitcroft	chomp($current_comment);
12364a0df2efSAndy Whitcroft	return($current_comment);
12374a0df2efSAndy Whitcroft}
12384a0df2efSAndy Whitcroftsub ctx_has_comment {
12394a0df2efSAndy Whitcroft	my ($first_line, $end_line) = @_;
12404a0df2efSAndy Whitcroft	my $cmt = ctx_locate_comment($first_line, $end_line);
12414a0df2efSAndy Whitcroft
124200df344fSAndy Whitcroft	##print "LINE: $rawlines[$end_line - 1 ]\n";
12434a0df2efSAndy Whitcroft	##print "CMMT: $cmt\n";
12444a0df2efSAndy Whitcroft
12454a0df2efSAndy Whitcroft	return ($cmt ne '');
12464a0df2efSAndy Whitcroft}
12474a0df2efSAndy Whitcroft
12484d001e4dSAndy Whitcroftsub raw_line {
12494d001e4dSAndy Whitcroft	my ($linenr, $cnt) = @_;
12504d001e4dSAndy Whitcroft
12514d001e4dSAndy Whitcroft	my $offset = $linenr - 1;
12524d001e4dSAndy Whitcroft	$cnt++;
12534d001e4dSAndy Whitcroft
12544d001e4dSAndy Whitcroft	my $line;
12554d001e4dSAndy Whitcroft	while ($cnt) {
12564d001e4dSAndy Whitcroft		$line = $rawlines[$offset++];
12574d001e4dSAndy Whitcroft		next if (defined($line) && $line =~ /^-/);
12584d001e4dSAndy Whitcroft		$cnt--;
12594d001e4dSAndy Whitcroft	}
12604d001e4dSAndy Whitcroft
12614d001e4dSAndy Whitcroft	return $line;
12624d001e4dSAndy Whitcroft}
12634d001e4dSAndy Whitcroft
12640a920b5bSAndy Whitcroftsub cat_vet {
12650a920b5bSAndy Whitcroft	my ($vet) = @_;
12669c0ca6f9SAndy Whitcroft	my ($res, $coded);
12670a920b5bSAndy Whitcroft
12689c0ca6f9SAndy Whitcroft	$res = '';
12696c72ffaaSAndy Whitcroft	while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
12706c72ffaaSAndy Whitcroft		$res .= $1;
12716c72ffaaSAndy Whitcroft		if ($2 ne '') {
12729c0ca6f9SAndy Whitcroft			$coded = sprintf("^%c", unpack('C', $2) + 64);
12736c72ffaaSAndy Whitcroft			$res .= $coded;
12746c72ffaaSAndy Whitcroft		}
12759c0ca6f9SAndy Whitcroft	}
12769c0ca6f9SAndy Whitcroft	$res =~ s/$/\$/;
12770a920b5bSAndy Whitcroft
12789c0ca6f9SAndy Whitcroft	return $res;
12790a920b5bSAndy Whitcroft}
12800a920b5bSAndy Whitcroft
1281c2fdda0dSAndy Whitcroftmy $av_preprocessor = 0;
1282cf655043SAndy Whitcroftmy $av_pending;
1283c2fdda0dSAndy Whitcroftmy @av_paren_type;
12841f65f947SAndy Whitcroftmy $av_pend_colon;
1285c2fdda0dSAndy Whitcroft
1286c2fdda0dSAndy Whitcroftsub annotate_reset {
1287c2fdda0dSAndy Whitcroft	$av_preprocessor = 0;
1288cf655043SAndy Whitcroft	$av_pending = '_';
1289cf655043SAndy Whitcroft	@av_paren_type = ('E');
12901f65f947SAndy Whitcroft	$av_pend_colon = 'O';
1291c2fdda0dSAndy Whitcroft}
1292c2fdda0dSAndy Whitcroft
12936c72ffaaSAndy Whitcroftsub annotate_values {
12946c72ffaaSAndy Whitcroft	my ($stream, $type) = @_;
12956c72ffaaSAndy Whitcroft
12966c72ffaaSAndy Whitcroft	my $res;
12971f65f947SAndy Whitcroft	my $var = '_' x length($stream);
12986c72ffaaSAndy Whitcroft	my $cur = $stream;
12996c72ffaaSAndy Whitcroft
1300c2fdda0dSAndy Whitcroft	print "$stream\n" if ($dbg_values > 1);
13016c72ffaaSAndy Whitcroft
13026c72ffaaSAndy Whitcroft	while (length($cur)) {
1303773647a0SAndy Whitcroft		@av_paren_type = ('E') if ($#av_paren_type < 0);
1304cf655043SAndy Whitcroft		print " <" . join('', @av_paren_type) .
1305171ae1a4SAndy Whitcroft				"> <$type> <$av_pending>" if ($dbg_values > 1);
13066c72ffaaSAndy Whitcroft		if ($cur =~ /^(\s+)/o) {
1307c2fdda0dSAndy Whitcroft			print "WS($1)\n" if ($dbg_values > 1);
1308c2fdda0dSAndy Whitcroft			if ($1 =~ /\n/ && $av_preprocessor) {
1309cf655043SAndy Whitcroft				$type = pop(@av_paren_type);
1310c2fdda0dSAndy Whitcroft				$av_preprocessor = 0;
13116c72ffaaSAndy Whitcroft			}
13126c72ffaaSAndy Whitcroft
1313c023e473SFlorian Mickler		} elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
13149446ef56SAndy Whitcroft			print "CAST($1)\n" if ($dbg_values > 1);
13159446ef56SAndy Whitcroft			push(@av_paren_type, $type);
1316addcdceaSAndy Whitcroft			$type = 'c';
13179446ef56SAndy Whitcroft
1318e91b6e26SAndy Whitcroft		} elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
1319c2fdda0dSAndy Whitcroft			print "DECLARE($1)\n" if ($dbg_values > 1);
13206c72ffaaSAndy Whitcroft			$type = 'T';
13216c72ffaaSAndy Whitcroft
1322389a2fe5SAndy Whitcroft		} elsif ($cur =~ /^($Modifier)\s*/) {
1323389a2fe5SAndy Whitcroft			print "MODIFIER($1)\n" if ($dbg_values > 1);
1324389a2fe5SAndy Whitcroft			$type = 'T';
1325389a2fe5SAndy Whitcroft
1326c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
1327171ae1a4SAndy Whitcroft			print "DEFINE($1,$2)\n" if ($dbg_values > 1);
1328c2fdda0dSAndy Whitcroft			$av_preprocessor = 1;
1329171ae1a4SAndy Whitcroft			push(@av_paren_type, $type);
1330171ae1a4SAndy Whitcroft			if ($2 ne '') {
1331cf655043SAndy Whitcroft				$av_pending = 'N';
1332171ae1a4SAndy Whitcroft			}
1333171ae1a4SAndy Whitcroft			$type = 'E';
1334171ae1a4SAndy Whitcroft
1335c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
1336171ae1a4SAndy Whitcroft			print "UNDEF($1)\n" if ($dbg_values > 1);
1337171ae1a4SAndy Whitcroft			$av_preprocessor = 1;
1338171ae1a4SAndy Whitcroft			push(@av_paren_type, $type);
13396c72ffaaSAndy Whitcroft
1340c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
1341cf655043SAndy Whitcroft			print "PRE_START($1)\n" if ($dbg_values > 1);
1342c2fdda0dSAndy Whitcroft			$av_preprocessor = 1;
1343cf655043SAndy Whitcroft
1344cf655043SAndy Whitcroft			push(@av_paren_type, $type);
1345cf655043SAndy Whitcroft			push(@av_paren_type, $type);
1346171ae1a4SAndy Whitcroft			$type = 'E';
1347cf655043SAndy Whitcroft
1348c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
1349cf655043SAndy Whitcroft			print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1350cf655043SAndy Whitcroft			$av_preprocessor = 1;
1351cf655043SAndy Whitcroft
1352cf655043SAndy Whitcroft			push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1353cf655043SAndy Whitcroft
1354171ae1a4SAndy Whitcroft			$type = 'E';
1355cf655043SAndy Whitcroft
1356c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:endif))/o) {
1357cf655043SAndy Whitcroft			print "PRE_END($1)\n" if ($dbg_values > 1);
1358cf655043SAndy Whitcroft
1359cf655043SAndy Whitcroft			$av_preprocessor = 1;
1360cf655043SAndy Whitcroft
1361cf655043SAndy Whitcroft			# Assume all arms of the conditional end as this
1362cf655043SAndy Whitcroft			# one does, and continue as if the #endif was not here.
1363cf655043SAndy Whitcroft			pop(@av_paren_type);
1364cf655043SAndy Whitcroft			push(@av_paren_type, $type);
1365171ae1a4SAndy Whitcroft			$type = 'E';
13666c72ffaaSAndy Whitcroft
13676c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\\\n)/o) {
1368c2fdda0dSAndy Whitcroft			print "PRECONT($1)\n" if ($dbg_values > 1);
13696c72ffaaSAndy Whitcroft
1370171ae1a4SAndy Whitcroft		} elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1371171ae1a4SAndy Whitcroft			print "ATTR($1)\n" if ($dbg_values > 1);
1372171ae1a4SAndy Whitcroft			$av_pending = $type;
1373171ae1a4SAndy Whitcroft			$type = 'N';
1374171ae1a4SAndy Whitcroft
13756c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
1376c2fdda0dSAndy Whitcroft			print "SIZEOF($1)\n" if ($dbg_values > 1);
13776c72ffaaSAndy Whitcroft			if (defined $2) {
1378cf655043SAndy Whitcroft				$av_pending = 'V';
13796c72ffaaSAndy Whitcroft			}
13806c72ffaaSAndy Whitcroft			$type = 'N';
13816c72ffaaSAndy Whitcroft
138214b111c1SAndy Whitcroft		} elsif ($cur =~ /^(if|while|for)\b/o) {
1383c2fdda0dSAndy Whitcroft			print "COND($1)\n" if ($dbg_values > 1);
138414b111c1SAndy Whitcroft			$av_pending = 'E';
13856c72ffaaSAndy Whitcroft			$type = 'N';
13866c72ffaaSAndy Whitcroft
13871f65f947SAndy Whitcroft		} elsif ($cur =~/^(case)/o) {
13881f65f947SAndy Whitcroft			print "CASE($1)\n" if ($dbg_values > 1);
13891f65f947SAndy Whitcroft			$av_pend_colon = 'C';
13901f65f947SAndy Whitcroft			$type = 'N';
13911f65f947SAndy Whitcroft
139214b111c1SAndy Whitcroft		} elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
1393c2fdda0dSAndy Whitcroft			print "KEYWORD($1)\n" if ($dbg_values > 1);
13946c72ffaaSAndy Whitcroft			$type = 'N';
13956c72ffaaSAndy Whitcroft
13966c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\()/o) {
1397c2fdda0dSAndy Whitcroft			print "PAREN('$1')\n" if ($dbg_values > 1);
1398cf655043SAndy Whitcroft			push(@av_paren_type, $av_pending);
1399cf655043SAndy Whitcroft			$av_pending = '_';
14006c72ffaaSAndy Whitcroft			$type = 'N';
14016c72ffaaSAndy Whitcroft
14026c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\))/o) {
1403cf655043SAndy Whitcroft			my $new_type = pop(@av_paren_type);
1404cf655043SAndy Whitcroft			if ($new_type ne '_') {
1405cf655043SAndy Whitcroft				$type = $new_type;
1406c2fdda0dSAndy Whitcroft				print "PAREN('$1') -> $type\n"
1407c2fdda0dSAndy Whitcroft							if ($dbg_values > 1);
14086c72ffaaSAndy Whitcroft			} else {
1409c2fdda0dSAndy Whitcroft				print "PAREN('$1')\n" if ($dbg_values > 1);
14106c72ffaaSAndy Whitcroft			}
14116c72ffaaSAndy Whitcroft
1412c8cb2ca3SAndy Whitcroft		} elsif ($cur =~ /^($Ident)\s*\(/o) {
1413c2fdda0dSAndy Whitcroft			print "FUNC($1)\n" if ($dbg_values > 1);
1414c8cb2ca3SAndy Whitcroft			$type = 'V';
1415cf655043SAndy Whitcroft			$av_pending = 'V';
14166c72ffaaSAndy Whitcroft
14178e761b04SAndy Whitcroft		} elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
14188e761b04SAndy Whitcroft			if (defined $2 && $type eq 'C' || $type eq 'T') {
14191f65f947SAndy Whitcroft				$av_pend_colon = 'B';
14208e761b04SAndy Whitcroft			} elsif ($type eq 'E') {
14218e761b04SAndy Whitcroft				$av_pend_colon = 'L';
14221f65f947SAndy Whitcroft			}
14231f65f947SAndy Whitcroft			print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
14241f65f947SAndy Whitcroft			$type = 'V';
14251f65f947SAndy Whitcroft
14266c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Ident|$Constant)/o) {
1427c2fdda0dSAndy Whitcroft			print "IDENT($1)\n" if ($dbg_values > 1);
14286c72ffaaSAndy Whitcroft			$type = 'V';
14296c72ffaaSAndy Whitcroft
14306c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Assignment)/o) {
1431c2fdda0dSAndy Whitcroft			print "ASSIGN($1)\n" if ($dbg_values > 1);
14326c72ffaaSAndy Whitcroft			$type = 'N';
14336c72ffaaSAndy Whitcroft
1434cf655043SAndy Whitcroft		} elsif ($cur =~/^(;|{|})/) {
1435c2fdda0dSAndy Whitcroft			print "END($1)\n" if ($dbg_values > 1);
143613214adfSAndy Whitcroft			$type = 'E';
14371f65f947SAndy Whitcroft			$av_pend_colon = 'O';
143813214adfSAndy Whitcroft
14398e761b04SAndy Whitcroft		} elsif ($cur =~/^(,)/) {
14408e761b04SAndy Whitcroft			print "COMMA($1)\n" if ($dbg_values > 1);
14418e761b04SAndy Whitcroft			$type = 'C';
14428e761b04SAndy Whitcroft
14431f65f947SAndy Whitcroft		} elsif ($cur =~ /^(\?)/o) {
14441f65f947SAndy Whitcroft			print "QUESTION($1)\n" if ($dbg_values > 1);
14451f65f947SAndy Whitcroft			$type = 'N';
14461f65f947SAndy Whitcroft
14471f65f947SAndy Whitcroft		} elsif ($cur =~ /^(:)/o) {
14481f65f947SAndy Whitcroft			print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
14491f65f947SAndy Whitcroft
14501f65f947SAndy Whitcroft			substr($var, length($res), 1, $av_pend_colon);
14511f65f947SAndy Whitcroft			if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
14521f65f947SAndy Whitcroft				$type = 'E';
14531f65f947SAndy Whitcroft			} else {
14541f65f947SAndy Whitcroft				$type = 'N';
14551f65f947SAndy Whitcroft			}
14561f65f947SAndy Whitcroft			$av_pend_colon = 'O';
14571f65f947SAndy Whitcroft
14588e761b04SAndy Whitcroft		} elsif ($cur =~ /^(\[)/o) {
145913214adfSAndy Whitcroft			print "CLOSE($1)\n" if ($dbg_values > 1);
14606c72ffaaSAndy Whitcroft			$type = 'N';
14616c72ffaaSAndy Whitcroft
14620d413866SAndy Whitcroft		} elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
146374048ed8SAndy Whitcroft			my $variant;
146474048ed8SAndy Whitcroft
146574048ed8SAndy Whitcroft			print "OPV($1)\n" if ($dbg_values > 1);
146674048ed8SAndy Whitcroft			if ($type eq 'V') {
146774048ed8SAndy Whitcroft				$variant = 'B';
146874048ed8SAndy Whitcroft			} else {
146974048ed8SAndy Whitcroft				$variant = 'U';
147074048ed8SAndy Whitcroft			}
147174048ed8SAndy Whitcroft
147274048ed8SAndy Whitcroft			substr($var, length($res), 1, $variant);
147374048ed8SAndy Whitcroft			$type = 'N';
147474048ed8SAndy Whitcroft
14756c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Operators)/o) {
1476c2fdda0dSAndy Whitcroft			print "OP($1)\n" if ($dbg_values > 1);
14776c72ffaaSAndy Whitcroft			if ($1 ne '++' && $1 ne '--') {
14786c72ffaaSAndy Whitcroft				$type = 'N';
14796c72ffaaSAndy Whitcroft			}
14806c72ffaaSAndy Whitcroft
14816c72ffaaSAndy Whitcroft		} elsif ($cur =~ /(^.)/o) {
1482c2fdda0dSAndy Whitcroft			print "C($1)\n" if ($dbg_values > 1);
14836c72ffaaSAndy Whitcroft		}
14846c72ffaaSAndy Whitcroft		if (defined $1) {
14856c72ffaaSAndy Whitcroft			$cur = substr($cur, length($1));
14866c72ffaaSAndy Whitcroft			$res .= $type x length($1);
14876c72ffaaSAndy Whitcroft		}
14886c72ffaaSAndy Whitcroft	}
14896c72ffaaSAndy Whitcroft
14901f65f947SAndy Whitcroft	return ($res, $var);
14916c72ffaaSAndy Whitcroft}
14926c72ffaaSAndy Whitcroft
14938905a67cSAndy Whitcroftsub possible {
149413214adfSAndy Whitcroft	my ($possible, $line) = @_;
14959a974fdbSAndy Whitcroft	my $notPermitted = qr{(?:
14960776e594SAndy Whitcroft		^(?:
14970776e594SAndy Whitcroft			$Modifier|
14980776e594SAndy Whitcroft			$Storage|
14990776e594SAndy Whitcroft			$Type|
15009a974fdbSAndy Whitcroft			DEFINE_\S+
15019a974fdbSAndy Whitcroft		)$|
15029a974fdbSAndy Whitcroft		^(?:
15030776e594SAndy Whitcroft			goto|
15040776e594SAndy Whitcroft			return|
15050776e594SAndy Whitcroft			case|
15060776e594SAndy Whitcroft			else|
15070776e594SAndy Whitcroft			asm|__asm__|
150889a88353SAndy Whitcroft			do|
150989a88353SAndy Whitcroft			\#|
151089a88353SAndy Whitcroft			\#\#|
15119a974fdbSAndy Whitcroft		)(?:\s|$)|
15120776e594SAndy Whitcroft		^(?:typedef|struct|enum)\b
15139a974fdbSAndy Whitcroft	    )}x;
15149a974fdbSAndy Whitcroft	warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
15159a974fdbSAndy Whitcroft	if ($possible !~ $notPermitted) {
1516c45dcabdSAndy Whitcroft		# Check for modifiers.
1517c45dcabdSAndy Whitcroft		$possible =~ s/\s*$Storage\s*//g;
1518c45dcabdSAndy Whitcroft		$possible =~ s/\s*$Sparse\s*//g;
1519c45dcabdSAndy Whitcroft		if ($possible =~ /^\s*$/) {
1520c45dcabdSAndy Whitcroft
1521c45dcabdSAndy Whitcroft		} elsif ($possible =~ /\s/) {
1522c45dcabdSAndy Whitcroft			$possible =~ s/\s*$Type\s*//g;
1523d2506586SAndy Whitcroft			for my $modifier (split(' ', $possible)) {
15249a974fdbSAndy Whitcroft				if ($modifier !~ $notPermitted) {
1525d2506586SAndy Whitcroft					warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1526d2506586SAndy Whitcroft					push(@modifierList, $modifier);
1527d2506586SAndy Whitcroft				}
15289a974fdbSAndy Whitcroft			}
1529c45dcabdSAndy Whitcroft
1530c45dcabdSAndy Whitcroft		} else {
153113214adfSAndy Whitcroft			warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
15328905a67cSAndy Whitcroft			push(@typeList, $possible);
1533c45dcabdSAndy Whitcroft		}
15348905a67cSAndy Whitcroft		build_types();
15350776e594SAndy Whitcroft	} else {
15360776e594SAndy Whitcroft		warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
15378905a67cSAndy Whitcroft	}
15388905a67cSAndy Whitcroft}
15398905a67cSAndy Whitcroft
15406c72ffaaSAndy Whitcroftmy $prefix = '';
15416c72ffaaSAndy Whitcroft
1542000d1cc1SJoe Perchessub show_type {
1543cbec18afSJoe Perches	my ($type) = @_;
154491bfe484SJoe Perches
1545cbec18afSJoe Perches	return defined $use_type{$type} if (scalar keys %use_type > 0);
1546cbec18afSJoe Perches
1547cbec18afSJoe Perches	return !defined $ignore_type{$type};
1548000d1cc1SJoe Perches}
1549000d1cc1SJoe Perches
1550f0a594c1SAndy Whitcroftsub report {
1551cbec18afSJoe Perches	my ($level, $type, $msg) = @_;
1552cbec18afSJoe Perches
1553cbec18afSJoe Perches	if (!show_type($type) ||
1554cbec18afSJoe Perches	    (defined $tst_only && $msg !~ /\Q$tst_only\E/)) {
1555773647a0SAndy Whitcroft		return 0;
1556773647a0SAndy Whitcroft	}
1557000d1cc1SJoe Perches	my $line;
1558000d1cc1SJoe Perches	if ($show_types) {
1559cbec18afSJoe Perches		$line = "$prefix$level:$type: $msg\n";
1560000d1cc1SJoe Perches	} else {
1561cbec18afSJoe Perches		$line = "$prefix$level: $msg\n";
1562000d1cc1SJoe Perches	}
15638905a67cSAndy Whitcroft	$line = (split('\n', $line))[0] . "\n" if ($terse);
15648905a67cSAndy Whitcroft
156513214adfSAndy Whitcroft	push(our @report, $line);
1566773647a0SAndy Whitcroft
1567773647a0SAndy Whitcroft	return 1;
1568f0a594c1SAndy Whitcroft}
1569cbec18afSJoe Perches
1570f0a594c1SAndy Whitcroftsub report_dump {
157113214adfSAndy Whitcroft	our @report;
1572f0a594c1SAndy Whitcroft}
1573000d1cc1SJoe Perches
1574d752fcc8SJoe Perchessub fixup_current_range {
1575d752fcc8SJoe Perches	my ($lineRef, $offset, $length) = @_;
1576d752fcc8SJoe Perches
1577d752fcc8SJoe Perches	if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) {
1578d752fcc8SJoe Perches		my $o = $1;
1579d752fcc8SJoe Perches		my $l = $2;
1580d752fcc8SJoe Perches		my $no = $o + $offset;
1581d752fcc8SJoe Perches		my $nl = $l + $length;
1582d752fcc8SJoe Perches		$$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/;
1583d752fcc8SJoe Perches	}
1584d752fcc8SJoe Perches}
1585d752fcc8SJoe Perches
1586d752fcc8SJoe Perchessub fix_inserted_deleted_lines {
1587d752fcc8SJoe Perches	my ($linesRef, $insertedRef, $deletedRef) = @_;
1588d752fcc8SJoe Perches
1589d752fcc8SJoe Perches	my $range_last_linenr = 0;
1590d752fcc8SJoe Perches	my $delta_offset = 0;
1591d752fcc8SJoe Perches
1592d752fcc8SJoe Perches	my $old_linenr = 0;
1593d752fcc8SJoe Perches	my $new_linenr = 0;
1594d752fcc8SJoe Perches
1595d752fcc8SJoe Perches	my $next_insert = 0;
1596d752fcc8SJoe Perches	my $next_delete = 0;
1597d752fcc8SJoe Perches
1598d752fcc8SJoe Perches	my @lines = ();
1599d752fcc8SJoe Perches
1600d752fcc8SJoe Perches	my $inserted = @{$insertedRef}[$next_insert++];
1601d752fcc8SJoe Perches	my $deleted = @{$deletedRef}[$next_delete++];
1602d752fcc8SJoe Perches
1603d752fcc8SJoe Perches	foreach my $old_line (@{$linesRef}) {
1604d752fcc8SJoe Perches		my $save_line = 1;
1605d752fcc8SJoe Perches		my $line = $old_line;	#don't modify the array
1606d752fcc8SJoe Perches		if ($line =~ /^(?:\+\+\+\|\-\-\-)\s+\S+/) {	#new filename
1607d752fcc8SJoe Perches			$delta_offset = 0;
1608d752fcc8SJoe Perches		} elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) {	#new hunk
1609d752fcc8SJoe Perches			$range_last_linenr = $new_linenr;
1610d752fcc8SJoe Perches			fixup_current_range(\$line, $delta_offset, 0);
1611d752fcc8SJoe Perches		}
1612d752fcc8SJoe Perches
1613d752fcc8SJoe Perches		while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) {
1614d752fcc8SJoe Perches			$deleted = @{$deletedRef}[$next_delete++];
1615d752fcc8SJoe Perches			$save_line = 0;
1616d752fcc8SJoe Perches			fixup_current_range(\$lines[$range_last_linenr], $delta_offset--, -1);
1617d752fcc8SJoe Perches		}
1618d752fcc8SJoe Perches
1619d752fcc8SJoe Perches		while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) {
1620d752fcc8SJoe Perches			push(@lines, ${$inserted}{'LINE'});
1621d752fcc8SJoe Perches			$inserted = @{$insertedRef}[$next_insert++];
1622d752fcc8SJoe Perches			$new_linenr++;
1623d752fcc8SJoe Perches			fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1);
1624d752fcc8SJoe Perches		}
1625d752fcc8SJoe Perches
1626d752fcc8SJoe Perches		if ($save_line) {
1627d752fcc8SJoe Perches			push(@lines, $line);
1628d752fcc8SJoe Perches			$new_linenr++;
1629d752fcc8SJoe Perches		}
1630d752fcc8SJoe Perches
1631d752fcc8SJoe Perches		$old_linenr++;
1632d752fcc8SJoe Perches	}
1633d752fcc8SJoe Perches
1634d752fcc8SJoe Perches	return @lines;
1635d752fcc8SJoe Perches}
1636d752fcc8SJoe Perches
1637f2d7e4d4SJoe Perchessub fix_insert_line {
1638f2d7e4d4SJoe Perches	my ($linenr, $line) = @_;
1639f2d7e4d4SJoe Perches
1640f2d7e4d4SJoe Perches	my $inserted = {
1641f2d7e4d4SJoe Perches		LINENR => $linenr,
1642f2d7e4d4SJoe Perches		LINE => $line,
1643f2d7e4d4SJoe Perches	};
1644f2d7e4d4SJoe Perches	push(@fixed_inserted, $inserted);
1645f2d7e4d4SJoe Perches}
1646f2d7e4d4SJoe Perches
1647f2d7e4d4SJoe Perchessub fix_delete_line {
1648f2d7e4d4SJoe Perches	my ($linenr, $line) = @_;
1649f2d7e4d4SJoe Perches
1650f2d7e4d4SJoe Perches	my $deleted = {
1651f2d7e4d4SJoe Perches		LINENR => $linenr,
1652f2d7e4d4SJoe Perches		LINE => $line,
1653f2d7e4d4SJoe Perches	};
1654f2d7e4d4SJoe Perches
1655f2d7e4d4SJoe Perches	push(@fixed_deleted, $deleted);
1656f2d7e4d4SJoe Perches}
1657f2d7e4d4SJoe Perches
1658de7d4f0eSAndy Whitcroftsub ERROR {
1659cbec18afSJoe Perches	my ($type, $msg) = @_;
1660cbec18afSJoe Perches
1661cbec18afSJoe Perches	if (report("ERROR", $type, $msg)) {
1662de7d4f0eSAndy Whitcroft		our $clean = 0;
16636c72ffaaSAndy Whitcroft		our $cnt_error++;
16643705ce5bSJoe Perches		return 1;
1665de7d4f0eSAndy Whitcroft	}
16663705ce5bSJoe Perches	return 0;
1667773647a0SAndy Whitcroft}
1668de7d4f0eSAndy Whitcroftsub WARN {
1669cbec18afSJoe Perches	my ($type, $msg) = @_;
1670cbec18afSJoe Perches
1671cbec18afSJoe Perches	if (report("WARNING", $type, $msg)) {
1672de7d4f0eSAndy Whitcroft		our $clean = 0;
16736c72ffaaSAndy Whitcroft		our $cnt_warn++;
16743705ce5bSJoe Perches		return 1;
1675de7d4f0eSAndy Whitcroft	}
16763705ce5bSJoe Perches	return 0;
1677773647a0SAndy Whitcroft}
1678de7d4f0eSAndy Whitcroftsub CHK {
1679cbec18afSJoe Perches	my ($type, $msg) = @_;
1680cbec18afSJoe Perches
1681cbec18afSJoe Perches	if ($check && report("CHECK", $type, $msg)) {
1682de7d4f0eSAndy Whitcroft		our $clean = 0;
16836c72ffaaSAndy Whitcroft		our $cnt_chk++;
16843705ce5bSJoe Perches		return 1;
16856c72ffaaSAndy Whitcroft	}
16863705ce5bSJoe Perches	return 0;
1687de7d4f0eSAndy Whitcroft}
1688de7d4f0eSAndy Whitcroft
16896ecd9674SAndy Whitcroftsub check_absolute_file {
16906ecd9674SAndy Whitcroft	my ($absolute, $herecurr) = @_;
16916ecd9674SAndy Whitcroft	my $file = $absolute;
16926ecd9674SAndy Whitcroft
16936ecd9674SAndy Whitcroft	##print "absolute<$absolute>\n";
16946ecd9674SAndy Whitcroft
16956ecd9674SAndy Whitcroft	# See if any suffix of this path is a path within the tree.
16966ecd9674SAndy Whitcroft	while ($file =~ s@^[^/]*/@@) {
16976ecd9674SAndy Whitcroft		if (-f "$root/$file") {
16986ecd9674SAndy Whitcroft			##print "file<$file>\n";
16996ecd9674SAndy Whitcroft			last;
17006ecd9674SAndy Whitcroft		}
17016ecd9674SAndy Whitcroft	}
17026ecd9674SAndy Whitcroft	if (! -f _)  {
17036ecd9674SAndy Whitcroft		return 0;
17046ecd9674SAndy Whitcroft	}
17056ecd9674SAndy Whitcroft
17066ecd9674SAndy Whitcroft	# It is, so see if the prefix is acceptable.
17076ecd9674SAndy Whitcroft	my $prefix = $absolute;
17086ecd9674SAndy Whitcroft	substr($prefix, -length($file)) = '';
17096ecd9674SAndy Whitcroft
17106ecd9674SAndy Whitcroft	##print "prefix<$prefix>\n";
17116ecd9674SAndy Whitcroft	if ($prefix ne ".../") {
1712000d1cc1SJoe Perches		WARN("USE_RELATIVE_PATH",
1713000d1cc1SJoe Perches		     "use relative pathname instead of absolute in changelog text\n" . $herecurr);
17146ecd9674SAndy Whitcroft	}
17156ecd9674SAndy Whitcroft}
17166ecd9674SAndy Whitcroft
17173705ce5bSJoe Perchessub trim {
17183705ce5bSJoe Perches	my ($string) = @_;
17193705ce5bSJoe Perches
1720b34c648bSJoe Perches	$string =~ s/^\s+|\s+$//g;
1721b34c648bSJoe Perches
1722b34c648bSJoe Perches	return $string;
1723b34c648bSJoe Perches}
1724b34c648bSJoe Perches
1725b34c648bSJoe Perchessub ltrim {
1726b34c648bSJoe Perches	my ($string) = @_;
1727b34c648bSJoe Perches
1728b34c648bSJoe Perches	$string =~ s/^\s+//;
1729b34c648bSJoe Perches
1730b34c648bSJoe Perches	return $string;
1731b34c648bSJoe Perches}
1732b34c648bSJoe Perches
1733b34c648bSJoe Perchessub rtrim {
1734b34c648bSJoe Perches	my ($string) = @_;
1735b34c648bSJoe Perches
1736b34c648bSJoe Perches	$string =~ s/\s+$//;
17373705ce5bSJoe Perches
17383705ce5bSJoe Perches	return $string;
17393705ce5bSJoe Perches}
17403705ce5bSJoe Perches
174152ea8506SJoe Perchessub string_find_replace {
174252ea8506SJoe Perches	my ($string, $find, $replace) = @_;
174352ea8506SJoe Perches
174452ea8506SJoe Perches	$string =~ s/$find/$replace/g;
174552ea8506SJoe Perches
174652ea8506SJoe Perches	return $string;
174752ea8506SJoe Perches}
174852ea8506SJoe Perches
17493705ce5bSJoe Perchessub tabify {
17503705ce5bSJoe Perches	my ($leading) = @_;
17513705ce5bSJoe Perches
17523705ce5bSJoe Perches	my $source_indent = 8;
17533705ce5bSJoe Perches	my $max_spaces_before_tab = $source_indent - 1;
17543705ce5bSJoe Perches	my $spaces_to_tab = " " x $source_indent;
17553705ce5bSJoe Perches
17563705ce5bSJoe Perches	#convert leading spaces to tabs
17573705ce5bSJoe Perches	1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
17583705ce5bSJoe Perches	#Remove spaces before a tab
17593705ce5bSJoe Perches	1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
17603705ce5bSJoe Perches
17613705ce5bSJoe Perches	return "$leading";
17623705ce5bSJoe Perches}
17633705ce5bSJoe Perches
1764d1fe9c09SJoe Perchessub pos_last_openparen {
1765d1fe9c09SJoe Perches	my ($line) = @_;
1766d1fe9c09SJoe Perches
1767d1fe9c09SJoe Perches	my $pos = 0;
1768d1fe9c09SJoe Perches
1769d1fe9c09SJoe Perches	my $opens = $line =~ tr/\(/\(/;
1770d1fe9c09SJoe Perches	my $closes = $line =~ tr/\)/\)/;
1771d1fe9c09SJoe Perches
1772d1fe9c09SJoe Perches	my $last_openparen = 0;
1773d1fe9c09SJoe Perches
1774d1fe9c09SJoe Perches	if (($opens == 0) || ($closes >= $opens)) {
1775d1fe9c09SJoe Perches		return -1;
1776d1fe9c09SJoe Perches	}
1777d1fe9c09SJoe Perches
1778d1fe9c09SJoe Perches	my $len = length($line);
1779d1fe9c09SJoe Perches
1780d1fe9c09SJoe Perches	for ($pos = 0; $pos < $len; $pos++) {
1781d1fe9c09SJoe Perches		my $string = substr($line, $pos);
1782d1fe9c09SJoe Perches		if ($string =~ /^($FuncArg|$balanced_parens)/) {
1783d1fe9c09SJoe Perches			$pos += length($1) - 1;
1784d1fe9c09SJoe Perches		} elsif (substr($line, $pos, 1) eq '(') {
1785d1fe9c09SJoe Perches			$last_openparen = $pos;
1786d1fe9c09SJoe Perches		} elsif (index($string, '(') == -1) {
1787d1fe9c09SJoe Perches			last;
1788d1fe9c09SJoe Perches		}
1789d1fe9c09SJoe Perches	}
1790d1fe9c09SJoe Perches
179191cb5195SJoe Perches	return length(expand_tabs(substr($line, 0, $last_openparen))) + 1;
1792d1fe9c09SJoe Perches}
1793d1fe9c09SJoe Perches
17940a920b5bSAndy Whitcroftsub process {
17950a920b5bSAndy Whitcroft	my $filename = shift;
17960a920b5bSAndy Whitcroft
17970a920b5bSAndy Whitcroft	my $linenr=0;
17980a920b5bSAndy Whitcroft	my $prevline="";
1799c2fdda0dSAndy Whitcroft	my $prevrawline="";
18000a920b5bSAndy Whitcroft	my $stashline="";
1801c2fdda0dSAndy Whitcroft	my $stashrawline="";
18020a920b5bSAndy Whitcroft
18034a0df2efSAndy Whitcroft	my $length;
18040a920b5bSAndy Whitcroft	my $indent;
18050a920b5bSAndy Whitcroft	my $previndent=0;
18060a920b5bSAndy Whitcroft	my $stashindent=0;
18070a920b5bSAndy Whitcroft
1808de7d4f0eSAndy Whitcroft	our $clean = 1;
18090a920b5bSAndy Whitcroft	my $signoff = 0;
18100a920b5bSAndy Whitcroft	my $is_patch = 0;
18110a920b5bSAndy Whitcroft
181229ee1b0cSJoe Perches	my $in_header_lines = $file ? 0 : 1;
181315662b3eSJoe Perches	my $in_commit_log = 0;		#Scanning lines before patch
181413f1937eSJoe Perches	my $reported_maintainer_file = 0;
1815fa64205dSPasi Savanainen	my $non_utf8_charset = 0;
1816fa64205dSPasi Savanainen
1817365dd4eaSJoe Perches	my $last_blank_line = 0;
1818365dd4eaSJoe Perches
181913214adfSAndy Whitcroft	our @report = ();
18206c72ffaaSAndy Whitcroft	our $cnt_lines = 0;
18216c72ffaaSAndy Whitcroft	our $cnt_error = 0;
18226c72ffaaSAndy Whitcroft	our $cnt_warn = 0;
18236c72ffaaSAndy Whitcroft	our $cnt_chk = 0;
18246c72ffaaSAndy Whitcroft
18250a920b5bSAndy Whitcroft	# Trace the real file/line as we go.
18260a920b5bSAndy Whitcroft	my $realfile = '';
18270a920b5bSAndy Whitcroft	my $realline = 0;
18280a920b5bSAndy Whitcroft	my $realcnt = 0;
18290a920b5bSAndy Whitcroft	my $here = '';
18300a920b5bSAndy Whitcroft	my $in_comment = 0;
1831c2fdda0dSAndy Whitcroft	my $comment_edge = 0;
18320a920b5bSAndy Whitcroft	my $first_line = 0;
18331e855726SWolfram Sang	my $p1_prefix = '';
18340a920b5bSAndy Whitcroft
183513214adfSAndy Whitcroft	my $prev_values = 'E';
183613214adfSAndy Whitcroft
183713214adfSAndy Whitcroft	# suppression flags
1838773647a0SAndy Whitcroft	my %suppress_ifbraces;
1839170d3a22SAndy Whitcroft	my %suppress_whiletrailers;
18402b474a1aSAndy Whitcroft	my %suppress_export;
18413e469cdcSAndy Whitcroft	my $suppress_statement = 0;
1842653d4876SAndy Whitcroft
18437e51f197SJoe Perches	my %signatures = ();
1844323c1260SJoe Perches
1845c2fdda0dSAndy Whitcroft	# Pre-scan the patch sanitizing the lines.
1846de7d4f0eSAndy Whitcroft	# Pre-scan the patch looking for any __setup documentation.
1847c2fdda0dSAndy Whitcroft	#
1848de7d4f0eSAndy Whitcroft	my @setup_docs = ();
1849de7d4f0eSAndy Whitcroft	my $setup_docs = 0;
1850773647a0SAndy Whitcroft
1851d8b07710SJoe Perches	my $camelcase_file_seeded = 0;
1852d8b07710SJoe Perches
1853773647a0SAndy Whitcroft	sanitise_line_reset();
1854c2fdda0dSAndy Whitcroft	my $line;
1855c2fdda0dSAndy Whitcroft	foreach my $rawline (@rawlines) {
1856773647a0SAndy Whitcroft		$linenr++;
1857773647a0SAndy Whitcroft		$line = $rawline;
1858c2fdda0dSAndy Whitcroft
18593705ce5bSJoe Perches		push(@fixed, $rawline) if ($fix);
18603705ce5bSJoe Perches
1861773647a0SAndy Whitcroft		if ($rawline=~/^\+\+\+\s+(\S+)/) {
1862de7d4f0eSAndy Whitcroft			$setup_docs = 0;
1863de7d4f0eSAndy Whitcroft			if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
1864de7d4f0eSAndy Whitcroft				$setup_docs = 1;
1865de7d4f0eSAndy Whitcroft			}
1866773647a0SAndy Whitcroft			#next;
1867de7d4f0eSAndy Whitcroft		}
1868773647a0SAndy Whitcroft		if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1869773647a0SAndy Whitcroft			$realline=$1-1;
1870773647a0SAndy Whitcroft			if (defined $2) {
1871773647a0SAndy Whitcroft				$realcnt=$3+1;
1872773647a0SAndy Whitcroft			} else {
1873773647a0SAndy Whitcroft				$realcnt=1+1;
1874773647a0SAndy Whitcroft			}
1875c45dcabdSAndy Whitcroft			$in_comment = 0;
1876773647a0SAndy Whitcroft
1877773647a0SAndy Whitcroft			# Guestimate if this is a continuing comment.  Run
1878773647a0SAndy Whitcroft			# the context looking for a comment "edge".  If this
1879773647a0SAndy Whitcroft			# edge is a close comment then we must be in a comment
1880773647a0SAndy Whitcroft			# at context start.
1881773647a0SAndy Whitcroft			my $edge;
188201fa9147SAndy Whitcroft			my $cnt = $realcnt;
188301fa9147SAndy Whitcroft			for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
188401fa9147SAndy Whitcroft				next if (defined $rawlines[$ln - 1] &&
188501fa9147SAndy Whitcroft					 $rawlines[$ln - 1] =~ /^-/);
188601fa9147SAndy Whitcroft				$cnt--;
188701fa9147SAndy Whitcroft				#print "RAW<$rawlines[$ln - 1]>\n";
1888721c1cb6SAndy Whitcroft				last if (!defined $rawlines[$ln - 1]);
1889fae17daeSAndy Whitcroft				if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
1890fae17daeSAndy Whitcroft				    $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
1891fae17daeSAndy Whitcroft					($edge) = $1;
1892fae17daeSAndy Whitcroft					last;
1893fae17daeSAndy Whitcroft				}
1894773647a0SAndy Whitcroft			}
1895773647a0SAndy Whitcroft			if (defined $edge && $edge eq '*/') {
1896773647a0SAndy Whitcroft				$in_comment = 1;
1897773647a0SAndy Whitcroft			}
1898773647a0SAndy Whitcroft
1899773647a0SAndy Whitcroft			# Guestimate if this is a continuing comment.  If this
1900773647a0SAndy Whitcroft			# is the start of a diff block and this line starts
1901773647a0SAndy Whitcroft			# ' *' then it is very likely a comment.
1902773647a0SAndy Whitcroft			if (!defined $edge &&
190383242e0cSAndy Whitcroft			    $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
1904773647a0SAndy Whitcroft			{
1905773647a0SAndy Whitcroft				$in_comment = 1;
1906773647a0SAndy Whitcroft			}
1907773647a0SAndy Whitcroft
1908773647a0SAndy Whitcroft			##print "COMMENT:$in_comment edge<$edge> $rawline\n";
1909773647a0SAndy Whitcroft			sanitise_line_reset($in_comment);
1910773647a0SAndy Whitcroft
1911171ae1a4SAndy Whitcroft		} elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
1912773647a0SAndy Whitcroft			# Standardise the strings and chars within the input to
1913171ae1a4SAndy Whitcroft			# simplify matching -- only bother with positive lines.
1914773647a0SAndy Whitcroft			$line = sanitise_line($rawline);
1915773647a0SAndy Whitcroft		}
1916773647a0SAndy Whitcroft		push(@lines, $line);
1917773647a0SAndy Whitcroft
1918773647a0SAndy Whitcroft		if ($realcnt > 1) {
1919773647a0SAndy Whitcroft			$realcnt-- if ($line =~ /^(?:\+| |$)/);
1920773647a0SAndy Whitcroft		} else {
1921773647a0SAndy Whitcroft			$realcnt = 0;
1922773647a0SAndy Whitcroft		}
1923773647a0SAndy Whitcroft
1924773647a0SAndy Whitcroft		#print "==>$rawline\n";
1925773647a0SAndy Whitcroft		#print "-->$line\n";
1926de7d4f0eSAndy Whitcroft
1927de7d4f0eSAndy Whitcroft		if ($setup_docs && $line =~ /^\+/) {
1928de7d4f0eSAndy Whitcroft			push(@setup_docs, $line);
1929de7d4f0eSAndy Whitcroft		}
1930de7d4f0eSAndy Whitcroft	}
1931de7d4f0eSAndy Whitcroft
19326c72ffaaSAndy Whitcroft	$prefix = '';
19336c72ffaaSAndy Whitcroft
1934773647a0SAndy Whitcroft	$realcnt = 0;
1935773647a0SAndy Whitcroft	$linenr = 0;
1936194f66fcSJoe Perches	$fixlinenr = -1;
19370a920b5bSAndy Whitcroft	foreach my $line (@lines) {
19380a920b5bSAndy Whitcroft		$linenr++;
1939194f66fcSJoe Perches		$fixlinenr++;
19401b5539b1SJoe Perches		my $sline = $line;	#copy of $line
19411b5539b1SJoe Perches		$sline =~ s/$;/ /g;	#with comments as spaces
19420a920b5bSAndy Whitcroft
1943c2fdda0dSAndy Whitcroft		my $rawline = $rawlines[$linenr - 1];
19446c72ffaaSAndy Whitcroft
19450a920b5bSAndy Whitcroft#extract the line range in the file after the patch is applied
19466c72ffaaSAndy Whitcroft		if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
19470a920b5bSAndy Whitcroft			$is_patch = 1;
19484a0df2efSAndy Whitcroft			$first_line = $linenr + 1;
19490a920b5bSAndy Whitcroft			$realline=$1-1;
19500a920b5bSAndy Whitcroft			if (defined $2) {
19510a920b5bSAndy Whitcroft				$realcnt=$3+1;
19520a920b5bSAndy Whitcroft			} else {
19530a920b5bSAndy Whitcroft				$realcnt=1+1;
19540a920b5bSAndy Whitcroft			}
1955c2fdda0dSAndy Whitcroft			annotate_reset();
195613214adfSAndy Whitcroft			$prev_values = 'E';
195713214adfSAndy Whitcroft
1958773647a0SAndy Whitcroft			%suppress_ifbraces = ();
1959170d3a22SAndy Whitcroft			%suppress_whiletrailers = ();
19602b474a1aSAndy Whitcroft			%suppress_export = ();
19613e469cdcSAndy Whitcroft			$suppress_statement = 0;
19620a920b5bSAndy Whitcroft			next;
19630a920b5bSAndy Whitcroft
19644a0df2efSAndy Whitcroft# track the line number as we move through the hunk, note that
19654a0df2efSAndy Whitcroft# new versions of GNU diff omit the leading space on completely
19664a0df2efSAndy Whitcroft# blank context lines so we need to count that too.
1967773647a0SAndy Whitcroft		} elsif ($line =~ /^( |\+|$)/) {
19680a920b5bSAndy Whitcroft			$realline++;
1969d8aaf121SAndy Whitcroft			$realcnt-- if ($realcnt != 0);
19700a920b5bSAndy Whitcroft
19714a0df2efSAndy Whitcroft			# Measure the line length and indent.
1972c2fdda0dSAndy Whitcroft			($length, $indent) = line_stats($rawline);
19730a920b5bSAndy Whitcroft
19740a920b5bSAndy Whitcroft			# Track the previous line.
19750a920b5bSAndy Whitcroft			($prevline, $stashline) = ($stashline, $line);
19760a920b5bSAndy Whitcroft			($previndent, $stashindent) = ($stashindent, $indent);
1977c2fdda0dSAndy Whitcroft			($prevrawline, $stashrawline) = ($stashrawline, $rawline);
1978c2fdda0dSAndy Whitcroft
1979773647a0SAndy Whitcroft			#warn "line<$line>\n";
19806c72ffaaSAndy Whitcroft
1981d8aaf121SAndy Whitcroft		} elsif ($realcnt == 1) {
1982d8aaf121SAndy Whitcroft			$realcnt--;
19830a920b5bSAndy Whitcroft		}
19840a920b5bSAndy Whitcroft
1985cc77cdcaSAndy Whitcroft		my $hunk_line = ($realcnt != 0);
1986cc77cdcaSAndy Whitcroft
19870a920b5bSAndy Whitcroft#make up the handle for any error we report on this line
1988773647a0SAndy Whitcroft		$prefix = "$filename:$realline: " if ($emacs && $file);
1989773647a0SAndy Whitcroft		$prefix = "$filename:$linenr: " if ($emacs && !$file);
1990773647a0SAndy Whitcroft
19916c72ffaaSAndy Whitcroft		$here = "#$linenr: " if (!$file);
19926c72ffaaSAndy Whitcroft		$here = "#$realline: " if ($file);
1993773647a0SAndy Whitcroft
19942ac73b4fSJoe Perches		my $found_file = 0;
1995773647a0SAndy Whitcroft		# extract the filename as it passes
19963bf9a009SRabin Vincent		if ($line =~ /^diff --git.*?(\S+)$/) {
19973bf9a009SRabin Vincent			$realfile = $1;
19982b7ab453SJoe Perches			$realfile =~ s@^([^/]*)/@@ if (!$file);
1999270c49a0SJoe Perches			$in_commit_log = 0;
20002ac73b4fSJoe Perches			$found_file = 1;
20013bf9a009SRabin Vincent		} elsif ($line =~ /^\+\+\+\s+(\S+)/) {
2002773647a0SAndy Whitcroft			$realfile = $1;
20032b7ab453SJoe Perches			$realfile =~ s@^([^/]*)/@@ if (!$file);
2004270c49a0SJoe Perches			$in_commit_log = 0;
20051e855726SWolfram Sang
20061e855726SWolfram Sang			$p1_prefix = $1;
2007e2f7aa4bSAndy Whitcroft			if (!$file && $tree && $p1_prefix ne '' &&
2008e2f7aa4bSAndy Whitcroft			    -e "$root/$p1_prefix") {
2009000d1cc1SJoe Perches				WARN("PATCH_PREFIX",
2010000d1cc1SJoe Perches				     "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
20111e855726SWolfram Sang			}
2012773647a0SAndy Whitcroft
2013c1ab3326SAndy Whitcroft			if ($realfile =~ m@^include/asm/@) {
2014000d1cc1SJoe Perches				ERROR("MODIFIED_INCLUDE_ASM",
2015000d1cc1SJoe Perches				      "do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
2016773647a0SAndy Whitcroft			}
20172ac73b4fSJoe Perches			$found_file = 1;
20182ac73b4fSJoe Perches		}
20192ac73b4fSJoe Perches
20202ac73b4fSJoe Perches		if ($found_file) {
20212ac73b4fSJoe Perches			if ($realfile =~ m@^(drivers/net/|net/)@) {
20222ac73b4fSJoe Perches				$check = 1;
20232ac73b4fSJoe Perches			} else {
20242ac73b4fSJoe Perches				$check = $check_orig;
20252ac73b4fSJoe Perches			}
2026773647a0SAndy Whitcroft			next;
2027773647a0SAndy Whitcroft		}
2028773647a0SAndy Whitcroft
2029389834b6SRandy Dunlap		$here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
20300a920b5bSAndy Whitcroft
2031c2fdda0dSAndy Whitcroft		my $hereline = "$here\n$rawline\n";
2032c2fdda0dSAndy Whitcroft		my $herecurr = "$here\n$rawline\n";
2033c2fdda0dSAndy Whitcroft		my $hereprev = "$here\n$prevrawline\n$rawline\n";
20340a920b5bSAndy Whitcroft
20356c72ffaaSAndy Whitcroft		$cnt_lines++ if ($realcnt != 0);
20366c72ffaaSAndy Whitcroft
20373bf9a009SRabin Vincent# Check for incorrect file permissions
20383bf9a009SRabin Vincent		if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
20393bf9a009SRabin Vincent			my $permhere = $here . "FILE: $realfile\n";
204004db4d25SJoe Perches			if ($realfile !~ m@scripts/@ &&
204104db4d25SJoe Perches			    $realfile !~ /\.(py|pl|awk|sh)$/) {
2042000d1cc1SJoe Perches				ERROR("EXECUTE_PERMISSIONS",
2043000d1cc1SJoe Perches				      "do not set execute permissions for source files\n" . $permhere);
20443bf9a009SRabin Vincent			}
20453bf9a009SRabin Vincent		}
20463bf9a009SRabin Vincent
204720112475SJoe Perches# Check the patch for a signoff:
2048d8aaf121SAndy Whitcroft		if ($line =~ /^\s*signed-off-by:/i) {
20494a0df2efSAndy Whitcroft			$signoff++;
205015662b3eSJoe Perches			$in_commit_log = 0;
20510a920b5bSAndy Whitcroft		}
205220112475SJoe Perches
205320112475SJoe Perches# Check signature styles
2054270c49a0SJoe Perches		if (!$in_header_lines &&
2055ce0338dfSJoe Perches		    $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
205620112475SJoe Perches			my $space_before = $1;
205720112475SJoe Perches			my $sign_off = $2;
205820112475SJoe Perches			my $space_after = $3;
205920112475SJoe Perches			my $email = $4;
206020112475SJoe Perches			my $ucfirst_sign_off = ucfirst(lc($sign_off));
206120112475SJoe Perches
2062ce0338dfSJoe Perches			if ($sign_off !~ /$signature_tags/) {
2063ce0338dfSJoe Perches				WARN("BAD_SIGN_OFF",
2064ce0338dfSJoe Perches				     "Non-standard signature: $sign_off\n" . $herecurr);
2065ce0338dfSJoe Perches			}
206620112475SJoe Perches			if (defined $space_before && $space_before ne "") {
20673705ce5bSJoe Perches				if (WARN("BAD_SIGN_OFF",
20683705ce5bSJoe Perches					 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
20693705ce5bSJoe Perches				    $fix) {
2070194f66fcSJoe Perches					$fixed[$fixlinenr] =
20713705ce5bSJoe Perches					    "$ucfirst_sign_off $email";
20723705ce5bSJoe Perches				}
207320112475SJoe Perches			}
207420112475SJoe Perches			if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
20753705ce5bSJoe Perches				if (WARN("BAD_SIGN_OFF",
20763705ce5bSJoe Perches					 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
20773705ce5bSJoe Perches				    $fix) {
2078194f66fcSJoe Perches					$fixed[$fixlinenr] =
20793705ce5bSJoe Perches					    "$ucfirst_sign_off $email";
20803705ce5bSJoe Perches				}
20813705ce5bSJoe Perches
208220112475SJoe Perches			}
208320112475SJoe Perches			if (!defined $space_after || $space_after ne " ") {
20843705ce5bSJoe Perches				if (WARN("BAD_SIGN_OFF",
20853705ce5bSJoe Perches					 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
20863705ce5bSJoe Perches				    $fix) {
2087194f66fcSJoe Perches					$fixed[$fixlinenr] =
20883705ce5bSJoe Perches					    "$ucfirst_sign_off $email";
20893705ce5bSJoe Perches				}
209020112475SJoe Perches			}
209120112475SJoe Perches
209220112475SJoe Perches			my ($email_name, $email_address, $comment) = parse_email($email);
209320112475SJoe Perches			my $suggested_email = format_email(($email_name, $email_address));
209420112475SJoe Perches			if ($suggested_email eq "") {
2095000d1cc1SJoe Perches				ERROR("BAD_SIGN_OFF",
2096000d1cc1SJoe Perches				      "Unrecognized email address: '$email'\n" . $herecurr);
209720112475SJoe Perches			} else {
209820112475SJoe Perches				my $dequoted = $suggested_email;
209920112475SJoe Perches				$dequoted =~ s/^"//;
210020112475SJoe Perches				$dequoted =~ s/" </ </;
210120112475SJoe Perches				# Don't force email to have quotes
210220112475SJoe Perches				# Allow just an angle bracketed address
210320112475SJoe Perches				if ("$dequoted$comment" ne $email &&
210420112475SJoe Perches				    "<$email_address>$comment" ne $email &&
210520112475SJoe Perches				    "$suggested_email$comment" ne $email) {
2106000d1cc1SJoe Perches					WARN("BAD_SIGN_OFF",
2107000d1cc1SJoe Perches					     "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
210820112475SJoe Perches				}
21090a920b5bSAndy Whitcroft			}
21107e51f197SJoe Perches
21117e51f197SJoe Perches# Check for duplicate signatures
21127e51f197SJoe Perches			my $sig_nospace = $line;
21137e51f197SJoe Perches			$sig_nospace =~ s/\s//g;
21147e51f197SJoe Perches			$sig_nospace = lc($sig_nospace);
21157e51f197SJoe Perches			if (defined $signatures{$sig_nospace}) {
21167e51f197SJoe Perches				WARN("BAD_SIGN_OFF",
21177e51f197SJoe Perches				     "Duplicate signature\n" . $herecurr);
21187e51f197SJoe Perches			} else {
21197e51f197SJoe Perches				$signatures{$sig_nospace} = 1;
21207e51f197SJoe Perches			}
21210a920b5bSAndy Whitcroft		}
21220a920b5bSAndy Whitcroft
21239b3189ebSJoe Perches# Check for old stable address
21249b3189ebSJoe Perches		if ($line =~ /^\s*cc:\s*.*<?\bstable\@kernel\.org\b>?.*$/i) {
21259b3189ebSJoe Perches			ERROR("STABLE_ADDRESS",
21269b3189ebSJoe Perches			      "The 'stable' address should be 'stable\@vger.kernel.org'\n" . $herecurr);
21279b3189ebSJoe Perches		}
21289b3189ebSJoe Perches
21297ebd05efSChristopher Covington# Check for unwanted Gerrit info
21307ebd05efSChristopher Covington		if ($in_commit_log && $line =~ /^\s*change-id:/i) {
21317ebd05efSChristopher Covington			ERROR("GERRIT_CHANGE_ID",
21327ebd05efSChristopher Covington			      "Remove Gerrit Change-Id's before submitting upstream.\n" . $herecurr);
21337ebd05efSChristopher Covington		}
21347ebd05efSChristopher Covington
2135d311cd44SJoe Perches# Check for improperly formed commit descriptions
2136d311cd44SJoe Perches		if ($in_commit_log &&
2137d311cd44SJoe Perches		    $line =~ /\bcommit\s+[0-9a-f]{5,}/i &&
213866881735SJoe Perches		    !($line =~ /\b[Cc]ommit [0-9a-f]{12,40} \("/ ||
213966881735SJoe Perches		      ($line =~ /\b[Cc]ommit [0-9a-f]{12,40}\s*$/ &&
214066881735SJoe Perches		       defined $rawlines[$linenr] &&
214166881735SJoe Perches		       $rawlines[$linenr] =~ /^\s*\("/))) {
2142d311cd44SJoe Perches			$line =~ /\b(c)ommit\s+([0-9a-f]{5,})/i;
2143d311cd44SJoe Perches			my $init_char = $1;
2144d311cd44SJoe Perches			my $orig_commit = lc($2);
2145d311cd44SJoe Perches			my $id = '01234567890ab';
2146d311cd44SJoe Perches			my $desc = 'commit description';
2147d311cd44SJoe Perches		        ($id, $desc) = git_commit_info($orig_commit, $id, $desc);
2148d311cd44SJoe Perches			ERROR("GIT_COMMIT_ID",
21493f6316b4SJoe Perches			      "Please use 12 or more chars for the git commit ID like: '${init_char}ommit $id (\"$desc\")'\n" . $herecurr);
2150d311cd44SJoe Perches		}
2151d311cd44SJoe Perches
215213f1937eSJoe Perches# Check for added, moved or deleted files
215313f1937eSJoe Perches		if (!$reported_maintainer_file && !$in_commit_log &&
215413f1937eSJoe Perches		    ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ ||
215513f1937eSJoe Perches		     $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ ||
215613f1937eSJoe Perches		     ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ &&
215713f1937eSJoe Perches		      (defined($1) || defined($2))))) {
215813f1937eSJoe Perches			$reported_maintainer_file = 1;
215913f1937eSJoe Perches			WARN("FILE_PATH_CHANGES",
216013f1937eSJoe Perches			     "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr);
216113f1937eSJoe Perches		}
216213f1937eSJoe Perches
216300df344fSAndy Whitcroft# Check for wrappage within a valid hunk of the file
21648905a67cSAndy Whitcroft		if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
2165000d1cc1SJoe Perches			ERROR("CORRUPTED_PATCH",
2166000d1cc1SJoe Perches			      "patch seems to be corrupt (line wrapped?)\n" .
21676c72ffaaSAndy Whitcroft				$herecurr) if (!$emitted_corrupt++);
2168de7d4f0eSAndy Whitcroft		}
2169de7d4f0eSAndy Whitcroft
21706ecd9674SAndy Whitcroft# Check for absolute kernel paths.
21716ecd9674SAndy Whitcroft		if ($tree) {
21726ecd9674SAndy Whitcroft			while ($line =~ m{(?:^|\s)(/\S*)}g) {
21736ecd9674SAndy Whitcroft				my $file = $1;
21746ecd9674SAndy Whitcroft
21756ecd9674SAndy Whitcroft				if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
21766ecd9674SAndy Whitcroft				    check_absolute_file($1, $herecurr)) {
21776ecd9674SAndy Whitcroft					#
21786ecd9674SAndy Whitcroft				} else {
21796ecd9674SAndy Whitcroft					check_absolute_file($file, $herecurr);
21806ecd9674SAndy Whitcroft				}
21816ecd9674SAndy Whitcroft			}
21826ecd9674SAndy Whitcroft		}
21836ecd9674SAndy Whitcroft
2184de7d4f0eSAndy Whitcroft# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
2185de7d4f0eSAndy Whitcroft		if (($realfile =~ /^$/ || $line =~ /^\+/) &&
2186171ae1a4SAndy Whitcroft		    $rawline !~ m/^$UTF8*$/) {
2187171ae1a4SAndy Whitcroft			my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
2188171ae1a4SAndy Whitcroft
2189171ae1a4SAndy Whitcroft			my $blank = copy_spacing($rawline);
2190171ae1a4SAndy Whitcroft			my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
2191171ae1a4SAndy Whitcroft			my $hereptr = "$hereline$ptr\n";
2192171ae1a4SAndy Whitcroft
219334d99219SJoe Perches			CHK("INVALID_UTF8",
2194000d1cc1SJoe Perches			    "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
219500df344fSAndy Whitcroft		}
21960a920b5bSAndy Whitcroft
219715662b3eSJoe Perches# Check if it's the start of a commit log
219815662b3eSJoe Perches# (not a header line and we haven't seen the patch filename)
219915662b3eSJoe Perches		if ($in_header_lines && $realfile =~ /^$/ &&
220029ee1b0cSJoe Perches		    !($rawline =~ /^\s+\S/ ||
220129ee1b0cSJoe Perches		      $rawline =~ /^(commit\b|from\b|[\w-]+:).*$/i)) {
220215662b3eSJoe Perches			$in_header_lines = 0;
220315662b3eSJoe Perches			$in_commit_log = 1;
220415662b3eSJoe Perches		}
220515662b3eSJoe Perches
2206fa64205dSPasi Savanainen# Check if there is UTF-8 in a commit log when a mail header has explicitly
2207fa64205dSPasi Savanainen# declined it, i.e defined some charset where it is missing.
2208fa64205dSPasi Savanainen		if ($in_header_lines &&
2209fa64205dSPasi Savanainen		    $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
2210fa64205dSPasi Savanainen		    $1 !~ /utf-8/i) {
2211fa64205dSPasi Savanainen			$non_utf8_charset = 1;
2212fa64205dSPasi Savanainen		}
2213fa64205dSPasi Savanainen
2214fa64205dSPasi Savanainen		if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
221515662b3eSJoe Perches		    $rawline =~ /$NON_ASCII_UTF8/) {
2216fa64205dSPasi Savanainen			WARN("UTF8_BEFORE_PATCH",
221715662b3eSJoe Perches			    "8-bit UTF-8 used in possible commit log\n" . $herecurr);
221815662b3eSJoe Perches		}
221915662b3eSJoe Perches
222030670854SAndy Whitcroft# ignore non-hunk lines and lines being removed
222130670854SAndy Whitcroft		next if (!$hunk_line || $line =~ /^-/);
222200df344fSAndy Whitcroft
22230a920b5bSAndy Whitcroft#trailing whitespace
22249c0ca6f9SAndy Whitcroft		if ($line =~ /^\+.*\015/) {
2225c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2226d5e616fcSJoe Perches			if (ERROR("DOS_LINE_ENDINGS",
2227d5e616fcSJoe Perches				  "DOS line endings\n" . $herevet) &&
2228d5e616fcSJoe Perches			    $fix) {
2229194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/[\s\015]+$//;
2230d5e616fcSJoe Perches			}
2231c2fdda0dSAndy Whitcroft		} elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
2232c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
22333705ce5bSJoe Perches			if (ERROR("TRAILING_WHITESPACE",
22343705ce5bSJoe Perches				  "trailing whitespace\n" . $herevet) &&
22353705ce5bSJoe Perches			    $fix) {
2236194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\s+$//;
22373705ce5bSJoe Perches			}
22383705ce5bSJoe Perches
2239d2c0a235SAndy Whitcroft			$rpt_cleaners = 1;
22400a920b5bSAndy Whitcroft		}
22415368df20SAndy Whitcroft
22424783f894SJosh Triplett# Check for FSF mailing addresses.
2243109d8cb2SAlexander Duyck		if ($rawline =~ /\bwrite to the Free/i ||
22443e2232f2SJoe Perches		    $rawline =~ /\b59\s+Temple\s+Pl/i ||
22453e2232f2SJoe Perches		    $rawline =~ /\b51\s+Franklin\s+St/i) {
22464783f894SJosh Triplett			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
22474783f894SJosh Triplett			my $msg_type = \&ERROR;
22484783f894SJosh Triplett			$msg_type = \&CHK if ($file);
22494783f894SJosh Triplett			&{$msg_type}("FSF_MAILING_ADDRESS",
22504783f894SJosh 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)
22514783f894SJosh Triplett		}
22524783f894SJosh Triplett
22533354957aSAndi Kleen# check for Kconfig help text having a real description
22549fe287d7SAndy Whitcroft# Only applies when adding the entry originally, after that we do not have
22559fe287d7SAndy Whitcroft# sufficient context to determine whether it is indeed long enough.
22563354957aSAndi Kleen		if ($realfile =~ /Kconfig/ &&
22578d73e0e7SJoe Perches		    $line =~ /^\+\s*config\s+/) {
22583354957aSAndi Kleen			my $length = 0;
22599fe287d7SAndy Whitcroft			my $cnt = $realcnt;
22609fe287d7SAndy Whitcroft			my $ln = $linenr + 1;
22619fe287d7SAndy Whitcroft			my $f;
2262a1385803SAndy Whitcroft			my $is_start = 0;
22639fe287d7SAndy Whitcroft			my $is_end = 0;
2264a1385803SAndy Whitcroft			for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
22659fe287d7SAndy Whitcroft				$f = $lines[$ln - 1];
22669fe287d7SAndy Whitcroft				$cnt-- if ($lines[$ln - 1] !~ /^-/);
22679fe287d7SAndy Whitcroft				$is_end = $lines[$ln - 1] =~ /^\+/;
22689fe287d7SAndy Whitcroft
22699fe287d7SAndy Whitcroft				next if ($f =~ /^-/);
22708d73e0e7SJoe Perches				last if (!$file && $f =~ /^\@\@/);
2271a1385803SAndy Whitcroft
22728d73e0e7SJoe Perches				if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate)\s*\"/) {
2273a1385803SAndy Whitcroft					$is_start = 1;
22748d73e0e7SJoe Perches				} elsif ($lines[$ln - 1] =~ /^\+\s*(?:---)?help(?:---)?$/) {
2275a1385803SAndy Whitcroft					$length = -1;
2276a1385803SAndy Whitcroft				}
2277a1385803SAndy Whitcroft
22789fe287d7SAndy Whitcroft				$f =~ s/^.//;
22793354957aSAndi Kleen				$f =~ s/#.*//;
22803354957aSAndi Kleen				$f =~ s/^\s+//;
22813354957aSAndi Kleen				next if ($f =~ /^$/);
22829fe287d7SAndy Whitcroft				if ($f =~ /^\s*config\s/) {
22839fe287d7SAndy Whitcroft					$is_end = 1;
22849fe287d7SAndy Whitcroft					last;
22859fe287d7SAndy Whitcroft				}
22863354957aSAndi Kleen				$length++;
22873354957aSAndi Kleen			}
2288000d1cc1SJoe Perches			WARN("CONFIG_DESCRIPTION",
2289a1385803SAndy Whitcroft			     "please write a paragraph that describes the config symbol fully\n" . $herecurr) if ($is_start && $is_end && $length < 4);
2290a1385803SAndy Whitcroft			#print "is_start<$is_start> is_end<$is_end> length<$length>\n";
22913354957aSAndi Kleen		}
22923354957aSAndi Kleen
22931ba8dfd1SKees Cook# discourage the addition of CONFIG_EXPERIMENTAL in Kconfig.
22941ba8dfd1SKees Cook		if ($realfile =~ /Kconfig/ &&
22951ba8dfd1SKees Cook		    $line =~ /.\s*depends on\s+.*\bEXPERIMENTAL\b/) {
22961ba8dfd1SKees Cook			WARN("CONFIG_EXPERIMENTAL",
22971ba8dfd1SKees Cook			     "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
22981ba8dfd1SKees Cook		}
22991ba8dfd1SKees Cook
2300c68e5878SArnaud Lacombe		if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
2301c68e5878SArnaud Lacombe		    ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
2302c68e5878SArnaud Lacombe			my $flag = $1;
2303c68e5878SArnaud Lacombe			my $replacement = {
2304c68e5878SArnaud Lacombe				'EXTRA_AFLAGS' =>   'asflags-y',
2305c68e5878SArnaud Lacombe				'EXTRA_CFLAGS' =>   'ccflags-y',
2306c68e5878SArnaud Lacombe				'EXTRA_CPPFLAGS' => 'cppflags-y',
2307c68e5878SArnaud Lacombe				'EXTRA_LDFLAGS' =>  'ldflags-y',
2308c68e5878SArnaud Lacombe			};
2309c68e5878SArnaud Lacombe
2310c68e5878SArnaud Lacombe			WARN("DEPRECATED_VARIABLE",
2311c68e5878SArnaud Lacombe			     "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
2312c68e5878SArnaud Lacombe		}
2313c68e5878SArnaud Lacombe
2314bff5da43SRob Herring# check for DT compatible documentation
23157dd05b38SFlorian Vaussard		if (defined $root &&
23167dd05b38SFlorian Vaussard			(($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) ||
23177dd05b38SFlorian Vaussard			 ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) {
23187dd05b38SFlorian Vaussard
2319bff5da43SRob Herring			my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g;
2320bff5da43SRob Herring
2321cc93319bSFlorian Vaussard			my $dt_path = $root . "/Documentation/devicetree/bindings/";
2322cc93319bSFlorian Vaussard			my $vp_file = $dt_path . "vendor-prefixes.txt";
2323cc93319bSFlorian Vaussard
2324bff5da43SRob Herring			foreach my $compat (@compats) {
2325bff5da43SRob Herring				my $compat2 = $compat;
2326185d566bSRob Herring				$compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/;
2327185d566bSRob Herring				my $compat3 = $compat;
2328185d566bSRob Herring				$compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/;
2329185d566bSRob Herring				`grep -Erq "$compat|$compat2|$compat3" $dt_path`;
2330bff5da43SRob Herring				if ( $? >> 8 ) {
2331bff5da43SRob Herring					WARN("UNDOCUMENTED_DT_STRING",
2332bff5da43SRob Herring					     "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr);
2333bff5da43SRob Herring				}
2334bff5da43SRob Herring
23354fbf32a6SFlorian Vaussard				next if $compat !~ /^([a-zA-Z0-9\-]+)\,/;
23364fbf32a6SFlorian Vaussard				my $vendor = $1;
2337cc93319bSFlorian Vaussard				`grep -Eq "^$vendor\\b" $vp_file`;
2338bff5da43SRob Herring				if ( $? >> 8 ) {
2339bff5da43SRob Herring					WARN("UNDOCUMENTED_DT_STRING",
2340cc93319bSFlorian Vaussard					     "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr);
2341bff5da43SRob Herring				}
2342bff5da43SRob Herring			}
2343bff5da43SRob Herring		}
2344bff5da43SRob Herring
23455368df20SAndy Whitcroft# check we are in a valid source file if not then ignore this hunk
23465368df20SAndy Whitcroft		next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
23475368df20SAndy Whitcroft
23486cd7f386SJoe Perches#line length limit
2349c45dcabdSAndy Whitcroft		if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ &&
2350f4c014c0SAndy Whitcroft		    $rawline !~ /^.\s*\*\s*\@$Ident\s/ &&
23510fccc622SJoe Perches		    !($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(KERN_\S+\s*|[^"]*))?"[X\t]*"\s*(?:|,|\)\s*;)\s*$/ ||
23528bbea968SJoe Perches		    $line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) &&
23536cd7f386SJoe Perches		    $length > $max_line_length)
2354c45dcabdSAndy Whitcroft		{
2355000d1cc1SJoe Perches			WARN("LONG_LINE",
23566cd7f386SJoe Perches			     "line over $max_line_length characters\n" . $herecurr);
23570a920b5bSAndy Whitcroft		}
23580a920b5bSAndy Whitcroft
2359ca56dc09SJosh Triplett# Check for user-visible strings broken across lines, which breaks the ability
23608c5fcd24SJoe Perches# to grep for the string.  Make exceptions when the previous string ends in a
23618c5fcd24SJoe Perches# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{'
23628c5fcd24SJoe Perches# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value
2363ca56dc09SJosh Triplett		if ($line =~ /^\+\s*"/ &&
2364ca56dc09SJosh Triplett		    $prevline =~ /"\s*$/ &&
23658c5fcd24SJoe Perches		    $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) {
2366ca56dc09SJosh Triplett			WARN("SPLIT_STRING",
2367ca56dc09SJosh Triplett			     "quoted string split across lines\n" . $hereprev);
2368ca56dc09SJosh Triplett		}
2369ca56dc09SJosh Triplett
2370ece9659fSDan Carpenter# check for missing a space in a string concatination
2371ece9659fSDan Carpenter		if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) {
2372ece9659fSDan Carpenter			WARN('MISSING_SPACE',
2373ece9659fSDan Carpenter			     "break quoted strings at a space character\n" . $hereprev);
2374ece9659fSDan Carpenter		}
2375ece9659fSDan Carpenter
23765e79d96eSJoe Perches# check for spaces before a quoted newline
23775e79d96eSJoe Perches		if ($rawline =~ /^.*\".*\s\\n/) {
23783705ce5bSJoe Perches			if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
23793705ce5bSJoe Perches				 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
23803705ce5bSJoe Perches			    $fix) {
2381194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
23823705ce5bSJoe Perches			}
23833705ce5bSJoe Perches
23845e79d96eSJoe Perches		}
23855e79d96eSJoe Perches
23868905a67cSAndy Whitcroft# check for adding lines without a newline.
23878905a67cSAndy Whitcroft		if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
2388000d1cc1SJoe Perches			WARN("MISSING_EOF_NEWLINE",
2389000d1cc1SJoe Perches			     "adding a line without newline at end of file\n" . $herecurr);
23908905a67cSAndy Whitcroft		}
23918905a67cSAndy Whitcroft
239242e41c54SMike Frysinger# Blackfin: use hi/lo macros
239342e41c54SMike Frysinger		if ($realfile =~ m@arch/blackfin/.*\.S$@) {
239442e41c54SMike Frysinger			if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
239542e41c54SMike Frysinger				my $herevet = "$here\n" . cat_vet($line) . "\n";
2396000d1cc1SJoe Perches				ERROR("LO_MACRO",
2397000d1cc1SJoe Perches				      "use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
239842e41c54SMike Frysinger			}
239942e41c54SMike Frysinger			if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
240042e41c54SMike Frysinger				my $herevet = "$here\n" . cat_vet($line) . "\n";
2401000d1cc1SJoe Perches				ERROR("HI_MACRO",
2402000d1cc1SJoe Perches				      "use the HI() macro, not (... >> 16)\n" . $herevet);
240342e41c54SMike Frysinger			}
240442e41c54SMike Frysinger		}
240542e41c54SMike Frysinger
2406b9ea10d6SAndy Whitcroft# check we are in a valid source file C or perl if not then ignore this hunk
2407b9ea10d6SAndy Whitcroft		next if ($realfile !~ /\.(h|c|pl)$/);
24080a920b5bSAndy Whitcroft
24090a920b5bSAndy Whitcroft# at the beginning of a line any tabs must come first and anything
24100a920b5bSAndy Whitcroft# more than 8 must use tabs.
2411c2fdda0dSAndy Whitcroft		if ($rawline =~ /^\+\s* \t\s*\S/ ||
2412c2fdda0dSAndy Whitcroft		    $rawline =~ /^\+\s*        \s*/) {
2413c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2414d2c0a235SAndy Whitcroft			$rpt_cleaners = 1;
24153705ce5bSJoe Perches			if (ERROR("CODE_INDENT",
24163705ce5bSJoe Perches				  "code indent should use tabs where possible\n" . $herevet) &&
24173705ce5bSJoe Perches			    $fix) {
2418194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
24193705ce5bSJoe Perches			}
24200a920b5bSAndy Whitcroft		}
24210a920b5bSAndy Whitcroft
242208e44365SAlberto Panizzo# check for space before tabs.
242308e44365SAlberto Panizzo		if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
242408e44365SAlberto Panizzo			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
24253705ce5bSJoe Perches			if (WARN("SPACE_BEFORE_TAB",
24263705ce5bSJoe Perches				"please, no space before tabs\n" . $herevet) &&
24273705ce5bSJoe Perches			    $fix) {
2428194f66fcSJoe Perches				while ($fixed[$fixlinenr] =~
2429c76f4cb3SJoe Perches					   s/(^\+.*) {8,8}+\t/$1\t\t/) {}
2430194f66fcSJoe Perches				while ($fixed[$fixlinenr] =~
2431c76f4cb3SJoe Perches					   s/(^\+.*) +\t/$1\t/) {}
24323705ce5bSJoe Perches			}
243308e44365SAlberto Panizzo		}
243408e44365SAlberto Panizzo
2435d1fe9c09SJoe Perches# check for && or || at the start of a line
2436d1fe9c09SJoe Perches		if ($rawline =~ /^\+\s*(&&|\|\|)/) {
2437d1fe9c09SJoe Perches			CHK("LOGICAL_CONTINUATIONS",
2438d1fe9c09SJoe Perches			    "Logical continuations should be on the previous line\n" . $hereprev);
2439d1fe9c09SJoe Perches		}
2440d1fe9c09SJoe Perches
2441d1fe9c09SJoe Perches# check multi-line statement indentation matches previous line
2442d1fe9c09SJoe Perches		if ($^V && $^V ge 5.10.0 &&
244391cb5195SJoe Perches		    $prevline =~ /^\+([ \t]*)((?:$c90_Keywords(?:\s+if)\s*)|(?:$Declare\s*)?(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*|$Ident\s*=\s*$Ident\s*)\(.*(\&\&|\|\||,)\s*$/) {
2444d1fe9c09SJoe Perches			$prevline =~ /^\+(\t*)(.*)$/;
2445d1fe9c09SJoe Perches			my $oldindent = $1;
2446d1fe9c09SJoe Perches			my $rest = $2;
2447d1fe9c09SJoe Perches
2448d1fe9c09SJoe Perches			my $pos = pos_last_openparen($rest);
2449d1fe9c09SJoe Perches			if ($pos >= 0) {
2450b34a26f3SJoe Perches				$line =~ /^(\+| )([ \t]*)/;
2451b34a26f3SJoe Perches				my $newindent = $2;
2452d1fe9c09SJoe Perches
2453d1fe9c09SJoe Perches				my $goodtabindent = $oldindent .
2454d1fe9c09SJoe Perches					"\t" x ($pos / 8) .
2455d1fe9c09SJoe Perches					" "  x ($pos % 8);
2456d1fe9c09SJoe Perches				my $goodspaceindent = $oldindent . " "  x $pos;
2457d1fe9c09SJoe Perches
2458d1fe9c09SJoe Perches				if ($newindent ne $goodtabindent &&
2459d1fe9c09SJoe Perches				    $newindent ne $goodspaceindent) {
24603705ce5bSJoe Perches
24613705ce5bSJoe Perches					if (CHK("PARENTHESIS_ALIGNMENT",
24623705ce5bSJoe Perches						"Alignment should match open parenthesis\n" . $hereprev) &&
24633705ce5bSJoe Perches					    $fix && $line =~ /^\+/) {
2464194f66fcSJoe Perches						$fixed[$fixlinenr] =~
24653705ce5bSJoe Perches						    s/^\+[ \t]*/\+$goodtabindent/;
24663705ce5bSJoe Perches					}
2467d1fe9c09SJoe Perches				}
2468d1fe9c09SJoe Perches			}
2469d1fe9c09SJoe Perches		}
2470d1fe9c09SJoe Perches
2471308cc8d8SJoe Perches		if ($line =~ /^\+.*\(\s*$Type\s*\)[ \t]+(?!$Assignment|$Arithmetic|{)/) {
24723705ce5bSJoe Perches			if (CHK("SPACING",
2473f27c95dbSJoe Perches				"No space is necessary after a cast\n" . $herecurr) &&
24743705ce5bSJoe Perches			    $fix) {
2475194f66fcSJoe Perches				$fixed[$fixlinenr] =~
2476f27c95dbSJoe Perches				    s/(\(\s*$Type\s*\))[ \t]+/$1/;
24773705ce5bSJoe Perches			}
2478aad4f614SJoe Perches		}
2479aad4f614SJoe Perches
248005880600SJoe Perches		if ($realfile =~ m@^(drivers/net/|net/)@ &&
2481fdb4bcd6SJoe Perches		    $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
248285ad978cSJoe Perches		    $rawline =~ /^\+[ \t]*\*/ &&
248385ad978cSJoe Perches		    $realline > 2) {
248405880600SJoe Perches			WARN("NETWORKING_BLOCK_COMMENT_STYLE",
248505880600SJoe Perches			     "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
248605880600SJoe Perches		}
248705880600SJoe Perches
248805880600SJoe Perches		if ($realfile =~ m@^(drivers/net/|net/)@ &&
2489a605e32eSJoe Perches		    $prevrawline =~ /^\+[ \t]*\/\*/ &&		#starting /*
2490a605e32eSJoe Perches		    $prevrawline !~ /\*\/[ \t]*$/ &&		#no trailing */
249161135e96SJoe Perches		    $rawline =~ /^\+/ &&			#line is new
2492a605e32eSJoe Perches		    $rawline !~ /^\+[ \t]*\*/) {		#no leading *
2493a605e32eSJoe Perches			WARN("NETWORKING_BLOCK_COMMENT_STYLE",
2494a605e32eSJoe Perches			     "networking block comments start with * on subsequent lines\n" . $hereprev);
2495a605e32eSJoe Perches		}
2496a605e32eSJoe Perches
2497a605e32eSJoe Perches		if ($realfile =~ m@^(drivers/net/|net/)@ &&
2498c24f9f19SJoe Perches		    $rawline !~ m@^\+[ \t]*\*/[ \t]*$@ &&	#trailing */
2499c24f9f19SJoe Perches		    $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ &&	#inline /*...*/
2500c24f9f19SJoe Perches		    $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ &&	#trailing **/
2501c24f9f19SJoe Perches		    $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) {	#non blank */
250205880600SJoe Perches			WARN("NETWORKING_BLOCK_COMMENT_STYLE",
250305880600SJoe Perches			     "networking block comments put the trailing */ on a separate line\n" . $herecurr);
250405880600SJoe Perches		}
250505880600SJoe Perches
25067f619191SJoe Perches# check for missing blank lines after struct/union declarations
25077f619191SJoe Perches# with exceptions for various attributes and macros
25087f619191SJoe Perches		if ($prevline =~ /^[\+ ]};?\s*$/ &&
25097f619191SJoe Perches		    $line =~ /^\+/ &&
25107f619191SJoe Perches		    !($line =~ /^\+\s*$/ ||
25117f619191SJoe Perches		      $line =~ /^\+\s*EXPORT_SYMBOL/ ||
25127f619191SJoe Perches		      $line =~ /^\+\s*MODULE_/i ||
25137f619191SJoe Perches		      $line =~ /^\+\s*\#\s*(?:end|elif|else)/ ||
25147f619191SJoe Perches		      $line =~ /^\+[a-z_]*init/ ||
25157f619191SJoe Perches		      $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ ||
25167f619191SJoe Perches		      $line =~ /^\+\s*DECLARE/ ||
25177f619191SJoe Perches		      $line =~ /^\+\s*__setup/)) {
2518d752fcc8SJoe Perches			if (CHK("LINE_SPACING",
2519d752fcc8SJoe Perches				"Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) &&
2520d752fcc8SJoe Perches			    $fix) {
2521f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, "\+");
2522d752fcc8SJoe Perches			}
25237f619191SJoe Perches		}
25247f619191SJoe Perches
2525365dd4eaSJoe Perches# check for multiple consecutive blank lines
2526365dd4eaSJoe Perches		if ($prevline =~ /^[\+ ]\s*$/ &&
2527365dd4eaSJoe Perches		    $line =~ /^\+\s*$/ &&
2528365dd4eaSJoe Perches		    $last_blank_line != ($linenr - 1)) {
2529d752fcc8SJoe Perches			if (CHK("LINE_SPACING",
2530d752fcc8SJoe Perches				"Please don't use multiple blank lines\n" . $hereprev) &&
2531d752fcc8SJoe Perches			    $fix) {
2532f2d7e4d4SJoe Perches				fix_delete_line($fixlinenr, $rawline);
2533d752fcc8SJoe Perches			}
2534d752fcc8SJoe Perches
2535365dd4eaSJoe Perches			$last_blank_line = $linenr;
2536365dd4eaSJoe Perches		}
2537365dd4eaSJoe Perches
25383b617e3bSJoe Perches# check for missing blank lines after declarations
25393f7bac03SJoe Perches		if ($sline =~ /^\+\s+\S/ &&			#Not at char 1
25403f7bac03SJoe Perches			# actual declarations
25413f7bac03SJoe Perches		    ($prevline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
25425a4e1fd3SJoe Perches			# function pointer declarations
25435a4e1fd3SJoe Perches		     $prevline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
25443f7bac03SJoe Perches			# foo bar; where foo is some local typedef or #define
25453f7bac03SJoe Perches		     $prevline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
25463f7bac03SJoe Perches			# known declaration macros
25473f7bac03SJoe Perches		     $prevline =~ /^\+\s+$declaration_macros/) &&
25483f7bac03SJoe Perches			# for "else if" which can look like "$Ident $Ident"
25493f7bac03SJoe Perches		    !($prevline =~ /^\+\s+$c90_Keywords\b/ ||
25503f7bac03SJoe Perches			# other possible extensions of declaration lines
25513f7bac03SJoe Perches		      $prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ ||
25523f7bac03SJoe Perches			# not starting a section or a macro "\" extended line
25533f7bac03SJoe Perches		      $prevline =~ /(?:\{\s*|\\)$/) &&
25543f7bac03SJoe Perches			# looks like a declaration
25553f7bac03SJoe Perches		    !($sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
25565a4e1fd3SJoe Perches			# function pointer declarations
25575a4e1fd3SJoe Perches		      $sline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
25583f7bac03SJoe Perches			# foo bar; where foo is some local typedef or #define
25593f7bac03SJoe Perches		      $sline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
25603f7bac03SJoe Perches			# known declaration macros
25613f7bac03SJoe Perches		      $sline =~ /^\+\s+$declaration_macros/ ||
25623f7bac03SJoe Perches			# start of struct or union or enum
25633b617e3bSJoe Perches		      $sline =~ /^\+\s+(?:union|struct|enum|typedef)\b/ ||
25643f7bac03SJoe Perches			# start or end of block or continuation of declaration
25653f7bac03SJoe Perches		      $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ ||
25663f7bac03SJoe Perches			# bitfield continuation
25673f7bac03SJoe Perches		      $sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ ||
25683f7bac03SJoe Perches			# other possible extensions of declaration lines
25693f7bac03SJoe Perches		      $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) &&
25703f7bac03SJoe Perches			# indentation of previous and current line are the same
25713f7bac03SJoe Perches		    (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) {
2572d752fcc8SJoe Perches			if (WARN("LINE_SPACING",
2573d752fcc8SJoe Perches				 "Missing a blank line after declarations\n" . $hereprev) &&
2574d752fcc8SJoe Perches			    $fix) {
2575f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, "\+");
2576d752fcc8SJoe Perches			}
25773b617e3bSJoe Perches		}
25783b617e3bSJoe Perches
25795f7ddae6SRaffaele Recalcati# check for spaces at the beginning of a line.
25806b4c5bebSAndy Whitcroft# Exceptions:
25816b4c5bebSAndy Whitcroft#  1) within comments
25826b4c5bebSAndy Whitcroft#  2) indented preprocessor commands
25836b4c5bebSAndy Whitcroft#  3) hanging labels
25843705ce5bSJoe Perches		if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/)  {
25855f7ddae6SRaffaele Recalcati			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
25863705ce5bSJoe Perches			if (WARN("LEADING_SPACE",
25873705ce5bSJoe Perches				 "please, no spaces at the start of a line\n" . $herevet) &&
25883705ce5bSJoe Perches			    $fix) {
2589194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
25903705ce5bSJoe Perches			}
25915f7ddae6SRaffaele Recalcati		}
25925f7ddae6SRaffaele Recalcati
2593b9ea10d6SAndy Whitcroft# check we are in a valid C source file if not then ignore this hunk
2594b9ea10d6SAndy Whitcroft		next if ($realfile !~ /\.(h|c)$/);
2595b9ea10d6SAndy Whitcroft
2596032a4c0fSJoe Perches# check indentation of any line with a bare else
2597032a4c0fSJoe Perches# if the previous line is a break or return and is indented 1 tab more...
2598032a4c0fSJoe Perches		if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {
2599032a4c0fSJoe Perches			my $tabs = length($1) + 1;
2600032a4c0fSJoe Perches			if ($prevline =~ /^\+\t{$tabs,$tabs}(?:break|return)\b/) {
2601032a4c0fSJoe Perches				WARN("UNNECESSARY_ELSE",
2602032a4c0fSJoe Perches				     "else is not generally useful after a break or return\n" . $hereprev);
2603032a4c0fSJoe Perches			}
2604032a4c0fSJoe Perches		}
2605032a4c0fSJoe Perches
2606c00df19aSJoe Perches# check indentation of a line with a break;
2607c00df19aSJoe Perches# if the previous line is a goto or return and is indented the same # of tabs
2608c00df19aSJoe Perches		if ($sline =~ /^\+([\t]+)break\s*;\s*$/) {
2609c00df19aSJoe Perches			my $tabs = $1;
2610c00df19aSJoe Perches			if ($prevline =~ /^\+$tabs(?:goto|return)\b/) {
2611c00df19aSJoe Perches				WARN("UNNECESSARY_BREAK",
2612c00df19aSJoe Perches				     "break is not useful after a goto or return\n" . $hereprev);
2613c00df19aSJoe Perches			}
2614c00df19aSJoe Perches		}
2615c00df19aSJoe Perches
26161ba8dfd1SKees Cook# discourage the addition of CONFIG_EXPERIMENTAL in #if(def).
26171ba8dfd1SKees Cook		if ($line =~ /^\+\s*\#\s*if.*\bCONFIG_EXPERIMENTAL\b/) {
26181ba8dfd1SKees Cook			WARN("CONFIG_EXPERIMENTAL",
26191ba8dfd1SKees Cook			     "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
26201ba8dfd1SKees Cook		}
26211ba8dfd1SKees Cook
2622c2fdda0dSAndy Whitcroft# check for RCS/CVS revision markers
2623cf655043SAndy Whitcroft		if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
2624000d1cc1SJoe Perches			WARN("CVS_KEYWORD",
2625000d1cc1SJoe Perches			     "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
2626c2fdda0dSAndy Whitcroft		}
262722f2a2efSAndy Whitcroft
262842e41c54SMike Frysinger# Blackfin: don't use __builtin_bfin_[cs]sync
262942e41c54SMike Frysinger		if ($line =~ /__builtin_bfin_csync/) {
263042e41c54SMike Frysinger			my $herevet = "$here\n" . cat_vet($line) . "\n";
2631000d1cc1SJoe Perches			ERROR("CSYNC",
2632000d1cc1SJoe Perches			      "use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
263342e41c54SMike Frysinger		}
263442e41c54SMike Frysinger		if ($line =~ /__builtin_bfin_ssync/) {
263542e41c54SMike Frysinger			my $herevet = "$here\n" . cat_vet($line) . "\n";
2636000d1cc1SJoe Perches			ERROR("SSYNC",
2637000d1cc1SJoe Perches			      "use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
263842e41c54SMike Frysinger		}
263942e41c54SMike Frysinger
264056e77d70SJoe Perches# check for old HOTPLUG __dev<foo> section markings
264156e77d70SJoe Perches		if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
264256e77d70SJoe Perches			WARN("HOTPLUG_SECTION",
264356e77d70SJoe Perches			     "Using $1 is unnecessary\n" . $herecurr);
264456e77d70SJoe Perches		}
264556e77d70SJoe Perches
26469c0ca6f9SAndy Whitcroft# Check for potential 'bare' types
26472b474a1aSAndy Whitcroft		my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
26482b474a1aSAndy Whitcroft		    $realline_next);
26493e469cdcSAndy Whitcroft#print "LINE<$line>\n";
26503e469cdcSAndy Whitcroft		if ($linenr >= $suppress_statement &&
26511b5539b1SJoe Perches		    $realcnt && $sline =~ /.\s*\S/) {
2652170d3a22SAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
2653f5fe35ddSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0);
2654171ae1a4SAndy Whitcroft			$stat =~ s/\n./\n /g;
2655171ae1a4SAndy Whitcroft			$cond =~ s/\n./\n /g;
2656171ae1a4SAndy Whitcroft
26573e469cdcSAndy Whitcroft#print "linenr<$linenr> <$stat>\n";
26583e469cdcSAndy Whitcroft			# If this statement has no statement boundaries within
26593e469cdcSAndy Whitcroft			# it there is no point in retrying a statement scan
26603e469cdcSAndy Whitcroft			# until we hit end of it.
26613e469cdcSAndy Whitcroft			my $frag = $stat; $frag =~ s/;+\s*$//;
26623e469cdcSAndy Whitcroft			if ($frag !~ /(?:{|;)/) {
26633e469cdcSAndy Whitcroft#print "skip<$line_nr_next>\n";
26643e469cdcSAndy Whitcroft				$suppress_statement = $line_nr_next;
26653e469cdcSAndy Whitcroft			}
2666f74bd194SAndy Whitcroft
26672b474a1aSAndy Whitcroft			# Find the real next line.
26682b474a1aSAndy Whitcroft			$realline_next = $line_nr_next;
26692b474a1aSAndy Whitcroft			if (defined $realline_next &&
26702b474a1aSAndy Whitcroft			    (!defined $lines[$realline_next - 1] ||
26712b474a1aSAndy Whitcroft			     substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
26722b474a1aSAndy Whitcroft				$realline_next++;
26732b474a1aSAndy Whitcroft			}
26742b474a1aSAndy Whitcroft
2675171ae1a4SAndy Whitcroft			my $s = $stat;
2676171ae1a4SAndy Whitcroft			$s =~ s/{.*$//s;
2677cf655043SAndy Whitcroft
2678c2fdda0dSAndy Whitcroft			# Ignore goto labels.
2679171ae1a4SAndy Whitcroft			if ($s =~ /$Ident:\*$/s) {
2680c2fdda0dSAndy Whitcroft
2681c2fdda0dSAndy Whitcroft			# Ignore functions being called
2682171ae1a4SAndy Whitcroft			} elsif ($s =~ /^.\s*$Ident\s*\(/s) {
2683c2fdda0dSAndy Whitcroft
2684463f2864SAndy Whitcroft			} elsif ($s =~ /^.\s*else\b/s) {
2685463f2864SAndy Whitcroft
2686c45dcabdSAndy Whitcroft			# declarations always start with types
2687d2506586SAndy 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) {
2688c45dcabdSAndy Whitcroft				my $type = $1;
2689c45dcabdSAndy Whitcroft				$type =~ s/\s+/ /g;
2690c45dcabdSAndy Whitcroft				possible($type, "A:" . $s);
2691c45dcabdSAndy Whitcroft
26926c72ffaaSAndy Whitcroft			# definitions in global scope can only start with types
2693a6a84062SAndy Whitcroft			} elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
2694c45dcabdSAndy Whitcroft				possible($1, "B:" . $s);
2695c2fdda0dSAndy Whitcroft			}
26968905a67cSAndy Whitcroft
26976c72ffaaSAndy Whitcroft			# any (foo ... *) is a pointer cast, and foo is a type
269865863862SAndy Whitcroft			while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
2699c45dcabdSAndy Whitcroft				possible($1, "C:" . $s);
27009c0ca6f9SAndy Whitcroft			}
27018905a67cSAndy Whitcroft
27028905a67cSAndy Whitcroft			# Check for any sort of function declaration.
27038905a67cSAndy Whitcroft			# int foo(something bar, other baz);
27048905a67cSAndy Whitcroft			# void (*store_gdt)(x86_descr_ptr *);
2705171ae1a4SAndy Whitcroft			if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
27068905a67cSAndy Whitcroft				my ($name_len) = length($1);
27078905a67cSAndy Whitcroft
2708cf655043SAndy Whitcroft				my $ctx = $s;
2709773647a0SAndy Whitcroft				substr($ctx, 0, $name_len + 1, '');
27108905a67cSAndy Whitcroft				$ctx =~ s/\)[^\)]*$//;
2711cf655043SAndy Whitcroft
27128905a67cSAndy Whitcroft				for my $arg (split(/\s*,\s*/, $ctx)) {
2713c45dcabdSAndy Whitcroft					if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
27148905a67cSAndy Whitcroft
2715c45dcabdSAndy Whitcroft						possible($1, "D:" . $s);
27168905a67cSAndy Whitcroft					}
27178905a67cSAndy Whitcroft				}
27188905a67cSAndy Whitcroft			}
27198905a67cSAndy Whitcroft
27209c0ca6f9SAndy Whitcroft		}
27219c0ca6f9SAndy Whitcroft
272200df344fSAndy Whitcroft#
272300df344fSAndy Whitcroft# Checks which may be anchored in the context.
272400df344fSAndy Whitcroft#
272500df344fSAndy Whitcroft
272600df344fSAndy Whitcroft# Check for switch () and associated case and default
272700df344fSAndy Whitcroft# statements should be at the same indent.
272800df344fSAndy Whitcroft		if ($line=~/\bswitch\s*\(.*\)/) {
272900df344fSAndy Whitcroft			my $err = '';
273000df344fSAndy Whitcroft			my $sep = '';
273100df344fSAndy Whitcroft			my @ctx = ctx_block_outer($linenr, $realcnt);
273200df344fSAndy Whitcroft			shift(@ctx);
273300df344fSAndy Whitcroft			for my $ctx (@ctx) {
273400df344fSAndy Whitcroft				my ($clen, $cindent) = line_stats($ctx);
273500df344fSAndy Whitcroft				if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
273600df344fSAndy Whitcroft							$indent != $cindent) {
273700df344fSAndy Whitcroft					$err .= "$sep$ctx\n";
273800df344fSAndy Whitcroft					$sep = '';
273900df344fSAndy Whitcroft				} else {
274000df344fSAndy Whitcroft					$sep = "[...]\n";
274100df344fSAndy Whitcroft				}
274200df344fSAndy Whitcroft			}
274300df344fSAndy Whitcroft			if ($err ne '') {
2744000d1cc1SJoe Perches				ERROR("SWITCH_CASE_INDENT_LEVEL",
2745000d1cc1SJoe Perches				      "switch and case should be at the same indent\n$hereline$err");
2746de7d4f0eSAndy Whitcroft			}
2747de7d4f0eSAndy Whitcroft		}
2748de7d4f0eSAndy Whitcroft
2749de7d4f0eSAndy Whitcroft# if/while/etc brace do not go on next line, unless defining a do while loop,
2750de7d4f0eSAndy Whitcroft# or if that brace on the next line is for something else
27510fe3dc2bSJoe Perches		if ($line =~ /(.*)\b((?:if|while|for|switch|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
2752773647a0SAndy Whitcroft			my $pre_ctx = "$1$2";
2753773647a0SAndy Whitcroft
27549c0ca6f9SAndy Whitcroft			my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
27558eef05ddSJoe Perches
27568eef05ddSJoe Perches			if ($line =~ /^\+\t{6,}/) {
27578eef05ddSJoe Perches				WARN("DEEP_INDENTATION",
27588eef05ddSJoe Perches				     "Too many leading tabs - consider code refactoring\n" . $herecurr);
27598eef05ddSJoe Perches			}
27608eef05ddSJoe Perches
2761de7d4f0eSAndy Whitcroft			my $ctx_cnt = $realcnt - $#ctx - 1;
2762de7d4f0eSAndy Whitcroft			my $ctx = join("\n", @ctx);
2763de7d4f0eSAndy Whitcroft
2764548596d5SAndy Whitcroft			my $ctx_ln = $linenr;
2765548596d5SAndy Whitcroft			my $ctx_skip = $realcnt;
2766de7d4f0eSAndy Whitcroft
2767548596d5SAndy Whitcroft			while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
2768548596d5SAndy Whitcroft					defined $lines[$ctx_ln - 1] &&
2769548596d5SAndy Whitcroft					$lines[$ctx_ln - 1] =~ /^-/)) {
2770548596d5SAndy Whitcroft				##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
2771548596d5SAndy Whitcroft				$ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
2772773647a0SAndy Whitcroft				$ctx_ln++;
2773773647a0SAndy Whitcroft			}
2774548596d5SAndy Whitcroft
277553210168SAndy Whitcroft			#print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
277653210168SAndy Whitcroft			#print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
2777773647a0SAndy Whitcroft
2778773647a0SAndy Whitcroft			if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
2779000d1cc1SJoe Perches				ERROR("OPEN_BRACE",
2780000d1cc1SJoe Perches				      "that open brace { should be on the previous line\n" .
278101464f30SAndy Whitcroft					"$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
278200df344fSAndy Whitcroft			}
2783773647a0SAndy Whitcroft			if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
2784773647a0SAndy Whitcroft			    $ctx =~ /\)\s*\;\s*$/ &&
2785773647a0SAndy Whitcroft			    defined $lines[$ctx_ln - 1])
2786773647a0SAndy Whitcroft			{
27879c0ca6f9SAndy Whitcroft				my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
27889c0ca6f9SAndy Whitcroft				if ($nindent > $indent) {
2789000d1cc1SJoe Perches					WARN("TRAILING_SEMICOLON",
2790000d1cc1SJoe Perches					     "trailing semicolon indicates no statements, indent implies otherwise\n" .
279101464f30SAndy Whitcroft						"$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
27929c0ca6f9SAndy Whitcroft				}
27939c0ca6f9SAndy Whitcroft			}
279400df344fSAndy Whitcroft		}
279500df344fSAndy Whitcroft
27964d001e4dSAndy Whitcroft# Check relative indent for conditionals and blocks.
27970fe3dc2bSJoe Perches		if ($line =~ /\b(?:(?:if|while|for|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
27983e469cdcSAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
27993e469cdcSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0)
28003e469cdcSAndy Whitcroft					if (!defined $stat);
28014d001e4dSAndy Whitcroft			my ($s, $c) = ($stat, $cond);
28024d001e4dSAndy Whitcroft
28034d001e4dSAndy Whitcroft			substr($s, 0, length($c), '');
28044d001e4dSAndy Whitcroft
28054d001e4dSAndy Whitcroft			# Make sure we remove the line prefixes as we have
28064d001e4dSAndy Whitcroft			# none on the first line, and are going to readd them
28074d001e4dSAndy Whitcroft			# where necessary.
28084d001e4dSAndy Whitcroft			$s =~ s/\n./\n/gs;
28094d001e4dSAndy Whitcroft
28104d001e4dSAndy Whitcroft			# Find out how long the conditional actually is.
28116f779c18SAndy Whitcroft			my @newlines = ($c =~ /\n/gs);
28126f779c18SAndy Whitcroft			my $cond_lines = 1 + $#newlines;
28134d001e4dSAndy Whitcroft
28144d001e4dSAndy Whitcroft			# We want to check the first line inside the block
28154d001e4dSAndy Whitcroft			# starting at the end of the conditional, so remove:
28164d001e4dSAndy Whitcroft			#  1) any blank line termination
28174d001e4dSAndy Whitcroft			#  2) any opening brace { on end of the line
28184d001e4dSAndy Whitcroft			#  3) any do (...) {
28194d001e4dSAndy Whitcroft			my $continuation = 0;
28204d001e4dSAndy Whitcroft			my $check = 0;
28214d001e4dSAndy Whitcroft			$s =~ s/^.*\bdo\b//;
28224d001e4dSAndy Whitcroft			$s =~ s/^\s*{//;
28234d001e4dSAndy Whitcroft			if ($s =~ s/^\s*\\//) {
28244d001e4dSAndy Whitcroft				$continuation = 1;
28254d001e4dSAndy Whitcroft			}
28269bd49efeSAndy Whitcroft			if ($s =~ s/^\s*?\n//) {
28274d001e4dSAndy Whitcroft				$check = 1;
28284d001e4dSAndy Whitcroft				$cond_lines++;
28294d001e4dSAndy Whitcroft			}
28304d001e4dSAndy Whitcroft
28314d001e4dSAndy Whitcroft			# Also ignore a loop construct at the end of a
28324d001e4dSAndy Whitcroft			# preprocessor statement.
28334d001e4dSAndy Whitcroft			if (($prevline =~ /^.\s*#\s*define\s/ ||
28344d001e4dSAndy Whitcroft			    $prevline =~ /\\\s*$/) && $continuation == 0) {
28354d001e4dSAndy Whitcroft				$check = 0;
28364d001e4dSAndy Whitcroft			}
28374d001e4dSAndy Whitcroft
28389bd49efeSAndy Whitcroft			my $cond_ptr = -1;
2839740504c6SAndy Whitcroft			$continuation = 0;
28409bd49efeSAndy Whitcroft			while ($cond_ptr != $cond_lines) {
28419bd49efeSAndy Whitcroft				$cond_ptr = $cond_lines;
28424d001e4dSAndy Whitcroft
2843f16fa28fSAndy Whitcroft				# If we see an #else/#elif then the code
2844f16fa28fSAndy Whitcroft				# is not linear.
2845f16fa28fSAndy Whitcroft				if ($s =~ /^\s*\#\s*(?:else|elif)/) {
2846f16fa28fSAndy Whitcroft					$check = 0;
2847f16fa28fSAndy Whitcroft				}
2848f16fa28fSAndy Whitcroft
28499bd49efeSAndy Whitcroft				# Ignore:
28509bd49efeSAndy Whitcroft				#  1) blank lines, they should be at 0,
28519bd49efeSAndy Whitcroft				#  2) preprocessor lines, and
28529bd49efeSAndy Whitcroft				#  3) labels.
2853740504c6SAndy Whitcroft				if ($continuation ||
2854740504c6SAndy Whitcroft				    $s =~ /^\s*?\n/ ||
28559bd49efeSAndy Whitcroft				    $s =~ /^\s*#\s*?/ ||
28569bd49efeSAndy Whitcroft				    $s =~ /^\s*$Ident\s*:/) {
2857740504c6SAndy Whitcroft					$continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
285830dad6ebSAndy Whitcroft					if ($s =~ s/^.*?\n//) {
28599bd49efeSAndy Whitcroft						$cond_lines++;
28609bd49efeSAndy Whitcroft					}
28614d001e4dSAndy Whitcroft				}
286230dad6ebSAndy Whitcroft			}
28634d001e4dSAndy Whitcroft
28644d001e4dSAndy Whitcroft			my (undef, $sindent) = line_stats("+" . $s);
28654d001e4dSAndy Whitcroft			my $stat_real = raw_line($linenr, $cond_lines);
28664d001e4dSAndy Whitcroft
28674d001e4dSAndy Whitcroft			# Check if either of these lines are modified, else
28684d001e4dSAndy Whitcroft			# this is not this patch's fault.
28694d001e4dSAndy Whitcroft			if (!defined($stat_real) ||
28704d001e4dSAndy Whitcroft			    $stat !~ /^\+/ && $stat_real !~ /^\+/) {
28714d001e4dSAndy Whitcroft				$check = 0;
28724d001e4dSAndy Whitcroft			}
28734d001e4dSAndy Whitcroft			if (defined($stat_real) && $cond_lines > 1) {
28744d001e4dSAndy Whitcroft				$stat_real = "[...]\n$stat_real";
28754d001e4dSAndy Whitcroft			}
28764d001e4dSAndy Whitcroft
28779bd49efeSAndy 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";
28784d001e4dSAndy Whitcroft
28794d001e4dSAndy Whitcroft			if ($check && (($sindent % 8) != 0 ||
28804d001e4dSAndy Whitcroft			    ($sindent <= $indent && $s ne ''))) {
2881000d1cc1SJoe Perches				WARN("SUSPECT_CODE_INDENT",
2882000d1cc1SJoe Perches				     "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
28834d001e4dSAndy Whitcroft			}
28844d001e4dSAndy Whitcroft		}
28854d001e4dSAndy Whitcroft
28866c72ffaaSAndy Whitcroft		# Track the 'values' across context and added lines.
28876c72ffaaSAndy Whitcroft		my $opline = $line; $opline =~ s/^./ /;
28881f65f947SAndy Whitcroft		my ($curr_values, $curr_vars) =
28891f65f947SAndy Whitcroft				annotate_values($opline . "\n", $prev_values);
28906c72ffaaSAndy Whitcroft		$curr_values = $prev_values . $curr_values;
2891c2fdda0dSAndy Whitcroft		if ($dbg_values) {
2892c2fdda0dSAndy Whitcroft			my $outline = $opline; $outline =~ s/\t/ /g;
2893cf655043SAndy Whitcroft			print "$linenr > .$outline\n";
2894cf655043SAndy Whitcroft			print "$linenr > $curr_values\n";
28951f65f947SAndy Whitcroft			print "$linenr >  $curr_vars\n";
2896c2fdda0dSAndy Whitcroft		}
28976c72ffaaSAndy Whitcroft		$prev_values = substr($curr_values, -1);
28986c72ffaaSAndy Whitcroft
289900df344fSAndy Whitcroft#ignore lines not being added
29003705ce5bSJoe Perches		next if ($line =~ /^[^\+]/);
290100df344fSAndy Whitcroft
2902653d4876SAndy Whitcroft# TEST: allow direct testing of the type matcher.
29037429c690SAndy Whitcroft		if ($dbg_type) {
29047429c690SAndy Whitcroft			if ($line =~ /^.\s*$Declare\s*$/) {
2905000d1cc1SJoe Perches				ERROR("TEST_TYPE",
2906000d1cc1SJoe Perches				      "TEST: is type\n" . $herecurr);
29077429c690SAndy Whitcroft			} elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
2908000d1cc1SJoe Perches				ERROR("TEST_NOT_TYPE",
2909000d1cc1SJoe Perches				      "TEST: is not type ($1 is)\n". $herecurr);
29107429c690SAndy Whitcroft			}
2911653d4876SAndy Whitcroft			next;
2912653d4876SAndy Whitcroft		}
2913a1ef277eSAndy Whitcroft# TEST: allow direct testing of the attribute matcher.
2914a1ef277eSAndy Whitcroft		if ($dbg_attr) {
29159360b0e5SAndy Whitcroft			if ($line =~ /^.\s*$Modifier\s*$/) {
2916000d1cc1SJoe Perches				ERROR("TEST_ATTR",
2917000d1cc1SJoe Perches				      "TEST: is attr\n" . $herecurr);
29189360b0e5SAndy Whitcroft			} elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
2919000d1cc1SJoe Perches				ERROR("TEST_NOT_ATTR",
2920000d1cc1SJoe Perches				      "TEST: is not attr ($1 is)\n". $herecurr);
2921a1ef277eSAndy Whitcroft			}
2922a1ef277eSAndy Whitcroft			next;
2923a1ef277eSAndy Whitcroft		}
2924653d4876SAndy Whitcroft
2925f0a594c1SAndy Whitcroft# check for initialisation to aggregates open brace on the next line
292699423c20SAndy Whitcroft		if ($line =~ /^.\s*{/ &&
292799423c20SAndy Whitcroft		    $prevline =~ /(?:^|[^=])=\s*$/) {
2928d752fcc8SJoe Perches			if (ERROR("OPEN_BRACE",
2929d752fcc8SJoe Perches				  "that open brace { should be on the previous line\n" . $hereprev) &&
2930f2d7e4d4SJoe Perches			    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
2931f2d7e4d4SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
2932f2d7e4d4SJoe Perches				fix_delete_line($fixlinenr, $rawline);
2933d752fcc8SJoe Perches				my $fixedline = $prevrawline;
2934d752fcc8SJoe Perches				$fixedline =~ s/\s*=\s*$/ = {/;
2935f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
2936d752fcc8SJoe Perches				$fixedline = $line;
2937d752fcc8SJoe Perches				$fixedline =~ s/^(.\s*){\s*/$1/;
2938f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
2939d752fcc8SJoe Perches			}
2940f0a594c1SAndy Whitcroft		}
2941f0a594c1SAndy Whitcroft
294200df344fSAndy Whitcroft#
294300df344fSAndy Whitcroft# Checks which are anchored on the added line.
294400df344fSAndy Whitcroft#
294500df344fSAndy Whitcroft
2946653d4876SAndy Whitcroft# check for malformed paths in #include statements (uses RAW line)
2947c45dcabdSAndy Whitcroft		if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
2948653d4876SAndy Whitcroft			my $path = $1;
2949653d4876SAndy Whitcroft			if ($path =~ m{//}) {
2950000d1cc1SJoe Perches				ERROR("MALFORMED_INCLUDE",
2951495e9d84SJoe Perches				      "malformed #include filename\n" . $herecurr);
2952495e9d84SJoe Perches			}
2953495e9d84SJoe Perches			if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
2954495e9d84SJoe Perches				ERROR("UAPI_INCLUDE",
2955495e9d84SJoe Perches				      "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
2956653d4876SAndy Whitcroft			}
2957653d4876SAndy Whitcroft		}
2958653d4876SAndy Whitcroft
295900df344fSAndy Whitcroft# no C99 // comments
296000df344fSAndy Whitcroft		if ($line =~ m{//}) {
29613705ce5bSJoe Perches			if (ERROR("C99_COMMENTS",
29623705ce5bSJoe Perches				  "do not use C99 // comments\n" . $herecurr) &&
29633705ce5bSJoe Perches			    $fix) {
2964194f66fcSJoe Perches				my $line = $fixed[$fixlinenr];
29653705ce5bSJoe Perches				if ($line =~ /\/\/(.*)$/) {
29663705ce5bSJoe Perches					my $comment = trim($1);
2967194f66fcSJoe Perches					$fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@;
29683705ce5bSJoe Perches				}
29693705ce5bSJoe Perches			}
297000df344fSAndy Whitcroft		}
297100df344fSAndy Whitcroft		# Remove C99 comments.
29720a920b5bSAndy Whitcroft		$line =~ s@//.*@@;
29736c72ffaaSAndy Whitcroft		$opline =~ s@//.*@@;
29740a920b5bSAndy Whitcroft
29752b474a1aSAndy Whitcroft# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
29762b474a1aSAndy Whitcroft# the whole statement.
29772b474a1aSAndy Whitcroft#print "APW <$lines[$realline_next - 1]>\n";
29782b474a1aSAndy Whitcroft		if (defined $realline_next &&
29792b474a1aSAndy Whitcroft		    exists $lines[$realline_next - 1] &&
29802b474a1aSAndy Whitcroft		    !defined $suppress_export{$realline_next} &&
29812b474a1aSAndy Whitcroft		    ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
29822b474a1aSAndy Whitcroft		     $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
29833cbf62dfSAndy Whitcroft			# Handle definitions which produce identifiers with
29843cbf62dfSAndy Whitcroft			# a prefix:
29853cbf62dfSAndy Whitcroft			#   XXX(foo);
29863cbf62dfSAndy Whitcroft			#   EXPORT_SYMBOL(something_foo);
2987653d4876SAndy Whitcroft			my $name = $1;
298887a53877SAndy Whitcroft			if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
29893cbf62dfSAndy Whitcroft			    $name =~ /^${Ident}_$2/) {
29903cbf62dfSAndy Whitcroft#print "FOO C name<$name>\n";
29913cbf62dfSAndy Whitcroft				$suppress_export{$realline_next} = 1;
29923cbf62dfSAndy Whitcroft
29933cbf62dfSAndy Whitcroft			} elsif ($stat !~ /(?:
29942b474a1aSAndy Whitcroft				\n.}\s*$|
299548012058SAndy Whitcroft				^.DEFINE_$Ident\(\Q$name\E\)|
299648012058SAndy Whitcroft				^.DECLARE_$Ident\(\Q$name\E\)|
299748012058SAndy Whitcroft				^.LIST_HEAD\(\Q$name\E\)|
29982b474a1aSAndy Whitcroft				^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
29992b474a1aSAndy Whitcroft				\b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
300048012058SAndy Whitcroft			    )/x) {
30012b474a1aSAndy Whitcroft#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
30022b474a1aSAndy Whitcroft				$suppress_export{$realline_next} = 2;
30032b474a1aSAndy Whitcroft			} else {
30042b474a1aSAndy Whitcroft				$suppress_export{$realline_next} = 1;
30050a920b5bSAndy Whitcroft			}
30060a920b5bSAndy Whitcroft		}
30072b474a1aSAndy Whitcroft		if (!defined $suppress_export{$linenr} &&
30082b474a1aSAndy Whitcroft		    $prevline =~ /^.\s*$/ &&
30092b474a1aSAndy Whitcroft		    ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
30102b474a1aSAndy Whitcroft		     $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
30112b474a1aSAndy Whitcroft#print "FOO B <$lines[$linenr - 1]>\n";
30122b474a1aSAndy Whitcroft			$suppress_export{$linenr} = 2;
30132b474a1aSAndy Whitcroft		}
30142b474a1aSAndy Whitcroft		if (defined $suppress_export{$linenr} &&
30152b474a1aSAndy Whitcroft		    $suppress_export{$linenr} == 2) {
3016000d1cc1SJoe Perches			WARN("EXPORT_SYMBOL",
3017000d1cc1SJoe Perches			     "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
30182b474a1aSAndy Whitcroft		}
30190a920b5bSAndy Whitcroft
30205150bda4SJoe Eloff# check for global initialisers.
3021d5e616fcSJoe Perches		if ($line =~ /^\+(\s*$Type\s*$Ident\s*(?:\s+$Modifier))*\s*=\s*(0|NULL|false)\s*;/) {
3022d5e616fcSJoe Perches			if (ERROR("GLOBAL_INITIALISERS",
3023000d1cc1SJoe Perches				  "do not initialise globals to 0 or NULL\n" .
3024d5e616fcSJoe Perches				      $herecurr) &&
3025d5e616fcSJoe Perches			    $fix) {
3026194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/($Type\s*$Ident\s*(?:\s+$Modifier))*\s*=\s*(0|NULL|false)\s*;/$1;/;
3027d5e616fcSJoe Perches			}
3028f0a594c1SAndy Whitcroft		}
30290a920b5bSAndy Whitcroft# check for static initialisers.
3030d5e616fcSJoe Perches		if ($line =~ /^\+.*\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
3031d5e616fcSJoe Perches			if (ERROR("INITIALISED_STATIC",
3032000d1cc1SJoe Perches				  "do not initialise statics to 0 or NULL\n" .
3033d5e616fcSJoe Perches				      $herecurr) &&
3034d5e616fcSJoe Perches			    $fix) {
3035194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*(0|NULL|false)\s*;/$1;/;
3036d5e616fcSJoe Perches			}
30370a920b5bSAndy Whitcroft		}
30380a920b5bSAndy Whitcroft
30391813087dSJoe Perches# check for misordered declarations of char/short/int/long with signed/unsigned
30401813087dSJoe Perches		while ($sline =~ m{(\b$TypeMisordered\b)}g) {
30411813087dSJoe Perches			my $tmp = trim($1);
30421813087dSJoe Perches			WARN("MISORDERED_TYPE",
30431813087dSJoe Perches			     "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr);
30441813087dSJoe Perches		}
30451813087dSJoe Perches
3046cb710ecaSJoe Perches# check for static const char * arrays.
3047cb710ecaSJoe Perches		if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
3048000d1cc1SJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
3049000d1cc1SJoe Perches			     "static const char * array should probably be static const char * const\n" .
3050cb710ecaSJoe Perches				$herecurr);
3051cb710ecaSJoe Perches               }
3052cb710ecaSJoe Perches
3053cb710ecaSJoe Perches# check for static char foo[] = "bar" declarations.
3054cb710ecaSJoe Perches		if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
3055000d1cc1SJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
3056000d1cc1SJoe Perches			     "static char array declaration should probably be static const char\n" .
3057cb710ecaSJoe Perches				$herecurr);
3058cb710ecaSJoe Perches               }
3059cb710ecaSJoe Perches
30609b0fa60dSJoe Perches# check for non-global char *foo[] = {"bar", ...} declarations.
30619b0fa60dSJoe Perches		if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) {
30629b0fa60dSJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
30639b0fa60dSJoe Perches			     "char * array declaration might be better as static const\n" .
30649b0fa60dSJoe Perches				$herecurr);
30659b0fa60dSJoe Perches               }
30669b0fa60dSJoe Perches
3067b36190c5SJoe Perches# check for function declarations without arguments like "int foo()"
3068b36190c5SJoe Perches		if ($line =~ /(\b$Type\s+$Ident)\s*\(\s*\)/) {
3069b36190c5SJoe Perches			if (ERROR("FUNCTION_WITHOUT_ARGS",
3070b36190c5SJoe Perches				  "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) &&
3071b36190c5SJoe Perches			    $fix) {
3072194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/;
3073b36190c5SJoe Perches			}
3074b36190c5SJoe Perches		}
3075b36190c5SJoe Perches
307692e112fdSJoe Perches# check for uses of DEFINE_PCI_DEVICE_TABLE
307792e112fdSJoe Perches		if ($line =~ /\bDEFINE_PCI_DEVICE_TABLE\s*\(\s*(\w+)\s*\)\s*=/) {
307892e112fdSJoe Perches			if (WARN("DEFINE_PCI_DEVICE_TABLE",
307992e112fdSJoe Perches				 "Prefer struct pci_device_id over deprecated DEFINE_PCI_DEVICE_TABLE\n" . $herecurr) &&
308092e112fdSJoe Perches			    $fix) {
3081194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b(?:static\s+|)DEFINE_PCI_DEVICE_TABLE\s*\(\s*(\w+)\s*\)\s*=\s*/static const struct pci_device_id $1\[\] = /;
308292e112fdSJoe Perches			}
308393ed0e2dSJoe Perches		}
308493ed0e2dSJoe Perches
3085653d4876SAndy Whitcroft# check for new typedefs, only function parameters and sparse annotations
3086653d4876SAndy Whitcroft# make sense.
3087653d4876SAndy Whitcroft		if ($line =~ /\btypedef\s/ &&
30888054576dSAndy Whitcroft		    $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
3089c45dcabdSAndy Whitcroft		    $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
30908ed22cadSAndy Whitcroft		    $line !~ /\b$typeTypedefs\b/ &&
3091653d4876SAndy Whitcroft		    $line !~ /\b__bitwise(?:__|)\b/) {
3092000d1cc1SJoe Perches			WARN("NEW_TYPEDEFS",
3093000d1cc1SJoe Perches			     "do not add new typedefs\n" . $herecurr);
30940a920b5bSAndy Whitcroft		}
30950a920b5bSAndy Whitcroft
30960a920b5bSAndy Whitcroft# * goes on variable not on type
309765863862SAndy Whitcroft		# (char*[ const])
3098bfcb2cc7SAndy Whitcroft		while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
3099bfcb2cc7SAndy Whitcroft			#print "AA<$1>\n";
31003705ce5bSJoe Perches			my ($ident, $from, $to) = ($1, $2, $2);
3101d8aaf121SAndy Whitcroft
310265863862SAndy Whitcroft			# Should start with a space.
310365863862SAndy Whitcroft			$to =~ s/^(\S)/ $1/;
310465863862SAndy Whitcroft			# Should not end with a space.
310565863862SAndy Whitcroft			$to =~ s/\s+$//;
310665863862SAndy Whitcroft			# '*'s should not have spaces between.
3107f9a0b3d1SAndy Whitcroft			while ($to =~ s/\*\s+\*/\*\*/) {
310865863862SAndy Whitcroft			}
3109d8aaf121SAndy Whitcroft
31103705ce5bSJoe Perches##			print "1: from<$from> to<$to> ident<$ident>\n";
311165863862SAndy Whitcroft			if ($from ne $to) {
31123705ce5bSJoe Perches				if (ERROR("POINTER_LOCATION",
31133705ce5bSJoe Perches					  "\"(foo$from)\" should be \"(foo$to)\"\n" .  $herecurr) &&
31143705ce5bSJoe Perches				    $fix) {
31153705ce5bSJoe Perches					my $sub_from = $ident;
31163705ce5bSJoe Perches					my $sub_to = $ident;
31173705ce5bSJoe Perches					$sub_to =~ s/\Q$from\E/$to/;
3118194f66fcSJoe Perches					$fixed[$fixlinenr] =~
31193705ce5bSJoe Perches					    s@\Q$sub_from\E@$sub_to@;
31203705ce5bSJoe Perches				}
312165863862SAndy Whitcroft			}
3122bfcb2cc7SAndy Whitcroft		}
3123bfcb2cc7SAndy Whitcroft		while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
3124bfcb2cc7SAndy Whitcroft			#print "BB<$1>\n";
31253705ce5bSJoe Perches			my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
3126d8aaf121SAndy Whitcroft
312765863862SAndy Whitcroft			# Should start with a space.
312865863862SAndy Whitcroft			$to =~ s/^(\S)/ $1/;
312965863862SAndy Whitcroft			# Should not end with a space.
313065863862SAndy Whitcroft			$to =~ s/\s+$//;
313165863862SAndy Whitcroft			# '*'s should not have spaces between.
3132f9a0b3d1SAndy Whitcroft			while ($to =~ s/\*\s+\*/\*\*/) {
313365863862SAndy Whitcroft			}
313465863862SAndy Whitcroft			# Modifiers should have spaces.
313565863862SAndy Whitcroft			$to =~ s/(\b$Modifier$)/$1 /;
313665863862SAndy Whitcroft
31373705ce5bSJoe Perches##			print "2: from<$from> to<$to> ident<$ident>\n";
3138667026e7SAndy Whitcroft			if ($from ne $to && $ident !~ /^$Modifier$/) {
31393705ce5bSJoe Perches				if (ERROR("POINTER_LOCATION",
31403705ce5bSJoe Perches					  "\"foo${from}bar\" should be \"foo${to}bar\"\n" .  $herecurr) &&
31413705ce5bSJoe Perches				    $fix) {
31423705ce5bSJoe Perches
31433705ce5bSJoe Perches					my $sub_from = $match;
31443705ce5bSJoe Perches					my $sub_to = $match;
31453705ce5bSJoe Perches					$sub_to =~ s/\Q$from\E/$to/;
3146194f66fcSJoe Perches					$fixed[$fixlinenr] =~
31473705ce5bSJoe Perches					    s@\Q$sub_from\E@$sub_to@;
31483705ce5bSJoe Perches				}
314965863862SAndy Whitcroft			}
31500a920b5bSAndy Whitcroft		}
31510a920b5bSAndy Whitcroft
31520a920b5bSAndy Whitcroft# # no BUG() or BUG_ON()
31530a920b5bSAndy Whitcroft# 		if ($line =~ /\b(BUG|BUG_ON)\b/) {
31540a920b5bSAndy Whitcroft# 			print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
31550a920b5bSAndy Whitcroft# 			print "$herecurr";
31560a920b5bSAndy Whitcroft# 			$clean = 0;
31570a920b5bSAndy Whitcroft# 		}
31580a920b5bSAndy Whitcroft
31598905a67cSAndy Whitcroft		if ($line =~ /\bLINUX_VERSION_CODE\b/) {
3160000d1cc1SJoe Perches			WARN("LINUX_VERSION_CODE",
3161000d1cc1SJoe Perches			     "LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
31628905a67cSAndy Whitcroft		}
31638905a67cSAndy Whitcroft
316417441227SJoe Perches# check for uses of printk_ratelimit
316517441227SJoe Perches		if ($line =~ /\bprintk_ratelimit\s*\(/) {
3166000d1cc1SJoe Perches			WARN("PRINTK_RATELIMITED",
3167000d1cc1SJoe Perches"Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
316817441227SJoe Perches		}
316917441227SJoe Perches
317000df344fSAndy Whitcroft# printk should use KERN_* levels.  Note that follow on printk's on the
317100df344fSAndy Whitcroft# same line do not need a level, so we use the current block context
317200df344fSAndy Whitcroft# to try and find and validate the current printk.  In summary the current
317325985edcSLucas De Marchi# printk includes all preceding printk's which have no newline on the end.
317400df344fSAndy Whitcroft# we assume the first bad printk is the one to report.
3175f0a594c1SAndy Whitcroft		if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
317600df344fSAndy Whitcroft			my $ok = 0;
317700df344fSAndy Whitcroft			for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
317800df344fSAndy Whitcroft				#print "CHECK<$lines[$ln - 1]\n";
317925985edcSLucas De Marchi				# we have a preceding printk if it ends
318000df344fSAndy Whitcroft				# with "\n" ignore it, else it is to blame
318100df344fSAndy Whitcroft				if ($lines[$ln - 1] =~ m{\bprintk\(}) {
318200df344fSAndy Whitcroft					if ($rawlines[$ln - 1] !~ m{\\n"}) {
318300df344fSAndy Whitcroft						$ok = 1;
318400df344fSAndy Whitcroft					}
318500df344fSAndy Whitcroft					last;
318600df344fSAndy Whitcroft				}
318700df344fSAndy Whitcroft			}
318800df344fSAndy Whitcroft			if ($ok == 0) {
3189000d1cc1SJoe Perches				WARN("PRINTK_WITHOUT_KERN_LEVEL",
3190000d1cc1SJoe Perches				     "printk() should include KERN_ facility level\n" . $herecurr);
31910a920b5bSAndy Whitcroft			}
319200df344fSAndy Whitcroft		}
31930a920b5bSAndy Whitcroft
3194243f3803SJoe Perches		if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
3195243f3803SJoe Perches			my $orig = $1;
3196243f3803SJoe Perches			my $level = lc($orig);
3197243f3803SJoe Perches			$level = "warn" if ($level eq "warning");
31988f26b837SJoe Perches			my $level2 = $level;
31998f26b837SJoe Perches			$level2 = "dbg" if ($level eq "debug");
3200243f3803SJoe Perches			WARN("PREFER_PR_LEVEL",
3201daa8b059SYogesh Chaudhari			     "Prefer [subsystem eg: netdev]_$level2([subsystem]dev, ... then dev_$level2(dev, ... then pr_$level(...  to printk(KERN_$orig ...\n" . $herecurr);
3202243f3803SJoe Perches		}
3203243f3803SJoe Perches
3204243f3803SJoe Perches		if ($line =~ /\bpr_warning\s*\(/) {
3205d5e616fcSJoe Perches			if (WARN("PREFER_PR_LEVEL",
3206d5e616fcSJoe Perches				 "Prefer pr_warn(... to pr_warning(...\n" . $herecurr) &&
3207d5e616fcSJoe Perches			    $fix) {
3208194f66fcSJoe Perches				$fixed[$fixlinenr] =~
3209d5e616fcSJoe Perches				    s/\bpr_warning\b/pr_warn/;
3210d5e616fcSJoe Perches			}
3211243f3803SJoe Perches		}
3212243f3803SJoe Perches
3213dc139313SJoe Perches		if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
3214dc139313SJoe Perches			my $orig = $1;
3215dc139313SJoe Perches			my $level = lc($orig);
3216dc139313SJoe Perches			$level = "warn" if ($level eq "warning");
3217dc139313SJoe Perches			$level = "dbg" if ($level eq "debug");
3218dc139313SJoe Perches			WARN("PREFER_DEV_LEVEL",
3219dc139313SJoe Perches			     "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
3220dc139313SJoe Perches		}
3221dc139313SJoe Perches
3222653d4876SAndy Whitcroft# function brace can't be on same line, except for #defines of do while,
3223653d4876SAndy Whitcroft# or if closed on same line
32248d182478SJoe Perches		if (($line=~/$Type\s*$Ident\(.*\).*\s*{/) and
3225c45dcabdSAndy Whitcroft		    !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) {
32268d182478SJoe Perches			if (ERROR("OPEN_BRACE",
32278d182478SJoe Perches				  "open brace '{' following function declarations go on the next line\n" . $herecurr) &&
32288d182478SJoe Perches			    $fix) {
32298d182478SJoe Perches				fix_delete_line($fixlinenr, $rawline);
32308d182478SJoe Perches				my $fixed_line = $rawline;
32318d182478SJoe Perches				$fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*){(.*)$/;
32328d182478SJoe Perches				my $line1 = $1;
32338d182478SJoe Perches				my $line2 = $2;
32348d182478SJoe Perches				fix_insert_line($fixlinenr, ltrim($line1));
32358d182478SJoe Perches				fix_insert_line($fixlinenr, "\+{");
32368d182478SJoe Perches				if ($line2 !~ /^\s*$/) {
32378d182478SJoe Perches					fix_insert_line($fixlinenr, "\+\t" . trim($line2));
32388d182478SJoe Perches				}
32398d182478SJoe Perches			}
32400a920b5bSAndy Whitcroft		}
3241653d4876SAndy Whitcroft
32428905a67cSAndy Whitcroft# open braces for enum, union and struct go on the same line.
32438905a67cSAndy Whitcroft		if ($line =~ /^.\s*{/ &&
32448905a67cSAndy Whitcroft		    $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
32458d182478SJoe Perches			if (ERROR("OPEN_BRACE",
32468d182478SJoe Perches				  "open brace '{' following $1 go on the same line\n" . $hereprev) &&
32478d182478SJoe Perches			    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
32488d182478SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
32498d182478SJoe Perches				fix_delete_line($fixlinenr, $rawline);
32508d182478SJoe Perches				my $fixedline = rtrim($prevrawline) . " {";
32518d182478SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
32528d182478SJoe Perches				$fixedline = $rawline;
32538d182478SJoe Perches				$fixedline =~ s/^(.\s*){\s*/$1\t/;
32548d182478SJoe Perches				if ($fixedline !~ /^\+\s*$/) {
32558d182478SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
32568d182478SJoe Perches				}
32578d182478SJoe Perches			}
32588905a67cSAndy Whitcroft		}
32598905a67cSAndy Whitcroft
32600c73b4ebSAndy Whitcroft# missing space after union, struct or enum definition
32613705ce5bSJoe Perches		if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
32623705ce5bSJoe Perches			if (WARN("SPACING",
32633705ce5bSJoe Perches				 "missing space after $1 definition\n" . $herecurr) &&
32643705ce5bSJoe Perches			    $fix) {
3265194f66fcSJoe Perches				$fixed[$fixlinenr] =~
32663705ce5bSJoe Perches				    s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
32673705ce5bSJoe Perches			}
32680c73b4ebSAndy Whitcroft		}
32690c73b4ebSAndy Whitcroft
327031070b5dSJoe Perches# Function pointer declarations
327131070b5dSJoe Perches# check spacing between type, funcptr, and args
327231070b5dSJoe Perches# canonical declaration is "type (*funcptr)(args...)"
327391f72e9cSJoe Perches		if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) {
327431070b5dSJoe Perches			my $declare = $1;
327531070b5dSJoe Perches			my $pre_pointer_space = $2;
327631070b5dSJoe Perches			my $post_pointer_space = $3;
327731070b5dSJoe Perches			my $funcname = $4;
327831070b5dSJoe Perches			my $post_funcname_space = $5;
327931070b5dSJoe Perches			my $pre_args_space = $6;
328031070b5dSJoe Perches
328191f72e9cSJoe Perches# the $Declare variable will capture all spaces after the type
328291f72e9cSJoe Perches# so check it for a missing trailing missing space but pointer return types
328391f72e9cSJoe Perches# don't need a space so don't warn for those.
328491f72e9cSJoe Perches			my $post_declare_space = "";
328591f72e9cSJoe Perches			if ($declare =~ /(\s+)$/) {
328691f72e9cSJoe Perches				$post_declare_space = $1;
328791f72e9cSJoe Perches				$declare = rtrim($declare);
328891f72e9cSJoe Perches			}
328991f72e9cSJoe Perches			if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) {
329031070b5dSJoe Perches				WARN("SPACING",
329131070b5dSJoe Perches				     "missing space after return type\n" . $herecurr);
329291f72e9cSJoe Perches				$post_declare_space = " ";
329331070b5dSJoe Perches			}
329431070b5dSJoe Perches
329531070b5dSJoe Perches# unnecessary space "type  (*funcptr)(args...)"
329691f72e9cSJoe Perches# This test is not currently implemented because these declarations are
329791f72e9cSJoe Perches# equivalent to
329891f72e9cSJoe Perches#	int  foo(int bar, ...)
329991f72e9cSJoe Perches# and this is form shouldn't/doesn't generate a checkpatch warning.
330091f72e9cSJoe Perches#
330191f72e9cSJoe Perches#			elsif ($declare =~ /\s{2,}$/) {
330291f72e9cSJoe Perches#				WARN("SPACING",
330391f72e9cSJoe Perches#				     "Multiple spaces after return type\n" . $herecurr);
330491f72e9cSJoe Perches#			}
330531070b5dSJoe Perches
330631070b5dSJoe Perches# unnecessary space "type ( *funcptr)(args...)"
330731070b5dSJoe Perches			if (defined $pre_pointer_space &&
330831070b5dSJoe Perches			    $pre_pointer_space =~ /^\s/) {
330931070b5dSJoe Perches				WARN("SPACING",
331031070b5dSJoe Perches				     "Unnecessary space after function pointer open parenthesis\n" . $herecurr);
331131070b5dSJoe Perches			}
331231070b5dSJoe Perches
331331070b5dSJoe Perches# unnecessary space "type (* funcptr)(args...)"
331431070b5dSJoe Perches			if (defined $post_pointer_space &&
331531070b5dSJoe Perches			    $post_pointer_space =~ /^\s/) {
331631070b5dSJoe Perches				WARN("SPACING",
331731070b5dSJoe Perches				     "Unnecessary space before function pointer name\n" . $herecurr);
331831070b5dSJoe Perches			}
331931070b5dSJoe Perches
332031070b5dSJoe Perches# unnecessary space "type (*funcptr )(args...)"
332131070b5dSJoe Perches			if (defined $post_funcname_space &&
332231070b5dSJoe Perches			    $post_funcname_space =~ /^\s/) {
332331070b5dSJoe Perches				WARN("SPACING",
332431070b5dSJoe Perches				     "Unnecessary space after function pointer name\n" . $herecurr);
332531070b5dSJoe Perches			}
332631070b5dSJoe Perches
332731070b5dSJoe Perches# unnecessary space "type (*funcptr) (args...)"
332831070b5dSJoe Perches			if (defined $pre_args_space &&
332931070b5dSJoe Perches			    $pre_args_space =~ /^\s/) {
333031070b5dSJoe Perches				WARN("SPACING",
333131070b5dSJoe Perches				     "Unnecessary space before function pointer arguments\n" . $herecurr);
333231070b5dSJoe Perches			}
333331070b5dSJoe Perches
333431070b5dSJoe Perches			if (show_type("SPACING") && $fix) {
3335194f66fcSJoe Perches				$fixed[$fixlinenr] =~
333691f72e9cSJoe Perches				    s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex;
333731070b5dSJoe Perches			}
333831070b5dSJoe Perches		}
333931070b5dSJoe Perches
33408d31cfceSAndy Whitcroft# check for spacing round square brackets; allowed:
33418d31cfceSAndy Whitcroft#  1. with a type on the left -- int [] a;
3342fe2a7dbcSAndy Whitcroft#  2. at the beginning of a line for slice initialisers -- [0...10] = 5,
3343fe2a7dbcSAndy Whitcroft#  3. inside a curly brace -- = { [0...10] = 5 }
33448d31cfceSAndy Whitcroft		while ($line =~ /(.*?\s)\[/g) {
33458d31cfceSAndy Whitcroft			my ($where, $prefix) = ($-[1], $1);
33468d31cfceSAndy Whitcroft			if ($prefix !~ /$Type\s+$/ &&
3347fe2a7dbcSAndy Whitcroft			    ($where != 0 || $prefix !~ /^.\s+$/) &&
3348daebc534SAndy Whitcroft			    $prefix !~ /[{,]\s+$/) {
33493705ce5bSJoe Perches				if (ERROR("BRACKET_SPACE",
33503705ce5bSJoe Perches					  "space prohibited before open square bracket '['\n" . $herecurr) &&
33513705ce5bSJoe Perches				    $fix) {
3352194f66fcSJoe Perches				    $fixed[$fixlinenr] =~
33533705ce5bSJoe Perches					s/^(\+.*?)\s+\[/$1\[/;
33543705ce5bSJoe Perches				}
33558d31cfceSAndy Whitcroft			}
33568d31cfceSAndy Whitcroft		}
33578d31cfceSAndy Whitcroft
3358f0a594c1SAndy Whitcroft# check for spaces between functions and their parentheses.
33596c72ffaaSAndy Whitcroft		while ($line =~ /($Ident)\s+\(/g) {
3360c2fdda0dSAndy Whitcroft			my $name = $1;
3361773647a0SAndy Whitcroft			my $ctx_before = substr($line, 0, $-[1]);
3362773647a0SAndy Whitcroft			my $ctx = "$ctx_before$name";
3363c2fdda0dSAndy Whitcroft
3364c2fdda0dSAndy Whitcroft			# Ignore those directives where spaces _are_ permitted.
3365773647a0SAndy Whitcroft			if ($name =~ /^(?:
3366773647a0SAndy Whitcroft				if|for|while|switch|return|case|
3367773647a0SAndy Whitcroft				volatile|__volatile__|
3368773647a0SAndy Whitcroft				__attribute__|format|__extension__|
3369773647a0SAndy Whitcroft				asm|__asm__)$/x)
3370773647a0SAndy Whitcroft			{
3371c2fdda0dSAndy Whitcroft			# cpp #define statements have non-optional spaces, ie
3372c2fdda0dSAndy Whitcroft			# if there is a space between the name and the open
3373c2fdda0dSAndy Whitcroft			# parenthesis it is simply not a parameter group.
3374c45dcabdSAndy Whitcroft			} elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
3375773647a0SAndy Whitcroft
3376773647a0SAndy Whitcroft			# cpp #elif statement condition may start with a (
3377c45dcabdSAndy Whitcroft			} elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
3378c2fdda0dSAndy Whitcroft
3379c2fdda0dSAndy Whitcroft			# If this whole things ends with a type its most
3380c2fdda0dSAndy Whitcroft			# likely a typedef for a function.
3381773647a0SAndy Whitcroft			} elsif ($ctx =~ /$Type$/) {
3382c2fdda0dSAndy Whitcroft
3383c2fdda0dSAndy Whitcroft			} else {
33843705ce5bSJoe Perches				if (WARN("SPACING",
33853705ce5bSJoe Perches					 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
33863705ce5bSJoe Perches					     $fix) {
3387194f66fcSJoe Perches					$fixed[$fixlinenr] =~
33883705ce5bSJoe Perches					    s/\b$name\s+\(/$name\(/;
33893705ce5bSJoe Perches				}
3390f0a594c1SAndy Whitcroft			}
33916c72ffaaSAndy Whitcroft		}
33929a4cad4eSEric Nelson
3393653d4876SAndy Whitcroft# Check operator spacing.
33940a920b5bSAndy Whitcroft		if (!($line=~/\#\s*include/)) {
33953705ce5bSJoe Perches			my $fixed_line = "";
33963705ce5bSJoe Perches			my $line_fixed = 0;
33973705ce5bSJoe Perches
33989c0ca6f9SAndy Whitcroft			my $ops = qr{
33999c0ca6f9SAndy Whitcroft				<<=|>>=|<=|>=|==|!=|
34009c0ca6f9SAndy Whitcroft				\+=|-=|\*=|\/=|%=|\^=|\|=|&=|
34019c0ca6f9SAndy Whitcroft				=>|->|<<|>>|<|>|=|!|~|
34021f65f947SAndy Whitcroft				&&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
340384731623SJoe Perches				\?:|\?|:
34049c0ca6f9SAndy Whitcroft			}x;
3405cf655043SAndy Whitcroft			my @elements = split(/($ops|;)/, $opline);
34063705ce5bSJoe Perches
34073705ce5bSJoe Perches##			print("element count: <" . $#elements . ">\n");
34083705ce5bSJoe Perches##			foreach my $el (@elements) {
34093705ce5bSJoe Perches##				print("el: <$el>\n");
34103705ce5bSJoe Perches##			}
34113705ce5bSJoe Perches
34123705ce5bSJoe Perches			my @fix_elements = ();
341300df344fSAndy Whitcroft			my $off = 0;
34146c72ffaaSAndy Whitcroft
34153705ce5bSJoe Perches			foreach my $el (@elements) {
34163705ce5bSJoe Perches				push(@fix_elements, substr($rawline, $off, length($el)));
34173705ce5bSJoe Perches				$off += length($el);
34183705ce5bSJoe Perches			}
34193705ce5bSJoe Perches
34203705ce5bSJoe Perches			$off = 0;
34213705ce5bSJoe Perches
34226c72ffaaSAndy Whitcroft			my $blank = copy_spacing($opline);
3423b34c648bSJoe Perches			my $last_after = -1;
34246c72ffaaSAndy Whitcroft
34250a920b5bSAndy Whitcroft			for (my $n = 0; $n < $#elements; $n += 2) {
34263705ce5bSJoe Perches
34273705ce5bSJoe Perches				my $good = $fix_elements[$n] . $fix_elements[$n + 1];
34283705ce5bSJoe Perches
34293705ce5bSJoe Perches##				print("n: <$n> good: <$good>\n");
34303705ce5bSJoe Perches
34314a0df2efSAndy Whitcroft				$off += length($elements[$n]);
34324a0df2efSAndy Whitcroft
343325985edcSLucas De Marchi				# Pick up the preceding and succeeding characters.
3434773647a0SAndy Whitcroft				my $ca = substr($opline, 0, $off);
3435773647a0SAndy Whitcroft				my $cc = '';
3436773647a0SAndy Whitcroft				if (length($opline) >= ($off + length($elements[$n + 1]))) {
3437773647a0SAndy Whitcroft					$cc = substr($opline, $off + length($elements[$n + 1]));
3438773647a0SAndy Whitcroft				}
3439773647a0SAndy Whitcroft				my $cb = "$ca$;$cc";
3440773647a0SAndy Whitcroft
34414a0df2efSAndy Whitcroft				my $a = '';
34424a0df2efSAndy Whitcroft				$a = 'V' if ($elements[$n] ne '');
34434a0df2efSAndy Whitcroft				$a = 'W' if ($elements[$n] =~ /\s$/);
3444cf655043SAndy Whitcroft				$a = 'C' if ($elements[$n] =~ /$;$/);
34454a0df2efSAndy Whitcroft				$a = 'B' if ($elements[$n] =~ /(\[|\()$/);
34464a0df2efSAndy Whitcroft				$a = 'O' if ($elements[$n] eq '');
3447773647a0SAndy Whitcroft				$a = 'E' if ($ca =~ /^\s*$/);
34484a0df2efSAndy Whitcroft
34490a920b5bSAndy Whitcroft				my $op = $elements[$n + 1];
34504a0df2efSAndy Whitcroft
34514a0df2efSAndy Whitcroft				my $c = '';
34520a920b5bSAndy Whitcroft				if (defined $elements[$n + 2]) {
34534a0df2efSAndy Whitcroft					$c = 'V' if ($elements[$n + 2] ne '');
34544a0df2efSAndy Whitcroft					$c = 'W' if ($elements[$n + 2] =~ /^\s/);
3455cf655043SAndy Whitcroft					$c = 'C' if ($elements[$n + 2] =~ /^$;/);
34564a0df2efSAndy Whitcroft					$c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
34574a0df2efSAndy Whitcroft					$c = 'O' if ($elements[$n + 2] eq '');
34588b1b3378SAndy Whitcroft					$c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
34594a0df2efSAndy Whitcroft				} else {
34604a0df2efSAndy Whitcroft					$c = 'E';
34610a920b5bSAndy Whitcroft				}
34620a920b5bSAndy Whitcroft
34634a0df2efSAndy Whitcroft				my $ctx = "${a}x${c}";
34644a0df2efSAndy Whitcroft
34654a0df2efSAndy Whitcroft				my $at = "(ctx:$ctx)";
34664a0df2efSAndy Whitcroft
34676c72ffaaSAndy Whitcroft				my $ptr = substr($blank, 0, $off) . "^";
3468de7d4f0eSAndy Whitcroft				my $hereptr = "$hereline$ptr\n";
34690a920b5bSAndy Whitcroft
347074048ed8SAndy Whitcroft				# Pull out the value of this operator.
34716c72ffaaSAndy Whitcroft				my $op_type = substr($curr_values, $off + 1, 1);
34720a920b5bSAndy Whitcroft
34731f65f947SAndy Whitcroft				# Get the full operator variant.
34741f65f947SAndy Whitcroft				my $opv = $op . substr($curr_vars, $off, 1);
34751f65f947SAndy Whitcroft
347613214adfSAndy Whitcroft				# Ignore operators passed as parameters.
347713214adfSAndy Whitcroft				if ($op_type ne 'V' &&
347813214adfSAndy Whitcroft				    $ca =~ /\s$/ && $cc =~ /^\s*,/) {
347913214adfSAndy Whitcroft
3480cf655043SAndy Whitcroft#				# Ignore comments
3481cf655043SAndy Whitcroft#				} elsif ($op =~ /^$;+$/) {
348213214adfSAndy Whitcroft
3483d8aaf121SAndy Whitcroft				# ; should have either the end of line or a space or \ after it
348413214adfSAndy Whitcroft				} elsif ($op eq ';') {
3485cf655043SAndy Whitcroft					if ($ctx !~ /.x[WEBC]/ &&
3486cf655043SAndy Whitcroft					    $cc !~ /^\\/ && $cc !~ /^;/) {
34873705ce5bSJoe Perches						if (ERROR("SPACING",
34883705ce5bSJoe Perches							  "space required after that '$op' $at\n" . $hereptr)) {
3489b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
34903705ce5bSJoe Perches							$line_fixed = 1;
34913705ce5bSJoe Perches						}
3492d8aaf121SAndy Whitcroft					}
3493d8aaf121SAndy Whitcroft
3494d8aaf121SAndy Whitcroft				# // is a comment
3495d8aaf121SAndy Whitcroft				} elsif ($op eq '//') {
34960a920b5bSAndy Whitcroft
3497b00e4814SJoe Perches				#   :   when part of a bitfield
3498b00e4814SJoe Perches				} elsif ($opv eq ':B') {
3499b00e4814SJoe Perches					# skip the bitfield test for now
3500b00e4814SJoe Perches
35011f65f947SAndy Whitcroft				# No spaces for:
35021f65f947SAndy Whitcroft				#   ->
3503b00e4814SJoe Perches				} elsif ($op eq '->') {
35044a0df2efSAndy Whitcroft					if ($ctx =~ /Wx.|.xW/) {
35053705ce5bSJoe Perches						if (ERROR("SPACING",
35063705ce5bSJoe Perches							  "spaces prohibited around that '$op' $at\n" . $hereptr)) {
3507b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
35083705ce5bSJoe Perches							if (defined $fix_elements[$n + 2]) {
35093705ce5bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
35103705ce5bSJoe Perches							}
3511b34c648bSJoe Perches							$line_fixed = 1;
35123705ce5bSJoe Perches						}
35130a920b5bSAndy Whitcroft					}
35140a920b5bSAndy Whitcroft
35150a920b5bSAndy Whitcroft				# , must have a space on the right.
35160a920b5bSAndy Whitcroft				} elsif ($op eq ',') {
3517cf655043SAndy Whitcroft					if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
35183705ce5bSJoe Perches						if (ERROR("SPACING",
35193705ce5bSJoe Perches							  "space required after that '$op' $at\n" . $hereptr)) {
3520b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
35213705ce5bSJoe Perches							$line_fixed = 1;
3522b34c648bSJoe Perches							$last_after = $n;
35233705ce5bSJoe Perches						}
35240a920b5bSAndy Whitcroft					}
35250a920b5bSAndy Whitcroft
35269c0ca6f9SAndy Whitcroft				# '*' as part of a type definition -- reported already.
352774048ed8SAndy Whitcroft				} elsif ($opv eq '*_') {
35289c0ca6f9SAndy Whitcroft					#warn "'*' is part of type\n";
35299c0ca6f9SAndy Whitcroft
35309c0ca6f9SAndy Whitcroft				# unary operators should have a space before and
35319c0ca6f9SAndy Whitcroft				# none after.  May be left adjacent to another
35329c0ca6f9SAndy Whitcroft				# unary operator, or a cast
35339c0ca6f9SAndy Whitcroft				} elsif ($op eq '!' || $op eq '~' ||
353474048ed8SAndy Whitcroft					 $opv eq '*U' || $opv eq '-U' ||
35350d413866SAndy Whitcroft					 $opv eq '&U' || $opv eq '&&U') {
3536cf655043SAndy Whitcroft					if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
35373705ce5bSJoe Perches						if (ERROR("SPACING",
35383705ce5bSJoe Perches							  "space required before that '$op' $at\n" . $hereptr)) {
3539b34c648bSJoe Perches							if ($n != $last_after + 2) {
3540b34c648bSJoe Perches								$good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]);
35413705ce5bSJoe Perches								$line_fixed = 1;
35423705ce5bSJoe Perches							}
35430a920b5bSAndy Whitcroft						}
3544b34c648bSJoe Perches					}
3545a3340b35SAndy Whitcroft					if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
3546171ae1a4SAndy Whitcroft						# A unary '*' may be const
3547171ae1a4SAndy Whitcroft
3548171ae1a4SAndy Whitcroft					} elsif ($ctx =~ /.xW/) {
35493705ce5bSJoe Perches						if (ERROR("SPACING",
35503705ce5bSJoe Perches							  "space prohibited after that '$op' $at\n" . $hereptr)) {
3551b34c648bSJoe Perches							$good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]);
35523705ce5bSJoe Perches							if (defined $fix_elements[$n + 2]) {
35533705ce5bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
35543705ce5bSJoe Perches							}
3555b34c648bSJoe Perches							$line_fixed = 1;
35563705ce5bSJoe Perches						}
35570a920b5bSAndy Whitcroft					}
35580a920b5bSAndy Whitcroft
35590a920b5bSAndy Whitcroft				# unary ++ and unary -- are allowed no space on one side.
35600a920b5bSAndy Whitcroft				} elsif ($op eq '++' or $op eq '--') {
3561773647a0SAndy Whitcroft					if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
35623705ce5bSJoe Perches						if (ERROR("SPACING",
35633705ce5bSJoe Perches							  "space required one side of that '$op' $at\n" . $hereptr)) {
3564b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
35653705ce5bSJoe Perches							$line_fixed = 1;
35663705ce5bSJoe Perches						}
35670a920b5bSAndy Whitcroft					}
3568773647a0SAndy Whitcroft					if ($ctx =~ /Wx[BE]/ ||
3569773647a0SAndy Whitcroft					    ($ctx =~ /Wx./ && $cc =~ /^;/)) {
35703705ce5bSJoe Perches						if (ERROR("SPACING",
35713705ce5bSJoe Perches							  "space prohibited before that '$op' $at\n" . $hereptr)) {
3572b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
35733705ce5bSJoe Perches							$line_fixed = 1;
35743705ce5bSJoe Perches						}
3575653d4876SAndy Whitcroft					}
3576773647a0SAndy Whitcroft					if ($ctx =~ /ExW/) {
35773705ce5bSJoe Perches						if (ERROR("SPACING",
35783705ce5bSJoe Perches							  "space prohibited after that '$op' $at\n" . $hereptr)) {
3579b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
35803705ce5bSJoe Perches							if (defined $fix_elements[$n + 2]) {
35813705ce5bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
3582773647a0SAndy Whitcroft							}
3583b34c648bSJoe Perches							$line_fixed = 1;
35843705ce5bSJoe Perches						}
35853705ce5bSJoe Perches					}
35860a920b5bSAndy Whitcroft
35870a920b5bSAndy Whitcroft				# << and >> may either have or not have spaces both sides
35889c0ca6f9SAndy Whitcroft				} elsif ($op eq '<<' or $op eq '>>' or
35899c0ca6f9SAndy Whitcroft					 $op eq '&' or $op eq '^' or $op eq '|' or
35909c0ca6f9SAndy Whitcroft					 $op eq '+' or $op eq '-' or
3591c2fdda0dSAndy Whitcroft					 $op eq '*' or $op eq '/' or
3592c2fdda0dSAndy Whitcroft					 $op eq '%')
35930a920b5bSAndy Whitcroft				{
3594773647a0SAndy Whitcroft					if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
35953705ce5bSJoe Perches						if (ERROR("SPACING",
35963705ce5bSJoe Perches							  "need consistent spacing around '$op' $at\n" . $hereptr)) {
3597b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
3598b34c648bSJoe Perches							if (defined $fix_elements[$n + 2]) {
3599b34c648bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
3600b34c648bSJoe Perches							}
36013705ce5bSJoe Perches							$line_fixed = 1;
36023705ce5bSJoe Perches						}
36030a920b5bSAndy Whitcroft					}
36040a920b5bSAndy Whitcroft
36051f65f947SAndy Whitcroft				# A colon needs no spaces before when it is
36061f65f947SAndy Whitcroft				# terminating a case value or a label.
36071f65f947SAndy Whitcroft				} elsif ($opv eq ':C' || $opv eq ':L') {
36081f65f947SAndy Whitcroft					if ($ctx =~ /Wx./) {
36093705ce5bSJoe Perches						if (ERROR("SPACING",
36103705ce5bSJoe Perches							  "space prohibited before that '$op' $at\n" . $hereptr)) {
3611b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
36123705ce5bSJoe Perches							$line_fixed = 1;
36133705ce5bSJoe Perches						}
36141f65f947SAndy Whitcroft					}
36151f65f947SAndy Whitcroft
36160a920b5bSAndy Whitcroft				# All the others need spaces both sides.
3617cf655043SAndy Whitcroft				} elsif ($ctx !~ /[EWC]x[CWE]/) {
36181f65f947SAndy Whitcroft					my $ok = 0;
36191f65f947SAndy Whitcroft
362022f2a2efSAndy Whitcroft					# Ignore email addresses <foo@bar>
36211f65f947SAndy Whitcroft					if (($op eq '<' &&
36221f65f947SAndy Whitcroft					     $cc =~ /^\S+\@\S+>/) ||
36231f65f947SAndy Whitcroft					    ($op eq '>' &&
36241f65f947SAndy Whitcroft					     $ca =~ /<\S+\@\S+$/))
36251f65f947SAndy Whitcroft					{
36261f65f947SAndy Whitcroft					    	$ok = 1;
36271f65f947SAndy Whitcroft					}
36281f65f947SAndy Whitcroft
362984731623SJoe Perches					# messages are ERROR, but ?: are CHK
36301f65f947SAndy Whitcroft					if ($ok == 0) {
363184731623SJoe Perches						my $msg_type = \&ERROR;
363284731623SJoe Perches						$msg_type = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
363384731623SJoe Perches
363484731623SJoe Perches						if (&{$msg_type}("SPACING",
36353705ce5bSJoe Perches								 "spaces required around that '$op' $at\n" . $hereptr)) {
3636b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
3637b34c648bSJoe Perches							if (defined $fix_elements[$n + 2]) {
3638b34c648bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
3639b34c648bSJoe Perches							}
36403705ce5bSJoe Perches							$line_fixed = 1;
36413705ce5bSJoe Perches						}
36420a920b5bSAndy Whitcroft					}
364322f2a2efSAndy Whitcroft				}
36444a0df2efSAndy Whitcroft				$off += length($elements[$n + 1]);
36453705ce5bSJoe Perches
36463705ce5bSJoe Perches##				print("n: <$n> GOOD: <$good>\n");
36473705ce5bSJoe Perches
36483705ce5bSJoe Perches				$fixed_line = $fixed_line . $good;
36490a920b5bSAndy Whitcroft			}
36503705ce5bSJoe Perches
36513705ce5bSJoe Perches			if (($#elements % 2) == 0) {
36523705ce5bSJoe Perches				$fixed_line = $fixed_line . $fix_elements[$#elements];
36533705ce5bSJoe Perches			}
36543705ce5bSJoe Perches
3655194f66fcSJoe Perches			if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) {
3656194f66fcSJoe Perches				$fixed[$fixlinenr] = $fixed_line;
36573705ce5bSJoe Perches			}
36583705ce5bSJoe Perches
36593705ce5bSJoe Perches
36600a920b5bSAndy Whitcroft		}
36610a920b5bSAndy Whitcroft
3662786b6326SJoe Perches# check for whitespace before a non-naked semicolon
3663d2e248e7SJoe Perches		if ($line =~ /^\+.*\S\s+;\s*$/) {
3664786b6326SJoe Perches			if (WARN("SPACING",
3665786b6326SJoe Perches				 "space prohibited before semicolon\n" . $herecurr) &&
3666786b6326SJoe Perches			    $fix) {
3667194f66fcSJoe Perches				1 while $fixed[$fixlinenr] =~
3668786b6326SJoe Perches				    s/^(\+.*\S)\s+;/$1;/;
3669786b6326SJoe Perches			}
3670786b6326SJoe Perches		}
3671786b6326SJoe Perches
3672f0a594c1SAndy Whitcroft# check for multiple assignments
3673f0a594c1SAndy Whitcroft		if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
3674000d1cc1SJoe Perches			CHK("MULTIPLE_ASSIGNMENTS",
3675000d1cc1SJoe Perches			    "multiple assignments should be avoided\n" . $herecurr);
3676f0a594c1SAndy Whitcroft		}
3677f0a594c1SAndy Whitcroft
367822f2a2efSAndy Whitcroft## # check for multiple declarations, allowing for a function declaration
367922f2a2efSAndy Whitcroft## # continuation.
368022f2a2efSAndy Whitcroft## 		if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
368122f2a2efSAndy Whitcroft## 		    $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
368222f2a2efSAndy Whitcroft##
368322f2a2efSAndy Whitcroft## 			# Remove any bracketed sections to ensure we do not
368422f2a2efSAndy Whitcroft## 			# falsly report the parameters of functions.
368522f2a2efSAndy Whitcroft## 			my $ln = $line;
368622f2a2efSAndy Whitcroft## 			while ($ln =~ s/\([^\(\)]*\)//g) {
368722f2a2efSAndy Whitcroft## 			}
368822f2a2efSAndy Whitcroft## 			if ($ln =~ /,/) {
3689000d1cc1SJoe Perches## 				WARN("MULTIPLE_DECLARATION",
3690000d1cc1SJoe Perches##				     "declaring multiple variables together should be avoided\n" . $herecurr);
369122f2a2efSAndy Whitcroft## 			}
369222f2a2efSAndy Whitcroft## 		}
3693f0a594c1SAndy Whitcroft
36940a920b5bSAndy Whitcroft#need space before brace following if, while, etc
369522f2a2efSAndy Whitcroft		if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
369622f2a2efSAndy Whitcroft		    $line =~ /do{/) {
36973705ce5bSJoe Perches			if (ERROR("SPACING",
36983705ce5bSJoe Perches				  "space required before the open brace '{'\n" . $herecurr) &&
36993705ce5bSJoe Perches			    $fix) {
3700194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/^(\+.*(?:do|\))){/$1 {/;
37013705ce5bSJoe Perches			}
3702de7d4f0eSAndy Whitcroft		}
3703de7d4f0eSAndy Whitcroft
3704c4a62ef9SJoe Perches## # check for blank lines before declarations
3705c4a62ef9SJoe Perches##		if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
3706c4a62ef9SJoe Perches##		    $prevrawline =~ /^.\s*$/) {
3707c4a62ef9SJoe Perches##			WARN("SPACING",
3708c4a62ef9SJoe Perches##			     "No blank lines before declarations\n" . $hereprev);
3709c4a62ef9SJoe Perches##		}
3710c4a62ef9SJoe Perches##
3711c4a62ef9SJoe Perches
3712de7d4f0eSAndy Whitcroft# closing brace should have a space following it when it has anything
3713de7d4f0eSAndy Whitcroft# on the line
3714de7d4f0eSAndy Whitcroft		if ($line =~ /}(?!(?:,|;|\)))\S/) {
3715d5e616fcSJoe Perches			if (ERROR("SPACING",
3716d5e616fcSJoe Perches				  "space required after that close brace '}'\n" . $herecurr) &&
3717d5e616fcSJoe Perches			    $fix) {
3718194f66fcSJoe Perches				$fixed[$fixlinenr] =~
3719d5e616fcSJoe Perches				    s/}((?!(?:,|;|\)))\S)/} $1/;
3720d5e616fcSJoe Perches			}
37210a920b5bSAndy Whitcroft		}
37220a920b5bSAndy Whitcroft
372322f2a2efSAndy Whitcroft# check spacing on square brackets
372422f2a2efSAndy Whitcroft		if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
37253705ce5bSJoe Perches			if (ERROR("SPACING",
37263705ce5bSJoe Perches				  "space prohibited after that open square bracket '['\n" . $herecurr) &&
37273705ce5bSJoe Perches			    $fix) {
3728194f66fcSJoe Perches				$fixed[$fixlinenr] =~
37293705ce5bSJoe Perches				    s/\[\s+/\[/;
37303705ce5bSJoe Perches			}
373122f2a2efSAndy Whitcroft		}
373222f2a2efSAndy Whitcroft		if ($line =~ /\s\]/) {
37333705ce5bSJoe Perches			if (ERROR("SPACING",
37343705ce5bSJoe Perches				  "space prohibited before that close square bracket ']'\n" . $herecurr) &&
37353705ce5bSJoe Perches			    $fix) {
3736194f66fcSJoe Perches				$fixed[$fixlinenr] =~
37373705ce5bSJoe Perches				    s/\s+\]/\]/;
37383705ce5bSJoe Perches			}
373922f2a2efSAndy Whitcroft		}
374022f2a2efSAndy Whitcroft
3741c45dcabdSAndy Whitcroft# check spacing on parentheses
37429c0ca6f9SAndy Whitcroft		if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
37439c0ca6f9SAndy Whitcroft		    $line !~ /for\s*\(\s+;/) {
37443705ce5bSJoe Perches			if (ERROR("SPACING",
37453705ce5bSJoe Perches				  "space prohibited after that open parenthesis '('\n" . $herecurr) &&
37463705ce5bSJoe Perches			    $fix) {
3747194f66fcSJoe Perches				$fixed[$fixlinenr] =~
37483705ce5bSJoe Perches				    s/\(\s+/\(/;
37493705ce5bSJoe Perches			}
375022f2a2efSAndy Whitcroft		}
375113214adfSAndy Whitcroft		if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
3752c45dcabdSAndy Whitcroft		    $line !~ /for\s*\(.*;\s+\)/ &&
3753c45dcabdSAndy Whitcroft		    $line !~ /:\s+\)/) {
37543705ce5bSJoe Perches			if (ERROR("SPACING",
37553705ce5bSJoe Perches				  "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
37563705ce5bSJoe Perches			    $fix) {
3757194f66fcSJoe Perches				$fixed[$fixlinenr] =~
37583705ce5bSJoe Perches				    s/\s+\)/\)/;
37593705ce5bSJoe Perches			}
376022f2a2efSAndy Whitcroft		}
376122f2a2efSAndy Whitcroft
3762e2826fd0SJoe Perches# check unnecessary parentheses around addressof/dereference single $Lvals
3763e2826fd0SJoe Perches# ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar
3764e2826fd0SJoe Perches
3765e2826fd0SJoe Perches		while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) {
3766e2826fd0SJoe Perches			CHK("UNNECESSARY_PARENTHESES",
3767e2826fd0SJoe Perches			    "Unnecessary parentheses around $1\n" . $herecurr);
3768e2826fd0SJoe Perches		    }
3769e2826fd0SJoe Perches
37700a920b5bSAndy Whitcroft#goto labels aren't indented, allow a single space however
37714a0df2efSAndy Whitcroft		if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
37720a920b5bSAndy Whitcroft		   !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
37733705ce5bSJoe Perches			if (WARN("INDENTED_LABEL",
37743705ce5bSJoe Perches				 "labels should not be indented\n" . $herecurr) &&
37753705ce5bSJoe Perches			    $fix) {
3776194f66fcSJoe Perches				$fixed[$fixlinenr] =~
37773705ce5bSJoe Perches				    s/^(.)\s+/$1/;
37783705ce5bSJoe Perches			}
37790a920b5bSAndy Whitcroft		}
37800a920b5bSAndy Whitcroft
37815b9553abSJoe Perches# return is not a function
3782507e5141SJoe Perches		if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {
3783c45dcabdSAndy Whitcroft			my $spacing = $1;
3784507e5141SJoe Perches			if ($^V && $^V ge 5.10.0 &&
37855b9553abSJoe Perches			    $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) {
37865b9553abSJoe Perches				my $value = $1;
37875b9553abSJoe Perches				$value = deparenthesize($value);
37885b9553abSJoe Perches				if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) {
3789000d1cc1SJoe Perches					ERROR("RETURN_PARENTHESES",
3790000d1cc1SJoe Perches					      "return is not a function, parentheses are not required\n" . $herecurr);
37915b9553abSJoe Perches				}
3792c45dcabdSAndy Whitcroft			} elsif ($spacing !~ /\s+/) {
3793000d1cc1SJoe Perches				ERROR("SPACING",
3794000d1cc1SJoe Perches				      "space required before the open parenthesis '('\n" . $herecurr);
3795c45dcabdSAndy Whitcroft			}
3796c45dcabdSAndy Whitcroft		}
3797507e5141SJoe Perches
3798b43ae21bSJoe Perches# unnecessary return in a void function
3799b43ae21bSJoe Perches# at end-of-function, with the previous line a single leading tab, then return;
3800b43ae21bSJoe Perches# and the line before that not a goto label target like "out:"
3801b43ae21bSJoe Perches		if ($sline =~ /^[ \+]}\s*$/ &&
3802b43ae21bSJoe Perches		    $prevline =~ /^\+\treturn\s*;\s*$/ &&
3803b43ae21bSJoe Perches		    $linenr >= 3 &&
3804b43ae21bSJoe Perches		    $lines[$linenr - 3] =~ /^[ +]/ &&
3805b43ae21bSJoe Perches		    $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) {
38069819cf25SJoe Perches			WARN("RETURN_VOID",
3807b43ae21bSJoe Perches			     "void function return statements are not generally useful\n" . $hereprev);
38089819cf25SJoe Perches               }
38099819cf25SJoe Perches
3810189248d8SJoe Perches# if statements using unnecessary parentheses - ie: if ((foo == bar))
3811189248d8SJoe Perches		if ($^V && $^V ge 5.10.0 &&
3812189248d8SJoe Perches		    $line =~ /\bif\s*((?:\(\s*){2,})/) {
3813189248d8SJoe Perches			my $openparens = $1;
3814189248d8SJoe Perches			my $count = $openparens =~ tr@\(@\(@;
3815189248d8SJoe Perches			my $msg = "";
3816189248d8SJoe Perches			if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) {
3817189248d8SJoe Perches				my $comp = $4;	#Not $1 because of $LvalOrFunc
3818189248d8SJoe Perches				$msg = " - maybe == should be = ?" if ($comp eq "==");
3819189248d8SJoe Perches				WARN("UNNECESSARY_PARENTHESES",
3820189248d8SJoe Perches				     "Unnecessary parentheses$msg\n" . $herecurr);
3821189248d8SJoe Perches			}
3822189248d8SJoe Perches		}
3823189248d8SJoe Perches
382453a3c448SAndy Whitcroft# Return of what appears to be an errno should normally be -'ve
382553a3c448SAndy Whitcroft		if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) {
382653a3c448SAndy Whitcroft			my $name = $1;
382753a3c448SAndy Whitcroft			if ($name ne 'EOF' && $name ne 'ERROR') {
3828000d1cc1SJoe Perches				WARN("USE_NEGATIVE_ERRNO",
3829000d1cc1SJoe Perches				     "return of an errno should typically be -ve (return -$1)\n" . $herecurr);
383053a3c448SAndy Whitcroft			}
383153a3c448SAndy Whitcroft		}
3832c45dcabdSAndy Whitcroft
38330a920b5bSAndy Whitcroft# Need a space before open parenthesis after if, while etc
38344a0df2efSAndy Whitcroft		if ($line =~ /\b(if|while|for|switch)\(/) {
38353705ce5bSJoe Perches			if (ERROR("SPACING",
38363705ce5bSJoe Perches				  "space required before the open parenthesis '('\n" . $herecurr) &&
38373705ce5bSJoe Perches			    $fix) {
3838194f66fcSJoe Perches				$fixed[$fixlinenr] =~
38393705ce5bSJoe Perches				    s/\b(if|while|for|switch)\(/$1 \(/;
38403705ce5bSJoe Perches			}
38410a920b5bSAndy Whitcroft		}
38420a920b5bSAndy Whitcroft
3843f5fe35ddSAndy Whitcroft# Check for illegal assignment in if conditional -- and check for trailing
3844f5fe35ddSAndy Whitcroft# statements after the conditional.
3845170d3a22SAndy Whitcroft		if ($line =~ /do\s*(?!{)/) {
38463e469cdcSAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
38473e469cdcSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0)
38483e469cdcSAndy Whitcroft					if (!defined $stat);
3849170d3a22SAndy Whitcroft			my ($stat_next) = ctx_statement_block($line_nr_next,
3850170d3a22SAndy Whitcroft						$remain_next, $off_next);
3851170d3a22SAndy Whitcroft			$stat_next =~ s/\n./\n /g;
3852170d3a22SAndy Whitcroft			##print "stat<$stat> stat_next<$stat_next>\n";
3853170d3a22SAndy Whitcroft
3854170d3a22SAndy Whitcroft			if ($stat_next =~ /^\s*while\b/) {
3855170d3a22SAndy Whitcroft				# If the statement carries leading newlines,
3856170d3a22SAndy Whitcroft				# then count those as offsets.
3857170d3a22SAndy Whitcroft				my ($whitespace) =
3858170d3a22SAndy Whitcroft					($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
3859170d3a22SAndy Whitcroft				my $offset =
3860170d3a22SAndy Whitcroft					statement_rawlines($whitespace) - 1;
3861170d3a22SAndy Whitcroft
3862170d3a22SAndy Whitcroft				$suppress_whiletrailers{$line_nr_next +
3863170d3a22SAndy Whitcroft								$offset} = 1;
3864170d3a22SAndy Whitcroft			}
3865170d3a22SAndy Whitcroft		}
3866170d3a22SAndy Whitcroft		if (!defined $suppress_whiletrailers{$linenr} &&
3867c11230f4SJoe Perches		    defined($stat) && defined($cond) &&
3868170d3a22SAndy Whitcroft		    $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
3869171ae1a4SAndy Whitcroft			my ($s, $c) = ($stat, $cond);
38708905a67cSAndy Whitcroft
3871b53c8e10SAndy Whitcroft			if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
3872000d1cc1SJoe Perches				ERROR("ASSIGN_IN_IF",
3873000d1cc1SJoe Perches				      "do not use assignment in if condition\n" . $herecurr);
38748905a67cSAndy Whitcroft			}
38758905a67cSAndy Whitcroft
38768905a67cSAndy Whitcroft			# Find out what is on the end of the line after the
38778905a67cSAndy Whitcroft			# conditional.
3878773647a0SAndy Whitcroft			substr($s, 0, length($c), '');
38798905a67cSAndy Whitcroft			$s =~ s/\n.*//g;
388013214adfSAndy Whitcroft			$s =~ s/$;//g; 	# Remove any comments
388153210168SAndy Whitcroft			if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
388253210168SAndy Whitcroft			    $c !~ /}\s*while\s*/)
3883773647a0SAndy Whitcroft			{
3884bb44ad39SAndy Whitcroft				# Find out how long the conditional actually is.
3885bb44ad39SAndy Whitcroft				my @newlines = ($c =~ /\n/gs);
3886bb44ad39SAndy Whitcroft				my $cond_lines = 1 + $#newlines;
388742bdf74cSHidetoshi Seto				my $stat_real = '';
3888bb44ad39SAndy Whitcroft
388942bdf74cSHidetoshi Seto				$stat_real = raw_line($linenr, $cond_lines)
389042bdf74cSHidetoshi Seto							. "\n" if ($cond_lines);
3891bb44ad39SAndy Whitcroft				if (defined($stat_real) && $cond_lines > 1) {
3892bb44ad39SAndy Whitcroft					$stat_real = "[...]\n$stat_real";
3893bb44ad39SAndy Whitcroft				}
3894bb44ad39SAndy Whitcroft
3895000d1cc1SJoe Perches				ERROR("TRAILING_STATEMENTS",
3896000d1cc1SJoe Perches				      "trailing statements should be on next line\n" . $herecurr . $stat_real);
38978905a67cSAndy Whitcroft			}
38988905a67cSAndy Whitcroft		}
38998905a67cSAndy Whitcroft
390013214adfSAndy Whitcroft# Check for bitwise tests written as boolean
390113214adfSAndy Whitcroft		if ($line =~ /
390213214adfSAndy Whitcroft			(?:
390313214adfSAndy Whitcroft				(?:\[|\(|\&\&|\|\|)
390413214adfSAndy Whitcroft				\s*0[xX][0-9]+\s*
390513214adfSAndy Whitcroft				(?:\&\&|\|\|)
390613214adfSAndy Whitcroft			|
390713214adfSAndy Whitcroft				(?:\&\&|\|\|)
390813214adfSAndy Whitcroft				\s*0[xX][0-9]+\s*
390913214adfSAndy Whitcroft				(?:\&\&|\|\||\)|\])
391013214adfSAndy Whitcroft			)/x)
391113214adfSAndy Whitcroft		{
3912000d1cc1SJoe Perches			WARN("HEXADECIMAL_BOOLEAN_TEST",
3913000d1cc1SJoe Perches			     "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
391413214adfSAndy Whitcroft		}
391513214adfSAndy Whitcroft
39168905a67cSAndy Whitcroft# if and else should not have general statements after it
391713214adfSAndy Whitcroft		if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
391813214adfSAndy Whitcroft			my $s = $1;
391913214adfSAndy Whitcroft			$s =~ s/$;//g; 	# Remove any comments
392013214adfSAndy Whitcroft			if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
3921000d1cc1SJoe Perches				ERROR("TRAILING_STATEMENTS",
3922000d1cc1SJoe Perches				      "trailing statements should be on next line\n" . $herecurr);
39230a920b5bSAndy Whitcroft			}
392413214adfSAndy Whitcroft		}
392539667782SAndy Whitcroft# if should not continue a brace
392639667782SAndy Whitcroft		if ($line =~ /}\s*if\b/) {
3927000d1cc1SJoe Perches			ERROR("TRAILING_STATEMENTS",
3928048b123fSRasmus Villemoes			      "trailing statements should be on next line (or did you mean 'else if'?)\n" .
392939667782SAndy Whitcroft				$herecurr);
393039667782SAndy Whitcroft		}
3931a1080bf8SAndy Whitcroft# case and default should not have general statements after them
3932a1080bf8SAndy Whitcroft		if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
3933a1080bf8SAndy Whitcroft		    $line !~ /\G(?:
39343fef12d6SAndy Whitcroft			(?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
3935a1080bf8SAndy Whitcroft			\s*return\s+
3936a1080bf8SAndy Whitcroft		    )/xg)
3937a1080bf8SAndy Whitcroft		{
3938000d1cc1SJoe Perches			ERROR("TRAILING_STATEMENTS",
3939000d1cc1SJoe Perches			      "trailing statements should be on next line\n" . $herecurr);
3940a1080bf8SAndy Whitcroft		}
39410a920b5bSAndy Whitcroft
39420a920b5bSAndy Whitcroft		# Check for }<nl>else {, these must be at the same
39430a920b5bSAndy Whitcroft		# indent level to be relevant to each other.
39448b8856f4SJoe Perches		if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ &&
39450a920b5bSAndy Whitcroft		    $previndent == $indent) {
39468b8856f4SJoe Perches			if (ERROR("ELSE_AFTER_BRACE",
39478b8856f4SJoe Perches				  "else should follow close brace '}'\n" . $hereprev) &&
39488b8856f4SJoe Perches			    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
39498b8856f4SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
39508b8856f4SJoe Perches				fix_delete_line($fixlinenr, $rawline);
39518b8856f4SJoe Perches				my $fixedline = $prevrawline;
39528b8856f4SJoe Perches				$fixedline =~ s/}\s*$//;
39538b8856f4SJoe Perches				if ($fixedline !~ /^\+\s*$/) {
39548b8856f4SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
39558b8856f4SJoe Perches				}
39568b8856f4SJoe Perches				$fixedline = $rawline;
39578b8856f4SJoe Perches				$fixedline =~ s/^(.\s*)else/$1} else/;
39588b8856f4SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
39598b8856f4SJoe Perches			}
39600a920b5bSAndy Whitcroft		}
39610a920b5bSAndy Whitcroft
39628b8856f4SJoe Perches		if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ &&
3963c2fdda0dSAndy Whitcroft		    $previndent == $indent) {
3964c2fdda0dSAndy Whitcroft			my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
3965c2fdda0dSAndy Whitcroft
3966c2fdda0dSAndy Whitcroft			# Find out what is on the end of the line after the
3967c2fdda0dSAndy Whitcroft			# conditional.
3968773647a0SAndy Whitcroft			substr($s, 0, length($c), '');
3969c2fdda0dSAndy Whitcroft			$s =~ s/\n.*//g;
3970c2fdda0dSAndy Whitcroft
3971c2fdda0dSAndy Whitcroft			if ($s =~ /^\s*;/) {
39728b8856f4SJoe Perches				if (ERROR("WHILE_AFTER_BRACE",
39738b8856f4SJoe Perches					  "while should follow close brace '}'\n" . $hereprev) &&
39748b8856f4SJoe Perches				    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
39758b8856f4SJoe Perches					fix_delete_line($fixlinenr - 1, $prevrawline);
39768b8856f4SJoe Perches					fix_delete_line($fixlinenr, $rawline);
39778b8856f4SJoe Perches					my $fixedline = $prevrawline;
39788b8856f4SJoe Perches					my $trailing = $rawline;
39798b8856f4SJoe Perches					$trailing =~ s/^\+//;
39808b8856f4SJoe Perches					$trailing = trim($trailing);
39818b8856f4SJoe Perches					$fixedline =~ s/}\s*$/} $trailing/;
39828b8856f4SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
39838b8856f4SJoe Perches				}
3984c2fdda0dSAndy Whitcroft			}
3985c2fdda0dSAndy Whitcroft		}
3986c2fdda0dSAndy Whitcroft
398795e2c602SJoe Perches#Specific variable tests
3988323c1260SJoe Perches		while ($line =~ m{($Constant|$Lval)}g) {
3989323c1260SJoe Perches			my $var = $1;
399095e2c602SJoe Perches
399195e2c602SJoe Perches#gcc binary extension
399295e2c602SJoe Perches			if ($var =~ /^$Binary$/) {
3993d5e616fcSJoe Perches				if (WARN("GCC_BINARY_CONSTANT",
3994d5e616fcSJoe Perches					 "Avoid gcc v4.3+ binary constant extension: <$var>\n" . $herecurr) &&
3995d5e616fcSJoe Perches				    $fix) {
3996d5e616fcSJoe Perches					my $hexval = sprintf("0x%x", oct($var));
3997194f66fcSJoe Perches					$fixed[$fixlinenr] =~
3998d5e616fcSJoe Perches					    s/\b$var\b/$hexval/;
3999d5e616fcSJoe Perches				}
400095e2c602SJoe Perches			}
400195e2c602SJoe Perches
400295e2c602SJoe Perches#CamelCase
4003807bd26cSJoe Perches			if ($var !~ /^$Constant$/ &&
4004be79794bSJoe Perches			    $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
400522735ce8SJoe Perches#Ignore Page<foo> variants
4006807bd26cSJoe Perches			    $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
400722735ce8SJoe Perches#Ignore SI style variants like nS, mV and dB (ie: max_uV, regulator_min_uA_show)
40083445686aSJoe Perches			    $var !~ /^(?:[a-z_]*?)_?[a-z][A-Z](?:_[a-z_]+)?$/) {
40097e781f67SJoe Perches				while ($var =~ m{($Ident)}g) {
40107e781f67SJoe Perches					my $word = $1;
40117e781f67SJoe Perches					next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
4012d8b07710SJoe Perches					if ($check) {
4013d8b07710SJoe Perches						seed_camelcase_includes();
4014d8b07710SJoe Perches						if (!$file && !$camelcase_file_seeded) {
4015d8b07710SJoe Perches							seed_camelcase_file($realfile);
4016d8b07710SJoe Perches							$camelcase_file_seeded = 1;
4017d8b07710SJoe Perches						}
4018d8b07710SJoe Perches					}
40197e781f67SJoe Perches					if (!defined $camelcase{$word}) {
40207e781f67SJoe Perches						$camelcase{$word} = 1;
4021be79794bSJoe Perches						CHK("CAMELCASE",
40227e781f67SJoe Perches						    "Avoid CamelCase: <$word>\n" . $herecurr);
40237e781f67SJoe Perches					}
4024323c1260SJoe Perches				}
4025323c1260SJoe Perches			}
40263445686aSJoe Perches		}
40270a920b5bSAndy Whitcroft
40280a920b5bSAndy Whitcroft#no spaces allowed after \ in define
4029d5e616fcSJoe Perches		if ($line =~ /\#\s*define.*\\\s+$/) {
4030d5e616fcSJoe Perches			if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
4031d5e616fcSJoe Perches				 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
4032d5e616fcSJoe Perches			    $fix) {
4033194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\s+$//;
4034d5e616fcSJoe Perches			}
40350a920b5bSAndy Whitcroft		}
40360a920b5bSAndy Whitcroft
4037653d4876SAndy Whitcroft#warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
4038c45dcabdSAndy Whitcroft		if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
4039e09dec48SAndy Whitcroft			my $file = "$1.h";
4040e09dec48SAndy Whitcroft			my $checkfile = "include/linux/$file";
4041e09dec48SAndy Whitcroft			if (-f "$root/$checkfile" &&
4042e09dec48SAndy Whitcroft			    $realfile ne $checkfile &&
40437840a94cSWolfram Sang			    $1 !~ /$allowed_asm_includes/)
4044c45dcabdSAndy Whitcroft			{
4045e09dec48SAndy Whitcroft				if ($realfile =~ m{^arch/}) {
4046000d1cc1SJoe Perches					CHK("ARCH_INCLUDE_LINUX",
4047000d1cc1SJoe Perches					    "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
4048e09dec48SAndy Whitcroft				} else {
4049000d1cc1SJoe Perches					WARN("INCLUDE_LINUX",
4050000d1cc1SJoe Perches					     "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
4051e09dec48SAndy Whitcroft				}
40520a920b5bSAndy Whitcroft			}
40530a920b5bSAndy Whitcroft		}
40540a920b5bSAndy Whitcroft
4055653d4876SAndy Whitcroft# multi-statement macros should be enclosed in a do while loop, grab the
4056653d4876SAndy Whitcroft# first statement and ensure its the whole macro if its not enclosed
4057cf655043SAndy Whitcroft# in a known good container
4058b8f96a31SAndy Whitcroft		if ($realfile !~ m@/vmlinux.lds.h$@ &&
4059b8f96a31SAndy Whitcroft		    $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
4060d8aaf121SAndy Whitcroft			my $ln = $linenr;
4061d8aaf121SAndy Whitcroft			my $cnt = $realcnt;
4062c45dcabdSAndy Whitcroft			my ($off, $dstat, $dcond, $rest);
4063c45dcabdSAndy Whitcroft			my $ctx = '';
4064c45dcabdSAndy Whitcroft			($dstat, $dcond, $ln, $cnt, $off) =
4065f74bd194SAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0);
4066f74bd194SAndy Whitcroft			$ctx = $dstat;
4067c45dcabdSAndy Whitcroft			#print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
4068a3bb97a7SAndy Whitcroft			#print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
4069c45dcabdSAndy Whitcroft
4070f74bd194SAndy Whitcroft			$dstat =~ s/^.\s*\#\s*define\s+$Ident(?:\([^\)]*\))?\s*//;
4071292f1a9bSAndy Whitcroft			$dstat =~ s/$;//g;
4072c45dcabdSAndy Whitcroft			$dstat =~ s/\\\n.//g;
4073c45dcabdSAndy Whitcroft			$dstat =~ s/^\s*//s;
4074c45dcabdSAndy Whitcroft			$dstat =~ s/\s*$//s;
4075c45dcabdSAndy Whitcroft
4076c45dcabdSAndy Whitcroft			# Flatten any parentheses and braces
4077bf30d6edSAndy Whitcroft			while ($dstat =~ s/\([^\(\)]*\)/1/ ||
4078bf30d6edSAndy Whitcroft			       $dstat =~ s/\{[^\{\}]*\}/1/ ||
4079c81769fdSAndy Whitcroft			       $dstat =~ s/\[[^\[\]]*\]/1/)
4080bf30d6edSAndy Whitcroft			{
4081c45dcabdSAndy Whitcroft			}
4082c45dcabdSAndy Whitcroft
4083e45bab8eSAndy Whitcroft			# Flatten any obvious string concatentation.
4084e45bab8eSAndy Whitcroft			while ($dstat =~ s/("X*")\s*$Ident/$1/ ||
4085e45bab8eSAndy Whitcroft			       $dstat =~ s/$Ident\s*("X*")/$1/)
4086e45bab8eSAndy Whitcroft			{
4087e45bab8eSAndy Whitcroft			}
4088e45bab8eSAndy Whitcroft
4089c45dcabdSAndy Whitcroft			my $exceptions = qr{
4090c45dcabdSAndy Whitcroft				$Declare|
4091c45dcabdSAndy Whitcroft				module_param_named|
4092a0a0a7a9SKees Cook				MODULE_PARM_DESC|
4093c45dcabdSAndy Whitcroft				DECLARE_PER_CPU|
4094c45dcabdSAndy Whitcroft				DEFINE_PER_CPU|
4095383099fdSAndy Whitcroft				__typeof__\(|
409622fd2d3eSStefani Seibold				union|
409722fd2d3eSStefani Seibold				struct|
4098ea71a0a0SAndy Whitcroft				\.$Ident\s*=\s*|
4099ea71a0a0SAndy Whitcroft				^\"|\"$
4100c45dcabdSAndy Whitcroft			}x;
41015eaa20b9SAndy Whitcroft			#print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
4102f74bd194SAndy Whitcroft			if ($dstat ne '' &&
4103f74bd194SAndy Whitcroft			    $dstat !~ /^(?:$Ident|-?$Constant),$/ &&			# 10, // foo(),
4104f74bd194SAndy Whitcroft			    $dstat !~ /^(?:$Ident|-?$Constant);$/ &&			# foo();
41053cc4b1c3SJoe Perches			    $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ &&		# 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
4106356fd398SJoe Perches			    $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ &&			# character constants
4107f74bd194SAndy Whitcroft			    $dstat !~ /$exceptions/ &&
4108f74bd194SAndy Whitcroft			    $dstat !~ /^\.$Ident\s*=/ &&				# .foo =
4109e942e2c3SJoe Perches			    $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ &&		# stringification #foo
411072f115f9SAndy Whitcroft			    $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ &&	# do {...} while (...); // do {...} while (...)
4111f74bd194SAndy Whitcroft			    $dstat !~ /^for\s*$Constant$/ &&				# for (...)
4112f74bd194SAndy Whitcroft			    $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ &&	# for (...) bar()
4113f74bd194SAndy Whitcroft			    $dstat !~ /^do\s*{/ &&					# do {...
4114f95a7e6aSJoe Perches			    $dstat !~ /^\({/ &&						# ({...
4115f95a7e6aSJoe Perches			    $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
4116c45dcabdSAndy Whitcroft			{
4117f74bd194SAndy Whitcroft				$ctx =~ s/\n*$//;
4118f74bd194SAndy Whitcroft				my $herectx = $here . "\n";
4119f74bd194SAndy Whitcroft				my $cnt = statement_rawlines($ctx);
4120f74bd194SAndy Whitcroft
4121f74bd194SAndy Whitcroft				for (my $n = 0; $n < $cnt; $n++) {
4122f74bd194SAndy Whitcroft					$herectx .= raw_line($linenr, $n) . "\n";
4123c45dcabdSAndy Whitcroft				}
4124c45dcabdSAndy Whitcroft
4125f74bd194SAndy Whitcroft				if ($dstat =~ /;/) {
4126f74bd194SAndy Whitcroft					ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
4127f74bd194SAndy Whitcroft					      "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
4128f74bd194SAndy Whitcroft				} else {
4129000d1cc1SJoe Perches					ERROR("COMPLEX_MACRO",
4130388982b5SAndrew Morton					      "Macros with complex values should be enclosed in parentheses\n" . "$herectx");
4131d8aaf121SAndy Whitcroft				}
41320a920b5bSAndy Whitcroft			}
41335023d347SJoe Perches
4134481eb486SJoe Perches# check for line continuations outside of #defines, preprocessor #, and asm
41355023d347SJoe Perches
41365023d347SJoe Perches		} else {
41375023d347SJoe Perches			if ($prevline !~ /^..*\\$/ &&
4138481eb486SJoe Perches			    $line !~ /^\+\s*\#.*\\$/ &&		# preprocessor
4139481eb486SJoe Perches			    $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ &&	# asm
41405023d347SJoe Perches			    $line =~ /^\+.*\\$/) {
41415023d347SJoe Perches				WARN("LINE_CONTINUATIONS",
41425023d347SJoe Perches				     "Avoid unnecessary line continuations\n" . $herecurr);
41435023d347SJoe Perches			}
4144653d4876SAndy Whitcroft		}
41450a920b5bSAndy Whitcroft
4146b13edf7fSJoe Perches# do {} while (0) macro tests:
4147b13edf7fSJoe Perches# single-statement macros do not need to be enclosed in do while (0) loop,
4148b13edf7fSJoe Perches# macro should not end with a semicolon
4149b13edf7fSJoe Perches		if ($^V && $^V ge 5.10.0 &&
4150b13edf7fSJoe Perches		    $realfile !~ m@/vmlinux.lds.h$@ &&
4151b13edf7fSJoe Perches		    $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
4152b13edf7fSJoe Perches			my $ln = $linenr;
4153b13edf7fSJoe Perches			my $cnt = $realcnt;
4154b13edf7fSJoe Perches			my ($off, $dstat, $dcond, $rest);
4155b13edf7fSJoe Perches			my $ctx = '';
4156b13edf7fSJoe Perches			($dstat, $dcond, $ln, $cnt, $off) =
4157b13edf7fSJoe Perches				ctx_statement_block($linenr, $realcnt, 0);
4158b13edf7fSJoe Perches			$ctx = $dstat;
4159b13edf7fSJoe Perches
4160b13edf7fSJoe Perches			$dstat =~ s/\\\n.//g;
4161b13edf7fSJoe Perches
4162b13edf7fSJoe Perches			if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
4163b13edf7fSJoe Perches				my $stmts = $2;
4164b13edf7fSJoe Perches				my $semis = $3;
4165b13edf7fSJoe Perches
4166b13edf7fSJoe Perches				$ctx =~ s/\n*$//;
4167b13edf7fSJoe Perches				my $cnt = statement_rawlines($ctx);
4168b13edf7fSJoe Perches				my $herectx = $here . "\n";
4169b13edf7fSJoe Perches
4170b13edf7fSJoe Perches				for (my $n = 0; $n < $cnt; $n++) {
4171b13edf7fSJoe Perches					$herectx .= raw_line($linenr, $n) . "\n";
4172b13edf7fSJoe Perches				}
4173b13edf7fSJoe Perches
4174ac8e97f8SJoe Perches				if (($stmts =~ tr/;/;/) == 1 &&
4175ac8e97f8SJoe Perches				    $stmts !~ /^\s*(if|while|for|switch)\b/) {
4176b13edf7fSJoe Perches					WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
4177b13edf7fSJoe Perches					     "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
4178b13edf7fSJoe Perches				}
4179b13edf7fSJoe Perches				if (defined $semis && $semis ne "") {
4180b13edf7fSJoe Perches					WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
4181b13edf7fSJoe Perches					     "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
4182b13edf7fSJoe Perches				}
4183f5ef95b1SJoe Perches			} elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) {
4184f5ef95b1SJoe Perches				$ctx =~ s/\n*$//;
4185f5ef95b1SJoe Perches				my $cnt = statement_rawlines($ctx);
4186f5ef95b1SJoe Perches				my $herectx = $here . "\n";
4187f5ef95b1SJoe Perches
4188f5ef95b1SJoe Perches				for (my $n = 0; $n < $cnt; $n++) {
4189f5ef95b1SJoe Perches					$herectx .= raw_line($linenr, $n) . "\n";
4190f5ef95b1SJoe Perches				}
4191f5ef95b1SJoe Perches
4192f5ef95b1SJoe Perches				WARN("TRAILING_SEMICOLON",
4193f5ef95b1SJoe Perches				     "macros should not use a trailing semicolon\n" . "$herectx");
4194b13edf7fSJoe Perches			}
4195b13edf7fSJoe Perches		}
4196b13edf7fSJoe Perches
4197080ba929SMike Frysinger# make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
4198080ba929SMike Frysinger# all assignments may have only one of the following with an assignment:
4199080ba929SMike Frysinger#	.
4200080ba929SMike Frysinger#	ALIGN(...)
4201080ba929SMike Frysinger#	VMLINUX_SYMBOL(...)
4202080ba929SMike Frysinger		if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
4203000d1cc1SJoe Perches			WARN("MISSING_VMLINUX_SYMBOL",
4204000d1cc1SJoe Perches			     "vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
4205080ba929SMike Frysinger		}
4206080ba929SMike Frysinger
4207f0a594c1SAndy Whitcroft# check for redundant bracing round if etc
420813214adfSAndy Whitcroft		if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
420913214adfSAndy Whitcroft			my ($level, $endln, @chunks) =
4210cf655043SAndy Whitcroft				ctx_statement_full($linenr, $realcnt, 1);
421113214adfSAndy Whitcroft			#print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
4212cf655043SAndy Whitcroft			#print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
4213cf655043SAndy Whitcroft			if ($#chunks > 0 && $level == 0) {
4214aad4f614SJoe Perches				my @allowed = ();
4215aad4f614SJoe Perches				my $allow = 0;
421613214adfSAndy Whitcroft				my $seen = 0;
4217773647a0SAndy Whitcroft				my $herectx = $here . "\n";
4218cf655043SAndy Whitcroft				my $ln = $linenr - 1;
421913214adfSAndy Whitcroft				for my $chunk (@chunks) {
422013214adfSAndy Whitcroft					my ($cond, $block) = @{$chunk};
422113214adfSAndy Whitcroft
4222773647a0SAndy Whitcroft					# If the condition carries leading newlines, then count those as offsets.
4223773647a0SAndy Whitcroft					my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
4224773647a0SAndy Whitcroft					my $offset = statement_rawlines($whitespace) - 1;
4225773647a0SAndy Whitcroft
4226aad4f614SJoe Perches					$allowed[$allow] = 0;
4227773647a0SAndy Whitcroft					#print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
4228773647a0SAndy Whitcroft
4229773647a0SAndy Whitcroft					# We have looked at and allowed this specific line.
4230773647a0SAndy Whitcroft					$suppress_ifbraces{$ln + $offset} = 1;
4231773647a0SAndy Whitcroft
4232773647a0SAndy Whitcroft					$herectx .= "$rawlines[$ln + $offset]\n[...]\n";
4233cf655043SAndy Whitcroft					$ln += statement_rawlines($block) - 1;
4234cf655043SAndy Whitcroft
4235773647a0SAndy Whitcroft					substr($block, 0, length($cond), '');
423613214adfSAndy Whitcroft
423713214adfSAndy Whitcroft					$seen++ if ($block =~ /^\s*{/);
423813214adfSAndy Whitcroft
4239aad4f614SJoe Perches					#print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
4240cf655043SAndy Whitcroft					if (statement_lines($cond) > 1) {
4241cf655043SAndy Whitcroft						#print "APW: ALLOWED: cond<$cond>\n";
4242aad4f614SJoe Perches						$allowed[$allow] = 1;
424313214adfSAndy Whitcroft					}
424413214adfSAndy Whitcroft					if ($block =~/\b(?:if|for|while)\b/) {
4245cf655043SAndy Whitcroft						#print "APW: ALLOWED: block<$block>\n";
4246aad4f614SJoe Perches						$allowed[$allow] = 1;
424713214adfSAndy Whitcroft					}
4248cf655043SAndy Whitcroft					if (statement_block_size($block) > 1) {
4249cf655043SAndy Whitcroft						#print "APW: ALLOWED: lines block<$block>\n";
4250aad4f614SJoe Perches						$allowed[$allow] = 1;
425113214adfSAndy Whitcroft					}
4252aad4f614SJoe Perches					$allow++;
425313214adfSAndy Whitcroft				}
4254aad4f614SJoe Perches				if ($seen) {
4255aad4f614SJoe Perches					my $sum_allowed = 0;
4256aad4f614SJoe Perches					foreach (@allowed) {
4257aad4f614SJoe Perches						$sum_allowed += $_;
4258aad4f614SJoe Perches					}
4259aad4f614SJoe Perches					if ($sum_allowed == 0) {
4260000d1cc1SJoe Perches						WARN("BRACES",
4261000d1cc1SJoe Perches						     "braces {} are not necessary for any arm of this statement\n" . $herectx);
4262aad4f614SJoe Perches					} elsif ($sum_allowed != $allow &&
4263aad4f614SJoe Perches						 $seen != $allow) {
4264aad4f614SJoe Perches						CHK("BRACES",
4265aad4f614SJoe Perches						    "braces {} should be used on all arms of this statement\n" . $herectx);
4266aad4f614SJoe Perches					}
426713214adfSAndy Whitcroft				}
426813214adfSAndy Whitcroft			}
426913214adfSAndy Whitcroft		}
4270773647a0SAndy Whitcroft		if (!defined $suppress_ifbraces{$linenr - 1} &&
427113214adfSAndy Whitcroft					$line =~ /\b(if|while|for|else)\b/) {
4272cf655043SAndy Whitcroft			my $allowed = 0;
4273f0a594c1SAndy Whitcroft
4274cf655043SAndy Whitcroft			# Check the pre-context.
4275cf655043SAndy Whitcroft			if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
4276cf655043SAndy Whitcroft				#print "APW: ALLOWED: pre<$1>\n";
4277cf655043SAndy Whitcroft				$allowed = 1;
4278f0a594c1SAndy Whitcroft			}
4279773647a0SAndy Whitcroft
4280773647a0SAndy Whitcroft			my ($level, $endln, @chunks) =
4281773647a0SAndy Whitcroft				ctx_statement_full($linenr, $realcnt, $-[0]);
4282773647a0SAndy Whitcroft
4283cf655043SAndy Whitcroft			# Check the condition.
4284cf655043SAndy Whitcroft			my ($cond, $block) = @{$chunks[0]};
4285773647a0SAndy Whitcroft			#print "CHECKING<$linenr> cond<$cond> block<$block>\n";
4286cf655043SAndy Whitcroft			if (defined $cond) {
4287773647a0SAndy Whitcroft				substr($block, 0, length($cond), '');
4288cf655043SAndy Whitcroft			}
4289cf655043SAndy Whitcroft			if (statement_lines($cond) > 1) {
4290cf655043SAndy Whitcroft				#print "APW: ALLOWED: cond<$cond>\n";
4291cf655043SAndy Whitcroft				$allowed = 1;
4292cf655043SAndy Whitcroft			}
4293cf655043SAndy Whitcroft			if ($block =~/\b(?:if|for|while)\b/) {
4294cf655043SAndy Whitcroft				#print "APW: ALLOWED: block<$block>\n";
4295cf655043SAndy Whitcroft				$allowed = 1;
4296cf655043SAndy Whitcroft			}
4297cf655043SAndy Whitcroft			if (statement_block_size($block) > 1) {
4298cf655043SAndy Whitcroft				#print "APW: ALLOWED: lines block<$block>\n";
4299cf655043SAndy Whitcroft				$allowed = 1;
4300cf655043SAndy Whitcroft			}
4301cf655043SAndy Whitcroft			# Check the post-context.
4302cf655043SAndy Whitcroft			if (defined $chunks[1]) {
4303cf655043SAndy Whitcroft				my ($cond, $block) = @{$chunks[1]};
4304cf655043SAndy Whitcroft				if (defined $cond) {
4305773647a0SAndy Whitcroft					substr($block, 0, length($cond), '');
4306cf655043SAndy Whitcroft				}
4307cf655043SAndy Whitcroft				if ($block =~ /^\s*\{/) {
4308cf655043SAndy Whitcroft					#print "APW: ALLOWED: chunk-1 block<$block>\n";
4309cf655043SAndy Whitcroft					$allowed = 1;
4310cf655043SAndy Whitcroft				}
4311cf655043SAndy Whitcroft			}
4312cf655043SAndy Whitcroft			if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
431369932487SJustin P. Mattock				my $herectx = $here . "\n";
4314f055663cSAndy Whitcroft				my $cnt = statement_rawlines($block);
4315cf655043SAndy Whitcroft
4316f055663cSAndy Whitcroft				for (my $n = 0; $n < $cnt; $n++) {
431769932487SJustin P. Mattock					$herectx .= raw_line($linenr, $n) . "\n";
4318cf655043SAndy Whitcroft				}
4319cf655043SAndy Whitcroft
4320000d1cc1SJoe Perches				WARN("BRACES",
4321000d1cc1SJoe Perches				     "braces {} are not necessary for single statement blocks\n" . $herectx);
4322f0a594c1SAndy Whitcroft			}
4323f0a594c1SAndy Whitcroft		}
4324f0a594c1SAndy Whitcroft
43250979ae66SJoe Perches# check for unnecessary blank lines around braces
432677b9a53aSJoe Perches		if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
43270979ae66SJoe Perches			CHK("BRACES",
43280979ae66SJoe Perches			    "Blank lines aren't necessary before a close brace '}'\n" . $hereprev);
43290979ae66SJoe Perches		}
433077b9a53aSJoe Perches		if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
43310979ae66SJoe Perches			CHK("BRACES",
43320979ae66SJoe Perches			    "Blank lines aren't necessary after an open brace '{'\n" . $hereprev);
43330979ae66SJoe Perches		}
43340979ae66SJoe Perches
43354a0df2efSAndy Whitcroft# no volatiles please
43366c72ffaaSAndy Whitcroft		my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
43376c72ffaaSAndy Whitcroft		if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
4338000d1cc1SJoe Perches			WARN("VOLATILE",
4339000d1cc1SJoe Perches			     "Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
43404a0df2efSAndy Whitcroft		}
43414a0df2efSAndy Whitcroft
434200df344fSAndy Whitcroft# warn about #if 0
4343c45dcabdSAndy Whitcroft		if ($line =~ /^.\s*\#\s*if\s+0\b/) {
4344000d1cc1SJoe Perches			CHK("REDUNDANT_CODE",
4345000d1cc1SJoe Perches			    "if this code is redundant consider removing it\n" .
4346de7d4f0eSAndy Whitcroft				$herecurr);
43474a0df2efSAndy Whitcroft		}
43484a0df2efSAndy Whitcroft
434903df4b51SAndy Whitcroft# check for needless "if (<foo>) fn(<foo>)" uses
435003df4b51SAndy Whitcroft		if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
435103df4b51SAndy Whitcroft			my $expr = '\s*\(\s*' . quotemeta($1) . '\s*\)\s*;';
435203df4b51SAndy Whitcroft			if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?)$expr/) {
435303df4b51SAndy Whitcroft				WARN('NEEDLESS_IF',
435403df4b51SAndy Whitcroft				     "$1(NULL) is safe this check is probably not required\n" . $hereprev);
43554c432a8fSGreg Kroah-Hartman			}
43564c432a8fSGreg Kroah-Hartman		}
4357f0a594c1SAndy Whitcroft
4358ebfdc409SJoe Perches# check for unnecessary "Out of Memory" messages
4359ebfdc409SJoe Perches		if ($line =~ /^\+.*\b$logFunctions\s*\(/ &&
4360ebfdc409SJoe Perches		    $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ &&
4361ebfdc409SJoe Perches		    (defined $1 || defined $3) &&
4362ebfdc409SJoe Perches		    $linenr > 3) {
4363ebfdc409SJoe Perches			my $testval = $2;
4364ebfdc409SJoe Perches			my $testline = $lines[$linenr - 3];
4365ebfdc409SJoe Perches
4366ebfdc409SJoe Perches			my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0);
4367ebfdc409SJoe Perches#			print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n");
4368ebfdc409SJoe Perches
4369ebfdc409SJoe 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)/) {
4370ebfdc409SJoe Perches				WARN("OOM_MESSAGE",
4371ebfdc409SJoe Perches				     "Possible unnecessary 'out of memory' message\n" . $hereprev);
4372ebfdc409SJoe Perches			}
4373ebfdc409SJoe Perches		}
4374ebfdc409SJoe Perches
43758716de38SJoe Perches# check for bad placement of section $InitAttribute (e.g.: __initdata)
43768716de38SJoe Perches		if ($line =~ /(\b$InitAttribute\b)/) {
43778716de38SJoe Perches			my $attr = $1;
43788716de38SJoe Perches			if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
43798716de38SJoe Perches				my $ptr = $1;
43808716de38SJoe Perches				my $var = $2;
43818716de38SJoe Perches				if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
43828716de38SJoe Perches				      ERROR("MISPLACED_INIT",
43838716de38SJoe Perches					    "$attr should be placed after $var\n" . $herecurr)) ||
43848716de38SJoe Perches				     ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
43858716de38SJoe Perches				      WARN("MISPLACED_INIT",
43868716de38SJoe Perches					   "$attr should be placed after $var\n" . $herecurr))) &&
43878716de38SJoe Perches				    $fix) {
4388194f66fcSJoe 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;
43898716de38SJoe Perches				}
43908716de38SJoe Perches			}
43918716de38SJoe Perches		}
43928716de38SJoe Perches
4393e970b884SJoe Perches# check for $InitAttributeData (ie: __initdata) with const
4394e970b884SJoe Perches		if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
4395e970b884SJoe Perches			my $attr = $1;
4396e970b884SJoe Perches			$attr =~ /($InitAttributePrefix)(.*)/;
4397e970b884SJoe Perches			my $attr_prefix = $1;
4398e970b884SJoe Perches			my $attr_type = $2;
4399e970b884SJoe Perches			if (ERROR("INIT_ATTRIBUTE",
4400e970b884SJoe Perches				  "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
4401e970b884SJoe Perches			    $fix) {
4402194f66fcSJoe Perches				$fixed[$fixlinenr] =~
4403e970b884SJoe Perches				    s/$InitAttributeData/${attr_prefix}initconst/;
4404e970b884SJoe Perches			}
4405e970b884SJoe Perches		}
4406e970b884SJoe Perches
4407e970b884SJoe Perches# check for $InitAttributeConst (ie: __initconst) without const
4408e970b884SJoe Perches		if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
4409e970b884SJoe Perches			my $attr = $1;
4410e970b884SJoe Perches			if (ERROR("INIT_ATTRIBUTE",
4411e970b884SJoe Perches				  "Use of $attr requires a separate use of const\n" . $herecurr) &&
4412e970b884SJoe Perches			    $fix) {
4413194f66fcSJoe Perches				my $lead = $fixed[$fixlinenr] =~
4414e970b884SJoe Perches				    /(^\+\s*(?:static\s+))/;
4415e970b884SJoe Perches				$lead = rtrim($1);
4416e970b884SJoe Perches				$lead = "$lead " if ($lead !~ /^\+$/);
4417e970b884SJoe Perches				$lead = "${lead}const ";
4418194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/;
4419e970b884SJoe Perches			}
4420e970b884SJoe Perches		}
4421e970b884SJoe Perches
4422fbdb8138SJoe Perches# don't use __constant_<foo> functions outside of include/uapi/
4423fbdb8138SJoe Perches		if ($realfile !~ m@^include/uapi/@ &&
4424fbdb8138SJoe Perches		    $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) {
4425fbdb8138SJoe Perches			my $constant_func = $1;
4426fbdb8138SJoe Perches			my $func = $constant_func;
4427fbdb8138SJoe Perches			$func =~ s/^__constant_//;
4428fbdb8138SJoe Perches			if (WARN("CONSTANT_CONVERSION",
4429fbdb8138SJoe Perches				 "$constant_func should be $func\n" . $herecurr) &&
4430fbdb8138SJoe Perches			    $fix) {
4431194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g;
4432fbdb8138SJoe Perches			}
4433fbdb8138SJoe Perches		}
4434fbdb8138SJoe Perches
44351a15a250SPatrick Pannuto# prefer usleep_range over udelay
443637581c28SBruce Allan		if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
443743c1d77cSJoe Perches			my $delay = $1;
44381a15a250SPatrick Pannuto			# ignore udelay's < 10, however
443943c1d77cSJoe Perches			if (! ($delay < 10) ) {
4440000d1cc1SJoe Perches				CHK("USLEEP_RANGE",
444143c1d77cSJoe Perches				    "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $herecurr);
444243c1d77cSJoe Perches			}
444343c1d77cSJoe Perches			if ($delay > 2000) {
444443c1d77cSJoe Perches				WARN("LONG_UDELAY",
444543c1d77cSJoe Perches				     "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr);
44461a15a250SPatrick Pannuto			}
44471a15a250SPatrick Pannuto		}
44481a15a250SPatrick Pannuto
444909ef8725SPatrick Pannuto# warn about unexpectedly long msleep's
445009ef8725SPatrick Pannuto		if ($line =~ /\bmsleep\s*\((\d+)\);/) {
445109ef8725SPatrick Pannuto			if ($1 < 20) {
4452000d1cc1SJoe Perches				WARN("MSLEEP",
445343c1d77cSJoe Perches				     "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $herecurr);
445409ef8725SPatrick Pannuto			}
445509ef8725SPatrick Pannuto		}
445609ef8725SPatrick Pannuto
445736ec1939SJoe Perches# check for comparisons of jiffies
445836ec1939SJoe Perches		if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
445936ec1939SJoe Perches			WARN("JIFFIES_COMPARISON",
446036ec1939SJoe Perches			     "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
446136ec1939SJoe Perches		}
446236ec1939SJoe Perches
44639d7a34a5SJoe Perches# check for comparisons of get_jiffies_64()
44649d7a34a5SJoe Perches		if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
44659d7a34a5SJoe Perches			WARN("JIFFIES_COMPARISON",
44669d7a34a5SJoe Perches			     "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
44679d7a34a5SJoe Perches		}
44689d7a34a5SJoe Perches
446900df344fSAndy Whitcroft# warn about #ifdefs in C files
4470c45dcabdSAndy Whitcroft#		if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
447100df344fSAndy Whitcroft#			print "#ifdef in C files should be avoided\n";
447200df344fSAndy Whitcroft#			print "$herecurr";
447300df344fSAndy Whitcroft#			$clean = 0;
447400df344fSAndy Whitcroft#		}
447500df344fSAndy Whitcroft
447622f2a2efSAndy Whitcroft# warn about spacing in #ifdefs
4477c45dcabdSAndy Whitcroft		if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
44783705ce5bSJoe Perches			if (ERROR("SPACING",
44793705ce5bSJoe Perches				  "exactly one space required after that #$1\n" . $herecurr) &&
44803705ce5bSJoe Perches			    $fix) {
4481194f66fcSJoe Perches				$fixed[$fixlinenr] =~
44823705ce5bSJoe Perches				    s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
44833705ce5bSJoe Perches			}
44843705ce5bSJoe Perches
448522f2a2efSAndy Whitcroft		}
448622f2a2efSAndy Whitcroft
44874a0df2efSAndy Whitcroft# check for spinlock_t definitions without a comment.
4488171ae1a4SAndy Whitcroft		if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
4489171ae1a4SAndy Whitcroft		    $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
44904a0df2efSAndy Whitcroft			my $which = $1;
44914a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
4492000d1cc1SJoe Perches				CHK("UNCOMMENTED_DEFINITION",
4493000d1cc1SJoe Perches				    "$1 definition without comment\n" . $herecurr);
44944a0df2efSAndy Whitcroft			}
44954a0df2efSAndy Whitcroft		}
44964a0df2efSAndy Whitcroft# check for memory barriers without a comment.
44974a0df2efSAndy Whitcroft		if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
44984a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
4499c1fd7bb9SJoe Perches				WARN("MEMORY_BARRIER",
4500000d1cc1SJoe Perches				     "memory barrier without comment\n" . $herecurr);
45014a0df2efSAndy Whitcroft			}
45024a0df2efSAndy Whitcroft		}
45034a0df2efSAndy Whitcroft# check of hardware specific defines
4504c45dcabdSAndy Whitcroft		if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
4505000d1cc1SJoe Perches			CHK("ARCH_DEFINES",
4506000d1cc1SJoe Perches			    "architecture specific defines should be avoided\n" .  $herecurr);
45070a920b5bSAndy Whitcroft		}
4508653d4876SAndy Whitcroft
4509d4977c78STobias Klauser# Check that the storage class is at the beginning of a declaration
4510d4977c78STobias Klauser		if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
4511000d1cc1SJoe Perches			WARN("STORAGE_CLASS",
4512000d1cc1SJoe Perches			     "storage class should be at the beginning of the declaration\n" . $herecurr)
4513d4977c78STobias Klauser		}
4514d4977c78STobias Klauser
4515de7d4f0eSAndy Whitcroft# check the location of the inline attribute, that it is between
4516de7d4f0eSAndy Whitcroft# storage class and type.
45179c0ca6f9SAndy Whitcroft		if ($line =~ /\b$Type\s+$Inline\b/ ||
45189c0ca6f9SAndy Whitcroft		    $line =~ /\b$Inline\s+$Storage\b/) {
4519000d1cc1SJoe Perches			ERROR("INLINE_LOCATION",
4520000d1cc1SJoe Perches			      "inline keyword should sit between storage class and type\n" . $herecurr);
4521de7d4f0eSAndy Whitcroft		}
4522de7d4f0eSAndy Whitcroft
45238905a67cSAndy Whitcroft# Check for __inline__ and __inline, prefer inline
45242b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
45252b7ab453SJoe Perches		    $line =~ /\b(__inline__|__inline)\b/) {
4526d5e616fcSJoe Perches			if (WARN("INLINE",
4527d5e616fcSJoe Perches				 "plain inline is preferred over $1\n" . $herecurr) &&
4528d5e616fcSJoe Perches			    $fix) {
4529194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/;
4530d5e616fcSJoe Perches
4531d5e616fcSJoe Perches			}
45328905a67cSAndy Whitcroft		}
45338905a67cSAndy Whitcroft
45343d130fd0SJoe Perches# Check for __attribute__ packed, prefer __packed
45352b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
45362b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
4537000d1cc1SJoe Perches			WARN("PREFER_PACKED",
4538000d1cc1SJoe Perches			     "__packed is preferred over __attribute__((packed))\n" . $herecurr);
45393d130fd0SJoe Perches		}
45403d130fd0SJoe Perches
454139b7e287SJoe Perches# Check for __attribute__ aligned, prefer __aligned
45422b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
45432b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
4544000d1cc1SJoe Perches			WARN("PREFER_ALIGNED",
4545000d1cc1SJoe Perches			     "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
454639b7e287SJoe Perches		}
454739b7e287SJoe Perches
45485f14d3bdSJoe Perches# Check for __attribute__ format(printf, prefer __printf
45492b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
45502b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
4551d5e616fcSJoe Perches			if (WARN("PREFER_PRINTF",
4552d5e616fcSJoe Perches				 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) &&
4553d5e616fcSJoe Perches			    $fix) {
4554194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf\s*,\s*(.*)\)\s*\)\s*\)/"__printf(" . trim($1) . ")"/ex;
4555d5e616fcSJoe Perches
4556d5e616fcSJoe Perches			}
45575f14d3bdSJoe Perches		}
45585f14d3bdSJoe Perches
45596061d949SJoe Perches# Check for __attribute__ format(scanf, prefer __scanf
45602b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
45612b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
4562d5e616fcSJoe Perches			if (WARN("PREFER_SCANF",
4563d5e616fcSJoe Perches				 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) &&
4564d5e616fcSJoe Perches			    $fix) {
4565194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\s*,\s*(.*)\)\s*\)\s*\)/"__scanf(" . trim($1) . ")"/ex;
4566d5e616fcSJoe Perches			}
45676061d949SJoe Perches		}
45686061d949SJoe Perches
45698f53a9b8SJoe Perches# check for sizeof(&)
45708f53a9b8SJoe Perches		if ($line =~ /\bsizeof\s*\(\s*\&/) {
4571000d1cc1SJoe Perches			WARN("SIZEOF_ADDRESS",
4572000d1cc1SJoe Perches			     "sizeof(& should be avoided\n" . $herecurr);
45738f53a9b8SJoe Perches		}
45748f53a9b8SJoe Perches
457566c80b60SJoe Perches# check for sizeof without parenthesis
457666c80b60SJoe Perches		if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
4577d5e616fcSJoe Perches			if (WARN("SIZEOF_PARENTHESIS",
4578d5e616fcSJoe Perches				 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
4579d5e616fcSJoe Perches			    $fix) {
4580194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
4581d5e616fcSJoe Perches			}
458266c80b60SJoe Perches		}
458366c80b60SJoe Perches
4584428e2fdcSJoe Perches# check for line continuations in quoted strings with odd counts of "
4585428e2fdcSJoe Perches		if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) {
4586000d1cc1SJoe Perches			WARN("LINE_CONTINUATIONS",
4587000d1cc1SJoe Perches			     "Avoid line continuations in quoted strings\n" . $herecurr);
4588428e2fdcSJoe Perches		}
4589428e2fdcSJoe Perches
459088982feaSJoe Perches# check for struct spinlock declarations
459188982feaSJoe Perches		if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
459288982feaSJoe Perches			WARN("USE_SPINLOCK_T",
459388982feaSJoe Perches			     "struct spinlock should be spinlock_t\n" . $herecurr);
459488982feaSJoe Perches		}
459588982feaSJoe Perches
4596a6962d72SJoe Perches# check for seq_printf uses that could be seq_puts
459706668727SJoe Perches		if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
4598a6962d72SJoe Perches			my $fmt = get_quoted_string($line, $rawline);
459906668727SJoe Perches			if ($fmt ne "" && $fmt !~ /[^\\]\%/) {
4600d5e616fcSJoe Perches				if (WARN("PREFER_SEQ_PUTS",
4601d5e616fcSJoe Perches					 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
4602d5e616fcSJoe Perches				    $fix) {
4603194f66fcSJoe Perches					$fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/;
4604d5e616fcSJoe Perches				}
4605a6962d72SJoe Perches			}
4606a6962d72SJoe Perches		}
4607a6962d72SJoe Perches
4608554e165cSAndy Whitcroft# Check for misused memsets
4609d1fe9c09SJoe Perches		if ($^V && $^V ge 5.10.0 &&
4610d1fe9c09SJoe Perches		    defined $stat &&
4611d7c76ba7SJoe Perches		    $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/s) {
4612554e165cSAndy Whitcroft
4613d7c76ba7SJoe Perches			my $ms_addr = $2;
4614d1fe9c09SJoe Perches			my $ms_val = $7;
4615d1fe9c09SJoe Perches			my $ms_size = $12;
4616d7c76ba7SJoe Perches
4617554e165cSAndy Whitcroft			if ($ms_size =~ /^(0x|)0$/i) {
4618554e165cSAndy Whitcroft				ERROR("MEMSET",
4619d7c76ba7SJoe Perches				      "memset to 0's uses 0 as the 2nd argument, not the 3rd\n" . "$here\n$stat\n");
4620554e165cSAndy Whitcroft			} elsif ($ms_size =~ /^(0x|)1$/i) {
4621554e165cSAndy Whitcroft				WARN("MEMSET",
4622d7c76ba7SJoe Perches				     "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
4623d7c76ba7SJoe Perches			}
4624d7c76ba7SJoe Perches		}
4625d7c76ba7SJoe Perches
462698a9bba5SJoe Perches# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar)
462798a9bba5SJoe Perches		if ($^V && $^V ge 5.10.0 &&
462898a9bba5SJoe Perches		    $line =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/s) {
462998a9bba5SJoe Perches			if (WARN("PREFER_ETHER_ADDR_COPY",
463098a9bba5SJoe Perches				 "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . $herecurr) &&
463198a9bba5SJoe Perches			    $fix) {
4632194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/;
463398a9bba5SJoe Perches			}
463498a9bba5SJoe Perches		}
463598a9bba5SJoe Perches
4636d7c76ba7SJoe Perches# typecasts on min/max could be min_t/max_t
4637d1fe9c09SJoe Perches		if ($^V && $^V ge 5.10.0 &&
4638d1fe9c09SJoe Perches		    defined $stat &&
4639d7c76ba7SJoe Perches		    $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
4640d1fe9c09SJoe Perches			if (defined $2 || defined $7) {
4641d7c76ba7SJoe Perches				my $call = $1;
4642d7c76ba7SJoe Perches				my $cast1 = deparenthesize($2);
4643d7c76ba7SJoe Perches				my $arg1 = $3;
4644d1fe9c09SJoe Perches				my $cast2 = deparenthesize($7);
4645d1fe9c09SJoe Perches				my $arg2 = $8;
4646d7c76ba7SJoe Perches				my $cast;
4647d7c76ba7SJoe Perches
4648d1fe9c09SJoe Perches				if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
4649d7c76ba7SJoe Perches					$cast = "$cast1 or $cast2";
4650d7c76ba7SJoe Perches				} elsif ($cast1 ne "") {
4651d7c76ba7SJoe Perches					$cast = $cast1;
4652d7c76ba7SJoe Perches				} else {
4653d7c76ba7SJoe Perches					$cast = $cast2;
4654d7c76ba7SJoe Perches				}
4655d7c76ba7SJoe Perches				WARN("MINMAX",
4656d7c76ba7SJoe Perches				     "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
4657554e165cSAndy Whitcroft			}
4658554e165cSAndy Whitcroft		}
4659554e165cSAndy Whitcroft
46604a273195SJoe Perches# check usleep_range arguments
46614a273195SJoe Perches		if ($^V && $^V ge 5.10.0 &&
46624a273195SJoe Perches		    defined $stat &&
46634a273195SJoe Perches		    $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
46644a273195SJoe Perches			my $min = $1;
46654a273195SJoe Perches			my $max = $7;
46664a273195SJoe Perches			if ($min eq $max) {
46674a273195SJoe Perches				WARN("USLEEP_RANGE",
46684a273195SJoe Perches				     "usleep_range should not use min == max args; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
46694a273195SJoe Perches			} elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
46704a273195SJoe Perches				 $min > $max) {
46714a273195SJoe Perches				WARN("USLEEP_RANGE",
46724a273195SJoe Perches				     "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
46734a273195SJoe Perches			}
46744a273195SJoe Perches		}
46754a273195SJoe Perches
4676823b794cSJoe Perches# check for naked sscanf
4677823b794cSJoe Perches		if ($^V && $^V ge 5.10.0 &&
4678823b794cSJoe Perches		    defined $stat &&
46796c8bd707SJoe Perches		    $line =~ /\bsscanf\b/ &&
4680823b794cSJoe Perches		    ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
4681823b794cSJoe Perches		     $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ &&
4682823b794cSJoe Perches		     $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) {
4683823b794cSJoe Perches			my $lc = $stat =~ tr@\n@@;
4684823b794cSJoe Perches			$lc = $lc + $linenr;
4685823b794cSJoe Perches			my $stat_real = raw_line($linenr, 0);
4686823b794cSJoe Perches		        for (my $count = $linenr + 1; $count <= $lc; $count++) {
4687823b794cSJoe Perches				$stat_real = $stat_real . "\n" . raw_line($count, 0);
4688823b794cSJoe Perches			}
4689823b794cSJoe Perches			WARN("NAKED_SSCANF",
4690823b794cSJoe Perches			     "unchecked sscanf return value\n" . "$here\n$stat_real\n");
4691823b794cSJoe Perches		}
4692823b794cSJoe Perches
4693afc819abSJoe Perches# check for simple sscanf that should be kstrto<foo>
4694afc819abSJoe Perches		if ($^V && $^V ge 5.10.0 &&
4695afc819abSJoe Perches		    defined $stat &&
4696afc819abSJoe Perches		    $line =~ /\bsscanf\b/) {
4697afc819abSJoe Perches			my $lc = $stat =~ tr@\n@@;
4698afc819abSJoe Perches			$lc = $lc + $linenr;
4699afc819abSJoe Perches			my $stat_real = raw_line($linenr, 0);
4700afc819abSJoe Perches		        for (my $count = $linenr + 1; $count <= $lc; $count++) {
4701afc819abSJoe Perches				$stat_real = $stat_real . "\n" . raw_line($count, 0);
4702afc819abSJoe Perches			}
4703afc819abSJoe Perches			if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) {
4704afc819abSJoe Perches				my $format = $6;
4705afc819abSJoe Perches				my $count = $format =~ tr@%@%@;
4706afc819abSJoe Perches				if ($count == 1 &&
4707afc819abSJoe Perches				    $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) {
4708afc819abSJoe Perches					WARN("SSCANF_TO_KSTRTO",
4709afc819abSJoe Perches					     "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n");
4710afc819abSJoe Perches				}
4711afc819abSJoe Perches			}
4712afc819abSJoe Perches		}
4713afc819abSJoe Perches
471470dc8a48SJoe Perches# check for new externs in .h files.
471570dc8a48SJoe Perches		if ($realfile =~ /\.h$/ &&
471670dc8a48SJoe Perches		    $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
4717d1d85780SJoe Perches			if (CHK("AVOID_EXTERNS",
471870dc8a48SJoe Perches				"extern prototypes should be avoided in .h files\n" . $herecurr) &&
471970dc8a48SJoe Perches			    $fix) {
4720194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
472170dc8a48SJoe Perches			}
472270dc8a48SJoe Perches		}
472370dc8a48SJoe Perches
4724de7d4f0eSAndy Whitcroft# check for new externs in .c files.
4725171ae1a4SAndy Whitcroft		if ($realfile =~ /\.c$/ && defined $stat &&
4726c45dcabdSAndy Whitcroft		    $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
4727171ae1a4SAndy Whitcroft		{
4728c45dcabdSAndy Whitcroft			my $function_name = $1;
4729c45dcabdSAndy Whitcroft			my $paren_space = $2;
4730171ae1a4SAndy Whitcroft
4731171ae1a4SAndy Whitcroft			my $s = $stat;
4732171ae1a4SAndy Whitcroft			if (defined $cond) {
4733171ae1a4SAndy Whitcroft				substr($s, 0, length($cond), '');
4734171ae1a4SAndy Whitcroft			}
4735c45dcabdSAndy Whitcroft			if ($s =~ /^\s*;/ &&
4736c45dcabdSAndy Whitcroft			    $function_name ne 'uninitialized_var')
4737c45dcabdSAndy Whitcroft			{
4738000d1cc1SJoe Perches				WARN("AVOID_EXTERNS",
4739000d1cc1SJoe Perches				     "externs should be avoided in .c files\n" .  $herecurr);
4740de7d4f0eSAndy Whitcroft			}
4741de7d4f0eSAndy Whitcroft
4742171ae1a4SAndy Whitcroft			if ($paren_space =~ /\n/) {
4743000d1cc1SJoe Perches				WARN("FUNCTION_ARGUMENTS",
4744000d1cc1SJoe Perches				     "arguments for function declarations should follow identifier\n" . $herecurr);
4745171ae1a4SAndy Whitcroft			}
47469c9ba34eSAndy Whitcroft
47479c9ba34eSAndy Whitcroft		} elsif ($realfile =~ /\.c$/ && defined $stat &&
47489c9ba34eSAndy Whitcroft		    $stat =~ /^.\s*extern\s+/)
47499c9ba34eSAndy Whitcroft		{
4750000d1cc1SJoe Perches			WARN("AVOID_EXTERNS",
4751000d1cc1SJoe Perches			     "externs should be avoided in .c files\n" .  $herecurr);
4752171ae1a4SAndy Whitcroft		}
4753171ae1a4SAndy Whitcroft
4754de7d4f0eSAndy Whitcroft# checks for new __setup's
4755de7d4f0eSAndy Whitcroft		if ($rawline =~ /\b__setup\("([^"]*)"/) {
4756de7d4f0eSAndy Whitcroft			my $name = $1;
4757de7d4f0eSAndy Whitcroft
4758de7d4f0eSAndy Whitcroft			if (!grep(/$name/, @setup_docs)) {
4759000d1cc1SJoe Perches				CHK("UNDOCUMENTED_SETUP",
4760000d1cc1SJoe Perches				    "__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
4761de7d4f0eSAndy Whitcroft			}
4762653d4876SAndy Whitcroft		}
47639c0ca6f9SAndy Whitcroft
47649c0ca6f9SAndy Whitcroft# check for pointless casting of kmalloc return
4765caf2a54fSJoe Perches		if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) {
4766000d1cc1SJoe Perches			WARN("UNNECESSARY_CASTS",
4767000d1cc1SJoe Perches			     "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
47689c0ca6f9SAndy Whitcroft		}
476913214adfSAndy Whitcroft
4770a640d25cSJoe Perches# alloc style
4771a640d25cSJoe Perches# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
4772a640d25cSJoe Perches		if ($^V && $^V ge 5.10.0 &&
4773a640d25cSJoe Perches		    $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*([kv][mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
4774a640d25cSJoe Perches			CHK("ALLOC_SIZEOF_STRUCT",
4775a640d25cSJoe Perches			    "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
4776a640d25cSJoe Perches		}
4777a640d25cSJoe Perches
477860a55369SJoe Perches# check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc
477960a55369SJoe Perches		if ($^V && $^V ge 5.10.0 &&
4780e367455aSJoe Perches		    $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*(k[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)\s*,/) {
478160a55369SJoe Perches			my $oldfunc = $3;
478260a55369SJoe Perches			my $a1 = $4;
478360a55369SJoe Perches			my $a2 = $10;
478460a55369SJoe Perches			my $newfunc = "kmalloc_array";
478560a55369SJoe Perches			$newfunc = "kcalloc" if ($oldfunc eq "kzalloc");
478660a55369SJoe Perches			my $r1 = $a1;
478760a55369SJoe Perches			my $r2 = $a2;
478860a55369SJoe Perches			if ($a1 =~ /^sizeof\s*\S/) {
478960a55369SJoe Perches				$r1 = $a2;
479060a55369SJoe Perches				$r2 = $a1;
479160a55369SJoe Perches			}
4792e367455aSJoe Perches			if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ &&
4793e367455aSJoe Perches			    !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) {
4794e367455aSJoe Perches				if (WARN("ALLOC_WITH_MULTIPLY",
4795e367455aSJoe Perches					 "Prefer $newfunc over $oldfunc with multiply\n" . $herecurr) &&
4796e367455aSJoe Perches				    $fix) {
4797194f66fcSJoe 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;
479860a55369SJoe Perches
479960a55369SJoe Perches				}
480060a55369SJoe Perches			}
480160a55369SJoe Perches		}
480260a55369SJoe Perches
4803972fdea2SJoe Perches# check for krealloc arg reuse
4804972fdea2SJoe Perches		if ($^V && $^V ge 5.10.0 &&
4805972fdea2SJoe Perches		    $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*\1\s*,/) {
4806972fdea2SJoe Perches			WARN("KREALLOC_ARG_REUSE",
4807972fdea2SJoe Perches			     "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
4808972fdea2SJoe Perches		}
4809972fdea2SJoe Perches
48105ce59ae0SJoe Perches# check for alloc argument mismatch
48115ce59ae0SJoe Perches		if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
48125ce59ae0SJoe Perches			WARN("ALLOC_ARRAY_ARGS",
48135ce59ae0SJoe Perches			     "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
48145ce59ae0SJoe Perches		}
48155ce59ae0SJoe Perches
4816caf2a54fSJoe Perches# check for multiple semicolons
4817caf2a54fSJoe Perches		if ($line =~ /;\s*;\s*$/) {
4818d5e616fcSJoe Perches			if (WARN("ONE_SEMICOLON",
4819d5e616fcSJoe Perches				 "Statements terminations use 1 semicolon\n" . $herecurr) &&
4820d5e616fcSJoe Perches			    $fix) {
4821194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g;
4822d5e616fcSJoe Perches			}
4823d1e2ad07SJoe Perches		}
4824d1e2ad07SJoe Perches
4825e81f239bSJoe Perches# check for case / default statements not preceded by break/fallthrough/switch
4826c34c09a8SJoe Perches		if ($line =~ /^.\s*(?:case\s+(?:$Ident|$Constant)\s*|default):/) {
4827c34c09a8SJoe Perches			my $has_break = 0;
4828c34c09a8SJoe Perches			my $has_statement = 0;
4829c34c09a8SJoe Perches			my $count = 0;
4830c34c09a8SJoe Perches			my $prevline = $linenr;
4831e81f239bSJoe Perches			while ($prevline > 1 && ($file || $count < 3) && !$has_break) {
4832c34c09a8SJoe Perches				$prevline--;
4833c34c09a8SJoe Perches				my $rline = $rawlines[$prevline - 1];
4834c34c09a8SJoe Perches				my $fline = $lines[$prevline - 1];
4835c34c09a8SJoe Perches				last if ($fline =~ /^\@\@/);
4836c34c09a8SJoe Perches				next if ($fline =~ /^\-/);
4837c34c09a8SJoe Perches				next if ($fline =~ /^.(?:\s*(?:case\s+(?:$Ident|$Constant)[\s$;]*|default):[\s$;]*)*$/);
4838c34c09a8SJoe Perches				$has_break = 1 if ($rline =~ /fall[\s_-]*(through|thru)/i);
4839c34c09a8SJoe Perches				next if ($fline =~ /^.[\s$;]*$/);
4840c34c09a8SJoe Perches				$has_statement = 1;
4841c34c09a8SJoe Perches				$count++;
4842c34c09a8SJoe Perches				$has_break = 1 if ($fline =~ /\bswitch\b|\b(?:break\s*;[\s$;]*$|return\b|goto\b|continue\b)/);
4843c34c09a8SJoe Perches			}
4844c34c09a8SJoe Perches			if (!$has_break && $has_statement) {
4845c34c09a8SJoe Perches				WARN("MISSING_BREAK",
4846c34c09a8SJoe Perches				     "Possible switch case/default not preceeded by break or fallthrough comment\n" . $herecurr);
4847c34c09a8SJoe Perches			}
4848c34c09a8SJoe Perches		}
4849c34c09a8SJoe Perches
4850d1e2ad07SJoe Perches# check for switch/default statements without a break;
4851d1e2ad07SJoe Perches		if ($^V && $^V ge 5.10.0 &&
4852d1e2ad07SJoe Perches		    defined $stat &&
4853d1e2ad07SJoe Perches		    $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
4854d1e2ad07SJoe Perches			my $ctx = '';
4855d1e2ad07SJoe Perches			my $herectx = $here . "\n";
4856d1e2ad07SJoe Perches			my $cnt = statement_rawlines($stat);
4857d1e2ad07SJoe Perches			for (my $n = 0; $n < $cnt; $n++) {
4858d1e2ad07SJoe Perches				$herectx .= raw_line($linenr, $n) . "\n";
4859d1e2ad07SJoe Perches			}
4860d1e2ad07SJoe Perches			WARN("DEFAULT_NO_BREAK",
4861d1e2ad07SJoe Perches			     "switch default: should use break\n" . $herectx);
4862caf2a54fSJoe Perches		}
4863caf2a54fSJoe Perches
486413214adfSAndy Whitcroft# check for gcc specific __FUNCTION__
4865d5e616fcSJoe Perches		if ($line =~ /\b__FUNCTION__\b/) {
4866d5e616fcSJoe Perches			if (WARN("USE_FUNC",
4867d5e616fcSJoe Perches				 "__func__ should be used instead of gcc specific __FUNCTION__\n"  . $herecurr) &&
4868d5e616fcSJoe Perches			    $fix) {
4869194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g;
4870d5e616fcSJoe Perches			}
487113214adfSAndy Whitcroft		}
4872773647a0SAndy Whitcroft
48732c92488aSJoe Perches# check for use of yield()
48742c92488aSJoe Perches		if ($line =~ /\byield\s*\(\s*\)/) {
48752c92488aSJoe Perches			WARN("YIELD",
48762c92488aSJoe Perches			     "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n"  . $herecurr);
48772c92488aSJoe Perches		}
48782c92488aSJoe Perches
4879179f8f40SJoe Perches# check for comparisons against true and false
4880179f8f40SJoe Perches		if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
4881179f8f40SJoe Perches			my $lead = $1;
4882179f8f40SJoe Perches			my $arg = $2;
4883179f8f40SJoe Perches			my $test = $3;
4884179f8f40SJoe Perches			my $otype = $4;
4885179f8f40SJoe Perches			my $trail = $5;
4886179f8f40SJoe Perches			my $op = "!";
4887179f8f40SJoe Perches
4888179f8f40SJoe Perches			($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
4889179f8f40SJoe Perches
4890179f8f40SJoe Perches			my $type = lc($otype);
4891179f8f40SJoe Perches			if ($type =~ /^(?:true|false)$/) {
4892179f8f40SJoe Perches				if (("$test" eq "==" && "$type" eq "true") ||
4893179f8f40SJoe Perches				    ("$test" eq "!=" && "$type" eq "false")) {
4894179f8f40SJoe Perches					$op = "";
4895179f8f40SJoe Perches				}
4896179f8f40SJoe Perches
4897179f8f40SJoe Perches				CHK("BOOL_COMPARISON",
4898179f8f40SJoe Perches				    "Using comparison to $otype is error prone\n" . $herecurr);
4899179f8f40SJoe Perches
4900179f8f40SJoe Perches## maybe suggesting a correct construct would better
4901179f8f40SJoe Perches##				    "Using comparison to $otype is error prone.  Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
4902179f8f40SJoe Perches
4903179f8f40SJoe Perches			}
4904179f8f40SJoe Perches		}
4905179f8f40SJoe Perches
49064882720bSThomas Gleixner# check for semaphores initialized locked
49074882720bSThomas Gleixner		if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
4908000d1cc1SJoe Perches			WARN("CONSIDER_COMPLETION",
4909000d1cc1SJoe Perches			     "consider using a completion\n" . $herecurr);
4910773647a0SAndy Whitcroft		}
49116712d858SJoe Perches
491267d0a075SJoe Perches# recommend kstrto* over simple_strto* and strict_strto*
491367d0a075SJoe Perches		if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
4914000d1cc1SJoe Perches			WARN("CONSIDER_KSTRTO",
491567d0a075SJoe Perches			     "$1 is obsolete, use k$3 instead\n" . $herecurr);
4916773647a0SAndy Whitcroft		}
49176712d858SJoe Perches
4918ae3ccc46SFabian Frederick# check for __initcall(), use device_initcall() explicitly or more appropriate function please
4919f3db6639SMichael Ellerman		if ($line =~ /^.\s*__initcall\s*\(/) {
4920000d1cc1SJoe Perches			WARN("USE_DEVICE_INITCALL",
4921ae3ccc46SFabian Frederick			     "please use device_initcall() or more appropriate function instead of __initcall() (see include/linux/init.h)\n" . $herecurr);
4922f3db6639SMichael Ellerman		}
49236712d858SJoe Perches
492479404849SEmese Revfy# check for various ops structs, ensure they are const.
492579404849SEmese Revfy		my $struct_ops = qr{acpi_dock_ops|
492679404849SEmese Revfy				address_space_operations|
492779404849SEmese Revfy				backlight_ops|
492879404849SEmese Revfy				block_device_operations|
492979404849SEmese Revfy				dentry_operations|
493079404849SEmese Revfy				dev_pm_ops|
493179404849SEmese Revfy				dma_map_ops|
493279404849SEmese Revfy				extent_io_ops|
493379404849SEmese Revfy				file_lock_operations|
493479404849SEmese Revfy				file_operations|
493579404849SEmese Revfy				hv_ops|
493679404849SEmese Revfy				ide_dma_ops|
493779404849SEmese Revfy				intel_dvo_dev_ops|
493879404849SEmese Revfy				item_operations|
493979404849SEmese Revfy				iwl_ops|
494079404849SEmese Revfy				kgdb_arch|
494179404849SEmese Revfy				kgdb_io|
494279404849SEmese Revfy				kset_uevent_ops|
494379404849SEmese Revfy				lock_manager_operations|
494479404849SEmese Revfy				microcode_ops|
494579404849SEmese Revfy				mtrr_ops|
494679404849SEmese Revfy				neigh_ops|
494779404849SEmese Revfy				nlmsvc_binding|
494879404849SEmese Revfy				pci_raw_ops|
494979404849SEmese Revfy				pipe_buf_operations|
495079404849SEmese Revfy				platform_hibernation_ops|
495179404849SEmese Revfy				platform_suspend_ops|
495279404849SEmese Revfy				proto_ops|
495379404849SEmese Revfy				rpc_pipe_ops|
495479404849SEmese Revfy				seq_operations|
495579404849SEmese Revfy				snd_ac97_build_ops|
495679404849SEmese Revfy				soc_pcmcia_socket_ops|
495779404849SEmese Revfy				stacktrace_ops|
495879404849SEmese Revfy				sysfs_ops|
495979404849SEmese Revfy				tty_operations|
496079404849SEmese Revfy				usb_mon_operations|
496179404849SEmese Revfy				wd_ops}x;
49626903ffb2SAndy Whitcroft		if ($line !~ /\bconst\b/ &&
496379404849SEmese Revfy		    $line =~ /\bstruct\s+($struct_ops)\b/) {
4964000d1cc1SJoe Perches			WARN("CONST_STRUCT",
4965000d1cc1SJoe Perches			     "struct $1 should normally be const\n" .
49666903ffb2SAndy Whitcroft				$herecurr);
49672b6db5cbSAndy Whitcroft		}
4968773647a0SAndy Whitcroft
4969773647a0SAndy Whitcroft# use of NR_CPUS is usually wrong
4970773647a0SAndy Whitcroft# ignore definitions of NR_CPUS and usage to define arrays as likely right
4971773647a0SAndy Whitcroft		if ($line =~ /\bNR_CPUS\b/ &&
4972c45dcabdSAndy Whitcroft		    $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
4973c45dcabdSAndy Whitcroft		    $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
4974171ae1a4SAndy Whitcroft		    $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
4975171ae1a4SAndy Whitcroft		    $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
4976171ae1a4SAndy Whitcroft		    $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
4977773647a0SAndy Whitcroft		{
4978000d1cc1SJoe Perches			WARN("NR_CPUS",
4979000d1cc1SJoe Perches			     "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
4980773647a0SAndy Whitcroft		}
49819c9ba34eSAndy Whitcroft
498252ea8506SJoe Perches# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
498352ea8506SJoe Perches		if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
498452ea8506SJoe Perches			ERROR("DEFINE_ARCH_HAS",
498552ea8506SJoe Perches			      "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
498652ea8506SJoe Perches		}
498752ea8506SJoe Perches
49889c9ba34eSAndy Whitcroft# check for %L{u,d,i} in strings
49899c9ba34eSAndy Whitcroft		my $string;
49909c9ba34eSAndy Whitcroft		while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
49919c9ba34eSAndy Whitcroft			$string = substr($rawline, $-[1], $+[1] - $-[1]);
49922a1bc5d5SAndy Whitcroft			$string =~ s/%%/__/g;
49939c9ba34eSAndy Whitcroft			if ($string =~ /(?<!%)%L[udi]/) {
4994000d1cc1SJoe Perches				WARN("PRINTF_L",
4995000d1cc1SJoe Perches				     "\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
49969c9ba34eSAndy Whitcroft				last;
49979c9ba34eSAndy Whitcroft			}
49989c9ba34eSAndy Whitcroft		}
4999691d77b6SAndy Whitcroft
5000691d77b6SAndy Whitcroft# whine mightly about in_atomic
5001691d77b6SAndy Whitcroft		if ($line =~ /\bin_atomic\s*\(/) {
5002691d77b6SAndy Whitcroft			if ($realfile =~ m@^drivers/@) {
5003000d1cc1SJoe Perches				ERROR("IN_ATOMIC",
5004000d1cc1SJoe Perches				      "do not use in_atomic in drivers\n" . $herecurr);
5005f4a87736SAndy Whitcroft			} elsif ($realfile !~ m@^kernel/@) {
5006000d1cc1SJoe Perches				WARN("IN_ATOMIC",
5007000d1cc1SJoe Perches				     "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
5008691d77b6SAndy Whitcroft			}
5009691d77b6SAndy Whitcroft		}
50101704f47bSPeter Zijlstra
50111704f47bSPeter Zijlstra# check for lockdep_set_novalidate_class
50121704f47bSPeter Zijlstra		if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
50131704f47bSPeter Zijlstra		    $line =~ /__lockdep_no_validate__\s*\)/ ) {
50141704f47bSPeter Zijlstra			if ($realfile !~ m@^kernel/lockdep@ &&
50151704f47bSPeter Zijlstra			    $realfile !~ m@^include/linux/lockdep@ &&
50161704f47bSPeter Zijlstra			    $realfile !~ m@^drivers/base/core@) {
5017000d1cc1SJoe Perches				ERROR("LOCKDEP",
5018000d1cc1SJoe Perches				      "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
50191704f47bSPeter Zijlstra			}
50201704f47bSPeter Zijlstra		}
502188f8831cSDave Jones
502288f8831cSDave Jones		if ($line =~ /debugfs_create_file.*S_IWUGO/ ||
502388f8831cSDave Jones		    $line =~ /DEVICE_ATTR.*S_IWUGO/ ) {
5024000d1cc1SJoe Perches			WARN("EXPORTED_WORLD_WRITABLE",
5025000d1cc1SJoe Perches			     "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
502688f8831cSDave Jones		}
50272435880fSJoe Perches
5028515a235eSJoe Perches# Mode permission misuses where it seems decimal should be octal
5029515a235eSJoe Perches# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop
5030515a235eSJoe Perches		if ($^V && $^V ge 5.10.0 &&
5031515a235eSJoe Perches		    $line =~ /$mode_perms_search/) {
50322435880fSJoe Perches			foreach my $entry (@mode_permission_funcs) {
50332435880fSJoe Perches				my $func = $entry->[0];
50342435880fSJoe Perches				my $arg_pos = $entry->[1];
50352435880fSJoe Perches
50362435880fSJoe Perches				my $skip_args = "";
50372435880fSJoe Perches				if ($arg_pos > 1) {
50382435880fSJoe Perches					$arg_pos--;
50392435880fSJoe Perches					$skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}";
50402435880fSJoe Perches				}
50412435880fSJoe Perches				my $test = "\\b$func\\s*\\(${skip_args}([\\d]+)\\s*[,\\)]";
5042515a235eSJoe Perches				if ($line =~ /$test/) {
50432435880fSJoe Perches					my $val = $1;
50442435880fSJoe Perches					$val = $6 if ($skip_args ne "");
50452435880fSJoe Perches
50461727cc70SJoe Perches					if ($val !~ /^0$/ &&
50471727cc70SJoe Perches					    (($val =~ /^$Int$/ && $val !~ /^$Octal$/) ||
50481727cc70SJoe Perches					     length($val) ne 4)) {
50492435880fSJoe Perches						ERROR("NON_OCTAL_PERMISSIONS",
50501727cc70SJoe Perches						      "Use 4 digit octal (0777) not decimal permissions\n" . $herecurr);
50512435880fSJoe Perches					}
50522435880fSJoe Perches				}
50532435880fSJoe Perches			}
505413214adfSAndy Whitcroft		}
5055515a235eSJoe Perches	}
505613214adfSAndy Whitcroft
505713214adfSAndy Whitcroft	# If we have no input at all, then there is nothing to report on
505813214adfSAndy Whitcroft	# so just keep quiet.
505913214adfSAndy Whitcroft	if ($#rawlines == -1) {
506013214adfSAndy Whitcroft		exit(0);
50610a920b5bSAndy Whitcroft	}
50620a920b5bSAndy Whitcroft
50638905a67cSAndy Whitcroft	# In mailback mode only produce a report in the negative, for
50648905a67cSAndy Whitcroft	# things that appear to be patches.
50658905a67cSAndy Whitcroft	if ($mailback && ($clean == 1 || !$is_patch)) {
50668905a67cSAndy Whitcroft		exit(0);
50678905a67cSAndy Whitcroft	}
50688905a67cSAndy Whitcroft
50698905a67cSAndy Whitcroft	# This is not a patch, and we are are in 'no-patch' mode so
50708905a67cSAndy Whitcroft	# just keep quiet.
50718905a67cSAndy Whitcroft	if (!$chk_patch && !$is_patch) {
50728905a67cSAndy Whitcroft		exit(0);
50738905a67cSAndy Whitcroft	}
50748905a67cSAndy Whitcroft
50758905a67cSAndy Whitcroft	if (!$is_patch) {
5076000d1cc1SJoe Perches		ERROR("NOT_UNIFIED_DIFF",
5077000d1cc1SJoe Perches		      "Does not appear to be a unified-diff format patch\n");
50780a920b5bSAndy Whitcroft	}
50790a920b5bSAndy Whitcroft	if ($is_patch && $chk_signoff && $signoff == 0) {
5080000d1cc1SJoe Perches		ERROR("MISSING_SIGN_OFF",
5081000d1cc1SJoe Perches		      "Missing Signed-off-by: line(s)\n");
50820a920b5bSAndy Whitcroft	}
50830a920b5bSAndy Whitcroft
5084f0a594c1SAndy Whitcroft	print report_dump();
508513214adfSAndy Whitcroft	if ($summary && !($clean == 1 && $quiet == 1)) {
508613214adfSAndy Whitcroft		print "$filename " if ($summary_file);
50876c72ffaaSAndy Whitcroft		print "total: $cnt_error errors, $cnt_warn warnings, " .
50886c72ffaaSAndy Whitcroft			(($check)? "$cnt_chk checks, " : "") .
50896c72ffaaSAndy Whitcroft			"$cnt_lines lines checked\n";
50908905a67cSAndy Whitcroft		print "\n" if ($quiet == 0);
50916c72ffaaSAndy Whitcroft	}
50928905a67cSAndy Whitcroft
5093d2c0a235SAndy Whitcroft	if ($quiet == 0) {
5094d1fe9c09SJoe Perches
5095d1fe9c09SJoe Perches		if ($^V lt 5.10.0) {
5096d1fe9c09SJoe Perches			print("NOTE: perl $^V is not modern enough to detect all possible issues.\n");
5097d1fe9c09SJoe Perches			print("An upgrade to at least perl v5.10.0 is suggested.\n\n");
5098d1fe9c09SJoe Perches		}
5099d1fe9c09SJoe Perches
5100d2c0a235SAndy Whitcroft		# If there were whitespace errors which cleanpatch can fix
5101d2c0a235SAndy Whitcroft		# then suggest that.
5102d2c0a235SAndy Whitcroft		if ($rpt_cleaners) {
5103d2c0a235SAndy Whitcroft			print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n";
5104d2c0a235SAndy Whitcroft			print "      scripts/cleanfile\n\n";
5105b0781216SMike Frysinger			$rpt_cleaners = 0;
5106d2c0a235SAndy Whitcroft		}
5107d2c0a235SAndy Whitcroft	}
5108d2c0a235SAndy Whitcroft
510991bfe484SJoe Perches	hash_show_words(\%use_type, "Used");
511091bfe484SJoe Perches	hash_show_words(\%ignore_type, "Ignored");
5111000d1cc1SJoe Perches
5112d752fcc8SJoe Perches	if ($clean == 0 && $fix &&
5113d752fcc8SJoe Perches	    ("@rawlines" ne "@fixed" ||
5114d752fcc8SJoe Perches	     $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) {
51159624b8d6SJoe Perches		my $newfile = $filename;
51169624b8d6SJoe Perches		$newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace);
51173705ce5bSJoe Perches		my $linecount = 0;
51183705ce5bSJoe Perches		my $f;
51193705ce5bSJoe Perches
5120d752fcc8SJoe Perches		@fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted);
5121d752fcc8SJoe Perches
51223705ce5bSJoe Perches		open($f, '>', $newfile)
51233705ce5bSJoe Perches		    or die "$P: Can't open $newfile for write\n";
51243705ce5bSJoe Perches		foreach my $fixed_line (@fixed) {
51253705ce5bSJoe Perches			$linecount++;
51263705ce5bSJoe Perches			if ($file) {
51273705ce5bSJoe Perches				if ($linecount > 3) {
51283705ce5bSJoe Perches					$fixed_line =~ s/^\+//;
51293705ce5bSJoe Perches					print $f $fixed_line . "\n";
51303705ce5bSJoe Perches				}
51313705ce5bSJoe Perches			} else {
51323705ce5bSJoe Perches				print $f $fixed_line . "\n";
51333705ce5bSJoe Perches			}
51343705ce5bSJoe Perches		}
51353705ce5bSJoe Perches		close($f);
51363705ce5bSJoe Perches
51373705ce5bSJoe Perches		if (!$quiet) {
51383705ce5bSJoe Perches			print << "EOM";
51393705ce5bSJoe PerchesWrote EXPERIMENTAL --fix correction(s) to '$newfile'
51403705ce5bSJoe Perches
51413705ce5bSJoe PerchesDo _NOT_ trust the results written to this file.
51423705ce5bSJoe PerchesDo _NOT_ submit these changes without inspecting them for correctness.
51433705ce5bSJoe Perches
51443705ce5bSJoe PerchesThis EXPERIMENTAL file is simply a convenience to help rewrite patches.
51453705ce5bSJoe PerchesNo warranties, expressed or implied...
51463705ce5bSJoe Perches
51473705ce5bSJoe PerchesEOM
51483705ce5bSJoe Perches		}
51493705ce5bSJoe Perches	}
51503705ce5bSJoe Perches
51510a920b5bSAndy Whitcroft	if ($clean == 1 && $quiet == 0) {
5152c2fdda0dSAndy Whitcroft		print "$vname has no obvious style problems and is ready for submission.\n"
51530a920b5bSAndy Whitcroft	}
51540a920b5bSAndy Whitcroft	if ($clean == 0 && $quiet == 0) {
5155000d1cc1SJoe Perches		print << "EOM";
5156000d1cc1SJoe Perches$vname has style problems, please review.
5157000d1cc1SJoe Perches
5158000d1cc1SJoe PerchesIf any of these errors are false positives, please report
5159000d1cc1SJoe Perchesthem to the maintainer, see CHECKPATCH in MAINTAINERS.
5160000d1cc1SJoe PerchesEOM
51610a920b5bSAndy Whitcroft	}
516213214adfSAndy Whitcroft
51630a920b5bSAndy Whitcroft	return $clean;
51640a920b5bSAndy Whitcroft}
5165