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