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
10 use Term::ANSIColor qw(:constants);
15 our $SrcFile = qr{\.(?:h|c|cpp|s|S|pl|py|sh)$};
19 use Getopt::Long qw(:config no_auto_abbrev);
24 my $chk_patch = undef;
25 my $chk_branch = undef;
46 $P [OPTION]... [FILE]...
47 $P [OPTION]... [GIT-REV-LIST]
53 --patch treat FILE as patchfile
54 --branch treat args as GIT revision list
55 --emacs emacs compile window format
56 --terse one line per report
57 -f, --file treat FILE as regular source file
58 --strict fail if only warnings are found
59 --no-summary suppress the per-file summary
60 --mailback only produce a report in case of warnings/errors
61 --summary-file include the filename in summary
62 --debug KEY=[0|1] turn on/off debugging of KEY, where KEY is one of
63 'values', 'possible', 'type', and 'attr' (default
65 --test-only=WORD report only warnings/errors containing WORD
67 --color[=WHEN] Use colors 'always', 'never', or only when output
68 is a terminal ('auto'). Default is 'auto'.
69 -h, --help, --version display this help and exit
71 When FILE is - read standard input.
77 # Use at your own risk
78 print "\n", MAGENTA, "WARNING:", RESET, " This code is highly experimental ... likely isn't a great style(9) match yet\n\n";
80 # Perl's Getopt::Long allows options to take optional arguments after a space.
81 # Prevent --color by itself from consuming other arguments
83 if ($_ eq "--color" || $_ eq "-color") {
84 $_ = "--color=$color";
89 'q|quiet+' => \$quiet,
91 'signoff!' => \$chk_signoff,
92 'patch!' => \$chk_patch,
93 'branch!' => \$chk_branch,
95 'github!' => \$github,
98 'strict!' => \$no_warnings,
100 'summary!' => \$summary,
101 'mailback!' => \$mailback,
102 'summary-file!' => \$summary_file,
104 'debug=s' => \%debug,
105 'test-only=s' => \$tst_only,
106 'color=s' => \$color,
107 'no-color' => sub { $color = 'never'; },
117 print "$P: no input files\n";
121 if (!defined $chk_branch && !defined $chk_patch && !defined $file) {
122 $chk_branch = $ARGV[0] =~ /.\.\./ ? 1 : 0;
123 $file = $ARGV[0] =~ /$SrcFile/ ? 1 : 0;
124 $chk_patch = $chk_branch || $file ? 0 : 1;
125 } elsif (!defined $chk_branch && !defined $chk_patch) {
127 $chk_branch = $chk_patch = 0;
129 $chk_branch = $ARGV[0] =~ /.\.\./ ? 1 : 0;
130 $chk_patch = $chk_branch ? 0 : 1;
132 } elsif (!defined $chk_branch && !defined $file) {
134 $chk_branch = $file = 0;
136 $chk_branch = $ARGV[0] =~ /.\.\./ ? 1 : 0;
137 $file = $chk_branch ? 0 : 1;
139 } elsif (!defined $chk_patch && !defined $file) {
141 $chk_patch = $file = 0;
143 $file = $ARGV[0] =~ /$SrcFile/ ? 1 : 0;
144 $chk_patch = $file ? 0 : 1;
146 } elsif (!defined $chk_branch) {
147 $chk_branch = $chk_patch || $file ? 0 : 1;
148 } elsif (!defined $chk_patch) {
149 $chk_patch = $chk_branch || $file ? 0 : 1;
150 } elsif (!defined $file) {
151 $file = $chk_patch || $chk_branch ? 0 : 1;
154 if (($chk_patch && $chk_branch) ||
155 ($chk_patch && $file) ||
156 ($chk_branch && $file)) {
157 die "Only one of --file, --branch, --patch is permitted\n";
159 if (!$chk_patch && !$chk_branch && !$file) {
160 die "One of --file, --branch, --patch is required\n";
163 if ($color =~ /^always$/i) {
165 } elsif ($color =~ /^never$/i) {
167 } elsif ($color =~ /^auto$/i) {
168 $color = (-t STDOUT);
170 die "Invalid color mode: $color\n";
174 my $dbg_possible = 0;
176 my $dbg_adv_checking = 0;
178 for my $key (keys %debug) {
180 eval "\${dbg_$key} = '$debug{$key}';";
184 my $rpt_cleaners = 0;
191 my $emitted_corrupt = 0;
194 [A-Za-z_][A-Za-z\d_]*
195 (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
197 our $Storage = qr{extern|static|asmlinkage};
202 # Notes to $Attribute:
207 QEMU_WARN_UNUSED_RESULT|
213 our $Inline = qr{inline};
214 our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
215 our $Lval = qr{$Ident(?:$Member)*};
217 our $Constant = qr{(?:[0-9]+|0x[0-9a-fA-F]+)[UL]*};
218 our $Assignment = qr{(?:\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=)};
219 our $Compare = qr{<=|>=|==|!=|<|>};
223 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%
230 our $NON_ASCII_UTF8 = qr{
231 [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
232 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
233 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
234 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
235 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
236 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
237 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
241 [\x09\x0A\x0D\x20-\x7E] # ASCII
245 # some readers default to ISO-8859-1 when showing email source. detect
246 # when UTF-8 is incorrectly interpreted as ISO-8859-1 and reencoded back.
247 # False positives are possible but very unlikely.
248 our $UTF8_MOJIBAKE = qr{
249 \xC3[\x82-\x9F] \xC2[\x80-\xBF] # c2-df 80-bf
250 | \xC3\xA0 \xC2[\xA0-\xBF] \xC2[\x80-\xBF] # e0 a0-bf 80-bf
251 | \xC3[\xA1-\xAC\xAE\xAF] (?: \xC2[\x80-\xBF]){2} # e1-ec/ee/ef 80-bf 80-bf
252 | \xC3\xAD \xC2[\x80-\x9F] \xC2[\x80-\xBF] # ed 80-9f 80-bf
253 | \xC3\xB0 \xC2[\x90-\xBF] (?: \xC2[\x80-\xBF]){2} # f0 90-bf 80-bf 80-bf
254 | \xC3[\xB1-\xB3] (?: \xC2[\x80-\xBF]){3} # f1-f3 80-bf 80-bf 80-bf
255 | \xC3\xB4 \xC2[\x80-\x8F] (?: \xC2[\x80-\xBF]){2} # f4 80-b8 80-bf 80-bf
258 # There are still some false positives, but this catches most
260 our $typeTypedefs = qr{(?x:
261 (?![KMGTPE]iB) # IEC binary prefix (do not match)
262 [A-Z][A-Z\d_]*[a-z][A-Za-z\d_]* # camelcase
263 | [A-Z][A-Z\d_]*AIOCB # all uppercase
264 | [A-Z][A-Z\d_]*CPU # all uppercase
265 | QEMUBH # all uppercase
270 qr{(?:unsigned\s+)?char},
271 qr{(?:unsigned\s+)?short},
272 qr{(?:unsigned\s+)?int},
273 qr{(?:unsigned\s+)?long},
274 qr{(?:unsigned\s+)?long\s+int},
275 qr{(?:unsigned\s+)?long\s+long},
276 qr{(?:unsigned\s+)?long\s+long\s+int},
285 qr{${Ident}_handler},
286 qr{${Ident}_handler_fn},
287 qr{target_(?:u)?long},
291 # This can be modified by sub possible. Since it can be empty, be careful
292 # about regexes that always match, because they can cause infinite loops.
293 our @modifierList = (
297 my $all = "(?x: \n" . join("|\n ", @typeList) . "\n)";
298 if (@modifierList > 0) {
299 my $mods = "(?x: \n" . join("|\n ", @modifierList) . "\n)";
300 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
302 $Modifier = qr{(?:$Attribute|$Sparse)};
305 (?:$Modifier\s+|const\s+)*
307 (?:typeof|__typeof__)\s*\(\s*\**\s*$Ident\s*\)|
311 (?:\s+$Modifier|\s+const)*
315 (?:[\s\*]+\s*const|[\s\*]+|(?:\s*\[\s*\])+)?
316 (?:\s+$Inline|\s+$Modifier)*
318 $Declare = qr{(?:$Storage\s+)?$Type};
322 $chk_signoff = 0 if ($file);
329 my %git_commits = ();
331 open($HASH, "-|", "git", "log", "--reverse", "--no-merges", "--format=%H %s", $ARGV[0]) ||
332 die "$P: git log --reverse --no-merges --format='%H %s' $ARGV[0] failed - $!\n";
334 for my $line (<$HASH>) {
335 $line =~ /^([0-9a-fA-F]{40,40}) (.*)$/;
336 next if (!defined($1) || !defined($2));
339 push(@patches, $sha1);
340 $git_commits{$sha1} = $subject;
345 die "$P: no revisions returned for revlist '$ARGV[0]'\n"
349 my $num_patches = @patches;
350 for my $hash (@patches) {
352 open($FILE, '-|', "git", "show", "--patch-with-stat", $hash) ||
353 die "$P: git show $hash - $!\n";
359 $vname = substr($hash, 0, 12) . ' (' . $git_commits{$hash} . ')';
360 if ($num_patches > 1 && $quiet == 0) {
361 my $prefix = "$i/$num_patches";
362 $prefix = BLUE . BOLD . $prefix . RESET if $color;
363 print "$prefix Checking commit $vname\n";
364 $vname = "Patch $i/$num_patches";
366 $vname = "Commit " . $vname;
368 if (!process($hash)) {
370 print "\n" if ($num_patches > 1 && $quiet == 0);
377 for my $filename (@ARGV) {
380 open($FILE, '-|', "diff -u /dev/null $filename") ||
381 die "$P: $filename: diff failed - $!\n";
382 } elsif ($filename eq '-') {
383 open($FILE, '<&STDIN');
385 open($FILE, '<', "$filename") ||
386 die "$P: $filename: open failed - $!\n";
388 if ($filename eq '-') {
389 $vname = 'Your patch';
393 print "Checking $filename...\n" if @ARGV > 1 && $quiet == 0;
399 if (!process($filename)) {
409 sub top_of_kernel_tree {
413 "Makefile.inc1", "README.md", "sys",
417 foreach my $check (@tree_check) {
418 if (! -e $root . '/' . $check) {
430 for my $c (split(//, $str)) {
434 for (; ($n % 8) != 0; $n++) {
446 (my $res = shift) =~ tr/\t/ /c;
453 # Drop the diff line leader and expand tabs
455 $line = expand_tabs($line);
457 # Pick the indent from the front of the line.
458 my ($white) = ($line =~ /^(\s*)/);
460 return (length($line), length($white));
463 my $sanitise_quote = '';
465 sub sanitise_line_reset {
466 my ($in_comment) = @_;
469 $sanitise_quote = '*/';
471 $sanitise_quote = '';
484 # Always copy over the diff marker.
485 $res = substr($line, 0, 1);
487 for ($off = 1; $off < length($line); $off++) {
488 $c = substr($line, $off, 1);
490 # Comments we are wacking completely including the begin
491 # and end, all to $;.
492 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
493 $sanitise_quote = '*/';
495 substr($res, $off, 2, "$;$;");
499 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
500 $sanitise_quote = '';
501 substr($res, $off, 2, "$;$;");
505 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
506 $sanitise_quote = '//';
508 substr($res, $off, 2, $sanitise_quote);
513 # A \ in a string means ignore the next character.
514 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
516 substr($res, $off, 2, 'XX');
521 if ($c eq "'" || $c eq '"') {
522 if ($sanitise_quote eq '') {
523 $sanitise_quote = $c;
525 substr($res, $off, 1, $c);
527 } elsif ($sanitise_quote eq $c) {
528 $sanitise_quote = '';
532 #print "c<$c> SQ<$sanitise_quote>\n";
533 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
534 substr($res, $off, 1, $;);
535 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
536 substr($res, $off, 1, $;);
537 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
538 substr($res, $off, 1, 'X');
540 substr($res, $off, 1, $c);
544 if ($sanitise_quote eq '//') {
545 $sanitise_quote = '';
548 # The pathname on a #include may be surrounded by '<' and '>'.
549 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
550 my $clean = 'X' x length($1);
551 $res =~ s@\<.*\>@<$clean>@;
553 # The whole of a #error is a string.
554 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
555 my $clean = 'X' x length($1);
556 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
562 sub ctx_statement_block {
563 my ($linenr, $remain, $off) = @_;
564 my $line = $linenr - 1;
581 @stack = (['', 0]) if ($#stack == -1);
583 #warn "CSB: blk<$blk> remain<$remain>\n";
584 # If we are about to drop off the end, pull in more
587 for (; $remain > 0; $line++) {
588 last if (!defined $lines[$line]);
589 next if ($lines[$line] =~ /^-/);
592 $blk .= $lines[$line] . "\n";
597 # Bail if there is no further context.
598 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
604 $c = substr($blk, $off, 1);
605 $remainder = substr($blk, $off);
607 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
609 # Handle nested #if/#else.
610 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
611 push(@stack, [ $type, $level ]);
612 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
613 ($type, $level) = @{$stack[$#stack - 1]};
614 } elsif ($remainder =~ /^#\s*endif\b/) {
615 ($type, $level) = @{pop(@stack)};
618 # Statement ends at the ';' or a close '}' at the
620 if ($level == 0 && $c eq ';') {
624 # An else is really a conditional as long as its not else if
625 if ($level == 0 && $coff_set == 0 &&
626 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
627 $remainder =~ /^(else)(?:\s|{)/ &&
628 $remainder !~ /^else\s+if\b/) {
629 $coff = $off + length($1) - 1;
631 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
632 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
635 if (($type eq '' || $type eq '(') && $c eq '(') {
639 if ($type eq '(' && $c eq ')') {
641 $type = ($level != 0)? '(' : '';
643 if ($level == 0 && $coff < $soff) {
646 #warn "CSB: mark coff<$coff>\n";
649 if (($type eq '' || $type eq '{') && $c eq '{') {
653 if ($type eq '{' && $c eq '}') {
655 $type = ($level != 0)? '{' : '';
658 if (substr($blk, $off + 1, 1) eq ';') {
666 # We are truly at the end, so shuffle to the next line.
673 my $statement = substr($blk, $soff, $off - $soff + 1);
674 my $condition = substr($blk, $soff, $coff - $soff + 1);
676 #warn "STATEMENT<$statement>\n";
677 #warn "CONDITION<$condition>\n";
679 #print "coff<$coff> soff<$off> loff<$loff>\n";
681 return ($statement, $condition,
682 $line, $remain + 1, $off - $loff + 1, $level);
685 sub statement_lines {
688 # Strip the diff line prefixes and rip blank lines at start and end.
689 $stmt =~ s/(^|\n)./$1/g;
693 my @stmt_lines = ($stmt =~ /\n/g);
695 return $#stmt_lines + 2;
698 sub statement_rawlines {
701 my @stmt_lines = ($stmt =~ /\n/g);
703 return $#stmt_lines + 2;
706 sub statement_block_size {
709 $stmt =~ s/(^|\n)./$1/g;
715 my @stmt_lines = ($stmt =~ /\n/g);
716 my @stmt_statements = ($stmt =~ /;/g);
718 my $stmt_lines = $#stmt_lines + 2;
719 my $stmt_statements = $#stmt_statements + 1;
721 if ($stmt_lines > $stmt_statements) {
724 return $stmt_statements;
728 sub ctx_statement_full {
729 my ($linenr, $remain, $off) = @_;
730 my ($statement, $condition, $level);
734 # Grab the first conditional/block pair.
735 ($statement, $condition, $linenr, $remain, $off, $level) =
736 ctx_statement_block($linenr, $remain, $off);
737 #print "F: c<$condition> s<$statement> remain<$remain>\n";
738 push(@chunks, [ $condition, $statement ]);
739 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
740 return ($level, $linenr, @chunks);
743 # Pull in the following conditional/block pairs and see if they
744 # could continue the statement.
746 ($statement, $condition, $linenr, $remain, $off, $level) =
747 ctx_statement_block($linenr, $remain, $off);
748 #print "C: c<$condition> s<$statement> remain<$remain>\n";
749 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
751 push(@chunks, [ $condition, $statement ]);
754 return ($level, $linenr, @chunks);
758 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
760 my $start = $linenr - 1;
767 my @stack = ($level);
768 for ($line = $start; $remain > 0; $line++) {
769 next if ($rawlines[$line] =~ /^-/);
772 $blk .= $rawlines[$line];
774 # Handle nested #if/#else.
775 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
776 push(@stack, $level);
777 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
778 $level = $stack[$#stack - 1];
779 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
780 $level = pop(@stack);
783 foreach my $c (split(//, $lines[$line])) {
784 ##print "C<$c>L<$level><$open$close>O<$off>\n";
790 if ($c eq $close && $level > 0) {
792 last if ($level == 0);
793 } elsif ($c eq $open) {
798 if (!$outer || $level <= 1) {
799 push(@res, $rawlines[$line]);
802 last if ($level == 0);
805 return ($level, @res);
807 sub ctx_block_outer {
808 my ($linenr, $remain) = @_;
810 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
814 my ($linenr, $remain) = @_;
816 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
820 my ($linenr, $remain, $off) = @_;
822 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
825 sub ctx_block_level {
826 my ($linenr, $remain) = @_;
828 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
830 sub ctx_statement_level {
831 my ($linenr, $remain, $off) = @_;
833 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
836 sub ctx_locate_comment {
837 my ($first_line, $end_line) = @_;
839 # Catch a comment on the end of the line itself.
840 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
841 return $current_comment if (defined $current_comment);
843 # Look through the context and try and figure out if there is a
846 $current_comment = '';
847 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
848 my $line = $rawlines[$linenr - 1];
850 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
853 if ($line =~ m@/\*@) {
856 if (!$in_comment && $current_comment ne '') {
857 $current_comment = '';
859 $current_comment .= $line . "\n" if ($in_comment);
860 if ($line =~ m@\*/@) {
865 chomp($current_comment);
866 return($current_comment);
868 sub ctx_has_comment {
869 my ($first_line, $end_line) = @_;
870 my $cmt = ctx_locate_comment($first_line, $end_line);
872 ##print "LINE: $rawlines[$end_line - 1 ]\n";
873 ##print "CMMT: $cmt\n";
879 my ($linenr, $cnt) = @_;
881 my $offset = $linenr - 1;
886 $line = $rawlines[$offset++];
887 next if (defined($line) && $line =~ /^-/);
899 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
902 $coded = sprintf("^%c", unpack('C', $2) + 64);
911 my $av_preprocessor = 0;
917 $av_preprocessor = 0;
919 @av_paren_type = ('E');
920 $av_pend_colon = 'O';
923 sub annotate_values {
924 my ($stream, $type) = @_;
927 my $var = '_' x length($stream);
930 print "$stream\n" if ($dbg_values > 1);
932 while (length($cur)) {
933 @av_paren_type = ('E') if ($#av_paren_type < 0);
934 print " <" . join('', @av_paren_type) .
935 "> <$type> <$av_pending>" if ($dbg_values > 1);
936 if ($cur =~ /^(\s+)/o) {
937 print "WS($1)\n" if ($dbg_values > 1);
938 if ($1 =~ /\n/ && $av_preprocessor) {
939 $type = pop(@av_paren_type);
940 $av_preprocessor = 0;
943 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
944 print "CAST($1)\n" if ($dbg_values > 1);
945 push(@av_paren_type, $type);
948 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
949 print "DECLARE($1)\n" if ($dbg_values > 1);
952 } elsif ($cur =~ /^($Modifier)\s*/) {
953 print "MODIFIER($1)\n" if ($dbg_values > 1);
956 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
957 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
958 $av_preprocessor = 1;
959 push(@av_paren_type, $type);
965 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
966 print "UNDEF($1)\n" if ($dbg_values > 1);
967 $av_preprocessor = 1;
968 push(@av_paren_type, $type);
970 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
971 print "PRE_START($1)\n" if ($dbg_values > 1);
972 $av_preprocessor = 1;
974 push(@av_paren_type, $type);
975 push(@av_paren_type, $type);
978 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
979 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
980 $av_preprocessor = 1;
982 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
986 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
987 print "PRE_END($1)\n" if ($dbg_values > 1);
989 $av_preprocessor = 1;
991 # Assume all arms of the conditional end as this
992 # one does, and continue as if the #endif was not here.
994 push(@av_paren_type, $type);
997 } elsif ($cur =~ /^(\\\n)/o) {
998 print "PRECONT($1)\n" if ($dbg_values > 1);
1000 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1001 print "ATTR($1)\n" if ($dbg_values > 1);
1002 $av_pending = $type;
1005 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
1006 print "SIZEOF($1)\n" if ($dbg_values > 1);
1012 } elsif ($cur =~ /^(if|while|for)\b/o) {
1013 print "COND($1)\n" if ($dbg_values > 1);
1017 } elsif ($cur =~/^(case)/o) {
1018 print "CASE($1)\n" if ($dbg_values > 1);
1019 $av_pend_colon = 'C';
1022 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
1023 print "KEYWORD($1)\n" if ($dbg_values > 1);
1026 } elsif ($cur =~ /^(\()/o) {
1027 print "PAREN('$1')\n" if ($dbg_values > 1);
1028 push(@av_paren_type, $av_pending);
1032 } elsif ($cur =~ /^(\))/o) {
1033 my $new_type = pop(@av_paren_type);
1034 if ($new_type ne '_') {
1036 print "PAREN('$1') -> $type\n"
1037 if ($dbg_values > 1);
1039 print "PAREN('$1')\n" if ($dbg_values > 1);
1042 } elsif ($cur =~ /^($Ident)\s*\(/o) {
1043 print "FUNC($1)\n" if ($dbg_values > 1);
1047 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
1048 if (defined $2 && $type eq 'C' || $type eq 'T') {
1049 $av_pend_colon = 'B';
1050 } elsif ($type eq 'E') {
1051 $av_pend_colon = 'L';
1053 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
1056 } elsif ($cur =~ /^($Ident|$Constant)/o) {
1057 print "IDENT($1)\n" if ($dbg_values > 1);
1060 } elsif ($cur =~ /^($Assignment)/o) {
1061 print "ASSIGN($1)\n" if ($dbg_values > 1);
1064 } elsif ($cur =~/^(;|{|})/) {
1065 print "END($1)\n" if ($dbg_values > 1);
1067 $av_pend_colon = 'O';
1069 } elsif ($cur =~/^(,)/) {
1070 print "COMMA($1)\n" if ($dbg_values > 1);
1073 } elsif ($cur =~ /^(\?)/o) {
1074 print "QUESTION($1)\n" if ($dbg_values > 1);
1077 } elsif ($cur =~ /^(:)/o) {
1078 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
1080 substr($var, length($res), 1, $av_pend_colon);
1081 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
1086 $av_pend_colon = 'O';
1088 } elsif ($cur =~ /^(\[)/o) {
1089 print "CLOSE($1)\n" if ($dbg_values > 1);
1092 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
1095 print "OPV($1)\n" if ($dbg_values > 1);
1102 substr($var, length($res), 1, $variant);
1105 } elsif ($cur =~ /^($Operators)/o) {
1106 print "OP($1)\n" if ($dbg_values > 1);
1107 if ($1 ne '++' && $1 ne '--') {
1111 } elsif ($cur =~ /(^.)/o) {
1112 print "C($1)\n" if ($dbg_values > 1);
1115 $cur = substr($cur, length($1));
1116 $res .= $type x length($1);
1120 return ($res, $var);
1124 my ($possible, $line) = @_;
1125 my $notPermitted = qr{(?:
1140 ^(?:typedef|struct|enum)\b|
1143 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
1144 if ($possible !~ $notPermitted) {
1145 # Check for modifiers.
1146 $possible =~ s/\s*$Storage\s*//g;
1147 $possible =~ s/\s*$Sparse\s*//g;
1148 if ($possible =~ /^\s*$/) {
1150 } elsif ($possible =~ /\s/) {
1151 $possible =~ s/\s*(?:$Type|\#\#)\s*//g;
1152 for my $modifier (split(' ', $possible)) {
1153 if ($modifier !~ $notPermitted) {
1154 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1155 push(@modifierList, $modifier);
1160 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
1161 push(@typeList, $possible);
1165 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
1172 my ($level, $msg) = @_;
1173 if (defined $tst_only && $msg !~ /\Q$tst_only\E/) {
1178 my $do_color = $color && !$github;
1179 $output .= BOLD if $do_color;
1180 $output .= "::error " if $github && $level eq 'ERROR';
1181 $output .= "::warning " if $github && $level eq 'WARNING';
1183 $output .= RED if $do_color && $level eq 'ERROR';
1184 $output .= MAGENTA if $do_color && $level eq 'WARNING';
1185 $output .= $level . ':' if !$github;
1186 $output .= RESET if $do_color;
1187 $output .= ' ' if (!$github);
1188 $output .= $msg . "\n";
1190 $output = (split('\n', $output))[0] . "\n" if ($terse);
1192 push(our @report, $output);
1200 if (report("ERROR", $_[0])) {
1206 if (report("WARNING", $_[0])) {
1212 # According to tests/qtest/bios-tables-test.c: do not
1213 # change expected file in the same commit with adding test
1215 my ($name, $acpi_testexpected, $acpi_nontestexpected) = @_;
1217 # Note: shell script that rebuilds the expected files is in the same
1218 # directory as files themselves.
1219 # Note: allowed diff list can be changed both when changing expected
1220 # files and when changing tests.
1221 if ($name =~ m#^tests/data/acpi/# and not $name =~ m#^\.sh$#) {
1222 $$acpi_testexpected = $name;
1223 } elsif ($name !~ m#^tests/qtest/bios-tables-test-allowed-diff.h$#) {
1224 $$acpi_nontestexpected = $name;
1226 if (defined $$acpi_testexpected and defined $$acpi_nontestexpected) {
1227 ERROR("Do not add expected files together with tests, " .
1228 "follow instructions in " .
1229 "tests/qtest/bios-tables-test.c: both " .
1230 $$acpi_testexpected . " and " .
1231 $$acpi_nontestexpected . " found\n");
1236 my $filename = shift;
1242 my $stashrawline="";
1253 my $in_header_lines = $file ? 0 : 1;
1254 my $in_commit_log = 0; #Scanning lines before patch
1255 my $non_utf8_charset = 0;
1263 # Trace the real file/line as we go.
1269 my $comment_edge = 0;
1273 my $prev_values = 'E';
1276 my %suppress_ifbraces;
1277 my %suppress_whiletrailers;
1278 my %suppress_export;
1280 my $acpi_testexpected;
1281 my $acpi_nontestexpected;
1283 # Pre-scan the patch sanitizing the lines.
1285 sanitise_line_reset();
1287 foreach my $rawline (@rawlines) {
1291 if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1300 # Guestimate if this is a continuing comment. Run
1301 # the context looking for a comment "edge". If this
1302 # edge is a close comment then we must be in a comment
1306 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
1307 next if (defined $rawlines[$ln - 1] &&
1308 $rawlines[$ln - 1] =~ /^-/);
1310 #print "RAW<$rawlines[$ln - 1]>\n";
1311 last if (!defined $rawlines[$ln - 1]);
1312 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
1313 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
1318 if (defined $edge && $edge eq '*/') {
1322 # Guestimate if this is a continuing comment. If this
1323 # is the start of a diff block and this line starts
1324 # ' *' then it is very likely a comment.
1325 if (!defined $edge &&
1326 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
1331 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
1332 sanitise_line_reset($in_comment);
1334 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
1335 # Standardise the strings and chars within the input to
1336 # simplify matching -- only bother with positive lines.
1337 $line = sanitise_line($rawline);
1339 push(@lines, $line);
1342 $realcnt-- if ($line =~ /^(?:\+| |$)/);
1347 #print "==>$rawline\n";
1348 #print "-->$line\n";
1355 foreach my $line (@lines) {
1358 my $rawline = $rawlines[$linenr - 1];
1360 #extract the line range in the file after the patch is applied
1361 if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1363 $first_line = $linenr + 1;
1373 %suppress_ifbraces = ();
1374 %suppress_whiletrailers = ();
1375 %suppress_export = ();
1378 # track the line number as we move through the hunk, note that
1379 # new versions of GNU diff omit the leading space on completely
1380 # blank context lines so we need to count that too.
1381 } elsif ($line =~ /^( |\+|$)/) {
1383 $realcnt-- if ($realcnt != 0);
1385 # Measure the line length and indent.
1386 ($length, $indent) = line_stats($rawline);
1388 # Track the previous line.
1389 ($prevline, $stashline) = ($stashline, $line);
1390 ($previndent, $stashindent) = ($stashindent, $indent);
1391 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
1393 #warn "line<$line>\n";
1395 } elsif ($realcnt == 1) {
1399 my $hunk_line = ($realcnt != 0);
1401 #make up the handle for any error we report on this line
1402 $prefix = "$filename:$realline: " if ($emacs && $file);
1403 $prefix = "$filename:$linenr: " if ($emacs && !$file);
1404 $prefix = "file=$filename,line=$realline:\:" if ($github && $file);
1405 $prefix = "file=$realfile,line=$realline:\:" if ($github && !$file);
1407 $here = "#$linenr: " if (!$file);
1408 $here = "#$realline: " if ($file);
1410 # extract the filename as it passes
1411 if ($line =~ /^diff --git.*?(\S+)$/) {
1413 $realfile =~ s@^([^/]*)/@@ if (!$file);
1414 checkfilename($realfile, \$acpi_testexpected, \$acpi_nontestexpected);
1415 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
1417 $realfile =~ s@^([^/]*)/@@ if (!$file);
1418 checkfilename($realfile, \$acpi_testexpected, \$acpi_nontestexpected);
1421 if (!$file && $tree && $p1_prefix ne '' && defined $root &&
1422 -e "$root/$p1_prefix") {
1423 WARN("patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
1429 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
1431 my $hereline = "$here\n$rawline\n";
1432 my $herecurr = "$here\n$rawline\n";
1433 my $hereprev = "$here\n$prevrawline\n$rawline\n";
1435 $cnt_lines++ if ($realcnt != 0);
1437 # Check for incorrect file permissions
1438 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
1439 my $permhere = $here . "FILE: $realfile\n";
1440 if ($realfile =~ /(\bMakefile(?:\.objs)?|\.c|\.cc|\.cpp|\.h|\.mak|\.[sS])$/) {
1441 ERROR("do not set execute permissions for source files\n" . $permhere);
1445 # Accept git diff extended headers as valid patches
1446 if ($line =~ /^(?:rename|copy) (?:from|to) [\w\/\.\-]+\s*$/) {
1450 # Filter out bad email addresses.
1451 if ($line =~ /^(Author|From): .*noreply.*/) {
1452 ERROR("Real email adress is needed\n" . $herecurr);
1455 #check the patch for a signoff:
1456 if ($line =~ /^\s*signed-off-by:/i) {
1457 # This is a signoff, if ugly, so do not double report.
1460 if (!($line =~ /^\s*Signed-off-by:/)) {
1461 ERROR("The correct form is \"Signed-off-by\"\n" .
1464 if ($line =~ /^\s*signed-off-by:\S/i) {
1465 ERROR("space required after Signed-off-by:\n" .
1470 # Check for wrappage within a valid hunk of the file
1471 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
1472 ERROR("patch seems to be corrupt (line wrapped?)\n" .
1473 $herecurr) if (!$emitted_corrupt++);
1476 # UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
1477 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
1478 $rawline !~ m/^$UTF8*$/) {
1479 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
1481 my $blank = copy_spacing($rawline);
1482 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
1483 my $hereptr = "$hereline$ptr\n";
1485 ERROR("Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
1488 if ($rawline =~ m/$UTF8_MOJIBAKE/) {
1489 ERROR("Doubly-encoded UTF-8\n" . $herecurr);
1491 # Check if it's the start of a commit log
1492 # (not a header line and we haven't seen the patch filename)
1493 if ($in_header_lines && $realfile =~ /^$/ &&
1494 !($rawline =~ /^\s+\S/ ||
1495 $rawline =~ /^(commit\b|from\b|[\w-]+:).*$/i)) {
1496 $in_header_lines = 0;
1500 # Check if there is UTF-8 in a commit log when a mail header has explicitly
1501 # declined it, i.e defined some charset where it is missing.
1502 if ($in_header_lines &&
1503 $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
1505 $non_utf8_charset = 1;
1508 if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
1509 $rawline =~ /$NON_ASCII_UTF8/) {
1510 WARN("8-bit UTF-8 used in possible commit log\n" . $herecurr);
1513 # ignore non-hunk lines and lines being removed
1514 next if (!$hunk_line || $line =~ /^-/);
1516 #trailing whitespace
1517 if ($line =~ /^\+.*\015/) {
1518 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1519 ERROR("DOS line endings\n" . $herevet);
1521 } elsif ($realfile =~ /^docs\/.+\.txt/ ||
1522 $realfile =~ /^docs\/.+\.md/) {
1523 if ($rawline =~ /^\+\s+$/ && $rawline !~ /^\+ {4}$/) {
1524 # TODO: properly check we're in a code block
1525 # (surrounding text is 4-column aligned)
1526 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1527 ERROR("code blocks in documentation should have " .
1528 "empty lines with exactly 4 columns of " .
1529 "whitespace\n" . $herevet);
1531 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
1532 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1533 ERROR("trailing whitespace\n" . $herevet);
1537 # check we are in a valid source file if not then ignore this hunk
1538 next if ($realfile !~ /$SrcFile/);
1540 #120 column limit; exempt URLs, if no other words on line
1541 if ($line =~ /^\+/ &&
1542 !($line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) &&
1543 !($rawline =~ /^[^[:alnum:]]*https?:\S*$/) &&
1545 $realfile !~ /\/tests\//)
1547 if ($length > 120) {
1548 ERROR("line over 120 characters\n" . $herecurr);
1550 WARN("line over 80 characters\n" . $herecurr);
1554 # check for spaces before a quoted newline
1555 if ($rawline =~ /^.*\".*\s\\n/) {
1556 ERROR("unnecessary whitespace before a quoted newline\n" . $herecurr);
1559 # check for adding lines without a newline.
1560 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
1561 ERROR("adding a line without newline at end of file\n" . $herecurr);
1564 # check for RCS/CVS revision markers
1565 if ($rawline =~ /^\+.*\$(FreeBSD|Revision|Log|Id)(?:\$|\b)/) {
1566 ERROR("CVS style keyword markers, these will _not_ be updated\n". $herecurr);
1569 # check we are in a valid C source file if not then ignore this hunk
1570 next if ($realfile !~ /\.(h|c|cpp)$/);
1572 # Block comment styles
1574 # Block comments use /* on a line of its own
1575 if ($rawline !~ m@^\+.*/\*.*\*/[ \t)}]*$@ && #inline /*...*/
1576 $rawline =~ m@^\+.*/\*[*-]?+[ \t]*[^ \t]@) { # /* or /** or /*- non-blank
1577 WARN("Block comments use a leading /* on a separate line\n" . $herecurr);
1580 # Block comments use * on subsequent lines
1581 if ($prevline =~ /$;[ \t]*$/ && #ends in comment
1582 $prevrawline =~ /^\+.*?\/\*/ && #starting /*
1583 $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */
1584 $rawline =~ /^\+/ && #line is new
1585 $rawline !~ /^\+[ \t]*\*/) { #no leading *
1586 WARN("Block comments use * on subsequent lines\n" . $hereprev);
1589 # Block comments use */ on trailing lines
1590 if ($rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */
1591 $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/
1592 $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/
1593 $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */
1594 WARN("Block comments use a trailing */ on a separate line\n" . $herecurr);
1597 # Block comment * alignment
1598 if ($prevline =~ /$;[ \t]*$/ && #ends in comment
1599 $line =~ /^\+[ \t]*$;/ && #leading comment
1600 $rawline =~ /^\+[ \t]*\*/ && #leading *
1601 (($prevrawline =~ /^\+.*?\/\*/ && #leading /*
1602 $prevrawline !~ /\*\/[ \t]*$/) || #no trailing */
1603 $prevrawline =~ /^\+[ \t]*\*/)) { #leading *
1605 $prevrawline =~ m@^\+([ \t]*/?)\*@;
1607 $oldindent = expand_tabs($1);
1609 $prevrawline =~ m@^\+(.*/?)\*@;
1610 $oldindent = expand_tabs($1);
1612 $rawline =~ m@^\+([ \t]*)\*@;
1614 $newindent = expand_tabs($newindent);
1615 if (length($oldindent) ne length($newindent)) {
1616 WARN("Block comments should align the * on each line\n" . $hereprev);
1620 # Check for potential 'bare' types
1621 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
1623 if ($realcnt && $line =~ /.\s*\S/) {
1624 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
1625 ctx_statement_block($linenr, $realcnt, 0);
1626 $stat =~ s/\n./\n /g;
1627 $cond =~ s/\n./\n /g;
1629 # Find the real next line.
1630 $realline_next = $line_nr_next;
1631 if (defined $realline_next &&
1632 (!defined $lines[$realline_next - 1] ||
1633 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
1640 # Ignore goto labels.
1641 if ($s =~ /$Ident:\*$/s) {
1643 # Ignore functions being called
1644 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
1646 } elsif ($s =~ /^.\s*else\b/s) {
1648 # declarations always start with types
1649 } 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) {
1652 possible($type, "A:" . $s);
1654 # definitions in global scope can only start with types
1655 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
1656 possible($1, "B:" . $s);
1659 # any (foo ... *) is a pointer cast, and foo is a type
1660 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
1661 possible($1, "C:" . $s);
1664 # Check for any sort of function declaration.
1665 # int foo(something bar, other baz);
1666 # void (*store_gdt)(x86_descr_ptr *);
1667 if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
1668 my ($name_len) = length($1);
1671 substr($ctx, 0, $name_len + 1, '');
1672 $ctx =~ s/\)[^\)]*$//;
1674 for my $arg (split(/\s*,\s*/, $ctx)) {
1675 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
1677 possible($1, "D:" . $s);
1685 # Checks which may be anchored in the context.
1688 # Check for switch () and associated case and default
1689 # statements should be at the same indent.
1690 if ($line=~/\bswitch\s*\(.*\)/) {
1693 my @ctx = ctx_block_outer($linenr, $realcnt);
1695 for my $ctx (@ctx) {
1696 my ($clen, $cindent) = line_stats($ctx);
1697 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
1698 $indent != $cindent) {
1699 $err .= "$sep$ctx\n";
1706 ERROR("switch and case should be at the same indent\n$hereline$err");
1710 # if/while/etc brace do not go on next line, unless defining a do while loop,
1711 # or if that brace on the next line is for something else
1712 if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
1713 my $pre_ctx = "$1$2";
1715 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
1716 my $ctx_cnt = $realcnt - $#ctx - 1;
1717 my $ctx = join("\n", @ctx);
1719 my $ctx_ln = $linenr;
1720 my $ctx_skip = $realcnt;
1722 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
1723 defined $lines[$ctx_ln - 1] &&
1724 $lines[$ctx_ln - 1] =~ /^-/)) {
1725 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
1726 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
1730 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
1731 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
1733 # The length of the "previous line" is checked against 80 because it
1734 # includes the + at the beginning of the line (if the actual line has
1735 # 79 or 80 characters, it is no longer possible to add a space and an
1736 # opening brace there)
1737 if ($#ctx == 0 && $ctx !~ /{\s*/ &&
1738 defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*\{/ &&
1739 defined($lines[$ctx_ln - 2]) && length($lines[$ctx_ln - 2]) < 80) {
1740 ERROR("that open brace { should be on the previous line\n" .
1741 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
1743 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
1744 $ctx =~ /\)\s*\;\s*$/ &&
1745 defined $lines[$ctx_ln - 1])
1747 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
1748 if ($nindent > $indent) {
1749 ERROR("trailing semicolon indicates no statements, indent implies otherwise\n" .
1750 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
1755 # 'do ... while (0/false)' only makes sense in macros, without trailing ';'
1756 if ($line =~ /while\s*\((0|false)\);/) {
1757 ERROR("suspicious ; after while (0)\n" . $herecurr);
1760 # Check superfluous trailing ';'
1761 if ($line =~ /;;$/) {
1762 ERROR("superfluous trailing semicolon\n" . $herecurr);
1765 # Check relative indent for conditionals and blocks.
1766 if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
1767 my ($s, $c) = ($stat, $cond);
1769 substr($s, 0, length($c), '');
1771 # Make sure we remove the line prefixes as we have
1772 # none on the first line, and are going to re-add them
1776 # Find out how long the conditional actually is.
1777 my @newlines = ($c =~ /\n/gs);
1778 my $cond_lines = 1 + $#newlines;
1780 # We want to check the first line inside the block
1781 # starting at the end of the conditional, so remove:
1782 # 1) any blank line termination
1783 # 2) any opening brace { on end of the line
1785 my $continuation = 0;
1787 $s =~ s/^.*\bdo\b//;
1789 if ($s =~ s/^\s*\\//) {
1792 if ($s =~ s/^\s*?\n//) {
1797 # Also ignore a loop construct at the end of a
1798 # preprocessor statement.
1799 if (($prevline =~ /^.\s*#\s*define\s/ ||
1800 $prevline =~ /\\\s*$/) && $continuation == 0) {
1806 while ($cond_ptr != $cond_lines) {
1807 $cond_ptr = $cond_lines;
1809 # If we see an #else/#elif then the code
1811 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
1816 # 1) blank lines, they should be at 0,
1817 # 2) preprocessor lines, and
1819 if ($continuation ||
1821 $s =~ /^\s*#\s*?/ ||
1822 $s =~ /^\s*$Ident\s*:/) {
1823 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
1824 if ($s =~ s/^.*?\n//) {
1830 my (undef, $sindent) = line_stats("+" . $s);
1831 my $stat_real = raw_line($linenr, $cond_lines);
1833 # Check if either of these lines are modified, else
1834 # this is not this patch's fault.
1835 if (!defined($stat_real) ||
1836 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
1839 if (defined($stat_real) && $cond_lines > 1) {
1840 $stat_real = "[...]\n$stat_real";
1843 #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";
1845 if ($check && (($sindent % 4) != 0 ||
1846 ($sindent <= $indent && $s ne ''))) {
1847 ERROR("suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
1851 # Track the 'values' across context and added lines.
1852 my $opline = $line; $opline =~ s/^./ /;
1853 my ($curr_values, $curr_vars) =
1854 annotate_values($opline . "\n", $prev_values);
1855 $curr_values = $prev_values . $curr_values;
1857 my $outline = $opline; $outline =~ s/\t/ /g;
1858 print "$linenr > .$outline\n";
1859 print "$linenr > $curr_values\n";
1860 print "$linenr > $curr_vars\n";
1862 $prev_values = substr($curr_values, -1);
1864 #ignore lines not being added
1865 if ($line=~/^[^\+]/) {next;}
1867 # check for initialisation to aggregates open brace on the next line
1868 if ($line =~ /^.\s*\{/ &&
1869 $prevline =~ /(?:^|[^=])=\s*$/) {
1870 ERROR("that open brace { should be on the previous line\n" . $hereprev);
1874 # Checks which are anchored on the added line.
1877 # check for malformed paths in #include statements (uses RAW line)
1878 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
1880 if ($path =~ m{//}) {
1881 ERROR("malformed #include filename\n" .
1886 # Remove C99 comments.
1888 $opline =~ s@//.*@@;
1890 # * goes on variable not on type
1892 if ($line =~ m{\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\)}) {
1893 my ($from, $to) = ($1, $1);
1895 # Should start with a space.
1896 $to =~ s/^(\S)/ $1/;
1897 # Should not end with a space.
1899 # '*'s should not have spaces between.
1900 while ($to =~ s/\*\s+\*/\*\*/) {
1903 #print "from<$from> to<$to>\n";
1905 ERROR("\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr);
1907 } elsif ($line =~ m{\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident)}) {
1908 my ($from, $to, $ident) = ($1, $1, $2);
1910 # Should start with a space.
1911 $to =~ s/^(\S)/ $1/;
1912 # Should not end with a space.
1914 # '*'s should not have spaces between.
1915 while ($to =~ s/\*\s+\*/\*\*/) {
1917 # Modifiers should have spaces.
1918 $to =~ s/(\b$Modifier$)/$1 /;
1920 #print "from<$from> to<$to> ident<$ident>\n";
1921 if ($from ne $to && $ident !~ /^$Modifier$/) {
1922 ERROR("\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr);
1926 # function brace can't be on same line, except for #defines of do while,
1927 # or if closed on same line
1928 if (($line=~/$Type\s*$Ident\(.*\).*\s\{/) and
1929 !($line=~/\#\s*define.*do\s\{/) and !($line=~/}/)) {
1930 ERROR("open brace '{' following function declarations go on the next line\n" . $herecurr);
1933 # missing space after union, struct or enum definition
1934 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?(?:\s+$Ident)?[=\{]/) {
1935 ERROR("missing space after $1 definition\n" . $herecurr);
1938 # check for spacing round square brackets; allowed:
1939 # 1. with a type on the left -- int [] a;
1940 # 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
1941 # 3. inside a curly brace -- = { [0...10] = 5 }
1942 # 4. after a comma -- [1] = 5, [2] = 6
1943 # 5. in a macro definition -- #define abc(x) [x] = y
1944 while ($line =~ /(.*?\s)\[/g) {
1945 my ($where, $prefix) = ($-[1], $1);
1946 if ($prefix !~ /$Type\s+$/ &&
1947 ($where != 0 || $prefix !~ /^.\s+$/) &&
1948 $prefix !~ /\#\s*define[^(]*\([^)]*\)\s+$/ &&
1949 $prefix !~ /[,{:]\s+$/) {
1950 ERROR("space prohibited before open square bracket '['\n" . $herecurr);
1954 # check for spaces between functions and their parentheses.
1955 while ($line =~ /($Ident)\s+\(/g) {
1957 my $ctx_before = substr($line, 0, $-[1]);
1958 my $ctx = "$ctx_before$name";
1960 # Ignore those directives where spaces _are_ permitted.
1962 if|for|while|switch|return|case|
1963 volatile|__volatile__|coroutine_fn|
1964 __attribute__|format|__extension__|
1968 # Ignore 'catch (...)' in C++
1969 } elsif ($name =~ /^catch$/ && $realfile =~ /(\.cpp|\.h)$/) {
1971 # cpp #define statements have non-optional spaces, ie
1972 # if there is a space between the name and the open
1973 # parenthesis it is simply not a parameter group.
1974 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
1976 # cpp #elif statement condition may start with a (
1977 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
1979 # If this whole things ends with a type its most
1980 # likely a typedef for a function.
1981 } elsif ($ctx =~ /$Type$/) {
1984 ERROR("space prohibited between function name and open parenthesis '('\n" . $herecurr);
1987 # Check operator spacing.
1988 if (!($line=~/\#\s*include/)) {
1990 <<=|>>=|<=|>=|==|!=|
1991 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
1992 =>|->|<<|>>|<|>|=|!|~|
1993 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
1996 my @elements = split(/($ops|;)/, $opline);
1999 my $blank = copy_spacing($opline);
2001 for (my $n = 0; $n < $#elements; $n += 2) {
2002 $off += length($elements[$n]);
2004 # Pick up the preceding and succeeding characters.
2005 my $ca = substr($opline, 0, $off);
2007 if (length($opline) >= ($off + length($elements[$n + 1]))) {
2008 $cc = substr($opline, $off + length($elements[$n + 1]));
2010 my $cb = "$ca$;$cc";
2013 $a = 'V' if ($elements[$n] ne '');
2014 $a = 'W' if ($elements[$n] =~ /\s$/);
2015 $a = 'C' if ($elements[$n] =~ /$;$/);
2016 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
2017 $a = 'O' if ($elements[$n] eq '');
2018 $a = 'E' if ($ca =~ /^\s*$/);
2020 my $op = $elements[$n + 1];
2023 if (defined $elements[$n + 2]) {
2024 $c = 'V' if ($elements[$n + 2] ne '');
2025 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
2026 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
2027 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
2028 $c = 'O' if ($elements[$n + 2] eq '');
2029 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
2034 my $ctx = "${a}x${c}";
2036 my $at = "(ctx:$ctx)";
2038 my $ptr = substr($blank, 0, $off) . "^";
2039 my $hereptr = "$hereline$ptr\n";
2041 # Pull out the value of this operator.
2042 my $op_type = substr($curr_values, $off + 1, 1);
2044 # Get the full operator variant.
2045 my $opv = $op . substr($curr_vars, $off, 1);
2047 # Ignore operators passed as parameters.
2048 if ($op_type ne 'V' &&
2049 $ca =~ /\s$/ && $cc =~ /^\s*,/) {
2052 # } elsif ($op =~ /^$;+$/) {
2054 # ; should have either the end of line or a space or \ after it
2055 } elsif ($op eq ';') {
2056 if ($ctx !~ /.x[WEBC]/ &&
2057 $cc !~ /^\\/ && $cc !~ /^;/) {
2058 ERROR("space required after that '$op' $at\n" . $hereptr);
2062 } elsif ($op eq '//') {
2064 # Ignore : used in class declaration in C++
2065 } elsif ($opv eq ':B' && $ctx =~ /Wx[WE]/ &&
2066 $line =~ /class/ && $realfile =~ /(\.cpp|\.h)$/) {
2070 # : when part of a bitfield
2071 } elsif ($op eq '->' || $opv eq ':B') {
2072 if ($ctx =~ /Wx.|.xW/) {
2073 ERROR("spaces prohibited around that '$op' $at\n" . $hereptr);
2076 # , must have a space on the right.
2077 # not required when having a single },{ on one line
2078 } elsif ($op eq ',') {
2079 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/ &&
2080 ($elements[$n] . $elements[$n + 2]) !~ " *}\\{") {
2081 ERROR("space required after that '$op' $at\n" . $hereptr);
2084 # '*' as part of a type definition -- reported already.
2085 } elsif ($opv eq '*_') {
2086 #warn "'*' is part of type\n";
2088 # unary operators should have a space before and
2089 # none after. May be left adjacent to another
2090 # unary operator, or a cast
2091 } elsif ($op eq '!' || $op eq '~' ||
2092 $opv eq '*U' || $opv eq '-U' ||
2093 $opv eq '&U' || $opv eq '&&U') {
2094 if ($op eq '~' && $ca =~ /::$/ && $realfile =~ /(\.cpp|\.h)$/) {
2095 # '~' used as a name of Destructor
2097 } elsif ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
2098 ERROR("space required before that '$op' $at\n" . $hereptr);
2100 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
2101 # A unary '*' may be const
2103 } elsif ($ctx =~ /.xW/) {
2104 ERROR("space prohibited after that '$op' $at\n" . $hereptr);
2107 # unary ++ and unary -- are allowed no space on one side.
2108 } elsif ($op eq '++' or $op eq '--') {
2109 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
2110 ERROR("space required one side of that '$op' $at\n" . $hereptr);
2112 if ($ctx =~ /Wx[BE]/ ||
2113 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
2114 ERROR("space prohibited before that '$op' $at\n" . $hereptr);
2116 if ($ctx =~ /ExW/) {
2117 ERROR("space prohibited after that '$op' $at\n" . $hereptr);
2120 # A colon needs no spaces before when it is
2121 # terminating a case value or a label.
2122 } elsif ($opv eq ':C' || $opv eq ':L') {
2123 if ($ctx =~ /Wx./) {
2124 ERROR("space prohibited before that '$op' $at\n" . $hereptr);
2127 # All the others need spaces both sides.
2128 } elsif ($ctx !~ /[EWC]x[CWE]/) {
2131 if ($realfile =~ /\.cpp|\.h$/) {
2132 # Ignore template arguments <...> in C++
2133 if (($op eq '<' || $op eq '>') && $line =~ /<.*>/) {
2143 # Ignore email addresses <foo@bar>
2145 $cc =~ /^\S+\@\S+>/) ||
2147 $ca =~ /<\S+\@\S+$/))
2153 if (($opv eq ':O' && $ca =~ /\?$/) ||
2154 ($op eq '?' && $cc =~ /^:/)) {
2159 ERROR("spaces required around that '$op' $at\n" . $hereptr);
2162 $off += length($elements[$n + 1]);
2166 #need space before brace following if, while, etc
2167 if (($line =~ /\(.*\)\{/ && $line !~ /\($Type\)\{/) ||
2169 ERROR("space required before the open brace '{'\n" . $herecurr);
2172 # closing brace should have a space following it when it has anything
2174 if ($line =~ /}(?!(?:,|;|\)))\S/) {
2175 ERROR("space required after that close brace '}'\n" . $herecurr);
2178 # check spacing on square brackets
2179 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
2180 ERROR("space prohibited after that open square bracket '['\n" . $herecurr);
2182 if ($line =~ /\s\]/) {
2183 ERROR("space prohibited before that close square bracket ']'\n" . $herecurr);
2186 # check spacing on parentheses
2187 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
2188 $line !~ /for\s*\(\s+;/) {
2189 ERROR("space prohibited after that open parenthesis '('\n" . $herecurr);
2191 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
2192 $line !~ /for\s*\(.*;\s+\)/ &&
2193 $line !~ /:\s+\)/) {
2194 ERROR("space prohibited before that close parenthesis ')'\n" . $herecurr);
2197 if ($line =~ /^.\s*(Q(?:S?LIST|SIMPLEQ|TAILQ)_HEAD)\s*\(\s*[^,]/ &&
2198 $line !~ /^.typedef/) {
2199 ERROR("named $1 should be typedefed separately\n" . $herecurr);
2202 # Return needs parens
2203 if ($line =~ /^.\s*return [^(]/) {
2204 ERROR("parentheses required on return\n" . $herecurr);
2207 # Need a space before open parenthesis after if, while etc
2208 if ($line=~/\b(if|while|for|switch|return)\(/) {
2209 ERROR("space required before the open parenthesis '('\n" . $herecurr);
2212 # Check for illegal assignment in if conditional -- and check for trailing
2213 # statements after the conditional.
2214 if ($line =~ /do\s*(?!{)/) {
2215 my ($stat_next) = ctx_statement_block($line_nr_next,
2216 $remain_next, $off_next);
2217 $stat_next =~ s/\n./\n /g;
2218 ##print "stat<$stat> stat_next<$stat_next>\n";
2220 if ($stat_next =~ /^\s*while\b/) {
2221 # If the statement carries leading newlines,
2222 # then count those as offsets.
2224 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
2226 statement_rawlines($whitespace) - 1;
2228 $suppress_whiletrailers{$line_nr_next +
2232 if (!defined $suppress_whiletrailers{$linenr} &&
2233 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
2234 my ($s, $c) = ($stat, $cond);
2236 # if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
2237 # ERROR("do not use assignment in if condition\n" . $herecurr);
2240 # Find out what is on the end of the line after the
2242 substr($s, 0, length($c), '');
2244 $s =~ s/$;//g; # Remove any comments
2245 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
2246 $c !~ /}\s*while\s*/)
2248 # Find out how long the conditional actually is.
2249 my @newlines = ($c =~ /\n/gs);
2250 my $cond_lines = 1 + $#newlines;
2253 $stat_real = raw_line($linenr, $cond_lines)
2254 . "\n" if ($cond_lines);
2255 if (defined($stat_real) && $cond_lines > 1) {
2256 $stat_real = "[...]\n$stat_real";
2259 ERROR("trailing statements should be on next line\n" . $herecurr . $stat_real);
2263 # Check for bitwise tests written as boolean
2275 ERROR("boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
2278 # if and else should not have general statements after it
2279 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
2281 $s =~ s/$;//g; # Remove any comments
2282 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
2283 ERROR("trailing statements should be on next line\n" . $herecurr);
2286 # if should not continue a brace
2287 if ($line =~ /}\s*if\b/) {
2288 ERROR("trailing statements should be on next line\n" .
2291 # case and default should not have general statements after them
2292 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
2294 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
2298 ERROR("trailing statements should be on next line\n" . $herecurr);
2301 # Check for }<nl>else {, these must be at the same
2302 # indent level to be relevant to each other.
2303 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
2304 $previndent == $indent) {
2305 ERROR("else should follow close brace '}'\n" . $hereprev);
2308 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and
2309 $previndent == $indent) {
2310 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
2312 # Find out what is on the end of the line after the
2314 substr($s, 0, length($c), '');
2317 if ($s =~ /^\s*;/) {
2318 ERROR("while should follow close brace '}'\n" . $hereprev);
2322 #no spaces allowed after \ in define
2323 if ($line=~/\#\s*define.*\\\s$/) {
2324 ERROR("Whitespace after \\ makes next lines useless\n" . $herecurr);
2327 # multi-statement macros should be enclosed in a do while loop, grab the
2328 # first statement and ensure its the whole macro if its not enclosed
2329 # in a known good container
2330 if ($realfile !~ m@/vmlinux.lds.h$@ &&
2331 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
2334 my ($off, $dstat, $dcond, $rest);
2337 my $args = defined($1);
2339 # Find the end of the macro and limit our statement
2341 while ($cnt > 0 && defined $lines[$ln - 1] &&
2342 $lines[$ln - 1] =~ /^(?:-|..*\\$)/)
2344 $ctx .= $rawlines[$ln - 1] . "\n";
2345 $cnt-- if ($lines[$ln - 1] !~ /^-/);
2348 $ctx .= $rawlines[$ln - 1];
2350 ($dstat, $dcond, $ln, $cnt, $off) =
2351 ctx_statement_block($linenr, $ln - $linenr + 1, 0);
2352 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
2353 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
2355 # Extract the remainder of the define (if any) and
2356 # rip off surrounding spaces, and trailing \'s.
2358 while ($off != 0 || ($cnt > 0 && $rest =~ /\\\s*$/)) {
2359 #print "ADDING cnt<$cnt> $off <" . substr($lines[$ln - 1], $off) . "> rest<$rest>\n";
2360 if ($off != 0 || $lines[$ln - 1] !~ /^-/) {
2361 $rest .= substr($lines[$ln - 1], $off) . "\n";
2367 $rest =~ s/\\\n.//g;
2371 # Clean up the original statement.
2373 substr($dstat, 0, length($dcond), '');
2375 $dstat =~ s/^.\s*\#\s*define\s+$Ident\s*//;
2378 $dstat =~ s/\\\n.//g;
2379 $dstat =~ s/^\s*//s;
2380 $dstat =~ s/\s*$//s;
2382 # Flatten any parentheses and braces
2383 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
2384 $dstat =~ s/\{[^\{\}]*\}/1/ ||
2385 $dstat =~ s/\[[^\{\}]*\]/1/)
2389 my $exceptions = qr{
2401 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
2402 if ($rest ne '' && $rest ne ',') {
2403 if ($rest !~ /while\s*\(/ &&
2404 $dstat !~ /$exceptions/)
2406 ERROR("Macros with multiple statements should be enclosed in a do - while loop\n" . "$here\n$ctx\n");
2409 } elsif ($ctx !~ /;/) {
2411 $dstat !~ /^(?:$Ident|-?$Constant)$/ &&
2412 $dstat !~ /$exceptions/ &&
2413 $dstat !~ /^\.$Ident\s*=/ &&
2414 $dstat =~ /$Operators/)
2416 ERROR("Macros with complex values should be enclosed in parenthesis\n" . "$here\n$ctx\n");
2421 # check for missing bracing around if etc
2422 if ($line =~ /(^.*)\b(?:if|while|for)\b/ &&
2423 $line !~ /\#\s*if/) {
2426 # Check the pre-context.
2427 if ($line =~ /(\}.*?)$/) {
2430 if ($line !~ /else/) {
2431 print "APW: ALLOWED: pre<$pre> line<$line>\n"
2436 my ($level, $endln, @chunks) =
2437 ctx_statement_full($linenr, $realcnt, 1);
2439 print "APW: chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
2440 print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n"
2443 if ($#chunks >= 0 && $level == 0) {
2445 my $herectx = $here . "\n";
2446 my $ln = $linenr - 1;
2447 for my $chunk (@chunks) {
2448 my ($cond, $block) = @{$chunk};
2450 # If the condition carries leading newlines, then count those as offsets.
2451 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
2452 my $offset = statement_rawlines($whitespace) - 1;
2454 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
2456 # We have looked at and allowed this specific line.
2457 $suppress_ifbraces{$ln + $offset} = 1;
2459 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
2460 $ln += statement_rawlines($block) - 1;
2462 substr($block, 0, length($cond), '');
2464 my $spaced_block = $block;
2465 $spaced_block =~ s/\n\+/ /g;
2467 $seen++ if ($spaced_block =~ /^\s*\{/);
2469 print "APW: cond<$cond> block<$block> allowed<$allowed>\n"
2471 if (statement_lines($cond) > 1) {
2472 print "APW: ALLOWED: cond<$cond>\n"
2476 if ($block =~/\b(?:if|for|while)\b/) {
2477 print "APW: ALLOWED: block<$block>\n"
2481 if (statement_block_size($block) > 1) {
2482 print "APW: ALLOWED: lines block<$block>\n"
2487 $allowed=1; # disable for now.
2488 if ($seen != ($#chunks + 1) && !$allowed) {
2489 ERROR("braces {} are necessary for all arms of this statement\n" . $herectx);
2493 if (!defined $suppress_ifbraces{$linenr - 1} &&
2494 $line =~ /\b(if|while|for|else)\b/ &&
2495 $line !~ /\#\s*if/ &&
2496 $line !~ /\#\s*else/) {
2499 # Check the pre-context.
2500 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
2503 if ($line !~ /else/) {
2504 print "APW: ALLOWED: pre<$pre> line<$line>\n"
2510 my ($level, $endln, @chunks) =
2511 ctx_statement_full($linenr, $realcnt, $-[0]);
2513 # Check the condition.
2514 my ($cond, $block) = @{$chunks[0]};
2515 print "CHECKING<$linenr> cond<$cond> block<$block>\n"
2516 if $dbg_adv_checking;
2517 if (defined $cond) {
2518 substr($block, 0, length($cond), '');
2520 if (statement_lines($cond) > 1) {
2521 print "APW: ALLOWED: cond<$cond>\n"
2525 if ($block =~/\b(?:if|for|while)\b/) {
2526 print "APW: ALLOWED: block<$block>\n"
2530 if (statement_block_size($block) > 1) {
2531 print "APW: ALLOWED: lines block<$block>\n"
2535 # Check the post-context.
2536 if (defined $chunks[1]) {
2537 my ($cond, $block) = @{$chunks[1]};
2538 if (defined $cond) {
2539 substr($block, 0, length($cond), '');
2541 if ($block =~ /^\s*\{/) {
2542 print "APW: ALLOWED: chunk-1 block<$block>\n"
2547 print "DCS: level=$level block<$block> allowed=$allowed\n"
2549 if ($level == 0 && $block !~ /^\s*\{/ && !$allowed) {
2550 my $herectx = $here . "\n";;
2551 my $cnt = statement_rawlines($block);
2553 for (my $n = 0; $n < $cnt; $n++) {
2554 $herectx .= raw_line($linenr, $n) . "\n";;
2557 WARN("braces {} are encouraged even for single statement blocks\n" . $herectx);
2562 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
2563 ERROR("if this code is redundant consider removing it\n" .
2567 # Check that the storage class is at the beginning of a declaration
2568 if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
2569 ERROR("storage class should be at the beginning of the declaration\n" . $herecurr)
2572 # check the location of the inline attribute, that it is between
2573 # storage class and type.
2574 if ($line =~ /\b$Type\s+$Inline\b/ ||
2575 $line =~ /\b$Inline\s+$Storage\b/) {
2576 ERROR("inline keyword should sit between storage class and type\n" . $herecurr);
2579 # check for sizeof(&)
2580 if ($line =~ /\bsizeof\s*\(\s*\&/) {
2581 ERROR("sizeof(& should be avoided\n" . $herecurr);
2584 # check for new externs in .c files.
2585 if ($realfile =~ /\.c$/ && defined $stat &&
2586 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
2588 my $function_name = $1;
2589 my $paren_space = $2;
2592 if (defined $cond) {
2593 substr($s, 0, length($cond), '');
2595 if ($s =~ /^\s*;/ &&
2596 $function_name ne 'uninitialized_var')
2598 ERROR("externs should be avoided in .c files\n" . $herecurr);
2601 if ($paren_space =~ /\n/) {
2602 ERROR("arguments for function declarations should follow identifier\n" . $herecurr);
2605 } elsif ($realfile =~ /\.c$/ && defined $stat &&
2606 $stat =~ /^.\s*extern\s+/)
2608 ERROR("externs should be avoided in .c files\n" . $herecurr);
2611 # check for gcc specific __FUNCTION__
2612 if ($line =~ /__FUNCTION__/) {
2613 ERROR("__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr);
2616 # recommend sigaction over signal for portability, when establishing a handler
2617 if ($line =~ /\bsignal\s*\(/ && !($line =~ /SIG_(?:IGN|DFL)/)) {
2618 ERROR("use sigaction to establish signal handlers; signal is not portable\n" . $herecurr);
2621 # format strings checks
2623 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
2624 $string = substr($rawline, $-[1], $+[1] - $-[1]);
2625 $string =~ s/%%/__/g;
2626 # check for %L{u,d,i} in strings
2627 if ($string =~ /(?<!%)%L[udi]/) {
2628 ERROR("\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
2632 # Continue checking for error messages that contains newlines. This
2633 # check handles cases where string literals are spread over multiple lines.
2635 # error_report("Error msg line #1"
2636 # "Error msg line #2\n");
2637 my $quoted_newline_regex = qr{\+\s*\".*\\n.*\"};
2638 my $continued_str_literal = qr{\+\s*\".*\"};
2640 if ($rawline =~ /$quoted_newline_regex/) {
2641 # Backtrack to first line that does not contain only a quoted literal
2642 # and assume that it is the start of the statement.
2643 my $i = $linenr - 2;
2645 while (($i >= 0) & $rawlines[$i] =~ /$continued_str_literal/) {
2652 # If we have no input at all, then there is nothing to report on
2653 # so just keep quiet.
2654 if ($#rawlines == -1) {
2658 # In mailback mode only produce a report in the negative, for
2659 # things that appear to be patches.
2660 if ($mailback && ($clean == 1 || !$is_patch)) {
2664 # This is not a patch, and we are are in 'no-patch' mode so
2666 if (!$chk_patch && !$is_patch) {
2670 if (!$is_patch && $filename !~ /cover-letter\.patch$/) {
2671 ERROR("Does not appear to be a unified-diff format patch\n");
2674 print report_dump();
2675 if ($summary && !($clean == 1 && $quiet == 1)) {
2676 print "$filename " if ($summary_file);
2677 print "total: $cnt_error errors, $cnt_warn warnings, " .
2678 "$cnt_lines lines checked\n";
2679 print "\n" if ($quiet == 0);
2683 # If there were whitespace errors which cleanpatch can fix
2684 # then suggest that.
2685 # if ($rpt_cleaners) {
2686 # print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n";
2687 # print " scripts/cleanfile\n\n";
2691 if ($clean == 1 && $quiet == 0) {
2692 print "$vname has no obvious style problems and is ready for submission.\n"
2695 return ($no_warnings ? $clean : $cnt_error == 0);