Statistics
| Branch: | Revision:

root / scripts / checkpatch.pl @ f53ec699

History | View | Annotate | Download (78.6 kB)

1 1ec3f6f9 Blue Swirl
#!/usr/bin/perl -w
2 1ec3f6f9 Blue Swirl
# (c) 2001, Dave Jones. (the file handling bit)
3 1ec3f6f9 Blue Swirl
# (c) 2005, Joel Schopp <jschopp@austin.ibm.com> (the ugly bit)
4 1ec3f6f9 Blue Swirl
# (c) 2007,2008, Andy Whitcroft <apw@uk.ibm.com> (new conditions, test suite)
5 1ec3f6f9 Blue Swirl
# (c) 2008-2010 Andy Whitcroft <apw@canonical.com>
6 1ec3f6f9 Blue Swirl
# Licensed under the terms of the GNU GPL License version 2
7 1ec3f6f9 Blue Swirl
8 1ec3f6f9 Blue Swirl
use strict;
9 1ec3f6f9 Blue Swirl
10 1ec3f6f9 Blue Swirl
my $P = $0;
11 1ec3f6f9 Blue Swirl
$P =~ s@.*/@@g;
12 1ec3f6f9 Blue Swirl
13 1ec3f6f9 Blue Swirl
my $V = '0.31';
14 1ec3f6f9 Blue Swirl
15 1ec3f6f9 Blue Swirl
use Getopt::Long qw(:config no_auto_abbrev);
16 1ec3f6f9 Blue Swirl
17 1ec3f6f9 Blue Swirl
my $quiet = 0;
18 1ec3f6f9 Blue Swirl
my $tree = 1;
19 1ec3f6f9 Blue Swirl
my $chk_signoff = 1;
20 1ec3f6f9 Blue Swirl
my $chk_patch = 1;
21 1ec3f6f9 Blue Swirl
my $tst_only;
22 1ec3f6f9 Blue Swirl
my $emacs = 0;
23 1ec3f6f9 Blue Swirl
my $terse = 0;
24 1ec3f6f9 Blue Swirl
my $file = 0;
25 1ec3f6f9 Blue Swirl
my $check = 0;
26 1ec3f6f9 Blue Swirl
my $summary = 1;
27 1ec3f6f9 Blue Swirl
my $mailback = 0;
28 1ec3f6f9 Blue Swirl
my $summary_file = 0;
29 1ec3f6f9 Blue Swirl
my $root;
30 1ec3f6f9 Blue Swirl
my %debug;
31 1ec3f6f9 Blue Swirl
my $help = 0;
32 1ec3f6f9 Blue Swirl
33 1ec3f6f9 Blue Swirl
sub help {
34 1ec3f6f9 Blue Swirl
	my ($exitcode) = @_;
35 1ec3f6f9 Blue Swirl
36 1ec3f6f9 Blue Swirl
	print << "EOM";
37 1ec3f6f9 Blue Swirl
Usage: $P [OPTION]... [FILE]...
38 1ec3f6f9 Blue Swirl
Version: $V
39 1ec3f6f9 Blue Swirl
40 1ec3f6f9 Blue Swirl
Options:
41 1ec3f6f9 Blue Swirl
  -q, --quiet                quiet
42 1ec3f6f9 Blue Swirl
  --no-tree                  run without a kernel tree
43 1ec3f6f9 Blue Swirl
  --no-signoff               do not check for 'Signed-off-by' line
44 1ec3f6f9 Blue Swirl
  --patch                    treat FILE as patchfile (default)
45 1ec3f6f9 Blue Swirl
  --emacs                    emacs compile window format
46 1ec3f6f9 Blue Swirl
  --terse                    one line per report
47 1ec3f6f9 Blue Swirl
  -f, --file                 treat FILE as regular source file
48 1ec3f6f9 Blue Swirl
  --subjective, --strict     enable more subjective tests
49 1ec3f6f9 Blue Swirl
  --root=PATH                PATH to the kernel tree root
50 1ec3f6f9 Blue Swirl
  --no-summary               suppress the per-file summary
51 1ec3f6f9 Blue Swirl
  --mailback                 only produce a report in case of warnings/errors
52 1ec3f6f9 Blue Swirl
  --summary-file             include the filename in summary
53 1ec3f6f9 Blue Swirl
  --debug KEY=[0|1]          turn on/off debugging of KEY, where KEY is one of
54 1ec3f6f9 Blue Swirl
                             'values', 'possible', 'type', and 'attr' (default
55 1ec3f6f9 Blue Swirl
                             is all off)
56 1ec3f6f9 Blue Swirl
  --test-only=WORD           report only warnings/errors containing WORD
57 1ec3f6f9 Blue Swirl
                             literally
58 1ec3f6f9 Blue Swirl
  -h, --help, --version      display this help and exit
59 1ec3f6f9 Blue Swirl
60 1ec3f6f9 Blue Swirl
When FILE is - read standard input.
61 1ec3f6f9 Blue Swirl
EOM
62 1ec3f6f9 Blue Swirl
63 1ec3f6f9 Blue Swirl
	exit($exitcode);
64 1ec3f6f9 Blue Swirl
}
65 1ec3f6f9 Blue Swirl
66 1ec3f6f9 Blue Swirl
GetOptions(
67 1ec3f6f9 Blue Swirl
	'q|quiet+'	=> \$quiet,
68 1ec3f6f9 Blue Swirl
	'tree!'		=> \$tree,
69 1ec3f6f9 Blue Swirl
	'signoff!'	=> \$chk_signoff,
70 1ec3f6f9 Blue Swirl
	'patch!'	=> \$chk_patch,
71 1ec3f6f9 Blue Swirl
	'emacs!'	=> \$emacs,
72 1ec3f6f9 Blue Swirl
	'terse!'	=> \$terse,
73 1ec3f6f9 Blue Swirl
	'f|file!'	=> \$file,
74 1ec3f6f9 Blue Swirl
	'subjective!'	=> \$check,
75 1ec3f6f9 Blue Swirl
	'strict!'	=> \$check,
76 1ec3f6f9 Blue Swirl
	'root=s'	=> \$root,
77 1ec3f6f9 Blue Swirl
	'summary!'	=> \$summary,
78 1ec3f6f9 Blue Swirl
	'mailback!'	=> \$mailback,
79 1ec3f6f9 Blue Swirl
	'summary-file!'	=> \$summary_file,
80 1ec3f6f9 Blue Swirl
81 1ec3f6f9 Blue Swirl
	'debug=s'	=> \%debug,
82 1ec3f6f9 Blue Swirl
	'test-only=s'	=> \$tst_only,
83 1ec3f6f9 Blue Swirl
	'h|help'	=> \$help,
84 1ec3f6f9 Blue Swirl
	'version'	=> \$help
85 1ec3f6f9 Blue Swirl
) or help(1);
86 1ec3f6f9 Blue Swirl
87 1ec3f6f9 Blue Swirl
help(0) if ($help);
88 1ec3f6f9 Blue Swirl
89 1ec3f6f9 Blue Swirl
my $exit = 0;
90 1ec3f6f9 Blue Swirl
91 1ec3f6f9 Blue Swirl
if ($#ARGV < 0) {
92 1ec3f6f9 Blue Swirl
	print "$P: no input files\n";
93 1ec3f6f9 Blue Swirl
	exit(1);
94 1ec3f6f9 Blue Swirl
}
95 1ec3f6f9 Blue Swirl
96 1ec3f6f9 Blue Swirl
my $dbg_values = 0;
97 1ec3f6f9 Blue Swirl
my $dbg_possible = 0;
98 1ec3f6f9 Blue Swirl
my $dbg_type = 0;
99 1ec3f6f9 Blue Swirl
my $dbg_attr = 0;
100 a99ac041 Don Slutz
my $dbg_adv_dcs = 0;
101 5424302e Don Slutz
my $dbg_adv_checking = 0;
102 69402a69 Don Slutz
my $dbg_adv_apw = 0;
103 1ec3f6f9 Blue Swirl
for my $key (keys %debug) {
104 1ec3f6f9 Blue Swirl
	## no critic
105 1ec3f6f9 Blue Swirl
	eval "\${dbg_$key} = '$debug{$key}';";
106 1ec3f6f9 Blue Swirl
	die "$@" if ($@);
107 1ec3f6f9 Blue Swirl
}
108 1ec3f6f9 Blue Swirl
109 1ec3f6f9 Blue Swirl
my $rpt_cleaners = 0;
110 1ec3f6f9 Blue Swirl
111 1ec3f6f9 Blue Swirl
if ($terse) {
112 1ec3f6f9 Blue Swirl
	$emacs = 1;
113 1ec3f6f9 Blue Swirl
	$quiet++;
114 1ec3f6f9 Blue Swirl
}
115 1ec3f6f9 Blue Swirl
116 1ec3f6f9 Blue Swirl
if ($tree) {
117 1ec3f6f9 Blue Swirl
	if (defined $root) {
118 1ec3f6f9 Blue Swirl
		if (!top_of_kernel_tree($root)) {
119 1ec3f6f9 Blue Swirl
			die "$P: $root: --root does not point at a valid tree\n";
120 1ec3f6f9 Blue Swirl
		}
121 1ec3f6f9 Blue Swirl
	} else {
122 1ec3f6f9 Blue Swirl
		if (top_of_kernel_tree('.')) {
123 1ec3f6f9 Blue Swirl
			$root = '.';
124 1ec3f6f9 Blue Swirl
		} elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
125 1ec3f6f9 Blue Swirl
						top_of_kernel_tree($1)) {
126 1ec3f6f9 Blue Swirl
			$root = $1;
127 1ec3f6f9 Blue Swirl
		}
128 1ec3f6f9 Blue Swirl
	}
129 1ec3f6f9 Blue Swirl
130 1ec3f6f9 Blue Swirl
	if (!defined $root) {
131 1ec3f6f9 Blue Swirl
		print "Must be run from the top-level dir. of a kernel tree\n";
132 1ec3f6f9 Blue Swirl
		exit(2);
133 1ec3f6f9 Blue Swirl
	}
134 1ec3f6f9 Blue Swirl
}
135 1ec3f6f9 Blue Swirl
136 1ec3f6f9 Blue Swirl
my $emitted_corrupt = 0;
137 1ec3f6f9 Blue Swirl
138 1ec3f6f9 Blue Swirl
our $Ident	= qr{
139 1ec3f6f9 Blue Swirl
			[A-Za-z_][A-Za-z\d_]*
140 1ec3f6f9 Blue Swirl
			(?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
141 1ec3f6f9 Blue Swirl
		}x;
142 1ec3f6f9 Blue Swirl
our $Storage	= qr{extern|static|asmlinkage};
143 1ec3f6f9 Blue Swirl
our $Sparse	= qr{
144 1ec3f6f9 Blue Swirl
			__user|
145 1ec3f6f9 Blue Swirl
			__kernel|
146 1ec3f6f9 Blue Swirl
			__force|
147 1ec3f6f9 Blue Swirl
			__iomem|
148 1ec3f6f9 Blue Swirl
			__must_check|
149 1ec3f6f9 Blue Swirl
			__init_refok|
150 1ec3f6f9 Blue Swirl
			__kprobes|
151 1ec3f6f9 Blue Swirl
			__ref
152 1ec3f6f9 Blue Swirl
		}x;
153 1ec3f6f9 Blue Swirl
154 1ec3f6f9 Blue Swirl
# Notes to $Attribute:
155 1ec3f6f9 Blue Swirl
# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
156 1ec3f6f9 Blue Swirl
our $Attribute	= qr{
157 1ec3f6f9 Blue Swirl
			const|
158 1ec3f6f9 Blue Swirl
			__percpu|
159 1ec3f6f9 Blue Swirl
			__nocast|
160 1ec3f6f9 Blue Swirl
			__safe|
161 1ec3f6f9 Blue Swirl
			__bitwise__|
162 1ec3f6f9 Blue Swirl
			__packed__|
163 1ec3f6f9 Blue Swirl
			__packed2__|
164 1ec3f6f9 Blue Swirl
			__naked|
165 1ec3f6f9 Blue Swirl
			__maybe_unused|
166 1ec3f6f9 Blue Swirl
			__always_unused|
167 1ec3f6f9 Blue Swirl
			__noreturn|
168 1ec3f6f9 Blue Swirl
			__used|
169 1ec3f6f9 Blue Swirl
			__cold|
170 1ec3f6f9 Blue Swirl
			__noclone|
171 1ec3f6f9 Blue Swirl
			__deprecated|
172 1ec3f6f9 Blue Swirl
			__read_mostly|
173 1ec3f6f9 Blue Swirl
			__kprobes|
174 1ec3f6f9 Blue Swirl
			__(?:mem|cpu|dev|)(?:initdata|initconst|init\b)|
175 1ec3f6f9 Blue Swirl
			____cacheline_aligned|
176 1ec3f6f9 Blue Swirl
			____cacheline_aligned_in_smp|
177 1ec3f6f9 Blue Swirl
			____cacheline_internodealigned_in_smp|
178 1ec3f6f9 Blue Swirl
			__weak
179 1ec3f6f9 Blue Swirl
		  }x;
180 1ec3f6f9 Blue Swirl
our $Modifier;
181 1ec3f6f9 Blue Swirl
our $Inline	= qr{inline|__always_inline|noinline};
182 1ec3f6f9 Blue Swirl
our $Member	= qr{->$Ident|\.$Ident|\[[^]]*\]};
183 1ec3f6f9 Blue Swirl
our $Lval	= qr{$Ident(?:$Member)*};
184 1ec3f6f9 Blue Swirl
185 1ec3f6f9 Blue Swirl
our $Constant	= qr{(?:[0-9]+|0x[0-9a-fA-F]+)[UL]*};
186 1ec3f6f9 Blue Swirl
our $Assignment	= qr{(?:\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=)};
187 1ec3f6f9 Blue Swirl
our $Compare    = qr{<=|>=|==|!=|<|>};
188 1ec3f6f9 Blue Swirl
our $Operators	= qr{
189 1ec3f6f9 Blue Swirl
			<=|>=|==|!=|
190 1ec3f6f9 Blue Swirl
			=>|->|<<|>>|<|>|!|~|
191 1ec3f6f9 Blue Swirl
			&&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%
192 1ec3f6f9 Blue Swirl
		  }x;
193 1ec3f6f9 Blue Swirl
194 1ec3f6f9 Blue Swirl
our $NonptrType;
195 1ec3f6f9 Blue Swirl
our $Type;
196 1ec3f6f9 Blue Swirl
our $Declare;
197 1ec3f6f9 Blue Swirl
198 1ec3f6f9 Blue Swirl
our $UTF8	= qr {
199 1ec3f6f9 Blue Swirl
	[\x09\x0A\x0D\x20-\x7E]              # ASCII
200 1ec3f6f9 Blue Swirl
	| [\xC2-\xDF][\x80-\xBF]             # non-overlong 2-byte
201 1ec3f6f9 Blue Swirl
	|  \xE0[\xA0-\xBF][\x80-\xBF]        # excluding overlongs
202 1ec3f6f9 Blue Swirl
	| [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}  # straight 3-byte
203 1ec3f6f9 Blue Swirl
	|  \xED[\x80-\x9F][\x80-\xBF]        # excluding surrogates
204 1ec3f6f9 Blue Swirl
	|  \xF0[\x90-\xBF][\x80-\xBF]{2}     # planes 1-3
205 1ec3f6f9 Blue Swirl
	| [\xF1-\xF3][\x80-\xBF]{3}          # planes 4-15
206 1ec3f6f9 Blue Swirl
	|  \xF4[\x80-\x8F][\x80-\xBF]{2}     # plane 16
207 1ec3f6f9 Blue Swirl
}x;
208 1ec3f6f9 Blue Swirl
209 1ec3f6f9 Blue Swirl
our $typeTypedefs = qr{(?x:
210 1ec3f6f9 Blue Swirl
	(?:__)?(?:u|s|be|le)(?:8|16|32|64)|
211 1ec3f6f9 Blue Swirl
	atomic_t
212 1ec3f6f9 Blue Swirl
)};
213 1ec3f6f9 Blue Swirl
214 1ec3f6f9 Blue Swirl
our $logFunctions = qr{(?x:
215 1ec3f6f9 Blue Swirl
	printk|
216 1ec3f6f9 Blue Swirl
	pr_(debug|dbg|vdbg|devel|info|warning|err|notice|alert|crit|emerg|cont)|
217 1ec3f6f9 Blue Swirl
	(dev|netdev|netif)_(printk|dbg|vdbg|info|warn|err|notice|alert|crit|emerg|WARN)|
218 1ec3f6f9 Blue Swirl
	WARN|
219 1ec3f6f9 Blue Swirl
	panic
220 1ec3f6f9 Blue Swirl
)};
221 1ec3f6f9 Blue Swirl
222 1ec3f6f9 Blue Swirl
our @typeList = (
223 1ec3f6f9 Blue Swirl
	qr{void},
224 1ec3f6f9 Blue Swirl
	qr{(?:unsigned\s+)?char},
225 1ec3f6f9 Blue Swirl
	qr{(?:unsigned\s+)?short},
226 1ec3f6f9 Blue Swirl
	qr{(?:unsigned\s+)?int},
227 1ec3f6f9 Blue Swirl
	qr{(?:unsigned\s+)?long},
228 1ec3f6f9 Blue Swirl
	qr{(?:unsigned\s+)?long\s+int},
229 1ec3f6f9 Blue Swirl
	qr{(?:unsigned\s+)?long\s+long},
230 1ec3f6f9 Blue Swirl
	qr{(?:unsigned\s+)?long\s+long\s+int},
231 1ec3f6f9 Blue Swirl
	qr{unsigned},
232 1ec3f6f9 Blue Swirl
	qr{float},
233 1ec3f6f9 Blue Swirl
	qr{double},
234 1ec3f6f9 Blue Swirl
	qr{bool},
235 1ec3f6f9 Blue Swirl
	qr{struct\s+$Ident},
236 1ec3f6f9 Blue Swirl
	qr{union\s+$Ident},
237 1ec3f6f9 Blue Swirl
	qr{enum\s+$Ident},
238 1ec3f6f9 Blue Swirl
	qr{${Ident}_t},
239 1ec3f6f9 Blue Swirl
	qr{${Ident}_handler},
240 1ec3f6f9 Blue Swirl
	qr{${Ident}_handler_fn},
241 1ec3f6f9 Blue Swirl
);
242 1ec3f6f9 Blue Swirl
our @modifierList = (
243 1ec3f6f9 Blue Swirl
	qr{fastcall},
244 1ec3f6f9 Blue Swirl
);
245 1ec3f6f9 Blue Swirl
246 1ec3f6f9 Blue Swirl
our $allowed_asm_includes = qr{(?x:
247 1ec3f6f9 Blue Swirl
	irq|
248 1ec3f6f9 Blue Swirl
	memory
249 1ec3f6f9 Blue Swirl
)};
250 1ec3f6f9 Blue Swirl
# memory.h: ARM has a custom one
251 1ec3f6f9 Blue Swirl
252 1ec3f6f9 Blue Swirl
sub build_types {
253 1ec3f6f9 Blue Swirl
	my $mods = "(?x:  \n" . join("|\n  ", @modifierList) . "\n)";
254 1ec3f6f9 Blue Swirl
	my $all = "(?x:  \n" . join("|\n  ", @typeList) . "\n)";
255 1ec3f6f9 Blue Swirl
	$Modifier	= qr{(?:$Attribute|$Sparse|$mods)};
256 1ec3f6f9 Blue Swirl
	$NonptrType	= qr{
257 1ec3f6f9 Blue Swirl
			(?:$Modifier\s+|const\s+)*
258 1ec3f6f9 Blue Swirl
			(?:
259 1ec3f6f9 Blue Swirl
				(?:typeof|__typeof__)\s*\(\s*\**\s*$Ident\s*\)|
260 1ec3f6f9 Blue Swirl
				(?:$typeTypedefs\b)|
261 1ec3f6f9 Blue Swirl
				(?:${all}\b)
262 1ec3f6f9 Blue Swirl
			)
263 1ec3f6f9 Blue Swirl
			(?:\s+$Modifier|\s+const)*
264 1ec3f6f9 Blue Swirl
		  }x;
265 1ec3f6f9 Blue Swirl
	$Type	= qr{
266 1ec3f6f9 Blue Swirl
			$NonptrType
267 1ec3f6f9 Blue Swirl
			(?:[\s\*]+\s*const|[\s\*]+|(?:\s*\[\s*\])+)?
268 1ec3f6f9 Blue Swirl
			(?:\s+$Inline|\s+$Modifier)*
269 1ec3f6f9 Blue Swirl
		  }x;
270 1ec3f6f9 Blue Swirl
	$Declare	= qr{(?:$Storage\s+)?$Type};
271 1ec3f6f9 Blue Swirl
}
272 1ec3f6f9 Blue Swirl
build_types();
273 1ec3f6f9 Blue Swirl
274 1ec3f6f9 Blue Swirl
$chk_signoff = 0 if ($file);
275 1ec3f6f9 Blue Swirl
276 1ec3f6f9 Blue Swirl
my @dep_includes = ();
277 1ec3f6f9 Blue Swirl
my @dep_functions = ();
278 1ec3f6f9 Blue Swirl
my $removal = "Documentation/feature-removal-schedule.txt";
279 1ec3f6f9 Blue Swirl
if ($tree && -f "$root/$removal") {
280 1ec3f6f9 Blue Swirl
	open(my $REMOVE, '<', "$root/$removal") ||
281 1ec3f6f9 Blue Swirl
				die "$P: $removal: open failed - $!\n";
282 1ec3f6f9 Blue Swirl
	while (<$REMOVE>) {
283 1ec3f6f9 Blue Swirl
		if (/^Check:\s+(.*\S)/) {
284 1ec3f6f9 Blue Swirl
			for my $entry (split(/[, ]+/, $1)) {
285 1ec3f6f9 Blue Swirl
				if ($entry =~ m@include/(.*)@) {
286 1ec3f6f9 Blue Swirl
					push(@dep_includes, $1);
287 1ec3f6f9 Blue Swirl
288 1ec3f6f9 Blue Swirl
				} elsif ($entry !~ m@/@) {
289 1ec3f6f9 Blue Swirl
					push(@dep_functions, $entry);
290 1ec3f6f9 Blue Swirl
				}
291 1ec3f6f9 Blue Swirl
			}
292 1ec3f6f9 Blue Swirl
		}
293 1ec3f6f9 Blue Swirl
	}
294 1ec3f6f9 Blue Swirl
	close($REMOVE);
295 1ec3f6f9 Blue Swirl
}
296 1ec3f6f9 Blue Swirl
297 1ec3f6f9 Blue Swirl
my @rawlines = ();
298 1ec3f6f9 Blue Swirl
my @lines = ();
299 1ec3f6f9 Blue Swirl
my $vname;
300 1ec3f6f9 Blue Swirl
for my $filename (@ARGV) {
301 1ec3f6f9 Blue Swirl
	my $FILE;
302 1ec3f6f9 Blue Swirl
	if ($file) {
303 1ec3f6f9 Blue Swirl
		open($FILE, '-|', "diff -u /dev/null $filename") ||
304 1ec3f6f9 Blue Swirl
			die "$P: $filename: diff failed - $!\n";
305 1ec3f6f9 Blue Swirl
	} elsif ($filename eq '-') {
306 1ec3f6f9 Blue Swirl
		open($FILE, '<&STDIN');
307 1ec3f6f9 Blue Swirl
	} else {
308 1ec3f6f9 Blue Swirl
		open($FILE, '<', "$filename") ||
309 1ec3f6f9 Blue Swirl
			die "$P: $filename: open failed - $!\n";
310 1ec3f6f9 Blue Swirl
	}
311 1ec3f6f9 Blue Swirl
	if ($filename eq '-') {
312 1ec3f6f9 Blue Swirl
		$vname = 'Your patch';
313 1ec3f6f9 Blue Swirl
	} else {
314 1ec3f6f9 Blue Swirl
		$vname = $filename;
315 1ec3f6f9 Blue Swirl
	}
316 1ec3f6f9 Blue Swirl
	while (<$FILE>) {
317 1ec3f6f9 Blue Swirl
		chomp;
318 1ec3f6f9 Blue Swirl
		push(@rawlines, $_);
319 1ec3f6f9 Blue Swirl
	}
320 1ec3f6f9 Blue Swirl
	close($FILE);
321 1ec3f6f9 Blue Swirl
	if (!process($filename)) {
322 1ec3f6f9 Blue Swirl
		$exit = 1;
323 1ec3f6f9 Blue Swirl
	}
324 1ec3f6f9 Blue Swirl
	@rawlines = ();
325 1ec3f6f9 Blue Swirl
	@lines = ();
326 1ec3f6f9 Blue Swirl
}
327 1ec3f6f9 Blue Swirl
328 1ec3f6f9 Blue Swirl
exit($exit);
329 1ec3f6f9 Blue Swirl
330 1ec3f6f9 Blue Swirl
sub top_of_kernel_tree {
331 1ec3f6f9 Blue Swirl
	my ($root) = @_;
332 1ec3f6f9 Blue Swirl
333 1ec3f6f9 Blue Swirl
	my @tree_check = (
334 b6469683 Blue Swirl
		"COPYING", "MAINTAINERS", "Makefile",
335 b6469683 Blue Swirl
		"README", "docs", "VERSION",
336 b6469683 Blue Swirl
		"vl.c"
337 1ec3f6f9 Blue Swirl
	);
338 1ec3f6f9 Blue Swirl
339 1ec3f6f9 Blue Swirl
	foreach my $check (@tree_check) {
340 1ec3f6f9 Blue Swirl
		if (! -e $root . '/' . $check) {
341 1ec3f6f9 Blue Swirl
			return 0;
342 1ec3f6f9 Blue Swirl
		}
343 1ec3f6f9 Blue Swirl
	}
344 1ec3f6f9 Blue Swirl
	return 1;
345 1ec3f6f9 Blue Swirl
}
346 1ec3f6f9 Blue Swirl
347 1ec3f6f9 Blue Swirl
sub expand_tabs {
348 1ec3f6f9 Blue Swirl
	my ($str) = @_;
349 1ec3f6f9 Blue Swirl
350 1ec3f6f9 Blue Swirl
	my $res = '';
351 1ec3f6f9 Blue Swirl
	my $n = 0;
352 1ec3f6f9 Blue Swirl
	for my $c (split(//, $str)) {
353 1ec3f6f9 Blue Swirl
		if ($c eq "\t") {
354 1ec3f6f9 Blue Swirl
			$res .= ' ';
355 1ec3f6f9 Blue Swirl
			$n++;
356 1ec3f6f9 Blue Swirl
			for (; ($n % 8) != 0; $n++) {
357 1ec3f6f9 Blue Swirl
				$res .= ' ';
358 1ec3f6f9 Blue Swirl
			}
359 1ec3f6f9 Blue Swirl
			next;
360 1ec3f6f9 Blue Swirl
		}
361 1ec3f6f9 Blue Swirl
		$res .= $c;
362 1ec3f6f9 Blue Swirl
		$n++;
363 1ec3f6f9 Blue Swirl
	}
364 1ec3f6f9 Blue Swirl
365 1ec3f6f9 Blue Swirl
	return $res;
366 1ec3f6f9 Blue Swirl
}
367 1ec3f6f9 Blue Swirl
sub copy_spacing {
368 1ec3f6f9 Blue Swirl
	(my $res = shift) =~ tr/\t/ /c;
369 1ec3f6f9 Blue Swirl
	return $res;
370 1ec3f6f9 Blue Swirl
}
371 1ec3f6f9 Blue Swirl
372 1ec3f6f9 Blue Swirl
sub line_stats {
373 1ec3f6f9 Blue Swirl
	my ($line) = @_;
374 1ec3f6f9 Blue Swirl
375 1ec3f6f9 Blue Swirl
	# Drop the diff line leader and expand tabs
376 1ec3f6f9 Blue Swirl
	$line =~ s/^.//;
377 1ec3f6f9 Blue Swirl
	$line = expand_tabs($line);
378 1ec3f6f9 Blue Swirl
379 1ec3f6f9 Blue Swirl
	# Pick the indent from the front of the line.
380 1ec3f6f9 Blue Swirl
	my ($white) = ($line =~ /^(\s*)/);
381 1ec3f6f9 Blue Swirl
382 1ec3f6f9 Blue Swirl
	return (length($line), length($white));
383 1ec3f6f9 Blue Swirl
}
384 1ec3f6f9 Blue Swirl
385 1ec3f6f9 Blue Swirl
my $sanitise_quote = '';
386 1ec3f6f9 Blue Swirl
387 1ec3f6f9 Blue Swirl
sub sanitise_line_reset {
388 1ec3f6f9 Blue Swirl
	my ($in_comment) = @_;
389 1ec3f6f9 Blue Swirl
390 1ec3f6f9 Blue Swirl
	if ($in_comment) {
391 1ec3f6f9 Blue Swirl
		$sanitise_quote = '*/';
392 1ec3f6f9 Blue Swirl
	} else {
393 1ec3f6f9 Blue Swirl
		$sanitise_quote = '';
394 1ec3f6f9 Blue Swirl
	}
395 1ec3f6f9 Blue Swirl
}
396 1ec3f6f9 Blue Swirl
sub sanitise_line {
397 1ec3f6f9 Blue Swirl
	my ($line) = @_;
398 1ec3f6f9 Blue Swirl
399 1ec3f6f9 Blue Swirl
	my $res = '';
400 1ec3f6f9 Blue Swirl
	my $l = '';
401 1ec3f6f9 Blue Swirl
402 1ec3f6f9 Blue Swirl
	my $qlen = 0;
403 1ec3f6f9 Blue Swirl
	my $off = 0;
404 1ec3f6f9 Blue Swirl
	my $c;
405 1ec3f6f9 Blue Swirl
406 1ec3f6f9 Blue Swirl
	# Always copy over the diff marker.
407 1ec3f6f9 Blue Swirl
	$res = substr($line, 0, 1);
408 1ec3f6f9 Blue Swirl
409 1ec3f6f9 Blue Swirl
	for ($off = 1; $off < length($line); $off++) {
410 1ec3f6f9 Blue Swirl
		$c = substr($line, $off, 1);
411 1ec3f6f9 Blue Swirl
412 1ec3f6f9 Blue Swirl
		# Comments we are wacking completly including the begin
413 1ec3f6f9 Blue Swirl
		# and end, all to $;.
414 1ec3f6f9 Blue Swirl
		if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
415 1ec3f6f9 Blue Swirl
			$sanitise_quote = '*/';
416 1ec3f6f9 Blue Swirl
417 1ec3f6f9 Blue Swirl
			substr($res, $off, 2, "$;$;");
418 1ec3f6f9 Blue Swirl
			$off++;
419 1ec3f6f9 Blue Swirl
			next;
420 1ec3f6f9 Blue Swirl
		}
421 1ec3f6f9 Blue Swirl
		if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
422 1ec3f6f9 Blue Swirl
			$sanitise_quote = '';
423 1ec3f6f9 Blue Swirl
			substr($res, $off, 2, "$;$;");
424 1ec3f6f9 Blue Swirl
			$off++;
425 1ec3f6f9 Blue Swirl
			next;
426 1ec3f6f9 Blue Swirl
		}
427 1ec3f6f9 Blue Swirl
		if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
428 1ec3f6f9 Blue Swirl
			$sanitise_quote = '//';
429 1ec3f6f9 Blue Swirl
430 1ec3f6f9 Blue Swirl
			substr($res, $off, 2, $sanitise_quote);
431 1ec3f6f9 Blue Swirl
			$off++;
432 1ec3f6f9 Blue Swirl
			next;
433 1ec3f6f9 Blue Swirl
		}
