xref: /linux-6.15/scripts/checkpatch.pl (revision bff5da43)
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;
278905a67cSAndy Whitcroftmy $summary = 1;
288905a67cSAndy Whitcroftmy $mailback = 0;
2913214adfSAndy Whitcroftmy $summary_file = 0;
30000d1cc1SJoe Perchesmy $show_types = 0;
313705ce5bSJoe Perchesmy $fix = 0;
329624b8d6SJoe Perchesmy $fix_inplace = 0;
336c72ffaaSAndy Whitcroftmy $root;
34c2fdda0dSAndy Whitcroftmy %debug;
353445686aSJoe Perchesmy %camelcase = ();
3691bfe484SJoe Perchesmy %use_type = ();
3791bfe484SJoe Perchesmy @use = ();
3891bfe484SJoe Perchesmy %ignore_type = ();
39000d1cc1SJoe Perchesmy @ignore = ();
4077f5b10aSHannes Edermy $help = 0;
41000d1cc1SJoe Perchesmy $configuration_file = ".checkpatch.conf";
426cd7f386SJoe Perchesmy $max_line_length = 80;
43d62a201fSDave Hansenmy $ignore_perl_version = 0;
44d62a201fSDave Hansenmy $minimum_perl_version = 5.10.0;
4577f5b10aSHannes Eder
4677f5b10aSHannes Edersub help {
4777f5b10aSHannes Eder	my ($exitcode) = @_;
4877f5b10aSHannes Eder
4977f5b10aSHannes Eder	print << "EOM";
5077f5b10aSHannes EderUsage: $P [OPTION]... [FILE]...
5177f5b10aSHannes EderVersion: $V
5277f5b10aSHannes Eder
5377f5b10aSHannes EderOptions:
5477f5b10aSHannes Eder  -q, --quiet                quiet
5577f5b10aSHannes Eder  --no-tree                  run without a kernel tree
5677f5b10aSHannes Eder  --no-signoff               do not check for 'Signed-off-by' line
5777f5b10aSHannes Eder  --patch                    treat FILE as patchfile (default)
5877f5b10aSHannes Eder  --emacs                    emacs compile window format
5977f5b10aSHannes Eder  --terse                    one line per report
6077f5b10aSHannes Eder  -f, --file                 treat FILE as regular source file
6177f5b10aSHannes Eder  --subjective, --strict     enable more subjective tests
6291bfe484SJoe Perches  --types TYPE(,TYPE2...)    show only these comma separated message types
63000d1cc1SJoe Perches  --ignore TYPE(,TYPE2...)   ignore various comma separated message types
646cd7f386SJoe Perches  --max-line-length=n        set the maximum line length, if exceeded, warn
65000d1cc1SJoe Perches  --show-types               show the message "types" in the output
6677f5b10aSHannes Eder  --root=PATH                PATH to the kernel tree root
6777f5b10aSHannes Eder  --no-summary               suppress the per-file summary
6877f5b10aSHannes Eder  --mailback                 only produce a report in case of warnings/errors
6977f5b10aSHannes Eder  --summary-file             include the filename in summary
7077f5b10aSHannes Eder  --debug KEY=[0|1]          turn on/off debugging of KEY, where KEY is one of
7177f5b10aSHannes Eder                             'values', 'possible', 'type', and 'attr' (default
7277f5b10aSHannes Eder                             is all off)
7377f5b10aSHannes Eder  --test-only=WORD           report only warnings/errors containing WORD
7477f5b10aSHannes Eder                             literally
753705ce5bSJoe Perches  --fix                      EXPERIMENTAL - may create horrible results
763705ce5bSJoe Perches                             If correctable single-line errors exist, create
773705ce5bSJoe Perches                             "<inputfile>.EXPERIMENTAL-checkpatch-fixes"
783705ce5bSJoe Perches                             with potential errors corrected to the preferred
793705ce5bSJoe Perches                             checkpatch style
809624b8d6SJoe Perches  --fix-inplace              EXPERIMENTAL - may create horrible results
819624b8d6SJoe Perches                             Is the same as --fix, but overwrites the input
829624b8d6SJoe Perches                             file.  It's your fault if there's no backup or git
83d62a201fSDave Hansen  --ignore-perl-version      override checking of perl version.  expect
84d62a201fSDave Hansen                             runtime errors.
8577f5b10aSHannes Eder  -h, --help, --version      display this help and exit
8677f5b10aSHannes Eder
8777f5b10aSHannes EderWhen FILE is - read standard input.
8877f5b10aSHannes EderEOM
8977f5b10aSHannes Eder
9077f5b10aSHannes Eder	exit($exitcode);
9177f5b10aSHannes Eder}
9277f5b10aSHannes Eder
93000d1cc1SJoe Perchesmy $conf = which_conf($configuration_file);
94000d1cc1SJoe Perchesif (-f $conf) {
95000d1cc1SJoe Perches	my @conf_args;
96000d1cc1SJoe Perches	open(my $conffile, '<', "$conf")
97000d1cc1SJoe Perches	    or warn "$P: Can't find a readable $configuration_file file $!\n";
98000d1cc1SJoe Perches
99000d1cc1SJoe Perches	while (<$conffile>) {
100000d1cc1SJoe Perches		my $line = $_;
101000d1cc1SJoe Perches
102000d1cc1SJoe Perches		$line =~ s/\s*\n?$//g;
103000d1cc1SJoe Perches		$line =~ s/^\s*//g;
104000d1cc1SJoe Perches		$line =~ s/\s+/ /g;
105000d1cc1SJoe Perches
106000d1cc1SJoe Perches		next if ($line =~ m/^\s*#/);
107000d1cc1SJoe Perches		next if ($line =~ m/^\s*$/);
108000d1cc1SJoe Perches
109000d1cc1SJoe Perches		my @words = split(" ", $line);
110000d1cc1SJoe Perches		foreach my $word (@words) {
111000d1cc1SJoe Perches			last if ($word =~ m/^#/);
112000d1cc1SJoe Perches			push (@conf_args, $word);
113000d1cc1SJoe Perches		}
114000d1cc1SJoe Perches	}
115000d1cc1SJoe Perches	close($conffile);
116000d1cc1SJoe Perches	unshift(@ARGV, @conf_args) if @conf_args;
117000d1cc1SJoe Perches}
118000d1cc1SJoe Perches
1190a920b5bSAndy WhitcroftGetOptions(
1206c72ffaaSAndy Whitcroft	'q|quiet+'	=> \$quiet,
1210a920b5bSAndy Whitcroft	'tree!'		=> \$tree,
1220a920b5bSAndy Whitcroft	'signoff!'	=> \$chk_signoff,
1230a920b5bSAndy Whitcroft	'patch!'	=> \$chk_patch,
1246c72ffaaSAndy Whitcroft	'emacs!'	=> \$emacs,
1258905a67cSAndy Whitcroft	'terse!'	=> \$terse,
12677f5b10aSHannes Eder	'f|file!'	=> \$file,
1276c72ffaaSAndy Whitcroft	'subjective!'	=> \$check,
1286c72ffaaSAndy Whitcroft	'strict!'	=> \$check,
129000d1cc1SJoe Perches	'ignore=s'	=> \@ignore,
13091bfe484SJoe Perches	'types=s'	=> \@use,
131000d1cc1SJoe Perches	'show-types!'	=> \$show_types,
1326cd7f386SJoe Perches	'max-line-length=i' => \$max_line_length,
1336c72ffaaSAndy Whitcroft	'root=s'	=> \$root,
1348905a67cSAndy Whitcroft	'summary!'	=> \$summary,
1358905a67cSAndy Whitcroft	'mailback!'	=> \$mailback,
13613214adfSAndy Whitcroft	'summary-file!'	=> \$summary_file,
1373705ce5bSJoe Perches	'fix!'		=> \$fix,
1389624b8d6SJoe Perches	'fix-inplace!'	=> \$fix_inplace,
139d62a201fSDave Hansen	'ignore-perl-version!' => \$ignore_perl_version,
140c2fdda0dSAndy Whitcroft	'debug=s'	=> \%debug,
141773647a0SAndy Whitcroft	'test-only=s'	=> \$tst_only,
14277f5b10aSHannes Eder	'h|help'	=> \$help,
14377f5b10aSHannes Eder	'version'	=> \$help
14477f5b10aSHannes Eder) or help(1);
14577f5b10aSHannes Eder
14677f5b10aSHannes Ederhelp(0) if ($help);
1470a920b5bSAndy Whitcroft
1489624b8d6SJoe Perches$fix = 1 if ($fix_inplace);
1499624b8d6SJoe Perches
1500a920b5bSAndy Whitcroftmy $exit = 0;
1510a920b5bSAndy Whitcroft
152d62a201fSDave Hansenif ($^V && $^V lt $minimum_perl_version) {
153d62a201fSDave Hansen	printf "$P: requires at least perl version %vd\n", $minimum_perl_version;
154d62a201fSDave Hansen	if (!$ignore_perl_version) {
155d62a201fSDave Hansen		exit(1);
156d62a201fSDave Hansen	}
157d62a201fSDave Hansen}
158d62a201fSDave Hansen
1590a920b5bSAndy Whitcroftif ($#ARGV < 0) {
16077f5b10aSHannes Eder	print "$P: no input files\n";
1610a920b5bSAndy Whitcroft	exit(1);
1620a920b5bSAndy Whitcroft}
1630a920b5bSAndy Whitcroft
16491bfe484SJoe Perchessub hash_save_array_words {
16591bfe484SJoe Perches	my ($hashRef, $arrayRef) = @_;
16691bfe484SJoe Perches
16791bfe484SJoe Perches	my @array = split(/,/, join(',', @$arrayRef));
16891bfe484SJoe Perches	foreach my $word (@array) {
169000d1cc1SJoe Perches		$word =~ s/\s*\n?$//g;
170000d1cc1SJoe Perches		$word =~ s/^\s*//g;
171000d1cc1SJoe Perches		$word =~ s/\s+/ /g;
172000d1cc1SJoe Perches		$word =~ tr/[a-z]/[A-Z]/;
173000d1cc1SJoe Perches
174000d1cc1SJoe Perches		next if ($word =~ m/^\s*#/);
175000d1cc1SJoe Perches		next if ($word =~ m/^\s*$/);
176000d1cc1SJoe Perches
17791bfe484SJoe Perches		$hashRef->{$word}++;
178000d1cc1SJoe Perches	}
17991bfe484SJoe Perches}
18091bfe484SJoe Perches
18191bfe484SJoe Perchessub hash_show_words {
18291bfe484SJoe Perches	my ($hashRef, $prefix) = @_;
18391bfe484SJoe Perches
18458cb3cf6SJoe Perches	if ($quiet == 0 && keys %$hashRef) {
18591bfe484SJoe Perches		print "NOTE: $prefix message types:";
18658cb3cf6SJoe Perches		foreach my $word (sort keys %$hashRef) {
18791bfe484SJoe Perches			print " $word";
18891bfe484SJoe Perches		}
18991bfe484SJoe Perches		print "\n\n";
19091bfe484SJoe Perches	}
19191bfe484SJoe Perches}
19291bfe484SJoe Perches
19391bfe484SJoe Percheshash_save_array_words(\%ignore_type, \@ignore);
19491bfe484SJoe Percheshash_save_array_words(\%use_type, \@use);
195000d1cc1SJoe Perches
196c2fdda0dSAndy Whitcroftmy $dbg_values = 0;
197c2fdda0dSAndy Whitcroftmy $dbg_possible = 0;
1987429c690SAndy Whitcroftmy $dbg_type = 0;
199a1ef277eSAndy Whitcroftmy $dbg_attr = 0;
200c2fdda0dSAndy Whitcroftfor my $key (keys %debug) {
20121caa13cSAndy Whitcroft	## no critic
20221caa13cSAndy Whitcroft	eval "\${dbg_$key} = '$debug{$key}';";
20321caa13cSAndy Whitcroft	die "$@" if ($@);
204c2fdda0dSAndy Whitcroft}
205c2fdda0dSAndy Whitcroft
206d2c0a235SAndy Whitcroftmy $rpt_cleaners = 0;
207d2c0a235SAndy Whitcroft
2088905a67cSAndy Whitcroftif ($terse) {
2098905a67cSAndy Whitcroft	$emacs = 1;
2108905a67cSAndy Whitcroft	$quiet++;
2118905a67cSAndy Whitcroft}
2128905a67cSAndy Whitcroft
2136c72ffaaSAndy Whitcroftif ($tree) {
2146c72ffaaSAndy Whitcroft	if (defined $root) {
2156c72ffaaSAndy Whitcroft		if (!top_of_kernel_tree($root)) {
2166c72ffaaSAndy Whitcroft			die "$P: $root: --root does not point at a valid tree\n";
2176c72ffaaSAndy Whitcroft		}
2186c72ffaaSAndy Whitcroft	} else {
2196c72ffaaSAndy Whitcroft		if (top_of_kernel_tree('.')) {
2206c72ffaaSAndy Whitcroft			$root = '.';
2216c72ffaaSAndy Whitcroft		} elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
2226c72ffaaSAndy Whitcroft						top_of_kernel_tree($1)) {
2236c72ffaaSAndy Whitcroft			$root = $1;
2246c72ffaaSAndy Whitcroft		}
2256c72ffaaSAndy Whitcroft	}
2266c72ffaaSAndy Whitcroft
2276c72ffaaSAndy Whitcroft	if (!defined $root) {
2280a920b5bSAndy Whitcroft		print "Must be run from the top-level dir. of a kernel tree\n";
2290a920b5bSAndy Whitcroft		exit(2);
2300a920b5bSAndy Whitcroft	}
2316c72ffaaSAndy Whitcroft}
2326c72ffaaSAndy Whitcroft
2336c72ffaaSAndy Whitcroftmy $emitted_corrupt = 0;
2346c72ffaaSAndy Whitcroft
2352ceb532bSAndy Whitcroftour $Ident	= qr{
2362ceb532bSAndy Whitcroft			[A-Za-z_][A-Za-z\d_]*
2372ceb532bSAndy Whitcroft			(?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
2382ceb532bSAndy Whitcroft		}x;
2396c72ffaaSAndy Whitcroftour $Storage	= qr{extern|static|asmlinkage};
2406c72ffaaSAndy Whitcroftour $Sparse	= qr{
2416c72ffaaSAndy Whitcroft			__user|
2426c72ffaaSAndy Whitcroft			__kernel|
2436c72ffaaSAndy Whitcroft			__force|
2446c72ffaaSAndy Whitcroft			__iomem|
2456c72ffaaSAndy Whitcroft			__must_check|
2466c72ffaaSAndy Whitcroft			__init_refok|
247417495edSAndy Whitcroft			__kprobes|
248165e72a6SSven Eckelmann			__ref|
249165e72a6SSven Eckelmann			__rcu
2506c72ffaaSAndy Whitcroft		}x;
251e970b884SJoe Perchesour $InitAttributePrefix = qr{__(?:mem|cpu|dev|net_|)};
252e970b884SJoe Perchesour $InitAttributeData = qr{$InitAttributePrefix(?:initdata\b)};
253e970b884SJoe Perchesour $InitAttributeConst = qr{$InitAttributePrefix(?:initconst\b)};
254e970b884SJoe Perchesour $InitAttributeInit = qr{$InitAttributePrefix(?:init\b)};
255e970b884SJoe Perchesour $InitAttribute = qr{$InitAttributeData|$InitAttributeConst|$InitAttributeInit};
2568716de38SJoe Perches
25752131292SWolfram Sang# Notes to $Attribute:
25852131292SWolfram Sang# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
2596c72ffaaSAndy Whitcroftour $Attribute	= qr{
2606c72ffaaSAndy Whitcroft			const|
26103f1df7dSJoe Perches			__percpu|
26203f1df7dSJoe Perches			__nocast|
26303f1df7dSJoe Perches			__safe|
26403f1df7dSJoe Perches			__bitwise__|
26503f1df7dSJoe Perches			__packed__|
26603f1df7dSJoe Perches			__packed2__|
26703f1df7dSJoe Perches			__naked|
26803f1df7dSJoe Perches			__maybe_unused|
26903f1df7dSJoe Perches			__always_unused|
27003f1df7dSJoe Perches			__noreturn|
27103f1df7dSJoe Perches			__used|
27203f1df7dSJoe Perches			__cold|
27303f1df7dSJoe Perches			__noclone|
27403f1df7dSJoe Perches			__deprecated|
2756c72ffaaSAndy Whitcroft			__read_mostly|
2766c72ffaaSAndy Whitcroft			__kprobes|
2778716de38SJoe Perches			$InitAttribute|
27824e1d81aSAndy Whitcroft			____cacheline_aligned|
27924e1d81aSAndy Whitcroft			____cacheline_aligned_in_smp|
2805fe3af11SAndy Whitcroft			____cacheline_internodealigned_in_smp|
2815fe3af11SAndy Whitcroft			__weak
2826c72ffaaSAndy Whitcroft		  }x;
283c45dcabdSAndy Whitcroftour $Modifier;
2846c72ffaaSAndy Whitcroftour $Inline	= qr{inline|__always_inline|noinline};
2856c72ffaaSAndy Whitcroftour $Member	= qr{->$Ident|\.$Ident|\[[^]]*\]};
2866c72ffaaSAndy Whitcroftour $Lval	= qr{$Ident(?:$Member)*};
2876c72ffaaSAndy Whitcroft
28895e2c602SJoe Perchesour $Int_type	= qr{(?i)llu|ull|ll|lu|ul|l|u};
28995e2c602SJoe Perchesour $Binary	= qr{(?i)0b[01]+$Int_type?};
29095e2c602SJoe Perchesour $Hex	= qr{(?i)0x[0-9a-f]+$Int_type?};
29195e2c602SJoe Perchesour $Int	= qr{[0-9]+$Int_type?};
292326b1ffcSJoe Perchesour $Float_hex	= qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?};
293326b1ffcSJoe Perchesour $Float_dec	= qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?};
294326b1ffcSJoe Perchesour $Float_int	= qr{(?i)[0-9]+e-?[0-9]+[fl]?};
29574349bccSJoe Perchesour $Float	= qr{$Float_hex|$Float_dec|$Float_int};
29695e2c602SJoe Perchesour $Constant	= qr{$Float|$Binary|$Hex|$Int};
297326b1ffcSJoe Perchesour $Assignment	= qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=};
29886f9d059SAndy Whitcroftour $Compare    = qr{<=|>=|==|!=|<|>};
29923f780c9SJoe Perchesour $Arithmetic = qr{\+|-|\*|\/|%};
3006c72ffaaSAndy Whitcroftour $Operators	= qr{
3016c72ffaaSAndy Whitcroft			<=|>=|==|!=|
3026c72ffaaSAndy Whitcroft			=>|->|<<|>>|<|>|!|~|
30323f780c9SJoe Perches			&&|\|\||,|\^|\+\+|--|&|\||$Arithmetic
3046c72ffaaSAndy Whitcroft		  }x;
3056c72ffaaSAndy Whitcroft
3068905a67cSAndy Whitcroftour $NonptrType;
3078716de38SJoe Perchesour $NonptrTypeWithAttr;
3088905a67cSAndy Whitcroftour $Type;
3098905a67cSAndy Whitcroftour $Declare;
3108905a67cSAndy Whitcroft
31115662b3eSJoe Perchesour $NON_ASCII_UTF8	= qr{
31215662b3eSJoe Perches	[\xC2-\xDF][\x80-\xBF]               # non-overlong 2-byte
313171ae1a4SAndy Whitcroft	|  \xE0[\xA0-\xBF][\x80-\xBF]        # excluding overlongs
314171ae1a4SAndy Whitcroft	| [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}  # straight 3-byte
315171ae1a4SAndy Whitcroft	|  \xED[\x80-\x9F][\x80-\xBF]        # excluding surrogates
316171ae1a4SAndy Whitcroft	|  \xF0[\x90-\xBF][\x80-\xBF]{2}     # planes 1-3
317171ae1a4SAndy Whitcroft	| [\xF1-\xF3][\x80-\xBF]{3}          # planes 4-15
318171ae1a4SAndy Whitcroft	|  \xF4[\x80-\x8F][\x80-\xBF]{2}     # plane 16
319171ae1a4SAndy Whitcroft}x;
320171ae1a4SAndy Whitcroft
32115662b3eSJoe Perchesour $UTF8	= qr{
32215662b3eSJoe Perches	[\x09\x0A\x0D\x20-\x7E]              # ASCII
32315662b3eSJoe Perches	| $NON_ASCII_UTF8
32415662b3eSJoe Perches}x;
32515662b3eSJoe Perches
3268ed22cadSAndy Whitcroftour $typeTypedefs = qr{(?x:
327fb9e9096SAndy Whitcroft	(?:__)?(?:u|s|be|le)(?:8|16|32|64)|
3288ed22cadSAndy Whitcroft	atomic_t
3298ed22cadSAndy Whitcroft)};
3308ed22cadSAndy Whitcroft
331691e669bSJoe Perchesour $logFunctions = qr{(?x:
3326e60c02eSJoe Perches	printk(?:_ratelimited|_once|)|
3337d0b6594SJacob Keller	(?:[a-z0-9]+_){1,2}(?:printk|emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)(?:_ratelimited|_once|)|
3346e60c02eSJoe Perches	WARN(?:_RATELIMIT|_ONCE|)|
335b0531722SJoe Perches	panic|
33606668727SJoe Perches	MODULE_[A-Z_]+|
33706668727SJoe Perches	seq_vprintf|seq_printf|seq_puts
338691e669bSJoe Perches)};
339691e669bSJoe Perches
34020112475SJoe Perchesour $signature_tags = qr{(?xi:
34120112475SJoe Perches	Signed-off-by:|
34220112475SJoe Perches	Acked-by:|
34320112475SJoe Perches	Tested-by:|
34420112475SJoe Perches	Reviewed-by:|
34520112475SJoe Perches	Reported-by:|
3468543ae12SMugunthan V N	Suggested-by:|
34720112475SJoe Perches	To:|
34820112475SJoe Perches	Cc:
34920112475SJoe Perches)};
35020112475SJoe Perches
3518905a67cSAndy Whitcroftour @typeList = (
3528905a67cSAndy Whitcroft	qr{void},
353c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?char},
354c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?short},
355c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?int},
356c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?long},
357c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?long\s+int},
358c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?long\s+long},
359c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?long\s+long\s+int},
3608905a67cSAndy Whitcroft	qr{unsigned},
3618905a67cSAndy Whitcroft	qr{float},
3628905a67cSAndy Whitcroft	qr{double},
3638905a67cSAndy Whitcroft	qr{bool},
3648905a67cSAndy Whitcroft	qr{struct\s+$Ident},
3658905a67cSAndy Whitcroft	qr{union\s+$Ident},
3668905a67cSAndy Whitcroft	qr{enum\s+$Ident},
3678905a67cSAndy Whitcroft	qr{${Ident}_t},
3688905a67cSAndy Whitcroft	qr{${Ident}_handler},
3698905a67cSAndy Whitcroft	qr{${Ident}_handler_fn},
3708905a67cSAndy Whitcroft);
3718716de38SJoe Perchesour @typeListWithAttr = (
3728716de38SJoe Perches	@typeList,
3738716de38SJoe Perches	qr{struct\s+$InitAttribute\s+$Ident},
3748716de38SJoe Perches	qr{union\s+$InitAttribute\s+$Ident},
3758716de38SJoe Perches);
3768716de38SJoe Perches
377c45dcabdSAndy Whitcroftour @modifierList = (
378c45dcabdSAndy Whitcroft	qr{fastcall},
379c45dcabdSAndy Whitcroft);
3808905a67cSAndy Whitcroft
3817840a94cSWolfram Sangour $allowed_asm_includes = qr{(?x:
3827840a94cSWolfram Sang	irq|
3837840a94cSWolfram Sang	memory
3847840a94cSWolfram Sang)};
3857840a94cSWolfram Sang# memory.h: ARM has a custom one
3867840a94cSWolfram Sang
3878905a67cSAndy Whitcroftsub build_types {
388d2172eb5SAndy Whitcroft	my $mods = "(?x:  \n" . join("|\n  ", @modifierList) . "\n)";
389d2172eb5SAndy Whitcroft	my $all = "(?x:  \n" . join("|\n  ", @typeList) . "\n)";
3908716de38SJoe Perches	my $allWithAttr = "(?x:  \n" . join("|\n  ", @typeListWithAttr) . "\n)";
391c8cb2ca3SAndy Whitcroft	$Modifier	= qr{(?:$Attribute|$Sparse|$mods)};
3928905a67cSAndy Whitcroft	$NonptrType	= qr{
393d2172eb5SAndy Whitcroft			(?:$Modifier\s+|const\s+)*
394cf655043SAndy Whitcroft			(?:
3956b48db24SAndy Whitcroft				(?:typeof|__typeof__)\s*\([^\)]*\)|
3968ed22cadSAndy Whitcroft				(?:$typeTypedefs\b)|
397c45dcabdSAndy Whitcroft				(?:${all}\b)
398cf655043SAndy Whitcroft			)
399c8cb2ca3SAndy Whitcroft			(?:\s+$Modifier|\s+const)*
4008905a67cSAndy Whitcroft		  }x;
4018716de38SJoe Perches	$NonptrTypeWithAttr	= qr{
4028716de38SJoe Perches			(?:$Modifier\s+|const\s+)*
4038716de38SJoe Perches			(?:
4048716de38SJoe Perches				(?:typeof|__typeof__)\s*\([^\)]*\)|
4058716de38SJoe Perches				(?:$typeTypedefs\b)|
4068716de38SJoe Perches				(?:${allWithAttr}\b)
4078716de38SJoe Perches			)
4088716de38SJoe Perches			(?:\s+$Modifier|\s+const)*
4098716de38SJoe Perches		  }x;
4108905a67cSAndy Whitcroft	$Type	= qr{
411c45dcabdSAndy Whitcroft			$NonptrType
412b337d8b8SAndy Whitcroft			(?:(?:\s|\*|\[\])+\s*const|(?:\s|\*|\[\])+|(?:\s*\[\s*\])+)?
413c8cb2ca3SAndy Whitcroft			(?:\s+$Inline|\s+$Modifier)*
4148905a67cSAndy Whitcroft		  }x;
4158905a67cSAndy Whitcroft	$Declare	= qr{(?:$Storage\s+)?$Type};
4168905a67cSAndy Whitcroft}
4178905a67cSAndy Whitcroftbuild_types();
4186c72ffaaSAndy Whitcroft
4197d2367afSJoe Perchesour $Typecast	= qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
420d1fe9c09SJoe Perches
421d1fe9c09SJoe Perches# Using $balanced_parens, $LvalOrFunc, or $FuncArg
422d1fe9c09SJoe Perches# requires at least perl version v5.10.0
423d1fe9c09SJoe Perches# Any use must be runtime checked with $^V
424d1fe9c09SJoe Perches
425d1fe9c09SJoe Perchesour $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
426d1fe9c09SJoe Perchesour $LvalOrFunc	= qr{($Lval)\s*($balanced_parens{0,1})\s*};
427d7c76ba7SJoe Perchesour $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant)};
4287d2367afSJoe Perches
4297d2367afSJoe Perchessub deparenthesize {
4307d2367afSJoe Perches	my ($string) = @_;
4317d2367afSJoe Perches	return "" if (!defined($string));
4327d2367afSJoe Perches	$string =~ s@^\s*\(\s*@@g;
4337d2367afSJoe Perches	$string =~ s@\s*\)\s*$@@g;
4347d2367afSJoe Perches	$string =~ s@\s+@ @g;
4357d2367afSJoe Perches	return $string;
4367d2367afSJoe Perches}
4377d2367afSJoe Perches
4383445686aSJoe Perchessub seed_camelcase_file {
4393445686aSJoe Perches	my ($file) = @_;
4403445686aSJoe Perches
4413445686aSJoe Perches	return if (!(-f $file));
4423445686aSJoe Perches
4433445686aSJoe Perches	local $/;
4443445686aSJoe Perches
4453445686aSJoe Perches	open(my $include_file, '<', "$file")
4463445686aSJoe Perches	    or warn "$P: Can't read '$file' $!\n";
4473445686aSJoe Perches	my $text = <$include_file>;
4483445686aSJoe Perches	close($include_file);
4493445686aSJoe Perches
4503445686aSJoe Perches	my @lines = split('\n', $text);
4513445686aSJoe Perches
4523445686aSJoe Perches	foreach my $line (@lines) {
4533445686aSJoe Perches		next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/);
4543445686aSJoe Perches		if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) {
4553445686aSJoe Perches			$camelcase{$1} = 1;
45611ea516aSJoe Perches		} elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) {
45711ea516aSJoe Perches			$camelcase{$1} = 1;
45811ea516aSJoe Perches		} elsif ($line =~ /^\s*(?:union|struct|enum)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[;\{]/) {
4593445686aSJoe Perches			$camelcase{$1} = 1;
4603445686aSJoe Perches		}
4613445686aSJoe Perches	}
4623445686aSJoe Perches}
4633445686aSJoe Perches
4643445686aSJoe Perchesmy $camelcase_seeded = 0;
4653445686aSJoe Perchessub seed_camelcase_includes {
4663445686aSJoe Perches	return if ($camelcase_seeded);
4673445686aSJoe Perches
4683445686aSJoe Perches	my $files;
469c707a81dSJoe Perches	my $camelcase_cache = "";
470c707a81dSJoe Perches	my @include_files = ();
471c707a81dSJoe Perches
472c707a81dSJoe Perches	$camelcase_seeded = 1;
473351b2a1fSJoe Perches
4743445686aSJoe Perches	if (-d ".git") {
475351b2a1fSJoe Perches		my $git_last_include_commit = `git log --no-merges --pretty=format:"%h%n" -1 -- include`;
476351b2a1fSJoe Perches		chomp $git_last_include_commit;
477c707a81dSJoe Perches		$camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit";
478c707a81dSJoe Perches	} else {
479c707a81dSJoe Perches		my $last_mod_date = 0;
480c707a81dSJoe Perches		$files = `find $root/include -name "*.h"`;
481c707a81dSJoe Perches		@include_files = split('\n', $files);
482c707a81dSJoe Perches		foreach my $file (@include_files) {
483c707a81dSJoe Perches			my $date = POSIX::strftime("%Y%m%d%H%M",
484c707a81dSJoe Perches						   localtime((stat $file)[9]));
485c707a81dSJoe Perches			$last_mod_date = $date if ($last_mod_date < $date);
486c707a81dSJoe Perches		}
487c707a81dSJoe Perches		$camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date";
488c707a81dSJoe Perches	}
489c707a81dSJoe Perches
490c707a81dSJoe Perches	if ($camelcase_cache ne "" && -f $camelcase_cache) {
491c707a81dSJoe Perches		open(my $camelcase_file, '<', "$camelcase_cache")
492c707a81dSJoe Perches		    or warn "$P: Can't read '$camelcase_cache' $!\n";
493351b2a1fSJoe Perches		while (<$camelcase_file>) {
494351b2a1fSJoe Perches			chomp;
495351b2a1fSJoe Perches			$camelcase{$_} = 1;
496351b2a1fSJoe Perches		}
497351b2a1fSJoe Perches		close($camelcase_file);
498351b2a1fSJoe Perches
499351b2a1fSJoe Perches		return;
500351b2a1fSJoe Perches	}
501c707a81dSJoe Perches
502c707a81dSJoe Perches	if (-d ".git") {
503c707a81dSJoe Perches		$files = `git ls-files "include/*.h"`;
504c707a81dSJoe Perches		@include_files = split('\n', $files);
5053445686aSJoe Perches	}
506c707a81dSJoe Perches
5073445686aSJoe Perches	foreach my $file (@include_files) {
5083445686aSJoe Perches		seed_camelcase_file($file);
5093445686aSJoe Perches	}
510351b2a1fSJoe Perches
511c707a81dSJoe Perches	if ($camelcase_cache ne "") {
512351b2a1fSJoe Perches		unlink glob ".checkpatch-camelcase.*";
513c707a81dSJoe Perches		open(my $camelcase_file, '>', "$camelcase_cache")
514c707a81dSJoe Perches		    or warn "$P: Can't write '$camelcase_cache' $!\n";
515351b2a1fSJoe Perches		foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) {
516351b2a1fSJoe Perches			print $camelcase_file ("$_\n");
517351b2a1fSJoe Perches		}
518351b2a1fSJoe Perches		close($camelcase_file);
519351b2a1fSJoe Perches	}
5203445686aSJoe Perches}
5213445686aSJoe Perches
5226c72ffaaSAndy Whitcroft$chk_signoff = 0 if ($file);
5230a920b5bSAndy Whitcroft
52400df344fSAndy Whitcroftmy @rawlines = ();
525c2fdda0dSAndy Whitcroftmy @lines = ();
5263705ce5bSJoe Perchesmy @fixed = ();
527c2fdda0dSAndy Whitcroftmy $vname;
5286c72ffaaSAndy Whitcroftfor my $filename (@ARGV) {
52921caa13cSAndy Whitcroft	my $FILE;
5306c72ffaaSAndy Whitcroft	if ($file) {
53121caa13cSAndy Whitcroft		open($FILE, '-|', "diff -u /dev/null $filename") ||
5326c72ffaaSAndy Whitcroft			die "$P: $filename: diff failed - $!\n";
53321caa13cSAndy Whitcroft	} elsif ($filename eq '-') {
53421caa13cSAndy Whitcroft		open($FILE, '<&STDIN');
5356c72ffaaSAndy Whitcroft	} else {
53621caa13cSAndy Whitcroft		open($FILE, '<', "$filename") ||
5376c72ffaaSAndy Whitcroft			die "$P: $filename: open failed - $!\n";
5386c72ffaaSAndy Whitcroft	}
539c2fdda0dSAndy Whitcroft	if ($filename eq '-') {
540c2fdda0dSAndy Whitcroft		$vname = 'Your patch';
541c2fdda0dSAndy Whitcroft	} else {
542c2fdda0dSAndy Whitcroft		$vname = $filename;
543c2fdda0dSAndy Whitcroft	}
54421caa13cSAndy Whitcroft	while (<$FILE>) {
5450a920b5bSAndy Whitcroft		chomp;
54600df344fSAndy Whitcroft		push(@rawlines, $_);
5476c72ffaaSAndy Whitcroft	}
54821caa13cSAndy Whitcroft	close($FILE);
549c2fdda0dSAndy Whitcroft	if (!process($filename)) {
5500a920b5bSAndy Whitcroft		$exit = 1;
5510a920b5bSAndy Whitcroft	}
55200df344fSAndy Whitcroft	@rawlines = ();
55313214adfSAndy Whitcroft	@lines = ();
5543705ce5bSJoe Perches	@fixed = ();
5550a920b5bSAndy Whitcroft}
5560a920b5bSAndy Whitcroft
5570a920b5bSAndy Whitcroftexit($exit);
5580a920b5bSAndy Whitcroft
5590a920b5bSAndy Whitcroftsub top_of_kernel_tree {
5606c72ffaaSAndy Whitcroft	my ($root) = @_;
5616c72ffaaSAndy Whitcroft
5626c72ffaaSAndy Whitcroft	my @tree_check = (
5636c72ffaaSAndy Whitcroft		"COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
5646c72ffaaSAndy Whitcroft		"README", "Documentation", "arch", "include", "drivers",
5656c72ffaaSAndy Whitcroft		"fs", "init", "ipc", "kernel", "lib", "scripts",
5666c72ffaaSAndy Whitcroft	);
5676c72ffaaSAndy Whitcroft
5686c72ffaaSAndy Whitcroft	foreach my $check (@tree_check) {
5696c72ffaaSAndy Whitcroft		if (! -e $root . '/' . $check) {
5700a920b5bSAndy Whitcroft			return 0;
5710a920b5bSAndy Whitcroft		}
5726c72ffaaSAndy Whitcroft	}
5736c72ffaaSAndy Whitcroft	return 1;
5746c72ffaaSAndy Whitcroft}
5750a920b5bSAndy Whitcroft
57620112475SJoe Perchessub parse_email {
57720112475SJoe Perches	my ($formatted_email) = @_;
57820112475SJoe Perches
57920112475SJoe Perches	my $name = "";
58020112475SJoe Perches	my $address = "";
58120112475SJoe Perches	my $comment = "";
58220112475SJoe Perches
58320112475SJoe Perches	if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
58420112475SJoe Perches		$name = $1;
58520112475SJoe Perches		$address = $2;
58620112475SJoe Perches		$comment = $3 if defined $3;
58720112475SJoe Perches	} elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
58820112475SJoe Perches		$address = $1;
58920112475SJoe Perches		$comment = $2 if defined $2;
59020112475SJoe Perches	} elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
59120112475SJoe Perches		$address = $1;
59220112475SJoe Perches		$comment = $2 if defined $2;
59320112475SJoe Perches		$formatted_email =~ s/$address.*$//;
59420112475SJoe Perches		$name = $formatted_email;
5953705ce5bSJoe Perches		$name = trim($name);
59620112475SJoe Perches		$name =~ s/^\"|\"$//g;
59720112475SJoe Perches		# If there's a name left after stripping spaces and
59820112475SJoe Perches		# leading quotes, and the address doesn't have both
59920112475SJoe Perches		# leading and trailing angle brackets, the address
60020112475SJoe Perches		# is invalid. ie:
60120112475SJoe Perches		#   "joe smith [email protected]" bad
60220112475SJoe Perches		#   "joe smith <[email protected]" bad
60320112475SJoe Perches		if ($name ne "" && $address !~ /^<[^>]+>$/) {
60420112475SJoe Perches			$name = "";
60520112475SJoe Perches			$address = "";
60620112475SJoe Perches			$comment = "";
60720112475SJoe Perches		}
60820112475SJoe Perches	}
60920112475SJoe Perches
6103705ce5bSJoe Perches	$name = trim($name);
61120112475SJoe Perches	$name =~ s/^\"|\"$//g;
6123705ce5bSJoe Perches	$address = trim($address);
61320112475SJoe Perches	$address =~ s/^\<|\>$//g;
61420112475SJoe Perches
61520112475SJoe Perches	if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
61620112475SJoe Perches		$name =~ s/(?<!\\)"/\\"/g; ##escape quotes
61720112475SJoe Perches		$name = "\"$name\"";
61820112475SJoe Perches	}
61920112475SJoe Perches
62020112475SJoe Perches	return ($name, $address, $comment);
62120112475SJoe Perches}
62220112475SJoe Perches
62320112475SJoe Perchessub format_email {
62420112475SJoe Perches	my ($name, $address) = @_;
62520112475SJoe Perches
62620112475SJoe Perches	my $formatted_email;
62720112475SJoe Perches
6283705ce5bSJoe Perches	$name = trim($name);
62920112475SJoe Perches	$name =~ s/^\"|\"$//g;
6303705ce5bSJoe Perches	$address = trim($address);
63120112475SJoe Perches
63220112475SJoe Perches	if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
63320112475SJoe Perches		$name =~ s/(?<!\\)"/\\"/g; ##escape quotes
63420112475SJoe Perches		$name = "\"$name\"";
63520112475SJoe Perches	}
63620112475SJoe Perches
63720112475SJoe Perches	if ("$name" eq "") {
63820112475SJoe Perches		$formatted_email = "$address";
63920112475SJoe Perches	} else {
64020112475SJoe Perches		$formatted_email = "$name <$address>";
64120112475SJoe Perches	}
64220112475SJoe Perches
64320112475SJoe Perches	return $formatted_email;
64420112475SJoe Perches}
64520112475SJoe Perches
646000d1cc1SJoe Perchessub which_conf {
647000d1cc1SJoe Perches	my ($conf) = @_;
648000d1cc1SJoe Perches
649000d1cc1SJoe Perches	foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
650000d1cc1SJoe Perches		if (-e "$path/$conf") {
651000d1cc1SJoe Perches			return "$path/$conf";
652000d1cc1SJoe Perches		}
653000d1cc1SJoe Perches	}
654000d1cc1SJoe Perches
655000d1cc1SJoe Perches	return "";
656000d1cc1SJoe Perches}
657000d1cc1SJoe Perches
6580a920b5bSAndy Whitcroftsub expand_tabs {
6590a920b5bSAndy Whitcroft	my ($str) = @_;
6600a920b5bSAndy Whitcroft
6610a920b5bSAndy Whitcroft	my $res = '';
6620a920b5bSAndy Whitcroft	my $n = 0;
6630a920b5bSAndy Whitcroft	for my $c (split(//, $str)) {
6640a920b5bSAndy Whitcroft		if ($c eq "\t") {
6650a920b5bSAndy Whitcroft			$res .= ' ';
6660a920b5bSAndy Whitcroft			$n++;
6670a920b5bSAndy Whitcroft			for (; ($n % 8) != 0; $n++) {
6680a920b5bSAndy Whitcroft				$res .= ' ';
6690a920b5bSAndy Whitcroft			}
6700a920b5bSAndy Whitcroft			next;
6710a920b5bSAndy Whitcroft		}
6720a920b5bSAndy Whitcroft		$res .= $c;
6730a920b5bSAndy Whitcroft		$n++;
6740a920b5bSAndy Whitcroft	}
6750a920b5bSAndy Whitcroft
6760a920b5bSAndy Whitcroft	return $res;
6770a920b5bSAndy Whitcroft}
6786c72ffaaSAndy Whitcroftsub copy_spacing {
679773647a0SAndy Whitcroft	(my $res = shift) =~ tr/\t/ /c;
6806c72ffaaSAndy Whitcroft	return $res;
6816c72ffaaSAndy Whitcroft}
6820a920b5bSAndy Whitcroft
6834a0df2efSAndy Whitcroftsub line_stats {
6844a0df2efSAndy Whitcroft	my ($line) = @_;
6854a0df2efSAndy Whitcroft
6864a0df2efSAndy Whitcroft	# Drop the diff line leader and expand tabs
6874a0df2efSAndy Whitcroft	$line =~ s/^.//;
6884a0df2efSAndy Whitcroft	$line = expand_tabs($line);
6894a0df2efSAndy Whitcroft
6904a0df2efSAndy Whitcroft	# Pick the indent from the front of the line.
6914a0df2efSAndy Whitcroft	my ($white) = ($line =~ /^(\s*)/);
6924a0df2efSAndy Whitcroft
6934a0df2efSAndy Whitcroft	return (length($line), length($white));
6944a0df2efSAndy Whitcroft}
6954a0df2efSAndy Whitcroft
696773647a0SAndy Whitcroftmy $sanitise_quote = '';
697773647a0SAndy Whitcroft
698773647a0SAndy Whitcroftsub sanitise_line_reset {
699773647a0SAndy Whitcroft	my ($in_comment) = @_;
700773647a0SAndy Whitcroft
701773647a0SAndy Whitcroft	if ($in_comment) {
702773647a0SAndy Whitcroft		$sanitise_quote = '*/';
703773647a0SAndy Whitcroft	} else {
704773647a0SAndy Whitcroft		$sanitise_quote = '';
705773647a0SAndy Whitcroft	}
706773647a0SAndy Whitcroft}
70700df344fSAndy Whitcroftsub sanitise_line {
70800df344fSAndy Whitcroft	my ($line) = @_;
70900df344fSAndy Whitcroft
71000df344fSAndy Whitcroft	my $res = '';
71100df344fSAndy Whitcroft	my $l = '';
71200df344fSAndy Whitcroft
713c2fdda0dSAndy Whitcroft	my $qlen = 0;
714773647a0SAndy Whitcroft	my $off = 0;
715773647a0SAndy Whitcroft	my $c;
71600df344fSAndy Whitcroft
717773647a0SAndy Whitcroft	# Always copy over the diff marker.
718773647a0SAndy Whitcroft	$res = substr($line, 0, 1);
719773647a0SAndy Whitcroft
720773647a0SAndy Whitcroft	for ($off = 1; $off < length($line); $off++) {
721773647a0SAndy Whitcroft		$c = substr($line, $off, 1);
722773647a0SAndy Whitcroft
723773647a0SAndy Whitcroft		# Comments we are wacking completly including the begin
724773647a0SAndy Whitcroft		# and end, all to $;.
725773647a0SAndy Whitcroft		if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
726773647a0SAndy Whitcroft			$sanitise_quote = '*/';
727773647a0SAndy Whitcroft
728773647a0SAndy Whitcroft			substr($res, $off, 2, "$;$;");
729773647a0SAndy Whitcroft			$off++;
73000df344fSAndy Whitcroft			next;
731773647a0SAndy Whitcroft		}
73281bc0e02SAndy Whitcroft		if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
733773647a0SAndy Whitcroft			$sanitise_quote = '';
734773647a0SAndy Whitcroft			substr($res, $off, 2, "$;$;");
735773647a0SAndy Whitcroft			$off++;
736773647a0SAndy Whitcroft			next;
737773647a0SAndy Whitcroft		}
738113f04a8SDaniel Walker		if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
739113f04a8SDaniel Walker			$sanitise_quote = '//';
740113f04a8SDaniel Walker
741113f04a8SDaniel Walker			substr($res, $off, 2, $sanitise_quote);
742113f04a8SDaniel Walker			$off++;
743113f04a8SDaniel Walker			next;
744113f04a8SDaniel Walker		}
745773647a0SAndy Whitcroft
746773647a0SAndy Whitcroft		# A \ in a string means ignore the next character.
747773647a0SAndy Whitcroft		if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
748773647a0SAndy Whitcroft		    $c eq "\\") {
749773647a0SAndy Whitcroft			substr($res, $off, 2, 'XX');
750773647a0SAndy Whitcroft			$off++;
751773647a0SAndy Whitcroft			next;
752773647a0SAndy Whitcroft		}
753773647a0SAndy Whitcroft		# Regular quotes.
754773647a0SAndy Whitcroft		if ($c eq "'" || $c eq '"') {
755773647a0SAndy Whitcroft			if ($sanitise_quote eq '') {
756773647a0SAndy Whitcroft				$sanitise_quote = $c;
757773647a0SAndy Whitcroft
758773647a0SAndy Whitcroft				substr($res, $off, 1, $c);
759773647a0SAndy Whitcroft				next;
760773647a0SAndy Whitcroft			} elsif ($sanitise_quote eq $c) {
761773647a0SAndy Whitcroft				$sanitise_quote = '';
76200df344fSAndy Whitcroft			}
76300df344fSAndy Whitcroft		}
764773647a0SAndy Whitcroft
765fae17daeSAndy Whitcroft		#print "c<$c> SQ<$sanitise_quote>\n";
766773647a0SAndy Whitcroft		if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
767773647a0SAndy Whitcroft			substr($res, $off, 1, $;);
768113f04a8SDaniel Walker		} elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
769113f04a8SDaniel Walker			substr($res, $off, 1, $;);
770773647a0SAndy Whitcroft		} elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
771773647a0SAndy Whitcroft			substr($res, $off, 1, 'X');
77200df344fSAndy Whitcroft		} else {
773773647a0SAndy Whitcroft			substr($res, $off, 1, $c);
77400df344fSAndy Whitcroft		}
775c2fdda0dSAndy Whitcroft	}
776c2fdda0dSAndy Whitcroft
777113f04a8SDaniel Walker	if ($sanitise_quote eq '//') {
778113f04a8SDaniel Walker		$sanitise_quote = '';
779113f04a8SDaniel Walker	}
780113f04a8SDaniel Walker
781c2fdda0dSAndy Whitcroft	# The pathname on a #include may be surrounded by '<' and '>'.
782c45dcabdSAndy Whitcroft	if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
783c2fdda0dSAndy Whitcroft		my $clean = 'X' x length($1);
784c2fdda0dSAndy Whitcroft		$res =~ s@\<.*\>@<$clean>@;
785c2fdda0dSAndy Whitcroft
786c2fdda0dSAndy Whitcroft	# The whole of a #error is a string.
787c45dcabdSAndy Whitcroft	} elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
788c2fdda0dSAndy Whitcroft		my $clean = 'X' x length($1);
789c45dcabdSAndy Whitcroft		$res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
790c2fdda0dSAndy Whitcroft	}
791c2fdda0dSAndy Whitcroft
79200df344fSAndy Whitcroft	return $res;
79300df344fSAndy Whitcroft}
79400df344fSAndy Whitcroft
795a6962d72SJoe Perchessub get_quoted_string {
796a6962d72SJoe Perches	my ($line, $rawline) = @_;
797a6962d72SJoe Perches
798a6962d72SJoe Perches	return "" if ($line !~ m/(\"[X]+\")/g);
799a6962d72SJoe Perches	return substr($rawline, $-[0], $+[0] - $-[0]);
800a6962d72SJoe Perches}
801a6962d72SJoe Perches
8028905a67cSAndy Whitcroftsub ctx_statement_block {
8038905a67cSAndy Whitcroft	my ($linenr, $remain, $off) = @_;
8048905a67cSAndy Whitcroft	my $line = $linenr - 1;
8058905a67cSAndy Whitcroft	my $blk = '';
8068905a67cSAndy Whitcroft	my $soff = $off;
8078905a67cSAndy Whitcroft	my $coff = $off - 1;
808773647a0SAndy Whitcroft	my $coff_set = 0;
8098905a67cSAndy Whitcroft
81013214adfSAndy Whitcroft	my $loff = 0;
81113214adfSAndy Whitcroft
8128905a67cSAndy Whitcroft	my $type = '';
8138905a67cSAndy Whitcroft	my $level = 0;
814a2750645SAndy Whitcroft	my @stack = ();
815cf655043SAndy Whitcroft	my $p;
8168905a67cSAndy Whitcroft	my $c;
8178905a67cSAndy Whitcroft	my $len = 0;
81813214adfSAndy Whitcroft
81913214adfSAndy Whitcroft	my $remainder;
8208905a67cSAndy Whitcroft	while (1) {
821a2750645SAndy Whitcroft		@stack = (['', 0]) if ($#stack == -1);
822a2750645SAndy Whitcroft
823773647a0SAndy Whitcroft		#warn "CSB: blk<$blk> remain<$remain>\n";
8248905a67cSAndy Whitcroft		# If we are about to drop off the end, pull in more
8258905a67cSAndy Whitcroft		# context.
8268905a67cSAndy Whitcroft		if ($off >= $len) {
8278905a67cSAndy Whitcroft			for (; $remain > 0; $line++) {
828dea33496SAndy Whitcroft				last if (!defined $lines[$line]);
829c2fdda0dSAndy Whitcroft				next if ($lines[$line] =~ /^-/);
8308905a67cSAndy Whitcroft				$remain--;
83113214adfSAndy Whitcroft				$loff = $len;
832c2fdda0dSAndy Whitcroft				$blk .= $lines[$line] . "\n";
8338905a67cSAndy Whitcroft				$len = length($blk);
8348905a67cSAndy Whitcroft				$line++;
8358905a67cSAndy Whitcroft				last;
8368905a67cSAndy Whitcroft			}
8378905a67cSAndy Whitcroft			# Bail if there is no further context.
8388905a67cSAndy Whitcroft			#warn "CSB: blk<$blk> off<$off> len<$len>\n";
83913214adfSAndy Whitcroft			if ($off >= $len) {
8408905a67cSAndy Whitcroft				last;
8418905a67cSAndy Whitcroft			}
842f74bd194SAndy Whitcroft			if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
843f74bd194SAndy Whitcroft				$level++;
844f74bd194SAndy Whitcroft				$type = '#';
845f74bd194SAndy Whitcroft			}
8468905a67cSAndy Whitcroft		}
847cf655043SAndy Whitcroft		$p = $c;
8488905a67cSAndy Whitcroft		$c = substr($blk, $off, 1);
84913214adfSAndy Whitcroft		$remainder = substr($blk, $off);
8508905a67cSAndy Whitcroft
851773647a0SAndy Whitcroft		#warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
8524635f4fbSAndy Whitcroft
8534635f4fbSAndy Whitcroft		# Handle nested #if/#else.
8544635f4fbSAndy Whitcroft		if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
8554635f4fbSAndy Whitcroft			push(@stack, [ $type, $level ]);
8564635f4fbSAndy Whitcroft		} elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
8574635f4fbSAndy Whitcroft			($type, $level) = @{$stack[$#stack - 1]};
8584635f4fbSAndy Whitcroft		} elsif ($remainder =~ /^#\s*endif\b/) {
8594635f4fbSAndy Whitcroft			($type, $level) = @{pop(@stack)};
8604635f4fbSAndy Whitcroft		}
8614635f4fbSAndy Whitcroft
8628905a67cSAndy Whitcroft		# Statement ends at the ';' or a close '}' at the
8638905a67cSAndy Whitcroft		# outermost level.
8648905a67cSAndy Whitcroft		if ($level == 0 && $c eq ';') {
8658905a67cSAndy Whitcroft			last;
8668905a67cSAndy Whitcroft		}
8678905a67cSAndy Whitcroft
86813214adfSAndy Whitcroft		# An else is really a conditional as long as its not else if
869773647a0SAndy Whitcroft		if ($level == 0 && $coff_set == 0 &&
870773647a0SAndy Whitcroft				(!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
871773647a0SAndy Whitcroft				$remainder =~ /^(else)(?:\s|{)/ &&
872773647a0SAndy Whitcroft				$remainder !~ /^else\s+if\b/) {
873773647a0SAndy Whitcroft			$coff = $off + length($1) - 1;
874773647a0SAndy Whitcroft			$coff_set = 1;
875773647a0SAndy Whitcroft			#warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
876773647a0SAndy Whitcroft			#warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
87713214adfSAndy Whitcroft		}
87813214adfSAndy Whitcroft
8798905a67cSAndy Whitcroft		if (($type eq '' || $type eq '(') && $c eq '(') {
8808905a67cSAndy Whitcroft			$level++;
8818905a67cSAndy Whitcroft			$type = '(';
8828905a67cSAndy Whitcroft		}
8838905a67cSAndy Whitcroft		if ($type eq '(' && $c eq ')') {
8848905a67cSAndy Whitcroft			$level--;
8858905a67cSAndy Whitcroft			$type = ($level != 0)? '(' : '';
8868905a67cSAndy Whitcroft
8878905a67cSAndy Whitcroft			if ($level == 0 && $coff < $soff) {
8888905a67cSAndy Whitcroft				$coff = $off;
889773647a0SAndy Whitcroft				$coff_set = 1;
890773647a0SAndy Whitcroft				#warn "CSB: mark coff<$coff>\n";
8918905a67cSAndy Whitcroft			}
8928905a67cSAndy Whitcroft		}
8938905a67cSAndy Whitcroft		if (($type eq '' || $type eq '{') && $c eq '{') {
8948905a67cSAndy Whitcroft			$level++;
8958905a67cSAndy Whitcroft			$type = '{';
8968905a67cSAndy Whitcroft		}
8978905a67cSAndy Whitcroft		if ($type eq '{' && $c eq '}') {
8988905a67cSAndy Whitcroft			$level--;
8998905a67cSAndy Whitcroft			$type = ($level != 0)? '{' : '';
9008905a67cSAndy Whitcroft
9018905a67cSAndy Whitcroft			if ($level == 0) {
902b998e001SPatrick Pannuto				if (substr($blk, $off + 1, 1) eq ';') {
903b998e001SPatrick Pannuto					$off++;
904b998e001SPatrick Pannuto				}
9058905a67cSAndy Whitcroft				last;
9068905a67cSAndy Whitcroft			}
9078905a67cSAndy Whitcroft		}
908f74bd194SAndy Whitcroft		# Preprocessor commands end at the newline unless escaped.
909f74bd194SAndy Whitcroft		if ($type eq '#' && $c eq "\n" && $p ne "\\") {
910f74bd194SAndy Whitcroft			$level--;
911f74bd194SAndy Whitcroft			$type = '';
912f74bd194SAndy Whitcroft			$off++;
913f74bd194SAndy Whitcroft			last;
914f74bd194SAndy Whitcroft		}
9158905a67cSAndy Whitcroft		$off++;
9168905a67cSAndy Whitcroft	}
917a3bb97a7SAndy Whitcroft	# We are truly at the end, so shuffle to the next line.
91813214adfSAndy Whitcroft	if ($off == $len) {
919a3bb97a7SAndy Whitcroft		$loff = $len + 1;
92013214adfSAndy Whitcroft		$line++;
92113214adfSAndy Whitcroft		$remain--;
92213214adfSAndy Whitcroft	}
9238905a67cSAndy Whitcroft
9248905a67cSAndy Whitcroft	my $statement = substr($blk, $soff, $off - $soff + 1);
9258905a67cSAndy Whitcroft	my $condition = substr($blk, $soff, $coff - $soff + 1);
9268905a67cSAndy Whitcroft
9278905a67cSAndy Whitcroft	#warn "STATEMENT<$statement>\n";
9288905a67cSAndy Whitcroft	#warn "CONDITION<$condition>\n";
9298905a67cSAndy Whitcroft
930773647a0SAndy Whitcroft	#print "coff<$coff> soff<$off> loff<$loff>\n";
93113214adfSAndy Whitcroft
93213214adfSAndy Whitcroft	return ($statement, $condition,
93313214adfSAndy Whitcroft			$line, $remain + 1, $off - $loff + 1, $level);
93413214adfSAndy Whitcroft}
93513214adfSAndy Whitcroft
936cf655043SAndy Whitcroftsub statement_lines {
937cf655043SAndy Whitcroft	my ($stmt) = @_;
938cf655043SAndy Whitcroft
939cf655043SAndy Whitcroft	# Strip the diff line prefixes and rip blank lines at start and end.
940cf655043SAndy Whitcroft	$stmt =~ s/(^|\n)./$1/g;
941cf655043SAndy Whitcroft	$stmt =~ s/^\s*//;
942cf655043SAndy Whitcroft	$stmt =~ s/\s*$//;
943cf655043SAndy Whitcroft
944cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
945cf655043SAndy Whitcroft
946cf655043SAndy Whitcroft	return $#stmt_lines + 2;
947cf655043SAndy Whitcroft}
948cf655043SAndy Whitcroft
949cf655043SAndy Whitcroftsub statement_rawlines {
950cf655043SAndy Whitcroft	my ($stmt) = @_;
951cf655043SAndy Whitcroft
952cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
953cf655043SAndy Whitcroft
954cf655043SAndy Whitcroft	return $#stmt_lines + 2;
955cf655043SAndy Whitcroft}
956cf655043SAndy Whitcroft
957cf655043SAndy Whitcroftsub statement_block_size {
958cf655043SAndy Whitcroft	my ($stmt) = @_;
959cf655043SAndy Whitcroft
960cf655043SAndy Whitcroft	$stmt =~ s/(^|\n)./$1/g;
961cf655043SAndy Whitcroft	$stmt =~ s/^\s*{//;
962cf655043SAndy Whitcroft	$stmt =~ s/}\s*$//;
963cf655043SAndy Whitcroft	$stmt =~ s/^\s*//;
964cf655043SAndy Whitcroft	$stmt =~ s/\s*$//;
965cf655043SAndy Whitcroft
966cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
967cf655043SAndy Whitcroft	my @stmt_statements = ($stmt =~ /;/g);
968cf655043SAndy Whitcroft
969cf655043SAndy Whitcroft	my $stmt_lines = $#stmt_lines + 2;
970cf655043SAndy Whitcroft	my $stmt_statements = $#stmt_statements + 1;
971cf655043SAndy Whitcroft
972cf655043SAndy Whitcroft	if ($stmt_lines > $stmt_statements) {
973cf655043SAndy Whitcroft		return $stmt_lines;
974cf655043SAndy Whitcroft	} else {
975cf655043SAndy Whitcroft		return $stmt_statements;
976cf655043SAndy Whitcroft	}
977cf655043SAndy Whitcroft}
978cf655043SAndy Whitcroft
97913214adfSAndy Whitcroftsub ctx_statement_full {
98013214adfSAndy Whitcroft	my ($linenr, $remain, $off) = @_;
98113214adfSAndy Whitcroft	my ($statement, $condition, $level);
98213214adfSAndy Whitcroft
98313214adfSAndy Whitcroft	my (@chunks);
98413214adfSAndy Whitcroft
985cf655043SAndy Whitcroft	# Grab the first conditional/block pair.
98613214adfSAndy Whitcroft	($statement, $condition, $linenr, $remain, $off, $level) =
98713214adfSAndy Whitcroft				ctx_statement_block($linenr, $remain, $off);
988773647a0SAndy Whitcroft	#print "F: c<$condition> s<$statement> remain<$remain>\n";
98913214adfSAndy Whitcroft	push(@chunks, [ $condition, $statement ]);
990cf655043SAndy Whitcroft	if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
991cf655043SAndy Whitcroft		return ($level, $linenr, @chunks);
992cf655043SAndy Whitcroft	}
993cf655043SAndy Whitcroft
994cf655043SAndy Whitcroft	# Pull in the following conditional/block pairs and see if they
995cf655043SAndy Whitcroft	# could continue the statement.
996cf655043SAndy Whitcroft	for (;;) {
99713214adfSAndy Whitcroft		($statement, $condition, $linenr, $remain, $off, $level) =
99813214adfSAndy Whitcroft				ctx_statement_block($linenr, $remain, $off);
999cf655043SAndy Whitcroft		#print "C: c<$condition> s<$statement> remain<$remain>\n";
1000773647a0SAndy Whitcroft		last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
1001cf655043SAndy Whitcroft		#print "C: push\n";
1002cf655043SAndy Whitcroft		push(@chunks, [ $condition, $statement ]);
100313214adfSAndy Whitcroft	}
100413214adfSAndy Whitcroft
100513214adfSAndy Whitcroft	return ($level, $linenr, @chunks);
10068905a67cSAndy Whitcroft}
10078905a67cSAndy Whitcroft
10084a0df2efSAndy Whitcroftsub ctx_block_get {
1009f0a594c1SAndy Whitcroft	my ($linenr, $remain, $outer, $open, $close, $off) = @_;
10104a0df2efSAndy Whitcroft	my $line;
10114a0df2efSAndy Whitcroft	my $start = $linenr - 1;
10124a0df2efSAndy Whitcroft	my $blk = '';
10134a0df2efSAndy Whitcroft	my @o;
10144a0df2efSAndy Whitcroft	my @c;
10154a0df2efSAndy Whitcroft	my @res = ();
10164a0df2efSAndy Whitcroft
1017f0a594c1SAndy Whitcroft	my $level = 0;
10184635f4fbSAndy Whitcroft	my @stack = ($level);
101900df344fSAndy Whitcroft	for ($line = $start; $remain > 0; $line++) {
102000df344fSAndy Whitcroft		next if ($rawlines[$line] =~ /^-/);
102100df344fSAndy Whitcroft		$remain--;
102200df344fSAndy Whitcroft
102300df344fSAndy Whitcroft		$blk .= $rawlines[$line];
10244635f4fbSAndy Whitcroft
10254635f4fbSAndy Whitcroft		# Handle nested #if/#else.
102601464f30SAndy Whitcroft		if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
10274635f4fbSAndy Whitcroft			push(@stack, $level);
102801464f30SAndy Whitcroft		} elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
10294635f4fbSAndy Whitcroft			$level = $stack[$#stack - 1];
103001464f30SAndy Whitcroft		} elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
10314635f4fbSAndy Whitcroft			$level = pop(@stack);
10324635f4fbSAndy Whitcroft		}
10334635f4fbSAndy Whitcroft
103401464f30SAndy Whitcroft		foreach my $c (split(//, $lines[$line])) {
1035f0a594c1SAndy Whitcroft			##print "C<$c>L<$level><$open$close>O<$off>\n";
1036f0a594c1SAndy Whitcroft			if ($off > 0) {
1037f0a594c1SAndy Whitcroft				$off--;
1038f0a594c1SAndy Whitcroft				next;
1039f0a594c1SAndy Whitcroft			}
10404a0df2efSAndy Whitcroft
1041f0a594c1SAndy Whitcroft			if ($c eq $close && $level > 0) {
1042f0a594c1SAndy Whitcroft				$level--;
1043f0a594c1SAndy Whitcroft				last if ($level == 0);
1044f0a594c1SAndy Whitcroft			} elsif ($c eq $open) {
1045f0a594c1SAndy Whitcroft				$level++;
1046f0a594c1SAndy Whitcroft			}
1047f0a594c1SAndy Whitcroft		}
10484a0df2efSAndy Whitcroft
1049f0a594c1SAndy Whitcroft		if (!$outer || $level <= 1) {
105000df344fSAndy Whitcroft			push(@res, $rawlines[$line]);
10514a0df2efSAndy Whitcroft		}
10524a0df2efSAndy Whitcroft
1053f0a594c1SAndy Whitcroft		last if ($level == 0);
10544a0df2efSAndy Whitcroft	}
10554a0df2efSAndy Whitcroft
1056f0a594c1SAndy Whitcroft	return ($level, @res);
10574a0df2efSAndy Whitcroft}
10584a0df2efSAndy Whitcroftsub ctx_block_outer {
10594a0df2efSAndy Whitcroft	my ($linenr, $remain) = @_;
10604a0df2efSAndy Whitcroft
1061f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
1062f0a594c1SAndy Whitcroft	return @r;
10634a0df2efSAndy Whitcroft}
10644a0df2efSAndy Whitcroftsub ctx_block {
10654a0df2efSAndy Whitcroft	my ($linenr, $remain) = @_;
10664a0df2efSAndy Whitcroft
1067f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1068f0a594c1SAndy Whitcroft	return @r;
1069653d4876SAndy Whitcroft}
1070653d4876SAndy Whitcroftsub ctx_statement {
1071f0a594c1SAndy Whitcroft	my ($linenr, $remain, $off) = @_;
1072f0a594c1SAndy Whitcroft
1073f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1074f0a594c1SAndy Whitcroft	return @r;
1075f0a594c1SAndy Whitcroft}
1076f0a594c1SAndy Whitcroftsub ctx_block_level {
1077653d4876SAndy Whitcroft	my ($linenr, $remain) = @_;
1078653d4876SAndy Whitcroft
1079f0a594c1SAndy Whitcroft	return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
10804a0df2efSAndy Whitcroft}
10819c0ca6f9SAndy Whitcroftsub ctx_statement_level {
10829c0ca6f9SAndy Whitcroft	my ($linenr, $remain, $off) = @_;
10839c0ca6f9SAndy Whitcroft
10849c0ca6f9SAndy Whitcroft	return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
10859c0ca6f9SAndy Whitcroft}
10864a0df2efSAndy Whitcroft
10874a0df2efSAndy Whitcroftsub ctx_locate_comment {
10884a0df2efSAndy Whitcroft	my ($first_line, $end_line) = @_;
10894a0df2efSAndy Whitcroft
10904a0df2efSAndy Whitcroft	# Catch a comment on the end of the line itself.
1091beae6332SAndy Whitcroft	my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
10924a0df2efSAndy Whitcroft	return $current_comment if (defined $current_comment);
10934a0df2efSAndy Whitcroft
10944a0df2efSAndy Whitcroft	# Look through the context and try and figure out if there is a
10954a0df2efSAndy Whitcroft	# comment.
10964a0df2efSAndy Whitcroft	my $in_comment = 0;
10974a0df2efSAndy Whitcroft	$current_comment = '';
10984a0df2efSAndy Whitcroft	for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
109900df344fSAndy Whitcroft		my $line = $rawlines[$linenr - 1];
110000df344fSAndy Whitcroft		#warn "           $line\n";
11014a0df2efSAndy Whitcroft		if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
11024a0df2efSAndy Whitcroft			$in_comment = 1;
11034a0df2efSAndy Whitcroft		}
11044a0df2efSAndy Whitcroft		if ($line =~ m@/\*@) {
11054a0df2efSAndy Whitcroft			$in_comment = 1;
11064a0df2efSAndy Whitcroft		}
11074a0df2efSAndy Whitcroft		if (!$in_comment && $current_comment ne '') {
11084a0df2efSAndy Whitcroft			$current_comment = '';
11094a0df2efSAndy Whitcroft		}
11104a0df2efSAndy Whitcroft		$current_comment .= $line . "\n" if ($in_comment);
11114a0df2efSAndy Whitcroft		if ($line =~ m@\*/@) {
11124a0df2efSAndy Whitcroft			$in_comment = 0;
11134a0df2efSAndy Whitcroft		}
11144a0df2efSAndy Whitcroft	}
11154a0df2efSAndy Whitcroft
11164a0df2efSAndy Whitcroft	chomp($current_comment);
11174a0df2efSAndy Whitcroft	return($current_comment);
11184a0df2efSAndy Whitcroft}
11194a0df2efSAndy Whitcroftsub ctx_has_comment {
11204a0df2efSAndy Whitcroft	my ($first_line, $end_line) = @_;
11214a0df2efSAndy Whitcroft	my $cmt = ctx_locate_comment($first_line, $end_line);
11224a0df2efSAndy Whitcroft
112300df344fSAndy Whitcroft	##print "LINE: $rawlines[$end_line - 1 ]\n";
11244a0df2efSAndy Whitcroft	##print "CMMT: $cmt\n";
11254a0df2efSAndy Whitcroft
11264a0df2efSAndy Whitcroft	return ($cmt ne '');
11274a0df2efSAndy Whitcroft}
11284a0df2efSAndy Whitcroft
11294d001e4dSAndy Whitcroftsub raw_line {
11304d001e4dSAndy Whitcroft	my ($linenr, $cnt) = @_;
11314d001e4dSAndy Whitcroft
11324d001e4dSAndy Whitcroft	my $offset = $linenr - 1;
11334d001e4dSAndy Whitcroft	$cnt++;
11344d001e4dSAndy Whitcroft
11354d001e4dSAndy Whitcroft	my $line;
11364d001e4dSAndy Whitcroft	while ($cnt) {
11374d001e4dSAndy Whitcroft		$line = $rawlines[$offset++];
11384d001e4dSAndy Whitcroft		next if (defined($line) && $line =~ /^-/);
11394d001e4dSAndy Whitcroft		$cnt--;
11404d001e4dSAndy Whitcroft	}
11414d001e4dSAndy Whitcroft
11424d001e4dSAndy Whitcroft	return $line;
11434d001e4dSAndy Whitcroft}
11444d001e4dSAndy Whitcroft
11450a920b5bSAndy Whitcroftsub cat_vet {
11460a920b5bSAndy Whitcroft	my ($vet) = @_;
11479c0ca6f9SAndy Whitcroft	my ($res, $coded);
11480a920b5bSAndy Whitcroft
11499c0ca6f9SAndy Whitcroft	$res = '';
11506c72ffaaSAndy Whitcroft	while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
11516c72ffaaSAndy Whitcroft		$res .= $1;
11526c72ffaaSAndy Whitcroft		if ($2 ne '') {
11539c0ca6f9SAndy Whitcroft			$coded = sprintf("^%c", unpack('C', $2) + 64);
11546c72ffaaSAndy Whitcroft			$res .= $coded;
11556c72ffaaSAndy Whitcroft		}
11569c0ca6f9SAndy Whitcroft	}
11579c0ca6f9SAndy Whitcroft	$res =~ s/$/\$/;
11580a920b5bSAndy Whitcroft
11599c0ca6f9SAndy Whitcroft	return $res;
11600a920b5bSAndy Whitcroft}
11610a920b5bSAndy Whitcroft
1162c2fdda0dSAndy Whitcroftmy $av_preprocessor = 0;
1163cf655043SAndy Whitcroftmy $av_pending;
1164c2fdda0dSAndy Whitcroftmy @av_paren_type;
11651f65f947SAndy Whitcroftmy $av_pend_colon;
1166c2fdda0dSAndy Whitcroft
1167c2fdda0dSAndy Whitcroftsub annotate_reset {
1168c2fdda0dSAndy Whitcroft	$av_preprocessor = 0;
1169cf655043SAndy Whitcroft	$av_pending = '_';
1170cf655043SAndy Whitcroft	@av_paren_type = ('E');
11711f65f947SAndy Whitcroft	$av_pend_colon = 'O';
1172c2fdda0dSAndy Whitcroft}
1173c2fdda0dSAndy Whitcroft
11746c72ffaaSAndy Whitcroftsub annotate_values {
11756c72ffaaSAndy Whitcroft	my ($stream, $type) = @_;
11766c72ffaaSAndy Whitcroft
11776c72ffaaSAndy Whitcroft	my $res;
11781f65f947SAndy Whitcroft	my $var = '_' x length($stream);
11796c72ffaaSAndy Whitcroft	my $cur = $stream;
11806c72ffaaSAndy Whitcroft
1181c2fdda0dSAndy Whitcroft	print "$stream\n" if ($dbg_values > 1);
11826c72ffaaSAndy Whitcroft
11836c72ffaaSAndy Whitcroft	while (length($cur)) {
1184773647a0SAndy Whitcroft		@av_paren_type = ('E') if ($#av_paren_type < 0);
1185cf655043SAndy Whitcroft		print " <" . join('', @av_paren_type) .
1186171ae1a4SAndy Whitcroft				"> <$type> <$av_pending>" if ($dbg_values > 1);
11876c72ffaaSAndy Whitcroft		if ($cur =~ /^(\s+)/o) {
1188c2fdda0dSAndy Whitcroft			print "WS($1)\n" if ($dbg_values > 1);
1189c2fdda0dSAndy Whitcroft			if ($1 =~ /\n/ && $av_preprocessor) {
1190cf655043SAndy Whitcroft				$type = pop(@av_paren_type);
1191c2fdda0dSAndy Whitcroft				$av_preprocessor = 0;
11926c72ffaaSAndy Whitcroft			}
11936c72ffaaSAndy Whitcroft
1194c023e473SFlorian Mickler		} elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
11959446ef56SAndy Whitcroft			print "CAST($1)\n" if ($dbg_values > 1);
11969446ef56SAndy Whitcroft			push(@av_paren_type, $type);
1197addcdceaSAndy Whitcroft			$type = 'c';
11989446ef56SAndy Whitcroft
1199e91b6e26SAndy Whitcroft		} elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
1200c2fdda0dSAndy Whitcroft			print "DECLARE($1)\n" if ($dbg_values > 1);
12016c72ffaaSAndy Whitcroft			$type = 'T';
12026c72ffaaSAndy Whitcroft
1203389a2fe5SAndy Whitcroft		} elsif ($cur =~ /^($Modifier)\s*/) {
1204389a2fe5SAndy Whitcroft			print "MODIFIER($1)\n" if ($dbg_values > 1);
1205389a2fe5SAndy Whitcroft			$type = 'T';
1206389a2fe5SAndy Whitcroft
1207c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
1208171ae1a4SAndy Whitcroft			print "DEFINE($1,$2)\n" if ($dbg_values > 1);
1209c2fdda0dSAndy Whitcroft			$av_preprocessor = 1;
1210171ae1a4SAndy Whitcroft			push(@av_paren_type, $type);
1211171ae1a4SAndy Whitcroft			if ($2 ne '') {
1212cf655043SAndy Whitcroft				$av_pending = 'N';
1213171ae1a4SAndy Whitcroft			}
1214171ae1a4SAndy Whitcroft			$type = 'E';
1215171ae1a4SAndy Whitcroft
1216c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
1217171ae1a4SAndy Whitcroft			print "UNDEF($1)\n" if ($dbg_values > 1);
1218171ae1a4SAndy Whitcroft			$av_preprocessor = 1;
1219171ae1a4SAndy Whitcroft			push(@av_paren_type, $type);
12206c72ffaaSAndy Whitcroft
1221c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
1222cf655043SAndy Whitcroft			print "PRE_START($1)\n" if ($dbg_values > 1);
1223c2fdda0dSAndy Whitcroft			$av_preprocessor = 1;
1224cf655043SAndy Whitcroft
1225cf655043SAndy Whitcroft			push(@av_paren_type, $type);
1226cf655043SAndy Whitcroft			push(@av_paren_type, $type);
1227171ae1a4SAndy Whitcroft			$type = 'E';
1228cf655043SAndy Whitcroft
1229c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
1230cf655043SAndy Whitcroft			print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1231cf655043SAndy Whitcroft			$av_preprocessor = 1;
1232cf655043SAndy Whitcroft
1233cf655043SAndy Whitcroft			push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1234cf655043SAndy Whitcroft
1235171ae1a4SAndy Whitcroft			$type = 'E';
1236cf655043SAndy Whitcroft
1237c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:endif))/o) {
1238cf655043SAndy Whitcroft			print "PRE_END($1)\n" if ($dbg_values > 1);
1239cf655043SAndy Whitcroft
1240cf655043SAndy Whitcroft			$av_preprocessor = 1;
1241cf655043SAndy Whitcroft
1242cf655043SAndy Whitcroft			# Assume all arms of the conditional end as this
1243cf655043SAndy Whitcroft			# one does, and continue as if the #endif was not here.
1244cf655043SAndy Whitcroft			pop(@av_paren_type);
1245cf655043SAndy Whitcroft			push(@av_paren_type, $type);
1246171ae1a4SAndy Whitcroft			$type = 'E';
12476c72ffaaSAndy Whitcroft
12486c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\\\n)/o) {
1249c2fdda0dSAndy Whitcroft			print "PRECONT($1)\n" if ($dbg_values > 1);
12506c72ffaaSAndy Whitcroft
1251171ae1a4SAndy Whitcroft		} elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1252171ae1a4SAndy Whitcroft			print "ATTR($1)\n" if ($dbg_values > 1);
1253171ae1a4SAndy Whitcroft			$av_pending = $type;
1254171ae1a4SAndy Whitcroft			$type = 'N';
1255171ae1a4SAndy Whitcroft
12566c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
1257c2fdda0dSAndy Whitcroft			print "SIZEOF($1)\n" if ($dbg_values > 1);
12586c72ffaaSAndy Whitcroft			if (defined $2) {
1259cf655043SAndy Whitcroft				$av_pending = 'V';
12606c72ffaaSAndy Whitcroft			}
12616c72ffaaSAndy Whitcroft			$type = 'N';
12626c72ffaaSAndy Whitcroft
126314b111c1SAndy Whitcroft		} elsif ($cur =~ /^(if|while|for)\b/o) {
1264c2fdda0dSAndy Whitcroft			print "COND($1)\n" if ($dbg_values > 1);
126514b111c1SAndy Whitcroft			$av_pending = 'E';
12666c72ffaaSAndy Whitcroft			$type = 'N';
12676c72ffaaSAndy Whitcroft
12681f65f947SAndy Whitcroft		} elsif ($cur =~/^(case)/o) {
12691f65f947SAndy Whitcroft			print "CASE($1)\n" if ($dbg_values > 1);
12701f65f947SAndy Whitcroft			$av_pend_colon = 'C';
12711f65f947SAndy Whitcroft			$type = 'N';
12721f65f947SAndy Whitcroft
127314b111c1SAndy Whitcroft		} elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
1274c2fdda0dSAndy Whitcroft			print "KEYWORD($1)\n" if ($dbg_values > 1);
12756c72ffaaSAndy Whitcroft			$type = 'N';
12766c72ffaaSAndy Whitcroft
12776c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\()/o) {
1278c2fdda0dSAndy Whitcroft			print "PAREN('$1')\n" if ($dbg_values > 1);
1279cf655043SAndy Whitcroft			push(@av_paren_type, $av_pending);
1280cf655043SAndy Whitcroft			$av_pending = '_';
12816c72ffaaSAndy Whitcroft			$type = 'N';
12826c72ffaaSAndy Whitcroft
12836c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\))/o) {
1284cf655043SAndy Whitcroft			my $new_type = pop(@av_paren_type);
1285cf655043SAndy Whitcroft			if ($new_type ne '_') {
1286cf655043SAndy Whitcroft				$type = $new_type;
1287c2fdda0dSAndy Whitcroft				print "PAREN('$1') -> $type\n"
1288c2fdda0dSAndy Whitcroft							if ($dbg_values > 1);
12896c72ffaaSAndy Whitcroft			} else {
1290c2fdda0dSAndy Whitcroft				print "PAREN('$1')\n" if ($dbg_values > 1);
12916c72ffaaSAndy Whitcroft			}
12926c72ffaaSAndy Whitcroft
1293c8cb2ca3SAndy Whitcroft		} elsif ($cur =~ /^($Ident)\s*\(/o) {
1294c2fdda0dSAndy Whitcroft			print "FUNC($1)\n" if ($dbg_values > 1);
1295c8cb2ca3SAndy Whitcroft			$type = 'V';
1296cf655043SAndy Whitcroft			$av_pending = 'V';
12976c72ffaaSAndy Whitcroft
12988e761b04SAndy Whitcroft		} elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
12998e761b04SAndy Whitcroft			if (defined $2 && $type eq 'C' || $type eq 'T') {
13001f65f947SAndy Whitcroft				$av_pend_colon = 'B';
13018e761b04SAndy Whitcroft			} elsif ($type eq 'E') {
13028e761b04SAndy Whitcroft				$av_pend_colon = 'L';
13031f65f947SAndy Whitcroft			}
13041f65f947SAndy Whitcroft			print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
13051f65f947SAndy Whitcroft			$type = 'V';
13061f65f947SAndy Whitcroft
13076c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Ident|$Constant)/o) {
1308c2fdda0dSAndy Whitcroft			print "IDENT($1)\n" if ($dbg_values > 1);
13096c72ffaaSAndy Whitcroft			$type = 'V';
13106c72ffaaSAndy Whitcroft
13116c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Assignment)/o) {
1312c2fdda0dSAndy Whitcroft			print "ASSIGN($1)\n" if ($dbg_values > 1);
13136c72ffaaSAndy Whitcroft			$type = 'N';
13146c72ffaaSAndy Whitcroft
1315cf655043SAndy Whitcroft		} elsif ($cur =~/^(;|{|})/) {
1316c2fdda0dSAndy Whitcroft			print "END($1)\n" if ($dbg_values > 1);
131713214adfSAndy Whitcroft			$type = 'E';
13181f65f947SAndy Whitcroft			$av_pend_colon = 'O';
131913214adfSAndy Whitcroft
13208e761b04SAndy Whitcroft		} elsif ($cur =~/^(,)/) {
13218e761b04SAndy Whitcroft			print "COMMA($1)\n" if ($dbg_values > 1);
13228e761b04SAndy Whitcroft			$type = 'C';
13238e761b04SAndy Whitcroft
13241f65f947SAndy Whitcroft		} elsif ($cur =~ /^(\?)/o) {
13251f65f947SAndy Whitcroft			print "QUESTION($1)\n" if ($dbg_values > 1);
13261f65f947SAndy Whitcroft			$type = 'N';
13271f65f947SAndy Whitcroft
13281f65f947SAndy Whitcroft		} elsif ($cur =~ /^(:)/o) {
13291f65f947SAndy Whitcroft			print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
13301f65f947SAndy Whitcroft
13311f65f947SAndy Whitcroft			substr($var, length($res), 1, $av_pend_colon);
13321f65f947SAndy Whitcroft			if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
13331f65f947SAndy Whitcroft				$type = 'E';
13341f65f947SAndy Whitcroft			} else {
13351f65f947SAndy Whitcroft				$type = 'N';
13361f65f947SAndy Whitcroft			}
13371f65f947SAndy Whitcroft			$av_pend_colon = 'O';
13381f65f947SAndy Whitcroft
13398e761b04SAndy Whitcroft		} elsif ($cur =~ /^(\[)/o) {
134013214adfSAndy Whitcroft			print "CLOSE($1)\n" if ($dbg_values > 1);
13416c72ffaaSAndy Whitcroft			$type = 'N';
13426c72ffaaSAndy Whitcroft
13430d413866SAndy Whitcroft		} elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
134474048ed8SAndy Whitcroft			my $variant;
134574048ed8SAndy Whitcroft
134674048ed8SAndy Whitcroft			print "OPV($1)\n" if ($dbg_values > 1);
134774048ed8SAndy Whitcroft			if ($type eq 'V') {
134874048ed8SAndy Whitcroft				$variant = 'B';
134974048ed8SAndy Whitcroft			} else {
135074048ed8SAndy Whitcroft				$variant = 'U';
135174048ed8SAndy Whitcroft			}
135274048ed8SAndy Whitcroft
135374048ed8SAndy Whitcroft			substr($var, length($res), 1, $variant);
135474048ed8SAndy Whitcroft			$type = 'N';
135574048ed8SAndy Whitcroft
13566c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Operators)/o) {
1357c2fdda0dSAndy Whitcroft			print "OP($1)\n" if ($dbg_values > 1);
13586c72ffaaSAndy Whitcroft			if ($1 ne '++' && $1 ne '--') {
13596c72ffaaSAndy Whitcroft				$type = 'N';
13606c72ffaaSAndy Whitcroft			}
13616c72ffaaSAndy Whitcroft
13626c72ffaaSAndy Whitcroft		} elsif ($cur =~ /(^.)/o) {
1363c2fdda0dSAndy Whitcroft			print "C($1)\n" if ($dbg_values > 1);
13646c72ffaaSAndy Whitcroft		}
13656c72ffaaSAndy Whitcroft		if (defined $1) {
13666c72ffaaSAndy Whitcroft			$cur = substr($cur, length($1));
13676c72ffaaSAndy Whitcroft			$res .= $type x length($1);
13686c72ffaaSAndy Whitcroft		}
13696c72ffaaSAndy Whitcroft	}
13706c72ffaaSAndy Whitcroft
13711f65f947SAndy Whitcroft	return ($res, $var);
13726c72ffaaSAndy Whitcroft}
13736c72ffaaSAndy Whitcroft
13748905a67cSAndy Whitcroftsub possible {
137513214adfSAndy Whitcroft	my ($possible, $line) = @_;
13769a974fdbSAndy Whitcroft	my $notPermitted = qr{(?:
13770776e594SAndy Whitcroft		^(?:
13780776e594SAndy Whitcroft			$Modifier|
13790776e594SAndy Whitcroft			$Storage|
13800776e594SAndy Whitcroft			$Type|
13819a974fdbSAndy Whitcroft			DEFINE_\S+
13829a974fdbSAndy Whitcroft		)$|
13839a974fdbSAndy Whitcroft		^(?:
13840776e594SAndy Whitcroft			goto|
13850776e594SAndy Whitcroft			return|
13860776e594SAndy Whitcroft			case|
13870776e594SAndy Whitcroft			else|
13880776e594SAndy Whitcroft			asm|__asm__|
138989a88353SAndy Whitcroft			do|
139089a88353SAndy Whitcroft			\#|
139189a88353SAndy Whitcroft			\#\#|
13929a974fdbSAndy Whitcroft		)(?:\s|$)|
13930776e594SAndy Whitcroft		^(?:typedef|struct|enum)\b
13949a974fdbSAndy Whitcroft	    )}x;
13959a974fdbSAndy Whitcroft	warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
13969a974fdbSAndy Whitcroft	if ($possible !~ $notPermitted) {
1397c45dcabdSAndy Whitcroft		# Check for modifiers.
1398c45dcabdSAndy Whitcroft		$possible =~ s/\s*$Storage\s*//g;
1399c45dcabdSAndy Whitcroft		$possible =~ s/\s*$Sparse\s*//g;
1400c45dcabdSAndy Whitcroft		if ($possible =~ /^\s*$/) {
1401c45dcabdSAndy Whitcroft
1402c45dcabdSAndy Whitcroft		} elsif ($possible =~ /\s/) {
1403c45dcabdSAndy Whitcroft			$possible =~ s/\s*$Type\s*//g;
1404d2506586SAndy Whitcroft			for my $modifier (split(' ', $possible)) {
14059a974fdbSAndy Whitcroft				if ($modifier !~ $notPermitted) {
1406d2506586SAndy Whitcroft					warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1407d2506586SAndy Whitcroft					push(@modifierList, $modifier);
1408d2506586SAndy Whitcroft				}
14099a974fdbSAndy Whitcroft			}
1410c45dcabdSAndy Whitcroft
1411c45dcabdSAndy Whitcroft		} else {
141213214adfSAndy Whitcroft			warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
14138905a67cSAndy Whitcroft			push(@typeList, $possible);
1414c45dcabdSAndy Whitcroft		}
14158905a67cSAndy Whitcroft		build_types();
14160776e594SAndy Whitcroft	} else {
14170776e594SAndy Whitcroft		warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
14188905a67cSAndy Whitcroft	}
14198905a67cSAndy Whitcroft}
14208905a67cSAndy Whitcroft
14216c72ffaaSAndy Whitcroftmy $prefix = '';
14226c72ffaaSAndy Whitcroft
1423000d1cc1SJoe Perchessub show_type {
142491bfe484SJoe Perches	return defined $use_type{$_[0]} if (scalar keys %use_type > 0);
142591bfe484SJoe Perches
1426000d1cc1SJoe Perches	return !defined $ignore_type{$_[0]};
1427000d1cc1SJoe Perches}
1428000d1cc1SJoe Perches
1429f0a594c1SAndy Whitcroftsub report {
1430000d1cc1SJoe Perches	if (!show_type($_[1]) ||
1431000d1cc1SJoe Perches	    (defined $tst_only && $_[2] !~ /\Q$tst_only\E/)) {
1432773647a0SAndy Whitcroft		return 0;
1433773647a0SAndy Whitcroft	}
1434000d1cc1SJoe Perches	my $line;
1435000d1cc1SJoe Perches	if ($show_types) {
1436000d1cc1SJoe Perches		$line = "$prefix$_[0]:$_[1]: $_[2]\n";
1437000d1cc1SJoe Perches	} else {
1438000d1cc1SJoe Perches		$line = "$prefix$_[0]: $_[2]\n";
1439000d1cc1SJoe Perches	}
14408905a67cSAndy Whitcroft	$line = (split('\n', $line))[0] . "\n" if ($terse);
14418905a67cSAndy Whitcroft
144213214adfSAndy Whitcroft	push(our @report, $line);
1443773647a0SAndy Whitcroft
1444773647a0SAndy Whitcroft	return 1;
1445f0a594c1SAndy Whitcroft}
1446f0a594c1SAndy Whitcroftsub report_dump {
144713214adfSAndy Whitcroft	our @report;
1448f0a594c1SAndy Whitcroft}
1449000d1cc1SJoe Perches
1450de7d4f0eSAndy Whitcroftsub ERROR {
1451000d1cc1SJoe Perches	if (report("ERROR", $_[0], $_[1])) {
1452de7d4f0eSAndy Whitcroft		our $clean = 0;
14536c72ffaaSAndy Whitcroft		our $cnt_error++;
14543705ce5bSJoe Perches		return 1;
1455de7d4f0eSAndy Whitcroft	}
14563705ce5bSJoe Perches	return 0;
1457773647a0SAndy Whitcroft}
1458de7d4f0eSAndy Whitcroftsub WARN {
1459000d1cc1SJoe Perches	if (report("WARNING", $_[0], $_[1])) {
1460de7d4f0eSAndy Whitcroft		our $clean = 0;
14616c72ffaaSAndy Whitcroft		our $cnt_warn++;
14623705ce5bSJoe Perches		return 1;
1463de7d4f0eSAndy Whitcroft	}
14643705ce5bSJoe Perches	return 0;
1465773647a0SAndy Whitcroft}
1466de7d4f0eSAndy Whitcroftsub CHK {
1467000d1cc1SJoe Perches	if ($check && report("CHECK", $_[0], $_[1])) {
1468de7d4f0eSAndy Whitcroft		our $clean = 0;
14696c72ffaaSAndy Whitcroft		our $cnt_chk++;
14703705ce5bSJoe Perches		return 1;
14716c72ffaaSAndy Whitcroft	}
14723705ce5bSJoe Perches	return 0;
1473de7d4f0eSAndy Whitcroft}
1474de7d4f0eSAndy Whitcroft
14756ecd9674SAndy Whitcroftsub check_absolute_file {
14766ecd9674SAndy Whitcroft	my ($absolute, $herecurr) = @_;
14776ecd9674SAndy Whitcroft	my $file = $absolute;
14786ecd9674SAndy Whitcroft
14796ecd9674SAndy Whitcroft	##print "absolute<$absolute>\n";
14806ecd9674SAndy Whitcroft
14816ecd9674SAndy Whitcroft	# See if any suffix of this path is a path within the tree.
14826ecd9674SAndy Whitcroft	while ($file =~ s@^[^/]*/@@) {
14836ecd9674SAndy Whitcroft		if (-f "$root/$file") {
14846ecd9674SAndy Whitcroft			##print "file<$file>\n";
14856ecd9674SAndy Whitcroft			last;
14866ecd9674SAndy Whitcroft		}
14876ecd9674SAndy Whitcroft	}
14886ecd9674SAndy Whitcroft	if (! -f _)  {
14896ecd9674SAndy Whitcroft		return 0;
14906ecd9674SAndy Whitcroft	}
14916ecd9674SAndy Whitcroft
14926ecd9674SAndy Whitcroft	# It is, so see if the prefix is acceptable.
14936ecd9674SAndy Whitcroft	my $prefix = $absolute;
14946ecd9674SAndy Whitcroft	substr($prefix, -length($file)) = '';
14956ecd9674SAndy Whitcroft
14966ecd9674SAndy Whitcroft	##print "prefix<$prefix>\n";
14976ecd9674SAndy Whitcroft	if ($prefix ne ".../") {
1498000d1cc1SJoe Perches		WARN("USE_RELATIVE_PATH",
1499000d1cc1SJoe Perches		     "use relative pathname instead of absolute in changelog text\n" . $herecurr);
15006ecd9674SAndy Whitcroft	}
15016ecd9674SAndy Whitcroft}
15026ecd9674SAndy Whitcroft
15033705ce5bSJoe Perchessub trim {
15043705ce5bSJoe Perches	my ($string) = @_;
15053705ce5bSJoe Perches
1506b34c648bSJoe Perches	$string =~ s/^\s+|\s+$//g;
1507b34c648bSJoe Perches
1508b34c648bSJoe Perches	return $string;
1509b34c648bSJoe Perches}
1510b34c648bSJoe Perches
1511b34c648bSJoe Perchessub ltrim {
1512b34c648bSJoe Perches	my ($string) = @_;
1513b34c648bSJoe Perches
1514b34c648bSJoe Perches	$string =~ s/^\s+//;
1515b34c648bSJoe Perches
1516b34c648bSJoe Perches	return $string;
1517b34c648bSJoe Perches}
1518b34c648bSJoe Perches
1519b34c648bSJoe Perchessub rtrim {
1520b34c648bSJoe Perches	my ($string) = @_;
1521b34c648bSJoe Perches
1522b34c648bSJoe Perches	$string =~ s/\s+$//;
15233705ce5bSJoe Perches
15243705ce5bSJoe Perches	return $string;
15253705ce5bSJoe Perches}
15263705ce5bSJoe Perches
152752ea8506SJoe Perchessub string_find_replace {
152852ea8506SJoe Perches	my ($string, $find, $replace) = @_;
152952ea8506SJoe Perches
153052ea8506SJoe Perches	$string =~ s/$find/$replace/g;
153152ea8506SJoe Perches
153252ea8506SJoe Perches	return $string;
153352ea8506SJoe Perches}
153452ea8506SJoe Perches
15353705ce5bSJoe Perchessub tabify {
15363705ce5bSJoe Perches	my ($leading) = @_;
15373705ce5bSJoe Perches
15383705ce5bSJoe Perches	my $source_indent = 8;
15393705ce5bSJoe Perches	my $max_spaces_before_tab = $source_indent - 1;
15403705ce5bSJoe Perches	my $spaces_to_tab = " " x $source_indent;
15413705ce5bSJoe Perches
15423705ce5bSJoe Perches	#convert leading spaces to tabs
15433705ce5bSJoe Perches	1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
15443705ce5bSJoe Perches	#Remove spaces before a tab
15453705ce5bSJoe Perches	1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
15463705ce5bSJoe Perches
15473705ce5bSJoe Perches	return "$leading";
15483705ce5bSJoe Perches}
15493705ce5bSJoe Perches
1550d1fe9c09SJoe Perchessub pos_last_openparen {
1551d1fe9c09SJoe Perches	my ($line) = @_;
1552d1fe9c09SJoe Perches
1553d1fe9c09SJoe Perches	my $pos = 0;
1554d1fe9c09SJoe Perches
1555d1fe9c09SJoe Perches	my $opens = $line =~ tr/\(/\(/;
1556d1fe9c09SJoe Perches	my $closes = $line =~ tr/\)/\)/;
1557d1fe9c09SJoe Perches
1558d1fe9c09SJoe Perches	my $last_openparen = 0;
1559d1fe9c09SJoe Perches
1560d1fe9c09SJoe Perches	if (($opens == 0) || ($closes >= $opens)) {
1561d1fe9c09SJoe Perches		return -1;
1562d1fe9c09SJoe Perches	}
1563d1fe9c09SJoe Perches
1564d1fe9c09SJoe Perches	my $len = length($line);
1565d1fe9c09SJoe Perches
1566d1fe9c09SJoe Perches	for ($pos = 0; $pos < $len; $pos++) {
1567d1fe9c09SJoe Perches		my $string = substr($line, $pos);
1568d1fe9c09SJoe Perches		if ($string =~ /^($FuncArg|$balanced_parens)/) {
1569d1fe9c09SJoe Perches			$pos += length($1) - 1;
1570d1fe9c09SJoe Perches		} elsif (substr($line, $pos, 1) eq '(') {
1571d1fe9c09SJoe Perches			$last_openparen = $pos;
1572d1fe9c09SJoe Perches		} elsif (index($string, '(') == -1) {
1573d1fe9c09SJoe Perches			last;
1574d1fe9c09SJoe Perches		}
1575d1fe9c09SJoe Perches	}
1576d1fe9c09SJoe Perches
1577d1fe9c09SJoe Perches	return $last_openparen + 1;
1578d1fe9c09SJoe Perches}
1579d1fe9c09SJoe Perches
15800a920b5bSAndy Whitcroftsub process {
15810a920b5bSAndy Whitcroft	my $filename = shift;
15820a920b5bSAndy Whitcroft
15830a920b5bSAndy Whitcroft	my $linenr=0;
15840a920b5bSAndy Whitcroft	my $prevline="";
1585c2fdda0dSAndy Whitcroft	my $prevrawline="";
15860a920b5bSAndy Whitcroft	my $stashline="";
1587c2fdda0dSAndy Whitcroft	my $stashrawline="";
15880a920b5bSAndy Whitcroft
15894a0df2efSAndy Whitcroft	my $length;
15900a920b5bSAndy Whitcroft	my $indent;
15910a920b5bSAndy Whitcroft	my $previndent=0;
15920a920b5bSAndy Whitcroft	my $stashindent=0;
15930a920b5bSAndy Whitcroft
1594de7d4f0eSAndy Whitcroft	our $clean = 1;
15950a920b5bSAndy Whitcroft	my $signoff = 0;
15960a920b5bSAndy Whitcroft	my $is_patch = 0;
15970a920b5bSAndy Whitcroft
159815662b3eSJoe Perches	my $in_header_lines = 1;
159915662b3eSJoe Perches	my $in_commit_log = 0;		#Scanning lines before patch
160015662b3eSJoe Perches
1601fa64205dSPasi Savanainen	my $non_utf8_charset = 0;
1602fa64205dSPasi Savanainen
160313214adfSAndy Whitcroft	our @report = ();
16046c72ffaaSAndy Whitcroft	our $cnt_lines = 0;
16056c72ffaaSAndy Whitcroft	our $cnt_error = 0;
16066c72ffaaSAndy Whitcroft	our $cnt_warn = 0;
16076c72ffaaSAndy Whitcroft	our $cnt_chk = 0;
16086c72ffaaSAndy Whitcroft
16090a920b5bSAndy Whitcroft	# Trace the real file/line as we go.
16100a920b5bSAndy Whitcroft	my $realfile = '';
16110a920b5bSAndy Whitcroft	my $realline = 0;
16120a920b5bSAndy Whitcroft	my $realcnt = 0;
16130a920b5bSAndy Whitcroft	my $here = '';
16140a920b5bSAndy Whitcroft	my $in_comment = 0;
1615c2fdda0dSAndy Whitcroft	my $comment_edge = 0;
16160a920b5bSAndy Whitcroft	my $first_line = 0;
16171e855726SWolfram Sang	my $p1_prefix = '';
16180a920b5bSAndy Whitcroft
161913214adfSAndy Whitcroft	my $prev_values = 'E';
162013214adfSAndy Whitcroft
162113214adfSAndy Whitcroft	# suppression flags
1622773647a0SAndy Whitcroft	my %suppress_ifbraces;
1623170d3a22SAndy Whitcroft	my %suppress_whiletrailers;
16242b474a1aSAndy Whitcroft	my %suppress_export;
16253e469cdcSAndy Whitcroft	my $suppress_statement = 0;
1626653d4876SAndy Whitcroft
16277e51f197SJoe Perches	my %signatures = ();
1628323c1260SJoe Perches
1629c2fdda0dSAndy Whitcroft	# Pre-scan the patch sanitizing the lines.
1630de7d4f0eSAndy Whitcroft	# Pre-scan the patch looking for any __setup documentation.
1631c2fdda0dSAndy Whitcroft	#
1632de7d4f0eSAndy Whitcroft	my @setup_docs = ();
1633de7d4f0eSAndy Whitcroft	my $setup_docs = 0;
1634773647a0SAndy Whitcroft
1635d8b07710SJoe Perches	my $camelcase_file_seeded = 0;
1636d8b07710SJoe Perches
1637773647a0SAndy Whitcroft	sanitise_line_reset();
1638c2fdda0dSAndy Whitcroft	my $line;
1639c2fdda0dSAndy Whitcroft	foreach my $rawline (@rawlines) {
1640773647a0SAndy Whitcroft		$linenr++;
1641773647a0SAndy Whitcroft		$line = $rawline;
1642c2fdda0dSAndy Whitcroft
16433705ce5bSJoe Perches		push(@fixed, $rawline) if ($fix);
16443705ce5bSJoe Perches
1645773647a0SAndy Whitcroft		if ($rawline=~/^\+\+\+\s+(\S+)/) {
1646de7d4f0eSAndy Whitcroft			$setup_docs = 0;
1647de7d4f0eSAndy Whitcroft			if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
1648de7d4f0eSAndy Whitcroft				$setup_docs = 1;
1649de7d4f0eSAndy Whitcroft			}
1650773647a0SAndy Whitcroft			#next;
1651de7d4f0eSAndy Whitcroft		}
1652773647a0SAndy Whitcroft		if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1653773647a0SAndy Whitcroft			$realline=$1-1;
1654773647a0SAndy Whitcroft			if (defined $2) {
1655773647a0SAndy Whitcroft				$realcnt=$3+1;
1656773647a0SAndy Whitcroft			} else {
1657773647a0SAndy Whitcroft				$realcnt=1+1;
1658773647a0SAndy Whitcroft			}
1659c45dcabdSAndy Whitcroft			$in_comment = 0;
1660773647a0SAndy Whitcroft
1661773647a0SAndy Whitcroft			# Guestimate if this is a continuing comment.  Run
1662773647a0SAndy Whitcroft			# the context looking for a comment "edge".  If this
1663773647a0SAndy Whitcroft			# edge is a close comment then we must be in a comment
1664773647a0SAndy Whitcroft			# at context start.
1665773647a0SAndy Whitcroft			my $edge;
166601fa9147SAndy Whitcroft			my $cnt = $realcnt;
166701fa9147SAndy Whitcroft			for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
166801fa9147SAndy Whitcroft				next if (defined $rawlines[$ln - 1] &&
166901fa9147SAndy Whitcroft					 $rawlines[$ln - 1] =~ /^-/);
167001fa9147SAndy Whitcroft				$cnt--;
167101fa9147SAndy Whitcroft				#print "RAW<$rawlines[$ln - 1]>\n";
1672721c1cb6SAndy Whitcroft				last if (!defined $rawlines[$ln - 1]);
1673fae17daeSAndy Whitcroft				if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
1674fae17daeSAndy Whitcroft				    $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
1675fae17daeSAndy Whitcroft					($edge) = $1;
1676fae17daeSAndy Whitcroft					last;
1677fae17daeSAndy Whitcroft				}
1678773647a0SAndy Whitcroft			}
1679773647a0SAndy Whitcroft			if (defined $edge && $edge eq '*/') {
1680773647a0SAndy Whitcroft				$in_comment = 1;
1681773647a0SAndy Whitcroft			}
1682773647a0SAndy Whitcroft
1683773647a0SAndy Whitcroft			# Guestimate if this is a continuing comment.  If this
1684773647a0SAndy Whitcroft			# is the start of a diff block and this line starts
1685773647a0SAndy Whitcroft			# ' *' then it is very likely a comment.
1686773647a0SAndy Whitcroft			if (!defined $edge &&
168783242e0cSAndy Whitcroft			    $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
1688773647a0SAndy Whitcroft			{
1689773647a0SAndy Whitcroft				$in_comment = 1;
1690773647a0SAndy Whitcroft			}
1691773647a0SAndy Whitcroft
1692773647a0SAndy Whitcroft			##print "COMMENT:$in_comment edge<$edge> $rawline\n";
1693773647a0SAndy Whitcroft			sanitise_line_reset($in_comment);
1694773647a0SAndy Whitcroft
1695171ae1a4SAndy Whitcroft		} elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
1696773647a0SAndy Whitcroft			# Standardise the strings and chars within the input to
1697171ae1a4SAndy Whitcroft			# simplify matching -- only bother with positive lines.
1698773647a0SAndy Whitcroft			$line = sanitise_line($rawline);
1699773647a0SAndy Whitcroft		}
1700773647a0SAndy Whitcroft		push(@lines, $line);
1701773647a0SAndy Whitcroft
1702773647a0SAndy Whitcroft		if ($realcnt > 1) {
1703773647a0SAndy Whitcroft			$realcnt-- if ($line =~ /^(?:\+| |$)/);
1704773647a0SAndy Whitcroft		} else {
1705773647a0SAndy Whitcroft			$realcnt = 0;
1706773647a0SAndy Whitcroft		}
1707773647a0SAndy Whitcroft
1708773647a0SAndy Whitcroft		#print "==>$rawline\n";
1709773647a0SAndy Whitcroft		#print "-->$line\n";
1710de7d4f0eSAndy Whitcroft
1711de7d4f0eSAndy Whitcroft		if ($setup_docs && $line =~ /^\+/) {
1712de7d4f0eSAndy Whitcroft			push(@setup_docs, $line);
1713de7d4f0eSAndy Whitcroft		}
1714de7d4f0eSAndy Whitcroft	}
1715de7d4f0eSAndy Whitcroft
17166c72ffaaSAndy Whitcroft	$prefix = '';
17176c72ffaaSAndy Whitcroft
1718773647a0SAndy Whitcroft	$realcnt = 0;
1719773647a0SAndy Whitcroft	$linenr = 0;
17200a920b5bSAndy Whitcroft	foreach my $line (@lines) {
17210a920b5bSAndy Whitcroft		$linenr++;
17221b5539b1SJoe Perches		my $sline = $line;	#copy of $line
17231b5539b1SJoe Perches		$sline =~ s/$;/ /g;	#with comments as spaces
17240a920b5bSAndy Whitcroft
1725c2fdda0dSAndy Whitcroft		my $rawline = $rawlines[$linenr - 1];
17266c72ffaaSAndy Whitcroft
17270a920b5bSAndy Whitcroft#extract the line range in the file after the patch is applied
17286c72ffaaSAndy Whitcroft		if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
17290a920b5bSAndy Whitcroft			$is_patch = 1;
17304a0df2efSAndy Whitcroft			$first_line = $linenr + 1;
17310a920b5bSAndy Whitcroft			$realline=$1-1;
17320a920b5bSAndy Whitcroft			if (defined $2) {
17330a920b5bSAndy Whitcroft				$realcnt=$3+1;
17340a920b5bSAndy Whitcroft			} else {
17350a920b5bSAndy Whitcroft				$realcnt=1+1;
17360a920b5bSAndy Whitcroft			}
1737c2fdda0dSAndy Whitcroft			annotate_reset();
173813214adfSAndy Whitcroft			$prev_values = 'E';
173913214adfSAndy Whitcroft
1740773647a0SAndy Whitcroft			%suppress_ifbraces = ();
1741170d3a22SAndy Whitcroft			%suppress_whiletrailers = ();
17422b474a1aSAndy Whitcroft			%suppress_export = ();
17433e469cdcSAndy Whitcroft			$suppress_statement = 0;
17440a920b5bSAndy Whitcroft			next;
17450a920b5bSAndy Whitcroft
17464a0df2efSAndy Whitcroft# track the line number as we move through the hunk, note that
17474a0df2efSAndy Whitcroft# new versions of GNU diff omit the leading space on completely
17484a0df2efSAndy Whitcroft# blank context lines so we need to count that too.
1749773647a0SAndy Whitcroft		} elsif ($line =~ /^( |\+|$)/) {
17500a920b5bSAndy Whitcroft			$realline++;
1751d8aaf121SAndy Whitcroft			$realcnt-- if ($realcnt != 0);
17520a920b5bSAndy Whitcroft
17534a0df2efSAndy Whitcroft			# Measure the line length and indent.
1754c2fdda0dSAndy Whitcroft			($length, $indent) = line_stats($rawline);
17550a920b5bSAndy Whitcroft
17560a920b5bSAndy Whitcroft			# Track the previous line.
17570a920b5bSAndy Whitcroft			($prevline, $stashline) = ($stashline, $line);
17580a920b5bSAndy Whitcroft			($previndent, $stashindent) = ($stashindent, $indent);
1759c2fdda0dSAndy Whitcroft			($prevrawline, $stashrawline) = ($stashrawline, $rawline);
1760c2fdda0dSAndy Whitcroft
1761773647a0SAndy Whitcroft			#warn "line<$line>\n";
17626c72ffaaSAndy Whitcroft
1763d8aaf121SAndy Whitcroft		} elsif ($realcnt == 1) {
1764d8aaf121SAndy Whitcroft			$realcnt--;
17650a920b5bSAndy Whitcroft		}
17660a920b5bSAndy Whitcroft
1767cc77cdcaSAndy Whitcroft		my $hunk_line = ($realcnt != 0);
1768cc77cdcaSAndy Whitcroft
17690a920b5bSAndy Whitcroft#make up the handle for any error we report on this line
1770773647a0SAndy Whitcroft		$prefix = "$filename:$realline: " if ($emacs && $file);
1771773647a0SAndy Whitcroft		$prefix = "$filename:$linenr: " if ($emacs && !$file);
1772773647a0SAndy Whitcroft
17736c72ffaaSAndy Whitcroft		$here = "#$linenr: " if (!$file);
17746c72ffaaSAndy Whitcroft		$here = "#$realline: " if ($file);
1775773647a0SAndy Whitcroft
1776773647a0SAndy Whitcroft		# extract the filename as it passes
17773bf9a009SRabin Vincent		if ($line =~ /^diff --git.*?(\S+)$/) {
17783bf9a009SRabin Vincent			$realfile = $1;
17792b7ab453SJoe Perches			$realfile =~ s@^([^/]*)/@@ if (!$file);
1780270c49a0SJoe Perches			$in_commit_log = 0;
17813bf9a009SRabin Vincent		} elsif ($line =~ /^\+\+\+\s+(\S+)/) {
1782773647a0SAndy Whitcroft			$realfile = $1;
17832b7ab453SJoe Perches			$realfile =~ s@^([^/]*)/@@ if (!$file);
1784270c49a0SJoe Perches			$in_commit_log = 0;
17851e855726SWolfram Sang
17861e855726SWolfram Sang			$p1_prefix = $1;
1787e2f7aa4bSAndy Whitcroft			if (!$file && $tree && $p1_prefix ne '' &&
1788e2f7aa4bSAndy Whitcroft			    -e "$root/$p1_prefix") {
1789000d1cc1SJoe Perches				WARN("PATCH_PREFIX",
1790000d1cc1SJoe Perches				     "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
17911e855726SWolfram Sang			}
1792773647a0SAndy Whitcroft
1793c1ab3326SAndy Whitcroft			if ($realfile =~ m@^include/asm/@) {
1794000d1cc1SJoe Perches				ERROR("MODIFIED_INCLUDE_ASM",
1795000d1cc1SJoe Perches				      "do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
1796773647a0SAndy Whitcroft			}
1797773647a0SAndy Whitcroft			next;
1798773647a0SAndy Whitcroft		}
1799773647a0SAndy Whitcroft
1800389834b6SRandy Dunlap		$here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
18010a920b5bSAndy Whitcroft
1802c2fdda0dSAndy Whitcroft		my $hereline = "$here\n$rawline\n";
1803c2fdda0dSAndy Whitcroft		my $herecurr = "$here\n$rawline\n";
1804c2fdda0dSAndy Whitcroft		my $hereprev = "$here\n$prevrawline\n$rawline\n";
18050a920b5bSAndy Whitcroft
18066c72ffaaSAndy Whitcroft		$cnt_lines++ if ($realcnt != 0);
18076c72ffaaSAndy Whitcroft
18083bf9a009SRabin Vincent# Check for incorrect file permissions
18093bf9a009SRabin Vincent		if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
18103bf9a009SRabin Vincent			my $permhere = $here . "FILE: $realfile\n";
181104db4d25SJoe Perches			if ($realfile !~ m@scripts/@ &&
181204db4d25SJoe Perches			    $realfile !~ /\.(py|pl|awk|sh)$/) {
1813000d1cc1SJoe Perches				ERROR("EXECUTE_PERMISSIONS",
1814000d1cc1SJoe Perches				      "do not set execute permissions for source files\n" . $permhere);
18153bf9a009SRabin Vincent			}
18163bf9a009SRabin Vincent		}
18173bf9a009SRabin Vincent
181820112475SJoe Perches# Check the patch for a signoff:
1819d8aaf121SAndy Whitcroft		if ($line =~ /^\s*signed-off-by:/i) {
18204a0df2efSAndy Whitcroft			$signoff++;
182115662b3eSJoe Perches			$in_commit_log = 0;
18220a920b5bSAndy Whitcroft		}
182320112475SJoe Perches
182420112475SJoe Perches# Check signature styles
1825270c49a0SJoe Perches		if (!$in_header_lines &&
1826ce0338dfSJoe Perches		    $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
182720112475SJoe Perches			my $space_before = $1;
182820112475SJoe Perches			my $sign_off = $2;
182920112475SJoe Perches			my $space_after = $3;
183020112475SJoe Perches			my $email = $4;
183120112475SJoe Perches			my $ucfirst_sign_off = ucfirst(lc($sign_off));
183220112475SJoe Perches
1833ce0338dfSJoe Perches			if ($sign_off !~ /$signature_tags/) {
1834ce0338dfSJoe Perches				WARN("BAD_SIGN_OFF",
1835ce0338dfSJoe Perches				     "Non-standard signature: $sign_off\n" . $herecurr);
1836ce0338dfSJoe Perches			}
183720112475SJoe Perches			if (defined $space_before && $space_before ne "") {
18383705ce5bSJoe Perches				if (WARN("BAD_SIGN_OFF",
18393705ce5bSJoe Perches					 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
18403705ce5bSJoe Perches				    $fix) {
18413705ce5bSJoe Perches					$fixed[$linenr - 1] =
18423705ce5bSJoe Perches					    "$ucfirst_sign_off $email";
18433705ce5bSJoe Perches				}
184420112475SJoe Perches			}
184520112475SJoe Perches			if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
18463705ce5bSJoe Perches				if (WARN("BAD_SIGN_OFF",
18473705ce5bSJoe Perches					 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
18483705ce5bSJoe Perches				    $fix) {
18493705ce5bSJoe Perches					$fixed[$linenr - 1] =
18503705ce5bSJoe Perches					    "$ucfirst_sign_off $email";
18513705ce5bSJoe Perches				}
18523705ce5bSJoe Perches
185320112475SJoe Perches			}
185420112475SJoe Perches			if (!defined $space_after || $space_after ne " ") {
18553705ce5bSJoe Perches				if (WARN("BAD_SIGN_OFF",
18563705ce5bSJoe Perches					 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
18573705ce5bSJoe Perches				    $fix) {
18583705ce5bSJoe Perches					$fixed[$linenr - 1] =
18593705ce5bSJoe Perches					    "$ucfirst_sign_off $email";
18603705ce5bSJoe Perches				}
186120112475SJoe Perches			}
186220112475SJoe Perches
186320112475SJoe Perches			my ($email_name, $email_address, $comment) = parse_email($email);
186420112475SJoe Perches			my $suggested_email = format_email(($email_name, $email_address));
186520112475SJoe Perches			if ($suggested_email eq "") {
1866000d1cc1SJoe Perches				ERROR("BAD_SIGN_OFF",
1867000d1cc1SJoe Perches				      "Unrecognized email address: '$email'\n" . $herecurr);
186820112475SJoe Perches			} else {
186920112475SJoe Perches				my $dequoted = $suggested_email;
187020112475SJoe Perches				$dequoted =~ s/^"//;
187120112475SJoe Perches				$dequoted =~ s/" </ </;
187220112475SJoe Perches				# Don't force email to have quotes
187320112475SJoe Perches				# Allow just an angle bracketed address
187420112475SJoe Perches				if ("$dequoted$comment" ne $email &&
187520112475SJoe Perches				    "<$email_address>$comment" ne $email &&
187620112475SJoe Perches				    "$suggested_email$comment" ne $email) {
1877000d1cc1SJoe Perches					WARN("BAD_SIGN_OFF",
1878000d1cc1SJoe Perches					     "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
187920112475SJoe Perches				}
18800a920b5bSAndy Whitcroft			}
18817e51f197SJoe Perches
18827e51f197SJoe Perches# Check for duplicate signatures
18837e51f197SJoe Perches			my $sig_nospace = $line;
18847e51f197SJoe Perches			$sig_nospace =~ s/\s//g;
18857e51f197SJoe Perches			$sig_nospace = lc($sig_nospace);
18867e51f197SJoe Perches			if (defined $signatures{$sig_nospace}) {
18877e51f197SJoe Perches				WARN("BAD_SIGN_OFF",
18887e51f197SJoe Perches				     "Duplicate signature\n" . $herecurr);
18897e51f197SJoe Perches			} else {
18907e51f197SJoe Perches				$signatures{$sig_nospace} = 1;
18917e51f197SJoe Perches			}
18920a920b5bSAndy Whitcroft		}
18930a920b5bSAndy Whitcroft
189400df344fSAndy Whitcroft# Check for wrappage within a valid hunk of the file
18958905a67cSAndy Whitcroft		if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
1896000d1cc1SJoe Perches			ERROR("CORRUPTED_PATCH",
1897000d1cc1SJoe Perches			      "patch seems to be corrupt (line wrapped?)\n" .
18986c72ffaaSAndy Whitcroft				$herecurr) if (!$emitted_corrupt++);
1899de7d4f0eSAndy Whitcroft		}
1900de7d4f0eSAndy Whitcroft
19016ecd9674SAndy Whitcroft# Check for absolute kernel paths.
19026ecd9674SAndy Whitcroft		if ($tree) {
19036ecd9674SAndy Whitcroft			while ($line =~ m{(?:^|\s)(/\S*)}g) {
19046ecd9674SAndy Whitcroft				my $file = $1;
19056ecd9674SAndy Whitcroft
19066ecd9674SAndy Whitcroft				if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
19076ecd9674SAndy Whitcroft				    check_absolute_file($1, $herecurr)) {
19086ecd9674SAndy Whitcroft					#
19096ecd9674SAndy Whitcroft				} else {
19106ecd9674SAndy Whitcroft					check_absolute_file($file, $herecurr);
19116ecd9674SAndy Whitcroft				}
19126ecd9674SAndy Whitcroft			}
19136ecd9674SAndy Whitcroft		}
19146ecd9674SAndy Whitcroft
1915de7d4f0eSAndy Whitcroft# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
1916de7d4f0eSAndy Whitcroft		if (($realfile =~ /^$/ || $line =~ /^\+/) &&
1917171ae1a4SAndy Whitcroft		    $rawline !~ m/^$UTF8*$/) {
1918171ae1a4SAndy Whitcroft			my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
1919171ae1a4SAndy Whitcroft
1920171ae1a4SAndy Whitcroft			my $blank = copy_spacing($rawline);
1921171ae1a4SAndy Whitcroft			my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
1922171ae1a4SAndy Whitcroft			my $hereptr = "$hereline$ptr\n";
1923171ae1a4SAndy Whitcroft
192434d99219SJoe Perches			CHK("INVALID_UTF8",
1925000d1cc1SJoe Perches			    "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
192600df344fSAndy Whitcroft		}
19270a920b5bSAndy Whitcroft
192815662b3eSJoe Perches# Check if it's the start of a commit log
192915662b3eSJoe Perches# (not a header line and we haven't seen the patch filename)
193015662b3eSJoe Perches		if ($in_header_lines && $realfile =~ /^$/ &&
1931270c49a0SJoe Perches		    $rawline !~ /^(commit\b|from\b|[\w-]+:).+$/i) {
193215662b3eSJoe Perches			$in_header_lines = 0;
193315662b3eSJoe Perches			$in_commit_log = 1;
193415662b3eSJoe Perches		}
193515662b3eSJoe Perches
1936fa64205dSPasi Savanainen# Check if there is UTF-8 in a commit log when a mail header has explicitly
1937fa64205dSPasi Savanainen# declined it, i.e defined some charset where it is missing.
1938fa64205dSPasi Savanainen		if ($in_header_lines &&
1939fa64205dSPasi Savanainen		    $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
1940fa64205dSPasi Savanainen		    $1 !~ /utf-8/i) {
1941fa64205dSPasi Savanainen			$non_utf8_charset = 1;
1942fa64205dSPasi Savanainen		}
1943fa64205dSPasi Savanainen
1944fa64205dSPasi Savanainen		if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
194515662b3eSJoe Perches		    $rawline =~ /$NON_ASCII_UTF8/) {
1946fa64205dSPasi Savanainen			WARN("UTF8_BEFORE_PATCH",
194715662b3eSJoe Perches			    "8-bit UTF-8 used in possible commit log\n" . $herecurr);
194815662b3eSJoe Perches		}
194915662b3eSJoe Perches
195030670854SAndy Whitcroft# ignore non-hunk lines and lines being removed
195130670854SAndy Whitcroft		next if (!$hunk_line || $line =~ /^-/);
195200df344fSAndy Whitcroft
19530a920b5bSAndy Whitcroft#trailing whitespace
19549c0ca6f9SAndy Whitcroft		if ($line =~ /^\+.*\015/) {
1955c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1956d5e616fcSJoe Perches			if (ERROR("DOS_LINE_ENDINGS",
1957d5e616fcSJoe Perches				  "DOS line endings\n" . $herevet) &&
1958d5e616fcSJoe Perches			    $fix) {
1959d5e616fcSJoe Perches				$fixed[$linenr - 1] =~ s/[\s\015]+$//;
1960d5e616fcSJoe Perches			}
1961c2fdda0dSAndy Whitcroft		} elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
1962c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
19633705ce5bSJoe Perches			if (ERROR("TRAILING_WHITESPACE",
19643705ce5bSJoe Perches				  "trailing whitespace\n" . $herevet) &&
19653705ce5bSJoe Perches			    $fix) {
1966d5e616fcSJoe Perches				$fixed[$linenr - 1] =~ s/\s+$//;
19673705ce5bSJoe Perches			}
19683705ce5bSJoe Perches
1969d2c0a235SAndy Whitcroft			$rpt_cleaners = 1;
19700a920b5bSAndy Whitcroft		}
19715368df20SAndy Whitcroft
19724783f894SJosh Triplett# Check for FSF mailing addresses.
1973109d8cb2SAlexander Duyck		if ($rawline =~ /\bwrite to the Free/i ||
19743e2232f2SJoe Perches		    $rawline =~ /\b59\s+Temple\s+Pl/i ||
19753e2232f2SJoe Perches		    $rawline =~ /\b51\s+Franklin\s+St/i) {
19764783f894SJosh Triplett			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
19774783f894SJosh Triplett			my $msg_type = \&ERROR;
19784783f894SJosh Triplett			$msg_type = \&CHK if ($file);
19794783f894SJosh Triplett			&{$msg_type}("FSF_MAILING_ADDRESS",
19804783f894SJosh 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)
19814783f894SJosh Triplett		}
19824783f894SJosh Triplett
19833354957aSAndi Kleen# check for Kconfig help text having a real description
19849fe287d7SAndy Whitcroft# Only applies when adding the entry originally, after that we do not have
19859fe287d7SAndy Whitcroft# sufficient context to determine whether it is indeed long enough.
19863354957aSAndi Kleen		if ($realfile =~ /Kconfig/ &&
1987a1385803SAndy Whitcroft		    $line =~ /.\s*config\s+/) {
19883354957aSAndi Kleen			my $length = 0;
19899fe287d7SAndy Whitcroft			my $cnt = $realcnt;
19909fe287d7SAndy Whitcroft			my $ln = $linenr + 1;
19919fe287d7SAndy Whitcroft			my $f;
1992a1385803SAndy Whitcroft			my $is_start = 0;
19939fe287d7SAndy Whitcroft			my $is_end = 0;
1994a1385803SAndy Whitcroft			for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
19959fe287d7SAndy Whitcroft				$f = $lines[$ln - 1];
19969fe287d7SAndy Whitcroft				$cnt-- if ($lines[$ln - 1] !~ /^-/);
19979fe287d7SAndy Whitcroft				$is_end = $lines[$ln - 1] =~ /^\+/;
19989fe287d7SAndy Whitcroft
19999fe287d7SAndy Whitcroft				next if ($f =~ /^-/);
2000a1385803SAndy Whitcroft
2001a1385803SAndy Whitcroft				if ($lines[$ln - 1] =~ /.\s*(?:bool|tristate)\s*\"/) {
2002a1385803SAndy Whitcroft					$is_start = 1;
2003a1385803SAndy Whitcroft				} elsif ($lines[$ln - 1] =~ /.\s*(?:---)?help(?:---)?$/) {
2004a1385803SAndy Whitcroft					$length = -1;
2005a1385803SAndy Whitcroft				}
2006a1385803SAndy Whitcroft
20079fe287d7SAndy Whitcroft				$f =~ s/^.//;
20083354957aSAndi Kleen				$f =~ s/#.*//;
20093354957aSAndi Kleen				$f =~ s/^\s+//;
20103354957aSAndi Kleen				next if ($f =~ /^$/);
20119fe287d7SAndy Whitcroft				if ($f =~ /^\s*config\s/) {
20129fe287d7SAndy Whitcroft					$is_end = 1;
20139fe287d7SAndy Whitcroft					last;
20149fe287d7SAndy Whitcroft				}
20153354957aSAndi Kleen				$length++;
20163354957aSAndi Kleen			}
2017000d1cc1SJoe Perches			WARN("CONFIG_DESCRIPTION",
2018a1385803SAndy Whitcroft			     "please write a paragraph that describes the config symbol fully\n" . $herecurr) if ($is_start && $is_end && $length < 4);
2019a1385803SAndy Whitcroft			#print "is_start<$is_start> is_end<$is_end> length<$length>\n";
20203354957aSAndi Kleen		}
20213354957aSAndi Kleen
20221ba8dfd1SKees Cook# discourage the addition of CONFIG_EXPERIMENTAL in Kconfig.
20231ba8dfd1SKees Cook		if ($realfile =~ /Kconfig/ &&
20241ba8dfd1SKees Cook		    $line =~ /.\s*depends on\s+.*\bEXPERIMENTAL\b/) {
20251ba8dfd1SKees Cook			WARN("CONFIG_EXPERIMENTAL",
20261ba8dfd1SKees Cook			     "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
20271ba8dfd1SKees Cook		}
20281ba8dfd1SKees Cook
2029c68e5878SArnaud Lacombe		if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
2030c68e5878SArnaud Lacombe		    ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
2031c68e5878SArnaud Lacombe			my $flag = $1;
2032c68e5878SArnaud Lacombe			my $replacement = {
2033c68e5878SArnaud Lacombe				'EXTRA_AFLAGS' =>   'asflags-y',
2034c68e5878SArnaud Lacombe				'EXTRA_CFLAGS' =>   'ccflags-y',
2035c68e5878SArnaud Lacombe				'EXTRA_CPPFLAGS' => 'cppflags-y',
2036c68e5878SArnaud Lacombe				'EXTRA_LDFLAGS' =>  'ldflags-y',
2037c68e5878SArnaud Lacombe			};
2038c68e5878SArnaud Lacombe
2039c68e5878SArnaud Lacombe			WARN("DEPRECATED_VARIABLE",
2040c68e5878SArnaud Lacombe			     "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
2041c68e5878SArnaud Lacombe		}
2042c68e5878SArnaud Lacombe
2043*bff5da43SRob Herring# check for DT compatible documentation
2044*bff5da43SRob Herring		if (defined $root && $realfile =~ /\.dts/ &&
2045*bff5da43SRob Herring		    $rawline =~ /^\+\s*compatible\s*=/) {
2046*bff5da43SRob Herring			my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g;
2047*bff5da43SRob Herring
2048*bff5da43SRob Herring			foreach my $compat (@compats) {
2049*bff5da43SRob Herring				my $compat2 = $compat;
2050*bff5da43SRob Herring				my $dt_path =  $root . "/Documentation/devicetree/bindings/";
2051*bff5da43SRob Herring				$compat2 =~ s/\,[a-z]*\-/\,<\.\*>\-/;
2052*bff5da43SRob Herring				`grep -Erq "$compat|$compat2" $dt_path`;
2053*bff5da43SRob Herring				if ( $? >> 8 ) {
2054*bff5da43SRob Herring					WARN("UNDOCUMENTED_DT_STRING",
2055*bff5da43SRob Herring					     "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr);
2056*bff5da43SRob Herring				}
2057*bff5da43SRob Herring
2058*bff5da43SRob Herring				my $vendor = $compat;
2059*bff5da43SRob Herring				my $vendor_path = $dt_path . "vendor-prefixes.txt";
2060*bff5da43SRob Herring				next if (! -f $vendor_path);
2061*bff5da43SRob Herring				$vendor =~ s/^([a-zA-Z0-9]+)\,.*/$1/;
2062*bff5da43SRob Herring				`grep -Eq "$vendor" $vendor_path`;
2063*bff5da43SRob Herring				if ( $? >> 8 ) {
2064*bff5da43SRob Herring					WARN("UNDOCUMENTED_DT_STRING",
2065*bff5da43SRob Herring					     "DT compatible string vendor \"$vendor\" appears un-documented -- check $vendor_path\n" . $herecurr);
2066*bff5da43SRob Herring				}
2067*bff5da43SRob Herring			}
2068*bff5da43SRob Herring		}
2069*bff5da43SRob Herring
20705368df20SAndy Whitcroft# check we are in a valid source file if not then ignore this hunk
20715368df20SAndy Whitcroft		next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
20725368df20SAndy Whitcroft
20736cd7f386SJoe Perches#line length limit
2074c45dcabdSAndy Whitcroft		if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ &&
2075f4c014c0SAndy Whitcroft		    $rawline !~ /^.\s*\*\s*\@$Ident\s/ &&
20760fccc622SJoe Perches		    !($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(KERN_\S+\s*|[^"]*))?"[X\t]*"\s*(?:|,|\)\s*;)\s*$/ ||
20778bbea968SJoe Perches		    $line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) &&
20786cd7f386SJoe Perches		    $length > $max_line_length)
2079c45dcabdSAndy Whitcroft		{
2080000d1cc1SJoe Perches			WARN("LONG_LINE",
20816cd7f386SJoe Perches			     "line over $max_line_length characters\n" . $herecurr);
20820a920b5bSAndy Whitcroft		}
20830a920b5bSAndy Whitcroft
2084ca56dc09SJosh Triplett# Check for user-visible strings broken across lines, which breaks the ability
20858c5fcd24SJoe Perches# to grep for the string.  Make exceptions when the previous string ends in a
20868c5fcd24SJoe Perches# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{'
20878c5fcd24SJoe Perches# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value
2088ca56dc09SJosh Triplett		if ($line =~ /^\+\s*"/ &&
2089ca56dc09SJosh Triplett		    $prevline =~ /"\s*$/ &&
20908c5fcd24SJoe Perches		    $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) {
2091ca56dc09SJosh Triplett			WARN("SPLIT_STRING",
2092ca56dc09SJosh Triplett			     "quoted string split across lines\n" . $hereprev);
2093ca56dc09SJosh Triplett		}
2094ca56dc09SJosh Triplett
20955e79d96eSJoe Perches# check for spaces before a quoted newline
20965e79d96eSJoe Perches		if ($rawline =~ /^.*\".*\s\\n/) {
20973705ce5bSJoe Perches			if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
20983705ce5bSJoe Perches				 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
20993705ce5bSJoe Perches			    $fix) {
21003705ce5bSJoe Perches				$fixed[$linenr - 1] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
21013705ce5bSJoe Perches			}
21023705ce5bSJoe Perches
21035e79d96eSJoe Perches		}
21045e79d96eSJoe Perches
21058905a67cSAndy Whitcroft# check for adding lines without a newline.
21068905a67cSAndy Whitcroft		if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
2107000d1cc1SJoe Perches			WARN("MISSING_EOF_NEWLINE",
2108000d1cc1SJoe Perches			     "adding a line without newline at end of file\n" . $herecurr);
21098905a67cSAndy Whitcroft		}
21108905a67cSAndy Whitcroft
211142e41c54SMike Frysinger# Blackfin: use hi/lo macros
211242e41c54SMike Frysinger		if ($realfile =~ m@arch/blackfin/.*\.S$@) {
211342e41c54SMike Frysinger			if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
211442e41c54SMike Frysinger				my $herevet = "$here\n" . cat_vet($line) . "\n";
2115000d1cc1SJoe Perches				ERROR("LO_MACRO",
2116000d1cc1SJoe Perches				      "use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
211742e41c54SMike Frysinger			}
211842e41c54SMike Frysinger			if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
211942e41c54SMike Frysinger				my $herevet = "$here\n" . cat_vet($line) . "\n";
2120000d1cc1SJoe Perches				ERROR("HI_MACRO",
2121000d1cc1SJoe Perches				      "use the HI() macro, not (... >> 16)\n" . $herevet);
212242e41c54SMike Frysinger			}
212342e41c54SMike Frysinger		}
212442e41c54SMike Frysinger
2125b9ea10d6SAndy Whitcroft# check we are in a valid source file C or perl if not then ignore this hunk
2126b9ea10d6SAndy Whitcroft		next if ($realfile !~ /\.(h|c|pl)$/);
21270a920b5bSAndy Whitcroft
21280a920b5bSAndy Whitcroft# at the beginning of a line any tabs must come first and anything
21290a920b5bSAndy Whitcroft# more than 8 must use tabs.
2130c2fdda0dSAndy Whitcroft		if ($rawline =~ /^\+\s* \t\s*\S/ ||
2131c2fdda0dSAndy Whitcroft		    $rawline =~ /^\+\s*        \s*/) {
2132c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2133d2c0a235SAndy Whitcroft			$rpt_cleaners = 1;
21343705ce5bSJoe Perches			if (ERROR("CODE_INDENT",
21353705ce5bSJoe Perches				  "code indent should use tabs where possible\n" . $herevet) &&
21363705ce5bSJoe Perches			    $fix) {
21373705ce5bSJoe Perches				$fixed[$linenr - 1] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
21383705ce5bSJoe Perches			}
21390a920b5bSAndy Whitcroft		}
21400a920b5bSAndy Whitcroft
214108e44365SAlberto Panizzo# check for space before tabs.
214208e44365SAlberto Panizzo		if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
214308e44365SAlberto Panizzo			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
21443705ce5bSJoe Perches			if (WARN("SPACE_BEFORE_TAB",
21453705ce5bSJoe Perches				"please, no space before tabs\n" . $herevet) &&
21463705ce5bSJoe Perches			    $fix) {
2147c76f4cb3SJoe Perches				while ($fixed[$linenr - 1] =~
2148c76f4cb3SJoe Perches					   s/(^\+.*) {8,8}+\t/$1\t\t/) {}
2149c76f4cb3SJoe Perches				while ($fixed[$linenr - 1] =~
2150c76f4cb3SJoe Perches					   s/(^\+.*) +\t/$1\t/) {}
21513705ce5bSJoe Perches			}
215208e44365SAlberto Panizzo		}
215308e44365SAlberto Panizzo
2154d1fe9c09SJoe Perches# check for && or || at the start of a line
2155d1fe9c09SJoe Perches		if ($rawline =~ /^\+\s*(&&|\|\|)/) {
2156d1fe9c09SJoe Perches			CHK("LOGICAL_CONTINUATIONS",
2157d1fe9c09SJoe Perches			    "Logical continuations should be on the previous line\n" . $hereprev);
2158d1fe9c09SJoe Perches		}
2159d1fe9c09SJoe Perches
2160d1fe9c09SJoe Perches# check multi-line statement indentation matches previous line
2161d1fe9c09SJoe Perches		if ($^V && $^V ge 5.10.0 &&
2162d1fe9c09SJoe Perches		    $prevline =~ /^\+(\t*)(if \(|$Ident\().*(\&\&|\|\||,)\s*$/) {
2163d1fe9c09SJoe Perches			$prevline =~ /^\+(\t*)(.*)$/;
2164d1fe9c09SJoe Perches			my $oldindent = $1;
2165d1fe9c09SJoe Perches			my $rest = $2;
2166d1fe9c09SJoe Perches
2167d1fe9c09SJoe Perches			my $pos = pos_last_openparen($rest);
2168d1fe9c09SJoe Perches			if ($pos >= 0) {
2169b34a26f3SJoe Perches				$line =~ /^(\+| )([ \t]*)/;
2170b34a26f3SJoe Perches				my $newindent = $2;
2171d1fe9c09SJoe Perches
2172d1fe9c09SJoe Perches				my $goodtabindent = $oldindent .
2173d1fe9c09SJoe Perches					"\t" x ($pos / 8) .
2174d1fe9c09SJoe Perches					" "  x ($pos % 8);
2175d1fe9c09SJoe Perches				my $goodspaceindent = $oldindent . " "  x $pos;
2176d1fe9c09SJoe Perches
2177d1fe9c09SJoe Perches				if ($newindent ne $goodtabindent &&
2178d1fe9c09SJoe Perches				    $newindent ne $goodspaceindent) {
21793705ce5bSJoe Perches
21803705ce5bSJoe Perches					if (CHK("PARENTHESIS_ALIGNMENT",
21813705ce5bSJoe Perches						"Alignment should match open parenthesis\n" . $hereprev) &&
21823705ce5bSJoe Perches					    $fix && $line =~ /^\+/) {
21833705ce5bSJoe Perches						$fixed[$linenr - 1] =~
21843705ce5bSJoe Perches						    s/^\+[ \t]*/\+$goodtabindent/;
21853705ce5bSJoe Perches					}
2186d1fe9c09SJoe Perches				}
2187d1fe9c09SJoe Perches			}
2188d1fe9c09SJoe Perches		}
2189d1fe9c09SJoe Perches
219023f780c9SJoe Perches		if ($line =~ /^\+.*\*[ \t]*\)[ \t]+(?!$Assignment|$Arithmetic)/) {
21913705ce5bSJoe Perches			if (CHK("SPACING",
21923705ce5bSJoe Perches				"No space is necessary after a cast\n" . $hereprev) &&
21933705ce5bSJoe Perches			    $fix) {
21943705ce5bSJoe Perches				$fixed[$linenr - 1] =~
21953705ce5bSJoe Perches				    s/^(\+.*\*[ \t]*\))[ \t]+/$1/;
21963705ce5bSJoe Perches			}
2197aad4f614SJoe Perches		}
2198aad4f614SJoe Perches
219905880600SJoe Perches		if ($realfile =~ m@^(drivers/net/|net/)@ &&
2200fdb4bcd6SJoe Perches		    $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
2201fdb4bcd6SJoe Perches		    $rawline =~ /^\+[ \t]*\*/) {
220205880600SJoe Perches			WARN("NETWORKING_BLOCK_COMMENT_STYLE",
220305880600SJoe Perches			     "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
220405880600SJoe Perches		}
220505880600SJoe Perches
220605880600SJoe Perches		if ($realfile =~ m@^(drivers/net/|net/)@ &&
2207a605e32eSJoe Perches		    $prevrawline =~ /^\+[ \t]*\/\*/ &&		#starting /*
2208a605e32eSJoe Perches		    $prevrawline !~ /\*\/[ \t]*$/ &&		#no trailing */
220961135e96SJoe Perches		    $rawline =~ /^\+/ &&			#line is new
2210a605e32eSJoe Perches		    $rawline !~ /^\+[ \t]*\*/) {		#no leading *
2211a605e32eSJoe Perches			WARN("NETWORKING_BLOCK_COMMENT_STYLE",
2212a605e32eSJoe Perches			     "networking block comments start with * on subsequent lines\n" . $hereprev);
2213a605e32eSJoe Perches		}
2214a605e32eSJoe Perches
2215a605e32eSJoe Perches		if ($realfile =~ m@^(drivers/net/|net/)@ &&
2216c24f9f19SJoe Perches		    $rawline !~ m@^\+[ \t]*\*/[ \t]*$@ &&	#trailing */
2217c24f9f19SJoe Perches		    $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ &&	#inline /*...*/
2218c24f9f19SJoe Perches		    $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ &&	#trailing **/
2219c24f9f19SJoe Perches		    $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) {	#non blank */
222005880600SJoe Perches			WARN("NETWORKING_BLOCK_COMMENT_STYLE",
222105880600SJoe Perches			     "networking block comments put the trailing */ on a separate line\n" . $herecurr);
222205880600SJoe Perches		}
222305880600SJoe Perches
22245f7ddae6SRaffaele Recalcati# check for spaces at the beginning of a line.
22256b4c5bebSAndy Whitcroft# Exceptions:
22266b4c5bebSAndy Whitcroft#  1) within comments
22276b4c5bebSAndy Whitcroft#  2) indented preprocessor commands
22286b4c5bebSAndy Whitcroft#  3) hanging labels
22293705ce5bSJoe Perches		if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/)  {
22305f7ddae6SRaffaele Recalcati			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
22313705ce5bSJoe Perches			if (WARN("LEADING_SPACE",
22323705ce5bSJoe Perches				 "please, no spaces at the start of a line\n" . $herevet) &&
22333705ce5bSJoe Perches			    $fix) {
22343705ce5bSJoe Perches				$fixed[$linenr - 1] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
22353705ce5bSJoe Perches			}
22365f7ddae6SRaffaele Recalcati		}
22375f7ddae6SRaffaele Recalcati
2238b9ea10d6SAndy Whitcroft# check we are in a valid C source file if not then ignore this hunk
2239b9ea10d6SAndy Whitcroft		next if ($realfile !~ /\.(h|c)$/);
2240b9ea10d6SAndy Whitcroft
22411ba8dfd1SKees Cook# discourage the addition of CONFIG_EXPERIMENTAL in #if(def).
22421ba8dfd1SKees Cook		if ($line =~ /^\+\s*\#\s*if.*\bCONFIG_EXPERIMENTAL\b/) {
22431ba8dfd1SKees Cook			WARN("CONFIG_EXPERIMENTAL",
22441ba8dfd1SKees Cook			     "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
22451ba8dfd1SKees Cook		}
22461ba8dfd1SKees Cook
2247c2fdda0dSAndy Whitcroft# check for RCS/CVS revision markers
2248cf655043SAndy Whitcroft		if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
2249000d1cc1SJoe Perches			WARN("CVS_KEYWORD",
2250000d1cc1SJoe Perches			     "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
2251c2fdda0dSAndy Whitcroft		}
225222f2a2efSAndy Whitcroft
225342e41c54SMike Frysinger# Blackfin: don't use __builtin_bfin_[cs]sync
225442e41c54SMike Frysinger		if ($line =~ /__builtin_bfin_csync/) {
225542e41c54SMike Frysinger			my $herevet = "$here\n" . cat_vet($line) . "\n";
2256000d1cc1SJoe Perches			ERROR("CSYNC",
2257000d1cc1SJoe Perches			      "use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
225842e41c54SMike Frysinger		}
225942e41c54SMike Frysinger		if ($line =~ /__builtin_bfin_ssync/) {
226042e41c54SMike Frysinger			my $herevet = "$here\n" . cat_vet($line) . "\n";
2261000d1cc1SJoe Perches			ERROR("SSYNC",
2262000d1cc1SJoe Perches			      "use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
226342e41c54SMike Frysinger		}
226442e41c54SMike Frysinger
226556e77d70SJoe Perches# check for old HOTPLUG __dev<foo> section markings
226656e77d70SJoe Perches		if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
226756e77d70SJoe Perches			WARN("HOTPLUG_SECTION",
226856e77d70SJoe Perches			     "Using $1 is unnecessary\n" . $herecurr);
226956e77d70SJoe Perches		}
227056e77d70SJoe Perches
22719c0ca6f9SAndy Whitcroft# Check for potential 'bare' types
22722b474a1aSAndy Whitcroft		my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
22732b474a1aSAndy Whitcroft		    $realline_next);
22743e469cdcSAndy Whitcroft#print "LINE<$line>\n";
22753e469cdcSAndy Whitcroft		if ($linenr >= $suppress_statement &&
22761b5539b1SJoe Perches		    $realcnt && $sline =~ /.\s*\S/) {
2277170d3a22SAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
2278f5fe35ddSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0);
2279171ae1a4SAndy Whitcroft			$stat =~ s/\n./\n /g;
2280171ae1a4SAndy Whitcroft			$cond =~ s/\n./\n /g;
2281171ae1a4SAndy Whitcroft
22823e469cdcSAndy Whitcroft#print "linenr<$linenr> <$stat>\n";
22833e469cdcSAndy Whitcroft			# If this statement has no statement boundaries within
22843e469cdcSAndy Whitcroft			# it there is no point in retrying a statement scan
22853e469cdcSAndy Whitcroft			# until we hit end of it.
22863e469cdcSAndy Whitcroft			my $frag = $stat; $frag =~ s/;+\s*$//;
22873e469cdcSAndy Whitcroft			if ($frag !~ /(?:{|;)/) {
22883e469cdcSAndy Whitcroft#print "skip<$line_nr_next>\n";
22893e469cdcSAndy Whitcroft				$suppress_statement = $line_nr_next;
22903e469cdcSAndy Whitcroft			}
2291f74bd194SAndy Whitcroft
22922b474a1aSAndy Whitcroft			# Find the real next line.
22932b474a1aSAndy Whitcroft			$realline_next = $line_nr_next;
22942b474a1aSAndy Whitcroft			if (defined $realline_next &&
22952b474a1aSAndy Whitcroft			    (!defined $lines[$realline_next - 1] ||
22962b474a1aSAndy Whitcroft			     substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
22972b474a1aSAndy Whitcroft				$realline_next++;
22982b474a1aSAndy Whitcroft			}
22992b474a1aSAndy Whitcroft
2300171ae1a4SAndy Whitcroft			my $s = $stat;
2301171ae1a4SAndy Whitcroft			$s =~ s/{.*$//s;
2302cf655043SAndy Whitcroft
2303c2fdda0dSAndy Whitcroft			# Ignore goto labels.
2304171ae1a4SAndy Whitcroft			if ($s =~ /$Ident:\*$/s) {
2305c2fdda0dSAndy Whitcroft
2306c2fdda0dSAndy Whitcroft			# Ignore functions being called
2307171ae1a4SAndy Whitcroft			} elsif ($s =~ /^.\s*$Ident\s*\(/s) {
2308c2fdda0dSAndy Whitcroft
2309463f2864SAndy Whitcroft			} elsif ($s =~ /^.\s*else\b/s) {
2310463f2864SAndy Whitcroft
2311c45dcabdSAndy Whitcroft			# declarations always start with types
2312d2506586SAndy 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) {
2313c45dcabdSAndy Whitcroft				my $type = $1;
2314c45dcabdSAndy Whitcroft				$type =~ s/\s+/ /g;
2315c45dcabdSAndy Whitcroft				possible($type, "A:" . $s);
2316c45dcabdSAndy Whitcroft
23176c72ffaaSAndy Whitcroft			# definitions in global scope can only start with types
2318a6a84062SAndy Whitcroft			} elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
2319c45dcabdSAndy Whitcroft				possible($1, "B:" . $s);
2320c2fdda0dSAndy Whitcroft			}
23218905a67cSAndy Whitcroft
23226c72ffaaSAndy Whitcroft			# any (foo ... *) is a pointer cast, and foo is a type
232365863862SAndy Whitcroft			while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
2324c45dcabdSAndy Whitcroft				possible($1, "C:" . $s);
23259c0ca6f9SAndy Whitcroft			}
23268905a67cSAndy Whitcroft
23278905a67cSAndy Whitcroft			# Check for any sort of function declaration.
23288905a67cSAndy Whitcroft			# int foo(something bar, other baz);
23298905a67cSAndy Whitcroft			# void (*store_gdt)(x86_descr_ptr *);
2330171ae1a4SAndy Whitcroft			if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
23318905a67cSAndy Whitcroft				my ($name_len) = length($1);
23328905a67cSAndy Whitcroft
2333cf655043SAndy Whitcroft				my $ctx = $s;
2334773647a0SAndy Whitcroft				substr($ctx, 0, $name_len + 1, '');
23358905a67cSAndy Whitcroft				$ctx =~ s/\)[^\)]*$//;
2336cf655043SAndy Whitcroft
23378905a67cSAndy Whitcroft				for my $arg (split(/\s*,\s*/, $ctx)) {
2338c45dcabdSAndy Whitcroft					if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
23398905a67cSAndy Whitcroft
2340c45dcabdSAndy Whitcroft						possible($1, "D:" . $s);
23418905a67cSAndy Whitcroft					}
23428905a67cSAndy Whitcroft				}
23438905a67cSAndy Whitcroft			}
23448905a67cSAndy Whitcroft
23459c0ca6f9SAndy Whitcroft		}
23469c0ca6f9SAndy Whitcroft
234700df344fSAndy Whitcroft#
234800df344fSAndy Whitcroft# Checks which may be anchored in the context.
234900df344fSAndy Whitcroft#
235000df344fSAndy Whitcroft
235100df344fSAndy Whitcroft# Check for switch () and associated case and default
235200df344fSAndy Whitcroft# statements should be at the same indent.
235300df344fSAndy Whitcroft		if ($line=~/\bswitch\s*\(.*\)/) {
235400df344fSAndy Whitcroft			my $err = '';
235500df344fSAndy Whitcroft			my $sep = '';
235600df344fSAndy Whitcroft			my @ctx = ctx_block_outer($linenr, $realcnt);
235700df344fSAndy Whitcroft			shift(@ctx);
235800df344fSAndy Whitcroft			for my $ctx (@ctx) {
235900df344fSAndy Whitcroft				my ($clen, $cindent) = line_stats($ctx);
236000df344fSAndy Whitcroft				if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
236100df344fSAndy Whitcroft							$indent != $cindent) {
236200df344fSAndy Whitcroft					$err .= "$sep$ctx\n";
236300df344fSAndy Whitcroft					$sep = '';
236400df344fSAndy Whitcroft				} else {
236500df344fSAndy Whitcroft					$sep = "[...]\n";
236600df344fSAndy Whitcroft				}
236700df344fSAndy Whitcroft			}
236800df344fSAndy Whitcroft			if ($err ne '') {
2369000d1cc1SJoe Perches				ERROR("SWITCH_CASE_INDENT_LEVEL",
2370000d1cc1SJoe Perches				      "switch and case should be at the same indent\n$hereline$err");
2371de7d4f0eSAndy Whitcroft			}
2372de7d4f0eSAndy Whitcroft		}
2373de7d4f0eSAndy Whitcroft
2374de7d4f0eSAndy Whitcroft# if/while/etc brace do not go on next line, unless defining a do while loop,
2375de7d4f0eSAndy Whitcroft# or if that brace on the next line is for something else
2376c45dcabdSAndy Whitcroft		if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
2377773647a0SAndy Whitcroft			my $pre_ctx = "$1$2";
2378773647a0SAndy Whitcroft
23799c0ca6f9SAndy Whitcroft			my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
23808eef05ddSJoe Perches
23818eef05ddSJoe Perches			if ($line =~ /^\+\t{6,}/) {
23828eef05ddSJoe Perches				WARN("DEEP_INDENTATION",
23838eef05ddSJoe Perches				     "Too many leading tabs - consider code refactoring\n" . $herecurr);
23848eef05ddSJoe Perches			}
23858eef05ddSJoe Perches
2386de7d4f0eSAndy Whitcroft			my $ctx_cnt = $realcnt - $#ctx - 1;
2387de7d4f0eSAndy Whitcroft			my $ctx = join("\n", @ctx);
2388de7d4f0eSAndy Whitcroft
2389548596d5SAndy Whitcroft			my $ctx_ln = $linenr;
2390548596d5SAndy Whitcroft			my $ctx_skip = $realcnt;
2391de7d4f0eSAndy Whitcroft
2392548596d5SAndy Whitcroft			while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
2393548596d5SAndy Whitcroft					defined $lines[$ctx_ln - 1] &&
2394548596d5SAndy Whitcroft					$lines[$ctx_ln - 1] =~ /^-/)) {
2395548596d5SAndy Whitcroft				##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
2396548596d5SAndy Whitcroft				$ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
2397773647a0SAndy Whitcroft				$ctx_ln++;
2398773647a0SAndy Whitcroft			}
2399548596d5SAndy Whitcroft
240053210168SAndy Whitcroft			#print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
240153210168SAndy Whitcroft			#print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
2402773647a0SAndy Whitcroft
2403773647a0SAndy Whitcroft			if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln -1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
2404000d1cc1SJoe Perches				ERROR("OPEN_BRACE",
2405000d1cc1SJoe Perches				      "that open brace { should be on the previous line\n" .
240601464f30SAndy Whitcroft					"$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
240700df344fSAndy Whitcroft			}
2408773647a0SAndy Whitcroft			if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
2409773647a0SAndy Whitcroft			    $ctx =~ /\)\s*\;\s*$/ &&
2410773647a0SAndy Whitcroft			    defined $lines[$ctx_ln - 1])
2411773647a0SAndy Whitcroft			{
24129c0ca6f9SAndy Whitcroft				my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
24139c0ca6f9SAndy Whitcroft				if ($nindent > $indent) {
2414000d1cc1SJoe Perches					WARN("TRAILING_SEMICOLON",
2415000d1cc1SJoe Perches					     "trailing semicolon indicates no statements, indent implies otherwise\n" .
241601464f30SAndy Whitcroft						"$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
24179c0ca6f9SAndy Whitcroft				}
24189c0ca6f9SAndy Whitcroft			}
241900df344fSAndy Whitcroft		}
242000df344fSAndy Whitcroft
24214d001e4dSAndy Whitcroft# Check relative indent for conditionals and blocks.
24224d001e4dSAndy Whitcroft		if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
24233e469cdcSAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
24243e469cdcSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0)
24253e469cdcSAndy Whitcroft					if (!defined $stat);
24264d001e4dSAndy Whitcroft			my ($s, $c) = ($stat, $cond);
24274d001e4dSAndy Whitcroft
24284d001e4dSAndy Whitcroft			substr($s, 0, length($c), '');
24294d001e4dSAndy Whitcroft
24304d001e4dSAndy Whitcroft			# Make sure we remove the line prefixes as we have
24314d001e4dSAndy Whitcroft			# none on the first line, and are going to readd them
24324d001e4dSAndy Whitcroft			# where necessary.
24334d001e4dSAndy Whitcroft			$s =~ s/\n./\n/gs;
24344d001e4dSAndy Whitcroft
24354d001e4dSAndy Whitcroft			# Find out how long the conditional actually is.
24366f779c18SAndy Whitcroft			my @newlines = ($c =~ /\n/gs);
24376f779c18SAndy Whitcroft			my $cond_lines = 1 + $#newlines;
24384d001e4dSAndy Whitcroft
24394d001e4dSAndy Whitcroft			# We want to check the first line inside the block
24404d001e4dSAndy Whitcroft			# starting at the end of the conditional, so remove:
24414d001e4dSAndy Whitcroft			#  1) any blank line termination
24424d001e4dSAndy Whitcroft			#  2) any opening brace { on end of the line
24434d001e4dSAndy Whitcroft			#  3) any do (...) {
24444d001e4dSAndy Whitcroft			my $continuation = 0;
24454d001e4dSAndy Whitcroft			my $check = 0;
24464d001e4dSAndy Whitcroft			$s =~ s/^.*\bdo\b//;
24474d001e4dSAndy Whitcroft			$s =~ s/^\s*{//;
24484d001e4dSAndy Whitcroft			if ($s =~ s/^\s*\\//) {
24494d001e4dSAndy Whitcroft				$continuation = 1;
24504d001e4dSAndy Whitcroft			}
24519bd49efeSAndy Whitcroft			if ($s =~ s/^\s*?\n//) {
24524d001e4dSAndy Whitcroft				$check = 1;
24534d001e4dSAndy Whitcroft				$cond_lines++;
24544d001e4dSAndy Whitcroft			}
24554d001e4dSAndy Whitcroft
24564d001e4dSAndy Whitcroft			# Also ignore a loop construct at the end of a
24574d001e4dSAndy Whitcroft			# preprocessor statement.
24584d001e4dSAndy Whitcroft			if (($prevline =~ /^.\s*#\s*define\s/ ||
24594d001e4dSAndy Whitcroft			    $prevline =~ /\\\s*$/) && $continuation == 0) {
24604d001e4dSAndy Whitcroft				$check = 0;
24614d001e4dSAndy Whitcroft			}
24624d001e4dSAndy Whitcroft
24639bd49efeSAndy Whitcroft			my $cond_ptr = -1;
2464740504c6SAndy Whitcroft			$continuation = 0;
24659bd49efeSAndy Whitcroft			while ($cond_ptr != $cond_lines) {
24669bd49efeSAndy Whitcroft				$cond_ptr = $cond_lines;
24674d001e4dSAndy Whitcroft
2468f16fa28fSAndy Whitcroft				# If we see an #else/#elif then the code
2469f16fa28fSAndy Whitcroft				# is not linear.
2470f16fa28fSAndy Whitcroft				if ($s =~ /^\s*\#\s*(?:else|elif)/) {
2471f16fa28fSAndy Whitcroft					$check = 0;
2472f16fa28fSAndy Whitcroft				}
2473f16fa28fSAndy Whitcroft
24749bd49efeSAndy Whitcroft				# Ignore:
24759bd49efeSAndy Whitcroft				#  1) blank lines, they should be at 0,
24769bd49efeSAndy Whitcroft				#  2) preprocessor lines, and
24779bd49efeSAndy Whitcroft				#  3) labels.
2478740504c6SAndy Whitcroft				if ($continuation ||
2479740504c6SAndy Whitcroft				    $s =~ /^\s*?\n/ ||
24809bd49efeSAndy Whitcroft				    $s =~ /^\s*#\s*?/ ||
24819bd49efeSAndy Whitcroft				    $s =~ /^\s*$Ident\s*:/) {
2482740504c6SAndy Whitcroft					$continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
248330dad6ebSAndy Whitcroft					if ($s =~ s/^.*?\n//) {
24849bd49efeSAndy Whitcroft						$cond_lines++;
24859bd49efeSAndy Whitcroft					}
24864d001e4dSAndy Whitcroft				}
248730dad6ebSAndy Whitcroft			}
24884d001e4dSAndy Whitcroft
24894d001e4dSAndy Whitcroft			my (undef, $sindent) = line_stats("+" . $s);
24904d001e4dSAndy Whitcroft			my $stat_real = raw_line($linenr, $cond_lines);
24914d001e4dSAndy Whitcroft
24924d001e4dSAndy Whitcroft			# Check if either of these lines are modified, else
24934d001e4dSAndy Whitcroft			# this is not this patch's fault.
24944d001e4dSAndy Whitcroft			if (!defined($stat_real) ||
24954d001e4dSAndy Whitcroft			    $stat !~ /^\+/ && $stat_real !~ /^\+/) {
24964d001e4dSAndy Whitcroft				$check = 0;
24974d001e4dSAndy Whitcroft			}
24984d001e4dSAndy Whitcroft			if (defined($stat_real) && $cond_lines > 1) {
24994d001e4dSAndy Whitcroft				$stat_real = "[...]\n$stat_real";
25004d001e4dSAndy Whitcroft			}
25014d001e4dSAndy Whitcroft
25029bd49efeSAndy 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";
25034d001e4dSAndy Whitcroft
25044d001e4dSAndy Whitcroft			if ($check && (($sindent % 8) != 0 ||
25054d001e4dSAndy Whitcroft			    ($sindent <= $indent && $s ne ''))) {
2506000d1cc1SJoe Perches				WARN("SUSPECT_CODE_INDENT",
2507000d1cc1SJoe Perches				     "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
25084d001e4dSAndy Whitcroft			}
25094d001e4dSAndy Whitcroft		}
25104d001e4dSAndy Whitcroft
25116c72ffaaSAndy Whitcroft		# Track the 'values' across context and added lines.
25126c72ffaaSAndy Whitcroft		my $opline = $line; $opline =~ s/^./ /;
25131f65f947SAndy Whitcroft		my ($curr_values, $curr_vars) =
25141f65f947SAndy Whitcroft				annotate_values($opline . "\n", $prev_values);
25156c72ffaaSAndy Whitcroft		$curr_values = $prev_values . $curr_values;
2516c2fdda0dSAndy Whitcroft		if ($dbg_values) {
2517c2fdda0dSAndy Whitcroft			my $outline = $opline; $outline =~ s/\t/ /g;
2518cf655043SAndy Whitcroft			print "$linenr > .$outline\n";
2519cf655043SAndy Whitcroft			print "$linenr > $curr_values\n";
25201f65f947SAndy Whitcroft			print "$linenr >  $curr_vars\n";
2521c2fdda0dSAndy Whitcroft		}
25226c72ffaaSAndy Whitcroft		$prev_values = substr($curr_values, -1);
25236c72ffaaSAndy Whitcroft
252400df344fSAndy Whitcroft#ignore lines not being added
25253705ce5bSJoe Perches		next if ($line =~ /^[^\+]/);
252600df344fSAndy Whitcroft
2527653d4876SAndy Whitcroft# TEST: allow direct testing of the type matcher.
25287429c690SAndy Whitcroft		if ($dbg_type) {
25297429c690SAndy Whitcroft			if ($line =~ /^.\s*$Declare\s*$/) {
2530000d1cc1SJoe Perches				ERROR("TEST_TYPE",
2531000d1cc1SJoe Perches				      "TEST: is type\n" . $herecurr);
25327429c690SAndy Whitcroft			} elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
2533000d1cc1SJoe Perches				ERROR("TEST_NOT_TYPE",
2534000d1cc1SJoe Perches				      "TEST: is not type ($1 is)\n". $herecurr);
25357429c690SAndy Whitcroft			}
2536653d4876SAndy Whitcroft			next;
2537653d4876SAndy Whitcroft		}
2538a1ef277eSAndy Whitcroft# TEST: allow direct testing of the attribute matcher.
2539a1ef277eSAndy Whitcroft		if ($dbg_attr) {
25409360b0e5SAndy Whitcroft			if ($line =~ /^.\s*$Modifier\s*$/) {
2541000d1cc1SJoe Perches				ERROR("TEST_ATTR",
2542000d1cc1SJoe Perches				      "TEST: is attr\n" . $herecurr);
25439360b0e5SAndy Whitcroft			} elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
2544000d1cc1SJoe Perches				ERROR("TEST_NOT_ATTR",
2545000d1cc1SJoe Perches				      "TEST: is not attr ($1 is)\n". $herecurr);
2546a1ef277eSAndy Whitcroft			}
2547a1ef277eSAndy Whitcroft			next;
2548a1ef277eSAndy Whitcroft		}
2549653d4876SAndy Whitcroft
2550f0a594c1SAndy Whitcroft# check for initialisation to aggregates open brace on the next line
255199423c20SAndy Whitcroft		if ($line =~ /^.\s*{/ &&
255299423c20SAndy Whitcroft		    $prevline =~ /(?:^|[^=])=\s*$/) {
2553000d1cc1SJoe Perches			ERROR("OPEN_BRACE",
2554000d1cc1SJoe Perches			      "that open brace { should be on the previous line\n" . $hereprev);
2555f0a594c1SAndy Whitcroft		}
2556f0a594c1SAndy Whitcroft
255700df344fSAndy Whitcroft#
255800df344fSAndy Whitcroft# Checks which are anchored on the added line.
255900df344fSAndy Whitcroft#
256000df344fSAndy Whitcroft
2561653d4876SAndy Whitcroft# check for malformed paths in #include statements (uses RAW line)
2562c45dcabdSAndy Whitcroft		if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
2563653d4876SAndy Whitcroft			my $path = $1;
2564653d4876SAndy Whitcroft			if ($path =~ m{//}) {
2565000d1cc1SJoe Perches				ERROR("MALFORMED_INCLUDE",
2566495e9d84SJoe Perches				      "malformed #include filename\n" . $herecurr);
2567495e9d84SJoe Perches			}
2568495e9d84SJoe Perches			if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
2569495e9d84SJoe Perches				ERROR("UAPI_INCLUDE",
2570495e9d84SJoe Perches				      "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
2571653d4876SAndy Whitcroft			}
2572653d4876SAndy Whitcroft		}
2573653d4876SAndy Whitcroft
257400df344fSAndy Whitcroft# no C99 // comments
257500df344fSAndy Whitcroft		if ($line =~ m{//}) {
25763705ce5bSJoe Perches			if (ERROR("C99_COMMENTS",
25773705ce5bSJoe Perches				  "do not use C99 // comments\n" . $herecurr) &&
25783705ce5bSJoe Perches			    $fix) {
25793705ce5bSJoe Perches				my $line = $fixed[$linenr - 1];
25803705ce5bSJoe Perches				if ($line =~ /\/\/(.*)$/) {
25813705ce5bSJoe Perches					my $comment = trim($1);
25823705ce5bSJoe Perches					$fixed[$linenr - 1] =~ s@\/\/(.*)$@/\* $comment \*/@;
25833705ce5bSJoe Perches				}
25843705ce5bSJoe Perches			}
258500df344fSAndy Whitcroft		}
258600df344fSAndy Whitcroft		# Remove C99 comments.
25870a920b5bSAndy Whitcroft		$line =~ s@//.*@@;
25886c72ffaaSAndy Whitcroft		$opline =~ s@//.*@@;
25890a920b5bSAndy Whitcroft
25902b474a1aSAndy Whitcroft# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
25912b474a1aSAndy Whitcroft# the whole statement.
25922b474a1aSAndy Whitcroft#print "APW <$lines[$realline_next - 1]>\n";
25932b474a1aSAndy Whitcroft		if (defined $realline_next &&
25942b474a1aSAndy Whitcroft		    exists $lines[$realline_next - 1] &&
25952b474a1aSAndy Whitcroft		    !defined $suppress_export{$realline_next} &&
25962b474a1aSAndy Whitcroft		    ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
25972b474a1aSAndy Whitcroft		     $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
25983cbf62dfSAndy Whitcroft			# Handle definitions which produce identifiers with
25993cbf62dfSAndy Whitcroft			# a prefix:
26003cbf62dfSAndy Whitcroft			#   XXX(foo);
26013cbf62dfSAndy Whitcroft			#   EXPORT_SYMBOL(something_foo);
2602653d4876SAndy Whitcroft			my $name = $1;
260387a53877SAndy Whitcroft			if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
26043cbf62dfSAndy Whitcroft			    $name =~ /^${Ident}_$2/) {
26053cbf62dfSAndy Whitcroft#print "FOO C name<$name>\n";
26063cbf62dfSAndy Whitcroft				$suppress_export{$realline_next} = 1;
26073cbf62dfSAndy Whitcroft
26083cbf62dfSAndy Whitcroft			} elsif ($stat !~ /(?:
26092b474a1aSAndy Whitcroft				\n.}\s*$|
261048012058SAndy Whitcroft				^.DEFINE_$Ident\(\Q$name\E\)|
261148012058SAndy Whitcroft				^.DECLARE_$Ident\(\Q$name\E\)|
261248012058SAndy Whitcroft				^.LIST_HEAD\(\Q$name\E\)|
26132b474a1aSAndy Whitcroft				^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
26142b474a1aSAndy Whitcroft				\b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
261548012058SAndy Whitcroft			    )/x) {
26162b474a1aSAndy Whitcroft#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
26172b474a1aSAndy Whitcroft				$suppress_export{$realline_next} = 2;
26182b474a1aSAndy Whitcroft			} else {
26192b474a1aSAndy Whitcroft				$suppress_export{$realline_next} = 1;
26200a920b5bSAndy Whitcroft			}
26210a920b5bSAndy Whitcroft		}
26222b474a1aSAndy Whitcroft		if (!defined $suppress_export{$linenr} &&
26232b474a1aSAndy Whitcroft		    $prevline =~ /^.\s*$/ &&
26242b474a1aSAndy Whitcroft		    ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
26252b474a1aSAndy Whitcroft		     $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
26262b474a1aSAndy Whitcroft#print "FOO B <$lines[$linenr - 1]>\n";
26272b474a1aSAndy Whitcroft			$suppress_export{$linenr} = 2;
26282b474a1aSAndy Whitcroft		}
26292b474a1aSAndy Whitcroft		if (defined $suppress_export{$linenr} &&
26302b474a1aSAndy Whitcroft		    $suppress_export{$linenr} == 2) {
2631000d1cc1SJoe Perches			WARN("EXPORT_SYMBOL",
2632000d1cc1SJoe Perches			     "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
26332b474a1aSAndy Whitcroft		}
26340a920b5bSAndy Whitcroft
26355150bda4SJoe Eloff# check for global initialisers.
2636d5e616fcSJoe Perches		if ($line =~ /^\+(\s*$Type\s*$Ident\s*(?:\s+$Modifier))*\s*=\s*(0|NULL|false)\s*;/) {
2637d5e616fcSJoe Perches			if (ERROR("GLOBAL_INITIALISERS",
2638000d1cc1SJoe Perches				  "do not initialise globals to 0 or NULL\n" .
2639d5e616fcSJoe Perches				      $herecurr) &&
2640d5e616fcSJoe Perches			    $fix) {
2641d5e616fcSJoe Perches				$fixed[$linenr - 1] =~ s/($Type\s*$Ident\s*(?:\s+$Modifier))*\s*=\s*(0|NULL|false)\s*;/$1;/;
2642d5e616fcSJoe Perches			}
2643f0a594c1SAndy Whitcroft		}
26440a920b5bSAndy Whitcroft# check for static initialisers.
2645d5e616fcSJoe Perches		if ($line =~ /^\+.*\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
2646d5e616fcSJoe Perches			if (ERROR("INITIALISED_STATIC",
2647000d1cc1SJoe Perches				  "do not initialise statics to 0 or NULL\n" .
2648d5e616fcSJoe Perches				      $herecurr) &&
2649d5e616fcSJoe Perches			    $fix) {
2650d5e616fcSJoe Perches				$fixed[$linenr - 1] =~ s/(\bstatic\s.*?)\s*=\s*(0|NULL|false)\s*;/$1;/;
2651d5e616fcSJoe Perches			}
26520a920b5bSAndy Whitcroft		}
26530a920b5bSAndy Whitcroft
2654cb710ecaSJoe Perches# check for static const char * arrays.
2655cb710ecaSJoe Perches		if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
2656000d1cc1SJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
2657000d1cc1SJoe Perches			     "static const char * array should probably be static const char * const\n" .
2658cb710ecaSJoe Perches				$herecurr);
2659cb710ecaSJoe Perches               }
2660cb710ecaSJoe Perches
2661cb710ecaSJoe Perches# check for static char foo[] = "bar" declarations.
2662cb710ecaSJoe Perches		if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
2663000d1cc1SJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
2664000d1cc1SJoe Perches			     "static char array declaration should probably be static const char\n" .
2665cb710ecaSJoe Perches				$herecurr);
2666cb710ecaSJoe Perches               }
2667cb710ecaSJoe Perches
266892e112fdSJoe Perches# check for uses of DEFINE_PCI_DEVICE_TABLE
266992e112fdSJoe Perches		if ($line =~ /\bDEFINE_PCI_DEVICE_TABLE\s*\(\s*(\w+)\s*\)\s*=/) {
267092e112fdSJoe Perches			if (WARN("DEFINE_PCI_DEVICE_TABLE",
267192e112fdSJoe Perches				 "Prefer struct pci_device_id over deprecated DEFINE_PCI_DEVICE_TABLE\n" . $herecurr) &&
267292e112fdSJoe Perches			    $fix) {
267392e112fdSJoe Perches				$fixed[$linenr - 1] =~ s/\b(?:static\s+|)DEFINE_PCI_DEVICE_TABLE\s*\(\s*(\w+)\s*\)\s*=\s*/static const struct pci_device_id $1\[\] = /;
267492e112fdSJoe Perches			}
267593ed0e2dSJoe Perches		}
267693ed0e2dSJoe Perches
2677653d4876SAndy Whitcroft# check for new typedefs, only function parameters and sparse annotations
2678653d4876SAndy Whitcroft# make sense.
2679653d4876SAndy Whitcroft		if ($line =~ /\btypedef\s/ &&
26808054576dSAndy Whitcroft		    $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
2681c45dcabdSAndy Whitcroft		    $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
26828ed22cadSAndy Whitcroft		    $line !~ /\b$typeTypedefs\b/ &&
2683653d4876SAndy Whitcroft		    $line !~ /\b__bitwise(?:__|)\b/) {
2684000d1cc1SJoe Perches			WARN("NEW_TYPEDEFS",
2685000d1cc1SJoe Perches			     "do not add new typedefs\n" . $herecurr);
26860a920b5bSAndy Whitcroft		}
26870a920b5bSAndy Whitcroft
26880a920b5bSAndy Whitcroft# * goes on variable not on type
268965863862SAndy Whitcroft		# (char*[ const])
2690bfcb2cc7SAndy Whitcroft		while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
2691bfcb2cc7SAndy Whitcroft			#print "AA<$1>\n";
26923705ce5bSJoe Perches			my ($ident, $from, $to) = ($1, $2, $2);
2693d8aaf121SAndy Whitcroft
269465863862SAndy Whitcroft			# Should start with a space.
269565863862SAndy Whitcroft			$to =~ s/^(\S)/ $1/;
269665863862SAndy Whitcroft			# Should not end with a space.
269765863862SAndy Whitcroft			$to =~ s/\s+$//;
269865863862SAndy Whitcroft			# '*'s should not have spaces between.
2699f9a0b3d1SAndy Whitcroft			while ($to =~ s/\*\s+\*/\*\*/) {
270065863862SAndy Whitcroft			}
2701d8aaf121SAndy Whitcroft
27023705ce5bSJoe Perches##			print "1: from<$from> to<$to> ident<$ident>\n";
270365863862SAndy Whitcroft			if ($from ne $to) {
27043705ce5bSJoe Perches				if (ERROR("POINTER_LOCATION",
27053705ce5bSJoe Perches					  "\"(foo$from)\" should be \"(foo$to)\"\n" .  $herecurr) &&
27063705ce5bSJoe Perches				    $fix) {
27073705ce5bSJoe Perches					my $sub_from = $ident;
27083705ce5bSJoe Perches					my $sub_to = $ident;
27093705ce5bSJoe Perches					$sub_to =~ s/\Q$from\E/$to/;
27103705ce5bSJoe Perches					$fixed[$linenr - 1] =~
27113705ce5bSJoe Perches					    s@\Q$sub_from\E@$sub_to@;
27123705ce5bSJoe Perches				}
271365863862SAndy Whitcroft			}
2714bfcb2cc7SAndy Whitcroft		}
2715bfcb2cc7SAndy Whitcroft		while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
2716bfcb2cc7SAndy Whitcroft			#print "BB<$1>\n";
27173705ce5bSJoe Perches			my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
2718d8aaf121SAndy Whitcroft
271965863862SAndy Whitcroft			# Should start with a space.
272065863862SAndy Whitcroft			$to =~ s/^(\S)/ $1/;
272165863862SAndy Whitcroft			# Should not end with a space.
272265863862SAndy Whitcroft			$to =~ s/\s+$//;
272365863862SAndy Whitcroft			# '*'s should not have spaces between.
2724f9a0b3d1SAndy Whitcroft			while ($to =~ s/\*\s+\*/\*\*/) {
272565863862SAndy Whitcroft			}
272665863862SAndy Whitcroft			# Modifiers should have spaces.
272765863862SAndy Whitcroft			$to =~ s/(\b$Modifier$)/$1 /;
272865863862SAndy Whitcroft
27293705ce5bSJoe Perches##			print "2: from<$from> to<$to> ident<$ident>\n";
2730667026e7SAndy Whitcroft			if ($from ne $to && $ident !~ /^$Modifier$/) {
27313705ce5bSJoe Perches				if (ERROR("POINTER_LOCATION",
27323705ce5bSJoe Perches					  "\"foo${from}bar\" should be \"foo${to}bar\"\n" .  $herecurr) &&
27333705ce5bSJoe Perches				    $fix) {
27343705ce5bSJoe Perches
27353705ce5bSJoe Perches					my $sub_from = $match;
27363705ce5bSJoe Perches					my $sub_to = $match;
27373705ce5bSJoe Perches					$sub_to =~ s/\Q$from\E/$to/;
27383705ce5bSJoe Perches					$fixed[$linenr - 1] =~
27393705ce5bSJoe Perches					    s@\Q$sub_from\E@$sub_to@;
27403705ce5bSJoe Perches				}
274165863862SAndy Whitcroft			}
27420a920b5bSAndy Whitcroft		}
27430a920b5bSAndy Whitcroft
27440a920b5bSAndy Whitcroft# # no BUG() or BUG_ON()
27450a920b5bSAndy Whitcroft# 		if ($line =~ /\b(BUG|BUG_ON)\b/) {
27460a920b5bSAndy Whitcroft# 			print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
27470a920b5bSAndy Whitcroft# 			print "$herecurr";
27480a920b5bSAndy Whitcroft# 			$clean = 0;
27490a920b5bSAndy Whitcroft# 		}
27500a920b5bSAndy Whitcroft
27518905a67cSAndy Whitcroft		if ($line =~ /\bLINUX_VERSION_CODE\b/) {
2752000d1cc1SJoe Perches			WARN("LINUX_VERSION_CODE",
2753000d1cc1SJoe Perches			     "LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
27548905a67cSAndy Whitcroft		}
27558905a67cSAndy Whitcroft
275617441227SJoe Perches# check for uses of printk_ratelimit
275717441227SJoe Perches		if ($line =~ /\bprintk_ratelimit\s*\(/) {
2758000d1cc1SJoe Perches			WARN("PRINTK_RATELIMITED",
2759000d1cc1SJoe Perches"Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
276017441227SJoe Perches		}
276117441227SJoe Perches
276200df344fSAndy Whitcroft# printk should use KERN_* levels.  Note that follow on printk's on the
276300df344fSAndy Whitcroft# same line do not need a level, so we use the current block context
276400df344fSAndy Whitcroft# to try and find and validate the current printk.  In summary the current
276525985edcSLucas De Marchi# printk includes all preceding printk's which have no newline on the end.
276600df344fSAndy Whitcroft# we assume the first bad printk is the one to report.
2767f0a594c1SAndy Whitcroft		if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
276800df344fSAndy Whitcroft			my $ok = 0;
276900df344fSAndy Whitcroft			for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
277000df344fSAndy Whitcroft				#print "CHECK<$lines[$ln - 1]\n";
277125985edcSLucas De Marchi				# we have a preceding printk if it ends
277200df344fSAndy Whitcroft				# with "\n" ignore it, else it is to blame
277300df344fSAndy Whitcroft				if ($lines[$ln - 1] =~ m{\bprintk\(}) {
277400df344fSAndy Whitcroft					if ($rawlines[$ln - 1] !~ m{\\n"}) {
277500df344fSAndy Whitcroft						$ok = 1;
277600df344fSAndy Whitcroft					}
277700df344fSAndy Whitcroft					last;
277800df344fSAndy Whitcroft				}
277900df344fSAndy Whitcroft			}
278000df344fSAndy Whitcroft			if ($ok == 0) {
2781000d1cc1SJoe Perches				WARN("PRINTK_WITHOUT_KERN_LEVEL",
2782000d1cc1SJoe Perches				     "printk() should include KERN_ facility level\n" . $herecurr);
27830a920b5bSAndy Whitcroft			}
278400df344fSAndy Whitcroft		}
27850a920b5bSAndy Whitcroft
2786243f3803SJoe Perches		if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
2787243f3803SJoe Perches			my $orig = $1;
2788243f3803SJoe Perches			my $level = lc($orig);
2789243f3803SJoe Perches			$level = "warn" if ($level eq "warning");
27908f26b837SJoe Perches			my $level2 = $level;
27918f26b837SJoe Perches			$level2 = "dbg" if ($level eq "debug");
2792243f3803SJoe Perches			WARN("PREFER_PR_LEVEL",
27938f26b837SJoe Perches			     "Prefer netdev_$level2(netdev, ... then dev_$level2(dev, ... then pr_$level(...  to printk(KERN_$orig ...\n" . $herecurr);
2794243f3803SJoe Perches		}
2795243f3803SJoe Perches
2796243f3803SJoe Perches		if ($line =~ /\bpr_warning\s*\(/) {
2797d5e616fcSJoe Perches			if (WARN("PREFER_PR_LEVEL",
2798d5e616fcSJoe Perches				 "Prefer pr_warn(... to pr_warning(...\n" . $herecurr) &&
2799d5e616fcSJoe Perches			    $fix) {
2800d5e616fcSJoe Perches				$fixed[$linenr - 1] =~
2801d5e616fcSJoe Perches				    s/\bpr_warning\b/pr_warn/;
2802d5e616fcSJoe Perches			}
2803243f3803SJoe Perches		}
2804243f3803SJoe Perches
2805dc139313SJoe Perches		if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
2806dc139313SJoe Perches			my $orig = $1;
2807dc139313SJoe Perches			my $level = lc($orig);
2808dc139313SJoe Perches			$level = "warn" if ($level eq "warning");
2809dc139313SJoe Perches			$level = "dbg" if ($level eq "debug");
2810dc139313SJoe Perches			WARN("PREFER_DEV_LEVEL",
2811dc139313SJoe Perches			     "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
2812dc139313SJoe Perches		}
2813dc139313SJoe Perches
2814653d4876SAndy Whitcroft# function brace can't be on same line, except for #defines of do while,
2815653d4876SAndy Whitcroft# or if closed on same line
2816c45dcabdSAndy Whitcroft		if (($line=~/$Type\s*$Ident\(.*\).*\s{/) and
2817c45dcabdSAndy Whitcroft		    !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) {
2818000d1cc1SJoe Perches			ERROR("OPEN_BRACE",
2819000d1cc1SJoe Perches			      "open brace '{' following function declarations go on the next line\n" . $herecurr);
28200a920b5bSAndy Whitcroft		}
2821653d4876SAndy Whitcroft
28228905a67cSAndy Whitcroft# open braces for enum, union and struct go on the same line.
28238905a67cSAndy Whitcroft		if ($line =~ /^.\s*{/ &&
28248905a67cSAndy Whitcroft		    $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
2825000d1cc1SJoe Perches			ERROR("OPEN_BRACE",
2826000d1cc1SJoe Perches			      "open brace '{' following $1 go on the same line\n" . $hereprev);
28278905a67cSAndy Whitcroft		}
28288905a67cSAndy Whitcroft
28290c73b4ebSAndy Whitcroft# missing space after union, struct or enum definition
28303705ce5bSJoe Perches		if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
28313705ce5bSJoe Perches			if (WARN("SPACING",
28323705ce5bSJoe Perches				 "missing space after $1 definition\n" . $herecurr) &&
28333705ce5bSJoe Perches			    $fix) {
28343705ce5bSJoe Perches				$fixed[$linenr - 1] =~
28353705ce5bSJoe Perches				    s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
28363705ce5bSJoe Perches			}
28370c73b4ebSAndy Whitcroft		}
28380c73b4ebSAndy Whitcroft
283931070b5dSJoe Perches# Function pointer declarations
284031070b5dSJoe Perches# check spacing between type, funcptr, and args
284131070b5dSJoe Perches# canonical declaration is "type (*funcptr)(args...)"
284231070b5dSJoe Perches#
284331070b5dSJoe Perches# the $Declare variable will capture all spaces after the type
284431070b5dSJoe Perches# so check it for trailing missing spaces or multiple spaces
284531070b5dSJoe Perches		if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)$Ident(\s*)\)(\s*)\(/) {
284631070b5dSJoe Perches			my $declare = $1;
284731070b5dSJoe Perches			my $pre_pointer_space = $2;
284831070b5dSJoe Perches			my $post_pointer_space = $3;
284931070b5dSJoe Perches			my $funcname = $4;
285031070b5dSJoe Perches			my $post_funcname_space = $5;
285131070b5dSJoe Perches			my $pre_args_space = $6;
285231070b5dSJoe Perches
285331070b5dSJoe Perches			if ($declare !~ /\s$/) {
285431070b5dSJoe Perches				WARN("SPACING",
285531070b5dSJoe Perches				     "missing space after return type\n" . $herecurr);
285631070b5dSJoe Perches			}
285731070b5dSJoe Perches
285831070b5dSJoe Perches# unnecessary space "type  (*funcptr)(args...)"
285931070b5dSJoe Perches			elsif ($declare =~ /\s{2,}$/) {
286031070b5dSJoe Perches				WARN("SPACING",
286131070b5dSJoe Perches				     "Multiple spaces after return type\n" . $herecurr);
286231070b5dSJoe Perches			}
286331070b5dSJoe Perches
286431070b5dSJoe Perches# unnecessary space "type ( *funcptr)(args...)"
286531070b5dSJoe Perches			if (defined $pre_pointer_space &&
286631070b5dSJoe Perches			    $pre_pointer_space =~ /^\s/) {
286731070b5dSJoe Perches				WARN("SPACING",
286831070b5dSJoe Perches				     "Unnecessary space after function pointer open parenthesis\n" . $herecurr);
286931070b5dSJoe Perches			}
287031070b5dSJoe Perches
287131070b5dSJoe Perches# unnecessary space "type (* funcptr)(args...)"
287231070b5dSJoe Perches			if (defined $post_pointer_space &&
287331070b5dSJoe Perches			    $post_pointer_space =~ /^\s/) {
287431070b5dSJoe Perches				WARN("SPACING",
287531070b5dSJoe Perches				     "Unnecessary space before function pointer name\n" . $herecurr);
287631070b5dSJoe Perches			}
287731070b5dSJoe Perches
287831070b5dSJoe Perches# unnecessary space "type (*funcptr )(args...)"
287931070b5dSJoe Perches			if (defined $post_funcname_space &&
288031070b5dSJoe Perches			    $post_funcname_space =~ /^\s/) {
288131070b5dSJoe Perches				WARN("SPACING",
288231070b5dSJoe Perches				     "Unnecessary space after function pointer name\n" . $herecurr);
288331070b5dSJoe Perches			}
288431070b5dSJoe Perches
288531070b5dSJoe Perches# unnecessary space "type (*funcptr) (args...)"
288631070b5dSJoe Perches			if (defined $pre_args_space &&
288731070b5dSJoe Perches			    $pre_args_space =~ /^\s/) {
288831070b5dSJoe Perches				WARN("SPACING",
288931070b5dSJoe Perches				     "Unnecessary space before function pointer arguments\n" . $herecurr);
289031070b5dSJoe Perches			}
289131070b5dSJoe Perches
289231070b5dSJoe Perches			if (show_type("SPACING") && $fix) {
289331070b5dSJoe Perches				$fixed[$linenr - 1] =~
289431070b5dSJoe Perches				    s/^(.\s*$Declare)\(\s*\*\s*($Ident)\s*\)\s*\(/rtrim($1) . " " . "\(\*$2\)\("/ex;
289531070b5dSJoe Perches			}
289631070b5dSJoe Perches		}
289731070b5dSJoe Perches
28988d31cfceSAndy Whitcroft# check for spacing round square brackets; allowed:
28998d31cfceSAndy Whitcroft#  1. with a type on the left -- int [] a;
2900fe2a7dbcSAndy Whitcroft#  2. at the beginning of a line for slice initialisers -- [0...10] = 5,
2901fe2a7dbcSAndy Whitcroft#  3. inside a curly brace -- = { [0...10] = 5 }
29028d31cfceSAndy Whitcroft		while ($line =~ /(.*?\s)\[/g) {
29038d31cfceSAndy Whitcroft			my ($where, $prefix) = ($-[1], $1);
29048d31cfceSAndy Whitcroft			if ($prefix !~ /$Type\s+$/ &&
2905fe2a7dbcSAndy Whitcroft			    ($where != 0 || $prefix !~ /^.\s+$/) &&
2906daebc534SAndy Whitcroft			    $prefix !~ /[{,]\s+$/) {
29073705ce5bSJoe Perches				if (ERROR("BRACKET_SPACE",
29083705ce5bSJoe Perches					  "space prohibited before open square bracket '['\n" . $herecurr) &&
29093705ce5bSJoe Perches				    $fix) {
29103705ce5bSJoe Perches				    $fixed[$linenr - 1] =~
29113705ce5bSJoe Perches					s/^(\+.*?)\s+\[/$1\[/;
29123705ce5bSJoe Perches				}
29138d31cfceSAndy Whitcroft			}
29148d31cfceSAndy Whitcroft		}
29158d31cfceSAndy Whitcroft
2916f0a594c1SAndy Whitcroft# check for spaces between functions and their parentheses.
29176c72ffaaSAndy Whitcroft		while ($line =~ /($Ident)\s+\(/g) {
2918c2fdda0dSAndy Whitcroft			my $name = $1;
2919773647a0SAndy Whitcroft			my $ctx_before = substr($line, 0, $-[1]);
2920773647a0SAndy Whitcroft			my $ctx = "$ctx_before$name";
2921c2fdda0dSAndy Whitcroft
2922c2fdda0dSAndy Whitcroft			# Ignore those directives where spaces _are_ permitted.
2923773647a0SAndy Whitcroft			if ($name =~ /^(?:
2924773647a0SAndy Whitcroft				if|for|while|switch|return|case|
2925773647a0SAndy Whitcroft				volatile|__volatile__|
2926773647a0SAndy Whitcroft				__attribute__|format|__extension__|
2927773647a0SAndy Whitcroft				asm|__asm__)$/x)
2928773647a0SAndy Whitcroft			{
2929c2fdda0dSAndy Whitcroft			# cpp #define statements have non-optional spaces, ie
2930c2fdda0dSAndy Whitcroft			# if there is a space between the name and the open
2931c2fdda0dSAndy Whitcroft			# parenthesis it is simply not a parameter group.
2932c45dcabdSAndy Whitcroft			} elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
2933773647a0SAndy Whitcroft
2934773647a0SAndy Whitcroft			# cpp #elif statement condition may start with a (
2935c45dcabdSAndy Whitcroft			} elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
2936c2fdda0dSAndy Whitcroft
2937c2fdda0dSAndy Whitcroft			# If this whole things ends with a type its most
2938c2fdda0dSAndy Whitcroft			# likely a typedef for a function.
2939773647a0SAndy Whitcroft			} elsif ($ctx =~ /$Type$/) {
2940c2fdda0dSAndy Whitcroft
2941c2fdda0dSAndy Whitcroft			} else {
29423705ce5bSJoe Perches				if (WARN("SPACING",
29433705ce5bSJoe Perches					 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
29443705ce5bSJoe Perches					     $fix) {
29453705ce5bSJoe Perches					$fixed[$linenr - 1] =~
29463705ce5bSJoe Perches					    s/\b$name\s+\(/$name\(/;
29473705ce5bSJoe Perches				}
2948f0a594c1SAndy Whitcroft			}
29496c72ffaaSAndy Whitcroft		}
29509a4cad4eSEric Nelson
2951653d4876SAndy Whitcroft# Check operator spacing.
29520a920b5bSAndy Whitcroft		if (!($line=~/\#\s*include/)) {
29533705ce5bSJoe Perches			my $fixed_line = "";
29543705ce5bSJoe Perches			my $line_fixed = 0;
29553705ce5bSJoe Perches
29569c0ca6f9SAndy Whitcroft			my $ops = qr{
29579c0ca6f9SAndy Whitcroft				<<=|>>=|<=|>=|==|!=|
29589c0ca6f9SAndy Whitcroft				\+=|-=|\*=|\/=|%=|\^=|\|=|&=|
29599c0ca6f9SAndy Whitcroft				=>|->|<<|>>|<|>|=|!|~|
29601f65f947SAndy Whitcroft				&&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
296184731623SJoe Perches				\?:|\?|:
29629c0ca6f9SAndy Whitcroft			}x;
2963cf655043SAndy Whitcroft			my @elements = split(/($ops|;)/, $opline);
29643705ce5bSJoe Perches
29653705ce5bSJoe Perches##			print("element count: <" . $#elements . ">\n");
29663705ce5bSJoe Perches##			foreach my $el (@elements) {
29673705ce5bSJoe Perches##				print("el: <$el>\n");
29683705ce5bSJoe Perches##			}
29693705ce5bSJoe Perches
29703705ce5bSJoe Perches			my @fix_elements = ();
297100df344fSAndy Whitcroft			my $off = 0;
29726c72ffaaSAndy Whitcroft
29733705ce5bSJoe Perches			foreach my $el (@elements) {
29743705ce5bSJoe Perches				push(@fix_elements, substr($rawline, $off, length($el)));
29753705ce5bSJoe Perches				$off += length($el);
29763705ce5bSJoe Perches			}
29773705ce5bSJoe Perches
29783705ce5bSJoe Perches			$off = 0;
29793705ce5bSJoe Perches
29806c72ffaaSAndy Whitcroft			my $blank = copy_spacing($opline);
2981b34c648bSJoe Perches			my $last_after = -1;
29826c72ffaaSAndy Whitcroft
29830a920b5bSAndy Whitcroft			for (my $n = 0; $n < $#elements; $n += 2) {
29843705ce5bSJoe Perches
29853705ce5bSJoe Perches				my $good = $fix_elements[$n] . $fix_elements[$n + 1];
29863705ce5bSJoe Perches
29873705ce5bSJoe Perches##				print("n: <$n> good: <$good>\n");
29883705ce5bSJoe Perches
29894a0df2efSAndy Whitcroft				$off += length($elements[$n]);
29904a0df2efSAndy Whitcroft
299125985edcSLucas De Marchi				# Pick up the preceding and succeeding characters.
2992773647a0SAndy Whitcroft				my $ca = substr($opline, 0, $off);
2993773647a0SAndy Whitcroft				my $cc = '';
2994773647a0SAndy Whitcroft				if (length($opline) >= ($off + length($elements[$n + 1]))) {
2995773647a0SAndy Whitcroft					$cc = substr($opline, $off + length($elements[$n + 1]));
2996773647a0SAndy Whitcroft				}
2997773647a0SAndy Whitcroft				my $cb = "$ca$;$cc";
2998773647a0SAndy Whitcroft
29994a0df2efSAndy Whitcroft				my $a = '';
30004a0df2efSAndy Whitcroft				$a = 'V' if ($elements[$n] ne '');
30014a0df2efSAndy Whitcroft				$a = 'W' if ($elements[$n] =~ /\s$/);
3002cf655043SAndy Whitcroft				$a = 'C' if ($elements[$n] =~ /$;$/);
30034a0df2efSAndy Whitcroft				$a = 'B' if ($elements[$n] =~ /(\[|\()$/);
30044a0df2efSAndy Whitcroft				$a = 'O' if ($elements[$n] eq '');
3005773647a0SAndy Whitcroft				$a = 'E' if ($ca =~ /^\s*$/);
30064a0df2efSAndy Whitcroft
30070a920b5bSAndy Whitcroft				my $op = $elements[$n + 1];
30084a0df2efSAndy Whitcroft
30094a0df2efSAndy Whitcroft				my $c = '';
30100a920b5bSAndy Whitcroft				if (defined $elements[$n + 2]) {
30114a0df2efSAndy Whitcroft					$c = 'V' if ($elements[$n + 2] ne '');
30124a0df2efSAndy Whitcroft					$c = 'W' if ($elements[$n + 2] =~ /^\s/);
3013cf655043SAndy Whitcroft					$c = 'C' if ($elements[$n + 2] =~ /^$;/);
30144a0df2efSAndy Whitcroft					$c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
30154a0df2efSAndy Whitcroft					$c = 'O' if ($elements[$n + 2] eq '');
30168b1b3378SAndy Whitcroft					$c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
30174a0df2efSAndy Whitcroft				} else {
30184a0df2efSAndy Whitcroft					$c = 'E';
30190a920b5bSAndy Whitcroft				}
30200a920b5bSAndy Whitcroft
30214a0df2efSAndy Whitcroft				my $ctx = "${a}x${c}";
30224a0df2efSAndy Whitcroft
30234a0df2efSAndy Whitcroft				my $at = "(ctx:$ctx)";
30244a0df2efSAndy Whitcroft
30256c72ffaaSAndy Whitcroft				my $ptr = substr($blank, 0, $off) . "^";
3026de7d4f0eSAndy Whitcroft				my $hereptr = "$hereline$ptr\n";
30270a920b5bSAndy Whitcroft
302874048ed8SAndy Whitcroft				# Pull out the value of this operator.
30296c72ffaaSAndy Whitcroft				my $op_type = substr($curr_values, $off + 1, 1);
30300a920b5bSAndy Whitcroft
30311f65f947SAndy Whitcroft				# Get the full operator variant.
30321f65f947SAndy Whitcroft				my $opv = $op . substr($curr_vars, $off, 1);
30331f65f947SAndy Whitcroft
303413214adfSAndy Whitcroft				# Ignore operators passed as parameters.
303513214adfSAndy Whitcroft				if ($op_type ne 'V' &&
303613214adfSAndy Whitcroft				    $ca =~ /\s$/ && $cc =~ /^\s*,/) {
303713214adfSAndy Whitcroft
3038cf655043SAndy Whitcroft#				# Ignore comments
3039cf655043SAndy Whitcroft#				} elsif ($op =~ /^$;+$/) {
304013214adfSAndy Whitcroft
3041d8aaf121SAndy Whitcroft				# ; should have either the end of line or a space or \ after it
304213214adfSAndy Whitcroft				} elsif ($op eq ';') {
3043cf655043SAndy Whitcroft					if ($ctx !~ /.x[WEBC]/ &&
3044cf655043SAndy Whitcroft					    $cc !~ /^\\/ && $cc !~ /^;/) {
30453705ce5bSJoe Perches						if (ERROR("SPACING",
30463705ce5bSJoe Perches							  "space required after that '$op' $at\n" . $hereptr)) {
3047b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
30483705ce5bSJoe Perches							$line_fixed = 1;
30493705ce5bSJoe Perches						}
3050d8aaf121SAndy Whitcroft					}
3051d8aaf121SAndy Whitcroft
3052d8aaf121SAndy Whitcroft				# // is a comment
3053d8aaf121SAndy Whitcroft				} elsif ($op eq '//') {
30540a920b5bSAndy Whitcroft
30551f65f947SAndy Whitcroft				# No spaces for:
30561f65f947SAndy Whitcroft				#   ->
30571f65f947SAndy Whitcroft				#   :   when part of a bitfield
30581f65f947SAndy Whitcroft				} elsif ($op eq '->' || $opv eq ':B') {
30594a0df2efSAndy Whitcroft					if ($ctx =~ /Wx.|.xW/) {
30603705ce5bSJoe Perches						if (ERROR("SPACING",
30613705ce5bSJoe Perches							  "spaces prohibited around that '$op' $at\n" . $hereptr)) {
3062b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
30633705ce5bSJoe Perches							if (defined $fix_elements[$n + 2]) {
30643705ce5bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
30653705ce5bSJoe Perches							}
3066b34c648bSJoe Perches							$line_fixed = 1;
30673705ce5bSJoe Perches						}
30680a920b5bSAndy Whitcroft					}
30690a920b5bSAndy Whitcroft
30700a920b5bSAndy Whitcroft				# , must have a space on the right.
30710a920b5bSAndy Whitcroft				} elsif ($op eq ',') {
3072cf655043SAndy Whitcroft					if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
30733705ce5bSJoe Perches						if (ERROR("SPACING",
30743705ce5bSJoe Perches							  "space required after that '$op' $at\n" . $hereptr)) {
3075b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
30763705ce5bSJoe Perches							$line_fixed = 1;
3077b34c648bSJoe Perches							$last_after = $n;
30783705ce5bSJoe Perches						}
30790a920b5bSAndy Whitcroft					}
30800a920b5bSAndy Whitcroft
30819c0ca6f9SAndy Whitcroft				# '*' as part of a type definition -- reported already.
308274048ed8SAndy Whitcroft				} elsif ($opv eq '*_') {
30839c0ca6f9SAndy Whitcroft					#warn "'*' is part of type\n";
30849c0ca6f9SAndy Whitcroft
30859c0ca6f9SAndy Whitcroft				# unary operators should have a space before and
30869c0ca6f9SAndy Whitcroft				# none after.  May be left adjacent to another
30879c0ca6f9SAndy Whitcroft				# unary operator, or a cast
30889c0ca6f9SAndy Whitcroft				} elsif ($op eq '!' || $op eq '~' ||
308974048ed8SAndy Whitcroft					 $opv eq '*U' || $opv eq '-U' ||
30900d413866SAndy Whitcroft					 $opv eq '&U' || $opv eq '&&U') {
3091cf655043SAndy Whitcroft					if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
30923705ce5bSJoe Perches						if (ERROR("SPACING",
30933705ce5bSJoe Perches							  "space required before that '$op' $at\n" . $hereptr)) {
3094b34c648bSJoe Perches							if ($n != $last_after + 2) {
3095b34c648bSJoe Perches								$good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]);
30963705ce5bSJoe Perches								$line_fixed = 1;
30973705ce5bSJoe Perches							}
30980a920b5bSAndy Whitcroft						}
3099b34c648bSJoe Perches					}
3100a3340b35SAndy Whitcroft					if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
3101171ae1a4SAndy Whitcroft						# A unary '*' may be const
3102171ae1a4SAndy Whitcroft
3103171ae1a4SAndy Whitcroft					} elsif ($ctx =~ /.xW/) {
31043705ce5bSJoe Perches						if (ERROR("SPACING",
31053705ce5bSJoe Perches							  "space prohibited after that '$op' $at\n" . $hereptr)) {
3106b34c648bSJoe Perches							$good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]);
31073705ce5bSJoe Perches							if (defined $fix_elements[$n + 2]) {
31083705ce5bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
31093705ce5bSJoe Perches							}
3110b34c648bSJoe Perches							$line_fixed = 1;
31113705ce5bSJoe Perches						}
31120a920b5bSAndy Whitcroft					}
31130a920b5bSAndy Whitcroft
31140a920b5bSAndy Whitcroft				# unary ++ and unary -- are allowed no space on one side.
31150a920b5bSAndy Whitcroft				} elsif ($op eq '++' or $op eq '--') {
3116773647a0SAndy Whitcroft					if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
31173705ce5bSJoe Perches						if (ERROR("SPACING",
31183705ce5bSJoe Perches							  "space required one side of that '$op' $at\n" . $hereptr)) {
3119b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
31203705ce5bSJoe Perches							$line_fixed = 1;
31213705ce5bSJoe Perches						}
31220a920b5bSAndy Whitcroft					}
3123773647a0SAndy Whitcroft					if ($ctx =~ /Wx[BE]/ ||
3124773647a0SAndy Whitcroft					    ($ctx =~ /Wx./ && $cc =~ /^;/)) {
31253705ce5bSJoe Perches						if (ERROR("SPACING",
31263705ce5bSJoe Perches							  "space prohibited before that '$op' $at\n" . $hereptr)) {
3127b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
31283705ce5bSJoe Perches							$line_fixed = 1;
31293705ce5bSJoe Perches						}
3130653d4876SAndy Whitcroft					}
3131773647a0SAndy Whitcroft					if ($ctx =~ /ExW/) {
31323705ce5bSJoe Perches						if (ERROR("SPACING",
31333705ce5bSJoe Perches							  "space prohibited after that '$op' $at\n" . $hereptr)) {
3134b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
31353705ce5bSJoe Perches							if (defined $fix_elements[$n + 2]) {
31363705ce5bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
3137773647a0SAndy Whitcroft							}
3138b34c648bSJoe Perches							$line_fixed = 1;
31393705ce5bSJoe Perches						}
31403705ce5bSJoe Perches					}
31410a920b5bSAndy Whitcroft
31420a920b5bSAndy Whitcroft				# << and >> may either have or not have spaces both sides
31439c0ca6f9SAndy Whitcroft				} elsif ($op eq '<<' or $op eq '>>' or
31449c0ca6f9SAndy Whitcroft					 $op eq '&' or $op eq '^' or $op eq '|' or
31459c0ca6f9SAndy Whitcroft					 $op eq '+' or $op eq '-' or
3146c2fdda0dSAndy Whitcroft					 $op eq '*' or $op eq '/' or
3147c2fdda0dSAndy Whitcroft					 $op eq '%')
31480a920b5bSAndy Whitcroft				{
3149773647a0SAndy Whitcroft					if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
31503705ce5bSJoe Perches						if (ERROR("SPACING",
31513705ce5bSJoe Perches							  "need consistent spacing around '$op' $at\n" . $hereptr)) {
3152b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
3153b34c648bSJoe Perches							if (defined $fix_elements[$n + 2]) {
3154b34c648bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
3155b34c648bSJoe Perches							}
31563705ce5bSJoe Perches							$line_fixed = 1;
31573705ce5bSJoe Perches						}
31580a920b5bSAndy Whitcroft					}
31590a920b5bSAndy Whitcroft
31601f65f947SAndy Whitcroft				# A colon needs no spaces before when it is
31611f65f947SAndy Whitcroft				# terminating a case value or a label.
31621f65f947SAndy Whitcroft				} elsif ($opv eq ':C' || $opv eq ':L') {
31631f65f947SAndy Whitcroft					if ($ctx =~ /Wx./) {
31643705ce5bSJoe Perches						if (ERROR("SPACING",
31653705ce5bSJoe Perches							  "space prohibited before that '$op' $at\n" . $hereptr)) {
3166b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
31673705ce5bSJoe Perches							$line_fixed = 1;
31683705ce5bSJoe Perches						}
31691f65f947SAndy Whitcroft					}
31701f65f947SAndy Whitcroft
31710a920b5bSAndy Whitcroft				# All the others need spaces both sides.
3172cf655043SAndy Whitcroft				} elsif ($ctx !~ /[EWC]x[CWE]/) {
31731f65f947SAndy Whitcroft					my $ok = 0;
31741f65f947SAndy Whitcroft
317522f2a2efSAndy Whitcroft					# Ignore email addresses <foo@bar>
31761f65f947SAndy Whitcroft					if (($op eq '<' &&
31771f65f947SAndy Whitcroft					     $cc =~ /^\S+\@\S+>/) ||
31781f65f947SAndy Whitcroft					    ($op eq '>' &&
31791f65f947SAndy Whitcroft					     $ca =~ /<\S+\@\S+$/))
31801f65f947SAndy Whitcroft					{
31811f65f947SAndy Whitcroft					    	$ok = 1;
31821f65f947SAndy Whitcroft					}
31831f65f947SAndy Whitcroft
318484731623SJoe Perches					# messages are ERROR, but ?: are CHK
31851f65f947SAndy Whitcroft					if ($ok == 0) {
318684731623SJoe Perches						my $msg_type = \&ERROR;
318784731623SJoe Perches						$msg_type = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
318884731623SJoe Perches
318984731623SJoe Perches						if (&{$msg_type}("SPACING",
31903705ce5bSJoe Perches								 "spaces required around that '$op' $at\n" . $hereptr)) {
3191b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
3192b34c648bSJoe Perches							if (defined $fix_elements[$n + 2]) {
3193b34c648bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
3194b34c648bSJoe Perches							}
31953705ce5bSJoe Perches							$line_fixed = 1;
31963705ce5bSJoe Perches						}
31970a920b5bSAndy Whitcroft					}
319822f2a2efSAndy Whitcroft				}
31994a0df2efSAndy Whitcroft				$off += length($elements[$n + 1]);
32003705ce5bSJoe Perches
32013705ce5bSJoe Perches##				print("n: <$n> GOOD: <$good>\n");
32023705ce5bSJoe Perches
32033705ce5bSJoe Perches				$fixed_line = $fixed_line . $good;
32040a920b5bSAndy Whitcroft			}
32053705ce5bSJoe Perches
32063705ce5bSJoe Perches			if (($#elements % 2) == 0) {
32073705ce5bSJoe Perches				$fixed_line = $fixed_line . $fix_elements[$#elements];
32083705ce5bSJoe Perches			}
32093705ce5bSJoe Perches
32103705ce5bSJoe Perches			if ($fix && $line_fixed && $fixed_line ne $fixed[$linenr - 1]) {
32113705ce5bSJoe Perches				$fixed[$linenr - 1] = $fixed_line;
32123705ce5bSJoe Perches			}
32133705ce5bSJoe Perches
32143705ce5bSJoe Perches
32150a920b5bSAndy Whitcroft		}
32160a920b5bSAndy Whitcroft
3217786b6326SJoe Perches# check for whitespace before a non-naked semicolon
3218d2e248e7SJoe Perches		if ($line =~ /^\+.*\S\s+;\s*$/) {
3219786b6326SJoe Perches			if (WARN("SPACING",
3220786b6326SJoe Perches				 "space prohibited before semicolon\n" . $herecurr) &&
3221786b6326SJoe Perches			    $fix) {
3222786b6326SJoe Perches				1 while $fixed[$linenr - 1] =~
3223786b6326SJoe Perches				    s/^(\+.*\S)\s+;/$1;/;
3224786b6326SJoe Perches			}
3225786b6326SJoe Perches		}
3226786b6326SJoe Perches
3227f0a594c1SAndy Whitcroft# check for multiple assignments
3228f0a594c1SAndy Whitcroft		if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
3229000d1cc1SJoe Perches			CHK("MULTIPLE_ASSIGNMENTS",
3230000d1cc1SJoe Perches			    "multiple assignments should be avoided\n" . $herecurr);
3231f0a594c1SAndy Whitcroft		}
3232f0a594c1SAndy Whitcroft
323322f2a2efSAndy Whitcroft## # check for multiple declarations, allowing for a function declaration
323422f2a2efSAndy Whitcroft## # continuation.
323522f2a2efSAndy Whitcroft## 		if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
323622f2a2efSAndy Whitcroft## 		    $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
323722f2a2efSAndy Whitcroft##
323822f2a2efSAndy Whitcroft## 			# Remove any bracketed sections to ensure we do not
323922f2a2efSAndy Whitcroft## 			# falsly report the parameters of functions.
324022f2a2efSAndy Whitcroft## 			my $ln = $line;
324122f2a2efSAndy Whitcroft## 			while ($ln =~ s/\([^\(\)]*\)//g) {
324222f2a2efSAndy Whitcroft## 			}
324322f2a2efSAndy Whitcroft## 			if ($ln =~ /,/) {
3244000d1cc1SJoe Perches## 				WARN("MULTIPLE_DECLARATION",
3245000d1cc1SJoe Perches##				     "declaring multiple variables together should be avoided\n" . $herecurr);
324622f2a2efSAndy Whitcroft## 			}
324722f2a2efSAndy Whitcroft## 		}
3248f0a594c1SAndy Whitcroft
32490a920b5bSAndy Whitcroft#need space before brace following if, while, etc
325022f2a2efSAndy Whitcroft		if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
325122f2a2efSAndy Whitcroft		    $line =~ /do{/) {
32523705ce5bSJoe Perches			if (ERROR("SPACING",
32533705ce5bSJoe Perches				  "space required before the open brace '{'\n" . $herecurr) &&
32543705ce5bSJoe Perches			    $fix) {
3255d5e616fcSJoe Perches				$fixed[$linenr - 1] =~ s/^(\+.*(?:do|\))){/$1 {/;
32563705ce5bSJoe Perches			}
3257de7d4f0eSAndy Whitcroft		}
3258de7d4f0eSAndy Whitcroft
3259c4a62ef9SJoe Perches## # check for blank lines before declarations
3260c4a62ef9SJoe Perches##		if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
3261c4a62ef9SJoe Perches##		    $prevrawline =~ /^.\s*$/) {
3262c4a62ef9SJoe Perches##			WARN("SPACING",
3263c4a62ef9SJoe Perches##			     "No blank lines before declarations\n" . $hereprev);
3264c4a62ef9SJoe Perches##		}
3265c4a62ef9SJoe Perches##
3266c4a62ef9SJoe Perches
3267de7d4f0eSAndy Whitcroft# closing brace should have a space following it when it has anything
3268de7d4f0eSAndy Whitcroft# on the line
3269de7d4f0eSAndy Whitcroft		if ($line =~ /}(?!(?:,|;|\)))\S/) {
3270d5e616fcSJoe Perches			if (ERROR("SPACING",
3271d5e616fcSJoe Perches				  "space required after that close brace '}'\n" . $herecurr) &&
3272d5e616fcSJoe Perches			    $fix) {
3273d5e616fcSJoe Perches				$fixed[$linenr - 1] =~
3274d5e616fcSJoe Perches				    s/}((?!(?:,|;|\)))\S)/} $1/;
3275d5e616fcSJoe Perches			}
32760a920b5bSAndy Whitcroft		}
32770a920b5bSAndy Whitcroft
327822f2a2efSAndy Whitcroft# check spacing on square brackets
327922f2a2efSAndy Whitcroft		if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
32803705ce5bSJoe Perches			if (ERROR("SPACING",
32813705ce5bSJoe Perches				  "space prohibited after that open square bracket '['\n" . $herecurr) &&
32823705ce5bSJoe Perches			    $fix) {
32833705ce5bSJoe Perches				$fixed[$linenr - 1] =~
32843705ce5bSJoe Perches				    s/\[\s+/\[/;
32853705ce5bSJoe Perches			}
328622f2a2efSAndy Whitcroft		}
328722f2a2efSAndy Whitcroft		if ($line =~ /\s\]/) {
32883705ce5bSJoe Perches			if (ERROR("SPACING",
32893705ce5bSJoe Perches				  "space prohibited before that close square bracket ']'\n" . $herecurr) &&
32903705ce5bSJoe Perches			    $fix) {
32913705ce5bSJoe Perches				$fixed[$linenr - 1] =~
32923705ce5bSJoe Perches				    s/\s+\]/\]/;
32933705ce5bSJoe Perches			}
329422f2a2efSAndy Whitcroft		}
329522f2a2efSAndy Whitcroft
3296c45dcabdSAndy Whitcroft# check spacing on parentheses
32979c0ca6f9SAndy Whitcroft		if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
32989c0ca6f9SAndy Whitcroft		    $line !~ /for\s*\(\s+;/) {
32993705ce5bSJoe Perches			if (ERROR("SPACING",
33003705ce5bSJoe Perches				  "space prohibited after that open parenthesis '('\n" . $herecurr) &&
33013705ce5bSJoe Perches			    $fix) {
33023705ce5bSJoe Perches				$fixed[$linenr - 1] =~
33033705ce5bSJoe Perches				    s/\(\s+/\(/;
33043705ce5bSJoe Perches			}
330522f2a2efSAndy Whitcroft		}
330613214adfSAndy Whitcroft		if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
3307c45dcabdSAndy Whitcroft		    $line !~ /for\s*\(.*;\s+\)/ &&
3308c45dcabdSAndy Whitcroft		    $line !~ /:\s+\)/) {
33093705ce5bSJoe Perches			if (ERROR("SPACING",
33103705ce5bSJoe Perches				  "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
33113705ce5bSJoe Perches			    $fix) {
33123705ce5bSJoe Perches				$fixed[$linenr - 1] =~
33133705ce5bSJoe Perches				    s/\s+\)/\)/;
33143705ce5bSJoe Perches			}
331522f2a2efSAndy Whitcroft		}
331622f2a2efSAndy Whitcroft
33170a920b5bSAndy Whitcroft#goto labels aren't indented, allow a single space however
33184a0df2efSAndy Whitcroft		if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
33190a920b5bSAndy Whitcroft		   !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
33203705ce5bSJoe Perches			if (WARN("INDENTED_LABEL",
33213705ce5bSJoe Perches				 "labels should not be indented\n" . $herecurr) &&
33223705ce5bSJoe Perches			    $fix) {
33233705ce5bSJoe Perches				$fixed[$linenr - 1] =~
33243705ce5bSJoe Perches				    s/^(.)\s+/$1/;
33253705ce5bSJoe Perches			}
33260a920b5bSAndy Whitcroft		}
33270a920b5bSAndy Whitcroft
3328c45dcabdSAndy Whitcroft# Return is not a function.
3329507e5141SJoe Perches		if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {
3330c45dcabdSAndy Whitcroft			my $spacing = $1;
3331507e5141SJoe Perches			if ($^V && $^V ge 5.10.0 &&
3332507e5141SJoe Perches			    $stat =~ /^.\s*return\s*$balanced_parens\s*;\s*$/) {
3333000d1cc1SJoe Perches				ERROR("RETURN_PARENTHESES",
3334000d1cc1SJoe Perches				      "return is not a function, parentheses are not required\n" . $herecurr);
3335c45dcabdSAndy Whitcroft
3336c45dcabdSAndy Whitcroft			} elsif ($spacing !~ /\s+/) {
3337000d1cc1SJoe Perches				ERROR("SPACING",
3338000d1cc1SJoe Perches				      "space required before the open parenthesis '('\n" . $herecurr);
3339c45dcabdSAndy Whitcroft			}
3340c45dcabdSAndy Whitcroft		}
3341507e5141SJoe Perches
3342189248d8SJoe Perches# if statements using unnecessary parentheses - ie: if ((foo == bar))
3343189248d8SJoe Perches		if ($^V && $^V ge 5.10.0 &&
3344189248d8SJoe Perches		    $line =~ /\bif\s*((?:\(\s*){2,})/) {
3345189248d8SJoe Perches			my $openparens = $1;
3346189248d8SJoe Perches			my $count = $openparens =~ tr@\(@\(@;
3347189248d8SJoe Perches			my $msg = "";
3348189248d8SJoe Perches			if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) {
3349189248d8SJoe Perches				my $comp = $4;	#Not $1 because of $LvalOrFunc
3350189248d8SJoe Perches				$msg = " - maybe == should be = ?" if ($comp eq "==");
3351189248d8SJoe Perches				WARN("UNNECESSARY_PARENTHESES",
3352189248d8SJoe Perches				     "Unnecessary parentheses$msg\n" . $herecurr);
3353189248d8SJoe Perches			}
3354189248d8SJoe Perches		}
3355189248d8SJoe Perches
335653a3c448SAndy Whitcroft# Return of what appears to be an errno should normally be -'ve
335753a3c448SAndy Whitcroft		if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) {
335853a3c448SAndy Whitcroft			my $name = $1;
335953a3c448SAndy Whitcroft			if ($name ne 'EOF' && $name ne 'ERROR') {
3360000d1cc1SJoe Perches				WARN("USE_NEGATIVE_ERRNO",
3361000d1cc1SJoe Perches				     "return of an errno should typically be -ve (return -$1)\n" . $herecurr);
336253a3c448SAndy Whitcroft			}
336353a3c448SAndy Whitcroft		}
3364c45dcabdSAndy Whitcroft
33650a920b5bSAndy Whitcroft# Need a space before open parenthesis after if, while etc
33664a0df2efSAndy Whitcroft		if ($line =~ /\b(if|while|for|switch)\(/) {
33673705ce5bSJoe Perches			if (ERROR("SPACING",
33683705ce5bSJoe Perches				  "space required before the open parenthesis '('\n" . $herecurr) &&
33693705ce5bSJoe Perches			    $fix) {
33703705ce5bSJoe Perches				$fixed[$linenr - 1] =~
33713705ce5bSJoe Perches				    s/\b(if|while|for|switch)\(/$1 \(/;
33723705ce5bSJoe Perches			}
33730a920b5bSAndy Whitcroft		}
33740a920b5bSAndy Whitcroft
3375f5fe35ddSAndy Whitcroft# Check for illegal assignment in if conditional -- and check for trailing
3376f5fe35ddSAndy Whitcroft# statements after the conditional.
3377170d3a22SAndy Whitcroft		if ($line =~ /do\s*(?!{)/) {
33783e469cdcSAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
33793e469cdcSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0)
33803e469cdcSAndy Whitcroft					if (!defined $stat);
3381170d3a22SAndy Whitcroft			my ($stat_next) = ctx_statement_block($line_nr_next,
3382170d3a22SAndy Whitcroft						$remain_next, $off_next);
3383170d3a22SAndy Whitcroft			$stat_next =~ s/\n./\n /g;
3384170d3a22SAndy Whitcroft			##print "stat<$stat> stat_next<$stat_next>\n";
3385170d3a22SAndy Whitcroft
3386170d3a22SAndy Whitcroft			if ($stat_next =~ /^\s*while\b/) {
3387170d3a22SAndy Whitcroft				# If the statement carries leading newlines,
3388170d3a22SAndy Whitcroft				# then count those as offsets.
3389170d3a22SAndy Whitcroft				my ($whitespace) =
3390170d3a22SAndy Whitcroft					($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
3391170d3a22SAndy Whitcroft				my $offset =
3392170d3a22SAndy Whitcroft					statement_rawlines($whitespace) - 1;
3393170d3a22SAndy Whitcroft
3394170d3a22SAndy Whitcroft				$suppress_whiletrailers{$line_nr_next +
3395170d3a22SAndy Whitcroft								$offset} = 1;
3396170d3a22SAndy Whitcroft			}
3397170d3a22SAndy Whitcroft		}
3398170d3a22SAndy Whitcroft		if (!defined $suppress_whiletrailers{$linenr} &&
3399c11230f4SJoe Perches		    defined($stat) && defined($cond) &&
3400170d3a22SAndy Whitcroft		    $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
3401171ae1a4SAndy Whitcroft			my ($s, $c) = ($stat, $cond);
34028905a67cSAndy Whitcroft
3403b53c8e10SAndy Whitcroft			if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
3404000d1cc1SJoe Perches				ERROR("ASSIGN_IN_IF",
3405000d1cc1SJoe Perches				      "do not use assignment in if condition\n" . $herecurr);
34068905a67cSAndy Whitcroft			}
34078905a67cSAndy Whitcroft
34088905a67cSAndy Whitcroft			# Find out what is on the end of the line after the
34098905a67cSAndy Whitcroft			# conditional.
3410773647a0SAndy Whitcroft			substr($s, 0, length($c), '');
34118905a67cSAndy Whitcroft			$s =~ s/\n.*//g;
341213214adfSAndy Whitcroft			$s =~ s/$;//g; 	# Remove any comments
341353210168SAndy Whitcroft			if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
341453210168SAndy Whitcroft			    $c !~ /}\s*while\s*/)
3415773647a0SAndy Whitcroft			{
3416bb44ad39SAndy Whitcroft				# Find out how long the conditional actually is.
3417bb44ad39SAndy Whitcroft				my @newlines = ($c =~ /\n/gs);
3418bb44ad39SAndy Whitcroft				my $cond_lines = 1 + $#newlines;
341942bdf74cSHidetoshi Seto				my $stat_real = '';
3420bb44ad39SAndy Whitcroft
342142bdf74cSHidetoshi Seto				$stat_real = raw_line($linenr, $cond_lines)
342242bdf74cSHidetoshi Seto							. "\n" if ($cond_lines);
3423bb44ad39SAndy Whitcroft				if (defined($stat_real) && $cond_lines > 1) {
3424bb44ad39SAndy Whitcroft					$stat_real = "[...]\n$stat_real";
3425bb44ad39SAndy Whitcroft				}
3426bb44ad39SAndy Whitcroft
3427000d1cc1SJoe Perches				ERROR("TRAILING_STATEMENTS",
3428000d1cc1SJoe Perches				      "trailing statements should be on next line\n" . $herecurr . $stat_real);
34298905a67cSAndy Whitcroft			}
34308905a67cSAndy Whitcroft		}
34318905a67cSAndy Whitcroft
343213214adfSAndy Whitcroft# Check for bitwise tests written as boolean
343313214adfSAndy Whitcroft		if ($line =~ /
343413214adfSAndy Whitcroft			(?:
343513214adfSAndy Whitcroft				(?:\[|\(|\&\&|\|\|)
343613214adfSAndy Whitcroft				\s*0[xX][0-9]+\s*
343713214adfSAndy Whitcroft				(?:\&\&|\|\|)
343813214adfSAndy Whitcroft			|
343913214adfSAndy Whitcroft				(?:\&\&|\|\|)
344013214adfSAndy Whitcroft				\s*0[xX][0-9]+\s*
344113214adfSAndy Whitcroft				(?:\&\&|\|\||\)|\])
344213214adfSAndy Whitcroft			)/x)
344313214adfSAndy Whitcroft		{
3444000d1cc1SJoe Perches			WARN("HEXADECIMAL_BOOLEAN_TEST",
3445000d1cc1SJoe Perches			     "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
344613214adfSAndy Whitcroft		}
344713214adfSAndy Whitcroft
34488905a67cSAndy Whitcroft# if and else should not have general statements after it
344913214adfSAndy Whitcroft		if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
345013214adfSAndy Whitcroft			my $s = $1;
345113214adfSAndy Whitcroft			$s =~ s/$;//g; 	# Remove any comments
345213214adfSAndy Whitcroft			if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
3453000d1cc1SJoe Perches				ERROR("TRAILING_STATEMENTS",
3454000d1cc1SJoe Perches				      "trailing statements should be on next line\n" . $herecurr);
34550a920b5bSAndy Whitcroft			}
345613214adfSAndy Whitcroft		}
345739667782SAndy Whitcroft# if should not continue a brace
345839667782SAndy Whitcroft		if ($line =~ /}\s*if\b/) {
3459000d1cc1SJoe Perches			ERROR("TRAILING_STATEMENTS",
3460000d1cc1SJoe Perches			      "trailing statements should be on next line\n" .
346139667782SAndy Whitcroft				$herecurr);
346239667782SAndy Whitcroft		}
3463a1080bf8SAndy Whitcroft# case and default should not have general statements after them
3464a1080bf8SAndy Whitcroft		if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
3465a1080bf8SAndy Whitcroft		    $line !~ /\G(?:
34663fef12d6SAndy Whitcroft			(?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
3467a1080bf8SAndy Whitcroft			\s*return\s+
3468a1080bf8SAndy Whitcroft		    )/xg)
3469a1080bf8SAndy Whitcroft		{
3470000d1cc1SJoe Perches			ERROR("TRAILING_STATEMENTS",
3471000d1cc1SJoe Perches			      "trailing statements should be on next line\n" . $herecurr);
3472a1080bf8SAndy Whitcroft		}
34730a920b5bSAndy Whitcroft
34740a920b5bSAndy Whitcroft		# Check for }<nl>else {, these must be at the same
34750a920b5bSAndy Whitcroft		# indent level to be relevant to each other.
34760a920b5bSAndy Whitcroft		if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
34770a920b5bSAndy Whitcroft						$previndent == $indent) {
3478000d1cc1SJoe Perches			ERROR("ELSE_AFTER_BRACE",
3479000d1cc1SJoe Perches			      "else should follow close brace '}'\n" . $hereprev);
34800a920b5bSAndy Whitcroft		}
34810a920b5bSAndy Whitcroft
3482c2fdda0dSAndy Whitcroft		if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and
3483c2fdda0dSAndy Whitcroft						$previndent == $indent) {
3484c2fdda0dSAndy Whitcroft			my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
3485c2fdda0dSAndy Whitcroft
3486c2fdda0dSAndy Whitcroft			# Find out what is on the end of the line after the
3487c2fdda0dSAndy Whitcroft			# conditional.
3488773647a0SAndy Whitcroft			substr($s, 0, length($c), '');
3489c2fdda0dSAndy Whitcroft			$s =~ s/\n.*//g;
3490c2fdda0dSAndy Whitcroft
3491c2fdda0dSAndy Whitcroft			if ($s =~ /^\s*;/) {
3492000d1cc1SJoe Perches				ERROR("WHILE_AFTER_BRACE",
3493000d1cc1SJoe Perches				      "while should follow close brace '}'\n" . $hereprev);
3494c2fdda0dSAndy Whitcroft			}
3495c2fdda0dSAndy Whitcroft		}
3496c2fdda0dSAndy Whitcroft
349795e2c602SJoe Perches#Specific variable tests
3498323c1260SJoe Perches		while ($line =~ m{($Constant|$Lval)}g) {
3499323c1260SJoe Perches			my $var = $1;
350095e2c602SJoe Perches
350195e2c602SJoe Perches#gcc binary extension
350295e2c602SJoe Perches			if ($var =~ /^$Binary$/) {
3503d5e616fcSJoe Perches				if (WARN("GCC_BINARY_CONSTANT",
3504d5e616fcSJoe Perches					 "Avoid gcc v4.3+ binary constant extension: <$var>\n" . $herecurr) &&
3505d5e616fcSJoe Perches				    $fix) {
3506d5e616fcSJoe Perches					my $hexval = sprintf("0x%x", oct($var));
3507d5e616fcSJoe Perches					$fixed[$linenr - 1] =~
3508d5e616fcSJoe Perches					    s/\b$var\b/$hexval/;
3509d5e616fcSJoe Perches				}
351095e2c602SJoe Perches			}
351195e2c602SJoe Perches
351295e2c602SJoe Perches#CamelCase
3513807bd26cSJoe Perches			if ($var !~ /^$Constant$/ &&
3514be79794bSJoe Perches			    $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
351522735ce8SJoe Perches#Ignore Page<foo> variants
3516807bd26cSJoe Perches			    $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
351722735ce8SJoe Perches#Ignore SI style variants like nS, mV and dB (ie: max_uV, regulator_min_uA_show)
35183445686aSJoe Perches			    $var !~ /^(?:[a-z_]*?)_?[a-z][A-Z](?:_[a-z_]+)?$/) {
35197e781f67SJoe Perches				while ($var =~ m{($Ident)}g) {
35207e781f67SJoe Perches					my $word = $1;
35217e781f67SJoe Perches					next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
3522d8b07710SJoe Perches					if ($check) {
3523d8b07710SJoe Perches						seed_camelcase_includes();
3524d8b07710SJoe Perches						if (!$file && !$camelcase_file_seeded) {
3525d8b07710SJoe Perches							seed_camelcase_file($realfile);
3526d8b07710SJoe Perches							$camelcase_file_seeded = 1;
3527d8b07710SJoe Perches						}
3528d8b07710SJoe Perches					}
35297e781f67SJoe Perches					if (!defined $camelcase{$word}) {
35307e781f67SJoe Perches						$camelcase{$word} = 1;
3531be79794bSJoe Perches						CHK("CAMELCASE",
35327e781f67SJoe Perches						    "Avoid CamelCase: <$word>\n" . $herecurr);
35337e781f67SJoe Perches					}
3534323c1260SJoe Perches				}
3535323c1260SJoe Perches			}
35363445686aSJoe Perches		}
35370a920b5bSAndy Whitcroft
35380a920b5bSAndy Whitcroft#no spaces allowed after \ in define
3539d5e616fcSJoe Perches		if ($line =~ /\#\s*define.*\\\s+$/) {
3540d5e616fcSJoe Perches			if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
3541d5e616fcSJoe Perches				 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
3542d5e616fcSJoe Perches			    $fix) {
3543d5e616fcSJoe Perches				$fixed[$linenr - 1] =~ s/\s+$//;
3544d5e616fcSJoe Perches			}
35450a920b5bSAndy Whitcroft		}
35460a920b5bSAndy Whitcroft
3547653d4876SAndy Whitcroft#warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
3548c45dcabdSAndy Whitcroft		if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
3549e09dec48SAndy Whitcroft			my $file = "$1.h";
3550e09dec48SAndy Whitcroft			my $checkfile = "include/linux/$file";
3551e09dec48SAndy Whitcroft			if (-f "$root/$checkfile" &&
3552e09dec48SAndy Whitcroft			    $realfile ne $checkfile &&
35537840a94cSWolfram Sang			    $1 !~ /$allowed_asm_includes/)
3554c45dcabdSAndy Whitcroft			{
3555e09dec48SAndy Whitcroft				if ($realfile =~ m{^arch/}) {
3556000d1cc1SJoe Perches					CHK("ARCH_INCLUDE_LINUX",
3557000d1cc1SJoe Perches					    "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
3558e09dec48SAndy Whitcroft				} else {
3559000d1cc1SJoe Perches					WARN("INCLUDE_LINUX",
3560000d1cc1SJoe Perches					     "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
3561e09dec48SAndy Whitcroft				}
35620a920b5bSAndy Whitcroft			}
35630a920b5bSAndy Whitcroft		}
35640a920b5bSAndy Whitcroft
3565653d4876SAndy Whitcroft# multi-statement macros should be enclosed in a do while loop, grab the
3566653d4876SAndy Whitcroft# first statement and ensure its the whole macro if its not enclosed
3567cf655043SAndy Whitcroft# in a known good container
3568b8f96a31SAndy Whitcroft		if ($realfile !~ m@/vmlinux.lds.h$@ &&
3569b8f96a31SAndy Whitcroft		    $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
3570d8aaf121SAndy Whitcroft			my $ln = $linenr;
3571d8aaf121SAndy Whitcroft			my $cnt = $realcnt;
3572c45dcabdSAndy Whitcroft			my ($off, $dstat, $dcond, $rest);
3573c45dcabdSAndy Whitcroft			my $ctx = '';
3574c45dcabdSAndy Whitcroft			($dstat, $dcond, $ln, $cnt, $off) =
3575f74bd194SAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0);
3576f74bd194SAndy Whitcroft			$ctx = $dstat;
3577c45dcabdSAndy Whitcroft			#print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
3578a3bb97a7SAndy Whitcroft			#print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
3579c45dcabdSAndy Whitcroft
3580f74bd194SAndy Whitcroft			$dstat =~ s/^.\s*\#\s*define\s+$Ident(?:\([^\)]*\))?\s*//;
3581292f1a9bSAndy Whitcroft			$dstat =~ s/$;//g;
3582c45dcabdSAndy Whitcroft			$dstat =~ s/\\\n.//g;
3583c45dcabdSAndy Whitcroft			$dstat =~ s/^\s*//s;
3584c45dcabdSAndy Whitcroft			$dstat =~ s/\s*$//s;
3585c45dcabdSAndy Whitcroft
3586c45dcabdSAndy Whitcroft			# Flatten any parentheses and braces
3587bf30d6edSAndy Whitcroft			while ($dstat =~ s/\([^\(\)]*\)/1/ ||
3588bf30d6edSAndy Whitcroft			       $dstat =~ s/\{[^\{\}]*\}/1/ ||
3589c81769fdSAndy Whitcroft			       $dstat =~ s/\[[^\[\]]*\]/1/)
3590bf30d6edSAndy Whitcroft			{
3591c45dcabdSAndy Whitcroft			}
3592c45dcabdSAndy Whitcroft
3593e45bab8eSAndy Whitcroft			# Flatten any obvious string concatentation.
3594e45bab8eSAndy Whitcroft			while ($dstat =~ s/("X*")\s*$Ident/$1/ ||
3595e45bab8eSAndy Whitcroft			       $dstat =~ s/$Ident\s*("X*")/$1/)
3596e45bab8eSAndy Whitcroft			{
3597e45bab8eSAndy Whitcroft			}
3598e45bab8eSAndy Whitcroft
3599c45dcabdSAndy Whitcroft			my $exceptions = qr{
3600c45dcabdSAndy Whitcroft				$Declare|
3601c45dcabdSAndy Whitcroft				module_param_named|
3602a0a0a7a9SKees Cook				MODULE_PARM_DESC|
3603c45dcabdSAndy Whitcroft				DECLARE_PER_CPU|
3604c45dcabdSAndy Whitcroft				DEFINE_PER_CPU|
3605383099fdSAndy Whitcroft				__typeof__\(|
360622fd2d3eSStefani Seibold				union|
360722fd2d3eSStefani Seibold				struct|
3608ea71a0a0SAndy Whitcroft				\.$Ident\s*=\s*|
3609ea71a0a0SAndy Whitcroft				^\"|\"$
3610c45dcabdSAndy Whitcroft			}x;
36115eaa20b9SAndy Whitcroft			#print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
3612f74bd194SAndy Whitcroft			if ($dstat ne '' &&
3613f74bd194SAndy Whitcroft			    $dstat !~ /^(?:$Ident|-?$Constant),$/ &&			# 10, // foo(),
3614f74bd194SAndy Whitcroft			    $dstat !~ /^(?:$Ident|-?$Constant);$/ &&			# foo();
36153cc4b1c3SJoe Perches			    $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ &&		# 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
3616b9df76acSAndy Whitcroft			    $dstat !~ /^'X'$/ &&					# character constants
3617f74bd194SAndy Whitcroft			    $dstat !~ /$exceptions/ &&
3618f74bd194SAndy Whitcroft			    $dstat !~ /^\.$Ident\s*=/ &&				# .foo =
3619e942e2c3SJoe Perches			    $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ &&		# stringification #foo
362072f115f9SAndy Whitcroft			    $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ &&	# do {...} while (...); // do {...} while (...)
3621f74bd194SAndy Whitcroft			    $dstat !~ /^for\s*$Constant$/ &&				# for (...)
3622f74bd194SAndy Whitcroft			    $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ &&	# for (...) bar()
3623f74bd194SAndy Whitcroft			    $dstat !~ /^do\s*{/ &&					# do {...
3624f95a7e6aSJoe Perches			    $dstat !~ /^\({/ &&						# ({...
3625f95a7e6aSJoe Perches			    $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
3626c45dcabdSAndy Whitcroft			{
3627f74bd194SAndy Whitcroft				$ctx =~ s/\n*$//;
3628f74bd194SAndy Whitcroft				my $herectx = $here . "\n";
3629f74bd194SAndy Whitcroft				my $cnt = statement_rawlines($ctx);
3630f74bd194SAndy Whitcroft
3631f74bd194SAndy Whitcroft				for (my $n = 0; $n < $cnt; $n++) {
3632f74bd194SAndy Whitcroft					$herectx .= raw_line($linenr, $n) . "\n";
3633c45dcabdSAndy Whitcroft				}
3634c45dcabdSAndy Whitcroft
3635f74bd194SAndy Whitcroft				if ($dstat =~ /;/) {
3636f74bd194SAndy Whitcroft					ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
3637f74bd194SAndy Whitcroft					      "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
3638f74bd194SAndy Whitcroft				} else {
3639000d1cc1SJoe Perches					ERROR("COMPLEX_MACRO",
3640f74bd194SAndy Whitcroft					      "Macros with complex values should be enclosed in parenthesis\n" . "$herectx");
3641d8aaf121SAndy Whitcroft				}
36420a920b5bSAndy Whitcroft			}
36435023d347SJoe Perches
3644481eb486SJoe Perches# check for line continuations outside of #defines, preprocessor #, and asm
36455023d347SJoe Perches
36465023d347SJoe Perches		} else {
36475023d347SJoe Perches			if ($prevline !~ /^..*\\$/ &&
3648481eb486SJoe Perches			    $line !~ /^\+\s*\#.*\\$/ &&		# preprocessor
3649481eb486SJoe Perches			    $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ &&	# asm
36505023d347SJoe Perches			    $line =~ /^\+.*\\$/) {
36515023d347SJoe Perches				WARN("LINE_CONTINUATIONS",
36525023d347SJoe Perches				     "Avoid unnecessary line continuations\n" . $herecurr);
36535023d347SJoe Perches			}
3654653d4876SAndy Whitcroft		}
36550a920b5bSAndy Whitcroft
3656b13edf7fSJoe Perches# do {} while (0) macro tests:
3657b13edf7fSJoe Perches# single-statement macros do not need to be enclosed in do while (0) loop,
3658b13edf7fSJoe Perches# macro should not end with a semicolon
3659b13edf7fSJoe Perches		if ($^V && $^V ge 5.10.0 &&
3660b13edf7fSJoe Perches		    $realfile !~ m@/vmlinux.lds.h$@ &&
3661b13edf7fSJoe Perches		    $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
3662b13edf7fSJoe Perches			my $ln = $linenr;
3663b13edf7fSJoe Perches			my $cnt = $realcnt;
3664b13edf7fSJoe Perches			my ($off, $dstat, $dcond, $rest);
3665b13edf7fSJoe Perches			my $ctx = '';
3666b13edf7fSJoe Perches			($dstat, $dcond, $ln, $cnt, $off) =
3667b13edf7fSJoe Perches				ctx_statement_block($linenr, $realcnt, 0);
3668b13edf7fSJoe Perches			$ctx = $dstat;
3669b13edf7fSJoe Perches
3670b13edf7fSJoe Perches			$dstat =~ s/\\\n.//g;
3671b13edf7fSJoe Perches
3672b13edf7fSJoe Perches			if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
3673b13edf7fSJoe Perches				my $stmts = $2;
3674b13edf7fSJoe Perches				my $semis = $3;
3675b13edf7fSJoe Perches
3676b13edf7fSJoe Perches				$ctx =~ s/\n*$//;
3677b13edf7fSJoe Perches				my $cnt = statement_rawlines($ctx);
3678b13edf7fSJoe Perches				my $herectx = $here . "\n";
3679b13edf7fSJoe Perches
3680b13edf7fSJoe Perches				for (my $n = 0; $n < $cnt; $n++) {
3681b13edf7fSJoe Perches					$herectx .= raw_line($linenr, $n) . "\n";
3682b13edf7fSJoe Perches				}
3683b13edf7fSJoe Perches
3684ac8e97f8SJoe Perches				if (($stmts =~ tr/;/;/) == 1 &&
3685ac8e97f8SJoe Perches				    $stmts !~ /^\s*(if|while|for|switch)\b/) {
3686b13edf7fSJoe Perches					WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
3687b13edf7fSJoe Perches					     "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
3688b13edf7fSJoe Perches				}
3689b13edf7fSJoe Perches				if (defined $semis && $semis ne "") {
3690b13edf7fSJoe Perches					WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
3691b13edf7fSJoe Perches					     "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
3692b13edf7fSJoe Perches				}
3693b13edf7fSJoe Perches			}
3694b13edf7fSJoe Perches		}
3695b13edf7fSJoe Perches
3696080ba929SMike Frysinger# make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
3697080ba929SMike Frysinger# all assignments may have only one of the following with an assignment:
3698080ba929SMike Frysinger#	.
3699080ba929SMike Frysinger#	ALIGN(...)
3700080ba929SMike Frysinger#	VMLINUX_SYMBOL(...)
3701080ba929SMike Frysinger		if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
3702000d1cc1SJoe Perches			WARN("MISSING_VMLINUX_SYMBOL",
3703000d1cc1SJoe Perches			     "vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
3704080ba929SMike Frysinger		}
3705080ba929SMike Frysinger
3706f0a594c1SAndy Whitcroft# check for redundant bracing round if etc
370713214adfSAndy Whitcroft		if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
370813214adfSAndy Whitcroft			my ($level, $endln, @chunks) =
3709cf655043SAndy Whitcroft				ctx_statement_full($linenr, $realcnt, 1);
371013214adfSAndy Whitcroft			#print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
3711cf655043SAndy Whitcroft			#print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
3712cf655043SAndy Whitcroft			if ($#chunks > 0 && $level == 0) {
3713aad4f614SJoe Perches				my @allowed = ();
3714aad4f614SJoe Perches				my $allow = 0;
371513214adfSAndy Whitcroft				my $seen = 0;
3716773647a0SAndy Whitcroft				my $herectx = $here . "\n";
3717cf655043SAndy Whitcroft				my $ln = $linenr - 1;
371813214adfSAndy Whitcroft				for my $chunk (@chunks) {
371913214adfSAndy Whitcroft					my ($cond, $block) = @{$chunk};
372013214adfSAndy Whitcroft
3721773647a0SAndy Whitcroft					# If the condition carries leading newlines, then count those as offsets.
3722773647a0SAndy Whitcroft					my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
3723773647a0SAndy Whitcroft					my $offset = statement_rawlines($whitespace) - 1;
3724773647a0SAndy Whitcroft
3725aad4f614SJoe Perches					$allowed[$allow] = 0;
3726773647a0SAndy Whitcroft					#print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
3727773647a0SAndy Whitcroft
3728773647a0SAndy Whitcroft					# We have looked at and allowed this specific line.
3729773647a0SAndy Whitcroft					$suppress_ifbraces{$ln + $offset} = 1;
3730773647a0SAndy Whitcroft
3731773647a0SAndy Whitcroft					$herectx .= "$rawlines[$ln + $offset]\n[...]\n";
3732cf655043SAndy Whitcroft					$ln += statement_rawlines($block) - 1;
3733cf655043SAndy Whitcroft
3734773647a0SAndy Whitcroft					substr($block, 0, length($cond), '');
373513214adfSAndy Whitcroft
373613214adfSAndy Whitcroft					$seen++ if ($block =~ /^\s*{/);
373713214adfSAndy Whitcroft
3738aad4f614SJoe Perches					#print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
3739cf655043SAndy Whitcroft					if (statement_lines($cond) > 1) {
3740cf655043SAndy Whitcroft						#print "APW: ALLOWED: cond<$cond>\n";
3741aad4f614SJoe Perches						$allowed[$allow] = 1;
374213214adfSAndy Whitcroft					}
374313214adfSAndy Whitcroft					if ($block =~/\b(?:if|for|while)\b/) {
3744cf655043SAndy Whitcroft						#print "APW: ALLOWED: block<$block>\n";
3745aad4f614SJoe Perches						$allowed[$allow] = 1;
374613214adfSAndy Whitcroft					}
3747cf655043SAndy Whitcroft					if (statement_block_size($block) > 1) {
3748cf655043SAndy Whitcroft						#print "APW: ALLOWED: lines block<$block>\n";
3749aad4f614SJoe Perches						$allowed[$allow] = 1;
375013214adfSAndy Whitcroft					}
3751aad4f614SJoe Perches					$allow++;
375213214adfSAndy Whitcroft				}
3753aad4f614SJoe Perches				if ($seen) {
3754aad4f614SJoe Perches					my $sum_allowed = 0;
3755aad4f614SJoe Perches					foreach (@allowed) {
3756aad4f614SJoe Perches						$sum_allowed += $_;
3757aad4f614SJoe Perches					}
3758aad4f614SJoe Perches					if ($sum_allowed == 0) {
3759000d1cc1SJoe Perches						WARN("BRACES",
3760000d1cc1SJoe Perches						     "braces {} are not necessary for any arm of this statement\n" . $herectx);
3761aad4f614SJoe Perches					} elsif ($sum_allowed != $allow &&
3762aad4f614SJoe Perches						 $seen != $allow) {
3763aad4f614SJoe Perches						CHK("BRACES",
3764aad4f614SJoe Perches						    "braces {} should be used on all arms of this statement\n" . $herectx);
3765aad4f614SJoe Perches					}
376613214adfSAndy Whitcroft				}
376713214adfSAndy Whitcroft			}
376813214adfSAndy Whitcroft		}
3769773647a0SAndy Whitcroft		if (!defined $suppress_ifbraces{$linenr - 1} &&
377013214adfSAndy Whitcroft					$line =~ /\b(if|while|for|else)\b/) {
3771cf655043SAndy Whitcroft			my $allowed = 0;
3772f0a594c1SAndy Whitcroft
3773cf655043SAndy Whitcroft			# Check the pre-context.
3774cf655043SAndy Whitcroft			if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
3775cf655043SAndy Whitcroft				#print "APW: ALLOWED: pre<$1>\n";
3776cf655043SAndy Whitcroft				$allowed = 1;
3777f0a594c1SAndy Whitcroft			}
3778773647a0SAndy Whitcroft
3779773647a0SAndy Whitcroft			my ($level, $endln, @chunks) =
3780773647a0SAndy Whitcroft				ctx_statement_full($linenr, $realcnt, $-[0]);
3781773647a0SAndy Whitcroft
3782cf655043SAndy Whitcroft			# Check the condition.
3783cf655043SAndy Whitcroft			my ($cond, $block) = @{$chunks[0]};
3784773647a0SAndy Whitcroft			#print "CHECKING<$linenr> cond<$cond> block<$block>\n";
3785cf655043SAndy Whitcroft			if (defined $cond) {
3786773647a0SAndy Whitcroft				substr($block, 0, length($cond), '');
3787cf655043SAndy Whitcroft			}
3788cf655043SAndy Whitcroft			if (statement_lines($cond) > 1) {
3789cf655043SAndy Whitcroft				#print "APW: ALLOWED: cond<$cond>\n";
3790cf655043SAndy Whitcroft				$allowed = 1;
3791cf655043SAndy Whitcroft			}
3792cf655043SAndy Whitcroft			if ($block =~/\b(?:if|for|while)\b/) {
3793cf655043SAndy Whitcroft				#print "APW: ALLOWED: block<$block>\n";
3794cf655043SAndy Whitcroft				$allowed = 1;
3795cf655043SAndy Whitcroft			}
3796cf655043SAndy Whitcroft			if (statement_block_size($block) > 1) {
3797cf655043SAndy Whitcroft				#print "APW: ALLOWED: lines block<$block>\n";
3798cf655043SAndy Whitcroft				$allowed = 1;
3799cf655043SAndy Whitcroft			}
3800cf655043SAndy Whitcroft			# Check the post-context.
3801cf655043SAndy Whitcroft			if (defined $chunks[1]) {
3802cf655043SAndy Whitcroft				my ($cond, $block) = @{$chunks[1]};
3803cf655043SAndy Whitcroft				if (defined $cond) {
3804773647a0SAndy Whitcroft					substr($block, 0, length($cond), '');
3805cf655043SAndy Whitcroft				}
3806cf655043SAndy Whitcroft				if ($block =~ /^\s*\{/) {
3807cf655043SAndy Whitcroft					#print "APW: ALLOWED: chunk-1 block<$block>\n";
3808cf655043SAndy Whitcroft					$allowed = 1;
3809cf655043SAndy Whitcroft				}
3810cf655043SAndy Whitcroft			}
3811cf655043SAndy Whitcroft			if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
381269932487SJustin P. Mattock				my $herectx = $here . "\n";
3813f055663cSAndy Whitcroft				my $cnt = statement_rawlines($block);
3814cf655043SAndy Whitcroft
3815f055663cSAndy Whitcroft				for (my $n = 0; $n < $cnt; $n++) {
381669932487SJustin P. Mattock					$herectx .= raw_line($linenr, $n) . "\n";
3817cf655043SAndy Whitcroft				}
3818cf655043SAndy Whitcroft
3819000d1cc1SJoe Perches				WARN("BRACES",
3820000d1cc1SJoe Perches				     "braces {} are not necessary for single statement blocks\n" . $herectx);
3821f0a594c1SAndy Whitcroft			}
3822f0a594c1SAndy Whitcroft		}
3823f0a594c1SAndy Whitcroft
38240979ae66SJoe Perches# check for unnecessary blank lines around braces
382577b9a53aSJoe Perches		if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
38260979ae66SJoe Perches			CHK("BRACES",
38270979ae66SJoe Perches			    "Blank lines aren't necessary before a close brace '}'\n" . $hereprev);
38280979ae66SJoe Perches		}
382977b9a53aSJoe Perches		if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
38300979ae66SJoe Perches			CHK("BRACES",
38310979ae66SJoe Perches			    "Blank lines aren't necessary after an open brace '{'\n" . $hereprev);
38320979ae66SJoe Perches		}
38330979ae66SJoe Perches
38344a0df2efSAndy Whitcroft# no volatiles please
38356c72ffaaSAndy Whitcroft		my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
38366c72ffaaSAndy Whitcroft		if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
3837000d1cc1SJoe Perches			WARN("VOLATILE",
3838000d1cc1SJoe Perches			     "Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
38394a0df2efSAndy Whitcroft		}
38404a0df2efSAndy Whitcroft
384100df344fSAndy Whitcroft# warn about #if 0
3842c45dcabdSAndy Whitcroft		if ($line =~ /^.\s*\#\s*if\s+0\b/) {
3843000d1cc1SJoe Perches			CHK("REDUNDANT_CODE",
3844000d1cc1SJoe Perches			    "if this code is redundant consider removing it\n" .
3845de7d4f0eSAndy Whitcroft				$herecurr);
38464a0df2efSAndy Whitcroft		}
38474a0df2efSAndy Whitcroft
384803df4b51SAndy Whitcroft# check for needless "if (<foo>) fn(<foo>)" uses
384903df4b51SAndy Whitcroft		if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
385003df4b51SAndy Whitcroft			my $expr = '\s*\(\s*' . quotemeta($1) . '\s*\)\s*;';
385103df4b51SAndy Whitcroft			if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?)$expr/) {
385203df4b51SAndy Whitcroft				WARN('NEEDLESS_IF',
385303df4b51SAndy Whitcroft				     "$1(NULL) is safe this check is probably not required\n" . $hereprev);
38544c432a8fSGreg Kroah-Hartman			}
38554c432a8fSGreg Kroah-Hartman		}
3856f0a594c1SAndy Whitcroft
38578716de38SJoe Perches# check for bad placement of section $InitAttribute (e.g.: __initdata)
38588716de38SJoe Perches		if ($line =~ /(\b$InitAttribute\b)/) {
38598716de38SJoe Perches			my $attr = $1;
38608716de38SJoe Perches			if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
38618716de38SJoe Perches				my $ptr = $1;
38628716de38SJoe Perches				my $var = $2;
38638716de38SJoe Perches				if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
38648716de38SJoe Perches				      ERROR("MISPLACED_INIT",
38658716de38SJoe Perches					    "$attr should be placed after $var\n" . $herecurr)) ||
38668716de38SJoe Perches				     ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
38678716de38SJoe Perches				      WARN("MISPLACED_INIT",
38688716de38SJoe Perches					   "$attr should be placed after $var\n" . $herecurr))) &&
38698716de38SJoe Perches				    $fix) {
38708716de38SJoe Perches					$fixed[$linenr - 1] =~ 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;
38718716de38SJoe Perches				}
38728716de38SJoe Perches			}
38738716de38SJoe Perches		}
38748716de38SJoe Perches
3875e970b884SJoe Perches# check for $InitAttributeData (ie: __initdata) with const
3876e970b884SJoe Perches		if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
3877e970b884SJoe Perches			my $attr = $1;
3878e970b884SJoe Perches			$attr =~ /($InitAttributePrefix)(.*)/;
3879e970b884SJoe Perches			my $attr_prefix = $1;
3880e970b884SJoe Perches			my $attr_type = $2;
3881e970b884SJoe Perches			if (ERROR("INIT_ATTRIBUTE",
3882e970b884SJoe Perches				  "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
3883e970b884SJoe Perches			    $fix) {
3884e970b884SJoe Perches				$fixed[$linenr - 1] =~
3885e970b884SJoe Perches				    s/$InitAttributeData/${attr_prefix}initconst/;
3886e970b884SJoe Perches			}
3887e970b884SJoe Perches		}
3888e970b884SJoe Perches
3889e970b884SJoe Perches# check for $InitAttributeConst (ie: __initconst) without const
3890e970b884SJoe Perches		if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
3891e970b884SJoe Perches			my $attr = $1;
3892e970b884SJoe Perches			if (ERROR("INIT_ATTRIBUTE",
3893e970b884SJoe Perches				  "Use of $attr requires a separate use of const\n" . $herecurr) &&
3894e970b884SJoe Perches			    $fix) {
3895e970b884SJoe Perches				my $lead = $fixed[$linenr - 1] =~
3896e970b884SJoe Perches				    /(^\+\s*(?:static\s+))/;
3897e970b884SJoe Perches				$lead = rtrim($1);
3898e970b884SJoe Perches				$lead = "$lead " if ($lead !~ /^\+$/);
3899e970b884SJoe Perches				$lead = "${lead}const ";
3900e970b884SJoe Perches				$fixed[$linenr - 1] =~ s/(^\+\s*(?:static\s+))/$lead/;
3901e970b884SJoe Perches			}
3902e970b884SJoe Perches		}
3903e970b884SJoe Perches
39041a15a250SPatrick Pannuto# prefer usleep_range over udelay
390537581c28SBruce Allan		if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
39061a15a250SPatrick Pannuto			# ignore udelay's < 10, however
390737581c28SBruce Allan			if (! ($1 < 10) ) {
3908000d1cc1SJoe Perches				CHK("USLEEP_RANGE",
3909000d1cc1SJoe Perches				    "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $line);
39101a15a250SPatrick Pannuto			}
39111a15a250SPatrick Pannuto		}
39121a15a250SPatrick Pannuto
391309ef8725SPatrick Pannuto# warn about unexpectedly long msleep's
391409ef8725SPatrick Pannuto		if ($line =~ /\bmsleep\s*\((\d+)\);/) {
391509ef8725SPatrick Pannuto			if ($1 < 20) {
3916000d1cc1SJoe Perches				WARN("MSLEEP",
3917000d1cc1SJoe Perches				     "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $line);
391809ef8725SPatrick Pannuto			}
391909ef8725SPatrick Pannuto		}
392009ef8725SPatrick Pannuto
392136ec1939SJoe Perches# check for comparisons of jiffies
392236ec1939SJoe Perches		if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
392336ec1939SJoe Perches			WARN("JIFFIES_COMPARISON",
392436ec1939SJoe Perches			     "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
392536ec1939SJoe Perches		}
392636ec1939SJoe Perches
39279d7a34a5SJoe Perches# check for comparisons of get_jiffies_64()
39289d7a34a5SJoe Perches		if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
39299d7a34a5SJoe Perches			WARN("JIFFIES_COMPARISON",
39309d7a34a5SJoe Perches			     "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
39319d7a34a5SJoe Perches		}
39329d7a34a5SJoe Perches
393300df344fSAndy Whitcroft# warn about #ifdefs in C files
3934c45dcabdSAndy Whitcroft#		if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
393500df344fSAndy Whitcroft#			print "#ifdef in C files should be avoided\n";
393600df344fSAndy Whitcroft#			print "$herecurr";
393700df344fSAndy Whitcroft#			$clean = 0;
393800df344fSAndy Whitcroft#		}
393900df344fSAndy Whitcroft
394022f2a2efSAndy Whitcroft# warn about spacing in #ifdefs
3941c45dcabdSAndy Whitcroft		if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
39423705ce5bSJoe Perches			if (ERROR("SPACING",
39433705ce5bSJoe Perches				  "exactly one space required after that #$1\n" . $herecurr) &&
39443705ce5bSJoe Perches			    $fix) {
39453705ce5bSJoe Perches				$fixed[$linenr - 1] =~
39463705ce5bSJoe Perches				    s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
39473705ce5bSJoe Perches			}
39483705ce5bSJoe Perches
394922f2a2efSAndy Whitcroft		}
395022f2a2efSAndy Whitcroft
39514a0df2efSAndy Whitcroft# check for spinlock_t definitions without a comment.
3952171ae1a4SAndy Whitcroft		if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
3953171ae1a4SAndy Whitcroft		    $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
39544a0df2efSAndy Whitcroft			my $which = $1;
39554a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
3956000d1cc1SJoe Perches				CHK("UNCOMMENTED_DEFINITION",
3957000d1cc1SJoe Perches				    "$1 definition without comment\n" . $herecurr);
39584a0df2efSAndy Whitcroft			}
39594a0df2efSAndy Whitcroft		}
39604a0df2efSAndy Whitcroft# check for memory barriers without a comment.
39614a0df2efSAndy Whitcroft		if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
39624a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
3963c1fd7bb9SJoe Perches				WARN("MEMORY_BARRIER",
3964000d1cc1SJoe Perches				     "memory barrier without comment\n" . $herecurr);
39654a0df2efSAndy Whitcroft			}
39664a0df2efSAndy Whitcroft		}
39674a0df2efSAndy Whitcroft# check of hardware specific defines
3968c45dcabdSAndy Whitcroft		if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
3969000d1cc1SJoe Perches			CHK("ARCH_DEFINES",
3970000d1cc1SJoe Perches			    "architecture specific defines should be avoided\n" .  $herecurr);
39710a920b5bSAndy Whitcroft		}
3972653d4876SAndy Whitcroft
3973d4977c78STobias Klauser# Check that the storage class is at the beginning of a declaration
3974d4977c78STobias Klauser		if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
3975000d1cc1SJoe Perches			WARN("STORAGE_CLASS",
3976000d1cc1SJoe Perches			     "storage class should be at the beginning of the declaration\n" . $herecurr)
3977d4977c78STobias Klauser		}
3978d4977c78STobias Klauser
3979de7d4f0eSAndy Whitcroft# check the location of the inline attribute, that it is between
3980de7d4f0eSAndy Whitcroft# storage class and type.
39819c0ca6f9SAndy Whitcroft		if ($line =~ /\b$Type\s+$Inline\b/ ||
39829c0ca6f9SAndy Whitcroft		    $line =~ /\b$Inline\s+$Storage\b/) {
3983000d1cc1SJoe Perches			ERROR("INLINE_LOCATION",
3984000d1cc1SJoe Perches			      "inline keyword should sit between storage class and type\n" . $herecurr);
3985de7d4f0eSAndy Whitcroft		}
3986de7d4f0eSAndy Whitcroft
39878905a67cSAndy Whitcroft# Check for __inline__ and __inline, prefer inline
39882b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
39892b7ab453SJoe Perches		    $line =~ /\b(__inline__|__inline)\b/) {
3990d5e616fcSJoe Perches			if (WARN("INLINE",
3991d5e616fcSJoe Perches				 "plain inline is preferred over $1\n" . $herecurr) &&
3992d5e616fcSJoe Perches			    $fix) {
3993d5e616fcSJoe Perches				$fixed[$linenr - 1] =~ s/\b(__inline__|__inline)\b/inline/;
3994d5e616fcSJoe Perches
3995d5e616fcSJoe Perches			}
39968905a67cSAndy Whitcroft		}
39978905a67cSAndy Whitcroft
39983d130fd0SJoe Perches# Check for __attribute__ packed, prefer __packed
39992b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
40002b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
4001000d1cc1SJoe Perches			WARN("PREFER_PACKED",
4002000d1cc1SJoe Perches			     "__packed is preferred over __attribute__((packed))\n" . $herecurr);
40033d130fd0SJoe Perches		}
40043d130fd0SJoe Perches
400539b7e287SJoe Perches# Check for __attribute__ aligned, prefer __aligned
40062b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
40072b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
4008000d1cc1SJoe Perches			WARN("PREFER_ALIGNED",
4009000d1cc1SJoe Perches			     "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
401039b7e287SJoe Perches		}
401139b7e287SJoe Perches
40125f14d3bdSJoe Perches# Check for __attribute__ format(printf, prefer __printf
40132b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
40142b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
4015d5e616fcSJoe Perches			if (WARN("PREFER_PRINTF",
4016d5e616fcSJoe Perches				 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) &&
4017d5e616fcSJoe Perches			    $fix) {
4018d5e616fcSJoe Perches				$fixed[$linenr - 1] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf\s*,\s*(.*)\)\s*\)\s*\)/"__printf(" . trim($1) . ")"/ex;
4019d5e616fcSJoe Perches
4020d5e616fcSJoe Perches			}
40215f14d3bdSJoe Perches		}
40225f14d3bdSJoe Perches
40236061d949SJoe Perches# Check for __attribute__ format(scanf, prefer __scanf
40242b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
40252b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
4026d5e616fcSJoe Perches			if (WARN("PREFER_SCANF",
4027d5e616fcSJoe Perches				 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) &&
4028d5e616fcSJoe Perches			    $fix) {
4029d5e616fcSJoe Perches				$fixed[$linenr - 1] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\s*,\s*(.*)\)\s*\)\s*\)/"__scanf(" . trim($1) . ")"/ex;
4030d5e616fcSJoe Perches			}
40316061d949SJoe Perches		}
40326061d949SJoe Perches
40338f53a9b8SJoe Perches# check for sizeof(&)
40348f53a9b8SJoe Perches		if ($line =~ /\bsizeof\s*\(\s*\&/) {
4035000d1cc1SJoe Perches			WARN("SIZEOF_ADDRESS",
4036000d1cc1SJoe Perches			     "sizeof(& should be avoided\n" . $herecurr);
40378f53a9b8SJoe Perches		}
40388f53a9b8SJoe Perches
403966c80b60SJoe Perches# check for sizeof without parenthesis
404066c80b60SJoe Perches		if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
4041d5e616fcSJoe Perches			if (WARN("SIZEOF_PARENTHESIS",
4042d5e616fcSJoe Perches				 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
4043d5e616fcSJoe Perches			    $fix) {
4044d5e616fcSJoe Perches				$fixed[$linenr - 1] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
4045d5e616fcSJoe Perches			}
404666c80b60SJoe Perches		}
404766c80b60SJoe Perches
4048428e2fdcSJoe Perches# check for line continuations in quoted strings with odd counts of "
4049428e2fdcSJoe Perches		if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) {
4050000d1cc1SJoe Perches			WARN("LINE_CONTINUATIONS",
4051000d1cc1SJoe Perches			     "Avoid line continuations in quoted strings\n" . $herecurr);
4052428e2fdcSJoe Perches		}
4053428e2fdcSJoe Perches
405488982feaSJoe Perches# check for struct spinlock declarations
405588982feaSJoe Perches		if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
405688982feaSJoe Perches			WARN("USE_SPINLOCK_T",
405788982feaSJoe Perches			     "struct spinlock should be spinlock_t\n" . $herecurr);
405888982feaSJoe Perches		}
405988982feaSJoe Perches
4060a6962d72SJoe Perches# check for seq_printf uses that could be seq_puts
406106668727SJoe Perches		if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
4062a6962d72SJoe Perches			my $fmt = get_quoted_string($line, $rawline);
406306668727SJoe Perches			if ($fmt ne "" && $fmt !~ /[^\\]\%/) {
4064d5e616fcSJoe Perches				if (WARN("PREFER_SEQ_PUTS",
4065d5e616fcSJoe Perches					 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
4066d5e616fcSJoe Perches				    $fix) {
4067d5e616fcSJoe Perches					$fixed[$linenr - 1] =~ s/\bseq_printf\b/seq_puts/;
4068d5e616fcSJoe Perches				}
4069a6962d72SJoe Perches			}
4070a6962d72SJoe Perches		}
4071a6962d72SJoe Perches
4072554e165cSAndy Whitcroft# Check for misused memsets
4073d1fe9c09SJoe Perches		if ($^V && $^V ge 5.10.0 &&
4074d1fe9c09SJoe Perches		    defined $stat &&
4075d7c76ba7SJoe Perches		    $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/s) {
4076554e165cSAndy Whitcroft
4077d7c76ba7SJoe Perches			my $ms_addr = $2;
4078d1fe9c09SJoe Perches			my $ms_val = $7;
4079d1fe9c09SJoe Perches			my $ms_size = $12;
4080d7c76ba7SJoe Perches
4081554e165cSAndy Whitcroft			if ($ms_size =~ /^(0x|)0$/i) {
4082554e165cSAndy Whitcroft				ERROR("MEMSET",
4083d7c76ba7SJoe Perches				      "memset to 0's uses 0 as the 2nd argument, not the 3rd\n" . "$here\n$stat\n");
4084554e165cSAndy Whitcroft			} elsif ($ms_size =~ /^(0x|)1$/i) {
4085554e165cSAndy Whitcroft				WARN("MEMSET",
4086d7c76ba7SJoe Perches				     "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
4087d7c76ba7SJoe Perches			}
4088d7c76ba7SJoe Perches		}
4089d7c76ba7SJoe Perches
4090d7c76ba7SJoe Perches# typecasts on min/max could be min_t/max_t
4091d1fe9c09SJoe Perches		if ($^V && $^V ge 5.10.0 &&
4092d1fe9c09SJoe Perches		    defined $stat &&
4093d7c76ba7SJoe Perches		    $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
4094d1fe9c09SJoe Perches			if (defined $2 || defined $7) {
4095d7c76ba7SJoe Perches				my $call = $1;
4096d7c76ba7SJoe Perches				my $cast1 = deparenthesize($2);
4097d7c76ba7SJoe Perches				my $arg1 = $3;
4098d1fe9c09SJoe Perches				my $cast2 = deparenthesize($7);
4099d1fe9c09SJoe Perches				my $arg2 = $8;
4100d7c76ba7SJoe Perches				my $cast;
4101d7c76ba7SJoe Perches
4102d1fe9c09SJoe Perches				if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
4103d7c76ba7SJoe Perches					$cast = "$cast1 or $cast2";
4104d7c76ba7SJoe Perches				} elsif ($cast1 ne "") {
4105d7c76ba7SJoe Perches					$cast = $cast1;
4106d7c76ba7SJoe Perches				} else {
4107d7c76ba7SJoe Perches					$cast = $cast2;
4108d7c76ba7SJoe Perches				}
4109d7c76ba7SJoe Perches				WARN("MINMAX",
4110d7c76ba7SJoe Perches				     "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
4111554e165cSAndy Whitcroft			}
4112554e165cSAndy Whitcroft		}
4113554e165cSAndy Whitcroft
41144a273195SJoe Perches# check usleep_range arguments
41154a273195SJoe Perches		if ($^V && $^V ge 5.10.0 &&
41164a273195SJoe Perches		    defined $stat &&
41174a273195SJoe Perches		    $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
41184a273195SJoe Perches			my $min = $1;
41194a273195SJoe Perches			my $max = $7;
41204a273195SJoe Perches			if ($min eq $max) {
41214a273195SJoe Perches				WARN("USLEEP_RANGE",
41224a273195SJoe Perches				     "usleep_range should not use min == max args; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
41234a273195SJoe Perches			} elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
41244a273195SJoe Perches				 $min > $max) {
41254a273195SJoe Perches				WARN("USLEEP_RANGE",
41264a273195SJoe Perches				     "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
41274a273195SJoe Perches			}
41284a273195SJoe Perches		}
41294a273195SJoe Perches
4130823b794cSJoe Perches# check for naked sscanf
4131823b794cSJoe Perches		if ($^V && $^V ge 5.10.0 &&
4132823b794cSJoe Perches		    defined $stat &&
4133823b794cSJoe Perches		    $stat =~ /\bsscanf\b/ &&
4134823b794cSJoe Perches		    ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
4135823b794cSJoe Perches		     $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ &&
4136823b794cSJoe Perches		     $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) {
4137823b794cSJoe Perches			my $lc = $stat =~ tr@\n@@;
4138823b794cSJoe Perches			$lc = $lc + $linenr;
4139823b794cSJoe Perches			my $stat_real = raw_line($linenr, 0);
4140823b794cSJoe Perches		        for (my $count = $linenr + 1; $count <= $lc; $count++) {
4141823b794cSJoe Perches				$stat_real = $stat_real . "\n" . raw_line($count, 0);
4142823b794cSJoe Perches			}
4143823b794cSJoe Perches			WARN("NAKED_SSCANF",
4144823b794cSJoe Perches			     "unchecked sscanf return value\n" . "$here\n$stat_real\n");
4145823b794cSJoe Perches		}
4146823b794cSJoe Perches
414770dc8a48SJoe Perches# check for new externs in .h files.
414870dc8a48SJoe Perches		if ($realfile =~ /\.h$/ &&
414970dc8a48SJoe Perches		    $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
4150d1d85780SJoe Perches			if (CHK("AVOID_EXTERNS",
415170dc8a48SJoe Perches				"extern prototypes should be avoided in .h files\n" . $herecurr) &&
415270dc8a48SJoe Perches			    $fix) {
415370dc8a48SJoe Perches				$fixed[$linenr - 1] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
415470dc8a48SJoe Perches			}
415570dc8a48SJoe Perches		}
415670dc8a48SJoe Perches
4157de7d4f0eSAndy Whitcroft# check for new externs in .c files.
4158171ae1a4SAndy Whitcroft		if ($realfile =~ /\.c$/ && defined $stat &&
4159c45dcabdSAndy Whitcroft		    $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
4160171ae1a4SAndy Whitcroft		{
4161c45dcabdSAndy Whitcroft			my $function_name = $1;
4162c45dcabdSAndy Whitcroft			my $paren_space = $2;
4163171ae1a4SAndy Whitcroft
4164171ae1a4SAndy Whitcroft			my $s = $stat;
4165171ae1a4SAndy Whitcroft			if (defined $cond) {
4166171ae1a4SAndy Whitcroft				substr($s, 0, length($cond), '');
4167171ae1a4SAndy Whitcroft			}
4168c45dcabdSAndy Whitcroft			if ($s =~ /^\s*;/ &&
4169c45dcabdSAndy Whitcroft			    $function_name ne 'uninitialized_var')
4170c45dcabdSAndy Whitcroft			{
4171000d1cc1SJoe Perches				WARN("AVOID_EXTERNS",
4172000d1cc1SJoe Perches				     "externs should be avoided in .c files\n" .  $herecurr);
4173de7d4f0eSAndy Whitcroft			}
4174de7d4f0eSAndy Whitcroft
4175171ae1a4SAndy Whitcroft			if ($paren_space =~ /\n/) {
4176000d1cc1SJoe Perches				WARN("FUNCTION_ARGUMENTS",
4177000d1cc1SJoe Perches				     "arguments for function declarations should follow identifier\n" . $herecurr);
4178171ae1a4SAndy Whitcroft			}
41799c9ba34eSAndy Whitcroft
41809c9ba34eSAndy Whitcroft		} elsif ($realfile =~ /\.c$/ && defined $stat &&
41819c9ba34eSAndy Whitcroft		    $stat =~ /^.\s*extern\s+/)
41829c9ba34eSAndy Whitcroft		{
4183000d1cc1SJoe Perches			WARN("AVOID_EXTERNS",
4184000d1cc1SJoe Perches			     "externs should be avoided in .c files\n" .  $herecurr);
4185171ae1a4SAndy Whitcroft		}
4186171ae1a4SAndy Whitcroft
4187de7d4f0eSAndy Whitcroft# checks for new __setup's
4188de7d4f0eSAndy Whitcroft		if ($rawline =~ /\b__setup\("([^"]*)"/) {
4189de7d4f0eSAndy Whitcroft			my $name = $1;
4190de7d4f0eSAndy Whitcroft
4191de7d4f0eSAndy Whitcroft			if (!grep(/$name/, @setup_docs)) {
4192000d1cc1SJoe Perches				CHK("UNDOCUMENTED_SETUP",
4193000d1cc1SJoe Perches				    "__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
4194de7d4f0eSAndy Whitcroft			}
4195653d4876SAndy Whitcroft		}
41969c0ca6f9SAndy Whitcroft
41979c0ca6f9SAndy Whitcroft# check for pointless casting of kmalloc return
4198caf2a54fSJoe Perches		if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) {
4199000d1cc1SJoe Perches			WARN("UNNECESSARY_CASTS",
4200000d1cc1SJoe Perches			     "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
42019c0ca6f9SAndy Whitcroft		}
420213214adfSAndy Whitcroft
4203a640d25cSJoe Perches# alloc style
4204a640d25cSJoe Perches# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
4205a640d25cSJoe Perches		if ($^V && $^V ge 5.10.0 &&
4206a640d25cSJoe Perches		    $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*([kv][mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
4207a640d25cSJoe Perches			CHK("ALLOC_SIZEOF_STRUCT",
4208a640d25cSJoe Perches			    "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
4209a640d25cSJoe Perches		}
4210a640d25cSJoe Perches
4211972fdea2SJoe Perches# check for krealloc arg reuse
4212972fdea2SJoe Perches		if ($^V && $^V ge 5.10.0 &&
4213972fdea2SJoe Perches		    $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*\1\s*,/) {
4214972fdea2SJoe Perches			WARN("KREALLOC_ARG_REUSE",
4215972fdea2SJoe Perches			     "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
4216972fdea2SJoe Perches		}
4217972fdea2SJoe Perches
42185ce59ae0SJoe Perches# check for alloc argument mismatch
42195ce59ae0SJoe Perches		if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
42205ce59ae0SJoe Perches			WARN("ALLOC_ARRAY_ARGS",
42215ce59ae0SJoe Perches			     "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
42225ce59ae0SJoe Perches		}
42235ce59ae0SJoe Perches
42247e4915e7SDavid Rientjes# check for GFP_NOWAIT use
42257e4915e7SDavid Rientjes		if ($line =~ /\b__GFP_NOFAIL\b/) {
42267e4915e7SDavid Rientjes			WARN("__GFP_NOFAIL",
42277e4915e7SDavid Rientjes			     "Use of __GFP_NOFAIL is deprecated, no new users should be added\n" . $herecurr);
42287e4915e7SDavid Rientjes		}
42297e4915e7SDavid Rientjes
4230caf2a54fSJoe Perches# check for multiple semicolons
4231caf2a54fSJoe Perches		if ($line =~ /;\s*;\s*$/) {
4232d5e616fcSJoe Perches			if (WARN("ONE_SEMICOLON",
4233d5e616fcSJoe Perches				 "Statements terminations use 1 semicolon\n" . $herecurr) &&
4234d5e616fcSJoe Perches			    $fix) {
4235d5e616fcSJoe Perches				$fixed[$linenr - 1] =~ s/(\s*;\s*){2,}$/;/g;
4236d5e616fcSJoe Perches			}
4237d1e2ad07SJoe Perches		}
4238d1e2ad07SJoe Perches
4239c34c09a8SJoe Perches# check for case / default statements not preceeded by break/fallthrough/switch
4240c34c09a8SJoe Perches		if ($line =~ /^.\s*(?:case\s+(?:$Ident|$Constant)\s*|default):/) {
4241c34c09a8SJoe Perches			my $has_break = 0;
4242c34c09a8SJoe Perches			my $has_statement = 0;
4243c34c09a8SJoe Perches			my $count = 0;
4244c34c09a8SJoe Perches			my $prevline = $linenr;
4245c34c09a8SJoe Perches			while ($prevline > 1 && $count < 3 && !$has_break) {
4246c34c09a8SJoe Perches				$prevline--;
4247c34c09a8SJoe Perches				my $rline = $rawlines[$prevline - 1];
4248c34c09a8SJoe Perches				my $fline = $lines[$prevline - 1];
4249c34c09a8SJoe Perches				last if ($fline =~ /^\@\@/);
4250c34c09a8SJoe Perches				next if ($fline =~ /^\-/);
4251c34c09a8SJoe Perches				next if ($fline =~ /^.(?:\s*(?:case\s+(?:$Ident|$Constant)[\s$;]*|default):[\s$;]*)*$/);
4252c34c09a8SJoe Perches				$has_break = 1 if ($rline =~ /fall[\s_-]*(through|thru)/i);
4253c34c09a8SJoe Perches				next if ($fline =~ /^.[\s$;]*$/);
4254c34c09a8SJoe Perches				$has_statement = 1;
4255c34c09a8SJoe Perches				$count++;
4256c34c09a8SJoe Perches				$has_break = 1 if ($fline =~ /\bswitch\b|\b(?:break\s*;[\s$;]*$|return\b|goto\b|continue\b)/);
4257c34c09a8SJoe Perches			}
4258c34c09a8SJoe Perches			if (!$has_break && $has_statement) {
4259c34c09a8SJoe Perches				WARN("MISSING_BREAK",
4260c34c09a8SJoe Perches				     "Possible switch case/default not preceeded by break or fallthrough comment\n" . $herecurr);
4261c34c09a8SJoe Perches			}
4262c34c09a8SJoe Perches		}
4263c34c09a8SJoe Perches
4264d1e2ad07SJoe Perches# check for switch/default statements without a break;
4265d1e2ad07SJoe Perches		if ($^V && $^V ge 5.10.0 &&
4266d1e2ad07SJoe Perches		    defined $stat &&
4267d1e2ad07SJoe Perches		    $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
4268d1e2ad07SJoe Perches			my $ctx = '';
4269d1e2ad07SJoe Perches			my $herectx = $here . "\n";
4270d1e2ad07SJoe Perches			my $cnt = statement_rawlines($stat);
4271d1e2ad07SJoe Perches			for (my $n = 0; $n < $cnt; $n++) {
4272d1e2ad07SJoe Perches				$herectx .= raw_line($linenr, $n) . "\n";
4273d1e2ad07SJoe Perches			}
4274d1e2ad07SJoe Perches			WARN("DEFAULT_NO_BREAK",
4275d1e2ad07SJoe Perches			     "switch default: should use break\n" . $herectx);
4276caf2a54fSJoe Perches		}
4277caf2a54fSJoe Perches
427813214adfSAndy Whitcroft# check for gcc specific __FUNCTION__
4279d5e616fcSJoe Perches		if ($line =~ /\b__FUNCTION__\b/) {
4280d5e616fcSJoe Perches			if (WARN("USE_FUNC",
4281d5e616fcSJoe Perches				 "__func__ should be used instead of gcc specific __FUNCTION__\n"  . $herecurr) &&
4282d5e616fcSJoe Perches			    $fix) {
4283d5e616fcSJoe Perches				$fixed[$linenr - 1] =~ s/\b__FUNCTION__\b/__func__/g;
4284d5e616fcSJoe Perches			}
428513214adfSAndy Whitcroft		}
4286773647a0SAndy Whitcroft
42872c92488aSJoe Perches# check for use of yield()
42882c92488aSJoe Perches		if ($line =~ /\byield\s*\(\s*\)/) {
42892c92488aSJoe Perches			WARN("YIELD",
42902c92488aSJoe Perches			     "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n"  . $herecurr);
42912c92488aSJoe Perches		}
42922c92488aSJoe Perches
4293179f8f40SJoe Perches# check for comparisons against true and false
4294179f8f40SJoe Perches		if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
4295179f8f40SJoe Perches			my $lead = $1;
4296179f8f40SJoe Perches			my $arg = $2;
4297179f8f40SJoe Perches			my $test = $3;
4298179f8f40SJoe Perches			my $otype = $4;
4299179f8f40SJoe Perches			my $trail = $5;
4300179f8f40SJoe Perches			my $op = "!";
4301179f8f40SJoe Perches
4302179f8f40SJoe Perches			($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
4303179f8f40SJoe Perches
4304179f8f40SJoe Perches			my $type = lc($otype);
4305179f8f40SJoe Perches			if ($type =~ /^(?:true|false)$/) {
4306179f8f40SJoe Perches				if (("$test" eq "==" && "$type" eq "true") ||
4307179f8f40SJoe Perches				    ("$test" eq "!=" && "$type" eq "false")) {
4308179f8f40SJoe Perches					$op = "";
4309179f8f40SJoe Perches				}
4310179f8f40SJoe Perches
4311179f8f40SJoe Perches				CHK("BOOL_COMPARISON",
4312179f8f40SJoe Perches				    "Using comparison to $otype is error prone\n" . $herecurr);
4313179f8f40SJoe Perches
4314179f8f40SJoe Perches## maybe suggesting a correct construct would better
4315179f8f40SJoe Perches##				    "Using comparison to $otype is error prone.  Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
4316179f8f40SJoe Perches
4317179f8f40SJoe Perches			}
4318179f8f40SJoe Perches		}
4319179f8f40SJoe Perches
43204882720bSThomas Gleixner# check for semaphores initialized locked
43214882720bSThomas Gleixner		if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
4322000d1cc1SJoe Perches			WARN("CONSIDER_COMPLETION",
4323000d1cc1SJoe Perches			     "consider using a completion\n" . $herecurr);
4324773647a0SAndy Whitcroft		}
43256712d858SJoe Perches
432667d0a075SJoe Perches# recommend kstrto* over simple_strto* and strict_strto*
432767d0a075SJoe Perches		if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
4328000d1cc1SJoe Perches			WARN("CONSIDER_KSTRTO",
432967d0a075SJoe Perches			     "$1 is obsolete, use k$3 instead\n" . $herecurr);
4330773647a0SAndy Whitcroft		}
43316712d858SJoe Perches
4332f3db6639SMichael Ellerman# check for __initcall(), use device_initcall() explicitly please
4333f3db6639SMichael Ellerman		if ($line =~ /^.\s*__initcall\s*\(/) {
4334000d1cc1SJoe Perches			WARN("USE_DEVICE_INITCALL",
4335000d1cc1SJoe Perches			     "please use device_initcall() instead of __initcall()\n" . $herecurr);
4336f3db6639SMichael Ellerman		}
43376712d858SJoe Perches
433879404849SEmese Revfy# check for various ops structs, ensure they are const.
433979404849SEmese Revfy		my $struct_ops = qr{acpi_dock_ops|
434079404849SEmese Revfy				address_space_operations|
434179404849SEmese Revfy				backlight_ops|
434279404849SEmese Revfy				block_device_operations|
434379404849SEmese Revfy				dentry_operations|
434479404849SEmese Revfy				dev_pm_ops|
434579404849SEmese Revfy				dma_map_ops|
434679404849SEmese Revfy				extent_io_ops|
434779404849SEmese Revfy				file_lock_operations|
434879404849SEmese Revfy				file_operations|
434979404849SEmese Revfy				hv_ops|
435079404849SEmese Revfy				ide_dma_ops|
435179404849SEmese Revfy				intel_dvo_dev_ops|
435279404849SEmese Revfy				item_operations|
435379404849SEmese Revfy				iwl_ops|
435479404849SEmese Revfy				kgdb_arch|
435579404849SEmese Revfy				kgdb_io|
435679404849SEmese Revfy				kset_uevent_ops|
435779404849SEmese Revfy				lock_manager_operations|
435879404849SEmese Revfy				microcode_ops|
435979404849SEmese Revfy				mtrr_ops|
436079404849SEmese Revfy				neigh_ops|
436179404849SEmese Revfy				nlmsvc_binding|
436279404849SEmese Revfy				pci_raw_ops|
436379404849SEmese Revfy				pipe_buf_operations|
436479404849SEmese Revfy				platform_hibernation_ops|
436579404849SEmese Revfy				platform_suspend_ops|
436679404849SEmese Revfy				proto_ops|
436779404849SEmese Revfy				rpc_pipe_ops|
436879404849SEmese Revfy				seq_operations|
436979404849SEmese Revfy				snd_ac97_build_ops|
437079404849SEmese Revfy				soc_pcmcia_socket_ops|
437179404849SEmese Revfy				stacktrace_ops|
437279404849SEmese Revfy				sysfs_ops|
437379404849SEmese Revfy				tty_operations|
437479404849SEmese Revfy				usb_mon_operations|
437579404849SEmese Revfy				wd_ops}x;
43766903ffb2SAndy Whitcroft		if ($line !~ /\bconst\b/ &&
437779404849SEmese Revfy		    $line =~ /\bstruct\s+($struct_ops)\b/) {
4378000d1cc1SJoe Perches			WARN("CONST_STRUCT",
4379000d1cc1SJoe Perches			     "struct $1 should normally be const\n" .
43806903ffb2SAndy Whitcroft				$herecurr);
43812b6db5cbSAndy Whitcroft		}
4382773647a0SAndy Whitcroft
4383773647a0SAndy Whitcroft# use of NR_CPUS is usually wrong
4384773647a0SAndy Whitcroft# ignore definitions of NR_CPUS and usage to define arrays as likely right
4385773647a0SAndy Whitcroft		if ($line =~ /\bNR_CPUS\b/ &&
4386c45dcabdSAndy Whitcroft		    $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
4387c45dcabdSAndy Whitcroft		    $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
4388171ae1a4SAndy Whitcroft		    $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
4389171ae1a4SAndy Whitcroft		    $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
4390171ae1a4SAndy Whitcroft		    $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
4391773647a0SAndy Whitcroft		{
4392000d1cc1SJoe Perches			WARN("NR_CPUS",
4393000d1cc1SJoe Perches			     "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
4394773647a0SAndy Whitcroft		}
43959c9ba34eSAndy Whitcroft
439652ea8506SJoe Perches# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
439752ea8506SJoe Perches		if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
439852ea8506SJoe Perches			ERROR("DEFINE_ARCH_HAS",
439952ea8506SJoe Perches			      "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
440052ea8506SJoe Perches		}
440152ea8506SJoe Perches
44029c9ba34eSAndy Whitcroft# check for %L{u,d,i} in strings
44039c9ba34eSAndy Whitcroft		my $string;
44049c9ba34eSAndy Whitcroft		while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
44059c9ba34eSAndy Whitcroft			$string = substr($rawline, $-[1], $+[1] - $-[1]);
44062a1bc5d5SAndy Whitcroft			$string =~ s/%%/__/g;
44079c9ba34eSAndy Whitcroft			if ($string =~ /(?<!%)%L[udi]/) {
4408000d1cc1SJoe Perches				WARN("PRINTF_L",
4409000d1cc1SJoe Perches				     "\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
44109c9ba34eSAndy Whitcroft				last;
44119c9ba34eSAndy Whitcroft			}
44129c9ba34eSAndy Whitcroft		}
4413691d77b6SAndy Whitcroft
4414691d77b6SAndy Whitcroft# whine mightly about in_atomic
4415691d77b6SAndy Whitcroft		if ($line =~ /\bin_atomic\s*\(/) {
4416691d77b6SAndy Whitcroft			if ($realfile =~ m@^drivers/@) {
4417000d1cc1SJoe Perches				ERROR("IN_ATOMIC",
4418000d1cc1SJoe Perches				      "do not use in_atomic in drivers\n" . $herecurr);
4419f4a87736SAndy Whitcroft			} elsif ($realfile !~ m@^kernel/@) {
4420000d1cc1SJoe Perches				WARN("IN_ATOMIC",
4421000d1cc1SJoe Perches				     "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
4422691d77b6SAndy Whitcroft			}
4423691d77b6SAndy Whitcroft		}
44241704f47bSPeter Zijlstra
44251704f47bSPeter Zijlstra# check for lockdep_set_novalidate_class
44261704f47bSPeter Zijlstra		if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
44271704f47bSPeter Zijlstra		    $line =~ /__lockdep_no_validate__\s*\)/ ) {
44281704f47bSPeter Zijlstra			if ($realfile !~ m@^kernel/lockdep@ &&
44291704f47bSPeter Zijlstra			    $realfile !~ m@^include/linux/lockdep@ &&
44301704f47bSPeter Zijlstra			    $realfile !~ m@^drivers/base/core@) {
4431000d1cc1SJoe Perches				ERROR("LOCKDEP",
4432000d1cc1SJoe Perches				      "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
44331704f47bSPeter Zijlstra			}
44341704f47bSPeter Zijlstra		}
443588f8831cSDave Jones
443688f8831cSDave Jones		if ($line =~ /debugfs_create_file.*S_IWUGO/ ||
443788f8831cSDave Jones		    $line =~ /DEVICE_ATTR.*S_IWUGO/ ) {
4438000d1cc1SJoe Perches			WARN("EXPORTED_WORLD_WRITABLE",
4439000d1cc1SJoe Perches			     "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
444088f8831cSDave Jones		}
444113214adfSAndy Whitcroft	}
444213214adfSAndy Whitcroft
444313214adfSAndy Whitcroft	# If we have no input at all, then there is nothing to report on
444413214adfSAndy Whitcroft	# so just keep quiet.
444513214adfSAndy Whitcroft	if ($#rawlines == -1) {
444613214adfSAndy Whitcroft		exit(0);
44470a920b5bSAndy Whitcroft	}
44480a920b5bSAndy Whitcroft
44498905a67cSAndy Whitcroft	# In mailback mode only produce a report in the negative, for
44508905a67cSAndy Whitcroft	# things that appear to be patches.
44518905a67cSAndy Whitcroft	if ($mailback && ($clean == 1 || !$is_patch)) {
44528905a67cSAndy Whitcroft		exit(0);
44538905a67cSAndy Whitcroft	}
44548905a67cSAndy Whitcroft
44558905a67cSAndy Whitcroft	# This is not a patch, and we are are in 'no-patch' mode so
44568905a67cSAndy Whitcroft	# just keep quiet.
44578905a67cSAndy Whitcroft	if (!$chk_patch && !$is_patch) {
44588905a67cSAndy Whitcroft		exit(0);
44598905a67cSAndy Whitcroft	}
44608905a67cSAndy Whitcroft
44618905a67cSAndy Whitcroft	if (!$is_patch) {
4462000d1cc1SJoe Perches		ERROR("NOT_UNIFIED_DIFF",
4463000d1cc1SJoe Perches		      "Does not appear to be a unified-diff format patch\n");
44640a920b5bSAndy Whitcroft	}
44650a920b5bSAndy Whitcroft	if ($is_patch && $chk_signoff && $signoff == 0) {
4466000d1cc1SJoe Perches		ERROR("MISSING_SIGN_OFF",
4467000d1cc1SJoe Perches		      "Missing Signed-off-by: line(s)\n");
44680a920b5bSAndy Whitcroft	}
44690a920b5bSAndy Whitcroft
4470f0a594c1SAndy Whitcroft	print report_dump();
447113214adfSAndy Whitcroft	if ($summary && !($clean == 1 && $quiet == 1)) {
447213214adfSAndy Whitcroft		print "$filename " if ($summary_file);
44736c72ffaaSAndy Whitcroft		print "total: $cnt_error errors, $cnt_warn warnings, " .
44746c72ffaaSAndy Whitcroft			(($check)? "$cnt_chk checks, " : "") .
44756c72ffaaSAndy Whitcroft			"$cnt_lines lines checked\n";
44768905a67cSAndy Whitcroft		print "\n" if ($quiet == 0);
44776c72ffaaSAndy Whitcroft	}
44788905a67cSAndy Whitcroft
4479d2c0a235SAndy Whitcroft	if ($quiet == 0) {
4480d1fe9c09SJoe Perches
4481d1fe9c09SJoe Perches		if ($^V lt 5.10.0) {
4482d1fe9c09SJoe Perches			print("NOTE: perl $^V is not modern enough to detect all possible issues.\n");
4483d1fe9c09SJoe Perches			print("An upgrade to at least perl v5.10.0 is suggested.\n\n");
4484d1fe9c09SJoe Perches		}
4485d1fe9c09SJoe Perches
4486d2c0a235SAndy Whitcroft		# If there were whitespace errors which cleanpatch can fix
4487d2c0a235SAndy Whitcroft		# then suggest that.
4488d2c0a235SAndy Whitcroft		if ($rpt_cleaners) {
4489d2c0a235SAndy Whitcroft			print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n";
4490d2c0a235SAndy Whitcroft			print "      scripts/cleanfile\n\n";
4491b0781216SMike Frysinger			$rpt_cleaners = 0;
4492d2c0a235SAndy Whitcroft		}
4493d2c0a235SAndy Whitcroft	}
4494d2c0a235SAndy Whitcroft
449591bfe484SJoe Perches	hash_show_words(\%use_type, "Used");
449691bfe484SJoe Perches	hash_show_words(\%ignore_type, "Ignored");
4497000d1cc1SJoe Perches
44983705ce5bSJoe Perches	if ($clean == 0 && $fix && "@rawlines" ne "@fixed") {
44999624b8d6SJoe Perches		my $newfile = $filename;
45009624b8d6SJoe Perches		$newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace);
45013705ce5bSJoe Perches		my $linecount = 0;
45023705ce5bSJoe Perches		my $f;
45033705ce5bSJoe Perches
45043705ce5bSJoe Perches		open($f, '>', $newfile)
45053705ce5bSJoe Perches		    or die "$P: Can't open $newfile for write\n";
45063705ce5bSJoe Perches		foreach my $fixed_line (@fixed) {
45073705ce5bSJoe Perches			$linecount++;
45083705ce5bSJoe Perches			if ($file) {
45093705ce5bSJoe Perches				if ($linecount > 3) {
45103705ce5bSJoe Perches					$fixed_line =~ s/^\+//;
45113705ce5bSJoe Perches					print $f $fixed_line. "\n";
45123705ce5bSJoe Perches				}
45133705ce5bSJoe Perches			} else {
45143705ce5bSJoe Perches				print $f $fixed_line . "\n";
45153705ce5bSJoe Perches			}
45163705ce5bSJoe Perches		}
45173705ce5bSJoe Perches		close($f);
45183705ce5bSJoe Perches
45193705ce5bSJoe Perches		if (!$quiet) {
45203705ce5bSJoe Perches			print << "EOM";
45213705ce5bSJoe PerchesWrote EXPERIMENTAL --fix correction(s) to '$newfile'
45223705ce5bSJoe Perches
45233705ce5bSJoe PerchesDo _NOT_ trust the results written to this file.
45243705ce5bSJoe PerchesDo _NOT_ submit these changes without inspecting them for correctness.
45253705ce5bSJoe Perches
45263705ce5bSJoe PerchesThis EXPERIMENTAL file is simply a convenience to help rewrite patches.
45273705ce5bSJoe PerchesNo warranties, expressed or implied...
45283705ce5bSJoe Perches
45293705ce5bSJoe PerchesEOM
45303705ce5bSJoe Perches		}
45313705ce5bSJoe Perches	}
45323705ce5bSJoe Perches
45330a920b5bSAndy Whitcroft	if ($clean == 1 && $quiet == 0) {
4534c2fdda0dSAndy Whitcroft		print "$vname has no obvious style problems and is ready for submission.\n"
45350a920b5bSAndy Whitcroft	}
45360a920b5bSAndy Whitcroft	if ($clean == 0 && $quiet == 0) {
4537000d1cc1SJoe Perches		print << "EOM";
4538000d1cc1SJoe Perches$vname has style problems, please review.
4539000d1cc1SJoe Perches
4540000d1cc1SJoe PerchesIf any of these errors are false positives, please report
4541000d1cc1SJoe Perchesthem to the maintainer, see CHECKPATCH in MAINTAINERS.
4542000d1cc1SJoe PerchesEOM
45430a920b5bSAndy Whitcroft	}
454413214adfSAndy Whitcroft
45450a920b5bSAndy Whitcroft	return $clean;
45460a920b5bSAndy Whitcroft}
4547