Commit | Line | Data |
---|---|---|
0a920b5b AW |
1 | #!/usr/bin/perl -w |
2 | # (c) 2001, Dave Jones. <davej@codemonkey.org.uk> (the file handling bit) | |
00df344f | 3 | # (c) 2005, Joel Schopp <jschopp@austin.ibm.com> (the ugly bit) |
0a920b5b AW |
4 | # (c) 2007, Andy Whitcroft <apw@uk.ibm.com> (new conditions, test suite, etc) |
5 | # Licensed under the terms of the GNU GPL License version 2 | |
6 | ||
7 | use strict; | |
8 | ||
9 | my $P = $0; | |
00df344f | 10 | $P =~ s@.*/@@g; |
0a920b5b | 11 | |
c2fdda0d | 12 | my $V = '0.13'; |
0a920b5b AW |
13 | |
14 | use Getopt::Long qw(:config no_auto_abbrev); | |
15 | ||
16 | my $quiet = 0; | |
17 | my $tree = 1; | |
18 | my $chk_signoff = 1; | |
19 | my $chk_patch = 1; | |
653d4876 | 20 | my $tst_type = 0; |
6c72ffaa | 21 | my $emacs = 0; |
8905a67c | 22 | my $terse = 0; |
6c72ffaa AW |
23 | my $file = 0; |
24 | my $check = 0; | |
8905a67c AW |
25 | my $summary = 1; |
26 | my $mailback = 0; | |
6c72ffaa | 27 | my $root; |
c2fdda0d | 28 | my %debug; |
0a920b5b | 29 | GetOptions( |
6c72ffaa | 30 | 'q|quiet+' => \$quiet, |
0a920b5b AW |
31 | 'tree!' => \$tree, |
32 | 'signoff!' => \$chk_signoff, | |
33 | 'patch!' => \$chk_patch, | |
653d4876 | 34 | 'test-type!' => \$tst_type, |
6c72ffaa | 35 | 'emacs!' => \$emacs, |
8905a67c | 36 | 'terse!' => \$terse, |
6c72ffaa AW |
37 | 'file!' => \$file, |
38 | 'subjective!' => \$check, | |
39 | 'strict!' => \$check, | |
40 | 'root=s' => \$root, | |
8905a67c AW |
41 | 'summary!' => \$summary, |
42 | 'mailback!' => \$mailback, | |
c2fdda0d | 43 | 'debug=s' => \%debug, |
0a920b5b AW |
44 | ) or exit; |
45 | ||
46 | my $exit = 0; | |
47 | ||
48 | if ($#ARGV < 0) { | |
00df344f | 49 | print "usage: $P [options] patchfile\n"; |
0a920b5b AW |
50 | print "version: $V\n"; |
51 | print "options: -q => quiet\n"; | |
52 | print " --no-tree => run without a kernel tree\n"; | |
8905a67c | 53 | print " --terse => one line per report\n"; |
6c72ffaa AW |
54 | print " --emacs => emacs compile window format\n"; |
55 | print " --file => check a source file\n"; | |
56 | print " --strict => enable more subjective tests\n"; | |
57 | print " --root => path to the kernel tree root\n"; | |
0a920b5b AW |
58 | exit(1); |
59 | } | |
60 | ||
c2fdda0d AW |
61 | my $dbg_values = 0; |
62 | my $dbg_possible = 0; | |
63 | for my $key (keys %debug) { | |
64 | eval "\${dbg_$key} = '$debug{$key}';" | |
65 | } | |
66 | ||
8905a67c AW |
67 | if ($terse) { |
68 | $emacs = 1; | |
69 | $quiet++; | |
70 | } | |
71 | ||
6c72ffaa AW |
72 | if ($tree) { |
73 | if (defined $root) { | |
74 | if (!top_of_kernel_tree($root)) { | |
75 | die "$P: $root: --root does not point at a valid tree\n"; | |
76 | } | |
77 | } else { | |
78 | if (top_of_kernel_tree('.')) { | |
79 | $root = '.'; | |
80 | } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ && | |
81 | top_of_kernel_tree($1)) { | |
82 | $root = $1; | |
83 | } | |
84 | } | |
85 | ||
86 | if (!defined $root) { | |
87 | print "Must be run from the top-level dir. of a kernel tree\n"; | |
88 | exit(2); | |
89 | } | |
0a920b5b AW |
90 | } |
91 | ||
6c72ffaa AW |
92 | my $emitted_corrupt = 0; |
93 | ||
94 | our $Ident = qr{[A-Za-z_][A-Za-z\d_]*}; | |
95 | our $Storage = qr{extern|static|asmlinkage}; | |
96 | our $Sparse = qr{ | |
97 | __user| | |
98 | __kernel| | |
99 | __force| | |
100 | __iomem| | |
101 | __must_check| | |
102 | __init_refok| | |
103 | __kprobes| | |
104 | fastcall | |
105 | }x; | |
106 | our $Attribute = qr{ | |
107 | const| | |
108 | __read_mostly| | |
109 | __kprobes| | |
110 | __(?:mem|cpu|dev|)(?:initdata|init) | |
111 | }x; | |
112 | our $Inline = qr{inline|__always_inline|noinline}; | |
6c72ffaa AW |
113 | our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]}; |
114 | our $Lval = qr{$Ident(?:$Member)*}; | |
115 | ||
116 | our $Constant = qr{(?:[0-9]+|0x[0-9a-fA-F]+)[UL]*}; | |
117 | our $Assignment = qr{(?:\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=)}; | |
118 | our $Operators = qr{ | |
119 | <=|>=|==|!=| | |
120 | =>|->|<<|>>|<|>|!|~| | |
c2fdda0d | 121 | &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|% |
6c72ffaa AW |
122 | }x; |
123 | ||
8905a67c AW |
124 | our $NonptrType; |
125 | our $Type; | |
126 | our $Declare; | |
127 | ||
128 | our @typeList = ( | |
129 | qr{void}, | |
130 | qr{char}, | |
131 | qr{short}, | |
132 | qr{int}, | |
133 | qr{long}, | |
134 | qr{unsigned}, | |
135 | qr{float}, | |
136 | qr{double}, | |
137 | qr{bool}, | |
138 | qr{long\s+int}, | |
139 | qr{long\s+long}, | |
140 | qr{long\s+long\s+int}, | |
141 | qr{(?:__)?(?:u|s|be|le)(?:8|16|32|64)}, | |
142 | qr{struct\s+$Ident}, | |
143 | qr{union\s+$Ident}, | |
144 | qr{enum\s+$Ident}, | |
145 | qr{${Ident}_t}, | |
146 | qr{${Ident}_handler}, | |
147 | qr{${Ident}_handler_fn}, | |
148 | ); | |
149 | ||
150 | sub build_types { | |
151 | my $all = "(?: \n" . join("|\n ", @typeList) . "\n)"; | |
152 | $NonptrType = qr{ | |
153 | \b | |
154 | (?:const\s+)? | |
155 | (?:unsigned\s+)? | |
156 | $all | |
157 | (?:\s+$Sparse|\s+const)* | |
158 | \b | |
159 | }x; | |
160 | $Type = qr{ | |
161 | \b$NonptrType\b | |
162 | (?:\s*\*+\s*const|\s*\*+|(?:\s*\[\s*\])+)? | |
c2fdda0d | 163 | (?:\s+$Inline|\s+$Sparse|\s+$Attribute)* |
8905a67c AW |
164 | }x; |
165 | $Declare = qr{(?:$Storage\s+)?$Type}; | |
166 | } | |
167 | build_types(); | |
6c72ffaa AW |
168 | |
169 | $chk_signoff = 0 if ($file); | |
170 | ||
4a0df2ef AW |
171 | my @dep_includes = (); |
172 | my @dep_functions = (); | |
6c72ffaa AW |
173 | my $removal = "Documentation/feature-removal-schedule.txt"; |
174 | if ($tree && -f "$root/$removal") { | |
175 | open(REMOVE, "<$root/$removal") || | |
176 | die "$P: $removal: open failed - $!\n"; | |
0a920b5b | 177 | while (<REMOVE>) { |
f0a594c1 AW |
178 | if (/^Check:\s+(.*\S)/) { |
179 | for my $entry (split(/[, ]+/, $1)) { | |
180 | if ($entry =~ m@include/(.*)@) { | |
4a0df2ef | 181 | push(@dep_includes, $1); |
4a0df2ef | 182 | |
f0a594c1 AW |
183 | } elsif ($entry !~ m@/@) { |
184 | push(@dep_functions, $entry); | |
185 | } | |
4a0df2ef | 186 | } |
0a920b5b AW |
187 | } |
188 | } | |
189 | } | |
190 | ||
00df344f | 191 | my @rawlines = (); |
c2fdda0d AW |
192 | my @lines = (); |
193 | my $vname; | |
6c72ffaa AW |
194 | for my $filename (@ARGV) { |
195 | if ($file) { | |
196 | open(FILE, "diff -u /dev/null $filename|") || | |
197 | die "$P: $filename: diff failed - $!\n"; | |
198 | } else { | |
199 | open(FILE, "<$filename") || | |
200 | die "$P: $filename: open failed - $!\n"; | |
0a920b5b | 201 | } |
c2fdda0d AW |
202 | if ($filename eq '-') { |
203 | $vname = 'Your patch'; | |
204 | } else { | |
205 | $vname = $filename; | |
206 | } | |
6c72ffaa AW |
207 | while (<FILE>) { |
208 | chomp; | |
209 | push(@rawlines, $_); | |
210 | } | |
211 | close(FILE); | |
c2fdda0d | 212 | if (!process($filename)) { |
6c72ffaa AW |
213 | $exit = 1; |
214 | } | |
215 | @rawlines = (); | |
0a920b5b AW |
216 | } |
217 | ||
218 | exit($exit); | |
219 | ||
220 | sub top_of_kernel_tree { | |
6c72ffaa AW |
221 | my ($root) = @_; |
222 | ||
223 | my @tree_check = ( | |
224 | "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile", | |
225 | "README", "Documentation", "arch", "include", "drivers", | |
226 | "fs", "init", "ipc", "kernel", "lib", "scripts", | |
227 | ); | |
228 | ||
229 | foreach my $check (@tree_check) { | |
230 | if (! -e $root . '/' . $check) { | |
231 | return 0; | |
232 | } | |
0a920b5b | 233 | } |
6c72ffaa | 234 | return 1; |
0a920b5b AW |
235 | } |
236 | ||
237 | sub expand_tabs { | |
238 | my ($str) = @_; | |
239 | ||
240 | my $res = ''; | |
241 | my $n = 0; | |
242 | for my $c (split(//, $str)) { | |
243 | if ($c eq "\t") { | |
244 | $res .= ' '; | |
245 | $n++; | |
246 | for (; ($n % 8) != 0; $n++) { | |
247 | $res .= ' '; | |
248 | } | |
249 | next; | |
250 | } | |
251 | $res .= $c; | |
252 | $n++; | |
253 | } | |
254 | ||
255 | return $res; | |
256 | } | |
6c72ffaa AW |
257 | sub copy_spacing { |
258 | my ($str) = @_; | |
259 | ||
260 | my $res = ''; | |
261 | for my $c (split(//, $str)) { | |
262 | if ($c eq "\t") { | |
263 | $res .= $c; | |
264 | } else { | |
265 | $res .= ' '; | |
266 | } | |
267 | } | |
268 | ||
269 | return $res; | |
270 | } | |
0a920b5b | 271 | |
4a0df2ef AW |
272 | sub line_stats { |
273 | my ($line) = @_; | |
274 | ||
275 | # Drop the diff line leader and expand tabs | |
276 | $line =~ s/^.//; | |
277 | $line = expand_tabs($line); | |
278 | ||
279 | # Pick the indent from the front of the line. | |
280 | my ($white) = ($line =~ /^(\s*)/); | |
281 | ||
282 | return (length($line), length($white)); | |
283 | } | |
284 | ||
00df344f AW |
285 | sub sanitise_line { |
286 | my ($line) = @_; | |
287 | ||
288 | my $res = ''; | |
289 | my $l = ''; | |
290 | ||
291 | my $quote = ''; | |
c2fdda0d | 292 | my $qlen = 0; |
00df344f AW |
293 | |
294 | foreach my $c (split(//, $line)) { | |
c2fdda0d AW |
295 | # The second backslash of a pair is not a "quote". |
296 | if ($l eq "\\" && $c eq "\\") { | |
297 | $c = 'X'; | |
298 | } | |
00df344f AW |
299 | if ($l ne "\\" && ($c eq "'" || $c eq '"')) { |
300 | if ($quote eq '') { | |
301 | $quote = $c; | |
302 | $res .= $c; | |
303 | $l = $c; | |
c2fdda0d | 304 | $qlen = 0; |
00df344f AW |
305 | next; |
306 | } elsif ($quote eq $c) { | |
307 | $quote = ''; | |
308 | } | |
309 | } | |
c2fdda0d AW |
310 | if ($quote eq "'" && $qlen > 1) { |
311 | $quote = ''; | |
312 | } | |
00df344f AW |
313 | if ($quote && $c ne "\t") { |
314 | $res .= "X"; | |
c2fdda0d | 315 | $qlen++; |
00df344f AW |
316 | } else { |
317 | $res .= $c; | |
318 | } | |
319 | ||
320 | $l = $c; | |
321 | } | |
322 | ||
c2fdda0d AW |
323 | # Clear out the comments. |
324 | while ($res =~ m@(/\*.*?\*/)@) { | |
325 | substr($res, $-[1], $+[1] - $-[1]) = ' ' x ($+[1] - $-[1]); | |
326 | } | |
327 | if ($res =~ m@(/\*.*)@) { | |
328 | substr($res, $-[1], $+[1] - $-[1]) = ' ' x ($+[1] - $-[1]); | |
329 | } | |
330 | if ($res =~ m@^.(.*\*/)@) { | |
331 | substr($res, $-[1], $+[1] - $-[1]) = ' ' x ($+[1] - $-[1]); | |
332 | } | |
333 | ||
334 | # The pathname on a #include may be surrounded by '<' and '>'. | |
335 | if ($res =~ /^.#\s*include\s+\<(.*)\>/) { | |
336 | my $clean = 'X' x length($1); | |
337 | $res =~ s@\<.*\>@<$clean>@; | |
338 | ||
339 | # The whole of a #error is a string. | |
340 | } elsif ($res =~ /^.#\s*(?:error|warning)\s+(.*)\b/) { | |
341 | my $clean = 'X' x length($1); | |
342 | $res =~ s@(#\s*(?:error|warning)\s+).*@$1$clean@; | |
343 | } | |
344 | ||
00df344f AW |
345 | return $res; |
346 | } | |
347 | ||
8905a67c AW |
348 | sub ctx_statement_block { |
349 | my ($linenr, $remain, $off) = @_; | |
350 | my $line = $linenr - 1; | |
351 | my $blk = ''; | |
352 | my $soff = $off; | |
353 | my $coff = $off - 1; | |
354 | ||
355 | my $type = ''; | |
356 | my $level = 0; | |
357 | my $c; | |
358 | my $len = 0; | |
359 | while (1) { | |
360 | #warn "CSB: blk<$blk>\n"; | |
361 | # If we are about to drop off the end, pull in more | |
362 | # context. | |
363 | if ($off >= $len) { | |
364 | for (; $remain > 0; $line++) { | |
c2fdda0d | 365 | next if ($lines[$line] =~ /^-/); |
8905a67c | 366 | $remain--; |
c2fdda0d | 367 | $blk .= $lines[$line] . "\n"; |
8905a67c AW |
368 | $len = length($blk); |
369 | $line++; | |
370 | last; | |
371 | } | |
372 | # Bail if there is no further context. | |
373 | #warn "CSB: blk<$blk> off<$off> len<$len>\n"; | |
374 | if ($off == $len) { | |
375 | last; | |
376 | } | |
377 | } | |
378 | $c = substr($blk, $off, 1); | |
379 | ||
380 | #warn "CSB: c<$c> type<$type> level<$level>\n"; | |
381 | # Statement ends at the ';' or a close '}' at the | |
382 | # outermost level. | |
383 | if ($level == 0 && $c eq ';') { | |
384 | last; | |
385 | } | |
386 | ||
387 | if (($type eq '' || $type eq '(') && $c eq '(') { | |
388 | $level++; | |
389 | $type = '('; | |
390 | } | |
391 | if ($type eq '(' && $c eq ')') { | |
392 | $level--; | |
393 | $type = ($level != 0)? '(' : ''; | |
394 | ||
395 | if ($level == 0 && $coff < $soff) { | |
396 | $coff = $off; | |
397 | } | |
398 | } | |
399 | if (($type eq '' || $type eq '{') && $c eq '{') { | |
400 | $level++; | |
401 | $type = '{'; | |
402 | } | |
403 | if ($type eq '{' && $c eq '}') { | |
404 | $level--; | |
405 | $type = ($level != 0)? '{' : ''; | |
406 | ||
407 | if ($level == 0) { | |
408 | last; | |
409 | } | |
410 | } | |
411 | $off++; | |
412 | } | |
413 | ||
414 | my $statement = substr($blk, $soff, $off - $soff + 1); | |
415 | my $condition = substr($blk, $soff, $coff - $soff + 1); | |
416 | ||
417 | #warn "STATEMENT<$statement>\n"; | |
418 | #warn "CONDITION<$condition>\n"; | |
419 | ||
420 | return ($statement, $condition); | |
421 | } | |
422 | ||
4a0df2ef | 423 | sub ctx_block_get { |
f0a594c1 | 424 | my ($linenr, $remain, $outer, $open, $close, $off) = @_; |
4a0df2ef AW |
425 | my $line; |
426 | my $start = $linenr - 1; | |
4a0df2ef AW |
427 | my $blk = ''; |
428 | my @o; | |
429 | my @c; | |
430 | my @res = (); | |
431 | ||
f0a594c1 | 432 | my $level = 0; |
00df344f AW |
433 | for ($line = $start; $remain > 0; $line++) { |
434 | next if ($rawlines[$line] =~ /^-/); | |
435 | $remain--; | |
436 | ||
437 | $blk .= $rawlines[$line]; | |
f0a594c1 AW |
438 | foreach my $c (split(//, $rawlines[$line])) { |
439 | ##print "C<$c>L<$level><$open$close>O<$off>\n"; | |
440 | if ($off > 0) { | |
441 | $off--; | |
442 | next; | |
443 | } | |
4a0df2ef | 444 | |
f0a594c1 AW |
445 | if ($c eq $close && $level > 0) { |
446 | $level--; | |
447 | last if ($level == 0); | |
448 | } elsif ($c eq $open) { | |
449 | $level++; | |
450 | } | |
451 | } | |
4a0df2ef | 452 | |
f0a594c1 | 453 | if (!$outer || $level <= 1) { |
00df344f | 454 | push(@res, $rawlines[$line]); |
4a0df2ef AW |
455 | } |
456 | ||
f0a594c1 | 457 | last if ($level == 0); |
4a0df2ef AW |
458 | } |
459 | ||
f0a594c1 | 460 | return ($level, @res); |
4a0df2ef AW |
461 | } |
462 | sub ctx_block_outer { | |
463 | my ($linenr, $remain) = @_; | |
464 | ||
f0a594c1 AW |
465 | my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0); |
466 | return @r; | |
4a0df2ef AW |
467 | } |
468 | sub ctx_block { | |
469 | my ($linenr, $remain) = @_; | |
470 | ||
f0a594c1 AW |
471 | my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0); |
472 | return @r; | |
653d4876 AW |
473 | } |
474 | sub ctx_statement { | |
f0a594c1 AW |
475 | my ($linenr, $remain, $off) = @_; |
476 | ||
477 | my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off); | |
478 | return @r; | |
479 | } | |
480 | sub ctx_block_level { | |
653d4876 AW |
481 | my ($linenr, $remain) = @_; |
482 | ||
f0a594c1 | 483 | return ctx_block_get($linenr, $remain, 0, '{', '}', 0); |
4a0df2ef | 484 | } |
9c0ca6f9 AW |
485 | sub ctx_statement_level { |
486 | my ($linenr, $remain, $off) = @_; | |
487 | ||
488 | return ctx_block_get($linenr, $remain, 0, '(', ')', $off); | |
489 | } | |
4a0df2ef AW |
490 | |
491 | sub ctx_locate_comment { | |
492 | my ($first_line, $end_line) = @_; | |
493 | ||
494 | # Catch a comment on the end of the line itself. | |
00df344f | 495 | my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*$@); |
4a0df2ef AW |
496 | return $current_comment if (defined $current_comment); |
497 | ||
498 | # Look through the context and try and figure out if there is a | |
499 | # comment. | |
500 | my $in_comment = 0; | |
501 | $current_comment = ''; | |
502 | for (my $linenr = $first_line; $linenr < $end_line; $linenr++) { | |
00df344f AW |
503 | my $line = $rawlines[$linenr - 1]; |
504 | #warn " $line\n"; | |
4a0df2ef AW |
505 | if ($linenr == $first_line and $line =~ m@^.\s*\*@) { |
506 | $in_comment = 1; | |
507 | } | |
508 | if ($line =~ m@/\*@) { | |
509 | $in_comment = 1; | |
510 | } | |
511 | if (!$in_comment && $current_comment ne '') { | |
512 | $current_comment = ''; | |
513 | } | |
514 | $current_comment .= $line . "\n" if ($in_comment); | |
515 | if ($line =~ m@\*/@) { | |
516 | $in_comment = 0; | |
517 | } | |
518 | } | |
519 | ||
520 | chomp($current_comment); | |
521 | return($current_comment); | |
522 | } | |
523 | sub ctx_has_comment { | |
524 | my ($first_line, $end_line) = @_; | |
525 | my $cmt = ctx_locate_comment($first_line, $end_line); | |
526 | ||
00df344f | 527 | ##print "LINE: $rawlines[$end_line - 1 ]\n"; |
4a0df2ef AW |
528 | ##print "CMMT: $cmt\n"; |
529 | ||
530 | return ($cmt ne ''); | |
531 | } | |
532 | ||
6c72ffaa AW |
533 | sub cat_vet { |
534 | my ($vet) = @_; | |
535 | my ($res, $coded); | |
9c0ca6f9 | 536 | |
6c72ffaa AW |
537 | $res = ''; |
538 | while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) { | |
539 | $res .= $1; | |
540 | if ($2 ne '') { | |
541 | $coded = sprintf("^%c", unpack('C', $2) + 64); | |
542 | $res .= $coded; | |
9c0ca6f9 AW |
543 | } |
544 | } | |
6c72ffaa | 545 | $res =~ s/$/\$/; |
9c0ca6f9 | 546 | |
6c72ffaa | 547 | return $res; |
9c0ca6f9 AW |
548 | } |
549 | ||
c2fdda0d AW |
550 | my $av_preprocessor = 0; |
551 | my $av_paren = 0; | |
552 | my @av_paren_type; | |
553 | ||
554 | sub annotate_reset { | |
555 | $av_preprocessor = 0; | |
556 | $av_paren = 0; | |
557 | @av_paren_type = (); | |
558 | } | |
559 | ||
6c72ffaa AW |
560 | sub annotate_values { |
561 | my ($stream, $type) = @_; | |
0a920b5b | 562 | |
6c72ffaa AW |
563 | my $res; |
564 | my $cur = $stream; | |
565 | ||
c2fdda0d | 566 | print "$stream\n" if ($dbg_values > 1); |
6c72ffaa | 567 | |
6c72ffaa | 568 | while (length($cur)) { |
c2fdda0d | 569 | print " <$type> " if ($dbg_values > 1); |
6c72ffaa | 570 | if ($cur =~ /^(\s+)/o) { |
c2fdda0d AW |
571 | print "WS($1)\n" if ($dbg_values > 1); |
572 | if ($1 =~ /\n/ && $av_preprocessor) { | |
573 | $av_preprocessor = 0; | |
6c72ffaa AW |
574 | $type = 'N'; |
575 | } | |
576 | ||
8905a67c | 577 | } elsif ($cur =~ /^($Type)/) { |
c2fdda0d | 578 | print "DECLARE($1)\n" if ($dbg_values > 1); |
6c72ffaa AW |
579 | $type = 'T'; |
580 | ||
581 | } elsif ($cur =~ /^(#\s*define\s*$Ident)(\(?)/o) { | |
c2fdda0d AW |
582 | print "DEFINE($1)\n" if ($dbg_values > 1); |
583 | $av_preprocessor = 1; | |
584 | $av_paren_type[$av_paren] = 'N'; | |
6c72ffaa | 585 | |
c2fdda0d AW |
586 | } elsif ($cur =~ /^(#\s*(?:ifdef|ifndef|if|else|elif|endif))/o) { |
587 | print "PRE($1)\n" if ($dbg_values > 1); | |
588 | $av_preprocessor = 1; | |
6c72ffaa AW |
589 | $type = 'N'; |
590 | ||
591 | } elsif ($cur =~ /^(\\\n)/o) { | |
c2fdda0d | 592 | print "PRECONT($1)\n" if ($dbg_values > 1); |
6c72ffaa AW |
593 | |
594 | } elsif ($cur =~ /^(sizeof)\s*(\()?/o) { | |
c2fdda0d | 595 | print "SIZEOF($1)\n" if ($dbg_values > 1); |
6c72ffaa | 596 | if (defined $2) { |
c2fdda0d | 597 | $av_paren_type[$av_paren] = 'V'; |
6c72ffaa AW |
598 | } |
599 | $type = 'N'; | |
600 | ||
8905a67c | 601 | } elsif ($cur =~ /^(if|while|typeof|for)\b/o) { |
c2fdda0d AW |
602 | print "COND($1)\n" if ($dbg_values > 1); |
603 | $av_paren_type[$av_paren] = 'N'; | |
6c72ffaa AW |
604 | $type = 'N'; |
605 | ||
606 | } elsif ($cur =~/^(return|case|else)/o) { | |
c2fdda0d | 607 | print "KEYWORD($1)\n" if ($dbg_values > 1); |
6c72ffaa AW |
608 | $type = 'N'; |
609 | ||
610 | } elsif ($cur =~ /^(\()/o) { | |
c2fdda0d AW |
611 | print "PAREN('$1')\n" if ($dbg_values > 1); |
612 | $av_paren++; | |
6c72ffaa AW |
613 | $type = 'N'; |
614 | ||
615 | } elsif ($cur =~ /^(\))/o) { | |
c2fdda0d AW |
616 | $av_paren-- if ($av_paren > 0); |
617 | if (defined $av_paren_type[$av_paren]) { | |
618 | $type = $av_paren_type[$av_paren]; | |
619 | undef $av_paren_type[$av_paren]; | |
620 | print "PAREN('$1') -> $type\n" | |
621 | if ($dbg_values > 1); | |
6c72ffaa | 622 | } else { |
c2fdda0d | 623 | print "PAREN('$1')\n" if ($dbg_values > 1); |
6c72ffaa AW |
624 | } |
625 | ||
626 | } elsif ($cur =~ /^($Ident)\(/o) { | |
c2fdda0d AW |
627 | print "FUNC($1)\n" if ($dbg_values > 1); |
628 | $av_paren_type[$av_paren] = 'V'; | |
6c72ffaa AW |
629 | |
630 | } elsif ($cur =~ /^($Ident|$Constant)/o) { | |
c2fdda0d | 631 | print "IDENT($1)\n" if ($dbg_values > 1); |
6c72ffaa AW |
632 | $type = 'V'; |
633 | ||
634 | } elsif ($cur =~ /^($Assignment)/o) { | |
c2fdda0d | 635 | print "ASSIGN($1)\n" if ($dbg_values > 1); |
6c72ffaa AW |
636 | $type = 'N'; |
637 | ||
638 | } elsif ($cur =~ /^(;|{|}|\?|:|\[)/o) { | |
c2fdda0d | 639 | print "END($1)\n" if ($dbg_values > 1); |
6c72ffaa AW |
640 | $type = 'N'; |
641 | ||
642 | } elsif ($cur =~ /^($Operators)/o) { | |
c2fdda0d | 643 | print "OP($1)\n" if ($dbg_values > 1); |
6c72ffaa AW |
644 | if ($1 ne '++' && $1 ne '--') { |
645 | $type = 'N'; | |
646 | } | |
647 | ||
648 | } elsif ($cur =~ /(^.)/o) { | |
c2fdda0d | 649 | print "C($1)\n" if ($dbg_values > 1); |
6c72ffaa AW |
650 | } |
651 | if (defined $1) { | |
652 | $cur = substr($cur, length($1)); | |
653 | $res .= $type x length($1); | |
654 | } | |
9c0ca6f9 | 655 | } |
0a920b5b | 656 | |
9c0ca6f9 | 657 | return $res; |
0a920b5b AW |
658 | } |
659 | ||
8905a67c AW |
660 | sub possible { |
661 | my ($possible) = @_; | |
662 | ||
663 | #print "CHECK<$possible>\n"; | |
664 | if ($possible !~ /^(?:$Storage|$Type|DEFINE_\S+)$/ && | |
665 | $possible ne 'goto' && $possible ne 'return' && | |
666 | $possible ne 'struct' && $possible ne 'enum' && | |
667 | $possible ne 'case' && $possible ne 'else' && | |
668 | $possible ne 'typedef') { | |
c2fdda0d | 669 | warn "POSSIBLE: $possible\n" if ($dbg_possible); |
8905a67c AW |
670 | push(@typeList, $possible); |
671 | build_types(); | |
672 | } | |
673 | } | |
674 | ||
6c72ffaa AW |
675 | my $prefix = ''; |
676 | ||
f0a594c1 AW |
677 | my @report = (); |
678 | sub report { | |
8905a67c AW |
679 | my $line = $prefix . $_[0]; |
680 | ||
681 | $line = (split('\n', $line))[0] . "\n" if ($terse); | |
682 | ||
683 | push(@report, $line); | |
f0a594c1 AW |
684 | } |
685 | sub report_dump { | |
686 | @report; | |
687 | } | |
de7d4f0e | 688 | sub ERROR { |
f0a594c1 | 689 | report("ERROR: $_[0]\n"); |
de7d4f0e | 690 | our $clean = 0; |
6c72ffaa | 691 | our $cnt_error++; |
de7d4f0e AW |
692 | } |
693 | sub WARN { | |
f0a594c1 | 694 | report("WARNING: $_[0]\n"); |
de7d4f0e | 695 | our $clean = 0; |
6c72ffaa | 696 | our $cnt_warn++; |
de7d4f0e AW |
697 | } |
698 | sub CHK { | |
6c72ffaa AW |
699 | if ($check) { |
700 | report("CHECK: $_[0]\n"); | |
701 | our $clean = 0; | |
702 | our $cnt_chk++; | |
703 | } | |
de7d4f0e AW |
704 | } |
705 | ||
0a920b5b AW |
706 | sub process { |
707 | my $filename = shift; | |
0a920b5b AW |
708 | |
709 | my $linenr=0; | |
710 | my $prevline=""; | |
c2fdda0d | 711 | my $prevrawline=""; |
0a920b5b | 712 | my $stashline=""; |
c2fdda0d | 713 | my $stashrawline=""; |
0a920b5b | 714 | |
4a0df2ef | 715 | my $length; |
0a920b5b AW |
716 | my $indent; |
717 | my $previndent=0; | |
718 | my $stashindent=0; | |
719 | ||
de7d4f0e | 720 | our $clean = 1; |
0a920b5b AW |
721 | my $signoff = 0; |
722 | my $is_patch = 0; | |
723 | ||
6c72ffaa AW |
724 | our $cnt_lines = 0; |
725 | our $cnt_error = 0; | |
726 | our $cnt_warn = 0; | |
727 | our $cnt_chk = 0; | |
728 | ||
0a920b5b AW |
729 | # Trace the real file/line as we go. |
730 | my $realfile = ''; | |
731 | my $realline = 0; | |
732 | my $realcnt = 0; | |
733 | my $here = ''; | |
734 | my $in_comment = 0; | |
c2fdda0d | 735 | my $comment_edge = 0; |
0a920b5b AW |
736 | my $first_line = 0; |
737 | ||
6c72ffaa | 738 | my $prev_values = 'N'; |
653d4876 | 739 | |
c2fdda0d | 740 | # Pre-scan the patch sanitizing the lines. |
de7d4f0e | 741 | # Pre-scan the patch looking for any __setup documentation. |
c2fdda0d | 742 | # |
de7d4f0e AW |
743 | my @setup_docs = (); |
744 | my $setup_docs = 0; | |
c2fdda0d AW |
745 | my $line; |
746 | foreach my $rawline (@rawlines) { | |
747 | # Standardise the strings and chars within the input to | |
748 | # simplify matching. | |
749 | $line = sanitise_line($rawline); | |
750 | push(@lines, $line); | |
751 | ||
752 | ##print "==>$rawline\n"; | |
753 | ##print "-->$line\n"; | |
754 | ||
de7d4f0e AW |
755 | if ($line=~/^\+\+\+\s+(\S+)/) { |
756 | $setup_docs = 0; | |
757 | if ($1 =~ m@Documentation/kernel-parameters.txt$@) { | |
758 | $setup_docs = 1; | |
759 | } | |
760 | next; | |
761 | } | |
762 | ||
763 | if ($setup_docs && $line =~ /^\+/) { | |
764 | push(@setup_docs, $line); | |
765 | } | |
766 | } | |
767 | ||
6c72ffaa AW |
768 | $prefix = ''; |
769 | ||
0a920b5b AW |
770 | foreach my $line (@lines) { |
771 | $linenr++; | |
772 | ||
c2fdda0d | 773 | my $rawline = $rawlines[$linenr - 1]; |
6c72ffaa | 774 | |
0a920b5b AW |
775 | #extract the filename as it passes |
776 | if ($line=~/^\+\+\+\s+(\S+)/) { | |
777 | $realfile=$1; | |
00df344f | 778 | $realfile =~ s@^[^/]*/@@; |
0a920b5b AW |
779 | $in_comment = 0; |
780 | next; | |
781 | } | |
782 | #extract the line range in the file after the patch is applied | |
6c72ffaa | 783 | if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) { |
0a920b5b | 784 | $is_patch = 1; |
4a0df2ef | 785 | $first_line = $linenr + 1; |
0a920b5b AW |
786 | $in_comment = 0; |
787 | $realline=$1-1; | |
788 | if (defined $2) { | |
789 | $realcnt=$3+1; | |
790 | } else { | |
791 | $realcnt=1+1; | |
792 | } | |
c2fdda0d | 793 | annotate_reset(); |
6c72ffaa | 794 | $prev_values = 'N'; |
0a920b5b AW |
795 | next; |
796 | } | |
797 | ||
4a0df2ef AW |
798 | # track the line number as we move through the hunk, note that |
799 | # new versions of GNU diff omit the leading space on completely | |
800 | # blank context lines so we need to count that too. | |
801 | if ($line =~ /^( |\+|$)/) { | |
0a920b5b | 802 | $realline++; |
d8aaf121 | 803 | $realcnt-- if ($realcnt != 0); |
0a920b5b | 804 | |
8905a67c AW |
805 | # Guestimate if this is a continuing comment. Run |
806 | # the context looking for a comment "edge". If this | |
807 | # edge is a close comment then we must be in a comment | |
808 | # at context start. | |
809 | if ($linenr == $first_line) { | |
810 | my $edge; | |
811 | for (my $ln = $first_line; $ln < ($linenr + $realcnt); $ln++) { | |
c2fdda0d | 812 | ($edge) = ($rawlines[$ln - 1] =~ m@(/\*|\*/)@); |
8905a67c AW |
813 | last if (defined $edge); |
814 | } | |
815 | if (defined $edge && $edge eq '*/') { | |
816 | $in_comment = 1; | |
817 | } | |
818 | } | |
819 | ||
0a920b5b AW |
820 | # Guestimate if this is a continuing comment. If this |
821 | # is the start of a diff block and this line starts | |
822 | # ' *' then it is very likely a comment. | |
c2fdda0d | 823 | if ($linenr == $first_line and $rawline =~ m@^.\s* \*(?:\s|$)@) { |
0a920b5b AW |
824 | $in_comment = 1; |
825 | } | |
8905a67c AW |
826 | |
827 | # Find the last comment edge on _this_ line. | |
c2fdda0d AW |
828 | $comment_edge = 0; |
829 | while (($rawline =~ m@(/\*|\*/)@g)) { | |
8905a67c AW |
830 | if ($1 eq '/*') { |
831 | $in_comment = 1; | |
832 | } else { | |
833 | $in_comment = 0; | |
834 | } | |
c2fdda0d | 835 | $comment_edge = 1; |
0a920b5b AW |
836 | } |
837 | ||
4a0df2ef | 838 | # Measure the line length and indent. |
c2fdda0d | 839 | ($length, $indent) = line_stats($rawline); |
0a920b5b AW |
840 | |
841 | # Track the previous line. | |
842 | ($prevline, $stashline) = ($stashline, $line); | |
843 | ($previndent, $stashindent) = ($stashindent, $indent); | |
c2fdda0d AW |
844 | ($prevrawline, $stashrawline) = ($stashrawline, $rawline); |
845 | ||
846 | #warn "ic<$in_comment> ce<$comment_edge> line<$line>\n"; | |
6c72ffaa | 847 | |
d8aaf121 AW |
848 | } elsif ($realcnt == 1) { |
849 | $realcnt--; | |
0a920b5b AW |
850 | } |
851 | ||
852 | #make up the handle for any error we report on this line | |
6c72ffaa AW |
853 | $here = "#$linenr: " if (!$file); |
854 | $here = "#$realline: " if ($file); | |
389834b6 | 855 | $here .= "FILE: $realfile:$realline:" if ($realcnt != 0); |
0a920b5b | 856 | |
c2fdda0d AW |
857 | my $hereline = "$here\n$rawline\n"; |
858 | my $herecurr = "$here\n$rawline\n"; | |
859 | my $hereprev = "$here\n$prevrawline\n$rawline\n"; | |
0a920b5b | 860 | |
6c72ffaa AW |
861 | $prefix = "$filename:$realline: " if ($emacs && $file); |
862 | $prefix = "$filename:$linenr: " if ($emacs && !$file); | |
863 | $cnt_lines++ if ($realcnt != 0); | |
864 | ||
0a920b5b | 865 | #check the patch for a signoff: |
d8aaf121 | 866 | if ($line =~ /^\s*signed-off-by:/i) { |
4a0df2ef AW |
867 | # This is a signoff, if ugly, so do not double report. |
868 | $signoff++; | |
0a920b5b | 869 | if (!($line =~ /^\s*Signed-off-by:/)) { |
de7d4f0e AW |
870 | WARN("Signed-off-by: is the preferred form\n" . |
871 | $herecurr); | |
0a920b5b AW |
872 | } |
873 | if ($line =~ /^\s*signed-off-by:\S/i) { | |
de7d4f0e AW |
874 | WARN("need space after Signed-off-by:\n" . |
875 | $herecurr); | |
0a920b5b AW |
876 | } |
877 | } | |
878 | ||
00df344f | 879 | # Check for wrappage within a valid hunk of the file |
8905a67c | 880 | if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) { |
de7d4f0e | 881 | ERROR("patch seems to be corrupt (line wrapped?)\n" . |
6c72ffaa | 882 | $herecurr) if (!$emitted_corrupt++); |
de7d4f0e AW |
883 | } |
884 | ||
885 | # UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php | |
886 | if (($realfile =~ /^$/ || $line =~ /^\+/) && | |
c2fdda0d | 887 | !($rawline =~ m/^( |
de7d4f0e AW |
888 | [\x09\x0A\x0D\x20-\x7E] # ASCII |
889 | | [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte | |
890 | | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs | |
891 | | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte | |
892 | | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates | |
893 | | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3 | |
894 | | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15 | |
895 | | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16 | |
896 | )*$/x )) { | |
c2fdda0d | 897 | ERROR("Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $herecurr); |
00df344f AW |
898 | } |
899 | ||
900 | #ignore lines being removed | |
901 | if ($line=~/^-/) {next;} | |
0a920b5b | 902 | |
00df344f AW |
903 | # check we are in a valid source file if not then ignore this hunk |
904 | next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/); | |
0a920b5b AW |
905 | |
906 | #trailing whitespace | |
9c0ca6f9 | 907 | if ($line =~ /^\+.*\015/) { |
c2fdda0d | 908 | my $herevet = "$here\n" . cat_vet($rawline) . "\n"; |
9c0ca6f9 AW |
909 | ERROR("DOS line endings\n" . $herevet); |
910 | ||
c2fdda0d AW |
911 | } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) { |
912 | my $herevet = "$here\n" . cat_vet($rawline) . "\n"; | |
de7d4f0e | 913 | ERROR("trailing whitespace\n" . $herevet); |
0a920b5b AW |
914 | } |
915 | #80 column limit | |
c2fdda0d | 916 | if ($line =~ /^\+/ && !($prevrawline=~/\/\*\*/) && $length > 80) { |
de7d4f0e | 917 | WARN("line over 80 characters\n" . $herecurr); |
0a920b5b AW |
918 | } |
919 | ||
8905a67c AW |
920 | # check for adding lines without a newline. |
921 | if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) { | |
922 | WARN("adding a line without newline at end of file\n" . $herecurr); | |
923 | } | |
924 | ||
0a920b5b AW |
925 | # check we are in a valid source file *.[hc] if not then ignore this hunk |
926 | next if ($realfile !~ /\.[hc]$/); | |
927 | ||
928 | # at the beginning of a line any tabs must come first and anything | |
929 | # more than 8 must use tabs. | |
c2fdda0d AW |
930 | if ($rawline =~ /^\+\s* \t\s*\S/ || |
931 | $rawline =~ /^\+\s* \s*/) { | |
932 | my $herevet = "$here\n" . cat_vet($rawline) . "\n"; | |
de7d4f0e | 933 | ERROR("use tabs not spaces\n" . $herevet); |
0a920b5b AW |
934 | } |
935 | ||
c2fdda0d AW |
936 | # check for RCS/CVS revision markers |
937 | if ($rawline =~ /\$(Revision|Log|Id)(?:\$|)/) { | |
938 | WARN("CVS style keyword markers, these will _not_ be updated\n". $herecurr); | |
939 | } | |
22f2a2ef AW |
940 | |
941 | # The rest of our checks refer specifically to C style | |
942 | # only apply those _outside_ comments. Only skip | |
943 | # lines in the middle of comments. | |
944 | next if (!$comment_edge && $in_comment); | |
00df344f | 945 | |
9c0ca6f9 | 946 | # Check for potential 'bare' types |
c2fdda0d AW |
947 | if ($realcnt) { |
948 | # Ignore goto labels. | |
949 | if ($line =~ /$Ident:\*$/) { | |
950 | ||
951 | # Ignore functions being called | |
952 | } elsif ($line =~ /^.\s*$Ident\s*\(/) { | |
953 | ||
8905a67c | 954 | # definitions in global scope can only start with types |
c2fdda0d | 955 | } elsif ($line =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b/) { |
8905a67c AW |
956 | possible($1); |
957 | ||
958 | # declarations always start with types | |
c2fdda0d | 959 | } elsif ($prev_values eq 'N' && $line =~ /^.\s*(?:$Storage\s+)?(?:const\s+)?($Ident)\b(:?\s+$Sparse)?\s*\**\s*$Ident\s*(?:;|=)/) { |
8905a67c | 960 | possible($1); |
c2fdda0d | 961 | } |
8905a67c AW |
962 | |
963 | # any (foo ... *) is a pointer cast, and foo is a type | |
c2fdda0d | 964 | while ($line =~ /\(($Ident)(?:\s+$Sparse)*\s*\*+\s*\)/g) { |
8905a67c AW |
965 | possible($1); |
966 | } | |
967 | ||
968 | # Check for any sort of function declaration. | |
969 | # int foo(something bar, other baz); | |
970 | # void (*store_gdt)(x86_descr_ptr *); | |
c2fdda0d | 971 | if ($prev_values eq 'N' && $line =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/) { |
8905a67c AW |
972 | my ($name_len) = length($1); |
973 | my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, $name_len); | |
974 | my $ctx = join("\n", @ctx); | |
975 | ||
976 | $ctx =~ s/\n.//; | |
977 | substr($ctx, 0, $name_len + 1) = ''; | |
978 | $ctx =~ s/\)[^\)]*$//; | |
979 | for my $arg (split(/\s*,\s*/, $ctx)) { | |
980 | if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/ || $arg =~ /^($Ident)$/) { | |
981 | ||
982 | possible($1); | |
983 | } | |
984 | } | |
9c0ca6f9 | 985 | } |
8905a67c | 986 | |
9c0ca6f9 AW |
987 | } |
988 | ||
653d4876 AW |
989 | # |
990 | # Checks which may be anchored in the context. | |
991 | # | |
00df344f | 992 | |
653d4876 AW |
993 | # Check for switch () and associated case and default |
994 | # statements should be at the same indent. | |
00df344f AW |
995 | if ($line=~/\bswitch\s*\(.*\)/) { |
996 | my $err = ''; | |
997 | my $sep = ''; | |
998 | my @ctx = ctx_block_outer($linenr, $realcnt); | |
999 | shift(@ctx); | |
1000 | for my $ctx (@ctx) { | |
1001 | my ($clen, $cindent) = line_stats($ctx); | |
1002 | if ($ctx =~ /^\+\s*(case\s+|default:)/ && | |
1003 | $indent != $cindent) { | |
1004 | $err .= "$sep$ctx\n"; | |
1005 | $sep = ''; | |
1006 | } else { | |
1007 | $sep = "[...]\n"; | |
1008 | } | |
1009 | } | |
1010 | if ($err ne '') { | |
9c0ca6f9 | 1011 | ERROR("switch and case should be at the same indent\n$hereline$err"); |
de7d4f0e AW |
1012 | } |
1013 | } | |
1014 | ||
1015 | # if/while/etc brace do not go on next line, unless defining a do while loop, | |
1016 | # or if that brace on the next line is for something else | |
1017 | if ($line =~ /\b(?:(if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.#/) { | |
9c0ca6f9 | 1018 | my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0); |
de7d4f0e AW |
1019 | my $ctx_ln = $linenr + $#ctx + 1; |
1020 | my $ctx_cnt = $realcnt - $#ctx - 1; | |
1021 | my $ctx = join("\n", @ctx); | |
1022 | ||
9c0ca6f9 | 1023 | # Skip over any removed lines in the context following statement. |
de7d4f0e AW |
1024 | while ($ctx_cnt > 0 && $lines[$ctx_ln - 1] =~ /^-/) { |
1025 | $ctx_ln++; | |
1026 | $ctx_cnt--; | |
1027 | } | |
1028 | ##warn "line<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>"; | |
1029 | ||
1030 | if ($ctx !~ /{\s*/ && $ctx_cnt > 0 && $lines[$ctx_ln - 1] =~ /^\+\s*{/) { | |
f0a594c1 | 1031 | ERROR("That open brace { should be on the previous line\n" . |
de7d4f0e | 1032 | "$here\n$ctx\n$lines[$ctx_ln - 1]"); |
00df344f | 1033 | } |
9c0ca6f9 AW |
1034 | if ($level == 0 && $ctx =~ /\)\s*\;\s*$/ && defined $lines[$ctx_ln - 1]) { |
1035 | my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]); | |
1036 | if ($nindent > $indent) { | |
1037 | WARN("Trailing semicolon indicates no statements, indent implies otherwise\n" . | |
1038 | "$here\n$ctx\n$lines[$ctx_ln - 1]"); | |
1039 | } | |
1040 | } | |
00df344f AW |
1041 | } |
1042 | ||
6c72ffaa AW |
1043 | # Track the 'values' across context and added lines. |
1044 | my $opline = $line; $opline =~ s/^./ /; | |
1045 | my $curr_values = annotate_values($opline . "\n", $prev_values); | |
1046 | $curr_values = $prev_values . $curr_values; | |
c2fdda0d AW |
1047 | if ($dbg_values) { |
1048 | my $outline = $opline; $outline =~ s/\t/ /g; | |
1049 | warn "--> .$outline\n"; | |
1050 | warn "--> $curr_values\n"; | |
1051 | } | |
6c72ffaa AW |
1052 | $prev_values = substr($curr_values, -1); |
1053 | ||
00df344f AW |
1054 | #ignore lines not being added |
1055 | if ($line=~/^[^\+]/) {next;} | |
1056 | ||
653d4876 AW |
1057 | # TEST: allow direct testing of the type matcher. |
1058 | if ($tst_type && $line =~ /^.$Declare$/) { | |
de7d4f0e | 1059 | ERROR("TEST: is type $Declare\n" . $herecurr); |
653d4876 AW |
1060 | next; |
1061 | } | |
1062 | ||
f0a594c1 AW |
1063 | # check for initialisation to aggregates open brace on the next line |
1064 | if ($prevline =~ /$Declare\s*$Ident\s*=\s*$/ && | |
1065 | $line =~ /^.\s*{/) { | |
1066 | ERROR("That open brace { should be on the previous line\n" . $hereprev); | |
1067 | } | |
1068 | ||
653d4876 AW |
1069 | # |
1070 | # Checks which are anchored on the added line. | |
1071 | # | |
1072 | ||
1073 | # check for malformed paths in #include statements (uses RAW line) | |
1074 | if ($rawline =~ m{^.#\s*include\s+[<"](.*)[">]}) { | |
1075 | my $path = $1; | |
1076 | if ($path =~ m{//}) { | |
de7d4f0e AW |
1077 | ERROR("malformed #include filename\n" . |
1078 | $herecurr); | |
653d4876 | 1079 | } |
653d4876 | 1080 | } |
00df344f | 1081 | |
0a920b5b | 1082 | # no C99 // comments |
00df344f | 1083 | if ($line =~ m{//}) { |
de7d4f0e | 1084 | ERROR("do not use C99 // comments\n" . $herecurr); |
0a920b5b | 1085 | } |
00df344f | 1086 | # Remove C99 comments. |
0a920b5b | 1087 | $line =~ s@//.*@@; |
6c72ffaa | 1088 | $opline =~ s@//.*@@; |
0a920b5b AW |
1089 | |
1090 | #EXPORT_SYMBOL should immediately follow its function closing }. | |
653d4876 AW |
1091 | if (($line =~ /EXPORT_SYMBOL.*\((.*)\)/) || |
1092 | ($line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) { | |
1093 | my $name = $1; | |
0a920b5b AW |
1094 | if (($prevline !~ /^}/) && |
1095 | ($prevline !~ /^\+}/) && | |
653d4876 | 1096 | ($prevline !~ /^ }/) && |
22f2a2ef | 1097 | ($prevline !~ /\b\Q$name\E(?:\s+$Attribute)?\s*(?:;|=)/)) { |
de7d4f0e | 1098 | WARN("EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr); |
0a920b5b AW |
1099 | } |
1100 | } | |
1101 | ||
f0a594c1 AW |
1102 | # check for external initialisers. |
1103 | if ($line =~ /^.$Type\s*$Ident\s*=\s*(0|NULL);/) { | |
1104 | ERROR("do not initialise externals to 0 or NULL\n" . | |
1105 | $herecurr); | |
1106 | } | |
653d4876 | 1107 | # check for static initialisers. |
f0a594c1 | 1108 | if ($line =~ /\s*static\s.*=\s*(0|NULL);/) { |
de7d4f0e AW |
1109 | ERROR("do not initialise statics to 0 or NULL\n" . |
1110 | $herecurr); | |
0a920b5b AW |
1111 | } |
1112 | ||
653d4876 AW |
1113 | # check for new typedefs, only function parameters and sparse annotations |
1114 | # make sense. | |
1115 | if ($line =~ /\btypedef\s/ && | |
9c0ca6f9 | 1116 | $line !~ /\btypedef\s+$Type\s+\(\s*\*?$Ident\s*\)\s*\(/ && |
653d4876 | 1117 | $line !~ /\b__bitwise(?:__|)\b/) { |
de7d4f0e | 1118 | WARN("do not add new typedefs\n" . $herecurr); |
0a920b5b AW |
1119 | } |
1120 | ||
1121 | # * goes on variable not on type | |
d8aaf121 | 1122 | if ($line =~ m{\($NonptrType(\*+)(?:\s+const)?\)}) { |
de7d4f0e AW |
1123 | ERROR("\"(foo$1)\" should be \"(foo $1)\"\n" . |
1124 | $herecurr); | |
d8aaf121 AW |
1125 | |
1126 | } elsif ($line =~ m{\($NonptrType\s+(\*+)(?!\s+const)\s+\)}) { | |
de7d4f0e AW |
1127 | ERROR("\"(foo $1 )\" should be \"(foo $1)\"\n" . |
1128 | $herecurr); | |
d8aaf121 | 1129 | |
9c0ca6f9 | 1130 | } elsif ($line =~ m{$NonptrType(\*+)(?:\s+(?:$Attribute|$Sparse))?\s+[A-Za-z\d_]+}) { |
de7d4f0e AW |
1131 | ERROR("\"foo$1 bar\" should be \"foo $1bar\"\n" . |
1132 | $herecurr); | |
d8aaf121 | 1133 | |
9c0ca6f9 | 1134 | } elsif ($line =~ m{$NonptrType\s+(\*+)(?!\s+(?:$Attribute|$Sparse))\s+[A-Za-z\d_]+}) { |
de7d4f0e AW |
1135 | ERROR("\"foo $1 bar\" should be \"foo $1bar\"\n" . |
1136 | $herecurr); | |
0a920b5b AW |
1137 | } |
1138 | ||
1139 | # # no BUG() or BUG_ON() | |
1140 | # if ($line =~ /\b(BUG|BUG_ON)\b/) { | |
1141 | # print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n"; | |
1142 | # print "$herecurr"; | |
1143 | # $clean = 0; | |
1144 | # } | |
1145 | ||
8905a67c | 1146 | if ($line =~ /\bLINUX_VERSION_CODE\b/) { |
c2fdda0d | 1147 | WARN("LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr); |
8905a67c AW |
1148 | } |
1149 | ||
00df344f AW |
1150 | # printk should use KERN_* levels. Note that follow on printk's on the |
1151 | # same line do not need a level, so we use the current block context | |
1152 | # to try and find and validate the current printk. In summary the current | |
1153 | # printk includes all preceeding printk's which have no newline on the end. | |
1154 | # we assume the first bad printk is the one to report. | |
f0a594c1 | 1155 | if ($line =~ /\bprintk\((?!KERN_)\s*"/) { |
00df344f AW |
1156 | my $ok = 0; |
1157 | for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) { | |
1158 | #print "CHECK<$lines[$ln - 1]\n"; | |
1159 | # we have a preceeding printk if it ends | |
1160 | # with "\n" ignore it, else it is to blame | |
1161 | if ($lines[$ln - 1] =~ m{\bprintk\(}) { | |
1162 | if ($rawlines[$ln - 1] !~ m{\\n"}) { | |
1163 | $ok = 1; | |
1164 | } | |
1165 | last; | |
1166 | } | |
1167 | } | |
1168 | if ($ok == 0) { | |
de7d4f0e | 1169 | WARN("printk() should include KERN_ facility level\n" . $herecurr); |
00df344f | 1170 | } |
0a920b5b AW |
1171 | } |
1172 | ||
653d4876 AW |
1173 | # function brace can't be on same line, except for #defines of do while, |
1174 | # or if closed on same line | |
c2fdda0d | 1175 | if (($line=~/$Type\s*[A-Za-z\d_]+\(.*\).*\s{/) and |
0a920b5b | 1176 | !($line=~/\#define.*do\s{/) and !($line=~/}/)) { |
de7d4f0e | 1177 | ERROR("open brace '{' following function declarations go on the next line\n" . $herecurr); |
0a920b5b | 1178 | } |
653d4876 | 1179 | |
8905a67c AW |
1180 | # open braces for enum, union and struct go on the same line. |
1181 | if ($line =~ /^.\s*{/ && | |
1182 | $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) { | |
1183 | ERROR("open brace '{' following $1 go on the same line\n" . $hereprev); | |
1184 | } | |
1185 | ||
f0a594c1 | 1186 | # check for spaces between functions and their parentheses. |
6c72ffaa | 1187 | while ($line =~ /($Ident)\s+\(/g) { |
c2fdda0d AW |
1188 | my $name = $1; |
1189 | my $ctx = substr($line, 0, $-[1]); | |
1190 | ||
1191 | # Ignore those directives where spaces _are_ permitted. | |
1192 | if ($name =~ /^(?:if|for|while|switch|return|volatile|__volatile__|__attribute__|format|__extension__|Copyright|case)$/) { | |
1193 | ||
1194 | # cpp #define statements have non-optional spaces, ie | |
1195 | # if there is a space between the name and the open | |
1196 | # parenthesis it is simply not a parameter group. | |
1197 | } elsif ($ctx =~ /^.\#\s*define\s*$/) { | |
1198 | ||
1199 | # If this whole things ends with a type its most | |
1200 | # likely a typedef for a function. | |
1201 | } elsif ("$ctx$name" =~ /$Type$/) { | |
1202 | ||
1203 | } else { | |
6c72ffaa AW |
1204 | WARN("no space between function name and open parenthesis '('\n" . $herecurr); |
1205 | } | |
f0a594c1 | 1206 | } |
653d4876 | 1207 | # Check operator spacing. |
0a920b5b | 1208 | if (!($line=~/\#\s*include/)) { |
9c0ca6f9 AW |
1209 | my $ops = qr{ |
1210 | <<=|>>=|<=|>=|==|!=| | |
1211 | \+=|-=|\*=|\/=|%=|\^=|\|=|&=| | |
1212 | =>|->|<<|>>|<|>|=|!|~| | |
c2fdda0d | 1213 | &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|% |
9c0ca6f9 AW |
1214 | }x; |
1215 | my @elements = split(/($ops|;)/, $opline); | |
00df344f | 1216 | my $off = 0; |
6c72ffaa AW |
1217 | |
1218 | my $blank = copy_spacing($opline); | |
1219 | ||
0a920b5b | 1220 | for (my $n = 0; $n < $#elements; $n += 2) { |
4a0df2ef AW |
1221 | $off += length($elements[$n]); |
1222 | ||
1223 | my $a = ''; | |
1224 | $a = 'V' if ($elements[$n] ne ''); | |
1225 | $a = 'W' if ($elements[$n] =~ /\s$/); | |
1226 | $a = 'B' if ($elements[$n] =~ /(\[|\()$/); | |
1227 | $a = 'O' if ($elements[$n] eq ''); | |
1228 | $a = 'E' if ($elements[$n] eq '' && $n == 0); | |
1229 | ||
0a920b5b | 1230 | my $op = $elements[$n + 1]; |
4a0df2ef AW |
1231 | |
1232 | my $c = ''; | |
0a920b5b | 1233 | if (defined $elements[$n + 2]) { |
4a0df2ef AW |
1234 | $c = 'V' if ($elements[$n + 2] ne ''); |
1235 | $c = 'W' if ($elements[$n + 2] =~ /^\s/); | |
1236 | $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/); | |
1237 | $c = 'O' if ($elements[$n + 2] eq ''); | |
22f2a2ef | 1238 | $c = 'E' if ($elements[$n + 2] =~ /\s*\\$/); |
4a0df2ef AW |
1239 | } else { |
1240 | $c = 'E'; | |
0a920b5b AW |
1241 | } |
1242 | ||
00df344f | 1243 | # Pick up the preceeding and succeeding characters. |
de7d4f0e | 1244 | my $ca = substr($opline, 0, $off); |
00df344f | 1245 | my $cc = ''; |
653d4876 | 1246 | if (length($opline) >= ($off + length($elements[$n + 1]))) { |
d8aaf121 | 1247 | $cc = substr($opline, $off + length($elements[$n + 1])); |
00df344f | 1248 | } |
de7d4f0e | 1249 | my $cb = "$ca$;$cc"; |
00df344f | 1250 | |
4a0df2ef AW |
1251 | my $ctx = "${a}x${c}"; |
1252 | ||
1253 | my $at = "(ctx:$ctx)"; | |
1254 | ||
6c72ffaa | 1255 | my $ptr = substr($blank, 0, $off) . "^"; |
de7d4f0e | 1256 | my $hereptr = "$hereline$ptr\n"; |
0a920b5b | 1257 | |
9c0ca6f9 AW |
1258 | # Classify operators into binary, unary, or |
1259 | # definitions (* only) where they have more | |
1260 | # than one mode. | |
6c72ffaa AW |
1261 | my $op_type = substr($curr_values, $off + 1, 1); |
1262 | my $op_left = substr($curr_values, $off, 1); | |
1263 | my $is_unary; | |
1264 | if ($op_type eq 'T') { | |
1265 | $is_unary = 2; | |
1266 | } elsif ($op_left eq 'V') { | |
1267 | $is_unary = 0; | |
1268 | } else { | |
1269 | $is_unary = 1; | |
9c0ca6f9 | 1270 | } |
9c0ca6f9 | 1271 | #if ($op eq '-' || $op eq '&' || $op eq '*') { |
6c72ffaa | 1272 | # print "UNARY: <$op_left$op_type $is_unary $a:$op:$c> <$ca:$op:$cc> <$unary_ctx>\n"; |
9c0ca6f9 | 1273 | #} |
0a920b5b | 1274 | |
d8aaf121 AW |
1275 | # ; should have either the end of line or a space or \ after it |
1276 | if ($op eq ';') { | |
de7d4f0e AW |
1277 | if ($ctx !~ /.x[WEB]/ && $cc !~ /^\\/ && |
1278 | $cc !~ /^;/) { | |
1279 | ERROR("need space after that '$op' $at\n" . $hereptr); | |
d8aaf121 AW |
1280 | } |
1281 | ||
1282 | # // is a comment | |
1283 | } elsif ($op eq '//') { | |
0a920b5b AW |
1284 | |
1285 | # -> should have no spaces | |
1286 | } elsif ($op eq '->') { | |
4a0df2ef | 1287 | if ($ctx =~ /Wx.|.xW/) { |
de7d4f0e | 1288 | ERROR("no spaces around that '$op' $at\n" . $hereptr); |
0a920b5b AW |
1289 | } |
1290 | ||
1291 | # , must have a space on the right. | |
1292 | } elsif ($op eq ',') { | |
d8aaf121 | 1293 | if ($ctx !~ /.xW|.xE/ && $cc !~ /^}/) { |
de7d4f0e | 1294 | ERROR("need space after that '$op' $at\n" . $hereptr); |
0a920b5b AW |
1295 | } |
1296 | ||
9c0ca6f9 AW |
1297 | # '*' as part of a type definition -- reported already. |
1298 | } elsif ($op eq '*' && $is_unary == 2) { | |
1299 | #warn "'*' is part of type\n"; | |
1300 | ||
1301 | # unary operators should have a space before and | |
1302 | # none after. May be left adjacent to another | |
1303 | # unary operator, or a cast | |
1304 | } elsif ($op eq '!' || $op eq '~' || | |
1305 | ($is_unary && ($op eq '*' || $op eq '-' || $op eq '&'))) { | |
1306 | if ($ctx !~ /[WEB]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) { | |
de7d4f0e | 1307 | ERROR("need space before that '$op' $at\n" . $hereptr); |
0a920b5b | 1308 | } |
4a0df2ef | 1309 | if ($ctx =~ /.xW/) { |
de7d4f0e | 1310 | ERROR("no space after that '$op' $at\n" . $hereptr); |
0a920b5b AW |
1311 | } |
1312 | ||
1313 | # unary ++ and unary -- are allowed no space on one side. | |
1314 | } elsif ($op eq '++' or $op eq '--') { | |
d8aaf121 | 1315 | if ($ctx !~ /[WOB]x[^W]/ && $ctx !~ /[^W]x[WOBE]/) { |
de7d4f0e | 1316 | ERROR("need space one side of that '$op' $at\n" . $hereptr); |
0a920b5b | 1317 | } |
d8aaf121 | 1318 | if ($ctx =~ /Wx./ && $cc =~ /^;/) { |
de7d4f0e | 1319 | ERROR("no space before that '$op' $at\n" . $hereptr); |
653d4876 | 1320 | } |
0a920b5b | 1321 | |
0a920b5b | 1322 | # << and >> may either have or not have spaces both sides |
9c0ca6f9 AW |
1323 | } elsif ($op eq '<<' or $op eq '>>' or |
1324 | $op eq '&' or $op eq '^' or $op eq '|' or | |
1325 | $op eq '+' or $op eq '-' or | |
c2fdda0d AW |
1326 | $op eq '*' or $op eq '/' or |
1327 | $op eq '%') | |
0a920b5b | 1328 | { |
9c0ca6f9 | 1329 | if ($ctx !~ /VxV|WxW|VxE|WxE|VxO/) { |
de7d4f0e AW |
1330 | ERROR("need consistent spacing around '$op' $at\n" . |
1331 | $hereptr); | |
0a920b5b AW |
1332 | } |
1333 | ||
1334 | # All the others need spaces both sides. | |
4a0df2ef | 1335 | } elsif ($ctx !~ /[EW]x[WE]/) { |
22f2a2ef AW |
1336 | # Ignore email addresses <foo@bar> |
1337 | if (!($op eq '<' && $cb =~ /$;\S+\@\S+>/) && | |
1338 | !($op eq '>' && $cb =~ /<\S+\@\S+$;/)) { | |
1339 | ERROR("need spaces around that '$op' $at\n" . $hereptr); | |
1340 | } | |
0a920b5b | 1341 | } |
4a0df2ef | 1342 | $off += length($elements[$n + 1]); |
0a920b5b AW |
1343 | } |
1344 | } | |
1345 | ||
f0a594c1 AW |
1346 | # check for multiple assignments |
1347 | if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) { | |
6c72ffaa | 1348 | CHK("multiple assignments should be avoided\n" . $herecurr); |
f0a594c1 AW |
1349 | } |
1350 | ||
22f2a2ef AW |
1351 | ## # check for multiple declarations, allowing for a function declaration |
1352 | ## # continuation. | |
1353 | ## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ && | |
1354 | ## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) { | |
1355 | ## | |
1356 | ## # Remove any bracketed sections to ensure we do not | |
1357 | ## # falsly report the parameters of functions. | |
1358 | ## my $ln = $line; | |
1359 | ## while ($ln =~ s/\([^\(\)]*\)//g) { | |
1360 | ## } | |
1361 | ## if ($ln =~ /,/) { | |
1362 | ## WARN("declaring multiple variables together should be avoided\n" . $herecurr); | |
1363 | ## } | |
1364 | ## } | |
f0a594c1 | 1365 | |
0a920b5b | 1366 | #need space before brace following if, while, etc |
22f2a2ef AW |
1367 | if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) || |
1368 | $line =~ /do{/) { | |
de7d4f0e AW |
1369 | ERROR("need a space before the open brace '{'\n" . $herecurr); |
1370 | } | |
1371 | ||
1372 | # closing brace should have a space following it when it has anything | |
1373 | # on the line | |
1374 | if ($line =~ /}(?!(?:,|;|\)))\S/) { | |
1375 | ERROR("need a space after that close brace '}'\n" . $herecurr); | |
0a920b5b AW |
1376 | } |
1377 | ||
22f2a2ef AW |
1378 | # check spacing on square brackets |
1379 | if ($line =~ /\[\s/ && $line !~ /\[\s*$/) { | |
1380 | ERROR("no space after that open square bracket '['\n" . $herecurr); | |
1381 | } | |
1382 | if ($line =~ /\s\]/) { | |
1383 | ERROR("no space before that close square bracket ']'\n" . $herecurr); | |
1384 | } | |
1385 | ||
1386 | # check spacing on paretheses | |
9c0ca6f9 AW |
1387 | if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ && |
1388 | $line !~ /for\s*\(\s+;/) { | |
22f2a2ef AW |
1389 | ERROR("no space after that open parenthesis '('\n" . $herecurr); |
1390 | } | |
9c0ca6f9 AW |
1391 | if ($line =~ /\s\)/ && $line !~ /^.\s*\)/ && |
1392 | $line !~ /for\s*\(.*;\s+\)/) { | |
22f2a2ef AW |
1393 | ERROR("no space before that close parenthesis ')'\n" . $herecurr); |
1394 | } | |
1395 | ||
0a920b5b | 1396 | #goto labels aren't indented, allow a single space however |
4a0df2ef | 1397 | if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and |
0a920b5b | 1398 | !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) { |
de7d4f0e | 1399 | WARN("labels should not be indented\n" . $herecurr); |
0a920b5b AW |
1400 | } |
1401 | ||
1402 | # Need a space before open parenthesis after if, while etc | |
4a0df2ef | 1403 | if ($line=~/\b(if|while|for|switch)\(/) { |
de7d4f0e | 1404 | ERROR("need a space before the open parenthesis '('\n" . $herecurr); |
0a920b5b AW |
1405 | } |
1406 | ||
1407 | # Check for illegal assignment in if conditional. | |
8905a67c AW |
1408 | if ($line =~ /\bif\s*\(/) { |
1409 | my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0); | |
1410 | ||
1411 | if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/) { | |
c2fdda0d | 1412 | ERROR("do not use assignment in if condition\n" . $herecurr); |
8905a67c AW |
1413 | } |
1414 | ||
1415 | # Find out what is on the end of the line after the | |
1416 | # conditional. | |
1417 | substr($s, 0, length($c)) = ''; | |
1418 | $s =~ s/\n.*//g; | |
1419 | ||
1420 | if (length($c) && $s !~ /^\s*({|;|\/\*.*\*\/)?\s*\\*\s*$/) { | |
1421 | ERROR("trailing statements should be on next line\n" . $herecurr); | |
1422 | } | |
1423 | } | |
1424 | ||
1425 | # if and else should not have general statements after it | |
1426 | if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/ && | |
1427 | $1 !~ /^\s*(?:\sif|{|\\|$)/) { | |
1428 | ERROR("trailing statements should be on next line\n" . $herecurr); | |
0a920b5b AW |
1429 | } |
1430 | ||
1431 | # Check for }<nl>else {, these must be at the same | |
1432 | # indent level to be relevant to each other. | |
1433 | if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and | |
1434 | $previndent == $indent) { | |
de7d4f0e | 1435 | ERROR("else should follow close brace '}'\n" . $hereprev); |
0a920b5b AW |
1436 | } |
1437 | ||
c2fdda0d AW |
1438 | if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and |
1439 | $previndent == $indent) { | |
1440 | my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0); | |
1441 | ||
1442 | # Find out what is on the end of the line after the | |
1443 | # conditional. | |
1444 | substr($s, 0, length($c)) = ''; | |
1445 | $s =~ s/\n.*//g; | |
1446 | ||
1447 | if ($s =~ /^\s*;/) { | |
1448 | ERROR("while should follow close brace '}'\n" . $hereprev); | |
1449 | } | |
1450 | } | |
1451 | ||
0a920b5b AW |
1452 | #studly caps, commented out until figure out how to distinguish between use of existing and adding new |
1453 | # if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) { | |
1454 | # print "No studly caps, use _\n"; | |
1455 | # print "$herecurr"; | |
1456 | # $clean = 0; | |
1457 | # } | |
1458 | ||
1459 | #no spaces allowed after \ in define | |
1460 | if ($line=~/\#define.*\\\s$/) { | |
de7d4f0e | 1461 | WARN("Whitepspace after \\ makes next lines useless\n" . $herecurr); |
0a920b5b AW |
1462 | } |
1463 | ||
653d4876 AW |
1464 | #warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line) |
1465 | if ($tree && $rawline =~ m{^.\#\s*include\s*\<asm\/(.*)\.h\>}) { | |
6c72ffaa AW |
1466 | my $checkfile = "$root/include/linux/$1.h"; |
1467 | if (-f $checkfile && $1 ne 'irq.h') { | |
de7d4f0e AW |
1468 | CHK("Use #include <linux/$1.h> instead of <asm/$1.h>\n" . |
1469 | $herecurr); | |
0a920b5b AW |
1470 | } |
1471 | } | |
1472 | ||
653d4876 AW |
1473 | # multi-statement macros should be enclosed in a do while loop, grab the |
1474 | # first statement and ensure its the whole macro if its not enclosed | |
1475 | # in a known goot container | |
9c0ca6f9 AW |
1476 | if ($prevline =~ /\#define.*\\/ && |
1477 | $prevline !~/(?:do\s+{|\(\{|\{)/ && | |
1478 | $line !~ /(?:do\s+{|\(\{|\{)/ && | |
1479 | $line !~ /^.\s*$Declare\s/) { | |
653d4876 AW |
1480 | # Grab the first statement, if that is the entire macro |
1481 | # its ok. This may start either on the #define line | |
1482 | # or the one below. | |
d8aaf121 AW |
1483 | my $ln = $linenr; |
1484 | my $cnt = $realcnt; | |
f0a594c1 | 1485 | my $off = 0; |
653d4876 | 1486 | |
f0a594c1 AW |
1487 | # If the macro starts on the define line start |
1488 | # grabbing the statement after the identifier | |
1489 | $prevline =~ m{^(.#\s*define\s*$Ident(?:\([^\)]*\))?\s*)(.*)\\\s*$}; | |
1490 | ##print "1<$1> 2<$2>\n"; | |
22f2a2ef | 1491 | if (defined $2 && $2 ne '') { |
f0a594c1 | 1492 | $off = length($1); |
d8aaf121 AW |
1493 | $ln--; |
1494 | $cnt++; | |
8905a67c AW |
1495 | while ($lines[$ln - 1] =~ /^-/) { |
1496 | $ln--; | |
1497 | $cnt++; | |
1498 | } | |
d8aaf121 | 1499 | } |
f0a594c1 | 1500 | my @ctx = ctx_statement($ln, $cnt, $off); |
de7d4f0e AW |
1501 | my $ctx_ln = $ln + $#ctx + 1; |
1502 | my $ctx = join("\n", @ctx); | |
1503 | ||
1504 | # Pull in any empty extension lines. | |
1505 | while ($ctx =~ /\\$/ && | |
1506 | $lines[$ctx_ln - 1] =~ /^.\s*(?:\\)?$/) { | |
1507 | $ctx .= $lines[$ctx_ln - 1]; | |
1508 | $ctx_ln++; | |
1509 | } | |
d8aaf121 AW |
1510 | |
1511 | if ($ctx =~ /\\$/) { | |
1512 | if ($ctx =~ /;/) { | |
de7d4f0e | 1513 | ERROR("Macros with multiple statements should be enclosed in a do - while loop\n" . "$here\n$ctx\n"); |
d8aaf121 | 1514 | } else { |
de7d4f0e | 1515 | ERROR("Macros with complex values should be enclosed in parenthesis\n" . "$here\n$ctx\n"); |
d8aaf121 | 1516 | } |
653d4876 | 1517 | } |
0a920b5b AW |
1518 | } |
1519 | ||
f0a594c1 AW |
1520 | # check for redundant bracing round if etc |
1521 | if ($line =~ /\b(if|while|for|else)\b/) { | |
1522 | # Locate the end of the opening statement. | |
1523 | my @control = ctx_statement($linenr, $realcnt, 0); | |
1524 | my $nr = $linenr + (scalar(@control) - 1); | |
1525 | my $cnt = $realcnt - (scalar(@control) - 1); | |
1526 | ||
1527 | my $off = $realcnt - $cnt; | |
1528 | #print "$off: line<$line>end<" . $lines[$nr - 1] . ">\n"; | |
1529 | ||
1530 | # If this is is a braced statement group check it | |
1531 | if ($lines[$nr - 1] =~ /{\s*$/) { | |
1532 | my ($lvl, @block) = ctx_block_level($nr, $cnt); | |
1533 | ||
8905a67c AW |
1534 | my $stmt = join("\n", @block); |
1535 | # Drop the diff line leader. | |
1536 | $stmt =~ s/\n./\n/g; | |
1537 | # Drop the code outside the block. | |
1538 | $stmt =~ s/(^[^{]*){\s*//; | |
22f2a2ef | 1539 | my $before = $1; |
8905a67c | 1540 | $stmt =~ s/\s*}([^}]*$)//; |
22f2a2ef | 1541 | my $after = $1; |
f0a594c1 AW |
1542 | |
1543 | #print "block<" . join(' ', @block) . "><" . scalar(@block) . ">\n"; | |
1544 | #print "stmt<$stmt>\n\n"; | |
1545 | ||
8905a67c AW |
1546 | # Count the newlines, if there is only one |
1547 | # then the block should not have {}'s. | |
1548 | my @lines = ($stmt =~ /\n/g); | |
c2fdda0d | 1549 | my @statements = ($stmt =~ /;/g); |
8905a67c | 1550 | #print "lines<" . scalar(@lines) . ">\n"; |
c2fdda0d | 1551 | #print "statements<" . scalar(@statements) . ">\n"; |
8905a67c | 1552 | if ($lvl == 0 && scalar(@lines) == 0 && |
c2fdda0d | 1553 | scalar(@statements) < 2 && |
22f2a2ef AW |
1554 | $stmt !~ /{/ && $stmt !~ /\bif\b/ && |
1555 | $before !~ /}/ && $after !~ /{/) { | |
f0a594c1 AW |
1556 | my $herectx = "$here\n" . join("\n", @control, @block[1 .. $#block]) . "\n"; |
1557 | shift(@block); | |
22f2a2ef | 1558 | WARN("braces {} are not necessary for single statement blocks\n" . $herectx); |
f0a594c1 AW |
1559 | } |
1560 | } | |
1561 | } | |
1562 | ||
653d4876 | 1563 | # don't include deprecated include files (uses RAW line) |
4a0df2ef | 1564 | for my $inc (@dep_includes) { |
653d4876 | 1565 | if ($rawline =~ m@\#\s*include\s*\<$inc>@) { |
de7d4f0e | 1566 | ERROR("Don't use <$inc>: see Documentation/feature-removal-schedule.txt\n" . $herecurr); |
0a920b5b AW |
1567 | } |
1568 | } | |
1569 | ||
4a0df2ef AW |
1570 | # don't use deprecated functions |
1571 | for my $func (@dep_functions) { | |
00df344f | 1572 | if ($line =~ /\b$func\b/) { |
de7d4f0e | 1573 | ERROR("Don't use $func(): see Documentation/feature-removal-schedule.txt\n" . $herecurr); |
4a0df2ef AW |
1574 | } |
1575 | } | |
1576 | ||
1577 | # no volatiles please | |
6c72ffaa AW |
1578 | my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b}; |
1579 | if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) { | |
de7d4f0e | 1580 | WARN("Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr); |
4a0df2ef AW |
1581 | } |
1582 | ||
9c0ca6f9 AW |
1583 | # SPIN_LOCK_UNLOCKED & RW_LOCK_UNLOCKED are deprecated |
1584 | if ($line =~ /\b(SPIN_LOCK_UNLOCKED|RW_LOCK_UNLOCKED)/) { | |
1585 | ERROR("Use of $1 is deprecated: see Documentation/spinlocks.txt\n" . $herecurr); | |
1586 | } | |
1587 | ||
00df344f AW |
1588 | # warn about #if 0 |
1589 | if ($line =~ /^.#\s*if\s+0\b/) { | |
de7d4f0e AW |
1590 | CHK("if this code is redundant consider removing it\n" . |
1591 | $herecurr); | |
4a0df2ef AW |
1592 | } |
1593 | ||
f0a594c1 AW |
1594 | # check for needless kfree() checks |
1595 | if ($prevline =~ /\bif\s*\(([^\)]*)\)/) { | |
1596 | my $expr = $1; | |
1597 | if ($line =~ /\bkfree\(\Q$expr\E\);/) { | |
1598 | WARN("kfree(NULL) is safe this check is probabally not required\n" . $hereprev); | |
1599 | } | |
1600 | } | |
1601 | ||
00df344f AW |
1602 | # warn about #ifdefs in C files |
1603 | # if ($line =~ /^.#\s*if(|n)def/ && ($realfile =~ /\.c$/)) { | |
1604 | # print "#ifdef in C files should be avoided\n"; | |
1605 | # print "$herecurr"; | |
1606 | # $clean = 0; | |
1607 | # } | |
1608 | ||
22f2a2ef AW |
1609 | # warn about spacing in #ifdefs |
1610 | if ($line =~ /^.#\s*(ifdef|ifndef|elif)\s\s+/) { | |
1611 | ERROR("exactly one space required after that #$1\n" . $herecurr); | |
1612 | } | |
1613 | ||
4a0df2ef AW |
1614 | # check for spinlock_t definitions without a comment. |
1615 | if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/) { | |
1616 | my $which = $1; | |
1617 | if (!ctx_has_comment($first_line, $linenr)) { | |
de7d4f0e | 1618 | CHK("$1 definition without comment\n" . $herecurr); |
4a0df2ef AW |
1619 | } |
1620 | } | |
1621 | # check for memory barriers without a comment. | |
1622 | if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) { | |
1623 | if (!ctx_has_comment($first_line, $linenr)) { | |
de7d4f0e | 1624 | CHK("memory barrier without comment\n" . $herecurr); |
4a0df2ef AW |
1625 | } |
1626 | } | |
1627 | # check of hardware specific defines | |
22f2a2ef | 1628 | if ($line =~ m@^.#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) { |
de7d4f0e | 1629 | CHK("architecture specific defines should be avoided\n" . $herecurr); |
0a920b5b | 1630 | } |
653d4876 | 1631 | |
de7d4f0e AW |
1632 | # check the location of the inline attribute, that it is between |
1633 | # storage class and type. | |
9c0ca6f9 AW |
1634 | if ($line =~ /\b$Type\s+$Inline\b/ || |
1635 | $line =~ /\b$Inline\s+$Storage\b/) { | |
de7d4f0e AW |
1636 | ERROR("inline keyword should sit between storage class and type\n" . $herecurr); |
1637 | } | |
1638 | ||
8905a67c AW |
1639 | # Check for __inline__ and __inline, prefer inline |
1640 | if ($line =~ /\b(__inline__|__inline)\b/) { | |
1641 | WARN("plain inline is preferred over $1\n" . $herecurr); | |
1642 | } | |
1643 | ||
de7d4f0e AW |
1644 | # check for new externs in .c files. |
1645 | if ($line =~ /^.\s*extern\s/ && ($realfile =~ /\.c$/)) { | |
1646 | WARN("externs should be avoided in .c files\n" . $herecurr); | |
1647 | } | |
1648 | ||
1649 | # checks for new __setup's | |
1650 | if ($rawline =~ /\b__setup\("([^"]*)"/) { | |
1651 | my $name = $1; | |
1652 | ||
1653 | if (!grep(/$name/, @setup_docs)) { | |
1654 | CHK("__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr); | |
1655 | } | |
653d4876 | 1656 | } |
9c0ca6f9 AW |
1657 | |
1658 | # check for pointless casting of kmalloc return | |
1659 | if ($line =~ /\*\s*\)\s*k[czm]alloc\b/) { | |
1660 | WARN("unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr); | |
1661 | } | |
0a920b5b AW |
1662 | } |
1663 | ||
8905a67c AW |
1664 | # In mailback mode only produce a report in the negative, for |
1665 | # things that appear to be patches. | |
1666 | if ($mailback && ($clean == 1 || !$is_patch)) { | |
1667 | exit(0); | |
1668 | } | |
1669 | ||
1670 | # This is not a patch, and we are are in 'no-patch' mode so | |
1671 | # just keep quiet. | |
1672 | if (!$chk_patch && !$is_patch) { | |
1673 | exit(0); | |
1674 | } | |
1675 | ||
1676 | if (!$is_patch) { | |
de7d4f0e | 1677 | ERROR("Does not appear to be a unified-diff format patch\n"); |
0a920b5b AW |
1678 | } |
1679 | if ($is_patch && $chk_signoff && $signoff == 0) { | |
de7d4f0e | 1680 | ERROR("Missing Signed-off-by: line(s)\n"); |
0a920b5b AW |
1681 | } |
1682 | ||
8905a67c AW |
1683 | print report_dump(); |
1684 | if ($summary) { | |
1685 | print "total: $cnt_error errors, $cnt_warn warnings, " . | |
1686 | (($check)? "$cnt_chk checks, " : "") . | |
1687 | "$cnt_lines lines checked\n"; | |
1688 | print "\n" if ($quiet == 0); | |
f0a594c1 | 1689 | } |
8905a67c | 1690 | |
0a920b5b | 1691 | if ($clean == 1 && $quiet == 0) { |
c2fdda0d | 1692 | print "$vname has no obvious style problems and is ready for submission.\n" |
0a920b5b AW |
1693 | } |
1694 | if ($clean == 0 && $quiet == 0) { | |
c2fdda0d | 1695 | print "$vname has style problems, please review. If any of these errors\n"; |
0a920b5b AW |
1696 | print "are false positives report them to the maintainer, see\n"; |
1697 | print "CHECKPATCH in MAINTAINERS.\n"; | |
1698 | } | |
1699 | return $clean; | |
1700 | } |