434 1ec3f6f9 Blue Swirl
435 1ec3f6f9 Blue Swirl
		# A \ in a string means ignore the next character.
436 1ec3f6f9 Blue Swirl
		if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
437 1ec3f6f9 Blue Swirl
		    $c eq "\\") {
438 1ec3f6f9 Blue Swirl
			substr($res, $off, 2, 'XX');
439 1ec3f6f9 Blue Swirl
			$off++;
440 1ec3f6f9 Blue Swirl
			next;
441 1ec3f6f9 Blue Swirl
		}
442 1ec3f6f9 Blue Swirl
		# Regular quotes.
443 1ec3f6f9 Blue Swirl
		if ($c eq "'" || $c eq '"') {
444 1ec3f6f9 Blue Swirl
			if ($sanitise_quote eq '') {
445 1ec3f6f9 Blue Swirl
				$sanitise_quote = $c;
446 1ec3f6f9 Blue Swirl
447 1ec3f6f9 Blue Swirl
				substr($res, $off, 1, $c);
448 1ec3f6f9 Blue Swirl
				next;
449 1ec3f6f9 Blue Swirl
			} elsif ($sanitise_quote eq $c) {
450 1ec3f6f9 Blue Swirl
				$sanitise_quote = '';
451 1ec3f6f9 Blue Swirl
			}
452 1ec3f6f9 Blue Swirl
		}
453 1ec3f6f9 Blue Swirl
454 1ec3f6f9 Blue Swirl
		#print "c<$c> SQ<$sanitise_quote>\n";
455 1ec3f6f9 Blue Swirl
		if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
456 1ec3f6f9 Blue Swirl
			substr($res, $off, 1, $;);
457 1ec3f6f9 Blue Swirl
		} elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
458 1ec3f6f9 Blue Swirl
			substr($res, $off, 1, $;);
459 1ec3f6f9 Blue Swirl
		} elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
460 1ec3f6f9 Blue Swirl
			substr($res, $off, 1, 'X');
461 1ec3f6f9 Blue Swirl
		} else {
462 1ec3f6f9 Blue Swirl
			substr($res, $off, 1, $c);
463 1ec3f6f9 Blue Swirl
		}
464 1ec3f6f9 Blue Swirl
	}
465 1ec3f6f9 Blue Swirl
466 1ec3f6f9 Blue Swirl
	if ($sanitise_quote eq '//') {
467 1ec3f6f9 Blue Swirl
		$sanitise_quote = '';
468 1ec3f6f9 Blue Swirl
	}
469 1ec3f6f9 Blue Swirl
470 1ec3f6f9 Blue Swirl
	# The pathname on a #include may be surrounded by '<' and '>'.
471 1ec3f6f9 Blue Swirl
	if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
472 1ec3f6f9 Blue Swirl
		my $clean = 'X' x length($1);
473 1ec3f6f9 Blue Swirl
		$res =~ s@\<.*\>@<$clean>@;
474 1ec3f6f9 Blue Swirl
475 1ec3f6f9 Blue Swirl
	# The whole of a #error is a string.
476 1ec3f6f9 Blue Swirl
	} elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
477 1ec3f6f9 Blue Swirl
		my $clean = 'X' x length($1);
478 1ec3f6f9 Blue Swirl
		$res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
479 1ec3f6f9 Blue Swirl
	}
480 1ec3f6f9 Blue Swirl
481 1ec3f6f9 Blue Swirl
	return $res;
482 1ec3f6f9 Blue Swirl
}
483 1ec3f6f9 Blue Swirl
484 1ec3f6f9 Blue Swirl
sub ctx_statement_block {
485 1ec3f6f9 Blue Swirl
	my ($linenr, $remain, $off) = @_;
486 1ec3f6f9 Blue Swirl
	my $line = $linenr - 1;
487 1ec3f6f9 Blue Swirl
	my $blk = '';
488 1ec3f6f9 Blue Swirl
	my $soff = $off;
489 1ec3f6f9 Blue Swirl
	my $coff = $off - 1;
490 1ec3f6f9 Blue Swirl
	my $coff_set = 0;
491 1ec3f6f9 Blue Swirl
492 1ec3f6f9 Blue Swirl
	my $loff = 0;
493 1ec3f6f9 Blue Swirl
494 1ec3f6f9 Blue Swirl
	my $type = '';
495 1ec3f6f9 Blue Swirl
	my $level = 0;
496 1ec3f6f9 Blue Swirl
	my @stack = ();
497 1ec3f6f9 Blue Swirl
	my $p;
498 1ec3f6f9 Blue Swirl
	my $c;
499 1ec3f6f9 Blue Swirl
	my $len = 0;
500 1ec3f6f9 Blue Swirl
501 1ec3f6f9 Blue Swirl
	my $remainder;
502 1ec3f6f9 Blue Swirl
	while (1) {
503 1ec3f6f9 Blue Swirl
		@stack = (['', 0]) if ($#stack == -1);
504 1ec3f6f9 Blue Swirl
505 1ec3f6f9 Blue Swirl
		#warn "CSB: blk<$blk> remain<$remain>\n";
506 1ec3f6f9 Blue Swirl
		# If we are about to drop off the end, pull in more
507 1ec3f6f9 Blue Swirl
		# context.
508 1ec3f6f9 Blue Swirl
		if ($off >= $len) {
509 1ec3f6f9 Blue Swirl
			for (; $remain > 0; $line++) {
510 1ec3f6f9 Blue Swirl
				last if (!defined $lines[$line]);
511 1ec3f6f9 Blue Swirl
				next if ($lines[$line] =~ /^-/);
512 1ec3f6f9 Blue Swirl
				$remain--;
513 1ec3f6f9 Blue Swirl
				$loff = $len;
514 1ec3f6f9 Blue Swirl
				$blk .= $lines[$line] . "\n";
515 1ec3f6f9 Blue Swirl
				$len = length($blk);
516 1ec3f6f9 Blue Swirl
				$line++;
517 1ec3f6f9 Blue Swirl
				last;
518 1ec3f6f9 Blue Swirl
			}
519 1ec3f6f9 Blue Swirl
			# Bail if there is no further context.
520 1ec3f6f9 Blue Swirl
			#warn "CSB: blk<$blk> off<$off> len<$len>\n";
521 1ec3f6f9 Blue Swirl
			if ($off >= $len) {
522 1ec3f6f9 Blue Swirl
				last;
523 1ec3f6f9 Blue Swirl
			}
524 1ec3f6f9 Blue Swirl
		}
525 1ec3f6f9 Blue Swirl
		$p = $c;
526 1ec3f6f9 Blue Swirl
		$c = substr($blk, $off, 1);
527 1ec3f6f9 Blue Swirl
		$remainder = substr($blk, $off);
528 1ec3f6f9 Blue Swirl
529 1ec3f6f9 Blue Swirl
		#warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
530 1ec3f6f9 Blue Swirl
531 1ec3f6f9 Blue Swirl
		# Handle nested #if/#else.
532 1ec3f6f9 Blue Swirl
		if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
533 1ec3f6f9 Blue Swirl
			push(@stack, [ $type, $level ]);
534 1ec3f6f9 Blue Swirl
		} elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
535 1ec3f6f9 Blue Swirl
			($type, $level) = @{$stack[$#stack - 1]};
536 1ec3f6f9 Blue Swirl
		} elsif ($remainder =~ /^#\s*endif\b/) {
537 1ec3f6f9 Blue Swirl
			($type, $level) = @{pop(@stack)};
538 1ec3f6f9 Blue Swirl
		}
539 1ec3f6f9 Blue Swirl
540 1ec3f6f9 Blue Swirl
		# Statement ends at the ';' or a close '}' at the
541 1ec3f6f9 Blue Swirl
		# outermost level.
542 1ec3f6f9 Blue Swirl
		if ($level == 0 && $c eq ';') {
543 1ec3f6f9 Blue Swirl
			last;
544 1ec3f6f9 Blue Swirl
		}
545 1ec3f6f9 Blue Swirl
546 1ec3f6f9 Blue Swirl
		# An else is really a conditional as long as its not else if
547 1ec3f6f9 Blue Swirl
		if ($level == 0 && $coff_set == 0 &&
548 1ec3f6f9 Blue Swirl
				(!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
549 1ec3f6f9 Blue Swirl
				$remainder =~ /^(else)(?:\s|{)/ &&
550 1ec3f6f9 Blue Swirl
				$remainder !~ /^else\s+if\b/) {
551 1ec3f6f9 Blue Swirl
			$coff = $off + length($1) - 1;
552 1ec3f6f9 Blue Swirl
			$coff_set = 1;
553 1ec3f6f9 Blue Swirl
			#warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
554 1ec3f6f9 Blue Swirl
			#warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
555 1ec3f6f9 Blue Swirl
		}
556 1ec3f6f9 Blue Swirl
557 1ec3f6f9 Blue Swirl
		if (($type eq '' || $type eq '(') && $c eq '(') {
558 1ec3f6f9 Blue Swirl
			$level++;
559 1ec3f6f9 Blue Swirl
			$type = '(';
560 1ec3f6f9 Blue Swirl
		}
561 1ec3f6f9 Blue Swirl
		if ($type eq '(' && $c eq ')') {
562 1ec3f6f9 Blue Swirl
			$level--;
563 1ec3f6f9 Blue Swirl
			$type = ($level != 0)? '(' : '';
564 1ec3f6f9 Blue Swirl
565 1ec3f6f9 Blue Swirl
			if ($level == 0 && $coff < $soff) {
566 1ec3f6f9 Blue Swirl
				$coff = $off;
567 1ec3f6f9 Blue Swirl
				$coff_set = 1;
568 1ec3f6f9 Blue Swirl
				#warn "CSB: mark coff<$coff>\n";
569 1ec3f6f9 Blue Swirl
			}
570 1ec3f6f9 Blue Swirl
		}
571 1ec3f6f9 Blue Swirl
		if (($type eq '' || $type eq '{') && $c eq '{') {
572 1ec3f6f9 Blue Swirl
			$level++;
573 1ec3f6f9 Blue Swirl
			$type = '{';
574 1ec3f6f9 Blue Swirl
		}
575 1ec3f6f9 Blue Swirl
		if ($type eq '{' && $c eq '}') {
576 1ec3f6f9 Blue Swirl
			$level--;
577 1ec3f6f9 Blue Swirl
			$type = ($level != 0)? '{' : '';
578 1ec3f6f9 Blue Swirl
579 1ec3f6f9 Blue Swirl
			if ($level == 0) {
580 1ec3f6f9 Blue Swirl
				if (substr($blk, $off + 1, 1) eq ';') {
581 1ec3f6f9 Blue Swirl
					$off++;
582 1ec3f6f9 Blue Swirl
				}
583 1ec3f6f9 Blue Swirl
				last;
584 1ec3f6f9 Blue Swirl
			}
585 1ec3f6f9 Blue Swirl
		}
586 1ec3f6f9 Blue Swirl
		$off++;
587 1ec3f6f9 Blue Swirl
	}
588 1ec3f6f9 Blue Swirl
	# We are truly at the end, so shuffle to the next line.
589 1ec3f6f9 Blue Swirl
	if ($off == $len) {
590 1ec3f6f9 Blue Swirl
		$loff = $len + 1;
591 1ec3f6f9 Blue Swirl
		$line++;
592 1ec3f6f9 Blue Swirl
		$remain--;
593 1ec3f6f9 Blue Swirl
	}
594 1ec3f6f9 Blue Swirl
595 1ec3f6f9 Blue Swirl
	my $statement = substr($blk, $soff, $off - $soff + 1);
596 1ec3f6f9 Blue Swirl
	my $condition = substr($blk, $soff, $coff - $soff + 1);
597 1ec3f6f9 Blue Swirl
598 1ec3f6f9 Blue Swirl
	#warn "STATEMENT<$statement>\n";
599 1ec3f6f9 Blue Swirl
	#warn "CONDITION<$condition>\n";
600 1ec3f6f9 Blue Swirl
601 1ec3f6f9 Blue Swirl
	#print "coff<$coff> soff<$off> loff<$loff>\n";
602 1ec3f6f9 Blue Swirl
603 1ec3f6f9 Blue Swirl
	return ($statement, $condition,
604 1ec3f6f9 Blue Swirl
			$line, $remain + 1, $off - $loff + 1, $level);
605 1ec3f6f9 Blue Swirl
}
606 1ec3f6f9 Blue Swirl
607 1ec3f6f9 Blue Swirl
sub statement_lines {
608 1ec3f6f9 Blue Swirl
	my ($stmt) = @_;
609 1ec3f6f9 Blue Swirl
610 1ec3f6f9 Blue Swirl
	# Strip the diff line prefixes and rip blank lines at start and end.
611 1ec3f6f9 Blue Swirl
	$stmt =~ s/(^|\n)./$1/g;
612 1ec3f6f9 Blue Swirl
	$stmt =~ s/^\s*//;
613 1ec3f6f9 Blue Swirl
	$stmt =~ s/\s*$//;
614 1ec3f6f9 Blue Swirl
615 1ec3f6f9 Blue Swirl
	my @stmt_lines = ($stmt =~ /\n/g);
616 1ec3f6f9 Blue Swirl
617 1ec3f6f9 Blue Swirl
	return $#stmt_lines + 2;
618 1ec3f6f9 Blue Swirl
}
619 1ec3f6f9 Blue Swirl
620 1ec3f6f9 Blue Swirl
sub statement_rawlines {
621 1ec3f6f9 Blue Swirl
	my ($stmt) = @_;
622 1ec3f6f9 Blue Swirl
623 1ec3f6f9 Blue Swirl
	my @stmt_lines = ($stmt =~ /\n/g);
624 1ec3f6f9 Blue Swirl
625 1ec3f6f9 Blue Swirl
	return $#stmt_lines + 2;
626 1ec3f6f9 Blue Swirl
}
627 1ec3f6f9 Blue Swirl
628 1ec3f6f9 Blue Swirl
sub statement_block_size {
629 1ec3f6f9 Blue Swirl
	my ($stmt) = @_;
630 1ec3f6f9 Blue Swirl
631 1ec3f6f9 Blue Swirl
	$stmt =~ s/(^|\n)./$1/g;
632 1ec3f6f9 Blue Swirl
	$stmt =~ s/^\s*{//;
633 1ec3f6f9 Blue Swirl
	$stmt =~ s/}\s*$//;
634 1ec3f6f9 Blue Swirl
	$stmt =~ s/^\s*//;
635 1ec3f6f9 Blue Swirl
	$stmt =~ s/\s*$//;
636 1ec3f6f9 Blue Swirl
637 1ec3f6f9 Blue Swirl
	my @stmt_lines = ($stmt =~ /\n/g);
638 1ec3f6f9 Blue Swirl
	my @stmt_statements = ($stmt =~ /;/g);
639 1ec3f6f9 Blue Swirl
640 1ec3f6f9 Blue Swirl
	my $stmt_lines = $#stmt_lines + 2;
641 1ec3f6f9 Blue Swirl
	my $stmt_statements = $#stmt_statements + 1;
642 1ec3f6f9 Blue Swirl
643 1ec3f6f9 Blue Swirl
	if ($stmt_lines > $stmt_statements) {
644 1ec3f6f9 Blue Swirl
		return $stmt_lines;
645 1ec3f6f9 Blue Swirl
	} else {
646 1ec3f6f9 Blue Swirl
		return $stmt_statements;
647 1ec3f6f9 Blue Swirl
	}
648 1ec3f6f9 Blue Swirl
}
649 1ec3f6f9 Blue Swirl
650 1ec3f6f9 Blue Swirl
sub ctx_statement_full {
651 1ec3f6f9 Blue Swirl
	my ($linenr, $remain, $off) = @_;
652 1ec3f6f9 Blue Swirl
	my ($statement, $condition, $level);
653 1ec3f6f9 Blue Swirl
654 1ec3f6f9 Blue Swirl
	my (@chunks);
655 1ec3f6f9 Blue Swirl
656 1ec3f6f9 Blue Swirl
	# Grab the first conditional/block pair.
657 1ec3f6f9 Blue Swirl
	($statement, $condition, $linenr, $remain, $off, $level) =
658 1ec3f6f9 Blue Swirl
				ctx_statement_block($linenr, $remain, $off);
659 1ec3f6f9 Blue Swirl
	#print "F: c<$condition> s<$statement> remain<$remain>\n";
660 1ec3f6f9 Blue Swirl
	push(@chunks, [ $condition, $statement ]);
661 1ec3f6f9 Blue Swirl
	if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
662 1ec3f6f9 Blue Swirl
		return ($level, $linenr, @chunks);
663 1ec3f6f9 Blue Swirl
	}
664 1ec3f6f9 Blue Swirl
665 1ec3f6f9 Blue Swirl
	# Pull in the following conditional/block pairs and see if they
666 1ec3f6f9 Blue Swirl
	# could continue the statement.
667 1ec3f6f9 Blue Swirl
	for (;;) {
668 1ec3f6f9 Blue Swirl
		($statement, $condition, $linenr, $remain, $off, $level) =
669 1ec3f6f9 Blue Swirl
				ctx_statement_block($linenr, $remain, $off);
670 1ec3f6f9 Blue Swirl
		#print "C: c<$condition> s<$statement> remain<$remain>\n";
671 1ec3f6f9 Blue Swirl
		last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
672 1ec3f6f9 Blue Swirl
		#print "C: push\n";
673 1ec3f6f9 Blue Swirl
		push(@chunks, [ $condition, $statement ]);
674 1ec3f6f9 Blue Swirl
	}
675 1ec3f6f9 Blue Swirl
676 1ec3f6f9 Blue Swirl
	return ($level, $linenr, @chunks);
677 1ec3f6f9 Blue Swirl
}
678 1ec3f6f9 Blue Swirl
679 1ec3f6f9 Blue Swirl
sub ctx_block_get {
680 1ec3f6f9 Blue Swirl
	my ($linenr, $remain, $outer, $open, $close, $off) = @_;
681 1ec3f6f9 Blue Swirl
	my $line;
682 1ec3f6f9 Blue Swirl
	my $start = $linenr - 1;
683 1ec3f6f9 Blue Swirl
	my $blk = '';
684 1ec3f6f9 Blue Swirl
	my @o;
685 1ec3f6f9 Blue Swirl
	my @c;
686 1ec3f6f9 Blue Swirl
	my @res = ();
687 1ec3f6f9 Blue Swirl
688 1ec3f6f9 Blue Swirl
	my $level = 0;
689 1ec3f6f9 Blue Swirl
	my @stack = ($level);
690 1ec3f6f9 Blue Swirl
	for ($line = $start; $remain > 0; $line++) {
691 1ec3f6f9 Blue Swirl
		next if ($rawlines[$line] =~ /^-/);
692 1ec3f6f9 Blue Swirl
		$remain--;
693 1ec3f6f9 Blue Swirl
694 1ec3f6f9 Blue Swirl
		$blk .= $rawlines[$line];
695 1ec3f6f9 Blue Swirl
696 1ec3f6f9 Blue Swirl
		# Handle nested #if/#else.
697 1ec3f6f9 Blue Swirl
		if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
698 1ec3f6f9 Blue Swirl
			push(@stack, $level);
699 1ec3f6f9 Blue Swirl
		} elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
700 1ec3f6f9 Blue Swirl
			$level = $stack[$#stack - 1];
701 1ec3f6f9 Blue Swirl
		} elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
702 1ec3f6f9 Blue Swirl
			$level = pop(@stack);
703 1ec3f6f9 Blue Swirl
		}
704 1ec3f6f9 Blue Swirl
705 1ec3f6f9 Blue Swirl
		foreach my $c (split(//, $lines[$line])) {
706 1ec3f6f9 Blue Swirl
			##print "C<$c>L<$level><$open$close>O<$off>\n";
707 1ec3f6f9 Blue Swirl
			if ($off > 0) {
708 1ec3f6f9 Blue Swirl
				$off--;
709 1ec3f6f9 Blue Swirl
				next;
710 1ec3f6f9 Blue Swirl
			}
711 1ec3f6f9 Blue Swirl
712 1ec3f6f9 Blue Swirl
			if ($c eq $close && $level > 0) {
713 1ec3f6f9 Blue Swirl
				$level--;
714 1ec3f6f9 Blue Swirl
				last if ($level == 0);
715 1ec3f6f9 Blue Swirl
			} elsif ($c eq $open) {
716 1ec3f6f9 Blue Swirl
				$level++;
717 1ec3f6f9 Blue Swirl
			}
718 1ec3f6f9 Blue Swirl
		}
719 1ec3f6f9 Blue Swirl
720 1ec3f6f9 Blue Swirl
		if (!$outer || $level <= 1) {
721 1ec3f6f9 Blue Swirl
			push(@res, $rawlines[$line]);
722 1ec3f6f9 Blue Swirl
		}
723 1ec3f6f9 Blue Swirl
724 1ec3f6f9 Blue Swirl
		last if ($level == 0);
725 1ec3f6f9 Blue Swirl
	}
726 1ec3f6f9 Blue Swirl
727 1ec3f6f9 Blue Swirl
	return ($level, @res);
728 1ec3f6f9 Blue Swirl
}
729 1ec3f6f9 Blue Swirl
sub ctx_block_outer {
730 1ec3f6f9 Blue Swirl
	my ($linenr, $remain) = @_;
731 1ec3f6f9 Blue Swirl
732 1ec3f6f9 Blue Swirl
	my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
733 1ec3f6f9 Blue Swirl
	return @r;
734 1ec3f6f9 Blue Swirl
}
735 1ec3f6f9 Blue Swirl
sub ctx_block {
736 1ec3f6f9 Blue Swirl
	my ($linenr, $remain) = @_;
737 1ec3f6f9 Blue Swirl
738 1ec3f6f9 Blue Swirl
	my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
739 1ec3f6f9 Blue Swirl
	return @r;
740 1ec3f6f9 Blue Swirl
}
741 1ec3f6f9 Blue Swirl
sub ctx_statement {
742 1ec3f6f9 Blue Swirl
	my ($linenr, $remain, $off) = @_;
743 1ec3f6f9 Blue Swirl
744 1ec3f6f9 Blue Swirl
	my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
745 1ec3f6f9 Blue Swirl
	return @r;
746 1ec3f6f9 Blue Swirl
}
747 1ec3f6f9 Blue Swirl
sub ctx_block_level {
748 1ec3f6f9 Blue Swirl
	my ($linenr, $remain) = @_;
749 1ec3f6f9 Blue Swirl
750 1ec3f6f9 Blue Swirl
	return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
751 1ec3f6f9 Blue Swirl
}
752 1ec3f6f9 Blue Swirl
sub ctx_statement_level {
753 1ec3f6f9 Blue Swirl
	my ($linenr, $remain, $off) = @_;
754 1ec3f6f9 Blue Swirl
755 1ec3f6f9 Blue Swirl
	return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
756 1ec3f6f9 Blue Swirl
}
757 1ec3f6f9 Blue Swirl
758 1ec3f6f9 Blue Swirl
sub ctx_locate_comment {
759 1ec3f6f9 Blue Swirl
	my ($first_line, $end_line) = @_;
760 1ec3f6f9 Blue Swirl
761 1ec3f6f9 Blue Swirl
	# Catch a comment on the end of the line itself.
762 1ec3f6f9 Blue Swirl
	my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
763 1ec3f6f9 Blue Swirl
	return $current_comment if (defined $current_comment);
764 1ec3f6f9 Blue Swirl
765 1ec3f6f9 Blue Swirl
	# Look through the context and try and figure out if there is a
766 1ec3f6f9 Blue Swirl
	# comment.
767 1ec3f6f9 Blue Swirl
	my $in_comment = 0;
768 1ec3f6f9 Blue Swirl
	$current_comment = '';
769 1ec3f6f9 Blue Swirl
	for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
770 1ec3f6f9 Blue Swirl
		my $line = $rawlines[$linenr - 1];
771 1ec3f6f9 Blue Swirl
		#warn "           $line\n";
772 1ec3f6f9 Blue Swirl
		if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
773 1ec3f6f9 Blue Swirl
			$in_comment = 1;
774 1ec3f6f9 Blue Swirl
		}
775 1ec3f6f9 Blue Swirl
		if ($line =~ m@/\*@) {
776 1ec3f6f9 Blue Swirl
			$in_comment = 1;
777 1ec3f6f9 Blue Swirl
		}
778 1ec3f6f9 Blue Swirl
		if (!$in_comment && $current_comment ne '') {
779 1ec3f6f9 Blue Swirl
			$current_comment = '';
780 1ec3f6f9 Blue Swirl
		}
781 1ec3f6f9 Blue Swirl
		$current_comment .= $line . "\n" if ($in_comment);
782 1ec3f6f9 Blue Swirl
		if ($line =~ m@\*/@) {
783 1ec3f6f9 Blue Swirl
			$in_comment = 0;
784 1ec3f6f9 Blue Swirl
		}
785 1ec3f6f9 Blue Swirl
	}
