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