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