786 1ec3f6f9 Blue Swirl
787 1ec3f6f9 Blue Swirl
	chomp($current_comment);
788 1ec3f6f9 Blue Swirl
	return($current_comment);
789 1ec3f6f9 Blue Swirl
}
790 1ec3f6f9 Blue Swirl
sub ctx_has_comment {
791 1ec3f6f9 Blue Swirl
	my ($first_line, $end_line) = @_;
792 1ec3f6f9 Blue Swirl
	my $cmt = ctx_locate_comment($first_line, $end_line);
793 1ec3f6f9 Blue Swirl
794 1ec3f6f9 Blue Swirl
	##print "LINE: $rawlines[$end_line - 1 ]\n";
795 1ec3f6f9 Blue Swirl
	##print "CMMT: $cmt\n";
796 1ec3f6f9 Blue Swirl
797 1ec3f6f9 Blue Swirl
	return ($cmt ne '');
798 1ec3f6f9 Blue Swirl
}
799 1ec3f6f9 Blue Swirl
800 1ec3f6f9 Blue Swirl
sub raw_line {
801 1ec3f6f9 Blue Swirl
	my ($linenr, $cnt) = @_;
802 1ec3f6f9 Blue Swirl
803 1ec3f6f9 Blue Swirl
	my $offset = $linenr - 1;
804 1ec3f6f9 Blue Swirl
	$cnt++;
805 1ec3f6f9 Blue Swirl
806 1ec3f6f9 Blue Swirl
	my $line;
807 1ec3f6f9 Blue Swirl
	while ($cnt) {
808 1ec3f6f9 Blue Swirl
		$line = $rawlines[$offset++];
809 1ec3f6f9 Blue Swirl
		next if (defined($line) && $line =~ /^-/);
810 1ec3f6f9 Blue Swirl
		$cnt--;
811 1ec3f6f9 Blue Swirl
	}
812 1ec3f6f9 Blue Swirl
813 1ec3f6f9 Blue Swirl
	return $line;
814 1ec3f6f9 Blue Swirl
}
815 1ec3f6f9 Blue Swirl
816 1ec3f6f9 Blue Swirl
sub cat_vet {
817 1ec3f6f9 Blue Swirl
	my ($vet) = @_;
818 1ec3f6f9 Blue Swirl
	my ($res, $coded);
819 1ec3f6f9 Blue Swirl
820 1ec3f6f9 Blue Swirl
	$res = '';
821 1ec3f6f9 Blue Swirl
	while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
822 1ec3f6f9 Blue Swirl
		$res .= $1;
823 1ec3f6f9 Blue Swirl
		if ($2 ne '') {
824 1ec3f6f9 Blue Swirl
			$coded = sprintf("^%c", unpack('C', $2) + 64);
825 1ec3f6f9 Blue Swirl
			$res .= $coded;
826 1ec3f6f9 Blue Swirl
		}
827 1ec3f6f9 Blue Swirl
	}
828 1ec3f6f9 Blue Swirl
	$res =~ s/$/\$/;
829 1ec3f6f9 Blue Swirl
830 1ec3f6f9 Blue Swirl
	return $res;
831 1ec3f6f9 Blue Swirl
}
832 1ec3f6f9 Blue Swirl
833 1ec3f6f9 Blue Swirl
my $av_preprocessor = 0;
834 1ec3f6f9 Blue Swirl
my $av_pending;
835 1ec3f6f9 Blue Swirl
my @av_paren_type;
836 1ec3f6f9 Blue Swirl
my $av_pend_colon;
837 1ec3f6f9 Blue Swirl
838 1ec3f6f9 Blue Swirl
sub annotate_reset {
839 1ec3f6f9 Blue Swirl
	$av_preprocessor = 0;
840 1ec3f6f9 Blue Swirl
	$av_pending = '_';
841 1ec3f6f9 Blue Swirl
	@av_paren_type = ('E');
842 1ec3f6f9 Blue Swirl
	$av_pend_colon = 'O';
843 1ec3f6f9 Blue Swirl
}
844 1ec3f6f9 Blue Swirl
845 1ec3f6f9 Blue Swirl
sub annotate_values {
846 1ec3f6f9 Blue Swirl
	my ($stream, $type) = @_;
847 1ec3f6f9 Blue Swirl
848 1ec3f6f9 Blue Swirl
	my $res;
849 1ec3f6f9 Blue Swirl
	my $var = '_' x length($stream);
850 1ec3f6f9 Blue Swirl
	my $cur = $stream;
851 1ec3f6f9 Blue Swirl
852 1ec3f6f9 Blue Swirl
	print "$stream\n" if ($dbg_values > 1);
853 1ec3f6f9 Blue Swirl
854 1ec3f6f9 Blue Swirl
	while (length($cur)) {
855 1ec3f6f9 Blue Swirl
		@av_paren_type = ('E') if ($#av_paren_type < 0);
856 1ec3f6f9 Blue Swirl
		print " <" . join('', @av_paren_type) .
857 1ec3f6f9 Blue Swirl
				"> <$type> <$av_pending>" if ($dbg_values > 1);
858 1ec3f6f9 Blue Swirl
		if ($cur =~ /^(\s+)/o) {
859 1ec3f6f9 Blue Swirl
			print "WS($1)\n" if ($dbg_values > 1);
860 1ec3f6f9 Blue Swirl
			if ($1 =~ /\n/ && $av_preprocessor) {
861 1ec3f6f9 Blue Swirl
				$type = pop(@av_paren_type);
862 1ec3f6f9 Blue Swirl
				$av_preprocessor = 0;
863 1ec3f6f9 Blue Swirl
			}
864 1ec3f6f9 Blue Swirl
865 61669f9a Florian Mickler
		} elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
866 1ec3f6f9 Blue Swirl
			print "CAST($1)\n" if ($dbg_values > 1);
867 1ec3f6f9 Blue Swirl
			push(@av_paren_type, $type);
868 1ec3f6f9 Blue Swirl
			$type = 'C';
869 1ec3f6f9 Blue Swirl
870 1ec3f6f9 Blue Swirl
		} elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
871 1ec3f6f9 Blue Swirl
			print "DECLARE($1)\n" if ($dbg_values > 1);
872 1ec3f6f9 Blue Swirl
			$type = 'T';
873 1ec3f6f9 Blue Swirl
874 1ec3f6f9 Blue Swirl
		} elsif ($cur =~ /^($Modifier)\s*/) {
875 1ec3f6f9 Blue Swirl
			print "MODIFIER($1)\n" if ($dbg_values > 1);
876 1ec3f6f9 Blue Swirl
			$type = 'T';
877 1ec3f6f9 Blue Swirl
878 1ec3f6f9 Blue Swirl
		} elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
879 1ec3f6f9 Blue Swirl
			print "DEFINE($1,$2)\n" if ($dbg_values > 1);
880 1ec3f6f9 Blue Swirl
			$av_preprocessor = 1;
881 1ec3f6f9 Blue Swirl
			push(@av_paren_type, $type);
882 1ec3f6f9 Blue Swirl
			if ($2 ne '') {
883 1ec3f6f9 Blue Swirl
				$av_pending = 'N';
884 1ec3f6f9 Blue Swirl
			}
885 1ec3f6f9 Blue Swirl
			$type = 'E';
886 1ec3f6f9 Blue Swirl
887 1ec3f6f9 Blue Swirl
		} elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
888 1ec3f6f9 Blue Swirl
			print "UNDEF($1)\n" if ($dbg_values > 1);
889 1ec3f6f9 Blue Swirl
			$av_preprocessor = 1;
890 1ec3f6f9 Blue Swirl
			push(@av_paren_type, $type);
891 1ec3f6f9 Blue Swirl
892 1ec3f6f9 Blue Swirl
		} elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
893 1ec3f6f9 Blue Swirl
			print "PRE_START($1)\n" if ($dbg_values > 1);
894 1ec3f6f9 Blue Swirl
			$av_preprocessor = 1;
895 1ec3f6f9 Blue Swirl
896 1ec3f6f9 Blue Swirl
			push(@av_paren_type, $type);
897 1ec3f6f9 Blue Swirl
			push(@av_paren_type, $type);
898 1ec3f6f9 Blue Swirl
			$type = 'E';
899 1ec3f6f9 Blue Swirl
900 1ec3f6f9 Blue Swirl
		} elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
901 1ec3f6f9 Blue Swirl
			print "PRE_RESTART($1)\n" if ($dbg_values > 1);
902 1ec3f6f9 Blue Swirl
			$av_preprocessor = 1;
903 1ec3f6f9 Blue Swirl
904 1ec3f6f9 Blue Swirl
			push(@av_paren_type, $av_paren_type[$#av_paren_type]);
905 1ec3f6f9 Blue Swirl
906 1ec3f6f9 Blue Swirl
			$type = 'E';
907 1ec3f6f9 Blue Swirl
908 1ec3f6f9 Blue Swirl
		} elsif ($cur =~ /^(\#\s*(?:endif))/o) {
909 1ec3f6f9 Blue Swirl
			print "PRE_END($1)\n" if ($dbg_values > 1);
910 1ec3f6f9 Blue Swirl
911 1ec3f6f9 Blue Swirl
			$av_preprocessor = 1;
912 1ec3f6f9 Blue Swirl
913 1ec3f6f9 Blue Swirl
			# Assume all arms of the conditional end as this
914 1ec3f6f9 Blue Swirl
			# one does, and continue as if the #endif was not here.
915 1ec3f6f9 Blue Swirl
			pop(@av_paren_type);
916 1ec3f6f9 Blue Swirl
			push(@av_paren_type, $type);
917 1ec3f6f9 Blue Swirl
			$type = 'E';
918 1ec3f6f9 Blue Swirl
919 1ec3f6f9 Blue Swirl
		} elsif ($cur =~ /^(\\\n)/o) {
920 1ec3f6f9 Blue Swirl
			print "PRECONT($1)\n" if ($dbg_values > 1);
921 1ec3f6f9 Blue Swirl
922 1ec3f6f9 Blue Swirl
		} elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
923 1ec3f6f9 Blue Swirl
			print "ATTR($1)\n" if ($dbg_values > 1);
924 1ec3f6f9 Blue Swirl
			$av_pending = $type;
925 1ec3f6f9 Blue Swirl
			$type = 'N';
926 1ec3f6f9 Blue Swirl
927 1ec3f6f9 Blue Swirl
		} elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
928 1ec3f6f9 Blue Swirl
			print "SIZEOF($1)\n" if ($dbg_values > 1);
929 1ec3f6f9 Blue Swirl
			if (defined $2) {
930 1ec3f6f9 Blue Swirl
				$av_pending = 'V';
931 1ec3f6f9 Blue Swirl
			}
932 1ec3f6f9 Blue Swirl
			$type = 'N';
933 1ec3f6f9 Blue Swirl
934 1ec3f6f9 Blue Swirl
		} elsif ($cur =~ /^(if|while|for)\b/o) {
935 1ec3f6f9 Blue Swirl
			print "COND($1)\n" if ($dbg_values > 1);
936 1ec3f6f9 Blue Swirl
			$av_pending = 'E';
937 1ec3f6f9 Blue Swirl
			$type = 'N';
938 1ec3f6f9 Blue Swirl
939 1ec3f6f9 Blue Swirl
		} elsif ($cur =~/^(case)/o) {
940 1ec3f6f9 Blue Swirl
			print "CASE($1)\n" if ($dbg_values > 1);
941 1ec3f6f9 Blue Swirl
			$av_pend_colon = 'C';
942 1ec3f6f9 Blue Swirl
			$type = 'N';
943 1ec3f6f9 Blue Swirl
944 1ec3f6f9 Blue Swirl
		} elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
945 1ec3f6f9 Blue Swirl
			print "KEYWORD($1)\n" if ($dbg_values > 1);
946 1ec3f6f9 Blue Swirl
			$type = 'N';
947 1ec3f6f9 Blue Swirl
948 1ec3f6f9 Blue Swirl
		} elsif ($cur =~ /^(\()/o) {
949 1ec3f6f9 Blue Swirl
			print "PAREN('$1')\n" if ($dbg_values > 1);
950 1ec3f6f9 Blue Swirl
			push(@av_paren_type, $av_pending);
951 1ec3f6f9 Blue Swirl
			$av_pending = '_';
952 1ec3f6f9 Blue Swirl
			$type = 'N';
953 1ec3f6f9 Blue Swirl
954 1ec3f6f9 Blue Swirl
		} elsif ($cur =~ /^(\))/o) {
955 1ec3f6f9 Blue Swirl
			my $new_type = pop(@av_paren_type);
956 1ec3f6f9 Blue Swirl
			if ($new_type ne '_') {
957 1ec3f6f9 Blue Swirl
				$type = $new_type;
958 1ec3f6f9 Blue Swirl
				print "PAREN('$1') -> $type\n"
959 1ec3f6f9 Blue Swirl
							if ($dbg_values > 1);
960 1ec3f6f9 Blue Swirl
			} else {
961 1ec3f6f9 Blue Swirl
				print "PAREN('$1')\n" if ($dbg_values > 1);
962 1ec3f6f9 Blue Swirl
			}
963 1ec3f6f9 Blue Swirl
964 1ec3f6f9 Blue Swirl
		} elsif ($cur =~ /^($Ident)\s*\(/o) {
965 1ec3f6f9 Blue Swirl
			print "FUNC($1)\n" if ($dbg_values > 1);
966 1ec3f6f9 Blue Swirl
			$type = 'V';
967 1ec3f6f9 Blue Swirl
			$av_pending = 'V';
968 1ec3f6f9 Blue Swirl
969 1ec3f6f9 Blue Swirl
		} elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
970 1ec3f6f9 Blue Swirl
			if (defined $2 && $type eq 'C' || $type eq 'T') {
971 1ec3f6f9 Blue Swirl
				$av_pend_colon = 'B';
972 1ec3f6f9 Blue Swirl
			} elsif ($type eq 'E') {
973 1ec3f6f9 Blue Swirl
				$av_pend_colon = 'L';
974 1ec3f6f9 Blue Swirl
			}
975 1ec3f6f9 Blue Swirl
			print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
976 1ec3f6f9 Blue Swirl
			$type = 'V';
977 1ec3f6f9 Blue Swirl
978 1ec3f6f9 Blue Swirl
		} elsif ($cur =~ /^($Ident|$Constant)/o) {
979 1ec3f6f9 Blue Swirl
			print "IDENT($1)\n" if ($dbg_values > 1);
980 1ec3f6f9 Blue Swirl
			$type = 'V';
981 1ec3f6f9 Blue Swirl
982 1ec3f6f9 Blue Swirl
		} elsif ($cur =~ /^($Assignment)/o) {
983 1ec3f6f9 Blue Swirl
			print "ASSIGN($1)\n" if ($dbg_values > 1);
984 1ec3f6f9 Blue Swirl
			$type = 'N';
985 1ec3f6f9 Blue Swirl
986 1ec3f6f9 Blue Swirl
		} elsif ($cur =~/^(;|{|})/) {
987 1ec3f6f9 Blue Swirl
			print "END($1)\n" if ($dbg_values > 1);
988 1ec3f6f9 Blue Swirl
			$type = 'E';
989 1ec3f6f9 Blue Swirl
			$av_pend_colon = 'O';
990 1ec3f6f9 Blue Swirl
991 1ec3f6f9 Blue Swirl
		} elsif ($cur =~/^(,)/) {
992 1ec3f6f9 Blue Swirl
			print "COMMA($1)\n" if ($dbg_values > 1);
993 1ec3f6f9 Blue Swirl
			$type = 'C';
994 1ec3f6f9 Blue Swirl
995 1ec3f6f9 Blue Swirl
		} elsif ($cur =~ /^(\?)/o) {
996 1ec3f6f9 Blue Swirl
			print "QUESTION($1)\n" if ($dbg_values > 1);
997 1ec3f6f9 Blue Swirl
			$type = 'N';
998 1ec3f6f9 Blue Swirl
999 1ec3f6f9 Blue Swirl
		} elsif ($cur =~ /^(:)/o) {
1000 1ec3f6f9 Blue Swirl
			print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
1001 1ec3f6f9 Blue Swirl
1002 1ec3f6f9 Blue Swirl
			substr($var, length($res), 1, $av_pend_colon);
1003 1ec3f6f9 Blue Swirl
			if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
1004 1ec3f6f9 Blue Swirl
				$type = 'E';
1005 1ec3f6f9 Blue Swirl
			} else {
1006 1ec3f6f9 Blue Swirl
				$type = 'N';
1007 1ec3f6f9 Blue Swirl
			}
1008 1ec3f6f9 Blue Swirl
			$av_pend_colon = 'O';
1009 1ec3f6f9 Blue Swirl
1010 1ec3f6f9 Blue Swirl
		} elsif ($cur =~ /^(\[)/o) {
1011 1ec3f6f9 Blue Swirl
			print "CLOSE($1)\n" if ($dbg_values > 1);
1012 1ec3f6f9 Blue Swirl
			$type = 'N';
1013 1ec3f6f9 Blue Swirl
1014 1ec3f6f9 Blue Swirl
		} elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
1015 1ec3f6f9 Blue Swirl
			my $variant;
1016 1ec3f6f9 Blue Swirl
1017 1ec3f6f9 Blue Swirl
			print "OPV($1)\n" if ($dbg_values > 1);
1018 1ec3f6f9 Blue Swirl
			if ($type eq 'V') {
1019 1ec3f6f9 Blue Swirl
				$variant = 'B';
1020 1ec3f6f9 Blue Swirl
			} else {
1021 1ec3f6f9 Blue Swirl
				$variant = 'U';
1022 1ec3f6f9 Blue Swirl
			}
1023 1ec3f6f9 Blue Swirl
1024 1ec3f6f9 Blue Swirl
			substr($var, length($res), 1, $variant);
1025 1ec3f6f9 Blue Swirl
			$type = 'N';
1026 1ec3f6f9 Blue Swirl
1027 1ec3f6f9 Blue Swirl
		} elsif ($cur =~ /^($Operators)/o) {
1028 1ec3f6f9 Blue Swirl
			print "OP($1)\n" if ($dbg_values > 1);
1029 1ec3f6f9 Blue Swirl
			if ($1 ne '++' && $1 ne '--') {
1030 1ec3f6f9 Blue Swirl
				$type = 'N';
1031 1ec3f6f9 Blue Swirl
			}
1032 1ec3f6f9 Blue Swirl
1033 1ec3f6f9 Blue Swirl
		} elsif ($cur =~ /(^.)/o) {
1034 1ec3f6f9 Blue Swirl
			print "C($1)\n" if ($dbg_values > 1);
1035 1ec3f6f9 Blue Swirl
		}
1036 1ec3f6f9 Blue Swirl
		if (defined $1) {
1037 1ec3f6f9 Blue Swirl
			$cur = substr($cur, length($1));
1038 1ec3f6f9 Blue Swirl
			$res .= $type x length($1);
1039 1ec3f6f9 Blue Swirl
		}
1040 1ec3f6f9 Blue Swirl
	}
1041 1ec3f6f9 Blue Swirl
1042 1ec3f6f9 Blue Swirl
	return ($res, $var);
1043 1ec3f6f9 Blue Swirl
}
1044 1ec3f6f9 Blue Swirl
1045 1ec3f6f9 Blue Swirl
sub possible {
1046 1ec3f6f9 Blue Swirl
	my ($possible, $line) = @_;
1047 1ec3f6f9 Blue Swirl
	my $notPermitted = qr{(?:
1048 1ec3f6f9 Blue Swirl
		^(?:
1049 1ec3f6f9 Blue Swirl
			$Modifier|
1050 1ec3f6f9 Blue Swirl
			$Storage|
1051 1ec3f6f9 Blue Swirl
			$Type|
1052 1ec3f6f9 Blue Swirl
			DEFINE_\S+
1053 1ec3f6f9 Blue Swirl
		)$|
1054 1ec3f6f9 Blue Swirl
		^(?:
1055 1ec3f6f9 Blue Swirl
			goto|
1056 1ec3f6f9 Blue Swirl
			return|
1057 1ec3f6f9 Blue Swirl
			case|
1058 1ec3f6f9 Blue Swirl
			else|
1059 1ec3f6f9 Blue Swirl
			asm|__asm__|
1060 1ec3f6f9 Blue Swirl
			do
1061 1ec3f6f9 Blue Swirl
		)(?:\s|$)|
1062 1ec3f6f9 Blue Swirl
		^(?:typedef|struct|enum)\b
1063 1ec3f6f9 Blue Swirl
	    )}x;
1064 1ec3f6f9 Blue Swirl
	warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
1065 1ec3f6f9 Blue Swirl
	if ($possible !~ $notPermitted) {
1066 1ec3f6f9 Blue Swirl
		# Check for modifiers.
1067 1ec3f6f9 Blue Swirl
		$possible =~ s/\s*$Storage\s*//g;
1068 1ec3f6f9 Blue Swirl
		$possible =~ s/\s*$Sparse\s*//g;
1069 1ec3f6f9 Blue Swirl
		if ($possible =~ /^\s*$/) {
1070 1ec3f6f9 Blue Swirl
1071 1ec3f6f9 Blue Swirl
		} elsif ($possible =~ /\s/) {
1072 1ec3f6f9 Blue Swirl
			$possible =~ s/\s*$Type\s*//g;
1073 1ec3f6f9 Blue Swirl
			for my $modifier (split(' ', $possible)) {
1074 1ec3f6f9 Blue Swirl
				if ($modifier !~ $notPermitted) {
1075 1ec3f6f9 Blue Swirl
					warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1076 1ec3f6f9 Blue Swirl
					push(@modifierList, $modifier);
1077 1ec3f6f9 Blue Swirl
				}
1078 1ec3f6f9 Blue Swirl
			}
1079 1ec3f6f9 Blue Swirl
1080 1ec3f6f9 Blue Swirl
		} else {
1081 1ec3f6f9 Blue Swirl
			warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
1082 1ec3f6f9 Blue Swirl
			push(@typeList, $possible);
1083 1ec3f6f9 Blue Swirl
		}
1084 1ec3f6f9 Blue Swirl
		build_types();
1085 1ec3f6f9 Blue Swirl
	} else {
1086 1ec3f6f9 Blue Swirl
		warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
1087 1ec3f6f9 Blue Swirl
	}
1088 1ec3f6f9 Blue Swirl
}
1089 1ec3f6f9 Blue Swirl
1090 1ec3f6f9 Blue Swirl
my $prefix = '';
1091 1ec3f6f9 Blue Swirl
1092 1ec3f6f9 Blue Swirl
sub report {
1093 1ec3f6f9 Blue Swirl
	if (defined $tst_only && $_[0] !~ /\Q$tst_only\E/) {
1094 1ec3f6f9 Blue Swirl
		return 0;
1095 1ec3f6f9 Blue Swirl
	}
1096 1ec3f6f9 Blue Swirl
	my $line = $prefix . $_[0];
1097 1ec3f6f9 Blue Swirl
1098 1ec3f6f9 Blue Swirl
	$line = (split('\n', $line))[0] . "\n" if ($terse);
1099 1ec3f6f9 Blue Swirl
1100 1ec3f6f9 Blue Swirl
	push(our @report, $line);
1101 1ec3f6f9 Blue Swirl
1102 1ec3f6f9 Blue Swirl
	return 1;
1103 1ec3f6f9 Blue Swirl
}
1104 1ec3f6f9 Blue Swirl
sub report_dump {
1105 1ec3f6f9 Blue Swirl
	our @report;
1106 1ec3f6f9 Blue Swirl
}
1107 1ec3f6f9 Blue Swirl
sub ERROR {
1108 1ec3f6f9 Blue Swirl
	if (report("ERROR: $_[0]\n")) {
1109 1ec3f6f9 Blue Swirl
		our $clean = 0;
1110 1ec3f6f9 Blue Swirl
		our $cnt_error++;
1111 1ec3f6f9 Blue Swirl
	}
1112 1ec3f6f9 Blue Swirl
}
1113 1ec3f6f9 Blue Swirl
sub WARN {
1114 1ec3f6f9 Blue Swirl
	if (report("WARNING: $_[0]\n")) {
1115 1ec3f6f9 Blue Swirl
		our $clean = 0;
1116 1ec3f6f9 Blue Swirl
		our $cnt_warn++;
1117 1ec3f6f9 Blue Swirl
	}
1118 1ec3f6f9 Blue Swirl
}
1119 1ec3f6f9 Blue Swirl
sub CHK {
1120 1ec3f6f9 Blue Swirl
	if ($check && report("CHECK: $_[0]\n")) {
1121 1ec3f6f9 Blue Swirl
		our $clean = 0;
1122 1ec3f6f9 Blue Swirl
		our $cnt_chk++;
1123 1ec3f6f9 Blue Swirl
	}
1124 1ec3f6f9 Blue Swirl
}
1125 1ec3f6f9 Blue Swirl
1126 1ec3f6f9 Blue Swirl
sub check_absolute_file {
1127 1ec3f6f9 Blue Swirl
	my ($absolute, $herecurr) = @_;
1128 1ec3f6f9 Blue Swirl
	my $file = $absolute;
1129 1ec3f6f9 Blue Swirl
1130 1ec3f6f9 Blue Swirl
	##print "absolute<$absolute>\n";
1131 1ec3f6f9 Blue Swirl
1132 1ec3f6f9 Blue Swirl
	# See if any suffix of this path is a path within the tree.
1133 1ec3f6f9 Blue Swirl
	while ($file =~ s@^[^/]*/@@) {
1134 1ec3f6f9 Blue Swirl
		if (-f "$root/$file") {
1135 1ec3f6f9 Blue Swirl
			##print "file<$file>\n";
1136 1ec3f6f9 Blue Swirl
			last;
1137 1ec3f6f9 Blue Swirl
		}
1138 1ec3f6f9 Blue Swirl
	}
1139 1ec3f6f9 Blue Swirl
	if (! -f _)  {
1140 1ec3f6f9 Blue Swirl
		return 0;
1141 1ec3f6f9 Blue Swirl
	}
1142 1ec3f6f9 Blue Swirl
1143 1ec3f6f9 Blue Swirl
	# It is, so see if the prefix is acceptable.
1144 1ec3f6f9 Blue Swirl
	my $prefix = $absolute;
1145 1ec3f6f9 Blue Swirl
	substr($prefix, -length($file)) = '';
1146 1ec3f6f9 Blue Swirl
1147 1ec3f6f9 Blue Swirl
	##print "prefix<$prefix>\n";
1148 1ec3f6f9 Blue Swirl
	if ($prefix ne ".../") {
1149 1ec3f6f9 Blue Swirl
		WARN("use relative pathname instead of absolute in changelog text\n" . $herecurr);
1150 1ec3f6f9 Blue Swirl
	}
1151 1ec3f6f9 Blue Swirl
}
1152 1ec3f6f9 Blue Swirl
1153 1ec3f6f9 Blue Swirl
sub process {
1154 1ec3f6f9 Blue Swirl
	my $filename = shift;
1155 1ec3f6f9 Blue Swirl
1156 1ec3f6f9 Blue Swirl
	my $linenr=0;
1157 1ec3f6f9 Blue Swirl
	my $prevline="";
1158 1ec3f6f9 Blue Swirl
	my $prevrawline="";
1159 1ec3f6f9 Blue Swirl
	my $stashline="";
1160 1ec3f6f9 Blue Swirl
	my $stashrawline="";
1161 1ec3f6f9 Blue Swirl
1162 1ec3f6f9 Blue Swirl
	my $length;
1163 1ec3f6f9 Blue Swirl
	my $indent;
1164 1ec3f6f9 Blue Swirl
	my $previndent=0;
1165 1ec3f6f9 Blue Swirl
	my $stashindent=0;
1166 1ec3f6f9 Blue Swirl
1167 1ec3f6f9 Blue Swirl
	our $clean = 1;
1168 1ec3f6f9 Blue Swirl
	my $signoff = 0;
1169 1ec3f6f9 Blue Swirl
	my $is_patch = 0;
1170 1ec3f6f9 Blue Swirl
1171 1ec3f6f9 Blue Swirl
	our @report = ();
1172 1ec3f6f9 Blue Swirl
	our $cnt_lines = 0;
1173 1ec3f6f9 Blue Swirl
	our $cnt_error = 0;
1174 1ec3f6f9 Blue Swirl
	our $cnt_warn = 0;
1175 1ec3f6f9 Blue Swirl
	our $cnt_chk = 0;
1176 1ec3f6f9 Blue Swirl
1177 1ec3f6f9 Blue Swirl
	# Trace the real file/line as we go.
1178 1ec3f6f9 Blue Swirl
	my $realfile = '';
1179 1ec3f6f9 Blue Swirl
	my $realline = 0;
1180 1ec3f6f9 Blue Swirl
	my $realcnt = 0;
1181 1ec3f6f9 Blue Swirl
	my $here = '';
1182 1ec3f6f9 Blue Swirl
	my $in_comment = 0;
1183 1ec3f6f9 Blue Swirl
	my $comment_edge = 0;
1184 1ec3f6f9 Blue Swirl
	my $first_line = 0;
1185 1ec3f6f9 Blue Swirl
	my $p1_prefix = '';
1186 1ec3f6f9 Blue Swirl
1187 1ec3f6f9 Blue Swirl
	my $prev_values = 'E';
1188 1ec3f6f9 Blue Swirl
1189 1ec3f6f9 Blue Swirl
	# suppression flags
1190 1ec3f6f9 Blue Swirl
	my %suppress_ifbraces;
1191 1ec3f6f9 Blue Swirl
	my %suppress_whiletrailers;
1192 1ec3f6f9 Blue Swirl
	my %suppress_export;
1193 1ec3f6f9 Blue Swirl
1194 1ec3f6f9 Blue Swirl
	# Pre-scan the patch sanitizing the lines.
1195 1ec3f6f9 Blue Swirl
	# Pre-scan the patch looking for any __setup documentation.
1196 1ec3f6f9 Blue Swirl
	#
1197 1ec3f6f9 Blue Swirl
	my @setup_docs = ();
1198 1ec3f6f9 Blue Swirl
	my $setup_docs = 0;
1199 1ec3f6f9 Blue Swirl
1200 1ec3f6f9 Blue Swirl
	sanitise_line_reset();
1201 1ec3f6f9 Blue Swirl
	my $line;
1202 1ec3f6f9 Blue Swirl
	foreach my $rawline (@rawlines) {
1203 1ec3f6f9 Blue Swirl
		$linenr++;
1204 1ec3f6f9 Blue Swirl
		$line = $rawline;
1205 1ec3f6f9 Blue Swirl
1206 1ec3f6f9 Blue Swirl
		if ($rawline=~/^\+\+\+\s+(\S+)/) {
1207 1ec3f6f9 Blue Swirl
			$setup_docs = 0;
1208 1ec3f6f9 Blue Swirl
			if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
1209 1ec3f6f9 Blue Swirl
				$setup_docs = 1;
1210 1ec3f6f9 Blue Swirl
			}
1211 1ec3f6f9 Blue Swirl
			#next;
1212 1ec3f6f9 Blue Swirl
		}
1213 1ec3f6f9 Blue Swirl
		if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1214 1ec3f6f9 Blue Swirl
			$realline=$1-1;
1215 1ec3f6f9 Blue Swirl
			if (defined $2) {
1216 1ec3f6f9 Blue Swirl
				$realcnt=$3+1;
1217 1ec3f6f9 Blue Swirl
			} else {
1218 1ec3f6f9 Blue Swirl
				$realcnt=1+1;
1219 1ec3f6f9 Blue Swirl
			}
1220 1ec3f6f9 Blue Swirl
			$in_comment = 0;
1221 1ec3f6f9 Blue Swirl
1222 1ec3f6f9 Blue Swirl
			# Guestimate if this is a continuing comment.  Run
1223 1ec3f6f9 Blue Swirl
			# the context looking for a comment "edge".  If this
1224 1ec3f6f9 Blue Swirl
			# edge is a close comment then we must be in a comment
1225 1ec3f6f9 Blue Swirl
			# at context start.
1226 1ec3f6f9 Blue Swirl
			my $edge;
1227 1ec3f6f9 Blue Swirl
			my $cnt = $realcnt;
1228 1ec3f6f9 Blue Swirl
			for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
1229 1ec3f6f9 Blue Swirl
				next if (defined $rawlines[$ln - 1] &&
1230 1ec3f6f9 Blue Swirl
					 $rawlines[$ln - 1] =~ /^-/);
1231 1ec3f6f9 Blue Swirl
				$cnt--;
1232 1ec3f6f9 Blue Swirl
				#print "RAW<$rawlines[$ln - 1]>\n";
1233 1ec3f6f9 Blue Swirl
				last if (!defined $rawlines[$ln - 1]);
1234 1ec3f6f9 Blue Swirl
				if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
1235 1ec3f6f9 Blue Swirl
				    $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
1236 1ec3f6f9 Blue Swirl
					($edge) = $1;
1237 1ec3f6f9 Blue Swirl
					last;
1238 1ec3f6f9 Blue Swirl
				}
