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