xref: /linux-6.15/scripts/checkpatch.pl (revision d752fcc8)
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;
3128716de38SJoe Perchesour $NonptrTypeWithAttr;
3138905a67cSAndy Whitcroftour $Type;
3148905a67cSAndy Whitcroftour $Declare;
3158905a67cSAndy Whitcroft
31615662b3eSJoe Perchesour $NON_ASCII_UTF8	= qr{
31715662b3eSJoe Perches	[\xC2-\xDF][\x80-\xBF]               # non-overlong 2-byte
318171ae1a4SAndy Whitcroft	|  \xE0[\xA0-\xBF][\x80-\xBF]        # excluding overlongs
319171ae1a4SAndy Whitcroft	| [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}  # straight 3-byte
320171ae1a4SAndy Whitcroft	|  \xED[\x80-\x9F][\x80-\xBF]        # excluding surrogates
321171ae1a4SAndy Whitcroft	|  \xF0[\x90-\xBF][\x80-\xBF]{2}     # planes 1-3
322171ae1a4SAndy Whitcroft	| [\xF1-\xF3][\x80-\xBF]{3}          # planes 4-15
323171ae1a4SAndy Whitcroft	|  \xF4[\x80-\x8F][\x80-\xBF]{2}     # plane 16
324171ae1a4SAndy Whitcroft}x;
325171ae1a4SAndy Whitcroft
32615662b3eSJoe Perchesour $UTF8	= qr{
32715662b3eSJoe Perches	[\x09\x0A\x0D\x20-\x7E]              # ASCII
32815662b3eSJoe Perches	| $NON_ASCII_UTF8
32915662b3eSJoe Perches}x;
33015662b3eSJoe Perches
3318ed22cadSAndy Whitcroftour $typeTypedefs = qr{(?x:
332fb9e9096SAndy Whitcroft	(?:__)?(?:u|s|be|le)(?:8|16|32|64)|
3338ed22cadSAndy Whitcroft	atomic_t
3348ed22cadSAndy Whitcroft)};
3358ed22cadSAndy Whitcroft
336691e669bSJoe Perchesour $logFunctions = qr{(?x:
3376e60c02eSJoe Perches	printk(?:_ratelimited|_once|)|
3387d0b6594SJacob Keller	(?:[a-z0-9]+_){1,2}(?:printk|emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)(?:_ratelimited|_once|)|
3396e60c02eSJoe Perches	WARN(?:_RATELIMIT|_ONCE|)|
340b0531722SJoe Perches	panic|
34106668727SJoe Perches	MODULE_[A-Z_]+|
34206668727SJoe Perches	seq_vprintf|seq_printf|seq_puts
343691e669bSJoe Perches)};
344691e669bSJoe Perches
34520112475SJoe Perchesour $signature_tags = qr{(?xi:
34620112475SJoe Perches	Signed-off-by:|
34720112475SJoe Perches	Acked-by:|
34820112475SJoe Perches	Tested-by:|
34920112475SJoe Perches	Reviewed-by:|
35020112475SJoe Perches	Reported-by:|
3518543ae12SMugunthan V N	Suggested-by:|
35220112475SJoe Perches	To:|
35320112475SJoe Perches	Cc:
35420112475SJoe Perches)};
35520112475SJoe Perches
3568905a67cSAndy Whitcroftour @typeList = (
3578905a67cSAndy Whitcroft	qr{void},
358c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?char},
359c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?short},
360c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?int},
361c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?long},
362c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?long\s+int},
363c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?long\s+long},
364c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?long\s+long\s+int},
3658905a67cSAndy Whitcroft	qr{unsigned},
3668905a67cSAndy Whitcroft	qr{float},
3678905a67cSAndy Whitcroft	qr{double},
3688905a67cSAndy Whitcroft	qr{bool},
3698905a67cSAndy Whitcroft	qr{struct\s+$Ident},
3708905a67cSAndy Whitcroft	qr{union\s+$Ident},
3718905a67cSAndy Whitcroft	qr{enum\s+$Ident},
3728905a67cSAndy Whitcroft	qr{${Ident}_t},
3738905a67cSAndy Whitcroft	qr{${Ident}_handler},
3748905a67cSAndy Whitcroft	qr{${Ident}_handler_fn},
3758905a67cSAndy Whitcroft);
3768716de38SJoe Perchesour @typeListWithAttr = (
3778716de38SJoe Perches	@typeList,
3788716de38SJoe Perches	qr{struct\s+$InitAttribute\s+$Ident},
3798716de38SJoe Perches	qr{union\s+$InitAttribute\s+$Ident},
3808716de38SJoe Perches);
3818716de38SJoe Perches
382c45dcabdSAndy Whitcroftour @modifierList = (
383c45dcabdSAndy Whitcroft	qr{fastcall},
384c45dcabdSAndy Whitcroft);
3858905a67cSAndy Whitcroft
3862435880fSJoe Perchesour @mode_permission_funcs = (
3872435880fSJoe Perches	["module_param", 3],
3882435880fSJoe Perches	["module_param_(?:array|named|string)", 4],
3892435880fSJoe Perches	["module_param_array_named", 5],
3902435880fSJoe Perches	["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2],
3912435880fSJoe Perches	["proc_create(?:_data|)", 2],
3922435880fSJoe Perches	["(?:CLASS|DEVICE|SENSOR)_ATTR", 2],
3932435880fSJoe Perches);
3942435880fSJoe Perches
395515a235eSJoe Perches#Create a search pattern for all these functions to speed up a loop below
396515a235eSJoe Perchesour $mode_perms_search = "";
397515a235eSJoe Perchesforeach my $entry (@mode_permission_funcs) {
398515a235eSJoe Perches	$mode_perms_search .= '|' if ($mode_perms_search ne "");
399515a235eSJoe Perches	$mode_perms_search .= $entry->[0];
400515a235eSJoe Perches}
401515a235eSJoe Perches
4023f7bac03SJoe Perchesour $declaration_macros = qr{(?x:
4033f7bac03SJoe Perches	(?:$Storage\s+)?(?:DECLARE|DEFINE)_[A-Z]+\s*\(|
4043f7bac03SJoe Perches	(?:$Storage\s+)?LIST_HEAD\s*\(
4053f7bac03SJoe Perches)};
4063f7bac03SJoe Perches
4077840a94cSWolfram Sangour $allowed_asm_includes = qr{(?x:
4087840a94cSWolfram Sang	irq|
4097840a94cSWolfram Sang	memory
4107840a94cSWolfram Sang)};
4117840a94cSWolfram Sang# memory.h: ARM has a custom one
4127840a94cSWolfram Sang
4138905a67cSAndy Whitcroftsub build_types {
414d2172eb5SAndy Whitcroft	my $mods = "(?x:  \n" . join("|\n  ", @modifierList) . "\n)";
415d2172eb5SAndy Whitcroft	my $all = "(?x:  \n" . join("|\n  ", @typeList) . "\n)";
4168716de38SJoe Perches	my $allWithAttr = "(?x:  \n" . join("|\n  ", @typeListWithAttr) . "\n)";
417c8cb2ca3SAndy Whitcroft	$Modifier	= qr{(?:$Attribute|$Sparse|$mods)};
4188905a67cSAndy Whitcroft	$NonptrType	= qr{
419d2172eb5SAndy Whitcroft			(?:$Modifier\s+|const\s+)*
420cf655043SAndy Whitcroft			(?:
4216b48db24SAndy Whitcroft				(?:typeof|__typeof__)\s*\([^\)]*\)|
4228ed22cadSAndy Whitcroft				(?:$typeTypedefs\b)|
423c45dcabdSAndy Whitcroft				(?:${all}\b)
424cf655043SAndy Whitcroft			)
425c8cb2ca3SAndy Whitcroft			(?:\s+$Modifier|\s+const)*
4268905a67cSAndy Whitcroft		  }x;
4278716de38SJoe Perches	$NonptrTypeWithAttr	= qr{
4288716de38SJoe Perches			(?:$Modifier\s+|const\s+)*
4298716de38SJoe Perches			(?:
4308716de38SJoe Perches				(?:typeof|__typeof__)\s*\([^\)]*\)|
4318716de38SJoe Perches				(?:$typeTypedefs\b)|
4328716de38SJoe Perches				(?:${allWithAttr}\b)
4338716de38SJoe Perches			)
4348716de38SJoe Perches			(?:\s+$Modifier|\s+const)*
4358716de38SJoe Perches		  }x;
4368905a67cSAndy Whitcroft	$Type	= qr{
437c45dcabdSAndy Whitcroft			$NonptrType
4381574a29fSJoe Perches			(?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
439c8cb2ca3SAndy Whitcroft			(?:\s+$Inline|\s+$Modifier)*
4408905a67cSAndy Whitcroft		  }x;
44191cb5195SJoe Perches	$Declare	= qr{(?:$Storage\s+(?:$Inline\s+)?)?$Type};
4428905a67cSAndy Whitcroft}
4438905a67cSAndy Whitcroftbuild_types();
4446c72ffaaSAndy Whitcroft
4457d2367afSJoe Perchesour $Typecast	= qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
446d1fe9c09SJoe Perches
447d1fe9c09SJoe Perches# Using $balanced_parens, $LvalOrFunc, or $FuncArg
448d1fe9c09SJoe Perches# requires at least perl version v5.10.0
449d1fe9c09SJoe Perches# Any use must be runtime checked with $^V
450d1fe9c09SJoe Perches
451d1fe9c09SJoe Perchesour $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
4522435880fSJoe Perchesour $LvalOrFunc	= qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*};
453d7c76ba7SJoe Perchesour $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant)};
4547d2367afSJoe Perches
4557d2367afSJoe Perchessub deparenthesize {
4567d2367afSJoe Perches	my ($string) = @_;
4577d2367afSJoe Perches	return "" if (!defined($string));
4585b9553abSJoe Perches
4595b9553abSJoe Perches	while ($string =~ /^\s*\(.*\)\s*$/) {
4605b9553abSJoe Perches		$string =~ s@^\s*\(\s*@@;
4615b9553abSJoe Perches		$string =~ s@\s*\)\s*$@@;
4625b9553abSJoe Perches	}
4635b9553abSJoe Perches
4647d2367afSJoe Perches	$string =~ s@\s+@ @g;
4655b9553abSJoe Perches
4667d2367afSJoe Perches	return $string;
4677d2367afSJoe Perches}
4687d2367afSJoe Perches
4693445686aSJoe Perchessub seed_camelcase_file {
4703445686aSJoe Perches	my ($file) = @_;
4713445686aSJoe Perches
4723445686aSJoe Perches	return if (!(-f $file));
4733445686aSJoe Perches
4743445686aSJoe Perches	local $/;
4753445686aSJoe Perches
4763445686aSJoe Perches	open(my $include_file, '<', "$file")
4773445686aSJoe Perches	    or warn "$P: Can't read '$file' $!\n";
4783445686aSJoe Perches	my $text = <$include_file>;
4793445686aSJoe Perches	close($include_file);
4803445686aSJoe Perches
4813445686aSJoe Perches	my @lines = split('\n', $text);
4823445686aSJoe Perches
4833445686aSJoe Perches	foreach my $line (@lines) {
4843445686aSJoe Perches		next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/);
4853445686aSJoe Perches		if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) {
4863445686aSJoe Perches			$camelcase{$1} = 1;
48711ea516aSJoe Perches		} elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) {
48811ea516aSJoe Perches			$camelcase{$1} = 1;
48911ea516aSJoe Perches		} elsif ($line =~ /^\s*(?:union|struct|enum)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[;\{]/) {
4903445686aSJoe Perches			$camelcase{$1} = 1;
4913445686aSJoe Perches		}
4923445686aSJoe Perches	}
4933445686aSJoe Perches}
4943445686aSJoe Perches
4953445686aSJoe Perchesmy $camelcase_seeded = 0;
4963445686aSJoe Perchessub seed_camelcase_includes {
4973445686aSJoe Perches	return if ($camelcase_seeded);
4983445686aSJoe Perches
4993445686aSJoe Perches	my $files;
500c707a81dSJoe Perches	my $camelcase_cache = "";
501c707a81dSJoe Perches	my @include_files = ();
502c707a81dSJoe Perches
503c707a81dSJoe Perches	$camelcase_seeded = 1;
504351b2a1fSJoe Perches
5053645e328SRichard Genoud	if (-e ".git") {
506351b2a1fSJoe Perches		my $git_last_include_commit = `git log --no-merges --pretty=format:"%h%n" -1 -- include`;
507351b2a1fSJoe Perches		chomp $git_last_include_commit;
508c707a81dSJoe Perches		$camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit";
509c707a81dSJoe Perches	} else {
510c707a81dSJoe Perches		my $last_mod_date = 0;
511c707a81dSJoe Perches		$files = `find $root/include -name "*.h"`;
512c707a81dSJoe Perches		@include_files = split('\n', $files);
513c707a81dSJoe Perches		foreach my $file (@include_files) {
514c707a81dSJoe Perches			my $date = POSIX::strftime("%Y%m%d%H%M",
515c707a81dSJoe Perches						   localtime((stat $file)[9]));
516c707a81dSJoe Perches			$last_mod_date = $date if ($last_mod_date < $date);
517c707a81dSJoe Perches		}
518c707a81dSJoe Perches		$camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date";
519c707a81dSJoe Perches	}
520c707a81dSJoe Perches
521c707a81dSJoe Perches	if ($camelcase_cache ne "" && -f $camelcase_cache) {
522c707a81dSJoe Perches		open(my $camelcase_file, '<', "$camelcase_cache")
523c707a81dSJoe Perches		    or warn "$P: Can't read '$camelcase_cache' $!\n";
524351b2a1fSJoe Perches		while (<$camelcase_file>) {
525351b2a1fSJoe Perches			chomp;
526351b2a1fSJoe Perches			$camelcase{$_} = 1;
527351b2a1fSJoe Perches		}
528351b2a1fSJoe Perches		close($camelcase_file);
529351b2a1fSJoe Perches
530351b2a1fSJoe Perches		return;
531351b2a1fSJoe Perches	}
532c707a81dSJoe Perches
5333645e328SRichard Genoud	if (-e ".git") {
534c707a81dSJoe Perches		$files = `git ls-files "include/*.h"`;
535c707a81dSJoe Perches		@include_files = split('\n', $files);
5363445686aSJoe Perches	}
537c707a81dSJoe Perches
5383445686aSJoe Perches	foreach my $file (@include_files) {
5393445686aSJoe Perches		seed_camelcase_file($file);
5403445686aSJoe Perches	}
541351b2a1fSJoe Perches
542c707a81dSJoe Perches	if ($camelcase_cache ne "") {
543351b2a1fSJoe Perches		unlink glob ".checkpatch-camelcase.*";
544c707a81dSJoe Perches		open(my $camelcase_file, '>', "$camelcase_cache")
545c707a81dSJoe Perches		    or warn "$P: Can't write '$camelcase_cache' $!\n";
546351b2a1fSJoe Perches		foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) {
547351b2a1fSJoe Perches			print $camelcase_file ("$_\n");
548351b2a1fSJoe Perches		}
549351b2a1fSJoe Perches		close($camelcase_file);
550351b2a1fSJoe Perches	}
5513445686aSJoe Perches}
5523445686aSJoe Perches
553d311cd44SJoe Perchessub git_commit_info {
554d311cd44SJoe Perches	my ($commit, $id, $desc) = @_;
555d311cd44SJoe Perches
556d311cd44SJoe Perches	return ($id, $desc) if ((which("git") eq "") || !(-e ".git"));
557d311cd44SJoe Perches
558d311cd44SJoe Perches	my $output = `git log --no-color --format='%H %s' -1 $commit 2>&1`;
559d311cd44SJoe Perches	$output =~ s/^\s*//gm;
560d311cd44SJoe Perches	my @lines = split("\n", $output);
561d311cd44SJoe Perches
562d311cd44SJoe Perches	if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous\./) {
563d311cd44SJoe Perches# Maybe one day convert this block of bash into something that returns
564d311cd44SJoe Perches# all matching commit ids, but it's very slow...
565d311cd44SJoe Perches#
566d311cd44SJoe Perches#		echo "checking commits $1..."
567d311cd44SJoe Perches#		git rev-list --remotes | grep -i "^$1" |
568d311cd44SJoe Perches#		while read line ; do
569d311cd44SJoe Perches#		    git log --format='%H %s' -1 $line |
570d311cd44SJoe Perches#		    echo "commit $(cut -c 1-12,41-)"
571d311cd44SJoe Perches#		done
572d311cd44SJoe Perches	} elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./) {
573d311cd44SJoe Perches	} else {
574d311cd44SJoe Perches		$id = substr($lines[0], 0, 12);
575d311cd44SJoe Perches		$desc = substr($lines[0], 41);
576d311cd44SJoe Perches	}
577d311cd44SJoe Perches
578d311cd44SJoe Perches	return ($id, $desc);
579d311cd44SJoe Perches}
580d311cd44SJoe Perches
5816c72ffaaSAndy Whitcroft$chk_signoff = 0 if ($file);
5820a920b5bSAndy Whitcroft
58300df344fSAndy Whitcroftmy @rawlines = ();
584c2fdda0dSAndy Whitcroftmy @lines = ();
5853705ce5bSJoe Perchesmy @fixed = ();
586*d752fcc8SJoe Perchesmy @fixed_inserted = ();
587*d752fcc8SJoe Perchesmy @fixed_deleted = ();
588194f66fcSJoe Perchesmy $fixlinenr = -1;
589194f66fcSJoe Perches
590c2fdda0dSAndy Whitcroftmy $vname;
5916c72ffaaSAndy Whitcroftfor my $filename (@ARGV) {
59221caa13cSAndy Whitcroft	my $FILE;
5936c72ffaaSAndy Whitcroft	if ($file) {
59421caa13cSAndy Whitcroft		open($FILE, '-|', "diff -u /dev/null $filename") ||
5956c72ffaaSAndy Whitcroft			die "$P: $filename: diff failed - $!\n";
59621caa13cSAndy Whitcroft	} elsif ($filename eq '-') {
59721caa13cSAndy Whitcroft		open($FILE, '<&STDIN');
5986c72ffaaSAndy Whitcroft	} else {
59921caa13cSAndy Whitcroft		open($FILE, '<', "$filename") ||
6006c72ffaaSAndy Whitcroft			die "$P: $filename: open failed - $!\n";
6016c72ffaaSAndy Whitcroft	}
602c2fdda0dSAndy Whitcroft	if ($filename eq '-') {
603c2fdda0dSAndy Whitcroft		$vname = 'Your patch';
604c2fdda0dSAndy Whitcroft	} else {
605c2fdda0dSAndy Whitcroft		$vname = $filename;
606c2fdda0dSAndy Whitcroft	}
60721caa13cSAndy Whitcroft	while (<$FILE>) {
6080a920b5bSAndy Whitcroft		chomp;
60900df344fSAndy Whitcroft		push(@rawlines, $_);
6106c72ffaaSAndy Whitcroft	}
61121caa13cSAndy Whitcroft	close($FILE);
612c2fdda0dSAndy Whitcroft	if (!process($filename)) {
6130a920b5bSAndy Whitcroft		$exit = 1;
6140a920b5bSAndy Whitcroft	}
61500df344fSAndy Whitcroft	@rawlines = ();
61613214adfSAndy Whitcroft	@lines = ();
6173705ce5bSJoe Perches	@fixed = ();
618*d752fcc8SJoe Perches	@fixed_inserted = ();
619*d752fcc8SJoe Perches	@fixed_deleted = ();
620194f66fcSJoe Perches	$fixlinenr = -1;
6210a920b5bSAndy Whitcroft}
6220a920b5bSAndy Whitcroft
6230a920b5bSAndy Whitcroftexit($exit);
6240a920b5bSAndy Whitcroft
6250a920b5bSAndy Whitcroftsub top_of_kernel_tree {
6266c72ffaaSAndy Whitcroft	my ($root) = @_;
6276c72ffaaSAndy Whitcroft
6286c72ffaaSAndy Whitcroft	my @tree_check = (
6296c72ffaaSAndy Whitcroft		"COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
6306c72ffaaSAndy Whitcroft		"README", "Documentation", "arch", "include", "drivers",
6316c72ffaaSAndy Whitcroft		"fs", "init", "ipc", "kernel", "lib", "scripts",
6326c72ffaaSAndy Whitcroft	);
6336c72ffaaSAndy Whitcroft
6346c72ffaaSAndy Whitcroft	foreach my $check (@tree_check) {
6356c72ffaaSAndy Whitcroft		if (! -e $root . '/' . $check) {
6360a920b5bSAndy Whitcroft			return 0;
6370a920b5bSAndy Whitcroft		}
6386c72ffaaSAndy Whitcroft	}
6396c72ffaaSAndy Whitcroft	return 1;
6406c72ffaaSAndy Whitcroft}
6410a920b5bSAndy Whitcroft
64220112475SJoe Perchessub parse_email {
64320112475SJoe Perches	my ($formatted_email) = @_;
64420112475SJoe Perches
64520112475SJoe Perches	my $name = "";
64620112475SJoe Perches	my $address = "";
64720112475SJoe Perches	my $comment = "";
64820112475SJoe Perches
64920112475SJoe Perches	if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
65020112475SJoe Perches		$name = $1;
65120112475SJoe Perches		$address = $2;
65220112475SJoe Perches		$comment = $3 if defined $3;
65320112475SJoe Perches	} elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
65420112475SJoe Perches		$address = $1;
65520112475SJoe Perches		$comment = $2 if defined $2;
65620112475SJoe Perches	} elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
65720112475SJoe Perches		$address = $1;
65820112475SJoe Perches		$comment = $2 if defined $2;
65920112475SJoe Perches		$formatted_email =~ s/$address.*$//;
66020112475SJoe Perches		$name = $formatted_email;
6613705ce5bSJoe Perches		$name = trim($name);
66220112475SJoe Perches		$name =~ s/^\"|\"$//g;
66320112475SJoe Perches		# If there's a name left after stripping spaces and
66420112475SJoe Perches		# leading quotes, and the address doesn't have both
66520112475SJoe Perches		# leading and trailing angle brackets, the address
66620112475SJoe Perches		# is invalid. ie:
66720112475SJoe Perches		#   "joe smith [email protected]" bad
66820112475SJoe Perches		#   "joe smith <[email protected]" bad
66920112475SJoe Perches		if ($name ne "" && $address !~ /^<[^>]+>$/) {
67020112475SJoe Perches			$name = "";
67120112475SJoe Perches			$address = "";
67220112475SJoe Perches			$comment = "";
67320112475SJoe Perches		}
67420112475SJoe Perches	}
67520112475SJoe Perches
6763705ce5bSJoe Perches	$name = trim($name);
67720112475SJoe Perches	$name =~ s/^\"|\"$//g;
6783705ce5bSJoe Perches	$address = trim($address);
67920112475SJoe Perches	$address =~ s/^\<|\>$//g;
68020112475SJoe Perches
68120112475SJoe Perches	if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
68220112475SJoe Perches		$name =~ s/(?<!\\)"/\\"/g; ##escape quotes
68320112475SJoe Perches		$name = "\"$name\"";
68420112475SJoe Perches	}
68520112475SJoe Perches
68620112475SJoe Perches	return ($name, $address, $comment);
68720112475SJoe Perches}
68820112475SJoe Perches
68920112475SJoe Perchessub format_email {
69020112475SJoe Perches	my ($name, $address) = @_;
69120112475SJoe Perches
69220112475SJoe Perches	my $formatted_email;
69320112475SJoe Perches
6943705ce5bSJoe Perches	$name = trim($name);
69520112475SJoe Perches	$name =~ s/^\"|\"$//g;
6963705ce5bSJoe Perches	$address = trim($address);
69720112475SJoe Perches
69820112475SJoe Perches	if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
69920112475SJoe Perches		$name =~ s/(?<!\\)"/\\"/g; ##escape quotes
70020112475SJoe Perches		$name = "\"$name\"";
70120112475SJoe Perches	}
70220112475SJoe Perches
70320112475SJoe Perches	if ("$name" eq "") {
70420112475SJoe Perches		$formatted_email = "$address";
70520112475SJoe Perches	} else {
70620112475SJoe Perches		$formatted_email = "$name <$address>";
70720112475SJoe Perches	}
70820112475SJoe Perches
70920112475SJoe Perches	return $formatted_email;
71020112475SJoe Perches}
71120112475SJoe Perches
712d311cd44SJoe Perchessub which {
713d311cd44SJoe Perches    my ($bin) = @_;
714d311cd44SJoe Perches
715d311cd44SJoe Perches    foreach my $path (split(/:/, $ENV{PATH})) {
716d311cd44SJoe Perches	if (-e "$path/$bin") {
717d311cd44SJoe Perches	    return "$path/$bin";
718d311cd44SJoe Perches	}
719d311cd44SJoe Perches    }
720d311cd44SJoe Perches
721d311cd44SJoe Perches    return "";
722d311cd44SJoe Perches}
723d311cd44SJoe Perches
724000d1cc1SJoe Perchessub which_conf {
725000d1cc1SJoe Perches	my ($conf) = @_;
726000d1cc1SJoe Perches
727000d1cc1SJoe Perches	foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
728000d1cc1SJoe Perches		if (-e "$path/$conf") {
729000d1cc1SJoe Perches			return "$path/$conf";
730000d1cc1SJoe Perches		}
731000d1cc1SJoe Perches	}
732000d1cc1SJoe Perches
733000d1cc1SJoe Perches	return "";
734000d1cc1SJoe Perches}
735000d1cc1SJoe Perches
7360a920b5bSAndy Whitcroftsub expand_tabs {
7370a920b5bSAndy Whitcroft	my ($str) = @_;
7380a920b5bSAndy Whitcroft
7390a920b5bSAndy Whitcroft	my $res = '';
7400a920b5bSAndy Whitcroft	my $n = 0;
7410a920b5bSAndy Whitcroft	for my $c (split(//, $str)) {
7420a920b5bSAndy Whitcroft		if ($c eq "\t") {
7430a920b5bSAndy Whitcroft			$res .= ' ';
7440a920b5bSAndy Whitcroft			$n++;
7450a920b5bSAndy Whitcroft			for (; ($n % 8) != 0; $n++) {
7460a920b5bSAndy Whitcroft				$res .= ' ';
7470a920b5bSAndy Whitcroft			}
7480a920b5bSAndy Whitcroft			next;
7490a920b5bSAndy Whitcroft		}
7500a920b5bSAndy Whitcroft		$res .= $c;
7510a920b5bSAndy Whitcroft		$n++;
7520a920b5bSAndy Whitcroft	}
7530a920b5bSAndy Whitcroft
7540a920b5bSAndy Whitcroft	return $res;
7550a920b5bSAndy Whitcroft}
7566c72ffaaSAndy Whitcroftsub copy_spacing {
757773647a0SAndy Whitcroft	(my $res = shift) =~ tr/\t/ /c;
7586c72ffaaSAndy Whitcroft	return $res;
7596c72ffaaSAndy Whitcroft}
7600a920b5bSAndy Whitcroft
7614a0df2efSAndy Whitcroftsub line_stats {
7624a0df2efSAndy Whitcroft	my ($line) = @_;
7634a0df2efSAndy Whitcroft
7644a0df2efSAndy Whitcroft	# Drop the diff line leader and expand tabs
7654a0df2efSAndy Whitcroft	$line =~ s/^.//;
7664a0df2efSAndy Whitcroft	$line = expand_tabs($line);
7674a0df2efSAndy Whitcroft
7684a0df2efSAndy Whitcroft	# Pick the indent from the front of the line.
7694a0df2efSAndy Whitcroft	my ($white) = ($line =~ /^(\s*)/);
7704a0df2efSAndy Whitcroft
7714a0df2efSAndy Whitcroft	return (length($line), length($white));
7724a0df2efSAndy Whitcroft}
7734a0df2efSAndy Whitcroft
774773647a0SAndy Whitcroftmy $sanitise_quote = '';
775773647a0SAndy Whitcroft
776773647a0SAndy Whitcroftsub sanitise_line_reset {
777773647a0SAndy Whitcroft	my ($in_comment) = @_;
778773647a0SAndy Whitcroft
779773647a0SAndy Whitcroft	if ($in_comment) {
780773647a0SAndy Whitcroft		$sanitise_quote = '*/';
781773647a0SAndy Whitcroft	} else {
782773647a0SAndy Whitcroft		$sanitise_quote = '';
783773647a0SAndy Whitcroft	}
784773647a0SAndy Whitcroft}
78500df344fSAndy Whitcroftsub sanitise_line {
78600df344fSAndy Whitcroft	my ($line) = @_;
78700df344fSAndy Whitcroft
78800df344fSAndy Whitcroft	my $res = '';
78900df344fSAndy Whitcroft	my $l = '';
79000df344fSAndy Whitcroft
791c2fdda0dSAndy Whitcroft	my $qlen = 0;
792773647a0SAndy Whitcroft	my $off = 0;
793773647a0SAndy Whitcroft	my $c;
79400df344fSAndy Whitcroft
795773647a0SAndy Whitcroft	# Always copy over the diff marker.
796773647a0SAndy Whitcroft	$res = substr($line, 0, 1);
797773647a0SAndy Whitcroft
798773647a0SAndy Whitcroft	for ($off = 1; $off < length($line); $off++) {
799773647a0SAndy Whitcroft		$c = substr($line, $off, 1);
800773647a0SAndy Whitcroft
801773647a0SAndy Whitcroft		# Comments we are wacking completly including the begin
802773647a0SAndy Whitcroft		# and end, all to $;.
803773647a0SAndy Whitcroft		if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
804773647a0SAndy Whitcroft			$sanitise_quote = '*/';
805773647a0SAndy Whitcroft
806773647a0SAndy Whitcroft			substr($res, $off, 2, "$;$;");
807773647a0SAndy Whitcroft			$off++;
80800df344fSAndy Whitcroft			next;
809773647a0SAndy Whitcroft		}
81081bc0e02SAndy Whitcroft		if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
811773647a0SAndy Whitcroft			$sanitise_quote = '';
812773647a0SAndy Whitcroft			substr($res, $off, 2, "$;$;");
813773647a0SAndy Whitcroft			$off++;
814773647a0SAndy Whitcroft			next;
815773647a0SAndy Whitcroft		}
816113f04a8SDaniel Walker		if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
817113f04a8SDaniel Walker			$sanitise_quote = '//';
818113f04a8SDaniel Walker
819113f04a8SDaniel Walker			substr($res, $off, 2, $sanitise_quote);
820113f04a8SDaniel Walker			$off++;
821113f04a8SDaniel Walker			next;
822113f04a8SDaniel Walker		}
823773647a0SAndy Whitcroft
824773647a0SAndy Whitcroft		# A \ in a string means ignore the next character.
825773647a0SAndy Whitcroft		if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
826773647a0SAndy Whitcroft		    $c eq "\\") {
827773647a0SAndy Whitcroft			substr($res, $off, 2, 'XX');
828773647a0SAndy Whitcroft			$off++;
829773647a0SAndy Whitcroft			next;
830773647a0SAndy Whitcroft		}
831773647a0SAndy Whitcroft		# Regular quotes.
832773647a0SAndy Whitcroft		if ($c eq "'" || $c eq '"') {
833773647a0SAndy Whitcroft			if ($sanitise_quote eq '') {
834773647a0SAndy Whitcroft				$sanitise_quote = $c;
835773647a0SAndy Whitcroft
836773647a0SAndy Whitcroft				substr($res, $off, 1, $c);
837773647a0SAndy Whitcroft				next;
838773647a0SAndy Whitcroft			} elsif ($sanitise_quote eq $c) {
839773647a0SAndy Whitcroft				$sanitise_quote = '';
84000df344fSAndy Whitcroft			}
84100df344fSAndy Whitcroft		}
842773647a0SAndy Whitcroft
843fae17daeSAndy Whitcroft		#print "c<$c> SQ<$sanitise_quote>\n";
844773647a0SAndy Whitcroft		if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
845773647a0SAndy Whitcroft			substr($res, $off, 1, $;);
846113f04a8SDaniel Walker		} elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
847113f04a8SDaniel Walker			substr($res, $off, 1, $;);
848773647a0SAndy Whitcroft		} elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
849773647a0SAndy Whitcroft			substr($res, $off, 1, 'X');
85000df344fSAndy Whitcroft		} else {
851773647a0SAndy Whitcroft			substr($res, $off, 1, $c);
85200df344fSAndy Whitcroft		}
853c2fdda0dSAndy Whitcroft	}
854c2fdda0dSAndy Whitcroft
855113f04a8SDaniel Walker	if ($sanitise_quote eq '//') {
856113f04a8SDaniel Walker		$sanitise_quote = '';
857113f04a8SDaniel Walker	}
858113f04a8SDaniel Walker
859c2fdda0dSAndy Whitcroft	# The pathname on a #include may be surrounded by '<' and '>'.
860c45dcabdSAndy Whitcroft	if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
861c2fdda0dSAndy Whitcroft		my $clean = 'X' x length($1);
862c2fdda0dSAndy Whitcroft		$res =~ s@\<.*\>@<$clean>@;
863c2fdda0dSAndy Whitcroft
864c2fdda0dSAndy Whitcroft	# The whole of a #error is a string.
865c45dcabdSAndy Whitcroft	} elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
866c2fdda0dSAndy Whitcroft		my $clean = 'X' x length($1);
867c45dcabdSAndy Whitcroft		$res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
868c2fdda0dSAndy Whitcroft	}
869c2fdda0dSAndy Whitcroft
87000df344fSAndy Whitcroft	return $res;
87100df344fSAndy Whitcroft}
87200df344fSAndy Whitcroft
873a6962d72SJoe Perchessub get_quoted_string {
874a6962d72SJoe Perches	my ($line, $rawline) = @_;
875a6962d72SJoe Perches
876a6962d72SJoe Perches	return "" if ($line !~ m/(\"[X]+\")/g);
877a6962d72SJoe Perches	return substr($rawline, $-[0], $+[0] - $-[0]);
878a6962d72SJoe Perches}
879a6962d72SJoe Perches
8808905a67cSAndy Whitcroftsub ctx_statement_block {
8818905a67cSAndy Whitcroft	my ($linenr, $remain, $off) = @_;
8828905a67cSAndy Whitcroft	my $line = $linenr - 1;
8838905a67cSAndy Whitcroft	my $blk = '';
8848905a67cSAndy Whitcroft	my $soff = $off;
8858905a67cSAndy Whitcroft	my $coff = $off - 1;
886773647a0SAndy Whitcroft	my $coff_set = 0;
8878905a67cSAndy Whitcroft
88813214adfSAndy Whitcroft	my $loff = 0;
88913214adfSAndy Whitcroft
8908905a67cSAndy Whitcroft	my $type = '';
8918905a67cSAndy Whitcroft	my $level = 0;
892a2750645SAndy Whitcroft	my @stack = ();
893cf655043SAndy Whitcroft	my $p;
8948905a67cSAndy Whitcroft	my $c;
8958905a67cSAndy Whitcroft	my $len = 0;
89613214adfSAndy Whitcroft
89713214adfSAndy Whitcroft	my $remainder;
8988905a67cSAndy Whitcroft	while (1) {
899a2750645SAndy Whitcroft		@stack = (['', 0]) if ($#stack == -1);
900a2750645SAndy Whitcroft
901773647a0SAndy Whitcroft		#warn "CSB: blk<$blk> remain<$remain>\n";
9028905a67cSAndy Whitcroft		# If we are about to drop off the end, pull in more
9038905a67cSAndy Whitcroft		# context.
9048905a67cSAndy Whitcroft		if ($off >= $len) {
9058905a67cSAndy Whitcroft			for (; $remain > 0; $line++) {
906dea33496SAndy Whitcroft				last if (!defined $lines[$line]);
907c2fdda0dSAndy Whitcroft				next if ($lines[$line] =~ /^-/);
9088905a67cSAndy Whitcroft				$remain--;
90913214adfSAndy Whitcroft				$loff = $len;
910c2fdda0dSAndy Whitcroft				$blk .= $lines[$line] . "\n";
9118905a67cSAndy Whitcroft				$len = length($blk);
9128905a67cSAndy Whitcroft				$line++;
9138905a67cSAndy Whitcroft				last;
9148905a67cSAndy Whitcroft			}
9158905a67cSAndy Whitcroft			# Bail if there is no further context.
9168905a67cSAndy Whitcroft			#warn "CSB: blk<$blk> off<$off> len<$len>\n";
91713214adfSAndy Whitcroft			if ($off >= $len) {
9188905a67cSAndy Whitcroft				last;
9198905a67cSAndy Whitcroft			}
920f74bd194SAndy Whitcroft			if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
921f74bd194SAndy Whitcroft				$level++;
922f74bd194SAndy Whitcroft				$type = '#';
923f74bd194SAndy Whitcroft			}
9248905a67cSAndy Whitcroft		}
925cf655043SAndy Whitcroft		$p = $c;
9268905a67cSAndy Whitcroft		$c = substr($blk, $off, 1);
92713214adfSAndy Whitcroft		$remainder = substr($blk, $off);
9288905a67cSAndy Whitcroft
929773647a0SAndy Whitcroft		#warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
9304635f4fbSAndy Whitcroft
9314635f4fbSAndy Whitcroft		# Handle nested #if/#else.
9324635f4fbSAndy Whitcroft		if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
9334635f4fbSAndy Whitcroft			push(@stack, [ $type, $level ]);
9344635f4fbSAndy Whitcroft		} elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
9354635f4fbSAndy Whitcroft			($type, $level) = @{$stack[$#stack - 1]};
9364635f4fbSAndy Whitcroft		} elsif ($remainder =~ /^#\s*endif\b/) {
9374635f4fbSAndy Whitcroft			($type, $level) = @{pop(@stack)};
9384635f4fbSAndy Whitcroft		}
9394635f4fbSAndy Whitcroft
9408905a67cSAndy Whitcroft		# Statement ends at the ';' or a close '}' at the
9418905a67cSAndy Whitcroft		# outermost level.
9428905a67cSAndy Whitcroft		if ($level == 0 && $c eq ';') {
9438905a67cSAndy Whitcroft			last;
9448905a67cSAndy Whitcroft		}
9458905a67cSAndy Whitcroft
94613214adfSAndy Whitcroft		# An else is really a conditional as long as its not else if
947773647a0SAndy Whitcroft		if ($level == 0 && $coff_set == 0 &&
948773647a0SAndy Whitcroft				(!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
949773647a0SAndy Whitcroft				$remainder =~ /^(else)(?:\s|{)/ &&
950773647a0SAndy Whitcroft				$remainder !~ /^else\s+if\b/) {
951773647a0SAndy Whitcroft			$coff = $off + length($1) - 1;
952773647a0SAndy Whitcroft			$coff_set = 1;
953773647a0SAndy Whitcroft			#warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
954773647a0SAndy Whitcroft			#warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
95513214adfSAndy Whitcroft		}
95613214adfSAndy Whitcroft
9578905a67cSAndy Whitcroft		if (($type eq '' || $type eq '(') && $c eq '(') {
9588905a67cSAndy Whitcroft			$level++;
9598905a67cSAndy Whitcroft			$type = '(';
9608905a67cSAndy Whitcroft		}
9618905a67cSAndy Whitcroft		if ($type eq '(' && $c eq ')') {
9628905a67cSAndy Whitcroft			$level--;
9638905a67cSAndy Whitcroft			$type = ($level != 0)? '(' : '';
9648905a67cSAndy Whitcroft
9658905a67cSAndy Whitcroft			if ($level == 0 && $coff < $soff) {
9668905a67cSAndy Whitcroft				$coff = $off;
967773647a0SAndy Whitcroft				$coff_set = 1;
968773647a0SAndy Whitcroft				#warn "CSB: mark coff<$coff>\n";
9698905a67cSAndy Whitcroft			}
9708905a67cSAndy Whitcroft		}
9718905a67cSAndy Whitcroft		if (($type eq '' || $type eq '{') && $c eq '{') {
9728905a67cSAndy Whitcroft			$level++;
9738905a67cSAndy Whitcroft			$type = '{';
9748905a67cSAndy Whitcroft		}
9758905a67cSAndy Whitcroft		if ($type eq '{' && $c eq '}') {
9768905a67cSAndy Whitcroft			$level--;
9778905a67cSAndy Whitcroft			$type = ($level != 0)? '{' : '';
9788905a67cSAndy Whitcroft
9798905a67cSAndy Whitcroft			if ($level == 0) {
980b998e001SPatrick Pannuto				if (substr($blk, $off + 1, 1) eq ';') {
981b998e001SPatrick Pannuto					$off++;
982b998e001SPatrick Pannuto				}
9838905a67cSAndy Whitcroft				last;
9848905a67cSAndy Whitcroft			}
9858905a67cSAndy Whitcroft		}
986f74bd194SAndy Whitcroft		# Preprocessor commands end at the newline unless escaped.
987f74bd194SAndy Whitcroft		if ($type eq '#' && $c eq "\n" && $p ne "\\") {
988f74bd194SAndy Whitcroft			$level--;
989f74bd194SAndy Whitcroft			$type = '';
990f74bd194SAndy Whitcroft			$off++;
991f74bd194SAndy Whitcroft			last;
992f74bd194SAndy Whitcroft		}
9938905a67cSAndy Whitcroft		$off++;
9948905a67cSAndy Whitcroft	}
995a3bb97a7SAndy Whitcroft	# We are truly at the end, so shuffle to the next line.
99613214adfSAndy Whitcroft	if ($off == $len) {
997a3bb97a7SAndy Whitcroft		$loff = $len + 1;
99813214adfSAndy Whitcroft		$line++;
99913214adfSAndy Whitcroft		$remain--;
100013214adfSAndy Whitcroft	}
10018905a67cSAndy Whitcroft
10028905a67cSAndy Whitcroft	my $statement = substr($blk, $soff, $off - $soff + 1);
10038905a67cSAndy Whitcroft	my $condition = substr($blk, $soff, $coff - $soff + 1);
10048905a67cSAndy Whitcroft
10058905a67cSAndy Whitcroft	#warn "STATEMENT<$statement>\n";
10068905a67cSAndy Whitcroft	#warn "CONDITION<$condition>\n";
10078905a67cSAndy Whitcroft
1008773647a0SAndy Whitcroft	#print "coff<$coff> soff<$off> loff<$loff>\n";
100913214adfSAndy Whitcroft
101013214adfSAndy Whitcroft	return ($statement, $condition,
101113214adfSAndy Whitcroft			$line, $remain + 1, $off - $loff + 1, $level);
101213214adfSAndy Whitcroft}
101313214adfSAndy Whitcroft
1014cf655043SAndy Whitcroftsub statement_lines {
1015cf655043SAndy Whitcroft	my ($stmt) = @_;
1016cf655043SAndy Whitcroft
1017cf655043SAndy Whitcroft	# Strip the diff line prefixes and rip blank lines at start and end.
1018cf655043SAndy Whitcroft	$stmt =~ s/(^|\n)./$1/g;
1019cf655043SAndy Whitcroft	$stmt =~ s/^\s*//;
1020cf655043SAndy Whitcroft	$stmt =~ s/\s*$//;
1021cf655043SAndy Whitcroft
1022cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
1023cf655043SAndy Whitcroft
1024cf655043SAndy Whitcroft	return $#stmt_lines + 2;
1025cf655043SAndy Whitcroft}
1026cf655043SAndy Whitcroft
1027cf655043SAndy Whitcroftsub statement_rawlines {
1028cf655043SAndy Whitcroft	my ($stmt) = @_;
1029cf655043SAndy Whitcroft
1030cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
1031cf655043SAndy Whitcroft
1032cf655043SAndy Whitcroft	return $#stmt_lines + 2;
1033cf655043SAndy Whitcroft}
1034cf655043SAndy Whitcroft
1035cf655043SAndy Whitcroftsub statement_block_size {
1036cf655043SAndy Whitcroft	my ($stmt) = @_;
1037cf655043SAndy Whitcroft
1038cf655043SAndy Whitcroft	$stmt =~ s/(^|\n)./$1/g;
1039cf655043SAndy Whitcroft	$stmt =~ s/^\s*{//;
1040cf655043SAndy Whitcroft	$stmt =~ s/}\s*$//;
1041cf655043SAndy Whitcroft	$stmt =~ s/^\s*//;
1042cf655043SAndy Whitcroft	$stmt =~ s/\s*$//;
1043cf655043SAndy Whitcroft
1044cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
1045cf655043SAndy Whitcroft	my @stmt_statements = ($stmt =~ /;/g);
1046cf655043SAndy Whitcroft
1047cf655043SAndy Whitcroft	my $stmt_lines = $#stmt_lines + 2;
1048cf655043SAndy Whitcroft	my $stmt_statements = $#stmt_statements + 1;
1049cf655043SAndy Whitcroft
1050cf655043SAndy Whitcroft	if ($stmt_lines > $stmt_statements) {
1051cf655043SAndy Whitcroft		return $stmt_lines;
1052cf655043SAndy Whitcroft	} else {
1053cf655043SAndy Whitcroft		return $stmt_statements;
1054cf655043SAndy Whitcroft	}
1055cf655043SAndy Whitcroft}
1056cf655043SAndy Whitcroft
105713214adfSAndy Whitcroftsub ctx_statement_full {
105813214adfSAndy Whitcroft	my ($linenr, $remain, $off) = @_;
105913214adfSAndy Whitcroft	my ($statement, $condition, $level);
106013214adfSAndy Whitcroft
106113214adfSAndy Whitcroft	my (@chunks);
106213214adfSAndy Whitcroft
1063cf655043SAndy Whitcroft	# Grab the first conditional/block pair.
106413214adfSAndy Whitcroft	($statement, $condition, $linenr, $remain, $off, $level) =
106513214adfSAndy Whitcroft				ctx_statement_block($linenr, $remain, $off);
1066773647a0SAndy Whitcroft	#print "F: c<$condition> s<$statement> remain<$remain>\n";
106713214adfSAndy Whitcroft	push(@chunks, [ $condition, $statement ]);
1068cf655043SAndy Whitcroft	if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
1069cf655043SAndy Whitcroft		return ($level, $linenr, @chunks);
1070cf655043SAndy Whitcroft	}
1071cf655043SAndy Whitcroft
1072cf655043SAndy Whitcroft	# Pull in the following conditional/block pairs and see if they
1073cf655043SAndy Whitcroft	# could continue the statement.
1074cf655043SAndy Whitcroft	for (;;) {
107513214adfSAndy Whitcroft		($statement, $condition, $linenr, $remain, $off, $level) =
107613214adfSAndy Whitcroft				ctx_statement_block($linenr, $remain, $off);
1077cf655043SAndy Whitcroft		#print "C: c<$condition> s<$statement> remain<$remain>\n";
1078773647a0SAndy Whitcroft		last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
1079cf655043SAndy Whitcroft		#print "C: push\n";
1080cf655043SAndy Whitcroft		push(@chunks, [ $condition, $statement ]);
108113214adfSAndy Whitcroft	}
108213214adfSAndy Whitcroft
108313214adfSAndy Whitcroft	return ($level, $linenr, @chunks);
10848905a67cSAndy Whitcroft}
10858905a67cSAndy Whitcroft
10864a0df2efSAndy Whitcroftsub ctx_block_get {
1087f0a594c1SAndy Whitcroft	my ($linenr, $remain, $outer, $open, $close, $off) = @_;
10884a0df2efSAndy Whitcroft	my $line;
10894a0df2efSAndy Whitcroft	my $start = $linenr - 1;
10904a0df2efSAndy Whitcroft	my $blk = '';
10914a0df2efSAndy Whitcroft	my @o;
10924a0df2efSAndy Whitcroft	my @c;
10934a0df2efSAndy Whitcroft	my @res = ();
10944a0df2efSAndy Whitcroft
1095f0a594c1SAndy Whitcroft	my $level = 0;
10964635f4fbSAndy Whitcroft	my @stack = ($level);
109700df344fSAndy Whitcroft	for ($line = $start; $remain > 0; $line++) {
109800df344fSAndy Whitcroft		next if ($rawlines[$line] =~ /^-/);
109900df344fSAndy Whitcroft		$remain--;
110000df344fSAndy Whitcroft
110100df344fSAndy Whitcroft		$blk .= $rawlines[$line];
11024635f4fbSAndy Whitcroft
11034635f4fbSAndy Whitcroft		# Handle nested #if/#else.
110401464f30SAndy Whitcroft		if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
11054635f4fbSAndy Whitcroft			push(@stack, $level);
110601464f30SAndy Whitcroft		} elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
11074635f4fbSAndy Whitcroft			$level = $stack[$#stack - 1];
110801464f30SAndy Whitcroft		} elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
11094635f4fbSAndy Whitcroft			$level = pop(@stack);
11104635f4fbSAndy Whitcroft		}
11114635f4fbSAndy Whitcroft
111201464f30SAndy Whitcroft		foreach my $c (split(//, $lines[$line])) {
1113f0a594c1SAndy Whitcroft			##print "C<$c>L<$level><$open$close>O<$off>\n";
1114f0a594c1SAndy Whitcroft			if ($off > 0) {
1115f0a594c1SAndy Whitcroft				$off--;
1116f0a594c1SAndy Whitcroft				next;
1117f0a594c1SAndy Whitcroft			}
11184a0df2efSAndy Whitcroft
1119f0a594c1SAndy Whitcroft			if ($c eq $close && $level > 0) {
1120f0a594c1SAndy Whitcroft				$level--;
1121f0a594c1SAndy Whitcroft				last if ($level == 0);
1122f0a594c1SAndy Whitcroft			} elsif ($c eq $open) {
1123f0a594c1SAndy Whitcroft				$level++;
1124f0a594c1SAndy Whitcroft			}
1125f0a594c1SAndy Whitcroft		}
11264a0df2efSAndy Whitcroft
1127f0a594c1SAndy Whitcroft		if (!$outer || $level <= 1) {
112800df344fSAndy Whitcroft			push(@res, $rawlines[$line]);
11294a0df2efSAndy Whitcroft		}
11304a0df2efSAndy Whitcroft
1131f0a594c1SAndy Whitcroft		last if ($level == 0);
11324a0df2efSAndy Whitcroft	}
11334a0df2efSAndy Whitcroft
1134f0a594c1SAndy Whitcroft	return ($level, @res);
11354a0df2efSAndy Whitcroft}
11364a0df2efSAndy Whitcroftsub ctx_block_outer {
11374a0df2efSAndy Whitcroft	my ($linenr, $remain) = @_;
11384a0df2efSAndy Whitcroft
1139f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
1140f0a594c1SAndy Whitcroft	return @r;
11414a0df2efSAndy Whitcroft}
11424a0df2efSAndy Whitcroftsub ctx_block {
11434a0df2efSAndy Whitcroft	my ($linenr, $remain) = @_;
11444a0df2efSAndy Whitcroft
1145f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1146f0a594c1SAndy Whitcroft	return @r;
1147653d4876SAndy Whitcroft}
1148653d4876SAndy Whitcroftsub ctx_statement {
1149f0a594c1SAndy Whitcroft	my ($linenr, $remain, $off) = @_;
1150f0a594c1SAndy Whitcroft
1151f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1152f0a594c1SAndy Whitcroft	return @r;
1153f0a594c1SAndy Whitcroft}
1154f0a594c1SAndy Whitcroftsub ctx_block_level {
1155653d4876SAndy Whitcroft	my ($linenr, $remain) = @_;
1156653d4876SAndy Whitcroft
1157f0a594c1SAndy Whitcroft	return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
11584a0df2efSAndy Whitcroft}
11599c0ca6f9SAndy Whitcroftsub ctx_statement_level {
11609c0ca6f9SAndy Whitcroft	my ($linenr, $remain, $off) = @_;
11619c0ca6f9SAndy Whitcroft
11629c0ca6f9SAndy Whitcroft	return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
11639c0ca6f9SAndy Whitcroft}
11644a0df2efSAndy Whitcroft
11654a0df2efSAndy Whitcroftsub ctx_locate_comment {
11664a0df2efSAndy Whitcroft	my ($first_line, $end_line) = @_;
11674a0df2efSAndy Whitcroft
11684a0df2efSAndy Whitcroft	# Catch a comment on the end of the line itself.
1169beae6332SAndy Whitcroft	my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
11704a0df2efSAndy Whitcroft	return $current_comment if (defined $current_comment);
11714a0df2efSAndy Whitcroft
11724a0df2efSAndy Whitcroft	# Look through the context and try and figure out if there is a
11734a0df2efSAndy Whitcroft	# comment.
11744a0df2efSAndy Whitcroft	my $in_comment = 0;
11754a0df2efSAndy Whitcroft	$current_comment = '';
11764a0df2efSAndy Whitcroft	for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
117700df344fSAndy Whitcroft		my $line = $rawlines[$linenr - 1];
117800df344fSAndy Whitcroft		#warn "           $line\n";
11794a0df2efSAndy Whitcroft		if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
11804a0df2efSAndy Whitcroft			$in_comment = 1;
11814a0df2efSAndy Whitcroft		}
11824a0df2efSAndy Whitcroft		if ($line =~ m@/\*@) {
11834a0df2efSAndy Whitcroft			$in_comment = 1;
11844a0df2efSAndy Whitcroft		}
11854a0df2efSAndy Whitcroft		if (!$in_comment && $current_comment ne '') {
11864a0df2efSAndy Whitcroft			$current_comment = '';
11874a0df2efSAndy Whitcroft		}
11884a0df2efSAndy Whitcroft		$current_comment .= $line . "\n" if ($in_comment);
11894a0df2efSAndy Whitcroft		if ($line =~ m@\*/@) {
11904a0df2efSAndy Whitcroft			$in_comment = 0;
11914a0df2efSAndy Whitcroft		}
11924a0df2efSAndy Whitcroft	}
11934a0df2efSAndy Whitcroft
11944a0df2efSAndy Whitcroft	chomp($current_comment);
11954a0df2efSAndy Whitcroft	return($current_comment);
11964a0df2efSAndy Whitcroft}
11974a0df2efSAndy Whitcroftsub ctx_has_comment {
11984a0df2efSAndy Whitcroft	my ($first_line, $end_line) = @_;
11994a0df2efSAndy Whitcroft	my $cmt = ctx_locate_comment($first_line, $end_line);
12004a0df2efSAndy Whitcroft
120100df344fSAndy Whitcroft	##print "LINE: $rawlines[$end_line - 1 ]\n";
12024a0df2efSAndy Whitcroft	##print "CMMT: $cmt\n";
12034a0df2efSAndy Whitcroft
12044a0df2efSAndy Whitcroft	return ($cmt ne '');
12054a0df2efSAndy Whitcroft}
12064a0df2efSAndy Whitcroft
12074d001e4dSAndy Whitcroftsub raw_line {
12084d001e4dSAndy Whitcroft	my ($linenr, $cnt) = @_;
12094d001e4dSAndy Whitcroft
12104d001e4dSAndy Whitcroft	my $offset = $linenr - 1;
12114d001e4dSAndy Whitcroft	$cnt++;
12124d001e4dSAndy Whitcroft
12134d001e4dSAndy Whitcroft	my $line;
12144d001e4dSAndy Whitcroft	while ($cnt) {
12154d001e4dSAndy Whitcroft		$line = $rawlines[$offset++];
12164d001e4dSAndy Whitcroft		next if (defined($line) && $line =~ /^-/);
12174d001e4dSAndy Whitcroft		$cnt--;
12184d001e4dSAndy Whitcroft	}
12194d001e4dSAndy Whitcroft
12204d001e4dSAndy Whitcroft	return $line;
12214d001e4dSAndy Whitcroft}
12224d001e4dSAndy Whitcroft
12230a920b5bSAndy Whitcroftsub cat_vet {
12240a920b5bSAndy Whitcroft	my ($vet) = @_;
12259c0ca6f9SAndy Whitcroft	my ($res, $coded);
12260a920b5bSAndy Whitcroft
12279c0ca6f9SAndy Whitcroft	$res = '';
12286c72ffaaSAndy Whitcroft	while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
12296c72ffaaSAndy Whitcroft		$res .= $1;
12306c72ffaaSAndy Whitcroft		if ($2 ne '') {
12319c0ca6f9SAndy Whitcroft			$coded = sprintf("^%c", unpack('C', $2) + 64);
12326c72ffaaSAndy Whitcroft			$res .= $coded;
12336c72ffaaSAndy Whitcroft		}
12349c0ca6f9SAndy Whitcroft	}
12359c0ca6f9SAndy Whitcroft	$res =~ s/$/\$/;
12360a920b5bSAndy Whitcroft
12379c0ca6f9SAndy Whitcroft	return $res;
12380a920b5bSAndy Whitcroft}
12390a920b5bSAndy Whitcroft
1240c2fdda0dSAndy Whitcroftmy $av_preprocessor = 0;
1241cf655043SAndy Whitcroftmy $av_pending;
1242c2fdda0dSAndy Whitcroftmy @av_paren_type;
12431f65f947SAndy Whitcroftmy $av_pend_colon;
1244c2fdda0dSAndy Whitcroft
1245c2fdda0dSAndy Whitcroftsub annotate_reset {
1246c2fdda0dSAndy Whitcroft	$av_preprocessor = 0;
1247cf655043SAndy Whitcroft	$av_pending = '_';
1248cf655043SAndy Whitcroft	@av_paren_type = ('E');
12491f65f947SAndy Whitcroft	$av_pend_colon = 'O';
1250c2fdda0dSAndy Whitcroft}
1251c2fdda0dSAndy Whitcroft
12526c72ffaaSAndy Whitcroftsub annotate_values {
12536c72ffaaSAndy Whitcroft	my ($stream, $type) = @_;
12546c72ffaaSAndy Whitcroft
12556c72ffaaSAndy Whitcroft	my $res;
12561f65f947SAndy Whitcroft	my $var = '_' x length($stream);
12576c72ffaaSAndy Whitcroft	my $cur = $stream;
12586c72ffaaSAndy Whitcroft
1259c2fdda0dSAndy Whitcroft	print "$stream\n" if ($dbg_values > 1);
12606c72ffaaSAndy Whitcroft
12616c72ffaaSAndy Whitcroft	while (length($cur)) {
1262773647a0SAndy Whitcroft		@av_paren_type = ('E') if ($#av_paren_type < 0);
1263cf655043SAndy Whitcroft		print " <" . join('', @av_paren_type) .
1264171ae1a4SAndy Whitcroft				"> <$type> <$av_pending>" if ($dbg_values > 1);
12656c72ffaaSAndy Whitcroft		if ($cur =~ /^(\s+)/o) {
1266c2fdda0dSAndy Whitcroft			print "WS($1)\n" if ($dbg_values > 1);
1267c2fdda0dSAndy Whitcroft			if ($1 =~ /\n/ && $av_preprocessor) {
1268cf655043SAndy Whitcroft				$type = pop(@av_paren_type);
1269c2fdda0dSAndy Whitcroft				$av_preprocessor = 0;
12706c72ffaaSAndy Whitcroft			}
12716c72ffaaSAndy Whitcroft
1272c023e473SFlorian Mickler		} elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
12739446ef56SAndy Whitcroft			print "CAST($1)\n" if ($dbg_values > 1);
12749446ef56SAndy Whitcroft			push(@av_paren_type, $type);
1275addcdceaSAndy Whitcroft			$type = 'c';
12769446ef56SAndy Whitcroft
1277e91b6e26SAndy Whitcroft		} elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
1278c2fdda0dSAndy Whitcroft			print "DECLARE($1)\n" if ($dbg_values > 1);
12796c72ffaaSAndy Whitcroft			$type = 'T';
12806c72ffaaSAndy Whitcroft
1281389a2fe5SAndy Whitcroft		} elsif ($cur =~ /^($Modifier)\s*/) {
1282389a2fe5SAndy Whitcroft			print "MODIFIER($1)\n" if ($dbg_values > 1);
1283389a2fe5SAndy Whitcroft			$type = 'T';
1284389a2fe5SAndy Whitcroft
1285c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
1286171ae1a4SAndy Whitcroft			print "DEFINE($1,$2)\n" if ($dbg_values > 1);
1287c2fdda0dSAndy Whitcroft			$av_preprocessor = 1;
1288171ae1a4SAndy Whitcroft			push(@av_paren_type, $type);
1289171ae1a4SAndy Whitcroft			if ($2 ne '') {
1290cf655043SAndy Whitcroft				$av_pending = 'N';
1291171ae1a4SAndy Whitcroft			}
1292171ae1a4SAndy Whitcroft			$type = 'E';
1293171ae1a4SAndy Whitcroft
1294c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
1295171ae1a4SAndy Whitcroft			print "UNDEF($1)\n" if ($dbg_values > 1);
1296171ae1a4SAndy Whitcroft			$av_preprocessor = 1;
1297171ae1a4SAndy Whitcroft			push(@av_paren_type, $type);
12986c72ffaaSAndy Whitcroft
1299c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
1300cf655043SAndy Whitcroft			print "PRE_START($1)\n" if ($dbg_values > 1);
1301c2fdda0dSAndy Whitcroft			$av_preprocessor = 1;
1302cf655043SAndy Whitcroft
1303cf655043SAndy Whitcroft			push(@av_paren_type, $type);
1304cf655043SAndy Whitcroft			push(@av_paren_type, $type);
1305171ae1a4SAndy Whitcroft			$type = 'E';
1306cf655043SAndy Whitcroft
1307c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
1308cf655043SAndy Whitcroft			print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1309cf655043SAndy Whitcroft			$av_preprocessor = 1;
1310cf655043SAndy Whitcroft
1311cf655043SAndy Whitcroft			push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1312cf655043SAndy Whitcroft
1313171ae1a4SAndy Whitcroft			$type = 'E';
1314cf655043SAndy Whitcroft
1315c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:endif))/o) {
1316cf655043SAndy Whitcroft			print "PRE_END($1)\n" if ($dbg_values > 1);
1317cf655043SAndy Whitcroft
1318cf655043SAndy Whitcroft			$av_preprocessor = 1;
1319cf655043SAndy Whitcroft
1320cf655043SAndy Whitcroft			# Assume all arms of the conditional end as this
1321cf655043SAndy Whitcroft			# one does, and continue as if the #endif was not here.
1322cf655043SAndy Whitcroft			pop(@av_paren_type);
1323cf655043SAndy Whitcroft			push(@av_paren_type, $type);
1324171ae1a4SAndy Whitcroft			$type = 'E';
13256c72ffaaSAndy Whitcroft
13266c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\\\n)/o) {
1327c2fdda0dSAndy Whitcroft			print "PRECONT($1)\n" if ($dbg_values > 1);
13286c72ffaaSAndy Whitcroft
1329171ae1a4SAndy Whitcroft		} elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1330171ae1a4SAndy Whitcroft			print "ATTR($1)\n" if ($dbg_values > 1);
1331171ae1a4SAndy Whitcroft			$av_pending = $type;
1332171ae1a4SAndy Whitcroft			$type = 'N';
1333171ae1a4SAndy Whitcroft
13346c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
1335c2fdda0dSAndy Whitcroft			print "SIZEOF($1)\n" if ($dbg_values > 1);
13366c72ffaaSAndy Whitcroft			if (defined $2) {
1337cf655043SAndy Whitcroft				$av_pending = 'V';
13386c72ffaaSAndy Whitcroft			}
13396c72ffaaSAndy Whitcroft			$type = 'N';
13406c72ffaaSAndy Whitcroft
134114b111c1SAndy Whitcroft		} elsif ($cur =~ /^(if|while|for)\b/o) {
1342c2fdda0dSAndy Whitcroft			print "COND($1)\n" if ($dbg_values > 1);
134314b111c1SAndy Whitcroft			$av_pending = 'E';
13446c72ffaaSAndy Whitcroft			$type = 'N';
13456c72ffaaSAndy Whitcroft
13461f65f947SAndy Whitcroft		} elsif ($cur =~/^(case)/o) {
13471f65f947SAndy Whitcroft			print "CASE($1)\n" if ($dbg_values > 1);
13481f65f947SAndy Whitcroft			$av_pend_colon = 'C';
13491f65f947SAndy Whitcroft			$type = 'N';
13501f65f947SAndy Whitcroft
135114b111c1SAndy Whitcroft		} elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
1352c2fdda0dSAndy Whitcroft			print "KEYWORD($1)\n" if ($dbg_values > 1);
13536c72ffaaSAndy Whitcroft			$type = 'N';
13546c72ffaaSAndy Whitcroft
13556c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\()/o) {
1356c2fdda0dSAndy Whitcroft			print "PAREN('$1')\n" if ($dbg_values > 1);
1357cf655043SAndy Whitcroft			push(@av_paren_type, $av_pending);
1358cf655043SAndy Whitcroft			$av_pending = '_';
13596c72ffaaSAndy Whitcroft			$type = 'N';
13606c72ffaaSAndy Whitcroft
13616c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\))/o) {
1362cf655043SAndy Whitcroft			my $new_type = pop(@av_paren_type);
1363cf655043SAndy Whitcroft			if ($new_type ne '_') {
1364cf655043SAndy Whitcroft				$type = $new_type;
1365c2fdda0dSAndy Whitcroft				print "PAREN('$1') -> $type\n"
1366c2fdda0dSAndy Whitcroft							if ($dbg_values > 1);
13676c72ffaaSAndy Whitcroft			} else {
1368c2fdda0dSAndy Whitcroft				print "PAREN('$1')\n" if ($dbg_values > 1);
13696c72ffaaSAndy Whitcroft			}
13706c72ffaaSAndy Whitcroft
1371c8cb2ca3SAndy Whitcroft		} elsif ($cur =~ /^($Ident)\s*\(/o) {
1372c2fdda0dSAndy Whitcroft			print "FUNC($1)\n" if ($dbg_values > 1);
1373c8cb2ca3SAndy Whitcroft			$type = 'V';
1374cf655043SAndy Whitcroft			$av_pending = 'V';
13756c72ffaaSAndy Whitcroft
13768e761b04SAndy Whitcroft		} elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
13778e761b04SAndy Whitcroft			if (defined $2 && $type eq 'C' || $type eq 'T') {
13781f65f947SAndy Whitcroft				$av_pend_colon = 'B';
13798e761b04SAndy Whitcroft			} elsif ($type eq 'E') {
13808e761b04SAndy Whitcroft				$av_pend_colon = 'L';
13811f65f947SAndy Whitcroft			}
13821f65f947SAndy Whitcroft			print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
13831f65f947SAndy Whitcroft			$type = 'V';
13841f65f947SAndy Whitcroft
13856c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Ident|$Constant)/o) {
1386c2fdda0dSAndy Whitcroft			print "IDENT($1)\n" if ($dbg_values > 1);
13876c72ffaaSAndy Whitcroft			$type = 'V';
13886c72ffaaSAndy Whitcroft
13896c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Assignment)/o) {
1390c2fdda0dSAndy Whitcroft			print "ASSIGN($1)\n" if ($dbg_values > 1);
13916c72ffaaSAndy Whitcroft			$type = 'N';
13926c72ffaaSAndy Whitcroft
1393cf655043SAndy Whitcroft		} elsif ($cur =~/^(;|{|})/) {
1394c2fdda0dSAndy Whitcroft			print "END($1)\n" if ($dbg_values > 1);
139513214adfSAndy Whitcroft			$type = 'E';
13961f65f947SAndy Whitcroft			$av_pend_colon = 'O';
139713214adfSAndy Whitcroft
13988e761b04SAndy Whitcroft		} elsif ($cur =~/^(,)/) {
13998e761b04SAndy Whitcroft			print "COMMA($1)\n" if ($dbg_values > 1);
14008e761b04SAndy Whitcroft			$type = 'C';
14018e761b04SAndy Whitcroft
14021f65f947SAndy Whitcroft		} elsif ($cur =~ /^(\?)/o) {
14031f65f947SAndy Whitcroft			print "QUESTION($1)\n" if ($dbg_values > 1);
14041f65f947SAndy Whitcroft			$type = 'N';
14051f65f947SAndy Whitcroft
14061f65f947SAndy Whitcroft		} elsif ($cur =~ /^(:)/o) {
14071f65f947SAndy Whitcroft			print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
14081f65f947SAndy Whitcroft
14091f65f947SAndy Whitcroft			substr($var, length($res), 1, $av_pend_colon);
14101f65f947SAndy Whitcroft			if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
14111f65f947SAndy Whitcroft				$type = 'E';
14121f65f947SAndy Whitcroft			} else {
14131f65f947SAndy Whitcroft				$type = 'N';
14141f65f947SAndy Whitcroft			}
14151f65f947SAndy Whitcroft			$av_pend_colon = 'O';
14161f65f947SAndy Whitcroft
14178e761b04SAndy Whitcroft		} elsif ($cur =~ /^(\[)/o) {
141813214adfSAndy Whitcroft			print "CLOSE($1)\n" if ($dbg_values > 1);
14196c72ffaaSAndy Whitcroft			$type = 'N';
14206c72ffaaSAndy Whitcroft
14210d413866SAndy Whitcroft		} elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
142274048ed8SAndy Whitcroft			my $variant;
142374048ed8SAndy Whitcroft
142474048ed8SAndy Whitcroft			print "OPV($1)\n" if ($dbg_values > 1);
142574048ed8SAndy Whitcroft			if ($type eq 'V') {
142674048ed8SAndy Whitcroft				$variant = 'B';
142774048ed8SAndy Whitcroft			} else {
142874048ed8SAndy Whitcroft				$variant = 'U';
142974048ed8SAndy Whitcroft			}
143074048ed8SAndy Whitcroft
143174048ed8SAndy Whitcroft			substr($var, length($res), 1, $variant);
143274048ed8SAndy Whitcroft			$type = 'N';
143374048ed8SAndy Whitcroft
14346c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Operators)/o) {
1435c2fdda0dSAndy Whitcroft			print "OP($1)\n" if ($dbg_values > 1);
14366c72ffaaSAndy Whitcroft			if ($1 ne '++' && $1 ne '--') {
14376c72ffaaSAndy Whitcroft				$type = 'N';
14386c72ffaaSAndy Whitcroft			}
14396c72ffaaSAndy Whitcroft
14406c72ffaaSAndy Whitcroft		} elsif ($cur =~ /(^.)/o) {
1441c2fdda0dSAndy Whitcroft			print "C($1)\n" if ($dbg_values > 1);
14426c72ffaaSAndy Whitcroft		}
14436c72ffaaSAndy Whitcroft		if (defined $1) {
14446c72ffaaSAndy Whitcroft			$cur = substr($cur, length($1));
14456c72ffaaSAndy Whitcroft			$res .= $type x length($1);
14466c72ffaaSAndy Whitcroft		}
14476c72ffaaSAndy Whitcroft	}
14486c72ffaaSAndy Whitcroft
14491f65f947SAndy Whitcroft	return ($res, $var);
14506c72ffaaSAndy Whitcroft}
14516c72ffaaSAndy Whitcroft
14528905a67cSAndy Whitcroftsub possible {
145313214adfSAndy Whitcroft	my ($possible, $line) = @_;
14549a974fdbSAndy Whitcroft	my $notPermitted = qr{(?:
14550776e594SAndy Whitcroft		^(?:
14560776e594SAndy Whitcroft			$Modifier|
14570776e594SAndy Whitcroft			$Storage|
14580776e594SAndy Whitcroft			$Type|
14599a974fdbSAndy Whitcroft			DEFINE_\S+
14609a974fdbSAndy Whitcroft		)$|
14619a974fdbSAndy Whitcroft		^(?:
14620776e594SAndy Whitcroft			goto|
14630776e594SAndy Whitcroft			return|
14640776e594SAndy Whitcroft			case|
14650776e594SAndy Whitcroft			else|
14660776e594SAndy Whitcroft			asm|__asm__|
146789a88353SAndy Whitcroft			do|
146889a88353SAndy Whitcroft			\#|
146989a88353SAndy Whitcroft			\#\#|
14709a974fdbSAndy Whitcroft		)(?:\s|$)|
14710776e594SAndy Whitcroft		^(?:typedef|struct|enum)\b
14729a974fdbSAndy Whitcroft	    )}x;
14739a974fdbSAndy Whitcroft	warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
14749a974fdbSAndy Whitcroft	if ($possible !~ $notPermitted) {
1475c45dcabdSAndy Whitcroft		# Check for modifiers.
1476c45dcabdSAndy Whitcroft		$possible =~ s/\s*$Storage\s*//g;
1477c45dcabdSAndy Whitcroft		$possible =~ s/\s*$Sparse\s*//g;
1478c45dcabdSAndy Whitcroft		if ($possible =~ /^\s*$/) {
1479c45dcabdSAndy Whitcroft
1480c45dcabdSAndy Whitcroft		} elsif ($possible =~ /\s/) {
1481c45dcabdSAndy Whitcroft			$possible =~ s/\s*$Type\s*//g;
1482d2506586SAndy Whitcroft			for my $modifier (split(' ', $possible)) {
14839a974fdbSAndy Whitcroft				if ($modifier !~ $notPermitted) {
1484d2506586SAndy Whitcroft					warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1485d2506586SAndy Whitcroft					push(@modifierList, $modifier);
1486d2506586SAndy Whitcroft				}
14879a974fdbSAndy Whitcroft			}
1488c45dcabdSAndy Whitcroft
1489c45dcabdSAndy Whitcroft		} else {
149013214adfSAndy Whitcroft			warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
14918905a67cSAndy Whitcroft			push(@typeList, $possible);
1492c45dcabdSAndy Whitcroft		}
14938905a67cSAndy Whitcroft		build_types();
14940776e594SAndy Whitcroft	} else {
14950776e594SAndy Whitcroft		warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
14968905a67cSAndy Whitcroft	}
14978905a67cSAndy Whitcroft}
14988905a67cSAndy Whitcroft
14996c72ffaaSAndy Whitcroftmy $prefix = '';
15006c72ffaaSAndy Whitcroft
1501000d1cc1SJoe Perchessub show_type {
1502cbec18afSJoe Perches	my ($type) = @_;
150391bfe484SJoe Perches
1504cbec18afSJoe Perches	return defined $use_type{$type} if (scalar keys %use_type > 0);
1505cbec18afSJoe Perches
1506cbec18afSJoe Perches	return !defined $ignore_type{$type};
1507000d1cc1SJoe Perches}
1508000d1cc1SJoe Perches
1509f0a594c1SAndy Whitcroftsub report {
1510cbec18afSJoe Perches	my ($level, $type, $msg) = @_;
1511cbec18afSJoe Perches
1512cbec18afSJoe Perches	if (!show_type($type) ||
1513cbec18afSJoe Perches	    (defined $tst_only && $msg !~ /\Q$tst_only\E/)) {
1514773647a0SAndy Whitcroft		return 0;
1515773647a0SAndy Whitcroft	}
1516000d1cc1SJoe Perches	my $line;
1517000d1cc1SJoe Perches	if ($show_types) {
1518cbec18afSJoe Perches		$line = "$prefix$level:$type: $msg\n";
1519000d1cc1SJoe Perches	} else {
1520cbec18afSJoe Perches		$line = "$prefix$level: $msg\n";
1521000d1cc1SJoe Perches	}
15228905a67cSAndy Whitcroft	$line = (split('\n', $line))[0] . "\n" if ($terse);
15238905a67cSAndy Whitcroft
152413214adfSAndy Whitcroft	push(our @report, $line);
1525773647a0SAndy Whitcroft
1526773647a0SAndy Whitcroft	return 1;
1527f0a594c1SAndy Whitcroft}
1528cbec18afSJoe Perches
1529f0a594c1SAndy Whitcroftsub report_dump {
153013214adfSAndy Whitcroft	our @report;
1531f0a594c1SAndy Whitcroft}
1532000d1cc1SJoe Perches
1533*d752fcc8SJoe Perchessub fixup_current_range {
1534*d752fcc8SJoe Perches	my ($lineRef, $offset, $length) = @_;
1535*d752fcc8SJoe Perches
1536*d752fcc8SJoe Perches	if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) {
1537*d752fcc8SJoe Perches		my $o = $1;
1538*d752fcc8SJoe Perches		my $l = $2;
1539*d752fcc8SJoe Perches		my $no = $o + $offset;
1540*d752fcc8SJoe Perches		my $nl = $l + $length;
1541*d752fcc8SJoe Perches		$$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/;
1542*d752fcc8SJoe Perches	}
1543*d752fcc8SJoe Perches}
1544*d752fcc8SJoe Perches
1545*d752fcc8SJoe Perchessub fix_inserted_deleted_lines {
1546*d752fcc8SJoe Perches	my ($linesRef, $insertedRef, $deletedRef) = @_;
1547*d752fcc8SJoe Perches
1548*d752fcc8SJoe Perches	my $range_last_linenr = 0;
1549*d752fcc8SJoe Perches	my $delta_offset = 0;
1550*d752fcc8SJoe Perches
1551*d752fcc8SJoe Perches	my $old_linenr = 0;
1552*d752fcc8SJoe Perches	my $new_linenr = 0;
1553*d752fcc8SJoe Perches
1554*d752fcc8SJoe Perches	my $next_insert = 0;
1555*d752fcc8SJoe Perches	my $next_delete = 0;
1556*d752fcc8SJoe Perches
1557*d752fcc8SJoe Perches	my @lines = ();
1558*d752fcc8SJoe Perches
1559*d752fcc8SJoe Perches	my $inserted = @{$insertedRef}[$next_insert++];
1560*d752fcc8SJoe Perches	my $deleted = @{$deletedRef}[$next_delete++];
1561*d752fcc8SJoe Perches
1562*d752fcc8SJoe Perches	foreach my $old_line (@{$linesRef}) {
1563*d752fcc8SJoe Perches		my $save_line = 1;
1564*d752fcc8SJoe Perches		my $line = $old_line;	#don't modify the array
1565*d752fcc8SJoe Perches		if ($line =~ /^(?:\+\+\+\|\-\-\-)\s+\S+/) {	#new filename
1566*d752fcc8SJoe Perches			$delta_offset = 0;
1567*d752fcc8SJoe Perches		} elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) {	#new hunk
1568*d752fcc8SJoe Perches			$range_last_linenr = $new_linenr;
1569*d752fcc8SJoe Perches			fixup_current_range(\$line, $delta_offset, 0);
1570*d752fcc8SJoe Perches		}
1571*d752fcc8SJoe Perches
1572*d752fcc8SJoe Perches		while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) {
1573*d752fcc8SJoe Perches			$deleted = @{$deletedRef}[$next_delete++];
1574*d752fcc8SJoe Perches			$save_line = 0;
1575*d752fcc8SJoe Perches			fixup_current_range(\$lines[$range_last_linenr], $delta_offset--, -1);
1576*d752fcc8SJoe Perches		}
1577*d752fcc8SJoe Perches
1578*d752fcc8SJoe Perches		while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) {
1579*d752fcc8SJoe Perches			push(@lines, ${$inserted}{'LINE'});
1580*d752fcc8SJoe Perches			$inserted = @{$insertedRef}[$next_insert++];
1581*d752fcc8SJoe Perches			$new_linenr++;
1582*d752fcc8SJoe Perches			fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1);
1583*d752fcc8SJoe Perches		}
1584*d752fcc8SJoe Perches
1585*d752fcc8SJoe Perches		if ($save_line) {
1586*d752fcc8SJoe Perches			push(@lines, $line);
1587*d752fcc8SJoe Perches			$new_linenr++;
1588*d752fcc8SJoe Perches		}
1589*d752fcc8SJoe Perches
1590*d752fcc8SJoe Perches		$old_linenr++;
1591*d752fcc8SJoe Perches	}
1592*d752fcc8SJoe Perches
1593*d752fcc8SJoe Perches	return @lines;
1594*d752fcc8SJoe Perches}
1595*d752fcc8SJoe Perches
1596de7d4f0eSAndy Whitcroftsub ERROR {
1597cbec18afSJoe Perches	my ($type, $msg) = @_;
1598cbec18afSJoe Perches
1599cbec18afSJoe Perches	if (report("ERROR", $type, $msg)) {
1600de7d4f0eSAndy Whitcroft		our $clean = 0;
16016c72ffaaSAndy Whitcroft		our $cnt_error++;
16023705ce5bSJoe Perches		return 1;
1603de7d4f0eSAndy Whitcroft	}
16043705ce5bSJoe Perches	return 0;
1605773647a0SAndy Whitcroft}
1606de7d4f0eSAndy Whitcroftsub WARN {
1607cbec18afSJoe Perches	my ($type, $msg) = @_;
1608cbec18afSJoe Perches
1609cbec18afSJoe Perches	if (report("WARNING", $type, $msg)) {
1610de7d4f0eSAndy Whitcroft		our $clean = 0;
16116c72ffaaSAndy Whitcroft		our $cnt_warn++;
16123705ce5bSJoe Perches		return 1;
1613de7d4f0eSAndy Whitcroft	}
16143705ce5bSJoe Perches	return 0;
1615773647a0SAndy Whitcroft}
1616de7d4f0eSAndy Whitcroftsub CHK {
1617cbec18afSJoe Perches	my ($type, $msg) = @_;
1618cbec18afSJoe Perches
1619cbec18afSJoe Perches	if ($check && report("CHECK", $type, $msg)) {
1620de7d4f0eSAndy Whitcroft		our $clean = 0;
16216c72ffaaSAndy Whitcroft		our $cnt_chk++;
16223705ce5bSJoe Perches		return 1;
16236c72ffaaSAndy Whitcroft	}
16243705ce5bSJoe Perches	return 0;
1625de7d4f0eSAndy Whitcroft}
1626de7d4f0eSAndy Whitcroft
16276ecd9674SAndy Whitcroftsub check_absolute_file {
16286ecd9674SAndy Whitcroft	my ($absolute, $herecurr) = @_;
16296ecd9674SAndy Whitcroft	my $file = $absolute;
16306ecd9674SAndy Whitcroft
16316ecd9674SAndy Whitcroft	##print "absolute<$absolute>\n";
16326ecd9674SAndy Whitcroft
16336ecd9674SAndy Whitcroft	# See if any suffix of this path is a path within the tree.
16346ecd9674SAndy Whitcroft	while ($file =~ s@^[^/]*/@@) {
16356ecd9674SAndy Whitcroft		if (-f "$root/$file") {
16366ecd9674SAndy Whitcroft			##print "file<$file>\n";
16376ecd9674SAndy Whitcroft			last;
16386ecd9674SAndy Whitcroft		}
16396ecd9674SAndy Whitcroft	}
16406ecd9674SAndy Whitcroft	if (! -f _)  {
16416ecd9674SAndy Whitcroft		return 0;
16426ecd9674SAndy Whitcroft	}
16436ecd9674SAndy Whitcroft
16446ecd9674SAndy Whitcroft	# It is, so see if the prefix is acceptable.
16456ecd9674SAndy Whitcroft	my $prefix = $absolute;
16466ecd9674SAndy Whitcroft	substr($prefix, -length($file)) = '';
16476ecd9674SAndy Whitcroft
16486ecd9674SAndy Whitcroft	##print "prefix<$prefix>\n";
16496ecd9674SAndy Whitcroft	if ($prefix ne ".../") {
1650000d1cc1SJoe Perches		WARN("USE_RELATIVE_PATH",
1651000d1cc1SJoe Perches		     "use relative pathname instead of absolute in changelog text\n" . $herecurr);
16526ecd9674SAndy Whitcroft	}
16536ecd9674SAndy Whitcroft}
16546ecd9674SAndy Whitcroft
16553705ce5bSJoe Perchessub trim {
16563705ce5bSJoe Perches	my ($string) = @_;
16573705ce5bSJoe Perches
1658b34c648bSJoe Perches	$string =~ s/^\s+|\s+$//g;
1659b34c648bSJoe Perches
1660b34c648bSJoe Perches	return $string;
1661b34c648bSJoe Perches}
1662b34c648bSJoe Perches
1663b34c648bSJoe Perchessub ltrim {
1664b34c648bSJoe Perches	my ($string) = @_;
1665b34c648bSJoe Perches
1666b34c648bSJoe Perches	$string =~ s/^\s+//;
1667b34c648bSJoe Perches
1668b34c648bSJoe Perches	return $string;
1669b34c648bSJoe Perches}
1670b34c648bSJoe Perches
1671b34c648bSJoe Perchessub rtrim {
1672b34c648bSJoe Perches	my ($string) = @_;
1673b34c648bSJoe Perches
1674b34c648bSJoe Perches	$string =~ s/\s+$//;
16753705ce5bSJoe Perches
16763705ce5bSJoe Perches	return $string;
16773705ce5bSJoe Perches}
16783705ce5bSJoe Perches
167952ea8506SJoe Perchessub string_find_replace {
168052ea8506SJoe Perches	my ($string, $find, $replace) = @_;
168152ea8506SJoe Perches
168252ea8506SJoe Perches	$string =~ s/$find/$replace/g;
168352ea8506SJoe Perches
168452ea8506SJoe Perches	return $string;
168552ea8506SJoe Perches}
168652ea8506SJoe Perches
16873705ce5bSJoe Perchessub tabify {
16883705ce5bSJoe Perches	my ($leading) = @_;
16893705ce5bSJoe Perches
16903705ce5bSJoe Perches	my $source_indent = 8;
16913705ce5bSJoe Perches	my $max_spaces_before_tab = $source_indent - 1;
16923705ce5bSJoe Perches	my $spaces_to_tab = " " x $source_indent;
16933705ce5bSJoe Perches
16943705ce5bSJoe Perches	#convert leading spaces to tabs
16953705ce5bSJoe Perches	1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
16963705ce5bSJoe Perches	#Remove spaces before a tab
16973705ce5bSJoe Perches	1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
16983705ce5bSJoe Perches
16993705ce5bSJoe Perches	return "$leading";
17003705ce5bSJoe Perches}
17013705ce5bSJoe Perches
1702d1fe9c09SJoe Perchessub pos_last_openparen {
1703d1fe9c09SJoe Perches	my ($line) = @_;
1704d1fe9c09SJoe Perches
1705d1fe9c09SJoe Perches	my $pos = 0;
1706d1fe9c09SJoe Perches
1707d1fe9c09SJoe Perches	my $opens = $line =~ tr/\(/\(/;
1708d1fe9c09SJoe Perches	my $closes = $line =~ tr/\)/\)/;
1709d1fe9c09SJoe Perches
1710d1fe9c09SJoe Perches	my $last_openparen = 0;
1711d1fe9c09SJoe Perches
1712d1fe9c09SJoe Perches	if (($opens == 0) || ($closes >= $opens)) {
1713d1fe9c09SJoe Perches		return -1;
1714d1fe9c09SJoe Perches	}
1715d1fe9c09SJoe Perches
1716d1fe9c09SJoe Perches	my $len = length($line);
1717d1fe9c09SJoe Perches
1718d1fe9c09SJoe Perches	for ($pos = 0; $pos < $len; $pos++) {
1719d1fe9c09SJoe Perches		my $string = substr($line, $pos);
1720d1fe9c09SJoe Perches		if ($string =~ /^($FuncArg|$balanced_parens)/) {
1721d1fe9c09SJoe Perches			$pos += length($1) - 1;
1722d1fe9c09SJoe Perches		} elsif (substr($line, $pos, 1) eq '(') {
1723d1fe9c09SJoe Perches			$last_openparen = $pos;
1724d1fe9c09SJoe Perches		} elsif (index($string, '(') == -1) {
1725d1fe9c09SJoe Perches			last;
1726d1fe9c09SJoe Perches		}
1727d1fe9c09SJoe Perches	}
1728d1fe9c09SJoe Perches
172991cb5195SJoe Perches	return length(expand_tabs(substr($line, 0, $last_openparen))) + 1;
1730d1fe9c09SJoe Perches}
1731d1fe9c09SJoe Perches
17320a920b5bSAndy Whitcroftsub process {
17330a920b5bSAndy Whitcroft	my $filename = shift;
17340a920b5bSAndy Whitcroft
17350a920b5bSAndy Whitcroft	my $linenr=0;
17360a920b5bSAndy Whitcroft	my $prevline="";
1737c2fdda0dSAndy Whitcroft	my $prevrawline="";
17380a920b5bSAndy Whitcroft	my $stashline="";
1739c2fdda0dSAndy Whitcroft	my $stashrawline="";
17400a920b5bSAndy Whitcroft
17414a0df2efSAndy Whitcroft	my $length;
17420a920b5bSAndy Whitcroft	my $indent;
17430a920b5bSAndy Whitcroft	my $previndent=0;
17440a920b5bSAndy Whitcroft	my $stashindent=0;
17450a920b5bSAndy Whitcroft
1746de7d4f0eSAndy Whitcroft	our $clean = 1;
17470a920b5bSAndy Whitcroft	my $signoff = 0;
17480a920b5bSAndy Whitcroft	my $is_patch = 0;
17490a920b5bSAndy Whitcroft
175029ee1b0cSJoe Perches	my $in_header_lines = $file ? 0 : 1;
175115662b3eSJoe Perches	my $in_commit_log = 0;		#Scanning lines before patch
175213f1937eSJoe Perches	my $reported_maintainer_file = 0;
1753fa64205dSPasi Savanainen	my $non_utf8_charset = 0;
1754fa64205dSPasi Savanainen
1755365dd4eaSJoe Perches	my $last_blank_line = 0;
1756365dd4eaSJoe Perches
175713214adfSAndy Whitcroft	our @report = ();
17586c72ffaaSAndy Whitcroft	our $cnt_lines = 0;
17596c72ffaaSAndy Whitcroft	our $cnt_error = 0;
17606c72ffaaSAndy Whitcroft	our $cnt_warn = 0;
17616c72ffaaSAndy Whitcroft	our $cnt_chk = 0;
17626c72ffaaSAndy Whitcroft
17630a920b5bSAndy Whitcroft	# Trace the real file/line as we go.
17640a920b5bSAndy Whitcroft	my $realfile = '';
17650a920b5bSAndy Whitcroft	my $realline = 0;
17660a920b5bSAndy Whitcroft	my $realcnt = 0;
17670a920b5bSAndy Whitcroft	my $here = '';
17680a920b5bSAndy Whitcroft	my $in_comment = 0;
1769c2fdda0dSAndy Whitcroft	my $comment_edge = 0;
17700a920b5bSAndy Whitcroft	my $first_line = 0;
17711e855726SWolfram Sang	my $p1_prefix = '';
17720a920b5bSAndy Whitcroft
177313214adfSAndy Whitcroft	my $prev_values = 'E';
177413214adfSAndy Whitcroft
177513214adfSAndy Whitcroft	# suppression flags
1776773647a0SAndy Whitcroft	my %suppress_ifbraces;
1777170d3a22SAndy Whitcroft	my %suppress_whiletrailers;
17782b474a1aSAndy Whitcroft	my %suppress_export;
17793e469cdcSAndy Whitcroft	my $suppress_statement = 0;
1780653d4876SAndy Whitcroft
17817e51f197SJoe Perches	my %signatures = ();
1782323c1260SJoe Perches
1783c2fdda0dSAndy Whitcroft	# Pre-scan the patch sanitizing the lines.
1784de7d4f0eSAndy Whitcroft	# Pre-scan the patch looking for any __setup documentation.
1785c2fdda0dSAndy Whitcroft	#
1786de7d4f0eSAndy Whitcroft	my @setup_docs = ();
1787de7d4f0eSAndy Whitcroft	my $setup_docs = 0;
1788773647a0SAndy Whitcroft
1789d8b07710SJoe Perches	my $camelcase_file_seeded = 0;
1790d8b07710SJoe Perches
1791773647a0SAndy Whitcroft	sanitise_line_reset();
1792c2fdda0dSAndy Whitcroft	my $line;
1793c2fdda0dSAndy Whitcroft	foreach my $rawline (@rawlines) {
1794773647a0SAndy Whitcroft		$linenr++;
1795773647a0SAndy Whitcroft		$line = $rawline;
1796c2fdda0dSAndy Whitcroft
17973705ce5bSJoe Perches		push(@fixed, $rawline) if ($fix);
17983705ce5bSJoe Perches
1799773647a0SAndy Whitcroft		if ($rawline=~/^\+\+\+\s+(\S+)/) {
1800de7d4f0eSAndy Whitcroft			$setup_docs = 0;
1801de7d4f0eSAndy Whitcroft			if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
1802de7d4f0eSAndy Whitcroft				$setup_docs = 1;
1803de7d4f0eSAndy Whitcroft			}
1804773647a0SAndy Whitcroft			#next;
1805de7d4f0eSAndy Whitcroft		}
1806773647a0SAndy Whitcroft		if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1807773647a0SAndy Whitcroft			$realline=$1-1;
1808773647a0SAndy Whitcroft			if (defined $2) {
1809773647a0SAndy Whitcroft				$realcnt=$3+1;
1810773647a0SAndy Whitcroft			} else {
1811773647a0SAndy Whitcroft				$realcnt=1+1;
1812773647a0SAndy Whitcroft			}
1813c45dcabdSAndy Whitcroft			$in_comment = 0;
1814773647a0SAndy Whitcroft
1815773647a0SAndy Whitcroft			# Guestimate if this is a continuing comment.  Run
1816773647a0SAndy Whitcroft			# the context looking for a comment "edge".  If this
1817773647a0SAndy Whitcroft			# edge is a close comment then we must be in a comment
1818773647a0SAndy Whitcroft			# at context start.
1819773647a0SAndy Whitcroft			my $edge;
182001fa9147SAndy Whitcroft			my $cnt = $realcnt;
182101fa9147SAndy Whitcroft			for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
182201fa9147SAndy Whitcroft				next if (defined $rawlines[$ln - 1] &&
182301fa9147SAndy Whitcroft					 $rawlines[$ln - 1] =~ /^-/);
182401fa9147SAndy Whitcroft				$cnt--;
182501fa9147SAndy Whitcroft				#print "RAW<$rawlines[$ln - 1]>\n";
1826721c1cb6SAndy Whitcroft				last if (!defined $rawlines[$ln - 1]);
1827fae17daeSAndy Whitcroft				if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
1828fae17daeSAndy Whitcroft				    $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
1829fae17daeSAndy Whitcroft					($edge) = $1;
1830fae17daeSAndy Whitcroft					last;
1831fae17daeSAndy Whitcroft				}
1832773647a0SAndy Whitcroft			}
1833773647a0SAndy Whitcroft			if (defined $edge && $edge eq '*/') {
1834773647a0SAndy Whitcroft				$in_comment = 1;
1835773647a0SAndy Whitcroft			}
1836773647a0SAndy Whitcroft
1837773647a0SAndy Whitcroft			# Guestimate if this is a continuing comment.  If this
1838773647a0SAndy Whitcroft			# is the start of a diff block and this line starts
1839773647a0SAndy Whitcroft			# ' *' then it is very likely a comment.
1840773647a0SAndy Whitcroft			if (!defined $edge &&
184183242e0cSAndy Whitcroft			    $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
1842773647a0SAndy Whitcroft			{
1843773647a0SAndy Whitcroft				$in_comment = 1;
1844773647a0SAndy Whitcroft			}
1845773647a0SAndy Whitcroft
1846773647a0SAndy Whitcroft			##print "COMMENT:$in_comment edge<$edge> $rawline\n";
1847773647a0SAndy Whitcroft			sanitise_line_reset($in_comment);
1848773647a0SAndy Whitcroft
1849171ae1a4SAndy Whitcroft		} elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
1850773647a0SAndy Whitcroft			# Standardise the strings and chars within the input to
1851171ae1a4SAndy Whitcroft			# simplify matching -- only bother with positive lines.
1852773647a0SAndy Whitcroft			$line = sanitise_line($rawline);
1853773647a0SAndy Whitcroft		}
1854773647a0SAndy Whitcroft		push(@lines, $line);
1855773647a0SAndy Whitcroft
1856773647a0SAndy Whitcroft		if ($realcnt > 1) {
1857773647a0SAndy Whitcroft			$realcnt-- if ($line =~ /^(?:\+| |$)/);
1858773647a0SAndy Whitcroft		} else {
1859773647a0SAndy Whitcroft			$realcnt = 0;
1860773647a0SAndy Whitcroft		}
1861773647a0SAndy Whitcroft
1862773647a0SAndy Whitcroft		#print "==>$rawline\n";
1863773647a0SAndy Whitcroft		#print "-->$line\n";
1864de7d4f0eSAndy Whitcroft
1865de7d4f0eSAndy Whitcroft		if ($setup_docs && $line =~ /^\+/) {
1866de7d4f0eSAndy Whitcroft			push(@setup_docs, $line);
1867de7d4f0eSAndy Whitcroft		}
1868de7d4f0eSAndy Whitcroft	}
1869de7d4f0eSAndy Whitcroft
18706c72ffaaSAndy Whitcroft	$prefix = '';
18716c72ffaaSAndy Whitcroft
1872773647a0SAndy Whitcroft	$realcnt = 0;
1873773647a0SAndy Whitcroft	$linenr = 0;
1874194f66fcSJoe Perches	$fixlinenr = -1;
18750a920b5bSAndy Whitcroft	foreach my $line (@lines) {
18760a920b5bSAndy Whitcroft		$linenr++;
1877194f66fcSJoe Perches		$fixlinenr++;
18781b5539b1SJoe Perches		my $sline = $line;	#copy of $line
18791b5539b1SJoe Perches		$sline =~ s/$;/ /g;	#with comments as spaces
18800a920b5bSAndy Whitcroft
1881c2fdda0dSAndy Whitcroft		my $rawline = $rawlines[$linenr - 1];
18826c72ffaaSAndy Whitcroft
18830a920b5bSAndy Whitcroft#extract the line range in the file after the patch is applied
18846c72ffaaSAndy Whitcroft		if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
18850a920b5bSAndy Whitcroft			$is_patch = 1;
18864a0df2efSAndy Whitcroft			$first_line = $linenr + 1;
18870a920b5bSAndy Whitcroft			$realline=$1-1;
18880a920b5bSAndy Whitcroft			if (defined $2) {
18890a920b5bSAndy Whitcroft				$realcnt=$3+1;
18900a920b5bSAndy Whitcroft			} else {
18910a920b5bSAndy Whitcroft				$realcnt=1+1;
18920a920b5bSAndy Whitcroft			}
1893c2fdda0dSAndy Whitcroft			annotate_reset();
189413214adfSAndy Whitcroft			$prev_values = 'E';
189513214adfSAndy Whitcroft
1896773647a0SAndy Whitcroft			%suppress_ifbraces = ();
1897170d3a22SAndy Whitcroft			%suppress_whiletrailers = ();
18982b474a1aSAndy Whitcroft			%suppress_export = ();
18993e469cdcSAndy Whitcroft			$suppress_statement = 0;
19000a920b5bSAndy Whitcroft			next;
19010a920b5bSAndy Whitcroft
19024a0df2efSAndy Whitcroft# track the line number as we move through the hunk, note that
19034a0df2efSAndy Whitcroft# new versions of GNU diff omit the leading space on completely
19044a0df2efSAndy Whitcroft# blank context lines so we need to count that too.
1905773647a0SAndy Whitcroft		} elsif ($line =~ /^( |\+|$)/) {
19060a920b5bSAndy Whitcroft			$realline++;
1907d8aaf121SAndy Whitcroft			$realcnt-- if ($realcnt != 0);
19080a920b5bSAndy Whitcroft
19094a0df2efSAndy Whitcroft			# Measure the line length and indent.
1910c2fdda0dSAndy Whitcroft			($length, $indent) = line_stats($rawline);
19110a920b5bSAndy Whitcroft
19120a920b5bSAndy Whitcroft			# Track the previous line.
19130a920b5bSAndy Whitcroft			($prevline, $stashline) = ($stashline, $line);
19140a920b5bSAndy Whitcroft			($previndent, $stashindent) = ($stashindent, $indent);
1915c2fdda0dSAndy Whitcroft			($prevrawline, $stashrawline) = ($stashrawline, $rawline);
1916c2fdda0dSAndy Whitcroft
1917773647a0SAndy Whitcroft			#warn "line<$line>\n";
19186c72ffaaSAndy Whitcroft
1919d8aaf121SAndy Whitcroft		} elsif ($realcnt == 1) {
1920d8aaf121SAndy Whitcroft			$realcnt--;
19210a920b5bSAndy Whitcroft		}
19220a920b5bSAndy Whitcroft
1923cc77cdcaSAndy Whitcroft		my $hunk_line = ($realcnt != 0);
1924cc77cdcaSAndy Whitcroft
19250a920b5bSAndy Whitcroft#make up the handle for any error we report on this line
1926773647a0SAndy Whitcroft		$prefix = "$filename:$realline: " if ($emacs && $file);
1927773647a0SAndy Whitcroft		$prefix = "$filename:$linenr: " if ($emacs && !$file);
1928773647a0SAndy Whitcroft
19296c72ffaaSAndy Whitcroft		$here = "#$linenr: " if (!$file);
19306c72ffaaSAndy Whitcroft		$here = "#$realline: " if ($file);
1931773647a0SAndy Whitcroft
19322ac73b4fSJoe Perches		my $found_file = 0;
1933773647a0SAndy Whitcroft		# extract the filename as it passes
19343bf9a009SRabin Vincent		if ($line =~ /^diff --git.*?(\S+)$/) {
19353bf9a009SRabin Vincent			$realfile = $1;
19362b7ab453SJoe Perches			$realfile =~ s@^([^/]*)/@@ if (!$file);
1937270c49a0SJoe Perches			$in_commit_log = 0;
19382ac73b4fSJoe Perches			$found_file = 1;
19393bf9a009SRabin Vincent		} elsif ($line =~ /^\+\+\+\s+(\S+)/) {
1940773647a0SAndy Whitcroft			$realfile = $1;
19412b7ab453SJoe Perches			$realfile =~ s@^([^/]*)/@@ if (!$file);
1942270c49a0SJoe Perches			$in_commit_log = 0;
19431e855726SWolfram Sang
19441e855726SWolfram Sang			$p1_prefix = $1;
1945e2f7aa4bSAndy Whitcroft			if (!$file && $tree && $p1_prefix ne '' &&
1946e2f7aa4bSAndy Whitcroft			    -e "$root/$p1_prefix") {
1947000d1cc1SJoe Perches				WARN("PATCH_PREFIX",
1948000d1cc1SJoe Perches				     "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
19491e855726SWolfram Sang			}
1950773647a0SAndy Whitcroft
1951c1ab3326SAndy Whitcroft			if ($realfile =~ m@^include/asm/@) {
1952000d1cc1SJoe Perches				ERROR("MODIFIED_INCLUDE_ASM",
1953000d1cc1SJoe Perches				      "do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
1954773647a0SAndy Whitcroft			}
19552ac73b4fSJoe Perches			$found_file = 1;
19562ac73b4fSJoe Perches		}
19572ac73b4fSJoe Perches
19582ac73b4fSJoe Perches		if ($found_file) {
19592ac73b4fSJoe Perches			if ($realfile =~ m@^(drivers/net/|net/)@) {
19602ac73b4fSJoe Perches				$check = 1;
19612ac73b4fSJoe Perches			} else {
19622ac73b4fSJoe Perches				$check = $check_orig;
19632ac73b4fSJoe Perches			}
1964773647a0SAndy Whitcroft			next;
1965773647a0SAndy Whitcroft		}
1966773647a0SAndy Whitcroft
1967389834b6SRandy Dunlap		$here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
19680a920b5bSAndy Whitcroft
1969c2fdda0dSAndy Whitcroft		my $hereline = "$here\n$rawline\n";
1970c2fdda0dSAndy Whitcroft		my $herecurr = "$here\n$rawline\n";
1971c2fdda0dSAndy Whitcroft		my $hereprev = "$here\n$prevrawline\n$rawline\n";
19720a920b5bSAndy Whitcroft
19736c72ffaaSAndy Whitcroft		$cnt_lines++ if ($realcnt != 0);
19746c72ffaaSAndy Whitcroft
19753bf9a009SRabin Vincent# Check for incorrect file permissions
19763bf9a009SRabin Vincent		if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
19773bf9a009SRabin Vincent			my $permhere = $here . "FILE: $realfile\n";
197804db4d25SJoe Perches			if ($realfile !~ m@scripts/@ &&
197904db4d25SJoe Perches			    $realfile !~ /\.(py|pl|awk|sh)$/) {
1980000d1cc1SJoe Perches				ERROR("EXECUTE_PERMISSIONS",
1981000d1cc1SJoe Perches				      "do not set execute permissions for source files\n" . $permhere);
19823bf9a009SRabin Vincent			}
19833bf9a009SRabin Vincent		}
19843bf9a009SRabin Vincent
198520112475SJoe Perches# Check the patch for a signoff:
1986d8aaf121SAndy Whitcroft		if ($line =~ /^\s*signed-off-by:/i) {
19874a0df2efSAndy Whitcroft			$signoff++;
198815662b3eSJoe Perches			$in_commit_log = 0;
19890a920b5bSAndy Whitcroft		}
199020112475SJoe Perches
199120112475SJoe Perches# Check signature styles
1992270c49a0SJoe Perches		if (!$in_header_lines &&
1993ce0338dfSJoe Perches		    $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
199420112475SJoe Perches			my $space_before = $1;
199520112475SJoe Perches			my $sign_off = $2;
199620112475SJoe Perches			my $space_after = $3;
199720112475SJoe Perches			my $email = $4;
199820112475SJoe Perches			my $ucfirst_sign_off = ucfirst(lc($sign_off));
199920112475SJoe Perches
2000ce0338dfSJoe Perches			if ($sign_off !~ /$signature_tags/) {
2001ce0338dfSJoe Perches				WARN("BAD_SIGN_OFF",
2002ce0338dfSJoe Perches				     "Non-standard signature: $sign_off\n" . $herecurr);
2003ce0338dfSJoe Perches			}
200420112475SJoe Perches			if (defined $space_before && $space_before ne "") {
20053705ce5bSJoe Perches				if (WARN("BAD_SIGN_OFF",
20063705ce5bSJoe Perches					 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
20073705ce5bSJoe Perches				    $fix) {
2008194f66fcSJoe Perches					$fixed[$fixlinenr] =
20093705ce5bSJoe Perches					    "$ucfirst_sign_off $email";
20103705ce5bSJoe Perches				}
201120112475SJoe Perches			}
201220112475SJoe Perches			if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
20133705ce5bSJoe Perches				if (WARN("BAD_SIGN_OFF",
20143705ce5bSJoe Perches					 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
20153705ce5bSJoe Perches				    $fix) {
2016194f66fcSJoe Perches					$fixed[$fixlinenr] =
20173705ce5bSJoe Perches					    "$ucfirst_sign_off $email";
20183705ce5bSJoe Perches				}
20193705ce5bSJoe Perches
202020112475SJoe Perches			}
202120112475SJoe Perches			if (!defined $space_after || $space_after ne " ") {
20223705ce5bSJoe Perches				if (WARN("BAD_SIGN_OFF",
20233705ce5bSJoe Perches					 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
20243705ce5bSJoe Perches				    $fix) {
2025194f66fcSJoe Perches					$fixed[$fixlinenr] =
20263705ce5bSJoe Perches					    "$ucfirst_sign_off $email";
20273705ce5bSJoe Perches				}
202820112475SJoe Perches			}
202920112475SJoe Perches
203020112475SJoe Perches			my ($email_name, $email_address, $comment) = parse_email($email);
203120112475SJoe Perches			my $suggested_email = format_email(($email_name, $email_address));
203220112475SJoe Perches			if ($suggested_email eq "") {
2033000d1cc1SJoe Perches				ERROR("BAD_SIGN_OFF",
2034000d1cc1SJoe Perches				      "Unrecognized email address: '$email'\n" . $herecurr);
203520112475SJoe Perches			} else {
203620112475SJoe Perches				my $dequoted = $suggested_email;
203720112475SJoe Perches				$dequoted =~ s/^"//;
203820112475SJoe Perches				$dequoted =~ s/" </ </;
203920112475SJoe Perches				# Don't force email to have quotes
204020112475SJoe Perches				# Allow just an angle bracketed address
204120112475SJoe Perches				if ("$dequoted$comment" ne $email &&
204220112475SJoe Perches				    "<$email_address>$comment" ne $email &&
204320112475SJoe Perches				    "$suggested_email$comment" ne $email) {
2044000d1cc1SJoe Perches					WARN("BAD_SIGN_OFF",
2045000d1cc1SJoe Perches					     "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
204620112475SJoe Perches				}
20470a920b5bSAndy Whitcroft			}
20487e51f197SJoe Perches
20497e51f197SJoe Perches# Check for duplicate signatures
20507e51f197SJoe Perches			my $sig_nospace = $line;
20517e51f197SJoe Perches			$sig_nospace =~ s/\s//g;
20527e51f197SJoe Perches			$sig_nospace = lc($sig_nospace);
20537e51f197SJoe Perches			if (defined $signatures{$sig_nospace}) {
20547e51f197SJoe Perches				WARN("BAD_SIGN_OFF",
20557e51f197SJoe Perches				     "Duplicate signature\n" . $herecurr);
20567e51f197SJoe Perches			} else {
20577e51f197SJoe Perches				$signatures{$sig_nospace} = 1;
20587e51f197SJoe Perches			}
20590a920b5bSAndy Whitcroft		}
20600a920b5bSAndy Whitcroft
20619b3189ebSJoe Perches# Check for old stable address
20629b3189ebSJoe Perches		if ($line =~ /^\s*cc:\s*.*<?\bstable\@kernel\.org\b>?.*$/i) {
20639b3189ebSJoe Perches			ERROR("STABLE_ADDRESS",
20649b3189ebSJoe Perches			      "The 'stable' address should be 'stable\@vger.kernel.org'\n" . $herecurr);
20659b3189ebSJoe Perches		}
20669b3189ebSJoe Perches
20677ebd05efSChristopher Covington# Check for unwanted Gerrit info
20687ebd05efSChristopher Covington		if ($in_commit_log && $line =~ /^\s*change-id:/i) {
20697ebd05efSChristopher Covington			ERROR("GERRIT_CHANGE_ID",
20707ebd05efSChristopher Covington			      "Remove Gerrit Change-Id's before submitting upstream.\n" . $herecurr);
20717ebd05efSChristopher Covington		}
20727ebd05efSChristopher Covington
2073d311cd44SJoe Perches# Check for improperly formed commit descriptions
2074d311cd44SJoe Perches		if ($in_commit_log &&
2075d311cd44SJoe Perches		    $line =~ /\bcommit\s+[0-9a-f]{5,}/i &&
2076d311cd44SJoe Perches		    $line !~ /\b[Cc]ommit [0-9a-f]{12,16} \("/) {
2077d311cd44SJoe Perches			$line =~ /\b(c)ommit\s+([0-9a-f]{5,})/i;
2078d311cd44SJoe Perches			my $init_char = $1;
2079d311cd44SJoe Perches			my $orig_commit = lc($2);
2080d311cd44SJoe Perches			my $id = '01234567890ab';
2081d311cd44SJoe Perches			my $desc = 'commit description';
2082d311cd44SJoe Perches		        ($id, $desc) = git_commit_info($orig_commit, $id, $desc);
2083d311cd44SJoe Perches			ERROR("GIT_COMMIT_ID",
2084d311cd44SJoe Perches			      "Please use 12 to 16 chars for the git commit ID like: '${init_char}ommit $id (\"$desc\")'\n" . $herecurr);
2085d311cd44SJoe Perches		}
2086d311cd44SJoe Perches
208713f1937eSJoe Perches# Check for added, moved or deleted files
208813f1937eSJoe Perches		if (!$reported_maintainer_file && !$in_commit_log &&
208913f1937eSJoe Perches		    ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ ||
209013f1937eSJoe Perches		     $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ ||
209113f1937eSJoe Perches		     ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ &&
209213f1937eSJoe Perches		      (defined($1) || defined($2))))) {
209313f1937eSJoe Perches			$reported_maintainer_file = 1;
209413f1937eSJoe Perches			WARN("FILE_PATH_CHANGES",
209513f1937eSJoe Perches			     "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr);
209613f1937eSJoe Perches		}
209713f1937eSJoe Perches
209800df344fSAndy Whitcroft# Check for wrappage within a valid hunk of the file
20998905a67cSAndy Whitcroft		if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
2100000d1cc1SJoe Perches			ERROR("CORRUPTED_PATCH",
2101000d1cc1SJoe Perches			      "patch seems to be corrupt (line wrapped?)\n" .
21026c72ffaaSAndy Whitcroft				$herecurr) if (!$emitted_corrupt++);
2103de7d4f0eSAndy Whitcroft		}
2104de7d4f0eSAndy Whitcroft
21056ecd9674SAndy Whitcroft# Check for absolute kernel paths.
21066ecd9674SAndy Whitcroft		if ($tree) {
21076ecd9674SAndy Whitcroft			while ($line =~ m{(?:^|\s)(/\S*)}g) {
21086ecd9674SAndy Whitcroft				my $file = $1;
21096ecd9674SAndy Whitcroft
21106ecd9674SAndy Whitcroft				if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
21116ecd9674SAndy Whitcroft				    check_absolute_file($1, $herecurr)) {
21126ecd9674SAndy Whitcroft					#
21136ecd9674SAndy Whitcroft				} else {
21146ecd9674SAndy Whitcroft					check_absolute_file($file, $herecurr);
21156ecd9674SAndy Whitcroft				}
21166ecd9674SAndy Whitcroft			}
21176ecd9674SAndy Whitcroft		}
21186ecd9674SAndy Whitcroft
2119de7d4f0eSAndy Whitcroft# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
2120de7d4f0eSAndy Whitcroft		if (($realfile =~ /^$/ || $line =~ /^\+/) &&
2121171ae1a4SAndy Whitcroft		    $rawline !~ m/^$UTF8*$/) {
2122171ae1a4SAndy Whitcroft			my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
2123171ae1a4SAndy Whitcroft
2124171ae1a4SAndy Whitcroft			my $blank = copy_spacing($rawline);
2125171ae1a4SAndy Whitcroft			my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
2126171ae1a4SAndy Whitcroft			my $hereptr = "$hereline$ptr\n";
2127171ae1a4SAndy Whitcroft
212834d99219SJoe Perches			CHK("INVALID_UTF8",
2129000d1cc1SJoe Perches			    "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
213000df344fSAndy Whitcroft		}
21310a920b5bSAndy Whitcroft
213215662b3eSJoe Perches# Check if it's the start of a commit log
213315662b3eSJoe Perches# (not a header line and we haven't seen the patch filename)
213415662b3eSJoe Perches		if ($in_header_lines && $realfile =~ /^$/ &&
213529ee1b0cSJoe Perches		    !($rawline =~ /^\s+\S/ ||
213629ee1b0cSJoe Perches		      $rawline =~ /^(commit\b|from\b|[\w-]+:).*$/i)) {
213715662b3eSJoe Perches			$in_header_lines = 0;
213815662b3eSJoe Perches			$in_commit_log = 1;
213915662b3eSJoe Perches		}
214015662b3eSJoe Perches
2141fa64205dSPasi Savanainen# Check if there is UTF-8 in a commit log when a mail header has explicitly
2142fa64205dSPasi Savanainen# declined it, i.e defined some charset where it is missing.
2143fa64205dSPasi Savanainen		if ($in_header_lines &&
2144fa64205dSPasi Savanainen		    $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
2145fa64205dSPasi Savanainen		    $1 !~ /utf-8/i) {
2146fa64205dSPasi Savanainen			$non_utf8_charset = 1;
2147fa64205dSPasi Savanainen		}
2148fa64205dSPasi Savanainen
2149fa64205dSPasi Savanainen		if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
215015662b3eSJoe Perches		    $rawline =~ /$NON_ASCII_UTF8/) {
2151fa64205dSPasi Savanainen			WARN("UTF8_BEFORE_PATCH",
215215662b3eSJoe Perches			    "8-bit UTF-8 used in possible commit log\n" . $herecurr);
215315662b3eSJoe Perches		}
215415662b3eSJoe Perches
215530670854SAndy Whitcroft# ignore non-hunk lines and lines being removed
215630670854SAndy Whitcroft		next if (!$hunk_line || $line =~ /^-/);
215700df344fSAndy Whitcroft
21580a920b5bSAndy Whitcroft#trailing whitespace
21599c0ca6f9SAndy Whitcroft		if ($line =~ /^\+.*\015/) {
2160c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2161d5e616fcSJoe Perches			if (ERROR("DOS_LINE_ENDINGS",
2162d5e616fcSJoe Perches				  "DOS line endings\n" . $herevet) &&
2163d5e616fcSJoe Perches			    $fix) {
2164194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/[\s\015]+$//;
2165d5e616fcSJoe Perches			}
2166c2fdda0dSAndy Whitcroft		} elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
2167c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
21683705ce5bSJoe Perches			if (ERROR("TRAILING_WHITESPACE",
21693705ce5bSJoe Perches				  "trailing whitespace\n" . $herevet) &&
21703705ce5bSJoe Perches			    $fix) {
2171194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\s+$//;
21723705ce5bSJoe Perches			}
21733705ce5bSJoe Perches
2174d2c0a235SAndy Whitcroft			$rpt_cleaners = 1;
21750a920b5bSAndy Whitcroft		}
21765368df20SAndy Whitcroft
21774783f894SJosh Triplett# Check for FSF mailing addresses.
2178109d8cb2SAlexander Duyck		if ($rawline =~ /\bwrite to the Free/i ||
21793e2232f2SJoe Perches		    $rawline =~ /\b59\s+Temple\s+Pl/i ||
21803e2232f2SJoe Perches		    $rawline =~ /\b51\s+Franklin\s+St/i) {
21814783f894SJosh Triplett			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
21824783f894SJosh Triplett			my $msg_type = \&ERROR;
21834783f894SJosh Triplett			$msg_type = \&CHK if ($file);
21844783f894SJosh Triplett			&{$msg_type}("FSF_MAILING_ADDRESS",
21854783f894SJosh 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)
21864783f894SJosh Triplett		}
21874783f894SJosh Triplett
21883354957aSAndi Kleen# check for Kconfig help text having a real description
21899fe287d7SAndy Whitcroft# Only applies when adding the entry originally, after that we do not have
21909fe287d7SAndy Whitcroft# sufficient context to determine whether it is indeed long enough.
21913354957aSAndi Kleen		if ($realfile =~ /Kconfig/ &&
21928d73e0e7SJoe Perches		    $line =~ /^\+\s*config\s+/) {
21933354957aSAndi Kleen			my $length = 0;
21949fe287d7SAndy Whitcroft			my $cnt = $realcnt;
21959fe287d7SAndy Whitcroft			my $ln = $linenr + 1;
21969fe287d7SAndy Whitcroft			my $f;
2197a1385803SAndy Whitcroft			my $is_start = 0;
21989fe287d7SAndy Whitcroft			my $is_end = 0;
2199a1385803SAndy Whitcroft			for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
22009fe287d7SAndy Whitcroft				$f = $lines[$ln - 1];
22019fe287d7SAndy Whitcroft				$cnt-- if ($lines[$ln - 1] !~ /^-/);
22029fe287d7SAndy Whitcroft				$is_end = $lines[$ln - 1] =~ /^\+/;
22039fe287d7SAndy Whitcroft
22049fe287d7SAndy Whitcroft				next if ($f =~ /^-/);
22058d73e0e7SJoe Perches				last if (!$file && $f =~ /^\@\@/);
2206a1385803SAndy Whitcroft
22078d73e0e7SJoe Perches				if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate)\s*\"/) {
2208a1385803SAndy Whitcroft					$is_start = 1;
22098d73e0e7SJoe Perches				} elsif ($lines[$ln - 1] =~ /^\+\s*(?:---)?help(?:---)?$/) {
2210a1385803SAndy Whitcroft					$length = -1;
2211a1385803SAndy Whitcroft				}
2212a1385803SAndy Whitcroft
22139fe287d7SAndy Whitcroft				$f =~ s/^.//;
22143354957aSAndi Kleen				$f =~ s/#.*//;
22153354957aSAndi Kleen				$f =~ s/^\s+//;
22163354957aSAndi Kleen				next if ($f =~ /^$/);
22179fe287d7SAndy Whitcroft				if ($f =~ /^\s*config\s/) {
22189fe287d7SAndy Whitcroft					$is_end = 1;
22199fe287d7SAndy Whitcroft					last;
22209fe287d7SAndy Whitcroft				}
22213354957aSAndi Kleen				$length++;
22223354957aSAndi Kleen			}
2223000d1cc1SJoe Perches			WARN("CONFIG_DESCRIPTION",
2224a1385803SAndy Whitcroft			     "please write a paragraph that describes the config symbol fully\n" . $herecurr) if ($is_start && $is_end && $length < 4);
2225a1385803SAndy Whitcroft			#print "is_start<$is_start> is_end<$is_end> length<$length>\n";
22263354957aSAndi Kleen		}
22273354957aSAndi Kleen
22281ba8dfd1SKees Cook# discourage the addition of CONFIG_EXPERIMENTAL in Kconfig.
22291ba8dfd1SKees Cook		if ($realfile =~ /Kconfig/ &&
22301ba8dfd1SKees Cook		    $line =~ /.\s*depends on\s+.*\bEXPERIMENTAL\b/) {
22311ba8dfd1SKees Cook			WARN("CONFIG_EXPERIMENTAL",
22321ba8dfd1SKees Cook			     "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
22331ba8dfd1SKees Cook		}
22341ba8dfd1SKees Cook
2235c68e5878SArnaud Lacombe		if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
2236c68e5878SArnaud Lacombe		    ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
2237c68e5878SArnaud Lacombe			my $flag = $1;
2238c68e5878SArnaud Lacombe			my $replacement = {
2239c68e5878SArnaud Lacombe				'EXTRA_AFLAGS' =>   'asflags-y',
2240c68e5878SArnaud Lacombe				'EXTRA_CFLAGS' =>   'ccflags-y',
2241c68e5878SArnaud Lacombe				'EXTRA_CPPFLAGS' => 'cppflags-y',
2242c68e5878SArnaud Lacombe				'EXTRA_LDFLAGS' =>  'ldflags-y',
2243c68e5878SArnaud Lacombe			};
2244c68e5878SArnaud Lacombe
2245c68e5878SArnaud Lacombe			WARN("DEPRECATED_VARIABLE",
2246c68e5878SArnaud Lacombe			     "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
2247c68e5878SArnaud Lacombe		}
2248c68e5878SArnaud Lacombe
2249bff5da43SRob Herring# check for DT compatible documentation
22507dd05b38SFlorian Vaussard		if (defined $root &&
22517dd05b38SFlorian Vaussard			(($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) ||
22527dd05b38SFlorian Vaussard			 ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) {
22537dd05b38SFlorian Vaussard
2254bff5da43SRob Herring			my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g;
2255bff5da43SRob Herring
2256cc93319bSFlorian Vaussard			my $dt_path = $root . "/Documentation/devicetree/bindings/";
2257cc93319bSFlorian Vaussard			my $vp_file = $dt_path . "vendor-prefixes.txt";
2258cc93319bSFlorian Vaussard
2259bff5da43SRob Herring			foreach my $compat (@compats) {
2260bff5da43SRob Herring				my $compat2 = $compat;
2261185d566bSRob Herring				$compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/;
2262185d566bSRob Herring				my $compat3 = $compat;
2263185d566bSRob Herring				$compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/;
2264185d566bSRob Herring				`grep -Erq "$compat|$compat2|$compat3" $dt_path`;
2265bff5da43SRob Herring				if ( $? >> 8 ) {
2266bff5da43SRob Herring					WARN("UNDOCUMENTED_DT_STRING",
2267bff5da43SRob Herring					     "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr);
2268bff5da43SRob Herring				}
2269bff5da43SRob Herring
22704fbf32a6SFlorian Vaussard				next if $compat !~ /^([a-zA-Z0-9\-]+)\,/;
22714fbf32a6SFlorian Vaussard				my $vendor = $1;
2272cc93319bSFlorian Vaussard				`grep -Eq "^$vendor\\b" $vp_file`;
2273bff5da43SRob Herring				if ( $? >> 8 ) {
2274bff5da43SRob Herring					WARN("UNDOCUMENTED_DT_STRING",
2275cc93319bSFlorian Vaussard					     "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr);
2276bff5da43SRob Herring				}
2277bff5da43SRob Herring			}
2278bff5da43SRob Herring		}
2279bff5da43SRob Herring
22805368df20SAndy Whitcroft# check we are in a valid source file if not then ignore this hunk
22815368df20SAndy Whitcroft		next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
22825368df20SAndy Whitcroft
22836cd7f386SJoe Perches#line length limit
2284c45dcabdSAndy Whitcroft		if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ &&
2285f4c014c0SAndy Whitcroft		    $rawline !~ /^.\s*\*\s*\@$Ident\s/ &&
22860fccc622SJoe Perches		    !($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(KERN_\S+\s*|[^"]*))?"[X\t]*"\s*(?:|,|\)\s*;)\s*$/ ||
22878bbea968SJoe Perches		    $line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) &&
22886cd7f386SJoe Perches		    $length > $max_line_length)
2289c45dcabdSAndy Whitcroft		{
2290000d1cc1SJoe Perches			WARN("LONG_LINE",
22916cd7f386SJoe Perches			     "line over $max_line_length characters\n" . $herecurr);
22920a920b5bSAndy Whitcroft		}
22930a920b5bSAndy Whitcroft
2294ca56dc09SJosh Triplett# Check for user-visible strings broken across lines, which breaks the ability
22958c5fcd24SJoe Perches# to grep for the string.  Make exceptions when the previous string ends in a
22968c5fcd24SJoe Perches# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{'
22978c5fcd24SJoe Perches# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value
2298ca56dc09SJosh Triplett		if ($line =~ /^\+\s*"/ &&
2299ca56dc09SJosh Triplett		    $prevline =~ /"\s*$/ &&
23008c5fcd24SJoe Perches		    $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) {
2301ca56dc09SJosh Triplett			WARN("SPLIT_STRING",
2302ca56dc09SJosh Triplett			     "quoted string split across lines\n" . $hereprev);
2303ca56dc09SJosh Triplett		}
2304ca56dc09SJosh Triplett
23055e79d96eSJoe Perches# check for spaces before a quoted newline
23065e79d96eSJoe Perches		if ($rawline =~ /^.*\".*\s\\n/) {
23073705ce5bSJoe Perches			if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
23083705ce5bSJoe Perches				 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
23093705ce5bSJoe Perches			    $fix) {
2310194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
23113705ce5bSJoe Perches			}
23123705ce5bSJoe Perches
23135e79d96eSJoe Perches		}
23145e79d96eSJoe Perches
23158905a67cSAndy Whitcroft# check for adding lines without a newline.
23168905a67cSAndy Whitcroft		if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
2317000d1cc1SJoe Perches			WARN("MISSING_EOF_NEWLINE",
2318000d1cc1SJoe Perches			     "adding a line without newline at end of file\n" . $herecurr);
23198905a67cSAndy Whitcroft		}
23208905a67cSAndy Whitcroft
232142e41c54SMike Frysinger# Blackfin: use hi/lo macros
232242e41c54SMike Frysinger		if ($realfile =~ m@arch/blackfin/.*\.S$@) {
232342e41c54SMike Frysinger			if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
232442e41c54SMike Frysinger				my $herevet = "$here\n" . cat_vet($line) . "\n";
2325000d1cc1SJoe Perches				ERROR("LO_MACRO",
2326000d1cc1SJoe Perches				      "use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
232742e41c54SMike Frysinger			}
232842e41c54SMike Frysinger			if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
232942e41c54SMike Frysinger				my $herevet = "$here\n" . cat_vet($line) . "\n";
2330000d1cc1SJoe Perches				ERROR("HI_MACRO",
2331000d1cc1SJoe Perches				      "use the HI() macro, not (... >> 16)\n" . $herevet);
233242e41c54SMike Frysinger			}
233342e41c54SMike Frysinger		}
233442e41c54SMike Frysinger
2335b9ea10d6SAndy Whitcroft# check we are in a valid source file C or perl if not then ignore this hunk
2336b9ea10d6SAndy Whitcroft		next if ($realfile !~ /\.(h|c|pl)$/);
23370a920b5bSAndy Whitcroft
23380a920b5bSAndy Whitcroft# at the beginning of a line any tabs must come first and anything
23390a920b5bSAndy Whitcroft# more than 8 must use tabs.
2340c2fdda0dSAndy Whitcroft		if ($rawline =~ /^\+\s* \t\s*\S/ ||
2341c2fdda0dSAndy Whitcroft		    $rawline =~ /^\+\s*        \s*/) {
2342c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2343d2c0a235SAndy Whitcroft			$rpt_cleaners = 1;
23443705ce5bSJoe Perches			if (ERROR("CODE_INDENT",
23453705ce5bSJoe Perches				  "code indent should use tabs where possible\n" . $herevet) &&
23463705ce5bSJoe Perches			    $fix) {
2347194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
23483705ce5bSJoe Perches			}
23490a920b5bSAndy Whitcroft		}
23500a920b5bSAndy Whitcroft
235108e44365SAlberto Panizzo# check for space before tabs.
235208e44365SAlberto Panizzo		if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
235308e44365SAlberto Panizzo			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
23543705ce5bSJoe Perches			if (WARN("SPACE_BEFORE_TAB",
23553705ce5bSJoe Perches				"please, no space before tabs\n" . $herevet) &&
23563705ce5bSJoe Perches			    $fix) {
2357194f66fcSJoe Perches				while ($fixed[$fixlinenr] =~
2358c76f4cb3SJoe Perches					   s/(^\+.*) {8,8}+\t/$1\t\t/) {}
2359194f66fcSJoe Perches				while ($fixed[$fixlinenr] =~
2360c76f4cb3SJoe Perches					   s/(^\+.*) +\t/$1\t/) {}
23613705ce5bSJoe Perches			}
236208e44365SAlberto Panizzo		}
236308e44365SAlberto Panizzo
2364d1fe9c09SJoe Perches# check for && or || at the start of a line
2365d1fe9c09SJoe Perches		if ($rawline =~ /^\+\s*(&&|\|\|)/) {
2366d1fe9c09SJoe Perches			CHK("LOGICAL_CONTINUATIONS",
2367d1fe9c09SJoe Perches			    "Logical continuations should be on the previous line\n" . $hereprev);
2368d1fe9c09SJoe Perches		}
2369d1fe9c09SJoe Perches
2370d1fe9c09SJoe Perches# check multi-line statement indentation matches previous line
2371d1fe9c09SJoe Perches		if ($^V && $^V ge 5.10.0 &&
237291cb5195SJoe Perches		    $prevline =~ /^\+([ \t]*)((?:$c90_Keywords(?:\s+if)\s*)|(?:$Declare\s*)?(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*|$Ident\s*=\s*$Ident\s*)\(.*(\&\&|\|\||,)\s*$/) {
2373d1fe9c09SJoe Perches			$prevline =~ /^\+(\t*)(.*)$/;
2374d1fe9c09SJoe Perches			my $oldindent = $1;
2375d1fe9c09SJoe Perches			my $rest = $2;
2376d1fe9c09SJoe Perches
2377d1fe9c09SJoe Perches			my $pos = pos_last_openparen($rest);
2378d1fe9c09SJoe Perches			if ($pos >= 0) {
2379b34a26f3SJoe Perches				$line =~ /^(\+| )([ \t]*)/;
2380b34a26f3SJoe Perches				my $newindent = $2;
2381d1fe9c09SJoe Perches
2382d1fe9c09SJoe Perches				my $goodtabindent = $oldindent .
2383d1fe9c09SJoe Perches					"\t" x ($pos / 8) .
2384d1fe9c09SJoe Perches					" "  x ($pos % 8);
2385d1fe9c09SJoe Perches				my $goodspaceindent = $oldindent . " "  x $pos;
2386d1fe9c09SJoe Perches
2387d1fe9c09SJoe Perches				if ($newindent ne $goodtabindent &&
2388d1fe9c09SJoe Perches				    $newindent ne $goodspaceindent) {
23893705ce5bSJoe Perches
23903705ce5bSJoe Perches					if (CHK("PARENTHESIS_ALIGNMENT",
23913705ce5bSJoe Perches						"Alignment should match open parenthesis\n" . $hereprev) &&
23923705ce5bSJoe Perches					    $fix && $line =~ /^\+/) {
2393194f66fcSJoe Perches						$fixed[$fixlinenr] =~
23943705ce5bSJoe Perches						    s/^\+[ \t]*/\+$goodtabindent/;
23953705ce5bSJoe Perches					}
2396d1fe9c09SJoe Perches				}
2397d1fe9c09SJoe Perches			}
2398d1fe9c09SJoe Perches		}
2399d1fe9c09SJoe Perches
2400f27c95dbSJoe Perches		if ($line =~ /^\+.*\(\s*$Type\s*\)[ \t]+(?!$Assignment|$Arithmetic)/) {
24013705ce5bSJoe Perches			if (CHK("SPACING",
2402f27c95dbSJoe Perches				"No space is necessary after a cast\n" . $herecurr) &&
24033705ce5bSJoe Perches			    $fix) {
2404194f66fcSJoe Perches				$fixed[$fixlinenr] =~
2405f27c95dbSJoe Perches				    s/(\(\s*$Type\s*\))[ \t]+/$1/;
24063705ce5bSJoe Perches			}
2407aad4f614SJoe Perches		}
2408aad4f614SJoe Perches
240905880600SJoe Perches		if ($realfile =~ m@^(drivers/net/|net/)@ &&
2410fdb4bcd6SJoe Perches		    $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
241185ad978cSJoe Perches		    $rawline =~ /^\+[ \t]*\*/ &&
241285ad978cSJoe Perches		    $realline > 2) {
241305880600SJoe Perches			WARN("NETWORKING_BLOCK_COMMENT_STYLE",
241405880600SJoe Perches			     "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
241505880600SJoe Perches		}
241605880600SJoe Perches
241705880600SJoe Perches		if ($realfile =~ m@^(drivers/net/|net/)@ &&
2418a605e32eSJoe Perches		    $prevrawline =~ /^\+[ \t]*\/\*/ &&		#starting /*
2419a605e32eSJoe Perches		    $prevrawline !~ /\*\/[ \t]*$/ &&		#no trailing */
242061135e96SJoe Perches		    $rawline =~ /^\+/ &&			#line is new
2421a605e32eSJoe Perches		    $rawline !~ /^\+[ \t]*\*/) {		#no leading *
2422a605e32eSJoe Perches			WARN("NETWORKING_BLOCK_COMMENT_STYLE",
2423a605e32eSJoe Perches			     "networking block comments start with * on subsequent lines\n" . $hereprev);
2424a605e32eSJoe Perches		}
2425a605e32eSJoe Perches
2426a605e32eSJoe Perches		if ($realfile =~ m@^(drivers/net/|net/)@ &&
2427c24f9f19SJoe Perches		    $rawline !~ m@^\+[ \t]*\*/[ \t]*$@ &&	#trailing */
2428c24f9f19SJoe Perches		    $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ &&	#inline /*...*/
2429c24f9f19SJoe Perches		    $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ &&	#trailing **/
2430c24f9f19SJoe Perches		    $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) {	#non blank */
243105880600SJoe Perches			WARN("NETWORKING_BLOCK_COMMENT_STYLE",
243205880600SJoe Perches			     "networking block comments put the trailing */ on a separate line\n" . $herecurr);
243305880600SJoe Perches		}
243405880600SJoe Perches
24357f619191SJoe Perches# check for missing blank lines after struct/union declarations
24367f619191SJoe Perches# with exceptions for various attributes and macros
24377f619191SJoe Perches		if ($prevline =~ /^[\+ ]};?\s*$/ &&
24387f619191SJoe Perches		    $line =~ /^\+/ &&
24397f619191SJoe Perches		    !($line =~ /^\+\s*$/ ||
24407f619191SJoe Perches		      $line =~ /^\+\s*EXPORT_SYMBOL/ ||
24417f619191SJoe Perches		      $line =~ /^\+\s*MODULE_/i ||
24427f619191SJoe Perches		      $line =~ /^\+\s*\#\s*(?:end|elif|else)/ ||
24437f619191SJoe Perches		      $line =~ /^\+[a-z_]*init/ ||
24447f619191SJoe Perches		      $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ ||
24457f619191SJoe Perches		      $line =~ /^\+\s*DECLARE/ ||
24467f619191SJoe Perches		      $line =~ /^\+\s*__setup/)) {
2447*d752fcc8SJoe Perches			if (CHK("LINE_SPACING",
2448*d752fcc8SJoe Perches				"Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) &&
2449*d752fcc8SJoe Perches			    $fix) {
2450*d752fcc8SJoe Perches			my $inserted = {
2451*d752fcc8SJoe Perches				LINENR => $fixlinenr,
2452*d752fcc8SJoe Perches				LINE => "\+",
2453*d752fcc8SJoe Perches			};
2454*d752fcc8SJoe Perches			push(@fixed_inserted, $inserted);
2455*d752fcc8SJoe Perches			}
24567f619191SJoe Perches		}
24577f619191SJoe Perches
2458365dd4eaSJoe Perches# check for multiple consecutive blank lines
2459365dd4eaSJoe Perches		if ($prevline =~ /^[\+ ]\s*$/ &&
2460365dd4eaSJoe Perches		    $line =~ /^\+\s*$/ &&
2461365dd4eaSJoe Perches		    $last_blank_line != ($linenr - 1)) {
2462*d752fcc8SJoe Perches			if (CHK("LINE_SPACING",
2463*d752fcc8SJoe Perches				"Please don't use multiple blank lines\n" . $hereprev) &&
2464*d752fcc8SJoe Perches			    $fix) {
2465*d752fcc8SJoe Perches			my $deleted = {
2466*d752fcc8SJoe Perches				LINENR => $fixlinenr,
2467*d752fcc8SJoe Perches				LINE => $rawline,
2468*d752fcc8SJoe Perches			};
2469*d752fcc8SJoe Perches			push(@fixed_deleted, $deleted);
2470*d752fcc8SJoe Perches			}
2471*d752fcc8SJoe Perches
2472365dd4eaSJoe Perches			$last_blank_line = $linenr;
2473365dd4eaSJoe Perches		}
2474365dd4eaSJoe Perches
24753b617e3bSJoe Perches# check for missing blank lines after declarations
24763f7bac03SJoe Perches		if ($sline =~ /^\+\s+\S/ &&			#Not at char 1
24773f7bac03SJoe Perches			# actual declarations
24783f7bac03SJoe Perches		    ($prevline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
24795a4e1fd3SJoe Perches			# function pointer declarations
24805a4e1fd3SJoe Perches		     $prevline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
24813f7bac03SJoe Perches			# foo bar; where foo is some local typedef or #define
24823f7bac03SJoe Perches		     $prevline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
24833f7bac03SJoe Perches			# known declaration macros
24843f7bac03SJoe Perches		     $prevline =~ /^\+\s+$declaration_macros/) &&
24853f7bac03SJoe Perches			# for "else if" which can look like "$Ident $Ident"
24863f7bac03SJoe Perches		    !($prevline =~ /^\+\s+$c90_Keywords\b/ ||
24873f7bac03SJoe Perches			# other possible extensions of declaration lines
24883f7bac03SJoe Perches		      $prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ ||
24893f7bac03SJoe Perches			# not starting a section or a macro "\" extended line
24903f7bac03SJoe Perches		      $prevline =~ /(?:\{\s*|\\)$/) &&
24913f7bac03SJoe Perches			# looks like a declaration
24923f7bac03SJoe Perches		    !($sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
24935a4e1fd3SJoe Perches			# function pointer declarations
24945a4e1fd3SJoe Perches		      $sline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
24953f7bac03SJoe Perches			# foo bar; where foo is some local typedef or #define
24963f7bac03SJoe Perches		      $sline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
24973f7bac03SJoe Perches			# known declaration macros
24983f7bac03SJoe Perches		      $sline =~ /^\+\s+$declaration_macros/ ||
24993f7bac03SJoe Perches			# start of struct or union or enum
25003b617e3bSJoe Perches		      $sline =~ /^\+\s+(?:union|struct|enum|typedef)\b/ ||
25013f7bac03SJoe Perches			# start or end of block or continuation of declaration
25023f7bac03SJoe Perches		      $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ ||
25033f7bac03SJoe Perches			# bitfield continuation
25043f7bac03SJoe Perches		      $sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ ||
25053f7bac03SJoe Perches			# other possible extensions of declaration lines
25063f7bac03SJoe Perches		      $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) &&
25073f7bac03SJoe Perches			# indentation of previous and current line are the same
25083f7bac03SJoe Perches		    (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) {
2509*d752fcc8SJoe Perches			if (WARN("LINE_SPACING",
2510*d752fcc8SJoe Perches				 "Missing a blank line after declarations\n" . $hereprev) &&
2511*d752fcc8SJoe Perches			    $fix) {
2512*d752fcc8SJoe Perches			my $inserted = {
2513*d752fcc8SJoe Perches				LINENR => $fixlinenr,
2514*d752fcc8SJoe Perches				LINE => "\+",
2515*d752fcc8SJoe Perches			};
2516*d752fcc8SJoe Perches			push(@fixed_inserted, $inserted);
2517*d752fcc8SJoe Perches			}
25183b617e3bSJoe Perches		}
25193b617e3bSJoe Perches
25205f7ddae6SRaffaele Recalcati# check for spaces at the beginning of a line.
25216b4c5bebSAndy Whitcroft# Exceptions:
25226b4c5bebSAndy Whitcroft#  1) within comments
25236b4c5bebSAndy Whitcroft#  2) indented preprocessor commands
25246b4c5bebSAndy Whitcroft#  3) hanging labels
25253705ce5bSJoe Perches		if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/)  {
25265f7ddae6SRaffaele Recalcati			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
25273705ce5bSJoe Perches			if (WARN("LEADING_SPACE",
25283705ce5bSJoe Perches				 "please, no spaces at the start of a line\n" . $herevet) &&
25293705ce5bSJoe Perches			    $fix) {
2530194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
25313705ce5bSJoe Perches			}
25325f7ddae6SRaffaele Recalcati		}
25335f7ddae6SRaffaele Recalcati
2534b9ea10d6SAndy Whitcroft# check we are in a valid C source file if not then ignore this hunk
2535b9ea10d6SAndy Whitcroft		next if ($realfile !~ /\.(h|c)$/);
2536b9ea10d6SAndy Whitcroft
2537032a4c0fSJoe Perches# check indentation of any line with a bare else
2538032a4c0fSJoe Perches# if the previous line is a break or return and is indented 1 tab more...
2539032a4c0fSJoe Perches		if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {
2540032a4c0fSJoe Perches			my $tabs = length($1) + 1;
2541032a4c0fSJoe Perches			if ($prevline =~ /^\+\t{$tabs,$tabs}(?:break|return)\b/) {
2542032a4c0fSJoe Perches				WARN("UNNECESSARY_ELSE",
2543032a4c0fSJoe Perches				     "else is not generally useful after a break or return\n" . $hereprev);
2544032a4c0fSJoe Perches			}
2545032a4c0fSJoe Perches		}
2546032a4c0fSJoe Perches
2547c00df19aSJoe Perches# check indentation of a line with a break;
2548c00df19aSJoe Perches# if the previous line is a goto or return and is indented the same # of tabs
2549c00df19aSJoe Perches		if ($sline =~ /^\+([\t]+)break\s*;\s*$/) {
2550c00df19aSJoe Perches			my $tabs = $1;
2551c00df19aSJoe Perches			if ($prevline =~ /^\+$tabs(?:goto|return)\b/) {
2552c00df19aSJoe Perches				WARN("UNNECESSARY_BREAK",
2553c00df19aSJoe Perches				     "break is not useful after a goto or return\n" . $hereprev);
2554c00df19aSJoe Perches			}
2555c00df19aSJoe Perches		}
2556c00df19aSJoe Perches
25571ba8dfd1SKees Cook# discourage the addition of CONFIG_EXPERIMENTAL in #if(def).
25581ba8dfd1SKees Cook		if ($line =~ /^\+\s*\#\s*if.*\bCONFIG_EXPERIMENTAL\b/) {
25591ba8dfd1SKees Cook			WARN("CONFIG_EXPERIMENTAL",
25601ba8dfd1SKees Cook			     "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
25611ba8dfd1SKees Cook		}
25621ba8dfd1SKees Cook
2563c2fdda0dSAndy Whitcroft# check for RCS/CVS revision markers
2564cf655043SAndy Whitcroft		if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
2565000d1cc1SJoe Perches			WARN("CVS_KEYWORD",
2566000d1cc1SJoe Perches			     "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
2567c2fdda0dSAndy Whitcroft		}
256822f2a2efSAndy Whitcroft
256942e41c54SMike Frysinger# Blackfin: don't use __builtin_bfin_[cs]sync
257042e41c54SMike Frysinger		if ($line =~ /__builtin_bfin_csync/) {
257142e41c54SMike Frysinger			my $herevet = "$here\n" . cat_vet($line) . "\n";
2572000d1cc1SJoe Perches			ERROR("CSYNC",
2573000d1cc1SJoe Perches			      "use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
257442e41c54SMike Frysinger		}
257542e41c54SMike Frysinger		if ($line =~ /__builtin_bfin_ssync/) {
257642e41c54SMike Frysinger			my $herevet = "$here\n" . cat_vet($line) . "\n";
2577000d1cc1SJoe Perches			ERROR("SSYNC",
2578000d1cc1SJoe Perches			      "use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
257942e41c54SMike Frysinger		}
258042e41c54SMike Frysinger
258156e77d70SJoe Perches# check for old HOTPLUG __dev<foo> section markings
258256e77d70SJoe Perches		if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
258356e77d70SJoe Perches			WARN("HOTPLUG_SECTION",
258456e77d70SJoe Perches			     "Using $1 is unnecessary\n" . $herecurr);
258556e77d70SJoe Perches		}
258656e77d70SJoe Perches
25879c0ca6f9SAndy Whitcroft# Check for potential 'bare' types
25882b474a1aSAndy Whitcroft		my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
25892b474a1aSAndy Whitcroft		    $realline_next);
25903e469cdcSAndy Whitcroft#print "LINE<$line>\n";
25913e469cdcSAndy Whitcroft		if ($linenr >= $suppress_statement &&
25921b5539b1SJoe Perches		    $realcnt && $sline =~ /.\s*\S/) {
2593170d3a22SAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
2594f5fe35ddSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0);
2595171ae1a4SAndy Whitcroft			$stat =~ s/\n./\n /g;
2596171ae1a4SAndy Whitcroft			$cond =~ s/\n./\n /g;
2597171ae1a4SAndy Whitcroft
25983e469cdcSAndy Whitcroft#print "linenr<$linenr> <$stat>\n";
25993e469cdcSAndy Whitcroft			# If this statement has no statement boundaries within
26003e469cdcSAndy Whitcroft			# it there is no point in retrying a statement scan
26013e469cdcSAndy Whitcroft			# until we hit end of it.
26023e469cdcSAndy Whitcroft			my $frag = $stat; $frag =~ s/;+\s*$//;
26033e469cdcSAndy Whitcroft			if ($frag !~ /(?:{|;)/) {
26043e469cdcSAndy Whitcroft#print "skip<$line_nr_next>\n";
26053e469cdcSAndy Whitcroft				$suppress_statement = $line_nr_next;
26063e469cdcSAndy Whitcroft			}
2607f74bd194SAndy Whitcroft
26082b474a1aSAndy Whitcroft			# Find the real next line.
26092b474a1aSAndy Whitcroft			$realline_next = $line_nr_next;
26102b474a1aSAndy Whitcroft			if (defined $realline_next &&
26112b474a1aSAndy Whitcroft			    (!defined $lines[$realline_next - 1] ||
26122b474a1aSAndy Whitcroft			     substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
26132b474a1aSAndy Whitcroft				$realline_next++;
26142b474a1aSAndy Whitcroft			}
26152b474a1aSAndy Whitcroft
2616171ae1a4SAndy Whitcroft			my $s = $stat;
2617171ae1a4SAndy Whitcroft			$s =~ s/{.*$//s;
2618cf655043SAndy Whitcroft
2619c2fdda0dSAndy Whitcroft			# Ignore goto labels.
2620171ae1a4SAndy Whitcroft			if ($s =~ /$Ident:\*$/s) {
2621c2fdda0dSAndy Whitcroft
2622c2fdda0dSAndy Whitcroft			# Ignore functions being called
2623171ae1a4SAndy Whitcroft			} elsif ($s =~ /^.\s*$Ident\s*\(/s) {
2624c2fdda0dSAndy Whitcroft
2625463f2864SAndy Whitcroft			} elsif ($s =~ /^.\s*else\b/s) {
2626463f2864SAndy Whitcroft
2627c45dcabdSAndy Whitcroft			# declarations always start with types
2628d2506586SAndy 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) {
2629c45dcabdSAndy Whitcroft				my $type = $1;
2630c45dcabdSAndy Whitcroft				$type =~ s/\s+/ /g;
2631c45dcabdSAndy Whitcroft				possible($type, "A:" . $s);
2632c45dcabdSAndy Whitcroft
26336c72ffaaSAndy Whitcroft			# definitions in global scope can only start with types
2634a6a84062SAndy Whitcroft			} elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
2635c45dcabdSAndy Whitcroft				possible($1, "B:" . $s);
2636c2fdda0dSAndy Whitcroft			}
26378905a67cSAndy Whitcroft
26386c72ffaaSAndy Whitcroft			# any (foo ... *) is a pointer cast, and foo is a type
263965863862SAndy Whitcroft			while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
2640c45dcabdSAndy Whitcroft				possible($1, "C:" . $s);
26419c0ca6f9SAndy Whitcroft			}
26428905a67cSAndy Whitcroft
26438905a67cSAndy Whitcroft			# Check for any sort of function declaration.
26448905a67cSAndy Whitcroft			# int foo(something bar, other baz);
26458905a67cSAndy Whitcroft			# void (*store_gdt)(x86_descr_ptr *);
2646171ae1a4SAndy Whitcroft			if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
26478905a67cSAndy Whitcroft				my ($name_len) = length($1);
26488905a67cSAndy Whitcroft
2649cf655043SAndy Whitcroft				my $ctx = $s;
2650773647a0SAndy Whitcroft				substr($ctx, 0, $name_len + 1, '');
26518905a67cSAndy Whitcroft				$ctx =~ s/\)[^\)]*$//;
2652cf655043SAndy Whitcroft
26538905a67cSAndy Whitcroft				for my $arg (split(/\s*,\s*/, $ctx)) {
2654c45dcabdSAndy Whitcroft					if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
26558905a67cSAndy Whitcroft
2656c45dcabdSAndy Whitcroft						possible($1, "D:" . $s);
26578905a67cSAndy Whitcroft					}
26588905a67cSAndy Whitcroft				}
26598905a67cSAndy Whitcroft			}
26608905a67cSAndy Whitcroft
26619c0ca6f9SAndy Whitcroft		}
26629c0ca6f9SAndy Whitcroft
266300df344fSAndy Whitcroft#
266400df344fSAndy Whitcroft# Checks which may be anchored in the context.
266500df344fSAndy Whitcroft#
266600df344fSAndy Whitcroft
266700df344fSAndy Whitcroft# Check for switch () and associated case and default
266800df344fSAndy Whitcroft# statements should be at the same indent.
266900df344fSAndy Whitcroft		if ($line=~/\bswitch\s*\(.*\)/) {
267000df344fSAndy Whitcroft			my $err = '';
267100df344fSAndy Whitcroft			my $sep = '';
267200df344fSAndy Whitcroft			my @ctx = ctx_block_outer($linenr, $realcnt);
267300df344fSAndy Whitcroft			shift(@ctx);
267400df344fSAndy Whitcroft			for my $ctx (@ctx) {
267500df344fSAndy Whitcroft				my ($clen, $cindent) = line_stats($ctx);
267600df344fSAndy Whitcroft				if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
267700df344fSAndy Whitcroft							$indent != $cindent) {
267800df344fSAndy Whitcroft					$err .= "$sep$ctx\n";
267900df344fSAndy Whitcroft					$sep = '';
268000df344fSAndy Whitcroft				} else {
268100df344fSAndy Whitcroft					$sep = "[...]\n";
268200df344fSAndy Whitcroft				}
268300df344fSAndy Whitcroft			}
268400df344fSAndy Whitcroft			if ($err ne '') {
2685000d1cc1SJoe Perches				ERROR("SWITCH_CASE_INDENT_LEVEL",
2686000d1cc1SJoe Perches				      "switch and case should be at the same indent\n$hereline$err");
2687de7d4f0eSAndy Whitcroft			}
2688de7d4f0eSAndy Whitcroft		}
2689de7d4f0eSAndy Whitcroft
2690de7d4f0eSAndy Whitcroft# if/while/etc brace do not go on next line, unless defining a do while loop,
2691de7d4f0eSAndy Whitcroft# or if that brace on the next line is for something else
2692c45dcabdSAndy Whitcroft		if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
2693773647a0SAndy Whitcroft			my $pre_ctx = "$1$2";
2694773647a0SAndy Whitcroft
26959c0ca6f9SAndy Whitcroft			my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
26968eef05ddSJoe Perches
26978eef05ddSJoe Perches			if ($line =~ /^\+\t{6,}/) {
26988eef05ddSJoe Perches				WARN("DEEP_INDENTATION",
26998eef05ddSJoe Perches				     "Too many leading tabs - consider code refactoring\n" . $herecurr);
27008eef05ddSJoe Perches			}
27018eef05ddSJoe Perches
2702de7d4f0eSAndy Whitcroft			my $ctx_cnt = $realcnt - $#ctx - 1;
2703de7d4f0eSAndy Whitcroft			my $ctx = join("\n", @ctx);
2704de7d4f0eSAndy Whitcroft
2705548596d5SAndy Whitcroft			my $ctx_ln = $linenr;
2706548596d5SAndy Whitcroft			my $ctx_skip = $realcnt;
2707de7d4f0eSAndy Whitcroft
2708548596d5SAndy Whitcroft			while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
2709548596d5SAndy Whitcroft					defined $lines[$ctx_ln - 1] &&
2710548596d5SAndy Whitcroft					$lines[$ctx_ln - 1] =~ /^-/)) {
2711548596d5SAndy Whitcroft				##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
2712548596d5SAndy Whitcroft				$ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
2713773647a0SAndy Whitcroft				$ctx_ln++;
2714773647a0SAndy Whitcroft			}
2715548596d5SAndy Whitcroft
271653210168SAndy Whitcroft			#print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
271753210168SAndy Whitcroft			#print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
2718773647a0SAndy Whitcroft
2719773647a0SAndy Whitcroft			if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
2720000d1cc1SJoe Perches				ERROR("OPEN_BRACE",
2721000d1cc1SJoe Perches				      "that open brace { should be on the previous line\n" .
272201464f30SAndy Whitcroft					"$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
272300df344fSAndy Whitcroft			}
2724773647a0SAndy Whitcroft			if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
2725773647a0SAndy Whitcroft			    $ctx =~ /\)\s*\;\s*$/ &&
2726773647a0SAndy Whitcroft			    defined $lines[$ctx_ln - 1])
2727773647a0SAndy Whitcroft			{
27289c0ca6f9SAndy Whitcroft				my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
27299c0ca6f9SAndy Whitcroft				if ($nindent > $indent) {
2730000d1cc1SJoe Perches					WARN("TRAILING_SEMICOLON",
2731000d1cc1SJoe Perches					     "trailing semicolon indicates no statements, indent implies otherwise\n" .
273201464f30SAndy Whitcroft						"$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
27339c0ca6f9SAndy Whitcroft				}
27349c0ca6f9SAndy Whitcroft			}
273500df344fSAndy Whitcroft		}
273600df344fSAndy Whitcroft
27374d001e4dSAndy Whitcroft# Check relative indent for conditionals and blocks.
27384d001e4dSAndy Whitcroft		if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
27393e469cdcSAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
27403e469cdcSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0)
27413e469cdcSAndy Whitcroft					if (!defined $stat);
27424d001e4dSAndy Whitcroft			my ($s, $c) = ($stat, $cond);
27434d001e4dSAndy Whitcroft
27444d001e4dSAndy Whitcroft			substr($s, 0, length($c), '');
27454d001e4dSAndy Whitcroft
27464d001e4dSAndy Whitcroft			# Make sure we remove the line prefixes as we have
27474d001e4dSAndy Whitcroft			# none on the first line, and are going to readd them
27484d001e4dSAndy Whitcroft			# where necessary.
27494d001e4dSAndy Whitcroft			$s =~ s/\n./\n/gs;
27504d001e4dSAndy Whitcroft
27514d001e4dSAndy Whitcroft			# Find out how long the conditional actually is.
27526f779c18SAndy Whitcroft			my @newlines = ($c =~ /\n/gs);
27536f779c18SAndy Whitcroft			my $cond_lines = 1 + $#newlines;
27544d001e4dSAndy Whitcroft
27554d001e4dSAndy Whitcroft			# We want to check the first line inside the block
27564d001e4dSAndy Whitcroft			# starting at the end of the conditional, so remove:
27574d001e4dSAndy Whitcroft			#  1) any blank line termination
27584d001e4dSAndy Whitcroft			#  2) any opening brace { on end of the line
27594d001e4dSAndy Whitcroft			#  3) any do (...) {
27604d001e4dSAndy Whitcroft			my $continuation = 0;
27614d001e4dSAndy Whitcroft			my $check = 0;
27624d001e4dSAndy Whitcroft			$s =~ s/^.*\bdo\b//;
27634d001e4dSAndy Whitcroft			$s =~ s/^\s*{//;
27644d001e4dSAndy Whitcroft			if ($s =~ s/^\s*\\//) {
27654d001e4dSAndy Whitcroft				$continuation = 1;
27664d001e4dSAndy Whitcroft			}
27679bd49efeSAndy Whitcroft			if ($s =~ s/^\s*?\n//) {
27684d001e4dSAndy Whitcroft				$check = 1;
27694d001e4dSAndy Whitcroft				$cond_lines++;
27704d001e4dSAndy Whitcroft			}
27714d001e4dSAndy Whitcroft
27724d001e4dSAndy Whitcroft			# Also ignore a loop construct at the end of a
27734d001e4dSAndy Whitcroft			# preprocessor statement.
27744d001e4dSAndy Whitcroft			if (($prevline =~ /^.\s*#\s*define\s/ ||
27754d001e4dSAndy Whitcroft			    $prevline =~ /\\\s*$/) && $continuation == 0) {
27764d001e4dSAndy Whitcroft				$check = 0;
27774d001e4dSAndy Whitcroft			}
27784d001e4dSAndy Whitcroft
27799bd49efeSAndy Whitcroft			my $cond_ptr = -1;
2780740504c6SAndy Whitcroft			$continuation = 0;
27819bd49efeSAndy Whitcroft			while ($cond_ptr != $cond_lines) {
27829bd49efeSAndy Whitcroft				$cond_ptr = $cond_lines;
27834d001e4dSAndy Whitcroft
2784f16fa28fSAndy Whitcroft				# If we see an #else/#elif then the code
2785f16fa28fSAndy Whitcroft				# is not linear.
2786f16fa28fSAndy Whitcroft				if ($s =~ /^\s*\#\s*(?:else|elif)/) {
2787f16fa28fSAndy Whitcroft					$check = 0;
2788f16fa28fSAndy Whitcroft				}
2789f16fa28fSAndy Whitcroft
27909bd49efeSAndy Whitcroft				# Ignore:
27919bd49efeSAndy Whitcroft				#  1) blank lines, they should be at 0,
27929bd49efeSAndy Whitcroft				#  2) preprocessor lines, and
27939bd49efeSAndy Whitcroft				#  3) labels.
2794740504c6SAndy Whitcroft				if ($continuation ||
2795740504c6SAndy Whitcroft				    $s =~ /^\s*?\n/ ||
27969bd49efeSAndy Whitcroft				    $s =~ /^\s*#\s*?/ ||
27979bd49efeSAndy Whitcroft				    $s =~ /^\s*$Ident\s*:/) {
2798740504c6SAndy Whitcroft					$continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
279930dad6ebSAndy Whitcroft					if ($s =~ s/^.*?\n//) {
28009bd49efeSAndy Whitcroft						$cond_lines++;
28019bd49efeSAndy Whitcroft					}
28024d001e4dSAndy Whitcroft				}
280330dad6ebSAndy Whitcroft			}
28044d001e4dSAndy Whitcroft
28054d001e4dSAndy Whitcroft			my (undef, $sindent) = line_stats("+" . $s);
28064d001e4dSAndy Whitcroft			my $stat_real = raw_line($linenr, $cond_lines);
28074d001e4dSAndy Whitcroft
28084d001e4dSAndy Whitcroft			# Check if either of these lines are modified, else
28094d001e4dSAndy Whitcroft			# this is not this patch's fault.
28104d001e4dSAndy Whitcroft			if (!defined($stat_real) ||
28114d001e4dSAndy Whitcroft			    $stat !~ /^\+/ && $stat_real !~ /^\+/) {
28124d001e4dSAndy Whitcroft				$check = 0;
28134d001e4dSAndy Whitcroft			}
28144d001e4dSAndy Whitcroft			if (defined($stat_real) && $cond_lines > 1) {
28154d001e4dSAndy Whitcroft				$stat_real = "[...]\n$stat_real";
28164d001e4dSAndy Whitcroft			}
28174d001e4dSAndy Whitcroft
28189bd49efeSAndy 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";
28194d001e4dSAndy Whitcroft
28204d001e4dSAndy Whitcroft			if ($check && (($sindent % 8) != 0 ||
28214d001e4dSAndy Whitcroft			    ($sindent <= $indent && $s ne ''))) {
2822000d1cc1SJoe Perches				WARN("SUSPECT_CODE_INDENT",
2823000d1cc1SJoe Perches				     "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
28244d001e4dSAndy Whitcroft			}
28254d001e4dSAndy Whitcroft		}
28264d001e4dSAndy Whitcroft
28276c72ffaaSAndy Whitcroft		# Track the 'values' across context and added lines.
28286c72ffaaSAndy Whitcroft		my $opline = $line; $opline =~ s/^./ /;
28291f65f947SAndy Whitcroft		my ($curr_values, $curr_vars) =
28301f65f947SAndy Whitcroft				annotate_values($opline . "\n", $prev_values);
28316c72ffaaSAndy Whitcroft		$curr_values = $prev_values . $curr_values;
2832c2fdda0dSAndy Whitcroft		if ($dbg_values) {
2833c2fdda0dSAndy Whitcroft			my $outline = $opline; $outline =~ s/\t/ /g;
2834cf655043SAndy Whitcroft			print "$linenr > .$outline\n";
2835cf655043SAndy Whitcroft			print "$linenr > $curr_values\n";
28361f65f947SAndy Whitcroft			print "$linenr >  $curr_vars\n";
2837c2fdda0dSAndy Whitcroft		}
28386c72ffaaSAndy Whitcroft		$prev_values = substr($curr_values, -1);
28396c72ffaaSAndy Whitcroft
284000df344fSAndy Whitcroft#ignore lines not being added
28413705ce5bSJoe Perches		next if ($line =~ /^[^\+]/);
284200df344fSAndy Whitcroft
2843653d4876SAndy Whitcroft# TEST: allow direct testing of the type matcher.
28447429c690SAndy Whitcroft		if ($dbg_type) {
28457429c690SAndy Whitcroft			if ($line =~ /^.\s*$Declare\s*$/) {
2846000d1cc1SJoe Perches				ERROR("TEST_TYPE",
2847000d1cc1SJoe Perches				      "TEST: is type\n" . $herecurr);
28487429c690SAndy Whitcroft			} elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
2849000d1cc1SJoe Perches				ERROR("TEST_NOT_TYPE",
2850000d1cc1SJoe Perches				      "TEST: is not type ($1 is)\n". $herecurr);
28517429c690SAndy Whitcroft			}
2852653d4876SAndy Whitcroft			next;
2853653d4876SAndy Whitcroft		}
2854a1ef277eSAndy Whitcroft# TEST: allow direct testing of the attribute matcher.
2855a1ef277eSAndy Whitcroft		if ($dbg_attr) {
28569360b0e5SAndy Whitcroft			if ($line =~ /^.\s*$Modifier\s*$/) {
2857000d1cc1SJoe Perches				ERROR("TEST_ATTR",
2858000d1cc1SJoe Perches				      "TEST: is attr\n" . $herecurr);
28599360b0e5SAndy Whitcroft			} elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
2860000d1cc1SJoe Perches				ERROR("TEST_NOT_ATTR",
2861000d1cc1SJoe Perches				      "TEST: is not attr ($1 is)\n". $herecurr);
2862a1ef277eSAndy Whitcroft			}
2863a1ef277eSAndy Whitcroft			next;
2864a1ef277eSAndy Whitcroft		}
2865653d4876SAndy Whitcroft
2866f0a594c1SAndy Whitcroft# check for initialisation to aggregates open brace on the next line
286799423c20SAndy Whitcroft		if ($line =~ /^.\s*{/ &&
286899423c20SAndy Whitcroft		    $prevline =~ /(?:^|[^=])=\s*$/) {
2869*d752fcc8SJoe Perches			if (ERROR("OPEN_BRACE",
2870*d752fcc8SJoe Perches				  "that open brace { should be on the previous line\n" . $hereprev) &&
2871*d752fcc8SJoe Perches			    $fix && $prevline =~ /^\+/) {
2872*d752fcc8SJoe Perches				my $deleted = {
2873*d752fcc8SJoe Perches					LINENR => $fixlinenr - 1,
2874*d752fcc8SJoe Perches					LINE => $prevrawline,
2875*d752fcc8SJoe Perches				};
2876*d752fcc8SJoe Perches				push(@fixed_deleted, $deleted);
2877*d752fcc8SJoe Perches				$deleted = {
2878*d752fcc8SJoe Perches					LINENR => $fixlinenr,
2879*d752fcc8SJoe Perches					LINE => $rawline,
2880*d752fcc8SJoe Perches				};
2881*d752fcc8SJoe Perches				push(@fixed_deleted, $deleted);
2882*d752fcc8SJoe Perches				my $fixedline = $prevrawline;
2883*d752fcc8SJoe Perches				$fixedline =~ s/\s*=\s*$/ = {/;
2884*d752fcc8SJoe Perches				my $inserted = {
2885*d752fcc8SJoe Perches					LINENR => $fixlinenr,
2886*d752fcc8SJoe Perches					LINE => $fixedline,
2887*d752fcc8SJoe Perches				};
2888*d752fcc8SJoe Perches				push(@fixed_inserted, $inserted);
2889*d752fcc8SJoe Perches				$fixedline = $line;
2890*d752fcc8SJoe Perches				$fixedline =~ s/^(.\s*){\s*/$1/;
2891*d752fcc8SJoe Perches				$inserted = {
2892*d752fcc8SJoe Perches					LINENR => $fixlinenr,
2893*d752fcc8SJoe Perches					LINE => $fixedline,
2894*d752fcc8SJoe Perches				};
2895*d752fcc8SJoe Perches				push(@fixed_inserted, $inserted);
2896*d752fcc8SJoe Perches			}
2897f0a594c1SAndy Whitcroft		}
2898f0a594c1SAndy Whitcroft
289900df344fSAndy Whitcroft#
290000df344fSAndy Whitcroft# Checks which are anchored on the added line.
290100df344fSAndy Whitcroft#
290200df344fSAndy Whitcroft
2903653d4876SAndy Whitcroft# check for malformed paths in #include statements (uses RAW line)
2904c45dcabdSAndy Whitcroft		if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
2905653d4876SAndy Whitcroft			my $path = $1;
2906653d4876SAndy Whitcroft			if ($path =~ m{//}) {
2907000d1cc1SJoe Perches				ERROR("MALFORMED_INCLUDE",
2908495e9d84SJoe Perches				      "malformed #include filename\n" . $herecurr);
2909495e9d84SJoe Perches			}
2910495e9d84SJoe Perches			if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
2911495e9d84SJoe Perches				ERROR("UAPI_INCLUDE",
2912495e9d84SJoe Perches				      "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
2913653d4876SAndy Whitcroft			}
2914653d4876SAndy Whitcroft		}
2915653d4876SAndy Whitcroft
291600df344fSAndy Whitcroft# no C99 // comments
291700df344fSAndy Whitcroft		if ($line =~ m{//}) {
29183705ce5bSJoe Perches			if (ERROR("C99_COMMENTS",
29193705ce5bSJoe Perches				  "do not use C99 // comments\n" . $herecurr) &&
29203705ce5bSJoe Perches			    $fix) {
2921194f66fcSJoe Perches				my $line = $fixed[$fixlinenr];
29223705ce5bSJoe Perches				if ($line =~ /\/\/(.*)$/) {
29233705ce5bSJoe Perches					my $comment = trim($1);
2924194f66fcSJoe Perches					$fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@;
29253705ce5bSJoe Perches				}
29263705ce5bSJoe Perches			}
292700df344fSAndy Whitcroft		}
292800df344fSAndy Whitcroft		# Remove C99 comments.
29290a920b5bSAndy Whitcroft		$line =~ s@//.*@@;
29306c72ffaaSAndy Whitcroft		$opline =~ s@//.*@@;
29310a920b5bSAndy Whitcroft
29322b474a1aSAndy Whitcroft# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
29332b474a1aSAndy Whitcroft# the whole statement.
29342b474a1aSAndy Whitcroft#print "APW <$lines[$realline_next - 1]>\n";
29352b474a1aSAndy Whitcroft		if (defined $realline_next &&
29362b474a1aSAndy Whitcroft		    exists $lines[$realline_next - 1] &&
29372b474a1aSAndy Whitcroft		    !defined $suppress_export{$realline_next} &&
29382b474a1aSAndy Whitcroft		    ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
29392b474a1aSAndy Whitcroft		     $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
29403cbf62dfSAndy Whitcroft			# Handle definitions which produce identifiers with
29413cbf62dfSAndy Whitcroft			# a prefix:
29423cbf62dfSAndy Whitcroft			#   XXX(foo);
29433cbf62dfSAndy Whitcroft			#   EXPORT_SYMBOL(something_foo);
2944653d4876SAndy Whitcroft			my $name = $1;
294587a53877SAndy Whitcroft			if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
29463cbf62dfSAndy Whitcroft			    $name =~ /^${Ident}_$2/) {
29473cbf62dfSAndy Whitcroft#print "FOO C name<$name>\n";
29483cbf62dfSAndy Whitcroft				$suppress_export{$realline_next} = 1;
29493cbf62dfSAndy Whitcroft
29503cbf62dfSAndy Whitcroft			} elsif ($stat !~ /(?:
29512b474a1aSAndy Whitcroft				\n.}\s*$|
295248012058SAndy Whitcroft				^.DEFINE_$Ident\(\Q$name\E\)|
295348012058SAndy Whitcroft				^.DECLARE_$Ident\(\Q$name\E\)|
295448012058SAndy Whitcroft				^.LIST_HEAD\(\Q$name\E\)|
29552b474a1aSAndy Whitcroft				^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
29562b474a1aSAndy Whitcroft				\b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
295748012058SAndy Whitcroft			    )/x) {
29582b474a1aSAndy Whitcroft#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
29592b474a1aSAndy Whitcroft				$suppress_export{$realline_next} = 2;
29602b474a1aSAndy Whitcroft			} else {
29612b474a1aSAndy Whitcroft				$suppress_export{$realline_next} = 1;
29620a920b5bSAndy Whitcroft			}
29630a920b5bSAndy Whitcroft		}
29642b474a1aSAndy Whitcroft		if (!defined $suppress_export{$linenr} &&
29652b474a1aSAndy Whitcroft		    $prevline =~ /^.\s*$/ &&
29662b474a1aSAndy Whitcroft		    ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
29672b474a1aSAndy Whitcroft		     $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
29682b474a1aSAndy Whitcroft#print "FOO B <$lines[$linenr - 1]>\n";
29692b474a1aSAndy Whitcroft			$suppress_export{$linenr} = 2;
29702b474a1aSAndy Whitcroft		}
29712b474a1aSAndy Whitcroft		if (defined $suppress_export{$linenr} &&
29722b474a1aSAndy Whitcroft		    $suppress_export{$linenr} == 2) {
2973000d1cc1SJoe Perches			WARN("EXPORT_SYMBOL",
2974000d1cc1SJoe Perches			     "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
29752b474a1aSAndy Whitcroft		}
29760a920b5bSAndy Whitcroft
29775150bda4SJoe Eloff# check for global initialisers.
2978d5e616fcSJoe Perches		if ($line =~ /^\+(\s*$Type\s*$Ident\s*(?:\s+$Modifier))*\s*=\s*(0|NULL|false)\s*;/) {
2979d5e616fcSJoe Perches			if (ERROR("GLOBAL_INITIALISERS",
2980000d1cc1SJoe Perches				  "do not initialise globals to 0 or NULL\n" .
2981d5e616fcSJoe Perches				      $herecurr) &&
2982d5e616fcSJoe Perches			    $fix) {
2983194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/($Type\s*$Ident\s*(?:\s+$Modifier))*\s*=\s*(0|NULL|false)\s*;/$1;/;
2984d5e616fcSJoe Perches			}
2985f0a594c1SAndy Whitcroft		}
29860a920b5bSAndy Whitcroft# check for static initialisers.
2987d5e616fcSJoe Perches		if ($line =~ /^\+.*\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
2988d5e616fcSJoe Perches			if (ERROR("INITIALISED_STATIC",
2989000d1cc1SJoe Perches				  "do not initialise statics to 0 or NULL\n" .
2990d5e616fcSJoe Perches				      $herecurr) &&
2991d5e616fcSJoe Perches			    $fix) {
2992194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*(0|NULL|false)\s*;/$1;/;
2993d5e616fcSJoe Perches			}
29940a920b5bSAndy Whitcroft		}
29950a920b5bSAndy Whitcroft
2996cb710ecaSJoe Perches# check for static const char * arrays.
2997cb710ecaSJoe Perches		if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
2998000d1cc1SJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
2999000d1cc1SJoe Perches			     "static const char * array should probably be static const char * const\n" .
3000cb710ecaSJoe Perches				$herecurr);
3001cb710ecaSJoe Perches               }
3002cb710ecaSJoe Perches
3003cb710ecaSJoe Perches# check for static char foo[] = "bar" declarations.
3004cb710ecaSJoe Perches		if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
3005000d1cc1SJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
3006000d1cc1SJoe Perches			     "static char array declaration should probably be static const char\n" .
3007cb710ecaSJoe Perches				$herecurr);
3008cb710ecaSJoe Perches               }
3009cb710ecaSJoe Perches
30109b0fa60dSJoe Perches# check for non-global char *foo[] = {"bar", ...} declarations.
30119b0fa60dSJoe Perches		if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) {
30129b0fa60dSJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
30139b0fa60dSJoe Perches			     "char * array declaration might be better as static const\n" .
30149b0fa60dSJoe Perches				$herecurr);
30159b0fa60dSJoe Perches               }
30169b0fa60dSJoe Perches
3017b36190c5SJoe Perches# check for function declarations without arguments like "int foo()"
3018b36190c5SJoe Perches		if ($line =~ /(\b$Type\s+$Ident)\s*\(\s*\)/) {
3019b36190c5SJoe Perches			if (ERROR("FUNCTION_WITHOUT_ARGS",
3020b36190c5SJoe Perches				  "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) &&
3021b36190c5SJoe Perches			    $fix) {
3022194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/;
3023b36190c5SJoe Perches			}
3024b36190c5SJoe Perches		}
3025b36190c5SJoe Perches
302692e112fdSJoe Perches# check for uses of DEFINE_PCI_DEVICE_TABLE
302792e112fdSJoe Perches		if ($line =~ /\bDEFINE_PCI_DEVICE_TABLE\s*\(\s*(\w+)\s*\)\s*=/) {
302892e112fdSJoe Perches			if (WARN("DEFINE_PCI_DEVICE_TABLE",
302992e112fdSJoe Perches				 "Prefer struct pci_device_id over deprecated DEFINE_PCI_DEVICE_TABLE\n" . $herecurr) &&
303092e112fdSJoe Perches			    $fix) {
3031194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b(?:static\s+|)DEFINE_PCI_DEVICE_TABLE\s*\(\s*(\w+)\s*\)\s*=\s*/static const struct pci_device_id $1\[\] = /;
303292e112fdSJoe Perches			}
303393ed0e2dSJoe Perches		}
303493ed0e2dSJoe Perches
3035653d4876SAndy Whitcroft# check for new typedefs, only function parameters and sparse annotations
3036653d4876SAndy Whitcroft# make sense.
3037653d4876SAndy Whitcroft		if ($line =~ /\btypedef\s/ &&
30388054576dSAndy Whitcroft		    $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
3039c45dcabdSAndy Whitcroft		    $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
30408ed22cadSAndy Whitcroft		    $line !~ /\b$typeTypedefs\b/ &&
3041653d4876SAndy Whitcroft		    $line !~ /\b__bitwise(?:__|)\b/) {
3042000d1cc1SJoe Perches			WARN("NEW_TYPEDEFS",
3043000d1cc1SJoe Perches			     "do not add new typedefs\n" . $herecurr);
30440a920b5bSAndy Whitcroft		}
30450a920b5bSAndy Whitcroft
30460a920b5bSAndy Whitcroft# * goes on variable not on type
304765863862SAndy Whitcroft		# (char*[ const])
3048bfcb2cc7SAndy Whitcroft		while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
3049bfcb2cc7SAndy Whitcroft			#print "AA<$1>\n";
30503705ce5bSJoe Perches			my ($ident, $from, $to) = ($1, $2, $2);
3051d8aaf121SAndy Whitcroft
305265863862SAndy Whitcroft			# Should start with a space.
305365863862SAndy Whitcroft			$to =~ s/^(\S)/ $1/;
305465863862SAndy Whitcroft			# Should not end with a space.
305565863862SAndy Whitcroft			$to =~ s/\s+$//;
305665863862SAndy Whitcroft			# '*'s should not have spaces between.
3057f9a0b3d1SAndy Whitcroft			while ($to =~ s/\*\s+\*/\*\*/) {
305865863862SAndy Whitcroft			}
3059d8aaf121SAndy Whitcroft
30603705ce5bSJoe Perches##			print "1: from<$from> to<$to> ident<$ident>\n";
306165863862SAndy Whitcroft			if ($from ne $to) {
30623705ce5bSJoe Perches				if (ERROR("POINTER_LOCATION",
30633705ce5bSJoe Perches					  "\"(foo$from)\" should be \"(foo$to)\"\n" .  $herecurr) &&
30643705ce5bSJoe Perches				    $fix) {
30653705ce5bSJoe Perches					my $sub_from = $ident;
30663705ce5bSJoe Perches					my $sub_to = $ident;
30673705ce5bSJoe Perches					$sub_to =~ s/\Q$from\E/$to/;
3068194f66fcSJoe Perches					$fixed[$fixlinenr] =~
30693705ce5bSJoe Perches					    s@\Q$sub_from\E@$sub_to@;
30703705ce5bSJoe Perches				}
307165863862SAndy Whitcroft			}
3072bfcb2cc7SAndy Whitcroft		}
3073bfcb2cc7SAndy Whitcroft		while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
3074bfcb2cc7SAndy Whitcroft			#print "BB<$1>\n";
30753705ce5bSJoe Perches			my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
3076d8aaf121SAndy Whitcroft
307765863862SAndy Whitcroft			# Should start with a space.
307865863862SAndy Whitcroft			$to =~ s/^(\S)/ $1/;
307965863862SAndy Whitcroft			# Should not end with a space.
308065863862SAndy Whitcroft			$to =~ s/\s+$//;
308165863862SAndy Whitcroft			# '*'s should not have spaces between.
3082f9a0b3d1SAndy Whitcroft			while ($to =~ s/\*\s+\*/\*\*/) {
308365863862SAndy Whitcroft			}
308465863862SAndy Whitcroft			# Modifiers should have spaces.
308565863862SAndy Whitcroft			$to =~ s/(\b$Modifier$)/$1 /;
308665863862SAndy Whitcroft
30873705ce5bSJoe Perches##			print "2: from<$from> to<$to> ident<$ident>\n";
3088667026e7SAndy Whitcroft			if ($from ne $to && $ident !~ /^$Modifier$/) {
30893705ce5bSJoe Perches				if (ERROR("POINTER_LOCATION",
30903705ce5bSJoe Perches					  "\"foo${from}bar\" should be \"foo${to}bar\"\n" .  $herecurr) &&
30913705ce5bSJoe Perches				    $fix) {
30923705ce5bSJoe Perches
30933705ce5bSJoe Perches					my $sub_from = $match;
30943705ce5bSJoe Perches					my $sub_to = $match;
30953705ce5bSJoe Perches					$sub_to =~ s/\Q$from\E/$to/;
3096194f66fcSJoe Perches					$fixed[$fixlinenr] =~
30973705ce5bSJoe Perches					    s@\Q$sub_from\E@$sub_to@;
30983705ce5bSJoe Perches				}
309965863862SAndy Whitcroft			}
31000a920b5bSAndy Whitcroft		}
31010a920b5bSAndy Whitcroft
31020a920b5bSAndy Whitcroft# # no BUG() or BUG_ON()
31030a920b5bSAndy Whitcroft# 		if ($line =~ /\b(BUG|BUG_ON)\b/) {
31040a920b5bSAndy Whitcroft# 			print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
31050a920b5bSAndy Whitcroft# 			print "$herecurr";
31060a920b5bSAndy Whitcroft# 			$clean = 0;
31070a920b5bSAndy Whitcroft# 		}
31080a920b5bSAndy Whitcroft
31098905a67cSAndy Whitcroft		if ($line =~ /\bLINUX_VERSION_CODE\b/) {
3110000d1cc1SJoe Perches			WARN("LINUX_VERSION_CODE",
3111000d1cc1SJoe Perches			     "LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
31128905a67cSAndy Whitcroft		}
31138905a67cSAndy Whitcroft
311417441227SJoe Perches# check for uses of printk_ratelimit
311517441227SJoe Perches		if ($line =~ /\bprintk_ratelimit\s*\(/) {
3116000d1cc1SJoe Perches			WARN("PRINTK_RATELIMITED",
3117000d1cc1SJoe Perches"Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
311817441227SJoe Perches		}
311917441227SJoe Perches
312000df344fSAndy Whitcroft# printk should use KERN_* levels.  Note that follow on printk's on the
312100df344fSAndy Whitcroft# same line do not need a level, so we use the current block context
312200df344fSAndy Whitcroft# to try and find and validate the current printk.  In summary the current
312325985edcSLucas De Marchi# printk includes all preceding printk's which have no newline on the end.
312400df344fSAndy Whitcroft# we assume the first bad printk is the one to report.
3125f0a594c1SAndy Whitcroft		if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
312600df344fSAndy Whitcroft			my $ok = 0;
312700df344fSAndy Whitcroft			for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
312800df344fSAndy Whitcroft				#print "CHECK<$lines[$ln - 1]\n";
312925985edcSLucas De Marchi				# we have a preceding printk if it ends
313000df344fSAndy Whitcroft				# with "\n" ignore it, else it is to blame
313100df344fSAndy Whitcroft				if ($lines[$ln - 1] =~ m{\bprintk\(}) {
313200df344fSAndy Whitcroft					if ($rawlines[$ln - 1] !~ m{\\n"}) {
313300df344fSAndy Whitcroft						$ok = 1;
313400df344fSAndy Whitcroft					}
313500df344fSAndy Whitcroft					last;
313600df344fSAndy Whitcroft				}
313700df344fSAndy Whitcroft			}
313800df344fSAndy Whitcroft			if ($ok == 0) {
3139000d1cc1SJoe Perches				WARN("PRINTK_WITHOUT_KERN_LEVEL",
3140000d1cc1SJoe Perches				     "printk() should include KERN_ facility level\n" . $herecurr);
31410a920b5bSAndy Whitcroft			}
314200df344fSAndy Whitcroft		}
31430a920b5bSAndy Whitcroft
3144243f3803SJoe Perches		if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
3145243f3803SJoe Perches			my $orig = $1;
3146243f3803SJoe Perches			my $level = lc($orig);
3147243f3803SJoe Perches			$level = "warn" if ($level eq "warning");
31488f26b837SJoe Perches			my $level2 = $level;
31498f26b837SJoe Perches			$level2 = "dbg" if ($level eq "debug");
3150243f3803SJoe Perches			WARN("PREFER_PR_LEVEL",
3151daa8b059SYogesh Chaudhari			     "Prefer [subsystem eg: netdev]_$level2([subsystem]dev, ... then dev_$level2(dev, ... then pr_$level(...  to printk(KERN_$orig ...\n" . $herecurr);
3152243f3803SJoe Perches		}
3153243f3803SJoe Perches
3154243f3803SJoe Perches		if ($line =~ /\bpr_warning\s*\(/) {
3155d5e616fcSJoe Perches			if (WARN("PREFER_PR_LEVEL",
3156d5e616fcSJoe Perches				 "Prefer pr_warn(... to pr_warning(...\n" . $herecurr) &&
3157d5e616fcSJoe Perches			    $fix) {
3158194f66fcSJoe Perches				$fixed[$fixlinenr] =~
3159d5e616fcSJoe Perches				    s/\bpr_warning\b/pr_warn/;
3160d5e616fcSJoe Perches			}
3161243f3803SJoe Perches		}
3162243f3803SJoe Perches
3163dc139313SJoe Perches		if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
3164dc139313SJoe Perches			my $orig = $1;
3165dc139313SJoe Perches			my $level = lc($orig);
3166dc139313SJoe Perches			$level = "warn" if ($level eq "warning");
3167dc139313SJoe Perches			$level = "dbg" if ($level eq "debug");
3168dc139313SJoe Perches			WARN("PREFER_DEV_LEVEL",
3169dc139313SJoe Perches			     "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
3170dc139313SJoe Perches		}
3171dc139313SJoe Perches
3172653d4876SAndy Whitcroft# function brace can't be on same line, except for #defines of do while,
3173653d4876SAndy Whitcroft# or if closed on same line
3174c45dcabdSAndy Whitcroft		if (($line=~/$Type\s*$Ident\(.*\).*\s{/) and
3175c45dcabdSAndy Whitcroft		    !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) {
3176000d1cc1SJoe Perches			ERROR("OPEN_BRACE",
3177000d1cc1SJoe Perches			      "open brace '{' following function declarations go on the next line\n" . $herecurr);
31780a920b5bSAndy Whitcroft		}
3179653d4876SAndy Whitcroft
31808905a67cSAndy Whitcroft# open braces for enum, union and struct go on the same line.
31818905a67cSAndy Whitcroft		if ($line =~ /^.\s*{/ &&
31828905a67cSAndy Whitcroft		    $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
3183000d1cc1SJoe Perches			ERROR("OPEN_BRACE",
3184000d1cc1SJoe Perches			      "open brace '{' following $1 go on the same line\n" . $hereprev);
31858905a67cSAndy Whitcroft		}
31868905a67cSAndy Whitcroft
31870c73b4ebSAndy Whitcroft# missing space after union, struct or enum definition
31883705ce5bSJoe Perches		if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
31893705ce5bSJoe Perches			if (WARN("SPACING",
31903705ce5bSJoe Perches				 "missing space after $1 definition\n" . $herecurr) &&
31913705ce5bSJoe Perches			    $fix) {
3192194f66fcSJoe Perches				$fixed[$fixlinenr] =~
31933705ce5bSJoe Perches				    s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
31943705ce5bSJoe Perches			}
31950c73b4ebSAndy Whitcroft		}
31960c73b4ebSAndy Whitcroft
319731070b5dSJoe Perches# Function pointer declarations
319831070b5dSJoe Perches# check spacing between type, funcptr, and args
319931070b5dSJoe Perches# canonical declaration is "type (*funcptr)(args...)"
320091f72e9cSJoe Perches		if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) {
320131070b5dSJoe Perches			my $declare = $1;
320231070b5dSJoe Perches			my $pre_pointer_space = $2;
320331070b5dSJoe Perches			my $post_pointer_space = $3;
320431070b5dSJoe Perches			my $funcname = $4;
320531070b5dSJoe Perches			my $post_funcname_space = $5;
320631070b5dSJoe Perches			my $pre_args_space = $6;
320731070b5dSJoe Perches
320891f72e9cSJoe Perches# the $Declare variable will capture all spaces after the type
320991f72e9cSJoe Perches# so check it for a missing trailing missing space but pointer return types
321091f72e9cSJoe Perches# don't need a space so don't warn for those.
321191f72e9cSJoe Perches			my $post_declare_space = "";
321291f72e9cSJoe Perches			if ($declare =~ /(\s+)$/) {
321391f72e9cSJoe Perches				$post_declare_space = $1;
321491f72e9cSJoe Perches				$declare = rtrim($declare);
321591f72e9cSJoe Perches			}
321691f72e9cSJoe Perches			if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) {
321731070b5dSJoe Perches				WARN("SPACING",
321831070b5dSJoe Perches				     "missing space after return type\n" . $herecurr);
321991f72e9cSJoe Perches				$post_declare_space = " ";
322031070b5dSJoe Perches			}
322131070b5dSJoe Perches
322231070b5dSJoe Perches# unnecessary space "type  (*funcptr)(args...)"
322391f72e9cSJoe Perches# This test is not currently implemented because these declarations are
322491f72e9cSJoe Perches# equivalent to
322591f72e9cSJoe Perches#	int  foo(int bar, ...)
322691f72e9cSJoe Perches# and this is form shouldn't/doesn't generate a checkpatch warning.
322791f72e9cSJoe Perches#
322891f72e9cSJoe Perches#			elsif ($declare =~ /\s{2,}$/) {
322991f72e9cSJoe Perches#				WARN("SPACING",
323091f72e9cSJoe Perches#				     "Multiple spaces after return type\n" . $herecurr);
323191f72e9cSJoe Perches#			}
323231070b5dSJoe Perches
323331070b5dSJoe Perches# unnecessary space "type ( *funcptr)(args...)"
323431070b5dSJoe Perches			if (defined $pre_pointer_space &&
323531070b5dSJoe Perches			    $pre_pointer_space =~ /^\s/) {
323631070b5dSJoe Perches				WARN("SPACING",
323731070b5dSJoe Perches				     "Unnecessary space after function pointer open parenthesis\n" . $herecurr);
323831070b5dSJoe Perches			}
323931070b5dSJoe Perches
324031070b5dSJoe Perches# unnecessary space "type (* funcptr)(args...)"
324131070b5dSJoe Perches			if (defined $post_pointer_space &&
324231070b5dSJoe Perches			    $post_pointer_space =~ /^\s/) {
324331070b5dSJoe Perches				WARN("SPACING",
324431070b5dSJoe Perches				     "Unnecessary space before function pointer name\n" . $herecurr);
324531070b5dSJoe Perches			}
324631070b5dSJoe Perches
324731070b5dSJoe Perches# unnecessary space "type (*funcptr )(args...)"
324831070b5dSJoe Perches			if (defined $post_funcname_space &&
324931070b5dSJoe Perches			    $post_funcname_space =~ /^\s/) {
325031070b5dSJoe Perches				WARN("SPACING",
325131070b5dSJoe Perches				     "Unnecessary space after function pointer name\n" . $herecurr);
325231070b5dSJoe Perches			}
325331070b5dSJoe Perches
325431070b5dSJoe Perches# unnecessary space "type (*funcptr) (args...)"
325531070b5dSJoe Perches			if (defined $pre_args_space &&
325631070b5dSJoe Perches			    $pre_args_space =~ /^\s/) {
325731070b5dSJoe Perches				WARN("SPACING",
325831070b5dSJoe Perches				     "Unnecessary space before function pointer arguments\n" . $herecurr);
325931070b5dSJoe Perches			}
326031070b5dSJoe Perches
326131070b5dSJoe Perches			if (show_type("SPACING") && $fix) {
3262194f66fcSJoe Perches				$fixed[$fixlinenr] =~
326391f72e9cSJoe Perches				    s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex;
326431070b5dSJoe Perches			}
326531070b5dSJoe Perches		}
326631070b5dSJoe Perches
32678d31cfceSAndy Whitcroft# check for spacing round square brackets; allowed:
32688d31cfceSAndy Whitcroft#  1. with a type on the left -- int [] a;
3269fe2a7dbcSAndy Whitcroft#  2. at the beginning of a line for slice initialisers -- [0...10] = 5,
3270fe2a7dbcSAndy Whitcroft#  3. inside a curly brace -- = { [0...10] = 5 }
32718d31cfceSAndy Whitcroft		while ($line =~ /(.*?\s)\[/g) {
32728d31cfceSAndy Whitcroft			my ($where, $prefix) = ($-[1], $1);
32738d31cfceSAndy Whitcroft			if ($prefix !~ /$Type\s+$/ &&
3274fe2a7dbcSAndy Whitcroft			    ($where != 0 || $prefix !~ /^.\s+$/) &&
3275daebc534SAndy Whitcroft			    $prefix !~ /[{,]\s+$/) {
32763705ce5bSJoe Perches				if (ERROR("BRACKET_SPACE",
32773705ce5bSJoe Perches					  "space prohibited before open square bracket '['\n" . $herecurr) &&
32783705ce5bSJoe Perches				    $fix) {
3279194f66fcSJoe Perches				    $fixed[$fixlinenr] =~
32803705ce5bSJoe Perches					s/^(\+.*?)\s+\[/$1\[/;
32813705ce5bSJoe Perches				}
32828d31cfceSAndy Whitcroft			}
32838d31cfceSAndy Whitcroft		}
32848d31cfceSAndy Whitcroft
3285f0a594c1SAndy Whitcroft# check for spaces between functions and their parentheses.
32866c72ffaaSAndy Whitcroft		while ($line =~ /($Ident)\s+\(/g) {
3287c2fdda0dSAndy Whitcroft			my $name = $1;
3288773647a0SAndy Whitcroft			my $ctx_before = substr($line, 0, $-[1]);
3289773647a0SAndy Whitcroft			my $ctx = "$ctx_before$name";
3290c2fdda0dSAndy Whitcroft
3291c2fdda0dSAndy Whitcroft			# Ignore those directives where spaces _are_ permitted.
3292773647a0SAndy Whitcroft			if ($name =~ /^(?:
3293773647a0SAndy Whitcroft				if|for|while|switch|return|case|
3294773647a0SAndy Whitcroft				volatile|__volatile__|
3295773647a0SAndy Whitcroft				__attribute__|format|__extension__|
3296773647a0SAndy Whitcroft				asm|__asm__)$/x)
3297773647a0SAndy Whitcroft			{
3298c2fdda0dSAndy Whitcroft			# cpp #define statements have non-optional spaces, ie
3299c2fdda0dSAndy Whitcroft			# if there is a space between the name and the open
3300c2fdda0dSAndy Whitcroft			# parenthesis it is simply not a parameter group.
3301c45dcabdSAndy Whitcroft			} elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
3302773647a0SAndy Whitcroft
3303773647a0SAndy Whitcroft			# cpp #elif statement condition may start with a (
3304c45dcabdSAndy Whitcroft			} elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
3305c2fdda0dSAndy Whitcroft
3306c2fdda0dSAndy Whitcroft			# If this whole things ends with a type its most
3307c2fdda0dSAndy Whitcroft			# likely a typedef for a function.
3308773647a0SAndy Whitcroft			} elsif ($ctx =~ /$Type$/) {
3309c2fdda0dSAndy Whitcroft
3310c2fdda0dSAndy Whitcroft			} else {
33113705ce5bSJoe Perches				if (WARN("SPACING",
33123705ce5bSJoe Perches					 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
33133705ce5bSJoe Perches					     $fix) {
3314194f66fcSJoe Perches					$fixed[$fixlinenr] =~
33153705ce5bSJoe Perches					    s/\b$name\s+\(/$name\(/;
33163705ce5bSJoe Perches				}
3317f0a594c1SAndy Whitcroft			}
33186c72ffaaSAndy Whitcroft		}
33199a4cad4eSEric Nelson
3320653d4876SAndy Whitcroft# Check operator spacing.
33210a920b5bSAndy Whitcroft		if (!($line=~/\#\s*include/)) {
33223705ce5bSJoe Perches			my $fixed_line = "";
33233705ce5bSJoe Perches			my $line_fixed = 0;
33243705ce5bSJoe Perches
33259c0ca6f9SAndy Whitcroft			my $ops = qr{
33269c0ca6f9SAndy Whitcroft				<<=|>>=|<=|>=|==|!=|
33279c0ca6f9SAndy Whitcroft				\+=|-=|\*=|\/=|%=|\^=|\|=|&=|
33289c0ca6f9SAndy Whitcroft				=>|->|<<|>>|<|>|=|!|~|
33291f65f947SAndy Whitcroft				&&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
333084731623SJoe Perches				\?:|\?|:
33319c0ca6f9SAndy Whitcroft			}x;
3332cf655043SAndy Whitcroft			my @elements = split(/($ops|;)/, $opline);
33333705ce5bSJoe Perches
33343705ce5bSJoe Perches##			print("element count: <" . $#elements . ">\n");
33353705ce5bSJoe Perches##			foreach my $el (@elements) {
33363705ce5bSJoe Perches##				print("el: <$el>\n");
33373705ce5bSJoe Perches##			}
33383705ce5bSJoe Perches
33393705ce5bSJoe Perches			my @fix_elements = ();
334000df344fSAndy Whitcroft			my $off = 0;
33416c72ffaaSAndy Whitcroft
33423705ce5bSJoe Perches			foreach my $el (@elements) {
33433705ce5bSJoe Perches				push(@fix_elements, substr($rawline, $off, length($el)));
33443705ce5bSJoe Perches				$off += length($el);
33453705ce5bSJoe Perches			}
33463705ce5bSJoe Perches
33473705ce5bSJoe Perches			$off = 0;
33483705ce5bSJoe Perches
33496c72ffaaSAndy Whitcroft			my $blank = copy_spacing($opline);
3350b34c648bSJoe Perches			my $last_after = -1;
33516c72ffaaSAndy Whitcroft
33520a920b5bSAndy Whitcroft			for (my $n = 0; $n < $#elements; $n += 2) {
33533705ce5bSJoe Perches
33543705ce5bSJoe Perches				my $good = $fix_elements[$n] . $fix_elements[$n + 1];
33553705ce5bSJoe Perches
33563705ce5bSJoe Perches##				print("n: <$n> good: <$good>\n");
33573705ce5bSJoe Perches
33584a0df2efSAndy Whitcroft				$off += length($elements[$n]);
33594a0df2efSAndy Whitcroft
336025985edcSLucas De Marchi				# Pick up the preceding and succeeding characters.
3361773647a0SAndy Whitcroft				my $ca = substr($opline, 0, $off);
3362773647a0SAndy Whitcroft				my $cc = '';
3363773647a0SAndy Whitcroft				if (length($opline) >= ($off + length($elements[$n + 1]))) {
3364773647a0SAndy Whitcroft					$cc = substr($opline, $off + length($elements[$n + 1]));
3365773647a0SAndy Whitcroft				}
3366773647a0SAndy Whitcroft				my $cb = "$ca$;$cc";
3367773647a0SAndy Whitcroft
33684a0df2efSAndy Whitcroft				my $a = '';
33694a0df2efSAndy Whitcroft				$a = 'V' if ($elements[$n] ne '');
33704a0df2efSAndy Whitcroft				$a = 'W' if ($elements[$n] =~ /\s$/);
3371cf655043SAndy Whitcroft				$a = 'C' if ($elements[$n] =~ /$;$/);
33724a0df2efSAndy Whitcroft				$a = 'B' if ($elements[$n] =~ /(\[|\()$/);
33734a0df2efSAndy Whitcroft				$a = 'O' if ($elements[$n] eq '');
3374773647a0SAndy Whitcroft				$a = 'E' if ($ca =~ /^\s*$/);
33754a0df2efSAndy Whitcroft
33760a920b5bSAndy Whitcroft				my $op = $elements[$n + 1];
33774a0df2efSAndy Whitcroft
33784a0df2efSAndy Whitcroft				my $c = '';
33790a920b5bSAndy Whitcroft				if (defined $elements[$n + 2]) {
33804a0df2efSAndy Whitcroft					$c = 'V' if ($elements[$n + 2] ne '');
33814a0df2efSAndy Whitcroft					$c = 'W' if ($elements[$n + 2] =~ /^\s/);
3382cf655043SAndy Whitcroft					$c = 'C' if ($elements[$n + 2] =~ /^$;/);
33834a0df2efSAndy Whitcroft					$c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
33844a0df2efSAndy Whitcroft					$c = 'O' if ($elements[$n + 2] eq '');
33858b1b3378SAndy Whitcroft					$c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
33864a0df2efSAndy Whitcroft				} else {
33874a0df2efSAndy Whitcroft					$c = 'E';
33880a920b5bSAndy Whitcroft				}
33890a920b5bSAndy Whitcroft
33904a0df2efSAndy Whitcroft				my $ctx = "${a}x${c}";
33914a0df2efSAndy Whitcroft
33924a0df2efSAndy Whitcroft				my $at = "(ctx:$ctx)";
33934a0df2efSAndy Whitcroft
33946c72ffaaSAndy Whitcroft				my $ptr = substr($blank, 0, $off) . "^";
3395de7d4f0eSAndy Whitcroft				my $hereptr = "$hereline$ptr\n";
33960a920b5bSAndy Whitcroft
339774048ed8SAndy Whitcroft				# Pull out the value of this operator.
33986c72ffaaSAndy Whitcroft				my $op_type = substr($curr_values, $off + 1, 1);
33990a920b5bSAndy Whitcroft
34001f65f947SAndy Whitcroft				# Get the full operator variant.
34011f65f947SAndy Whitcroft				my $opv = $op . substr($curr_vars, $off, 1);
34021f65f947SAndy Whitcroft
340313214adfSAndy Whitcroft				# Ignore operators passed as parameters.
340413214adfSAndy Whitcroft				if ($op_type ne 'V' &&
340513214adfSAndy Whitcroft				    $ca =~ /\s$/ && $cc =~ /^\s*,/) {
340613214adfSAndy Whitcroft
3407cf655043SAndy Whitcroft#				# Ignore comments
3408cf655043SAndy Whitcroft#				} elsif ($op =~ /^$;+$/) {
340913214adfSAndy Whitcroft
3410d8aaf121SAndy Whitcroft				# ; should have either the end of line or a space or \ after it
341113214adfSAndy Whitcroft				} elsif ($op eq ';') {
3412cf655043SAndy Whitcroft					if ($ctx !~ /.x[WEBC]/ &&
3413cf655043SAndy Whitcroft					    $cc !~ /^\\/ && $cc !~ /^;/) {
34143705ce5bSJoe Perches						if (ERROR("SPACING",
34153705ce5bSJoe Perches							  "space required after that '$op' $at\n" . $hereptr)) {
3416b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
34173705ce5bSJoe Perches							$line_fixed = 1;
34183705ce5bSJoe Perches						}
3419d8aaf121SAndy Whitcroft					}
3420d8aaf121SAndy Whitcroft
3421d8aaf121SAndy Whitcroft				# // is a comment
3422d8aaf121SAndy Whitcroft				} elsif ($op eq '//') {
34230a920b5bSAndy Whitcroft
3424b00e4814SJoe Perches				#   :   when part of a bitfield
3425b00e4814SJoe Perches				} elsif ($opv eq ':B') {
3426b00e4814SJoe Perches					# skip the bitfield test for now
3427b00e4814SJoe Perches
34281f65f947SAndy Whitcroft				# No spaces for:
34291f65f947SAndy Whitcroft				#   ->
3430b00e4814SJoe Perches				} elsif ($op eq '->') {
34314a0df2efSAndy Whitcroft					if ($ctx =~ /Wx.|.xW/) {
34323705ce5bSJoe Perches						if (ERROR("SPACING",
34333705ce5bSJoe Perches							  "spaces prohibited around that '$op' $at\n" . $hereptr)) {
3434b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
34353705ce5bSJoe Perches							if (defined $fix_elements[$n + 2]) {
34363705ce5bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
34373705ce5bSJoe Perches							}
3438b34c648bSJoe Perches							$line_fixed = 1;
34393705ce5bSJoe Perches						}
34400a920b5bSAndy Whitcroft					}
34410a920b5bSAndy Whitcroft
34420a920b5bSAndy Whitcroft				# , must have a space on the right.
34430a920b5bSAndy Whitcroft				} elsif ($op eq ',') {
3444cf655043SAndy Whitcroft					if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
34453705ce5bSJoe Perches						if (ERROR("SPACING",
34463705ce5bSJoe Perches							  "space required after that '$op' $at\n" . $hereptr)) {
3447b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
34483705ce5bSJoe Perches							$line_fixed = 1;
3449b34c648bSJoe Perches							$last_after = $n;
34503705ce5bSJoe Perches						}
34510a920b5bSAndy Whitcroft					}
34520a920b5bSAndy Whitcroft
34539c0ca6f9SAndy Whitcroft				# '*' as part of a type definition -- reported already.
345474048ed8SAndy Whitcroft				} elsif ($opv eq '*_') {
34559c0ca6f9SAndy Whitcroft					#warn "'*' is part of type\n";
34569c0ca6f9SAndy Whitcroft
34579c0ca6f9SAndy Whitcroft				# unary operators should have a space before and
34589c0ca6f9SAndy Whitcroft				# none after.  May be left adjacent to another
34599c0ca6f9SAndy Whitcroft				# unary operator, or a cast
34609c0ca6f9SAndy Whitcroft				} elsif ($op eq '!' || $op eq '~' ||
346174048ed8SAndy Whitcroft					 $opv eq '*U' || $opv eq '-U' ||
34620d413866SAndy Whitcroft					 $opv eq '&U' || $opv eq '&&U') {
3463cf655043SAndy Whitcroft					if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
34643705ce5bSJoe Perches						if (ERROR("SPACING",
34653705ce5bSJoe Perches							  "space required before that '$op' $at\n" . $hereptr)) {
3466b34c648bSJoe Perches							if ($n != $last_after + 2) {
3467b34c648bSJoe Perches								$good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]);
34683705ce5bSJoe Perches								$line_fixed = 1;
34693705ce5bSJoe Perches							}
34700a920b5bSAndy Whitcroft						}
3471b34c648bSJoe Perches					}
3472a3340b35SAndy Whitcroft					if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
3473171ae1a4SAndy Whitcroft						# A unary '*' may be const
3474171ae1a4SAndy Whitcroft
3475171ae1a4SAndy Whitcroft					} elsif ($ctx =~ /.xW/) {
34763705ce5bSJoe Perches						if (ERROR("SPACING",
34773705ce5bSJoe Perches							  "space prohibited after that '$op' $at\n" . $hereptr)) {
3478b34c648bSJoe Perches							$good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]);
34793705ce5bSJoe Perches							if (defined $fix_elements[$n + 2]) {
34803705ce5bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
34813705ce5bSJoe Perches							}
3482b34c648bSJoe Perches							$line_fixed = 1;
34833705ce5bSJoe Perches						}
34840a920b5bSAndy Whitcroft					}
34850a920b5bSAndy Whitcroft
34860a920b5bSAndy Whitcroft				# unary ++ and unary -- are allowed no space on one side.
34870a920b5bSAndy Whitcroft				} elsif ($op eq '++' or $op eq '--') {
3488773647a0SAndy Whitcroft					if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
34893705ce5bSJoe Perches						if (ERROR("SPACING",
34903705ce5bSJoe Perches							  "space required one side of that '$op' $at\n" . $hereptr)) {
3491b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
34923705ce5bSJoe Perches							$line_fixed = 1;
34933705ce5bSJoe Perches						}
34940a920b5bSAndy Whitcroft					}
3495773647a0SAndy Whitcroft					if ($ctx =~ /Wx[BE]/ ||
3496773647a0SAndy Whitcroft					    ($ctx =~ /Wx./ && $cc =~ /^;/)) {
34973705ce5bSJoe Perches						if (ERROR("SPACING",
34983705ce5bSJoe Perches							  "space prohibited before that '$op' $at\n" . $hereptr)) {
3499b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
35003705ce5bSJoe Perches							$line_fixed = 1;
35013705ce5bSJoe Perches						}
3502653d4876SAndy Whitcroft					}
3503773647a0SAndy Whitcroft					if ($ctx =~ /ExW/) {
35043705ce5bSJoe Perches						if (ERROR("SPACING",
35053705ce5bSJoe Perches							  "space prohibited after that '$op' $at\n" . $hereptr)) {
3506b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
35073705ce5bSJoe Perches							if (defined $fix_elements[$n + 2]) {
35083705ce5bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
3509773647a0SAndy Whitcroft							}
3510b34c648bSJoe Perches							$line_fixed = 1;
35113705ce5bSJoe Perches						}
35123705ce5bSJoe Perches					}
35130a920b5bSAndy Whitcroft
35140a920b5bSAndy Whitcroft				# << and >> may either have or not have spaces both sides
35159c0ca6f9SAndy Whitcroft				} elsif ($op eq '<<' or $op eq '>>' or
35169c0ca6f9SAndy Whitcroft					 $op eq '&' or $op eq '^' or $op eq '|' or
35179c0ca6f9SAndy Whitcroft					 $op eq '+' or $op eq '-' or
3518c2fdda0dSAndy Whitcroft					 $op eq '*' or $op eq '/' or
3519c2fdda0dSAndy Whitcroft					 $op eq '%')
35200a920b5bSAndy Whitcroft				{
3521773647a0SAndy Whitcroft					if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
35223705ce5bSJoe Perches						if (ERROR("SPACING",
35233705ce5bSJoe Perches							  "need consistent spacing around '$op' $at\n" . $hereptr)) {
3524b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
3525b34c648bSJoe Perches							if (defined $fix_elements[$n + 2]) {
3526b34c648bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
3527b34c648bSJoe Perches							}
35283705ce5bSJoe Perches							$line_fixed = 1;
35293705ce5bSJoe Perches						}
35300a920b5bSAndy Whitcroft					}
35310a920b5bSAndy Whitcroft
35321f65f947SAndy Whitcroft				# A colon needs no spaces before when it is
35331f65f947SAndy Whitcroft				# terminating a case value or a label.
35341f65f947SAndy Whitcroft				} elsif ($opv eq ':C' || $opv eq ':L') {
35351f65f947SAndy Whitcroft					if ($ctx =~ /Wx./) {
35363705ce5bSJoe Perches						if (ERROR("SPACING",
35373705ce5bSJoe Perches							  "space prohibited before that '$op' $at\n" . $hereptr)) {
3538b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
35393705ce5bSJoe Perches							$line_fixed = 1;
35403705ce5bSJoe Perches						}
35411f65f947SAndy Whitcroft					}
35421f65f947SAndy Whitcroft
35430a920b5bSAndy Whitcroft				# All the others need spaces both sides.
3544cf655043SAndy Whitcroft				} elsif ($ctx !~ /[EWC]x[CWE]/) {
35451f65f947SAndy Whitcroft					my $ok = 0;
35461f65f947SAndy Whitcroft
354722f2a2efSAndy Whitcroft					# Ignore email addresses <foo@bar>
35481f65f947SAndy Whitcroft					if (($op eq '<' &&
35491f65f947SAndy Whitcroft					     $cc =~ /^\S+\@\S+>/) ||
35501f65f947SAndy Whitcroft					    ($op eq '>' &&
35511f65f947SAndy Whitcroft					     $ca =~ /<\S+\@\S+$/))
35521f65f947SAndy Whitcroft					{
35531f65f947SAndy Whitcroft					    	$ok = 1;
35541f65f947SAndy Whitcroft					}
35551f65f947SAndy Whitcroft
355684731623SJoe Perches					# messages are ERROR, but ?: are CHK
35571f65f947SAndy Whitcroft					if ($ok == 0) {
355884731623SJoe Perches						my $msg_type = \&ERROR;
355984731623SJoe Perches						$msg_type = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
356084731623SJoe Perches
356184731623SJoe Perches						if (&{$msg_type}("SPACING",
35623705ce5bSJoe Perches								 "spaces required around that '$op' $at\n" . $hereptr)) {
3563b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
3564b34c648bSJoe Perches							if (defined $fix_elements[$n + 2]) {
3565b34c648bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
3566b34c648bSJoe Perches							}
35673705ce5bSJoe Perches							$line_fixed = 1;
35683705ce5bSJoe Perches						}
35690a920b5bSAndy Whitcroft					}
357022f2a2efSAndy Whitcroft				}
35714a0df2efSAndy Whitcroft				$off += length($elements[$n + 1]);
35723705ce5bSJoe Perches
35733705ce5bSJoe Perches##				print("n: <$n> GOOD: <$good>\n");
35743705ce5bSJoe Perches
35753705ce5bSJoe Perches				$fixed_line = $fixed_line . $good;
35760a920b5bSAndy Whitcroft			}
35773705ce5bSJoe Perches
35783705ce5bSJoe Perches			if (($#elements % 2) == 0) {
35793705ce5bSJoe Perches				$fixed_line = $fixed_line . $fix_elements[$#elements];
35803705ce5bSJoe Perches			}
35813705ce5bSJoe Perches
3582194f66fcSJoe Perches			if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) {
3583194f66fcSJoe Perches				$fixed[$fixlinenr] = $fixed_line;
35843705ce5bSJoe Perches			}
35853705ce5bSJoe Perches
35863705ce5bSJoe Perches
35870a920b5bSAndy Whitcroft		}
35880a920b5bSAndy Whitcroft
3589786b6326SJoe Perches# check for whitespace before a non-naked semicolon
3590d2e248e7SJoe Perches		if ($line =~ /^\+.*\S\s+;\s*$/) {
3591786b6326SJoe Perches			if (WARN("SPACING",
3592786b6326SJoe Perches				 "space prohibited before semicolon\n" . $herecurr) &&
3593786b6326SJoe Perches			    $fix) {
3594194f66fcSJoe Perches				1 while $fixed[$fixlinenr] =~
3595786b6326SJoe Perches				    s/^(\+.*\S)\s+;/$1;/;
3596786b6326SJoe Perches			}
3597786b6326SJoe Perches		}
3598786b6326SJoe Perches
3599f0a594c1SAndy Whitcroft# check for multiple assignments
3600f0a594c1SAndy Whitcroft		if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
3601000d1cc1SJoe Perches			CHK("MULTIPLE_ASSIGNMENTS",
3602000d1cc1SJoe Perches			    "multiple assignments should be avoided\n" . $herecurr);
3603f0a594c1SAndy Whitcroft		}
3604f0a594c1SAndy Whitcroft
360522f2a2efSAndy Whitcroft## # check for multiple declarations, allowing for a function declaration
360622f2a2efSAndy Whitcroft## # continuation.
360722f2a2efSAndy Whitcroft## 		if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
360822f2a2efSAndy Whitcroft## 		    $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
360922f2a2efSAndy Whitcroft##
361022f2a2efSAndy Whitcroft## 			# Remove any bracketed sections to ensure we do not
361122f2a2efSAndy Whitcroft## 			# falsly report the parameters of functions.
361222f2a2efSAndy Whitcroft## 			my $ln = $line;
361322f2a2efSAndy Whitcroft## 			while ($ln =~ s/\([^\(\)]*\)//g) {
361422f2a2efSAndy Whitcroft## 			}
361522f2a2efSAndy Whitcroft## 			if ($ln =~ /,/) {
3616000d1cc1SJoe Perches## 				WARN("MULTIPLE_DECLARATION",
3617000d1cc1SJoe Perches##				     "declaring multiple variables together should be avoided\n" . $herecurr);
361822f2a2efSAndy Whitcroft## 			}
361922f2a2efSAndy Whitcroft## 		}
3620f0a594c1SAndy Whitcroft
36210a920b5bSAndy Whitcroft#need space before brace following if, while, etc
362222f2a2efSAndy Whitcroft		if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
362322f2a2efSAndy Whitcroft		    $line =~ /do{/) {
36243705ce5bSJoe Perches			if (ERROR("SPACING",
36253705ce5bSJoe Perches				  "space required before the open brace '{'\n" . $herecurr) &&
36263705ce5bSJoe Perches			    $fix) {
3627194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/^(\+.*(?:do|\))){/$1 {/;
36283705ce5bSJoe Perches			}
3629de7d4f0eSAndy Whitcroft		}
3630de7d4f0eSAndy Whitcroft
3631c4a62ef9SJoe Perches## # check for blank lines before declarations
3632c4a62ef9SJoe Perches##		if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
3633c4a62ef9SJoe Perches##		    $prevrawline =~ /^.\s*$/) {
3634c4a62ef9SJoe Perches##			WARN("SPACING",
3635c4a62ef9SJoe Perches##			     "No blank lines before declarations\n" . $hereprev);
3636c4a62ef9SJoe Perches##		}
3637c4a62ef9SJoe Perches##
3638c4a62ef9SJoe Perches
3639de7d4f0eSAndy Whitcroft# closing brace should have a space following it when it has anything
3640de7d4f0eSAndy Whitcroft# on the line
3641de7d4f0eSAndy Whitcroft		if ($line =~ /}(?!(?:,|;|\)))\S/) {
3642d5e616fcSJoe Perches			if (ERROR("SPACING",
3643d5e616fcSJoe Perches				  "space required after that close brace '}'\n" . $herecurr) &&
3644d5e616fcSJoe Perches			    $fix) {
3645194f66fcSJoe Perches				$fixed[$fixlinenr] =~
3646d5e616fcSJoe Perches				    s/}((?!(?:,|;|\)))\S)/} $1/;
3647d5e616fcSJoe Perches			}
36480a920b5bSAndy Whitcroft		}
36490a920b5bSAndy Whitcroft
365022f2a2efSAndy Whitcroft# check spacing on square brackets
365122f2a2efSAndy Whitcroft		if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
36523705ce5bSJoe Perches			if (ERROR("SPACING",
36533705ce5bSJoe Perches				  "space prohibited after that open square bracket '['\n" . $herecurr) &&
36543705ce5bSJoe Perches			    $fix) {
3655194f66fcSJoe Perches				$fixed[$fixlinenr] =~
36563705ce5bSJoe Perches				    s/\[\s+/\[/;
36573705ce5bSJoe Perches			}
365822f2a2efSAndy Whitcroft		}
365922f2a2efSAndy Whitcroft		if ($line =~ /\s\]/) {
36603705ce5bSJoe Perches			if (ERROR("SPACING",
36613705ce5bSJoe Perches				  "space prohibited before that close square bracket ']'\n" . $herecurr) &&
36623705ce5bSJoe Perches			    $fix) {
3663194f66fcSJoe Perches				$fixed[$fixlinenr] =~
36643705ce5bSJoe Perches				    s/\s+\]/\]/;
36653705ce5bSJoe Perches			}
366622f2a2efSAndy Whitcroft		}
366722f2a2efSAndy Whitcroft
3668c45dcabdSAndy Whitcroft# check spacing on parentheses
36699c0ca6f9SAndy Whitcroft		if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
36709c0ca6f9SAndy Whitcroft		    $line !~ /for\s*\(\s+;/) {
36713705ce5bSJoe Perches			if (ERROR("SPACING",
36723705ce5bSJoe Perches				  "space prohibited after that open parenthesis '('\n" . $herecurr) &&
36733705ce5bSJoe Perches			    $fix) {
3674194f66fcSJoe Perches				$fixed[$fixlinenr] =~
36753705ce5bSJoe Perches				    s/\(\s+/\(/;
36763705ce5bSJoe Perches			}
367722f2a2efSAndy Whitcroft		}
367813214adfSAndy Whitcroft		if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
3679c45dcabdSAndy Whitcroft		    $line !~ /for\s*\(.*;\s+\)/ &&
3680c45dcabdSAndy Whitcroft		    $line !~ /:\s+\)/) {
36813705ce5bSJoe Perches			if (ERROR("SPACING",
36823705ce5bSJoe Perches				  "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
36833705ce5bSJoe Perches			    $fix) {
3684194f66fcSJoe Perches				print("fixlinenr: <$fixlinenr> fixed[fixlinenr]: <$fixed[$fixlinenr]>\n");
3685194f66fcSJoe Perches				$fixed[$fixlinenr] =~
36863705ce5bSJoe Perches				    s/\s+\)/\)/;
36873705ce5bSJoe Perches			}
368822f2a2efSAndy Whitcroft		}
368922f2a2efSAndy Whitcroft
3690e2826fd0SJoe Perches# check unnecessary parentheses around addressof/dereference single $Lvals
3691e2826fd0SJoe Perches# ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar
3692e2826fd0SJoe Perches
3693e2826fd0SJoe Perches		while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) {
3694e2826fd0SJoe Perches			CHK("UNNECESSARY_PARENTHESES",
3695e2826fd0SJoe Perches			    "Unnecessary parentheses around $1\n" . $herecurr);
3696e2826fd0SJoe Perches		    }
3697e2826fd0SJoe Perches
36980a920b5bSAndy Whitcroft#goto labels aren't indented, allow a single space however
36994a0df2efSAndy Whitcroft		if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
37000a920b5bSAndy Whitcroft		   !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
37013705ce5bSJoe Perches			if (WARN("INDENTED_LABEL",
37023705ce5bSJoe Perches				 "labels should not be indented\n" . $herecurr) &&
37033705ce5bSJoe Perches			    $fix) {
3704194f66fcSJoe Perches				$fixed[$fixlinenr] =~
37053705ce5bSJoe Perches				    s/^(.)\s+/$1/;
37063705ce5bSJoe Perches			}
37070a920b5bSAndy Whitcroft		}
37080a920b5bSAndy Whitcroft
37095b9553abSJoe Perches# return is not a function
3710507e5141SJoe Perches		if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {
3711c45dcabdSAndy Whitcroft			my $spacing = $1;
3712507e5141SJoe Perches			if ($^V && $^V ge 5.10.0 &&
37135b9553abSJoe Perches			    $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) {
37145b9553abSJoe Perches				my $value = $1;
37155b9553abSJoe Perches				$value = deparenthesize($value);
37165b9553abSJoe Perches				if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) {
3717000d1cc1SJoe Perches					ERROR("RETURN_PARENTHESES",
3718000d1cc1SJoe Perches					      "return is not a function, parentheses are not required\n" . $herecurr);
37195b9553abSJoe Perches				}
3720c45dcabdSAndy Whitcroft			} elsif ($spacing !~ /\s+/) {
3721000d1cc1SJoe Perches				ERROR("SPACING",
3722000d1cc1SJoe Perches				      "space required before the open parenthesis '('\n" . $herecurr);
3723c45dcabdSAndy Whitcroft			}
3724c45dcabdSAndy Whitcroft		}
3725507e5141SJoe Perches
3726b43ae21bSJoe Perches# unnecessary return in a void function
3727b43ae21bSJoe Perches# at end-of-function, with the previous line a single leading tab, then return;
3728b43ae21bSJoe Perches# and the line before that not a goto label target like "out:"
3729b43ae21bSJoe Perches		if ($sline =~ /^[ \+]}\s*$/ &&
3730b43ae21bSJoe Perches		    $prevline =~ /^\+\treturn\s*;\s*$/ &&
3731b43ae21bSJoe Perches		    $linenr >= 3 &&
3732b43ae21bSJoe Perches		    $lines[$linenr - 3] =~ /^[ +]/ &&
3733b43ae21bSJoe Perches		    $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) {
37349819cf25SJoe Perches			WARN("RETURN_VOID",
3735b43ae21bSJoe Perches			     "void function return statements are not generally useful\n" . $hereprev);
37369819cf25SJoe Perches               }
37379819cf25SJoe Perches
3738189248d8SJoe Perches# if statements using unnecessary parentheses - ie: if ((foo == bar))
3739189248d8SJoe Perches		if ($^V && $^V ge 5.10.0 &&
3740189248d8SJoe Perches		    $line =~ /\bif\s*((?:\(\s*){2,})/) {
3741189248d8SJoe Perches			my $openparens = $1;
3742189248d8SJoe Perches			my $count = $openparens =~ tr@\(@\(@;
3743189248d8SJoe Perches			my $msg = "";
3744189248d8SJoe Perches			if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) {
3745189248d8SJoe Perches				my $comp = $4;	#Not $1 because of $LvalOrFunc
3746189248d8SJoe Perches				$msg = " - maybe == should be = ?" if ($comp eq "==");
3747189248d8SJoe Perches				WARN("UNNECESSARY_PARENTHESES",
3748189248d8SJoe Perches				     "Unnecessary parentheses$msg\n" . $herecurr);
3749189248d8SJoe Perches			}
3750189248d8SJoe Perches		}
3751189248d8SJoe Perches
375253a3c448SAndy Whitcroft# Return of what appears to be an errno should normally be -'ve
375353a3c448SAndy Whitcroft		if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) {
375453a3c448SAndy Whitcroft			my $name = $1;
375553a3c448SAndy Whitcroft			if ($name ne 'EOF' && $name ne 'ERROR') {
3756000d1cc1SJoe Perches				WARN("USE_NEGATIVE_ERRNO",
3757000d1cc1SJoe Perches				     "return of an errno should typically be -ve (return -$1)\n" . $herecurr);
375853a3c448SAndy Whitcroft			}
375953a3c448SAndy Whitcroft		}
3760c45dcabdSAndy Whitcroft
37610a920b5bSAndy Whitcroft# Need a space before open parenthesis after if, while etc
37624a0df2efSAndy Whitcroft		if ($line =~ /\b(if|while|for|switch)\(/) {
37633705ce5bSJoe Perches			if (ERROR("SPACING",
37643705ce5bSJoe Perches				  "space required before the open parenthesis '('\n" . $herecurr) &&
37653705ce5bSJoe Perches			    $fix) {
3766194f66fcSJoe Perches				$fixed[$fixlinenr] =~
37673705ce5bSJoe Perches				    s/\b(if|while|for|switch)\(/$1 \(/;
37683705ce5bSJoe Perches			}
37690a920b5bSAndy Whitcroft		}
37700a920b5bSAndy Whitcroft
3771f5fe35ddSAndy Whitcroft# Check for illegal assignment in if conditional -- and check for trailing
3772f5fe35ddSAndy Whitcroft# statements after the conditional.
3773170d3a22SAndy Whitcroft		if ($line =~ /do\s*(?!{)/) {
37743e469cdcSAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
37753e469cdcSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0)
37763e469cdcSAndy Whitcroft					if (!defined $stat);
3777170d3a22SAndy Whitcroft			my ($stat_next) = ctx_statement_block($line_nr_next,
3778170d3a22SAndy Whitcroft						$remain_next, $off_next);
3779170d3a22SAndy Whitcroft			$stat_next =~ s/\n./\n /g;
3780170d3a22SAndy Whitcroft			##print "stat<$stat> stat_next<$stat_next>\n";
3781170d3a22SAndy Whitcroft
3782170d3a22SAndy Whitcroft			if ($stat_next =~ /^\s*while\b/) {
3783170d3a22SAndy Whitcroft				# If the statement carries leading newlines,
3784170d3a22SAndy Whitcroft				# then count those as offsets.
3785170d3a22SAndy Whitcroft				my ($whitespace) =
3786170d3a22SAndy Whitcroft					($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
3787170d3a22SAndy Whitcroft				my $offset =
3788170d3a22SAndy Whitcroft					statement_rawlines($whitespace) - 1;
3789170d3a22SAndy Whitcroft
3790170d3a22SAndy Whitcroft				$suppress_whiletrailers{$line_nr_next +
3791170d3a22SAndy Whitcroft								$offset} = 1;
3792170d3a22SAndy Whitcroft			}
3793170d3a22SAndy Whitcroft		}
3794170d3a22SAndy Whitcroft		if (!defined $suppress_whiletrailers{$linenr} &&
3795c11230f4SJoe Perches		    defined($stat) && defined($cond) &&
3796170d3a22SAndy Whitcroft		    $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
3797171ae1a4SAndy Whitcroft			my ($s, $c) = ($stat, $cond);
37988905a67cSAndy Whitcroft
3799b53c8e10SAndy Whitcroft			if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
3800000d1cc1SJoe Perches				ERROR("ASSIGN_IN_IF",
3801000d1cc1SJoe Perches				      "do not use assignment in if condition\n" . $herecurr);
38028905a67cSAndy Whitcroft			}
38038905a67cSAndy Whitcroft
38048905a67cSAndy Whitcroft			# Find out what is on the end of the line after the
38058905a67cSAndy Whitcroft			# conditional.
3806773647a0SAndy Whitcroft			substr($s, 0, length($c), '');
38078905a67cSAndy Whitcroft			$s =~ s/\n.*//g;
380813214adfSAndy Whitcroft			$s =~ s/$;//g; 	# Remove any comments
380953210168SAndy Whitcroft			if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
381053210168SAndy Whitcroft			    $c !~ /}\s*while\s*/)
3811773647a0SAndy Whitcroft			{
3812bb44ad39SAndy Whitcroft				# Find out how long the conditional actually is.
3813bb44ad39SAndy Whitcroft				my @newlines = ($c =~ /\n/gs);
3814bb44ad39SAndy Whitcroft				my $cond_lines = 1 + $#newlines;
381542bdf74cSHidetoshi Seto				my $stat_real = '';
3816bb44ad39SAndy Whitcroft
381742bdf74cSHidetoshi Seto				$stat_real = raw_line($linenr, $cond_lines)
381842bdf74cSHidetoshi Seto							. "\n" if ($cond_lines);
3819bb44ad39SAndy Whitcroft				if (defined($stat_real) && $cond_lines > 1) {
3820bb44ad39SAndy Whitcroft					$stat_real = "[...]\n$stat_real";
3821bb44ad39SAndy Whitcroft				}
3822bb44ad39SAndy Whitcroft
3823000d1cc1SJoe Perches				ERROR("TRAILING_STATEMENTS",
3824000d1cc1SJoe Perches				      "trailing statements should be on next line\n" . $herecurr . $stat_real);
38258905a67cSAndy Whitcroft			}
38268905a67cSAndy Whitcroft		}
38278905a67cSAndy Whitcroft
382813214adfSAndy Whitcroft# Check for bitwise tests written as boolean
382913214adfSAndy Whitcroft		if ($line =~ /
383013214adfSAndy Whitcroft			(?:
383113214adfSAndy Whitcroft				(?:\[|\(|\&\&|\|\|)
383213214adfSAndy Whitcroft				\s*0[xX][0-9]+\s*
383313214adfSAndy Whitcroft				(?:\&\&|\|\|)
383413214adfSAndy Whitcroft			|
383513214adfSAndy Whitcroft				(?:\&\&|\|\|)
383613214adfSAndy Whitcroft				\s*0[xX][0-9]+\s*
383713214adfSAndy Whitcroft				(?:\&\&|\|\||\)|\])
383813214adfSAndy Whitcroft			)/x)
383913214adfSAndy Whitcroft		{
3840000d1cc1SJoe Perches			WARN("HEXADECIMAL_BOOLEAN_TEST",
3841000d1cc1SJoe Perches			     "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
384213214adfSAndy Whitcroft		}
384313214adfSAndy Whitcroft
38448905a67cSAndy Whitcroft# if and else should not have general statements after it
384513214adfSAndy Whitcroft		if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
384613214adfSAndy Whitcroft			my $s = $1;
384713214adfSAndy Whitcroft			$s =~ s/$;//g; 	# Remove any comments
384813214adfSAndy Whitcroft			if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
3849000d1cc1SJoe Perches				ERROR("TRAILING_STATEMENTS",
3850000d1cc1SJoe Perches				      "trailing statements should be on next line\n" . $herecurr);
38510a920b5bSAndy Whitcroft			}
385213214adfSAndy Whitcroft		}
385339667782SAndy Whitcroft# if should not continue a brace
385439667782SAndy Whitcroft		if ($line =~ /}\s*if\b/) {
3855000d1cc1SJoe Perches			ERROR("TRAILING_STATEMENTS",
3856048b123fSRasmus Villemoes			      "trailing statements should be on next line (or did you mean 'else if'?)\n" .
385739667782SAndy Whitcroft				$herecurr);
385839667782SAndy Whitcroft		}
3859a1080bf8SAndy Whitcroft# case and default should not have general statements after them
3860a1080bf8SAndy Whitcroft		if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
3861a1080bf8SAndy Whitcroft		    $line !~ /\G(?:
38623fef12d6SAndy Whitcroft			(?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
3863a1080bf8SAndy Whitcroft			\s*return\s+
3864a1080bf8SAndy Whitcroft		    )/xg)
3865a1080bf8SAndy Whitcroft		{
3866000d1cc1SJoe Perches			ERROR("TRAILING_STATEMENTS",
3867000d1cc1SJoe Perches			      "trailing statements should be on next line\n" . $herecurr);
3868a1080bf8SAndy Whitcroft		}
38690a920b5bSAndy Whitcroft
38700a920b5bSAndy Whitcroft		# Check for }<nl>else {, these must be at the same
38710a920b5bSAndy Whitcroft		# indent level to be relevant to each other.
38720a920b5bSAndy Whitcroft		if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
38730a920b5bSAndy Whitcroft						$previndent == $indent) {
3874000d1cc1SJoe Perches			ERROR("ELSE_AFTER_BRACE",
3875000d1cc1SJoe Perches			      "else should follow close brace '}'\n" . $hereprev);
38760a920b5bSAndy Whitcroft		}
38770a920b5bSAndy Whitcroft
3878c2fdda0dSAndy Whitcroft		if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and
3879c2fdda0dSAndy Whitcroft						$previndent == $indent) {
3880c2fdda0dSAndy Whitcroft			my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
3881c2fdda0dSAndy Whitcroft
3882c2fdda0dSAndy Whitcroft			# Find out what is on the end of the line after the
3883c2fdda0dSAndy Whitcroft			# conditional.
3884773647a0SAndy Whitcroft			substr($s, 0, length($c), '');
3885c2fdda0dSAndy Whitcroft			$s =~ s/\n.*//g;
3886c2fdda0dSAndy Whitcroft
3887c2fdda0dSAndy Whitcroft			if ($s =~ /^\s*;/) {
3888000d1cc1SJoe Perches				ERROR("WHILE_AFTER_BRACE",
3889000d1cc1SJoe Perches				      "while should follow close brace '}'\n" . $hereprev);
3890c2fdda0dSAndy Whitcroft			}
3891c2fdda0dSAndy Whitcroft		}
3892c2fdda0dSAndy Whitcroft
389395e2c602SJoe Perches#Specific variable tests
3894323c1260SJoe Perches		while ($line =~ m{($Constant|$Lval)}g) {
3895323c1260SJoe Perches			my $var = $1;
389695e2c602SJoe Perches
389795e2c602SJoe Perches#gcc binary extension
389895e2c602SJoe Perches			if ($var =~ /^$Binary$/) {
3899d5e616fcSJoe Perches				if (WARN("GCC_BINARY_CONSTANT",
3900d5e616fcSJoe Perches					 "Avoid gcc v4.3+ binary constant extension: <$var>\n" . $herecurr) &&
3901d5e616fcSJoe Perches				    $fix) {
3902d5e616fcSJoe Perches					my $hexval = sprintf("0x%x", oct($var));
3903194f66fcSJoe Perches					$fixed[$fixlinenr] =~
3904d5e616fcSJoe Perches					    s/\b$var\b/$hexval/;
3905d5e616fcSJoe Perches				}
390695e2c602SJoe Perches			}
390795e2c602SJoe Perches
390895e2c602SJoe Perches#CamelCase
3909807bd26cSJoe Perches			if ($var !~ /^$Constant$/ &&
3910be79794bSJoe Perches			    $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
391122735ce8SJoe Perches#Ignore Page<foo> variants
3912807bd26cSJoe Perches			    $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
391322735ce8SJoe Perches#Ignore SI style variants like nS, mV and dB (ie: max_uV, regulator_min_uA_show)
39143445686aSJoe Perches			    $var !~ /^(?:[a-z_]*?)_?[a-z][A-Z](?:_[a-z_]+)?$/) {
39157e781f67SJoe Perches				while ($var =~ m{($Ident)}g) {
39167e781f67SJoe Perches					my $word = $1;
39177e781f67SJoe Perches					next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
3918d8b07710SJoe Perches					if ($check) {
3919d8b07710SJoe Perches						seed_camelcase_includes();
3920d8b07710SJoe Perches						if (!$file && !$camelcase_file_seeded) {
3921d8b07710SJoe Perches							seed_camelcase_file($realfile);
3922d8b07710SJoe Perches							$camelcase_file_seeded = 1;
3923d8b07710SJoe Perches						}
3924d8b07710SJoe Perches					}
39257e781f67SJoe Perches					if (!defined $camelcase{$word}) {
39267e781f67SJoe Perches						$camelcase{$word} = 1;
3927be79794bSJoe Perches						CHK("CAMELCASE",
39287e781f67SJoe Perches						    "Avoid CamelCase: <$word>\n" . $herecurr);
39297e781f67SJoe Perches					}
3930323c1260SJoe Perches				}
3931323c1260SJoe Perches			}
39323445686aSJoe Perches		}
39330a920b5bSAndy Whitcroft
39340a920b5bSAndy Whitcroft#no spaces allowed after \ in define
3935d5e616fcSJoe Perches		if ($line =~ /\#\s*define.*\\\s+$/) {
3936d5e616fcSJoe Perches			if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
3937d5e616fcSJoe Perches				 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
3938d5e616fcSJoe Perches			    $fix) {
3939194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\s+$//;
3940d5e616fcSJoe Perches			}
39410a920b5bSAndy Whitcroft		}
39420a920b5bSAndy Whitcroft
3943653d4876SAndy Whitcroft#warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
3944c45dcabdSAndy Whitcroft		if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
3945e09dec48SAndy Whitcroft			my $file = "$1.h";
3946e09dec48SAndy Whitcroft			my $checkfile = "include/linux/$file";
3947e09dec48SAndy Whitcroft			if (-f "$root/$checkfile" &&
3948e09dec48SAndy Whitcroft			    $realfile ne $checkfile &&
39497840a94cSWolfram Sang			    $1 !~ /$allowed_asm_includes/)
3950c45dcabdSAndy Whitcroft			{
3951e09dec48SAndy Whitcroft				if ($realfile =~ m{^arch/}) {
3952000d1cc1SJoe Perches					CHK("ARCH_INCLUDE_LINUX",
3953000d1cc1SJoe Perches					    "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
3954e09dec48SAndy Whitcroft				} else {
3955000d1cc1SJoe Perches					WARN("INCLUDE_LINUX",
3956000d1cc1SJoe Perches					     "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
3957e09dec48SAndy Whitcroft				}
39580a920b5bSAndy Whitcroft			}
39590a920b5bSAndy Whitcroft		}
39600a920b5bSAndy Whitcroft
3961653d4876SAndy Whitcroft# multi-statement macros should be enclosed in a do while loop, grab the
3962653d4876SAndy Whitcroft# first statement and ensure its the whole macro if its not enclosed
3963cf655043SAndy Whitcroft# in a known good container
3964b8f96a31SAndy Whitcroft		if ($realfile !~ m@/vmlinux.lds.h$@ &&
3965b8f96a31SAndy Whitcroft		    $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
3966d8aaf121SAndy Whitcroft			my $ln = $linenr;
3967d8aaf121SAndy Whitcroft			my $cnt = $realcnt;
3968c45dcabdSAndy Whitcroft			my ($off, $dstat, $dcond, $rest);
3969c45dcabdSAndy Whitcroft			my $ctx = '';
3970c45dcabdSAndy Whitcroft			($dstat, $dcond, $ln, $cnt, $off) =
3971f74bd194SAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0);
3972f74bd194SAndy Whitcroft			$ctx = $dstat;
3973c45dcabdSAndy Whitcroft			#print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
3974a3bb97a7SAndy Whitcroft			#print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
3975c45dcabdSAndy Whitcroft
3976f74bd194SAndy Whitcroft			$dstat =~ s/^.\s*\#\s*define\s+$Ident(?:\([^\)]*\))?\s*//;
3977292f1a9bSAndy Whitcroft			$dstat =~ s/$;//g;
3978c45dcabdSAndy Whitcroft			$dstat =~ s/\\\n.//g;
3979c45dcabdSAndy Whitcroft			$dstat =~ s/^\s*//s;
3980c45dcabdSAndy Whitcroft			$dstat =~ s/\s*$//s;
3981c45dcabdSAndy Whitcroft
3982c45dcabdSAndy Whitcroft			# Flatten any parentheses and braces
3983bf30d6edSAndy Whitcroft			while ($dstat =~ s/\([^\(\)]*\)/1/ ||
3984bf30d6edSAndy Whitcroft			       $dstat =~ s/\{[^\{\}]*\}/1/ ||
3985c81769fdSAndy Whitcroft			       $dstat =~ s/\[[^\[\]]*\]/1/)
3986bf30d6edSAndy Whitcroft			{
3987c45dcabdSAndy Whitcroft			}
3988c45dcabdSAndy Whitcroft
3989e45bab8eSAndy Whitcroft			# Flatten any obvious string concatentation.
3990e45bab8eSAndy Whitcroft			while ($dstat =~ s/("X*")\s*$Ident/$1/ ||
3991e45bab8eSAndy Whitcroft			       $dstat =~ s/$Ident\s*("X*")/$1/)
3992e45bab8eSAndy Whitcroft			{
3993e45bab8eSAndy Whitcroft			}
3994e45bab8eSAndy Whitcroft
3995c45dcabdSAndy Whitcroft			my $exceptions = qr{
3996c45dcabdSAndy Whitcroft				$Declare|
3997c45dcabdSAndy Whitcroft				module_param_named|
3998a0a0a7a9SKees Cook				MODULE_PARM_DESC|
3999c45dcabdSAndy Whitcroft				DECLARE_PER_CPU|
4000c45dcabdSAndy Whitcroft				DEFINE_PER_CPU|
4001383099fdSAndy Whitcroft				__typeof__\(|
400222fd2d3eSStefani Seibold				union|
400322fd2d3eSStefani Seibold				struct|
4004ea71a0a0SAndy Whitcroft				\.$Ident\s*=\s*|
4005ea71a0a0SAndy Whitcroft				^\"|\"$
4006c45dcabdSAndy Whitcroft			}x;
40075eaa20b9SAndy Whitcroft			#print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
4008f74bd194SAndy Whitcroft			if ($dstat ne '' &&
4009f74bd194SAndy Whitcroft			    $dstat !~ /^(?:$Ident|-?$Constant),$/ &&			# 10, // foo(),
4010f74bd194SAndy Whitcroft			    $dstat !~ /^(?:$Ident|-?$Constant);$/ &&			# foo();
40113cc4b1c3SJoe Perches			    $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ &&		# 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
4012356fd398SJoe Perches			    $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ &&			# character constants
4013f74bd194SAndy Whitcroft			    $dstat !~ /$exceptions/ &&
4014f74bd194SAndy Whitcroft			    $dstat !~ /^\.$Ident\s*=/ &&				# .foo =
4015e942e2c3SJoe Perches			    $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ &&		# stringification #foo
401672f115f9SAndy Whitcroft			    $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ &&	# do {...} while (...); // do {...} while (...)
4017f74bd194SAndy Whitcroft			    $dstat !~ /^for\s*$Constant$/ &&				# for (...)
4018f74bd194SAndy Whitcroft			    $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ &&	# for (...) bar()
4019f74bd194SAndy Whitcroft			    $dstat !~ /^do\s*{/ &&					# do {...
4020f95a7e6aSJoe Perches			    $dstat !~ /^\({/ &&						# ({...
4021f95a7e6aSJoe Perches			    $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
4022c45dcabdSAndy Whitcroft			{
4023f74bd194SAndy Whitcroft				$ctx =~ s/\n*$//;
4024f74bd194SAndy Whitcroft				my $herectx = $here . "\n";
4025f74bd194SAndy Whitcroft				my $cnt = statement_rawlines($ctx);
4026f74bd194SAndy Whitcroft
4027f74bd194SAndy Whitcroft				for (my $n = 0; $n < $cnt; $n++) {
4028f74bd194SAndy Whitcroft					$herectx .= raw_line($linenr, $n) . "\n";
4029c45dcabdSAndy Whitcroft				}
4030c45dcabdSAndy Whitcroft
4031f74bd194SAndy Whitcroft				if ($dstat =~ /;/) {
4032f74bd194SAndy Whitcroft					ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
4033f74bd194SAndy Whitcroft					      "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
4034f74bd194SAndy Whitcroft				} else {
4035000d1cc1SJoe Perches					ERROR("COMPLEX_MACRO",
4036f74bd194SAndy Whitcroft					      "Macros with complex values should be enclosed in parenthesis\n" . "$herectx");
4037d8aaf121SAndy Whitcroft				}
40380a920b5bSAndy Whitcroft			}
40395023d347SJoe Perches
4040481eb486SJoe Perches# check for line continuations outside of #defines, preprocessor #, and asm
40415023d347SJoe Perches
40425023d347SJoe Perches		} else {
40435023d347SJoe Perches			if ($prevline !~ /^..*\\$/ &&
4044481eb486SJoe Perches			    $line !~ /^\+\s*\#.*\\$/ &&		# preprocessor
4045481eb486SJoe Perches			    $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ &&	# asm
40465023d347SJoe Perches			    $line =~ /^\+.*\\$/) {
40475023d347SJoe Perches				WARN("LINE_CONTINUATIONS",
40485023d347SJoe Perches				     "Avoid unnecessary line continuations\n" . $herecurr);
40495023d347SJoe Perches			}
4050653d4876SAndy Whitcroft		}
40510a920b5bSAndy Whitcroft
4052b13edf7fSJoe Perches# do {} while (0) macro tests:
4053b13edf7fSJoe Perches# single-statement macros do not need to be enclosed in do while (0) loop,
4054b13edf7fSJoe Perches# macro should not end with a semicolon
4055b13edf7fSJoe Perches		if ($^V && $^V ge 5.10.0 &&
4056b13edf7fSJoe Perches		    $realfile !~ m@/vmlinux.lds.h$@ &&
4057b13edf7fSJoe Perches		    $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
4058b13edf7fSJoe Perches			my $ln = $linenr;
4059b13edf7fSJoe Perches			my $cnt = $realcnt;
4060b13edf7fSJoe Perches			my ($off, $dstat, $dcond, $rest);
4061b13edf7fSJoe Perches			my $ctx = '';
4062b13edf7fSJoe Perches			($dstat, $dcond, $ln, $cnt, $off) =
4063b13edf7fSJoe Perches				ctx_statement_block($linenr, $realcnt, 0);
4064b13edf7fSJoe Perches			$ctx = $dstat;
4065b13edf7fSJoe Perches
4066b13edf7fSJoe Perches			$dstat =~ s/\\\n.//g;
4067b13edf7fSJoe Perches
4068b13edf7fSJoe Perches			if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
4069b13edf7fSJoe Perches				my $stmts = $2;
4070b13edf7fSJoe Perches				my $semis = $3;
4071b13edf7fSJoe Perches
4072b13edf7fSJoe Perches				$ctx =~ s/\n*$//;
4073b13edf7fSJoe Perches				my $cnt = statement_rawlines($ctx);
4074b13edf7fSJoe Perches				my $herectx = $here . "\n";
4075b13edf7fSJoe Perches
4076b13edf7fSJoe Perches				for (my $n = 0; $n < $cnt; $n++) {
4077b13edf7fSJoe Perches					$herectx .= raw_line($linenr, $n) . "\n";
4078b13edf7fSJoe Perches				}
4079b13edf7fSJoe Perches
4080ac8e97f8SJoe Perches				if (($stmts =~ tr/;/;/) == 1 &&
4081ac8e97f8SJoe Perches				    $stmts !~ /^\s*(if|while|for|switch)\b/) {
4082b13edf7fSJoe Perches					WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
4083b13edf7fSJoe Perches					     "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
4084b13edf7fSJoe Perches				}
4085b13edf7fSJoe Perches				if (defined $semis && $semis ne "") {
4086b13edf7fSJoe Perches					WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
4087b13edf7fSJoe Perches					     "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
4088b13edf7fSJoe Perches				}
4089f5ef95b1SJoe Perches			} elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) {
4090f5ef95b1SJoe Perches				$ctx =~ s/\n*$//;
4091f5ef95b1SJoe Perches				my $cnt = statement_rawlines($ctx);
4092f5ef95b1SJoe Perches				my $herectx = $here . "\n";
4093f5ef95b1SJoe Perches
4094f5ef95b1SJoe Perches				for (my $n = 0; $n < $cnt; $n++) {
4095f5ef95b1SJoe Perches					$herectx .= raw_line($linenr, $n) . "\n";
4096f5ef95b1SJoe Perches				}
4097f5ef95b1SJoe Perches
4098f5ef95b1SJoe Perches				WARN("TRAILING_SEMICOLON",
4099f5ef95b1SJoe Perches				     "macros should not use a trailing semicolon\n" . "$herectx");
4100b13edf7fSJoe Perches			}
4101b13edf7fSJoe Perches		}
4102b13edf7fSJoe Perches
4103080ba929SMike Frysinger# make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
4104080ba929SMike Frysinger# all assignments may have only one of the following with an assignment:
4105080ba929SMike Frysinger#	.
4106080ba929SMike Frysinger#	ALIGN(...)
4107080ba929SMike Frysinger#	VMLINUX_SYMBOL(...)
4108080ba929SMike Frysinger		if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
4109000d1cc1SJoe Perches			WARN("MISSING_VMLINUX_SYMBOL",
4110000d1cc1SJoe Perches			     "vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
4111080ba929SMike Frysinger		}
4112080ba929SMike Frysinger
4113f0a594c1SAndy Whitcroft# check for redundant bracing round if etc
411413214adfSAndy Whitcroft		if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
411513214adfSAndy Whitcroft			my ($level, $endln, @chunks) =
4116cf655043SAndy Whitcroft				ctx_statement_full($linenr, $realcnt, 1);
411713214adfSAndy Whitcroft			#print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
4118cf655043SAndy Whitcroft			#print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
4119cf655043SAndy Whitcroft			if ($#chunks > 0 && $level == 0) {
4120aad4f614SJoe Perches				my @allowed = ();
4121aad4f614SJoe Perches				my $allow = 0;
412213214adfSAndy Whitcroft				my $seen = 0;
4123773647a0SAndy Whitcroft				my $herectx = $here . "\n";
4124cf655043SAndy Whitcroft				my $ln = $linenr - 1;
412513214adfSAndy Whitcroft				for my $chunk (@chunks) {
412613214adfSAndy Whitcroft					my ($cond, $block) = @{$chunk};
412713214adfSAndy Whitcroft
4128773647a0SAndy Whitcroft					# If the condition carries leading newlines, then count those as offsets.
4129773647a0SAndy Whitcroft					my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
4130773647a0SAndy Whitcroft					my $offset = statement_rawlines($whitespace) - 1;
4131773647a0SAndy Whitcroft
4132aad4f614SJoe Perches					$allowed[$allow] = 0;
4133773647a0SAndy Whitcroft					#print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
4134773647a0SAndy Whitcroft
4135773647a0SAndy Whitcroft					# We have looked at and allowed this specific line.
4136773647a0SAndy Whitcroft					$suppress_ifbraces{$ln + $offset} = 1;
4137773647a0SAndy Whitcroft
4138773647a0SAndy Whitcroft					$herectx .= "$rawlines[$ln + $offset]\n[...]\n";
4139cf655043SAndy Whitcroft					$ln += statement_rawlines($block) - 1;
4140cf655043SAndy Whitcroft
4141773647a0SAndy Whitcroft					substr($block, 0, length($cond), '');
414213214adfSAndy Whitcroft
414313214adfSAndy Whitcroft					$seen++ if ($block =~ /^\s*{/);
414413214adfSAndy Whitcroft
4145aad4f614SJoe Perches					#print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
4146cf655043SAndy Whitcroft					if (statement_lines($cond) > 1) {
4147cf655043SAndy Whitcroft						#print "APW: ALLOWED: cond<$cond>\n";
4148aad4f614SJoe Perches						$allowed[$allow] = 1;
414913214adfSAndy Whitcroft					}
415013214adfSAndy Whitcroft					if ($block =~/\b(?:if|for|while)\b/) {
4151cf655043SAndy Whitcroft						#print "APW: ALLOWED: block<$block>\n";
4152aad4f614SJoe Perches						$allowed[$allow] = 1;
415313214adfSAndy Whitcroft					}
4154cf655043SAndy Whitcroft					if (statement_block_size($block) > 1) {
4155cf655043SAndy Whitcroft						#print "APW: ALLOWED: lines block<$block>\n";
4156aad4f614SJoe Perches						$allowed[$allow] = 1;
415713214adfSAndy Whitcroft					}
4158aad4f614SJoe Perches					$allow++;
415913214adfSAndy Whitcroft				}
4160aad4f614SJoe Perches				if ($seen) {
4161aad4f614SJoe Perches					my $sum_allowed = 0;
4162aad4f614SJoe Perches					foreach (@allowed) {
4163aad4f614SJoe Perches						$sum_allowed += $_;
4164aad4f614SJoe Perches					}
4165aad4f614SJoe Perches					if ($sum_allowed == 0) {
4166000d1cc1SJoe Perches						WARN("BRACES",
4167000d1cc1SJoe Perches						     "braces {} are not necessary for any arm of this statement\n" . $herectx);
4168aad4f614SJoe Perches					} elsif ($sum_allowed != $allow &&
4169aad4f614SJoe Perches						 $seen != $allow) {
4170aad4f614SJoe Perches						CHK("BRACES",
4171aad4f614SJoe Perches						    "braces {} should be used on all arms of this statement\n" . $herectx);
4172aad4f614SJoe Perches					}
417313214adfSAndy Whitcroft				}
417413214adfSAndy Whitcroft			}
417513214adfSAndy Whitcroft		}
4176773647a0SAndy Whitcroft		if (!defined $suppress_ifbraces{$linenr - 1} &&
417713214adfSAndy Whitcroft					$line =~ /\b(if|while|for|else)\b/) {
4178cf655043SAndy Whitcroft			my $allowed = 0;
4179f0a594c1SAndy Whitcroft
4180cf655043SAndy Whitcroft			# Check the pre-context.
4181cf655043SAndy Whitcroft			if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
4182cf655043SAndy Whitcroft				#print "APW: ALLOWED: pre<$1>\n";
4183cf655043SAndy Whitcroft				$allowed = 1;
4184f0a594c1SAndy Whitcroft			}
4185773647a0SAndy Whitcroft
4186773647a0SAndy Whitcroft			my ($level, $endln, @chunks) =
4187773647a0SAndy Whitcroft				ctx_statement_full($linenr, $realcnt, $-[0]);
4188773647a0SAndy Whitcroft
4189cf655043SAndy Whitcroft			# Check the condition.
4190cf655043SAndy Whitcroft			my ($cond, $block) = @{$chunks[0]};
4191773647a0SAndy Whitcroft			#print "CHECKING<$linenr> cond<$cond> block<$block>\n";
4192cf655043SAndy Whitcroft			if (defined $cond) {
4193773647a0SAndy Whitcroft				substr($block, 0, length($cond), '');
4194cf655043SAndy Whitcroft			}
4195cf655043SAndy Whitcroft			if (statement_lines($cond) > 1) {
4196cf655043SAndy Whitcroft				#print "APW: ALLOWED: cond<$cond>\n";
4197cf655043SAndy Whitcroft				$allowed = 1;
4198cf655043SAndy Whitcroft			}
4199cf655043SAndy Whitcroft			if ($block =~/\b(?:if|for|while)\b/) {
4200cf655043SAndy Whitcroft				#print "APW: ALLOWED: block<$block>\n";
4201cf655043SAndy Whitcroft				$allowed = 1;
4202cf655043SAndy Whitcroft			}
4203cf655043SAndy Whitcroft			if (statement_block_size($block) > 1) {
4204cf655043SAndy Whitcroft				#print "APW: ALLOWED: lines block<$block>\n";
4205cf655043SAndy Whitcroft				$allowed = 1;
4206cf655043SAndy Whitcroft			}
4207cf655043SAndy Whitcroft			# Check the post-context.
4208cf655043SAndy Whitcroft			if (defined $chunks[1]) {
4209cf655043SAndy Whitcroft				my ($cond, $block) = @{$chunks[1]};
4210cf655043SAndy Whitcroft				if (defined $cond) {
4211773647a0SAndy Whitcroft					substr($block, 0, length($cond), '');
4212cf655043SAndy Whitcroft				}
4213cf655043SAndy Whitcroft				if ($block =~ /^\s*\{/) {
4214cf655043SAndy Whitcroft					#print "APW: ALLOWED: chunk-1 block<$block>\n";
4215cf655043SAndy Whitcroft					$allowed = 1;
4216cf655043SAndy Whitcroft				}
4217cf655043SAndy Whitcroft			}
4218cf655043SAndy Whitcroft			if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
421969932487SJustin P. Mattock				my $herectx = $here . "\n";
4220f055663cSAndy Whitcroft				my $cnt = statement_rawlines($block);
4221cf655043SAndy Whitcroft
4222f055663cSAndy Whitcroft				for (my $n = 0; $n < $cnt; $n++) {
422369932487SJustin P. Mattock					$herectx .= raw_line($linenr, $n) . "\n";
4224cf655043SAndy Whitcroft				}
4225cf655043SAndy Whitcroft
4226000d1cc1SJoe Perches				WARN("BRACES",
4227000d1cc1SJoe Perches				     "braces {} are not necessary for single statement blocks\n" . $herectx);
4228f0a594c1SAndy Whitcroft			}
4229f0a594c1SAndy Whitcroft		}
4230f0a594c1SAndy Whitcroft
42310979ae66SJoe Perches# check for unnecessary blank lines around braces
423277b9a53aSJoe Perches		if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
42330979ae66SJoe Perches			CHK("BRACES",
42340979ae66SJoe Perches			    "Blank lines aren't necessary before a close brace '}'\n" . $hereprev);
42350979ae66SJoe Perches		}
423677b9a53aSJoe Perches		if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
42370979ae66SJoe Perches			CHK("BRACES",
42380979ae66SJoe Perches			    "Blank lines aren't necessary after an open brace '{'\n" . $hereprev);
42390979ae66SJoe Perches		}
42400979ae66SJoe Perches
42414a0df2efSAndy Whitcroft# no volatiles please
42426c72ffaaSAndy Whitcroft		my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
42436c72ffaaSAndy Whitcroft		if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
4244000d1cc1SJoe Perches			WARN("VOLATILE",
4245000d1cc1SJoe Perches			     "Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
42464a0df2efSAndy Whitcroft		}
42474a0df2efSAndy Whitcroft
424800df344fSAndy Whitcroft# warn about #if 0
4249c45dcabdSAndy Whitcroft		if ($line =~ /^.\s*\#\s*if\s+0\b/) {
4250000d1cc1SJoe Perches			CHK("REDUNDANT_CODE",
4251000d1cc1SJoe Perches			    "if this code is redundant consider removing it\n" .
4252de7d4f0eSAndy Whitcroft				$herecurr);
42534a0df2efSAndy Whitcroft		}
42544a0df2efSAndy Whitcroft
425503df4b51SAndy Whitcroft# check for needless "if (<foo>) fn(<foo>)" uses
425603df4b51SAndy Whitcroft		if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
425703df4b51SAndy Whitcroft			my $expr = '\s*\(\s*' . quotemeta($1) . '\s*\)\s*;';
425803df4b51SAndy Whitcroft			if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?)$expr/) {
425903df4b51SAndy Whitcroft				WARN('NEEDLESS_IF',
426003df4b51SAndy Whitcroft				     "$1(NULL) is safe this check is probably not required\n" . $hereprev);
42614c432a8fSGreg Kroah-Hartman			}
42624c432a8fSGreg Kroah-Hartman		}
4263f0a594c1SAndy Whitcroft
4264ebfdc409SJoe Perches# check for unnecessary "Out of Memory" messages
4265ebfdc409SJoe Perches		if ($line =~ /^\+.*\b$logFunctions\s*\(/ &&
4266ebfdc409SJoe Perches		    $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ &&
4267ebfdc409SJoe Perches		    (defined $1 || defined $3) &&
4268ebfdc409SJoe Perches		    $linenr > 3) {
4269ebfdc409SJoe Perches			my $testval = $2;
4270ebfdc409SJoe Perches			my $testline = $lines[$linenr - 3];
4271ebfdc409SJoe Perches
4272ebfdc409SJoe Perches			my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0);
4273ebfdc409SJoe Perches#			print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n");
4274ebfdc409SJoe Perches
4275ebfdc409SJoe 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)/) {
4276ebfdc409SJoe Perches				WARN("OOM_MESSAGE",
4277ebfdc409SJoe Perches				     "Possible unnecessary 'out of memory' message\n" . $hereprev);
4278ebfdc409SJoe Perches			}
4279ebfdc409SJoe Perches		}
4280ebfdc409SJoe Perches
42818716de38SJoe Perches# check for bad placement of section $InitAttribute (e.g.: __initdata)
42828716de38SJoe Perches		if ($line =~ /(\b$InitAttribute\b)/) {
42838716de38SJoe Perches			my $attr = $1;
42848716de38SJoe Perches			if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
42858716de38SJoe Perches				my $ptr = $1;
42868716de38SJoe Perches				my $var = $2;
42878716de38SJoe Perches				if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
42888716de38SJoe Perches				      ERROR("MISPLACED_INIT",
42898716de38SJoe Perches					    "$attr should be placed after $var\n" . $herecurr)) ||
42908716de38SJoe Perches				     ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
42918716de38SJoe Perches				      WARN("MISPLACED_INIT",
42928716de38SJoe Perches					   "$attr should be placed after $var\n" . $herecurr))) &&
42938716de38SJoe Perches				    $fix) {
4294194f66fcSJoe 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;
42958716de38SJoe Perches				}
42968716de38SJoe Perches			}
42978716de38SJoe Perches		}
42988716de38SJoe Perches
4299e970b884SJoe Perches# check for $InitAttributeData (ie: __initdata) with const
4300e970b884SJoe Perches		if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
4301e970b884SJoe Perches			my $attr = $1;
4302e970b884SJoe Perches			$attr =~ /($InitAttributePrefix)(.*)/;
4303e970b884SJoe Perches			my $attr_prefix = $1;
4304e970b884SJoe Perches			my $attr_type = $2;
4305e970b884SJoe Perches			if (ERROR("INIT_ATTRIBUTE",
4306e970b884SJoe Perches				  "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
4307e970b884SJoe Perches			    $fix) {
4308194f66fcSJoe Perches				$fixed[$fixlinenr] =~
4309e970b884SJoe Perches				    s/$InitAttributeData/${attr_prefix}initconst/;
4310e970b884SJoe Perches			}
4311e970b884SJoe Perches		}
4312e970b884SJoe Perches
4313e970b884SJoe Perches# check for $InitAttributeConst (ie: __initconst) without const
4314e970b884SJoe Perches		if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
4315e970b884SJoe Perches			my $attr = $1;
4316e970b884SJoe Perches			if (ERROR("INIT_ATTRIBUTE",
4317e970b884SJoe Perches				  "Use of $attr requires a separate use of const\n" . $herecurr) &&
4318e970b884SJoe Perches			    $fix) {
4319194f66fcSJoe Perches				my $lead = $fixed[$fixlinenr] =~
4320e970b884SJoe Perches				    /(^\+\s*(?:static\s+))/;
4321e970b884SJoe Perches				$lead = rtrim($1);
4322e970b884SJoe Perches				$lead = "$lead " if ($lead !~ /^\+$/);
4323e970b884SJoe Perches				$lead = "${lead}const ";
4324194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/;
4325e970b884SJoe Perches			}
4326e970b884SJoe Perches		}
4327e970b884SJoe Perches
4328fbdb8138SJoe Perches# don't use __constant_<foo> functions outside of include/uapi/
4329fbdb8138SJoe Perches		if ($realfile !~ m@^include/uapi/@ &&
4330fbdb8138SJoe Perches		    $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) {
4331fbdb8138SJoe Perches			my $constant_func = $1;
4332fbdb8138SJoe Perches			my $func = $constant_func;
4333fbdb8138SJoe Perches			$func =~ s/^__constant_//;
4334fbdb8138SJoe Perches			if (WARN("CONSTANT_CONVERSION",
4335fbdb8138SJoe Perches				 "$constant_func should be $func\n" . $herecurr) &&
4336fbdb8138SJoe Perches			    $fix) {
4337194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g;
4338fbdb8138SJoe Perches			}
4339fbdb8138SJoe Perches		}
4340fbdb8138SJoe Perches
43411a15a250SPatrick Pannuto# prefer usleep_range over udelay
434237581c28SBruce Allan		if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
434343c1d77cSJoe Perches			my $delay = $1;
43441a15a250SPatrick Pannuto			# ignore udelay's < 10, however
434543c1d77cSJoe Perches			if (! ($delay < 10) ) {
4346000d1cc1SJoe Perches				CHK("USLEEP_RANGE",
434743c1d77cSJoe Perches				    "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $herecurr);
434843c1d77cSJoe Perches			}
434943c1d77cSJoe Perches			if ($delay > 2000) {
435043c1d77cSJoe Perches				WARN("LONG_UDELAY",
435143c1d77cSJoe Perches				     "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr);
43521a15a250SPatrick Pannuto			}
43531a15a250SPatrick Pannuto		}
43541a15a250SPatrick Pannuto
435509ef8725SPatrick Pannuto# warn about unexpectedly long msleep's
435609ef8725SPatrick Pannuto		if ($line =~ /\bmsleep\s*\((\d+)\);/) {
435709ef8725SPatrick Pannuto			if ($1 < 20) {
4358000d1cc1SJoe Perches				WARN("MSLEEP",
435943c1d77cSJoe Perches				     "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $herecurr);
436009ef8725SPatrick Pannuto			}
436109ef8725SPatrick Pannuto		}
436209ef8725SPatrick Pannuto
436336ec1939SJoe Perches# check for comparisons of jiffies
436436ec1939SJoe Perches		if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
436536ec1939SJoe Perches			WARN("JIFFIES_COMPARISON",
436636ec1939SJoe Perches			     "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
436736ec1939SJoe Perches		}
436836ec1939SJoe Perches
43699d7a34a5SJoe Perches# check for comparisons of get_jiffies_64()
43709d7a34a5SJoe Perches		if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
43719d7a34a5SJoe Perches			WARN("JIFFIES_COMPARISON",
43729d7a34a5SJoe Perches			     "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
43739d7a34a5SJoe Perches		}
43749d7a34a5SJoe Perches
437500df344fSAndy Whitcroft# warn about #ifdefs in C files
4376c45dcabdSAndy Whitcroft#		if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
437700df344fSAndy Whitcroft#			print "#ifdef in C files should be avoided\n";
437800df344fSAndy Whitcroft#			print "$herecurr";
437900df344fSAndy Whitcroft#			$clean = 0;
438000df344fSAndy Whitcroft#		}
438100df344fSAndy Whitcroft
438222f2a2efSAndy Whitcroft# warn about spacing in #ifdefs
4383c45dcabdSAndy Whitcroft		if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
43843705ce5bSJoe Perches			if (ERROR("SPACING",
43853705ce5bSJoe Perches				  "exactly one space required after that #$1\n" . $herecurr) &&
43863705ce5bSJoe Perches			    $fix) {
4387194f66fcSJoe Perches				$fixed[$fixlinenr] =~
43883705ce5bSJoe Perches				    s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
43893705ce5bSJoe Perches			}
43903705ce5bSJoe Perches
439122f2a2efSAndy Whitcroft		}
439222f2a2efSAndy Whitcroft
43934a0df2efSAndy Whitcroft# check for spinlock_t definitions without a comment.
4394171ae1a4SAndy Whitcroft		if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
4395171ae1a4SAndy Whitcroft		    $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
43964a0df2efSAndy Whitcroft			my $which = $1;
43974a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
4398000d1cc1SJoe Perches				CHK("UNCOMMENTED_DEFINITION",
4399000d1cc1SJoe Perches				    "$1 definition without comment\n" . $herecurr);
44004a0df2efSAndy Whitcroft			}
44014a0df2efSAndy Whitcroft		}
44024a0df2efSAndy Whitcroft# check for memory barriers without a comment.
44034a0df2efSAndy Whitcroft		if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
44044a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
4405c1fd7bb9SJoe Perches				WARN("MEMORY_BARRIER",
4406000d1cc1SJoe Perches				     "memory barrier without comment\n" . $herecurr);
44074a0df2efSAndy Whitcroft			}
44084a0df2efSAndy Whitcroft		}
44094a0df2efSAndy Whitcroft# check of hardware specific defines
4410c45dcabdSAndy Whitcroft		if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
4411000d1cc1SJoe Perches			CHK("ARCH_DEFINES",
4412000d1cc1SJoe Perches			    "architecture specific defines should be avoided\n" .  $herecurr);
44130a920b5bSAndy Whitcroft		}
4414653d4876SAndy Whitcroft
4415d4977c78STobias Klauser# Check that the storage class is at the beginning of a declaration
4416d4977c78STobias Klauser		if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
4417000d1cc1SJoe Perches			WARN("STORAGE_CLASS",
4418000d1cc1SJoe Perches			     "storage class should be at the beginning of the declaration\n" . $herecurr)
4419d4977c78STobias Klauser		}
4420d4977c78STobias Klauser
4421de7d4f0eSAndy Whitcroft# check the location of the inline attribute, that it is between
4422de7d4f0eSAndy Whitcroft# storage class and type.
44239c0ca6f9SAndy Whitcroft		if ($line =~ /\b$Type\s+$Inline\b/ ||
44249c0ca6f9SAndy Whitcroft		    $line =~ /\b$Inline\s+$Storage\b/) {
4425000d1cc1SJoe Perches			ERROR("INLINE_LOCATION",
4426000d1cc1SJoe Perches			      "inline keyword should sit between storage class and type\n" . $herecurr);
4427de7d4f0eSAndy Whitcroft		}
4428de7d4f0eSAndy Whitcroft
44298905a67cSAndy Whitcroft# Check for __inline__ and __inline, prefer inline
44302b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
44312b7ab453SJoe Perches		    $line =~ /\b(__inline__|__inline)\b/) {
4432d5e616fcSJoe Perches			if (WARN("INLINE",
4433d5e616fcSJoe Perches				 "plain inline is preferred over $1\n" . $herecurr) &&
4434d5e616fcSJoe Perches			    $fix) {
4435194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/;
4436d5e616fcSJoe Perches
4437d5e616fcSJoe Perches			}
44388905a67cSAndy Whitcroft		}
44398905a67cSAndy Whitcroft
44403d130fd0SJoe Perches# Check for __attribute__ packed, prefer __packed
44412b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
44422b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
4443000d1cc1SJoe Perches			WARN("PREFER_PACKED",
4444000d1cc1SJoe Perches			     "__packed is preferred over __attribute__((packed))\n" . $herecurr);
44453d130fd0SJoe Perches		}
44463d130fd0SJoe Perches
444739b7e287SJoe Perches# Check for __attribute__ aligned, prefer __aligned
44482b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
44492b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
4450000d1cc1SJoe Perches			WARN("PREFER_ALIGNED",
4451000d1cc1SJoe Perches			     "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
445239b7e287SJoe Perches		}
445339b7e287SJoe Perches
44545f14d3bdSJoe Perches# Check for __attribute__ format(printf, prefer __printf
44552b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
44562b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
4457d5e616fcSJoe Perches			if (WARN("PREFER_PRINTF",
4458d5e616fcSJoe Perches				 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) &&
4459d5e616fcSJoe Perches			    $fix) {
4460194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf\s*,\s*(.*)\)\s*\)\s*\)/"__printf(" . trim($1) . ")"/ex;
4461d5e616fcSJoe Perches
4462d5e616fcSJoe Perches			}
44635f14d3bdSJoe Perches		}
44645f14d3bdSJoe Perches
44656061d949SJoe Perches# Check for __attribute__ format(scanf, prefer __scanf
44662b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
44672b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
4468d5e616fcSJoe Perches			if (WARN("PREFER_SCANF",
4469d5e616fcSJoe Perches				 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) &&
4470d5e616fcSJoe Perches			    $fix) {
4471194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\s*,\s*(.*)\)\s*\)\s*\)/"__scanf(" . trim($1) . ")"/ex;
4472d5e616fcSJoe Perches			}
44736061d949SJoe Perches		}
44746061d949SJoe Perches
44758f53a9b8SJoe Perches# check for sizeof(&)
44768f53a9b8SJoe Perches		if ($line =~ /\bsizeof\s*\(\s*\&/) {
4477000d1cc1SJoe Perches			WARN("SIZEOF_ADDRESS",
4478000d1cc1SJoe Perches			     "sizeof(& should be avoided\n" . $herecurr);
44798f53a9b8SJoe Perches		}
44808f53a9b8SJoe Perches
448166c80b60SJoe Perches# check for sizeof without parenthesis
448266c80b60SJoe Perches		if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
4483d5e616fcSJoe Perches			if (WARN("SIZEOF_PARENTHESIS",
4484d5e616fcSJoe Perches				 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
4485d5e616fcSJoe Perches			    $fix) {
4486194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
4487d5e616fcSJoe Perches			}
448866c80b60SJoe Perches		}
448966c80b60SJoe Perches
4490428e2fdcSJoe Perches# check for line continuations in quoted strings with odd counts of "
4491428e2fdcSJoe Perches		if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) {
4492000d1cc1SJoe Perches			WARN("LINE_CONTINUATIONS",
4493000d1cc1SJoe Perches			     "Avoid line continuations in quoted strings\n" . $herecurr);
4494428e2fdcSJoe Perches		}
4495428e2fdcSJoe Perches
449688982feaSJoe Perches# check for struct spinlock declarations
449788982feaSJoe Perches		if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
449888982feaSJoe Perches			WARN("USE_SPINLOCK_T",
449988982feaSJoe Perches			     "struct spinlock should be spinlock_t\n" . $herecurr);
450088982feaSJoe Perches		}
450188982feaSJoe Perches
4502a6962d72SJoe Perches# check for seq_printf uses that could be seq_puts
450306668727SJoe Perches		if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
4504a6962d72SJoe Perches			my $fmt = get_quoted_string($line, $rawline);
450506668727SJoe Perches			if ($fmt ne "" && $fmt !~ /[^\\]\%/) {
4506d5e616fcSJoe Perches				if (WARN("PREFER_SEQ_PUTS",
4507d5e616fcSJoe Perches					 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
4508d5e616fcSJoe Perches				    $fix) {
4509194f66fcSJoe Perches					$fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/;
4510d5e616fcSJoe Perches				}
4511a6962d72SJoe Perches			}
4512a6962d72SJoe Perches		}
4513a6962d72SJoe Perches
4514554e165cSAndy Whitcroft# Check for misused memsets
4515d1fe9c09SJoe Perches		if ($^V && $^V ge 5.10.0 &&
4516d1fe9c09SJoe Perches		    defined $stat &&
4517d7c76ba7SJoe Perches		    $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/s) {
4518554e165cSAndy Whitcroft
4519d7c76ba7SJoe Perches			my $ms_addr = $2;
4520d1fe9c09SJoe Perches			my $ms_val = $7;
4521d1fe9c09SJoe Perches			my $ms_size = $12;
4522d7c76ba7SJoe Perches
4523554e165cSAndy Whitcroft			if ($ms_size =~ /^(0x|)0$/i) {
4524554e165cSAndy Whitcroft				ERROR("MEMSET",
4525d7c76ba7SJoe Perches				      "memset to 0's uses 0 as the 2nd argument, not the 3rd\n" . "$here\n$stat\n");
4526554e165cSAndy Whitcroft			} elsif ($ms_size =~ /^(0x|)1$/i) {
4527554e165cSAndy Whitcroft				WARN("MEMSET",
4528d7c76ba7SJoe Perches				     "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
4529d7c76ba7SJoe Perches			}
4530d7c76ba7SJoe Perches		}
4531d7c76ba7SJoe Perches
453298a9bba5SJoe Perches# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar)
453398a9bba5SJoe Perches		if ($^V && $^V ge 5.10.0 &&
453498a9bba5SJoe Perches		    $line =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/s) {
453598a9bba5SJoe Perches			if (WARN("PREFER_ETHER_ADDR_COPY",
453698a9bba5SJoe Perches				 "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . $herecurr) &&
453798a9bba5SJoe Perches			    $fix) {
4538194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/;
453998a9bba5SJoe Perches			}
454098a9bba5SJoe Perches		}
454198a9bba5SJoe Perches
4542d7c76ba7SJoe Perches# typecasts on min/max could be min_t/max_t
4543d1fe9c09SJoe Perches		if ($^V && $^V ge 5.10.0 &&
4544d1fe9c09SJoe Perches		    defined $stat &&
4545d7c76ba7SJoe Perches		    $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
4546d1fe9c09SJoe Perches			if (defined $2 || defined $7) {
4547d7c76ba7SJoe Perches				my $call = $1;
4548d7c76ba7SJoe Perches				my $cast1 = deparenthesize($2);
4549d7c76ba7SJoe Perches				my $arg1 = $3;
4550d1fe9c09SJoe Perches				my $cast2 = deparenthesize($7);
4551d1fe9c09SJoe Perches				my $arg2 = $8;
4552d7c76ba7SJoe Perches				my $cast;
4553d7c76ba7SJoe Perches
4554d1fe9c09SJoe Perches				if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
4555d7c76ba7SJoe Perches					$cast = "$cast1 or $cast2";
4556d7c76ba7SJoe Perches				} elsif ($cast1 ne "") {
4557d7c76ba7SJoe Perches					$cast = $cast1;
4558d7c76ba7SJoe Perches				} else {
4559d7c76ba7SJoe Perches					$cast = $cast2;
4560d7c76ba7SJoe Perches				}
4561d7c76ba7SJoe Perches				WARN("MINMAX",
4562d7c76ba7SJoe Perches				     "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
4563554e165cSAndy Whitcroft			}
4564554e165cSAndy Whitcroft		}
4565554e165cSAndy Whitcroft
45664a273195SJoe Perches# check usleep_range arguments
45674a273195SJoe Perches		if ($^V && $^V ge 5.10.0 &&
45684a273195SJoe Perches		    defined $stat &&
45694a273195SJoe Perches		    $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
45704a273195SJoe Perches			my $min = $1;
45714a273195SJoe Perches			my $max = $7;
45724a273195SJoe Perches			if ($min eq $max) {
45734a273195SJoe Perches				WARN("USLEEP_RANGE",
45744a273195SJoe Perches				     "usleep_range should not use min == max args; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
45754a273195SJoe Perches			} elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
45764a273195SJoe Perches				 $min > $max) {
45774a273195SJoe Perches				WARN("USLEEP_RANGE",
45784a273195SJoe Perches				     "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
45794a273195SJoe Perches			}
45804a273195SJoe Perches		}
45814a273195SJoe Perches
4582823b794cSJoe Perches# check for naked sscanf
4583823b794cSJoe Perches		if ($^V && $^V ge 5.10.0 &&
4584823b794cSJoe Perches		    defined $stat &&
45856c8bd707SJoe Perches		    $line =~ /\bsscanf\b/ &&
4586823b794cSJoe Perches		    ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
4587823b794cSJoe Perches		     $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ &&
4588823b794cSJoe Perches		     $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) {
4589823b794cSJoe Perches			my $lc = $stat =~ tr@\n@@;
4590823b794cSJoe Perches			$lc = $lc + $linenr;
4591823b794cSJoe Perches			my $stat_real = raw_line($linenr, 0);
4592823b794cSJoe Perches		        for (my $count = $linenr + 1; $count <= $lc; $count++) {
4593823b794cSJoe Perches				$stat_real = $stat_real . "\n" . raw_line($count, 0);
4594823b794cSJoe Perches			}
4595823b794cSJoe Perches			WARN("NAKED_SSCANF",
4596823b794cSJoe Perches			     "unchecked sscanf return value\n" . "$here\n$stat_real\n");
4597823b794cSJoe Perches		}
4598823b794cSJoe Perches
4599afc819abSJoe Perches# check for simple sscanf that should be kstrto<foo>
4600afc819abSJoe Perches		if ($^V && $^V ge 5.10.0 &&
4601afc819abSJoe Perches		    defined $stat &&
4602afc819abSJoe Perches		    $line =~ /\bsscanf\b/) {
4603afc819abSJoe Perches			my $lc = $stat =~ tr@\n@@;
4604afc819abSJoe Perches			$lc = $lc + $linenr;
4605afc819abSJoe Perches			my $stat_real = raw_line($linenr, 0);
4606afc819abSJoe Perches		        for (my $count = $linenr + 1; $count <= $lc; $count++) {
4607afc819abSJoe Perches				$stat_real = $stat_real . "\n" . raw_line($count, 0);
4608afc819abSJoe Perches			}
4609afc819abSJoe Perches			if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) {
4610afc819abSJoe Perches				my $format = $6;
4611afc819abSJoe Perches				my $count = $format =~ tr@%@%@;
4612afc819abSJoe Perches				if ($count == 1 &&
4613afc819abSJoe Perches				    $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) {
4614afc819abSJoe Perches					WARN("SSCANF_TO_KSTRTO",
4615afc819abSJoe Perches					     "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n");
4616afc819abSJoe Perches				}
4617afc819abSJoe Perches			}
4618afc819abSJoe Perches		}
4619afc819abSJoe Perches
462070dc8a48SJoe Perches# check for new externs in .h files.
462170dc8a48SJoe Perches		if ($realfile =~ /\.h$/ &&
462270dc8a48SJoe Perches		    $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
4623d1d85780SJoe Perches			if (CHK("AVOID_EXTERNS",
462470dc8a48SJoe Perches				"extern prototypes should be avoided in .h files\n" . $herecurr) &&
462570dc8a48SJoe Perches			    $fix) {
4626194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
462770dc8a48SJoe Perches			}
462870dc8a48SJoe Perches		}
462970dc8a48SJoe Perches
4630de7d4f0eSAndy Whitcroft# check for new externs in .c files.
4631171ae1a4SAndy Whitcroft		if ($realfile =~ /\.c$/ && defined $stat &&
4632c45dcabdSAndy Whitcroft		    $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
4633171ae1a4SAndy Whitcroft		{
4634c45dcabdSAndy Whitcroft			my $function_name = $1;
4635c45dcabdSAndy Whitcroft			my $paren_space = $2;
4636171ae1a4SAndy Whitcroft
4637171ae1a4SAndy Whitcroft			my $s = $stat;
4638171ae1a4SAndy Whitcroft			if (defined $cond) {
4639171ae1a4SAndy Whitcroft				substr($s, 0, length($cond), '');
4640171ae1a4SAndy Whitcroft			}
4641c45dcabdSAndy Whitcroft			if ($s =~ /^\s*;/ &&
4642c45dcabdSAndy Whitcroft			    $function_name ne 'uninitialized_var')
4643c45dcabdSAndy Whitcroft			{
4644000d1cc1SJoe Perches				WARN("AVOID_EXTERNS",
4645000d1cc1SJoe Perches				     "externs should be avoided in .c files\n" .  $herecurr);
4646de7d4f0eSAndy Whitcroft			}
4647de7d4f0eSAndy Whitcroft
4648171ae1a4SAndy Whitcroft			if ($paren_space =~ /\n/) {
4649000d1cc1SJoe Perches				WARN("FUNCTION_ARGUMENTS",
4650000d1cc1SJoe Perches				     "arguments for function declarations should follow identifier\n" . $herecurr);
4651171ae1a4SAndy Whitcroft			}
46529c9ba34eSAndy Whitcroft
46539c9ba34eSAndy Whitcroft		} elsif ($realfile =~ /\.c$/ && defined $stat &&
46549c9ba34eSAndy Whitcroft		    $stat =~ /^.\s*extern\s+/)
46559c9ba34eSAndy Whitcroft		{
4656000d1cc1SJoe Perches			WARN("AVOID_EXTERNS",
4657000d1cc1SJoe Perches			     "externs should be avoided in .c files\n" .  $herecurr);
4658171ae1a4SAndy Whitcroft		}
4659171ae1a4SAndy Whitcroft
4660de7d4f0eSAndy Whitcroft# checks for new __setup's
4661de7d4f0eSAndy Whitcroft		if ($rawline =~ /\b__setup\("([^"]*)"/) {
4662de7d4f0eSAndy Whitcroft			my $name = $1;
4663de7d4f0eSAndy Whitcroft
4664de7d4f0eSAndy Whitcroft			if (!grep(/$name/, @setup_docs)) {
4665000d1cc1SJoe Perches				CHK("UNDOCUMENTED_SETUP",
4666000d1cc1SJoe Perches				    "__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
4667de7d4f0eSAndy Whitcroft			}
4668653d4876SAndy Whitcroft		}
46699c0ca6f9SAndy Whitcroft
46709c0ca6f9SAndy Whitcroft# check for pointless casting of kmalloc return
4671caf2a54fSJoe Perches		if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) {
4672000d1cc1SJoe Perches			WARN("UNNECESSARY_CASTS",
4673000d1cc1SJoe Perches			     "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
46749c0ca6f9SAndy Whitcroft		}
467513214adfSAndy Whitcroft
4676a640d25cSJoe Perches# alloc style
4677a640d25cSJoe Perches# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
4678a640d25cSJoe Perches		if ($^V && $^V ge 5.10.0 &&
4679a640d25cSJoe Perches		    $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*([kv][mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
4680a640d25cSJoe Perches			CHK("ALLOC_SIZEOF_STRUCT",
4681a640d25cSJoe Perches			    "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
4682a640d25cSJoe Perches		}
4683a640d25cSJoe Perches
468460a55369SJoe Perches# check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc
468560a55369SJoe Perches		if ($^V && $^V ge 5.10.0 &&
4686e367455aSJoe Perches		    $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*(k[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)\s*,/) {
468760a55369SJoe Perches			my $oldfunc = $3;
468860a55369SJoe Perches			my $a1 = $4;
468960a55369SJoe Perches			my $a2 = $10;
469060a55369SJoe Perches			my $newfunc = "kmalloc_array";
469160a55369SJoe Perches			$newfunc = "kcalloc" if ($oldfunc eq "kzalloc");
469260a55369SJoe Perches			my $r1 = $a1;
469360a55369SJoe Perches			my $r2 = $a2;
469460a55369SJoe Perches			if ($a1 =~ /^sizeof\s*\S/) {
469560a55369SJoe Perches				$r1 = $a2;
469660a55369SJoe Perches				$r2 = $a1;
469760a55369SJoe Perches			}
4698e367455aSJoe Perches			if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ &&
4699e367455aSJoe Perches			    !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) {
4700e367455aSJoe Perches				if (WARN("ALLOC_WITH_MULTIPLY",
4701e367455aSJoe Perches					 "Prefer $newfunc over $oldfunc with multiply\n" . $herecurr) &&
4702e367455aSJoe Perches				    $fix) {
4703194f66fcSJoe 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;
470460a55369SJoe Perches
470560a55369SJoe Perches				}
470660a55369SJoe Perches			}
470760a55369SJoe Perches		}
470860a55369SJoe Perches
4709972fdea2SJoe Perches# check for krealloc arg reuse
4710972fdea2SJoe Perches		if ($^V && $^V ge 5.10.0 &&
4711972fdea2SJoe Perches		    $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*\1\s*,/) {
4712972fdea2SJoe Perches			WARN("KREALLOC_ARG_REUSE",
4713972fdea2SJoe Perches			     "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
4714972fdea2SJoe Perches		}
4715972fdea2SJoe Perches
47165ce59ae0SJoe Perches# check for alloc argument mismatch
47175ce59ae0SJoe Perches		if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
47185ce59ae0SJoe Perches			WARN("ALLOC_ARRAY_ARGS",
47195ce59ae0SJoe Perches			     "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
47205ce59ae0SJoe Perches		}
47215ce59ae0SJoe Perches
4722caf2a54fSJoe Perches# check for multiple semicolons
4723caf2a54fSJoe Perches		if ($line =~ /;\s*;\s*$/) {
4724d5e616fcSJoe Perches			if (WARN("ONE_SEMICOLON",
4725d5e616fcSJoe Perches				 "Statements terminations use 1 semicolon\n" . $herecurr) &&
4726d5e616fcSJoe Perches			    $fix) {
4727194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g;
4728d5e616fcSJoe Perches			}
4729d1e2ad07SJoe Perches		}
4730d1e2ad07SJoe Perches
4731c34c09a8SJoe Perches# check for case / default statements not preceeded by break/fallthrough/switch
4732c34c09a8SJoe Perches		if ($line =~ /^.\s*(?:case\s+(?:$Ident|$Constant)\s*|default):/) {
4733c34c09a8SJoe Perches			my $has_break = 0;
4734c34c09a8SJoe Perches			my $has_statement = 0;
4735c34c09a8SJoe Perches			my $count = 0;
4736c34c09a8SJoe Perches			my $prevline = $linenr;
4737c34c09a8SJoe Perches			while ($prevline > 1 && $count < 3 && !$has_break) {
4738c34c09a8SJoe Perches				$prevline--;
4739c34c09a8SJoe Perches				my $rline = $rawlines[$prevline - 1];
4740c34c09a8SJoe Perches				my $fline = $lines[$prevline - 1];
4741c34c09a8SJoe Perches				last if ($fline =~ /^\@\@/);
4742c34c09a8SJoe Perches				next if ($fline =~ /^\-/);
4743c34c09a8SJoe Perches				next if ($fline =~ /^.(?:\s*(?:case\s+(?:$Ident|$Constant)[\s$;]*|default):[\s$;]*)*$/);
4744c34c09a8SJoe Perches				$has_break = 1 if ($rline =~ /fall[\s_-]*(through|thru)/i);
4745c34c09a8SJoe Perches				next if ($fline =~ /^.[\s$;]*$/);
4746c34c09a8SJoe Perches				$has_statement = 1;
4747c34c09a8SJoe Perches				$count++;
4748c34c09a8SJoe Perches				$has_break = 1 if ($fline =~ /\bswitch\b|\b(?:break\s*;[\s$;]*$|return\b|goto\b|continue\b)/);
4749c34c09a8SJoe Perches			}
4750c34c09a8SJoe Perches			if (!$has_break && $has_statement) {
4751c34c09a8SJoe Perches				WARN("MISSING_BREAK",
4752c34c09a8SJoe Perches				     "Possible switch case/default not preceeded by break or fallthrough comment\n" . $herecurr);
4753c34c09a8SJoe Perches			}
4754c34c09a8SJoe Perches		}
4755c34c09a8SJoe Perches
4756d1e2ad07SJoe Perches# check for switch/default statements without a break;
4757d1e2ad07SJoe Perches		if ($^V && $^V ge 5.10.0 &&
4758d1e2ad07SJoe Perches		    defined $stat &&
4759d1e2ad07SJoe Perches		    $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
4760d1e2ad07SJoe Perches			my $ctx = '';
4761d1e2ad07SJoe Perches			my $herectx = $here . "\n";
4762d1e2ad07SJoe Perches			my $cnt = statement_rawlines($stat);
4763d1e2ad07SJoe Perches			for (my $n = 0; $n < $cnt; $n++) {
4764d1e2ad07SJoe Perches				$herectx .= raw_line($linenr, $n) . "\n";
4765d1e2ad07SJoe Perches			}
4766d1e2ad07SJoe Perches			WARN("DEFAULT_NO_BREAK",
4767d1e2ad07SJoe Perches			     "switch default: should use break\n" . $herectx);
4768caf2a54fSJoe Perches		}
4769caf2a54fSJoe Perches
477013214adfSAndy Whitcroft# check for gcc specific __FUNCTION__
4771d5e616fcSJoe Perches		if ($line =~ /\b__FUNCTION__\b/) {
4772d5e616fcSJoe Perches			if (WARN("USE_FUNC",
4773d5e616fcSJoe Perches				 "__func__ should be used instead of gcc specific __FUNCTION__\n"  . $herecurr) &&
4774d5e616fcSJoe Perches			    $fix) {
4775194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g;
4776d5e616fcSJoe Perches			}
477713214adfSAndy Whitcroft		}
4778773647a0SAndy Whitcroft
47792c92488aSJoe Perches# check for use of yield()
47802c92488aSJoe Perches		if ($line =~ /\byield\s*\(\s*\)/) {
47812c92488aSJoe Perches			WARN("YIELD",
47822c92488aSJoe Perches			     "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n"  . $herecurr);
47832c92488aSJoe Perches		}
47842c92488aSJoe Perches
4785179f8f40SJoe Perches# check for comparisons against true and false
4786179f8f40SJoe Perches		if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
4787179f8f40SJoe Perches			my $lead = $1;
4788179f8f40SJoe Perches			my $arg = $2;
4789179f8f40SJoe Perches			my $test = $3;
4790179f8f40SJoe Perches			my $otype = $4;
4791179f8f40SJoe Perches			my $trail = $5;
4792179f8f40SJoe Perches			my $op = "!";
4793179f8f40SJoe Perches
4794179f8f40SJoe Perches			($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
4795179f8f40SJoe Perches
4796179f8f40SJoe Perches			my $type = lc($otype);
4797179f8f40SJoe Perches			if ($type =~ /^(?:true|false)$/) {
4798179f8f40SJoe Perches				if (("$test" eq "==" && "$type" eq "true") ||
4799179f8f40SJoe Perches				    ("$test" eq "!=" && "$type" eq "false")) {
4800179f8f40SJoe Perches					$op = "";
4801179f8f40SJoe Perches				}
4802179f8f40SJoe Perches
4803179f8f40SJoe Perches				CHK("BOOL_COMPARISON",
4804179f8f40SJoe Perches				    "Using comparison to $otype is error prone\n" . $herecurr);
4805179f8f40SJoe Perches
4806179f8f40SJoe Perches## maybe suggesting a correct construct would better
4807179f8f40SJoe Perches##				    "Using comparison to $otype is error prone.  Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
4808179f8f40SJoe Perches
4809179f8f40SJoe Perches			}
4810179f8f40SJoe Perches		}
4811179f8f40SJoe Perches
48124882720bSThomas Gleixner# check for semaphores initialized locked
48134882720bSThomas Gleixner		if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
4814000d1cc1SJoe Perches			WARN("CONSIDER_COMPLETION",
4815000d1cc1SJoe Perches			     "consider using a completion\n" . $herecurr);
4816773647a0SAndy Whitcroft		}
48176712d858SJoe Perches
481867d0a075SJoe Perches# recommend kstrto* over simple_strto* and strict_strto*
481967d0a075SJoe Perches		if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
4820000d1cc1SJoe Perches			WARN("CONSIDER_KSTRTO",
482167d0a075SJoe Perches			     "$1 is obsolete, use k$3 instead\n" . $herecurr);
4822773647a0SAndy Whitcroft		}
48236712d858SJoe Perches
4824ae3ccc46SFabian Frederick# check for __initcall(), use device_initcall() explicitly or more appropriate function please
4825f3db6639SMichael Ellerman		if ($line =~ /^.\s*__initcall\s*\(/) {
4826000d1cc1SJoe Perches			WARN("USE_DEVICE_INITCALL",
4827ae3ccc46SFabian Frederick			     "please use device_initcall() or more appropriate function instead of __initcall() (see include/linux/init.h)\n" . $herecurr);
4828f3db6639SMichael Ellerman		}
48296712d858SJoe Perches
483079404849SEmese Revfy# check for various ops structs, ensure they are const.
483179404849SEmese Revfy		my $struct_ops = qr{acpi_dock_ops|
483279404849SEmese Revfy				address_space_operations|
483379404849SEmese Revfy				backlight_ops|
483479404849SEmese Revfy				block_device_operations|
483579404849SEmese Revfy				dentry_operations|
483679404849SEmese Revfy				dev_pm_ops|
483779404849SEmese Revfy				dma_map_ops|
483879404849SEmese Revfy				extent_io_ops|
483979404849SEmese Revfy				file_lock_operations|
484079404849SEmese Revfy				file_operations|
484179404849SEmese Revfy				hv_ops|
484279404849SEmese Revfy				ide_dma_ops|
484379404849SEmese Revfy				intel_dvo_dev_ops|
484479404849SEmese Revfy				item_operations|
484579404849SEmese Revfy				iwl_ops|
484679404849SEmese Revfy				kgdb_arch|
484779404849SEmese Revfy				kgdb_io|
484879404849SEmese Revfy				kset_uevent_ops|
484979404849SEmese Revfy				lock_manager_operations|
485079404849SEmese Revfy				microcode_ops|
485179404849SEmese Revfy				mtrr_ops|
485279404849SEmese Revfy				neigh_ops|
485379404849SEmese Revfy				nlmsvc_binding|
485479404849SEmese Revfy				pci_raw_ops|
485579404849SEmese Revfy				pipe_buf_operations|
485679404849SEmese Revfy				platform_hibernation_ops|
485779404849SEmese Revfy				platform_suspend_ops|
485879404849SEmese Revfy				proto_ops|
485979404849SEmese Revfy				rpc_pipe_ops|
486079404849SEmese Revfy				seq_operations|
486179404849SEmese Revfy				snd_ac97_build_ops|
486279404849SEmese Revfy				soc_pcmcia_socket_ops|
486379404849SEmese Revfy				stacktrace_ops|
486479404849SEmese Revfy				sysfs_ops|
486579404849SEmese Revfy				tty_operations|
486679404849SEmese Revfy				usb_mon_operations|
486779404849SEmese Revfy				wd_ops}x;
48686903ffb2SAndy Whitcroft		if ($line !~ /\bconst\b/ &&
486979404849SEmese Revfy		    $line =~ /\bstruct\s+($struct_ops)\b/) {
4870000d1cc1SJoe Perches			WARN("CONST_STRUCT",
4871000d1cc1SJoe Perches			     "struct $1 should normally be const\n" .
48726903ffb2SAndy Whitcroft				$herecurr);
48732b6db5cbSAndy Whitcroft		}
4874773647a0SAndy Whitcroft
4875773647a0SAndy Whitcroft# use of NR_CPUS is usually wrong
4876773647a0SAndy Whitcroft# ignore definitions of NR_CPUS and usage to define arrays as likely right
4877773647a0SAndy Whitcroft		if ($line =~ /\bNR_CPUS\b/ &&
4878c45dcabdSAndy Whitcroft		    $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
4879c45dcabdSAndy Whitcroft		    $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
4880171ae1a4SAndy Whitcroft		    $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
4881171ae1a4SAndy Whitcroft		    $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
4882171ae1a4SAndy Whitcroft		    $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
4883773647a0SAndy Whitcroft		{
4884000d1cc1SJoe Perches			WARN("NR_CPUS",
4885000d1cc1SJoe Perches			     "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
4886773647a0SAndy Whitcroft		}
48879c9ba34eSAndy Whitcroft
488852ea8506SJoe Perches# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
488952ea8506SJoe Perches		if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
489052ea8506SJoe Perches			ERROR("DEFINE_ARCH_HAS",
489152ea8506SJoe Perches			      "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
489252ea8506SJoe Perches		}
489352ea8506SJoe Perches
48949c9ba34eSAndy Whitcroft# check for %L{u,d,i} in strings
48959c9ba34eSAndy Whitcroft		my $string;
48969c9ba34eSAndy Whitcroft		while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
48979c9ba34eSAndy Whitcroft			$string = substr($rawline, $-[1], $+[1] - $-[1]);
48982a1bc5d5SAndy Whitcroft			$string =~ s/%%/__/g;
48999c9ba34eSAndy Whitcroft			if ($string =~ /(?<!%)%L[udi]/) {
4900000d1cc1SJoe Perches				WARN("PRINTF_L",
4901000d1cc1SJoe Perches				     "\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
49029c9ba34eSAndy Whitcroft				last;
49039c9ba34eSAndy Whitcroft			}
49049c9ba34eSAndy Whitcroft		}
4905691d77b6SAndy Whitcroft
4906691d77b6SAndy Whitcroft# whine mightly about in_atomic
4907691d77b6SAndy Whitcroft		if ($line =~ /\bin_atomic\s*\(/) {
4908691d77b6SAndy Whitcroft			if ($realfile =~ m@^drivers/@) {
4909000d1cc1SJoe Perches				ERROR("IN_ATOMIC",
4910000d1cc1SJoe Perches				      "do not use in_atomic in drivers\n" . $herecurr);
4911f4a87736SAndy Whitcroft			} elsif ($realfile !~ m@^kernel/@) {
4912000d1cc1SJoe Perches				WARN("IN_ATOMIC",
4913000d1cc1SJoe Perches				     "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
4914691d77b6SAndy Whitcroft			}
4915691d77b6SAndy Whitcroft		}
49161704f47bSPeter Zijlstra
49171704f47bSPeter Zijlstra# check for lockdep_set_novalidate_class
49181704f47bSPeter Zijlstra		if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
49191704f47bSPeter Zijlstra		    $line =~ /__lockdep_no_validate__\s*\)/ ) {
49201704f47bSPeter Zijlstra			if ($realfile !~ m@^kernel/lockdep@ &&
49211704f47bSPeter Zijlstra			    $realfile !~ m@^include/linux/lockdep@ &&
49221704f47bSPeter Zijlstra			    $realfile !~ m@^drivers/base/core@) {
4923000d1cc1SJoe Perches				ERROR("LOCKDEP",
4924000d1cc1SJoe Perches				      "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
49251704f47bSPeter Zijlstra			}
49261704f47bSPeter Zijlstra		}
492788f8831cSDave Jones
492888f8831cSDave Jones		if ($line =~ /debugfs_create_file.*S_IWUGO/ ||
492988f8831cSDave Jones		    $line =~ /DEVICE_ATTR.*S_IWUGO/ ) {
4930000d1cc1SJoe Perches			WARN("EXPORTED_WORLD_WRITABLE",
4931000d1cc1SJoe Perches			     "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
493288f8831cSDave Jones		}
49332435880fSJoe Perches
4934515a235eSJoe Perches# Mode permission misuses where it seems decimal should be octal
4935515a235eSJoe Perches# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop
4936515a235eSJoe Perches		if ($^V && $^V ge 5.10.0 &&
4937515a235eSJoe Perches		    $line =~ /$mode_perms_search/) {
49382435880fSJoe Perches			foreach my $entry (@mode_permission_funcs) {
49392435880fSJoe Perches				my $func = $entry->[0];
49402435880fSJoe Perches				my $arg_pos = $entry->[1];
49412435880fSJoe Perches
49422435880fSJoe Perches				my $skip_args = "";
49432435880fSJoe Perches				if ($arg_pos > 1) {
49442435880fSJoe Perches					$arg_pos--;
49452435880fSJoe Perches					$skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}";
49462435880fSJoe Perches				}
49472435880fSJoe Perches				my $test = "\\b$func\\s*\\(${skip_args}([\\d]+)\\s*[,\\)]";
4948515a235eSJoe Perches				if ($line =~ /$test/) {
49492435880fSJoe Perches					my $val = $1;
49502435880fSJoe Perches					$val = $6 if ($skip_args ne "");
49512435880fSJoe Perches
49521727cc70SJoe Perches					if ($val !~ /^0$/ &&
49531727cc70SJoe Perches					    (($val =~ /^$Int$/ && $val !~ /^$Octal$/) ||
49541727cc70SJoe Perches					     length($val) ne 4)) {
49552435880fSJoe Perches						ERROR("NON_OCTAL_PERMISSIONS",
49561727cc70SJoe Perches						      "Use 4 digit octal (0777) not decimal permissions\n" . $herecurr);
49572435880fSJoe Perches					}
49582435880fSJoe Perches				}
49592435880fSJoe Perches			}
496013214adfSAndy Whitcroft		}
4961515a235eSJoe Perches	}
496213214adfSAndy Whitcroft
496313214adfSAndy Whitcroft	# If we have no input at all, then there is nothing to report on
496413214adfSAndy Whitcroft	# so just keep quiet.
496513214adfSAndy Whitcroft	if ($#rawlines == -1) {
496613214adfSAndy Whitcroft		exit(0);
49670a920b5bSAndy Whitcroft	}
49680a920b5bSAndy Whitcroft
49698905a67cSAndy Whitcroft	# In mailback mode only produce a report in the negative, for
49708905a67cSAndy Whitcroft	# things that appear to be patches.
49718905a67cSAndy Whitcroft	if ($mailback && ($clean == 1 || !$is_patch)) {
49728905a67cSAndy Whitcroft		exit(0);
49738905a67cSAndy Whitcroft	}
49748905a67cSAndy Whitcroft
49758905a67cSAndy Whitcroft	# This is not a patch, and we are are in 'no-patch' mode so
49768905a67cSAndy Whitcroft	# just keep quiet.
49778905a67cSAndy Whitcroft	if (!$chk_patch && !$is_patch) {
49788905a67cSAndy Whitcroft		exit(0);
49798905a67cSAndy Whitcroft	}
49808905a67cSAndy Whitcroft
49818905a67cSAndy Whitcroft	if (!$is_patch) {
4982000d1cc1SJoe Perches		ERROR("NOT_UNIFIED_DIFF",
4983000d1cc1SJoe Perches		      "Does not appear to be a unified-diff format patch\n");
49840a920b5bSAndy Whitcroft	}
49850a920b5bSAndy Whitcroft	if ($is_patch && $chk_signoff && $signoff == 0) {
4986000d1cc1SJoe Perches		ERROR("MISSING_SIGN_OFF",
4987000d1cc1SJoe Perches		      "Missing Signed-off-by: line(s)\n");
49880a920b5bSAndy Whitcroft	}
49890a920b5bSAndy Whitcroft
4990f0a594c1SAndy Whitcroft	print report_dump();
499113214adfSAndy Whitcroft	if ($summary && !($clean == 1 && $quiet == 1)) {
499213214adfSAndy Whitcroft		print "$filename " if ($summary_file);
49936c72ffaaSAndy Whitcroft		print "total: $cnt_error errors, $cnt_warn warnings, " .
49946c72ffaaSAndy Whitcroft			(($check)? "$cnt_chk checks, " : "") .
49956c72ffaaSAndy Whitcroft			"$cnt_lines lines checked\n";
49968905a67cSAndy Whitcroft		print "\n" if ($quiet == 0);
49976c72ffaaSAndy Whitcroft	}
49988905a67cSAndy Whitcroft
4999d2c0a235SAndy Whitcroft	if ($quiet == 0) {
5000d1fe9c09SJoe Perches
5001d1fe9c09SJoe Perches		if ($^V lt 5.10.0) {
5002d1fe9c09SJoe Perches			print("NOTE: perl $^V is not modern enough to detect all possible issues.\n");
5003d1fe9c09SJoe Perches			print("An upgrade to at least perl v5.10.0 is suggested.\n\n");
5004d1fe9c09SJoe Perches		}
5005d1fe9c09SJoe Perches
5006d2c0a235SAndy Whitcroft		# If there were whitespace errors which cleanpatch can fix
5007d2c0a235SAndy Whitcroft		# then suggest that.
5008d2c0a235SAndy Whitcroft		if ($rpt_cleaners) {
5009d2c0a235SAndy Whitcroft			print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n";
5010d2c0a235SAndy Whitcroft			print "      scripts/cleanfile\n\n";
5011b0781216SMike Frysinger			$rpt_cleaners = 0;
5012d2c0a235SAndy Whitcroft		}
5013d2c0a235SAndy Whitcroft	}
5014d2c0a235SAndy Whitcroft
501591bfe484SJoe Perches	hash_show_words(\%use_type, "Used");
501691bfe484SJoe Perches	hash_show_words(\%ignore_type, "Ignored");
5017000d1cc1SJoe Perches
5018*d752fcc8SJoe Perches	if ($clean == 0 && $fix &&
5019*d752fcc8SJoe Perches	    ("@rawlines" ne "@fixed" ||
5020*d752fcc8SJoe Perches	     $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) {
50219624b8d6SJoe Perches		my $newfile = $filename;
50229624b8d6SJoe Perches		$newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace);
50233705ce5bSJoe Perches		my $linecount = 0;
50243705ce5bSJoe Perches		my $f;
50253705ce5bSJoe Perches
5026*d752fcc8SJoe Perches		@fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted);
5027*d752fcc8SJoe Perches
50283705ce5bSJoe Perches		open($f, '>', $newfile)
50293705ce5bSJoe Perches		    or die "$P: Can't open $newfile for write\n";
50303705ce5bSJoe Perches		foreach my $fixed_line (@fixed) {
50313705ce5bSJoe Perches			$linecount++;
50323705ce5bSJoe Perches			if ($file) {
50333705ce5bSJoe Perches				if ($linecount > 3) {
50343705ce5bSJoe Perches					$fixed_line =~ s/^\+//;
50353705ce5bSJoe Perches					print $f $fixed_line . "\n";
50363705ce5bSJoe Perches				}
50373705ce5bSJoe Perches			} else {
50383705ce5bSJoe Perches				print $f $fixed_line . "\n";
50393705ce5bSJoe Perches			}
50403705ce5bSJoe Perches		}
50413705ce5bSJoe Perches		close($f);
50423705ce5bSJoe Perches
50433705ce5bSJoe Perches		if (!$quiet) {
50443705ce5bSJoe Perches			print << "EOM";
50453705ce5bSJoe PerchesWrote EXPERIMENTAL --fix correction(s) to '$newfile'
50463705ce5bSJoe Perches
50473705ce5bSJoe PerchesDo _NOT_ trust the results written to this file.
50483705ce5bSJoe PerchesDo _NOT_ submit these changes without inspecting them for correctness.
50493705ce5bSJoe Perches
50503705ce5bSJoe PerchesThis EXPERIMENTAL file is simply a convenience to help rewrite patches.
50513705ce5bSJoe PerchesNo warranties, expressed or implied...
50523705ce5bSJoe Perches
50533705ce5bSJoe PerchesEOM
50543705ce5bSJoe Perches		}
50553705ce5bSJoe Perches	}
50563705ce5bSJoe Perches
50570a920b5bSAndy Whitcroft	if ($clean == 1 && $quiet == 0) {
5058c2fdda0dSAndy Whitcroft		print "$vname has no obvious style problems and is ready for submission.\n"
50590a920b5bSAndy Whitcroft	}
50600a920b5bSAndy Whitcroft	if ($clean == 0 && $quiet == 0) {
5061000d1cc1SJoe Perches		print << "EOM";
5062000d1cc1SJoe Perches$vname has style problems, please review.
5063000d1cc1SJoe Perches
5064000d1cc1SJoe PerchesIf any of these errors are false positives, please report
5065000d1cc1SJoe Perchesthem to the maintainer, see CHECKPATCH in MAINTAINERS.
5066000d1cc1SJoe PerchesEOM
50670a920b5bSAndy Whitcroft	}
506813214adfSAndy Whitcroft
50690a920b5bSAndy Whitcroft	return $clean;
50700a920b5bSAndy Whitcroft}
5071