1239 1ec3f6f9 Blue Swirl
			}
1240 1ec3f6f9 Blue Swirl
			if (defined $edge && $edge eq '*/') {
1241 1ec3f6f9 Blue Swirl
				$in_comment = 1;
1242 1ec3f6f9 Blue Swirl
			}
1243 1ec3f6f9 Blue Swirl
1244 1ec3f6f9 Blue Swirl
			# Guestimate if this is a continuing comment.  If this
1245 1ec3f6f9 Blue Swirl
			# is the start of a diff block and this line starts
1246 1ec3f6f9 Blue Swirl
			# ' *' then it is very likely a comment.
1247 1ec3f6f9 Blue Swirl
			if (!defined $edge &&
1248 1ec3f6f9 Blue Swirl
			    $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
1249 1ec3f6f9 Blue Swirl
			{
1250 1ec3f6f9 Blue Swirl
				$in_comment = 1;
1251 1ec3f6f9 Blue Swirl
			}
1252 1ec3f6f9 Blue Swirl
1253 1ec3f6f9 Blue Swirl
			##print "COMMENT:$in_comment edge<$edge> $rawline\n";
1254 1ec3f6f9 Blue Swirl
			sanitise_line_reset($in_comment);
1255 1ec3f6f9 Blue Swirl
1256 1ec3f6f9 Blue Swirl
		} elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
1257 1ec3f6f9 Blue Swirl
			# Standardise the strings and chars within the input to
1258 1ec3f6f9 Blue Swirl
			# simplify matching -- only bother with positive lines.
1259 1ec3f6f9 Blue Swirl
			$line = sanitise_line($rawline);
1260 1ec3f6f9 Blue Swirl
		}
1261 1ec3f6f9 Blue Swirl
		push(@lines, $line);
1262 1ec3f6f9 Blue Swirl
1263 1ec3f6f9 Blue Swirl
		if ($realcnt > 1) {
1264 1ec3f6f9 Blue Swirl
			$realcnt-- if ($line =~ /^(?:\+| |$)/);
1265 1ec3f6f9 Blue Swirl
		} else {
1266 1ec3f6f9 Blue Swirl
			$realcnt = 0;
1267 1ec3f6f9 Blue Swirl
		}
1268 1ec3f6f9 Blue Swirl
1269 1ec3f6f9 Blue Swirl
		#print "==>$rawline\n";
1270 1ec3f6f9 Blue Swirl
		#print "-->$line\n";
1271 1ec3f6f9 Blue Swirl
1272 1ec3f6f9 Blue Swirl
		if ($setup_docs && $line =~ /^\+/) {
1273 1ec3f6f9 Blue Swirl
			push(@setup_docs, $line);
1274 1ec3f6f9 Blue Swirl
		}
1275 1ec3f6f9 Blue Swirl
	}
1276 1ec3f6f9 Blue Swirl
1277 1ec3f6f9 Blue Swirl
	$prefix = '';
1278 1ec3f6f9 Blue Swirl
1279 1ec3f6f9 Blue Swirl
	$realcnt = 0;
1280 1ec3f6f9 Blue Swirl
	$linenr = 0;
1281 1ec3f6f9 Blue Swirl
	foreach my $line (@lines) {
1282 1ec3f6f9 Blue Swirl
		$linenr++;
1283 1ec3f6f9 Blue Swirl
1284 1ec3f6f9 Blue Swirl
		my $rawline = $rawlines[$linenr - 1];
1285 1ec3f6f9 Blue Swirl
1286 1ec3f6f9 Blue Swirl
#extract the line range in the file after the patch is applied
1287 1ec3f6f9 Blue Swirl
		if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1288 1ec3f6f9 Blue Swirl
			$is_patch = 1;
1289 1ec3f6f9 Blue Swirl
			$first_line = $linenr + 1;
1290 1ec3f6f9 Blue Swirl
			$realline=$1-1;
1291 1ec3f6f9 Blue Swirl
			if (defined $2) {
1292 1ec3f6f9 Blue Swirl
				$realcnt=$3+1;
1293 1ec3f6f9 Blue Swirl
			} else {
1294 1ec3f6f9 Blue Swirl
				$realcnt=1+1;
1295 1ec3f6f9 Blue Swirl
			}
1296 1ec3f6f9 Blue Swirl
			annotate_reset();
1297 1ec3f6f9 Blue Swirl
			$prev_values = 'E';
1298 1ec3f6f9 Blue Swirl
1299 1ec3f6f9 Blue Swirl
			%suppress_ifbraces = ();
1300 1ec3f6f9 Blue Swirl
			%suppress_whiletrailers = ();
1301 1ec3f6f9 Blue Swirl
			%suppress_export = ();
1302 1ec3f6f9 Blue Swirl
			next;
1303 1ec3f6f9 Blue Swirl
1304 1ec3f6f9 Blue Swirl
# track the line number as we move through the hunk, note that
1305 1ec3f6f9 Blue Swirl
# new versions of GNU diff omit the leading space on completely
1306 1ec3f6f9 Blue Swirl
# blank context lines so we need to count that too.
1307 1ec3f6f9 Blue Swirl
		} elsif ($line =~ /^( |\+|$)/) {
1308 1ec3f6f9 Blue Swirl
			$realline++;
1309 1ec3f6f9 Blue Swirl
			$realcnt-- if ($realcnt != 0);
1310 1ec3f6f9 Blue Swirl
1311 1ec3f6f9 Blue Swirl
			# Measure the line length and indent.
1312 1ec3f6f9 Blue Swirl
			($length, $indent) = line_stats($rawline);
1313 1ec3f6f9 Blue Swirl
1314 1ec3f6f9 Blue Swirl
			# Track the previous line.
1315 1ec3f6f9 Blue Swirl
			($prevline, $stashline) = ($stashline, $line);
1316 1ec3f6f9 Blue Swirl
			($previndent, $stashindent) = ($stashindent, $indent);
1317 1ec3f6f9 Blue Swirl
			($prevrawline, $stashrawline) = ($stashrawline, $rawline);
1318 1ec3f6f9 Blue Swirl
1319 1ec3f6f9 Blue Swirl
			#warn "line<$line>\n";
1320 1ec3f6f9 Blue Swirl
1321 1ec3f6f9 Blue Swirl
		} elsif ($realcnt == 1) {
1322 1ec3f6f9 Blue Swirl
			$realcnt--;
1323 1ec3f6f9 Blue Swirl
		}
1324 1ec3f6f9 Blue Swirl
1325 1ec3f6f9 Blue Swirl
		my $hunk_line = ($realcnt != 0);
1326 1ec3f6f9 Blue Swirl
1327 1ec3f6f9 Blue Swirl
#make up the handle for any error we report on this line
1328 1ec3f6f9 Blue Swirl
		$prefix = "$filename:$realline: " if ($emacs && $file);
1329 1ec3f6f9 Blue Swirl
		$prefix = "$filename:$linenr: " if ($emacs && !$file);
1330 1ec3f6f9 Blue Swirl
1331 1ec3f6f9 Blue Swirl
		$here = "#$linenr: " if (!$file);
1332 1ec3f6f9 Blue Swirl
		$here = "#$realline: " if ($file);
1333 1ec3f6f9 Blue Swirl
1334 1ec3f6f9 Blue Swirl
		# extract the filename as it passes
1335 1ec3f6f9 Blue Swirl
		if ($line =~ /^diff --git.*?(\S+)$/) {
1336 1ec3f6f9 Blue Swirl
			$realfile = $1;
1337 1ec3f6f9 Blue Swirl
			$realfile =~ s@^([^/]*)/@@;
1338 1ec3f6f9 Blue Swirl
1339 1ec3f6f9 Blue Swirl
		} elsif ($line =~ /^\+\+\+\s+(\S+)/) {
1340 1ec3f6f9 Blue Swirl
			$realfile = $1;
1341 1ec3f6f9 Blue Swirl
			$realfile =~ s@^([^/]*)/@@;
1342 1ec3f6f9 Blue Swirl
1343 1ec3f6f9 Blue Swirl
			$p1_prefix = $1;
1344 1ec3f6f9 Blue Swirl
			if (!$file && $tree && $p1_prefix ne '' &&
1345 1ec3f6f9 Blue Swirl
			    -e "$root/$p1_prefix") {
1346 1ec3f6f9 Blue Swirl
				WARN("patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
1347 1ec3f6f9 Blue Swirl
			}
1348 1ec3f6f9 Blue Swirl
1349 1ec3f6f9 Blue Swirl
			if ($realfile =~ m@^include/asm/@) {
1350 1ec3f6f9 Blue Swirl
				ERROR("do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
1351 1ec3f6f9 Blue Swirl
			}
1352 1ec3f6f9 Blue Swirl
			next;
1353 1ec3f6f9 Blue Swirl
		}
1354 1ec3f6f9 Blue Swirl
1355 1ec3f6f9 Blue Swirl
		$here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
1356 1ec3f6f9 Blue Swirl
1357 1ec3f6f9 Blue Swirl
		my $hereline = "$here\n$rawline\n";
1358 1ec3f6f9 Blue Swirl
		my $herecurr = "$here\n$rawline\n";
1359 1ec3f6f9 Blue Swirl
		my $hereprev = "$here\n$prevrawline\n$rawline\n";
1360 1ec3f6f9 Blue Swirl
1361 1ec3f6f9 Blue Swirl
		$cnt_lines++ if ($realcnt != 0);
1362 1ec3f6f9 Blue Swirl
1363 1ec3f6f9 Blue Swirl
# Check for incorrect file permissions
1364 1ec3f6f9 Blue Swirl
		if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
1365 1ec3f6f9 Blue Swirl
			my $permhere = $here . "FILE: $realfile\n";
1366 1ec3f6f9 Blue Swirl
			if ($realfile =~ /(Makefile|Kconfig|\.c|\.h|\.S|\.tmpl)$/) {
1367 1ec3f6f9 Blue Swirl
				ERROR("do not set execute permissions for source files\n" . $permhere);
1368 1ec3f6f9 Blue Swirl
			}
1369 1ec3f6f9 Blue Swirl
		}
1370 1ec3f6f9 Blue Swirl
1371 1ec3f6f9 Blue Swirl
#check the patch for a signoff:
1372 1ec3f6f9 Blue Swirl
		if ($line =~ /^\s*signed-off-by:/i) {
1373 1ec3f6f9 Blue Swirl
			# This is a signoff, if ugly, so do not double report.
1374 1ec3f6f9 Blue Swirl
			$signoff++;
1375 1ec3f6f9 Blue Swirl
			if (!($line =~ /^\s*Signed-off-by:/)) {
1376 1ec3f6f9 Blue Swirl
				WARN("Signed-off-by: is the preferred form\n" .
1377 1ec3f6f9 Blue Swirl
					$herecurr);
1378 1ec3f6f9 Blue Swirl
			}
1379 1ec3f6f9 Blue Swirl
			if ($line =~ /^\s*signed-off-by:\S/i) {
1380 1ec3f6f9 Blue Swirl
				WARN("space required after Signed-off-by:\n" .
1381 1ec3f6f9 Blue Swirl
					$herecurr);
1382 1ec3f6f9 Blue Swirl
			}
1383 1ec3f6f9 Blue Swirl
		}
1384 1ec3f6f9 Blue Swirl
1385 1ec3f6f9 Blue Swirl
# Check for wrappage within a valid hunk of the file
1386 1ec3f6f9 Blue Swirl
		if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
1387 1ec3f6f9 Blue Swirl
			ERROR("patch seems to be corrupt (line wrapped?)\n" .
1388 1ec3f6f9 Blue Swirl
				$herecurr) if (!$emitted_corrupt++);
1389 1ec3f6f9 Blue Swirl
		}
1390 1ec3f6f9 Blue Swirl
1391 1ec3f6f9 Blue Swirl
# Check for absolute kernel paths.
1392 1ec3f6f9 Blue Swirl
		if ($tree) {
1393 1ec3f6f9 Blue Swirl
			while ($line =~ m{(?:^|\s)(/\S*)}g) {
1394 1ec3f6f9 Blue Swirl
				my $file = $1;
1395 1ec3f6f9 Blue Swirl
1396 1ec3f6f9 Blue Swirl
				if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
1397 1ec3f6f9 Blue Swirl
				    check_absolute_file($1, $herecurr)) {
1398 1ec3f6f9 Blue Swirl
					#
1399 1ec3f6f9 Blue Swirl
				} else {
1400 1ec3f6f9 Blue Swirl
					check_absolute_file($file, $herecurr);
1401 1ec3f6f9 Blue Swirl
				}
1402 1ec3f6f9 Blue Swirl
			}
1403 1ec3f6f9 Blue Swirl
		}
1404 1ec3f6f9 Blue Swirl
1405 1ec3f6f9 Blue Swirl
# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
1406 1ec3f6f9 Blue Swirl
		if (($realfile =~ /^$/ || $line =~ /^\+/) &&
1407 1ec3f6f9 Blue Swirl
		    $rawline !~ m/^$UTF8*$/) {
1408 1ec3f6f9 Blue Swirl
			my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
1409 1ec3f6f9 Blue Swirl
1410 1ec3f6f9 Blue Swirl
			my $blank = copy_spacing($rawline);
1411 1ec3f6f9 Blue Swirl
			my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
1412 1ec3f6f9 Blue Swirl
			my $hereptr = "$hereline$ptr\n";
1413 1ec3f6f9 Blue Swirl
1414 1ec3f6f9 Blue Swirl
			ERROR("Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
1415 1ec3f6f9 Blue Swirl
		}
1416 1ec3f6f9 Blue Swirl
1417 1ec3f6f9 Blue Swirl
# ignore non-hunk lines and lines being removed
1418 1ec3f6f9 Blue Swirl
		next if (!$hunk_line || $line =~ /^-/);
1419 1ec3f6f9 Blue Swirl
1420 1ec3f6f9 Blue Swirl
#trailing whitespace
1421 1ec3f6f9 Blue Swirl
		if ($line =~ /^\+.*\015/) {
1422 1ec3f6f9 Blue Swirl
			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1423 1ec3f6f9 Blue Swirl
			ERROR("DOS line endings\n" . $herevet);
1424 1ec3f6f9 Blue Swirl
1425 1ec3f6f9 Blue Swirl
		} elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
1426 1ec3f6f9 Blue Swirl
			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1427 1ec3f6f9 Blue Swirl
			ERROR("trailing whitespace\n" . $herevet);
1428 1ec3f6f9 Blue Swirl
			$rpt_cleaners = 1;
1429 1ec3f6f9 Blue Swirl
		}
1430 1ec3f6f9 Blue Swirl
1431 1ec3f6f9 Blue Swirl
# check for Kconfig help text having a real description
1432 1ec3f6f9 Blue Swirl
# Only applies when adding the entry originally, after that we do not have
1433 1ec3f6f9 Blue Swirl
# sufficient context to determine whether it is indeed long enough.
1434 1ec3f6f9 Blue Swirl
		if ($realfile =~ /Kconfig/ &&
1435 1ec3f6f9 Blue Swirl
		    $line =~ /\+\s*(?:---)?help(?:---)?$/) {
1436 1ec3f6f9 Blue Swirl
			my $length = 0;
1437 1ec3f6f9 Blue Swirl
			my $cnt = $realcnt;
1438 1ec3f6f9 Blue Swirl
			my $ln = $linenr + 1;
1439 1ec3f6f9 Blue Swirl
			my $f;
1440 1ec3f6f9 Blue Swirl
			my $is_end = 0;
1441 1ec3f6f9 Blue Swirl
			while ($cnt > 0 && defined $lines[$ln - 1]) {
1442 1ec3f6f9 Blue Swirl
				$f = $lines[$ln - 1];
1443 1ec3f6f9 Blue Swirl
				$cnt-- if ($lines[$ln - 1] !~ /^-/);
1444 1ec3f6f9 Blue Swirl
				$is_end = $lines[$ln - 1] =~ /^\+/;
1445 1ec3f6f9 Blue Swirl
				$ln++;
1446 1ec3f6f9 Blue Swirl
1447 1ec3f6f9 Blue Swirl
				next if ($f =~ /^-/);
1448 1ec3f6f9 Blue Swirl
				$f =~ s/^.//;
1449 1ec3f6f9 Blue Swirl
				$f =~ s/#.*//;
1450 1ec3f6f9 Blue Swirl
				$f =~ s/^\s+//;
1451 1ec3f6f9 Blue Swirl
				next if ($f =~ /^$/);
1452 1ec3f6f9 Blue Swirl
				if ($f =~ /^\s*config\s/) {
1453 1ec3f6f9 Blue Swirl
					$is_end = 1;
1454 1ec3f6f9 Blue Swirl
					last;
1455 1ec3f6f9 Blue Swirl
				}
1456 1ec3f6f9 Blue Swirl
				$length++;
1457 1ec3f6f9 Blue Swirl
			}
1458 1ec3f6f9 Blue Swirl
			WARN("please write a paragraph that describes the config symbol fully\n" . $herecurr) if ($is_end && $length < 4);
1459 1ec3f6f9 Blue Swirl
			#print "is_end<$is_end> length<$length>\n";
1460 1ec3f6f9 Blue Swirl
		}
1461 1ec3f6f9 Blue Swirl
1462 1ec3f6f9 Blue Swirl
# check we are in a valid source file if not then ignore this hunk
1463 1ec3f6f9 Blue Swirl
		next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
1464 1ec3f6f9 Blue Swirl
1465 1ec3f6f9 Blue Swirl
#80 column limit
1466 1ec3f6f9 Blue Swirl
		if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ &&
1467 1ec3f6f9 Blue Swirl
		    $rawline !~ /^.\s*\*\s*\@$Ident\s/ &&
1468 1ec3f6f9 Blue Swirl
		    !($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(KERN_\S+\s*|[^"]*))?"[X\t]*"\s*(?:,|\)\s*;)\s*$/ ||
1469 1ec3f6f9 Blue Swirl
		    $line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) &&
1470 1ec3f6f9 Blue Swirl
		    $length > 80)
1471 1ec3f6f9 Blue Swirl
		{
1472 1ec3f6f9 Blue Swirl
			WARN("line over 80 characters\n" . $herecurr);
1473 1ec3f6f9 Blue Swirl
		}
1474 1ec3f6f9 Blue Swirl
1475 1ec3f6f9 Blue Swirl
# check for spaces before a quoted newline
1476 1ec3f6f9 Blue Swirl
		if ($rawline =~ /^.*\".*\s\\n/) {
1477 1ec3f6f9 Blue Swirl
			WARN("unnecessary whitespace before a quoted newline\n" . $herecurr);
1478 1ec3f6f9 Blue Swirl
		}
1479 1ec3f6f9 Blue Swirl
1480 1ec3f6f9 Blue Swirl
# check for adding lines without a newline.
1481 1ec3f6f9 Blue Swirl
		if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
1482 1ec3f6f9 Blue Swirl
			WARN("adding a line without newline at end of file\n" . $herecurr);
1483 1ec3f6f9 Blue Swirl
		}
1484 1ec3f6f9 Blue Swirl
1485 1ec3f6f9 Blue Swirl
# Blackfin: use hi/lo macros
1486 1ec3f6f9 Blue Swirl
		if ($realfile =~ m@arch/blackfin/.*\.S$@) {
1487 1ec3f6f9 Blue Swirl
			if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
1488 1ec3f6f9 Blue Swirl
				my $herevet = "$here\n" . cat_vet($line) . "\n";
1489 1ec3f6f9 Blue Swirl
				ERROR("use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
1490 1ec3f6f9 Blue Swirl
			}
1491 1ec3f6f9 Blue Swirl
			if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
1492 1ec3f6f9 Blue Swirl
				my $herevet = "$here\n" . cat_vet($line) . "\n";
1493 1ec3f6f9 Blue Swirl
				ERROR("use the HI() macro, not (... >> 16)\n" . $herevet);
1494 1ec3f6f9 Blue Swirl
			}
1495 1ec3f6f9 Blue Swirl
		}
1496 1ec3f6f9 Blue Swirl
1497 1ec3f6f9 Blue Swirl
# check we are in a valid source file C or perl if not then ignore this hunk
1498 1ec3f6f9 Blue Swirl
		next if ($realfile !~ /\.(h|c|pl)$/);
1499 1ec3f6f9 Blue Swirl
1500 b6469683 Blue Swirl
# in QEMU, no tabs are allowed
1501 ad36ce8b Blue Swirl
		if ($rawline =~ /^\+.*\t/) {
1502 1ec3f6f9 Blue Swirl
			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1503 b6469683 Blue Swirl
			ERROR("code indent should never use tabs\n" . $herevet);
1504 1ec3f6f9 Blue Swirl
			$rpt_cleaners = 1;
1505 1ec3f6f9 Blue Swirl
		}
1506 1ec3f6f9 Blue Swirl
1507 1ec3f6f9 Blue Swirl
# check we are in a valid C source file if not then ignore this hunk
1508 1ec3f6f9 Blue Swirl
		next if ($realfile !~ /\.(h|c)$/);
1509 1ec3f6f9 Blue Swirl
1510 1ec3f6f9 Blue Swirl
# check for RCS/CVS revision markers
1511 1ec3f6f9 Blue Swirl
		if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
1512 1ec3f6f9 Blue Swirl
			WARN("CVS style keyword markers, these will _not_ be updated\n". $herecurr);
1513 1ec3f6f9 Blue Swirl
		}
