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 | |
9c0ca6f9 | 12 | my $V = '0.10'; |
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; |
0a920b5b AW |
21 | GetOptions( |
22 | 'q|quiet' => \$quiet, | |
23 | 'tree!' => \$tree, | |
24 | 'signoff!' => \$chk_signoff, | |
25 | 'patch!' => \$chk_patch, | |
653d4876 | 26 | 'test-type!' => \$tst_type, |
0a920b5b AW |
27 | ) or exit; |
28 | ||
29 | my $exit = 0; | |
30 | ||
31 | if ($#ARGV < 0) { | |
00df344f | 32 | print "usage: $P [options] patchfile\n"; |
0a920b5b AW |
33 | print "version: $V\n"; |
34 | print "options: -q => quiet\n"; | |
35 | print " --no-tree => run without a kernel tree\n"; | |
36 | exit(1); | |
37 | } | |
38 | ||
39 | if ($tree && !top_of_kernel_tree()) { | |
40 | print "Must be run from the top-level dir. of a kernel tree\n"; | |
41 | exit(2); | |
42 | } | |
43 | ||
4a0df2ef AW |
44 | my @dep_includes = (); |
45 | my @dep_functions = (); | |
0a920b5b AW |
46 | my $removal = 'Documentation/feature-removal-schedule.txt'; |
47 | if ($tree && -f $removal) { | |
48 | open(REMOVE, "<$removal") || die "$P: $removal: open failed - $!\n"; | |
49 | while (<REMOVE>) { | |
f0a594c1 AW |
50 | if (/^Check:\s+(.*\S)/) { |
51 | for my $entry (split(/[, ]+/, $1)) { | |
52 | if ($entry =~ m@include/(.*)@) { | |
4a0df2ef | 53 | push(@dep_includes, $1); |
4a0df2ef | 54 | |
f0a594c1 AW |
55 | } elsif ($entry !~ m@/@) { |
56 | push(@dep_functions, $entry); | |
57 | } | |
4a0df2ef | 58 | } |
0a920b5b AW |
59 | } |
60 | } | |
61 | } | |
62 | ||
00df344f | 63 | my @rawlines = (); |
0a920b5b AW |
64 | while (<>) { |
65 | chomp; | |
00df344f | 66 | push(@rawlines, $_); |
0a920b5b | 67 | if (eof(ARGV)) { |
00df344f | 68 | if (!process($ARGV, @rawlines)) { |
0a920b5b AW |
69 | $exit = 1; |
70 | } | |
00df344f | 71 | @rawlines = (); |
0a920b5b AW |
72 | } |
73 | } | |
74 | ||
75 | exit($exit); | |
76 | ||
77 | sub top_of_kernel_tree { | |
78 | if ((-f "COPYING") && (-f "CREDITS") && (-f "Kbuild") && | |
79 | (-f "MAINTAINERS") && (-f "Makefile") && (-f "README") && | |
80 | (-d "Documentation") && (-d "arch") && (-d "include") && | |
81 | (-d "drivers") && (-d "fs") && (-d "init") && (-d "ipc") && | |
82 | (-d "kernel") && (-d "lib") && (-d "scripts")) { | |
83 | return 1; | |
84 | } | |
85 | return 0; | |
86 | } | |
87 | ||
88 | sub expand_tabs { | |
89 | my ($str) = @_; | |
90 | ||
91 | my $res = ''; | |
92 | my $n = 0; | |
93 | for my $c (split(//, $str)) { | |
94 | if ($c eq "\t") { | |
95 | $res .= ' '; | |
96 | $n++; | |
97 | for (; ($n % 8) != 0; $n++) { | |
98 | $res .= ' '; | |
99 | } | |
100 | next; | |
101 | } | |
102 | $res .= $c; | |
103 | $n++; | |
104 | } | |
105 | ||
106 | return $res; | |
107 | } | |
108 | ||
4a0df2ef AW |
109 | sub line_stats { |
110 | my ($line) = @_; | |
111 | ||
112 | # Drop the diff line leader and expand tabs | |
113 | $line =~ s/^.//; | |
114 | $line = expand_tabs($line); | |
115 | ||
116 | # Pick the indent from the front of the line. | |
117 | my ($white) = ($line =~ /^(\s*)/); | |
118 | ||
119 | return (length($line), length($white)); | |
120 | } | |
121 | ||
00df344f AW |
122 | sub sanitise_line { |
123 | my ($line) = @_; | |
124 | ||
125 | my $res = ''; | |
126 | my $l = ''; | |
127 | ||
128 | my $quote = ''; | |
129 | ||
130 | foreach my $c (split(//, $line)) { | |
131 | if ($l ne "\\" && ($c eq "'" || $c eq '"')) { | |
132 | if ($quote eq '') { | |
133 | $quote = $c; | |
134 | $res .= $c; | |
135 | $l = $c; | |
136 | next; | |
137 | } elsif ($quote eq $c) { | |
138 | $quote = ''; | |
139 | } | |
140 | } | |
141 | if ($quote && $c ne "\t") { | |
142 | $res .= "X"; | |
143 | } else { | |
144 | $res .= $c; | |
145 | } | |
146 | ||
147 | $l = $c; | |
148 | } | |
149 | ||
150 | return $res; | |
151 | } | |
152 | ||
4a0df2ef | 153 | sub ctx_block_get { |
f0a594c1 | 154 | my ($linenr, $remain, $outer, $open, $close, $off) = @_; |
4a0df2ef AW |
155 | my $line; |
156 | my $start = $linenr - 1; | |
4a0df2ef AW |
157 | my $blk = ''; |
158 | my @o; | |
159 | my @c; | |
160 | my @res = (); | |
161 | ||
f0a594c1 | 162 | my $level = 0; |
00df344f AW |
163 | for ($line = $start; $remain > 0; $line++) { |
164 | next if ($rawlines[$line] =~ /^-/); | |
165 | $remain--; | |
166 | ||
167 | $blk .= $rawlines[$line]; | |
f0a594c1 AW |
168 | foreach my $c (split(//, $rawlines[$line])) { |
169 | ##print "C<$c>L<$level><$open$close>O<$off>\n"; | |
170 | if ($off > 0) { | |
171 | $off--; | |
172 | next; | |
173 | } | |
4a0df2ef | 174 | |
f0a594c1 AW |
175 | if ($c eq $close && $level > 0) { |
176 | $level--; | |
177 | last if ($level == 0); | |
178 | } elsif ($c eq $open) { | |
179 | $level++; | |
180 | } | |
181 | } | |
4a0df2ef | 182 | |
f0a594c1 | 183 | if (!$outer || $level <= 1) { |
00df344f | 184 | push(@res, $rawlines[$line]); |
4a0df2ef AW |
185 | } |
186 | ||
f0a594c1 | 187 | last if ($level == 0); |
4a0df2ef AW |
188 | } |
189 | ||
f0a594c1 | 190 | return ($level, @res); |
4a0df2ef AW |
191 | } |
192 | sub ctx_block_outer { | |
193 | my ($linenr, $remain) = @_; | |
194 | ||
f0a594c1 AW |
195 | my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0); |
196 | return @r; | |
4a0df2ef AW |
197 | } |
198 | sub ctx_block { | |
199 | my ($linenr, $remain) = @_; | |
200 | ||
f0a594c1 AW |
201 | my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0); |
202 | return @r; | |
653d4876 AW |
203 | } |
204 | sub ctx_statement { | |
f0a594c1 AW |
205 | my ($linenr, $remain, $off) = @_; |
206 | ||
207 | my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off); | |
208 | return @r; | |
209 | } | |
210 | sub ctx_block_level { | |
653d4876 AW |
211 | my ($linenr, $remain) = @_; |
212 | ||
f0a594c1 | 213 | return ctx_block_get($linenr, $remain, 0, '{', '}', 0); |
4a0df2ef | 214 | } |
9c0ca6f9 AW |
215 | sub ctx_statement_level { |
216 | my ($linenr, $remain, $off) = @_; | |
217 | ||
218 | return ctx_block_get($linenr, $remain, 0, '(', ')', $off); | |
219 | } | |
4a0df2ef AW |
220 | |
221 | sub ctx_locate_comment { | |
222 | my ($first_line, $end_line) = @_; | |
223 | ||
224 | # Catch a comment on the end of the line itself. | |
00df344f | 225 | my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*$@); |
4a0df2ef AW |
226 | return $current_comment if (defined $current_comment); |
227 | ||
228 | # Look through the context and try and figure out if there is a | |
229 | # comment. | |
230 | my $in_comment = 0; | |
231 | $current_comment = ''; | |
232 | for (my $linenr = $first_line; $linenr < $end_line; $linenr++) { | |
00df344f AW |
233 | my $line = $rawlines[$linenr - 1]; |
234 | #warn " $line\n"; | |
4a0df2ef AW |
235 | if ($linenr == $first_line and $line =~ m@^.\s*\*@) { |
236 | $in_comment = 1; | |
237 | } | |
238 | if ($line =~ m@/\*@) { | |
239 | $in_comment = 1; | |
240 | } | |
241 | if (!$in_comment && $current_comment ne '') { | |
242 | $current_comment = ''; | |
243 | } | |
244 | $current_comment .= $line . "\n" if ($in_comment); | |
245 | if ($line =~ m@\*/@) { | |
246 | $in_comment = 0; | |
247 | } | |
248 | } | |
249 | ||
250 | chomp($current_comment); | |
251 | return($current_comment); | |
252 | } | |
253 | sub ctx_has_comment { | |
254 | my ($first_line, $end_line) = @_; | |
255 | my $cmt = ctx_locate_comment($first_line, $end_line); | |
256 | ||
00df344f | 257 | ##print "LINE: $rawlines[$end_line - 1 ]\n"; |
4a0df2ef AW |
258 | ##print "CMMT: $cmt\n"; |
259 | ||
260 | return ($cmt ne ''); | |
261 | } | |
262 | ||
9c0ca6f9 AW |
263 | sub ctx_expr_before { |
264 | my ($line) = @_; | |
265 | ||
266 | ##print "CHECK<$line>\n"; | |
267 | ||
268 | my $pos = length($line) - 1; | |
269 | my $count = 0; | |
270 | my $c; | |
271 | ||
272 | for (; $pos >= 0; $pos--) { | |
273 | $c = substr($line, $pos, 1); | |
274 | ##print "CHECK: c<$c> count<$count>\n"; | |
275 | if ($c eq ')') { | |
276 | $count++; | |
277 | } elsif ($c eq '(') { | |
278 | last if (--$count == 0); | |
279 | } | |
280 | } | |
281 | ||
282 | ##print "CHECK: result<" . substr($line, 0, $pos) . ">\n"; | |
283 | ||
284 | return substr($line, 0, $pos); | |
285 | } | |
286 | ||
0a920b5b AW |
287 | sub cat_vet { |
288 | my ($vet) = @_; | |
9c0ca6f9 | 289 | my ($res, $coded); |
0a920b5b | 290 | |
9c0ca6f9 AW |
291 | $res = ''; |
292 | while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]])/g) { | |
293 | $coded = sprintf("^%c", unpack('C', $2) + 64); | |
294 | $res .= $1 . $coded; | |
295 | } | |
296 | $res =~ s/$/\$/; | |
0a920b5b | 297 | |
9c0ca6f9 | 298 | return $res; |
0a920b5b AW |
299 | } |
300 | ||
f0a594c1 AW |
301 | my @report = (); |
302 | sub report { | |
303 | push(@report, $_[0]); | |
304 | } | |
305 | sub report_dump { | |
306 | @report; | |
307 | } | |
de7d4f0e | 308 | sub ERROR { |
f0a594c1 | 309 | report("ERROR: $_[0]\n"); |
de7d4f0e AW |
310 | our $clean = 0; |
311 | } | |
312 | sub WARN { | |
f0a594c1 | 313 | report("WARNING: $_[0]\n"); |
de7d4f0e AW |
314 | our $clean = 0; |
315 | } | |
316 | sub CHK { | |
f0a594c1 | 317 | report("CHECK: $_[0]\n"); |
de7d4f0e AW |
318 | our $clean = 0; |
319 | } | |
320 | ||
0a920b5b AW |
321 | sub process { |
322 | my $filename = shift; | |
323 | my @lines = @_; | |
324 | ||
325 | my $linenr=0; | |
326 | my $prevline=""; | |
327 | my $stashline=""; | |
328 | ||
4a0df2ef | 329 | my $length; |
0a920b5b AW |
330 | my $indent; |
331 | my $previndent=0; | |
332 | my $stashindent=0; | |
333 | ||
de7d4f0e | 334 | our $clean = 1; |
0a920b5b AW |
335 | my $signoff = 0; |
336 | my $is_patch = 0; | |
337 | ||
338 | # Trace the real file/line as we go. | |
339 | my $realfile = ''; | |
340 | my $realline = 0; | |
341 | my $realcnt = 0; | |
342 | my $here = ''; | |
343 | my $in_comment = 0; | |
344 | my $first_line = 0; | |
345 | ||
d8aaf121 | 346 | my $Ident = qr{[A-Za-z\d_]+}; |
9c0ca6f9 AW |
347 | my $Storage = qr{extern|static|asmlinkage}; |
348 | my $Sparse = qr{ | |
349 | __user| | |
350 | __kernel| | |
351 | __force| | |
352 | __iomem| | |
353 | __must_check| | |
354 | __init_refok| | |
355 | fastcall | |
356 | }x; | |
357 | my $Inline = qr{inline|__always_inline|noinline}; | |
d8aaf121 AW |
358 | my $NonptrType = qr{ |
359 | \b | |
360 | (?:const\s+)? | |
361 | (?:unsigned\s+)? | |
362 | (?: | |
363 | void| | |
364 | char| | |
365 | short| | |
366 | int| | |
367 | long| | |
368 | unsigned| | |
369 | float| | |
370 | double| | |
22f2a2ef | 371 | bool| |
d8aaf121 AW |
372 | long\s+int| |
373 | long\s+long| | |
374 | long\s+long\s+int| | |
de7d4f0e AW |
375 | u8|u16|u32|u64| |
376 | s8|s16|s32|s64| | |
d8aaf121 AW |
377 | struct\s+$Ident| |
378 | union\s+$Ident| | |
de7d4f0e | 379 | enum\s+$Ident| |
d8aaf121 AW |
380 | ${Ident}_t |
381 | ) | |
382 | (?:\s+$Sparse)* | |
383 | \b | |
384 | }x; | |
385 | my $Type = qr{ | |
386 | \b$NonptrType\b | |
22f2a2ef AW |
387 | (?:\s*\*+\s*const|\s*\*+|(?:\s*\[\s*\])+)? |
388 | (?:\s+$Sparse)* | |
d8aaf121 AW |
389 | }x; |
390 | my $Declare = qr{(?:$Storage\s+)?$Type}; | |
9c0ca6f9 AW |
391 | my $Attribute = qr{ |
392 | const| | |
393 | __read_mostly| | |
394 | __(?:mem|cpu|dev|)(?:initdata|init) | |
395 | }x; | |
f0a594c1 AW |
396 | my $Member = qr{->$Ident|\.$Ident|\[[^]]*\]}; |
397 | my $Lval = qr{$Ident(?:$Member)*}; | |
653d4876 | 398 | |
9c0ca6f9 AW |
399 | # Possible bare types. |
400 | my @bare = (); | |
401 | my $Bare = $NonptrType; | |
402 | ||
de7d4f0e AW |
403 | # Pre-scan the patch looking for any __setup documentation. |
404 | my @setup_docs = (); | |
405 | my $setup_docs = 0; | |
406 | foreach my $line (@lines) { | |
407 | if ($line=~/^\+\+\+\s+(\S+)/) { | |
408 | $setup_docs = 0; | |
409 | if ($1 =~ m@Documentation/kernel-parameters.txt$@) { | |
410 | $setup_docs = 1; | |
411 | } | |
412 | next; | |
413 | } | |
414 | ||
415 | if ($setup_docs && $line =~ /^\+/) { | |
416 | push(@setup_docs, $line); | |
417 | } | |
418 | } | |
419 | ||
0a920b5b AW |
420 | foreach my $line (@lines) { |
421 | $linenr++; | |
422 | ||
653d4876 AW |
423 | my $rawline = $line; |
424 | ||
0a920b5b AW |
425 | #extract the filename as it passes |
426 | if ($line=~/^\+\+\+\s+(\S+)/) { | |
427 | $realfile=$1; | |
00df344f | 428 | $realfile =~ s@^[^/]*/@@; |
0a920b5b AW |
429 | $in_comment = 0; |
430 | next; | |
431 | } | |
432 | #extract the line range in the file after the patch is applied | |
433 | if ($line=~/^\@\@ -\d+,\d+ \+(\d+)(,(\d+))? \@\@/) { | |
434 | $is_patch = 1; | |
4a0df2ef | 435 | $first_line = $linenr + 1; |
0a920b5b AW |
436 | $in_comment = 0; |
437 | $realline=$1-1; | |
438 | if (defined $2) { | |
439 | $realcnt=$3+1; | |
440 | } else { | |
441 | $realcnt=1+1; | |
442 | } | |
443 | next; | |
444 | } | |
445 | ||
4a0df2ef AW |
446 | # track the line number as we move through the hunk, note that |
447 | # new versions of GNU diff omit the leading space on completely | |
448 | # blank context lines so we need to count that too. | |
449 | if ($line =~ /^( |\+|$)/) { | |
0a920b5b | 450 | $realline++; |
d8aaf121 | 451 | $realcnt-- if ($realcnt != 0); |
0a920b5b AW |
452 | |
453 | # track any sort of multi-line comment. Obviously if | |
454 | # the added text or context do not include the whole | |
455 | # comment we will not see it. Such is life. | |
456 | # | |
457 | # Guestimate if this is a continuing comment. If this | |
458 | # is the start of a diff block and this line starts | |
459 | # ' *' then it is very likely a comment. | |
4a0df2ef | 460 | if ($linenr == $first_line and $line =~ m@^.\s*\*@) { |
0a920b5b AW |
461 | $in_comment = 1; |
462 | } | |
463 | if ($line =~ m@/\*@) { | |
464 | $in_comment = 1; | |
465 | } | |
466 | if ($line =~ m@\*/@) { | |
467 | $in_comment = 0; | |
468 | } | |
469 | ||
4a0df2ef AW |
470 | # Measure the line length and indent. |
471 | ($length, $indent) = line_stats($line); | |
0a920b5b AW |
472 | |
473 | # Track the previous line. | |
474 | ($prevline, $stashline) = ($stashline, $line); | |
475 | ($previndent, $stashindent) = ($stashindent, $indent); | |
d8aaf121 AW |
476 | } elsif ($realcnt == 1) { |
477 | $realcnt--; | |
0a920b5b AW |
478 | } |
479 | ||
480 | #make up the handle for any error we report on this line | |
389834b6 RD |
481 | $here = "#$linenr: "; |
482 | $here .= "FILE: $realfile:$realline:" if ($realcnt != 0); | |
0a920b5b | 483 | |
00df344f | 484 | my $hereline = "$here\n$line\n"; |
de7d4f0e AW |
485 | my $herecurr = "$here\n$line\n"; |
486 | my $hereprev = "$here\n$prevline\n$line\n"; | |
0a920b5b AW |
487 | |
488 | #check the patch for a signoff: | |
d8aaf121 | 489 | if ($line =~ /^\s*signed-off-by:/i) { |
4a0df2ef AW |
490 | # This is a signoff, if ugly, so do not double report. |
491 | $signoff++; | |
0a920b5b | 492 | if (!($line =~ /^\s*Signed-off-by:/)) { |
de7d4f0e AW |
493 | WARN("Signed-off-by: is the preferred form\n" . |
494 | $herecurr); | |
0a920b5b AW |
495 | } |
496 | if ($line =~ /^\s*signed-off-by:\S/i) { | |
de7d4f0e AW |
497 | WARN("need space after Signed-off-by:\n" . |
498 | $herecurr); | |
0a920b5b AW |
499 | } |
500 | } | |
501 | ||
00df344f AW |
502 | # Check for wrappage within a valid hunk of the file |
503 | if ($realcnt != 0 && $line !~ m{^(?:\+|-| |$)}) { | |
de7d4f0e AW |
504 | ERROR("patch seems to be corrupt (line wrapped?)\n" . |
505 | $herecurr); | |
506 | } | |
507 | ||
508 | # UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php | |
509 | if (($realfile =~ /^$/ || $line =~ /^\+/) && | |
510 | !($line =~ m/^( | |
511 | [\x09\x0A\x0D\x20-\x7E] # ASCII | |
512 | | [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte | |
513 | | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs | |
514 | | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte | |
515 | | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates | |
516 | | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3 | |
517 | | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15 | |
518 | | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16 | |
519 | )*$/x )) { | |
520 | ERROR("Invalid UTF-8\n" . $herecurr); | |
00df344f AW |
521 | } |
522 | ||
523 | #ignore lines being removed | |
524 | if ($line=~/^-/) {next;} | |
0a920b5b | 525 | |
00df344f AW |
526 | # check we are in a valid source file if not then ignore this hunk |
527 | next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/); | |
0a920b5b AW |
528 | |
529 | #trailing whitespace | |
9c0ca6f9 AW |
530 | if ($line =~ /^\+.*\015/) { |
531 | my $herevet = "$here\n" . cat_vet($line) . "\n"; | |
532 | ERROR("DOS line endings\n" . $herevet); | |
533 | ||
534 | } elsif ($line =~ /^\+.*\S\s+$/ || $line =~ /^\+\s+$/) { | |
de7d4f0e AW |
535 | my $herevet = "$here\n" . cat_vet($line) . "\n"; |
536 | ERROR("trailing whitespace\n" . $herevet); | |
0a920b5b AW |
537 | } |
538 | #80 column limit | |
00df344f | 539 | if ($line =~ /^\+/ && !($prevline=~/\/\*\*/) && $length > 80) { |
de7d4f0e | 540 | WARN("line over 80 characters\n" . $herecurr); |
0a920b5b AW |
541 | } |
542 | ||
543 | # check we are in a valid source file *.[hc] if not then ignore this hunk | |
544 | next if ($realfile !~ /\.[hc]$/); | |
545 | ||
546 | # at the beginning of a line any tabs must come first and anything | |
547 | # more than 8 must use tabs. | |
548 | if ($line=~/^\+\s* \t\s*\S/ or $line=~/^\+\s* \s*/) { | |
de7d4f0e AW |
549 | my $herevet = "$here\n" . cat_vet($line) . "\n"; |
550 | ERROR("use tabs not spaces\n" . $herevet); | |
0a920b5b AW |
551 | } |
552 | ||
653d4876 | 553 | # Remove comments from the line before processing. |
22f2a2ef AW |
554 | my $comment_edge = ($line =~ s@/\*.*\*/@@g) + |
555 | ($line =~ s@/\*.*@@) + | |
556 | ($line =~ s@^(.).*\*/@$1@); | |
557 | ||
558 | # The rest of our checks refer specifically to C style | |
559 | # only apply those _outside_ comments. Only skip | |
560 | # lines in the middle of comments. | |
561 | next if (!$comment_edge && $in_comment); | |
00df344f | 562 | |
653d4876 AW |
563 | # Standardise the strings and chars within the input to simplify matching. |
564 | $line = sanitise_line($line); | |
565 | ||
9c0ca6f9 AW |
566 | # Check for potential 'bare' types |
567 | if ($realcnt && | |
568 | $line !~ /^.\s*(?:$Storage\s+)?(?:$Inline\s+)?$Type\b/ && | |
569 | $line !~ /$Ident:\s*$/ && | |
570 | $line !~ /^.\s*$Ident\s*\(/ && | |
571 | ($line =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?($Ident)\b/ || | |
572 | $line =~ /^.\s*(?:$Storage\s+)?($Ident)\b\s*\**\s*$Ident\s*(?:;|=)/)) { | |
573 | my $possible = $1; | |
574 | if ($possible !~ /^(?:$Storage|$Type|DEFINE_\S+)$/ && | |
575 | $possible ne 'goto' && $possible ne 'return' && | |
576 | $possible ne 'struct' && $possible ne 'enum' && | |
577 | $possible ne 'case' && $possible ne 'else' && | |
578 | $possible ne 'typedef') { | |
579 | #print "POSSIBLE<$possible>\n"; | |
580 | push(@bare, $possible); | |
581 | my $bare = join("|", @bare); | |
582 | $Bare = qr{ | |
583 | \b(?:$bare)\b | |
584 | (?:\s*\*+\s*const|\s*\*+|(?:\s*\[\s*\])+)? | |
585 | (?:\s+$Sparse)* | |
586 | }x; | |
587 | } | |
588 | } | |
589 | ||
653d4876 AW |
590 | # |
591 | # Checks which may be anchored in the context. | |
592 | # | |
00df344f | 593 | |
653d4876 AW |
594 | # Check for switch () and associated case and default |
595 | # statements should be at the same indent. | |
00df344f AW |
596 | if ($line=~/\bswitch\s*\(.*\)/) { |
597 | my $err = ''; | |
598 | my $sep = ''; | |
599 | my @ctx = ctx_block_outer($linenr, $realcnt); | |
600 | shift(@ctx); | |
601 | for my $ctx (@ctx) { | |
602 | my ($clen, $cindent) = line_stats($ctx); | |
603 | if ($ctx =~ /^\+\s*(case\s+|default:)/ && | |
604 | $indent != $cindent) { | |
605 | $err .= "$sep$ctx\n"; | |
606 | $sep = ''; | |
607 | } else { | |
608 | $sep = "[...]\n"; | |
609 | } | |
610 | } | |
611 | if ($err ne '') { | |
9c0ca6f9 | 612 | ERROR("switch and case should be at the same indent\n$hereline$err"); |
de7d4f0e AW |
613 | } |
614 | } | |
615 | ||
616 | # if/while/etc brace do not go on next line, unless defining a do while loop, | |
617 | # or if that brace on the next line is for something else | |
618 | if ($line =~ /\b(?:(if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.#/) { | |
9c0ca6f9 | 619 | my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0); |
de7d4f0e AW |
620 | my $ctx_ln = $linenr + $#ctx + 1; |
621 | my $ctx_cnt = $realcnt - $#ctx - 1; | |
622 | my $ctx = join("\n", @ctx); | |
623 | ||
9c0ca6f9 | 624 | # Skip over any removed lines in the context following statement. |
de7d4f0e AW |
625 | while ($ctx_cnt > 0 && $lines[$ctx_ln - 1] =~ /^-/) { |
626 | $ctx_ln++; | |
627 | $ctx_cnt--; | |
628 | } | |
629 | ##warn "line<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>"; | |
630 | ||
631 | if ($ctx !~ /{\s*/ && $ctx_cnt > 0 && $lines[$ctx_ln - 1] =~ /^\+\s*{/) { | |
f0a594c1 | 632 | ERROR("That open brace { should be on the previous line\n" . |
de7d4f0e | 633 | "$here\n$ctx\n$lines[$ctx_ln - 1]"); |
00df344f | 634 | } |
9c0ca6f9 AW |
635 | if ($level == 0 && $ctx =~ /\)\s*\;\s*$/ && defined $lines[$ctx_ln - 1]) { |
636 | my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]); | |
637 | if ($nindent > $indent) { | |
638 | WARN("Trailing semicolon indicates no statements, indent implies otherwise\n" . | |
639 | "$here\n$ctx\n$lines[$ctx_ln - 1]"); | |
640 | } | |
641 | } | |
00df344f AW |
642 | } |
643 | ||
644 | #ignore lines not being added | |
645 | if ($line=~/^[^\+]/) {next;} | |
646 | ||
653d4876 AW |
647 | # TEST: allow direct testing of the type matcher. |
648 | if ($tst_type && $line =~ /^.$Declare$/) { | |
de7d4f0e | 649 | ERROR("TEST: is type $Declare\n" . $herecurr); |
653d4876 AW |
650 | next; |
651 | } | |
652 | ||
f0a594c1 AW |
653 | # check for initialisation to aggregates open brace on the next line |
654 | if ($prevline =~ /$Declare\s*$Ident\s*=\s*$/ && | |
655 | $line =~ /^.\s*{/) { | |
656 | ERROR("That open brace { should be on the previous line\n" . $hereprev); | |
657 | } | |
658 | ||
653d4876 AW |
659 | # |
660 | # Checks which are anchored on the added line. | |
661 | # | |
662 | ||
663 | # check for malformed paths in #include statements (uses RAW line) | |
664 | if ($rawline =~ m{^.#\s*include\s+[<"](.*)[">]}) { | |
665 | my $path = $1; | |
666 | if ($path =~ m{//}) { | |
de7d4f0e AW |
667 | ERROR("malformed #include filename\n" . |
668 | $herecurr); | |
653d4876 AW |
669 | } |
670 | # Sanitise this special form of string. | |
671 | $path = 'X' x length($path); | |
672 | $line =~ s{\<.*\>}{<$path>}; | |
673 | } | |
00df344f | 674 | |
0a920b5b | 675 | # no C99 // comments |
00df344f | 676 | if ($line =~ m{//}) { |
de7d4f0e | 677 | ERROR("do not use C99 // comments\n" . $herecurr); |
0a920b5b | 678 | } |
00df344f | 679 | # Remove C99 comments. |
0a920b5b AW |
680 | $line =~ s@//.*@@; |
681 | ||
682 | #EXPORT_SYMBOL should immediately follow its function closing }. | |
653d4876 AW |
683 | if (($line =~ /EXPORT_SYMBOL.*\((.*)\)/) || |
684 | ($line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) { | |
685 | my $name = $1; | |
0a920b5b AW |
686 | if (($prevline !~ /^}/) && |
687 | ($prevline !~ /^\+}/) && | |
653d4876 | 688 | ($prevline !~ /^ }/) && |
22f2a2ef | 689 | ($prevline !~ /\b\Q$name\E(?:\s+$Attribute)?\s*(?:;|=)/)) { |
de7d4f0e | 690 | WARN("EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr); |
0a920b5b AW |
691 | } |
692 | } | |
693 | ||
f0a594c1 AW |
694 | # check for external initialisers. |
695 | if ($line =~ /^.$Type\s*$Ident\s*=\s*(0|NULL);/) { | |
696 | ERROR("do not initialise externals to 0 or NULL\n" . | |
697 | $herecurr); | |
698 | } | |
653d4876 | 699 | # check for static initialisers. |
f0a594c1 | 700 | if ($line =~ /\s*static\s.*=\s*(0|NULL);/) { |
de7d4f0e AW |
701 | ERROR("do not initialise statics to 0 or NULL\n" . |
702 | $herecurr); | |
0a920b5b AW |
703 | } |
704 | ||
653d4876 AW |
705 | # check for new typedefs, only function parameters and sparse annotations |
706 | # make sense. | |
707 | if ($line =~ /\btypedef\s/ && | |
9c0ca6f9 | 708 | $line !~ /\btypedef\s+$Type\s+\(\s*\*?$Ident\s*\)\s*\(/ && |
653d4876 | 709 | $line !~ /\b__bitwise(?:__|)\b/) { |
de7d4f0e | 710 | WARN("do not add new typedefs\n" . $herecurr); |
0a920b5b AW |
711 | } |
712 | ||
713 | # * goes on variable not on type | |
d8aaf121 | 714 | if ($line =~ m{\($NonptrType(\*+)(?:\s+const)?\)}) { |
de7d4f0e AW |
715 | ERROR("\"(foo$1)\" should be \"(foo $1)\"\n" . |
716 | $herecurr); | |
d8aaf121 AW |
717 | |
718 | } elsif ($line =~ m{\($NonptrType\s+(\*+)(?!\s+const)\s+\)}) { | |
de7d4f0e AW |
719 | ERROR("\"(foo $1 )\" should be \"(foo $1)\"\n" . |
720 | $herecurr); | |
d8aaf121 | 721 | |
9c0ca6f9 | 722 | } elsif ($line =~ m{$NonptrType(\*+)(?:\s+(?:$Attribute|$Sparse))?\s+[A-Za-z\d_]+}) { |
de7d4f0e AW |
723 | ERROR("\"foo$1 bar\" should be \"foo $1bar\"\n" . |
724 | $herecurr); | |
d8aaf121 | 725 | |
9c0ca6f9 | 726 | } elsif ($line =~ m{$NonptrType\s+(\*+)(?!\s+(?:$Attribute|$Sparse))\s+[A-Za-z\d_]+}) { |
de7d4f0e AW |
727 | ERROR("\"foo $1 bar\" should be \"foo $1bar\"\n" . |
728 | $herecurr); | |
0a920b5b AW |
729 | } |
730 | ||
731 | # # no BUG() or BUG_ON() | |
732 | # if ($line =~ /\b(BUG|BUG_ON)\b/) { | |
733 | # print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n"; | |
734 | # print "$herecurr"; | |
735 | # $clean = 0; | |
736 | # } | |
737 | ||
00df344f AW |
738 | # printk should use KERN_* levels. Note that follow on printk's on the |
739 | # same line do not need a level, so we use the current block context | |
740 | # to try and find and validate the current printk. In summary the current | |
741 | # printk includes all preceeding printk's which have no newline on the end. | |
742 | # we assume the first bad printk is the one to report. | |
f0a594c1 | 743 | if ($line =~ /\bprintk\((?!KERN_)\s*"/) { |
00df344f AW |
744 | my $ok = 0; |
745 | for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) { | |
746 | #print "CHECK<$lines[$ln - 1]\n"; | |
747 | # we have a preceeding printk if it ends | |
748 | # with "\n" ignore it, else it is to blame | |
749 | if ($lines[$ln - 1] =~ m{\bprintk\(}) { | |
750 | if ($rawlines[$ln - 1] !~ m{\\n"}) { | |
751 | $ok = 1; | |
752 | } | |
753 | last; | |
754 | } | |
755 | } | |
756 | if ($ok == 0) { | |
de7d4f0e | 757 | WARN("printk() should include KERN_ facility level\n" . $herecurr); |
00df344f | 758 | } |
0a920b5b AW |
759 | } |
760 | ||
653d4876 AW |
761 | # function brace can't be on same line, except for #defines of do while, |
762 | # or if closed on same line | |
d8aaf121 | 763 | if (($line=~/$Type\s*[A-Za-z\d_]+\(.*\).* {/) and |
0a920b5b | 764 | !($line=~/\#define.*do\s{/) and !($line=~/}/)) { |
de7d4f0e | 765 | ERROR("open brace '{' following function declarations go on the next line\n" . $herecurr); |
0a920b5b | 766 | } |
653d4876 | 767 | |
f0a594c1 AW |
768 | # check for spaces between functions and their parentheses. |
769 | if ($line =~ /($Ident)\s+\(/ && | |
22f2a2ef | 770 | $1 !~ /^(?:if|for|while|switch|return|volatile|__volatile__|__attribute__|format|__extension__|Copyright)$/ && |
f0a594c1 | 771 | $line !~ /$Type\s+\(/ && $line !~ /^.\#\s*define\b/) { |
22f2a2ef | 772 | WARN("no space between function name and open parenthesis '('\n" . $herecurr); |
f0a594c1 | 773 | } |
653d4876 | 774 | # Check operator spacing. |
4a0df2ef AW |
775 | # Note we expand the line with the leading + as the real |
776 | # line will be displayed with the leading + and the tabs | |
777 | # will therefore also expand that way. | |
0a920b5b | 778 | my $opline = $line; |
4a0df2ef | 779 | $opline = expand_tabs($opline); |
00df344f | 780 | $opline =~ s/^./ /; |
0a920b5b | 781 | if (!($line=~/\#\s*include/)) { |
9c0ca6f9 AW |
782 | my $ops = qr{ |
783 | <<=|>>=|<=|>=|==|!=| | |
784 | \+=|-=|\*=|\/=|%=|\^=|\|=|&=| | |
785 | =>|->|<<|>>|<|>|=|!|~| | |
786 | &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/ | |
787 | }x; | |
788 | my @elements = split(/($ops|;)/, $opline); | |
00df344f | 789 | my $off = 0; |
0a920b5b | 790 | for (my $n = 0; $n < $#elements; $n += 2) { |
4a0df2ef AW |
791 | $off += length($elements[$n]); |
792 | ||
793 | my $a = ''; | |
794 | $a = 'V' if ($elements[$n] ne ''); | |
795 | $a = 'W' if ($elements[$n] =~ /\s$/); | |
796 | $a = 'B' if ($elements[$n] =~ /(\[|\()$/); | |
797 | $a = 'O' if ($elements[$n] eq ''); | |
798 | $a = 'E' if ($elements[$n] eq '' && $n == 0); | |
799 | ||
0a920b5b | 800 | my $op = $elements[$n + 1]; |
4a0df2ef AW |
801 | |
802 | my $c = ''; | |
0a920b5b | 803 | if (defined $elements[$n + 2]) { |
4a0df2ef AW |
804 | $c = 'V' if ($elements[$n + 2] ne ''); |
805 | $c = 'W' if ($elements[$n + 2] =~ /^\s/); | |
806 | $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/); | |
807 | $c = 'O' if ($elements[$n + 2] eq ''); | |
22f2a2ef | 808 | $c = 'E' if ($elements[$n + 2] =~ /\s*\\$/); |
4a0df2ef AW |
809 | } else { |
810 | $c = 'E'; | |
0a920b5b AW |
811 | } |
812 | ||
00df344f | 813 | # Pick up the preceeding and succeeding characters. |
de7d4f0e | 814 | my $ca = substr($opline, 0, $off); |
00df344f | 815 | my $cc = ''; |
653d4876 | 816 | if (length($opline) >= ($off + length($elements[$n + 1]))) { |
d8aaf121 | 817 | $cc = substr($opline, $off + length($elements[$n + 1])); |
00df344f | 818 | } |
de7d4f0e | 819 | my $cb = "$ca$;$cc"; |
00df344f | 820 | |
4a0df2ef AW |
821 | my $ctx = "${a}x${c}"; |
822 | ||
823 | my $at = "(ctx:$ctx)"; | |
824 | ||
825 | my $ptr = (" " x $off) . "^"; | |
de7d4f0e | 826 | my $hereptr = "$hereline$ptr\n"; |
0a920b5b | 827 | |
9c0ca6f9 AW |
828 | # Classify operators into binary, unary, or |
829 | # definitions (* only) where they have more | |
830 | # than one mode. | |
831 | my $unary_ctx = $prevline . $ca; | |
832 | $unary_ctx =~ s/^./ /; | |
833 | my $is_unary = 0; | |
834 | my $Unary = qr{ | |
835 | (?: | |
836 | ^|;|,|$ops|\(|\?|:| | |
837 | \(\s*$Type\s*\)| | |
838 | $Type| | |
839 | return|case|else| | |
840 | \{|\}| | |
841 | \[| | |
842 | ^.\#\s*define\s+$Ident\s*(?:\([^\)]*\))?| | |
843 | ^.\#\s*else| | |
844 | ^.\#\s*endif| | |
845 | ^.\#\s*(?:if|ifndef|ifdef)\b.* | |
846 | )\s*(?:|\\)\s*$ | |
847 | }x; | |
848 | my $UnaryFalse = qr{ | |
849 | sizeof\s*\(\s*$Type\s*\)\s*$ | |
850 | }x; | |
851 | my $UnaryDefine = qr{ | |
852 | (?:$Type|$Bare)\s*| | |
853 | (?:$Type|$Bare).*,\s*\** | |
854 | }x; | |
855 | if ($op eq '-' || $op eq '&' || $op eq '*') { | |
856 | # An operator is binary if the left hand | |
857 | # side is a value. Pick out the known | |
858 | # non-values. | |
859 | if ($unary_ctx =~ /$Unary$/s && | |
860 | $unary_ctx !~ /$UnaryFalse$/s) { | |
861 | $is_unary = 1; | |
862 | ||
863 | # Special handling for ')' check if this | |
864 | # brace represents a conditional, if so | |
865 | # we are unary. | |
866 | } elsif ($unary_ctx =~ /\)\s*$/) { | |
867 | my $before = ctx_expr_before($unary_ctx); | |
868 | if ($before =~ /(?:for|if|while)\s*$/) { | |
869 | $is_unary = 1; | |
870 | } | |
871 | } | |
872 | ||
873 | # Check for type definition for of '*'. | |
874 | if ($op eq '*' && $unary_ctx =~ /$UnaryDefine$/) { | |
875 | $is_unary = 2; | |
876 | } | |
877 | } | |
878 | ||
879 | #if ($op eq '-' || $op eq '&' || $op eq '*') { | |
880 | # print "UNARY: <$is_unary $a:$op:$c> <$ca:$op:$cc> <$unary_ctx>\n"; | |
881 | #} | |
0a920b5b | 882 | |
d8aaf121 AW |
883 | # ; should have either the end of line or a space or \ after it |
884 | if ($op eq ';') { | |
de7d4f0e AW |
885 | if ($ctx !~ /.x[WEB]/ && $cc !~ /^\\/ && |
886 | $cc !~ /^;/) { | |
887 | ERROR("need space after that '$op' $at\n" . $hereptr); | |
d8aaf121 AW |
888 | } |
889 | ||
890 | # // is a comment | |
891 | } elsif ($op eq '//') { | |
0a920b5b AW |
892 | |
893 | # -> should have no spaces | |
894 | } elsif ($op eq '->') { | |
4a0df2ef | 895 | if ($ctx =~ /Wx.|.xW/) { |
de7d4f0e | 896 | ERROR("no spaces around that '$op' $at\n" . $hereptr); |
0a920b5b AW |
897 | } |
898 | ||
899 | # , must have a space on the right. | |
900 | } elsif ($op eq ',') { | |
d8aaf121 | 901 | if ($ctx !~ /.xW|.xE/ && $cc !~ /^}/) { |
de7d4f0e | 902 | ERROR("need space after that '$op' $at\n" . $hereptr); |
0a920b5b AW |
903 | } |
904 | ||
9c0ca6f9 AW |
905 | # '*' as part of a type definition -- reported already. |
906 | } elsif ($op eq '*' && $is_unary == 2) { | |
907 | #warn "'*' is part of type\n"; | |
908 | ||
909 | # unary operators should have a space before and | |
910 | # none after. May be left adjacent to another | |
911 | # unary operator, or a cast | |
912 | } elsif ($op eq '!' || $op eq '~' || | |
913 | ($is_unary && ($op eq '*' || $op eq '-' || $op eq '&'))) { | |
914 | if ($ctx !~ /[WEB]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) { | |
de7d4f0e | 915 | ERROR("need space before that '$op' $at\n" . $hereptr); |
0a920b5b | 916 | } |
4a0df2ef | 917 | if ($ctx =~ /.xW/) { |
de7d4f0e | 918 | ERROR("no space after that '$op' $at\n" . $hereptr); |
0a920b5b AW |
919 | } |
920 | ||
921 | # unary ++ and unary -- are allowed no space on one side. | |
922 | } elsif ($op eq '++' or $op eq '--') { | |
d8aaf121 | 923 | if ($ctx !~ /[WOB]x[^W]/ && $ctx !~ /[^W]x[WOBE]/) { |
de7d4f0e | 924 | ERROR("need space one side of that '$op' $at\n" . $hereptr); |
0a920b5b | 925 | } |
d8aaf121 | 926 | if ($ctx =~ /Wx./ && $cc =~ /^;/) { |
de7d4f0e | 927 | ERROR("no space before that '$op' $at\n" . $hereptr); |
653d4876 | 928 | } |
0a920b5b | 929 | |
0a920b5b | 930 | # << and >> may either have or not have spaces both sides |
9c0ca6f9 AW |
931 | } elsif ($op eq '<<' or $op eq '>>' or |
932 | $op eq '&' or $op eq '^' or $op eq '|' or | |
933 | $op eq '+' or $op eq '-' or | |
934 | $op eq '*' or $op eq '/') | |
0a920b5b | 935 | { |
9c0ca6f9 | 936 | if ($ctx !~ /VxV|WxW|VxE|WxE|VxO/) { |
de7d4f0e AW |
937 | ERROR("need consistent spacing around '$op' $at\n" . |
938 | $hereptr); | |
0a920b5b AW |
939 | } |
940 | ||
941 | # All the others need spaces both sides. | |
4a0df2ef | 942 | } elsif ($ctx !~ /[EW]x[WE]/) { |
22f2a2ef AW |
943 | # Ignore email addresses <foo@bar> |
944 | if (!($op eq '<' && $cb =~ /$;\S+\@\S+>/) && | |
945 | !($op eq '>' && $cb =~ /<\S+\@\S+$;/)) { | |
946 | ERROR("need spaces around that '$op' $at\n" . $hereptr); | |
947 | } | |
0a920b5b | 948 | } |
4a0df2ef | 949 | $off += length($elements[$n + 1]); |
0a920b5b AW |
950 | } |
951 | } | |
952 | ||
f0a594c1 AW |
953 | # check for multiple assignments |
954 | if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) { | |
955 | WARN("multiple assignments should be avoided\n" . $herecurr); | |
956 | } | |
957 | ||
22f2a2ef AW |
958 | ## # check for multiple declarations, allowing for a function declaration |
959 | ## # continuation. | |
960 | ## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ && | |
961 | ## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) { | |
962 | ## | |
963 | ## # Remove any bracketed sections to ensure we do not | |
964 | ## # falsly report the parameters of functions. | |
965 | ## my $ln = $line; | |
966 | ## while ($ln =~ s/\([^\(\)]*\)//g) { | |
967 | ## } | |
968 | ## if ($ln =~ /,/) { | |
969 | ## WARN("declaring multiple variables together should be avoided\n" . $herecurr); | |
970 | ## } | |
971 | ## } | |
f0a594c1 | 972 | |
0a920b5b | 973 | #need space before brace following if, while, etc |
22f2a2ef AW |
974 | if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) || |
975 | $line =~ /do{/) { | |
de7d4f0e AW |
976 | ERROR("need a space before the open brace '{'\n" . $herecurr); |
977 | } | |
978 | ||
979 | # closing brace should have a space following it when it has anything | |
980 | # on the line | |
981 | if ($line =~ /}(?!(?:,|;|\)))\S/) { | |
982 | ERROR("need a space after that close brace '}'\n" . $herecurr); | |
0a920b5b AW |
983 | } |
984 | ||
22f2a2ef AW |
985 | # check spacing on square brackets |
986 | if ($line =~ /\[\s/ && $line !~ /\[\s*$/) { | |
987 | ERROR("no space after that open square bracket '['\n" . $herecurr); | |
988 | } | |
989 | if ($line =~ /\s\]/) { | |
990 | ERROR("no space before that close square bracket ']'\n" . $herecurr); | |
991 | } | |
992 | ||
993 | # check spacing on paretheses | |
9c0ca6f9 AW |
994 | if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ && |
995 | $line !~ /for\s*\(\s+;/) { | |
22f2a2ef AW |
996 | ERROR("no space after that open parenthesis '('\n" . $herecurr); |
997 | } | |
9c0ca6f9 AW |
998 | if ($line =~ /\s\)/ && $line !~ /^.\s*\)/ && |
999 | $line !~ /for\s*\(.*;\s+\)/) { | |
22f2a2ef AW |
1000 | ERROR("no space before that close parenthesis ')'\n" . $herecurr); |
1001 | } | |
1002 | ||
0a920b5b | 1003 | #goto labels aren't indented, allow a single space however |
4a0df2ef | 1004 | if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and |
0a920b5b | 1005 | !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) { |
de7d4f0e | 1006 | WARN("labels should not be indented\n" . $herecurr); |
0a920b5b AW |
1007 | } |
1008 | ||
1009 | # Need a space before open parenthesis after if, while etc | |
4a0df2ef | 1010 | if ($line=~/\b(if|while|for|switch)\(/) { |
de7d4f0e | 1011 | ERROR("need a space before the open parenthesis '('\n" . $herecurr); |
0a920b5b AW |
1012 | } |
1013 | ||
1014 | # Check for illegal assignment in if conditional. | |
653d4876 | 1015 | if ($line=~/\bif\s*\(.*[^<>!=]=[^=].*\)/) { |
00df344f | 1016 | #next if ($line=~/\".*\Q$op\E.*\"/ or $line=~/\'\Q$op\E\'/); |
de7d4f0e | 1017 | ERROR("do not use assignment in if condition\n" . $herecurr); |
0a920b5b AW |
1018 | } |
1019 | ||
1020 | # Check for }<nl>else {, these must be at the same | |
1021 | # indent level to be relevant to each other. | |
1022 | if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and | |
1023 | $previndent == $indent) { | |
de7d4f0e | 1024 | ERROR("else should follow close brace '}'\n" . $hereprev); |
0a920b5b AW |
1025 | } |
1026 | ||
0a920b5b AW |
1027 | #studly caps, commented out until figure out how to distinguish between use of existing and adding new |
1028 | # if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) { | |
1029 | # print "No studly caps, use _\n"; | |
1030 | # print "$herecurr"; | |
1031 | # $clean = 0; | |
1032 | # } | |
1033 | ||
1034 | #no spaces allowed after \ in define | |
1035 | if ($line=~/\#define.*\\\s$/) { | |
de7d4f0e | 1036 | WARN("Whitepspace after \\ makes next lines useless\n" . $herecurr); |
0a920b5b AW |
1037 | } |
1038 | ||
653d4876 AW |
1039 | #warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line) |
1040 | if ($tree && $rawline =~ m{^.\#\s*include\s*\<asm\/(.*)\.h\>}) { | |
0a920b5b AW |
1041 | my $checkfile = "include/linux/$1.h"; |
1042 | if (-f $checkfile) { | |
de7d4f0e AW |
1043 | CHK("Use #include <linux/$1.h> instead of <asm/$1.h>\n" . |
1044 | $herecurr); | |
0a920b5b AW |
1045 | } |
1046 | } | |
1047 | ||
d8aaf121 AW |
1048 | # if and else should not have general statements after it |
1049 | if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/ && | |
de7d4f0e AW |
1050 | $1 !~ /^\s*(?:\sif|{|\\|$)/) { |
1051 | ERROR("trailing statements should be on next line\n" . $herecurr); | |
d8aaf121 AW |
1052 | } |
1053 | ||
653d4876 AW |
1054 | # multi-statement macros should be enclosed in a do while loop, grab the |
1055 | # first statement and ensure its the whole macro if its not enclosed | |
1056 | # in a known goot container | |
9c0ca6f9 AW |
1057 | if ($prevline =~ /\#define.*\\/ && |
1058 | $prevline !~/(?:do\s+{|\(\{|\{)/ && | |
1059 | $line !~ /(?:do\s+{|\(\{|\{)/ && | |
1060 | $line !~ /^.\s*$Declare\s/) { | |
653d4876 AW |
1061 | # Grab the first statement, if that is the entire macro |
1062 | # its ok. This may start either on the #define line | |
1063 | # or the one below. | |
d8aaf121 AW |
1064 | my $ln = $linenr; |
1065 | my $cnt = $realcnt; | |
f0a594c1 | 1066 | my $off = 0; |
653d4876 | 1067 | |
f0a594c1 AW |
1068 | # If the macro starts on the define line start |
1069 | # grabbing the statement after the identifier | |
1070 | $prevline =~ m{^(.#\s*define\s*$Ident(?:\([^\)]*\))?\s*)(.*)\\\s*$}; | |
1071 | ##print "1<$1> 2<$2>\n"; | |
22f2a2ef | 1072 | if (defined $2 && $2 ne '') { |
f0a594c1 | 1073 | $off = length($1); |
d8aaf121 AW |
1074 | $ln--; |
1075 | $cnt++; | |
1076 | } | |
f0a594c1 | 1077 | my @ctx = ctx_statement($ln, $cnt, $off); |
de7d4f0e AW |
1078 | my $ctx_ln = $ln + $#ctx + 1; |
1079 | my $ctx = join("\n", @ctx); | |
1080 | ||
1081 | # Pull in any empty extension lines. | |
1082 | while ($ctx =~ /\\$/ && | |
1083 | $lines[$ctx_ln - 1] =~ /^.\s*(?:\\)?$/) { | |
1084 | $ctx .= $lines[$ctx_ln - 1]; | |
1085 | $ctx_ln++; | |
1086 | } | |
d8aaf121 AW |
1087 | |
1088 | if ($ctx =~ /\\$/) { | |
1089 | if ($ctx =~ /;/) { | |
de7d4f0e | 1090 | ERROR("Macros with multiple statements should be enclosed in a do - while loop\n" . "$here\n$ctx\n"); |
d8aaf121 | 1091 | } else { |
de7d4f0e | 1092 | ERROR("Macros with complex values should be enclosed in parenthesis\n" . "$here\n$ctx\n"); |
d8aaf121 | 1093 | } |
653d4876 | 1094 | } |
0a920b5b AW |
1095 | } |
1096 | ||
f0a594c1 AW |
1097 | # check for redundant bracing round if etc |
1098 | if ($line =~ /\b(if|while|for|else)\b/) { | |
1099 | # Locate the end of the opening statement. | |
1100 | my @control = ctx_statement($linenr, $realcnt, 0); | |
1101 | my $nr = $linenr + (scalar(@control) - 1); | |
1102 | my $cnt = $realcnt - (scalar(@control) - 1); | |
1103 | ||
1104 | my $off = $realcnt - $cnt; | |
1105 | #print "$off: line<$line>end<" . $lines[$nr - 1] . ">\n"; | |
1106 | ||
1107 | # If this is is a braced statement group check it | |
1108 | if ($lines[$nr - 1] =~ /{\s*$/) { | |
1109 | my ($lvl, @block) = ctx_block_level($nr, $cnt); | |
1110 | ||
1111 | my $stmt = join(' ', @block); | |
22f2a2ef AW |
1112 | $stmt =~ s/(^[^{]*){//; |
1113 | my $before = $1; | |
1114 | $stmt =~ s/}([^}]*$)//; | |
1115 | my $after = $1; | |
f0a594c1 AW |
1116 | |
1117 | #print "block<" . join(' ', @block) . "><" . scalar(@block) . ">\n"; | |
1118 | #print "stmt<$stmt>\n\n"; | |
1119 | ||
1120 | # Count the ;'s if there is fewer than two | |
1121 | # then there can only be one statement, | |
1122 | # if there is a brace inside we cannot | |
1123 | # trivially detect if its one statement. | |
1124 | # Also nested if's often require braces to | |
1125 | # disambiguate the else binding so shhh there. | |
1126 | my @semi = ($stmt =~ /;/g); | |
22f2a2ef | 1127 | push(@semi, "/**/") if ($stmt =~ m@/\*@); |
f0a594c1 AW |
1128 | ##print "semi<" . scalar(@semi) . ">\n"; |
1129 | if ($lvl == 0 && scalar(@semi) < 2 && | |
22f2a2ef AW |
1130 | $stmt !~ /{/ && $stmt !~ /\bif\b/ && |
1131 | $before !~ /}/ && $after !~ /{/) { | |
f0a594c1 AW |
1132 | my $herectx = "$here\n" . join("\n", @control, @block[1 .. $#block]) . "\n"; |
1133 | shift(@block); | |
22f2a2ef | 1134 | WARN("braces {} are not necessary for single statement blocks\n" . $herectx); |
f0a594c1 AW |
1135 | } |
1136 | } | |
1137 | } | |
1138 | ||
653d4876 | 1139 | # don't include deprecated include files (uses RAW line) |
4a0df2ef | 1140 | for my $inc (@dep_includes) { |
653d4876 | 1141 | if ($rawline =~ m@\#\s*include\s*\<$inc>@) { |
de7d4f0e | 1142 | ERROR("Don't use <$inc>: see Documentation/feature-removal-schedule.txt\n" . $herecurr); |
0a920b5b AW |
1143 | } |
1144 | } | |
1145 | ||
4a0df2ef AW |
1146 | # don't use deprecated functions |
1147 | for my $func (@dep_functions) { | |
00df344f | 1148 | if ($line =~ /\b$func\b/) { |
de7d4f0e | 1149 | ERROR("Don't use $func(): see Documentation/feature-removal-schedule.txt\n" . $herecurr); |
4a0df2ef AW |
1150 | } |
1151 | } | |
1152 | ||
1153 | # no volatiles please | |
00df344f | 1154 | if ($line =~ /\bvolatile\b/ && $line !~ /\basm\s+volatile\b/) { |
de7d4f0e | 1155 | WARN("Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr); |
4a0df2ef AW |
1156 | } |
1157 | ||
9c0ca6f9 AW |
1158 | # SPIN_LOCK_UNLOCKED & RW_LOCK_UNLOCKED are deprecated |
1159 | if ($line =~ /\b(SPIN_LOCK_UNLOCKED|RW_LOCK_UNLOCKED)/) { | |
1160 | ERROR("Use of $1 is deprecated: see Documentation/spinlocks.txt\n" . $herecurr); | |
1161 | } | |
1162 | ||
00df344f AW |
1163 | # warn about #if 0 |
1164 | if ($line =~ /^.#\s*if\s+0\b/) { | |
de7d4f0e AW |
1165 | CHK("if this code is redundant consider removing it\n" . |
1166 | $herecurr); | |
4a0df2ef AW |
1167 | } |
1168 | ||
f0a594c1 AW |
1169 | # check for needless kfree() checks |
1170 | if ($prevline =~ /\bif\s*\(([^\)]*)\)/) { | |
1171 | my $expr = $1; | |
1172 | if ($line =~ /\bkfree\(\Q$expr\E\);/) { | |
1173 | WARN("kfree(NULL) is safe this check is probabally not required\n" . $hereprev); | |
1174 | } | |
1175 | } | |
1176 | ||
00df344f AW |
1177 | # warn about #ifdefs in C files |
1178 | # if ($line =~ /^.#\s*if(|n)def/ && ($realfile =~ /\.c$/)) { | |
1179 | # print "#ifdef in C files should be avoided\n"; | |
1180 | # print "$herecurr"; | |
1181 | # $clean = 0; | |
1182 | # } | |
1183 | ||
22f2a2ef AW |
1184 | # warn about spacing in #ifdefs |
1185 | if ($line =~ /^.#\s*(ifdef|ifndef|elif)\s\s+/) { | |
1186 | ERROR("exactly one space required after that #$1\n" . $herecurr); | |
1187 | } | |
1188 | ||
4a0df2ef AW |
1189 | # check for spinlock_t definitions without a comment. |
1190 | if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/) { | |
1191 | my $which = $1; | |
1192 | if (!ctx_has_comment($first_line, $linenr)) { | |
de7d4f0e | 1193 | CHK("$1 definition without comment\n" . $herecurr); |
4a0df2ef AW |
1194 | } |
1195 | } | |
1196 | # check for memory barriers without a comment. | |
1197 | if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) { | |
1198 | if (!ctx_has_comment($first_line, $linenr)) { | |
de7d4f0e | 1199 | CHK("memory barrier without comment\n" . $herecurr); |
4a0df2ef AW |
1200 | } |
1201 | } | |
1202 | # check of hardware specific defines | |
22f2a2ef | 1203 | if ($line =~ m@^.#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) { |
de7d4f0e | 1204 | CHK("architecture specific defines should be avoided\n" . $herecurr); |
0a920b5b | 1205 | } |
653d4876 | 1206 | |
de7d4f0e AW |
1207 | # check the location of the inline attribute, that it is between |
1208 | # storage class and type. | |
9c0ca6f9 AW |
1209 | if ($line =~ /\b$Type\s+$Inline\b/ || |
1210 | $line =~ /\b$Inline\s+$Storage\b/) { | |
de7d4f0e AW |
1211 | ERROR("inline keyword should sit between storage class and type\n" . $herecurr); |
1212 | } | |
1213 | ||
1214 | # check for new externs in .c files. | |
1215 | if ($line =~ /^.\s*extern\s/ && ($realfile =~ /\.c$/)) { | |
1216 | WARN("externs should be avoided in .c files\n" . $herecurr); | |
1217 | } | |
1218 | ||
1219 | # checks for new __setup's | |
1220 | if ($rawline =~ /\b__setup\("([^"]*)"/) { | |
1221 | my $name = $1; | |
1222 | ||
1223 | if (!grep(/$name/, @setup_docs)) { | |
1224 | CHK("__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr); | |
1225 | } | |
653d4876 | 1226 | } |
9c0ca6f9 AW |
1227 | |
1228 | # check for pointless casting of kmalloc return | |
1229 | if ($line =~ /\*\s*\)\s*k[czm]alloc\b/) { | |
1230 | WARN("unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr); | |
1231 | } | |
0a920b5b AW |
1232 | } |
1233 | ||
1234 | if ($chk_patch && !$is_patch) { | |
de7d4f0e | 1235 | ERROR("Does not appear to be a unified-diff format patch\n"); |
0a920b5b AW |
1236 | } |
1237 | if ($is_patch && $chk_signoff && $signoff == 0) { | |
de7d4f0e | 1238 | ERROR("Missing Signed-off-by: line(s)\n"); |
0a920b5b AW |
1239 | } |
1240 | ||
f0a594c1 AW |
1241 | if ($clean == 0 && ($chk_patch || $is_patch)) { |
1242 | print report_dump(); | |
1243 | } | |
0a920b5b AW |
1244 | if ($clean == 1 && $quiet == 0) { |
1245 | print "Your patch has no obvious style problems and is ready for submission.\n" | |
1246 | } | |
1247 | if ($clean == 0 && $quiet == 0) { | |
1248 | print "Your patch has style problems, please review. If any of these errors\n"; | |
1249 | print "are false positives report them to the maintainer, see\n"; | |
1250 | print "CHECKPATCH in MAINTAINERS.\n"; | |
1251 | } | |
1252 | return $clean; | |
1253 | } |