xref: /linux-6.15/scripts/checkpatch.pl (revision 23f780c9)
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;
90a920b5bSAndy Whitcroft
100a920b5bSAndy Whitcroftmy $P = $0;
1100df344fSAndy Whitcroft$P =~ s@.*/@@g;
120a920b5bSAndy Whitcroft
13000d1cc1SJoe Perchesmy $V = '0.32';
140a920b5bSAndy Whitcroft
150a920b5bSAndy Whitcroftuse Getopt::Long qw(:config no_auto_abbrev);
160a920b5bSAndy Whitcroft
170a920b5bSAndy Whitcroftmy $quiet = 0;
180a920b5bSAndy Whitcroftmy $tree = 1;
190a920b5bSAndy Whitcroftmy $chk_signoff = 1;
200a920b5bSAndy Whitcroftmy $chk_patch = 1;
21773647a0SAndy Whitcroftmy $tst_only;
226c72ffaaSAndy Whitcroftmy $emacs = 0;
238905a67cSAndy Whitcroftmy $terse = 0;
246c72ffaaSAndy Whitcroftmy $file = 0;
256c72ffaaSAndy Whitcroftmy $check = 0;
268905a67cSAndy Whitcroftmy $summary = 1;
278905a67cSAndy Whitcroftmy $mailback = 0;
2813214adfSAndy Whitcroftmy $summary_file = 0;
29000d1cc1SJoe Perchesmy $show_types = 0;
306c72ffaaSAndy Whitcroftmy $root;
31c2fdda0dSAndy Whitcroftmy %debug;
32000d1cc1SJoe Perchesmy %ignore_type = ();
33000d1cc1SJoe Perchesmy @ignore = ();
3477f5b10aSHannes Edermy $help = 0;
35000d1cc1SJoe Perchesmy $configuration_file = ".checkpatch.conf";
366cd7f386SJoe Perchesmy $max_line_length = 80;
3777f5b10aSHannes Eder
3877f5b10aSHannes Edersub help {
3977f5b10aSHannes Eder	my ($exitcode) = @_;
4077f5b10aSHannes Eder
4177f5b10aSHannes Eder	print << "EOM";
4277f5b10aSHannes EderUsage: $P [OPTION]... [FILE]...
4377f5b10aSHannes EderVersion: $V
4477f5b10aSHannes Eder
4577f5b10aSHannes EderOptions:
4677f5b10aSHannes Eder  -q, --quiet                quiet
4777f5b10aSHannes Eder  --no-tree                  run without a kernel tree
4877f5b10aSHannes Eder  --no-signoff               do not check for 'Signed-off-by' line
4977f5b10aSHannes Eder  --patch                    treat FILE as patchfile (default)
5077f5b10aSHannes Eder  --emacs                    emacs compile window format
5177f5b10aSHannes Eder  --terse                    one line per report
5277f5b10aSHannes Eder  -f, --file                 treat FILE as regular source file
5377f5b10aSHannes Eder  --subjective, --strict     enable more subjective tests
54000d1cc1SJoe Perches  --ignore TYPE(,TYPE2...)   ignore various comma separated message types
556cd7f386SJoe Perches  --max-line-length=n        set the maximum line length, if exceeded, warn
56000d1cc1SJoe Perches  --show-types               show the message "types" in the output
5777f5b10aSHannes Eder  --root=PATH                PATH to the kernel tree root
5877f5b10aSHannes Eder  --no-summary               suppress the per-file summary
5977f5b10aSHannes Eder  --mailback                 only produce a report in case of warnings/errors
6077f5b10aSHannes Eder  --summary-file             include the filename in summary
6177f5b10aSHannes Eder  --debug KEY=[0|1]          turn on/off debugging of KEY, where KEY is one of
6277f5b10aSHannes Eder                             'values', 'possible', 'type', and 'attr' (default
6377f5b10aSHannes Eder                             is all off)
6477f5b10aSHannes Eder  --test-only=WORD           report only warnings/errors containing WORD
6577f5b10aSHannes Eder                             literally
6677f5b10aSHannes Eder  -h, --help, --version      display this help and exit
6777f5b10aSHannes Eder
6877f5b10aSHannes EderWhen FILE is - read standard input.
6977f5b10aSHannes EderEOM
7077f5b10aSHannes Eder
7177f5b10aSHannes Eder	exit($exitcode);
7277f5b10aSHannes Eder}
7377f5b10aSHannes Eder
74000d1cc1SJoe Perchesmy $conf = which_conf($configuration_file);
75000d1cc1SJoe Perchesif (-f $conf) {
76000d1cc1SJoe Perches	my @conf_args;
77000d1cc1SJoe Perches	open(my $conffile, '<', "$conf")
78000d1cc1SJoe Perches	    or warn "$P: Can't find a readable $configuration_file file $!\n";
79000d1cc1SJoe Perches
80000d1cc1SJoe Perches	while (<$conffile>) {
81000d1cc1SJoe Perches		my $line = $_;
82000d1cc1SJoe Perches
83000d1cc1SJoe Perches		$line =~ s/\s*\n?$//g;
84000d1cc1SJoe Perches		$line =~ s/^\s*//g;
85000d1cc1SJoe Perches		$line =~ s/\s+/ /g;
86000d1cc1SJoe Perches
87000d1cc1SJoe Perches		next if ($line =~ m/^\s*#/);
88000d1cc1SJoe Perches		next if ($line =~ m/^\s*$/);
89000d1cc1SJoe Perches
90000d1cc1SJoe Perches		my @words = split(" ", $line);
91000d1cc1SJoe Perches		foreach my $word (@words) {
92000d1cc1SJoe Perches			last if ($word =~ m/^#/);
93000d1cc1SJoe Perches			push (@conf_args, $word);
94000d1cc1SJoe Perches		}
95000d1cc1SJoe Perches	}
96000d1cc1SJoe Perches	close($conffile);
97000d1cc1SJoe Perches	unshift(@ARGV, @conf_args) if @conf_args;
98000d1cc1SJoe Perches}
99000d1cc1SJoe Perches
1000a920b5bSAndy WhitcroftGetOptions(
1016c72ffaaSAndy Whitcroft	'q|quiet+'	=> \$quiet,
1020a920b5bSAndy Whitcroft	'tree!'		=> \$tree,
1030a920b5bSAndy Whitcroft	'signoff!'	=> \$chk_signoff,
1040a920b5bSAndy Whitcroft	'patch!'	=> \$chk_patch,
1056c72ffaaSAndy Whitcroft	'emacs!'	=> \$emacs,
1068905a67cSAndy Whitcroft	'terse!'	=> \$terse,
10777f5b10aSHannes Eder	'f|file!'	=> \$file,
1086c72ffaaSAndy Whitcroft	'subjective!'	=> \$check,
1096c72ffaaSAndy Whitcroft	'strict!'	=> \$check,
110000d1cc1SJoe Perches	'ignore=s'	=> \@ignore,
111000d1cc1SJoe Perches	'show-types!'	=> \$show_types,
1126cd7f386SJoe Perches	'max-line-length=i' => \$max_line_length,
1136c72ffaaSAndy Whitcroft	'root=s'	=> \$root,
1148905a67cSAndy Whitcroft	'summary!'	=> \$summary,
1158905a67cSAndy Whitcroft	'mailback!'	=> \$mailback,
11613214adfSAndy Whitcroft	'summary-file!'	=> \$summary_file,
11713214adfSAndy Whitcroft
118c2fdda0dSAndy Whitcroft	'debug=s'	=> \%debug,
119773647a0SAndy Whitcroft	'test-only=s'	=> \$tst_only,
12077f5b10aSHannes Eder	'h|help'	=> \$help,
12177f5b10aSHannes Eder	'version'	=> \$help
12277f5b10aSHannes Eder) or help(1);
12377f5b10aSHannes Eder
12477f5b10aSHannes Ederhelp(0) if ($help);
1250a920b5bSAndy Whitcroft
1260a920b5bSAndy Whitcroftmy $exit = 0;
1270a920b5bSAndy Whitcroft
1280a920b5bSAndy Whitcroftif ($#ARGV < 0) {
12977f5b10aSHannes Eder	print "$P: no input files\n";
1300a920b5bSAndy Whitcroft	exit(1);
1310a920b5bSAndy Whitcroft}
1320a920b5bSAndy Whitcroft
133000d1cc1SJoe Perches@ignore = split(/,/, join(',',@ignore));
134000d1cc1SJoe Perchesforeach my $word (@ignore) {
135000d1cc1SJoe Perches	$word =~ s/\s*\n?$//g;
136000d1cc1SJoe Perches	$word =~ s/^\s*//g;
137000d1cc1SJoe Perches	$word =~ s/\s+/ /g;
138000d1cc1SJoe Perches	$word =~ tr/[a-z]/[A-Z]/;
139000d1cc1SJoe Perches
140000d1cc1SJoe Perches	next if ($word =~ m/^\s*#/);
141000d1cc1SJoe Perches	next if ($word =~ m/^\s*$/);
142000d1cc1SJoe Perches
143000d1cc1SJoe Perches	$ignore_type{$word}++;
144000d1cc1SJoe Perches}
145000d1cc1SJoe Perches
146c2fdda0dSAndy Whitcroftmy $dbg_values = 0;
147c2fdda0dSAndy Whitcroftmy $dbg_possible = 0;
1487429c690SAndy Whitcroftmy $dbg_type = 0;
149a1ef277eSAndy Whitcroftmy $dbg_attr = 0;
150c2fdda0dSAndy Whitcroftfor my $key (keys %debug) {
15121caa13cSAndy Whitcroft	## no critic
15221caa13cSAndy Whitcroft	eval "\${dbg_$key} = '$debug{$key}';";
15321caa13cSAndy Whitcroft	die "$@" if ($@);
154c2fdda0dSAndy Whitcroft}
155c2fdda0dSAndy Whitcroft
156d2c0a235SAndy Whitcroftmy $rpt_cleaners = 0;
157d2c0a235SAndy Whitcroft
1588905a67cSAndy Whitcroftif ($terse) {
1598905a67cSAndy Whitcroft	$emacs = 1;
1608905a67cSAndy Whitcroft	$quiet++;
1618905a67cSAndy Whitcroft}
1628905a67cSAndy Whitcroft
1636c72ffaaSAndy Whitcroftif ($tree) {
1646c72ffaaSAndy Whitcroft	if (defined $root) {
1656c72ffaaSAndy Whitcroft		if (!top_of_kernel_tree($root)) {
1666c72ffaaSAndy Whitcroft			die "$P: $root: --root does not point at a valid tree\n";
1676c72ffaaSAndy Whitcroft		}
1686c72ffaaSAndy Whitcroft	} else {
1696c72ffaaSAndy Whitcroft		if (top_of_kernel_tree('.')) {
1706c72ffaaSAndy Whitcroft			$root = '.';
1716c72ffaaSAndy Whitcroft		} elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
1726c72ffaaSAndy Whitcroft						top_of_kernel_tree($1)) {
1736c72ffaaSAndy Whitcroft			$root = $1;
1746c72ffaaSAndy Whitcroft		}
1756c72ffaaSAndy Whitcroft	}
1766c72ffaaSAndy Whitcroft
1776c72ffaaSAndy Whitcroft	if (!defined $root) {
1780a920b5bSAndy Whitcroft		print "Must be run from the top-level dir. of a kernel tree\n";
1790a920b5bSAndy Whitcroft		exit(2);
1800a920b5bSAndy Whitcroft	}
1816c72ffaaSAndy Whitcroft}
1826c72ffaaSAndy Whitcroft
1836c72ffaaSAndy Whitcroftmy $emitted_corrupt = 0;
1846c72ffaaSAndy Whitcroft
1852ceb532bSAndy Whitcroftour $Ident	= qr{
1862ceb532bSAndy Whitcroft			[A-Za-z_][A-Za-z\d_]*
1872ceb532bSAndy Whitcroft			(?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
1882ceb532bSAndy Whitcroft		}x;
1896c72ffaaSAndy Whitcroftour $Storage	= qr{extern|static|asmlinkage};
1906c72ffaaSAndy Whitcroftour $Sparse	= qr{
1916c72ffaaSAndy Whitcroft			__user|
1926c72ffaaSAndy Whitcroft			__kernel|
1936c72ffaaSAndy Whitcroft			__force|
1946c72ffaaSAndy Whitcroft			__iomem|
1956c72ffaaSAndy Whitcroft			__must_check|
1966c72ffaaSAndy Whitcroft			__init_refok|
197417495edSAndy Whitcroft			__kprobes|
198165e72a6SSven Eckelmann			__ref|
199165e72a6SSven Eckelmann			__rcu
2006c72ffaaSAndy Whitcroft		}x;
20152131292SWolfram Sang
20252131292SWolfram Sang# Notes to $Attribute:
20352131292SWolfram Sang# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
2046c72ffaaSAndy Whitcroftour $Attribute	= qr{
2056c72ffaaSAndy Whitcroft			const|
20603f1df7dSJoe Perches			__percpu|
20703f1df7dSJoe Perches			__nocast|
20803f1df7dSJoe Perches			__safe|
20903f1df7dSJoe Perches			__bitwise__|
21003f1df7dSJoe Perches			__packed__|
21103f1df7dSJoe Perches			__packed2__|
21203f1df7dSJoe Perches			__naked|
21303f1df7dSJoe Perches			__maybe_unused|
21403f1df7dSJoe Perches			__always_unused|
21503f1df7dSJoe Perches			__noreturn|
21603f1df7dSJoe Perches			__used|
21703f1df7dSJoe Perches			__cold|
21803f1df7dSJoe Perches			__noclone|
21903f1df7dSJoe Perches			__deprecated|
2206c72ffaaSAndy Whitcroft			__read_mostly|
2216c72ffaaSAndy Whitcroft			__kprobes|
22252131292SWolfram Sang			__(?:mem|cpu|dev|)(?:initdata|initconst|init\b)|
22324e1d81aSAndy Whitcroft			____cacheline_aligned|
22424e1d81aSAndy Whitcroft			____cacheline_aligned_in_smp|
2255fe3af11SAndy Whitcroft			____cacheline_internodealigned_in_smp|
2265fe3af11SAndy Whitcroft			__weak
2276c72ffaaSAndy Whitcroft		  }x;
228c45dcabdSAndy Whitcroftour $Modifier;
2296c72ffaaSAndy Whitcroftour $Inline	= qr{inline|__always_inline|noinline};
2306c72ffaaSAndy Whitcroftour $Member	= qr{->$Ident|\.$Ident|\[[^]]*\]};
2316c72ffaaSAndy Whitcroftour $Lval	= qr{$Ident(?:$Member)*};
2326c72ffaaSAndy Whitcroft
23395e2c602SJoe Perchesour $Int_type	= qr{(?i)llu|ull|ll|lu|ul|l|u};
23495e2c602SJoe Perchesour $Binary	= qr{(?i)0b[01]+$Int_type?};
23595e2c602SJoe Perchesour $Hex	= qr{(?i)0x[0-9a-f]+$Int_type?};
23695e2c602SJoe Perchesour $Int	= qr{[0-9]+$Int_type?};
237326b1ffcSJoe Perchesour $Float_hex	= qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?};
238326b1ffcSJoe Perchesour $Float_dec	= qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?};
239326b1ffcSJoe Perchesour $Float_int	= qr{(?i)[0-9]+e-?[0-9]+[fl]?};
24074349bccSJoe Perchesour $Float	= qr{$Float_hex|$Float_dec|$Float_int};
24195e2c602SJoe Perchesour $Constant	= qr{$Float|$Binary|$Hex|$Int};
242326b1ffcSJoe Perchesour $Assignment	= qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=};
24386f9d059SAndy Whitcroftour $Compare    = qr{<=|>=|==|!=|<|>};
244*23f780c9SJoe Perchesour $Arithmetic = qr{\+|-|\*|\/|%};
2456c72ffaaSAndy Whitcroftour $Operators	= qr{
2466c72ffaaSAndy Whitcroft			<=|>=|==|!=|
2476c72ffaaSAndy Whitcroft			=>|->|<<|>>|<|>|!|~|
248*23f780c9SJoe Perches			&&|\|\||,|\^|\+\+|--|&|\||$Arithmetic
2496c72ffaaSAndy Whitcroft		  }x;
2506c72ffaaSAndy Whitcroft
2518905a67cSAndy Whitcroftour $NonptrType;
2528905a67cSAndy Whitcroftour $Type;
2538905a67cSAndy Whitcroftour $Declare;
2548905a67cSAndy Whitcroft
25515662b3eSJoe Perchesour $NON_ASCII_UTF8	= qr{
25615662b3eSJoe Perches	[\xC2-\xDF][\x80-\xBF]               # non-overlong 2-byte
257171ae1a4SAndy Whitcroft	|  \xE0[\xA0-\xBF][\x80-\xBF]        # excluding overlongs
258171ae1a4SAndy Whitcroft	| [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}  # straight 3-byte
259171ae1a4SAndy Whitcroft	|  \xED[\x80-\x9F][\x80-\xBF]        # excluding surrogates
260171ae1a4SAndy Whitcroft	|  \xF0[\x90-\xBF][\x80-\xBF]{2}     # planes 1-3
261171ae1a4SAndy Whitcroft	| [\xF1-\xF3][\x80-\xBF]{3}          # planes 4-15
262171ae1a4SAndy Whitcroft	|  \xF4[\x80-\x8F][\x80-\xBF]{2}     # plane 16
263171ae1a4SAndy Whitcroft}x;
264171ae1a4SAndy Whitcroft
26515662b3eSJoe Perchesour $UTF8	= qr{
26615662b3eSJoe Perches	[\x09\x0A\x0D\x20-\x7E]              # ASCII
26715662b3eSJoe Perches	| $NON_ASCII_UTF8
26815662b3eSJoe Perches}x;
26915662b3eSJoe Perches
2708ed22cadSAndy Whitcroftour $typeTypedefs = qr{(?x:
271fb9e9096SAndy Whitcroft	(?:__)?(?:u|s|be|le)(?:8|16|32|64)|
2728ed22cadSAndy Whitcroft	atomic_t
2738ed22cadSAndy Whitcroft)};
2748ed22cadSAndy Whitcroft
275691e669bSJoe Perchesour $logFunctions = qr{(?x:
2766e60c02eSJoe Perches	printk(?:_ratelimited|_once|)|
2776e60c02eSJoe Perches	[a-z0-9]+_(?:printk|emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)(?:_ratelimited|_once|)|
2786e60c02eSJoe Perches	WARN(?:_RATELIMIT|_ONCE|)|
279b0531722SJoe Perches	panic|
280b0531722SJoe Perches	MODULE_[A-Z_]+
281691e669bSJoe Perches)};
282691e669bSJoe Perches
28320112475SJoe Perchesour $signature_tags = qr{(?xi:
28420112475SJoe Perches	Signed-off-by:|
28520112475SJoe Perches	Acked-by:|
28620112475SJoe Perches	Tested-by:|
28720112475SJoe Perches	Reviewed-by:|
28820112475SJoe Perches	Reported-by:|
2898543ae12SMugunthan V N	Suggested-by:|
29020112475SJoe Perches	To:|
29120112475SJoe Perches	Cc:
29220112475SJoe Perches)};
29320112475SJoe Perches
2948905a67cSAndy Whitcroftour @typeList = (
2958905a67cSAndy Whitcroft	qr{void},
296c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?char},
297c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?short},
298c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?int},
299c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?long},
300c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?long\s+int},
301c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?long\s+long},
302c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?long\s+long\s+int},
3038905a67cSAndy Whitcroft	qr{unsigned},
3048905a67cSAndy Whitcroft	qr{float},
3058905a67cSAndy Whitcroft	qr{double},
3068905a67cSAndy Whitcroft	qr{bool},
3078905a67cSAndy Whitcroft	qr{struct\s+$Ident},
3088905a67cSAndy Whitcroft	qr{union\s+$Ident},
3098905a67cSAndy Whitcroft	qr{enum\s+$Ident},
3108905a67cSAndy Whitcroft	qr{${Ident}_t},
3118905a67cSAndy Whitcroft	qr{${Ident}_handler},
3128905a67cSAndy Whitcroft	qr{${Ident}_handler_fn},
3138905a67cSAndy Whitcroft);
314c45dcabdSAndy Whitcroftour @modifierList = (
315c45dcabdSAndy Whitcroft	qr{fastcall},
316c45dcabdSAndy Whitcroft);
3178905a67cSAndy Whitcroft
3187840a94cSWolfram Sangour $allowed_asm_includes = qr{(?x:
3197840a94cSWolfram Sang	irq|
3207840a94cSWolfram Sang	memory
3217840a94cSWolfram Sang)};
3227840a94cSWolfram Sang# memory.h: ARM has a custom one
3237840a94cSWolfram Sang
3248905a67cSAndy Whitcroftsub build_types {
325d2172eb5SAndy Whitcroft	my $mods = "(?x:  \n" . join("|\n  ", @modifierList) . "\n)";
326d2172eb5SAndy Whitcroft	my $all = "(?x:  \n" . join("|\n  ", @typeList) . "\n)";
327c8cb2ca3SAndy Whitcroft	$Modifier	= qr{(?:$Attribute|$Sparse|$mods)};
3288905a67cSAndy Whitcroft	$NonptrType	= qr{
329d2172eb5SAndy Whitcroft			(?:$Modifier\s+|const\s+)*
330cf655043SAndy Whitcroft			(?:
3316b48db24SAndy Whitcroft				(?:typeof|__typeof__)\s*\([^\)]*\)|
3328ed22cadSAndy Whitcroft				(?:$typeTypedefs\b)|
333c45dcabdSAndy Whitcroft				(?:${all}\b)
334cf655043SAndy Whitcroft			)
335c8cb2ca3SAndy Whitcroft			(?:\s+$Modifier|\s+const)*
3368905a67cSAndy Whitcroft		  }x;
3378905a67cSAndy Whitcroft	$Type	= qr{
338c45dcabdSAndy Whitcroft			$NonptrType
339b337d8b8SAndy Whitcroft			(?:(?:\s|\*|\[\])+\s*const|(?:\s|\*|\[\])+|(?:\s*\[\s*\])+)?
340c8cb2ca3SAndy Whitcroft			(?:\s+$Inline|\s+$Modifier)*
3418905a67cSAndy Whitcroft		  }x;
3428905a67cSAndy Whitcroft	$Declare	= qr{(?:$Storage\s+)?$Type};
3438905a67cSAndy Whitcroft}
3448905a67cSAndy Whitcroftbuild_types();
3456c72ffaaSAndy Whitcroft
3467d2367afSJoe Perches
3477d2367afSJoe Perchesour $Typecast	= qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
348d1fe9c09SJoe Perches
349d1fe9c09SJoe Perches# Using $balanced_parens, $LvalOrFunc, or $FuncArg
350d1fe9c09SJoe Perches# requires at least perl version v5.10.0
351d1fe9c09SJoe Perches# Any use must be runtime checked with $^V
352d1fe9c09SJoe Perches
353d1fe9c09SJoe Perchesour $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
354d1fe9c09SJoe Perchesour $LvalOrFunc	= qr{($Lval)\s*($balanced_parens{0,1})\s*};
355d7c76ba7SJoe Perchesour $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant)};
3567d2367afSJoe Perches
3577d2367afSJoe Perchessub deparenthesize {
3587d2367afSJoe Perches	my ($string) = @_;
3597d2367afSJoe Perches	return "" if (!defined($string));
3607d2367afSJoe Perches	$string =~ s@^\s*\(\s*@@g;
3617d2367afSJoe Perches	$string =~ s@\s*\)\s*$@@g;
3627d2367afSJoe Perches	$string =~ s@\s+@ @g;
3637d2367afSJoe Perches	return $string;
3647d2367afSJoe Perches}
3657d2367afSJoe Perches
3666c72ffaaSAndy Whitcroft$chk_signoff = 0 if ($file);
3670a920b5bSAndy Whitcroft
36800df344fSAndy Whitcroftmy @rawlines = ();
369c2fdda0dSAndy Whitcroftmy @lines = ();
370c2fdda0dSAndy Whitcroftmy $vname;
3716c72ffaaSAndy Whitcroftfor my $filename (@ARGV) {
37221caa13cSAndy Whitcroft	my $FILE;
3736c72ffaaSAndy Whitcroft	if ($file) {
37421caa13cSAndy Whitcroft		open($FILE, '-|', "diff -u /dev/null $filename") ||
3756c72ffaaSAndy Whitcroft			die "$P: $filename: diff failed - $!\n";
37621caa13cSAndy Whitcroft	} elsif ($filename eq '-') {
37721caa13cSAndy Whitcroft		open($FILE, '<&STDIN');
3786c72ffaaSAndy Whitcroft	} else {
37921caa13cSAndy Whitcroft		open($FILE, '<', "$filename") ||
3806c72ffaaSAndy Whitcroft			die "$P: $filename: open failed - $!\n";
3816c72ffaaSAndy Whitcroft	}
382c2fdda0dSAndy Whitcroft	if ($filename eq '-') {
383c2fdda0dSAndy Whitcroft		$vname = 'Your patch';
384c2fdda0dSAndy Whitcroft	} else {
385c2fdda0dSAndy Whitcroft		$vname = $filename;
386c2fdda0dSAndy Whitcroft	}
38721caa13cSAndy Whitcroft	while (<$FILE>) {
3880a920b5bSAndy Whitcroft		chomp;
38900df344fSAndy Whitcroft		push(@rawlines, $_);
3906c72ffaaSAndy Whitcroft	}
39121caa13cSAndy Whitcroft	close($FILE);
392c2fdda0dSAndy Whitcroft	if (!process($filename)) {
3930a920b5bSAndy Whitcroft		$exit = 1;
3940a920b5bSAndy Whitcroft	}
39500df344fSAndy Whitcroft	@rawlines = ();
39613214adfSAndy Whitcroft	@lines = ();
3970a920b5bSAndy Whitcroft}
3980a920b5bSAndy Whitcroft
3990a920b5bSAndy Whitcroftexit($exit);
4000a920b5bSAndy Whitcroft
4010a920b5bSAndy Whitcroftsub top_of_kernel_tree {
4026c72ffaaSAndy Whitcroft	my ($root) = @_;
4036c72ffaaSAndy Whitcroft
4046c72ffaaSAndy Whitcroft	my @tree_check = (
4056c72ffaaSAndy Whitcroft		"COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
4066c72ffaaSAndy Whitcroft		"README", "Documentation", "arch", "include", "drivers",
4076c72ffaaSAndy Whitcroft		"fs", "init", "ipc", "kernel", "lib", "scripts",
4086c72ffaaSAndy Whitcroft	);
4096c72ffaaSAndy Whitcroft
4106c72ffaaSAndy Whitcroft	foreach my $check (@tree_check) {
4116c72ffaaSAndy Whitcroft		if (! -e $root . '/' . $check) {
4120a920b5bSAndy Whitcroft			return 0;
4130a920b5bSAndy Whitcroft		}
4146c72ffaaSAndy Whitcroft	}
4156c72ffaaSAndy Whitcroft	return 1;
4166c72ffaaSAndy Whitcroft}
4170a920b5bSAndy Whitcroft
41820112475SJoe Perchessub parse_email {
41920112475SJoe Perches	my ($formatted_email) = @_;
42020112475SJoe Perches
42120112475SJoe Perches	my $name = "";
42220112475SJoe Perches	my $address = "";
42320112475SJoe Perches	my $comment = "";
42420112475SJoe Perches
42520112475SJoe Perches	if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
42620112475SJoe Perches		$name = $1;
42720112475SJoe Perches		$address = $2;
42820112475SJoe Perches		$comment = $3 if defined $3;
42920112475SJoe Perches	} elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
43020112475SJoe Perches		$address = $1;
43120112475SJoe Perches		$comment = $2 if defined $2;
43220112475SJoe Perches	} elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
43320112475SJoe Perches		$address = $1;
43420112475SJoe Perches		$comment = $2 if defined $2;
43520112475SJoe Perches		$formatted_email =~ s/$address.*$//;
43620112475SJoe Perches		$name = $formatted_email;
43720112475SJoe Perches		$name =~ s/^\s+|\s+$//g;
43820112475SJoe Perches		$name =~ s/^\"|\"$//g;
43920112475SJoe Perches		# If there's a name left after stripping spaces and
44020112475SJoe Perches		# leading quotes, and the address doesn't have both
44120112475SJoe Perches		# leading and trailing angle brackets, the address
44220112475SJoe Perches		# is invalid. ie:
44320112475SJoe Perches		#   "joe smith [email protected]" bad
44420112475SJoe Perches		#   "joe smith <[email protected]" bad
44520112475SJoe Perches		if ($name ne "" && $address !~ /^<[^>]+>$/) {
44620112475SJoe Perches			$name = "";
44720112475SJoe Perches			$address = "";
44820112475SJoe Perches			$comment = "";
44920112475SJoe Perches		}
45020112475SJoe Perches	}
45120112475SJoe Perches
45220112475SJoe Perches	$name =~ s/^\s+|\s+$//g;
45320112475SJoe Perches	$name =~ s/^\"|\"$//g;
45420112475SJoe Perches	$address =~ s/^\s+|\s+$//g;
45520112475SJoe Perches	$address =~ s/^\<|\>$//g;
45620112475SJoe Perches
45720112475SJoe Perches	if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
45820112475SJoe Perches		$name =~ s/(?<!\\)"/\\"/g; ##escape quotes
45920112475SJoe Perches		$name = "\"$name\"";
46020112475SJoe Perches	}
46120112475SJoe Perches
46220112475SJoe Perches	return ($name, $address, $comment);
46320112475SJoe Perches}
46420112475SJoe Perches
46520112475SJoe Perchessub format_email {
46620112475SJoe Perches	my ($name, $address) = @_;
46720112475SJoe Perches
46820112475SJoe Perches	my $formatted_email;
46920112475SJoe Perches
47020112475SJoe Perches	$name =~ s/^\s+|\s+$//g;
47120112475SJoe Perches	$name =~ s/^\"|\"$//g;
47220112475SJoe Perches	$address =~ s/^\s+|\s+$//g;
47320112475SJoe Perches
47420112475SJoe Perches	if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
47520112475SJoe Perches		$name =~ s/(?<!\\)"/\\"/g; ##escape quotes
47620112475SJoe Perches		$name = "\"$name\"";
47720112475SJoe Perches	}
47820112475SJoe Perches
47920112475SJoe Perches	if ("$name" eq "") {
48020112475SJoe Perches		$formatted_email = "$address";
48120112475SJoe Perches	} else {
48220112475SJoe Perches		$formatted_email = "$name <$address>";
48320112475SJoe Perches	}
48420112475SJoe Perches
48520112475SJoe Perches	return $formatted_email;
48620112475SJoe Perches}
48720112475SJoe Perches
488000d1cc1SJoe Perchessub which_conf {
489000d1cc1SJoe Perches	my ($conf) = @_;
490000d1cc1SJoe Perches
491000d1cc1SJoe Perches	foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
492000d1cc1SJoe Perches		if (-e "$path/$conf") {
493000d1cc1SJoe Perches			return "$path/$conf";
494000d1cc1SJoe Perches		}
495000d1cc1SJoe Perches	}
496000d1cc1SJoe Perches
497000d1cc1SJoe Perches	return "";
498000d1cc1SJoe Perches}
499000d1cc1SJoe Perches
5000a920b5bSAndy Whitcroftsub expand_tabs {
5010a920b5bSAndy Whitcroft	my ($str) = @_;
5020a920b5bSAndy Whitcroft
5030a920b5bSAndy Whitcroft	my $res = '';
5040a920b5bSAndy Whitcroft	my $n = 0;
5050a920b5bSAndy Whitcroft	for my $c (split(//, $str)) {
5060a920b5bSAndy Whitcroft		if ($c eq "\t") {
5070a920b5bSAndy Whitcroft			$res .= ' ';
5080a920b5bSAndy Whitcroft			$n++;
5090a920b5bSAndy Whitcroft			for (; ($n % 8) != 0; $n++) {
5100a920b5bSAndy Whitcroft				$res .= ' ';
5110a920b5bSAndy Whitcroft			}
5120a920b5bSAndy Whitcroft			next;
5130a920b5bSAndy Whitcroft		}
5140a920b5bSAndy Whitcroft		$res .= $c;
5150a920b5bSAndy Whitcroft		$n++;
5160a920b5bSAndy Whitcroft	}
5170a920b5bSAndy Whitcroft
5180a920b5bSAndy Whitcroft	return $res;
5190a920b5bSAndy Whitcroft}
5206c72ffaaSAndy Whitcroftsub copy_spacing {
521773647a0SAndy Whitcroft	(my $res = shift) =~ tr/\t/ /c;
5226c72ffaaSAndy Whitcroft	return $res;
5236c72ffaaSAndy Whitcroft}
5240a920b5bSAndy Whitcroft
5254a0df2efSAndy Whitcroftsub line_stats {
5264a0df2efSAndy Whitcroft	my ($line) = @_;
5274a0df2efSAndy Whitcroft
5284a0df2efSAndy Whitcroft	# Drop the diff line leader and expand tabs
5294a0df2efSAndy Whitcroft	$line =~ s/^.//;
5304a0df2efSAndy Whitcroft	$line = expand_tabs($line);
5314a0df2efSAndy Whitcroft
5324a0df2efSAndy Whitcroft	# Pick the indent from the front of the line.
5334a0df2efSAndy Whitcroft	my ($white) = ($line =~ /^(\s*)/);
5344a0df2efSAndy Whitcroft
5354a0df2efSAndy Whitcroft	return (length($line), length($white));
5364a0df2efSAndy Whitcroft}
5374a0df2efSAndy Whitcroft
538773647a0SAndy Whitcroftmy $sanitise_quote = '';
539773647a0SAndy Whitcroft
540773647a0SAndy Whitcroftsub sanitise_line_reset {
541773647a0SAndy Whitcroft	my ($in_comment) = @_;
542773647a0SAndy Whitcroft
543773647a0SAndy Whitcroft	if ($in_comment) {
544773647a0SAndy Whitcroft		$sanitise_quote = '*/';
545773647a0SAndy Whitcroft	} else {
546773647a0SAndy Whitcroft		$sanitise_quote = '';
547773647a0SAndy Whitcroft	}
548773647a0SAndy Whitcroft}
54900df344fSAndy Whitcroftsub sanitise_line {
55000df344fSAndy Whitcroft	my ($line) = @_;
55100df344fSAndy Whitcroft
55200df344fSAndy Whitcroft	my $res = '';
55300df344fSAndy Whitcroft	my $l = '';
55400df344fSAndy Whitcroft
555c2fdda0dSAndy Whitcroft	my $qlen = 0;
556773647a0SAndy Whitcroft	my $off = 0;
557773647a0SAndy Whitcroft	my $c;
55800df344fSAndy Whitcroft
559773647a0SAndy Whitcroft	# Always copy over the diff marker.
560773647a0SAndy Whitcroft	$res = substr($line, 0, 1);
561773647a0SAndy Whitcroft
562773647a0SAndy Whitcroft	for ($off = 1; $off < length($line); $off++) {
563773647a0SAndy Whitcroft		$c = substr($line, $off, 1);
564773647a0SAndy Whitcroft
565773647a0SAndy Whitcroft		# Comments we are wacking completly including the begin
566773647a0SAndy Whitcroft		# and end, all to $;.
567773647a0SAndy Whitcroft		if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
568773647a0SAndy Whitcroft			$sanitise_quote = '*/';
569773647a0SAndy Whitcroft
570773647a0SAndy Whitcroft			substr($res, $off, 2, "$;$;");
571773647a0SAndy Whitcroft			$off++;
57200df344fSAndy Whitcroft			next;
573773647a0SAndy Whitcroft		}
57481bc0e02SAndy Whitcroft		if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
575773647a0SAndy Whitcroft			$sanitise_quote = '';
576773647a0SAndy Whitcroft			substr($res, $off, 2, "$;$;");
577773647a0SAndy Whitcroft			$off++;
578773647a0SAndy Whitcroft			next;
579773647a0SAndy Whitcroft		}
580113f04a8SDaniel Walker		if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
581113f04a8SDaniel Walker			$sanitise_quote = '//';
582113f04a8SDaniel Walker
583113f04a8SDaniel Walker			substr($res, $off, 2, $sanitise_quote);
584113f04a8SDaniel Walker			$off++;
585113f04a8SDaniel Walker			next;
586113f04a8SDaniel Walker		}
587773647a0SAndy Whitcroft
588773647a0SAndy Whitcroft		# A \ in a string means ignore the next character.
589773647a0SAndy Whitcroft		if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
590773647a0SAndy Whitcroft		    $c eq "\\") {
591773647a0SAndy Whitcroft			substr($res, $off, 2, 'XX');
592773647a0SAndy Whitcroft			$off++;
593773647a0SAndy Whitcroft			next;
594773647a0SAndy Whitcroft		}
595773647a0SAndy Whitcroft		# Regular quotes.
596773647a0SAndy Whitcroft		if ($c eq "'" || $c eq '"') {
597773647a0SAndy Whitcroft			if ($sanitise_quote eq '') {
598773647a0SAndy Whitcroft				$sanitise_quote = $c;
599773647a0SAndy Whitcroft
600773647a0SAndy Whitcroft				substr($res, $off, 1, $c);
601773647a0SAndy Whitcroft				next;
602773647a0SAndy Whitcroft			} elsif ($sanitise_quote eq $c) {
603773647a0SAndy Whitcroft				$sanitise_quote = '';
60400df344fSAndy Whitcroft			}
60500df344fSAndy Whitcroft		}
606773647a0SAndy Whitcroft
607fae17daeSAndy Whitcroft		#print "c<$c> SQ<$sanitise_quote>\n";
608773647a0SAndy Whitcroft		if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
609773647a0SAndy Whitcroft			substr($res, $off, 1, $;);
610113f04a8SDaniel Walker		} elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
611113f04a8SDaniel Walker			substr($res, $off, 1, $;);
612773647a0SAndy Whitcroft		} elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
613773647a0SAndy Whitcroft			substr($res, $off, 1, 'X');
61400df344fSAndy Whitcroft		} else {
615773647a0SAndy Whitcroft			substr($res, $off, 1, $c);
61600df344fSAndy Whitcroft		}
617c2fdda0dSAndy Whitcroft	}
618c2fdda0dSAndy Whitcroft
619113f04a8SDaniel Walker	if ($sanitise_quote eq '//') {
620113f04a8SDaniel Walker		$sanitise_quote = '';
621113f04a8SDaniel Walker	}
622113f04a8SDaniel Walker
623c2fdda0dSAndy Whitcroft	# The pathname on a #include may be surrounded by '<' and '>'.
624c45dcabdSAndy Whitcroft	if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
625c2fdda0dSAndy Whitcroft		my $clean = 'X' x length($1);
626c2fdda0dSAndy Whitcroft		$res =~ s@\<.*\>@<$clean>@;
627c2fdda0dSAndy Whitcroft
628c2fdda0dSAndy Whitcroft	# The whole of a #error is a string.
629c45dcabdSAndy Whitcroft	} elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
630c2fdda0dSAndy Whitcroft		my $clean = 'X' x length($1);
631c45dcabdSAndy Whitcroft		$res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
632c2fdda0dSAndy Whitcroft	}
633c2fdda0dSAndy Whitcroft
63400df344fSAndy Whitcroft	return $res;
63500df344fSAndy Whitcroft}
63600df344fSAndy Whitcroft
637a6962d72SJoe Perchessub get_quoted_string {
638a6962d72SJoe Perches	my ($line, $rawline) = @_;
639a6962d72SJoe Perches
640a6962d72SJoe Perches	return "" if ($line !~ m/(\"[X]+\")/g);
641a6962d72SJoe Perches	return substr($rawline, $-[0], $+[0] - $-[0]);
642a6962d72SJoe Perches}
643a6962d72SJoe Perches
6448905a67cSAndy Whitcroftsub ctx_statement_block {
6458905a67cSAndy Whitcroft	my ($linenr, $remain, $off) = @_;
6468905a67cSAndy Whitcroft	my $line = $linenr - 1;
6478905a67cSAndy Whitcroft	my $blk = '';
6488905a67cSAndy Whitcroft	my $soff = $off;
6498905a67cSAndy Whitcroft	my $coff = $off - 1;
650773647a0SAndy Whitcroft	my $coff_set = 0;
6518905a67cSAndy Whitcroft
65213214adfSAndy Whitcroft	my $loff = 0;
65313214adfSAndy Whitcroft
6548905a67cSAndy Whitcroft	my $type = '';
6558905a67cSAndy Whitcroft	my $level = 0;
656a2750645SAndy Whitcroft	my @stack = ();
657cf655043SAndy Whitcroft	my $p;
6588905a67cSAndy Whitcroft	my $c;
6598905a67cSAndy Whitcroft	my $len = 0;
66013214adfSAndy Whitcroft
66113214adfSAndy Whitcroft	my $remainder;
6628905a67cSAndy Whitcroft	while (1) {
663a2750645SAndy Whitcroft		@stack = (['', 0]) if ($#stack == -1);
664a2750645SAndy Whitcroft
665773647a0SAndy Whitcroft		#warn "CSB: blk<$blk> remain<$remain>\n";
6668905a67cSAndy Whitcroft		# If we are about to drop off the end, pull in more
6678905a67cSAndy Whitcroft		# context.
6688905a67cSAndy Whitcroft		if ($off >= $len) {
6698905a67cSAndy Whitcroft			for (; $remain > 0; $line++) {
670dea33496SAndy Whitcroft				last if (!defined $lines[$line]);
671c2fdda0dSAndy Whitcroft				next if ($lines[$line] =~ /^-/);
6728905a67cSAndy Whitcroft				$remain--;
67313214adfSAndy Whitcroft				$loff = $len;
674c2fdda0dSAndy Whitcroft				$blk .= $lines[$line] . "\n";
6758905a67cSAndy Whitcroft				$len = length($blk);
6768905a67cSAndy Whitcroft				$line++;
6778905a67cSAndy Whitcroft				last;
6788905a67cSAndy Whitcroft			}
6798905a67cSAndy Whitcroft			# Bail if there is no further context.
6808905a67cSAndy Whitcroft			#warn "CSB: blk<$blk> off<$off> len<$len>\n";
68113214adfSAndy Whitcroft			if ($off >= $len) {
6828905a67cSAndy Whitcroft				last;
6838905a67cSAndy Whitcroft			}
684f74bd194SAndy Whitcroft			if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
685f74bd194SAndy Whitcroft				$level++;
686f74bd194SAndy Whitcroft				$type = '#';
687f74bd194SAndy Whitcroft			}
6888905a67cSAndy Whitcroft		}
689cf655043SAndy Whitcroft		$p = $c;
6908905a67cSAndy Whitcroft		$c = substr($blk, $off, 1);
69113214adfSAndy Whitcroft		$remainder = substr($blk, $off);
6928905a67cSAndy Whitcroft
693773647a0SAndy Whitcroft		#warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
6944635f4fbSAndy Whitcroft
6954635f4fbSAndy Whitcroft		# Handle nested #if/#else.
6964635f4fbSAndy Whitcroft		if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
6974635f4fbSAndy Whitcroft			push(@stack, [ $type, $level ]);
6984635f4fbSAndy Whitcroft		} elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
6994635f4fbSAndy Whitcroft			($type, $level) = @{$stack[$#stack - 1]};
7004635f4fbSAndy Whitcroft		} elsif ($remainder =~ /^#\s*endif\b/) {
7014635f4fbSAndy Whitcroft			($type, $level) = @{pop(@stack)};
7024635f4fbSAndy Whitcroft		}
7034635f4fbSAndy Whitcroft
7048905a67cSAndy Whitcroft		# Statement ends at the ';' or a close '}' at the
7058905a67cSAndy Whitcroft		# outermost level.
7068905a67cSAndy Whitcroft		if ($level == 0 && $c eq ';') {
7078905a67cSAndy Whitcroft			last;
7088905a67cSAndy Whitcroft		}
7098905a67cSAndy Whitcroft
71013214adfSAndy Whitcroft		# An else is really a conditional as long as its not else if
711773647a0SAndy Whitcroft		if ($level == 0 && $coff_set == 0 &&
712773647a0SAndy Whitcroft				(!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
713773647a0SAndy Whitcroft				$remainder =~ /^(else)(?:\s|{)/ &&
714773647a0SAndy Whitcroft				$remainder !~ /^else\s+if\b/) {
715773647a0SAndy Whitcroft			$coff = $off + length($1) - 1;
716773647a0SAndy Whitcroft			$coff_set = 1;
717773647a0SAndy Whitcroft			#warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
718773647a0SAndy Whitcroft			#warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
71913214adfSAndy Whitcroft		}
72013214adfSAndy Whitcroft
7218905a67cSAndy Whitcroft		if (($type eq '' || $type eq '(') && $c eq '(') {
7228905a67cSAndy Whitcroft			$level++;
7238905a67cSAndy Whitcroft			$type = '(';
7248905a67cSAndy Whitcroft		}
7258905a67cSAndy Whitcroft		if ($type eq '(' && $c eq ')') {
7268905a67cSAndy Whitcroft			$level--;
7278905a67cSAndy Whitcroft			$type = ($level != 0)? '(' : '';
7288905a67cSAndy Whitcroft
7298905a67cSAndy Whitcroft			if ($level == 0 && $coff < $soff) {
7308905a67cSAndy Whitcroft				$coff = $off;
731773647a0SAndy Whitcroft				$coff_set = 1;
732773647a0SAndy Whitcroft				#warn "CSB: mark coff<$coff>\n";
7338905a67cSAndy Whitcroft			}
7348905a67cSAndy Whitcroft		}
7358905a67cSAndy Whitcroft		if (($type eq '' || $type eq '{') && $c eq '{') {
7368905a67cSAndy Whitcroft			$level++;
7378905a67cSAndy Whitcroft			$type = '{';
7388905a67cSAndy Whitcroft		}
7398905a67cSAndy Whitcroft		if ($type eq '{' && $c eq '}') {
7408905a67cSAndy Whitcroft			$level--;
7418905a67cSAndy Whitcroft			$type = ($level != 0)? '{' : '';
7428905a67cSAndy Whitcroft
7438905a67cSAndy Whitcroft			if ($level == 0) {
744b998e001SPatrick Pannuto				if (substr($blk, $off + 1, 1) eq ';') {
745b998e001SPatrick Pannuto					$off++;
746b998e001SPatrick Pannuto				}
7478905a67cSAndy Whitcroft				last;
7488905a67cSAndy Whitcroft			}
7498905a67cSAndy Whitcroft		}
750f74bd194SAndy Whitcroft		# Preprocessor commands end at the newline unless escaped.
751f74bd194SAndy Whitcroft		if ($type eq '#' && $c eq "\n" && $p ne "\\") {
752f74bd194SAndy Whitcroft			$level--;
753f74bd194SAndy Whitcroft			$type = '';
754f74bd194SAndy Whitcroft			$off++;
755f74bd194SAndy Whitcroft			last;
756f74bd194SAndy Whitcroft		}
7578905a67cSAndy Whitcroft		$off++;
7588905a67cSAndy Whitcroft	}
759a3bb97a7SAndy Whitcroft	# We are truly at the end, so shuffle to the next line.
76013214adfSAndy Whitcroft	if ($off == $len) {
761a3bb97a7SAndy Whitcroft		$loff = $len + 1;
76213214adfSAndy Whitcroft		$line++;
76313214adfSAndy Whitcroft		$remain--;
76413214adfSAndy Whitcroft	}
7658905a67cSAndy Whitcroft
7668905a67cSAndy Whitcroft	my $statement = substr($blk, $soff, $off - $soff + 1);
7678905a67cSAndy Whitcroft	my $condition = substr($blk, $soff, $coff - $soff + 1);
7688905a67cSAndy Whitcroft
7698905a67cSAndy Whitcroft	#warn "STATEMENT<$statement>\n";
7708905a67cSAndy Whitcroft	#warn "CONDITION<$condition>\n";
7718905a67cSAndy Whitcroft
772773647a0SAndy Whitcroft	#print "coff<$coff> soff<$off> loff<$loff>\n";
77313214adfSAndy Whitcroft
77413214adfSAndy Whitcroft	return ($statement, $condition,
77513214adfSAndy Whitcroft			$line, $remain + 1, $off - $loff + 1, $level);
77613214adfSAndy Whitcroft}
77713214adfSAndy Whitcroft
778cf655043SAndy Whitcroftsub statement_lines {
779cf655043SAndy Whitcroft	my ($stmt) = @_;
780cf655043SAndy Whitcroft
781cf655043SAndy Whitcroft	# Strip the diff line prefixes and rip blank lines at start and end.
782cf655043SAndy Whitcroft	$stmt =~ s/(^|\n)./$1/g;
783cf655043SAndy Whitcroft	$stmt =~ s/^\s*//;
784cf655043SAndy Whitcroft	$stmt =~ s/\s*$//;
785cf655043SAndy Whitcroft
786cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
787cf655043SAndy Whitcroft
788cf655043SAndy Whitcroft	return $#stmt_lines + 2;
789cf655043SAndy Whitcroft}
790cf655043SAndy Whitcroft
791cf655043SAndy Whitcroftsub statement_rawlines {
792cf655043SAndy Whitcroft	my ($stmt) = @_;
793cf655043SAndy Whitcroft
794cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
795cf655043SAndy Whitcroft
796cf655043SAndy Whitcroft	return $#stmt_lines + 2;
797cf655043SAndy Whitcroft}
798cf655043SAndy Whitcroft
799cf655043SAndy Whitcroftsub statement_block_size {
800cf655043SAndy Whitcroft	my ($stmt) = @_;
801cf655043SAndy Whitcroft
802cf655043SAndy Whitcroft	$stmt =~ s/(^|\n)./$1/g;
803cf655043SAndy Whitcroft	$stmt =~ s/^\s*{//;
804cf655043SAndy Whitcroft	$stmt =~ s/}\s*$//;
805cf655043SAndy Whitcroft	$stmt =~ s/^\s*//;
806cf655043SAndy Whitcroft	$stmt =~ s/\s*$//;
807cf655043SAndy Whitcroft
808cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
809cf655043SAndy Whitcroft	my @stmt_statements = ($stmt =~ /;/g);
810cf655043SAndy Whitcroft
811cf655043SAndy Whitcroft	my $stmt_lines = $#stmt_lines + 2;
812cf655043SAndy Whitcroft	my $stmt_statements = $#stmt_statements + 1;
813cf655043SAndy Whitcroft
814cf655043SAndy Whitcroft	if ($stmt_lines > $stmt_statements) {
815cf655043SAndy Whitcroft		return $stmt_lines;
816cf655043SAndy Whitcroft	} else {
817cf655043SAndy Whitcroft		return $stmt_statements;
818cf655043SAndy Whitcroft	}
819cf655043SAndy Whitcroft}
820cf655043SAndy Whitcroft
82113214adfSAndy Whitcroftsub ctx_statement_full {
82213214adfSAndy Whitcroft	my ($linenr, $remain, $off) = @_;
82313214adfSAndy Whitcroft	my ($statement, $condition, $level);
82413214adfSAndy Whitcroft
82513214adfSAndy Whitcroft	my (@chunks);
82613214adfSAndy Whitcroft
827cf655043SAndy Whitcroft	# Grab the first conditional/block pair.
82813214adfSAndy Whitcroft	($statement, $condition, $linenr, $remain, $off, $level) =
82913214adfSAndy Whitcroft				ctx_statement_block($linenr, $remain, $off);
830773647a0SAndy Whitcroft	#print "F: c<$condition> s<$statement> remain<$remain>\n";
83113214adfSAndy Whitcroft	push(@chunks, [ $condition, $statement ]);
832cf655043SAndy Whitcroft	if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
833cf655043SAndy Whitcroft		return ($level, $linenr, @chunks);
834cf655043SAndy Whitcroft	}
835cf655043SAndy Whitcroft
836cf655043SAndy Whitcroft	# Pull in the following conditional/block pairs and see if they
837cf655043SAndy Whitcroft	# could continue the statement.
838cf655043SAndy Whitcroft	for (;;) {
83913214adfSAndy Whitcroft		($statement, $condition, $linenr, $remain, $off, $level) =
84013214adfSAndy Whitcroft				ctx_statement_block($linenr, $remain, $off);
841cf655043SAndy Whitcroft		#print "C: c<$condition> s<$statement> remain<$remain>\n";
842773647a0SAndy Whitcroft		last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
843cf655043SAndy Whitcroft		#print "C: push\n";
844cf655043SAndy Whitcroft		push(@chunks, [ $condition, $statement ]);
84513214adfSAndy Whitcroft	}
84613214adfSAndy Whitcroft
84713214adfSAndy Whitcroft	return ($level, $linenr, @chunks);
8488905a67cSAndy Whitcroft}
8498905a67cSAndy Whitcroft
8504a0df2efSAndy Whitcroftsub ctx_block_get {
851f0a594c1SAndy Whitcroft	my ($linenr, $remain, $outer, $open, $close, $off) = @_;
8524a0df2efSAndy Whitcroft	my $line;
8534a0df2efSAndy Whitcroft	my $start = $linenr - 1;
8544a0df2efSAndy Whitcroft	my $blk = '';
8554a0df2efSAndy Whitcroft	my @o;
8564a0df2efSAndy Whitcroft	my @c;
8574a0df2efSAndy Whitcroft	my @res = ();
8584a0df2efSAndy Whitcroft
859f0a594c1SAndy Whitcroft	my $level = 0;
8604635f4fbSAndy Whitcroft	my @stack = ($level);
86100df344fSAndy Whitcroft	for ($line = $start; $remain > 0; $line++) {
86200df344fSAndy Whitcroft		next if ($rawlines[$line] =~ /^-/);
86300df344fSAndy Whitcroft		$remain--;
86400df344fSAndy Whitcroft
86500df344fSAndy Whitcroft		$blk .= $rawlines[$line];
8664635f4fbSAndy Whitcroft
8674635f4fbSAndy Whitcroft		# Handle nested #if/#else.
86801464f30SAndy Whitcroft		if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
8694635f4fbSAndy Whitcroft			push(@stack, $level);
87001464f30SAndy Whitcroft		} elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
8714635f4fbSAndy Whitcroft			$level = $stack[$#stack - 1];
87201464f30SAndy Whitcroft		} elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
8734635f4fbSAndy Whitcroft			$level = pop(@stack);
8744635f4fbSAndy Whitcroft		}
8754635f4fbSAndy Whitcroft
87601464f30SAndy Whitcroft		foreach my $c (split(//, $lines[$line])) {
877f0a594c1SAndy Whitcroft			##print "C<$c>L<$level><$open$close>O<$off>\n";
878f0a594c1SAndy Whitcroft			if ($off > 0) {
879f0a594c1SAndy Whitcroft				$off--;
880f0a594c1SAndy Whitcroft				next;
881f0a594c1SAndy Whitcroft			}
8824a0df2efSAndy Whitcroft
883f0a594c1SAndy Whitcroft			if ($c eq $close && $level > 0) {
884f0a594c1SAndy Whitcroft				$level--;
885f0a594c1SAndy Whitcroft				last if ($level == 0);
886f0a594c1SAndy Whitcroft			} elsif ($c eq $open) {
887f0a594c1SAndy Whitcroft				$level++;
888f0a594c1SAndy Whitcroft			}
889f0a594c1SAndy Whitcroft		}
8904a0df2efSAndy Whitcroft
891f0a594c1SAndy Whitcroft		if (!$outer || $level <= 1) {
89200df344fSAndy Whitcroft			push(@res, $rawlines[$line]);
8934a0df2efSAndy Whitcroft		}
8944a0df2efSAndy Whitcroft
895f0a594c1SAndy Whitcroft		last if ($level == 0);
8964a0df2efSAndy Whitcroft	}
8974a0df2efSAndy Whitcroft
898f0a594c1SAndy Whitcroft	return ($level, @res);
8994a0df2efSAndy Whitcroft}
9004a0df2efSAndy Whitcroftsub ctx_block_outer {
9014a0df2efSAndy Whitcroft	my ($linenr, $remain) = @_;
9024a0df2efSAndy Whitcroft
903f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
904f0a594c1SAndy Whitcroft	return @r;
9054a0df2efSAndy Whitcroft}
9064a0df2efSAndy Whitcroftsub ctx_block {
9074a0df2efSAndy Whitcroft	my ($linenr, $remain) = @_;
9084a0df2efSAndy Whitcroft
909f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
910f0a594c1SAndy Whitcroft	return @r;
911653d4876SAndy Whitcroft}
912653d4876SAndy Whitcroftsub ctx_statement {
913f0a594c1SAndy Whitcroft	my ($linenr, $remain, $off) = @_;
914f0a594c1SAndy Whitcroft
915f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
916f0a594c1SAndy Whitcroft	return @r;
917f0a594c1SAndy Whitcroft}
918f0a594c1SAndy Whitcroftsub ctx_block_level {
919653d4876SAndy Whitcroft	my ($linenr, $remain) = @_;
920653d4876SAndy Whitcroft
921f0a594c1SAndy Whitcroft	return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
9224a0df2efSAndy Whitcroft}
9239c0ca6f9SAndy Whitcroftsub ctx_statement_level {
9249c0ca6f9SAndy Whitcroft	my ($linenr, $remain, $off) = @_;
9259c0ca6f9SAndy Whitcroft
9269c0ca6f9SAndy Whitcroft	return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
9279c0ca6f9SAndy Whitcroft}
9284a0df2efSAndy Whitcroft
9294a0df2efSAndy Whitcroftsub ctx_locate_comment {
9304a0df2efSAndy Whitcroft	my ($first_line, $end_line) = @_;
9314a0df2efSAndy Whitcroft
9324a0df2efSAndy Whitcroft	# Catch a comment on the end of the line itself.
933beae6332SAndy Whitcroft	my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
9344a0df2efSAndy Whitcroft	return $current_comment if (defined $current_comment);
9354a0df2efSAndy Whitcroft
9364a0df2efSAndy Whitcroft	# Look through the context and try and figure out if there is a
9374a0df2efSAndy Whitcroft	# comment.
9384a0df2efSAndy Whitcroft	my $in_comment = 0;
9394a0df2efSAndy Whitcroft	$current_comment = '';
9404a0df2efSAndy Whitcroft	for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
94100df344fSAndy Whitcroft		my $line = $rawlines[$linenr - 1];
94200df344fSAndy Whitcroft		#warn "           $line\n";
9434a0df2efSAndy Whitcroft		if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
9444a0df2efSAndy Whitcroft			$in_comment = 1;
9454a0df2efSAndy Whitcroft		}
9464a0df2efSAndy Whitcroft		if ($line =~ m@/\*@) {
9474a0df2efSAndy Whitcroft			$in_comment = 1;
9484a0df2efSAndy Whitcroft		}
9494a0df2efSAndy Whitcroft		if (!$in_comment && $current_comment ne '') {
9504a0df2efSAndy Whitcroft			$current_comment = '';
9514a0df2efSAndy Whitcroft		}
9524a0df2efSAndy Whitcroft		$current_comment .= $line . "\n" if ($in_comment);
9534a0df2efSAndy Whitcroft		if ($line =~ m@\*/@) {
9544a0df2efSAndy Whitcroft			$in_comment = 0;
9554a0df2efSAndy Whitcroft		}
9564a0df2efSAndy Whitcroft	}
9574a0df2efSAndy Whitcroft
9584a0df2efSAndy Whitcroft	chomp($current_comment);
9594a0df2efSAndy Whitcroft	return($current_comment);
9604a0df2efSAndy Whitcroft}
9614a0df2efSAndy Whitcroftsub ctx_has_comment {
9624a0df2efSAndy Whitcroft	my ($first_line, $end_line) = @_;
9634a0df2efSAndy Whitcroft	my $cmt = ctx_locate_comment($first_line, $end_line);
9644a0df2efSAndy Whitcroft
96500df344fSAndy Whitcroft	##print "LINE: $rawlines[$end_line - 1 ]\n";
9664a0df2efSAndy Whitcroft	##print "CMMT: $cmt\n";
9674a0df2efSAndy Whitcroft
9684a0df2efSAndy Whitcroft	return ($cmt ne '');
9694a0df2efSAndy Whitcroft}
9704a0df2efSAndy Whitcroft
9714d001e4dSAndy Whitcroftsub raw_line {
9724d001e4dSAndy Whitcroft	my ($linenr, $cnt) = @_;
9734d001e4dSAndy Whitcroft
9744d001e4dSAndy Whitcroft	my $offset = $linenr - 1;
9754d001e4dSAndy Whitcroft	$cnt++;
9764d001e4dSAndy Whitcroft
9774d001e4dSAndy Whitcroft	my $line;
9784d001e4dSAndy Whitcroft	while ($cnt) {
9794d001e4dSAndy Whitcroft		$line = $rawlines[$offset++];
9804d001e4dSAndy Whitcroft		next if (defined($line) && $line =~ /^-/);
9814d001e4dSAndy Whitcroft		$cnt--;
9824d001e4dSAndy Whitcroft	}
9834d001e4dSAndy Whitcroft
9844d001e4dSAndy Whitcroft	return $line;
9854d001e4dSAndy Whitcroft}
9864d001e4dSAndy Whitcroft
9870a920b5bSAndy Whitcroftsub cat_vet {
9880a920b5bSAndy Whitcroft	my ($vet) = @_;
9899c0ca6f9SAndy Whitcroft	my ($res, $coded);
9900a920b5bSAndy Whitcroft
9919c0ca6f9SAndy Whitcroft	$res = '';
9926c72ffaaSAndy Whitcroft	while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
9936c72ffaaSAndy Whitcroft		$res .= $1;
9946c72ffaaSAndy Whitcroft		if ($2 ne '') {
9959c0ca6f9SAndy Whitcroft			$coded = sprintf("^%c", unpack('C', $2) + 64);
9966c72ffaaSAndy Whitcroft			$res .= $coded;
9976c72ffaaSAndy Whitcroft		}
9989c0ca6f9SAndy Whitcroft	}
9999c0ca6f9SAndy Whitcroft	$res =~ s/$/\$/;
10000a920b5bSAndy Whitcroft
10019c0ca6f9SAndy Whitcroft	return $res;
10020a920b5bSAndy Whitcroft}
10030a920b5bSAndy Whitcroft
1004c2fdda0dSAndy Whitcroftmy $av_preprocessor = 0;
1005cf655043SAndy Whitcroftmy $av_pending;
1006c2fdda0dSAndy Whitcroftmy @av_paren_type;
10071f65f947SAndy Whitcroftmy $av_pend_colon;
1008c2fdda0dSAndy Whitcroft
1009c2fdda0dSAndy Whitcroftsub annotate_reset {
1010c2fdda0dSAndy Whitcroft	$av_preprocessor = 0;
1011cf655043SAndy Whitcroft	$av_pending = '_';
1012cf655043SAndy Whitcroft	@av_paren_type = ('E');
10131f65f947SAndy Whitcroft	$av_pend_colon = 'O';
1014c2fdda0dSAndy Whitcroft}
1015c2fdda0dSAndy Whitcroft
10166c72ffaaSAndy Whitcroftsub annotate_values {
10176c72ffaaSAndy Whitcroft	my ($stream, $type) = @_;
10186c72ffaaSAndy Whitcroft
10196c72ffaaSAndy Whitcroft	my $res;
10201f65f947SAndy Whitcroft	my $var = '_' x length($stream);
10216c72ffaaSAndy Whitcroft	my $cur = $stream;
10226c72ffaaSAndy Whitcroft
1023c2fdda0dSAndy Whitcroft	print "$stream\n" if ($dbg_values > 1);
10246c72ffaaSAndy Whitcroft
10256c72ffaaSAndy Whitcroft	while (length($cur)) {
1026773647a0SAndy Whitcroft		@av_paren_type = ('E') if ($#av_paren_type < 0);
1027cf655043SAndy Whitcroft		print " <" . join('', @av_paren_type) .
1028171ae1a4SAndy Whitcroft				"> <$type> <$av_pending>" if ($dbg_values > 1);
10296c72ffaaSAndy Whitcroft		if ($cur =~ /^(\s+)/o) {
1030c2fdda0dSAndy Whitcroft			print "WS($1)\n" if ($dbg_values > 1);
1031c2fdda0dSAndy Whitcroft			if ($1 =~ /\n/ && $av_preprocessor) {
1032cf655043SAndy Whitcroft				$type = pop(@av_paren_type);
1033c2fdda0dSAndy Whitcroft				$av_preprocessor = 0;
10346c72ffaaSAndy Whitcroft			}
10356c72ffaaSAndy Whitcroft
1036c023e473SFlorian Mickler		} elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
10379446ef56SAndy Whitcroft			print "CAST($1)\n" if ($dbg_values > 1);
10389446ef56SAndy Whitcroft			push(@av_paren_type, $type);
1039addcdceaSAndy Whitcroft			$type = 'c';
10409446ef56SAndy Whitcroft
1041e91b6e26SAndy Whitcroft		} elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
1042c2fdda0dSAndy Whitcroft			print "DECLARE($1)\n" if ($dbg_values > 1);
10436c72ffaaSAndy Whitcroft			$type = 'T';
10446c72ffaaSAndy Whitcroft
1045389a2fe5SAndy Whitcroft		} elsif ($cur =~ /^($Modifier)\s*/) {
1046389a2fe5SAndy Whitcroft			print "MODIFIER($1)\n" if ($dbg_values > 1);
1047389a2fe5SAndy Whitcroft			$type = 'T';
1048389a2fe5SAndy Whitcroft
1049c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
1050171ae1a4SAndy Whitcroft			print "DEFINE($1,$2)\n" if ($dbg_values > 1);
1051c2fdda0dSAndy Whitcroft			$av_preprocessor = 1;
1052171ae1a4SAndy Whitcroft			push(@av_paren_type, $type);
1053171ae1a4SAndy Whitcroft			if ($2 ne '') {
1054cf655043SAndy Whitcroft				$av_pending = 'N';
1055171ae1a4SAndy Whitcroft			}
1056171ae1a4SAndy Whitcroft			$type = 'E';
1057171ae1a4SAndy Whitcroft
1058c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
1059171ae1a4SAndy Whitcroft			print "UNDEF($1)\n" if ($dbg_values > 1);
1060171ae1a4SAndy Whitcroft			$av_preprocessor = 1;
1061171ae1a4SAndy Whitcroft			push(@av_paren_type, $type);
10626c72ffaaSAndy Whitcroft
1063c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
1064cf655043SAndy Whitcroft			print "PRE_START($1)\n" if ($dbg_values > 1);
1065c2fdda0dSAndy Whitcroft			$av_preprocessor = 1;
1066cf655043SAndy Whitcroft
1067cf655043SAndy Whitcroft			push(@av_paren_type, $type);
1068cf655043SAndy Whitcroft			push(@av_paren_type, $type);
1069171ae1a4SAndy Whitcroft			$type = 'E';
1070cf655043SAndy Whitcroft
1071c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
1072cf655043SAndy Whitcroft			print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1073cf655043SAndy Whitcroft			$av_preprocessor = 1;
1074cf655043SAndy Whitcroft
1075cf655043SAndy Whitcroft			push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1076cf655043SAndy Whitcroft
1077171ae1a4SAndy Whitcroft			$type = 'E';
1078cf655043SAndy Whitcroft
1079c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:endif))/o) {
1080cf655043SAndy Whitcroft			print "PRE_END($1)\n" if ($dbg_values > 1);
1081cf655043SAndy Whitcroft
1082cf655043SAndy Whitcroft			$av_preprocessor = 1;
1083cf655043SAndy Whitcroft
1084cf655043SAndy Whitcroft			# Assume all arms of the conditional end as this
1085cf655043SAndy Whitcroft			# one does, and continue as if the #endif was not here.
1086cf655043SAndy Whitcroft			pop(@av_paren_type);
1087cf655043SAndy Whitcroft			push(@av_paren_type, $type);
1088171ae1a4SAndy Whitcroft			$type = 'E';
10896c72ffaaSAndy Whitcroft
10906c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\\\n)/o) {
1091c2fdda0dSAndy Whitcroft			print "PRECONT($1)\n" if ($dbg_values > 1);
10926c72ffaaSAndy Whitcroft
1093171ae1a4SAndy Whitcroft		} elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1094171ae1a4SAndy Whitcroft			print "ATTR($1)\n" if ($dbg_values > 1);
1095171ae1a4SAndy Whitcroft			$av_pending = $type;
1096171ae1a4SAndy Whitcroft			$type = 'N';
1097171ae1a4SAndy Whitcroft
10986c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
1099c2fdda0dSAndy Whitcroft			print "SIZEOF($1)\n" if ($dbg_values > 1);
11006c72ffaaSAndy Whitcroft			if (defined $2) {
1101cf655043SAndy Whitcroft				$av_pending = 'V';
11026c72ffaaSAndy Whitcroft			}
11036c72ffaaSAndy Whitcroft			$type = 'N';
11046c72ffaaSAndy Whitcroft
110514b111c1SAndy Whitcroft		} elsif ($cur =~ /^(if|while|for)\b/o) {
1106c2fdda0dSAndy Whitcroft			print "COND($1)\n" if ($dbg_values > 1);
110714b111c1SAndy Whitcroft			$av_pending = 'E';
11086c72ffaaSAndy Whitcroft			$type = 'N';
11096c72ffaaSAndy Whitcroft
11101f65f947SAndy Whitcroft		} elsif ($cur =~/^(case)/o) {
11111f65f947SAndy Whitcroft			print "CASE($1)\n" if ($dbg_values > 1);
11121f65f947SAndy Whitcroft			$av_pend_colon = 'C';
11131f65f947SAndy Whitcroft			$type = 'N';
11141f65f947SAndy Whitcroft
111514b111c1SAndy Whitcroft		} elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
1116c2fdda0dSAndy Whitcroft			print "KEYWORD($1)\n" if ($dbg_values > 1);
11176c72ffaaSAndy Whitcroft			$type = 'N';
11186c72ffaaSAndy Whitcroft
11196c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\()/o) {
1120c2fdda0dSAndy Whitcroft			print "PAREN('$1')\n" if ($dbg_values > 1);
1121cf655043SAndy Whitcroft			push(@av_paren_type, $av_pending);
1122cf655043SAndy Whitcroft			$av_pending = '_';
11236c72ffaaSAndy Whitcroft			$type = 'N';
11246c72ffaaSAndy Whitcroft
11256c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\))/o) {
1126cf655043SAndy Whitcroft			my $new_type = pop(@av_paren_type);
1127cf655043SAndy Whitcroft			if ($new_type ne '_') {
1128cf655043SAndy Whitcroft				$type = $new_type;
1129c2fdda0dSAndy Whitcroft				print "PAREN('$1') -> $type\n"
1130c2fdda0dSAndy Whitcroft							if ($dbg_values > 1);
11316c72ffaaSAndy Whitcroft			} else {
1132c2fdda0dSAndy Whitcroft				print "PAREN('$1')\n" if ($dbg_values > 1);
11336c72ffaaSAndy Whitcroft			}
11346c72ffaaSAndy Whitcroft
1135c8cb2ca3SAndy Whitcroft		} elsif ($cur =~ /^($Ident)\s*\(/o) {
1136c2fdda0dSAndy Whitcroft			print "FUNC($1)\n" if ($dbg_values > 1);
1137c8cb2ca3SAndy Whitcroft			$type = 'V';
1138cf655043SAndy Whitcroft			$av_pending = 'V';
11396c72ffaaSAndy Whitcroft
11408e761b04SAndy Whitcroft		} elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
11418e761b04SAndy Whitcroft			if (defined $2 && $type eq 'C' || $type eq 'T') {
11421f65f947SAndy Whitcroft				$av_pend_colon = 'B';
11438e761b04SAndy Whitcroft			} elsif ($type eq 'E') {
11448e761b04SAndy Whitcroft				$av_pend_colon = 'L';
11451f65f947SAndy Whitcroft			}
11461f65f947SAndy Whitcroft			print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
11471f65f947SAndy Whitcroft			$type = 'V';
11481f65f947SAndy Whitcroft
11496c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Ident|$Constant)/o) {
1150c2fdda0dSAndy Whitcroft			print "IDENT($1)\n" if ($dbg_values > 1);
11516c72ffaaSAndy Whitcroft			$type = 'V';
11526c72ffaaSAndy Whitcroft
11536c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Assignment)/o) {
1154c2fdda0dSAndy Whitcroft			print "ASSIGN($1)\n" if ($dbg_values > 1);
11556c72ffaaSAndy Whitcroft			$type = 'N';
11566c72ffaaSAndy Whitcroft
1157cf655043SAndy Whitcroft		} elsif ($cur =~/^(;|{|})/) {
1158c2fdda0dSAndy Whitcroft			print "END($1)\n" if ($dbg_values > 1);
115913214adfSAndy Whitcroft			$type = 'E';
11601f65f947SAndy Whitcroft			$av_pend_colon = 'O';
116113214adfSAndy Whitcroft
11628e761b04SAndy Whitcroft		} elsif ($cur =~/^(,)/) {
11638e761b04SAndy Whitcroft			print "COMMA($1)\n" if ($dbg_values > 1);
11648e761b04SAndy Whitcroft			$type = 'C';
11658e761b04SAndy Whitcroft
11661f65f947SAndy Whitcroft		} elsif ($cur =~ /^(\?)/o) {
11671f65f947SAndy Whitcroft			print "QUESTION($1)\n" if ($dbg_values > 1);
11681f65f947SAndy Whitcroft			$type = 'N';
11691f65f947SAndy Whitcroft
11701f65f947SAndy Whitcroft		} elsif ($cur =~ /^(:)/o) {
11711f65f947SAndy Whitcroft			print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
11721f65f947SAndy Whitcroft
11731f65f947SAndy Whitcroft			substr($var, length($res), 1, $av_pend_colon);
11741f65f947SAndy Whitcroft			if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
11751f65f947SAndy Whitcroft				$type = 'E';
11761f65f947SAndy Whitcroft			} else {
11771f65f947SAndy Whitcroft				$type = 'N';
11781f65f947SAndy Whitcroft			}
11791f65f947SAndy Whitcroft			$av_pend_colon = 'O';
11801f65f947SAndy Whitcroft
11818e761b04SAndy Whitcroft		} elsif ($cur =~ /^(\[)/o) {
118213214adfSAndy Whitcroft			print "CLOSE($1)\n" if ($dbg_values > 1);
11836c72ffaaSAndy Whitcroft			$type = 'N';
11846c72ffaaSAndy Whitcroft
11850d413866SAndy Whitcroft		} elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
118674048ed8SAndy Whitcroft			my $variant;
118774048ed8SAndy Whitcroft
118874048ed8SAndy Whitcroft			print "OPV($1)\n" if ($dbg_values > 1);
118974048ed8SAndy Whitcroft			if ($type eq 'V') {
119074048ed8SAndy Whitcroft				$variant = 'B';
119174048ed8SAndy Whitcroft			} else {
119274048ed8SAndy Whitcroft				$variant = 'U';
119374048ed8SAndy Whitcroft			}
119474048ed8SAndy Whitcroft
119574048ed8SAndy Whitcroft			substr($var, length($res), 1, $variant);
119674048ed8SAndy Whitcroft			$type = 'N';
119774048ed8SAndy Whitcroft
11986c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Operators)/o) {
1199c2fdda0dSAndy Whitcroft			print "OP($1)\n" if ($dbg_values > 1);
12006c72ffaaSAndy Whitcroft			if ($1 ne '++' && $1 ne '--') {
12016c72ffaaSAndy Whitcroft				$type = 'N';
12026c72ffaaSAndy Whitcroft			}
12036c72ffaaSAndy Whitcroft
12046c72ffaaSAndy Whitcroft		} elsif ($cur =~ /(^.)/o) {
1205c2fdda0dSAndy Whitcroft			print "C($1)\n" if ($dbg_values > 1);
12066c72ffaaSAndy Whitcroft		}
12076c72ffaaSAndy Whitcroft		if (defined $1) {
12086c72ffaaSAndy Whitcroft			$cur = substr($cur, length($1));
12096c72ffaaSAndy Whitcroft			$res .= $type x length($1);
12106c72ffaaSAndy Whitcroft		}
12116c72ffaaSAndy Whitcroft	}
12126c72ffaaSAndy Whitcroft
12131f65f947SAndy Whitcroft	return ($res, $var);
12146c72ffaaSAndy Whitcroft}
12156c72ffaaSAndy Whitcroft
12168905a67cSAndy Whitcroftsub possible {
121713214adfSAndy Whitcroft	my ($possible, $line) = @_;
12189a974fdbSAndy Whitcroft	my $notPermitted = qr{(?:
12190776e594SAndy Whitcroft		^(?:
12200776e594SAndy Whitcroft			$Modifier|
12210776e594SAndy Whitcroft			$Storage|
12220776e594SAndy Whitcroft			$Type|
12239a974fdbSAndy Whitcroft			DEFINE_\S+
12249a974fdbSAndy Whitcroft		)$|
12259a974fdbSAndy Whitcroft		^(?:
12260776e594SAndy Whitcroft			goto|
12270776e594SAndy Whitcroft			return|
12280776e594SAndy Whitcroft			case|
12290776e594SAndy Whitcroft			else|
12300776e594SAndy Whitcroft			asm|__asm__|
123189a88353SAndy Whitcroft			do|
123289a88353SAndy Whitcroft			\#|
123389a88353SAndy Whitcroft			\#\#|
12349a974fdbSAndy Whitcroft		)(?:\s|$)|
12350776e594SAndy Whitcroft		^(?:typedef|struct|enum)\b
12369a974fdbSAndy Whitcroft	    )}x;
12379a974fdbSAndy Whitcroft	warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
12389a974fdbSAndy Whitcroft	if ($possible !~ $notPermitted) {
1239c45dcabdSAndy Whitcroft		# Check for modifiers.
1240c45dcabdSAndy Whitcroft		$possible =~ s/\s*$Storage\s*//g;
1241c45dcabdSAndy Whitcroft		$possible =~ s/\s*$Sparse\s*//g;
1242c45dcabdSAndy Whitcroft		if ($possible =~ /^\s*$/) {
1243c45dcabdSAndy Whitcroft
1244c45dcabdSAndy Whitcroft		} elsif ($possible =~ /\s/) {
1245c45dcabdSAndy Whitcroft			$possible =~ s/\s*$Type\s*//g;
1246d2506586SAndy Whitcroft			for my $modifier (split(' ', $possible)) {
12479a974fdbSAndy Whitcroft				if ($modifier !~ $notPermitted) {
1248d2506586SAndy Whitcroft					warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1249d2506586SAndy Whitcroft					push(@modifierList, $modifier);
1250d2506586SAndy Whitcroft				}
12519a974fdbSAndy Whitcroft			}
1252c45dcabdSAndy Whitcroft
1253c45dcabdSAndy Whitcroft		} else {
125413214adfSAndy Whitcroft			warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
12558905a67cSAndy Whitcroft			push(@typeList, $possible);
1256c45dcabdSAndy Whitcroft		}
12578905a67cSAndy Whitcroft		build_types();
12580776e594SAndy Whitcroft	} else {
12590776e594SAndy Whitcroft		warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
12608905a67cSAndy Whitcroft	}
12618905a67cSAndy Whitcroft}
12628905a67cSAndy Whitcroft
12636c72ffaaSAndy Whitcroftmy $prefix = '';
12646c72ffaaSAndy Whitcroft
1265000d1cc1SJoe Perchessub show_type {
1266000d1cc1SJoe Perches       return !defined $ignore_type{$_[0]};
1267000d1cc1SJoe Perches}
1268000d1cc1SJoe Perches
1269f0a594c1SAndy Whitcroftsub report {
1270000d1cc1SJoe Perches	if (!show_type($_[1]) ||
1271000d1cc1SJoe Perches	    (defined $tst_only && $_[2] !~ /\Q$tst_only\E/)) {
1272773647a0SAndy Whitcroft		return 0;
1273773647a0SAndy Whitcroft	}
1274000d1cc1SJoe Perches	my $line;
1275000d1cc1SJoe Perches	if ($show_types) {
1276000d1cc1SJoe Perches		$line = "$prefix$_[0]:$_[1]: $_[2]\n";
1277000d1cc1SJoe Perches	} else {
1278000d1cc1SJoe Perches		$line = "$prefix$_[0]: $_[2]\n";
1279000d1cc1SJoe Perches	}
12808905a67cSAndy Whitcroft	$line = (split('\n', $line))[0] . "\n" if ($terse);
12818905a67cSAndy Whitcroft
128213214adfSAndy Whitcroft	push(our @report, $line);
1283773647a0SAndy Whitcroft
1284773647a0SAndy Whitcroft	return 1;
1285f0a594c1SAndy Whitcroft}
1286f0a594c1SAndy Whitcroftsub report_dump {
128713214adfSAndy Whitcroft	our @report;
1288f0a594c1SAndy Whitcroft}
1289000d1cc1SJoe Perches
1290de7d4f0eSAndy Whitcroftsub ERROR {
1291000d1cc1SJoe Perches	if (report("ERROR", $_[0], $_[1])) {
1292de7d4f0eSAndy Whitcroft		our $clean = 0;
12936c72ffaaSAndy Whitcroft		our $cnt_error++;
1294de7d4f0eSAndy Whitcroft	}
1295773647a0SAndy Whitcroft}
1296de7d4f0eSAndy Whitcroftsub WARN {
1297000d1cc1SJoe Perches	if (report("WARNING", $_[0], $_[1])) {
1298de7d4f0eSAndy Whitcroft		our $clean = 0;
12996c72ffaaSAndy Whitcroft		our $cnt_warn++;
1300de7d4f0eSAndy Whitcroft	}
1301773647a0SAndy Whitcroft}
1302de7d4f0eSAndy Whitcroftsub CHK {
1303000d1cc1SJoe Perches	if ($check && report("CHECK", $_[0], $_[1])) {
1304de7d4f0eSAndy Whitcroft		our $clean = 0;
13056c72ffaaSAndy Whitcroft		our $cnt_chk++;
13066c72ffaaSAndy Whitcroft	}
1307de7d4f0eSAndy Whitcroft}
1308de7d4f0eSAndy Whitcroft
13096ecd9674SAndy Whitcroftsub check_absolute_file {
13106ecd9674SAndy Whitcroft	my ($absolute, $herecurr) = @_;
13116ecd9674SAndy Whitcroft	my $file = $absolute;
13126ecd9674SAndy Whitcroft
13136ecd9674SAndy Whitcroft	##print "absolute<$absolute>\n";
13146ecd9674SAndy Whitcroft
13156ecd9674SAndy Whitcroft	# See if any suffix of this path is a path within the tree.
13166ecd9674SAndy Whitcroft	while ($file =~ s@^[^/]*/@@) {
13176ecd9674SAndy Whitcroft		if (-f "$root/$file") {
13186ecd9674SAndy Whitcroft			##print "file<$file>\n";
13196ecd9674SAndy Whitcroft			last;
13206ecd9674SAndy Whitcroft		}
13216ecd9674SAndy Whitcroft	}
13226ecd9674SAndy Whitcroft	if (! -f _)  {
13236ecd9674SAndy Whitcroft		return 0;
13246ecd9674SAndy Whitcroft	}
13256ecd9674SAndy Whitcroft
13266ecd9674SAndy Whitcroft	# It is, so see if the prefix is acceptable.
13276ecd9674SAndy Whitcroft	my $prefix = $absolute;
13286ecd9674SAndy Whitcroft	substr($prefix, -length($file)) = '';
13296ecd9674SAndy Whitcroft
13306ecd9674SAndy Whitcroft	##print "prefix<$prefix>\n";
13316ecd9674SAndy Whitcroft	if ($prefix ne ".../") {
1332000d1cc1SJoe Perches		WARN("USE_RELATIVE_PATH",
1333000d1cc1SJoe Perches		     "use relative pathname instead of absolute in changelog text\n" . $herecurr);
13346ecd9674SAndy Whitcroft	}
13356ecd9674SAndy Whitcroft}
13366ecd9674SAndy Whitcroft
1337d1fe9c09SJoe Perchessub pos_last_openparen {
1338d1fe9c09SJoe Perches	my ($line) = @_;
1339d1fe9c09SJoe Perches
1340d1fe9c09SJoe Perches	my $pos = 0;
1341d1fe9c09SJoe Perches
1342d1fe9c09SJoe Perches	my $opens = $line =~ tr/\(/\(/;
1343d1fe9c09SJoe Perches	my $closes = $line =~ tr/\)/\)/;
1344d1fe9c09SJoe Perches
1345d1fe9c09SJoe Perches	my $last_openparen = 0;
1346d1fe9c09SJoe Perches
1347d1fe9c09SJoe Perches	if (($opens == 0) || ($closes >= $opens)) {
1348d1fe9c09SJoe Perches		return -1;
1349d1fe9c09SJoe Perches	}
1350d1fe9c09SJoe Perches
1351d1fe9c09SJoe Perches	my $len = length($line);
1352d1fe9c09SJoe Perches
1353d1fe9c09SJoe Perches	for ($pos = 0; $pos < $len; $pos++) {
1354d1fe9c09SJoe Perches		my $string = substr($line, $pos);
1355d1fe9c09SJoe Perches		if ($string =~ /^($FuncArg|$balanced_parens)/) {
1356d1fe9c09SJoe Perches			$pos += length($1) - 1;
1357d1fe9c09SJoe Perches		} elsif (substr($line, $pos, 1) eq '(') {
1358d1fe9c09SJoe Perches			$last_openparen = $pos;
1359d1fe9c09SJoe Perches		} elsif (index($string, '(') == -1) {
1360d1fe9c09SJoe Perches			last;
1361d1fe9c09SJoe Perches		}
1362d1fe9c09SJoe Perches	}
1363d1fe9c09SJoe Perches
1364d1fe9c09SJoe Perches	return $last_openparen + 1;
1365d1fe9c09SJoe Perches}
1366d1fe9c09SJoe Perches
13670a920b5bSAndy Whitcroftsub process {
13680a920b5bSAndy Whitcroft	my $filename = shift;
13690a920b5bSAndy Whitcroft
13700a920b5bSAndy Whitcroft	my $linenr=0;
13710a920b5bSAndy Whitcroft	my $prevline="";
1372c2fdda0dSAndy Whitcroft	my $prevrawline="";
13730a920b5bSAndy Whitcroft	my $stashline="";
1374c2fdda0dSAndy Whitcroft	my $stashrawline="";
13750a920b5bSAndy Whitcroft
13764a0df2efSAndy Whitcroft	my $length;
13770a920b5bSAndy Whitcroft	my $indent;
13780a920b5bSAndy Whitcroft	my $previndent=0;
13790a920b5bSAndy Whitcroft	my $stashindent=0;
13800a920b5bSAndy Whitcroft
1381de7d4f0eSAndy Whitcroft	our $clean = 1;
13820a920b5bSAndy Whitcroft	my $signoff = 0;
13830a920b5bSAndy Whitcroft	my $is_patch = 0;
13840a920b5bSAndy Whitcroft
138515662b3eSJoe Perches	my $in_header_lines = 1;
138615662b3eSJoe Perches	my $in_commit_log = 0;		#Scanning lines before patch
138715662b3eSJoe Perches
1388fa64205dSPasi Savanainen	my $non_utf8_charset = 0;
1389fa64205dSPasi Savanainen
139013214adfSAndy Whitcroft	our @report = ();
13916c72ffaaSAndy Whitcroft	our $cnt_lines = 0;
13926c72ffaaSAndy Whitcroft	our $cnt_error = 0;
13936c72ffaaSAndy Whitcroft	our $cnt_warn = 0;
13946c72ffaaSAndy Whitcroft	our $cnt_chk = 0;
13956c72ffaaSAndy Whitcroft
13960a920b5bSAndy Whitcroft	# Trace the real file/line as we go.
13970a920b5bSAndy Whitcroft	my $realfile = '';
13980a920b5bSAndy Whitcroft	my $realline = 0;
13990a920b5bSAndy Whitcroft	my $realcnt = 0;
14000a920b5bSAndy Whitcroft	my $here = '';
14010a920b5bSAndy Whitcroft	my $in_comment = 0;
1402c2fdda0dSAndy Whitcroft	my $comment_edge = 0;
14030a920b5bSAndy Whitcroft	my $first_line = 0;
14041e855726SWolfram Sang	my $p1_prefix = '';
14050a920b5bSAndy Whitcroft
140613214adfSAndy Whitcroft	my $prev_values = 'E';
140713214adfSAndy Whitcroft
140813214adfSAndy Whitcroft	# suppression flags
1409773647a0SAndy Whitcroft	my %suppress_ifbraces;
1410170d3a22SAndy Whitcroft	my %suppress_whiletrailers;
14112b474a1aSAndy Whitcroft	my %suppress_export;
14123e469cdcSAndy Whitcroft	my $suppress_statement = 0;
1413653d4876SAndy Whitcroft
1414323c1260SJoe Perches	my %camelcase = ();
1415323c1260SJoe Perches
1416c2fdda0dSAndy Whitcroft	# Pre-scan the patch sanitizing the lines.
1417de7d4f0eSAndy Whitcroft	# Pre-scan the patch looking for any __setup documentation.
1418c2fdda0dSAndy Whitcroft	#
1419de7d4f0eSAndy Whitcroft	my @setup_docs = ();
1420de7d4f0eSAndy Whitcroft	my $setup_docs = 0;
1421773647a0SAndy Whitcroft
1422773647a0SAndy Whitcroft	sanitise_line_reset();
1423c2fdda0dSAndy Whitcroft	my $line;
1424c2fdda0dSAndy Whitcroft	foreach my $rawline (@rawlines) {
1425773647a0SAndy Whitcroft		$linenr++;
1426773647a0SAndy Whitcroft		$line = $rawline;
1427c2fdda0dSAndy Whitcroft
1428773647a0SAndy Whitcroft		if ($rawline=~/^\+\+\+\s+(\S+)/) {
1429de7d4f0eSAndy Whitcroft			$setup_docs = 0;
1430de7d4f0eSAndy Whitcroft			if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
1431de7d4f0eSAndy Whitcroft				$setup_docs = 1;
1432de7d4f0eSAndy Whitcroft			}
1433773647a0SAndy Whitcroft			#next;
1434de7d4f0eSAndy Whitcroft		}
1435773647a0SAndy Whitcroft		if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1436773647a0SAndy Whitcroft			$realline=$1-1;
1437773647a0SAndy Whitcroft			if (defined $2) {
1438773647a0SAndy Whitcroft				$realcnt=$3+1;
1439773647a0SAndy Whitcroft			} else {
1440773647a0SAndy Whitcroft				$realcnt=1+1;
1441773647a0SAndy Whitcroft			}
1442c45dcabdSAndy Whitcroft			$in_comment = 0;
1443773647a0SAndy Whitcroft
1444773647a0SAndy Whitcroft			# Guestimate if this is a continuing comment.  Run
1445773647a0SAndy Whitcroft			# the context looking for a comment "edge".  If this
1446773647a0SAndy Whitcroft			# edge is a close comment then we must be in a comment
1447773647a0SAndy Whitcroft			# at context start.
1448773647a0SAndy Whitcroft			my $edge;
144901fa9147SAndy Whitcroft			my $cnt = $realcnt;
145001fa9147SAndy Whitcroft			for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
145101fa9147SAndy Whitcroft				next if (defined $rawlines[$ln - 1] &&
145201fa9147SAndy Whitcroft					 $rawlines[$ln - 1] =~ /^-/);
145301fa9147SAndy Whitcroft				$cnt--;
145401fa9147SAndy Whitcroft				#print "RAW<$rawlines[$ln - 1]>\n";
1455721c1cb6SAndy Whitcroft				last if (!defined $rawlines[$ln - 1]);
1456fae17daeSAndy Whitcroft				if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
1457fae17daeSAndy Whitcroft				    $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
1458fae17daeSAndy Whitcroft					($edge) = $1;
1459fae17daeSAndy Whitcroft					last;
1460fae17daeSAndy Whitcroft				}
1461773647a0SAndy Whitcroft			}
1462773647a0SAndy Whitcroft			if (defined $edge && $edge eq '*/') {
1463773647a0SAndy Whitcroft				$in_comment = 1;
1464773647a0SAndy Whitcroft			}
1465773647a0SAndy Whitcroft
1466773647a0SAndy Whitcroft			# Guestimate if this is a continuing comment.  If this
1467773647a0SAndy Whitcroft			# is the start of a diff block and this line starts
1468773647a0SAndy Whitcroft			# ' *' then it is very likely a comment.
1469773647a0SAndy Whitcroft			if (!defined $edge &&
147083242e0cSAndy Whitcroft			    $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
1471773647a0SAndy Whitcroft			{
1472773647a0SAndy Whitcroft				$in_comment = 1;
1473773647a0SAndy Whitcroft			}
1474773647a0SAndy Whitcroft
1475773647a0SAndy Whitcroft			##print "COMMENT:$in_comment edge<$edge> $rawline\n";
1476773647a0SAndy Whitcroft			sanitise_line_reset($in_comment);
1477773647a0SAndy Whitcroft
1478171ae1a4SAndy Whitcroft		} elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
1479773647a0SAndy Whitcroft			# Standardise the strings and chars within the input to
1480171ae1a4SAndy Whitcroft			# simplify matching -- only bother with positive lines.
1481773647a0SAndy Whitcroft			$line = sanitise_line($rawline);
1482773647a0SAndy Whitcroft		}
1483773647a0SAndy Whitcroft		push(@lines, $line);
1484773647a0SAndy Whitcroft
1485773647a0SAndy Whitcroft		if ($realcnt > 1) {
1486773647a0SAndy Whitcroft			$realcnt-- if ($line =~ /^(?:\+| |$)/);
1487773647a0SAndy Whitcroft		} else {
1488773647a0SAndy Whitcroft			$realcnt = 0;
1489773647a0SAndy Whitcroft		}
1490773647a0SAndy Whitcroft
1491773647a0SAndy Whitcroft		#print "==>$rawline\n";
1492773647a0SAndy Whitcroft		#print "-->$line\n";
1493de7d4f0eSAndy Whitcroft
1494de7d4f0eSAndy Whitcroft		if ($setup_docs && $line =~ /^\+/) {
1495de7d4f0eSAndy Whitcroft			push(@setup_docs, $line);
1496de7d4f0eSAndy Whitcroft		}
1497de7d4f0eSAndy Whitcroft	}
1498de7d4f0eSAndy Whitcroft
14996c72ffaaSAndy Whitcroft	$prefix = '';
15006c72ffaaSAndy Whitcroft
1501773647a0SAndy Whitcroft	$realcnt = 0;
1502773647a0SAndy Whitcroft	$linenr = 0;
15030a920b5bSAndy Whitcroft	foreach my $line (@lines) {
15040a920b5bSAndy Whitcroft		$linenr++;
15050a920b5bSAndy Whitcroft
1506c2fdda0dSAndy Whitcroft		my $rawline = $rawlines[$linenr - 1];
15076c72ffaaSAndy Whitcroft
15080a920b5bSAndy Whitcroft#extract the line range in the file after the patch is applied
15096c72ffaaSAndy Whitcroft		if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
15100a920b5bSAndy Whitcroft			$is_patch = 1;
15114a0df2efSAndy Whitcroft			$first_line = $linenr + 1;
15120a920b5bSAndy Whitcroft			$realline=$1-1;
15130a920b5bSAndy Whitcroft			if (defined $2) {
15140a920b5bSAndy Whitcroft				$realcnt=$3+1;
15150a920b5bSAndy Whitcroft			} else {
15160a920b5bSAndy Whitcroft				$realcnt=1+1;
15170a920b5bSAndy Whitcroft			}
1518c2fdda0dSAndy Whitcroft			annotate_reset();
151913214adfSAndy Whitcroft			$prev_values = 'E';
152013214adfSAndy Whitcroft
1521773647a0SAndy Whitcroft			%suppress_ifbraces = ();
1522170d3a22SAndy Whitcroft			%suppress_whiletrailers = ();
15232b474a1aSAndy Whitcroft			%suppress_export = ();
15243e469cdcSAndy Whitcroft			$suppress_statement = 0;
15250a920b5bSAndy Whitcroft			next;
15260a920b5bSAndy Whitcroft
15274a0df2efSAndy Whitcroft# track the line number as we move through the hunk, note that
15284a0df2efSAndy Whitcroft# new versions of GNU diff omit the leading space on completely
15294a0df2efSAndy Whitcroft# blank context lines so we need to count that too.
1530773647a0SAndy Whitcroft		} elsif ($line =~ /^( |\+|$)/) {
15310a920b5bSAndy Whitcroft			$realline++;
1532d8aaf121SAndy Whitcroft			$realcnt-- if ($realcnt != 0);
15330a920b5bSAndy Whitcroft
15344a0df2efSAndy Whitcroft			# Measure the line length and indent.
1535c2fdda0dSAndy Whitcroft			($length, $indent) = line_stats($rawline);
15360a920b5bSAndy Whitcroft
15370a920b5bSAndy Whitcroft			# Track the previous line.
15380a920b5bSAndy Whitcroft			($prevline, $stashline) = ($stashline, $line);
15390a920b5bSAndy Whitcroft			($previndent, $stashindent) = ($stashindent, $indent);
1540c2fdda0dSAndy Whitcroft			($prevrawline, $stashrawline) = ($stashrawline, $rawline);
1541c2fdda0dSAndy Whitcroft
1542773647a0SAndy Whitcroft			#warn "line<$line>\n";
15436c72ffaaSAndy Whitcroft
1544d8aaf121SAndy Whitcroft		} elsif ($realcnt == 1) {
1545d8aaf121SAndy Whitcroft			$realcnt--;
15460a920b5bSAndy Whitcroft		}
15470a920b5bSAndy Whitcroft
1548cc77cdcaSAndy Whitcroft		my $hunk_line = ($realcnt != 0);
1549cc77cdcaSAndy Whitcroft
15500a920b5bSAndy Whitcroft#make up the handle for any error we report on this line
1551773647a0SAndy Whitcroft		$prefix = "$filename:$realline: " if ($emacs && $file);
1552773647a0SAndy Whitcroft		$prefix = "$filename:$linenr: " if ($emacs && !$file);
1553773647a0SAndy Whitcroft
15546c72ffaaSAndy Whitcroft		$here = "#$linenr: " if (!$file);
15556c72ffaaSAndy Whitcroft		$here = "#$realline: " if ($file);
1556773647a0SAndy Whitcroft
1557773647a0SAndy Whitcroft		# extract the filename as it passes
15583bf9a009SRabin Vincent		if ($line =~ /^diff --git.*?(\S+)$/) {
15593bf9a009SRabin Vincent			$realfile = $1;
15603bf9a009SRabin Vincent			$realfile =~ s@^([^/]*)/@@;
1561270c49a0SJoe Perches			$in_commit_log = 0;
15623bf9a009SRabin Vincent		} elsif ($line =~ /^\+\+\+\s+(\S+)/) {
1563773647a0SAndy Whitcroft			$realfile = $1;
15641e855726SWolfram Sang			$realfile =~ s@^([^/]*)/@@;
1565270c49a0SJoe Perches			$in_commit_log = 0;
15661e855726SWolfram Sang
15671e855726SWolfram Sang			$p1_prefix = $1;
1568e2f7aa4bSAndy Whitcroft			if (!$file && $tree && $p1_prefix ne '' &&
1569e2f7aa4bSAndy Whitcroft			    -e "$root/$p1_prefix") {
1570000d1cc1SJoe Perches				WARN("PATCH_PREFIX",
1571000d1cc1SJoe Perches				     "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
15721e855726SWolfram Sang			}
1573773647a0SAndy Whitcroft
1574c1ab3326SAndy Whitcroft			if ($realfile =~ m@^include/asm/@) {
1575000d1cc1SJoe Perches				ERROR("MODIFIED_INCLUDE_ASM",
1576000d1cc1SJoe Perches				      "do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
1577773647a0SAndy Whitcroft			}
1578773647a0SAndy Whitcroft			next;
1579773647a0SAndy Whitcroft		}
1580773647a0SAndy Whitcroft
1581389834b6SRandy Dunlap		$here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
15820a920b5bSAndy Whitcroft
1583c2fdda0dSAndy Whitcroft		my $hereline = "$here\n$rawline\n";
1584c2fdda0dSAndy Whitcroft		my $herecurr = "$here\n$rawline\n";
1585c2fdda0dSAndy Whitcroft		my $hereprev = "$here\n$prevrawline\n$rawline\n";
15860a920b5bSAndy Whitcroft
15876c72ffaaSAndy Whitcroft		$cnt_lines++ if ($realcnt != 0);
15886c72ffaaSAndy Whitcroft
15893bf9a009SRabin Vincent# Check for incorrect file permissions
15903bf9a009SRabin Vincent		if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
15913bf9a009SRabin Vincent			my $permhere = $here . "FILE: $realfile\n";
159204db4d25SJoe Perches			if ($realfile !~ m@scripts/@ &&
159304db4d25SJoe Perches			    $realfile !~ /\.(py|pl|awk|sh)$/) {
1594000d1cc1SJoe Perches				ERROR("EXECUTE_PERMISSIONS",
1595000d1cc1SJoe Perches				      "do not set execute permissions for source files\n" . $permhere);
15963bf9a009SRabin Vincent			}
15973bf9a009SRabin Vincent		}
15983bf9a009SRabin Vincent
159920112475SJoe Perches# Check the patch for a signoff:
1600d8aaf121SAndy Whitcroft		if ($line =~ /^\s*signed-off-by:/i) {
16014a0df2efSAndy Whitcroft			$signoff++;
160215662b3eSJoe Perches			$in_commit_log = 0;
16030a920b5bSAndy Whitcroft		}
160420112475SJoe Perches
160520112475SJoe Perches# Check signature styles
1606270c49a0SJoe Perches		if (!$in_header_lines &&
1607ce0338dfSJoe Perches		    $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
160820112475SJoe Perches			my $space_before = $1;
160920112475SJoe Perches			my $sign_off = $2;
161020112475SJoe Perches			my $space_after = $3;
161120112475SJoe Perches			my $email = $4;
161220112475SJoe Perches			my $ucfirst_sign_off = ucfirst(lc($sign_off));
161320112475SJoe Perches
1614ce0338dfSJoe Perches			if ($sign_off !~ /$signature_tags/) {
1615ce0338dfSJoe Perches				WARN("BAD_SIGN_OFF",
1616ce0338dfSJoe Perches				     "Non-standard signature: $sign_off\n" . $herecurr);
1617ce0338dfSJoe Perches			}
161820112475SJoe Perches			if (defined $space_before && $space_before ne "") {
1619000d1cc1SJoe Perches				WARN("BAD_SIGN_OFF",
1620000d1cc1SJoe Perches				     "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr);
162120112475SJoe Perches			}
162220112475SJoe Perches			if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
1623000d1cc1SJoe Perches				WARN("BAD_SIGN_OFF",
1624000d1cc1SJoe Perches				     "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr);
162520112475SJoe Perches			}
162620112475SJoe Perches			if (!defined $space_after || $space_after ne " ") {
1627000d1cc1SJoe Perches				WARN("BAD_SIGN_OFF",
1628000d1cc1SJoe Perches				     "Use a single space after $ucfirst_sign_off\n" . $herecurr);
162920112475SJoe Perches			}
163020112475SJoe Perches
163120112475SJoe Perches			my ($email_name, $email_address, $comment) = parse_email($email);
163220112475SJoe Perches			my $suggested_email = format_email(($email_name, $email_address));
163320112475SJoe Perches			if ($suggested_email eq "") {
1634000d1cc1SJoe Perches				ERROR("BAD_SIGN_OFF",
1635000d1cc1SJoe Perches				      "Unrecognized email address: '$email'\n" . $herecurr);
163620112475SJoe Perches			} else {
163720112475SJoe Perches				my $dequoted = $suggested_email;
163820112475SJoe Perches				$dequoted =~ s/^"//;
163920112475SJoe Perches				$dequoted =~ s/" </ </;
164020112475SJoe Perches				# Don't force email to have quotes
164120112475SJoe Perches				# Allow just an angle bracketed address
164220112475SJoe Perches				if ("$dequoted$comment" ne $email &&
164320112475SJoe Perches				    "<$email_address>$comment" ne $email &&
164420112475SJoe Perches				    "$suggested_email$comment" ne $email) {
1645000d1cc1SJoe Perches					WARN("BAD_SIGN_OFF",
1646000d1cc1SJoe Perches					     "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
164720112475SJoe Perches				}
16480a920b5bSAndy Whitcroft			}
16490a920b5bSAndy Whitcroft		}
16500a920b5bSAndy Whitcroft
165100df344fSAndy Whitcroft# Check for wrappage within a valid hunk of the file
16528905a67cSAndy Whitcroft		if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
1653000d1cc1SJoe Perches			ERROR("CORRUPTED_PATCH",
1654000d1cc1SJoe Perches			      "patch seems to be corrupt (line wrapped?)\n" .
16556c72ffaaSAndy Whitcroft				$herecurr) if (!$emitted_corrupt++);
1656de7d4f0eSAndy Whitcroft		}
1657de7d4f0eSAndy Whitcroft
16586ecd9674SAndy Whitcroft# Check for absolute kernel paths.
16596ecd9674SAndy Whitcroft		if ($tree) {
16606ecd9674SAndy Whitcroft			while ($line =~ m{(?:^|\s)(/\S*)}g) {
16616ecd9674SAndy Whitcroft				my $file = $1;
16626ecd9674SAndy Whitcroft
16636ecd9674SAndy Whitcroft				if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
16646ecd9674SAndy Whitcroft				    check_absolute_file($1, $herecurr)) {
16656ecd9674SAndy Whitcroft					#
16666ecd9674SAndy Whitcroft				} else {
16676ecd9674SAndy Whitcroft					check_absolute_file($file, $herecurr);
16686ecd9674SAndy Whitcroft				}
16696ecd9674SAndy Whitcroft			}
16706ecd9674SAndy Whitcroft		}
16716ecd9674SAndy Whitcroft
1672de7d4f0eSAndy Whitcroft# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
1673de7d4f0eSAndy Whitcroft		if (($realfile =~ /^$/ || $line =~ /^\+/) &&
1674171ae1a4SAndy Whitcroft		    $rawline !~ m/^$UTF8*$/) {
1675171ae1a4SAndy Whitcroft			my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
1676171ae1a4SAndy Whitcroft
1677171ae1a4SAndy Whitcroft			my $blank = copy_spacing($rawline);
1678171ae1a4SAndy Whitcroft			my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
1679171ae1a4SAndy Whitcroft			my $hereptr = "$hereline$ptr\n";
1680171ae1a4SAndy Whitcroft
168134d99219SJoe Perches			CHK("INVALID_UTF8",
1682000d1cc1SJoe Perches			    "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
168300df344fSAndy Whitcroft		}
16840a920b5bSAndy Whitcroft
168515662b3eSJoe Perches# Check if it's the start of a commit log
168615662b3eSJoe Perches# (not a header line and we haven't seen the patch filename)
168715662b3eSJoe Perches		if ($in_header_lines && $realfile =~ /^$/ &&
1688270c49a0SJoe Perches		    $rawline !~ /^(commit\b|from\b|[\w-]+:).+$/i) {
168915662b3eSJoe Perches			$in_header_lines = 0;
169015662b3eSJoe Perches			$in_commit_log = 1;
169115662b3eSJoe Perches		}
169215662b3eSJoe Perches
1693fa64205dSPasi Savanainen# Check if there is UTF-8 in a commit log when a mail header has explicitly
1694fa64205dSPasi Savanainen# declined it, i.e defined some charset where it is missing.
1695fa64205dSPasi Savanainen		if ($in_header_lines &&
1696fa64205dSPasi Savanainen		    $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
1697fa64205dSPasi Savanainen		    $1 !~ /utf-8/i) {
1698fa64205dSPasi Savanainen			$non_utf8_charset = 1;
1699fa64205dSPasi Savanainen		}
1700fa64205dSPasi Savanainen
1701fa64205dSPasi Savanainen		if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
170215662b3eSJoe Perches		    $rawline =~ /$NON_ASCII_UTF8/) {
1703fa64205dSPasi Savanainen			WARN("UTF8_BEFORE_PATCH",
170415662b3eSJoe Perches			    "8-bit UTF-8 used in possible commit log\n" . $herecurr);
170515662b3eSJoe Perches		}
170615662b3eSJoe Perches
170730670854SAndy Whitcroft# ignore non-hunk lines and lines being removed
170830670854SAndy Whitcroft		next if (!$hunk_line || $line =~ /^-/);
170900df344fSAndy Whitcroft
17100a920b5bSAndy Whitcroft#trailing whitespace
17119c0ca6f9SAndy Whitcroft		if ($line =~ /^\+.*\015/) {
1712c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1713000d1cc1SJoe Perches			ERROR("DOS_LINE_ENDINGS",
1714000d1cc1SJoe Perches			      "DOS line endings\n" . $herevet);
17159c0ca6f9SAndy Whitcroft
1716c2fdda0dSAndy Whitcroft		} elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
1717c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1718000d1cc1SJoe Perches			ERROR("TRAILING_WHITESPACE",
1719000d1cc1SJoe Perches			      "trailing whitespace\n" . $herevet);
1720d2c0a235SAndy Whitcroft			$rpt_cleaners = 1;
17210a920b5bSAndy Whitcroft		}
17225368df20SAndy Whitcroft
17233354957aSAndi Kleen# check for Kconfig help text having a real description
17249fe287d7SAndy Whitcroft# Only applies when adding the entry originally, after that we do not have
17259fe287d7SAndy Whitcroft# sufficient context to determine whether it is indeed long enough.
17263354957aSAndi Kleen		if ($realfile =~ /Kconfig/ &&
1727a1385803SAndy Whitcroft		    $line =~ /.\s*config\s+/) {
17283354957aSAndi Kleen			my $length = 0;
17299fe287d7SAndy Whitcroft			my $cnt = $realcnt;
17309fe287d7SAndy Whitcroft			my $ln = $linenr + 1;
17319fe287d7SAndy Whitcroft			my $f;
1732a1385803SAndy Whitcroft			my $is_start = 0;
17339fe287d7SAndy Whitcroft			my $is_end = 0;
1734a1385803SAndy Whitcroft			for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
17359fe287d7SAndy Whitcroft				$f = $lines[$ln - 1];
17369fe287d7SAndy Whitcroft				$cnt-- if ($lines[$ln - 1] !~ /^-/);
17379fe287d7SAndy Whitcroft				$is_end = $lines[$ln - 1] =~ /^\+/;
17389fe287d7SAndy Whitcroft
17399fe287d7SAndy Whitcroft				next if ($f =~ /^-/);
1740a1385803SAndy Whitcroft
1741a1385803SAndy Whitcroft				if ($lines[$ln - 1] =~ /.\s*(?:bool|tristate)\s*\"/) {
1742a1385803SAndy Whitcroft					$is_start = 1;
1743a1385803SAndy Whitcroft				} elsif ($lines[$ln - 1] =~ /.\s*(?:---)?help(?:---)?$/) {
1744a1385803SAndy Whitcroft					$length = -1;
1745a1385803SAndy Whitcroft				}
1746a1385803SAndy Whitcroft
17479fe287d7SAndy Whitcroft				$f =~ s/^.//;
17483354957aSAndi Kleen				$f =~ s/#.*//;
17493354957aSAndi Kleen				$f =~ s/^\s+//;
17503354957aSAndi Kleen				next if ($f =~ /^$/);
17519fe287d7SAndy Whitcroft				if ($f =~ /^\s*config\s/) {
17529fe287d7SAndy Whitcroft					$is_end = 1;
17539fe287d7SAndy Whitcroft					last;
17549fe287d7SAndy Whitcroft				}
17553354957aSAndi Kleen				$length++;
17563354957aSAndi Kleen			}
1757000d1cc1SJoe Perches			WARN("CONFIG_DESCRIPTION",
1758a1385803SAndy Whitcroft			     "please write a paragraph that describes the config symbol fully\n" . $herecurr) if ($is_start && $is_end && $length < 4);
1759a1385803SAndy Whitcroft			#print "is_start<$is_start> is_end<$is_end> length<$length>\n";
17603354957aSAndi Kleen		}
17613354957aSAndi Kleen
17621ba8dfd1SKees Cook# discourage the addition of CONFIG_EXPERIMENTAL in Kconfig.
17631ba8dfd1SKees Cook		if ($realfile =~ /Kconfig/ &&
17641ba8dfd1SKees Cook		    $line =~ /.\s*depends on\s+.*\bEXPERIMENTAL\b/) {
17651ba8dfd1SKees Cook			WARN("CONFIG_EXPERIMENTAL",
17661ba8dfd1SKees Cook			     "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
17671ba8dfd1SKees Cook		}
17681ba8dfd1SKees Cook
1769c68e5878SArnaud Lacombe		if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
1770c68e5878SArnaud Lacombe		    ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
1771c68e5878SArnaud Lacombe			my $flag = $1;
1772c68e5878SArnaud Lacombe			my $replacement = {
1773c68e5878SArnaud Lacombe				'EXTRA_AFLAGS' =>   'asflags-y',
1774c68e5878SArnaud Lacombe				'EXTRA_CFLAGS' =>   'ccflags-y',
1775c68e5878SArnaud Lacombe				'EXTRA_CPPFLAGS' => 'cppflags-y',
1776c68e5878SArnaud Lacombe				'EXTRA_LDFLAGS' =>  'ldflags-y',
1777c68e5878SArnaud Lacombe			};
1778c68e5878SArnaud Lacombe
1779c68e5878SArnaud Lacombe			WARN("DEPRECATED_VARIABLE",
1780c68e5878SArnaud Lacombe			     "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
1781c68e5878SArnaud Lacombe		}
1782c68e5878SArnaud Lacombe
17835368df20SAndy Whitcroft# check we are in a valid source file if not then ignore this hunk
17845368df20SAndy Whitcroft		next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
17855368df20SAndy Whitcroft
17866cd7f386SJoe Perches#line length limit
1787c45dcabdSAndy Whitcroft		if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ &&
1788f4c014c0SAndy Whitcroft		    $rawline !~ /^.\s*\*\s*\@$Ident\s/ &&
17890fccc622SJoe Perches		    !($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(KERN_\S+\s*|[^"]*))?"[X\t]*"\s*(?:|,|\)\s*;)\s*$/ ||
17908bbea968SJoe Perches		    $line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) &&
17916cd7f386SJoe Perches		    $length > $max_line_length)
1792c45dcabdSAndy Whitcroft		{
1793000d1cc1SJoe Perches			WARN("LONG_LINE",
17946cd7f386SJoe Perches			     "line over $max_line_length characters\n" . $herecurr);
17950a920b5bSAndy Whitcroft		}
17960a920b5bSAndy Whitcroft
1797ca56dc09SJosh Triplett# Check for user-visible strings broken across lines, which breaks the ability
1798ca56dc09SJosh Triplett# to grep for the string.  Limited to strings used as parameters (those
1799ca56dc09SJosh Triplett# following an open parenthesis), which almost completely eliminates false
1800ca56dc09SJosh Triplett# positives, as well as warning only once per parameter rather than once per
1801ca56dc09SJosh Triplett# line of the string.  Make an exception when the previous string ends in a
1802ca56dc09SJosh Triplett# newline (multiple lines in one string constant) or \n\t (common in inline
1803ca56dc09SJosh Triplett# assembly to indent the instruction on the following line).
1804ca56dc09SJosh Triplett		if ($line =~ /^\+\s*"/ &&
1805ca56dc09SJosh Triplett		    $prevline =~ /"\s*$/ &&
1806ca56dc09SJosh Triplett		    $prevline =~ /\(/ &&
1807ca56dc09SJosh Triplett		    $prevrawline !~ /\\n(?:\\t)*"\s*$/) {
1808ca56dc09SJosh Triplett			WARN("SPLIT_STRING",
1809ca56dc09SJosh Triplett			     "quoted string split across lines\n" . $hereprev);
1810ca56dc09SJosh Triplett		}
1811ca56dc09SJosh Triplett
18125e79d96eSJoe Perches# check for spaces before a quoted newline
18135e79d96eSJoe Perches		if ($rawline =~ /^.*\".*\s\\n/) {
1814000d1cc1SJoe Perches			WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
1815000d1cc1SJoe Perches			     "unnecessary whitespace before a quoted newline\n" . $herecurr);
18165e79d96eSJoe Perches		}
18175e79d96eSJoe Perches
18188905a67cSAndy Whitcroft# check for adding lines without a newline.
18198905a67cSAndy Whitcroft		if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
1820000d1cc1SJoe Perches			WARN("MISSING_EOF_NEWLINE",
1821000d1cc1SJoe Perches			     "adding a line without newline at end of file\n" . $herecurr);
18228905a67cSAndy Whitcroft		}
18238905a67cSAndy Whitcroft
182442e41c54SMike Frysinger# Blackfin: use hi/lo macros
182542e41c54SMike Frysinger		if ($realfile =~ m@arch/blackfin/.*\.S$@) {
182642e41c54SMike Frysinger			if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
182742e41c54SMike Frysinger				my $herevet = "$here\n" . cat_vet($line) . "\n";
1828000d1cc1SJoe Perches				ERROR("LO_MACRO",
1829000d1cc1SJoe Perches				      "use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
183042e41c54SMike Frysinger			}
183142e41c54SMike Frysinger			if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
183242e41c54SMike Frysinger				my $herevet = "$here\n" . cat_vet($line) . "\n";
1833000d1cc1SJoe Perches				ERROR("HI_MACRO",
1834000d1cc1SJoe Perches				      "use the HI() macro, not (... >> 16)\n" . $herevet);
183542e41c54SMike Frysinger			}
183642e41c54SMike Frysinger		}
183742e41c54SMike Frysinger
1838b9ea10d6SAndy Whitcroft# check we are in a valid source file C or perl if not then ignore this hunk
1839b9ea10d6SAndy Whitcroft		next if ($realfile !~ /\.(h|c|pl)$/);
18400a920b5bSAndy Whitcroft
18410a920b5bSAndy Whitcroft# at the beginning of a line any tabs must come first and anything
18420a920b5bSAndy Whitcroft# more than 8 must use tabs.
1843c2fdda0dSAndy Whitcroft		if ($rawline =~ /^\+\s* \t\s*\S/ ||
1844c2fdda0dSAndy Whitcroft		    $rawline =~ /^\+\s*        \s*/) {
1845c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1846000d1cc1SJoe Perches			ERROR("CODE_INDENT",
1847000d1cc1SJoe Perches			      "code indent should use tabs where possible\n" . $herevet);
1848d2c0a235SAndy Whitcroft			$rpt_cleaners = 1;
18490a920b5bSAndy Whitcroft		}
18500a920b5bSAndy Whitcroft
185108e44365SAlberto Panizzo# check for space before tabs.
185208e44365SAlberto Panizzo		if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
185308e44365SAlberto Panizzo			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1854000d1cc1SJoe Perches			WARN("SPACE_BEFORE_TAB",
1855000d1cc1SJoe Perches			     "please, no space before tabs\n" . $herevet);
185608e44365SAlberto Panizzo		}
185708e44365SAlberto Panizzo
1858d1fe9c09SJoe Perches# check for && or || at the start of a line
1859d1fe9c09SJoe Perches		if ($rawline =~ /^\+\s*(&&|\|\|)/) {
1860d1fe9c09SJoe Perches			CHK("LOGICAL_CONTINUATIONS",
1861d1fe9c09SJoe Perches			    "Logical continuations should be on the previous line\n" . $hereprev);
1862d1fe9c09SJoe Perches		}
1863d1fe9c09SJoe Perches
1864d1fe9c09SJoe Perches# check multi-line statement indentation matches previous line
1865d1fe9c09SJoe Perches		if ($^V && $^V ge 5.10.0 &&
1866d1fe9c09SJoe Perches		    $prevline =~ /^\+(\t*)(if \(|$Ident\().*(\&\&|\|\||,)\s*$/) {
1867d1fe9c09SJoe Perches			$prevline =~ /^\+(\t*)(.*)$/;
1868d1fe9c09SJoe Perches			my $oldindent = $1;
1869d1fe9c09SJoe Perches			my $rest = $2;
1870d1fe9c09SJoe Perches
1871d1fe9c09SJoe Perches			my $pos = pos_last_openparen($rest);
1872d1fe9c09SJoe Perches			if ($pos >= 0) {
1873b34a26f3SJoe Perches				$line =~ /^(\+| )([ \t]*)/;
1874b34a26f3SJoe Perches				my $newindent = $2;
1875d1fe9c09SJoe Perches
1876d1fe9c09SJoe Perches				my $goodtabindent = $oldindent .
1877d1fe9c09SJoe Perches					"\t" x ($pos / 8) .
1878d1fe9c09SJoe Perches					" "  x ($pos % 8);
1879d1fe9c09SJoe Perches				my $goodspaceindent = $oldindent . " "  x $pos;
1880d1fe9c09SJoe Perches
1881d1fe9c09SJoe Perches				if ($newindent ne $goodtabindent &&
1882d1fe9c09SJoe Perches				    $newindent ne $goodspaceindent) {
1883d1fe9c09SJoe Perches					CHK("PARENTHESIS_ALIGNMENT",
1884d1fe9c09SJoe Perches					    "Alignment should match open parenthesis\n" . $hereprev);
1885d1fe9c09SJoe Perches				}
1886d1fe9c09SJoe Perches			}
1887d1fe9c09SJoe Perches		}
1888d1fe9c09SJoe Perches
1889*23f780c9SJoe Perches		if ($line =~ /^\+.*\*[ \t]*\)[ \t]+(?!$Assignment|$Arithmetic)/) {
1890aad4f614SJoe Perches			CHK("SPACING",
1891aad4f614SJoe Perches			    "No space is necessary after a cast\n" . $hereprev);
1892aad4f614SJoe Perches		}
1893aad4f614SJoe Perches
189405880600SJoe Perches		if ($realfile =~ m@^(drivers/net/|net/)@ &&
1895fdb4bcd6SJoe Perches		    $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
1896fdb4bcd6SJoe Perches		    $rawline =~ /^\+[ \t]*\*/) {
189705880600SJoe Perches			WARN("NETWORKING_BLOCK_COMMENT_STYLE",
189805880600SJoe Perches			     "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
189905880600SJoe Perches		}
190005880600SJoe Perches
190105880600SJoe Perches		if ($realfile =~ m@^(drivers/net/|net/)@ &&
1902a605e32eSJoe Perches		    $prevrawline =~ /^\+[ \t]*\/\*/ &&		#starting /*
1903a605e32eSJoe Perches		    $prevrawline !~ /\*\/[ \t]*$/ &&		#no trailing */
1904a605e32eSJoe Perches		    $rawline !~ /^\+[ \t]*\*/) {		#no leading *
1905a605e32eSJoe Perches			WARN("NETWORKING_BLOCK_COMMENT_STYLE",
1906a605e32eSJoe Perches			     "networking block comments start with * on subsequent lines\n" . $hereprev);
1907a605e32eSJoe Perches		}
1908a605e32eSJoe Perches
1909a605e32eSJoe Perches		if ($realfile =~ m@^(drivers/net/|net/)@ &&
1910c24f9f19SJoe Perches		    $rawline !~ m@^\+[ \t]*\*/[ \t]*$@ &&	#trailing */
1911c24f9f19SJoe Perches		    $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ &&	#inline /*...*/
1912c24f9f19SJoe Perches		    $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ &&	#trailing **/
1913c24f9f19SJoe Perches		    $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) {	#non blank */
191405880600SJoe Perches			WARN("NETWORKING_BLOCK_COMMENT_STYLE",
191505880600SJoe Perches			     "networking block comments put the trailing */ on a separate line\n" . $herecurr);
191605880600SJoe Perches		}
191705880600SJoe Perches
19185f7ddae6SRaffaele Recalcati# check for spaces at the beginning of a line.
19196b4c5bebSAndy Whitcroft# Exceptions:
19206b4c5bebSAndy Whitcroft#  1) within comments
19216b4c5bebSAndy Whitcroft#  2) indented preprocessor commands
19226b4c5bebSAndy Whitcroft#  3) hanging labels
19236b4c5bebSAndy Whitcroft		if ($rawline =~ /^\+ / && $line !~ /\+ *(?:$;|#|$Ident:)/)  {
19245f7ddae6SRaffaele Recalcati			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1925000d1cc1SJoe Perches			WARN("LEADING_SPACE",
1926000d1cc1SJoe Perches			     "please, no spaces at the start of a line\n" . $herevet);
19275f7ddae6SRaffaele Recalcati		}
19285f7ddae6SRaffaele Recalcati
1929b9ea10d6SAndy Whitcroft# check we are in a valid C source file if not then ignore this hunk
1930b9ea10d6SAndy Whitcroft		next if ($realfile !~ /\.(h|c)$/);
1931b9ea10d6SAndy Whitcroft
19321ba8dfd1SKees Cook# discourage the addition of CONFIG_EXPERIMENTAL in #if(def).
19331ba8dfd1SKees Cook		if ($line =~ /^\+\s*\#\s*if.*\bCONFIG_EXPERIMENTAL\b/) {
19341ba8dfd1SKees Cook			WARN("CONFIG_EXPERIMENTAL",
19351ba8dfd1SKees Cook			     "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
19361ba8dfd1SKees Cook		}
19371ba8dfd1SKees Cook
1938c2fdda0dSAndy Whitcroft# check for RCS/CVS revision markers
1939cf655043SAndy Whitcroft		if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
1940000d1cc1SJoe Perches			WARN("CVS_KEYWORD",
1941000d1cc1SJoe Perches			     "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
1942c2fdda0dSAndy Whitcroft		}
194322f2a2efSAndy Whitcroft
194442e41c54SMike Frysinger# Blackfin: don't use __builtin_bfin_[cs]sync
194542e41c54SMike Frysinger		if ($line =~ /__builtin_bfin_csync/) {
194642e41c54SMike Frysinger			my $herevet = "$here\n" . cat_vet($line) . "\n";
1947000d1cc1SJoe Perches			ERROR("CSYNC",
1948000d1cc1SJoe Perches			      "use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
194942e41c54SMike Frysinger		}
195042e41c54SMike Frysinger		if ($line =~ /__builtin_bfin_ssync/) {
195142e41c54SMike Frysinger			my $herevet = "$here\n" . cat_vet($line) . "\n";
1952000d1cc1SJoe Perches			ERROR("SSYNC",
1953000d1cc1SJoe Perches			      "use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
195442e41c54SMike Frysinger		}
195542e41c54SMike Frysinger
195656e77d70SJoe Perches# check for old HOTPLUG __dev<foo> section markings
195756e77d70SJoe Perches		if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
195856e77d70SJoe Perches			WARN("HOTPLUG_SECTION",
195956e77d70SJoe Perches			     "Using $1 is unnecessary\n" . $herecurr);
196056e77d70SJoe Perches		}
196156e77d70SJoe Perches
19629c0ca6f9SAndy Whitcroft# Check for potential 'bare' types
19632b474a1aSAndy Whitcroft		my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
19642b474a1aSAndy Whitcroft		    $realline_next);
19653e469cdcSAndy Whitcroft#print "LINE<$line>\n";
19663e469cdcSAndy Whitcroft		if ($linenr >= $suppress_statement &&
19673e469cdcSAndy Whitcroft		    $realcnt && $line =~ /.\s*\S/) {
1968170d3a22SAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
1969f5fe35ddSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0);
1970171ae1a4SAndy Whitcroft			$stat =~ s/\n./\n /g;
1971171ae1a4SAndy Whitcroft			$cond =~ s/\n./\n /g;
1972171ae1a4SAndy Whitcroft
19733e469cdcSAndy Whitcroft#print "linenr<$linenr> <$stat>\n";
19743e469cdcSAndy Whitcroft			# If this statement has no statement boundaries within
19753e469cdcSAndy Whitcroft			# it there is no point in retrying a statement scan
19763e469cdcSAndy Whitcroft			# until we hit end of it.
19773e469cdcSAndy Whitcroft			my $frag = $stat; $frag =~ s/;+\s*$//;
19783e469cdcSAndy Whitcroft			if ($frag !~ /(?:{|;)/) {
19793e469cdcSAndy Whitcroft#print "skip<$line_nr_next>\n";
19803e469cdcSAndy Whitcroft				$suppress_statement = $line_nr_next;
19813e469cdcSAndy Whitcroft			}
1982f74bd194SAndy Whitcroft
19832b474a1aSAndy Whitcroft			# Find the real next line.
19842b474a1aSAndy Whitcroft			$realline_next = $line_nr_next;
19852b474a1aSAndy Whitcroft			if (defined $realline_next &&
19862b474a1aSAndy Whitcroft			    (!defined $lines[$realline_next - 1] ||
19872b474a1aSAndy Whitcroft			     substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
19882b474a1aSAndy Whitcroft				$realline_next++;
19892b474a1aSAndy Whitcroft			}
19902b474a1aSAndy Whitcroft
1991171ae1a4SAndy Whitcroft			my $s = $stat;
1992171ae1a4SAndy Whitcroft			$s =~ s/{.*$//s;
1993cf655043SAndy Whitcroft
1994c2fdda0dSAndy Whitcroft			# Ignore goto labels.
1995171ae1a4SAndy Whitcroft			if ($s =~ /$Ident:\*$/s) {
1996c2fdda0dSAndy Whitcroft
1997c2fdda0dSAndy Whitcroft			# Ignore functions being called
1998171ae1a4SAndy Whitcroft			} elsif ($s =~ /^.\s*$Ident\s*\(/s) {
1999c2fdda0dSAndy Whitcroft
2000463f2864SAndy Whitcroft			} elsif ($s =~ /^.\s*else\b/s) {
2001463f2864SAndy Whitcroft
2002c45dcabdSAndy Whitcroft			# declarations always start with types
2003d2506586SAndy 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) {
2004c45dcabdSAndy Whitcroft				my $type = $1;
2005c45dcabdSAndy Whitcroft				$type =~ s/\s+/ /g;
2006c45dcabdSAndy Whitcroft				possible($type, "A:" . $s);
2007c45dcabdSAndy Whitcroft
20086c72ffaaSAndy Whitcroft			# definitions in global scope can only start with types
2009a6a84062SAndy Whitcroft			} elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
2010c45dcabdSAndy Whitcroft				possible($1, "B:" . $s);
2011c2fdda0dSAndy Whitcroft			}
20128905a67cSAndy Whitcroft
20136c72ffaaSAndy Whitcroft			# any (foo ... *) is a pointer cast, and foo is a type
201465863862SAndy Whitcroft			while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
2015c45dcabdSAndy Whitcroft				possible($1, "C:" . $s);
20169c0ca6f9SAndy Whitcroft			}
20178905a67cSAndy Whitcroft
20188905a67cSAndy Whitcroft			# Check for any sort of function declaration.
20198905a67cSAndy Whitcroft			# int foo(something bar, other baz);
20208905a67cSAndy Whitcroft			# void (*store_gdt)(x86_descr_ptr *);
2021171ae1a4SAndy Whitcroft			if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
20228905a67cSAndy Whitcroft				my ($name_len) = length($1);
20238905a67cSAndy Whitcroft
2024cf655043SAndy Whitcroft				my $ctx = $s;
2025773647a0SAndy Whitcroft				substr($ctx, 0, $name_len + 1, '');
20268905a67cSAndy Whitcroft				$ctx =~ s/\)[^\)]*$//;
2027cf655043SAndy Whitcroft
20288905a67cSAndy Whitcroft				for my $arg (split(/\s*,\s*/, $ctx)) {
2029c45dcabdSAndy Whitcroft					if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
20308905a67cSAndy Whitcroft
2031c45dcabdSAndy Whitcroft						possible($1, "D:" . $s);
20328905a67cSAndy Whitcroft					}
20338905a67cSAndy Whitcroft				}
20348905a67cSAndy Whitcroft			}
20358905a67cSAndy Whitcroft
20369c0ca6f9SAndy Whitcroft		}
20379c0ca6f9SAndy Whitcroft
203800df344fSAndy Whitcroft#
203900df344fSAndy Whitcroft# Checks which may be anchored in the context.
204000df344fSAndy Whitcroft#
204100df344fSAndy Whitcroft
204200df344fSAndy Whitcroft# Check for switch () and associated case and default
204300df344fSAndy Whitcroft# statements should be at the same indent.
204400df344fSAndy Whitcroft		if ($line=~/\bswitch\s*\(.*\)/) {
204500df344fSAndy Whitcroft			my $err = '';
204600df344fSAndy Whitcroft			my $sep = '';
204700df344fSAndy Whitcroft			my @ctx = ctx_block_outer($linenr, $realcnt);
204800df344fSAndy Whitcroft			shift(@ctx);
204900df344fSAndy Whitcroft			for my $ctx (@ctx) {
205000df344fSAndy Whitcroft				my ($clen, $cindent) = line_stats($ctx);
205100df344fSAndy Whitcroft				if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
205200df344fSAndy Whitcroft							$indent != $cindent) {
205300df344fSAndy Whitcroft					$err .= "$sep$ctx\n";
205400df344fSAndy Whitcroft					$sep = '';
205500df344fSAndy Whitcroft				} else {
205600df344fSAndy Whitcroft					$sep = "[...]\n";
205700df344fSAndy Whitcroft				}
205800df344fSAndy Whitcroft			}
205900df344fSAndy Whitcroft			if ($err ne '') {
2060000d1cc1SJoe Perches				ERROR("SWITCH_CASE_INDENT_LEVEL",
2061000d1cc1SJoe Perches				      "switch and case should be at the same indent\n$hereline$err");
2062de7d4f0eSAndy Whitcroft			}
2063de7d4f0eSAndy Whitcroft		}
2064de7d4f0eSAndy Whitcroft
2065de7d4f0eSAndy Whitcroft# if/while/etc brace do not go on next line, unless defining a do while loop,
2066de7d4f0eSAndy Whitcroft# or if that brace on the next line is for something else
2067c45dcabdSAndy Whitcroft		if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
2068773647a0SAndy Whitcroft			my $pre_ctx = "$1$2";
2069773647a0SAndy Whitcroft
20709c0ca6f9SAndy Whitcroft			my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
20718eef05ddSJoe Perches
20728eef05ddSJoe Perches			if ($line =~ /^\+\t{6,}/) {
20738eef05ddSJoe Perches				WARN("DEEP_INDENTATION",
20748eef05ddSJoe Perches				     "Too many leading tabs - consider code refactoring\n" . $herecurr);
20758eef05ddSJoe Perches			}
20768eef05ddSJoe Perches
2077de7d4f0eSAndy Whitcroft			my $ctx_cnt = $realcnt - $#ctx - 1;
2078de7d4f0eSAndy Whitcroft			my $ctx = join("\n", @ctx);
2079de7d4f0eSAndy Whitcroft
2080548596d5SAndy Whitcroft			my $ctx_ln = $linenr;
2081548596d5SAndy Whitcroft			my $ctx_skip = $realcnt;
2082de7d4f0eSAndy Whitcroft
2083548596d5SAndy Whitcroft			while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
2084548596d5SAndy Whitcroft					defined $lines[$ctx_ln - 1] &&
2085548596d5SAndy Whitcroft					$lines[$ctx_ln - 1] =~ /^-/)) {
2086548596d5SAndy Whitcroft				##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
2087548596d5SAndy Whitcroft				$ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
2088773647a0SAndy Whitcroft				$ctx_ln++;
2089773647a0SAndy Whitcroft			}
2090548596d5SAndy Whitcroft
209153210168SAndy Whitcroft			#print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
209253210168SAndy Whitcroft			#print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
2093773647a0SAndy Whitcroft
2094773647a0SAndy Whitcroft			if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln -1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
2095000d1cc1SJoe Perches				ERROR("OPEN_BRACE",
2096000d1cc1SJoe Perches				      "that open brace { should be on the previous line\n" .
209701464f30SAndy Whitcroft					"$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
209800df344fSAndy Whitcroft			}
2099773647a0SAndy Whitcroft			if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
2100773647a0SAndy Whitcroft			    $ctx =~ /\)\s*\;\s*$/ &&
2101773647a0SAndy Whitcroft			    defined $lines[$ctx_ln - 1])
2102773647a0SAndy Whitcroft			{
21039c0ca6f9SAndy Whitcroft				my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
21049c0ca6f9SAndy Whitcroft				if ($nindent > $indent) {
2105000d1cc1SJoe Perches					WARN("TRAILING_SEMICOLON",
2106000d1cc1SJoe Perches					     "trailing semicolon indicates no statements, indent implies otherwise\n" .
210701464f30SAndy Whitcroft						"$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
21089c0ca6f9SAndy Whitcroft				}
21099c0ca6f9SAndy Whitcroft			}
211000df344fSAndy Whitcroft		}
211100df344fSAndy Whitcroft
21124d001e4dSAndy Whitcroft# Check relative indent for conditionals and blocks.
21134d001e4dSAndy Whitcroft		if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
21143e469cdcSAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
21153e469cdcSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0)
21163e469cdcSAndy Whitcroft					if (!defined $stat);
21174d001e4dSAndy Whitcroft			my ($s, $c) = ($stat, $cond);
21184d001e4dSAndy Whitcroft
21194d001e4dSAndy Whitcroft			substr($s, 0, length($c), '');
21204d001e4dSAndy Whitcroft
21214d001e4dSAndy Whitcroft			# Make sure we remove the line prefixes as we have
21224d001e4dSAndy Whitcroft			# none on the first line, and are going to readd them
21234d001e4dSAndy Whitcroft			# where necessary.
21244d001e4dSAndy Whitcroft			$s =~ s/\n./\n/gs;
21254d001e4dSAndy Whitcroft
21264d001e4dSAndy Whitcroft			# Find out how long the conditional actually is.
21276f779c18SAndy Whitcroft			my @newlines = ($c =~ /\n/gs);
21286f779c18SAndy Whitcroft			my $cond_lines = 1 + $#newlines;
21294d001e4dSAndy Whitcroft
21304d001e4dSAndy Whitcroft			# We want to check the first line inside the block
21314d001e4dSAndy Whitcroft			# starting at the end of the conditional, so remove:
21324d001e4dSAndy Whitcroft			#  1) any blank line termination
21334d001e4dSAndy Whitcroft			#  2) any opening brace { on end of the line
21344d001e4dSAndy Whitcroft			#  3) any do (...) {
21354d001e4dSAndy Whitcroft			my $continuation = 0;
21364d001e4dSAndy Whitcroft			my $check = 0;
21374d001e4dSAndy Whitcroft			$s =~ s/^.*\bdo\b//;
21384d001e4dSAndy Whitcroft			$s =~ s/^\s*{//;
21394d001e4dSAndy Whitcroft			if ($s =~ s/^\s*\\//) {
21404d001e4dSAndy Whitcroft				$continuation = 1;
21414d001e4dSAndy Whitcroft			}
21429bd49efeSAndy Whitcroft			if ($s =~ s/^\s*?\n//) {
21434d001e4dSAndy Whitcroft				$check = 1;
21444d001e4dSAndy Whitcroft				$cond_lines++;
21454d001e4dSAndy Whitcroft			}
21464d001e4dSAndy Whitcroft
21474d001e4dSAndy Whitcroft			# Also ignore a loop construct at the end of a
21484d001e4dSAndy Whitcroft			# preprocessor statement.
21494d001e4dSAndy Whitcroft			if (($prevline =~ /^.\s*#\s*define\s/ ||
21504d001e4dSAndy Whitcroft			    $prevline =~ /\\\s*$/) && $continuation == 0) {
21514d001e4dSAndy Whitcroft				$check = 0;
21524d001e4dSAndy Whitcroft			}
21534d001e4dSAndy Whitcroft
21549bd49efeSAndy Whitcroft			my $cond_ptr = -1;
2155740504c6SAndy Whitcroft			$continuation = 0;
21569bd49efeSAndy Whitcroft			while ($cond_ptr != $cond_lines) {
21579bd49efeSAndy Whitcroft				$cond_ptr = $cond_lines;
21584d001e4dSAndy Whitcroft
2159f16fa28fSAndy Whitcroft				# If we see an #else/#elif then the code
2160f16fa28fSAndy Whitcroft				# is not linear.
2161f16fa28fSAndy Whitcroft				if ($s =~ /^\s*\#\s*(?:else|elif)/) {
2162f16fa28fSAndy Whitcroft					$check = 0;
2163f16fa28fSAndy Whitcroft				}
2164f16fa28fSAndy Whitcroft
21659bd49efeSAndy Whitcroft				# Ignore:
21669bd49efeSAndy Whitcroft				#  1) blank lines, they should be at 0,
21679bd49efeSAndy Whitcroft				#  2) preprocessor lines, and
21689bd49efeSAndy Whitcroft				#  3) labels.
2169740504c6SAndy Whitcroft				if ($continuation ||
2170740504c6SAndy Whitcroft				    $s =~ /^\s*?\n/ ||
21719bd49efeSAndy Whitcroft				    $s =~ /^\s*#\s*?/ ||
21729bd49efeSAndy Whitcroft				    $s =~ /^\s*$Ident\s*:/) {
2173740504c6SAndy Whitcroft					$continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
217430dad6ebSAndy Whitcroft					if ($s =~ s/^.*?\n//) {
21759bd49efeSAndy Whitcroft						$cond_lines++;
21769bd49efeSAndy Whitcroft					}
21774d001e4dSAndy Whitcroft				}
217830dad6ebSAndy Whitcroft			}
21794d001e4dSAndy Whitcroft
21804d001e4dSAndy Whitcroft			my (undef, $sindent) = line_stats("+" . $s);
21814d001e4dSAndy Whitcroft			my $stat_real = raw_line($linenr, $cond_lines);
21824d001e4dSAndy Whitcroft
21834d001e4dSAndy Whitcroft			# Check if either of these lines are modified, else
21844d001e4dSAndy Whitcroft			# this is not this patch's fault.
21854d001e4dSAndy Whitcroft			if (!defined($stat_real) ||
21864d001e4dSAndy Whitcroft			    $stat !~ /^\+/ && $stat_real !~ /^\+/) {
21874d001e4dSAndy Whitcroft				$check = 0;
21884d001e4dSAndy Whitcroft			}
21894d001e4dSAndy Whitcroft			if (defined($stat_real) && $cond_lines > 1) {
21904d001e4dSAndy Whitcroft				$stat_real = "[...]\n$stat_real";
21914d001e4dSAndy Whitcroft			}
21924d001e4dSAndy Whitcroft
21939bd49efeSAndy 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";
21944d001e4dSAndy Whitcroft
21954d001e4dSAndy Whitcroft			if ($check && (($sindent % 8) != 0 ||
21964d001e4dSAndy Whitcroft			    ($sindent <= $indent && $s ne ''))) {
2197000d1cc1SJoe Perches				WARN("SUSPECT_CODE_INDENT",
2198000d1cc1SJoe Perches				     "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
21994d001e4dSAndy Whitcroft			}
22004d001e4dSAndy Whitcroft		}
22014d001e4dSAndy Whitcroft
22026c72ffaaSAndy Whitcroft		# Track the 'values' across context and added lines.
22036c72ffaaSAndy Whitcroft		my $opline = $line; $opline =~ s/^./ /;
22041f65f947SAndy Whitcroft		my ($curr_values, $curr_vars) =
22051f65f947SAndy Whitcroft				annotate_values($opline . "\n", $prev_values);
22066c72ffaaSAndy Whitcroft		$curr_values = $prev_values . $curr_values;
2207c2fdda0dSAndy Whitcroft		if ($dbg_values) {
2208c2fdda0dSAndy Whitcroft			my $outline = $opline; $outline =~ s/\t/ /g;
2209cf655043SAndy Whitcroft			print "$linenr > .$outline\n";
2210cf655043SAndy Whitcroft			print "$linenr > $curr_values\n";
22111f65f947SAndy Whitcroft			print "$linenr >  $curr_vars\n";
2212c2fdda0dSAndy Whitcroft		}
22136c72ffaaSAndy Whitcroft		$prev_values = substr($curr_values, -1);
22146c72ffaaSAndy Whitcroft
221500df344fSAndy Whitcroft#ignore lines not being added
221600df344fSAndy Whitcroft		if ($line=~/^[^\+]/) {next;}
221700df344fSAndy Whitcroft
2218653d4876SAndy Whitcroft# TEST: allow direct testing of the type matcher.
22197429c690SAndy Whitcroft		if ($dbg_type) {
22207429c690SAndy Whitcroft			if ($line =~ /^.\s*$Declare\s*$/) {
2221000d1cc1SJoe Perches				ERROR("TEST_TYPE",
2222000d1cc1SJoe Perches				      "TEST: is type\n" . $herecurr);
22237429c690SAndy Whitcroft			} elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
2224000d1cc1SJoe Perches				ERROR("TEST_NOT_TYPE",
2225000d1cc1SJoe Perches				      "TEST: is not type ($1 is)\n". $herecurr);
22267429c690SAndy Whitcroft			}
2227653d4876SAndy Whitcroft			next;
2228653d4876SAndy Whitcroft		}
2229a1ef277eSAndy Whitcroft# TEST: allow direct testing of the attribute matcher.
2230a1ef277eSAndy Whitcroft		if ($dbg_attr) {
22319360b0e5SAndy Whitcroft			if ($line =~ /^.\s*$Modifier\s*$/) {
2232000d1cc1SJoe Perches				ERROR("TEST_ATTR",
2233000d1cc1SJoe Perches				      "TEST: is attr\n" . $herecurr);
22349360b0e5SAndy Whitcroft			} elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
2235000d1cc1SJoe Perches				ERROR("TEST_NOT_ATTR",
2236000d1cc1SJoe Perches				      "TEST: is not attr ($1 is)\n". $herecurr);
2237a1ef277eSAndy Whitcroft			}
2238a1ef277eSAndy Whitcroft			next;
2239a1ef277eSAndy Whitcroft		}
2240653d4876SAndy Whitcroft
2241f0a594c1SAndy Whitcroft# check for initialisation to aggregates open brace on the next line
224299423c20SAndy Whitcroft		if ($line =~ /^.\s*{/ &&
224399423c20SAndy Whitcroft		    $prevline =~ /(?:^|[^=])=\s*$/) {
2244000d1cc1SJoe Perches			ERROR("OPEN_BRACE",
2245000d1cc1SJoe Perches			      "that open brace { should be on the previous line\n" . $hereprev);
2246f0a594c1SAndy Whitcroft		}
2247f0a594c1SAndy Whitcroft
224800df344fSAndy Whitcroft#
224900df344fSAndy Whitcroft# Checks which are anchored on the added line.
225000df344fSAndy Whitcroft#
225100df344fSAndy Whitcroft
2252653d4876SAndy Whitcroft# check for malformed paths in #include statements (uses RAW line)
2253c45dcabdSAndy Whitcroft		if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
2254653d4876SAndy Whitcroft			my $path = $1;
2255653d4876SAndy Whitcroft			if ($path =~ m{//}) {
2256000d1cc1SJoe Perches				ERROR("MALFORMED_INCLUDE",
2257495e9d84SJoe Perches				      "malformed #include filename\n" . $herecurr);
2258495e9d84SJoe Perches			}
2259495e9d84SJoe Perches			if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
2260495e9d84SJoe Perches				ERROR("UAPI_INCLUDE",
2261495e9d84SJoe Perches				      "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
2262653d4876SAndy Whitcroft			}
2263653d4876SAndy Whitcroft		}
2264653d4876SAndy Whitcroft
226500df344fSAndy Whitcroft# no C99 // comments
226600df344fSAndy Whitcroft		if ($line =~ m{//}) {
2267000d1cc1SJoe Perches			ERROR("C99_COMMENTS",
2268000d1cc1SJoe Perches			      "do not use C99 // comments\n" . $herecurr);
226900df344fSAndy Whitcroft		}
227000df344fSAndy Whitcroft		# Remove C99 comments.
22710a920b5bSAndy Whitcroft		$line =~ s@//.*@@;
22726c72ffaaSAndy Whitcroft		$opline =~ s@//.*@@;
22730a920b5bSAndy Whitcroft
22742b474a1aSAndy Whitcroft# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
22752b474a1aSAndy Whitcroft# the whole statement.
22762b474a1aSAndy Whitcroft#print "APW <$lines[$realline_next - 1]>\n";
22772b474a1aSAndy Whitcroft		if (defined $realline_next &&
22782b474a1aSAndy Whitcroft		    exists $lines[$realline_next - 1] &&
22792b474a1aSAndy Whitcroft		    !defined $suppress_export{$realline_next} &&
22802b474a1aSAndy Whitcroft		    ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
22812b474a1aSAndy Whitcroft		     $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
22823cbf62dfSAndy Whitcroft			# Handle definitions which produce identifiers with
22833cbf62dfSAndy Whitcroft			# a prefix:
22843cbf62dfSAndy Whitcroft			#   XXX(foo);
22853cbf62dfSAndy Whitcroft			#   EXPORT_SYMBOL(something_foo);
2286653d4876SAndy Whitcroft			my $name = $1;
228787a53877SAndy Whitcroft			if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
22883cbf62dfSAndy Whitcroft			    $name =~ /^${Ident}_$2/) {
22893cbf62dfSAndy Whitcroft#print "FOO C name<$name>\n";
22903cbf62dfSAndy Whitcroft				$suppress_export{$realline_next} = 1;
22913cbf62dfSAndy Whitcroft
22923cbf62dfSAndy Whitcroft			} elsif ($stat !~ /(?:
22932b474a1aSAndy Whitcroft				\n.}\s*$|
229448012058SAndy Whitcroft				^.DEFINE_$Ident\(\Q$name\E\)|
229548012058SAndy Whitcroft				^.DECLARE_$Ident\(\Q$name\E\)|
229648012058SAndy Whitcroft				^.LIST_HEAD\(\Q$name\E\)|
22972b474a1aSAndy Whitcroft				^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
22982b474a1aSAndy Whitcroft				\b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
229948012058SAndy Whitcroft			    )/x) {
23002b474a1aSAndy Whitcroft#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
23012b474a1aSAndy Whitcroft				$suppress_export{$realline_next} = 2;
23022b474a1aSAndy Whitcroft			} else {
23032b474a1aSAndy Whitcroft				$suppress_export{$realline_next} = 1;
23040a920b5bSAndy Whitcroft			}
23050a920b5bSAndy Whitcroft		}
23062b474a1aSAndy Whitcroft		if (!defined $suppress_export{$linenr} &&
23072b474a1aSAndy Whitcroft		    $prevline =~ /^.\s*$/ &&
23082b474a1aSAndy Whitcroft		    ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
23092b474a1aSAndy Whitcroft		     $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
23102b474a1aSAndy Whitcroft#print "FOO B <$lines[$linenr - 1]>\n";
23112b474a1aSAndy Whitcroft			$suppress_export{$linenr} = 2;
23122b474a1aSAndy Whitcroft		}
23132b474a1aSAndy Whitcroft		if (defined $suppress_export{$linenr} &&
23142b474a1aSAndy Whitcroft		    $suppress_export{$linenr} == 2) {
2315000d1cc1SJoe Perches			WARN("EXPORT_SYMBOL",
2316000d1cc1SJoe Perches			     "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
23172b474a1aSAndy Whitcroft		}
23180a920b5bSAndy Whitcroft
23195150bda4SJoe Eloff# check for global initialisers.
2320c45dcabdSAndy Whitcroft		if ($line =~ /^.$Type\s*$Ident\s*(?:\s+$Modifier)*\s*=\s*(0|NULL|false)\s*;/) {
2321000d1cc1SJoe Perches			ERROR("GLOBAL_INITIALISERS",
2322000d1cc1SJoe Perches			      "do not initialise globals to 0 or NULL\n" .
2323f0a594c1SAndy Whitcroft				$herecurr);
2324f0a594c1SAndy Whitcroft		}
23250a920b5bSAndy Whitcroft# check for static initialisers.
23262d1bafd7SAndy Whitcroft		if ($line =~ /\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
2327000d1cc1SJoe Perches			ERROR("INITIALISED_STATIC",
2328000d1cc1SJoe Perches			      "do not initialise statics to 0 or NULL\n" .
2329de7d4f0eSAndy Whitcroft				$herecurr);
23300a920b5bSAndy Whitcroft		}
23310a920b5bSAndy Whitcroft
2332cb710ecaSJoe Perches# check for static const char * arrays.
2333cb710ecaSJoe Perches		if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
2334000d1cc1SJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
2335000d1cc1SJoe Perches			     "static const char * array should probably be static const char * const\n" .
2336cb710ecaSJoe Perches				$herecurr);
2337cb710ecaSJoe Perches               }
2338cb710ecaSJoe Perches
2339cb710ecaSJoe Perches# check for static char foo[] = "bar" declarations.
2340cb710ecaSJoe Perches		if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
2341000d1cc1SJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
2342000d1cc1SJoe Perches			     "static char array declaration should probably be static const char\n" .
2343cb710ecaSJoe Perches				$herecurr);
2344cb710ecaSJoe Perches               }
2345cb710ecaSJoe Perches
234693ed0e2dSJoe Perches# check for declarations of struct pci_device_id
234793ed0e2dSJoe Perches		if ($line =~ /\bstruct\s+pci_device_id\s+\w+\s*\[\s*\]\s*\=\s*\{/) {
2348000d1cc1SJoe Perches			WARN("DEFINE_PCI_DEVICE_TABLE",
2349000d1cc1SJoe Perches			     "Use DEFINE_PCI_DEVICE_TABLE for struct pci_device_id\n" . $herecurr);
235093ed0e2dSJoe Perches		}
235193ed0e2dSJoe Perches
2352653d4876SAndy Whitcroft# check for new typedefs, only function parameters and sparse annotations
2353653d4876SAndy Whitcroft# make sense.
2354653d4876SAndy Whitcroft		if ($line =~ /\btypedef\s/ &&
23558054576dSAndy Whitcroft		    $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
2356c45dcabdSAndy Whitcroft		    $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
23578ed22cadSAndy Whitcroft		    $line !~ /\b$typeTypedefs\b/ &&
2358653d4876SAndy Whitcroft		    $line !~ /\b__bitwise(?:__|)\b/) {
2359000d1cc1SJoe Perches			WARN("NEW_TYPEDEFS",
2360000d1cc1SJoe Perches			     "do not add new typedefs\n" . $herecurr);
23610a920b5bSAndy Whitcroft		}
23620a920b5bSAndy Whitcroft
23630a920b5bSAndy Whitcroft# * goes on variable not on type
236465863862SAndy Whitcroft		# (char*[ const])
2365bfcb2cc7SAndy Whitcroft		while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
2366bfcb2cc7SAndy Whitcroft			#print "AA<$1>\n";
2367bfcb2cc7SAndy Whitcroft			my ($from, $to) = ($2, $2);
2368d8aaf121SAndy Whitcroft
236965863862SAndy Whitcroft			# Should start with a space.
237065863862SAndy Whitcroft			$to =~ s/^(\S)/ $1/;
237165863862SAndy Whitcroft			# Should not end with a space.
237265863862SAndy Whitcroft			$to =~ s/\s+$//;
237365863862SAndy Whitcroft			# '*'s should not have spaces between.
2374f9a0b3d1SAndy Whitcroft			while ($to =~ s/\*\s+\*/\*\*/) {
237565863862SAndy Whitcroft			}
2376d8aaf121SAndy Whitcroft
237765863862SAndy Whitcroft			#print "from<$from> to<$to>\n";
237865863862SAndy Whitcroft			if ($from ne $to) {
2379000d1cc1SJoe Perches				ERROR("POINTER_LOCATION",
2380000d1cc1SJoe Perches				      "\"(foo$from)\" should be \"(foo$to)\"\n" .  $herecurr);
238165863862SAndy Whitcroft			}
2382bfcb2cc7SAndy Whitcroft		}
2383bfcb2cc7SAndy Whitcroft		while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
2384bfcb2cc7SAndy Whitcroft			#print "BB<$1>\n";
2385bfcb2cc7SAndy Whitcroft			my ($from, $to, $ident) = ($2, $2, $3);
2386d8aaf121SAndy Whitcroft
238765863862SAndy Whitcroft			# Should start with a space.
238865863862SAndy Whitcroft			$to =~ s/^(\S)/ $1/;
238965863862SAndy Whitcroft			# Should not end with a space.
239065863862SAndy Whitcroft			$to =~ s/\s+$//;
239165863862SAndy Whitcroft			# '*'s should not have spaces between.
2392f9a0b3d1SAndy Whitcroft			while ($to =~ s/\*\s+\*/\*\*/) {
239365863862SAndy Whitcroft			}
239465863862SAndy Whitcroft			# Modifiers should have spaces.
239565863862SAndy Whitcroft			$to =~ s/(\b$Modifier$)/$1 /;
239665863862SAndy Whitcroft
2397667026e7SAndy Whitcroft			#print "from<$from> to<$to> ident<$ident>\n";
2398667026e7SAndy Whitcroft			if ($from ne $to && $ident !~ /^$Modifier$/) {
2399000d1cc1SJoe Perches				ERROR("POINTER_LOCATION",
2400000d1cc1SJoe Perches				      "\"foo${from}bar\" should be \"foo${to}bar\"\n" .  $herecurr);
240165863862SAndy Whitcroft			}
24020a920b5bSAndy Whitcroft		}
24030a920b5bSAndy Whitcroft
24040a920b5bSAndy Whitcroft# # no BUG() or BUG_ON()
24050a920b5bSAndy Whitcroft# 		if ($line =~ /\b(BUG|BUG_ON)\b/) {
24060a920b5bSAndy Whitcroft# 			print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
24070a920b5bSAndy Whitcroft# 			print "$herecurr";
24080a920b5bSAndy Whitcroft# 			$clean = 0;
24090a920b5bSAndy Whitcroft# 		}
24100a920b5bSAndy Whitcroft
24118905a67cSAndy Whitcroft		if ($line =~ /\bLINUX_VERSION_CODE\b/) {
2412000d1cc1SJoe Perches			WARN("LINUX_VERSION_CODE",
2413000d1cc1SJoe Perches			     "LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
24148905a67cSAndy Whitcroft		}
24158905a67cSAndy Whitcroft
241617441227SJoe Perches# check for uses of printk_ratelimit
241717441227SJoe Perches		if ($line =~ /\bprintk_ratelimit\s*\(/) {
2418000d1cc1SJoe Perches			WARN("PRINTK_RATELIMITED",
2419000d1cc1SJoe Perches"Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
242017441227SJoe Perches		}
242117441227SJoe Perches
242200df344fSAndy Whitcroft# printk should use KERN_* levels.  Note that follow on printk's on the
242300df344fSAndy Whitcroft# same line do not need a level, so we use the current block context
242400df344fSAndy Whitcroft# to try and find and validate the current printk.  In summary the current
242525985edcSLucas De Marchi# printk includes all preceding printk's which have no newline on the end.
242600df344fSAndy Whitcroft# we assume the first bad printk is the one to report.
2427f0a594c1SAndy Whitcroft		if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
242800df344fSAndy Whitcroft			my $ok = 0;
242900df344fSAndy Whitcroft			for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
243000df344fSAndy Whitcroft				#print "CHECK<$lines[$ln - 1]\n";
243125985edcSLucas De Marchi				# we have a preceding printk if it ends
243200df344fSAndy Whitcroft				# with "\n" ignore it, else it is to blame
243300df344fSAndy Whitcroft				if ($lines[$ln - 1] =~ m{\bprintk\(}) {
243400df344fSAndy Whitcroft					if ($rawlines[$ln - 1] !~ m{\\n"}) {
243500df344fSAndy Whitcroft						$ok = 1;
243600df344fSAndy Whitcroft					}
243700df344fSAndy Whitcroft					last;
243800df344fSAndy Whitcroft				}
243900df344fSAndy Whitcroft			}
244000df344fSAndy Whitcroft			if ($ok == 0) {
2441000d1cc1SJoe Perches				WARN("PRINTK_WITHOUT_KERN_LEVEL",
2442000d1cc1SJoe Perches				     "printk() should include KERN_ facility level\n" . $herecurr);
24430a920b5bSAndy Whitcroft			}
244400df344fSAndy Whitcroft		}
24450a920b5bSAndy Whitcroft
2446243f3803SJoe Perches		if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
2447243f3803SJoe Perches			my $orig = $1;
2448243f3803SJoe Perches			my $level = lc($orig);
2449243f3803SJoe Perches			$level = "warn" if ($level eq "warning");
24508f26b837SJoe Perches			my $level2 = $level;
24518f26b837SJoe Perches			$level2 = "dbg" if ($level eq "debug");
2452243f3803SJoe Perches			WARN("PREFER_PR_LEVEL",
24538f26b837SJoe Perches			     "Prefer netdev_$level2(netdev, ... then dev_$level2(dev, ... then pr_$level(...  to printk(KERN_$orig ...\n" . $herecurr);
2454243f3803SJoe Perches		}
2455243f3803SJoe Perches
2456243f3803SJoe Perches		if ($line =~ /\bpr_warning\s*\(/) {
2457243f3803SJoe Perches			WARN("PREFER_PR_LEVEL",
2458243f3803SJoe Perches			     "Prefer pr_warn(... to pr_warning(...\n" . $herecurr);
2459243f3803SJoe Perches		}
2460243f3803SJoe Perches
2461dc139313SJoe Perches		if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
2462dc139313SJoe Perches			my $orig = $1;
2463dc139313SJoe Perches			my $level = lc($orig);
2464dc139313SJoe Perches			$level = "warn" if ($level eq "warning");
2465dc139313SJoe Perches			$level = "dbg" if ($level eq "debug");
2466dc139313SJoe Perches			WARN("PREFER_DEV_LEVEL",
2467dc139313SJoe Perches			     "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
2468dc139313SJoe Perches		}
2469dc139313SJoe Perches
2470653d4876SAndy Whitcroft# function brace can't be on same line, except for #defines of do while,
2471653d4876SAndy Whitcroft# or if closed on same line
2472c45dcabdSAndy Whitcroft		if (($line=~/$Type\s*$Ident\(.*\).*\s{/) and
2473c45dcabdSAndy Whitcroft		    !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) {
2474000d1cc1SJoe Perches			ERROR("OPEN_BRACE",
2475000d1cc1SJoe Perches			      "open brace '{' following function declarations go on the next line\n" . $herecurr);
24760a920b5bSAndy Whitcroft		}
2477653d4876SAndy Whitcroft
24788905a67cSAndy Whitcroft# open braces for enum, union and struct go on the same line.
24798905a67cSAndy Whitcroft		if ($line =~ /^.\s*{/ &&
24808905a67cSAndy Whitcroft		    $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
2481000d1cc1SJoe Perches			ERROR("OPEN_BRACE",
2482000d1cc1SJoe Perches			      "open brace '{' following $1 go on the same line\n" . $hereprev);
24838905a67cSAndy Whitcroft		}
24848905a67cSAndy Whitcroft
24850c73b4ebSAndy Whitcroft# missing space after union, struct or enum definition
24860c73b4ebSAndy Whitcroft		if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?(?:\s+$Ident)?[=\{]/) {
2487000d1cc1SJoe Perches		    WARN("SPACING",
2488000d1cc1SJoe Perches			 "missing space after $1 definition\n" . $herecurr);
24890c73b4ebSAndy Whitcroft		}
24900c73b4ebSAndy Whitcroft
24918d31cfceSAndy Whitcroft# check for spacing round square brackets; allowed:
24928d31cfceSAndy Whitcroft#  1. with a type on the left -- int [] a;
2493fe2a7dbcSAndy Whitcroft#  2. at the beginning of a line for slice initialisers -- [0...10] = 5,
2494fe2a7dbcSAndy Whitcroft#  3. inside a curly brace -- = { [0...10] = 5 }
24958d31cfceSAndy Whitcroft		while ($line =~ /(.*?\s)\[/g) {
24968d31cfceSAndy Whitcroft			my ($where, $prefix) = ($-[1], $1);
24978d31cfceSAndy Whitcroft			if ($prefix !~ /$Type\s+$/ &&
2498fe2a7dbcSAndy Whitcroft			    ($where != 0 || $prefix !~ /^.\s+$/) &&
2499daebc534SAndy Whitcroft			    $prefix !~ /[{,]\s+$/) {
2500000d1cc1SJoe Perches				ERROR("BRACKET_SPACE",
2501000d1cc1SJoe Perches				      "space prohibited before open square bracket '['\n" . $herecurr);
25028d31cfceSAndy Whitcroft			}
25038d31cfceSAndy Whitcroft		}
25048d31cfceSAndy Whitcroft
2505f0a594c1SAndy Whitcroft# check for spaces between functions and their parentheses.
25066c72ffaaSAndy Whitcroft		while ($line =~ /($Ident)\s+\(/g) {
2507c2fdda0dSAndy Whitcroft			my $name = $1;
2508773647a0SAndy Whitcroft			my $ctx_before = substr($line, 0, $-[1]);
2509773647a0SAndy Whitcroft			my $ctx = "$ctx_before$name";
2510c2fdda0dSAndy Whitcroft
2511c2fdda0dSAndy Whitcroft			# Ignore those directives where spaces _are_ permitted.
2512773647a0SAndy Whitcroft			if ($name =~ /^(?:
2513773647a0SAndy Whitcroft				if|for|while|switch|return|case|
2514773647a0SAndy Whitcroft				volatile|__volatile__|
2515773647a0SAndy Whitcroft				__attribute__|format|__extension__|
2516773647a0SAndy Whitcroft				asm|__asm__)$/x)
2517773647a0SAndy Whitcroft			{
2518c2fdda0dSAndy Whitcroft
2519c2fdda0dSAndy Whitcroft			# cpp #define statements have non-optional spaces, ie
2520c2fdda0dSAndy Whitcroft			# if there is a space between the name and the open
2521c2fdda0dSAndy Whitcroft			# parenthesis it is simply not a parameter group.
2522c45dcabdSAndy Whitcroft			} elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
2523773647a0SAndy Whitcroft
2524773647a0SAndy Whitcroft			# cpp #elif statement condition may start with a (
2525c45dcabdSAndy Whitcroft			} elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
2526c2fdda0dSAndy Whitcroft
2527c2fdda0dSAndy Whitcroft			# If this whole things ends with a type its most
2528c2fdda0dSAndy Whitcroft			# likely a typedef for a function.
2529773647a0SAndy Whitcroft			} elsif ($ctx =~ /$Type$/) {
2530c2fdda0dSAndy Whitcroft
2531c2fdda0dSAndy Whitcroft			} else {
2532000d1cc1SJoe Perches				WARN("SPACING",
2533000d1cc1SJoe Perches				     "space prohibited between function name and open parenthesis '('\n" . $herecurr);
2534f0a594c1SAndy Whitcroft			}
25356c72ffaaSAndy Whitcroft		}
25369a4cad4eSEric Nelson
25379a4cad4eSEric Nelson# check for whitespace before a non-naked semicolon
25389a4cad4eSEric Nelson		if ($line =~ /^\+.*\S\s+;/) {
25395646bc71SJoe Perches			WARN("SPACING",
25409a4cad4eSEric Nelson			     "space prohibited before semicolon\n" . $herecurr);
25419a4cad4eSEric Nelson		}
25429a4cad4eSEric Nelson
2543653d4876SAndy Whitcroft# Check operator spacing.
25440a920b5bSAndy Whitcroft		if (!($line=~/\#\s*include/)) {
25459c0ca6f9SAndy Whitcroft			my $ops = qr{
25469c0ca6f9SAndy Whitcroft				<<=|>>=|<=|>=|==|!=|
25479c0ca6f9SAndy Whitcroft				\+=|-=|\*=|\/=|%=|\^=|\|=|&=|
25489c0ca6f9SAndy Whitcroft				=>|->|<<|>>|<|>|=|!|~|
25491f65f947SAndy Whitcroft				&&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
25501f65f947SAndy Whitcroft				\?|:
25519c0ca6f9SAndy Whitcroft			}x;
2552cf655043SAndy Whitcroft			my @elements = split(/($ops|;)/, $opline);
255300df344fSAndy Whitcroft			my $off = 0;
25546c72ffaaSAndy Whitcroft
25556c72ffaaSAndy Whitcroft			my $blank = copy_spacing($opline);
25566c72ffaaSAndy Whitcroft
25570a920b5bSAndy Whitcroft			for (my $n = 0; $n < $#elements; $n += 2) {
25584a0df2efSAndy Whitcroft				$off += length($elements[$n]);
25594a0df2efSAndy Whitcroft
256025985edcSLucas De Marchi				# Pick up the preceding and succeeding characters.
2561773647a0SAndy Whitcroft				my $ca = substr($opline, 0, $off);
2562773647a0SAndy Whitcroft				my $cc = '';
2563773647a0SAndy Whitcroft				if (length($opline) >= ($off + length($elements[$n + 1]))) {
2564773647a0SAndy Whitcroft					$cc = substr($opline, $off + length($elements[$n + 1]));
2565773647a0SAndy Whitcroft				}
2566773647a0SAndy Whitcroft				my $cb = "$ca$;$cc";
2567773647a0SAndy Whitcroft
25684a0df2efSAndy Whitcroft				my $a = '';
25694a0df2efSAndy Whitcroft				$a = 'V' if ($elements[$n] ne '');
25704a0df2efSAndy Whitcroft				$a = 'W' if ($elements[$n] =~ /\s$/);
2571cf655043SAndy Whitcroft				$a = 'C' if ($elements[$n] =~ /$;$/);
25724a0df2efSAndy Whitcroft				$a = 'B' if ($elements[$n] =~ /(\[|\()$/);
25734a0df2efSAndy Whitcroft				$a = 'O' if ($elements[$n] eq '');
2574773647a0SAndy Whitcroft				$a = 'E' if ($ca =~ /^\s*$/);
25754a0df2efSAndy Whitcroft
25760a920b5bSAndy Whitcroft				my $op = $elements[$n + 1];
25774a0df2efSAndy Whitcroft
25784a0df2efSAndy Whitcroft				my $c = '';
25790a920b5bSAndy Whitcroft				if (defined $elements[$n + 2]) {
25804a0df2efSAndy Whitcroft					$c = 'V' if ($elements[$n + 2] ne '');
25814a0df2efSAndy Whitcroft					$c = 'W' if ($elements[$n + 2] =~ /^\s/);
2582cf655043SAndy Whitcroft					$c = 'C' if ($elements[$n + 2] =~ /^$;/);
25834a0df2efSAndy Whitcroft					$c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
25844a0df2efSAndy Whitcroft					$c = 'O' if ($elements[$n + 2] eq '');
25858b1b3378SAndy Whitcroft					$c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
25864a0df2efSAndy Whitcroft				} else {
25874a0df2efSAndy Whitcroft					$c = 'E';
25880a920b5bSAndy Whitcroft				}
25890a920b5bSAndy Whitcroft
25904a0df2efSAndy Whitcroft				my $ctx = "${a}x${c}";
25914a0df2efSAndy Whitcroft
25924a0df2efSAndy Whitcroft				my $at = "(ctx:$ctx)";
25934a0df2efSAndy Whitcroft
25946c72ffaaSAndy Whitcroft				my $ptr = substr($blank, 0, $off) . "^";
2595de7d4f0eSAndy Whitcroft				my $hereptr = "$hereline$ptr\n";
25960a920b5bSAndy Whitcroft
259774048ed8SAndy Whitcroft				# Pull out the value of this operator.
25986c72ffaaSAndy Whitcroft				my $op_type = substr($curr_values, $off + 1, 1);
25990a920b5bSAndy Whitcroft
26001f65f947SAndy Whitcroft				# Get the full operator variant.
26011f65f947SAndy Whitcroft				my $opv = $op . substr($curr_vars, $off, 1);
26021f65f947SAndy Whitcroft
260313214adfSAndy Whitcroft				# Ignore operators passed as parameters.
260413214adfSAndy Whitcroft				if ($op_type ne 'V' &&
260513214adfSAndy Whitcroft				    $ca =~ /\s$/ && $cc =~ /^\s*,/) {
260613214adfSAndy Whitcroft
2607cf655043SAndy Whitcroft#				# Ignore comments
2608cf655043SAndy Whitcroft#				} elsif ($op =~ /^$;+$/) {
260913214adfSAndy Whitcroft
2610d8aaf121SAndy Whitcroft				# ; should have either the end of line or a space or \ after it
261113214adfSAndy Whitcroft				} elsif ($op eq ';') {
2612cf655043SAndy Whitcroft					if ($ctx !~ /.x[WEBC]/ &&
2613cf655043SAndy Whitcroft					    $cc !~ /^\\/ && $cc !~ /^;/) {
2614000d1cc1SJoe Perches						ERROR("SPACING",
2615000d1cc1SJoe Perches						      "space required after that '$op' $at\n" . $hereptr);
2616d8aaf121SAndy Whitcroft					}
2617d8aaf121SAndy Whitcroft
2618d8aaf121SAndy Whitcroft				# // is a comment
2619d8aaf121SAndy Whitcroft				} elsif ($op eq '//') {
26200a920b5bSAndy Whitcroft
26211f65f947SAndy Whitcroft				# No spaces for:
26221f65f947SAndy Whitcroft				#   ->
26231f65f947SAndy Whitcroft				#   :   when part of a bitfield
26241f65f947SAndy Whitcroft				} elsif ($op eq '->' || $opv eq ':B') {
26254a0df2efSAndy Whitcroft					if ($ctx =~ /Wx.|.xW/) {
2626000d1cc1SJoe Perches						ERROR("SPACING",
2627000d1cc1SJoe Perches						      "spaces prohibited around that '$op' $at\n" . $hereptr);
26280a920b5bSAndy Whitcroft					}
26290a920b5bSAndy Whitcroft
26300a920b5bSAndy Whitcroft				# , must have a space on the right.
26310a920b5bSAndy Whitcroft				} elsif ($op eq ',') {
2632cf655043SAndy Whitcroft					if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
2633000d1cc1SJoe Perches						ERROR("SPACING",
2634000d1cc1SJoe Perches						      "space required after that '$op' $at\n" . $hereptr);
26350a920b5bSAndy Whitcroft					}
26360a920b5bSAndy Whitcroft
26379c0ca6f9SAndy Whitcroft				# '*' as part of a type definition -- reported already.
263874048ed8SAndy Whitcroft				} elsif ($opv eq '*_') {
26399c0ca6f9SAndy Whitcroft					#warn "'*' is part of type\n";
26409c0ca6f9SAndy Whitcroft
26419c0ca6f9SAndy Whitcroft				# unary operators should have a space before and
26429c0ca6f9SAndy Whitcroft				# none after.  May be left adjacent to another
26439c0ca6f9SAndy Whitcroft				# unary operator, or a cast
26449c0ca6f9SAndy Whitcroft				} elsif ($op eq '!' || $op eq '~' ||
264574048ed8SAndy Whitcroft					 $opv eq '*U' || $opv eq '-U' ||
26460d413866SAndy Whitcroft					 $opv eq '&U' || $opv eq '&&U') {
2647cf655043SAndy Whitcroft					if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
2648000d1cc1SJoe Perches						ERROR("SPACING",
2649000d1cc1SJoe Perches						      "space required before that '$op' $at\n" . $hereptr);
26500a920b5bSAndy Whitcroft					}
2651a3340b35SAndy Whitcroft					if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
2652171ae1a4SAndy Whitcroft						# A unary '*' may be const
2653171ae1a4SAndy Whitcroft
2654171ae1a4SAndy Whitcroft					} elsif ($ctx =~ /.xW/) {
2655000d1cc1SJoe Perches						ERROR("SPACING",
2656000d1cc1SJoe Perches						      "space prohibited after that '$op' $at\n" . $hereptr);
26570a920b5bSAndy Whitcroft					}
26580a920b5bSAndy Whitcroft
26590a920b5bSAndy Whitcroft				# unary ++ and unary -- are allowed no space on one side.
26600a920b5bSAndy Whitcroft				} elsif ($op eq '++' or $op eq '--') {
2661773647a0SAndy Whitcroft					if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
2662000d1cc1SJoe Perches						ERROR("SPACING",
2663000d1cc1SJoe Perches						      "space required one side of that '$op' $at\n" . $hereptr);
26640a920b5bSAndy Whitcroft					}
2665773647a0SAndy Whitcroft					if ($ctx =~ /Wx[BE]/ ||
2666773647a0SAndy Whitcroft					    ($ctx =~ /Wx./ && $cc =~ /^;/)) {
2667000d1cc1SJoe Perches						ERROR("SPACING",
2668000d1cc1SJoe Perches						      "space prohibited before that '$op' $at\n" . $hereptr);
2669653d4876SAndy Whitcroft					}
2670773647a0SAndy Whitcroft					if ($ctx =~ /ExW/) {
2671000d1cc1SJoe Perches						ERROR("SPACING",
2672000d1cc1SJoe Perches						      "space prohibited after that '$op' $at\n" . $hereptr);
2673773647a0SAndy Whitcroft					}
2674773647a0SAndy Whitcroft
26750a920b5bSAndy Whitcroft
26760a920b5bSAndy Whitcroft				# << and >> may either have or not have spaces both sides
26779c0ca6f9SAndy Whitcroft				} elsif ($op eq '<<' or $op eq '>>' or
26789c0ca6f9SAndy Whitcroft					 $op eq '&' or $op eq '^' or $op eq '|' or
26799c0ca6f9SAndy Whitcroft					 $op eq '+' or $op eq '-' or
2680c2fdda0dSAndy Whitcroft					 $op eq '*' or $op eq '/' or
2681c2fdda0dSAndy Whitcroft					 $op eq '%')
26820a920b5bSAndy Whitcroft				{
2683773647a0SAndy Whitcroft					if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
2684000d1cc1SJoe Perches						ERROR("SPACING",
2685000d1cc1SJoe Perches						      "need consistent spacing around '$op' $at\n" .
2686de7d4f0eSAndy Whitcroft							$hereptr);
26870a920b5bSAndy Whitcroft					}
26880a920b5bSAndy Whitcroft
26891f65f947SAndy Whitcroft				# A colon needs no spaces before when it is
26901f65f947SAndy Whitcroft				# terminating a case value or a label.
26911f65f947SAndy Whitcroft				} elsif ($opv eq ':C' || $opv eq ':L') {
26921f65f947SAndy Whitcroft					if ($ctx =~ /Wx./) {
2693000d1cc1SJoe Perches						ERROR("SPACING",
2694000d1cc1SJoe Perches						      "space prohibited before that '$op' $at\n" . $hereptr);
26951f65f947SAndy Whitcroft					}
26961f65f947SAndy Whitcroft
26970a920b5bSAndy Whitcroft				# All the others need spaces both sides.
2698cf655043SAndy Whitcroft				} elsif ($ctx !~ /[EWC]x[CWE]/) {
26991f65f947SAndy Whitcroft					my $ok = 0;
27001f65f947SAndy Whitcroft
270122f2a2efSAndy Whitcroft					# Ignore email addresses <foo@bar>
27021f65f947SAndy Whitcroft					if (($op eq '<' &&
27031f65f947SAndy Whitcroft					     $cc =~ /^\S+\@\S+>/) ||
27041f65f947SAndy Whitcroft					    ($op eq '>' &&
27051f65f947SAndy Whitcroft					     $ca =~ /<\S+\@\S+$/))
27061f65f947SAndy Whitcroft					{
27071f65f947SAndy Whitcroft					    	$ok = 1;
27081f65f947SAndy Whitcroft					}
27091f65f947SAndy Whitcroft
27101f65f947SAndy Whitcroft					# Ignore ?:
27111f65f947SAndy Whitcroft					if (($opv eq ':O' && $ca =~ /\?$/) ||
27121f65f947SAndy Whitcroft					    ($op eq '?' && $cc =~ /^:/)) {
27131f65f947SAndy Whitcroft					    	$ok = 1;
27141f65f947SAndy Whitcroft					}
27151f65f947SAndy Whitcroft
27161f65f947SAndy Whitcroft					if ($ok == 0) {
2717000d1cc1SJoe Perches						ERROR("SPACING",
2718000d1cc1SJoe Perches						      "spaces required around that '$op' $at\n" . $hereptr);
27190a920b5bSAndy Whitcroft					}
272022f2a2efSAndy Whitcroft				}
27214a0df2efSAndy Whitcroft				$off += length($elements[$n + 1]);
27220a920b5bSAndy Whitcroft			}
27230a920b5bSAndy Whitcroft		}
27240a920b5bSAndy Whitcroft
2725f0a594c1SAndy Whitcroft# check for multiple assignments
2726f0a594c1SAndy Whitcroft		if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
2727000d1cc1SJoe Perches			CHK("MULTIPLE_ASSIGNMENTS",
2728000d1cc1SJoe Perches			    "multiple assignments should be avoided\n" . $herecurr);
2729f0a594c1SAndy Whitcroft		}
2730f0a594c1SAndy Whitcroft
273122f2a2efSAndy Whitcroft## # check for multiple declarations, allowing for a function declaration
273222f2a2efSAndy Whitcroft## # continuation.
273322f2a2efSAndy Whitcroft## 		if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
273422f2a2efSAndy Whitcroft## 		    $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
273522f2a2efSAndy Whitcroft##
273622f2a2efSAndy Whitcroft## 			# Remove any bracketed sections to ensure we do not
273722f2a2efSAndy Whitcroft## 			# falsly report the parameters of functions.
273822f2a2efSAndy Whitcroft## 			my $ln = $line;
273922f2a2efSAndy Whitcroft## 			while ($ln =~ s/\([^\(\)]*\)//g) {
274022f2a2efSAndy Whitcroft## 			}
274122f2a2efSAndy Whitcroft## 			if ($ln =~ /,/) {
2742000d1cc1SJoe Perches## 				WARN("MULTIPLE_DECLARATION",
2743000d1cc1SJoe Perches##				     "declaring multiple variables together should be avoided\n" . $herecurr);
274422f2a2efSAndy Whitcroft## 			}
274522f2a2efSAndy Whitcroft## 		}
2746f0a594c1SAndy Whitcroft
27470a920b5bSAndy Whitcroft#need space before brace following if, while, etc
274822f2a2efSAndy Whitcroft		if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
274922f2a2efSAndy Whitcroft		    $line =~ /do{/) {
2750000d1cc1SJoe Perches			ERROR("SPACING",
2751000d1cc1SJoe Perches			      "space required before the open brace '{'\n" . $herecurr);
2752de7d4f0eSAndy Whitcroft		}
2753de7d4f0eSAndy Whitcroft
2754c4a62ef9SJoe Perches## # check for blank lines before declarations
2755c4a62ef9SJoe Perches##		if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
2756c4a62ef9SJoe Perches##		    $prevrawline =~ /^.\s*$/) {
2757c4a62ef9SJoe Perches##			WARN("SPACING",
2758c4a62ef9SJoe Perches##			     "No blank lines before declarations\n" . $hereprev);
2759c4a62ef9SJoe Perches##		}
2760c4a62ef9SJoe Perches##
2761c4a62ef9SJoe Perches
2762de7d4f0eSAndy Whitcroft# closing brace should have a space following it when it has anything
2763de7d4f0eSAndy Whitcroft# on the line
2764de7d4f0eSAndy Whitcroft		if ($line =~ /}(?!(?:,|;|\)))\S/) {
2765000d1cc1SJoe Perches			ERROR("SPACING",
2766000d1cc1SJoe Perches			      "space required after that close brace '}'\n" . $herecurr);
27670a920b5bSAndy Whitcroft		}
27680a920b5bSAndy Whitcroft
276922f2a2efSAndy Whitcroft# check spacing on square brackets
277022f2a2efSAndy Whitcroft		if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
2771000d1cc1SJoe Perches			ERROR("SPACING",
2772000d1cc1SJoe Perches			      "space prohibited after that open square bracket '['\n" . $herecurr);
277322f2a2efSAndy Whitcroft		}
277422f2a2efSAndy Whitcroft		if ($line =~ /\s\]/) {
2775000d1cc1SJoe Perches			ERROR("SPACING",
2776000d1cc1SJoe Perches			      "space prohibited before that close square bracket ']'\n" . $herecurr);
277722f2a2efSAndy Whitcroft		}
277822f2a2efSAndy Whitcroft
2779c45dcabdSAndy Whitcroft# check spacing on parentheses
27809c0ca6f9SAndy Whitcroft		if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
27819c0ca6f9SAndy Whitcroft		    $line !~ /for\s*\(\s+;/) {
2782000d1cc1SJoe Perches			ERROR("SPACING",
2783000d1cc1SJoe Perches			      "space prohibited after that open parenthesis '('\n" . $herecurr);
278422f2a2efSAndy Whitcroft		}
278513214adfSAndy Whitcroft		if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
2786c45dcabdSAndy Whitcroft		    $line !~ /for\s*\(.*;\s+\)/ &&
2787c45dcabdSAndy Whitcroft		    $line !~ /:\s+\)/) {
2788000d1cc1SJoe Perches			ERROR("SPACING",
2789000d1cc1SJoe Perches			      "space prohibited before that close parenthesis ')'\n" . $herecurr);
279022f2a2efSAndy Whitcroft		}
279122f2a2efSAndy Whitcroft
27920a920b5bSAndy Whitcroft#goto labels aren't indented, allow a single space however
27934a0df2efSAndy Whitcroft		if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
27940a920b5bSAndy Whitcroft		   !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
2795000d1cc1SJoe Perches			WARN("INDENTED_LABEL",
2796000d1cc1SJoe Perches			     "labels should not be indented\n" . $herecurr);
27970a920b5bSAndy Whitcroft		}
27980a920b5bSAndy Whitcroft
2799c45dcabdSAndy Whitcroft# Return is not a function.
2800c45dcabdSAndy Whitcroft		if (defined($stat) && $stat =~ /^.\s*return(\s*)(\(.*);/s) {
2801c45dcabdSAndy Whitcroft			my $spacing = $1;
2802c45dcabdSAndy Whitcroft			my $value = $2;
2803c45dcabdSAndy Whitcroft
280486f9d059SAndy Whitcroft			# Flatten any parentheses
2805fb2d2c1bSAndy Whitcroft			$value =~ s/\(/ \(/g;
2806fb2d2c1bSAndy Whitcroft			$value =~ s/\)/\) /g;
2807e01886adSAndy Whitcroft			while ($value =~ s/\[[^\[\]]*\]/1/ ||
280863f17f89SAndy Whitcroft			       $value !~ /(?:$Ident|-?$Constant)\s*
280963f17f89SAndy Whitcroft					     $Compare\s*
281063f17f89SAndy Whitcroft					     (?:$Ident|-?$Constant)/x &&
281163f17f89SAndy Whitcroft			       $value =~ s/\([^\(\)]*\)/1/) {
2812c45dcabdSAndy Whitcroft			}
2813fb2d2c1bSAndy Whitcroft#print "value<$value>\n";
2814fb2d2c1bSAndy Whitcroft			if ($value =~ /^\s*(?:$Ident|-?$Constant)\s*$/) {
2815000d1cc1SJoe Perches				ERROR("RETURN_PARENTHESES",
2816000d1cc1SJoe Perches				      "return is not a function, parentheses are not required\n" . $herecurr);
2817c45dcabdSAndy Whitcroft
2818c45dcabdSAndy Whitcroft			} elsif ($spacing !~ /\s+/) {
2819000d1cc1SJoe Perches				ERROR("SPACING",
2820000d1cc1SJoe Perches				      "space required before the open parenthesis '('\n" . $herecurr);
2821c45dcabdSAndy Whitcroft			}
2822c45dcabdSAndy Whitcroft		}
282353a3c448SAndy Whitcroft# Return of what appears to be an errno should normally be -'ve
282453a3c448SAndy Whitcroft		if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) {
282553a3c448SAndy Whitcroft			my $name = $1;
282653a3c448SAndy Whitcroft			if ($name ne 'EOF' && $name ne 'ERROR') {
2827000d1cc1SJoe Perches				WARN("USE_NEGATIVE_ERRNO",
2828000d1cc1SJoe Perches				     "return of an errno should typically be -ve (return -$1)\n" . $herecurr);
282953a3c448SAndy Whitcroft			}
283053a3c448SAndy Whitcroft		}
2831c45dcabdSAndy Whitcroft
28320a920b5bSAndy Whitcroft# Need a space before open parenthesis after if, while etc
28334a0df2efSAndy Whitcroft		if ($line=~/\b(if|while|for|switch)\(/) {
2834000d1cc1SJoe Perches			ERROR("SPACING", "space required before the open parenthesis '('\n" . $herecurr);
28350a920b5bSAndy Whitcroft		}
28360a920b5bSAndy Whitcroft
2837f5fe35ddSAndy Whitcroft# Check for illegal assignment in if conditional -- and check for trailing
2838f5fe35ddSAndy Whitcroft# statements after the conditional.
2839170d3a22SAndy Whitcroft		if ($line =~ /do\s*(?!{)/) {
28403e469cdcSAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
28413e469cdcSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0)
28423e469cdcSAndy Whitcroft					if (!defined $stat);
2843170d3a22SAndy Whitcroft			my ($stat_next) = ctx_statement_block($line_nr_next,
2844170d3a22SAndy Whitcroft						$remain_next, $off_next);
2845170d3a22SAndy Whitcroft			$stat_next =~ s/\n./\n /g;
2846170d3a22SAndy Whitcroft			##print "stat<$stat> stat_next<$stat_next>\n";
2847170d3a22SAndy Whitcroft
2848170d3a22SAndy Whitcroft			if ($stat_next =~ /^\s*while\b/) {
2849170d3a22SAndy Whitcroft				# If the statement carries leading newlines,
2850170d3a22SAndy Whitcroft				# then count those as offsets.
2851170d3a22SAndy Whitcroft				my ($whitespace) =
2852170d3a22SAndy Whitcroft					($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
2853170d3a22SAndy Whitcroft				my $offset =
2854170d3a22SAndy Whitcroft					statement_rawlines($whitespace) - 1;
2855170d3a22SAndy Whitcroft
2856170d3a22SAndy Whitcroft				$suppress_whiletrailers{$line_nr_next +
2857170d3a22SAndy Whitcroft								$offset} = 1;
2858170d3a22SAndy Whitcroft			}
2859170d3a22SAndy Whitcroft		}
2860170d3a22SAndy Whitcroft		if (!defined $suppress_whiletrailers{$linenr} &&
2861170d3a22SAndy Whitcroft		    $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
2862171ae1a4SAndy Whitcroft			my ($s, $c) = ($stat, $cond);
28638905a67cSAndy Whitcroft
2864b53c8e10SAndy Whitcroft			if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
2865000d1cc1SJoe Perches				ERROR("ASSIGN_IN_IF",
2866000d1cc1SJoe Perches				      "do not use assignment in if condition\n" . $herecurr);
28678905a67cSAndy Whitcroft			}
28688905a67cSAndy Whitcroft
28698905a67cSAndy Whitcroft			# Find out what is on the end of the line after the
28708905a67cSAndy Whitcroft			# conditional.
2871773647a0SAndy Whitcroft			substr($s, 0, length($c), '');
28728905a67cSAndy Whitcroft			$s =~ s/\n.*//g;
287313214adfSAndy Whitcroft			$s =~ s/$;//g; 	# Remove any comments
287453210168SAndy Whitcroft			if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
287553210168SAndy Whitcroft			    $c !~ /}\s*while\s*/)
2876773647a0SAndy Whitcroft			{
2877bb44ad39SAndy Whitcroft				# Find out how long the conditional actually is.
2878bb44ad39SAndy Whitcroft				my @newlines = ($c =~ /\n/gs);
2879bb44ad39SAndy Whitcroft				my $cond_lines = 1 + $#newlines;
288042bdf74cSHidetoshi Seto				my $stat_real = '';
2881bb44ad39SAndy Whitcroft
288242bdf74cSHidetoshi Seto				$stat_real = raw_line($linenr, $cond_lines)
288342bdf74cSHidetoshi Seto							. "\n" if ($cond_lines);
2884bb44ad39SAndy Whitcroft				if (defined($stat_real) && $cond_lines > 1) {
2885bb44ad39SAndy Whitcroft					$stat_real = "[...]\n$stat_real";
2886bb44ad39SAndy Whitcroft				}
2887bb44ad39SAndy Whitcroft
2888000d1cc1SJoe Perches				ERROR("TRAILING_STATEMENTS",
2889000d1cc1SJoe Perches				      "trailing statements should be on next line\n" . $herecurr . $stat_real);
28908905a67cSAndy Whitcroft			}
28918905a67cSAndy Whitcroft		}
28928905a67cSAndy Whitcroft
289313214adfSAndy Whitcroft# Check for bitwise tests written as boolean
289413214adfSAndy Whitcroft		if ($line =~ /
289513214adfSAndy Whitcroft			(?:
289613214adfSAndy Whitcroft				(?:\[|\(|\&\&|\|\|)
289713214adfSAndy Whitcroft				\s*0[xX][0-9]+\s*
289813214adfSAndy Whitcroft				(?:\&\&|\|\|)
289913214adfSAndy Whitcroft			|
290013214adfSAndy Whitcroft				(?:\&\&|\|\|)
290113214adfSAndy Whitcroft				\s*0[xX][0-9]+\s*
290213214adfSAndy Whitcroft				(?:\&\&|\|\||\)|\])
290313214adfSAndy Whitcroft			)/x)
290413214adfSAndy Whitcroft		{
2905000d1cc1SJoe Perches			WARN("HEXADECIMAL_BOOLEAN_TEST",
2906000d1cc1SJoe Perches			     "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
290713214adfSAndy Whitcroft		}
290813214adfSAndy Whitcroft
29098905a67cSAndy Whitcroft# if and else should not have general statements after it
291013214adfSAndy Whitcroft		if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
291113214adfSAndy Whitcroft			my $s = $1;
291213214adfSAndy Whitcroft			$s =~ s/$;//g; 	# Remove any comments
291313214adfSAndy Whitcroft			if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
2914000d1cc1SJoe Perches				ERROR("TRAILING_STATEMENTS",
2915000d1cc1SJoe Perches				      "trailing statements should be on next line\n" . $herecurr);
29160a920b5bSAndy Whitcroft			}
291713214adfSAndy Whitcroft		}
291839667782SAndy Whitcroft# if should not continue a brace
291939667782SAndy Whitcroft		if ($line =~ /}\s*if\b/) {
2920000d1cc1SJoe Perches			ERROR("TRAILING_STATEMENTS",
2921000d1cc1SJoe Perches			      "trailing statements should be on next line\n" .
292239667782SAndy Whitcroft				$herecurr);
292339667782SAndy Whitcroft		}
2924a1080bf8SAndy Whitcroft# case and default should not have general statements after them
2925a1080bf8SAndy Whitcroft		if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
2926a1080bf8SAndy Whitcroft		    $line !~ /\G(?:
29273fef12d6SAndy Whitcroft			(?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
2928a1080bf8SAndy Whitcroft			\s*return\s+
2929a1080bf8SAndy Whitcroft		    )/xg)
2930a1080bf8SAndy Whitcroft		{
2931000d1cc1SJoe Perches			ERROR("TRAILING_STATEMENTS",
2932000d1cc1SJoe Perches			      "trailing statements should be on next line\n" . $herecurr);
2933a1080bf8SAndy Whitcroft		}
29340a920b5bSAndy Whitcroft
29350a920b5bSAndy Whitcroft		# Check for }<nl>else {, these must be at the same
29360a920b5bSAndy Whitcroft		# indent level to be relevant to each other.
29370a920b5bSAndy Whitcroft		if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
29380a920b5bSAndy Whitcroft						$previndent == $indent) {
2939000d1cc1SJoe Perches			ERROR("ELSE_AFTER_BRACE",
2940000d1cc1SJoe Perches			      "else should follow close brace '}'\n" . $hereprev);
29410a920b5bSAndy Whitcroft		}
29420a920b5bSAndy Whitcroft
2943c2fdda0dSAndy Whitcroft		if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and
2944c2fdda0dSAndy Whitcroft						$previndent == $indent) {
2945c2fdda0dSAndy Whitcroft			my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
2946c2fdda0dSAndy Whitcroft
2947c2fdda0dSAndy Whitcroft			# Find out what is on the end of the line after the
2948c2fdda0dSAndy Whitcroft			# conditional.
2949773647a0SAndy Whitcroft			substr($s, 0, length($c), '');
2950c2fdda0dSAndy Whitcroft			$s =~ s/\n.*//g;
2951c2fdda0dSAndy Whitcroft
2952c2fdda0dSAndy Whitcroft			if ($s =~ /^\s*;/) {
2953000d1cc1SJoe Perches				ERROR("WHILE_AFTER_BRACE",
2954000d1cc1SJoe Perches				      "while should follow close brace '}'\n" . $hereprev);
2955c2fdda0dSAndy Whitcroft			}
2956c2fdda0dSAndy Whitcroft		}
2957c2fdda0dSAndy Whitcroft
295895e2c602SJoe Perches#Specific variable tests
2959323c1260SJoe Perches		while ($line =~ m{($Constant|$Lval)}g) {
2960323c1260SJoe Perches			my $var = $1;
296195e2c602SJoe Perches
296295e2c602SJoe Perches#gcc binary extension
296395e2c602SJoe Perches			if ($var =~ /^$Binary$/) {
296495e2c602SJoe Perches				WARN("GCC_BINARY_CONSTANT",
296595e2c602SJoe Perches				     "Avoid gcc v4.3+ binary constant extension: <$var>\n" . $herecurr);
296695e2c602SJoe Perches			}
296795e2c602SJoe Perches
296895e2c602SJoe Perches#CamelCase
2969807bd26cSJoe Perches			if ($var !~ /^$Constant$/ &&
2970be79794bSJoe Perches			    $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
2971807bd26cSJoe Perches			    $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
2972323c1260SJoe Perches			    !defined $camelcase{$var}) {
2973323c1260SJoe Perches				$camelcase{$var} = 1;
2974be79794bSJoe Perches				CHK("CAMELCASE",
2975323c1260SJoe Perches				    "Avoid CamelCase: <$var>\n" . $herecurr);
2976323c1260SJoe Perches			}
2977323c1260SJoe Perches		}
29780a920b5bSAndy Whitcroft
29790a920b5bSAndy Whitcroft#no spaces allowed after \ in define
2980c45dcabdSAndy Whitcroft		if ($line=~/\#\s*define.*\\\s$/) {
2981000d1cc1SJoe Perches			WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
2982000d1cc1SJoe Perches			     "Whitepspace after \\ makes next lines useless\n" . $herecurr);
29830a920b5bSAndy Whitcroft		}
29840a920b5bSAndy Whitcroft
2985653d4876SAndy Whitcroft#warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
2986c45dcabdSAndy Whitcroft		if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
2987e09dec48SAndy Whitcroft			my $file = "$1.h";
2988e09dec48SAndy Whitcroft			my $checkfile = "include/linux/$file";
2989e09dec48SAndy Whitcroft			if (-f "$root/$checkfile" &&
2990e09dec48SAndy Whitcroft			    $realfile ne $checkfile &&
29917840a94cSWolfram Sang			    $1 !~ /$allowed_asm_includes/)
2992c45dcabdSAndy Whitcroft			{
2993e09dec48SAndy Whitcroft				if ($realfile =~ m{^arch/}) {
2994000d1cc1SJoe Perches					CHK("ARCH_INCLUDE_LINUX",
2995000d1cc1SJoe Perches					    "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
2996e09dec48SAndy Whitcroft				} else {
2997000d1cc1SJoe Perches					WARN("INCLUDE_LINUX",
2998000d1cc1SJoe Perches					     "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
2999e09dec48SAndy Whitcroft				}
30000a920b5bSAndy Whitcroft			}
30010a920b5bSAndy Whitcroft		}
30020a920b5bSAndy Whitcroft
3003653d4876SAndy Whitcroft# multi-statement macros should be enclosed in a do while loop, grab the
3004653d4876SAndy Whitcroft# first statement and ensure its the whole macro if its not enclosed
3005cf655043SAndy Whitcroft# in a known good container
3006b8f96a31SAndy Whitcroft		if ($realfile !~ m@/vmlinux.lds.h$@ &&
3007b8f96a31SAndy Whitcroft		    $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
3008d8aaf121SAndy Whitcroft			my $ln = $linenr;
3009d8aaf121SAndy Whitcroft			my $cnt = $realcnt;
3010c45dcabdSAndy Whitcroft			my ($off, $dstat, $dcond, $rest);
3011c45dcabdSAndy Whitcroft			my $ctx = '';
3012c45dcabdSAndy Whitcroft			($dstat, $dcond, $ln, $cnt, $off) =
3013f74bd194SAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0);
3014f74bd194SAndy Whitcroft			$ctx = $dstat;
3015c45dcabdSAndy Whitcroft			#print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
3016a3bb97a7SAndy Whitcroft			#print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
3017c45dcabdSAndy Whitcroft
3018f74bd194SAndy Whitcroft			$dstat =~ s/^.\s*\#\s*define\s+$Ident(?:\([^\)]*\))?\s*//;
3019292f1a9bSAndy Whitcroft			$dstat =~ s/$;//g;
3020c45dcabdSAndy Whitcroft			$dstat =~ s/\\\n.//g;
3021c45dcabdSAndy Whitcroft			$dstat =~ s/^\s*//s;
3022c45dcabdSAndy Whitcroft			$dstat =~ s/\s*$//s;
3023c45dcabdSAndy Whitcroft
3024c45dcabdSAndy Whitcroft			# Flatten any parentheses and braces
3025bf30d6edSAndy Whitcroft			while ($dstat =~ s/\([^\(\)]*\)/1/ ||
3026bf30d6edSAndy Whitcroft			       $dstat =~ s/\{[^\{\}]*\}/1/ ||
3027c81769fdSAndy Whitcroft			       $dstat =~ s/\[[^\[\]]*\]/1/)
3028bf30d6edSAndy Whitcroft			{
3029c45dcabdSAndy Whitcroft			}
3030c45dcabdSAndy Whitcroft
3031e45bab8eSAndy Whitcroft			# Flatten any obvious string concatentation.
3032e45bab8eSAndy Whitcroft			while ($dstat =~ s/("X*")\s*$Ident/$1/ ||
3033e45bab8eSAndy Whitcroft			       $dstat =~ s/$Ident\s*("X*")/$1/)
3034e45bab8eSAndy Whitcroft			{
3035e45bab8eSAndy Whitcroft			}
3036e45bab8eSAndy Whitcroft
3037c45dcabdSAndy Whitcroft			my $exceptions = qr{
3038c45dcabdSAndy Whitcroft				$Declare|
3039c45dcabdSAndy Whitcroft				module_param_named|
3040a0a0a7a9SKees Cook				MODULE_PARM_DESC|
3041c45dcabdSAndy Whitcroft				DECLARE_PER_CPU|
3042c45dcabdSAndy Whitcroft				DEFINE_PER_CPU|
3043383099fdSAndy Whitcroft				__typeof__\(|
304422fd2d3eSStefani Seibold				union|
304522fd2d3eSStefani Seibold				struct|
3046ea71a0a0SAndy Whitcroft				\.$Ident\s*=\s*|
3047ea71a0a0SAndy Whitcroft				^\"|\"$
3048c45dcabdSAndy Whitcroft			}x;
30495eaa20b9SAndy Whitcroft			#print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
3050f74bd194SAndy Whitcroft			if ($dstat ne '' &&
3051f74bd194SAndy Whitcroft			    $dstat !~ /^(?:$Ident|-?$Constant),$/ &&			# 10, // foo(),
3052f74bd194SAndy Whitcroft			    $dstat !~ /^(?:$Ident|-?$Constant);$/ &&			# foo();
30533cc4b1c3SJoe Perches			    $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ &&		# 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
3054b9df76acSAndy Whitcroft			    $dstat !~ /^'X'$/ &&					# character constants
3055f74bd194SAndy Whitcroft			    $dstat !~ /$exceptions/ &&
3056f74bd194SAndy Whitcroft			    $dstat !~ /^\.$Ident\s*=/ &&				# .foo =
3057e942e2c3SJoe Perches			    $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ &&		# stringification #foo
305872f115f9SAndy Whitcroft			    $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ &&	# do {...} while (...); // do {...} while (...)
3059f74bd194SAndy Whitcroft			    $dstat !~ /^for\s*$Constant$/ &&				# for (...)
3060f74bd194SAndy Whitcroft			    $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ &&	# for (...) bar()
3061f74bd194SAndy Whitcroft			    $dstat !~ /^do\s*{/ &&					# do {...
3062f74bd194SAndy Whitcroft			    $dstat !~ /^\({/)						# ({...
3063c45dcabdSAndy Whitcroft			{
3064f74bd194SAndy Whitcroft				$ctx =~ s/\n*$//;
3065f74bd194SAndy Whitcroft				my $herectx = $here . "\n";
3066f74bd194SAndy Whitcroft				my $cnt = statement_rawlines($ctx);
3067f74bd194SAndy Whitcroft
3068f74bd194SAndy Whitcroft				for (my $n = 0; $n < $cnt; $n++) {
3069f74bd194SAndy Whitcroft					$herectx .= raw_line($linenr, $n) . "\n";
3070c45dcabdSAndy Whitcroft				}
3071c45dcabdSAndy Whitcroft
3072f74bd194SAndy Whitcroft				if ($dstat =~ /;/) {
3073f74bd194SAndy Whitcroft					ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
3074f74bd194SAndy Whitcroft					      "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
3075f74bd194SAndy Whitcroft				} else {
3076000d1cc1SJoe Perches					ERROR("COMPLEX_MACRO",
3077f74bd194SAndy Whitcroft					      "Macros with complex values should be enclosed in parenthesis\n" . "$herectx");
3078d8aaf121SAndy Whitcroft				}
30790a920b5bSAndy Whitcroft			}
30805023d347SJoe Perches
3081481eb486SJoe Perches# check for line continuations outside of #defines, preprocessor #, and asm
30825023d347SJoe Perches
30835023d347SJoe Perches		} else {
30845023d347SJoe Perches			if ($prevline !~ /^..*\\$/ &&
3085481eb486SJoe Perches			    $line !~ /^\+\s*\#.*\\$/ &&		# preprocessor
3086481eb486SJoe Perches			    $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ &&	# asm
30875023d347SJoe Perches			    $line =~ /^\+.*\\$/) {
30885023d347SJoe Perches				WARN("LINE_CONTINUATIONS",
30895023d347SJoe Perches				     "Avoid unnecessary line continuations\n" . $herecurr);
30905023d347SJoe Perches			}
3091653d4876SAndy Whitcroft		}
30920a920b5bSAndy Whitcroft
3093b13edf7fSJoe Perches# do {} while (0) macro tests:
3094b13edf7fSJoe Perches# single-statement macros do not need to be enclosed in do while (0) loop,
3095b13edf7fSJoe Perches# macro should not end with a semicolon
3096b13edf7fSJoe Perches		if ($^V && $^V ge 5.10.0 &&
3097b13edf7fSJoe Perches		    $realfile !~ m@/vmlinux.lds.h$@ &&
3098b13edf7fSJoe Perches		    $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
3099b13edf7fSJoe Perches			my $ln = $linenr;
3100b13edf7fSJoe Perches			my $cnt = $realcnt;
3101b13edf7fSJoe Perches			my ($off, $dstat, $dcond, $rest);
3102b13edf7fSJoe Perches			my $ctx = '';
3103b13edf7fSJoe Perches			($dstat, $dcond, $ln, $cnt, $off) =
3104b13edf7fSJoe Perches				ctx_statement_block($linenr, $realcnt, 0);
3105b13edf7fSJoe Perches			$ctx = $dstat;
3106b13edf7fSJoe Perches
3107b13edf7fSJoe Perches			$dstat =~ s/\\\n.//g;
3108b13edf7fSJoe Perches
3109b13edf7fSJoe Perches			if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
3110b13edf7fSJoe Perches				my $stmts = $2;
3111b13edf7fSJoe Perches				my $semis = $3;
3112b13edf7fSJoe Perches
3113b13edf7fSJoe Perches				$ctx =~ s/\n*$//;
3114b13edf7fSJoe Perches				my $cnt = statement_rawlines($ctx);
3115b13edf7fSJoe Perches				my $herectx = $here . "\n";
3116b13edf7fSJoe Perches
3117b13edf7fSJoe Perches				for (my $n = 0; $n < $cnt; $n++) {
3118b13edf7fSJoe Perches					$herectx .= raw_line($linenr, $n) . "\n";
3119b13edf7fSJoe Perches				}
3120b13edf7fSJoe Perches
3121ac8e97f8SJoe Perches				if (($stmts =~ tr/;/;/) == 1 &&
3122ac8e97f8SJoe Perches				    $stmts !~ /^\s*(if|while|for|switch)\b/) {
3123b13edf7fSJoe Perches					WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
3124b13edf7fSJoe Perches					     "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
3125b13edf7fSJoe Perches				}
3126b13edf7fSJoe Perches				if (defined $semis && $semis ne "") {
3127b13edf7fSJoe Perches					WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
3128b13edf7fSJoe Perches					     "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
3129b13edf7fSJoe Perches				}
3130b13edf7fSJoe Perches			}
3131b13edf7fSJoe Perches		}
3132b13edf7fSJoe Perches
3133080ba929SMike Frysinger# make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
3134080ba929SMike Frysinger# all assignments may have only one of the following with an assignment:
3135080ba929SMike Frysinger#	.
3136080ba929SMike Frysinger#	ALIGN(...)
3137080ba929SMike Frysinger#	VMLINUX_SYMBOL(...)
3138080ba929SMike Frysinger		if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
3139000d1cc1SJoe Perches			WARN("MISSING_VMLINUX_SYMBOL",
3140000d1cc1SJoe Perches			     "vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
3141080ba929SMike Frysinger		}
3142080ba929SMike Frysinger
3143f0a594c1SAndy Whitcroft# check for redundant bracing round if etc
314413214adfSAndy Whitcroft		if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
314513214adfSAndy Whitcroft			my ($level, $endln, @chunks) =
3146cf655043SAndy Whitcroft				ctx_statement_full($linenr, $realcnt, 1);
314713214adfSAndy Whitcroft			#print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
3148cf655043SAndy Whitcroft			#print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
3149cf655043SAndy Whitcroft			if ($#chunks > 0 && $level == 0) {
3150aad4f614SJoe Perches				my @allowed = ();
3151aad4f614SJoe Perches				my $allow = 0;
315213214adfSAndy Whitcroft				my $seen = 0;
3153773647a0SAndy Whitcroft				my $herectx = $here . "\n";
3154cf655043SAndy Whitcroft				my $ln = $linenr - 1;
315513214adfSAndy Whitcroft				for my $chunk (@chunks) {
315613214adfSAndy Whitcroft					my ($cond, $block) = @{$chunk};
315713214adfSAndy Whitcroft
3158773647a0SAndy Whitcroft					# If the condition carries leading newlines, then count those as offsets.
3159773647a0SAndy Whitcroft					my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
3160773647a0SAndy Whitcroft					my $offset = statement_rawlines($whitespace) - 1;
3161773647a0SAndy Whitcroft
3162aad4f614SJoe Perches					$allowed[$allow] = 0;
3163773647a0SAndy Whitcroft					#print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
3164773647a0SAndy Whitcroft
3165773647a0SAndy Whitcroft					# We have looked at and allowed this specific line.
3166773647a0SAndy Whitcroft					$suppress_ifbraces{$ln + $offset} = 1;
3167773647a0SAndy Whitcroft
3168773647a0SAndy Whitcroft					$herectx .= "$rawlines[$ln + $offset]\n[...]\n";
3169cf655043SAndy Whitcroft					$ln += statement_rawlines($block) - 1;
3170cf655043SAndy Whitcroft
3171773647a0SAndy Whitcroft					substr($block, 0, length($cond), '');
317213214adfSAndy Whitcroft
317313214adfSAndy Whitcroft					$seen++ if ($block =~ /^\s*{/);
317413214adfSAndy Whitcroft
3175aad4f614SJoe Perches					#print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
3176cf655043SAndy Whitcroft					if (statement_lines($cond) > 1) {
3177cf655043SAndy Whitcroft						#print "APW: ALLOWED: cond<$cond>\n";
3178aad4f614SJoe Perches						$allowed[$allow] = 1;
317913214adfSAndy Whitcroft					}
318013214adfSAndy Whitcroft					if ($block =~/\b(?:if|for|while)\b/) {
3181cf655043SAndy Whitcroft						#print "APW: ALLOWED: block<$block>\n";
3182aad4f614SJoe Perches						$allowed[$allow] = 1;
318313214adfSAndy Whitcroft					}
3184cf655043SAndy Whitcroft					if (statement_block_size($block) > 1) {
3185cf655043SAndy Whitcroft						#print "APW: ALLOWED: lines block<$block>\n";
3186aad4f614SJoe Perches						$allowed[$allow] = 1;
318713214adfSAndy Whitcroft					}
3188aad4f614SJoe Perches					$allow++;
318913214adfSAndy Whitcroft				}
3190aad4f614SJoe Perches				if ($seen) {
3191aad4f614SJoe Perches					my $sum_allowed = 0;
3192aad4f614SJoe Perches					foreach (@allowed) {
3193aad4f614SJoe Perches						$sum_allowed += $_;
3194aad4f614SJoe Perches					}
3195aad4f614SJoe Perches					if ($sum_allowed == 0) {
3196000d1cc1SJoe Perches						WARN("BRACES",
3197000d1cc1SJoe Perches						     "braces {} are not necessary for any arm of this statement\n" . $herectx);
3198aad4f614SJoe Perches					} elsif ($sum_allowed != $allow &&
3199aad4f614SJoe Perches						 $seen != $allow) {
3200aad4f614SJoe Perches						CHK("BRACES",
3201aad4f614SJoe Perches						    "braces {} should be used on all arms of this statement\n" . $herectx);
3202aad4f614SJoe Perches					}
320313214adfSAndy Whitcroft				}
320413214adfSAndy Whitcroft			}
320513214adfSAndy Whitcroft		}
3206773647a0SAndy Whitcroft		if (!defined $suppress_ifbraces{$linenr - 1} &&
320713214adfSAndy Whitcroft					$line =~ /\b(if|while|for|else)\b/) {
3208cf655043SAndy Whitcroft			my $allowed = 0;
3209f0a594c1SAndy Whitcroft
3210cf655043SAndy Whitcroft			# Check the pre-context.
3211cf655043SAndy Whitcroft			if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
3212cf655043SAndy Whitcroft				#print "APW: ALLOWED: pre<$1>\n";
3213cf655043SAndy Whitcroft				$allowed = 1;
3214f0a594c1SAndy Whitcroft			}
3215773647a0SAndy Whitcroft
3216773647a0SAndy Whitcroft			my ($level, $endln, @chunks) =
3217773647a0SAndy Whitcroft				ctx_statement_full($linenr, $realcnt, $-[0]);
3218773647a0SAndy Whitcroft
3219cf655043SAndy Whitcroft			# Check the condition.
3220cf655043SAndy Whitcroft			my ($cond, $block) = @{$chunks[0]};
3221773647a0SAndy Whitcroft			#print "CHECKING<$linenr> cond<$cond> block<$block>\n";
3222cf655043SAndy Whitcroft			if (defined $cond) {
3223773647a0SAndy Whitcroft				substr($block, 0, length($cond), '');
3224cf655043SAndy Whitcroft			}
3225cf655043SAndy Whitcroft			if (statement_lines($cond) > 1) {
3226cf655043SAndy Whitcroft				#print "APW: ALLOWED: cond<$cond>\n";
3227cf655043SAndy Whitcroft				$allowed = 1;
3228cf655043SAndy Whitcroft			}
3229cf655043SAndy Whitcroft			if ($block =~/\b(?:if|for|while)\b/) {
3230cf655043SAndy Whitcroft				#print "APW: ALLOWED: block<$block>\n";
3231cf655043SAndy Whitcroft				$allowed = 1;
3232cf655043SAndy Whitcroft			}
3233cf655043SAndy Whitcroft			if (statement_block_size($block) > 1) {
3234cf655043SAndy Whitcroft				#print "APW: ALLOWED: lines block<$block>\n";
3235cf655043SAndy Whitcroft				$allowed = 1;
3236cf655043SAndy Whitcroft			}
3237cf655043SAndy Whitcroft			# Check the post-context.
3238cf655043SAndy Whitcroft			if (defined $chunks[1]) {
3239cf655043SAndy Whitcroft				my ($cond, $block) = @{$chunks[1]};
3240cf655043SAndy Whitcroft				if (defined $cond) {
3241773647a0SAndy Whitcroft					substr($block, 0, length($cond), '');
3242cf655043SAndy Whitcroft				}
3243cf655043SAndy Whitcroft				if ($block =~ /^\s*\{/) {
3244cf655043SAndy Whitcroft					#print "APW: ALLOWED: chunk-1 block<$block>\n";
3245cf655043SAndy Whitcroft					$allowed = 1;
3246cf655043SAndy Whitcroft				}
3247cf655043SAndy Whitcroft			}
3248cf655043SAndy Whitcroft			if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
324969932487SJustin P. Mattock				my $herectx = $here . "\n";
3250f055663cSAndy Whitcroft				my $cnt = statement_rawlines($block);
3251cf655043SAndy Whitcroft
3252f055663cSAndy Whitcroft				for (my $n = 0; $n < $cnt; $n++) {
325369932487SJustin P. Mattock					$herectx .= raw_line($linenr, $n) . "\n";
3254cf655043SAndy Whitcroft				}
3255cf655043SAndy Whitcroft
3256000d1cc1SJoe Perches				WARN("BRACES",
3257000d1cc1SJoe Perches				     "braces {} are not necessary for single statement blocks\n" . $herectx);
3258f0a594c1SAndy Whitcroft			}
3259f0a594c1SAndy Whitcroft		}
3260f0a594c1SAndy Whitcroft
32610979ae66SJoe Perches# check for unnecessary blank lines around braces
326277b9a53aSJoe Perches		if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
32630979ae66SJoe Perches			CHK("BRACES",
32640979ae66SJoe Perches			    "Blank lines aren't necessary before a close brace '}'\n" . $hereprev);
32650979ae66SJoe Perches		}
326677b9a53aSJoe Perches		if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
32670979ae66SJoe Perches			CHK("BRACES",
32680979ae66SJoe Perches			    "Blank lines aren't necessary after an open brace '{'\n" . $hereprev);
32690979ae66SJoe Perches		}
32700979ae66SJoe Perches
32714a0df2efSAndy Whitcroft# no volatiles please
32726c72ffaaSAndy Whitcroft		my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
32736c72ffaaSAndy Whitcroft		if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
3274000d1cc1SJoe Perches			WARN("VOLATILE",
3275000d1cc1SJoe Perches			     "Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
32764a0df2efSAndy Whitcroft		}
32774a0df2efSAndy Whitcroft
327800df344fSAndy Whitcroft# warn about #if 0
3279c45dcabdSAndy Whitcroft		if ($line =~ /^.\s*\#\s*if\s+0\b/) {
3280000d1cc1SJoe Perches			CHK("REDUNDANT_CODE",
3281000d1cc1SJoe Perches			    "if this code is redundant consider removing it\n" .
3282de7d4f0eSAndy Whitcroft				$herecurr);
32834a0df2efSAndy Whitcroft		}
32844a0df2efSAndy Whitcroft
328503df4b51SAndy Whitcroft# check for needless "if (<foo>) fn(<foo>)" uses
328603df4b51SAndy Whitcroft		if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
328703df4b51SAndy Whitcroft			my $expr = '\s*\(\s*' . quotemeta($1) . '\s*\)\s*;';
328803df4b51SAndy Whitcroft			if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?)$expr/) {
328903df4b51SAndy Whitcroft				WARN('NEEDLESS_IF',
329003df4b51SAndy Whitcroft				     "$1(NULL) is safe this check is probably not required\n" . $hereprev);
32914c432a8fSGreg Kroah-Hartman			}
32924c432a8fSGreg Kroah-Hartman		}
3293f0a594c1SAndy Whitcroft
32941a15a250SPatrick Pannuto# prefer usleep_range over udelay
329537581c28SBruce Allan		if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
32961a15a250SPatrick Pannuto			# ignore udelay's < 10, however
329737581c28SBruce Allan			if (! ($1 < 10) ) {
3298000d1cc1SJoe Perches				CHK("USLEEP_RANGE",
3299000d1cc1SJoe Perches				    "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $line);
33001a15a250SPatrick Pannuto			}
33011a15a250SPatrick Pannuto		}
33021a15a250SPatrick Pannuto
330309ef8725SPatrick Pannuto# warn about unexpectedly long msleep's
330409ef8725SPatrick Pannuto		if ($line =~ /\bmsleep\s*\((\d+)\);/) {
330509ef8725SPatrick Pannuto			if ($1 < 20) {
3306000d1cc1SJoe Perches				WARN("MSLEEP",
3307000d1cc1SJoe Perches				     "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $line);
330809ef8725SPatrick Pannuto			}
330909ef8725SPatrick Pannuto		}
331009ef8725SPatrick Pannuto
331136ec1939SJoe Perches# check for comparisons of jiffies
331236ec1939SJoe Perches		if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
331336ec1939SJoe Perches			WARN("JIFFIES_COMPARISON",
331436ec1939SJoe Perches			     "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
331536ec1939SJoe Perches		}
331636ec1939SJoe Perches
33179d7a34a5SJoe Perches# check for comparisons of get_jiffies_64()
33189d7a34a5SJoe Perches		if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
33199d7a34a5SJoe Perches			WARN("JIFFIES_COMPARISON",
33209d7a34a5SJoe Perches			     "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
33219d7a34a5SJoe Perches		}
33229d7a34a5SJoe Perches
332300df344fSAndy Whitcroft# warn about #ifdefs in C files
3324c45dcabdSAndy Whitcroft#		if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
332500df344fSAndy Whitcroft#			print "#ifdef in C files should be avoided\n";
332600df344fSAndy Whitcroft#			print "$herecurr";
332700df344fSAndy Whitcroft#			$clean = 0;
332800df344fSAndy Whitcroft#		}
332900df344fSAndy Whitcroft
333022f2a2efSAndy Whitcroft# warn about spacing in #ifdefs
3331c45dcabdSAndy Whitcroft		if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
3332000d1cc1SJoe Perches			ERROR("SPACING",
3333000d1cc1SJoe Perches			      "exactly one space required after that #$1\n" . $herecurr);
333422f2a2efSAndy Whitcroft		}
333522f2a2efSAndy Whitcroft
33364a0df2efSAndy Whitcroft# check for spinlock_t definitions without a comment.
3337171ae1a4SAndy Whitcroft		if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
3338171ae1a4SAndy Whitcroft		    $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
33394a0df2efSAndy Whitcroft			my $which = $1;
33404a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
3341000d1cc1SJoe Perches				CHK("UNCOMMENTED_DEFINITION",
3342000d1cc1SJoe Perches				    "$1 definition without comment\n" . $herecurr);
33434a0df2efSAndy Whitcroft			}
33444a0df2efSAndy Whitcroft		}
33454a0df2efSAndy Whitcroft# check for memory barriers without a comment.
33464a0df2efSAndy Whitcroft		if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
33474a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
3348000d1cc1SJoe Perches				CHK("MEMORY_BARRIER",
3349000d1cc1SJoe Perches				    "memory barrier without comment\n" . $herecurr);
33504a0df2efSAndy Whitcroft			}
33514a0df2efSAndy Whitcroft		}
33524a0df2efSAndy Whitcroft# check of hardware specific defines
3353c45dcabdSAndy Whitcroft		if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
3354000d1cc1SJoe Perches			CHK("ARCH_DEFINES",
3355000d1cc1SJoe Perches			    "architecture specific defines should be avoided\n" .  $herecurr);
33560a920b5bSAndy Whitcroft		}
3357653d4876SAndy Whitcroft
3358d4977c78STobias Klauser# Check that the storage class is at the beginning of a declaration
3359d4977c78STobias Klauser		if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
3360000d1cc1SJoe Perches			WARN("STORAGE_CLASS",
3361000d1cc1SJoe Perches			     "storage class should be at the beginning of the declaration\n" . $herecurr)
3362d4977c78STobias Klauser		}
3363d4977c78STobias Klauser
3364de7d4f0eSAndy Whitcroft# check the location of the inline attribute, that it is between
3365de7d4f0eSAndy Whitcroft# storage class and type.
33669c0ca6f9SAndy Whitcroft		if ($line =~ /\b$Type\s+$Inline\b/ ||
33679c0ca6f9SAndy Whitcroft		    $line =~ /\b$Inline\s+$Storage\b/) {
3368000d1cc1SJoe Perches			ERROR("INLINE_LOCATION",
3369000d1cc1SJoe Perches			      "inline keyword should sit between storage class and type\n" . $herecurr);
3370de7d4f0eSAndy Whitcroft		}
3371de7d4f0eSAndy Whitcroft
33728905a67cSAndy Whitcroft# Check for __inline__ and __inline, prefer inline
33738905a67cSAndy Whitcroft		if ($line =~ /\b(__inline__|__inline)\b/) {
3374000d1cc1SJoe Perches			WARN("INLINE",
3375000d1cc1SJoe Perches			     "plain inline is preferred over $1\n" . $herecurr);
33768905a67cSAndy Whitcroft		}
33778905a67cSAndy Whitcroft
33783d130fd0SJoe Perches# Check for __attribute__ packed, prefer __packed
33793d130fd0SJoe Perches		if ($line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
3380000d1cc1SJoe Perches			WARN("PREFER_PACKED",
3381000d1cc1SJoe Perches			     "__packed is preferred over __attribute__((packed))\n" . $herecurr);
33823d130fd0SJoe Perches		}
33833d130fd0SJoe Perches
338439b7e287SJoe Perches# Check for __attribute__ aligned, prefer __aligned
338539b7e287SJoe Perches		if ($line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
3386000d1cc1SJoe Perches			WARN("PREFER_ALIGNED",
3387000d1cc1SJoe Perches			     "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
338839b7e287SJoe Perches		}
338939b7e287SJoe Perches
33905f14d3bdSJoe Perches# Check for __attribute__ format(printf, prefer __printf
33915f14d3bdSJoe Perches		if ($line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
33925f14d3bdSJoe Perches			WARN("PREFER_PRINTF",
33935f14d3bdSJoe Perches			     "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr);
33945f14d3bdSJoe Perches		}
33955f14d3bdSJoe Perches
33966061d949SJoe Perches# Check for __attribute__ format(scanf, prefer __scanf
33976061d949SJoe Perches		if ($line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
33986061d949SJoe Perches			WARN("PREFER_SCANF",
33996061d949SJoe Perches			     "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr);
34006061d949SJoe Perches		}
34016061d949SJoe Perches
34028f53a9b8SJoe Perches# check for sizeof(&)
34038f53a9b8SJoe Perches		if ($line =~ /\bsizeof\s*\(\s*\&/) {
3404000d1cc1SJoe Perches			WARN("SIZEOF_ADDRESS",
3405000d1cc1SJoe Perches			     "sizeof(& should be avoided\n" . $herecurr);
34068f53a9b8SJoe Perches		}
34078f53a9b8SJoe Perches
340866c80b60SJoe Perches# check for sizeof without parenthesis
340966c80b60SJoe Perches		if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
341066c80b60SJoe Perches			WARN("SIZEOF_PARENTHESIS",
341166c80b60SJoe Perches			     "sizeof $1 should be sizeof($1)\n" . $herecurr);
341266c80b60SJoe Perches		}
341366c80b60SJoe Perches
3414428e2fdcSJoe Perches# check for line continuations in quoted strings with odd counts of "
3415428e2fdcSJoe Perches		if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) {
3416000d1cc1SJoe Perches			WARN("LINE_CONTINUATIONS",
3417000d1cc1SJoe Perches			     "Avoid line continuations in quoted strings\n" . $herecurr);
3418428e2fdcSJoe Perches		}
3419428e2fdcSJoe Perches
342088982feaSJoe Perches# check for struct spinlock declarations
342188982feaSJoe Perches		if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
342288982feaSJoe Perches			WARN("USE_SPINLOCK_T",
342388982feaSJoe Perches			     "struct spinlock should be spinlock_t\n" . $herecurr);
342488982feaSJoe Perches		}
342588982feaSJoe Perches
3426a6962d72SJoe Perches# check for seq_printf uses that could be seq_puts
3427a6962d72SJoe Perches		if ($line =~ /\bseq_printf\s*\(/) {
3428a6962d72SJoe Perches			my $fmt = get_quoted_string($line, $rawline);
3429a6962d72SJoe Perches			if ($fmt !~ /[^\\]\%/) {
3430a6962d72SJoe Perches				WARN("PREFER_SEQ_PUTS",
3431a6962d72SJoe Perches				     "Prefer seq_puts to seq_printf\n" . $herecurr);
3432a6962d72SJoe Perches			}
3433a6962d72SJoe Perches		}
3434a6962d72SJoe Perches
3435554e165cSAndy Whitcroft# Check for misused memsets
3436d1fe9c09SJoe Perches		if ($^V && $^V ge 5.10.0 &&
3437d1fe9c09SJoe Perches		    defined $stat &&
3438d7c76ba7SJoe Perches		    $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/s) {
3439554e165cSAndy Whitcroft
3440d7c76ba7SJoe Perches			my $ms_addr = $2;
3441d1fe9c09SJoe Perches			my $ms_val = $7;
3442d1fe9c09SJoe Perches			my $ms_size = $12;
3443d7c76ba7SJoe Perches
3444554e165cSAndy Whitcroft			if ($ms_size =~ /^(0x|)0$/i) {
3445554e165cSAndy Whitcroft				ERROR("MEMSET",
3446d7c76ba7SJoe Perches				      "memset to 0's uses 0 as the 2nd argument, not the 3rd\n" . "$here\n$stat\n");
3447554e165cSAndy Whitcroft			} elsif ($ms_size =~ /^(0x|)1$/i) {
3448554e165cSAndy Whitcroft				WARN("MEMSET",
3449d7c76ba7SJoe Perches				     "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
3450d7c76ba7SJoe Perches			}
3451d7c76ba7SJoe Perches		}
3452d7c76ba7SJoe Perches
3453d7c76ba7SJoe Perches# typecasts on min/max could be min_t/max_t
3454d1fe9c09SJoe Perches		if ($^V && $^V ge 5.10.0 &&
3455d1fe9c09SJoe Perches		    defined $stat &&
3456d7c76ba7SJoe Perches		    $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
3457d1fe9c09SJoe Perches			if (defined $2 || defined $7) {
3458d7c76ba7SJoe Perches				my $call = $1;
3459d7c76ba7SJoe Perches				my $cast1 = deparenthesize($2);
3460d7c76ba7SJoe Perches				my $arg1 = $3;
3461d1fe9c09SJoe Perches				my $cast2 = deparenthesize($7);
3462d1fe9c09SJoe Perches				my $arg2 = $8;
3463d7c76ba7SJoe Perches				my $cast;
3464d7c76ba7SJoe Perches
3465d1fe9c09SJoe Perches				if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
3466d7c76ba7SJoe Perches					$cast = "$cast1 or $cast2";
3467d7c76ba7SJoe Perches				} elsif ($cast1 ne "") {
3468d7c76ba7SJoe Perches					$cast = $cast1;
3469d7c76ba7SJoe Perches				} else {
3470d7c76ba7SJoe Perches					$cast = $cast2;
3471d7c76ba7SJoe Perches				}
3472d7c76ba7SJoe Perches				WARN("MINMAX",
3473d7c76ba7SJoe Perches				     "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
3474554e165cSAndy Whitcroft			}
3475554e165cSAndy Whitcroft		}
3476554e165cSAndy Whitcroft
34774a273195SJoe Perches# check usleep_range arguments
34784a273195SJoe Perches		if ($^V && $^V ge 5.10.0 &&
34794a273195SJoe Perches		    defined $stat &&
34804a273195SJoe Perches		    $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
34814a273195SJoe Perches			my $min = $1;
34824a273195SJoe Perches			my $max = $7;
34834a273195SJoe Perches			if ($min eq $max) {
34844a273195SJoe Perches				WARN("USLEEP_RANGE",
34854a273195SJoe Perches				     "usleep_range should not use min == max args; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
34864a273195SJoe Perches			} elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
34874a273195SJoe Perches				 $min > $max) {
34884a273195SJoe Perches				WARN("USLEEP_RANGE",
34894a273195SJoe Perches				     "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
34904a273195SJoe Perches			}
34914a273195SJoe Perches		}
34924a273195SJoe Perches
3493de7d4f0eSAndy Whitcroft# check for new externs in .c files.
3494171ae1a4SAndy Whitcroft		if ($realfile =~ /\.c$/ && defined $stat &&
3495c45dcabdSAndy Whitcroft		    $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
3496171ae1a4SAndy Whitcroft		{
3497c45dcabdSAndy Whitcroft			my $function_name = $1;
3498c45dcabdSAndy Whitcroft			my $paren_space = $2;
3499171ae1a4SAndy Whitcroft
3500171ae1a4SAndy Whitcroft			my $s = $stat;
3501171ae1a4SAndy Whitcroft			if (defined $cond) {
3502171ae1a4SAndy Whitcroft				substr($s, 0, length($cond), '');
3503171ae1a4SAndy Whitcroft			}
3504c45dcabdSAndy Whitcroft			if ($s =~ /^\s*;/ &&
3505c45dcabdSAndy Whitcroft			    $function_name ne 'uninitialized_var')
3506c45dcabdSAndy Whitcroft			{
3507000d1cc1SJoe Perches				WARN("AVOID_EXTERNS",
3508000d1cc1SJoe Perches				     "externs should be avoided in .c files\n" .  $herecurr);
3509de7d4f0eSAndy Whitcroft			}
3510de7d4f0eSAndy Whitcroft
3511171ae1a4SAndy Whitcroft			if ($paren_space =~ /\n/) {
3512000d1cc1SJoe Perches				WARN("FUNCTION_ARGUMENTS",
3513000d1cc1SJoe Perches				     "arguments for function declarations should follow identifier\n" . $herecurr);
3514171ae1a4SAndy Whitcroft			}
35159c9ba34eSAndy Whitcroft
35169c9ba34eSAndy Whitcroft		} elsif ($realfile =~ /\.c$/ && defined $stat &&
35179c9ba34eSAndy Whitcroft		    $stat =~ /^.\s*extern\s+/)
35189c9ba34eSAndy Whitcroft		{
3519000d1cc1SJoe Perches			WARN("AVOID_EXTERNS",
3520000d1cc1SJoe Perches			     "externs should be avoided in .c files\n" .  $herecurr);
3521171ae1a4SAndy Whitcroft		}
3522171ae1a4SAndy Whitcroft
3523de7d4f0eSAndy Whitcroft# checks for new __setup's
3524de7d4f0eSAndy Whitcroft		if ($rawline =~ /\b__setup\("([^"]*)"/) {
3525de7d4f0eSAndy Whitcroft			my $name = $1;
3526de7d4f0eSAndy Whitcroft
3527de7d4f0eSAndy Whitcroft			if (!grep(/$name/, @setup_docs)) {
3528000d1cc1SJoe Perches				CHK("UNDOCUMENTED_SETUP",
3529000d1cc1SJoe Perches				    "__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
3530de7d4f0eSAndy Whitcroft			}
3531653d4876SAndy Whitcroft		}
35329c0ca6f9SAndy Whitcroft
35339c0ca6f9SAndy Whitcroft# check for pointless casting of kmalloc return
3534caf2a54fSJoe Perches		if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) {
3535000d1cc1SJoe Perches			WARN("UNNECESSARY_CASTS",
3536000d1cc1SJoe Perches			     "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
35379c0ca6f9SAndy Whitcroft		}
353813214adfSAndy Whitcroft
3539a640d25cSJoe Perches# alloc style
3540a640d25cSJoe Perches# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
3541a640d25cSJoe Perches		if ($^V && $^V ge 5.10.0 &&
3542a640d25cSJoe Perches		    $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*([kv][mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
3543a640d25cSJoe Perches			CHK("ALLOC_SIZEOF_STRUCT",
3544a640d25cSJoe Perches			    "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
3545a640d25cSJoe Perches		}
3546a640d25cSJoe Perches
3547972fdea2SJoe Perches# check for krealloc arg reuse
3548972fdea2SJoe Perches		if ($^V && $^V ge 5.10.0 &&
3549972fdea2SJoe Perches		    $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*\1\s*,/) {
3550972fdea2SJoe Perches			WARN("KREALLOC_ARG_REUSE",
3551972fdea2SJoe Perches			     "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
3552972fdea2SJoe Perches		}
3553972fdea2SJoe Perches
35545ce59ae0SJoe Perches# check for alloc argument mismatch
35555ce59ae0SJoe Perches		if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
35565ce59ae0SJoe Perches			WARN("ALLOC_ARRAY_ARGS",
35575ce59ae0SJoe Perches			     "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
35585ce59ae0SJoe Perches		}
35595ce59ae0SJoe Perches
3560caf2a54fSJoe Perches# check for multiple semicolons
3561caf2a54fSJoe Perches		if ($line =~ /;\s*;\s*$/) {
3562000d1cc1SJoe Perches			WARN("ONE_SEMICOLON",
3563000d1cc1SJoe Perches			     "Statements terminations use 1 semicolon\n" . $herecurr);
3564d1e2ad07SJoe Perches		}
3565d1e2ad07SJoe Perches
3566d1e2ad07SJoe Perches# check for switch/default statements without a break;
3567d1e2ad07SJoe Perches		if ($^V && $^V ge 5.10.0 &&
3568d1e2ad07SJoe Perches		    defined $stat &&
3569d1e2ad07SJoe Perches		    $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
3570d1e2ad07SJoe Perches			my $ctx = '';
3571d1e2ad07SJoe Perches			my $herectx = $here . "\n";
3572d1e2ad07SJoe Perches			my $cnt = statement_rawlines($stat);
3573d1e2ad07SJoe Perches			for (my $n = 0; $n < $cnt; $n++) {
3574d1e2ad07SJoe Perches				$herectx .= raw_line($linenr, $n) . "\n";
3575d1e2ad07SJoe Perches			}
3576d1e2ad07SJoe Perches			WARN("DEFAULT_NO_BREAK",
3577d1e2ad07SJoe Perches			     "switch default: should use break\n" . $herectx);
3578caf2a54fSJoe Perches		}
3579caf2a54fSJoe Perches
358013214adfSAndy Whitcroft# check for gcc specific __FUNCTION__
358113214adfSAndy Whitcroft		if ($line =~ /__FUNCTION__/) {
3582000d1cc1SJoe Perches			WARN("USE_FUNC",
3583000d1cc1SJoe Perches			     "__func__ should be used instead of gcc specific __FUNCTION__\n"  . $herecurr);
358413214adfSAndy Whitcroft		}
3585773647a0SAndy Whitcroft
35862c92488aSJoe Perches# check for use of yield()
35872c92488aSJoe Perches		if ($line =~ /\byield\s*\(\s*\)/) {
35882c92488aSJoe Perches			WARN("YIELD",
35892c92488aSJoe Perches			     "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n"  . $herecurr);
35902c92488aSJoe Perches		}
35912c92488aSJoe Perches
3592179f8f40SJoe Perches# check for comparisons against true and false
3593179f8f40SJoe Perches		if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
3594179f8f40SJoe Perches			my $lead = $1;
3595179f8f40SJoe Perches			my $arg = $2;
3596179f8f40SJoe Perches			my $test = $3;
3597179f8f40SJoe Perches			my $otype = $4;
3598179f8f40SJoe Perches			my $trail = $5;
3599179f8f40SJoe Perches			my $op = "!";
3600179f8f40SJoe Perches
3601179f8f40SJoe Perches			($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
3602179f8f40SJoe Perches
3603179f8f40SJoe Perches			my $type = lc($otype);
3604179f8f40SJoe Perches			if ($type =~ /^(?:true|false)$/) {
3605179f8f40SJoe Perches				if (("$test" eq "==" && "$type" eq "true") ||
3606179f8f40SJoe Perches				    ("$test" eq "!=" && "$type" eq "false")) {
3607179f8f40SJoe Perches					$op = "";
3608179f8f40SJoe Perches				}
3609179f8f40SJoe Perches
3610179f8f40SJoe Perches				CHK("BOOL_COMPARISON",
3611179f8f40SJoe Perches				    "Using comparison to $otype is error prone\n" . $herecurr);
3612179f8f40SJoe Perches
3613179f8f40SJoe Perches## maybe suggesting a correct construct would better
3614179f8f40SJoe Perches##				    "Using comparison to $otype is error prone.  Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
3615179f8f40SJoe Perches
3616179f8f40SJoe Perches			}
3617179f8f40SJoe Perches		}
3618179f8f40SJoe Perches
36194882720bSThomas Gleixner# check for semaphores initialized locked
36204882720bSThomas Gleixner		if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
3621000d1cc1SJoe Perches			WARN("CONSIDER_COMPLETION",
3622000d1cc1SJoe Perches			     "consider using a completion\n" . $herecurr);
3623773647a0SAndy Whitcroft		}
36246712d858SJoe Perches
362567d0a075SJoe Perches# recommend kstrto* over simple_strto* and strict_strto*
362667d0a075SJoe Perches		if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
3627000d1cc1SJoe Perches			WARN("CONSIDER_KSTRTO",
362867d0a075SJoe Perches			     "$1 is obsolete, use k$3 instead\n" . $herecurr);
3629773647a0SAndy Whitcroft		}
36306712d858SJoe Perches
3631f3db6639SMichael Ellerman# check for __initcall(), use device_initcall() explicitly please
3632f3db6639SMichael Ellerman		if ($line =~ /^.\s*__initcall\s*\(/) {
3633000d1cc1SJoe Perches			WARN("USE_DEVICE_INITCALL",
3634000d1cc1SJoe Perches			     "please use device_initcall() instead of __initcall()\n" . $herecurr);
3635f3db6639SMichael Ellerman		}
36366712d858SJoe Perches
363779404849SEmese Revfy# check for various ops structs, ensure they are const.
363879404849SEmese Revfy		my $struct_ops = qr{acpi_dock_ops|
363979404849SEmese Revfy				address_space_operations|
364079404849SEmese Revfy				backlight_ops|
364179404849SEmese Revfy				block_device_operations|
364279404849SEmese Revfy				dentry_operations|
364379404849SEmese Revfy				dev_pm_ops|
364479404849SEmese Revfy				dma_map_ops|
364579404849SEmese Revfy				extent_io_ops|
364679404849SEmese Revfy				file_lock_operations|
364779404849SEmese Revfy				file_operations|
364879404849SEmese Revfy				hv_ops|
364979404849SEmese Revfy				ide_dma_ops|
365079404849SEmese Revfy				intel_dvo_dev_ops|
365179404849SEmese Revfy				item_operations|
365279404849SEmese Revfy				iwl_ops|
365379404849SEmese Revfy				kgdb_arch|
365479404849SEmese Revfy				kgdb_io|
365579404849SEmese Revfy				kset_uevent_ops|
365679404849SEmese Revfy				lock_manager_operations|
365779404849SEmese Revfy				microcode_ops|
365879404849SEmese Revfy				mtrr_ops|
365979404849SEmese Revfy				neigh_ops|
366079404849SEmese Revfy				nlmsvc_binding|
366179404849SEmese Revfy				pci_raw_ops|
366279404849SEmese Revfy				pipe_buf_operations|
366379404849SEmese Revfy				platform_hibernation_ops|
366479404849SEmese Revfy				platform_suspend_ops|
366579404849SEmese Revfy				proto_ops|
366679404849SEmese Revfy				rpc_pipe_ops|
366779404849SEmese Revfy				seq_operations|
366879404849SEmese Revfy				snd_ac97_build_ops|
366979404849SEmese Revfy				soc_pcmcia_socket_ops|
367079404849SEmese Revfy				stacktrace_ops|
367179404849SEmese Revfy				sysfs_ops|
367279404849SEmese Revfy				tty_operations|
367379404849SEmese Revfy				usb_mon_operations|
367479404849SEmese Revfy				wd_ops}x;
36756903ffb2SAndy Whitcroft		if ($line !~ /\bconst\b/ &&
367679404849SEmese Revfy		    $line =~ /\bstruct\s+($struct_ops)\b/) {
3677000d1cc1SJoe Perches			WARN("CONST_STRUCT",
3678000d1cc1SJoe Perches			     "struct $1 should normally be const\n" .
36796903ffb2SAndy Whitcroft				$herecurr);
36802b6db5cbSAndy Whitcroft		}
3681773647a0SAndy Whitcroft
3682773647a0SAndy Whitcroft# use of NR_CPUS is usually wrong
3683773647a0SAndy Whitcroft# ignore definitions of NR_CPUS and usage to define arrays as likely right
3684773647a0SAndy Whitcroft		if ($line =~ /\bNR_CPUS\b/ &&
3685c45dcabdSAndy Whitcroft		    $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
3686c45dcabdSAndy Whitcroft		    $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
3687171ae1a4SAndy Whitcroft		    $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
3688171ae1a4SAndy Whitcroft		    $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
3689171ae1a4SAndy Whitcroft		    $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
3690773647a0SAndy Whitcroft		{
3691000d1cc1SJoe Perches			WARN("NR_CPUS",
3692000d1cc1SJoe Perches			     "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
3693773647a0SAndy Whitcroft		}
36949c9ba34eSAndy Whitcroft
36959c9ba34eSAndy Whitcroft# check for %L{u,d,i} in strings
36969c9ba34eSAndy Whitcroft		my $string;
36979c9ba34eSAndy Whitcroft		while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
36989c9ba34eSAndy Whitcroft			$string = substr($rawline, $-[1], $+[1] - $-[1]);
36992a1bc5d5SAndy Whitcroft			$string =~ s/%%/__/g;
37009c9ba34eSAndy Whitcroft			if ($string =~ /(?<!%)%L[udi]/) {
3701000d1cc1SJoe Perches				WARN("PRINTF_L",
3702000d1cc1SJoe Perches				     "\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
37039c9ba34eSAndy Whitcroft				last;
37049c9ba34eSAndy Whitcroft			}
37059c9ba34eSAndy Whitcroft		}
3706691d77b6SAndy Whitcroft
3707691d77b6SAndy Whitcroft# whine mightly about in_atomic
3708691d77b6SAndy Whitcroft		if ($line =~ /\bin_atomic\s*\(/) {
3709691d77b6SAndy Whitcroft			if ($realfile =~ m@^drivers/@) {
3710000d1cc1SJoe Perches				ERROR("IN_ATOMIC",
3711000d1cc1SJoe Perches				      "do not use in_atomic in drivers\n" . $herecurr);
3712f4a87736SAndy Whitcroft			} elsif ($realfile !~ m@^kernel/@) {
3713000d1cc1SJoe Perches				WARN("IN_ATOMIC",
3714000d1cc1SJoe Perches				     "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
3715691d77b6SAndy Whitcroft			}
3716691d77b6SAndy Whitcroft		}
37171704f47bSPeter Zijlstra
37181704f47bSPeter Zijlstra# check for lockdep_set_novalidate_class
37191704f47bSPeter Zijlstra		if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
37201704f47bSPeter Zijlstra		    $line =~ /__lockdep_no_validate__\s*\)/ ) {
37211704f47bSPeter Zijlstra			if ($realfile !~ m@^kernel/lockdep@ &&
37221704f47bSPeter Zijlstra			    $realfile !~ m@^include/linux/lockdep@ &&
37231704f47bSPeter Zijlstra			    $realfile !~ m@^drivers/base/core@) {
3724000d1cc1SJoe Perches				ERROR("LOCKDEP",
3725000d1cc1SJoe Perches				      "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
37261704f47bSPeter Zijlstra			}
37271704f47bSPeter Zijlstra		}
372888f8831cSDave Jones
372988f8831cSDave Jones		if ($line =~ /debugfs_create_file.*S_IWUGO/ ||
373088f8831cSDave Jones		    $line =~ /DEVICE_ATTR.*S_IWUGO/ ) {
3731000d1cc1SJoe Perches			WARN("EXPORTED_WORLD_WRITABLE",
3732000d1cc1SJoe Perches			     "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
373388f8831cSDave Jones		}
373413214adfSAndy Whitcroft	}
373513214adfSAndy Whitcroft
373613214adfSAndy Whitcroft	# If we have no input at all, then there is nothing to report on
373713214adfSAndy Whitcroft	# so just keep quiet.
373813214adfSAndy Whitcroft	if ($#rawlines == -1) {
373913214adfSAndy Whitcroft		exit(0);
37400a920b5bSAndy Whitcroft	}
37410a920b5bSAndy Whitcroft
37428905a67cSAndy Whitcroft	# In mailback mode only produce a report in the negative, for
37438905a67cSAndy Whitcroft	# things that appear to be patches.
37448905a67cSAndy Whitcroft	if ($mailback && ($clean == 1 || !$is_patch)) {
37458905a67cSAndy Whitcroft		exit(0);
37468905a67cSAndy Whitcroft	}
37478905a67cSAndy Whitcroft
37488905a67cSAndy Whitcroft	# This is not a patch, and we are are in 'no-patch' mode so
37498905a67cSAndy Whitcroft	# just keep quiet.
37508905a67cSAndy Whitcroft	if (!$chk_patch && !$is_patch) {
37518905a67cSAndy Whitcroft		exit(0);
37528905a67cSAndy Whitcroft	}
37538905a67cSAndy Whitcroft
37548905a67cSAndy Whitcroft	if (!$is_patch) {
3755000d1cc1SJoe Perches		ERROR("NOT_UNIFIED_DIFF",
3756000d1cc1SJoe Perches		      "Does not appear to be a unified-diff format patch\n");
37570a920b5bSAndy Whitcroft	}
37580a920b5bSAndy Whitcroft	if ($is_patch && $chk_signoff && $signoff == 0) {
3759000d1cc1SJoe Perches		ERROR("MISSING_SIGN_OFF",
3760000d1cc1SJoe Perches		      "Missing Signed-off-by: line(s)\n");
37610a920b5bSAndy Whitcroft	}
37620a920b5bSAndy Whitcroft
3763f0a594c1SAndy Whitcroft	print report_dump();
376413214adfSAndy Whitcroft	if ($summary && !($clean == 1 && $quiet == 1)) {
376513214adfSAndy Whitcroft		print "$filename " if ($summary_file);
37666c72ffaaSAndy Whitcroft		print "total: $cnt_error errors, $cnt_warn warnings, " .
37676c72ffaaSAndy Whitcroft			(($check)? "$cnt_chk checks, " : "") .
37686c72ffaaSAndy Whitcroft			"$cnt_lines lines checked\n";
37698905a67cSAndy Whitcroft		print "\n" if ($quiet == 0);
37706c72ffaaSAndy Whitcroft	}
37718905a67cSAndy Whitcroft
3772d2c0a235SAndy Whitcroft	if ($quiet == 0) {
3773d1fe9c09SJoe Perches
3774d1fe9c09SJoe Perches		if ($^V lt 5.10.0) {
3775d1fe9c09SJoe Perches			print("NOTE: perl $^V is not modern enough to detect all possible issues.\n");
3776d1fe9c09SJoe Perches			print("An upgrade to at least perl v5.10.0 is suggested.\n\n");
3777d1fe9c09SJoe Perches		}
3778d1fe9c09SJoe Perches
3779d2c0a235SAndy Whitcroft		# If there were whitespace errors which cleanpatch can fix
3780d2c0a235SAndy Whitcroft		# then suggest that.
3781d2c0a235SAndy Whitcroft		if ($rpt_cleaners) {
3782d2c0a235SAndy Whitcroft			print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n";
3783d2c0a235SAndy Whitcroft			print "      scripts/cleanfile\n\n";
3784b0781216SMike Frysinger			$rpt_cleaners = 0;
3785d2c0a235SAndy Whitcroft		}
3786d2c0a235SAndy Whitcroft	}
3787d2c0a235SAndy Whitcroft
378811232688SArtem Bityutskiy	if ($quiet == 0 && keys %ignore_type) {
3789000d1cc1SJoe Perches	    print "NOTE: Ignored message types:";
3790000d1cc1SJoe Perches	    foreach my $ignore (sort keys %ignore_type) {
3791000d1cc1SJoe Perches		print " $ignore";
3792000d1cc1SJoe Perches	    }
379311232688SArtem Bityutskiy	    print "\n\n";
3794000d1cc1SJoe Perches	}
3795000d1cc1SJoe Perches
37960a920b5bSAndy Whitcroft	if ($clean == 1 && $quiet == 0) {
3797c2fdda0dSAndy Whitcroft		print "$vname has no obvious style problems and is ready for submission.\n"
37980a920b5bSAndy Whitcroft	}
37990a920b5bSAndy Whitcroft	if ($clean == 0 && $quiet == 0) {
3800000d1cc1SJoe Perches		print << "EOM";
3801000d1cc1SJoe Perches$vname has style problems, please review.
3802000d1cc1SJoe Perches
3803000d1cc1SJoe PerchesIf any of these errors are false positives, please report
3804000d1cc1SJoe Perchesthem to the maintainer, see CHECKPATCH in MAINTAINERS.
3805000d1cc1SJoe PerchesEOM
38060a920b5bSAndy Whitcroft	}
380713214adfSAndy Whitcroft
38080a920b5bSAndy Whitcroft	return $clean;
38090a920b5bSAndy Whitcroft}
3810