1514 1ec3f6f9 Blue Swirl
1515 1ec3f6f9 Blue Swirl
# Blackfin: don't use __builtin_bfin_[cs]sync
1516 1ec3f6f9 Blue Swirl
		if ($line =~ /__builtin_bfin_csync/) {
1517 1ec3f6f9 Blue Swirl
			my $herevet = "$here\n" . cat_vet($line) . "\n";
1518 1ec3f6f9 Blue Swirl
			ERROR("use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
1519 1ec3f6f9 Blue Swirl
		}
1520 1ec3f6f9 Blue Swirl
		if ($line =~ /__builtin_bfin_ssync/) {
1521 1ec3f6f9 Blue Swirl
			my $herevet = "$here\n" . cat_vet($line) . "\n";
1522 1ec3f6f9 Blue Swirl
			ERROR("use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
1523 1ec3f6f9 Blue Swirl
		}
1524 1ec3f6f9 Blue Swirl
1525 1ec3f6f9 Blue Swirl
# Check for potential 'bare' types
1526 1ec3f6f9 Blue Swirl
		my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
1527 1ec3f6f9 Blue Swirl
		    $realline_next);
1528 1ec3f6f9 Blue Swirl
		if ($realcnt && $line =~ /.\s*\S/) {
1529 1ec3f6f9 Blue Swirl
			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
1530 1ec3f6f9 Blue Swirl
				ctx_statement_block($linenr, $realcnt, 0);
1531 1ec3f6f9 Blue Swirl
			$stat =~ s/\n./\n /g;
1532 1ec3f6f9 Blue Swirl
			$cond =~ s/\n./\n /g;
1533 1ec3f6f9 Blue Swirl
1534 1ec3f6f9 Blue Swirl
			# Find the real next line.
1535 1ec3f6f9 Blue Swirl
			$realline_next = $line_nr_next;
1536 1ec3f6f9 Blue Swirl
			if (defined $realline_next &&
1537 1ec3f6f9 Blue Swirl
			    (!defined $lines[$realline_next - 1] ||
1538 1ec3f6f9 Blue Swirl
			     substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
1539 1ec3f6f9 Blue Swirl
				$realline_next++;
1540 1ec3f6f9 Blue Swirl
			}
1541 1ec3f6f9 Blue Swirl
1542 1ec3f6f9 Blue Swirl
			my $s = $stat;
1543 1ec3f6f9 Blue Swirl
			$s =~ s/{.*$//s;
1544 1ec3f6f9 Blue Swirl
1545 1ec3f6f9 Blue Swirl
			# Ignore goto labels.
1546 1ec3f6f9 Blue Swirl
			if ($s =~ /$Ident:\*$/s) {
1547 1ec3f6f9 Blue Swirl
1548 1ec3f6f9 Blue Swirl
			# Ignore functions being called
1549 1ec3f6f9 Blue Swirl
			} elsif ($s =~ /^.\s*$Ident\s*\(/s) {
1550 1ec3f6f9 Blue Swirl
1551 1ec3f6f9 Blue Swirl
			} elsif ($s =~ /^.\s*else\b/s) {
1552 1ec3f6f9 Blue Swirl
1553 1ec3f6f9 Blue Swirl
			# declarations always start with types
1554 1ec3f6f9 Blue Swirl
			} 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) {
1555 1ec3f6f9 Blue Swirl
				my $type = $1;
1556 1ec3f6f9 Blue Swirl
				$type =~ s/\s+/ /g;
1557 1ec3f6f9 Blue Swirl
				possible($type, "A:" . $s);
1558 1ec3f6f9 Blue Swirl
1559 1ec3f6f9 Blue Swirl
			# definitions in global scope can only start with types
1560 1ec3f6f9 Blue Swirl
			} elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
1561 1ec3f6f9 Blue Swirl
				possible($1, "B:" . $s);
1562 1ec3f6f9 Blue Swirl
			}
1563 1ec3f6f9 Blue Swirl
1564 1ec3f6f9 Blue Swirl
			# any (foo ... *) is a pointer cast, and foo is a type
1565 1ec3f6f9 Blue Swirl
			while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
1566 1ec3f6f9 Blue Swirl
				possible($1, "C:" . $s);
1567 1ec3f6f9 Blue Swirl
			}
1568 1ec3f6f9 Blue Swirl
1569 1ec3f6f9 Blue Swirl
			# Check for any sort of function declaration.
1570 1ec3f6f9 Blue Swirl
			# int foo(something bar, other baz);
1571 1ec3f6f9 Blue Swirl
			# void (*store_gdt)(x86_descr_ptr *);
1572 1ec3f6f9 Blue Swirl
			if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
1573 1ec3f6f9 Blue Swirl
				my ($name_len) = length($1);
1574 1ec3f6f9 Blue Swirl
1575 1ec3f6f9 Blue Swirl
				my $ctx = $s;
1576 1ec3f6f9 Blue Swirl
				substr($ctx, 0, $name_len + 1, '');
1577 1ec3f6f9 Blue Swirl
				$ctx =~ s/\)[^\)]*$//;
1578 1ec3f6f9 Blue Swirl
1579 1ec3f6f9 Blue Swirl
				for my $arg (split(/\s*,\s*/, $ctx)) {
1580 1ec3f6f9 Blue Swirl
					if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
1581 1ec3f6f9 Blue Swirl
1582 1ec3f6f9 Blue Swirl
						possible($1, "D:" . $s);
1583 1ec3f6f9 Blue Swirl
					}
1584 1ec3f6f9 Blue Swirl
				}
1585 1ec3f6f9 Blue Swirl
			}
1586 1ec3f6f9 Blue Swirl
1587 1ec3f6f9 Blue Swirl
		}
1588 1ec3f6f9 Blue Swirl
1589 1ec3f6f9 Blue Swirl
#
1590 1ec3f6f9 Blue Swirl
# Checks which may be anchored in the context.
1591 1ec3f6f9 Blue Swirl
#
1592 1ec3f6f9 Blue Swirl
1593 1ec3f6f9 Blue Swirl
# Check for switch () and associated case and default
1594 1ec3f6f9 Blue Swirl
# statements should be at the same indent.
1595 1ec3f6f9 Blue Swirl
		if ($line=~/\bswitch\s*\(.*\)/) {
1596 1ec3f6f9 Blue Swirl
			my $err = '';
1597 1ec3f6f9 Blue Swirl
			my $sep = '';
1598 1ec3f6f9 Blue Swirl
			my @ctx = ctx_block_outer($linenr, $realcnt);
1599 1ec3f6f9 Blue Swirl
			shift(@ctx);
1600 1ec3f6f9 Blue Swirl
			for my $ctx (@ctx) {
1601 1ec3f6f9 Blue Swirl
				my ($clen, $cindent) = line_stats($ctx);
1602 1ec3f6f9 Blue Swirl
				if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
1603 1ec3f6f9 Blue Swirl
							$indent != $cindent) {
1604 1ec3f6f9 Blue Swirl
					$err .= "$sep$ctx\n";
1605 1ec3f6f9 Blue Swirl
					$sep = '';
1606 1ec3f6f9 Blue Swirl
				} else {
1607 1ec3f6f9 Blue Swirl
					$sep = "[...]\n";
1608 1ec3f6f9 Blue Swirl
				}
1609 1ec3f6f9 Blue Swirl
			}
1610 1ec3f6f9 Blue Swirl
			if ($err ne '') {
1611 1ec3f6f9 Blue Swirl
				ERROR("switch and case should be at the same indent\n$hereline$err");
1612 1ec3f6f9 Blue Swirl
			}
1613 1ec3f6f9 Blue Swirl
		}
1614 1ec3f6f9 Blue Swirl
1615 1ec3f6f9 Blue Swirl
# if/while/etc brace do not go on next line, unless defining a do while loop,
1616 1ec3f6f9 Blue Swirl
# or if that brace on the next line is for something else
1617 1ec3f6f9 Blue Swirl
		if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
1618 1ec3f6f9 Blue Swirl
			my $pre_ctx = "$1$2";
1619 1ec3f6f9 Blue Swirl
1620 1ec3f6f9 Blue Swirl
			my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
1621 1ec3f6f9 Blue Swirl
			my $ctx_cnt = $realcnt - $#ctx - 1;
1622 1ec3f6f9 Blue Swirl
			my $ctx = join("\n", @ctx);
1623 1ec3f6f9 Blue Swirl
1624 1ec3f6f9 Blue Swirl
			my $ctx_ln = $linenr;
1625 1ec3f6f9 Blue Swirl
			my $ctx_skip = $realcnt;
1626 1ec3f6f9 Blue Swirl
1627 1ec3f6f9 Blue Swirl
			while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
1628 1ec3f6f9 Blue Swirl
					defined $lines[$ctx_ln - 1] &&
1629 1ec3f6f9 Blue Swirl
					$lines[$ctx_ln - 1] =~ /^-/)) {
1630 1ec3f6f9 Blue Swirl
				##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
1631 1ec3f6f9 Blue Swirl
				$ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
1632 1ec3f6f9 Blue Swirl
				$ctx_ln++;
1633 1ec3f6f9 Blue Swirl
			}
1634 1ec3f6f9 Blue Swirl
1635 1ec3f6f9 Blue Swirl
			#print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
1636 1ec3f6f9 Blue Swirl
			#print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
1637 1ec3f6f9 Blue Swirl
1638 1ec3f6f9 Blue Swirl
			if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln -1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
1639 1ec3f6f9 Blue Swirl
				ERROR("that open brace { should be on the previous line\n" .
1640 1ec3f6f9 Blue Swirl
					"$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
1641 1ec3f6f9 Blue Swirl
			}
1642 1ec3f6f9 Blue Swirl
			if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
1643 1ec3f6f9 Blue Swirl
			    $ctx =~ /\)\s*\;\s*$/ &&
1644 1ec3f6f9 Blue Swirl
			    defined $lines[$ctx_ln - 1])
1645 1ec3f6f9 Blue Swirl
			{
1646 1ec3f6f9 Blue Swirl
				my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
1647 1ec3f6f9 Blue Swirl
				if ($nindent > $indent) {
1648 1ec3f6f9 Blue Swirl
					WARN("trailing semicolon indicates no statements, indent implies otherwise\n" .
1649 1ec3f6f9 Blue Swirl
						"$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
1650 1ec3f6f9 Blue Swirl
				}
1651 1ec3f6f9 Blue Swirl
			}
1652 1ec3f6f9 Blue Swirl
		}
1653 1ec3f6f9 Blue Swirl
1654 1ec3f6f9 Blue Swirl
# Check relative indent for conditionals and blocks.
1655 1ec3f6f9 Blue Swirl
		if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
1656 1ec3f6f9 Blue Swirl
			my ($s, $c) = ($stat, $cond);
1657 1ec3f6f9 Blue Swirl
1658 1ec3f6f9 Blue Swirl
			substr($s, 0, length($c), '');
1659 1ec3f6f9 Blue Swirl
1660 1ec3f6f9 Blue Swirl
			# Make sure we remove the line prefixes as we have
1661 1ec3f6f9 Blue Swirl
			# none on the first line, and are going to readd them
1662 1ec3f6f9 Blue Swirl
			# where necessary.
1663 1ec3f6f9 Blue Swirl
			$s =~ s/\n./\n/gs;
1664 1ec3f6f9 Blue Swirl
1665 1ec3f6f9 Blue Swirl
			# Find out how long the conditional actually is.
1666 1ec3f6f9 Blue Swirl
			my @newlines = ($c =~ /\n/gs);
1667 1ec3f6f9 Blue Swirl
			my $cond_lines = 1 + $#newlines;
1668 1ec3f6f9 Blue Swirl
1669 1ec3f6f9 Blue Swirl
			# We want to check the first line inside the block
1670 1ec3f6f9 Blue Swirl
			# starting at the end of the conditional, so remove:
1671 1ec3f6f9 Blue Swirl
			#  1) any blank line termination
1672 1ec3f6f9 Blue Swirl
			#  2) any opening brace { on end of the line
1673 1ec3f6f9 Blue Swirl
			#  3) any do (...) {
1674 1ec3f6f9 Blue Swirl
			my $continuation = 0;
1675 1ec3f6f9 Blue Swirl
			my $check = 0;
1676 1ec3f6f9 Blue Swirl
			$s =~ s/^.*\bdo\b//;
1677 1ec3f6f9 Blue Swirl
			$s =~ s/^\s*{//;
1678 1ec3f6f9 Blue Swirl
			if ($s =~ s/^\s*\\//) {
1679 1ec3f6f9 Blue Swirl
				$continuation = 1;
1680 1ec3f6f9 Blue Swirl
			}
1681 1ec3f6f9 Blue Swirl
			if ($s =~ s/^\s*?\n//) {
1682 1ec3f6f9 Blue Swirl
				$check = 1;
1683 1ec3f6f9 Blue Swirl
				$cond_lines++;
1684 1ec3f6f9 Blue Swirl
			}
1685 1ec3f6f9 Blue Swirl
1686 1ec3f6f9 Blue Swirl
			# Also ignore a loop construct at the end of a
1687 1ec3f6f9 Blue Swirl
			# preprocessor statement.
1688 1ec3f6f9 Blue Swirl
			if (($prevline =~ /^.\s*#\s*define\s/ ||
1689 1ec3f6f9 Blue Swirl
			    $prevline =~ /\\\s*$/) && $continuation == 0) {
1690 1ec3f6f9 Blue Swirl
				$check = 0;
1691 1ec3f6f9 Blue Swirl
			}
1692 1ec3f6f9 Blue Swirl
1693 1ec3f6f9 Blue Swirl
			my $cond_ptr = -1;
1694 1ec3f6f9 Blue Swirl
			$continuation = 0;
1695 1ec3f6f9 Blue Swirl
			while ($cond_ptr != $cond_lines) {
1696 1ec3f6f9 Blue Swirl
				$cond_ptr = $cond_lines;
1697 1ec3f6f9 Blue Swirl
1698 1ec3f6f9 Blue Swirl
				# If we see an #else/#elif then the code
1699 1ec3f6f9 Blue Swirl
				# is not linear.
1700 1ec3f6f9 Blue Swirl
				if ($s =~ /^\s*\#\s*(?:else|elif)/) {
1701 1ec3f6f9 Blue Swirl
					$check = 0;
1702 1ec3f6f9 Blue Swirl
				}
1703 1ec3f6f9 Blue Swirl
1704 1ec3f6f9 Blue Swirl
				# Ignore:
1705 1ec3f6f9 Blue Swirl
				#  1) blank lines, they should be at 0,
1706 1ec3f6f9 Blue Swirl
				#  2) preprocessor lines, and
1707 1ec3f6f9 Blue Swirl
				#  3) labels.
1708 1ec3f6f9 Blue Swirl
				if ($continuation ||
1709 1ec3f6f9 Blue Swirl
				    $s =~ /^\s*?\n/ ||
1710 1ec3f6f9 Blue Swirl
				    $s =~ /^\s*#\s*?/ ||
1711 1ec3f6f9 Blue Swirl
				    $s =~ /^\s*$Ident\s*:/) {
1712 1ec3f6f9 Blue Swirl
					$continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
1713 1ec3f6f9 Blue Swirl
					if ($s =~ s/^.*?\n//) {
1714 1ec3f6f9 Blue Swirl
						$cond_lines++;
1715 1ec3f6f9 Blue Swirl
					}
1716 1ec3f6f9 Blue Swirl
				}
1717 1ec3f6f9 Blue Swirl
			}
1718 1ec3f6f9 Blue Swirl
1719 1ec3f6f9 Blue Swirl
			my (undef, $sindent) = line_stats("+" . $s);
1720 1ec3f6f9 Blue Swirl
			my $stat_real = raw_line($linenr, $cond_lines);
1721 1ec3f6f9 Blue Swirl
1722 1ec3f6f9 Blue Swirl
			# Check if either of these lines are modified, else
1723 1ec3f6f9 Blue Swirl
			# this is not this patch's fault.
1724 1ec3f6f9 Blue Swirl
			if (!defined($stat_real) ||
1725 1ec3f6f9 Blue Swirl
			    $stat !~ /^\+/ && $stat_real !~ /^\+/) {
1726 1ec3f6f9 Blue Swirl
				$check = 0;
1727 1ec3f6f9 Blue Swirl
			}
1728 1ec3f6f9 Blue Swirl
			if (defined($stat_real) && $cond_lines > 1) {
1729 1ec3f6f9 Blue Swirl
				$stat_real = "[...]\n$stat_real";
1730 1ec3f6f9 Blue Swirl
			}
1731 1ec3f6f9 Blue Swirl
1732 1ec3f6f9 Blue Swirl
			#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";
1733 1ec3f6f9 Blue Swirl
1734 b6469683 Blue Swirl
			if ($check && (($sindent % 4) != 0 ||
1735 1ec3f6f9 Blue Swirl
			    ($sindent <= $indent && $s ne ''))) {
1736 1ec3f6f9 Blue Swirl
				WARN("suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
1737 1ec3f6f9 Blue Swirl
			}
1738 1ec3f6f9 Blue Swirl
		}
1739 1ec3f6f9 Blue Swirl
1740 1ec3f6f9 Blue Swirl
		# Track the 'values' across context and added lines.
1741 1ec3f6f9 Blue Swirl
		my $opline = $line; $opline =~ s/^./ /;
1742 1ec3f6f9 Blue Swirl
		my ($curr_values, $curr_vars) =
1743 1ec3f6f9 Blue Swirl
				annotate_values($opline . "\n", $prev_values);
1744 1ec3f6f9 Blue Swirl
		$curr_values = $prev_values . $curr_values;
1745 1ec3f6f9 Blue Swirl
		if ($dbg_values) {
1746 1ec3f6f9 Blue Swirl
			my $outline = $opline; $outline =~ s/\t/ /g;
1747 1ec3f6f9 Blue Swirl
			print "$linenr > .$outline\n";
1748 1ec3f6f9 Blue Swirl
			print "$linenr > $curr_values\n";
1749 1ec3f6f9 Blue Swirl
			print "$linenr >  $curr_vars\n";
1750 1ec3f6f9 Blue Swirl
		}
1751 1ec3f6f9 Blue Swirl
		$prev_values = substr($curr_values, -1);
1752 1ec3f6f9 Blue Swirl
1753 1ec3f6f9 Blue Swirl
#ignore lines not being added
1754 1ec3f6f9 Blue Swirl
		if ($line=~/^[^\+]/) {next;}
1755 1ec3f6f9 Blue Swirl
1756 1ec3f6f9 Blue Swirl
# TEST: allow direct testing of the type matcher.
1757 1ec3f6f9 Blue Swirl
		if ($dbg_type) {
1758 1ec3f6f9 Blue Swirl
			if ($line =~ /^.\s*$Declare\s*$/) {
1759 1ec3f6f9 Blue Swirl
				ERROR("TEST: is type\n" . $herecurr);
1760 1ec3f6f9 Blue Swirl
			} elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
1761 1ec3f6f9 Blue Swirl
				ERROR("TEST: is not type ($1 is)\n". $herecurr);
1762 1ec3f6f9 Blue Swirl
			}
1763 1ec3f6f9 Blue Swirl
			next;
1764 1ec3f6f9 Blue Swirl
		}
1765 1ec3f6f9 Blue Swirl
# TEST: allow direct testing of the attribute matcher.
1766 1ec3f6f9 Blue Swirl
		if ($dbg_attr) {
1767 1ec3f6f9 Blue Swirl
			if ($line =~ /^.\s*$Modifier\s*$/) {
1768 1ec3f6f9 Blue Swirl
				ERROR("TEST: is attr\n" . $herecurr);
1769 1ec3f6f9 Blue Swirl
			} elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
1770 1ec3f6f9 Blue Swirl
				ERROR("TEST: is not attr ($1 is)\n". $herecurr);
1771 1ec3f6f9 Blue Swirl
			}
1772 1ec3f6f9 Blue Swirl
			next;
1773 1ec3f6f9 Blue Swirl
		}
