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