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