1774 1ec3f6f9 Blue Swirl
1775 1ec3f6f9 Blue Swirl
# check for initialisation to aggregates open brace on the next line
1776 1ec3f6f9 Blue Swirl
		if ($line =~ /^.\s*{/ &&
1777 1ec3f6f9 Blue Swirl
		    $prevline =~ /(?:^|[^=])=\s*$/) {
1778 1ec3f6f9 Blue Swirl
			ERROR("that open brace { should be on the previous line\n" . $hereprev);
1779 1ec3f6f9 Blue Swirl
		}
1780 1ec3f6f9 Blue Swirl
1781 1ec3f6f9 Blue Swirl
#
1782 1ec3f6f9 Blue Swirl
# Checks which are anchored on the added line.
1783 1ec3f6f9 Blue Swirl
#
1784 1ec3f6f9 Blue Swirl
1785 1ec3f6f9 Blue Swirl
# check for malformed paths in #include statements (uses RAW line)
1786 1ec3f6f9 Blue Swirl
		if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
1787 1ec3f6f9 Blue Swirl
			my $path = $1;
1788 1ec3f6f9 Blue Swirl
			if ($path =~ m{//}) {
1789 1ec3f6f9 Blue Swirl
				ERROR("malformed #include filename\n" .
1790 1ec3f6f9 Blue Swirl
					$herecurr);
1791 1ec3f6f9 Blue Swirl
			}
1792 1ec3f6f9 Blue Swirl
		}
1793 1ec3f6f9 Blue Swirl
1794 1ec3f6f9 Blue Swirl
# no C99 // comments
1795 1ec3f6f9 Blue Swirl
		if ($line =~ m{//}) {
1796 1ec3f6f9 Blue Swirl
			ERROR("do not use C99 // comments\n" . $herecurr);
1797 1ec3f6f9 Blue Swirl
		}
1798 1ec3f6f9 Blue Swirl
		# Remove C99 comments.
1799 1ec3f6f9 Blue Swirl
		$line =~ s@//.*@@;
1800 1ec3f6f9 Blue Swirl
		$opline =~ s@//.*@@;
1801 1ec3f6f9 Blue Swirl
1802 1ec3f6f9 Blue Swirl
# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
1803 1ec3f6f9 Blue Swirl
# the whole statement.
1804 1ec3f6f9 Blue Swirl
#print "APW <$lines[$realline_next - 1]>\n";
1805 1ec3f6f9 Blue Swirl
		if (defined $realline_next &&
1806 1ec3f6f9 Blue Swirl
		    exists $lines[$realline_next - 1] &&
1807 1ec3f6f9 Blue Swirl
		    !defined $suppress_export{$realline_next} &&
1808 1ec3f6f9 Blue Swirl
		    ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
1809 1ec3f6f9 Blue Swirl
		     $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
1810 1ec3f6f9 Blue Swirl
			# Handle definitions which produce identifiers with
1811 1ec3f6f9 Blue Swirl
			# a prefix:
1812 1ec3f6f9 Blue Swirl
			#   XXX(foo);
1813 1ec3f6f9 Blue Swirl
			#   EXPORT_SYMBOL(something_foo);
1814 1ec3f6f9 Blue Swirl
			my $name = $1;
1815 1ec3f6f9 Blue Swirl
			if ($stat =~ /^.([A-Z_]+)\s*\(\s*($Ident)/ &&
1816 1ec3f6f9 Blue Swirl
			    $name =~ /^${Ident}_$2/) {
1817 1ec3f6f9 Blue Swirl
#print "FOO C name<$name>\n";
1818 1ec3f6f9 Blue Swirl
				$suppress_export{$realline_next} = 1;
1819 1ec3f6f9 Blue Swirl
1820 1ec3f6f9 Blue Swirl
			} elsif ($stat !~ /(?:
1821 1ec3f6f9 Blue Swirl
				\n.}\s*$|
1822 1ec3f6f9 Blue Swirl
				^.DEFINE_$Ident\(\Q$name\E\)|
1823 1ec3f6f9 Blue Swirl
				^.DECLARE_$Ident\(\Q$name\E\)|
1824 1ec3f6f9 Blue Swirl
				^.LIST_HEAD\(\Q$name\E\)|
1825 1ec3f6f9 Blue Swirl
				^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
1826 1ec3f6f9 Blue Swirl
				\b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
1827 1ec3f6f9 Blue Swirl
			    )/x) {
1828 1ec3f6f9 Blue Swirl
#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
1829 1ec3f6f9 Blue Swirl
				$suppress_export{$realline_next} = 2;
1830 1ec3f6f9 Blue Swirl
			} else {
1831 1ec3f6f9 Blue Swirl
				$suppress_export{$realline_next} = 1;
1832 1ec3f6f9 Blue Swirl
			}
1833 1ec3f6f9 Blue Swirl
		}
1834 1ec3f6f9 Blue Swirl
		if (!defined $suppress_export{$linenr} &&
1835 1ec3f6f9 Blue Swirl
		    $prevline =~ /^.\s*$/ &&
1836 1ec3f6f9 Blue Swirl
		    ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
1837 1ec3f6f9 Blue Swirl
		     $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
1838 1ec3f6f9 Blue Swirl
#print "FOO B <$lines[$linenr - 1]>\n";
1839 1ec3f6f9 Blue Swirl
			$suppress_export{$linenr} = 2;
1840 1ec3f6f9 Blue Swirl
		}
1841 1ec3f6f9 Blue Swirl
		if (defined $suppress_export{$linenr} &&
1842 1ec3f6f9 Blue Swirl
		    $suppress_export{$linenr} == 2) {
1843 1ec3f6f9 Blue Swirl
			WARN("EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
1844 1ec3f6f9 Blue Swirl
		}
1845 1ec3f6f9 Blue Swirl
1846 1ec3f6f9 Blue Swirl
# check for global initialisers.
1847 1ec3f6f9 Blue Swirl
		if ($line =~ /^.$Type\s*$Ident\s*(?:\s+$Modifier)*\s*=\s*(0|NULL|false)\s*;/) {
1848 1ec3f6f9 Blue Swirl
			ERROR("do not initialise globals to 0 or NULL\n" .
1849 1ec3f6f9 Blue Swirl
				$herecurr);
1850 1ec3f6f9 Blue Swirl
		}
1851 1ec3f6f9 Blue Swirl
# check for static initialisers.
1852 1ec3f6f9 Blue Swirl
		if ($line =~ /\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
1853 1ec3f6f9 Blue Swirl
			ERROR("do not initialise statics to 0 or NULL\n" .
1854 1ec3f6f9 Blue Swirl
				$herecurr);
1855 1ec3f6f9 Blue Swirl
		}
1856 1ec3f6f9 Blue Swirl
1857 1ec3f6f9 Blue Swirl
# * goes on variable not on type
1858 1ec3f6f9 Blue Swirl
		# (char*[ const])
1859 1ec3f6f9 Blue Swirl
		if ($line =~ m{\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\)}) {
1860 1ec3f6f9 Blue Swirl
			my ($from, $to) = ($1, $1);
1861 1ec3f6f9 Blue Swirl
1862 1ec3f6f9 Blue Swirl
			# Should start with a space.
1863 1ec3f6f9 Blue Swirl
			$to =~ s/^(\S)/ $1/;
1864 1ec3f6f9 Blue Swirl
			# Should not end with a space.
1865 1ec3f6f9 Blue Swirl
			$to =~ s/\s+$//;
1866 1ec3f6f9 Blue Swirl
			# '*'s should not have spaces between.
1867 1ec3f6f9 Blue Swirl
			while ($to =~ s/\*\s+\*/\*\*/) {
1868 1ec3f6f9 Blue Swirl
			}
1869 1ec3f6f9 Blue Swirl
1870 1ec3f6f9 Blue Swirl
			#print "from<$from> to<$to>\n";
1871 1ec3f6f9 Blue Swirl
			if ($from ne $to) {
1872 1ec3f6f9 Blue Swirl
				ERROR("\"(foo$from)\" should be \"(foo$to)\"\n" .  $herecurr);
1873 1ec3f6f9 Blue Swirl
			}
1874 1ec3f6f9 Blue Swirl
		} elsif ($line =~ m{\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident)}) {
1875 1ec3f6f9 Blue Swirl
			my ($from, $to, $ident) = ($1, $1, $2);
1876 1ec3f6f9 Blue Swirl
1877 1ec3f6f9 Blue Swirl
			# Should start with a space.
1878 1ec3f6f9 Blue Swirl
			$to =~ s/^(\S)/ $1/;
1879 1ec3f6f9 Blue Swirl
			# Should not end with a space.
1880 1ec3f6f9 Blue Swirl
			$to =~ s/\s+$//;
1881 1ec3f6f9 Blue Swirl
			# '*'s should not have spaces between.
1882 1ec3f6f9 Blue Swirl
			while ($to =~ s/\*\s+\*/\*\*/) {
1883 1ec3f6f9 Blue Swirl
			}
1884 1ec3f6f9 Blue Swirl
			# Modifiers should have spaces.
1885 1ec3f6f9 Blue Swirl
			$to =~ s/(\b$Modifier$)/$1 /;
1886 1ec3f6f9 Blue Swirl
1887 1ec3f6f9 Blue Swirl
			#print "from<$from> to<$to> ident<$ident>\n";
1888 1ec3f6f9 Blue Swirl
			if ($from ne $to && $ident !~ /^$Modifier$/) {
1889 1ec3f6f9 Blue Swirl
				ERROR("\"foo${from}bar\" should be \"foo${to}bar\"\n" .  $herecurr);
1890 1ec3f6f9 Blue Swirl
			}
1891 1ec3f6f9 Blue Swirl
		}
1892 1ec3f6f9 Blue Swirl
1893 1ec3f6f9 Blue Swirl
# # no BUG() or BUG_ON()
1894 1ec3f6f9 Blue Swirl
# 		if ($line =~ /\b(BUG|BUG_ON)\b/) {
1895 1ec3f6f9 Blue Swirl
# 			print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
1896 1ec3f6f9 Blue Swirl
# 			print "$herecurr";
1897 1ec3f6f9 Blue Swirl
# 			$clean = 0;
1898 1ec3f6f9 Blue Swirl
# 		}
1899 1ec3f6f9 Blue Swirl
1900 1ec3f6f9 Blue Swirl
		if ($line =~ /\bLINUX_VERSION_CODE\b/) {
1901 1ec3f6f9 Blue Swirl
			WARN("LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
1902 1ec3f6f9 Blue Swirl
		}
1903 1ec3f6f9 Blue Swirl
1904 1ec3f6f9 Blue Swirl
# printk should use KERN_* levels.  Note that follow on printk's on the
1905 1ec3f6f9 Blue Swirl
# same line do not need a level, so we use the current block context
1906 1ec3f6f9 Blue Swirl
# to try and find and validate the current printk.  In summary the current
1907 68dfbcd4 Dong Xu Wang
# printk includes all preceding printk's which have no newline on the end.
1908 1ec3f6f9 Blue Swirl
# we assume the first bad printk is the one to report.
1909 1ec3f6f9 Blue Swirl
		if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
1910 1ec3f6f9 Blue Swirl
			my $ok = 0;
1911 1ec3f6f9 Blue Swirl
			for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
1912 1ec3f6f9 Blue Swirl
				#print "CHECK<$lines[$ln - 1]\n";
1913 e7d81004 Stefan Weil
				# we have a preceding printk if it ends
1914 1ec3f6f9 Blue Swirl
				# with "\n" ignore it, else it is to blame
1915 1ec3f6f9 Blue Swirl
				if ($lines[$ln - 1] =~ m{\bprintk\(}) {
1916 1ec3f6f9 Blue Swirl
					if ($rawlines[$ln - 1] !~ m{\\n"}) {
1917 1ec3f6f9 Blue Swirl
						$ok = 1;
1918 1ec3f6f9 Blue Swirl
					}
1919 1ec3f6f9 Blue Swirl
					last;
1920 1ec3f6f9 Blue Swirl
				}
1921 1ec3f6f9 Blue Swirl
			}
1922 1ec3f6f9 Blue Swirl
			if ($ok == 0) {
1923 1ec3f6f9 Blue Swirl
				WARN("printk() should include KERN_ facility level\n" . $herecurr);
1924 1ec3f6f9 Blue Swirl
			}
1925 1ec3f6f9 Blue Swirl
		}
1926 1ec3f6f9 Blue Swirl
1927 1ec3f6f9 Blue Swirl
# function brace can't be on same line, except for #defines of do while,
1928 1ec3f6f9 Blue Swirl
# or if closed on same line
1929 1ec3f6f9 Blue Swirl
		if (($line=~/$Type\s*$Ident\(.*\).*\s{/) and
1930 1ec3f6f9 Blue Swirl
		    !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) {
1931 1ec3f6f9 Blue Swirl
			ERROR("open brace '{' following function declarations go on the next line\n" . $herecurr);
1932 1ec3f6f9 Blue Swirl
		}
1933 1ec3f6f9 Blue Swirl
1934 1ec3f6f9 Blue Swirl
# open braces for enum, union and struct go on the same line.
1935 1ec3f6f9 Blue Swirl
		if ($line =~ /^.\s*{/ &&
1936 1ec3f6f9 Blue Swirl
		    $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
1937 1ec3f6f9 Blue Swirl
			ERROR("open brace '{' following $1 go on the same line\n" . $hereprev);
1938 1ec3f6f9 Blue Swirl
		}
1939 1ec3f6f9 Blue Swirl
1940 1ec3f6f9 Blue Swirl
# missing space after union, struct or enum definition
1941 1ec3f6f9 Blue Swirl
		if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?(?:\s+$Ident)?[=\{]/) {
1942 1ec3f6f9 Blue Swirl
		    WARN("missing space after $1 definition\n" . $herecurr);
1943 1ec3f6f9 Blue Swirl
		}
1944 1ec3f6f9 Blue Swirl
1945 1ec3f6f9 Blue Swirl
# check for spacing round square brackets; allowed:
1946 1ec3f6f9 Blue Swirl
#  1. with a type on the left -- int [] a;
1947 1ec3f6f9 Blue Swirl
#  2. at the beginning of a line for slice initialisers -- [0...10] = 5,
1948 1ec3f6f9 Blue Swirl
#  3. inside a curly brace -- = { [0...10] = 5 }
1949 1ec3f6f9 Blue Swirl
		while ($line =~ /(.*?\s)\[/g) {
1950 1ec3f6f9 Blue Swirl
			my ($where, $prefix) = ($-[1], $1);
1951 1ec3f6f9 Blue Swirl
			if ($prefix !~ /$Type\s+$/ &&
1952 1ec3f6f9 Blue Swirl
			    ($where != 0 || $prefix !~ /^.\s+$/) &&
1953 1ec3f6f9 Blue Swirl
			    $prefix !~ /{\s+$/) {
1954 1ec3f6f9 Blue Swirl
				ERROR("space prohibited before open square bracket '['\n" . $herecurr);
1955 1ec3f6f9 Blue Swirl
			}
1956 1ec3f6f9 Blue Swirl
		}
1957 1ec3f6f9 Blue Swirl
1958 1ec3f6f9 Blue Swirl
# check for spaces between functions and their parentheses.
1959 1ec3f6f9 Blue Swirl
		while ($line =~ /($Ident)\s+\(/g) {
1960 1ec3f6f9 Blue Swirl
			my $name = $1;
1961 1ec3f6f9 Blue Swirl
			my $ctx_before = substr($line, 0, $-[1]);
1962 1ec3f6f9 Blue Swirl
			my $ctx = "$ctx_before$name";
1963 1ec3f6f9 Blue Swirl
1964 1ec3f6f9 Blue Swirl
			# Ignore those directives where spaces _are_ permitted.
1965 1ec3f6f9 Blue Swirl
			if ($name =~ /^(?:
1966 1ec3f6f9 Blue Swirl
				if|for|while|switch|return|case|
1967 1ec3f6f9 Blue Swirl
				volatile|__volatile__|
1968 1ec3f6f9 Blue Swirl
				__attribute__|format|__extension__|
1969 1ec3f6f9 Blue Swirl
				asm|__asm__)$/x)
1970 1ec3f6f9 Blue Swirl
			{
1971 1ec3f6f9 Blue Swirl
1972 1ec3f6f9 Blue Swirl
			# cpp #define statements have non-optional spaces, ie
1973 1ec3f6f9 Blue Swirl
			# if there is a space between the name and the open
1974 1ec3f6f9 Blue Swirl
			# parenthesis it is simply not a parameter group.
1975 1ec3f6f9 Blue Swirl
			} elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
1976 1ec3f6f9 Blue Swirl
1977 1ec3f6f9 Blue Swirl
			# cpp #elif statement condition may start with a (
1978 1ec3f6f9 Blue Swirl
			} elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
1979 1ec3f6f9 Blue Swirl
1980 1ec3f6f9 Blue Swirl
			# If this whole things ends with a type its most
1981 1ec3f6f9 Blue Swirl
			# likely a typedef for a function.
1982 1ec3f6f9 Blue Swirl
			} elsif ($ctx =~ /$Type$/) {
1983 1ec3f6f9 Blue Swirl
1984 1ec3f6f9 Blue Swirl
			} else {
1985 1ec3f6f9 Blue Swirl
				WARN("space prohibited between function name and open parenthesis '('\n" . $herecurr);
1986 1ec3f6f9 Blue Swirl
			}
1987 1ec3f6f9 Blue Swirl
		}
1988 1ec3f6f9 Blue Swirl
# Check operator spacing.
1989 1ec3f6f9 Blue Swirl
		if (!($line=~/\#\s*include/)) {
1990 1ec3f6f9 Blue Swirl
			my $ops = qr{
1991 1ec3f6f9 Blue Swirl
				<<=|>>=|<=|>=|==|!=|
1992 1ec3f6f9 Blue Swirl
				\+=|-=|\*=|\/=|%=|\^=|\|=|&=|
1993 1ec3f6f9 Blue Swirl
				=>|->|<<|>>|<|>|=|!|~|
1994 1ec3f6f9 Blue Swirl
				&&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
1995 1ec3f6f9 Blue Swirl
				\?|:
1996 1ec3f6f9 Blue Swirl
			}x;
1997 1ec3f6f9 Blue Swirl
			my @elements = split(/($ops|;)/, $opline);
1998 1ec3f6f9 Blue Swirl
			my $off = 0;
1999 1ec3f6f9 Blue Swirl
2000 1ec3f6f9 Blue Swirl
			my $blank = copy_spacing($opline);
2001 1ec3f6f9 Blue Swirl
2002 1ec3f6f9 Blue Swirl
			for (my $n = 0; $n < $#elements; $n += 2) {
2003 1ec3f6f9 Blue Swirl
				$off += length($elements[$n]);
2004 1ec3f6f9 Blue Swirl
2005 e7d81004 Stefan Weil
				# Pick up the preceding and succeeding characters.
2006 1ec3f6f9 Blue Swirl
				my $ca = substr($opline, 0, $off);
2007 1ec3f6f9 Blue Swirl
				my $cc = '';
2008 1ec3f6f9 Blue Swirl
				if (length($opline) >= ($off + length($elements[$n + 1]))) {
2009 1ec3f6f9 Blue Swirl
					$cc = substr($opline, $off + length($elements[$n + 1]));
2010 1ec3f6f9 Blue Swirl
				}
2011 1ec3f6f9 Blue Swirl
				my $cb = "$ca$;$cc";
2012 1ec3f6f9 Blue Swirl
2013 1ec3f6f9 Blue Swirl
				my $a = '';
2014 1ec3f6f9 Blue Swirl
				$a = 'V' if ($elements[$n] ne '');
2015 1ec3f6f9 Blue Swirl
				$a = 'W' if ($elements[$n] =~ /\s$/);
2016 1ec3f6f9 Blue Swirl
				$a = 'C' if ($elements[$n] =~ /$;$/);
2017 1ec3f6f9 Blue Swirl
				$a = 'B' if ($elements[$n] =~ /(\[|\()$/);
2018 1ec3f6f9 Blue Swirl
				$a = 'O' if ($elements[$n] eq '');
2019 1ec3f6f9 Blue Swirl
				$a = 'E' if ($ca =~ /^\s*$/);
2020 1ec3f6f9 Blue Swirl
2021 1ec3f6f9 Blue Swirl
				my $op = $elements[$n + 1];
2022 1ec3f6f9 Blue Swirl
2023 1ec3f6f9 Blue Swirl
				my $c = '';
2024 1ec3f6f9 Blue Swirl
				if (defined $elements[$n + 2]) {
2025 1ec3f6f9 Blue Swirl
					$c = 'V' if ($elements[$n + 2] ne '');
2026 1ec3f6f9 Blue Swirl
					$c = 'W' if ($elements[$n + 2] =~ /^\s/);
2027 1ec3f6f9 Blue Swirl
					$c = 'C' if ($elements[$n + 2] =~ /^$;/);
2028 1ec3f6f9 Blue Swirl
					$c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
2029 1ec3f6f9 Blue Swirl
					$c = 'O' if ($elements[$n + 2] eq '');
2030 1ec3f6f9 Blue Swirl
					$c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
2031 1ec3f6f9 Blue Swirl
				} else {
2032 1ec3f6f9 Blue Swirl
					$c = 'E';
2033 1ec3f6f9 Blue Swirl
				}
2034 1ec3f6f9 Blue Swirl
2035 1ec3f6f9 Blue Swirl
				my $ctx = "${a}x${c}";
2036 1ec3f6f9 Blue Swirl
2037 1ec3f6f9 Blue Swirl
				my $at = "(ctx:$ctx)";
2038 1ec3f6f9 Blue Swirl
2039 1ec3f6f9 Blue Swirl
				my $ptr = substr($blank, 0, $off) . "^";
2040 1ec3f6f9 Blue Swirl
				my $hereptr = "$hereline$ptr\n";
2041 1ec3f6f9 Blue Swirl
2042 1ec3f6f9 Blue Swirl
				# Pull out the value of this operator.
2043 1ec3f6f9 Blue Swirl
				my $op_type = substr($curr_values, $off + 1, 1);
2044 1ec3f6f9 Blue Swirl
2045 1ec3f6f9 Blue Swirl
				# Get the full operator variant.
2046 1ec3f6f9 Blue Swirl
				my $opv = $op . substr($curr_vars, $off, 1);
2047 1ec3f6f9 Blue Swirl
2048 1ec3f6f9 Blue Swirl
				# Ignore operators passed as parameters.
2049 1ec3f6f9 Blue Swirl
				if ($op_type ne 'V' &&
2050 1ec3f6f9 Blue Swirl
				    $ca =~ /\s$/ && $cc =~ /^\s*,/) {
2051 1ec3f6f9 Blue Swirl
2052 1ec3f6f9 Blue Swirl
#				# Ignore comments
2053 1ec3f6f9 Blue Swirl
#				} elsif ($op =~ /^$;+$/) {
2054 1ec3f6f9 Blue Swirl
2055 1ec3f6f9 Blue Swirl
				# ; should have either the end of line or a space or \ after it
2056 1ec3f6f9 Blue Swirl
				} elsif ($op eq ';') {
2057 1ec3f6f9 Blue Swirl
					if ($ctx !~ /.x[WEBC]/ &&
2058 1ec3f6f9 Blue Swirl
					    $cc !~ /^\\/ && $cc !~ /^;/) {
2059 1ec3f6f9 Blue Swirl
						ERROR("space required after that '$op' $at\n" . $hereptr);
2060 1ec3f6f9 Blue Swirl
					}
2061 1ec3f6f9 Blue Swirl
2062 1ec3f6f9 Blue Swirl
				# // is a comment
2063 1ec3f6f9 Blue Swirl
				} elsif ($op eq '//') {
2064 1ec3f6f9 Blue Swirl
2065 1ec3f6f9 Blue Swirl
				# No spaces for:
2066 1ec3f6f9 Blue Swirl
				#   ->
2067 1ec3f6f9 Blue Swirl
				#   :   when part of a bitfield
2068 1ec3f6f9 Blue Swirl
				} elsif ($op eq '->' || $opv eq ':B') {
2069 1ec3f6f9 Blue Swirl
					if ($ctx =~ /Wx.|.xW/) {
2070 1ec3f6f9 Blue Swirl
						ERROR("spaces prohibited around that '$op' $at\n" . $hereptr);
2071 1ec3f6f9 Blue Swirl
					}
2072 1ec3f6f9 Blue Swirl
2073 1ec3f6f9 Blue Swirl
				# , must have a space on the right.
2074 9fbe4784 Alexander Graf
                                # not required when having a single },{ on one line
2075 1ec3f6f9 Blue Swirl
				} elsif ($op eq ',') {
2076 9fbe4784 Alexander Graf
					if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/ &&
2077 9fbe4784 Alexander Graf
                                            ($elements[$n] . $elements[$n + 2]) !~ " *}{") {
2078 1ec3f6f9 Blue Swirl
						ERROR("space required after that '$op' $at\n" . $hereptr);
2079 1ec3f6f9 Blue Swirl
					}
2080 1ec3f6f9 Blue Swirl
2081 1ec3f6f9 Blue Swirl
				# '*' as part of a type definition -- reported already.
2082 1ec3f6f9 Blue Swirl
				} elsif ($opv eq '*_') {
2083 1ec3f6f9 Blue Swirl
					#warn "'*' is part of type\n";
2084 1ec3f6f9 Blue Swirl
2085 1ec3f6f9 Blue Swirl
				# unary operators should have a space before and
2086 1ec3f6f9 Blue Swirl
				# none after.  May be left adjacent to another
2087 1ec3f6f9 Blue Swirl
				# unary operator, or a cast
2088 1ec3f6f9 Blue Swirl
				} elsif ($op eq '!' || $op eq '~' ||
2089 1ec3f6f9 Blue Swirl
					 $opv eq '*U' || $opv eq '-U' ||
2090 1ec3f6f9 Blue Swirl
					 $opv eq '&U' || $opv eq '&&U') {
2091 1ec3f6f9 Blue Swirl
					if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
2092 1ec3f6f9 Blue Swirl
						ERROR("space required before that '$op' $at\n" . $hereptr);
2093 1ec3f6f9 Blue Swirl
					}
2094 1ec3f6f9 Blue Swirl
					if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
2095 1ec3f6f9 Blue Swirl
						# A unary '*' may be const
2096 1ec3f6f9 Blue Swirl
2097 1ec3f6f9 Blue Swirl
					} elsif ($ctx =~ /.xW/) {
2098 1ec3f6f9 Blue Swirl
						ERROR("space prohibited after that '$op' $at\n" . $hereptr);
2099 1ec3f6f9 Blue Swirl
					}
2100 1ec3f6f9 Blue Swirl
2101 1ec3f6f9 Blue Swirl
				# unary ++ and unary -- are allowed no space on one side.
2102 1ec3f6f9 Blue Swirl
				} elsif ($op eq '++' or $op eq '--') {
2103 1ec3f6f9 Blue Swirl
					if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
2104 1ec3f6f9 Blue Swirl
						ERROR("space required one side of that '$op' $at\n" . $hereptr);
2105 1ec3f6f9 Blue Swirl
					}
2106 1ec3f6f9 Blue Swirl
					if ($ctx =~ /Wx[BE]/ ||
2107 1ec3f6f9 Blue Swirl
					    ($ctx =~ /Wx./ && $cc =~ /^;/)) {
2108 1ec3f6f9 Blue Swirl
						ERROR("space prohibited before that '$op' $at\n" . $hereptr);
2109 1ec3f6f9 Blue Swirl
					}
2110 1ec3f6f9 Blue Swirl
					if ($ctx =~ /ExW/) {
2111 1ec3f6f9 Blue Swirl
						ERROR("space prohibited after that '$op' $at\n" . $hereptr);
2112 1ec3f6f9 Blue Swirl
					}
2113 1ec3f6f9 Blue Swirl
2114 1ec3f6f9 Blue Swirl
2115 1ec3f6f9 Blue Swirl
				# << and >> may either have or not have spaces both sides
2116 1ec3f6f9 Blue Swirl
				} elsif ($op eq '<<' or $op eq '>>' or
2117 1ec3f6f9 Blue Swirl
					 $op eq '&' or $op eq '^' or $op eq '|' or
2118 1ec3f6f9 Blue Swirl
					 $op eq '+' or $op eq '-' or
2119 1ec3f6f9 Blue Swirl
					 $op eq '*' or $op eq '/' or
2120 1ec3f6f9 Blue Swirl
					 $op eq '%')
2121 1ec3f6f9 Blue Swirl
				{
2122 1ec3f6f9 Blue Swirl
					if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
2123 1ec3f6f9 Blue Swirl
						ERROR("need consistent spacing around '$op' $at\n" .
2124 1ec3f6f9 Blue Swirl
							$hereptr);
2125 1ec3f6f9 Blue Swirl
					}
2126 1ec3f6f9 Blue Swirl
2127 1ec3f6f9 Blue Swirl
				# A colon needs no spaces before when it is
2128 1ec3f6f9 Blue Swirl
				# terminating a case value or a label.
2129 1ec3f6f9 Blue Swirl
				} elsif ($opv eq ':C' || $opv eq ':L') {
2130 1ec3f6f9 Blue Swirl
					if ($ctx =~ /Wx./) {
2131 1ec3f6f9 Blue Swirl
						ERROR("space prohibited before that '$op' $at\n" . $hereptr);
2132 1ec3f6f9 Blue Swirl
					}
2133 1ec3f6f9 Blue Swirl
2134 1ec3f6f9 Blue Swirl
				# All the others need spaces both sides.
2135 1ec3f6f9 Blue Swirl
				} elsif ($ctx !~ /[EWC]x[CWE]/) {
2136 1ec3f6f9 Blue Swirl
					my $ok = 0;
2137 1ec3f6f9 Blue Swirl
2138 1ec3f6f9 Blue Swirl
					# Ignore email addresses <foo@bar>
2139 1ec3f6f9 Blue Swirl
					if (($op eq '<' &&
2140 1ec3f6f9 Blue Swirl
					     $cc =~ /^\S+\@\S+>/) ||
2141 1ec3f6f9 Blue Swirl
					    ($op eq '>' &&
2142 1ec3f6f9 Blue Swirl
					     $ca =~ /<\S+\@\S+$/))
2143 1ec3f6f9 Blue Swirl
					{
2144 1ec3f6f9 Blue Swirl
						$ok = 1;
2145 1ec3f6f9 Blue Swirl
					}
2146 1ec3f6f9 Blue Swirl
2147 1ec3f6f9 Blue Swirl
					# Ignore ?:
2148 1ec3f6f9 Blue Swirl
					if (($opv eq ':O' && $ca =~ /\?$/) ||
2149 1ec3f6f9 Blue Swirl
					    ($op eq '?' && $cc =~ /^:/)) {
2150 1ec3f6f9 Blue Swirl
						$ok = 1;
2151 1ec3f6f9 Blue Swirl
					}
2152 1ec3f6f9 Blue Swirl
2153 1ec3f6f9 Blue Swirl
					if ($ok == 0) {
2154 1ec3f6f9 Blue Swirl
						ERROR("spaces required around that '$op' $at\n" . $hereptr);
2155 1ec3f6f9 Blue Swirl
					}
2156 1ec3f6f9 Blue Swirl
				}
2157 1ec3f6f9 Blue Swirl
				$off += length($elements[$n + 1]);
2158 1ec3f6f9 Blue Swirl
			}
2159 1ec3f6f9 Blue Swirl
		}
2160 1ec3f6f9 Blue Swirl
2161 1ec3f6f9 Blue Swirl
# check for multiple assignments
2162 1ec3f6f9 Blue Swirl
		if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
2163 1ec3f6f9 Blue Swirl
			CHK("multiple assignments should be avoided\n" . $herecurr);
2164 1ec3f6f9 Blue Swirl
		}
2165 1ec3f6f9 Blue Swirl
2166 1ec3f6f9 Blue Swirl
## # check for multiple declarations, allowing for a function declaration
2167 1ec3f6f9 Blue Swirl
## # continuation.
2168 1ec3f6f9 Blue Swirl
## 		if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
2169 1ec3f6f9 Blue Swirl
## 		    $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
2170 1ec3f6f9 Blue Swirl
##
2171 1ec3f6f9 Blue Swirl
## 			# Remove any bracketed sections to ensure we do not
2172 1ec3f6f9 Blue Swirl
## 			# falsly report the parameters of functions.
2173 1ec3f6f9 Blue Swirl
## 			my $ln = $line;
2174 1ec3f6f9 Blue Swirl
## 			while ($ln =~ s/\([^\(\)]*\)//g) {
2175 1ec3f6f9 Blue Swirl
## 			}
2176 1ec3f6f9 Blue Swirl
## 			if ($ln =~ /,/) {
2177 1ec3f6f9 Blue Swirl
## 				WARN("declaring multiple variables together should be avoided\n" . $herecurr);
2178 1ec3f6f9 Blue Swirl
## 			}
2179 1ec3f6f9 Blue Swirl
## 		}
2180 1ec3f6f9 Blue Swirl
2181 1ec3f6f9 Blue Swirl
#need space before brace following if, while, etc
2182 1ec3f6f9 Blue Swirl
		if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
2183 1ec3f6f9 Blue Swirl
		    $line =~ /do{/) {
2184 1ec3f6f9 Blue Swirl
			ERROR("space required before the open brace '{'\n" . $herecurr);
2185 1ec3f6f9 Blue Swirl
		}
2186 1ec3f6f9 Blue Swirl
2187 1ec3f6f9 Blue Swirl
# closing brace should have a space following it when it has anything
2188 1ec3f6f9 Blue Swirl
# on the line
2189 1ec3f6f9 Blue Swirl
		if ($line =~ /}(?!(?:,|;|\)))\S/) {
2190 1ec3f6f9 Blue Swirl
			ERROR("space required after that close brace '}'\n" . $herecurr);
2191 1ec3f6f9 Blue Swirl
		}
2192 1ec3f6f9 Blue Swirl
2193 1ec3f6f9 Blue Swirl
# check spacing on square brackets
2194 1ec3f6f9 Blue Swirl
		if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
2195 1ec3f6f9 Blue Swirl
			ERROR("space prohibited after that open square bracket '['\n" . $herecurr);
2196 1ec3f6f9 Blue Swirl
		}
2197 1ec3f6f9 Blue Swirl
		if ($line =~ /\s\]/) {
2198 1ec3f6f9 Blue Swirl
			ERROR("space prohibited before that close square bracket ']'\n" . $herecurr);
2199 1ec3f6f9 Blue Swirl
		}
2200 1ec3f6f9 Blue Swirl
2201 1ec3f6f9 Blue Swirl
# check spacing on parentheses
2202 1ec3f6f9 Blue Swirl
		if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
2203 1ec3f6f9 Blue Swirl
		    $line !~ /for\s*\(\s+;/) {
2204 1ec3f6f9 Blue Swirl
			ERROR("space prohibited after that open parenthesis '('\n" . $herecurr);
2205 1ec3f6f9 Blue Swirl
		}
2206 1ec3f6f9 Blue Swirl
		if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
2207 1ec3f6f9 Blue Swirl
		    $line !~ /for\s*\(.*;\s+\)/ &&
2208 1ec3f6f9 Blue Swirl
		    $line !~ /:\s+\)/) {
2209 1ec3f6f9 Blue Swirl
			ERROR("space prohibited before that close parenthesis ')'\n" . $herecurr);
2210 1ec3f6f9 Blue Swirl
		}
2211 1ec3f6f9 Blue Swirl
2212 1ec3f6f9 Blue Swirl
# Return is not a function.
2213 1ec3f6f9 Blue Swirl
		if (defined($stat) && $stat =~ /^.\s*return(\s*)(\(.*);/s) {
2214 1ec3f6f9 Blue Swirl
			my $spacing = $1;
2215 1ec3f6f9 Blue Swirl
			my $value = $2;
2216 1ec3f6f9 Blue Swirl
2217 1ec3f6f9 Blue Swirl
			# Flatten any parentheses
2218 1ec3f6f9 Blue Swirl
			$value =~ s/\(/ \(/g;
2219 1ec3f6f9 Blue Swirl
			$value =~ s/\)/\) /g;
2220 1ec3f6f9 Blue Swirl
			while ($value =~ s/\[[^\{\}]*\]/1/ ||
2221 1ec3f6f9 Blue Swirl
			       $value !~ /(?:$Ident|-?$Constant)\s*
2222 1ec3f6f9 Blue Swirl
					     $Compare\s*
2223 1ec3f6f9 Blue Swirl
					     (?:$Ident|-?$Constant)/x &&
2224 1ec3f6f9 Blue Swirl
			       $value =~ s/\([^\(\)]*\)/1/) {
2225 1ec3f6f9 Blue Swirl
			}
2226 1ec3f6f9 Blue Swirl
#print "value<$value>\n";
2227 1ec3f6f9 Blue Swirl
			if ($value =~ /^\s*(?:$Ident|-?$Constant)\s*$/) {
2228 1ec3f6f9 Blue Swirl
				ERROR("return is not a function, parentheses are not required\n" . $herecurr);
2229 1ec3f6f9 Blue Swirl
2230 1ec3f6f9 Blue Swirl
			} elsif ($spacing !~ /\s+/) {
2231 1ec3f6f9 Blue Swirl
				ERROR("space required before the open parenthesis '('\n" . $herecurr);
2232 1ec3f6f9 Blue Swirl
			}
2233 1ec3f6f9 Blue Swirl
		}
2234 1ec3f6f9 Blue Swirl
# Return of what appears to be an errno should normally be -'ve
2235 1ec3f6f9 Blue Swirl
		if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) {
2236 1ec3f6f9 Blue Swirl
			my $name = $1;
2237 1ec3f6f9 Blue Swirl
			if ($name ne 'EOF' && $name ne 'ERROR') {
2238 1ec3f6f9 Blue Swirl
				CHK("return of an errno should typically be -ve (return -$1)\n" . $herecurr);
2239 1ec3f6f9 Blue Swirl
			}
2240 1ec3f6f9 Blue Swirl
		}
