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