xref: /linux-6.15/scripts/checkpatch.pl (revision be79794b)
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
233326b1ffcSJoe Perchesour $Float_hex	= qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?};
234326b1ffcSJoe Perchesour $Float_dec	= qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?};
235326b1ffcSJoe Perchesour $Float_int	= qr{(?i)[0-9]+e-?[0-9]+[fl]?};
23674349bccSJoe Perchesour $Float	= qr{$Float_hex|$Float_dec|$Float_int};
237326b1ffcSJoe Perchesour $Constant	= qr{$Float|(?i)(?:0x[0-9a-f]+|[0-9]+)[ul]*};
238326b1ffcSJoe Perchesour $Assignment	= qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=};
23986f9d059SAndy Whitcroftour $Compare    = qr{<=|>=|==|!=|<|>};
2406c72ffaaSAndy Whitcroftour $Operators	= qr{
2416c72ffaaSAndy Whitcroft			<=|>=|==|!=|
2426c72ffaaSAndy Whitcroft			=>|->|<<|>>|<|>|!|~|
243c2fdda0dSAndy Whitcroft			&&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%
2446c72ffaaSAndy Whitcroft		  }x;
2456c72ffaaSAndy Whitcroft
2468905a67cSAndy Whitcroftour $NonptrType;
2478905a67cSAndy Whitcroftour $Type;
2488905a67cSAndy Whitcroftour $Declare;
2498905a67cSAndy Whitcroft
25015662b3eSJoe Perchesour $NON_ASCII_UTF8	= qr{
25115662b3eSJoe Perches	[\xC2-\xDF][\x80-\xBF]               # non-overlong 2-byte
252171ae1a4SAndy Whitcroft	|  \xE0[\xA0-\xBF][\x80-\xBF]        # excluding overlongs
253171ae1a4SAndy Whitcroft	| [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}  # straight 3-byte
254171ae1a4SAndy Whitcroft	|  \xED[\x80-\x9F][\x80-\xBF]        # excluding surrogates
255171ae1a4SAndy Whitcroft	|  \xF0[\x90-\xBF][\x80-\xBF]{2}     # planes 1-3
256171ae1a4SAndy Whitcroft	| [\xF1-\xF3][\x80-\xBF]{3}          # planes 4-15
257171ae1a4SAndy Whitcroft	|  \xF4[\x80-\x8F][\x80-\xBF]{2}     # plane 16
258171ae1a4SAndy Whitcroft}x;
259171ae1a4SAndy Whitcroft
26015662b3eSJoe Perchesour $UTF8	= qr{
26115662b3eSJoe Perches	[\x09\x0A\x0D\x20-\x7E]              # ASCII
26215662b3eSJoe Perches	| $NON_ASCII_UTF8
26315662b3eSJoe Perches}x;
26415662b3eSJoe Perches
2658ed22cadSAndy Whitcroftour $typeTypedefs = qr{(?x:
266fb9e9096SAndy Whitcroft	(?:__)?(?:u|s|be|le)(?:8|16|32|64)|
2678ed22cadSAndy Whitcroft	atomic_t
2688ed22cadSAndy Whitcroft)};
2698ed22cadSAndy Whitcroft
270691e669bSJoe Perchesour $logFunctions = qr{(?x:
2716e60c02eSJoe Perches	printk(?:_ratelimited|_once|)|
2726e60c02eSJoe Perches	[a-z0-9]+_(?:printk|emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)(?:_ratelimited|_once|)|
2736e60c02eSJoe Perches	WARN(?:_RATELIMIT|_ONCE|)|
274b0531722SJoe Perches	panic|
275b0531722SJoe Perches	MODULE_[A-Z_]+
276691e669bSJoe Perches)};
277691e669bSJoe Perches
27820112475SJoe Perchesour $signature_tags = qr{(?xi:
27920112475SJoe Perches	Signed-off-by:|
28020112475SJoe Perches	Acked-by:|
28120112475SJoe Perches	Tested-by:|
28220112475SJoe Perches	Reviewed-by:|
28320112475SJoe Perches	Reported-by:|
2848543ae12SMugunthan V N	Suggested-by:|
28520112475SJoe Perches	To:|
28620112475SJoe Perches	Cc:
28720112475SJoe Perches)};
28820112475SJoe Perches
2898905a67cSAndy Whitcroftour @typeList = (
2908905a67cSAndy Whitcroft	qr{void},
291c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?char},
292c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?short},
293c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?int},
294c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?long},
295c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?long\s+int},
296c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?long\s+long},
297c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?long\s+long\s+int},
2988905a67cSAndy Whitcroft	qr{unsigned},
2998905a67cSAndy Whitcroft	qr{float},
3008905a67cSAndy Whitcroft	qr{double},
3018905a67cSAndy Whitcroft	qr{bool},
3028905a67cSAndy Whitcroft	qr{struct\s+$Ident},
3038905a67cSAndy Whitcroft	qr{union\s+$Ident},
3048905a67cSAndy Whitcroft	qr{enum\s+$Ident},
3058905a67cSAndy Whitcroft	qr{${Ident}_t},
3068905a67cSAndy Whitcroft	qr{${Ident}_handler},
3078905a67cSAndy Whitcroft	qr{${Ident}_handler_fn},
3088905a67cSAndy Whitcroft);
309c45dcabdSAndy Whitcroftour @modifierList = (
310c45dcabdSAndy Whitcroft	qr{fastcall},
311c45dcabdSAndy Whitcroft);
3128905a67cSAndy Whitcroft
3137840a94cSWolfram Sangour $allowed_asm_includes = qr{(?x:
3147840a94cSWolfram Sang	irq|
3157840a94cSWolfram Sang	memory
3167840a94cSWolfram Sang)};
3177840a94cSWolfram Sang# memory.h: ARM has a custom one
3187840a94cSWolfram Sang
3198905a67cSAndy Whitcroftsub build_types {
320d2172eb5SAndy Whitcroft	my $mods = "(?x:  \n" . join("|\n  ", @modifierList) . "\n)";
321d2172eb5SAndy Whitcroft	my $all = "(?x:  \n" . join("|\n  ", @typeList) . "\n)";
322c8cb2ca3SAndy Whitcroft	$Modifier	= qr{(?:$Attribute|$Sparse|$mods)};
3238905a67cSAndy Whitcroft	$NonptrType	= qr{
324d2172eb5SAndy Whitcroft			(?:$Modifier\s+|const\s+)*
325cf655043SAndy Whitcroft			(?:
3266b48db24SAndy Whitcroft				(?:typeof|__typeof__)\s*\([^\)]*\)|
3278ed22cadSAndy Whitcroft				(?:$typeTypedefs\b)|
328c45dcabdSAndy Whitcroft				(?:${all}\b)
329cf655043SAndy Whitcroft			)
330c8cb2ca3SAndy Whitcroft			(?:\s+$Modifier|\s+const)*
3318905a67cSAndy Whitcroft		  }x;
3328905a67cSAndy Whitcroft	$Type	= qr{
333c45dcabdSAndy Whitcroft			$NonptrType
334b337d8b8SAndy Whitcroft			(?:(?:\s|\*|\[\])+\s*const|(?:\s|\*|\[\])+|(?:\s*\[\s*\])+)?
335c8cb2ca3SAndy Whitcroft			(?:\s+$Inline|\s+$Modifier)*
3368905a67cSAndy Whitcroft		  }x;
3378905a67cSAndy Whitcroft	$Declare	= qr{(?:$Storage\s+)?$Type};
3388905a67cSAndy Whitcroft}
3398905a67cSAndy Whitcroftbuild_types();
3406c72ffaaSAndy Whitcroft
3417d2367afSJoe Perches
3427d2367afSJoe Perchesour $Typecast	= qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
343d1fe9c09SJoe Perches
344d1fe9c09SJoe Perches# Using $balanced_parens, $LvalOrFunc, or $FuncArg
345d1fe9c09SJoe Perches# requires at least perl version v5.10.0
346d1fe9c09SJoe Perches# Any use must be runtime checked with $^V
347d1fe9c09SJoe Perches
348d1fe9c09SJoe Perchesour $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
349d1fe9c09SJoe Perchesour $LvalOrFunc	= qr{($Lval)\s*($balanced_parens{0,1})\s*};
350d7c76ba7SJoe Perchesour $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant)};
3517d2367afSJoe Perches
3527d2367afSJoe Perchessub deparenthesize {
3537d2367afSJoe Perches	my ($string) = @_;
3547d2367afSJoe Perches	return "" if (!defined($string));
3557d2367afSJoe Perches	$string =~ s@^\s*\(\s*@@g;
3567d2367afSJoe Perches	$string =~ s@\s*\)\s*$@@g;
3577d2367afSJoe Perches	$string =~ s@\s+@ @g;
3587d2367afSJoe Perches	return $string;
3597d2367afSJoe Perches}
3607d2367afSJoe Perches
3616c72ffaaSAndy Whitcroft$chk_signoff = 0 if ($file);
3620a920b5bSAndy Whitcroft
36300df344fSAndy Whitcroftmy @rawlines = ();
364c2fdda0dSAndy Whitcroftmy @lines = ();
365c2fdda0dSAndy Whitcroftmy $vname;
3666c72ffaaSAndy Whitcroftfor my $filename (@ARGV) {
36721caa13cSAndy Whitcroft	my $FILE;
3686c72ffaaSAndy Whitcroft	if ($file) {
36921caa13cSAndy Whitcroft		open($FILE, '-|', "diff -u /dev/null $filename") ||
3706c72ffaaSAndy Whitcroft			die "$P: $filename: diff failed - $!\n";
37121caa13cSAndy Whitcroft	} elsif ($filename eq '-') {
37221caa13cSAndy Whitcroft		open($FILE, '<&STDIN');
3736c72ffaaSAndy Whitcroft	} else {
37421caa13cSAndy Whitcroft		open($FILE, '<', "$filename") ||
3756c72ffaaSAndy Whitcroft			die "$P: $filename: open failed - $!\n";
3766c72ffaaSAndy Whitcroft	}
377c2fdda0dSAndy Whitcroft	if ($filename eq '-') {
378c2fdda0dSAndy Whitcroft		$vname = 'Your patch';
379c2fdda0dSAndy Whitcroft	} else {
380c2fdda0dSAndy Whitcroft		$vname = $filename;
381c2fdda0dSAndy Whitcroft	}
38221caa13cSAndy Whitcroft	while (<$FILE>) {
3830a920b5bSAndy Whitcroft		chomp;
38400df344fSAndy Whitcroft		push(@rawlines, $_);
3856c72ffaaSAndy Whitcroft	}
38621caa13cSAndy Whitcroft	close($FILE);
387c2fdda0dSAndy Whitcroft	if (!process($filename)) {
3880a920b5bSAndy Whitcroft		$exit = 1;
3890a920b5bSAndy Whitcroft	}
39000df344fSAndy Whitcroft	@rawlines = ();
39113214adfSAndy Whitcroft	@lines = ();
3920a920b5bSAndy Whitcroft}
3930a920b5bSAndy Whitcroft
3940a920b5bSAndy Whitcroftexit($exit);
3950a920b5bSAndy Whitcroft
3960a920b5bSAndy Whitcroftsub top_of_kernel_tree {
3976c72ffaaSAndy Whitcroft	my ($root) = @_;
3986c72ffaaSAndy Whitcroft
3996c72ffaaSAndy Whitcroft	my @tree_check = (
4006c72ffaaSAndy Whitcroft		"COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
4016c72ffaaSAndy Whitcroft		"README", "Documentation", "arch", "include", "drivers",
4026c72ffaaSAndy Whitcroft		"fs", "init", "ipc", "kernel", "lib", "scripts",
4036c72ffaaSAndy Whitcroft	);
4046c72ffaaSAndy Whitcroft
4056c72ffaaSAndy Whitcroft	foreach my $check (@tree_check) {
4066c72ffaaSAndy Whitcroft		if (! -e $root . '/' . $check) {
4070a920b5bSAndy Whitcroft			return 0;
4080a920b5bSAndy Whitcroft		}
4096c72ffaaSAndy Whitcroft	}
4106c72ffaaSAndy Whitcroft	return 1;
4116c72ffaaSAndy Whitcroft}
4120a920b5bSAndy Whitcroft
41320112475SJoe Perchessub parse_email {
41420112475SJoe Perches	my ($formatted_email) = @_;
41520112475SJoe Perches
41620112475SJoe Perches	my $name = "";
41720112475SJoe Perches	my $address = "";
41820112475SJoe Perches	my $comment = "";
41920112475SJoe Perches
42020112475SJoe Perches	if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
42120112475SJoe Perches		$name = $1;
42220112475SJoe Perches		$address = $2;
42320112475SJoe Perches		$comment = $3 if defined $3;
42420112475SJoe Perches	} elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
42520112475SJoe Perches		$address = $1;
42620112475SJoe Perches		$comment = $2 if defined $2;
42720112475SJoe Perches	} elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
42820112475SJoe Perches		$address = $1;
42920112475SJoe Perches		$comment = $2 if defined $2;
43020112475SJoe Perches		$formatted_email =~ s/$address.*$//;
43120112475SJoe Perches		$name = $formatted_email;
43220112475SJoe Perches		$name =~ s/^\s+|\s+$//g;
43320112475SJoe Perches		$name =~ s/^\"|\"$//g;
43420112475SJoe Perches		# If there's a name left after stripping spaces and
43520112475SJoe Perches		# leading quotes, and the address doesn't have both
43620112475SJoe Perches		# leading and trailing angle brackets, the address
43720112475SJoe Perches		# is invalid. ie:
43820112475SJoe Perches		#   "joe smith [email protected]" bad
43920112475SJoe Perches		#   "joe smith <[email protected]" bad
44020112475SJoe Perches		if ($name ne "" && $address !~ /^<[^>]+>$/) {
44120112475SJoe Perches			$name = "";
44220112475SJoe Perches			$address = "";
44320112475SJoe Perches			$comment = "";
44420112475SJoe Perches		}
44520112475SJoe Perches	}
44620112475SJoe Perches
44720112475SJoe Perches	$name =~ s/^\s+|\s+$//g;
44820112475SJoe Perches	$name =~ s/^\"|\"$//g;
44920112475SJoe Perches	$address =~ s/^\s+|\s+$//g;
45020112475SJoe Perches	$address =~ s/^\<|\>$//g;
45120112475SJoe Perches
45220112475SJoe Perches	if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
45320112475SJoe Perches		$name =~ s/(?<!\\)"/\\"/g; ##escape quotes
45420112475SJoe Perches		$name = "\"$name\"";
45520112475SJoe Perches	}
45620112475SJoe Perches
45720112475SJoe Perches	return ($name, $address, $comment);
45820112475SJoe Perches}
45920112475SJoe Perches
46020112475SJoe Perchessub format_email {
46120112475SJoe Perches	my ($name, $address) = @_;
46220112475SJoe Perches
46320112475SJoe Perches	my $formatted_email;
46420112475SJoe Perches
46520112475SJoe Perches	$name =~ s/^\s+|\s+$//g;
46620112475SJoe Perches	$name =~ s/^\"|\"$//g;
46720112475SJoe Perches	$address =~ s/^\s+|\s+$//g;
46820112475SJoe Perches
46920112475SJoe Perches	if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
47020112475SJoe Perches		$name =~ s/(?<!\\)"/\\"/g; ##escape quotes
47120112475SJoe Perches		$name = "\"$name\"";
47220112475SJoe Perches	}
47320112475SJoe Perches
47420112475SJoe Perches	if ("$name" eq "") {
47520112475SJoe Perches		$formatted_email = "$address";
47620112475SJoe Perches	} else {
47720112475SJoe Perches		$formatted_email = "$name <$address>";
47820112475SJoe Perches	}
47920112475SJoe Perches
48020112475SJoe Perches	return $formatted_email;
48120112475SJoe Perches}
48220112475SJoe Perches
483000d1cc1SJoe Perchessub which_conf {
484000d1cc1SJoe Perches	my ($conf) = @_;
485000d1cc1SJoe Perches
486000d1cc1SJoe Perches	foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
487000d1cc1SJoe Perches		if (-e "$path/$conf") {
488000d1cc1SJoe Perches			return "$path/$conf";
489000d1cc1SJoe Perches		}
490000d1cc1SJoe Perches	}
491000d1cc1SJoe Perches
492000d1cc1SJoe Perches	return "";
493000d1cc1SJoe Perches}
494000d1cc1SJoe Perches
4950a920b5bSAndy Whitcroftsub expand_tabs {
4960a920b5bSAndy Whitcroft	my ($str) = @_;
4970a920b5bSAndy Whitcroft
4980a920b5bSAndy Whitcroft	my $res = '';
4990a920b5bSAndy Whitcroft	my $n = 0;
5000a920b5bSAndy Whitcroft	for my $c (split(//, $str)) {
5010a920b5bSAndy Whitcroft		if ($c eq "\t") {
5020a920b5bSAndy Whitcroft			$res .= ' ';
5030a920b5bSAndy Whitcroft			$n++;
5040a920b5bSAndy Whitcroft			for (; ($n % 8) != 0; $n++) {
5050a920b5bSAndy Whitcroft				$res .= ' ';
5060a920b5bSAndy Whitcroft			}
5070a920b5bSAndy Whitcroft			next;
5080a920b5bSAndy Whitcroft		}
5090a920b5bSAndy Whitcroft		$res .= $c;
5100a920b5bSAndy Whitcroft		$n++;
5110a920b5bSAndy Whitcroft	}
5120a920b5bSAndy Whitcroft
5130a920b5bSAndy Whitcroft	return $res;
5140a920b5bSAndy Whitcroft}
5156c72ffaaSAndy Whitcroftsub copy_spacing {
516773647a0SAndy Whitcroft	(my $res = shift) =~ tr/\t/ /c;
5176c72ffaaSAndy Whitcroft	return $res;
5186c72ffaaSAndy Whitcroft}
5190a920b5bSAndy Whitcroft
5204a0df2efSAndy Whitcroftsub line_stats {
5214a0df2efSAndy Whitcroft	my ($line) = @_;
5224a0df2efSAndy Whitcroft
5234a0df2efSAndy Whitcroft	# Drop the diff line leader and expand tabs
5244a0df2efSAndy Whitcroft	$line =~ s/^.//;
5254a0df2efSAndy Whitcroft	$line = expand_tabs($line);
5264a0df2efSAndy Whitcroft
5274a0df2efSAndy Whitcroft	# Pick the indent from the front of the line.
5284a0df2efSAndy Whitcroft	my ($white) = ($line =~ /^(\s*)/);
5294a0df2efSAndy Whitcroft
5304a0df2efSAndy Whitcroft	return (length($line), length($white));
5314a0df2efSAndy Whitcroft}
5324a0df2efSAndy Whitcroft
533773647a0SAndy Whitcroftmy $sanitise_quote = '';
534773647a0SAndy Whitcroft
535773647a0SAndy Whitcroftsub sanitise_line_reset {
536773647a0SAndy Whitcroft	my ($in_comment) = @_;
537773647a0SAndy Whitcroft
538773647a0SAndy Whitcroft	if ($in_comment) {
539773647a0SAndy Whitcroft		$sanitise_quote = '*/';
540773647a0SAndy Whitcroft	} else {
541773647a0SAndy Whitcroft		$sanitise_quote = '';
542773647a0SAndy Whitcroft	}
543773647a0SAndy Whitcroft}
54400df344fSAndy Whitcroftsub sanitise_line {
54500df344fSAndy Whitcroft	my ($line) = @_;
54600df344fSAndy Whitcroft
54700df344fSAndy Whitcroft	my $res = '';
54800df344fSAndy Whitcroft	my $l = '';
54900df344fSAndy Whitcroft
550c2fdda0dSAndy Whitcroft	my $qlen = 0;
551773647a0SAndy Whitcroft	my $off = 0;
552773647a0SAndy Whitcroft	my $c;
55300df344fSAndy Whitcroft
554773647a0SAndy Whitcroft	# Always copy over the diff marker.
555773647a0SAndy Whitcroft	$res = substr($line, 0, 1);
556773647a0SAndy Whitcroft
557773647a0SAndy Whitcroft	for ($off = 1; $off < length($line); $off++) {
558773647a0SAndy Whitcroft		$c = substr($line, $off, 1);
559773647a0SAndy Whitcroft
560773647a0SAndy Whitcroft		# Comments we are wacking completly including the begin
561773647a0SAndy Whitcroft		# and end, all to $;.
562773647a0SAndy Whitcroft		if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
563773647a0SAndy Whitcroft			$sanitise_quote = '*/';
564773647a0SAndy Whitcroft
565773647a0SAndy Whitcroft			substr($res, $off, 2, "$;$;");
566773647a0SAndy Whitcroft			$off++;
56700df344fSAndy Whitcroft			next;
568773647a0SAndy Whitcroft		}
56981bc0e02SAndy Whitcroft		if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
570773647a0SAndy Whitcroft			$sanitise_quote = '';
571773647a0SAndy Whitcroft			substr($res, $off, 2, "$;$;");
572773647a0SAndy Whitcroft			$off++;
573773647a0SAndy Whitcroft			next;
574773647a0SAndy Whitcroft		}
575113f04a8SDaniel Walker		if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
576113f04a8SDaniel Walker			$sanitise_quote = '//';
577113f04a8SDaniel Walker
578113f04a8SDaniel Walker			substr($res, $off, 2, $sanitise_quote);
579113f04a8SDaniel Walker			$off++;
580113f04a8SDaniel Walker			next;
581113f04a8SDaniel Walker		}
582773647a0SAndy Whitcroft
583773647a0SAndy Whitcroft		# A \ in a string means ignore the next character.
584773647a0SAndy Whitcroft		if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
585773647a0SAndy Whitcroft		    $c eq "\\") {
586773647a0SAndy Whitcroft			substr($res, $off, 2, 'XX');
587773647a0SAndy Whitcroft			$off++;
588773647a0SAndy Whitcroft			next;
589773647a0SAndy Whitcroft		}
590773647a0SAndy Whitcroft		# Regular quotes.
591773647a0SAndy Whitcroft		if ($c eq "'" || $c eq '"') {
592773647a0SAndy Whitcroft			if ($sanitise_quote eq '') {
593773647a0SAndy Whitcroft				$sanitise_quote = $c;
594773647a0SAndy Whitcroft
595773647a0SAndy Whitcroft				substr($res, $off, 1, $c);
596773647a0SAndy Whitcroft				next;
597773647a0SAndy Whitcroft			} elsif ($sanitise_quote eq $c) {
598773647a0SAndy Whitcroft				$sanitise_quote = '';
59900df344fSAndy Whitcroft			}
60000df344fSAndy Whitcroft		}
601773647a0SAndy Whitcroft
602fae17daeSAndy Whitcroft		#print "c<$c> SQ<$sanitise_quote>\n";
603773647a0SAndy Whitcroft		if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
604773647a0SAndy Whitcroft			substr($res, $off, 1, $;);
605113f04a8SDaniel Walker		} elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
606113f04a8SDaniel Walker			substr($res, $off, 1, $;);
607773647a0SAndy Whitcroft		} elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
608773647a0SAndy Whitcroft			substr($res, $off, 1, 'X');
60900df344fSAndy Whitcroft		} else {
610773647a0SAndy Whitcroft			substr($res, $off, 1, $c);
61100df344fSAndy Whitcroft		}
612c2fdda0dSAndy Whitcroft	}
613c2fdda0dSAndy Whitcroft
614113f04a8SDaniel Walker	if ($sanitise_quote eq '//') {
615113f04a8SDaniel Walker		$sanitise_quote = '';
616113f04a8SDaniel Walker	}
617113f04a8SDaniel Walker
618c2fdda0dSAndy Whitcroft	# The pathname on a #include may be surrounded by '<' and '>'.
619c45dcabdSAndy Whitcroft	if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
620c2fdda0dSAndy Whitcroft		my $clean = 'X' x length($1);
621c2fdda0dSAndy Whitcroft		$res =~ s@\<.*\>@<$clean>@;
622c2fdda0dSAndy Whitcroft
623c2fdda0dSAndy Whitcroft	# The whole of a #error is a string.
624c45dcabdSAndy Whitcroft	} elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
625c2fdda0dSAndy Whitcroft		my $clean = 'X' x length($1);
626c45dcabdSAndy Whitcroft		$res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
627c2fdda0dSAndy Whitcroft	}
628c2fdda0dSAndy Whitcroft
62900df344fSAndy Whitcroft	return $res;
63000df344fSAndy Whitcroft}
63100df344fSAndy Whitcroft
632a6962d72SJoe Perchessub get_quoted_string {
633a6962d72SJoe Perches	my ($line, $rawline) = @_;
634a6962d72SJoe Perches
635a6962d72SJoe Perches	return "" if ($line !~ m/(\"[X]+\")/g);
636a6962d72SJoe Perches	return substr($rawline, $-[0], $+[0] - $-[0]);
637a6962d72SJoe Perches}
638a6962d72SJoe Perches
6398905a67cSAndy Whitcroftsub ctx_statement_block {
6408905a67cSAndy Whitcroft	my ($linenr, $remain, $off) = @_;
6418905a67cSAndy Whitcroft	my $line = $linenr - 1;
6428905a67cSAndy Whitcroft	my $blk = '';
6438905a67cSAndy Whitcroft	my $soff = $off;
6448905a67cSAndy Whitcroft	my $coff = $off - 1;
645773647a0SAndy Whitcroft	my $coff_set = 0;
6468905a67cSAndy Whitcroft
64713214adfSAndy Whitcroft	my $loff = 0;
64813214adfSAndy Whitcroft
6498905a67cSAndy Whitcroft	my $type = '';
6508905a67cSAndy Whitcroft	my $level = 0;
651a2750645SAndy Whitcroft	my @stack = ();
652cf655043SAndy Whitcroft	my $p;
6538905a67cSAndy Whitcroft	my $c;
6548905a67cSAndy Whitcroft	my $len = 0;
65513214adfSAndy Whitcroft
65613214adfSAndy Whitcroft	my $remainder;
6578905a67cSAndy Whitcroft	while (1) {
658a2750645SAndy Whitcroft		@stack = (['', 0]) if ($#stack == -1);
659a2750645SAndy Whitcroft
660773647a0SAndy Whitcroft		#warn "CSB: blk<$blk> remain<$remain>\n";
6618905a67cSAndy Whitcroft		# If we are about to drop off the end, pull in more
6628905a67cSAndy Whitcroft		# context.
6638905a67cSAndy Whitcroft		if ($off >= $len) {
6648905a67cSAndy Whitcroft			for (; $remain > 0; $line++) {
665dea33496SAndy Whitcroft				last if (!defined $lines[$line]);
666c2fdda0dSAndy Whitcroft				next if ($lines[$line] =~ /^-/);
6678905a67cSAndy Whitcroft				$remain--;
66813214adfSAndy Whitcroft				$loff = $len;
669c2fdda0dSAndy Whitcroft				$blk .= $lines[$line] . "\n";
6708905a67cSAndy Whitcroft				$len = length($blk);
6718905a67cSAndy Whitcroft				$line++;
6728905a67cSAndy Whitcroft				last;
6738905a67cSAndy Whitcroft			}
6748905a67cSAndy Whitcroft			# Bail if there is no further context.
6758905a67cSAndy Whitcroft			#warn "CSB: blk<$blk> off<$off> len<$len>\n";
67613214adfSAndy Whitcroft			if ($off >= $len) {
6778905a67cSAndy Whitcroft				last;
6788905a67cSAndy Whitcroft			}
679f74bd194SAndy Whitcroft			if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
680f74bd194SAndy Whitcroft				$level++;
681f74bd194SAndy Whitcroft				$type = '#';
682f74bd194SAndy Whitcroft			}
6838905a67cSAndy Whitcroft		}
684cf655043SAndy Whitcroft		$p = $c;
6858905a67cSAndy Whitcroft		$c = substr($blk, $off, 1);
68613214adfSAndy Whitcroft		$remainder = substr($blk, $off);
6878905a67cSAndy Whitcroft
688773647a0SAndy Whitcroft		#warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
6894635f4fbSAndy Whitcroft
6904635f4fbSAndy Whitcroft		# Handle nested #if/#else.
6914635f4fbSAndy Whitcroft		if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
6924635f4fbSAndy Whitcroft			push(@stack, [ $type, $level ]);
6934635f4fbSAndy Whitcroft		} elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
6944635f4fbSAndy Whitcroft			($type, $level) = @{$stack[$#stack - 1]};
6954635f4fbSAndy Whitcroft		} elsif ($remainder =~ /^#\s*endif\b/) {
6964635f4fbSAndy Whitcroft			($type, $level) = @{pop(@stack)};
6974635f4fbSAndy Whitcroft		}
6984635f4fbSAndy Whitcroft
6998905a67cSAndy Whitcroft		# Statement ends at the ';' or a close '}' at the
7008905a67cSAndy Whitcroft		# outermost level.
7018905a67cSAndy Whitcroft		if ($level == 0 && $c eq ';') {
7028905a67cSAndy Whitcroft			last;
7038905a67cSAndy Whitcroft		}
7048905a67cSAndy Whitcroft
70513214adfSAndy Whitcroft		# An else is really a conditional as long as its not else if
706773647a0SAndy Whitcroft		if ($level == 0 && $coff_set == 0 &&
707773647a0SAndy Whitcroft				(!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
708773647a0SAndy Whitcroft				$remainder =~ /^(else)(?:\s|{)/ &&
709773647a0SAndy Whitcroft				$remainder !~ /^else\s+if\b/) {
710773647a0SAndy Whitcroft			$coff = $off + length($1) - 1;
711773647a0SAndy Whitcroft			$coff_set = 1;
712773647a0SAndy Whitcroft			#warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
713773647a0SAndy Whitcroft			#warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
71413214adfSAndy Whitcroft		}
71513214adfSAndy Whitcroft
7168905a67cSAndy Whitcroft		if (($type eq '' || $type eq '(') && $c eq '(') {
7178905a67cSAndy Whitcroft			$level++;
7188905a67cSAndy Whitcroft			$type = '(';
7198905a67cSAndy Whitcroft		}
7208905a67cSAndy Whitcroft		if ($type eq '(' && $c eq ')') {
7218905a67cSAndy Whitcroft			$level--;
7228905a67cSAndy Whitcroft			$type = ($level != 0)? '(' : '';
7238905a67cSAndy Whitcroft
7248905a67cSAndy Whitcroft			if ($level == 0 && $coff < $soff) {
7258905a67cSAndy Whitcroft				$coff = $off;
726773647a0SAndy Whitcroft				$coff_set = 1;
727773647a0SAndy Whitcroft				#warn "CSB: mark coff<$coff>\n";
7288905a67cSAndy Whitcroft			}
7298905a67cSAndy Whitcroft		}
7308905a67cSAndy Whitcroft		if (($type eq '' || $type eq '{') && $c eq '{') {
7318905a67cSAndy Whitcroft			$level++;
7328905a67cSAndy Whitcroft			$type = '{';
7338905a67cSAndy Whitcroft		}
7348905a67cSAndy Whitcroft		if ($type eq '{' && $c eq '}') {
7358905a67cSAndy Whitcroft			$level--;
7368905a67cSAndy Whitcroft			$type = ($level != 0)? '{' : '';
7378905a67cSAndy Whitcroft
7388905a67cSAndy Whitcroft			if ($level == 0) {
739b998e001SPatrick Pannuto				if (substr($blk, $off + 1, 1) eq ';') {
740b998e001SPatrick Pannuto					$off++;
741b998e001SPatrick Pannuto				}
7428905a67cSAndy Whitcroft				last;
7438905a67cSAndy Whitcroft			}
7448905a67cSAndy Whitcroft		}
745f74bd194SAndy Whitcroft		# Preprocessor commands end at the newline unless escaped.
746f74bd194SAndy Whitcroft		if ($type eq '#' && $c eq "\n" && $p ne "\\") {
747f74bd194SAndy Whitcroft			$level--;
748f74bd194SAndy Whitcroft			$type = '';
749f74bd194SAndy Whitcroft			$off++;
750f74bd194SAndy Whitcroft			last;
751f74bd194SAndy Whitcroft		}
7528905a67cSAndy Whitcroft		$off++;
7538905a67cSAndy Whitcroft	}
754a3bb97a7SAndy Whitcroft	# We are truly at the end, so shuffle to the next line.
75513214adfSAndy Whitcroft	if ($off == $len) {
756a3bb97a7SAndy Whitcroft		$loff = $len + 1;
75713214adfSAndy Whitcroft		$line++;
75813214adfSAndy Whitcroft		$remain--;
75913214adfSAndy Whitcroft	}
7608905a67cSAndy Whitcroft
7618905a67cSAndy Whitcroft	my $statement = substr($blk, $soff, $off - $soff + 1);
7628905a67cSAndy Whitcroft	my $condition = substr($blk, $soff, $coff - $soff + 1);
7638905a67cSAndy Whitcroft
7648905a67cSAndy Whitcroft	#warn "STATEMENT<$statement>\n";
7658905a67cSAndy Whitcroft	#warn "CONDITION<$condition>\n";
7668905a67cSAndy Whitcroft
767773647a0SAndy Whitcroft	#print "coff<$coff> soff<$off> loff<$loff>\n";
76813214adfSAndy Whitcroft
76913214adfSAndy Whitcroft	return ($statement, $condition,
77013214adfSAndy Whitcroft			$line, $remain + 1, $off - $loff + 1, $level);
77113214adfSAndy Whitcroft}
77213214adfSAndy Whitcroft
773cf655043SAndy Whitcroftsub statement_lines {
774cf655043SAndy Whitcroft	my ($stmt) = @_;
775cf655043SAndy Whitcroft
776cf655043SAndy Whitcroft	# Strip the diff line prefixes and rip blank lines at start and end.
777cf655043SAndy Whitcroft	$stmt =~ s/(^|\n)./$1/g;
778cf655043SAndy Whitcroft	$stmt =~ s/^\s*//;
779cf655043SAndy Whitcroft	$stmt =~ s/\s*$//;
780cf655043SAndy Whitcroft
781cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
782cf655043SAndy Whitcroft
783cf655043SAndy Whitcroft	return $#stmt_lines + 2;
784cf655043SAndy Whitcroft}
785cf655043SAndy Whitcroft
786cf655043SAndy Whitcroftsub statement_rawlines {
787cf655043SAndy Whitcroft	my ($stmt) = @_;
788cf655043SAndy Whitcroft
789cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
790cf655043SAndy Whitcroft
791cf655043SAndy Whitcroft	return $#stmt_lines + 2;
792cf655043SAndy Whitcroft}
793cf655043SAndy Whitcroft
794cf655043SAndy Whitcroftsub statement_block_size {
795cf655043SAndy Whitcroft	my ($stmt) = @_;
796cf655043SAndy Whitcroft
797cf655043SAndy Whitcroft	$stmt =~ s/(^|\n)./$1/g;
798cf655043SAndy Whitcroft	$stmt =~ s/^\s*{//;
799cf655043SAndy Whitcroft	$stmt =~ s/}\s*$//;
800cf655043SAndy Whitcroft	$stmt =~ s/^\s*//;
801cf655043SAndy Whitcroft	$stmt =~ s/\s*$//;
802cf655043SAndy Whitcroft
803cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
804cf655043SAndy Whitcroft	my @stmt_statements = ($stmt =~ /;/g);
805cf655043SAndy Whitcroft
806cf655043SAndy Whitcroft	my $stmt_lines = $#stmt_lines + 2;
807cf655043SAndy Whitcroft	my $stmt_statements = $#stmt_statements + 1;
808cf655043SAndy Whitcroft
809cf655043SAndy Whitcroft	if ($stmt_lines > $stmt_statements) {
810cf655043SAndy Whitcroft		return $stmt_lines;
811cf655043SAndy Whitcroft	} else {
812cf655043SAndy Whitcroft		return $stmt_statements;
813cf655043SAndy Whitcroft	}
814cf655043SAndy Whitcroft}
815cf655043SAndy Whitcroft
81613214adfSAndy Whitcroftsub ctx_statement_full {
81713214adfSAndy Whitcroft	my ($linenr, $remain, $off) = @_;
81813214adfSAndy Whitcroft	my ($statement, $condition, $level);
81913214adfSAndy Whitcroft
82013214adfSAndy Whitcroft	my (@chunks);
82113214adfSAndy Whitcroft
822cf655043SAndy Whitcroft	# Grab the first conditional/block pair.
82313214adfSAndy Whitcroft	($statement, $condition, $linenr, $remain, $off, $level) =
82413214adfSAndy Whitcroft				ctx_statement_block($linenr, $remain, $off);
825773647a0SAndy Whitcroft	#print "F: c<$condition> s<$statement> remain<$remain>\n";
82613214adfSAndy Whitcroft	push(@chunks, [ $condition, $statement ]);
827cf655043SAndy Whitcroft	if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
828cf655043SAndy Whitcroft		return ($level, $linenr, @chunks);
829cf655043SAndy Whitcroft	}
830cf655043SAndy Whitcroft
831cf655043SAndy Whitcroft	# Pull in the following conditional/block pairs and see if they
832cf655043SAndy Whitcroft	# could continue the statement.
833cf655043SAndy Whitcroft	for (;;) {
83413214adfSAndy Whitcroft		($statement, $condition, $linenr, $remain, $off, $level) =
83513214adfSAndy Whitcroft				ctx_statement_block($linenr, $remain, $off);
836cf655043SAndy Whitcroft		#print "C: c<$condition> s<$statement> remain<$remain>\n";
837773647a0SAndy Whitcroft		last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
838cf655043SAndy Whitcroft		#print "C: push\n";
839cf655043SAndy Whitcroft		push(@chunks, [ $condition, $statement ]);
84013214adfSAndy Whitcroft	}
84113214adfSAndy Whitcroft
84213214adfSAndy Whitcroft	return ($level, $linenr, @chunks);
8438905a67cSAndy Whitcroft}
8448905a67cSAndy Whitcroft
8454a0df2efSAndy Whitcroftsub ctx_block_get {
846f0a594c1SAndy Whitcroft	my ($linenr, $remain, $outer, $open, $close, $off) = @_;
8474a0df2efSAndy Whitcroft	my $line;
8484a0df2efSAndy Whitcroft	my $start = $linenr - 1;
8494a0df2efSAndy Whitcroft	my $blk = '';
8504a0df2efSAndy Whitcroft	my @o;
8514a0df2efSAndy Whitcroft	my @c;
8524a0df2efSAndy Whitcroft	my @res = ();
8534a0df2efSAndy Whitcroft
854f0a594c1SAndy Whitcroft	my $level = 0;
8554635f4fbSAndy Whitcroft	my @stack = ($level);
85600df344fSAndy Whitcroft	for ($line = $start; $remain > 0; $line++) {
85700df344fSAndy Whitcroft		next if ($rawlines[$line] =~ /^-/);
85800df344fSAndy Whitcroft		$remain--;
85900df344fSAndy Whitcroft
86000df344fSAndy Whitcroft		$blk .= $rawlines[$line];
8614635f4fbSAndy Whitcroft
8624635f4fbSAndy Whitcroft		# Handle nested #if/#else.
86301464f30SAndy Whitcroft		if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
8644635f4fbSAndy Whitcroft			push(@stack, $level);
86501464f30SAndy Whitcroft		} elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
8664635f4fbSAndy Whitcroft			$level = $stack[$#stack - 1];
86701464f30SAndy Whitcroft		} elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
8684635f4fbSAndy Whitcroft			$level = pop(@stack);
8694635f4fbSAndy Whitcroft		}
8704635f4fbSAndy Whitcroft
87101464f30SAndy Whitcroft		foreach my $c (split(//, $lines[$line])) {
872f0a594c1SAndy Whitcroft			##print "C<$c>L<$level><$open$close>O<$off>\n";
873f0a594c1SAndy Whitcroft			if ($off > 0) {
874f0a594c1SAndy Whitcroft				$off--;
875f0a594c1SAndy Whitcroft				next;
876f0a594c1SAndy Whitcroft			}
8774a0df2efSAndy Whitcroft
878f0a594c1SAndy Whitcroft			if ($c eq $close && $level > 0) {
879f0a594c1SAndy Whitcroft				$level--;
880f0a594c1SAndy Whitcroft				last if ($level == 0);
881f0a594c1SAndy Whitcroft			} elsif ($c eq $open) {
882f0a594c1SAndy Whitcroft				$level++;
883f0a594c1SAndy Whitcroft			}
884f0a594c1SAndy Whitcroft		}
8854a0df2efSAndy Whitcroft
886f0a594c1SAndy Whitcroft		if (!$outer || $level <= 1) {
88700df344fSAndy Whitcroft			push(@res, $rawlines[$line]);
8884a0df2efSAndy Whitcroft		}
8894a0df2efSAndy Whitcroft
890f0a594c1SAndy Whitcroft		last if ($level == 0);
8914a0df2efSAndy Whitcroft	}
8924a0df2efSAndy Whitcroft
893f0a594c1SAndy Whitcroft	return ($level, @res);
8944a0df2efSAndy Whitcroft}
8954a0df2efSAndy Whitcroftsub ctx_block_outer {
8964a0df2efSAndy Whitcroft	my ($linenr, $remain) = @_;
8974a0df2efSAndy Whitcroft
898f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
899f0a594c1SAndy Whitcroft	return @r;
9004a0df2efSAndy Whitcroft}
9014a0df2efSAndy Whitcroftsub ctx_block {
9024a0df2efSAndy Whitcroft	my ($linenr, $remain) = @_;
9034a0df2efSAndy Whitcroft
904f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
905f0a594c1SAndy Whitcroft	return @r;
906653d4876SAndy Whitcroft}
907653d4876SAndy Whitcroftsub ctx_statement {
908f0a594c1SAndy Whitcroft	my ($linenr, $remain, $off) = @_;
909f0a594c1SAndy Whitcroft
910f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
911f0a594c1SAndy Whitcroft	return @r;
912f0a594c1SAndy Whitcroft}
913f0a594c1SAndy Whitcroftsub ctx_block_level {
914653d4876SAndy Whitcroft	my ($linenr, $remain) = @_;
915653d4876SAndy Whitcroft
916f0a594c1SAndy Whitcroft	return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
9174a0df2efSAndy Whitcroft}
9189c0ca6f9SAndy Whitcroftsub ctx_statement_level {
9199c0ca6f9SAndy Whitcroft	my ($linenr, $remain, $off) = @_;
9209c0ca6f9SAndy Whitcroft
9219c0ca6f9SAndy Whitcroft	return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
9229c0ca6f9SAndy Whitcroft}
9234a0df2efSAndy Whitcroft
9244a0df2efSAndy Whitcroftsub ctx_locate_comment {
9254a0df2efSAndy Whitcroft	my ($first_line, $end_line) = @_;
9264a0df2efSAndy Whitcroft
9274a0df2efSAndy Whitcroft	# Catch a comment on the end of the line itself.
928beae6332SAndy Whitcroft	my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
9294a0df2efSAndy Whitcroft	return $current_comment if (defined $current_comment);
9304a0df2efSAndy Whitcroft
9314a0df2efSAndy Whitcroft	# Look through the context and try and figure out if there is a
9324a0df2efSAndy Whitcroft	# comment.
9334a0df2efSAndy Whitcroft	my $in_comment = 0;
9344a0df2efSAndy Whitcroft	$current_comment = '';
9354a0df2efSAndy Whitcroft	for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
93600df344fSAndy Whitcroft		my $line = $rawlines[$linenr - 1];
93700df344fSAndy Whitcroft		#warn "           $line\n";
9384a0df2efSAndy Whitcroft		if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
9394a0df2efSAndy Whitcroft			$in_comment = 1;
9404a0df2efSAndy Whitcroft		}
9414a0df2efSAndy Whitcroft		if ($line =~ m@/\*@) {
9424a0df2efSAndy Whitcroft			$in_comment = 1;
9434a0df2efSAndy Whitcroft		}
9444a0df2efSAndy Whitcroft		if (!$in_comment && $current_comment ne '') {
9454a0df2efSAndy Whitcroft			$current_comment = '';
9464a0df2efSAndy Whitcroft		}
9474a0df2efSAndy Whitcroft		$current_comment .= $line . "\n" if ($in_comment);
9484a0df2efSAndy Whitcroft		if ($line =~ m@\*/@) {
9494a0df2efSAndy Whitcroft			$in_comment = 0;
9504a0df2efSAndy Whitcroft		}
9514a0df2efSAndy Whitcroft	}
9524a0df2efSAndy Whitcroft
9534a0df2efSAndy Whitcroft	chomp($current_comment);
9544a0df2efSAndy Whitcroft	return($current_comment);
9554a0df2efSAndy Whitcroft}
9564a0df2efSAndy Whitcroftsub ctx_has_comment {
9574a0df2efSAndy Whitcroft	my ($first_line, $end_line) = @_;
9584a0df2efSAndy Whitcroft	my $cmt = ctx_locate_comment($first_line, $end_line);
9594a0df2efSAndy Whitcroft
96000df344fSAndy Whitcroft	##print "LINE: $rawlines[$end_line - 1 ]\n";
9614a0df2efSAndy Whitcroft	##print "CMMT: $cmt\n";
9624a0df2efSAndy Whitcroft
9634a0df2efSAndy Whitcroft	return ($cmt ne '');
9644a0df2efSAndy Whitcroft}
9654a0df2efSAndy Whitcroft
9664d001e4dSAndy Whitcroftsub raw_line {
9674d001e4dSAndy Whitcroft	my ($linenr, $cnt) = @_;
9684d001e4dSAndy Whitcroft
9694d001e4dSAndy Whitcroft	my $offset = $linenr - 1;
9704d001e4dSAndy Whitcroft	$cnt++;
9714d001e4dSAndy Whitcroft
9724d001e4dSAndy Whitcroft	my $line;
9734d001e4dSAndy Whitcroft	while ($cnt) {
9744d001e4dSAndy Whitcroft		$line = $rawlines[$offset++];
9754d001e4dSAndy Whitcroft		next if (defined($line) && $line =~ /^-/);
9764d001e4dSAndy Whitcroft		$cnt--;
9774d001e4dSAndy Whitcroft	}
9784d001e4dSAndy Whitcroft
9794d001e4dSAndy Whitcroft	return $line;
9804d001e4dSAndy Whitcroft}
9814d001e4dSAndy Whitcroft
9820a920b5bSAndy Whitcroftsub cat_vet {
9830a920b5bSAndy Whitcroft	my ($vet) = @_;
9849c0ca6f9SAndy Whitcroft	my ($res, $coded);
9850a920b5bSAndy Whitcroft
9869c0ca6f9SAndy Whitcroft	$res = '';
9876c72ffaaSAndy Whitcroft	while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
9886c72ffaaSAndy Whitcroft		$res .= $1;
9896c72ffaaSAndy Whitcroft		if ($2 ne '') {
9909c0ca6f9SAndy Whitcroft			$coded = sprintf("^%c", unpack('C', $2) + 64);
9916c72ffaaSAndy Whitcroft			$res .= $coded;
9926c72ffaaSAndy Whitcroft		}
9939c0ca6f9SAndy Whitcroft	}
9949c0ca6f9SAndy Whitcroft	$res =~ s/$/\$/;
9950a920b5bSAndy Whitcroft
9969c0ca6f9SAndy Whitcroft	return $res;
9970a920b5bSAndy Whitcroft}
9980a920b5bSAndy Whitcroft
999c2fdda0dSAndy Whitcroftmy $av_preprocessor = 0;
1000cf655043SAndy Whitcroftmy $av_pending;
1001c2fdda0dSAndy Whitcroftmy @av_paren_type;
10021f65f947SAndy Whitcroftmy $av_pend_colon;
1003c2fdda0dSAndy Whitcroft
1004c2fdda0dSAndy Whitcroftsub annotate_reset {
1005c2fdda0dSAndy Whitcroft	$av_preprocessor = 0;
1006cf655043SAndy Whitcroft	$av_pending = '_';
1007cf655043SAndy Whitcroft	@av_paren_type = ('E');
10081f65f947SAndy Whitcroft	$av_pend_colon = 'O';
1009c2fdda0dSAndy Whitcroft}
1010c2fdda0dSAndy Whitcroft
10116c72ffaaSAndy Whitcroftsub annotate_values {
10126c72ffaaSAndy Whitcroft	my ($stream, $type) = @_;
10136c72ffaaSAndy Whitcroft
10146c72ffaaSAndy Whitcroft	my $res;
10151f65f947SAndy Whitcroft	my $var = '_' x length($stream);
10166c72ffaaSAndy Whitcroft	my $cur = $stream;
10176c72ffaaSAndy Whitcroft
1018c2fdda0dSAndy Whitcroft	print "$stream\n" if ($dbg_values > 1);
10196c72ffaaSAndy Whitcroft
10206c72ffaaSAndy Whitcroft	while (length($cur)) {
1021773647a0SAndy Whitcroft		@av_paren_type = ('E') if ($#av_paren_type < 0);
1022cf655043SAndy Whitcroft		print " <" . join('', @av_paren_type) .
1023171ae1a4SAndy Whitcroft				"> <$type> <$av_pending>" if ($dbg_values > 1);
10246c72ffaaSAndy Whitcroft		if ($cur =~ /^(\s+)/o) {
1025c2fdda0dSAndy Whitcroft			print "WS($1)\n" if ($dbg_values > 1);
1026c2fdda0dSAndy Whitcroft			if ($1 =~ /\n/ && $av_preprocessor) {
1027cf655043SAndy Whitcroft				$type = pop(@av_paren_type);
1028c2fdda0dSAndy Whitcroft				$av_preprocessor = 0;
10296c72ffaaSAndy Whitcroft			}
10306c72ffaaSAndy Whitcroft
1031c023e473SFlorian Mickler		} elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
10329446ef56SAndy Whitcroft			print "CAST($1)\n" if ($dbg_values > 1);
10339446ef56SAndy Whitcroft			push(@av_paren_type, $type);
1034addcdceaSAndy Whitcroft			$type = 'c';
10359446ef56SAndy Whitcroft
1036e91b6e26SAndy Whitcroft		} elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
1037c2fdda0dSAndy Whitcroft			print "DECLARE($1)\n" if ($dbg_values > 1);
10386c72ffaaSAndy Whitcroft			$type = 'T';
10396c72ffaaSAndy Whitcroft
1040389a2fe5SAndy Whitcroft		} elsif ($cur =~ /^($Modifier)\s*/) {
1041389a2fe5SAndy Whitcroft			print "MODIFIER($1)\n" if ($dbg_values > 1);
1042389a2fe5SAndy Whitcroft			$type = 'T';
1043389a2fe5SAndy Whitcroft
1044c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
1045171ae1a4SAndy Whitcroft			print "DEFINE($1,$2)\n" if ($dbg_values > 1);
1046c2fdda0dSAndy Whitcroft			$av_preprocessor = 1;
1047171ae1a4SAndy Whitcroft			push(@av_paren_type, $type);
1048171ae1a4SAndy Whitcroft			if ($2 ne '') {
1049cf655043SAndy Whitcroft				$av_pending = 'N';
1050171ae1a4SAndy Whitcroft			}
1051171ae1a4SAndy Whitcroft			$type = 'E';
1052171ae1a4SAndy Whitcroft
1053c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
1054171ae1a4SAndy Whitcroft			print "UNDEF($1)\n" if ($dbg_values > 1);
1055171ae1a4SAndy Whitcroft			$av_preprocessor = 1;
1056171ae1a4SAndy Whitcroft			push(@av_paren_type, $type);
10576c72ffaaSAndy Whitcroft
1058c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
1059cf655043SAndy Whitcroft			print "PRE_START($1)\n" if ($dbg_values > 1);
1060c2fdda0dSAndy Whitcroft			$av_preprocessor = 1;
1061cf655043SAndy Whitcroft
1062cf655043SAndy Whitcroft			push(@av_paren_type, $type);
1063cf655043SAndy Whitcroft			push(@av_paren_type, $type);
1064171ae1a4SAndy Whitcroft			$type = 'E';
1065cf655043SAndy Whitcroft
1066c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
1067cf655043SAndy Whitcroft			print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1068cf655043SAndy Whitcroft			$av_preprocessor = 1;
1069cf655043SAndy Whitcroft
1070cf655043SAndy Whitcroft			push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1071cf655043SAndy Whitcroft
1072171ae1a4SAndy Whitcroft			$type = 'E';
1073cf655043SAndy Whitcroft
1074c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:endif))/o) {
1075cf655043SAndy Whitcroft			print "PRE_END($1)\n" if ($dbg_values > 1);
1076cf655043SAndy Whitcroft
1077cf655043SAndy Whitcroft			$av_preprocessor = 1;
1078cf655043SAndy Whitcroft
1079cf655043SAndy Whitcroft			# Assume all arms of the conditional end as this
1080cf655043SAndy Whitcroft			# one does, and continue as if the #endif was not here.
1081cf655043SAndy Whitcroft			pop(@av_paren_type);
1082cf655043SAndy Whitcroft			push(@av_paren_type, $type);
1083171ae1a4SAndy Whitcroft			$type = 'E';
10846c72ffaaSAndy Whitcroft
10856c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\\\n)/o) {
1086c2fdda0dSAndy Whitcroft			print "PRECONT($1)\n" if ($dbg_values > 1);
10876c72ffaaSAndy Whitcroft
1088171ae1a4SAndy Whitcroft		} elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1089171ae1a4SAndy Whitcroft			print "ATTR($1)\n" if ($dbg_values > 1);
1090171ae1a4SAndy Whitcroft			$av_pending = $type;
1091171ae1a4SAndy Whitcroft			$type = 'N';
1092171ae1a4SAndy Whitcroft
10936c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
1094c2fdda0dSAndy Whitcroft			print "SIZEOF($1)\n" if ($dbg_values > 1);
10956c72ffaaSAndy Whitcroft			if (defined $2) {
1096cf655043SAndy Whitcroft				$av_pending = 'V';
10976c72ffaaSAndy Whitcroft			}
10986c72ffaaSAndy Whitcroft			$type = 'N';
10996c72ffaaSAndy Whitcroft
110014b111c1SAndy Whitcroft		} elsif ($cur =~ /^(if|while|for)\b/o) {
1101c2fdda0dSAndy Whitcroft			print "COND($1)\n" if ($dbg_values > 1);
110214b111c1SAndy Whitcroft			$av_pending = 'E';
11036c72ffaaSAndy Whitcroft			$type = 'N';
11046c72ffaaSAndy Whitcroft
11051f65f947SAndy Whitcroft		} elsif ($cur =~/^(case)/o) {
11061f65f947SAndy Whitcroft			print "CASE($1)\n" if ($dbg_values > 1);
11071f65f947SAndy Whitcroft			$av_pend_colon = 'C';
11081f65f947SAndy Whitcroft			$type = 'N';
11091f65f947SAndy Whitcroft
111014b111c1SAndy Whitcroft		} elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
1111c2fdda0dSAndy Whitcroft			print "KEYWORD($1)\n" if ($dbg_values > 1);
11126c72ffaaSAndy Whitcroft			$type = 'N';
11136c72ffaaSAndy Whitcroft
11146c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\()/o) {
1115c2fdda0dSAndy Whitcroft			print "PAREN('$1')\n" if ($dbg_values > 1);
1116cf655043SAndy Whitcroft			push(@av_paren_type, $av_pending);
1117cf655043SAndy Whitcroft			$av_pending = '_';
11186c72ffaaSAndy Whitcroft			$type = 'N';
11196c72ffaaSAndy Whitcroft
11206c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\))/o) {
1121cf655043SAndy Whitcroft			my $new_type = pop(@av_paren_type);
1122cf655043SAndy Whitcroft			if ($new_type ne '_') {
1123cf655043SAndy Whitcroft				$type = $new_type;
1124c2fdda0dSAndy Whitcroft				print "PAREN('$1') -> $type\n"
1125c2fdda0dSAndy Whitcroft							if ($dbg_values > 1);
11266c72ffaaSAndy Whitcroft			} else {
1127c2fdda0dSAndy Whitcroft				print "PAREN('$1')\n" if ($dbg_values > 1);
11286c72ffaaSAndy Whitcroft			}
11296c72ffaaSAndy Whitcroft
1130c8cb2ca3SAndy Whitcroft		} elsif ($cur =~ /^($Ident)\s*\(/o) {
1131c2fdda0dSAndy Whitcroft			print "FUNC($1)\n" if ($dbg_values > 1);
1132c8cb2ca3SAndy Whitcroft			$type = 'V';
1133cf655043SAndy Whitcroft			$av_pending = 'V';
11346c72ffaaSAndy Whitcroft
11358e761b04SAndy Whitcroft		} elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
11368e761b04SAndy Whitcroft			if (defined $2 && $type eq 'C' || $type eq 'T') {
11371f65f947SAndy Whitcroft				$av_pend_colon = 'B';
11388e761b04SAndy Whitcroft			} elsif ($type eq 'E') {
11398e761b04SAndy Whitcroft				$av_pend_colon = 'L';
11401f65f947SAndy Whitcroft			}
11411f65f947SAndy Whitcroft			print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
11421f65f947SAndy Whitcroft			$type = 'V';
11431f65f947SAndy Whitcroft
11446c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Ident|$Constant)/o) {
1145c2fdda0dSAndy Whitcroft			print "IDENT($1)\n" if ($dbg_values > 1);
11466c72ffaaSAndy Whitcroft			$type = 'V';
11476c72ffaaSAndy Whitcroft
11486c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Assignment)/o) {
1149c2fdda0dSAndy Whitcroft			print "ASSIGN($1)\n" if ($dbg_values > 1);
11506c72ffaaSAndy Whitcroft			$type = 'N';
11516c72ffaaSAndy Whitcroft
1152cf655043SAndy Whitcroft		} elsif ($cur =~/^(;|{|})/) {
1153c2fdda0dSAndy Whitcroft			print "END($1)\n" if ($dbg_values > 1);
115413214adfSAndy Whitcroft			$type = 'E';
11551f65f947SAndy Whitcroft			$av_pend_colon = 'O';
115613214adfSAndy Whitcroft
11578e761b04SAndy Whitcroft		} elsif ($cur =~/^(,)/) {
11588e761b04SAndy Whitcroft			print "COMMA($1)\n" if ($dbg_values > 1);
11598e761b04SAndy Whitcroft			$type = 'C';
11608e761b04SAndy Whitcroft
11611f65f947SAndy Whitcroft		} elsif ($cur =~ /^(\?)/o) {
11621f65f947SAndy Whitcroft			print "QUESTION($1)\n" if ($dbg_values > 1);
11631f65f947SAndy Whitcroft			$type = 'N';
11641f65f947SAndy Whitcroft
11651f65f947SAndy Whitcroft		} elsif ($cur =~ /^(:)/o) {
11661f65f947SAndy Whitcroft			print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
11671f65f947SAndy Whitcroft
11681f65f947SAndy Whitcroft			substr($var, length($res), 1, $av_pend_colon);
11691f65f947SAndy Whitcroft			if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
11701f65f947SAndy Whitcroft				$type = 'E';
11711f65f947SAndy Whitcroft			} else {
11721f65f947SAndy Whitcroft				$type = 'N';
11731f65f947SAndy Whitcroft			}
11741f65f947SAndy Whitcroft			$av_pend_colon = 'O';
11751f65f947SAndy Whitcroft
11768e761b04SAndy Whitcroft		} elsif ($cur =~ /^(\[)/o) {
117713214adfSAndy Whitcroft			print "CLOSE($1)\n" if ($dbg_values > 1);
11786c72ffaaSAndy Whitcroft			$type = 'N';
11796c72ffaaSAndy Whitcroft
11800d413866SAndy Whitcroft		} elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
118174048ed8SAndy Whitcroft			my $variant;
118274048ed8SAndy Whitcroft
118374048ed8SAndy Whitcroft			print "OPV($1)\n" if ($dbg_values > 1);
118474048ed8SAndy Whitcroft			if ($type eq 'V') {
118574048ed8SAndy Whitcroft				$variant = 'B';
118674048ed8SAndy Whitcroft			} else {
118774048ed8SAndy Whitcroft				$variant = 'U';
118874048ed8SAndy Whitcroft			}
118974048ed8SAndy Whitcroft
119074048ed8SAndy Whitcroft			substr($var, length($res), 1, $variant);
119174048ed8SAndy Whitcroft			$type = 'N';
119274048ed8SAndy Whitcroft
11936c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Operators)/o) {
1194c2fdda0dSAndy Whitcroft			print "OP($1)\n" if ($dbg_values > 1);
11956c72ffaaSAndy Whitcroft			if ($1 ne '++' && $1 ne '--') {
11966c72ffaaSAndy Whitcroft				$type = 'N';
11976c72ffaaSAndy Whitcroft			}
11986c72ffaaSAndy Whitcroft
11996c72ffaaSAndy Whitcroft		} elsif ($cur =~ /(^.)/o) {
1200c2fdda0dSAndy Whitcroft			print "C($1)\n" if ($dbg_values > 1);
12016c72ffaaSAndy Whitcroft		}
12026c72ffaaSAndy Whitcroft		if (defined $1) {
12036c72ffaaSAndy Whitcroft			$cur = substr($cur, length($1));
12046c72ffaaSAndy Whitcroft			$res .= $type x length($1);
12056c72ffaaSAndy Whitcroft		}
12066c72ffaaSAndy Whitcroft	}
12076c72ffaaSAndy Whitcroft
12081f65f947SAndy Whitcroft	return ($res, $var);
12096c72ffaaSAndy Whitcroft}
12106c72ffaaSAndy Whitcroft
12118905a67cSAndy Whitcroftsub possible {
121213214adfSAndy Whitcroft	my ($possible, $line) = @_;
12139a974fdbSAndy Whitcroft	my $notPermitted = qr{(?:
12140776e594SAndy Whitcroft		^(?:
12150776e594SAndy Whitcroft			$Modifier|
12160776e594SAndy Whitcroft			$Storage|
12170776e594SAndy Whitcroft			$Type|
12189a974fdbSAndy Whitcroft			DEFINE_\S+
12199a974fdbSAndy Whitcroft		)$|
12209a974fdbSAndy Whitcroft		^(?:
12210776e594SAndy Whitcroft			goto|
12220776e594SAndy Whitcroft			return|
12230776e594SAndy Whitcroft			case|
12240776e594SAndy Whitcroft			else|
12250776e594SAndy Whitcroft			asm|__asm__|
122689a88353SAndy Whitcroft			do|
122789a88353SAndy Whitcroft			\#|
122889a88353SAndy Whitcroft			\#\#|
12299a974fdbSAndy Whitcroft		)(?:\s|$)|
12300776e594SAndy Whitcroft		^(?:typedef|struct|enum)\b
12319a974fdbSAndy Whitcroft	    )}x;
12329a974fdbSAndy Whitcroft	warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
12339a974fdbSAndy Whitcroft	if ($possible !~ $notPermitted) {
1234c45dcabdSAndy Whitcroft		# Check for modifiers.
1235c45dcabdSAndy Whitcroft		$possible =~ s/\s*$Storage\s*//g;
1236c45dcabdSAndy Whitcroft		$possible =~ s/\s*$Sparse\s*//g;
1237c45dcabdSAndy Whitcroft		if ($possible =~ /^\s*$/) {
1238c45dcabdSAndy Whitcroft
1239c45dcabdSAndy Whitcroft		} elsif ($possible =~ /\s/) {
1240c45dcabdSAndy Whitcroft			$possible =~ s/\s*$Type\s*//g;
1241d2506586SAndy Whitcroft			for my $modifier (split(' ', $possible)) {
12429a974fdbSAndy Whitcroft				if ($modifier !~ $notPermitted) {
1243d2506586SAndy Whitcroft					warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1244d2506586SAndy Whitcroft					push(@modifierList, $modifier);
1245d2506586SAndy Whitcroft				}
12469a974fdbSAndy Whitcroft			}
1247c45dcabdSAndy Whitcroft
1248c45dcabdSAndy Whitcroft		} else {
124913214adfSAndy Whitcroft			warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
12508905a67cSAndy Whitcroft			push(@typeList, $possible);
1251c45dcabdSAndy Whitcroft		}
12528905a67cSAndy Whitcroft		build_types();
12530776e594SAndy Whitcroft	} else {
12540776e594SAndy Whitcroft		warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
12558905a67cSAndy Whitcroft	}
12568905a67cSAndy Whitcroft}
12578905a67cSAndy Whitcroft
12586c72ffaaSAndy Whitcroftmy $prefix = '';
12596c72ffaaSAndy Whitcroft
1260000d1cc1SJoe Perchessub show_type {
1261000d1cc1SJoe Perches       return !defined $ignore_type{$_[0]};
1262000d1cc1SJoe Perches}
1263000d1cc1SJoe Perches
1264f0a594c1SAndy Whitcroftsub report {
1265000d1cc1SJoe Perches	if (!show_type($_[1]) ||
1266000d1cc1SJoe Perches	    (defined $tst_only && $_[2] !~ /\Q$tst_only\E/)) {
1267773647a0SAndy Whitcroft		return 0;
1268773647a0SAndy Whitcroft	}
1269000d1cc1SJoe Perches	my $line;
1270000d1cc1SJoe Perches	if ($show_types) {
1271000d1cc1SJoe Perches		$line = "$prefix$_[0]:$_[1]: $_[2]\n";
1272000d1cc1SJoe Perches	} else {
1273000d1cc1SJoe Perches		$line = "$prefix$_[0]: $_[2]\n";
1274000d1cc1SJoe Perches	}
12758905a67cSAndy Whitcroft	$line = (split('\n', $line))[0] . "\n" if ($terse);
12768905a67cSAndy Whitcroft
127713214adfSAndy Whitcroft	push(our @report, $line);
1278773647a0SAndy Whitcroft
1279773647a0SAndy Whitcroft	return 1;
1280f0a594c1SAndy Whitcroft}
1281f0a594c1SAndy Whitcroftsub report_dump {
128213214adfSAndy Whitcroft	our @report;
1283f0a594c1SAndy Whitcroft}
1284000d1cc1SJoe Perches
1285de7d4f0eSAndy Whitcroftsub ERROR {
1286000d1cc1SJoe Perches	if (report("ERROR", $_[0], $_[1])) {
1287de7d4f0eSAndy Whitcroft		our $clean = 0;
12886c72ffaaSAndy Whitcroft		our $cnt_error++;
1289de7d4f0eSAndy Whitcroft	}
1290773647a0SAndy Whitcroft}
1291de7d4f0eSAndy Whitcroftsub WARN {
1292000d1cc1SJoe Perches	if (report("WARNING", $_[0], $_[1])) {
1293de7d4f0eSAndy Whitcroft		our $clean = 0;
12946c72ffaaSAndy Whitcroft		our $cnt_warn++;
1295de7d4f0eSAndy Whitcroft	}
1296773647a0SAndy Whitcroft}
1297de7d4f0eSAndy Whitcroftsub CHK {
1298000d1cc1SJoe Perches	if ($check && report("CHECK", $_[0], $_[1])) {
1299de7d4f0eSAndy Whitcroft		our $clean = 0;
13006c72ffaaSAndy Whitcroft		our $cnt_chk++;
13016c72ffaaSAndy Whitcroft	}
1302de7d4f0eSAndy Whitcroft}
1303de7d4f0eSAndy Whitcroft
13046ecd9674SAndy Whitcroftsub check_absolute_file {
13056ecd9674SAndy Whitcroft	my ($absolute, $herecurr) = @_;
13066ecd9674SAndy Whitcroft	my $file = $absolute;
13076ecd9674SAndy Whitcroft
13086ecd9674SAndy Whitcroft	##print "absolute<$absolute>\n";
13096ecd9674SAndy Whitcroft
13106ecd9674SAndy Whitcroft	# See if any suffix of this path is a path within the tree.
13116ecd9674SAndy Whitcroft	while ($file =~ s@^[^/]*/@@) {
13126ecd9674SAndy Whitcroft		if (-f "$root/$file") {
13136ecd9674SAndy Whitcroft			##print "file<$file>\n";
13146ecd9674SAndy Whitcroft			last;
13156ecd9674SAndy Whitcroft		}
13166ecd9674SAndy Whitcroft	}
13176ecd9674SAndy Whitcroft	if (! -f _)  {
13186ecd9674SAndy Whitcroft		return 0;
13196ecd9674SAndy Whitcroft	}
13206ecd9674SAndy Whitcroft
13216ecd9674SAndy Whitcroft	# It is, so see if the prefix is acceptable.
13226ecd9674SAndy Whitcroft	my $prefix = $absolute;
13236ecd9674SAndy Whitcroft	substr($prefix, -length($file)) = '';
13246ecd9674SAndy Whitcroft
13256ecd9674SAndy Whitcroft	##print "prefix<$prefix>\n";
13266ecd9674SAndy Whitcroft	if ($prefix ne ".../") {
1327000d1cc1SJoe Perches		WARN("USE_RELATIVE_PATH",
1328000d1cc1SJoe Perches		     "use relative pathname instead of absolute in changelog text\n" . $herecurr);
13296ecd9674SAndy Whitcroft	}
13306ecd9674SAndy Whitcroft}
13316ecd9674SAndy Whitcroft
1332d1fe9c09SJoe Perchessub pos_last_openparen {
1333d1fe9c09SJoe Perches	my ($line) = @_;
1334d1fe9c09SJoe Perches
1335d1fe9c09SJoe Perches	my $pos = 0;
1336d1fe9c09SJoe Perches
1337d1fe9c09SJoe Perches	my $opens = $line =~ tr/\(/\(/;
1338d1fe9c09SJoe Perches	my $closes = $line =~ tr/\)/\)/;
1339d1fe9c09SJoe Perches
1340d1fe9c09SJoe Perches	my $last_openparen = 0;
1341d1fe9c09SJoe Perches
1342d1fe9c09SJoe Perches	if (($opens == 0) || ($closes >= $opens)) {
1343d1fe9c09SJoe Perches		return -1;
1344d1fe9c09SJoe Perches	}
1345d1fe9c09SJoe Perches
1346d1fe9c09SJoe Perches	my $len = length($line);
1347d1fe9c09SJoe Perches
1348d1fe9c09SJoe Perches	for ($pos = 0; $pos < $len; $pos++) {
1349d1fe9c09SJoe Perches		my $string = substr($line, $pos);
1350d1fe9c09SJoe Perches		if ($string =~ /^($FuncArg|$balanced_parens)/) {
1351d1fe9c09SJoe Perches			$pos += length($1) - 1;
1352d1fe9c09SJoe Perches		} elsif (substr($line, $pos, 1) eq '(') {
1353d1fe9c09SJoe Perches			$last_openparen = $pos;
1354d1fe9c09SJoe Perches		} elsif (index($string, '(') == -1) {
1355d1fe9c09SJoe Perches			last;
1356d1fe9c09SJoe Perches		}
1357d1fe9c09SJoe Perches	}
1358d1fe9c09SJoe Perches
1359d1fe9c09SJoe Perches	return $last_openparen + 1;
1360d1fe9c09SJoe Perches}
1361d1fe9c09SJoe Perches
13620a920b5bSAndy Whitcroftsub process {
13630a920b5bSAndy Whitcroft	my $filename = shift;
13640a920b5bSAndy Whitcroft
13650a920b5bSAndy Whitcroft	my $linenr=0;
13660a920b5bSAndy Whitcroft	my $prevline="";
1367c2fdda0dSAndy Whitcroft	my $prevrawline="";
13680a920b5bSAndy Whitcroft	my $stashline="";
1369c2fdda0dSAndy Whitcroft	my $stashrawline="";
13700a920b5bSAndy Whitcroft
13714a0df2efSAndy Whitcroft	my $length;
13720a920b5bSAndy Whitcroft	my $indent;
13730a920b5bSAndy Whitcroft	my $previndent=0;
13740a920b5bSAndy Whitcroft	my $stashindent=0;
13750a920b5bSAndy Whitcroft
1376de7d4f0eSAndy Whitcroft	our $clean = 1;
13770a920b5bSAndy Whitcroft	my $signoff = 0;
13780a920b5bSAndy Whitcroft	my $is_patch = 0;
13790a920b5bSAndy Whitcroft
138015662b3eSJoe Perches	my $in_header_lines = 1;
138115662b3eSJoe Perches	my $in_commit_log = 0;		#Scanning lines before patch
138215662b3eSJoe Perches
1383fa64205dSPasi Savanainen	my $non_utf8_charset = 0;
1384fa64205dSPasi Savanainen
138513214adfSAndy Whitcroft	our @report = ();
13866c72ffaaSAndy Whitcroft	our $cnt_lines = 0;
13876c72ffaaSAndy Whitcroft	our $cnt_error = 0;
13886c72ffaaSAndy Whitcroft	our $cnt_warn = 0;
13896c72ffaaSAndy Whitcroft	our $cnt_chk = 0;
13906c72ffaaSAndy Whitcroft
13910a920b5bSAndy Whitcroft	# Trace the real file/line as we go.
13920a920b5bSAndy Whitcroft	my $realfile = '';
13930a920b5bSAndy Whitcroft	my $realline = 0;
13940a920b5bSAndy Whitcroft	my $realcnt = 0;
13950a920b5bSAndy Whitcroft	my $here = '';
13960a920b5bSAndy Whitcroft	my $in_comment = 0;
1397c2fdda0dSAndy Whitcroft	my $comment_edge = 0;
13980a920b5bSAndy Whitcroft	my $first_line = 0;
13991e855726SWolfram Sang	my $p1_prefix = '';
14000a920b5bSAndy Whitcroft
140113214adfSAndy Whitcroft	my $prev_values = 'E';
140213214adfSAndy Whitcroft
140313214adfSAndy Whitcroft	# suppression flags
1404773647a0SAndy Whitcroft	my %suppress_ifbraces;
1405170d3a22SAndy Whitcroft	my %suppress_whiletrailers;
14062b474a1aSAndy Whitcroft	my %suppress_export;
14073e469cdcSAndy Whitcroft	my $suppress_statement = 0;
1408653d4876SAndy Whitcroft
1409323c1260SJoe Perches	my %camelcase = ();
1410323c1260SJoe Perches
1411c2fdda0dSAndy Whitcroft	# Pre-scan the patch sanitizing the lines.
1412de7d4f0eSAndy Whitcroft	# Pre-scan the patch looking for any __setup documentation.
1413c2fdda0dSAndy Whitcroft	#
1414de7d4f0eSAndy Whitcroft	my @setup_docs = ();
1415de7d4f0eSAndy Whitcroft	my $setup_docs = 0;
1416773647a0SAndy Whitcroft
1417773647a0SAndy Whitcroft	sanitise_line_reset();
1418c2fdda0dSAndy Whitcroft	my $line;
1419c2fdda0dSAndy Whitcroft	foreach my $rawline (@rawlines) {
1420773647a0SAndy Whitcroft		$linenr++;
1421773647a0SAndy Whitcroft		$line = $rawline;
1422c2fdda0dSAndy Whitcroft
1423773647a0SAndy Whitcroft		if ($rawline=~/^\+\+\+\s+(\S+)/) {
1424de7d4f0eSAndy Whitcroft			$setup_docs = 0;
1425de7d4f0eSAndy Whitcroft			if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
1426de7d4f0eSAndy Whitcroft				$setup_docs = 1;
1427de7d4f0eSAndy Whitcroft			}
1428773647a0SAndy Whitcroft			#next;
1429de7d4f0eSAndy Whitcroft		}
1430773647a0SAndy Whitcroft		if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1431773647a0SAndy Whitcroft			$realline=$1-1;
1432773647a0SAndy Whitcroft			if (defined $2) {
1433773647a0SAndy Whitcroft				$realcnt=$3+1;
1434773647a0SAndy Whitcroft			} else {
1435773647a0SAndy Whitcroft				$realcnt=1+1;
1436773647a0SAndy Whitcroft			}
1437c45dcabdSAndy Whitcroft			$in_comment = 0;
1438773647a0SAndy Whitcroft
1439773647a0SAndy Whitcroft			# Guestimate if this is a continuing comment.  Run
1440773647a0SAndy Whitcroft			# the context looking for a comment "edge".  If this
1441773647a0SAndy Whitcroft			# edge is a close comment then we must be in a comment
1442773647a0SAndy Whitcroft			# at context start.
1443773647a0SAndy Whitcroft			my $edge;
144401fa9147SAndy Whitcroft			my $cnt = $realcnt;
144501fa9147SAndy Whitcroft			for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
144601fa9147SAndy Whitcroft				next if (defined $rawlines[$ln - 1] &&
144701fa9147SAndy Whitcroft					 $rawlines[$ln - 1] =~ /^-/);
144801fa9147SAndy Whitcroft				$cnt--;
144901fa9147SAndy Whitcroft				#print "RAW<$rawlines[$ln - 1]>\n";
1450721c1cb6SAndy Whitcroft				last if (!defined $rawlines[$ln - 1]);
1451fae17daeSAndy Whitcroft				if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
1452fae17daeSAndy Whitcroft				    $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
1453fae17daeSAndy Whitcroft					($edge) = $1;
1454fae17daeSAndy Whitcroft					last;
1455fae17daeSAndy Whitcroft				}
1456773647a0SAndy Whitcroft			}
1457773647a0SAndy Whitcroft			if (defined $edge && $edge eq '*/') {
1458773647a0SAndy Whitcroft				$in_comment = 1;
1459773647a0SAndy Whitcroft			}
1460773647a0SAndy Whitcroft
1461773647a0SAndy Whitcroft			# Guestimate if this is a continuing comment.  If this
1462773647a0SAndy Whitcroft			# is the start of a diff block and this line starts
1463773647a0SAndy Whitcroft			# ' *' then it is very likely a comment.
1464773647a0SAndy Whitcroft			if (!defined $edge &&
146583242e0cSAndy Whitcroft			    $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
1466773647a0SAndy Whitcroft			{
1467773647a0SAndy Whitcroft				$in_comment = 1;
1468773647a0SAndy Whitcroft			}
1469773647a0SAndy Whitcroft
1470773647a0SAndy Whitcroft			##print "COMMENT:$in_comment edge<$edge> $rawline\n";
1471773647a0SAndy Whitcroft			sanitise_line_reset($in_comment);
1472773647a0SAndy Whitcroft
1473171ae1a4SAndy Whitcroft		} elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
1474773647a0SAndy Whitcroft			# Standardise the strings and chars within the input to
1475171ae1a4SAndy Whitcroft			# simplify matching -- only bother with positive lines.
1476773647a0SAndy Whitcroft			$line = sanitise_line($rawline);
1477773647a0SAndy Whitcroft		}
1478773647a0SAndy Whitcroft		push(@lines, $line);
1479773647a0SAndy Whitcroft
1480773647a0SAndy Whitcroft		if ($realcnt > 1) {
1481773647a0SAndy Whitcroft			$realcnt-- if ($line =~ /^(?:\+| |$)/);
1482773647a0SAndy Whitcroft		} else {
1483773647a0SAndy Whitcroft			$realcnt = 0;
1484773647a0SAndy Whitcroft		}
1485773647a0SAndy Whitcroft
1486773647a0SAndy Whitcroft		#print "==>$rawline\n";
1487773647a0SAndy Whitcroft		#print "-->$line\n";
1488de7d4f0eSAndy Whitcroft
1489de7d4f0eSAndy Whitcroft		if ($setup_docs && $line =~ /^\+/) {
1490de7d4f0eSAndy Whitcroft			push(@setup_docs, $line);
1491de7d4f0eSAndy Whitcroft		}
1492de7d4f0eSAndy Whitcroft	}
1493de7d4f0eSAndy Whitcroft
14946c72ffaaSAndy Whitcroft	$prefix = '';
14956c72ffaaSAndy Whitcroft
1496773647a0SAndy Whitcroft	$realcnt = 0;
1497773647a0SAndy Whitcroft	$linenr = 0;
14980a920b5bSAndy Whitcroft	foreach my $line (@lines) {
14990a920b5bSAndy Whitcroft		$linenr++;
15000a920b5bSAndy Whitcroft
1501c2fdda0dSAndy Whitcroft		my $rawline = $rawlines[$linenr - 1];
15026c72ffaaSAndy Whitcroft
15030a920b5bSAndy Whitcroft#extract the line range in the file after the patch is applied
15046c72ffaaSAndy Whitcroft		if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
15050a920b5bSAndy Whitcroft			$is_patch = 1;
15064a0df2efSAndy Whitcroft			$first_line = $linenr + 1;
15070a920b5bSAndy Whitcroft			$realline=$1-1;
15080a920b5bSAndy Whitcroft			if (defined $2) {
15090a920b5bSAndy Whitcroft				$realcnt=$3+1;
15100a920b5bSAndy Whitcroft			} else {
15110a920b5bSAndy Whitcroft				$realcnt=1+1;
15120a920b5bSAndy Whitcroft			}
1513c2fdda0dSAndy Whitcroft			annotate_reset();
151413214adfSAndy Whitcroft			$prev_values = 'E';
151513214adfSAndy Whitcroft
1516773647a0SAndy Whitcroft			%suppress_ifbraces = ();
1517170d3a22SAndy Whitcroft			%suppress_whiletrailers = ();
15182b474a1aSAndy Whitcroft			%suppress_export = ();
15193e469cdcSAndy Whitcroft			$suppress_statement = 0;
15200a920b5bSAndy Whitcroft			next;
15210a920b5bSAndy Whitcroft
15224a0df2efSAndy Whitcroft# track the line number as we move through the hunk, note that
15234a0df2efSAndy Whitcroft# new versions of GNU diff omit the leading space on completely
15244a0df2efSAndy Whitcroft# blank context lines so we need to count that too.
1525773647a0SAndy Whitcroft		} elsif ($line =~ /^( |\+|$)/) {
15260a920b5bSAndy Whitcroft			$realline++;
1527d8aaf121SAndy Whitcroft			$realcnt-- if ($realcnt != 0);
15280a920b5bSAndy Whitcroft
15294a0df2efSAndy Whitcroft			# Measure the line length and indent.
1530c2fdda0dSAndy Whitcroft			($length, $indent) = line_stats($rawline);
15310a920b5bSAndy Whitcroft
15320a920b5bSAndy Whitcroft			# Track the previous line.
15330a920b5bSAndy Whitcroft			($prevline, $stashline) = ($stashline, $line);
15340a920b5bSAndy Whitcroft			($previndent, $stashindent) = ($stashindent, $indent);
1535c2fdda0dSAndy Whitcroft			($prevrawline, $stashrawline) = ($stashrawline, $rawline);
1536c2fdda0dSAndy Whitcroft
1537773647a0SAndy Whitcroft			#warn "line<$line>\n";
15386c72ffaaSAndy Whitcroft
1539d8aaf121SAndy Whitcroft		} elsif ($realcnt == 1) {
1540d8aaf121SAndy Whitcroft			$realcnt--;
15410a920b5bSAndy Whitcroft		}
15420a920b5bSAndy Whitcroft
1543cc77cdcaSAndy Whitcroft		my $hunk_line = ($realcnt != 0);
1544cc77cdcaSAndy Whitcroft
15450a920b5bSAndy Whitcroft#make up the handle for any error we report on this line
1546773647a0SAndy Whitcroft		$prefix = "$filename:$realline: " if ($emacs && $file);
1547773647a0SAndy Whitcroft		$prefix = "$filename:$linenr: " if ($emacs && !$file);
1548773647a0SAndy Whitcroft
15496c72ffaaSAndy Whitcroft		$here = "#$linenr: " if (!$file);
15506c72ffaaSAndy Whitcroft		$here = "#$realline: " if ($file);
1551773647a0SAndy Whitcroft
1552773647a0SAndy Whitcroft		# extract the filename as it passes
15533bf9a009SRabin Vincent		if ($line =~ /^diff --git.*?(\S+)$/) {
15543bf9a009SRabin Vincent			$realfile = $1;
15553bf9a009SRabin Vincent			$realfile =~ s@^([^/]*)/@@;
1556270c49a0SJoe Perches			$in_commit_log = 0;
15573bf9a009SRabin Vincent		} elsif ($line =~ /^\+\+\+\s+(\S+)/) {
1558773647a0SAndy Whitcroft			$realfile = $1;
15591e855726SWolfram Sang			$realfile =~ s@^([^/]*)/@@;
1560270c49a0SJoe Perches			$in_commit_log = 0;
15611e855726SWolfram Sang
15621e855726SWolfram Sang			$p1_prefix = $1;
1563e2f7aa4bSAndy Whitcroft			if (!$file && $tree && $p1_prefix ne '' &&
1564e2f7aa4bSAndy Whitcroft			    -e "$root/$p1_prefix") {
1565000d1cc1SJoe Perches				WARN("PATCH_PREFIX",
1566000d1cc1SJoe Perches				     "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
15671e855726SWolfram Sang			}
1568773647a0SAndy Whitcroft
1569c1ab3326SAndy Whitcroft			if ($realfile =~ m@^include/asm/@) {
1570000d1cc1SJoe Perches				ERROR("MODIFIED_INCLUDE_ASM",
1571000d1cc1SJoe Perches				      "do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
1572773647a0SAndy Whitcroft			}
1573773647a0SAndy Whitcroft			next;
1574773647a0SAndy Whitcroft		}
1575773647a0SAndy Whitcroft
1576389834b6SRandy Dunlap		$here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
15770a920b5bSAndy Whitcroft
1578c2fdda0dSAndy Whitcroft		my $hereline = "$here\n$rawline\n";
1579c2fdda0dSAndy Whitcroft		my $herecurr = "$here\n$rawline\n";
1580c2fdda0dSAndy Whitcroft		my $hereprev = "$here\n$prevrawline\n$rawline\n";
15810a920b5bSAndy Whitcroft
15826c72ffaaSAndy Whitcroft		$cnt_lines++ if ($realcnt != 0);
15836c72ffaaSAndy Whitcroft
15843bf9a009SRabin Vincent# Check for incorrect file permissions
15853bf9a009SRabin Vincent		if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
15863bf9a009SRabin Vincent			my $permhere = $here . "FILE: $realfile\n";
158704db4d25SJoe Perches			if ($realfile !~ m@scripts/@ &&
158804db4d25SJoe Perches			    $realfile !~ /\.(py|pl|awk|sh)$/) {
1589000d1cc1SJoe Perches				ERROR("EXECUTE_PERMISSIONS",
1590000d1cc1SJoe Perches				      "do not set execute permissions for source files\n" . $permhere);
15913bf9a009SRabin Vincent			}
15923bf9a009SRabin Vincent		}
15933bf9a009SRabin Vincent
159420112475SJoe Perches# Check the patch for a signoff:
1595d8aaf121SAndy Whitcroft		if ($line =~ /^\s*signed-off-by:/i) {
15964a0df2efSAndy Whitcroft			$signoff++;
159715662b3eSJoe Perches			$in_commit_log = 0;
15980a920b5bSAndy Whitcroft		}
159920112475SJoe Perches
160020112475SJoe Perches# Check signature styles
1601270c49a0SJoe Perches		if (!$in_header_lines &&
1602ce0338dfSJoe Perches		    $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
160320112475SJoe Perches			my $space_before = $1;
160420112475SJoe Perches			my $sign_off = $2;
160520112475SJoe Perches			my $space_after = $3;
160620112475SJoe Perches			my $email = $4;
160720112475SJoe Perches			my $ucfirst_sign_off = ucfirst(lc($sign_off));
160820112475SJoe Perches
1609ce0338dfSJoe Perches			if ($sign_off !~ /$signature_tags/) {
1610ce0338dfSJoe Perches				WARN("BAD_SIGN_OFF",
1611ce0338dfSJoe Perches				     "Non-standard signature: $sign_off\n" . $herecurr);
1612ce0338dfSJoe Perches			}
161320112475SJoe Perches			if (defined $space_before && $space_before ne "") {
1614000d1cc1SJoe Perches				WARN("BAD_SIGN_OFF",
1615000d1cc1SJoe Perches				     "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr);
161620112475SJoe Perches			}
161720112475SJoe Perches			if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
1618000d1cc1SJoe Perches				WARN("BAD_SIGN_OFF",
1619000d1cc1SJoe Perches				     "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr);
162020112475SJoe Perches			}
162120112475SJoe Perches			if (!defined $space_after || $space_after ne " ") {
1622000d1cc1SJoe Perches				WARN("BAD_SIGN_OFF",
1623000d1cc1SJoe Perches				     "Use a single space after $ucfirst_sign_off\n" . $herecurr);
162420112475SJoe Perches			}
162520112475SJoe Perches
162620112475SJoe Perches			my ($email_name, $email_address, $comment) = parse_email($email);
162720112475SJoe Perches			my $suggested_email = format_email(($email_name, $email_address));
162820112475SJoe Perches			if ($suggested_email eq "") {
1629000d1cc1SJoe Perches				ERROR("BAD_SIGN_OFF",
1630000d1cc1SJoe Perches				      "Unrecognized email address: '$email'\n" . $herecurr);
163120112475SJoe Perches			} else {
163220112475SJoe Perches				my $dequoted = $suggested_email;
163320112475SJoe Perches				$dequoted =~ s/^"//;
163420112475SJoe Perches				$dequoted =~ s/" </ </;
163520112475SJoe Perches				# Don't force email to have quotes
163620112475SJoe Perches				# Allow just an angle bracketed address
163720112475SJoe Perches				if ("$dequoted$comment" ne $email &&
163820112475SJoe Perches				    "<$email_address>$comment" ne $email &&
163920112475SJoe Perches				    "$suggested_email$comment" ne $email) {
1640000d1cc1SJoe Perches					WARN("BAD_SIGN_OFF",
1641000d1cc1SJoe Perches					     "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
164220112475SJoe Perches				}
16430a920b5bSAndy Whitcroft			}
16440a920b5bSAndy Whitcroft		}
16450a920b5bSAndy Whitcroft
164600df344fSAndy Whitcroft# Check for wrappage within a valid hunk of the file
16478905a67cSAndy Whitcroft		if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
1648000d1cc1SJoe Perches			ERROR("CORRUPTED_PATCH",
1649000d1cc1SJoe Perches			      "patch seems to be corrupt (line wrapped?)\n" .
16506c72ffaaSAndy Whitcroft				$herecurr) if (!$emitted_corrupt++);
1651de7d4f0eSAndy Whitcroft		}
1652de7d4f0eSAndy Whitcroft
16536ecd9674SAndy Whitcroft# Check for absolute kernel paths.
16546ecd9674SAndy Whitcroft		if ($tree) {
16556ecd9674SAndy Whitcroft			while ($line =~ m{(?:^|\s)(/\S*)}g) {
16566ecd9674SAndy Whitcroft				my $file = $1;
16576ecd9674SAndy Whitcroft
16586ecd9674SAndy Whitcroft				if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
16596ecd9674SAndy Whitcroft				    check_absolute_file($1, $herecurr)) {
16606ecd9674SAndy Whitcroft					#
16616ecd9674SAndy Whitcroft				} else {
16626ecd9674SAndy Whitcroft					check_absolute_file($file, $herecurr);
16636ecd9674SAndy Whitcroft				}
16646ecd9674SAndy Whitcroft			}
16656ecd9674SAndy Whitcroft		}
16666ecd9674SAndy Whitcroft
1667de7d4f0eSAndy Whitcroft# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
1668de7d4f0eSAndy Whitcroft		if (($realfile =~ /^$/ || $line =~ /^\+/) &&
1669171ae1a4SAndy Whitcroft		    $rawline !~ m/^$UTF8*$/) {
1670171ae1a4SAndy Whitcroft			my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
1671171ae1a4SAndy Whitcroft
1672171ae1a4SAndy Whitcroft			my $blank = copy_spacing($rawline);
1673171ae1a4SAndy Whitcroft			my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
1674171ae1a4SAndy Whitcroft			my $hereptr = "$hereline$ptr\n";
1675171ae1a4SAndy Whitcroft
167634d99219SJoe Perches			CHK("INVALID_UTF8",
1677000d1cc1SJoe Perches			    "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
167800df344fSAndy Whitcroft		}
16790a920b5bSAndy Whitcroft
168015662b3eSJoe Perches# Check if it's the start of a commit log
168115662b3eSJoe Perches# (not a header line and we haven't seen the patch filename)
168215662b3eSJoe Perches		if ($in_header_lines && $realfile =~ /^$/ &&
1683270c49a0SJoe Perches		    $rawline !~ /^(commit\b|from\b|[\w-]+:).+$/i) {
168415662b3eSJoe Perches			$in_header_lines = 0;
168515662b3eSJoe Perches			$in_commit_log = 1;
168615662b3eSJoe Perches		}
168715662b3eSJoe Perches
1688fa64205dSPasi Savanainen# Check if there is UTF-8 in a commit log when a mail header has explicitly
1689fa64205dSPasi Savanainen# declined it, i.e defined some charset where it is missing.
1690fa64205dSPasi Savanainen		if ($in_header_lines &&
1691fa64205dSPasi Savanainen		    $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
1692fa64205dSPasi Savanainen		    $1 !~ /utf-8/i) {
1693fa64205dSPasi Savanainen			$non_utf8_charset = 1;
1694fa64205dSPasi Savanainen		}
1695fa64205dSPasi Savanainen
1696fa64205dSPasi Savanainen		if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
169715662b3eSJoe Perches		    $rawline =~ /$NON_ASCII_UTF8/) {
1698fa64205dSPasi Savanainen			WARN("UTF8_BEFORE_PATCH",
169915662b3eSJoe Perches			    "8-bit UTF-8 used in possible commit log\n" . $herecurr);
170015662b3eSJoe Perches		}
170115662b3eSJoe Perches
170230670854SAndy Whitcroft# ignore non-hunk lines and lines being removed
170330670854SAndy Whitcroft		next if (!$hunk_line || $line =~ /^-/);
170400df344fSAndy Whitcroft
17050a920b5bSAndy Whitcroft#trailing whitespace
17069c0ca6f9SAndy Whitcroft		if ($line =~ /^\+.*\015/) {
1707c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1708000d1cc1SJoe Perches			ERROR("DOS_LINE_ENDINGS",
1709000d1cc1SJoe Perches			      "DOS line endings\n" . $herevet);
17109c0ca6f9SAndy Whitcroft
1711c2fdda0dSAndy Whitcroft		} elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
1712c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1713000d1cc1SJoe Perches			ERROR("TRAILING_WHITESPACE",
1714000d1cc1SJoe Perches			      "trailing whitespace\n" . $herevet);
1715d2c0a235SAndy Whitcroft			$rpt_cleaners = 1;
17160a920b5bSAndy Whitcroft		}
17175368df20SAndy Whitcroft
17183354957aSAndi Kleen# check for Kconfig help text having a real description
17199fe287d7SAndy Whitcroft# Only applies when adding the entry originally, after that we do not have
17209fe287d7SAndy Whitcroft# sufficient context to determine whether it is indeed long enough.
17213354957aSAndi Kleen		if ($realfile =~ /Kconfig/ &&
1722a1385803SAndy Whitcroft		    $line =~ /.\s*config\s+/) {
17233354957aSAndi Kleen			my $length = 0;
17249fe287d7SAndy Whitcroft			my $cnt = $realcnt;
17259fe287d7SAndy Whitcroft			my $ln = $linenr + 1;
17269fe287d7SAndy Whitcroft			my $f;
1727a1385803SAndy Whitcroft			my $is_start = 0;
17289fe287d7SAndy Whitcroft			my $is_end = 0;
1729a1385803SAndy Whitcroft			for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
17309fe287d7SAndy Whitcroft				$f = $lines[$ln - 1];
17319fe287d7SAndy Whitcroft				$cnt-- if ($lines[$ln - 1] !~ /^-/);
17329fe287d7SAndy Whitcroft				$is_end = $lines[$ln - 1] =~ /^\+/;
17339fe287d7SAndy Whitcroft
17349fe287d7SAndy Whitcroft				next if ($f =~ /^-/);
1735a1385803SAndy Whitcroft
1736a1385803SAndy Whitcroft				if ($lines[$ln - 1] =~ /.\s*(?:bool|tristate)\s*\"/) {
1737a1385803SAndy Whitcroft					$is_start = 1;
1738a1385803SAndy Whitcroft				} elsif ($lines[$ln - 1] =~ /.\s*(?:---)?help(?:---)?$/) {
1739a1385803SAndy Whitcroft					$length = -1;
1740a1385803SAndy Whitcroft				}
1741a1385803SAndy Whitcroft
17429fe287d7SAndy Whitcroft				$f =~ s/^.//;
17433354957aSAndi Kleen				$f =~ s/#.*//;
17443354957aSAndi Kleen				$f =~ s/^\s+//;
17453354957aSAndi Kleen				next if ($f =~ /^$/);
17469fe287d7SAndy Whitcroft				if ($f =~ /^\s*config\s/) {
17479fe287d7SAndy Whitcroft					$is_end = 1;
17489fe287d7SAndy Whitcroft					last;
17499fe287d7SAndy Whitcroft				}
17503354957aSAndi Kleen				$length++;
17513354957aSAndi Kleen			}
1752000d1cc1SJoe Perches			WARN("CONFIG_DESCRIPTION",
1753a1385803SAndy Whitcroft			     "please write a paragraph that describes the config symbol fully\n" . $herecurr) if ($is_start && $is_end && $length < 4);
1754a1385803SAndy Whitcroft			#print "is_start<$is_start> is_end<$is_end> length<$length>\n";
17553354957aSAndi Kleen		}
17563354957aSAndi Kleen
17571ba8dfd1SKees Cook# discourage the addition of CONFIG_EXPERIMENTAL in Kconfig.
17581ba8dfd1SKees Cook		if ($realfile =~ /Kconfig/ &&
17591ba8dfd1SKees Cook		    $line =~ /.\s*depends on\s+.*\bEXPERIMENTAL\b/) {
17601ba8dfd1SKees Cook			WARN("CONFIG_EXPERIMENTAL",
17611ba8dfd1SKees Cook			     "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
17621ba8dfd1SKees Cook		}
17631ba8dfd1SKees Cook
1764c68e5878SArnaud Lacombe		if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
1765c68e5878SArnaud Lacombe		    ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
1766c68e5878SArnaud Lacombe			my $flag = $1;
1767c68e5878SArnaud Lacombe			my $replacement = {
1768c68e5878SArnaud Lacombe				'EXTRA_AFLAGS' =>   'asflags-y',
1769c68e5878SArnaud Lacombe				'EXTRA_CFLAGS' =>   'ccflags-y',
1770c68e5878SArnaud Lacombe				'EXTRA_CPPFLAGS' => 'cppflags-y',
1771c68e5878SArnaud Lacombe				'EXTRA_LDFLAGS' =>  'ldflags-y',
1772c68e5878SArnaud Lacombe			};
1773c68e5878SArnaud Lacombe
1774c68e5878SArnaud Lacombe			WARN("DEPRECATED_VARIABLE",
1775c68e5878SArnaud Lacombe			     "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
1776c68e5878SArnaud Lacombe		}
1777c68e5878SArnaud Lacombe
17785368df20SAndy Whitcroft# check we are in a valid source file if not then ignore this hunk
17795368df20SAndy Whitcroft		next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
17805368df20SAndy Whitcroft
17816cd7f386SJoe Perches#line length limit
1782c45dcabdSAndy Whitcroft		if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ &&
1783f4c014c0SAndy Whitcroft		    $rawline !~ /^.\s*\*\s*\@$Ident\s/ &&
17840fccc622SJoe Perches		    !($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(KERN_\S+\s*|[^"]*))?"[X\t]*"\s*(?:|,|\)\s*;)\s*$/ ||
17858bbea968SJoe Perches		    $line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) &&
17866cd7f386SJoe Perches		    $length > $max_line_length)
1787c45dcabdSAndy Whitcroft		{
1788000d1cc1SJoe Perches			WARN("LONG_LINE",
17896cd7f386SJoe Perches			     "line over $max_line_length characters\n" . $herecurr);
17900a920b5bSAndy Whitcroft		}
17910a920b5bSAndy Whitcroft
1792ca56dc09SJosh Triplett# Check for user-visible strings broken across lines, which breaks the ability
1793ca56dc09SJosh Triplett# to grep for the string.  Limited to strings used as parameters (those
1794ca56dc09SJosh Triplett# following an open parenthesis), which almost completely eliminates false
1795ca56dc09SJosh Triplett# positives, as well as warning only once per parameter rather than once per
1796ca56dc09SJosh Triplett# line of the string.  Make an exception when the previous string ends in a
1797ca56dc09SJosh Triplett# newline (multiple lines in one string constant) or \n\t (common in inline
1798ca56dc09SJosh Triplett# assembly to indent the instruction on the following line).
1799ca56dc09SJosh Triplett		if ($line =~ /^\+\s*"/ &&
1800ca56dc09SJosh Triplett		    $prevline =~ /"\s*$/ &&
1801ca56dc09SJosh Triplett		    $prevline =~ /\(/ &&
1802ca56dc09SJosh Triplett		    $prevrawline !~ /\\n(?:\\t)*"\s*$/) {
1803ca56dc09SJosh Triplett			WARN("SPLIT_STRING",
1804ca56dc09SJosh Triplett			     "quoted string split across lines\n" . $hereprev);
1805ca56dc09SJosh Triplett		}
1806ca56dc09SJosh Triplett
18075e79d96eSJoe Perches# check for spaces before a quoted newline
18085e79d96eSJoe Perches		if ($rawline =~ /^.*\".*\s\\n/) {
1809000d1cc1SJoe Perches			WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
1810000d1cc1SJoe Perches			     "unnecessary whitespace before a quoted newline\n" . $herecurr);
18115e79d96eSJoe Perches		}
18125e79d96eSJoe Perches
18138905a67cSAndy Whitcroft# check for adding lines without a newline.
18148905a67cSAndy Whitcroft		if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
1815000d1cc1SJoe Perches			WARN("MISSING_EOF_NEWLINE",
1816000d1cc1SJoe Perches			     "adding a line without newline at end of file\n" . $herecurr);
18178905a67cSAndy Whitcroft		}
18188905a67cSAndy Whitcroft
181942e41c54SMike Frysinger# Blackfin: use hi/lo macros
182042e41c54SMike Frysinger		if ($realfile =~ m@arch/blackfin/.*\.S$@) {
182142e41c54SMike Frysinger			if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
182242e41c54SMike Frysinger				my $herevet = "$here\n" . cat_vet($line) . "\n";
1823000d1cc1SJoe Perches				ERROR("LO_MACRO",
1824000d1cc1SJoe Perches				      "use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
182542e41c54SMike Frysinger			}
182642e41c54SMike Frysinger			if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
182742e41c54SMike Frysinger				my $herevet = "$here\n" . cat_vet($line) . "\n";
1828000d1cc1SJoe Perches				ERROR("HI_MACRO",
1829000d1cc1SJoe Perches				      "use the HI() macro, not (... >> 16)\n" . $herevet);
183042e41c54SMike Frysinger			}
183142e41c54SMike Frysinger		}
183242e41c54SMike Frysinger
1833b9ea10d6SAndy Whitcroft# check we are in a valid source file C or perl if not then ignore this hunk
1834b9ea10d6SAndy Whitcroft		next if ($realfile !~ /\.(h|c|pl)$/);
18350a920b5bSAndy Whitcroft
18360a920b5bSAndy Whitcroft# at the beginning of a line any tabs must come first and anything
18370a920b5bSAndy Whitcroft# more than 8 must use tabs.
1838c2fdda0dSAndy Whitcroft		if ($rawline =~ /^\+\s* \t\s*\S/ ||
1839c2fdda0dSAndy Whitcroft		    $rawline =~ /^\+\s*        \s*/) {
1840c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1841000d1cc1SJoe Perches			ERROR("CODE_INDENT",
1842000d1cc1SJoe Perches			      "code indent should use tabs where possible\n" . $herevet);
1843d2c0a235SAndy Whitcroft			$rpt_cleaners = 1;
18440a920b5bSAndy Whitcroft		}
18450a920b5bSAndy Whitcroft
184608e44365SAlberto Panizzo# check for space before tabs.
184708e44365SAlberto Panizzo		if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
184808e44365SAlberto Panizzo			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1849000d1cc1SJoe Perches			WARN("SPACE_BEFORE_TAB",
1850000d1cc1SJoe Perches			     "please, no space before tabs\n" . $herevet);
185108e44365SAlberto Panizzo		}
185208e44365SAlberto Panizzo
1853d1fe9c09SJoe Perches# check for && or || at the start of a line
1854d1fe9c09SJoe Perches		if ($rawline =~ /^\+\s*(&&|\|\|)/) {
1855d1fe9c09SJoe Perches			CHK("LOGICAL_CONTINUATIONS",
1856d1fe9c09SJoe Perches			    "Logical continuations should be on the previous line\n" . $hereprev);
1857d1fe9c09SJoe Perches		}
1858d1fe9c09SJoe Perches
1859d1fe9c09SJoe Perches# check multi-line statement indentation matches previous line
1860d1fe9c09SJoe Perches		if ($^V && $^V ge 5.10.0 &&
1861d1fe9c09SJoe Perches		    $prevline =~ /^\+(\t*)(if \(|$Ident\().*(\&\&|\|\||,)\s*$/) {
1862d1fe9c09SJoe Perches			$prevline =~ /^\+(\t*)(.*)$/;
1863d1fe9c09SJoe Perches			my $oldindent = $1;
1864d1fe9c09SJoe Perches			my $rest = $2;
1865d1fe9c09SJoe Perches
1866d1fe9c09SJoe Perches			my $pos = pos_last_openparen($rest);
1867d1fe9c09SJoe Perches			if ($pos >= 0) {
1868b34a26f3SJoe Perches				$line =~ /^(\+| )([ \t]*)/;
1869b34a26f3SJoe Perches				my $newindent = $2;
1870d1fe9c09SJoe Perches
1871d1fe9c09SJoe Perches				my $goodtabindent = $oldindent .
1872d1fe9c09SJoe Perches					"\t" x ($pos / 8) .
1873d1fe9c09SJoe Perches					" "  x ($pos % 8);
1874d1fe9c09SJoe Perches				my $goodspaceindent = $oldindent . " "  x $pos;
1875d1fe9c09SJoe Perches
1876d1fe9c09SJoe Perches				if ($newindent ne $goodtabindent &&
1877d1fe9c09SJoe Perches				    $newindent ne $goodspaceindent) {
1878d1fe9c09SJoe Perches					CHK("PARENTHESIS_ALIGNMENT",
1879d1fe9c09SJoe Perches					    "Alignment should match open parenthesis\n" . $hereprev);
1880d1fe9c09SJoe Perches				}
1881d1fe9c09SJoe Perches			}
1882d1fe9c09SJoe Perches		}
1883d1fe9c09SJoe Perches
1884aad4f614SJoe Perches		if ($line =~ /^\+.*\*[ \t]*\)[ \t]+/) {
1885aad4f614SJoe Perches			CHK("SPACING",
1886aad4f614SJoe Perches			    "No space is necessary after a cast\n" . $hereprev);
1887aad4f614SJoe Perches		}
1888aad4f614SJoe Perches
188905880600SJoe Perches		if ($realfile =~ m@^(drivers/net/|net/)@ &&
189005880600SJoe Perches		    $rawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
189105880600SJoe Perches		    $prevrawline =~ /^\+[ \t]*$/) {
189205880600SJoe Perches			WARN("NETWORKING_BLOCK_COMMENT_STYLE",
189305880600SJoe Perches			     "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
189405880600SJoe Perches		}
189505880600SJoe Perches
189605880600SJoe Perches		if ($realfile =~ m@^(drivers/net/|net/)@ &&
1897c24f9f19SJoe Perches		    $rawline !~ m@^\+[ \t]*\*/[ \t]*$@ &&	#trailing */
1898c24f9f19SJoe Perches		    $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ &&	#inline /*...*/
1899c24f9f19SJoe Perches		    $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ &&	#trailing **/
1900c24f9f19SJoe Perches		    $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) {	#non blank */
190105880600SJoe Perches			WARN("NETWORKING_BLOCK_COMMENT_STYLE",
190205880600SJoe Perches			     "networking block comments put the trailing */ on a separate line\n" . $herecurr);
190305880600SJoe Perches		}
190405880600SJoe Perches
19055f7ddae6SRaffaele Recalcati# check for spaces at the beginning of a line.
19066b4c5bebSAndy Whitcroft# Exceptions:
19076b4c5bebSAndy Whitcroft#  1) within comments
19086b4c5bebSAndy Whitcroft#  2) indented preprocessor commands
19096b4c5bebSAndy Whitcroft#  3) hanging labels
19106b4c5bebSAndy Whitcroft		if ($rawline =~ /^\+ / && $line !~ /\+ *(?:$;|#|$Ident:)/)  {
19115f7ddae6SRaffaele Recalcati			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1912000d1cc1SJoe Perches			WARN("LEADING_SPACE",
1913000d1cc1SJoe Perches			     "please, no spaces at the start of a line\n" . $herevet);
19145f7ddae6SRaffaele Recalcati		}
19155f7ddae6SRaffaele Recalcati
1916b9ea10d6SAndy Whitcroft# check we are in a valid C source file if not then ignore this hunk
1917b9ea10d6SAndy Whitcroft		next if ($realfile !~ /\.(h|c)$/);
1918b9ea10d6SAndy Whitcroft
19191ba8dfd1SKees Cook# discourage the addition of CONFIG_EXPERIMENTAL in #if(def).
19201ba8dfd1SKees Cook		if ($line =~ /^\+\s*\#\s*if.*\bCONFIG_EXPERIMENTAL\b/) {
19211ba8dfd1SKees Cook			WARN("CONFIG_EXPERIMENTAL",
19221ba8dfd1SKees Cook			     "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
19231ba8dfd1SKees Cook		}
19241ba8dfd1SKees Cook
1925c2fdda0dSAndy Whitcroft# check for RCS/CVS revision markers
1926cf655043SAndy Whitcroft		if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
1927000d1cc1SJoe Perches			WARN("CVS_KEYWORD",
1928000d1cc1SJoe Perches			     "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
1929c2fdda0dSAndy Whitcroft		}
193022f2a2efSAndy Whitcroft
193142e41c54SMike Frysinger# Blackfin: don't use __builtin_bfin_[cs]sync
193242e41c54SMike Frysinger		if ($line =~ /__builtin_bfin_csync/) {
193342e41c54SMike Frysinger			my $herevet = "$here\n" . cat_vet($line) . "\n";
1934000d1cc1SJoe Perches			ERROR("CSYNC",
1935000d1cc1SJoe Perches			      "use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
193642e41c54SMike Frysinger		}
193742e41c54SMike Frysinger		if ($line =~ /__builtin_bfin_ssync/) {
193842e41c54SMike Frysinger			my $herevet = "$here\n" . cat_vet($line) . "\n";
1939000d1cc1SJoe Perches			ERROR("SSYNC",
1940000d1cc1SJoe Perches			      "use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
194142e41c54SMike Frysinger		}
194242e41c54SMike Frysinger
194356e77d70SJoe Perches# check for old HOTPLUG __dev<foo> section markings
194456e77d70SJoe Perches		if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
194556e77d70SJoe Perches			WARN("HOTPLUG_SECTION",
194656e77d70SJoe Perches			     "Using $1 is unnecessary\n" . $herecurr);
194756e77d70SJoe Perches		}
194856e77d70SJoe Perches
19499c0ca6f9SAndy Whitcroft# Check for potential 'bare' types
19502b474a1aSAndy Whitcroft		my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
19512b474a1aSAndy Whitcroft		    $realline_next);
19523e469cdcSAndy Whitcroft#print "LINE<$line>\n";
19533e469cdcSAndy Whitcroft		if ($linenr >= $suppress_statement &&
19543e469cdcSAndy Whitcroft		    $realcnt && $line =~ /.\s*\S/) {
1955170d3a22SAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
1956f5fe35ddSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0);
1957171ae1a4SAndy Whitcroft			$stat =~ s/\n./\n /g;
1958171ae1a4SAndy Whitcroft			$cond =~ s/\n./\n /g;
1959171ae1a4SAndy Whitcroft
19603e469cdcSAndy Whitcroft#print "linenr<$linenr> <$stat>\n";
19613e469cdcSAndy Whitcroft			# If this statement has no statement boundaries within
19623e469cdcSAndy Whitcroft			# it there is no point in retrying a statement scan
19633e469cdcSAndy Whitcroft			# until we hit end of it.
19643e469cdcSAndy Whitcroft			my $frag = $stat; $frag =~ s/;+\s*$//;
19653e469cdcSAndy Whitcroft			if ($frag !~ /(?:{|;)/) {
19663e469cdcSAndy Whitcroft#print "skip<$line_nr_next>\n";
19673e469cdcSAndy Whitcroft				$suppress_statement = $line_nr_next;
19683e469cdcSAndy Whitcroft			}
1969f74bd194SAndy Whitcroft
19702b474a1aSAndy Whitcroft			# Find the real next line.
19712b474a1aSAndy Whitcroft			$realline_next = $line_nr_next;
19722b474a1aSAndy Whitcroft			if (defined $realline_next &&
19732b474a1aSAndy Whitcroft			    (!defined $lines[$realline_next - 1] ||
19742b474a1aSAndy Whitcroft			     substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
19752b474a1aSAndy Whitcroft				$realline_next++;
19762b474a1aSAndy Whitcroft			}
19772b474a1aSAndy Whitcroft
1978171ae1a4SAndy Whitcroft			my $s = $stat;
1979171ae1a4SAndy Whitcroft			$s =~ s/{.*$//s;
1980cf655043SAndy Whitcroft
1981c2fdda0dSAndy Whitcroft			# Ignore goto labels.
1982171ae1a4SAndy Whitcroft			if ($s =~ /$Ident:\*$/s) {
1983c2fdda0dSAndy Whitcroft
1984c2fdda0dSAndy Whitcroft			# Ignore functions being called
1985171ae1a4SAndy Whitcroft			} elsif ($s =~ /^.\s*$Ident\s*\(/s) {
1986c2fdda0dSAndy Whitcroft
1987463f2864SAndy Whitcroft			} elsif ($s =~ /^.\s*else\b/s) {
1988463f2864SAndy Whitcroft
1989c45dcabdSAndy Whitcroft			# declarations always start with types
1990d2506586SAndy 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) {
1991c45dcabdSAndy Whitcroft				my $type = $1;
1992c45dcabdSAndy Whitcroft				$type =~ s/\s+/ /g;
1993c45dcabdSAndy Whitcroft				possible($type, "A:" . $s);
1994c45dcabdSAndy Whitcroft
19956c72ffaaSAndy Whitcroft			# definitions in global scope can only start with types
1996a6a84062SAndy Whitcroft			} elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
1997c45dcabdSAndy Whitcroft				possible($1, "B:" . $s);
1998c2fdda0dSAndy Whitcroft			}
19998905a67cSAndy Whitcroft
20006c72ffaaSAndy Whitcroft			# any (foo ... *) is a pointer cast, and foo is a type
200165863862SAndy Whitcroft			while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
2002c45dcabdSAndy Whitcroft				possible($1, "C:" . $s);
20039c0ca6f9SAndy Whitcroft			}
20048905a67cSAndy Whitcroft
20058905a67cSAndy Whitcroft			# Check for any sort of function declaration.
20068905a67cSAndy Whitcroft			# int foo(something bar, other baz);
20078905a67cSAndy Whitcroft			# void (*store_gdt)(x86_descr_ptr *);
2008171ae1a4SAndy Whitcroft			if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
20098905a67cSAndy Whitcroft				my ($name_len) = length($1);
20108905a67cSAndy Whitcroft
2011cf655043SAndy Whitcroft				my $ctx = $s;
2012773647a0SAndy Whitcroft				substr($ctx, 0, $name_len + 1, '');
20138905a67cSAndy Whitcroft				$ctx =~ s/\)[^\)]*$//;
2014cf655043SAndy Whitcroft
20158905a67cSAndy Whitcroft				for my $arg (split(/\s*,\s*/, $ctx)) {
2016c45dcabdSAndy Whitcroft					if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
20178905a67cSAndy Whitcroft
2018c45dcabdSAndy Whitcroft						possible($1, "D:" . $s);
20198905a67cSAndy Whitcroft					}
20208905a67cSAndy Whitcroft				}
20218905a67cSAndy Whitcroft			}
20228905a67cSAndy Whitcroft
20239c0ca6f9SAndy Whitcroft		}
20249c0ca6f9SAndy Whitcroft
202500df344fSAndy Whitcroft#
202600df344fSAndy Whitcroft# Checks which may be anchored in the context.
202700df344fSAndy Whitcroft#
202800df344fSAndy Whitcroft
202900df344fSAndy Whitcroft# Check for switch () and associated case and default
203000df344fSAndy Whitcroft# statements should be at the same indent.
203100df344fSAndy Whitcroft		if ($line=~/\bswitch\s*\(.*\)/) {
203200df344fSAndy Whitcroft			my $err = '';
203300df344fSAndy Whitcroft			my $sep = '';
203400df344fSAndy Whitcroft			my @ctx = ctx_block_outer($linenr, $realcnt);
203500df344fSAndy Whitcroft			shift(@ctx);
203600df344fSAndy Whitcroft			for my $ctx (@ctx) {
203700df344fSAndy Whitcroft				my ($clen, $cindent) = line_stats($ctx);
203800df344fSAndy Whitcroft				if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
203900df344fSAndy Whitcroft							$indent != $cindent) {
204000df344fSAndy Whitcroft					$err .= "$sep$ctx\n";
204100df344fSAndy Whitcroft					$sep = '';
204200df344fSAndy Whitcroft				} else {
204300df344fSAndy Whitcroft					$sep = "[...]\n";
204400df344fSAndy Whitcroft				}
204500df344fSAndy Whitcroft			}
204600df344fSAndy Whitcroft			if ($err ne '') {
2047000d1cc1SJoe Perches				ERROR("SWITCH_CASE_INDENT_LEVEL",
2048000d1cc1SJoe Perches				      "switch and case should be at the same indent\n$hereline$err");
2049de7d4f0eSAndy Whitcroft			}
2050de7d4f0eSAndy Whitcroft		}
2051de7d4f0eSAndy Whitcroft
2052de7d4f0eSAndy Whitcroft# if/while/etc brace do not go on next line, unless defining a do while loop,
2053de7d4f0eSAndy Whitcroft# or if that brace on the next line is for something else
2054c45dcabdSAndy Whitcroft		if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
2055773647a0SAndy Whitcroft			my $pre_ctx = "$1$2";
2056773647a0SAndy Whitcroft
20579c0ca6f9SAndy Whitcroft			my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
20588eef05ddSJoe Perches
20598eef05ddSJoe Perches			if ($line =~ /^\+\t{6,}/) {
20608eef05ddSJoe Perches				WARN("DEEP_INDENTATION",
20618eef05ddSJoe Perches				     "Too many leading tabs - consider code refactoring\n" . $herecurr);
20628eef05ddSJoe Perches			}
20638eef05ddSJoe Perches
2064de7d4f0eSAndy Whitcroft			my $ctx_cnt = $realcnt - $#ctx - 1;
2065de7d4f0eSAndy Whitcroft			my $ctx = join("\n", @ctx);
2066de7d4f0eSAndy Whitcroft
2067548596d5SAndy Whitcroft			my $ctx_ln = $linenr;
2068548596d5SAndy Whitcroft			my $ctx_skip = $realcnt;
2069de7d4f0eSAndy Whitcroft
2070548596d5SAndy Whitcroft			while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
2071548596d5SAndy Whitcroft					defined $lines[$ctx_ln - 1] &&
2072548596d5SAndy Whitcroft					$lines[$ctx_ln - 1] =~ /^-/)) {
2073548596d5SAndy Whitcroft				##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
2074548596d5SAndy Whitcroft				$ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
2075773647a0SAndy Whitcroft				$ctx_ln++;
2076773647a0SAndy Whitcroft			}
2077548596d5SAndy Whitcroft
207853210168SAndy Whitcroft			#print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
207953210168SAndy Whitcroft			#print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
2080773647a0SAndy Whitcroft
2081773647a0SAndy Whitcroft			if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln -1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
2082000d1cc1SJoe Perches				ERROR("OPEN_BRACE",
2083000d1cc1SJoe Perches				      "that open brace { should be on the previous line\n" .
208401464f30SAndy Whitcroft					"$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
208500df344fSAndy Whitcroft			}
2086773647a0SAndy Whitcroft			if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
2087773647a0SAndy Whitcroft			    $ctx =~ /\)\s*\;\s*$/ &&
2088773647a0SAndy Whitcroft			    defined $lines[$ctx_ln - 1])
2089773647a0SAndy Whitcroft			{
20909c0ca6f9SAndy Whitcroft				my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
20919c0ca6f9SAndy Whitcroft				if ($nindent > $indent) {
2092000d1cc1SJoe Perches					WARN("TRAILING_SEMICOLON",
2093000d1cc1SJoe Perches					     "trailing semicolon indicates no statements, indent implies otherwise\n" .
209401464f30SAndy Whitcroft						"$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
20959c0ca6f9SAndy Whitcroft				}
20969c0ca6f9SAndy Whitcroft			}
209700df344fSAndy Whitcroft		}
209800df344fSAndy Whitcroft
20994d001e4dSAndy Whitcroft# Check relative indent for conditionals and blocks.
21004d001e4dSAndy Whitcroft		if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
21013e469cdcSAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
21023e469cdcSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0)
21033e469cdcSAndy Whitcroft					if (!defined $stat);
21044d001e4dSAndy Whitcroft			my ($s, $c) = ($stat, $cond);
21054d001e4dSAndy Whitcroft
21064d001e4dSAndy Whitcroft			substr($s, 0, length($c), '');
21074d001e4dSAndy Whitcroft
21084d001e4dSAndy Whitcroft			# Make sure we remove the line prefixes as we have
21094d001e4dSAndy Whitcroft			# none on the first line, and are going to readd them
21104d001e4dSAndy Whitcroft			# where necessary.
21114d001e4dSAndy Whitcroft			$s =~ s/\n./\n/gs;
21124d001e4dSAndy Whitcroft
21134d001e4dSAndy Whitcroft			# Find out how long the conditional actually is.
21146f779c18SAndy Whitcroft			my @newlines = ($c =~ /\n/gs);
21156f779c18SAndy Whitcroft			my $cond_lines = 1 + $#newlines;
21164d001e4dSAndy Whitcroft
21174d001e4dSAndy Whitcroft			# We want to check the first line inside the block
21184d001e4dSAndy Whitcroft			# starting at the end of the conditional, so remove:
21194d001e4dSAndy Whitcroft			#  1) any blank line termination
21204d001e4dSAndy Whitcroft			#  2) any opening brace { on end of the line
21214d001e4dSAndy Whitcroft			#  3) any do (...) {
21224d001e4dSAndy Whitcroft			my $continuation = 0;
21234d001e4dSAndy Whitcroft			my $check = 0;
21244d001e4dSAndy Whitcroft			$s =~ s/^.*\bdo\b//;
21254d001e4dSAndy Whitcroft			$s =~ s/^\s*{//;
21264d001e4dSAndy Whitcroft			if ($s =~ s/^\s*\\//) {
21274d001e4dSAndy Whitcroft				$continuation = 1;
21284d001e4dSAndy Whitcroft			}
21299bd49efeSAndy Whitcroft			if ($s =~ s/^\s*?\n//) {
21304d001e4dSAndy Whitcroft				$check = 1;
21314d001e4dSAndy Whitcroft				$cond_lines++;
21324d001e4dSAndy Whitcroft			}
21334d001e4dSAndy Whitcroft
21344d001e4dSAndy Whitcroft			# Also ignore a loop construct at the end of a
21354d001e4dSAndy Whitcroft			# preprocessor statement.
21364d001e4dSAndy Whitcroft			if (($prevline =~ /^.\s*#\s*define\s/ ||
21374d001e4dSAndy Whitcroft			    $prevline =~ /\\\s*$/) && $continuation == 0) {
21384d001e4dSAndy Whitcroft				$check = 0;
21394d001e4dSAndy Whitcroft			}
21404d001e4dSAndy Whitcroft
21419bd49efeSAndy Whitcroft			my $cond_ptr = -1;
2142740504c6SAndy Whitcroft			$continuation = 0;
21439bd49efeSAndy Whitcroft			while ($cond_ptr != $cond_lines) {
21449bd49efeSAndy Whitcroft				$cond_ptr = $cond_lines;
21454d001e4dSAndy Whitcroft
2146f16fa28fSAndy Whitcroft				# If we see an #else/#elif then the code
2147f16fa28fSAndy Whitcroft				# is not linear.
2148f16fa28fSAndy Whitcroft				if ($s =~ /^\s*\#\s*(?:else|elif)/) {
2149f16fa28fSAndy Whitcroft					$check = 0;
2150f16fa28fSAndy Whitcroft				}
2151f16fa28fSAndy Whitcroft
21529bd49efeSAndy Whitcroft				# Ignore:
21539bd49efeSAndy Whitcroft				#  1) blank lines, they should be at 0,
21549bd49efeSAndy Whitcroft				#  2) preprocessor lines, and
21559bd49efeSAndy Whitcroft				#  3) labels.
2156740504c6SAndy Whitcroft				if ($continuation ||
2157740504c6SAndy Whitcroft				    $s =~ /^\s*?\n/ ||
21589bd49efeSAndy Whitcroft				    $s =~ /^\s*#\s*?/ ||
21599bd49efeSAndy Whitcroft				    $s =~ /^\s*$Ident\s*:/) {
2160740504c6SAndy Whitcroft					$continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
216130dad6ebSAndy Whitcroft					if ($s =~ s/^.*?\n//) {
21629bd49efeSAndy Whitcroft						$cond_lines++;
21639bd49efeSAndy Whitcroft					}
21644d001e4dSAndy Whitcroft				}
216530dad6ebSAndy Whitcroft			}
21664d001e4dSAndy Whitcroft
21674d001e4dSAndy Whitcroft			my (undef, $sindent) = line_stats("+" . $s);
21684d001e4dSAndy Whitcroft			my $stat_real = raw_line($linenr, $cond_lines);
21694d001e4dSAndy Whitcroft
21704d001e4dSAndy Whitcroft			# Check if either of these lines are modified, else
21714d001e4dSAndy Whitcroft			# this is not this patch's fault.
21724d001e4dSAndy Whitcroft			if (!defined($stat_real) ||
21734d001e4dSAndy Whitcroft			    $stat !~ /^\+/ && $stat_real !~ /^\+/) {
21744d001e4dSAndy Whitcroft				$check = 0;
21754d001e4dSAndy Whitcroft			}
21764d001e4dSAndy Whitcroft			if (defined($stat_real) && $cond_lines > 1) {
21774d001e4dSAndy Whitcroft				$stat_real = "[...]\n$stat_real";
21784d001e4dSAndy Whitcroft			}
21794d001e4dSAndy Whitcroft
21809bd49efeSAndy 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";
21814d001e4dSAndy Whitcroft
21824d001e4dSAndy Whitcroft			if ($check && (($sindent % 8) != 0 ||
21834d001e4dSAndy Whitcroft			    ($sindent <= $indent && $s ne ''))) {
2184000d1cc1SJoe Perches				WARN("SUSPECT_CODE_INDENT",
2185000d1cc1SJoe Perches				     "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
21864d001e4dSAndy Whitcroft			}
21874d001e4dSAndy Whitcroft		}
21884d001e4dSAndy Whitcroft
21896c72ffaaSAndy Whitcroft		# Track the 'values' across context and added lines.
21906c72ffaaSAndy Whitcroft		my $opline = $line; $opline =~ s/^./ /;
21911f65f947SAndy Whitcroft		my ($curr_values, $curr_vars) =
21921f65f947SAndy Whitcroft				annotate_values($opline . "\n", $prev_values);
21936c72ffaaSAndy Whitcroft		$curr_values = $prev_values . $curr_values;
2194c2fdda0dSAndy Whitcroft		if ($dbg_values) {
2195c2fdda0dSAndy Whitcroft			my $outline = $opline; $outline =~ s/\t/ /g;
2196cf655043SAndy Whitcroft			print "$linenr > .$outline\n";
2197cf655043SAndy Whitcroft			print "$linenr > $curr_values\n";
21981f65f947SAndy Whitcroft			print "$linenr >  $curr_vars\n";
2199c2fdda0dSAndy Whitcroft		}
22006c72ffaaSAndy Whitcroft		$prev_values = substr($curr_values, -1);
22016c72ffaaSAndy Whitcroft
220200df344fSAndy Whitcroft#ignore lines not being added
220300df344fSAndy Whitcroft		if ($line=~/^[^\+]/) {next;}
220400df344fSAndy Whitcroft
2205653d4876SAndy Whitcroft# TEST: allow direct testing of the type matcher.
22067429c690SAndy Whitcroft		if ($dbg_type) {
22077429c690SAndy Whitcroft			if ($line =~ /^.\s*$Declare\s*$/) {
2208000d1cc1SJoe Perches				ERROR("TEST_TYPE",
2209000d1cc1SJoe Perches				      "TEST: is type\n" . $herecurr);
22107429c690SAndy Whitcroft			} elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
2211000d1cc1SJoe Perches				ERROR("TEST_NOT_TYPE",
2212000d1cc1SJoe Perches				      "TEST: is not type ($1 is)\n". $herecurr);
22137429c690SAndy Whitcroft			}
2214653d4876SAndy Whitcroft			next;
2215653d4876SAndy Whitcroft		}
2216a1ef277eSAndy Whitcroft# TEST: allow direct testing of the attribute matcher.
2217a1ef277eSAndy Whitcroft		if ($dbg_attr) {
22189360b0e5SAndy Whitcroft			if ($line =~ /^.\s*$Modifier\s*$/) {
2219000d1cc1SJoe Perches				ERROR("TEST_ATTR",
2220000d1cc1SJoe Perches				      "TEST: is attr\n" . $herecurr);
22219360b0e5SAndy Whitcroft			} elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
2222000d1cc1SJoe Perches				ERROR("TEST_NOT_ATTR",
2223000d1cc1SJoe Perches				      "TEST: is not attr ($1 is)\n". $herecurr);
2224a1ef277eSAndy Whitcroft			}
2225a1ef277eSAndy Whitcroft			next;
2226a1ef277eSAndy Whitcroft		}
2227653d4876SAndy Whitcroft
2228f0a594c1SAndy Whitcroft# check for initialisation to aggregates open brace on the next line
222999423c20SAndy Whitcroft		if ($line =~ /^.\s*{/ &&
223099423c20SAndy Whitcroft		    $prevline =~ /(?:^|[^=])=\s*$/) {
2231000d1cc1SJoe Perches			ERROR("OPEN_BRACE",
2232000d1cc1SJoe Perches			      "that open brace { should be on the previous line\n" . $hereprev);
2233f0a594c1SAndy Whitcroft		}
2234f0a594c1SAndy Whitcroft
223500df344fSAndy Whitcroft#
223600df344fSAndy Whitcroft# Checks which are anchored on the added line.
223700df344fSAndy Whitcroft#
223800df344fSAndy Whitcroft
2239653d4876SAndy Whitcroft# check for malformed paths in #include statements (uses RAW line)
2240c45dcabdSAndy Whitcroft		if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
2241653d4876SAndy Whitcroft			my $path = $1;
2242653d4876SAndy Whitcroft			if ($path =~ m{//}) {
2243000d1cc1SJoe Perches				ERROR("MALFORMED_INCLUDE",
2244495e9d84SJoe Perches				      "malformed #include filename\n" . $herecurr);
2245495e9d84SJoe Perches			}
2246495e9d84SJoe Perches			if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
2247495e9d84SJoe Perches				ERROR("UAPI_INCLUDE",
2248495e9d84SJoe Perches				      "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
2249653d4876SAndy Whitcroft			}
2250653d4876SAndy Whitcroft		}
2251653d4876SAndy Whitcroft
225200df344fSAndy Whitcroft# no C99 // comments
225300df344fSAndy Whitcroft		if ($line =~ m{//}) {
2254000d1cc1SJoe Perches			ERROR("C99_COMMENTS",
2255000d1cc1SJoe Perches			      "do not use C99 // comments\n" . $herecurr);
225600df344fSAndy Whitcroft		}
225700df344fSAndy Whitcroft		# Remove C99 comments.
22580a920b5bSAndy Whitcroft		$line =~ s@//.*@@;
22596c72ffaaSAndy Whitcroft		$opline =~ s@//.*@@;
22600a920b5bSAndy Whitcroft
22612b474a1aSAndy Whitcroft# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
22622b474a1aSAndy Whitcroft# the whole statement.
22632b474a1aSAndy Whitcroft#print "APW <$lines[$realline_next - 1]>\n";
22642b474a1aSAndy Whitcroft		if (defined $realline_next &&
22652b474a1aSAndy Whitcroft		    exists $lines[$realline_next - 1] &&
22662b474a1aSAndy Whitcroft		    !defined $suppress_export{$realline_next} &&
22672b474a1aSAndy Whitcroft		    ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
22682b474a1aSAndy Whitcroft		     $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
22693cbf62dfSAndy Whitcroft			# Handle definitions which produce identifiers with
22703cbf62dfSAndy Whitcroft			# a prefix:
22713cbf62dfSAndy Whitcroft			#   XXX(foo);
22723cbf62dfSAndy Whitcroft			#   EXPORT_SYMBOL(something_foo);
2273653d4876SAndy Whitcroft			my $name = $1;
227487a53877SAndy Whitcroft			if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
22753cbf62dfSAndy Whitcroft			    $name =~ /^${Ident}_$2/) {
22763cbf62dfSAndy Whitcroft#print "FOO C name<$name>\n";
22773cbf62dfSAndy Whitcroft				$suppress_export{$realline_next} = 1;
22783cbf62dfSAndy Whitcroft
22793cbf62dfSAndy Whitcroft			} elsif ($stat !~ /(?:
22802b474a1aSAndy Whitcroft				\n.}\s*$|
228148012058SAndy Whitcroft				^.DEFINE_$Ident\(\Q$name\E\)|
228248012058SAndy Whitcroft				^.DECLARE_$Ident\(\Q$name\E\)|
228348012058SAndy Whitcroft				^.LIST_HEAD\(\Q$name\E\)|
22842b474a1aSAndy Whitcroft				^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
22852b474a1aSAndy Whitcroft				\b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
228648012058SAndy Whitcroft			    )/x) {
22872b474a1aSAndy Whitcroft#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
22882b474a1aSAndy Whitcroft				$suppress_export{$realline_next} = 2;
22892b474a1aSAndy Whitcroft			} else {
22902b474a1aSAndy Whitcroft				$suppress_export{$realline_next} = 1;
22910a920b5bSAndy Whitcroft			}
22920a920b5bSAndy Whitcroft		}
22932b474a1aSAndy Whitcroft		if (!defined $suppress_export{$linenr} &&
22942b474a1aSAndy Whitcroft		    $prevline =~ /^.\s*$/ &&
22952b474a1aSAndy Whitcroft		    ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
22962b474a1aSAndy Whitcroft		     $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
22972b474a1aSAndy Whitcroft#print "FOO B <$lines[$linenr - 1]>\n";
22982b474a1aSAndy Whitcroft			$suppress_export{$linenr} = 2;
22992b474a1aSAndy Whitcroft		}
23002b474a1aSAndy Whitcroft		if (defined $suppress_export{$linenr} &&
23012b474a1aSAndy Whitcroft		    $suppress_export{$linenr} == 2) {
2302000d1cc1SJoe Perches			WARN("EXPORT_SYMBOL",
2303000d1cc1SJoe Perches			     "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
23042b474a1aSAndy Whitcroft		}
23050a920b5bSAndy Whitcroft
23065150bda4SJoe Eloff# check for global initialisers.
2307c45dcabdSAndy Whitcroft		if ($line =~ /^.$Type\s*$Ident\s*(?:\s+$Modifier)*\s*=\s*(0|NULL|false)\s*;/) {
2308000d1cc1SJoe Perches			ERROR("GLOBAL_INITIALISERS",
2309000d1cc1SJoe Perches			      "do not initialise globals to 0 or NULL\n" .
2310f0a594c1SAndy Whitcroft				$herecurr);
2311f0a594c1SAndy Whitcroft		}
23120a920b5bSAndy Whitcroft# check for static initialisers.
23132d1bafd7SAndy Whitcroft		if ($line =~ /\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
2314000d1cc1SJoe Perches			ERROR("INITIALISED_STATIC",
2315000d1cc1SJoe Perches			      "do not initialise statics to 0 or NULL\n" .
2316de7d4f0eSAndy Whitcroft				$herecurr);
23170a920b5bSAndy Whitcroft		}
23180a920b5bSAndy Whitcroft
2319cb710ecaSJoe Perches# check for static const char * arrays.
2320cb710ecaSJoe Perches		if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
2321000d1cc1SJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
2322000d1cc1SJoe Perches			     "static const char * array should probably be static const char * const\n" .
2323cb710ecaSJoe Perches				$herecurr);
2324cb710ecaSJoe Perches               }
2325cb710ecaSJoe Perches
2326cb710ecaSJoe Perches# check for static char foo[] = "bar" declarations.
2327cb710ecaSJoe Perches		if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
2328000d1cc1SJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
2329000d1cc1SJoe Perches			     "static char array declaration should probably be static const char\n" .
2330cb710ecaSJoe Perches				$herecurr);
2331cb710ecaSJoe Perches               }
2332cb710ecaSJoe Perches
233393ed0e2dSJoe Perches# check for declarations of struct pci_device_id
233493ed0e2dSJoe Perches		if ($line =~ /\bstruct\s+pci_device_id\s+\w+\s*\[\s*\]\s*\=\s*\{/) {
2335000d1cc1SJoe Perches			WARN("DEFINE_PCI_DEVICE_TABLE",
2336000d1cc1SJoe Perches			     "Use DEFINE_PCI_DEVICE_TABLE for struct pci_device_id\n" . $herecurr);
233793ed0e2dSJoe Perches		}
233893ed0e2dSJoe Perches
2339653d4876SAndy Whitcroft# check for new typedefs, only function parameters and sparse annotations
2340653d4876SAndy Whitcroft# make sense.
2341653d4876SAndy Whitcroft		if ($line =~ /\btypedef\s/ &&
23428054576dSAndy Whitcroft		    $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
2343c45dcabdSAndy Whitcroft		    $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
23448ed22cadSAndy Whitcroft		    $line !~ /\b$typeTypedefs\b/ &&
2345653d4876SAndy Whitcroft		    $line !~ /\b__bitwise(?:__|)\b/) {
2346000d1cc1SJoe Perches			WARN("NEW_TYPEDEFS",
2347000d1cc1SJoe Perches			     "do not add new typedefs\n" . $herecurr);
23480a920b5bSAndy Whitcroft		}
23490a920b5bSAndy Whitcroft
23500a920b5bSAndy Whitcroft# * goes on variable not on type
235165863862SAndy Whitcroft		# (char*[ const])
2352bfcb2cc7SAndy Whitcroft		while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
2353bfcb2cc7SAndy Whitcroft			#print "AA<$1>\n";
2354bfcb2cc7SAndy Whitcroft			my ($from, $to) = ($2, $2);
2355d8aaf121SAndy Whitcroft
235665863862SAndy Whitcroft			# Should start with a space.
235765863862SAndy Whitcroft			$to =~ s/^(\S)/ $1/;
235865863862SAndy Whitcroft			# Should not end with a space.
235965863862SAndy Whitcroft			$to =~ s/\s+$//;
236065863862SAndy Whitcroft			# '*'s should not have spaces between.
2361f9a0b3d1SAndy Whitcroft			while ($to =~ s/\*\s+\*/\*\*/) {
236265863862SAndy Whitcroft			}
2363d8aaf121SAndy Whitcroft
236465863862SAndy Whitcroft			#print "from<$from> to<$to>\n";
236565863862SAndy Whitcroft			if ($from ne $to) {
2366000d1cc1SJoe Perches				ERROR("POINTER_LOCATION",
2367000d1cc1SJoe Perches				      "\"(foo$from)\" should be \"(foo$to)\"\n" .  $herecurr);
236865863862SAndy Whitcroft			}
2369bfcb2cc7SAndy Whitcroft		}
2370bfcb2cc7SAndy Whitcroft		while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
2371bfcb2cc7SAndy Whitcroft			#print "BB<$1>\n";
2372bfcb2cc7SAndy Whitcroft			my ($from, $to, $ident) = ($2, $2, $3);
2373d8aaf121SAndy Whitcroft
237465863862SAndy Whitcroft			# Should start with a space.
237565863862SAndy Whitcroft			$to =~ s/^(\S)/ $1/;
237665863862SAndy Whitcroft			# Should not end with a space.
237765863862SAndy Whitcroft			$to =~ s/\s+$//;
237865863862SAndy Whitcroft			# '*'s should not have spaces between.
2379f9a0b3d1SAndy Whitcroft			while ($to =~ s/\*\s+\*/\*\*/) {
238065863862SAndy Whitcroft			}
238165863862SAndy Whitcroft			# Modifiers should have spaces.
238265863862SAndy Whitcroft			$to =~ s/(\b$Modifier$)/$1 /;
238365863862SAndy Whitcroft
2384667026e7SAndy Whitcroft			#print "from<$from> to<$to> ident<$ident>\n";
2385667026e7SAndy Whitcroft			if ($from ne $to && $ident !~ /^$Modifier$/) {
2386000d1cc1SJoe Perches				ERROR("POINTER_LOCATION",
2387000d1cc1SJoe Perches				      "\"foo${from}bar\" should be \"foo${to}bar\"\n" .  $herecurr);
238865863862SAndy Whitcroft			}
23890a920b5bSAndy Whitcroft		}
23900a920b5bSAndy Whitcroft
23910a920b5bSAndy Whitcroft# # no BUG() or BUG_ON()
23920a920b5bSAndy Whitcroft# 		if ($line =~ /\b(BUG|BUG_ON)\b/) {
23930a920b5bSAndy Whitcroft# 			print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
23940a920b5bSAndy Whitcroft# 			print "$herecurr";
23950a920b5bSAndy Whitcroft# 			$clean = 0;
23960a920b5bSAndy Whitcroft# 		}
23970a920b5bSAndy Whitcroft
23988905a67cSAndy Whitcroft		if ($line =~ /\bLINUX_VERSION_CODE\b/) {
2399000d1cc1SJoe Perches			WARN("LINUX_VERSION_CODE",
2400000d1cc1SJoe Perches			     "LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
24018905a67cSAndy Whitcroft		}
24028905a67cSAndy Whitcroft
240317441227SJoe Perches# check for uses of printk_ratelimit
240417441227SJoe Perches		if ($line =~ /\bprintk_ratelimit\s*\(/) {
2405000d1cc1SJoe Perches			WARN("PRINTK_RATELIMITED",
2406000d1cc1SJoe Perches"Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
240717441227SJoe Perches		}
240817441227SJoe Perches
240900df344fSAndy Whitcroft# printk should use KERN_* levels.  Note that follow on printk's on the
241000df344fSAndy Whitcroft# same line do not need a level, so we use the current block context
241100df344fSAndy Whitcroft# to try and find and validate the current printk.  In summary the current
241225985edcSLucas De Marchi# printk includes all preceding printk's which have no newline on the end.
241300df344fSAndy Whitcroft# we assume the first bad printk is the one to report.
2414f0a594c1SAndy Whitcroft		if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
241500df344fSAndy Whitcroft			my $ok = 0;
241600df344fSAndy Whitcroft			for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
241700df344fSAndy Whitcroft				#print "CHECK<$lines[$ln - 1]\n";
241825985edcSLucas De Marchi				# we have a preceding printk if it ends
241900df344fSAndy Whitcroft				# with "\n" ignore it, else it is to blame
242000df344fSAndy Whitcroft				if ($lines[$ln - 1] =~ m{\bprintk\(}) {
242100df344fSAndy Whitcroft					if ($rawlines[$ln - 1] !~ m{\\n"}) {
242200df344fSAndy Whitcroft						$ok = 1;
242300df344fSAndy Whitcroft					}
242400df344fSAndy Whitcroft					last;
242500df344fSAndy Whitcroft				}
242600df344fSAndy Whitcroft			}
242700df344fSAndy Whitcroft			if ($ok == 0) {
2428000d1cc1SJoe Perches				WARN("PRINTK_WITHOUT_KERN_LEVEL",
2429000d1cc1SJoe Perches				     "printk() should include KERN_ facility level\n" . $herecurr);
24300a920b5bSAndy Whitcroft			}
243100df344fSAndy Whitcroft		}
24320a920b5bSAndy Whitcroft
2433243f3803SJoe Perches		if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
2434243f3803SJoe Perches			my $orig = $1;
2435243f3803SJoe Perches			my $level = lc($orig);
2436243f3803SJoe Perches			$level = "warn" if ($level eq "warning");
24378f26b837SJoe Perches			my $level2 = $level;
24388f26b837SJoe Perches			$level2 = "dbg" if ($level eq "debug");
2439243f3803SJoe Perches			WARN("PREFER_PR_LEVEL",
24408f26b837SJoe Perches			     "Prefer netdev_$level2(netdev, ... then dev_$level2(dev, ... then pr_$level(...  to printk(KERN_$orig ...\n" . $herecurr);
2441243f3803SJoe Perches		}
2442243f3803SJoe Perches
2443243f3803SJoe Perches		if ($line =~ /\bpr_warning\s*\(/) {
2444243f3803SJoe Perches			WARN("PREFER_PR_LEVEL",
2445243f3803SJoe Perches			     "Prefer pr_warn(... to pr_warning(...\n" . $herecurr);
2446243f3803SJoe Perches		}
2447243f3803SJoe Perches
2448dc139313SJoe Perches		if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
2449dc139313SJoe Perches			my $orig = $1;
2450dc139313SJoe Perches			my $level = lc($orig);
2451dc139313SJoe Perches			$level = "warn" if ($level eq "warning");
2452dc139313SJoe Perches			$level = "dbg" if ($level eq "debug");
2453dc139313SJoe Perches			WARN("PREFER_DEV_LEVEL",
2454dc139313SJoe Perches			     "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
2455dc139313SJoe Perches		}
2456dc139313SJoe Perches
2457653d4876SAndy Whitcroft# function brace can't be on same line, except for #defines of do while,
2458653d4876SAndy Whitcroft# or if closed on same line
2459c45dcabdSAndy Whitcroft		if (($line=~/$Type\s*$Ident\(.*\).*\s{/) and
2460c45dcabdSAndy Whitcroft		    !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) {
2461000d1cc1SJoe Perches			ERROR("OPEN_BRACE",
2462000d1cc1SJoe Perches			      "open brace '{' following function declarations go on the next line\n" . $herecurr);
24630a920b5bSAndy Whitcroft		}
2464653d4876SAndy Whitcroft
24658905a67cSAndy Whitcroft# open braces for enum, union and struct go on the same line.
24668905a67cSAndy Whitcroft		if ($line =~ /^.\s*{/ &&
24678905a67cSAndy Whitcroft		    $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
2468000d1cc1SJoe Perches			ERROR("OPEN_BRACE",
2469000d1cc1SJoe Perches			      "open brace '{' following $1 go on the same line\n" . $hereprev);
24708905a67cSAndy Whitcroft		}
24718905a67cSAndy Whitcroft
24720c73b4ebSAndy Whitcroft# missing space after union, struct or enum definition
24730c73b4ebSAndy Whitcroft		if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?(?:\s+$Ident)?[=\{]/) {
2474000d1cc1SJoe Perches		    WARN("SPACING",
2475000d1cc1SJoe Perches			 "missing space after $1 definition\n" . $herecurr);
24760c73b4ebSAndy Whitcroft		}
24770c73b4ebSAndy Whitcroft
24788d31cfceSAndy Whitcroft# check for spacing round square brackets; allowed:
24798d31cfceSAndy Whitcroft#  1. with a type on the left -- int [] a;
2480fe2a7dbcSAndy Whitcroft#  2. at the beginning of a line for slice initialisers -- [0...10] = 5,
2481fe2a7dbcSAndy Whitcroft#  3. inside a curly brace -- = { [0...10] = 5 }
24828d31cfceSAndy Whitcroft		while ($line =~ /(.*?\s)\[/g) {
24838d31cfceSAndy Whitcroft			my ($where, $prefix) = ($-[1], $1);
24848d31cfceSAndy Whitcroft			if ($prefix !~ /$Type\s+$/ &&
2485fe2a7dbcSAndy Whitcroft			    ($where != 0 || $prefix !~ /^.\s+$/) &&
2486daebc534SAndy Whitcroft			    $prefix !~ /[{,]\s+$/) {
2487000d1cc1SJoe Perches				ERROR("BRACKET_SPACE",
2488000d1cc1SJoe Perches				      "space prohibited before open square bracket '['\n" . $herecurr);
24898d31cfceSAndy Whitcroft			}
24908d31cfceSAndy Whitcroft		}
24918d31cfceSAndy Whitcroft
2492f0a594c1SAndy Whitcroft# check for spaces between functions and their parentheses.
24936c72ffaaSAndy Whitcroft		while ($line =~ /($Ident)\s+\(/g) {
2494c2fdda0dSAndy Whitcroft			my $name = $1;
2495773647a0SAndy Whitcroft			my $ctx_before = substr($line, 0, $-[1]);
2496773647a0SAndy Whitcroft			my $ctx = "$ctx_before$name";
2497c2fdda0dSAndy Whitcroft
2498c2fdda0dSAndy Whitcroft			# Ignore those directives where spaces _are_ permitted.
2499773647a0SAndy Whitcroft			if ($name =~ /^(?:
2500773647a0SAndy Whitcroft				if|for|while|switch|return|case|
2501773647a0SAndy Whitcroft				volatile|__volatile__|
2502773647a0SAndy Whitcroft				__attribute__|format|__extension__|
2503773647a0SAndy Whitcroft				asm|__asm__)$/x)
2504773647a0SAndy Whitcroft			{
2505c2fdda0dSAndy Whitcroft
2506c2fdda0dSAndy Whitcroft			# cpp #define statements have non-optional spaces, ie
2507c2fdda0dSAndy Whitcroft			# if there is a space between the name and the open
2508c2fdda0dSAndy Whitcroft			# parenthesis it is simply not a parameter group.
2509c45dcabdSAndy Whitcroft			} elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
2510773647a0SAndy Whitcroft
2511773647a0SAndy Whitcroft			# cpp #elif statement condition may start with a (
2512c45dcabdSAndy Whitcroft			} elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
2513c2fdda0dSAndy Whitcroft
2514c2fdda0dSAndy Whitcroft			# If this whole things ends with a type its most
2515c2fdda0dSAndy Whitcroft			# likely a typedef for a function.
2516773647a0SAndy Whitcroft			} elsif ($ctx =~ /$Type$/) {
2517c2fdda0dSAndy Whitcroft
2518c2fdda0dSAndy Whitcroft			} else {
2519000d1cc1SJoe Perches				WARN("SPACING",
2520000d1cc1SJoe Perches				     "space prohibited between function name and open parenthesis '('\n" . $herecurr);
2521f0a594c1SAndy Whitcroft			}
25226c72ffaaSAndy Whitcroft		}
25239a4cad4eSEric Nelson
25249a4cad4eSEric Nelson# check for whitespace before a non-naked semicolon
25259a4cad4eSEric Nelson		if ($line =~ /^\+.*\S\s+;/) {
25265646bc71SJoe Perches			WARN("SPACING",
25279a4cad4eSEric Nelson			     "space prohibited before semicolon\n" . $herecurr);
25289a4cad4eSEric Nelson		}
25299a4cad4eSEric Nelson
2530653d4876SAndy Whitcroft# Check operator spacing.
25310a920b5bSAndy Whitcroft		if (!($line=~/\#\s*include/)) {
25329c0ca6f9SAndy Whitcroft			my $ops = qr{
25339c0ca6f9SAndy Whitcroft				<<=|>>=|<=|>=|==|!=|
25349c0ca6f9SAndy Whitcroft				\+=|-=|\*=|\/=|%=|\^=|\|=|&=|
25359c0ca6f9SAndy Whitcroft				=>|->|<<|>>|<|>|=|!|~|
25361f65f947SAndy Whitcroft				&&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
25371f65f947SAndy Whitcroft				\?|:
25389c0ca6f9SAndy Whitcroft			}x;
2539cf655043SAndy Whitcroft			my @elements = split(/($ops|;)/, $opline);
254000df344fSAndy Whitcroft			my $off = 0;
25416c72ffaaSAndy Whitcroft
25426c72ffaaSAndy Whitcroft			my $blank = copy_spacing($opline);
25436c72ffaaSAndy Whitcroft
25440a920b5bSAndy Whitcroft			for (my $n = 0; $n < $#elements; $n += 2) {
25454a0df2efSAndy Whitcroft				$off += length($elements[$n]);
25464a0df2efSAndy Whitcroft
254725985edcSLucas De Marchi				# Pick up the preceding and succeeding characters.
2548773647a0SAndy Whitcroft				my $ca = substr($opline, 0, $off);
2549773647a0SAndy Whitcroft				my $cc = '';
2550773647a0SAndy Whitcroft				if (length($opline) >= ($off + length($elements[$n + 1]))) {
2551773647a0SAndy Whitcroft					$cc = substr($opline, $off + length($elements[$n + 1]));
2552773647a0SAndy Whitcroft				}
2553773647a0SAndy Whitcroft				my $cb = "$ca$;$cc";
2554773647a0SAndy Whitcroft
25554a0df2efSAndy Whitcroft				my $a = '';
25564a0df2efSAndy Whitcroft				$a = 'V' if ($elements[$n] ne '');
25574a0df2efSAndy Whitcroft				$a = 'W' if ($elements[$n] =~ /\s$/);
2558cf655043SAndy Whitcroft				$a = 'C' if ($elements[$n] =~ /$;$/);
25594a0df2efSAndy Whitcroft				$a = 'B' if ($elements[$n] =~ /(\[|\()$/);
25604a0df2efSAndy Whitcroft				$a = 'O' if ($elements[$n] eq '');
2561773647a0SAndy Whitcroft				$a = 'E' if ($ca =~ /^\s*$/);
25624a0df2efSAndy Whitcroft
25630a920b5bSAndy Whitcroft				my $op = $elements[$n + 1];
25644a0df2efSAndy Whitcroft
25654a0df2efSAndy Whitcroft				my $c = '';
25660a920b5bSAndy Whitcroft				if (defined $elements[$n + 2]) {
25674a0df2efSAndy Whitcroft					$c = 'V' if ($elements[$n + 2] ne '');
25684a0df2efSAndy Whitcroft					$c = 'W' if ($elements[$n + 2] =~ /^\s/);
2569cf655043SAndy Whitcroft					$c = 'C' if ($elements[$n + 2] =~ /^$;/);
25704a0df2efSAndy Whitcroft					$c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
25714a0df2efSAndy Whitcroft					$c = 'O' if ($elements[$n + 2] eq '');
25728b1b3378SAndy Whitcroft					$c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
25734a0df2efSAndy Whitcroft				} else {
25744a0df2efSAndy Whitcroft					$c = 'E';
25750a920b5bSAndy Whitcroft				}
25760a920b5bSAndy Whitcroft
25774a0df2efSAndy Whitcroft				my $ctx = "${a}x${c}";
25784a0df2efSAndy Whitcroft
25794a0df2efSAndy Whitcroft				my $at = "(ctx:$ctx)";
25804a0df2efSAndy Whitcroft
25816c72ffaaSAndy Whitcroft				my $ptr = substr($blank, 0, $off) . "^";
2582de7d4f0eSAndy Whitcroft				my $hereptr = "$hereline$ptr\n";
25830a920b5bSAndy Whitcroft
258474048ed8SAndy Whitcroft				# Pull out the value of this operator.
25856c72ffaaSAndy Whitcroft				my $op_type = substr($curr_values, $off + 1, 1);
25860a920b5bSAndy Whitcroft
25871f65f947SAndy Whitcroft				# Get the full operator variant.
25881f65f947SAndy Whitcroft				my $opv = $op . substr($curr_vars, $off, 1);
25891f65f947SAndy Whitcroft
259013214adfSAndy Whitcroft				# Ignore operators passed as parameters.
259113214adfSAndy Whitcroft				if ($op_type ne 'V' &&
259213214adfSAndy Whitcroft				    $ca =~ /\s$/ && $cc =~ /^\s*,/) {
259313214adfSAndy Whitcroft
2594cf655043SAndy Whitcroft#				# Ignore comments
2595cf655043SAndy Whitcroft#				} elsif ($op =~ /^$;+$/) {
259613214adfSAndy Whitcroft
2597d8aaf121SAndy Whitcroft				# ; should have either the end of line or a space or \ after it
259813214adfSAndy Whitcroft				} elsif ($op eq ';') {
2599cf655043SAndy Whitcroft					if ($ctx !~ /.x[WEBC]/ &&
2600cf655043SAndy Whitcroft					    $cc !~ /^\\/ && $cc !~ /^;/) {
2601000d1cc1SJoe Perches						ERROR("SPACING",
2602000d1cc1SJoe Perches						      "space required after that '$op' $at\n" . $hereptr);
2603d8aaf121SAndy Whitcroft					}
2604d8aaf121SAndy Whitcroft
2605d8aaf121SAndy Whitcroft				# // is a comment
2606d8aaf121SAndy Whitcroft				} elsif ($op eq '//') {
26070a920b5bSAndy Whitcroft
26081f65f947SAndy Whitcroft				# No spaces for:
26091f65f947SAndy Whitcroft				#   ->
26101f65f947SAndy Whitcroft				#   :   when part of a bitfield
26111f65f947SAndy Whitcroft				} elsif ($op eq '->' || $opv eq ':B') {
26124a0df2efSAndy Whitcroft					if ($ctx =~ /Wx.|.xW/) {
2613000d1cc1SJoe Perches						ERROR("SPACING",
2614000d1cc1SJoe Perches						      "spaces prohibited around that '$op' $at\n" . $hereptr);
26150a920b5bSAndy Whitcroft					}
26160a920b5bSAndy Whitcroft
26170a920b5bSAndy Whitcroft				# , must have a space on the right.
26180a920b5bSAndy Whitcroft				} elsif ($op eq ',') {
2619cf655043SAndy Whitcroft					if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
2620000d1cc1SJoe Perches						ERROR("SPACING",
2621000d1cc1SJoe Perches						      "space required after that '$op' $at\n" . $hereptr);
26220a920b5bSAndy Whitcroft					}
26230a920b5bSAndy Whitcroft
26249c0ca6f9SAndy Whitcroft				# '*' as part of a type definition -- reported already.
262574048ed8SAndy Whitcroft				} elsif ($opv eq '*_') {
26269c0ca6f9SAndy Whitcroft					#warn "'*' is part of type\n";
26279c0ca6f9SAndy Whitcroft
26289c0ca6f9SAndy Whitcroft				# unary operators should have a space before and
26299c0ca6f9SAndy Whitcroft				# none after.  May be left adjacent to another
26309c0ca6f9SAndy Whitcroft				# unary operator, or a cast
26319c0ca6f9SAndy Whitcroft				} elsif ($op eq '!' || $op eq '~' ||
263274048ed8SAndy Whitcroft					 $opv eq '*U' || $opv eq '-U' ||
26330d413866SAndy Whitcroft					 $opv eq '&U' || $opv eq '&&U') {
2634cf655043SAndy Whitcroft					if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
2635000d1cc1SJoe Perches						ERROR("SPACING",
2636000d1cc1SJoe Perches						      "space required before that '$op' $at\n" . $hereptr);
26370a920b5bSAndy Whitcroft					}
2638a3340b35SAndy Whitcroft					if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
2639171ae1a4SAndy Whitcroft						# A unary '*' may be const
2640171ae1a4SAndy Whitcroft
2641171ae1a4SAndy Whitcroft					} elsif ($ctx =~ /.xW/) {
2642000d1cc1SJoe Perches						ERROR("SPACING",
2643000d1cc1SJoe Perches						      "space prohibited after that '$op' $at\n" . $hereptr);
26440a920b5bSAndy Whitcroft					}
26450a920b5bSAndy Whitcroft
26460a920b5bSAndy Whitcroft				# unary ++ and unary -- are allowed no space on one side.
26470a920b5bSAndy Whitcroft				} elsif ($op eq '++' or $op eq '--') {
2648773647a0SAndy Whitcroft					if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
2649000d1cc1SJoe Perches						ERROR("SPACING",
2650000d1cc1SJoe Perches						      "space required one side of that '$op' $at\n" . $hereptr);
26510a920b5bSAndy Whitcroft					}
2652773647a0SAndy Whitcroft					if ($ctx =~ /Wx[BE]/ ||
2653773647a0SAndy Whitcroft					    ($ctx =~ /Wx./ && $cc =~ /^;/)) {
2654000d1cc1SJoe Perches						ERROR("SPACING",
2655000d1cc1SJoe Perches						      "space prohibited before that '$op' $at\n" . $hereptr);
2656653d4876SAndy Whitcroft					}
2657773647a0SAndy Whitcroft					if ($ctx =~ /ExW/) {
2658000d1cc1SJoe Perches						ERROR("SPACING",
2659000d1cc1SJoe Perches						      "space prohibited after that '$op' $at\n" . $hereptr);
2660773647a0SAndy Whitcroft					}
2661773647a0SAndy Whitcroft
26620a920b5bSAndy Whitcroft
26630a920b5bSAndy Whitcroft				# << and >> may either have or not have spaces both sides
26649c0ca6f9SAndy Whitcroft				} elsif ($op eq '<<' or $op eq '>>' or
26659c0ca6f9SAndy Whitcroft					 $op eq '&' or $op eq '^' or $op eq '|' or
26669c0ca6f9SAndy Whitcroft					 $op eq '+' or $op eq '-' or
2667c2fdda0dSAndy Whitcroft					 $op eq '*' or $op eq '/' or
2668c2fdda0dSAndy Whitcroft					 $op eq '%')
26690a920b5bSAndy Whitcroft				{
2670773647a0SAndy Whitcroft					if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
2671000d1cc1SJoe Perches						ERROR("SPACING",
2672000d1cc1SJoe Perches						      "need consistent spacing around '$op' $at\n" .
2673de7d4f0eSAndy Whitcroft							$hereptr);
26740a920b5bSAndy Whitcroft					}
26750a920b5bSAndy Whitcroft
26761f65f947SAndy Whitcroft				# A colon needs no spaces before when it is
26771f65f947SAndy Whitcroft				# terminating a case value or a label.
26781f65f947SAndy Whitcroft				} elsif ($opv eq ':C' || $opv eq ':L') {
26791f65f947SAndy Whitcroft					if ($ctx =~ /Wx./) {
2680000d1cc1SJoe Perches						ERROR("SPACING",
2681000d1cc1SJoe Perches						      "space prohibited before that '$op' $at\n" . $hereptr);
26821f65f947SAndy Whitcroft					}
26831f65f947SAndy Whitcroft
26840a920b5bSAndy Whitcroft				# All the others need spaces both sides.
2685cf655043SAndy Whitcroft				} elsif ($ctx !~ /[EWC]x[CWE]/) {
26861f65f947SAndy Whitcroft					my $ok = 0;
26871f65f947SAndy Whitcroft
268822f2a2efSAndy Whitcroft					# Ignore email addresses <foo@bar>
26891f65f947SAndy Whitcroft					if (($op eq '<' &&
26901f65f947SAndy Whitcroft					     $cc =~ /^\S+\@\S+>/) ||
26911f65f947SAndy Whitcroft					    ($op eq '>' &&
26921f65f947SAndy Whitcroft					     $ca =~ /<\S+\@\S+$/))
26931f65f947SAndy Whitcroft					{
26941f65f947SAndy Whitcroft					    	$ok = 1;
26951f65f947SAndy Whitcroft					}
26961f65f947SAndy Whitcroft
26971f65f947SAndy Whitcroft					# Ignore ?:
26981f65f947SAndy Whitcroft					if (($opv eq ':O' && $ca =~ /\?$/) ||
26991f65f947SAndy Whitcroft					    ($op eq '?' && $cc =~ /^:/)) {
27001f65f947SAndy Whitcroft					    	$ok = 1;
27011f65f947SAndy Whitcroft					}
27021f65f947SAndy Whitcroft
27031f65f947SAndy Whitcroft					if ($ok == 0) {
2704000d1cc1SJoe Perches						ERROR("SPACING",
2705000d1cc1SJoe Perches						      "spaces required around that '$op' $at\n" . $hereptr);
27060a920b5bSAndy Whitcroft					}
270722f2a2efSAndy Whitcroft				}
27084a0df2efSAndy Whitcroft				$off += length($elements[$n + 1]);
27090a920b5bSAndy Whitcroft			}
27100a920b5bSAndy Whitcroft		}
27110a920b5bSAndy Whitcroft
2712f0a594c1SAndy Whitcroft# check for multiple assignments
2713f0a594c1SAndy Whitcroft		if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
2714000d1cc1SJoe Perches			CHK("MULTIPLE_ASSIGNMENTS",
2715000d1cc1SJoe Perches			    "multiple assignments should be avoided\n" . $herecurr);
2716f0a594c1SAndy Whitcroft		}
2717f0a594c1SAndy Whitcroft
271822f2a2efSAndy Whitcroft## # check for multiple declarations, allowing for a function declaration
271922f2a2efSAndy Whitcroft## # continuation.
272022f2a2efSAndy Whitcroft## 		if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
272122f2a2efSAndy Whitcroft## 		    $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
272222f2a2efSAndy Whitcroft##
272322f2a2efSAndy Whitcroft## 			# Remove any bracketed sections to ensure we do not
272422f2a2efSAndy Whitcroft## 			# falsly report the parameters of functions.
272522f2a2efSAndy Whitcroft## 			my $ln = $line;
272622f2a2efSAndy Whitcroft## 			while ($ln =~ s/\([^\(\)]*\)//g) {
272722f2a2efSAndy Whitcroft## 			}
272822f2a2efSAndy Whitcroft## 			if ($ln =~ /,/) {
2729000d1cc1SJoe Perches## 				WARN("MULTIPLE_DECLARATION",
2730000d1cc1SJoe Perches##				     "declaring multiple variables together should be avoided\n" . $herecurr);
273122f2a2efSAndy Whitcroft## 			}
273222f2a2efSAndy Whitcroft## 		}
2733f0a594c1SAndy Whitcroft
27340a920b5bSAndy Whitcroft#need space before brace following if, while, etc
273522f2a2efSAndy Whitcroft		if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
273622f2a2efSAndy Whitcroft		    $line =~ /do{/) {
2737000d1cc1SJoe Perches			ERROR("SPACING",
2738000d1cc1SJoe Perches			      "space required before the open brace '{'\n" . $herecurr);
2739de7d4f0eSAndy Whitcroft		}
2740de7d4f0eSAndy Whitcroft
2741de7d4f0eSAndy Whitcroft# closing brace should have a space following it when it has anything
2742de7d4f0eSAndy Whitcroft# on the line
2743de7d4f0eSAndy Whitcroft		if ($line =~ /}(?!(?:,|;|\)))\S/) {
2744000d1cc1SJoe Perches			ERROR("SPACING",
2745000d1cc1SJoe Perches			      "space required after that close brace '}'\n" . $herecurr);
27460a920b5bSAndy Whitcroft		}
27470a920b5bSAndy Whitcroft
274822f2a2efSAndy Whitcroft# check spacing on square brackets
274922f2a2efSAndy Whitcroft		if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
2750000d1cc1SJoe Perches			ERROR("SPACING",
2751000d1cc1SJoe Perches			      "space prohibited after that open square bracket '['\n" . $herecurr);
275222f2a2efSAndy Whitcroft		}
275322f2a2efSAndy Whitcroft		if ($line =~ /\s\]/) {
2754000d1cc1SJoe Perches			ERROR("SPACING",
2755000d1cc1SJoe Perches			      "space prohibited before that close square bracket ']'\n" . $herecurr);
275622f2a2efSAndy Whitcroft		}
275722f2a2efSAndy Whitcroft
2758c45dcabdSAndy Whitcroft# check spacing on parentheses
27599c0ca6f9SAndy Whitcroft		if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
27609c0ca6f9SAndy Whitcroft		    $line !~ /for\s*\(\s+;/) {
2761000d1cc1SJoe Perches			ERROR("SPACING",
2762000d1cc1SJoe Perches			      "space prohibited after that open parenthesis '('\n" . $herecurr);
276322f2a2efSAndy Whitcroft		}
276413214adfSAndy Whitcroft		if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
2765c45dcabdSAndy Whitcroft		    $line !~ /for\s*\(.*;\s+\)/ &&
2766c45dcabdSAndy Whitcroft		    $line !~ /:\s+\)/) {
2767000d1cc1SJoe Perches			ERROR("SPACING",
2768000d1cc1SJoe Perches			      "space prohibited before that close parenthesis ')'\n" . $herecurr);
276922f2a2efSAndy Whitcroft		}
277022f2a2efSAndy Whitcroft
27710a920b5bSAndy Whitcroft#goto labels aren't indented, allow a single space however
27724a0df2efSAndy Whitcroft		if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
27730a920b5bSAndy Whitcroft		   !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
2774000d1cc1SJoe Perches			WARN("INDENTED_LABEL",
2775000d1cc1SJoe Perches			     "labels should not be indented\n" . $herecurr);
27760a920b5bSAndy Whitcroft		}
27770a920b5bSAndy Whitcroft
2778c45dcabdSAndy Whitcroft# Return is not a function.
2779c45dcabdSAndy Whitcroft		if (defined($stat) && $stat =~ /^.\s*return(\s*)(\(.*);/s) {
2780c45dcabdSAndy Whitcroft			my $spacing = $1;
2781c45dcabdSAndy Whitcroft			my $value = $2;
2782c45dcabdSAndy Whitcroft
278386f9d059SAndy Whitcroft			# Flatten any parentheses
2784fb2d2c1bSAndy Whitcroft			$value =~ s/\(/ \(/g;
2785fb2d2c1bSAndy Whitcroft			$value =~ s/\)/\) /g;
2786e01886adSAndy Whitcroft			while ($value =~ s/\[[^\[\]]*\]/1/ ||
278763f17f89SAndy Whitcroft			       $value !~ /(?:$Ident|-?$Constant)\s*
278863f17f89SAndy Whitcroft					     $Compare\s*
278963f17f89SAndy Whitcroft					     (?:$Ident|-?$Constant)/x &&
279063f17f89SAndy Whitcroft			       $value =~ s/\([^\(\)]*\)/1/) {
2791c45dcabdSAndy Whitcroft			}
2792fb2d2c1bSAndy Whitcroft#print "value<$value>\n";
2793fb2d2c1bSAndy Whitcroft			if ($value =~ /^\s*(?:$Ident|-?$Constant)\s*$/) {
2794000d1cc1SJoe Perches				ERROR("RETURN_PARENTHESES",
2795000d1cc1SJoe Perches				      "return is not a function, parentheses are not required\n" . $herecurr);
2796c45dcabdSAndy Whitcroft
2797c45dcabdSAndy Whitcroft			} elsif ($spacing !~ /\s+/) {
2798000d1cc1SJoe Perches				ERROR("SPACING",
2799000d1cc1SJoe Perches				      "space required before the open parenthesis '('\n" . $herecurr);
2800c45dcabdSAndy Whitcroft			}
2801c45dcabdSAndy Whitcroft		}
280253a3c448SAndy Whitcroft# Return of what appears to be an errno should normally be -'ve
280353a3c448SAndy Whitcroft		if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) {
280453a3c448SAndy Whitcroft			my $name = $1;
280553a3c448SAndy Whitcroft			if ($name ne 'EOF' && $name ne 'ERROR') {
2806000d1cc1SJoe Perches				WARN("USE_NEGATIVE_ERRNO",
2807000d1cc1SJoe Perches				     "return of an errno should typically be -ve (return -$1)\n" . $herecurr);
280853a3c448SAndy Whitcroft			}
280953a3c448SAndy Whitcroft		}
2810c45dcabdSAndy Whitcroft
28110a920b5bSAndy Whitcroft# Need a space before open parenthesis after if, while etc
28124a0df2efSAndy Whitcroft		if ($line=~/\b(if|while|for|switch)\(/) {
2813000d1cc1SJoe Perches			ERROR("SPACING", "space required before the open parenthesis '('\n" . $herecurr);
28140a920b5bSAndy Whitcroft		}
28150a920b5bSAndy Whitcroft
2816f5fe35ddSAndy Whitcroft# Check for illegal assignment in if conditional -- and check for trailing
2817f5fe35ddSAndy Whitcroft# statements after the conditional.
2818170d3a22SAndy Whitcroft		if ($line =~ /do\s*(?!{)/) {
28193e469cdcSAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
28203e469cdcSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0)
28213e469cdcSAndy Whitcroft					if (!defined $stat);
2822170d3a22SAndy Whitcroft			my ($stat_next) = ctx_statement_block($line_nr_next,
2823170d3a22SAndy Whitcroft						$remain_next, $off_next);
2824170d3a22SAndy Whitcroft			$stat_next =~ s/\n./\n /g;
2825170d3a22SAndy Whitcroft			##print "stat<$stat> stat_next<$stat_next>\n";
2826170d3a22SAndy Whitcroft
2827170d3a22SAndy Whitcroft			if ($stat_next =~ /^\s*while\b/) {
2828170d3a22SAndy Whitcroft				# If the statement carries leading newlines,
2829170d3a22SAndy Whitcroft				# then count those as offsets.
2830170d3a22SAndy Whitcroft				my ($whitespace) =
2831170d3a22SAndy Whitcroft					($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
2832170d3a22SAndy Whitcroft				my $offset =
2833170d3a22SAndy Whitcroft					statement_rawlines($whitespace) - 1;
2834170d3a22SAndy Whitcroft
2835170d3a22SAndy Whitcroft				$suppress_whiletrailers{$line_nr_next +
2836170d3a22SAndy Whitcroft								$offset} = 1;
2837170d3a22SAndy Whitcroft			}
2838170d3a22SAndy Whitcroft		}
2839170d3a22SAndy Whitcroft		if (!defined $suppress_whiletrailers{$linenr} &&
2840170d3a22SAndy Whitcroft		    $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
2841171ae1a4SAndy Whitcroft			my ($s, $c) = ($stat, $cond);
28428905a67cSAndy Whitcroft
2843b53c8e10SAndy Whitcroft			if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
2844000d1cc1SJoe Perches				ERROR("ASSIGN_IN_IF",
2845000d1cc1SJoe Perches				      "do not use assignment in if condition\n" . $herecurr);
28468905a67cSAndy Whitcroft			}
28478905a67cSAndy Whitcroft
28488905a67cSAndy Whitcroft			# Find out what is on the end of the line after the
28498905a67cSAndy Whitcroft			# conditional.
2850773647a0SAndy Whitcroft			substr($s, 0, length($c), '');
28518905a67cSAndy Whitcroft			$s =~ s/\n.*//g;
285213214adfSAndy Whitcroft			$s =~ s/$;//g; 	# Remove any comments
285353210168SAndy Whitcroft			if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
285453210168SAndy Whitcroft			    $c !~ /}\s*while\s*/)
2855773647a0SAndy Whitcroft			{
2856bb44ad39SAndy Whitcroft				# Find out how long the conditional actually is.
2857bb44ad39SAndy Whitcroft				my @newlines = ($c =~ /\n/gs);
2858bb44ad39SAndy Whitcroft				my $cond_lines = 1 + $#newlines;
285942bdf74cSHidetoshi Seto				my $stat_real = '';
2860bb44ad39SAndy Whitcroft
286142bdf74cSHidetoshi Seto				$stat_real = raw_line($linenr, $cond_lines)
286242bdf74cSHidetoshi Seto							. "\n" if ($cond_lines);
2863bb44ad39SAndy Whitcroft				if (defined($stat_real) && $cond_lines > 1) {
2864bb44ad39SAndy Whitcroft					$stat_real = "[...]\n$stat_real";
2865bb44ad39SAndy Whitcroft				}
2866bb44ad39SAndy Whitcroft
2867000d1cc1SJoe Perches				ERROR("TRAILING_STATEMENTS",
2868000d1cc1SJoe Perches				      "trailing statements should be on next line\n" . $herecurr . $stat_real);
28698905a67cSAndy Whitcroft			}
28708905a67cSAndy Whitcroft		}
28718905a67cSAndy Whitcroft
287213214adfSAndy Whitcroft# Check for bitwise tests written as boolean
287313214adfSAndy Whitcroft		if ($line =~ /
287413214adfSAndy Whitcroft			(?:
287513214adfSAndy Whitcroft				(?:\[|\(|\&\&|\|\|)
287613214adfSAndy Whitcroft				\s*0[xX][0-9]+\s*
287713214adfSAndy Whitcroft				(?:\&\&|\|\|)
287813214adfSAndy Whitcroft			|
287913214adfSAndy Whitcroft				(?:\&\&|\|\|)
288013214adfSAndy Whitcroft				\s*0[xX][0-9]+\s*
288113214adfSAndy Whitcroft				(?:\&\&|\|\||\)|\])
288213214adfSAndy Whitcroft			)/x)
288313214adfSAndy Whitcroft		{
2884000d1cc1SJoe Perches			WARN("HEXADECIMAL_BOOLEAN_TEST",
2885000d1cc1SJoe Perches			     "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
288613214adfSAndy Whitcroft		}
288713214adfSAndy Whitcroft
28888905a67cSAndy Whitcroft# if and else should not have general statements after it
288913214adfSAndy Whitcroft		if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
289013214adfSAndy Whitcroft			my $s = $1;
289113214adfSAndy Whitcroft			$s =~ s/$;//g; 	# Remove any comments
289213214adfSAndy Whitcroft			if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
2893000d1cc1SJoe Perches				ERROR("TRAILING_STATEMENTS",
2894000d1cc1SJoe Perches				      "trailing statements should be on next line\n" . $herecurr);
28950a920b5bSAndy Whitcroft			}
289613214adfSAndy Whitcroft		}
289739667782SAndy Whitcroft# if should not continue a brace
289839667782SAndy Whitcroft		if ($line =~ /}\s*if\b/) {
2899000d1cc1SJoe Perches			ERROR("TRAILING_STATEMENTS",
2900000d1cc1SJoe Perches			      "trailing statements should be on next line\n" .
290139667782SAndy Whitcroft				$herecurr);
290239667782SAndy Whitcroft		}
2903a1080bf8SAndy Whitcroft# case and default should not have general statements after them
2904a1080bf8SAndy Whitcroft		if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
2905a1080bf8SAndy Whitcroft		    $line !~ /\G(?:
29063fef12d6SAndy Whitcroft			(?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
2907a1080bf8SAndy Whitcroft			\s*return\s+
2908a1080bf8SAndy Whitcroft		    )/xg)
2909a1080bf8SAndy Whitcroft		{
2910000d1cc1SJoe Perches			ERROR("TRAILING_STATEMENTS",
2911000d1cc1SJoe Perches			      "trailing statements should be on next line\n" . $herecurr);
2912a1080bf8SAndy Whitcroft		}
29130a920b5bSAndy Whitcroft
29140a920b5bSAndy Whitcroft		# Check for }<nl>else {, these must be at the same
29150a920b5bSAndy Whitcroft		# indent level to be relevant to each other.
29160a920b5bSAndy Whitcroft		if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
29170a920b5bSAndy Whitcroft						$previndent == $indent) {
2918000d1cc1SJoe Perches			ERROR("ELSE_AFTER_BRACE",
2919000d1cc1SJoe Perches			      "else should follow close brace '}'\n" . $hereprev);
29200a920b5bSAndy Whitcroft		}
29210a920b5bSAndy Whitcroft
2922c2fdda0dSAndy Whitcroft		if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and
2923c2fdda0dSAndy Whitcroft						$previndent == $indent) {
2924c2fdda0dSAndy Whitcroft			my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
2925c2fdda0dSAndy Whitcroft
2926c2fdda0dSAndy Whitcroft			# Find out what is on the end of the line after the
2927c2fdda0dSAndy Whitcroft			# conditional.
2928773647a0SAndy Whitcroft			substr($s, 0, length($c), '');
2929c2fdda0dSAndy Whitcroft			$s =~ s/\n.*//g;
2930c2fdda0dSAndy Whitcroft
2931c2fdda0dSAndy Whitcroft			if ($s =~ /^\s*;/) {
2932000d1cc1SJoe Perches				ERROR("WHILE_AFTER_BRACE",
2933000d1cc1SJoe Perches				      "while should follow close brace '}'\n" . $hereprev);
2934c2fdda0dSAndy Whitcroft			}
2935c2fdda0dSAndy Whitcroft		}
2936c2fdda0dSAndy Whitcroft
2937323c1260SJoe Perches#CamelCase
2938323c1260SJoe Perches		while ($line =~ m{($Constant|$Lval)}g) {
2939323c1260SJoe Perches			my $var = $1;
2940323c1260SJoe Perches			if ($var !~ /$Constant/ &&
2941*be79794bSJoe Perches			    $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
2942be987d9fSJoe Perches			    $var !~ /"^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
2943323c1260SJoe Perches			    !defined $camelcase{$var}) {
2944323c1260SJoe Perches				$camelcase{$var} = 1;
2945*be79794bSJoe Perches				CHK("CAMELCASE",
2946323c1260SJoe Perches				    "Avoid CamelCase: <$var>\n" . $herecurr);
2947323c1260SJoe Perches			}
2948323c1260SJoe Perches		}
29490a920b5bSAndy Whitcroft
29500a920b5bSAndy Whitcroft#no spaces allowed after \ in define
2951c45dcabdSAndy Whitcroft		if ($line=~/\#\s*define.*\\\s$/) {
2952000d1cc1SJoe Perches			WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
2953000d1cc1SJoe Perches			     "Whitepspace after \\ makes next lines useless\n" . $herecurr);
29540a920b5bSAndy Whitcroft		}
29550a920b5bSAndy Whitcroft
2956653d4876SAndy Whitcroft#warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
2957c45dcabdSAndy Whitcroft		if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
2958e09dec48SAndy Whitcroft			my $file = "$1.h";
2959e09dec48SAndy Whitcroft			my $checkfile = "include/linux/$file";
2960e09dec48SAndy Whitcroft			if (-f "$root/$checkfile" &&
2961e09dec48SAndy Whitcroft			    $realfile ne $checkfile &&
29627840a94cSWolfram Sang			    $1 !~ /$allowed_asm_includes/)
2963c45dcabdSAndy Whitcroft			{
2964e09dec48SAndy Whitcroft				if ($realfile =~ m{^arch/}) {
2965000d1cc1SJoe Perches					CHK("ARCH_INCLUDE_LINUX",
2966000d1cc1SJoe Perches					    "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
2967e09dec48SAndy Whitcroft				} else {
2968000d1cc1SJoe Perches					WARN("INCLUDE_LINUX",
2969000d1cc1SJoe Perches					     "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
2970e09dec48SAndy Whitcroft				}
29710a920b5bSAndy Whitcroft			}
29720a920b5bSAndy Whitcroft		}
29730a920b5bSAndy Whitcroft
2974653d4876SAndy Whitcroft# multi-statement macros should be enclosed in a do while loop, grab the
2975653d4876SAndy Whitcroft# first statement and ensure its the whole macro if its not enclosed
2976cf655043SAndy Whitcroft# in a known good container
2977b8f96a31SAndy Whitcroft		if ($realfile !~ m@/vmlinux.lds.h$@ &&
2978b8f96a31SAndy Whitcroft		    $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
2979d8aaf121SAndy Whitcroft			my $ln = $linenr;
2980d8aaf121SAndy Whitcroft			my $cnt = $realcnt;
2981c45dcabdSAndy Whitcroft			my ($off, $dstat, $dcond, $rest);
2982c45dcabdSAndy Whitcroft			my $ctx = '';
2983c45dcabdSAndy Whitcroft			($dstat, $dcond, $ln, $cnt, $off) =
2984f74bd194SAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0);
2985f74bd194SAndy Whitcroft			$ctx = $dstat;
2986c45dcabdSAndy Whitcroft			#print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
2987a3bb97a7SAndy Whitcroft			#print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
2988c45dcabdSAndy Whitcroft
2989f74bd194SAndy Whitcroft			$dstat =~ s/^.\s*\#\s*define\s+$Ident(?:\([^\)]*\))?\s*//;
2990292f1a9bSAndy Whitcroft			$dstat =~ s/$;//g;
2991c45dcabdSAndy Whitcroft			$dstat =~ s/\\\n.//g;
2992c45dcabdSAndy Whitcroft			$dstat =~ s/^\s*//s;
2993c45dcabdSAndy Whitcroft			$dstat =~ s/\s*$//s;
2994c45dcabdSAndy Whitcroft
2995c45dcabdSAndy Whitcroft			# Flatten any parentheses and braces
2996bf30d6edSAndy Whitcroft			while ($dstat =~ s/\([^\(\)]*\)/1/ ||
2997bf30d6edSAndy Whitcroft			       $dstat =~ s/\{[^\{\}]*\}/1/ ||
2998c81769fdSAndy Whitcroft			       $dstat =~ s/\[[^\[\]]*\]/1/)
2999bf30d6edSAndy Whitcroft			{
3000c45dcabdSAndy Whitcroft			}
3001c45dcabdSAndy Whitcroft
3002e45bab8eSAndy Whitcroft			# Flatten any obvious string concatentation.
3003e45bab8eSAndy Whitcroft			while ($dstat =~ s/("X*")\s*$Ident/$1/ ||
3004e45bab8eSAndy Whitcroft			       $dstat =~ s/$Ident\s*("X*")/$1/)
3005e45bab8eSAndy Whitcroft			{
3006e45bab8eSAndy Whitcroft			}
3007e45bab8eSAndy Whitcroft
3008c45dcabdSAndy Whitcroft			my $exceptions = qr{
3009c45dcabdSAndy Whitcroft				$Declare|
3010c45dcabdSAndy Whitcroft				module_param_named|
3011a0a0a7a9SKees Cook				MODULE_PARM_DESC|
3012c45dcabdSAndy Whitcroft				DECLARE_PER_CPU|
3013c45dcabdSAndy Whitcroft				DEFINE_PER_CPU|
3014383099fdSAndy Whitcroft				__typeof__\(|
301522fd2d3eSStefani Seibold				union|
301622fd2d3eSStefani Seibold				struct|
3017ea71a0a0SAndy Whitcroft				\.$Ident\s*=\s*|
3018ea71a0a0SAndy Whitcroft				^\"|\"$
3019c45dcabdSAndy Whitcroft			}x;
30205eaa20b9SAndy Whitcroft			#print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
3021f74bd194SAndy Whitcroft			if ($dstat ne '' &&
3022f74bd194SAndy Whitcroft			    $dstat !~ /^(?:$Ident|-?$Constant),$/ &&			# 10, // foo(),
3023f74bd194SAndy Whitcroft			    $dstat !~ /^(?:$Ident|-?$Constant);$/ &&			# foo();
3024fd1b57acSAndy Whitcroft			    $dstat !~ /^[!~-]?(?:$Ident|$Constant)$/ &&		# 10 // foo() // !foo // ~foo // -foo
3025b9df76acSAndy Whitcroft			    $dstat !~ /^'X'$/ &&					# character constants
3026f74bd194SAndy Whitcroft			    $dstat !~ /$exceptions/ &&
3027f74bd194SAndy Whitcroft			    $dstat !~ /^\.$Ident\s*=/ &&				# .foo =
3028e942e2c3SJoe Perches			    $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ &&		# stringification #foo
302972f115f9SAndy Whitcroft			    $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ &&	# do {...} while (...); // do {...} while (...)
3030f74bd194SAndy Whitcroft			    $dstat !~ /^for\s*$Constant$/ &&				# for (...)
3031f74bd194SAndy Whitcroft			    $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ &&	# for (...) bar()
3032f74bd194SAndy Whitcroft			    $dstat !~ /^do\s*{/ &&					# do {...
3033f74bd194SAndy Whitcroft			    $dstat !~ /^\({/)						# ({...
3034c45dcabdSAndy Whitcroft			{
3035f74bd194SAndy Whitcroft				$ctx =~ s/\n*$//;
3036f74bd194SAndy Whitcroft				my $herectx = $here . "\n";
3037f74bd194SAndy Whitcroft				my $cnt = statement_rawlines($ctx);
3038f74bd194SAndy Whitcroft
3039f74bd194SAndy Whitcroft				for (my $n = 0; $n < $cnt; $n++) {
3040f74bd194SAndy Whitcroft					$herectx .= raw_line($linenr, $n) . "\n";
3041c45dcabdSAndy Whitcroft				}
3042c45dcabdSAndy Whitcroft
3043f74bd194SAndy Whitcroft				if ($dstat =~ /;/) {
3044f74bd194SAndy Whitcroft					ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
3045f74bd194SAndy Whitcroft					      "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
3046f74bd194SAndy Whitcroft				} else {
3047000d1cc1SJoe Perches					ERROR("COMPLEX_MACRO",
3048f74bd194SAndy Whitcroft					      "Macros with complex values should be enclosed in parenthesis\n" . "$herectx");
3049d8aaf121SAndy Whitcroft				}
30500a920b5bSAndy Whitcroft			}
30515023d347SJoe Perches
3052481eb486SJoe Perches# check for line continuations outside of #defines, preprocessor #, and asm
30535023d347SJoe Perches
30545023d347SJoe Perches		} else {
30555023d347SJoe Perches			if ($prevline !~ /^..*\\$/ &&
3056481eb486SJoe Perches			    $line !~ /^\+\s*\#.*\\$/ &&		# preprocessor
3057481eb486SJoe Perches			    $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ &&	# asm
30585023d347SJoe Perches			    $line =~ /^\+.*\\$/) {
30595023d347SJoe Perches				WARN("LINE_CONTINUATIONS",
30605023d347SJoe Perches				     "Avoid unnecessary line continuations\n" . $herecurr);
30615023d347SJoe Perches			}
3062653d4876SAndy Whitcroft		}
30630a920b5bSAndy Whitcroft
3064b13edf7fSJoe Perches# do {} while (0) macro tests:
3065b13edf7fSJoe Perches# single-statement macros do not need to be enclosed in do while (0) loop,
3066b13edf7fSJoe Perches# macro should not end with a semicolon
3067b13edf7fSJoe Perches		if ($^V && $^V ge 5.10.0 &&
3068b13edf7fSJoe Perches		    $realfile !~ m@/vmlinux.lds.h$@ &&
3069b13edf7fSJoe Perches		    $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
3070b13edf7fSJoe Perches			my $ln = $linenr;
3071b13edf7fSJoe Perches			my $cnt = $realcnt;
3072b13edf7fSJoe Perches			my ($off, $dstat, $dcond, $rest);
3073b13edf7fSJoe Perches			my $ctx = '';
3074b13edf7fSJoe Perches			($dstat, $dcond, $ln, $cnt, $off) =
3075b13edf7fSJoe Perches				ctx_statement_block($linenr, $realcnt, 0);
3076b13edf7fSJoe Perches			$ctx = $dstat;
3077b13edf7fSJoe Perches
3078b13edf7fSJoe Perches			$dstat =~ s/\\\n.//g;
3079b13edf7fSJoe Perches
3080b13edf7fSJoe Perches			if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
3081b13edf7fSJoe Perches				my $stmts = $2;
3082b13edf7fSJoe Perches				my $semis = $3;
3083b13edf7fSJoe Perches
3084b13edf7fSJoe Perches				$ctx =~ s/\n*$//;
3085b13edf7fSJoe Perches				my $cnt = statement_rawlines($ctx);
3086b13edf7fSJoe Perches				my $herectx = $here . "\n";
3087b13edf7fSJoe Perches
3088b13edf7fSJoe Perches				for (my $n = 0; $n < $cnt; $n++) {
3089b13edf7fSJoe Perches					$herectx .= raw_line($linenr, $n) . "\n";
3090b13edf7fSJoe Perches				}
3091b13edf7fSJoe Perches
3092ac8e97f8SJoe Perches				if (($stmts =~ tr/;/;/) == 1 &&
3093ac8e97f8SJoe Perches				    $stmts !~ /^\s*(if|while|for|switch)\b/) {
3094b13edf7fSJoe Perches					WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
3095b13edf7fSJoe Perches					     "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
3096b13edf7fSJoe Perches				}
3097b13edf7fSJoe Perches				if (defined $semis && $semis ne "") {
3098b13edf7fSJoe Perches					WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
3099b13edf7fSJoe Perches					     "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
3100b13edf7fSJoe Perches				}
3101b13edf7fSJoe Perches			}
3102b13edf7fSJoe Perches		}
3103b13edf7fSJoe Perches
3104080ba929SMike Frysinger# make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
3105080ba929SMike Frysinger# all assignments may have only one of the following with an assignment:
3106080ba929SMike Frysinger#	.
3107080ba929SMike Frysinger#	ALIGN(...)
3108080ba929SMike Frysinger#	VMLINUX_SYMBOL(...)
3109080ba929SMike Frysinger		if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
3110000d1cc1SJoe Perches			WARN("MISSING_VMLINUX_SYMBOL",
3111000d1cc1SJoe Perches			     "vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
3112080ba929SMike Frysinger		}
3113080ba929SMike Frysinger
3114f0a594c1SAndy Whitcroft# check for redundant bracing round if etc
311513214adfSAndy Whitcroft		if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
311613214adfSAndy Whitcroft			my ($level, $endln, @chunks) =
3117cf655043SAndy Whitcroft				ctx_statement_full($linenr, $realcnt, 1);
311813214adfSAndy Whitcroft			#print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
3119cf655043SAndy Whitcroft			#print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
3120cf655043SAndy Whitcroft			if ($#chunks > 0 && $level == 0) {
3121aad4f614SJoe Perches				my @allowed = ();
3122aad4f614SJoe Perches				my $allow = 0;
312313214adfSAndy Whitcroft				my $seen = 0;
3124773647a0SAndy Whitcroft				my $herectx = $here . "\n";
3125cf655043SAndy Whitcroft				my $ln = $linenr - 1;
312613214adfSAndy Whitcroft				for my $chunk (@chunks) {
312713214adfSAndy Whitcroft					my ($cond, $block) = @{$chunk};
312813214adfSAndy Whitcroft
3129773647a0SAndy Whitcroft					# If the condition carries leading newlines, then count those as offsets.
3130773647a0SAndy Whitcroft					my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
3131773647a0SAndy Whitcroft					my $offset = statement_rawlines($whitespace) - 1;
3132773647a0SAndy Whitcroft
3133aad4f614SJoe Perches					$allowed[$allow] = 0;
3134773647a0SAndy Whitcroft					#print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
3135773647a0SAndy Whitcroft
3136773647a0SAndy Whitcroft					# We have looked at and allowed this specific line.
3137773647a0SAndy Whitcroft					$suppress_ifbraces{$ln + $offset} = 1;
3138773647a0SAndy Whitcroft
3139773647a0SAndy Whitcroft					$herectx .= "$rawlines[$ln + $offset]\n[...]\n";
3140cf655043SAndy Whitcroft					$ln += statement_rawlines($block) - 1;
3141cf655043SAndy Whitcroft
3142773647a0SAndy Whitcroft					substr($block, 0, length($cond), '');
314313214adfSAndy Whitcroft
314413214adfSAndy Whitcroft					$seen++ if ($block =~ /^\s*{/);
314513214adfSAndy Whitcroft
3146aad4f614SJoe Perches					#print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
3147cf655043SAndy Whitcroft					if (statement_lines($cond) > 1) {
3148cf655043SAndy Whitcroft						#print "APW: ALLOWED: cond<$cond>\n";
3149aad4f614SJoe Perches						$allowed[$allow] = 1;
315013214adfSAndy Whitcroft					}
315113214adfSAndy Whitcroft					if ($block =~/\b(?:if|for|while)\b/) {
3152cf655043SAndy Whitcroft						#print "APW: ALLOWED: block<$block>\n";
3153aad4f614SJoe Perches						$allowed[$allow] = 1;
315413214adfSAndy Whitcroft					}
3155cf655043SAndy Whitcroft					if (statement_block_size($block) > 1) {
3156cf655043SAndy Whitcroft						#print "APW: ALLOWED: lines block<$block>\n";
3157aad4f614SJoe Perches						$allowed[$allow] = 1;
315813214adfSAndy Whitcroft					}
3159aad4f614SJoe Perches					$allow++;
316013214adfSAndy Whitcroft				}
3161aad4f614SJoe Perches				if ($seen) {
3162aad4f614SJoe Perches					my $sum_allowed = 0;
3163aad4f614SJoe Perches					foreach (@allowed) {
3164aad4f614SJoe Perches						$sum_allowed += $_;
3165aad4f614SJoe Perches					}
3166aad4f614SJoe Perches					if ($sum_allowed == 0) {
3167000d1cc1SJoe Perches						WARN("BRACES",
3168000d1cc1SJoe Perches						     "braces {} are not necessary for any arm of this statement\n" . $herectx);
3169aad4f614SJoe Perches					} elsif ($sum_allowed != $allow &&
3170aad4f614SJoe Perches						 $seen != $allow) {
3171aad4f614SJoe Perches						CHK("BRACES",
3172aad4f614SJoe Perches						    "braces {} should be used on all arms of this statement\n" . $herectx);
3173aad4f614SJoe Perches					}
317413214adfSAndy Whitcroft				}
317513214adfSAndy Whitcroft			}
317613214adfSAndy Whitcroft		}
3177773647a0SAndy Whitcroft		if (!defined $suppress_ifbraces{$linenr - 1} &&
317813214adfSAndy Whitcroft					$line =~ /\b(if|while|for|else)\b/) {
3179cf655043SAndy Whitcroft			my $allowed = 0;
3180f0a594c1SAndy Whitcroft
3181cf655043SAndy Whitcroft			# Check the pre-context.
3182cf655043SAndy Whitcroft			if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
3183cf655043SAndy Whitcroft				#print "APW: ALLOWED: pre<$1>\n";
3184cf655043SAndy Whitcroft				$allowed = 1;
3185f0a594c1SAndy Whitcroft			}
3186773647a0SAndy Whitcroft
3187773647a0SAndy Whitcroft			my ($level, $endln, @chunks) =
3188773647a0SAndy Whitcroft				ctx_statement_full($linenr, $realcnt, $-[0]);
3189773647a0SAndy Whitcroft
3190cf655043SAndy Whitcroft			# Check the condition.
3191cf655043SAndy Whitcroft			my ($cond, $block) = @{$chunks[0]};
3192773647a0SAndy Whitcroft			#print "CHECKING<$linenr> cond<$cond> block<$block>\n";
3193cf655043SAndy Whitcroft			if (defined $cond) {
3194773647a0SAndy Whitcroft				substr($block, 0, length($cond), '');
3195cf655043SAndy Whitcroft			}
3196cf655043SAndy Whitcroft			if (statement_lines($cond) > 1) {
3197cf655043SAndy Whitcroft				#print "APW: ALLOWED: cond<$cond>\n";
3198cf655043SAndy Whitcroft				$allowed = 1;
3199cf655043SAndy Whitcroft			}
3200cf655043SAndy Whitcroft			if ($block =~/\b(?:if|for|while)\b/) {
3201cf655043SAndy Whitcroft				#print "APW: ALLOWED: block<$block>\n";
3202cf655043SAndy Whitcroft				$allowed = 1;
3203cf655043SAndy Whitcroft			}
3204cf655043SAndy Whitcroft			if (statement_block_size($block) > 1) {
3205cf655043SAndy Whitcroft				#print "APW: ALLOWED: lines block<$block>\n";
3206cf655043SAndy Whitcroft				$allowed = 1;
3207cf655043SAndy Whitcroft			}
3208cf655043SAndy Whitcroft			# Check the post-context.
3209cf655043SAndy Whitcroft			if (defined $chunks[1]) {
3210cf655043SAndy Whitcroft				my ($cond, $block) = @{$chunks[1]};
3211cf655043SAndy Whitcroft				if (defined $cond) {
3212773647a0SAndy Whitcroft					substr($block, 0, length($cond), '');
3213cf655043SAndy Whitcroft				}
3214cf655043SAndy Whitcroft				if ($block =~ /^\s*\{/) {
3215cf655043SAndy Whitcroft					#print "APW: ALLOWED: chunk-1 block<$block>\n";
3216cf655043SAndy Whitcroft					$allowed = 1;
3217cf655043SAndy Whitcroft				}
3218cf655043SAndy Whitcroft			}
3219cf655043SAndy Whitcroft			if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
322069932487SJustin P. Mattock				my $herectx = $here . "\n";
3221f055663cSAndy Whitcroft				my $cnt = statement_rawlines($block);
3222cf655043SAndy Whitcroft
3223f055663cSAndy Whitcroft				for (my $n = 0; $n < $cnt; $n++) {
322469932487SJustin P. Mattock					$herectx .= raw_line($linenr, $n) . "\n";
3225cf655043SAndy Whitcroft				}
3226cf655043SAndy Whitcroft
3227000d1cc1SJoe Perches				WARN("BRACES",
3228000d1cc1SJoe Perches				     "braces {} are not necessary for single statement blocks\n" . $herectx);
3229f0a594c1SAndy Whitcroft			}
3230f0a594c1SAndy Whitcroft		}
3231f0a594c1SAndy Whitcroft
32320979ae66SJoe Perches# check for unnecessary blank lines around braces
323374c8f433SMatthijs Kooijman		if (($line =~ /^.\s*}\s*$/ && $prevline =~ /^.\s*$/)) {
32340979ae66SJoe Perches			CHK("BRACES",
32350979ae66SJoe Perches			    "Blank lines aren't necessary before a close brace '}'\n" . $hereprev);
32360979ae66SJoe Perches		}
32370979ae66SJoe Perches		if (($line =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
32380979ae66SJoe Perches			CHK("BRACES",
32390979ae66SJoe Perches			    "Blank lines aren't necessary after an open brace '{'\n" . $hereprev);
32400979ae66SJoe Perches		}
32410979ae66SJoe Perches
32424a0df2efSAndy Whitcroft# no volatiles please
32436c72ffaaSAndy Whitcroft		my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
32446c72ffaaSAndy Whitcroft		if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
3245000d1cc1SJoe Perches			WARN("VOLATILE",
3246000d1cc1SJoe Perches			     "Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
32474a0df2efSAndy Whitcroft		}
32484a0df2efSAndy Whitcroft
324900df344fSAndy Whitcroft# warn about #if 0
3250c45dcabdSAndy Whitcroft		if ($line =~ /^.\s*\#\s*if\s+0\b/) {
3251000d1cc1SJoe Perches			CHK("REDUNDANT_CODE",
3252000d1cc1SJoe Perches			    "if this code is redundant consider removing it\n" .
3253de7d4f0eSAndy Whitcroft				$herecurr);
32544a0df2efSAndy Whitcroft		}
32554a0df2efSAndy Whitcroft
325603df4b51SAndy Whitcroft# check for needless "if (<foo>) fn(<foo>)" uses
325703df4b51SAndy Whitcroft		if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
325803df4b51SAndy Whitcroft			my $expr = '\s*\(\s*' . quotemeta($1) . '\s*\)\s*;';
325903df4b51SAndy Whitcroft			if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?)$expr/) {
326003df4b51SAndy Whitcroft				WARN('NEEDLESS_IF',
326103df4b51SAndy Whitcroft				     "$1(NULL) is safe this check is probably not required\n" . $hereprev);
32624c432a8fSGreg Kroah-Hartman			}
32634c432a8fSGreg Kroah-Hartman		}
3264f0a594c1SAndy Whitcroft
32651a15a250SPatrick Pannuto# prefer usleep_range over udelay
326637581c28SBruce Allan		if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
32671a15a250SPatrick Pannuto			# ignore udelay's < 10, however
326837581c28SBruce Allan			if (! ($1 < 10) ) {
3269000d1cc1SJoe Perches				CHK("USLEEP_RANGE",
3270000d1cc1SJoe Perches				    "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $line);
32711a15a250SPatrick Pannuto			}
32721a15a250SPatrick Pannuto		}
32731a15a250SPatrick Pannuto
327409ef8725SPatrick Pannuto# warn about unexpectedly long msleep's
327509ef8725SPatrick Pannuto		if ($line =~ /\bmsleep\s*\((\d+)\);/) {
327609ef8725SPatrick Pannuto			if ($1 < 20) {
3277000d1cc1SJoe Perches				WARN("MSLEEP",
3278000d1cc1SJoe Perches				     "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $line);
327909ef8725SPatrick Pannuto			}
328009ef8725SPatrick Pannuto		}
328109ef8725SPatrick Pannuto
328200df344fSAndy Whitcroft# warn about #ifdefs in C files
3283c45dcabdSAndy Whitcroft#		if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
328400df344fSAndy Whitcroft#			print "#ifdef in C files should be avoided\n";
328500df344fSAndy Whitcroft#			print "$herecurr";
328600df344fSAndy Whitcroft#			$clean = 0;
328700df344fSAndy Whitcroft#		}
328800df344fSAndy Whitcroft
328922f2a2efSAndy Whitcroft# warn about spacing in #ifdefs
3290c45dcabdSAndy Whitcroft		if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
3291000d1cc1SJoe Perches			ERROR("SPACING",
3292000d1cc1SJoe Perches			      "exactly one space required after that #$1\n" . $herecurr);
329322f2a2efSAndy Whitcroft		}
329422f2a2efSAndy Whitcroft
32954a0df2efSAndy Whitcroft# check for spinlock_t definitions without a comment.
3296171ae1a4SAndy Whitcroft		if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
3297171ae1a4SAndy Whitcroft		    $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
32984a0df2efSAndy Whitcroft			my $which = $1;
32994a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
3300000d1cc1SJoe Perches				CHK("UNCOMMENTED_DEFINITION",
3301000d1cc1SJoe Perches				    "$1 definition without comment\n" . $herecurr);
33024a0df2efSAndy Whitcroft			}
33034a0df2efSAndy Whitcroft		}
33044a0df2efSAndy Whitcroft# check for memory barriers without a comment.
33054a0df2efSAndy Whitcroft		if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
33064a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
3307000d1cc1SJoe Perches				CHK("MEMORY_BARRIER",
3308000d1cc1SJoe Perches				    "memory barrier without comment\n" . $herecurr);
33094a0df2efSAndy Whitcroft			}
33104a0df2efSAndy Whitcroft		}
33114a0df2efSAndy Whitcroft# check of hardware specific defines
3312c45dcabdSAndy Whitcroft		if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
3313000d1cc1SJoe Perches			CHK("ARCH_DEFINES",
3314000d1cc1SJoe Perches			    "architecture specific defines should be avoided\n" .  $herecurr);
33150a920b5bSAndy Whitcroft		}
3316653d4876SAndy Whitcroft
3317d4977c78STobias Klauser# Check that the storage class is at the beginning of a declaration
3318d4977c78STobias Klauser		if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
3319000d1cc1SJoe Perches			WARN("STORAGE_CLASS",
3320000d1cc1SJoe Perches			     "storage class should be at the beginning of the declaration\n" . $herecurr)
3321d4977c78STobias Klauser		}
3322d4977c78STobias Klauser
3323de7d4f0eSAndy Whitcroft# check the location of the inline attribute, that it is between
3324de7d4f0eSAndy Whitcroft# storage class and type.
33259c0ca6f9SAndy Whitcroft		if ($line =~ /\b$Type\s+$Inline\b/ ||
33269c0ca6f9SAndy Whitcroft		    $line =~ /\b$Inline\s+$Storage\b/) {
3327000d1cc1SJoe Perches			ERROR("INLINE_LOCATION",
3328000d1cc1SJoe Perches			      "inline keyword should sit between storage class and type\n" . $herecurr);
3329de7d4f0eSAndy Whitcroft		}
3330de7d4f0eSAndy Whitcroft
33318905a67cSAndy Whitcroft# Check for __inline__ and __inline, prefer inline
33328905a67cSAndy Whitcroft		if ($line =~ /\b(__inline__|__inline)\b/) {
3333000d1cc1SJoe Perches			WARN("INLINE",
3334000d1cc1SJoe Perches			     "plain inline is preferred over $1\n" . $herecurr);
33358905a67cSAndy Whitcroft		}
33368905a67cSAndy Whitcroft
33373d130fd0SJoe Perches# Check for __attribute__ packed, prefer __packed
33383d130fd0SJoe Perches		if ($line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
3339000d1cc1SJoe Perches			WARN("PREFER_PACKED",
3340000d1cc1SJoe Perches			     "__packed is preferred over __attribute__((packed))\n" . $herecurr);
33413d130fd0SJoe Perches		}
33423d130fd0SJoe Perches
334339b7e287SJoe Perches# Check for __attribute__ aligned, prefer __aligned
334439b7e287SJoe Perches		if ($line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
3345000d1cc1SJoe Perches			WARN("PREFER_ALIGNED",
3346000d1cc1SJoe Perches			     "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
334739b7e287SJoe Perches		}
334839b7e287SJoe Perches
33495f14d3bdSJoe Perches# Check for __attribute__ format(printf, prefer __printf
33505f14d3bdSJoe Perches		if ($line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
33515f14d3bdSJoe Perches			WARN("PREFER_PRINTF",
33525f14d3bdSJoe Perches			     "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr);
33535f14d3bdSJoe Perches		}
33545f14d3bdSJoe Perches
33556061d949SJoe Perches# Check for __attribute__ format(scanf, prefer __scanf
33566061d949SJoe Perches		if ($line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
33576061d949SJoe Perches			WARN("PREFER_SCANF",
33586061d949SJoe Perches			     "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr);
33596061d949SJoe Perches		}
33606061d949SJoe Perches
33618f53a9b8SJoe Perches# check for sizeof(&)
33628f53a9b8SJoe Perches		if ($line =~ /\bsizeof\s*\(\s*\&/) {
3363000d1cc1SJoe Perches			WARN("SIZEOF_ADDRESS",
3364000d1cc1SJoe Perches			     "sizeof(& should be avoided\n" . $herecurr);
33658f53a9b8SJoe Perches		}
33668f53a9b8SJoe Perches
336766c80b60SJoe Perches# check for sizeof without parenthesis
336866c80b60SJoe Perches		if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
336966c80b60SJoe Perches			WARN("SIZEOF_PARENTHESIS",
337066c80b60SJoe Perches			     "sizeof $1 should be sizeof($1)\n" . $herecurr);
337166c80b60SJoe Perches		}
337266c80b60SJoe Perches
3373428e2fdcSJoe Perches# check for line continuations in quoted strings with odd counts of "
3374428e2fdcSJoe Perches		if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) {
3375000d1cc1SJoe Perches			WARN("LINE_CONTINUATIONS",
3376000d1cc1SJoe Perches			     "Avoid line continuations in quoted strings\n" . $herecurr);
3377428e2fdcSJoe Perches		}
3378428e2fdcSJoe Perches
337988982feaSJoe Perches# check for struct spinlock declarations
338088982feaSJoe Perches		if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
338188982feaSJoe Perches			WARN("USE_SPINLOCK_T",
338288982feaSJoe Perches			     "struct spinlock should be spinlock_t\n" . $herecurr);
338388982feaSJoe Perches		}
338488982feaSJoe Perches
3385a6962d72SJoe Perches# check for seq_printf uses that could be seq_puts
3386a6962d72SJoe Perches		if ($line =~ /\bseq_printf\s*\(/) {
3387a6962d72SJoe Perches			my $fmt = get_quoted_string($line, $rawline);
3388a6962d72SJoe Perches			if ($fmt !~ /[^\\]\%/) {
3389a6962d72SJoe Perches				WARN("PREFER_SEQ_PUTS",
3390a6962d72SJoe Perches				     "Prefer seq_puts to seq_printf\n" . $herecurr);
3391a6962d72SJoe Perches			}
3392a6962d72SJoe Perches		}
3393a6962d72SJoe Perches
3394554e165cSAndy Whitcroft# Check for misused memsets
3395d1fe9c09SJoe Perches		if ($^V && $^V ge 5.10.0 &&
3396d1fe9c09SJoe Perches		    defined $stat &&
3397d7c76ba7SJoe Perches		    $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/s) {
3398554e165cSAndy Whitcroft
3399d7c76ba7SJoe Perches			my $ms_addr = $2;
3400d1fe9c09SJoe Perches			my $ms_val = $7;
3401d1fe9c09SJoe Perches			my $ms_size = $12;
3402d7c76ba7SJoe Perches
3403554e165cSAndy Whitcroft			if ($ms_size =~ /^(0x|)0$/i) {
3404554e165cSAndy Whitcroft				ERROR("MEMSET",
3405d7c76ba7SJoe Perches				      "memset to 0's uses 0 as the 2nd argument, not the 3rd\n" . "$here\n$stat\n");
3406554e165cSAndy Whitcroft			} elsif ($ms_size =~ /^(0x|)1$/i) {
3407554e165cSAndy Whitcroft				WARN("MEMSET",
3408d7c76ba7SJoe Perches				     "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
3409d7c76ba7SJoe Perches			}
3410d7c76ba7SJoe Perches		}
3411d7c76ba7SJoe Perches
3412d7c76ba7SJoe Perches# typecasts on min/max could be min_t/max_t
3413d1fe9c09SJoe Perches		if ($^V && $^V ge 5.10.0 &&
3414d1fe9c09SJoe Perches		    defined $stat &&
3415d7c76ba7SJoe Perches		    $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
3416d1fe9c09SJoe Perches			if (defined $2 || defined $7) {
3417d7c76ba7SJoe Perches				my $call = $1;
3418d7c76ba7SJoe Perches				my $cast1 = deparenthesize($2);
3419d7c76ba7SJoe Perches				my $arg1 = $3;
3420d1fe9c09SJoe Perches				my $cast2 = deparenthesize($7);
3421d1fe9c09SJoe Perches				my $arg2 = $8;
3422d7c76ba7SJoe Perches				my $cast;
3423d7c76ba7SJoe Perches
3424d1fe9c09SJoe Perches				if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
3425d7c76ba7SJoe Perches					$cast = "$cast1 or $cast2";
3426d7c76ba7SJoe Perches				} elsif ($cast1 ne "") {
3427d7c76ba7SJoe Perches					$cast = $cast1;
3428d7c76ba7SJoe Perches				} else {
3429d7c76ba7SJoe Perches					$cast = $cast2;
3430d7c76ba7SJoe Perches				}
3431d7c76ba7SJoe Perches				WARN("MINMAX",
3432d7c76ba7SJoe Perches				     "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
3433554e165cSAndy Whitcroft			}
3434554e165cSAndy Whitcroft		}
3435554e165cSAndy Whitcroft
34364a273195SJoe Perches# check usleep_range arguments
34374a273195SJoe Perches		if ($^V && $^V ge 5.10.0 &&
34384a273195SJoe Perches		    defined $stat &&
34394a273195SJoe Perches		    $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
34404a273195SJoe Perches			my $min = $1;
34414a273195SJoe Perches			my $max = $7;
34424a273195SJoe Perches			if ($min eq $max) {
34434a273195SJoe Perches				WARN("USLEEP_RANGE",
34444a273195SJoe Perches				     "usleep_range should not use min == max args; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
34454a273195SJoe Perches			} elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
34464a273195SJoe Perches				 $min > $max) {
34474a273195SJoe Perches				WARN("USLEEP_RANGE",
34484a273195SJoe Perches				     "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
34494a273195SJoe Perches			}
34504a273195SJoe Perches		}
34514a273195SJoe Perches
3452de7d4f0eSAndy Whitcroft# check for new externs in .c files.
3453171ae1a4SAndy Whitcroft		if ($realfile =~ /\.c$/ && defined $stat &&
3454c45dcabdSAndy Whitcroft		    $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
3455171ae1a4SAndy Whitcroft		{
3456c45dcabdSAndy Whitcroft			my $function_name = $1;
3457c45dcabdSAndy Whitcroft			my $paren_space = $2;
3458171ae1a4SAndy Whitcroft
3459171ae1a4SAndy Whitcroft			my $s = $stat;
3460171ae1a4SAndy Whitcroft			if (defined $cond) {
3461171ae1a4SAndy Whitcroft				substr($s, 0, length($cond), '');
3462171ae1a4SAndy Whitcroft			}
3463c45dcabdSAndy Whitcroft			if ($s =~ /^\s*;/ &&
3464c45dcabdSAndy Whitcroft			    $function_name ne 'uninitialized_var')
3465c45dcabdSAndy Whitcroft			{
3466000d1cc1SJoe Perches				WARN("AVOID_EXTERNS",
3467000d1cc1SJoe Perches				     "externs should be avoided in .c files\n" .  $herecurr);
3468de7d4f0eSAndy Whitcroft			}
3469de7d4f0eSAndy Whitcroft
3470171ae1a4SAndy Whitcroft			if ($paren_space =~ /\n/) {
3471000d1cc1SJoe Perches				WARN("FUNCTION_ARGUMENTS",
3472000d1cc1SJoe Perches				     "arguments for function declarations should follow identifier\n" . $herecurr);
3473171ae1a4SAndy Whitcroft			}
34749c9ba34eSAndy Whitcroft
34759c9ba34eSAndy Whitcroft		} elsif ($realfile =~ /\.c$/ && defined $stat &&
34769c9ba34eSAndy Whitcroft		    $stat =~ /^.\s*extern\s+/)
34779c9ba34eSAndy Whitcroft		{
3478000d1cc1SJoe Perches			WARN("AVOID_EXTERNS",
3479000d1cc1SJoe Perches			     "externs should be avoided in .c files\n" .  $herecurr);
3480171ae1a4SAndy Whitcroft		}
3481171ae1a4SAndy Whitcroft
3482de7d4f0eSAndy Whitcroft# checks for new __setup's
3483de7d4f0eSAndy Whitcroft		if ($rawline =~ /\b__setup\("([^"]*)"/) {
3484de7d4f0eSAndy Whitcroft			my $name = $1;
3485de7d4f0eSAndy Whitcroft
3486de7d4f0eSAndy Whitcroft			if (!grep(/$name/, @setup_docs)) {
3487000d1cc1SJoe Perches				CHK("UNDOCUMENTED_SETUP",
3488000d1cc1SJoe Perches				    "__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
3489de7d4f0eSAndy Whitcroft			}
3490653d4876SAndy Whitcroft		}
34919c0ca6f9SAndy Whitcroft
34929c0ca6f9SAndy Whitcroft# check for pointless casting of kmalloc return
3493caf2a54fSJoe Perches		if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) {
3494000d1cc1SJoe Perches			WARN("UNNECESSARY_CASTS",
3495000d1cc1SJoe Perches			     "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
34969c0ca6f9SAndy Whitcroft		}
349713214adfSAndy Whitcroft
3498972fdea2SJoe Perches# check for krealloc arg reuse
3499972fdea2SJoe Perches		if ($^V && $^V ge 5.10.0 &&
3500972fdea2SJoe Perches		    $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*\1\s*,/) {
3501972fdea2SJoe Perches			WARN("KREALLOC_ARG_REUSE",
3502972fdea2SJoe Perches			     "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
3503972fdea2SJoe Perches		}
3504972fdea2SJoe Perches
35055ce59ae0SJoe Perches# check for alloc argument mismatch
35065ce59ae0SJoe Perches		if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
35075ce59ae0SJoe Perches			WARN("ALLOC_ARRAY_ARGS",
35085ce59ae0SJoe Perches			     "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
35095ce59ae0SJoe Perches		}
35105ce59ae0SJoe Perches
3511caf2a54fSJoe Perches# check for multiple semicolons
3512caf2a54fSJoe Perches		if ($line =~ /;\s*;\s*$/) {
3513000d1cc1SJoe Perches			WARN("ONE_SEMICOLON",
3514000d1cc1SJoe Perches			     "Statements terminations use 1 semicolon\n" . $herecurr);
3515d1e2ad07SJoe Perches		}
3516d1e2ad07SJoe Perches
3517d1e2ad07SJoe Perches# check for switch/default statements without a break;
3518d1e2ad07SJoe Perches		if ($^V && $^V ge 5.10.0 &&
3519d1e2ad07SJoe Perches		    defined $stat &&
3520d1e2ad07SJoe Perches		    $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
3521d1e2ad07SJoe Perches			my $ctx = '';
3522d1e2ad07SJoe Perches			my $herectx = $here . "\n";
3523d1e2ad07SJoe Perches			my $cnt = statement_rawlines($stat);
3524d1e2ad07SJoe Perches			for (my $n = 0; $n < $cnt; $n++) {
3525d1e2ad07SJoe Perches				$herectx .= raw_line($linenr, $n) . "\n";
3526d1e2ad07SJoe Perches			}
3527d1e2ad07SJoe Perches			WARN("DEFAULT_NO_BREAK",
3528d1e2ad07SJoe Perches			     "switch default: should use break\n" . $herectx);
3529caf2a54fSJoe Perches		}
3530caf2a54fSJoe Perches
353113214adfSAndy Whitcroft# check for gcc specific __FUNCTION__
353213214adfSAndy Whitcroft		if ($line =~ /__FUNCTION__/) {
3533000d1cc1SJoe Perches			WARN("USE_FUNC",
3534000d1cc1SJoe Perches			     "__func__ should be used instead of gcc specific __FUNCTION__\n"  . $herecurr);
353513214adfSAndy Whitcroft		}
3536773647a0SAndy Whitcroft
35372c92488aSJoe Perches# check for use of yield()
35382c92488aSJoe Perches		if ($line =~ /\byield\s*\(\s*\)/) {
35392c92488aSJoe Perches			WARN("YIELD",
35402c92488aSJoe Perches			     "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n"  . $herecurr);
35412c92488aSJoe Perches		}
35422c92488aSJoe Perches
35434882720bSThomas Gleixner# check for semaphores initialized locked
35444882720bSThomas Gleixner		if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
3545000d1cc1SJoe Perches			WARN("CONSIDER_COMPLETION",
3546000d1cc1SJoe Perches			     "consider using a completion\n" . $herecurr);
3547773647a0SAndy Whitcroft		}
35486712d858SJoe Perches
354967d0a075SJoe Perches# recommend kstrto* over simple_strto* and strict_strto*
355067d0a075SJoe Perches		if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
3551000d1cc1SJoe Perches			WARN("CONSIDER_KSTRTO",
355267d0a075SJoe Perches			     "$1 is obsolete, use k$3 instead\n" . $herecurr);
3553773647a0SAndy Whitcroft		}
35546712d858SJoe Perches
3555f3db6639SMichael Ellerman# check for __initcall(), use device_initcall() explicitly please
3556f3db6639SMichael Ellerman		if ($line =~ /^.\s*__initcall\s*\(/) {
3557000d1cc1SJoe Perches			WARN("USE_DEVICE_INITCALL",
3558000d1cc1SJoe Perches			     "please use device_initcall() instead of __initcall()\n" . $herecurr);
3559f3db6639SMichael Ellerman		}
35606712d858SJoe Perches
356179404849SEmese Revfy# check for various ops structs, ensure they are const.
356279404849SEmese Revfy		my $struct_ops = qr{acpi_dock_ops|
356379404849SEmese Revfy				address_space_operations|
356479404849SEmese Revfy				backlight_ops|
356579404849SEmese Revfy				block_device_operations|
356679404849SEmese Revfy				dentry_operations|
356779404849SEmese Revfy				dev_pm_ops|
356879404849SEmese Revfy				dma_map_ops|
356979404849SEmese Revfy				extent_io_ops|
357079404849SEmese Revfy				file_lock_operations|
357179404849SEmese Revfy				file_operations|
357279404849SEmese Revfy				hv_ops|
357379404849SEmese Revfy				ide_dma_ops|
357479404849SEmese Revfy				intel_dvo_dev_ops|
357579404849SEmese Revfy				item_operations|
357679404849SEmese Revfy				iwl_ops|
357779404849SEmese Revfy				kgdb_arch|
357879404849SEmese Revfy				kgdb_io|
357979404849SEmese Revfy				kset_uevent_ops|
358079404849SEmese Revfy				lock_manager_operations|
358179404849SEmese Revfy				microcode_ops|
358279404849SEmese Revfy				mtrr_ops|
358379404849SEmese Revfy				neigh_ops|
358479404849SEmese Revfy				nlmsvc_binding|
358579404849SEmese Revfy				pci_raw_ops|
358679404849SEmese Revfy				pipe_buf_operations|
358779404849SEmese Revfy				platform_hibernation_ops|
358879404849SEmese Revfy				platform_suspend_ops|
358979404849SEmese Revfy				proto_ops|
359079404849SEmese Revfy				rpc_pipe_ops|
359179404849SEmese Revfy				seq_operations|
359279404849SEmese Revfy				snd_ac97_build_ops|
359379404849SEmese Revfy				soc_pcmcia_socket_ops|
359479404849SEmese Revfy				stacktrace_ops|
359579404849SEmese Revfy				sysfs_ops|
359679404849SEmese Revfy				tty_operations|
359779404849SEmese Revfy				usb_mon_operations|
359879404849SEmese Revfy				wd_ops}x;
35996903ffb2SAndy Whitcroft		if ($line !~ /\bconst\b/ &&
360079404849SEmese Revfy		    $line =~ /\bstruct\s+($struct_ops)\b/) {
3601000d1cc1SJoe Perches			WARN("CONST_STRUCT",
3602000d1cc1SJoe Perches			     "struct $1 should normally be const\n" .
36036903ffb2SAndy Whitcroft				$herecurr);
36042b6db5cbSAndy Whitcroft		}
3605773647a0SAndy Whitcroft
3606773647a0SAndy Whitcroft# use of NR_CPUS is usually wrong
3607773647a0SAndy Whitcroft# ignore definitions of NR_CPUS and usage to define arrays as likely right
3608773647a0SAndy Whitcroft		if ($line =~ /\bNR_CPUS\b/ &&
3609c45dcabdSAndy Whitcroft		    $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
3610c45dcabdSAndy Whitcroft		    $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
3611171ae1a4SAndy Whitcroft		    $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
3612171ae1a4SAndy Whitcroft		    $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
3613171ae1a4SAndy Whitcroft		    $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
3614773647a0SAndy Whitcroft		{
3615000d1cc1SJoe Perches			WARN("NR_CPUS",
3616000d1cc1SJoe Perches			     "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
3617773647a0SAndy Whitcroft		}
36189c9ba34eSAndy Whitcroft
36199c9ba34eSAndy Whitcroft# check for %L{u,d,i} in strings
36209c9ba34eSAndy Whitcroft		my $string;
36219c9ba34eSAndy Whitcroft		while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
36229c9ba34eSAndy Whitcroft			$string = substr($rawline, $-[1], $+[1] - $-[1]);
36232a1bc5d5SAndy Whitcroft			$string =~ s/%%/__/g;
36249c9ba34eSAndy Whitcroft			if ($string =~ /(?<!%)%L[udi]/) {
3625000d1cc1SJoe Perches				WARN("PRINTF_L",
3626000d1cc1SJoe Perches				     "\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
36279c9ba34eSAndy Whitcroft				last;
36289c9ba34eSAndy Whitcroft			}
36299c9ba34eSAndy Whitcroft		}
3630691d77b6SAndy Whitcroft
3631691d77b6SAndy Whitcroft# whine mightly about in_atomic
3632691d77b6SAndy Whitcroft		if ($line =~ /\bin_atomic\s*\(/) {
3633691d77b6SAndy Whitcroft			if ($realfile =~ m@^drivers/@) {
3634000d1cc1SJoe Perches				ERROR("IN_ATOMIC",
3635000d1cc1SJoe Perches				      "do not use in_atomic in drivers\n" . $herecurr);
3636f4a87736SAndy Whitcroft			} elsif ($realfile !~ m@^kernel/@) {
3637000d1cc1SJoe Perches				WARN("IN_ATOMIC",
3638000d1cc1SJoe Perches				     "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
3639691d77b6SAndy Whitcroft			}
3640691d77b6SAndy Whitcroft		}
36411704f47bSPeter Zijlstra
36421704f47bSPeter Zijlstra# check for lockdep_set_novalidate_class
36431704f47bSPeter Zijlstra		if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
36441704f47bSPeter Zijlstra		    $line =~ /__lockdep_no_validate__\s*\)/ ) {
36451704f47bSPeter Zijlstra			if ($realfile !~ m@^kernel/lockdep@ &&
36461704f47bSPeter Zijlstra			    $realfile !~ m@^include/linux/lockdep@ &&
36471704f47bSPeter Zijlstra			    $realfile !~ m@^drivers/base/core@) {
3648000d1cc1SJoe Perches				ERROR("LOCKDEP",
3649000d1cc1SJoe Perches				      "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
36501704f47bSPeter Zijlstra			}
36511704f47bSPeter Zijlstra		}
365288f8831cSDave Jones
365388f8831cSDave Jones		if ($line =~ /debugfs_create_file.*S_IWUGO/ ||
365488f8831cSDave Jones		    $line =~ /DEVICE_ATTR.*S_IWUGO/ ) {
3655000d1cc1SJoe Perches			WARN("EXPORTED_WORLD_WRITABLE",
3656000d1cc1SJoe Perches			     "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
365788f8831cSDave Jones		}
365813214adfSAndy Whitcroft	}
365913214adfSAndy Whitcroft
366013214adfSAndy Whitcroft	# If we have no input at all, then there is nothing to report on
366113214adfSAndy Whitcroft	# so just keep quiet.
366213214adfSAndy Whitcroft	if ($#rawlines == -1) {
366313214adfSAndy Whitcroft		exit(0);
36640a920b5bSAndy Whitcroft	}
36650a920b5bSAndy Whitcroft
36668905a67cSAndy Whitcroft	# In mailback mode only produce a report in the negative, for
36678905a67cSAndy Whitcroft	# things that appear to be patches.
36688905a67cSAndy Whitcroft	if ($mailback && ($clean == 1 || !$is_patch)) {
36698905a67cSAndy Whitcroft		exit(0);
36708905a67cSAndy Whitcroft	}
36718905a67cSAndy Whitcroft
36728905a67cSAndy Whitcroft	# This is not a patch, and we are are in 'no-patch' mode so
36738905a67cSAndy Whitcroft	# just keep quiet.
36748905a67cSAndy Whitcroft	if (!$chk_patch && !$is_patch) {
36758905a67cSAndy Whitcroft		exit(0);
36768905a67cSAndy Whitcroft	}
36778905a67cSAndy Whitcroft
36788905a67cSAndy Whitcroft	if (!$is_patch) {
3679000d1cc1SJoe Perches		ERROR("NOT_UNIFIED_DIFF",
3680000d1cc1SJoe Perches		      "Does not appear to be a unified-diff format patch\n");
36810a920b5bSAndy Whitcroft	}
36820a920b5bSAndy Whitcroft	if ($is_patch && $chk_signoff && $signoff == 0) {
3683000d1cc1SJoe Perches		ERROR("MISSING_SIGN_OFF",
3684000d1cc1SJoe Perches		      "Missing Signed-off-by: line(s)\n");
36850a920b5bSAndy Whitcroft	}
36860a920b5bSAndy Whitcroft
3687f0a594c1SAndy Whitcroft	print report_dump();
368813214adfSAndy Whitcroft	if ($summary && !($clean == 1 && $quiet == 1)) {
368913214adfSAndy Whitcroft		print "$filename " if ($summary_file);
36906c72ffaaSAndy Whitcroft		print "total: $cnt_error errors, $cnt_warn warnings, " .
36916c72ffaaSAndy Whitcroft			(($check)? "$cnt_chk checks, " : "") .
36926c72ffaaSAndy Whitcroft			"$cnt_lines lines checked\n";
36938905a67cSAndy Whitcroft		print "\n" if ($quiet == 0);
36946c72ffaaSAndy Whitcroft	}
36958905a67cSAndy Whitcroft
3696d2c0a235SAndy Whitcroft	if ($quiet == 0) {
3697d1fe9c09SJoe Perches
3698d1fe9c09SJoe Perches		if ($^V lt 5.10.0) {
3699d1fe9c09SJoe Perches			print("NOTE: perl $^V is not modern enough to detect all possible issues.\n");
3700d1fe9c09SJoe Perches			print("An upgrade to at least perl v5.10.0 is suggested.\n\n");
3701d1fe9c09SJoe Perches		}
3702d1fe9c09SJoe Perches
3703d2c0a235SAndy Whitcroft		# If there were whitespace errors which cleanpatch can fix
3704d2c0a235SAndy Whitcroft		# then suggest that.
3705d2c0a235SAndy Whitcroft		if ($rpt_cleaners) {
3706d2c0a235SAndy Whitcroft			print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n";
3707d2c0a235SAndy Whitcroft			print "      scripts/cleanfile\n\n";
3708b0781216SMike Frysinger			$rpt_cleaners = 0;
3709d2c0a235SAndy Whitcroft		}
3710d2c0a235SAndy Whitcroft	}
3711d2c0a235SAndy Whitcroft
371211232688SArtem Bityutskiy	if ($quiet == 0 && keys %ignore_type) {
3713000d1cc1SJoe Perches	    print "NOTE: Ignored message types:";
3714000d1cc1SJoe Perches	    foreach my $ignore (sort keys %ignore_type) {
3715000d1cc1SJoe Perches		print " $ignore";
3716000d1cc1SJoe Perches	    }
371711232688SArtem Bityutskiy	    print "\n\n";
3718000d1cc1SJoe Perches	}
3719000d1cc1SJoe Perches
37200a920b5bSAndy Whitcroft	if ($clean == 1 && $quiet == 0) {
3721c2fdda0dSAndy Whitcroft		print "$vname has no obvious style problems and is ready for submission.\n"
37220a920b5bSAndy Whitcroft	}
37230a920b5bSAndy Whitcroft	if ($clean == 0 && $quiet == 0) {
3724000d1cc1SJoe Perches		print << "EOM";
3725000d1cc1SJoe Perches$vname has style problems, please review.
3726000d1cc1SJoe Perches
3727000d1cc1SJoe PerchesIf any of these errors are false positives, please report
3728000d1cc1SJoe Perchesthem to the maintainer, see CHECKPATCH in MAINTAINERS.
3729000d1cc1SJoe PerchesEOM
37300a920b5bSAndy Whitcroft	}
373113214adfSAndy Whitcroft
37320a920b5bSAndy Whitcroft	return $clean;
37330a920b5bSAndy Whitcroft}
3734