2241 1ec3f6f9 Blue Swirl
2242 1ec3f6f9 Blue Swirl
# Need a space before open parenthesis after if, while etc
2243 1ec3f6f9 Blue Swirl
		if ($line=~/\b(if|while|for|switch)\(/) {
2244 1ec3f6f9 Blue Swirl
			ERROR("space required before the open parenthesis '('\n" . $herecurr);
2245 1ec3f6f9 Blue Swirl
		}
2246 1ec3f6f9 Blue Swirl
2247 1ec3f6f9 Blue Swirl
# Check for illegal assignment in if conditional -- and check for trailing
2248 1ec3f6f9 Blue Swirl
# statements after the conditional.
2249 1ec3f6f9 Blue Swirl
		if ($line =~ /do\s*(?!{)/) {
2250 1ec3f6f9 Blue Swirl
			my ($stat_next) = ctx_statement_block($line_nr_next,
2251 1ec3f6f9 Blue Swirl
						$remain_next, $off_next);
2252 1ec3f6f9 Blue Swirl
			$stat_next =~ s/\n./\n /g;
2253 1ec3f6f9 Blue Swirl
			##print "stat<$stat> stat_next<$stat_next>\n";
2254 1ec3f6f9 Blue Swirl
2255 1ec3f6f9 Blue Swirl
			if ($stat_next =~ /^\s*while\b/) {
2256 1ec3f6f9 Blue Swirl
				# If the statement carries leading newlines,
2257 1ec3f6f9 Blue Swirl
				# then count those as offsets.
2258 1ec3f6f9 Blue Swirl
				my ($whitespace) =
2259 1ec3f6f9 Blue Swirl
					($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
2260 1ec3f6f9 Blue Swirl
				my $offset =
2261 1ec3f6f9 Blue Swirl
					statement_rawlines($whitespace) - 1;
2262 1ec3f6f9 Blue Swirl
2263 1ec3f6f9 Blue Swirl
				$suppress_whiletrailers{$line_nr_next +
2264 1ec3f6f9 Blue Swirl
								$offset} = 1;
2265 1ec3f6f9 Blue Swirl
			}
2266 1ec3f6f9 Blue Swirl
		}
2267 1ec3f6f9 Blue Swirl
		if (!defined $suppress_whiletrailers{$linenr} &&
2268 1ec3f6f9 Blue Swirl
		    $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
2269 1ec3f6f9 Blue Swirl
			my ($s, $c) = ($stat, $cond);
2270 1ec3f6f9 Blue Swirl
2271 1ec3f6f9 Blue Swirl
			if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
2272 1ec3f6f9 Blue Swirl
				ERROR("do not use assignment in if condition\n" . $herecurr);
2273 1ec3f6f9 Blue Swirl
			}
2274 1ec3f6f9 Blue Swirl
2275 1ec3f6f9 Blue Swirl
			# Find out what is on the end of the line after the
2276 1ec3f6f9 Blue Swirl
			# conditional.
2277 1ec3f6f9 Blue Swirl
			substr($s, 0, length($c), '');
2278 1ec3f6f9 Blue Swirl
			$s =~ s/\n.*//g;
2279 1ec3f6f9 Blue Swirl
			$s =~ s/$;//g; 	# Remove any comments
2280 1ec3f6f9 Blue Swirl
			if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
2281 1ec3f6f9 Blue Swirl
			    $c !~ /}\s*while\s*/)
2282 1ec3f6f9 Blue Swirl
			{
2283 1ec3f6f9 Blue Swirl
				# Find out how long the conditional actually is.
2284 1ec3f6f9 Blue Swirl
				my @newlines = ($c =~ /\n/gs);
2285 1ec3f6f9 Blue Swirl
				my $cond_lines = 1 + $#newlines;
2286 1ec3f6f9 Blue Swirl
				my $stat_real = '';
2287 1ec3f6f9 Blue Swirl
2288 1ec3f6f9 Blue Swirl
				$stat_real = raw_line($linenr, $cond_lines)
2289 1ec3f6f9 Blue Swirl
							. "\n" if ($cond_lines);
2290 1ec3f6f9 Blue Swirl
				if (defined($stat_real) && $cond_lines > 1) {
2291 1ec3f6f9 Blue Swirl
					$stat_real = "[...]\n$stat_real";
2292 1ec3f6f9 Blue Swirl
				}
2293 1ec3f6f9 Blue Swirl
2294 1ec3f6f9 Blue Swirl
				ERROR("trailing statements should be on next line\n" . $herecurr . $stat_real);
2295 1ec3f6f9 Blue Swirl
			}
2296 1ec3f6f9 Blue Swirl
		}
2297 1ec3f6f9 Blue Swirl
2298 1ec3f6f9 Blue Swirl
# Check for bitwise tests written as boolean
2299 1ec3f6f9 Blue Swirl
		if ($line =~ /
2300 1ec3f6f9 Blue Swirl
			(?:
2301 1ec3f6f9 Blue Swirl
				(?:\[|\(|\&\&|\|\|)
2302 1ec3f6f9 Blue Swirl
				\s*0[xX][0-9]+\s*
2303 1ec3f6f9 Blue Swirl
				(?:\&\&|\|\|)
2304 1ec3f6f9 Blue Swirl
			|
2305 1ec3f6f9 Blue Swirl
				(?:\&\&|\|\|)
2306 1ec3f6f9 Blue Swirl
				\s*0[xX][0-9]+\s*
2307 1ec3f6f9 Blue Swirl
				(?:\&\&|\|\||\)|\])
2308 1ec3f6f9 Blue Swirl
			)/x)
2309 1ec3f6f9 Blue Swirl
		{
2310 1ec3f6f9 Blue Swirl
			WARN("boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
2311 1ec3f6f9 Blue Swirl
		}
2312 1ec3f6f9 Blue Swirl
2313 1ec3f6f9 Blue Swirl
# if and else should not have general statements after it
2314 1ec3f6f9 Blue Swirl
		if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
2315 1ec3f6f9 Blue Swirl
			my $s = $1;
2316 1ec3f6f9 Blue Swirl
			$s =~ s/$;//g; 	# Remove any comments
2317 1ec3f6f9 Blue Swirl
			if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
2318 1ec3f6f9 Blue Swirl
				ERROR("trailing statements should be on next line\n" . $herecurr);
2319 1ec3f6f9 Blue Swirl
			}
2320 1ec3f6f9 Blue Swirl
		}
2321 1ec3f6f9 Blue Swirl
# if should not continue a brace
2322 1ec3f6f9 Blue Swirl
		if ($line =~ /}\s*if\b/) {
2323 1ec3f6f9 Blue Swirl
			ERROR("trailing statements should be on next line\n" .
2324 1ec3f6f9 Blue Swirl
				$herecurr);
2325 1ec3f6f9 Blue Swirl
		}
2326 1ec3f6f9 Blue Swirl
# case and default should not have general statements after them
2327 1ec3f6f9 Blue Swirl
		if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
2328 1ec3f6f9 Blue Swirl
		    $line !~ /\G(?:
2329 1ec3f6f9 Blue Swirl
			(?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
2330 1ec3f6f9 Blue Swirl
			\s*return\s+
2331 1ec3f6f9 Blue Swirl
		    )/xg)
2332 1ec3f6f9 Blue Swirl
		{
2333 1ec3f6f9 Blue Swirl
			ERROR("trailing statements should be on next line\n" . $herecurr);
2334 1ec3f6f9 Blue Swirl
		}
2335 1ec3f6f9 Blue Swirl
2336 1ec3f6f9 Blue Swirl
		# Check for }<nl>else {, these must be at the same
2337 1ec3f6f9 Blue Swirl
		# indent level to be relevant to each other.
2338 1ec3f6f9 Blue Swirl
		if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
2339 1ec3f6f9 Blue Swirl
						$previndent == $indent) {
2340 1ec3f6f9 Blue Swirl
			ERROR("else should follow close brace '}'\n" . $hereprev);
2341 1ec3f6f9 Blue Swirl
		}
2342 1ec3f6f9 Blue Swirl
2343 1ec3f6f9 Blue Swirl
		if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and
2344 1ec3f6f9 Blue Swirl
						$previndent == $indent) {
2345 1ec3f6f9 Blue Swirl
			my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
2346 1ec3f6f9 Blue Swirl
2347 1ec3f6f9 Blue Swirl
			# Find out what is on the end of the line after the
2348 1ec3f6f9 Blue Swirl
			# conditional.
2349 1ec3f6f9 Blue Swirl
			substr($s, 0, length($c), '');
2350 1ec3f6f9 Blue Swirl
			$s =~ s/\n.*//g;
2351 1ec3f6f9 Blue Swirl
2352 1ec3f6f9 Blue Swirl
			if ($s =~ /^\s*;/) {
2353 1ec3f6f9 Blue Swirl
				ERROR("while should follow close brace '}'\n" . $hereprev);
2354 1ec3f6f9 Blue Swirl
			}
2355 1ec3f6f9 Blue Swirl
		}
2356 1ec3f6f9 Blue Swirl
2357 1ec3f6f9 Blue Swirl
#studly caps, commented out until figure out how to distinguish between use of existing and adding new
2358 1ec3f6f9 Blue Swirl
#		if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) {
2359 1ec3f6f9 Blue Swirl
#		    print "No studly caps, use _\n";
2360 1ec3f6f9 Blue Swirl
#		    print "$herecurr";
2361 1ec3f6f9 Blue Swirl
#		    $clean = 0;
2362 1ec3f6f9 Blue Swirl
#		}
2363 1ec3f6f9 Blue Swirl
2364 1ec3f6f9 Blue Swirl
#no spaces allowed after \ in define
2365 1ec3f6f9 Blue Swirl
		if ($line=~/\#\s*define.*\\\s$/) {
2366 1ec3f6f9 Blue Swirl
			WARN("Whitepspace after \\ makes next lines useless\n" . $herecurr);
2367 1ec3f6f9 Blue Swirl
		}
2368 1ec3f6f9 Blue Swirl
2369 1ec3f6f9 Blue Swirl
#warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
2370 1ec3f6f9 Blue Swirl
		if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
2371 1ec3f6f9 Blue Swirl
			my $file = "$1.h";
2372 1ec3f6f9 Blue Swirl
			my $checkfile = "include/linux/$file";
2373 1ec3f6f9 Blue Swirl
			if (-f "$root/$checkfile" &&
2374 1ec3f6f9 Blue Swirl
			    $realfile ne $checkfile &&
2375 1ec3f6f9 Blue Swirl
			    $1 !~ /$allowed_asm_includes/)
2376 1ec3f6f9 Blue Swirl
			{
2377 1ec3f6f9 Blue Swirl
				if ($realfile =~ m{^arch/}) {
2378 1ec3f6f9 Blue Swirl
					CHK("Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
2379 1ec3f6f9 Blue Swirl
				} else {
2380 1ec3f6f9 Blue Swirl
					WARN("Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
2381 1ec3f6f9 Blue Swirl
				}
2382 1ec3f6f9 Blue Swirl
			}
2383 1ec3f6f9 Blue Swirl
		}
2384 1ec3f6f9 Blue Swirl
2385 1ec3f6f9 Blue Swirl
# multi-statement macros should be enclosed in a do while loop, grab the
2386 1ec3f6f9 Blue Swirl
# first statement and ensure its the whole macro if its not enclosed
2387 1ec3f6f9 Blue Swirl
# in a known good container
2388 1ec3f6f9 Blue Swirl
		if ($realfile !~ m@/vmlinux.lds.h$@ &&
2389 1ec3f6f9 Blue Swirl
		    $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
2390 1ec3f6f9 Blue Swirl
			my $ln = $linenr;
2391 1ec3f6f9 Blue Swirl
			my $cnt = $realcnt;
2392 1ec3f6f9 Blue Swirl
			my ($off, $dstat, $dcond, $rest);
2393 1ec3f6f9 Blue Swirl
			my $ctx = '';
2394 1ec3f6f9 Blue Swirl
2395 1ec3f6f9 Blue Swirl
			my $args = defined($1);
2396 1ec3f6f9 Blue Swirl
2397 1ec3f6f9 Blue Swirl
			# Find the end of the macro and limit our statement
2398 1ec3f6f9 Blue Swirl
			# search to that.
2399 1ec3f6f9 Blue Swirl
			while ($cnt > 0 && defined $lines[$ln - 1] &&
2400 1ec3f6f9 Blue Swirl
				$lines[$ln - 1] =~ /^(?:-|..*\\$)/)
2401 1ec3f6f9 Blue Swirl
			{
2402 1ec3f6f9 Blue Swirl
				$ctx .= $rawlines[$ln - 1] . "\n";
2403 1ec3f6f9 Blue Swirl
				$cnt-- if ($lines[$ln - 1] !~ /^-/);
2404 1ec3f6f9 Blue Swirl
				$ln++;
2405 1ec3f6f9 Blue Swirl
			}
2406 1ec3f6f9 Blue Swirl
			$ctx .= $rawlines[$ln - 1];
2407 1ec3f6f9 Blue Swirl
2408 1ec3f6f9 Blue Swirl
			($dstat, $dcond, $ln, $cnt, $off) =
2409 1ec3f6f9 Blue Swirl
				ctx_statement_block($linenr, $ln - $linenr + 1, 0);
2410 1ec3f6f9 Blue Swirl
			#print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
2411 1ec3f6f9 Blue Swirl
			#print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
2412 1ec3f6f9 Blue Swirl
2413 1ec3f6f9 Blue Swirl
			# Extract the remainder of the define (if any) and
2414 1ec3f6f9 Blue Swirl
			# rip off surrounding spaces, and trailing \'s.
2415 1ec3f6f9 Blue Swirl
			$rest = '';
2416 1ec3f6f9 Blue Swirl
			while ($off != 0 || ($cnt > 0 && $rest =~ /\\\s*$/)) {
2417 1ec3f6f9 Blue Swirl
				#print "ADDING cnt<$cnt> $off <" . substr($lines[$ln - 1], $off) . "> rest<$rest>\n";
2418 1ec3f6f9 Blue Swirl
				if ($off != 0 || $lines[$ln - 1] !~ /^-/) {
2419 1ec3f6f9 Blue Swirl
					$rest .= substr($lines[$ln - 1], $off) . "\n";
2420 1ec3f6f9 Blue Swirl
					$cnt--;
2421 1ec3f6f9 Blue Swirl
				}
2422 1ec3f6f9 Blue Swirl
				$ln++;
2423 1ec3f6f9 Blue Swirl
				$off = 0;
2424 1ec3f6f9 Blue Swirl
			}
2425 1ec3f6f9 Blue Swirl
			$rest =~ s/\\\n.//g;
2426 1ec3f6f9 Blue Swirl
			$rest =~ s/^\s*//s;
2427 1ec3f6f9 Blue Swirl
			$rest =~ s/\s*$//s;
2428 1ec3f6f9 Blue Swirl
2429 1ec3f6f9 Blue Swirl
			# Clean up the original statement.
2430 1ec3f6f9 Blue Swirl
			if ($args) {
2431 1ec3f6f9 Blue Swirl
				substr($dstat, 0, length($dcond), '');
2432 1ec3f6f9 Blue Swirl
			} else {
2433 1ec3f6f9 Blue Swirl
				$dstat =~ s/^.\s*\#\s*define\s+$Ident\s*//;
2434 1ec3f6f9 Blue Swirl
			}
2435 1ec3f6f9 Blue Swirl
			$dstat =~ s/$;//g;
2436 1ec3f6f9 Blue Swirl
			$dstat =~ s/\\\n.//g;
2437 1ec3f6f9 Blue Swirl
			$dstat =~ s/^\s*//s;
2438 1ec3f6f9 Blue Swirl
			$dstat =~ s/\s*$//s;
2439 1ec3f6f9 Blue Swirl
2440 1ec3f6f9 Blue Swirl
			# Flatten any parentheses and braces
2441 1ec3f6f9 Blue Swirl
			while ($dstat =~ s/\([^\(\)]*\)/1/ ||
2442 1ec3f6f9 Blue Swirl
			       $dstat =~ s/\{[^\{\}]*\}/1/ ||
2443 1ec3f6f9 Blue Swirl
			       $dstat =~ s/\[[^\{\}]*\]/1/)
2444 1ec3f6f9 Blue Swirl
			{
2445 1ec3f6f9 Blue Swirl
			}
2446 1ec3f6f9 Blue Swirl
2447 1ec3f6f9 Blue Swirl
			my $exceptions = qr{
2448 1ec3f6f9 Blue Swirl
				$Declare|
2449 1ec3f6f9 Blue Swirl
				module_param_named|
2450 1ec3f6f9 Blue Swirl
				MODULE_PARAM_DESC|
2451 1ec3f6f9 Blue Swirl
				DECLARE_PER_CPU|
2452 1ec3f6f9 Blue Swirl
				DEFINE_PER_CPU|
2453 1ec3f6f9 Blue Swirl
				__typeof__\(|
2454 1ec3f6f9 Blue Swirl
				union|
2455 1ec3f6f9 Blue Swirl
				struct|
2456 1ec3f6f9 Blue Swirl
				\.$Ident\s*=\s*|
2457 1ec3f6f9 Blue Swirl
				^\"|\"$
2458 1ec3f6f9 Blue Swirl
			}x;
2459 1ec3f6f9 Blue Swirl
			#print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
2460 1ec3f6f9 Blue Swirl
			if ($rest ne '' && $rest ne ',') {
2461 1ec3f6f9 Blue Swirl
				if ($rest !~ /while\s*\(/ &&
2462 1ec3f6f9 Blue Swirl
				    $dstat !~ /$exceptions/)
2463 1ec3f6f9 Blue Swirl
				{
2464 1ec3f6f9 Blue Swirl
					ERROR("Macros with multiple statements should be enclosed in a do - while loop\n" . "$here\n$ctx\n");
2465 1ec3f6f9 Blue Swirl
				}
2466 1ec3f6f9 Blue Swirl
2467 1ec3f6f9 Blue Swirl
			} elsif ($ctx !~ /;/) {
2468 1ec3f6f9 Blue Swirl
				if ($dstat ne '' &&
2469 1ec3f6f9 Blue Swirl
				    $dstat !~ /^(?:$Ident|-?$Constant)$/ &&
2470 1ec3f6f9 Blue Swirl
				    $dstat !~ /$exceptions/ &&
2471 1ec3f6f9 Blue Swirl
				    $dstat !~ /^\.$Ident\s*=/ &&
2472 1ec3f6f9 Blue Swirl
				    $dstat =~ /$Operators/)
2473 1ec3f6f9 Blue Swirl
				{
2474 1ec3f6f9 Blue Swirl
					ERROR("Macros with complex values should be enclosed in parenthesis\n" . "$here\n$ctx\n");
2475 1ec3f6f9 Blue Swirl
				}
2476 1ec3f6f9 Blue Swirl
			}
2477 1ec3f6f9 Blue Swirl
		}
2478 1ec3f6f9 Blue Swirl
2479 1ec3f6f9 Blue Swirl
# make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
2480 1ec3f6f9 Blue Swirl
# all assignments may have only one of the following with an assignment:
2481 1ec3f6f9 Blue Swirl
#	.
2482 1ec3f6f9 Blue Swirl
#	ALIGN(...)
2483 1ec3f6f9 Blue Swirl
#	VMLINUX_SYMBOL(...)
2484 1ec3f6f9 Blue Swirl
		if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
2485 1ec3f6f9 Blue Swirl
			WARN("vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
2486 1ec3f6f9 Blue Swirl
		}
2487 1ec3f6f9 Blue Swirl
2488 b6469683 Blue Swirl
# check for missing bracing round if etc
2489 b6469683 Blue Swirl
		if ($line =~ /(^.*)\bif\b/ && $line !~ /\#\s*if/) {
2490 1ec3f6f9 Blue Swirl
			my ($level, $endln, @chunks) =
2491 1ec3f6f9 Blue Swirl
				ctx_statement_full($linenr, $realcnt, 1);
2492 69402a69 Don Slutz
                        if ($dbg_adv_apw) {
2493 69402a69 Don Slutz
                            print "APW: chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
2494 69402a69 Don Slutz
                            print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n"
2495 69402a69 Don Slutz
                                if $#chunks >= 1;
2496 69402a69 Don Slutz
                        }
2497 b6469683 Blue Swirl
			if ($#chunks >= 0 && $level == 0) {
2498 1ec3f6f9 Blue Swirl
				my $allowed = 0;
2499 1ec3f6f9 Blue Swirl
				my $seen = 0;
2500 1ec3f6f9 Blue Swirl
				my $herectx = $here . "\n";
2501 1ec3f6f9 Blue Swirl
				my $ln = $linenr - 1;
2502 1ec3f6f9 Blue Swirl
				for my $chunk (@chunks) {
2503 1ec3f6f9 Blue Swirl
					my ($cond, $block) = @{$chunk};
2504 1ec3f6f9 Blue Swirl
2505 1ec3f6f9 Blue Swirl
					# If the condition carries leading newlines, then count those as offsets.
2506 1ec3f6f9 Blue Swirl
					my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
2507 1ec3f6f9 Blue Swirl
					my $offset = statement_rawlines($whitespace) - 1;
2508 1ec3f6f9 Blue Swirl
2509 1ec3f6f9 Blue Swirl
					#print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
2510 1ec3f6f9 Blue Swirl
2511 1ec3f6f9 Blue Swirl
					# We have looked at and allowed this specific line.
2512 1ec3f6f9 Blue Swirl
					$suppress_ifbraces{$ln + $offset} = 1;
2513 1ec3f6f9 Blue Swirl
2514 1ec3f6f9 Blue Swirl
					$herectx .= "$rawlines[$ln + $offset]\n[...]\n";
2515 1ec3f6f9 Blue Swirl
					$ln += statement_rawlines($block) - 1;
2516 1ec3f6f9 Blue Swirl
2517 1ec3f6f9 Blue Swirl
					substr($block, 0, length($cond), '');
2518 1ec3f6f9 Blue Swirl
2519 1ec3f6f9 Blue Swirl
					$seen++ if ($block =~ /^\s*{/);
2520 1ec3f6f9 Blue Swirl
2521 69402a69 Don Slutz
                                        print "APW: cond<$cond> block<$block> allowed<$allowed>\n"
2522 69402a69 Don Slutz
                                            if $dbg_adv_apw;
2523 1ec3f6f9 Blue Swirl
					if (statement_lines($cond) > 1) {
2524 69402a69 Don Slutz
                                            print "APW: ALLOWED: cond<$cond>\n"
2525 69402a69 Don Slutz
                                                if $dbg_adv_apw;
2526 69402a69 Don Slutz
                                            $allowed = 1;
2527 1ec3f6f9 Blue Swirl
					}
2528 1ec3f6f9 Blue Swirl
					if ($block =~/\b(?:if|for|while)\b/) {
2529 69402a69 Don Slutz
                                            print "APW: ALLOWED: block<$block>\n"
2530 69402a69 Don Slutz
                                                if $dbg_adv_apw;
2531 69402a69 Don Slutz
                                            $allowed = 1;
2532 1ec3f6f9 Blue Swirl
					}
2533 1ec3f6f9 Blue Swirl
					if (statement_block_size($block) > 1) {
2534 69402a69 Don Slutz
                                            print "APW: ALLOWED: lines block<$block>\n"
2535 69402a69 Don Slutz
                                                if $dbg_adv_apw;
2536 69402a69 Don Slutz
                                            $allowed = 1;
2537 1ec3f6f9 Blue Swirl
					}
2538 1ec3f6f9 Blue Swirl
				}
2539 01c4330b Pavel Borzenkov
				if ($seen != ($#chunks + 1)) {
2540 b6469683 Blue Swirl
					WARN("braces {} are necessary for all arms of this statement\n" . $herectx);
2541 1ec3f6f9 Blue Swirl
				}
2542 1ec3f6f9 Blue Swirl
			}
2543 1ec3f6f9 Blue Swirl
		}
2544 1ec3f6f9 Blue Swirl
		if (!defined $suppress_ifbraces{$linenr - 1} &&
2545 789f88d0 Jan Kiszka
					$line =~ /\b(if|while|for|else)\b/ &&
2546 d0510af2 Blue Swirl
					$line !~ /\#\s*if/ &&
2547 789f88d0 Jan Kiszka
					$line !~ /\#\s*else/) {
2548 1ec3f6f9 Blue Swirl
			my $allowed = 0;
2549 1ec3f6f9 Blue Swirl
2550 dfe7053a Don Slutz
                        # Check the pre-context.
2551 dfe7053a Don Slutz
                        if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
2552 dfe7053a Don Slutz
                            my $pre = $1;
2553 dfe7053a Don Slutz
2554 dfe7053a Don Slutz
                            if ($line !~ /else/) {
2555 dfe7053a Don Slutz
                                print "APW: ALLOWED: pre<$pre> line<$line>\n"
2556 dfe7053a Don Slutz
                                    if $dbg_adv_apw;
2557 dfe7053a Don Slutz
                                $allowed = 1;
2558 dfe7053a Don Slutz
                            }
2559 dfe7053a Don Slutz
                        }
2560 1ec3f6f9 Blue Swirl
2561 1ec3f6f9 Blue Swirl
			my ($level, $endln, @chunks) =
2562 1ec3f6f9 Blue Swirl
				ctx_statement_full($linenr, $realcnt, $-[0]);
2563 1ec3f6f9 Blue Swirl
2564 1ec3f6f9 Blue Swirl
			# Check the condition.
2565 1ec3f6f9 Blue Swirl
			my ($cond, $block) = @{$chunks[0]};
2566 5424302e Don Slutz
                        print "CHECKING<$linenr> cond<$cond> block<$block>\n"
2567 5424302e Don Slutz
                            if $dbg_adv_checking;
2568 1ec3f6f9 Blue Swirl
			if (defined $cond) {
2569 1ec3f6f9 Blue Swirl
				substr($block, 0, length($cond), '');
2570 1ec3f6f9 Blue Swirl
			}
2571 1ec3f6f9 Blue Swirl
			if (statement_lines($cond) > 1) {
2572 69402a69 Don Slutz
                            print "APW: ALLOWED: cond<$cond>\n"
2573 69402a69 Don Slutz
                                if $dbg_adv_apw;
2574 69402a69 Don Slutz
                            $allowed = 1;
2575 1ec3f6f9 Blue Swirl
			}
2576 1ec3f6f9 Blue Swirl
			if ($block =~/\b(?:if|for|while)\b/) {
2577 69402a69 Don Slutz
                            print "APW: ALLOWED: block<$block>\n"
2578 69402a69 Don Slutz
                                if $dbg_adv_apw;
2579 69402a69 Don Slutz
                            $allowed = 1;
2580 1ec3f6f9 Blue Swirl
			}
2581 1ec3f6f9 Blue Swirl
			if (statement_block_size($block) > 1) {
2582 69402a69 Don Slutz
                            print "APW: ALLOWED: lines block<$block>\n"
2583 69402a69 Don Slutz
                                if $dbg_adv_apw;
2584 69402a69 Don Slutz
                            $allowed = 1;
2585 1ec3f6f9 Blue Swirl
			}
2586 1ec3f6f9 Blue Swirl
			# Check the post-context.
2587 1ec3f6f9 Blue Swirl
			if (defined $chunks[1]) {
2588 1ec3f6f9 Blue Swirl
				my ($cond, $block) = @{$chunks[1]};
2589 1ec3f6f9 Blue Swirl
				if (defined $cond) {
2590 1ec3f6f9 Blue Swirl
					substr($block, 0, length($cond), '');
2591 1ec3f6f9 Blue Swirl
				}
2592 1ec3f6f9 Blue Swirl
				if ($block =~ /^\s*\{/) {
2593 69402a69 Don Slutz
                                    print "APW: ALLOWED: chunk-1 block<$block>\n"
2594 69402a69 Don Slutz
                                        if $dbg_adv_apw;
2595 69402a69 Don Slutz
                                    $allowed = 1;
2596 1ec3f6f9 Blue Swirl
				}
2597 1ec3f6f9 Blue Swirl
			}
2598 a99ac041 Don Slutz
                        print "DCS: level=$level block<$block> allowed=$allowed\n"
2599 a99ac041 Don Slutz
                            if $dbg_adv_dcs;
2600 b6469683 Blue Swirl
			if ($level == 0 && $block !~ /^\s*\{/ && !$allowed) {
2601 1ec3f6f9 Blue Swirl
				my $herectx = $here . "\n";;
2602 1ec3f6f9 Blue Swirl
				my $cnt = statement_rawlines($block);
2603 1ec3f6f9 Blue Swirl
2604 1ec3f6f9 Blue Swirl
				for (my $n = 0; $n < $cnt; $n++) {
2605 1ec3f6f9 Blue Swirl
					$herectx .= raw_line($linenr, $n) . "\n";;
2606 1ec3f6f9 Blue Swirl
				}
2607 1ec3f6f9 Blue Swirl
2608 b6469683 Blue Swirl
				WARN("braces {} are necessary even for single statement blocks\n" . $herectx);
2609 1ec3f6f9 Blue Swirl
			}
2610 1ec3f6f9 Blue Swirl
		}
2611 1ec3f6f9 Blue Swirl
2612 1ec3f6f9 Blue Swirl
# don't include deprecated include files (uses RAW line)
2613 1ec3f6f9 Blue Swirl
		for my $inc (@dep_includes) {
2614 1ec3f6f9 Blue Swirl
			if ($rawline =~ m@^.\s*\#\s*include\s*\<$inc>@) {
2615 1ec3f6f9 Blue Swirl
				ERROR("Don't use <$inc>: see Documentation/feature-removal-schedule.txt\n" . $herecurr);
2616 1ec3f6f9 Blue Swirl
			}
2617 1ec3f6f9 Blue Swirl
		}
2618 1ec3f6f9 Blue Swirl
2619 1ec3f6f9 Blue Swirl
# don't use deprecated functions
2620 1ec3f6f9 Blue Swirl
		for my $func (@dep_functions) {
2621 1ec3f6f9 Blue Swirl
			if ($line =~ /\b$func\b/) {
2622 1ec3f6f9 Blue Swirl
				ERROR("Don't use $func(): see Documentation/feature-removal-schedule.txt\n" . $herecurr);
2623 1ec3f6f9 Blue Swirl
			}
2624 1ec3f6f9 Blue Swirl
		}
2625 1ec3f6f9 Blue Swirl
2626 1ec3f6f9 Blue Swirl
# no volatiles please
2627 1ec3f6f9 Blue Swirl
		my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
2628 1ec3f6f9 Blue Swirl
		if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
2629 1ec3f6f9 Blue Swirl
			WARN("Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
2630 1ec3f6f9 Blue Swirl
		}
2631 1ec3f6f9 Blue Swirl
2632 1ec3f6f9 Blue Swirl
# SPIN_LOCK_UNLOCKED & RW_LOCK_UNLOCKED are deprecated
2633 1ec3f6f9 Blue Swirl
		if ($line =~ /\b(SPIN_LOCK_UNLOCKED|RW_LOCK_UNLOCKED)/) {
2634 1ec3f6f9 Blue Swirl
			ERROR("Use of $1 is deprecated: see Documentation/spinlocks.txt\n" . $herecurr);
2635 1ec3f6f9 Blue Swirl
		}
2636 1ec3f6f9 Blue Swirl
2637 1ec3f6f9 Blue Swirl
# warn about #if 0
2638 1ec3f6f9 Blue Swirl
		if ($line =~ /^.\s*\#\s*if\s+0\b/) {
2639 1ec3f6f9 Blue Swirl
			CHK("if this code is redundant consider removing it\n" .
2640 1ec3f6f9 Blue Swirl
				$herecurr);
2641 1ec3f6f9 Blue Swirl
		}
2642 1ec3f6f9 Blue Swirl
2643 1ec3f6f9 Blue Swirl
# check for needless kfree() checks
2644 1ec3f6f9 Blue Swirl
		if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
2645 1ec3f6f9 Blue Swirl
			my $expr = $1;
2646 1ec3f6f9 Blue Swirl
			if ($line =~ /\bkfree\(\Q$expr\E\);/) {
2647 1ec3f6f9 Blue Swirl
				WARN("kfree(NULL) is safe this check is probably not required\n" . $hereprev);
2648 1ec3f6f9 Blue Swirl
			}
2649 1ec3f6f9 Blue Swirl
		}
2650 1ec3f6f9 Blue Swirl
# check for needless usb_free_urb() checks
2651 1ec3f6f9 Blue Swirl
		if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
2652 1ec3f6f9 Blue Swirl
			my $expr = $1;
2653 1ec3f6f9 Blue Swirl
			if ($line =~ /\busb_free_urb\(\Q$expr\E\);/) {
2654 1ec3f6f9 Blue Swirl
				WARN("usb_free_urb(NULL) is safe this check is probably not required\n" . $hereprev);
2655 1ec3f6f9 Blue Swirl
			}
2656 1ec3f6f9 Blue Swirl
		}
2657 1ec3f6f9 Blue Swirl
2658 1ec3f6f9 Blue Swirl
# prefer usleep_range over udelay
2659 1ec3f6f9 Blue Swirl
		if ($line =~ /\budelay\s*\(\s*(\w+)\s*\)/) {
2660 1ec3f6f9 Blue Swirl
			# ignore udelay's < 10, however
2661 1ec3f6f9 Blue Swirl
			if (! (($1 =~ /(\d+)/) && ($1 < 10)) ) {
2662 1ec3f6f9 Blue Swirl
				CHK("usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $line);
2663 1ec3f6f9 Blue Swirl
			}
2664 1ec3f6f9 Blue Swirl
		}
2665 1ec3f6f9 Blue Swirl
2666 1ec3f6f9 Blue Swirl
# warn about unexpectedly long msleep's
2667 1ec3f6f9 Blue Swirl
		if ($line =~ /\bmsleep\s*\((\d+)\);/) {
2668 1ec3f6f9 Blue Swirl
			if ($1 < 20) {
2669 1ec3f6f9 Blue Swirl
				WARN("msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $line);
2670 1ec3f6f9 Blue Swirl
			}
2671 1ec3f6f9 Blue Swirl
		}
2672 1ec3f6f9 Blue Swirl
2673 1ec3f6f9 Blue Swirl
# warn about #ifdefs in C files
2674 1ec3f6f9 Blue Swirl
#		if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
2675 1ec3f6f9 Blue Swirl
#			print "#ifdef in C files should be avoided\n";
2676 1ec3f6f9 Blue Swirl
#			print "$herecurr";
2677 1ec3f6f9 Blue Swirl
#			$clean = 0;
2678 1ec3f6f9 Blue Swirl
#		}
2679 1ec3f6f9 Blue Swirl
2680 1ec3f6f9 Blue Swirl
# warn about spacing in #ifdefs
2681 1ec3f6f9 Blue Swirl
		if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
2682 1ec3f6f9 Blue Swirl
			ERROR("exactly one space required after that #$1\n" . $herecurr);
2683 1ec3f6f9 Blue Swirl
		}
2684 1ec3f6f9 Blue Swirl
2685 1ec3f6f9 Blue Swirl
# check for spinlock_t definitions without a comment.
2686 1ec3f6f9 Blue Swirl
		if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
2687 1ec3f6f9 Blue Swirl
		    $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
2688 1ec3f6f9 Blue Swirl
			my $which = $1;
2689 1ec3f6f9 Blue Swirl
			if (!ctx_has_comment($first_line, $linenr)) {
2690 1ec3f6f9 Blue Swirl
				CHK("$1 definition without comment\n" . $herecurr);
2691 1ec3f6f9 Blue Swirl
			}
2692 1ec3f6f9 Blue Swirl
		}
2693 1ec3f6f9 Blue Swirl
# check for memory barriers without a comment.
2694 1ec3f6f9 Blue Swirl
		if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
2695 1ec3f6f9 Blue Swirl
			if (!ctx_has_comment($first_line, $linenr)) {
2696 1ec3f6f9 Blue Swirl
				CHK("memory barrier without comment\n" . $herecurr);
2697 1ec3f6f9 Blue Swirl
			}
2698 1ec3f6f9 Blue Swirl
		}
2699 1ec3f6f9 Blue Swirl
# check of hardware specific defines
2700 1ec3f6f9 Blue Swirl
		if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
2701 1ec3f6f9 Blue Swirl
			CHK("architecture specific defines should be avoided\n" .  $herecurr);
2702 1ec3f6f9 Blue Swirl
		}
2703 1ec3f6f9 Blue Swirl
2704 1ec3f6f9 Blue Swirl
# Check that the storage class is at the beginning of a declaration
2705 1ec3f6f9 Blue Swirl
		if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
2706 1ec3f6f9 Blue Swirl
			WARN("storage class should be at the beginning of the declaration\n" . $herecurr)
2707 1ec3f6f9 Blue Swirl
		}
2708 1ec3f6f9 Blue Swirl
2709 1ec3f6f9 Blue Swirl
# check the location of the inline attribute, that it is between
2710 1ec3f6f9 Blue Swirl
# storage class and type.
2711 1ec3f6f9 Blue Swirl
		if ($line =~ /\b$Type\s+$Inline\b/ ||
2712 1ec3f6f9 Blue Swirl
		    $line =~ /\b$Inline\s+$Storage\b/) {
2713 1ec3f6f9 Blue Swirl
			ERROR("inline keyword should sit between storage class and type\n" . $herecurr);
2714 1ec3f6f9 Blue Swirl
		}
2715 1ec3f6f9 Blue Swirl
2716 1ec3f6f9 Blue Swirl
# Check for __inline__ and __inline, prefer inline
2717 1ec3f6f9 Blue Swirl
		if ($line =~ /\b(__inline__|__inline)\b/) {
2718 1ec3f6f9 Blue Swirl
			WARN("plain inline is preferred over $1\n" . $herecurr);
2719 1ec3f6f9 Blue Swirl
		}
2720 1ec3f6f9 Blue Swirl
2721 1ec3f6f9 Blue Swirl
# check for sizeof(&)
2722 1ec3f6f9 Blue Swirl
		if ($line =~ /\bsizeof\s*\(\s*\&/) {
2723 1ec3f6f9 Blue Swirl
			WARN("sizeof(& should be avoided\n" . $herecurr);
2724 1ec3f6f9 Blue Swirl
		}
2725 1ec3f6f9 Blue Swirl
2726 1ec3f6f9 Blue Swirl
# check for new externs in .c files.
2727 1ec3f6f9 Blue Swirl
		if ($realfile =~ /\.c$/ && defined $stat &&
2728 1ec3f6f9 Blue Swirl
		    $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
2729 1ec3f6f9 Blue Swirl
		{
2730 1ec3f6f9 Blue Swirl
			my $function_name = $1;
2731 1ec3f6f9 Blue Swirl
			my $paren_space = $2;
2732 1ec3f6f9 Blue Swirl
2733 1ec3f6f9 Blue Swirl
			my $s = $stat;
2734 1ec3f6f9 Blue Swirl
			if (defined $cond) {
2735 1ec3f6f9 Blue Swirl
				substr($s, 0, length($cond), '');
2736 1ec3f6f9 Blue Swirl
			}
2737 1ec3f6f9 Blue Swirl
			if ($s =~ /^\s*;/ &&
2738 1ec3f6f9 Blue Swirl
			    $function_name ne 'uninitialized_var')
2739 1ec3f6f9 Blue Swirl
			{
2740 1ec3f6f9 Blue Swirl
				WARN("externs should be avoided in .c files\n" .  $herecurr);
2741 1ec3f6f9 Blue Swirl
			}
2742 1ec3f6f9 Blue Swirl
2743 1ec3f6f9 Blue Swirl
			if ($paren_space =~ /\n/) {
2744 1ec3f6f9 Blue Swirl
				WARN("arguments for function declarations should follow identifier\n" . $herecurr);
2745 1ec3f6f9 Blue Swirl
			}
2746 1ec3f6f9 Blue Swirl
2747 1ec3f6f9 Blue Swirl
		} elsif ($realfile =~ /\.c$/ && defined $stat &&
2748 1ec3f6f9 Blue Swirl
		    $stat =~ /^.\s*extern\s+/)
2749 1ec3f6f9 Blue Swirl
		{
2750 1ec3f6f9 Blue Swirl
			WARN("externs should be avoided in .c files\n" .  $herecurr);
2751 1ec3f6f9 Blue Swirl
		}
2752 1ec3f6f9 Blue Swirl
2753 1ec3f6f9 Blue Swirl
# checks for new __setup's
2754 1ec3f6f9 Blue Swirl
		if ($rawline =~ /\b__setup\("([^"]*)"/) {
2755 1ec3f6f9 Blue Swirl
			my $name = $1;
2756 1ec3f6f9 Blue Swirl
2757 1ec3f6f9 Blue Swirl
			if (!grep(/$name/, @setup_docs)) {
2758 1ec3f6f9 Blue Swirl
				CHK("__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
2759 1ec3f6f9 Blue Swirl
			}
2760 1ec3f6f9 Blue Swirl
		}
2761 1ec3f6f9 Blue Swirl
2762 1ec3f6f9 Blue Swirl
# check for pointless casting of kmalloc return
2763 1ec3f6f9 Blue Swirl
		if ($line =~ /\*\s*\)\s*k[czm]alloc\b/) {
2764 1ec3f6f9 Blue Swirl
			WARN("unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
2765 1ec3f6f9 Blue Swirl
		}
2766 1ec3f6f9 Blue Swirl
2767 1ec3f6f9 Blue Swirl
# check for gcc specific __FUNCTION__
2768 1ec3f6f9 Blue Swirl
		if ($line =~ /__FUNCTION__/) {
2769 1ec3f6f9 Blue Swirl
			WARN("__func__ should be used instead of gcc specific __FUNCTION__\n"  . $herecurr);
2770 1ec3f6f9 Blue Swirl
		}
2771 1ec3f6f9 Blue Swirl
2772 1ec3f6f9 Blue Swirl
# check for semaphores used as mutexes
2773 1ec3f6f9 Blue Swirl
		if ($line =~ /^.\s*(DECLARE_MUTEX|init_MUTEX)\s*\(/) {
2774 1ec3f6f9 Blue Swirl
			WARN("mutexes are preferred for single holder semaphores\n" . $herecurr);
2775 1ec3f6f9 Blue Swirl
		}
2776 1ec3f6f9 Blue Swirl
# check for semaphores used as mutexes
2777 1ec3f6f9 Blue Swirl
		if ($line =~ /^.\s*init_MUTEX_LOCKED\s*\(/) {
2778 1ec3f6f9 Blue Swirl
			WARN("consider using a completion\n" . $herecurr);
2779 1ec3f6f9 Blue Swirl
2780 1ec3f6f9 Blue Swirl
		}
2781 1ec3f6f9 Blue Swirl
# recommend strict_strto* over simple_strto*
2782 1ec3f6f9 Blue Swirl
		if ($line =~ /\bsimple_(strto.*?)\s*\(/) {
2783 1ec3f6f9 Blue Swirl
			WARN("consider using strict_$1 in preference to simple_$1\n" . $herecurr);
2784 1ec3f6f9 Blue Swirl
		}
2785 1ec3f6f9 Blue Swirl
# check for __initcall(), use device_initcall() explicitly please
2786 1ec3f6f9 Blue Swirl
		if ($line =~ /^.\s*__initcall\s*\(/) {
2787 1ec3f6f9 Blue Swirl
			WARN("please use device_initcall() instead of __initcall()\n" . $herecurr);
2788 1ec3f6f9 Blue Swirl
		}
2789 1ec3f6f9 Blue Swirl
# check for various ops structs, ensure they are const.
2790 1ec3f6f9 Blue Swirl
		my $struct_ops = qr{acpi_dock_ops|
2791 1ec3f6f9 Blue Swirl
				address_space_operations|
2792 1ec3f6f9 Blue Swirl
				backlight_ops|
2793 1ec3f6f9 Blue Swirl
				block_device_operations|
2794 1ec3f6f9 Blue Swirl
				dentry_operations|
2795 1ec3f6f9 Blue Swirl
				dev_pm_ops|
2796 1ec3f6f9 Blue Swirl
				dma_map_ops|
2797 1ec3f6f9 Blue Swirl
				extent_io_ops|
2798 1ec3f6f9 Blue Swirl
				file_lock_operations|
2799 1ec3f6f9 Blue Swirl
				file_operations|
2800 1ec3f6f9 Blue Swirl
				hv_ops|
2801 1ec3f6f9 Blue Swirl
				ide_dma_ops|
2802 1ec3f6f9 Blue Swirl
				intel_dvo_dev_ops|
2803 1ec3f6f9 Blue Swirl
				item_operations|
2804 1ec3f6f9 Blue Swirl
				iwl_ops|
2805 1ec3f6f9 Blue Swirl
				kgdb_arch|
2806 1ec3f6f9 Blue Swirl
				kgdb_io|
2807 1ec3f6f9 Blue Swirl
				kset_uevent_ops|
2808 1ec3f6f9 Blue Swirl
				lock_manager_operations|
2809 1ec3f6f9 Blue Swirl
				microcode_ops|
2810 1ec3f6f9 Blue Swirl
				mtrr_ops|
2811 1ec3f6f9 Blue Swirl
				neigh_ops|
2812 1ec3f6f9 Blue Swirl
				nlmsvc_binding|
2813 1ec3f6f9 Blue Swirl
				pci_raw_ops|
2814 1ec3f6f9 Blue Swirl
				pipe_buf_operations|
2815 1ec3f6f9 Blue Swirl
				platform_hibernation_ops|
2816 1ec3f6f9 Blue Swirl
				platform_suspend_ops|
2817 1ec3f6f9 Blue Swirl
				proto_ops|
2818 1ec3f6f9 Blue Swirl
				rpc_pipe_ops|
2819 1ec3f6f9 Blue Swirl
				seq_operations|
2820 1ec3f6f9 Blue Swirl
				snd_ac97_build_ops|
2821 1ec3f6f9 Blue Swirl
				soc_pcmcia_socket_ops|
2822 1ec3f6f9 Blue Swirl
				stacktrace_ops|
2823 1ec3f6f9 Blue Swirl
				sysfs_ops|
2824 1ec3f6f9 Blue Swirl
				tty_operations|
2825 1ec3f6f9 Blue Swirl
				usb_mon_operations|
2826 1ec3f6f9 Blue Swirl
				wd_ops}x;
2827 1ec3f6f9 Blue Swirl
		if ($line !~ /\bconst\b/ &&
2828 1ec3f6f9 Blue Swirl
		    $line =~ /\bstruct\s+($struct_ops)\b/) {
2829 1ec3f6f9 Blue Swirl
			WARN("struct $1 should normally be const\n" .
2830 1ec3f6f9 Blue Swirl
				$herecurr);
2831 1ec3f6f9 Blue Swirl
		}
2832 1ec3f6f9 Blue Swirl
2833 1ec3f6f9 Blue Swirl
# use of NR_CPUS is usually wrong
2834 1ec3f6f9 Blue Swirl
# ignore definitions of NR_CPUS and usage to define arrays as likely right
2835 1ec3f6f9 Blue Swirl
		if ($line =~ /\bNR_CPUS\b/ &&
2836 1ec3f6f9 Blue Swirl
		    $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
2837 1ec3f6f9 Blue Swirl
		    $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
2838 1ec3f6f9 Blue Swirl
		    $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
2839 1ec3f6f9 Blue Swirl
		    $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
2840 1ec3f6f9 Blue Swirl
		    $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
2841 1ec3f6f9 Blue Swirl
		{
2842 1ec3f6f9 Blue Swirl
			WARN("usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
2843 1ec3f6f9 Blue Swirl
		}
2844 1ec3f6f9 Blue Swirl
2845 1ec3f6f9 Blue Swirl
# check for %L{u,d,i} in strings
2846 1ec3f6f9 Blue Swirl
		my $string;
2847 1ec3f6f9 Blue Swirl
		while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
2848 1ec3f6f9 Blue Swirl
			$string = substr($rawline, $-[1], $+[1] - $-[1]);
2849 1ec3f6f9 Blue Swirl
			$string =~ s/%%/__/g;
2850 1ec3f6f9 Blue Swirl
			if ($string =~ /(?<!%)%L[udi]/) {
2851 1ec3f6f9 Blue Swirl
				WARN("\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
2852 1ec3f6f9 Blue Swirl
				last;
2853 1ec3f6f9 Blue Swirl
			}
2854 1ec3f6f9 Blue Swirl
		}
2855 1ec3f6f9 Blue Swirl
2856 1ec3f6f9 Blue Swirl
# whine mightly about in_atomic
2857 1ec3f6f9 Blue Swirl
		if ($line =~ /\bin_atomic\s*\(/) {
2858 1ec3f6f9 Blue Swirl
			if ($realfile =~ m@^drivers/@) {
2859 1ec3f6f9 Blue Swirl
				ERROR("do not use in_atomic in drivers\n" . $herecurr);
2860 1ec3f6f9 Blue Swirl
			} elsif ($realfile !~ m@^kernel/@) {
2861 1ec3f6f9 Blue Swirl
				WARN("use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
2862 1ec3f6f9 Blue Swirl
			}
2863 1ec3f6f9 Blue Swirl
		}
2864 1ec3f6f9 Blue Swirl
2865 1ec3f6f9 Blue Swirl
# check for lockdep_set_novalidate_class
2866 1ec3f6f9 Blue Swirl
		if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
2867 1ec3f6f9 Blue Swirl
		    $line =~ /__lockdep_no_validate__\s*\)/ ) {
2868 1ec3f6f9 Blue Swirl
			if ($realfile !~ m@^kernel/lockdep@ &&
2869 1ec3f6f9 Blue Swirl
			    $realfile !~ m@^include/linux/lockdep@ &&
2870 1ec3f6f9 Blue Swirl
			    $realfile !~ m@^drivers/base/core@) {
2871 1ec3f6f9 Blue Swirl
				ERROR("lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
2872 1ec3f6f9 Blue Swirl
			}
2873 1ec3f6f9 Blue Swirl
		}
2874 9964d8f9 Stefan Weil
2875 9964d8f9 Stefan Weil
# QEMU specific tests
2876 9964d8f9 Stefan Weil
		if ($rawline =~ /\b(?:Qemu|QEmu)\b/) {
2877 9964d8f9 Stefan Weil
			WARN("use QEMU instead of Qemu or QEmu\n" . $herecurr);
2878 9964d8f9 Stefan Weil
		}
2879 1ec3f6f9 Blue Swirl
	}
2880 1ec3f6f9 Blue Swirl
2881 1ec3f6f9 Blue Swirl
	# If we have no input at all, then there is nothing to report on
2882 1ec3f6f9 Blue Swirl
	# so just keep quiet.
2883 1ec3f6f9 Blue Swirl
	if ($#rawlines == -1) {
2884 1ec3f6f9 Blue Swirl
		exit(0);
2885 1ec3f6f9 Blue Swirl
	}
2886 1ec3f6f9 Blue Swirl
2887 1ec3f6f9 Blue Swirl
	# In mailback mode only produce a report in the negative, for
2888 1ec3f6f9 Blue Swirl
	# things that appear to be patches.
2889 1ec3f6f9 Blue Swirl
	if ($mailback && ($clean == 1 || !$is_patch)) {
2890 1ec3f6f9 Blue Swirl
		exit(0);
2891 1ec3f6f9 Blue Swirl
	}
2892 1ec3f6f9 Blue Swirl
2893 1ec3f6f9 Blue Swirl
	# This is not a patch, and we are are in 'no-patch' mode so
2894 1ec3f6f9 Blue Swirl
	# just keep quiet.
2895 1ec3f6f9 Blue Swirl
	if (!$chk_patch && !$is_patch) {
2896 1ec3f6f9 Blue Swirl
		exit(0);
2897 1ec3f6f9 Blue Swirl
	}
2898 1ec3f6f9 Blue Swirl
2899 1ec3f6f9 Blue Swirl
	if (!$is_patch) {
2900 1ec3f6f9 Blue Swirl
		ERROR("Does not appear to be a unified-diff format patch\n");
2901 1ec3f6f9 Blue Swirl
	}
2902 1ec3f6f9 Blue Swirl
	if ($is_patch && $chk_signoff && $signoff == 0) {
2903 1ec3f6f9 Blue Swirl
		ERROR("Missing Signed-off-by: line(s)\n");
2904 1ec3f6f9 Blue Swirl
	}
2905 1ec3f6f9 Blue Swirl
2906 1ec3f6f9 Blue Swirl
	print report_dump();
2907 1ec3f6f9 Blue Swirl
	if ($summary && !($clean == 1 && $quiet == 1)) {
2908 1ec3f6f9 Blue Swirl
		print "$filename " if ($summary_file);
2909 1ec3f6f9 Blue Swirl
		print "total: $cnt_error errors, $cnt_warn warnings, " .
2910 1ec3f6f9 Blue Swirl
			(($check)? "$cnt_chk checks, " : "") .
2911 1ec3f6f9 Blue Swirl
			"$cnt_lines lines checked\n";
2912 1ec3f6f9 Blue Swirl
		print "\n" if ($quiet == 0);
2913 1ec3f6f9 Blue Swirl
	}
2914 1ec3f6f9 Blue Swirl
2915 1ec3f6f9 Blue Swirl
	if ($quiet == 0) {
2916 1ec3f6f9 Blue Swirl
		# If there were whitespace errors which cleanpatch can fix
2917 1ec3f6f9 Blue Swirl
		# then suggest that.
2918 b6469683 Blue Swirl
#		if ($rpt_cleaners) {
2919 b6469683 Blue Swirl
#			print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n";
2920 b6469683 Blue Swirl
#			print "      scripts/cleanfile\n\n";
2921 b6469683 Blue Swirl
#		}
2922 1ec3f6f9 Blue Swirl
	}
2923 1ec3f6f9 Blue Swirl
2924 1ec3f6f9 Blue Swirl
	if ($clean == 1 && $quiet == 0) {
2925 1ec3f6f9 Blue Swirl
		print "$vname has no obvious style problems and is ready for submission.\n"
2926 1ec3f6f9 Blue Swirl
	}
2927 1ec3f6f9 Blue Swirl
	if ($clean == 0 && $quiet == 0) {
2928 1ec3f6f9 Blue Swirl
		print "$vname has style problems, please review.  If any of these errors\n";
2929 1ec3f6f9 Blue Swirl
		print "are false positives report them to the maintainer, see\n";
2930 1ec3f6f9 Blue Swirl
		print "CHECKPATCH in MAINTAINERS.\n";
2931 1ec3f6f9 Blue Swirl
	}
2932 1ec3f6f9 Blue Swirl
2933 1ec3f6f9 Blue Swirl
	return $clean;
2934 1ec3f6f9 Blue Swirl
}